commit a674cb4ef422ed595bf8473fc0b83cda4c471dc7 Author: FDB Dev Team Date: Thu May 25 13:48:44 2017 -0700 Initial repository commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..8ef40b8f3c --- /dev/null +++ b/.gitignore @@ -0,0 +1,89 @@ +# Build artifacts +/bin/ +/lib/ +/packages/ +bindings/flow/bin/ +bindings/java/foundationdb-client*.jar +bindings/java/foundationdb-tests*.jar +bindings/java/fdb-java-*-sources.jar +bindings/nodejs/build/ +bindings/nodejs/modules/ +packaging/msi/FDBInstaller.msi + +# Generated source, build, and packaging files +*.g.cpp +*.g.h +*.g.S +*.g.asm +*.pom +bindings/go/src/fdb/generated.go +bindings/java/pom*.xml +bindings/java/src*/main/overview.html +bindings/java/src*/main/com/apple/foundationdb/*Options.java +bindings/java/src*/main/com/apple/foundationdb/StreamingMode.java +bindings/java/src*/main/com/apple/foundationdb/MutationType.java +bindings/java/src*/main/com/apple/foundationdb/ConflictRangeType.java +bindings/java/src*/main/com/apple/foundationdb/FDBException.java +bindings/nodejs/package.json +bindings/python/fdb/fdb*options.py +bindings/python/dist/ +bindings/python/setup.py +bindings/python/MANIFEST +bindings/ruby/lib/fdboptions.rb +bindings/ruby/fdb.gemspec +fdbclient/vexillographer/obj/ +fdbrpc/hgVersion*.h +fdbrpc/libeio/config.h +flow/hgVersion*.h +generated.mk +versions.h +packaging/msi/FDBInstaller.wix* + +# Intermediate build artifacts and dependencies +*.class +*.py[cod] +*.sass-cache +.ccache +.deps/ +.objs/ +bindings/c/fdb_c.symbols +bindings/go/build +bindings/go/godoc +bindings/java/.classstamp* +bindings/java/classes*/ +bindings/java/javadoc*/ +bindings/nodejs/fdb_node.stamp +bindings/nodejs/node_modules/ + +# Testing and logging +bindings/nodejs/fdb_node*.log +bindings/nodejs/npm-debug.log +packaging/msi/*.log +packaging/msi/obj +simfdb +tests/oldBinaries +trace.*.xml + +# Editor files +*.iml +*.opensdf +*.sdf +*.suo +*.user +.idea/ +.project +.pydevproject +.vscode +FoundationDB.xcodeproj +foundationdb.VC.db +foundationdb.VC.VC.opendb +ipch/ + +# Temporary and user configuration files +*~ +*.orig +*.rej +*.swp +.envrc +.DS_Store +temp/ diff --git a/ACKNOWLEDGEMENTS b/ACKNOWLEDGEMENTS new file mode 100644 index 0000000000..dee3ad8d23 --- /dev/null +++ b/ACKNOWLEDGEMENTS @@ -0,0 +1,459 @@ +Acknowledgements +Portions of this FoundationDB Software may utilize the following copyrighted +material, the use of which is hereby acknowledged. + +_____________________ + +Mark Adler, Robert Važan (CRC-32C [Castagnoli] for C++ and .NET) + This license covers C++ code of the CRC-32C library as well as binaries generated + from it. It is standard zlib license. .NET code is distributed under BSD license. + + Copyright (c) 2013 - 2014 Mark Adler, Robert Važan + + This software is provided ‘as-is’, without any express or implied warranty. In no + event will the author be held liable for any damages arising from the use of this + software. + + Permission is granted to anyone to use this software for any purpose, including + commercial applications, and to alter it and redistribute it freely, subject to + the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not claim that + you wrote the original software. If you use this software in a product, an + acknowledgment in the product documentation would be appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source distribution. + +Steven J. Bethard (argparse.py from https://code.google.com/p/argparse/) + argparse is licensed under the Python license, see: + https://code.google.com/p/argparse/source/browse/LICENSE.txt and + https://code.google.com/p/argparse/source/browse/doc/source/Python-License.txt + +Russ Cox (asm.S from libcoroutine) + This software was developed as part of a project at MIT. + + Copyright (c) 2005-2007 Russ Cox, + Massachusetts Institute of Technology + + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Beman Dawes, Christopher M. Kohlhoff (Boost) + The Boost License (http://www.boost.org/users/license.html) does not require + attribution or republication when Boost is redistributed in object code form. + +Steve Dekorte (libcoroutine) + Copyright (c) 2002, 2003 Steve Dekorte + All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + Â¥ Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + + Â¥ Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimer in the documentation and/or + other materials provided with the distribution. + + Â¥ Neither the name of the author nor the names of other contributors may be used + to endorse or promote products derived from this software without specific + prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Jean-loup Gailly, Mark Adler (zlib) + Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + +The Go Authors (Go Tools) + Copyright (c) 2009 The Go Authors. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following disclaimer + in the documentation and/or other materials provided with the + distribution. + * Neither the name of Google Inc. nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Bob Jenkins (lookup3.c) + By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this + code any way you wish, private, educational, or commercial. Its free. + +JS Foundation and other contributors (JQuery) + Permission is hereby granted, free of charge, to any person obtaining + a copy of this software and associated documentation files (the + "Software"), to deal in the Software without restriction, including + without limitation the rights to use, copy, modify, merge, publish, + distribute, sublicense, and/or sell copies of the Software, and to + permit persons to whom the Software is furnished to do so, subject to + the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +Marcin Kalicinski (RapidXML) + Copyright (c) 2006, 2007 Marcin Kalicinski + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + of the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + IN THE SOFTWARE. + +Marc Alexander Lehmann (Libeio) + All files in libeio are Copyright (C)2007,2008 Marc Alexander Lehmann. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Microsoft Corporation (CppWindowsService) + Microsoft Public License (Ms-PL) + + This license governs use of the accompanying software. If you use the software, + you accept this license. If you do not accept the license, do not use the + software. + + Definitions + The terms "reproduce," "reproduction," "derivative works," and "distribution" + have the same meaning here as under U.S. copyright law. + + A "contribution" is the original software, or any additions or changes to the + software. + A "contributor" is any person that distributes its contribution under this + license. + "Licensed patents" are a contributor's patent claims that read directly on its + contribution. + + Grant of Rights + (A) Copyright Grant- Subject to the terms of this license, including the license + conditions and limitations in section 3, each contributor grants you a + non-exclusive, worldwide, royalty-free copyright license to reproduce its + contribution, prepare derivative works of its contribution, and distribute its + contribution or any derivative works that you create. + (B) Patent Grant- Subject to the terms of this license, including the license + conditions and limitations in section 3, each contributor grants you a + non-exclusive, worldwide, royalty-free license under its licensed patents to + make, have made, use, sell, offer for sale, import, and/or otherwise dispose of + its contribution in the software or derivative works of the contribution in the + software. + + Conditions and Limitations + (A) No Trademark License- This license does not grant you rights to use any + contributors' name, logo, or trademarks. + (B) If you bring a patent claim against any contributor over patents that you + claim are infringed by the software, your patent license from such contributor + to the software ends automatically. + (C) If you distribute any portion of the software, you must retain all + copyright, patent, trademark, and attribution notices that are present in the + software. + (D) If you distribute any portion of the software in source code form, you may + do so only under this license by including a complete copy of this license with + your distribution. If you distribute any portion of the software in compiled or + object code form, you may only do so under a license that complies with this + license. + (E) The software is licensed "as-is." You bear the risk of using it. The + contributors give no express warranties, guarantees, or conditions. You may have + additional consumer rights under your local laws which this license cannot + change. To the extent permitted under your local laws, the contributors exclude + the implied warranties of merchantability, fitness for a particular purpose and + non-infringement. + +Marcel Moolenaar (amd64-ucontext.h) + Copyright (c) 1999 Marcel Moolenaar + + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer + in this position and unchanged. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +Alexander Peslyak (md5) + Copyright (c) 2001 Alexander Peslyak and it is hereby released to the + general public under the following terms: + + Redistribution and use in source and binary forms, with or without + modification, are permitted. + + There's ABSOLUTELY NO WARRANTY, express or implied. + +Steve Reid, Bruce Guenter, Volker Grabsch + 100% Public Domain. + +Salvatore Sanfilippo, Pieter Noordhuis (Linenoise) + Copyright (c) 2010-2014, Salvatore Sanfilippo + Copyright (c) 2010-2013, Pieter Noordhuis + + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +SQLite authors (SQLite) + All of the code and documentation in SQLite has been dedicated to the public + domain by the authors. All code authors, and representatives of the companies + they work for, have signed affidavits dedicating their contributions to the + public domain and originals of those signed affidavits are stored in a + firesafe at the main offices of Hwaci. Anyone is free to copy, modify, publish, + use, compile, sell, or distribute the original SQLite code, either in source + code form or as a compiled binary, for any purpose, commercial or + non-commercial, and by any means. + +The Abseil Authors (Abseil) + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +Brodie Thiesfield (SimpleIni, SimpleOpt) + Copyright (c) 2006-2012, Brodie Thiesfield + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +THL A29 Limited, Milo Yip (RapidJSON) + Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. + + Licensed under the MIT License (the "License"); you may not use this file except + in compliance with the License. You may obtain a copy of the License at + + http://opensource.org/licenses/MIT + + Unless required by applicable law or agreed to in writing, software distributed + under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. See the License for the + specific language governing permissions and limitations under the License. + +Unicode, Inc. (ConvertUTF from SimpleIni) + Copyright 2001-2004 Unicode, Inc. + + Disclaimer + + This source code is provided as is by Unicode, Inc. No claims are + made as to fitness for any particular purpose. No warranties of any + kind are expressed or implied. The recipient agrees to determine + applicability of information provided. If this file has been + purchased on magnetic or optical media from Unicode, Inc., the + sole remedy for any claim will be exchange of defective media + within 90 days of receipt. + + Limitations on Rights to Redistribute This Code + + Unicode, Inc. hereby grants the right to freely use the information + supplied in this file in the creation of products supporting the + Unicode Standard, and to make copies of this file in any form + for internal or external distribution as long as this notice + remains attached. + +Chris Venter (libb64) + The libb64 project has been placed in the public domain. + +Dmitry Vyukov (Intrusive MPSC node-based queue) + Copyright (c) 2010-2011 Dmitry Vyukov. All rights reserved. + + Redistribution and use in source and binary forms, with or without modification, + are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY DMITRY VYUKOV "AS IS" AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT + SHALL DMITRY VYUKOV OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +John W. Wilkinson (JSON Spirit) + Copyright (c) 2007 - 2010 John W. Wilkinson + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..19586598a8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,207 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +------------------------------------------------------------------------------- +SOFTWARE DISTRIBUTED WITH FOUNDATIONDB: + +The FoundationDB software includes a number of subcomponents with separate +copyright notices and license terms - please see the file ACKNOWLEDGEMENTS. +------------------------------------------------------------------------------- diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..de59197f71 --- /dev/null +++ b/Makefile @@ -0,0 +1,192 @@ +export +PLATFORM := $(shell uname) +ARCH := $(shell uname -m) + +TOPDIR := $(shell pwd) + +ifeq ($(ARCH),x86_64) + ARCH := x64 +else + $(error Not prepared to compile on $(ARCH)) +endif + +MONO := $(shell which mono) +ifeq ($(MONO),) + MONO := /usr/bin/mono +endif + +MCS := $(shell which dmcs) +ifeq ($(MCS),) + MCS := /usr/bin/dmcs +endif + +CFLAGS := -Werror -Wno-error=format -fPIC -DNO_INTELLISENSE -fvisibility=hidden -DNDEBUG=1 -Wreturn-type +ifeq ($(RELEASE),true) + CFLAGS += -DFDB_CLEAN_BUILD +endif +ifeq ($(NIGHTLY),true) + CFLAGS += -DFDB_CLEAN_BUILD +endif + +ifeq ($(PLATFORM),Linux) + PLATFORM := linux + + CC ?= gcc + CXX ?= g++ + + CXXFLAGS += -std=c++0x + + BOOSTDIR ?= /opt/boost_1_52_0 + DLEXT := so + java_DLEXT := so + TARGET_LIBC_VERSION ?= 2.11 +else ifeq ($(PLATFORM),Darwin) + PLATFORM := osx + + CC := /usr/bin/clang + CXX := /usr/bin/clang + + CFLAGS += -mmacosx-version-min=10.7 -stdlib=libc++ + CXXFLAGS += -std=c++11 -stdlib=libc++ -msse4.2 -Wno-undefined-var-template -Wno-unknown-warning-option + + .LIBPATTERNS := lib%.dylib lib%.a + + BOOSTDIR ?= $(HOME)/boost_1_52_0 + DLEXT := dylib + java_DLEXT := jnilib +else + $(error Not prepared to compile on platform $(PLATFORM)) +endif + +CCACHE := $(shell which ccache) +ifneq ($(CCACHE),) + CCACHE_CC := $(CCACHE) $(CC) + CCACHE_CXX := $(CCACHE) $(CXX) +else + CCACHE_CC := $(CC) + CCACHE_CXX := $(CXX) +endif + +ACTORCOMPILER := bin/actorcompiler.exe + +# UNSTRIPPED := 1 + +# Normal optimization level +CFLAGS += -O2 + +# Or turn off optimization entirely +# CFLAGS += -O0 + +# Debugging symbols are a good thing (and harmless, since we keep them +# in external debug files) +CFLAGS += -g + +# valgrind-compatibile builds are enabled by uncommenting lines in valgind.mk + +CXXFLAGS += -Wno-deprecated +LDFLAGS := +LIBS := +STATIC_LIBS := + +# Add library search paths (that aren't -Llib) to the VPATH +VPATH += $(addprefix :,$(filter-out lib,$(patsubst -L%,%,$(filter -L%,$(LDFLAGS))))) + +CS_PROJECTS := flow/actorcompiler flow/coveragetool fdbclient/vexillographer +CPP_PROJECTS := flow fdbrpc fdbclient fdbbackup fdbserver fdbcli bindings/c bindings/java fdbmonitor bindings/flow/tester bindings/flow +OTHER_PROJECTS := bindings/python bindings/ruby bindings/nodejs bindings/go + +CS_MK_GENERATED := $(CS_PROJECTS:=/generated.mk) +CPP_MK_GENERATED := $(CPP_PROJECTS:=/generated.mk) + +MK_GENERATED := $(CS_MK_GENERATED) $(CPP_MK_GENERATED) + +# build/valgrind.mk needs to be included before any _MK_GENERATED (which in turn includes local.mk) +MK_INCLUDE := build/scver.mk build/valgrind.mk $(CS_MK_GENERATED) $(CPP_MK_GENERATED) $(OTHER_PROJECTS:=/include.mk) build/packages.mk + +ALL_MAKEFILES := Makefile $(MK_INCLUDE) $(patsubst %/generated.mk,%/local.mk,$(MK_GENERATED)) + +TARGETS = + +.PHONY: clean all Makefiles + +default: fdbserver fdbbackup fdbcli fdb_c fdb_python fdb_python_sdist + +all: $(CS_PROJECTS) $(CPP_PROJECTS) $(OTHER_PROJECTS) + +# These are always defined and ready to use. Any target that uses them and needs them up to date +# should depend on versions.target +VERSION := $(shell cat versions.target | grep '' | sed -e 's,^[^>]*>,,' -e 's,<.*,,') +PACKAGE_NAME := $(shell cat versions.target | grep '' | sed -e 's,^[^>]*>,,' -e 's,<.*,,') + +versions.h: Makefile versions.target + @rm -f $@ +ifeq ($(RELEASE),true) + @echo "#define FDB_VT_VERSION \"$(VERSION)\"" >> $@ +else + @echo "#define FDB_VT_VERSION \"$(VERSION)-PRERELEASE\"" >> $@ +endif + @echo "#define FDB_VT_PACKAGE_NAME \"$(PACKAGE_NAME)\"" >> $@ + +bindings: fdb_c fdb_python fdb_ruby fdb_java fdb_java-completable fdb_node fdb_flow fdb_flow_tester fdb_go fdb_go_tester + +Makefiles: $(MK_GENERATED) + +$(CS_MK_GENERATED): build/csprojtom4.py build/csproj.mk Makefile + @echo "Creating $@" + @python build/csprojtom4.py $(@D)/*.csproj | m4 -DGENDIR="$(@D)" -DGENNAME=`basename $(@D)/*.csproj .csproj` - build/csproj.mk > $(@D)/generated.mk + +$(CPP_MK_GENERATED): build/vcxprojtom4.py build/vcxproj.mk Makefile + @echo "Creating $@" + @python build/vcxprojtom4.py $(@D)/*.vcxproj | m4 -DGENDIR="$(@D)" -DGENNAME=`basename $(@D)/*.vcxproj .vcxproj` - build/vcxproj.mk > $(@D)/generated.mk + +DEPSDIR := .deps +OBJDIR := .objs + +include $(MK_INCLUDE) + +clean: $(CLEAN_TARGETS) + @echo "Cleaning toplevel" + @rm -rf $(OBJDIR) + @rm -rf $(DEPSDIR) + @rm -rf lib/libstdc++.a + @rm -rf bin/coverage.*.xml + +targets: + @echo "Available targets:" + @for i in $(sort $(TARGETS)); do echo " $$i" ; done + @echo "Append _clean to clean specific target." + +lib/libstdc++.a: $(shell $(CC) -print-file-name=libstdc++_pic.a) + @echo "Frobnicating $@" + @mkdir -p lib + @rm -rf .libstdc++ + @mkdir .libstdc++ + @(cd .libstdc++ && ar x $<) + @for i in .libstdc++/*.o ; do \ + nm $$i | grep -q \@ || continue ; \ + nm $$i | awk '$$3 ~ /@@/ { COPY = $$3; sub(/@@.*/, "", COPY); print $$3, COPY; }' > .libstdc++/replacements ; \ + objcopy --redefine-syms=.libstdc++/replacements $$i $$i.new && mv $$i.new $$i ; \ + rm .libstdc++/replacements ; \ + nm $$i | awk '$$3 ~ /@/ { print $$3; }' > .libstdc++/deletes ; \ + objcopy --strip-symbols=.libstdc++/deletes $$i $$i.new && mv $$i.new $$i ; \ + rm .libstdc++/deletes ; \ + done + @ar rcs $@ .libstdc++/*.o + @rm -r .libstdc++ + + +ifeq ($(PLATFORM),osx) + MD5SUM=md5 +else + MD5SUM=md5sum +endif + +.SECONDEXPANSION: + +bin/coverage.%.xml: bin/coveragetool.exe $$(%_ALL_SOURCES) + @echo "Creating $@" + @$(MONO) bin/coveragetool.exe $@ $(filter-out $<,$^) >/dev/null + +$(CPP_MK_GENERATED): $$(@D)/*.vcxproj + +$(CS_MK_GENERATED): $$(@D)/*.csproj diff --git a/README.md b/README.md new file mode 100755 index 0000000000..68edc85987 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# FoundationDB + +FoundationDB is a distributed database designed to handle large volumes of structured data across clusters of commodity servers. It organizes data as an ordered key-value store and employs ACID transactions for all operations. It is especially well-suited for read/write workloads but also has excellent performance for write-intensive workloads. Users interact with the database using API language binding. diff --git a/bindings/__init__.py b/bindings/__init__.py new file mode 100644 index 0000000000..c7f6e4d441 --- /dev/null +++ b/bindings/__init__.py @@ -0,0 +1,20 @@ +# +# __init__.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + diff --git a/bindings/bindingtester/__init__.py b/bindings/bindingtester/__init__.py new file mode 100644 index 0000000000..9b164ee812 --- /dev/null +++ b/bindings/bindingtester/__init__.py @@ -0,0 +1,94 @@ +# +# __init__.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import sys +import os + +sys.path[:0]=[os.path.join(os.path.dirname(__file__), '..', '..', 'bindings', 'python')] + +import util + +FDB_API_VERSION = 500 + +LOGGING = { + 'version' : 1, + 'disable_existing_loggers' : False, + 'formatters' : { + 'simple' : { + 'format' : '%(message)s' + } + }, + 'handlers' : { + 'console' : { + 'level' : 'NOTSET', + 'class' : 'logging.StreamHandler', + 'stream' : sys.stdout, + 'formatter' : 'simple' + } + }, + 'loggers' : { + 'foundationdb.bindingtester' : { + 'level' : 'INFO', + 'handlers' : ['console'] + } + } +} + +class Result: + def __init__(self, subspace, key, values): + self.subspace_tuple = util.subspace_to_tuple(subspace) + self.key_tuple = subspace.unpack(key) + self.values = values + + def matches(self, rhs, specification): + if not isinstance(rhs, Result): + return False + + left_key = self.key_tuple[specification.key_start_index:] + right_key = self.key_tuple[specification.key_start_index:] + + if len(left_key) != len(right_key) or left_key != right_key: + return False + + for value in self.values: + for rValue in rhs.values: + if value == rValue: + return True + + return False + + def matches_global_error_filter(self, specification): + return any([specification.matches_global_error_filter(v) for v in self.values]) + + # A non-unique sequence of numbers used to align results from different testers + def sequence_num(self, specification): + if specification.ordering_index is not None: + return self.key_tuple[specification.ordering_index] + + return None + + def __str__(self): + if len(self.values) == 1: + value_str = repr(self.values[0]) + else: + value_str = repr(self.values) + + return '%s = %s' % (repr(self.subspace_tuple + self.key_tuple), value_str) + diff --git a/bindings/bindingtester/bindingtester.py b/bindings/bindingtester/bindingtester.py new file mode 100755 index 0000000000..0a19832d60 --- /dev/null +++ b/bindings/bindingtester/bindingtester.py @@ -0,0 +1,450 @@ +#!/usr/bin/env python +# +# bindingtester.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +import sys +import subprocess +import struct +import random +import argparse +import math +import os +import copy +import traceback +from threading import Timer, Event + +import logging.config + +from collections import OrderedDict + +sys.path[:0]=[os.path.join(os.path.dirname(__file__), '..')] + +import bindingtester + +from bindingtester import FDB_API_VERSION +from bindingtester import Result + +from bindingtester import util +from bindingtester.tests import Test, InstructionSet + +from known_testers import Tester + +import fdb +import fdb.tuple + +fdb.api_version(FDB_API_VERSION) + +class ResultSet(object): + def __init__(self, specification): + self.specification = specification + self.tester_results = OrderedDict() + + def add(self, name, results): + num = 1 + base_name = name + while name in self.tester_results: + num += 1 + name = '%s (%d)' % (base_name, num) + + self.tester_results[name] = results + + def check_for_errors(self): + if len(self.tester_results) == 1: + return (0, False) + + util.get_logger().info('Comparing results from \'%s\'...' % repr(util.subspace_to_tuple(self.specification.subspace))) + + num_errors = 0 + indices = [0 for i in range(len(self.tester_results))] + + name_length = max([len(name) for name in self.tester_results.keys()]) + + has_filtered_error = False + + while True: + results = { i : r[indices[i]] for i, r in enumerate(self.tester_results.values()) if len(r) > indices[i] } + if len(results) == 0: + break + + sequence_nums = [ r.sequence_num(self.specification) for r in results.values() ] + if any([s is not None for s in sequence_nums]): + results = { i : r for i, r in results.items() if r.sequence_num(self.specification) == min(sequence_nums) } + else: + results = { i : r for i, r in results.items() if r.matches(min(results.values()), self.specification) } + + for i in results.keys(): + indices[i] += 1 + + all_results = { i : results[i] if i in results else None for i in range(len(self.tester_results)) } + result_str = '\n'.join([' %-*s - %s' % (name_length, self.tester_results.keys()[i], r) for i, r in all_results.items()]) + + result_list = results.values() + if any(r.matches_global_error_filter(self.specification) for r in result_list): + has_filtered_error = True + + if len(results) < len(all_results) or not all(result_list[0].matches(r, self.specification) for r in result_list): + util.get_logger().error('\nIncorrect result: \n%s' % result_str) + num_errors += 1 + else: + util.get_logger().debug('\nCorrect result: \n%s' % result_str) + + if num_errors > 0: + util.get_logger().error('') + else: + util.get_logger().debug('') + + return (num_errors, has_filtered_error) + +def choose_api_version(selected_api_version, tester_min_version, tester_max_version, test_min_version, test_max_version): + if selected_api_version is not None: + if selected_api_version < tester_min_version or selected_api_version > tester_max_version: + raise Exception('Not all testers support the API version %d (min=%d, max=%d)' % (selected_api_version, tester_min_version, tester_max_version)) + elif selected_api_version < test_min_version or selected_api_version > test_max_version: + raise Exception('API version %d is not supported by the specified test (min=%d, max=%d)' % (selected_api_version, test_min_version, test_max_version)) + + api_version = selected_api_version + else: + min_version = max(tester_min_version, test_min_version) + max_version = min(tester_max_version, test_max_version) + + if min_version > max_version: + raise Exception('Not all testers support the API versions required by the specified test (tester: min=%d, max=%d; test: min=%d, max=%d)' % (tester_min_version, tester_max_version, test_min_version, test_max_version)) + + if random.random() < 0.7: + api_version = max_version + elif random.random() < 0.7: + api_version = min_version + elif random.random() < 0.9: + api_version = random.choice([v for v in [13, 14, 16, 21, 22, 23, 100, 200, 300, 400, 410, 420, 430, 440, 450, 460, 500] if v >= min_version and v <= max_version]) + else: + api_version = random.randint(min_version, max_version) + + return api_version + +class TestRunner(object): + def __init__(self, args): + self.args = copy.copy(args) + + self.db = fdb.open(self.args.cluster_file) + self.test_seed = random.randint(0, 0xffffffff) + + self.testers = [Tester.get_test(self.args.test1)] + if self.args.test2 is not None: + self.testers.append(Tester.get_test(self.args.test2)) + + self.test = Test.create_test(self.args.test_name, fdb.Subspace((self.args.output_subspace,))) + + if self.test is None: + raise Exception('the test \'%s\' could not be found' % self.args.test_name) + + min_api_version = max([tester.min_api_version for tester in self.testers]) + max_api_version = min([tester.max_api_version for tester in self.testers]) + self.args.api_version = choose_api_version(self.args.api_version, min_api_version, max_api_version, self.test.min_api_version, self.test.max_api_version) + + util.get_logger().info('\nCreating test at API version %d' % self.args.api_version) + + max_int_bits = min([tester.max_int_bits for tester in self.testers]) + if self.args.max_int_bits is None: + self.args.max_int_bits = max_int_bits + elif self.args.max_int_bits > max_int_bits: + raise Exception('The specified testers support at most %d-bit ints, but --max-int-bits was set to %d' % (max_int_bits, self.args.max_int_bits)) + + self.args.no_threads = self.args.no_threads or any([not tester.threads_enabled for tester in self.testers]) + if self.args.no_threads and self.args.concurrency > 1: + raise Exception('Not all testers support concurrency') + + + def print_test(self): + test_instructions = self._generate_test() + + for top_level_subspace, top_level_thread in test_instructions.items(): + for subspace, thread in top_level_thread.get_threads(top_level_subspace).items(): + util.get_logger().error('\nThread at prefix %r:' % util.subspace_to_tuple(subspace)) + if self.args.print_all: + instructions = thread + offset = 0 + else: + instructions = thread.core_instructions() + offset = thread.core_test_begin + + for i, instruction in enumerate(instructions): + if self.args.print_all or (instruction.operation != 'SWAP' and instruction.operation != 'PUSH'): + util.get_logger().error(' %d. %r' % (i+offset, instruction)) + + util.get_logger().error(''); + + def run_test(self): + test_instructions = self._generate_test() + expected_results = self.test.get_expected_results() + + tester_results = { s.subspace : ResultSet(s) for s in self.test.get_result_specifications() } + for subspace, results in expected_results.items(): + tester_results[subspace].add('expected', results) + + tester_errors = {} + + for tester in self.testers: + self._insert_instructions(test_instructions) + self.test.pre_run(self.db, self.args) + return_code = self._run_tester(tester) + if return_code != 0: + util.get_logger().error('Test of type %s failed to complete successfully with random seed %d and %d operations\n' % (self.args.test_name, self.args.seed, self.args.num_ops)) + return 2 + + tester_errors[tester] = self.test.validate(self.db, self.args) + + for spec in self.test.get_result_specifications(): + tester_results[spec.subspace].add(tester.name, self._get_results(spec.subspace)) + + return_code = self._validate_results(tester_errors, tester_results) + util.get_logger().info('Completed %s test with random seed %d and %d operations\n' % (self.args.test_name, self.args.seed, self.args.num_ops)) + + return return_code + + def insert_test(self): + test_instructions = self._generate_test() + self._insert_instructions(test_instructions) + + def _generate_test(self): + util.get_logger().info('Generating %s test at seed %d with %d op(s) and %d concurrent tester(s)...' % (self.args.test_name, self.args.seed, self.args.num_ops, self.args.concurrency)) + + random.seed(self.test_seed) + + if self.args.concurrency == 1: + self.test.setup(self.args) + test_instructions = { fdb.Subspace((self.args.instruction_prefix,)) : self.test.generate(self.args, 0) } + else: + test_instructions = {} + main_thread = InstructionSet() + for i in range(self.args.concurrency): + #thread_spec = fdb.Subspace(('thread_spec', i)) + thread_spec = 'thread_spec%d' % i + main_thread.push_args(thread_spec) + main_thread.append('START_THREAD') + self.test.setup(self.args) + test_instructions[fdb.Subspace((thread_spec,))] = self.test.generate(self.args, i) + + test_instructions[fdb.Subspace((self.args.instruction_prefix,))] = main_thread + + return test_instructions + + def _insert_instructions(self, test_instructions): + util.get_logger().info('\nInserting test into database...') + del self.db[:] + for subspace, thread in test_instructions.items(): + thread.insert_operations(self.db, subspace) + + def _run_tester(self, test): + params = test.cmd.split(' ') + [self.args.instruction_prefix, str(self.args.api_version)] + if self.args.cluster_file is not None: + params += [self.args.cluster_file] + + util.get_logger().info('\nRunning tester \'%s\'...' % ' '.join(params)) + sys.stdout.flush(); + proc = subprocess.Popen(params) + timed_out = Event() + + def killProc(): + proc.kill() + timed_out.set() + + timer = Timer(self.args.timeout, killProc) + try: + timer.start() + ret_code = proc.wait() + except Exception as e: + raise Exception('Unable to run tester (%s)' % e) + finally: + timer.cancel() + + if ret_code != 0: + signal_name = str(ret_code) + if ret_code < 0: + signal_name = util.signal_number_to_name(-ret_code) + + reason = 'exit code: %s' % (signal_name,) + if timed_out.is_set(): + reason = 'timed out after %d seconds' % (self.args.timeout,) + util.get_logger().error('\n\'%s\' did not complete succesfully (%s)' % (params[0], reason)) + + util.get_logger().info('') + return ret_code + + def _get_results(self, subspace, instruction_index=None): + util.get_logger().info('Reading results from \'%s\'...' % repr(util.subspace_to_tuple(subspace))) + + results = [] + next_key = subspace.range().start + while True: + next_results = self.db.get_range(next_key, subspace.range().stop, 1000) + if len(next_results) == 0: + break + + results += [Result(subspace, kv.key, (kv.value,)) for kv in next_results] + next_key = fdb.KeySelector.first_greater_than(next_results[-1].key) + + return results + + def _validate_results(self, tester_errors, tester_results): + util.get_logger().info('') + + num_incorrect = 0 + has_filtered_error = False + for r in tester_results.values(): + (count, filtered_error) = r.check_for_errors() + num_incorrect += count + has_filtered_error = has_filtered_error or filtered_error + + num_errors = sum([len(e) for e in tester_errors.values()]) + + for tester, errors in tester_errors.items(): + if len(errors) > 0: + util.get_logger().error('The %s tester reported errors:\n' % tester.name) + for i, error in enumerate(errors): + util.get_logger().error(' %d. %s' % (i+1, error)) + + log_message = '\nTest with seed %d and concurrency %d had %d incorrect result(s) and %d error(s) at API version %d' % (self.args.seed, self.args.concurrency, num_incorrect, num_errors, self.args.api_version) + if num_errors == 0 and (num_incorrect == 0 or has_filtered_error): + util.get_logger().info(log_message) + if has_filtered_error: + util.get_logger().info("Test had permissible non-deterministic errors; disregarding results...") + return 0 + else: + util.get_logger().error(log_message) + return 1 + +def bisect(test_runner, args): + util.get_logger().info('') + + lower_bound = 0 + upper_bound = args.num_ops + + while True: + test_runner.args.num_ops = int((lower_bound + upper_bound) / 2) + result = test_runner.run_test() + + if lower_bound == upper_bound: + if result != 0: + util.get_logger().error('Found minimal failing test with %d operations' % lower_bound) + if args.print_test: + test_runner.print_test() + + return 0 + elif upper_bound < args.num_ops: + util.get_logger().error('Error finding minimal failing test for seed %d. The failure may not be deterministic' % args.seed) + return 1 + else: + util.get_logger().error('No failing test found for seed %d with %d ops. Try specifying a larger --num-ops parameter.' % (args.seed, args.num_ops)) + return 0 + + elif result == 0: + util.get_logger().info('Test with %d operations succeeded\n' % test_runner.args.num_ops) + lower_bound = test_runner.args.num_ops + 1 + + else: + util.get_logger().info('Test with %d operations failed with error code %d\n' % (test_runner.args.num_ops, result)) + upper_bound = test_runner.args.num_ops + +def parse_args(argv): + parser = argparse.ArgumentParser(description='FoundationDB Binding API Tester') + parser.add_argument('--test-name', default='scripted', help='The name of the test to run. Must be the name of a test specified in the tests folder. (default=\'scripted\')') + + parser.add_argument(metavar='tester1', dest='test1', help='Name of the first tester to invoke') + parser.add_argument('--compare', metavar='tester2', nargs='?', type=str, default=None, const='python', dest='test2', help='When specified, a second tester will be run and compared against the first. This flag takes an optional argument for the second tester to invoke (default = \'python\').') + + parser.add_argument('--print-test', action='store_true', help='Instead of running a test, prints the set of instructions generated for that test. Unless --all is specified, all setup, finalization, PUSH, and SWAP instructions will be excluded.') + parser.add_argument('--all', dest='print_all', action='store_true', help='Causes --print-test to print all instructions.') + parser.add_argument('--bisect', action='store_true', help='Run the specified test varying the number of operations until a minimal failing test is found. Does not work for concurrent tests.') + parser.add_argument('--insert-only', action='store_true', help='Insert the test instructions into the database, but do not run it.') + + parser.add_argument('--concurrency', type=int, default=1, help='Number of concurrent test threads to run. (default = 1).') + parser.add_argument('--num-ops', type=int, default=100, help='The number of operations to generate per thread (default = 100)') + parser.add_argument('--seed', type=int, help='The random seed to use for generating the test') + parser.add_argument('--max-int-bits', type=int, default=None, help='Maximum number of bits to use for int types in testers. By default, the largest value supported by the testers being run will be chosen.') + parser.add_argument('--api-version', default=None, type=int, help='The API version that the testers should use. Not supported in scripted mode. (default = random version supported by all testers)') + parser.add_argument('--cluster-file', type=str, default=None, help='The cluster file for the cluster being connected to. (default None)') + parser.add_argument('--timeout', type=int, default=600, help='The timeout in seconds for running each individual tester. (default 600)') + parser.add_argument('--enable-client-trace-logging', nargs='?', type=str, default=None, const='.', help='Enables trace file output. This flag takes an optional argument specifying the output directory (default = \'.\').') + parser.add_argument('--instruction-prefix', type=str, default='test_spec', help='The prefix under which the main thread of test instructions are inserted (default=\'test_spec\').') + parser.add_argument('--output-subspace', type=str, default='tester_output', help='The string used to create the output subspace for the testers. The subspace will be of the form (,). (default=\'tester_output\')') + + parser.add_argument('--logging-level', type=str, default='INFO', choices=['ERROR', 'WARNING', 'INFO', 'DEBUG'], help='Specifies the level of detail in the tester output (default=\'INFO\').') + + # SOMEDAY: this applies only to the scripted test. Should we invoke test files specifically (as in circus), + # or invoke them here and allow tests to add arguments? + parser.add_argument('--no-threads', action='store_true', help='Disables the START_THREAD instruction in the scripted test.') + + return parser.parse_args(argv) + +def validate_args(args): + if args.insert_only and args.bisect: + raise Exception('--bisect cannot be used with --insert-only') + if args.print_all and not args.print_test: + raise Exception('cannot specify --all without --print-test') + if args.bisect and not args.seed: + raise Exception('--seed must be specified if using --bisect') + if args.concurrency < 1: + raise Exception('--concurrency must be a positive integer') + if args.concurrency > 1 and args.test2: + raise Exception('--compare cannot be used with concurrent tests') + +def main(argv): + args = parse_args(argv) + try: + from bindingtester import LOGGING + + logging.config.dictConfig(LOGGING) + util.initialize_logger_level(args.logging_level) + + validate_args(args) + + if args.seed is None: + args.seed = random.randint(0, 0xffffffff) + + random.seed(args.seed) + + if args.enable_client_trace_logging is not None: + fdb.options.set_trace_enable(args.enable_client_trace_logging) + + test_runner = TestRunner(args) + + if args.bisect: + return bisect(test_runner, args) + + if args.print_test: + return test_runner.print_test() + + if args.insert_only: + return test_runner.insert_test() + + return test_runner.run_test() + + except Exception as e: + util.get_logger().error('\nERROR: %s' % e) + util.get_logger().debug(traceback.format_exc()) + exit(3) + + except: + util.get_logger().error('\nERROR: %s' % sys.exc_info()[0]) + util.get_logger().info(traceback.format_exc()) + exit(3) + +if __name__ == '__main__': sys.exit(main(sys.argv[1:])) diff --git a/bindings/bindingtester/known_testers.py b/bindings/bindingtester/known_testers.py new file mode 100644 index 0000000000..56cfd4669e --- /dev/null +++ b/bindings/bindingtester/known_testers.py @@ -0,0 +1,68 @@ +# +# known_testers.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import os + +MAX_API_VERSION = 500 + +class Tester: + def __init__(self, name, cmd, max_int_bits=64, min_api_version=0, max_api_version=MAX_API_VERSION, threads_enabled=True): + self.name = name + self.cmd = cmd + self.max_int_bits = max_int_bits + self.min_api_version = min_api_version + self.max_api_version = max_api_version + self.threads_enabled = threads_enabled + + def supports_api_version(self, api_version): + return api_version >= self.min_api_version and api_version <= self.max_api_version + + @classmethod + def get_test(cls, test_name_or_args): + if test_name_or_args in testers: + return testers[test_name_or_args] + else: + return Tester(test_name_or_args.split(' ')[0], test_name_or_args) + +def _absolute_path(path): + return os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', path) + +_java_cmd = 'java -ea -cp %s:%s com.apple.cie.foundationdb.test.' % ( + _absolute_path('java/foundationdb-client.jar'), + _absolute_path('java/foundationdb-tests.jar')) + +_java_completable_cmd = 'java -ea -cp %s:%s com.apple.cie.foundationdb.test.' % ( + _absolute_path('java/foundationdb-client-completable.jar'), + _absolute_path('java/foundationdb-tests-completable.jar')) + +# We could set min_api_version lower on some of these if the testers were updated to support them +testers = { + 'python' : Tester('python', 'python ' + _absolute_path('python/tests/tester.py'), 2040, 23, MAX_API_VERSION), + 'python3' : Tester('python3', 'python3 ' + _absolute_path('python/tests/tester.py'), 2040, 23, MAX_API_VERSION), + 'node' : Tester('node', _absolute_path('nodejs/tests/tester.js'), 53, 23, MAX_API_VERSION), + 'streamline' : Tester('streamline', _absolute_path('nodejs/tests/streamline_tester._js'), 53, 23, MAX_API_VERSION), + 'ruby' : Tester('ruby', _absolute_path('ruby/tests/tester.rb'), 64, 23, MAX_API_VERSION), + 'java' : Tester('java', _java_cmd + 'StackTester', 63, 500, MAX_API_VERSION), + 'java_async' : Tester('java', _java_cmd + 'AsyncStackTester', 63, 500, MAX_API_VERSION), + 'java_completable' : Tester('java', _java_completable_cmd + 'StackTester', 63, 500, MAX_API_VERSION), + 'java_completable_async' : Tester('java', _java_completable_cmd + 'AsyncStackTester', 63, 500, MAX_API_VERSION), + 'go' : Tester('go', _absolute_path('go/bin/_stacktester'), 63, 200, MAX_API_VERSION), + 'flow' : Tester('flow', _absolute_path('flow/bin/fdb_flow_tester'), 63, 200, MAX_API_VERSION), +} diff --git a/bindings/bindingtester/run_binding_tester.sh b/bindings/bindingtester/run_binding_tester.sh new file mode 100755 index 0000000000..bad918d008 --- /dev/null +++ b/bindings/bindingtester/run_binding_tester.sh @@ -0,0 +1,382 @@ +#!/bin/bash +###################################################### +# +# FoundationDB Binding Test Script +# +# Test script for running FoundationDB binding tests +# +# Defines: +# +# Author: Alvin Moore +# Date: 16-04-28 +# Version: 1.0 +###################################################### + +# Defines +SCRIPTDIR=$( cd "${BASH_SOURCE[0]%\/*}" && pwd ) +CWD=$(pwd) +OSNAME="$(uname -s)" +DEBUGLEVEL="${DEBUGLEVEL:-1}" +DISPLAYERROR="${DISPLAYERROR:-0}" +OPERATIONS="${OPERATIONS:-1000}" +HCAOPERATIONS="${HCAOPERATIONS:-100}" +CONCURRENCY="${CONCURRENCY:-5}" +BREAKONERROR="${BREAKONERROR:-0}" +RUNSCRIPTS="${RUNSCRIPTS:-1}" +RUNTESTS="${RUNTESTS:-1}" +RANDOMTEST="${RANDOMTEST:-0}" +BINDINGTESTS="${BINDINGTESTS:-python python3 java java_async java_completable java_completable_async node go flow}" +LOGLEVEL="${LOGLEVEL:-INFO}" +_BINDINGTESTS=(${BINDINGTESTS}) +DISABLEDTESTS=( 'ruby' ) +TESTFILE="${SCRIPTDIR}/bindingtester.py" +TESTTYPES=('API' 'Concurrent API' 'Directory' 'Directory HCA') +TESTTOTAL="${#TESTTYPES[@]}" +TESTINDEX="${TESTINDEX:-$TESTTOTAL}" +LOGSTDOUT="${LOGSTDOUT:-0}" +CONSOLELOG="${CONSOLELOG:-${CWD}/console.log}" +VERSION="1.6" + +# Display syntax +if [ "${#}" -lt 2 ] +then + echo 'run_binding_tester.sh ' + echo ' cycles: number of cycles to run test (0 => unlimitted)' + echo '' + echo ' Modifiable Environment Variables:' + echo ' CONCURRENCY: number of concurrent requests' + echo ' OPERATIONS: number of operations per test' + echo ' HCAOPERATIONS: number of HCA operations per test' + echo ' BINDINGTESTS: lists of binding tests to run' + echo ' BREAKONERROR: stop on first error, if positive number' + echo " TESTINDEX: (0-${TESTTOTAL}) ${TESTTYPES[*]}" + echo ' RANDOMTEST: select a single random test, if positive number' + echo ' LOGLEVEL: ERROR, WARNING, INFO, DEBUG' + echo '' + echo " version: ${VERSION}" + exit 1 +fi + +# Read arguments +MAXCYCLES="${1}" +ERRORFILE="${2}" + +function logError() +{ + local status=0 + + if [ "$#" -lt 3 ] + then + echo "runCommand [args ...]" + let status="${status} + 1" + else + local message="${1}" + local output="${2}" + local command="${3}" + shift + shift + shift + + let errorTotal="${errorTotal} + 1" + + # Display the error, if enabled + if [ "${DISPLAYERROR}" -gt 0 ] + then + printf '%-16s Error #%3d:\n' "$(date '+%F %H-%M-%S')" "${errorTotal}" + echo "Message: '${message}'" + echo "Command: '${command} ${@}'" + echo "Error: ${output}" + fi + + # Create the file, if not present + if [[ ! -f "${ERRORFILE}" ]] + then + dir=$(dirname "${ERRORFILE}") + + if [ ! -d "${dir}" ] && ! mkdir -p "${dir}" + then + echo "Failed to create directory: ${dir} for error file: ${ERRORFILE}" + let status="${status} + 1" + printf '\n%-16s Error #%3d:\n' "$(date '+%F %H-%M-%S')" "${errorTotal}" + echo "Message: '${message}'" + echo "Command: '${command} ${@}'" + echo "Error: ${output}" + fi + fi + + # Initialize the error log, if first error + if [[ "${errorTotal}" -eq 1 ]] + then + : + fi + + # Write the error to the log + if [[ "${status}" -eq 0 ]] + then + printf '\n%-16s Error #%3d:\n' "$(date '+%F %H-%M-%S')" "${errorTotal}" >> "${ERRORFILE}" + echo "Message: '${message}'" >> "${ERRORFILE}" + echo "Command: '${command} ${@}'" >> "${ERRORFILE}" + echo -n "Error:" >> "${ERRORFILE}" + echo "${output}" >> "${ERRORFILE}" + echo '----------------------------------------------------------------------------------------------------' >> "${ERRORFILE}" + fi + fi + + return "${status}" +} + +function runCommand() +{ + local status=0 + + if [ "$#" -lt 2 ] + then + echo "runCommand [args ...]" + let status="${status} + 1" + else + local message="${1}" + local command="${2}" + local time="${SECONDS}" + shift + shift + + if [ "${DEBUGLEVEL}" -gt 2 ]; then + printf "%-16s %-70s \n" "" "${command} ${*}" + fi + + if [ "${DEBUGLEVEL}" -gt 1 ]; then + printf "%-16s %-40s " "" "${message}" + fi + + if [ "${LOGSTDOUT}" -gt 0 ] ; then + printf "Running command: ${command} ${*}\n\n" >> "${CONSOLELOG}" + "${command}" "${@}" 2>&1 >> "${CONSOLELOG}" + result=$? + output=$(cat "${CONSOLELOG}") + else + output=$("${command}" "${@}" 2>&1) + result=$? + fi + let time="${SECONDS} - ${time}" + + # Check return code + if [ "${result}" -ne 0 ] + then + if [ "${DEBUGLEVEL}" -gt 0 ]; then + echo "failed after ${time} seconds." + fi + let status="${status} + 1" + logError "${message}" "${output}" "${command}" "${@}" + elif [ "${DEBUGLEVEL}" -gt 0 ];then + echo "passed in ${time} seconds." + fi + fi + + return "${status}" +} + +function runScriptedTest() +{ + local status=0 + + if [ "$#" -lt 1 ] + then + echo "runScriptedTest " + let status="${status} + 1" + else + local test="${1}" + + if ! runCommand "Scripting ${test} ..." 'python' '-u' "${TESTFILE}" "${test}" --test-name scripted --logging-level "${LOGLEVEL}" + then + let status="${status} + 1" + fi + fi + + return "${status}" +} + +function runTest() +{ + local status=0 + + if [ "$#" -lt 1 ] + then + echo "runTest " + let status="${status} + 1" + else + local test="${1}" + + if [ "${DEBUGLEVEL}" -gt 0 ]; then + printf "%-16s %-40s \n" "$(date '+%F %H-%M-%S')" "Testing ${test}" + fi + + # API + if ([[ "${TESTINDEX}" -eq 0 ]] || [[ "${TESTINDEX}" -eq "${TESTTOTAL}" ]]) && ([[ "${BREAKONERROR}" -eq 0 ]] || [[ "${status}" -eq 0 ]]) && ! runCommand " ${TESTTYPES[0]}" 'python' '-u' "${TESTFILE}" "${test}" --test-name api --compare --num-ops "${OPERATIONS}" --logging-level "${LOGLEVEL}" + then + let status="${status} + 1" + fi + + # Concurrent API + if ([[ "${TESTINDEX}" -eq 1 ]] || [[ "${TESTINDEX}" -eq "${TESTTOTAL}" ]]) && ([[ "${BREAKONERROR}" -eq 0 ]] || [[ "${status}" -eq 0 ]]) && ! runCommand " ${TESTTYPES[1]}" 'python' '-u' "${TESTFILE}" "${test}" --test-name api --concurrency "${CONCURRENCY}" --num-ops "${OPERATIONS}" --logging-level "${LOGLEVEL}" + then + let status="${status} + 1" + fi + + # Directory + if ([[ "${TESTINDEX}" -eq 2 ]] || [[ "${TESTINDEX}" -eq "${TESTTOTAL}" ]]) && ([[ "${BREAKONERROR}" -eq 0 ]] || [[ "${status}" -eq 0 ]]) && ! runCommand " ${TESTTYPES[2]}" 'python' '-u' "${TESTFILE}" "${test}" --test-name directory --compare --num-ops "${OPERATIONS}" --logging-level "${LOGLEVEL}" + then + let status="${status} + 1" + fi + + # Directory HCA + if ([[ "${TESTINDEX}" -eq 3 ]] || [[ "${TESTINDEX}" -eq "${TESTTOTAL}" ]]) && ([[ "${BREAKONERROR}" -eq 0 ]] || [[ "${status}" -eq 0 ]]) && ! runCommand " ${TESTTYPES[3]}" 'python' '-u' "${TESTFILE}" "${test}" --test-name directory_hca --concurrency "${CONCURRENCY}" --num-ops "${HCAOPERATIONS}" --logging-level "${LOGLEVEL}" + then + let status="${status} + 1" + fi + fi + + return "${status}" +} + +# Initialize the variables +status=0 +cycles=0 +rundate="$(date +%F_%H-%M-%S)" +errorTotal=0 + + +# Select a random test, if enabled +if [ "${RANDOMTEST}" -gt 0 ] +then + let testIndex="${RANDOM} % ${#_BINDINGTESTS[@]}" + randomTest="${_BINDINGTESTS[$testIndex]}" + # Remove the random test from the list of binding tests + _BINDINGTESTS=("${_BINDINGTESTS[@]/${randomTest}}") + DISABLEDTESTS+=("${_BINDINGTESTS[@]}") + _BINDINGTESTS=("${randomTest}") + + # Choose a random test + let TESTINDEX="${RANDOM} % ${TESTTOTAL}" + + # Select scripted or tests, if enabled + if [ "${RUNSCRIPTS}" -gt 0 ] && [ "${RUNTESTS}" -gt 0 ]; then + # Select scripted tests, if 1 out of 100 + if [ $((${RANDOM} % 100)) -eq 0 ]; then + RUNTESTS=0 + else + RUNSCRIPTS=0 + fi + fi +fi + +# Determine the name of the test type +# from the test index +if [ "${TESTINDEX}" -lt "${TESTTOTAL}" ]; then + TESTNAME="${TESTTYPES[$TESTINDEX]}" +else + TESTNAME="All Tests" + TESTINDEX="${TESTTOTAL}" +fi + +if [ "${DEBUGLEVEL}" -gt 0 ] +then + echo '' + echo '' + echo '*******************************************************************************************' + echo '' + printf "%-16s %-40s \n" "$(date '+%F %H-%M-%S')" "FoundationDb Binding Tester" + printf "%-20s Host OS: %-40s \n" "" "${OSNAME}" + printf "%-20s Max Cycles: %-40s \n" "" "${MAXCYCLES}" + printf "%-20s Operations: %-40s \n" "" "${OPERATIONS}" + printf "%-20s HCA Operations: %-40s \n" "" "${HCAOPERATIONS}" + printf "%-20s Concurrency: %-40s \n" "" "${CONCURRENCY}" + printf "%-20s Tests: (%2d) %-40s \n" "" "${#_BINDINGTESTS[@]}" "${_BINDINGTESTS[*]}" + printf "%-20s Disabled: (%2d) %-40s \n" "" "${#DISABLEDTESTS[@]}" "${DISABLEDTESTS[*]}" + printf "%-20s Error Log: %-40s \n" "" "${ERRORFILE}" + printf "%-20s Log Level: %-40s \n" "" "${LOGLEVEL}" + printf "%-20s Random Test: %-40s \n" "" "${RANDOMTEST}" + printf "%-20s Test Type: (%d) %-40s \n" "" "${TESTINDEX}" "${TESTNAME}" + printf "%-20s Run Scripts: %-40s \n" "" "${RUNSCRIPTS}" + printf "%-20s Run Tests: %-40s \n" "" "${RUNTESTS}" + printf "%-20s Debug Level: %-40s \n" "" "${DEBUGLEVEL}" + printf "%-20s Script Version: %-40s \n" "" "${VERSION}" + echo '' +fi + +# Run the scripted tests, if enabled +if [ "${RUNSCRIPTS}" -gt 0 ] +then + if [ "${DEBUGLEVEL}" -gt 0 ]; then + printf "%-16s %-40s \n" "$(date '+%F %H-%M-%S')" "Running scripted tests" + fi + + for test in "${_BINDINGTESTS[@]}" + do + # Run the specified scripted test + if ! runScriptedTest "${test}" + then + let status="${status} + 1" + + # Break Stop the test, if enabled + if [[ "${BREAKONERROR}" -ne 0 ]] + then + break + fi + fi + done +fi + +# Run the individual tests, if enabled +while [[ "${RUNTESTS}" -gt 0 ]] && ([[ "${BREAKONERROR}" -eq 0 ]] || [[ "${status}" -eq 0 ]]) && ([[ "${cycles}" -lt "${MAXCYCLES}" ]] || [[ "${MAXCYCLES}" -eq 0 ]]) +do + let cycles="${cycles} + 1" + if [ "${DEBUGLEVEL}" -gt 0 ]; then + printf "\n%-16s Cycle #%3d \n" "$(date '+%F %H-%M-%S')" "${cycles}" + fi + + for test in "${_BINDINGTESTS[@]}" + do + # Run the specified test + if ! runTest "${test}" + then + let status="${status} + 1" + + # Break Stop the test, if enabled + if [[ "${BREAKONERROR}" -ne 0 ]] + then + break + fi + fi + done +done + +# Final report +if [ "${status}" -eq 0 ] +then + if [ "${DEBUGLEVEL}" -gt 0 ]; then + printf "\n%-16s Successfully completed ${cycles} cycles of the FDB binding tester for ${#_BINDINGTESTS[@]} binding tests in %d seconds.\n" "$(date '+%F %H-%M-%S')" "${SECONDS}" + fi +elif [ "${DEBUGLEVEL}" -gt 0 ]; then + printf "\n%-16s Failed to complete all ${cycles} cycles of the FDB binding tester for ${#_BINDINGTESTS[@]} binding tests in %d seconds.\n" "$(date '+%F %H-%M-%S')" "${SECONDS}" +fi + +if [ "${DEBUGLEVEL}" -gt 0 ] +then + echo '' + echo '' + echo '*******************************************************************************************' + echo '' + printf "%-16s %-40s \n" "$(date '+%F %H-%M-%S')" "Binding Tester Results" + printf "%-20s Cycles: %-40s \n" "" "${cycles}" + printf "%-20s Failed Tests: %-40s \n" "" "${status}" + printf "%-20s Errors: %-40s \n" "" "${errorTotal}" + printf "%-20s Tests: (%2d) %-40s \n" "" "${#_BINDINGTESTS[@]}" "${_BINDINGTESTS[*]}" + printf "%-20s Version: %-40s \n" "" "${VERSION}" +fi + +# Ensure that status is a returnable number +if [[ "${status}" -ne 0 ]]; then + status=1 +fi + +exit "${status}" diff --git a/bindings/bindingtester/run_tester_loop.sh b/bindings/bindingtester/run_tester_loop.sh new file mode 100755 index 0000000000..1a6256f406 --- /dev/null +++ b/bindings/bindingtester/run_tester_loop.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +LOGGING_LEVEL=WARNING + +function run() { + echo "Running $1 api" + ./bindingtester.py $1 --test-name api --cluster-file fdb.cluster --compare --num-ops 1000 --logging-level $LOGGING_LEVEL + echo "Running $1 concurrent api" + ./bindingtester.py $1 --test-name api --cluster-file fdb.cluster --num-ops 1000 --concurrency 5 --logging-level $LOGGING_LEVEL + echo "Running $1 directory" + ./bindingtester.py $1 --test-name directory --cluster-file fdb.cluster --compare --num-ops 1000 --logging-level $LOGGING_LEVEL + echo "Running $1 directory hca" + ./bindingtester.py $1 --test-name directory_hca --cluster-file fdb.cluster --num-ops 100 --concurrency 5 --logging-level $LOGGING_LEVEL +} + +function scripted() { + echo "Running $1 scripted" + ./bindingtester.py $1 --test-name scripted --cluster-file fdb.cluster --logging-level $LOGGING_LEVEL +} + +function run_scripted() { + scripted python + scripted python3 + scripted ruby + scripted java + scripted java_async + scripted java_completable + scripted java_completable_async + scripted node + scripted go + scripted flow +} + +run_scripted + +i=1 +while `true`; do + echo "Pass $i" + i=$((i+1)) + run python + run python3 + run ruby + run java + run java_async + run java_completable + run java_completable_async + run node + #run streamline + run go + run flow +done diff --git a/bindings/bindingtester/spec/bindingApiTester.txt b/bindings/bindingtester/spec/bindingApiTester.txt new file mode 100644 index 0000000000..6b2c05bffe --- /dev/null +++ b/bindings/bindingtester/spec/bindingApiTester.txt @@ -0,0 +1,343 @@ +Overview +-------- + +Your API test program must implement a simple stack machine that exercises the +FoundationDB API. The program is invoked with two or three arguments. The first +argument is a prefix that is the first element of a tuple, the second is the +API version, and the third argument is the path to a cluster file. If the +third argument is not specified, your program may assume that fdb.open() will +succeed with no arguments (an fdb.cluster file will exist in the current +directory). Otherwise, your program should connect to the cluster specified +by the given cluster file. + +Your stack machine should begin reading the range returned by the tuple range +method of prefix and execute each instruction (stored in the value of the key) +until the range has been exhausted. When this stack machine (along with any +additional stack machines created as part of the test) have finished running, +your program should terminate. + +Upon successful termination, your program should exit with code 0. If your +program or any of your stack machines failed to run correctly, then it should +exit with a nonzero exit code. + +Instructions are also stored as packed tuples and should be expanded with the +tuple unpack method. The first element of the instruction tuple represents an +operation, and will always be returned as a unicode string. An operation may have +a second element which provides additional data, which may be of any tuple type. + +Your stack machine must maintain a small amount of state while executing +instructions: + + - A global transaction map from byte string to Transactions. This map is + shared by all tester 'threads'. + + - A stack of data items of mixed types and their associated metadata. At a + minimum, each item should be stored with the 0-based instruction number + which resulted in it being put onto the stack. Your stack must support push + and pop operations. It may be helpful if it supports random access, clear + and a peek operation. The stack is initialized to be empty. + + - A current FDB transaction name (stored as a byte string). The transaction + name should be initialized to the prefix that instructions are being read + from. + + - A last seen FDB version, which is a 64-bit integer. + + +Data Operations +--------------- + +PUSH + + Pushes the provided item onto the stack. + +DUP + + Duplicates the top item on the stack. The instruction number for the + duplicate item should be the same as the original. + +EMPTY_STACK + + Discards all items in the stack. + +SWAP + + Pops the top item off of the stack as INDEX. Swaps the items in the stack at + depth 0 and depth INDEX. Does not modify the instruction numbers of the + swapped items. + +POP + + Pops and discards the top item on the stack. + +SUB + + Pops the top two items off of the stack as A and B and then pushes the + difference (A-B) onto the stack. A and B may be assumed to be integers. + +CONCAT + + Pops the top two items off the stack as A and B and then pushes the + concatenation of A and B onto the stack. A and B can be assumed to + be of the same type and will be either byte strings or unicode strings. + +LOG_STACK + + Pops the top item off the stack as PREFIX. Using a new transaction with normal + retry logic, inserts a key-value pair into the database for each item in the + stack of the form: + + PREFIX + tuple.pack((stackIndex, instructionNumber)) = tuple.pack((item,)) + + where stackIndex is the current index of the item in the stack. The oldest + item in the stack should have stackIndex 0. + + If the byte string created by tuple packing the item exceeds 40000 bytes, + then the value should be truncated to the first 40000 bytes of the packed + tuple. + + When finished, the stack should be empty. Note that because the stack may be + large, it may be necessary to commit the transaction every so often (e.g. + after every 100 sets) to avoid past_version errors. + +FoundationDB Operations +----------------------- + +All of these operations map to a portion of the FoundationDB API. When an +operation applies to a transaction, it should use the transaction stored in +the global transaction map corresponding to the current transaction name. Certain +instructions will be followed by one or both of _SNAPSHOT and _DATABASE to +indicate that they may appear with these variations. _SNAPSHOT operations should +perform the operation as a snapshot read. _DATABASE operations should (if +possible) make use of the methods available directly on the FoundationDB +database object, rather than the currently open transaction. + +If your binding does not support operations directly on a database object, you +should simulate it using an anonymous transaction. Remember that set and clear +operations must immediately commit (with appropriate retry behavior!). + +Any error that bubbles out of these operations must be caught. In the event of +an error, you must push the packed tuple of the string "ERROR" and the error +code (as a string, not an integer). + +Some operations may allow you to push future values onto the stack. When popping +objects from the stack, the future MUST BE waited on and errors caught before +any operations that use the result of the future. + +Whether or not you choose to push a future, any operation that supports optional +futures must apply the following rules to the result: + + - If the result is an error, then its value is to be converted to an error + string as defined above + + - If the result is void (i.e. the future was just a signal of + completion), then its value should be the byte string + "RESULT_NOT_PRESENT" + + - If the result is from a GET operation in which no result was + returned, then its value is to be converted to the byte string + "RESULT_NOT_PRESENT" + +NEW_TRANSACTION + + Creates a new transaction and stores it in the global transaction map + under the currently used transaction name. + +USE_TRANSACTION + + Pop the top item off of the stack as TRANSACTION_NAME. Begin using the + transaction stored at TRANSACTION_NAME in the transaction map for future + operations. If no entry exists in the map for the given name, a new + transaction should be inserted. + +ON_ERROR + + Pops the top item off of the stack as ERROR_CODE. Passes ERROR_CODE in a + language-appropriate way to the on_error method of current transaction + object and blocks on the future. If on_error re-raises the error, bubbles + the error out as indicated above. May optionally push a future onto the + stack. + +GET (_SNAPSHOT, _DATABASE) + + Pops the top item off of the stack as KEY and then looks up KEY in the + database using the get() method. May optionally push a future onto the + stack. + +GET_KEY (_SNAPSHOT, _DATABASE) + + Pops the top four items off of the stack as KEY, OR_EQUAL, OFFSET, PREFIX + and then constructs a key selector. This key selector is then resolved + using the get_key() method to yield RESULT. If RESULT starts with PREFIX, + then RESULT is pushed onto the stack. Otherwise, if RESULT < PREFIX, PREFIX + is pushed onto the stack. If RESULT > PREFIX, then strinc(PREFIX) is pushed + onto the stack. May optionally push a future onto the stack. + +GET_RANGE (_SNAPSHOT, _DATABASE) + + Pops the top five items off of the stack as BEGIN_KEY, END_KEY, LIMIT, + REVERSE and STREAMING_MODE. Performs a range read in a language-appropriate + way using these parameters. The resulting range of n key-value pairs are + packed into a tuple as [k1,v1,k2,v2,...,kn,vn], and this single packed value + is pushed onto the stack. + +GET_RANGE_STARTS_WITH (_SNAPSHOT, _DATABASE) + + Pops the top four items off of the stack as PREFIX, LIMIT, REVERSE and + STREAMING_MODE. Performs a prefix range read in a language-appropriate way + using these parameters. Output is pushed onto the stack as with GET_RANGE. + +GET_RANGE_SELECTOR (_SNAPSHOT, _DATABASE) + + Pops the top ten items off of the stack as BEGIN_KEY, BEGIN_OR_EQUAL, + BEGIN_OFFSET, END_KEY, END_OR_EQUAL, END_OFFSET, LIMIT, REVERSE, + STREAMING_MODE, and PREFIX. Constructs key selectors BEGIN and END from + the first six parameters, and then performs a range read in a language- + appropriate way using BEGIN, END, LIMIT, REVERSE and STREAMING_MODE. Output + is pushed onto the stack as with GET_RANGE, excluding any keys that do not + begin with PREFIX. + +GET_READ_VERSION (_SNAPSHOT) + + Gets the current read version and stores it in the internal stack machine + state as the last seen version. Pushed the string "GOT_READ_VERSION" onto + the stack. + +GET_VERSIONSTAMP + + Calls get_versionstamp and pushes the resulting future onto the stack. + +SET (_DATABASE) + + Pops the top two items off of the stack as KEY and VALUE. Sets KEY to have + the value VALUE. A SET_DATABASE call may optionally push a future onto the + stack. + +SET_READ_VERSION + + Sets the current transaction read version to the internal state machine last + seen version. + +CLEAR (_DATABASE) + + Pops the top item off of the stack as KEY and then clears KEY from the + database. A CLEAR_DATABASE call may optionally push a future onto the stack. + +CLEAR_RANGE (_DATABASE) + + Pops the top two items off of the stack as BEGIN_KEY and END_KEY. Clears the + range of keys from BEGIN_KEY to END_KEY in the database. A + CLEAR_RANGE_DATABASE call may optionally push a future onto the stack. + +CLEAR_RANGE_STARTS_WITH (_DATABASE) + + Pops the top item off of the stack as PREFIX and then clears all keys from + the database that begin with PREFIX. A CLEAR_RANGE_STARTS_WITH_DATABASE call + may optionally push a future onto the stack. + +ATOMIC_OP (_DATABASE) + + Pops the top three items off of the stack as OPTYPE, KEY, and VALUE. + Performs the atomic operation described by OPTYPE upon KEY with VALUE. An + ATOMIC_OP_DATABASE call may optionally push a future onto the stack. + +READ_CONFLICT_RANGE and WRITE_CONFLICT_RANGE + + Pops the top two items off of the stack as BEGIN_KEY and END_KEY. Adds a + read conflict range or write conflict range from BEGIN_KEY to END_KEY. + Pushes the byte string "SET_CONFLICT_RANGE" onto the stack. + +READ_CONFLICT_KEY and WRITE_CONFLICT_KEY + + Pops the top item off of the stack as KEY. Adds KEY as a read conflict key + or write conflict key. Pushes the byte string "SET_CONFLICT_KEY" onto the + stack. + +DISABLE_WRITE_CONFLICT + + Sets the NEXT_WRITE_NO_WRITE_CONFLICT_RANGE transaction option on the + current transaction. Does not modify the stack. + +COMMIT + + Commits the current transaction (with no retry behavior). May optionally + push a future onto the stack. + +RESET + + Resets the current transaction. + +CANCEL + + Cancels the current transaction. + +GET_COMMITTED_VERSION + + Gets the committed version from the current transaction and stores it in the + internal stack machine state as the last seen version. Pushes the byte + string "GOT_COMMITTED_VERSION" onto the stack. + +WAIT_FUTURE + + Pops the top item off the stack and pushes it back on. If the top item on + the stack is a future, this will have the side effect of waiting on the + result of the future and pushing the result on the stack. Does not change + the instruction number of the item. + +Tuple Operations +---------------- + +TUPLE_PACK + + Pops the top item off of the stack as N. Pops the next N items off of the + stack and packs them as the tuple [item0,item1,...,itemN], and then pushes + this single packed value onto the stack. + +TUPLE_UNPACK + + Pops the top item off of the stack as PACKED, and then unpacks PACKED into a + tuple. For each element of the tuple, packs it as a new tuple and pushes it + onto the stack. + +TUPLE_RANGE + + Pops the top item off of the stack as N. Pops the next N items off of the + stack, and passes these items as a tuple (or array, or language-appropriate + structure) to the tuple range method. Pushes the begin and end elements of + the returned range onto the stack. + + +Thread Operations +----------------- + +START_THREAD + + Pops the top item off of the stack as PREFIX. Creates a new stack machine + instance operating on the same database as the current stack machine, but + operating on PREFIX. The new stack machine should have independent internal + state. The new stack machine should begin executing instructions concurrent + with the current stack machine through a language-appropriate mechanism. + +WAIT_EMPTY + + Pops the top item off of the stack as PREFIX. Blocks execution until the + range with prefix PREFIX is not present in the database. This should be + implemented as a polling loop inside of a language- and binding-appropriate + retryable construct which synthesizes FoundationDB error 1020 when the range + is not empty. Pushes the string "WAITED_FOR_EMPTY" onto the stack when + complete. + +Miscellaneous +------------- + +UNIT_TESTS + + This is called during the scripted test to allow bindings to test features + which aren't supported by the stack tester. Things currently tested in the + UNIT_TESTS section: + + Transaction options + Watches + Cancellation + Retry limits + Timeouts diff --git a/bindings/bindingtester/spec/directoryLayerTester.txt b/bindings/bindingtester/spec/directoryLayerTester.txt new file mode 100644 index 0000000000..03ed3135d2 --- /dev/null +++ b/bindings/bindingtester/spec/directoryLayerTester.txt @@ -0,0 +1,241 @@ +Overview +-------- + +The directory layer is tested by adding some additional instructions and state to +the existing stack tester. Each 'thread' of the stack tester should have its own +directory testing state. + +Additional State and Initialization +----------------------------------- + +Your tester should store three additional pieces of state. + +directory list - The items in this list should be accessible by index. The list +should support an append operation. It will be required to store Subspaces, +DirectorySubspaces, and DirectoryLayers. + +directory list index - an index into the directory list of the currently active +directory. + +error index - the index to use when the directory at directory list index is not +present + +At the beginning of the test, the list should contain just the default directory +layer. The directory index and error index should both be set to 0. + +Popping Tuples +------------- + +Some instructions will require you to pop N tuples. To do this, repeat the +following procedure N times: + +Pop 1 item off the stack as M. Pop M items off the stack as +tuple = [item1, ..., itemM]. + +Errors +------ + +In the even that you encounter an error when performing a directory layer +operation, you should push the byte string: "DIRECTORY_ERROR" onto the stack. If +the operation being performed was supposed to append an item to the directory +list, then a null entry should be appended instead. + +New Instructions +---------------- + +Below are the new instructions that must be implemented to test the directory +layer. Some instructions specify that the current directory should be used +for the operation. In that case, use the object in the directory list specified +by the current directory list index. Operations that are not defined for a +particular object will not be called (e.g. a DirectoryLayer will never be asked +to pack a key). + +Directory/Subspace/Layer Creation +--------------------------------- + +DIRECTORY_CREATE_SUBSPACE + + Pop 1 tuple off the stack as [path]. Pop 1 additional item as [raw_prefix]. + Create a subspace with path as the prefix tuple and the specified + raw_prefix. Append it to the directory list. + +DIRECTORY_CREATE_LAYER + + Pop 3 items off the stack as [index1, index2, allow_manual_prefixes]. Let + node_subspace be the object in the directory list at index1 and + content_subspace be the object in the directory list at index2. Create a new + directory layer with the specified node_subspace and content_subspace. If + allow_manual_prefixes is 1, then enable manual prefixes on the directory + layer. Append the resulting directory layer to the directory list. + + If either of the two specified subspaces are null, then do not create a + directory layer and instead push null onto the directory list. + +DIRECTORY_CREATE_OR_OPEN[_DATABASE] + + Use the current directory for this operation. + + Pop 1 tuple off the stack as [path]. Pop 1 additional item as [layer]. + create_or_open a directory with the specified path and layer. If layer is + null, use the default value for that parameter. + +DIRECTORY_CREATE[_DATABASE] + + Pop 1 tuple off the stack as [path]. Pop 2 additional items as + [layer, prefix]. create a directory with the specified path, layer, + and prefix. If either of layer or prefix is null, use the default value for + that parameter (layer='', prefix=null). + +DIRECTORY_OPEN[_DATABASE|_SNAPSHOT] + + Use the current directory for this operation. + + Pop 1 tuple off the stack as [path]. Pop 1 additional item as [layer]. Open + a directory with the specified path and layer. If layer is null, use the + default value (layer=''). + +Directory Management +-------------------- + +DIRECTORY_CHANGE + + Pop the top item off the stack as [index]. Set the current directory list + index to index. In the event that the directory at this new index is null + (as the result of a previous error), set the directory list index to the + error index. + +DIRECTORY_SET_ERROR_INDEX + + Pop the top item off the stack as [error_index]. Set the current error index + to error_index. + +Directory Operations +-------------------- + +DIRECTORY_MOVE[_DATABASE] + + Use the current directory for this operation. + + Pop 2 tuples off the stack as [old_path, new_path]. Call move with the + specified old_path and new_path. Append the result onto the directory list. + +DIRECTORY_MOVE_TO[_DATABASE] + + Use the current directory for this operation. + + Pop 1 tuple off the stack as [new_absolute_path]. Call moveTo with the + specified new_absolute_path. Append the result onto the directory list. + +DIRECTORY_REMOVE[_DATABASE] + + Use the current directory for this operation. + + Pop 1 item off the stack as [count] (either 0 or 1). If count is 1, pop 1 + tuple off the stack as [path]. Call remove, passing it path if one was + popped. + +DIRECTORY_REMOVE_IF_EXISTS[_DATABASE] + + Use the current directory for this operation. + + Pop 1 item off the stack as [count] (either 0 or 1). If count is 1, pop 1 + tuple off the stack as [path]. Call remove_if_exits, passing it path if one + was popped. + +DIRECTORY_LIST[_DATABASE|_SNAPSHOT] + + Use the current directory for this operation. + + Pop 1 item off the stack as [count] (either 0 or 1). If count is 1, pop 1 + tuple off the stack as [path]. Call list, passing it path if one was popped. + Pack the resulting list of directories using the tuple layer and push the + packed string onto the stack. + +DIRECTORY_EXISTS[_DATABASE|_SNAPSHOT] + + Use the current directory for this operation. + + Pop 1 item off the stack as [count] (either 0 or 1). If count is 1, pop 1 + tuple off the stack as [path]. Call exists, passing it path if one + was popped. Push 1 onto the stack if the path exists and 0 if it does not. + +Subspace Operations +------------------- + +DIRECTORY_PACK_KEY + + Use the current directory for this operation. + + Pop 1 tuple off the stack as [key_tuple]. Pack key_tuple and push the result + onto the stack. + +DIRECTORY_UNPACK_KEY + + Use the current directory for this operation. + + Pop 1 item off the stack as [key]. Unpack key and push the resulting tuple + onto the stack one item at a time. + +DIRECTORY_RANGE + + Use the current directory for this operation. + + Pop 1 tuple off the stack as [tuple]. Create a range using tuple and push + range.begin and range.end onto the stack. + +DIRECTORY_CONTAINS + + Use the current directory for this operation. + + Pop 1 item off the stack as [key]. Check if the current directory contains + the specified key. Push 1 if it does and 0 if it doesn't. + +DIRECTORY_OPEN_SUBSPACE + + Use the current directory for this operation. + + Pop 1 tuple off the stack as [tuple]. Open the subspace of the current + directory specified by tuple and push it onto the directory list. + +Directory Logging +-------------------- + +DIRECTORY_LOG_SUBSPACE + + Use the current directory for this operation. + + Pop 1 item off the stack as [prefix]. Let key equal + prefix + tuple.pack([dir_index]). Set key to be the result of calling + directory.key() in the current transaction. + +DIRECTORY_LOG_DIRECTORY + + Use the current directory for this operation. + + Pop 1 item off the stack as [raw_prefix]. Create a subspace log_subspace + with path (dir_index) and the specified raw_prefix. Set: + + tr[log_subspace[u'path']] = the tuple packed path of the directory. + + tr[log_subspace[u'layer']] = the tuple packed layer of the directory. + + tr[log_subspace[u'exists']] = the packed tuple containing a 1 if the + directory exists and 0 if it doesn't. + + tr[log_subspace[u'children']] the tuple packed list of children of the + directory. + + Where log_subspace[u] is the subspace packed tuple containing only the + single specified unicode string . + +Other +----- + +DIRECTORY_STRIP_PREFIX + + Use the current directory for this operation. + + Pop 1 item off the stack as [byte_array]. Call .key() on the current + subspace and store the result as [prefix]. Throw an error if the popped + array does not start with prefix. Otherwise, remove the prefix from the + popped array and push the result onto the stack. diff --git a/bindings/bindingtester/tests/__init__.py b/bindings/bindingtester/tests/__init__.py new file mode 100644 index 0000000000..c65073bf18 --- /dev/null +++ b/bindings/bindingtester/tests/__init__.py @@ -0,0 +1,197 @@ +# +# __init__.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import math +import re + +import fdb + +from bindingtester import FDB_API_VERSION +from bindingtester import util + +fdb.api_version(FDB_API_VERSION) + +class ResultSpecification(object): + def __init__(self, subspace, key_start_index=0, ordering_index=None, global_error_filter=None): + self.subspace = subspace + self.key_start_index = key_start_index + self.ordering_index = ordering_index + + if global_error_filter is not None: + error_str = '|'.join(['%d' % e for e in global_error_filter]) + self.error_regex = re.compile(r'\x01+ERROR\x00\xff*\x01' + error_str + r'\x00') + else: + self.error_regex = None + + def matches_global_error_filter(self, str): + if self.error_regex is None: + return False + + return self.error_regex.search(str) is not None + + +class Test(object): + def __init__(self, subspace, min_api_version=0, max_api_version=int(1e9)): + self.subspace = subspace + self.min_api_version = min_api_version + self.max_api_version = max_api_version + + # Returns nothing + def setup(self, args): + pass + + # Returns an instance of TestInstructions + def generate(self, args, thread_number): + pass + + # Returns nothing + def pre_run(self, db, args): + pass + + # Returns a list of ResultSpecifications to read data from and compare with other testers + def get_result_specifications(self): + return [] + + # Returns a dict { subspace => results } of results that the test is expected to have. + # Compared against subspaces returned by get_result_subspaces. A subspace omitted from this dictionary + # can still be compared against other testers if it is added to the list returned by get_result_subspaces. + def get_expected_results(self): + return {} + + # Returns a list of error strings + def validate(self, db, args): + return [] + + @classmethod + def create_test(cls, name, subspace): + target = 'bindingtester.tests.%s' % name + test_class = [s for s in cls.__subclasses__() if s.__module__ == target] + if len(test_class) == 0: + return None + + return test_class[0](subspace) + +class Instruction(object): + def __init__(self, operation): + self.operation = operation + self.argument = None + self.value = fdb.tuple.pack((unicode(self.operation),)) + + def to_value(self): + return self.value + + def __str__(self): + return self.operation + + def __repr__(self): + return repr(self.operation) + +class PushInstruction(Instruction): + def __init__(self, argument): + self.operation = 'PUSH' + self.argument = argument + self.value = fdb.tuple.pack((unicode("PUSH"), argument)) + + def __str__(self): + return '%s %s' % (self.operation, self.argument) + + def __repr__(self): + return '%r %r' % (self.operation, self.argument) + +class TestInstructions(object): + def __init__(self): + pass + + # returns a dictionary of subspace => InstructionSets + def get_threads(self, subspace): + pass + + def insert_operations(self, db, subspace): + pass + +class InstructionSet(TestInstructions, list): + def __init__(self): + TestInstructions.__init__(self) + list.__init__(self) + + self.core_test_begin = 0 + self.core_test_end = None + + def push_args(self, *args): + self.extend([PushInstruction(arg) for arg in reversed(args)]) + + def append(self, instruction): + if isinstance(instruction, Instruction): + list.append(self, instruction) + else: + list.append(self, Instruction(instruction)) + + def get_threads(self, subspace): + return { subspace : self } + + def setup_complete(self): + self.core_test_begin = len(self) + + def begin_finalization(self): + self.core_test_end = len(self) + + def core_instructions(self): + return self[self.core_test_begin : self.core_test_end] + + @fdb.transactional + def _insert_operations_transactional(self, tr, subspace, start, count): + for i, instruction in enumerate(self[start : start+count]): + tr[subspace.pack((start + i,))] = instruction.to_value() + + def insert_operations(self, db, subspace): + for i in range(0, int(math.ceil(len(self) / 1000.0))): + self._insert_operations_transactional(db, subspace, i*1000, 1000) + +class ThreadedInstructionSet(TestInstructions): + def __init__(self): + super(ThreadedInstructionSet, self).__init__() + self.threads = {} + + def get_threads(self, subspace): + result = dict(self.threads) + if None in self.threads: + result[subspace] = result[None] + del result[None] + + return result + + def insert_operations(self, db, subspace): + for thread_subspace, thread in self.threads.items(): + if thread_subspace is None: + thread_subspace = subspace + + thread.insert_operations(db, thread_subspace) + + def create_thread(self, subspace=None, thread_instructions=None): + if subspace in self.threads: + raise 'An instruction set with the subspace %r has already been created' % util.subspace_to_tuple(subspace) + + if thread_instructions == None: + thread_instructions = InstructionSet() + + self.threads[subspace] = thread_instructions + return thread_instructions + +util.import_subclasses(__file__, 'bindingtester.tests') diff --git a/bindings/bindingtester/tests/api.py b/bindings/bindingtester/tests/api.py new file mode 100644 index 0000000000..1b1e92d3d0 --- /dev/null +++ b/bindings/bindingtester/tests/api.py @@ -0,0 +1,482 @@ +# +# api.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import random + +import fdb + +from bindingtester import FDB_API_VERSION +from bindingtester.tests import Test, Instruction, InstructionSet, ResultSpecification +from bindingtester.tests import test_util + +fdb.api_version(FDB_API_VERSION) + +class ApiTest(Test): + def __init__(self, subspace): + super(ApiTest, self).__init__(subspace) + self.workspace = self.subspace['workspace'] # The keys and values here must match between subsequent runs of the same test + self.scratch = self.subspace['scratch'] # The keys and values here can differ between runs + self.stack_subspace = self.subspace['stack'] + + self.versionstamped_values = self.scratch['versionstamped_values'] + self.versionstamped_keys = self.scratch['versionstamped_keys'] + + def setup(self, args): + self.stack_size = 0 + self.string_depth = 0 + self.key_depth = 0 + self.max_keys = 1000 + + self.has_version = False + self.can_set_version = True + self.is_committed = True + self.can_use_key_selectors = True + + self.generated_keys = [] + self.outstanding_ops = [] + self.random = test_util.RandomGenerator(args.max_int_bits) + + def add_stack_items(self, num): + self.stack_size += num + self.string_depth = 0 + self.key_depth = 0 + + def add_strings(self, num): + self.stack_size += num + self.string_depth += num + self.key_depth = 0 + + def add_keys(self, num): + self.stack_size += num + self.string_depth += num + self.key_depth += num + + def remove(self, num): + self.stack_size -= num + self.string_depth = max(0, self.string_depth - num) + self.key_depth = max(0, self.key_depth - num) + + self.outstanding_ops = [i for i in self.outstanding_ops if i[0] <= self.stack_size] + + def ensure_string(self, instructions, num): + while self.string_depth < num: + instructions.push_args(self.random.random_string(random.randint(0, 100))) + self.add_strings(1) + + self.remove(num) + + def choose_key(self): + if random.random() < float(len(self.generated_keys)) / self.max_keys: + tup = random.choice(self.generated_keys) + if random.random() < 0.3: + return self.workspace.pack(tup[0:random.randint(0, len(tup))]) + + return self.workspace.pack(tup) + + tup = self.random.random_tuple(5) + self.generated_keys.append(tup) + + return self.workspace.pack(tup) + + def ensure_key(self, instructions, num): + while self.key_depth < num: + instructions.push_args(self.choose_key()) + self.add_keys(1) + + self.remove(num) + + def ensure_key_value(self, instructions): + if self.string_depth == 0: + instructions.push_args(self.choose_key(), self.random.random_string(random.randint(0, 100))) + + elif self.string_depth == 1 or self.key_depth == 0: + self.ensure_key(instructions, 1) + self.remove(1) + + else: + self.remove(2) + + def preload_database(self, instructions, num): + for i in range(num): + self.ensure_key_value(instructions) + instructions.append('SET') + + if i % 100 == 99: + test_util.blocking_commit(instructions) + + test_util.blocking_commit(instructions) + self.add_stack_items(1) + + def wait_for_reads(self, instructions): + while len(self.outstanding_ops) > 0 and self.outstanding_ops[-1][0] <= self.stack_size: + read = self.outstanding_ops.pop() + #print '%d. waiting for read at instruction %r' % (len(instructions), read) + test_util.to_front(instructions, self.stack_size - read[0]) + instructions.append('WAIT_FUTURE') + + def generate(self, args, thread_number): + instructions = InstructionSet() + + op_choices = ['NEW_TRANSACTION', 'COMMIT'] + + reads = ['GET', 'GET_KEY', 'GET_RANGE', 'GET_RANGE_STARTS_WITH', 'GET_RANGE_SELECTOR'] + mutations = ['SET', 'CLEAR', 'CLEAR_RANGE', 'CLEAR_RANGE_STARTS_WITH', 'ATOMIC_OP'] + snapshot_reads = [x + '_SNAPSHOT' for x in reads] + database_reads = [x + '_DATABASE' for x in reads] + database_mutations = [x + '_DATABASE' for x in mutations] + mutations += ['VERSIONSTAMP'] + versions = ['GET_READ_VERSION', 'SET_READ_VERSION', 'GET_COMMITTED_VERSION'] + snapshot_versions = ['GET_READ_VERSION_SNAPSHOT'] + tuples = ['TUPLE_PACK', 'TUPLE_UNPACK', 'TUPLE_RANGE', 'SUB'] + resets = ['ON_ERROR', 'RESET', 'CANCEL'] + read_conflicts = ['READ_CONFLICT_RANGE', 'READ_CONFLICT_KEY'] + write_conflicts = ['WRITE_CONFLICT_RANGE', 'WRITE_CONFLICT_KEY', 'DISABLE_WRITE_CONFLICT'] + + op_choices += reads + op_choices += mutations + op_choices += snapshot_reads + op_choices += database_reads + op_choices += database_mutations + op_choices += versions + op_choices += snapshot_versions + op_choices += tuples + op_choices += read_conflicts + op_choices += write_conflicts + op_choices += resets + + idempotent_atomic_ops = [u'BIT_AND', u'BIT_OR', u'MAX', u'MIN'] + atomic_ops = idempotent_atomic_ops + [u'ADD', u'BIT_XOR'] + + if args.concurrency > 1: + self.max_keys = random.randint(100, 1000) + else: + self.max_keys = random.randint(100, 10000) + + instructions.append('NEW_TRANSACTION') + instructions.append('GET_READ_VERSION') + + self.preload_database(instructions, self.max_keys) + + instructions.setup_complete() + + for i in range(args.num_ops): + op = random.choice(op_choices) + index = len(instructions) + + #print 'Adding instruction %s at %d' % (op, index) + + if args.concurrency == 1 and (op in database_mutations): + self.wait_for_reads(instructions) + test_util.blocking_commit(instructions) + self.add_stack_items(1) + + if op in resets or op == 'NEW_TRANSACTION': + if args.concurrency == 1: + self.wait_for_reads(instructions) + + self.outstanding_ops = [] + + if op == 'NEW_TRANSACTION': + instructions.append(op) + self.is_committed = False + self.can_set_version = True + self.can_use_key_selectors = True + + elif op == 'ON_ERROR': + instructions.push_args(random.randint(0, 5000)) + instructions.append(op) + + self.outstanding_ops.append((self.stack_size, len(instructions)-1)) + if args.concurrency == 1: + self.wait_for_reads(instructions) + + instructions.append('NEW_TRANSACTION') + self.is_committed = False + self.can_set_version = True + self.can_use_key_selectors = True + self.add_strings(1) + + elif op == 'GET' or op == 'GET_SNAPSHOT' or op == 'GET_DATABASE': + self.ensure_key(instructions, 1) + instructions.append(op) + self.add_strings(1) + self.can_set_version = False + + elif op == 'GET_KEY' or op == 'GET_KEY_SNAPSHOT' or op == 'GET_KEY_DATABASE': + if op.endswith('_DATABASE') or self.can_use_key_selectors: + self.ensure_key(instructions, 1) + instructions.push_args(self.workspace.key()) + instructions.push_args(*self.random.random_selector_params()) + test_util.to_front(instructions, 3) + instructions.append(op) + + #Don't add key here because we may be outside of our prefix + self.add_strings(1) + self.can_set_version = False + + elif op == 'GET_RANGE' or op == 'GET_RANGE_SNAPSHOT' or op == 'GET_RANGE_DATABASE': + self.ensure_key(instructions, 2) + range_params = self.random.random_range_params() + instructions.push_args(*range_params) + test_util.to_front(instructions, 4) + test_util.to_front(instructions, 4) + instructions.append(op) + + if range_params[0] >= 1 and range_params[0] <= 1000: # avoid adding a string if the limit is large + self.add_strings(1) + else: + self.add_stack_items(1) + + self.can_set_version = False + + elif op == 'GET_RANGE_STARTS_WITH' or op == 'GET_RANGE_STARTS_WITH_SNAPSHOT' or op == 'GET_RANGE_STARTS_WITH_DATABASE': + #TODO: not tested well + self.ensure_key(instructions, 1) + range_params = self.random.random_range_params() + instructions.push_args(*range_params) + test_util.to_front(instructions, 3) + instructions.append(op) + + if range_params[0] >= 1 and range_params[0] <= 1000: # avoid adding a string if the limit is large + self.add_strings(1) + else: + self.add_stack_items(1) + + self.can_set_version = False + + elif op == 'GET_RANGE_SELECTOR' or op == 'GET_RANGE_SELECTOR_SNAPSHOT' or op == 'GET_RANGE_SELECTOR_DATABASE': + if op.endswith('_DATABASE') or self.can_use_key_selectors: + self.ensure_key(instructions, 2) + instructions.push_args(self.workspace.key()) + range_params = self.random.random_range_params() + instructions.push_args(*range_params) + instructions.push_args(*self.random.random_selector_params()) + test_util.to_front(instructions, 6) + instructions.push_args(*self.random.random_selector_params()) + test_util.to_front(instructions, 9) + instructions.append(op) + + if range_params[0] >= 1 and range_params[0] <= 1000: # avoid adding a string if the limit is large + self.add_strings(1) + else: + self.add_stack_items(1) + + self.can_set_version = False + + elif op == 'GET_READ_VERSION' or op == 'GET_READ_VERSION_SNAPSHOT': + instructions.append(op) + self.has_version = self.can_set_version + self.add_strings(1) + + elif op == 'SET' or op == 'SET_DATABASE': + self.ensure_key_value(instructions) + instructions.append(op) + if op == 'SET_DATABASE': + self.add_stack_items(1) + + elif op == 'SET_READ_VERSION': + if self.has_version and self.can_set_version: + instructions.append(op) + self.can_set_version = False + + elif op == 'CLEAR' or op == 'CLEAR_DATABASE': + self.ensure_key(instructions, 1) + instructions.append(op) + if op == 'CLEAR_DATABASE': + self.add_stack_items(1) + + elif op == 'CLEAR_RANGE' or op == 'CLEAR_RANGE_DATABASE': + #Protect against inverted range + key1 = self.workspace.pack(self.random.random_tuple(5)) + key2 = self.workspace.pack(self.random.random_tuple(5)) + + if key1 > key2: + key1, key2 = key2, key1 + + instructions.push_args(key1, key2) + + instructions.append(op) + if op == 'CLEAR_RANGE_DATABASE': + self.add_stack_items(1) + + elif op == 'CLEAR_RANGE_STARTS_WITH' or op == 'CLEAR_RANGE_STARTS_WITH_DATABASE': + self.ensure_key(instructions, 1) + instructions.append(op) + if op == 'CLEAR_RANGE_STARTS_WITH_DATABASE': + self.add_stack_items(1) + + elif op == 'ATOMIC_OP' or op == 'ATOMIC_OP_DATABASE': + self.ensure_key_value(instructions) + if op == 'ATOMIC_OP' or args.concurrency > 1: + instructions.push_args(random.choice(atomic_ops)) + else: + instructions.push_args(random.choice(idempotent_atomic_ops)) + + instructions.append(op) + if op == 'ATOMIC_OP_DATABASE': + self.add_stack_items(1) + + elif op == 'VERSIONSTAMP': + rand_str1 = self.random.random_string(100) + key1 = self.versionstamped_values.pack((rand_str1,)) + + split = random.randint(0, 70) + rand_str2 = self.random.random_string(20+split) + 'XXXXXXXXXX' + self.random.random_string(70-split) + key2 = self.versionstamped_keys.pack() + rand_str2 + index = key2.find('XXXXXXXXXX') + key2 += chr(index%256)+chr(index/256) + + instructions.push_args(u'SET_VERSIONSTAMPED_VALUE', key1, 'XXXXXXXXXX' + rand_str2) + instructions.append('ATOMIC_OP') + + instructions.push_args(u'SET_VERSIONSTAMPED_KEY', key2, rand_str1) + instructions.append('ATOMIC_OP') + self.can_use_key_selectors = False + + elif op == 'READ_CONFLICT_RANGE' or op == 'WRITE_CONFLICT_RANGE': + self.ensure_key(instructions, 2) + instructions.append(op) + self.add_strings(1) + + elif op == 'READ_CONFLICT_KEY' or op == 'WRITE_CONFLICT_KEY': + self.ensure_key(instructions, 1) + instructions.append(op) + self.add_strings(1) + + elif op == 'DISABLE_WRITE_CONFLICT': + instructions.append(op) + + elif op == 'COMMIT': + if args.concurrency == 1 or i < self.max_keys or random.random() < 0.9: + if args.concurrency == 1: + self.wait_for_reads(instructions) + test_util.blocking_commit(instructions) + self.add_stack_items(1) + self.is_committed = True + self.can_set_version = True + self.can_use_key_selectors = True + else: + instructions.append(op) + self.add_strings(1) + + elif op == 'RESET': + instructions.append(op) + self.is_committed = False + self.can_set_version = True + self.can_use_key_selectors = True + + elif op == 'CANCEL': + instructions.append(op) + self.is_committed = False + self.can_set_version = False + + elif op == 'GET_COMMITTED_VERSION': + if self.is_committed: + instructions.append(op) + self.has_version = True + self.add_strings(1) + + elif op == 'TUPLE_PACK' or op == 'TUPLE_RANGE': + tup = self.random.random_tuple(10) + instructions.push_args(len(tup), *tup) + instructions.append(op) + if op == 'TUPLE_PACK': + self.add_strings(1) + else: + self.add_strings(2) + + elif op == 'TUPLE_UNPACK': + tup = self.random.random_tuple(10) + instructions.push_args(len(tup), *tup) + instructions.append('TUPLE_PACK') + instructions.append(op) + self.add_strings(len(tup)) + + #Use SUB to test if integers are correctly unpacked + elif op == 'SUB': + a = self.random.random_int() / 2 + b = self.random.random_int() / 2 + instructions.push_args(0, a, b) + instructions.append(op) + instructions.push_args(1) + instructions.append('SWAP') + instructions.append(op) + instructions.push_args(1) + instructions.append('TUPLE_PACK') + self.add_stack_items(1) + + else: + assert False + + if op in reads or op in snapshot_reads: + self.outstanding_ops.append((self.stack_size, len(instructions)-1)) + + if args.concurrency == 1 and (op in database_reads or op in database_mutations): + instructions.append('WAIT_FUTURE') + + instructions.begin_finalization() + + if args.concurrency == 1: + self.wait_for_reads(instructions) + test_util.blocking_commit(instructions) + self.add_stack_items(1) + + instructions.append('NEW_TRANSACTION') + instructions.push_args(self.stack_subspace.key()) + instructions.append('LOG_STACK') + + test_util.blocking_commit(instructions) + + return instructions + + @fdb.transactional + def check_versionstamps(self, tr, begin_key, limit): + next_begin = None + incorrect_versionstamps = 0 + for k,v in tr.get_range(begin_key, self.versionstamped_values.range().stop, limit=limit): + next_begin = k + '\x00' + tup = fdb.tuple.unpack(k) + key = self.versionstamped_keys.pack() + v[10:].replace('XXXXXXXXXX', v[:10], 1) + if tr[key] != tup[-1]: + incorrect_versionstamps += 1 + + return (next_begin, incorrect_versionstamps) + + def validate(self, db, args): + errors = [] + + begin = self.versionstamped_values.range().start + incorrect_versionstamps = 0 + + while begin is not None: + (begin, current_incorrect_versionstamps) = self.check_versionstamps(db, begin, 100) + incorrect_versionstamps += current_incorrect_versionstamps + + if incorrect_versionstamps > 0: + errors.append('There were %d failed version stamp operations' % incorrect_versionstamps) + + return errors + + def get_result_specifications(self): + return [ + ResultSpecification(self.workspace, global_error_filter=[1007, 1021]), + ResultSpecification(self.stack_subspace, key_start_index=1, ordering_index=1, global_error_filter=[1007, 1021]) + ] + diff --git a/bindings/bindingtester/tests/directory.py b/bindings/bindingtester/tests/directory.py new file mode 100644 index 0000000000..c0037a61a8 --- /dev/null +++ b/bindings/bindingtester/tests/directory.py @@ -0,0 +1,373 @@ +# +# directory.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import random + +import fdb + +from bindingtester import FDB_API_VERSION +from bindingtester import util + +from bindingtester.tests import Test, Instruction, InstructionSet, ResultSpecification +from bindingtester.tests import test_util, directory_util + +from bindingtester.tests.directory_util import DirListEntry + +fdb.api_version(FDB_API_VERSION) + +class DirectoryTest(Test): + + def __init__(self, subspace): + super(DirectoryTest, self).__init__(subspace) + self.stack_subspace = subspace['stack'] + self.directory_log = subspace['directory_log']['directory'] + self.subspace_log = subspace['directory_log']['subspace'] + self.prefix_log = subspace['prefix_log'] + + self.prepopulated_dirs = [] + self.next_path = 1 + + def ensure_default_directory_subspace(self, instructions, path): + directory_util.create_default_directory_subspace(instructions, path, self.random) + + child = self.root.add_child((path,), path, self.root, DirListEntry(True, True)) + self.dir_list.append(child) + self.dir_index = directory_util.DEFAULT_DIRECTORY_INDEX + + def generate_layer(self): + if random.random < 0.7: + return '' + else: + choice = random.randint(0, 3) + if choice == 0: + return 'partition' + elif choice == 1: + return 'test_layer' + else: + return self.random.random_string(random.randint(0, 5)) + + def setup(self, args): + self.dir_index = 0 + self.random = test_util.RandomGenerator(args.max_int_bits) + + def generate(self, args, thread_number): + instructions = InstructionSet() + + op_choices = ['NEW_TRANSACTION', 'COMMIT'] + + general = ['DIRECTORY_CREATE_SUBSPACE', 'DIRECTORY_CREATE_LAYER'] + + op_choices += general + + directory_mutations = ['DIRECTORY_CREATE_OR_OPEN', 'DIRECTORY_CREATE', 'DIRECTORY_MOVE', 'DIRECTORY_MOVE_TO', + 'DIRECTORY_REMOVE', 'DIRECTORY_REMOVE_IF_EXISTS'] + directory_reads = ['DIRECTORY_EXISTS', 'DIRECTORY_OPEN', 'DIRECTORY_LIST'] + + directory_db_mutations = [x + '_DATABASE' for x in directory_mutations] + directory_db_reads = [x + '_DATABASE' for x in directory_reads] + directory_snapshot_reads = [x + '_SNAPSHOT' for x in directory_reads] + + directory = [] + directory += directory_mutations + directory += directory_reads + directory += directory_db_mutations + directory += directory_db_reads + directory += directory_snapshot_reads + + subspace = ['DIRECTORY_PACK_KEY', 'DIRECTORY_UNPACK_KEY', 'DIRECTORY_RANGE', 'DIRECTORY_CONTAINS', 'DIRECTORY_OPEN_SUBSPACE'] + + instructions.append('NEW_TRANSACTION') + + default_path = unicode('default%d' % self.next_path) + self.next_path += 1 + self.dir_list = directory_util.setup_directories(instructions, default_path, self.random) + self.root = self.dir_list[0] + + instructions.push_args(0) + instructions.append('DIRECTORY_CHANGE') + + # Generate some directories that we are going to create in advance. This tests that other bindings + # are compatible with the Python implementation + self.prepopulated_dirs = [ (generate_path(min_length=1), self.generate_layer()) for i in range(5) ] + + for path, layer in self.prepopulated_dirs: + instructions.push_args(layer) + instructions.push_args(*test_util.with_length(path)) + instructions.append('DIRECTORY_OPEN') + #print '%d. Selected %s, dir=%s, has_known_prefix=%s, dir_list_len=%d' % (len(instructions), 'DIRECTORY_OPEN', repr(self.dir_index), False, len(self.dir_list)) + self.dir_list.append(self.dir_list[0].add_child(path, default_path, self.root, DirListEntry(True, True, has_known_prefix=False))) + + instructions.setup_complete() + + for i in range(args.num_ops): + if random.random() < 0.5: + self.dir_index = random.randrange(0, len(self.dir_list)) + instructions.push_args(self.dir_index) + instructions.append('DIRECTORY_CHANGE') + + choices = op_choices[:] + if self.dir_list[self.dir_index].is_directory: + choices += directory + if self.dir_list[self.dir_index].is_subspace: + choices += subspace + + op = random.choice(choices) + dir_entry = self.dir_list[self.dir_index] + + #print '%d. Selected %s, dir=%s, has_known_prefix=%s, dir_list_len=%d' % (len(instructions), op, repr(self.dir_index), repr(dir_entry.has_known_prefix), len(self.dir_list)) + + if op.endswith('_DATABASE') or op.endswith('_SNAPSHOT'): + root_op = op[0:-9] + else: + root_op = op + + if root_op == 'NEW_TRANSACTION': + instructions.append(op) + + elif root_op == 'COMMIT': + test_util.blocking_commit(instructions) + + elif root_op == 'DIRECTORY_CREATE_SUBSPACE': + path = generate_path() + instructions.push_args(generate_prefix(allow_empty=False, is_partition=True)) + instructions.push_args(*test_util.with_length(path)) + instructions.append(op) + self.dir_list.append(DirListEntry(False, True)) + + elif root_op == 'DIRECTORY_CREATE_LAYER': + indices = [] + for i in range(2): + instructions.push_args(generate_prefix(allow_empty=False, is_partition=True)) + instructions.push_args(*test_util.with_length(generate_path())) + instructions.append('DIRECTORY_CREATE_SUBSPACE') + indices.append(len(self.dir_list)) + self.dir_list.append(DirListEntry(False, True)) + + instructions.push_args(random.choice([0,1])) + instructions.push_args(*indices) + instructions.append(op) + self.dir_list.append(DirListEntry(True, False, False)) + + elif root_op == 'DIRECTORY_CREATE_OR_OPEN': + # Because allocated prefixes are non-deterministic, we cannot have overlapping + # transactions that allocate/remove these prefixes in a comparison test + if op.endswith('_DATABASE') and args.concurrency == 1: + test_util.blocking_commit(instructions) + + path = generate_path() + op_args = test_util.with_length(path) + (self.generate_layer(),) + directory_util.push_instruction_and_record_prefix(instructions, op, op_args, path, len(self.dir_list), self.random, self.prefix_log) + + if not op.endswith('_DATABASE') and args.concurrency == 1: + test_util.blocking_commit(instructions) + + self.dir_list.append(dir_entry.add_child(path, default_path, self.root, DirListEntry(True, True, False))) + + elif root_op == 'DIRECTORY_CREATE': + layer = self.generate_layer() + is_partition = layer == 'partition' + + allow_empty_prefix = random.random() < 0.8 + prefix = generate_prefix(allow_empty=allow_empty_prefix, is_partition=is_partition) + + # Because allocated prefixes are non-deterministic, we cannot have overlapping + # transactions that allocate/remove these prefixes in a comparison test + if op.endswith('_DATABASE') and args.concurrency == 1: # and allow_empty_prefix: + test_util.blocking_commit(instructions) + + path = generate_path() + op_args = test_util.with_length(path) + (layer, prefix) + if prefix is None: + directory_util.push_instruction_and_record_prefix(instructions, op, op_args, path, len(self.dir_list), self.random, self.prefix_log) + else: + instructions.push_args(*op_args) + instructions.append(op) + + if not op.endswith('_DATABASE') and args.concurrency == 1: # and allow_empty_prefix: + test_util.blocking_commit(instructions) + + self.dir_list.append(dir_entry.add_child(path, default_path, self.root, DirListEntry(True, True, bool(prefix)))) + + elif root_op == 'DIRECTORY_OPEN': + path = generate_path() + instructions.push_args(self.generate_layer()) + instructions.push_args(*test_util.with_length(path)) + instructions.append(op) + self.dir_list.append(dir_entry.add_child(path, default_path, self.root, DirListEntry(True, True))) + + elif root_op == 'DIRECTORY_MOVE': + old_path = generate_path() + new_path = generate_path() + instructions.push_args(*(test_util.with_length(old_path) + test_util.with_length(new_path))) + instructions.append(op) + # This could probably be made to sometimes set has_known_prefix to true + self.dir_list.append(dir_entry.add_child(new_path, default_path, self.root, DirListEntry(True, True, False))) + + # Make sure that the default directory subspace still exists after moving the specified directory + if dir_entry.is_directory and not dir_entry.is_subspace and old_path == (u'',): + self.ensure_default_directory_subspace(instructions, default_path) + + elif root_op == 'DIRECTORY_MOVE_TO': + new_path = generate_path() + instructions.push_args(*test_util.with_length(new_path)) + instructions.append(op) + self.dir_list.append(dir_entry.root.add_child(new_path, default_path, self.root, DirListEntry(True, True, dir_entry.has_known_prefix))) + + # Make sure that the default directory subspace still exists after moving the current directory + self.ensure_default_directory_subspace(instructions, default_path) + + # FIXME: There is currently a problem with removing partitions. In these generated tests, it's possible + # for a removed partition to resurrect itself and insert keys into the database using its allocated + # prefix. The result is non-deterministic HCA errors. + elif root_op == 'DIRECTORY_REMOVE' or root_op == 'DIRECTORY_REMOVE_IF_EXISTS': + # Because allocated prefixes are non-deterministic, we cannot have overlapping + # transactions that allocate/remove these prefixes in a comparison test + if op.endswith('_DATABASE') and args.concurrency == 1: + test_util.blocking_commit(instructions) + + path = () + count = random.randint(0, 1) + if count == 1: + path = generate_path() + instructions.push_args(*test_util.with_length(path)) + instructions.push_args(count) + + instructions.append(op) + + # Make sure that the default directory subspace still exists after removing the specified directory + if path == () or (dir_entry.is_directory and not dir_entry.is_subspace and path == (u'',)): + self.ensure_default_directory_subspace(instructions, default_path) + + elif root_op == 'DIRECTORY_LIST' or root_op == 'DIRECTORY_EXISTS': + path = () + count = random.randint(0, 1) + if count == 1: + path = generate_path() + instructions.push_args(*test_util.with_length(path)) + instructions.push_args(count) + instructions.append(op) + + elif root_op == 'DIRECTORY_PACK_KEY': + t = self.random.random_tuple(5) + instructions.push_args(*test_util.with_length(t)) + instructions.append(op) + instructions.append('DIRECTORY_STRIP_PREFIX') + + elif root_op == 'DIRECTORY_UNPACK_KEY' or root_op == 'DIRECTORY_CONTAINS': + if not dir_entry.has_known_prefix or random.random() < 0.2 or root_op == 'DIRECTORY_UNPACK_KEY': + t = self.random.random_tuple(5) + instructions.push_args(*test_util.with_length(t)) + instructions.append('DIRECTORY_PACK_KEY') + instructions.append(op) + else: + instructions.push_args(fdb.tuple.pack(self.random.random_tuple(5))) + instructions.append(op) + + elif root_op == 'DIRECTORY_RANGE' or root_op == 'DIRECTORY_OPEN_SUBSPACE': + t = self.random.random_tuple(5) + instructions.push_args(*test_util.with_length(t)) + instructions.append(op) + if root_op == 'DIRECTORY_OPEN_SUBSPACE': + self.dir_list.append(DirListEntry(False, True, dir_entry.has_known_prefix)) + else: + test_util.to_front(instructions, 1) + instructions.append('DIRECTORY_STRIP_PREFIX') + test_util.to_front(instructions, 1) + instructions.append('DIRECTORY_STRIP_PREFIX') + + instructions.begin_finalization() + + test_util.blocking_commit(instructions) + + instructions.append('NEW_TRANSACTION') + + for i, dir_entry in enumerate(self.dir_list): + instructions.push_args(i) + instructions.append('DIRECTORY_CHANGE') + if dir_entry.is_directory: + instructions.push_args(self.directory_log.key()) + instructions.append('DIRECTORY_LOG_DIRECTORY') + if dir_entry.has_known_prefix and dir_entry.is_subspace: + #print '%d. Logging subspace: %d' % (i, dir_entry.dir_id) + instructions.push_args(self.subspace_log.key()) + instructions.append('DIRECTORY_LOG_SUBSPACE') + if (i+1) % 100 == 0: + test_util.blocking_commit(instructions) + + instructions.push_args(self.stack_subspace.key()) + instructions.append('LOG_STACK') + + test_util.blocking_commit(instructions) + return instructions + + def pre_run(self, db, args): + for (path, layer) in self.prepopulated_dirs: + try: + util.get_logger().debug('Prepopulating directory: %r (layer=%r)' % (path, layer)) + fdb.directory.create_or_open(db, path, layer) + except Exception as e: + util.get_logger().debug('Could not create directory %r: %r' % (path, e)) + pass + + def validate(self, db, args): + errors = [] + # This check doesn't work in the current test because of the way we use partitions. + # If a partition is created, allocates a prefix, and then is removed, subsequent prefix + # allocations could collide with prior ones. We can get around this by not allowing + # a removed directory (or partition) to be used, but that weakens the test in another way. + #errors += directory_util.check_for_duplicate_prefixes(db, self.prefix_log) + return errors + + def get_result_specfications(self): + return [ + ResultSpecification(self.stack, key_start_index=1, ordering_index=1), + ResultSpecification(self.directory_log, ordering_index=0), + ResultSpecification(self.subspace_log, ordering_index=0) + ] + +# Utility functions +def generate_path(min_length = 0): + length = int(random.random() * random.random() * (4 - min_length)) + min_length + path = () + for i in range(length): + if random.random() < 0.05: + path = path + (u'',) + else: + path = path + (random.choice([u'1', u'2', u'3']),) + + return path + +def generate_prefix(allow_empty=True, is_partition=False): + if allow_empty and random.random() < 0.8: + return None + elif is_partition or random.random() < 0.5: + length = random.randint(0 if allow_empty else 1, 5) + if length == 0: + return '' + + if not is_partition: + first = chr(random.randint(ord('\x1d'), 255) % 255) + return first + ''.join(chr(random.randrange(0, 256)) for i in range(0, length-1)) + else: + return ''.join(chr(random.randrange(ord('\x02'), ord('\x14'))) for i in range(0, length)) + else: + prefix = 'abcdefg' + generated = prefix[0:random.randrange(0 if allow_empty else 1, len(prefix))] + return generated diff --git a/bindings/bindingtester/tests/directory_hca.py b/bindings/bindingtester/tests/directory_hca.py new file mode 100644 index 0000000000..e017c64b57 --- /dev/null +++ b/bindings/bindingtester/tests/directory_hca.py @@ -0,0 +1,130 @@ +# +# directory_hca.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import random + +import fdb + +from bindingtester import FDB_API_VERSION +from bindingtester import util + +from bindingtester.tests import Test, Instruction, InstructionSet, ResultSpecification +from bindingtester.tests import test_util, directory_util + +fdb.api_version(FDB_API_VERSION) + +class DirectoryHcaTest(Test): + def __init__(self, subspace): + super(DirectoryHcaTest, self).__init__(subspace) + self.coordination = subspace['coordination'] + self.prefix_log = subspace['prefix_log'] + self.next_path = 1 + + def setup(self, args): + self.random = test_util.RandomGenerator(args.max_int_bits) + self.transactions = ['tr%d' % i for i in range(3)] # SOMEDAY: parameterize this number? + self.barrier_num = 0 + + self.max_directories_per_transaction = 30 + if args.api_version < 300: + if args.concurrency > 8: + raise Exception('Directory HCA test does not support concurrency larger than 8 with API version less than 300') + + self.max_directories_per_transaction = 8.0 / args.concurrency + + def commit_transactions(self, instructions, args): + for tr in self.transactions: + if random.random() < 0.8 or args.api_version < 300: + instructions.push_args(tr) + instructions.append('USE_TRANSACTION') + test_util.blocking_commit(instructions) + + def barrier(self, instructions, thread_number, thread_ending=False): + if not thread_ending: + instructions.push_args(self.coordination[(self.barrier_num+1)][thread_number].key(), '') + instructions.append('SET_DATABASE') + instructions.append('WAIT_FUTURE') + + instructions.push_args(self.coordination[self.barrier_num][thread_number].key()) + instructions.append('CLEAR_DATABASE') + instructions.append('WAIT_FUTURE') + instructions.push_args(self.coordination[self.barrier_num].key()) + instructions.append('WAIT_EMPTY') + + self.barrier_num += 1 + + def generate(self, args, thread_number): + instructions = InstructionSet() + + instructions.append('NEW_TRANSACTION') + + default_path = unicode('default%d' % self.next_path) + self.next_path += 1 + dir_list = directory_util.setup_directories(instructions, default_path, self.random) + num_dirs = len(dir_list) + + instructions.push_args(directory_util.DEFAULT_DIRECTORY_INDEX) + instructions.append('DIRECTORY_CHANGE') + + instructions.setup_complete() + + current_op = 0 + + while current_op < args.num_ops: + if args.concurrency > 1: + self.barrier(instructions, thread_number) + + instructions.push_args(random.choice(self.transactions)) + instructions.append('USE_TRANSACTION') + + if thread_number == 0 and args.concurrency > 1: + num_directories = 1 + else: + num_directories = int(max(1, pow(random.random(), 4) * min(self.max_directories_per_transaction, args.num_ops - current_op))) + + for i in range(num_directories): + path = (self.random.random_unicode_str(16),) + op_args = test_util.with_length(path) + ('', None) + directory_util.push_instruction_and_record_prefix(instructions, 'DIRECTORY_CREATE', op_args, path, num_dirs, self.random, self.prefix_log) + num_dirs += 1 + + current_op += num_directories + + if args.concurrency > 1: + self.barrier(instructions, thread_number, thread_ending=(current_op >= args.num_ops)) + + if thread_number == 0: + self.commit_transactions(instructions, args) + + return instructions + + @fdb.transactional + def pre_run(self, tr, args): + if args.concurrency > 1: + for i in range(args.concurrency): + tr[self.coordination[0][i]] = '' + + def validate(self, db, args): + errors = [] + errors += directory_util.check_for_duplicate_prefixes(db, self.prefix_log) + errors += directory_util.validate_hca_state(db) + + return errors + diff --git a/bindings/bindingtester/tests/directory_util.py b/bindings/bindingtester/tests/directory_util.py new file mode 100644 index 0000000000..de49ac9957 --- /dev/null +++ b/bindings/bindingtester/tests/directory_util.py @@ -0,0 +1,191 @@ +# +# directory_util.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import random +import struct + +import fdb + +from bindingtester import FDB_API_VERSION +from bindingtester import util + +from bindingtester.tests import test_util + +fdb.api_version(FDB_API_VERSION) + +DEFAULT_DIRECTORY_INDEX = 4 +DEFAULT_DIRECTORY_PREFIX = 'default' +DIRECTORY_ERROR_STRING = 'DIRECTORY_ERROR' + +class DirListEntry: + dir_id = 0 # Used for debugging + + def __init__(self, is_directory, is_subspace, has_known_prefix=True, path=(), root=None): + self.root = root or self + self.path = path + self.is_directory = is_directory + self.is_subspace = is_subspace + self.has_known_prefix = has_known_prefix + self.children = {} + + self.dir_id = DirListEntry.dir_id + 1 + DirListEntry.dir_id += 1 + + def __repr__(self): + return 'DirEntry %d %r: %d' % (self.dir_id, self.path, self.has_known_prefix) + + def add_child(self, subpath, default_path, root, child): + if default_path in root.children: + #print 'Adding child %r to default directory %r at %r' % (child, root.children[DirectoryTest.DEFAULT_DIRECTORY_PATH].path, subpath) + c = root.children[default_path]._add_child_impl(subpath, child) + child.has_known_prefix = c.has_known_prefix and child.has_known_prefix + #print 'Added %r' % c + + #print 'Adding child %r to directory %r at %r' % (child, self.path, subpath) + c = self._add_child_impl(subpath, child) + #print 'Added %r' % c + return c + + def _add_child_impl(self, subpath, child): + #print '%d, %d. Adding child (recursive): %s %s' % (self.dir_id, child.dir_id, repr(self.path), repr(subpath)) + if len(subpath) == 0: + self.has_known_prefix = self.has_known_prefix and child.has_known_prefix + #print '%d, %d. Setting child: %d' % (self.dir_id, child.dir_id, self.has_known_prefix) + self._merge_children(child) + + return self + else: + if not subpath[0] in self.children: + #print '%d, %d. Path %s was absent (%s)' % (self.dir_id, child.dir_id, repr(self.path + subpath[0:1]), repr(self.children)) + subdir = DirListEntry(True, True, path = self.path+subpath[0:1], root = self.root) + subdir.has_known_prefix = len(subpath) == 1 + self.children[subpath[0]] = subdir + else: + subdir = self.children[subpath[0]] + subdir.has_known_prefix = False + #print '%d, %d. Path was present' % (self.dir_id, child.dir_id) + + return subdir._add_child_impl(subpath[1:], child) + + def _merge_children(self, other): + for c in other.children: + if not c in self.children: + self.children[c] = other.children[c] + else: + self.children[c].has_known_prefix = self.children[c].has_known_prefix and other.children[c].has_known_prefix + self.children[c]._merge_children(other.children[c]) + +def setup_directories(instructions, default_path, random): + dir_list = [DirListEntry(True, False, True)] + instructions.push_args(0, '\xfe') + instructions.append('DIRECTORY_CREATE_SUBSPACE') + dir_list.append(DirListEntry(False, True)) + + instructions.push_args(0, '') + instructions.append('DIRECTORY_CREATE_SUBSPACE') + dir_list.append(DirListEntry(False, True)) + + instructions.push_args(1, 2, 1) + instructions.append('DIRECTORY_CREATE_LAYER') + dir_list.append(DirListEntry(True, False, True)) + + create_default_directory_subspace(instructions, default_path, random) + dir_list.append(DirListEntry(True, True, True)) + + instructions.push_args(DEFAULT_DIRECTORY_INDEX) + instructions.append('DIRECTORY_SET_ERROR_INDEX') + + return dir_list + +def create_default_directory_subspace(instructions, path, random): + test_util.blocking_commit(instructions) + instructions.push_args(3) + instructions.append('DIRECTORY_CHANGE') + prefix = random.random_string(16) + instructions.push_args(1, path, '', '%s-%s' % (DEFAULT_DIRECTORY_PREFIX, prefix)) + instructions.append('DIRECTORY_CREATE_DATABASE') + + instructions.push_args(DEFAULT_DIRECTORY_INDEX) + instructions.append('DIRECTORY_CHANGE') + +def push_instruction_and_record_prefix(instructions, op, op_args, path, dir_index, random, subspace): + if not op.endswith('_DATABASE'): + instructions.push_args(1, *test_util.with_length(path)) + instructions.append('DIRECTORY_EXISTS') + + # This op must leave the stack in the state it is in at this point, with the exception + # that it may leave an error on the stack + instructions.push_args(*op_args) + instructions.append(op) + + if not op.endswith('_DATABASE'): + instructions.push_args(dir_index) + instructions.append('DIRECTORY_CHANGE') + + instructions.push_args(1, '', random.random_string(16), '') + instructions.append('DIRECTORY_PACK_KEY') + test_util.to_front(instructions, 3) # move the existence result up to the front of the stack + + t = util.subspace_to_tuple(subspace) + instructions.push_args(len(t) + 3, *t) + + instructions.append('TUPLE_PACK') # subspace[][][random.random_string(16)] = '' + instructions.append('SET') + + instructions.push_args(DEFAULT_DIRECTORY_INDEX) + instructions.append('DIRECTORY_CHANGE') + +def check_for_duplicate_prefixes(db, subspace): + last_prefix = None + start_key = subspace[0].range().start + + duplicates = set() + count = 0 + while True: + prefixes = db.get_range(start_key, subspace[0].range().stop, limit=1000) + if len(prefixes) == 0: + break + + start_key = fdb.KeySelector.first_greater_than(prefixes[-1].key) + + prefixes = [subspace[0].unpack(kv.key)[0] for kv in prefixes] + prefixes = [p for p in prefixes if not (p.startswith(DEFAULT_DIRECTORY_PREFIX) or p == DIRECTORY_ERROR_STRING)] + count += len(prefixes) + + prefixes = [last_prefix] + prefixes + duplicates.update([p for i,p in enumerate(prefixes[1:]) if p == prefixes[i]]) + last_prefix = prefixes[-1] + + util.get_logger().info('Checked %d directory prefixes for duplicates' % count) + return ['The prefix %r was allocated multiple times' % d[:-2] for d in set(duplicates)] + +def validate_hca_state(db): + hca = fdb.Subspace(('\xfe', 'hca'), '\xfe') + counters = hca[0] + recent = hca[1] + + last_counter = db.get_range(counters.range().start, counters.range().stop, limit=1, reverse=True) + [(start, reported_count)] = [(counters.unpack(kv.key)[0], struct.unpack(' reported_count: + return ['The HCA reports %d prefixes allocated in current window, but it actually allocated %d' % (reported_count, actual_count)] + + return [] diff --git a/bindings/bindingtester/tests/scripted.py b/bindings/bindingtester/tests/scripted.py new file mode 100644 index 0000000000..4237613e42 --- /dev/null +++ b/bindings/bindingtester/tests/scripted.py @@ -0,0 +1,395 @@ +# +# scripted.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import random + +import fdb + +from bindingtester import FDB_API_VERSION +from bindingtester import Result + +from bindingtester.tests import Test, Instruction, ThreadedInstructionSet, ResultSpecification +from bindingtester.tests import test_util + +fdb.api_version(FDB_API_VERSION) + +# SOMEDAY: This should probably be broken up into smaller tests +class ScriptedTest(Test): + TEST_API_VERSION = 500 + + def __init__(self, subspace): + super(ScriptedTest, self).__init__(subspace, ScriptedTest.TEST_API_VERSION, ScriptedTest.TEST_API_VERSION) + self.workspace = self.subspace['workspace'] + self.results_subspace = self.subspace['results'] + #self.thread_subspace = self.subspace['threads'] # TODO: update START_THREAD so that we can create threads in subspaces + + def setup(self, args): + if args.concurrency > 1: + raise Exception('Scripted tests cannot be run with a concurrency greater than 1') + + # SOMEDAY: this is only a limitation because we don't know how many operations the bisection should start with + # it should be fixable. + # + # We also need to enable the commented out support for num_ops in this file and make it so the default value runs + # the entire test + if args.bisect: + raise Exception('Scripted tests cannot be bisected') + + def generate(self, args, thread_number): + self.results = [] + + test_instructions = ThreadedInstructionSet() + main_thread = test_instructions.create_thread() + + foo = [self.workspace.pack(('foo%d' % i,)) for i in range(0,6)] + + main_thread.append('NEW_TRANSACTION') + main_thread.push_args(1020) + main_thread.append('ON_ERROR') + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + main_thread.append('GET_READ_VERSION') + main_thread.push_args(foo[1], 'bar') + main_thread.append('SET') + main_thread.push_args(foo[1]) + main_thread.append('GET') + self.add_result(main_thread, args, 'bar') + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + + main_thread.push_args(2000) + main_thread.append('ON_ERROR') + self.add_result(main_thread, args, test_util.error_string(2000)) + + main_thread.append('NEW_TRANSACTION') + main_thread.push_args(0) + main_thread.append('ON_ERROR') + self.add_result(main_thread, args, test_util.error_string(2000)) + + main_thread.append('NEW_TRANSACTION') + main_thread.push_args(foo[1]) + main_thread.append('DUP') + main_thread.append('DUP') + main_thread.append('GET') + self.add_result(main_thread, args, 'bar') + main_thread.append('CLEAR') + main_thread.append('GET_SNAPSHOT') + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + main_thread.push_args(foo[1]) + main_thread.append('GET_DATABASE') + self.add_result(main_thread, args, 'bar') + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + + main_thread.append('SET_READ_VERSION') + main_thread.push_args(foo[1]) + main_thread.append('DUP') + main_thread.append('GET') + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + main_thread.append('CLEAR') + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, test_util.error_string(1020)) + + main_thread.push_args(foo[1]) + main_thread.append('GET_SNAPSHOT') + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + main_thread.push_args(foo[1]) + main_thread.append('CLEAR') + main_thread.append('COMMIT') + main_thread.append('WAIT_FUTURE') + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + main_thread.append('GET_COMMITTED_VERSION') + main_thread.append('RESET') + main_thread.append('EMPTY_STACK') + + main_thread.append('NEW_TRANSACTION') + main_thread.push_args(1, 'bar', foo[1], foo[2], 'bar2', foo[3], 'bar3', foo[4], 'bar4', foo[5], 'bar5') + main_thread.append('SWAP') + main_thread.append('SET') + main_thread.append('SET') + main_thread.append('SET') + main_thread.append('SET') + main_thread.append('SET_DATABASE') + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + + main_thread.append('SET_READ_VERSION') + main_thread.push_args(foo[2]) + main_thread.append('GET') + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + + main_thread.append('NEW_TRANSACTION') + main_thread.push_args('', 0, -1, '') + main_thread.append('GET_KEY') + self.add_result(main_thread, args, '') + + main_thread.append('NEW_TRANSACTION') + main_thread.append('GET_READ_VERSION_SNAPSHOT') + main_thread.push_args('random', foo[1], foo[3], 0, 1, 1) + main_thread.append('POP') + main_thread.append('GET_RANGE') + self.add_result(main_thread, args, fdb.tuple.pack((foo[2], 'bar2', foo[1], 'bar'))) + main_thread.push_args(foo[1], foo[3], 1, 1, 0) + main_thread.append('GET_RANGE_SNAPSHOT') + self.add_result(main_thread, args, fdb.tuple.pack((foo[2], 'bar2'))) + main_thread.push_args(foo[1], foo[3], 0, 0, 4) + main_thread.append('GET_RANGE_DATABASE') + self.add_result(main_thread, args, fdb.tuple.pack((foo[1], 'bar', foo[2], 'bar2'))) + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + + main_thread.push_args(foo[3], foo[5]) + main_thread.append('CLEAR_RANGE') + main_thread.push_args(foo[1], 0, 3, '') + main_thread.append('GET_KEY') + self.add_result(main_thread, args, foo[5]) + main_thread.push_args(foo[1], 1, 2, '') + main_thread.append('GET_KEY_SNAPSHOT') + self.add_result(main_thread, args, foo[5]) + main_thread.push_args(foo[5], 0, -2, '') + main_thread.append('GET_KEY_DATABASE') + self.add_result(main_thread, args, foo[2]) + main_thread.push_args(self.workspace.key(), 2, 0, 2) + main_thread.append('GET_RANGE_STARTS_WITH') + self.add_result(main_thread, args, fdb.tuple.pack((foo[1], 'bar', foo[2], 'bar2'))) + main_thread.push_args(self.workspace.key(), 4, 0, 3) + main_thread.append('GET_RANGE_STARTS_WITH_SNAPSHOT') + self.add_result(main_thread, args, fdb.tuple.pack((foo[1], 'bar', foo[2], 'bar2', foo[5], 'bar5'))) + main_thread.push_args(self.workspace.key(), 3, 1, -1) + main_thread.append('GET_RANGE_STARTS_WITH_DATABASE') + self.add_result(main_thread, args, fdb.tuple.pack((foo[5], 'bar5', foo[4], 'bar4', foo[3], 'bar3'))) + main_thread.push_args(foo[1], 0, 1, foo[1], 0, 3, 0, 0, -1, '') + main_thread.append('GET_RANGE_SELECTOR') + self.add_result(main_thread, args, fdb.tuple.pack((foo[1], 'bar', foo[2], 'bar2'))) + main_thread.push_args(foo[1], 1, 0, foo[1], 1, 3, 0, 0, -1, '') + main_thread.append('GET_RANGE_SELECTOR_SNAPSHOT') + self.add_result(main_thread, args, fdb.tuple.pack((foo[1], 'bar', foo[2], 'bar2', foo[5], 'bar5'))) + main_thread.push_args(foo[1], 0, 1, foo[1], 1, 3, 0, 0, -1, '') + main_thread.append('GET_RANGE_SELECTOR_DATABASE') + self.add_result(main_thread, args, fdb.tuple.pack((foo[1], 'bar', foo[2], 'bar2', foo[3], 'bar3'))) + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + + main_thread.push_args(self.workspace.key()) + main_thread.append('CLEAR_RANGE_STARTS_WITH') + main_thread.push_args(self.workspace.key(), 0, 0, -1) + main_thread.append('GET_RANGE_STARTS_WITH') + self.add_result(main_thread, args, '') + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + + main_thread.append('SET_READ_VERSION') + main_thread.push_args(foo[1]) + main_thread.append('GET') + self.add_result(main_thread, args, 'bar') + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + + main_thread.push_args(foo[1], 'bar', foo[2], 'bar2', foo[3], 'bar3', foo[4], 'bar4', foo[5], 'bar5') + main_thread.append('SET') + main_thread.append('SET') + main_thread.append('SET') + main_thread.append('SET') + main_thread.append('SET') + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + + main_thread.push_args(foo[2]) + main_thread.append('CLEAR_DATABASE') + main_thread.append('WAIT_FUTURE') + main_thread.push_args(self.workspace.key(), 0, 0, -1) + main_thread.append('GET_RANGE_STARTS_WITH_DATABASE') + self.add_result(main_thread, args, fdb.tuple.pack((foo[1], 'bar', foo[3], 'bar3', foo[4], 'bar4', foo[5], 'bar5'))) + + main_thread.push_args(foo[3], foo[5]) + main_thread.append('CLEAR_RANGE_DATABASE') + main_thread.append('WAIT_FUTURE') + main_thread.push_args(self.workspace.key(), 0, 0, -1) + main_thread.append('GET_RANGE_STARTS_WITH_DATABASE') + self.add_result(main_thread, args, fdb.tuple.pack((foo[1], 'bar', foo[5], 'bar5'))) + + main_thread.push_args(self.workspace.key()) + main_thread.append('CLEAR_RANGE_STARTS_WITH_DATABASE') + main_thread.append('WAIT_FUTURE') + main_thread.push_args(self.workspace.key(), 0, 0, -1) + main_thread.append('GET_RANGE_STARTS_WITH_DATABASE') + self.add_result(main_thread, args, '') + + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + + main_thread.append('NEW_TRANSACTION') + main_thread.push_args(foo[1], foo[5], 0, 0, 0) + main_thread.append('GET_RANGE') + self.add_result(main_thread, args, test_util.error_string(2210)) + main_thread.push_args(foo[1], foo[5], 0, 0, 0) + main_thread.append('GET_RANGE_DATABASE') + self.add_result(main_thread, args, test_util.error_string(2210)) + + self.append_range_test(main_thread, args, 100, 256) + self.append_range_test(main_thread, args, 1000, 8) + + main_thread.append('EMPTY_STACK') + tup = (0, 'foo', -1093, u'unicode\u9348test', 0xffffffff + 100, 'bar\x00\xff') + main_thread.push_args(*test_util.with_length(tup)) + main_thread.append('TUPLE_PACK') + main_thread.append('DUP') + self.add_result(main_thread, args, fdb.tuple.pack(tup)) + main_thread.append('TUPLE_UNPACK') + for item in reversed(tup): + self.add_result(main_thread, args, fdb.tuple.pack((item,))) + + main_thread.push_args(0xffffffff, -100) + main_thread.append('SUB') + main_thread.push_args(1) + main_thread.append('TUPLE_PACK') + self.add_result(main_thread, args, fdb.tuple.pack((0xffffffff + 100,))) + + main_thread.append('EMPTY_STACK') + main_thread.push_args(*test_util.with_length(tup)) + main_thread.append('TUPLE_RANGE') + rng = fdb.tuple.range(tup) + self.add_result(main_thread, args, rng.stop) + self.add_result(main_thread, args, rng.start) + + stampKey = 'stampedXXXXXXXXXXsuffix' + stampKeyIndex = stampKey.find('XXXXXXXXXX') + stampKeyStr = chr(stampKeyIndex%256) + chr(stampKeyIndex/256) + main_thread.push_args(u'SET_VERSIONSTAMPED_KEY', stampKey + stampKeyStr, 'stampedBar') + main_thread.append('ATOMIC_OP') + main_thread.push_args(u'SET_VERSIONSTAMPED_VALUE', 'stampedValue', 'XXXXXXXXXX') + main_thread.append('ATOMIC_OP') + + main_thread.push_args('suffix') + main_thread.append('GET_VERSIONSTAMP') + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + main_thread.push_args('stamped') + main_thread.append('CONCAT') + main_thread.append('CONCAT') + main_thread.append('GET') + self.add_result(main_thread, args, 'stampedBar') + + main_thread.push_args('stampedValue', 'suffix') + main_thread.append('GET') + main_thread.push_args('stamped') + main_thread.append('CONCAT') + main_thread.append('CONCAT') + main_thread.append('GET') + self.add_result(main_thread, args, 'stampedBar') + + main_thread.append('GET_VERSIONSTAMP') + test_util.blocking_commit(main_thread) + self.add_result(main_thread, args, 'RESULT_NOT_PRESENT') + self.add_result(main_thread, args, test_util.error_string(2021)) + + main_thread.push_args('sentinel') + main_thread.append('UNIT_TESTS') + self.add_result(main_thread, args, 'sentinel') + + if not args.no_threads: + wait_key = 'waitKey' + #threads = [self.thread_subspace[i] for i in range(0, 2)] + threads = ['thread_spec%d' % i for i in range(0, 2)] + for thread_spec in threads: + main_thread.push_args(self.workspace.pack((wait_key, thread_spec)), '') + main_thread.append('SET_DATABASE') + main_thread.append('WAIT_FUTURE') + + for thread_spec in threads: + main_thread.push_args(thread_spec) + #if len(main_thread) < args.num_ops: + main_thread.append('START_THREAD') + thread = test_instructions.create_thread(fdb.Subspace((thread_spec,))) + thread.append('NEW_TRANSACTION') + thread.push_args(foo[1], foo[1], 'bar%s' % thread_spec, self.workspace.pack((wait_key, thread_spec)), self.workspace.pack((wait_key, thread_spec))) + thread.append('GET') + thread.append('POP') + thread.append('SET') + thread.append('CLEAR') + test_util.blocking_commit(thread) + thread.append('POP') + thread.append('CLEAR_DATABASE') + thread.push_args(self.workspace.pack((wait_key,))) + thread.append('WAIT_EMPTY') + + thread.append('NEW_TRANSACTION') + thread.push_args(foo[1]) + thread.append('GET') + self.add_result(thread, args, 'barthread_spec0', 'barthread_spec1') + + main_thread.append('EMPTY_STACK') + #if len(main_thread) > args.num_ops: + #main_thread[args.num_ops:] = [] + + return test_instructions + + def get_result_specifications(self): + return [ + ResultSpecification(self.results_subspace, ordering_index=0, global_error_filter=[1007, 1021]) + ] + + def get_expected_results(self): + return { self.results_subspace : self.results } + + def append_range_test(self, instructions, args, num_pairs, kv_length): + instructions.append('NEW_TRANSACTION') + + instructions.push_args(self.workspace.key()) + instructions.append('CLEAR_RANGE_STARTS_WITH') + + kvpairs = [] + for i in range(0, num_pairs*2): + kvpairs.append(self.workspace.pack(('foo', ''.join(chr(random.randint(0, 254)) for i in range(0, kv_length))))) + + kvpairs = list(set(kvpairs)) + if len(kvpairs) % 2 == 1: + kvpairs = kvpairs[:-1] + kvpairs.sort() + + instructions.push_args(*kvpairs) + for i in range(0, len(kvpairs)/2): + instructions.append('SET') + if i % 100 == 99: + test_util.blocking_commit(instructions) + self.add_result(instructions, args, 'RESULT_NOT_PRESENT') + + foo_range = self.workspace.range(('foo',)) + instructions.push_args(foo_range.start, foo_range.stop, 0, 0, -1) + instructions.append('GET_RANGE') + self.add_result(instructions, args, fdb.tuple.pack(tuple(kvpairs))) + instructions.push_args(self.workspace.key(), 0, 0, -1) + instructions.append('GET_RANGE_STARTS_WITH') + self.add_result(instructions, args, fdb.tuple.pack(tuple(kvpairs))) + instructions.push_args(foo_range.start, 0, 1, foo_range.stop, 0, 1, 0, 0, -1, '') + instructions.append('GET_RANGE_SELECTOR') + self.add_result(instructions, args, fdb.tuple.pack(tuple(kvpairs))) + test_util.blocking_commit(instructions) + self.add_result(instructions, args, 'RESULT_NOT_PRESENT') + + def add_result(self, instructions, args, *values): + key = self.results_subspace.pack((len(self.results),)) + instructions.push_args(key) + instructions.append('SET_DATABASE') + + #if len(instructions) <= args.num_ops: + self.results.append(Result(self.results_subspace, key, values)) + + instructions.append('POP') + diff --git a/bindings/bindingtester/tests/test_util.py b/bindings/bindingtester/tests/test_util.py new file mode 100644 index 0000000000..1e10dd5a92 --- /dev/null +++ b/bindings/bindingtester/tests/test_util.py @@ -0,0 +1,125 @@ +# +# test_util.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import random +import unicodedata + +import fdb + +from bindingtester import util + +class RandomGenerator(object): + def __init__(self, max_int_bits=64): + self.max_int_bits = max_int_bits + + def random_unicode_str(self, length): + return u''.join(self.random_unicode_char() for i in range(0, length)) + + def random_int(self): + num_bits = random.randint(0, self.max_int_bits) # This way, we test small numbers with higher probability + + max_value = (1 << num_bits) - 1 + min_value = -max_value - 1 + num = random.randint(min_value, max_value) + + #util.get_logger().debug('generating int (%d): %d - %s' % (num_bits, num, repr(fdb.tuple.pack((num,))))) + return num + + def random_tuple(self, max_size): + size = random.randint(1, max_size) + tup = [] + + for i in range(size): + choice = random.randint(0, 3) + if choice == 0: + tup.append(self.random_int()) + elif choice == 1: + tup.append(None) + elif choice == 2: + tup.append(self.random_string(random.randint(0, 100))) + elif choice == 3: + tup.append(self.random_unicode_str(random.randint(0, 100))) + else: + assert false + + return tuple(tup) + + def random_range_params(self): + if random.random() < 0.75: + limit = random.randint(1, 1e3) + elif random.random() < 0.75: + limit = 0 + else: + limit = random.randint(1e8, (1<<31)-1) + + return (limit, random.randint(0, 1), random.randint(-2, 4)) + + def random_selector_params(self): + if random.random() < 0.9: + offset = random.randint(-20, 20) + else: + offset = random.randint(-1000, 1000) + + return (random.randint(0, 1), offset) + + def random_string(self, length): + if length == 0: + return '' + + return chr(random.randint(0, 254)) + ''.join(chr(random.randint(0, 255)) for i in range(0, length-1)) + + def random_unicode_char(self): + while True: + c = random.randint(0, 0xffff) + if unicodedata.category(unichr(c))[0] in 'LMNPSZ': + return unichr(c) + + +def error_string(error_code): + return fdb.tuple.pack(('ERROR', str(error_code))) + +def blocking_commit(instructions): + instructions.append('COMMIT') + instructions.append('WAIT_FUTURE') + instructions.append('RESET') + +def to_front(instructions, index): + if index == 0: + pass + elif index == 1: + instructions.push_args(1) + instructions.append('SWAP') + elif index == 2: + instructions.push_args(index-1) + instructions.append('SWAP') + instructions.push_args(index) + instructions.append('SWAP') + else: + instructions.push_args(index-1) + instructions.append('SWAP') + instructions.push_args(index) + instructions.append('SWAP') + instructions.push_args(index-1) + instructions.append('SWAP') + to_front(instructions, index-1) + +def with_length(tup): + return (len(tup),) + tup + diff --git a/bindings/bindingtester/util.py b/bindings/bindingtester/util.py new file mode 100644 index 0000000000..486fa5d0cd --- /dev/null +++ b/bindings/bindingtester/util.py @@ -0,0 +1,72 @@ +# +# util.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import logging +import signal +import os +import glob + +import fdb + +def initialize_logger_level(logging_level): + logger = get_logger() + + assert logging_level in ["DEBUG", "INFO", "WARNING", "ERROR"] + + if logging_level == "DEBUG": + logger.setLevel(logging.DEBUG) + elif logging_level == "INFO": + logger.setLevel(logging.INFO) + elif logging_level == "WARNING": + logger.setLevel(logging.WARNING) + elif logging_level == "ERROR": + logger.setLevel(logging.ERROR) + +def get_logger(): + return logging.getLogger('foundationdb.bindingtester') + +# Attempts to get the name associated with a process termination signal +def signal_number_to_name(signal_num): + name = [] + for key in signal.__dict__.keys(): + if key.startswith("SIG") and getattr(signal, key) == signal_num: + name.append(key) + if len(name) == 1: + return name[0] + else: + return str(signal_num) + +def import_subclasses(filename, module_path): + for f in glob.glob(os.path.join(os.path.dirname(filename), '*.py')): + fn = os.path.basename(f) + if fn == '__init__.py': + continue + __import__('%s.%s' % (module_path, os.path.splitext(fn)[0])) + +# Attempts to unpack a subspace +# This throws an exception if the subspace cannot be unpacked as a tuple +# As a result, the binding tester cannot use subspaces that have non-tuple raw prefixes +def subspace_to_tuple(subspace): + try: + return fdb.tuple.unpack(subspace.key()) + except Exception as e: + get_logger().debug(e) + raise Exception('The binding tester does not support subspaces with non-tuple raw prefixes') + diff --git a/bindings/c/ThreadCleanup.cpp b/bindings/c/ThreadCleanup.cpp new file mode 100644 index 0000000000..1bee0e6b4f --- /dev/null +++ b/bindings/c/ThreadCleanup.cpp @@ -0,0 +1,60 @@ +/* + * ThreadCleanup.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/Platform.h" +#include "flow/FastAlloc.h" + +#if defined(WIN32) + +#include + +BOOL WINAPI DllMain( HINSTANCE dll, DWORD reason, LPVOID reserved ) { + + if (reason == DLL_THREAD_DETACH) + releaseAllThreadMagazines(); + return TRUE; +} + +#elif defined( __unixish__ ) + +static pthread_key_t threadDestructorKey; + +static void threadDestructor(void*) { + releaseAllThreadMagazines(); +} + +void registerThread() { + pthread_setspecific( threadDestructorKey, (const void*)1 ); +} + +static int initThreadDestructorKey() { + if (!pthread_key_create(&threadDestructorKey, &threadDestructor)) { + registerThread(); + setFastAllocatorThreadInitFunction( ®isterThread ); + } + + return 0; +} + +static int threadDestructorKeyInit = initThreadDestructorKey(); + +#else +#error Port me! +#endif \ No newline at end of file diff --git a/bindings/c/fdb_c.cpp b/bindings/c/fdb_c.cpp new file mode 100644 index 0000000000..a9981003e8 --- /dev/null +++ b/bindings/c/fdb_c.cpp @@ -0,0 +1,681 @@ +/* + * fdb_c.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define FDB_API_VERSION 500 + +#include "fdbclient/MultiVersionTransaction.h" +#include "foundationdb/fdb_c.h" + +int g_api_version = 0; + +/* + * Our clients might share these ThreadSafe types between threads. It is therefore + * unsafe to call addRef on them. + * + * type mapping: + * FDBFuture -> ThreadSingleAssignmentVarBase + * FDBCluster -> ICluster + * FDBDatabase -> IDatabase + * FDBTransaction -> ITransaction + */ +#define TSAVB(f) ((ThreadSingleAssignmentVarBase*)(f)) +#define TSAV(T, f) ((ThreadSingleAssignmentVar*)(f)) + +#define DB(d) ((IDatabase*)d) +#define CLUSTER(c) ((ICluster*)c) +#define TXN(t) ((ITransaction*)t) + +#define API (MultiVersionApi::api) + +/* This must be true so that we can return the data pointer of a + Standalone as an array of FDBKeyValue. */ +static_assert( sizeof(FDBKeyValue) == sizeof(KeyValueRef), + "FDBKeyValue / KeyValueRef size mismatch" ); + + +#define TSAV_ERROR(type, error) ((FDBFuture*)(ThreadFuture(error())).extractPtr()) + + +extern "C" DLLEXPORT +const char *fdb_get_error( fdb_error_t code ) { + return Error::fromUnvalidatedCode( code ).what(); +} + +extern "C" DLLEXPORT +fdb_bool_t fdb_error_predicate( int predicate_test, fdb_error_t code ) { + if(predicate_test == FDBErrorPredicates::RETRYABLE) { + return fdb_error_predicate( FDBErrorPredicates::MAYBE_COMMITTED, code ) || + fdb_error_predicate( FDBErrorPredicates::RETRYABLE_NOT_COMMITTED, code ); + } + if(predicate_test == FDBErrorPredicates::MAYBE_COMMITTED) { + return code == error_code_commit_unknown_result || + code == error_code_cluster_version_changed; + } + if(predicate_test == FDBErrorPredicates::RETRYABLE_NOT_COMMITTED) { + return code == error_code_not_committed || + code == error_code_past_version || + code == error_code_future_version || + code == error_code_database_locked; + } + return false; +} + +#define RETURN_ON_ERROR(code_to_run) \ + try { code_to_run } \ + catch( Error& e) { if (e.code() <= 0) return internal_error().code(); else return e.code(); } \ + catch( ... ) { return error_code_unknown_error; } + +#define CATCH_AND_RETURN(code_to_run) \ + RETURN_ON_ERROR(code_to_run); \ + return error_code_success; + +#define CATCH_AND_DIE(code_to_run) \ + try { code_to_run } \ + catch ( Error& e ) { fprintf( stderr, "Unexpected FDB error %d\n", e.code() ); abort(); } \ + catch ( ... ) { fprintf( stderr, "Unexpected FDB unknown error\n" ); abort(); } + +extern "C" DLLEXPORT +fdb_error_t fdb_network_set_option( FDBNetworkOption option, + uint8_t const* value, + int value_length ) +{ + CATCH_AND_RETURN( + API->setNetworkOption( (FDBNetworkOptions::Option)option, value ? StringRef( value, value_length ) : Optional() ); ); +} + +extern "C" +fdb_error_t fdb_setup_network_impl() { + CATCH_AND_RETURN( API->setupNetwork(); ); +} + +extern "C" +fdb_error_t fdb_setup_network_v13( const char* localAddress ) { + fdb_error_t errorCode = fdb_network_set_option( FDB_NET_OPTION_LOCAL_ADDRESS, (uint8_t const*)localAddress, strlen(localAddress) ); + if(errorCode != 0) + return errorCode; + + return fdb_setup_network_impl(); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_run_network() { + CATCH_AND_RETURN( API->runNetwork(); ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_stop_network() { + CATCH_AND_RETURN( API->stopNetwork(); ); +} + +extern "C" DLLEXPORT +FDBFuture* fdb_cluster_configure_database( FDBCluster* c, int config_type, + int config_mode, uint8_t const* db_name, + int db_name_length ) +{ + // Obsolete, but needed for linker compatibility with api version 12 and below + return (FDBFuture*)ThreadFuture(client_invalid_operation()).extractPtr(); +} + +extern "C" DLLEXPORT +void fdb_future_cancel( FDBFuture* f ) { + CATCH_AND_DIE( + TSAVB(f)->addref(); + TSAVB(f)->cancel(); + ); +} + +extern "C" DLLEXPORT +void fdb_future_release_memory( FDBFuture* f ) { + CATCH_AND_DIE( TSAVB(f)->releaseMemory(); ); +} + +extern "C" DLLEXPORT +void fdb_future_destroy( FDBFuture* f ) { + CATCH_AND_DIE( TSAVB(f)->cancel(); ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_future_block_until_ready( FDBFuture* f ) { + CATCH_AND_RETURN( TSAVB(f)->blockUntilReady(); ); +} + +extern "C" DLLEXPORT +fdb_bool_t fdb_future_is_error_v22( FDBFuture* f ) { + return TSAVB(f)->isError(); +} + +extern "C" DLLEXPORT +fdb_bool_t fdb_future_is_ready( FDBFuture* f ) { + return TSAVB(f)->isReady(); +} + +class CAPICallback : public ThreadCallback { +public: + CAPICallback(void (*callbackf)(FDBFuture*, void*), FDBFuture* f, + void* userdata) + : callbackf(callbackf), f(f), userdata(userdata) {} + + virtual bool canFire(int notMadeActive) { return true; } + virtual void fire(const Void& unused, int& userParam) { + (*callbackf)(f, userdata); + delete this; + } + virtual void error(const Error&, int& userParam) { + (*callbackf)(f, userdata); + delete this; + } + +private: + void (*callbackf)(FDBFuture*, void*); + FDBFuture* f; + void* userdata; +}; + +extern "C" DLLEXPORT +fdb_error_t fdb_future_set_callback( FDBFuture* f, + void (*callbackf)(FDBFuture*, void*), + void* userdata ) { + CAPICallback* cb = new CAPICallback(callbackf, f, userdata); + int ignore; + CATCH_AND_RETURN( TSAVB(f)->callOrSetAsCallback( cb, ignore, 0 ); ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_future_get_error_impl( FDBFuture* f ) { + return TSAVB(f)->getErrorCode(); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_future_get_error_v22( FDBFuture* f, const char** description ) { + if ( !( TSAVB(f)->isError() ) ) + return error_code_future_not_error; + if (description) + *description = TSAVB(f)->error.what(); + return TSAVB(f)->error.code(); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_future_get_version( FDBFuture* f, int64_t* out_version ) { + CATCH_AND_RETURN( *out_version = TSAV(Version, f)->get(); ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_future_get_key( FDBFuture* f, uint8_t const** out_key, + int* out_key_length ) { + CATCH_AND_RETURN( + KeyRef key = TSAV(Key, f)->get(); + *out_key = key.begin(); + *out_key_length = key.size(); ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_future_get_cluster( FDBFuture* f, FDBCluster** out_cluster ) { + CATCH_AND_RETURN( + *out_cluster = (FDBCluster*) + ( (TSAV( Reference, f )->get() ).extractPtr() ); ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_future_get_database( FDBFuture* f, FDBDatabase** out_database ) { + CATCH_AND_RETURN( + *out_database = (FDBDatabase*) + ( (TSAV( Reference, f )->get() ).extractPtr() ); ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_future_get_value( FDBFuture* f, fdb_bool_t* out_present, + uint8_t const** out_value, int* out_value_length ) { + CATCH_AND_RETURN( + Optional v = TSAV(Optional, f)->get(); + *out_present = v.present(); + if (*out_present) { + *out_value = v.get().begin(); + *out_value_length = v.get().size(); + } ); +} + +extern "C" +fdb_error_t fdb_future_get_keyvalue_array_impl( + FDBFuture* f, FDBKeyValue const** out_kv, + int* out_count, fdb_bool_t* out_more ) +{ + CATCH_AND_RETURN( + Standalone rrr = TSAV(Standalone, f)->get(); + *out_kv = (FDBKeyValue*)rrr.begin(); + *out_count = rrr.size(); + *out_more = rrr.more; ); +} + +extern "C" +fdb_error_t fdb_future_get_keyvalue_array_v13( + FDBFuture* f, FDBKeyValue const** out_kv, int* out_count) +{ + CATCH_AND_RETURN( + Standalone rrr = TSAV(Standalone, f)->get(); + *out_kv = (FDBKeyValue*)rrr.begin(); + *out_count = rrr.size(); ); +} + +extern "C" +fdb_error_t fdb_future_get_string_array( + FDBFuture* f, const char*** out_strings, int* out_count) +{ + CATCH_AND_RETURN( + Standalone> na = TSAV(Standalone>, f)->get(); + *out_strings = (const char **) na.begin(); + *out_count = na.size(); + ); +} + +extern "C" DLLEXPORT +FDBFuture* fdb_create_cluster( const char* cluster_file_path ) { + return (FDBFuture*) API->createCluster( cluster_file_path ? cluster_file_path : ""/*, g_api_version*/ ).extractPtr(); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_cluster_set_option( FDBCluster* c, + FDBClusterOption option, + uint8_t const* value, + int value_length ) +{ + CATCH_AND_RETURN( + CLUSTER(c)->setOption( (FDBClusterOptions::Option)option, value ? StringRef( value, value_length ) : Optional() ); ); +} + +extern "C" DLLEXPORT +void fdb_cluster_destroy( FDBCluster* c ) { + CATCH_AND_DIE( CLUSTER(c)->delref(); ); +} + +extern "C" DLLEXPORT +FDBFuture* fdb_cluster_create_database( FDBCluster* c, uint8_t const* db_name, + int db_name_length ) { + return (FDBFuture*) + ( CLUSTER(c)->createDatabase( StringRef( db_name, + db_name_length ) ).extractPtr() ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_database_set_option( FDBDatabase* d, + FDBDatabaseOption option, + uint8_t const* value, + int value_length ) +{ + CATCH_AND_RETURN( + DB(d)->setOption( (FDBDatabaseOptions::Option)option, value ? StringRef( value, value_length ) : Optional() ); ); +} + +extern "C" DLLEXPORT +void fdb_database_destroy( FDBDatabase* d ) { + CATCH_AND_DIE( DB(d)->delref(); ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_database_create_transaction( FDBDatabase* d, + FDBTransaction** out_transaction ) +{ + CATCH_AND_RETURN( + Reference tr = DB(d)->createTransaction(); + if(g_api_version <= 15) + tr->setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS); + *out_transaction = (FDBTransaction*)tr.extractPtr(); ); +} + + +extern "C" DLLEXPORT +void fdb_transaction_destroy( FDBTransaction* tr ) { + try { + TXN(tr)->delref(); + } catch ( ... ) { } +} + +extern "C" DLLEXPORT +void fdb_transaction_cancel( FDBTransaction* tr ) { + CATCH_AND_DIE( TXN(tr)->cancel(); ); +} + +extern "C" DLLEXPORT +void fdb_transaction_set_read_version( FDBTransaction* tr, int64_t version ) { + CATCH_AND_DIE( TXN(tr)->setVersion( version ); ); +} + +extern "C" DLLEXPORT +FDBFuture* fdb_transaction_get_read_version( FDBTransaction* tr ) { + return (FDBFuture*)( TXN(tr)->getReadVersion().extractPtr() ); +} + +extern "C" +FDBFuture* fdb_transaction_get_impl( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length, fdb_bool_t snapshot ) { + return (FDBFuture*) + ( TXN(tr)->get( KeyRef( key_name, key_name_length ), snapshot ).extractPtr() ); +} + +extern "C" +FDBFuture* fdb_transaction_get_v13( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length ) +{ + return fdb_transaction_get_impl( tr, key_name, key_name_length, 0 ); +} + +extern "C" +FDBFuture* fdb_transaction_get_key_impl( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length, fdb_bool_t or_equal, + int offset, fdb_bool_t snapshot ) { + return (FDBFuture*)( TXN(tr)->getKey( KeySelectorRef( + KeyRef( key_name, + key_name_length ), + or_equal, offset ), + snapshot ).extractPtr() ); +} + +extern "C" +FDBFuture* fdb_transaction_get_key_v13( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length, fdb_bool_t or_equal, + int offset ) { + return fdb_transaction_get_key_impl( tr, key_name, key_name_length, + or_equal, offset, false ); +} + +extern "C" +FDBFuture* fdb_transaction_get_addresses_for_key( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length ){ + return (FDBFuture*)( TXN(tr)->getAddressesForKey( KeyRef(key_name, key_name_length) ).extractPtr() ); + +} + +extern "C" +FDBFuture* fdb_transaction_get_range_impl( + FDBTransaction* tr, uint8_t const* begin_key_name, + int begin_key_name_length, fdb_bool_t begin_or_equal, int begin_offset, + uint8_t const* end_key_name, int end_key_name_length, + fdb_bool_t end_or_equal, int end_offset, int limit, int target_bytes, + FDBStreamingMode mode, int iteration, fdb_bool_t snapshot, + fdb_bool_t reverse ) +{ + /* This method may be called with a runtime API version of 13, in + which negative row limits are a reverse range read */ + if (g_api_version <= 13 && limit < 0) { + limit = -limit; + reverse = true; + } + + /* Zero at the C API maps to "infinity" at lower levels */ + if (!limit) + limit = CLIENT_KNOBS->ROW_LIMIT_UNLIMITED; + if (!target_bytes) + target_bytes = CLIENT_KNOBS->BYTE_LIMIT_UNLIMITED; + + /* Unlimited/unlimited with mode _EXACT isn't permitted */ + if (limit == CLIENT_KNOBS->ROW_LIMIT_UNLIMITED && target_bytes == CLIENT_KNOBS->BYTE_LIMIT_UNLIMITED && mode == FDB_STREAMING_MODE_EXACT) + return TSAV_ERROR(Standalone, exact_mode_without_limits); + + /* _ITERATOR mode maps to one of the known streaming modes + depending on iteration */ + static const int mode_bytes_array[] = {CLIENT_KNOBS->BYTE_LIMIT_UNLIMITED, 256, 1000, 4096, 80000}; + + /* The progression used for FDB_STREAMING_MODE_ITERATOR. + Goes from small -> medium -> large. Then 1.5 * previous until serial. */ + static const int iteration_progression[] = { 256, 1000, 4096, 6144, 9216, 13824, 20736, 31104, 46656, 69984, 80000 }; + + /* length(iteration_progression) */ + static const int max_iteration = sizeof(iteration_progression) / sizeof(int); + + if(mode == FDB_STREAMING_MODE_WANT_ALL) + mode = FDB_STREAMING_MODE_SERIAL; + + int mode_bytes; + if (mode == FDB_STREAMING_MODE_ITERATOR) { + if (iteration <= 0) + return TSAV_ERROR(Standalone, client_invalid_operation); + + iteration = std::min(iteration, max_iteration); + mode_bytes = iteration_progression[iteration - 1]; + } + else if(mode >= 0 && mode <= FDB_STREAMING_MODE_SERIAL) + mode_bytes = mode_bytes_array[mode]; + else + return TSAV_ERROR(Standalone, client_invalid_operation); + + if(target_bytes == CLIENT_KNOBS->BYTE_LIMIT_UNLIMITED) + target_bytes = mode_bytes; + else if(mode_bytes != CLIENT_KNOBS->BYTE_LIMIT_UNLIMITED) + target_bytes = std::min(target_bytes, mode_bytes); + + return (FDBFuture*)( TXN(tr)->getRange( + KeySelectorRef( + KeyRef( begin_key_name, + begin_key_name_length ), + begin_or_equal, begin_offset ), + KeySelectorRef( + KeyRef( end_key_name, + end_key_name_length ), + end_or_equal, end_offset ), + GetRangeLimits(limit, target_bytes), + snapshot, reverse ).extractPtr() ); +} + +extern "C" +FDBFuture* fdb_transaction_get_range_selector_v13( + FDBTransaction* tr, uint8_t const* begin_key_name, int begin_key_name_length, + fdb_bool_t begin_or_equal, int begin_offset, uint8_t const* end_key_name, + int end_key_name_length, fdb_bool_t end_or_equal, int end_offset, int limit ) +{ + return fdb_transaction_get_range_impl( + tr, begin_key_name, begin_key_name_length, begin_or_equal, begin_offset, + end_key_name, end_key_name_length, end_or_equal, end_offset, + limit, 0, FDB_STREAMING_MODE_EXACT, 0, false, false); +} + +extern "C" +FDBFuture* fdb_transaction_get_range_v13( + FDBTransaction* tr, uint8_t const* begin_key_name, int begin_key_name_length, + uint8_t const* end_key_name, int end_key_name_length, int limit ) +{ + return fdb_transaction_get_range_selector_v13( + tr, + FDB_KEYSEL_FIRST_GREATER_OR_EQUAL(begin_key_name, + begin_key_name_length), + FDB_KEYSEL_FIRST_GREATER_OR_EQUAL(end_key_name, + end_key_name_length), + limit ); +} + +extern "C" DLLEXPORT +void fdb_transaction_set( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length, uint8_t const* value, + int value_length ) { + CATCH_AND_DIE( + TXN(tr)->set( KeyRef( key_name, key_name_length ), + ValueRef( value, value_length ) ); ); +} + +extern "C" DLLEXPORT +void fdb_transaction_atomic_op( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length, uint8_t const* param, + int param_length, FDBMutationType operation_type ) { + CATCH_AND_DIE( + TXN(tr)->atomicOp( KeyRef( key_name, key_name_length ), + ValueRef( param, param_length ), + (FDBMutationTypes::Option) operation_type ); ); +} + +extern "C" DLLEXPORT +void fdb_transaction_clear( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length ) { + CATCH_AND_DIE( + TXN(tr)->clear( KeyRef( key_name, key_name_length ) ); ); +} + +extern "C" DLLEXPORT +void fdb_transaction_clear_range( + FDBTransaction* tr, uint8_t const* begin_key_name, int begin_key_name_length, + uint8_t const* end_key_name, int end_key_name_length ) +{ + CATCH_AND_DIE( + TXN(tr)->clear( KeyRef( begin_key_name, + begin_key_name_length ), + KeyRef( end_key_name, + end_key_name_length ) ); ); +} + +extern "C" DLLEXPORT +FDBFuture* fdb_transaction_watch( FDBTransaction *tr, uint8_t const* key_name, + int key_name_length) +{ + return (FDBFuture*)( TXN(tr)->watch(KeyRef(key_name, key_name_length)).extractPtr() ); +} + +extern "C" DLLEXPORT +FDBFuture* fdb_transaction_commit( FDBTransaction* tr ) { + return (FDBFuture*)( TXN(tr)->commit().extractPtr() ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_transaction_get_committed_version( FDBTransaction* tr, + int64_t* out_version ) +{ + CATCH_AND_RETURN( + *out_version = TXN(tr)->getCommittedVersion(); ); +} + +extern "C" DLLEXPORT +FDBFuture* fdb_transaction_get_versionstamp( FDBTransaction* tr ) +{ + return (FDBFuture*)(TXN(tr)->getVersionstamp().extractPtr()); +} + +extern "C" +fdb_error_t fdb_transaction_set_option_impl( FDBTransaction* tr, + FDBTransactionOption option, + uint8_t const* value, + int value_length ) +{ + CATCH_AND_RETURN( + TXN(tr)->setOption( (FDBTransactionOptions::Option)option, value ? StringRef( value, value_length ) : Optional() ); ); +} + +extern "C" +void fdb_transaction_set_option_v13( FDBTransaction* tr, + FDBTransactionOption option ) +{ + fdb_transaction_set_option_impl( tr, option, NULL, 0 ); +} + +extern "C" DLLEXPORT +FDBFuture* fdb_transaction_on_error( FDBTransaction* tr, fdb_error_t error ) { + return (FDBFuture*)( TXN(tr)->onError( + Error::fromUnvalidatedCode( error ) ).extractPtr() ); +} + +extern "C" DLLEXPORT +void fdb_transaction_reset( FDBTransaction* tr ) { + CATCH_AND_DIE( TXN(tr)->reset(); ); +} + +extern "C" DLLEXPORT +fdb_error_t fdb_transaction_add_conflict_range( FDBTransaction*tr, uint8_t const* begin_key_name, + int begin_key_name_length, uint8_t const* end_key_name, + int end_key_name_length, FDBConflictRangeType type) { + CATCH_AND_RETURN( + KeyRangeRef range(KeyRef(begin_key_name, begin_key_name_length), KeyRef(end_key_name, end_key_name_length)); + if(type == FDBConflictRangeType::FDB_CONFLICT_RANGE_TYPE_READ) + TXN(tr)->addReadConflictRange(range); + else if(type == FDBConflictRangeType::FDB_CONFLICT_RANGE_TYPE_WRITE) + TXN(tr)->addWriteConflictRange(range); + else + return error_code_client_invalid_operation; + ); + +} + +#include "fdb_c_function_pointers.g.h" + +#define FDB_API_CHANGED(func, ver) if (header_version < ver) fdb_api_ptr_##func = (void*)&(func##_v##ver##_PREV); else if (fdb_api_ptr_##func == (void*)&fdb_api_ptr_unimpl) fdb_api_ptr_##func = (void*)&(func##_impl); + +#define FDB_API_REMOVED(func, ver) if (header_version < ver) fdb_api_ptr_##func = (void*)&(func##_v##ver##_PREV); else fdb_api_ptr_##func = (void*)&fdb_api_ptr_removed; + +extern "C" DLLEXPORT +fdb_error_t fdb_select_api_version_impl( int runtime_version, int header_version ) { + /* Can only call this once */ + if (g_api_version != 0) + return error_code_api_version_already_set; + + /* Caller screwed up, this makes no sense */ + if (runtime_version > header_version) + return error_code_api_version_invalid; + + /* Caller requested a version we don't speak */ + if (header_version > FDB_API_VERSION) + return error_code_api_version_not_supported; + + /* No backwards compatibility for earlier versions */ + if (runtime_version < 13) + return error_code_api_version_not_supported; + + RETURN_ON_ERROR( + API->selectApiVersion(runtime_version); + ); + + g_api_version = runtime_version; + + platformInit(); + Error::init(); + + // Versioned API changes -- descending order by version (new changes at top) + // FDB_API_CHANGED( function, ver ) means there is a new implementation as of ver, and a function function_(ver-1) is the old implementation + // FDB_API_REMOVED( function, ver ) means the function was removed as of ver, and function_(ver-1) is the old implementation + FDB_API_CHANGED( fdb_future_get_error, 23 ); + FDB_API_REMOVED( fdb_future_is_error, 23 ); + FDB_API_CHANGED( fdb_future_get_keyvalue_array, 14 ); + FDB_API_CHANGED( fdb_transaction_get_key, 14 ); + FDB_API_CHANGED( fdb_transaction_get_range, 14 ); + FDB_API_REMOVED( fdb_transaction_get_range_selector, 14 ); + FDB_API_CHANGED( fdb_transaction_get, 14 ); + FDB_API_CHANGED( fdb_setup_network, 14 ); + FDB_API_CHANGED( fdb_transaction_set_option, 14 ); + /* End versioned API changes */ + + return error_code_success; +} + +extern "C" DLLEXPORT +int fdb_get_max_api_version() { + return FDB_API_VERSION; +} + +extern "C" DLLEXPORT +const char* fdb_get_client_version() { + return API->getClientVersion(); +} + +#if defined(__APPLE__) +#include +__attribute__((constructor)) +static void initialize() { + //OS X ld doesn't support -z nodelete, so we dlopen to increment the reference count of this module + Dl_info info; + int ret = dladdr((void*)&fdb_select_api_version_impl, &info); + if(!ret || !info.dli_fname) + return; //If we get here somehow, we face the risk of seg faults if somebody unloads our library + + dlopen(info.dli_fname, RTLD_NOLOAD | RTLD_NODELETE); +} +#endif diff --git a/bindings/c/fdb_c.map b/bindings/c/fdb_c.map new file mode 100644 index 0000000000..d9ba8ae656 --- /dev/null +++ b/bindings/c/fdb_c.map @@ -0,0 +1,6 @@ +{ + global: + fdb_*; + local: + *; +}; diff --git a/bindings/c/fdb_c.vcxproj b/bindings/c/fdb_c.vcxproj new file mode 100644 index 0000000000..14af02f63d --- /dev/null +++ b/bindings/c/fdb_c.vcxproj @@ -0,0 +1,124 @@ + + + + + + FDB_CLEAN_BUILD;%(PreprocessorDefinitions) + + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + + + + + + + + {CACB2C8E-3E55-4309-A411-2A9C56C6C1CB} + c + + + + DynamicLibrary + true + MultiByte + v140_xp + + + DynamicLibrary + false + true + MultiByte + v140_xp + + + + + + + + + + + + + + + + + +FOR /F "tokens=1" %%i in ('hg.exe id') do copy /Y "$(TargetPath)" "$(TargetPath)-%%i" + + + + ..\..\;C:\Program Files\boost_1_52_0;$(IncludePath) + $(SolutionDir)bin\$(Configuration)\ + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + + + $(SolutionDir)bin\$(Configuration)\ + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + ..\..\;C:\Program Files\boost_1_52_0;$(IncludePath) + + + + Level3 + Disabled + WIN32;_WIN32_WINNT=_WIN32_WINNT_WS03;WINVER=_WIN32_WINNT_WS03;NTDDI_VERSION=NTDDI_WS03;_DEBUG;_HAS_ITERATOR_DEBUGGING=0;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebug + /bigobj @..\..\flow/no_intellisense.opt %(AdditionalOptions) + + + true + $(SolutionDir)bin\$(Configuration)\fdbclient.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + WIN32;_WIN32_WINNT=_WIN32_WINNT_WS03;WINVER=_WIN32_WINNT_WS03;NTDDI_VERSION=NTDDI_WS03;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreaded + /bigobj @..\..\flow/no_intellisense.opt %(AdditionalOptions) + + + true + true + true + $(SolutionDir)bin\$(Configuration)\fdbclient.lib;%(AdditionalDependencies) + + + + _MASM;ClCompile + + + + c:\Python27\python.exe "$(ProjectDir)/generate_asm.py" windows "$(ProjectDir)/fdb_c.cpp" "$(ProjectDir)/fdb_c.g.asm" "$(ProjectDir)/fdb_c_function_pointers.g.h" + Generating API trampolines + $(ProjectDir)/fdb_c_function_pointers.g.h;$(ProjectDir)/fdb_c.g.asm + $(ProjectDir)/fdb_c.cpp;$(ProjectDir)/generate_asm.py + + + + + + + \ No newline at end of file diff --git a/bindings/c/foundationdb/fdb_c.h b/bindings/c/foundationdb/fdb_c.h new file mode 100644 index 0000000000..9a9a60578b --- /dev/null +++ b/bindings/c/foundationdb/fdb_c.h @@ -0,0 +1,314 @@ +/* + * fdb_c.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_C_H +#define FDB_C_H +#pragma once + + +#ifndef DLLEXPORT +#define DLLEXPORT +#endif + +#if !defined(FDB_API_VERSION) +#error You must #define FDB_API_VERSION prior to including fdb_c.h (current version is 500) +#elif FDB_API_VERSION < 13 +#error API version no longer supported (upgrade to 13) +#elif FDB_API_VERSION > 500 +#error Requested API version requires a newer version of this header +#endif + +#if FDB_API_VERSION >= 23 && !defined(WARN_UNUSED_RESULT) +#ifdef __GNUG__ +#define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) +#else +#define WARN_UNUSED_RESULT +#endif +#else +#define WARN_UNUSED_RESULT +#endif + +// With default settings, gcc will not warn about unprototyped functions being called, so it +// is easy to erroneously call a function which is not available at FDB_API_VERSION and then +// get an error only at runtime. These macros ensure a compile error in such cases, and +// attempt to make the compile error slightly informative. +#define This_FoundationDB_API_function_is_removed_at_this_FDB_API_VERSION() [=====] +#define FDB_REMOVED_FUNCTION This_FoundationDB_API_function_is_removed_at_this_FDB_API_VERSION(0) + +#include + +#include "fdb_c_options.g.h" + +#ifdef __cplusplus +extern "C" { +#endif + + /* Pointers to these opaque types represent objects in the FDB API */ + typedef struct future FDBFuture; + typedef struct cluster FDBCluster; + typedef struct database FDBDatabase; + typedef struct transaction FDBTransaction; + + typedef int fdb_error_t; + typedef int fdb_bool_t; + + DLLEXPORT const char* + fdb_get_error( fdb_error_t code ); + + DLLEXPORT fdb_bool_t + fdb_error_predicate( int predicate_test, fdb_error_t code ); + + #define /* fdb_error_t */ fdb_select_api_version(v) fdb_select_api_version_impl(v, FDB_API_VERSION) + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_network_set_option( FDBNetworkOption option, uint8_t const* value, + int value_length ); + +#if FDB_API_VERSION >= 14 + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t fdb_setup_network(); +#endif + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t fdb_run_network(); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t fdb_stop_network(); + +#pragma pack(push, 4) + typedef struct keyvalue { + const void* key; + int key_length; + const void* value; + int value_length; + } FDBKeyValue; +#pragma pack(pop) + + DLLEXPORT void fdb_future_cancel( FDBFuture *f ); + + DLLEXPORT void fdb_future_release_memory( FDBFuture* f ); + + DLLEXPORT void fdb_future_destroy( FDBFuture* f ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t fdb_future_block_until_ready( FDBFuture* f ); + + DLLEXPORT fdb_bool_t fdb_future_is_ready( FDBFuture* f ); + + typedef void (*FDBCallback)(FDBFuture* future, void* callback_parameter); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_future_set_callback( FDBFuture* f, FDBCallback callback, + void* callback_parameter ); + +#if FDB_API_VERSION >= 23 + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_future_get_error( FDBFuture* f ); +#endif + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_future_get_version( FDBFuture* f, int64_t* out_version ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_future_get_key( FDBFuture* f, uint8_t const** out_key, + int* out_key_length ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_future_get_cluster( FDBFuture* f, FDBCluster** out_cluster ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_future_get_database( FDBFuture* f, FDBDatabase** out_database ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_future_get_value( FDBFuture* f, fdb_bool_t *out_present, + uint8_t const** out_value, + int* out_value_length ); + +#if FDB_API_VERSION >= 14 + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_future_get_keyvalue_array( FDBFuture* f, FDBKeyValue const** out_kv, + int* out_count, fdb_bool_t* out_more ); +#endif + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t fdb_future_get_string_array(FDBFuture* f, + const char*** out_strings, int* out_count); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* fdb_create_cluster( const char* cluster_file_path ); + + DLLEXPORT void fdb_cluster_destroy( FDBCluster* c ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_cluster_set_option( FDBCluster* c, FDBClusterOption option, + uint8_t const* value, int value_length ); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* + fdb_cluster_create_database( FDBCluster* c, uint8_t const* db_name, + int db_name_length ); + + DLLEXPORT void fdb_database_destroy( FDBDatabase* d ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_database_set_option( FDBDatabase* d, FDBDatabaseOption option, + uint8_t const* value, int value_length ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_database_create_transaction( FDBDatabase* d, + FDBTransaction** out_transaction ); + + DLLEXPORT void fdb_transaction_destroy( FDBTransaction* tr); + + DLLEXPORT void fdb_transaction_cancel( FDBTransaction* tr); + +#if FDB_API_VERSION >= 14 + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_transaction_set_option( FDBTransaction* tr, FDBTransactionOption option, + uint8_t const* value, int value_length ); +#endif + + DLLEXPORT void + fdb_transaction_set_read_version( FDBTransaction* tr, int64_t version ); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* fdb_transaction_get_read_version( FDBTransaction* tr ); + +#if FDB_API_VERSION >= 14 + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* + fdb_transaction_get( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length, fdb_bool_t snapshot ); +#endif + +#if FDB_API_VERSION >= 14 + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* + fdb_transaction_get_key( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length, fdb_bool_t or_equal, + int offset, fdb_bool_t snapshot ); +#endif + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* + fdb_transaction_get_addresses_for_key(FDBTransaction* tr, uint8_t const* key_name, + int key_name_length); + +#if FDB_API_VERSION >= 14 + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* fdb_transaction_get_range( + FDBTransaction* tr, uint8_t const* begin_key_name, + int begin_key_name_length, fdb_bool_t begin_or_equal, int begin_offset, + uint8_t const* end_key_name, int end_key_name_length, + fdb_bool_t end_or_equal, int end_offset, int limit, int target_bytes, + FDBStreamingMode mode, int iteration, fdb_bool_t snapshot, + fdb_bool_t reverse ); +#endif + + DLLEXPORT void + fdb_transaction_set( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length, uint8_t const* value, + int value_length ); + + DLLEXPORT void + fdb_transaction_atomic_op( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length, uint8_t const* param, + int param_length, FDBMutationType operation_type ); + + DLLEXPORT void + fdb_transaction_clear( FDBTransaction* tr, uint8_t const* key_name, + int key_name_length ); + + DLLEXPORT void fdb_transaction_clear_range( + FDBTransaction* tr, uint8_t const* begin_key_name, + int begin_key_name_length, uint8_t const* end_key_name, + int end_key_name_length ); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* fdb_transaction_watch( FDBTransaction *tr, + uint8_t const* key_name, + int key_name_length); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* fdb_transaction_commit( FDBTransaction* tr ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_transaction_get_committed_version( FDBTransaction* tr, + int64_t* out_version ); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* fdb_transaction_get_versionstamp( FDBTransaction* tr ); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* + fdb_transaction_on_error( FDBTransaction* tr, fdb_error_t error ); + + DLLEXPORT void fdb_transaction_reset( FDBTransaction* tr ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_transaction_add_conflict_range(FDBTransaction *tr, + uint8_t const* begin_key_name, + int begin_key_name_length, + uint8_t const* end_key_name, + int end_key_name_length, + FDBConflictRangeType type); + + #define FDB_KEYSEL_LAST_LESS_THAN(k, l) k, l, 0, 0 + #define FDB_KEYSEL_LAST_LESS_OR_EQUAL(k, l) k, l, 1, 0 + #define FDB_KEYSEL_FIRST_GREATER_THAN(k, l) k, l, 1, 1 + #define FDB_KEYSEL_FIRST_GREATER_OR_EQUAL(k, l) k, l, 0, 1 + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_select_api_version_impl( int runtime_version, int header_version ); + + DLLEXPORT int fdb_get_max_api_version(); + DLLEXPORT const char* fdb_get_client_version(); + + /* LEGACY API VERSIONS */ + +#if FDB_API_VERSION < 23 + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t + fdb_future_get_error( FDBFuture* f, + const char** out_description /* = NULL */ ); + + DLLEXPORT fdb_bool_t fdb_future_is_error( FDBFuture* f ); +#else + #define fdb_future_is_error(x) FDB_REMOVED_FUNCTION +#endif + +#if FDB_API_VERSION < 14 + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t fdb_future_get_keyvalue_array( + FDBFuture* f, FDBKeyValue const** out_kv, int* out_count ); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* fdb_transaction_get( + FDBTransaction* tr, uint8_t const* key_name, int key_name_length ); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* fdb_transaction_get_key( + FDBTransaction* tr, uint8_t const* key_name, int key_name_length, + fdb_bool_t or_equal, int offset ); + + DLLEXPORT WARN_UNUSED_RESULT fdb_error_t fdb_setup_network( const char* local_address ); + + DLLEXPORT void fdb_transaction_set_option( + FDBTransaction* tr, FDBTransactionOption option ); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* fdb_transaction_get_range( + FDBTransaction* tr, uint8_t const* begin_key_name, + int begin_key_name_length, uint8_t const* end_key_name, + int end_key_name_length, int limit ); + + DLLEXPORT WARN_UNUSED_RESULT FDBFuture* fdb_transaction_get_range_selector( + FDBTransaction* tr, uint8_t const* begin_key_name, + int begin_key_name_length, fdb_bool_t begin_or_equal, + int begin_offset, uint8_t const* end_key_name, + int end_key_name_length, fdb_bool_t end_or_equal, int end_offset, + int limit ); +#else + #define fdb_transaction_get_range_selector(tr,bkn,bknl,boe,bo,ekn,eknl,eoe,eo,lim) FDB_REMOVED_FUNCTION +#endif + +#ifdef __cplusplus +} +#endif +#endif diff --git a/bindings/c/generate_asm.py b/bindings/c/generate_asm.py new file mode 100755 index 0000000000..1a2349b598 --- /dev/null +++ b/bindings/c/generate_asm.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python +# +# generate_asm.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +import re +import sys + +(platform, source, asm, h) = sys.argv[1:] + +functions = {} + +func_re = re.compile("^\s*FDB_API_(?:CHANGED|REMOVED)\s*\(\s*([^,]*),\s*([^)]*)\).*") + +with open(source, 'r') as srcfile: + for l in srcfile: + m = func_re.match(l) + if m: + func, ver = m.groups() + if not func in functions: + functions[func] = [] + ver = int(ver) + if not ver in functions[func]: + functions[func].append(ver) + +def write_windows_asm(asmfile, functions): + asmfile.write(".data\n") + for f in functions: + asmfile.write("\textern fdb_api_ptr_%s:qword\n" % f) + + asmfile.write("\n.code\n") + + for f in functions: + asmfile.write("\n%s proc EXPORT\n" % f) + asmfile.write("\tmov r11, qword ptr [fdb_api_ptr_%s]\n" % f) + asmfile.write("\tjmp r11\n") + asmfile.write("%s endp\n" % f) + + asmfile.write("\nEND\n") + +def write_unix_asm(asmfile, functions, prefix): + asmfile.write(".intel_syntax noprefix\n") + + if platform == "linux": + asmfile.write("\n.data\n") + for f in functions: + asmfile.write("\t.extern fdb_api_ptr_%s\n" % f) + + asmfile.write("\n.text\n") + for f in functions: + asmfile.write("\t.global %s\n\t.type %s, @function\n" % (f, f)) + + for f in functions: + asmfile.write("\n.globl %s%s\n" % (prefix, f)) + asmfile.write("%s%s:\n" % (prefix, f)) + asmfile.write("\tmov r11, qword ptr [%sfdb_api_ptr_%s@GOTPCREL+rip]\n" % (prefix, f)) + asmfile.write("\tmov r11, qword ptr [r11]\n") + asmfile.write("\tjmp r11\n") + +with open(asm, 'w') as asmfile, open(h, 'w') as hfile: + hfile.write("void fdb_api_ptr_unimpl() { fprintf(stderr, \"UNIMPLEMENTED FDB API FUNCTION\\n\"); abort(); }\n\n") + hfile.write("void fdb_api_ptr_removed() { fprintf(stderr, \"REMOVED FDB API FUNCTION\\n\"); abort(); }\n\n") + + if platform == "linux": + write_unix_asm(asmfile, functions, '') + elif platform == "osx": + write_unix_asm(asmfile, functions, '_') + elif platform == "windows": + write_windows_asm(asmfile, functions) + + for f in functions: + if platform == "windows": + hfile.write("extern \"C\" ") + hfile.write("void* fdb_api_ptr_%s = (void*)&fdb_api_ptr_unimpl;\n" % f) + for v in functions[f]: + hfile.write("#define %s_v%d_PREV %s_v%d\n" % (f, v, f, v-1)) diff --git a/bindings/c/local.mk b/bindings/c/local.mk new file mode 100644 index 0000000000..4df783d5c1 --- /dev/null +++ b/bindings/c/local.mk @@ -0,0 +1,76 @@ +# +# local.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile; -*- + +fdb_c_CFLAGS := $(fdbclient_CFLAGS) +fdb_c_LDFLAGS := $(fdbrpc_LDFLAGS) +fdb_c_LIBS := lib/libfdbclient.a lib/libfdbrpc.a lib/libflow.a + +ifeq ($(PLATFORM),linux) + fdb_c_LIBS += lib/libstdc++.a -lm -lpthread -lrt -ldl + fdb_c_LDFLAGS += -Wl,--version-script=bindings/c/fdb_c.map -static-libgcc -Wl,-z,nodelete +endif + +ifeq ($(PLATFORM),osx) + fdb_c_LDFLAGS += -lc++ -Xlinker -exported_symbols_list -Xlinker bindings/c/fdb_c.symbols + + lib/libfdb_c.dylib: bindings/c/fdb_c.symbols + + bindings/c/fdb_c.symbols: bindings/c/foundationdb/fdb_c.h $(ALL_MAKEFILES) + @awk '{sub(/^[ \t]+/, "");} /^#/ {next;} /DLLEXPORT\ .*[^ ]\(/ {sub(/\(.*/, ""); print "_" $$NF; next;} /DLLEXPORT/ { DLLEXPORT=1; next;} DLLEXPORT==1 {sub(/\(.*/, ""); print "_" $$0; DLLEXPORT=0}' $< | sort | uniq > $@ + + fdb_c_clean: fdb_c_symbols_clean + + fdb_c_symbols_clean: + @rm -f bindings/c/fdb_c.symbols + + fdb_javac_release: lib/libfdb_c.$(DLEXT) + mkdir -p lib + rm -f lib/libfdb_c.$(java_DLEXT)-* + cp lib/libfdb_c.$(DLEXT) lib/libfdb_c.$(DLEXT)-$(VERSION_ID) + cp lib/libfdb_c.$(DLEXT)-debug lib/libfdb_c.$(DLEXT)-debug-$(VERSION_ID) + + fdb_javac_release_clean: + rm -f lib/libfdb_c.$(DLEXT)-* + rm -f lib/libfdb_c.$(javac_DLEXT)-* + + # OS X needs to put its java lib in packages + packages: fdb_javac_lib_package + + fdb_javac_lib_package: lib/libfdb_c.dylib + mkdir -p packages + cp lib/libfdb_c.$(DLEXT) packages/libfdb_c.$(DLEXT)-$(VERSION_ID) + cp lib/libfdb_c.$(DLEXT)-debug packages/libfdb_c.$(DLEXT)-debug-$(VERSION_ID) +endif + +fdb_c_GENERATED_SOURCES += bindings/c/foundationdb/fdb_c_options.g.h bindings/c/fdb_c.g.S bindings/c/fdb_c_function_pointers.g.h + +bindings/c/%.g.S bindings/c/%_function_pointers.g.h: bindings/c/%.cpp bindings/c/generate_asm.py $(ALL_MAKEFILES) + @echo "Scanning $<" + @bindings/c/generate_asm.py $(PLATFORM) bindings/c/fdb_c.cpp bindings/c/fdb_c.g.S bindings/c/fdb_c_function_pointers.g.h + +.PRECIOUS: bindings/c/fdb_c_function_pointers.g.h + +fdb_c_BUILD_SOURCES += bindings/c/fdb_c.g.S + +bindings/c/foundationdb/fdb_c_options.g.h: bin/vexillographer.exe fdbclient/vexillographer/fdb.options $(ALL_MAKEFILES) + @echo "Building $@" + @$(MONO) bin/vexillographer.exe fdbclient/vexillographer/fdb.options c $@ diff --git a/bindings/flow/DirectoryLayer.actor.cpp b/bindings/flow/DirectoryLayer.actor.cpp new file mode 100755 index 0000000000..5f3e0b805b --- /dev/null +++ b/bindings/flow/DirectoryLayer.actor.cpp @@ -0,0 +1,507 @@ +/* + * DirectoryLayer.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "DirectoryLayer.h" +#include "DirectoryPartition.h" + +namespace FDB { + const uint8_t DirectoryLayer::LITTLE_ENDIAN_LONG_ONE[8] = {1,0,0,0,0,0,0,0}; + const StringRef DirectoryLayer::HIGH_CONTENTION_KEY = LiteralStringRef("hca"); + const StringRef DirectoryLayer::LAYER_KEY = LiteralStringRef("layer"); + const StringRef DirectoryLayer::VERSION_KEY = LiteralStringRef("version"); + const uint64_t DirectoryLayer::SUB_DIR_KEY = 0; + + const uint32_t DirectoryLayer::VERSION[3] = {1, 0, 0}; + + const StringRef DirectoryLayer::DEFAULT_NODE_SUBSPACE_PREFIX = LiteralStringRef("\xfe"); + const Subspace DirectoryLayer::DEFAULT_NODE_SUBSPACE = Subspace(DEFAULT_NODE_SUBSPACE_PREFIX); + const Subspace DirectoryLayer::DEFAULT_CONTENT_SUBSPACE = Subspace(); + const StringRef DirectoryLayer::PARTITION_LAYER = LiteralStringRef("partition"); + + DirectoryLayer::DirectoryLayer(Subspace nodeSubspace, Subspace contentSubspace, bool allowManualPrefixes) : + nodeSubspace(nodeSubspace), contentSubspace(contentSubspace), allowManualPrefixes(allowManualPrefixes), + rootNode(nodeSubspace.get(nodeSubspace.key())), allocator(rootNode.get(HIGH_CONTENTION_KEY)) + { } + + Subspace DirectoryLayer::nodeWithPrefix(StringRef const& prefix) const { + return nodeSubspace.get(prefix); + } + + template + Optional DirectoryLayer::nodeWithPrefix(Optional const& prefix) const { + if(!prefix.present()) { + return Optional(); + } + + return nodeWithPrefix(prefix.get()); + } + + ACTOR Future find(Reference dirLayer, Reference tr, IDirectory::Path path) { + state int pathIndex = 0; + state DirectoryLayer::Node node = DirectoryLayer::Node(dirLayer, dirLayer->rootNode, IDirectory::Path(), path); + + for(; pathIndex != path.size(); ++pathIndex) { + ASSERT(node.subspace.present()); + Optional> val = wait(tr->get(node.subspace.get().get(DirectoryLayer::SUB_DIR_KEY).get(path[pathIndex], true).key())); + + node.path.push_back(path[pathIndex]); + node = DirectoryLayer::Node(dirLayer, dirLayer->nodeWithPrefix(val), node.path, path); + + DirectoryLayer::Node _node = wait(node.loadMetadata(tr)); + node = _node; + + if(!node.exists() || node.layer == DirectoryLayer::PARTITION_LAYER) { + return node; + } + } + + if(!node.loadedMetadata) { + DirectoryLayer::Node _node = wait(node.loadMetadata(tr)); + node = _node; + } + + return node; + } + + IDirectory::Path DirectoryLayer::toAbsolutePath(IDirectory::Path const& subpath) const { + Path path; + + path.reserve(this->path.size() + subpath.size()); + path.insert(path.end(), this->path.begin(), this->path.end()); + path.insert(path.end(), subpath.begin(), subpath.end()); + + return path; + } + + Reference DirectoryLayer::contentsOfNode(Subspace const& node, Path const& path, Standalone const& layer) { + Standalone prefix = nodeSubspace.unpack(node.key()).getString(0); + + if(layer == PARTITION_LAYER) { + return Reference(new DirectoryPartition(toAbsolutePath(path), prefix, Reference::addRef(this))); + } + else { + return Reference(new DirectorySubspace(toAbsolutePath(path), prefix, Reference::addRef(this), layer)); + } + } + + Reference DirectoryLayer::openInternal(Standalone const& layer, Node const& existingNode, bool allowOpen) { + if (!allowOpen) { + throw directory_already_exists(); + } + if(layer.size() > 0 && layer != existingNode.layer) { + throw mismatched_layer(); + } + + return existingNode.getContents(); + } + + Future> DirectoryLayer::open(Reference const& tr, Path const& path, Standalone const& layer) { + return createOrOpenInternal(tr, path, layer, Optional>(), false, true); + } + + void DirectoryLayer::initializeDirectory(Reference const& tr) const { + tr->set(rootNode.pack(VERSION_KEY), StringRef((uint8_t*)VERSION, 12)); + } + + ACTOR Future checkVersionInternal(const DirectoryLayer* dirLayer, Reference tr, bool writeAccess) { + Optional> versionBytes = wait(tr->get(dirLayer->rootNode.pack(DirectoryLayer::VERSION_KEY))); + + if(!versionBytes.present()) { + if(writeAccess) { + dirLayer->initializeDirectory(tr); + } + return Void(); + } + else { + if(versionBytes.get().size() != 12) { + throw invalid_database_value(); + } + if(((uint32_t*)versionBytes.get().begin())[0] > DirectoryLayer::VERSION[0]) { + throw incompatible_directory_version(); + } + else if(((uint32_t*)versionBytes.get().begin())[1] > DirectoryLayer::VERSION[1] && writeAccess) { + throw incompatible_directory_version(); + } + } + + return Void(); + } + + Future DirectoryLayer::checkVersion(Reference const& tr, bool writeAccess) const { + return checkVersionInternal(this, tr, writeAccess); + } + + ACTOR Future> getPrefix(Reference dirLayer, Reference tr, Optional> prefix) { + if(!prefix.present()) { + Standalone allocated = wait(dirLayer->allocator.allocate(tr)); + state Standalone finalPrefix = allocated.withPrefix(dirLayer->contentSubspace.key()); + + FDBStandalone result = wait(tr->getRange(KeyRangeRef(finalPrefix, strinc(finalPrefix)), 1)); + + if(result.size() > 0) { + throw directory_prefix_not_empty(); + } + + return finalPrefix; + } + + return prefix.get(); + } + + ACTOR Future> nodeContainingKey(Reference dirLayer, Reference tr, Standalone key, bool snapshot) { + if(key.startsWith(dirLayer->nodeSubspace.key())) { + return dirLayer->rootNode; + } + + KeyRange range = KeyRangeRef(dirLayer->nodeSubspace.range().begin, keyAfter(dirLayer->nodeSubspace.pack(key))); + FDBStandalone result = wait(tr->getRange(range, 1, snapshot, true)); + + if(result.size() > 0) { + Standalone prevPrefix = dirLayer->nodeSubspace.unpack(result[0].key).getString(0); + if(key.startsWith(prevPrefix)) { + return dirLayer->nodeWithPrefix(prevPrefix); + } + } + + return Optional(); + } + + ACTOR Future isPrefixFree(Reference dirLayer, Reference tr, Standalone prefix, bool snapshot){ + if(!prefix.size()) { + return false; + } + + Optional node = wait(nodeContainingKey(dirLayer, tr, prefix, snapshot)); + if(node.present()) { + return false; + } + + FDBStandalone result = wait(tr->getRange(KeyRangeRef(dirLayer->nodeSubspace.pack(prefix), dirLayer->nodeSubspace.pack(strinc(prefix))), 1, snapshot)); + return !result.size(); + + } + + ACTOR Future getParentNode(Reference dirLayer, Reference tr, IDirectory::Path path) { + if(path.size() > 1) { + Reference parent = wait(dirLayer->createOrOpenInternal(tr, IDirectory::Path(path.begin(), path.end() - 1), StringRef(), Optional>(), true, true)); + return dirLayer->nodeWithPrefix(parent->key()); + } + else { + return dirLayer->rootNode; + } + } + + ACTOR Future> createInternal( + Reference dirLayer, Reference tr, IDirectory::Path path, + Standalone layer, Optional> prefix, bool allowCreate) + { + if(!allowCreate) { + throw directory_does_not_exist(); + } + + Void _ = wait(dirLayer->checkVersion(tr, true)); + + state Standalone newPrefix = wait(getPrefix(dirLayer, tr, prefix)); + bool isFree = wait(isPrefixFree(dirLayer, tr, newPrefix, !prefix.present())); + + if(!isFree) { + throw directory_prefix_in_use(); + } + + Subspace parentNode = wait(getParentNode(dirLayer, tr, path)); + Subspace node = dirLayer->nodeWithPrefix(newPrefix); + + tr->set(parentNode.get(DirectoryLayer::SUB_DIR_KEY).get(path.back(), true).key(), newPrefix); + tr->set(node.get(DirectoryLayer::LAYER_KEY).key(), layer); + return dirLayer->contentsOfNode(node, path, layer); + } + + ACTOR Future> _createOrOpenInternal( + Reference dirLayer, Reference tr, IDirectory::Path path, + Standalone layer, Optional> prefix, bool allowCreate, bool allowOpen) + { + ASSERT(!prefix.present() || allowCreate); + Void _ = wait(dirLayer->checkVersion(tr, false)); + + if(prefix.present() && !dirLayer->allowManualPrefixes) { + if(!dirLayer->getPath().size()) { + throw manual_prefixes_not_enabled(); + } + else { + throw prefix_in_partition(); + } + } + + if(!path.size()){ + throw cannot_open_root_directory(); + } + + state DirectoryLayer::Node existingNode = wait(find(dirLayer, tr, path)); + if(existingNode.exists()) { + if(existingNode.isInPartition()) { + IDirectory::Path subpath = existingNode.getPartitionSubpath(); + Reference dirSpace = wait(existingNode.getContents()->getDirectoryLayer()->createOrOpenInternal(tr, subpath, layer, prefix, allowCreate, allowOpen)); + return dirSpace; + } + return dirLayer->openInternal(layer, existingNode, allowOpen); + } + else { + Reference dirSpace = wait(createInternal(dirLayer, tr, path, layer, prefix, allowCreate)); + return dirSpace; + } + } + + Future> DirectoryLayer::createOrOpenInternal( + Reference const& tr, Path const& path, Standalone const& layer, + Optional> const& prefix, bool allowCreate, bool allowOpen) + { + return _createOrOpenInternal(Reference::addRef(this), tr, path, layer, prefix, allowCreate, allowOpen); + } + + Future> DirectoryLayer::create( + Reference const& tr, Path const& path, Standalone const& layer, + Optional> const& prefix) + { + return createOrOpenInternal(tr, path, layer, prefix, true, false); + } + + Future> DirectoryLayer::createOrOpen(Reference const& tr, Path const& path, Standalone const& layer) { + return createOrOpenInternal(tr, path, layer, Optional>(), true, true); + } + + ACTOR Future>> listInternal(Reference dirLayer, Reference tr, IDirectory::Path path) { + Void _ = wait(dirLayer->checkVersion(tr, false)); + + state DirectoryLayer::Node node = wait(find(dirLayer, tr, path)); + + if(!node.exists()) { + throw directory_does_not_exist(); + } + if(node.isInPartition(true)) { + Standalone> partitionList = wait(node.getContents()->getDirectoryLayer()->list(tr, node.getPartitionSubpath())); + return partitionList; + } + + state Subspace subdir = node.subspace.get().get(DirectoryLayer::SUB_DIR_KEY); + state Key begin = subdir.range().begin; + state Standalone> subdirectories; + + loop { + FDBStandalone subdirRange = wait(tr->getRange(KeyRangeRef(begin, subdir.range().end))); + + for(int i = 0; i < subdirRange.size(); ++i) { + subdirectories.push_back_deep(subdirectories.arena(), subdir.unpack(subdirRange[i].key).getString(0)); + } + + if(!subdirRange.more) { + return subdirectories; + } + + begin = keyAfter(subdirRange.back().key); + } + } + + Future>> DirectoryLayer::list(Reference const& tr, Path const& path) { + return listInternal(Reference::addRef(this), tr, path); + } + + bool pathsEqual(IDirectory::Path const& path1, IDirectory::Path const& path2, size_t maxElementsToCheck = std::numeric_limits::max()) { + if(std::min(path1.size(), maxElementsToCheck) != std::min(path2.size(), maxElementsToCheck)) { + return false; + } + for(int i = 0; i < path1.size() && i < maxElementsToCheck; ++i) { + if(path1[i] != path2[i]) { + return false; + } + } + + return true; + } + + ACTOR Future removeFromParent(Reference dirLayer, Reference tr, IDirectory::Path path) { + ASSERT(path.size() >= 1); + DirectoryLayer::Node parentNode = wait(find(dirLayer, tr, IDirectory::Path(path.begin(), path.end() - 1))); + if(parentNode.subspace.present()) { + tr->clear(parentNode.subspace.get().get(DirectoryLayer::SUB_DIR_KEY).get(path.back(), true).key()); + } + + return Void(); + } + + ACTOR Future> moveInternal(Reference dirLayer, Reference tr, IDirectory::Path oldPath, IDirectory::Path newPath) { + Void _ = wait(dirLayer->checkVersion(tr, true)); + + if(oldPath.size() <= newPath.size()) { + if(pathsEqual(oldPath, newPath, oldPath.size())) { + throw invalid_destination_directory(); + } + } + + std::vector> futures; + futures.push_back(find(dirLayer, tr, oldPath)); + futures.push_back(find(dirLayer, tr, newPath)); + + std::vector nodes = wait(getAll(futures)); + + state DirectoryLayer::Node oldNode = nodes[0]; + state DirectoryLayer::Node newNode = nodes[1]; + + if(!oldNode.exists()) { + throw directory_does_not_exist(); + } + + if(oldNode.isInPartition() || newNode.isInPartition()) { + if(!oldNode.isInPartition() || !newNode.isInPartition() || !pathsEqual(oldNode.path, newNode.path)) { + throw cannot_move_directory_between_partitions(); + } + + Reference partitionMove = wait(newNode.getContents()->move(tr, oldNode.getPartitionSubpath(), newNode.getPartitionSubpath())); + return partitionMove; + } + + if(newNode.exists() || newPath.empty()) { + throw directory_already_exists(); + } + + DirectoryLayer::Node parentNode = wait(find(dirLayer, tr, IDirectory::Path(newPath.begin(), newPath.end() - 1))); + if(!parentNode.exists()) { + throw parent_directory_does_not_exist(); + } + + tr->set(parentNode.subspace.get().get(DirectoryLayer::SUB_DIR_KEY).get(newPath.back(), true).key(), dirLayer->nodeSubspace.unpack(oldNode.subspace.get().key()).getString(0)); + Void _ = wait(removeFromParent(dirLayer, tr, oldPath)); + + return dirLayer->contentsOfNode(oldNode.subspace.get(), newPath, oldNode.layer); + } + + Future> DirectoryLayer::move(Reference const& tr, Path const& oldPath, Path const& newPath) { + return moveInternal(Reference::addRef(this), tr, oldPath, newPath); + } + + Future> DirectoryLayer::moveTo(Reference const& tr, Path const& newAbsolutePath) { + throw cannot_modify_root_directory(); + } + + Future removeRecursive(Reference const&, Reference const&, Subspace const&); + ACTOR Future removeRecursive(Reference dirLayer, Reference tr, Subspace nodeSub) { + state Subspace subdir = nodeSub.get(DirectoryLayer::SUB_DIR_KEY); + state Key begin = subdir.range().begin; + state std::vector> futures; + + loop { + FDBStandalone range = wait(tr->getRange(KeyRangeRef(begin, subdir.range().end))); + for (int i = 0; i < range.size(); ++i) { + Subspace subNode = dirLayer->nodeWithPrefix(range[i].value); + futures.push_back(removeRecursive(dirLayer, tr, subNode)); + } + + if(!range.more) { + break; + } + + begin = keyAfter(range.back().key); + } + + // waits are done concurrently + Void _ = wait(waitForAll(futures)); + + Standalone nodePrefix = dirLayer->nodeSubspace.unpack(nodeSub.key()).getString(0); + + tr->clear(KeyRangeRef(nodePrefix, strinc(nodePrefix))); + tr->clear(nodeSub.range()); + + return Void(); + } + + Future removeInternal(Reference const&, Reference const&, IDirectory::Path const&, bool const&); + ACTOR Future removeInternal(Reference dirLayer, Reference tr, IDirectory::Path path, bool failOnNonexistent) { + Void _ = wait(dirLayer->checkVersion(tr, true)); + + if(path.empty()) { + throw cannot_modify_root_directory(); + } + + state DirectoryLayer::Node node = wait(find(dirLayer, tr, path)); + + if(!node.exists()) { + if(failOnNonexistent) { + throw directory_does_not_exist(); + } + else { + return false; + } + } + + if(node.isInPartition()) { + bool recurse = wait(removeInternal(node.getContents()->getDirectoryLayer(), tr, node.getPartitionSubpath(), failOnNonexistent)); + return recurse; + } + + + state std::vector> futures; + futures.push_back(removeRecursive(dirLayer, tr, node.subspace.get())); + futures.push_back(removeFromParent(dirLayer, tr, path)); + + Void _ = wait(waitForAll(futures)); + + return true; + } + + Future DirectoryLayer::remove(Reference const& tr, Path const& path) { + return success(removeInternal(Reference::addRef(this), tr, path, true)); + } + + Future DirectoryLayer::removeIfExists(Reference const& tr, Path const& path) { + return removeInternal(Reference::addRef(this), tr, path, false); + } + + ACTOR Future existsInternal(Reference dirLayer, Reference tr, IDirectory::Path path) { + Void _ = wait(dirLayer->checkVersion(tr, false)); + + DirectoryLayer::Node node = wait(find(dirLayer, tr, path)); + + if(!node.exists()) { + return false; + } + + if(node.isInPartition()) { + bool exists = wait(node.getContents()->getDirectoryLayer()->exists(tr, node.getPartitionSubpath())); + return exists; + } + + return true; + } + + Future DirectoryLayer::exists(Reference const& tr, Path const& path) { + return existsInternal(Reference::addRef(this), tr, path); + } + + Reference DirectoryLayer::getDirectoryLayer() { + return Reference::addRef(this); + } + + const Standalone DirectoryLayer::getLayer() const { + return StringRef(); + } + + const IDirectory::Path DirectoryLayer::getPath() const { + return path; + } +} diff --git a/bindings/flow/DirectoryLayer.h b/bindings/flow/DirectoryLayer.h new file mode 100755 index 0000000000..e2a8458623 --- /dev/null +++ b/bindings/flow/DirectoryLayer.h @@ -0,0 +1,111 @@ +/* + * DirectoryLayer.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_FLOW_DIRECTORY_LAYER_H +#define FDB_FLOW_DIRECTORY_LAYER_H + +#pragma once + +#include "IDirectory.h" +#include "DirectorySubspace.h" +#include "HighContentionAllocator.h" + +namespace FDB { + class DirectoryLayer : public IDirectory { + public: + DirectoryLayer(Subspace nodeSubspace = DEFAULT_NODE_SUBSPACE, Subspace contentSubspace = DEFAULT_CONTENT_SUBSPACE, bool allowManualPrefixes = false); + + Future> create(Reference const& tr, Path const& path, Standalone const& layer = Standalone(), Optional> const& prefix = Optional>()); + Future> open(Reference const& tr, Path const& path, Standalone const& layer = Standalone()); + Future> createOrOpen(Reference const& tr, Path const& path, Standalone const& layer = Standalone()); + + Future exists(Reference const& tr, Path const& path = Path()); + Future>> list(Reference const& tr, Path const& path = Path()); + + Future> move(Reference const& tr, Path const& oldPath, Path const& newPath); + Future> moveTo(Reference const& tr, Path const& newAbsolutePath); + + Future remove(Reference const& tr, Path const& path = Path()); + Future removeIfExists(Reference const& tr, Path const& path = Path()); + + Reference getDirectoryLayer(); + const Standalone getLayer() const; + const Path getPath() const; + + static const Subspace DEFAULT_NODE_SUBSPACE; + static const Subspace DEFAULT_CONTENT_SUBSPACE; + static const StringRef PARTITION_LAYER; + + //private: + static const uint8_t LITTLE_ENDIAN_LONG_ONE[8]; + static const StringRef HIGH_CONTENTION_KEY; + static const StringRef LAYER_KEY; + static const StringRef VERSION_KEY; + static const uint64_t SUB_DIR_KEY; + static const uint32_t VERSION[3]; + static const StringRef DEFAULT_NODE_SUBSPACE_PREFIX; + + struct Node { + Node() {} + Node(Reference const& directoryLayer, Optional const& subspace, Path const& path, Path const& targetPath); + + bool exists() const; + + Future loadMetadata(Reference tr); + void ensureMetadataLoaded() const; + + bool isInPartition(bool includeEmptySubpath = false) const; + Path getPartitionSubpath() const; + Reference getContents() const; + + Reference directoryLayer; + Optional subspace; + Path path; + Path targetPath; + Standalone layer; + + bool loadedMetadata; + }; + + Reference openInternal(Standalone const& layer, Node const& existingNode, bool allowOpen); + Future> createOrOpenInternal(Reference const& tr, Path const& path, Standalone const& layer, Optional> const& prefix, bool allowCreate, bool allowOpen); + + void initializeDirectory(Reference const& tr) const; + Future checkVersion(Reference const& tr, bool writeAccess) const; + + template + Optional nodeWithPrefix(Optional const& prefix) const; + Subspace nodeWithPrefix(StringRef const& prefix) const; + + Reference contentsOfNode(Subspace const& node, Path const& path, Standalone const& layer); + + Path toAbsolutePath(Path const& subpath) const; + + Subspace rootNode; + Subspace nodeSubspace; + Subspace contentSubspace; + HighContentionAllocator allocator; + bool allowManualPrefixes; + + Path path; + }; +} + +#endif \ No newline at end of file diff --git a/bindings/flow/DirectoryPartition.h b/bindings/flow/DirectoryPartition.h new file mode 100644 index 0000000000..1c3f27de9c --- /dev/null +++ b/bindings/flow/DirectoryPartition.h @@ -0,0 +1,61 @@ +/* + * DirectoryPartition.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_FLOW_DIRECTORY_PARTITION_H +#define FDB_FLOW_DIRECTORY_PARTITION_H + +#pragma once + +#include "IDirectory.h" +#include "DirectorySubspace.h" +#include "DirectoryLayer.h" + +namespace FDB { + class DirectoryPartition : public DirectorySubspace { + + public: + DirectoryPartition(Path const& path, StringRef const& prefix, Reference parentDirectoryLayer) + : DirectorySubspace(path, prefix, Reference(new DirectoryLayer(Subspace(DirectoryLayer::DEFAULT_NODE_SUBSPACE_PREFIX.withPrefix(prefix)), Subspace(prefix))), DirectoryLayer::PARTITION_LAYER), + parentDirectoryLayer(parentDirectoryLayer) + { + this->directoryLayer->path = path; + } + virtual ~DirectoryPartition() {} + + virtual Key key() const { throw cannot_use_partition_as_subspace(); } + virtual bool contains(KeyRef const& key) const { throw cannot_use_partition_as_subspace(); } + + virtual Key pack(Tuple const& tuple = Tuple()) const { throw cannot_use_partition_as_subspace(); } + virtual Tuple unpack(KeyRef const& key) const { throw cannot_use_partition_as_subspace(); } + virtual KeyRange range(Tuple const& tuple = Tuple()) const { throw cannot_use_partition_as_subspace(); } + + virtual Subspace subspace(Tuple const& tuple) const { throw cannot_use_partition_as_subspace(); } + virtual Subspace get(Tuple const& tuple) const { throw cannot_use_partition_as_subspace(); } + + protected: + Reference parentDirectoryLayer; + + virtual Reference getDirectoryLayerForPath(Path const& path) const { + return path.empty() ? parentDirectoryLayer : directoryLayer; + } + }; +} + +#endif \ No newline at end of file diff --git a/bindings/flow/DirectorySubspace.cpp b/bindings/flow/DirectorySubspace.cpp new file mode 100755 index 0000000000..27483b4a6e --- /dev/null +++ b/bindings/flow/DirectorySubspace.cpp @@ -0,0 +1,109 @@ +/* + * DirectorySubspace.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "DirectorySubspace.h" + +namespace FDB { + DirectorySubspace::DirectorySubspace(Path const& path, StringRef const& prefix, Reference directoryLayer, Standalone const& layer) + : Subspace(prefix), directoryLayer(directoryLayer), path(path), layer(layer) { } + + + Future> DirectorySubspace::create(Reference const& tr, Path const& path, Standalone const& layer, + Optional> const& prefix) + { + return directoryLayer->create(tr, getPartitionSubpath(path), layer, prefix); + } + + Future> DirectorySubspace::open(Reference const& tr, Path const& path, Standalone const& layer) { + return directoryLayer->open(tr, getPartitionSubpath(path), layer); + } + + Future> DirectorySubspace::createOrOpen(Reference const& tr, Path const& path, Standalone const& layer) { + return directoryLayer->createOrOpen(tr, getPartitionSubpath(path), layer); + } + + Future DirectorySubspace::exists(Reference const& tr, Path const& path) { + Reference directoryLayer = getDirectoryLayerForPath(path); + return directoryLayer->exists(tr, getPartitionSubpath(path, directoryLayer)); + } + + Future>> DirectorySubspace::list(Reference const& tr, Path const& path) { + return directoryLayer->list(tr, getPartitionSubpath(path)); + } + + Future> DirectorySubspace::move(Reference const& tr, Path const& oldPath, Path const& newPath) { + return directoryLayer->move(tr, getPartitionSubpath(oldPath), getPartitionSubpath(newPath)); + } + + Future> DirectorySubspace::moveTo(Reference const& tr, Path const& newAbsolutePath) { + Reference directoryLayer = getDirectoryLayerForPath(Path()); + Path directoryLayerPath = directoryLayer->getPath(); + + if(directoryLayerPath.size() > newAbsolutePath.size()) { + return cannot_move_directory_between_partitions(); + } + + for(int i = 0; i < directoryLayerPath.size(); ++i) { + if(directoryLayerPath[i] != newAbsolutePath[i]) { + return cannot_move_directory_between_partitions(); + } + } + + Path newRelativePath(newAbsolutePath.begin() + directoryLayerPath.size(), newAbsolutePath.end()); + return directoryLayer->move(tr, getPartitionSubpath(Path(), directoryLayer), newRelativePath); + } + + Future DirectorySubspace::remove(Reference const& tr, Path const& path) { + Reference directoryLayer = getDirectoryLayerForPath(path); + return directoryLayer->remove(tr, getPartitionSubpath(path, directoryLayer)); + } + + Future DirectorySubspace::removeIfExists(Reference const& tr, Path const& path) { + Reference directoryLayer = getDirectoryLayerForPath(path); + return directoryLayer->removeIfExists(tr, getPartitionSubpath(path, directoryLayer)); + } + + Reference DirectorySubspace::getDirectoryLayer() { + return directoryLayer; + } + + const Standalone DirectorySubspace::getLayer() const { + return layer; + } + + const IDirectory::Path DirectorySubspace::getPath() const { + return path; + } + + IDirectory::Path DirectorySubspace::getPartitionSubpath(Path const& path, Reference directoryLayer) const { + if(!directoryLayer) { + directoryLayer = this->directoryLayer; + } + + Path newPath(this->path.begin() + directoryLayer->getPath().size(), this->path.end()); + newPath.insert(newPath.end(), path.begin(), path.end()); + + return newPath; + } + + Reference DirectorySubspace::getDirectoryLayerForPath(Path const& path) const { + return directoryLayer; + } +} diff --git a/bindings/flow/DirectorySubspace.h b/bindings/flow/DirectorySubspace.h new file mode 100755 index 0000000000..9990ac7c5f --- /dev/null +++ b/bindings/flow/DirectorySubspace.h @@ -0,0 +1,66 @@ +/* + * DirectorySubspace.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_FLOW_DIRECTORY_SUBSPACE_H +#define FDB_FLOW_DIRECTORY_SUBSPACE_H + +#pragma once + +#include "IDirectory.h" +#include "DirectoryLayer.h" +#include "Subspace.h" + +namespace FDB { + class DirectorySubspace : public IDirectory, public Subspace { + + public: + DirectorySubspace(Path const& path, StringRef const& prefix, Reference directorLayer, Standalone const& layer = Standalone()); + virtual ~DirectorySubspace() {} + + virtual Future> create(Reference const& tr, Path const& path, Standalone const& layer = Standalone(), + Optional> const& prefix = Optional>()); + + virtual Future> open(Reference const& tr, Path const& path, Standalone const& layer = Standalone()); + virtual Future> createOrOpen(Reference const& tr, Path const& path, Standalone const& layer = Standalone()); + + virtual Future exists(Reference const& tr, Path const& path = Path()); + virtual Future>> list(Reference const& tr, Path const& path = Path()); + + virtual Future> move(Reference const& tr, Path const& oldPath, Path const& newPath); + virtual Future> moveTo(Reference const& tr, Path const& newAbsolutePath); + + virtual Future remove(Reference const& tr, Path const& path = Path()); + virtual Future removeIfExists(Reference const& tr, Path const& path = Path()); + + virtual Reference getDirectoryLayer(); + virtual const Standalone getLayer() const; + virtual const Path getPath() const; + + protected: + Reference directoryLayer; + Path path; + Standalone layer; + + virtual Path getPartitionSubpath(Path const& path, Reference directoryLayer = Reference()) const; + virtual Reference getDirectoryLayerForPath(Path const& path) const; + }; +} + +#endif \ No newline at end of file diff --git a/bindings/flow/FDBLoanerTypes.h b/bindings/flow/FDBLoanerTypes.h new file mode 100755 index 0000000000..fc76079634 --- /dev/null +++ b/bindings/flow/FDBLoanerTypes.h @@ -0,0 +1,315 @@ +/* + * FDBLoanerTypes.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_FLOW_LOANER_TYPES_H +#define FDB_FLOW_LOANER_TYPES_H + +namespace FDB { + typedef StringRef KeyRef; + typedef StringRef ValueRef; + + typedef int64_t Version; + + typedef Standalone Key; + typedef Standalone Value; + + inline Key keyAfter( const KeyRef& key ) { + if(key == LiteralStringRef("\xff\xff")) + return key; + + Standalone r; + uint8_t* s = new (r.arena()) uint8_t[ key.size() + 1 ]; + memcpy(s, key.begin(), key.size() ); + s[key.size()] = 0; + ((StringRef&) r) = StringRef( s, key.size() + 1 ); + return r; + } + + inline KeyRef keyAfter( const KeyRef& key, Arena& arena ) { + if(key == LiteralStringRef("\xff\xff")) + return key; + uint8_t* t = new ( arena ) uint8_t[ key.size()+1 ]; + memcpy(t, key.begin(), key.size() ); + t[key.size()] = 0; + return KeyRef(t,key.size()+1); + } + + struct KeySelectorRef { + KeyRef key; // Find the last item less than key + bool orEqual; // (or equal to key, if this is true) + int offset; // and then move forward this many items (or backward if negative) + KeySelectorRef() {} + KeySelectorRef( const KeyRef& key, bool orEqual, int offset ) : key(key), orEqual(orEqual), offset(offset) {} + + KeySelectorRef( Arena& arena, const KeySelectorRef& copyFrom ) : key(arena,copyFrom.key), orEqual(copyFrom.orEqual), offset(copyFrom.offset) {} + int expectedSize() const { return key.expectedSize(); } + + // std::string toString() const { + // if (offset > 0) { + // if (orEqual) return format("firstGreaterThan(%s)%+d", printable(key).c_str(), offset-1); + // else return format("firstGreaterOrEqual(%s)%+d", printable(key).c_str(), offset-1); + // } else { + // if (orEqual) return format("lastLessOrEqual(%s)%+d", printable(key).c_str(), offset); + // else return format("lastLessThan(%s)%+d", printable(key).c_str(), offset); + // } + // } + + bool isBackward() const { return !orEqual && offset<=0; } // True if the resolution of the KeySelector depends only on keys less than key + bool isFirstGreaterOrEqual() const { return !orEqual && offset==1; } + bool isFirstGreaterThan() const { return orEqual && offset==1; } + bool isLastLessOrEqual() const { return orEqual && offset==0; } + + // True iff, regardless of the contents of the database, lhs must resolve to a key > rhs + bool isDefinitelyGreater( KeyRef const& k ) { + return offset >= 1 && ( isFirstGreaterOrEqual() ? key > k : key >= k ); + } + // True iff, regardless of the contents of the database, lhs must resolve to a key < rhs + bool isDefinitelyLess( KeyRef const& k ) { + return offset <= 0 && ( isLastLessOrEqual() ? key < k : key <= k ); + } + + template + void serialize( Ar& ar ) { + ar & key & orEqual & offset; + } + }; + inline bool operator == (const KeySelectorRef& lhs, const KeySelectorRef& rhs) { return lhs.key == rhs.key && lhs.orEqual==rhs.orEqual && lhs.offset==rhs.offset; } + inline KeySelectorRef lastLessThan( const KeyRef& k ) { + return KeySelectorRef( k, false, 0 ); + } + inline KeySelectorRef lastLessOrEqual( const KeyRef& k ) { + return KeySelectorRef( k, true, 0 ); + } + inline KeySelectorRef firstGreaterThan( const KeyRef& k ) { + return KeySelectorRef( k, true, +1 ); + } + inline KeySelectorRef firstGreaterOrEqual( const KeyRef& k ) { + return KeySelectorRef( k, false, +1 ); + } + inline KeySelectorRef operator + (const KeySelectorRef& s, int off) { + return KeySelectorRef(s.key, s.orEqual, s.offset+off); + } + inline KeySelectorRef operator - (const KeySelectorRef& s, int off) { + return KeySelectorRef(s.key, s.orEqual, s.offset-off); + } + + typedef Standalone KeySelector; + + struct KeyValueRef { + KeyRef key; + ValueRef value; + KeyValueRef() {} + KeyValueRef( const KeyRef& key, const ValueRef& value ) : key(key), value(value) {} + KeyValueRef( Arena& a, const KeyValueRef& copyFrom ) : key(a, copyFrom.key), value(a, copyFrom.value) {} + bool operator == ( const KeyValueRef& r ) const { return key == r.key && value == r.value; } + + int expectedSize() const { return key.expectedSize() + value.expectedSize(); } + + template + force_inline void serialize(Ar& ar) { ar & key & value; } + + struct OrderByKey { + bool operator()(KeyValueRef const& a, KeyValueRef const& b) const { + return a.key < b.key; + } + template + bool operator()(T const& a, KeyValueRef const& b) const { + return a < b.key; + } + template + bool operator()(KeyValueRef const& a, T const& b) const { + return a.key < b; + } + }; + + struct OrderByKeyBack { + bool operator()(KeyValueRef const& a, KeyValueRef const& b) const { + return a.key > b.key; + } + template + bool operator()(T const& a, KeyValueRef const& b) const { + return a > b.key; + } + template + bool operator()(KeyValueRef const& a, T const& b) const { + return a.key > b; + } + }; + }; + + typedef Standalone KeyValue; + + struct RangeResultRef : VectorRef { + bool more; // True if (but not necessarily only if) values remain in the *key* range requested (possibly beyond the limits requested) + // False implies that no such values remain + Optional readThrough; // Only present when 'more' is true. When present, this value represent the end (or beginning if reverse) of the range + // which was read to produce these results. This is guarenteed to be less than the requested range. + bool readToBegin; + bool readThroughEnd; + + RangeResultRef() : more(false), readToBegin(false), readThroughEnd(false) {} + RangeResultRef( Arena& p, const RangeResultRef& toCopy ) : more( toCopy.more ), readToBegin( toCopy.readToBegin ), readThroughEnd( toCopy.readThroughEnd ), readThrough( toCopy.readThrough.present() ? KeyRef( p, toCopy.readThrough.get() ) : Optional() ), VectorRef( p, toCopy ) {} + RangeResultRef( const VectorRef& value, bool more, Optional readThrough = Optional() ) : VectorRef( value ), more( more ), readThrough( readThrough ), readToBegin( false ), readThroughEnd( false ) {} + RangeResultRef( bool readToBegin, bool readThroughEnd ) : more(false), readToBegin(readToBegin), readThroughEnd(readThroughEnd) { } + + template + void serialize( Ar& ar ) { + ar & ((VectorRef&)*this) & more & readThrough & readToBegin & readThroughEnd; + } + }; + + struct GetRangeLimits { + enum { ROW_LIMIT_UNLIMITED = -1, BYTE_LIMIT_UNLIMITED = -1 }; + + int rows; + int minRows; + int bytes; + + GetRangeLimits() : rows( ROW_LIMIT_UNLIMITED ), minRows(1), bytes( BYTE_LIMIT_UNLIMITED ) {} + explicit GetRangeLimits( int rowLimit ) : rows( rowLimit ), minRows(1), bytes( BYTE_LIMIT_UNLIMITED ) {} + GetRangeLimits( int rowLimit, int byteLimit ) : rows( rowLimit ), minRows(1), bytes( byteLimit ) {} + + void decrement( VectorRef const& data ); + void decrement( KeyValueRef const& data ); + + // True if either the row or byte limit has been reached + bool isReached(); + + // True if data would cause the row or byte limit to be reached + bool reachedBy( VectorRef const& data ); + + bool hasByteLimit(); + bool hasRowLimit(); + + bool hasSatisfiedMinRows(); + bool isValid() { return (rows >= 0 || rows == ROW_LIMIT_UNLIMITED) + && (bytes >= 0 || bytes == BYTE_LIMIT_UNLIMITED) + && minRows >= 0 && (minRows <= rows || rows == ROW_LIMIT_UNLIMITED); } + }; + + struct KeyRangeRef { + const KeyRef begin, end; + KeyRangeRef() {} + KeyRangeRef( const KeyRef& begin, const KeyRef& end ) : begin(begin), end(end) { + if( begin > end ) { + throw inverted_range(); + } + } + KeyRangeRef( Arena& a, const KeyRangeRef& copyFrom ) : begin(a, copyFrom.begin), end(a, copyFrom.end) {} + bool operator == ( const KeyRangeRef& r ) const { return begin == r.begin && end == r.end; } + bool operator != ( const KeyRangeRef& r ) const { return begin != r.begin || end != r.end; } + bool contains( const KeyRef& key ) const { return begin <= key && key < end; } + bool contains( const KeyRangeRef& keys ) const { return begin <= keys.begin && keys.end <= end; } + bool intersects( const KeyRangeRef& keys ) const { return begin < keys.end && keys.begin < end; } + bool empty() const { return begin == end; } + + Standalone withPrefix( const StringRef& prefix ) const { + return KeyRangeRef( begin.withPrefix(prefix), end.withPrefix(prefix) ); + } + + const KeyRangeRef& operator = (const KeyRangeRef& rhs) { + const_cast(begin) = rhs.begin; + const_cast(end) = rhs.end; + return *this; + } + + int expectedSize() const { return begin.expectedSize() + end.expectedSize(); } + + template + force_inline void serialize(Ar& ar) { + ar & const_cast(begin) & const_cast(end); + if( begin > end ) { + throw inverted_range(); + }; + } + + struct ArbitraryOrder { + bool operator()(KeyRangeRef const& a, KeyRangeRef const& b) const { + if (a.begin < b.begin) return true; + if (a.begin > b.begin) return false; + return a.end < b.end; + } + }; + }; + + inline KeyRangeRef operator & (const KeyRangeRef& lhs, const KeyRangeRef& rhs) { + KeyRef b = std::max(lhs.begin, rhs.begin), e = std::min(lhs.end, rhs.end); + if (e < b) + return KeyRangeRef(); + return KeyRangeRef(b,e); + } + + typedef Standalone KeyRange; + + std::string printable( const StringRef& val ); + + template + static std::string describe(T const& item) { + return item.toString(); + } + template + static std::string describe(std::map const& items, int max_items = -1) { + if (!items.size()) + return "[no items]"; + + std::string s; + int count = 0; + for (auto it = items.begin(); it != items.end(); it++) { + if (++count > max_items && max_items >= 0) + break; + if (count > 1) s += ","; + s += describe(it->first) + "=>" + describe(it->second); + } + return s; + } + + template + static std::string describeList(T const& items, int max_items) { + if (!items.size()) + return "[no items]"; + + std::string s; + int count = 0; + for (auto const& item : items) { + if (++count > max_items && max_items >= 0) + break; + if (count > 1) s += ","; + s += describe(item); + } + return s; + } + + template + static std::string describe(std::vector const& items, int max_items = -1) { + return describeList(items, max_items); + } + + template + static std::string describe(std::set const& items, int max_items = -1) { + return describeList(items, max_items); + } + + template + static std::string describe(std::pair const& pair) { + return "first: " + describe(pair.first) + " second: " + describe(pair.second); + } +} + +#endif /* FDB_LOANER_TYPES_H */ diff --git a/bindings/flow/HighContentionAllocator.actor.cpp b/bindings/flow/HighContentionAllocator.actor.cpp new file mode 100755 index 0000000000..e6981cfaa5 --- /dev/null +++ b/bindings/flow/HighContentionAllocator.actor.cpp @@ -0,0 +1,110 @@ +/* + * HighContentionAllocator.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "HighContentionAllocator.h" + +namespace FDB { + ACTOR Future> _allocate(Reference tr, Subspace counters, Subspace recent){ + state int64_t start = 0; + state int64_t window = 0; + + loop { + FDBStandalone range = wait(tr->getRange(counters.range(), 1, true, true)); + + if(range.size() > 0) { + start = counters.unpack(range[0].key).getInt(0); + } + + state bool windowAdvanced = false; + loop { + // if thread safety is needed, this should be locked { + if(windowAdvanced) { + tr->clear(KeyRangeRef(counters.key(), counters.get(start).key())); + tr->setOption(FDBTransactionOption::FDB_TR_OPTION_NEXT_WRITE_NO_WRITE_CONFLICT_RANGE); + tr->clear(KeyRangeRef(recent.key(), recent.get(start).key())); + } + + int64_t inc = 1; + tr->atomicOp(counters.get(start).key(), StringRef((uint8_t*)&inc, 8), FDB_MUTATION_TYPE_ADD); + Future>> countFuture = tr->get(counters.get(start).key(), true); + // } + + Optional> countValue = wait(countFuture); + + int64_t count = 0; + if(countValue.present()) { + if(countValue.get().size() != 8) { + throw invalid_database_value(); + } + count = *(int64_t*)countValue.get().begin(); + } + + window = HighContentionAllocator::windowSize(start); + if(count * 2 < window) { + break; + } + + start += window; + windowAdvanced = true; + } + + loop { + state int64_t candidate = g_random->randomInt(start, start + window); + + // if thread safety is needed, this should be locked { + state Future> latestCounter = tr->getRange(counters.range(), 1, true, true); + state Future>> candidateValue = tr->get(recent.get(candidate).key()); + tr->setOption(FDBTransactionOption::FDB_TR_OPTION_NEXT_WRITE_NO_WRITE_CONFLICT_RANGE); + tr->set(recent.get(candidate).key(), ValueRef()); + // } + + Void _ = wait(success(latestCounter) && success(candidateValue)); + int64_t currentWindowStart = 0; + if(latestCounter.get().size() > 0) { + currentWindowStart = counters.unpack(latestCounter.get()[0].key).getInt(0); + } + + if(currentWindowStart > start) { + break; + } + + if(!candidateValue.get().present()) { + tr->addWriteConflictKey(recent.get(candidate).key()); + return Tuple().append(candidate).pack(); + } + } + } + } + + Future> HighContentionAllocator::allocate(Reference const& tr) const { + return _allocate(tr, counters, recent); + } + + int64_t HighContentionAllocator::windowSize(int64_t start) { + if (start < 255) { + return 64; + } + if (start < 65535) { + return 1024; + } + + return 8192; + } +} diff --git a/bindings/flow/HighContentionAllocator.h b/bindings/flow/HighContentionAllocator.h new file mode 100755 index 0000000000..602bddaf56 --- /dev/null +++ b/bindings/flow/HighContentionAllocator.h @@ -0,0 +1,41 @@ +/* + * HighContentionAllocator.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_FLOW_HIGH_CONTENTION_ALLOCATOR_H +#define FDB_FLOW_HIGH_CONTENTION_ALLOCATOR_H + +#pragma once + +#include "Subspace.h" + +namespace FDB { + class HighContentionAllocator { + public: + HighContentionAllocator(Subspace subspace) : counters(subspace.get(0)), recent(subspace.get(1)) {} + Future> allocate(Reference const& tr) const; + + static int64_t windowSize(int64_t start); + private: + Subspace counters; + Subspace recent; + }; +} + +#endif diff --git a/bindings/flow/IDirectory.h b/bindings/flow/IDirectory.h new file mode 100755 index 0000000000..73ebe94ffb --- /dev/null +++ b/bindings/flow/IDirectory.h @@ -0,0 +1,60 @@ +/* + * IDirectory.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_FLOW_IDIRECTORY_H +#define FDB_FLOW_IDIRECTORY_H + +#pragma once + +#include "flow/flow.h" +#include "bindings/flow/fdb_flow.h" + +namespace FDB { + class DirectoryLayer; + class DirectorySubspace; + + class IDirectory : public ReferenceCounted { + public: + typedef std::vector> Path; + + virtual Future> create(Reference const& tr, Path const& path, Standalone const& layer = Standalone(), + Optional> const& prefix = Optional>()) = 0; + + virtual Future> open(Reference const& tr, Path const& path, Standalone const& layer = Standalone()) = 0; + virtual Future> createOrOpen(Reference const& tr, Path const& path, Standalone const& layer = Standalone()) = 0; + + virtual Future exists(Reference const& tr, Path const& path = Path()) = 0; + virtual Future>> list(Reference const& tr, Path const& path = Path()) = 0; + + virtual Future> move(Reference const& tr, Path const& oldPath, Path const& newPath) = 0; + virtual Future> moveTo(Reference const& tr, Path const& newAbsolutePath) = 0; + + virtual Future remove(Reference const& tr, Path const& path = Path()) = 0; + virtual Future removeIfExists(Reference const& tr, Path const& path = Path()) = 0; + + virtual Reference getDirectoryLayer() = 0; + virtual const Standalone getLayer() const = 0; + virtual const Path getPath() const = 0; + + virtual ~IDirectory() {}; + }; +} + +#endif \ No newline at end of file diff --git a/bindings/flow/Node.actor.cpp b/bindings/flow/Node.actor.cpp new file mode 100755 index 0000000000..ca93f832b0 --- /dev/null +++ b/bindings/flow/Node.actor.cpp @@ -0,0 +1,70 @@ +/* + * Node.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "DirectoryLayer.h" + +namespace FDB { + DirectoryLayer::Node::Node(Reference const& directoryLayer, Optional const& subspace, IDirectory::Path const& path, IDirectory::Path const& targetPath) + : directoryLayer(directoryLayer), + subspace(subspace), + path(path), + targetPath(targetPath), + loadedMetadata(false) + { } + + bool DirectoryLayer::Node::exists() const { + return subspace.present(); + } + + ACTOR Future loadMetadata(DirectoryLayer::Node *n, Reference tr) { + if(!n->exists()){ + n->loadedMetadata = true; + return *n; + } + + Optional> layer = wait(tr->get(n->subspace.get().pack(DirectoryLayer::LAYER_KEY))); + + n->layer = layer.present() ? layer.get() : Standalone(); + n->loadedMetadata = true; + + return *n; + } + + //Calls to loadMetadata must keep the Node alive while the future is outstanding + Future DirectoryLayer::Node::loadMetadata(Reference tr) { + return FDB::loadMetadata(this, tr); + } + + bool DirectoryLayer::Node::isInPartition(bool includeEmptySubpath) const { + ASSERT(loadedMetadata); + return exists() && layer == DirectoryLayer::PARTITION_LAYER && (includeEmptySubpath || targetPath.size() > path.size()); + } + + IDirectory::Path DirectoryLayer::Node::getPartitionSubpath() const { + return Path(targetPath.begin() + path.size(), targetPath.end()); + } + + Reference DirectoryLayer::Node::getContents() const { + ASSERT(exists()); + ASSERT(loadedMetadata); + + return directoryLayer->contentsOfNode(subspace.get(), path, layer); +} +} diff --git a/bindings/flow/Subspace.cpp b/bindings/flow/Subspace.cpp new file mode 100755 index 0000000000..bbc064cbf1 --- /dev/null +++ b/bindings/flow/Subspace.cpp @@ -0,0 +1,91 @@ +/* + * Subspace.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Subspace.h" + +namespace FDB { + Subspace::Subspace(Tuple const& tuple, StringRef const& rawPrefix){ + StringRef packed = tuple.pack(); + + this->rawPrefix.reserve(this->rawPrefix.arena(), rawPrefix.size() + packed.size()); + this->rawPrefix.append(this->rawPrefix.arena(), rawPrefix.begin(), rawPrefix.size()); + this->rawPrefix.append(this->rawPrefix.arena(), packed.begin(), packed.size()); + } + + Subspace::Subspace(Tuple const& tuple, Standalone> const& rawPrefix) { + this->rawPrefix.reserve(this->rawPrefix.arena(), rawPrefix.size() + tuple.pack().size()); + this->rawPrefix.append(this->rawPrefix.arena(), rawPrefix.begin(), rawPrefix.size()); + this->rawPrefix.append(this->rawPrefix.arena(), tuple.pack().begin(), tuple.pack().size()); + } + + Subspace::Subspace(StringRef const& rawPrefix){ + this->rawPrefix.append(this->rawPrefix.arena(), rawPrefix.begin(), rawPrefix.size()); + } + + Subspace::~Subspace() { } + + Key Subspace::key() const { + return StringRef(rawPrefix.begin(), rawPrefix.size()); + } + + Key Subspace::pack(const Tuple& tuple) const { + return tuple.pack().withPrefix(StringRef(rawPrefix.begin(), rawPrefix.size())); + } + + Tuple Subspace::unpack(StringRef const& key) const { + if (!contains(key)) { + throw key_not_in_subspace(); + } + return Tuple::unpack(key.substr(rawPrefix.size())); + } + + KeyRange Subspace::range(Tuple const& tuple) const { + VectorRef begin; + VectorRef end; + + KeyRange keyRange; + + begin.reserve(keyRange.arena(), rawPrefix.size() + tuple.pack().size() + 1); + begin.append(keyRange.arena(), rawPrefix.begin(), rawPrefix.size()); + begin.append(keyRange.arena(), tuple.pack().begin(), tuple.pack().size()); + begin.push_back(keyRange.arena(), uint8_t('\x00')); + + end.reserve(keyRange.arena(), rawPrefix.size() + tuple.pack().size() + 1); + end.append(keyRange.arena(), rawPrefix.begin(), rawPrefix.size()); + end.append(keyRange.arena(), tuple.pack().begin(), tuple.pack().size()); + end.push_back(keyRange.arena(), uint8_t('\xff')); + + // FIXME: test that this uses the keyRange arena and doesn't create another one + keyRange.KeyRangeRef::operator=(KeyRangeRef(StringRef(begin.begin(), begin.size()), StringRef(end.begin(), end.size()))); + return keyRange; + } + + bool Subspace::contains(KeyRef const& key) const { + return key.startsWith(StringRef(rawPrefix.begin(), rawPrefix.size())); + } + + Subspace Subspace::subspace(Tuple const& tuple) const { + return Subspace(tuple, rawPrefix); + } + + Subspace Subspace::get(Tuple const& tuple) const { + return subspace(tuple); + } +} \ No newline at end of file diff --git a/bindings/flow/Subspace.h b/bindings/flow/Subspace.h new file mode 100755 index 0000000000..cc3fcb60ae --- /dev/null +++ b/bindings/flow/Subspace.h @@ -0,0 +1,80 @@ +/* + * Subspace.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_FLOW_SUBSPACE_H +#define FDB_FLOW_SUBSPACE_H + +#pragma once + +#include "flow/flow.h" +#include "bindings/flow/fdb_flow.h" +#include "Tuple.h" + +namespace FDB { + class Subspace { + public: + Subspace(Tuple const& tuple = Tuple(), StringRef const& rawPrefix = StringRef()); + Subspace(StringRef const& rawPrefix); + + virtual ~Subspace(); + + virtual Key key() const; + virtual bool contains(KeyRef const& key) const; + + virtual Key pack(Tuple const& tuple = Tuple()) const; + virtual Tuple unpack(KeyRef const& key) const; + virtual KeyRange range(Tuple const& tuple = Tuple()) const; + + template + Key pack(T const& item) const { + Tuple t; + t.append(item); + return pack(t); + } + + Key pack(StringRef const& item, bool utf8=false) const { + Tuple t; + t.append(item, utf8); + return pack(t); + } + + virtual Subspace subspace(Tuple const& tuple) const; + virtual Subspace get(Tuple const& tuple) const; + + template + Subspace get(T const& item) const { + Tuple t; + t.append(item); + return get(t); + } + + Subspace get(StringRef const& item, bool utf8=false) const { + Tuple t; + t.append(item, utf8); + return get(t); + } + + private: + Subspace(Tuple const& tuple, Standalone> const& rawPrefix); + Standalone> rawPrefix; + }; +} + +#endif \ No newline at end of file diff --git a/bindings/flow/Tuple.cpp b/bindings/flow/Tuple.cpp new file mode 100755 index 0000000000..25eeb3faf2 --- /dev/null +++ b/bindings/flow/Tuple.cpp @@ -0,0 +1,248 @@ +/* + * Tuple.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Tuple.h" + +namespace FDB { + static size_t find_string_terminator(const StringRef data, size_t offset) { + size_t i = offset; + while (i < data.size() - 1 && !(data[i] == '\x00' && data[i+1] != (uint8_t)'\xff')) { + i += (data[i] == '\x00' ? 2 : 1); + } + + return i; + } + + Tuple::Tuple(StringRef const& str) { + data.append(data.arena(), str.begin(), str.size()); + + size_t i = 0; + while(i < data.size()) { + offsets.push_back(i); + + if(data[i] == '\x01' || data[i] == '\x02') { + i = find_string_terminator(str, i+1) + 1; + } + else if(data[i] >= '\x0c' && data[i] <= '\x1c') { + i += abs(data[i] - '\x14') + 1; + } + else if(data[i] == '\x00') { + i += 1; + } + else { + throw invalid_tuple_data_type(); + } + } + } + + Tuple Tuple::unpack(StringRef const& str) { + return Tuple(str); + } + + Tuple& Tuple::append(Tuple const& tuple) { + for(size_t offset : tuple.offsets) { + offsets.push_back(offset + data.size()); + } + + data.append(data.arena(), tuple.data.begin(), tuple.data.size()); + + return *this; + } + + Tuple& Tuple::append(StringRef const& str, bool utf8) { + offsets.push_back(data.size()); + + const uint8_t utfChar = uint8_t(utf8 ? '\x02' : '\x01'); + data.append(data.arena(), &utfChar, 1); + + size_t lastPos = 0; + for(size_t pos = 0; pos < str.size(); ++pos) { + if(str[pos] == '\x00') { + data.append(data.arena(), str.begin() + lastPos, pos - lastPos); + data.push_back(data.arena(), (uint8_t)'\x00'); + data.push_back(data.arena(), (uint8_t)'\xff'); + lastPos = pos + 1; + } + } + + data.append(data.arena(), str.begin() + lastPos, str.size() - lastPos); + data.push_back(data.arena(), (uint8_t)'\x00'); + + return *this; + } + + Tuple& Tuple::append( int64_t value ) { + uint64_t swap = value; + bool neg = false; + + offsets.push_back( data.size() ); + + if ( value < 0 ) { + value = ~(-value); + neg = true; + } + + swap = bigEndian64(value); + + for ( int i = 0; i < 8; i++ ) { + if ( ((uint8_t*)&swap)[i] != (neg ? 255 : 0) ) { + data.push_back( data.arena(), (uint8_t)(20 + (8-i) * (neg ? -1 : 1)) ); + data.append( data.arena(), ((const uint8_t *)&swap) + i, 8 - i ); + return *this; + } + } + + data.push_back( data.arena(), (uint8_t)'\x14' ); + return *this; + } + + Tuple& Tuple::appendNull() { + offsets.push_back(data.size()); + data.push_back(data.arena(), (uint8_t)'\x00'); + return *this; + } + + Tuple::ElementType Tuple::getType(size_t index) const { + if(index >= offsets.size()) { + throw invalid_tuple_index(); + } + + uint8_t code = data[offsets[index]]; + + if(code == '\x00') { + return ElementType::NULL_TYPE; + } + else if(code == '\x01') { + return ElementType::BYTES; + } + else if(code == '\x02') { + return ElementType::UTF8; + } + else if(code >= '\x0c' && code <= '\x1c') { + return ElementType::INT; + } + else { + throw invalid_tuple_data_type(); + } + } + + Standalone Tuple::getString(size_t index) const { + if(index >= offsets.size()) { + throw invalid_tuple_index(); + } + + uint8_t code = data[offsets[index]]; + if(code != '\x01' && code != '\x02') { + throw invalid_tuple_data_type(); + } + + size_t b = offsets[index] + 1; + size_t e; + if (offsets.size() > index + 1) { + e = offsets[index+1]; + } else { + e = data.size(); + } + + Standalone result; + VectorRef staging; + + for (size_t i = b; i < e; ++i) { + if(data[i] == '\x00') { + staging.append(result.arena(), data.begin() + b, i - b); + ++i; + b = i + 1; + + if(i < e) { + staging.push_back(result.arena(), '\x00'); + } + } + } + + if(b < e) { + staging.append(result.arena(), data.begin() + b, e - b); + } + + result.StringRef::operator=(StringRef(staging.begin(), staging.size())); + return result; + } + + int64_t Tuple::getInt(size_t index) const { + if(index >= offsets.size()) { + throw invalid_tuple_index(); + } + + int64_t swap; + bool neg = false; + + ASSERT(offsets[index] < data.size()); + uint8_t code = data[offsets[index]]; + if(code < '\x0c' || code > '\x1c') { + throw invalid_tuple_data_type(); + } + + int8_t len = code - '\x14'; + + if ( len < 0 ) { + len = -len; + neg = true; + } + + memset( &swap, neg ? '\xff' : 0, 8 - len ); + memcpy( ((uint8_t*)&swap) + 8 - len, data.begin() + offsets[index] + 1, len ); + + swap = bigEndian64( swap ); + + if ( neg ) { + swap = -(~swap); + } + + return swap; + } + + KeyRange Tuple::range(Tuple const& tuple) const { + VectorRef begin; + VectorRef end; + + KeyRange keyRange; + + begin.reserve(keyRange.arena(), data.size() + tuple.pack().size() + 1); + begin.append(keyRange.arena(), data.begin(), data.size()); + begin.append(keyRange.arena(), tuple.pack().begin(), tuple.pack().size()); + begin.push_back(keyRange.arena(), uint8_t('\x00')); + + end.reserve(keyRange.arena(), data.size() + tuple.pack().size() + 1); + end.append(keyRange.arena(), data.begin(), data.size()); + end.append(keyRange.arena(), tuple.pack().begin(), tuple.pack().size()); + end.push_back(keyRange.arena(), uint8_t('\xff')); + + keyRange.KeyRangeRef::operator=(KeyRangeRef(StringRef(begin.begin(), begin.size()), StringRef(end.begin(), end.size()))); + return keyRange; + } + + Tuple Tuple::subTuple(size_t start, size_t end) const { + if(start >= offsets.size() || end <= start) { + return Tuple(); + } + + size_t endPos = end < offsets.size() ? offsets[end] : data.size(); + return Tuple(StringRef(data.begin() + offsets[start], endPos - offsets[start])); + } +} diff --git a/bindings/flow/Tuple.h b/bindings/flow/Tuple.h new file mode 100755 index 0000000000..890627f35e --- /dev/null +++ b/bindings/flow/Tuple.h @@ -0,0 +1,66 @@ +/* + * Tuple.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_FLOW_TUPLE_H +#define FDB_FLOW_TUPLE_H + +#pragma once + +#include "bindings/flow/fdb_flow.h" + +namespace FDB { + struct Tuple { + Tuple() {} + + static Tuple unpack(StringRef const& str); + + Tuple& append(Tuple const& tuple); + Tuple& append(StringRef const& str, bool utf8=false); + Tuple& append(int64_t); + Tuple& appendNull(); + + StringRef pack() const { return StringRef(data.begin(), data.size()); } + + template + Tuple& operator<<(T const& t) { + return append(t); + } + + enum ElementType { NULL_TYPE, INT, BYTES, UTF8 }; + + // this is number of elements, not length of data + size_t size() const { return offsets.size(); } + + ElementType getType(size_t index) const; + Standalone getString(size_t index) const; + int64_t getInt(size_t index) const; + + KeyRange range(Tuple const& tuple = Tuple()) const; + + Tuple subTuple(size_t beginIndex, size_t endIndex = std::numeric_limits::max()) const; + + private: + Tuple(const StringRef& data); + Standalone> data; + std::vector offsets; + }; +} + +#endif /* _FDB_TUPLE_H_ */ diff --git a/bindings/flow/fdb_flow.actor.cpp b/bindings/flow/fdb_flow.actor.cpp new file mode 100644 index 0000000000..678fa4a8cb --- /dev/null +++ b/bindings/flow/fdb_flow.actor.cpp @@ -0,0 +1,359 @@ +/* + * fdb_flow.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fdb_flow.h" + +#include "flow/DeterministicRandom.h" +#include "flow/SystemMonitor.h" + +#include + +using namespace FDB; + +THREAD_FUNC networkThread(void* fdb) { + ((FDB::API*)fdb)->runNetwork(); + THREAD_RETURN; +} + +ACTOR Future _test() { + API *fdb = FDB::API::selectAPIVersion(400); + auto c = fdb->createCluster( std::string() ); + auto db = c->createDatabase( LiteralStringRef("DB") ); + state Reference tr( new Transaction(db) ); + + // tr->setVersion(1); + + Version ver = wait( tr->getReadVersion() ); + printf("%ld\n", ver); + + state std::vector< Future > versions; + + state double starttime = timer_monotonic(); + state int i; + // for (i = 0; i < 100000; i++) { + // Version v = wait( tr->getReadVersion() ); + // } + for ( i = 0; i < 100000; i++ ) { + versions.push_back( tr->getReadVersion() ); + } + for ( i = 0; i < 100000; i++ ) { + Version v = wait( versions[i] ); + } + // Void _ = wait( waitForAllReady( versions ) ); + printf("Elapsed: %lf\n", timer_monotonic() - starttime ); + + tr->set( LiteralStringRef("foo"), LiteralStringRef("bar") ); + + Optional< FDBStandalone > v = wait( tr->get( LiteralStringRef("foo") ) ); + if ( v.present() ) { + printf("%s\n", v.get().toString().c_str() ); + } + + FDBStandalone r = wait( tr->getRange( KeyRangeRef( LiteralStringRef("a"), LiteralStringRef("z") ), 100 ) ); + + for ( auto kv : r ) { + printf("%s is %s\n", kv.key.toString().c_str(), kv.value.toString().c_str()); + } + + g_network->stop(); + return Void(); +} + +void fdb_flow_test() { + API *fdb = FDB::API::selectAPIVersion(400); + fdb->setupNetwork(); + startThread(networkThread, fdb); + + int randomSeed = platform::getRandomSeed(); + + g_random = new DeterministicRandom(randomSeed); + g_nondeterministic_random = new DeterministicRandom(platform::getRandomSeed()); + g_debug_random = new DeterministicRandom(platform::getRandomSeed()); + + g_network = newNet2( NetworkAddress(), false ); + + openTraceFile(NetworkAddress(), 1000000, 1000000, "."); + systemMonitor(); + uncancellable(recurring(&systemMonitor, 5.0, TaskFlushTrace)); + + Future t = _test(); + + g_network->run(); +} + +namespace FDB { + + static inline void throw_on_error( fdb_error_t e ) { + if (e) + throw Error(e); + } + + void CFuture::blockUntilReady() { + throw_on_error( fdb_future_block_until_ready( f ) ); + } + + void backToFutureCallback( FDBFuture* f, void* data ) { + g_network->onMainThread( Promise((SAV*)data), TaskDefaultOnMainThread ); // SOMEDAY: think about this priority + } + + // backToFuture( FDBFuture*, (FDBFuture* -> Type) ) -> Future + // Takes an FDBFuture (from the alien client world, with callbacks potentially firing on an alien thread) + // and converts it into a Future (with callbacks working on this thread, cancellation etc). + // You must pass as the second parameter a function which takes a ready FDBFuture* and returns a value of Type + ACTOR template static Future backToFuture( FDBFuture* _f, Function convertValue ) { + state Reference f( new CFuture(_f) ); + + Promise ready; + Future onReady = ready.getFuture(); + + throw_on_error( fdb_future_set_callback( f->f, backToFutureCallback, ready.extractRawPointer() ) ); + Void _ = wait( onReady ); + + return convertValue( f ); + } + + void API::setNetworkOption( FDBNetworkOption option, Optional value ) { + if ( value.present() ) + throw_on_error( fdb_network_set_option( option, value.get().begin(), value.get().size() ) ); + else + throw_on_error( fdb_network_set_option( option, NULL, 0 ) ); + } + + API* API::instance = NULL; + API::API(int version) : version(version) {} + + API* API::selectAPIVersion(int apiVersion) { + if(API::instance && apiVersion != API::instance->version) { + throw api_version_already_set(); + } + + if(apiVersion < 200 || apiVersion > FDB_API_VERSION) { + throw api_version_not_supported(); + } + + throw_on_error( fdb_select_api_version_impl(apiVersion, FDB_API_VERSION) ); + + if(!API::instance) { + API::instance = new API(apiVersion); + } + + return API::instance; + } + + void API::setupNetwork() { + throw_on_error( fdb_setup_network() ); + } + + void API::runNetwork() { + throw_on_error( fdb_run_network() ); + } + + void API::stopNetwork() { + throw_on_error( fdb_stop_network() ); + } + + bool API::evaluatePredicate(FDBErrorPredicate pred, Error const& e) { + return fdb_error_predicate( pred, e.code() ); + } + + Reference API::createCluster( std::string const& connFilename ) { + CFuture f( fdb_create_cluster( connFilename.c_str() ) ); + f.blockUntilReady(); + + FDBCluster* c; + throw_on_error( fdb_future_get_cluster( f.f, &c ) ); + + return Reference( new Cluster(c) ); + } + + Reference Cluster::createDatabase( Standalone dbName ) { + CFuture f( fdb_cluster_create_database( c, dbName.begin(), dbName.size() ) ); + f.blockUntilReady(); + + FDBDatabase* db; + throw_on_error( fdb_future_get_database( f.f, &db ) ); + + return Reference( new DatabaseContext(db) ); + } + + void DatabaseContext::setDatabaseOption(FDBDatabaseOption option, Optional value) { + if (value.present()) + throw_on_error(fdb_database_set_option(db, option, value.get().begin(), value.get().size())); + else + throw_on_error(fdb_database_set_option(db, option, NULL, 0)); + } + + Transaction::Transaction( Reference const& db ) { + throw_on_error( fdb_database_create_transaction( db->db, &tr ) ); + } + + void Transaction::setVersion( Version v ) { + fdb_transaction_set_read_version( tr, v ); + } + + Future Transaction::getReadVersion() { + return backToFuture( fdb_transaction_get_read_version( tr ), [](Reference f){ + Version value; + + throw_on_error( fdb_future_get_version( f->f, &value ) ); + + return value; + } ); + } + + Future< Optional> > Transaction::get( const Key& key, bool snapshot ) { + return backToFuture< Optional> >( fdb_transaction_get( tr, key.begin(), key.size(), snapshot ), [](Reference f) { + fdb_bool_t present; + uint8_t const* value; + int value_length; + + throw_on_error( fdb_future_get_value( f->f, &present, &value, &value_length ) ); + + if ( present ) { + return Optional>( FDBStandalone( f, ValueRef( value, value_length ) ) ); + } else { + return Optional>(); + } + } ); + } + + Future< Void > Transaction::watch( const Key& key ) { + return backToFuture< Void >( fdb_transaction_watch( tr, key.begin(), key.size() ), [](Reference f) { + throw_on_error( fdb_future_get_error( f->f ) ); + return Void(); + } ); + } + + Future< FDBStandalone > Transaction::getKey( const KeySelector& key, bool snapshot ) { + return backToFuture< FDBStandalone >( fdb_transaction_get_key( tr, key.key.begin(), key.key.size(), key.orEqual, key.offset, snapshot ), [](Reference f) { + uint8_t const* key; + int key_length; + + throw_on_error( fdb_future_get_key( f->f, &key, &key_length ) ); + + return FDBStandalone( f, KeyRef( key, key_length ) ); + } ); + } + + Future< FDBStandalone > Transaction::getRange( const KeySelector& begin, const KeySelector& end, GetRangeLimits limits, bool snapshot, bool reverse, FDBStreamingMode streamingMode ) { + // FIXME: iteration + return backToFuture< FDBStandalone >( fdb_transaction_get_range( tr, begin.key.begin(), begin.key.size(), begin.orEqual, begin.offset, end.key.begin(), end.key.size(), end.orEqual, end.offset, limits.rows, limits.bytes, streamingMode, 1, snapshot, reverse ), [](Reference f) { + FDBKeyValue const* kv; + int count; + fdb_bool_t more; + + throw_on_error( fdb_future_get_keyvalue_array( f->f, &kv, &count, &more ) ); + + return FDBStandalone( f, RangeResultRef( VectorRef( (KeyValueRef*)kv, count ), more ) ); + } ); + } + + void Transaction::addReadConflictRange( KeyRangeRef const& keys ) { + throw_on_error( fdb_transaction_add_conflict_range( tr, keys.begin.begin(), keys.begin.size(), keys.end.begin(), keys.end.size(), FDB_CONFLICT_RANGE_TYPE_READ ) ); + } + + void Transaction::addReadConflictKey( KeyRef const& key ) { + return addReadConflictRange(KeyRange(KeyRangeRef(key, keyAfter(key)))); + } + + void Transaction::addWriteConflictRange( KeyRangeRef const& keys ) { + throw_on_error( fdb_transaction_add_conflict_range( tr, keys.begin.begin(), keys.begin.size(), keys.end.begin(), keys.end.size(), FDB_CONFLICT_RANGE_TYPE_WRITE ) ); + } + + void Transaction::addWriteConflictKey( KeyRef const& key ) { + return addWriteConflictRange(KeyRange(KeyRangeRef(key, keyAfter(key)))); + } + + void Transaction::atomicOp( const KeyRef& key, const ValueRef& operand, FDBMutationType operationType ) { + fdb_transaction_atomic_op( tr, key.begin(), key.size(), operand.begin(), operand.size(), operationType ); + } + + void Transaction::set( const KeyRef& key, const ValueRef& value ) { + fdb_transaction_set( tr, key.begin(), key.size(), value.begin(), value.size() ); + } + + void Transaction::clear( const KeyRangeRef& range ) { + fdb_transaction_clear_range( tr, range.begin.begin(), range.begin.size(), range.end.begin(), range.end.size() ); + } + + void Transaction::clear( const KeyRef& key ) { + fdb_transaction_clear( tr, key.begin(), key.size() ); + } + + Future Transaction::commit() { + return backToFuture< Void >( fdb_transaction_commit( tr ), [](Reference f) { + throw_on_error( fdb_future_get_error( f->f ) ); + return Void(); + } ); + } + + Version Transaction::getCommittedVersion() { + Version v; + + throw_on_error( fdb_transaction_get_committed_version( tr, &v ) ); + return v; + } + + Future> Transaction::getVersionstamp() { + return backToFuture< FDBStandalone >( fdb_transaction_get_versionstamp( tr ), [](Reference f) { + uint8_t const* key; + int key_length; + + throw_on_error( fdb_future_get_key( f->f, &key, &key_length ) ); + + return FDBStandalone( f, StringRef( key, key_length ) ); + } ); + } + + void Transaction::setOption( FDBTransactionOption option, Optional value ) { + if ( value.present() ) { + throw_on_error( fdb_transaction_set_option( tr, option, value.get().begin(), value.get().size() ) ); + } else { + throw_on_error( fdb_transaction_set_option( tr, option, NULL, 0 ) ); + } + } + + Future Transaction::onError( Error const& e ) { + return backToFuture< Void >( fdb_transaction_on_error( tr, e.code() ), [](Reference f) { + throw_on_error( fdb_future_get_error( f->f ) ); + return Void(); + } ); + } + + void Transaction::cancel() { + fdb_transaction_cancel( tr ); + } + + void Transaction::reset() { + fdb_transaction_reset( tr ); + } + + std::string printable( const StringRef& val ) { + std::string s; + for(int i=0; i= 32 && b < 127 && b != '\\') s += (char)b; + else if (b == '\\') s += "\\\\"; + else s += format("\\x%02x", b); + } + return s; + } + +} diff --git a/bindings/flow/fdb_flow.h b/bindings/flow/fdb_flow.h new file mode 100644 index 0000000000..013c0f0826 --- /dev/null +++ b/bindings/flow/fdb_flow.h @@ -0,0 +1,182 @@ +/* + * fdb_flow.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_FLOW_FDB_FLOW_H +#define FDB_FLOW_FDB_FLOW_H + +#include + +#define FDB_API_VERSION 500 +#include +#undef DLLEXPORT + +#include "FDBLoanerTypes.h" + +namespace FDB { + + class DatabaseContext : public ReferenceCounted, NonCopyable { + friend class Cluster; + friend class Transaction; + public: + ~DatabaseContext() { + fdb_database_destroy( db ); + } + + void setDatabaseOption(FDBDatabaseOption option, Optional value = Optional()); + + private: + FDBDatabase* db; + explicit DatabaseContext( FDBDatabase* db ) : db(db) {} + }; + + class Cluster : public ReferenceCounted, NonCopyable { + public: + ~Cluster() { + fdb_cluster_destroy( c ); + } + + Reference createDatabase( Standalone dbName ); + + private: + explicit Cluster( FDBCluster* c ) : c(c) {} + FDBCluster* c; + + friend class API; + }; + + class API { + public: + static API* selectAPIVersion(int apiVersion); + + void setNetworkOption(FDBNetworkOption option, Optional value = Optional()); + + void setupNetwork(); + void runNetwork(); + void stopNetwork(); + + Reference createCluster( std::string const& connFilename ); + + bool evaluatePredicate(FDBErrorPredicate pred, Error const& e); + + private: + static API* instance; + + API(int version); + int version; + }; + + struct CFuture : NonCopyable, ReferenceCounted, FastAllocated { + CFuture() : f(NULL) {} + explicit CFuture( FDBFuture* f ) : f(f) {} + ~CFuture() { + if (f) { + fdb_future_destroy(f); + } + } + + void blockUntilReady(); + + FDBFuture* f; + }; + + template + class FDBStandalone : public T { + public: + FDBStandalone() {} + FDBStandalone( Reference f, T const& t ) : T(t), f(f) {} + FDBStandalone( FDBStandalone const& o ) : T((T const&)o), f(o.f) {} + private: + Reference f; + }; + + class Transaction : public ReferenceCounted, private NonCopyable, public FastAllocated { + public: + explicit Transaction( Reference const& db ); + ~Transaction() { + if (tr) { + fdb_transaction_destroy(tr); + } + } + + void setVersion( Version v ); + Future getReadVersion(); + + Future< Optional> > get( const Key& key, bool snapshot = false ); + Future< Void > watch( const Key& key ); + Future< FDBStandalone > getKey( const KeySelector& key, bool snapshot = false ); + Future< FDBStandalone > getRange( const KeySelector& begin, const KeySelector& end, GetRangeLimits limits = GetRangeLimits(), bool snapshot = false, bool reverse = false, FDBStreamingMode streamingMode = FDB_STREAMING_MODE_SERIAL); + Future< FDBStandalone > getRange( const KeySelector& begin, const KeySelector& end, int limit, bool snapshot = false, bool reverse = false, FDBStreamingMode streamingMode = FDB_STREAMING_MODE_SERIAL ) { + return getRange( begin, end, GetRangeLimits(limit), snapshot, reverse, streamingMode ); + } + Future< FDBStandalone > getRange( const KeyRange& keys, int limit, bool snapshot = false, bool reverse = false, FDBStreamingMode streamingMode = FDB_STREAMING_MODE_SERIAL ) { + return getRange( KeySelector( firstGreaterOrEqual(keys.begin), keys.arena() ), + KeySelector( firstGreaterOrEqual(keys.end), keys.arena() ), + limit, snapshot, reverse, streamingMode ); + } + Future< FDBStandalone > getRange( const KeyRange& keys, GetRangeLimits limits = GetRangeLimits(), bool snapshot = false, bool reverse = false, FDBStreamingMode streamingMode = FDB_STREAMING_MODE_SERIAL ) { + return getRange( KeySelector( firstGreaterOrEqual(keys.begin), keys.arena() ), + KeySelector( firstGreaterOrEqual(keys.end), keys.arena() ), + limits, snapshot, reverse, streamingMode ); + } + + // Future< Standalone> > getAddressesForKey(const Key& key); + + void addReadConflictRange( KeyRangeRef const& keys ); + void addReadConflictKey( KeyRef const& key ); + void addWriteConflictRange( KeyRangeRef const& keys ); + void addWriteConflictKey( KeyRef const& key ); + // void makeSelfConflicting() { tr.makeSelfConflicting(); } + + void atomicOp( const KeyRef& key, const ValueRef& operand, FDBMutationType operationType ); + void set( const KeyRef& key, const ValueRef& value ); + void clear( const KeyRangeRef& range ); + void clear( const KeyRef& key ); + + Future commit(); + Version getCommittedVersion(); + Future> getVersionstamp(); + + void setOption( FDBTransactionOption option, Optional value = Optional() ); + + Future onError( Error const& e ); + + void cancel(); + void reset(); + // double getBackoff() { return tr.getBackoff(); } + // void debugTransaction(UID dID) { tr.debugTransaction(dID); } + + Transaction() : tr(NULL) {} + Transaction( Transaction&& r ) noexcept(true) { + tr = r.tr; + r.tr = NULL; + } + Transaction& operator=( Transaction&& r ) noexcept(true) { + tr = r.tr; + r.tr = NULL; + return *this; + } + + private: + FDBTransaction* tr; + }; + +} + +#endif diff --git a/bindings/flow/fdb_flow.vcxproj b/bindings/flow/fdb_flow.vcxproj new file mode 100755 index 0000000000..2fce553f59 --- /dev/null +++ b/bindings/flow/fdb_flow.vcxproj @@ -0,0 +1,150 @@ + + + + + + FDB_CLEAN_BUILD;%(PreprocessorDefinitions) + + + + Debug + X64 + + + Release + X64 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {2BA0A5E2-EB4C-4A32-948C-CBAABD77AF87} + v4.5.2 + Win32Proj + fdb_flow + + + $(SolutionDir)bin\$(Configuration)\ + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + $(IntDir)\$(MSBuildProjectName).log + + + + StaticLibrary + MultiByte + v140_xp + + + StaticLibrary + MultiByte + v140_xp + + + + + + + + + + true + ..\..\;C:\Program Files\boost_1_52_0;$(IncludePath) + + + false + ..\..\;C:\Program Files\boost_1_52_0;$(IncludePath) + + + + + + Level3 + false + ProgramDatabase + Disabled + EnableFastChecks + MultiThreadedDebug + WIN32;_WIN32_WINNT=0x0502;WINVER=0x0502;NTDDI_VERSION=0x05020000;_DEBUG;_HAS_ITERATOR_DEBUGGING=0;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories);..\c + true + /bigobj @..\..\flow/no_intellisense.opt %(AdditionalOptions) + + + Console + true + Advapi32.lib + + + $(TargetDir)flow.lib + + + + + Level3 + + + ProgramDatabase + Full + MultiThreaded + true + WIN32;_WIN32_WINNT=0x0502;WINVER=0x0502;NTDDI_VERSION=0x05020000;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories);..\c + NotSet + false + /bigobj @..\..\flow/no_intellisense.opt %(AdditionalOptions) + true + Speed + false + + + Console + true + false + false + Default + Advapi32.lib + /LTCG %(AdditionalOptions) + + + $(TargetDir)flow.lib + + + + + + + + + + \ No newline at end of file diff --git a/bindings/flow/local.mk b/bindings/flow/local.mk new file mode 100644 index 0000000000..95814f970e --- /dev/null +++ b/bindings/flow/local.mk @@ -0,0 +1,26 @@ +# +# local.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile; -*- + +fdb_flow_CFLAGS := -Ibindings/c $(fdbrpc_CFLAGS) +fdb_flow_LDFLAGS := -Llib -lfdb_c $(fdbrpc_LDFLAGS) +fdb_flow_LIBS := lib/libfdbrpc.a + diff --git a/bindings/flow/tester/DirectoryTester.actor.cpp b/bindings/flow/tester/DirectoryTester.actor.cpp new file mode 100644 index 0000000000..713359a2ef --- /dev/null +++ b/bindings/flow/tester/DirectoryTester.actor.cpp @@ -0,0 +1,561 @@ +/* + * DirectoryTester.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Tester.actor.h" + +using namespace FDB; + +ACTOR Future> popTuples(Reference data, int count = 1) { + state std::vector tuples; + + while(tuples.size() < count) { + Standalone sizeStr = wait(data->stack.pop()[0].value); + int size = Tuple::unpack(sizeStr).getInt(0); + + state std::vector tupleItems = data->stack.pop(size); + state Tuple tuple; + + state int index; + for(index = 0; index < tupleItems.size(); ++index) { + Standalone itemStr = wait(tupleItems[index].value); + tuple.append(Tuple::unpack(itemStr)); + } + + tuples.push_back(tuple); + } + + return tuples; +} + +ACTOR Future popTuple(Reference data) { + std::vector tuples = wait(popTuples(data)); + return tuples[0]; +} + +ACTOR Future> popPaths(Reference data, int count = 1) { + std::vector tuples = wait(popTuples(data, count)); + + std::vector paths; + for(auto &tuple : tuples) { + IDirectory::Path path; + for(int i = 0; i < tuple.size(); ++i) { + path.push_back(tuple.getString(i)); + } + + paths.push_back(path); + } + + return paths; +} + +ACTOR Future popPath(Reference data) { + std::vector paths = wait(popPaths(data)); + return paths[0]; +} + +std::string pathToString(IDirectory::Path const& path) { + std::string str; + str += "["; + for(int i = 0; i < path.size(); ++i) { + str += path[i].toString(); + if(i < path.size() - 1) { + str += ", "; + } + } + + return str + "]"; +} + +IDirectory::Path combinePaths(IDirectory::Path const& path1, IDirectory::Path const& path2) { + IDirectory::Path outPath(path1.begin(), path1.end()); + for(auto p : path2) { + outPath.push_back(p); + } + + return outPath; +} + +void logOp(std::string message, bool force=false) { + if(LOG_OPS || force) { + printf("%s\n", message.c_str()); + fflush(stdout); + } +} + +//DIRECTORY_CREATE_SUBSPACE +struct DirectoryCreateSubspaceFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state Tuple path = wait(popTuple(data)); + Tuple rawPrefix = wait(data->stack.waitAndPop()); + + logOp(format("Created subspace at %s: %s", tupleToString(path).c_str(), printable(rawPrefix.getString(0)).c_str())); + data->directoryData.push(new Subspace(path, rawPrefix.getString(0))); + return Void(); + } +}; +const char* DirectoryCreateSubspaceFunc::name = "DIRECTORY_CREATE_SUBSPACE"; +REGISTER_INSTRUCTION_FUNC(DirectoryCreateSubspaceFunc); + +//DIRECTORY_CREATE_LAYER +struct DirectoryCreateLayerFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + std::vector args = wait(data->stack.waitAndPop(3)); + + int index1 = args[0].getInt(0); + int index2 = args[1].getInt(0); + bool allowManualPrefixes = args[2].getInt(0) != 0; + + if(!data->directoryData.directoryList[index1].valid() || !data->directoryData.directoryList[index2].valid()) { + logOp("Create directory layer: None"); + data->directoryData.push(); + } + else { + Subspace* nodeSubspace = data->directoryData.directoryList[index1].subspace.get(); + Subspace* contentSubspace = data->directoryData.directoryList[index2].subspace.get(); + logOp(format("Create directory layer: node_subspace (%d) = %s, content_subspace (%d) = %s, allow_manual_prefixes = %d", index1, printable(nodeSubspace->key()).c_str(), index2, printable(nodeSubspace->key()).c_str(), allowManualPrefixes)); + data->directoryData.push(Reference(new DirectoryLayer(*nodeSubspace, *contentSubspace, allowManualPrefixes))); + } + + return Void(); + } +}; +const char* DirectoryCreateLayerFunc::name = "DIRECTORY_CREATE_LAYER"; +REGISTER_INSTRUCTION_FUNC(DirectoryCreateLayerFunc); + +//DIRECTORY_CHANGE +struct DirectoryChangeFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple index = wait(data->stack.waitAndPop()); + data->directoryData.directoryListIndex = index.getInt(0); + ASSERT(data->directoryData.directoryListIndex < data->directoryData.directoryList.size()); + + if(!data->directoryData.directoryList[data->directoryData.directoryListIndex].valid()) { + data->directoryData.directoryListIndex = data->directoryData.directoryErrorIndex; + } + + if(LOG_DIRS) { + DirectoryOrSubspace d = data->directoryData.directoryList[data->directoryData.directoryListIndex]; + printf("Changed directory to %d (%s @\'%s\')\n", data->directoryData.directoryListIndex, d.typeString().c_str(), d.directory.present() ? pathToString(d.directory.get()->getPath()).c_str() : printable(d.subspace.get()->key()).c_str()); + fflush(stdout); + } + + return Void(); + } +}; +const char* DirectoryChangeFunc::name = "DIRECTORY_CHANGE"; +REGISTER_INSTRUCTION_FUNC(DirectoryChangeFunc); + +//DIRECTORY_SET_ERROR_INDEX +struct DirectorySetErrorIndexFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple index = wait(data->stack.waitAndPop()); + data->directoryData.directoryErrorIndex = index.getInt(0); + + return Void(); + } +}; +const char* DirectorySetErrorIndexFunc::name = "DIRECTORY_SET_ERROR_INDEX"; +REGISTER_INSTRUCTION_FUNC(DirectorySetErrorIndexFunc); + +//DIRECTORY_CREATE_OR_OPEN +struct DirectoryCreateOrOpenFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state IDirectory::Path path = wait(popPath(data)); + Tuple layerTuple = wait(data->stack.waitAndPop()); + Standalone layer = layerTuple.getType(0) == Tuple::NULL_TYPE ? StringRef() : layerTuple.getString(0); + + Reference directory = data->directoryData.directory(); + logOp(format("create_or_open %s: layer=%s", pathToString(combinePaths(directory->getPath(), path)).c_str(), printable(layer).c_str())); + + Reference dirSubspace = wait(executeMutation(instruction, [this, directory, layer] () { + return directory->createOrOpen(instruction->tr, path, layer); + })); + + data->directoryData.push(dirSubspace); + + return Void(); + } +}; +const char* DirectoryCreateOrOpenFunc::name = "DIRECTORY_CREATE_OR_OPEN"; +REGISTER_INSTRUCTION_FUNC(DirectoryCreateOrOpenFunc); + +//DIRECTORY_CREATE +struct DirectoryCreateFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state IDirectory::Path path = wait(popPath(data)); + std::vector args = wait(data->stack.waitAndPop(2)); + Standalone layer = args[0].getType(0) == Tuple::NULL_TYPE ? StringRef() : args[0].getString(0); + Optional> prefix = args[1].getType(0) == Tuple::NULL_TYPE ? Optional>() : args[1].getString(0); + + Reference directory = data->directoryData.directory(); + logOp(format("create %s: layer=%s, prefix=%s", pathToString(combinePaths(directory->getPath(), path)).c_str(), printable(layer).c_str(), prefix.present() ? printable(prefix.get()).c_str() : "")); + + Reference dirSubspace = wait(executeMutation(instruction, [this, directory, layer, prefix] () { + return directory->create(instruction->tr, path, layer, prefix); + })); + + data->directoryData.push(dirSubspace); + + return Void(); + } +}; +const char* DirectoryCreateFunc::name = "DIRECTORY_CREATE"; +REGISTER_INSTRUCTION_FUNC(DirectoryCreateFunc); + +//DIRECTORY_OPEN +struct DirectoryOpenFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state IDirectory::Path path = wait(popPath(data)); + Tuple layerTuple = wait(data->stack.waitAndPop()); + Standalone layer = layerTuple.getType(0) == Tuple::NULL_TYPE ? StringRef() : layerTuple.getString(0); + + Reference directory = data->directoryData.directory(); + logOp(format("open %s: layer=%s", pathToString(combinePaths(directory->getPath(), path)).c_str(), printable(layer).c_str())); + Reference dirSubspace = wait(directory->open(instruction->tr, path, layer)); + data->directoryData.push(dirSubspace); + + return Void(); + } +}; +const char* DirectoryOpenFunc::name = "DIRECTORY_OPEN"; +REGISTER_INSTRUCTION_FUNC(DirectoryOpenFunc); + +//DIRECTORY_MOVE +struct DirectoryMoveFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + std::vector paths = wait(popPaths(data, 2)); + + Reference directory = data->directoryData.directory(); + logOp(format("move %s to %s", pathToString(combinePaths(directory->getPath(), paths[0])).c_str(), pathToString(combinePaths(directory->getPath(), paths[1])).c_str())); + + Reference dirSubspace = wait(executeMutation(instruction, [this, directory, paths] () { + return directory->move(instruction->tr, paths[0], paths[1]); + })); + + data->directoryData.push(dirSubspace); + + return Void(); + } +}; +const char* DirectoryMoveFunc::name = "DIRECTORY_MOVE"; +REGISTER_INSTRUCTION_FUNC(DirectoryMoveFunc); + +//DIRECTORY_MOVE_TO +struct DirectoryMoveToFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + IDirectory::Path path = wait(popPath(data)); + + Reference directory = data->directoryData.directory(); + logOp(format("move %s to %s", pathToString(directory->getPath()).c_str(), pathToString(path).c_str())); + + Reference dirSubspace = wait(executeMutation(instruction, [this, directory, path] () { + return directory->moveTo(instruction->tr, path); + })); + + data->directoryData.push(dirSubspace); + + return Void(); + } +}; +const char* DirectoryMoveToFunc::name = "DIRECTORY_MOVE_TO"; +REGISTER_INSTRUCTION_FUNC(DirectoryMoveToFunc); + +//DIRECTORY_REMOVE +struct DirectoryRemoveFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple count = wait(data->stack.waitAndPop()); + state Reference directory = data->directoryData.directory(); + if(count.getInt(0) == 0) { + logOp(format("remove %s", pathToString(directory->getPath()).c_str())); + + Void _ = wait(executeMutation(instruction, [this] () { + return directory->remove(instruction->tr); + })); + } + else { + IDirectory::Path path = wait(popPath(data)); + logOp(format("remove %s", pathToString(combinePaths(directory->getPath(), path)).c_str())); + + Void _ = wait(executeMutation(instruction, [this, path] () { + return directory->remove(instruction->tr, path); + })); + } + + return Void(); + } +}; +const char* DirectoryRemoveFunc::name = "DIRECTORY_REMOVE"; +REGISTER_INSTRUCTION_FUNC(DirectoryRemoveFunc); + +//DIRECTORY_REMOVE_IF_EXISTS +struct DirectoryRemoveIfExistsFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple count = wait(data->stack.waitAndPop()); + state Reference directory = data->directoryData.directory(); + if(count.getInt(0) == 0) { + logOp(format("remove_if_exists %s", pathToString(directory->getPath()).c_str())); + + bool _ = wait(executeMutation(instruction, [this] () { + return directory->removeIfExists(instruction->tr); + })); + } + else { + IDirectory::Path path = wait(popPath(data)); + logOp(format("remove_if_exists %s", pathToString(combinePaths(directory->getPath(), path)).c_str())); + + bool _ = wait(executeMutation(instruction, [this, path] () { + return directory->removeIfExists(instruction->tr, path); + })); + } + + return Void(); + } +}; +const char* DirectoryRemoveIfExistsFunc::name = "DIRECTORY_REMOVE_IF_EXISTS"; +REGISTER_INSTRUCTION_FUNC(DirectoryRemoveIfExistsFunc); + +//DIRECTORY_LIST +struct DirectoryListFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple count = wait(data->stack.waitAndPop()); + state Reference directory = data->directoryData.directory(); + state Standalone> subdirs; + if(count.getInt(0) == 0) { + logOp(format("list %s", pathToString(directory->getPath()).c_str())); + Standalone> _subdirs = wait(directory->list(instruction->tr)); + subdirs = _subdirs; + } + else { + IDirectory::Path path = wait(popPath(data)); + logOp(format("list %s", pathToString(combinePaths(directory->getPath(), path)).c_str())); + Standalone> _subdirs = wait(directory->list(instruction->tr, path)); + subdirs = _subdirs; + } + + Tuple subdirTuple; + for(auto &sd : subdirs) { + subdirTuple.append(sd, true); + } + + data->stack.pushTuple(subdirTuple.pack()); + return Void(); + } +}; +const char* DirectoryListFunc::name = "DIRECTORY_LIST"; +REGISTER_INSTRUCTION_FUNC(DirectoryListFunc); + +//DIRECTORY_EXISTS +struct DirectoryExistsFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple count = wait(data->stack.waitAndPop()); + state Reference directory = data->directoryData.directory(); + state bool result; + if(count.getInt(0) == 0) { + bool _result = wait(directory->exists(instruction->tr)); + result = _result; + logOp(format("exists %s: %d", pathToString(directory->getPath()).c_str(), result)); + } + else { + state IDirectory::Path path = wait(popPath(data)); + bool _result = wait(directory->exists(instruction->tr, path)); + result = _result; + logOp(format("exists %s: %d", pathToString(combinePaths(directory->getPath(), path)).c_str(), result)); + } + + data->stack.push(Tuple().append(result ? 1 : 0).pack()); + return Void(); + } +}; +const char* DirectoryExistsFunc::name = "DIRECTORY_EXISTS"; +REGISTER_INSTRUCTION_FUNC(DirectoryExistsFunc); + +//DIRECTORY_PACK_KEY +struct DirectoryPackKeyFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple tuple = wait(popTuple(data)); + data->stack.pushTuple(data->directoryData.subspace()->pack(tuple)); + + return Void(); + } +}; +const char* DirectoryPackKeyFunc::name = "DIRECTORY_PACK_KEY"; +REGISTER_INSTRUCTION_FUNC(DirectoryPackKeyFunc); + +//DIRECTORY_UNPACK_KEY +struct DirectoryUnpackKeyFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple key = wait(data->stack.waitAndPop()); + Subspace *subspace = data->directoryData.subspace(); + logOp(format("Unpack %s in subspace with prefix %s", printable(key.getString(0)).c_str(), printable(subspace->key()).c_str())); + Tuple tuple = subspace->unpack(key.getString(0)); + for(int i = 0; i < tuple.size(); ++i) { + data->stack.push(tuple.subTuple(i, i+1).pack()); + } + + return Void(); + } +}; +const char* DirectoryUnpackKeyFunc::name = "DIRECTORY_UNPACK_KEY"; +REGISTER_INSTRUCTION_FUNC(DirectoryUnpackKeyFunc); + +//DIRECTORY_RANGE +struct DirectoryRangeFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple tuple = wait(popTuple(data)); + KeyRange range = data->directoryData.subspace()->range(tuple); + data->stack.pushTuple(range.begin); + data->stack.pushTuple(range.end); + + return Void(); + } +}; +const char* DirectoryRangeFunc::name = "DIRECTORY_RANGE"; +REGISTER_INSTRUCTION_FUNC(DirectoryRangeFunc); + +//DIRECTORY_CONTAINS +struct DirectoryContainsFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple key = wait(data->stack.waitAndPop()); + bool result = data->directoryData.subspace()->contains(key.getString(0)); + data->stack.push(Tuple().append(result ? 1 : 0).pack()); + + return Void(); + } +}; +const char* DirectoryContainsFunc::name = "DIRECTORY_CONTAINS"; +REGISTER_INSTRUCTION_FUNC(DirectoryContainsFunc); + +//DIRECTORY_OPEN_SUBSPACE +struct DirectoryOpenSubspaceFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple tuple = wait(popTuple(data)); + Subspace *subspace = data->directoryData.subspace(); + logOp(format("open_subspace %s (at %s)", tupleToString(tuple).c_str(), printable(subspace->key()).c_str())); + Subspace *child = new Subspace(subspace->subspace(tuple)); + data->directoryData.push(child); + + return Void(); + } +}; +const char* DirectoryOpenSubspaceFunc::name = "DIRECTORY_OPEN_SUBSPACE"; +REGISTER_INSTRUCTION_FUNC(DirectoryOpenSubspaceFunc); + +//DIRECTORY_LOG_SUBSPACE +struct DirectoryLogSubspaceFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple prefix = wait(data->stack.waitAndPop()); + Tuple tuple; + tuple.append(data->directoryData.directoryListIndex); + instruction->tr->set(Subspace(tuple, prefix.getString(0)).key(), data->directoryData.subspace()->key()); + + return Void(); + } +}; +const char* DirectoryLogSubspaceFunc::name = "DIRECTORY_LOG_SUBSPACE"; +REGISTER_INSTRUCTION_FUNC(DirectoryLogSubspaceFunc); + +//DIRECTORY_LOG_DIRECTORY +struct DirectoryLogDirectoryFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state Reference directory = data->directoryData.directory(); + state Tuple prefix = wait(data->stack.waitAndPop()); + state bool exists = wait(directory->exists(instruction->tr)); + + state Tuple childrenTuple; + if(exists) { + Standalone> children = wait(directory->list(instruction->tr)); + for(auto &c : children) { + childrenTuple.append(c, true); + } + } + + Subspace logSubspace(Tuple().append(data->directoryData.directoryListIndex), prefix.getString(0)); + + Tuple pathTuple; + for(auto &p : directory->getPath()) { + pathTuple.append(p, true); + } + + instruction->tr->set(logSubspace.pack(LiteralStringRef("path"), true), pathTuple.pack()); + instruction->tr->set(logSubspace.pack(LiteralStringRef("layer"), true), Tuple().append(directory->getLayer()).pack()); + instruction->tr->set(logSubspace.pack(LiteralStringRef("exists"), true), Tuple().append(exists ? 1 : 0).pack()); + instruction->tr->set(logSubspace.pack(LiteralStringRef("children"), true), childrenTuple.pack()); + + return Void(); + } +}; +const char* DirectoryLogDirectoryFunc::name = "DIRECTORY_LOG_DIRECTORY"; +REGISTER_INSTRUCTION_FUNC(DirectoryLogDirectoryFunc); + +//DIRECTORY_STRIP_PREFIX +struct DirectoryStripPrefixFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Tuple str = wait(data->stack.waitAndPop()); + Subspace *subspace = data->directoryData.subspace(); + ASSERT(str.getString(0).startsWith(subspace->key())); + data->stack.pushTuple(str.getString(0).substr(subspace->key().size())); + return Void(); + } +}; +const char* DirectoryStripPrefixFunc::name = "DIRECTORY_STRIP_PREFIX"; +REGISTER_INSTRUCTION_FUNC(DirectoryStripPrefixFunc); + diff --git a/bindings/flow/tester/Tester.actor.cpp b/bindings/flow/tester/Tester.actor.cpp new file mode 100644 index 0000000000..499a116413 --- /dev/null +++ b/bindings/flow/tester/Tester.actor.cpp @@ -0,0 +1,1588 @@ +/* + * Tester.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fdbrpc/fdbrpc.h" +#include "flow/DeterministicRandom.h" +#include "bindings/flow/Tuple.h" +#include "bindings/flow/FDBLoanerTypes.h" + +#include "Tester.actor.h" + +#ifdef __linux__ +#include +#endif + +// Otherwise we have to type setupNetwork(), Cluster::createCluster(), etc. +using namespace FDB; + +std::map optionInfo; +std::set opsThatCreateDirectories; + +std::map, Reference> trMap; + +// NOTE: This was taken from within fdb_c.cpp (where it is defined as a static within the get_range function). +// If that changes, this will also have to be changed. +const int ITERATION_PROGRESSION[] = { 256, 1000, 4096, 6144, 9216, 13824, 20736, 31104, 46656, 69984, 80000 }; +const int MAX_ITERATION = sizeof(ITERATION_PROGRESSION)/sizeof(int); + +static Future runTest(Reference const& data, Reference const& db, StringRef const& prefix); + +THREAD_FUNC networkThread( void* api ) { + // This is the fdb_flow network we're running on a thread + ((API*)api)->runNetwork(); + THREAD_RETURN; +} + +bool hasEnding(std::string const &fullString, std::string const &ending) +{ + if (fullString.length() >= ending.length()) { + return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending)); + } + else { + return false; + } +} + +ACTOR Future> waitAndPop(FlowTesterStack *self, int count) { + state std::vector tuples; + state std::vector items = self->pop(count); + + state int index; + for(index = 0; index < items.size(); ++index) { + Standalone itemStr = wait(items[index].value); + tuples.push_back(Tuple::unpack(itemStr)); + } + + return tuples; +} + +Future> FlowTesterStack::waitAndPop(int count) { + return ::waitAndPop(this, count); +} + +ACTOR Future waitAndPop(FlowTesterStack *self) { + std::vector tuples = wait(waitAndPop(self, 1)); + return tuples[0]; +} + +Future FlowTesterStack::waitAndPop() { + return ::waitAndPop(this); +} + +std::string tupleToString(Tuple const& tuple) { + std::string str = "("; + for(int i = 0; i < tuple.size(); ++i) { + Tuple::ElementType type = tuple.getType(i); + if(type == Tuple::NULL_TYPE) { + str += "NULL"; + } + else if(type == Tuple::BYTES || type == Tuple::UTF8) { + if(type == Tuple::UTF8) { + str += "u"; + } + str += "\'" + printable(tuple.getString(i)) + "\'"; + } + else if(type == Tuple::INT) { + str += format("%ld", tuple.getInt(i)); + } + else { + ASSERT(false); + } + + if(i < tuple.size() - 1) { + str += ", "; + } + } + + str += ")"; + return str; +} + +ACTOR Future< Standalone > getRange(Reference tr, KeySelectorRef begin, KeySelectorRef end, int limits = 0, bool snapshot = false, bool reverse = false, FDBStreamingMode streamingMode = FDB_STREAMING_MODE_SERIAL) { + state KeySelector ks_begin(begin); + state KeySelector ks_end(end); + state Standalone results; + state int iteration = 1; + loop{ + // printf("=====DB: begin:%s, end:%s, limits:%d\n", printable(begin.key).c_str(), printable(end.key).c_str(), limits); + state FDBStandalone r; + if (streamingMode == FDB_STREAMING_MODE_ITERATOR && iteration > 1) { + int effective_iteration = std::min(iteration, MAX_ITERATION); + int bytes_limit = ITERATION_PROGRESSION[effective_iteration - 1]; + FDBStandalone rTemp = wait(tr->getRange(ks_begin, ks_end, GetRangeLimits(limits, bytes_limit), snapshot, reverse, (FDBStreamingMode)FDB_STREAMING_MODE_EXACT)); + r = rTemp; + } else { + FDBStandalone rTemp = wait(tr->getRange(ks_begin, ks_end, limits, snapshot, reverse, streamingMode)); + r = rTemp; + } + iteration += 1; + // printf("=====DB: count:%d\n", r.size()); + for (auto & s : r) { + // printf("=====key:%s, value:%s\n", printable(StringRef(s.key)).c_str(), printable(StringRef(s.value)).c_str()); + results.push_back_deep(results.arena(), s); + + if (reverse) + ks_end = KeySelector(firstGreaterOrEqual(s.key)); + else + ks_begin = KeySelector(firstGreaterThan(s.key)); + } + + ASSERT(limits == 0 || limits >= r.size()); + + if (!r.more || (limits > 0 && limits == r.size())) { + return results; + } + + if(limits > 0) { + limits -= r.size(); + } + } +} + +ACTOR Future< Standalone > getRange(Reference tr, KeyRange keys, int limits = 0, bool snapshot = false, bool reverse = false, FDBStreamingMode streamingMode = FDB_STREAMING_MODE_SERIAL) { + state Key begin(keys.begin); + state Key end(keys.end); + state Standalone results; + state int iteration = 1; + loop{ + // printf("=====DB: begin:%s, limits:%d\n", printable(begin).c_str(), limits); + KeyRange keyRange(KeyRangeRef(begin, end > begin ? end : begin)); + state FDBStandalone r; + if (streamingMode == FDB_STREAMING_MODE_ITERATOR && iteration > 1) { + int effective_iteration = std::min(iteration, MAX_ITERATION); + int bytes_limit = ITERATION_PROGRESSION[effective_iteration - 1]; + FDBStandalone rTemp = wait(tr->getRange(keyRange, GetRangeLimits(limits, bytes_limit), snapshot, reverse, (FDBStreamingMode)FDB_STREAMING_MODE_EXACT)); + r = rTemp; + } else { + FDBStandalone rTemp = wait(tr->getRange(keyRange, limits, snapshot, reverse, streamingMode)); + r = rTemp; + } + iteration += 1; + // printf("=====DB: count:%d\n", r.size()); + for (auto & s : r) { + // printf("=====key:%s, value:%s\n", printable(StringRef(s.key)).c_str(), printable(StringRef(s.value)).c_str()); + results.push_back_deep(results.arena(), s); + + if (reverse) + end = s.key; + else + begin = keyAfter(s.key); + } + + ASSERT(limits == 0 || limits >= r.size()); + + if (!r.more || (limits > 0 && limits == r.size())) { + return results; + } + + if(limits > 0) { + limits -= r.size(); + } + } +} + +ACTOR static Future debugPrintRange(Reference tr, std::string subspace, std::string msg) { + if (!tr) + return Void(); + + Standalone results = wait(getRange(tr, KeyRange(KeyRangeRef(subspace + '\x00', subspace + '\xff')))); + // printf("==================================================DB:%s:%s, count:%d\n", msg.c_str(), printable(subspace).c_str(), results.size()); + for (auto & s : results) { + // printf("=====key:%s, value:%s\n", printable(StringRef(s.key)).c_str(), printable(StringRef(s.value)).c_str()); + } + + return Void(); +} + +ACTOR Future stackSub(FlowTesterStack* stack) { + if (stack->data.size() < 2) + return Void(); + + StackItem a = stack->data.back(); + stack->data.pop_back(); + state Standalone sa = wait(a.value); + + StackItem b = stack->data.back(); + stack->data.pop_back(); + Standalone sb = wait(b.value); + + int64_t c = Tuple::unpack(sa).getInt(0) - Tuple::unpack(sb).getInt(0); + Tuple f; + f.append(c); + stack->push(f.pack()); + + return Void(); +} + +ACTOR Future stackConcat(FlowTesterStack* stack) { + if(stack->data.size() < 2) + return Void(); + + StackItem a = stack->data.back(); + stack->data.pop_back(); + state Standalone sa = wait(a.value); + state Tuple ta = Tuple::unpack(sa); + + StackItem b = stack->data.back(); + stack->data.pop_back(); + Standalone sb = wait(b.value); + state Tuple tb = Tuple::unpack(sb); + + ASSERT(ta.getType(0) == tb.getType(0)); + stack->pushTuple(tb.getString(0).withPrefix(ta.getString(0)), ta.getType(0) == Tuple::ElementType::UTF8); + + return Void(); +} + +ACTOR Future stackSwap(FlowTesterStack* stack) { + if (stack->data.size() < 3) + return Void(); + + StackItem pop = stack->data.back(); + stack->data.pop_back(); + + Standalone sv = wait(pop.value); + int64_t idx = stack->data.size() - 1; + int64_t idx1 = idx - Tuple::unpack(sv).getInt(0); + if (idx1 < idx) { + //printf("=============SWAP:%d,%d\n", idx, stack->data.size()); + StackItem item = stack->data[idx]; + stack->data[idx] = stack->data[idx1]; + stack->data[idx1] = item; + } + return Void(); +} + +ACTOR Future printFlowTesterStack(FlowTesterStack* stack) { + // printf("====================stack item count:%ld\n", stack->data.size()); + state int idx; + for (idx = stack->data.size() - 1; idx >= 0; --idx) { + Standalone value = wait(stack->data[idx].value); + // printf("==========stack item:%d, index:%d, value:%s\n", idx, stack->data[idx].index, printable(value).c_str()); + } + return Void(); +} + +// +// Data Operations +// + +struct PushFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + Tuple t = Tuple::unpack(instruction->instruction); + Standalone param = t.subTuple(1).pack(); + data->stack.push(param); + return Void(); + } +}; +const char* PushFunc::name = "PUSH"; +REGISTER_INSTRUCTION_FUNC(PushFunc); + +struct DupFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + data->stack.dup(); + return Void(); + } +}; +const char* DupFunc::name = "DUP"; +REGISTER_INSTRUCTION_FUNC(DupFunc); + +struct EmptyStackFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + //Void _ = wait(printFlowTesterStack(&(data->stack))); + //Void _ = wait(debugPrintRange(instruction->tr, "\x01test_results", "")); + data->stack.clear(); + return Void(); + } +}; +const char* EmptyStackFunc::name = "EMPTY_STACK"; +REGISTER_INSTRUCTION_FUNC(EmptyStackFunc); + +struct SwapFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Void _ = wait(stackSwap(&(data->stack))); + return Void(); + } +}; +const char* SwapFunc::name = "SWAP"; +REGISTER_INSTRUCTION_FUNC(SwapFunc); + +struct PopFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + for(StackItem item : items) { + Standalone _ = wait(item.value); + } + return Void(); + } +}; +const char* PopFunc::name = "POP"; +REGISTER_INSTRUCTION_FUNC(PopFunc); + +struct SubFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Void _ = wait(stackSub(&(data->stack))); + return Void(); + } +}; +const char* SubFunc::name = "SUB"; +REGISTER_INSTRUCTION_FUNC(SubFunc); + +struct ConcatFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Void _ = wait(stackConcat(&(data->stack))); + return Void(); + } +}; +const char* ConcatFunc::name = "CONCAT"; +REGISTER_INSTRUCTION_FUNC(ConcatFunc); + +struct LogStackFunc : InstructionFunc { + static const char* name; + + ACTOR static Future logStack(Reference data, std::map entries, Standalone prefix) { + loop { + state Reference tr(new Transaction(data->db)); + try { + for(auto it : entries) { + Tuple tk; + tk.append(it.first); + tk.append(it.second.index); + state Standalone pk = tk.pack().withPrefix(prefix); + Standalone pv = wait(it.second.value); + tr->set(pk, pv.substr(0, std::min(pv.size(), 40000))); + } + + Void _ = wait(tr->commit()); + return Void(); + } + catch(Error &e) { + Void _ = wait(tr->onError(e)); + } + } + } + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.empty()) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone prefix = Tuple::unpack(s1).getString(0); + + state std::map entries; + while(data->stack.data.size() > 0) { + state std::vector it = data->stack.pop(); + ASSERT(it.size() == 1); + entries[data->stack.data.size()] = it.front(); + if(entries.size() == 100) { + Void _ = wait(logStack(data, entries, prefix)); + entries.clear(); + } + + Void _ = wait(logStack(data, entries, prefix)); + } + + return Void(); + } +}; +const char* LogStackFunc::name = "LOG_STACK"; +REGISTER_INSTRUCTION_FUNC(LogStackFunc); + +// +// FoundationDB Operations +// +ACTOR Future> waitForVoid(Future f) { + try{ + Void _ = wait(f); + Tuple t; + t.append(LiteralStringRef("RESULT_NOT_PRESENT")); + return t.pack(); + } + catch (Error& e){ + // printf("FDBError1:%d\n", e.code()); + Tuple t; + t.append(LiteralStringRef("ERROR")); + t.append(format("%d", e.code())); + // pack above as error string into another tuple + Tuple ret; + ret.append(t.pack()); + return ret.pack(); + } +} + +ACTOR Future> waitForValue(Future> f) { + try{ + FDBStandalone value = wait(f); + Tuple t; + t.append(value); + return t.pack(); + } + catch (Error& e){ + // printf("FDBError2:%d\n", e.code()); + Tuple t; + t.append(LiteralStringRef("ERROR")); + t.append(format("%d", e.code())); + // pack above as error string into another tuple + Tuple ret; + ret.append(t.pack()); + return ret.pack(); + } +} + +ACTOR Future> waitForValue(Future< Optional> > f) { + try{ + Optional> value = wait(f); + Standalone str; + if (value.present()) + str = value.get(); + else + str = LiteralStringRef("RESULT_NOT_PRESENT"); + + Tuple t; + t.append(str); + return t.pack(); + } + catch (Error& e){ + // printf("FDBError3:%d\n", e.code()); + Tuple t; + t.append(LiteralStringRef("ERROR")); + t.append(format("%d", e.code())); + // pack above as error string into another tuple + Tuple ret; + ret.append(t.pack()); + return ret.pack(); + } +} + +ACTOR Future> getKey(Future> f, Standalone prefixFilter) { + try { + FDBStandalone key = wait(f); + Tuple t; + + if(key.startsWith(prefixFilter)) { + t.append(key); + } + else if(key < prefixFilter) { + t.append(prefixFilter); + } + else { + t.append(strinc(prefixFilter)); + } + + return t.pack(); + } + catch(Error& e){ + // printf("FDBError4:%d\n", e.code()); + Tuple t; + t.append(LiteralStringRef("ERROR")); + t.append(format("%d", e.code())); + // pack above as error string into another tuple + Tuple ret; + ret.append(t.pack()); + return ret.pack(); + } +} + +struct NewTransactionFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + trMap[data->trName] = Reference(new Transaction(data->db)); + return Void(); + } +}; +const char* NewTransactionFunc::name = "NEW_TRANSACTION"; +REGISTER_INSTRUCTION_FUNC(NewTransactionFunc); + +struct UseTransactionFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + Standalone name = wait(items[0].value); + data->trName = name; + + if(trMap.count(data->trName) == 0) { + trMap[data->trName] = Reference(new Transaction(data->db)); + } + return Void(); + } +}; +const char* UseTransactionFunc::name = "USE_TRANSACTION"; +REGISTER_INSTRUCTION_FUNC(UseTransactionFunc); + +struct OnErrorFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.empty()) + return Void(); + + Standalone value = wait(items[0].value); + int err_code = Tuple::unpack(value).getInt(0); + // printf("OnError:%d:%d:%s\n", err_code, items[0].index, printable(value).c_str()); + + data->stack.push(waitForVoid(instruction->tr->onError(Error(err_code)))); + return Void(); + } +}; +const char* OnErrorFunc::name = "ON_ERROR"; +REGISTER_INSTRUCTION_FUNC(OnErrorFunc); + +struct SetFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(2); + if (items.size() != 2) + return Void(); + + Standalone sk = wait(items[0].value); + state Standalone key = Tuple::unpack(sk).getString(0); + // if (instruction->isDatabase) + // printf("SetDatabase:%s, isDatabase:%d\n", printable(key).c_str(), instruction->isDatabase); + Standalone sv = wait(items[1].value); + Standalone value = Tuple::unpack(sv).getString(0); + //printf("SetDatabase:%s:%s:%s\n", printable(key).c_str(), printable(sv).c_str(), printable(value).c_str()); + + Reference instructionCopy = instruction; + Standalone keyCopy = key; + + Future mutation = executeMutation(instruction, [instructionCopy, keyCopy, value] () -> Future { + instructionCopy->tr->set(keyCopy, value); + return Void(); + }); + + if (instruction->isDatabase) { + data->stack.push(waitForVoid(mutation)); + } + else { + Void _ = wait(mutation); + } + + return Void(); + } +}; +const char* SetFunc::name = "SET"; +REGISTER_INSTRUCTION_FUNC(SetFunc); + +struct GetFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone sk = wait(items[0].value); + state Standalone key = Tuple::unpack(sk).getString(0); + + Future< Optional> > fk = instruction->tr->get(StringRef(key), instruction->isSnapshot); + data->stack.push(waitForValue(holdWhile(instruction->tr, fk))); + + return Void(); + } +}; +const char* GetFunc::name = "GET"; +REGISTER_INSTRUCTION_FUNC(GetFunc); + +struct GetKeyFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(4); + if (items.size() != 4) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone key = Tuple::unpack(s1).getString(0); + + Standalone s2 = wait(items[1].value); + state int64_t or_equal = Tuple::unpack(s2).getInt(0); + + Standalone s3 = wait(items[2].value); + state int64_t offset = Tuple::unpack(s3).getInt(0); + + Standalone s4 = wait(items[3].value); + Standalone prefix = Tuple::unpack(s4).getString(0); + + //printf("===================GET_KEY:%s, %ld, %ld\n", printable(key).c_str(), or_equal, offset); + Future> fk = instruction->tr->getKey(KeySelector(KeySelectorRef(key, or_equal, offset)), instruction->isSnapshot); + data->stack.push(getKey(holdWhile(instruction->tr, fk), prefix)); + + return Void(); + } +}; +const char* GetKeyFunc::name = "GET_KEY"; +REGISTER_INSTRUCTION_FUNC(GetKeyFunc); + +struct GetReadVersionFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + Version v = wait(instruction->tr->getReadVersion()); + data->lastVersion = v; + data->stack.pushTuple(LiteralStringRef("GOT_READ_VERSION")); + return Void(); + } +}; +const char* GetReadVersionFunc::name = "GET_READ_VERSION"; +REGISTER_INSTRUCTION_FUNC(GetReadVersionFunc); + +struct SetReadVersionFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + instruction->tr->setVersion(data->lastVersion); + return Void(); + } +}; +const char* SetReadVersionFunc::name = "SET_READ_VERSION"; +REGISTER_INSTRUCTION_FUNC(SetReadVersionFunc); + +// GET_COMMITTED_VERSION +struct GetCommittedVersionFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + data->lastVersion = instruction->tr->getCommittedVersion(); + data->stack.pushTuple(LiteralStringRef("GOT_COMMITTED_VERSION")); + return Void(); + } +}; +const char* GetCommittedVersionFunc::name = "GET_COMMITTED_VERSION"; +REGISTER_INSTRUCTION_FUNC(GetCommittedVersionFunc); + +// GET_VERSIONSTAMP +struct GetVersionstampFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + data->stack.push(waitForValue(instruction->tr->getVersionstamp())); + return Void(); + } +}; +const char* GetVersionstampFunc::name = "GET_VERSIONSTAMP"; +REGISTER_INSTRUCTION_FUNC(GetVersionstampFunc); + +// COMMIT +struct CommitFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + data->stack.push(waitForVoid(holdWhile(instruction->tr, instruction->tr->commit()))); + return Void(); + } +}; +const char* CommitFunc::name = "COMMIT"; +REGISTER_INSTRUCTION_FUNC(CommitFunc); + +// WAIT_FUTURE +struct WaitFutureFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone sk = wait(items[0].value); + data->stack.push(StackItem(items[0].index, sk)); + return Void(); + } +}; +const char* WaitFutureFunc::name = "WAIT_FUTURE"; +REGISTER_INSTRUCTION_FUNC(WaitFutureFunc); + +// CLEAR +struct ClearFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone sk = wait(items[0].value); + Standalone key = Tuple::unpack(sk).getString(0); + + Reference instructionCopy = instruction; + + Future mutation = executeMutation(instruction, [instructionCopy, key] () -> Future { + instructionCopy->tr->clear(key); + return Void(); + }); + + if (instruction->isDatabase) { + data->stack.push(waitForVoid(mutation)); + } + else { + Void _ = wait(mutation); + } + + return Void(); + } +}; +const char* ClearFunc::name = "CLEAR"; +REGISTER_INSTRUCTION_FUNC(ClearFunc); + +// RESET +struct ResetFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + instruction->tr->reset(); + return Void(); + } +}; +const char* ResetFunc::name = "RESET"; +REGISTER_INSTRUCTION_FUNC(ResetFunc); + +// CANCEL +struct CancelFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + instruction->tr->cancel(); + return Void(); + } +}; +const char* CancelFunc::name = "CANCEL"; +REGISTER_INSTRUCTION_FUNC(CancelFunc); + +struct GetRangeFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(5); + if (items.size() != 5) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone begin = Tuple::unpack(s1).getString(0); + + Standalone s2 = wait(items[1].value); + state Standalone end = Tuple::unpack(s2).getString(0); + + Standalone s3 = wait(items[2].value); + state int limit = Tuple::unpack(s3).getInt(0); + + Standalone s4 = wait(items[3].value); + state int reverse = Tuple::unpack(s4).getInt(0); + + Standalone s5 = wait(items[4].value); + FDBStreamingMode mode = (FDBStreamingMode)Tuple::unpack(s5).getInt(0); + + // printf("================GetRange: %s, %s, %d, %d, %d, %d\n", printable(begin).c_str(), printable(end).c_str(), limit, reverse, mode, instruction->isSnapshot); + + Standalone results = wait(getRange(instruction->tr, KeyRange(KeyRangeRef(begin, end > begin ? end : begin)), limit, instruction->isSnapshot, reverse, mode)); + Tuple t; + for (auto & s : results) { + t.append(s.key); + t.append(s.value); + //printf("=====key:%s, value:%s\n", printable(StringRef(s.key)).c_str(), printable(StringRef(s.value)).c_str()); + } + //printf("=====Results Count:%d, size:%d\n", results.size(), str.size()); + + data->stack.push(Tuple().append(t.pack()).pack()); + return Void(); + } +}; +const char* GetRangeFunc::name = "GET_RANGE"; +REGISTER_INSTRUCTION_FUNC(GetRangeFunc); + +struct GetRangeStartsWithFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(4); + if (items.size() != 4) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone prefix = Tuple::unpack(s1).getString(0); + + Standalone s2 = wait(items[1].value); + state int limit = Tuple::unpack(s2).getInt(0); + + Standalone s3 = wait(items[2].value); + state int reverse = Tuple::unpack(s3).getInt(0); + + Standalone s4 = wait(items[3].value); + FDBStreamingMode mode = (FDBStreamingMode)Tuple::unpack(s4).getInt(0); + + //printf("================GetRangeStartsWithFunc: %s, %d, %d, %d, %d\n", printable(prefix).c_str(), limit, reverse, mode, isSnapshot); + Standalone results = wait(getRange(instruction->tr, KeyRange(KeyRangeRef(prefix, strinc(prefix))), limit, instruction->isSnapshot, reverse, mode)); + Tuple t; + //printf("=====Results Count:%d\n", results.size()); + for (auto & s : results) { + t.append(s.key); + t.append(s.value); + //printf("=====key:%s, value:%s\n", printable(StringRef(s.key)).c_str(), printable(StringRef(s.value)).c_str()); + } + + data->stack.push(Tuple().append(t.pack()).pack()); + return Void(); + } +}; +const char* GetRangeStartsWithFunc::name = "GET_RANGE_STARTS_WITH"; +REGISTER_INSTRUCTION_FUNC(GetRangeStartsWithFunc); + +struct ClearRangeFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(2); + if (items.size() != 2) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone begin = Tuple::unpack(s1).getString(0); + + Standalone s2 = wait(items[1].value); + Standalone end = Tuple::unpack(s2).getString(0); + + Reference instructionCopy = instruction; + Standalone beginCopy = begin; + + Future mutation = executeMutation(instruction, [instructionCopy, beginCopy, end] () -> Future { + instructionCopy->tr->clear(KeyRangeRef(beginCopy, end)); + return Void(); + }); + + if (instruction->isDatabase) { + data->stack.push(waitForVoid(mutation)); + } + else { + Void _ = wait(mutation); + } + + return Void(); + } +}; +const char* ClearRangeFunc::name = "CLEAR_RANGE"; +REGISTER_INSTRUCTION_FUNC(ClearRangeFunc); + +struct ClearRangeStartWithFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone s1 = wait(items[0].value); + Standalone begin = Tuple::unpack(s1).getString(0); + + Reference instructionCopy = instruction; + + Future mutation = executeMutation(instruction, [instructionCopy, begin] () -> Future { + instructionCopy->tr->clear(KeyRangeRef(begin, strinc(begin))); + return Void(); + }); + + if (instruction->isDatabase) { + data->stack.push(waitForVoid(mutation)); + } + else { + Void _ = wait(mutation); + } + + return Void(); + } +}; +const char* ClearRangeStartWithFunc::name = "CLEAR_RANGE_STARTS_WITH"; +REGISTER_INSTRUCTION_FUNC(ClearRangeStartWithFunc); + +struct GetRangeSelectorFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(10); + if (items.size() != 10) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone begin = Tuple::unpack(s1).getString(0); + + Standalone s2 = wait(items[1].value); + state bool begin_or_equal = Tuple::unpack(s2).getInt(0); + + Standalone s3 = wait(items[2].value); + state int64_t begin_offset = Tuple::unpack(s3).getInt(0); + + Standalone s4 = wait(items[3].value); + state Standalone end = Tuple::unpack(s4).getString(0); + + Standalone s5 = wait(items[4].value); + state bool end_or_equal = Tuple::unpack(s5).getInt(0); + + Standalone s6 = wait(items[5].value); + state int64_t end_offset = Tuple::unpack(s6).getInt(0); + + Standalone s7 = wait(items[6].value); + state int limit = Tuple::unpack(s7).getInt(0); + + Standalone s8 = wait(items[7].value); + state int reverse = Tuple::unpack(s8).getInt(0); + + Standalone s9 = wait(items[8].value); + state FDBStreamingMode mode = (FDBStreamingMode) Tuple::unpack(s9).getInt(0); + + Standalone s10 = wait(items[9].value); + state Optional> prefix; + Tuple t10 = Tuple::unpack(s10); + if(t10.getType(0) != Tuple::ElementType::NULL_TYPE) { + prefix = t10.getString(0); + } + + //printf("================GetRangeSelectorFunc: %s, %d, %ld, %s, %d, %ld, %d, %d, %d, %d, %s\n", printable(begin).c_str(), begin_or_equal, begin_offset, + // printable(end).c_str(), end_or_equal, end_offset, + // limit, reverse, mode, instruction->isSnapshot, printable(prefix).c_str()); + Future> f = getRange(instruction->tr, KeySelectorRef(begin, begin_or_equal, begin_offset), KeySelectorRef(end, end_or_equal, end_offset), limit, instruction->isSnapshot, reverse, mode); + Standalone results = wait(holdWhile(instruction->tr, f)); + Tuple t; + //printf("=====Results Count:%d\n", results.size()); + for (auto & s : results) { + if(!prefix.present() || s.key.startsWith(prefix.get())) { + t.append(s.key); + t.append(s.value); + //printf("=====key:%s, value:%s\n", printable(StringRef(s.key)).c_str(), printable(StringRef(s.value)).c_str()); + } + } + + data->stack.push(Tuple().append(t.pack()).pack()); + return Void(); + } +}; +const char* GetRangeSelectorFunc::name = "GET_RANGE_SELECTOR"; +REGISTER_INSTRUCTION_FUNC(GetRangeSelectorFunc); + +// Tuple Operations +// TUPLE_PACK +struct TuplePackFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone s1 = wait(items[0].value); + state int64_t count = Tuple::unpack(s1).getInt(0); + + state std::vector items1 = data->stack.pop(count); + if (items1.size() != count) + return Void(); + + state Tuple tuple; + state int i = 0; + for (; i < items1.size(); ++i) { + Standalone str = wait(items1[i].value); + Tuple itemTuple = Tuple::unpack(str); + if(g_random->coinflip()) { + Tuple::ElementType type = itemTuple.getType(0); + if(type == Tuple::NULL_TYPE) { + tuple.appendNull(); + } + else if(type == Tuple::INT) { + tuple << itemTuple.getInt(0); + } + else if(type == Tuple::BYTES) { + tuple.append(itemTuple.getString(0), false); + } + else if(type == Tuple::UTF8) { + tuple.append(itemTuple.getString(0), true); + } + else { + ASSERT(false); + } + } + else { + tuple << itemTuple; + } + } + + data->stack.pushTuple(tuple.pack()); + return Void(); + } +}; +const char* TuplePackFunc::name = "TUPLE_PACK"; +REGISTER_INSTRUCTION_FUNC(TuplePackFunc); + +// TUPLE_UNPACK +struct TupleUnpackFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone s1 = wait(items[0].value); + Tuple t = Tuple::unpack(Tuple::unpack(s1).getString(0)); + + for (int i = 0; i < t.size(); ++i) { + Standalone str = t.subTuple(i, i + 1).pack(); + //printf("=====value:%s\n", printable(str).c_str()); + data->stack.pushTuple(str); + } + return Void(); + } +}; +const char* TupleUnpackFunc::name = "TUPLE_UNPACK"; +REGISTER_INSTRUCTION_FUNC(TupleUnpackFunc); + +// TUPLE_RANGE +struct TupleRangeFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone s1 = wait(items[0].value); + state int64_t count = Tuple::unpack(s1).getInt(0); + + state std::vector items1 = data->stack.pop(count); + if (items1.size() != count) + return Void(); + + state Tuple tuple; + state int i = 0; + for (; i < items1.size(); ++i) { + Standalone str = wait(items1[i].value); + Tuple itemTuple = Tuple::unpack(str); + if(g_random->coinflip()) { + Tuple::ElementType type = itemTuple.getType(0); + if(type == Tuple::NULL_TYPE) { + tuple.appendNull(); + } + else if(type == Tuple::INT) { + tuple << itemTuple.getInt(0); + } + else if(type == Tuple::BYTES) { + tuple.append(itemTuple.getString(0), false); + } + else if(type == Tuple::UTF8) { + tuple.append(itemTuple.getString(0), true); + } + else { + ASSERT(false); + } + } + else { + tuple << itemTuple; + } + } + + KeyRange range = tuple.range(); + + data->stack.pushTuple(range.begin); + data->stack.pushTuple(range.end); + return Void(); + } +}; +const char* TupleRangeFunc::name = "TUPLE_RANGE"; +REGISTER_INSTRUCTION_FUNC(TupleRangeFunc); + +// Thread Operations +// START_THREAD +struct StartThreadFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone prefix = Tuple::unpack(s1).getString(0); + // printf("=========START_THREAD:%s\n", printable(prefix).c_str()); + + Reference newData = Reference(new FlowTesterData(data->api)); + data->subThreads.push_back(runTest(newData, data->db, prefix)); + + return Void(); + } +}; +const char* StartThreadFunc::name = "START_THREAD"; +REGISTER_INSTRUCTION_FUNC(StartThreadFunc); + +// WAIT_EMPTY +struct WaitEmptyFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone prefix = Tuple::unpack(s1).getString(0); + // printf("=========WAIT_EMPTY:%s\n", printable(prefix).c_str()); + + state Reference tr(new Transaction(data->db)); + loop { + try { + FDBStandalone results = wait(tr->getRange(KeyRangeRef(prefix, strinc(prefix)), 1)); + if(results.size() > 0) { + throw not_committed(); + } + break; + } + catch(Error &e) { + Void _ = wait(tr->onError(e)); + } + } + + return Void(); + } +}; +const char* WaitEmptyFunc::name = "WAIT_EMPTY"; +REGISTER_INSTRUCTION_FUNC(WaitEmptyFunc); + +// DISABLE_WRITE_CONFLICT +struct DisableWriteConflictFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + if (instruction->tr) { + instruction->tr->setOption(FDBTransactionOption::FDB_TR_OPTION_NEXT_WRITE_NO_WRITE_CONFLICT_RANGE); + } + return Void(); + } +}; +const char* DisableWriteConflictFunc::name = "DISABLE_WRITE_CONFLICT"; +REGISTER_INSTRUCTION_FUNC(DisableWriteConflictFunc); + +// READ_CONFLICT_KEY +struct ReadConflictKeyFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone key = Tuple::unpack(s1).getString(0); + // printf("=========READ_CONFLICT_KEY:%s\n", printable(key).c_str()); + instruction->tr->addReadConflictKey(key); + + data->stack.pushTuple(LiteralStringRef("SET_CONFLICT_KEY")); + return Void(); + } +}; +const char* ReadConflictKeyFunc::name = "READ_CONFLICT_KEY"; +REGISTER_INSTRUCTION_FUNC(ReadConflictKeyFunc); + +// WRITE_CONFLICT_KEY +struct WriteConflictKeyFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(); + if (items.size() != 1) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone key = Tuple::unpack(s1).getString(0); + // printf("=========WRITE_CONFLICT_KEY:%s\n", printable(key).c_str()); + instruction->tr->addWriteConflictKey(key); + + data->stack.pushTuple(LiteralStringRef("SET_CONFLICT_KEY")); + return Void(); + } +}; +const char* WriteConflictKeyFunc::name = "WRITE_CONFLICT_KEY"; +REGISTER_INSTRUCTION_FUNC(WriteConflictKeyFunc); + +// READ_CONFLICT_RANGE +struct ReadConflictRangeFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(2); + if (items.size() != 2) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone begin = Tuple::unpack(s1).getString(0); + Standalone s2 = wait(items[1].value); + state Standalone end = Tuple::unpack(s2).getString(0); + + // printf("=========READ_CONFLICT_RANGE:%s:%s\n", printable(begin).c_str(), printable(end).c_str()); + instruction->tr->addReadConflictRange(KeyRange(KeyRangeRef(begin, end))); + data->stack.pushTuple(LiteralStringRef("SET_CONFLICT_RANGE")); + return Void(); + } +}; +const char* ReadConflictRangeFunc::name = "READ_CONFLICT_RANGE"; +REGISTER_INSTRUCTION_FUNC(ReadConflictRangeFunc); + +// WRITE_CONFLICT_RANGE +struct WriteConflictRangeFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(2); + if (items.size() != 2) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone begin = Tuple::unpack(s1).getString(0); + Standalone s2 = wait(items[1].value); + state Standalone end = Tuple::unpack(s2).getString(0); + + // printf("=========WRITE_CONFLICT_RANGE:%s:%s\n", printable(begin).c_str(), printable(end).c_str()); + instruction->tr->addWriteConflictRange(KeyRange(KeyRangeRef(begin, end))); + + data->stack.pushTuple(LiteralStringRef("SET_CONFLICT_RANGE")); + return Void(); + } +}; +const char* WriteConflictRangeFunc::name = "WRITE_CONFLICT_RANGE"; +REGISTER_INSTRUCTION_FUNC(WriteConflictRangeFunc); + +// ATOMIC_OP +struct AtomicOPFunc : InstructionFunc { + static const char* name; + + ACTOR static Future call(Reference data, Reference instruction) { + state std::vector items = data->stack.pop(3); + if (items.size() != 3) + return Void(); + + Standalone s1 = wait(items[0].value); + state Standalone op = Tuple::unpack(s1).getString(0); + Standalone s2 = wait(items[1].value); + state Standalone key = Tuple::unpack(s2).getString(0); + Standalone s3 = wait(items[2].value); + state Standalone value = Tuple::unpack(s3).getString(0); + + ASSERT(optionInfo.find(op.toString()) != optionInfo.end()); + + FDBMutationType atomicOp = optionInfo[op.toString()]; + + Reference instructionCopy = instruction; + Standalone keyCopy = key; + Standalone valueCopy = value; + + // printf("=========ATOMIC_OP:%s:%s:%s\n", printable(op).c_str(), printable(key).c_str(), printable(value).c_str()); + Future mutation = executeMutation(instruction, [instructionCopy, keyCopy, valueCopy, atomicOp] () -> Future { + instructionCopy->tr->atomicOp(keyCopy, valueCopy, atomicOp); + return Void(); + }); + + if (instruction->isDatabase) { + data->stack.push(waitForVoid(mutation)); + } + else { + Void _ = wait(mutation); + } + + return Void(); + } +}; +const char* AtomicOPFunc::name = "ATOMIC_OP"; +REGISTER_INSTRUCTION_FUNC(AtomicOPFunc); + +// UNIT_TESTS +struct UnitTestsFunc : InstructionFunc { + static const char* name; + + static Future call(Reference const& data, Reference const& instruction) { + ASSERT(data->api->evaluatePredicate(FDBErrorPredicate::FDB_ERROR_PREDICATE_RETRYABLE, Error(1020))); + ASSERT(!data->api->evaluatePredicate(FDBErrorPredicate::FDB_ERROR_PREDICATE_RETRYABLE, Error(10))); + return Void(); + } +}; +const char* UnitTestsFunc::name = "UNIT_TESTS"; +REGISTER_INSTRUCTION_FUNC(UnitTestsFunc); + +ACTOR static Future getInstructions(Reference data, StringRef prefix) { + state Reference tr(new Transaction(data->db)); + + // get test instructions + state Tuple testSpec; + testSpec.append(prefix); + loop { + try { + Standalone results = wait(getRange(tr, testSpec.range())); + data->instructions = results; + return Void(); + } + catch(Error &e) { + Void _ = wait(tr->onError(e)); + } + } +} + +ACTOR static Future doInstructions(Reference data) { + // printf("Total num instructions:%d\n", data->instructions.size()); + state size_t idx = 0; + for (; idx < data->instructions.size(); ++idx) { + Tuple opTuple = Tuple::unpack(data->instructions[idx].value); + state Standalone op = opTuple.getString(0); + + state bool isDatabase = op.endsWith(LiteralStringRef("_DATABASE")); + state bool isSnapshot = op.endsWith(LiteralStringRef("_SNAPSHOT")); + state bool isDirectory = op.startsWith(LiteralStringRef("DIRECTORY_")); + + try { + if(LOG_INSTRUCTIONS) { + if(op != LiteralStringRef("SWAP") && op != LiteralStringRef("PUSH")) { + printf("%lu. %s\n", idx, tupleToString(opTuple).c_str()); + fflush(stdout); + } + } + + if (isDatabase) + op = op.substr(0, op.size() - 9); + else if (isSnapshot) + op = op.substr(0, op.size() - 9); + + // printf("[==========]%ld/%ld:%s:%s: isDatabase:%d, isSnapshot:%d, stack count:%ld\n", + // idx, data->instructions.size(), printable(StringRef(data->instructions[idx].key)).c_str(), printable(StringRef(data->instructions[idx].value)).c_str(), + // isDatabase, isSnapshot, data->stack.data.size()); + + //Void _ = wait(printFlowTesterStack(&(data->stack))); + //Void _ = wait(debugPrintRange(instruction->tr, "\x01test_results", "")); + + state Reference instruction = Reference(new InstructionData(isDatabase, isSnapshot, data->instructions[idx].value, Reference())); + if (isDatabase) { + state Reference tr(new Transaction(data->db)); + instruction->tr = tr; + } + else { + instruction->tr = trMap[data->trName]; + } + + // Flow directory operations don't support snapshot reads + if (isDirectory && isSnapshot) { + Version readVersion = wait(instruction->tr->getReadVersion()); + instruction->tr = Reference(new Transaction(data->db)); + instruction->tr->setVersion(readVersion); + } + + data->stack.index = idx; + Void _ = wait(InstructionFunc::call(op.toString(), data, instruction)); + } + catch (Error& e) { + if(LOG_ERRORS) { + printf("Error: %s (%d)\n", e.what(), e.code()); + fflush(stdout); + } + + if(isDirectory) { + if(opsThatCreateDirectories.count(op.toString())) { + data->directoryData.directoryList.push_back(DirectoryOrSubspace()); + } + data->stack.pushTuple(LiteralStringRef("DIRECTORY_ERROR")); + } + else { + data->stack.pushError(e.code()); + } + } + } + // printf("Total num instructions:%d\n", data->instructions.size()); + return Void(); +} + +ACTOR static Future runTest(Reference data, Reference db, StringRef prefix) { + ASSERT(data); + try { + data->db = db; + Void _ = wait(getInstructions(data, prefix)); + Void _ = wait(doInstructions(data)); + Void _ = wait(waitForAll(data->subThreads)); + } + catch (Error& e) { + TraceEvent(SevError, "FlowTesterDataRunError").error(e); + } + + return Void(); +} + +void populateAtomicOpMap() { + optionInfo["ADD"] = FDBMutationType::FDB_MUTATION_TYPE_ADD; + optionInfo["AND"] = FDBMutationType::FDB_MUTATION_TYPE_AND; + optionInfo["BIT_AND"] = FDBMutationType::FDB_MUTATION_TYPE_BIT_AND; + optionInfo["OR"] = FDBMutationType::FDB_MUTATION_TYPE_OR; + optionInfo["BIT_OR"] = FDBMutationType::FDB_MUTATION_TYPE_BIT_OR; + optionInfo["XOR"] = FDBMutationType::FDB_MUTATION_TYPE_XOR; + optionInfo["BIT_XOR"] = FDBMutationType::FDB_MUTATION_TYPE_BIT_XOR; + optionInfo["MAX"] = FDBMutationType::FDB_MUTATION_TYPE_MAX; + optionInfo["MIN"] = FDBMutationType::FDB_MUTATION_TYPE_MIN; + optionInfo["SET_VERSIONSTAMPED_KEY"] = FDBMutationType::FDB_MUTATION_TYPE_SET_VERSIONSTAMPED_KEY; + optionInfo["SET_VERSIONSTAMPED_VALUE"] = FDBMutationType::FDB_MUTATION_TYPE_SET_VERSIONSTAMPED_VALUE; +} + +void populateOpsThatCreateDirectories() { + opsThatCreateDirectories.insert("DIRECTORY_CREATE_SUBSPACE"); + opsThatCreateDirectories.insert("DIRECTORY_CREATE_LAYER"); + opsThatCreateDirectories.insert("DIRECTORY_CREATE_OR_OPEN"); + opsThatCreateDirectories.insert("DIRECTORY_CREATE"); + opsThatCreateDirectories.insert("DIRECTORY_OPEN"); + opsThatCreateDirectories.insert("DIRECTORY_MOVE"); + opsThatCreateDirectories.insert("DIRECTORY_MOVE_TO"); + opsThatCreateDirectories.insert("DIRECTORY_OPEN_SUBSPACE"); +} + +ACTOR void startTest(std::string clusterFilename, StringRef prefix, int apiVersion) { + try { + populateAtomicOpMap(); // FIXME: NOOOOOO! + populateOpsThatCreateDirectories(); // FIXME + + // This is "our" network + g_network = newNet2(NetworkAddress(), false); + + API *fdb = API::selectAPIVersion(apiVersion); + //fdb->setNetworkOption(FDBNetworkOption::FDB_NET_OPTION_TRACE_ENABLE); + + // We have to start the fdb_flow network and thread separately! + fdb->setupNetwork(); + startThread(networkThread, fdb); + + // Connect to the default cluster/database, and create a transaction + auto cluster = fdb->createCluster(clusterFilename); + Reference db = cluster->createDatabase(LiteralStringRef("DB")); + + Reference data = Reference(new FlowTesterData(fdb)); + Void _ = wait(runTest(data, db, prefix)); + + // Stopping the network returns from g_network->run() and allows + // the program to terminate + g_network->stop(); + } + catch(Error &e) { + TraceEvent("ErrorRunningTest").error(e); + if(LOG_ERRORS) { + printf("Flow tester encountered error: %s\n", e.what()); + fflush(stdout); + } + flushAndExit(1); + } + +} + +ACTOR void _test_versionstamp() { + try { + g_network = newNet2(NetworkAddress(), false); + + API *fdb = FDB::API::selectAPIVersion(410); + + fdb->setupNetwork(); + startThread(networkThread, fdb); + + auto c = fdb->createCluster(std::string()); + auto db = c->createDatabase(LiteralStringRef("DB")); + state Reference tr(new Transaction(db)); + + state Future> ftrVersion = tr->getVersionstamp(); + + tr->atomicOp(LiteralStringRef("foo"), LiteralStringRef("blahblahbl"), FDBMutationType::FDB_MUTATION_TYPE_SET_VERSIONSTAMPED_VALUE); + + Void _ = wait(tr->commit()); // should use retry loop + + tr->reset(); + + Optional> optionalDbVersion = wait(tr->get(LiteralStringRef("foo"))); + state FDBStandalone dbVersion = optionalDbVersion.get(); + FDBStandalone trVersion = wait(ftrVersion); + + ASSERT(trVersion.compare(dbVersion) == 0); + + fprintf(stderr, "%s\n", printable(trVersion).c_str()); + + g_network->stop(); + } + catch (Error &e) { + TraceEvent("ErrorRunningTest").error(e); + if (LOG_ERRORS) { + printf("Flow tester encountered error: %s\n", e.what()); + fflush(stdout); + } + flushAndExit(1); + } +} + +int main( int argc, char** argv ) { + try { + platformInit(); + registerCrashHandler(); + g_random = new DeterministicRandom(1); + g_nondeterministic_random = new DeterministicRandom(platform::getRandomSeed()); + + // Get arguments + if (argc < 3) { + fprintf(stderr, "Missing arguments! Usage: fdb_flow_tester prefix api_version [cluster_filename]\n"); + return 1; + + /*_test_versionstamp(); + g_network->run(); + + flushAndExit(FDB_EXIT_SUCCESS);*/ + } + StringRef prefix((const uint8_t*)argv[1], strlen(argv[1])); + int apiVersion; + sscanf(argv[2], "%d", &apiVersion); + std::string clusterFilename; + if (argc > 3) { + clusterFilename = std::string(argv[3]); + } + + // start test + startTest(clusterFilename, prefix, apiVersion); + + // Run the network until someone tells us to stop + g_network->run(); + + flushAndExit(FDB_EXIT_SUCCESS); + } + catch (Error& e) { + fprintf(stderr, "Error: %s\n", e.what()); + TraceEvent(SevError, "MainError").error(e); + flushAndExit(FDB_EXIT_MAIN_ERROR); + } + catch (std::exception& e) { + fprintf(stderr, "std::exception: %s\n", e.what()); + TraceEvent(SevError, "MainError").error(unknown_error()).detail("std::exception", e.what()); + flushAndExit(FDB_EXIT_MAIN_EXCEPTION); + } +} diff --git a/bindings/flow/tester/Tester.actor.h b/bindings/flow/tester/Tester.actor.h new file mode 100644 index 0000000000..1c320b6670 --- /dev/null +++ b/bindings/flow/tester/Tester.actor.h @@ -0,0 +1,242 @@ +/* + * Tester.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FDB_FLOW_TESTER_TESTER_ACTOR_G_H) + #define FDB_FLOW_TESTER_TESTER_ACTOR_G_H + #include "Tester.actor.g.h" +#elif !defined(FDB_FLOW_TESTER_TESTER_ACTOR_H) + #define FDB_FLOW_TESTER_TESTER_ACTOR_H + +#pragma once + +#include "flow/IDispatched.h" +#include "bindings/flow/fdb_flow.h" +#include "bindings/flow/IDirectory.h" +#include "bindings/flow/Subspace.h" +#include "bindings/flow/DirectoryLayer.h" + +#define LOG_ALL 0 +#define LOG_INSTRUCTIONS LOG_ALL || 0 +#define LOG_OPS LOG_ALL || 0 +#define LOG_DIRS LOG_ALL || 0 +#define LOG_ERRORS LOG_ALL || 0 + +struct FlowTesterData; + +struct StackItem { + StackItem() : index(-1) {} + StackItem(uint32_t i, Future> v) : index(i), value(v) {} + StackItem(uint32_t i, Standalone v) : index(i), value(v) {} + uint32_t index; + Future> value; +}; + +struct FlowTesterStack { + uint32_t index; + std::vector data; + + void push(Future> value) { + data.push_back(StackItem(index, value)); + } + + void push(Standalone value) { + push(Future>(value)); + } + + void push(const StackItem& item) { + data.push_back(item); + } + + void pushTuple(StringRef value, bool utf8=false) { + FDB::Tuple t; + t.append(value, utf8); + data.push_back(StackItem(index, t.pack())); + } + + void pushError(int errorCode) { + FDB::Tuple t; + t.append(LiteralStringRef("ERROR")); + t.append(format("%d", errorCode)); + // pack above as error string into another tuple + pushTuple(t.pack().toString()); + } + + std::vector pop(uint32_t count = 1) { + std::vector items; + while (!data.empty() && count > 0) { + items.push_back(data.back()); + data.pop_back(); + count--; + } + return items; + } + + Future> waitAndPop(int count); + Future waitAndPop(); + + void dup() { + if (data.empty()) + return; + data.push_back(data.back()); + } + + void clear() { + data.clear(); + } +}; + +struct InstructionData : public ReferenceCounted { + bool isDatabase; + bool isSnapshot; + StringRef instruction; + Reference tr; + + InstructionData(bool _isDatabase, bool _isSnapshot, StringRef _instruction, Reference _tr) + : isDatabase(_isDatabase) + , isSnapshot(_isSnapshot) + , instruction(_instruction) + , tr(_tr) {} +}; + +struct FlowTesterData; + +struct InstructionFunc : IDispatched(Reference data, Reference instruction)>> { + static Future call(std::string op, Reference data, Reference instruction) { + ASSERT(data); + ASSERT(instruction); + + auto it = dispatches().find(op); + if(it == dispatches().end()) { + fprintf(stderr, "Unrecognized instruction: %s\n", op.c_str()); + ASSERT(false); + } + + return dispatch(op)(data, instruction); + } +}; +#define REGISTER_INSTRUCTION_FUNC(Op) REGISTER_COMMAND(InstructionFunc, Op, name, call) + +struct DirectoryOrSubspace { + Optional> directory; + Optional subspace; + + DirectoryOrSubspace() {} + DirectoryOrSubspace(Reference directory) : directory(directory) {} + DirectoryOrSubspace(FDB::Subspace *subspace) : subspace(subspace) {} + DirectoryOrSubspace(Reference dirSubspace) : directory(dirSubspace), subspace(dirSubspace.getPtr()) {} + + bool valid() { + return directory.present() || subspace.present(); + } + + std::string typeString() { + if(directory.present() && subspace.present()) { + return "DirectorySubspace"; + } + else if(directory.present()) { + return "IDirectory"; + } + else if(subspace.present()) { + return "Subspace"; + } + else { + return "InvalidDirectory"; + } + } +}; + +struct DirectoryTesterData { + std::vector directoryList; + int directoryListIndex; + int directoryErrorIndex; + + Reference directory() { + ASSERT(directoryListIndex < directoryList.size()); + ASSERT(directoryList[directoryListIndex].directory.present()); + return directoryList[directoryListIndex].directory.get(); + } + + FDB::Subspace* subspace() { + ASSERT(directoryListIndex < directoryList.size()); + ASSERT(directoryList[directoryListIndex].subspace.present()); + return directoryList[directoryListIndex].subspace.get(); + } + + DirectoryTesterData() : directoryListIndex(0), directoryErrorIndex(0) { + directoryList.push_back(Reference(new FDB::DirectoryLayer())); + } + + template + void push(T item) { + directoryList.push_back(DirectoryOrSubspace(item)); + if(LOG_DIRS) { + printf("Pushed %s at %lu\n", directoryList.back().typeString().c_str(), directoryList.size()-1); + fflush(stdout); + } + } + + void push() { push(DirectoryOrSubspace()); } +}; + +struct FlowTesterData : public ReferenceCounted { + FDB::API *api; + Reference db; + Standalone instructions; + Standalone trName; + FlowTesterStack stack; + FDB::Version lastVersion; + DirectoryTesterData directoryData; + + std::vector> subThreads; + + Future processInstruction(Reference instruction) { + return InstructionFunc::call(instruction->instruction.toString(), Reference::addRef(this), instruction); + } + + FlowTesterData(FDB::API *api) { + this->api = api; + } +}; + +std::string tupleToString(FDB::Tuple const& tuple); + +ACTOR template +Future()().getValue())> executeMutation(Reference instruction, F func) { + loop { + try { + state decltype(fake()().getValue()) result = wait(func()); + if(instruction->isDatabase) { + Void _ = wait(instruction->tr->commit()); + } + return result; + } + catch(Error &e) { + if(instruction->isDatabase) { + Void _ = wait(instruction->tr->onError(e)); + } + else { + throw; + } + } + } +} + +#endif diff --git a/bindings/flow/tester/fdb_flow_tester.vcxproj b/bindings/flow/tester/fdb_flow_tester.vcxproj new file mode 100644 index 0000000000..ce8fe46ea8 --- /dev/null +++ b/bindings/flow/tester/fdb_flow_tester.vcxproj @@ -0,0 +1,128 @@ + + + + + -PRERELEASE + + + + + + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + {086EB89C-CDBD-4ABE-8296-5CA224244C80} + Win32Proj + fdb_flow_tester + + + + Application + true + MultiByte + v140_xp + + + Application + false + false + MultiByte + v140_xp + + + + + + + + + + + + + true + $(SolutionDir)bin\$(Configuration)\ + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + $(IncludePath);../../../;C:\Program Files\boost_1_52_0 + + + false + $(SolutionDir)bin\$(Configuration)\ + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + $(IncludePath);../;C:\Program Files\boost_1_52_0 + + + + FDB_VT_VERSION="$(Version)$(PreReleaseDecoration)";FDB_VT_PACKAGE_NAME="$(PackageName)";%(PreprocessorDefinitions) + + + + + + + Level3 + Disabled + WIN32;_WIN32_WINNT=_WIN32_WINNT_WS03;WINVER=_WIN32_WINNT_WS03;NTDDI_VERSION=NTDDI_WS03;_DEBUG;_HAS_ITERATOR_DEBUGGING=0;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + false + MultiThreadedDebug + @../../../flow/no_intellisense.opt %(AdditionalOptions) + + + Console + true + $(SolutionDir)bin\$(Configuration)\fdbclient.lib;$(SolutionDir)bin\$(Configuration)\fdb_c.lib;$(SolutionDir)bin\$(Configuration)\fdb_flow.lib;Advapi32.lib + + + + + + + + + Level3 + + + Full + true + WIN32;_WIN32_WINNT=_WIN32_WINNT_WS03;WINVER=_WIN32_WINNT_WS03;NTDDI_VERSION=NTDDI_WS03;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + true + Speed + MultiThreaded + false + StreamingSIMDExtensions2 + @../flow/no_intellisense.opt %(AdditionalOptions) + + + Console + true + false + false + $(SolutionDir)bin\$(Configuration)\fdbclient.lib;$(SolutionDir)bin\$(Configuration)\fdb_c.lib;$(SolutionDir)bin\$(Configuration)\fdb_flow.lib;Advapi32.lib + Default + + + + + + + + + + + \ No newline at end of file diff --git a/bindings/flow/tester/fdb_flow_tester.vcxproj.filters b/bindings/flow/tester/fdb_flow_tester.vcxproj.filters new file mode 100644 index 0000000000..f3a7efcce6 --- /dev/null +++ b/bindings/flow/tester/fdb_flow_tester.vcxproj.filters @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/bindings/flow/tester/local.mk b/bindings/flow/tester/local.mk new file mode 100644 index 0000000000..570ad9c0bf --- /dev/null +++ b/bindings/flow/tester/local.mk @@ -0,0 +1,42 @@ +# +# local.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile; -*- + +fdb_flow_tester_CFLAGS := -Ibindings/c $(fdbrpc_CFLAGS) +fdb_flow_tester_LDFLAGS := -Llib $(fdbrpc_LDFLAGS) -lfdb_c +fdb_flow_tester_LIBS := lib/libfdb_flow.a lib/libfdbrpc.a lib/libflow.a lib/libfdb_c.$(DLEXT) + +fdb_flow_tester: lib/libfdb_c.$(DLEXT) + @mkdir -p bindings/flow/bin + @rm -f bindings/flow/bin/fdb_flow_tester + @cp bin/fdb_flow_tester bindings/flow/bin/fdb_flow_tester + +fdb_flow_tester_clean: _fdb_flow_tester_clean + +_fdb_flow_tester_clean: + @rm -rf bindings/flow/bin + +ifeq ($(PLATFORM),linux) + fdb_flow_tester_LIBS += -ldl -lpthread -lrt + fdb_flow_tester_LDFLAGS += -static-libstdc++ -static-libgcc +else ifeq ($(PLATFORM),osx) + fdb_flow_tester_LDFLAGS += -lc++ +endif diff --git a/bindings/go/README.md b/bindings/go/README.md new file mode 100644 index 0000000000..89f93976b2 --- /dev/null +++ b/bindings/go/README.md @@ -0,0 +1,21 @@ +fdb-go +====== + +[Go language](http://golang.org) bindings for [FoundationDB](http://foundationdb.org/documentation/), a distributed key-value store with ACID transactions. + +This package requires: + +- Go 1.1+ with CGO enabled +- FoundationDB C API 2.0.x, 3.0.x, or 4.x.y (part of the [FoundationDB clients package](https://files.foundationdb.org/fdb-c/)) + +Use of this package requires the selection of a FoundationDB API version at runtime. This package currently supports FoundationDB API versions 200-500. + +To install this package, in the top level of this repository run: + + make fdb_go + +Documentation +------------- + +* [API documentation](https://foundationdb.org/documentation/godoc/fdb.html) +* [Tutorial](https://foundationdb.org/documentation/class-scheduling-go.html) diff --git a/bindings/go/godoc-resources/godocs.js b/bindings/go/godoc-resources/godocs.js new file mode 100644 index 0000000000..2ea3c7ada2 --- /dev/null +++ b/bindings/go/godoc-resources/godocs.js @@ -0,0 +1,567 @@ +// Copyright 2012 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +/* A little code to ease navigation of these documents. + * + * On window load we: + * + Bind search box hint placeholder show/hide events (bindSearchEvents) + * + Generate a table of contents (generateTOC) + * + Bind foldable sections (bindToggles) + * + Bind links to foldable sections (bindToggleLinks) + */ + +(function() { +'use strict'; + +// Mobile-friendly topbar menu +$(function() { + var menu = $('#menu'); + var menuButton = $('#menu-button'); + var menuButtonArrow = $('#menu-button-arrow'); + menuButton.click(function(event) { + menu.toggleClass('menu-visible'); + menuButtonArrow.toggleClass('vertical-flip'); + event.preventDefault(); + return false; + }); +}); + +function bindSearchEvents() { + + var search = $('#search'); + if (search.length === 0) { + return; // no search box + } + + function clearInactive() { + if (search.is('.inactive')) { + search.val(''); + search.removeClass('inactive'); + } + } + + function restoreInactive() { + if (search.val() !== '') { + return; + } + search.val(search.attr('placeholder')); + search.addClass('inactive'); + } + + search.on('focus', clearInactive); + search.on('blur', restoreInactive); + + restoreInactive(); +} + +/* Generates a table of contents: looks for h2 and h3 elements and generates + * links. "Decorates" the element with id=="nav" with this table of contents. + */ +function generateTOC() { + if ($('#manual-nav').length > 0) { + return; + } + + var nav = $('#nav'); + if (nav.length === 0) { + return; + } + + var toc_items = []; + $(nav).nextAll('h2, h3').each(function() { + var node = this; + if (node.id == '') + node.id = 'tmp_' + toc_items.length; + var link = $('').attr('href', '#' + node.id).text($(node).text()); + var item; + if ($(node).is('h2')) { + item = $('
'); + } else { // h3 + item = $('
'); + } + item.append(link); + toc_items.push(item); + }); + if (toc_items.length <= 1) { + return; + } + + var dl1 = $('
'); + var dl2 = $('
'); + + var split_index = (toc_items.length / 2) + 1; + if (split_index < 8) { + split_index = toc_items.length; + } + for (var i = 0; i < split_index; i++) { + dl1.append(toc_items[i]); + } + for (/* keep using i */; i < toc_items.length; i++) { + dl2.append(toc_items[i]); + } + + var tocTable = $('').appendTo(nav); + var tocBody = $('').appendTo(tocTable); + var tocRow = $('').appendTo(tocBody); + + // 1st column + $(']","i"),bv=/^(?:checkbox|radio)$/,bw=/checked\s*(?:[^=]|=\s*.checked.)/i,bx=/\/(java|ecma)script/i,by=/^\s*\s*$/g,bz={option:[1,""],legend:[1,"
","
"],thead:[1,"
').appendTo(tocRow).append(dl1); + // 2nd column + $('').appendTo(tocRow).append(dl2); +} + +function bindToggle(el) { + $('.toggleButton', el).click(function() { + if ($(el).is('.toggle')) { + $(el).addClass('toggleVisible').removeClass('toggle'); + } else { + $(el).addClass('toggle').removeClass('toggleVisible'); + } + }); +} +function bindToggles(selector) { + $(selector).each(function(i, el) { + bindToggle(el); + }); +} + +function bindToggleLink(el, prefix) { + $(el).click(function() { + var href = $(el).attr('href'); + var i = href.indexOf('#'+prefix); + if (i < 0) { + return; + } + var id = '#' + prefix + href.slice(i+1+prefix.length); + if ($(id).is('.toggle')) { + $(id).find('.toggleButton').first().click(); + } + }); +} +function bindToggleLinks(selector, prefix) { + $(selector).each(function(i, el) { + bindToggleLink(el, prefix); + }); +} + +function setupDropdownPlayground() { + if (!$('#page').is('.wide')) { + return; // don't show on front page + } + var button = $('#playgroundButton'); + var div = $('#playground'); + var setup = false; + button.toggle(function() { + button.addClass('active'); + div.show(); + if (setup) { + return; + } + setup = true; + playground({ + 'codeEl': $('.code', div), + 'outputEl': $('.output', div), + 'runEl': $('.run', div), + 'fmtEl': $('.fmt', div), + 'shareEl': $('.share', div), + 'shareRedirect': '//play.golang.org/p/' + }); + }, + function() { + button.removeClass('active'); + div.hide(); + }); + button.show(); + $('#menu').css('min-width', '+=60'); +} + +function setupInlinePlayground() { + 'use strict'; + // Set up playground when each element is toggled. + $('div.play').each(function (i, el) { + // Set up playground for this example. + var setup = function() { + var code = $('.code', el); + playground({ + 'codeEl': code, + 'outputEl': $('.output', el), + 'runEl': $('.run', el), + 'fmtEl': $('.fmt', el), + 'shareEl': $('.share', el), + 'shareRedirect': '//play.golang.org/p/' + }); + + // Make the code textarea resize to fit content. + var resize = function() { + code.height(0); + var h = code[0].scrollHeight; + code.height(h+20); // minimize bouncing. + code.closest('.input').height(h); + }; + code.on('keydown', resize); + code.on('keyup', resize); + code.keyup(); // resize now. + }; + + // If example already visible, set up playground now. + if ($(el).is(':visible')) { + setup(); + return; + } + + // Otherwise, set up playground when example is expanded. + var built = false; + $(el).closest('.toggle').click(function() { + // Only set up once. + if (!built) { + setup(); + built = true; + } + }); + }); +} + +// fixFocus tries to put focus to div#page so that keyboard navigation works. +function fixFocus() { + var page = $('div#page'); + var topbar = $('div#topbar'); + page.css('outline', 0); // disable outline when focused + page.attr('tabindex', -1); // and set tabindex so that it is focusable + $(window).resize(function (evt) { + // only focus page when the topbar is at fixed position (that is, it's in + // front of page, and keyboard event will go to the former by default.) + // by focusing page, keyboard event will go to page so that up/down arrow, + // space, etc. will work as expected. + if (topbar.css('position') == "fixed") + page.focus(); + }).resize(); +} + +function toggleHash() { + var hash = $(window.location.hash); + if (hash.is('.toggle')) { + hash.find('.toggleButton').first().click(); + } +} + +function personalizeInstallInstructions() { + var prefix = '?download='; + var s = window.location.search; + if (s.indexOf(prefix) != 0) { + // No 'download' query string; bail. + return; + } + + var filename = s.substr(prefix.length); + var filenameRE = /^go1\.\d+(\.\d+)?([a-z0-9]+)?\.([a-z0-9]+)(-[a-z0-9]+)?(-osx10\.[68])?\.([a-z.]+)$/; + $('.downloadFilename').text(filename); + $('.hideFromDownload').hide(); + var m = filenameRE.exec(filename); + if (!m) { + // Can't interpret file name; bail. + return; + } + + var os = m[3]; + var ext = m[6]; + if (ext != 'tar.gz') { + $('#tarballInstructions').hide(); + } + if (os != 'darwin' || ext != 'pkg') { + $('#darwinPackageInstructions').hide(); + } + if (os != 'windows') { + $('#windowsInstructions').hide(); + } else { + if (ext != 'msi') { + $('#windowsInstallerInstructions').hide(); + } + if (ext != 'zip') { + $('#windowsZipInstructions').hide(); + } + } + + var download = "https://storage.googleapis.com/golang/" + filename; + + var message = $('

'+ + 'Your download should begin shortly. '+ + 'If it does not, click this link.

'); + message.find('a').attr('href', download); + message.insertAfter('#nav'); + + window.location = download; +} + +$(document).ready(function() { + bindSearchEvents(); + generateTOC(); + bindToggles(".toggle"); + bindToggles(".toggleVisible"); + bindToggleLinks(".exampleLink", "example_"); + bindToggleLinks(".overviewLink", ""); + bindToggleLinks(".examplesLink", ""); + bindToggleLinks(".indexLink", ""); + setupDropdownPlayground(); + setupInlinePlayground(); + fixFocus(); + setupTypeInfo(); + setupCallgraphs(); + toggleHash(); + personalizeInstallInstructions(); + + // godoc.html defines window.initFuncs in the tag, and root.html and + // codewalk.js push their on-page-ready functions to the list. + // We execute those functions here, to avoid loading jQuery until the page + // content is loaded. + for (var i = 0; i < window.initFuncs.length; i++) window.initFuncs[i](); +}); + +// -- analysis --------------------------------------------------------- + +// escapeHTML returns HTML for s, with metacharacters quoted. +// It is safe for use in both elements and attributes +// (unlike the "set innerText, read innerHTML" trick). +function escapeHTML(s) { + return s.replace(/&/g, '&'). + replace(/\"/g, '"'). + replace(/\'/g, '''). + replace(//g, '>'); +} + +// makeAnchor returns HTML for an element, given an anchorJSON object. +function makeAnchor(json) { + var html = escapeHTML(json.Text); + if (json.Href != "") { + html = "" + html + ""; + } + return html; +} + +function showLowFrame(html) { + var lowframe = document.getElementById('lowframe'); + lowframe.style.height = "200px"; + lowframe.innerHTML = "

" + html + "

\n" + + "
✘
" +}; + +document.hideLowFrame = function() { + var lowframe = document.getElementById('lowframe'); + lowframe.style.height = "0px"; +} + +// onClickCallers is the onclick action for the 'func' tokens of a +// function declaration. +document.onClickCallers = function(index) { + var data = document.ANALYSIS_DATA[index] + if (data.Callers.length == 1 && data.Callers[0].Sites.length == 1) { + document.location = data.Callers[0].Sites[0].Href; // jump to sole caller + return; + } + + var html = "Callers of " + escapeHTML(data.Callee) + ":
\n"; + for (var i = 0; i < data.Callers.length; i++) { + var caller = data.Callers[i]; + html += "" + escapeHTML(caller.Func) + ""; + var sites = caller.Sites; + if (sites != null && sites.length > 0) { + html += " at line "; + for (var j = 0; j < sites.length; j++) { + if (j > 0) { + html += ", "; + } + html += "" + makeAnchor(sites[j]) + ""; + } + } + html += "
\n"; + } + showLowFrame(html); +}; + +// onClickCallees is the onclick action for the '(' token of a function call. +document.onClickCallees = function(index) { + var data = document.ANALYSIS_DATA[index] + if (data.Callees.length == 1) { + document.location = data.Callees[0].Href; // jump to sole callee + return; + } + + var html = "Callees of this " + escapeHTML(data.Descr) + ":
\n"; + for (var i = 0; i < data.Callees.length; i++) { + html += "" + makeAnchor(data.Callees[i]) + "
\n"; + } + showLowFrame(html); +}; + +// onClickTypeInfo is the onclick action for identifiers declaring a named type. +document.onClickTypeInfo = function(index) { + var data = document.ANALYSIS_DATA[index]; + var html = "Type " + data.Name + ": " + + "      (size=" + data.Size + ", align=" + data.Align + ")
\n"; + html += implementsHTML(data); + html += methodsetHTML(data); + showLowFrame(html); +}; + +// implementsHTML returns HTML for the implements relation of the +// specified TypeInfoJSON value. +function implementsHTML(info) { + var html = ""; + if (info.ImplGroups != null) { + for (var i = 0; i < info.ImplGroups.length; i++) { + var group = info.ImplGroups[i]; + var x = "" + escapeHTML(group.Descr) + " "; + for (var j = 0; j < group.Facts.length; j++) { + var fact = group.Facts[j]; + var y = "" + makeAnchor(fact.Other) + ""; + if (fact.ByKind != null) { + html += escapeHTML(fact.ByKind) + " type " + y + " implements " + x; + } else { + html += x + " implements " + y; + } + html += "
\n"; + } + } + } + return html; +} + + +// methodsetHTML returns HTML for the methodset of the specified +// TypeInfoJSON value. +function methodsetHTML(info) { + var html = ""; + if (info.Methods != null) { + for (var i = 0; i < info.Methods.length; i++) { + html += "" + makeAnchor(info.Methods[i]) + "
\n"; + } + } + return html; +} + +// onClickComm is the onclick action for channel "make" and "<-" +// send/receive tokens. +document.onClickComm = function(index) { + var ops = document.ANALYSIS_DATA[index].Ops + if (ops.length == 1) { + document.location = ops[0].Op.Href; // jump to sole element + return; + } + + var html = "Operations on this channel:
\n"; + for (var i = 0; i < ops.length; i++) { + html += makeAnchor(ops[i].Op) + " by " + escapeHTML(ops[i].Fn) + "
\n"; + } + if (ops.length == 0) { + html += "(none)
\n"; + } + showLowFrame(html); +}; + +$(window).load(function() { + // Scroll window so that first selection is visible. + // (This means we don't need to emit id='L%d' spans for each line.) + // TODO(adonovan): ideally, scroll it so that it's under the pointer, + // but I don't know how to get the pointer y coordinate. + var elts = document.getElementsByClassName("selection"); + if (elts.length > 0) { + elts[0].scrollIntoView() + } +}); + +// setupTypeInfo populates the "Implements" and "Method set" toggle for +// each type in the package doc. +function setupTypeInfo() { + for (var i in document.ANALYSIS_DATA) { + var data = document.ANALYSIS_DATA[i]; + + var el = document.getElementById("implements-" + i); + if (el != null) { + // el != null => data is TypeInfoJSON. + if (data.ImplGroups != null) { + el.innerHTML = implementsHTML(data); + el.parentNode.parentNode.style.display = "block"; + } + } + + var el = document.getElementById("methodset-" + i); + if (el != null) { + // el != null => data is TypeInfoJSON. + if (data.Methods != null) { + el.innerHTML = methodsetHTML(data); + el.parentNode.parentNode.style.display = "block"; + } + } + } +} + +function setupCallgraphs() { + if (document.CALLGRAPH == null) { + return + } + document.getElementById("pkg-callgraph").style.display = "block"; + + var treeviews = document.getElementsByClassName("treeview"); + for (var i = 0; i < treeviews.length; i++) { + var tree = treeviews[i]; + if (tree.id == null || tree.id.indexOf("callgraph-") != 0) { + continue; + } + var id = tree.id.substring("callgraph-".length); + $(tree).treeview({collapsed: true, animated: "fast"}); + document.cgAddChildren(tree, tree, [id]); + tree.parentNode.parentNode.style.display = "block"; + } +} + +document.cgAddChildren = function(tree, ul, indices) { + if (indices != null) { + for (var i = 0; i < indices.length; i++) { + var li = cgAddChild(tree, ul, document.CALLGRAPH[indices[i]]); + if (i == indices.length - 1) { + $(li).addClass("last"); + } + } + } + $(tree).treeview({animated: "fast", add: ul}); +} + +// cgAddChild adds an
  • element for document.CALLGRAPH node cgn to +// the parent
      element ul. tree is the tree's root
        element. +function cgAddChild(tree, ul, cgn) { + var li = document.createElement("li"); + ul.appendChild(li); + li.className = "closed"; + + var code = document.createElement("code"); + + if (cgn.Callees != null) { + $(li).addClass("expandable"); + + // Event handlers and innerHTML updates don't play nicely together, + // hence all this explicit DOM manipulation. + var hitarea = document.createElement("div"); + hitarea.className = "hitarea expandable-hitarea"; + li.appendChild(hitarea); + + li.appendChild(code); + + var childUL = document.createElement("ul"); + li.appendChild(childUL); + childUL.setAttribute('style', "display: none;"); + + var onClick = function() { + document.cgAddChildren(tree, childUL, cgn.Callees); + hitarea.removeEventListener('click', onClick) + }; + hitarea.addEventListener('click', onClick); + + } else { + li.appendChild(code); + } + code.innerHTML += " " + makeAnchor(cgn.Func); + return li +} + +})(); diff --git a/bindings/go/godoc-resources/jquery.js b/bindings/go/godoc-resources/jquery.js new file mode 100644 index 0000000000..bc3fbc81b2 --- /dev/null +++ b/bindings/go/godoc-resources/jquery.js @@ -0,0 +1,2 @@ +/*! jQuery v1.8.2 jquery.com | jquery.org/license */ +(function(a,b){function G(a){var b=F[a]={};return p.each(a.split(s),function(a,c){b[c]=!0}),b}function J(a,c,d){if(d===b&&a.nodeType===1){var e="data-"+c.replace(I,"-$1").toLowerCase();d=a.getAttribute(e);if(typeof d=="string"){try{d=d==="true"?!0:d==="false"?!1:d==="null"?null:+d+""===d?+d:H.test(d)?p.parseJSON(d):d}catch(f){}p.data(a,c,d)}else d=b}return d}function K(a){var b;for(b in a){if(b==="data"&&p.isEmptyObject(a[b]))continue;if(b!=="toJSON")return!1}return!0}function ba(){return!1}function bb(){return!0}function bh(a){return!a||!a.parentNode||a.parentNode.nodeType===11}function bi(a,b){do a=a[b];while(a&&a.nodeType!==1);return a}function bj(a,b,c){b=b||0;if(p.isFunction(b))return p.grep(a,function(a,d){var e=!!b.call(a,d,a);return e===c});if(b.nodeType)return p.grep(a,function(a,d){return a===b===c});if(typeof b=="string"){var d=p.grep(a,function(a){return a.nodeType===1});if(be.test(b))return p.filter(b,d,!c);b=p.filter(b,d)}return p.grep(a,function(a,d){return p.inArray(a,b)>=0===c})}function bk(a){var b=bl.split("|"),c=a.createDocumentFragment();if(c.createElement)while(b.length)c.createElement(b.pop());return c}function bC(a,b){return a.getElementsByTagName(b)[0]||a.appendChild(a.ownerDocument.createElement(b))}function bD(a,b){if(b.nodeType!==1||!p.hasData(a))return;var c,d,e,f=p._data(a),g=p._data(b,f),h=f.events;if(h){delete g.handle,g.events={};for(c in h)for(d=0,e=h[c].length;d").appendTo(e.body),c=b.css("display");b.remove();if(c==="none"||c===""){bI=e.body.appendChild(bI||p.extend(e.createElement("iframe"),{frameBorder:0,width:0,height:0}));if(!bJ||!bI.createElement)bJ=(bI.contentWindow||bI.contentDocument).document,bJ.write(""),bJ.close();b=bJ.body.appendChild(bJ.createElement(a)),c=bH(b,"display"),e.body.removeChild(bI)}return bS[a]=c,c}function ci(a,b,c,d){var e;if(p.isArray(b))p.each(b,function(b,e){c||ce.test(a)?d(a,e):ci(a+"["+(typeof e=="object"?b:"")+"]",e,c,d)});else if(!c&&p.type(b)==="object")for(e in b)ci(a+"["+e+"]",b[e],c,d);else d(a,b)}function cz(a){return function(b,c){typeof b!="string"&&(c=b,b="*");var d,e,f,g=b.toLowerCase().split(s),h=0,i=g.length;if(p.isFunction(c))for(;h)[^>]*$|#([\w\-]*)$)/,v=/^<(\w+)\s*\/?>(?:<\/\1>|)$/,w=/^[\],:{}\s]*$/,x=/(?:^|:|,)(?:\s*\[)+/g,y=/\\(?:["\\\/bfnrt]|u[\da-fA-F]{4})/g,z=/"[^"\\\r\n]*"|true|false|null|-?(?:\d\d*\.|)\d+(?:[eE][\-+]?\d+|)/g,A=/^-ms-/,B=/-([\da-z])/gi,C=function(a,b){return(b+"").toUpperCase()},D=function(){e.addEventListener?(e.removeEventListener("DOMContentLoaded",D,!1),p.ready()):e.readyState==="complete"&&(e.detachEvent("onreadystatechange",D),p.ready())},E={};p.fn=p.prototype={constructor:p,init:function(a,c,d){var f,g,h,i;if(!a)return this;if(a.nodeType)return this.context=this[0]=a,this.length=1,this;if(typeof a=="string"){a.charAt(0)==="<"&&a.charAt(a.length-1)===">"&&a.length>=3?f=[null,a,null]:f=u.exec(a);if(f&&(f[1]||!c)){if(f[1])return c=c instanceof p?c[0]:c,i=c&&c.nodeType?c.ownerDocument||c:e,a=p.parseHTML(f[1],i,!0),v.test(f[1])&&p.isPlainObject(c)&&this.attr.call(a,c,!0),p.merge(this,a);g=e.getElementById(f[2]);if(g&&g.parentNode){if(g.id!==f[2])return d.find(a);this.length=1,this[0]=g}return this.context=e,this.selector=a,this}return!c||c.jquery?(c||d).find(a):this.constructor(c).find(a)}return p.isFunction(a)?d.ready(a):(a.selector!==b&&(this.selector=a.selector,this.context=a.context),p.makeArray(a,this))},selector:"",jquery:"1.8.2",length:0,size:function(){return this.length},toArray:function(){return k.call(this)},get:function(a){return a==null?this.toArray():a<0?this[this.length+a]:this[a]},pushStack:function(a,b,c){var d=p.merge(this.constructor(),a);return d.prevObject=this,d.context=this.context,b==="find"?d.selector=this.selector+(this.selector?" ":"")+c:b&&(d.selector=this.selector+"."+b+"("+c+")"),d},each:function(a,b){return p.each(this,a,b)},ready:function(a){return p.ready.promise().done(a),this},eq:function(a){return a=+a,a===-1?this.slice(a):this.slice(a,a+1)},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},slice:function(){return this.pushStack(k.apply(this,arguments),"slice",k.call(arguments).join(","))},map:function(a){return this.pushStack(p.map(this,function(b,c){return a.call(b,c,b)}))},end:function(){return this.prevObject||this.constructor(null)},push:j,sort:[].sort,splice:[].splice},p.fn.init.prototype=p.fn,p.extend=p.fn.extend=function(){var a,c,d,e,f,g,h=arguments[0]||{},i=1,j=arguments.length,k=!1;typeof h=="boolean"&&(k=h,h=arguments[1]||{},i=2),typeof h!="object"&&!p.isFunction(h)&&(h={}),j===i&&(h=this,--i);for(;i0)return;d.resolveWith(e,[p]),p.fn.trigger&&p(e).trigger("ready").off("ready")},isFunction:function(a){return p.type(a)==="function"},isArray:Array.isArray||function(a){return p.type(a)==="array"},isWindow:function(a){return a!=null&&a==a.window},isNumeric:function(a){return!isNaN(parseFloat(a))&&isFinite(a)},type:function(a){return a==null?String(a):E[m.call(a)]||"object"},isPlainObject:function(a){if(!a||p.type(a)!=="object"||a.nodeType||p.isWindow(a))return!1;try{if(a.constructor&&!n.call(a,"constructor")&&!n.call(a.constructor.prototype,"isPrototypeOf"))return!1}catch(c){return!1}var d;for(d in a);return d===b||n.call(a,d)},isEmptyObject:function(a){var b;for(b in a)return!1;return!0},error:function(a){throw new Error(a)},parseHTML:function(a,b,c){var d;return!a||typeof a!="string"?null:(typeof b=="boolean"&&(c=b,b=0),b=b||e,(d=v.exec(a))?[b.createElement(d[1])]:(d=p.buildFragment([a],b,c?null:[]),p.merge([],(d.cacheable?p.clone(d.fragment):d.fragment).childNodes)))},parseJSON:function(b){if(!b||typeof b!="string")return null;b=p.trim(b);if(a.JSON&&a.JSON.parse)return a.JSON.parse(b);if(w.test(b.replace(y,"@").replace(z,"]").replace(x,"")))return(new Function("return "+b))();p.error("Invalid JSON: "+b)},parseXML:function(c){var d,e;if(!c||typeof c!="string")return null;try{a.DOMParser?(e=new DOMParser,d=e.parseFromString(c,"text/xml")):(d=new ActiveXObject("Microsoft.XMLDOM"),d.async="false",d.loadXML(c))}catch(f){d=b}return(!d||!d.documentElement||d.getElementsByTagName("parsererror").length)&&p.error("Invalid XML: "+c),d},noop:function(){},globalEval:function(b){b&&r.test(b)&&(a.execScript||function(b){a.eval.call(a,b)})(b)},camelCase:function(a){return a.replace(A,"ms-").replace(B,C)},nodeName:function(a,b){return a.nodeName&&a.nodeName.toLowerCase()===b.toLowerCase()},each:function(a,c,d){var e,f=0,g=a.length,h=g===b||p.isFunction(a);if(d){if(h){for(e in a)if(c.apply(a[e],d)===!1)break}else for(;f0&&a[0]&&a[i-1]||i===0||p.isArray(a));if(j)for(;h-1)i.splice(c,1),e&&(c<=g&&g--,c<=h&&h--)}),this},has:function(a){return p.inArray(a,i)>-1},empty:function(){return i=[],this},disable:function(){return i=j=c=b,this},disabled:function(){return!i},lock:function(){return j=b,c||l.disable(),this},locked:function(){return!j},fireWith:function(a,b){return b=b||[],b=[a,b.slice?b.slice():b],i&&(!d||j)&&(e?j.push(b):k(b)),this},fire:function(){return l.fireWith(this,arguments),this},fired:function(){return!!d}};return l},p.extend({Deferred:function(a){var b=[["resolve","done",p.Callbacks("once memory"),"resolved"],["reject","fail",p.Callbacks("once memory"),"rejected"],["notify","progress",p.Callbacks("memory")]],c="pending",d={state:function(){return c},always:function(){return e.done(arguments).fail(arguments),this},then:function(){var a=arguments;return p.Deferred(function(c){p.each(b,function(b,d){var f=d[0],g=a[b];e[d[1]](p.isFunction(g)?function(){var a=g.apply(this,arguments);a&&p.isFunction(a.promise)?a.promise().done(c.resolve).fail(c.reject).progress(c.notify):c[f+"With"](this===e?c:this,[a])}:c[f])}),a=null}).promise()},promise:function(a){return a!=null?p.extend(a,d):d}},e={};return d.pipe=d.then,p.each(b,function(a,f){var g=f[2],h=f[3];d[f[1]]=g.add,h&&g.add(function(){c=h},b[a^1][2].disable,b[2][2].lock),e[f[0]]=g.fire,e[f[0]+"With"]=g.fireWith}),d.promise(e),a&&a.call(e,e),e},when:function(a){var b=0,c=k.call(arguments),d=c.length,e=d!==1||a&&p.isFunction(a.promise)?d:0,f=e===1?a:p.Deferred(),g=function(a,b,c){return function(d){b[a]=this,c[a]=arguments.length>1?k.call(arguments):d,c===h?f.notifyWith(b,c):--e||f.resolveWith(b,c)}},h,i,j;if(d>1){h=new Array(d),i=new Array(d),j=new Array(d);for(;b
        a",c=n.getElementsByTagName("*"),d=n.getElementsByTagName("a")[0],d.style.cssText="top:1px;float:left;opacity:.5";if(!c||!c.length)return{};f=e.createElement("select"),g=f.appendChild(e.createElement("option")),h=n.getElementsByTagName("input")[0],b={leadingWhitespace:n.firstChild.nodeType===3,tbody:!n.getElementsByTagName("tbody").length,htmlSerialize:!!n.getElementsByTagName("link").length,style:/top/.test(d.getAttribute("style")),hrefNormalized:d.getAttribute("href")==="/a",opacity:/^0.5/.test(d.style.opacity),cssFloat:!!d.style.cssFloat,checkOn:h.value==="on",optSelected:g.selected,getSetAttribute:n.className!=="t",enctype:!!e.createElement("form").enctype,html5Clone:e.createElement("nav").cloneNode(!0).outerHTML!=="<:nav>",boxModel:e.compatMode==="CSS1Compat",submitBubbles:!0,changeBubbles:!0,focusinBubbles:!1,deleteExpando:!0,noCloneEvent:!0,inlineBlockNeedsLayout:!1,shrinkWrapBlocks:!1,reliableMarginRight:!0,boxSizingReliable:!0,pixelPosition:!1},h.checked=!0,b.noCloneChecked=h.cloneNode(!0).checked,f.disabled=!0,b.optDisabled=!g.disabled;try{delete n.test}catch(o){b.deleteExpando=!1}!n.addEventListener&&n.attachEvent&&n.fireEvent&&(n.attachEvent("onclick",m=function(){b.noCloneEvent=!1}),n.cloneNode(!0).fireEvent("onclick"),n.detachEvent("onclick",m)),h=e.createElement("input"),h.value="t",h.setAttribute("type","radio"),b.radioValue=h.value==="t",h.setAttribute("checked","checked"),h.setAttribute("name","t"),n.appendChild(h),i=e.createDocumentFragment(),i.appendChild(n.lastChild),b.checkClone=i.cloneNode(!0).cloneNode(!0).lastChild.checked,b.appendChecked=h.checked,i.removeChild(h),i.appendChild(n);if(n.attachEvent)for(k in{submit:!0,change:!0,focusin:!0})j="on"+k,l=j in n,l||(n.setAttribute(j,"return;"),l=typeof n[j]=="function"),b[k+"Bubbles"]=l;return p(function(){var c,d,f,g,h="padding:0;margin:0;border:0;display:block;overflow:hidden;",i=e.getElementsByTagName("body")[0];if(!i)return;c=e.createElement("div"),c.style.cssText="visibility:hidden;border:0;width:0;height:0;position:static;top:0;margin-top:1px",i.insertBefore(c,i.firstChild),d=e.createElement("div"),c.appendChild(d),d.innerHTML="
        t
        ",f=d.getElementsByTagName("td"),f[0].style.cssText="padding:0;margin:0;border:0;display:none",l=f[0].offsetHeight===0,f[0].style.display="",f[1].style.display="none",b.reliableHiddenOffsets=l&&f[0].offsetHeight===0,d.innerHTML="",d.style.cssText="box-sizing:border-box;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;padding:1px;border:1px;display:block;width:4px;margin-top:1%;position:absolute;top:1%;",b.boxSizing=d.offsetWidth===4,b.doesNotIncludeMarginInBodyOffset=i.offsetTop!==1,a.getComputedStyle&&(b.pixelPosition=(a.getComputedStyle(d,null)||{}).top!=="1%",b.boxSizingReliable=(a.getComputedStyle(d,null)||{width:"4px"}).width==="4px",g=e.createElement("div"),g.style.cssText=d.style.cssText=h,g.style.marginRight=g.style.width="0",d.style.width="1px",d.appendChild(g),b.reliableMarginRight=!parseFloat((a.getComputedStyle(g,null)||{}).marginRight)),typeof d.style.zoom!="undefined"&&(d.innerHTML="",d.style.cssText=h+"width:1px;padding:1px;display:inline;zoom:1",b.inlineBlockNeedsLayout=d.offsetWidth===3,d.style.display="block",d.style.overflow="visible",d.innerHTML="
        ",d.firstChild.style.width="5px",b.shrinkWrapBlocks=d.offsetWidth!==3,c.style.zoom=1),i.removeChild(c),c=d=f=g=null}),i.removeChild(n),c=d=f=g=h=i=n=null,b}();var H=/(?:\{[\s\S]*\}|\[[\s\S]*\])$/,I=/([A-Z])/g;p.extend({cache:{},deletedIds:[],uuid:0,expando:"jQuery"+(p.fn.jquery+Math.random()).replace(/\D/g,""),noData:{embed:!0,object:"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000",applet:!0},hasData:function(a){return a=a.nodeType?p.cache[a[p.expando]]:a[p.expando],!!a&&!K(a)},data:function(a,c,d,e){if(!p.acceptData(a))return;var f,g,h=p.expando,i=typeof c=="string",j=a.nodeType,k=j?p.cache:a,l=j?a[h]:a[h]&&h;if((!l||!k[l]||!e&&!k[l].data)&&i&&d===b)return;l||(j?a[h]=l=p.deletedIds.pop()||p.guid++:l=h),k[l]||(k[l]={},j||(k[l].toJSON=p.noop));if(typeof c=="object"||typeof c=="function")e?k[l]=p.extend(k[l],c):k[l].data=p.extend(k[l].data,c);return f=k[l],e||(f.data||(f.data={}),f=f.data),d!==b&&(f[p.camelCase(c)]=d),i?(g=f[c],g==null&&(g=f[p.camelCase(c)])):g=f,g},removeData:function(a,b,c){if(!p.acceptData(a))return;var d,e,f,g=a.nodeType,h=g?p.cache:a,i=g?a[p.expando]:p.expando;if(!h[i])return;if(b){d=c?h[i]:h[i].data;if(d){p.isArray(b)||(b in d?b=[b]:(b=p.camelCase(b),b in d?b=[b]:b=b.split(" ")));for(e=0,f=b.length;e1,null,!1))},removeData:function(a){return this.each(function(){p.removeData(this,a)})}}),p.extend({queue:function(a,b,c){var d;if(a)return b=(b||"fx")+"queue",d=p._data(a,b),c&&(!d||p.isArray(c)?d=p._data(a,b,p.makeArray(c)):d.push(c)),d||[]},dequeue:function(a,b){b=b||"fx";var c=p.queue(a,b),d=c.length,e=c.shift(),f=p._queueHooks(a,b),g=function(){p.dequeue(a,b)};e==="inprogress"&&(e=c.shift(),d--),e&&(b==="fx"&&c.unshift("inprogress"),delete f.stop,e.call(a,g,f)),!d&&f&&f.empty.fire()},_queueHooks:function(a,b){var c=b+"queueHooks";return p._data(a,c)||p._data(a,c,{empty:p.Callbacks("once memory").add(function(){p.removeData(a,b+"queue",!0),p.removeData(a,c,!0)})})}}),p.fn.extend({queue:function(a,c){var d=2;return typeof a!="string"&&(c=a,a="fx",d--),arguments.length1)},removeAttr:function(a){return this.each(function(){p.removeAttr(this,a)})},prop:function(a,b){return p.access(this,p.prop,a,b,arguments.length>1)},removeProp:function(a){return a=p.propFix[a]||a,this.each(function(){try{this[a]=b,delete this[a]}catch(c){}})},addClass:function(a){var b,c,d,e,f,g,h;if(p.isFunction(a))return this.each(function(b){p(this).addClass(a.call(this,b,this.className))});if(a&&typeof a=="string"){b=a.split(s);for(c=0,d=this.length;c=0)d=d.replace(" "+c[f]+" "," ");e.className=a?p.trim(d):""}}}return this},toggleClass:function(a,b){var c=typeof a,d=typeof b=="boolean";return p.isFunction(a)?this.each(function(c){p(this).toggleClass(a.call(this,c,this.className,b),b)}):this.each(function(){if(c==="string"){var e,f=0,g=p(this),h=b,i=a.split(s);while(e=i[f++])h=d?h:!g.hasClass(e),g[h?"addClass":"removeClass"](e)}else if(c==="undefined"||c==="boolean")this.className&&p._data(this,"__className__",this.className),this.className=this.className||a===!1?"":p._data(this,"__className__")||""})},hasClass:function(a){var b=" "+a+" ",c=0,d=this.length;for(;c=0)return!0;return!1},val:function(a){var c,d,e,f=this[0];if(!arguments.length){if(f)return c=p.valHooks[f.type]||p.valHooks[f.nodeName.toLowerCase()],c&&"get"in c&&(d=c.get(f,"value"))!==b?d:(d=f.value,typeof d=="string"?d.replace(P,""):d==null?"":d);return}return e=p.isFunction(a),this.each(function(d){var f,g=p(this);if(this.nodeType!==1)return;e?f=a.call(this,d,g.val()):f=a,f==null?f="":typeof f=="number"?f+="":p.isArray(f)&&(f=p.map(f,function(a){return a==null?"":a+""})),c=p.valHooks[this.type]||p.valHooks[this.nodeName.toLowerCase()];if(!c||!("set"in c)||c.set(this,f,"value")===b)this.value=f})}}),p.extend({valHooks:{option:{get:function(a){var b=a.attributes.value;return!b||b.specified?a.value:a.text}},select:{get:function(a){var b,c,d,e,f=a.selectedIndex,g=[],h=a.options,i=a.type==="select-one";if(f<0)return null;c=i?f:0,d=i?f+1:h.length;for(;c=0}),c.length||(a.selectedIndex=-1),c}}},attrFn:{},attr:function(a,c,d,e){var f,g,h,i=a.nodeType;if(!a||i===3||i===8||i===2)return;if(e&&p.isFunction(p.fn[c]))return p(a)[c](d);if(typeof a.getAttribute=="undefined")return p.prop(a,c,d);h=i!==1||!p.isXMLDoc(a),h&&(c=c.toLowerCase(),g=p.attrHooks[c]||(T.test(c)?M:L));if(d!==b){if(d===null){p.removeAttr(a,c);return}return g&&"set"in g&&h&&(f=g.set(a,d,c))!==b?f:(a.setAttribute(c,d+""),d)}return g&&"get"in g&&h&&(f=g.get(a,c))!==null?f:(f=a.getAttribute(c),f===null?b:f)},removeAttr:function(a,b){var c,d,e,f,g=0;if(b&&a.nodeType===1){d=b.split(s);for(;g=0}})});var V=/^(?:textarea|input|select)$/i,W=/^([^\.]*|)(?:\.(.+)|)$/,X=/(?:^|\s)hover(\.\S+|)\b/,Y=/^key/,Z=/^(?:mouse|contextmenu)|click/,$=/^(?:focusinfocus|focusoutblur)$/,_=function(a){return p.event.special.hover?a:a.replace(X,"mouseenter$1 mouseleave$1")};p.event={add:function(a,c,d,e,f){var g,h,i,j,k,l,m,n,o,q,r;if(a.nodeType===3||a.nodeType===8||!c||!d||!(g=p._data(a)))return;d.handler&&(o=d,d=o.handler,f=o.selector),d.guid||(d.guid=p.guid++),i=g.events,i||(g.events=i={}),h=g.handle,h||(g.handle=h=function(a){return typeof p!="undefined"&&(!a||p.event.triggered!==a.type)?p.event.dispatch.apply(h.elem,arguments):b},h.elem=a),c=p.trim(_(c)).split(" ");for(j=0;j=0&&(s=s.slice(0,-1),i=!0),s.indexOf(".")>=0&&(t=s.split("."),s=t.shift(),t.sort());if((!f||p.event.customEvent[s])&&!p.event.global[s])return;c=typeof c=="object"?c[p.expando]?c:new p.Event(s,c):new p.Event(s),c.type=s,c.isTrigger=!0,c.exclusive=i,c.namespace=t.join("."),c.namespace_re=c.namespace?new RegExp("(^|\\.)"+t.join("\\.(?:.*\\.|)")+"(\\.|$)"):null,m=s.indexOf(":")<0?"on"+s:"";if(!f){h=p.cache;for(j in h)h[j].events&&h[j].events[s]&&p.event.trigger(c,d,h[j].handle.elem,!0);return}c.result=b,c.target||(c.target=f),d=d!=null?p.makeArray(d):[],d.unshift(c),n=p.event.special[s]||{};if(n.trigger&&n.trigger.apply(f,d)===!1)return;q=[[f,n.bindType||s]];if(!g&&!n.noBubble&&!p.isWindow(f)){r=n.delegateType||s,k=$.test(r+s)?f:f.parentNode;for(l=f;k;k=k.parentNode)q.push([k,r]),l=k;l===(f.ownerDocument||e)&&q.push([l.defaultView||l.parentWindow||a,r])}for(j=0;j=0:p.find(m,this,null,[f]).length),h[m]&&j.push(l);j.length&&u.push({elem:f,matches:j})}o.length>q&&u.push({elem:this,matches:o.slice(q)});for(d=0;d0?this.on(b,null,a,c):this.trigger(b)},Y.test(b)&&(p.event.fixHooks[b]=p.event.keyHooks),Z.test(b)&&(p.event.fixHooks[b]=p.event.mouseHooks)}),function(a,b){function bc(a,b,c,d){c=c||[],b=b||r;var e,f,i,j,k=b.nodeType;if(!a||typeof a!="string")return c;if(k!==1&&k!==9)return[];i=g(b);if(!i&&!d)if(e=P.exec(a))if(j=e[1]){if(k===9){f=b.getElementById(j);if(!f||!f.parentNode)return c;if(f.id===j)return c.push(f),c}else if(b.ownerDocument&&(f=b.ownerDocument.getElementById(j))&&h(b,f)&&f.id===j)return c.push(f),c}else{if(e[2])return w.apply(c,x.call(b.getElementsByTagName(a),0)),c;if((j=e[3])&&_&&b.getElementsByClassName)return w.apply(c,x.call(b.getElementsByClassName(j),0)),c}return bp(a.replace(L,"$1"),b,c,d,i)}function bd(a){return function(b){var c=b.nodeName.toLowerCase();return c==="input"&&b.type===a}}function be(a){return function(b){var c=b.nodeName.toLowerCase();return(c==="input"||c==="button")&&b.type===a}}function bf(a){return z(function(b){return b=+b,z(function(c,d){var e,f=a([],c.length,b),g=f.length;while(g--)c[e=f[g]]&&(c[e]=!(d[e]=c[e]))})})}function bg(a,b,c){if(a===b)return c;var d=a.nextSibling;while(d){if(d===b)return-1;d=d.nextSibling}return 1}function bh(a,b){var c,d,f,g,h,i,j,k=C[o][a];if(k)return b?0:k.slice(0);h=a,i=[],j=e.preFilter;while(h){if(!c||(d=M.exec(h)))d&&(h=h.slice(d[0].length)),i.push(f=[]);c=!1;if(d=N.exec(h))f.push(c=new q(d.shift())),h=h.slice(c.length),c.type=d[0].replace(L," ");for(g in e.filter)(d=W[g].exec(h))&&(!j[g]||(d=j[g](d,r,!0)))&&(f.push(c=new q(d.shift())),h=h.slice(c.length),c.type=g,c.matches=d);if(!c)break}return b?h.length:h?bc.error(a):C(a,i).slice(0)}function bi(a,b,d){var e=b.dir,f=d&&b.dir==="parentNode",g=u++;return b.first?function(b,c,d){while(b=b[e])if(f||b.nodeType===1)return a(b,c,d)}:function(b,d,h){if(!h){var i,j=t+" "+g+" ",k=j+c;while(b=b[e])if(f||b.nodeType===1){if((i=b[o])===k)return b.sizset;if(typeof i=="string"&&i.indexOf(j)===0){if(b.sizset)return b}else{b[o]=k;if(a(b,d,h))return b.sizset=!0,b;b.sizset=!1}}}else while(b=b[e])if(f||b.nodeType===1)if(a(b,d,h))return b}}function bj(a){return a.length>1?function(b,c,d){var e=a.length;while(e--)if(!a[e](b,c,d))return!1;return!0}:a[0]}function bk(a,b,c,d,e){var f,g=[],h=0,i=a.length,j=b!=null;for(;h-1},h,!0),m=[function(a,c,d){return!g&&(d||c!==l)||((b=c).nodeType?j(a,c,d):k(a,c,d))}];for(;i1&&bj(m),i>1&&a.slice(0,i-1).join("").replace(L,"$1"),c,i0,f=a.length>0,g=function(h,i,j,k,m){var n,o,p,q=[],s=0,u="0",x=h&&[],y=m!=null,z=l,A=h||f&&e.find.TAG("*",m&&i.parentNode||i),B=t+=z==null?1:Math.E;y&&(l=i!==r&&i,c=g.el);for(;(n=A[u])!=null;u++){if(f&&n){for(o=0;p=a[o];o++)if(p(n,i,j)){k.push(n);break}y&&(t=B,c=++g.el)}d&&((n=!p&&n)&&s--,h&&x.push(n))}s+=u;if(d&&u!==s){for(o=0;p=b[o];o++)p(x,q,i,j);if(h){if(s>0)while(u--)!x[u]&&!q[u]&&(q[u]=v.call(k));q=bk(q)}w.apply(k,q),y&&!h&&q.length>0&&s+b.length>1&&bc.uniqueSort(k)}return y&&(t=B,l=z),x};return g.el=0,d?z(g):g}function bo(a,b,c,d){var e=0,f=b.length;for(;e2&&(j=h[0]).type==="ID"&&b.nodeType===9&&!f&&e.relative[h[1].type]){b=e.find.ID(j.matches[0].replace(V,""),b,f)[0];if(!b)return c;a=a.slice(h.shift().length)}for(g=W.POS.test(a)?-1:h.length-1;g>=0;g--){j=h[g];if(e.relative[k=j.type])break;if(l=e.find[k])if(d=l(j.matches[0].replace(V,""),R.test(h[0].type)&&b.parentNode||b,f)){h.splice(g,1),a=d.length&&h.join("");if(!a)return w.apply(c,x.call(d,0)),c;break}}}return i(a,m)(d,b,f,c,R.test(a)),c}function bq(){}var c,d,e,f,g,h,i,j,k,l,m=!0,n="undefined",o=("sizcache"+Math.random()).replace(".",""),q=String,r=a.document,s=r.documentElement,t=0,u=0,v=[].pop,w=[].push,x=[].slice,y=[].indexOf||function(a){var b=0,c=this.length;for(;be.cacheLength&&delete a[b.shift()],a[c]=d},a)},B=A(),C=A(),D=A(),E="[\\x20\\t\\r\\n\\f]",F="(?:\\\\.|[-\\w]|[^\\x00-\\xa0])+",G=F.replace("w","w#"),H="([*^$|!~]?=)",I="\\["+E+"*("+F+")"+E+"*(?:"+H+E+"*(?:(['\"])((?:\\\\.|[^\\\\])*?)\\3|("+G+")|)|)"+E+"*\\]",J=":("+F+")(?:\\((?:(['\"])((?:\\\\.|[^\\\\])*?)\\2|([^()[\\]]*|(?:(?:"+I+")|[^:]|\\\\.)*|.*))\\)|)",K=":(even|odd|eq|gt|lt|nth|first|last)(?:\\("+E+"*((?:-\\d)?\\d*)"+E+"*\\)|)(?=[^-]|$)",L=new RegExp("^"+E+"+|((?:^|[^\\\\])(?:\\\\.)*)"+E+"+$","g"),M=new RegExp("^"+E+"*,"+E+"*"),N=new RegExp("^"+E+"*([\\x20\\t\\r\\n\\f>+~])"+E+"*"),O=new RegExp(J),P=/^(?:#([\w\-]+)|(\w+)|\.([\w\-]+))$/,Q=/^:not/,R=/[\x20\t\r\n\f]*[+~]/,S=/:not\($/,T=/h\d/i,U=/input|select|textarea|button/i,V=/\\(?!\\)/g,W={ID:new RegExp("^#("+F+")"),CLASS:new RegExp("^\\.("+F+")"),NAME:new RegExp("^\\[name=['\"]?("+F+")['\"]?\\]"),TAG:new RegExp("^("+F.replace("w","w*")+")"),ATTR:new RegExp("^"+I),PSEUDO:new RegExp("^"+J),POS:new RegExp(K,"i"),CHILD:new RegExp("^:(only|nth|first|last)-child(?:\\("+E+"*(even|odd|(([+-]|)(\\d*)n|)"+E+"*(?:([+-]|)"+E+"*(\\d+)|))"+E+"*\\)|)","i"),needsContext:new RegExp("^"+E+"*[>+~]|"+K,"i")},X=function(a){var b=r.createElement("div");try{return a(b)}catch(c){return!1}finally{b=null}},Y=X(function(a){return a.appendChild(r.createComment("")),!a.getElementsByTagName("*").length}),Z=X(function(a){return a.innerHTML="",a.firstChild&&typeof a.firstChild.getAttribute!==n&&a.firstChild.getAttribute("href")==="#"}),$=X(function(a){a.innerHTML="";var b=typeof a.lastChild.getAttribute("multiple");return b!=="boolean"&&b!=="string"}),_=X(function(a){return a.innerHTML="",!a.getElementsByClassName||!a.getElementsByClassName("e").length?!1:(a.lastChild.className="e",a.getElementsByClassName("e").length===2)}),ba=X(function(a){a.id=o+0,a.innerHTML="
        ",s.insertBefore(a,s.firstChild);var b=r.getElementsByName&&r.getElementsByName(o).length===2+r.getElementsByName(o+0).length;return d=!r.getElementById(o),s.removeChild(a),b});try{x.call(s.childNodes,0)[0].nodeType}catch(bb){x=function(a){var b,c=[];for(;b=this[a];a++)c.push(b);return c}}bc.matches=function(a,b){return bc(a,null,null,b)},bc.matchesSelector=function(a,b){return bc(b,null,null,[a]).length>0},f=bc.getText=function(a){var b,c="",d=0,e=a.nodeType;if(e){if(e===1||e===9||e===11){if(typeof a.textContent=="string")return a.textContent;for(a=a.firstChild;a;a=a.nextSibling)c+=f(a)}else if(e===3||e===4)return a.nodeValue}else for(;b=a[d];d++)c+=f(b);return c},g=bc.isXML=function(a){var b=a&&(a.ownerDocument||a).documentElement;return b?b.nodeName!=="HTML":!1},h=bc.contains=s.contains?function(a,b){var c=a.nodeType===9?a.documentElement:a,d=b&&b.parentNode;return a===d||!!(d&&d.nodeType===1&&c.contains&&c.contains(d))}:s.compareDocumentPosition?function(a,b){return b&&!!(a.compareDocumentPosition(b)&16)}:function(a,b){while(b=b.parentNode)if(b===a)return!0;return!1},bc.attr=function(a,b){var c,d=g(a);return d||(b=b.toLowerCase()),(c=e.attrHandle[b])?c(a):d||$?a.getAttribute(b):(c=a.getAttributeNode(b),c?typeof a[b]=="boolean"?a[b]?b:null:c.specified?c.value:null:null)},e=bc.selectors={cacheLength:50,createPseudo:z,match:W,attrHandle:Z?{}:{href:function(a){return a.getAttribute("href",2)},type:function(a){return a.getAttribute("type")}},find:{ID:d?function(a,b,c){if(typeof b.getElementById!==n&&!c){var d=b.getElementById(a);return d&&d.parentNode?[d]:[]}}:function(a,c,d){if(typeof c.getElementById!==n&&!d){var e=c.getElementById(a);return e?e.id===a||typeof e.getAttributeNode!==n&&e.getAttributeNode("id").value===a?[e]:b:[]}},TAG:Y?function(a,b){if(typeof b.getElementsByTagName!==n)return b.getElementsByTagName(a)}:function(a,b){var c=b.getElementsByTagName(a);if(a==="*"){var d,e=[],f=0;for(;d=c[f];f++)d.nodeType===1&&e.push(d);return e}return c},NAME:ba&&function(a,b){if(typeof b.getElementsByName!==n)return b.getElementsByName(name)},CLASS:_&&function(a,b,c){if(typeof b.getElementsByClassName!==n&&!c)return b.getElementsByClassName(a)}},relative:{">":{dir:"parentNode",first:!0}," ":{dir:"parentNode"},"+":{dir:"previousSibling",first:!0},"~":{dir:"previousSibling"}},preFilter:{ATTR:function(a){return a[1]=a[1].replace(V,""),a[3]=(a[4]||a[5]||"").replace(V,""),a[2]==="~="&&(a[3]=" "+a[3]+" "),a.slice(0,4)},CHILD:function(a){return a[1]=a[1].toLowerCase(),a[1]==="nth"?(a[2]||bc.error(a[0]),a[3]=+(a[3]?a[4]+(a[5]||1):2*(a[2]==="even"||a[2]==="odd")),a[4]=+(a[6]+a[7]||a[2]==="odd")):a[2]&&bc.error(a[0]),a},PSEUDO:function(a){var b,c;if(W.CHILD.test(a[0]))return null;if(a[3])a[2]=a[3];else if(b=a[4])O.test(b)&&(c=bh(b,!0))&&(c=b.indexOf(")",b.length-c)-b.length)&&(b=b.slice(0,c),a[0]=a[0].slice(0,c)),a[2]=b;return a.slice(0,3)}},filter:{ID:d?function(a){return a=a.replace(V,""),function(b){return b.getAttribute("id")===a}}:function(a){return a=a.replace(V,""),function(b){var c=typeof b.getAttributeNode!==n&&b.getAttributeNode("id");return c&&c.value===a}},TAG:function(a){return a==="*"?function(){return!0}:(a=a.replace(V,"").toLowerCase(),function(b){return b.nodeName&&b.nodeName.toLowerCase()===a})},CLASS:function(a){var b=B[o][a];return b||(b=B(a,new RegExp("(^|"+E+")"+a+"("+E+"|$)"))),function(a){return b.test(a.className||typeof a.getAttribute!==n&&a.getAttribute("class")||"")}},ATTR:function(a,b,c){return function(d,e){var f=bc.attr(d,a);return f==null?b==="!=":b?(f+="",b==="="?f===c:b==="!="?f!==c:b==="^="?c&&f.indexOf(c)===0:b==="*="?c&&f.indexOf(c)>-1:b==="$="?c&&f.substr(f.length-c.length)===c:b==="~="?(" "+f+" ").indexOf(c)>-1:b==="|="?f===c||f.substr(0,c.length+1)===c+"-":!1):!0}},CHILD:function(a,b,c,d){return a==="nth"?function(a){var b,e,f=a.parentNode;if(c===1&&d===0)return!0;if(f){e=0;for(b=f.firstChild;b;b=b.nextSibling)if(b.nodeType===1){e++;if(a===b)break}}return e-=d,e===c||e%c===0&&e/c>=0}:function(b){var c=b;switch(a){case"only":case"first":while(c=c.previousSibling)if(c.nodeType===1)return!1;if(a==="first")return!0;c=b;case"last":while(c=c.nextSibling)if(c.nodeType===1)return!1;return!0}}},PSEUDO:function(a,b){var c,d=e.pseudos[a]||e.setFilters[a.toLowerCase()]||bc.error("unsupported pseudo: "+a);return d[o]?d(b):d.length>1?(c=[a,a,"",b],e.setFilters.hasOwnProperty(a.toLowerCase())?z(function(a,c){var e,f=d(a,b),g=f.length;while(g--)e=y.call(a,f[g]),a[e]=!(c[e]=f[g])}):function(a){return d(a,0,c)}):d}},pseudos:{not:z(function(a){var b=[],c=[],d=i(a.replace(L,"$1"));return d[o]?z(function(a,b,c,e){var f,g=d(a,null,e,[]),h=a.length;while(h--)if(f=g[h])a[h]=!(b[h]=f)}):function(a,e,f){return b[0]=a,d(b,null,f,c),!c.pop()}}),has:z(function(a){return function(b){return bc(a,b).length>0}}),contains:z(function(a){return function(b){return(b.textContent||b.innerText||f(b)).indexOf(a)>-1}}),enabled:function(a){return a.disabled===!1},disabled:function(a){return a.disabled===!0},checked:function(a){var b=a.nodeName.toLowerCase();return b==="input"&&!!a.checked||b==="option"&&!!a.selected},selected:function(a){return a.parentNode&&a.parentNode.selectedIndex,a.selected===!0},parent:function(a){return!e.pseudos.empty(a)},empty:function(a){var b;a=a.firstChild;while(a){if(a.nodeName>"@"||(b=a.nodeType)===3||b===4)return!1;a=a.nextSibling}return!0},header:function(a){return T.test(a.nodeName)},text:function(a){var b,c;return a.nodeName.toLowerCase()==="input"&&(b=a.type)==="text"&&((c=a.getAttribute("type"))==null||c.toLowerCase()===b)},radio:bd("radio"),checkbox:bd("checkbox"),file:bd("file"),password:bd("password"),image:bd("image"),submit:be("submit"),reset:be("reset"),button:function(a){var b=a.nodeName.toLowerCase();return b==="input"&&a.type==="button"||b==="button"},input:function(a){return U.test(a.nodeName)},focus:function(a){var b=a.ownerDocument;return a===b.activeElement&&(!b.hasFocus||b.hasFocus())&&(!!a.type||!!a.href)},active:function(a){return a===a.ownerDocument.activeElement},first:bf(function(a,b,c){return[0]}),last:bf(function(a,b,c){return[b-1]}),eq:bf(function(a,b,c){return[c<0?c+b:c]}),even:bf(function(a,b,c){for(var d=0;d=0;)a.push(d);return a}),gt:bf(function(a,b,c){for(var d=c<0?c+b:c;++d",a.querySelectorAll("[selected]").length||e.push("\\["+E+"*(?:checked|disabled|ismap|multiple|readonly|selected|value)"),a.querySelectorAll(":checked").length||e.push(":checked")}),X(function(a){a.innerHTML="

        ",a.querySelectorAll("[test^='']").length&&e.push("[*^$]="+E+"*(?:\"\"|'')"),a.innerHTML="",a.querySelectorAll(":enabled").length||e.push(":enabled",":disabled")}),e=new RegExp(e.join("|")),bp=function(a,d,f,g,h){if(!g&&!h&&(!e||!e.test(a))){var i,j,k=!0,l=o,m=d,n=d.nodeType===9&&a;if(d.nodeType===1&&d.nodeName.toLowerCase()!=="object"){i=bh(a),(k=d.getAttribute("id"))?l=k.replace(c,"\\$&"):d.setAttribute("id",l),l="[id='"+l+"'] ",j=i.length;while(j--)i[j]=l+i[j].join("");m=R.test(a)&&d.parentNode||d,n=i.join(",")}if(n)try{return w.apply(f,x.call(m.querySelectorAll(n),0)),f}catch(p){}finally{k||d.removeAttribute("id")}}return b(a,d,f,g,h)},h&&(X(function(b){a=h.call(b,"div");try{h.call(b,"[test!='']:sizzle"),f.push("!=",J)}catch(c){}}),f=new RegExp(f.join("|")),bc.matchesSelector=function(b,c){c=c.replace(d,"='$1']");if(!g(b)&&!f.test(c)&&(!e||!e.test(c)))try{var i=h.call(b,c);if(i||a||b.document&&b.document.nodeType!==11)return i}catch(j){}return bc(c,null,null,[b]).length>0})}(),e.pseudos.nth=e.pseudos.eq,e.filters=bq.prototype=e.pseudos,e.setFilters=new bq,bc.attr=p.attr,p.find=bc,p.expr=bc.selectors,p.expr[":"]=p.expr.pseudos,p.unique=bc.uniqueSort,p.text=bc.getText,p.isXMLDoc=bc.isXML,p.contains=bc.contains}(a);var bc=/Until$/,bd=/^(?:parents|prev(?:Until|All))/,be=/^.[^:#\[\.,]*$/,bf=p.expr.match.needsContext,bg={children:!0,contents:!0,next:!0,prev:!0};p.fn.extend({find:function(a){var b,c,d,e,f,g,h=this;if(typeof a!="string")return p(a).filter(function(){for(b=0,c=h.length;b0)for(e=d;e=0:p.filter(a,this).length>0:this.filter(a).length>0)},closest:function(a,b){var c,d=0,e=this.length,f=[],g=bf.test(a)||typeof a!="string"?p(a,b||this.context):0;for(;d-1:p.find.matchesSelector(c,a)){f.push(c);break}c=c.parentNode}}return f=f.length>1?p.unique(f):f,this.pushStack(f,"closest",a)},index:function(a){return a?typeof a=="string"?p.inArray(this[0],p(a)):p.inArray(a.jquery?a[0]:a,this):this[0]&&this[0].parentNode?this.prevAll().length:-1},add:function(a,b){var c=typeof a=="string"?p(a,b):p.makeArray(a&&a.nodeType?[a]:a),d=p.merge(this.get(),c);return this.pushStack(bh(c[0])||bh(d[0])?d:p.unique(d))},addBack:function(a){return this.add(a==null?this.prevObject:this.prevObject.filter(a))}}),p.fn.andSelf=p.fn.addBack,p.each({parent:function(a){var b=a.parentNode;return b&&b.nodeType!==11?b:null},parents:function(a){return p.dir(a,"parentNode")},parentsUntil:function(a,b,c){return p.dir(a,"parentNode",c)},next:function(a){return bi(a,"nextSibling")},prev:function(a){return bi(a,"previousSibling")},nextAll:function(a){return p.dir(a,"nextSibling")},prevAll:function(a){return p.dir(a,"previousSibling")},nextUntil:function(a,b,c){return p.dir(a,"nextSibling",c)},prevUntil:function(a,b,c){return p.dir(a,"previousSibling",c)},siblings:function(a){return p.sibling((a.parentNode||{}).firstChild,a)},children:function(a){return p.sibling(a.firstChild)},contents:function(a){return p.nodeName(a,"iframe")?a.contentDocument||a.contentWindow.document:p.merge([],a.childNodes)}},function(a,b){p.fn[a]=function(c,d){var e=p.map(this,b,c);return bc.test(a)||(d=c),d&&typeof d=="string"&&(e=p.filter(d,e)),e=this.length>1&&!bg[a]?p.unique(e):e,this.length>1&&bd.test(a)&&(e=e.reverse()),this.pushStack(e,a,k.call(arguments).join(","))}}),p.extend({filter:function(a,b,c){return c&&(a=":not("+a+")"),b.length===1?p.find.matchesSelector(b[0],a)?[b[0]]:[]:p.find.matches(a,b)},dir:function(a,c,d){var e=[],f=a[c];while(f&&f.nodeType!==9&&(d===b||f.nodeType!==1||!p(f).is(d)))f.nodeType===1&&e.push(f),f=f[c];return e},sibling:function(a,b){var c=[];for(;a;a=a.nextSibling)a.nodeType===1&&a!==b&&c.push(a);return c}});var bl="abbr|article|aside|audio|bdi|canvas|data|datalist|details|figcaption|figure|footer|header|hgroup|mark|meter|nav|output|progress|section|summary|time|video",bm=/ jQuery\d+="(?:null|\d+)"/g,bn=/^\s+/,bo=/<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi,bp=/<([\w:]+)/,bq=/
  • ","
    "],tr:[2,"","
    "],td:[3,"","
    "],col:[2,"","
    "],area:[1,"",""],_default:[0,"",""]},bA=bk(e),bB=bA.appendChild(e.createElement("div"));bz.optgroup=bz.option,bz.tbody=bz.tfoot=bz.colgroup=bz.caption=bz.thead,bz.th=bz.td,p.support.htmlSerialize||(bz._default=[1,"X
    ","
    "]),p.fn.extend({text:function(a){return p.access(this,function(a){return a===b?p.text(this):this.empty().append((this[0]&&this[0].ownerDocument||e).createTextNode(a))},null,a,arguments.length)},wrapAll:function(a){if(p.isFunction(a))return this.each(function(b){p(this).wrapAll(a.call(this,b))});if(this[0]){var b=p(a,this[0].ownerDocument).eq(0).clone(!0);this[0].parentNode&&b.insertBefore(this[0]),b.map(function(){var a=this;while(a.firstChild&&a.firstChild.nodeType===1)a=a.firstChild;return a}).append(this)}return this},wrapInner:function(a){return p.isFunction(a)?this.each(function(b){p(this).wrapInner(a.call(this,b))}):this.each(function(){var b=p(this),c=b.contents();c.length?c.wrapAll(a):b.append(a)})},wrap:function(a){var b=p.isFunction(a);return this.each(function(c){p(this).wrapAll(b?a.call(this,c):a)})},unwrap:function(){return this.parent().each(function(){p.nodeName(this,"body")||p(this).replaceWith(this.childNodes)}).end()},append:function(){return this.domManip(arguments,!0,function(a){(this.nodeType===1||this.nodeType===11)&&this.appendChild(a)})},prepend:function(){return this.domManip(arguments,!0,function(a){(this.nodeType===1||this.nodeType===11)&&this.insertBefore(a,this.firstChild)})},before:function(){if(!bh(this[0]))return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this)});if(arguments.length){var a=p.clean(arguments);return this.pushStack(p.merge(a,this),"before",this.selector)}},after:function(){if(!bh(this[0]))return this.domManip(arguments,!1,function(a){this.parentNode.insertBefore(a,this.nextSibling)});if(arguments.length){var a=p.clean(arguments);return this.pushStack(p.merge(this,a),"after",this.selector)}},remove:function(a,b){var c,d=0;for(;(c=this[d])!=null;d++)if(!a||p.filter(a,[c]).length)!b&&c.nodeType===1&&(p.cleanData(c.getElementsByTagName("*")),p.cleanData([c])),c.parentNode&&c.parentNode.removeChild(c);return this},empty:function(){var a,b=0;for(;(a=this[b])!=null;b++){a.nodeType===1&&p.cleanData(a.getElementsByTagName("*"));while(a.firstChild)a.removeChild(a.firstChild)}return this},clone:function(a,b){return a=a==null?!1:a,b=b==null?a:b,this.map(function(){return p.clone(this,a,b)})},html:function(a){return p.access(this,function(a){var c=this[0]||{},d=0,e=this.length;if(a===b)return c.nodeType===1?c.innerHTML.replace(bm,""):b;if(typeof a=="string"&&!bs.test(a)&&(p.support.htmlSerialize||!bu.test(a))&&(p.support.leadingWhitespace||!bn.test(a))&&!bz[(bp.exec(a)||["",""])[1].toLowerCase()]){a=a.replace(bo,"<$1>");try{for(;d1&&typeof j=="string"&&bw.test(j))return this.each(function(){p(this).domManip(a,c,d)});if(p.isFunction(j))return this.each(function(e){var f=p(this);a[0]=j.call(this,e,c?f.html():b),f.domManip(a,c,d)});if(this[0]){e=p.buildFragment(a,this,k),g=e.fragment,f=g.firstChild,g.childNodes.length===1&&(g=f);if(f){c=c&&p.nodeName(f,"tr");for(h=e.cacheable||l-1;i0?this.clone(!0):this).get(),p(g[e])[b](d),f=f.concat(d);return this.pushStack(f,a,g.selector)}}),p.extend({clone:function(a,b,c){var d,e,f,g;p.support.html5Clone||p.isXMLDoc(a)||!bu.test("<"+a.nodeName+">")?g=a.cloneNode(!0):(bB.innerHTML=a.outerHTML,bB.removeChild(g=bB.firstChild));if((!p.support.noCloneEvent||!p.support.noCloneChecked)&&(a.nodeType===1||a.nodeType===11)&&!p.isXMLDoc(a)){bE(a,g),d=bF(a),e=bF(g);for(f=0;d[f];++f)e[f]&&bE(d[f],e[f])}if(b){bD(a,g);if(c){d=bF(a),e=bF(g);for(f=0;d[f];++f)bD(d[f],e[f])}}return d=e=null,g},clean:function(a,b,c,d){var f,g,h,i,j,k,l,m,n,o,q,r,s=b===e&&bA,t=[];if(!b||typeof b.createDocumentFragment=="undefined")b=e;for(f=0;(h=a[f])!=null;f++){typeof h=="number"&&(h+="");if(!h)continue;if(typeof h=="string")if(!br.test(h))h=b.createTextNode(h);else{s=s||bk(b),l=b.createElement("div"),s.appendChild(l),h=h.replace(bo,"<$1>"),i=(bp.exec(h)||["",""])[1].toLowerCase(),j=bz[i]||bz._default,k=j[0],l.innerHTML=j[1]+h+j[2];while(k--)l=l.lastChild;if(!p.support.tbody){m=bq.test(h),n=i==="table"&&!m?l.firstChild&&l.firstChild.childNodes:j[1]===""&&!m?l.childNodes:[];for(g=n.length-1;g>=0;--g)p.nodeName(n[g],"tbody")&&!n[g].childNodes.length&&n[g].parentNode.removeChild(n[g])}!p.support.leadingWhitespace&&bn.test(h)&&l.insertBefore(b.createTextNode(bn.exec(h)[0]),l.firstChild),h=l.childNodes,l.parentNode.removeChild(l)}h.nodeType?t.push(h):p.merge(t,h)}l&&(h=l=s=null);if(!p.support.appendChecked)for(f=0;(h=t[f])!=null;f++)p.nodeName(h,"input")?bG(h):typeof h.getElementsByTagName!="undefined"&&p.grep(h.getElementsByTagName("input"),bG);if(c){q=function(a){if(!a.type||bx.test(a.type))return d?d.push(a.parentNode?a.parentNode.removeChild(a):a):c.appendChild(a)};for(f=0;(h=t[f])!=null;f++)if(!p.nodeName(h,"script")||!q(h))c.appendChild(h),typeof h.getElementsByTagName!="undefined"&&(r=p.grep(p.merge([],h.getElementsByTagName("script")),q),t.splice.apply(t,[f+1,0].concat(r)),f+=r.length)}return t},cleanData:function(a,b){var c,d,e,f,g=0,h=p.expando,i=p.cache,j=p.support.deleteExpando,k=p.event.special;for(;(e=a[g])!=null;g++)if(b||p.acceptData(e)){d=e[h],c=d&&i[d];if(c){if(c.events)for(f in c.events)k[f]?p.event.remove(e,f):p.removeEvent(e,f,c.handle);i[d]&&(delete i[d],j?delete e[h]:e.removeAttribute?e.removeAttribute(h):e[h]=null,p.deletedIds.push(d))}}}}),function(){var a,b;p.uaMatch=function(a){a=a.toLowerCase();var b=/(chrome)[ \/]([\w.]+)/.exec(a)||/(webkit)[ \/]([\w.]+)/.exec(a)||/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(a)||/(msie) ([\w.]+)/.exec(a)||a.indexOf("compatible")<0&&/(mozilla)(?:.*? rv:([\w.]+)|)/.exec(a)||[];return{browser:b[1]||"",version:b[2]||"0"}},a=p.uaMatch(g.userAgent),b={},a.browser&&(b[a.browser]=!0,b.version=a.version),b.chrome?b.webkit=!0:b.webkit&&(b.safari=!0),p.browser=b,p.sub=function(){function a(b,c){return new a.fn.init(b,c)}p.extend(!0,a,this),a.superclass=this,a.fn=a.prototype=this(),a.fn.constructor=a,a.sub=this.sub,a.fn.init=function c(c,d){return d&&d instanceof p&&!(d instanceof a)&&(d=a(d)),p.fn.init.call(this,c,d,b)},a.fn.init.prototype=a.fn;var b=a(e);return a}}();var bH,bI,bJ,bK=/alpha\([^)]*\)/i,bL=/opacity=([^)]*)/,bM=/^(top|right|bottom|left)$/,bN=/^(none|table(?!-c[ea]).+)/,bO=/^margin/,bP=new RegExp("^("+q+")(.*)$","i"),bQ=new RegExp("^("+q+")(?!px)[a-z%]+$","i"),bR=new RegExp("^([-+])=("+q+")","i"),bS={},bT={position:"absolute",visibility:"hidden",display:"block"},bU={letterSpacing:0,fontWeight:400},bV=["Top","Right","Bottom","Left"],bW=["Webkit","O","Moz","ms"],bX=p.fn.toggle;p.fn.extend({css:function(a,c){return p.access(this,function(a,c,d){return d!==b?p.style(a,c,d):p.css(a,c)},a,c,arguments.length>1)},show:function(){return b$(this,!0)},hide:function(){return b$(this)},toggle:function(a,b){var c=typeof a=="boolean";return p.isFunction(a)&&p.isFunction(b)?bX.apply(this,arguments):this.each(function(){(c?a:bZ(this))?p(this).show():p(this).hide()})}}),p.extend({cssHooks:{opacity:{get:function(a,b){if(b){var c=bH(a,"opacity");return c===""?"1":c}}}},cssNumber:{fillOpacity:!0,fontWeight:!0,lineHeight:!0,opacity:!0,orphans:!0,widows:!0,zIndex:!0,zoom:!0},cssProps:{"float":p.support.cssFloat?"cssFloat":"styleFloat"},style:function(a,c,d,e){if(!a||a.nodeType===3||a.nodeType===8||!a.style)return;var f,g,h,i=p.camelCase(c),j=a.style;c=p.cssProps[i]||(p.cssProps[i]=bY(j,i)),h=p.cssHooks[c]||p.cssHooks[i];if(d===b)return h&&"get"in h&&(f=h.get(a,!1,e))!==b?f:j[c];g=typeof d,g==="string"&&(f=bR.exec(d))&&(d=(f[1]+1)*f[2]+parseFloat(p.css(a,c)),g="number");if(d==null||g==="number"&&isNaN(d))return;g==="number"&&!p.cssNumber[i]&&(d+="px");if(!h||!("set"in h)||(d=h.set(a,d,e))!==b)try{j[c]=d}catch(k){}},css:function(a,c,d,e){var f,g,h,i=p.camelCase(c);return c=p.cssProps[i]||(p.cssProps[i]=bY(a.style,i)),h=p.cssHooks[c]||p.cssHooks[i],h&&"get"in h&&(f=h.get(a,!0,e)),f===b&&(f=bH(a,c)),f==="normal"&&c in bU&&(f=bU[c]),d||e!==b?(g=parseFloat(f),d||p.isNumeric(g)?g||0:f):f},swap:function(a,b,c){var d,e,f={};for(e in b)f[e]=a.style[e],a.style[e]=b[e];d=c.call(a);for(e in b)a.style[e]=f[e];return d}}),a.getComputedStyle?bH=function(b,c){var d,e,f,g,h=a.getComputedStyle(b,null),i=b.style;return h&&(d=h[c],d===""&&!p.contains(b.ownerDocument,b)&&(d=p.style(b,c)),bQ.test(d)&&bO.test(c)&&(e=i.width,f=i.minWidth,g=i.maxWidth,i.minWidth=i.maxWidth=i.width=d,d=h.width,i.width=e,i.minWidth=f,i.maxWidth=g)),d}:e.documentElement.currentStyle&&(bH=function(a,b){var c,d,e=a.currentStyle&&a.currentStyle[b],f=a.style;return e==null&&f&&f[b]&&(e=f[b]),bQ.test(e)&&!bM.test(b)&&(c=f.left,d=a.runtimeStyle&&a.runtimeStyle.left,d&&(a.runtimeStyle.left=a.currentStyle.left),f.left=b==="fontSize"?"1em":e,e=f.pixelLeft+"px",f.left=c,d&&(a.runtimeStyle.left=d)),e===""?"auto":e}),p.each(["height","width"],function(a,b){p.cssHooks[b]={get:function(a,c,d){if(c)return a.offsetWidth===0&&bN.test(bH(a,"display"))?p.swap(a,bT,function(){return cb(a,b,d)}):cb(a,b,d)},set:function(a,c,d){return b_(a,c,d?ca(a,b,d,p.support.boxSizing&&p.css(a,"boxSizing")==="border-box"):0)}}}),p.support.opacity||(p.cssHooks.opacity={get:function(a,b){return bL.test((b&&a.currentStyle?a.currentStyle.filter:a.style.filter)||"")?.01*parseFloat(RegExp.$1)+"":b?"1":""},set:function(a,b){var c=a.style,d=a.currentStyle,e=p.isNumeric(b)?"alpha(opacity="+b*100+")":"",f=d&&d.filter||c.filter||"";c.zoom=1;if(b>=1&&p.trim(f.replace(bK,""))===""&&c.removeAttribute){c.removeAttribute("filter");if(d&&!d.filter)return}c.filter=bK.test(f)?f.replace(bK,e):f+" "+e}}),p(function(){p.support.reliableMarginRight||(p.cssHooks.marginRight={get:function(a,b){return p.swap(a,{display:"inline-block"},function(){if(b)return bH(a,"marginRight")})}}),!p.support.pixelPosition&&p.fn.position&&p.each(["top","left"],function(a,b){p.cssHooks[b]={get:function(a,c){if(c){var d=bH(a,b);return bQ.test(d)?p(a).position()[b]+"px":d}}}})}),p.expr&&p.expr.filters&&(p.expr.filters.hidden=function(a){return a.offsetWidth===0&&a.offsetHeight===0||!p.support.reliableHiddenOffsets&&(a.style&&a.style.display||bH(a,"display"))==="none"},p.expr.filters.visible=function(a){return!p.expr.filters.hidden(a)}),p.each({margin:"",padding:"",border:"Width"},function(a,b){p.cssHooks[a+b]={expand:function(c){var d,e=typeof c=="string"?c.split(" "):[c],f={};for(d=0;d<4;d++)f[a+bV[d]+b]=e[d]||e[d-2]||e[0];return f}},bO.test(a)||(p.cssHooks[a+b].set=b_)});var cd=/%20/g,ce=/\[\]$/,cf=/\r?\n/g,cg=/^(?:color|date|datetime|datetime-local|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,ch=/^(?:select|textarea)/i;p.fn.extend({serialize:function(){return p.param(this.serializeArray())},serializeArray:function(){return this.map(function(){return this.elements?p.makeArray(this.elements):this}).filter(function(){return this.name&&!this.disabled&&(this.checked||ch.test(this.nodeName)||cg.test(this.type))}).map(function(a,b){var c=p(this).val();return c==null?null:p.isArray(c)?p.map(c,function(a,c){return{name:b.name,value:a.replace(cf,"\r\n")}}):{name:b.name,value:c.replace(cf,"\r\n")}}).get()}}),p.param=function(a,c){var d,e=[],f=function(a,b){b=p.isFunction(b)?b():b==null?"":b,e[e.length]=encodeURIComponent(a)+"="+encodeURIComponent(b)};c===b&&(c=p.ajaxSettings&&p.ajaxSettings.traditional);if(p.isArray(a)||a.jquery&&!p.isPlainObject(a))p.each(a,function(){f(this.name,this.value)});else for(d in a)ci(d,a[d],c,f);return e.join("&").replace(cd,"+")};var cj,ck,cl=/#.*$/,cm=/^(.*?):[ \t]*([^\r\n]*)\r?$/mg,cn=/^(?:about|app|app\-storage|.+\-extension|file|res|widget):$/,co=/^(?:GET|HEAD)$/,cp=/^\/\//,cq=/\?/,cr=/)<[^<]*)*<\/script>/gi,cs=/([?&])_=[^&]*/,ct=/^([\w\+\.\-]+:)(?:\/\/([^\/?#:]*)(?::(\d+)|)|)/,cu=p.fn.load,cv={},cw={},cx=["*/"]+["*"];try{ck=f.href}catch(cy){ck=e.createElement("a"),ck.href="",ck=ck.href}cj=ct.exec(ck.toLowerCase())||[],p.fn.load=function(a,c,d){if(typeof a!="string"&&cu)return cu.apply(this,arguments);if(!this.length)return this;var e,f,g,h=this,i=a.indexOf(" ");return i>=0&&(e=a.slice(i,a.length),a=a.slice(0,i)),p.isFunction(c)?(d=c,c=b):c&&typeof c=="object"&&(f="POST"),p.ajax({url:a,type:f,dataType:"html",data:c,complete:function(a,b){d&&h.each(d,g||[a.responseText,b,a])}}).done(function(a){g=arguments,h.html(e?p("
    ").append(a.replace(cr,"")).find(e):a)}),this},p.each("ajaxStart ajaxStop ajaxComplete ajaxError ajaxSuccess ajaxSend".split(" "),function(a,b){p.fn[b]=function(a){return this.on(b,a)}}),p.each(["get","post"],function(a,c){p[c]=function(a,d,e,f){return p.isFunction(d)&&(f=f||e,e=d,d=b),p.ajax({type:c,url:a,data:d,success:e,dataType:f})}}),p.extend({getScript:function(a,c){return p.get(a,b,c,"script")},getJSON:function(a,b,c){return p.get(a,b,c,"json")},ajaxSetup:function(a,b){return b?cB(a,p.ajaxSettings):(b=a,a=p.ajaxSettings),cB(a,b),a},ajaxSettings:{url:ck,isLocal:cn.test(cj[1]),global:!0,type:"GET",contentType:"application/x-www-form-urlencoded; charset=UTF-8",processData:!0,async:!0,accepts:{xml:"application/xml, text/xml",html:"text/html",text:"text/plain",json:"application/json, text/javascript","*":cx},contents:{xml:/xml/,html:/html/,json:/json/},responseFields:{xml:"responseXML",text:"responseText"},converters:{"* text":a.String,"text html":!0,"text json":p.parseJSON,"text xml":p.parseXML},flatOptions:{context:!0,url:!0}},ajaxPrefilter:cz(cv),ajaxTransport:cz(cw),ajax:function(a,c){function y(a,c,f,i){var k,s,t,u,w,y=c;if(v===2)return;v=2,h&&clearTimeout(h),g=b,e=i||"",x.readyState=a>0?4:0,f&&(u=cC(l,x,f));if(a>=200&&a<300||a===304)l.ifModified&&(w=x.getResponseHeader("Last-Modified"),w&&(p.lastModified[d]=w),w=x.getResponseHeader("Etag"),w&&(p.etag[d]=w)),a===304?(y="notmodified",k=!0):(k=cD(l,u),y=k.state,s=k.data,t=k.error,k=!t);else{t=y;if(!y||a)y="error",a<0&&(a=0)}x.status=a,x.statusText=(c||y)+"",k?o.resolveWith(m,[s,y,x]):o.rejectWith(m,[x,y,t]),x.statusCode(r),r=b,j&&n.trigger("ajax"+(k?"Success":"Error"),[x,l,k?s:t]),q.fireWith(m,[x,y]),j&&(n.trigger("ajaxComplete",[x,l]),--p.active||p.event.trigger("ajaxStop"))}typeof a=="object"&&(c=a,a=b),c=c||{};var d,e,f,g,h,i,j,k,l=p.ajaxSetup({},c),m=l.context||l,n=m!==l&&(m.nodeType||m instanceof p)?p(m):p.event,o=p.Deferred(),q=p.Callbacks("once memory"),r=l.statusCode||{},t={},u={},v=0,w="canceled",x={readyState:0,setRequestHeader:function(a,b){if(!v){var c=a.toLowerCase();a=u[c]=u[c]||a,t[a]=b}return this},getAllResponseHeaders:function(){return v===2?e:null},getResponseHeader:function(a){var c;if(v===2){if(!f){f={};while(c=cm.exec(e))f[c[1].toLowerCase()]=c[2]}c=f[a.toLowerCase()]}return c===b?null:c},overrideMimeType:function(a){return v||(l.mimeType=a),this},abort:function(a){return a=a||w,g&&g.abort(a),y(0,a),this}};o.promise(x),x.success=x.done,x.error=x.fail,x.complete=q.add,x.statusCode=function(a){if(a){var b;if(v<2)for(b in a)r[b]=[r[b],a[b]];else b=a[x.status],x.always(b)}return this},l.url=((a||l.url)+"").replace(cl,"").replace(cp,cj[1]+"//"),l.dataTypes=p.trim(l.dataType||"*").toLowerCase().split(s),l.crossDomain==null&&(i=ct.exec(l.url.toLowerCase())||!1,l.crossDomain=i&&i.join(":")+(i[3]?"":i[1]==="http:"?80:443)!==cj.join(":")+(cj[3]?"":cj[1]==="http:"?80:443)),l.data&&l.processData&&typeof l.data!="string"&&(l.data=p.param(l.data,l.traditional)),cA(cv,l,c,x);if(v===2)return x;j=l.global,l.type=l.type.toUpperCase(),l.hasContent=!co.test(l.type),j&&p.active++===0&&p.event.trigger("ajaxStart");if(!l.hasContent){l.data&&(l.url+=(cq.test(l.url)?"&":"?")+l.data,delete l.data),d=l.url;if(l.cache===!1){var z=p.now(),A=l.url.replace(cs,"$1_="+z);l.url=A+(A===l.url?(cq.test(l.url)?"&":"?")+"_="+z:"")}}(l.data&&l.hasContent&&l.contentType!==!1||c.contentType)&&x.setRequestHeader("Content-Type",l.contentType),l.ifModified&&(d=d||l.url,p.lastModified[d]&&x.setRequestHeader("If-Modified-Since",p.lastModified[d]),p.etag[d]&&x.setRequestHeader("If-None-Match",p.etag[d])),x.setRequestHeader("Accept",l.dataTypes[0]&&l.accepts[l.dataTypes[0]]?l.accepts[l.dataTypes[0]]+(l.dataTypes[0]!=="*"?", "+cx+"; q=0.01":""):l.accepts["*"]);for(k in l.headers)x.setRequestHeader(k,l.headers[k]);if(!l.beforeSend||l.beforeSend.call(m,x,l)!==!1&&v!==2){w="abort";for(k in{success:1,error:1,complete:1})x[k](l[k]);g=cA(cw,l,c,x);if(!g)y(-1,"No Transport");else{x.readyState=1,j&&n.trigger("ajaxSend",[x,l]),l.async&&l.timeout>0&&(h=setTimeout(function(){x.abort("timeout")},l.timeout));try{v=1,g.send(t,y)}catch(B){if(v<2)y(-1,B);else throw B}}return x}return x.abort()},active:0,lastModified:{},etag:{}});var cE=[],cF=/\?/,cG=/(=)\?(?=&|$)|\?\?/,cH=p.now();p.ajaxSetup({jsonp:"callback",jsonpCallback:function(){var a=cE.pop()||p.expando+"_"+cH++;return this[a]=!0,a}}),p.ajaxPrefilter("json jsonp",function(c,d,e){var f,g,h,i=c.data,j=c.url,k=c.jsonp!==!1,l=k&&cG.test(j),m=k&&!l&&typeof i=="string"&&!(c.contentType||"").indexOf("application/x-www-form-urlencoded")&&cG.test(i);if(c.dataTypes[0]==="jsonp"||l||m)return f=c.jsonpCallback=p.isFunction(c.jsonpCallback)?c.jsonpCallback():c.jsonpCallback,g=a[f],l?c.url=j.replace(cG,"$1"+f):m?c.data=i.replace(cG,"$1"+f):k&&(c.url+=(cF.test(j)?"&":"?")+c.jsonp+"="+f),c.converters["script json"]=function(){return h||p.error(f+" was not called"),h[0]},c.dataTypes[0]="json",a[f]=function(){h=arguments},e.always(function(){a[f]=g,c[f]&&(c.jsonpCallback=d.jsonpCallback,cE.push(f)),h&&p.isFunction(g)&&g(h[0]),h=g=b}),"script"}),p.ajaxSetup({accepts:{script:"text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"},contents:{script:/javascript|ecmascript/},converters:{"text script":function(a){return p.globalEval(a),a}}}),p.ajaxPrefilter("script",function(a){a.cache===b&&(a.cache=!1),a.crossDomain&&(a.type="GET",a.global=!1)}),p.ajaxTransport("script",function(a){if(a.crossDomain){var c,d=e.head||e.getElementsByTagName("head")[0]||e.documentElement;return{send:function(f,g){c=e.createElement("script"),c.async="async",a.scriptCharset&&(c.charset=a.scriptCharset),c.src=a.url,c.onload=c.onreadystatechange=function(a,e){if(e||!c.readyState||/loaded|complete/.test(c.readyState))c.onload=c.onreadystatechange=null,d&&c.parentNode&&d.removeChild(c),c=b,e||g(200,"success")},d.insertBefore(c,d.firstChild)},abort:function(){c&&c.onload(0,1)}}}});var cI,cJ=a.ActiveXObject?function(){for(var a in cI)cI[a](0,1)}:!1,cK=0;p.ajaxSettings.xhr=a.ActiveXObject?function(){return!this.isLocal&&cL()||cM()}:cL,function(a){p.extend(p.support,{ajax:!!a,cors:!!a&&"withCredentials"in a})}(p.ajaxSettings.xhr()),p.support.ajax&&p.ajaxTransport(function(c){if(!c.crossDomain||p.support.cors){var d;return{send:function(e,f){var g,h,i=c.xhr();c.username?i.open(c.type,c.url,c.async,c.username,c.password):i.open(c.type,c.url,c.async);if(c.xhrFields)for(h in c.xhrFields)i[h]=c.xhrFields[h];c.mimeType&&i.overrideMimeType&&i.overrideMimeType(c.mimeType),!c.crossDomain&&!e["X-Requested-With"]&&(e["X-Requested-With"]="XMLHttpRequest");try{for(h in e)i.setRequestHeader(h,e[h])}catch(j){}i.send(c.hasContent&&c.data||null),d=function(a,e){var h,j,k,l,m;try{if(d&&(e||i.readyState===4)){d=b,g&&(i.onreadystatechange=p.noop,cJ&&delete cI[g]);if(e)i.readyState!==4&&i.abort();else{h=i.status,k=i.getAllResponseHeaders(),l={},m=i.responseXML,m&&m.documentElement&&(l.xml=m);try{l.text=i.responseText}catch(a){}try{j=i.statusText}catch(n){j=""}!h&&c.isLocal&&!c.crossDomain?h=l.text?200:404:h===1223&&(h=204)}}}catch(o){e||f(-1,o)}l&&f(h,j,l,k)},c.async?i.readyState===4?setTimeout(d,0):(g=++cK,cJ&&(cI||(cI={},p(a).unload(cJ)),cI[g]=d),i.onreadystatechange=d):d()},abort:function(){d&&d(0,1)}}}});var cN,cO,cP=/^(?:toggle|show|hide)$/,cQ=new RegExp("^(?:([-+])=|)("+q+")([a-z%]*)$","i"),cR=/queueHooks$/,cS=[cY],cT={"*":[function(a,b){var c,d,e=this.createTween(a,b),f=cQ.exec(b),g=e.cur(),h=+g||0,i=1,j=20;if(f){c=+f[2],d=f[3]||(p.cssNumber[a]?"":"px");if(d!=="px"&&h){h=p.css(e.elem,a,!0)||c||1;do i=i||".5",h=h/i,p.style(e.elem,a,h+d);while(i!==(i=e.cur()/g)&&i!==1&&--j)}e.unit=d,e.start=h,e.end=f[1]?h+(f[1]+1)*c:c}return e}]};p.Animation=p.extend(cW,{tweener:function(a,b){p.isFunction(a)?(b=a,a=["*"]):a=a.split(" ");var c,d=0,e=a.length;for(;d-1,j={},k={},l,m;i?(k=e.position(),l=k.top,m=k.left):(l=parseFloat(g)||0,m=parseFloat(h)||0),p.isFunction(b)&&(b=b.call(a,c,f)),b.top!=null&&(j.top=b.top-f.top+l),b.left!=null&&(j.left=b.left-f.left+m),"using"in b?b.using.call(a,j):e.css(j)}},p.fn.extend({position:function(){if(!this[0])return;var a=this[0],b=this.offsetParent(),c=this.offset(),d=c_.test(b[0].nodeName)?{top:0,left:0}:b.offset();return c.top-=parseFloat(p.css(a,"marginTop"))||0,c.left-=parseFloat(p.css(a,"marginLeft"))||0,d.top+=parseFloat(p.css(b[0],"borderTopWidth"))||0,d.left+=parseFloat(p.css(b[0],"borderLeftWidth"))||0,{top:c.top-d.top,left:c.left-d.left}},offsetParent:function(){return this.map(function(){var a=this.offsetParent||e.body;while(a&&!c_.test(a.nodeName)&&p.css(a,"position")==="static")a=a.offsetParent;return a||e.body})}}),p.each({scrollLeft:"pageXOffset",scrollTop:"pageYOffset"},function(a,c){var d=/Y/.test(c);p.fn[a]=function(e){return p.access(this,function(a,e,f){var g=da(a);if(f===b)return g?c in g?g[c]:g.document.documentElement[e]:a[e];g?g.scrollTo(d?p(g).scrollLeft():f,d?f:p(g).scrollTop()):a[e]=f},a,e,arguments.length,null)}}),p.each({Height:"height",Width:"width"},function(a,c){p.each({padding:"inner"+a,content:c,"":"outer"+a},function(d,e){p.fn[e]=function(e,f){var g=arguments.length&&(d||typeof e!="boolean"),h=d||(e===!0||f===!0?"margin":"border");return p.access(this,function(c,d,e){var f;return p.isWindow(c)?c.document.documentElement["client"+a]:c.nodeType===9?(f=c.documentElement,Math.max(c.body["scroll"+a],f["scroll"+a],c.body["offset"+a],f["offset"+a],f["client"+a])):e===b?p.css(c,d,e,h):p.style(c,d,e,h)},c,g?e:b,g,null)}})}),a.jQuery=a.$=p,typeof define=="function"&&define.amd&&define.amd.jQuery&&define("jquery",[],function(){return p})})(window); \ No newline at end of file diff --git a/bindings/go/godoc-resources/jquery.treeview.css b/bindings/go/godoc-resources/jquery.treeview.css new file mode 100644 index 0000000000..ac33361a62 --- /dev/null +++ b/bindings/go/godoc-resources/jquery.treeview.css @@ -0,0 +1,76 @@ +/* https://github.com/jzaefferer/jquery-treeview/blob/master/jquery.treeview.css */ +/* License: MIT. */ +.treeview, .treeview ul { + padding: 0; + margin: 0; + list-style: none; +} + +.treeview ul { + background-color: white; + margin-top: 4px; +} + +.treeview .hitarea { + background: url(images/treeview-default.gif) -64px -25px no-repeat; + height: 16px; + width: 16px; + margin-left: -16px; + float: left; + cursor: pointer; +} +/* fix for IE6 */ +* html .hitarea { + display: inline; + float:none; +} + +.treeview li { + margin: 0; + padding: 3px 0pt 3px 16px; +} + +.treeview a.selected { + background-color: #eee; +} + +#treecontrol { margin: 1em 0; display: none; } + +.treeview .hover { color: red; cursor: pointer; } + +.treeview li { background: url(images/treeview-default-line.gif) 0 0 no-repeat; } +.treeview li.collapsable, .treeview li.expandable { background-position: 0 -176px; } + +.treeview .expandable-hitarea { background-position: -80px -3px; } + +.treeview li.last { background-position: 0 -1766px } +.treeview li.lastCollapsable, .treeview li.lastExpandable { background-image: url(images/treeview-default.gif); } +.treeview li.lastCollapsable { background-position: 0 -111px } +.treeview li.lastExpandable { background-position: -32px -67px } + +.treeview div.lastCollapsable-hitarea, .treeview div.lastExpandable-hitarea { background-position: 0; } + +.treeview-red li { background-image: url(images/treeview-red-line.gif); } +.treeview-red .hitarea, .treeview-red li.lastCollapsable, .treeview-red li.lastExpandable { background-image: url(images/treeview-red.gif); } + +.treeview-black li { background-image: url(images/treeview-black-line.gif); } +.treeview-black .hitarea, .treeview-black li.lastCollapsable, .treeview-black li.lastExpandable { background-image: url(images/treeview-black.gif); } + +.treeview-gray li { background-image: url(images/treeview-gray-line.gif); } +.treeview-gray .hitarea, .treeview-gray li.lastCollapsable, .treeview-gray li.lastExpandable { background-image: url(images/treeview-gray.gif); } + +.treeview-famfamfam li { background-image: url(images/treeview-famfamfam-line.gif); } +.treeview-famfamfam .hitarea, .treeview-famfamfam li.lastCollapsable, .treeview-famfamfam li.lastExpandable { background-image: url(images/treeview-famfamfam.gif); } + +.treeview .placeholder { + background: url(images/ajax-loader.gif) 0 0 no-repeat; + height: 16px; + width: 16px; + display: block; +} + +.filetree li { padding: 3px 0 2px 16px; } +.filetree span.folder, .filetree span.file { padding: 1px 0 1px 16px; display: block; } +.filetree span.folder { background: url(images/folder.gif) 0 0 no-repeat; } +.filetree li.expandable span.folder { background: url(images/folder-closed.gif) 0 0 no-repeat; } +.filetree span.file { background: url(images/file.gif) 0 0 no-repeat; } diff --git a/bindings/go/godoc-resources/jquery.treeview.edit.js b/bindings/go/godoc-resources/jquery.treeview.edit.js new file mode 100644 index 0000000000..9895b02631 --- /dev/null +++ b/bindings/go/godoc-resources/jquery.treeview.edit.js @@ -0,0 +1,39 @@ +/* https://github.com/jzaefferer/jquery-treeview/blob/master/jquery.treeview.edit.js */ +/* License: MIT. */ +(function($) { + var CLASSES = $.treeview.classes; + var proxied = $.fn.treeview; + $.fn.treeview = function(settings) { + settings = $.extend({}, settings); + if (settings.add) { + return this.trigger("add", [settings.add]); + } + if (settings.remove) { + return this.trigger("remove", [settings.remove]); + } + return proxied.apply(this, arguments).bind("add", function(event, branches) { + $(branches).prev() + .removeClass(CLASSES.last) + .removeClass(CLASSES.lastCollapsable) + .removeClass(CLASSES.lastExpandable) + .find(">.hitarea") + .removeClass(CLASSES.lastCollapsableHitarea) + .removeClass(CLASSES.lastExpandableHitarea); + $(branches).find("li").andSelf().prepareBranches(settings).applyClasses(settings, $(this).data("toggler")); + }).bind("remove", function(event, branches) { + var prev = $(branches).prev(); + var parent = $(branches).parent(); + $(branches).remove(); + prev.filter(":last-child").addClass(CLASSES.last) + .filter("." + CLASSES.expandable).replaceClass(CLASSES.last, CLASSES.lastExpandable).end() + .find(">.hitarea").replaceClass(CLASSES.expandableHitarea, CLASSES.lastExpandableHitarea).end() + .filter("." + CLASSES.collapsable).replaceClass(CLASSES.last, CLASSES.lastCollapsable).end() + .find(">.hitarea").replaceClass(CLASSES.collapsableHitarea, CLASSES.lastCollapsableHitarea); + if (parent.is(":not(:has(>))") && parent[0] != this) { + parent.parent().removeClass(CLASSES.collapsable).removeClass(CLASSES.expandable) + parent.siblings(".hitarea").andSelf().remove(); + } + }); + }; + +})(jQuery); diff --git a/bindings/go/godoc-resources/jquery.treeview.js b/bindings/go/godoc-resources/jquery.treeview.js new file mode 100644 index 0000000000..356af2380f --- /dev/null +++ b/bindings/go/godoc-resources/jquery.treeview.js @@ -0,0 +1,256 @@ +/* + * Treeview 1.4.1 - jQuery plugin to hide and show branches of a tree + * + * http://bassistance.de/jquery-plugins/jquery-plugin-treeview/ + * http://docs.jquery.com/Plugins/Treeview + * + * Copyright (c) 2007 Jörn Zaefferer + * + * Dual licensed under the MIT and GPL licenses: + * http://www.opensource.org/licenses/mit-license.php + * http://www.gnu.org/licenses/gpl.html + * + * Revision: $Id: jquery.treeview.js 5759 2008-07-01 07:50:28Z joern.zaefferer $ + * + */ + +;(function($) { + + // TODO rewrite as a widget, removing all the extra plugins + $.extend($.fn, { + swapClass: function(c1, c2) { + var c1Elements = this.filter('.' + c1); + this.filter('.' + c2).removeClass(c2).addClass(c1); + c1Elements.removeClass(c1).addClass(c2); + return this; + }, + replaceClass: function(c1, c2) { + return this.filter('.' + c1).removeClass(c1).addClass(c2).end(); + }, + hoverClass: function(className) { + className = className || "hover"; + return this.hover(function() { + $(this).addClass(className); + }, function() { + $(this).removeClass(className); + }); + }, + heightToggle: function(animated, callback) { + animated ? + this.animate({ height: "toggle" }, animated, callback) : + this.each(function(){ + jQuery(this)[ jQuery(this).is(":hidden") ? "show" : "hide" ](); + if(callback) + callback.apply(this, arguments); + }); + }, + heightHide: function(animated, callback) { + if (animated) { + this.animate({ height: "hide" }, animated, callback); + } else { + this.hide(); + if (callback) + this.each(callback); + } + }, + prepareBranches: function(settings) { + if (!settings.prerendered) { + // mark last tree items + this.filter(":last-child:not(ul)").addClass(CLASSES.last); + // collapse whole tree, or only those marked as closed, anyway except those marked as open + this.filter((settings.collapsed ? "" : "." + CLASSES.closed) + ":not(." + CLASSES.open + ")").find(">ul").hide(); + } + // return all items with sublists + return this.filter(":has(>ul)"); + }, + applyClasses: function(settings, toggler) { + // TODO use event delegation + this.filter(":has(>ul):not(:has(>a))").find(">span").unbind("click.treeview").bind("click.treeview", function(event) { + // don't handle click events on children, eg. checkboxes + if ( this == event.target ) + toggler.apply($(this).next()); + }).add( $("a", this) ).hoverClass(); + + if (!settings.prerendered) { + // handle closed ones first + this.filter(":has(>ul:hidden)") + .addClass(CLASSES.expandable) + .replaceClass(CLASSES.last, CLASSES.lastExpandable); + + // handle open ones + this.not(":has(>ul:hidden)") + .addClass(CLASSES.collapsable) + .replaceClass(CLASSES.last, CLASSES.lastCollapsable); + + // create hitarea if not present + var hitarea = this.find("div." + CLASSES.hitarea); + if (!hitarea.length) + hitarea = this.prepend("
    ").find("div." + CLASSES.hitarea); + hitarea.removeClass().addClass(CLASSES.hitarea).each(function() { + var classes = ""; + $.each($(this).parent().attr("class").split(" "), function() { + classes += this + "-hitarea "; + }); + $(this).addClass( classes ); + }) + } + + // apply event to hitarea + this.find("div." + CLASSES.hitarea).click( toggler ); + }, + treeview: function(settings) { + + settings = $.extend({ + cookieId: "treeview" + }, settings); + + if ( settings.toggle ) { + var callback = settings.toggle; + settings.toggle = function() { + return callback.apply($(this).parent()[0], arguments); + }; + } + + // factory for treecontroller + function treeController(tree, control) { + // factory for click handlers + function handler(filter) { + return function() { + // reuse toggle event handler, applying the elements to toggle + // start searching for all hitareas + toggler.apply( $("div." + CLASSES.hitarea, tree).filter(function() { + // for plain toggle, no filter is provided, otherwise we need to check the parent element + return filter ? $(this).parent("." + filter).length : true; + }) ); + return false; + }; + } + // click on first element to collapse tree + $("a:eq(0)", control).click( handler(CLASSES.collapsable) ); + // click on second to expand tree + $("a:eq(1)", control).click( handler(CLASSES.expandable) ); + // click on third to toggle tree + $("a:eq(2)", control).click( handler() ); + } + + // handle toggle event + function toggler() { + $(this) + .parent() + // swap classes for hitarea + .find(">.hitarea") + .swapClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea ) + .swapClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea ) + .end() + // swap classes for parent li + .swapClass( CLASSES.collapsable, CLASSES.expandable ) + .swapClass( CLASSES.lastCollapsable, CLASSES.lastExpandable ) + // find child lists + .find( ">ul" ) + // toggle them + .heightToggle( settings.animated, settings.toggle ); + if ( settings.unique ) { + $(this).parent() + .siblings() + // swap classes for hitarea + .find(">.hitarea") + .replaceClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea ) + .replaceClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea ) + .end() + .replaceClass( CLASSES.collapsable, CLASSES.expandable ) + .replaceClass( CLASSES.lastCollapsable, CLASSES.lastExpandable ) + .find( ">ul" ) + .heightHide( settings.animated, settings.toggle ); + } + } + this.data("toggler", toggler); + + function serialize() { + function binary(arg) { + return arg ? 1 : 0; + } + var data = []; + branches.each(function(i, e) { + data[i] = $(e).is(":has(>ul:visible)") ? 1 : 0; + }); + $.cookie(settings.cookieId, data.join(""), settings.cookieOptions ); + } + + function deserialize() { + var stored = $.cookie(settings.cookieId); + if ( stored ) { + var data = stored.split(""); + branches.each(function(i, e) { + $(e).find(">ul")[ parseInt(data[i]) ? "show" : "hide" ](); + }); + } + } + + // add treeview class to activate styles + this.addClass("treeview"); + + // prepare branches and find all tree items with child lists + var branches = this.find("li").prepareBranches(settings); + + switch(settings.persist) { + case "cookie": + var toggleCallback = settings.toggle; + settings.toggle = function() { + serialize(); + if (toggleCallback) { + toggleCallback.apply(this, arguments); + } + }; + deserialize(); + break; + case "location": + var current = this.find("a").filter(function() { + return this.href.toLowerCase() == location.href.toLowerCase(); + }); + if ( current.length ) { + // TODO update the open/closed classes + var items = current.addClass("selected").parents("ul, li").add( current.next() ).show(); + if (settings.prerendered) { + // if prerendered is on, replicate the basic class swapping + items.filter("li") + .swapClass( CLASSES.collapsable, CLASSES.expandable ) + .swapClass( CLASSES.lastCollapsable, CLASSES.lastExpandable ) + .find(">.hitarea") + .swapClass( CLASSES.collapsableHitarea, CLASSES.expandableHitarea ) + .swapClass( CLASSES.lastCollapsableHitarea, CLASSES.lastExpandableHitarea ); + } + } + break; + } + + branches.applyClasses(settings, toggler); + + // if control option is set, create the treecontroller and show it + if ( settings.control ) { + treeController(this, settings.control); + $(settings.control).show(); + } + + return this; + } + }); + + // classes used by the plugin + // need to be styled via external stylesheet, see first example + $.treeview = {}; + var CLASSES = ($.treeview.classes = { + open: "open", + closed: "closed", + expandable: "expandable", + expandableHitarea: "expandable-hitarea", + lastExpandableHitarea: "lastExpandable-hitarea", + collapsable: "collapsable", + collapsableHitarea: "collapsable-hitarea", + lastCollapsableHitarea: "lastCollapsable-hitarea", + lastCollapsable: "lastCollapsable", + lastExpandable: "lastExpandable", + last: "last", + hitarea: "hitarea" + }); + +})(jQuery); diff --git a/bindings/go/godoc-resources/style.css b/bindings/go/godoc-resources/style.css new file mode 100644 index 0000000000..4d2b07a78d --- /dev/null +++ b/bindings/go/godoc-resources/style.css @@ -0,0 +1,776 @@ +body { + margin: 0; + font-family: Arial, sans-serif; + font-size: 16px; + background-color: #fff; + line-height: 1.3em; +} +pre, +code { + font-family: Menlo, monospace; + font-size: 14px; +} +pre { + line-height: 1.4em; + overflow-x: auto; +} +pre .comment { + color: #006600; +} +pre .highlight, +pre .highlight-comment, +pre .selection-highlight, +pre .selection-highlight-comment { + background: #FFFF00; +} +pre .selection, +pre .selection-comment { + background: #FF9632; +} +pre .ln { + color: #999; +} +body { + color: #222; +} +a, +.exampleHeading .text { + color: #375EAB; + text-decoration: none; +} +a:hover, +.exampleHeading .text:hover { + text-decoration: underline; +} +p { + max-width: 800px; + word-wrap: break-word; +} +p, +pre, +ul, +ol { + margin: 20px; +} +pre { + background: #EFEFEF; + padding: 10px; + + -webkit-border-radius: 5px; + -moz-border-radius: 5px; + border-radius: 5px; +} + +h1, +h2, +h3, +h4, +.rootHeading { + margin: 20px 0 20px; + padding: 0; + color: #375EAB; + font-weight: bold; +} +h1 { + font-size: 28px; + line-height: 1; +} +h2 { + font-size: 20px; + background: #E0EBF5; + padding: 8px; + line-height: 1.25; + font-weight: normal; +} +h2 a { + font-weight: bold; +} +h3 { + font-size: 20px; +} +h3, +h4 { + margin: 20px 5px; +} +h4 { + font-size: 16px; +} +.rootHeading { + font-size: 20px; + margin: 0; +} + +dl { + margin: 20px; +} +dd { + margin: 0; +} +dd.indent { + margin: 0 20px; +} +dl, +dd { + font-size: 14px; +} +div#nav table td { + vertical-align: top; +} + + +.pkg-dir { + padding: 0 10px; +} +.pkg-dir table { + border-collapse: collapse; + border-spacing: 0; +} +.pkg-name { + padding-right: 10px; +} +.alert { + color: #AA0000; +} + +.top-heading { + float: left; + padding: 21px 0; + font-size: 20px; + font-weight: normal; +} +.top-heading a { + color: #222; + text-decoration: none; +} + +div#topbar { + background: #E0EBF5; + height: 64px; + overflow: hidden; +} + +body { + text-align: center; +} +div#page { + width: 100%; +} +div#page > .container, +div#topbar > .container { + text-align: left; + margin-left: auto; + margin-right: auto; + padding: 0 20px; +} +div#topbar > .container, +div#page > .container { + max-width: 950px; +} +div#page.wide > .container, +div#topbar.wide > .container { + max-width: none; +} +div#plusone { + float: right; + clear: right; + margin-top: 5px; +} + +div#footer { + text-align: center; + color: #666; + font-size: 14px; + margin: 40px 0; +} + +div#menu > a, +div#menu > input, +div#learn .buttons a, +div.play .buttons a, +div#blog .read a, +#menu-button { + padding: 10px; + + text-decoration: none; + font-size: 16px; + + -webkit-border-radius: 5px; + -moz-border-radius: 5px; + border-radius: 5px; +} +div#playground .buttons a, +div#menu > a, +div#menu > input, +#menu-button { + border: 1px solid #375EAB; +} +div#playground .buttons a, +div#menu > a, +#menu-button { + color: white; + background: #375EAB; +} +#playgroundButton.active { + background: white; + color: #375EAB; +} +a#start, +div#learn .buttons a, +div.play .buttons a, +div#blog .read a { + color: #222; + border: 1px solid #375EAB; + background: #E0EBF5; +} +.download { + width: 150px; +} + +div#menu { + text-align: right; + padding: 10px; + white-space: nowrap; + max-height: 0; + -moz-transition: max-height .25s linear; + transition: max-height .25s linear; + width: 100%; +} +div#menu.menu-visible { + max-height: 500px; +} +div#menu > a, +#menu-button { + margin: 10px 2px; + padding: 10px; +} +div#menu > input { + position: relative; + top: 1px; + width: 140px; + background: white; + color: #222; + box-sizing: border-box; +} +div#menu > input.inactive { + color: #999; +} + +#menu-button { + display: none; + position: absolute; + right: 5px; + top: 0; + margin-right: 5px; +} +#menu-button-arrow { + display: inline-block; +} +.vertical-flip { + transform: rotate(-180deg); +} + +div.left { + float: left; + clear: left; + margin-right: 2.5%; +} +div.right { + float: right; + clear: right; + margin-left: 2.5%; +} +div.left, +div.right { + width: 45%; +} + +div#learn, +div#about { + padding-top: 20px; +} +div#learn h2, +div#about { + margin: 0; +} +div#about { + font-size: 20px; + margin: 0 auto 30px; +} +div#gopher { + background: url(/doc/gopher/frontpage.png) no-repeat; + background-position: center top; + height: 155px; +} +a#start { + display: block; + padding: 10px; + + text-align: center; + text-decoration: none; + + -webkit-border-radius: 5px; + -moz-border-radius: 5px; + border-radius: 5px; +} +a#start .big { + display: block; + font-weight: bold; + font-size: 20px; +} +a#start .desc { + display: block; + font-size: 14px; + font-weight: normal; + margin-top: 5px; +} + +div#learn .popout { + float: right; + display: block; + cursor: pointer; + font-size: 12px; + background: url(/doc/share.png) no-repeat; + background-position: right top; + padding: 5px 27px; +} +div#learn pre, +div#learn textarea { + padding: 0; + margin: 0; + font-family: Menlo, monospace; + font-size: 14px; +} +div#learn .input { + padding: 10px; + margin-top: 10px; + height: 150px; + + -webkit-border-top-left-radius: 5px; + -webkit-border-top-right-radius: 5px; + -moz-border-radius-topleft: 5px; + -moz-border-radius-topright: 5px; + border-top-left-radius: 5px; + border-top-right-radius: 5px; +} +div#learn .input textarea { + width: 100%; + height: 100%; + border: none; + outline: none; + resize: none; +} +div#learn .output { + border-top: none !important; + + padding: 10px; + height: 59px; + overflow: auto; + + -webkit-border-bottom-right-radius: 5px; + -webkit-border-bottom-left-radius: 5px; + -moz-border-radius-bottomright: 5px; + -moz-border-radius-bottomleft: 5px; + border-bottom-right-radius: 5px; + border-bottom-left-radius: 5px; +} +div#learn .output pre { + padding: 0; + + -webkit-border-radius: 0; + -moz-border-radius: 0; + border-radius: 0; +} +div#learn .input, +div#learn .input textarea, +div#learn .output, +div#learn .output pre { + background: #FFFFD8; +} +div#learn .input, +div#learn .output { + border: 1px solid #375EAB; +} +div#learn .buttons { + float: right; + padding: 20px 0 10px 0; + text-align: right; +} +div#learn .buttons a { + height: 16px; + margin-left: 5px; + padding: 10px; +} +div#learn .toys { + margin-top: 8px; +} +div#learn .toys select { + border: 1px solid #375EAB; + margin: 0; +} +div#learn .output .exit { + display: none; +} + +div#video { + max-width: 100%; +} +div#blog, +div#video { + margin-top: 40px; +} +div#blog > a, +div#blog > div, +div#blog > h2, +div#video > a, +div#video > div, +div#video > h2 { + margin-bottom: 10px; +} +div#blog .title, +div#video .title { + display: block; + font-size: 20px; +} +div#blog .when { + color: #666; + font-size: 14px; +} +div#blog .read { + text-align: right; +} + +.toggleButton { cursor: pointer; } +.toggle .collapsed { display: block; } +.toggle .expanded { display: none; } +.toggleVisible .collapsed { display: none; } +.toggleVisible .expanded { display: block; } + +table.codetable { margin-left: auto; margin-right: auto; border-style: none; } +table.codetable td { padding-right: 10px; } +hr { border-style: none; border-top: 1px solid black; } + +img.gopher { + float: right; + margin-left: 10px; + margin-bottom: 10px; + z-index: -1; +} +h2 { clear: right; } + +/* example and drop-down playground */ +div.play { + padding: 0 20px 40px 20px; +} +div.play pre, +div.play textarea, +div.play .lines { + padding: 0; + margin: 0; + font-family: Menlo, monospace; + font-size: 14px; +} +div.play .input { + padding: 10px; + margin-top: 10px; + + -webkit-border-top-left-radius: 5px; + -webkit-border-top-right-radius: 5px; + -moz-border-radius-topleft: 5px; + -moz-border-radius-topright: 5px; + border-top-left-radius: 5px; + border-top-right-radius: 5px; + + overflow: hidden; +} +div.play .input textarea { + width: 100%; + height: 100%; + border: none; + outline: none; + resize: none; + + overflow: hidden; +} +div#playground .input textarea { + overflow: auto; + resize: auto; +} +div.play .output { + border-top: none !important; + + padding: 10px; + max-height: 200px; + overflow: auto; + + -webkit-border-bottom-right-radius: 5px; + -webkit-border-bottom-left-radius: 5px; + -moz-border-radius-bottomright: 5px; + -moz-border-radius-bottomleft: 5px; + border-bottom-right-radius: 5px; + border-bottom-left-radius: 5px; +} +div.play .output pre { + padding: 0; + + -webkit-border-radius: 0; + -moz-border-radius: 0; + border-radius: 0; +} +div.play .input, +div.play .input textarea, +div.play .output, +div.play .output pre { + background: #FFFFD8; +} +div.play .input, +div.play .output { + border: 1px solid #375EAB; +} +div.play .buttons { + float: right; + padding: 20px 0 10px 0; + text-align: right; +} +div.play .buttons a { + height: 16px; + margin-left: 5px; + padding: 10px; + cursor: pointer; +} +.output .stderr { + color: #933; +} +.output .system { + color: #999; +} + +/* drop-down playground */ +#playgroundButton, +div#playground { + /* start hidden; revealed by javascript */ + display: none; +} +div#playground { + position: absolute; + top: 63px; + right: 20px; + padding: 0 10px 10px 10px; + z-index: 1; + text-align: left; + background: #E0EBF5; + + border: 1px solid #B0BBC5; + border-top: none; + + -webkit-border-bottom-left-radius: 5px; + -webkit-border-bottom-right-radius: 5px; + -moz-border-radius-bottomleft: 5px; + -moz-border-radius-bottomright: 5px; + border-bottom-left-radius: 5px; + border-bottom-right-radius: 5px; +} +div#playground .code { + width: 520px; + height: 200px; +} +div#playground .output { + height: 100px; +} + +/* Inline runnable snippets (play.js/initPlayground) */ +#content .code pre, #content .playground pre, #content .output pre { + margin: 0; + padding: 0; + background: none; + border: none; + outline: 0px solid transparent; + overflow: auto; +} +#content .playground .number, #content .code .number { + color: #999; +} +#content .code, #content .playground, #content .output { + width: auto; + margin: 20px; + padding: 10px; + -webkit-border-radius: 5px; + -moz-border-radius: 5px; + border-radius: 5px; +} +#content .code, #content .playground { + background: #e9e9e9; +} +#content .output { + background: #202020; +} +#content .output .stdout, #content .output pre { + color: #e6e6e6; +} +#content .output .stderr, #content .output .error { + color: rgb(244, 74, 63); +} +#content .output .system, #content .output .exit { + color: rgb(255, 209, 77) +} +#content .buttons { + position: relative; + float: right; + top: -50px; + right: 30px; +} +#content .output .buttons { + top: -60px; + right: 0; + height: 0; +} +#content .buttons .kill { + display: none; + visibility: hidden; +} +a.error { + font-weight: bold; + color: white; + background-color: darkred; + border-bottom-left-radius: 4px; + border-bottom-right-radius: 4px; + border-top-left-radius: 4px; + border-top-right-radius: 4px; + padding: 2px 4px 2px 4px; /* TRBL */ +} + + +#heading-narrow { + display: none; +} + +.downloading { + background: #F9F9BE; + padding: 10px; + text-align: center; + border-radius: 5px; +} + +@media (max-width: 930px) { + #heading-wide { + display: none; + } + #heading-narrow { + display: block; + } +} + + +@media (max-width: 760px) { + .container .left, + .container .right { + width: auto; + float: none; + } + + div#about { + max-width: 500px; + text-align: center; + } +} + +@media (min-width: 700px) and (max-width: 1000px) { + div#menu > a { + margin: 5px 0; + font-size: 14px; + } + + div#menu > input { + font-size: 14px; + } +} + +@media (max-width: 700px) { + body { + font-size: 15px; + } + + pre, + code { + font-size: 13px; + } + + div#page > .container { + padding: 0 10px; + } + + div#topbar { + height: auto; + padding: 10px; + } + + div#topbar > .container { + padding: 0; + } + + #heading-wide { + display: block; + } + #heading-narrow { + display: none; + } + + .top-heading { + float: none; + display: inline-block; + padding: 12px; + } + + div#menu { + padding: 0; + min-width: 0; + text-align: left; + float: left; + } + + div#menu > a, + div#menu > input { + display: block; + margin-left: 0; + margin-right: 0; + } + + div#menu > input { + width: 100%; + } + + #menu-button { + display: inline-block; + } + + p, + pre, + ul, + ol { + margin: 10px; + } + + .pkg-synopsis { + display: none; + } + + img.gopher { + display: none; + } +} + +@media (max-width: 480px) { + #heading-wide { + display: none; + } + #heading-narrow { + display: block; + } +} + +@media print { + pre { + background: #FFF; + border: 1px solid #BBB; + white-space: pre-wrap; + } +} diff --git a/bindings/go/include.mk b/bindings/go/include.mk new file mode 100644 index 0000000000..4a5ea38433 --- /dev/null +++ b/bindings/go/include.mk @@ -0,0 +1,103 @@ +# +# include.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +TARGETS += fdb_go fdb_go_tester +CLEAN_TARGETS += fdb_go_clean fdb_go_tester_clean + +GOPATH := $(CURDIR)/bindings/go + +# We only override if the environment didn't set it (this is used by +# the fdbwebsite documentation build process) +GODOC_DIR ?= bindings/go + +CGO_CFLAGS := -I$(CURDIR)/bindings/c +CGO_LDFLAGS := -L$(CURDIR)/lib + +ifeq ($(PLATFORM),linux) + GOPLATFORM := linux_amd64 +else ifeq ($(PLATFORM),osx) + GOPLATFORM := darwin_amd64 +else + $(error Not prepared to compile on platform $(PLATFORM)) +endif + +GO_PACKAGE_OUTDIR := $(GOPATH)/pkg/$(GOPLATFORM) + +GO_PACKAGES := fdb fdb/tuple fdb/subspace fdb/directory +GO_PACKAGE_OBJECTS := $(addprefix $(GO_PACKAGE_OUTDIR)/,$(GO_PACKAGES:=.a)) + +GO_SRC := $(shell find $(GOPATH)/src -name '*.go') + +fdb_go: $(GO_PACKAGE_OBJECTS) $(GO_SRC) + +fdb_go_clean: + @echo "Cleaning fdb_go" + @rm -rf $(GO_PACKAGE_OUTDIR) + +fdb_go_tester: $(GOPATH)/bin/_stacktester + +fdb_go_tester_clean: + @echo "Cleaning fdb_go_tester" + @rm -rf $(GOPATH)/bin + +$(GOPATH)/bin/_stacktester: $(GO_SRC) $(GO_PACKAGE_OBJECTS) bindings/go/src/fdb/generated.go + @echo "Compiling $(basename $(notdir $@))" + @go install _stacktester + +$(GO_PACKAGE_OUTDIR)/fdb/tuple.a: $(GO_SRC) $(GO_PACKAGE_OUTDIR)/fdb.a bindings/go/src/fdb/generated.go + @echo "Compiling fdb/tuple" + @go install fdb/tuple + +$(GO_PACKAGE_OUTDIR)/fdb/subspace.a: $(GO_SRC) $(GO_PACKAGE_OUTDIR)/fdb.a $(GO_PACKAGE_OUTDIR)/fdb/tuple.a bindings/go/src/fdb/generated.go + @echo "Compiling fdb/subspace" + @go install fdb/subspace + +$(GO_PACKAGE_OUTDIR)/fdb/directory.a: $(GO_SRC) $(GO_PACKAGE_OUTDIR)/fdb.a $(GO_PACKAGE_OUTDIR)/fdb/tuple.a $(GO_PACKAGE_OUTDIR)/fdb/subspace.a bindings/go/src/fdb/generated.go + @echo "Compiling fdb/directory" + @go install fdb/directory + +$(GO_PACKAGE_OUTDIR)/fdb.a: $(GO_SRC) bindings/go/src/fdb/generated.go + @echo "Compiling fdb" + @go install fdb + +bindings/go/src/fdb/generated.go: lib/libfdb_c.$(DLEXT) bindings/go/src/_util/translate_fdb_options.go fdbclient/vexillographer/fdb.options + @echo "Building $@" + @go run bindings/go/src/_util/translate_fdb_options.go < fdbclient/vexillographer/fdb.options > $@ + +godoc: $(GO_SRC) + @echo "Generating Go Documentation" + @rm -rf $(GODOC_DIR)/godoc + @mkdir -p $(GODOC_DIR)/godoc + @mkdir -p $(GODOC_DIR)/godoc/lib/godoc + @godoc -url "http://localhost:6060/pkg/fdb" > $(GODOC_DIR)/godoc/fdb.html + @godoc -url "http://localhost:6060/pkg/fdb/tuple" > $(GODOC_DIR)/godoc/fdb.tuple.html + @godoc -url "http://localhost:6060/pkg/fdb/subspace" > $(GODOC_DIR)/godoc/fdb.subspace.html + @godoc -url "http://localhost:6060/pkg/fdb/directory" > $(GODOC_DIR)/godoc/fdb.directory.html + @cp $(GOPATH)/godoc-resources/* $(GODOC_DIR)/godoc/lib/godoc + @echo "Mangling paths in Go Documentation" + @(find $(GODOC_DIR)/godoc/ -name *.html -exec sed -i '' -e 's_/lib_lib_' {} \;) + @(sed -i -e 's_a href="tuple/"_a href="fdb.tuple.html"_' $(GODOC_DIR)/godoc/fdb.html) + @(sed -i -e 's_a href="subspace/"_a href="fdb.subspace.html"_' $(GODOC_DIR)/godoc/fdb.html) + @(sed -i -e 's_a href="directory/"_a href="fdb.directory.html"_' $(GODOC_DIR)/godoc/fdb.html) + +godoc_clean: + @echo "Cleaning Go Documentation" + @rm -rf $(GODOC_DIR)/godoc + diff --git a/bindings/go/src/_stacktester/directory.go b/bindings/go/src/_stacktester/directory.go new file mode 100644 index 0000000000..ac4ff1217b --- /dev/null +++ b/bindings/go/src/_stacktester/directory.go @@ -0,0 +1,298 @@ +/* + * directory.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "fdb" + "fdb/tuple" + "fdb/subspace" + "fdb/directory" + "strings" + "bytes" +) + +func (sm *StackMachine) popTuples(count int) []tuple.Tuple { + tuples := make([]tuple.Tuple, count) + + for i := 0; i < count; i++ { + c := sm.waitAndPop().item.(int64) + tuples[i] = make(tuple.Tuple, c) + for j := int64(0); j < c; j++ { + tuples[i][j] = sm.waitAndPop().item + } + } + + return tuples +} + +func tupleToPath(t tuple.Tuple) []string { + ret := make([]string, len(t)) + for i, el := range t { + ret[i] = el.(string) + } + return ret +} + +func tuplePackStrings(s []string) []byte { + t := make(tuple.Tuple, len(s)) + for i, el := range s { + t[i] = el + } + return t.Pack() +} + +type DirectoryExtension struct { + list []interface{} + index int64 + errorIndex int64 +} + +func newDirectoryExtension() *DirectoryExtension { + de := DirectoryExtension{} + de.store(directory.Root()) + return &de +} + +func (de *DirectoryExtension) store(el interface{}) { + de.list = append(de.list, el) +} + +func (de *DirectoryExtension) cwd() directory.Directory { + return de.list[de.index].(directory.Directory) +} + +func (de *DirectoryExtension) css() subspace.Subspace { + return de.list[de.index].(subspace.Subspace) +} + +func (sm *StackMachine) maybePath() []string { + count := sm.waitAndPop().item.(int64) + var path []string + if count > 0 { + tuples := sm.popTuples(1) + path = tupleToPath(tuples[0]) + } + return path +} + +var createOps = map[string]bool { + "CREATE_SUBSPACE": true, + "CREATE_LAYER": true, + "CREATE_OR_OPEN": true, + "CREATE": true, + "OPEN": true, + "MOVE": true, + "MOVE_TO": true, + "OPEN_SUBSPACE": true, +} + +func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, idx int, t fdb.Transactor, rt fdb.ReadTransactor) { + defer func() { + if r := recover(); r != nil { + sm.store(idx, []byte("DIRECTORY_ERROR")) + if createOps[op] { + de.store(nil) + } + } + }() + + var e error + + switch { + case op == "CREATE_SUBSPACE": + tuples := sm.popTuples(1) + rp := sm.waitAndPop().item.([]byte) + s := subspace.FromBytes(rp).Sub(tuples[0]...) + de.store(s) + case op == "CREATE_LAYER": + idx1 := sm.waitAndPop().item.(int64) + idx2 := sm.waitAndPop().item.(int64) + amp := sm.waitAndPop().item.(int64) + nodeSS := de.list[idx1] + contentSS := de.list[idx2] + + if nodeSS == nil || contentSS == nil { + de.store(nil) + } else { + de.store(directory.NewDirectoryLayer(nodeSS.(subspace.Subspace), contentSS.(subspace.Subspace), (amp == int64(1)))) + } + case op == "CREATE_OR_OPEN": + tuples := sm.popTuples(1) + l := sm.waitAndPop().item + var layer []byte + if l != nil { + layer = l.([]byte) + } + d, e := de.cwd().CreateOrOpen(t, tupleToPath(tuples[0]), layer) + if e != nil { panic(e) } + de.store(d) + case op == "CREATE": + tuples := sm.popTuples(1) + l := sm.waitAndPop().item + var layer []byte + if l != nil { + layer = l.([]byte) + } + p := sm.waitAndPop().item + var d directory.Directory + if p == nil { + d, e = de.cwd().Create(t, tupleToPath(tuples[0]), layer) + } else { + // p.([]byte) itself may be nil, but CreatePrefix handles that appropriately + d, e = de.cwd().CreatePrefix(t, tupleToPath(tuples[0]), layer, p.([]byte)) + } + if e != nil { panic(e) } + de.store(d) + case op == "OPEN": + tuples := sm.popTuples(1) + l := sm.waitAndPop().item + var layer []byte + if l != nil { + layer = l.([]byte) + } + d, e := de.cwd().Open(rt, tupleToPath(tuples[0]), layer) + if e != nil { panic(e) } + de.store(d) + case op == "CHANGE": + i := sm.waitAndPop().item.(int64) + if de.list[i] == nil { + i = de.errorIndex + } + de.index = i + case op == "SET_ERROR_INDEX": + de.errorIndex = sm.waitAndPop().item.(int64) + case op == "MOVE": + tuples := sm.popTuples(2) + d, e := de.cwd().Move(t, tupleToPath(tuples[0]), tupleToPath(tuples[1])) + if e != nil { panic(e) } + de.store(d) + case op == "MOVE_TO": + tuples := sm.popTuples(1) + d, e := de.cwd().MoveTo(t, tupleToPath(tuples[0])) + if e != nil { panic(e) } + de.store(d) + case strings.HasPrefix(op, "REMOVE"): + path := sm.maybePath() + // This ***HAS*** to call Transact to ensure that any directory version + // key set in the process of trying to remove this potentially + // non-existent directory, in the REMOVE but not REMOVE_IF_EXISTS case, + // doesn't end up committing the version key. (Other languages have + // separate remove() and remove_if_exists() so don't have this tricky + // issue). + _, e := t.Transact(func (tr fdb.Transaction) (interface{}, error) { + ok, e := de.cwd().Remove(tr, path) + if e != nil { panic(e) } + switch op[6:] { + case "": + if !ok { + panic("directory does not exist") + } + case "_IF_EXISTS": + } + return nil, nil + }) + if e != nil { panic(e) } + case op == "LIST": + subs, e := de.cwd().List(rt, sm.maybePath()) + if e != nil { panic(e) } + t := make(tuple.Tuple, len(subs)) + for i, s := range subs { t[i] = s } + sm.store(idx, t.Pack()) + case op == "EXISTS": + b, e := de.cwd().Exists(rt, sm.maybePath()) + if e != nil { panic(e) } + if b { + sm.store(idx, int64(1)) + } else { + sm.store(idx, int64(0)) + } + case op == "PACK_KEY": + tuples := sm.popTuples(1) + sm.store(idx, de.css().Pack(tuples[0])) + case op == "UNPACK_KEY": + t, e := de.css().Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) + if e != nil { panic(e) } + for _, el := range(t) { + sm.store(idx, el) + } + case op == "RANGE": + ss := de.css().Sub(sm.popTuples(1)[0]...) + bk, ek := ss.FDBRangeKeys() + sm.store(idx, bk) + sm.store(idx, ek) + case op == "CONTAINS": + k := sm.waitAndPop().item.([]byte) + b := de.css().Contains(fdb.Key(k)) + if b { + sm.store(idx, int64(1)) + } else { + sm.store(idx, int64(0)) + } + case op == "OPEN_SUBSPACE": + de.store(de.css().Sub(sm.popTuples(1)[0]...)) + case op == "LOG_SUBSPACE": + k := sm.waitAndPop().item.([]byte) + k = append(k, tuple.Tuple{de.index}.Pack()...) + v := de.css().Bytes() + t.Transact(func (tr fdb.Transaction) (interface{}, error) { + tr.Set(fdb.Key(k), v) + return nil, nil + }) + case op == "LOG_DIRECTORY": + rp := sm.waitAndPop().item.([]byte) + ss := subspace.FromBytes(rp).Sub(de.index) + k1 := ss.Pack(tuple.Tuple{"path"}) + v1 := tuplePackStrings(de.cwd().GetPath()) + k2 := ss.Pack(tuple.Tuple{"layer"}) + v2 := tuple.Tuple{de.cwd().GetLayer()}.Pack() + k3 := ss.Pack(tuple.Tuple{"exists"}) + var v3 []byte + exists, e := de.cwd().Exists(rt, nil) + if e != nil { panic(e) } + if exists { + v3 = tuple.Tuple{1}.Pack() + } else { + v3 = tuple.Tuple{0}.Pack() + } + k4 := ss.Pack(tuple.Tuple{"children"}) + var subs []string + if exists { + subs, e = de.cwd().List(rt, nil) + if e != nil { panic(e) } + } + v4 := tuplePackStrings(subs) + t.Transact(func (tr fdb.Transaction) (interface{}, error) { + tr.Set(k1, v1) + tr.Set(k2, v2) + tr.Set(k3, v3) + tr.Set(k4, v4) + return nil, nil + }) + case op == "STRIP_PREFIX": + ba := sm.waitAndPop().item.([]byte) + ssb := de.css().Bytes() + if !bytes.HasPrefix(ba, ssb) { + panic("prefix mismatch") + } + ba = ba[len(ssb):] + sm.store(idx, ba) + } +} diff --git a/bindings/go/src/_stacktester/stacktester.go b/bindings/go/src/_stacktester/stacktester.go new file mode 100644 index 0000000000..dab5a0c9ca --- /dev/null +++ b/bindings/go/src/_stacktester/stacktester.go @@ -0,0 +1,726 @@ +/* + * stacktester.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "bytes" + "fdb" + "fdb/tuple" + "log" + "fmt" + "os" + "strings" + "sync" + "runtime" + "reflect" + "time" + "strconv" +) + +const verbose bool = false + +var trMap = map[string]fdb.Transaction {} +var trMapLock = sync.RWMutex{} + +func int64ToBool(i int64) bool { + switch i { + case 0: + return false + default: + return true + } +} + +type stackEntry struct { + item interface{} + idx int +} + +type StackMachine struct { + prefix []byte + trName string + stack []stackEntry + lastVersion int64 + threads sync.WaitGroup + verbose bool + de *DirectoryExtension +} + +func newStackMachine(prefix []byte, verbose bool) *StackMachine { + sm := StackMachine{verbose: verbose, prefix: prefix, de: newDirectoryExtension(), trName: string(prefix[:])} + return &sm +} + +func (sm *StackMachine) waitAndPop() (ret stackEntry) { + defer func() { + if r := recover(); r != nil { + switch r := r.(type) { + case fdb.Error: + ret.item = []byte(tuple.Tuple{[]byte("ERROR"), []byte(fmt.Sprintf("%d", r.Code))}.Pack()) + default: + panic(r) + } + } + }() + + ret, sm.stack = sm.stack[len(sm.stack) - 1], sm.stack[:len(sm.stack) - 1] + switch el := ret.item.(type) { + case int64, []byte, string: + case fdb.Key: + ret.item = []byte(el) + case fdb.FutureNil: + el.MustGet() + ret.item = []byte("RESULT_NOT_PRESENT") + case fdb.FutureByteSlice: + v := el.MustGet() + if v != nil { + ret.item = v + } else { + ret.item = []byte("RESULT_NOT_PRESENT") + } + case fdb.FutureKey: + ret.item = []byte(el.MustGet()) + case nil: + default: + log.Fatalf("Don't know how to pop stack element %v %T\n", el, el) + } + return +} + +func (sm *StackMachine) popSelector() fdb.KeySelector { + sel := fdb.KeySelector{fdb.Key(sm.waitAndPop().item.([]byte)), int64ToBool(sm.waitAndPop().item.(int64)), int(sm.waitAndPop().item.(int64))} + return sel +} + +func (sm *StackMachine) popKeyRange() fdb.KeyRange { + kr := fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))} + return kr +} + +func (sm *StackMachine) popRangeOptions() fdb.RangeOptions { + ro := fdb.RangeOptions{Limit: int(sm.waitAndPop().item.(int64)), Reverse: int64ToBool(sm.waitAndPop().item.(int64)), Mode: fdb.StreamingMode(sm.waitAndPop().item.(int64) + 1)} + return ro +} + +func (sm *StackMachine) popPrefixRange() fdb.ExactRange { + er, e := fdb.PrefixRange(sm.waitAndPop().item.([]byte)) + if e != nil { + panic(e) + } + return er +} + +func (sm *StackMachine) pushRange(idx int, sl []fdb.KeyValue, prefixFilter []byte) { + var t tuple.Tuple = make(tuple.Tuple, 0, len(sl) * 2) + + for _, kv := range(sl) { + if prefixFilter == nil || bytes.HasPrefix(kv.Key, prefixFilter) { + t = append(t, kv.Key) + t = append(t, kv.Value) + } + } + + sm.store(idx, []byte(t.Pack())) +} + +func (sm *StackMachine) store(idx int, item interface{}) { + sm.stack = append(sm.stack, stackEntry{item, idx}) +} + +func (sm *StackMachine) dumpStack() { + for i := len(sm.stack) - 1; i >= 0; i-- { + fmt.Printf(" %d.", sm.stack[i].idx) + el := sm.stack[i].item + switch el := el.(type) { + case int64: + fmt.Printf(" %d", el) + case fdb.FutureNil: + fmt.Printf(" FutureNil") + case fdb.FutureByteSlice: + fmt.Printf(" FutureByteSlice") + case fdb.FutureKey: + fmt.Printf(" FutureKey") + case []byte: + fmt.Printf(" %+q", string(el)) + case fdb.Key: + fmt.Printf(" %+q", string(el)) + case string: + fmt.Printf(" %+q", el) + case nil: + fmt.Printf(" nil") + default: + log.Fatalf("Don't know how to dump stack element %v %T\n", el, el) + } + if i != 0 { + fmt.Printf(",") + } + } +} + +func (sm *StackMachine) executeMutation(t fdb.Transactor, f func (fdb.Transaction) (interface{}, error), isDB bool, idx int) { + _, e := t.Transact(f) + if e != nil { + panic(e) + } + if isDB { + sm.store(idx, []byte("RESULT_NOT_PRESENT")) + } +} + +func (sm *StackMachine) checkWatches(watches [4]fdb.FutureNil, expected bool) (bool) { + for _, watch := range(watches) { + if watch.IsReady() || expected { + e := watch.Get() + if e != nil { + switch e := e.(type) { + case fdb.Error: + tr, tr_error := db.CreateTransaction() + if tr_error != nil { + panic(tr_error) + } + tr.OnError(e).MustGet() + default: + panic(e) + } + } + if !expected { + return false + } + } + } + + return true +} + +func (sm *StackMachine) testWatches() { + for { + _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + tr.Set(fdb.Key("w0"), []byte("0")) + tr.Set(fdb.Key("w2"), []byte("2")) + tr.Set(fdb.Key("w3"), []byte("3")) + return nil, nil + }) + if e != nil { panic(e) } + + var watches [4]fdb.FutureNil + + _, e = db.Transact(func(tr fdb.Transaction) (interface{}, error) { + watches[0] = tr.Watch(fdb.Key("w0")) + watches[1] = tr.Watch(fdb.Key("w1")) + watches[2] = tr.Watch(fdb.Key("w2")) + watches[3] = tr.Watch(fdb.Key("w3")) + + tr.Set(fdb.Key("w0"), []byte("0")) + tr.Clear(fdb.Key("w1")) + return nil, nil + }) + if e != nil { panic(e) } + + time.Sleep(5 * time.Second) + + if !sm.checkWatches(watches, false) { + continue + } + + _, e = db.Transact(func(tr fdb.Transaction) (interface{}, error) { + tr.Set(fdb.Key("w0"), []byte("a")) + tr.Set(fdb.Key("w1"), []byte("b")) + tr.Clear(fdb.Key("w2")) + tr.BitXor(fdb.Key("w3"), []byte("\xff\xff")) + return nil, nil + }) + if e != nil { panic(e) } + + if sm.checkWatches(watches, true) { + return + } + } +} + +func (sm *StackMachine) testLocality() { + _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + tr.Options().SetTimeout(60*1000) + tr.Options().SetReadSystemKeys() + boundaryKeys, e := db.LocalityGetBoundaryKeys(fdb.KeyRange{fdb.Key(""), fdb.Key("\xff\xff")}, 0, 0) + if e != nil { panic(e) } + + for i:=0; i 0 { + entries[len(sm.stack)-1] = sm.waitAndPop() + if len(entries) == 100 { + sm.logStack(entries, prefix) + entries = make(map[int]stackEntry) + } + } + + sm.logStack(entries, prefix) + case op == "GET": + key := fdb.Key(sm.waitAndPop().item.([]byte)) + res, e := rt.ReadTransact(func (rtr fdb.ReadTransaction) (interface{}, error) { + return rtr.Get(key), nil + }) + if e != nil { panic(e) } + + sm.store(idx, res.(fdb.FutureByteSlice)) + case op == "COMMIT": + sm.store(idx, sm.currentTransaction().Commit()) + case op == "RESET": + sm.currentTransaction().Reset() + case op == "CLEAR": + key := fdb.Key(sm.waitAndPop().item.([]byte)) + sm.executeMutation(t, func (tr fdb.Transaction) (interface{}, error) { + tr.Clear(key) + return nil, nil + }, isDB, idx) + case op == "SET_READ_VERSION": + sm.currentTransaction().SetReadVersion(sm.lastVersion) + case op == "WAIT_FUTURE": + entry := sm.waitAndPop() + sm.store(entry.idx, entry.item) + case op == "GET_COMMITTED_VERSION": + sm.lastVersion, e = sm.currentTransaction().GetCommittedVersion() + if e != nil { + panic(e) + } + sm.store(idx, []byte("GOT_COMMITTED_VERSION")) + case op == "GET_VERSIONSTAMP": + sm.store(idx, sm.currentTransaction().GetVersionstamp()) + case op == "GET_KEY": + sel := sm.popSelector() + prefix := sm.waitAndPop().item.([]byte) + res, e := rt.ReadTransact(func (rtr fdb.ReadTransaction) (interface{}, error) { + return rtr.GetKey(sel).MustGet(), nil + }) + if e != nil { panic(e) } + + key := res.(fdb.Key) + + if bytes.HasPrefix(key, prefix) { + sm.store(idx, key) + } else if bytes.Compare(key, prefix) < 0 { + sm.store(idx, prefix) + } else { + s, e := fdb.Strinc(prefix) + if e != nil { panic(e) } + sm.store(idx, s) + } + case strings.HasPrefix(op, "GET_RANGE"): + var r fdb.Range + + switch op[9:] { + case "_STARTS_WITH": + r = sm.popPrefixRange() + case "_SELECTOR": + r = fdb.SelectorRange{sm.popSelector(), sm.popSelector()} + case "": + r = sm.popKeyRange() + } + + ro := sm.popRangeOptions() + var prefix []byte = nil + if op[9:] == "_SELECTOR" { + prefix = sm.waitAndPop().item.([]byte) + } + + res, e := rt.ReadTransact(func (rtr fdb.ReadTransaction) (interface{}, error) { + return rtr.GetRange(r, ro).GetSliceOrPanic(), nil + }) + if e != nil { panic(e) } + + sm.pushRange(idx, res.([]fdb.KeyValue), prefix) + case strings.HasPrefix(op, "CLEAR_RANGE"): + var er fdb.ExactRange + + switch op[11:] { + case "_STARTS_WITH": + er = sm.popPrefixRange() + case "": + er = sm.popKeyRange() + } + + sm.executeMutation(t, func (tr fdb.Transaction) (interface{}, error) { + tr.ClearRange(er) + return nil, nil + }, isDB, idx) + case op == "TUPLE_PACK": + var t tuple.Tuple + count := sm.waitAndPop().item.(int64) + for i := 0; i < int(count); i++ { + t = append(t, sm.waitAndPop().item) + } + sm.store(idx, []byte(t.Pack())) + case op == "TUPLE_UNPACK": + t, e := tuple.Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) + if e != nil { + panic(e) + } + for _, el := range(t) { + sm.store(idx, []byte(tuple.Tuple{el}.Pack())) + } + case op == "TUPLE_RANGE": + var t tuple.Tuple + count := sm.waitAndPop().item.(int64) + for i := 0; i < int(count); i++ { + t = append(t, sm.waitAndPop().item) + } + bk, ek := t.FDBRangeKeys() + sm.store(idx, []byte(bk.FDBKey())) + sm.store(idx, []byte(ek.FDBKey())) + case op == "START_THREAD": + newsm := newStackMachine(sm.waitAndPop().item.([]byte), verbose) + sm.threads.Add(1) + go func() { + newsm.Run() + sm.threads.Done() + }() + case op == "WAIT_EMPTY": + prefix := sm.waitAndPop().item.([]byte) + er, e := fdb.PrefixRange(prefix) + if e != nil { + panic(e) + } + db.Transact(func (tr fdb.Transaction) (interface{}, error) { + v := tr.GetRange(er, fdb.RangeOptions{}).GetSliceOrPanic() + if len(v) != 0 { + panic(fdb.Error{1020}) + } + return nil, nil + }) + sm.store(idx, []byte("WAITED_FOR_EMPTY")) + case op == "READ_CONFLICT_RANGE": + e = sm.currentTransaction().AddReadConflictRange(fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))}) + if e != nil { + panic(e) + } + sm.store(idx, []byte("SET_CONFLICT_RANGE")) + case op == "WRITE_CONFLICT_RANGE": + e = sm.currentTransaction().AddWriteConflictRange(fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))}) + if e != nil { + panic(e) + } + sm.store(idx, []byte("SET_CONFLICT_RANGE")) + case op == "READ_CONFLICT_KEY": + e = sm.currentTransaction().AddReadConflictKey(fdb.Key(sm.waitAndPop().item.([]byte))) + if e != nil { + panic(e) + } + sm.store(idx, []byte("SET_CONFLICT_KEY")) + case op == "WRITE_CONFLICT_KEY": + e = sm.currentTransaction().AddWriteConflictKey(fdb.Key(sm.waitAndPop().item.([]byte))) + if e != nil { + panic(e) + } + sm.store(idx, []byte("SET_CONFLICT_KEY")) + case op == "ATOMIC_OP": + opname := strings.Replace(strings.Title(strings.Replace(strings.ToLower(sm.waitAndPop().item.(string)), "_", " ", -1)), " ", "", -1) + key := fdb.Key(sm.waitAndPop().item.([]byte)) + value := sm.waitAndPop().item.([]byte) + sm.executeMutation(t, func (tr fdb.Transaction) (interface{}, error) { + reflect.ValueOf(tr).MethodByName(opname).Call([]reflect.Value{reflect.ValueOf(key), reflect.ValueOf(value)}) + return nil, nil + }, isDB, idx) + case op == "DISABLE_WRITE_CONFLICT": + sm.currentTransaction().Options().SetNextWriteNoWriteConflictRange() + case op == "CANCEL": + sm.currentTransaction().Cancel() + case op == "UNIT_TESTS": + db.Options().SetLocationCacheSize(100001) + db.Options().SetMaxWatches(10001) + + _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + tr.Options().SetPrioritySystemImmediate() + tr.Options().SetPriorityBatch() + tr.Options().SetCausalReadRisky() + tr.Options().SetCausalWriteRisky() + tr.Options().SetReadYourWritesDisable() + tr.Options().SetReadAheadDisable() + tr.Options().SetReadSystemKeys() + tr.Options().SetAccessSystemKeys() + tr.Options().SetDurabilityDevNullIsWebScale() + tr.Options().SetTimeout(60*1000) + tr.Options().SetRetryLimit(50) + tr.Options().SetMaxRetryDelay(100) + tr.Options().SetUsedDuringCommitProtectionDisable() + tr.Options().SetTransactionLoggingEnable("my_transaction") + + return tr.Get(fdb.Key("\xff")).MustGet(), nil + }) + + if e != nil { panic(e) } + + sm.testWatches() + sm.testLocality() + + case strings.HasPrefix(op, "DIRECTORY_"): + sm.de.processOp(sm, op[10:], isDB, idx, t, rt) + default: + log.Fatalf("Unhandled operation %s\n", string(inst[0].([]byte))) + } + + if sm.verbose { + fmt.Printf(" to [") + sm.dumpStack() + fmt.Printf(" ] (%d)\n\n", len(sm.stack)) + } + + runtime.Gosched() +} + +func (sm *StackMachine) Run() { + r, e := db.Transact(func (tr fdb.Transaction) (interface{}, error) { + return tr.GetRange(tuple.Tuple{sm.prefix}, fdb.RangeOptions{}).GetSliceOrPanic(), nil + }) + if e != nil { + panic(e) + } + + instructions := r.([]fdb.KeyValue) + + for i, kv := range(instructions) { + inst, _ := tuple.Unpack(fdb.Key(kv.Value)) + + if sm.verbose { + fmt.Printf("Instruction %d\n", i) + } + sm.processInst(i, inst) + } + + sm.threads.Wait() +} + +var db fdb.Database + +func main() { + var clusterFile string + + prefix := []byte(os.Args[1]) + if len(os.Args) > 3 { + clusterFile = os.Args[3] + } + + var e error + var apiVersion int + + apiVersion, e = strconv.Atoi(os.Args[2]) + if e != nil { + log.Fatal(e) + } + + e = fdb.APIVersion(apiVersion) + if e != nil { + log.Fatal(e) + } + + db, e = fdb.Open(clusterFile, []byte("DB")) + if e != nil { + log.Fatal(e) + } + + sm := newStackMachine(prefix, verbose) + + sm.Run() +} diff --git a/bindings/go/src/_util/translate_fdb_options.go b/bindings/go/src/_util/translate_fdb_options.go new file mode 100644 index 0000000000..35f6d61721 --- /dev/null +++ b/bindings/go/src/_util/translate_fdb_options.go @@ -0,0 +1,228 @@ +/* + * translate_fdb_options.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go options translator + +package main + +import ( + "encoding/xml" + "io/ioutil" + "fmt" + "log" + "strings" + "os" + "unicode" + "unicode/utf8" + "go/doc" +) + +type Option struct { + Name string `xml:"name,attr"` + Code int `xml:"code,attr"` + ParamType string `xml:"paramType,attr"` + ParamDesc string `xml:"paramDescription,attr"` + Description string `xml:"description,attr"` + Hidden bool `xml:"hidden,attr"` +} +type Scope struct { + Name string `xml:"name,attr"` + Option []Option +} +type Options struct { + Scope []Scope +} + +func writeOptString(receiver string, function string, opt Option) { + fmt.Printf(`func (o %s) %s(param string) error { + return o.setOpt(%d, []byte(param)) +} +`, receiver, function, opt.Code) +} + +func writeOptBytes(receiver string, function string, opt Option) { + fmt.Printf(`func (o %s) %s(param []byte) error { + return o.setOpt(%d, param) +} +`, receiver, function, opt.Code) +} + +func writeOptInt(receiver string, function string, opt Option) { + fmt.Printf(`func (o %s) %s(param int64) error { + b, e := int64ToBytes(param) + if e != nil { + return e + } + return o.setOpt(%d, b) +} +`, receiver, function, opt.Code) +} + +func writeOptNone(receiver string, function string, opt Option) { + fmt.Printf(`func (o %s) %s() error { + return o.setOpt(%d, nil) +} +`, receiver, function, opt.Code) +} + +func writeOpt(receiver string, opt Option) { + function := "Set" + translateName(opt.Name) + + fmt.Println() + + if opt.Description != "" { + fmt.Printf("// %s\n", opt.Description) + if opt.ParamDesc != "" { + fmt.Printf("//\n// Parameter: %s\n", opt.ParamDesc) + } + } else { + fmt.Printf("// Not yet implemented.\n") + } + + switch opt.ParamType { + case "String": + writeOptString(receiver, function, opt) + case "Bytes": + writeOptBytes(receiver, function, opt) + case "Int": + writeOptInt(receiver, function, opt) + case "": + writeOptNone(receiver, function, opt) + default: + log.Fatalf("Totally unexpected ParamType %s", opt.ParamType) + } +} + +func translateName(old string) string { + return strings.Replace(strings.Title(strings.Replace(old, "_", " ", -1)), " ", "", -1) +} + +func lowerFirst (s string) string { + if s == "" { + return "" + } + r, n := utf8.DecodeRuneInString(s) + return string(unicode.ToLower(r)) + s[n:] +} + +func writeMutation(opt Option) { + desc := lowerFirst(opt.Description) + tname := translateName(opt.Name) + fmt.Printf(` +// %s %s +func (t Transaction) %s(key KeyConvertible, param []byte) { + t.atomicOp(key.FDBKey(), param, %d) +} +`, tname, desc, tname, opt.Code) +} + +func writeEnum(scope Scope, opt Option, delta int) { + fmt.Println() + if opt.Description != "" { + doc.ToText(os.Stdout, opt.Description, " // ", "", 73) + // fmt.Printf(" // %s\n", opt.Description) + } + fmt.Printf(" %s %s = %d\n", scope.Name + translateName(opt.Name), scope.Name, opt.Code + delta) +} + +func main() { + var err error + + v := Options{} + + data, err := ioutil.ReadAll(os.Stdin) + if err != nil { + log.Fatal(err) + } + + err = xml.Unmarshal(data, &v) + if err != nil { + log.Fatal(err) + } + + fmt.Print(`// DO NOT EDIT THIS FILE BY HAND. This file was generated using +// translate_fdb_options.go, part of the fdb-go repository, and a copy of the +// fdb.options file (installed as part of the FoundationDB client, typically +// found as /usr/include/foundationdb/fdb.options). + +// To regenerate this file, from the top level of an fdb-go repository checkout, +// run: +// $ go run _util/translate_fdb_options.go < /usr/include/foundationdb/fdb.options > fdb/generated.go + +package fdb + +import ( + "bytes" + "encoding/binary" +) + +func int64ToBytes(i int64) ([]byte, error) { + buf := new(bytes.Buffer) + if e := binary.Write(buf, binary.LittleEndian, i); e != nil { + return nil, e + } + return buf.Bytes(), nil +} +`) + + for _, scope := range(v.Scope) { + if strings.HasSuffix(scope.Name, "Option") { + receiver := scope.Name + "s" + + for _, opt := range(scope.Option) { + if opt.Description != "Deprecated" && !opt.Hidden { // Eww + writeOpt(receiver, opt) + } + } + continue + } + + if scope.Name == "MutationType" { + for _, opt := range(scope.Option) { + if opt.Description != "Deprecated" && !opt.Hidden { // Eww + writeMutation(opt) + } + } + continue + } + + // We really need the default StreamingMode (0) to be ITERATOR + var d int + if scope.Name == "StreamingMode" { + d = 1 + } + + // ConflictRangeType shouldn't be exported + if scope.Name == "ConflictRangeType" { + scope.Name = "conflictRangeType" + } + + fmt.Printf(` +type %s int +const ( +`, scope.Name) + for _, opt := range(scope.Option) { + if !opt.Hidden { + writeEnum(scope, opt, d) + } + } + fmt.Println(")") + } +} diff --git a/bindings/go/src/fdb/cluster.go b/bindings/go/src/fdb/cluster.go new file mode 100644 index 0000000000..908021c7f7 --- /dev/null +++ b/bindings/go/src/fdb/cluster.go @@ -0,0 +1,74 @@ +/* + * cluster.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +package fdb + +/* + #define FDB_API_VERSION 500 + #include +*/ +import "C" + +import ( + "runtime" +) + +// Cluster is a handle to a FoundationDB cluster. Cluster is a lightweight +// object that may be efficiently copied, and is safe for concurrent use by +// multiple goroutines. +// +// It is generally preferable to use Open or OpenDefault to obtain a database +// handle directly. +type Cluster struct { + *cluster +} + +type cluster struct { + ptr *C.FDBCluster +} + +func (c *cluster) destroy() { + C.fdb_cluster_destroy(c.ptr) +} + +// OpenDatabase returns a database handle from the FoundationDB cluster. It is +// generally preferable to use Open or OpenDefault to obtain a database handle +// directly. +// +// In the current release, the database name must be []byte("DB"). +func (c Cluster) OpenDatabase(dbName []byte) (Database, error) { + f := C.fdb_cluster_create_database(c.ptr, byteSliceToPtr(dbName), C.int(len(dbName))) + fdb_future_block_until_ready(f) + + var outd *C.FDBDatabase + + if err := C.fdb_future_get_database(f, &outd); err != 0 { + return Database{}, Error{int(err)} + } + + C.fdb_future_destroy(f) + + d := &database{outd} + runtime.SetFinalizer(d, (*database).destroy) + + return Database{d}, nil +} diff --git a/bindings/go/src/fdb/database.go b/bindings/go/src/fdb/database.go new file mode 100644 index 0000000000..af72e41211 --- /dev/null +++ b/bindings/go/src/fdb/database.go @@ -0,0 +1,238 @@ +/* + * database.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +package fdb + +/* + #define FDB_API_VERSION 500 + #include +*/ +import "C" + +import ( + "runtime" +) + +// Database is a handle to a FoundationDB database. Database is a lightweight +// object that may be efficiently copied, and is safe for concurrent use by +// multiple goroutines. +// +// Although Database provides convenience methods for reading and writing data, +// modifications to a database are usually made via transactions, which are +// usually created and committed automatically by the (Database).Transact +// method. +type Database struct { + *database +} + +type database struct { + ptr *C.FDBDatabase +} + +// DatabaseOptions is a handle with which to set options that affect a Database +// object. A DatabaseOptions instance should be obtained with the +// (Database).Options method. +type DatabaseOptions struct { + d *database +} + +func (opt DatabaseOptions) setOpt(code int, param []byte) error { + return setOpt(func(p *C.uint8_t, pl C.int) C.fdb_error_t { + return C.fdb_database_set_option(opt.d.ptr, C.FDBDatabaseOption(code), p, pl) + }, param) +} + +func (d *database) destroy() { + C.fdb_database_destroy(d.ptr) +} + +// CreateTransaction returns a new FoundationDB transaction. It is generally +// preferable to use the (Database).Transact method, which handles +// automatically creating and committing a transaction with appropriate retry +// behavior. +func (d Database) CreateTransaction() (Transaction, error) { + var outt *C.FDBTransaction + + if err := C.fdb_database_create_transaction(d.ptr, &outt); err != 0 { + return Transaction{}, Error{int(err)} + } + + t := &transaction{outt, d} + runtime.SetFinalizer(t, (*transaction).destroy) + + return Transaction{t}, nil +} + +func retryable(wrapped func() (interface{}, error), onError func(Error) FutureNil) (ret interface{}, e error) { + for { + ret, e = wrapped() + + /* No error means success! */ + if e == nil { + return + } + + ep, ok := e.(Error) + if ok { + e = onError(ep).Get() + } + + /* If OnError returns an error, then it's not + /* retryable; otherwise take another pass at things */ + if e != nil { + return + } + } +} + +// Transact runs a caller-provided function inside a retry loop, providing it +// with a newly created Transaction. After the function returns, the Transaction +// will be committed automatically. Any error during execution of the function +// (by panic or return) or the commit will cause the function and commit to be +// retried or, if fatal, return the error to the caller. +// +// When working with Future objects in a transactional function, you may either +// explicity check and return error values using Get, or call MustGet. Transact +// will recover a panicked Error and either retry the transaction or return the +// error. +// +// Do not return Future objects from the function provided to Transact. The +// Transaction created by Transact may be finalized at any point after Transact +// returns, resulting in the cancellation of any outstanding +// reads. Additionally, any errors returned or panicked by the Future will no +// longer be able to trigger a retry of the caller-provided function. +// +// See the Transactor interface for an example of using Transact with +// Transaction and Database objects. +func (d Database) Transact(f func(Transaction) (interface{}, error)) (interface{}, error) { + tr, e := d.CreateTransaction() + /* Any error here is non-retryable */ + if e != nil { + return nil, e + } + + wrapped := func() (ret interface{}, e error) { + defer panicToError(&e) + + ret, e = f(tr) + + if e == nil { + e = tr.Commit().Get() + } + + return + } + + return retryable(wrapped, tr.OnError) +} + +// ReadTransact runs a caller-provided function inside a retry loop, providing +// it with a newly created Transaction (as a ReadTransaction). Any error during +// execution of the function (by panic or return) will cause the function to be +// retried or, if fatal, return the error to the caller. +// +// When working with Future objects in a read-only transactional function, you +// may either explicity check and return error values using Get, or call +// MustGet. ReadTransact will recover a panicked Error and either retry the +// transaction or return the error. +// +// Do not return Future objects from the function provided to ReadTransact. The +// Transaction created by ReadTransact may be finalized at any point after +// ReadTransact returns, resulting in the cancellation of any outstanding +// reads. Additionally, any errors returned or panicked by the Future will no +// longer be able to trigger a retry of the caller-provided function. +// +// See the ReadTransactor interface for an example of using ReadTransact with +// Transaction, Snapshot and Database objects. +func (d Database) ReadTransact(f func(ReadTransaction) (interface{}, error)) (interface{}, error) { + tr, e := d.CreateTransaction() + /* Any error here is non-retryable */ + if e != nil { + return nil, e + } + + wrapped := func() (ret interface{}, e error) { + defer panicToError(&e) + + ret, e = f(tr) + + if e == nil { + e = tr.Commit().Get() + } + + return + } + + return retryable(wrapped, tr.OnError) +} + +// Options returns a DatabaseOptions instance suitable for setting options +// specific to this database. +func (d Database) Options() DatabaseOptions { + return DatabaseOptions{d.database} +} + +// LocalityGetBoundaryKeys returns a slice of keys that fall within the provided +// range. Each key is located at the start of a contiguous range stored on a +// single server. +// +// If limit is non-zero, only the first limit keys will be returned. In large +// databases, the number of boundary keys may be large. In these cases, a +// non-zero limit should be used, along with multiple calls to +// LocalityGetBoundaryKeys. +// +// If readVersion is non-zero, the boundary keys as of readVersion will be +// returned. +func (d Database) LocalityGetBoundaryKeys(er ExactRange, limit int, readVersion int64) ([]Key, error) { + tr, e := d.CreateTransaction() + if e != nil { + return nil, e + } + + if readVersion != 0 { + tr.SetReadVersion(readVersion) + } + + tr.Options().SetReadSystemKeys() + tr.Options().SetLockAware() + + bk, ek := er.FDBRangeKeys() + ffer := KeyRange{append(Key("\xFF/keyServers/"), bk.FDBKey()...), append(Key("\xFF/keyServers/"), ek.FDBKey()...)} + + kvs, e := tr.Snapshot().GetRange(ffer, RangeOptions{Limit: limit}).GetSliceWithError() + if e != nil { + return nil, e + } + + size := len(kvs) + if limit != 0 && limit < size { + size = limit + } + + boundaries := make([]Key, size) + + for i := 0; i < size; i++ { + boundaries[i] = kvs[i].Key[13:] + } + + return boundaries, nil +} diff --git a/bindings/go/src/fdb/directory/allocator.go b/bindings/go/src/fdb/directory/allocator.go new file mode 100644 index 0000000000..69400fff8d --- /dev/null +++ b/bindings/go/src/fdb/directory/allocator.go @@ -0,0 +1,156 @@ +/* + * allocator.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go Directory Layer + +package directory + +import ( + "fdb" + "fdb/subspace" + "encoding/binary" + "bytes" + "math/rand" + "sync" +) + +var oneBytes = []byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} +var allocatorMutex = sync.Mutex{} + +type highContentionAllocator struct { + counters, recent subspace.Subspace +} + +func newHCA(s subspace.Subspace) highContentionAllocator { + var hca highContentionAllocator + + hca.counters = s.Sub(0) + hca.recent = s.Sub(1) + + return hca +} + +func windowSize(start int64) int64 { + // Larger window sizes are better for high contention, smaller sizes for + // keeping the keys small. But if there are many allocations, the keys + // can't be too small. So start small and scale up. We don't want this to + // ever get *too* big because we have to store about window_size/2 recent + // items. + if start < 255 { return 64 } + if start < 65535 { return 1024 } + return 8192 +} + +func (hca highContentionAllocator) allocate(tr fdb.Transaction, s subspace.Subspace) (subspace.Subspace, error) { + for { + rr := tr.Snapshot().GetRange(hca.counters, fdb.RangeOptions{Limit:1, Reverse:true}) + kvs := rr.GetSliceOrPanic() + + var start int64 + var window int64 + + if len(kvs) == 1 { + t, e := hca.counters.Unpack(kvs[0].Key) + if e != nil { + return nil, e + } + start = t[0].(int64) + } + + windowAdvanced := false + for { + allocatorMutex.Lock() + + if windowAdvanced { + tr.ClearRange(fdb.KeyRange{hca.counters, hca.counters.Sub(start)}) + tr.Options().SetNextWriteNoWriteConflictRange() + tr.ClearRange(fdb.KeyRange{hca.recent, hca.recent.Sub(start)}) + } + + // Increment the allocation count for the current window + tr.Add(hca.counters.Sub(start), oneBytes) + countFuture := tr.Snapshot().Get(hca.counters.Sub(start)) + + allocatorMutex.Unlock() + + countStr, e := countFuture.Get() + if e != nil { + return nil, e + } + + var count int64 + if countStr == nil { + count = 0 + } else { + e = binary.Read(bytes.NewBuffer(countStr), binary.LittleEndian, &count) + if e != nil { + return nil, e + } + } + + window = windowSize(start) + if count * 2 < window { + break + } + + start += window + windowAdvanced = true + } + + for { + // As of the snapshot being read from, the window is less than half + // full, so this should be expected to take 2 tries. Under high + // contention (and when the window advances), there is an additional + // subsequent risk of conflict for this transaction. + candidate := rand.Int63n(window) + start + key := hca.recent.Sub(candidate) + + allocatorMutex.Lock() + + latestCounter := tr.Snapshot().GetRange(hca.counters, fdb.RangeOptions{Limit:1, Reverse:true}) + candidateValue := tr.Get(key) + tr.Options().SetNextWriteNoWriteConflictRange() + tr.Set(key, []byte("")) + + allocatorMutex.Unlock() + + kvs = latestCounter.GetSliceOrPanic() + if len(kvs) > 0 { + t, e := hca.counters.Unpack(kvs[0].Key) + if e != nil { + return nil, e + } + currentStart := t[0].(int64) + if currentStart > start { + break + } + } + + v, e := candidateValue.Get() + if e != nil { + return nil, e + } + if v == nil { + tr.AddWriteConflictKey(key) + return s.Sub(candidate), nil + } + } + } +} diff --git a/bindings/go/src/fdb/directory/directory.go b/bindings/go/src/fdb/directory/directory.go new file mode 100644 index 0000000000..72e82230a1 --- /dev/null +++ b/bindings/go/src/fdb/directory/directory.go @@ -0,0 +1,241 @@ +/* + * directory.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go Directory Layer + +// Package directory provides a tool for managing related subspaces. Directories +// are a recommended approach for administering applications. Each application +// should create or open at least one directory to manage its subspaces. +// +// For general guidance on directory usage, see the Directories section of the +// Developer Guide +// (https://foundationdb.org/documentation/developer-guide.html#developer-guide-directories). +// +// Directories are identified by hierarchical paths analogous to the paths in a +// Unix-like file system. A path is represented as a slice of strings. Each +// directory has an associated subspace used to store its content. The directory +// layer maps each path to a short prefix used for the corresponding +// subspace. In effect, directories provide a level of indirection for access to +// subspaces. +// +// Directory operations are transactional. A byte slice layer option is used as +// a metadata identifier when opening a directory. +package directory + +import ( + "fdb" + "fdb/subspace" + "errors" +) + +const ( + _SUBDIRS int = 0 + + // []int32{1,0,0} by any other name + _MAJORVERSION int32 = 1 + _MINORVERSION int32 = 0 + _MICROVERSION int32 = 0 +) + +// Directory represents a subspace of keys in a FoundationDB database, +// identified by a hierarchical path. +type Directory interface { + // CreateOrOpen opens the directory specified by path (relative to this + // Directory), and returns the directory and its contents as a + // DirectorySubspace. If the directory does not exist, it is created + // (creating parent directories if necessary). + // + // If the byte slice layer is specified and the directory is new, it is + // recorded as the layer; if layer is specified and the directory already + // exists, it is compared against the layer specified when the directory was + // created, and an error is returned if they differ. + CreateOrOpen(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) + + // Open opens the directory specified by path (relative to this Directory), + // and returns the directory and its contents as a DirectorySubspace (or an + // error if the directory does not exist). + // + // If the byte slice layer is specified, it is compared against the layer + // specified when the directory was created, and an error is returned if + // they differ. + Open(rt fdb.ReadTransactor, path []string, layer []byte) (DirectorySubspace, error) + + // Create creates a directory specified by path (relative to this + // Directory), and returns the directory and its contents as a + // DirectorySubspace (or an error if the directory already exists). + // + // If the byte slice layer is specified, it is recorded as the layer and + // will be checked when opening the directory in the future. + Create(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) + + // CreatePrefix behaves like Create, but uses a manually specified byte + // slice prefix to physically store the contents of this directory, rather + // than an automatically allocated prefix. + // + // If this Directory was created in a root directory that does not allow + // manual prefixes, CreatePrefix will return an error. The default root + // directory does not allow manual prefixes. + CreatePrefix(t fdb.Transactor, path []string, layer []byte, prefix []byte) (DirectorySubspace, error) + + // Move moves the directory at oldPath to newPath (both relative to this + // Directory), and returns the directory (at its new location) and its + // contents as a DirectorySubspace. Move will return an error if a directory + // does not exist at oldPath, a directory already exists at newPath, or the + // parent directory of newPath does not exist. + // + // There is no effect on the physical prefix of the given directory or on + // clients that already have the directory open. + Move(t fdb.Transactor, oldPath []string, newPath []string) (DirectorySubspace, error) + + // MoveTo moves this directory to newAbsolutePath (relative to the root + // directory of this Directory), and returns the directory (at its new + // location) and its contents as a DirectorySubspace. MoveTo will return an + // error if a directory already exists at newAbsolutePath or the parent + // directory of newAbsolutePath does not exist. + // + // There is no effect on the physical prefix of the given directory or on + // clients that already have the directory open. + MoveTo(t fdb.Transactor, newAbsolutePath []string) (DirectorySubspace, error) + + // Remove removes the directory at path (relative to this Directory), its + // content, and all subdirectories. Remove returns true if a directory + // existed at path and was removed, and false if no directory exists at + // path. + // + // Note that clients that have already opened this directory might still + // insert data into its contents after removal. + Remove(t fdb.Transactor, path []string) (bool, error) + + // Exists returns true if the directory at path (relative to this Directory) + // exists, and false otherwise. + Exists(rt fdb.ReadTransactor, path []string) (bool, error) + + // List returns the names of the immediate subdirectories of the directory + // at path (relative to this Directory) as a slice of strings. Each string + // is the name of the last component of a subdirectory's path. + List(rt fdb.ReadTransactor, path []string) ([]string, error) + + // GetLayer returns the layer specified when this Directory was created. + GetLayer() []byte + + // GetPath returns the path with which this Directory was opened. + GetPath() []string +} + +func stringsEqual(a, b []string) bool { + if len(a) != len(b) { + return false + } + for i, v := range a { + if v != b[i] { + return false + } + } + return true +} + +func moveTo(t fdb.Transactor, dl directoryLayer, path, newAbsolutePath []string) (DirectorySubspace, error) { + partition_len := len(dl.path) + + if !stringsEqual(newAbsolutePath[:partition_len], dl.path) { + return nil, errors.New("cannot move between partitions") + } + + return dl.Move(t, path[partition_len:], newAbsolutePath[partition_len:]) +} + +var root = NewDirectoryLayer(subspace.FromBytes([]byte{0xFE}), subspace.AllKeys(), false) + +// CreateOrOpen opens the directory specified by path (resolved relative to the +// default root directory), and returns the directory and its contents as a +// DirectorySubspace. If the directory does not exist, it is created (creating +// parent directories if necessary). +// +// If the byte slice layer is specified and the directory is new, it is recorded +// as the layer; if layer is specified and the directory already exists, it is +// compared against the layer specified when the directory was created, and an +// error is returned if they differ. +func CreateOrOpen(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) { + return root.CreateOrOpen(t, path, layer) +} + +// Open opens the directory specified by path (resolved relative to the default +// root directory), and returns the directory and its contents as a +// DirectorySubspace (or an error if the directory does not exist). +// +// If the byte slice layer is specified, it is compared against the layer +// specified when the directory was created, and an error is returned if they +// differ. +func Open(rt fdb.ReadTransactor, path []string, layer []byte) (DirectorySubspace, error) { + return root.Open(rt, path, layer) +} + +// Create creates a directory specified by path (resolved relative to the +// default root directory), and returns the directory and its contents as a +// DirectorySubspace (or an error if the directory already exists). +// +// If the byte slice layer is specified, it is recorded as the layer and will be +// checked when opening the directory in the future. +func Create(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) { + return root.Create(t, path, layer) +} + +// Move moves the directory at oldPath to newPath (both resolved relative to the +// default root directory), and returns the directory (at its new location) and +// its contents as a DirectorySubspace. Move will return an error if a directory +// does not exist at oldPath, a directory already exists at newPath, or the +// parent directory of newPath does not exit. +// +// There is no effect on the physical prefix of the given directory or on +// clients that already have the directory open. +func Move(t fdb.Transactor, oldPath []string, newPath []string) (DirectorySubspace, error) { + return root.Move(t, oldPath, newPath) +} + +// Exists returns true if the directory at path (relative to the default root +// directory) exists, and false otherwise. +func Exists(rt fdb.ReadTransactor, path []string) (bool, error) { + return root.Exists(rt, path) +} + +// List returns the names of the immediate subdirectories of the default root +// directory as a slice of strings. Each string is the name of the last +// component of a subdirectory's path. +func List(rt fdb.ReadTransactor, path []string) ([]string, error) { + return root.List(rt, path) +} + +// Root returns the default root directory. Any attempt to move or remove the +// root directory will return an error. +// +// The default root directory stores directory layer metadata in keys beginning +// with 0xFE, and allocates newly created directories in (unused) prefixes +// starting with 0x00 through 0xFD. This is appropriate for otherwise empty +// databases, but may conflict with other formal or informal partitionings of +// keyspace. If you already have other content in your database, you may wish to +// use NewDirectoryLayer to construct a non-standard root directory to control +// where metadata and keys are stored. +// +// As an alternative to Root, you may use the package-level functions +// CreateOrOpen, Open, Create, CreatePrefix, Move, Exists and List to operate +// directly on the default DirectoryLayer. +func Root() Directory { + return root +} diff --git a/bindings/go/src/fdb/directory/directoryLayer.go b/bindings/go/src/fdb/directory/directoryLayer.go new file mode 100644 index 0000000000..a5bfd4f102 --- /dev/null +++ b/bindings/go/src/fdb/directory/directoryLayer.go @@ -0,0 +1,589 @@ +/* + * directoryLayer.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go Directory Layer + +package directory + +import ( + "fdb" + "fdb/subspace" + "fdb/tuple" + "encoding/binary" + "bytes" + "fmt" + "errors" +) + +type directoryLayer struct { + nodeSS subspace.Subspace + contentSS subspace.Subspace + + allowManualPrefixes bool + + allocator highContentionAllocator + rootNode subspace.Subspace + + path []string +} + +// NewDirectoryLayer returns a new root directory (as a Directory). The +// subspaces nodeSS and contentSS control where the directory metadata and +// contents are stored. The default root directory has a nodeSS of +// subspace.FromBytes([]byte{0xFE}) and a contentSS of +// subspace.AllKeys(). Specifying more restrictive values for nodeSS and +// contentSS will allow using the directory layer alongside other content in a +// database. +// +// If allowManualPrefixes is false, all calls to CreatePrefix on the returned +// Directory (or any subdirectories) will fail, and all directory prefixes will +// be automatically allocated. The default root directory does not allow manual +// prefixes. +func NewDirectoryLayer(nodeSS, contentSS subspace.Subspace, allowManualPrefixes bool) Directory { + var dl directoryLayer + + dl.nodeSS = subspace.FromBytes(nodeSS.Bytes()) + dl.contentSS = subspace.FromBytes(contentSS.Bytes()) + + dl.allowManualPrefixes = allowManualPrefixes + + dl.rootNode = dl.nodeSS.Sub(dl.nodeSS.Bytes()) + dl.allocator = newHCA(dl.rootNode.Sub([]byte("hca"))) + + return dl +} + +func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transaction, path []string, layer []byte, prefix []byte, allowCreate, allowOpen bool) (DirectorySubspace, error) { + if e := dl.checkVersion(rtr, nil); e != nil { + return nil, e + } + + if prefix != nil && !dl.allowManualPrefixes { + if len(dl.path) == 0 { + return nil, errors.New("cannot specify a prefix unless manual prefixes are enabled") + } else { + return nil, errors.New("cannot specify a prefix in a partition") + } + } + + if len(path) == 0 { + return nil, errors.New("the root directory cannot be opened") + } + + existingNode := dl.find(rtr, path).prefetchMetadata(rtr) + if existingNode.exists() { + if existingNode.isInPartition(nil, false) { + subpath := existingNode.getPartitionSubpath() + enc, e := existingNode.getContents(dl, nil) + if e != nil { + return nil, e + } + return enc.(directoryPartition).createOrOpen(rtr, tr, subpath, layer, prefix, allowCreate, allowOpen) + } + + if !allowOpen { + return nil, errors.New("the directory already exists") + } + + if layer != nil && bytes.Compare(existingNode._layer.MustGet(), layer) != 0 { + return nil, errors.New("the directory was created with an incompatible layer") + } + + return existingNode.getContents(dl, nil) + } + + if !allowCreate { + return nil, errors.New("the directory does not exist") + } + + if e := dl.checkVersion(rtr, tr); e != nil { + return nil, e + } + + if prefix == nil { + newss, e := dl.allocator.allocate(*tr, dl.contentSS) + if e != nil { + return nil, fmt.Errorf("unable to allocate new directory prefix (%s)", e.Error()) + } + + if !isRangeEmpty(rtr, newss) { + return nil, fmt.Errorf("the database has keys stored at the prefix chosen by the automatic prefix allocator: %v", prefix) + } + + prefix = newss.Bytes() + + pf, e := dl.isPrefixFree(rtr.Snapshot(), prefix) + if e != nil { return nil, e } + if !pf { + return nil, errors.New("the directory layer has manually allocated prefixes that conflict with the automatic prefix allocator") + } + } else { + pf, e := dl.isPrefixFree(rtr, prefix) + if e != nil { return nil, e } + if !pf { + return nil, errors.New("the given prefix is already in use") + } + } + + var parentNode subspace.Subspace + + if len(path) > 1 { + pd, e := dl.createOrOpen(rtr, tr, path[:len(path)-1], nil, nil, true, true) + if e != nil { + return nil, e + } + parentNode = dl.nodeWithPrefix(pd.Bytes()) + } else { + parentNode = dl.rootNode + } + + if parentNode == nil { + return nil, errors.New("the parent directory does not exist") + } + + node := dl.nodeWithPrefix(prefix) + tr.Set(parentNode.Sub(_SUBDIRS, path[len(path)-1]), prefix) + + if layer == nil { + layer = []byte{} + } + + tr.Set(node.Sub([]byte("layer")), layer) + + return dl.contentsOfNode(node, path, layer) +} + +func (dl directoryLayer) CreateOrOpen(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) { + r, e := t.Transact(func (tr fdb.Transaction) (interface{}, error) { + return dl.createOrOpen(tr, &tr, path, layer, nil, true, true) + }) + if e != nil { + return nil, e + } + return r.(DirectorySubspace), nil +} + +func (dl directoryLayer) Create(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) { + r, e := t.Transact(func (tr fdb.Transaction) (interface{}, error) { + return dl.createOrOpen(tr, &tr, path, layer, nil, true, false) + }) + if e != nil { + return nil, e + } + return r.(DirectorySubspace), nil +} + +func (dl directoryLayer) CreatePrefix(t fdb.Transactor, path []string, layer []byte, prefix []byte) (DirectorySubspace, error) { + if prefix == nil { + prefix = []byte{} + } + r, e := t.Transact(func (tr fdb.Transaction) (interface{}, error) { + return dl.createOrOpen(tr, &tr, path, layer, prefix, true, false) + }) + if e != nil { + return nil, e + } + return r.(DirectorySubspace), nil +} + +func (dl directoryLayer) Open(rt fdb.ReadTransactor, path []string, layer []byte) (DirectorySubspace, error) { + r, e := rt.ReadTransact(func (rtr fdb.ReadTransaction) (interface{}, error) { + return dl.createOrOpen(rtr, nil, path, layer, nil, false, true) + }) + if e != nil { + return nil, e + } + return r.(DirectorySubspace), nil +} + +func (dl directoryLayer) Exists(rt fdb.ReadTransactor, path []string) (bool, error) { + r, e := rt.ReadTransact(func (rtr fdb.ReadTransaction) (interface{}, error) { + if e := dl.checkVersion(rtr, nil); e != nil { + return false, e + } + + node := dl.find(rtr, path).prefetchMetadata(rtr) + if !node.exists() { + return false, nil + } + + if node.isInPartition(nil, false) { + nc, e := node.getContents(dl, nil) + if e != nil { + return false, e + } + return nc.Exists(rtr, node.getPartitionSubpath()) + } + + return true, nil + }) + if e != nil { + return false, e + } + return r.(bool), nil +} + +func (dl directoryLayer) List(rt fdb.ReadTransactor, path []string) ([]string, error) { + r, e := rt.ReadTransact(func (rtr fdb.ReadTransaction) (interface{}, error) { + if e := dl.checkVersion(rtr, nil); e != nil { + return nil, e + } + + node := dl.find(rtr, path).prefetchMetadata(rtr) + if !node.exists() { + return nil, errors.New("the directory does not exist") + } + + if node.isInPartition(nil, true) { + nc, e := node.getContents(dl, nil) + if e != nil { + return nil, e + } + return nc.List(rtr, node.getPartitionSubpath()) + } + + return dl.subdirNames(rtr, node.subspace) + }) + if e != nil { + return nil, e + } + return r.([]string), nil +} + +func (dl directoryLayer) MoveTo(t fdb.Transactor, newAbsolutePath []string) (DirectorySubspace, error) { + return nil, errors.New("the root directory cannot be moved") +} + +func (dl directoryLayer) Move(t fdb.Transactor, oldPath []string, newPath []string) (DirectorySubspace, error) { + r, e := t.Transact(func (tr fdb.Transaction) (interface{}, error) { + if e := dl.checkVersion(tr, &tr); e != nil { + return nil, e + } + + sliceEnd := len(oldPath) + if sliceEnd > len(newPath) { + sliceEnd = len(newPath) + } + if stringsEqual(oldPath, newPath[:sliceEnd]) { + return nil, errors.New("the destination directory cannot be a subdirectory of the source directory") + } + + oldNode := dl.find(tr, oldPath).prefetchMetadata(tr) + newNode := dl.find(tr, newPath).prefetchMetadata(tr) + + if !oldNode.exists() { + return nil, errors.New("the source directory does not exist") + } + + if oldNode.isInPartition(nil, false) || newNode.isInPartition(nil, false) { + if !oldNode.isInPartition(nil, false) || !newNode.isInPartition(nil, false) || !stringsEqual(oldNode.path, newNode.path) { + return nil, errors.New("cannot move between partitions") + } + + nnc, e := newNode.getContents(dl, nil) + if e != nil { + return nil, e + } + return nnc.Move(tr, oldNode.getPartitionSubpath(), newNode.getPartitionSubpath()) + } + + if newNode.exists() { + return nil, errors.New("the destination directory already exists. Remove it first") + } + + parentNode := dl.find(tr, newPath[:len(newPath)-1]) + if !parentNode.exists() { + return nil, errors.New("the parent of the destination directory does not exist. Create it first") + } + + p, e := dl.nodeSS.Unpack(oldNode.subspace) + if e != nil { + return nil, e + } + tr.Set(parentNode.subspace.Sub(_SUBDIRS, newPath[len(newPath)-1]), p[0].([]byte)) + + dl.removeFromParent(tr, oldPath) + + return dl.contentsOfNode(oldNode.subspace, newPath, oldNode._layer.MustGet()) + }) + if e != nil { + return nil, e + } + return r.(DirectorySubspace), nil +} + +func (dl directoryLayer) Remove(t fdb.Transactor, path []string) (bool, error) { + r, e := t.Transact(func (tr fdb.Transaction) (interface{}, error) { + if e := dl.checkVersion(tr, &tr); e != nil { + return false, e + } + + if len(path) == 0 { + return false, errors.New("the root directory cannot be removed") + } + + node := dl.find(tr, path).prefetchMetadata(tr) + + if !node.exists() { + return false, nil + } + + if node.isInPartition(nil, false) { + nc, e := node.getContents(dl, nil) + if e != nil { + return false, e + } + return nc.(directoryPartition).Remove(tr, node.getPartitionSubpath()) + } + + if e := dl.removeRecursive(tr, node.subspace); e != nil { + return false, e + } + dl.removeFromParent(tr, path) + + return true, nil + }) + if e != nil { + return false, e + } + return r.(bool), nil +} + +func (dl directoryLayer) removeRecursive(tr fdb.Transaction, node subspace.Subspace) error { + nodes := dl.subdirNodes(tr, node) + for i := range nodes { + if e := dl.removeRecursive(tr, nodes[i]); e != nil { + return e + } + } + + p, e := dl.nodeSS.Unpack(node) + if e != nil { return e } + kr, e := fdb.PrefixRange(p[0].([]byte)) + if e != nil { return e } + + tr.ClearRange(kr) + tr.ClearRange(node) + + return nil +} + +func (dl directoryLayer) removeFromParent(tr fdb.Transaction, path []string) { + parent := dl.find(tr, path[:len(path)-1]) + tr.Clear(parent.subspace.Sub(_SUBDIRS, path[len(path)-1])) +} + +func (dl directoryLayer) GetLayer() []byte { + return []byte{} +} + +func (dl directoryLayer) GetPath() []string { + return dl.path +} + +func (dl directoryLayer) subdirNames(rtr fdb.ReadTransaction, node subspace.Subspace) ([]string, error) { + sd := node.Sub(_SUBDIRS) + + rr := rtr.GetRange(sd, fdb.RangeOptions{}) + ri := rr.Iterator() + + var ret []string + + for ri.Advance() { + kv := ri.MustGet() + + p, e := sd.Unpack(kv.Key) + if e != nil { + return nil, e + } + + ret = append(ret, p[0].(string)) + } + + return ret, nil +} + +func (dl directoryLayer) subdirNodes(tr fdb.Transaction, node subspace.Subspace) []subspace.Subspace { + sd := node.Sub(_SUBDIRS) + + rr := tr.GetRange(sd, fdb.RangeOptions{}) + ri := rr.Iterator() + + var ret []subspace.Subspace + + for ri.Advance() { + kv := ri.MustGet() + + ret = append(ret, dl.nodeWithPrefix(kv.Value)) + } + + return ret +} + +func (dl directoryLayer) nodeContainingKey(rtr fdb.ReadTransaction, key []byte) (subspace.Subspace, error) { + if bytes.HasPrefix(key, dl.nodeSS.Bytes()) { + return dl.rootNode, nil + } + + bk, _ := dl.nodeSS.FDBRangeKeys() + kr := fdb.KeyRange{bk, fdb.Key(append(dl.nodeSS.Pack(tuple.Tuple{key}), 0x00))} + + kvs := rtr.GetRange(kr, fdb.RangeOptions{Reverse:true, Limit:1}).GetSliceOrPanic() + if len(kvs) == 1 { + pp, e := dl.nodeSS.Unpack(kvs[0].Key) + if e != nil { + return nil, e + } + prevPrefix := pp[0].([]byte) + if bytes.HasPrefix(key, prevPrefix) { + return dl.nodeWithPrefix(prevPrefix), nil + } + } + + return nil, nil +} + +func (dl directoryLayer) isPrefixFree(rtr fdb.ReadTransaction, prefix []byte) (bool, error) { + if len(prefix) == 0 { + return false, nil + } + + nck, e := dl.nodeContainingKey(rtr, prefix) + if e != nil { + return false, e + } + if nck != nil { + return false, nil + } + + kr, e := fdb.PrefixRange(prefix) + if e != nil { + return false, e + } + + bk, ek := kr.FDBRangeKeys() + if !isRangeEmpty(rtr, fdb.KeyRange{dl.nodeSS.Pack(tuple.Tuple{bk}), dl.nodeSS.Pack(tuple.Tuple{ek})}) { + return false, nil + } + + return true, nil +} + +func (dl directoryLayer) checkVersion(rtr fdb.ReadTransaction, tr *fdb.Transaction) error { + version := rtr.Get(dl.rootNode.Sub([]byte("version"))).MustGet() + + if version == nil { + if tr != nil { + dl.initializeDirectory(*tr) + } + return nil + } + + var versions []int32 + buf := bytes.NewBuffer(version) + + for i := 0; i < 3; i++ { + var v int32 + err := binary.Read(buf, binary.LittleEndian, &v) + if err != nil { + return errors.New("cannot determine directory version present in database") + } + versions = append(versions, v) + } + + if versions[0] > _MAJORVERSION { + return fmt.Errorf("cannot load directory with version %d.%d.%d using directory layer %d.%d.%d", versions[0], versions[1], versions[2], _MAJORVERSION, _MINORVERSION, _MICROVERSION) + } + + if versions[1] > _MINORVERSION && tr != nil /* aka write access allowed */ { + return fmt.Errorf("directory with version %d.%d.%d is read-only when opened using directory layer %d.%d.%d", versions[0], versions[1], versions[2], _MAJORVERSION, _MINORVERSION, _MICROVERSION) + } + + return nil +} + +func (dl directoryLayer) initializeDirectory(tr fdb.Transaction) { + buf := new(bytes.Buffer) + + // bytes.Buffer claims that Write will always return a nil error, which + // means the error return here can only be an encoding issue. So long as we + // don't set our own versions to something completely invalid, we should be + // OK to ignore error returns. + binary.Write(buf, binary.LittleEndian, _MAJORVERSION) + binary.Write(buf, binary.LittleEndian, _MINORVERSION) + binary.Write(buf, binary.LittleEndian, _MICROVERSION) + + tr.Set(dl.rootNode.Sub([]byte("version")), buf.Bytes()) +} + +func (dl directoryLayer) contentsOfNode(node subspace.Subspace, path []string, layer []byte) (DirectorySubspace, error) { + p, e := dl.nodeSS.Unpack(node) + if e != nil { + return nil, e + } + prefix := p[0] + + newPath := make([]string, len(dl.path) + len(path)) + copy(newPath, dl.path) + copy(newPath[len(dl.path):], path) + + pb := prefix.([]byte) + ss := subspace.FromBytes(pb) + + if bytes.Compare(layer, []byte("partition")) == 0 { + nssb := make([]byte, len(pb) + 1) + copy(nssb, pb) + nssb[len(pb)] = 0xFE + ndl := NewDirectoryLayer(subspace.FromBytes(nssb), ss, false).(directoryLayer) + ndl.path = newPath + return directoryPartition{ndl, dl}, nil + } else { + return directorySubspace{ss, dl, newPath, layer}, nil + } +} + +func (dl directoryLayer) nodeWithPrefix(prefix []byte) subspace.Subspace { + if prefix == nil { return nil } + return dl.nodeSS.Sub(prefix) +} + +func (dl directoryLayer) find(rtr fdb.ReadTransaction, path []string) *node { + n := &node{dl.rootNode, []string{}, path, nil} + for i := range path { + n = &node{dl.nodeWithPrefix(rtr.Get(n.subspace.Sub(_SUBDIRS, path[i])).MustGet()), path[:i+1], path, nil} + if !n.exists() || bytes.Compare(n.layer(rtr).MustGet(), []byte("partition")) == 0 { + return n + } + } + return n +} + +func (dl directoryLayer) partitionSubpath(lpath, rpath []string) []string { + r := make([]string, len(lpath) - len(dl.path) + len(rpath)) + copy(r, lpath[len(dl.path):]) + copy(r[len(lpath) - len(dl.path):], rpath) + return r +} + +func isRangeEmpty(rtr fdb.ReadTransaction, r fdb.Range) bool { + kvs := rtr.GetRange(r, fdb.RangeOptions{Limit: 1}).GetSliceOrPanic() + + return len(kvs) == 0 +} diff --git a/bindings/go/src/fdb/directory/directoryPartition.go b/bindings/go/src/fdb/directory/directoryPartition.go new file mode 100644 index 0000000000..253d9482db --- /dev/null +++ b/bindings/go/src/fdb/directory/directoryPartition.go @@ -0,0 +1,92 @@ +/* + * directoryPartition.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go Directory Layer + +package directory + +import ( + "fdb" + "fdb/subspace" + "fdb/tuple" +) + +type directoryPartition struct { + directoryLayer + parentDirectoryLayer directoryLayer +} + +func (dp directoryPartition) Sub(el ...tuple.TupleElement) subspace.Subspace { + panic("cannot open subspace in the root of a directory partition") +} + +func (dp directoryPartition) Bytes() []byte { + panic("cannot get key for the root of a directory partition") +} + +func (dp directoryPartition) Pack(t tuple.Tuple) fdb.Key { + panic("cannot pack keys using the root of a directory partition") +} + +func (dp directoryPartition) Unpack(k fdb.KeyConvertible) (tuple.Tuple, error) { + panic("cannot unpack keys using the root of a directory partition") +} + +func (dp directoryPartition) Contains(k fdb.KeyConvertible) bool { + panic("cannot check whether a key belongs to the root of a directory partition") +} + +func (dp directoryPartition) FDBKey() fdb.Key { + panic("cannot use the root of a directory partition as a key") +} + +func (dp directoryPartition) FDBRangeKeys() (fdb.KeyConvertible, fdb.KeyConvertible) { + panic("cannot get range for the root of a directory partition") +} + +func (dp directoryPartition) FDBRangeKeySelectors() (fdb.Selectable, fdb.Selectable) { + panic("cannot get range for the root of a directory partition") +} + +func (dp directoryPartition) GetLayer() []byte { + return []byte("partition") +} + +func (dp directoryPartition) getLayerForPath(path []string) directoryLayer { + if len(path) == 0 { + return dp.parentDirectoryLayer + } else { + return dp.directoryLayer + } +} + +func (dp directoryPartition) MoveTo(t fdb.Transactor, newAbsolutePath []string) (DirectorySubspace, error) { + return moveTo(t, dp.parentDirectoryLayer, dp.path, newAbsolutePath) +} + +func (dp directoryPartition) Remove(t fdb.Transactor, path []string) (bool, error) { + dl := dp.getLayerForPath(path) + return dl.Remove(t, dl.partitionSubpath(dp.path, path)) +} + +func (dp directoryPartition) Exists(rt fdb.ReadTransactor, path []string) (bool, error) { + dl := dp.getLayerForPath(path) + return dl.Exists(rt, dl.partitionSubpath(dp.path, path)) +} diff --git a/bindings/go/src/fdb/directory/directorySubspace.go b/bindings/go/src/fdb/directory/directorySubspace.go new file mode 100644 index 0000000000..70641f4422 --- /dev/null +++ b/bindings/go/src/fdb/directory/directorySubspace.go @@ -0,0 +1,88 @@ +/* + * directorySubspace.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go Directory Layer + +package directory + +import ( + "fdb" + "fdb/subspace" +) + +// DirectorySubspace represents a Directory that may also be used as a Subspace +// to store key/value pairs. Subdirectories of a root directory (as returned by +// Root or NewDirectoryLayer) are DirectorySubspaces, and provide all methods of +// the Directory and subspace.Subspace interfaces. +type DirectorySubspace interface { + subspace.Subspace + Directory +} + +type directorySubspace struct { + subspace.Subspace + dl directoryLayer + path []string + layer []byte +} + +func (d directorySubspace) CreateOrOpen(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) { + return d.dl.CreateOrOpen(t, d.dl.partitionSubpath(d.path, path), layer) +} + +func (d directorySubspace) Create(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) { + return d.dl.Create(t, d.dl.partitionSubpath(d.path, path), layer) +} + +func (d directorySubspace) CreatePrefix(t fdb.Transactor, path []string, layer []byte, prefix []byte) (DirectorySubspace, error) { + return d.dl.CreatePrefix(t, d.dl.partitionSubpath(d.path, path), layer, prefix) +} + +func (d directorySubspace) Open(rt fdb.ReadTransactor, path []string, layer []byte) (DirectorySubspace, error) { + return d.dl.Open(rt, d.dl.partitionSubpath(d.path, path), layer) +} + +func (d directorySubspace) MoveTo(t fdb.Transactor, newAbsolutePath []string) (DirectorySubspace, error) { + return moveTo(t, d.dl, d.path, newAbsolutePath) +} + +func (d directorySubspace) Move(t fdb.Transactor, oldPath []string, newPath []string) (DirectorySubspace, error) { + return d.dl.Move(t, d.dl.partitionSubpath(d.path, oldPath), d.dl.partitionSubpath(d.path, newPath)) +} + +func (d directorySubspace) Remove(t fdb.Transactor, path []string) (bool, error) { + return d.dl.Remove(t, d.dl.partitionSubpath(d.path, path)) +} + +func (d directorySubspace) Exists(rt fdb.ReadTransactor, path []string) (bool, error) { + return d.dl.Exists(rt, d.dl.partitionSubpath(d.path, path)) +} + +func (d directorySubspace) List(rt fdb.ReadTransactor, path []string) (subdirs []string, e error) { + return d.dl.List(rt, d.dl.partitionSubpath(d.path, path)) +} + +func (d directorySubspace) GetLayer() []byte { + return d.layer +} + +func (d directorySubspace) GetPath() []string { + return d.path +} diff --git a/bindings/go/src/fdb/directory/node.go b/bindings/go/src/fdb/directory/node.go new file mode 100644 index 0000000000..a608abafcc --- /dev/null +++ b/bindings/go/src/fdb/directory/node.go @@ -0,0 +1,71 @@ +/* + * node.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go Directory Layer + +package directory + +import ( + "fdb" + "fdb/subspace" + "bytes" +) + +type node struct { + subspace subspace.Subspace + path []string + targetPath []string + _layer fdb.FutureByteSlice +} + +func (n *node) exists() bool { + if n.subspace == nil { + return false + } + return true +} + +func (n *node) prefetchMetadata(rtr fdb.ReadTransaction) *node { + if n.exists() { + n.layer(rtr) + } + return n +} + +func (n *node) layer(rtr fdb.ReadTransaction) fdb.FutureByteSlice { + if n._layer == nil { + fv := rtr.Get(n.subspace.Sub([]byte("layer"))) + n._layer = fv + } + + return n._layer +} + +func (n *node) isInPartition(tr *fdb.Transaction, includeEmptySubpath bool) bool { + return n.exists() && bytes.Compare(n._layer.MustGet(), []byte("partition")) == 0 && (includeEmptySubpath || len(n.targetPath) > len(n.path)) +} + +func (n *node) getPartitionSubpath() []string { + return n.targetPath[len(n.path):] +} + +func (n *node) getContents(dl directoryLayer, tr *fdb.Transaction) (DirectorySubspace, error) { + return dl.contentsOfNode(n.subspace, n.path, n._layer.MustGet()) +} diff --git a/bindings/go/src/fdb/doc.go b/bindings/go/src/fdb/doc.go new file mode 100644 index 0000000000..aee3e85cfe --- /dev/null +++ b/bindings/go/src/fdb/doc.go @@ -0,0 +1,209 @@ +/* + * doc.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +/* +Package fdb provides an interface to FoundationDB databases (version 2.0 or higher). + +To build and run programs using this package, you must have an installed copy of +the FoundationDB client libraries (version 2.0.0 or later), available for Linux, +Windows and OS X at https://files.foundationdb.org/fdb-c/. + +This documentation specifically applies to the FoundationDB Go binding. For more +extensive guidance to programming with FoundationDB, as well as API +documentation for the other FoundationDB interfaces, please see +https://foundationdb.org/documentation/index.html. + +Basic Usage + +A basic interaction with the FoundationDB API is demonstrated below: + + package main + + import ( + "github.com/apple/foundationdb/bindings/go/fdb" + "log" + "fmt" + ) + + func main() { + // Different API versions may expose different runtime behaviors. + fdb.MustAPIVersion(200) + + // Open the default database from the system cluster + db := fdb.MustOpenDefault() + + // Database reads and writes happen inside transactions + ret, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + tr.Set(fdb.Key("hello"), []byte("world")) + return tr.Get(fdb.Key("foo")).MustGet(), nil + // db.Transact automatically commits (and if necessary, + // retries) the transaction + }) + if e != nil { + log.Fatalf("Unable to perform FDB transaction (%v)", e) + } + + fmt.Printf("hello is now world, foo was: %s\n", string(ret.([]byte))) + } + +Futures + +Many functions in this package are asynchronous and return Future objects. A +Future represents a value (or error) to be available at some later +time. Functions documented as blocking on a Future will block the calling +goroutine until the Future is ready (although if the Future is already ready, +the call will not block at all). While a goroutine is blocked on a Future, other +goroutines are free to execute and interact with the FoundationDB API. + +It is possible (and often recommended) to call several asynchronous operations +and have multiple Future objects outstanding inside a single goroutine. All +operations will execute in parallel, and the calling goroutine will not block +until a blocking method on any one of the Futures is called. + +On Panics + +Idiomatic Go code strongly frowns at panics that escape library/package +boundaries, in favor of explicitly returned errors. Idiomatic FoundationDB +client programs, however, are built around the idea of retryable +programmer-provided transactional functions. Retryable transactions can be +implemented using only error values: + + ret, e := db.Transact(func (tr Transaction) (interface{}, error) { + // FoundationDB futures represent a value that will become available + futureValueOne := tr.Get(fdb.Key("foo")) + futureValueTwo := tr.Get(fdb.Key("bar")) + + // Both reads are being carried out in parallel + + // Get the first value (or any error) + valueOne, e := futureValueOne.Get() + if e != nil { + return nil, e + } + + // Get the second value (or any error) + valueTwo, e := futureValueTwo.Get() + if e != nil { + return nil, e + } + + // Return the two values + return []string{valueOne, valueTwo}, nil + }) + +If either read encounters an error, it will be returned to Transact, which will +determine if the error is retryable or not (using (Transaction).OnError). If the +error is an FDB Error and retryable (such as a conflict with with another +transaction), then the programmer-provided function will be run again. If the +error is fatal (or not an FDB Error), then the error will be returned to the +caller of Transact. + +In practice, checking for an error from every asynchronous future type in the +FoundationDB API quickly becomes frustrating. As a convenience, every Future +type also has a MustGet method, which returns the same type and value as Get, +but exposes FoundationDB Errors via a panic rather than an explicitly returned +error. The above example may be rewritten as: + + ret, e := db.Transact(func (tr Transaction) (interface{}, error) { + // FoundationDB futures represent a value that will become available + futureValueOne := tr.Get(fdb.Key("foo")) + futureValueTwo := tr.Get(fdb.Key("bar")) + + // Both reads are being carried out in parallel + + // Get the first value + valueOne := futureValueOne.MustGet() + // Get the second value + valueTwo := futureValueTwo.MustGet() + + // Return the two values + return []string{valueOne, valueTwo}, nil + }) + +Any panic that occurs during execution of the caller-provided function will be +recovered by the (Database).Transact method. If the error is an FDB Error, it +will either result in a retry of the function or be returned by Transact. If the +error is any other type (panics from code other than MustGet), Transact will +re-panic the original value. + +Note that (Transaction).Transact also recovers panics, but does not itself +retry. If the recovered value is an FDB Error, it will be returned to the caller +of (Transaction).Transact; all other values will be re-panicked. + +Transactions and Goroutines + +When using a Transactor in the fdb package, particular care must be taken if +goroutines are created inside of the function passed to the Transact method. Any +panic from the goroutine will not be recovered by Transact, and (unless +otherwise recovered) will result in the termination of that goroutine. + +Furthermore, any errors returned or panicked by fdb methods called in the +goroutine must be safely returned to the function passed to Transact, and either +returned or panicked, to allow Transact to appropriately retry or terminate the +transactional function. + +Lastly, a transactional function may be retried indefinitely. It is advisable to +make sure any goroutines created during the transactional function have +completed before returning from the transactional function, or a potentially +unbounded number of goroutines may be created. + +Given these complexities, it is generally best practice to use a single +goroutine for each logical thread of interaction with FoundationDB, and allow +each goroutine to block when necessary to wait for Futures to become ready. + +Streaming Modes + +When using GetRange methods in the FoundationDB API, clients can request large +ranges of the database to iterate over. Making such a request doesn't +necessarily mean that the client will consume all of the data in the range -- +sometimes the client doesn't know how far it intends to iterate in +advance. FoundationDB tries to balance latency and bandwidth by requesting data +for iteration in batches. + +The Mode field of the RangeOptions struct allows a client to customize this +performance tradeoff by providing extra information about how the iterator will +be used. + +The default value of Mode is StreamingModeIterator, which tries to provide a +reasonable default balance. Other streaming modes that prioritize throughput or +latency are available -- see the documented StreamingMode values for specific +options. + +Atomic Operations + +The FDB package provides a number of atomic operations on the Database and +Transaction objects. An atomic operation is a single database command that +carries out several logical steps: reading the value of a key, performing a +transformation on that value, and writing the result. Different atomic +operations perform different transformations. Like other database operations, an +atomic operation is used within a transaction. + +For more information on atomic operations in FoundationDB, please see +https://foundationdb.org/documentation/developer-guide.html#atomic-operations. The +operands to atomic operations in this API must be provided as appropriately +encoded byte slices. To convert a Go type to a byte slice, see the binary +package. + +The current atomic operations in this API are Add, BitAnd, BitOr, BitXor, Max, Min, +SetVersionstampedKey, SetVersionstampedValue (all methods on Transaction). +*/ +package fdb diff --git a/bindings/go/src/fdb/errors.go b/bindings/go/src/fdb/errors.go new file mode 100644 index 0000000000..3e31256597 --- /dev/null +++ b/bindings/go/src/fdb/errors.go @@ -0,0 +1,59 @@ +/* + * errors.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +package fdb + +/* + #define FDB_API_VERSION 200 + #include +*/ +import "C" + +import ( + "fmt" +) + +// Error represents a low-level error returned by the FoundationDB C library. An +// Error may be returned by any FoundationDB API function that returns error, or +// as a panic from any FoundationDB API function whose name ends with OrPanic. +// +// You may compare the Code field of an Error against the list of FoundationDB +// error codes at https://foundationdb.org/documentation/api-error-codes.html, +// but generally an Error should be passed to (Transaction).OnError. When using +// (Database).Transact, non-fatal errors will be retried automatically. +type Error struct { + Code int +} + +func (e Error) Error() string { + return fmt.Sprintf("FoundationDB error code %d (%s)", e.Code, C.GoString(C.fdb_get_error(C.fdb_error_t(e.Code)))) +} + +// SOMEDAY: these (along with others) should be coming from fdb.options? + +var ( + errNetworkNotSetup = Error{2008} + + errAPIVersionUnset = Error{2200} + errAPIVersionAlreadySet = Error{2201} + errAPIVersionNotSupported = Error{2203} +) diff --git a/bindings/go/src/fdb/fdb.go b/bindings/go/src/fdb/fdb.go new file mode 100644 index 0000000000..0fd4407160 --- /dev/null +++ b/bindings/go/src/fdb/fdb.go @@ -0,0 +1,358 @@ +/* + * fdb.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +package fdb + +/* + #define FDB_API_VERSION 500 + #include + #include +*/ +import "C" + +import ( + "runtime" + "sync" + "unsafe" + "fmt" + "log" +) + +/* Would put this in futures.go but for the documented issue with +/* exports and functions in preamble +/* (https://code.google.com/p/go-wiki/wiki/cgo#Global_functions) */ +//export unlockMutex +func unlockMutex(p unsafe.Pointer) { + m := (*sync.Mutex)(p) + m.Unlock() +} + +// A Transactor can execute a function that requires a Transaction. Functions +// written to accept a Transactor are called transactional functions, and may be +// called with either a Database or a Transaction. +type Transactor interface { + // Transact executes the caller-provided function, providing it with a + // Transaction (itself a Transactor, allowing composition of transactional + // functions). + Transact(func (Transaction) (interface{}, error)) (interface{}, error) + + // All Transactors are also ReadTransactors, allowing them to be used with + // read-only transactional functions. + ReadTransactor +} + +// A ReadTransactor can execute a function that requires a +// ReadTransaction. Functions written to accept a ReadTransactor are called +// read-only transactional functions, and may be called with a Database, +// Transaction or Snapshot. +type ReadTransactor interface { + // ReadTransact executes the caller-provided function, providing it with a + // ReadTransaction (itself a ReadTransactor, allowing composition of + // read-only transactional functions). + ReadTransact(func (ReadTransaction) (interface{}, error)) (interface{}, error) +} + +func setOpt(setter func(*C.uint8_t, C.int) C.fdb_error_t, param []byte) error { + if err := setter(byteSliceToPtr(param), C.int(len(param))); err != 0 { + return Error{int(err)} + } + + return nil +} + +// NetworkOptions is a handle with which to set options that affect the entire +// FoundationDB client. A NetworkOptions instance should be obtained with the +// fdb.Options function. +type NetworkOptions struct { +} + +// Options returns a NetworkOptions instance suitable for setting options that +// affect the entire FoundationDB client. +func Options() NetworkOptions { + return NetworkOptions{} +} + +func (opt NetworkOptions) setOpt(code int, param []byte) error { + networkMutex.Lock() + defer networkMutex.Unlock() + + if apiVersion == 0 { + return errAPIVersionUnset + } + + return setOpt(func(p *C.uint8_t, pl C.int) C.fdb_error_t { + return C.fdb_network_set_option(C.FDBNetworkOption(code), p, pl) + }, param) +} + +// APIVersion determines the runtime behavior the fdb package. If the requested +// version is not supported by both the fdb package and the FoundationDB C +// library, an error will be returned. APIVersion must be called prior to any +// other functions in the fdb package. +// +// Currently, this package supports API versions 200 through 500. +// +// Warning: When using the multi-version client API, setting an API version that +// is not supported by a particular client library will prevent that client from +// being used to connect to the cluster. In particular, you should not advance +// the API version of your application after upgrading your client until the +// cluster has also been upgraded. +func APIVersion(version int) error { + headerVersion := 500 + + networkMutex.Lock() + defer networkMutex.Unlock() + + if apiVersion != 0 { + if apiVersion == version { + return nil + } + return errAPIVersionAlreadySet + } + + if version < 200 || version > 500 { + return errAPIVersionNotSupported + } + + if e := C.fdb_select_api_version_impl(C.int(version), C.int(headerVersion)); e != 0 { + if e != 0 { + if e == 2203 { + maxSupportedVersion := C.fdb_get_max_api_version() + if headerVersion > int(maxSupportedVersion) { + return fmt.Errorf("This version of the FoundationDB Go binding is not supported by the installed FoundationDB C library. The binding requires a library that supports API version %d, but the installed library supports a maximum version of %d.", version, maxSupportedVersion) + } else { + return fmt.Errorf("API version %d is not supported by the installed FoundationDB C library.", version) + } + } + return Error{int(e)} + } + } + + apiVersion = version + + return nil +} + +// MustAPIVersion is like APIVersion but panics if the API version is not +// supported. +func MustAPIVersion(version int) { + err := APIVersion(version) + if err != nil { + panic(err) + } +} + +var apiVersion int +var networkStarted bool +var networkMutex sync.Mutex + +var openClusters map[string]Cluster +var openDatabases map[string]Database + +func init() { + openClusters = make(map[string]Cluster) + openDatabases = make(map[string]Database) +} + +func startNetwork() error { + if e := C.fdb_setup_network(); e != 0 { + return Error{int(e)} + } + + go func() { + e := C.fdb_run_network() + if e != 0 { + log.Printf("Unhandled error in FoundationDB network thread: %v (%v)\n", C.GoString(C.fdb_get_error(e)), e) + } + }() + + networkStarted = true + + return nil +} + +// StartNetwork initializes the FoundationDB client networking engine. It is not +// necessary to call StartNetwork when using the fdb.Open or fdb.OpenDefault +// functions to obtain a database handle. StartNetwork must not be called more +// than once. +func StartNetwork() error { + networkMutex.Lock() + defer networkMutex.Unlock() + + if apiVersion == 0 { + return errAPIVersionUnset + } + + return startNetwork() +} + +// DefaultClusterFile should be passed to fdb.Open or fdb.CreateCluster to allow +// the FoundationDB C library to select the platform-appropriate default cluster +// file on the current machine. +const DefaultClusterFile string = "" + +// OpenDefault returns a database handle to the default database from the +// FoundationDB cluster identified by the DefaultClusterFile on the current +// machine. The FoundationDB client networking engine will be initialized first, +// if necessary. +func OpenDefault() (Database, error) { + return Open(DefaultClusterFile, []byte("DB")) +} + +// MustOpenDefault is like OpenDefault but panics if the default database cannot +// be opened. +func MustOpenDefault() Database { + db, err := OpenDefault() + if err != nil { + panic(err) + } + return db +} + +// Open returns a database handle to the named database from the FoundationDB +// cluster identified by the provided cluster file and database name. The +// FoundationDB client networking engine will be initialized first, if +// necessary. +// +// In the current release, the database name must be []byte("DB"). +func Open(clusterFile string, dbName []byte) (Database, error) { + networkMutex.Lock() + defer networkMutex.Unlock() + + if apiVersion == 0 { + return Database{}, errAPIVersionUnset + } + + var e error + + if !networkStarted { + e = startNetwork() + if e != nil { + return Database{}, e + } + } + + cluster, ok := openClusters[clusterFile] + if !ok { + cluster, e = createCluster(clusterFile) + if e != nil { + return Database{}, e + } + openClusters[clusterFile] = cluster + } + + db, ok := openDatabases[string(dbName)] + if !ok { + db, e = cluster.OpenDatabase(dbName) + if e != nil { + return Database{}, e + } + openDatabases[string(dbName)] = db + } + + return db, nil +} + +// MustOpen is like Open but panics if the database cannot be opened. +func MustOpen(clusterFile string, dbName []byte) Database { + db, err := Open(clusterFile, dbName) + if err != nil { + panic(err) + } + return db +} + +func createCluster(clusterFile string) (Cluster, error) { + var cf *C.char + + if len(clusterFile) != 0 { + cf = C.CString(clusterFile) + defer C.free(unsafe.Pointer(cf)) + } + + f := C.fdb_create_cluster(cf) + fdb_future_block_until_ready(f) + + var outc *C.FDBCluster + + if err := C.fdb_future_get_cluster(f, &outc); err != 0 { + return Cluster{}, Error{int(err)} + } + + C.fdb_future_destroy(f) + + c := &cluster{outc} + runtime.SetFinalizer(c, (*cluster).destroy) + + return Cluster{c}, nil +} + +// CreateCluster returns a cluster handle to the FoundationDB cluster identified +// by the provided cluster file. +func CreateCluster(clusterFile string) (Cluster, error) { + networkMutex.Lock() + defer networkMutex.Unlock() + + if apiVersion == 0 { + return Cluster{}, errAPIVersionUnset + } + + if !networkStarted { + return Cluster{}, errNetworkNotSetup + } + + return createCluster(clusterFile) +} + +func byteSliceToPtr(b []byte) *C.uint8_t { + if len(b) > 0 { + return (*C.uint8_t)(unsafe.Pointer(&b[0])) + } else { + return nil + } +} + +// A KeyConvertible can be converted to a FoundationDB Key. All functions in the +// FoundationDB API that address a specific key accept a KeyConvertible. +type KeyConvertible interface { + FDBKey() Key +} + +// Key represents a FoundationDB key, a lexicographically-ordered sequence of +// bytes. Key implements the KeyConvertible interface. +type Key []byte + +// FDBKey allows Key to (trivially) satisfy the KeyConvertible interface. +func (k Key) FDBKey() Key { + return k +} + +func panicToError(e *error) { + if r := recover(); r != nil { + fe, ok := r.(Error) + if ok { + *e = fe + } else { + panic(r) + } + } +} diff --git a/bindings/go/src/fdb/fdb_test.go b/bindings/go/src/fdb/fdb_test.go new file mode 100644 index 0000000000..4f07a5c6f9 --- /dev/null +++ b/bindings/go/src/fdb/fdb_test.go @@ -0,0 +1,263 @@ +/* + * fdb_test.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +package fdb_test + +import ( + "fdb" + "fmt" + "testing" +) + +func ExampleOpenDefault() { + var e error + + e = fdb.APIVersion(400) + if e != nil { + fmt.Printf("Unable to set API version: %v\n", e) + return + } + + // OpenDefault opens the database described by the platform-specific default + // cluster file and the database name []byte("DB"). + db, e := fdb.OpenDefault() + if e != nil { + fmt.Printf("Unable to open default database: %v\n", e) + return + } + + _ = db +} + +func ExampleVersionstamp(t *testing.T) { + fdb.MustAPIVersion(400) + db := fdb.MustOpenDefault() + + setVs := func(t fdb.Transactor, key fdb.Key ) (fdb.FutureKey, error) { + fmt.Printf("setOne called with: %T\n", t) + ret, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + tr.SetVersionstampedValue(key, []byte("blahblahbl")) + return tr.GetVersionstamp(), nil + }) + return ret.(fdb.FutureKey), e + } + + getOne := func(rt fdb.ReadTransactor, key fdb.Key) ([]byte, error) { + fmt.Printf("getOne called with: %T\n", rt) + ret, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + return rtr.Get(key).MustGet(), nil + }) + if e != nil { + return nil, e + } + return ret.([]byte), nil + } + + var v []byte + var fvs fdb.FutureKey + var k fdb.Key + + fvs, _ = setVs(db, fdb.Key("foo")) + v, _ = getOne(db, fdb.Key("foo")) + t.Log(v) + k, _ = fvs.Get() + t.Log(k) +} + +func ExampleTransactor() { + fdb.MustAPIVersion(400) + db := fdb.MustOpenDefault() + + setOne := func(t fdb.Transactor, key fdb.Key, value []byte) error { + fmt.Printf("setOne called with: %T\n", t) + _, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + // We don't actually call tr.Set here to avoid mutating a real database. + // tr.Set(key, value) + return nil, nil + }) + return e + } + + setMany := func(t fdb.Transactor, value []byte, keys ...fdb.Key) error { + fmt.Printf("setMany called with: %T\n", t) + _, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + for _, key := range(keys) { + setOne(tr, key, value) + } + return nil, nil + }) + return e + } + + var e error + + fmt.Println("Calling setOne with a database:") + e = setOne(db, []byte("foo"), []byte("bar")) + if e != nil { + fmt.Println(e) + return + } + fmt.Println("\nCalling setMany with a database:") + e = setMany(db, []byte("bar"), fdb.Key("foo1"), fdb.Key("foo2"), fdb.Key("foo3")) + if e != nil { + fmt.Println(e) + return + } + + // Output: + // Calling setOne with a database: + // setOne called with: fdb.Database + // + // Calling setMany with a database: + // setMany called with: fdb.Database + // setOne called with: fdb.Transaction + // setOne called with: fdb.Transaction + // setOne called with: fdb.Transaction +} + +func ExampleReadTransactor() { + fdb.MustAPIVersion(400) + db := fdb.MustOpenDefault() + + getOne := func(rt fdb.ReadTransactor, key fdb.Key) ([]byte, error) { + fmt.Printf("getOne called with: %T\n", rt) + ret, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + return rtr.Get(key).MustGet(), nil + }) + if e != nil { + return nil, e + } + return ret.([]byte), nil + } + + getTwo := func(rt fdb.ReadTransactor, key1, key2 fdb.Key) ([][]byte, error) { + fmt.Printf("getTwo called with: %T\n", rt) + ret, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + r1, _ := getOne(rtr, key1) + r2, _ := getOne(rtr.Snapshot(), key2) + return [][]byte{r1, r2}, nil + }) + if e != nil { + return nil, e + } + return ret.([][]byte), nil + } + + var e error + + fmt.Println("Calling getOne with a database:") + _, e = getOne(db, fdb.Key("foo")) + if e != nil { + fmt.Println(e) + return + } + fmt.Println("\nCalling getTwo with a database:") + _, e = getTwo(db, fdb.Key("foo"), fdb.Key("bar")) + if e != nil { + fmt.Println(e) + return + } + + // Output: + // Calling getOne with a database: + // getOne called with: fdb.Database + // + // Calling getTwo with a database: + // getTwo called with: fdb.Database + // getOne called with: fdb.Transaction + // getOne called with: fdb.Snapshot +} + +func ExamplePrefixRange() { + fdb.MustAPIVersion(400) + db := fdb.MustOpenDefault() + + tr, e := db.CreateTransaction() + if e != nil { + fmt.Printf("Unable to create transaction: %v\n", e) + return + } + + // Clear and initialize data in this transaction. In examples we do not + // commit transactions to avoid mutating a real database. + tr.ClearRange(fdb.KeyRange{fdb.Key(""), fdb.Key{0xFF}}) + tr.Set(fdb.Key("alpha"), []byte("1")) + tr.Set(fdb.Key("alphabetA"), []byte("2")) + tr.Set(fdb.Key("alphabetB"), []byte("3")) + tr.Set(fdb.Key("alphabetize"), []byte("4")) + tr.Set(fdb.Key("beta"), []byte("5")) + + // Construct the range of all keys beginning with "alphabet". It is safe to + // ignore the error return from PrefixRange unless the provided prefix might + // consist entirely of zero or more 0xFF bytes. + pr, _ := fdb.PrefixRange([]byte("alphabet")) + + // Read and process the range + kvs, e := tr.GetRange(pr, fdb.RangeOptions{}).GetSliceWithError() + if e != nil { + fmt.Printf("Unable to read range: %v\n", e) + } + for _, kv := range kvs { + fmt.Printf("%s: %s\n", string(kv.Key), string(kv.Value)) + } + + // Output: + // alphabetA: 2 + // alphabetB: 3 + // alphabetize: 4 +} + +func ExampleRangeIterator() { + fdb.MustAPIVersion(400) + db := fdb.MustOpenDefault() + + tr, e := db.CreateTransaction() + if e != nil { + fmt.Printf("Unable to create transaction: %v\n", e) + return + } + + // Clear and initialize data in this transaction. In examples we do not + // commit transactions to avoid mutating a real database. + tr.ClearRange(fdb.KeyRange{fdb.Key(""), fdb.Key{0xFF}}) + tr.Set(fdb.Key("apple"), []byte("foo")) + tr.Set(fdb.Key("cherry"), []byte("baz")) + tr.Set(fdb.Key("banana"), []byte("bar")) + + rr := tr.GetRange(fdb.KeyRange{fdb.Key(""), fdb.Key{0xFF}}, fdb.RangeOptions{}) + ri := rr.Iterator() + + // Advance will return true until the iterator is exhausted + for ri.Advance() { + kv, e := ri.Get() + if e != nil { + fmt.Printf("Unable to read next value: %v\n", e) + return + } + fmt.Printf("%s is %s\n", kv.Key, kv.Value) + } + + // Output: + // apple is foo + // banana is bar + // cherry is baz +} diff --git a/bindings/go/src/fdb/futures.go b/bindings/go/src/fdb/futures.go new file mode 100644 index 0000000000..25ac0421ef --- /dev/null +++ b/bindings/go/src/fdb/futures.go @@ -0,0 +1,376 @@ +/* + * futures.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +package fdb + +/* + #cgo LDFLAGS: -lfdb_c -lm + #define FDB_API_VERSION 500 + #include + #include + + extern void unlockMutex(void*); + + void go_callback(FDBFuture* f, void* m) { + unlockMutex(m); + } + + void go_set_callback(void* f, void* m) { + fdb_future_set_callback(f, (FDBCallback)&go_callback, m); + } +*/ +import "C" + +import ( + "unsafe" + "sync" + "runtime" +) + +// A Future represents a value (or error) to be available at some later +// time. Asynchronous FDB API functions return one of the types that implement +// the Future interface. All Future types additionally implement Get and MustGet +// methods with different return types. Calling BlockUntilReady, Get or MustGet +// will block the calling goroutine until the Future is ready. +type Future interface { + // BlockUntilReady blocks the calling goroutine until the future is ready. A + // future becomes ready either when it receives a value of its enclosed type + // (if any) or is set to an error state. + BlockUntilReady() + + // IsReady returns true if the future is ready, and false otherwise, without + // blocking. A future is ready either when has received a value of its + // enclosed type (if any) or has been set to an error state. + IsReady() bool + + // Cancel cancels a future and its associated asynchronous operation. If + // called before the future becomes ready, attempts to access the future + // will return an error. Cancel has no effect if the future is already + // ready. + // + // Note that even if a future is not ready, the associated asynchronous + // operation may already have completed and be unable to be cancelled. + Cancel() +} + +type future struct { + ptr *C.FDBFuture +} + +func newFuture(ptr *C.FDBFuture) *future { + f := &future{ptr} + runtime.SetFinalizer(f, func(f *future) { C.fdb_future_destroy(f.ptr) }) + return f +} + +func fdb_future_block_until_ready(f *C.FDBFuture) { + if C.fdb_future_is_ready(f) != 0 { + return + } + + m := &sync.Mutex{} + m.Lock() + C.go_set_callback(unsafe.Pointer(f), unsafe.Pointer(m)) + m.Lock() +} + +func (f future) BlockUntilReady() { + fdb_future_block_until_ready(f.ptr) +} + +func (f future) IsReady() bool { + return C.fdb_future_is_ready(f.ptr) != 0 +} + +func (f future) Cancel() { + C.fdb_future_cancel(f.ptr) +} + +// FutureByteSlice represents the asynchronous result of a function that returns +// a value from a database. FutureByteSlice is a lightweight object that may be +// efficiently copied, and is safe for concurrent use by multiple goroutines. +type FutureByteSlice interface { + // Get returns a database value (or nil if there is no value), or an error + // if the asynchronous operation associated with this future did not + // successfully complete. The current goroutine will be blocked until the + // future is ready. + Get() ([]byte, error) + + // MustGet returns a database value (or nil if there is no value), or panics + // if the asynchronous operation associated with this future did not + // successfully complete. The current goroutine will be blocked until the + // future is ready. + MustGet() []byte + + Future +} + +type futureByteSlice struct { + *future + v []byte + e error + o sync.Once +} + +func (f *futureByteSlice) Get() ([]byte, error) { + f.o.Do(func() { + var present C.fdb_bool_t + var value *C.uint8_t + var length C.int + + f.BlockUntilReady() + + if err := C.fdb_future_get_value(f.ptr, &present, &value, &length); err != 0 { + f.e = Error{int(err)} + } else { + if present != 0 { + f.v = C.GoBytes(unsafe.Pointer(value), length) + } + } + + C.fdb_future_release_memory(f.ptr) + }) + + return f.v, f.e +} + +func (f *futureByteSlice) MustGet() []byte { + val, err := f.Get() + if err != nil { + panic(err) + } + return val +} + +// FutureKey represents the asynchronous result of a function that returns a key +// from a database. FutureKey is a lightweight object that may be efficiently +// copied, and is safe for concurrent use by multiple goroutines. +type FutureKey interface { + // Get returns a database key or an error if the asynchronous operation + // associated with this future did not successfully complete. The current + // goroutine will be blocked until the future is ready. + Get() (Key, error) + + // MustGet returns a database key, or panics if the asynchronous operation + // associated with this future did not successfully complete. The current + // goroutine will be blocked until the future is ready. + MustGet() Key + + Future +} + +type futureKey struct { + *future + k Key + e error + o sync.Once +} + +func (f *futureKey) Get() (Key, error) { + f.o.Do(func() { + var value *C.uint8_t + var length C.int + + f.BlockUntilReady() + + if err := C.fdb_future_get_key(f.ptr, &value, &length); err != 0 { + f.e = Error{int(err)} + } else { + f.k = C.GoBytes(unsafe.Pointer(value), length) + } + + C.fdb_future_release_memory(f.ptr) + }) + + return f.k, f.e +} + +func (f *futureKey) MustGet() Key { + val, err := f.Get() + if err != nil { + panic(err) + } + return val +} + +// FutureNil represents the asynchronous result of a function that has no return +// value. FutureNil is a lightweight object that may be efficiently copied, and +// is safe for concurrent use by multiple goroutines. +type FutureNil interface { + // Get returns an error if the asynchronous operation associated with this + // future did not successfully complete. The current goroutine will be + // blocked until the future is ready. + Get() error + + // MustGet panics if the asynchronous operation associated with this future + // did not successfully complete. The current goroutine will be blocked + // until the future is ready. + MustGet() + + Future +} + +type futureNil struct { + *future +} + +func (f futureNil) Get() error { + f.BlockUntilReady() + if err := C.fdb_future_get_error(f.ptr); err != 0 { + return Error{int(err)} + } + + return nil +} + +func (f futureNil) MustGet() { + if err := f.Get(); err != nil { + panic(err) + } +} + +type futureKeyValueArray struct { + *future +} + +func stringRefToSlice(ptr unsafe.Pointer) []byte { + size := *((*C.int)(unsafe.Pointer(uintptr(ptr)+8))) + + if size == 0 { + return []byte{} + } + + src := unsafe.Pointer(*(**C.uint8_t)(unsafe.Pointer(ptr))) + + return C.GoBytes(src, size) +} + +func (f futureKeyValueArray) Get() ([]KeyValue, bool, error) { + f.BlockUntilReady() + + var kvs *C.FDBKeyValue + var count C.int + var more C.fdb_bool_t + + if err := C.fdb_future_get_keyvalue_array(f.ptr, &kvs, &count, &more); err != 0 { + return nil, false, Error{int(err)} + } + + ret := make([]KeyValue, int(count)) + + for i := 0; i < int(count); i++ { + kvptr := unsafe.Pointer(uintptr(unsafe.Pointer(kvs)) + uintptr(i * 24)) + + ret[i].Key = stringRefToSlice(kvptr) + ret[i].Value = stringRefToSlice(unsafe.Pointer(uintptr(kvptr) + 12)) + } + + return ret, (more != 0), nil +} + +// FutureInt64 represents the asynchronous result of a function that returns a +// database version. FutureInt64 is a lightweight object that may be efficiently +// copied, and is safe for concurrent use by multiple goroutines. +type FutureInt64 interface { + // Get returns a database version or an error if the asynchronous operation + // associated with this future did not successfully complete. The current + // goroutine will be blocked until the future is ready. + Get() (int64, error) + + // MustGet returns a database version, or panics if the asynchronous + // operation associated with this future did not successfully complete. The + // current goroutine will be blocked until the future is ready. + MustGet() int64 + + Future +} + +type futureInt64 struct { + *future +} + +func (f futureInt64) Get() (int64, error) { + f.BlockUntilReady() + + var ver C.int64_t + if err := C.fdb_future_get_version(f.ptr, &ver); err != 0 { + return 0, Error{int(err)} + } + return int64(ver), nil +} + +func (f futureInt64) MustGet() int64 { + val, err := f.Get() + if err != nil { + panic(err) + } + return val +} + +// FutureStringSlice represents the asynchronous result of a function that +// returns a slice of strings. FutureStringSlice is a lightweight object that +// may be efficiently copied, and is safe for concurrent use by multiple +// goroutines. +type FutureStringSlice interface { + // Get returns a slice of strings or an error if the asynchronous operation + // associated with this future did not successfully complete. The current + // goroutine will be blocked until the future is ready. + Get() ([]string, error) + + // MustGet returns a slice of strings or panics if the asynchronous + // operation associated with this future did not successfully complete. The + // current goroutine will be blocked until the future is ready. + MustGet() []string + + Future +} + +type futureStringSlice struct { + *future +} + +func (f futureStringSlice) Get() ([]string, error) { + f.BlockUntilReady() + + var strings **C.char + var count C.int + + if err := C.fdb_future_get_string_array(f.ptr, (***C.char)(unsafe.Pointer(&strings)), &count); err != 0 { + return nil, Error{int(err)} + } + + ret := make([]string, int(count)) + + for i := 0; i < int(count); i++ { + ret[i] = C.GoString((*C.char)(*(**C.char)(unsafe.Pointer(uintptr(unsafe.Pointer(strings))+uintptr(i*8))))) + } + + return ret, nil +} + +func (f futureStringSlice) MustGet() []string { + val, err := f.Get() + if err != nil { + panic(err) + } + return val +} diff --git a/bindings/go/src/fdb/keyselector.go b/bindings/go/src/fdb/keyselector.go new file mode 100644 index 0000000000..654cd1e17e --- /dev/null +++ b/bindings/go/src/fdb/keyselector.go @@ -0,0 +1,74 @@ +/* + * keyselector.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +package fdb + +// A Selectable can be converted to a FoundationDB KeySelector. All functions in +// the FoundationDB API that resolve a key selector to a key accept Selectable. +type Selectable interface { + FDBKeySelector() KeySelector +} + +// KeySelector represents a description of a key in a FoundationDB database. A +// KeySelector may be resolved to a specific key with the GetKey method, or used +// as the endpoints of a SelectorRange to be used with a GetRange function. +// +// The most common key selectors are constructed with the functions documented +// below. For details of how KeySelectors are specified and resolved, see +// https://foundationdb.org/documentation/developer-guide.html#key-selectors. +type KeySelector struct { + Key KeyConvertible + OrEqual bool + Offset int +} + +func (ks KeySelector) FDBKeySelector() KeySelector { + return ks +} + +// LastLessThan returns the KeySelector specifying the lexigraphically greatest +// key present in the database which is lexigraphically strictly less than the +// given key. +func LastLessThan(key KeyConvertible) KeySelector { + return KeySelector{key, false, 0} +} + +// LastLessOrEqual returns the KeySelector specifying the lexigraphically +// greatest key present in the database which is lexigraphically less than or +// equal to the given key. +func LastLessOrEqual(key KeyConvertible) KeySelector { + return KeySelector{key, true, 0} +} + +// FirstGreaterThan returns the KeySelector specifying the lexigraphically least +// key present in the database which is lexigraphically strictly greater than +// the given key. +func FirstGreaterThan(key KeyConvertible) KeySelector { + return KeySelector{key, true, 1} +} + +// FirstGreaterOrEqual returns the KeySelector specifying the lexigraphically +// least key present in the database which is lexigraphically greater than or +// equal to the given key. +func FirstGreaterOrEqual(key KeyConvertible) KeySelector { + return KeySelector{key, false, 1} +} diff --git a/bindings/go/src/fdb/range.go b/bindings/go/src/fdb/range.go new file mode 100644 index 0000000000..db9ecfd5ae --- /dev/null +++ b/bindings/go/src/fdb/range.go @@ -0,0 +1,317 @@ +/* + * range.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +package fdb + +/* + #define FDB_API_VERSION 500 + #include +*/ +import "C" + +import ( + "fmt" +) + +// KeyValue represents a single key-value pair in the database. +type KeyValue struct { + Key Key + Value []byte +} + +// RangeOptions specify how a database range read operation is carried +// out. RangeOptions objects are passed to GetRange methods of Database, +// Transaction and Snapshot. +// +// The zero value of RangeOptions represents the default range read +// configuration (no limit, lexicographic order, to be used as an iterator). +type RangeOptions struct { + // Limit restricts the number of key-value pairs returned as part of a range + // read. A value of 0 indicates no limit. + Limit int + + // Mode sets the streaming mode of the range read, allowing the database to + // balance latency and bandwidth for this read. + Mode StreamingMode + + // Reverse indicates that the read should be performed in lexicographic + // (false) or reverse lexicographic (true) order. When Reverse is true and + // Limit is non-zero, the last Limit key-value pairs in the range are + // returned. + Reverse bool +} + +// A Range describes all keys between a begin (inclusive) and end (exclusive) +// key selector. +type Range interface { + // FDBRangeKeySelectors returns a pair of key selectors that describe the + // beginning and end of a range. + FDBRangeKeySelectors() (begin, end Selectable) +} + +// An ExactRange describes all keys between a begin (inclusive) and end +// (exclusive) key. If you need to specify an ExactRange and you have only a +// Range, you must resolve the selectors returned by +// (Range).FDBRangeKeySelectors to keys using the (Transaction).GetKey method. +// +// Any object that implements ExactRange also implements Range, and may be used +// accordingly. +type ExactRange interface { + // FDBRangeKeys returns a pair of keys that describe the beginning and end + // of a range. + FDBRangeKeys() (begin, end KeyConvertible) + + // An object that implements ExactRange must also implement Range + // (logically, by returning FirstGreaterOrEqual of the keys returned by + // FDBRangeKeys). + Range +} + +// KeyRange is an ExactRange constructed from a pair of KeyConvertibles. Note +// that the default zero-value of KeyRange specifies an empty range before all +// keys in the database. +type KeyRange struct { + Begin, End KeyConvertible +} + +// FDBRangeKeys allows KeyRange to satisfy the ExactRange interface. +func (kr KeyRange) FDBRangeKeys() (KeyConvertible, KeyConvertible) { + return kr.Begin, kr.End +} + +// FDBRangeKeySelectors allows KeyRange to satisfy the Range interface. +func (kr KeyRange) FDBRangeKeySelectors() (Selectable, Selectable) { + return FirstGreaterOrEqual(kr.Begin), FirstGreaterOrEqual(kr.End) +} + +// SelectorRange is a Range constructed directly from a pair of Selectable +// objects. Note that the default zero-value of SelectorRange specifies an empty +// range before all keys in the database. +type SelectorRange struct { + Begin, End Selectable +} + +// FDBRangeKeySelectors allows SelectorRange to satisfy the Range interface. +func (sr SelectorRange) FDBRangeKeySelectors() (Selectable, Selectable) { + return sr.Begin, sr.End +} + +// RangeResult is a handle to the asynchronous result of a range +// read. RangeResult is safe for concurrent use by multiple goroutines. +// +// A RangeResult should not be returned from a transactional function passed to +// the Transact method of a Transactor. +type RangeResult struct { + t *transaction + sr SelectorRange + options RangeOptions + snapshot bool + f *futureKeyValueArray +} + +// GetSliceWithError returns a slice of KeyValue objects satisfying the range +// specified in the read that returned this RangeResult, or an error if any of +// the asynchronous operations associated with this result did not successfully +// complete. The current goroutine will be blocked until all reads have +// completed. +func (rr RangeResult) GetSliceWithError() ([]KeyValue, error) { + var ret []KeyValue + + ri := rr.Iterator() + + if rr.options.Limit != 0 { + ri.options.Mode = StreamingModeExact + } else { + ri.options.Mode = StreamingModeWantAll + } + + for ri.Advance() { + if ri.err != nil { + return nil, ri.err + } + ret = append(ret, ri.kvs...) + ri.index = len(ri.kvs) + ri.fetchNextBatch() + } + + return ret, nil +} + +// GetSliceOrPanic returns a slice of KeyValue objects satisfying the range +// specified in the read that returned this RangeResult, or panics if any of the +// asynchronous operations associated with this result did not successfully +// complete. The current goroutine will be blocked until all reads have +// completed. +func (rr RangeResult) GetSliceOrPanic() []KeyValue { + kvs, e := rr.GetSliceWithError() + if e != nil { + panic(e) + } + return kvs +} + +// Iterator returns a RangeIterator over the key-value pairs satisfying the +// range specified in the read that returned this RangeResult. +func (rr RangeResult) Iterator() *RangeIterator { + return &RangeIterator{ + t: rr.t, + f: rr.f, + sr: rr.sr, + options: rr.options, + iteration: 1, + snapshot: rr.snapshot, + } +} + +// RangeIterator returns the key-value pairs in the database (as KeyValue +// objects) satisfying the range specified in a range read. RangeIterator is +// constructed with the (RangeResult).Iterator method. +// +// You must call Advance and get a true result prior to calling Get or MustGet. +// +// RangeIterator should not be copied or used concurrently from multiple +// goroutines, but multiple RangeIterators may be constructed from a single +// RangeResult and used concurrently. RangeIterator should not be returned from +// a transactional function passed to the Transact method of a Transactor. +type RangeIterator struct { + t *transaction + f *futureKeyValueArray + sr SelectorRange + options RangeOptions + iteration int + done bool + more bool + kvs []KeyValue + index int + err error + snapshot bool +} + +// Advance attempts to advance the iterator to the next key-value pair. Advance +// returns true if there are more key-value pairs satisfying the range, or false +// if the range has been exhausted. You must call this before every call to Get +// or MustGet. +func (ri *RangeIterator) Advance() bool { + if ri.done { + return false + } + + if ri.f == nil { + return true + } + + ri.kvs, ri.more, ri.err = ri.f.Get() + ri.index = 0 + ri.f = nil + + if ri.err != nil || len(ri.kvs) > 0 { + return true + } + + return false +} + +func (ri *RangeIterator) fetchNextBatch() { + if !ri.more || ri.index == ri.options.Limit { + ri.done = true + return + } + + if ri.options.Limit > 0 { + // Not worried about this being zero, checked equality above + ri.options.Limit -= ri.index + } + + if ri.options.Reverse { + ri.sr.End = FirstGreaterOrEqual(ri.kvs[ri.index-1].Key) + } else { + ri.sr.Begin = FirstGreaterThan(ri.kvs[ri.index-1].Key) + } + + ri.iteration += 1 + + f := ri.t.doGetRange(ri.sr, ri.options, ri.snapshot, ri.iteration) + ri.f = &f +} + +// Get returns the next KeyValue in a range read, or an error if one of the +// asynchronous operations associated with this range did not successfully +// complete. The Advance method of this RangeIterator must have returned true +// prior to calling Get. +func (ri *RangeIterator) Get() (kv KeyValue, e error) { + if ri.err != nil { + e = ri.err + return + } + + kv = ri.kvs[ri.index] + + ri.index += 1 + + if ri.index == len(ri.kvs) { + ri.fetchNextBatch() + } + + return +} + +// MustGet returns the next KeyValue in a range read, or panics if one of the +// asynchronous operations associated with this range did not successfully +// complete. The Advance method of this RangeIterator must have returned true +// prior to calling MustGet. +func (ri *RangeIterator) MustGet() KeyValue { + kv, e := ri.Get() + if e != nil { + panic(e) + } + return kv +} + +func Strinc(prefix []byte) ([]byte, error) { + for i := len(prefix) - 1; i >= 0; i-- { + if prefix[i] != 0xFF { + ret := make([]byte, i+1) + copy(ret, prefix[:i+1]) + ret[i] += 1 + return ret, nil + } + } + + return nil, fmt.Errorf("Key must contain at least one byte not equal to 0xFF") +} + +// PrefixRange returns the KeyRange describing the range of keys k such that +// bytes.HasPrefix(k, prefix) is true. PrefixRange returns an error if prefix is +// empty or entirely 0xFF bytes. +// +// Do not use PrefixRange on objects that already implement the Range or +// ExactRange interfaces. The prefix range of the byte representation of these +// objects may not correspond to their logical range. +func PrefixRange(prefix []byte) (KeyRange, error) { + begin := make([]byte, len(prefix)) + copy(begin, prefix) + end, e := Strinc(begin) + if e != nil { + return KeyRange{}, nil + } + return KeyRange{Key(begin), Key(end)}, nil +} diff --git a/bindings/go/src/fdb/snapshot.go b/bindings/go/src/fdb/snapshot.go new file mode 100644 index 0000000000..18d2b85ba0 --- /dev/null +++ b/bindings/go/src/fdb/snapshot.go @@ -0,0 +1,88 @@ +/* + * snapshot.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +package fdb + +// Snapshot is a handle to a FoundationDB transaction snapshot, suitable for +// performing snapshot reads. Snapshot reads offer a more relaxed isolation +// level than FoundationDB's default serializable isolation, reducing +// transaction conflicts but making it harder to reason about concurrency. +// +// For more information on snapshot reads, see +// https://foundationdb.org/documentation/developer-guide.html#snapshot-reads. +type Snapshot struct { + *transaction +} + +// ReadTransact executes the caller-provided function, passing it the Snapshot +// receiver object (as a ReadTransaction). +// +// A panic of type Error during execution of the function will be recovered and +// returned to the caller as an error, but ReadTransact will not retry the +// function. +// +// By satisfying the ReadTransactor interface, Snapshot may be passed to a +// read-only transactional function from another (possibly read-only) +// transactional function, allowing composition. +// +// See the ReadTransactor interface for an example of using ReadTransact with +// Transaction, Snapshot and Database objects. +func (s Snapshot) ReadTransact(f func (ReadTransaction) (interface{}, error)) (r interface{}, e error) { + defer panicToError(&e) + + r, e = f(s) + return +} + +// Snapshot returns the receiver and allows Snapshot to satisfy the +// ReadTransaction interface. +func (s Snapshot) Snapshot() Snapshot { + return s +} + +// Get is equivalent to (Transaction).Get, performed as a snapshot read. +func (s Snapshot) Get(key KeyConvertible) FutureByteSlice { + return s.get(key.FDBKey(), 1) +} + +// GetKey is equivalent to (Transaction).GetKey, performed as a snapshot read. +func (s Snapshot) GetKey(sel Selectable) FutureKey { + return s.getKey(sel.FDBKeySelector(), 1) +} + +// GetRange is equivalent to (Transaction).GetRange, performed as a snapshot +// read. +func (s Snapshot) GetRange(r Range, options RangeOptions) RangeResult { + return s.getRange(r, options, true) +} + +// GetReadVersion is equivalent to (Transaction).GetReadVersion, performed as +// a snapshot read. +func (s Snapshot) GetReadVersion() FutureInt64 { + return s.getReadVersion() +} + +// GetDatabase returns a handle to the database with which this snapshot is +// interacting. +func (s Snapshot) GetDatabase() Database { + return s.transaction.db +} diff --git a/bindings/go/src/fdb/subspace/subspace.go b/bindings/go/src/fdb/subspace/subspace.go new file mode 100644 index 0000000000..3e48842a1f --- /dev/null +++ b/bindings/go/src/fdb/subspace/subspace.go @@ -0,0 +1,141 @@ +/* + * subspace.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go Subspace Layer + +// Package subspace provides a convenient way to use FoundationDB tuples to +// define namespaces for different categories of data. The namespace is +// specified by a prefix tuple which is prepended to all tuples packed by the +// subspace. When unpacking a key with the subspace, the prefix tuple will be +// removed from the result. +// +// As a best practice, API clients should use at least one subspace for +// application data. For general guidance on subspace usage, see the Subspaces +// section of the Developer Guide +// (https://foundationdb.org/documentation/developer-guide.html#developer-guide-sub-keyspaces). +package subspace + +import ( + "fdb" + "fdb/tuple" + "bytes" + "errors" +) + +// Subspace represents a well-defined region of keyspace in a FoundationDB +// database. +type Subspace interface { + // Sub returns a new Subspace whose prefix extends this Subspace with the + // encoding of the provided element(s). If any of the elements are not a + // valid tuple.TupleElement, Sub will panic. + Sub(el ...tuple.TupleElement) Subspace + + // Bytes returns the literal bytes of the prefix of this Subspace. + Bytes() []byte + + // Pack returns the key encoding the specified Tuple with the prefix of this + // Subspace prepended. + Pack(t tuple.Tuple) fdb.Key + + // Unpack returns the Tuple encoded by the given key with the prefix of this + // Subspace removed. Unpack will return an error if the key is not in this + // Subspace or does not encode a well-formed Tuple. + Unpack(k fdb.KeyConvertible) (tuple.Tuple, error) + + // Contains returns true if the provided key starts with the prefix of this + // Subspace, indicating that the Subspace logically contains the key. + Contains(k fdb.KeyConvertible) bool + + // All Subspaces implement fdb.KeyConvertible and may be used as + // FoundationDB keys (corresponding to the prefix of this Subspace). + fdb.KeyConvertible + + // All Subspaces implement fdb.ExactRange and fdb.Range, and describe all + // keys logically in this Subspace. + fdb.ExactRange +} + +type subspace struct { + b []byte +} + +// AllKeys returns the Subspace corresponding to all keys in a FoundationDB +// database. +func AllKeys() Subspace { + return subspace{} +} + +// Sub returns a new Subspace whose prefix is the encoding of the provided +// element(s). If any of the elements are not a valid tuple.TupleElement, a +// runtime panic will occur. +func Sub(el ...tuple.TupleElement) Subspace { + return subspace{tuple.Tuple(el).Pack()} +} + +// FromBytes returns a new Subspace from the provided bytes. +func FromBytes(b []byte) Subspace { + s := make([]byte, len(b)) + copy(s, b) + return subspace{s} +} + +func (s subspace) Sub(el ...tuple.TupleElement) Subspace { + return subspace{concat(s.Bytes(), tuple.Tuple(el).Pack()...)} +} + +func (s subspace) Bytes() []byte { + return s.b +} + +func (s subspace) Pack(t tuple.Tuple) fdb.Key { + return fdb.Key(concat(s.b, t.Pack()...)) +} + +func (s subspace) Unpack(k fdb.KeyConvertible) (tuple.Tuple, error) { + key := k.FDBKey() + if !bytes.HasPrefix(key, s.b) { + return nil, errors.New("key is not in subspace") + } + return tuple.Unpack(key[len(s.b):]) +} + +func (s subspace) Contains(k fdb.KeyConvertible) bool { + return bytes.HasPrefix(k.FDBKey(), s.b) +} + +func (s subspace) FDBKey() fdb.Key { + return fdb.Key(s.b) +} + +func (s subspace) FDBRangeKeys() (fdb.KeyConvertible, fdb.KeyConvertible) { + return fdb.Key(concat(s.b, 0x00)), fdb.Key(concat(s.b, 0xFF)) +} + +func (s subspace) FDBRangeKeySelectors() (fdb.Selectable, fdb.Selectable) { + begin, end := s.FDBRangeKeys() + return fdb.FirstGreaterOrEqual(begin), fdb.FirstGreaterOrEqual(end) +} + +func concat(a []byte, b ...byte) []byte { + r := make([]byte, len(a) + len(b)) + copy(r, a) + copy(r[len(a):], b) + return r +} diff --git a/bindings/go/src/fdb/transaction.go b/bindings/go/src/fdb/transaction.go new file mode 100644 index 0000000000..9749a68a03 --- /dev/null +++ b/bindings/go/src/fdb/transaction.go @@ -0,0 +1,459 @@ +/* + * transaction.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go API + +package fdb + +/* + #define FDB_API_VERSION 500 + #include +*/ +import "C" + +// A ReadTransaction can asynchronously read from a FoundationDB +// database. Transaction and Snapshot both satisfy the ReadTransaction +// interface. +// +// All ReadTransactions satisfy the ReadTransactor interface and may be used +// with read-only transactional functions. +type ReadTransaction interface { + Get(key KeyConvertible) FutureByteSlice + GetKey(sel Selectable) FutureKey + GetRange(r Range, options RangeOptions) RangeResult + GetReadVersion() FutureInt64 + GetDatabase() Database + Snapshot() Snapshot + + ReadTransactor +} + +// Transaction is a handle to a FoundationDB transaction. Transaction is a +// lightweight object that may be efficiently copied, and is safe for concurrent +// use by multiple goroutines. +// +// In FoundationDB, a transaction is a mutable snapshot of a database. All read +// and write operations on a transaction see and modify an otherwise-unchanging +// version of the database and only change the underlying database if and when +// the transaction is committed. Read operations do see the effects of previous +// write operations on the same transaction. Committing a transaction usually +// succeeds in the absence of conflicts. +// +// Transactions group operations into a unit with the properties of atomicity, +// isolation, and durability. Transactions also provide the ability to maintain +// an applications invariants or integrity constraints, supporting the property +// of consistency. Together these properties are known as ACID. +// +// Transactions are also causally consistent: once a transaction has been +// successfully committed, all subsequently created transactions will see the +// modifications made by it. +type Transaction struct { + *transaction +} + +type transaction struct { + ptr *C.FDBTransaction + db Database +} + +// TransactionOptions is a handle with which to set options that affect a +// Transaction object. A TransactionOptions instance should be obtained with the +// (Transaction).Options method. +type TransactionOptions struct { + transaction *transaction +} + +func (opt TransactionOptions) setOpt(code int, param []byte) error { + return setOpt(func(p *C.uint8_t, pl C.int) C.fdb_error_t { + return C.fdb_transaction_set_option(opt.transaction.ptr, C.FDBTransactionOption(code), p, pl) + }, param) +} + +func (t *transaction) destroy() { + C.fdb_transaction_destroy(t.ptr) +} + +// GetDatabase returns a handle to the database with which this transaction is +// interacting. +func (t Transaction) GetDatabase() Database { + return t.transaction.db +} + +// Transact executes the caller-provided function, passing it the Transaction +// receiver object. +// +// A panic of type Error during execution of the function will be recovered and +// returned to the caller as an error, but Transact will not retry the function +// or commit the Transaction after the caller-provided function completes. +// +// By satisfying the Transactor interface, Transaction may be passed to a +// transactional function from another transactional function, allowing +// composition. The outermost transactional function must have been provided a +// Database, or else the transaction will never be committed. +// +// See the Transactor interface for an example of using Transact with +// Transaction and Database objects. +func (t Transaction) Transact(f func (Transaction) (interface{}, error)) (r interface{}, e error) { + defer panicToError(&e) + + r, e = f(t) + return +} + +// ReadTransact executes the caller-provided function, passing it the +// Transaction receiver object (as a ReadTransaction). +// +// A panic of type Error during execution of the function will be recovered and +// returned to the caller as an error, but ReadTransact will not retry the +// function. +// +// By satisfying the ReadTransactor interface, Transaction may be passed to a +// read-only transactional function from another (possibly read-only) +// transactional function, allowing composition. +// +// See the ReadTransactor interface for an example of using ReadTransact with +// Transaction, Snapshot and Database objects. +func (t Transaction) ReadTransact(f func(ReadTransaction) (interface{}, error)) (r interface{}, e error) { + defer panicToError(&e) + + r, e = f(t) + return +} + +// Cancel cancels a transaction. All pending or future uses of the transaction +// will encounter an error. The Transaction object may be reused after calling +// (Transaction).Reset. +// +// Be careful if you are using (Transaction).Reset and (Transaction).Cancel +// concurrently with the same transaction. Since they negate each others +// effects, a race condition between these calls will leave the transaction in +// an unknown state. +// +// If your program attempts to cancel a transaction after (Transaction).Commit +// has been called but before it returns, unpredictable behavior will +// result. While it is guaranteed that the transaction will eventually end up in +// a cancelled state, the commit may or may not occur. Moreover, even if the +// call to (Transaction).Commit appears to return a transaction_cancelled +// error, the commit may have occurred or may occur in the future. This can make +// it more difficult to reason about the order in which transactions occur. +func (t Transaction) Cancel() { + C.fdb_transaction_cancel(t.ptr) +} + +// (Infrequently used) SetReadVersion sets the database version that the transaction will read from +// the database. The database cannot guarantee causal consistency if this method +// is used (the transactions reads will be causally consistent only if the +// provided read version has that property). +func (t Transaction) SetReadVersion(version int64) { + C.fdb_transaction_set_read_version(t.ptr, C.int64_t(version)) +} + +// Snapshot returns a Snapshot object, suitable for performing snapshot +// reads. Snapshot reads offer a more relaxed isolation level than +// FoundationDB's default serializable isolation, reducing transaction conflicts +// but making it harder to reason about concurrency. +// +// For more information on snapshot reads, see +// https://foundationdb.org/documentation/developer-guide.html#using-snapshot-reads. +func (t Transaction) Snapshot() Snapshot { + return Snapshot{t.transaction} +} + +// OnError determines whether an error returned by a Transaction method is +// retryable. Waiting on the returned future will return the same error when +// fatal, or return nil (after blocking the calling goroutine for a suitable +// delay) for retryable errors. +// +// Typical code will not use OnError directly. (Database).Transact uses +// OnError internally to implement a correct retry loop. +func (t Transaction) OnError(e Error) FutureNil { + return &futureNil{newFuture(C.fdb_transaction_on_error(t.ptr, C.fdb_error_t(e.Code)))} +} + +// Commit attempts to commit the modifications made in the transaction to the +// database. Waiting on the returned future will block the calling goroutine +// until the transaction has either been committed successfully or an error is +// encountered. Any error should be passed to (Transaction).OnError to determine +// if the error is retryable or not. +// +// As with other client/server databases, in some failure scenarios a client may +// be unable to determine whether a transaction succeeded. For more information, +// see +// https://foundationdb.org/documentation/developer-guide.html#developer-guide-unknown-results. +func (t Transaction) Commit() FutureNil { + return &futureNil{newFuture(C.fdb_transaction_commit(t.ptr))} +} + +// Watch creates a watch and returns a FutureNil that will become ready when the +// watch reports a change to the value of the specified key. +// +// A watchs behavior is relative to the transaction that created it. A watch +// will report a change in relation to the keys value as readable by that +// transaction. The initial value used for comparison is either that of the +// transactions read version or the value as modified by the transaction itself +// prior to the creation of the watch. If the value changes and then changes +// back to its initial value, the watch might not report the change. +// +// Until the transaction that created it has been committed, a watch will not +// report changes made by other transactions. In contrast, a watch will +// immediately report changes made by the transaction itself. Watches cannot be +// created if the transaction has called SetReadYourWritesDisable on the +// Transaction options, and an attempt to do so will return a watches_disabled +// error. +// +// If the transaction used to create a watch encounters an error during commit, +// then the watch will be set with that error. A transaction whose commit +// result is unknown will set all of its watches with the commit_unknown_result +// error. If an uncommitted transaction is reset or destroyed, then any watches +// it created will be set with the transaction_cancelled error. +// +// By default, each database connection can have no more than 10,000 watches +// that have not yet reported a change. When this number is exceeded, an attempt +// to create a watch will return a too_many_watches error. This limit can be +// changed using SetMaxWatches on the Database. Because a watch outlives the +// transaction that creates it, any watch that is no longer needed should be +// cancelled by calling (FutureNil).Cancel on its returned future. +func (t Transaction) Watch(key KeyConvertible) FutureNil { + kb := key.FDBKey() + return &futureNil{newFuture(C.fdb_transaction_watch(t.ptr, byteSliceToPtr(kb), C.int(len(kb))))} +} + +func (t *transaction) get(key []byte, snapshot int) FutureByteSlice { + return &futureByteSlice{future: newFuture(C.fdb_transaction_get(t.ptr, byteSliceToPtr(key), C.int(len(key)), C.fdb_bool_t(snapshot)))} +} + +// Get returns the (future) value associated with the specified key. The read is +// performed asynchronously and does not block the calling goroutine. The future +// will become ready when the read is complete. +func (t Transaction) Get(key KeyConvertible) FutureByteSlice { + return t.get(key.FDBKey(), 0) +} + +func (t *transaction) doGetRange(r Range, options RangeOptions, snapshot bool, iteration int) futureKeyValueArray { + begin, end := r.FDBRangeKeySelectors() + bsel := begin.FDBKeySelector() + esel := end.FDBKeySelector() + bkey := bsel.Key.FDBKey() + ekey := esel.Key.FDBKey() + + return futureKeyValueArray{newFuture(C.fdb_transaction_get_range(t.ptr, byteSliceToPtr(bkey), C.int(len(bkey)), C.fdb_bool_t(boolToInt(bsel.OrEqual)), C.int(bsel.Offset), byteSliceToPtr(ekey), C.int(len(ekey)), C.fdb_bool_t(boolToInt(esel.OrEqual)), C.int(esel.Offset), C.int(options.Limit), C.int(0), C.FDBStreamingMode(options.Mode-1), C.int(iteration), C.fdb_bool_t(boolToInt(snapshot)), C.fdb_bool_t(boolToInt(options.Reverse))))} +} + +func (t *transaction) getRange(r Range, options RangeOptions, snapshot bool) RangeResult { + f := t.doGetRange(r, options, snapshot, 1) + begin, end := r.FDBRangeKeySelectors() + return RangeResult{ + t: t, + sr: SelectorRange{begin, end}, + options: options, + snapshot: snapshot, + f: &f, + } +} + +// GetRange performs a range read. The returned RangeResult represents all +// KeyValue objects kv where beginKey <= kv.Key < endKey, ordered by kv.Key +// (where beginKey and endKey are the keys described by the key selectors +// returned by r.FDBKeySelectors). All reads performed as a result of GetRange +// are asynchronous and do not block the calling goroutine. +func (t Transaction) GetRange(r Range, options RangeOptions) RangeResult { + return t.getRange(r, options, false) +} + +func (t *transaction) getReadVersion() FutureInt64 { + return &futureInt64{newFuture(C.fdb_transaction_get_read_version(t.ptr))} +} + +// (Infrequently used) GetReadVersion returns the (future) transaction read version. The read is +// performed asynchronously and does not block the calling goroutine. The future +// will become ready when the read version is available. +func (t Transaction) GetReadVersion() FutureInt64 { + return t.getReadVersion() +} + +// Set associated the given key and value, overwriting any previous association +// with key. Set returns immediately, having modified the snapshot of the +// database represented by the transaction. +func (t Transaction) Set(key KeyConvertible, value []byte) { + kb := key.FDBKey() + C.fdb_transaction_set(t.ptr, byteSliceToPtr(kb), C.int(len(kb)), byteSliceToPtr(value), C.int(len(value))) +} + +// Clear removes the specified key (and any associated value), if it +// exists. Clear returns immediately, having modified the snapshot of the +// database represented by the transaction. +func (t Transaction) Clear(key KeyConvertible) { + kb := key.FDBKey() + C.fdb_transaction_clear(t.ptr, byteSliceToPtr(kb), C.int(len(kb))) +} + +// ClearRange removes all keys k such that begin <= k < end, and their +// associated values. ClearRange returns immediately, having modified the +// snapshot of the database represented by the transaction. +func (t Transaction) ClearRange(er ExactRange) { + begin, end := er.FDBRangeKeys() + bkb := begin.FDBKey() + ekb := end.FDBKey() + C.fdb_transaction_clear_range(t.ptr, byteSliceToPtr(bkb), C.int(len(bkb)), byteSliceToPtr(ekb), C.int(len(ekb))) +} + +// (Infrequently used) GetCommittedVersion returns the version number at which a +// successful commit modified the database. This must be called only after the +// successful (non-error) completion of a call to Commit on this Transaction, or +// the behavior is undefined. Read-only transactions do not modify the database +// when committed and will have a committed version of -1. Keep in mind that a +// transaction which reads keys and then sets them to their current values may +// be optimized to a read-only transaction. +func (t Transaction) GetCommittedVersion() (int64, error) { + var version C.int64_t + + if err := C.fdb_transaction_get_committed_version(t.ptr, &version); err != 0 { + return 0, Error{int(err)} + } + + return int64(version), nil +} + +// (Infrequently used) Returns a future which will contain the versionstamp +// which was used by any versionstamp operations in this transaction. The +// future will be ready only after the successful completion of a call to Commit +// on this Transaction. Read-only transactions do not modify the database when +// committed and will result in the future completing with an error. Keep in +// mind that a transaction which reads keys and then sets them to their current +// values may be optimized to a read-only transaction. +func (t Transaction) GetVersionstamp() FutureKey { + return &futureKey{future: newFuture(C.fdb_transaction_get_versionstamp(t.ptr))} +} + +// Reset rolls back a transaction, completely resetting it to its initial +// state. This is logically equivalent to destroying the transaction and +// creating a new one. +func (t Transaction) Reset() { + C.fdb_transaction_reset(t.ptr) +} + +func boolToInt(b bool) int { + if b { + return 1 + } else { + return 0 + } +} + +func (t *transaction) getKey(sel KeySelector, snapshot int) FutureKey { + key := sel.Key.FDBKey() + return &futureKey{future: newFuture(C.fdb_transaction_get_key(t.ptr, byteSliceToPtr(key), C.int(len(key)), C.fdb_bool_t(boolToInt(sel.OrEqual)), C.int(sel.Offset), C.fdb_bool_t(snapshot)))} +} + +// GetKey returns the future key referenced by the provided key selector. The +// read is performed asynchronously and does not block the calling +// goroutine. The future will become ready when the read version is available. +// +// By default, the key is cached for the duration of the transaction, providing +// a potential performance benefit. However, the value of the key is also +// retrieved, using network bandwidth. Invoking +// (TransactionOptions).SetReadYourWritesDisable will avoid both the caching and +// the increased network bandwidth. +func (t Transaction) GetKey(sel Selectable) FutureKey { + return t.getKey(sel.FDBKeySelector(), 0) +} + +func (t Transaction) atomicOp(key []byte, param []byte, code int) { + C.fdb_transaction_atomic_op(t.ptr, byteSliceToPtr(key), C.int(len(key)), byteSliceToPtr(param), C.int(len(param)), C.FDBMutationType(code)) +} + +func addConflictRange(t *transaction, er ExactRange, crtype conflictRangeType) error { + begin, end := er.FDBRangeKeys() + bkb := begin.FDBKey() + ekb := end.FDBKey() + if err := C.fdb_transaction_add_conflict_range(t.ptr, byteSliceToPtr(bkb), C.int(len(bkb)), byteSliceToPtr(ekb), C.int(len(ekb)), C.FDBConflictRangeType(crtype)); err != 0 { + return Error{int(err)} + } + + return nil +} + +// AddReadConflictRange adds a range of keys to the transactions read conflict +// ranges as if you had read the range. As a result, other transactions that +// write a key in this range could cause the transaction to fail with a +// conflict. +// +// For more information on conflict ranges, see +// https://foundationdb.org/documentation/developer-guide.html#conflict-ranges. +func (t Transaction) AddReadConflictRange(er ExactRange) error { + return addConflictRange(t.transaction, er, conflictRangeTypeRead) +} + +func copyAndAppend(orig []byte, b byte) []byte { + ret := make([]byte, len(orig) + 1) + copy(ret, orig) + ret[len(orig)] = b + return ret +} + +// AddReadConflictKey adds a key to the transactions read conflict ranges as if +// you had read the key. As a result, other transactions that concurrently write +// this key could cause the transaction to fail with a conflict. +// +// For more information on conflict ranges, see +// https://foundationdb.org/documentation/developer-guide.html#conflict-ranges. +func (t Transaction) AddReadConflictKey(key KeyConvertible) error { + return addConflictRange(t.transaction, KeyRange{key, Key(copyAndAppend(key.FDBKey(), 0x00))}, conflictRangeTypeRead) +} + +// AddWriteConflictRange adds a range of keys to the transactions write +// conflict ranges as if you had cleared the range. As a result, other +// transactions that concurrently read a key in this range could fail with a +// conflict. +// +// For more information on conflict ranges, see +// https://foundationdb.org/documentation/developer-guide.html#conflict-ranges. +func (t Transaction) AddWriteConflictRange(er ExactRange) error { + return addConflictRange(t.transaction, er, conflictRangeTypeWrite) +} + +// AddWriteConflictKey adds a key to the transactions write conflict ranges as +// if you had written the key. As a result, other transactions that concurrently +// read this key could fail with a conflict. +// +// For more information on conflict ranges, see +// https://foundationdb.org/documentation/developer-guide.html#conflict-ranges. +func (t Transaction) AddWriteConflictKey(key KeyConvertible) error { + return addConflictRange(t.transaction, KeyRange{key, Key(copyAndAppend(key.FDBKey(), 0x00))}, conflictRangeTypeWrite) +} + +// Options returns a TransactionOptions instance suitable for setting options +// specific to this transaction. +func (t Transaction) Options() TransactionOptions { + return TransactionOptions{t.transaction} +} + +func localityGetAddressesForKey(t *transaction, key KeyConvertible) FutureStringSlice { + kb := key.FDBKey() + return &futureStringSlice{newFuture(C.fdb_transaction_get_addresses_for_key(t.ptr, byteSliceToPtr(kb), C.int(len(kb))))} +} + +// LocalityGetAddressesForKey returns the (future) public network addresses of +// each of the storage servers responsible for storing key and its associated +// value. The read is performed asynchronously and does not block the calling +// goroutine. The future will become ready when the read is complete. +func (t Transaction) LocalityGetAddressesForKey(key KeyConvertible) FutureStringSlice { + return localityGetAddressesForKey(t.transaction, key) +} diff --git a/bindings/go/src/fdb/tuple/tuple.go b/bindings/go/src/fdb/tuple/tuple.go new file mode 100644 index 0000000000..2cec4c2a64 --- /dev/null +++ b/bindings/go/src/fdb/tuple/tuple.go @@ -0,0 +1,259 @@ +/* + * tuple.go + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// FoundationDB Go Tuple Layer + +// Package tuple provides a layer for encoding and decoding multi-element tuples +// into keys usable by FoundationDB. The encoded key maintains the same sort +// order as the original tuple: sorted first by the first element, then by the +// second element, etc. This makes the tuple layer ideal for building a variety +// of higher-level data models. +// +// For general guidance on tuple usage, see the Tuple section of Data Modeling +// (https://foundationdb.org/documentation/data-modeling.html#data-modeling-tuples). +// +// FoundationDB tuples can currently encode byte and unicode strings, integers +// and NULL values. In Go these are represented as []byte, string, int64 and +// nil. +package tuple + +import ( + "fmt" + "encoding/binary" + "bytes" + "fdb" +) + +// A TupleElement is one of the types that may be encoded in FoundationDB +// tuples. Although the Go compiler cannot enforce this, it is a programming +// error to use an unsupported types as a TupleElement (and will typically +// result in a runtime panic). +// +// The valid types for TupleElement are []byte (or fdb.KeyConvertible), string, +// int64 (or int), and nil. +type TupleElement interface{} + +// Tuple is a slice of objects that can be encoded as FoundationDB tuples. If +// any of the TupleElements are of unsupported types, a runtime panic will occur +// when the Tuple is packed. +// +// Given a Tuple T containing objects only of these types, then T will be +// identical to the Tuple returned by unpacking the byte slice obtained by +// packing T (modulo type normalization to []byte and int64). +type Tuple []TupleElement + +var sizeLimits = []uint64{ + 1 << (0 * 8) - 1, + 1 << (1 * 8) - 1, + 1 << (2 * 8) - 1, + 1 << (3 * 8) - 1, + 1 << (4 * 8) - 1, + 1 << (5 * 8) - 1, + 1 << (6 * 8) - 1, + 1 << (7 * 8) - 1, + 1 << (8 * 8) - 1, +} + +func encodeBytes(buf *bytes.Buffer, code byte, b []byte) { + buf.WriteByte(code) + buf.Write(bytes.Replace(b, []byte{0x00}, []byte{0x00, 0xFF}, -1)) + buf.WriteByte(0x00) +} + +func bisectLeft(u uint64) int { + var n int + for sizeLimits[n] < u { + n += 1 + } + return n +} + +func encodeInt(buf *bytes.Buffer, i int64) { + if i == 0 { + buf.WriteByte(0x14) + return + } + + var n int + var ibuf bytes.Buffer + + switch { + case i > 0: + n = bisectLeft(uint64(i)) + buf.WriteByte(byte(0x14+n)) + binary.Write(&ibuf, binary.BigEndian, i) + case i < 0: + n = bisectLeft(uint64(-i)) + buf.WriteByte(byte(0x14-n)) + binary.Write(&ibuf, binary.BigEndian, int64(sizeLimits[n])+i) + } + + buf.Write(ibuf.Bytes()[8-n:]) +} + +// Pack returns a new byte slice encoding the provided tuple. Pack will panic if +// the tuple contains an element of any type other than []byte, +// fdb.KeyConvertible, string, int64, int or nil. +// +// Tuple satisfies the fdb.KeyConvertible interface, so it is not necessary to +// call Pack when using a Tuple with a FoundationDB API function that requires a +// key. +func (t Tuple) Pack() []byte { + buf := new(bytes.Buffer) + + for i, e := range(t) { + switch e := e.(type) { + case nil: + buf.WriteByte(0x00) + case int64: + encodeInt(buf, e) + case int: + encodeInt(buf, int64(e)) + case []byte: + encodeBytes(buf, 0x01, e) + case fdb.KeyConvertible: + encodeBytes(buf, 0x01, []byte(e.FDBKey())) + case string: + encodeBytes(buf, 0x02, []byte(e)) + default: + panic(fmt.Sprintf("unencodable element at index %d (%v, type %T)", i, t[i], t[i])) + } + } + + return buf.Bytes() +} + +func findTerminator(b []byte) int { + bp := b + var length int + + for { + idx := bytes.IndexByte(bp, 0x00) + length += idx + if idx + 1 == len(bp) || bp[idx+1] != 0xFF { + break + } + length += 2 + bp = bp[idx+2:] + } + + return length +} + +func decodeBytes(b []byte) ([]byte, int) { + idx := findTerminator(b[1:]) + return bytes.Replace(b[1:idx+1], []byte{0x00, 0xFF}, []byte{0x00}, -1), idx + 2 +} + +func decodeString(b []byte) (string, int) { + bp, idx := decodeBytes(b) + return string(bp), idx +} + +func decodeInt(b []byte) (int64, int) { + if b[0] == 0x14 { + return 0, 1 + } + + var neg bool + + n := int(b[0]) - 20 + if n < 0 { + n = -n + neg = true + } + + bp := make([]byte, 8) + copy(bp[8-n:], b[1:n+1]) + + var ret int64 + + binary.Read(bytes.NewBuffer(bp), binary.BigEndian, &ret) + + if neg { + ret -= int64(sizeLimits[n]) + } + + return ret, n+1 +} + +// Unpack returns the tuple encoded by the provided byte slice, or an error if +// the key does not correctly encode a FoundationDB tuple. +func Unpack(b []byte) (Tuple, error) { + var t Tuple + + var i int + + for i < len(b) { + var el interface{} + var off int + + switch { + case b[i] == 0x00: + el = nil + off = 1 + case b[i] == 0x01: + el, off = decodeBytes(b[i:]) + case b[i] == 0x02: + el, off = decodeString(b[i:]) + case 0x0c <= b[i] && b[i] <= 0x1c: + el, off = decodeInt(b[i:]) + default: + return nil, fmt.Errorf("unable to decode tuple element with unknown typecode %02x", b[i]) + } + + t = append(t, el) + i += off + } + + return t, nil +} + +// FDBKey returns the packed representation of a Tuple, and allows Tuple to +// satisfy the fdb.KeyConvertible interface. FDBKey will panic in the same +// circumstances as Pack. +func (t Tuple) FDBKey() fdb.Key { + return t.Pack() +} + +// FDBRangeKeys allows Tuple to satisfy the fdb.ExactRange interface. The range +// represents all keys that encode tuples strictly starting with a Tuple (that +// is, all tuples of greater length than the Tuple of which the Tuple is a +// prefix). +func (t Tuple) FDBRangeKeys() (fdb.KeyConvertible, fdb.KeyConvertible) { + p := t.Pack() + return fdb.Key(concat(p, 0x00)), fdb.Key(concat(p, 0xFF)) +} + +// FDBRangeKeySelectors allows Tuple to satisfy the fdb.Range interface. The +// range represents all keys that encode tuples strictly starting with a Tuple +// (that is, all tuples of greater length than the Tuple of which the Tuple is a +// prefix). +func (t Tuple) FDBRangeKeySelectors() (fdb.Selectable, fdb.Selectable) { + b, e := t.FDBRangeKeys() + return fdb.FirstGreaterOrEqual(b), fdb.FirstGreaterOrEqual(e) +} + +func concat(a []byte, b ...byte) []byte { + r := make([]byte, len(a) + len(b)) + copy(r, a) + copy(r[len(a):], b) + return r +} diff --git a/bindings/java/fdbJNI.cpp b/bindings/java/fdbJNI.cpp new file mode 100644 index 0000000000..1795ac7f37 --- /dev/null +++ b/bindings/java/fdbJNI.cpp @@ -0,0 +1,1065 @@ +/* + * fdbJNI.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +#define FDB_API_VERSION 500 + +#include + +#define JNI_NULL 0 + +#if defined(__GNUG__) +#define thread_local __thread +// TODO: figure out why the default definition suppresses visibility +#undef JNIEXPORT +#define JNIEXPORT __attribute__ ((visibility ("default"))) +#elif defined(_MSC_VER) +#define thread_local __declspec(thread) +#else +#error Missing thread local storage +#endif + +static thread_local JNIEnv* g_thread_jenv = 0; // Defined for the network thread once it is running, and for any thread that has called registerCallback +static thread_local jmethodID g_IFutureCallback_call_methodID = 0; + +void throwOutOfMem(JNIEnv *jenv) { + const char *className = "java/lang/OutOfMemoryError"; + jclass illegalArgClass = jenv->FindClass( className ); + + if(jenv->ExceptionOccurred()) + return; + + if( jenv->ThrowNew( illegalArgClass, NULL ) != 0 ) { + if( !jenv->ExceptionOccurred() ) { + jenv->FatalError("Could not throw OutOfMemoryError"); + } else { + // This means that an exception is pending. We do not know what it is, but are sure + // that control flow will include throwing that exception into the calling Java code. + } + } +} + +static jthrowable getThrowable(JNIEnv *jenv, fdb_error_t e, const char* msg = NULL) { + jclass excepClass = jenv->FindClass("com/apple/cie/foundationdb/FDBException"); + if(jenv->ExceptionOccurred()) + return JNI_NULL; + + jmethodID excepCtor = jenv->GetMethodID(excepClass, "", "(Ljava/lang/String;I)V"); + if(jenv->ExceptionOccurred()) + return JNI_NULL; + + const char *fdb_message = msg ? msg : fdb_get_error(e); + jstring m = jenv->NewStringUTF(fdb_message); + if(jenv->ExceptionOccurred()) + return JNI_NULL; + + jthrowable t = (jthrowable)jenv->NewObject(excepClass, excepCtor, m, e); + if(jenv->ExceptionOccurred()) + return JNI_NULL; + + return t; +} + +void throwNamedException(JNIEnv *jenv, const char *class_full_name, const char* message ) { + jclass exceptionClass = jenv->FindClass( class_full_name ); + if(jenv->ExceptionOccurred()) + return; + + if( jenv->ThrowNew( exceptionClass, message ) != 0 ) { + if(jenv->ExceptionOccurred()) + return; + jenv->FatalError("FDB: Error throwing exception"); + } +} + +void throwRuntimeEx(JNIEnv *jenv, const char* message) { + throwNamedException( jenv, "java/lang/RuntimeException", message ); +} + +void throwParamNotNull(JNIEnv *jenv) { + throwNamedException( jenv, "java/lang/IllegalArgumentException", "Argument cannot be null" ); +} + +#ifdef __cplusplus +extern "C" { +#endif + +static void callCallback( FDBFuture* f, void* data ) { + jobject callback = (jobject)data; + g_thread_jenv->CallVoidMethod( callback, g_IFutureCallback_call_methodID ); + g_thread_jenv->DeleteGlobalRef(callback); +} + +// If the methods are not found, exceptions are thrown and this will return false. +// Returns TRUE on success, false otherwise. +static bool findCallbackMethods(JNIEnv *jenv) { + jclass cls = jenv->FindClass("java/lang/Runnable"); + if(jenv->ExceptionOccurred()) + return false; + + g_IFutureCallback_call_methodID = jenv->GetMethodID(cls, "run", "()V"); + if(jenv->ExceptionOccurred()) + return false; + + return true; +} + +// Attempts to throw 't', attempts to shut down the JVM if this fails. +void safeThrow( JNIEnv *jenv, jthrowable t ) { + if( jenv->Throw( t ) != 0 ) { + jenv->FatalError("FDB: Unable to throw exception"); + } +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_NativeFuture_Future_1registerCallback(JNIEnv *jenv, jobject cls, jlong future, jobject callback) { + // SOMEDAY: Do this on module load instead. Can we cache method ids across threads? + if( !g_IFutureCallback_call_methodID ) { + if( !findCallbackMethods( jenv ) ) { + return; + } + } + + if( !future || !callback ) { + throwParamNotNull(jenv); + return; + } + FDBFuture *f = (FDBFuture *)future; + + // This is documented as not throwing, but simply returning NULL on OMM. + // As belt and suspenders, we will check for pending exceptions and then, + // if there are none and the result is NULL, we'll throw our own OMM. + callback = jenv->NewGlobalRef( callback ); + if( !callback ) { + if( !jenv->ExceptionOccurred() ) + throwOutOfMem(jenv); + return; + } + + // Here we cache a thread-local reference to jenv + g_thread_jenv = jenv; + fdb_error_t err = fdb_future_set_callback( f, &callCallback, callback ); + if( err ) + safeThrow( jenv, getThrowable( jenv, err ) ); +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_NativeFuture_Future_1blockUntilReady(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return; + } + FDBFuture *sav = (FDBFuture *)future; + + fdb_error_t err = fdb_future_block_until_ready( sav ); + if( err ) + safeThrow( jenv, getThrowable( jenv, err ) ); +} + +JNIEXPORT jthrowable JNICALL Java_com_apple_cie_foundationdb_NativeFuture_Future_1getError(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return JNI_NULL; + } + FDBFuture *sav = (FDBFuture *)future; + return getThrowable( jenv, fdb_future_get_error( sav ) ); +} + +JNIEXPORT jboolean JNICALL Java_com_apple_cie_foundationdb_NativeFuture_Future_1isReady(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return JNI_FALSE; + } + FDBFuture *var = (FDBFuture *)future; + return (jboolean)fdb_future_is_ready(var); +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_NativeFuture_Future_1dispose(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return; + } + FDBFuture *var = (FDBFuture *)future; + fdb_future_destroy(var); +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_NativeFuture_Future_1cancel(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return; + } + FDBFuture *var = (FDBFuture *)future; + fdb_future_cancel(var); +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_NativeFuture_Future_1releaseMemory(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return; + } + FDBFuture *var = (FDBFuture *)future; + fdb_future_release_memory(var); +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FutureVersion_FutureVersion_1get(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return 0; + } + FDBFuture *f = (FDBFuture *)future; + + int64_t version = 0; + fdb_error_t err = fdb_future_get_version(f, &version); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + return 0; + } + + return (jlong)version; +} + +JNIEXPORT jobject JNICALL Java_com_apple_cie_foundationdb_FutureStrings_FutureStrings_1get(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return JNI_NULL; + } + FDBFuture *f = (FDBFuture *)future; + + const char **strings; + int count; + fdb_error_t err = fdb_future_get_string_array( f, &strings, &count ); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + return JNI_NULL; + } + + jclass str_clazz = jenv->FindClass("java/lang/String"); + if( jenv->ExceptionOccurred() ) + return JNI_NULL; + jobjectArray arr = jenv->NewObjectArray(count, str_clazz, NULL); + if( !arr ) { + if( !jenv->ExceptionOccurred() ) + throwOutOfMem(jenv); + return JNI_NULL; + } + + for(int i = 0; i < count; i++) { + jstring str = jenv->NewStringUTF( strings[i] ); + if( !str ) { + if( !jenv->ExceptionOccurred() ) + throwOutOfMem(jenv); + return JNI_NULL; + } + + jenv->SetObjectArrayElement( arr, i, str ); + if( jenv->ExceptionOccurred() ) + return JNI_NULL; + } + + return arr; +} + +JNIEXPORT jobject JNICALL Java_com_apple_cie_foundationdb_FutureResults_FutureResults_1getSummary(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return JNI_NULL; + } + + jclass resultCls = jenv->FindClass("com/apple/cie/foundationdb/RangeResultSummary"); + if( jenv->ExceptionOccurred() ) + return JNI_NULL; + jmethodID resultCtorId = jenv->GetMethodID(resultCls, "", "([BIZ)V"); + if( jenv->ExceptionOccurred() ) + return JNI_NULL; + + FDBFuture *f = (FDBFuture *)future; + + const FDBKeyValue *kvs; + int count; + fdb_bool_t more; + fdb_error_t err = fdb_future_get_keyvalue_array( f, &kvs, &count, &more ); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + return JNI_NULL; + } + + jbyteArray lastKey = NULL; + if(count) { + lastKey = jenv->NewByteArray(kvs[count - 1].key_length); + if( !lastKey ) { + if( !jenv->ExceptionOccurred() ) + throwOutOfMem(jenv); + return JNI_NULL; + } + + uint8_t *keyvalues_barr = (uint8_t *)jenv->GetByteArrayElements(lastKey, NULL); + if (!keyvalues_barr) { + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return JNI_NULL; + } + + memcpy(keyvalues_barr, kvs[count - 1].key, kvs[count - 1].key_length); + // void function that is not documented as not throwing + jenv->ReleaseByteArrayElements(lastKey, (jbyte *)keyvalues_barr, 0); + } + + jobject result = jenv->NewObject(resultCls, resultCtorId, lastKey, count, (jboolean)more); + if( jenv->ExceptionOccurred() ) + return JNI_NULL; + + return result; +} + +JNIEXPORT jobject JNICALL Java_com_apple_cie_foundationdb_FutureResults_FutureResults_1get(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return JNI_NULL; + } + + jclass resultCls = jenv->FindClass("com/apple/cie/foundationdb/RangeResult"); + jmethodID resultCtorId = jenv->GetMethodID(resultCls, "", "([B[IZ)V"); + + FDBFuture *f = (FDBFuture *)future; + + const FDBKeyValue *kvs; + int count; + fdb_bool_t more; + fdb_error_t err = fdb_future_get_keyvalue_array( f, &kvs, &count, &more ); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + return JNI_NULL; + } + + int totalKeyValueSize = 0; + for(int i = 0; i < count; i++) { + totalKeyValueSize += kvs[i].key_length + kvs[i].value_length; + } + + jbyteArray keyValueArray = jenv->NewByteArray(totalKeyValueSize); + if( !keyValueArray ) { + if( !jenv->ExceptionOccurred() ) + throwOutOfMem(jenv); + return JNI_NULL; + } + uint8_t *keyvalues_barr = (uint8_t *)jenv->GetByteArrayElements(keyValueArray, NULL); + if (!keyvalues_barr) { + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return JNI_NULL; + } + + jintArray lengthArray = jenv->NewIntArray(count * 2); + if( !lengthArray ) { + if( !jenv->ExceptionOccurred() ) + throwOutOfMem(jenv); + return JNI_NULL; + } + + jint *length_barr = jenv->GetIntArrayElements(lengthArray, NULL); + if( !length_barr ) { + if( !jenv->ExceptionOccurred() ) + throwOutOfMem(jenv); + return JNI_NULL; + } + + int offset = 0; + for(int i = 0; i < count; i++) { + memcpy(keyvalues_barr + offset, kvs[i].key, kvs[i].key_length); + length_barr[ i * 2 ] = kvs[i].key_length; + offset += kvs[i].key_length; + + memcpy(keyvalues_barr + offset, kvs[i].value, kvs[i].value_length); + length_barr[ (i * 2) + 1 ] = kvs[i].value_length; + offset += kvs[i].value_length; + } + + jenv->ReleaseByteArrayElements(keyValueArray, (jbyte *)keyvalues_barr, 0); + jenv->ReleaseIntArrayElements(lengthArray, length_barr, 0); + + jobject result = jenv->NewObject(resultCls, resultCtorId, keyValueArray, lengthArray, (jboolean)more); + if( jenv->ExceptionOccurred() ) + return JNI_NULL; + + return result; +} + +// SOMEDAY: this could be done much more efficiently with Direct ByteBuffers +JNIEXPORT jbyteArray JNICALL Java_com_apple_cie_foundationdb_FutureResult_FutureResult_1get(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return JNI_NULL; + } + FDBFuture *f = (FDBFuture *)future; + + fdb_bool_t present; + const uint8_t *value; + int length; + fdb_error_t err = fdb_future_get_value(f, &present, &value, &length); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + return JNI_NULL; + } + + if( !present ) + return JNI_NULL; + + jbyteArray result = jenv->NewByteArray(length); + if( !result ) { + if( !jenv->ExceptionOccurred() ) + throwOutOfMem(jenv); + return JNI_NULL; + } + uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements(result, NULL); + if (!barr) { + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return JNI_NULL; + } + + memcpy(barr, value, length); + // passing "0" here commits the data back and releases the native copy + jenv->ReleaseByteArrayElements(result, (jbyte *)barr, 0); + return result; +} + +JNIEXPORT jbyteArray JNICALL Java_com_apple_cie_foundationdb_FutureKey_FutureKey_1get(JNIEnv * jenv, jclass, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return JNI_NULL; + } + FDBFuture *f = (FDBFuture *)future; + + const uint8_t *value; + int length; + fdb_error_t err = fdb_future_get_key(f, &value, &length); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + return JNI_NULL; + } + + jbyteArray result = jenv->NewByteArray(length); + if( !result ) { + if( !jenv->ExceptionOccurred() ) + throwOutOfMem(jenv); + return JNI_NULL; + } + uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements(result, NULL); + if (!barr) { + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return JNI_NULL; + } + + memcpy(barr, value, length); + // passing "0" here commits the data back and releases the native copy + jenv->ReleaseByteArrayElements(result, (jbyte *)barr, 0); + return result; +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FutureCluster_FutureCluster_1get(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return 0; + } + FDBFuture *f = (FDBFuture *)future; + + FDBCluster *cluster; + fdb_error_t err = fdb_future_get_cluster(f, &cluster); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + return 0; + } + return (jlong)cluster; +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FutureDatabase_FutureDatabase_1get(JNIEnv *jenv, jobject, jlong future) { + if( !future ) { + throwParamNotNull(jenv); + return 0; + } + FDBFuture *f = (FDBFuture *)future; + + FDBDatabase *database; + fdb_error_t err = fdb_future_get_database(f, &database); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + return 0; + } + return (jlong)database; +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBDatabase_Database_1createTransaction(JNIEnv *jenv, jobject, jlong dbPtr) { + if( !dbPtr ) { + throwParamNotNull(jenv); + return 0; + } + FDBDatabase *database = (FDBDatabase *)dbPtr; + FDBTransaction *tr; + fdb_error_t err = fdb_database_create_transaction(database, &tr); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + return 0; + } + return (jlong)tr; +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBDatabase_Database_1dispose(JNIEnv *jenv, jobject, jlong dPtr) { + if( !dPtr ) { + throwParamNotNull(jenv); + return; + } + fdb_database_destroy( (FDBDatabase *)dPtr ); +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBDatabase_Database_1setOption(JNIEnv *jenv, jobject, jlong dPtr, jint code, jbyteArray value) { + if( !dPtr ) { + throwParamNotNull(jenv); + return; + } + FDBDatabase *c = (FDBDatabase *)dPtr; + uint8_t *barr = NULL; + int size = 0; + + if(value != 0) { + barr = (uint8_t *)jenv->GetByteArrayElements( value, NULL ); + if (!barr) { + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + size = jenv->GetArrayLength( value ); + } + fdb_error_t err = fdb_database_set_option( c, (FDBDatabaseOption)code, barr, size ); + if(value != 0) + jenv->ReleaseByteArrayElements( value, (jbyte *)barr, JNI_ABORT ); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + } +} + +JNIEXPORT jboolean JNICALL Java_com_apple_cie_foundationdb_FDB_Error_1predicate(JNIEnv *jenv, jobject, jint predicate, jint code) { + return (jboolean)fdb_error_predicate(predicate, code); +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDB_Cluster_1create(JNIEnv *jenv, jobject, jstring clusterFileName) { + const char* fileName = 0; + if(clusterFileName != 0) { + fileName = jenv->GetStringUTFChars(clusterFileName, 0); + if( jenv->ExceptionOccurred() ) + return 0; + } + FDBFuture *cluster = fdb_create_cluster( fileName ); + if(clusterFileName != 0) + jenv->ReleaseStringUTFChars( clusterFileName, fileName ); + return (jlong)cluster; +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_Cluster_Cluster_1setOption(JNIEnv *jenv, jobject, jlong cPtr, jint code, jbyteArray value) { + if( !cPtr ) { + throwParamNotNull(jenv); + return; + } + FDBCluster *c = (FDBCluster *)cPtr; + uint8_t *barr = NULL; + int size = 0; + + if(value != 0) { + barr = (uint8_t *)jenv->GetByteArrayElements( value, NULL ); + if (!barr) { + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + size = jenv->GetArrayLength( value ); + } + fdb_error_t err = fdb_cluster_set_option( c, (FDBClusterOption)code, barr, size ); + if(value != 0) + jenv->ReleaseByteArrayElements( value, (jbyte *)barr, JNI_ABORT ); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + } +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_Cluster_Cluster_1dispose(JNIEnv *jenv, jobject, jlong cPtr) { + if( !cPtr ) { + throwParamNotNull(jenv); + return; + } + fdb_cluster_destroy( (FDBCluster *)cPtr ); +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_Cluster_Cluster_1createDatabase(JNIEnv *jenv, jobject, jlong cPtr, jbyteArray dbNameBytes) { + if( !cPtr || !dbNameBytes ) { + throwParamNotNull(jenv); + return 0; + } + FDBCluster *cluster = (FDBCluster *)cPtr; + + uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( dbNameBytes, NULL ); + if (!barr) { + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return 0; + } + + int size = jenv->GetArrayLength( dbNameBytes ); + FDBFuture * f = fdb_cluster_create_database( cluster, barr, size ); + jenv->ReleaseByteArrayElements( dbNameBytes, (jbyte *)barr, JNI_ABORT ); + return (jlong)f; +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1setVersion(JNIEnv *jenv, jobject, jlong tPtr, jlong version) { + if( !tPtr ) { + throwParamNotNull(jenv); + return; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + fdb_transaction_set_read_version( tr, version ); +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1getReadVersion(JNIEnv *jenv, jobject, jlong tPtr) { + if( !tPtr ) { + throwParamNotNull(jenv); + return 0; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + FDBFuture *f = fdb_transaction_get_read_version( tr ); + return (jlong)f; +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1get(JNIEnv *jenv, jobject, jlong tPtr, jbyteArray keyBytes, jboolean snapshot) { + if( !tPtr || !keyBytes ) { + throwParamNotNull(jenv); + return 0; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + + uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( keyBytes, NULL ); + if(!barr) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return 0; + } + + FDBFuture *f = fdb_transaction_get( tr, barr, jenv->GetArrayLength( keyBytes ), (fdb_bool_t)snapshot ); + jenv->ReleaseByteArrayElements( keyBytes, (jbyte *)barr, JNI_ABORT ); + return (jlong)f; +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1getKey(JNIEnv *jenv, jobject, jlong tPtr, + jbyteArray keyBytes, jboolean orEqual, jint offset, jboolean snapshot) { + if( !tPtr || !keyBytes ) { + throwParamNotNull(jenv); + return 0; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + + uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( keyBytes, NULL ); + if(!barr) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return 0; + } + + FDBFuture *f = fdb_transaction_get_key( tr, barr, jenv->GetArrayLength( keyBytes ), orEqual, offset, (fdb_bool_t)snapshot ); + jenv->ReleaseByteArrayElements( keyBytes, (jbyte *)barr, JNI_ABORT ); + return (jlong)f; +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1getRange + (JNIEnv *jenv, jobject, jlong tPtr, jbyteArray keyBeginBytes, jboolean orEqualBegin, jint offsetBegin, + jbyteArray keyEndBytes, jboolean orEqualEnd, jint offsetEnd, jint rowLimit, jint targetBytes, + jint streamingMode, jint iteration, jboolean snapshot, jboolean reverse) { + if( !tPtr || !keyBeginBytes || !keyEndBytes ) { + throwParamNotNull(jenv); + return 0; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + + uint8_t *barrBegin = (uint8_t *)jenv->GetByteArrayElements( keyBeginBytes, NULL ); + if (!barrBegin) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return 0; + } + + uint8_t *barrEnd = (uint8_t *)jenv->GetByteArrayElements( keyEndBytes, NULL ); + if (!barrEnd) { + jenv->ReleaseByteArrayElements( keyBeginBytes, (jbyte *)barrBegin, JNI_ABORT ); + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return 0; + } + + FDBFuture *f = fdb_transaction_get_range( tr, + barrBegin, jenv->GetArrayLength( keyBeginBytes ), orEqualBegin, offsetBegin, + barrEnd, jenv->GetArrayLength( keyEndBytes ), orEqualEnd, offsetEnd, rowLimit, + targetBytes, (FDBStreamingMode)streamingMode, iteration, snapshot, reverse); + jenv->ReleaseByteArrayElements( keyBeginBytes, (jbyte *)barrBegin, JNI_ABORT ); + jenv->ReleaseByteArrayElements( keyEndBytes, (jbyte *)barrEnd, JNI_ABORT ); + return (jlong)f; +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1set(JNIEnv *jenv, jobject, jlong tPtr, jbyteArray keyBytes, jbyteArray valueBytes) { + if( !tPtr || !keyBytes || !valueBytes ) { + throwParamNotNull(jenv); + return; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + + uint8_t *barrKey = (uint8_t *)jenv->GetByteArrayElements( keyBytes, NULL ); + if (!barrKey) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + + uint8_t *barrValue = (uint8_t *)jenv->GetByteArrayElements( valueBytes, NULL ); + if (!barrValue) { + jenv->ReleaseByteArrayElements( keyBytes, (jbyte *)barrKey, JNI_ABORT ); + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + + fdb_transaction_set( tr, + barrKey, jenv->GetArrayLength( keyBytes ), + barrValue, jenv->GetArrayLength( valueBytes ) ); + jenv->ReleaseByteArrayElements( keyBytes, (jbyte *)barrKey, JNI_ABORT ); + jenv->ReleaseByteArrayElements( valueBytes, (jbyte *)barrValue, JNI_ABORT ); +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1clear__J_3B(JNIEnv *jenv, jobject, jlong tPtr, jbyteArray keyBytes) { + if( !tPtr || !keyBytes ) { + throwParamNotNull(jenv); + return; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + + uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( keyBytes, NULL ); + if (!barr) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + + fdb_transaction_clear( tr, barr, jenv->GetArrayLength( keyBytes ) ); + jenv->ReleaseByteArrayElements( keyBytes, (jbyte *)barr, JNI_ABORT ); +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1clear__J_3B_3B(JNIEnv *jenv, jobject, jlong tPtr, jbyteArray keyBeginBytes, jbyteArray keyEndBytes) { + if( !tPtr || !keyBeginBytes || !keyEndBytes ) { + throwParamNotNull(jenv); + return; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + + uint8_t *barrKeyBegin = (uint8_t *)jenv->GetByteArrayElements( keyBeginBytes, NULL ); + if (!barrKeyBegin) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + + uint8_t *barrKeyEnd = (uint8_t *)jenv->GetByteArrayElements( keyEndBytes, NULL ); + if (!barrKeyEnd) { + jenv->ReleaseByteArrayElements( keyBeginBytes, (jbyte *)barrKeyBegin, JNI_ABORT ); + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + + fdb_transaction_clear_range( tr, + barrKeyBegin, jenv->GetArrayLength( keyBeginBytes ), + barrKeyEnd, jenv->GetArrayLength( keyEndBytes ) ); + jenv->ReleaseByteArrayElements( keyBeginBytes, (jbyte *)barrKeyBegin, JNI_ABORT ); + jenv->ReleaseByteArrayElements( keyEndBytes, (jbyte *)barrKeyEnd, JNI_ABORT ); +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1mutate(JNIEnv *jenv, jobject, jlong tPtr, jint code, + jbyteArray key, jbyteArray value ) { + if( !tPtr || !key || !value ) { + throwParamNotNull(jenv); + return; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + + uint8_t *barrKey = (uint8_t *)jenv->GetByteArrayElements( key, NULL ); + if (!barrKey) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + + uint8_t *barrValue = (uint8_t *)jenv->GetByteArrayElements( value, NULL ); + if (!barrValue) { + jenv->ReleaseByteArrayElements( key, (jbyte *)barrKey, JNI_ABORT ); + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + + fdb_transaction_atomic_op( tr, + barrKey, jenv->GetArrayLength( key ), + barrValue, jenv->GetArrayLength( value ), + (FDBMutationType)code); + + jenv->ReleaseByteArrayElements( key, (jbyte *)barrKey, JNI_ABORT ); + jenv->ReleaseByteArrayElements( value, (jbyte *)barrValue, JNI_ABORT ); +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1commit(JNIEnv *jenv, jobject, jlong tPtr) { + if( !tPtr ) { + throwParamNotNull(jenv); + return 0; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + FDBFuture *f = fdb_transaction_commit( tr ); + return (jlong)f; +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1setOption(JNIEnv *jenv, jobject, jlong tPtr, jint code, jbyteArray value) { + if( !tPtr ) { + throwParamNotNull(jenv); + return; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + uint8_t *barr = NULL; + int size = 0; + + if(value != 0) { + barr = (uint8_t *)jenv->GetByteArrayElements( value, NULL ); + if (!barr) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + size = jenv->GetArrayLength( value ); + } + fdb_error_t err = fdb_transaction_set_option( tr, (FDBTransactionOption)code, barr, size ); + if(value != 0) + jenv->ReleaseByteArrayElements( value, (jbyte *)barr, JNI_ABORT ); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + } +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1getCommittedVersion(JNIEnv *jenv, jobject, jlong tPtr) { + if( !tPtr ) { + throwParamNotNull(jenv); + return 0; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + int64_t version; + fdb_error_t err = fdb_transaction_get_committed_version( tr, &version ); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + return 0; + } + return (jlong)version; +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1getVersionstamp(JNIEnv *jenv, jobject, jlong tPtr) { + if (!tPtr) { + throwParamNotNull(jenv); + return 0; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + FDBFuture *f = fdb_transaction_get_versionstamp(tr); + return (jlong)f; +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1getKeyLocations(JNIEnv *jenv, jobject, jlong tPtr, jbyteArray key) { + if( !tPtr || !key ) { + throwParamNotNull(jenv); + return 0; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + + uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( key, NULL ); + if (!barr) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return 0; + } + int size = jenv->GetArrayLength( key ); + + FDBFuture *f = fdb_transaction_get_addresses_for_key( tr, barr, size ); + return (jlong)f; +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1onError(JNIEnv *jenv, jobject, jlong tPtr, jint errorCode) { + if( !tPtr ) { + throwParamNotNull(jenv); + return 0; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + FDBFuture *f = fdb_transaction_on_error( tr, (fdb_error_t)errorCode ); + return (jlong)f; +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1dispose(JNIEnv *jenv, jobject, jlong tPtr) { + if( !tPtr ) { + throwParamNotNull(jenv); + return; + } + fdb_transaction_destroy( (FDBTransaction *)tPtr ); +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1reset(JNIEnv *jenv, jobject, jlong tPtr) { + if( !tPtr ) { + throwParamNotNull(jenv); + return; + } + fdb_transaction_reset( (FDBTransaction *)tPtr ); +} + +JNIEXPORT jlong JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1watch(JNIEnv *jenv, jobject, jlong tPtr, jbyteArray key) { + if( !tPtr || !key ) { + throwParamNotNull(jenv); + return 0; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + + uint8_t *barr = (uint8_t *)jenv->GetByteArrayElements( key, NULL ); + if (!barr) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return 0; + } + int size = jenv->GetArrayLength( key ); + FDBFuture *f = fdb_transaction_watch( tr, barr, size ); + return (jlong)f; +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1cancel(JNIEnv *jenv, jobject, jlong tPtr) { + if( !tPtr ) { + throwParamNotNull(jenv); + return; + } + fdb_transaction_cancel( (FDBTransaction *)tPtr ); +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDBTransaction_Transaction_1addConflictRange( + JNIEnv *jenv, jobject, jlong tPtr, jbyteArray keyBegin, jbyteArray keyEnd, jint conflictType) { + if( !tPtr || !keyBegin || !keyEnd ) { + throwParamNotNull(jenv); + return; + } + FDBTransaction *tr = (FDBTransaction *)tPtr; + + uint8_t *begin_barr = (uint8_t *)jenv->GetByteArrayElements( keyBegin, NULL ); + if (!begin_barr) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + int begin_size = jenv->GetArrayLength( keyBegin ); + + uint8_t *end_barr = (uint8_t *)jenv->GetByteArrayElements( keyEnd, NULL ); + if (!end_barr) { + jenv->ReleaseByteArrayElements( keyBegin, (jbyte *)begin_barr, JNI_ABORT ); + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + int end_size = jenv->GetArrayLength( keyEnd ); + + fdb_error_t err = fdb_transaction_add_conflict_range( tr, begin_barr, begin_size, end_barr, end_size, (FDBConflictRangeType)conflictType ); + + jenv->ReleaseByteArrayElements( keyBegin, (jbyte *)begin_barr, JNI_ABORT ); + jenv->ReleaseByteArrayElements( keyEnd, (jbyte *)end_barr, JNI_ABORT ); + + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + } +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDB_Select_1API_1version(JNIEnv *jenv, jclass, jint version) { + fdb_error_t err = fdb_select_api_version( (int)version ); + if( err ) { + if( err == 2203 ) { + int maxSupportedVersion = fdb_get_max_api_version(); + + char errorStr[1024]; + if(FDB_API_VERSION > maxSupportedVersion) { + snprintf(errorStr, sizeof(errorStr), "This version of the FoundationDB Java binding is not supported by the installed " + "FoundationDB C library. The binding requires a library that supports API version " + "%d, but the installed library supports a maximum version of %d.", + FDB_API_VERSION, maxSupportedVersion); + } + else { + snprintf(errorStr, sizeof(errorStr), "API version %d is not supported by the installed FoundationDB C library.", version); + } + + safeThrow( jenv, getThrowable( jenv, err, errorStr ) ); + } + else { + safeThrow( jenv, getThrowable( jenv, err ) ); + } + } +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDB_Network_1setOption(JNIEnv *jenv, jobject, jint code, jbyteArray value) { + uint8_t *barr = NULL; + int size = 0; + if(value != 0) { + barr = (uint8_t *)jenv->GetByteArrayElements( value, NULL ); + if (!barr) { + if( !jenv->ExceptionOccurred() ) + throwRuntimeEx( jenv, "Error getting handle to native resources" ); + return; + } + size = jenv->GetArrayLength( value ); + } + fdb_error_t err = fdb_network_set_option((FDBNetworkOption)code, barr, size); + if(value != 0) + jenv->ReleaseByteArrayElements( value, (jbyte *)barr, JNI_ABORT ); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + } +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDB_Network_1setup(JNIEnv *jenv, jobject) { + fdb_error_t err = fdb_setup_network(); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + } +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDB_Network_1run(JNIEnv *jenv, jobject) { + // initialize things for the callbacks on the network thread + g_thread_jenv = jenv; + if( !g_IFutureCallback_call_methodID ) { + if( !findCallbackMethods( jenv ) ) + return; + } + + fdb_error_t err = fdb_run_network(); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + } +} + +JNIEXPORT void JNICALL Java_com_apple_cie_foundationdb_FDB_Network_1stop(JNIEnv *jenv, jobject) { + fdb_error_t err = fdb_stop_network(); + if( err ) { + safeThrow( jenv, getThrowable( jenv, err ) ); + } +} + +#ifdef __cplusplus +} +#endif + diff --git a/bindings/java/fdb_java.vcxproj b/bindings/java/fdb_java.vcxproj new file mode 100644 index 0000000000..00ee28b5aa --- /dev/null +++ b/bindings/java/fdb_java.vcxproj @@ -0,0 +1,124 @@ + + + + + -PRERELEASE + + + FDB_CLEAN_BUILD;%(PreprocessorDefinitions) + + + + + + Debug + x64 + + + Release + x64 + + + + {9617584C-22E8-4272-934F-733F378BF6AE} + java + + + + DynamicLibrary + true + MultiByte + v140_xp + + + DynamicLibrary + false + true + MultiByte + v140_xp + + + + + + + + + + ..\..\;C:\Program Files\Java\jdk6\include\win32;C:\Program Files\Java\jdk6\include;C:\Program Files\boost_1_52_0;$(IncludePath) + $(SolutionDir)bin\$(Configuration)\ + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + + + + echo "Removing java jars from directory: $(OutDir)" +if exist "$(OutDir)fdb-java-*.jar" del "$(OutDir)fdb-java-*.jar" + + + rmdir /S /Q classes +mkdir classes\main +mkdir classes\test +"C:\Program Files\Java\jdk6\bin\javac.exe" -source 1.6 -target 1.6 -d classes\main src\main\com\apple\cie\foundationdb\*.java src\main\com\apple\cie\foundationdb\async\*.java src\main\com\apple\cie\foundationdb\tuple\*.java src\main\com\apple\cie\foundationdb\directory\*.java src\main\com\apple\cie\foundationdb\subspace\*.java +"C:\Program Files\Java\jdk6\bin\javac.exe" -source 1.6 -target 1.6 -cp classes\main -d classes\test src\test\com\apple\cie\foundationdb\test\*.java +mkdir classes\main\lib\windows\amd64 +copy "$(TargetPath)" "classes\main\lib\windows\amd64" +"C:\Program Files\Java\jdk6\bin\jar.exe" cf "$(OutDir)fdb-java-$(Version)$(PreReleaseDecoration)-windows-$(Platform).jar" -C classes\main com\apple\cie\foundationdb -C classes\main lib\windows\amd64 +"C:\Program Files\Java\jdk6\bin\jar.exe" cf "$(OutDir)foundationdb-tests.jar" -C classes\test com\apple\cie\foundationdb +FOR /F "tokens=1" %%i in ('hg.exe id') do copy /Y "$(TargetPath)" "$(TargetPath)-%%i" + + + true + $(SolutionDir)bin\$(Configuration)\fdb_c.lib;%(AdditionalDependencies) + + + + + Level3 + Disabled + %(AdditionalIncludeDirectories);$(SolutionDir)bindings\c + WIN32;_WIN32_WINNT=_WIN32_WINNT_WS03;WINVER=_WIN32_WINNT_WS03;NTDDI_VERSION=NTDDI_WS03;_DEBUG;_HAS_ITERATOR_DEBUGGING=0;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebug + /bigobj "@$(SolutionDir)flow/no_intellisense.opt" %(AdditionalOptions) + + + Windows + + + + + Level3 + MaxSpeed + true + true + %(AdditionalIncludeDirectories);$(SolutionDir)bindings\c + WIN32;_WIN32_WINNT=_WIN32_WINNT_WS03;WINVER=_WIN32_WINNT_WS03;NTDDI_VERSION=NTDDI_WS03;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreaded + /bigobj "@$(SolutionDir)flow/no_intellisense.opt" %(AdditionalOptions) + + + true + true + Windows + + + + + + + + + + + + + + false + + + false + + + + + + \ No newline at end of file diff --git a/bindings/java/local.mk b/bindings/java/local.mk new file mode 100644 index 0000000000..83636de36d --- /dev/null +++ b/bindings/java/local.mk @@ -0,0 +1,224 @@ +# +# local.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile; -*- + +fdb_java_LDFLAGS := -Llib +fdb_java_CFLAGS := $(fdbclient_CFLAGS) -Ibindings/c + +# We only override if the environment didn't set it (this is used by +# the fdbwebsite documentation build process) +JAVADOC_DIR ?= bindings/java + +fdb_java_LIBS := lib/libfdb_c.$(DLEXT) +fdb_java_LDFLAGS += -Llib + +ifeq ($(RELEASE),true) + JARVER = $(VERSION) + APPLEJARVER = $(VERSION) +else + JARVER = $(VERSION)-PRERELEASE + APPLEJARVER = $(VERSION)-SNAPSHOT +endif + +define add_java_binding_targets + + JAVA$(1)_GENERATED_SOURCES := bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/NetworkOptions.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/ClusterOptions.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/DatabaseOptions.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/TransactionOptions.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/StreamingMode.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/ConflictRangeType.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/MutationType.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/FDBException.java + + JAVA$(1)_SOURCES := $$(JAVA$(1)_GENERATED_SOURCES) bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/*.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/async/*.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/tuple/*.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/directory/*.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/subspace/*.java bindings/java/src$(1)/test/com/apple/apple/foundationdbdb/test/*.java + + fdb_java$(1): bindings/java/foundationdb-client$(1).jar bindings/java/foundationdb-tests$(1).jar + + bindings/java/foundationdb-tests$(1).jar: bindings/java/.classstamp$(1) + @echo "Building $$@" + @jar cf $$@ -C bindings/java/classes$(1)/test com/apple/apple/foundationdbdb + + bindings/java/foundationdb-client$(1).jar: bindings/java/.classstamp$(1) lib/libfdb_java.$(DLEXT) + @echo "Building $$@" + @rm -rf bindings/java/classes$(1)/main/lib/$$(PLATFORM)/$$(java_ARCH) + @mkdir -p bindings/java/classes$(1)/main/lib/$$(PLATFORM)/$$(java_ARCH) + @cp lib/libfdb_java.$$(DLEXT) bindings/java/classes$(1)/main/lib/$$(PLATFORM)/$$(java_ARCH)/libfdb_java.$$(java_DLEXT) + @jar cf $$@ -C bindings/java/classes$(1)/main com/apple/apple/foundationdbdb -C bindings/java/classes$(1)/main lib + + fdb_java$(1)_jar_clean: + @rm -rf $$(JAVA$(1)_GENERATED_SOURCES) + @rm -rf bindings/java/classes$(1) + @rm -f bindings/java/foundationdb-client$(1).jar bindings/java/foundationdb-tests$(1).jar bindings/java/.classstamp$(1) + + # Redefinition of a target already defined in generated.mk, but it's "okay" and the way things were done before. + fdb_java_clean: fdb_java$(1)_jar_clean + + bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/StreamingMode.java: bin/vexillographer.exe fdbclient/vexillographer/fdb.options + @echo "Building Java options" + @$$(MONO) bin/vexillographer.exe fdbclient/vexillographer/fdb.options java $$(@D) + + bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/MutationType.java: bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/StreamingMode.java + @true + + bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/ConflictRangeType.java: bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/StreamingMode.java + @true + + bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/FDBException.java: bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/StreamingMode.java + @true + + bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/%Options.java: bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/StreamingMode.java + @true + + bindings/java/src$(1)/main/overview.html: bindings/java/src$(1)/main/overview.html.in $$(ALL_MAKEFILES) versions.target + @m4 -DVERSION=$$(VERSION) $$< > $$@ + + bindings/java/.classstamp$(1): $$(JAVA$(1)_SOURCES) + @echo "Compiling Java$(1) source" + @rm -rf bindings/java/classes$(1) + @mkdir -p bindings/java/classes$(1)/main + @mkdir -p bindings/java/classes$(1)/test + @$$(JAVAC) $$(JAVA$(1)FLAGS) -d bindings/java/classes$(1)/main bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/*.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/async/*.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/tuple/*.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/directory/*.java bindings/java/src$(1)/main/com/apple/apple/foundationdbdb/subspace/*.java + @$$(JAVAC) $$(JAVA$(1)FLAGS) -cp bindings/java/classes$(1)/main -d bindings/java/classes$(1)/test bindings/java/src$(1)/test/com/apple/apple/foundationdbdb/test/*.java + @echo timestamp > bindings/java/.classstamp$(1) + + javadoc$(1): $$(JAVA$(1)_SOURCES) bindings/java/src$(1)/main/overview.html + @echo "Generating Javadocs" + @mkdir -p $$(JAVADOC_DIR)/javadoc$(1)/ + @javadoc -quiet -public -notimestamp -source 1.8 -sourcepath bindings/java/src$(1)/main \ + -overview bindings/java/src$(1)/main/overview.html -d $$(JAVADOC_DIR)/javadoc$(1)/ \ + -windowtitle "FoundationDB Java Client API" \ + -doctitle "FoundationDB Java Client API" \ + -link "http://docs.oracle.com/javase/8/docs/api" \ + com.apple.cie.foundationdb.org.apple.cie.foundationdb.async com.apple.cie.foundationdb.tuple com.apple.cie.foundationdb.directory com.apple.cie.foundationdb.subspace + + javadoc$(1)_clean: + @rm -rf $$(JAVADOC_DIR)/javadoc$(1) + @rm bindings/java/src$(1)/main/overview.html + + ifeq ($$(PLATFORM),linux) + # We only need javadoc from one source + TARGETS += javadoc$(1) + CLEAN_TARGETS += javadoc$(1)_clean + + # _release builds the lib on OS X and the jars (including the OS X lib) on Linux + TARGETS += fdb_java$(1)_release + CLEAN_TARGETS += fdb_java$(1)_release_clean + + ifneq ($$(FATJAR),) + packages/fdb-java$(1)-$$(JARVER).jar: $$(MAC_OBJ_JAVA) $$(WINDOWS_OBJ_JAVA) + endif + + bindings/java/pom$(1).xml: bindings/java/pom.xml.in $$(ALL_MAKEFILES) versions.target + @echo "Generating $$@" + @m4 -DVERSION=$$(JARVER) -DNAME=fdb-java$(1) $$< > $$@ + + bindings/java/fdb-java$(1)-$(APPLEJARVER).pom: bindings/java/pom$(1).xml + @echo "Copying $$@" + sed -e 's/-PRERELEASE/-SNAPSHOT/g' bindings/java/pom$(1).xml > "$$@" + + packages/fdb-java$(1)-$$(JARVER).jar: fdb_java$(1) versions.target + @echo "Building $$@" + @rm -f $$@ + @rm -rf packages/jar$(1)_regular + @mkdir -p packages/jar$(1)_regular + @cd packages/jar$(1)_regular && unzip -qq $$(TOPDIR)/bindings/java/foundationdb-client$(1).jar + ifneq ($$(FATJAR),) + @mkdir -p packages/jar$(1)_regular/lib/windows/amd64 + @mkdir -p packages/jar$(1)_regular/lib/osx/x86_64 + @cp $$(MAC_OBJ_JAVA) packages/jar$(1)_regular/lib/osx/x86_64/libfdb_java.jnilib + @cp $$(WINDOWS_OBJ_JAVA) packages/jar$(1)_regular/lib/windows/amd64/fdb_java.dll + endif + @cd packages/jar$(1)_regular && jar cf $$(TOPDIR)/$$@ * + @rm -r packages/jar$(1)_regular + @cd bindings && jar uf $$(TOPDIR)/$$@ ../LICENSE + + packages/fdb-java$(1)-$$(JARVER)-sources.jar: $$(JAVA$(1)_GENERATED_SOURCES) versions.target + @echo "Building $$@" + @rm -f $$@ + @jar cf $(TOPDIR)/$$@ -C bindings/java/src$(1)/main com/apple/apple/foundationdbdb + + packages/fdb-java$(1)-$$(JARVER)-javadoc.jar: javadoc$(1) versions.target + @echo "Building $$@" + @rm -f $$@ + @cd $$(JAVADOC_DIR)/javadoc$(1)/ && jar cf $$(TOPDIR)/$$@ * + @cd bindings && jar uf $$(TOPDIR)/$$@ ../LICENSE + + packages/fdb-java$(1)-$$(JARVER)-bundle.jar: packages/fdb-java$(1)-$$(JARVER).jar packages/fdb-java$(1)-$$(JARVER)-javadoc.jar packages/fdb-java$(1)-$$(JARVER)-sources.jar bindings/java/pom$(1).xml bindings/java/fdb-java$(1)-$$(APPLEJARVER).pom versions.target + @echo "Building $$@" + @rm -f $$@ + @rm -rf packages/bundle$(1)_regular + @mkdir -p packages/bundle$(1)_regular + @cp packages/fdb-java$(1)-$$(JARVER).jar packages/fdb-java$(1)-$$(JARVER)-javadoc.jar packages/fdb-java$(1)-$$(JARVER)-sources.jar bindings/java/fdb-java$(1)-$$(APPLEJARVER).pom packages/bundle$(1)_regular + @cp bindings/java/pom$(1).xml packages/bundle$(1)_regular/pom.xml + @cd packages/bundle$(1)_regular && jar cf $(TOPDIR)/$$@ * + @rm -rf packages/bundle$(1)_regular + + fdb_java$(1)_release: packages/fdb-java$(1)-$$(JARVER)-bundle.jar + + fdb_java$(1)_release_clean: + @echo "Cleaning Java release" + @rm -f packages/fdb-java$(1)-*.jar packages/fdb-java$(1)-*-sources.jar bindings/java/pom$(1).xml bindings/java/fdb-java$(1)-$$(APPLEJARVER).pom + + endif + +endef + +$(eval $(call add_java_binding_targets,)) +ifeq ($(JAVAVERMAJOR).$(JAVAVERMINOR),1.8) + $(eval $(call add_java_binding_targets,-completable)) +endif + +ifeq ($(PLATFORM),linux) + + fdb_java_CFLAGS += -I/usr/lib/jvm/java-8-openjdk-amd64/include -I/usr/lib/jvm/java-8-openjdk-amd64/include/linux + fdb_java_LDFLAGS += -static-libgcc + + # Linux is where we build all the java packages + packages: fdb_java_release fdb_java-completable_release + packages_clean: fdb_java_release_clean fdb_java-completable_release_clean + + java_ARCH := amd64 + + ifneq ($(FATJAR),) + MAC_OBJ_JAVA := lib/libfdb_java.jnilib-$(VERSION_ID) + WINDOWS_OBJ_JAVA := lib/fdb_java.dll-$(VERSION_ID) + endif + +else ifeq ($(PLATFORM),osx) + TARGETS += fdb_java_release + CLEAN_TARGETS += fdb_java_release_clean + java_ARCH := x86_64 + + fdb_java_release: lib/libfdb_java.$(DLEXT) + @mkdir -p lib + @rm -f lib/libfdb_java.$(java_DLEXT)-* + @cp lib/libfdb_java.$(DLEXT) lib/libfdb_java.$(java_DLEXT)-$(VERSION_ID) + @cp lib/libfdb_java.$(DLEXT)-debug lib/libfdb_java.$(java_DLEXT)-debug-$(VERSION_ID) + + fdb_java_release_clean: + @rm -f lib/libfdb_java.$(DLEXT)-* + @rm -f lib/libfdb_java.$(java_DLEXT)-* + + # FIXME: Surely there is a better way to grab the JNI headers on any version of OS X. + fdb_java_CFLAGS += -I/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/JavaVM.framework/Versions/A/Headers + + # OS X needs to put its java lib in packages + packages: fdb_java_lib_package + + fdb_java_lib_package: fdb_java_release + mkdir -p packages + cp lib/libfdb_java.$(java_DLEXT)-$(VERSION_ID) packages + cp lib/libfdb_java.$(java_DLEXT)-debug-$(VERSION_ID) packages +endif diff --git a/bindings/java/pom.xml.in b/bindings/java/pom.xml.in new file mode 100644 index 0000000000..86ff1e266d --- /dev/null +++ b/bindings/java/pom.xml.in @@ -0,0 +1,39 @@ + + 4.0.0 + + com.apple.cie.foundationdb + NAME + VERSION + jar + + foundationdb-java + Java bindings for the FoundationDB database. These bindings require the FoundationDB client, which is under a different license. The client can be obtained from https://files.foundationdb.org/fdb-c/. + 2010 + http://foundationdb.org + + + FoundationDB + http://foundationdb.org + + + + + FoundationDB + + + + + http://0.0.0.0 + + + + + The Apache v2 License + http://www.apache.org/licenses/ + + + + diff --git a/bindings/java/src-completable/junit/com/apple/cie/foundationdb/tuple/AllTests.java b/bindings/java/src-completable/junit/com/apple/cie/foundationdb/tuple/AllTests.java new file mode 100644 index 0000000000..8be6513092 --- /dev/null +++ b/bindings/java/src-completable/junit/com/apple/cie/foundationdb/tuple/AllTests.java @@ -0,0 +1,31 @@ +/* + * AllTests.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.tuple; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ ArrayUtilTests.class }) +public class AllTests { + +} diff --git a/bindings/java/src-completable/junit/com/apple/cie/foundationdb/tuple/ArrayUtilTests.java b/bindings/java/src-completable/junit/com/apple/cie/foundationdb/tuple/ArrayUtilTests.java new file mode 100644 index 0000000000..a984a6d9ef --- /dev/null +++ b/bindings/java/src-completable/junit/com/apple/cie/foundationdb/tuple/ArrayUtilTests.java @@ -0,0 +1,308 @@ +/* + * ArrayUtilTests.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.tuple; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import org.junit.Test; + +/** + * @author Ben + * + */ +public class ArrayUtilTests { + + /** + * Test method for {@link ByteArrayUtil#join(byte[], java.util.List)}. + */ + @Test + public void testJoinByteArrayListOfbyte() { + byte[] a = new byte[] {'a', 'b', 'c'}; + byte[] b = new byte[] {'d', 'e', 'f'}; + + List parts = new ArrayList(); + parts.add(a); + parts.add(b); + parts.add(new byte[] {}); + byte[] result = new byte[] {'a', 'b', 'c', 'z', 'd', 'e', 'f', 'z'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(a); + parts.add(b); + result = new byte[] {'z', 'a', 'b', 'c', 'z', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(a); + parts.add(b); + result = new byte[] {'z', 'z', 'a', 'b', 'c', 'z', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(a); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(b); + result = new byte[] {'a', 'b', 'c', 'z', 'z', 'z', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(a); + parts.add(b); + parts.add(new byte[] {'b'}); + result = new byte[] {'a', 'b', 'c', 'z', 'd', 'e', 'f', 'z', 'b'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + result = new byte[] {'z', 'z'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + result = new byte[] {}; + assertArrayEquals(result, ByteArrayUtil.join(null, parts)); + } + + /** + * Test method for {@link ByteArrayUtil#join(byte[][])}. + */ + @Test + public void testJoinByteArrayArray() { + byte[] a = new byte[] {'a', 'b', 'c'}; + byte[] b = new byte[] {'d', 'e', 'f'}; + + List parts = new ArrayList(); + parts.add(a); + parts.add(b); + parts.add(new byte[] {}); + byte[] result = new byte[] {'a', 'b', 'c', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.join(parts.toArray(new byte[][]{}))); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(a); + parts.add(b); + result = new byte[] {'a', 'b', 'c', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.join(parts.toArray(new byte[][]{}))); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(a); + parts.add(b); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {'b'}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + result = new byte[] {'a', 'b', 'c', 'd', 'e', 'f', 'b'}; + assertArrayEquals(result, ByteArrayUtil.join(parts.toArray(new byte[][]{}))); + + parts = new ArrayList(); + parts.add(a); + parts.add(b); + parts.add(new byte[] {'b'}); + result = new byte[] {'a', 'b', 'c', 'd', 'e', 'f', 'b'}; + assertArrayEquals(result, ByteArrayUtil.join(parts.toArray(new byte[][]{}))); + + // Self-referential, with conversion to array + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(a); + parts.add(b); + parts.add(new byte[] {}); + assertArrayEquals(ByteArrayUtil.join(a, b), ByteArrayUtil.join(parts.toArray(new byte[][]{}))); + + // Test exception on null elements + boolean isError = false; + try { + ByteArrayUtil.join(a, b, null); + } catch(Exception e) { + isError = true; + } finally { + assertTrue(isError); + } + } + + /** + * Test method for {@link ByteArrayUtil#regionEquals(byte[], int, byte[])}. + */ + @Test + public void testRegionEquals() { + byte[] src = new byte[] {'a', (byte)12, (byte)255, 'n', 'm', 'z', 'k'}; + assertTrue(ByteArrayUtil.regionEquals(src, 3, new byte[] { 'n', 'm' })); + + assertFalse(ByteArrayUtil.regionEquals(src, 2, new byte[] { 'n', 'm' })); + + assertTrue(ByteArrayUtil.regionEquals(null, 0, null)); + + assertFalse(ByteArrayUtil.regionEquals(src, 0, null)); + } + + /** + * Test method for {@link ByteArrayUtil#replace(byte[], byte[], byte[])}. + */ + @Test + public void testReplace() { + byte[] a = new byte[] {'a', 'b', 'c'}; + byte[] b = new byte[] {'d', 'e', 'f'}; + + byte[] src = ByteArrayUtil.join(a, b, a, b); + byte[] result = new byte[] {'z', 'd', 'e', 'f', 'z', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.replace(src, a, new byte[] {'z'})); + + src = ByteArrayUtil.join(a, b, a, b); + assertArrayEquals(ByteArrayUtil.join(b, b), ByteArrayUtil.replace(src, a, new byte[] {})); + + src = ByteArrayUtil.join(a, b, a, b); + assertArrayEquals(ByteArrayUtil.join(a, a), ByteArrayUtil.replace(src, b, new byte[] {})); + + src = ByteArrayUtil.join(a, a, a); + assertArrayEquals(new byte[] {}, ByteArrayUtil.replace(src, a, new byte[] {})); + } + + /** + * Test method for {@link ByteArrayUtil#split(byte[], byte[])}. + */ + @Test + public void testSplit() { + byte[] a = new byte[] {'a', 'b', 'c'}; + byte[] b = new byte[] {'d', 'e', 'f'}; + + byte[] src = ByteArrayUtil.join(a, b, a, b, a); + List parts = ByteArrayUtil.split(src, b); + assertEquals(parts.size(), 3); + for(byte[] p : parts) { + assertArrayEquals(a, p); + } + + src = ByteArrayUtil.join(b, a, b, a, b, a); + parts = ByteArrayUtil.split(src, b); + assertEquals(parts.size(), 4); + int counter = 0; + for(byte[] p : parts) { + if(counter++ == 0) + assertArrayEquals(new byte[]{}, p); + else + assertArrayEquals(a, p); + } + + src = ByteArrayUtil.join(a, b, a, b, a, b); + parts = ByteArrayUtil.split(src, b); + assertEquals(parts.size(), 4); + counter = 0; + for(byte[] p : parts) { + if(counter++ < 3) + assertArrayEquals(a, p); + else + assertArrayEquals(new byte[]{}, p); + } + + // Multiple ending delimiters + src = ByteArrayUtil.join(a, b, a, b, a, b, b, b); + parts = ByteArrayUtil.split(src, b); + assertEquals(parts.size(), 6); + counter = 0; + for(byte[] p : parts) { + if(counter++ < 3) + assertArrayEquals(a, p); + else + assertArrayEquals(new byte[]{}, p); + } + } + + /** + * Test method for {@link ByteArrayUtil#bisectLeft(java.math.BigInteger[], java.math.BigInteger)}. + */ + @Test + public void testBisectLeft() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#compareUnsigned(byte[], byte[])}. + */ + @Test + public void testCompare() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#findNext(byte[], byte, int)}. + */ + @Test + public void testFindNext() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#findTerminator(byte[], byte, byte, int)}. + */ + @Test + public void testFindTerminator() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#copyOfRange(byte[], int, int)}. + */ + @Test + public void testCopyOfRange() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#strinc(byte[])}. + */ + @Test + public void testStrinc() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#printable(byte[])}. + */ + @Test + public void testPrintable() { + fail("Not yet implemented"); + } + +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/Cluster.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/Cluster.java new file mode 100644 index 0000000000..60a5006a01 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/Cluster.java @@ -0,0 +1,101 @@ +/* + * Cluster.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.nio.charset.Charset; +import java.util.concurrent.Executor; + +/** + * The {@code Cluster} represents a connection to a physical set of cooperating machines + * running FoundationDB. A {@code Cluster} is opened with a reference to a cluster file. + */ +public class Cluster extends DefaultDisposableImpl implements Disposable { + private ClusterOptions options; + private final Executor executor; + + private static final Charset UTF8 = Charset.forName("UTF-8"); + + protected Cluster(long cPtr, Executor executor) { + super(cPtr); + this.executor = executor; + this.options = new ClusterOptions(new OptionConsumer() { + @Override + public void setOption(int code, byte[] parameter) { + pointerReadLock.lock(); + try { + Cluster_setOption(getPtr(), code, parameter); + } finally { + pointerReadLock.unlock(); + } + } + }); + } + + /** + * Returns a set of options that can be set on a {@code Cluster}. In the current version + * of the API, there are no options that can be set on a {@code Cluster}. + * + * @return a set of cluster-specific options affecting this {@code Cluster} + */ + public ClusterOptions options() { return options; } + + @Override + protected void finalize() throws Throwable { + dispose(); + super.finalize(); + } + + /** + * Creates a connection to a specific database on an FDB cluster. + * + * @return a {@code Future} that will be set to a {@code Database} upon + * successful connection. + */ + public Database openDatabase() throws FDBException { + return openDatabase(executor); + } + + /** + * Creates a connection to a specific database on an FDB cluster. + * + * @return a {@code Future} that will be set to a {@code Database} upon + * successful connection. + */ + public Database openDatabase(Executor e) throws FDBException { + FutureDatabase futureDatabase = null; + pointerReadLock.lock(); + try { + futureDatabase = new FutureDatabase(Cluster_createDatabase(getPtr(), "DB".getBytes(UTF8)), e); + } finally { + pointerReadLock.unlock(); + } + return futureDatabase.join(); + } + + @Override + protected void disposeInternal(long cPtr) { + Cluster_dispose(cPtr); + } + + private native void Cluster_dispose(long cPtr); + private native long Cluster_createDatabase(long cPtr, byte[] dbName); + private native void Cluster_setOption(long cPtr, int code, byte[] value) throws FDBException; +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/Database.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/Database.java new file mode 100644 index 0000000000..1026d04148 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/Database.java @@ -0,0 +1,213 @@ +/* + * Database.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.function.Function; + +/** + * A mutable, lexicographically ordered mapping from binary keys to binary values. + * A {@code Database} is stored on a FoundationDB {@link Cluster}. + * {@link Transaction}s are used to manipulate data within a single + * {@code Database} -- multiple, concurrent + * {@code Transaction}s on a {@code Database} enforce ACID properties.
    + *
    + * The simplest correct programs using FoundationDB will make use of the methods defined + * in the {@link TransactionContext} interface. When used on a {@code Database} these + * methods will call {@code Transaction#commit()} after user code has been + * executed. These methods will not return successfully until {@code commit()} has + * returned successfully. + * + */ +public interface Database extends Disposable, TransactionContext { + /** + * Creates a {@link Transaction} that operates on this {@code Database}.
    + *
    + * Note: Java transactions automatically set the {@link TransactionOptions#setUsedDuringCommitProtectionDisable} + * option. This is because the Java bindings disallow use of {@code Transaction} objects after either + * {@link Transaction#reset} or {@link Transaction#onError} is called. + * + * @return a newly created {@code Transaction} that reads from and writes to this {@code Database}. + */ + default Transaction createTransaction() { + return createTransaction(getExecutor()); + } + + /** + * Creates a {@link Transaction} that operates on this {@code Database} with the given {@link Executor} + * for asynchronous callbacks. + * + * @param e the {@link Executor} to use when executing asynchronous callbacks for the database + * @return a newly created {@code Transaction} that reads from and writes to this {@code Database}. + */ + Transaction createTransaction(Executor e); + + /** + * Returns a set of options that can be set on a {@code Database} + * + * @return a set of database-specific options affecting this {@code Database} + */ + DatabaseOptions options(); + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. + * {@link Function#apply(Object) apply(ReadTransaction)} will be called on the + * supplied {@link Function} until a non-retryable + * {@link FDBException} (or any {@code Throwable} other than an {@code FDBException}) + * is thrown. This call is blocking -- this + * method will not return until the {@code Function} has been called and completed without error.
    + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + */ + @Override + default T read(Function retryable) { + return read(retryable, getExecutor()); + } + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. Use + * this formulation of {@link #read(Function)} if one wants to set a custom {@link Executor} + * for the transaction when run. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + * + * @see #read(Function) + */ + T read(Function retryable, Executor e); + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. + * {@link Function#apply(Object) apply(ReadTransaction)} will be called on the + * supplied {@link Function} until a non-retryable + * {@link FDBException} (or any {@code Throwable} other than an {@code FDBException}) + * is thrown. This call is non-blocking -- this + * method will return immediately and with a {@link CompletableFuture} that will be + * set when the {@code Function} has been called and completed without error.
    + *
    + * Any errors encountered executing {@code retryable}, or received from the + * database, will be set on the returned {@code CompletableFuture}. + * + * @param retryable the block of logic to execute in a {@link ReadTransaction} against + * this database + */ + @Override + default CompletableFuture readAsync( + Function> retryable) { + return readAsync(retryable, getExecutor()); + } + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. + * Use this version of {@link #readAsync(Function)} if one wants to set a custom + * {@link Executor} for the transaction when run. + * + * @param retryable the block of logic to execute in a {@link ReadTransaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + * + * @see #readAsync(Function) + */ + CompletableFuture readAsync( + Function> retryable, Executor e); + + /** + * Runs a transactional function against this {@code Database} with retry logic. + * {@link Function#apply(Object) apply(Transaction)} will be called on the + * supplied {@link Function} until a non-retryable + * {@link FDBException} (or any {@code Throwable} other than an {@code FDBException}) + * is thrown or {@link Transaction#commit() commit()}, + * when called after {@code apply()}, returns success. This call is blocking -- this + * method will not return until {@code commit()} has been called and returned success.
    + *
    + * As with other client/server databases, in some failure scenarios a client may + * be unable to determine whether a transaction succeeded. In these cases, your + * transaction may be executed twice. For more information about how to reason + * about these situations see + * the FounationDB Developer Guide + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + */ + @Override + default T run(Function retryable) { + return run(retryable, getExecutor()); + } + + /** + * Runs a transactional function against this {@code Database} with retry logic. + * Use this formulation of {@link #run(Function)} if one would like to set a + * custom {@link Executor} for the transaction when run. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + */ + T run(Function retryable, Executor e); + + /** + * Runs a transactional function against this {@code Database} with retry logic. + * {@link Function#apply(Object) apply(Transaction)} will be called on the + * supplied {@link Function} until a non-retryable + * {@link FDBException} (or any {@code Throwable} other than an {@code FDBException}) + * is thrown or {@link Transaction#commit() commit()}, + * when called after {@code apply()}, returns success. This call is non-blocking -- this + * method will return immediately and with a {@link CompletableFuture} that will be + * set when {@code commit()} has been called and returned success.
    + *
    + * As with other client/server databases, in some failure scenarios a client may + * be unable to determine whether a transaction succeeded. In these cases, your + * transaction may be executed twice. For more information about how to reason + * about these situations see + * the FounationDB Developer Guide
    + *
    + * Any errors encountered executing {@code retryable}, or received from the + * database, will be set on the returned {@code CompletableFuture}. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + */ + @Override + default CompletableFuture runAsync( + Function> retryable) { + return runAsync(retryable, getExecutor()); + } + + /** + * Runs a transactional function against this {@code Database} with retry logic. Use + * this formulation of the non-blocking {@link #runAsync(Function)} if one wants + * to set a custom {@link Executor} for the transaction when run. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + * + * @see #run(Function) + */ + CompletableFuture runAsync( + Function> retryable, Executor e); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/DefaultDisposableImpl.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/DefaultDisposableImpl.java new file mode 100644 index 0000000000..c7c0464447 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/DefaultDisposableImpl.java @@ -0,0 +1,82 @@ +/* + * DefaultDisposableImpl.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +abstract class DefaultDisposableImpl implements Disposable { + private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); + protected final Lock pointerReadLock = rwl.readLock(); + + private boolean disposed = false; + private long cPtr; + + public DefaultDisposableImpl() { + } + + public DefaultDisposableImpl(long cPtr) { + this.cPtr = cPtr; + if(this.cPtr == 0) + this.disposed = true; + } + + public boolean isDisposed() { + // we must have a read lock for this function to make sense, however it + // does not make sense to take the lock here, since the code that uses + // the result must inherently have the read lock itself. + assert( rwl.getReadHoldCount() > 0 ); + + return disposed; + } + + @Override + public void dispose() { + rwl.writeLock().lock(); + long ptr = 0; + try { + if(disposed) + return; + + ptr = cPtr; + this.cPtr = 0; + disposed = true; + } finally { + rwl.writeLock().unlock(); + } + + disposeInternal(ptr); + } + + protected long getPtr() { + // we must have a read lock for this function to make sense, however it + // does not make sense to take the lock here, since the code that uses + // the result must inherently have the read lock itself. + assert( rwl.getReadHoldCount() > 0 ); + + if(this.disposed) + throw new IllegalStateException("Cannot access disposed object"); + + return this.cPtr; + } + + protected abstract void disposeInternal(long cPtr); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/Disposable.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/Disposable.java new file mode 100644 index 0000000000..b1e211c013 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/Disposable.java @@ -0,0 +1,33 @@ +/* + * Disposable.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +/** + * A FoundationDB object with native resources that can be freed. It is not mandatory to call + * {@link Disposable#dispose()} most of the time, as disposal will happen at finalization. + */ +public interface Disposable { + /** + * Dispose of the object. This can be called multiple times, but care should be + * taken that an object is not in use in another thread at the time of the call. + */ + void dispose(); +} \ No newline at end of file diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FDB.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FDB.java new file mode 100644 index 0000000000..ac0b9a560e --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FDB.java @@ -0,0 +1,411 @@ +/* + * FDB.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Semaphore; +import java.util.concurrent.ThreadFactory; + +/** + * The starting point for accessing FoundationDB. + *
    + *

    Setting API version

    + * The FoundationDB API is accessed with a call to {@link #selectAPIVersion(int)}. + * This call is required before using any other part of the API. The call allows + * an error to be thrown at this point to prevent client code from accessing a later library + * with incorrect assumptions from the current version. The API version documented here is version + * {@code 500}.

    + * FoundationDB encapsulates multiple versions of its interface by requiring + * the client to explicitly specify the version of the API it uses. The purpose + * of this design is to allow you to upgrade the server, client libraries, or + * bindings without having to modify client code. The client libraries support + * all previous versions of the API. The API version specified by the client is + * used to control the behavior of the binding. You can therefore upgrade to + * more recent packages (and thus receive various improvements) without having + * to change your code.

    + * Warning: When using the multi-version client API, setting an API version that + * is not supported by a particular client library will prevent that client from + * being used to connect to the cluster. In particular, you should not advance + * the API version of your application after upgrading your client until the + * cluster has also been upgraded.
    + *

    Getting a database

    + * Once the API version has been set, the easiest way to get a {@link Database} object to use is + * to call {@link #open}. + *
    + *

    Client networking

    + * The network is started either implicitly with a call to a variant of {@link #open()} or + * {@link #createCluster()}, or started explicitly with a call to {@link #startNetwork()}. + *
    + * + */ +public class FDB { + static FDB singleton = null; + + static class DaemonThreadFactory implements ThreadFactory { + private final ThreadFactory factory; + + public DaemonThreadFactory(ThreadFactory factory) { + this.factory = factory; + } + + @Override + public Thread newThread(Runnable r) { + Thread t = factory.newThread(r); + t.setDaemon(true); + return t; + } + } + + public static final ExecutorService DEFAULT_EXECUTOR; + + final int apiVersion; + private volatile boolean netStarted = false; + private volatile boolean netStopped = false; + final private Semaphore netRunning = new Semaphore(1); + private final NetworkOptions options; + + static { + try { + JNIUtil.loadLibrary("fdb_c"); + } catch (Throwable t) { + // EAT: this can be useful for loading on windows + } + JNIUtil.loadLibrary("fdb_java"); + + ThreadFactory factory = new DaemonThreadFactory(Executors.defaultThreadFactory()); + DEFAULT_EXECUTOR = Executors.newCachedThreadPool(factory); + } + + /** + * Called only once to create the FDB singleton. + */ + private FDB(int apiVersion) { + this.apiVersion = apiVersion; + + options = new NetworkOptions(new OptionConsumer() { + @Override + public void setOption(int code, byte[] parameter) { + Network_setOption(code, parameter); + } + }); + + Runtime.getRuntime().addShutdownHook(new Thread( + new Runnable(){ + @Override + public void run() { + FDB.this.stopNetwork(); + } + } + )); + } + + /** + * Returns a set of options that can be set on a the FoundationDB API. Generally, + * these options to the top level of the API affect the networking engine and + * therefore must be set before the network engine is started. The network is started + * by calls to {@link #startNetwork()} and implicitly by calls to {@link #open()} and + * {@link #createCluster()} (and their respective variants). + * + * @return a set of options affecting this instance of the FoundationDB API + */ + public NetworkOptions options() { return options; } + + /** + * Select the version for the client API. An exception will be thrown if the + * requested version is not supported by this implementation of the API. As + * only one version can be selected for the lifetime of the JVM, the result + * of a successful call to this method is always the same instance of a FDB + * object.

    + * + * Warning: When using the multi-version client API, setting an API version that + * is not supported by a particular client library will prevent that client from + * being used to connect to the cluster. In particular, you should not advance + * the API version of your application after upgrading your client until the + * cluster has also been upgraded. + * + * @param version the API version required + * + * @return the FoundationDB API object + */ + public synchronized static FDB selectAPIVersion(final int version) throws FDBException { + if(singleton != null) { + if(version != singleton.apiVersion) { + throw new IllegalArgumentException( + "FoundationDB API already started at different version"); + } + return singleton; + } + if(version < 500) + throw new IllegalArgumentException("API version not supported (minimum 500)"); + if(version > 500) + throw new IllegalArgumentException("API version not supported (maximum 500)"); + Select_API_version(version); + return singleton = new FDB(version); + } + + /** + * Connects to the cluster specified by the + * default fdb.cluster file. + * If the FoundationDB network has not been started, it will be started in the course of this call + * as if {@link FDB#startNetwork()} had been called. + * + * @return a {@code CompletableFuture} that will be set to a FoundationDB {@code Cluster}. + * + * @throws FDBException on errors encountered starting the FoundationDB networking engine + * @throws IllegalStateException if the network had been previously stopped + */ + public Cluster createCluster() throws IllegalStateException, FDBException { + return createCluster(null); + } + + /** + * Connects to the cluster specified by {@code clusterFilePath}. If the FoundationDB network + * has not been started, it will be started in the course of this call as if + * {@link #startNetwork()} had been called. + * + * @param clusterFilePath the + * cluster file + * defining the FoundationDB cluster. This can be {@code null} if the + * default fdb.cluster file + * is to be used. + * + * @return a {@code CompletableFuture} that will be set to a FoundationDB {@code Cluster}. + * + * @throws FDBException on errors encountered starting the FoundationDB networking engine + * @throws IllegalStateException if the network had been previously stopped + */ + public Cluster createCluster(String clusterFilePath) throws IllegalStateException, FDBException { + return createCluster(clusterFilePath, DEFAULT_EXECUTOR); + } + + /** + * Connects to the cluster specified by {@code clusterFilePath}. If the FoundationDB network + * has not been started, it will be started in the course of this call. The supplied + * {@link Executor} will be used as the default for the execution of all callbacks that + * are produced from using the resulting {@link Cluster}. + * + * @param clusterFilePath the + * cluster file + * defining the FoundationDB cluster. This can be {@code null} if the + * default fdb.cluster file + * is to be used. + * @param e used to run the FDB network thread + * + * @return a {@code CompletableFuture} that will be set to a FoundationDB {@code Cluster}. + * + * @throws FDBException on errors encountered starting the FoundationDB networking engine + * @throws IllegalStateException if the network had been previously stopped + */ + public Cluster createCluster(String clusterFilePath, Executor e) + throws FDBException, IllegalStateException { + FutureCluster f; + synchronized (this) { + if (!isConnected()) { + startNetwork(e); + } + f = new FutureCluster(Cluster_create(clusterFilePath), e); + } + return f.join(); + } + + /** + * Initializes networking, connects with the + * default fdb.cluster file, + * and opens the database. + * + * @return a {@code CompletableFuture} that will be set to a FoundationDB {@link Database} + */ + public Database open() throws FDBException { + return open(null); + } + + /** + * Initializes networking, connects to the cluster specified by {@code clusterFilePath} + * and opens the database. + * + * @param clusterFilePath the + * cluster file + * defining the FoundationDB cluster. This can be {@code null} if the + * default fdb.cluster file + * is to be used. + * + * @return a {@code CompletableFuture} that will be set to a FoundationDB {@link Database} + */ + public Database open(String clusterFilePath) throws FDBException { + return open(clusterFilePath, DEFAULT_EXECUTOR); + } + + /** + * Initializes networking, connects to the cluster specified by {@code clusterFilePath} + * and opens the database. + * + * @param clusterFilePath the + * cluster file + * defining the FoundationDB cluster. This can be {@code null} if the + * default fdb.cluster file + * is to be used. + * @param e the {@link Executor} to use to execute asynchronous callbacks + * + * @return a {@code CompletableFuture} that will be set to a FoundationDB {@link Database} + */ + public Database open(String clusterFilePath, Executor e) throws FDBException { + FutureCluster f; + synchronized (this) { + if (!isConnected()) { + startNetwork(); + } + f = new FutureCluster(Cluster_create(clusterFilePath), e); + } + Cluster c = f.join(); + return c.openDatabase(e); + } + + /** + * Initializes networking. Can only be called once. This version of + * {@code startNetwork()} will use the default thread pool to execute the + * FoundationDB network.
    + *
    + * Configuration of the networking engine can be achieved through calls to the methods + * in {@link NetworkOptions}. + * + * @throws IllegalStateException if the network has already been stopped + * + * @see NetworkOptions + */ + public void startNetwork() throws FDBException, IllegalStateException { + startNetwork(DEFAULT_EXECUTOR); + } + + /** + * Initializes networking. Can only be called once. The FoundationDB + * networking event loop will be run in the specified {@code Executor}. This + * event loop is a blocking operation that is not + * expected to terminate until the program in complete. This will therefore consume an + * entire thread from {@code e} if {@code e} is a thread pool or will completely block + * operation of a single threaded {@code Executor}.
    + *
    + * Manual configuration of the networking engine can be achieved through calls on + * {@link NetworkOptions}. These options should be set before a call + * to this method. + * + * @see NetworkOptions + * + * @throws IllegalStateException if the network has already been stopped + */ + public synchronized void startNetwork(Executor e) throws FDBException, IllegalStateException { + if(netStopped) + throw new IllegalStateException("Network has been stopped and cannot be restarted"); + if(netStarted) { + return; + } + Network_setup(); + netStarted = true; + + e.execute(new Runnable() { + @Override + public void run() { + boolean acquired = false; + try { + while(!acquired) { + try { + // make attempt to avoid a needless deadlock + synchronized (FDB.this) { + if(netStopped) { + return; + } + } + + netRunning.acquire(); + acquired = true; + } catch(InterruptedException e) {} + } + try { + Network_run(); + } catch (Throwable t) { + System.err.println("Unhandled error in FoundationDB network thread: " + t.getMessage()); + // eat this error. we have nowhere to send it. + } + } finally { + if(acquired) { + netRunning.release(); + } + synchronized (FDB.this) { + netStopped = true; + } + } + } + }); + } + + /** + * Gets the state of the FoundationDB networking thread. + * + * @return {@code true} if the FDB network thread is running, {@code false} otherwise. + */ + private synchronized boolean isConnected() { + return netStarted && !netStopped; + } + + /** + * Stops the FoundationDB networking engine. This can be called only once -- the network + * cannot be restarted after this call. This call blocks for the completion of + * the FoundationDB networking engine. + * + * @throws FDBException on errors while stopping the network + */ + public synchronized void stopNetwork() throws FDBException { + if(!netStarted || netStopped) { + netStopped = true; + return; + } + Network_stop(); + // set netStarted here in case the network has never really ever been run + netStopped = netStarted = true; + while(true) { + try { + // This will be released when runNetwork() returns. + // Taking this and never releasing it will also assure + // that we will never again be able to call runNetwork() + netRunning.acquire(); + return; + } catch (InterruptedException e) {} + } + } + + protected static boolean evalErrorPredicate(int predicate, int code) { + if(singleton == null) + throw new IllegalStateException("FDB API not yet initalized"); + return singleton.Error_predicate(predicate, code); + } + + static native void Select_API_version(int version) throws FDBException; + + private native void Network_setOption(int code, byte[] value) throws FDBException; + private native void Network_setup() throws FDBException; + private native void Network_run() throws FDBException; + private native void Network_stop() throws FDBException; + + private native boolean Error_predicate(int predicate, int code); + + private native long Cluster_create(String clusterFileName); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FDBDatabase.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FDBDatabase.java new file mode 100644 index 0000000000..25bbd1b426 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FDBDatabase.java @@ -0,0 +1,146 @@ +/* + * FDBDatabase.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.BiFunction; +import java.util.function.Function; + +import com.apple.cie.foundationdb.async.AsyncUtil; + +class FDBDatabase extends DefaultDisposableImpl implements Database, Disposable, OptionConsumer { + private DatabaseOptions options; + private Executor executor; + + protected FDBDatabase(long cPtr, Executor executor) { + super(cPtr); + this.executor = executor; + this.options = new DatabaseOptions(this); + } + + @Override + public DatabaseOptions options() { + return options; + } + + @Override + public T run(Function retryable, Executor e) { + Transaction t = this.createTransaction(); + try { + while (true) { + try { + T returnVal = retryable.apply(t); + t.commit().join(); + return returnVal; + } catch (RuntimeException err) { + t = t.onError(err).join(); + } + } + } finally { + t.dispose(); + } + } + + @Override + public T read(Function retryable, Executor e) { + return this.run(retryable); + } + + @Override + public CompletableFuture runAsync(final Function> retryable, Executor e) { + final AtomicReference trRef = new AtomicReference<>(createTransaction(e)); + final AtomicReference returnValue = new AtomicReference<>(); + return AsyncUtil.whileTrue(v -> { + CompletableFuture process = AsyncUtil.applySafely(retryable, trRef.get()); + + return process.thenComposeAsync(returnVal -> + trRef.get().commit().thenApply(o -> { + returnValue.set(returnVal); + return false; + }) + , e).handleAsync((value, t) -> { + if(t == null) + return CompletableFuture.completedFuture(value); + if(!(t instanceof RuntimeException)) + throw new CompletionException(t); + return trRef.get().onError(t).thenApply(newTr -> { + trRef.set(newTr); + return true; + }); + }, e).thenCompose(x -> x); + }, e).thenApply(o -> { + trRef.get().dispose(); + return returnValue.get(); + }); + } + + @Override + public CompletableFuture readAsync( + Function> retryable, Executor e) { + return this.runAsync(retryable, e); + } + + @Override + protected void finalize() throws Throwable { + dispose(); + super.finalize(); + } + + @Override + public Transaction createTransaction(Executor e) { + pointerReadLock.lock(); + try { + Transaction tr = new FDBTransaction(Database_createTransaction(getPtr()), this, e); + tr.options().setUsedDuringCommitProtectionDisable(); + return tr; + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public void setOption(int code, byte[] value) { + pointerReadLock.lock(); + try { + Database_setOption(getPtr(), code, value); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public Executor getExecutor() { + return executor; + } + + @Override + protected void disposeInternal(long cPtr) { + Database_dispose(cPtr); + } + + private native long Database_createTransaction(long cPtr); + private native void Database_dispose(long cPtr); + private native void Database_setOption(long cPtr, int code, byte[] value) throws FDBException; +} \ No newline at end of file diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FDBTransaction.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FDBTransaction.java new file mode 100644 index 0000000000..60f6b890f8 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FDBTransaction.java @@ -0,0 +1,603 @@ +/* + * FDBTransaction.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.function.Function; + +import com.apple.cie.foundationdb.async.*; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +class FDBTransaction extends DefaultDisposableImpl implements Disposable, Transaction, OptionConsumer { + private final Database database; + private final Executor executor; + private final TransactionOptions options; + + private boolean transactionOwner; + + public final ReadTransaction snapshot; + + class ReadSnapshot implements ReadTransaction { + @Override + public CompletableFuture getReadVersion() { + return FDBTransaction.this.getReadVersion(); + } + + @Override + public CompletableFuture get(byte[] key) { + return get_internal(key, true); + } + + @Override + public CompletableFuture getKey(KeySelector selector) { + return getKey_internal(selector, true); + } + + /////////////////// + // getRange -> KeySelectors + /////////////////// + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse, StreamingMode mode) { + return new RangeQuery(FDBTransaction.this, true, begin, end, limit, reverse, mode); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse) { + return getRange(begin, end, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit) { + return getRange(begin, end, limit, false); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end) { + return getRange(begin, end, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + /////////////////// + // getRange -> byte[]s + /////////////////// + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse, StreamingMode mode) { + return getRange(KeySelector.firstGreaterOrEqual(begin), + KeySelector.firstGreaterOrEqual(end), + limit, reverse, mode); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse) { + return getRange(begin, end, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit) { + return getRange(begin, end, limit, false); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end) { + return getRange(begin, end, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + /////////////////// + // getRange (Range) + /////////////////// + @Override + public AsyncIterable getRange(Range range, + int limit, boolean reverse, StreamingMode mode) { + return getRange(range.begin, range.end, limit, reverse, mode); + } + @Override + public AsyncIterable getRange(Range range, + int limit, boolean reverse) { + return getRange(range, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(Range range, + int limit) { + return getRange(range, limit, false); + } + @Override + public AsyncIterable getRange(Range range) { + return getRange(range, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + @Override + public TransactionOptions options() { + return FDBTransaction.this.options(); + } + + @Override + public T read(Function retryable) { + return retryable.apply(this); + } + + @Override + public CompletableFuture readAsync( + Function> retryable) { + return AsyncUtil.applySafely(retryable, this); + } + + @Override + public Executor getExecutor() { + return FDBTransaction.this.getExecutor(); + } + } + + protected FDBTransaction(long cPtr, Database database, Executor executor) { + super(cPtr); + this.database = database; + this.executor = executor; + snapshot = new ReadSnapshot(); + options = new TransactionOptions(this); + transactionOwner = true; + } + + @Override + public ReadTransaction snapshot() { + return snapshot; + } + + @Override + public TransactionOptions options() { + return options; + } + + @Override + public void setReadVersion(long version) { + pointerReadLock.lock(); + try { + Transaction_setVersion(getPtr(), version); + } finally { + pointerReadLock.unlock(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public CompletableFuture getReadVersion() { + pointerReadLock.lock(); + try { + return new FutureVersion( Transaction_getReadVersion(getPtr())); + } finally { + pointerReadLock.unlock(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public CompletableFuture get(byte[] key) { + return get_internal(key, false); + } + + private CompletableFuture get_internal(byte[] key, boolean isSnapshot) { + pointerReadLock.lock(); + try { + return new FutureResult( Transaction_get(getPtr(), key, isSnapshot)); + } finally { + pointerReadLock.unlock(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public CompletableFuture getKey(KeySelector selector) { + return getKey_internal(selector, false); + } + + private CompletableFuture getKey_internal(KeySelector selector, boolean isSnapshot) { + pointerReadLock.lock(); + try { + return new FutureKey( Transaction_getKey(getPtr(), + selector.getKey(), selector.orEqual(), selector.getOffset(), isSnapshot)); + } finally { + pointerReadLock.unlock(); + } + } + + /////////////////// + // getRange -> KeySelectors + /////////////////// + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse, StreamingMode mode) { + return new RangeQuery(this, false, begin, end, limit, reverse, mode); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse) { + return getRange(begin, end, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit) { + return getRange(begin, end, limit, false); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end) { + return getRange(begin, end, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + /////////////////// + // getRange -> byte[]s + /////////////////// + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse, StreamingMode mode) { + return getRange(KeySelector.firstGreaterOrEqual(begin), + KeySelector.firstGreaterOrEqual(end), + limit, reverse, mode); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse) { + return getRange(begin, end, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit) { + return getRange(begin, end, limit, false); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end) { + return getRange(begin, end, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + /////////////////// + // getRange (Range) + /////////////////// + @Override + public AsyncIterable getRange(Range range, + int limit, boolean reverse, StreamingMode mode) { + return getRange(range.begin, range.end, limit, reverse, mode); + } + @Override + public AsyncIterable getRange(Range range, + int limit, boolean reverse) { + return getRange(range, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(Range range, + int limit) { + return getRange(range, limit, false); + } + @Override + public AsyncIterable getRange(Range range) { + return getRange(range, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + @Override + public Database getDatabase() { + return database; + } + + protected FutureResults getRange_internal( + KeySelector begin, KeySelector end, + int rowLimit, int targetBytes, int streamingMode, + int iteration, boolean isSnapshot, boolean reverse) { + pointerReadLock.lock(); + try { + /*System.out.println(String.format( + " -- range get: (%s, %s) limit: %d, bytes: %d, mode: %d, iteration: %d, snap: %s, reverse %s", + begin.toString(), end.toString(), rowLimit, targetBytes, streamingMode, + iteration, Boolean.toString(isSnapshot), Boolean.toString(reverse)));*/ + return new FutureResults(Transaction_getRange( + getPtr(), begin.getKey(), begin.orEqual(), begin.getOffset(), + end.getKey(), end.orEqual(), end.getOffset(), rowLimit, targetBytes, + streamingMode, iteration, isSnapshot, reverse)); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public void addReadConflictRange(byte[] keyBegin, byte[] keyEnd) { + addConflictRange(keyBegin, keyEnd, ConflictRangeType.READ); + } + + @Override + public void addReadConflictKey(byte[] key) { + addConflictRange(key, ByteArrayUtil.join(key, new byte[]{(byte) 0}), ConflictRangeType.READ); + } + + @Override + public void addWriteConflictRange(byte[] keyBegin, byte[] keyEnd) { + addConflictRange(keyBegin, keyEnd, ConflictRangeType.WRITE); + } + + @Override + public void addWriteConflictKey(byte[] key) { + addConflictRange(key, ByteArrayUtil.join(key, new byte[] { (byte)0 }), ConflictRangeType.WRITE); + } + + private void addConflictRange(byte[] keyBegin, byte[] keyEnd, + ConflictRangeType type) { + pointerReadLock.lock(); + try { + Transaction_addConflictRange(getPtr(), keyBegin, keyEnd, type.code()); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public T run(Function retryable) { + return retryable.apply(this); + } + + @Override + public CompletableFuture runAsync( + Function> retryable) { + return AsyncUtil.applySafely(retryable, this); + } + + @Override + public T read(Function retryable) { + return retryable.apply(this); + } + + @Override + public CompletableFuture readAsync( + Function> retryable) { + return AsyncUtil.applySafely(retryable, this); + } + + @Override + public void set(byte[] key, byte[] value) { + if(key == null || value == null) + throw new IllegalArgumentException("Keys/Values must be non-null"); + pointerReadLock.lock(); + try { + Transaction_set(getPtr(), key, value); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public void clear(byte[] key) { + if(key == null) + throw new IllegalArgumentException("Key cannot be null"); + pointerReadLock.lock(); + try { + Transaction_clear(getPtr(), key); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public void clear(byte[] beginKey, byte[] endKey) { + if(beginKey == null || endKey == null) + throw new IllegalArgumentException("Keys cannot be null"); + pointerReadLock.lock(); + try { + Transaction_clear(getPtr(), beginKey, endKey); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + @Deprecated + public void clearRangeStartsWith(byte[] prefix) { + clear(Range.startsWith(prefix)); + } + + @Override + public void clear(Range range) { + clear(range.begin, range.end); + } + + @Override + public void mutate(MutationType optype, byte[] key, byte[] value) { + pointerReadLock.lock(); + try { + Transaction_mutate(getPtr(), optype.code(), key, value); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public void setOption(int code, byte[] param) { + pointerReadLock.lock(); + try { + Transaction_setOption(getPtr(), code, param); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public CompletableFuture commit() { + pointerReadLock.lock(); + try { + return new FutureVoid(Transaction_commit(getPtr())); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public Long getCommittedVersion() { + pointerReadLock.lock(); + try { + return Transaction_getCommittedVersion(getPtr()); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public CompletableFuture getVersionstamp() { + pointerReadLock.lock(); + try { + return new FutureKey(Transaction_getVersionstamp(getPtr())); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public CompletableFuture watch(byte[] key) throws FDBException { + pointerReadLock.lock(); + try { + return new FutureVoid(Transaction_watch(getPtr(), key)); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public CompletableFuture onError(Throwable e) { + if((e instanceof CompletionException || e instanceof ExecutionException) && e.getCause() != null) { + e = e.getCause(); + } + if(!(e instanceof FDBException)) { + CompletableFuture future = new CompletableFuture<>(); + future.completeExceptionally(e); + return future; + } + pointerReadLock.lock(); + try { + CompletableFuture f = new FutureVoid(Transaction_onError(getPtr(), ((FDBException)e).getCode())); + final Transaction tr = transfer(); + return f.thenApply(v -> tr) + .whenComplete((v, t) -> { + if(t != null) { + tr.dispose(); + } + }); + } finally { + pointerReadLock.unlock(); + if(!transactionOwner) { + dispose(); + } + } + } + + @Override + public Transaction reset() { + pointerReadLock.lock(); + try { + Transaction_reset(getPtr()); + return transfer(); + } finally { + pointerReadLock.unlock(); + if(!transactionOwner) { + dispose(); + } + } + } + + @Override + public void cancel() { + pointerReadLock.lock(); + try { + Transaction_cancel(getPtr()); + } finally { + pointerReadLock.unlock(); + } + } + + public CompletableFuture getAddressesForKey(byte[] key) { + pointerReadLock.lock(); + try { + return new FutureStrings(Transaction_getKeyLocations(getPtr(), key)); + } finally { + pointerReadLock.unlock(); + } + } + + // Must hold pointerReadLock when calling + private FDBTransaction transfer() { + FDBTransaction tr = new FDBTransaction(getPtr(), database, executor); + tr.options().setUsedDuringCommitProtectionDisable(); + transactionOwner = false; + return tr; + } + + @Override + protected long getPtr() { + if(!transactionOwner) { + throw new IllegalStateException("Transaction has been invalidated by reset"); + } + else { + return super.getPtr(); + } + } + + @Override + protected void finalize() throws Throwable { + dispose(); + } + + @Override + protected void disposeInternal(long cPtr) { + if(transactionOwner) { + Transaction_dispose(cPtr); + } + } + + @Override + public Executor getExecutor() { + return executor; + } + + private native long Transaction_getReadVersion(long cPtr); + private native void Transaction_setVersion(long cPtr, long version); + private native long Transaction_get(long cPtr, byte[] key, boolean isSnapshot); + private native long Transaction_getKey(long cPtr, byte[] key, boolean orEqual, + int offset, boolean isSnapshot); + private native long Transaction_getRange(long cPtr, + byte[] keyBegin, boolean orEqualBegin, int offsetBegin, + byte[] keyEnd, boolean orEqualEnd, int offsetEnd, + int rowLimit, int targetBytes, int streamingMode, int iteration, + boolean isSnapshot, boolean reverse); + private native void Transaction_addConflictRange(long cPtr, + byte[] keyBegin, byte[] keyEnd, int conflictRangeType); + private native void Transaction_set(long cPtr, byte[] key, byte[] value); + private native void Transaction_clear(long cPtr, byte[] key); + private native void Transaction_clear(long cPtr, byte[] beginKey, byte[] endKey); + private native void Transaction_mutate(long ptr, int code, byte[] key, byte[] value); + private native void Transaction_setOption(long cPtr, int code, byte[] value) throws FDBException; + private native long Transaction_commit(long cPtr); + private native long Transaction_getCommittedVersion(long cPtr); + private native long Transaction_getVersionstamp(long cPtr); + private native long Transaction_onError(long cPtr, int errorCode); + private native void Transaction_dispose(long cPtr); + private native void Transaction_reset(long cPtr); + private native long Transaction_watch(long ptr, byte[] key) throws FDBException; + private native void Transaction_cancel(long cPtr); + private native long Transaction_getKeyLocations(long cPtr, byte[] key); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureCluster.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureCluster.java new file mode 100644 index 0000000000..0d2f4d9650 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureCluster.java @@ -0,0 +1,40 @@ +/* + * FutureCluster.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + +class FutureCluster extends NativeFuture { + private final Executor executor; + + protected FutureCluster(long cPtr, Executor executor) { + super(cPtr); + this.executor = executor; + registerMarshalCallback(); + } + + @Override + public Cluster getIfDone_internal() throws FDBException { + return new Cluster(FutureCluster_get(cPtr), executor); + } + + private native long FutureCluster_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureDatabase.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureDatabase.java new file mode 100644 index 0000000000..a6d7561139 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureDatabase.java @@ -0,0 +1,40 @@ +/* + * FutureDatabase.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + +class FutureDatabase extends NativeFuture { + private final Executor executor; + + FutureDatabase(long cPtr, Executor executor) { + super(cPtr); + this.executor = executor; + registerMarshalCallback(); + } + + @Override + public Database getIfDone_internal() throws FDBException { + return new FDBDatabase(FutureDatabase_get(cPtr), executor); + } + + private native long FutureDatabase_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureKey.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureKey.java new file mode 100644 index 0000000000..de47f56ac8 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureKey.java @@ -0,0 +1,37 @@ +/* + * FutureKey.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + + + +class FutureKey extends NativeFuture { + FutureKey(long cPtr) { + super(cPtr); + registerMarshalCallback(); + } + + @Override + public byte[] getIfDone_internal() throws FDBException { + return FutureKey_get(cPtr); + } + + private native byte[] FutureKey_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureResult.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureResult.java new file mode 100644 index 0000000000..59e17244e6 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureResult.java @@ -0,0 +1,36 @@ +/* + * FutureResult.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + + +class FutureResult extends NativeFuture { + FutureResult(long cPtr) { + super(cPtr); + registerMarshalCallback(); + } + + @Override + public byte[] getIfDone_internal() throws FDBException { + return FutureResult_get(cPtr); + } + + private native byte[] FutureResult_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureResults.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureResults.java new file mode 100644 index 0000000000..9c45e3f05e --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureResults.java @@ -0,0 +1,56 @@ +/* + * FutureResults.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + + +class FutureResults extends NativeFuture { + FutureResults(long cPtr) { + super(cPtr); + registerMarshalCallback(); + } + + @Override + protected void postMarshal() { + // We can't dispose because this class actually marshals on-demand + } + + @Override + public RangeResultInfo getIfDone_internal() throws FDBException { + FDBException err = Future_getError(cPtr); + + if(!err.isSuccess()) { + throw err; + } + + return new RangeResultInfo(this); + } + + public RangeResultSummary getSummary() { + return FutureResults_getSummary(cPtr); + } + + public RangeResult getResults() { + return FutureResults_get(cPtr); + } + + private native RangeResultSummary FutureResults_getSummary(long ptr) throws FDBException; + private native RangeResult FutureResults_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureStrings.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureStrings.java new file mode 100644 index 0000000000..5fd3236fc7 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureStrings.java @@ -0,0 +1,36 @@ +/* + * FutureStrings.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + + +class FutureStrings extends NativeFuture { + FutureStrings(long cPtr) { + super(cPtr); + registerMarshalCallback(); + } + + @Override + public String[] getIfDone_internal() throws FDBException { + return FutureStrings_get(cPtr); + } + + private native String[] FutureStrings_get(long ptr) throws FDBException; +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureVersion.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureVersion.java new file mode 100644 index 0000000000..429aa4b368 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureVersion.java @@ -0,0 +1,36 @@ +/* + * FutureVersion.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + + +class FutureVersion extends NativeFuture { + FutureVersion(long cPtr) { + super(cPtr); + registerMarshalCallback(); + } + + @Override + Long getIfDone_internal() throws FDBException { + return FutureVersion_get(cPtr); + } + + private native long FutureVersion_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureVoid.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureVoid.java new file mode 100644 index 0000000000..414779f9ad --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/FutureVoid.java @@ -0,0 +1,41 @@ +/* + * FutureVoid.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + + +class FutureVoid extends NativeFuture { + FutureVoid(long cPtr) { + super(cPtr); + registerMarshalCallback(); + } + + @Override + public Void getIfDone_internal() throws FDBException { + // With "future-cleanup" we get rid of FutureVoid_get and replace instead + // with a get on the error and throw if the error is not success. + FDBException err = Future_getError(cPtr); + + if(!err.isSuccess()) { + throw err; + } + return null; + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/JNIUtil.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/JNIUtil.java new file mode 100644 index 0000000000..fd310c7df8 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/JNIUtil.java @@ -0,0 +1,182 @@ +/* + * JNIUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Utility for loading a dynamic library from the classpath. + * + */ +class JNIUtil { + private static final String SEPARATOR = "/"; + private static final String LOADABLE_PREFIX = "FDB_LIBRARY_PATH_"; + private static final String TEMPFILE_PREFIX = "fdbjni"; + private static final String TEMPFILE_SUFFIX = ".library"; + + private enum OS { + WIN32("windows", "amd64", false), + LINUX("linux", "amd64", true), + OSX("osx", "x86_64", true); + + private final String name; + private final String arch; + private final boolean canDeleteEager; + + OS(String name, String arch, boolean canDeleteEager) { + this.name = name; + this.arch = arch; + this.canDeleteEager = canDeleteEager; + } + + public String getName() { + return this.name; + } + + public String getArch() { + return this.arch; + } + } + + /** + * Attempts a platform specific load of a library using classpath resources. In + * the case that the load fails, a call to {@link System#loadLibrary(String)} + * will be made as a way to use a library not included in a jar. + * + * @param libName the name of the library to attempt to load. This name should be + * undecorated with file extensions and, in the case of *nix, "lib" prefixes. + */ + static void loadLibrary(String libName) throws UnsatisfiedLinkError { + if(libName == null) { + throw new NullPointerException("Library name must not be null"); + } + + String libPathToLoad = null; + try { + String prop = LOADABLE_PREFIX + libName.toUpperCase(); + libPathToLoad = System.getProperty(prop); + } catch(SecurityException e) { + // eat + } + + if(libPathToLoad != null) { + System.load(libPathToLoad); + return; + } + + OS os = getRunningOS(); + String path = getPath(os, libName); + + if ((os.getName().equals("linux") && !path.endsWith(".so")) || (os.getName().equals("windows") && !path.endsWith(".dll")) || (os.getName().equals("osx") && !path.endsWith(".jnilib") && !path.endsWith(".dylib"))) { + throw new IllegalStateException("OS sanity check failed. System property os.name reports " + os.getName()+" but System.mapLibraryName is looking for " + getLibName(libName)); + } + + File exported; + + try { + exported = exportResource(path); + } + catch (IOException e) { + throw new UnsatisfiedLinkError(e.getMessage()); + } + String filename = exported.getAbsolutePath(); + + System.load(filename); + if(os.canDeleteEager) { + try { + exported.delete(); + } catch(Throwable t) { + // EAT, since we do not care that an eager deletion did not work... + } + } + } + + /** + * Gets a relative path for a library. The path will be of the form: + * {@code {os}/{arch}/{name}}. + * + * @return a relative path to a resource to be loaded from the classpath + */ + private static String getPath(OS os, String libName) { + return SEPARATOR + + "lib" + SEPARATOR + + os.getName() + SEPARATOR + + os.getArch() + SEPARATOR + + getLibName(libName); + } + + /** + * Export a resource from the classpath to a temporary file. + * + * @param path the relative path of the file to load from the classpath + * + * @return the absolute path to the exported file + * @throws IOException + */ + private static File exportResource(String path) throws IOException { + InputStream resource = JNIUtil.class.getResourceAsStream(path); + if(resource == null) + throw new IllegalStateException("Embedded library jar:" + path + " not found"); + File f = saveStreamAsTempFile(resource); + return f; + } + + private static File saveStreamAsTempFile(InputStream resource) throws IOException { + File f = File.createTempFile(TEMPFILE_PREFIX, TEMPFILE_SUFFIX); + FileOutputStream outputStream = new FileOutputStream(f); + copyStream(resource, outputStream); + outputStream.flush(); + outputStream.close(); + f.deleteOnExit(); + return f; + } + + private static void copyStream(InputStream resource, OutputStream fileOutputStream) throws IOException { + byte[] buffer = new byte[4096]; + int bytesRead; + while((bytesRead = resource.read(buffer)) > 0) { + fileOutputStream.write(buffer, 0, bytesRead); + } + } + + private static String getLibName(String libName) { + String systemLibName = System.mapLibraryName(libName); + if (systemLibName.endsWith(".dylib")) { + systemLibName = systemLibName.replace("dylib", "jnilib"); + } + return systemLibName; + } + + private static OS getRunningOS() { + String osname = System.getProperty("os.name").toLowerCase(); + if(osname.startsWith("windows")) + return OS.WIN32; + if(osname.startsWith("linux")) + return OS.LINUX; + if(osname.startsWith("mac") || osname.startsWith("darwin")) + return OS.OSX; + throw new IllegalStateException("Unknown or unsupported OS: " + osname); + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/KeySelector.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/KeySelector.java new file mode 100644 index 0000000000..31429c7639 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/KeySelector.java @@ -0,0 +1,165 @@ +/* + * KeySelector.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +/** + * A {@code KeySelector} identifies a particular key in the database. FoundationDB's + * lexicographically ordered data model permits finding keys based on their order (for + * example, finding the first key in the database greater than a given key). Key selectors + * represent a description of a key in the database that could be resolved to an actual + * key by {@code Transaction}'s {@link Transaction#getKey(KeySelector) getKey()} + * or used directly as the beginning or end of a range in {@code Transaction}'s + * {@link Transaction#getRange(KeySelector, KeySelector) getRange()}.
    + *
    + * For more about how key selectors work in practice, see + * the KeySelector documentation. + *
    + *
    + * Generally one of the following static methods should be used to construct a {@code KeySelector}: + *
    • {@link #lastLessThan(byte[]) lastLessThan}
    • + *
    • {@link #lastLessOrEqual(byte[]) lastLessOrEqual}
    • + *
    • {@link #firstGreaterThan(byte[]) firstGreaterThan}
    • + *
    • {@link #firstGreaterOrEqual(byte[]) firstGreaterOrEqual}
    + *
    + * This is an immutable class. The {@code add(int)} call does not + * modify internal state, but returns a new instance. + *
    + */ +public class KeySelector { + private final byte[] key; + private final boolean orEqual; + private final int offset; + + /** + * Constructs a new {@code KeySelector} from the given parameters. Client code + * will not generally call this constructor. + * + * @param key the base key to reference + * @param orEqual true if the key should be considered for equality + * @param offset the number of keys to offset from once the key is found + */ + public KeySelector(byte[] key, boolean orEqual,int offset) { + this.key = key; + this.orEqual = orEqual; + this.offset = offset; + } + + /** + * Creates a {@code KeySelector} that picks the last key less than the parameter + * + * @param key the key to use as the edge of the edge of selection criteria + * + * @return a newly created {@code KeySelector} + */ + public static KeySelector lastLessThan( byte[] key ) { + return new KeySelector( key, false, 0 ); + } + + /** + * Creates a {@code KeySelector} that picks the last key less than or equal to the parameter + * + * @param key the key to use as the edge of the edge of selection criteria + * + * @return a newly created {@code KeySelector} + */ + public static KeySelector lastLessOrEqual( byte[] key ) { + return new KeySelector( key, true, 0 ); + } + + /** + * Creates a {@code KeySelector} that picks the first key greater than the parameter + * + * @param key the key to use as the edge of the edge of selection criteria + * + * @return a newly created {@code KeySelector} + */ + public static KeySelector firstGreaterThan( byte[] key ) { + return new KeySelector( key, true, +1 ); + } + + /** + * Creates a {@code KeySelector} that picks the first key greater than or equal to the parameter + * + * @param key the key to use as the edge of the edge of selection criteria + * + * @return a newly created {@code KeySelector} + */ + public static KeySelector firstGreaterOrEqual( byte[] key ) { + return new KeySelector( key, false, +1 ); + } + + /** + * Returns a new {@code KeySelector} offset by a given + * number of keys from this one. For example, an offset of {@code 1} means + * that the new {@code KeySelector} specifies the key in the database + * after the key selected by this {@code KeySelector}. The offset can be negative; + * these will move the selector to previous keys in the database.
    + *
    + * Note that large offsets take time O(offset) to resolve, making them a + * poor choice for iterating through a large range. (Instead, use the keys + * returned from a range query operation + * themselves to create a new beginning {@code KeySelector}.) For more information see + * the KeySelector documentation. + * + * @param offset the number of keys to offset the {@code KeySelector}. This number can be + * negative. + * + * @return a newly created {@code KeySelector} that is offset by a number of keys. + */ + public KeySelector add( int offset ) { + return new KeySelector( getKey(), orEqual(), getOffset() + offset ); + } + + /** + * Returns a copy of the key that serves as the anchor for this {@code KeySelector}. This is + * not the key to which this {@code KeySelector} would resolve to. For this + * function see {@link ReadTransaction#getKey(KeySelector)}. + * + * @return a copy of the "anchor" key for this {@code KeySelector}. + */ + public byte[] getKey() { + byte[] res = new byte[key.length]; + System.arraycopy(key, 0, res, 0, key.length); + return res; + } + + @Override + public String toString() { + return String.format("(%s, %s, %d)", + ByteArrayUtil.printable(getKey()), Boolean.toString(orEqual()), getOffset()); + } + + /** + * Returns the {@code or-equal} parameter of this {@code KeySelector}. For internal use. + */ + boolean orEqual() { + return orEqual; + } + + /** + * Returns the key offset for this {@code KeySelector}. For internal use. + */ + public int getOffset() { + return offset; + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/KeyValue.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/KeyValue.java new file mode 100644 index 0000000000..be034be2a5 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/KeyValue.java @@ -0,0 +1,80 @@ +/* + * KeyValue.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.Arrays; + +/** + * A key/value pair. Range read operation on FoundationDB return {@code KeyValue}s. + * This is a simple value type; mutating it won't affect your {@code Transaction} or + * the {@code Database}. + * + */ +public class KeyValue { + private final byte[] key, value; + + /** + * Constructs a new {@code KeyValue} from the specified key and value. + * + * @param key the key portion of the pair + * @param value the value portion of the pair + */ + public KeyValue(byte[] key, byte[] value) { + this.key = key; + this.value = value; + } + + /** + * Gets the key from the pair. + * + * @return the key + */ + public byte[] getKey() { + return this.key; + } + + /** + * Gets the value from the pair. + * + * @return the value + */ + public byte[] getValue() { + return this.value; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + if (obj == this) + return true; + if (!(obj instanceof KeyValue)) + return false; + + KeyValue rhs = (KeyValue) obj; + return Arrays.equals(key, rhs.key) && Arrays.equals(value, rhs.value); + } + + @Override + public int hashCode() { + return 17 + (37 * Arrays.hashCode(key) + Arrays.hashCode(value)); + } +} \ No newline at end of file diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/LocalityUtil.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/LocalityUtil.java new file mode 100644 index 0000000000..3fc4fcd9bc --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/LocalityUtil.java @@ -0,0 +1,243 @@ +/* + * LocalityUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.function.BiFunction; + +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncIterator; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +/** + * The FoundationDB API comes with a set of functions for discovering the + * storage locations of keys within your cluster. This information can be useful + * for advanced users who wish to take into account the location of keys in the + * design of applications or processes. + * + */ +public class LocalityUtil { + /** + * Returns a {@code AsyncIterable} of keys {@code k} such that + * {@code begin <= k < end} and {@code k} is located at the start of a + * contiguous range stored on a single server.
    + *
    + * This method is not transactional. The returned boundaries are an estimate + * and may not represent the exact boundary locations at any database version. + * + * @param db the database to query + * @param begin the inclusive start of the range + * @param end the exclusive end of the range + * + * @return an sequence of keys denoting the start of single-server ranges + */ + public static AsyncIterable getBoundaryKeys(Database db, byte[] begin, byte[] end) { + return getBoundaryKeys_internal(db.createTransaction(), begin, end); + } + + /** + * Returns a {@code AsyncIterable} of keys {@code k} such that + * {@code begin <= k < end} and {@code k} is located at the start of a + * contiguous range stored on a single server.
    + *
    + * This method is not transactional. The returned boundaries + * are an estimate and may not represent the exact boundary locations at + * any database version. The passed {@code Transaction} is not used + * for reads directly, instead it is used to get access to associated + * {@link Database}. As a result, options (such as retry limit) set on the + * passed {@code Transaction} will not be applied. If, however, the passed + * {@code Transaction} has already gotten a read version there is some + * latency advantage to using this form of the method. Also, if the database + * is unavailable prior to the function call, any timeout set on the + * passed {@code Transaction} will still trigger. + * + * @param tr the transaction on which to base the query + * @param begin the inclusive start of the range + * @param end the exclusive end of the range + * + * @return an sequence of keys denoting the start of single-server ranges + */ + public static AsyncIterable getBoundaryKeys(Transaction tr, byte[] begin, byte[] end) { + Transaction local = tr.getDatabase().createTransaction(); + CompletableFuture readVersion = tr.getReadVersion(); + if(readVersion.isDone() && !readVersion.isCompletedExceptionally()) { + local.setReadVersion(readVersion.getNow(null)); + } + return new BoundaryIterable(local, begin, end); + } + + /** + * Returns a list of public network addresses as strings, one for each of + * the storage servers responsible for storing {@code key} and its associated + * value. + * + * If locality information is not available, the returned future will carry a + * {@link FDBException} locality_information_unavailable. + * + * @param tr the transaction in which to gather location information + * @param key the key for which to gather location information + * + * @return a list of addresses in string form + */ + public static CompletableFuture getAddressesForKey(Transaction tr, byte[] key) { + if (!(tr instanceof FDBTransaction)) { + CompletableFuture future = new CompletableFuture(); + future.completeExceptionally(new FDBException("locality_information_unavailable", 1033)); + return future; + } + return ((FDBTransaction)tr).getAddressesForKey(key); + } + + private static AsyncIterable getBoundaryKeys_internal(Transaction tr, byte[] begin, byte[] end) { + return new BoundaryIterable(tr, begin, end); + } + + static class BoundaryIterable implements AsyncIterable { + final Transaction tr; + final byte[] begin; + final byte[] end; + final AsyncIterable firstGet; + + public BoundaryIterable(Transaction tr, byte[] begin, byte[] end) { + this.tr = tr; + this.begin = Arrays.copyOf(begin, begin.length); + this.end = Arrays.copyOf(end, end.length); + + tr.options().setReadSystemKeys(); + tr.options().setLockAware(); + firstGet = tr.getRange(keyServersForKey(begin), keyServersForKey(end)); + } + + @Override + public AsyncIterator iterator() { + return new BoundaryIterator(); + } + + @Override + public CompletableFuture> asList() { + return AsyncUtil.collect(this, tr.getExecutor()); + } + + class BoundaryIterator implements AsyncIterator { + AsyncIterator block = BoundaryIterable.this.firstGet.iterator(); + Transaction tr = BoundaryIterable.this.tr; + byte[] begin = BoundaryIterable.this.begin; + byte[] lastBegin = begin; + private CompletableFuture nextFuture; + + public BoundaryIterator() { + nextFuture = block.onHasNext().handleAsync(handler, tr.getExecutor()).thenCompose(x -> x); + } + + @Override + public CompletableFuture onHasNext() { + return nextFuture; + } + + @Override + public boolean hasNext() { + return nextFuture.join(); + } + + CompletableFuture restartGet() { + if(ByteArrayUtil.compareUnsigned(begin, end) >= 0) { + return CompletableFuture.completedFuture(false); + } + lastBegin = begin; + tr.options().setReadSystemKeys(); + block = tr.getRange( + keyServersForKey(begin), + keyServersForKey(end)).iterator(); + nextFuture = block.onHasNext().handleAsync(handler, tr.getExecutor()).thenCompose(x -> x); + return nextFuture; + } + + BiFunction> handler = new BiFunction>() { + @Override + public CompletableFuture apply(Boolean b, Throwable o) { + if(b != null) { + return CompletableFuture.completedFuture(b); + } + if(o instanceof FDBException) { + FDBException err = (FDBException) o; + if(err.getCode() == 1007 && !Arrays.equals(begin, lastBegin)) { + BoundaryIterator.this.tr.dispose(); + BoundaryIterator.this.tr = + BoundaryIterator.this.tr.getDatabase().createTransaction(); + return restartGet(); + } + } + + if(!(o instanceof RuntimeException)) + throw new CompletionException(o); + + CompletableFuture onError = BoundaryIterator.this.tr.onError((RuntimeException) o); + return onError.thenComposeAsync(tr -> { + BoundaryIterator.this.tr = tr; + return restartGet(); + }, tr.getExecutor()); + } + }; + + @Override + public byte[] next() { + if(!nextFuture.isDone()) { + throw new IllegalStateException("Call to next without hasNext()=true"); + } + KeyValue o = block.next(); + byte[] key = o.getKey(); + byte[] suffix = Arrays.copyOfRange(key, 13, key.length); + BoundaryIterator.this.begin = ByteArrayUtil.join(suffix, new byte[] { (byte)0 }); + nextFuture = block.onHasNext().handleAsync(handler, tr.getExecutor()).thenCompose(x -> x); + return suffix; + } + + @Override + public void remove() { + throw new UnsupportedOperationException("Boundary keys are read-only"); + } + + @Override + public void cancel() { + // TODO Auto-generated method stub + } + + @Override + public void dispose() { + BoundaryIterator.this.tr.dispose(); + } + } + } + + static Charset ASCII = Charset.forName("US-ASCII"); + static byte[] keyServersForKey(byte[] key) { + return ByteArrayUtil.join(new byte[] { (byte)255 }, + "/keyServers/".getBytes(ASCII), + key); + } + + private LocalityUtil() {} +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/NativeFuture.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/NativeFuture.java new file mode 100644 index 0000000000..c6ccb3b267 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/NativeFuture.java @@ -0,0 +1,92 @@ +/* + * NativeFuture.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.CompletableFuture; + +abstract class NativeFuture extends CompletableFuture { + protected final long cPtr; + + protected NativeFuture(long cPtr) { + this.cPtr = cPtr; + } + + // Adds a callback to call marshalWhenDone when the C-future + // has completed. Every subclass should add this to its constructor + // after it has initialized its members. This is excluded from the + // constructor of this class because a quickly completing future can + // lead to a race where the marshalWhenDone tries to run on an + // unconstructed subclass. + protected void registerMarshalCallback() { + Future_registerCallback(cPtr, new Runnable() { + @Override + public void run() { + NativeFuture.this.marshalWhenDone(); + } + }); + } + + private void marshalWhenDone() { + try { + T val = getIfDone_internal(); + postMarshal(); + complete(val); + } catch(FDBException t) { + assert(t.getCode() != 2015); // future_not_set not possible + if(t.getCode() != 1102) { // future_released + completeExceptionally(t); + } + } catch(Throwable t) { + completeExceptionally(t); + } + } + + protected void postMarshal() { + dispose(); + } + + abstract T getIfDone_internal() throws FDBException; + + public void dispose() { + Future_releaseMemory(cPtr); + } + + @Override + protected void finalize() throws Throwable { + Future_dispose(cPtr); + } + + @Override + public T join() { + Future_blockUntilReady(cPtr); + return super.join(); + } + + private native void Future_registerCallback(long cPtr, Runnable callback); + private native void Future_blockUntilReady(long cPtr); + private native boolean Future_isReady(long cPtr); + private native void Future_dispose(long cPtr); + private native void Future_cancel(long cPtr); + private native void Future_releaseMemory(long cPtr); + + // Used by FutureVoid + protected native FDBException Future_getError(long cPtr); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/OptionConsumer.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/OptionConsumer.java new file mode 100644 index 0000000000..4d0c398f4d --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/OptionConsumer.java @@ -0,0 +1,37 @@ +/* + * OptionConsumer.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +/** + * An object on which encoded options can be set. Rarely used outside of + * internal implementation. + */ +public interface OptionConsumer { + /** + * Attempt to set the given option. The parameter interpretation is completely + * dependent on the option code. Normally this interface should not be used + * from outside this package. + * + * @param code the encoded parameter to set + * @param parameter the value, the range of which is dependent on the parameter {@code code} + */ + public void setOption(int code, byte[] parameter); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/OptionsSet.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/OptionsSet.java new file mode 100644 index 0000000000..b3051aebb6 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/OptionsSet.java @@ -0,0 +1,62 @@ +/* + * OptionsSet.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.Charset; + +abstract class OptionsSet { + private final static Charset CHARSET_UTF8 = Charset.forName("UTF-8"); + OptionConsumer consumer; + + public OptionsSet(OptionConsumer provider) { + this.consumer = provider; + } + + /** + * Returns the object on which these options are being set. Rarely used by + * client code, since all options should be top-level methods on extending + * classes. + * + * @return target of option set calls + */ + public OptionConsumer getOptionConsumer() { return consumer; } + + protected void setOption(int code) { + consumer.setOption(code, null); + } + + protected void setOption(int code, byte[] param) { + consumer.setOption(code, param); + } + + protected void setOption(int code, String param) { + consumer.setOption(code, param == null ? null : param.getBytes(CHARSET_UTF8)); + } + + protected void setOption(int code, long param) { + ByteBuffer b = ByteBuffer.allocate(8); + b.order(ByteOrder.LITTLE_ENDIAN); + b.putLong(param); + consumer.setOption(code, b.array() ); + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/Range.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/Range.java new file mode 100644 index 0000000000..6a0b091f8d --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/Range.java @@ -0,0 +1,119 @@ +/* + * Range.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.Arrays; + +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +/** + * A simple description of an exact range of keyspace, specified by a begin and end key. As with + * all FoundationDB APIs, {@code begin} is inclusive, {@code end} exclusive. + * + */ +public class Range { + /** + * The beginning of the range. This constraint on the range is inclusive. + */ + public final byte[] begin; + + /** + * The end of the range. This constraint on the range is exclusive. + */ + public final byte[] end; + + /** + * Construct a new {@code Range} with an inclusive begin key and an exclusive + * end key. + * @param begin the inclusive beginning of the range. + * @param end the exclusive end of the range. + */ + public Range(byte[] begin, byte[] end) { + this.begin = begin; + this.end = end; + } + + /** + * Returns a {@code Range} that describes all possible keys that are prefixed with a + * specified key. Use the result of this call as an input to + * {@link Transaction#getRange(Range)} to replicate the now-removed call + * {@code Transaction.getRangeStartsWith(k)}. + * + * @param prefix the key prefixing the range, must not be {@code null} + * + * @return the range of keys starting with {@code prefix} + */ + public static Range startsWith(byte[] prefix) { + if(prefix == null) + throw new NullPointerException("prefix cannot be null"); + return new Range( prefix, ByteArrayUtil.strinc(prefix) ); + } + + /** + * Returns {@code true} if the given {@link Object} is a {@code Range} + * object that refers to the same key range within the keyspace. + * This will be true if the given range has the same {@link #begin} + * and {@link #end} key. This will return {@code false} if the given + * {@link Object} is not a {@code Range} instance. + * + * @param o the {@link Object} to check for equality + * + * @return whether the given {@link Object} matches this {@code Range} + */ + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + else if(o == null || !(o instanceof Range)) { + return false; + } + else { + Range that = (Range)o; + return Arrays.equals(this.begin, that.begin) && Arrays.equals(this.end, that.end); + } + } + + /** + * Computes a hash code from the {@link #begin} and {@link #end} keys of + * this {@code Range}. In particular, it will take the bitwise XOR of the + * hash of {@link #begin} with 37 times the hash of {@link #end}. + * + * @return hash code derived from the hashes of {@link #begin} and {@link #end} + */ + @Override + public int hashCode() { + return Arrays.hashCode(begin) ^ (37 * Arrays.hashCode(end)); + } + + /** + * Returns a human-readable {@link String} representation of this {@code Range}. + * It will contain human-readable representations of both the {@link #begin} + * and {@link #end} keys. + * + * @return a human-readable representation of this {@code Range} + */ + @Override + public String toString() { + return "Range(" + (begin == null ? "null" : "\"" + ByteArrayUtil.printable(begin) + "\"") + + ", " + (end == null ? "null" : "\"" + ByteArrayUtil.printable(end) + "\"") + ")"; + } +} \ No newline at end of file diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeQuery.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeQuery.java new file mode 100644 index 0000000000..fc9d2317a1 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeQuery.java @@ -0,0 +1,352 @@ +/* + * RangeQuery.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.List; +import java.util.NoSuchElementException; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncIterator; +import com.apple.cie.foundationdb.async.AsyncUtil; + +/** + * Represents a query against FoundationDB for a range of keys. The + * result of this query can be iterated over in a blocking fashion with a call to + * {@link #iterator()} (as specified by {@link Iterable}). + * If the calling program uses an asynchronous paradigm, a non-blocking + * {@link AsyncIterator} is returned from {@link #iterator()}. Both of these + * constructions will not begin to query the database until the first call to + * {@code hasNext()}. As the query uses its {@link Transaction} of origin to fetch + * all the data, the use of this query object must not span more than a few seconds. + * + *

    NOTE: although resulting {@code Iterator}s do support the {@code remove()} + * operation, the remove is not durable until {@code commit()} on the {@code Transaction} + * that yielded this query returns true. + */ +class RangeQuery implements AsyncIterable, Iterable { + private final FDBTransaction tr; + private final KeySelector begin; + private final KeySelector end; + private final boolean snapshot; + private final int rowLimit; + private final boolean reverse; + private final StreamingMode streamingMode; + private final FutureResults firstChunk; + + RangeQuery(FDBTransaction transaction, boolean isSnapshot, + KeySelector begin, KeySelector end, int rowLimit, + boolean reverse, StreamingMode streamingMode) { + this.tr = transaction; + this.begin = begin; + this.end = end; + this.snapshot = isSnapshot; + this.rowLimit = rowLimit; + this.reverse = reverse; + this.streamingMode = streamingMode; + + // start the first fetch... + firstChunk = tr.getRange_internal(begin, end, + rowLimit, 0, streamingMode.code(), 1, snapshot, reverse); + } + + /** + * Returns all the results from the range requested as a {@code List}. If there were no + * limits on the original query and there is a large amount of data in the database + * this call could use a very large amount of memory. + * + * @return a {@code CompletableFuture} that will be set to the contents of the database + * constrained by the query parameters. + */ + @Override + public CompletableFuture> asList() { + StreamingMode mode = this.streamingMode; + if(mode == StreamingMode.ITERATOR) + mode = (this.rowLimit == 0) ? StreamingMode.WANT_ALL : StreamingMode.EXACT; + + // if the streaming mode is EXACT, try and grab things as one chunk + if(mode == StreamingMode.EXACT) { + CompletableFuture range = tr.getRange_internal( + this.begin, this.end, this.rowLimit, 0, StreamingMode.EXACT.code(), + 1, this.snapshot, this.reverse); + return range.thenApply(new Function>() { + @Override + public List apply(RangeResultInfo o) { + return o.get().values; + } + }); + } + + // If the streaming mode is not EXACT, simply collect the results of an iteration into a list + return AsyncUtil.collect( + new RangeQuery(tr, snapshot, begin, end, rowLimit, reverse, mode), tr.getExecutor()); + } + + /** + * Returns an {@code Iterator} over the results of this query against FoundationDB. + * + * @return an {@code Iterator} over type {@code KeyValue}. + */ + @Override + public AsyncRangeIterator iterator() { + return new AsyncRangeIterator(this.rowLimit, this.reverse, this.streamingMode); + } + + private class AsyncRangeIterator implements AsyncIterator { + // immutable aspects of this iterator + private final boolean rowsLimited; + private final boolean reverse; + private final StreamingMode streamingMode; + + // There is the chance for parallelism in the two "chunks" for fetched data + private RangeResult chunk = null; + private RangeResult nextChunk = null; + private boolean fetchOutstanding = true; + private byte[] prevKey = null; + private int index = 0; + // The first request is made in the constructor for the parent Iterable, so start at 1 + private int iteration = 1; + private KeySelector begin; + private KeySelector end; + + private int rowsRemaining; + + private CompletableFuture nextFuture; + private boolean isCancelled = false; + + private AsyncRangeIterator(int rowLimit, boolean reverse, StreamingMode streamingMode) { + this.begin = RangeQuery.this.begin; + this.end = RangeQuery.this.end; + this.rowsLimited = rowLimit != 0; + this.rowsRemaining = rowLimit; + this.reverse = reverse; + this.streamingMode = streamingMode; + + // Register for completion, etc. on the first chunk. Some of the fields in + // this class were initialized with the knowledge that this fetch is active + // at creation time. This set normally happens in startNextFetch, but + // the first fetch has already been configured and started. + CompletableFuture promise = new CompletableFuture(); + nextFuture = promise; + + // FIXME: should we propagate cancellation into the first chuck fetch? + // This would invalidate the whole iterable, not just the iterator + //promise.onCancelledCancel(firstChunk); + + // FIXME: I have no idea if this will just get garbage collected away, etc. + firstChunk.whenComplete(new FetchComplete(firstChunk, promise)); + } + + private synchronized boolean mainChunkIsTheLast() { + return !chunk.more || (rowsLimited && rowsRemaining < 1); + } + + class FetchComplete implements BiConsumer { + final FutureResults fetchingChunk; + final CompletableFuture promise; + + public FetchComplete(FutureResults fetch, CompletableFuture promise) { + this.fetchingChunk = fetch; + this.promise = promise; + } + + @Override + public void accept(RangeResultInfo data, Throwable error) { + final RangeResultSummary summary; + + if(error != null) { + promise.completeExceptionally(error); + if(error instanceof Error) { + throw (Error)error; + } + + return; + } + + summary = data.getSummary(); + if(summary.lastKey == null) { + promise.complete(Boolean.FALSE); + return; + } + + synchronized(AsyncRangeIterator.this) { + fetchOutstanding = false; + + // adjust the total number of rows we should ever fetch + rowsRemaining -= summary.keyCount; + + // set up the next fetch + if (reverse) { + end = KeySelector.firstGreaterOrEqual(summary.lastKey); + } else { + begin = KeySelector.firstGreaterThan(summary.lastKey); + } + + // If this is the first fetch or the main chunk is exhausted + if(chunk == null || index == chunk.values.size()) { + nextChunk = null; + chunk = data.get(); + index = 0; + } else { + nextChunk = data.get(); + } + } + + promise.complete(Boolean.TRUE); + } + } + + private synchronized void startNextFetch() { + if(fetchOutstanding) + throw new IllegalStateException("Reentrant call not allowed"); // This can not be called reentrantly + if(isCancelled) + return; + + if(mainChunkIsTheLast()) + return; + + fetchOutstanding = true; + nextChunk = null; + + FutureResults fetchingChunk = tr.getRange_internal(begin, end, + rowsLimited ? rowsRemaining : 0, 0, streamingMode.code(), + ++iteration, snapshot, reverse); + + CompletableFuture promise = new CompletableFuture(); + nextFuture = promise; + + // FIXME: BOOOOOOOOOO! Maybe we don't need this? + // promise.onCancelledCancel(fetchingChunk); + + // TODO: again, I have no idea if this will get out-of-scope collected right away + fetchingChunk.whenComplete(new FetchComplete(fetchingChunk, promise)); + } + + @Override + public synchronized CompletableFuture onHasNext() { + if(isCancelled) + throw new CancellationException(); + + // This will only happen before the first fetch has completed + if(chunk == null) { + return nextFuture; + } + + // We have a chunk and are still working though it + if(index < chunk.values.size()) { + return CompletableFuture.completedFuture(true); + } + + // If we are at the end of the current chunk there is either: + // - no more data -or- + // - we are already fetching the next block + return mainChunkIsTheLast() ? + //new ReadyFuture(false, tr.getExecutor()) : + CompletableFuture.completedFuture(false) : + nextFuture; + } + + @Override + public boolean hasNext() { + return onHasNext().join(); + } + + // moves to the last position in the current chunk + /*public synchronized void consumeAll() { + index = chunk.values.size() - 1; + }*/ + + @Override + public KeyValue next() { + CompletableFuture nextFuture; + synchronized(this) { + if(isCancelled) + throw new CancellationException(); + + // at least the first chunk has been fetched and there is at least one + // available result + if(chunk != null && index < chunk.values.size()) { + // If this is the first call to next() on a chunk, then we will want to + // start fetching the data for the next block + boolean initialNext = index == 0; + + KeyValue result = chunk.values.get(index); + prevKey = result.getKey(); + index++; + + // If this is the first call to next() on a chunk there cannot + // be another waiting, since we could not have issued a request + assert(!(initialNext && nextChunk != null)); + + // we are at the end of the current chunk and there is more to be had already + if(index == chunk.values.size() && nextChunk != null) { + index = 0; + chunk = nextChunk; + nextChunk = null; + } + + if(initialNext) { + startNextFetch(); + } + + return result; + } + + nextFuture = onHasNext(); + } + + // If there was no result ready then we need to wait on the future + // and return the proper result, throwing if there are no more elements + return nextFuture.thenApply(NEXT_MAPPER).join(); + } + + @Override + public synchronized void remove() { + if(prevKey == null) + throw new IllegalStateException("No value has been fetched from database"); + + tr.clear(prevKey); + } + + @Override + public synchronized void cancel() { + isCancelled = true; + nextFuture.cancel(true); + } + + @Override + public void dispose() { + cancel(); + } + + private final Function NEXT_MAPPER = new Function() { + @Override + public KeyValue apply(Boolean o) { + if(o) + return next(); + throw new NoSuchElementException(); + } + }; + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeResult.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeResult.java new file mode 100644 index 0000000000..92d2e98431 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeResult.java @@ -0,0 +1,54 @@ +/* + * RangeResult.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.ArrayList; +import java.util.List; + +class RangeResult { + final List values; + final boolean more; + + RangeResult(byte[] keyValues, int[] lengths, boolean more) { + if(lengths.length % 2 != 0) { + throw new IllegalArgumentException("There needs to be an even number of lenghts!"); + } + + int count = lengths.length / 2; + values = new ArrayList(count); + + int offset = 0; + for(int i = 0; i < count; i++) { + int keyLength = lengths[i * 2]; + int valueLength = lengths[(i * 2) + 1]; + + byte[] k = new byte[keyLength]; + System.arraycopy(keyValues, offset, k, 0, keyLength); + + byte[] v = new byte[valueLength]; + System.arraycopy(keyValues, offset + keyLength, v, 0, valueLength); + + offset += keyLength + valueLength; + values.add(new KeyValue(k, v)); + } + this.more = more; + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeResultInfo.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeResultInfo.java new file mode 100644 index 0000000000..b44d10f1d9 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeResultInfo.java @@ -0,0 +1,37 @@ +/* + * RangeResultInfo.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +class RangeResultInfo { + RangeResultSummary getSummary() { + return f.getSummary(); + } + + RangeResult get() { + return f.getResults(); + } + + RangeResultInfo(FutureResults f) { + this.f = f; + } + + private FutureResults f; +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeResultSummary.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeResultSummary.java new file mode 100644 index 0000000000..433cb4a970 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/RangeResultSummary.java @@ -0,0 +1,33 @@ +/* + * RangeResultSummary.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +class RangeResultSummary { + final byte[] lastKey; + final int keyCount; + final boolean more; + + RangeResultSummary(byte[] lastKey, int keyCount, boolean more) { + this.lastKey = lastKey; + this.keyCount = keyCount; + this.more = more; + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/ReadTransaction.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/ReadTransaction.java new file mode 100644 index 0000000000..79e32937d4 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/ReadTransaction.java @@ -0,0 +1,335 @@ +/* + * ReadTransaction.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.CompletableFuture; + +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncIterator; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * A read-only subset of a FoundationDB {@link Transaction}. This is the interface that + * {@code Transaction}'s {@link Transaction#snapshot snapshot} presents. + * + *
    + * Note: Client must call {@link Transaction#commit()} and wait on the result on all transactions, + * even ones that only read. This is done automatically when using the retry loops from + * {@link Database#run(Function)}. This is explained more in the intro to {@link Transaction}. + * + * @see Transaction + */ +public interface ReadTransaction extends ReadTransactionContext { + /** + * When passed to a {@code getRange()} call that takes a {@code limit} parameter, + * indicates that the query should return unlimited rows. + */ + public static final int ROW_LIMIT_UNLIMITED = 0; + + /** + * Gets the version at which the reads for this {@code Transaction} will access the database. + * @return the version for database reads + */ + public CompletableFuture getReadVersion(); + + /** + * Gets a value from the database. The call will return {@code null} if the key is not + * present in the database. + * + * @param key the key whose value to fetch from the database + * + * @return a {@code CompletableFuture} which will be set to the value corresponding to + * the key or to null if the key does not exist. + */ + public CompletableFuture get(byte[] key); + + /** + * Returns the key referenced by the specified {@code KeySelector}. + * By default, the key is cached for the duration of the transaction, providing + * a potential performance benefit. However, the value of the key is also retrieved, + * using network bandwidth. Invoking {@code setReadYourWritesDisable} will avoid + * both the caching and the increased network bandwidth. + * + * @see KeySelector + * + * @param selector the relative key location to resolve + * + * @return a {@code CompletableFuture} which will be set to an absolute database key + */ + public CompletableFuture getKey(KeySelector selector); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code KeySelector}s, with the begin + * {@code KeySelector} inclusive and the end {@code KeySelector} exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(KeySelector begin, KeySelector end); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code KeySelector}s, with the begin + * {@code KeySelector} inclusive and the end {@code KeySelector} exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code KeySelector}s, with the begin + * {@code KeySelector} inclusive and the end {@code KeySelector} exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code KeySelector}s, with the begin + * {@code KeySelector} inclusive and the end {@code KeySelector} exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * @param mode provide a hint about how the results are to be used. This + * can provide speed improvements or efficiency gains based on the caller's + * knowledge of the upcoming access pattern. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse, StreamingMode mode); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(byte[] begin, byte[] end); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * @param mode provide a hint about how the results are to be used. This + * can provide speed improvements or efficiency gains based on the caller's + * knowledge of the upcoming access pattern. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse, StreamingMode mode); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. {@link Range}s are returned + * from calls to {@link Tuple#range()} and {@link Range#startsWith(byte[])}.
    + *
    + * Note: users of older version of the API should replace old calls to + * {@code getRangeStartsWith( k )} with {@code getRange(Range.startsWith( k ))} + * + * @see KeySelector + * @see AsyncIterator + * + * @param range the range of keys to return + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(Range range); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. {@link Range}s are returned + * from calls to {@link Tuple#range()} and {@link Range#startsWith(byte[])}.
    + *
    + * Note: users of older version of the API should replace old calls to + * {@code getRangeStartsWith( k )} with {@code getRange(Range.startsWith( k ))} + * + * @see KeySelector + * @see AsyncIterator + * + * @param range the range of keys to return + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(Range range, + int limit); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. {@link Range}s are returned + * from calls to {@link Tuple#range()} and {@link Range#startsWith(byte[])}.
    + *
    + * Note: users of older version of the API should replace old calls to + * {@code getRangeStartsWith( k )} with {@code getRange(Range.startsWith( k ))} + * + * @see KeySelector + * @see AsyncIterator + * + * @param range the range of keys to return + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(Range range, + int limit, boolean reverse); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. {@link Range}s are returned + * from calls to {@link Tuple#range()} and {@link Range#startsWith(byte[])}.
    + *
    + * Note: users of older version of the API should replace old calls to + * {@code getRangeStartsWith( k )} with {@code getRange(Range.startsWith( k ))} + * + * @see KeySelector + * @see AsyncIterator + * + * @param range the range of keys to return + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * @param mode provide a hint about how the results are to be used. This + * can provide speed improvements or efficiency gains based on the caller's + * knowledge of the upcoming access pattern. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(Range range, + int limit, boolean reverse, StreamingMode mode); + + /** + * Returns a set of options that can be set on a {@code Transaction} + * + * @return a set of transaction-specific options affecting this {@code Transaction} + */ + public TransactionOptions options(); + +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/ReadTransactionContext.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/ReadTransactionContext.java new file mode 100644 index 0000000000..865de398a6 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/ReadTransactionContext.java @@ -0,0 +1,75 @@ +/* + * ReadTransactionContext.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; +import java.util.function.Function; + +/** + * A context in which a {@code ReadTransaction} is available for database operations. The + * behavior of the methods specified in this interface, especially in the face + * errors, is implementation specific. In particular, some implementations will + * run {@link Function}s multiple times (retry) when certain errors are encountered. + * Therefore a {@code Function} should be prepared to be called more than once. This + * consideration means that a {@code Function} should use caution when directly + * modifying state in a class, especially in a way that could be observed were that + * {@code Function} to not complete successfully. + */ +public interface ReadTransactionContext { + /** + * Runs a function in this context that takes a read-only transaction. Depending on the type of + * context, this may execute the supplied function multiple times if an error is + * encountered. This method is blocking -- control will not return from this call + * until work is complete. + * + * @param retryable the block of logic to execute against a {@link ReadTransaction} + * in this context + * + * @return a result of the last call to {@code retryable} + */ + T read(Function retryable); + + /** + * Runs a function in this context that takes a read-only transaction. Depending on the type of + * context, this may execute the supplied function multiple times if an error is + * encountered. This call is non-blocking -- control flow will return immediately + * with a {@code CompletableFuture} that will be set when the process is complete. + * + * @param retryable the block of logic to execute against a {@link ReadTransaction} + * in this context + * + * @return a {@code CompletableFuture} that will be set to the value returned by the last call + * to {@code retryable} + */ + CompletableFuture readAsync( + Function> retryable); + + /** + * Retrieves the {@link Executor} used by this {@code TransactionContext} when running + * asynchronous callbacks. + * + * @return the {@link Executor} used by this {@code TransactionContext} + */ + Executor getExecutor(); + +} \ No newline at end of file diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/Transaction.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/Transaction.java new file mode 100644 index 0000000000..61d4e51fa9 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/Transaction.java @@ -0,0 +1,380 @@ +/* + * Transaction.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * A Transaction represents a FoundationDB database transaction. All operations on FoundationDB + * take place, explicitly or implicitly, through a Transaction.
    + *
    + * In FoundationDB, a transaction is a mutable snapshot of a database. All read and write operations + * on a transaction see and modify an otherwise-unchanging version of the database and only change + * the underlying database if and when the transaction is committed. Read operations do see the + * effects of previous write operations on the same transaction. Committing a transaction usually + * succeeds in the absence of + * conflicts.
    + *
    + * Transactions group operations into a unit with the properties of atomicity, isolation, and + * durability. Transactions also provide the ability to maintain an application's invariants or + * integrity constraints, supporting the property of consistency. Together these properties are + * known as ACID.
    + *
    + * Transactions are also causally consistent: once a transaction has been successfully committed, + * all subsequently created transactions will see the modifications made by it. + * The most convenient way for a developer to manage the lifecycle and retrying + * of a {@code Transaction} is to use {@link Database#run(Function)}. Otherwise, the client + * must have retry logic for fatal failures, failures to commit, and other transient errors.
    + *
    + * Keys and values in FoundationDB are byte arrays. To encode other data types, see the + * {@link Tuple Tuple API} and + * tuple layer documentation.
    + *
    + * When used as a {@link TransactionContext}, the methods {@code run()} and + * {@code runAsync()} on a {@code Transaction} will simply attempt the operations + * without any retry loop.
    + *
    + * Note: Client must call {@link #commit()} and wait on the result on all transactions, even + * ones that only read. This is done automatically when using the retry loops from + * {@link Database#run(Function)}. This is because outstanding reads originating from a + * {@code Transaction} will be cancelled when a {@code Transaction} is garbage collected. + * Since the garbage collector reserves the right to collect an in-scope object if it + * determines that there are no subsequent references it it, this can happen in seemingly + * innocuous situations. {@code CompletableFuture}s returned from {@code commit()} will block until + * all reads are complete, thereby saving the calling code from this potentially confusing + * situation.
    + *
    + * Note: All keys with a first byte of {@code 0xff} are reserved for internal use.
    + *
    + * Note: Java transactions automatically set the {@link TransactionOptions#setUsedDuringCommitProtectionDisable} + * option. This is because the Java bindings disallow use of {@code Transaction} objects after either + * {@link #reset()} or {@link #onError} is called. + */ +public interface Transaction extends Disposable, ReadTransaction, TransactionContext { + + /** + * Return special-purpose, read-only view of the database. Reads done through this interface are known as "snapshot reads". + * Snapshot reads selectively relax FoundationDB's isolation property, reducing + * Transaction conflicts + * but making reasoning about concurrency harder.
    + *
    + * For more information about how to use snapshot reads correctly, see + * Using snapshot reads. + */ + public ReadTransaction snapshot(); + + /** + * Directly sets the version of the database at which to execute reads. The + * normal operation of a transaction is to determine an appropriately recent + * version; this call overrides that behavior. If the version is set too + * far in the past, {@code past_version} errors will be thrown from read operations. + * Infrequently used. + * + * @param version the version at which to read from the database + */ + public void setReadVersion(long version); + + + /** + * Adds a range of keys to the transaction's read conflict ranges as if you + * had read the range. As a result, other transactions that write a key in + * this range could cause the transaction to fail with a conflict. + * + * @param keyBegin the first key in the range (inclusive) + * @param keyEnd the ending key for the range (exclusive) + */ + public void addReadConflictRange(byte[] keyBegin, byte[] keyEnd); + + /** + * Adds a key to the transaction's read conflict ranges as if you had read + * the key. As a result, other transactions that concurrently write this key + * could cause the transaction to fail with a conflict. + * + * @param key the key to be added to the range + */ + public void addReadConflictKey(byte[] key); + + /** + * Adds a range of keys to the transaction's write conflict ranges as if you + * had cleared the range. As a result, other transactions that concurrently + * read a key in this range could fail with a conflict. + * + * @param keyBegin the first key in the range (inclusive) + * @param keyEnd the ending key for the range (exclusive) + */ + public void addWriteConflictRange(byte[] keyBegin, byte[] keyEnd); + + /** + * Adds a key to the transaction's write conflict ranges as if you had + * written the key. As a result, other transactions that concurrently read + * this key could fail with a conflict. + * + * @param key the key to be added to the range + */ + public void addWriteConflictKey(byte[] key); + + /** + * Sets the value for a given key. This will not affect the + * database until {@link #commit} is called. + * + * @param key the key whose value is to be set + * @param value the value to set in the database + * @throws IllegalArgumentException + * @throws FDBException + */ + public void set(byte[] key, byte[] value); + + /** + * Clears a given key from the database. This will not affect the + * database until {@link #commit} is called. + * + * @param key the key whose value is to be cleared + * @throws IllegalArgumentException + * @throws FDBException + */ + public void clear(byte[] key); + + /** + * Clears a range of keys in the database. The upper bound of the range is + * exclusive; that is, the key (if one exists) that is specified as the end + * of the range will NOT be cleared as part of this operation. Range clears are + * efficient with FoundationDB -- clearing large amounts of data will be fast. + * This will not affect the database until {@link #commit} is called. + * + * @param beginKey the first clear + * @param endKey the key one past the last key to clear + * @throws IllegalArgumentException + * @throws FDBException + */ + public void clear(byte[] beginKey, byte[] endKey); + + /** + * Clears a range of keys in the database. The upper bound of the range is + * exclusive; that is, the key (if one exists) that is specified as the end + * of the range will NOT be cleared as part of this operation. Range clears are + * efficient with FoundationDB -- clearing large amounts of data will be fast. + * This will not affect the database until {@link #commit} is called. + * + * @param range the range of keys to clear + * + * @throws FDBException + */ + public void clear(Range range); + + /** + * Replace with calls to {@link #clear(Range)} with a parameter from a call to + * {@link Range#startsWith(byte[])}. + * + * @param prefix the starting bytes from the keys to be cleared. + * + * @throws FDBException + */ + @Deprecated + public void clearRangeStartsWith(byte[] prefix); + + /** + * An atomic operation is a single database command that carries out several + * logical steps: reading the value of a key, performing a transformation on + * that value, and writing the result. Different atomic operations perform + * different transformations. Like other database operations, an atomic + * operation is used within a transaction.
    + *
    + * Atomic operations do not expose the current value of the key to the client + * but simply send the database the transformation to apply. In regard to + * conflict checking, an atomic operation is equivalent to a write without a + * read. It can only cause other transactions performing reads of the key + * to conflict.
    + *
    + * By combining these logical steps into a single, read-free operation, + * FoundationDB can guarantee that the transaction will not conflict due to + * the operation. This makes atomic operations ideal for operating on keys + * that are frequently modified. A common example is the use of a key-value + * pair as a counter.
    + *
    + * Note: If a transaction uses both an atomic operation and a serializable + * read on the same key, the benefits of using the atomic operation (for both + * conflict checking and performance) are lost. + * + * The behavior of each {@link MutationType} is documented at its definition. + * + * @param optype the operation to perform + * @param key the target of the operation + * @param param the value with which to modify the key + */ + public void mutate(MutationType optype, byte[] key, byte[] param); + + /** + * Commit this {@code Transaction}. See notes in class description. Consider using + * {@code Database}'s {@link Database#run(Function) run()} calls for managing + * transactional access to FoundationDB. + * + * @return a {@code CompletableFuture} that, when set without error, guarantees the + * {@code Transaction}'s modifications committed durably to the + * database. If the commit failed, it will throw an {@link FDBException}. + *

    + * As with other client/server databases, in some failure scenarios a client may + * be unable to determine whether a transaction succeeded. In these cases, an + * {@link FDBException} will be thrown with error code {@code commit_unknown_result} (1021). + * The {@link #onError} function regards this exception as a retryable one, so + * retry loops that don't specifically detect {@code commit_unknown_result} could end + * up executing a transaction twice. For more information, see the FoundationDB + * Developer Guide documentation. + * + * If any operation is performed on a transaction after a commit has been + * issued but before it has returned, both the commit and the operation will + * throw an error code {@code used_during_commit}(2017). In this case, all + * subsequent operations on this transaction will throw this error. + */ + public CompletableFuture commit(); + + /** + * Gets the version number at which a successful commit modified the database. + * This must be called only after the successful (non-error) completion of a call + * to {@link #commit()} on this {@code Transaction}, or the behavior is undefined. + * Read-only transactions do not modify the database when committed and will have + * a committed version of -1. Keep in mind that a transaction which reads keys and + * then sets them to their current values may be optimized to a read-only transaction. + * + * @return the database version at which the commit succeeded + */ + public Long getCommittedVersion(); + + /** + * Returns a future which will contain the versionstamp which was used by any versionstamp + * operations in this transaction. The future will be ready only after the successful + * completion of a call to {@link #commit()} on this {@code Transaction}. Read-only + * transactions do not modify the database when committed and will result in the future + * completing with an error. Keep in mind that a transaction which reads keys and then sets + * them to their current values may be optimized to a read-only transaction. + * + * @return a future containing the versionstamp which was used for any versionstamp operations + * in this transaction + */ + public CompletableFuture getVersionstamp(); + + /** + * Resets a transaction and returns a delayed signal for error recovery. If the error + * encountered by the {@code Transaction} could not be recovered from, the returned + * {@code CompletableFuture} will be set to an error state. + * + * The current {@code Transaction} object will be invalidated by this call and will throw errors + * when used. The newly reset {@code Transaction} will be returned through the {@code CompletableFuture} + * if the error was retryable. + * + * If the error is not retryable, then no reset {@code Transaction} is returned, leaving this + * {@code Transaction} permanently invalidated. + * + * @param e the error caught while executing get()s and set()s on this {@code Transaction} + * @return a {@code CompletableFuture} to be set with a reset {@code Transaction} object to retry the transaction + */ + public CompletableFuture onError(Throwable e); + + /** + * Resets a {@code Transaction} to its initial state after creation and returns it as a + * new object. The current {@code Transaction} object is invalidated and will throw errors when used. + * This clears all information about mutations, conflict information built from {@code get}s, + * and the read version. This does not clear any options set on this {@code Transaction} via + * a {@link TransactionOptions} object. + * + * @return a new {@code Transaction} object with reset state. + */ + public Transaction reset(); + + /** + * Cancels the {@code Transaction}. All pending and any future uses of the + * {@code Transaction} will throw an {@link RuntimeException}. + */ + public void cancel(); + + /** + * Creates a watch that will become ready when it reports a change to + * the value of the specified key.
    + *
    + * A watch's behavior is relative to the transaction that created it. A + * watch will report a change in relation to the key's value as readable by + * that transaction. The initial value used for comparison is either that of + * the transaction's read version or the value as modified by the transaction + * itself prior to the creation of the watch. If the value changes and then + * changes back to its initial value, the watch might not report the change.
    + *
    + * Until the transaction that created it has been committed, a watch will + * not report changes made by other transactions. In contrast, a watch + * will immediately report changes made by the transaction itself. Watches + * cannot be created if the transaction has set + * {@link TransactionOptions#setReadYourWritesDisable()}, and an attempt to do + * so will raise a {@code watches_disabled} exception.
    + *
    + * If the transaction used to create a watch encounters an exception during + * commit, then the watch will be set with that exception. A transaction whose + * commit result is unknown will set all of its watches with the + * {@code commit_unknown_result} exception. If an uncommitted transaction is + * reset or destroyed, then any watches it created will be set with the + * {@code transaction_cancelled} exception.
    + *
    + * By default, each database connection can have no more than 10,000 watches + * that have not yet reported a change. When this number is exceeded, an + * attempt to create a watch will raise a {@code too_many_watches} exception. + * Because a watch outlives the transaction that creates it, any watch that is no + * longer needed should be cancelled.
    + *
    + * NOTE: calling code must call {@link Transaction#commit()} for + * the watch to be registered with the database. + * + * @param key the key to watch for changes in value + * + * @return a {@code CompletableFuture} that will become ready when the value changes + * + * @throws FDBException if too many watches have been created on this database. The + * limit defaults to 10,000 and can be modified with a call to + * {@link DatabaseOptions#setMaxWatches(long)}. + */ + public CompletableFuture watch(byte[] key) throws FDBException; + + /** + * Returns the {@link Database} that this {@code Transaction} is interacting + * with. + * + * @return the {@link Database} object + */ + public Database getDatabase(); + + /** + * Run a function once against this {@code Transaction}. This call blocks while + * user code is executing, returning the result of that code on completion. + * + * @return the return value of {@code retryable} + */ + @Override + public T run(Function retryable); + + /** + * Run a function once against this {@code Transaction}. This call returns + * immediately with a {@code CompletableFuture} handle to the result. + * + * @return a {@code CompletableFuture} that will be set to the return value of {@code retryable} + */ + @Override + public CompletableFuture runAsync( + Function> retryable); + +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/TransactionContext.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/TransactionContext.java new file mode 100644 index 0000000000..386911d238 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/TransactionContext.java @@ -0,0 +1,63 @@ +/* + * TransactionContext.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +/** + * A context in which a {@code Transaction} is available for database operations. The + * behavior of the methods specified in this interface, especially in the face + * errors, is implementation specific. In particular, some implementations will + * run {@link Function}s multiple times (retry) when certain errors are encountered. + * Therefore a {@code Function} should be prepared to be called more than once. This + * consideration means that a {@code Function} should use caution when directly + * modifying state in a class, especially in a way that could be observed were that + * {@code Function} to not complete successfully. + */ +public interface TransactionContext extends ReadTransactionContext { + /** + * Runs a function in this context that takes a transaction. Depending on the type of + * context, this may execute the supplied function multiple times if an error is + * encountered. This method is blocking -- control will not return from this call + * until work is complete. + * + * @param retryable the block of logic to execute against a {@link Transaction} + * in this context + * + * @return a result of the last call to {@code retryable} + */ + T run(Function retryable); + + /** + * Runs a function in this context that takes a transaction. Depending on the type of + * context, this may execute the supplied function multiple times if an error is + * encountered. This call is non-blocking -- control flow will return immediately + * with a {@code CompletableFuture} that will be set when the process is complete. + * + * @param retryable the block of logic to execute against a {@link Transaction} + * in this context + * + * @return a {@code CompletableFuture} that will be set to the value returned by the last call + * to {@code retryable} + */ + CompletableFuture runAsync(Function> retryable); +} \ No newline at end of file diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/AsyncIterable.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/AsyncIterable.java new file mode 100644 index 0000000000..8a6b511e08 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/AsyncIterable.java @@ -0,0 +1,51 @@ +/* + * AsyncIterable.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + + +/** + * A collection of elements that can be iterated over in a non-blocking fashion. + * + * @param the type of element yielded from iteration + */ +public interface AsyncIterable extends Iterable { + /** + * Gets a non-blocking iterator to be used to enumerate all values. + * + * @return a handle to be used for non-blocking iteration + */ + @Override + public AsyncIterator iterator(); + + /** + * Asynchronously return the results of this operation as a {@code List}. This is + * added as a convenience to users and an opportunity for providers + * of this interface to optimize large operations. + * + * @see AsyncUtil#collect(AsyncIterable) + * + * @return a {@code CompletableFuture} that will be set to contents of this operation + */ + public CompletableFuture> asList(); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/AsyncIterator.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/AsyncIterator.java new file mode 100644 index 0000000000..5c8faf0062 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/AsyncIterator.java @@ -0,0 +1,88 @@ +/* + * AsyncIterator.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.concurrent.CompletableFuture; + +import com.apple.cie.foundationdb.Disposable; + +/** + * A version of {@code Iterator} that allows for non-blocking iteration over elements. + * Calls to {@link #next()} will not block if {@link #onHasNext()} has been called + * since the last call to {@code next()} and the {@code CompletableFuture} returned from + * {@code onHasNext()} has completed. + * + * @param the type of object yielded by {@code next()} + */ +public interface AsyncIterator extends Iterator, Disposable { + /** + * Returns a asynchronous signal for the presence of more elements in the sequence. + * Once the future returned by {@link #onHasNext()} is ready, the next call to + * {@link #next} will not block. + * + * @return a {@code CompletableFuture} that will be set to {@code true} if {@code next()} + * would return another element without blocking or to {@code false} if there are + * no more elements in the sequence. + */ + public CompletableFuture onHasNext(); + + /** + * Blocking call to determine if the sequence contains more elements. This call + * is equivalent to calling {@code onHasNext().get()}. + * + * @see AsyncIterator#onHasNext() + * + * @return {@code true} if there are more elements in the sequence, {@code false} + * otherwise. + */ + @Override + public boolean hasNext(); + + /** + * Returns the next element in the sequence. This will not block if, since the + * last call to {@code next()}, {@link #onHasNext()} was called and the resulting + *

    FIXME!!!!

    has completed or the blocking call {@link #hasNext()} was called + * and has returned. It is legal, therefore, to make a call to {@code next()} without a + * preceding call to + * {@link #hasNext()} or {@link #onHasNext()}, but that invocation of {@code next()} + * may block on remote operations. + * + * @return the next element in the sequence, blocking if necessary. + * + * @throws NoSuchElementException if the sequence has been exhausted. + */ + @Override + public T next(); + + /** + * Cancels any outstanding asynchronous work associated with this {@code AsyncIterator}. + */ + public void cancel(); + + /** + * Cancel this {@code AsyncIterable} and dispose of associated resources. Equivalent + * to calling {@link AsyncIterator#cancel()}. + */ + @Override + public void dispose(); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/AsyncUtil.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/AsyncUtil.java new file mode 100644 index 0000000000..1d566b4945 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/AsyncUtil.java @@ -0,0 +1,378 @@ +/* + * AsyncUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import static com.apple.cie.foundationdb.FDB.DEFAULT_EXECUTOR; + +import com.apple.cie.foundationdb.FDBException; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; +import java.util.function.Function; + +/** + * Provided utilities for using and manipulating {@link CompletableFuture}s. + */ +public class AsyncUtil { + /** + * Run {@code Function} {@code func}, returning all caught exceptions as a + * {@code CompletableFuture} in an error state. + * + * @param func the {@code Function} to run + * @param value the input to pass to {@code func} + * + * @return the output of {@code func}, or a {@code CompletableFuture} carrying any exception + * caught in the process. + */ + public static CompletableFuture applySafely( Function> func, I value ) { + try { + return func.apply(value); + } catch (RuntimeException e) { + CompletableFuture future = new CompletableFuture(); + future.completeExceptionally(e); + return future; + } + } + + /** + * Iterates over a set of items and returns the result as a list. + * + * @param iterable the source of data over which to iterate + * + * @return a {@code CompletableFuture} which will be set to the amalgamation of results + * from iteration. + */ + public static CompletableFuture> collect(final AsyncIterable iterable) { + return collect(iterable, DEFAULT_EXECUTOR); + } + + /** + * Iterates over a set of items and returns the result as a list. + * + * @param iterable the source of data over which to iterate + * @param executor the {@link Executor} to use for asynchronous operations + * + * @return a {@code CompletableFuture} which will be set to the amalgamation of results + * from iteration. + */ + public static CompletableFuture> collect(final AsyncIterable iterable, final Executor executor) { + final AsyncIterator it = iterable.iterator(); + final List accumulator = new LinkedList(); + + // The condition of the while loop is simply "onHasNext()" returning true + Function> condition = new Function>() { + @Override + public CompletableFuture apply(Void v) { + return it.onHasNext().thenApply(new Function() { + @Override + public Boolean apply(Boolean o) { + if(o) { + accumulator.add(it.next()); + } + return o; + } + }); + } + }; + + CompletableFuture complete = whileTrue(condition, executor); + CompletableFuture> result = tag(complete, accumulator); + + return result; + } + + /** + * Map an {@code AsyncIterable} into an {@code AsyncIterable} of another type or with + * each element modified in some fashion. + * + * @param iterable input + * @param func mapping function applied to each element + * @return a new iterable with each element mapped to a different value + */ + public static AsyncIterable mapIterable(final AsyncIterable iterable, + final Function func) { + return new AsyncIterable() { + @Override + public AsyncIterator iterator() { + final AsyncIterator it = iterable.iterator(); + return new AsyncIterator() { + + @Override + public void remove() { + it.remove(); + } + + @Override + public CompletableFuture onHasNext() { + return it.onHasNext(); + } + + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public T next() { + return func.apply(it.next()); + } + + @Override + public void cancel() { + it.cancel(); + } + + @Override + public void dispose() { + it.dispose(); + } + }; + } + + @Override + public CompletableFuture> asList() { + return iterable.asList().thenApply(new Function, List>() { + @Override + public List apply(List o) { + ArrayList out = new ArrayList(o.size()); + for(V in : o) + out.add(func.apply(in)); + return out; + } + }); + } + }; + } + + private static class LoopPartial implements BiFunction { + final Function> body; + final CompletableFuture done; + final Executor executor; + + public LoopPartial(Function> body, Executor executor) { + this.body = body; + this.done = new CompletableFuture<>(); + this.executor = executor; + } + + @Override + public Void apply(Boolean more, Throwable error) { + if (error != null) { + done.completeExceptionally(error); + } else { + while (true) { + if (!more) { + done.complete(null); + break; + } + CompletableFuture result; + try { + result = body.apply(null); + } catch (Exception e) { + done.completeExceptionally(e); + break; + } + if (result.isDone()) { + if (result.isCompletedExceptionally()) { + result.handle(this); + break; + } else { + more = result.join(); + } + } else { + result.handleAsync(this, executor); + break; + } + } + } + + return null; + } + + public CompletableFuture run() { + apply(true, null); + return done; + } + } + + /** + * Executes an asynchronous operation repeatedly until it returns {@code False}. + * + * @param body the asynchronous operation over which to loop + * + * @return a {@code PartialFuture} which will be set at completion of the loop. + */ + public static CompletableFuture whileTrue(Function> body) { + return whileTrue(body, DEFAULT_EXECUTOR); + } + + /** + * Executes an asynchronous operation repeatedly until it returns {@code False}. + * + * @param body the asynchronous operation over which to loop + * @param executor the {@link Executor} to use for asynchronous operations + * + * @return a {@code PartialFuture} which will be set at completion of the loop. + */ + public static CompletableFuture whileTrue(Function> body, Executor executor) { + return new LoopPartial(body, executor).run(); + } + + /** + * Maps the outcome of a task into a completion signal. Can be useful if {@code task} has + * side-effects for which all is needed is a signal of completion. + * All errors from {@code task} will be passed to the resulting {@code CompletableFuture}. + * + * @param task the asynchronous process for which to signal completion + * + * @return a newly created {@code CompletableFuture} that is set when {@code task} completes + */ + public static CompletableFuture success(CompletableFuture task) { + return task.thenApply(new Function() { + @Override + public Void apply(V o) { + return null; + } + }); + } + + /** + * Maps the readiness of a {@link CompletableFuture} into a completion signal. When + * the given {@link CompletableFuture} is set to a value or an error, the returned {@link CompletableFuture} + * will be set to null. The returned {@link CompletableFuture} will never be set to an error unless + * it is explicitly cancelled. + * + * @param task the asynchronous process to monitor the readiness of + * + * @return a new {@link CompletableFuture} that is set when {@code task} is ready. + */ + public static CompletableFuture whenReady(CompletableFuture task) { + return task.thenApply(new Function() { + @Override + public Void apply(V o) { + return null; + } + }).exceptionally(new Function() { + @Override + public Void apply(Throwable o) { + return null; + } + }); + } + + public static CompletableFuture composeExceptionally(CompletableFuture task, Function> fn) { + return task.handle(new BiFunction() { + @Override + public Throwable apply(V v, Throwable e) { + return e; + } + }).thenCompose(new Function>() { + @Override + public CompletableFuture apply(Throwable e) { + if (e != null) { + return fn.apply(e); + } else { + return task; + } + } + }); + } + + /** + * Collects the results of many asynchronous processes into one asynchronous output. If + * any of the tasks returns an error, the output is set to that error. + * + * @param tasks the tasks whose output is to be added to the output + * + * @return a {@code CompletableFuture} that will be set to the collective result of the tasks + */ + public static CompletableFuture> getAll(final Collection> tasks) { + return whenAll(tasks).thenApply(new Function>() { + @Override + public List apply(Void o) { + List result = new ArrayList(); + for(CompletableFuture f : tasks) { + assert(f.isDone()); + result.add(f.getNow(null)); + } + return result; + } + }); + } + + /** + * Replaces the output of an asynchronous task with a predetermined value. + * + * @param task the asynchronous process whose output is to be replaced + * + * @param value the predetermined value to be returned on success of {@code task} + * + * @return a {@code CompletableFuture} that will be set to {@code value} on completion of {@code task} + */ + public static CompletableFuture tag(CompletableFuture task, final V value) { + return task.thenApply(new Function() { + @Override + public V apply(T o) { + return value; + } + }); + } + + /** + * Return a {@code CompletableFuture} that will be set when any of the {@code PartialFuture} + * inputs are done. A {@code CompletableFuture} is done both on success and failure. + * + * @param input the list of {@code PartialFuture}s to monitor. This list + * must not be modified during the execution of this call. + * + * @return a signal that will be set when any of the {@code CompletableFuture}s are done + */ + public static CompletableFuture whenAny(final Collection> input) { + @SuppressWarnings("unchecked") + CompletableFuture[] array = (CompletableFuture[]) input.toArray(new CompletableFuture[input.size()]); + CompletableFuture anyOf = CompletableFuture.anyOf(array); + return success(anyOf); + } + + /** + * Return a {@code CompletableFuture} that will be set when all the {@code PartialFuture} + * inputs are done. A {@code CompletableFuture} is done both on success and failure. + * + * @param input the list of {@code PartialFuture}s to monitor. This list + * must not be modified during the execution of this call. + * + * @return a signal that will be set when all of the {@code CompletableFuture}s are done + */ + public static CompletableFuture whenAll(final Collection> input) { + @SuppressWarnings("unchecked") + CompletableFuture[] array = (CompletableFuture[]) input.toArray(new CompletableFuture[input.size()]); + return CompletableFuture.allOf(array); + } + + private AsyncUtil() {} +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/Cancellable.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/Cancellable.java new file mode 100644 index 0000000000..4fd3980b68 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/Cancellable.java @@ -0,0 +1,37 @@ +/* + * Cancellable.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +/** + * Describes an operation or signal that can be cancelled. Cancellation will be assumed + * to "chain" -- that is, once an {@code cancel()} is called, an operation will be cancelled + * even if there are other consumers of the operation which have not cancelled. + * + */ +public interface Cancellable { + /** + * Cancels this operation or signal. This will end the work that would have been done + * and notify all consumers of the operation that a result will not be returned. It + * is not an error to call this method on an operation that has already completed or + * already been cancelled. This method will not block or throw non-fatal exceptions. + */ + public abstract void cancel(); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/CloneableException.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/CloneableException.java new file mode 100644 index 0000000000..c260479288 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/CloneableException.java @@ -0,0 +1,34 @@ +/* + * CloneableException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +/** + * An {@link Exception} that can be duplicated with a new backtrace. + */ +public interface CloneableException { + /** + * Create and return a new {@code Exception} that has a backtrace including the + * calling line of code. + * + * @return a newly created {@code Exception}. + */ + public Exception retargetClone(); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/package-info.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/package-info.java new file mode 100644 index 0000000000..38619798b4 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/async/package-info.java @@ -0,0 +1,25 @@ +/* + * package-info.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides additional constructs for asynchronous programming against Java's CompletableFutures. + * + */ +package com.apple.cie.foundationdb.async; diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/Directory.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/Directory.java new file mode 100644 index 0000000000..25c4f00ff2 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/Directory.java @@ -0,0 +1,375 @@ +/* + * Directory.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import static com.apple.cie.foundationdb.directory.DirectoryLayer.EMPTY_BYTES; +import static com.apple.cie.foundationdb.directory.DirectoryLayer.EMPTY_PATH; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import com.apple.cie.foundationdb.ReadTransactionContext; +import com.apple.cie.foundationdb.TransactionContext; + +/** + * Represents a directory in the {@code DirectoryLayer}. A {@code Directory} stores the path + * at which it is located and the layer that was used to create it. + * + * The {@code Directory} interface contains methods to operate on itself and its + * subdirectories. + */ +public interface Directory { + + /** + * Gets the path represented by this {@code Directory}. + * + * @return this {@code Directory}'s path + */ + List getPath(); + + /** + * Gets the layer byte string that was stored when this {@code Directory} + * was created. + * + * @return this {@code Directory}'s layer byte string + */ + byte[] getLayer(); + + /** + * Get the {@link DirectoryLayer} that was used to create this {@code Directory}. + * + * @return the {@link DirectoryLayer} that created this {@link Directory} + */ + DirectoryLayer getDirectoryLayer(); + + /** + * Creates or opens the subdirectory of this {@code Directory} located at {@code subpath} + * (creating parent directories, if necessary). + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link CompletableFuture} which will be set to the created or opened {@link DirectorySubspace} + */ + default CompletableFuture createOrOpen(TransactionContext tcx, List subpath) { + return createOrOpen(tcx, subpath, EMPTY_BYTES); + } + + /** + * Creates or opens the subdirectory of this {@code Directory} located at {@code subpath} + * (creating parent directories, if necessary). If the directory is new, then the {@code layer} + * byte string will be recorded as its layer. If the directory already exists, the {@code layer} + * byte string will be compared against the {@code layer} set when the directory was created. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link MismatchedLayerException} - if the directory has already been created with a different {@code layer} byte string
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @param layer a {@code byte[]} specifying a layer to set on a new directory or check for on an existing directory + * @return a {@link CompletableFuture} which will be set to the created or opened {@link DirectorySubspace} + */ + CompletableFuture createOrOpen(TransactionContext tcx, List subpath, byte[] layer); + + /** + * Opens the subdirectory of this {@code Directory} located at {@code subpath}. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if the directory does not exist
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link CompletableFuture} which will be set to the opened {@link DirectorySubspace} + */ + default CompletableFuture open(ReadTransactionContext tcx, List subpath) { + return open(tcx, subpath, EMPTY_BYTES); + } + + /** + * Opens the subdirectory of this {@code Directory} located at {@code subpath}. + * The {@code layer} byte string will be compared against the {@code layer} set when + * the directory was created. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link MismatchedLayerException} - if the directory was created with a different {@code layer} byte string
    • + *
    • {@link NoSuchDirectoryException} - if the directory does not exist
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @param layer a {@code byte[]} specifying the expected layer + * @return a {@link CompletableFuture} which will be set to the opened {@link DirectorySubspace} + */ + CompletableFuture open(ReadTransactionContext tcx, List subpath, byte[] layer); + + /** + * Creates a subdirectory of this {@code Directory} located at {@code subpath} + * (creating parent directories if necessary). + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link DirectoryAlreadyExistsException} - if the given directory already exists
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link CompletableFuture} which will be set to the created {@link DirectorySubspace} + */ + default CompletableFuture create(TransactionContext tcx, List subpath) { + return create(tcx, subpath, EMPTY_BYTES); + } + + /** + * Creates a subdirectory of this {@code Directory} located at {@code subpath} + * (creating parent directories if necessary). The {@code layer} byte string will be recorded as + * the new directory's layer and checked by future calls to {@link #open(ReadTransactionContext, List, byte[])}. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link DirectoryAlreadyExistsException} - if the given directory already exists
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @param layer a {@code byte[]} specifying a layer to set for the directory + * @return a {@link CompletableFuture} which will be set to the created {@link DirectorySubspace} + */ + default CompletableFuture create(TransactionContext tcx, List subpath, byte[] layer) { + return create(tcx, subpath, layer, null); + } + + /** + * Creates a subdirectory of this {@code Directory} located at {@code subpath} + * (creating parent directories if necessary). The {@code layer} byte string will be recorded as + * the new directory's layer and checked by future calls to {@link #open(ReadTransactionContext, List, byte[])}. + * The specified {@code prefix} will be used for this directory's contents instead of allocating a + * prefix automatically. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link DirectoryAlreadyExistsException} - if the given directory already exists
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @param layer a {@code byte[]} specifying a layer to set for the directory + * @param prefix a {@code byte[]} specifying the key prefix to use for the directory's contents + * @return a {@link CompletableFuture} which will be set to the created {@link DirectorySubspace} + */ + CompletableFuture create(TransactionContext tcx, List subpath, byte[] layer, byte[] prefix); + + /** + * Moves this {@code Directory} to the specified {@code newAbsolutePath}. + *

    + * There is no effect on the physical prefix of the given directory, or on clients that already + * have the directory open. + *

    + *

    + * It is invalid to move a directory to: + *

    + *
      + *
    • A location where a directory already exists
    • + *
    • A location whose parent does not exist
    • + *
    • A subdirectory of itself
    • + *
    • A different partition
    • + *
    + * + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if this {@code Directory} doesn't exist
    • + *
    • {@link DirectoryAlreadyExistsException} - if a directory already exists at {@code newAbsolutePath}
    • + *
    • {@link DirectoryMoveException} - if an invalid move location is specified
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param newAbsolutePath a {@code List} specifying the new absolute path for this {@code Directory} + * @return a {@link CompletableFuture} which will be set to the {@link DirectorySubspace} for this {@code Directory} + * at its new location. + */ + CompletableFuture moveTo(TransactionContext tcx, List newAbsolutePath); + + /** + * Moves the subdirectory of this {@code Directory} located at {@code oldSubpath} to {@code newSubpath}. + * + *

    + * There is no effect on the physical prefix of the given directory, or on clients that already + * have the directory open. + *

    + *

    + * It is invalid to move a directory to: + *

    + *
      + *
    • A location where a directory already exists
    • + *
    • A location whose parent does not exist
    • + *
    • A subdirectory of itself
    • + *
    • A different partition
    • + *
    + * + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no {@code Directory} exists at {@code oldSubpath}
    • + *
    • {@link DirectoryAlreadyExistsException} - if a directory already exists at {@code newSubpath}
    • + *
    • {@link DirectoryMoveException} - if an invalid move location is specified
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param oldSubpath a {@code List} specifying the subpath of the directory to move + * @param newSubpath a {@code List} specifying the subpath to move to + * @return a {@link CompletableFuture} which will be set to the {@link DirectorySubspace} for this {@code Directory} + * at its new location. + */ + CompletableFuture move(TransactionContext tcx, List oldSubpath, List newSubpath); + + /** + * Removes this {@code Directory} and all of its subdirectories, as well as all of their contents. + * This should not be called on the root directory, or it will result in the returned future being + * set to a {@link DirectoryException}. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if this {@code Directory} doesn't exist
    • + *
    • {@link DirectoryException} - if this is called on the root directory
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @return a {@link CompletableFuture} which will be set once this {@code Directory} has been removed + */ + default CompletableFuture remove(TransactionContext tcx) { + return remove(tcx, EMPTY_PATH); + } + + /** + * Removes the subdirectory of this {@code Directory} located at {@code subpath} and all of its subdirectories, + * as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no directory exists at {@code subpath}
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link CompletableFuture} which will be set once the {@code Directory} has been removed + */ + CompletableFuture remove(TransactionContext tcx, List subpath); + + /** + * Removes this {@code Directory} and all of its subdirectories, as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @return a {@link CompletableFuture} which will be set to true once this {@code Directory} has been removed, + * or false if it didn't exist. + */ + default CompletableFuture removeIfExists(TransactionContext tcx) { + return removeIfExists(tcx, EMPTY_PATH); + } + + /** + * Removes the subdirectory of this {@code Directory} located at {@code subpath} and all of its subdirectories, + * as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link CompletableFuture} which will be set to true once the {@code Directory} has been removed, + * or false if it didn't exist. + */ + CompletableFuture removeIfExists(TransactionContext tcx, List subpath); + + /** + * List the subdirectories of this directory. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if this {@code Directory} doesn't exists
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @return a {@link CompletableFuture} which will be set to a {@code List} of names of the subdirectories + * of this {@code Directory}. Each name is a unicode string representing the last component of a + * subdirectory's path. + */ + default CompletableFuture> list(ReadTransactionContext tcx) { + return list(tcx, EMPTY_PATH); + } + + /** + * List the subdirectories of this directory at a given {@code subpath}. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no directory exists at {@code subpath}
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link CompletableFuture} which will be set to a {@code List} of names of the subdirectories + * of the directory at {@code subpath}. Each name is a unicode string representing the last component + * of a subdirectory's path. + */ + CompletableFuture> list(ReadTransactionContext tcx, List subpath); + + /** + * Checks if this {@code Directory} exists. + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @return a {@link CompletableFuture} which will be set to {@code true} if this {@code Directory} exists, or {@code false} if it + * doesn't + */ + default CompletableFuture exists(ReadTransactionContext tcx) { + return exists(tcx, EMPTY_PATH); + } + + /** + * Checks if the subdirectory of this {@code Directory} located at {@code subpath} exists. + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link CompletableFuture} which will be set to {@code true} if the specified subdirectory exists, or {@code false} if it + * doesn't + */ + CompletableFuture exists(ReadTransactionContext tcx, List subpath); +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryAlreadyExistsException.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryAlreadyExistsException.java new file mode 100644 index 0000000000..7f4e64f4f1 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryAlreadyExistsException.java @@ -0,0 +1,35 @@ +/* + * DirectoryAlreadyExistsException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +/** + * A {@link DirectoryException} that is thrown when a directory is being + * created at or moved to a location that already has a directory present. + */ +@SuppressWarnings("serial") +public class DirectoryAlreadyExistsException extends DirectoryException { + DirectoryAlreadyExistsException(List path) { + super("Directory already exists", path); + } +} + diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryException.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryException.java new file mode 100644 index 0000000000..a99e925587 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryException.java @@ -0,0 +1,40 @@ +/* + * DirectoryException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +/** + * An {@link Exception} that is thrown by the directory layer corresponding + * to a specific path. + */ +@SuppressWarnings("serial") +public class DirectoryException extends RuntimeException { + /** + * The directory path that was responsible for the error. + */ + public final List path; + + DirectoryException(String baseMsg, List path) { + super(baseMsg + ": path=" + DirectoryUtil.pathStr(path)); + this.path = path; + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryLayer.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryLayer.java new file mode 100644 index 0000000000..39cda2a0ee --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryLayer.java @@ -0,0 +1,1344 @@ +/* + * DirectoryLayer.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Random; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.MutationType; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.ReadTransaction; +import com.apple.cie.foundationdb.ReadTransactionContext; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncIterator; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.subspace.Subspace; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * Provides a class for managing directories in FoundationDB. + * + *

    + * The FoundationDB API provides directories as a tool for managing related + * {@link Subspace}s. Directories are a recommended approach for administering + * applications. Each application should create or open at least one directory + * to manage its subspaces. + * + * For general guidance on directory usage, see the discussion in the + * Developer Guide. + *

    + *

    + * Directories are identified by hierarchical paths analogous to the paths + * in a Unix-like file system. A path is represented as a List of strings. + * Each directory has an associated subspace used to store its content. The + * layer maps each path to a short prefix used for the corresponding + * subspace. In effect, directories provide a level of indirection for + * access to subspaces. + *

    + */ +public class DirectoryLayer implements Directory +{ + private static final Charset UTF_8 = Charset.forName("UTF-8"); + private static final byte[] LITTLE_ENDIAN_LONG_ONE = { 1, 0, 0, 0, 0, 0, 0, 0 }; + private static final byte[] HIGH_CONTENTION_KEY = "hca".getBytes(UTF_8); + private static final byte[] LAYER_KEY = "layer".getBytes(UTF_8); + private static final byte[] VERSION_KEY = "version".getBytes(UTF_8); + private static final long SUB_DIR_KEY = 0; + private static final Integer[] VERSION = { 1, 0, 0 }; + + static final byte[] EMPTY_BYTES = new byte[0]; + static final List EMPTY_PATH = Collections.emptyList(); + static final byte[] DEFAULT_NODE_SUBSPACE_PREFIX = { (byte)0xFE }; + + /** + * The default node {@link Subspace} used by a {@code DirectoryLayer} when none is specified. + */ + public static final Subspace DEFAULT_NODE_SUBSPACE = new Subspace(DEFAULT_NODE_SUBSPACE_PREFIX); + + /** + * The default content {@link Subspace} used by a {@code DirectoryLayer} when none is specified. + */ + public static final Subspace DEFAULT_CONTENT_SUBSPACE = new Subspace(); + + private final Subspace rootNode; + private final Subspace nodeSubspace; + private final Subspace contentSubspace; + private final HighContentionAllocator allocator; + private final boolean allowManualPrefixes; + + private List path = EMPTY_PATH; + /** + * The layer string to pass to {@link Directory#createOrOpen(TransactionContext, List, byte[])} or + * {@link Directory#create(TransactionContext, List, byte[])} to create a {@code DirectoryPartition}. + */ + public static final byte[] PARTITION_LAYER = "partition".getBytes(Charset.forName("UTF-8")); + + private static DirectoryLayer defaultDirectoryLayer = new DirectoryLayer(); + + /** + * Constructor for a {@code DirectoryLayer} formed with default node and + * content subspaces. A {@code DirectoryLayer} + * defines a new root directory. The node subspace and content subspace + * control where the directory metadata and contents, respectively, are + * stored. The default root directory has a node subspace with raw prefix + * {@code \xFE} and a content subspace with no prefix. + * Prefixes can not be specified in calls to {@link Directory#create(TransactionContext, List, byte[], byte[])}. + * + * @see #getDefault + */ + public DirectoryLayer() { + this(DEFAULT_NODE_SUBSPACE, DEFAULT_CONTENT_SUBSPACE, false); + } + + /** + * Constructor for a {@code DirectoryLayer} formed with default node and + * content subspaces. A {@code DirectoryLayer} + * defines a new root directory. The node subspace and content subspace + * control where the directory metadata and contents, respectively, are + * stored. The default root directory has a node subspace with raw prefix + * {@code \xFE} and a content subspace with no prefix. + * + * @param allowManualPrefixes whether or not prefixes can be specified in calls to + * {@link Directory#create(TransactionContext, List, byte[], byte[])} + */ + public DirectoryLayer(boolean allowManualPrefixes) { + this(DEFAULT_NODE_SUBSPACE, DEFAULT_CONTENT_SUBSPACE, allowManualPrefixes); + } + + /** + * Constructor for a {@code DirectoryLayer} formed with a specified node + * subspace and specified content subspace. A {@code DirectoryLayer} + * defines a new root directory. The node subspace and content subspace + * control where the directory metadata and contents, respectively, are + * stored. The default root directory has a node subspace with raw prefix + * {@code \xFE} and a content subspace with no prefix. Specifying more + * restrictive values for the node subspace and content subspace will allow + * using the directory layer alongside other content in a database. + * Prefixes can not be specified in calls to {@link Directory#create(TransactionContext, List, byte[], byte[])}. + * + * @param nodeSubspace a {@link Subspace} used to store directory metadata + * @param contentSubspace a {@link Subspace} used to store directory content + */ + public DirectoryLayer(Subspace nodeSubspace, Subspace contentSubspace) { + this(nodeSubspace, contentSubspace, false); + } + + /** + * Constructor for a {@code DirectoryLayer} formed with a specified node + * subspace and specified content subspace. A {@code DirectoryLayer} + * defines a new root directory. The node subspace and content subspace + * control where the directory metadata and contents, respectively, are + * stored. The default root directory has a node subspace with raw prefix + * {@code \xFE} and a content subspace with no prefix. Specifying more + * restrictive values for the node subspace and content subspace will allow + * using the directory layer alongside other content in a database. + * + * @param nodeSubspace a {@link Subspace} used to store directory metadata + * @param contentSubspace a {@link Subspace} used to store directory content + * @param allowManualPrefixes whether or not prefixes can be specified in calls to + * {@link Directory#create(TransactionContext, List, byte[], byte[])} + */ + public DirectoryLayer(Subspace nodeSubspace, Subspace contentSubspace, boolean allowManualPrefixes) { + this.nodeSubspace = nodeSubspace; + this.contentSubspace = contentSubspace; + // The root node is the one whose contents are the node subspace + this.rootNode = nodeSubspace.get(nodeSubspace.getKey()); + this.allocator = new HighContentionAllocator(rootNode.get(HIGH_CONTENTION_KEY)); + this.allowManualPrefixes = allowManualPrefixes; + } + + /** + * Creates a new {@code DirectoryLayer} formed with a specified node subspace and default content subspace. + * Prefixes can not be specified in calls to {@link Directory#create(TransactionContext, List, byte[], byte[])}. + * + * @param node_subspace a {@link Subspace} used to store directory metadata + * @return a {@code DirectoryLayer} formed with {@code node_subspace} and a default content subspace + */ + public static Directory createWithNodeSubspace(Subspace node_subspace) { + return new DirectoryLayer(node_subspace, DEFAULT_CONTENT_SUBSPACE); + } + + /** + * Creates a new {@code DirectoryLayer} formed with a default node subspace and specified content subspace. + * Prefixes can not be specified in calls to {@link Directory#create(TransactionContext, List, byte[], byte[])}. + * + * @param content_subspace a {@link Subspace} used to store directory content + * @return a {@code DirectoryLayer} formed with a {@code content_subspace} and a default node subspace + */ + public static Directory createWithContentSubspace(Subspace content_subspace) { + return new DirectoryLayer(DEFAULT_NODE_SUBSPACE, content_subspace); + } + + /** + * Gets the default instance of the DirectoryLayer. The default instance + * is created with the default node and content subspaces. + * + * Prefixes can not be specified in calls to {@link Directory#create(TransactionContext, List, byte[], byte[])}. + * + * @return the default {@code DirectoryLayer} + */ + public static DirectoryLayer getDefault() { + return defaultDirectoryLayer; + } + + /** + * Tests whether this {@code DirectoryLayer} is equal to {@code rhs}. + * Two {@code DirectoryLayer}s are equal if they have the same node subspace, + * content subspace, and path. + * + * @param rhs the object to check for equality + * @return {@code true} if this {@code DirectoryLayer} and {@code rhs} are equal + */ + @Override + public boolean equals(Object rhs) { + if(this == rhs) { + return true; + } + if(rhs == null || getClass() != rhs.getClass()) { + return false; + } + DirectoryLayer other = (DirectoryLayer)rhs; + + return (path == other.path || path.equals(other.path)) + && nodeSubspace.equals(other.nodeSubspace) + && contentSubspace.equals(other.contentSubspace); + } + + /** + * Sets path of directory to {@code path} + * + * @param path a {@code List} specifying a path + */ + void setPath(List path) { + this.path = path; + } + + /** + * Gets the path for the root node of this {@code DirectoryLayer}. Normally constructed + * {@code DirectoryLayer}s have an empty path, but {@code DirectoryLayer}s returned by + * {@link Directory#getDirectoryLayer} for {@link Directory}s inside of a partition + * could have non-empty paths. + * + * @return the path for the root node of this {@code DirectoryLayer} + */ + @Override + public List getPath() { + return Collections.unmodifiableList(path); + } + + /** + * Returns the layer byte string for this {@code DirectoryLayer}, which is always an empty + * array. + * + * @return an empty byte array + */ + @Override + public byte[] getLayer() { + return EMPTY_BYTES; + } + + /** + * Returns {@code this}. + * + * @return {@code this} + */ + @Override + public DirectoryLayer getDirectoryLayer() { + return this; + } + + /** + * Creates or opens the directory located at {@code path}(creating parent directories, if necessary). + * If the directory is new, then the {@code layer} byte string will be recorded as its layer. + * If the directory already exists, the {@code layer} byte string will be compared against the {@code layer} + * set when the directory was created. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link MismatchedLayerException} - if the directory has already been created with a different {@code layer} byte string
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @param layer a {@code byte[]} specifying a layer to set on a new directory or check for on an existing directory + * @return a {@link CompletableFuture} which will be set to the created or opened {@link DirectorySubspace} + */ + @Override + public CompletableFuture createOrOpen(TransactionContext tcx, final List path, final byte[] layer) { + return tcx.runAsync(new Function>() { + @Override + public CompletableFuture apply(Transaction tr) { + return createOrOpenInternal(tr, tr, path, layer, null, true, true); + } + }); + } + + /** + * Opens the directory located at {@code path}. + * The {@code layer} byte string will be compared against the {@code layer} set when + * the directory was created. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link MismatchedLayerException} - if the directory was created with a different {@code layer} byte string
    • + *
    • {@link NoSuchDirectoryException} - if the directory does not exist
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @param layer a {@code byte[]} specifying the expected layer + * @return a {@link CompletableFuture} which will be set to the opened {@link DirectorySubspace} + */ + @Override + public CompletableFuture open(ReadTransactionContext tcx, final List path, final byte[] layer) { + return tcx.readAsync(new Function>() { + @Override + public CompletableFuture apply(ReadTransaction rtr) { + return createOrOpenInternal(rtr, null, path, layer, null, false, true); + } + }); + } + + /** + * Creates a directory located at {@code path} (creating parent directories if necessary). + * The {@code layer} byte string will be recorded as the new directory's layer and checked by + * future calls to {@link #open(ReadTransactionContext, List, byte[])}. The specified {@code prefix} + * will be used for this directory's contents instead of allocating a prefix automatically. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link DirectoryAlreadyExistsException} - if the given directory already exists
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path of this {@code Directory} + * @param layer a {@code byte[]} specifying a layer to set for the directory + * @param prefix a {@code byte[]} specifying the key prefix to use for the directory's contents + * @return a {@link CompletableFuture} which will be set to the created {@link DirectorySubspace} + */ + @Override + public CompletableFuture create(TransactionContext tcx, final List path, final byte[] layer, final byte[] prefix) { + return tcx.runAsync(new Function>() { + @Override + public CompletableFuture apply(Transaction tr) { + return createOrOpenInternal(tr, tr, path, layer, prefix, true, false); + } + }); + } + + /** + * This method should not be called on a {@code DirectoryLayer}. Calling this method will result in the returned + * {@link CompletableFuture} being set to a {@link DirectoryMoveException}. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link DirectoryMoveException}
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param newAbsolutePath a {@code List} specifying a path + * @return a {@link CompletableFuture} which will be set to a {@link DirectoryMoveException} + */ + @Override + public CompletableFuture moveTo(TransactionContext tcx, List newAbsolutePath) { + CompletableFuture future = new CompletableFuture(); + future.completeExceptionally(new DirectoryMoveException("The root directory cannot be moved.", path, newAbsolutePath)); + return future; + } + + /** + * Moves the directory located at {@code oldPath} to {@code newPath}. + * + *

    + * There is no effect on the physical prefix of the given directory, or on clients that already + * have the directory open. + *

    + *

    + * It is invalid to move a directory to: + *

    + *
      + *
    • A location where a directory already exists
    • + *
    • A location whose parent does not exist
    • + *
    • A subdirectory of itself
    • + *
    • A different partition
    • + *
    + * + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no {@code Directory} exists at {@code oldPath}
    • + *
    • {@link DirectoryAlreadyExistsException} - if a directory already exists at {@code newPath}
    • + *
    • {@link DirectoryMoveException} - if an invalid move location is specified
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param oldPath a {@code List} specifying the path of the directory to move + * @param newPath a {@code List} specifying the path to move to + * @return a {@link CompletableFuture} which will be set to the {@link DirectorySubspace} for this {@code Directory} + * at its new location. + */ + @Override + public CompletableFuture move(final TransactionContext tcx, final List oldPath, final List newPath) { + final List oldPathCopy = new ArrayList(oldPath); + final List newPathCopy = new ArrayList(newPath); + + return tcx.runAsync(new Function>() { + @Override + public CompletableFuture apply(final Transaction tr) { + return checkOrWriteVersion(tr) + .thenComposeAsync(new Function>>() { + @Override + public CompletableFuture> apply(Void ignore) { + if(oldPathCopy.size() <= newPathCopy.size() && oldPathCopy.equals(newPathCopy.subList(0, oldPathCopy.size()))) + throw new DirectoryMoveException("The destination directory cannot be a subdirectory of the source directory.", toAbsolutePath(oldPathCopy), toAbsolutePath(newPathCopy)); + + ArrayList> futures = new ArrayList>(); + futures.add(new NodeFinder(oldPathCopy).find(tr).thenComposeAsync(new NodeMetadataLoader(tr), tr.getExecutor())); + futures.add(new NodeFinder(newPathCopy).find(tr).thenComposeAsync(new NodeMetadataLoader(tr), tr.getExecutor())); + + return AsyncUtil.getAll(futures); + } + }, tr.getExecutor()) + .thenCompose(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(List nodes) { + final Node oldNode = nodes.get(0); + final Node newNode = nodes.get(1); + + if(!oldNode.exists()) + throw new NoSuchDirectoryException(toAbsolutePath(oldPathCopy)); + + if(oldNode.isInPartition(false) || newNode.isInPartition(false)) { + if(!oldNode.isInPartition(false) || !newNode.isInPartition(false) || !oldNode.path.equals(newNode.path)) + throw new DirectoryMoveException("Cannot move between partitions.", toAbsolutePath(oldPathCopy), toAbsolutePath(newPathCopy)); + + return newNode.getContents().move(tr, oldNode.getPartitionSubpath(), newNode.getPartitionSubpath()); + } + + if(newNode.exists()) + throw new DirectoryAlreadyExistsException(toAbsolutePath(newPathCopy)); + + final List parentPath = PathUtil.popBack(newPathCopy); + return new NodeFinder(parentPath).find(tr) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Node parentNode) { + if(!parentNode.exists()) + throw new NoSuchDirectoryException(toAbsolutePath(parentPath)); + + tr.set( + parentNode.subspace.get(SUB_DIR_KEY).get(getLast(newPathCopy)).getKey(), + contentsOfNode(oldNode.subspace, EMPTY_PATH, EMPTY_BYTES).getKey() + ); + + return removeFromParent(tr, oldPathCopy) + .thenApply(new Function() { + @Override + public DirectorySubspace apply(Void ignore) { + return contentsOfNode(oldNode.subspace, newPathCopy, oldNode.layer); + } + }); + } + }, tr.getExecutor()); + } + }); + } + }); + } + + /** + * Removes the directory located at {@code path} and all of its subdirectories, + * as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no directory exists at {@code path}
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @return a {@link CompletableFuture} which will be set once the {@code Directory} has been removed + */ + @Override + public CompletableFuture remove(TransactionContext tcx, List path) { + return AsyncUtil.success(removeInternal(tcx, path, true)); + } + + /** + * Removes the directory located at {@code subpath} and all of its subdirectories, + * as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @return a {@link CompletableFuture} which will be set to true once the {@code Directory} has been removed, + * or false if it didn't exist. + */ + @Override + public CompletableFuture removeIfExists(TransactionContext tcx, List path) { + return removeInternal(tcx, path, false); + } + + /** + * List the subdirectories of the directory at a given {@code path}. + * + *

    The returned {@link CompletableFuture} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no directory exists at {@code path}
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @return a {@link CompletableFuture} which will be set to a {@code List} of names of the subdirectories + * of the directory at {@code path}. Each name is a unicode string representing the last component + * of a subdirectory's path. + */ + @Override + public CompletableFuture> list(final ReadTransactionContext tcx, final List path) { + final List pathCopy = new ArrayList(path); + + return tcx.readAsync(new Function>>() { + @Override + public CompletableFuture> apply(final ReadTransaction tr) { + return checkVersion(tr) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + return new NodeFinder(pathCopy).find(tr).thenComposeAsync(new NodeMetadataLoader(tr), tr.getExecutor()); + } + }, tr.getExecutor()) + .thenComposeAsync(new Function>>() { + @Override + public CompletableFuture> apply(Node node) { + if(!node.exists()) + throw new NoSuchDirectoryException(toAbsolutePath(pathCopy)); + + if(node.isInPartition(true)) + return node.getContents().list(tr, node.getPartitionSubpath()); + + final Subspace subdir = node.subspace.get(SUB_DIR_KEY); + + return AsyncUtil.collect( + AsyncUtil.mapIterable(tr.getRange(subdir.range()), + new Function() { + @Override + public String apply(KeyValue o) { + return subdir.unpack(o.getKey()).getString(0); + } + }), tr.getExecutor()); + } + }, tr.getExecutor()); + } + }); + } + + /** + * Returns {@code true}. + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @return {@code true} + */ + @Override + public CompletableFuture exists(ReadTransactionContext tcx) { + return CompletableFuture.completedFuture(true); + } + + /** + * Checks if the directory located at {@code path} exists. + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path of this {@code Directory} + * @return a {@link CompletableFuture} which will be set to {@code true} if the specified directory exists, or {@code false} if it + * doesn't + */ + @Override + public CompletableFuture exists(final ReadTransactionContext tcx, final List path) { + final List pathCopy = new ArrayList(path); + + return tcx.readAsync(new Function>() { + @Override + public CompletableFuture apply(final ReadTransaction tr) { + return checkVersion(tr) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + return new NodeFinder(pathCopy).find(tr).thenComposeAsync(new NodeMetadataLoader(tr), tr.getExecutor()); + }; + }, tr.getExecutor()) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Node node) { + if(!node.exists()) + //return new ReadyFuture(false); + return CompletableFuture.completedFuture(false); + else if(node.isInPartition(false)) + return node.getContents().exists(tr, node.getPartitionSubpath()); + + //return new ReadyFuture(true); + return CompletableFuture.completedFuture(true); + } + }, tr.getExecutor()); + } + }); + } + + // + // Internal + // + + private Subspace nodeWithPrefix(byte[] prefix) { + if(prefix == null) { + return null; + } + return nodeSubspace.get(prefix); + } + + private CompletableFuture nodeContainingKey(final ReadTransaction tr, final byte[] key) { + // Right now this is only used for _is_prefix_free(), but if we add + // parent pointers to directory nodes, it could also be used to find a + // path based on a key. + if(ByteArrayUtil.startsWith(key, nodeSubspace.getKey())) { + return CompletableFuture.completedFuture(rootNode); + } + + return tr.getRange(nodeSubspace.range().begin, ByteArrayUtil.join(nodeSubspace.pack(key), new byte[]{0x00}), 1, true) + .asList() + .thenApply(new Function, Subspace>() { + @Override + public Subspace apply(List results) { + if(results.size() > 0) { + byte[] resultKey = results.get(0).getKey(); + byte[] prevPrefix = nodeSubspace.unpack(resultKey).getBytes(0); + if(ByteArrayUtil.startsWith(key, prevPrefix)) { + return nodeWithPrefix(prevPrefix); + } + } + + return null; + } + }); + } + + private List toAbsolutePath(List subPath) { + return PathUtil.join(path, subPath); + } + + private DirectorySubspace contentsOfNode(Subspace node, List path, byte[] layer) { + byte[] prefix = nodeSubspace.unpack(node.getKey()).getBytes(0); + + if(Arrays.equals(layer, DirectoryLayer.PARTITION_LAYER)) + return new DirectoryPartition(toAbsolutePath(path), prefix, this); + else + return new DirectorySubspace(toAbsolutePath(path), prefix, this, layer); + } + + private CompletableFuture removeInternal(final TransactionContext tcx, final List path, final boolean mustExist) { + final List pathCopy = new ArrayList(path); + + return tcx.runAsync(new Function>() { + @Override + public CompletableFuture apply(final Transaction tr) { + return checkOrWriteVersion(tr) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + if(pathCopy.size() == 0) + throw new DirectoryException("The root directory cannot be removed.", toAbsolutePath(pathCopy)); + + return new NodeFinder(pathCopy).find(tr).thenComposeAsync(new NodeMetadataLoader(tr), tr.getExecutor()); + } + }, tr.getExecutor()) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Node node) { + if(!node.exists()) { + if(mustExist) + throw new NoSuchDirectoryException(toAbsolutePath(pathCopy)); + else + return CompletableFuture.completedFuture(false); + } + + if(node.isInPartition(false)) + return node.getContents().getDirectoryLayer().removeInternal(tr, node.getPartitionSubpath(), mustExist); + else { + ArrayList> futures = new ArrayList>(); + futures.add(removeRecursive(tr, node.subspace)); + futures.add(removeFromParent(tr, pathCopy)); + + return AsyncUtil.tag(AsyncUtil.whenAll(futures), true); + } + } + }, tr.getExecutor()); + } + }); + } + + private CompletableFuture removeFromParent(final Transaction tr, final List path) { + return new NodeFinder(PathUtil.popBack(path)).find(tr) + .thenApply(new Function() { + @Override + public Void apply(Node parent) { + tr.clear(parent.subspace.get(SUB_DIR_KEY).get(getLast(path)).getKey()); + return null; + } + }); + } + + private CompletableFuture removeRecursive(final Transaction tr, final Subspace node) { + Subspace subdir = node.get(SUB_DIR_KEY); + final AsyncIterator rangeItr = tr.getRange(subdir.range()).iterator(); + + tr.clear(Range.startsWith(nodeSubspace.unpack(node.getKey()).getBytes(0))); + tr.clear(node.range()); + + return AsyncUtil.whileTrue(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + CompletableFuture subdirRemoveFuture; + if(rangeItr.onHasNext().isDone() && rangeItr.hasNext()) + subdirRemoveFuture = removeRecursive(tr, nodeWithPrefix(rangeItr.next().getValue())); + else + subdirRemoveFuture = CompletableFuture.completedFuture(null); + + return subdirRemoveFuture + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + return rangeItr.onHasNext(); + } + }, tr.getExecutor()); + } + }, tr.getExecutor()); + } + + private CompletableFuture isPrefixFree(final ReadTransaction tr, final byte[] prefix) { + // Returns true if the given prefix does not "intersect" any currently + // allocated prefix (including the root node). This means that it neither + // contains any other prefix nor is contained by any other prefix. + if(prefix == null || prefix.length == 0) + //return new ReadyFuture(false); + return CompletableFuture.completedFuture(false); + + return nodeContainingKey(tr, prefix) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Subspace node) { + if(node != null) + //return new ReadyFuture(false); + return CompletableFuture.completedFuture(false); + + AsyncIterator it = tr.getRange(nodeSubspace.pack(prefix), nodeSubspace.pack(ByteArrayUtil.strinc(prefix)), 1).iterator(); + return it.onHasNext() + .thenApply(new Function() { + @Override + public Boolean apply(Boolean hasNext) { + return !hasNext; + } + }); + } + }, tr.getExecutor()); + } + + private CompletableFuture getVersionValue(final ReadTransaction tr) { + return tr.get(rootNode.pack(VERSION_KEY)); + } + + private CompletableFuture checkOrWriteVersion(final Transaction tr) { + return getVersionValue(tr).thenApply(new WritableVersionCheck(tr)); + } + + private CompletableFuture checkVersion(final ReadTransaction tr) { + return getVersionValue(tr).thenApply(new VersionCheck()); + } + + private CompletableFuture createOrOpenInternal(final ReadTransaction rtr, + final Transaction tr, + final List path, + final byte[] layer, + final byte[] prefix, + final boolean allowCreate, + final boolean allowOpen) + { + final List pathCopy = new ArrayList(path); + + if(prefix != null && !allowManualPrefixes) { + String errorMessage; + if(this.path.size() == 0) + errorMessage = "Cannot specify a prefix unless manual prefixes are enabled."; + else + errorMessage = "Cannot specify a prefix in a partition."; + + CompletableFuture future = new CompletableFuture(); + future.completeExceptionally(new IllegalArgumentException(errorMessage)); + return future; + } + + return checkVersion(rtr) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + // Root directory contains node metadata and so may not be opened. + if(pathCopy.size() == 0) { + throw new IllegalArgumentException("The root directory may not be opened."); + } + + return new NodeFinder(pathCopy).find(rtr).thenComposeAsync(new NodeMetadataLoader(rtr), rtr.getExecutor()); + } + }, rtr.getExecutor()) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(final Node existingNode) { + if(existingNode.exists()) { + if(existingNode.isInPartition(false)) { + List subpath = existingNode.getPartitionSubpath(); + DirectoryLayer directoryLayer = existingNode.getContents().getDirectoryLayer(); + return directoryLayer.createOrOpenInternal( + rtr, tr, subpath, layer, prefix, allowCreate, allowOpen); + } + + DirectorySubspace opened = openInternal(pathCopy, layer, existingNode, allowOpen); + return CompletableFuture.completedFuture(opened); + } + else + return createInternal(tr, pathCopy, layer, prefix, allowCreate); + } + }, rtr.getExecutor()); + } + + private DirectorySubspace openInternal(final List path, + final byte[] layer, + final Node existingNode, + final boolean allowOpen) + { + if(!allowOpen) { + throw new DirectoryAlreadyExistsException(toAbsolutePath(path)); + } + else { + if(layer.length > 0 && !Arrays.equals(layer, existingNode.layer)) { + throw new MismatchedLayerException(toAbsolutePath(path), existingNode.layer, layer); + } + + return existingNode.getContents(); + } + } + + private CompletableFuture createInternal(final Transaction tr, + final List path, + final byte[] layer, + final byte[] prefix, + final boolean allowCreate) + { + if(!allowCreate) { + throw new NoSuchDirectoryException(toAbsolutePath(path)); + } + + return checkOrWriteVersion(tr) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + if(prefix == null) { + return allocator.allocate(tr) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(byte[] allocated) { + final byte[] finalPrefix = ByteArrayUtil.join(contentSubspace.getKey(), allocated); + return tr.getRange(Range.startsWith(finalPrefix), 1) + .asList() + .thenApply(new Function, byte[]>() { + @Override + public byte[] apply(List results) { + if(results.size() > 0) { + throw new IllegalStateException("The database has keys stored at the prefix chosen by the automatic " + + "prefix allocator: " + ByteArrayUtil.printable(finalPrefix) + "."); + } + + return finalPrefix; + } + }); + } + }, tr.getExecutor()); + } + else + return CompletableFuture.completedFuture(prefix); + //return new ReadyFuture(prefix); + } + }, tr.getExecutor()) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(final byte[] actualPrefix) { + return isPrefixFree(prefix == null ? tr.snapshot() : tr, actualPrefix) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Boolean prefixFree) { + if(!prefixFree) { + if(prefix == null) { + throw new IllegalStateException("The directory layer has manually allocated prefixes that conflict " + + "with the automatic prefix allocator."); + } + else + throw new IllegalArgumentException("Prefix already in use: " + ByteArrayUtil.printable(actualPrefix) + "."); + } + else if(path.size() > 1) { + return createOrOpen(tr, PathUtil.popBack(path)) + .thenApply(new Function() { + @Override + public Subspace apply(DirectorySubspace dir) { + return nodeWithPrefix(dir.getKey()); + } + }); + } + else + return CompletableFuture.completedFuture(rootNode); + } + }, tr.getExecutor()) + .thenApply(new Function() { + @Override + public DirectorySubspace apply(Subspace parentNode) { + if(parentNode == null) + throw new IllegalStateException("The parent directory does not exist."); //Shouldn't happen + Subspace node = nodeWithPrefix(actualPrefix); + tr.set(parentNode.get(SUB_DIR_KEY).get(getLast(path)).getKey(), actualPrefix); + tr.set(node.get(LAYER_KEY).getKey(), layer); + return contentsOfNode(node, path, layer); + } + }); + } + }, tr.getExecutor()); + } + + // + // Helpers + // + + private static long unpackLittleEndian(byte[] bytes) { + assert bytes.length == 8; + int value = 0; + for(int i = 0; i < 8; ++i) { + value += (bytes[i] << (i * 8)); + } + return value; + } + + private static String getLast(List list) { + assert list.size() > 0; + return list.get(list.size() - 1); + } + + private class VersionCheck implements Function { + @Override + public Void apply(byte[] versionBytes) { + if(versionBytes == null) { + return null; + } + + ByteBuffer versionBuf = ByteBuffer.wrap(versionBytes); + versionBuf.order(ByteOrder.LITTLE_ENDIAN); + + Integer version[] = new Integer[3]; + for(int i = 0; i < version.length; ++i) + version[i] = versionBuf.getInt(); + + String dirVersion = String.format("version %d.%d.%d", (Object[])version); + String layerVersion = String.format("directory layer %d.%d.%d", (Object[])VERSION); + + throwOnError(version, dirVersion, layerVersion); + + return null; + } + + protected void throwOnError(Integer[] version, String dirVersion, + String layerVersion) { + if(version[0] > VERSION[0]) + throw new DirectoryVersionException("Cannot load directory with " + dirVersion + " using " + layerVersion + "."); + } + } + + private class WritableVersionCheck extends VersionCheck { + private final Transaction tr; + + private WritableVersionCheck(Transaction tr) { + this.tr = tr; + } + + @Override + public Void apply(byte[] versionBytes) { + if(versionBytes == null) { + // initializeVersion + ByteBuffer buf = ByteBuffer.allocate(VERSION.length * 4); + buf.order(ByteOrder.LITTLE_ENDIAN); + for(int ver : VERSION) + buf.putInt(ver); + + tr.set(rootNode.pack(VERSION_KEY), buf.array()); + return null; + } + + return super.apply(versionBytes); + } + + @Override + protected void throwOnError(Integer[] version, String dirVersion, + String layerVersion) { + super.throwOnError(version, dirVersion, layerVersion); + if(version[1] > VERSION[1]) + throw new DirectoryVersionException("Directory with " + dirVersion + " is read-only when opened with " + layerVersion + "."); + } + } + + private class NodeFinder { + private List path; + private int index; + private Node node; + private List currentPath; + + public NodeFinder(List path) { + this.path = path; + } + + public CompletableFuture find(final ReadTransaction tr) { + index = 0; + node = new Node(rootNode, currentPath, path); + currentPath = new ArrayList(); + + return AsyncUtil.whileTrue(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + if(index == path.size()) + return CompletableFuture.completedFuture(false); + + return tr.get(node.subspace.get(SUB_DIR_KEY).get(path.get(index)).getKey()) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(byte[] key) { + currentPath.add(path.get(index)); + node = new Node(nodeWithPrefix(key), currentPath, path); + + if(!node.exists()) + return CompletableFuture.completedFuture(false); + + return node.loadMetadata(tr) + .thenApply(new Function() { + @Override + public Boolean apply(Node ignore) { + ++index; + return !Arrays.equals(node.layer, DirectoryLayer.PARTITION_LAYER); + } + }); + } + }, tr.getExecutor()); + } + }, tr.getExecutor()) + .thenApply(new Function() { + @Override + public Node apply(Void ignore) { + return node; + } + }); + } + } + + private static class NodeMetadataLoader implements Function> { + private final ReadTransaction tr; + + public NodeMetadataLoader(ReadTransaction tr) { + this.tr = tr; + } + + @Override + public CompletableFuture apply(Node node) { + return node.loadMetadata(tr); + } + } + + private class Node { + public final Subspace subspace; + public final List path; + public final List targetPath; + public byte[] layer; + + private boolean loadedMetadata; + + public Node(Subspace subspace, List path, List targetPath) { + this.subspace = subspace; + this.path = path; + this.targetPath = targetPath; + + layer = null; + loadedMetadata = false; + } + + public boolean exists() { + return subspace != null; + } + + public CompletableFuture loadMetadata(ReadTransaction tr) { + if(!exists()) { + loadedMetadata = true; + return CompletableFuture.completedFuture(this); + } + + return tr.get(subspace.pack(new Tuple().add(LAYER_KEY))) + .thenApply(new Function() { + @Override + public Node apply(byte[] value) { + layer = value; + loadedMetadata = true; + return Node.this; + } + }); + } + + public void ensureMetadataLoaded() { + if(!loadedMetadata) + throw new IllegalStateException("Metadata for node has not been loaded"); + } + + public boolean isInPartition(boolean includeEmptySubpath) { + ensureMetadataLoaded(); + return exists() && Arrays.equals(layer, DirectoryLayer.PARTITION_LAYER) && (includeEmptySubpath || targetPath.size() > path.size()); + } + + public List getPartitionSubpath() { + ensureMetadataLoaded(); + return targetPath.subList(path.size(), targetPath.size()); + } + + public DirectorySubspace getContents() { + ensureMetadataLoaded(); + return contentsOfNode(subspace, path, layer); + } + } + + private static class PrefixFinder { + private final Random random; + + private long windowStart; + private int windowSize; + + private long candidate; + private boolean restart; + + public PrefixFinder() { + this.random = new Random(); + this.windowStart = 0; + } + + public CompletableFuture find(final Transaction tr, final HighContentionAllocator allocator) { + return AsyncUtil.whileTrue(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + final AsyncIterator rangeItr = tr.snapshot().getRange(allocator.counters.range(), 1, true).iterator(); + return rangeItr.onHasNext() + .thenApply(new Function() { + @Override + public Void apply(Boolean hasNext) { + if(hasNext) { + KeyValue kv = rangeItr.next(); + windowStart = allocator.counters.unpack(kv.getKey()).getLong(0); + } + + return null; + } + }) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + return chooseWindow(tr, allocator); + } + }, tr.getExecutor()) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + return choosePrefix(tr, allocator); // false exits the loop (i.e. we have a valid prefix) + } + }, tr.getExecutor()); + } + }, tr.getExecutor()) + .thenApply(new Function() { + @Override + public byte[] apply(Void ignore) { + return Tuple.from(candidate).pack(); + } + }); + } + + public CompletableFuture chooseWindow(final Transaction tr, final HighContentionAllocator allocator) { + final long initialWindowStart = windowStart; + return AsyncUtil.whileTrue(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + final byte[] counterKey = allocator.counters.get(windowStart).getKey(); + + Range oldCounters = new Range(allocator.counters.getKey(), counterKey); + Range oldAllocations = new Range(allocator.recent.getKey(), allocator.recent.get(windowStart).getKey()); + + CompletableFuture newCountRead; + // SOMEDAY: synchronize on something transaction local + synchronized(HighContentionAllocator.class) { + if(windowStart > initialWindowStart) { + tr.clear(oldCounters); + tr.options().setNextWriteNoWriteConflictRange(); + tr.clear(oldAllocations); + } + + tr.mutate(MutationType.ADD, counterKey, LITTLE_ENDIAN_LONG_ONE); + newCountRead = tr.snapshot().get(counterKey); + } + + return newCountRead + .thenApply(new Function() { + @Override + public Boolean apply(byte[] newCountBytes) { + long newCount = newCountBytes == null ? 0 : unpackLittleEndian(newCountBytes); + windowSize = getWindowSize(windowStart); + if(newCount * 2 >= windowSize) { + windowStart += windowSize; + return true; + } + + return false; // exit the loop + } + }); + } + }, tr.getExecutor()); + } + + public CompletableFuture choosePrefix(final Transaction tr, final HighContentionAllocator allocator) { + restart = false; + return AsyncUtil.whileTrue(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + // As of the snapshot being read from, the window is less than half + // full, so this should be expected to take 2 tries. Under high + // contention (and when the window advances), there is an additional + // subsequent risk of conflict for this transaction. + candidate = windowStart + random.nextInt(windowSize); + final byte[] allocationKey = allocator.recent.get(candidate).getKey(); + Range countersRange = allocator.counters.range(); + + AsyncIterable counterRange; + CompletableFuture allocationTemp; + // SOMEDAY: synchronize on something transaction local + synchronized(HighContentionAllocator.class) { + counterRange = tr.snapshot().getRange(countersRange, 1, true); + allocationTemp = tr.get(allocationKey); + tr.options().setNextWriteNoWriteConflictRange(); + tr.set(allocationKey, EMPTY_BYTES); + } + + final CompletableFuture> lastCounter = counterRange.asList(); + final CompletableFuture allocation = allocationTemp; + + List> futures = new ArrayList>(); + futures.add(AsyncUtil.success(lastCounter)); + futures.add(AsyncUtil.success(allocation)); + + return AsyncUtil.whenAll(futures) + .thenApply(new Function() { + @Override + public Boolean apply(Void ignore) { + long currentWindowStart = 0; + List result = lastCounter.join(); + if(!result.isEmpty()) { + currentWindowStart = allocator.counters.unpack(result.get(0).getKey()).getLong(0); + } + + if(currentWindowStart > windowStart) { + restart = true; + return false; // exit the loop and rerun the allocation from the beginning + } + + if(allocation.join() == null) { + tr.addWriteConflictKey(allocationKey); + return false; // exit the loop and return this candidate + } + + return true; + } + }); + } + }, tr.getExecutor()) + .thenApply(new Function() { + @Override + public Boolean apply(Void ignore) { + return restart; + } + }); + } + + private static int getWindowSize(long start) { + // Larger window sizes are better for high contention, smaller sizes for + // keeping the keys small. But if there are many allocations, the keys + // can't be too small. So start small and scale up. We don't want this + // to ever get *too* big because we have to store about window_size/2 + // recent items. + if(start < 255) { + return 64; + } + if(start < 65535) { + return 1024; + } + return 8192; + } + } + + private static class HighContentionAllocator { + public final Subspace counters; + public final Subspace recent; + + public HighContentionAllocator(Subspace subspace) { + this.counters = subspace.get(0); + this.recent = subspace.get(1); + } + + /** + * Returns a byte string that: + *
      + *
    1. has never and will never be returned by another call to this method on the same subspace
    2. + *
    3. is nearly as short as possible given the above
    4. + *
    + */ + public CompletableFuture allocate(final Transaction tr) { + return new PrefixFinder().find(tr, this); + } + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryMoveException.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryMoveException.java new file mode 100644 index 0000000000..cc9f7abb56 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryMoveException.java @@ -0,0 +1,47 @@ +/* + * DirectoryMoveException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +/** + * An {@link Exception} that is thrown when an invalid directory move + * is attempted. + */ +@SuppressWarnings("serial") +public class DirectoryMoveException extends RuntimeException { + /** + * The path of the directory being moved. + */ + public final List sourcePath; + + /** + * The path that the directory was being moved to. + */ + public final List destPath; + + DirectoryMoveException(String message, List sourcePath, List destPath) { + super(message + ": sourcePath=" + DirectoryUtil.pathStr(sourcePath) + ", destPath=" + DirectoryUtil.pathStr(destPath)); + this.sourcePath = sourcePath; + this.destPath = destPath; + } +} + diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryPartition.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryPartition.java new file mode 100644 index 0000000000..2a4b33a655 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryPartition.java @@ -0,0 +1,197 @@ +/* + * DirectoryPartition.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import static com.apple.cie.foundationdb.directory.DirectoryLayer.DEFAULT_NODE_SUBSPACE_PREFIX; + +import java.util.List; + +import com.apple.cie.foundationdb.subspace.Subspace; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * A {@code DirectoryPartition} is a {@link DirectorySubspace} whose prefix is prepended to all of its descendant directories' prefixes. + * + *

    + * A {@code DirectoryPartition} cannot be used as a {@link Subspace}. Instead, you must create at least one subdirectory to store + * content. + *

    + * + * For general guidance on partition usage, see + * The Developer Guide. + */ +class DirectoryPartition extends DirectorySubspace { + + private final DirectoryLayer parentDirectoryLayer; + + DirectoryPartition(List path, byte[] prefix, DirectoryLayer parentDirectoryLayer) { + super(path, + prefix, + new DirectoryLayer(new Subspace(ByteArrayUtil.join(prefix, DEFAULT_NODE_SUBSPACE_PREFIX)), new Subspace(prefix)), + DirectoryLayer.PARTITION_LAYER); + + getDirectoryLayer().setPath(path); + this.parentDirectoryLayer = parentDirectoryLayer; + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Subspace get(Object o) { + throw new UnsupportedOperationException("Cannot open subspace in the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Subspace get(Tuple name) { + throw new UnsupportedOperationException("Cannot open subspace in the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public byte[] getKey() { + throw new UnsupportedOperationException("Cannot get key for the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public byte[] pack() { + throw new UnsupportedOperationException("Cannot pack keys using the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public byte[] pack(Object o) { + throw new UnsupportedOperationException("Cannot pack keys using the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public byte[] pack(Tuple tuple) { + throw new UnsupportedOperationException("Cannot pack keys using the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Tuple unpack(byte[] key) { + throw new UnsupportedOperationException("Cannot unpack keys using the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Range range() { + throw new UnsupportedOperationException("Cannot get range for the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Range range(Tuple tuple) { + throw new UnsupportedOperationException("Cannot get range for the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public boolean contains(byte[] key) { + throw new UnsupportedOperationException("Cannot check whether a key belongs to the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Subspace subspace(Tuple tuple) { + throw new UnsupportedOperationException("Cannot open subspace in the root of a directory partition."); + } + + @Override + DirectoryLayer getLayerForPath(List path) { + if(path.size() == 0) + return parentDirectoryLayer; + else + return getDirectoryLayer(); + } + + /** + * Returns whether this {@code DirectoryPartition} is equal to {@code rhs}. + * Two {@code DirectoryPartition}s are equal if they were created by the same + * {@link DirectoryLayer} and have the same path, layer, and subspace prefix. + * + * @param rhs the {@code} Object to test for equality + * @return true if this is equal to {@code rhs} + */ + @Override + public boolean equals(Object rhs) { + if(this == rhs) { + return true; + } + if(rhs == null || getClass() != rhs.getClass()) { + return false; + } + + DirectoryPartition other = (DirectoryPartition)rhs; + return (getPath() == other.getPath() || getPath() == other.getPath()) && + parentDirectoryLayer.equals(other.parentDirectoryLayer) && + super.equals(rhs); + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectorySubspace.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectorySubspace.java new file mode 100644 index 0000000000..60f1a295fb --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectorySubspace.java @@ -0,0 +1,180 @@ +/* + * DirectorySubspace.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import static com.apple.cie.foundationdb.directory.DirectoryLayer.EMPTY_BYTES; +import static com.apple.cie.foundationdb.directory.DirectoryLayer.EMPTY_PATH; +import static com.apple.cie.foundationdb.tuple.ByteArrayUtil.printable; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import com.apple.cie.foundationdb.ReadTransactionContext; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.subspace.Subspace; + +/** + * A DirectorySubspace represents the contents of a directory, but it + * also remembers the path with which it was opened and offers convenience + * methods to operate on the directory at that path. + * + *

    + * An instance of DirectorySubspace can be used for all the usual subspace + * operations. It can also be used to operate on the directory with which + * it was opened. + *

    + */ +public class DirectorySubspace extends Subspace implements Directory { + private final List path; + private final byte[] layer; + private final DirectoryLayer directoryLayer; + + DirectorySubspace(List path, byte[] prefix, DirectoryLayer directoryLayer) { + this(path, prefix, directoryLayer, EMPTY_BYTES); + } + + DirectorySubspace(List path, byte[] prefix, DirectoryLayer directoryLayer, byte[] layer) { + super(prefix); + this.path = path; + this.layer = layer; + this.directoryLayer = directoryLayer; + } + + /** + * @return a printable representation of this {@code DirectorySubspace} + */ + @Override + public String toString() { + return getClass().getSimpleName() + '(' + DirectoryUtil.pathStr(path) + ", " + printable(getKey()) + ')'; + } + + /** + * Returns whether this {@code DirectorySubspace} is equal to {@code rhs}. + * Two {@code DirectorySubspace}s are equal if they were created by the same + * {@link DirectoryLayer} and have the same path, layer, and subspace prefix. + * + * @param rhs the {@code} Object to test for equality + * @return true if this is equal to {@code rhs} + */ + @Override + public boolean equals(Object rhs) { + if(this == rhs) { + return true; + } + if(rhs == null || getClass() != rhs.getClass()) { + return false; + } + DirectorySubspace other = (DirectorySubspace)rhs; + return (path == other.path || path.equals(other.path)) && + Arrays.equals(layer, other.layer) && + directoryLayer.equals(other.directoryLayer) && + super.equals(rhs); + } + + @Override + public List getPath() { + return Collections.unmodifiableList(path); + } + + @Override + public byte[] getLayer() { + return Arrays.copyOf(layer, layer.length); + } + + @Override + public DirectoryLayer getDirectoryLayer() { + return directoryLayer; + } + + @Override + public CompletableFuture createOrOpen(TransactionContext tcx, List subpath, byte[] otherLayer) { + return directoryLayer.createOrOpen(tcx, getPartitionSubpath(subpath), otherLayer); + } + + @Override + public CompletableFuture open(ReadTransactionContext tcx, List subpath, byte[] otherLayer) { + return directoryLayer.open(tcx, getPartitionSubpath(subpath), otherLayer); + } + + @Override + public CompletableFuture create(TransactionContext tcx, List subpath, byte[] otherLayer, byte[] prefix) { + return directoryLayer.create(tcx, getPartitionSubpath(subpath), otherLayer, prefix); + } + + @Override + public CompletableFuture> list(ReadTransactionContext tcx, List subpath) { + return directoryLayer.list(tcx, getPartitionSubpath(subpath)); + } + + @Override + public CompletableFuture move(TransactionContext tcx, List oldSubpath, List newSubpath) { + return directoryLayer.move(tcx, getPartitionSubpath(oldSubpath), getPartitionSubpath(newSubpath)); + } + + @Override + public CompletableFuture moveTo(TransactionContext tcx, List newAbsolutePath) { + DirectoryLayer dir = getLayerForPath(EMPTY_PATH); + int partitionLen = dir.getPath().size(); + List partitionPath = newAbsolutePath.subList(0, Math.min(newAbsolutePath.size(), partitionLen)); + if(!partitionPath.equals(dir.getPath())) + throw new DirectoryMoveException("Cannot move between partitions", path, newAbsolutePath); + + return dir.move(tcx, + getPartitionSubpath(EMPTY_PATH, dir), + newAbsolutePath.subList(partitionLen, newAbsolutePath.size())); + } + + @Override + public CompletableFuture remove(TransactionContext tcx, List subpath) { + DirectoryLayer dir = getLayerForPath(subpath); + return dir.remove(tcx, getPartitionSubpath(subpath, dir)); + } + + @Override + public CompletableFuture removeIfExists(TransactionContext tcx, List subpath) { + DirectoryLayer dir = getLayerForPath(subpath); + return dir.removeIfExists(tcx, getPartitionSubpath(subpath, dir)); + } + + @Override + public CompletableFuture exists(ReadTransactionContext tcx, List subpath) { + DirectoryLayer dir = getLayerForPath(subpath); + return dir.exists(tcx, getPartitionSubpath(subpath, dir)); + } + + private List getPartitionSubpath(List path) { + return getPartitionSubpath(path, directoryLayer); + } + + private List getPartitionSubpath(List path, DirectoryLayer directoryLayer) { + return PathUtil.join(this.path.subList(directoryLayer.getPath().size(), this.path.size()), path); + } + + /** + * Called by all functions that could operate on this subspace directly (moveTo, remove, removeIfExists, exists). + * Subclasses can chooose to return a different directory layer to use for the operation if path is in fact empty. + */ + DirectoryLayer getLayerForPath(List path) { + return directoryLayer; + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryUtil.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryUtil.java new file mode 100644 index 0000000000..2383f187e6 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryUtil.java @@ -0,0 +1,45 @@ +/* + * DirectoryUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +class DirectoryUtil { + static String pathStr(List t) { + if(t == null) { + return String.valueOf(t); + } + StringBuilder sb = new StringBuilder(); + sb.append('('); + boolean first = true; + for(String s : t) { + if(!first) { + sb.append(','); + } else { + first = false; + } + + sb.append(s); + } + sb.append(')'); + return sb.toString(); + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryVersionException.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryVersionException.java new file mode 100644 index 0000000000..406d66bd03 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/DirectoryVersionException.java @@ -0,0 +1,33 @@ +/* + * DirectoryVersionException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +/** + * An {@link Exception} that is thrown when the version of the directory layer + * used to create directories in the database is incompatible with this version + * of the directory layer. + */ +@SuppressWarnings("serial") +public class DirectoryVersionException extends RuntimeException { + DirectoryVersionException(String message) { + super(message); + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/MismatchedLayerException.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/MismatchedLayerException.java new file mode 100644 index 0000000000..feea71e3f3 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/MismatchedLayerException.java @@ -0,0 +1,48 @@ +/* + * MismatchedLayerException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +import java.util.List; + +/** + * A {@link DirectoryException} that is thrown when a directory is opened with an incompatible layer. + */ +@SuppressWarnings("serial") +public class MismatchedLayerException extends DirectoryException { + /** + * The layer byte string that the directory was created with. + */ + public final byte[] stored; + + /** + * The layer byte string that the directory was opened with. + */ + public final byte[] opened; + + MismatchedLayerException(List path, byte[] stored, byte[] opened) { + super("Mismatched layer: stored=" + ByteArrayUtil.printable(stored) + ", opened=" + ByteArrayUtil.printable(opened), path); + this.stored = stored; + this.opened = opened; + } +} + diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/NoSuchDirectoryException.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/NoSuchDirectoryException.java new file mode 100644 index 0000000000..00e8a6c5e5 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/NoSuchDirectoryException.java @@ -0,0 +1,34 @@ +/* + * NoSuchDirectoryException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +/** + * A {@link DirectoryException} that is thrown when a directory that doesn't + * exist is used. + */ +@SuppressWarnings("serial") +public class NoSuchDirectoryException extends DirectoryException { + NoSuchDirectoryException(List path) { + super("No such directory", path); + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/PathUtil.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/PathUtil.java new file mode 100644 index 0000000000..5b69f6579a --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/PathUtil.java @@ -0,0 +1,95 @@ +/* + * PathUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +/** + * The {@code PathUtil} class provides static helper functions useful for working + * with directory paths. + */ +public class PathUtil { + /** + * Joins two paths into one larger path. + * + * @param path1 The first path to join + * @param path2 The path to append to {@code path1} + * @return a new list which contains all the items in {@code path1} followed + * by all the items in {@code path2} + */ + public static List join(List path1, List path2) { + List newPath = new LinkedList(path1); + newPath.addAll(path2); + return newPath; + } + + /** + * Extends a path by an arbitrary number of elements. + * + * @param path The path to extend + * @param subPaths The items to append to path + * @return a new list which contains all the items in {@code path} followed + * by all additional items specified in {@code subPaths} + */ + public static List extend(List path, String... subPaths) { + return join(path, Arrays.asList(subPaths)); + } + + /** + * Creates a new path from an arbitrary number of elements. + * + * @param subPaths The items in the path + * @return a list which contains all the items specified in {@code subPaths} + */ + public static List from(String... subPaths) { + return new LinkedList(Arrays.asList(subPaths)); + } + + /** + * Removes the first item from a path. + * + * @param path the path whose first item is being popped + * @return a new list which contains all the items in {@code path} except + * for the first item + */ + public static List popFront(List path) { + if(path.isEmpty()) + throw new IllegalStateException("Path contains no elements."); + + return new LinkedList(path.subList(1, path.size())); + } + + /** + * Removes the last item from a path. + * + * @param path the path whose last item is being popped + * @return a new list which contains all the items in {@code path} except + * for the last item + */ + public static List popBack(List path) { + if(path.isEmpty()) + throw new IllegalStateException("Path contains no elements."); + + return new LinkedList(path.subList(0, path.size() - 1)); + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/package-info.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/package-info.java new file mode 100644 index 0000000000..4616883692 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/directory/package-info.java @@ -0,0 +1,38 @@ +/* + * package-info.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides tools for managing hierarchically related key subspaces. + * Directories are a recommended approach for + * administering applications. Each application should create or + * open at least one directory to manage its subspaces.
    + *
    + * Directories are identified by hierarchical paths analogous to the + * paths in a Unix-like file system. A path is represented as a tuple + * of strings. Each directory has an associated subspace used to store + * its content. The directory layer maps each path to a short prefix + * used for the corresponding subspace. In effect, directories provide + * a level of indirection for access to subspaces.
    + *
    + * See general + * directory documentation for information about how directories work and + * interact with other parts of the built-in keyspace management features. + */ +package com.apple.cie.foundationdb.directory; \ No newline at end of file diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/package-info.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/package-info.java new file mode 100644 index 0000000000..13f9a3a638 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/package-info.java @@ -0,0 +1,35 @@ +/* + * package-info.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides an API for the FoundationDB transactional key/value store. Clients operating + * on a {@link com.apple.cie.foundationdb.Database} should, in most cases, use the + * {@link com.apple.cie.foundationdb.TransactionContext#run(Function) run(Function)} + * or the + * {@link com.apple.cie.foundationdb.TransactionContext#runAsync(Function) runAsync(Function)} + * constructs. These two functions (and their two derivations) implement a proper + * retry loop around the work that needs to get done and, in the case of {@code Database}, + * assure that {@link com.apple.cie.foundationdb.Transaction#commit()} has returned successfully + * before itself returning. If you are not able to use these functions for some reason + * please closely read and understand the other + * developer + * documentation on FoundationDB transactions. + */ +package com.apple.cie.foundationdb; diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/subspace/Subspace.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/subspace/Subspace.java new file mode 100644 index 0000000000..b2de6e2355 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/subspace/Subspace.java @@ -0,0 +1,240 @@ +/* + * Subspace.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.subspace; + +import static com.apple.cie.foundationdb.tuple.ByteArrayUtil.join; +import static com.apple.cie.foundationdb.tuple.ByteArrayUtil.printable; + +import java.util.Arrays; + +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * {@code Subspace} provide a convenient way to use {@link Tuple}s to define namespaces for + * different categories of data. The namespace is specified by a prefix {@link Tuple} + * which is prepended to all {@link Tuple}s packed by the {@code Subspace}. When unpacking a key + * with the {@code Subspace}, the prefix {@link Tuple} will be removed from the result. + * + *

    + * For general guidance on subspace usage, see the discussion in + * Developer Guide. + *

    + * + *

    + * As a best practice, API clients should use at least one subspace for application data. + *

    + */ +public class Subspace +{ + static final Tuple EMPTY_TUPLE = Tuple.from(); + static final byte[] EMPTY_BYTES = new byte[0]; + + private final byte[] rawPrefix; + + /** + * Constructor for a subspace formed with an empty prefix {@link Tuple}. + */ + public Subspace() { + this(EMPTY_TUPLE, EMPTY_BYTES); + } + + /** + * Constructor for a subspace formed with the specified prefix {@link Tuple}. + * + * @param prefix a {@link Tuple} used to form the subspace + */ + public Subspace(Tuple prefix) { + this(prefix, EMPTY_BYTES); + } + + /** + * Constructor for a subspace formed with the specified byte string, which will + * be prepended to all packed keys. + * + * @param rawPrefix a byte array used as the prefix for all packed keys + */ + public Subspace(byte[] rawPrefix) { + this(EMPTY_TUPLE, rawPrefix); + } + + /** + * Constructor for a subspace formed with both a prefix {@link Tuple} and a + * prefix byte string. The prefix {@code Tuple} will be prepended to all + * {@code Tuples} packed by the {@code Subspace}, and the byte string prefix + * will be prepended to the packed result. + * + * @param prefix a {@code Tuple} used to form the subspace + * @param rawPrefix a byte array used as the prefix for all packed keys + */ + public Subspace(Tuple prefix, byte[] rawPrefix) { + this.rawPrefix = join(rawPrefix, prefix.pack()); + } + + /** + * Returns true if this {@code Subspace} is equal to {@code rhs}. + * Two {@code Subspace}s are equal if they have the same prefix. + * + * @param rhs the object to check for equality + * @return {@code true} if this {@code Subspace} and {@code rhs} have equal prefixes + */ + @Override + public boolean equals(Object rhs) { + if(this == rhs) { + return true; + } + if(rhs == null || getClass() != rhs.getClass()) { + return false; + } + Subspace other = (Subspace)rhs; + return Arrays.equals(rawPrefix, other.rawPrefix) ; + } + + /** + * @return a printable representation of the subspace + */ + @Override + public String toString() { + return "Subspace(rawPrefix=" + printable(rawPrefix) + ")"; + } + + /** + * Gets a new subspace which is equivalent to this subspace with its prefix {@link Tuple} extended by + * the specified {@code Object}. The object will be inserted into a {@link Tuple} and passed to {@link #get(Tuple)}. + * + * @param obj an {@code Object} compatible with {@code Tuple}s + * @return a new subspace formed by joining this {@code Subspace}'s prefix to {@code obj} + */ + public Subspace get(Object obj) { + return get(Tuple.from(obj)); + } + + /** + * Gets a new subspace which is equivalent to this subspace with its prefix {@link Tuple} extended by + * the specified {@link Tuple}. + * + * @param tuple the {@link Tuple} used to form the new {@code Subspace} + * @return a new subspace formed by joining this {@code Subspace}'s prefix to {@code tuple} + */ + public Subspace get(Tuple tuple) { + return subspace(tuple); + } + + /** + * Gets the key encoding the prefix used for this {@code Subspace}. This is equivalent to + * {@link #pack}ing the empty {@link Tuple}. + * + * @return the key encoding the prefix used for this {@code Subspace} + */ + public byte[] getKey() { + return pack(); + } + + /** + * Gets the key encoding the prefix used for this {@code Subspace}. + * + * @return the key encoding the prefix used for this {@code Subspace} + */ + public byte[] pack() { + return Arrays.copyOf(rawPrefix, rawPrefix.length); + } + + /** + * Gets the key encoding the specified {@code Object} in this {@code Subspace}. {@code obj} is + * inserted into a {@link Tuple} and packed with {@link #pack(Tuple)}. + * + * @param obj an {@code Object} to be packed that is compatible with {@link Tuple}s + * @return the key encoding the tuple derived from {@code obj} + */ + public byte[] pack(Object obj) { + return pack(Tuple.from(obj)); + } + + /** + * Gets the key encoding the specified tuple in this {@code Subspace}. For example, if you have a {@code Subspace} + * with prefix {@link Tuple} {@code ("users")} and you use it to pack the {@link Tuple} {@code ("Smith")}, + * the result is the same as if you packed the {@link Tuple} {@code ("users", "Smith")}. + * + * @param tuple the {@code Tuple} to be packed + * @return the key encoding the specified tuple in this {@code Subspace} + */ + public byte[] pack(Tuple tuple) { + return join(rawPrefix, tuple.pack()); + } + + /** + * Gets the {@link Tuple} encoded by the given key, with this {@code Subspace}'s prefix {@link Tuple} and + * {@code raw prefix} removed. + * + * @param key The key being decoded + * @return the {@link Tuple} encoded by {@code key} with the prefix removed + */ + public Tuple unpack(byte[] key) { + if(!contains(key)) + throw new IllegalArgumentException("Cannot unpack key that is not contained in subspace."); + + return Tuple.fromBytes(Arrays.copyOfRange(key, rawPrefix.length, key.length)); + } + + /** + * Gets a {@link Range} respresenting all keys strictly in the {@code Subspace}. + * + * @return the {@link Range} of keyspace corresponding to this {@code Subspace} + */ + public Range range() { + return range(EMPTY_TUPLE); + } + + /** + * Gets a {@link Range} representing all keys in the {@code Subspace} strictly starting with + * the specified {@link Tuple}. + * + * @param tuple the {@code Tuple} whose sub-keys we are searching for + * @return the {@link Range} of keyspace corresponding to {@code tuple} + */ + public Range range(Tuple tuple) { + Range p = tuple.range(); + return new Range(join(rawPrefix, p.begin), join(rawPrefix, p.end)); + } + + /** + * Tests whether the specified key starts with this {@code Subspace}'s prefix, indicating that + * the {@code Subspace} logically contains key. + * + * @param key the key to be tested + * @return {@code true} if {@code key} starts with {@code Subspace.key()} + */ + public boolean contains(byte[] key) { + return ByteArrayUtil.startsWith(key, rawPrefix); + } + + /** + * Gets a new subspace which is equivalent to this subspace with its prefix {@link Tuple} extended by + * the specified {@link Tuple}. + * + * @param tuple the {@link Tuple} used to form the new {@code Subspace} + * @return a new subspace formed by joining this {@code Subspace}'s prefix to {@code tuple} + */ + public Subspace subspace(Tuple tuple) { + return new Subspace(tuple, rawPrefix); + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/subspace/package-info.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/subspace/package-info.java new file mode 100644 index 0000000000..b632c787d9 --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/subspace/package-info.java @@ -0,0 +1,33 @@ +/* + * package-info.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides a convenient way to define namespaces for different categories + * of data. The namespace is specified + * by a prefix tuple which is prepended to all tuples packed by the subspace. + * When unpacking a key with the subspace, the prefix tuple will be removed + * from the result. As a best practice, API clients should use at least one + * subspace for application data.
    + *
    + * See general + * subspace documentation for information about how subspaces work and + * interact with other parts of the built-in keyspace management features. + */ +package com.apple.cie.foundationdb.subspace; \ No newline at end of file diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/ByteArrayUtil.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/ByteArrayUtil.java new file mode 100644 index 0000000000..2257ae997d --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/ByteArrayUtil.java @@ -0,0 +1,419 @@ +/* + * ByteArrayUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.tuple; + +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +import com.apple.cie.foundationdb.Transaction; + +/** + * Utility functions for operating on byte arrays. Although built for + * the FoundationDB tuple layer, some functions may be useful otherwise, such as use of + * {@link #printable(byte[])} for debugging non-text keys and values. + * + */ +public class ByteArrayUtil { + + /** + * Joins a set of byte arrays into a larger array. The {@code interlude} is placed + * between each of the elements, but not at the beginning or end. In the case that + * the list is empty or {@code null}, a zero-length byte array will be returned. + * + * @param interlude can be {@code null} or zero length. Placed internally between + * concatenated elements. + * @param parts the pieces to be joined. May be {@code null}, but does not allow + * for elements in the list to be {@code null}. + * + * @return a newly created concatenation of the input + */ + public static byte[] join(byte[] interlude, List parts) { + if(parts == null) + return new byte[0]; + int partCount = parts.size(); + if(partCount == 0) + return new byte[0]; + + if(interlude == null) + interlude = new byte[0]; + + int element_totals = 0; + int interludeSize = interlude.length; + for(byte[] e : parts) { + element_totals += e.length; + } + + byte[] dest = new byte[(interludeSize * (partCount - 1)) + element_totals]; + + //System.out.println(" interlude -> " + ArrayUtils.printable(interlude)); + + int start_byte = 0; + int index = 0; + for(byte[] part : parts) { + //System.out.println(" section -> " + ArrayUtils.printable(parts.get(i))); + int length = part.length; + if(length > 0) { + System.arraycopy(part, 0, dest, start_byte, length); + start_byte += length; + } + if(index < partCount - 1 && interludeSize > 0) { + // If this is not the last element, append the interlude + System.arraycopy(interlude, 0, dest, start_byte, interludeSize); + start_byte += interludeSize; + } + index++; + } + + //System.out.println(" complete -> " + ArrayUtils.printable(dest)); + return dest; + } + + /** + * Joins a variable number of byte arrays into one larger array. + * + * @param parts the elements to join. {@code null} elements are not allowed. + * + * @return a newly created concatenation of the input + */ + public static byte[] join(byte[] ... parts) { + return join(null, Arrays.asList(parts)); + } + + /** + * Tests for the presence of a specific sequence of bytes in a larger array at a + * specific location.
    + * If {@code src} is {@code null} there is a case for a match. First, if {@code start} + * is non-zero, an {@code IllegalArgumentException} will be thrown. If {@code start} + * is {@code 0}, will evaluate to {@code true} if {@code pattern} is {@code null}; + * {@code false} otherwise.
    + * In all other cases, a {@code null} pattern will never match. + * + * @param src the sequence of bytes in which to search for {@code pattern} + * @param start the index at which to look for a match. The length of {@code pattern} added + * to this index must not pass the end of {@code src.} + * @param pattern the series of {@code byte}s to match. If {@code null}, will only match + * a {@code null} {@code src} at position {@code 0}. + * + * @return {@code true} if {@code pattern} is found in {@code src} at {@code start}. + */ + static boolean regionEquals(byte[] src, int start, byte[] pattern) { + if(src == null) { + if(start == 0) { + return pattern == null; + } + throw new IllegalArgumentException("start index after end of src"); + } + if(pattern == null) + return false; + + // At this point neither src or pattern are null... + + if(start >= src.length) + throw new IllegalArgumentException("start index after end of src"); + + if(src.length < start + pattern.length) + return false; + + for(int i = 0; i < pattern.length; i++) + if(pattern[i] != src[start + i]) + return false; + + return true; + } + + /** + * Replaces occurrences of a pattern in a byte array. Does not mutate the contents + * of the parameter {@code src}. + * + * @param src the source to search for {@code pattern} + * @param pattern the pattern for which to search + * @param replacement the sequence of bytes to replace {@code pattern} with. + * + * @return a newly created array where {@code pattern} replaced with {@code replacement} + */ + public static byte[] replace(byte[] src, byte[] pattern, byte[] replacement) { + return join(replacement, split(src, pattern)); + } + + /** + * Replaces occurrences of a pattern in a byte array. Does not mutate the contents + * of the parameter {@code src}. + * + * @param src the source to search for {@code pattern} + * @param offset the location in {@code src} at which to start the operation + * @param length the number of bytes past {@code offset} to search for {@code pattern} + * @param pattern the pattern for which to search + * @param replacement the sequence of bytes to replace {@code pattern} with. + * + * @return a newly created array where {@code pattern} replaced with {@code replacement} + */ + public static byte[] replace(byte[] src, int offset, int length, + byte[] pattern, byte[] replacement) { + return join(replacement, split(src, offset, length, pattern)); + } + + /** + * Splits a byte array at each occurrence of a pattern. If the pattern is found at + * the beginning or end of the array the result will have a leading or trailing + * zero-length array. The delimiter is not included in the output array. Does not + * mutate the contents the source array. + * + * @param src the array to split + * @param delimiter the byte pattern on which to split + * + * @return a list of byte arrays from {@code src} now not containing {@code delimiter} + */ + public static List split(byte[] src, byte[] delimiter) { + return split(src, 0, src.length, delimiter); + } + + /** + * Splits a byte array at each occurrence of a pattern. If the pattern is found at + * the beginning or end of the array the result will have a leading or trailing + * zero-length array. The delimiter is not included in the output array. Does not + * mutate the contents the source array. + * + * @param src the array to split + * @param offset the location in the array at which to start the operation + * @param length the number of bytes to search, must not extend past the end of {@code src} + * @param delimiter the byte pattern on which to split + * + * @return a list of byte arrays from {@code src} now not containing {@code delimiter} + */ + public static List split(byte[] src, int offset, int length, byte[] delimiter) { + List parts = new LinkedList(); + int idx = offset; + int lastSplitEnd = offset; + while(idx <= (offset+length) - delimiter.length) { + if(regionEquals(src, idx, delimiter)) { + // copy the last region of bytes into "parts", copyOfRange is happy with zero-sized ranges + parts.add(Arrays.copyOfRange(src, lastSplitEnd, idx)); + idx += delimiter.length; + lastSplitEnd = idx; + } else { + idx++; + } + } + if(lastSplitEnd == offset + length) + // if the last replacement ended at the end of src, we need a tailing empty entry + parts.add(new byte[0]); + else { + parts.add(Arrays.copyOfRange(src, lastSplitEnd, offset + length)); + } + return parts; + } + + static int bisectLeft(BigInteger[] arr, BigInteger i) { + int n = Arrays.binarySearch(arr, i); + if(n >= 0) + return n; + int ip = (n + 1) * -1; + return ip; + } + + /** + * Compare byte arrays for equality and ordering purposes. Elements in the array + * are interpreted and compared as unsigned bytes. Neither parameter + * may be {@code null} + + * @return return -1, 0, or 1 if {@code l} is less than, equal to, or greater than + * {@code r}. + */ + public static int compareUnsigned(byte[] l, byte[] r) { + for(int idx = 0; idx < l.length && idx < r.length; ++idx) { + if(l[idx] != r[idx]) { + return (l[idx] & 0xFF) < (r[idx] & 0xFF) ? -1 : 1; + } + } + if(l.length == r.length) + return 0; + return l.length < r.length ? -1 : 1; + } + + /** + * Check if a byte array starts with another byte array. + * + * @param array the source byte array + * + * @param prefix the byte array that we are checking if {@code src} + * starts with. + * + * @return {@code true} if {@code array} starts with {@code prefix} + */ + public static boolean startsWith(byte[] array, byte[] prefix) { + if(array.length < prefix.length) { + return false; + } + for(int i = 0; i < prefix.length; ++i) { + if(prefix[i] != array[i]) { + return false; + } + } + return true; + } + + /** + * Scan through an array of bytes to find the first occurrence of a specific value. + * + * @param src array to scan. Must not be {@code null}. + * @param what the value for which to search. + * @param start the index at which to start the search. If this is at or after + * the end of {@code src}, the result will always be {@code -1}. + * @param end the index one past the last entry at which to search + * + * @return return the location of the first instance of {@code value}, or + * {@code -1} if not found. + */ + static int findNext(byte[] src, byte what, int start, int end) { + for(int i = start; i < end; i++) { + if(src[i] == what) + return i; + } + return -1; + } + + /** + * Gets the index of the first element after the next occurrence of the byte sequence [nm] + * @param v the bytes to scan through + * @param n first character to find + * @param m second character to find + * @param start the index at which to start the scan + * + * @return the index after the next occurrence of [nm] + */ + static int findTerminator(byte[] v, byte n, byte m, int start) { + return findTerminator(v, n, m, start, v.length); + } + + /** + * Gets the index of the first element after the next occurrence of the byte sequence [nm] + * @param v the bytes to scan through + * @param n first character to find + * @param m second character to find + * @param start the index at which to start the scan + * @param end the index at which to stop the search (exclusive) + * + * @return the index after the next occurrence of [nm] + */ + static int findTerminator(byte[] v, byte n, byte m, int start, int end) { + int pos = start; + while(true) { + pos = findNext(v, n, pos, end); + if(pos < 0) + return end; + if(pos + 1 == end || v[pos+1] != m) + return pos; + pos += 2; + } + } + + /** + * Computes the first key that would sort outside the range prefixed by {@code key}. + * {@code key} must be non-null, and contain at least some character this is not + * {@code \xFF} (255). + * + * @param key prefix key + * + * @return a newly created byte array + */ + public static byte[] strinc(byte[] key) { + byte[] copy = rstrip(key, (byte)0xff); + if(copy.length == 0) + throw new IllegalArgumentException("No key beyond supplied prefix"); + + // Since rstrip makes sure the last character is not \xff, we can be sure + // we're able to add 1 to it without overflow. + copy[copy.length -1] = (byte) (copy[copy.length - 1] + 1); + return copy; + } + + /** + * Get a copy of an array, with all matching characters stripped from trailing edge. + * @param input array to copy. Must not be null. + * @param target byte to exclude from copy. + * @return returns a copy of {@code input} excluding occurrences of {@code target} + * at the end. + */ + static byte[] rstrip(byte[] input, byte target) { + int i = input.length - 1; + for(; i >= 0; i--) { + if(input[i] != target) + break; + } + return Arrays.copyOfRange(input, 0, i + 1); + } + + /** + * Encode an 64-bit integer (long) into a byte array. Encodes the integer in little + * endian byte order. The result is valid for use with + * {@link Transaction#mutate(com.apple.cie.foundationdb.MutationType, byte[], byte[]) Transaction.mutate(...)}. + * + * @param i the number to encode + * @return an 8-byte array containing the + * + * @see Transaction#mutate(com.apple.cie.foundationdb.MutationType, byte[], byte[]) + */ + public static byte[] encodeInt(long i) { + return ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(i).array(); + } + + /** + * Decode a little-endian encoded long integer from an 8-byte array. + * + * @param src the non-null, 8-element byte array from which to decode + * @return a decoded 64-bit integer + */ + public static long decodeInt(byte[] src) { + if(src.length != 8) { + throw new IllegalArgumentException("Source array must be of length 8"); + } + return ByteBuffer.wrap(src).order(ByteOrder.LITTLE_ENDIAN).getLong(); + } + + /** + * Gets a human readable version of a byte array. The bytes that correspond with + * ASCII printable characters [32-127) are passed through. Other bytes are + * replaced with {@code \x} followed by a two character zero-padded hex code for the + * byte. + * + * @param val the byte array for which to create a human readable form + * + * @return a modification of the byte array with unprintable characters replaced. + */ + public static String printable(byte[] val) { + if(val == null) + return null; + StringBuilder s = new StringBuilder(); + for(int i=0; i= 32 && b < 127 && b != '\\') s.append((char)b); + else if (b == '\\') s.append("\\\\"); + else s.append(String.format("\\x%02x", b)); + } + return s.toString(); + } + + private ByteArrayUtil() {} +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/Tuple.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/Tuple.java new file mode 100644 index 0000000000..e70586a83a --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/Tuple.java @@ -0,0 +1,575 @@ +/* + * Tuple.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.tuple; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.apple.cie.foundationdb.Range; + +/** + * Represents a set of elements that make up a sortable, typed key. This object + * is comparable with other {@code Tuple}s and will sort in Java in + * the same order in which they would sort in FoundationDB. {@code Tuple}s sort + * first by the first element, then by the second, etc. This makes the tuple layer + * ideal for building a variety of higher-level data models.
    + *

    Types

    + * A {@code Tuple} can + * contain byte arrays ({@code byte[]}), {@link String}s, {@link Number}s, and {@code null}. All + * {@code Number}s will be converted to a {@code long} integral value, so all + * floating point information will be lost and their range will be constrained to the range + * [{@code 2^63-1}, {@code -2^63}]. Note that for numbers outside this range the way that Java + * truncates integral values may yield unexpected results.
    + *

    {@code null} values

    + * The FoundationDB tuple specification has a special type-code for {@code None}; {@code nil}; or, + * as Java would understand it, {@code null}. + * The behavior of the layer in the presence of {@code null} varies by type with the intention + * of matching expected behavior in Java. {@code byte[]} and {@link String}s can be {@code null}, + * where integral numbers (i.e. {@code long}s) cannot. + * This means that the typed getters ({@link #getBytes(int) getBytes()} and {@link #getString(int) getString()}) + * will return {@code null} if the entry at that location was {@code null} and the typed adds + * ({@link #add(byte[])} and {@link #add(String)}) will accept {@code null}. The + * {@link #getLong(int) typed get for integers}, however, will throw a {@code NullPointerException} if + * the entry in the {@code Tuple} was {@code null} at that position.
    + *
    + * This class is not thread safe. + */ +public class Tuple implements Comparable, Iterable { + private List elements; + + private Tuple(List elements, Object newItem) { + this(new LinkedList(elements)); + this.elements.add(newItem); + } + + private Tuple(List elements) { + this.elements = new ArrayList(elements); + } + + /** + * Creates a copy of this {@code Tuple} with an appended last element. The parameter + * is untyped but only {@link String}, {@code byte[]}, {@link Number}s, and {@code null} are allowed. + * All {@code Number}s are converted to a 8 byte integral value, so all floating point + * information is lost. + * + * @param o the object to append. Must be {@link String}, {@code byte[]}, + * {@link Number}s, or {@code null}. + * + * @return a newly created {@code Tuple} + */ + public Tuple addObject(Object o) { + if(o != null && + !(o instanceof String) && + !(o instanceof byte[]) && + !(o instanceof Number)) { + throw new IllegalArgumentException("Parameter type (" + o.getClass().getName() + ") not recognized"); + } + return new Tuple(this.elements, o); + } + + /** + * Creates a copy of this {@code Tuple} with a {@code String} appended as the last element. + * + * @param s the {@code String} to append + * + * @return a newly created {@code Tuple} + */ + public Tuple add(String s) { + return new Tuple(this.elements, s); + } + + /** + * Creates a copy of this {@code Tuple} with a {@code long} appended as the last element. + * + * @param l the number to append + * + * @return a newly created {@code Tuple} + */ + public Tuple add(long l) { + return new Tuple(this.elements, l); + } + + /** + * Creates a copy of this {@code Tuple} with a {@code byte} array appended as the last element. + * + * @param b the {@code byte}s to append + * + * @return a newly created {@code Tuple} + */ + public Tuple add(byte[] b) { + return new Tuple(this.elements, b); + } + + /** + * Creates a copy of this {@code Tuple} with a {@code byte} array appended as the last element. + * + * @param b the {@code byte}s to append + * @param offset the starting index of {@code b} to add + * @param length the number of elements of {@code b} to copy into this {@code Tuple} + * + * @return a newly created {@code Tuple} + */ + public Tuple add(byte[] b, int offset, int length) { + return new Tuple(this.elements, Arrays.copyOfRange(b, offset, offset + length)); + } + + /** + * Create a copy of this {@code Tuple} with a list of items appended. + * + * @param o the list of objects to append. Elements must be {@link String}, {@code byte[]}, + * {@link Number}s, or {@code null}. + * + * @return a newly created {@code Tuple} + */ + public Tuple addAll(List o) { + List merged = new ArrayList(o.size() + this.elements.size()); + merged.addAll(this.elements); + merged.addAll(o); + return new Tuple(merged); + } + + /** + * Create a copy of this {@code Tuple} with all elements from anther {@code Tuple} appended. + * + * @param other the {@code Tuple} whose elements should be appended + * + * @return a newly created {@code Tuple} + */ + public Tuple addAll(Tuple other) { + List merged = new ArrayList(this.size() + other.size()); + merged.addAll(this.elements); + merged.addAll(other.peekItems()); + return new Tuple(merged); + } + + /** + * Get an encoded representation of this {@code Tuple}. Each element is encoded to + * {@code byte}s and concatenated. + * + * @return a serialized representation of this {@code Tuple}. + */ + public byte[] pack() { + return TupleUtil.pack(elements); + } + + /** + * Gets the unserialized contents of this {@code Tuple}. + * + * @return the elements that make up this {@code Tuple}. + */ + public List getItems() { + return new ArrayList(elements); + } + + /** + * Gets a {@link Stream} of the unserialized contents of this {@code Tuple}. + * + * @return a {@link Stream} of the elements that make up this {@code Tuple}. + */ + public Stream stream() { + return elements.stream(); + } + + /** + * Returns the internal elements that make up this tuple. For internal use only, as + * modifications to the result will mean that this Tuple is modified. + * + * @return the elements of this Tuple, without copying + */ + private List peekItems() { + return this.elements; + } + + /** + * Gets an {@code Iterator} over the {@code Objects} in this {@code Tuple}. This {@code Iterator} is + * unmodifiable and will throw an exception if {@link Iterator#remove() remove()} is called. + * + * @return an unmodifiable {@code Iterator} over the elements in the {@code Tuple}. + */ + @Override + public Iterator iterator() { + return Collections.unmodifiableList(this.elements).iterator(); + } + + /** + * Construct a new empty {@code Tuple}. After creation, items can be added + * with calls the the variations of {@code add()}. + * + * @see #from(Object...) + * @see #fromBytes(byte[]) + * @see #fromItems(Iterable) + */ + public Tuple() { + this.elements = new LinkedList(); + } + + /** + * Construct a new {@code Tuple} with elements decoded from a supplied {@code byte} array. + * + * @param bytes encoded {@code Tuple} source. Must not be {@code null} + * + * @return a newly constructed object. + */ + public static Tuple fromBytes(byte[] bytes) { + return fromBytes(bytes, 0, bytes.length); + } + + /** + * Construct a new {@code Tuple} with elements decoded from a supplied {@code byte} array. + * + * @param bytes encoded {@code Tuple} source. Must not be {@code null} + * + * @return a newly constructed object. + */ + public static Tuple fromBytes(byte[] bytes, int offset, int length) { + Tuple t = new Tuple(); + t.elements = TupleUtil.unpack(bytes, offset, length); + return t; + } + + /** + * Gets the number of elements in this {@code Tuple}. + * + * @return the count of elements + */ + public int size() { + return this.elements.size(); + } + + /** + * Determine if this {@code Tuple} contains no elements. + * + * @return {@code true} if this {@code Tuple} contains no elements, {@code false} otherwise + */ + public boolean isEmpty() { + return this.elements.isEmpty(); + } + + /** + * Gets an indexed item as a {@code long}. This function will not do type conversion + * and so will throw a {@code ClassCastException} if the element is not a number type. + * The element at the index may not be {@code null}. + * + * @param index the location of the item to return + * + * @return the item at {@code index} as a {@code long} + */ + public long getLong(int index) { + Object o = this.elements.get(index); + if(o == null) + throw new NullPointerException("Number types in Tuples may not be null"); + return ((Number)o).longValue(); + } + + /** + * Gets an indexed item as a {@code byte[]}. This function will not do type conversion + * and so will throw a {@code ClassCastException} if the tuple element is not a + * {@code byte} array. + * + * @param index the location of the element to return + * + * @return the item at {@code index} as a {@code byte[]} + */ + public byte[] getBytes(int index) { + Object o = this.elements.get(index); + // Check needed, since the null may be of type "Object" and may not be casted to byte[] + if(o == null) + return null; + return (byte[])o; + } + + /** + * Gets an indexed item as a {@code String}. This function will not do type conversion + * and so will throw a {@code ClassCastException} if the tuple element is not of + * {@code String} type. + * + * @param index the location of the element to return + * + * @return the item at {@code index} as a {@code String} + */ + public String getString(int index) { + Object o = this.elements.get(index); + // Check needed, since the null may be of type "Object" and may not be casted to byte[] + if(o == null) { + return null; + } + return (String)o; + } + + /** + * Gets an indexed item without forcing a type. + * + * @param index the index of the item to return + * + * @return an item from the list, without forcing type conversion + */ + public Object get(int index) { + return this.elements.get(index); + } + + /** + * Creates a new {@code Tuple} with the first item of this {@code Tuple} removed. + * + * @return a newly created {@code Tuple} + */ + public Tuple popFront() { + if(elements.size() == 0) + throw new IllegalStateException("Tuple contains no elements"); + + + List items = new ArrayList(elements.size() - 1); + for(int i = 1; i < this.elements.size(); i++) { + items.add(this.elements.get(i)); + } + return new Tuple(items); + } + + /** + * Creates a new {@code Tuple} with the last item of this {@code Tuple} removed. + * + * @return a newly created {@code Tuple} + */ + public Tuple popBack() { + if(elements.size() == 0) + throw new IllegalStateException("Tuple contains no elements"); + + + List items = new ArrayList(elements.size() - 1); + for(int i = 0; i < this.elements.size() - 1; i++) { + items.add(this.elements.get(i)); + } + return new Tuple(items); + } + + /** + * Returns a range representing all keys that encode {@code Tuple}s strictly starting + * with this {@code Tuple}. + *
    + *
    + * For example: + *
    +	 *   Tuple t = Tuple.from("a", "b");
    +	 *   Range r = t.range();
    + * {@code r} includes all tuples ("a", "b", ...) + * + * @return the keyspace range containing all {@code Tuple}s that have this {@code Tuple} + * as a prefix. + */ + public Range range() { + byte[] p = pack(); + //System.out.println("Packed tuple is: " + ByteArrayUtil.printable(p)); + return new Range(ByteArrayUtil.join(p, new byte[] {0x0}), + ByteArrayUtil.join(p, new byte[] {(byte)0xff})); + } + + /** + * Compare the byte-array representation of this {@code Tuple} against another. This method + * will sort {@code Tuple}s in the same order that they would be sorted as keys in + * FoundationDB. Returns a negative integer, zero, or a positive integer when this object's + * byte-array representation is found to be less than, equal to, or greater than the + * specified {@code Tuple}. + * + * @param t the {@code Tuple} against which to compare + * + * @return a negative integer, zero, or a positive integer when this {@code Tuple} is + * less than, equal, or greater than the parameter {@code t}. + */ + @Override + public int compareTo(Tuple t) { + return ByteArrayUtil.compareUnsigned(this.pack(), t.pack()); + } + + /** + * Returns a hash code value for this {@code Tuple}. + * {@inheritDoc} + * + * @return a hashcode + */ + @Override + public int hashCode() { + return Arrays.hashCode(this.pack()); + } + + /** + * Tests for equality with another {@code Tuple}. If the passed object is not a {@code Tuple} + * this returns false. If the object is a {@code Tuple}, this returns true if + * {@link Tuple#compareTo(Tuple) compareTo()} would return {@code 0}. + * + * @return {@code true} if {@code obj} is a {@code Tuple} and their binary representation + * is identical. + */ + @Override + public boolean equals(Object o) { + if(o == null) + return false; + if(o instanceof Tuple) { + return Arrays.equals(this.pack(), ((Tuple) o).pack()); + } + return false; + } + + /** + * Returns a string representing this {@code Tuple}. + * + * @return a string + */ + @Override + public String toString() { + StringBuilder s = new StringBuilder("("); + boolean first = true; + + for(Object o : elements) { + if(!first) { + s.append(", "); + } + + first = false; + if(o == null) { + s.append("null"); + } + else if(o instanceof String) { + s.append("\""); + s.append(o); + s.append("\""); + } + else if(o instanceof byte[]) { + s.append("b\""); + s.append(ByteArrayUtil.printable((byte[])o)); + s.append("\""); + } + else { + s.append(o); + } + } + + s.append(")"); + return s.toString(); + } + + /** + * Creates a new {@code Tuple} from a variable number of elements. The elements + * must follow the type guidelines from {@link Tuple#addObject(Object) add}, and so + * can only be {@link String}s, {@code byte[]}s, {@link Number}s, or {@code null}s. + * + * @param items the elements from which to create the {@code Tuple}. + * + * @return a newly created {@code Tuple} + */ + public static Tuple fromItems(Iterable items) { + Tuple t = new Tuple(); + for(Object o : items) { + t = t.addObject(o); + } + return t; + } + + /** + * Efficiently creates a new {@code Tuple} from a list of objects. The elements + * must follow the type guidelines from {@link Tuple#addObject(Object) add}, and so + * can only be {@link String}s, {@code byte[]}s, {@link Number}s, or {@code null}s. + * + * @param items the elements from which to create the {@code Tuple}. + * + * @return a newly created {@code Tuple} + */ + public static Tuple fromList(List items) { + return new Tuple(items); + } + + /** + * Efficiently creates a new {@code Tuple} from a {@link Stream} of objects. The + * elements must follow the type guidelines from {@link Tuple#addObject(Object) add}, + * and so can only be {@link String}s, {@code byte[]}s, {@link Number}s, or {@code null}s. + * + * @param items the {@link Stream} of items from which to create the {@code Tuple}. + * + * @return a newly created {@code Tuple} + */ + public static Tuple fromStream(Stream items) { + Tuple t = new Tuple(); + t.elements = items.collect(Collectors.toList()); + return t; + } + + /** + * Creates a new {@code Tuple} from a variable number of elements. The elements + * must follow the type guidelines from {@link Tuple#addObject(Object) add}, and so + * can only be {@link String}s, {@code byte[]}s, {@link Number}s, or {@code null}s. + * + * @param items the elements from which to create the {@code Tuple}. + * + * @return a newly created {@code Tuple} + */ + public static Tuple from(Object ... items) { + return fromList(Arrays.asList(items)); + } + + static void main(String[] args) { + for( int i : new int[] {10, 100, 1000, 10000, 100000, 1000000} ) { + createTuple(i); + } + + Tuple t = new Tuple(); + t = t.add(Long.MAX_VALUE); + t = t.add(Long.MAX_VALUE - 1); + t = t.add(Long.MAX_VALUE - 2); + t = t.add(1); + t = t.add(0); + t = t.add(-1); + t = t.add(Long.MIN_VALUE + 2); + t = t.add(Long.MIN_VALUE + 1); + t = t.add(Long.MIN_VALUE); + t = t.add("foo"); + byte[] bytes = t.pack(); + System.out.println("Packed: " + ByteArrayUtil.printable(bytes)); + List items = Tuple.fromBytes(bytes).getItems(); + for(Object obj : items) { + System.out.println(" -> type: (" + obj.getClass().getName() + "): " + obj); + } + + t = Tuple.fromStream(t.stream().map(item -> { + if(item instanceof String) { + return ((String)item).toUpperCase(); + } else { + return item; + } + })); + System.out.println("Upper cased: " + t); + } + + private static Tuple createTuple(int items) { + List elements = new ArrayList(items); + for(int i = 0; i < items; i++) { + elements.add(new byte[]{99}); + } + long start = System.currentTimeMillis(); + Tuple t = Tuple.fromList(elements); + t.pack(); + System.out.println("Took " + (System.currentTimeMillis() - start) + " ms for " + items + " (" + elements.size() + ")"); + return t; + } +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/TupleUtil.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/TupleUtil.java new file mode 100644 index 0000000000..6cdd93d2be --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/TupleUtil.java @@ -0,0 +1,233 @@ +/* + * TupleUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.tuple; + +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +class TupleUtil { + private static final byte nil = 0x0; + private static final byte[] nil_rep = new byte[] {nil, (byte)0xFF}; + private static final BigInteger[] size_limits; + private static final Charset UTF8; + + static { + size_limits = new BigInteger[9]; + for(int i = 0; i < 9; i++) { + size_limits[i] = (BigInteger.ONE).shiftLeft(i * 8).subtract(BigInteger.ONE); + } + UTF8 = Charset.forName("UTF-8"); + } + + static class DecodeResult { + final int end; + final Object o; + + DecodeResult(int pos, Object o) { + this.end = pos; + this.o = o; + } + } + + public static byte[] join(List items) { + return ByteArrayUtil.join(null, items); + } + + static byte[] encode(Object t) { + if(t == null) + return new byte[] {nil}; + if(t instanceof byte[]) + return encode((byte[])t); + if(t instanceof String) + return encode((String)t); + if(t instanceof Number) + return encode(((Number)t).longValue()); + throw new IllegalArgumentException("Unsupported data type: " + t.getClass().getName()); + } + + static byte[] encode(byte[] bytes) { + List list = new ArrayList(3); + list.add(new byte[] {0x1}); + list.add(ByteArrayUtil.replace(bytes, new byte[] {0x0}, nil_rep)); + list.add(new byte[] {0x0}); + + //System.out.println("Joining bytes..."); + return ByteArrayUtil.join(null, list); + } + + static byte[] encode(String s) { + List list = new ArrayList(3); + list.add(new byte[] {0x2}); + list.add(ByteArrayUtil.replace(s.getBytes(UTF8), new byte[] {0x0}, nil_rep)); + list.add(new byte[] {0x0}); + + //System.out.println("Joining string..."); + return ByteArrayUtil.join(null, list); + } + + static byte[] encode(long i) { + //System.out.println("Encoding integral " + i); + if(i == 0) { + return new byte[] { 20 }; + } + if(i > 0) { + int n = ByteArrayUtil.bisectLeft(size_limits, BigInteger.valueOf(i)); + assert n <= size_limits.length; + byte[] bytes = ByteBuffer.allocate(8).order(ByteOrder.BIG_ENDIAN).putLong(i).array(); + //System.out.println(" -- integral has 'n' of " + n + " and output bytes of " + bytes.length); + byte[] result = new byte[n+1]; + result[0] = (byte)(20 + n); + System.arraycopy(bytes, bytes.length - n, result, 1, n); + return result; + } + BigInteger bI = BigInteger.valueOf(i); + int n = ByteArrayUtil.bisectLeft(size_limits, bI.negate()); + + assert n >= 0 && n < size_limits.length; // can we do this? it seems to be required for the following statement + + long maxv = size_limits[n].add(bI).longValue(); + byte[] bytes = ByteBuffer.allocate(8).order(ByteOrder.BIG_ENDIAN).putLong(maxv).array(); + byte[] result = new byte[n+1]; + result[0] = (byte)(20 - n); + System.arraycopy(bytes, bytes.length - n, result, 1, n); + return result; + } + + static byte[] encode(Integer i) { + return encode(i.longValue()); + } + + static DecodeResult decode(byte[] rep, int pos, int last) { + //System.out.println("Decoding '" + ArrayUtils.printable(rep) + "' at " + pos); + + // SOMEDAY: codes over 127 will be a problem with the signed Java byte mess + int code = rep[pos]; + int start = pos + 1; + if(code == 0x0) { + return new DecodeResult(start, null); + } + if(code == 0x1) { + int end = ByteArrayUtil.findTerminator(rep, (byte)0x0, (byte)0xff, start, last); + //System.out.println("End of byte string: " + end); + byte[] range = ByteArrayUtil.replace(rep, start, end - start, nil_rep, new byte[] { nil }); + //System.out.println(" -> byte string contents: '" + ArrayUtils.printable(range) + "'"); + return new DecodeResult(end + 1, range); + } + if(code == 0x2) { + int end = ByteArrayUtil.findTerminator(rep, (byte)0x0, (byte)0xff, start, last); + //System.out.println("End of UTF8 string: " + end); + byte[] stringBytes = ByteArrayUtil.replace(rep, start, end - start, nil_rep, new byte[] { nil }); + String str = new String(stringBytes, UTF8); + //System.out.println(" -> UTF8 string contents: '" + str + "'"); + return new DecodeResult(end + 1, str); + } + if(code >=12 && code <=28) { + // decode a long + byte[] longBytes = new byte[9]; + Arrays.fill(longBytes, (byte)0); + boolean upper = code >= 20; + int n = upper ? code - 20 : 20 - code; + int end = start + n; + + if(rep.length < end) { + throw new RuntimeException("Invalid tuple (possible truncation)"); + } + + System.arraycopy(rep, start, longBytes, 9-n, n); + if (!upper) + for(int i=9-n; i<9; i++) + longBytes[i] = (byte)~longBytes[i]; + + BigInteger val = new BigInteger(longBytes); + if (!upper) val = val.negate(); + + if (val.compareTo(BigInteger.valueOf(Long.MIN_VALUE))<0 || + val.compareTo(BigInteger.valueOf(Long.MAX_VALUE))>0) + throw new RuntimeException("Value out of range for type long."); + + return new DecodeResult(end, val.longValue()); + } + throw new IllegalArgumentException("Unknown tuple data type " + code + " at index " + pos); + } + + static List unpack(byte[] bytes, int start, int length) { + List items = new LinkedList(); + int pos = start; + int end = start + length; + while(pos < bytes.length) { + DecodeResult decoded = decode(bytes, pos, end); + items.add(decoded.o); + pos = decoded.end; + } + return items; + } + + static byte[] pack(List items) { + if(items.size() == 0) + return new byte[0]; + + List parts = new ArrayList(items.size()); + for(Object t : items) { + //System.out.println("Starting encode: " + ArrayUtils.printable((byte[])t)); + byte[] encoded = encode(t); + //System.out.println(" encoded -> '" + ArrayUtils.printable(encoded) + "'"); + parts.add(encoded); + } + //System.out.println("Joining whole tuple..."); + return ByteArrayUtil.join(null, parts); + } + + public static void main(String[] args) { + try { + byte[] bytes = encode( 4 ); + assert 4 == (Integer)(decode( bytes, 0, bytes.length ).o); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Error " + e.getMessage()); + } + + try { + byte[] bytes = encode( "\u021Aest \u0218tring" ); + String string = (String)(decode( bytes, 0, bytes.length ).o); + System.out.println("contents -> " + string); + assert "\u021Aest \u0218tring" == string; + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Error " + e.getMessage()); + } + + /*Object[] a = new Object[] { "\u0000a", -2, "b\u0001", 12345, ""}; + List o = Arrays.asList(a); + byte[] packed = pack( o ); + System.out.println("packed length: " + packed.length); + o = unpack( packed ); + System.out.println("unpacked elements: " + packed); + for(Object obj : o) + System.out.println(" -> type: " + obj.getClass().getName());*/ + } + private TupleUtil() {} +} diff --git a/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/package-info.java b/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/package-info.java new file mode 100644 index 0000000000..3f1b37369a --- /dev/null +++ b/bindings/java/src-completable/main/com/apple/cie/foundationdb/tuple/package-info.java @@ -0,0 +1,29 @@ +/* + * package-info.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides a set of utilities for serializing and deserializing typed data + * for use in FoundationDB. When packed together into a {@link com.apple.cie.foundationdb.tuple.Tuple} + * this data is suitable for use as an index or organizational structure within FoundationDB + * keyspace. See general Tuple + * documentation for information about how Tuples sort and can be used to efficiently + * model data. + */ +package com.apple.cie.foundationdb.tuple; \ No newline at end of file diff --git a/bindings/java/src-completable/main/overview.html.in b/bindings/java/src-completable/main/overview.html.in new file mode 100644 index 0000000000..9ec77aef8a --- /dev/null +++ b/bindings/java/src-completable/main/overview.html.in @@ -0,0 +1,90 @@ + + +This documents the client API for using FoundationDB from Java.
    +
    +

    Installation

    +FoundationDB's Java bindings rely on native libraries that are installed as part of the +FoundationDB client binaries installation (see + +Installing FoundationDB client binaries). The FoundationDB Java bindings are available +through Artifactory. To use them in your Maven-enabled project, add a dependency to your +pom.xml like:
    +
    +{@code
    +
    +    com.apple.cie.foundationdb
    +    fdb-java-completable
    +    VERSION
    +
    +}
    +
    +Alternatively, simply download the JAR from +Artifactory +and add it to your classpath.
    +
    +

    Getting started

    +To start using FoundationDB from Java, create an instance of the +{@link FDB FoundationDB API interface} with the version of the +API that you want to use (this release of the FoundationDB Java API supports only version {@code 500}). +With this API object you can then open {@link Cluster}s and +{@link Database}s and start using {@link Transaction}s. +Here we give an example. The example relies on a cluster file at the +default location +for your platform and a running server.
    +
    +
    +{@code
    +import com.apple.cie.foundationdb.*;
    +import Function;
    +import Tuple;
    +
    +public class Example {
    +  public static void main(String[] args) {
    +    FDB fdb = FDB.selectAPIVersion(500);
    +    Database db = fdb.open();
    +
    +    // Run an operation on the database
    +    db.run(new Function() {
    +      public Void apply(Transaction tr) {
    +        tr.set(Tuple.from("hello").pack(), Tuple.from("world").pack());
    +        return null;
    +      }
    +    });
    +
    +    // Get the value of 'hello' from the database
    +    String hello = db.run(new Function() {
    +      public String apply(Transaction tr) {
    +        byte[] result = tr.get(Tuple.from("hello").pack()).get();
    +        return Tuple.fromBytes(result).getString(0);
    +      }
    +    });
    +    System.out.println("Hello " + hello);
    +  }
    +}
    +}
    +
    +

    FoundationDB {@link com.apple.cie.foundationdb.tuple Tuple API}

    +The {@link com.apple.cie.foundationdb.tuple Tuple API} is provided with the core Java API for FoundationDB. +This layer is provided in some form in all official language bindings. It enables +cross-language support for storing and retrieving typed data from the +binary data that FoundationDB supports. And, just as importantly, data packed into +{@code Tuple}s and used as keys sort in predictable and useful ways. See the +{@link com.apple.cie.foundationdb.tuple Tuple class documentation} for information about use in Java +and general Tuple documentation +for information about how Tuples sort and can be used to efficiently model data. +
    +

    FoundationDB {@link com.apple.cie.foundationdb.directory Directory API}

    +The {@link com.apple.cie.foundationdb.directory Directory API} is provided with the core +Java API for FoundationDB. This layer is provided in some form in all official +language bindings. The FoundationDB API provides directories as a tool for +managing related {@link Subspace}s. Directories are a +recommended approach for administering applications. Each application should +create or open at least one directory to manage its subspaces. Directories are +identified by hierarchical paths analogous to the paths in a Unix-like file system. +A path is represented as a List of strings. Each directory has an associated +subspace used to store its content. The layer maps each path to a short prefix used +for the corresponding subspace. In effect, directories provide a level of indirection +for access to subspaces. + + + diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/AsListTest.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/AsListTest.java new file mode 100644 index 0000000000..a0c28147fb --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/AsListTest.java @@ -0,0 +1,88 @@ +/* + * AsListTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.LocalityUtil; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncUtil; + +import java.util.function.Function; +import java.util.concurrent.CompletableFuture; + +public class AsListTest { + /** + * When the database contains keys a, b, c, d, e -- this should return 5 items, + * a bug made the addition of the clear into the result returning 0 items. + */ + public static void main(String[] args) { + FDB fdb = FDB.selectAPIVersion(500); + Database database = fdb.open("T:\\circus\\tags\\RebarCluster-bbc\\cluster_id.txt"); + database.options().setLocationCacheSize(42); + Transaction tr = database.createTransaction(); + //tr.clear("g".getBytes()); + /*tr.clear("bbb".getBytes()); + AsyncIterable query = tr.getRange( + KeySelector.firstGreaterOrEqual("a".getBytes()), + KeySelector.firstGreaterOrEqual("e".getBytes()), + Integer.MAX_VALUE); + //List list = query.asList().get(); + //System.out.println("List size: " + list.size()); +*/ + String[] keyAddresses = LocalityUtil.getAddressesForKey(tr, "a".getBytes()).join(); + for(String s : keyAddresses) { + System.out.println(" @ " + s); + } + + @SuppressWarnings("unused") + CompletableFuture i = AsyncUtil.applySafely(new Function>() { + @Override + public CompletableFuture apply(Exception o) { + return CompletableFuture.completedFuture(3); + } + }, new RuntimeException()); + + CompletableFuture f = null; + + @SuppressWarnings({ "unused", "null" }) + CompletableFuture g = f.thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Integer o) { + return CompletableFuture.completedFuture(o.toString()); + } + }); + + @SuppressWarnings({ "unused", "null" }) + CompletableFuture g2 = f.thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Integer o) { + return CompletableFuture.completedFuture(o.toString()); + } + }).exceptionally(new Function() { + @Override + public String apply(Throwable o) { + // TODO Auto-generated method stub + return null; + } + }); + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/AsyncDirectoryExtension.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/AsyncDirectoryExtension.java new file mode 100644 index 0000000000..1281b63ddb --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/AsyncDirectoryExtension.java @@ -0,0 +1,445 @@ +/* + * AsyncDirectoryExtension.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.directory.Directory; +import com.apple.cie.foundationdb.directory.DirectoryLayer; +import com.apple.cie.foundationdb.directory.DirectorySubspace; +import com.apple.cie.foundationdb.subspace.Subspace; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +class AsyncDirectoryExtension { + List dirList = new ArrayList(); + int dirIndex = 0; + int errorIndex = 0; + + public AsyncDirectoryExtension() { + dirList.add(DirectoryLayer.getDefault()); + } + + Directory directory() { + return (Directory)dirList.get(dirIndex); + } + + Subspace subspace() { + return (Subspace)dirList.get(dirIndex); + } + + CompletableFuture processInstruction(final Instruction inst) { + return executeInstruction(inst) + .exceptionally(new Function() { + @Override + public Void apply(Throwable e) { + DirectoryUtil.pushError(inst, e, dirList); + return null; + } + }); + } + + CompletableFuture executeInstruction(final Instruction inst) { + final DirectoryOperation op = DirectoryOperation.valueOf(inst.op); + + if(op == DirectoryOperation.DIRECTORY_CREATE_SUBSPACE) { + return DirectoryUtil.popTuple(inst) + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(final Tuple prefix) { + return inst.popParam() + .thenApplyAsync(new Function() { + @Override + public Void apply(Object rawPrefix) { + dirList.add(new Subspace(prefix, (byte[])rawPrefix)); + return null; + } + }); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE_LAYER) { + return inst.popParams(3) + .thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List params) { + Subspace nodeSubspace = (Subspace)dirList.get(StackUtils.getInt(params.get(0))); + Subspace contentSubspace = (Subspace)dirList.get(StackUtils.getInt(params.get(1))); + boolean allowManualPrefixes = StackUtils.getInt(params.get(2)) == 1; + + if(nodeSubspace == null || contentSubspace == null) + dirList.add(null); + else + dirList.add(new DirectoryLayer(nodeSubspace, contentSubspace, allowManualPrefixes)); + + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_CHANGE) { + return inst.popParam() + .thenApplyAsync(new Function() { + @Override + public Void apply(Object index) { + dirIndex = StackUtils.getInt(index); + if(dirList.get(dirIndex) == null) + dirIndex = errorIndex; + + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_SET_ERROR_INDEX) { + return inst.popParam() + .thenApplyAsync(new Function() { + @Override + public Void apply(Object index) { + errorIndex = StackUtils.getInt(index); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN || op == DirectoryOperation.DIRECTORY_OPEN) { + return DirectoryUtil.popPath(inst) + .thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(final List path) { + return inst.popParam() + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Object layer) { + CompletableFuture dir; + if(layer == null) { + if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN) + dir = directory().createOrOpen(inst.tcx, path); + else + dir = directory().open(inst.readTcx, path); + } + else { + if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN) + dir = directory().createOrOpen(inst.tcx, path, (byte[])layer); + else + dir = directory().open(inst.readTcx, path, (byte[])layer); + } + + return dir.thenApplyAsync(new Function() { + @Override + public Void apply(DirectorySubspace dirSubspace) { + dirList.add(dirSubspace); + return null; + } + }); + } + }); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE) { + return DirectoryUtil.popPath(inst) + .thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(final List path) { + return inst.popParams(2) + .thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(List params) { + byte[] layer = (byte[])params.get(0); + byte[] prefix = (byte[])params.get(1); + + CompletableFuture dir; + if(layer == null && prefix == null) + dir = directory().create(inst.tcx, path); + else if(prefix == null) + dir = directory().create(inst.tcx, path, layer); + else { + if(layer == null) + layer = new byte[0]; + + dir = directory().create(inst.tcx, path, layer, prefix); + } + + return dir.thenApplyAsync(new Function() { + @Override + public Void apply(DirectorySubspace dirSubspace) { + dirList.add(dirSubspace); + return null; + } + }); + } + }); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_MOVE) { + return DirectoryUtil.popPaths(inst, 2) + .thenComposeAsync(new Function>, CompletableFuture>() { + @Override + public CompletableFuture apply(List> paths) { + return directory().move(inst.tcx, paths.get(0), paths.get(1)); + } + }) + .thenApplyAsync(new Function() { + @Override + public Void apply(DirectorySubspace dirSubspace) { + dirList.add(dirSubspace); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_MOVE_TO) { + return DirectoryUtil.popPath(inst) + .thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(List newAbsolutePath) { + return directory().moveTo(inst.tcx, newAbsolutePath); + } + }) + .thenApplyAsync(new Function() { + @Override + public Void apply(DirectorySubspace dirSubspace) { + dirList.add(dirSubspace); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_REMOVE) { + return inst.popParam() + .thenComposeAsync(new Function>>>() { + @Override + public CompletableFuture>> apply(Object count) { + return DirectoryUtil.popPaths(inst, StackUtils.getInt(count)); + } + }) + .thenComposeAsync(new Function>, CompletableFuture>() { + @Override + public CompletableFuture apply(List> path) { + if(path.size() == 0) + return directory().remove(inst.tcx); + else + return directory().remove(inst.tcx, path.get(0)); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_REMOVE_IF_EXISTS) { + return inst.popParam() + .thenComposeAsync(new Function>>>() { + @Override + public CompletableFuture>> apply(Object count) { + return DirectoryUtil.popPaths(inst, StackUtils.getInt(count)); + } + }) + .thenComposeAsync(new Function>, CompletableFuture>() { + @Override + public CompletableFuture apply(List> path) { + if(path.size() == 0) + return AsyncUtil.success(directory().removeIfExists(inst.tcx)); + else + return AsyncUtil.success(directory().removeIfExists(inst.tcx, path.get(0))); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_LIST) { + return inst.popParam() + .thenComposeAsync(new Function>>>() { + @Override + public CompletableFuture>> apply(Object count) { + return DirectoryUtil.popPaths(inst, StackUtils.getInt(count)); + } + }) + .thenComposeAsync(new Function>, CompletableFuture>>() { + @Override + public CompletableFuture> apply(List> path) { + if(path.size() == 0) + return directory().list(inst.readTcx); + else + return directory().list(inst.readTcx, path.get(0)); + } + }) + .thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List children) { + inst.push(Tuple.fromItems(children).pack()); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_EXISTS) { + return inst.popParam() + .thenComposeAsync(new Function>>>() { + @Override + public CompletableFuture>> apply(Object count) { + return DirectoryUtil.popPaths(inst, StackUtils.getInt(count)); + } + }) + .thenComposeAsync(new Function>, CompletableFuture>() { + @Override + public CompletableFuture apply(List> path) { + if(path.size() == 0) + return directory().exists(inst.readTcx); + else + return directory().exists(inst.readTcx, path.get(0)); + } + }) + .thenApplyAsync(new Function() { + @Override + public Void apply(Boolean exists){ + inst.push(exists ? 1 : 0); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_PACK_KEY) { + return DirectoryUtil.popTuple(inst) + .thenApplyAsync(new Function() { + @Override + public Void apply(Tuple keyTuple) { + inst.push(subspace().pack(keyTuple)); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_UNPACK_KEY) { + return inst.popParam() + .thenApplyAsync(new Function() { + @Override + public Void apply(Object key) { + Tuple tup = subspace().unpack((byte[])key); + for(Object o : tup) + inst.push(o); + + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_RANGE) { + return DirectoryUtil.popTuple(inst) + .thenApplyAsync(new Function() { + @Override + public Void apply(Tuple tup) { + Range range = subspace().range(tup); + inst.push(range.begin); + inst.push(range.end); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_CONTAINS) { + return inst.popParam() + .thenApplyAsync(new Function() { + @Override + public Void apply(Object key) { + inst.push(subspace().contains((byte[])key) ? 1 : 0); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_OPEN_SUBSPACE) { + return DirectoryUtil.popTuple(inst) + .thenApplyAsync(new Function() { + @Override + public Void apply(Tuple prefix) { + dirList.add(subspace().subspace(prefix)); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_LOG_SUBSPACE) { + return inst.popParam() + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(final Object prefix) { + return inst.tcx.runAsync(new Function>() { + @Override + public CompletableFuture apply(Transaction tr) { + tr.set(ByteArrayUtil.join((byte[])prefix, new Tuple().add(dirIndex).pack()), subspace().getKey()); + return CompletableFuture.completedFuture(null); + } + }); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_LOG_DIRECTORY) { + return inst.popParam() + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Object prefix) { + final Subspace logSubspace = new Subspace(new Tuple().add(dirIndex), (byte[])prefix); + return inst.tcx.runAsync(new Function>() { + @Override + public CompletableFuture apply(final Transaction tr) { + return directory().exists(tr) + .thenComposeAsync(new Function>>() { + @Override + public CompletableFuture> apply(Boolean exists) { + tr.set(logSubspace.pack("path"), Tuple.fromItems(directory().getPath()).pack()); + tr.set(logSubspace.pack("layer"), new Tuple().add(directory().getLayer()).pack()); + tr.set(logSubspace.pack("exists"), new Tuple().add(exists ? 1 : 0).pack()); + if(exists) + return directory().list(tr); + else + return CompletableFuture.completedFuture(new ArrayList()); + } + }) + .thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List children) { + tr.set(logSubspace.pack("children"), Tuple.fromItems(children).pack()); + return null; + } + }); + } + }); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_STRIP_PREFIX) { + return inst.popParam() + .thenApplyAsync(new Function() { + @Override + public Void apply(Object param) { + byte[] str = (byte[])param; + byte[] rawPrefix = subspace().getKey(); + + if(str.length < rawPrefix.length) + throw new RuntimeException("String does not start with raw prefix"); + + for(int i = 0; i < rawPrefix.length; ++i) + if(str[i] != rawPrefix[i]) + throw new RuntimeException("String does not start with raw prefix"); + + inst.push(Arrays.copyOfRange(str, rawPrefix.length, str.length)); + return null; + } + }); + } + else { + throw new RuntimeException("Unknown operation:" + inst.op); + } + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/AsyncStackTester.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/AsyncStackTester.java new file mode 100644 index 0000000000..508ee2a334 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/AsyncStackTester.java @@ -0,0 +1,821 @@ +/* + * AsyncStackTester.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.*; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.KeySelector; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.MutationType; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.ReadTransaction; +import com.apple.cie.foundationdb.StreamingMode; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +public class AsyncStackTester { + static final String DIRECTORY_PREFIX = "DIRECTORY_"; + + static class WaitEmpty implements Function> { + private final byte[] prefix; + WaitEmpty(byte[] prefix) { + this.prefix = prefix; + } + + @Override + public CompletableFuture apply(Transaction tr) { + return tr.getRange(Range.startsWith(prefix)).asList().thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List list) { + if(list.size() > 0) { + //System.out.println(" - Throwing new fake commit error..."); + throw new FDBException("ERROR: Fake commit conflict", 1020); + } + return null; + } + }); + } + } + + static CompletableFuture processInstruction(final Instruction inst) { + StackOperation op = StackOperation.valueOf(inst.op); + if(op == StackOperation.PUSH) { + Object item = inst.tokens.get(1); + inst.push(item); + /*if(item instanceof byte[]) + System.out.println(inst.context.preStr + " - " + "Pushing '" + ByteArrayUtil.printable((byte[]) item) + "'"); + else if(item instanceof Number) + System.out.println(inst.context.preStr + " - " + "Pushing " + ((Number)item).longValue()); + else if(item instanceof String) + System.out.println(inst.context.preStr + " - " + "Pushing (utf8) '" + item.toString() + "'"); + else if(item == null) + System.out.println(inst.context.preStr + " - " + "Pushing null"); + else + System.out.println(inst.context.preStr + " - " + "Pushing item of type " + item.getClass().getName());*/ + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.POP) { + inst.pop(); + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.DUP) { + if(inst.size() == 0) + throw new RuntimeException("No stack bro!! (" + inst.context.preStr + ")"); + StackEntry e = inst.pop(); + inst.push(e); + inst.push(e); + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.EMPTY_STACK) { + inst.clear(); + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.SWAP) { + return inst.popParam() + .thenApplyAsync(new Function() { + @Override + public Void apply(Object param) { + int index = StackUtils.getInt(param); + if(index >= inst.size()) + throw new IllegalArgumentException("Stack index not valid"); + + inst.swap(index); + return null; + } + }); + } + else if(op == StackOperation.WAIT_FUTURE) { + return popAndWait(inst) + .thenApplyAsync(new Function() { + @Override + public Void apply(StackEntry e) { + inst.push(e); + return null; + } + }); + } + else if(op == StackOperation.WAIT_EMPTY) { + return inst.popParam() + .thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Object param) { + WaitEmpty retryable = new WaitEmpty((byte[])param); + return inst.context.db.runAsync(retryable).thenApply(new Function() { + @Override + public Void apply(Void o) { + inst.push( "WAITED_FOR_EMPTY".getBytes()); + return null; + } + }); + } + }); + } + else if(op == StackOperation.START_THREAD) { + return inst.popParam() + .thenApplyAsync(new Function() { + @Override + public Void apply(Object param) { + //System.out.println(inst.context.preStr + " - " + "Starting new thread at prefix: " + ByteArrayUtil.printable((byte[]) params.get(0))); + inst.context.addContext((byte[])param); + return null; + } + }); + } + else if(op == StackOperation.NEW_TRANSACTION) { + inst.context.newTransaction(); + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.USE_TRANSACTION) { + return inst.popParam() + .thenApplyAsync(new Function() { + public Void apply(Object param) { + inst.context.switchTransaction((byte[])param); + return null; + } + }); + } + else if(op == StackOperation.SET) { + return inst.popParams(2).thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(final List params) { + /*System.out.println(inst.context.preStr + " - " + "Setting '" + ByteArrayUtil.printable((byte[]) params.get(0)) + + "' to '" + ByteArrayUtil.printable((byte[]) params.get(1)) + "'"); */ + return executeMutation(inst, new Function>() { + @Override + public CompletableFuture apply(Transaction tr) { + tr.set((byte[])params.get(0), (byte[])params.get(1)); + return CompletableFuture.completedFuture(null); + } + }); + } + }); + } + else if(op == StackOperation.CLEAR) { + return inst.popParam().thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(final Object param) { + //System.out.println(inst.context.preStr + " - " + "Clearing: '" + ByteArrayUtil.printable((byte[])param) + "'"); + return executeMutation(inst, new Function>() { + @Override + public CompletableFuture apply(Transaction tr) { + tr.clear((byte[])param); + return CompletableFuture.completedFuture(null); + } + }); + } + }); + } + else if(op == StackOperation.CLEAR_RANGE) { + return inst.popParams(2).thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(final List params) { + return executeMutation(inst, new Function>() { + @Override + public CompletableFuture apply(Transaction tr) { + tr.clear((byte[])params.get(0), (byte[])params.get(1)); + return CompletableFuture.completedFuture(null); + } + }); + } + }); + } + else if(op == StackOperation.CLEAR_RANGE_STARTS_WITH) { + return inst.popParam().thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(final Object param) { + return executeMutation(inst, new Function>() { + @Override + public CompletableFuture apply(Transaction tr) { + tr.clear(Range.startsWith((byte[])param)); + return CompletableFuture.completedFuture(null); + } + }); + } + }); + } + else if(op == StackOperation.ATOMIC_OP) { + return inst.popParams(3).thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(final List params) { + final MutationType optype = MutationType.valueOf((String)params.get(0)); + return executeMutation(inst, + new Function>() { + @Override + public CompletableFuture apply(Transaction tr) { + tr.mutate(optype, (byte[])params.get(1), (byte[])params.get(2)); + return CompletableFuture.completedFuture(null); + } + } + ); + } + }); + } + else if(op == StackOperation.COMMIT) { + inst.push(inst.tr.commit()); + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.RESET) { + inst.setTransaction(inst.tr.reset()); + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.CANCEL) { + inst.tr.cancel(); + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.READ_CONFLICT_RANGE) { + return inst.popParams(2).thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List params) { + inst.tr.addReadConflictRange((byte[])params.get(0), (byte[])params.get(1)); + inst.push("SET_CONFLICT_RANGE".getBytes()); + return null; + } + }); + } + else if(op == StackOperation.WRITE_CONFLICT_RANGE) { + return inst.popParams(2).thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List params) { + inst.tr.addWriteConflictRange((byte[])params.get(0), (byte[])params.get(1)); + inst.push("SET_CONFLICT_RANGE".getBytes()); + return null; + } + }); + } + else if(op == StackOperation.READ_CONFLICT_KEY) { + return inst.popParam().thenApplyAsync(new Function() { + @Override + public Void apply(Object param) { + inst.tr.addReadConflictKey((byte[])param); + inst.push("SET_CONFLICT_KEY".getBytes()); + return null; + } + }); + } + else if(op == StackOperation.WRITE_CONFLICT_KEY) { + return inst.popParam().thenApplyAsync(new Function() { + @Override + public Void apply(Object param) { + inst.tr.addWriteConflictKey((byte[])param); + inst.push("SET_CONFLICT_KEY".getBytes()); + return null; + } + }); + } + else if(op == StackOperation.DISABLE_WRITE_CONFLICT) { + inst.tr.options().setNextWriteNoWriteConflictRange(); + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.GET) { + return inst.popParam().thenApplyAsync(new Function() { + @Override + public Void apply(Object param) { + inst.push(inst.readTcx.readAsync(readTr -> readTr.get((byte[]) param))); + return null; + } + }); + } + else if(op == StackOperation.GET_RANGE) { + return inst.popParams(5).thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(List params) { + int limit = StackUtils.getInt(params.get(2)); + boolean reverse = StackUtils.getBoolean(params.get(3)); + StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(4), StreamingMode.ITERATOR.code())); + + CompletableFuture> range = inst.readTcx.readAsync(readTr -> readTr.getRange((byte[])params.get(0), (byte[])params.get(1), limit, reverse, mode).asList()); + return pushRange(inst, range); + } + }); + } + else if(op == StackOperation.GET_RANGE_SELECTOR) { + return inst.popParams(10).thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(List params) { + int limit = StackUtils.getInt(params.get(6)); + boolean reverse = StackUtils.getBoolean(params.get(7)); + StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(8), StreamingMode.ITERATOR.code())); + + KeySelector start = StackUtils.createSelector(params.get(0),params.get(1), params.get(2)); + KeySelector end = StackUtils.createSelector(params.get(3), params.get(4), params.get(5)); + + CompletableFuture> range = inst.readTcx.readAsync(readTr -> readTr.getRange(start, end, limit, reverse, mode).asList()); + return pushRange(inst, range, (byte[])params.get(9)); + } + }); + } + else if(op == StackOperation.GET_RANGE_STARTS_WITH) { + return inst.popParams(4).thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(List params) { + int limit = StackUtils.getInt(params.get(1)); + boolean reverse = StackUtils.getBoolean(params.get(2)); + StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(3), StreamingMode.ITERATOR.code())); + + CompletableFuture> range = inst.readTcx.readAsync(readTr -> readTr.getRange(Range.startsWith((byte[])params.get(0)), limit, reverse, mode).asList()); + return pushRange(inst, range); + } + }); + } + else if(op == StackOperation.GET_KEY) { + return inst.popParams(4).thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List params) { + KeySelector start = StackUtils.createSelector(params.get(0),params.get(1), params.get(2)); + inst.push(inst.readTcx.readAsync(readTr -> executeGetKey(readTr.getKey(start), (byte[])params.get(3)))); + return null; + } + }); + } + else if(op == StackOperation.GET_READ_VERSION) { + return inst.readTr.getReadVersion().thenApplyAsync(new Function() { + @Override + public Void apply(Long readVersion) { + inst.context.lastVersion = readVersion; + inst.push("GOT_READ_VERSION".getBytes()); + return null; + } + }); + } + else if(op == StackOperation.GET_COMMITTED_VERSION) { + try { + inst.context.lastVersion = inst.tr.getCommittedVersion(); + inst.push("GOT_COMMITTED_VERSION".getBytes()); + } + catch(FDBException e) { + StackUtils.pushError(inst, e); + } + + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.GET_VERSIONSTAMP) { + try { + inst.push(inst.tr.getVersionstamp()); + } + catch(FDBException e) { + StackUtils.pushError(inst, e); + } + + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.SET_READ_VERSION) { + if(inst.context.lastVersion == null) + throw new IllegalArgumentException("Read version has not been read"); + inst.tr.setReadVersion(inst.context.lastVersion); + return CompletableFuture.completedFuture(null); + } + else if(op == StackOperation.ON_ERROR) { + return inst.popParam().thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Object param) { + int errorCode = StackUtils.getInt(param); + + // 1102 (future_released) and 2015 (future_not_set) are not errors to Java. + // This is never encountered by user code, so we have to do something rather + // messy here to get compatibility with other languages. + // + // First, try on error with a retryable error. If it fails, then the transaction is in + // a failed state and we should rethrow the error. Otherwise, throw the original error. + boolean filteredError = errorCode == 1102 || errorCode == 2015; + + FDBException err = new FDBException("Fake testing error", filteredError ? 1020 : errorCode); + CompletableFuture f = inst.tr.onError(err) + .whenComplete((tr, t) -> { + if(t != null) { + inst.context.newTransaction(); // Other bindings allow reuse of non-retryable transactions, so we need to emulate that behavior. + } + else { + inst.setTransaction(tr); + } + }) + .thenApply(v -> null); + + if(filteredError) { + f.join(); + throw new FDBException("Fake testing error", errorCode); + } + + inst.push(f); + return CompletableFuture.completedFuture(null); + } + }); + } + else if(op == StackOperation.SUB) { + return inst.popParams(2).thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List params) { + long result = StackUtils.getNumber(params.get(0)).longValue() - + StackUtils.getNumber(params.get(1)).longValue(); + inst.push(result); + return null; + } + }); + } + else if(op == StackOperation.CONCAT) { + return inst.popParams(2).thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List params) { + if(params.get(0) instanceof String) { + inst.push((String)params.get(0) + (String)params.get(1)); + } + else { + inst.push(ByteArrayUtil.join((byte[])params.get(0), (byte[])params.get(1))); + } + + return null; + } + }); + } + else if(op == StackOperation.TUPLE_PACK) { + return inst.popParam().thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Object param) { + int tupleSize = StackUtils.getInt(param); + //System.out.println(inst.context.preStr + " - " + "Packing top " + tupleSize + " items from stack"); + return inst.popParams(tupleSize).thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List elements) { + byte[] coded = Tuple.fromItems(elements).pack(); + //System.out.println(inst.context.preStr + " - " + " -> result '" + ByteArrayUtil.printable(coded) + "'"); + inst.push(coded); + return null; + } + }); + } + }); + } + else if(op == StackOperation.TUPLE_UNPACK) { + return inst.popParam().thenApplyAsync(new Function() { + @Override + public Void apply(Object param) { + /*System.out.println(inst.context.preStr + " - " + "Unpacking tuple code: " + + ByteArrayUtil.printable((byte[]) param)); */ + Tuple t = Tuple.fromBytes((byte[])param); + for(Object o : t.getItems()) { + byte[] itemBytes = Tuple.from(o).pack(); + inst.push(itemBytes); + } + return null; + } + }); + } + else if(op == StackOperation.TUPLE_RANGE) { + return inst.popParam().thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Object param) { + int tupleSize = StackUtils.getInt(param); + //System.out.println(inst.context.preStr + " - " + "Tuple range with top " + tupleSize + " items from stack"); + return inst.popParams(tupleSize).thenApplyAsync(new Function, Void>() { + @Override + public Void apply(List elements) { + Range range = Tuple.fromItems(elements).range(); + inst.push(range.begin); + inst.push(range.end); + return null; + } + }); + } + }); + } + else if(op == StackOperation.UNIT_TESTS) { + inst.context.db.options().setLocationCacheSize(100001); + return inst.context.db.runAsync(tr -> { + tr.options().setPrioritySystemImmediate(); + tr.options().setPriorityBatch(); + tr.options().setCausalReadRisky(); + tr.options().setCausalWriteRisky(); + tr.options().setReadYourWritesDisable(); + tr.options().setReadAheadDisable(); + tr.options().setReadSystemKeys(); + tr.options().setAccessSystemKeys(); + tr.options().setDurabilityDevNullIsWebScale(); + tr.options().setTimeout(60*1000); + tr.options().setRetryLimit(50); + tr.options().setMaxRetryDelay(100); + tr.options().setUsedDuringCommitProtectionDisable(); + tr.options().setTransactionLoggingEnable("my_transaction"); + + if(!(new FDBException("Fake", 1020)).isRetryable() || + (new FDBException("Fake", 10)).isRetryable()) + throw new RuntimeException("Unit test failed: Error predicate incorrect"); + + byte[] test = {(byte)0xff}; + return tr.get(test).thenRunAsync(() -> {}); + }).exceptionally(t -> { + throw new RuntimeException("Unit tests failed: " + t.getMessage()); + }); + } + else if(op == StackOperation.LOG_STACK) { + return inst.popParam().thenComposeAsync(prefix -> doLogStack(inst, (byte[])prefix)); + } + + throw new IllegalArgumentException("Unrecognized (or unimplemented) operation"); + } + + private static CompletableFuture executeMutation(final Instruction inst, Function> r) { + // run this with a retry loop + return inst.tcx.runAsync(r).thenApplyAsync(new Function() { + @Override + public Void apply(Void a) { + if(inst.isDatabase) + inst.push("RESULT_NOT_PRESENT".getBytes()); + return null; + } + }); + } + + private static CompletableFuture executeGetKey(final CompletableFuture keyFuture, final byte[] prefixFilter) { + return keyFuture.thenApplyAsync(new Function() { + @Override + public byte[] apply(byte[] key) { + if(ByteArrayUtil.startsWith(key, prefixFilter)) { + return key; + } + else if(ByteArrayUtil.compareUnsigned(key, prefixFilter) < 0) { + return prefixFilter; + } + else { + return ByteArrayUtil.strinc(prefixFilter); + } + } + }); + } + + private static CompletableFuture doLogStack(final Instruction inst, final byte[] prefix) { + Map entries = new HashMap<>(); + while(inst.size() > 0) { + entries.put(inst.size() - 1, inst.pop()); + if(entries.size() == 100) { + return logStack(inst.context.db, entries, prefix).thenComposeAsync(v -> doLogStack(inst, prefix)); + } + } + + return logStack(inst.context.db, entries, prefix); + } + + private static CompletableFuture logStack(final Database db, final Map entries, final byte[] prefix) { + return db.runAsync(tr -> { + for(Map.Entry it : entries.entrySet()) { + byte[] pk = ByteArrayUtil.join(prefix, Tuple.from(it.getKey(), it.getValue().idx).pack()); + byte[] pv = Tuple.from(StackUtils.serializeFuture(it.getValue().value)).pack(); + tr.set(pk, pv.length < 40000 ? pv : Arrays.copyOfRange(pv, 0, 40000)); + } + + return CompletableFuture.completedFuture(null); + }); + } + private static CompletableFuture logStack(final Instruction inst, final byte[] prefix, int i) { + //System.out.println("Logging stack at " + i); + while(inst.size() > 0) { + StackEntry e = inst.pop(); + byte[] pk = ByteArrayUtil.join(prefix, Tuple.from(i, e.idx).pack()); + byte[] pv = Tuple.from(StackUtils.serializeFuture(e.value)).pack(); + inst.tr.set(pk, pv.length < 40000 ? pv : Arrays.copyOfRange(pv, 0, 40000)); + i--; + if(i % 100 == 0) { + final int saved = i; + return inst.tr.commit().thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(Void o) { + inst.setTransaction(inst.tr.reset()); + return logStack(inst, prefix, saved); + } + }); + } + } + return inst.tr.commit().thenApplyAsync(new Function() { + @Override + public Void apply(Void a) { + inst.setTransaction(inst.tr.reset()); + return null; + } + }); + } + + private static CompletableFuture pushRange(Instruction inst, CompletableFuture> range) { + return pushRange(inst, range, null); + } + + private static CompletableFuture pushRange(Instruction inst, CompletableFuture> range, byte[] prefixFilter) { + //System.out.println("Waiting on range data to push..."); + return range.thenApplyAsync(new ListPusher(inst, prefixFilter)); + } + + /** + * Pushes the result of a range query onto the stack as a {@code Tuple} + */ + private static class ListPusher implements Function, Void> { + final Instruction inst; + final byte[] prefixFilter; + + ListPusher(Instruction inst, byte[] prefixFilter) { + this.inst = inst; + this.prefixFilter = prefixFilter; + } + @Override + public Void apply(List list) { + List o = new LinkedList(); + for(KeyValue kv : list) { + if(prefixFilter == null || ByteArrayUtil.startsWith(kv.getKey(), prefixFilter)) { + o.add(kv.getKey()); + o.add(kv.getValue()); + } + } + //System.out.println("Added " + o.size() / 2 + " pairs to stack/tuple"); + inst.push(Tuple.fromItems(o).pack()); + return null; + } + } + + static class AsynchronousContext extends Context { + List operations = null; + int currentOp = 0; + + AsyncDirectoryExtension directoryExtension = new AsyncDirectoryExtension(); + + AsynchronousContext(Database db, byte[] prefix) { + super(db, prefix); + } + + @Override + Context createContext(byte[] prefix) { + return new AsynchronousContext(this.db, prefix); + } + + CompletableFuture processOp(byte[] operation) { + Tuple tokens = Tuple.fromBytes(operation); + final Instruction inst = new Instruction(this, tokens); + + /*if(!inst.op.equals("PUSH") && !inst.op.equals("SWAP")) { + System.out.println(inst.context.preStr + "\t- " + Thread.currentThread().getName() + + "\t- OP (" + inst.context.instructionIndex + "):" + inst.op); + }*/ + + if(inst.op.startsWith(DIRECTORY_PREFIX)) + return directoryExtension.processInstruction(inst); + else { + return AsyncUtil.composeExceptionally(processInstruction(inst), + new Function>() { + @Override + public CompletableFuture apply(Throwable e) { + FDBException ex = StackUtils.getRootFDBException(e); + if(ex != null) { + StackUtils.pushError(inst, ex); + return CompletableFuture.completedFuture(null); + } + else { + CompletableFuture f = new CompletableFuture(); + f.completeExceptionally(e); + return f; + } + } + }); + } + } + + @Override void executeOperations() { + executeRemainingOperations().join(); + } + + CompletableFuture executeRemainingOperations() { + Transaction t = db.createTransaction(); + + final Function> processNext = new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + instructionIndex++; + return executeRemainingOperations(); + } + }; + + if(operations == null || ++currentOp == operations.size()) { + return t.getRange(nextKey, endKey, 1000).asList() + .thenComposeAsync(new Function, CompletableFuture>() { + @Override + public CompletableFuture apply(List next) { + if(next.size() < 1) { + //System.out.println("No key found after: " + ByteArrayUtil.printable(nextKey.getKey())); + return CompletableFuture.completedFuture(null); + } + + operations = next; + currentOp = 0; + nextKey = KeySelector.firstGreaterThan(next.get(next.size()-1).getKey()); + + return processOp(next.get(0).getValue()).thenComposeAsync(processNext); + } + }); + } + + return processOp(operations.get(currentOp).getValue()).thenComposeAsync(processNext); + } + } + + static CompletableFuture popAndWait(Stack stack) { + StackEntry entry = stack.pop(); + Object item = entry.value; + if(!(item instanceof CompletableFuture)) { + return CompletableFuture.completedFuture(entry); + } + final int idx = entry.idx; + + @SuppressWarnings("unchecked") + final CompletableFuture future = (CompletableFuture)item; + CompletableFuture flattened = flatten(future); + + return flattened.thenApplyAsync(new Function() { + @Override + public StackEntry apply(Object o) { + return new StackEntry(idx, o); + } + }); + } + + private static CompletableFuture flatten(final CompletableFuture future) { + CompletableFuture f = future.thenApplyAsync(new Function() { + @Override + public Object apply(Object o) { + if(o == null) + return "RESULT_NOT_PRESENT".getBytes(); + return o; + } + }); + + return AsyncUtil.composeExceptionally(f, new Function>() { + @Override + public CompletableFuture apply(Throwable t) { + FDBException e = StackUtils.getRootFDBException(t); + if(e != null) { + return CompletableFuture.completedFuture(StackUtils.getErrorBytes(e)); + } + + CompletableFuture error = new CompletableFuture(); + error.completeExceptionally(t); + return error; + } + }); + } + + + /** + * Run a stack-machine based test. + */ + public static void main(String[] args) { + if(args.length < 1) + throw new IllegalArgumentException("StackTester needs parameters "); + + //System.out.println("Prefix: " + args[0]); + + byte[] prefix = args[0].getBytes(); + + FDB fdb = FDB.selectAPIVersion(Integer.parseInt(args[1])); + //ExecutorService executor = Executors.newFixedThreadPool(2); + Cluster cl = fdb.createCluster(args.length > 2 ? args[2] : null); + + Database db = cl.openDatabase(); + + Context c = new AsynchronousContext(db, prefix); + //System.out.println("Starting test..."); + c.run(); + //System.out.println("Done with test."); + + /*byte[] key = Tuple.from("test_results".getBytes(), 5).pack(); + byte[] bs = db.createTransaction().get(key).get(); + System.out.println("output of " + ByteArrayUtil.printable(key) + " as: " + ByteArrayUtil.printable(bs));*/ + + /*fdb.stopNetwork(); + executor.shutdown();*/ + } + +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/BlockingBenchmark.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/BlockingBenchmark.java new file mode 100644 index 0000000000..4a6fd88130 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/BlockingBenchmark.java @@ -0,0 +1,148 @@ +/* + * BlockingBenchmark.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; + +public class BlockingBenchmark { + private static final int REPS = 100000; + private static final int PARALLEL = 100; + + public static void main(String[] args) throws InterruptedException { + FDB fdb = FDB.selectAPIVersion(500); + + // The cluster file DOES NOT need to be valid, although it must exist. + // This is because the database is never really contacted in this test. + Database database = fdb.open("T:\\circus\\tags\\RebarCluster-bbc\\cluster_id.txt"); + + byte[] key = {0x1, 0x1, 0x1, 0x1, 0x1}; + byte[] val = {0x2, 0x2, 0x2, 0x2, 0x2}; + + Transaction tr = database.createTransaction(); + tr.setReadVersion(100000); + final Function identity = new Function() { + @Override + public Long apply(Long o) { + return o; + } + }; + + + System.out.println("readVersion().join():"); + runTests(tr, new Function, Void>() { + @Override + public Void apply(CompletableFuture o) { + try { + o.join(); + } catch(Exception e) { } + + return null; + } + }); + + System.out.println("readVersion().get():"); + runTests(tr, new Function, Void>() { + @Override + public Void apply(CompletableFuture o) { + try { + o.get(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch(Exception e) { } + + return null; + } + }); + + System.out.println("readVersion().thenApplyAsync(identity).get():"); + runTests(tr, new Function, Void>() { + @Override + public Void apply(CompletableFuture o) { + try { + o.thenApplyAsync(identity).get(); + } catch(Exception e) { } + + return null; + } + }); + + System.out.println("readVersion().thenApplyAsync^10(identity).get():"); + runTests(tr, new Function, Void>() { + @Override + public Void apply(CompletableFuture o) { + for(int i=0; i<10; i++) + o = o.thenApplyAsync(identity); + try { + o.get(); + } catch(Exception e) { } + + return null; + } + }); + + System.out.println("readVersion().get^100():"); + runTests(tr, new Function, Void>() { + @Override + public Void apply(CompletableFuture o) { + for(int i=0; i<100; i++) { + try { + o.get(); + } catch(Exception e) { } + } + return null; + } + }); + + } + + private static void runTests(Transaction tr, Function, Void> blockMethod) { + for(int r=0; r<4; r++) { + long start = System.currentTimeMillis(); + for(int i = 0; i < REPS; i++) { + blockMethod.apply( tr.getReadVersion() ); + } + + long taken = System.currentTimeMillis() - start; + System.out.println(" " + REPS + " done in " + taken + "ms -> " + ((taken * 1000.0) / REPS) + " us latency"); + + ArrayList> futures = new ArrayList>(PARALLEL); + for(int j=0; j " + (REPS / (taken)) + " KHz"); + } + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/ConcurrentGetSetGet.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/ConcurrentGetSetGet.java new file mode 100644 index 0000000000..fc86ae6cc9 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/ConcurrentGetSetGet.java @@ -0,0 +1,136 @@ +/* + * ConcurrentGetSetGet.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.nio.charset.Charset; +import java.security.SecureRandom; +import java.util.Random; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; + +public class ConcurrentGetSetGet { + public static final Charset UTF8 = Charset.forName("UTF-8"); + + final Semaphore semaphore = new Semaphore(CONCURRENCY); + final AtomicInteger errorCount = new AtomicInteger(); + final AtomicInteger attemptCount = new AtomicInteger(); + final AtomicInteger getCompleteCount = new AtomicInteger(); + + // Total trials + public static final int COUNT = 1000; + + // How many to have outstanding at once + public static final int CONCURRENCY = 100; + + private static byte[] $(String s) { + return s.getBytes(UTF8); + } + + public static void main(String[] args) { + Database database = FDB.selectAPIVersion(500).open(); + new ConcurrentGetSetGet().apply(database); + } + + public void apply(Database d) { + new Thread(new Runnable() { + @Override + public void run() { + int loops = 0; + try { + Thread.sleep(5000); + System.out.println("Loop " + loops++ + ":"); + System.out.println(" attempts: " + attemptCount.get()); + System.out.println(" gets complete: " + getCompleteCount.get()); + System.out.println(" errors: " + errorCount.get()); + System.out.println(" sem: " + semaphore); + System.out.println(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + }).start(); + final Random random = new SecureRandom(); + try { + long start = System.currentTimeMillis(); + long current = start; + for (int i = 0; i < COUNT; i++) { + semaphore.acquire(); + long wait = System.currentTimeMillis() - current; + if (wait > 100) { + System.out.println("Waited " + wait + "ms"); + } + current = System.currentTimeMillis(); + d.runAsync(new Function>() { + @Override + public CompletableFuture apply(final Transaction r) { + attemptCount.addAndGet(1); + final String key = "test:" + random.nextInt(); + return r.get($(key)).thenComposeAsync(new Function>() { + @Override + public CompletableFuture apply(byte[] o) { + r.set($(key), $("value")); + return r.get($(key)).thenApplyAsync(new Function() { + @Override + public Void apply(byte[] o) { + getCompleteCount.addAndGet(1); + semaphore.release(); + return null; + } + }); + } + }).exceptionally(new Function() { + @Override + public Void apply(Throwable o) { + errorCount.addAndGet(1); + System.err.println("Fail (" + o.getMessage() + ")"); + semaphore.release(); + return null; + } + }); + } + }); + } + semaphore.acquire(CONCURRENCY); + long diff = System.currentTimeMillis() - start; + System.out.println("time taken (ms): " + diff); + System.out.println("tr/sec:" + COUNT * 1000l / diff); + System.out.println("attempts: " + attemptCount.get()); + System.out.println("gets complete: " + getCompleteCount.get()); + System.out.println("errors: " + errorCount.get()); + System.out.println(); + // Can be enabled in Database.java + //System.out.println("db success: " + d.commitSuccessCount.get()); + //System.out.println("db errors: " + d.commitErrorCount.get()); + System.exit(0); + } catch (Throwable throwable) { + throwable.printStackTrace(); + System.exit(1); + } + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Context.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Context.java new file mode 100644 index 0000000000..43342fd623 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Context.java @@ -0,0 +1,184 @@ +/* + * Context.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import java.util.concurrent.CompletableFuture; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.KeySelector; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.StreamingMode; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +abstract class Context implements Runnable { + final Stack stack = new Stack(); + final Database db; + final String preStr; + int instructionIndex = 0; + String trName; + KeySelector nextKey, endKey; + Long lastVersion = null; + List children = new LinkedList(); + + static Map transactionMap = new HashMap(); + + Context(Database db, byte[] prefix) { + this.db = db; + Range r = Tuple.from(prefix).range(); + this.nextKey = KeySelector.firstGreaterOrEqual(r.begin); + this.endKey = KeySelector.firstGreaterOrEqual(r.end); + + this.trName = ByteArrayUtil.printable(prefix); + this.preStr = ByteArrayUtil.printable(prefix); + + newTransaction(); + } + + @Override + public void run() { + try { + executeOperations(); + } catch(Throwable t) { + // EAT + t.printStackTrace(); + } + while(children.size() > 0) { + //System.out.println("Shutting down...waiting on " + children.size() + " threads"); + final Thread t = children.get(0); + while(t.isAlive()) { + try { + t.join(); + } catch (InterruptedException e) { + // EAT + } + } + children.remove(0); + } + } + + public Transaction getCurrentTransaction() { + synchronized(Context.transactionMap) { + return Context.transactionMap.get(this.trName); + } + } + + public void updateCurrentTransaction(Transaction tr) { + synchronized(Context.transactionMap) { + Context.transactionMap.put(this.trName, tr); + } + } + + public void newTransaction() { + synchronized(Context.transactionMap) { + Context.transactionMap.put(this.trName, db.createTransaction()); + } + } + + public void switchTransaction(byte[] trName) { + synchronized(Context.transactionMap) { + this.trName = ByteArrayUtil.printable(trName); + if(!Context.transactionMap.containsKey(this.trName)) { + newTransaction(); + } + } + } + + abstract void executeOperations() throws Throwable; + abstract Context createContext(byte[] prefix); + + void addContext(byte[] prefix) { + Thread t = new Thread(createContext(prefix)); + t.start(); + children.add(t); + } + + StreamingMode streamingModeFromCode(int code) { + for(StreamingMode x : StreamingMode.values()) { + if(x.code() == code) { + return x; + } + } + throw new IllegalArgumentException("Invalid code: " + code); + } + + void popParams(int num, final List params, final CompletableFuture done) { + while(num-- > 0) { + Object item = stack.pop().value; + if(item instanceof CompletableFuture) { + @SuppressWarnings("unchecked") + final CompletableFuture future = (CompletableFuture)item; + final int nextNum = num; + future.whenCompleteAsync(new BiConsumer() { + @Override + public void accept(Object o, Throwable t) { + if(t != null) { + Throwable root = StackUtils.getRootFDBException(t); + if(root instanceof FDBException) { + params.add(StackUtils.getErrorBytes((FDBException)root)); + popParams(nextNum, params, done); + } + else { + done.completeExceptionally(t); + } + } + else { + if(o == null) + params.add("RESULT_NOT_PRESENT".getBytes()); + else + params.add(o); + + popParams(nextNum, params, done); + } + } + }); + + return; + } + else + params.add(item); + } + + done.complete(null); + } + + CompletableFuture> popParams(int num) { + final List params = new LinkedList(); + CompletableFuture done = new CompletableFuture(); + popParams(num, params, done); + + return done.thenApplyAsync(new Function>() { + @Override + public List apply(Void n) { + return params; + } + }); + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/ContinuousSample.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/ContinuousSample.java new file mode 100644 index 0000000000..d1550b33ac --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/ContinuousSample.java @@ -0,0 +1,95 @@ +/* + * ContinuousSample.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +public class ContinuousSample > { + public ContinuousSample( int sampleSize ) { + this.sampleSize = sampleSize; + this.samples = new ArrayList(sampleSize); + this.populationSize = 0; + this.sorted = true; + } + + public ContinuousSample addSample(T sample) { + if(populationSize == 0) + _min = _max = sample; + populationSize++; + sorted = false; + + if( populationSize <= sampleSize ) { + samples.add( sample ); + } else if( random.nextDouble() < ( (double)sampleSize / populationSize ) ) { + samples.add(random.nextInt(sampleSize), sample); + } + + _max = sample.compareTo(_max) > 0 ? sample : _max; + _min = sample.compareTo(_min) < 0 ? sample : _min; + return this; + } + + public double mean() { + if (samples.size() == 0) return 0; + double sum = 0; + for( int c = 0; c < samples.size(); c++ ) { + sum += samples.get(c).doubleValue(); + } + return sum / samples.size(); + } + + public T median() { + return percentile( 0.5 ); + } + + public T percentile( double percentile ) { + if( samples.size() == 0 || percentile < 0.0 || percentile > 1.0 ) + return null; + sort(); + int idx = (int)Math.floor( ( samples.size() - 1 ) * percentile ); + return samples.get(idx); + } + + public T min() { return _min; } + public T max() { return _max; } + + @Override + public String toString() { + return String.format("Mean: %.2f, Median: %.2f, 90%%: %.2f, 98%%: %.2f", + mean(), median(), percentile(0.90), percentile(0.98)); + } + + private Random random = new Random(); + private int sampleSize; + private long populationSize; + private boolean sorted; + private List samples; + private T _min, _max; + + private void sort() { + if( !sorted && samples.size() > 1 ) + Collections.sort(samples); + sorted = true; + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryExtension.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryExtension.java new file mode 100644 index 0000000000..12339e6c81 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryExtension.java @@ -0,0 +1,230 @@ +/* + * DirectoryExtension.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import java.util.concurrent.CompletableFuture; + +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.directory.Directory; +import com.apple.cie.foundationdb.directory.DirectoryLayer; +import com.apple.cie.foundationdb.directory.DirectorySubspace; +import com.apple.cie.foundationdb.subspace.Subspace; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +class DirectoryExtension { + List dirList = new ArrayList(); + int dirIndex = 0; + int errorIndex = 0; + + public DirectoryExtension() { + dirList.add(DirectoryLayer.getDefault()); + } + + Directory directory() { + return (Directory)dirList.get(dirIndex); + } + + Subspace subspace() { + return (Subspace)dirList.get(dirIndex); + } + + void processInstruction(final Instruction inst) { + try { + //System.out.println("Processing operation " + inst.op + " (" + inst.context.instructionIndex + ")"); + DirectoryOperation op = DirectoryOperation.valueOf(inst.op); + if(op == DirectoryOperation.DIRECTORY_CREATE_SUBSPACE) { + Tuple prefix = DirectoryUtil.popTuple(inst).get(); + byte[] rawPrefix = (byte[])inst.popParam().get(); + dirList.add(new Subspace(prefix, rawPrefix)); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE_LAYER) { + List params = inst.popParams(3).get(); + Subspace nodeSubspace = (Subspace)dirList.get(StackUtils.getInt(params.get(0))); + Subspace contentSubspace = (Subspace)dirList.get(StackUtils.getInt(params.get(1))); + boolean allowManualPrefixes = StackUtils.getInt(params.get(2)) == 1; + + if(nodeSubspace == null || contentSubspace == null) + dirList.add(null); + else + dirList.add(new DirectoryLayer(nodeSubspace, contentSubspace, allowManualPrefixes)); + } + else if(op == DirectoryOperation.DIRECTORY_CHANGE) { + dirIndex = StackUtils.getInt(inst.popParam().get()); + if(dirList.get(dirIndex) == null) + dirIndex = errorIndex; + } + else if(op == DirectoryOperation.DIRECTORY_SET_ERROR_INDEX) { + errorIndex = StackUtils.getInt(inst.popParam().get()); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN || op == DirectoryOperation.DIRECTORY_OPEN) { + List path = DirectoryUtil.popPath(inst).get(); + byte[] layer = (byte[])inst.popParam().get(); + + CompletableFuture dir; + if(layer == null) { + if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN) + dir = directory().createOrOpen(inst.tcx, path); + else + dir = directory().open(inst.readTcx, path); + } + else { + if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN) + dir = directory().createOrOpen(inst.tcx, path, layer); + else + dir = directory().open(inst.readTcx, path, layer); + } + + dirList.add(dir.get()); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE) { + List path = DirectoryUtil.popPath(inst).get(); + List params = inst.popParams(2).get(); + + byte[] layer = (byte[])params.get(0); + byte[] prefix = (byte[])params.get(1); + + CompletableFuture dir; + if(layer == null && prefix == null) + dir = directory().create(inst.tcx, path); + else if(prefix == null) + dir = directory().create(inst.tcx, path, layer); + else { + if(layer == null) + layer = new byte[0]; + + dir = directory().create(inst.tcx, path, layer, prefix); + } + + dirList.add(dir.get()); + } + else if(op == DirectoryOperation.DIRECTORY_MOVE) { + List> paths = DirectoryUtil.popPaths(inst, 2).get(); + DirectorySubspace dir = directory().move(inst.tcx, paths.get(0), paths.get(1)).get(); + dirList.add(dir); + } + else if(op == DirectoryOperation.DIRECTORY_MOVE_TO) { + List newAbsolutePath = DirectoryUtil.popPath(inst).get(); + DirectorySubspace dir = directory().moveTo(inst.tcx, newAbsolutePath).get(); + dirList.add(dir); + } + else if(op == DirectoryOperation.DIRECTORY_REMOVE) { + int count = StackUtils.getInt(inst.popParam().get()); + List> path = DirectoryUtil.popPaths(inst, count).get(); + if(path.size() == 0) + directory().remove(inst.tcx).get(); + else + directory().remove(inst.tcx, path.get(0)).get(); + } + else if(op == DirectoryOperation.DIRECTORY_REMOVE_IF_EXISTS) { + int count = StackUtils.getInt(inst.popParam().get()); + List> path = DirectoryUtil.popPaths(inst, count).get(); + if(path.size() == 0) + directory().removeIfExists(inst.tcx).get(); + else + directory().removeIfExists(inst.tcx, path.get(0)).get(); + } + else if(op == DirectoryOperation.DIRECTORY_LIST) { + int count = StackUtils.getInt(inst.popParam().get()); + List> path = DirectoryUtil.popPaths(inst, count).get(); + List children; + if(path.size() == 0) + children = directory().list(inst.readTcx).get(); + else + children = directory().list(inst.readTcx, path.get(0)).get(); + + inst.push(Tuple.fromItems(children).pack()); + } + else if(op == DirectoryOperation.DIRECTORY_EXISTS) { + int count = StackUtils.getInt(inst.popParam().get()); + List> path = DirectoryUtil.popPaths(inst, count).get(); + boolean exists; + if(path.size() == 0) + exists = directory().exists(inst.readTcx).get(); + else + exists = directory().exists(inst.readTcx, path.get(0)).get(); + + inst.push(exists ? 1 : 0); + } + else if(op == DirectoryOperation.DIRECTORY_PACK_KEY) { + Tuple keyTuple = DirectoryUtil.popTuple(inst).get(); + inst.push(subspace().pack(keyTuple)); + } + else if(op == DirectoryOperation.DIRECTORY_UNPACK_KEY) { + byte[] key = (byte[])inst.popParam().get(); + Tuple tup = subspace().unpack(key); + for(Object o : tup) + inst.push(o); + } + else if(op == DirectoryOperation.DIRECTORY_RANGE) { + Tuple tup = DirectoryUtil.popTuple(inst).get(); + Range range = subspace().range(tup); + inst.push(range.begin); + inst.push(range.end); + } + else if(op == DirectoryOperation.DIRECTORY_CONTAINS) { + byte[] key = (byte[])inst.popParam().get(); + inst.push(subspace().contains(key) ? 1 : 0); + } + else if(op == DirectoryOperation.DIRECTORY_OPEN_SUBSPACE) { + Tuple prefix = DirectoryUtil.popTuple(inst).get(); + dirList.add(subspace().subspace(prefix)); + } + else if(op == DirectoryOperation.DIRECTORY_LOG_SUBSPACE) { + final byte[] prefix = (byte[])inst.popParam().get(); + inst.tr.set(ByteArrayUtil.join(prefix, new Tuple().add(dirIndex).pack()), subspace().getKey()); + } + else if(op == DirectoryOperation.DIRECTORY_LOG_DIRECTORY) { + final byte[] prefix = (byte[])inst.popParam().get(); + boolean exists = directory().exists(inst.tr).get(); + List children = exists ? directory().list(inst.tr).get() : new ArrayList(); + Subspace logSubspace = new Subspace(new Tuple().add(dirIndex), prefix); + inst.tr.set(logSubspace.pack("path"), Tuple.fromItems(directory().getPath()).pack()); + inst.tr.set(logSubspace.pack("layer"), new Tuple().add(directory().getLayer()).pack()); + inst.tr.set(logSubspace.pack("exists"), new Tuple().add(exists ? 1 : 0).pack()); + inst.tr.set(logSubspace.pack("children"), Tuple.fromItems(children).pack()); + } + else if(op == DirectoryOperation.DIRECTORY_STRIP_PREFIX) { + byte[] str = (byte[])inst.popParam().get(); + byte[] rawPrefix = subspace().getKey(); + + if(str.length < rawPrefix.length) + throw new RuntimeException("String does not start with raw prefix"); + + for(int i = 0; i < rawPrefix.length; ++i) + if(str[i] != rawPrefix[i]) + throw new RuntimeException("String does not start with raw prefix"); + + inst.push(Arrays.copyOfRange(str, rawPrefix.length, str.length)); + } + else { + throw new RuntimeException("Unknown operation:" + inst.op); + } + } + catch(Throwable t) { + DirectoryUtil.pushError(inst, t, dirList); + } + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryOperation.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryOperation.java new file mode 100644 index 0000000000..70b28051a3 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryOperation.java @@ -0,0 +1,57 @@ +/* + * DirectoryOperation.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +enum DirectoryOperation { + DIRECTORY_CREATE_SUBSPACE(true), + DIRECTORY_CREATE_LAYER(true), + DIRECTORY_CHANGE, + DIRECTORY_SET_ERROR_INDEX, + DIRECTORY_CREATE_OR_OPEN(true), + DIRECTORY_CREATE(true), + DIRECTORY_OPEN(true), + DIRECTORY_MOVE(true), + DIRECTORY_MOVE_TO(true), + DIRECTORY_REMOVE, + DIRECTORY_REMOVE_IF_EXISTS, + DIRECTORY_LIST, + DIRECTORY_EXISTS, + DIRECTORY_CHECK_LAYER, + DIRECTORY_PACK_KEY, + DIRECTORY_UNPACK_KEY, + DIRECTORY_RANGE, + DIRECTORY_CONTAINS, + DIRECTORY_OPEN_SUBSPACE(true), + DIRECTORY_LOG_SUBSPACE, + DIRECTORY_LOG_DIRECTORY, + DIRECTORY_STRIP_PREFIX; + + boolean createsDirectory; + + DirectoryOperation() { + this(false); + } + + DirectoryOperation(boolean createsDirectory) { + this.createsDirectory = createsDirectory; + } +} + diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryTest.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryTest.java new file mode 100644 index 0000000000..d22f939a53 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryTest.java @@ -0,0 +1,89 @@ +/* + * DirectoryTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import java.util.function.Function; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.directory.DirectoryLayer; +import com.apple.cie.foundationdb.directory.DirectorySubspace; + +public class DirectoryTest { + private static final String CLUSTER_FILE = "/home/ajb/fdb.cluster"; + + public static void main(String[] args) throws Exception { + try { + Cluster c = FDB.selectAPIVersion(500).createCluster(CLUSTER_FILE); + Database db = c.openDatabase(); + runTests(db); + } catch(Throwable t) { + t.printStackTrace(); + } + } + + private static void runTests(TransactionContext db) throws Exception { + System.out.println("Running tests..."); + final DirectoryLayer dir = new DirectoryLayer(); + + try { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + List path = new ArrayList<>(); + path.add("foo"); + DirectorySubspace foo = dir.create(tr, path).join();//, "partition".getBytes("UTF-8")).get(); + System.out.println(foo.getPath()); + path.add("bar"); + DirectorySubspace bar = dir.create(tr, path).join();//, "partition".getBytes("UTF-8")).get(); + System.out.println(foo.getPath()); + path.add("baz"); + DirectorySubspace baz = dir.create(tr, path).join(); + System.out.println(foo.getPath()); + System.out.println("Created foo: " + foo.exists(tr).join()); + System.out.println("Created bar: " + bar.exists(tr).join()); + System.out.println("Created baz: " + baz.exists(tr).join()); + + DirectorySubspace bat = baz.moveTo(tr, Arrays.asList("foo", "bar", "bat")).join(); + + System.out.println("Moved baz to bat: " + bat.exists(tr).join()); + + foo.removeIfExists(tr).join(); + + System.out.println("Removed foo: " + foo.exists(tr).join()); + + return null; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + + System.exit(0); + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryUtil.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryUtil.java new file mode 100644 index 0000000000..e5385a75ae --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/DirectoryUtil.java @@ -0,0 +1,125 @@ +/* + * DirectoryUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +class DirectoryUtil { + private static class TuplePopper { + private Instruction inst; + private int num; + private List tuples = new ArrayList(); + + TuplePopper(Instruction inst, int num) { + this.inst = inst; + this.num = num; + } + + CompletableFuture> pop() { + return AsyncUtil.whileTrue(new Function>() { + @Override + public CompletableFuture apply(Void ignore) { + if(num-- == 0) { + return CompletableFuture.completedFuture(false); + } + return inst.popParam() + .thenComposeAsync(new Function>>() { + @Override + public CompletableFuture> apply(Object count) { + return inst.popParams(StackUtils.getInt(count)); + } + }) + .thenApplyAsync(new Function, Boolean>() { + @Override + public Boolean apply(List elements) { + tuples.add(Tuple.fromItems(elements)); + return num > 0; + } + }); + } + }) + .thenApplyAsync(new Function>() { + @Override + public List apply(Void ignore) { + return tuples; + } + }); + } + } + + static CompletableFuture> popTuples(Instruction inst, int num) { + return new TuplePopper(inst, num).pop(); + } + + static CompletableFuture popTuple(Instruction inst) { + return popTuples(inst, 1) + .thenApplyAsync(new Function, Tuple>() { + @Override + public Tuple apply(List tuples) { + return tuples.get(0); + } + }); + } + + static CompletableFuture>> popPaths(Instruction inst, int num) { + return popTuples(inst, num) + .thenApplyAsync(new Function, List>>() { + @Override + public List> apply(List tuples) { + List> paths = new ArrayList>(); + for(Tuple t : tuples) { + List path = new ArrayList(); + for(int i = 0; i < t.size(); ++i) + path.add(t.getString(i)); + + paths.add(path); + } + + return paths; + } + }); + } + + static CompletableFuture> popPath(Instruction inst) { + return popPaths(inst, 1) + .thenApplyAsync(new Function>, List>() { + @Override + public List apply(List> paths) { + return paths.get(0); + } + }); + } + + static void pushError(Instruction inst, Throwable t, List dirList) { + //System.err.println(t.getMessage()); + //t.printStackTrace(); + inst.push("DIRECTORY_ERROR".getBytes()); + DirectoryOperation op = DirectoryOperation.valueOf(inst.op); + if(op.createsDirectory) + dirList.add(null); + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Example.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Example.java new file mode 100644 index 0000000000..73e0ea9c6f --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Example.java @@ -0,0 +1,55 @@ +/* + * Example.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.concurrent.ExecutionException; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.tuple.Tuple; + +public class Example { + public static void main(String[] args) throws ExecutionException, InterruptedException { + FDB fdb = FDB.selectAPIVersion(500); + Database db = fdb.open(); + + // Run an operation on the database + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + tr.set(Tuple.from("hello").pack(), Tuple.from("world").pack()); + return null; + } + }); + + // Get the value of 'hello' from the database + String hello = db.run(new Function() { + @Override + public String apply(Transaction tr) { + byte[] result = tr.get(Tuple.from("hello").pack()).join(); + return Tuple.fromBytes(result).getString(0); + } + }); + System.out.println("Hello " + hello); + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Instruction.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Instruction.java new file mode 100644 index 0000000000..82adf957d2 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Instruction.java @@ -0,0 +1,129 @@ +/* + * Instruction.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +import com.apple.cie.foundationdb.ReadTransaction; +import com.apple.cie.foundationdb.ReadTransactionContext; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.tuple.Tuple; + +import java.util.List; + +class Instruction extends Stack { + private final static String SUFFIX_SNAPSHOT = "_SNAPSHOT"; + private final static String SUFFIX_DATABASE = "_DATABASE"; + + String op; + Tuple tokens; + Context context; + boolean isDatabase; + boolean isSnapshot; + Transaction tr; + ReadTransaction readTr; + TransactionContext tcx; + ReadTransactionContext readTcx; + + public Instruction(Context context, Tuple tokens) { + this.context = context; + this.tokens = tokens; + + op = tokens.getString(0); + isDatabase = op.endsWith(SUFFIX_DATABASE); + isSnapshot = op.endsWith(SUFFIX_SNAPSHOT); + + if(isDatabase) { + this.tr = context.db.createTransaction(); + readTr = this.tr; + op = op.substring(0, op.length() - SUFFIX_DATABASE.length()); + } + else if(isSnapshot) { + this.tr = context.getCurrentTransaction(); + readTr = this.tr.snapshot(); + op = op.substring(0, op.length() - SUFFIX_SNAPSHOT.length()); + } + else { + this.tr = context.getCurrentTransaction(); + readTr = this.tr; + } + + tcx = isDatabase ? context.db : this.tr; + readTcx = isDatabase ? context.db : this.readTr; + } + + void setTransaction(Transaction tr) { + this.tr = tr; + if(isSnapshot) { + readTr = this.tr.snapshot(); + } + else { + readTr = tr; + } + + if(!isDatabase) { + context.updateCurrentTransaction(tr); + } + } + + void push(Object o) { + context.stack.push(context.instructionIndex, o); + } + + void push(int idx, Object o) { + context.stack.push(idx, o); + } + + void push(StackEntry e) { + context.stack.push(e); + } + + StackEntry pop() { + return context.stack.pop(); + } + + void swap(int index) { + context.stack.swap(index); + } + + int size() { + return context.stack.size(); + } + + void clear() { + context.stack.clear(); + } + + CompletableFuture> popParams(int num) { + return context.popParams(num); + } + + CompletableFuture popParam() { + return popParams(1) + .thenApplyAsync(new Function, Object>() { + public Object apply(List params) { + return params.get(0); + } + }); + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/IterableTest.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/IterableTest.java new file mode 100644 index 0000000000..4c00d04e9a --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/IterableTest.java @@ -0,0 +1,74 @@ +/* + * IterableTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; + +public class IterableTest { + private static final String CLUSTER_FILE = "C:\\Users\\Ben\\workspace\\fdb\\fdb.cluster"; + + public static void main(String[] args) throws InterruptedException { + final int reps = 1000; + try { + Cluster cluster = FDB.selectAPIVersion(500).createCluster(CLUSTER_FILE); + Database db = cluster.openDatabase(); + runTests(reps, db); + } catch(Throwable t) { + t.printStackTrace(); + } + } + + private static void runTests(final int reps, TransactionContext db) { + System.out.println("Running tests..."); + long start = System.currentTimeMillis(); + final AtomicInteger lastcount = new AtomicInteger(0); + try { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + for(KeyValue e : tr.getRange("vcount".getBytes(), "zz".getBytes())) { + System.out.println("K: " + new String(e.getKey()) + ", V: " + new String(e.getValue())); + } + return null; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + long end = System.currentTimeMillis(); + + double seconds = (end - start) / 1000.0; + System.out.println(" Transactions: " + reps); + System.out.println(" Total Time: " + seconds); + System.out.println(" Gets+Sets / sec: " + reps / seconds); + System.out.println(" Count: " + lastcount.get()); + + System.exit(0); + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/LocalityTests.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/LocalityTests.java new file mode 100644 index 0000000000..c5e6a15cc6 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/LocalityTests.java @@ -0,0 +1,58 @@ +/* + * LocalityTests.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.List; +import java.util.concurrent.CompletableFuture; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.LocalityUtil; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +public class LocalityTests { + + public static void main(String[] args) { + FDB fdb = FDB.selectAPIVersion(500); + Database database = fdb.open(args[0]); + { + Transaction tr = database.createTransaction(); + String[] keyAddresses = LocalityUtil.getAddressesForKey(tr, "a".getBytes()).join(); + for(String s : keyAddresses) { + System.out.println(" @ " + s); + } + } + + long start = System.currentTimeMillis(); + AsyncIterable keys = LocalityUtil.getBoundaryKeys(database, new byte[0], new byte[] { (byte)255 } ); + CompletableFuture> collection = AsyncUtil.collect(keys); + List list = collection.join(); + System.out.println("Took " + (System.currentTimeMillis() - start) + "ms to get " + + list.size() + " items"); + int i = 0; + for(byte[] key : collection.join()) { + System.out.println(i++ + ": " + ByteArrayUtil.printable(key)); + } + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/OSTest.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/OSTest.java new file mode 100644 index 0000000000..36577119dc --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/OSTest.java @@ -0,0 +1,38 @@ +/* + * OSTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.io.InputStream; + +public class OSTest { + + /** + * @param args + */ + public static void main(String[] args) { + System.out.println("OS name: " + System.getProperty("os.name")); + System.out.println("OS arch: " + System.getProperty("os.arch")); + + InputStream stream = OSTest.class.getResourceAsStream("/lib/linux/amd64/libfdb_java.so"); + System.out.println("Stream: " + stream); + } + +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/ParallelRandomScan.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/ParallelRandomScan.java new file mode 100644 index 0000000000..fda877ac27 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/ParallelRandomScan.java @@ -0,0 +1,136 @@ +/* + * ParallelRandomScan.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.nio.ByteBuffer; +import java.util.Random; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.StreamingMode; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncIterator; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +public class ParallelRandomScan { + private static final int ROWS = 1000000; + private static final int DURATION_MS = 2000; + private static final int PARALLELISM_MIN = 10; + private static final int PARALLELISM_MAX = 100; + private static final int PARALLELISM_STEP = 5; + + public static void main(String[] args) throws InterruptedException { + FDB api = FDB.selectAPIVersion(500); + Database database = api.open(args[0]); + + for(int i = PARALLELISM_MIN; i <= PARALLELISM_MAX; i += PARALLELISM_STEP) { + runTest(database, i, ROWS, DURATION_MS); + Thread.sleep(1000); + } + } + + private static void runTest(Database database, + int parallelism, int rows, int duration) throws InterruptedException { + final Random r = new Random(); + final AtomicInteger readsCompleted = new AtomicInteger(0); + final AtomicInteger errors = new AtomicInteger(0); + final Transaction tr = database.createTransaction(); + final Semaphore coordinator = new Semaphore(parallelism); + final ContinuousSample latencies = new ContinuousSample<>(1000); + + tr.options().setReadYourWritesDisable(); + + // Clearing the whole database before starting means all reads are local + /*ByteBuffer buf = ByteBuffer.allocate(4); + buf.putInt(0, Integer.MAX_VALUE); + tr.clear(new byte[0], buf.array());*/ + + // We use this for the key generation + ByteBuffer buf = ByteBuffer.allocate(4); + + // Eat the cost of the read version up-front + tr.getReadVersion().join(); + + final long start = System.currentTimeMillis(); + while(true) { + coordinator.acquire(); + if(System.currentTimeMillis() - start > duration) { + coordinator.release(); + break; + } + + int row = r.nextInt(rows - 1); + buf.putInt(0, row); + AsyncIterable range = tr.getRange( + buf.array(), ByteArrayUtil.strinc(buf.array()), 1, false, StreamingMode.SMALL); + + final long launch = System.nanoTime(); + + final AsyncIterator it = range.iterator(); + final CompletableFuture f = it.onHasNext().thenApplyAsync( + new Function() { + @Override + public KeyValue apply(Boolean o) { + if(!o) { + return null; + } + return it.next(); + } + } + ); + f.whenCompleteAsync(new BiConsumer() { + @Override + public void accept(KeyValue kv, Throwable t) { + if(kv != null) { + readsCompleted.incrementAndGet(); + long timeTaken = System.nanoTime() - launch; + synchronized(latencies) { + latencies.addSample(timeTaken); + } + } + else if(t != null) { + errors.incrementAndGet(); + } + + coordinator.release(); + } + }); + } + + // Block for ALL tasks to end! + coordinator.acquire(parallelism); + long end = System.currentTimeMillis(); + + double rowsPerSecond = readsCompleted.get() / ((end - start) / 1000.0); + System.out.println(parallelism + " ->\t" + rowsPerSecond); + System.out.println(String.format(" Reads: %d, errors: %d, time: %dms", + readsCompleted.get(), errors.get(), (int)(end - start))); + System.out.println(String.format(" Mean: %.2f, Median: %d, 98%%: %d", + latencies.mean(), latencies.median(), latencies.percentile(0.98))); + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/RangeTest.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/RangeTest.java new file mode 100644 index 0000000000..bac74f6266 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/RangeTest.java @@ -0,0 +1,190 @@ +/* + * RangeTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.List; +import java.util.concurrent.CompletionException; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.KeySelector; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; + +public class RangeTest { + private static final int API_VERSION = 500; + + public static void main(String[] args) { + System.out.println("About to use version " + API_VERSION); + FDB fdb = FDB.selectAPIVersion(API_VERSION); + + /* + final String CLUSTER_FILE = "T:\\Ben\\cluster"; + String clusterFile = CLUSTER_FILE; + if(args.length > 0) { + clusterFile = args[0]; + } + + System.out.println("Using cluster file: " + clusterFile); + Cluster cluster = fdb.createCluster(clusterFile).get(); + Database db = cluster.openDatabase().get(); + */ + + Database db = fdb.open(); + + try { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + long version = tr.getReadVersion().join(); + System.out.println("DB version: " + version); + tr.get("apple1".getBytes()).join(); + tr.set("apple1".getBytes(), "crunchy1".getBytes()); + tr.set("apple2".getBytes(), "crunchy2".getBytes()); + tr.set("apple3".getBytes(), "crunchy3".getBytes()); + tr.set("apple4".getBytes(), "crunchy4".getBytes()); + tr.set("apple5".getBytes(), "crunchy5".getBytes()); + tr.set("apple6".getBytes(), "crunchy6".getBytes()); + System.out.println("Attempting to commit apple/crunchy pairs..."); + + return null; + } + }); + } catch (Throwable e){ + e.printStackTrace(); + System.out.println("Non retryable exception caught..."); + } + + System.out.println("First transaction was successful"); + + checkRange(db.createTransaction()); + + Transaction tr = db.createTransaction(); + long version = tr.getReadVersion().join(); + System.out.println("DB version: " + version); + + byte[] bs = tr.get("apple3".getBytes()).join(); + System.out.println("Got apple3: " + new String(bs)); + + tr.cancel(); + try { + tr.get("apple3".getBytes()).join(); + throw new RuntimeException("The get() should have thrown an error!"); + } catch(CompletionException ex) { + FDBException e = (FDBException)ex.getCause(); + if(e.getCode() != 1025) { + System.err.println("Transaction was not cancelled correctly (" + e.getCode() + ")"); + throw e; + } + System.out.println("Transaction was cancelled correctly"); + } + + tr = tr.reset(); + version = tr.getReadVersion().join(); + System.out.println("DB version: " + version); + + tr.clear("apple3".getBytes(), "apple6".getBytes()); + try { + tr.commit().join(); + System.out.println("Clear range transaction was successful"); + } catch(FDBException e) { + System.err.println("Error in the clear of a single value"); + e.printStackTrace(); + return; + } + //db.dispose(); + //cluster.dispose(); + + tr = db.createTransaction(); + checkRange(tr); + + Range r1 = new Range("apple".getBytes(), "banana".getBytes()); + Range r2 = new Range("apple".getBytes(), "banana".getBytes()); + Range r3 = new Range("apple".getBytes(), "crepe".getBytes()); + Range r4 = new Range(null, "banana".getBytes()); + Range r5 = new Range(new byte[]{0x15, 0x01}, null); + + System.out.println("ranges: " + r1 + ", " + r2 + ", " + r3 + ", " + r4 + ", " + r5); + + if(r1.equals(null)) { + System.err.println("range " + r1 + " equals null"); + } else if(!r1.equals(r1)) { + System.err.println("range equality not reflexive"); + } else if(r1.hashCode() != r1.hashCode()) { + System.err.println("range hashcode not reflexive"); + } else if(!r1.equals(r2)) { + System.err.println("range " + r1 + " and " + r2 + " not equal"); + } else if(r1.hashCode() != r2.hashCode()) { + System.err.println("ranges " + r1 + " and " + r2 + " do not have same hash codes"); + } else if(r1.equals(r3)) { + System.err.println("ranges " + r1 + " and " + r3 + " are equal"); + } else if (r1.hashCode() == r3.hashCode()) { + System.err.println("range " + r1 + " and " + r3 + " have same hash code"); + } else if(r1.equals(r4)) { + System.err.println("ranges " + r1 + " and " + r4 + " are equal"); + } else if(r1.hashCode() == r4.hashCode()) { + System.err.println("range " + r1 + " and " + r4 + " have same hash code"); + } else if(r1.equals(r5)) { + System.err.println("ranges " + r1 + " and " + r5 + " are equal"); + } else if(r1.hashCode() == r5.hashCode()) { + System.err.println("range " + r1 + " and " + r5 + " have same hash code"); + } else { + System.out.println("range comparisons okay"); + } + + db.dispose(); + //cluster.dispose(); + //fdb.stopNetwork(); + System.out.println("Done with test program"); + } + + private static void checkRange(Transaction tr) { + long version = tr.getReadVersion().join(); + System.out.println("DB version: " + version); + byte[] val = tr.get("apple4".getBytes()).join(); + System.out.println("Value is " + + (val != null ? new String(val) : "not present")); + + AsyncIterable entryList = tr.getRange( + KeySelector.firstGreaterOrEqual("apple".getBytes()), + KeySelector.firstGreaterOrEqual("banana".getBytes()),4); + List entries = entryList.asList().join(); + + System.out.println("List size is " + entries.size()); + for(int i=0; i < entries.size(); i++) { + String key = new String(entries.get(i).getKey()); + String value = new String(entries.get(i).getValue()); + System.out.println(" (" + i + ") -> " + key + ", " + value); + } + + System.out.println("\nAlso:"); + for(KeyValue kv : entryList) { + String key = new String(kv.getKey()); + String value = new String(kv.getValue()); + System.out.println(" -- " + key + " -> " + value); + } + + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/SerialInsertion.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/SerialInsertion.java new file mode 100644 index 0000000000..eab2b4cdc4 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/SerialInsertion.java @@ -0,0 +1,97 @@ +/* + * SerialInsertion.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; + +public class SerialInsertion { + private static final int THREAD_COUNT = 10; + private static final int BATCH_SIZE = 1000; + private static final int NODES = 1000000; + + public static void main(String[] args) { + FDB api = FDB.selectAPIVersion(500); + Database database = api.open("T:\\circus\\tags\\RebarCluster-bbc\\cluster_id.txt"); + long start = System.currentTimeMillis(); + + List threads = new ArrayList(THREAD_COUNT); + int nodesPerThread = NODES / THREAD_COUNT; + for(int i = 0; i < THREAD_COUNT; i++) { + // deal with non even division by adding remainder onto last thread's work + if(i == THREAD_COUNT - 1) { + nodesPerThread += (NODES % THREAD_COUNT); + } + InsertionThread t = new InsertionThread(database, nodesPerThread * i, nodesPerThread); + t.start(); + threads.add(t); + } + for(InsertionThread t : threads) { + try { + t.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + System.out.println("Time taken: " + (System.currentTimeMillis() - start) + "ms"); + } + + static class InsertionThread extends Thread { + private final Database db; + private final int insertionStart; + private final int insertionCount; + + InsertionThread(Database db, int insertionStart, int insertionCount) { + this.db = db; + this.insertionStart = insertionStart; + this.insertionCount = insertionCount; + } + + @Override + public void run() { + byte[] value = new byte[] { '.', '.', '.', '.' }; + int done = 0; + ByteBuffer buf = ByteBuffer.allocate(4); + Transaction tr = db.createTransaction(); + while(done < insertionCount) { + try { + int i = 0; + for(; i < BATCH_SIZE && done + i < insertionCount; i++) { + buf.putInt(0, insertionStart + done + i); + tr.set(buf.array(), value); + } + tr.commit().join(); + tr = tr.reset(); + done += i; + } catch(RuntimeException e) { + tr = tr.onError(e).join(); + } + } + } + } + +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/SerialIteration.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/SerialIteration.java new file mode 100644 index 0000000000..fac1c93ca7 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/SerialIteration.java @@ -0,0 +1,136 @@ +/* + * SerialIteration.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.ReadTransaction; +import com.apple.cie.foundationdb.StreamingMode; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; + +public class SerialIteration { + private static final int ROWS = 1000000; + private static final int RUNS = 25; + private static final int THREAD_COUNT = 1; + + public static void main(String[] args) throws InterruptedException { + FDB api = FDB.selectAPIVersion(500); + Database database = api.open(args[0]); + + for(int i = 1; i <= THREAD_COUNT; i++) { + runThreadedTest(database, i); + Thread.sleep(1000); + } + } + + private static double runThreadedTest(Database database, int threadCount) { + List threads = new ArrayList(threadCount); + for(int i = 0; i < threadCount; i++) { + IterationThread thread = new IterationThread(database); + thread.start(); + threads.add(thread); + } + + double rowsPerSecond = 0; + for(IterationThread t : threads) { + try { + t.join(); + rowsPerSecond += t.getAverageRowsPerSecond(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + System.out.println("\n===================="); + System.out.println("Global rows/sec (over " + threadCount + " threads): " + rowsPerSecond); + return rowsPerSecond; + } + + private static class IterationThread extends Thread { + private static final Random r = new Random(); + private final Database db; + private double averageRowsPerSecond; + + IterationThread(Database database) { + this.db = database; + } + + double getAverageRowsPerSecond() { + return this.averageRowsPerSecond; + } + + @Override + public void run() { + try { + Thread.sleep(r.nextInt(1000)); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + long totalTime = 0; + int totalRows = 0; + for(int i = 0; i < RUNS; i++) { + long start = System.currentTimeMillis(); + int rowsRead = scanDatabase(db, ROWS); + long taken = System.currentTimeMillis() - start; + //System.out.println(i + ": time taken for " + ROWS + " rows: " + taken + "ms"); + System.out.print("."); + System.out.flush(); + if(i != 0) { + totalRows += rowsRead; + totalTime += taken; + } + } + averageRowsPerSecond = (totalRows / (double)totalTime) * 1000; + //System.out.println("Average rows/s: " + averageRowsPerSecond); + System.out.print("+"); + System.out.flush(); + } + } + + private static int scanDatabase(Database database, int rows) { + Transaction tr = database.createTransaction(); + tr.options().setReadYourWritesDisable(); + + ByteBuffer buf = ByteBuffer.allocate(4); + buf.putInt(0, Integer.MAX_VALUE); + AsyncIterable range = tr.getRange(new byte[0], buf.array(), + ReadTransaction.ROW_LIMIT_UNLIMITED, false, StreamingMode.WANT_ALL); + + int counter = 0; + try { + for(@SuppressWarnings("unused") KeyValue keys : range) { + counter++; + } + } catch(Exception e) { + e.printStackTrace(); + } + return counter; + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/SerialTest.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/SerialTest.java new file mode 100644 index 0000000000..992461ac38 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/SerialTest.java @@ -0,0 +1,86 @@ +/* + * SerialTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; + +public class SerialTest { + + private static final String CLUSTER_FILE = "T:\\Ben\\cluster"; + + public static void main(String[] args) throws InterruptedException { + final int reps = 1000; + try { + Cluster c = FDB.selectAPIVersion(500).createCluster(CLUSTER_FILE); + Database db = c.openDatabase(); + runTests(reps, db); + + /*Cluster fCluster = Cluster.create("C:\\Users\\Ben\\workspace\\fdb\\fdb.cluster").get(); + System.out.println("I now have the cluster"); + Database db = cluster.openDatabase().get(); + + runTests(reps, db);*/ + } catch(Throwable t) { + t.printStackTrace(); + } + } + + private static void runTests(int reps, TransactionContext db) { + System.out.println("Running tests..."); + long start = System.currentTimeMillis(); + final AtomicInteger lastcount = new AtomicInteger(0); + for(int i = 0; i < reps; i++) { + try { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + byte[] val = tr.get("count".getBytes()).join(); + //System.out.println("Got value"); + int count = Integer.parseInt(new String(val)); + tr.set("count".getBytes(), Integer.toString(count + 1).getBytes()); + lastcount.set(count); + + return null; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + } + long end = System.currentTimeMillis(); + + double seconds = (end - start) / 1000.0; + System.out.println(" Transactions: " + reps); + System.out.println(" Total Time: " + seconds); + System.out.println(" Gets+Sets / sec: " + reps / seconds); + System.out.println(" Count: " + lastcount.get()); + + System.exit(0); + } + +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Stack.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Stack.java new file mode 100644 index 0000000000..ee3bfe0521 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/Stack.java @@ -0,0 +1,57 @@ +/* + * Stack.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +class Stack { + java.util.Stack stack = new java.util.Stack(); + + void push(int idx, Object o) { + stack.push(new StackEntry(idx, o)); + } + + void push(StackEntry e) { + stack.push(e); + } + + StackEntry pop() { + return stack.pop(); + } + + void swap(int index) { + if(index >= stack.size()) + throw new IllegalArgumentException("Stack index not valid"); + + if(index > 0) { + int fromBack = stack.size() - index - 1; + StackEntry tmp = stack.elementAt(fromBack); + stack.set(fromBack, stack.pop()); + stack.push(tmp); + } + } + + int size() { + return stack.size(); + } + + void clear() { + stack.clear(); + } +} \ No newline at end of file diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackEntry.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackEntry.java new file mode 100644 index 0000000000..1b6ff36634 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackEntry.java @@ -0,0 +1,30 @@ +/* + * StackEntry.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +class StackEntry { + int idx; + Object value; + public StackEntry(int idx, Object value) { + this.idx = idx; + this.value = value; + } +} \ No newline at end of file diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackOperation.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackOperation.java new file mode 100644 index 0000000000..10c8c5c253 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackOperation.java @@ -0,0 +1,68 @@ +/* + * StackOperation.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +enum StackOperation { + PUSH, + POP, + DUP, + EMPTY_STACK, + SWAP, + WAIT_EMPTY, + START_THREAD, + WAIT_FUTURE, + NEW_TRANSACTION, + USE_TRANSACTION, + SET, + CLEAR, + CLEAR_RANGE, + CLEAR_RANGE_STARTS_WITH, + ATOMIC_OP, + + // explicit conflict ranges... + READ_CONFLICT_RANGE, + WRITE_CONFLICT_RANGE, + READ_CONFLICT_KEY, + WRITE_CONFLICT_KEY, + DISABLE_WRITE_CONFLICT, + + COMMIT, + RESET, + CANCEL, + GET, + GET_RANGE, + GET_RANGE_SELECTOR, + GET_RANGE_STARTS_WITH, + GET_KEY, + GET_READ_VERSION, + GET_COMMITTED_VERSION, + GET_VERSIONSTAMP, + SET_READ_VERSION, + ON_ERROR, + SUB, + CONCAT, + TUPLE_PACK, + TUPLE_UNPACK, + TUPLE_RANGE, + UNIT_TESTS, /* Possibly unimplemented */ + + LOG_STACK +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackTester.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackTester.java new file mode 100644 index 0000000000..458e617789 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackTester.java @@ -0,0 +1,652 @@ +/* + * StackTester.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.*; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.function.Function; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.KeySelector; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.LocalityUtil; +import com.apple.cie.foundationdb.MutationType; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.ReadTransaction; +import com.apple.cie.foundationdb.StreamingMode; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * Implements a cross-binding test of the FoundationDB API. + * + */ +public class StackTester { + static final String DIRECTORY_PREFIX = "DIRECTORY_"; + + static class WaitEmpty implements Function { + private final byte[] prefix; + WaitEmpty(byte[] prefix) { + this.prefix = prefix; + } + + @Override + public Void apply(Transaction tr) { + List asList = tr.getRange(Range.startsWith(prefix)).asList().join(); + if(asList.size() > 0) { + //System.out.println(" - Throwing new fake commit error..."); + throw new FDBException("ERROR: Fake commit conflict", 1020); + } + return null; + } + } + + static void processInstruction(Instruction inst) { + try { + StackOperation op = StackOperation.valueOf(inst.op); + if(op == StackOperation.PUSH) { + Object item = inst.tokens.get(1); + inst.push(item); + /*if(item instanceof byte[]) + System.out.println(inst.context.preStr + " - " + "Pushing '" + ByteArrayUtil.printable((byte[]) item) + "'"); + else if(item instanceof Number) + System.out.println(inst.context.preStr + " - " + "Pushing " + ((Number)item).longValue()); + else if(item instanceof String) + System.out.println(inst.context.preStr + " - " + "Pushing (utf8) '" + item.toString() + "'"); + else if(item == null) + System.out.println(inst.context.preStr + " - " + "Pushing null"); + else + System.out.println(inst.context.preStr + " - " + "Pushing item of type " + item.getClass().getName());*/ + } + else if(op == StackOperation.POP) { + inst.pop(); + } + else if(op == StackOperation.DUP) { + if(inst.size() == 0) + throw new RuntimeException("No stack bro!! (" + inst.context.preStr + ")"); + StackEntry e = inst.pop(); + inst.push(e); + inst.push(e); + } + else if(op == StackOperation.EMPTY_STACK) { + inst.clear(); + } + else if(op == StackOperation.SWAP) { + List params = inst.popParams(1).join(); + int index = StackUtils.getInt(params, 0); + inst.swap(index); + } + else if(op == StackOperation.WAIT_FUTURE) { + StackEntry e = inst.pop(); + inst.push(e.idx, StackUtils.serializeFuture(e.value)); + } + else if(op == StackOperation.WAIT_EMPTY) { + List params = inst.popParams(1).join(); + inst.context.db.run(new WaitEmpty((byte [])params.get(0))); + inst.push("WAITED_FOR_EMPTY".getBytes()); + } + else if(op == StackOperation.START_THREAD) { + List params = inst.popParams(1).join(); + //System.out.println(inst.context.preStr + " - " + "Starting new thread at prefix: " + ByteArrayUtil.printable((byte[]) params.get(0))); + inst.context.addContext((byte[])params.get(0)); + } + else if(op == StackOperation.NEW_TRANSACTION) { + inst.context.newTransaction(); + } + else if (op == StackOperation.USE_TRANSACTION) { + List params = inst.popParams(1).join(); + inst.context.switchTransaction((byte[])params.get(0)); + } + else if(op == StackOperation.SET) { + final List params = inst.popParams(2).join(); + //System.out.println(inst.context.preStr + " - " + "Setting '" + ArrayUtils.printable((byte[]) params.get(0)) + + // "' to '" + ArrayUtils.printable((byte[]) params.get(1)) + "'"); + executeMutation(inst, + new Function() { + @Override + public Void apply(Transaction tr) { + tr.set((byte[])params.get(0), (byte[])params.get(1)); + return null; + } + }); + } + else if(op == StackOperation.CLEAR) { + final List params = inst.popParams(1).join(); + //System.out.println(inst.context.preStr + " - " + "Clearing: '" + ByteArrayUtil.printable((byte[]) params.get(0)) + "'"); + executeMutation(inst, + new Function() { + @Override + public Void apply(Transaction tr) { + tr.clear((byte[])params.get(0)); + return null; + } + } + ); + } + else if(op == StackOperation.CLEAR_RANGE) { + final List params = inst.popParams(2).join(); + executeMutation(inst, + new Function() { + @Override + public Void apply(Transaction tr) { + tr.clear((byte[])params.get(0), (byte[])params.get(1)); + return null; + } + }); + } + else if(op == StackOperation.CLEAR_RANGE_STARTS_WITH) { + final List params = inst.popParams(1).join(); + executeMutation(inst, + new Function() { + @Override + public Void apply(Transaction tr) { + tr.clear(Range.startsWith((byte[])params.get(0))); + return null; + } + }); + } + else if(op == StackOperation.ATOMIC_OP) { + final List params = inst.popParams(3).join(); + final MutationType optype = MutationType.valueOf((String)params.get(0)); + executeMutation(inst, + new Function() { + @Override + public Void apply(Transaction tr) { + tr.mutate(optype, (byte[])params.get(1), (byte[])params.get(2)); + return null; + } + } + ); + } + else if(op == StackOperation.COMMIT) { + inst.push(inst.tr.commit()); + } + else if(op == StackOperation.READ_CONFLICT_RANGE) { + List params = inst.popParams(2).join(); + inst.tr.addReadConflictRange((byte[])params.get(0), (byte[])params.get(1)); + inst.push("SET_CONFLICT_RANGE".getBytes()); + } + else if(op == StackOperation.WRITE_CONFLICT_RANGE) { + List params = inst.popParams(2).join(); + inst.tr.addWriteConflictRange((byte[])params.get(0), (byte[])params.get(1)); + inst.push("SET_CONFLICT_RANGE".getBytes()); + } + else if(op == StackOperation.READ_CONFLICT_KEY) { + List params = inst.popParams(1).join(); + inst.tr.addReadConflictKey((byte[])params.get(0)); + inst.push("SET_CONFLICT_KEY".getBytes()); + } + else if(op == StackOperation.WRITE_CONFLICT_KEY) { + List params = inst.popParams(1).join(); + inst.tr.addWriteConflictKey((byte[])params.get(0)); + inst.push("SET_CONFLICT_KEY".getBytes()); + } + else if(op == StackOperation.DISABLE_WRITE_CONFLICT) { + inst.tr.options().setNextWriteNoWriteConflictRange(); + } + else if(op == StackOperation.RESET) { + inst.setTransaction(inst.tr.reset()); + } + else if(op == StackOperation.CANCEL) { + inst.tr.cancel(); + } + else if(op == StackOperation.GET) { + List params = inst.popParams(1).join(); + CompletableFuture f = inst.readTcx.read(readTr -> readTr.get((byte[])params.get(0))); + inst.push(f); + } + else if(op == StackOperation.GET_RANGE) { + List params = inst.popParams(5).join(); + + byte[] begin = (byte[])params.get(0); + byte[] end = (byte[])params.get(1); + int limit = StackUtils.getInt(params.get(2)); + boolean reverse = StackUtils.getBoolean(params.get(3)); + StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(4), StreamingMode.ITERATOR.code())); + + /*System.out.println("GetRange: " + ByteArrayUtil.printable(begin) + + ", " + ByteArrayUtil.printable(end));*/ + + List items = inst.readTcx.read(readTr -> executeRangeQuery(readTr.getRange(begin, end, limit, reverse, mode))); + inst.push(Tuple.fromItems(items).pack()); + } + else if(op == StackOperation.GET_RANGE_SELECTOR) { + List params = inst.popParams(10).join(); + + KeySelector start = StackUtils.createSelector(params.get(0),params.get(1), params.get(2)); + KeySelector end = StackUtils.createSelector(params.get(3), params.get(4), params.get(5)); + int limit = StackUtils.getInt(params.get(6)); + boolean reverse = StackUtils.getBoolean(params.get(7)); + StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(8), StreamingMode.ITERATOR.code())); + + List items = inst.readTcx.read(readTr -> executeRangeQuery(readTr.getRange(start, end, limit, reverse, mode), (byte[])params.get(9))); + inst.push(Tuple.fromItems(items).pack()); + } + else if(op == StackOperation.GET_RANGE_STARTS_WITH) { + List params = inst.popParams(4).join(); + + byte[] prefix = (byte[])params.get(0); + int limit = StackUtils.getInt(params.get(1)); + boolean reverse = StackUtils.getBoolean(params.get(2)); + StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(3), StreamingMode.ITERATOR.code())); + + List items = inst.readTcx.read(readTr -> executeRangeQuery(readTr.getRange(Range.startsWith(prefix), limit, reverse, mode))); + inst.push(Tuple.fromItems(items).pack()); + } + else if(op == StackOperation.GET_KEY) { + List params = inst.popParams(4).join(); + KeySelector start = StackUtils.createSelector(params.get(0),params.get(1), params.get(2)); + byte[] key = inst.readTcx.read(readTr -> filterKeyResult(readTr.getKey(start).join(), (byte[])params.get(3))); + inst.push(key); + } + else if(op == StackOperation.GET_READ_VERSION) { + inst.context.lastVersion = inst.readTr.getReadVersion().join(); + inst.push("GOT_READ_VERSION".getBytes()); + } + else if(op == StackOperation.GET_COMMITTED_VERSION) { + inst.context.lastVersion = inst.tr.getCommittedVersion(); + inst.push("GOT_COMMITTED_VERSION".getBytes()); + } + else if(op == StackOperation.GET_VERSIONSTAMP) { + inst.push(inst.tr.getVersionstamp()); + } + else if(op == StackOperation.SET_READ_VERSION) { + if(inst.context.lastVersion == null) + throw new IllegalArgumentException("Read version has not been read " + inst.context.preStr); + inst.tr.setReadVersion(inst.context.lastVersion); + } + else if(op == StackOperation.ON_ERROR) { + List params = inst.popParams(1).join(); + int errorCode = StackUtils.getInt(params, 0); + + // 1102 (future_released) and 2015 (future_not_set) are not errors to Java. + // This is never encountered by user code, so we have to do something rather + // messy here to get compatibility with other languages. + // + // First, try on error with a retryable error. If it fails, then the transaction is in + // a failed state and we should rethrow the error. Otherwise, throw the original error. + boolean filteredError = errorCode == 1102 || errorCode == 2015; + + FDBException err = new FDBException("Fake testing error", filteredError ? 1020 : errorCode); + + try { + inst.setTransaction(inst.tr.onError(err).join()); + } + catch(Throwable t) { + inst.context.newTransaction(); // Other bindings allow reuse of non-retryable transactions, so we need to emulate that behavior. + throw t; + } + + if(filteredError) { + throw new FDBException("Fake testing error", errorCode); + } + + inst.push(CompletableFuture.completedFuture((Void)null)); + } + else if(op == StackOperation.SUB) { + List params = inst.popParams(2).join(); + long result = StackUtils.getNumber(params.get(0)).longValue() - StackUtils.getNumber(params.get(1)).longValue(); + inst.push(result); + } + else if(op == StackOperation.CONCAT) { + List params = inst.popParams(2).join(); + if(params.get(0) instanceof String) { + inst.push((String)params.get(0) + (String)params.get(1)); + } + else { + inst.push(ByteArrayUtil.join((byte[])params.get(0), (byte[])params.get(1))); + } + } + else if(op == StackOperation.TUPLE_PACK) { + List params = inst.popParams(1).join(); + int tupleSize = StackUtils.getInt(params.get(0)); + //System.out.println(inst.context.preStr + " - " + "Packing top " + tupleSize + " items from stack"); + List elements = inst.popParams(tupleSize).join(); + byte[] coded = Tuple.fromItems(elements).pack(); + //System.out.println(inst.context.preStr + " - " + " -> result '" + ByteArrayUtil.printable(coded) + "'"); + inst.push(coded); + } + else if(op == StackOperation.TUPLE_UNPACK) { + List params = inst.popParams(1).join(); + /*System.out.println(inst.context.preStr + " - " + "Unpacking tuple code: " + + ByteArrayUtil.printable((byte[]) params.get(0)));*/ + Tuple t = Tuple.fromBytes((byte[])params.get(0)); + for(Object o : t.getItems()) { + byte[] itemBytes = Tuple.from(o).pack(); + inst.push(itemBytes); + } + } + else if(op == StackOperation.TUPLE_RANGE) { + List params = inst.popParams(1).join(); + int tupleSize = StackUtils.getInt(params, 0); + //System.out.println(inst.context.preStr + " - " + "Tuple range with top " + tupleSize + " items from stack"); + List elements = inst.popParams(tupleSize).join(); + Range range = Tuple.fromItems(elements).range(); + inst.push(range.begin); + inst.push(range.end); + } + else if(op == StackOperation.UNIT_TESTS) { + try { + inst.context.db.options().setLocationCacheSize(100001); + inst.context.db.run(tr -> { + tr.options().setPrioritySystemImmediate(); + tr.options().setPriorityBatch(); + tr.options().setCausalReadRisky(); + tr.options().setCausalWriteRisky(); + tr.options().setReadYourWritesDisable(); + tr.options().setReadAheadDisable(); + tr.options().setReadSystemKeys(); + tr.options().setAccessSystemKeys(); + tr.options().setDurabilityDevNullIsWebScale(); + tr.options().setTimeout(60*1000); + tr.options().setRetryLimit(50); + tr.options().setMaxRetryDelay(100); + tr.options().setUsedDuringCommitProtectionDisable(); + tr.options().setTransactionLoggingEnable("my_transaction"); + + if(!(new FDBException("Fake", 1020)).isRetryable() || + (new FDBException("Fake", 10)).isRetryable()) + throw new RuntimeException("Unit test failed: Error predicate incorrect"); + + byte[] test = {(byte) 0xff}; + tr.get(test).join(); + + return null; + }); + + testWatches(inst.context.db); + testLocality(inst.context.db); + } + catch(Exception e) { + throw new RuntimeException("Unit tests failed: " + e.getMessage()); + } + } + else if(op == StackOperation.LOG_STACK) { + List params = inst.popParams(1).join(); + byte[] prefix = (byte[]) params.get(0); + + Map entries = new HashMap<>(); + while(inst.size() > 0) { + entries.put(inst.size()-1, inst.pop()); + if(entries.size() == 100) { + logStack(inst.context.db, entries, prefix); + entries.clear(); + } + } + + logStack(inst.context.db, entries, prefix); + } + else { + throw new IllegalArgumentException("Unrecognized (or unimplemented) operation"); + } + } catch (FDBException e) { + //System.out.println(" Pushing error! (" + e.getMessage() + ")"); + StackUtils.pushError(inst, e); + //throw e; + } catch (CompletionException e) { + FDBException ex = StackUtils.getRootFDBException(e); + if(ex == null) { + throw e; + } + + StackUtils.pushError(inst, ex); + } + } + + static class SynchronousContext extends Context { + DirectoryExtension directoryExtension = new DirectoryExtension(); + + SynchronousContext(Database db, byte[] prefix) { + super(db, prefix); + } + + @Override + Context createContext(byte[] prefix) { + return new SynchronousContext(this.db, prefix); + } + + void processOp(byte[] operation) { + Tuple tokens = Tuple.fromBytes(operation); + Instruction inst = new Instruction(this, tokens); + + //if(!inst.op.equals("PUSH") && !inst.op.equals("SWAP")) + // System.out.println(inst.context.preStr + " - " + "OP (" + inst.context.instructionIndex + "):" + inst.op); + /*for(Object o : inst.tokens.getItems()) + System.out.print(", " + o);*/ + + if(inst.op.startsWith(DIRECTORY_PREFIX)) + directoryExtension.processInstruction(inst); + else + processInstruction(inst); + } + + @Override + void executeOperations() { + KeySelector begin = nextKey; + while(true) { + Transaction t = db.createTransaction(); + List keyValues = t.getRange(begin, endKey/*, 1000*/).asList().join(); + if(keyValues.size() == 0) + break; + //System.out.println(" * Got " + keyValues.size() + " instructions"); + + for(KeyValue next : keyValues) { + begin = KeySelector.firstGreaterThan(next.getKey()); + processOp(next.getValue()); + instructionIndex++; + } + } + //System.out.println(" * Completed " + instructionIndex + " instructions"); + } + } + + private static void executeMutation(Instruction inst, Function r) { + // run this with a retry loop (and commit) + inst.tcx.run(r); + if(inst.isDatabase) + inst.push("RESULT_NOT_PRESENT".getBytes()); + } + + static byte[] filterKeyResult(byte[] key, final byte[] prefixFilter) { + if(ByteArrayUtil.startsWith(key, prefixFilter)) { + return key; + } + else if(ByteArrayUtil.compareUnsigned(key, prefixFilter) < 0) { + return prefixFilter; + } + else { + return ByteArrayUtil.strinc(prefixFilter); + } + } + + private static List executeRangeQuery(AsyncIterable itr) { + return executeRangeQuery(itr, null); + } + + private static List executeRangeQuery(AsyncIterable itr, byte[] prefixFilter) { + if(Math.random() < 0.5) + return getRange(itr, prefixFilter); + else + return getRangeAsList(itr, prefixFilter); + } + + private static List getRange(AsyncIterable itr, byte[] prefixFilter) { + //System.out.println("GetRange"); + List o = new LinkedList<>(); + for(KeyValue kv : itr) { + if(prefixFilter == null || ByteArrayUtil.startsWith(kv.getKey(), prefixFilter)) { + o.add(kv.getKey()); + o.add(kv.getValue()); + } + } + + return o; + } + + private static List getRangeAsList(AsyncIterable itr, byte[] prefixFilter) { + //System.out.println("GetRangeAsList"); + List list = itr.asList().join(); + List o = new LinkedList<>(); + for(KeyValue kv : list) { + if(prefixFilter == null || ByteArrayUtil.startsWith(kv.getKey(), prefixFilter)) { + o.add(kv.getKey()); + o.add(kv.getValue()); + } + } + + return o; + } + + private static void logStack(Database db, Map entries, byte[] prefix) { + db.run(tr -> { + for(Map.Entry it : entries.entrySet()) { + byte[] pk = ByteArrayUtil.join(prefix, Tuple.from(it.getKey(), it.getValue().idx).pack()); + byte[] pv = Tuple.from(StackUtils.serializeFuture(it.getValue().value)).pack(); + tr.set(pk, pv.length < 40000 ? pv : Arrays.copyOfRange(pv, 0, 40000)); + } + + return null; + }); + } + + private static boolean checkWatches(List> watches, Database db, boolean expected) { + for(CompletableFuture w : watches) { + if(w.isDone() || expected) { + try { + w.join(); + if(!expected) { + throw new IllegalStateException("A watch triggered too early"); + } + } + catch(FDBException e) { + Transaction tr = db.createTransaction(); + tr.onError(e).join(); + return false; + } + } + } + + return true; + } + + private static void testWatches(Database db) { + while(true) { + db.run(tr -> { + tr.set("foo".getBytes(), "f".getBytes()); + tr.clear("bar".getBytes()); + return null; + }); + + List> watches = db.run(tr -> { + List> watchList = new LinkedList<>(); + watchList.add(tr.watch("foo".getBytes())); + watchList.add(tr.watch("bar".getBytes())); + + tr.set("foo".getBytes(), "f".getBytes()); + return watchList; + }); + + db.run(tr -> { + tr.clear("bar".getBytes()); + return null; + }); + + try { + Thread.sleep(5000); + } + catch(InterruptedException e) { + e.printStackTrace(); + // continue... + } + + if(!checkWatches(watches, db, false)) { + continue; + } + + db.run(tr -> { + tr.set("foo".getBytes(), "bar".getBytes()); + tr.set("bar".getBytes(), "foo".getBytes()); + return null; + }); + + if(checkWatches(watches, db, true)) { + return; + } + } + } + + private static void testLocality(Database db) { + db.run(tr -> { + tr.options().setTimeout(60*1000); + tr.options().setReadSystemKeys(); + tr.getReadVersion().join(); + AsyncIterable boundaryKeys = LocalityUtil.getBoundaryKeys( + tr, new byte[0], new byte[]{(byte) 255, (byte) 255}); + List keys = boundaryKeys.asList().join(); + for(int i = 0; i < keys.size() - 1; i++) { + byte[] start = keys.get(i); + byte[] end = tr.getKey(KeySelector.lastLessThan(keys.get(i + 1))).join(); + List startAddresses = Arrays.asList(LocalityUtil.getAddressesForKey(tr, start).join()); + List endAddresses = Arrays.asList(LocalityUtil.getAddressesForKey(tr, end).join()); + for(String a : startAddresses) { + if(!endAddresses.contains(a)) { + throw new RuntimeException("Locality not internally consistent."); + } + } + } + + return null; + }); + } + + /** + * Run a stack-machine based test. + * + * @param args 0: version, 1: snapshot + */ + public static void main(String[] args) { + if(args.length < 1) + throw new IllegalArgumentException("StackTester needs parameters "); + byte[] prefix = args[0].getBytes(); + + FDB fdb = FDB.selectAPIVersion(Integer.parseInt(args[1])); + Database db; + if(args.length == 2) + db = fdb.open(); + else + db = fdb.open(args[2]); + + Context c = new SynchronousContext(db, prefix); + //System.out.println("Starting test..."); + c.run(); + //System.out.println("Done with test."); + } +} + diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackUtils.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackUtils.java new file mode 100644 index 0000000000..3d0a6e50bc --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/StackUtils.java @@ -0,0 +1,121 @@ +/* + * StackUtils.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.KeySelector; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +public class StackUtils { + + static boolean pushError(Instruction inst, FDBException error) { + if(error.getCode() != 0) + inst.push(getErrorBytes(error)); + + return error.getCode() != 0; + } + + static byte[] getErrorBytes(FDBException error) { + Tuple t = new Tuple() + .add("ERROR".getBytes()) + .add(Integer.toString(error.getCode()).getBytes()); + + return t.pack(); + } + + static Object serializeFuture(Object item) { + try { + if(!(item instanceof CompletableFuture)) { + return item; + } + CompletableFuture future = (CompletableFuture)item; + item = future.join(); + if(item == null) + item = "RESULT_NOT_PRESENT".getBytes(); + } + catch(CompletionException e) { + FDBException ex = getRootFDBException(e); + if(ex == null) { + throw e; + } + + item = getErrorBytes(ex); + } + return item; + } + + //////////////////////// + // Utilities for forcing Objects into various types + //////////////////////// + static int getInt(List params, int index) { + Object object = params.get(index); + return getInt(object); + } + + static int getInt(Object object) { + return getInt(object, null); + } + + static int getInt(Object object, Integer defaultValue) { + if(object == null) { + if(defaultValue == null) + throw new NullPointerException("Null input with no default"); + return defaultValue; + } + return ((Number)object).intValue(); + } + + static Number getNumber(Object object) { + return ((Number)object); + } + + static boolean getBoolean(Object o) { + return getBoolean(o, null); + } + + static boolean getBoolean(Object o, Boolean defaultValue) { + if(o == null) { + if(defaultValue == null) + throw new NullPointerException("Null input with no default"); + return defaultValue; + } + return getInt(o) != 0; + } + + static KeySelector createSelector(Object key, Object orEqualObj, Object offsetObj) { + boolean orEqual = getBoolean(orEqualObj, null); + int offset = getInt(offsetObj); + return new KeySelector((byte[])key, orEqual, offset); + } + + static FDBException getRootFDBException(Throwable t) { + while(t != null && t != t.getCause() && !(t instanceof FDBException)){ + t = t.getCause(); + } + + return (t instanceof FDBException) ? (FDBException)t : null; + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/TestApp.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/TestApp.java new file mode 100644 index 0000000000..4f19fc8c49 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/TestApp.java @@ -0,0 +1,91 @@ +/* + * TestApp.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.concurrent.CompletableFuture; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; + +public class TestApp { + + public static void main(String[] args) throws Exception { + try { + Cluster cluster = FDB.selectAPIVersion(500).createCluster(); + System.out.println("I now have the cluster"); + Database db = cluster.openDatabase(); + + Transaction tr = db.createTransaction(); + System.out.println("TR: " + tr); + + byte[] appleValue = tr.get("apple".getBytes()).get(); + System.out.println("Apple: " + (appleValue == null ? null : new String(appleValue))); + + tr.set("apple".getBytes(), "crunchy".getBytes()); + System.out.println("Attempting to commit apple/crunchy..."); + tr.commit().get(); // FIXME: this is not an ok use of the API + tr = tr.reset(); + + long topTime = 0, getTime = 0, bottomTime = 0; + + for(int i = 0; i < 1000; i++) { + long a = System.currentTimeMillis(); + + final byte[] key = ("apple" + i).getBytes(); + tr = db.createTransaction(); + CompletableFuture future = tr.get(key); + + long b = System.currentTimeMillis(); + + future.get(); + + long c = System.currentTimeMillis(); + + tr.set(key, ("Apple" + i).getBytes()); + final CompletableFuture commit = tr.commit(); + + long d = System.currentTimeMillis(); + + commit.whenCompleteAsync((v, error) -> { + if(error != null) { + error.printStackTrace(); + } + }); + + topTime += b - a; + getTime += c - b; + bottomTime += d - c; + } + + System.out.println(" Top: " + topTime); + System.out.println(" Get: " + getTime); + System.out.println(" Bottom: " + bottomTime); + + tr.dispose(); + db.dispose(); + cluster.dispose(); + } catch(Throwable t) { + t.printStackTrace(); + } + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/TupleTest.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/TupleTest.java new file mode 100644 index 0000000000..7ff1d877c0 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/TupleTest.java @@ -0,0 +1,76 @@ +/* + * TupleTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.function.Function; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.tuple.Tuple; + +public class TupleTest { + private static final String CLUSTER_FILE = "C:\\Users\\Ben\\workspace\\fdb\\fdb.cluster"; + + public static void main(String[] args) throws InterruptedException { + final int reps = 1000; + try { + Cluster c = FDB.selectAPIVersion(500).createCluster(CLUSTER_FILE); + Database db = c.openDatabase(); + runTests(reps, db); + } catch(Throwable t) { + t.printStackTrace(); + } + } + + private static void runTests(final int reps, TransactionContext db) { + System.out.println("Running tests..."); + long start = System.currentTimeMillis(); + try { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + Tuple t = new Tuple(); + t.add(100230045000L); + t.add("Hello!"); + t.add("foo".getBytes()); + + /*for(Map.Entry e : tr.getRange("vcount".getBytes(), "zz".getBytes())) { + System.out.println("K: " + new String(e.getKey()) + ", V: " + new String(e.getValue())); + }*/ + return null; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + long end = System.currentTimeMillis(); + + double seconds = (end - start) / 1000.0; + System.out.println(" Transactions: " + reps); + System.out.println(" Total Time: " + seconds); + System.out.println(" Gets+Sets / sec: " + reps / seconds); + + System.exit(0); + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/WatchTest.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/WatchTest.java new file mode 100644 index 0000000000..cae1a923f7 --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/WatchTest.java @@ -0,0 +1,116 @@ +/* + * WatchTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.Random; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.Transaction; + +public class WatchTest { + + public static void main(String[] args) { + FDB fdb = FDB.selectAPIVersion(500); + Database database = fdb.open(args[0]); + database.options().setLocationCacheSize(42); + Transaction tr = database.createTransaction(); + byte[] bs = tr.get("a".getBytes()).join(); + System.out.println("`a' -> " + (bs == null ? "" : new String(bs))); + final CompletableFuture watch = tr.watch("a".getBytes()); + System.err.println("Watch started..."); + //System.exit(0); + tr.commit().join(); + watch.cancel(true); + try { + watch.join(); + System.out.println("`a' changed"); + } catch(FDBException e) { + System.out.println("`a' watch error -> " + e.getMessage()); + if(e.getCode() != 1101) + throw e; + } + + raceTest(database); + } + + public static void raceTest(Database db) { + ExecutorService e = Executors.newCachedThreadPool(); // Executors.newFixedThreadPool(2); + Random r = new Random(); + + Transaction tr = db.createTransaction(); + byte[] key = "hello".getBytes(); + + for(int i = 0; i < 10000; i++) { + final CompletableFuture f = tr.watch(key); + final AtomicInteger a = new AtomicInteger(); + Runnable cancel = new Runnable() { + @Override + public void run() { + System.err.println("`f' cancel()..."); + f.cancel(true); + a.incrementAndGet(); + } + }; + Runnable get = new Runnable() { + @Override + public void run() { + try { + System.err.println("`f' get()..."); + f.join(); + System.err.println("`f' changed"); + } catch(FDBException e) { + System.err.println("`f' watch error -> " + e.getMessage()); + if(e.getCode() != 1101) + throw e; + } finally { + a.incrementAndGet(); + } + } + }; + if(r.nextBoolean()) { + e.execute(cancel); + e.execute(get); + } else { + e.execute(get); + e.execute(cancel); + } + + while(a.get() != 2); { + try { + Thread.sleep(1); + } catch (InterruptedException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + + //if(i % 1000 == 0) { + System.out.println("Done with " + i); + //} + } + } +} diff --git a/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/WhileTrueTest.java b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/WhileTrueTest.java new file mode 100644 index 0000000000..e71f8c0fab --- /dev/null +++ b/bindings/java/src-completable/test/com/apple/cie/foundationdb/test/WhileTrueTest.java @@ -0,0 +1,35 @@ +/* + * WhileTrueTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import com.apple.cie.foundationdb.async.AsyncUtil; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.atomic.AtomicInteger; + +public class WhileTrueTest { + public static void main(String[] args) { + // This should cause memory issues using the old implementation but not the new one. + // Pro tip: Run with options -Xms16m -Xmx16m -XX:+HeadDumpOnOutOfMemoryError + AtomicInteger count = new AtomicInteger(1000000); + AsyncUtil.whileTrue(v -> CompletableFuture.completedFuture(count.decrementAndGet()).thenApplyAsync(c -> c > 0)).join(); + System.out.println("Final value: " + count.get()); + } +} diff --git a/bindings/java/src/junit/com/apple/cie/foundationdb/tuple/AllTests.java b/bindings/java/src/junit/com/apple/cie/foundationdb/tuple/AllTests.java new file mode 100644 index 0000000000..8be6513092 --- /dev/null +++ b/bindings/java/src/junit/com/apple/cie/foundationdb/tuple/AllTests.java @@ -0,0 +1,31 @@ +/* + * AllTests.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.tuple; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ ArrayUtilTests.class }) +public class AllTests { + +} diff --git a/bindings/java/src/junit/com/apple/cie/foundationdb/tuple/ArrayUtilTests.java b/bindings/java/src/junit/com/apple/cie/foundationdb/tuple/ArrayUtilTests.java new file mode 100644 index 0000000000..a984a6d9ef --- /dev/null +++ b/bindings/java/src/junit/com/apple/cie/foundationdb/tuple/ArrayUtilTests.java @@ -0,0 +1,308 @@ +/* + * ArrayUtilTests.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.tuple; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.util.ArrayList; +import java.util.List; + +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import org.junit.Test; + +/** + * @author Ben + * + */ +public class ArrayUtilTests { + + /** + * Test method for {@link ByteArrayUtil#join(byte[], java.util.List)}. + */ + @Test + public void testJoinByteArrayListOfbyte() { + byte[] a = new byte[] {'a', 'b', 'c'}; + byte[] b = new byte[] {'d', 'e', 'f'}; + + List parts = new ArrayList(); + parts.add(a); + parts.add(b); + parts.add(new byte[] {}); + byte[] result = new byte[] {'a', 'b', 'c', 'z', 'd', 'e', 'f', 'z'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(a); + parts.add(b); + result = new byte[] {'z', 'a', 'b', 'c', 'z', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(a); + parts.add(b); + result = new byte[] {'z', 'z', 'a', 'b', 'c', 'z', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(a); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(b); + result = new byte[] {'a', 'b', 'c', 'z', 'z', 'z', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(a); + parts.add(b); + parts.add(new byte[] {'b'}); + result = new byte[] {'a', 'b', 'c', 'z', 'd', 'e', 'f', 'z', 'b'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + result = new byte[] {'z', 'z'}; + assertArrayEquals(result, ByteArrayUtil.join(new byte[] {'z'}, parts)); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + result = new byte[] {}; + assertArrayEquals(result, ByteArrayUtil.join(null, parts)); + } + + /** + * Test method for {@link ByteArrayUtil#join(byte[][])}. + */ + @Test + public void testJoinByteArrayArray() { + byte[] a = new byte[] {'a', 'b', 'c'}; + byte[] b = new byte[] {'d', 'e', 'f'}; + + List parts = new ArrayList(); + parts.add(a); + parts.add(b); + parts.add(new byte[] {}); + byte[] result = new byte[] {'a', 'b', 'c', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.join(parts.toArray(new byte[][]{}))); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(a); + parts.add(b); + result = new byte[] {'a', 'b', 'c', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.join(parts.toArray(new byte[][]{}))); + + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(a); + parts.add(b); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {'b'}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + parts.add(new byte[] {}); + result = new byte[] {'a', 'b', 'c', 'd', 'e', 'f', 'b'}; + assertArrayEquals(result, ByteArrayUtil.join(parts.toArray(new byte[][]{}))); + + parts = new ArrayList(); + parts.add(a); + parts.add(b); + parts.add(new byte[] {'b'}); + result = new byte[] {'a', 'b', 'c', 'd', 'e', 'f', 'b'}; + assertArrayEquals(result, ByteArrayUtil.join(parts.toArray(new byte[][]{}))); + + // Self-referential, with conversion to array + parts = new ArrayList(); + parts.add(new byte[] {}); + parts.add(a); + parts.add(b); + parts.add(new byte[] {}); + assertArrayEquals(ByteArrayUtil.join(a, b), ByteArrayUtil.join(parts.toArray(new byte[][]{}))); + + // Test exception on null elements + boolean isError = false; + try { + ByteArrayUtil.join(a, b, null); + } catch(Exception e) { + isError = true; + } finally { + assertTrue(isError); + } + } + + /** + * Test method for {@link ByteArrayUtil#regionEquals(byte[], int, byte[])}. + */ + @Test + public void testRegionEquals() { + byte[] src = new byte[] {'a', (byte)12, (byte)255, 'n', 'm', 'z', 'k'}; + assertTrue(ByteArrayUtil.regionEquals(src, 3, new byte[] { 'n', 'm' })); + + assertFalse(ByteArrayUtil.regionEquals(src, 2, new byte[] { 'n', 'm' })); + + assertTrue(ByteArrayUtil.regionEquals(null, 0, null)); + + assertFalse(ByteArrayUtil.regionEquals(src, 0, null)); + } + + /** + * Test method for {@link ByteArrayUtil#replace(byte[], byte[], byte[])}. + */ + @Test + public void testReplace() { + byte[] a = new byte[] {'a', 'b', 'c'}; + byte[] b = new byte[] {'d', 'e', 'f'}; + + byte[] src = ByteArrayUtil.join(a, b, a, b); + byte[] result = new byte[] {'z', 'd', 'e', 'f', 'z', 'd', 'e', 'f'}; + assertArrayEquals(result, ByteArrayUtil.replace(src, a, new byte[] {'z'})); + + src = ByteArrayUtil.join(a, b, a, b); + assertArrayEquals(ByteArrayUtil.join(b, b), ByteArrayUtil.replace(src, a, new byte[] {})); + + src = ByteArrayUtil.join(a, b, a, b); + assertArrayEquals(ByteArrayUtil.join(a, a), ByteArrayUtil.replace(src, b, new byte[] {})); + + src = ByteArrayUtil.join(a, a, a); + assertArrayEquals(new byte[] {}, ByteArrayUtil.replace(src, a, new byte[] {})); + } + + /** + * Test method for {@link ByteArrayUtil#split(byte[], byte[])}. + */ + @Test + public void testSplit() { + byte[] a = new byte[] {'a', 'b', 'c'}; + byte[] b = new byte[] {'d', 'e', 'f'}; + + byte[] src = ByteArrayUtil.join(a, b, a, b, a); + List parts = ByteArrayUtil.split(src, b); + assertEquals(parts.size(), 3); + for(byte[] p : parts) { + assertArrayEquals(a, p); + } + + src = ByteArrayUtil.join(b, a, b, a, b, a); + parts = ByteArrayUtil.split(src, b); + assertEquals(parts.size(), 4); + int counter = 0; + for(byte[] p : parts) { + if(counter++ == 0) + assertArrayEquals(new byte[]{}, p); + else + assertArrayEquals(a, p); + } + + src = ByteArrayUtil.join(a, b, a, b, a, b); + parts = ByteArrayUtil.split(src, b); + assertEquals(parts.size(), 4); + counter = 0; + for(byte[] p : parts) { + if(counter++ < 3) + assertArrayEquals(a, p); + else + assertArrayEquals(new byte[]{}, p); + } + + // Multiple ending delimiters + src = ByteArrayUtil.join(a, b, a, b, a, b, b, b); + parts = ByteArrayUtil.split(src, b); + assertEquals(parts.size(), 6); + counter = 0; + for(byte[] p : parts) { + if(counter++ < 3) + assertArrayEquals(a, p); + else + assertArrayEquals(new byte[]{}, p); + } + } + + /** + * Test method for {@link ByteArrayUtil#bisectLeft(java.math.BigInteger[], java.math.BigInteger)}. + */ + @Test + public void testBisectLeft() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#compareUnsigned(byte[], byte[])}. + */ + @Test + public void testCompare() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#findNext(byte[], byte, int)}. + */ + @Test + public void testFindNext() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#findTerminator(byte[], byte, byte, int)}. + */ + @Test + public void testFindTerminator() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#copyOfRange(byte[], int, int)}. + */ + @Test + public void testCopyOfRange() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#strinc(byte[])}. + */ + @Test + public void testStrinc() { + fail("Not yet implemented"); + } + + /** + * Test method for {@link ByteArrayUtil#printable(byte[])}. + */ + @Test + public void testPrintable() { + fail("Not yet implemented"); + } + +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/Cluster.java b/bindings/java/src/main/com/apple/cie/foundationdb/Cluster.java new file mode 100644 index 0000000000..19a579d57a --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/Cluster.java @@ -0,0 +1,102 @@ +/* + * Cluster.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.nio.charset.Charset; +import java.util.concurrent.Executor; + + +/** + * The {@code Cluster} represents a connection to a physical set of cooperating machines + * running FoundationDB. A {@code Cluster} is opened with a reference to a cluster file. + */ +public class Cluster extends DefaultDisposableImpl implements Disposable { + private Executor executor; + private ClusterOptions options; + + private static final Charset UTF8 = Charset.forName("UTF-8"); + + protected Cluster(long cPtr, Executor e) { + super(cPtr); + this.executor = e; + this.options = new ClusterOptions(new OptionConsumer() { + @Override + public void setOption(int code, byte[] parameter) { + pointerReadLock.lock(); + try { + Cluster_setOption(getPtr(), code, parameter); + } finally { + pointerReadLock.unlock(); + } + } + }); + } + + /** + * Returns a set of options that can be set on a {@code Cluster}. In the current version + * of the API, there are no options that can be set on a {@code Cluster}. + * + * @return a set of cluster-specific options affecting this {@code Cluster} + */ + public ClusterOptions options() { return options; } + + @Override + protected void finalize() throws Throwable { + dispose(); + super.finalize(); + } + + /** + * Creates a connection to a specific database on an FDB cluster. + * + * @return a {@code Future} that will be set to a {@code Database} upon + * successful connection. + */ + public Database openDatabase() throws FDBException { + return openDatabase(executor); + } + + /** + * Creates a connection to a specific database on an FDB cluster. + * + * @return a {@code Future} that will be set to a {@code Database} upon + * successful connection. + */ + public Database openDatabase(Executor e) throws FDBException { + FutureDatabase futureDatabase = null; + pointerReadLock.lock(); + try { + futureDatabase = new FutureDatabase(Cluster_createDatabase(getPtr(), "DB".getBytes(UTF8)), e); + } finally { + pointerReadLock.unlock(); + } + return futureDatabase.get(); + } + + @Override + protected void disposeInternal(long cPtr) { + Cluster_dispose(cPtr); + } + + private native void Cluster_dispose(long cPtr); + private native long Cluster_createDatabase(long cPtr, byte[] dbName); + private native void Cluster_setOption(long cPtr, int code, byte[] value) throws FDBException; +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/Database.java b/bindings/java/src/main/com/apple/cie/foundationdb/Database.java new file mode 100644 index 0000000000..c02e66c1a0 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/Database.java @@ -0,0 +1,328 @@ +/* + * Database.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.PartialFunction; +import com.apple.cie.foundationdb.async.PartialFuture; + +import java.util.concurrent.Executor; + +/** + * A mutable, lexicographically ordered mapping from binary keys to binary values. + * A {@code Database} is stored on a FoundationDB {@link Cluster}. + * {@link Transaction}s are used to manipulate data within a single + * {@code Database} -- multiple, concurrent + * {@code Transaction}s on a {@code Database} enforce ACID properties.
    + *
    + * The simplest correct programs using FoundationDB will make use of the methods defined + * in the {@link TransactionContext} interface. When used on a {@code Database} these + * methods will call {@code Transaction#commit()} after user code has been + * executed. These methods will not return successfully until {@code commit()} has + * returned successfully. + * + */ +public interface Database extends Disposable, TransactionContext { + /** + * Creates a {@link Transaction} that operates on this {@code Database}.
    + *
    + * Note: Java transactions automatically set the {@link TransactionOptions#setUsedDuringCommitProtectionDisable} + * option. This is because the Java bindings disallow use of {@code Transaction} objects after either + * {@link Transaction#reset} or {@link Transaction#onError} is called. + * + * @return a newly created {@code Transaction} that reads from and writes to this {@code Database}. + */ + public Transaction createTransaction(); + + /** + * Creates a {@link Transaction} that operates on this {@code Database} with the given {@link Executor} + * for asynchronous callbacks. + * + * @param e the {@link Executor} to use when executing asynchronous callbacks for the database + * @return a newly created {@code Transaction} that reads from and writes to this {@code Database}. + */ + public Transaction createTransaction(Executor e); + + /** + * Returns a set of options that can be set on a {@code Database} + * + * @return a set of database-specific options affecting this {@code Database} + */ + public DatabaseOptions options(); + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. + * {@link Function#apply(Object) apply(ReadTransaction)} will be called on the + * supplied {@link Function} until a non-retryable + * {@link FDBException} (or any {@code Throwable} other than an {@code FDBException}) + * is thrown. This call is blocking -- this + * method will not return until the {@code Function} has been called and completed without error.
    + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + */ + @Override + public T read(Function retryable); + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. Use + * this formulation of {@link #read(Function)} if one wants to set a custom {@link Executor} + * for the transaction when run. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + * + * @see #read(Function) + */ + public T read(Function retryable, Executor e); + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. Use + * this formulation of {@link #read(Function)} if the user code being executed + * throws a checked exception. + * + * @param retryable the block of logic to execute in a {@link ReadTransaction} against + * this database + * + * @throws Exception if non-transient database errors are encountered or user code throws any other error + * + * @see #read(Function) + */ + @Override + public abstract T read(PartialFunction retryable) throws Exception; + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. Use + * this formulation of {@link #read(Function)} if the user code being executed + * throws a checked exception and one wants to set a custom {@link Executor} for the + * transaction when run. + * + * @param retryable the block of logic to execute in a {@link ReadTransaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + * + * @throws Exception if non-transient database errors are encountered or user code throws any other error + * + * @see #read(Function) + */ + public abstract T read(PartialFunction retryable, Executor e) throws Exception; + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. + * {@link Function#apply(Object) apply(ReadTransaction)} will be called on the + * supplied {@link Function} until a non-retryable + * {@link FDBException} (or any {@code Throwable} other than an {@code FDBException}) + * is thrown. This call is non-blocking -- this + * method will return immediately and with a {@link Future} that will be + * set when the {@code Function} has been called and completed without error.
    + *
    + * Any errors encountered executing {@code retryable}, or received from the + * database, will be set on the returned {@code Future}. + * + * @param retryable the block of logic to execute in a {@link ReadTransaction} against + * this database + */ + @Override + public abstract Future readAsync( + Function> retryable); + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. + * Use this version of {@link #readAsync(Function)} if one wants to set a custom + * {@link Executor} for the transaction when run. + * + * @param retryable the block of logic to execute in a {@link ReadTransaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + * + * @see #readAsync(Function) + */ + public abstract Future readAsync( + Function> retryable, Executor e); + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. Use + * this formulation of the non-blocking {@link #readAsync(Function)} if the user code being executed + * throws a checked exception. + * + * @param retryable the block of logic to execute in a {@link ReadTransaction} against + * this database + * + * @see #read(Function) + */ + @Override + public abstract PartialFuture readAsync( + PartialFunction> retryable); + + /** + * Runs a read-only transactional function against this {@code Database} with retry logic. Use + * this formulation of the non-blocking {@link #readAsync(Function)} if the user code being executed + * throws a checked exception and one wants to set a custom {@link Executor} for the + * transaction when run. + * + * @param retryable the block of logic to execute in a {@link ReadTransaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + * + * @see #read(Function) + */ + public abstract PartialFuture readAsync( + PartialFunction> retryable, Executor e); + + + /** + * Runs a transactional function against this {@code Database} with retry logic. + * {@link Function#apply(Object) apply(Transaction)} will be called on the + * supplied {@link Function} until a non-retryable + * {@link FDBException} (or any {@code Throwable} other than an {@code FDBException}) + * is thrown or {@link Transaction#commit() commit()}, + * when called after {@code apply()}, returns success. This call is blocking -- this + * method will not return until {@code commit()} has been called and returned success.
    + *
    + * As with other client/server databases, in some failure scenarios a client may + * be unable to determine whether a transaction succeeded. In these cases, your + * transaction may be executed twice. For more information about how to reason + * about these situations see + * the FounationDB Developer Guide + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + */ + @Override + public T run(Function retryable); + + /** + * Runs a transactional function against this {@code Database} with retry logic. + * Use this formulation of {@link #run(Function)} if the user code will not throw + * a checked exception but one would like to set a custom {@link Executor} + * for the transaction when run. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + */ + public T run(Function retryable, Executor e); + + /** + * Runs a transactional function against this {@code Database} with retry logic. Use + * this formulation of {@link #run(Function)} if the user code being executed + * throws a checked exception. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + * + * @throws Exception if non-transient database errors are encountered or user code throws any other error + * + * @see #run(Function) + */ + @Override + public abstract T run(PartialFunction retryable) throws Exception; + + /** + * Runs a transactional function against this {@code Database} with retry logic. Use + * this formulation of {@link #run(Function)} if the user code being executed + * throws a checked exception and one would like to set a custom executor for the + * transaction when run. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + * + * @throws Exception if non-transient database errors are encountered or user code throws any other error + * + * @see #run(Function) + */ + public abstract T run(PartialFunction retryable, Executor e) throws Exception; + + /** + * Runs a transactional function against this {@code Database} with retry logic. + * {@link Function#apply(Object) apply(Transaction)} will be called on the + * supplied {@link Function} until a non-retryable + * {@link FDBException} (or any {@code Throwable} other than an {@code FDBException}) + * is thrown or {@link Transaction#commit() commit()}, + * when called after {@code apply()}, returns success. This call is non-blocking -- this + * method will return immediately and with a {@link Future} that will be + * set when {@code commit()} has been called and returned success.
    + *
    + * As with other client/server databases, in some failure scenarios a client may + * be unable to determine whether a transaction succeeded. In these cases, your + * transaction may be executed twice. For more information about how to reason + * about these situations see + * the FounationDB Developer Guide
    + *
    + * Any errors encountered executing {@code retryable}, or received from the + * database, will be set on the returned {@code Future}. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + */ + @Override + public abstract Future runAsync( + Function> retryable); + + /** + * Runs a transactional function against this {@code Database} with retry logic. Use + * this formulation of the non-blocking {@link #runAsync(Function)} if one wants + * to set a custom {@link Executor} for the transaction when run. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + * + * @see #run(Function) + */ + public abstract Future runAsync( + Function> retryable, Executor e); + + /** + * Runs a transactional function against this {@code Database} with retry logic. Use + * this formulation of the non-blocking {@link #runAsync(Function)} if the user code being executed + * throws a checked exception. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + * + * @see #run(Function) + */ + @Override + public abstract PartialFuture runAsync( + PartialFunction> retryable); + + /** + * Runs a transactional function against this {@code Database} with retry logic. Use + * this formulation of the non-blocking {@link #runAsync(Function)} if the user code being executed + * throws a checked exception and one wants to set a custom {@link Executor} for the transaction + * when run. + * + * @param retryable the block of logic to execute in a {@link Transaction} against + * this database + * @param e the {@link Executor} to use for asynchronous callbacks + * + * @see #run(Function) + */ + public abstract PartialFuture runAsync( + PartialFunction> retryable, Executor e); + +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/DefaultDisposableImpl.java b/bindings/java/src/main/com/apple/cie/foundationdb/DefaultDisposableImpl.java new file mode 100644 index 0000000000..c7c0464447 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/DefaultDisposableImpl.java @@ -0,0 +1,82 @@ +/* + * DefaultDisposableImpl.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +abstract class DefaultDisposableImpl implements Disposable { + private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); + protected final Lock pointerReadLock = rwl.readLock(); + + private boolean disposed = false; + private long cPtr; + + public DefaultDisposableImpl() { + } + + public DefaultDisposableImpl(long cPtr) { + this.cPtr = cPtr; + if(this.cPtr == 0) + this.disposed = true; + } + + public boolean isDisposed() { + // we must have a read lock for this function to make sense, however it + // does not make sense to take the lock here, since the code that uses + // the result must inherently have the read lock itself. + assert( rwl.getReadHoldCount() > 0 ); + + return disposed; + } + + @Override + public void dispose() { + rwl.writeLock().lock(); + long ptr = 0; + try { + if(disposed) + return; + + ptr = cPtr; + this.cPtr = 0; + disposed = true; + } finally { + rwl.writeLock().unlock(); + } + + disposeInternal(ptr); + } + + protected long getPtr() { + // we must have a read lock for this function to make sense, however it + // does not make sense to take the lock here, since the code that uses + // the result must inherently have the read lock itself. + assert( rwl.getReadHoldCount() > 0 ); + + if(this.disposed) + throw new IllegalStateException("Cannot access disposed object"); + + return this.cPtr; + } + + protected abstract void disposeInternal(long cPtr); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/Disposable.java b/bindings/java/src/main/com/apple/cie/foundationdb/Disposable.java new file mode 100644 index 0000000000..b1e211c013 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/Disposable.java @@ -0,0 +1,33 @@ +/* + * Disposable.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +/** + * A FoundationDB object with native resources that can be freed. It is not mandatory to call + * {@link Disposable#dispose()} most of the time, as disposal will happen at finalization. + */ +public interface Disposable { + /** + * Dispose of the object. This can be called multiple times, but care should be + * taken that an object is not in use in another thread at the time of the call. + */ + void dispose(); +} \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FDB.java b/bindings/java/src/main/com/apple/cie/foundationdb/FDB.java new file mode 100644 index 0000000000..3f652a4ab3 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FDB.java @@ -0,0 +1,411 @@ +/* + * FDB.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Semaphore; +import java.util.concurrent.ThreadFactory; + +/** + * The starting point for accessing FoundationDB. + *
    + *

    Setting API version

    + * The FoundationDB API is accessed with a call to {@link #selectAPIVersion(int)}. + * This call is required before using any other part of the API. The call allows + * an error to be thrown at this point to prevent client code from accessing a later library + * with incorrect assumptions from the current version. The API version documented here is version + * {@code 500}.

    + * FoundationDB encapsulates multiple versions of its interface by requiring + * the client to explicitly specify the version of the API it uses. The purpose + * of this design is to allow you to upgrade the server, client libraries, or + * bindings without having to modify client code. The client libraries support + * all previous versions of the API. The API version specified by the client is + * used to control the behavior of the binding. You can therefore upgrade to + * more recent packages (and thus receive various improvements) without having + * to change your code.

    + * Warning: When using the multi-version client API, setting an API version that + * is not supported by a particular client library will prevent that client from + * being used to connect to the cluster. In particular, you should not advance + * the API version of your application after upgrading your client until the + * cluster has also been upgraded.
    + *

    Getting a database

    + * Once the API version has been set, the easiest way to get a {@link Database} object to use is + * to call {@link #open}. + *
    + *

    Client networking

    + * The network is started either implicitly with a call to a variant of {@link #open()} or + * {@link #createCluster()}, or started explicitly with a call to {@link #startNetwork()}. + *
    + * + */ +public class FDB { + static FDB singleton = null; + + static class DaemonThreadFactory implements ThreadFactory { + private final ThreadFactory factory; + + public DaemonThreadFactory(ThreadFactory factory) { + this.factory = factory; + } + + @Override + public Thread newThread(Runnable r) { + Thread t = factory.newThread(r); + t.setDaemon(true); + return t; + } + } + + public static final ExecutorService DEFAULT_EXECUTOR; + + final int apiVersion; + private volatile boolean netStarted = false; + private volatile boolean netStopped = false; + final private Semaphore netRunning = new Semaphore(1); + private final NetworkOptions options; + + static { + try { + JNIUtil.loadLibrary("fdb_c"); + } catch (Throwable t) { + // EAT: this can be useful for loading on windows + } + JNIUtil.loadLibrary("fdb_java"); + + ThreadFactory factory = new DaemonThreadFactory(Executors.defaultThreadFactory()); + DEFAULT_EXECUTOR = Executors.newCachedThreadPool(factory); + } + + /** + * Called only once to create the FDB singleton. + */ + private FDB(int apiVersion) { + this.apiVersion = apiVersion; + + options = new NetworkOptions(new OptionConsumer() { + @Override + public void setOption(int code, byte[] parameter) { + Network_setOption(code, parameter); + } + }); + + Runtime.getRuntime().addShutdownHook(new Thread( + new Runnable(){ + @Override + public void run() { + FDB.this.stopNetwork(); + } + } + )); + } + + /** + * Returns a set of options that can be set on a the FoundationDB API. Generally, + * these options to the top level of the API affect the networking engine and + * therefore must be set before the network engine is started. The network is started + * by calls to {@link #startNetwork()} and implicitly by calls to {@link #open()} and + * {@link #createCluster()} (and their respective variants). + * + * @return a set of options affecting this instance of the FoundationDB API + */ + public NetworkOptions options() { return options; } + + /** + * Select the version for the client API. An exception will be thrown if the + * requested version is not supported by this implementation of the API. As + * only one version can be selected for the lifetime of the JVM, the result + * of a successful call to this method is always the same instance of a FDB + * object.

    + * + * Warning: When using the multi-version client API, setting an API version that + * is not supported by a particular client library will prevent that client from + * being used to connect to the cluster. In particular, you should not advance + * the API version of your application after upgrading your client until the + * cluster has also been upgraded. + * + * @param version the API version required + * + * @return the FoundationDB API object + */ + public synchronized static FDB selectAPIVersion(final int version) throws FDBException { + if(singleton != null) { + if(version != singleton.apiVersion) { + throw new IllegalArgumentException( + "FoundationDB API already started at different version"); + } + return singleton; + } + if(version < 500) + throw new IllegalArgumentException("API version not supported (minimum 500)"); + if(version > 500) + throw new IllegalArgumentException("API version not supported (maximum 500)"); + Select_API_version(version); + return singleton = new FDB(version); + } + + /** + * Connects to the cluster specified by the + * default fdb.cluster file. + * If the FoundationDB network has not been started, it will be started in the course of this call + * as if {@link FDB#startNetwork()} had been called. + * + * @return a {@code Future} that will be set to a FoundationDB {@code Cluster}. + * + * @throws FDBException on errors encountered starting the FoundationDB networking engine + * @throws IllegalStateException if the network had been previously stopped + */ + public Cluster createCluster() throws IllegalStateException, FDBException { + return createCluster(null, DEFAULT_EXECUTOR); + } + + /** + * Connects to the cluster specified by {@code clusterFilePath}. If the FoundationDB network + * has not been started, it will be started in the course of this call as if + * {@link #startNetwork()} had been called. + * + * @param clusterFilePath the + * cluster file + * defining the FoundationDB cluster. This can be {@code null} if the + * default fdb.cluster file + * is to be used. + * + * @return a {@code Future} that will be set to a FoundationDB {@code Cluster}. + * + * @throws FDBException on errors encountered starting the FoundationDB networking engine + * @throws IllegalStateException if the network had been previously stopped + */ + public Cluster createCluster(String clusterFilePath) throws IllegalStateException, FDBException { + return createCluster(clusterFilePath, DEFAULT_EXECUTOR); + } + + /** + * Connects to the cluster specified by {@code clusterFilePath}. If the FoundationDB network + * has not been started, it will be started in the course of this call. The supplied + * {@link Executor} will be used as the default for the execution of all callbacks that + * are produced from using the resulting {@link Cluster}. + * + * @param clusterFilePath the + * cluster file + * defining the FoundationDB cluster. This can be {@code null} if the + * default fdb.cluster file + * is to be used. + * @param e used to execute all callbacks from the {@link Cluster}. + * + * @return a {@code Future} that will be set to a FoundationDB {@code Cluster}. + * + * @throws FDBException on errors encountered starting the FoundationDB networking engine + * @throws IllegalStateException if the network had been previously stopped + */ + public Cluster createCluster(String clusterFilePath, Executor e) + throws FDBException, IllegalStateException { + FutureCluster f; + synchronized (this) { + if (!isConnected()) { + startNetwork(e); + } + f = new FutureCluster(Cluster_create(clusterFilePath), e); + } + return f.get(); + } + + /** + * Initializes networking, connects with the + * default fdb.cluster file, + * and opens the database. + * + * @return a {@code Future} that will be set to a FoundationDB {@link Database} + */ + public Database open() throws FDBException { + return open(null); + } + + /** + * Initializes networking, connects to the cluster specified by {@code clusterFilePath} + * and opens the database. + * + * @param clusterFilePath the + * cluster file + * defining the FoundationDB cluster. This can be {@code null} if the + * default fdb.cluster file + * is to be used. + * + * @return a {@code Future} that will be set to a FoundationDB {@link Database} + */ + public Database open(String clusterFilePath) throws FDBException { + return open(clusterFilePath, DEFAULT_EXECUTOR); + } + + /** + * Initializes networking, connects to the cluster specified by {@code clusterFilePath} + * and opens the database. + * + * @param clusterFilePath the + * cluster file + * defining the FoundationDB cluster. This can be {@code null} if the + * default fdb.cluster file + * is to be used. + * @param e the {@link Executor} to use when executing asynchronous callbacks + * + * @return a {@code Future} that will be set to a FoundationDB {@link Database} + */ + public Database open(String clusterFilePath, Executor e) throws FDBException { + FutureCluster f; + synchronized (this) { + if (!isConnected()) { + startNetwork(); + } + f = new FutureCluster(Cluster_create(clusterFilePath), e); + } + Cluster c = f.get(); + return c.openDatabase(e); + } + + /** + * Initializes networking. Can only be called once. This version of + * {@code startNetwork()} will use the default thread pool to execute the + * FoundationDB network.
    + *
    + * Configuration of the networking engine can be achieved through calls to the methods + * in {@link NetworkOptions}. + * + * @throws IllegalStateException if the network has already been stopped + * + * @see NetworkOptions + */ + public void startNetwork() throws FDBException, IllegalStateException { + startNetwork(DEFAULT_EXECUTOR); + } + + /** + * Initializes networking. Can only be called once. The FoundationDB + * networking event loop will be run in the specified {@code Executor}. This + * event loop is a blocking operation that is not + * expected to terminate until the program in complete. This will therefore consume an + * entire thread from {@code e} if {@code e} is a thread pool or will completely block + * operation of a single threaded {@code Executor}.
    + *
    + * Manual configuration of the networking engine can be achieved through calls on + * {@link NetworkOptions}. These options should be set before a call + * to this method. + * + * @see NetworkOptions + * + * @throws IllegalStateException if the network has already been stopped + */ + public synchronized void startNetwork(Executor e) throws FDBException, IllegalStateException { + if(netStopped) + throw new IllegalStateException("Network has been stopped and cannot be restarted"); + if(netStarted) { + return; + } + Network_setup(); + netStarted = true; + + e.execute(new Runnable() { + @Override + public void run() { + boolean acquired = false; + try { + while(!acquired) { + try { + // make attempt to avoid a needless deadlock + synchronized (FDB.this) { + if(netStopped) { + return; + } + } + + netRunning.acquire(); + acquired = true; + } catch(InterruptedException e) {} + } + try { + Network_run(); + } catch (Throwable t) { + System.err.println("Unhandled error in FoundationDB network thread: " + t.getMessage()); + // eat this error. we have nowhere to send it. + } + } finally { + if(acquired) { + netRunning.release(); + } + synchronized (FDB.this) { + netStopped = true; + } + } + } + }); + } + + /** + * Gets the state of the FoundationDB networking thread. + * + * @return {@code true} if the FDB network thread is running, {@code false} otherwise. + */ + private synchronized boolean isConnected() { + return netStarted && !netStopped; + } + + /** + * Stops the FoundationDB networking engine. This can be called only once -- the network + * cannot be restarted after this call. This call blocks for the completion of + * the FoundationDB networking engine. + * + * @throws FDBException on errors while stopping the network + */ + public synchronized void stopNetwork() throws FDBException { + if(!netStarted || netStopped) { + netStopped = true; + return; + } + Network_stop(); + // set netStarted here in case the network has never really ever been run + netStopped = netStarted = true; + while(true) { + try { + // This will be released when runNetwork() returns. + // Taking this and never releasing it will also assure + // that we will never again be able to call runNetwork() + netRunning.acquire(); + return; + } catch (InterruptedException e) {} + } + } + + protected static boolean evalErrorPredicate(int predicate, int code) { + if(singleton == null) + throw new IllegalStateException("FDB API not yet initalized"); + return singleton.Error_predicate(predicate, code); + } + + static native void Select_API_version(int version) throws FDBException; + + private native void Network_setOption(int code, byte[] value) throws FDBException; + private native void Network_setup() throws FDBException; + private native void Network_run() throws FDBException; + private native void Network_stop() throws FDBException; + + private native boolean Error_predicate(int predicate, int code); + + private native long Cluster_create(String clusterFileName); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FDBDatabase.java b/bindings/java/src/main/com/apple/cie/foundationdb/FDBDatabase.java new file mode 100644 index 0000000000..6eff53a0c9 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FDBDatabase.java @@ -0,0 +1,279 @@ +/* + * FDBDatabase.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; +import java.util.concurrent.atomic.AtomicReference; + +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.PartialFunction; +import com.apple.cie.foundationdb.async.PartialFuture; + +class FDBDatabase extends DefaultDisposableImpl implements Database, Disposable, OptionConsumer { + private Executor executor; + private DatabaseOptions options; + + protected FDBDatabase(long cPtr, Executor executor) { + super(cPtr); + this.executor = executor; + this.options = new DatabaseOptions(this); + } + + @Override + public DatabaseOptions options() { + return options; + } + + @Override + public T run(Function retryable) { + return this.run(retryable, executor); + } + + @Override + public T run(Function retryable, Executor e) { + Transaction t = this.createTransaction(e); + try { + while (true) { + try { + T returnVal = retryable.apply(t); + t.commit().get(); + return returnVal; + } catch (RuntimeException err) { + t = t.onError(err).get(); + } + } + } finally { + t.dispose(); + } + } + + @Override + public T read(Function retryable) { + return this.read(retryable, executor); + } + + @Override + public T read(Function retryable, Executor e) { + return this.run(retryable, e); + } + + @Override + public T run(PartialFunction retryable) throws Exception { + return this.run(retryable, executor); + } + + @Override + public T run(PartialFunction retryable, Executor e) throws Exception { + Transaction t = this.createTransaction(e); + try { + while (true) { + try { + T returnVal = retryable.apply(t); + t.commit().get(); + return returnVal; + } catch (RuntimeException err) { + t = t.onError(err).get(); + } + } + } finally { + t.dispose(); + } + } + + @Override + public T read(PartialFunction retryable) + throws Exception { + return this.read(retryable, executor); + } + + @Override + public T read(PartialFunction retryable, Executor e) + throws Exception { + return this.run(retryable, e); + } + + @Override + public Future runAsync(final Function> retryable) { + return this.runAsync(retryable, executor); + } + + @Override + public Future runAsync(final Function> retryable, Executor e) { + final AtomicReference trRef = new AtomicReference(createTransaction(e)); + final AtomicReference returnValue = new AtomicReference(); + return AsyncUtil.whileTrue(new Function>() { + @Override + public Future apply(Void v) { + Future process = AsyncUtil.applySafely(retryable, trRef.get()); + + return process.flatMap(new Function>() { + @Override + public Future apply(final T returnVal) { + return trRef.get().commit().map(new Function() { + @Override + public Boolean apply(Void o) { + returnValue.set(returnVal); + return false; + } + }); + } + }).rescueRuntime(new Function>() { + @Override + public Future apply(RuntimeException err) { + return trRef.get().onError(err).map(new Function() { + @Override + public Boolean apply(final Transaction tr) { + trRef.set(tr); + return true; + } + }); + } + }); + } + }).map(new Function(){ + @Override + public T apply(Void o) { + trRef.get().dispose(); + return returnValue.get(); + } + }); + } + + @Override + public Future readAsync( + Function> retryable) { + return this.readAsync(retryable, executor); + } + + @Override + public Future readAsync( + Function> retryable, Executor e) { + return this.runAsync(retryable, e); + } + + @Override + public PartialFuture runAsync(final PartialFunction> retryable) { + return this.runAsync(retryable, executor); + } + + @Override + public PartialFuture runAsync(final PartialFunction> retryable, Executor e) { + final AtomicReference trRef = new AtomicReference(createTransaction()); + final AtomicReference returnValue = new AtomicReference(); + return AsyncUtil.whileTrue(new Function>() { + @Override + public PartialFuture apply(Void v) { + PartialFuture process = AsyncUtil.applySafely(retryable, trRef.get()); + + return process.flatMap(new Function>() { + @Override + public Future apply(final T returnVal) { + return trRef.get().commit().map(new Function() { + @Override + public Boolean apply(Void o) { + returnValue.set(returnVal); + return false; + } + }); + } + }).rescue(new Function>() { + @Override + public PartialFuture apply(Exception err) { + return trRef.get().onError(err).map(new Function() { + @Override + public Boolean apply(final Transaction tr) { + trRef.set(tr); + return true; + } + }); + } + }); + } + }).map(new Function(){ + @Override + public T apply(Void o) { + trRef.get().dispose(); + return returnValue.get(); + } + }); + } + + @Override + public PartialFuture readAsync( + PartialFunction> retryable) { + return this.readAsync(retryable, executor); + } + + @Override + public PartialFuture readAsync( + PartialFunction> retryable, Executor e) { + return this.runAsync(retryable, e); + } + + @Override + protected void finalize() throws Throwable { + dispose(); + super.finalize(); + } + + @Override + public Transaction createTransaction() { + return createTransaction(executor); + } + + @Override + public Transaction createTransaction(Executor e) { + pointerReadLock.lock(); + try { + Transaction tr = new FDBTransaction(Database_createTransaction(getPtr()), this, e); + tr.options().setUsedDuringCommitProtectionDisable(); + return tr; + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public void setOption(int code, byte[] value) { + pointerReadLock.lock(); + try { + Database_setOption(getPtr(), code, value); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public Executor getExecutor() { + return executor; + } + + @Override + protected void disposeInternal(long cPtr) { + Database_dispose(cPtr); + } + + private native long Database_createTransaction(long cPtr); + private native void Database_dispose(long cPtr); + private native void Database_setOption(long cPtr, int code, byte[] value) throws FDBException; +} \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FDBTransaction.java b/bindings/java/src/main/com/apple/cie/foundationdb/FDBTransaction.java new file mode 100644 index 0000000000..61163530a1 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FDBTransaction.java @@ -0,0 +1,650 @@ +/* + * FDBTransaction.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + +import com.apple.cie.foundationdb.async.*; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.PartialFunction; +import com.apple.cie.foundationdb.async.PartialFuture; +import com.apple.cie.foundationdb.async.ReadyFuture; + +class FDBTransaction extends DefaultDisposableImpl implements Disposable, Transaction, OptionConsumer { + private final Executor executor; + private final Database database; + private final TransactionOptions options; + + private boolean transactionOwner; + + public final ReadTransaction snapshot; + + class ReadSnapshot implements ReadTransaction { + @Override + public Future getReadVersion() { + return FDBTransaction.this.getReadVersion(); + } + + @Override + public Future get(byte[] key) { + return get_internal(key, true); + } + + @Override + public Future getKey(KeySelector selector) { + return getKey_internal(selector, true); + } + + /////////////////// + // getRange -> KeySelectors + /////////////////// + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse, StreamingMode mode) { + return new RangeQuery(FDBTransaction.this, true, begin, end, limit, reverse, mode); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse) { + return getRange(begin, end, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit) { + return getRange(begin, end, limit, false); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end) { + return getRange(begin, end, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + /////////////////// + // getRange -> byte[]s + /////////////////// + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse, StreamingMode mode) { + return getRange(KeySelector.firstGreaterOrEqual(begin), + KeySelector.firstGreaterOrEqual(end), + limit, reverse, mode); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse) { + return getRange(begin, end, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit) { + return getRange(begin, end, limit, false); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end) { + return getRange(begin, end, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + /////////////////// + // getRange (Range) + /////////////////// + @Override + public AsyncIterable getRange(Range range, + int limit, boolean reverse, StreamingMode mode) { + return getRange(range.begin, range.end, limit, reverse, mode); + } + @Override + public AsyncIterable getRange(Range range, + int limit, boolean reverse) { + return getRange(range, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(Range range, + int limit) { + return getRange(range, limit, false); + } + @Override + public AsyncIterable getRange(Range range) { + return getRange(range, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + @Override + public TransactionOptions options() { + return FDBTransaction.this.options(); + } + + @Override + public T read(Function retryable) { + return retryable.apply(this); + } + + @Override + public T read(PartialFunction retryable) + throws Exception { + return retryable.apply(this); + } + + @Override + public Future readAsync( + Function> retryable) { + return AsyncUtil.applySafely(retryable, this); + } + + @Override + public PartialFuture readAsync( + PartialFunction> retryable) { + return AsyncUtil.applySafely(retryable, this); + } + + @Override + public Executor getExecutor() { + return FDBTransaction.this.getExecutor(); + } + } + + protected FDBTransaction(long cPtr, Database database, Executor executor) { + super(cPtr); + this.database = database; + this.executor = executor; + snapshot = new ReadSnapshot(); + options = new TransactionOptions(this); + transactionOwner = true; + } + + @Override + public ReadTransaction snapshot() { + return snapshot; + } + + @Override + public TransactionOptions options() { + return options; + } + + @Override + public void setReadVersion(long version) { + pointerReadLock.lock(); + try { + Transaction_setVersion(getPtr(), version); + } finally { + pointerReadLock.unlock(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public Future getReadVersion() { + pointerReadLock.lock(); + try { + return new FutureVersion( Transaction_getReadVersion(getPtr()), this.executor ); + } finally { + pointerReadLock.unlock(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public Future get(byte[] key) { + return get_internal(key, false); + } + + private Future get_internal(byte[] key, boolean isSnapshot) { + pointerReadLock.lock(); + try { + return new FutureResult( Transaction_get(getPtr(), key, isSnapshot), this.executor ); + } finally { + pointerReadLock.unlock(); + } + } + + /** + * {@inheritDoc} + */ + @Override + public Future getKey(KeySelector selector) { + return getKey_internal(selector, false); + } + + private Future getKey_internal(KeySelector selector, boolean isSnapshot) { + pointerReadLock.lock(); + try { + return new FutureKey( Transaction_getKey(getPtr(), + selector.getKey(), selector.orEqual(), selector.getOffset(), isSnapshot), this.executor ); + } finally { + pointerReadLock.unlock(); + } + } + + /////////////////// + // getRange -> KeySelectors + /////////////////// + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse, StreamingMode mode) { + return new RangeQuery(this, false, begin, end, limit, reverse, mode); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse) { + return getRange(begin, end, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit) { + return getRange(begin, end, limit, false); + } + @Override + public AsyncIterable getRange(KeySelector begin, KeySelector end) { + return getRange(begin, end, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + /////////////////// + // getRange -> byte[]s + /////////////////// + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse, StreamingMode mode) { + return getRange(KeySelector.firstGreaterOrEqual(begin), + KeySelector.firstGreaterOrEqual(end), + limit, reverse, mode); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse) { + return getRange(begin, end, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit) { + return getRange(begin, end, limit, false); + } + @Override + public AsyncIterable getRange(byte[] begin, byte[] end) { + return getRange(begin, end, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + /////////////////// + // getRange (Range) + /////////////////// + @Override + public AsyncIterable getRange(Range range, + int limit, boolean reverse, StreamingMode mode) { + return getRange(range.begin, range.end, limit, reverse, mode); + } + @Override + public AsyncIterable getRange(Range range, + int limit, boolean reverse) { + return getRange(range, limit, reverse, StreamingMode.ITERATOR); + } + @Override + public AsyncIterable getRange(Range range, + int limit) { + return getRange(range, limit, false); + } + @Override + public AsyncIterable getRange(Range range) { + return getRange(range, ReadTransaction.ROW_LIMIT_UNLIMITED); + } + + @Override + public Database getDatabase() { + return database; + } + + protected FutureResults getRange_internal( + KeySelector begin, KeySelector end, + int rowLimit, int targetBytes, int streamingMode, + int iteration, boolean isSnapshot, boolean reverse) { + pointerReadLock.lock(); + try { + /*System.out.println(String.format( + " -- range get: (%s, %s) limit: %d, bytes: %d, mode: %d, iteration: %d, snap: %s, reverse %s", + begin.toString(), end.toString(), rowLimit, targetBytes, streamingMode, + iteration, Boolean.toString(isSnapshot), Boolean.toString(reverse)));*/ + return new FutureResults(Transaction_getRange( + getPtr(), begin.getKey(), begin.orEqual(), begin.getOffset(), + end.getKey(), end.orEqual(), end.getOffset(), rowLimit, targetBytes, + streamingMode, iteration, isSnapshot, reverse), this.executor); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public void addReadConflictRange(byte[] keyBegin, byte[] keyEnd) { + addConflictRange(keyBegin, keyEnd, ConflictRangeType.READ); + } + + @Override + public void addReadConflictKey(byte[] key) { + addConflictRange(key, ByteArrayUtil.join(key, new byte[]{(byte) 0}), ConflictRangeType.READ); + } + + @Override + public void addWriteConflictRange(byte[] keyBegin, byte[] keyEnd) { + addConflictRange(keyBegin, keyEnd, ConflictRangeType.WRITE); + } + + @Override + public void addWriteConflictKey(byte[] key) { + addConflictRange(key, ByteArrayUtil.join(key, new byte[] { (byte)0 }), ConflictRangeType.WRITE); + } + + private void addConflictRange(byte[] keyBegin, byte[] keyEnd, + ConflictRangeType type) { + pointerReadLock.lock(); + try { + Transaction_addConflictRange(getPtr(), keyBegin, keyEnd, type.code()); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public T run(Function retryable) { + return retryable.apply(this); + } + + @Override + public T run(PartialFunction retryable) throws Exception { + return retryable.apply(this); + } + + @Override + public Future runAsync( + Function> retryable) { + return AsyncUtil.applySafely(retryable, this); + } + + @Override + public PartialFuture runAsync( + PartialFunction> retryable) { + return AsyncUtil.applySafely(retryable, this); + } + + @Override + public T read(Function retryable) { + return retryable.apply(this); + } + + @Override + public T read(PartialFunction retryable) + throws Exception { + return retryable.apply(this); + } + + @Override + public Future readAsync( + Function> retryable) { + return AsyncUtil.applySafely(retryable, this); + } + + @Override + public PartialFuture readAsync( + PartialFunction> retryable) { + return AsyncUtil.applySafely(retryable, this); + } + + @Override + public void set(byte[] key, byte[] value) { + if(key == null || value == null) + throw new IllegalArgumentException("Keys/Values must be non-null"); + pointerReadLock.lock(); + try { + Transaction_set(getPtr(), key, value); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public void clear(byte[] key) { + if(key == null) + throw new IllegalArgumentException("Key cannot be null"); + pointerReadLock.lock(); + try { + Transaction_clear(getPtr(), key); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public void clear(byte[] beginKey, byte[] endKey) { + if(beginKey == null || endKey == null) + throw new IllegalArgumentException("Keys cannot be null"); + pointerReadLock.lock(); + try { + Transaction_clear(getPtr(), beginKey, endKey); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + @Deprecated + public void clearRangeStartsWith(byte[] prefix) { + clear(Range.startsWith(prefix)); + } + + @Override + public void clear(Range range) { + clear(range.begin, range.end); + } + + @Override + public void mutate(MutationType optype, byte[] key, byte[] value) { + pointerReadLock.lock(); + try { + Transaction_mutate(getPtr(), optype.code(), key, value); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public void setOption(int code, byte[] param) { + pointerReadLock.lock(); + try { + Transaction_setOption(getPtr(), code, param); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public Future commit() { + pointerReadLock.lock(); + try { + return new FutureVoid(Transaction_commit(getPtr()), this.executor); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public Long getCommittedVersion() { + pointerReadLock.lock(); + try { + return Transaction_getCommittedVersion(getPtr()); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public Future getVersionstamp() { + pointerReadLock.lock(); + try { + return new FutureKey( Transaction_getVersionstamp(getPtr()), this.executor ); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public Future watch(byte[] key) throws FDBException { + pointerReadLock.lock(); + try { + return new FutureVoid(Transaction_watch(getPtr(), key), this.executor); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public Future onError(RuntimeException e) { + if(!(e instanceof FDBException)) { + return new ReadyFuture(e, getExecutor()); + } + pointerReadLock.lock(); + try { + FutureVoid f = new FutureVoid(Transaction_onError(getPtr(), ((FDBException)e).getCode()), this.executor); + final Transaction tr = transfer(); + return f.map(new Function() { + @Override + public Transaction apply(Void o) { + return tr; + } + }) + .rescueRuntime(new Function>() { + @Override + public Future apply(RuntimeException e) { + tr.dispose(); + throw e; + } + }); + } finally { + pointerReadLock.unlock(); + if(!transactionOwner) { + dispose(); + } + } + } + + @Override + public PartialFuture onError(Exception e) { + if (!(e instanceof RuntimeException)) + return new ReadyPartialFuture(e, getExecutor()); + + return onError((RuntimeException)e); + } + + @Override + public Transaction reset() { + pointerReadLock.lock(); + try { + Transaction_reset(getPtr()); + return transfer(); + } finally { + pointerReadLock.unlock(); + if(!transactionOwner) { + dispose(); + } + } + } + + @Override + public void cancel() { + pointerReadLock.lock(); + try { + Transaction_cancel(getPtr()); + } finally { + pointerReadLock.unlock(); + } + } + + public Future getAddressesForKey(byte[] key) { + pointerReadLock.lock(); + try { + return new FutureStrings(Transaction_getKeyLocations(getPtr(), key), this.executor); + } finally { + pointerReadLock.unlock(); + } + } + + @Override + public Executor getExecutor() { + return executor; + } + + // Must hold pointerReadLock when calling + private FDBTransaction transfer() { + FDBTransaction tr = new FDBTransaction(getPtr(), database, executor); + tr.options().setUsedDuringCommitProtectionDisable(); + transactionOwner = false; + return tr; + } + + @Override + protected long getPtr() { + if(!transactionOwner) { + throw new IllegalStateException("Transaction has been invalidated by reset"); + } + else { + return super.getPtr(); + } + } + + @Override + protected void finalize() throws Throwable { + dispose(); + } + + @Override + protected void disposeInternal(long cPtr) { + if(transactionOwner) { + Transaction_dispose(cPtr); + } + } + + private native long Transaction_getReadVersion(long cPtr); + private native void Transaction_setVersion(long cPtr, long version); + private native long Transaction_get(long cPtr, byte[] key, boolean isSnapshot); + private native long Transaction_getKey(long cPtr, byte[] key, boolean orEqual, + int offset, boolean isSnapshot); + private native long Transaction_getRange(long cPtr, + byte[] keyBegin, boolean orEqualBegin, int offsetBegin, + byte[] keyEnd, boolean orEqualEnd, int offsetEnd, + int rowLimit, int targetBytes, int streamingMode, int iteration, + boolean isSnapshot, boolean reverse); + private native void Transaction_addConflictRange(long cPtr, + byte[] keyBegin, byte[] keyEnd, int conflictRangeType); + private native void Transaction_set(long cPtr, byte[] key, byte[] value); + private native void Transaction_clear(long cPtr, byte[] key); + private native void Transaction_clear(long cPtr, byte[] beginKey, byte[] endKey); + private native void Transaction_mutate(long ptr, int code, byte[] key, byte[] value); + private native void Transaction_setOption(long cPtr, int code, byte[] value) throws FDBException; + private native long Transaction_commit(long cPtr); + private native long Transaction_getCommittedVersion(long cPtr); + private native long Transaction_getVersionstamp(long cPtr); + private native long Transaction_onError(long cPtr, int errorCode); + private native void Transaction_dispose(long cPtr); + private native void Transaction_reset(long cPtr); + private native long Transaction_watch(long ptr, byte[] key) throws FDBException; + private native void Transaction_cancel(long cPtr); + private native long Transaction_getKeyLocations(long cPtr, byte[] key); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FutureCluster.java b/bindings/java/src/main/com/apple/cie/foundationdb/FutureCluster.java new file mode 100644 index 0000000000..08cd8e44dd --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FutureCluster.java @@ -0,0 +1,40 @@ +/* + * FutureCluster.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + + +class FutureCluster extends NativeFuture { + private Executor executor; + + protected FutureCluster(long cPtr, Executor e) { + super(cPtr, e); + this.executor = e; + } + + @Override + public Cluster getIfDone_internal() throws FDBException { + return new Cluster(FutureCluster_get(cPtr), executor); + } + + private native long FutureCluster_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FutureDatabase.java b/bindings/java/src/main/com/apple/cie/foundationdb/FutureDatabase.java new file mode 100644 index 0000000000..67bcedc6f0 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FutureDatabase.java @@ -0,0 +1,41 @@ +/* + * FutureDatabase.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + + + +class FutureDatabase extends NativeFuture { + private Executor executor; + + FutureDatabase(long cPtr, Executor e) { + super(cPtr, e); + this.executor = e; + } + + @Override + public Database getIfDone_internal() throws FDBException { + return new FDBDatabase(FutureDatabase_get(cPtr), executor); + } + + private native long FutureDatabase_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FutureKey.java b/bindings/java/src/main/com/apple/cie/foundationdb/FutureKey.java new file mode 100644 index 0000000000..c0cf900694 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FutureKey.java @@ -0,0 +1,37 @@ +/* + * FutureKey.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + + +class FutureKey extends NativeFuture { + FutureKey(long cPtr, Executor e) { + super(cPtr, e); + } + + @Override + public byte[] getIfDone_internal() throws FDBException { + return FutureKey_get(cPtr); + } + + private native byte[] FutureKey_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FutureResult.java b/bindings/java/src/main/com/apple/cie/foundationdb/FutureResult.java new file mode 100644 index 0000000000..fa9356059c --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FutureResult.java @@ -0,0 +1,36 @@ +/* + * FutureResult.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + +class FutureResult extends NativeFuture { + FutureResult(long cPtr, Executor e) { + super(cPtr, e); + } + + @Override + public byte[] getIfDone_internal() throws FDBException { + return FutureResult_get(cPtr); + } + + private native byte[] FutureResult_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FutureResults.java b/bindings/java/src/main/com/apple/cie/foundationdb/FutureResults.java new file mode 100644 index 0000000000..c1eef996d6 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FutureResults.java @@ -0,0 +1,41 @@ +/* + * FutureResults.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + +class FutureResults extends NativeFuture { + FutureResults(long cPtr, Executor e) { + super(cPtr, e); + } + + @Override + public RangeResult getIfDone_internal() throws FDBException { + return FutureResults_get(cPtr); + } + + public RangeResultSummary getSummaryIfDone() throws FDBException { + return FutureResults_getSummary(cPtr); + } + + private native RangeResultSummary FutureResults_getSummary(long ptr) throws FDBException; + private native RangeResult FutureResults_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FutureStrings.java b/bindings/java/src/main/com/apple/cie/foundationdb/FutureStrings.java new file mode 100644 index 0000000000..2e934b12c1 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FutureStrings.java @@ -0,0 +1,36 @@ +/* + * FutureStrings.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + +class FutureStrings extends NativeFuture { + FutureStrings(long cPtr, Executor e) { + super(cPtr, e); + } + + @Override + public String[] getIfDone_internal() throws FDBException { + return FutureStrings_get(cPtr); + } + + private native String[] FutureStrings_get(long ptr) throws FDBException; +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FutureVersion.java b/bindings/java/src/main/com/apple/cie/foundationdb/FutureVersion.java new file mode 100644 index 0000000000..33591e6430 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FutureVersion.java @@ -0,0 +1,36 @@ +/* + * FutureVersion.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + +class FutureVersion extends NativeFuture { + FutureVersion(long cPtr, Executor e) { + super(cPtr, e); + } + + @Override + Long getIfDone_internal() throws FDBException { + return FutureVersion_get(cPtr); + } + + private native long FutureVersion_get(long cPtr) throws FDBException; +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/FutureVoid.java b/bindings/java/src/main/com/apple/cie/foundationdb/FutureVoid.java new file mode 100644 index 0000000000..9ce80d76a0 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/FutureVoid.java @@ -0,0 +1,41 @@ +/* + * FutureVoid.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.concurrent.Executor; + +class FutureVoid extends NativeFuture { + FutureVoid(long cPtr, Executor e) { + super(cPtr, e); + } + + @Override + public Void getIfDone_internal() throws FDBException { + // With "future-cleanup" we get rid of FutureVoid_get and replace instead + // with a get on the error and throw if the error is not success. + FDBException err = Future_getError(cPtr); + + if(!err.isSuccess()) { + throw err; + } + return null; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/JNIUtil.java b/bindings/java/src/main/com/apple/cie/foundationdb/JNIUtil.java new file mode 100644 index 0000000000..fd310c7df8 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/JNIUtil.java @@ -0,0 +1,182 @@ +/* + * JNIUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * Utility for loading a dynamic library from the classpath. + * + */ +class JNIUtil { + private static final String SEPARATOR = "/"; + private static final String LOADABLE_PREFIX = "FDB_LIBRARY_PATH_"; + private static final String TEMPFILE_PREFIX = "fdbjni"; + private static final String TEMPFILE_SUFFIX = ".library"; + + private enum OS { + WIN32("windows", "amd64", false), + LINUX("linux", "amd64", true), + OSX("osx", "x86_64", true); + + private final String name; + private final String arch; + private final boolean canDeleteEager; + + OS(String name, String arch, boolean canDeleteEager) { + this.name = name; + this.arch = arch; + this.canDeleteEager = canDeleteEager; + } + + public String getName() { + return this.name; + } + + public String getArch() { + return this.arch; + } + } + + /** + * Attempts a platform specific load of a library using classpath resources. In + * the case that the load fails, a call to {@link System#loadLibrary(String)} + * will be made as a way to use a library not included in a jar. + * + * @param libName the name of the library to attempt to load. This name should be + * undecorated with file extensions and, in the case of *nix, "lib" prefixes. + */ + static void loadLibrary(String libName) throws UnsatisfiedLinkError { + if(libName == null) { + throw new NullPointerException("Library name must not be null"); + } + + String libPathToLoad = null; + try { + String prop = LOADABLE_PREFIX + libName.toUpperCase(); + libPathToLoad = System.getProperty(prop); + } catch(SecurityException e) { + // eat + } + + if(libPathToLoad != null) { + System.load(libPathToLoad); + return; + } + + OS os = getRunningOS(); + String path = getPath(os, libName); + + if ((os.getName().equals("linux") && !path.endsWith(".so")) || (os.getName().equals("windows") && !path.endsWith(".dll")) || (os.getName().equals("osx") && !path.endsWith(".jnilib") && !path.endsWith(".dylib"))) { + throw new IllegalStateException("OS sanity check failed. System property os.name reports " + os.getName()+" but System.mapLibraryName is looking for " + getLibName(libName)); + } + + File exported; + + try { + exported = exportResource(path); + } + catch (IOException e) { + throw new UnsatisfiedLinkError(e.getMessage()); + } + String filename = exported.getAbsolutePath(); + + System.load(filename); + if(os.canDeleteEager) { + try { + exported.delete(); + } catch(Throwable t) { + // EAT, since we do not care that an eager deletion did not work... + } + } + } + + /** + * Gets a relative path for a library. The path will be of the form: + * {@code {os}/{arch}/{name}}. + * + * @return a relative path to a resource to be loaded from the classpath + */ + private static String getPath(OS os, String libName) { + return SEPARATOR + + "lib" + SEPARATOR + + os.getName() + SEPARATOR + + os.getArch() + SEPARATOR + + getLibName(libName); + } + + /** + * Export a resource from the classpath to a temporary file. + * + * @param path the relative path of the file to load from the classpath + * + * @return the absolute path to the exported file + * @throws IOException + */ + private static File exportResource(String path) throws IOException { + InputStream resource = JNIUtil.class.getResourceAsStream(path); + if(resource == null) + throw new IllegalStateException("Embedded library jar:" + path + " not found"); + File f = saveStreamAsTempFile(resource); + return f; + } + + private static File saveStreamAsTempFile(InputStream resource) throws IOException { + File f = File.createTempFile(TEMPFILE_PREFIX, TEMPFILE_SUFFIX); + FileOutputStream outputStream = new FileOutputStream(f); + copyStream(resource, outputStream); + outputStream.flush(); + outputStream.close(); + f.deleteOnExit(); + return f; + } + + private static void copyStream(InputStream resource, OutputStream fileOutputStream) throws IOException { + byte[] buffer = new byte[4096]; + int bytesRead; + while((bytesRead = resource.read(buffer)) > 0) { + fileOutputStream.write(buffer, 0, bytesRead); + } + } + + private static String getLibName(String libName) { + String systemLibName = System.mapLibraryName(libName); + if (systemLibName.endsWith(".dylib")) { + systemLibName = systemLibName.replace("dylib", "jnilib"); + } + return systemLibName; + } + + private static OS getRunningOS() { + String osname = System.getProperty("os.name").toLowerCase(); + if(osname.startsWith("windows")) + return OS.WIN32; + if(osname.startsWith("linux")) + return OS.LINUX; + if(osname.startsWith("mac") || osname.startsWith("darwin")) + return OS.OSX; + throw new IllegalStateException("Unknown or unsupported OS: " + osname); + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/KeySelector.java b/bindings/java/src/main/com/apple/cie/foundationdb/KeySelector.java new file mode 100644 index 0000000000..31429c7639 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/KeySelector.java @@ -0,0 +1,165 @@ +/* + * KeySelector.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +/** + * A {@code KeySelector} identifies a particular key in the database. FoundationDB's + * lexicographically ordered data model permits finding keys based on their order (for + * example, finding the first key in the database greater than a given key). Key selectors + * represent a description of a key in the database that could be resolved to an actual + * key by {@code Transaction}'s {@link Transaction#getKey(KeySelector) getKey()} + * or used directly as the beginning or end of a range in {@code Transaction}'s + * {@link Transaction#getRange(KeySelector, KeySelector) getRange()}.
    + *
    + * For more about how key selectors work in practice, see + * the KeySelector documentation. + *
    + *
    + * Generally one of the following static methods should be used to construct a {@code KeySelector}: + *
    • {@link #lastLessThan(byte[]) lastLessThan}
    • + *
    • {@link #lastLessOrEqual(byte[]) lastLessOrEqual}
    • + *
    • {@link #firstGreaterThan(byte[]) firstGreaterThan}
    • + *
    • {@link #firstGreaterOrEqual(byte[]) firstGreaterOrEqual}
    + *
    + * This is an immutable class. The {@code add(int)} call does not + * modify internal state, but returns a new instance. + *
    + */ +public class KeySelector { + private final byte[] key; + private final boolean orEqual; + private final int offset; + + /** + * Constructs a new {@code KeySelector} from the given parameters. Client code + * will not generally call this constructor. + * + * @param key the base key to reference + * @param orEqual true if the key should be considered for equality + * @param offset the number of keys to offset from once the key is found + */ + public KeySelector(byte[] key, boolean orEqual,int offset) { + this.key = key; + this.orEqual = orEqual; + this.offset = offset; + } + + /** + * Creates a {@code KeySelector} that picks the last key less than the parameter + * + * @param key the key to use as the edge of the edge of selection criteria + * + * @return a newly created {@code KeySelector} + */ + public static KeySelector lastLessThan( byte[] key ) { + return new KeySelector( key, false, 0 ); + } + + /** + * Creates a {@code KeySelector} that picks the last key less than or equal to the parameter + * + * @param key the key to use as the edge of the edge of selection criteria + * + * @return a newly created {@code KeySelector} + */ + public static KeySelector lastLessOrEqual( byte[] key ) { + return new KeySelector( key, true, 0 ); + } + + /** + * Creates a {@code KeySelector} that picks the first key greater than the parameter + * + * @param key the key to use as the edge of the edge of selection criteria + * + * @return a newly created {@code KeySelector} + */ + public static KeySelector firstGreaterThan( byte[] key ) { + return new KeySelector( key, true, +1 ); + } + + /** + * Creates a {@code KeySelector} that picks the first key greater than or equal to the parameter + * + * @param key the key to use as the edge of the edge of selection criteria + * + * @return a newly created {@code KeySelector} + */ + public static KeySelector firstGreaterOrEqual( byte[] key ) { + return new KeySelector( key, false, +1 ); + } + + /** + * Returns a new {@code KeySelector} offset by a given + * number of keys from this one. For example, an offset of {@code 1} means + * that the new {@code KeySelector} specifies the key in the database + * after the key selected by this {@code KeySelector}. The offset can be negative; + * these will move the selector to previous keys in the database.
    + *
    + * Note that large offsets take time O(offset) to resolve, making them a + * poor choice for iterating through a large range. (Instead, use the keys + * returned from a range query operation + * themselves to create a new beginning {@code KeySelector}.) For more information see + * the KeySelector documentation. + * + * @param offset the number of keys to offset the {@code KeySelector}. This number can be + * negative. + * + * @return a newly created {@code KeySelector} that is offset by a number of keys. + */ + public KeySelector add( int offset ) { + return new KeySelector( getKey(), orEqual(), getOffset() + offset ); + } + + /** + * Returns a copy of the key that serves as the anchor for this {@code KeySelector}. This is + * not the key to which this {@code KeySelector} would resolve to. For this + * function see {@link ReadTransaction#getKey(KeySelector)}. + * + * @return a copy of the "anchor" key for this {@code KeySelector}. + */ + public byte[] getKey() { + byte[] res = new byte[key.length]; + System.arraycopy(key, 0, res, 0, key.length); + return res; + } + + @Override + public String toString() { + return String.format("(%s, %s, %d)", + ByteArrayUtil.printable(getKey()), Boolean.toString(orEqual()), getOffset()); + } + + /** + * Returns the {@code or-equal} parameter of this {@code KeySelector}. For internal use. + */ + boolean orEqual() { + return orEqual; + } + + /** + * Returns the key offset for this {@code KeySelector}. For internal use. + */ + public int getOffset() { + return offset; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/KeyValue.java b/bindings/java/src/main/com/apple/cie/foundationdb/KeyValue.java new file mode 100644 index 0000000000..be034be2a5 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/KeyValue.java @@ -0,0 +1,80 @@ +/* + * KeyValue.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.Arrays; + +/** + * A key/value pair. Range read operation on FoundationDB return {@code KeyValue}s. + * This is a simple value type; mutating it won't affect your {@code Transaction} or + * the {@code Database}. + * + */ +public class KeyValue { + private final byte[] key, value; + + /** + * Constructs a new {@code KeyValue} from the specified key and value. + * + * @param key the key portion of the pair + * @param value the value portion of the pair + */ + public KeyValue(byte[] key, byte[] value) { + this.key = key; + this.value = value; + } + + /** + * Gets the key from the pair. + * + * @return the key + */ + public byte[] getKey() { + return this.key; + } + + /** + * Gets the value from the pair. + * + * @return the value + */ + public byte[] getValue() { + return this.value; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + if (obj == this) + return true; + if (!(obj instanceof KeyValue)) + return false; + + KeyValue rhs = (KeyValue) obj; + return Arrays.equals(key, rhs.key) && Arrays.equals(value, rhs.value); + } + + @Override + public int hashCode() { + return 17 + (37 * Arrays.hashCode(key) + Arrays.hashCode(value)); + } +} \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/LocalityUtil.java b/bindings/java/src/main/com/apple/cie/foundationdb/LocalityUtil.java new file mode 100644 index 0000000000..66394517c9 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/LocalityUtil.java @@ -0,0 +1,237 @@ +/* + * LocalityUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.List; + +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncIterator; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.ReadyFuture; + +/** + * The FoundationDB API comes with a set of functions for discovering the + * storage locations of keys within your cluster. This information can be useful + * for advanced users who wish to take into account the location of keys in the + * design of applications or processes. + * + */ +public class LocalityUtil { + /** + * Returns a {@code AsyncIterable} of keys {@code k} such that + * {@code begin <= k < end} and {@code k} is located at the start of a + * contiguous range stored on a single server.
    + *
    + * This method is not transactional. The returned boundaries are an estimate + * and may not represent the exact boundary locations at any database version. + * + * @param db the database to query + * @param begin the inclusive start of the range + * @param end the exclusive end of the range + * + * @return an sequence of keys denoting the start of single-server ranges + */ + public static AsyncIterable getBoundaryKeys(Database db, byte[] begin, byte[] end) { + return getBoundaryKeys_internal(db.createTransaction(), begin, end); + } + + /** + * Returns a {@code AsyncIterable} of keys {@code k} such that + * {@code begin <= k < end} and {@code k} is located at the start of a + * contiguous range stored on a single server.
    + *
    + * This method is not transactional. The returned boundaries + * are an estimate and may not represent the exact boundary locations at + * any database version. The passed {@code Transaction} is not used + * for reads directly, instead it is used to get access to associated + * {@link Database}. As a result, options (such as retry limit) set on the + * passed {@code Transaction} will not be applied. If, however, the passed + * {@code Transaction} has already gotten a read version there is some + * latency advantage to using this form of the method. Also, if the database + * is unavailable prior to the function call, any timeout set on the + * passed {@code Transaction} will still trigger. + * + * @param tr the transaction on which to base the query + * @param begin the inclusive start of the range + * @param end the exclusive end of the range + * + * @return an sequence of keys denoting the start of single-server ranges + */ + public static AsyncIterable getBoundaryKeys(Transaction tr, byte[] begin, byte[] end) { + Transaction local = tr.getDatabase().createTransaction(); + Future readVersion = tr.getReadVersion(); + if(readVersion.isDone() && !readVersion.isError()) { + local.setReadVersion(readVersion.get()); + } + return new BoundaryIterable(local, begin, end); + } + + /** + * Returns a list of public network addresses as strings, one for each of + * the storage servers responsible for storing {@code key} and its associated + * value. + * + * If locality information is not available, the returned future will carry a + * {@link FDBException} locality_information_unavailable. + * + * @param tr the transaction in which to gather location information + * @param key the key for which to gather location information + * + * @return a list of addresses in string form + */ + public static Future getAddressesForKey(Transaction tr, byte[] key) { + if (!(tr instanceof FDBTransaction)) + return new ReadyFuture( new FDBException("locality_information_unavailable", 1033) ); + return ((FDBTransaction)tr).getAddressesForKey(key); + } + + private static AsyncIterable getBoundaryKeys_internal(Transaction tr, byte[] begin, byte[] end) { + return new BoundaryIterable(tr, begin, end); + } + + static class BoundaryIterable implements AsyncIterable { + final Transaction tr; + final byte[] begin; + final byte[] end; + final AsyncIterable firstGet; + + public BoundaryIterable(Transaction tr, byte[] begin, byte[] end) { + this.tr = tr; + this.begin = Arrays.copyOf(begin, begin.length); + this.end = Arrays.copyOf(end, end.length); + + tr.options().setReadSystemKeys(); + tr.options().setLockAware(); + firstGet = tr.getRange(keyServersForKey(begin), keyServersForKey(end)); + } + + @Override + public AsyncIterator iterator() { + return new BoundaryIterator(); + } + + @Override + public Future> asList() { + return AsyncUtil.collect(this); + } + + class BoundaryIterator implements AsyncIterator { + AsyncIterator block = BoundaryIterable.this.firstGet.iterator(); + Transaction tr = BoundaryIterable.this.tr; + byte[] begin = BoundaryIterable.this.begin; + byte[] lastBegin = begin; + private Future nextFuture; + + public BoundaryIterator() { + nextFuture = block.onHasNext().rescueRuntime(handler); + } + + @Override + public Future onHasNext() { + return nextFuture; + } + + @Override + public boolean hasNext() { + return nextFuture.get(); + } + + Future restartGet() { + if(ByteArrayUtil.compareUnsigned(begin, end) >= 0) { + return new ReadyFuture(Boolean.FALSE); + } + lastBegin = begin; + tr.options().setReadSystemKeys(); + block = tr.getRange( + keyServersForKey(begin), + keyServersForKey(end)).iterator(); + nextFuture = block.onHasNext().rescueRuntime(handler); + return nextFuture; + } + + Function> handler = new Function>() { + @Override + public Future apply(RuntimeException o) { + if(o instanceof FDBException) { + FDBException err = (FDBException) o; + if(err.getCode() == 1007 && !Arrays.equals(begin, lastBegin)) { + BoundaryIterator.this.tr.dispose(); + BoundaryIterator.this.tr = + BoundaryIterator.this.tr.getDatabase().createTransaction(); + return restartGet(); + } + } + + Future onError = BoundaryIterator.this.tr.onError(o); + return onError.flatMap(new Function>() { + @Override + public Future apply(Transaction tr) { + BoundaryIterator.this.tr = tr; + return restartGet(); + } + }); + } + }; + + @Override + public byte[] next() { + if(!nextFuture.isDone()) { + throw new IllegalStateException("Call to next without hasNext()=true"); + } + KeyValue o = block.next(); + byte[] key = o.getKey(); + byte[] suffix = Arrays.copyOfRange(key, 13, key.length); + BoundaryIterator.this.begin = ByteArrayUtil.join(suffix, new byte[] { (byte)0 }); + nextFuture = block.onHasNext().rescueRuntime(handler); + return suffix; + } + + @Override + public void remove() { + throw new UnsupportedOperationException("Boundary keys are read-only"); + } + + @Override + public void cancel() { + // TODO Auto-generated method stub + } + + @Override + public void dispose() { + BoundaryIterator.this.tr.dispose(); + } + } + } + + static Charset ASCII = Charset.forName("US-ASCII"); + static byte[] keyServersForKey(byte[] key) { + return ByteArrayUtil.join(new byte[] { (byte)255 }, + "/keyServers/".getBytes(ASCII), + key); + } + + private LocalityUtil() {} +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/NativeFuture.java b/bindings/java/src/main/com/apple/cie/foundationdb/NativeFuture.java new file mode 100644 index 0000000000..577d39d7f1 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/NativeFuture.java @@ -0,0 +1,185 @@ +/* + * NativeFuture.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.io.IOException; +import java.nio.channels.AsynchronousCloseException; +import java.nio.channels.spi.AbstractInterruptibleChannel; +import java.util.concurrent.Executor; + +import com.apple.cie.foundationdb.async.AbstractFuture; + +abstract class NativeFuture extends AbstractFuture { + protected final long cPtr; + + private final Object valueLock = new Object(); + private boolean valueSet = false; + private T value; + + protected NativeFuture(long cPtr, Executor e) { + super(e); + this.cPtr = cPtr; + } + + @Override + protected void registerSingleCallback(Runnable callback) { + if(isDone()) { + callback.run(); + } else { + Future_registerCallback(cPtr, callback); + } + } + + + @Override + public void blockInterruptibly() throws InterruptedException { + if(this.isDone()) + return; + blockChannel.blockUntilReady(); + } + + /** + * Blocks until this {@code Future} is set to either a value or an error. + * @throws FDBException + */ + @Override + public void blockUntilReady() { + NativeFuture.this.Future_blockUntilReady(cPtr); + } + + /** + * Gets the readiness of this {@code Future}. A {@code Future} is ready + * if the value has been set or set to an error. + * + * @return true if the {@code Future} is set to a value or error + */ + @Override + public boolean isDone() { + return Future_isReady(cPtr); + } + + @Override + public boolean isError() { + int code = Future_getError(cPtr).getCode(); + if(code==1102) throw new RuntimeException(""); + // is an error if is not success, future_not_set, or future_released + return code != 0 && code != 2015 && code != 1102; + } + + @Override + public FDBException getError() { + FDBException err = Future_getError(cPtr); + + // If this is not an error + int code = err.getCode(); + if(code == 2015) // future_not_set + throw new IllegalStateException("Future not ready"); + if(code == 1102 || code == 0) // future_released, success + throw new IllegalStateException("Future set to value, not error"); + + return err; + } + + @Override + protected T getIfDone() { + try { + T val = getIfDone_internal(); + synchronized (valueLock) { + this.value = val; + this.valueSet = true; + } + Future_releaseMemory(cPtr); + } catch(FDBException t) { + if(t.getCode() == 2015) { // future_not_set + throw new IllegalStateException("Future not ready"); + } + if(t.getCode() != 1102) { // future_released + throw t; + } + } + + synchronized (valueLock) { + // For us to get here with no value set that means that + // someone else called releaseMemory without also first + // setting "value". The only way this could happen is + // through a call to dispose(). + if(!valueSet) + throw new IllegalStateException("Future value accessed after disposal"); + return this.value; + } + } + + abstract T getIfDone_internal() throws FDBException; + + @Override + public void cancel() { + Future_cancel(cPtr); + } + + @Override + public void dispose() { + Future_releaseMemory(cPtr); + synchronized (valueLock) { + this.value = null; + this.valueSet = false; + } + } + + @Override + protected void finalize() throws Throwable { + Future_dispose(cPtr); + } + + FutureChannel blockChannel = new FutureChannel(); + + private native void Future_registerCallback(long cPtr, Runnable callback); + private native void Future_blockUntilReady(long cPtr); + private native boolean Future_isReady(long cPtr); + private native void Future_dispose(long cPtr); + private native void Future_cancel(long cPtr); + private native void Future_releaseMemory(long cPtr); + + // Used by FutureVoid + protected native FDBException Future_getError(long cPtr); + + private final class FutureChannel extends AbstractInterruptibleChannel { + @Override + protected void implCloseChannel() throws IOException { + NativeFuture.this.cancel(); + } + + void blockUntilReady() throws InterruptedException { + boolean completed = false; + try { + begin(); + NativeFuture.this.Future_blockUntilReady(cPtr); // Perform blocking I/O operation + completed = true; + } finally { + try { + end(completed); + } catch (AsynchronousCloseException e) { + throw new InterruptedException(); + } + } + } + } + +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/OptionConsumer.java b/bindings/java/src/main/com/apple/cie/foundationdb/OptionConsumer.java new file mode 100644 index 0000000000..4d0c398f4d --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/OptionConsumer.java @@ -0,0 +1,37 @@ +/* + * OptionConsumer.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +/** + * An object on which encoded options can be set. Rarely used outside of + * internal implementation. + */ +public interface OptionConsumer { + /** + * Attempt to set the given option. The parameter interpretation is completely + * dependent on the option code. Normally this interface should not be used + * from outside this package. + * + * @param code the encoded parameter to set + * @param parameter the value, the range of which is dependent on the parameter {@code code} + */ + public void setOption(int code, byte[] parameter); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/OptionsSet.java b/bindings/java/src/main/com/apple/cie/foundationdb/OptionsSet.java new file mode 100644 index 0000000000..d5a8b0d702 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/OptionsSet.java @@ -0,0 +1,62 @@ +/* + * OptionsSet.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.Charset; + +abstract class OptionsSet { + private final static Charset CHARSET_UTF8 = Charset.forName("UTF-8"); + OptionConsumer consumer; + + public OptionsSet(OptionConsumer provider) { + this.consumer = provider; + } + + /** + * Returns the object on which these options are being set. Rarely used by + * client code, since all options should be top-level methods on extending + * classes. + * + * @return target of option set calls + */ + public OptionConsumer getOptionConsumer() { return consumer; } + + protected void setOption(int code) { + consumer.setOption(code, null); + } + + protected void setOption(int code, byte[] param) { + consumer.setOption(code, param); + } + + protected void setOption(int code, String param) { + consumer.setOption(code, param.getBytes(CHARSET_UTF8)); + } + + protected void setOption(int code, long param) { + ByteBuffer b = ByteBuffer.allocate(8); + b.order(ByteOrder.LITTLE_ENDIAN); + b.putLong(param); + consumer.setOption(code, b.array() ); + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/Range.java b/bindings/java/src/main/com/apple/cie/foundationdb/Range.java new file mode 100644 index 0000000000..6a0b091f8d --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/Range.java @@ -0,0 +1,119 @@ +/* + * Range.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.Arrays; + +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +/** + * A simple description of an exact range of keyspace, specified by a begin and end key. As with + * all FoundationDB APIs, {@code begin} is inclusive, {@code end} exclusive. + * + */ +public class Range { + /** + * The beginning of the range. This constraint on the range is inclusive. + */ + public final byte[] begin; + + /** + * The end of the range. This constraint on the range is exclusive. + */ + public final byte[] end; + + /** + * Construct a new {@code Range} with an inclusive begin key and an exclusive + * end key. + * @param begin the inclusive beginning of the range. + * @param end the exclusive end of the range. + */ + public Range(byte[] begin, byte[] end) { + this.begin = begin; + this.end = end; + } + + /** + * Returns a {@code Range} that describes all possible keys that are prefixed with a + * specified key. Use the result of this call as an input to + * {@link Transaction#getRange(Range)} to replicate the now-removed call + * {@code Transaction.getRangeStartsWith(k)}. + * + * @param prefix the key prefixing the range, must not be {@code null} + * + * @return the range of keys starting with {@code prefix} + */ + public static Range startsWith(byte[] prefix) { + if(prefix == null) + throw new NullPointerException("prefix cannot be null"); + return new Range( prefix, ByteArrayUtil.strinc(prefix) ); + } + + /** + * Returns {@code true} if the given {@link Object} is a {@code Range} + * object that refers to the same key range within the keyspace. + * This will be true if the given range has the same {@link #begin} + * and {@link #end} key. This will return {@code false} if the given + * {@link Object} is not a {@code Range} instance. + * + * @param o the {@link Object} to check for equality + * + * @return whether the given {@link Object} matches this {@code Range} + */ + @Override + public boolean equals(Object o) { + if(this == o) { + return true; + } + else if(o == null || !(o instanceof Range)) { + return false; + } + else { + Range that = (Range)o; + return Arrays.equals(this.begin, that.begin) && Arrays.equals(this.end, that.end); + } + } + + /** + * Computes a hash code from the {@link #begin} and {@link #end} keys of + * this {@code Range}. In particular, it will take the bitwise XOR of the + * hash of {@link #begin} with 37 times the hash of {@link #end}. + * + * @return hash code derived from the hashes of {@link #begin} and {@link #end} + */ + @Override + public int hashCode() { + return Arrays.hashCode(begin) ^ (37 * Arrays.hashCode(end)); + } + + /** + * Returns a human-readable {@link String} representation of this {@code Range}. + * It will contain human-readable representations of both the {@link #begin} + * and {@link #end} keys. + * + * @return a human-readable representation of this {@code Range} + */ + @Override + public String toString() { + return "Range(" + (begin == null ? "null" : "\"" + ByteArrayUtil.printable(begin) + "\"") + + ", " + (end == null ? "null" : "\"" + ByteArrayUtil.printable(end) + "\"") + ")"; + } +} \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/RangeQuery.java b/bindings/java/src/main/com/apple/cie/foundationdb/RangeQuery.java new file mode 100644 index 0000000000..d09a863fa7 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/RangeQuery.java @@ -0,0 +1,355 @@ +/* + * RangeQuery.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.List; +import java.util.NoSuchElementException; +import java.util.concurrent.CancellationException; + +import com.apple.cie.foundationdb.async.*; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; + +/** + * Represents a query against FoundationDB for a range of keys. The + * result of this query can be iterated over in a blocking fashion with a call to + * {@link #iterator()} (as specified by {@link Iterable}). + * If the calling program uses an asynchronous paradigm, a non-blocking + * {@link AsyncIterator} is returned from {@link #asyncIterator()}. Both of these + * constructions will not begin to query the database until the first call to + * {@code hasNext()}. As the query uses its {@link Transaction} of origin to fetch + * all the data, the use of this query object must not span more than a few seconds. + * + *

    NOTE: although resulting {@code Iterator}s do support the {@code remove()} + * operation, the remove is not durable until {@code commit()} on the {@code Transaction} + * that yielded this query returns true. + */ +class RangeQuery implements AsyncIterable, Iterable { + private final FDBTransaction tr; + private final KeySelector begin; + private final KeySelector end; + private final boolean snapshot; + private final int rowLimit; + private final boolean reverse; + private final StreamingMode streamingMode; + private final FutureResults firstChunk; + + RangeQuery(FDBTransaction transaction, boolean isSnapshot, + KeySelector begin, KeySelector end, int rowLimit, + boolean reverse, StreamingMode streamingMode) { + this.tr = transaction; + this.begin = begin; + this.end = end; + this.snapshot = isSnapshot; + this.rowLimit = rowLimit; + this.reverse = reverse; + this.streamingMode = streamingMode; + + // start the first fetch... + firstChunk = tr.getRange_internal(begin, end, + rowLimit, 0, streamingMode.code(), 1, snapshot, reverse); + } + + /** + * Returns all the results from the range requested as a {@code List}. If there were no + * limits on the original query and there is a large amount of data in the database + * this call could use a very large amount of memory. + * + * @return a {@code Future} that will be set to the contents of the database + * constrained by the query parameters. + */ + @Override + public Future> asList() { + StreamingMode mode = this.streamingMode; + if(mode == StreamingMode.ITERATOR) + mode = (this.rowLimit == 0) ? StreamingMode.WANT_ALL : StreamingMode.EXACT; + + // if the streaming mode is EXACT, try and grab things as one chunk + if(mode == StreamingMode.EXACT) { + Future range = tr.getRange_internal( + this.begin, this.end, this.rowLimit, 0, StreamingMode.EXACT.code(), + 1, this.snapshot, this.reverse); + return range.map(new Function>() { + @Override + public List apply(RangeResult o) { + return o.values; + } + }); + } + + // If the streaming mode is not EXACT, simply collect the results of an iteration into a list + return AsyncUtil.collect( + new RangeQuery(tr, snapshot, begin, end, rowLimit, reverse, mode)); + } + + /** + * Returns an {@code Iterator} over the results of this query against FoundationDB. + * + * @return an {@code Iterator} over type {@code KeyValue}. + */ + @Override + public AsyncRangeIterator iterator() { + return new AsyncRangeIterator(this.rowLimit, this.reverse, this.streamingMode); + } + + private class AsyncRangeIterator implements AsyncIterator { + // immutable aspects of this iterator + private final boolean rowsLimited; + private final boolean reverse; + private final StreamingMode streamingMode; + + // There is the chance for parallelism in the two "chunks" for fetched data + private RangeResult chunk = null; + private RangeResult nextChunk = null; + private boolean fetchOutstanding = true; + private byte[] prevKey = null; + private int index = 0; + // The first request is made in the constructor for the parent Iterable, so start at 1 + private int iteration = 1; + private KeySelector begin; + private KeySelector end; + + private int rowsRemaining; + + private Future nextFuture; + private boolean isCancelled = false; + + private AsyncRangeIterator(int rowLimit, boolean reverse, StreamingMode streamingMode) { + this.begin = RangeQuery.this.begin; + this.end = RangeQuery.this.end; + this.rowsLimited = rowLimit != 0; + this.rowsRemaining = rowLimit; + this.reverse = reverse; + this.streamingMode = streamingMode; + + // Register for completion, etc. on the first chunk. Some of the fields in + // this class were initialized with the knowledge that this fetch is active + // at creation time. This set normally happens in startNextFetch, but + // the first fetch has already been configured and started. + SettableFuture promise = new SettableFuture(tr.getExecutor()); + nextFuture = promise; + + // FIXME: should we propagate cancellation into the first chuck fetch? + // This would invalidate the whole iterable, not just the iterator + //promise.onCancelledCancel(firstChunk); + + firstChunk.onReady(new FetchComplete(firstChunk, promise)); + } + + private synchronized boolean mainChunkIsTheLast() { + return !chunk.more || (rowsLimited && rowsRemaining < 1); + } + + class FetchComplete implements Runnable { + final FutureResults fetchingChunk; + final Settable promise; + + public FetchComplete(FutureResults fetch, Settable promise) { + this.fetchingChunk = fetch; + this.promise = promise; + } + + @Override + public void run() { + final RangeResultSummary summary; + boolean toSet = false; + try { + summary = fetchingChunk.getSummaryIfDone(); + if(summary.lastKey == null) { + toSet = true; + } + } catch (RuntimeException e) { + // all the "get" calls will end here if there was a native-level error + promise.setError(e); + return; + } catch (Error e) { + promise.setError(e); + throw e; + } + if(toSet) { + promise.set(Boolean.FALSE); + return; + } + try { + synchronized(AsyncRangeIterator.this) { + fetchOutstanding = false; + + // adjust the total number of rows we should ever fetch + rowsRemaining -= summary.keyCount; + + // set up the next fetch + if(reverse) { + end = KeySelector.firstGreaterOrEqual(summary.lastKey); + } else { + begin = KeySelector.firstGreaterThan(summary.lastKey); + } + + RangeResult data = fetchingChunk.getIfDone(); + // If this is the first fetch or the main chunk is exhausted + if(chunk == null || index == chunk.values.size()) { + nextChunk = null; + chunk = data; + index = 0; + } else { + nextChunk = data; + } + } + } catch (RuntimeException e) { + // all the "get" calls will end here if there was a native-level error + promise.setError(e); + return; + } catch (Error e) { + promise.setError(e); + throw e; + } + promise.set(Boolean.TRUE); + } + }; + + private synchronized void startNextFetch() { + if(fetchOutstanding) + throw new IllegalStateException("Reentrant call not allowed"); // This is not be called reentrantly + if(isCancelled) + return; + + if(mainChunkIsTheLast()) + return; + + fetchOutstanding = true; + nextChunk = null; + + FutureResults fetchingChunk = tr.getRange_internal(begin, end, + rowsLimited ? rowsRemaining : 0, 0, streamingMode.code(), + ++iteration, snapshot, reverse); + + SettableFuture promise = new SettableFuture(tr.getExecutor()); + nextFuture = promise; + promise.onCancelledCancel(fetchingChunk); + fetchingChunk.onReady(new FetchComplete(fetchingChunk, promise)); + } + + @Override + public synchronized Future onHasNext() { + if(isCancelled) + throw new CancellationException(); + + // This will only happen before the first fetch has completed + if(chunk == null) { + return nextFuture; + } + + // We have a chunk and are still working though it + if(index < chunk.values.size()) { + return new ReadyFuture(true, tr.getExecutor()); + } + + // If we are at the end of the current chunk there is either: + // - no more data -or- + // - we are already fetching the next block + return mainChunkIsTheLast() ? + new ReadyFuture(false, tr.getExecutor()) : + nextFuture; + } + + @Override + public boolean hasNext() { + return onHasNext().get(); + } + + // moves to the last position in the current chunk + /*public synchronized void consumeAll() { + index = chunk.values.size() - 1; + }*/ + + @Override + public KeyValue next() { + Future nextFuture; + synchronized(this) { + if(isCancelled) + throw new CancellationException(); + + // at least the first chunk has been fetched and there is at least one + // available result + if(chunk != null && index < chunk.values.size()) { + // If this is the first call to next() on a chunk, then we will want to + // start fetching the data for the next block + boolean initialNext = index == 0; + + KeyValue result = chunk.values.get(index); + prevKey = result.getKey(); + index++; + + // If this is the first call to next() on a chunk there cannot + // be another waiting, since we could not have issued a request + assert(!(initialNext && nextChunk != null)); + + // we are at the end of the current chunk and there is more to be had already + if(index == chunk.values.size() && nextChunk != null) { + index = 0; + chunk = nextChunk; + nextChunk = null; + } + + if(initialNext) { + startNextFetch(); + } + + return result; + } + + nextFuture = onHasNext(); + } + + // If there was no result ready then we need to wait on the future + // and return the proper result, throwing if there are no more elements + return nextFuture.map(NEXT_MAPPER).get(); + } + + @Override + public synchronized void remove() { + if(prevKey == null) + throw new IllegalStateException("No value has been fetched from database"); + + tr.clear(prevKey); + } + + @Override + public synchronized void cancel() { + isCancelled = true; + nextFuture.cancel(); + } + + @Override + public void dispose() { + cancel(); + } + + private final Function NEXT_MAPPER = new Function() { + @Override + public KeyValue apply(Boolean o) { + if(o) + return next(); + throw new NoSuchElementException(); + } + }; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/RangeResult.java b/bindings/java/src/main/com/apple/cie/foundationdb/RangeResult.java new file mode 100644 index 0000000000..92d2e98431 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/RangeResult.java @@ -0,0 +1,54 @@ +/* + * RangeResult.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import java.util.ArrayList; +import java.util.List; + +class RangeResult { + final List values; + final boolean more; + + RangeResult(byte[] keyValues, int[] lengths, boolean more) { + if(lengths.length % 2 != 0) { + throw new IllegalArgumentException("There needs to be an even number of lenghts!"); + } + + int count = lengths.length / 2; + values = new ArrayList(count); + + int offset = 0; + for(int i = 0; i < count; i++) { + int keyLength = lengths[i * 2]; + int valueLength = lengths[(i * 2) + 1]; + + byte[] k = new byte[keyLength]; + System.arraycopy(keyValues, offset, k, 0, keyLength); + + byte[] v = new byte[valueLength]; + System.arraycopy(keyValues, offset + keyLength, v, 0, valueLength); + + offset += keyLength + valueLength; + values.add(new KeyValue(k, v)); + } + this.more = more; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/RangeResultSummary.java b/bindings/java/src/main/com/apple/cie/foundationdb/RangeResultSummary.java new file mode 100644 index 0000000000..433cb4a970 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/RangeResultSummary.java @@ -0,0 +1,33 @@ +/* + * RangeResultSummary.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +class RangeResultSummary { + final byte[] lastKey; + final int keyCount; + final boolean more; + + RangeResultSummary(byte[] lastKey, int keyCount, boolean more) { + this.lastKey = lastKey; + this.keyCount = keyCount; + this.more = more; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/ReadTransaction.java b/bindings/java/src/main/com/apple/cie/foundationdb/ReadTransaction.java new file mode 100644 index 0000000000..8b261dff22 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/ReadTransaction.java @@ -0,0 +1,334 @@ +/* + * ReadTransaction.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncIterator; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * A read-only subset of a FoundationDB {@link Transaction}. This is the interface that + * {@code Transaction}'s {@link Transaction#snapshot snapshot} presents. + * + *
    + * Note: Client must call {@link Transaction#commit()} and wait on the result on all transactions, + * even ones that only read. This is done automatically when using the retry loops from + * {@link Database#run(Function)}. This is explained more in the intro to {@link Transaction}. + * + * @see Transaction + */ +public interface ReadTransaction extends ReadTransactionContext { + /** + * When passed to a {@code getRange()} call that takes a {@code limit} parameter, + * indicates that the query should return unlimited rows. + */ + public static final int ROW_LIMIT_UNLIMITED = 0; + + /** + * Gets the version at which the reads for this {@code Transaction} will access the database. + * @return the version for database reads + */ + public Future getReadVersion(); + + /** + * Gets a value from the database. The call will return {@code null} if the key is not + * present in the database. + * + * @param key the key whose value to fetch from the database + * + * @return a {@code Future} which will be set to the value corresponding to + * the key or to null if the key does not exist. + */ + public Future get(byte[] key); + + /** + * Returns the key referenced by the specified {@code KeySelector}. + * By default, the key is cached for the duration of the transaction, providing + * a potential performance benefit. However, the value of the key is also retrieved, + * using network bandwidth. Invoking {@code setReadYourWritesDisable} will avoid + * both the caching and the increased network bandwidth. + * + * @see KeySelector + * + * @param selector the relative key location to resolve + * + * @return a {@code Future} which will be set to an absolute database key + */ + public Future getKey(KeySelector selector); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code KeySelector}s, with the begin + * {@code KeySelector} inclusive and the end {@code KeySelector} exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(KeySelector begin, KeySelector end); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code KeySelector}s, with the begin + * {@code KeySelector} inclusive and the end {@code KeySelector} exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code KeySelector}s, with the begin + * {@code KeySelector} inclusive and the end {@code KeySelector} exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code KeySelector}s, with the begin + * {@code KeySelector} inclusive and the end {@code KeySelector} exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * @param mode provide a hint about how the results are to be used. This + * can provide speed improvements or efficiency gains based on the caller's + * knowledge of the upcoming access pattern. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(KeySelector begin, KeySelector end, + int limit, boolean reverse, StreamingMode mode); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(byte[] begin, byte[] end); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. + * + * @see KeySelector + * @see AsyncIterator + * + * @param begin the beginning of the range (inclusive) + * @param end the end of the range (exclusive) + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * @param mode provide a hint about how the results are to be used. This + * can provide speed improvements or efficiency gains based on the caller's + * knowledge of the upcoming access pattern. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(byte[] begin, byte[] end, + int limit, boolean reverse, StreamingMode mode); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. {@link Range}s are returned + * from calls to {@link Tuple#range()} and {@link Range#startsWith(byte[])}.
    + *
    + * Note: users of older version of the API should replace old calls to + * {@code getRangeStartsWith( k )} with {@code getRange(Range.startsWith( k ))} + * + * @see KeySelector + * @see AsyncIterator + * + * @param range the range of keys to return + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(Range range); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. {@link Range}s are returned + * from calls to {@link Tuple#range()} and {@link Range#startsWith(byte[])}.
    + *
    + * Note: users of older version of the API should replace old calls to + * {@code getRangeStartsWith( k )} with {@code getRange(Range.startsWith( k ))} + * + * @see KeySelector + * @see AsyncIterator + * + * @param range the range of keys to return + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(Range range, + int limit); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. {@link Range}s are returned + * from calls to {@link Tuple#range()} and {@link Range#startsWith(byte[])}.
    + *
    + * Note: users of older version of the API should replace old calls to + * {@code getRangeStartsWith( k )} with {@code getRange(Range.startsWith( k ))} + * + * @see KeySelector + * @see AsyncIterator + * + * @param range the range of keys to return + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(Range range, + int limit, boolean reverse); + + /** + * Gets an ordered range of keys and values from the database. The begin + * and end keys are specified by {@code byte[]} arrays, with the begin + * key inclusive and the end key exclusive. {@link Range}s are returned + * from calls to {@link Tuple#range()} and {@link Range#startsWith(byte[])}.
    + *
    + * Note: users of older version of the API should replace old calls to + * {@code getRangeStartsWith( k )} with {@code getRange(Range.startsWith( k ))} + * + * @see KeySelector + * @see AsyncIterator + * + * @param range the range of keys to return + * @param limit the maximum number of results to return. Limits results to the + * first keys in the range. Pass {@link #ROW_LIMIT_UNLIMITED} if this query + * should not limit the number of results. If {@code reverse} is {@code true} rows + * will be limited starting at the end of the range. + * @param reverse return results starting at the end of the range in reverse order + * @param mode provide a hint about how the results are to be used. This + * can provide speed improvements or efficiency gains based on the caller's + * knowledge of the upcoming access pattern. + * + * @return a handle to access the results of the asynchronous call + */ + public AsyncIterable getRange(Range range, + int limit, boolean reverse, StreamingMode mode); + + /** + * Returns a set of options that can be set on a {@code Transaction} + * + * @return a set of transaction-specific options affecting this {@code Transaction} + */ + public TransactionOptions options(); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/ReadTransactionContext.java b/bindings/java/src/main/com/apple/cie/foundationdb/ReadTransactionContext.java new file mode 100644 index 0000000000..40e90c74bd --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/ReadTransactionContext.java @@ -0,0 +1,102 @@ +/* + * ReadTransactionContext.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.PartialFunction; +import com.apple.cie.foundationdb.async.PartialFuture; + +import java.util.concurrent.Executor; + +/** + * A context in which a {@code ReadTransaction} is available for database operations. The + * behavior of the methods specified in this interface, especially in the face + * errors, is implementation specific. In particular, some implementations will + * run {@link Function}s multiple times (retry) when certain errors are encountered. + * Therefore a {@code Function} should be prepared to be called more than once. This + * consideration means that a {@code Function} should use caution when directly + * modifying state in a class, especially in a way that could be observed were that + * {@code Function} to not complete successfully. + */ +public interface ReadTransactionContext { + /** + * Runs a function in this context that takes a read-only transaction. Depending on the type of + * context, this may execute the supplied function multiple times if an error is + * encountered. This method is blocking -- control will not return from this call + * until work is complete. + * + * @param retryable the block of logic to execute against a {@link ReadTransaction} + * in this context + * + * @return a result of the last call to {@code retryable} + */ + public abstract T read(Function retryable); + + /** + * Runs a function in this context that takes a read-only transaction. Use this formulation of + * {@link #read(Function)} if the called user code throws checked exceptions. + * + * @param retryable the block of logic to execute against a {@link ReadTransaction} + * in this context + * + * @return a result of the last call to {@code retryable} + * + * @see #read(Function) + */ + public abstract T read(PartialFunction retryable) throws Exception; + + /** + * Runs a function in this context that takes a read-only transaction. Depending on the type of + * context, this may execute the supplied function multiple times if an error is + * encountered. This call is non-blocking -- control flow will return immediately + * with a {@code Future} that will be set when the process is complete. + * + * @param retryable the block of logic to execute against a {@link ReadTransaction} + * in this context + * + * @return a {@code Future} that will be set to the value returned by the last call + * to {@code retryable} + */ + public abstract Future readAsync( + Function> retryable); + + /** + * Runs a function in this context that takes a read-only transaction. Use this formulation of + * {@link #readAsync(Function)} if the called user code throws checked exceptions. + * + * @param retryable the block of logic to execute against a {@link ReadTransaction} + * in this context + * + * @return a {@code PartialFuture} that will be set to the value returned by the last call + * to {@code retryable} + */ + public abstract PartialFuture readAsync( + PartialFunction> retryable); + + /** + * Retrieves the {@link Executor} used by this {@code TransactionContext} when running + * asynchronous callbacks. + * + * @return the {@link Executor} used by this {@code TransactionContext} + */ + public abstract Executor getExecutor(); +} \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/Transaction.java b/bindings/java/src/main/com/apple/cie/foundationdb/Transaction.java new file mode 100644 index 0000000000..3ec30f70a6 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/Transaction.java @@ -0,0 +1,421 @@ +/* + * Transaction.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import com.apple.cie.foundationdb.async.Cancellable; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.PartialFunction; +import com.apple.cie.foundationdb.async.PartialFuture; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * A Transaction represents a FoundationDB database transaction. All operations on FoundationDB + * take place, explicitly or implicitly, through a Transaction.
    + *
    + * In FoundationDB, a transaction is a mutable snapshot of a database. All read and write operations + * on a transaction see and modify an otherwise-unchanging version of the database and only change + * the underlying database if and when the transaction is committed. Read operations do see the + * effects of previous write operations on the same transaction. Committing a transaction usually + * succeeds in the absence of + * conflicts.
    + *
    + * Transactions group operations into a unit with the properties of atomicity, isolation, and + * durability. Transactions also provide the ability to maintain an application's invariants or + * integrity constraints, supporting the property of consistency. Together these properties are + * known as ACID.
    + *
    + * Transactions are also causally consistent: once a transaction has been successfully committed, + * all subsequently created transactions will see the modifications made by it. + * The most convenient way for a developer to manage the lifecycle and retrying + * of a {@code Transaction} is to use {@link Database#run(Function)}. Otherwise, the client + * must have retry logic for fatal failures, failures to commit, and other transient errors.
    + *
    + * Keys and values in FoundationDB are byte arrays. To encode other data types, see the + * {@link Tuple Tuple API} and + * tuple layer documentation.
    + *
    + * When used as a {@link TransactionContext}, the methods {@code run()} and + * {@code runAsync()} on a {@code Transaction} will simply attempt the operations + * without any retry loop.
    + *
    + * Note: Client must call {@link #commit()} and wait on the result on all transactions, even + * ones that only read. This is done automatically when using the retry loops from + * {@link Database#run(Function)}. This is because outstanding reads originating from a + * {@code Transaction} will be cancelled when a {@code Transaction} is garbage collected. + * Since the garbage collector reserves the right to collect an in-scope object if it + * determines that there are no subsequent references it it, this can happen in seemingly + * innocuous situations. {@code Future}s returned from {@code commit()} will block until + * all reads are complete, thereby saving the calling code from this potentially confusing + * situation.
    + *
    + * Note: All keys with a first byte of {@code 0xff} are reserved for internal use.
    + *
    + * Note: Java transactions automatically set the {@link TransactionOptions#setUsedDuringCommitProtectionDisable} + * option. This is because the Java bindings disallow use of {@code Transaction} objects after either + * {@link #reset()} or {@link #onError} is called. + */ +public interface Transaction extends Cancellable, Disposable, ReadTransaction, TransactionContext { + + /** + * Return special-purpose, read-only view of the database. Reads done through this interface are known as "snapshot reads". + * Snapshot reads selectively relax FoundationDB's isolation property, reducing + * Transaction conflicts + * but making reasoning about concurrency harder.
    + *
    + * For more information about how to use snapshot reads correctly, see + * Using snapshot reads. + */ + public ReadTransaction snapshot(); + + /** + * Directly sets the version of the database at which to execute reads. The + * normal operation of a transaction is to determine an appropriately recent + * version; this call overrides that behavior. If the version is set too + * far in the past, {@code past_version} errors will be thrown from read operations. + * Infrequently used. + * + * @param version the version at which to read from the database + */ + public void setReadVersion(long version); + + + /** + * Adds a range of keys to the transaction's read conflict ranges as if you + * had read the range. As a result, other transactions that write a key in + * this range could cause the transaction to fail with a conflict. + * + * @param keyBegin the first key in the range (inclusive) + * @param keyEnd the ending key for the range (exclusive) + */ + public void addReadConflictRange(byte[] keyBegin, byte[] keyEnd); + + /** + * Adds a key to the transaction's read conflict ranges as if you had read + * the key. As a result, other transactions that concurrently write this key + * could cause the transaction to fail with a conflict. + * + * @param key the key to be added to the range + */ + public void addReadConflictKey(byte[] key); + + /** + * Adds a range of keys to the transaction's write conflict ranges as if you + * had cleared the range. As a result, other transactions that concurrently + * read a key in this range could fail with a conflict. + * + * @param keyBegin the first key in the range (inclusive) + * @param keyEnd the ending key for the range (exclusive) + */ + public void addWriteConflictRange(byte[] keyBegin, byte[] keyEnd); + + /** + * Adds a key to the transaction's write conflict ranges as if you had + * written the key. As a result, other transactions that concurrently read + * this key could fail with a conflict. + * + * @param key the key to be added to the range + */ + public void addWriteConflictKey(byte[] key); + + /** + * Sets the value for a given key. This will not affect the + * database until {@link #commit} is called. + * + * @param key the key whose value is to be set + * @param value the value to set in the database + * @throws IllegalArgumentException + * @throws FDBException + */ + public void set(byte[] key, byte[] value); + + /** + * Clears a given key from the database. This will not affect the + * database until {@link #commit} is called. + * + * @param key the key whose value is to be cleared + * @throws IllegalArgumentException + * @throws FDBException + */ + public void clear(byte[] key); + + /** + * Clears a range of keys in the database. The upper bound of the range is + * exclusive; that is, the key (if one exists) that is specified as the end + * of the range will NOT be cleared as part of this operation. Range clears are + * efficient with FoundationDB -- clearing large amounts of data will be fast. + * This will not affect the database until {@link #commit} is called. + * + * @param beginKey the first clear + * @param endKey the key one past the last key to clear + * @throws IllegalArgumentException + * @throws FDBException + */ + public void clear(byte[] beginKey, byte[] endKey); + + /** + * Clears a range of keys in the database. The upper bound of the range is + * exclusive; that is, the key (if one exists) that is specified as the end + * of the range will NOT be cleared as part of this operation. Range clears are + * efficient with FoundationDB -- clearing large amounts of data will be fast. + * This will not affect the database until {@link #commit} is called. + * + * @param range the range of keys to clear + * + * @throws FDBException + */ + public void clear(Range range); + + /** + * Replace with calls to {@link #clear(Range)} with a parameter from a call to + * {@link Range#startsWith(byte[])}. + * + * @param prefix the starting bytes from the keys to be cleared. + * + * @throws FDBException + */ + @Deprecated + public void clearRangeStartsWith(byte[] prefix); + + /** + * An atomic operation is a single database command that carries out several + * logical steps: reading the value of a key, performing a transformation on + * that value, and writing the result. Different atomic operations perform + * different transformations. Like other database operations, an atomic + * operation is used within a transaction.
    + *
    + * Atomic operations do not expose the current value of the key to the client + * but simply send the database the transformation to apply. In regard to + * conflict checking, an atomic operation is equivalent to a write without a + * read. It can only cause other transactions performing reads of the key + * to conflict.
    + *
    + * By combining these logical steps into a single, read-free operation, + * FoundationDB can guarantee that the transaction will not conflict due to + * the operation. This makes atomic operations ideal for operating on keys + * that are frequently modified. A common example is the use of a key-value + * pair as a counter.
    + *
    + * Note: If a transaction uses both an atomic operation and a serializable + * read on the same key, the benefits of using the atomic operation (for both + * conflict checking and performance) are lost. + * + * The behavior of each {@link MutationType} is documented at its definition. + * + * @param optype the operation to perform + * @param key the target of the operation + * @param param the value with which to modify the key + */ + public void mutate(MutationType optype, byte[] key, byte[] param); + + /** + * Commit this {@code Transaction}. See notes in class description. Consider using + * {@code Database}'s {@link Database#run(Function) run()} calls for managing + * transactional access to FoundationDB. + * + * @return a {@code Future} that, when set without error, guarantees the + * {@code Transaction}'s modifications committed durably to the + * database. If the commit failed, it will throw an {@link FDBException}. + *

    + * As with other client/server databases, in some failure scenarios a client may + * be unable to determine whether a transaction succeeded. In these cases, an + * {@link FDBException} will be thrown with error code {@code commit_unknown_result} (1021). + * The {@link #onError} function regards this exception as a retryable one, so + * retry loops that don't specifically detect {@code commit_unknown_result} could end + * up executing a transaction twice. For more information, see the FoundationDB + * Developer Guide documentation. + * + * If any operation is performed on a transaction after a commit has been + * issued but before it has returned, both the commit and the operation will + * throw an error code {@code used_during_commit}(2017). In this case, all + * subsequent operations on this transaction will throw this error. + */ + public Future commit(); + + /** + * Gets the version number at which a successful commit modified the database. + * This must be called only after the successful (non-error) completion of a call + * to {@link #commit()} on this {@code Transaction}, or the behavior is undefined. + * Read-only transactions do not modify the database when committed and will have + * a committed version of -1. Keep in mind that a transaction which reads keys and + * then sets them to their current values may be optimized to a read-only transaction. + * + * @return the database version at which the commit succeeded + */ + public Long getCommittedVersion(); + + /** + * Returns a future which will contain the versionstamp which was used by any versionstamp + * operations in this transaction. The future will be ready only after the successful + * completion of a call to {@link #commit()} on this {@code Transaction}. Read-only + * transactions do not modify the database when committed and will result in the future + * completing with an error. Keep in mind that a transaction which reads keys and then sets + * them to their current values may be optimized to a read-only transaction. + * + * @return a future containing the versionstamp which was used for any versionstamp operations + * in this transaction + */ + public Future getVersionstamp(); + + /** + * Resets a transaction and returns a delayed signal for error recovery. If the error + * encountered by the {@code Transaction} could not be recovered from, the returned + * {@code Future} will be set to an error state. + * + * The current {@code Transaction} object will be invalidated by this call and will throw errors + * when used. The newly reset {@code Transaction} will be returned through the {@code Future} + * if the error was retryable. + * + * If the error is not retryable, then no reset {@code Transaction} is returned, leaving this + * {@code Transaction} permanently invalidated. + * + * @param e the error caught while executing get()s and set()s on this {@code Transaction} + * @return a {@code Future} to be set with a reset {@code Transaction} object to retry the transaction + */ + public Future onError(RuntimeException e); + + /** + * Resets a transaction and returns a delayed signal for error recovery. If the error + * encountered by the {@code Transaction} could not be recovered from, the returned + * {@code PartialFuture} will be set to an error state. + * + * The current {@code Transaction} object will be invalidated by this call and will throw errors + * when used. The newly reset {@code Transaction} will be returned through the {@code PartialFuture} + * if the error was retryable. + * + * @param e the error caught while executing get()s and set()s on this {@code Transaction} + * @return a {@code PartialFuture} to be set with a reset {@code Transaction} object to retry the transaction + */ + public PartialFuture onError(Exception e); + + /** + * Resets a {@code Transaction} to its initial state after creation and returns it as a + * new object. The current {@code Transaction} object is invalidated and will throw errors when used. + * This clears all information about mutations, conflict information built from {@code get}s, + * and the read version. This does not clear any options set on this {@code Transaction} via + * a {@link TransactionOptions} object. + * + * @return a new {@code Transaction} object with reset state. + + */ + public Transaction reset(); + + /** + * Cancels the {@code Transaction}. All pending and any future uses of the + * {@code Transaction} will throw an {@link RuntimeException}. + */ + @Override + public void cancel(); + + /** + * Creates a watch that will become ready when it reports a change to + * the value of the specified key.
    + *
    + * A watch's behavior is relative to the transaction that created it. A + * watch will report a change in relation to the key's value as readable by + * that transaction. The initial value used for comparison is either that of + * the transaction's read version or the value as modified by the transaction + * itself prior to the creation of the watch. If the value changes and then + * changes back to its initial value, the watch might not report the change.
    + *
    + * Until the transaction that created it has been committed, a watch will + * not report changes made by other transactions. In contrast, a watch + * will immediately report changes made by the transaction itself. Watches + * cannot be created if the transaction has set + * {@link TransactionOptions#setReadYourWritesDisable()}, and an attempt to do + * so will raise a {@code watches_disabled} exception.
    + *
    + * If the transaction used to create a watch encounters an exception during + * commit, then the watch will be set with that exception. A transaction whose + * commit result is unknown will set all of its watches with the + * {@code commit_unknown_result} exception. If an uncommitted transaction is + * reset or destroyed, then any watches it created will be set with the + * {@code transaction_cancelled} exception.
    + *
    + * By default, each database connection can have no more than 10,000 watches + * that have not yet reported a change. When this number is exceeded, an + * attempt to create a watch will raise a {@code too_many_watches} exception. + * Because a watch outlives the transaction that creates it, any watch that is no + * longer needed should be cancelled.
    + *
    + * NOTE: calling code must call {@link Transaction#commit()} for + * the watch to be registered with the database. + * + * @param key the key to watch for changes in value + * + * @return a {@code Future} that will become ready when the value changes + * + * @throws FDBException if too many watches have been created on this database. The + * limit defaults to 10,000 and can be modified with a call to + * {@link DatabaseOptions#setMaxWatches(long)}. + */ + public Future watch(byte[] key) throws FDBException; + + /** + * Returns the {@link Database} that this {@code Transaction} is interacting + * with. + * + * @return the {@link Database} object + */ + public Database getDatabase(); + + /** + * Run a function once against this {@code Transaction}. This call blocks while + * user code is executing, returning the result of that code on completion. + * + * @return the return value of {@code retryable} + */ + @Override + public T run(Function retryable); + + /** + * Run a function once against this {@code Transaction}. This call blocks while + * user code is executing, returning the result of that code on completion. + * + * @return the return value of {@code retryable} + * + * @throws Exception if an error is encountered during execution + */ + @Override + public T run(PartialFunction retryable) + throws Exception; + + /** + * Run a function once against this {@code Transaction}. This call returns + * immediately with a {@code Future} handle to the result. + * + * @return a {@code Future} that will be set to the return value of {@code retryable} + */ + @Override + public Future runAsync( + Function> retryable); + + /** + * Run a function once against this {@code Transaction}. This call returns + * immediately with a {@code PartialFuture} handle to the result. Use this + * formulation of {@link #runAsync(Function)} if user code throws a checked exception. + * + * @return a {@code PartialFuture} that will be set to the return value of {@code retryable} + */ + @Override + public PartialFuture runAsync( + PartialFunction> retryable); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/TransactionContext.java b/bindings/java/src/main/com/apple/cie/foundationdb/TransactionContext.java new file mode 100644 index 0000000000..8a82f176e4 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/TransactionContext.java @@ -0,0 +1,103 @@ +/* + * TransactionContext.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb; + +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.PartialFunction; +import com.apple.cie.foundationdb.async.PartialFuture; + +import java.util.concurrent.Executor; + +/** + * A context in which a {@code Transaction} is available for database operations. The + * behavior of the methods specified in this interface, especially in the face + * errors, is implementation specific. In particular, some implementations will + * run {@link Function}s multiple times (retry) when certain errors are encountered. + * Therefore a {@code Function} should be prepared to be called more than once. This + * consideration means that a {@code Function} should use caution when directly + * modifying state in a class, especially in a way that could be observed were that + * {@code Function} to not complete successfully. + */ +public interface TransactionContext extends ReadTransactionContext { + /** + * Runs a function in this context that takes a transaction. Depending on the type of + * context, this may execute the supplied function multiple times if an error is + * encountered. This method is blocking -- control will not return from this call + * until work is complete. + * + * @param retryable the block of logic to execute against a {@link Transaction} + * in this context + * + * @return a result of the last call to {@code retryable} + */ + public abstract T run(Function retryable); + + /** + * Runs a function in this context that takes a transaction. Use this formulation of + * {@link #run(Function)} if the called user code throws checked exceptions. + * + * @param retryable the block of logic to execute against a {@link Transaction} + * in this context + * + * @return a result of the last call to {@code retryable} + * + * @see #run(Function) + */ + public abstract T run(PartialFunction retryable) throws Exception; + + /** + * Runs a function in this context that takes a transaction. Depending on the type of + * context, this may execute the supplied function multiple times if an error is + * encountered. This call is non-blocking -- control flow will return immediately + * with a {@code Future} that will be set when the process is complete. + * + * @param retryable the block of logic to execute against a {@link Transaction} + * in this context + * + * @return a {@code Future} that will be set to the value returned by the last call + * to {@code retryable} + */ + public abstract Future runAsync( + Function> retryable); + + /** + * Runs a function in this context that takes a transaction. Use this formulation of + * {@link #runAsync(Function)} if the called user code throws checked exceptions. + * + * @param retryable the block of logic to execute against a {@link Transaction} + * in this context + * + * @return a {@code PartialFuture} that will be set to the value returned by the last call + * to {@code retryable} + */ + public abstract PartialFuture runAsync( + PartialFunction> retryable); + + /** + * Retrieves the {@link Executor} used by this {@code TransactionContext} when running + * asynchronous callbacks. + * + * @return the {@link Executor} used by this {@code TransactionContext} + */ + public abstract Executor getExecutor(); + +} \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/AbstractFuture.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/AbstractFuture.java new file mode 100644 index 0000000000..6b5e6f138d --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/AbstractFuture.java @@ -0,0 +1,167 @@ +/* + * AbstractFuture.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.concurrent.Executor; + +/** + * An abstract implementation of part of the {@link Future} interface. This class should + * generally only be useful to the advanced user of the FoundationDB Async library for + * creating new types of native-backed futures. + * + * @param the type of the eventual value of the {@code Future} + */ +public abstract class AbstractFuture extends AbstractPartialFuture implements Future { + public AbstractFuture(Executor executor) { + super(executor); + } + + @Deprecated + public AbstractFuture(long cPtr, Executor executor) { + super(cPtr, executor); + } + + @Override + protected abstract T getIfDone(); + + @Override + public T get() { + blockUntilReady(); + return getIfDone(); + } + + @Override + public T getInterruptibly() throws InterruptedException { + blockInterruptibly(); + return getIfDone(); + } + + @Override + public Future flatMap(final Function> m) { + final SettableFuture r = new SettableFuture(getExecutor()); + onReady(new Runnable() { + @Override + public void run() { + final Future rP; + try { + rP = m.apply(get()); + } catch(RuntimeException e){ + r.setError(e); + return; + } catch(Error e) { + r.setError(e); + throw e; + } + rP.onReady(new Runnable() { + @Override + public void run() { + V v; + try { + v = rP.get(); + } catch (RuntimeException e) { + r.setError(e); + return; + } catch (Error e) { + r.setError(e); + throw e; + } + r.set(v); + } + }); + r.onCancelledCancel(rP); + } + }); + r.onCancelledCancel(this); + return r; + } + + @Override + public Future map(final Function m) { + final SettableFuture r = new SettableFuture(getExecutor()); + onReady(new Runnable() { + @Override + public void run() { + V mapped; + try { + mapped = m.apply(get()); + } catch(RuntimeException e) { + r.setError(e); + return; + } catch(Error e) { + r.setError(e); + throw e; + } + r.set(mapped); + } + }); + r.onCancelledCancel(this); + return r; + } + + @Override + public Future rescueRuntime(final Function> m) { + final SettableFuture r = new SettableFuture(getExecutor()); + onReady(new Runnable() { + @Override + public void run() { + T t; + try { + t = get(); + } catch (Error error) { + r.setError(error); + throw error; + } catch(RuntimeException error) { + final Future f; + try { + f = m.apply(error); + } catch(RuntimeException e) { + r.setError(e); + return; + } catch(Error e) { + r.setError(e); + throw e; + } + f.onReady(new Runnable() { + @Override + public void run() { + T t; + try { + t = f.get(); + } catch(RuntimeException e) { + r.setError(e); + return; + } catch(Error e) { + r.setError(e); + throw e; + } + r.set(t); + } + }); + r.onCancelledCancel(f); + return; + } + r.set(t); + } + }); + r.onCancelledCancel(this); + return r; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/AbstractPartialFuture.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/AbstractPartialFuture.java new file mode 100644 index 0000000000..2730854c64 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/AbstractPartialFuture.java @@ -0,0 +1,343 @@ +/* + * AbstractPartialFuture.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.LinkedList; +import java.util.concurrent.Executor; + +import com.apple.cie.foundationdb.Disposable; +import com.apple.cie.foundationdb.FDBException; + +/** + * An abstract implementation of part of the {@link PartialFuture} interface. This class should + * generally only be useful to the advanced user of the FoundationDB Async library for + * creating new types of native-backed futures. + * + * @param the type of the eventual value of the {@code PartialFuture} + */ +public abstract class AbstractPartialFuture implements Disposable, PartialFuture { + private boolean isCallbackRegistered = false; + + private boolean hasFired = false; + + private LinkedList runnables; + + private final Executor executor; + + public AbstractPartialFuture(Executor executor) { + this.executor = executor; + } + + @Deprecated + public AbstractPartialFuture(long cPtr, Executor executor) { + if(cPtr != 0) + throw new IllegalArgumentException("Cannot construct with pointer"); + this.executor = executor; + } + + /** + * To be called only once. Called synchronously if set. + */ + protected abstract void registerSingleCallback(Runnable callback); + + /** + * Gets the value from this {@code Future}. It must be in the ready and non-error + * state. This is a non-blocking call. If this {@code Future} is set to + * an error {@code get()} throws this error. + * + * @return the value to which this {@code Future} has been set. + * @throws FDBException if the value is either unset, or has been set to an + * error. + */ + protected abstract T getIfDone() throws Exception; + + /** + * If the returns false, guarantee that a callback added in the same synchronized block + * will be called when the future is set. + */ + private boolean setupCallback() { + // assert that we were only called when already synchronized on "this" + if(!Thread.holdsLock(this)) + throw new IllegalStateException("called without external synchronization"); + + if(isCallbackRegistered) + return hasFired; + + this.runnables = new LinkedList(); + + registerSingleCallback(new Runnable() { + @Override + public void run() { + synchronized (AbstractPartialFuture.this) { + if (!isCallbackRegistered) { + hasFired = true; + return; + } + } + AbstractPartialFuture.this.executeCallbacks(); + } + }); + isCallbackRegistered = true; + return hasFired; // this is most likely be false, but if the executor was + // synchronous and the future already set, we could have already + // done the callback. + } + + private void executeCallbacks() { + LinkedList runnables; + + synchronized (this) { + runnables = this.runnables; + this.runnables = null; + hasFired = true; + } + + for(Runnable cbp : runnables) { + this.executor.execute(cbp); + } + } + + @Override + public void onReady(final Runnable r) { + boolean shouldRun = false; + synchronized (this) { + if(setupCallback()) + shouldRun = true; + else + runnables.add(r); + } + if(shouldRun) { + this.executor.execute(new Runnable() { + @Override + public void run() { + r.run(); + } + }); + } + } + + @Override + public boolean onReadyAlready(final Runnable r) { + synchronized (this) { + if(setupCallback()) + return true; + runnables.add(r); + return false; + } + } + + @Override + public PartialFuture flatMap(final PartialFunction> m) { + final SettablePartialFuture r = new SettablePartialFuture(getExecutor()); + onReady(new Runnable() { + @Override + public void run() { + final PartialFuture rP; + try { + rP = m.apply(get()); + } catch(Exception e){ + r.setError(e); + return; + } catch(Error e) { + r.setError(e); + throw e; + } + rP.onReady(new Runnable() { + @Override + public void run() { + V v; + try { + v = rP.get(); + } catch (Exception e) { + r.setError(e); + return; + } catch (Error e) { + r.setError(e); + throw e; + } + r.set(v); + } + }); + r.onCancelledCancel(rP); + } + }); + r.onCancelledCancel(this); + return r; + } + + @Override + public PartialFuture map(final PartialFunction m) { + final SettablePartialFuture r = new SettablePartialFuture(getExecutor()); + onReady(new Runnable() { + @Override + public void run() { + V mapped; + try { + mapped = m.apply(get()); + } catch(Exception e) { + r.setError(e); + return; + } catch(Error e) { + r.setError(e); + throw e; + } + r.set(mapped); + } + }); + r.onCancelledCancel(this); + return r; + } + + @Override + public Future rescue(final Function> m) { + final SettableFuture r = new SettableFuture(getExecutor()); + onReady(new Runnable() { + @Override + public void run() { + T t; + try { + t = get(); + } catch (Error error) { + r.setError(error); + throw error; + } catch(Exception error) { + final Future f; + try { + f = m.apply(error); + } catch(RuntimeException e) { + r.setError(e); + return; + } catch(Error e) { + r.setError(e); + throw e; + } + f.onReady(new Runnable() { + @Override + public void run() { + T t; + try { + t = f.get(); + } catch(RuntimeException e) { + r.setError(e); + return; + } catch(Error e) { + r.setError(e); + throw e; + } + r.set(t); + } + }); + r.onCancelledCancel(f); + return; + } + r.set(t); + } + }); + r.onCancelledCancel(this); + return r; + } + + @Override + public PartialFuture rescue( + final PartialFunction> m) { + final SettablePartialFuture r = new SettablePartialFuture(getExecutor()); + onReady(new Runnable() { + @Override + public void run() { + T t; + try { + t = get(); + } catch (Error error) { + r.setError(error); + throw error; + } catch(Exception error) { + final PartialFuture f; + try { + f = m.apply(error); + } catch(Exception e) { + r.setError(e); + return; + } catch(Error e) { + r.setError(e); + throw e; + } + f.onReady(new Runnable() { + @Override + public void run() { + T t; + try { + t = f.get(); + } catch(Exception e) { + r.setError(e); + return; + } catch(Error e) { + r.setError(e); + throw e; + } + r.set(t); + } + }); + r.onCancelledCancel(f); + return; + } + r.set(t); + } + }); + r.onCancelledCancel(this); + return r; + } + + @Override + public T get() throws Exception { + blockUntilReady(); + return getIfDone(); + } + + @Override + public T getInterruptibly() throws Exception { + blockInterruptibly(); + return getIfDone(); + } + + @Override + public void dispose() { + cancel(); + } + + protected Executor getExecutor() { + return this.executor; + } + + /** + * A runnable that, when run, calls {@code notify()} on itself. + * + */ + protected static final class SelfNotifier implements Runnable { + public SelfNotifier() {} + + @Override + public void run() { + synchronized (this) { + this.notify(); + } + } + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/AsyncIterable.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/AsyncIterable.java new file mode 100644 index 0000000000..baf1c75c84 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/AsyncIterable.java @@ -0,0 +1,50 @@ +/* + * AsyncIterable.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.List; + + +/** + * A collection of elements that can be iterated over in a non-blocking fashion. + * + * @param the type of element yielded from iteration + */ +public interface AsyncIterable extends Iterable { + /** + * Gets a non-blocking iterator to be used to enumerate all values. + * + * @return a handle to be used for non-blocking iteration + */ + @Override + public AsyncIterator iterator(); + + /** + * Asynchronously return the results of this operation as a {@code List}. This is + * added as a convenience to users and an opportunity for providers + * of this interface to optimize large operations. + * + * @see AsyncUtil#collect(AsyncIterable) + * + * @return a {@code Future} that will be set to contents of this operation + */ + public Future> asList(); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/AsyncIterator.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/AsyncIterator.java new file mode 100644 index 0000000000..23638702c3 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/AsyncIterator.java @@ -0,0 +1,88 @@ +/* + * AsyncIterator.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +import com.apple.cie.foundationdb.Disposable; + +/** + * A version of {@code Iterator} that allows for non-blocking iteration over elements. + * Calls to {@link #next()} will not block if {@link #onHasNext()} has been called + * since the last call to {@code next()} and the {@code Future} returned from + * {@code onHasNext()} has completed. + * + * @param the type of object yielded by {@code next()} + */ +public interface AsyncIterator extends Iterator, Cancellable, Disposable { + /** + * Returns a asynchronous signal for the presence of more elements in the sequence. + * Once the future returned by {@link #onHasNext()} is ready, the next call to + * {@link #next} will not block. + * + * @return a {@code Future} that will be set to {@code true} if {@code next()} + * would return another element without blocking or to {@code false} if there are + * no more elements in the sequence. + */ + public Future onHasNext(); + + /** + * Blocking call to determine if the sequence contains more elements. This call + * is equivalent to calling {@code onHasNext().get()}. + * + * @see AsyncIterator#onHasNext() + * + * @return {@code true} if there are more elements in the sequence, {@code false} + * otherwise. + */ + @Override + public boolean hasNext(); + + /** + * Returns the next element in the sequence. This will not block if, since the + * last call to {@code next()}, {@link #onHasNext()} was called and the resulting + * {@link Future} has completed or the blocking call {@link #hasNext()} was called + * and has returned. It is legal, therefore, to make a call to {@code next()} without a + * preceding call to + * {@link #hasNext()} or {@link #onHasNext()}, but that invocation of {@code next()} + * may block on remote operations. + * + * @return the next element in the sequence, blocking if necessary. + * + * @throws NoSuchElementException if the sequence has been exhausted. + */ + @Override + public T next(); + + /** + * Cancels any outstanding asynchronous work associated with this {@code AsyncIterator}. + */ + @Override + public void cancel(); + + /** + * Cancel this {@code AsyncIterable} and dispose of associated resources. Equivalent + * to calling {@link AsyncIterator#cancel()}. + */ + @Override + public void dispose(); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/AsyncUtil.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/AsyncUtil.java new file mode 100644 index 0000000000..0c979ce546 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/AsyncUtil.java @@ -0,0 +1,597 @@ +/* + * AsyncUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Provided utilities for using and manipulating {@link Future}s. Many of the methods + * in this class have two versions -- one for dealing with {@link PartialFuture}s and + * one for {@code Future}s. + */ +public class AsyncUtil { + /** + * Run {@code Function} {@code func}, returning all caught exceptions as a + * {@code Future} in an error state. + * + * @param func the {@code Function} to run + * @param value the input to pass to {@code func} + * + * @return the output of {@code func}, or a {@code Future} carrying any exception + * caught in the process. + */ + public static Future applySafely( Function> func, I value ) { + try { + return func.apply(value); + } catch (RuntimeException e) { + return new ReadyFuture(e); + } + } + + /** + * Run {@code PartialFunction} {@code func}, returning all caught exceptions as a + * {@code PartialFuture} in an error state. + * + * @param func the {@code PartialFunction} to run + * @param value the input to pass to {@code func} + * + * @return the output of {@code func}, or a {@code PartialFuture} carrying any exception + * caught in the process. + */ + public static PartialFuture applySafely( PartialFunction> func, I value ) { + try { + return func.apply(value); + } catch (Exception e) { + return new ReadyPartialFuture(e); + } + } + + /** + * Iterates over a set of items and returns the result as a list. + * + * @param iterable the source of data over which to iterate + * + * @return a {@code Future} which will be set to the amalgamation of results + * from iteration. + */ + public static Future> collect(final AsyncIterable iterable) { + final AsyncIterator it = iterable.iterator(); + final List accumulator = new LinkedList(); + + // The condition of the while loop is simply "onHasNext()" returning true + Function> condition = new Function>() { + @Override + public Future apply(Void v) { + return it.onHasNext().map(new Function() { + @Override + public Boolean apply(Boolean o) { + if(o) { + accumulator.add(it.next()); + } + return o; + } + }); + } + }; + + Future complete = whileTrue(condition); + Future> result = tag(complete, accumulator); + + return result; + } + + /** + * Map an {@code AsyncIterable} into an {@code AsyncIterable} of another type or with + * each element modified in some fashion. + * + * @param iterable input + * @param func mapping function applied to each element + * @return a new iterable with each element mapped to a different value + */ + public static AsyncIterable mapIterable(final AsyncIterable iterable, + final Function func) { + return new AsyncIterable() { + @Override + public AsyncIterator iterator() { + final AsyncIterator it = iterable.iterator(); + return new AsyncIterator() { + + @Override + public void remove() { + it.remove(); + } + + @Override + public Future onHasNext() { + return it.onHasNext(); + } + + @Override + public boolean hasNext() { + return it.hasNext(); + } + + @Override + public T next() { + return func.apply(it.next()); + } + + @Override + public void cancel() { + it.cancel(); + } + + @Override + public void dispose() { + it.dispose(); + } + }; + } + + @Override + public Future> asList() { + return iterable.asList().map(new Function, List>() { + @Override + public List apply(List o) { + ArrayList out = new ArrayList(o.size()); + for(V in : o) + out.add(func.apply(in)); + return out; + } + }); + } + }; + } + + /** + * Cast a checked {@link Exception} in a {@code RuntimeException} for when code + * knows that a {@code PartialFuture} is an instance of a {@code Future}. + */ + private static Function> makeTotal = new Function>() { + @Override + public Future apply(Exception e) { + throw (RuntimeException)e; + } + }; + + private static class LoopPartial { + final PartialFunction> body; + final SettablePartialFuture done; + PartialFuture process; + boolean m_cancelled = false; + + public LoopPartial(PartialFunction> body) { + this.body = body; + this.done = new SettablePartialFuture(); + this.done.onCancelled(new Runnable() { + @Override + public void run() { + synchronized( LoopPartial.this ) { + LoopPartial.this.m_cancelled = true; + LoopPartial.this.process.cancel(); + } + } + }); + this.run(); + } + + private boolean shouldContinue(PartialFuture process) { + try { + // Any exception encountered on process will be re-thrown here + if(process.get()) + return true; + } catch (Exception e) { + done.setError(e); + return false; + } catch (Error e) { + done.setError(e); + throw e; + } + done.set(null); + return false; + } + + private void run() { + while(true) { + try { + final PartialFuture process = body.apply(null); + synchronized (this) { + if (m_cancelled) process.cancel(); + this.process = process; + } + + if(process.onReadyAlready(new Runnable() { + @Override + public void run() { + if(shouldContinue(process)) + LoopPartial.this.run(); + }})) + { + if(shouldContinue(process)) + continue; + } + } catch(Exception e) { + done.setError(e); + } catch (Error e) { + done.setError(e); + throw e; + } + + break; + }; + } + } + + /** + * Executes an asynchronous operation repeatedly until it returns {@code False}. + * + * @param body the asynchronous operation over which to loop + * + * @return a {@code PartialFuture} which will be set at completion of the loop. + */ + public static PartialFuture whileTrue(PartialFunction> body) { + return new LoopPartial(body).done; + } + + /** + * Executes an asynchronous operation repeatedly until it returns {@code False}. + * + * @param body the asynchronous operation over which to loop + * + * @return a {@code Future} which will be set at completion of the loop. + */ + public static Future whileTrue(Function> body) { + // Since body can't throw checked exceptions and the implementation of LoopPartial + // doesn't create any, we can use the partial version of the while loop and then + // simply force any resulting Exceptions to type RuntimeException + return new LoopPartial(body).done.rescue(makeTotal); + } + + /** + * Executes {@code body} repeatedly after each time {@code condition} returns {@code true}. + * At each iteration, {@code condition} is evaluated before the call to {@code body}. + * + * @param condition evaluated at the start of each loop, if it returns {@code true}, + * {@code body} is called. + * @param body called each time that {@code condition} returns {@code true}. + * + * @return a signal set when the condition finally returns {@code false} and the loop is + * complete. + */ + /*public static Future whileTrue( + final Callable> condition, + final Callable> body ) + { +// This WOULD be the lambda version of this function, but we're on Java 6! +// return While( () -> +// condition.apply(null).flatMap( cond_true -> +// cond_true ? tag(body.apply(null), true) : new Settable(false) +// ) ); + + return whileTrue(new Callable>() { + @Override + public Future call() { + try { + return condition.call().flatMap(new Function>() { + @Override + public Future apply(Boolean cond_true) throws Exception { + if(cond_true) { + return tag(body.call(), Boolean.TRUE); + } else { + return new ReadyPartialFuture(false); + } + }}); + } catch (Throwable e) { + return new ReadyPartialFuture(e); + } + } + }); + }*/ + + /** + * Maps the outcome of a task into a completion signal. Can be useful if {@code task} has + * side-effects for which all is needed is a signal of completion. + * All errors from {@code task} will be passed to the resulting {@code Future}. + * + * @param task the asynchronous process for which to signal completion + * + * @return a newly created {@code Future} that is set when {@code task} completes + */ + public static Future success(Future task) { + return task.map(new Function() { + @Override + public Void apply(V o) { + return null; + } + }); + } + + /** + * Maps the outcome of a task into a completion signal. Can be useful if {@code task} has + * side-effects for which all is needed is a signal of completion. + * All errors from {@code task} will be passed to the resulting {@code PartialFuture}. + * + * @param task the asynchronous process for which to signal completion + * + * @return a newly created {@code Future} that is set when {@code task} completes + */ + public static PartialFuture success(PartialFuture task) { + return task.map(new Function() { + @Override + public Void apply(V o) { + return null; + } + }); + } + + /** + * Maps the readiness of a {@link PartialFuture} into a completion signal. When + * the given {@link PartialFuture} is set to a value or an error, the returned {@link Future} + * will be set to null. The returned {@link Future} will never be set to an error unless + * it is explicitly cancelled. + * + * @param task the asynchronous process to monitor the readiness of + * + * @return a new {@link Future} that is set when {@code task} is ready. + */ + public static Future whenReady(PartialFuture task) { + return task.map(new PartialFunction() { + @Override + public Void apply(V o) throws Exception { + return null; + } + }).rescue(new Function>() { + @Override + public Future apply(Exception o) { + return ReadyFuture.DONE; + } + }); + } + + /** + * Collects the results of many asynchronous processes into one asynchronous output. If + * any of the tasks returns an error, the output is set to that error. + * + * @param tasks the tasks whose output is to be added to the output + * + * @return a {@code Future} that will be set to the collective result of the tasks + */ + public static Future> getAll(final Collection> tasks) { + return whenAll(tasks).map(new Function>() { + @Override + public List apply(Void o) { + List result = new ArrayList(); + for(Future f : tasks) + result.add(f.get()); + return result; + } + }); + } + + /** + * Collects the results of many asynchronous processes into one asynchronous output. If + * any of the tasks returns an error, the output is set to that error. + * + * @param tasks the tasks whose output is to be added to the output + * + * @return a {@code Future} that will be set to the collective result of the tasks + */ + public static PartialFuture> getAllPartial(final Collection> tasks) { + return whenAll(tasks).map(new PartialFunction>() { + @Override + public List apply(Void o) throws Exception { + List result = new ArrayList(); + for(PartialFuture f : tasks) + result.add( f.get() ); + return result; + } + }); + } + + /** + * Replaces the output of an asynchronous task with a predetermined value. + * + * @param task the asynchronous process whose output is to be replaced + * + * @param value the predetermined value to be returned on success of {@code task} + * + * @return a {@code Future} that will be set to {@code value} on completion of {@code task} + */ + public static Future tag(Future task, final V value) { + return task.map(new Function() { + @Override + public V apply(T o) { + return value; + } + }); + } + + /** + * Replaces the output of an asynchronous task with a predetermined value. + * + * @param task the asynchronous process whose output is to be replaced + * + * @param value the predetermined value to be returned on success of {@code task} + * + * @return a {@code Future} that will be set to {@code value} on completion of {@code task} + */ + public static PartialFuture tag(PartialFuture task, final V value) { + return task.map(new Function() { + @Override + public V apply(T o) { + return value; + } + }); + } + + /** + * Return a {@code Future} that will be set when any of the {@code PartialFuture} + * inputs are done. A {@code Future} is done both on success and failure. + * + * @param input the list of {@code PartialFuture}s to monitor. This list + * must not be modified during the execution of this call. + * + * @return a signal that will be set when any of the {@code Future}s are done + */ + public static Future whenAny(final Collection> input) { + // Short-circuit work for the case that there is no need for the callback mechanism + for(PartialFuture a : input) { + if(a.isDone()) { + return ReadyFuture.DONE; + } + } + + final SettableFuture p = new SettableFuture(); + for(PartialFuture a : input) { + p.onCancelledCancel(a); + a.onReady(new Runnable() { + @Override + public void run() { + if(p.isSet()) + return; + try { + p.set((Void)null); + } catch(Exception e) { + // There is significant chance that this is a race to set the + // promise, therefore this being an error is not surprising. + // Also, there is nowhere for an exception to go, aside from + // from going to a default handler. + } + } + }); + } + return p; + } + + /** + * Return a {@code Future} that will be set when all the {@code PartialFuture} + * inputs are done. A {@code Future} is done both on success and failure. + * + * @param input the list of {@code PartialFuture}s to monitor. This list + * must not be modified during the execution of this call. + * + * @return a signal that will be set when all of the {@code Future}s are done + */ + public static Future whenAll(final Collection> input) { + int count = input.size(); + if(count == 0) { + return ReadyFuture.DONE; + } + + // Is this possibly susceptible to a race where the count is taken before + // the iteration and therefore the collection could have been modified? + // This could lead to a mismatch in the + final AtomicInteger outstanding = new AtomicInteger(count); + final SettableFuture p = new SettableFuture(); + for(PartialFuture a : input) { + p.onCancelledCancel(a); + a.onReady(new Runnable() { + @Override + public void run() { + if(outstanding.decrementAndGet() == 0) { + p.set((Void)null); + } + } + }); + } + return p; + } + + private AsyncUtil() {} + + /* + private static AsyncIterable readyIterable(final Iterable it) { + return new AsyncIterable() { + @Override + public AsyncIterator iterator() { + return new ReadyIterator(it.iterator()); + } + + @Override + public Future> asList() { + ArrayList c = new ArrayList(); + for(T t: it) { + c.add(t); + } + return new ReadyFuture>(c); + } + }; + } + + private static class ReadyIterator implements AsyncIterator { + private Iterator iterator; + + public ReadyIterator(Iterator iterator) { + this.iterator = iterator; + } + + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public T next() { + return iterator.next(); + } + + @Override + public void remove() { + // This will throw if the underlying container does not support it + iterator.remove(); + } + + @Override + public Future onHasNext() { + return new ReadyFuture(iterator.hasNext()); + } + } + + private static void testCollectionAction(AsyncIterable readyIterable) { + Future> collected = collect(readyIterable); + try { + List list = collected.get(); + System.out.println("Via dual parameter (default) collect(): List of " + list.size() + " items"); + } catch(Throwable t) { + t.printStackTrace(); + } + + collected = singleBodyTest(readyIterable); + try { + List list = collected.get(); + System.out.println("Via single parmeter (test) collections: List of " + list.size() + " items"); + } catch(Throwable t) { + t.printStackTrace(); + } + } + + public static void main(String[] args) { + // test collect over large set of items + AsyncIterable readyIterable = readyIterable(Collections.nCopies(10000, Integer.valueOf(14))); + for(int i = 0; i < 100; i++) { + testCollectionAction(readyIterable); + } + }*/ +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/Cancellable.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/Cancellable.java new file mode 100644 index 0000000000..4fd3980b68 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/Cancellable.java @@ -0,0 +1,37 @@ +/* + * Cancellable.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +/** + * Describes an operation or signal that can be cancelled. Cancellation will be assumed + * to "chain" -- that is, once an {@code cancel()} is called, an operation will be cancelled + * even if there are other consumers of the operation which have not cancelled. + * + */ +public interface Cancellable { + /** + * Cancels this operation or signal. This will end the work that would have been done + * and notify all consumers of the operation that a result will not be returned. It + * is not an error to call this method on an operation that has already completed or + * already been cancelled. This method will not block or throw non-fatal exceptions. + */ + public abstract void cancel(); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/CloneableException.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/CloneableException.java new file mode 100644 index 0000000000..c260479288 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/CloneableException.java @@ -0,0 +1,34 @@ +/* + * CloneableException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +/** + * An {@link Exception} that can be duplicated with a new backtrace. + */ +public interface CloneableException { + /** + * Create and return a new {@code Exception} that has a backtrace including the + * calling line of code. + * + * @return a newly created {@code Exception}. + */ + public Exception retargetClone(); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/Function.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/Function.java new file mode 100644 index 0000000000..60b28d38a9 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/Function.java @@ -0,0 +1,47 @@ +/* + * Function.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +/** + * Applies a process to an input with typed output. The function itself should return + * immediately and, if a blocking process is needed, launch that process and return + * a {@link Future} handle. If the code implementing {@link #apply(Object) apply} throws + * a checked exception use {@link PartialFunction} instead. + * + * @see PartialFunction + * @see Future + * + * @param the type of the input variable + * @param the type of the output + */ +public interface Function extends PartialFunction { + /** + * Applies a process on input {@code o} to obtain a result or type {@code V}. A + * special case of {@link PartialFunction#apply(Object)} that does not throw + * checked exceptions. + * + * @param o the input to process + * + * @return a result of type {@code V} + */ + @Override + public V apply(T o); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/Future.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/Future.java new file mode 100644 index 0000000000..b8ec2d2bba --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/Future.java @@ -0,0 +1,115 @@ +/* + * Future.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +/** + * The typed result of an asynchronous process. + * A {@code Future} will be set exactly once to either a "completed" {@code T} + * or to an unchecked exception. A {@code Future} is a special case of a + * {@link PartialFuture} that cannot result in a checked exception.
    + *
    + * A {@code Future} can be waited on by blocking, or by using asynchronous callbacks. + * + * @param the type of the eventual value of the {@code Future} + */ +public interface Future extends PartialFuture { + /** + * Apply a function to the successful result of this operation. This version of + * {@code map()}, since it accepts only {@code Function}s that cannot throw checked + * exceptions, returns a {@code Future}. Errors encountered + * in the execution of this operation are sent to the resulting {@code Future}.
    + *
    + * If the returned {@code Future} is {@link Future#cancel() cancelled} the input + * {@code Future} will also be cancelled. + * + * @param m the map to the conversion to be run + * @return a new {@code Future} that passes errors and converts output of this {@code Future}. + */ + public Future map(final Function m); + + /** + * Applies the successful result of this operation as the input into another asynchronous + * operation and returns a handle to that result. + * That is, when the operation represented by this {@code Future} is complete and a result + * is returned, the {@code Mapper} {@code m} is invoked to begin a new asynchronous process + * using the result. This version of + * {@code map()}, since it accepts only {@code Function}s that cannot throw checked + * exceptions, returns a {@code Future}. + * Errors in the execution of either operation are sent to the resulting {@code Future}.
    + *
    + * If the returned {@code Future} is {@link Future#cancel() cancelled} the input + * {@code Future} will also be cancelled. + * + * @param m the map to the asynchronous process to be run + * @return a new {@code Future} that passes errors and converts output of this {@code Future} + * to the output of another asynchronous process. + */ + public Future flatMap(final Function> m); + + /** + * Returns a {@code Future} that modifies the error behavior of this {@code Future}. + * Since {@code Future} cannot be set to an {@link Exception} other than a + * {@code RuntimeException}, the handling {@code Function} need not handle other, + * more general, types. If an {@link Error} is the output of this process, this handler + * is not invoked and that {@code Error} will be passed on to the resulting {@code Future}. + * + * @param m the process to run that maps an error into another asynchronous operation + * + * @return a newly created {@code Future} with modified error behavior + */ + public Future rescueRuntime(Function> m); + + /** + * Blocks until a value is set on this {@code Future} and returns it. + * If this {@code Future} is set to a RuntimeException or Error, throws it. + * A {@code Future} cannot be set to a checked exception, so there is no + * need to throw from this call. + */ + @Override + public T get(); + + /** + * Blocks until a value is set on this {@code Future} and returns it. + * If this {@code PartialFuture} is set to an {@link Exception} or {@link Error} + * this call will throw that error. + * + * @return the output value of the asynchronous process + * + * @throws InterruptedException if the blocked thread is interrupted + */ + @Override + public T getInterruptibly() throws InterruptedException; + + /** + * Gets the error from a {@code Future} if it has been set to this state. If + * this {@code Future} is not yet set, or has not been set to an error, throws + * an exception. Although this method returns a {@code Throwable}, the set of + * possible return types is more limited. A {@code Future} is limited to being + * set to an unchecked exceptions -- that is an {@link Error} or a + * {@link RuntimeException} (or one of its subclasses). + * + * @return the error output of the {@code Future} + * + * @throws IllegalStateException if this {@code Future} is not in an error state + */ + @Override + public Throwable getError(); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/PartialFunction.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/PartialFunction.java new file mode 100644 index 0000000000..acc6ff9e92 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/PartialFunction.java @@ -0,0 +1,48 @@ +/* + * PartialFunction.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +/** + * Applies a process to an input with typed output. A {@code PartialFunction} is, + * like the mathematical concept of the same name, not defined over all possible inputs, + * and can therefore throw {@code Exception}s. The function itself should return + * immediately and, if a blocking process is needed, launch that process and return + * a {@link Future} handle. If the code implementing {@link #apply(Object) apply} does + * not throw a checked exception consider using {@link Function}. + * + * @see Function + * @see PartialFuture + * + * @param the type of the input variable + * @param the type of the output + */ +public interface PartialFunction { + /** + * Applies a process on input {@code o} to obtain a result or type {@code V}. + * + * @param o the input to process + * + * @return a result of type {@code V} + * + * @throws Exception on inputs for which no result is defined + */ + public V apply(T o) throws Exception; +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/PartialFuture.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/PartialFuture.java new file mode 100644 index 0000000000..523fc516b0 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/PartialFuture.java @@ -0,0 +1,212 @@ +/* + * PartialFuture.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import com.apple.cie.foundationdb.Disposable; + +/** + * The typed result of an asynchronous process. + * A {@code PartialFuture} will be set exactly once to either a "completed" {@code T} + * or to an {@link Exception}. A {@code PartialFuture} is "partial" in that some + * outcomes of the process involve the error output being a checked exception. A + * {@link Future} is a special case of a {@code PartialFuture} that cannot result in + * a checked exception.
    + *
    + * A {@code PartialFuture} can be waited on by blocking, or by using asynchronous callbacks. + * + * @param the type of the eventual value of the {@code PartialFuture} + */ +public interface PartialFuture extends Cancellable, Disposable { + /** + * Registers a {@code Runnable} to run as the callback for this {@code Future} + * on a default thread. It will be called after the future becomes ready, including + * if the future is set to a value, set to an error, or cancelled.
    + *
    + * Consider using {@link #map(PartialFunction)} and {@link #flatMap(PartialFunction)} + * instead, as they automatically provide error propagation and cancellation. + * + * @param r the code to run when the future is set either to value or error + */ + public void onReady(Runnable r); + + /** + * Checks if this {@code Future} is ready and, if not, atomically registers a + * {@code Runnable} to run as the callback for this future on a default thread. The + * callback will not be called if the future is ready. Callers can rely on the guarantee + * that if this function returns {@code false} their callback will be executed on + * completion (including if the future is set to a value, set to an error, or cancelled), + * and if {@code true} they can then call {@link #get()} without blocking. + * + * @param r the code to run when the future is set if it is not currently ready + * + * @return {@code true} if the future is ready, {@code false} otherwise + */ + public boolean onReadyAlready(Runnable r); + + /** + * Apply a function to the successful result of this operation. Errors + * in the execution of this operation are sent to the resulting {@code Future}.
    + *
    + * If the returned {@code Future} is {@link PartialFuture#cancel() cancelled} the input + * input {@code Future} will also be cancelled. + * + * @param m the map to the conversion to be run + * @return a new {@code Future} that passes errors and converts output of this {@code Future}. + */ + public PartialFuture map(PartialFunction m); + + /** + * Applies the successful result of this operation as the input into another asynchronous + * operation and returns a handle to that result. + * That is, when the operation represented by this {@code Future} is complete and a result + * is returned, the {@code Mapper} {@code m} is invoked to begin a new asynchronous process + * using the result. + * Errors in the execution of either operation are sent to the resulting {@code Future}.
    + *
    + * If the returned {@code Future} is {@link PartialFuture#cancel() cancelled} the input + * input {@code Future} will also be cancelled. + * + * @param m the map to the asynchronous process to be run + * @return a new {@code Future} that passes errors and converts output of this {@code Future} + * to the output of another asynchronous process. + */ + public PartialFuture flatMap(PartialFunction> m); + + /** + * Map any error result of this {@code Future} to another asynchronous process. This can be + * used when an error is expected or can be logically dealt with in some way. If the + * rescue process cannot handle the error, or if the rescue process itself throws an error, + * that error will be set on this {@code Future}. If the result + * of this call is used in place of this {@code Future}, the user of the output + * {@code Future} + * will see successes of this process, successes of the rescue process, or errors from + * the rescue process. + * + * @param m the process to run that maps an error into another asynchronous operation + * + * @return a newly created {@code Future} with modified error behavior + */ + public PartialFuture rescue(PartialFunction> m); + + /** + * Convert an error output of this operation into another asynchronous process. + * + * @param m the process to run on completion into error state. + * + * @return the non-checked {@code Future} output with modified error behavior + */ + public Future rescue(Function> m); + + /** + * Blocks until this {@code Future} is set to either a value or an error. + * When this function returns without an error, {@link #isDone()} will return + * {@code true}. + *
    + * Code should never block for a future to become ready in code invoked as a callback. + * Blocking in callback code will generally block client networking and cause a deadlock. + * + */ + public void blockUntilReady(); + + /** + * Blocks until this {@code Future} is set to either a value or an error. + * When this function returns without an error, {@link #isDone()} will return + * {@code true}. + *
    + * Code should never block for a future to become ready in code invoked as a callback. + * Blocking in callback code will generally block client networking and cause a deadlock. + * + * @throws InterruptedException if the blocked thread is interrupted + */ + public void blockInterruptibly() throws InterruptedException; + + /** + * Blocks until a value is set on this {@code PartialFuture} and returns it. + * If this {@code PartialFuture} is set to an {@link Exception} or {@link Error} + * this call will throw that error. + * + * @return the output value of the asynchronous process + * + * @throws Exception if the process was not successfully executed. + * @throws InterruptedException if the blocked thread is interrupted + * @throws Error if the process encountered a serious {@code Error} during execution. + */ + public T get() throws Exception; + + /** + * Blocks until a value is set on this {@code PartialFuture} and returns it. + * If this {@code PartialFuture} is set to an {@link Exception} or {@link Error} + * this call will throw that error. + * + * @return the output value of the asynchronous process + * + * @throws Exception if the process was not successfully executed. + * @throws InterruptedException if the blocked thread is interrupted + * @throws Error if the process encountered a serious {@code Error} during execution. + */ + public T getInterruptibly() throws Exception; + + /** + * Cancels this asynchronous operation. If called before the {@code Future} is + * ready, subsequent attempts to access its value will throw. + * Cancelling a {@code Future} which is already set has no effect. + * Cancelling a {@code Future} should not be assumed to eliminate the affects + * of launching the operation. Many asynchronous operations start work on remote + * machines -- this work will generally not be stopped if the local {@code Future} + * is cancelled. + */ + @Override + public void cancel(); + + /** + * Gets the readiness of this {@code Future}. A {@code Future} is ready + * if the value has been set or an error has been set. + * + * @return true if the {@code Future} is set to a value or error + */ + public boolean isDone(); + + /** + * Tests if this {@code Future} is set to an error + * @return true if in error state, false otherwise + */ + public boolean isError(); + + /** + * Gets the error from a {@code Future} if it has been set to this state. If + * this {@code Future} is not yet set, or has not been set to an error, throws + * an exception. Although this method returns a {@code Throwable}, the set of + * possible types is more limited. A {@code PartialFuture} is limited to be set in + * an error state to an {@code Exception} (or a subclass thereof) or an unchecked + * {@link Error}. + * + * @return the error output of the {@code Future} + * + * @throws IllegalStateException if this {@code PartialFuture} is not in an error state + */ + public Throwable getError(); + + /** + * Equivalent to calling {@link #cancel()}. + */ + @Override + public void dispose(); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/ReadyFuture.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/ReadyFuture.java new file mode 100644 index 0000000000..c315954019 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/ReadyFuture.java @@ -0,0 +1,154 @@ +/* + * ReadyFuture.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.concurrent.Executor; + +/** + * A {@code Future} that is always in the "set" state. No call on a {@code ReadyFuture} + * will block, since this is either a value or an error from its time of creation. A + * {@code ReadyFuture} is useful for returning the result of an asynchronous process.
    + *
    + * If code needs to return a {@code Future} to indicate completion, + * {@link #DONE} should be used. + * + * @param the type of value that can be set + */ +public class ReadyFuture extends ReadyPartialFuture implements Future { + /** + * A set {@code Future} that indicates a process has completed successfully. If the + * the contract of a function is to signal completion with a {@code Future} + * this value can be returned at successful completion. + */ + public static final Future DONE = new ReadyFuture((Void)null); + + /** + * Create a new {@code ReadyFuture} set to a value. + * + * @param value the final value of this {@code Future} + */ + public ReadyFuture(T value) { + super(value); + } + + /** + * Create a new {@code ReadyFuture} set to an error. If {@code error} is {@code null} the + * {@code Future} will be set to a {@code null} value and NOT an error. + * + * @param error the final error of this {@code Future} + */ + public ReadyFuture(Error error) { + super(error); + } + + /** + * Create a new {@code ReadyFuture} set to an error. If {@code error} is {@code null} the + * {@code Future} will be set to a {@code null} value and NOT an error. + * + * @param error the final error of this {@code Future} + */ + public ReadyFuture(RuntimeException error) { + super(error); + } + + /** + * Create a new {@code ReadyFuture} set to a value. + * + * @param value the final value of this {@code Future} + * @param executor the executor with which to execute callbacks + */ + public ReadyFuture(T value, Executor executor) { + super(value, executor); + } + + /** + * Create a new {@code ReadyFuture} set to an error. If {@code error} is {@code null} the + * {@code Future} will be set to a {@code null} value and NOT an error. + * + * @param error the final error of this {@code Future} + * @param executor the executor with which to execute callbacks + */ + public ReadyFuture(Error error, Executor executor) { + super(error, executor); + } + + /** + * Create a new {@code ReadyFuture} set to an error. If {@code error} is {@code null} the + * {@code Future} will be set to a {@code null} value and NOT an error. + * + * @param error the final error of this {@code Future} + * @param executor the executor with which to execute callbacks + */ + public ReadyFuture(RuntimeException error, Executor executor) { + super(error, executor); + } + + @Override + public T get() { + try { + return super.get(); + } catch (Error e) { + throw e; + } catch (Throwable t) { + throw (RuntimeException)t; + } + } + + @Override + public T getInterruptibly() { + return this.get(); + } + + @Override + public Future map(Function m) { + try { + V mapped = m.apply(get()); + return new ReadyFuture(mapped, executor); + } catch(RuntimeException t) { + return new ReadyFuture(t, executor); + } + } + + @Override + public Future flatMap(Function> m) { + try { + return m.apply(get()); + } catch(RuntimeException t) { + return new ReadyFuture(t, executor); + } + } + + @Override + public Future rescueRuntime(Function> m) { + if(!isError()) { + return new ReadyFuture(get(), executor); + } + + try { + Throwable error = getError(); + if (error instanceof Error) + throw (Error)error; + return m.apply( (RuntimeException)error ); + } catch(RuntimeException t) { + return new ReadyFuture(t, executor); + } + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/ReadyPartialFuture.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/ReadyPartialFuture.java new file mode 100644 index 0000000000..52f0efc36a --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/ReadyPartialFuture.java @@ -0,0 +1,243 @@ +/* + * ReadyPartialFuture.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.concurrent.Executor; + +/** + * A {@code PartialFuture} that is always in the "set" state. No call on a {@code ReadyPartialFuture} + * will block, since this is either a value or an error from its time of creation. A + * {@code ReadyPartialFuture} is useful for returning the result of an asynchronous process.
    + *
    + * If code needs to return a {@code Future} to indicate completion, {@link ReadyFuture#DONE} + * should be used. + * + * @param the type of value that can be set + */ +public class ReadyPartialFuture implements PartialFuture { + private static final Executor DEFAULT_ES = new Executor() { + @Override + public void execute(Runnable command) { + command.run(); + } + }; + + protected final Executor executor; + private final boolean isError; + private final T value; + private final Throwable error; + + /** + * Create a new {@code ReadyFuture} set to a value. + * + * @param value the final value of this {@code Future} + */ + public ReadyPartialFuture(T value) { + this(value, DEFAULT_ES); + } + + /** + * Create a new {@code ReadyFuture} set to an error. If {@code error} is {@code null} the + * {@code Future} will be set to a {@code null} value and NOT an error. + * + * @param error the final error of this {@code Future} + */ + public ReadyPartialFuture(Error error) { + this(error, DEFAULT_ES); + } + + /** + * Create a new {@code ReadyPartialFuture} set to an error. If {@code error} is + * {@code null} the {@code Future} will be set to a {@code null} value and + * NOT an error. + * + * @param error the final error of this {@code Future} + */ + public ReadyPartialFuture(Exception error) { + this(error, DEFAULT_ES); + } + + /** + * Create a new {@code ReadyPartialFuture} set to a value. + * + * @param value the final value of this {@code Future} + * @param executor the executor with which to execute callbacks + */ + public ReadyPartialFuture(T value, Executor executor) { + this.isError = false; + this.value = value; + this.error = null; + this.executor = executor; + } + + /** + * Create a new {@code ReadyFuture} set to an error. If {@code error} is {@code null} the + * {@code Future} will be set to a {@code null} value and NOT an error. + * + * @param error the final error of this {@code Future} + * @param executor the executor with which to execute callbacks + */ + public ReadyPartialFuture(Error error, Executor executor) { + this((Throwable)error, executor); + } + + /** + * Create a new {@code ReadyFuture} set to an error. If {@code error} is {@code null} the + * {@code Future} will be set to a {@code null} value and NOT an error. + * + * @param error the final error of this {@code Future} + * @param executor the executor with which to execute callbacks + */ + public ReadyPartialFuture(Exception error, Executor executor) { + this((Throwable)error, executor); + } + + private ReadyPartialFuture(Throwable error, Executor executor) { + // If the error is set to null, this is NOT an error + isError = error != null; + + this.error = error; + this.value = null; + this.executor = executor; + } + + /** + * Does nothing, as a {@code ReadyPartialFuture} cannot be cancelled. + */ + @Override + public void cancel() {} + + @Override + public void onReady(Runnable r) { + this.executor.execute(r); + } + + @Override + public boolean onReadyAlready(Runnable r) { + return true; + } + + @Override + public PartialFuture flatMap( + PartialFunction> m) { + try { + return m.apply(get()); + } catch(Exception t) { + return new ReadyPartialFuture(t, executor); + } + } + + @Override + public PartialFuture map(PartialFunction m) { + try { + V mapped = m.apply(get()); + return new ReadyPartialFuture(mapped, executor); + } catch(Exception t) { + return new ReadyPartialFuture(t, executor); + } + } + + @Override + public Future rescue(Function> m) { + if(!isError()) { + return new ReadyFuture(value, executor); + } + + try { + if (error instanceof Error) + throw (Error)error; + return m.apply( (Exception)error ); + } catch(RuntimeException t) { + return new ReadyFuture(t, executor); + } + } + + @Override + public PartialFuture rescue( + PartialFunction> m) { + if(!isError()) { + return this; + } + + try { + if (error instanceof Error) + throw (Error)error; + return m.apply((Exception)error); + } catch(Exception t) { + return new ReadyPartialFuture(t, executor); + } + } + + /** + * Always returns immediately. + */ + @Override + public void blockUntilReady() { + return; + } + + @Override + public void blockInterruptibly() { + return; + } + + @Override + public T get() throws Exception { + if(isError()) { + if (this.error instanceof Error) + throw (Error)this.error; + throw (Exception)this.error; + } + return value; + } + + @Override + public T getInterruptibly() throws Exception { + return this.get(); + } + + /** + * A {@code ReadyFuture} is always done. + * + * @return always returns {@code true}. + */ + @Override + public boolean isDone() { + return true; + } + + @Override + public boolean isError() { + return isError; + } + + @Override + public Throwable getError() { + if(!isError()) + throw new IllegalStateException("Future is set to value, not error"); + return error; + } + + @Override + public void dispose() { + cancel(); + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/Settable.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/Settable.java new file mode 100644 index 0000000000..e41045ad5f --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/Settable.java @@ -0,0 +1,105 @@ +/* + * Settable.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +/** + * Used to communicate output to consumers of an asynchronous process. This is the general + * case of the concept {@code Promise}, where {@link SettablePartial} is a special case + * that can also accept an unchecked {@link Exception}. + * + * @param the type of output of a successful process + */ +public interface Settable { + /** + * Set this {@code Settable} to a value. This will invoke callbacks, and unblock + * all {@code Thread}s waiting on associated {@code Future}s. A {@code Settable} + * can be set only once and this call + * will throw an exception if this object is already set to a value or error. It + * is not, however, an error to call this if the operation has been cancelled. + * If the task has been cancelled, this call will have no effect. + * + * @param value the output of this successful process + * + * @throws IllegalStateException if {@code set()} or {@code setError()} has + * already been called. + */ + public abstract void set(T value); + + /** + * Set this {@code Settable} to an error. This call will invoke callbacks, and unblock + * all {@code Thread}s waiting on associated {@code Future}s. A {@code Settable} + * can be set only once and this call will + * throw an exception if this object is already set to a value or error. It + * is not, however, an error to call this method if the operation has been cancelled. + * If the task has been cancelled, this call will have no effect. + * + * @param error the error encountered in the course of this process + * + * @throws IllegalStateException if {@code set()} or {@code setError()} has + * already been called. + */ + public abstract void setError(RuntimeException error); + + /** + * Set this {@code Settable} to an error. This call will invoke callbacks, and unblock + * all {@code Thread}s waiting on associated {@code Future}s. A {@code Settable} + * can be set only once and this call will + * throw an exception if this object is already set to a value or error. It + * is not, however, an error to call this method if the operation has been cancelled. + * If the task has been cancelled, this call will have no effect. + * + * @param error the error encountered in the course of this process + * + * @throws IllegalStateException if {@code set()} or {@code setError()} has + * already been called. + */ + public abstract void setError(Error error); + + /** + * Returns {@code true} if this {@code Settable} has been set to a value or error. + * + * @return {@code true} if a value or error has been set, {@code false} otherwise + */ + public abstract boolean isSet(); + + /** + * Register code to be called if this operation is cancelled. {@code Settable}s can + * be cancelled by calls to {@link Future#cancel()} on associated {@code Future}s. + * If this {@code Settable} is already set, the + * callback will not be registered and this call will return {@code false}. If this + * call returns {@code true} the registered callback is guaranteed to be called if + * the associated {@code Future} is cancelled. + * + * @param r routine to execute if cancelled. + * + * @return {@code true} if the callback was registered, {@code false} otherwise. + */ + public abstract boolean onCancelled(Runnable r); + + /** + * Link an operation to cancel in the event that this {@code Settable} is cancelled. + * This is a convenience method (for more complicated calls to + * {@link #onCancelled(Runnable)}) with possible internal optimizations. + * + * @param c the operation to cancel + */ + public abstract void onCancelledCancel(Cancellable c); +} \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/SettableFuture.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/SettableFuture.java new file mode 100644 index 0000000000..f3399932af --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/SettableFuture.java @@ -0,0 +1,284 @@ +/* + * SettableFuture.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.Executor; + +import com.apple.cie.foundationdb.FDBException; + +/** + * Represents an {@link Future} that can be fulfilled via the {@link Settable} interface. + * + * {@code SettableFuture} has a default {@code Executor} that is used when a + * {@code SettableFuture} is created without passing this parameter, which runs + * callback functions immediately on the stack when the future is fulfilled. + * + * @see ReadyFuture + * + * @param the output type of a successful process + */ +public class SettableFuture extends AbstractFuture implements Settable { + private static final Executor DEFAULT_ES = new Executor() { + @Override + public void execute(Runnable command) { + command.run(); + } + }; + + /** + * Construct a {@code SettableFuture} with the default {@code Executor} + */ + public SettableFuture() { + this(DEFAULT_ES); + } + + /** + * Construct a {@code SettableFuture} with a specified {@code Executor} + * + * @param executor the {@code Executor} with which to execute callbacks + */ + public SettableFuture(Executor executor) { + super(executor); + } + + private List cancelCallbacks = null; + private List chainedCancels = null; + + private volatile boolean isSet = false; + private volatile boolean isError = false; + private volatile boolean isCancelled = false; + private T value = null; + private Throwable error = null; + + private Runnable callback = null; + + @Override + public void set(T value) { + Runnable localCb = null; + synchronized(this) { + if(isCancelled) { + return; + } + if(isSet()) { + throw new IllegalStateException("Settable already set"); + } + this.isSet = true; + this.isError = false; + this.value = value; + localCb = this.callback; + this.callback = null; + this.cancelCallbacks = null; + this.chainedCancels = null; + } + if(localCb != null) + localCb.run(); + } + + @Override + public void setError(Error error) { + setErrorInternal(error); + } + + @Override + public void setError(RuntimeException error) { + setErrorInternal(error); + } + + private void setErrorInternal(Throwable error) { + Runnable localCb = null; + synchronized(this) { + if(isCancelled) { + return; + } + if(isSet()) { + throw new IllegalStateException("Settable already set"); + } + this.isSet = true; + this.isError = true; + this.error = error; + localCb = this.callback; + this.callback = null; + this.cancelCallbacks = null; + this.chainedCancels = null; + } + if(localCb != null) + localCb.run(); + } + + @Override + public void cancel() { + Runnable localCb = null; + List cancelCbs = null; + List cancels = null; + synchronized(this) { + if(isSet()) { + return; + } + this.isSet = true; + this.isError = true; + this.isCancelled = true; + this.error = (new FDBException("operation_cancelled", 1101)); + localCb = this.callback; + cancelCbs = this.cancelCallbacks; + cancels = this.chainedCancels; + this.callback = null; + this.cancelCallbacks = null; + this.chainedCancels = null; + } + if(cancelCbs != null) { + for(Runnable r : cancelCbs) { + getExecutor().execute(r); + } + } + if(cancels != null) { + for(Cancellable c : cancels) { + c.cancel(); + } + } + if(localCb != null) + localCb.run(); + } + + + @Override + public synchronized boolean isSet() { + return isSet; + } + + + @Override + public synchronized boolean onCancelled(Runnable r) { + if(isCancelled) { + r.run(); + return true; + } + if(isSet()) { + return false; + } + if(this.cancelCallbacks == null) { + this.cancelCallbacks = new LinkedList(); + } + this.cancelCallbacks.add(r); + return true; + } + + @Override + public synchronized void onCancelledCancel(Cancellable c) { + if(isCancelled) { + c.cancel(); + return; + } + if(isSet()) { + return; + } + if(this.chainedCancels == null) { + this.chainedCancels = new LinkedList(); + } + this.chainedCancels.add(c); + } + + @Override + public T getIfDone() { + if(!isDone()) + throw new IllegalStateException("Future has not been set"); + + if(isError()) { + if (error instanceof Error) throw (Error)error; + if(error instanceof CloneableException) { + throw (RuntimeException)(((CloneableException)error).retargetClone()); + } + throw (RuntimeException)error; + } + + return value; + } + + @Override + public void blockInterruptibly() throws InterruptedException { + if(isDone()) + return; + + Runnable trigger = new SelfNotifier(); + onReady(trigger); + + synchronized (trigger) { + while(!isDone()) { + trigger.wait(); + } + } + } + + @Override + public void blockUntilReady() { + if(isDone()) + return; + + Runnable trigger = new SelfNotifier(); + onReady(trigger); + + boolean interrupted = false; + synchronized (trigger) { + while(!isDone()) { + try { + trigger.wait(); + } catch (InterruptedException e) { + interrupted = true; + } + } + } + if(interrupted) { + Thread.currentThread().interrupt(); + } + } + + @Override + public boolean isDone() { + return isSet(); + } + + @Override + public synchronized boolean isError() { + if(!isDone()) { + throw new IllegalStateException("Not yet set"); + } + return isError; + } + + @Override + public synchronized Throwable getError() { + if(!isError()) + throw new IllegalStateException("Not in an error state"); + return this.error; + } + + @Override + protected synchronized void registerSingleCallback(Runnable callback) { + if(isDone()) { + callback.run(); + return; + } + if(this.callback != null) + throw new IllegalStateException("Callback already registered"); + this.callback = callback; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/SettablePartial.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/SettablePartial.java new file mode 100644 index 0000000000..d62d9472ba --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/SettablePartial.java @@ -0,0 +1,45 @@ +/* + * SettablePartial.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +/** + * Used to communicate output to consumers of an asynchronous process. This is a + * specialization of {@link Settable} -- one that can also accept a checked + * {@link Exception} as a possible error outcome. + * + * @param the type of output of a successful process + */ +public interface SettablePartial extends Settable { + /** + * Set this {@code Settable} to an error. This call will invoke callbacks, and unblock + * all {@code Thread}s waiting on associated {@code Future}s. A {@code Settable} + * can be set only once and this call will + * throw an exception if this object is already set to a value or error. It + * is not, however, an error to call this method if the operation has been cancelled. + * If the task has been cancelled, this call will have no effect. + * + * @param error the error encountered in the course of this process + * + * @throws IllegalStateException if {@code set()} or {@code setError()} has + * already been called. + */ + public abstract void setError(Exception error); +} \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/SettablePartialFuture.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/SettablePartialFuture.java new file mode 100644 index 0000000000..f252adcd50 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/SettablePartialFuture.java @@ -0,0 +1,289 @@ +/* + * SettablePartialFuture.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.async; + +import java.util.LinkedList; +import java.util.List; +import java.util.concurrent.Executor; + +import com.apple.cie.foundationdb.FDBException; + +/** + * Represents a {@link PartialFuture} that can be fulfilled via the {@link SettablePartial} + * interface. + * + * {@code SettablePartialFuture} has a default {@code Executor} that is used when a + * {@code SettablePartialFuture} is created without passing this parameter, which runs + * callback functions immediately on the stack when the future is fulfilled. + * + * @see ReadyPartialFuture + * + * @param the output type of a successful process + */ +public class SettablePartialFuture extends AbstractPartialFuture implements SettablePartial { + private static final Executor DEFAULT_ES = new Executor() { + @Override + public void execute(Runnable command) { + command.run(); + } + }; + + /** + * Construct a {@code SettableFuture} with the default {@code Executor} + */ + public SettablePartialFuture() { + this(DEFAULT_ES); + } + + /** + * Construct a {@code SettableFuture} with a specified {@code Executor} + * + * @param executor the {@code Executor} with which to execute callbacks + */ + public SettablePartialFuture(Executor executor) { + super(executor); + } + + private List cancelCallbacks = null; + private List chainedCancels = null; + + private volatile boolean isSet = false; + private volatile boolean isError = false; + private volatile boolean isCancelled = false; + private T value = null; + private Throwable error = null; + + private Runnable callback = null; + + @Override + public void set(T value) { + Runnable localCb = null; + synchronized(this) { + if(isCancelled) { + return; + } + if(isSet()) { + throw new IllegalStateException("Settable already set"); + } + this.isSet = true; + this.isError = false; + this.value = value; + localCb = this.callback; + this.callback = null; + this.cancelCallbacks = null; + this.chainedCancels = null; + } + if(localCb != null) + localCb.run(); + } + + @Override + public void setError(Error error) { + setErrorInternal(error); + } + + @Override + public void setError(RuntimeException error) { + setErrorInternal(error); + } + + @Override + public void setError(Exception error) { + setErrorInternal(error); + } + + private void setErrorInternal(Throwable error) { + Runnable localCb = null; + synchronized(this) { + if(isCancelled) { + return; + } + if(isSet()) { + throw new IllegalStateException("Settable already set"); + } + this.isSet = true; + this.isError = true; + this.error = error; + localCb = this.callback; + this.callback = null; + this.cancelCallbacks = null; + this.chainedCancels = null; + } + if(localCb != null) + localCb.run(); + } + + @Override + public void cancel() { + Runnable localCb = null; + List cancelCbs = null; + List cancels = null; + synchronized(this) { + if(isSet()) { + return; + } + this.isSet = true; + this.isError = true; + this.isCancelled = true; + this.error = (new FDBException("operation_cancelled", 1101)); + localCb = this.callback; + cancelCbs = this.cancelCallbacks; + cancels = this.chainedCancels; + this.callback = null; + this.cancelCallbacks = null; + this.chainedCancels = null; + } + if(cancelCbs != null) { + for(Runnable r : cancelCbs) { + getExecutor().execute(r); + } + } + if(cancels != null) { + for(Cancellable c : cancels) { + c.cancel(); + } + } + if(localCb != null) + localCb.run(); + } + + + @Override + public synchronized boolean isSet() { + return isSet; + } + + @Override + public synchronized boolean onCancelled(Runnable r) { + if(isCancelled) { + r.run(); + return true; + } + if(isSet()) { + return false; + } + if(this.cancelCallbacks == null) { + this.cancelCallbacks = new LinkedList(); + } + this.cancelCallbacks.add(r); + return true; + } + + @Override + public synchronized void onCancelledCancel(Cancellable c) { + if(isCancelled) { + c.cancel(); + return; + } + if(isSet()) { + return; + } + if(this.chainedCancels == null) { + this.chainedCancels = new LinkedList(); + } + this.chainedCancels.add(c); + } + + @Override + public T getIfDone() throws Exception { + if(!isDone()) + throw new IllegalStateException("Future has not been set"); + + if(isError()) { + if (error instanceof Error) throw (Error)error; + if(error instanceof CloneableException) { + throw ((CloneableException)error).retargetClone(); + } + throw (Exception)error; + } + + return value; + } + + @Override + public void blockUntilReady() { + if(isDone()) + return; + + Runnable trigger = new SelfNotifier(); + onReady(trigger); + + boolean interrupted = false; + synchronized (trigger) { + while(!isDone()) { + try { + trigger.wait(); + } catch (InterruptedException e) { + interrupted = true; + } + } + } + if(interrupted) { + Thread.currentThread().interrupt(); + } + } + + @Override + public void blockInterruptibly() throws InterruptedException { + if(isDone()) + return; + + Runnable trigger = new SelfNotifier(); + onReady(trigger); + + synchronized (trigger) { + while(!isDone()) { + trigger.wait(); + } + } + } + + @Override + public boolean isDone() { + return isSet(); + } + + @Override + public synchronized boolean isError() { + if(!isDone()) { + throw new IllegalStateException("Not yet set"); + } + return isError; + } + + @Override + public synchronized Throwable getError() { + if(!isError()) + throw new IllegalStateException("Not in an error state"); + return this.error; + } + + @Override + protected synchronized void registerSingleCallback(Runnable callback) { + if(isDone()) { + callback.run(); + return; + } + if(this.callback != null) + throw new IllegalStateException("Callback already registered"); + this.callback = callback; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/async/package-info.java b/bindings/java/src/main/com/apple/cie/foundationdb/async/package-info.java new file mode 100644 index 0000000000..128f6161ab --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/async/package-info.java @@ -0,0 +1,59 @@ +/* + * package-info.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides a basic asynchronous programming library for Java. + *

    + * + *

    Concepts

    + * In this library, the concepts from computer science of {@code Future}/{@code Promise} + * are present and named {@link com.apple.cie.foundationdb.async.Future}/{@link com.apple.cie.foundationdb.async.Settable}. + * As a way to deal in a reasonable manner with the presence of checked Exceptions in + * Java (which essentially add information to the return signature of code) this library + * adds the idea of {@code Partial}. Named after the mathematical concept of partial + * functions -- which do not have an output defined for the full range of inputs -- + * {@link com.apple.cie.foundationdb.async.PartialFunction} are declared to throw + * {@link java.lang.Exception}s as some inputs will not have a valid output. Using + * the same names, {@link com.apple.cie.foundationdb.async.PartialFuture}s are the outputs + * of {@link com.apple.cie.foundationdb.async.PartialFunction}s and can have checked exceptions + * set as an unsuccessful output -- and those checked exceptions can be thrown from + * {@link com.apple.cie.foundationdb.async.PartialFuture#get()}. + * + *

    Practical Use

    + * Most client use of this library should either use a blocking structure with threads + * calling {@link com.apple.cie.foundationdb.async.Future#get()} or use the asynchronous "mapping" + * functions. Use {@link com.apple.cie.foundationdb.async.Future#map(Function)} when the output + * of a {@code Future} needs only simple, fast transformation (e.g. for decoding small + * pieces of data from one format to another). Use + * {@link com.apple.cie.foundationdb.async.Future#flatMap(Function)} when the output will serve + * as the input to another longer running asynchronous process. + * + *

    Cancellation

    + * {@link com.apple.cie.foundationdb.async.Future} extends the interface + * {@link com.apple.cie.foundationdb.async.Cancellable} and therefore can represent an operation + * that can be cancelled in some fashion. Cancelling a {@code Future} will cancel the + * process behind it, even if the {@code Future} being cancelled is a derived result + * of another process (see {@link com.apple.cie.foundationdb.async.Future#flatMap(Function)}). + * Therefore, {@code Future}s should only be cancelled when it is known that no other + * piece of code depends on the output of this source process. Calling + * {@code Future#cancel()} is the same as calling + * {@link com.apple.cie.foundationdb.async.Future#dispose()}. + */ +package com.apple.cie.foundationdb.async; diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/Directory.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/Directory.java new file mode 100644 index 0000000000..7142e6e707 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/Directory.java @@ -0,0 +1,353 @@ +/* + * Directory.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +import com.apple.cie.foundationdb.ReadTransactionContext; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.async.Future; + +/** + * Represents a directory in the {@code DirectoryLayer}. A {@code Directory} stores the path + * at which it is located and the layer that was used to create it. + * + * The {@code Directory} interface contains methods to operate on itself and its + * subdirectories. + */ +public interface Directory { + + /** + * Gets the path represented by this {@code Directory}. + * + * @return this {@code Directory}'s path + */ + List getPath(); + + /** + * Gets the layer byte string that was stored when this {@code Directory} + * was created. + * + * @return this {@code Directory}'s layer byte string + */ + byte[] getLayer(); + + /** + * Get the {@link DirectoryLayer} that was used to create this {@code Directory}. + * + * @return the {@link DirectoryLayer} that created this {@link Directory} + */ + DirectoryLayer getDirectoryLayer(); + + /** + * Creates or opens the subdirectory of this {@code Directory} located at {@code subpath} + * (creating parent directories, if necessary). + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link Future} which will be set to the created or opened {@link DirectorySubspace} + */ + Future createOrOpen(TransactionContext tcx, List subpath); + + /** + * Creates or opens the subdirectory of this {@code Directory} located at {@code subpath} + * (creating parent directories, if necessary). If the directory is new, then the {@code layer} + * byte string will be recorded as its layer. If the directory already exists, the {@code layer} + * byte string will be compared against the {@code layer} set when the directory was created. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link MismatchedLayerException} - if the directory has already been created with a different {@code layer} byte string
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @param layer a {@code byte[]} specifying a layer to set on a new directory or check for on an existing directory + * @return a {@link Future} which will be set to the created or opened {@link DirectorySubspace} + */ + Future createOrOpen(TransactionContext tcx, List subpath, byte[] layer); + + /** + * Opens the subdirectory of this {@code Directory} located at {@code subpath}. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if the directory does not exist
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link Future} which will be set to the opened {@link DirectorySubspace} + */ + Future open(ReadTransactionContext tcx, List subpath); + + /** + * Opens the subdirectory of this {@code Directory} located at {@code subpath}. + * The {@code layer} byte string will be compared against the {@code layer} set when + * the directory was created. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link MismatchedLayerException} - if the directory was created with a different {@code layer} byte string
    • + *
    • {@link NoSuchDirectoryException} - if the directory does not exist
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @param layer a {@code byte[]} specifying the expected layer + * @return a {@link Future} which will be set to the opened {@link DirectorySubspace} + */ + Future open(ReadTransactionContext tcx, List subpath, byte[] layer); + + /** + * Creates a subdirectory of this {@code Directory} located at {@code subpath} + * (creating parent directories if necessary). + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link DirectoryAlreadyExistsException} - if the given directory already exists
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link Future} which will be set to the created {@link DirectorySubspace} + */ + Future create(TransactionContext tcx, List subpath); + + /** + * Creates a subdirectory of this {@code Directory} located at {@code subpath} + * (creating parent directories if necessary). The {@code layer} byte string will be recorded as + * the new directory's layer and checked by future calls to {@link #open(ReadTransactionContext, List, byte[])}. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link DirectoryAlreadyExistsException} - if the given directory already exists
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @param layer a {@code byte[]} specifying a layer to set for the directory + * @return a {@link Future} which will be set to the created {@link DirectorySubspace} + */ + Future create(TransactionContext tcx, List subpath, byte[] layer); + + /** + * Creates a subdirectory of this {@code Directory} located at {@code subpath} + * (creating parent directories if necessary). The {@code layer} byte string will be recorded as + * the new directory's layer and checked by future calls to {@link #open(ReadTransactionContext, List, byte[])}. + * The specified {@code prefix} will be used for this directory's contents instead of allocating a + * prefix automatically. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link DirectoryAlreadyExistsException} - if the given directory already exists
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @param layer a {@code byte[]} specifying a layer to set for the directory + * @param prefix a {@code byte[]} specifying the key prefix to use for the directory's contents + * @return a {@link Future} which will be set to the created {@link DirectorySubspace} + */ + Future create(TransactionContext tcx, List subpath, byte[] layer, byte[] prefix); + + /** + * Moves this {@code Directory} to the specified {@code newAbsolutePath}. + *

    + * There is no effect on the physical prefix of the given directory, or on clients that already + * have the directory open. + *

    + *

    + * It is invalid to move a directory to: + *

    + *
      + *
    • A location where a directory already exists
    • + *
    • A location whose parent does not exist
    • + *
    • A subdirectory of itself
    • + *
    • A different partition
    • + *
    + * + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if this {@code Directory} doesn't exist
    • + *
    • {@link DirectoryAlreadyExistsException} - if a directory already exists at {@code newAbsolutePath}
    • + *
    • {@link DirectoryMoveException} - if an invalid move location is specified
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param newAbsolutePath a {@code List} specifying the new absolute path for this {@code Directory} + * @return a {@link Future} which will be set to the {@link DirectorySubspace} for this {@code Directory} + * at its new location. + */ + Future moveTo(TransactionContext tcx, List newAbsolutePath); + + /** + * Moves the subdirectory of this {@code Directory} located at {@code oldSubpath} to {@code newSubpath}. + * + *

    + * There is no effect on the physical prefix of the given directory, or on clients that already + * have the directory open. + *

    + *

    + * It is invalid to move a directory to: + *

    + *
      + *
    • A location where a directory already exists
    • + *
    • A location whose parent does not exist
    • + *
    • A subdirectory of itself
    • + *
    • A different partition
    • + *
    + * + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no {@code Directory} exists at {@code oldSubpath}
    • + *
    • {@link DirectoryAlreadyExistsException} - if a directory already exists at {@code newSubpath}
    • + *
    • {@link DirectoryMoveException} - if an invalid move location is specified
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param oldSubpath a {@code List} specifying the subpath of the directory to move + * @param newSubpath a {@code List} specifying the subpath to move to + * @return a {@link Future} which will be set to the {@link DirectorySubspace} for this {@code Directory} + * at its new location. + */ + Future move(TransactionContext tcx, List oldSubpath, List newSubpath); + + /** + * Removes this {@code Directory} and all of its subdirectories, as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if this {@code Directory} doesn't exist
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @return a {@link Future} which will be set once this {@code Directory} has been removed + */ + Future remove(TransactionContext tcx); + + /** + * Removes the subdirectory of this {@code Directory} located at {@code subpath} and all of its subdirectories, + * as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no directory exists at {@code subpath}
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link Future} which will be set once the {@code Directory} has been removed + */ + Future remove(TransactionContext tcx, List subpath); + + /** + * Removes this {@code Directory} and all of its subdirectories, as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @return a {@link Future} which will be set to true once this {@code Directory} has been removed, + * or false if it didn't exist. + */ + Future removeIfExists(TransactionContext tcx); + + /** + * Removes the subdirectory of this {@code Directory} located at {@code subpath} and all of its subdirectories, + * as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link Future} which will be set to true once the {@code Directory} has been removed, + * or false if it didn't exist. + */ + Future removeIfExists(TransactionContext tcx, List subpath); + + /** + * List the subdirectories of this directory. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if this {@code Directory} doesn't exists
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @return a {@link Future} which will be set to a {@code List} of names of the subdirectories + * of this {@code Directory}. Each name is a unicode string representing the last component of a + * subdirectory's path. + */ + Future> list(ReadTransactionContext tcx); + + /** + * List the subdirectories of this directory at a given {@code subpath}. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no directory exists at {@code subpath}
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link Future} which will be set to a {@code List} of names of the subdirectories + * of the directory at {@code subpath}. Each name is a unicode string representing the last component + * of a subdirectory's path. + */ + Future> list(ReadTransactionContext tcx, List subpath); + + /** + * Checks if this {@code Directory} exists. + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @return a {@link Future} which will be set to {@code true} if this {@code Directory} exists, or {@code false} if it + * doesn't + */ + Future exists(ReadTransactionContext tcx); + + /** + * Checks if the subdirectory of this {@code Directory} located at {@code subpath} exists. + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param subpath a {@code List} specifying a subpath of this {@code Directory} + * @return a {@link Future} which will be set to {@code true} if the specified subdirectory exists, or {@code false} if it + * doesn't + */ + Future exists(ReadTransactionContext tcx, List subpath); +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryAlreadyExistsException.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryAlreadyExistsException.java new file mode 100644 index 0000000000..7f4e64f4f1 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryAlreadyExistsException.java @@ -0,0 +1,35 @@ +/* + * DirectoryAlreadyExistsException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +/** + * A {@link DirectoryException} that is thrown when a directory is being + * created at or moved to a location that already has a directory present. + */ +@SuppressWarnings("serial") +public class DirectoryAlreadyExistsException extends DirectoryException { + DirectoryAlreadyExistsException(List path) { + super("Directory already exists", path); + } +} + diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryException.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryException.java new file mode 100644 index 0000000000..a99e925587 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryException.java @@ -0,0 +1,40 @@ +/* + * DirectoryException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +/** + * An {@link Exception} that is thrown by the directory layer corresponding + * to a specific path. + */ +@SuppressWarnings("serial") +public class DirectoryException extends RuntimeException { + /** + * The directory path that was responsible for the error. + */ + public final List path; + + DirectoryException(String baseMsg, List path) { + super(baseMsg + ": path=" + DirectoryUtil.pathStr(path)); + this.path = path; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryLayer.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryLayer.java new file mode 100644 index 0000000000..da64f48a7e --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryLayer.java @@ -0,0 +1,1450 @@ +/* + * DirectoryLayer.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import static com.apple.cie.foundationdb.tuple.ByteArrayUtil.join; + +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +import com.apple.cie.foundationdb.subspace.Subspace; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.MutationType; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.ReadTransaction; +import com.apple.cie.foundationdb.ReadTransactionContext; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncIterator; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.ReadyFuture; + +/** + * Provides a class for managing directories in FoundationDB. + * + *

    + * The FoundationDB API provides directories as a tool for managing related + * {@link Subspace}s. Directories are a recommended approach for administering + * applications. Each application should create or open at least one directory + * to manage its subspaces. + * + * For general guidance on directory usage, see the discussion in the + * Developer Guide. + *

    + *

    + * Directories are identified by hierarchical paths analogous to the paths + * in a Unix-like file system. A path is represented as a List of strings. + * Each directory has an associated subspace used to store its content. The + * layer maps each path to a short prefix used for the corresponding + * subspace. In effect, directories provide a level of indirection for + * access to subspaces. + *

    + */ +public class DirectoryLayer implements Directory +{ + private static final Charset UTF_8 = Charset.forName("UTF-8"); + private static final byte[] LITTLE_ENDIAN_LONG_ONE = { 1, 0, 0, 0, 0, 0, 0, 0 }; + private static final byte[] HIGH_CONTENTION_KEY = "hca".getBytes(UTF_8); + private static final byte[] LAYER_KEY = "layer".getBytes(UTF_8); + private static final byte[] VERSION_KEY = "version".getBytes(UTF_8); + private static final long SUB_DIR_KEY = 0; + private static final Integer[] VERSION = { 1, 0, 0 }; + + static final byte[] EMPTY_BYTES = new byte[0]; + static final List EMPTY_PATH = Collections.emptyList(); + static final byte[] DEFAULT_NODE_SUBSPACE_PREFIX = { (byte)0xFE }; + + /** + * The default node {@link Subspace} used by a {@code DirectoryLayer} when none is specified. + */ + public static final Subspace DEFAULT_NODE_SUBSPACE = new Subspace(DEFAULT_NODE_SUBSPACE_PREFIX); + + /** + * The default content {@link Subspace} used by a {@code DirectoryLayer} when none is specified. + */ + public static final Subspace DEFAULT_CONTENT_SUBSPACE = new Subspace(); + + private final Subspace rootNode; + private final Subspace nodeSubspace; + private final Subspace contentSubspace; + private final HighContentionAllocator allocator; + private final boolean allowManualPrefixes; + + private List path = EMPTY_PATH; + /** + * The layer string to pass to {@link Directory#createOrOpen(TransactionContext, List, byte[])} or + * {@link Directory#create(TransactionContext, List, byte[])} to create a {@code DirectoryPartition}. + */ + public static final byte[] PARTITION_LAYER = "partition".getBytes(Charset.forName("UTF-8")); + + private static DirectoryLayer defaultDirectoryLayer = new DirectoryLayer(); + + /** + * Constructor for a {@code DirectoryLayer} formed with default node and + * content subspaces. A {@code DirectoryLayer} + * defines a new root directory. The node subspace and content subspace + * control where the directory metadata and contents, respectively, are + * stored. The default root directory has a node subspace with raw prefix + * {@code \xFE} and a content subspace with no prefix. + * Prefixes can not be specified in calls to {@link Directory#create(TransactionContext, List, byte[], byte[])}. + * + * @see #getDefault + */ + public DirectoryLayer() { + this(DEFAULT_NODE_SUBSPACE, DEFAULT_CONTENT_SUBSPACE, false); + } + + /** + * Constructor for a {@code DirectoryLayer} formed with default node and + * content subspaces. A {@code DirectoryLayer} + * defines a new root directory. The node subspace and content subspace + * control where the directory metadata and contents, respectively, are + * stored. The default root directory has a node subspace with raw prefix + * {@code \xFE} and a content subspace with no prefix. + * + * @param allowManualPrefixes whether or not prefixes can be specified in calls to + * {@link Directory#create(TransactionContext, List, byte[], byte[])} + */ + public DirectoryLayer(boolean allowManualPrefixes) { + this(DEFAULT_NODE_SUBSPACE, DEFAULT_CONTENT_SUBSPACE, allowManualPrefixes); + } + + /** + * Constructor for a {@code DirectoryLayer} formed with a specified node + * subspace and specified content subspace. A {@code DirectoryLayer} + * defines a new root directory. The node subspace and content subspace + * control where the directory metadata and contents, respectively, are + * stored. The default root directory has a node subspace with raw prefix + * {@code \xFE} and a content subspace with no prefix. Specifying more + * restrictive values for the node subspace and content subspace will allow + * using the directory layer alongside other content in a database. + * Prefixes can not be specified in calls to {@link Directory#create(TransactionContext, List, byte[], byte[])}. + * + * @param nodeSubspace a {@link Subspace} used to store directory metadata + * @param contentSubspace a {@link Subspace} used to store directory content + */ + public DirectoryLayer(Subspace nodeSubspace, Subspace contentSubspace) { + this(nodeSubspace, contentSubspace, false); + } + + /** + * Constructor for a {@code DirectoryLayer} formed with a specified node + * subspace and specified content subspace. A {@code DirectoryLayer} + * defines a new root directory. The node subspace and content subspace + * control where the directory metadata and contents, respectively, are + * stored. The default root directory has a node subspace with raw prefix + * {@code \xFE} and a content subspace with no prefix. Specifying more + * restrictive values for the node subspace and content subspace will allow + * using the directory layer alongside other content in a database. + * + * @param nodeSubspace a {@link Subspace} used to store directory metadata + * @param contentSubspace a {@link Subspace} used to store directory content + * @param allowManualPrefixes whether or not prefixes can be specified in calls to + * {@link Directory#create(TransactionContext, List, byte[], byte[])} + */ + public DirectoryLayer(Subspace nodeSubspace, Subspace contentSubspace, boolean allowManualPrefixes) { + this.nodeSubspace = nodeSubspace; + this.contentSubspace = contentSubspace; + // The root node is the one whose contents are the node subspace + this.rootNode = nodeSubspace.get(nodeSubspace.getKey()); + this.allocator = new HighContentionAllocator(rootNode.get(HIGH_CONTENTION_KEY)); + this.allowManualPrefixes = allowManualPrefixes; + } + + /** + * Creates a new {@code DirectoryLayer} formed with a specified node subspace and default content subspace. + * Prefixes can not be specified in calls to {@link Directory#create(TransactionContext, List, byte[], byte[])}. + * + * @param node_subspace a {@link Subspace} used to store directory metadata + * @return a {@code DirectoryLayer} formed with {@code node_subspace} and a default content subspace + */ + public static Directory createWithNodeSubspace(Subspace node_subspace) { + return new DirectoryLayer(node_subspace, DEFAULT_CONTENT_SUBSPACE); + } + + /** + * Creates a new {@code DirectoryLayer} formed with a default node subspace and specified content subspace. + * Prefixes can not be specified in calls to {@link Directory#create(TransactionContext, List, byte[], byte[])}. + * + * @param content_subspace a {@link Subspace} used to store directory content + * @return a {@code DirectoryLayer} formed with a {@code content_subspace} and a default node subspace + */ + public static Directory createWithContentSubspace(Subspace content_subspace) { + return new DirectoryLayer(DEFAULT_NODE_SUBSPACE, content_subspace); + } + + /** + * Gets the default instance of the DirectoryLayer. The default instance + * is created with the default node and content subspaces. + * + * Prefixes can not be specified in calls to {@link Directory#create(TransactionContext, List, byte[], byte[])}. + * + * @return the default {@code DirectoryLayer} + */ + public static DirectoryLayer getDefault() { + return defaultDirectoryLayer; + } + + /** + * Tests whether this {@code DirectoryLayer} is equal to {@code rhs}. + * Two {@code DirectoryLayer}s are equal if they have the same node subspace, + * content subspace, and path. + * + * @param rhs the object to check for equality + * @return {@code true} if this {@code DirectoryLayer} and {@code rhs} are equal + */ + @Override + public boolean equals(Object rhs) { + if(this == rhs) { + return true; + } + if(rhs == null || getClass() != rhs.getClass()) { + return false; + } + DirectoryLayer other = (DirectoryLayer)rhs; + + return (path == other.path || path.equals(other.path)) + && nodeSubspace.equals(other.nodeSubspace) + && contentSubspace.equals(other.contentSubspace); + } + + /** + * Sets path of directory to {@code path} + * + * @param path a {@code List} specifying a path + */ + void setPath(List path) { + this.path = path; + } + + /** + * Gets the path for the root node of this {@code DirectoryLayer}. Normally constructed + * {@code DirectoryLayer}s have an empty path, but {@code DirectoryLayer}s returned by + * {@link Directory#getDirectoryLayer} for {@link Directory}s inside of a partition + * could have non-empty paths. + * + * @return the path for the root node of this {@code DirectoryLayer} + */ + @Override + public List getPath() { + return Collections.unmodifiableList(path); + } + + /** + * Returns the layer byte string for this {@code DirectoryLayer}, which is always an empty + * array. + * + * @return an empty byte array + */ + @Override + public byte[] getLayer() { + return EMPTY_BYTES; + } + + /** + * Returns {@code this}. + * + * @return {@code this} + */ + @Override + public DirectoryLayer getDirectoryLayer() { + return this; + } + + /** + * Creates or opens the directory located at {@code path} (creating parent directories, if necessary). + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @return a {@link Future} which will be set to the created or opened {@link DirectorySubspace} + */ + @Override + public Future createOrOpen(TransactionContext tcx, List path) { + return createOrOpen(tcx, path, EMPTY_BYTES); + } + + /** + * Creates or opens the directory located at {@code path}(creating parent directories, if necessary). + * If the directory is new, then the {@code layer} byte string will be recorded as its layer. + * If the directory already exists, the {@code layer} byte string will be compared against the {@code layer} + * set when the directory was created. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link MismatchedLayerException} - if the directory has already been created with a different {@code layer} byte string
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @param layer a {@code byte[]} specifying a layer to set on a new directory or check for on an existing directory + * @return a {@link Future} which will be set to the created or opened {@link DirectorySubspace} + */ + @Override + public Future createOrOpen(TransactionContext tcx, final List path, final byte[] layer) { + return tcx.runAsync(new Function>() { + @Override + public Future apply(Transaction tr) { + return createOrOpenInternal(tr, tr, path, layer, null, true, true); + } + }); + } + + /** + * Opens the directory located at {@code path}. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if the directory does not exist
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @return a {@link Future} which will be set to the opened {@link DirectorySubspace} + */ + @Override + public Future open(ReadTransactionContext tcx, List path) { + return open(tcx, path, EMPTY_BYTES); + } + + /** + * Opens the directory located at {@code path}. + * The {@code layer} byte string will be compared against the {@code layer} set when + * the directory was created. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link MismatchedLayerException} - if the directory was created with a different {@code layer} byte string
    • + *
    • {@link NoSuchDirectoryException} - if the directory does not exist
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @param layer a {@code byte[]} specifying the expected layer + * @return a {@link Future} which will be set to the opened {@link DirectorySubspace} + */ + @Override + public Future open(ReadTransactionContext tcx, final List path, final byte[] layer) { + return tcx.readAsync(new Function>() { + @Override + public Future apply(ReadTransaction rtr) { + return createOrOpenInternal(rtr, null, path, layer, null, false, true); + } + }); + } + + /** + * Creates a directory located at {@code path} (creating parent directories if necessary). + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link DirectoryAlreadyExistsException} - if the given directory already exists
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @return a {@link Future} which will be set to the created {@link DirectorySubspace} + */ + @Override + public Future create(TransactionContext tcx, List path) { + return create(tcx, path, EMPTY_BYTES, null); + } + + /** + * Creates a directory located at {@code path} (creating parent directories if necessary). + * The {@code layer} byte string will be recorded as the new directory's layer and checked by + * future calls to {@link #open(ReadTransactionContext, List, byte[])}. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link DirectoryAlreadyExistsException} - if the given directory already exists
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path of this {@code Directory} + * @param layer a {@code byte[]} specifying a layer to set for the directory + * @return a {@link Future} which will be set to the created {@link DirectorySubspace} + */ + @Override + public Future create(TransactionContext tcx, List path, byte[] layer) { + return create(tcx, path, layer, null); + } + + /** + * Creates a directory located at {@code path} (creating parent directories if necessary). + * The {@code layer} byte string will be recorded as the new directory's layer and checked by + * future calls to {@link #open(ReadTransactionContext, List, byte[])}. The specified {@code prefix} + * will be used for this directory's contents instead of allocating a prefix automatically. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link DirectoryAlreadyExistsException} - if the given directory already exists
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path of this {@code Directory} + * @param layer a {@code byte[]} specifying a layer to set for the directory + * @param prefix a {@code byte[]} specifying the key prefix to use for the directory's contents + * @return a {@link Future} which will be set to the created {@link DirectorySubspace} + */ + @Override + public Future create(TransactionContext tcx, final List path, final byte[] layer, final byte[] prefix) { + return tcx.runAsync(new Function>() { + @Override + public Future apply(Transaction tr) { + return createOrOpenInternal(tr, tr, path, layer, prefix, true, false); + } + }); + } + + /** + * This method should not be called on a {@code DirectoryLayer}. Calling this method will result in the returned + * {@link Future} being set to a {@link DirectoryMoveException}. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link DirectoryMoveException}
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param newAbsolutePath a {@code List} specifying a path + * @return a {@link Future} which will be set to a {@link DirectoryMoveException} + */ + @Override + public Future moveTo(TransactionContext tcx, List newAbsolutePath) { + return new ReadyFuture(new DirectoryMoveException("The root directory cannot be moved.", path, newAbsolutePath)); + } + + /** + * Moves the directory located at {@code oldPath} to {@code newPath}. + * + *

    + * There is no effect on the physical prefix of the given directory, or on clients that already + * have the directory open. + *

    + *

    + * It is invalid to move a directory to: + *

    + *
      + *
    • A location where a directory already exists
    • + *
    • A location whose parent does not exist
    • + *
    • A subdirectory of itself
    • + *
    • A different partition
    • + *
    + * + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no {@code Directory} exists at {@code oldPath}
    • + *
    • {@link DirectoryAlreadyExistsException} - if a directory already exists at {@code newPath}
    • + *
    • {@link DirectoryMoveException} - if an invalid move location is specified
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param oldPath a {@code List} specifying the path of the directory to move + * @param newPath a {@code List} specifying the path to move to + * @return a {@link Future} which will be set to the {@link DirectorySubspace} for this {@code Directory} + * at its new location. + */ + @Override + public Future move(final TransactionContext tcx, final List oldPath, final List newPath) { + final List oldPathCopy = new ArrayList(oldPath); + final List newPathCopy = new ArrayList(newPath); + + return tcx.runAsync(new Function>() { + @Override + public Future apply(final Transaction tr) { + return checkOrWriteVersion(tr) + .flatMap(new Function>>() { + @Override + public Future> apply(Void ignore) { + if(oldPathCopy.size() <= newPathCopy.size() && oldPathCopy.equals(newPathCopy.subList(0, oldPathCopy.size()))) + throw new DirectoryMoveException("The destination directory cannot be a subdirectory of the source directory.", toAbsolutePath(oldPathCopy), toAbsolutePath(newPathCopy)); + + ArrayList> futures = new ArrayList>(); + futures.add(new NodeFinder(oldPathCopy).find(tr).flatMap(new NodeMetadataLoader(tr))); + futures.add(new NodeFinder(newPathCopy).find(tr).flatMap(new NodeMetadataLoader(tr))); + + return AsyncUtil.getAll(futures); + } + }) + .flatMap(new Function, Future>() { + @Override + public Future apply(List nodes) { + final Node oldNode = nodes.get(0); + final Node newNode = nodes.get(1); + + if(!oldNode.exists()) + throw new NoSuchDirectoryException(toAbsolutePath(oldPathCopy)); + + if(oldNode.isInPartition(false) || newNode.isInPartition(false)) { + if(!oldNode.isInPartition(false) || !newNode.isInPartition(false) || !oldNode.path.equals(newNode.path)) + throw new DirectoryMoveException("Cannot move between partitions.", toAbsolutePath(oldPathCopy), toAbsolutePath(newPathCopy)); + + return newNode.getContents().move(tr, oldNode.getPartitionSubpath(), newNode.getPartitionSubpath()); + } + + if(newNode.exists()) + throw new DirectoryAlreadyExistsException(toAbsolutePath(newPathCopy)); + + final List parentPath = PathUtil.popBack(newPathCopy); + return new NodeFinder(parentPath).find(tr) + .flatMap(new Function>() { + @Override + public Future apply(Node parentNode) { + if(!parentNode.exists()) + throw new NoSuchDirectoryException(toAbsolutePath(parentPath)); + + tr.set( + parentNode.subspace.get(SUB_DIR_KEY).get(getLast(newPathCopy)).getKey(), + contentsOfNode(oldNode.subspace, EMPTY_PATH, EMPTY_BYTES).getKey() + ); + + return removeFromParent(tr, oldPathCopy) + .map(new Function() { + @Override + public DirectorySubspace apply(Void ignore) { + return contentsOfNode(oldNode.subspace, newPathCopy, oldNode.layer); + } + }); + } + }); + } + }); + } + }); + } + + /** + * This method should not be called on the root directory. Calling this method will result in the returned + * {@link Future} being set to a {@link DirectoryException}. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link DirectoryException}
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @return a {@link Future} which will be set to a {@link DirectoryException} + */ + @Override + public Future remove(TransactionContext tcx) { + return remove(tcx, EMPTY_PATH); + } + + /** + * Removes the directory located at {@code path} and all of its subdirectories, + * as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no directory exists at {@code path}
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @return a {@link Future} which will be set once the {@code Directory} has been removed + */ + @Override + public Future remove(TransactionContext tcx, List path) { + return AsyncUtil.success(removeInternal(tcx, path, true)); + } + + /** + * This method should not be called on the root directory. Calling this method will result in the returned + * {@link Future} being set to a {@link DirectoryException}. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link DirectoryException}
    • + *
    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @return a {@link Future} which will be set to a {@link DirectoryException} + */ + @Override + public Future removeIfExists(TransactionContext tcx) { + return removeIfExists(tcx, EMPTY_PATH); + } + + /** + * Removes the directory located at {@code subpath} and all of its subdirectories, + * as well as all of their contents. + * + *

    + * Warning: Clients that have already opened the directory might + * still insert data into its contents after it is removed. + *

    + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @return a {@link Future} which will be set to true once the {@code Directory} has been removed, + * or false if it didn't exist. + */ + @Override + public Future removeIfExists(TransactionContext tcx, List path) { + return removeInternal(tcx, path, false); + } + + /** + * List the subdirectories of the root directory. + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @return a {@link Future} which will be set to a {@code List} of names of the subdirectories + * of the root directory. Each name is a unicode string representing the last component of a + * subdirectory's path. + */ + @Override + public Future> list(ReadTransactionContext tcx) { + return list(tcx, EMPTY_PATH); + } + + /** + * List the subdirectories of the directory at a given {@code path}. + * + *

    The returned {@link Future} can be set to the following errors:

    + *
      + *
    • {@link NoSuchDirectoryException} - if no directory exists at {@code path}
    • + *
    + * + * @param tcx the {@link ReadTransactionContext} to execute this operation in + * @param path a {@code List} specifying a path + * @return a {@link Future} which will be set to a {@code List} of names of the subdirectories + * of the directory at {@code path}. Each name is a unicode string representing the last component + * of a subdirectory's path. + */ + @Override + public Future> list(final ReadTransactionContext tcx, final List path) { + final List pathCopy = new ArrayList(path); + + return tcx.readAsync(new Function>>() { + @Override + public Future> apply(final ReadTransaction tr) { + return checkVersion(tr) + .flatMap(new Function>() { + @Override + public Future apply(Void ignore) { + return new NodeFinder(pathCopy).find(tr).flatMap(new NodeMetadataLoader(tr)); + } + }) + .flatMap(new Function>>() { + @Override + public Future> apply(Node node) { + if(!node.exists()) + throw new NoSuchDirectoryException(toAbsolutePath(pathCopy)); + + if(node.isInPartition(true)) + return node.getContents().list(tr, node.getPartitionSubpath()); + + final Subspace subdir = node.subspace.get(SUB_DIR_KEY); + + return AsyncUtil.collect( + AsyncUtil.mapIterable(tr.getRange(subdir.range()), + new Function() { + @Override + public String apply(KeyValue o) { + return subdir.unpack(o.getKey()).getString(0); + } + })); + } + }); + } + }); + } + + /** + * Returns {@code true}. + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @return {@code true} + */ + @Override + public Future exists(ReadTransactionContext tcx) { + return new ReadyFuture(true); + } + + /** + * Checks if the directory located at {@code path} exists. + * + * @param tcx the {@link TransactionContext} to execute this operation in + * @param path a {@code List} specifying a path of this {@code Directory} + * @return a {@link Future} which will be set to {@code true} if the specified directory exists, or {@code false} if it + * doesn't + */ + @Override + public Future exists(final ReadTransactionContext tcx, final List path) { + final List pathCopy = new ArrayList(path); + + return tcx.readAsync(new Function>() { + @Override + public Future apply(final ReadTransaction tr) { + return checkVersion(tr) + .flatMap(new Function>() { + @Override + public Future apply(Void ignore) { + return new NodeFinder(pathCopy).find(tr).flatMap(new NodeMetadataLoader(tr)); + }; + }) + .flatMap(new Function>() { + @Override + public Future apply(Node node) { + if(!node.exists()) + return new ReadyFuture(false); + else if(node.isInPartition(false)) + return node.getContents().exists(tr, node.getPartitionSubpath()); + + return new ReadyFuture(true); + } + }); + } + }); + } + + // + // Internal + // + + private Subspace nodeWithPrefix(byte[] prefix) { + if(prefix == null) { + return null; + } + return nodeSubspace.get(prefix); + } + + private Future nodeContainingKey(final ReadTransaction tr, final byte[] key) { + // Right now this is only used for _is_prefix_free(), but if we add + // parent pointers to directory nodes, it could also be used to find a + // path based on a key. + if(ByteArrayUtil.startsWith(key, nodeSubspace.getKey())) { + return new ReadyFuture(rootNode); + } + + return tr.getRange(nodeSubspace.range().begin, ByteArrayUtil.join(nodeSubspace.pack(key), new byte[]{0x00}), 1, true) + .asList() + .map(new Function, Subspace>() { + @Override + public Subspace apply(List results) { + if(results.size() > 0) { + byte[] resultKey = results.get(0).getKey(); + byte[] prevPrefix = nodeSubspace.unpack(resultKey).getBytes(0); + if(ByteArrayUtil.startsWith(key, prevPrefix)) { + return nodeWithPrefix(prevPrefix); + } + } + + return null; + } + }); + } + + private List toAbsolutePath(List subPath) { + return PathUtil.join(path, subPath); + } + + private DirectorySubspace contentsOfNode(Subspace node, List path, byte[] layer) { + byte[] prefix = nodeSubspace.unpack(node.getKey()).getBytes(0); + + if(Arrays.equals(layer, DirectoryLayer.PARTITION_LAYER)) + return new DirectoryPartition(toAbsolutePath(path), prefix, this); + else + return new DirectorySubspace(toAbsolutePath(path), prefix, this, layer); + } + + private Future removeInternal(final TransactionContext tcx, final List path, final boolean mustExist) { + final List pathCopy = new ArrayList(path); + + return tcx.runAsync(new Function>() { + @Override + public Future apply(final Transaction tr) { + return checkOrWriteVersion(tr) + .flatMap(new Function>() { + @Override + public Future apply(Void ignore) { + if(pathCopy.size() == 0) + throw new DirectoryException("The root directory cannot be removed.", toAbsolutePath(pathCopy)); + + return new NodeFinder(pathCopy).find(tr).flatMap(new NodeMetadataLoader(tr)); + } + }) + .flatMap(new Function>() { + @Override + public Future apply(Node node) { + if(!node.exists()) { + if(mustExist) + throw new NoSuchDirectoryException(toAbsolutePath(pathCopy)); + else + return new ReadyFuture(false); + } + + if(node.isInPartition(false)) + return node.getContents().getDirectoryLayer().removeInternal(tr, node.getPartitionSubpath(), mustExist); + else { + ArrayList> futures = new ArrayList>(); + futures.add(removeRecursive(tr, node.subspace)); + futures.add(removeFromParent(tr, pathCopy)); + + return AsyncUtil.tag(AsyncUtil.whenAll(futures), true); + } + } + }); + } + }); + } + + private Future removeFromParent(final Transaction tr, final List path) { + return new NodeFinder(PathUtil.popBack(path)).find(tr) + .map(new Function() { + @Override + public Void apply(Node parent) { + tr.clear(parent.subspace.get(SUB_DIR_KEY).get(getLast(path)).getKey()); + return null; + } + }); + } + + private Future removeRecursive(final Transaction tr, final Subspace node) { + Subspace subdir = node.get(SUB_DIR_KEY); + final AsyncIterator rangeItr = tr.getRange(subdir.range()).iterator(); + + tr.clear(Range.startsWith(nodeSubspace.unpack(node.getKey()).getBytes(0))); + tr.clear(node.range()); + + return AsyncUtil.whileTrue(new Function>() { + @Override + public Future apply(Void ignore) { + Future subdirRemoveFuture; + if(rangeItr.onHasNext().isDone() && rangeItr.hasNext()) + subdirRemoveFuture = removeRecursive(tr, nodeWithPrefix(rangeItr.next().getValue())); + else + subdirRemoveFuture = ReadyFuture.DONE; + + return subdirRemoveFuture + .flatMap(new Function>() { + @Override + public Future apply(Void ignore) { + return rangeItr.onHasNext(); + } + }); + } + }); + } + + private Future isPrefixFree(final ReadTransaction tr, final byte[] prefix) { + // Returns true if the given prefix does not "intersect" any currently + // allocated prefix (including the root node). This means that it neither + // contains any other prefix nor is contained by any other prefix. + if(prefix == null || prefix.length == 0) + return new ReadyFuture(false); + + return nodeContainingKey(tr, prefix). + flatMap(new Function>() { + @Override + public Future apply(Subspace node) { + if(node != null) + return new ReadyFuture(false); + + AsyncIterator it = tr.getRange(nodeSubspace.pack(prefix), nodeSubspace.pack(ByteArrayUtil.strinc(prefix)), 1).iterator(); + return it.onHasNext() + .map(new Function() { + @Override + public Boolean apply(Boolean hasNext) { + return !hasNext; + } + }); + } + }); + } + + private Future getVersionValue(final ReadTransaction tr) { + return tr.get(rootNode.pack(VERSION_KEY)); + } + + private Future checkOrWriteVersion(final Transaction tr) { + return getVersionValue(tr).map(new WritableVersionCheck(tr)); + } + + private Future checkVersion(final ReadTransaction tr) { + return getVersionValue(tr).map(new VersionCheck()); + } + + private Future createOrOpenInternal(final ReadTransaction rtr, + final Transaction tr, + final List path, + final byte[] layer, + final byte[] prefix, + final boolean allowCreate, + final boolean allowOpen) + { + final List pathCopy = new ArrayList(path); + + if(prefix != null && !allowManualPrefixes) { + String errorMessage; + if(this.path.size() == 0) + errorMessage = "Cannot specify a prefix unless manual prefixes are enabled."; + else + errorMessage = "Cannot specify a prefix in a partition."; + + return new ReadyFuture(new IllegalArgumentException(errorMessage)); + } + + return checkVersion(rtr) + .flatMap(new Function>() { + @Override + public Future apply(Void ignore) { + // Root directory contains node metadata and so may not be opened. + if(pathCopy.size() == 0) { + throw new IllegalArgumentException("The root directory may not be opened."); + } + + return new NodeFinder(pathCopy).find(rtr).flatMap(new NodeMetadataLoader(rtr)); + } + }) + .flatMap(new Function>() { + @Override + public Future apply(final Node existingNode) { + if(existingNode.exists()) { + if(existingNode.isInPartition(false)) { + List subpath = existingNode.getPartitionSubpath(); + DirectoryLayer directoryLayer = existingNode.getContents().getDirectoryLayer(); + return directoryLayer.createOrOpenInternal( + rtr, tr, subpath, layer, prefix, allowCreate, allowOpen); + } + + DirectorySubspace opened = openInternal(pathCopy, layer, existingNode, allowOpen); + return new ReadyFuture(opened); + } + else + return createInternal(tr, pathCopy, layer, prefix, allowCreate); + } + }); + } + + private DirectorySubspace openInternal(final List path, + final byte[] layer, + final Node existingNode, + final boolean allowOpen) + { + if(!allowOpen) { + throw new DirectoryAlreadyExistsException(toAbsolutePath(path)); + } + else { + if(layer.length > 0 && !Arrays.equals(layer, existingNode.layer)) { + throw new MismatchedLayerException(toAbsolutePath(path), existingNode.layer, layer); + } + + return existingNode.getContents(); + } + } + + private Future createInternal(final Transaction tr, + final List path, + final byte[] layer, + final byte[] prefix, + final boolean allowCreate) + { + if(!allowCreate) { + throw new NoSuchDirectoryException(toAbsolutePath(path)); + } + + return checkOrWriteVersion(tr) + .flatMap(new Function>() { + @Override + public Future apply(Void ignore) { + if(prefix == null) { + return allocator.allocate(tr) + .flatMap(new Function>() { + @Override + public Future apply(byte[] allocated) { + final byte[] finalPrefix = ByteArrayUtil.join(contentSubspace.getKey(), allocated); + return tr.getRange(Range.startsWith(finalPrefix), 1) + .asList() + .map(new Function, byte[]>() { + @Override + public byte[] apply(List results) { + if(results.size() > 0) { + throw new IllegalStateException("The database has keys stored at the prefix chosen by the automatic " + + "prefix allocator: " + ByteArrayUtil.printable(finalPrefix) + "."); + } + + return finalPrefix; + } + }); + } + }); + } + else + return new ReadyFuture(prefix); + } + }) + .flatMap(new Function>() { + @Override + public Future apply(final byte[] actualPrefix) { + return isPrefixFree(prefix == null ? tr.snapshot() : tr, actualPrefix) + .flatMap(new Function>() { + @Override + public Future apply(Boolean prefixFree) { + if(!prefixFree) { + if(prefix == null) { + throw new IllegalStateException("The directory layer has manually allocated prefixes that conflict " + + "with the automatic prefix allocator."); + } + else + throw new IllegalArgumentException("Prefix already in use: " + ByteArrayUtil.printable(actualPrefix) + "."); + } + else if(path.size() > 1) { + return createOrOpen(tr, PathUtil.popBack(path)) + .map(new Function() { + @Override + public Subspace apply(DirectorySubspace dir) { + return nodeWithPrefix(dir.getKey()); + } + }); + } + else + return new ReadyFuture(rootNode); + } + }) + .map(new Function() { + @Override + public DirectorySubspace apply(Subspace parentNode) { + if(parentNode == null) + throw new IllegalStateException("The parent directory does not exist."); //Shouldn't happen + Subspace node = nodeWithPrefix(actualPrefix); + tr.set(parentNode.get(SUB_DIR_KEY).get(getLast(path)).getKey(), actualPrefix); + tr.set(node.get(LAYER_KEY).getKey(), layer); + return contentsOfNode(node, path, layer); + } + }); + } + }); + } + + // + // Helpers + // + + private static long unpackLittleEndian(byte[] bytes) { + assert bytes.length == 8; + int value = 0; + for(int i = 0; i < 8; ++i) { + value += (bytes[i] << (i * 8)); + } + return value; + } + + private static String getLast(List list) { + assert list.size() > 0; + return list.get(list.size() - 1); + } + + private class VersionCheck implements Function { + @Override + public Void apply(byte[] versionBytes) { + if(versionBytes == null) { + return null; + } + + ByteBuffer versionBuf = ByteBuffer.wrap(versionBytes); + versionBuf.order(ByteOrder.LITTLE_ENDIAN); + + Integer version[] = new Integer[3]; + for(int i = 0; i < version.length; ++i) + version[i] = versionBuf.getInt(); + + String dirVersion = String.format("version %d.%d.%d", (Object[])version); + String layerVersion = String.format("directory layer %d.%d.%d", (Object[])VERSION); + + throwOnError(version, dirVersion, layerVersion); + + return null; + } + + protected void throwOnError(Integer[] version, String dirVersion, + String layerVersion) { + if(version[0] > VERSION[0]) + throw new DirectoryVersionException("Cannot load directory with " + dirVersion + " using " + layerVersion + "."); + } + } + + private class WritableVersionCheck extends VersionCheck { + private final Transaction tr; + + private WritableVersionCheck(Transaction tr) { + this.tr = tr; + } + + @Override + public Void apply(byte[] versionBytes) { + if(versionBytes == null) { + // initializeVersion + ByteBuffer buf = ByteBuffer.allocate(VERSION.length * 4); + buf.order(ByteOrder.LITTLE_ENDIAN); + for(int ver : VERSION) + buf.putInt(ver); + + tr.set(rootNode.pack(VERSION_KEY), buf.array()); + return null; + } + + return super.apply(versionBytes); + } + + @Override + protected void throwOnError(Integer[] version, String dirVersion, + String layerVersion) { + super.throwOnError(version, dirVersion, layerVersion); + if(version[1] > VERSION[1]) + throw new DirectoryVersionException("Directory with " + dirVersion + " is read-only when opened with " + layerVersion + "."); + } + } + + private class NodeFinder { + private List path; + private int index; + private Node node; + private List currentPath; + + public NodeFinder(List path) { + this.path = path; + } + + public Future find(final ReadTransaction tr) { + index = 0; + node = new Node(rootNode, currentPath, path); + currentPath = new ArrayList(); + + return AsyncUtil.whileTrue(new Function>() { + @Override + public Future apply(Void ignore) { + if(index == path.size()) + return new ReadyFuture(false); + + return tr.get(node.subspace.get(SUB_DIR_KEY).get(path.get(index)).getKey()) + .flatMap(new Function>() { + @Override + public Future apply(byte[] key) { + currentPath.add(path.get(index)); + node = new Node(nodeWithPrefix(key), currentPath, path); + + if(!node.exists()) + return new ReadyFuture(false); + + return node.loadMetadata(tr) + .map(new Function() { + @Override + public Boolean apply(Node ignore) { + ++index; + return !Arrays.equals(node.layer, DirectoryLayer.PARTITION_LAYER); + } + }); + } + }); + } + }) + .map(new Function() { + @Override + public Node apply(Void ignore) { + return node; + } + }); + } + } + + private static class NodeMetadataLoader implements Function> { + private final ReadTransaction tr; + + public NodeMetadataLoader(ReadTransaction tr) { + this.tr = tr; + } + + @Override + public Future apply(Node node) { + return node.loadMetadata(tr); + } + } + + private class Node { + public final Subspace subspace; + public final List path; + public final List targetPath; + public byte[] layer; + + private boolean loadedMetadata; + + public Node(Subspace subspace, List path, List targetPath) { + this.subspace = subspace; + this.path = path; + this.targetPath = targetPath; + + layer = null; + loadedMetadata = false; + } + + public boolean exists() { + return subspace != null; + } + + public Future loadMetadata(ReadTransaction tr) { + if(!exists()) { + loadedMetadata = true; + return new ReadyFuture(this); + } + + return tr.get(subspace.pack(new Tuple().add(LAYER_KEY))) + .map(new Function() { + @Override + public Node apply(byte[] value) { + layer = value; + loadedMetadata = true; + return Node.this; + } + }); + } + + public void ensureMetadataLoaded() { + if(!loadedMetadata) + throw new IllegalStateException("Metadata for node has not been loaded"); + } + + public boolean isInPartition(boolean includeEmptySubpath) { + ensureMetadataLoaded(); + return exists() && Arrays.equals(layer, DirectoryLayer.PARTITION_LAYER) && (includeEmptySubpath || targetPath.size() > path.size()); + } + + public List getPartitionSubpath() { + ensureMetadataLoaded(); + return targetPath.subList(path.size(), targetPath.size()); + } + + public DirectorySubspace getContents() { + ensureMetadataLoaded(); + return contentsOfNode(subspace, path, layer); + } + } + + private static class PrefixFinder { + private final Random random; + + private long windowStart; + private int windowSize; + + private long candidate; + private boolean restart; + + public PrefixFinder() { + this.random = new Random(); + this.windowStart = 0; + } + + public Future find(final Transaction tr, final HighContentionAllocator allocator) { + return AsyncUtil.whileTrue(new Function>() { + @Override + public Future apply(Void ignore) { + final AsyncIterator rangeItr = tr.snapshot().getRange(allocator.counters.range(), 1, true).iterator(); + return rangeItr.onHasNext() + .map(new Function() { + @Override + public Void apply(Boolean hasNext) { + if(hasNext) { + KeyValue kv = rangeItr.next(); + windowStart = allocator.counters.unpack(kv.getKey()).getLong(0); + } + + return null; + } + }) + .flatMap(new Function>() { + @Override + public Future apply(Void ignore) { + return chooseWindow(tr, allocator); + } + }) + .flatMap(new Function>() { + @Override + public Future apply(Void ignore) { + return choosePrefix(tr, allocator); // false exits the loop (i.e. we have a valid prefix) + } + }); + } + }) + .map(new Function() { + @Override + public byte[] apply(Void ignore) { + return Tuple.from(candidate).pack(); + } + }); + } + + public Future chooseWindow(final Transaction tr, final HighContentionAllocator allocator) { + final long initialWindowStart = windowStart; + return AsyncUtil.whileTrue(new Function>() { + @Override + public Future apply(Void ignore) { + final byte[] counterKey = allocator.counters.get(windowStart).getKey(); + + Range oldCounters = new Range(allocator.counters.getKey(), counterKey); + Range oldAllocations = new Range(allocator.recent.getKey(), allocator.recent.get(windowStart).getKey()); + + Future newCountRead; + // SOMEDAY: synchronize on something transaction local + synchronized(HighContentionAllocator.class) { + if(windowStart > initialWindowStart) { + tr.clear(oldCounters); + tr.options().setNextWriteNoWriteConflictRange(); + tr.clear(oldAllocations); + } + + tr.mutate(MutationType.ADD, counterKey, LITTLE_ENDIAN_LONG_ONE); + newCountRead = tr.snapshot().get(counterKey); + } + + return newCountRead + .map(new Function() { + @Override + public Boolean apply(byte[] newCountBytes) { + long newCount = newCountBytes == null ? 0 : unpackLittleEndian(newCountBytes); + windowSize = getWindowSize(windowStart); + if(newCount * 2 >= windowSize) { + windowStart += windowSize; + return true; + } + + return false; // exit the loop + } + }); + } + }); + } + + public Future choosePrefix(final Transaction tr, final HighContentionAllocator allocator) { + restart = false; + return AsyncUtil.whileTrue(new Function>() { + @Override + public Future apply(Void ignore) { + // As of the snapshot being read from, the window is less than half + // full, so this should be expected to take 2 tries. Under high + // contention (and when the window advances), there is an additional + // subsequent risk of conflict for this transaction. + candidate = windowStart + random.nextInt(windowSize); + final byte[] allocationKey = allocator.recent.get(candidate).getKey(); + Range countersRange = allocator.counters.range(); + + AsyncIterable counterRange; + Future allocationTemp; + // SOMEDAY: synchronize on something transaction local + synchronized(HighContentionAllocator.class) { + counterRange = tr.snapshot().getRange(countersRange, 1, true); + allocationTemp = tr.get(allocationKey); + tr.options().setNextWriteNoWriteConflictRange(); + tr.set(allocationKey, EMPTY_BYTES); + } + + final Future> lastCounter = counterRange.asList(); + final Future allocation = allocationTemp; + + List> futures = new ArrayList>(); + futures.add(AsyncUtil.success(lastCounter)); + futures.add(AsyncUtil.success(allocation)); + + return AsyncUtil.whenAll(futures) + .map(new Function() { + @Override + public Boolean apply(Void ignore) { + long currentWindowStart = 0; + if(!lastCounter.get().isEmpty()) { + currentWindowStart = allocator.counters.unpack(lastCounter.get().get(0).getKey()).getLong(0); + } + + if(currentWindowStart > windowStart) { + restart = true; + return false; // exit the loop and rerun the allocation from the beginning + } + + if(allocation.get() == null) { + tr.addWriteConflictKey(allocationKey); + return false; // exit the loop and return this candidate + } + + return true; + } + }); + } + }) + .map(new Function() { + @Override + public Boolean apply(Void ignore) { + return restart; + } + }); + } + + private static int getWindowSize(long start) { + // Larger window sizes are better for high contention, smaller sizes for + // keeping the keys small. But if there are many allocations, the keys + // can't be too small. So start small and scale up. We don't want this + // to ever get *too* big because we have to store about window_size/2 + // recent items. + if(start < 255) { + return 64; + } + if(start < 65535) { + return 1024; + } + return 8192; + } + } + + private static class HighContentionAllocator { + public final Subspace counters; + public final Subspace recent; + + public HighContentionAllocator(Subspace subspace) { + this.counters = subspace.get(0); + this.recent = subspace.get(1); + } + + /** + * Returns a byte string that: + *
      + *
    1. has never and will never be returned by another call to this method on the same subspace
    2. + *
    3. is nearly as short as possible given the above
    4. + *
    + */ + public Future allocate(final Transaction tr) { + return new PrefixFinder().find(tr, this); + } + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryMoveException.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryMoveException.java new file mode 100644 index 0000000000..cc9f7abb56 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryMoveException.java @@ -0,0 +1,47 @@ +/* + * DirectoryMoveException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +/** + * An {@link Exception} that is thrown when an invalid directory move + * is attempted. + */ +@SuppressWarnings("serial") +public class DirectoryMoveException extends RuntimeException { + /** + * The path of the directory being moved. + */ + public final List sourcePath; + + /** + * The path that the directory was being moved to. + */ + public final List destPath; + + DirectoryMoveException(String message, List sourcePath, List destPath) { + super(message + ": sourcePath=" + DirectoryUtil.pathStr(sourcePath) + ", destPath=" + DirectoryUtil.pathStr(destPath)); + this.sourcePath = sourcePath; + this.destPath = destPath; + } +} + diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryPartition.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryPartition.java new file mode 100644 index 0000000000..2a4b33a655 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryPartition.java @@ -0,0 +1,197 @@ +/* + * DirectoryPartition.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import static com.apple.cie.foundationdb.directory.DirectoryLayer.DEFAULT_NODE_SUBSPACE_PREFIX; + +import java.util.List; + +import com.apple.cie.foundationdb.subspace.Subspace; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * A {@code DirectoryPartition} is a {@link DirectorySubspace} whose prefix is prepended to all of its descendant directories' prefixes. + * + *

    + * A {@code DirectoryPartition} cannot be used as a {@link Subspace}. Instead, you must create at least one subdirectory to store + * content. + *

    + * + * For general guidance on partition usage, see + * The Developer Guide. + */ +class DirectoryPartition extends DirectorySubspace { + + private final DirectoryLayer parentDirectoryLayer; + + DirectoryPartition(List path, byte[] prefix, DirectoryLayer parentDirectoryLayer) { + super(path, + prefix, + new DirectoryLayer(new Subspace(ByteArrayUtil.join(prefix, DEFAULT_NODE_SUBSPACE_PREFIX)), new Subspace(prefix)), + DirectoryLayer.PARTITION_LAYER); + + getDirectoryLayer().setPath(path); + this.parentDirectoryLayer = parentDirectoryLayer; + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Subspace get(Object o) { + throw new UnsupportedOperationException("Cannot open subspace in the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Subspace get(Tuple name) { + throw new UnsupportedOperationException("Cannot open subspace in the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public byte[] getKey() { + throw new UnsupportedOperationException("Cannot get key for the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public byte[] pack() { + throw new UnsupportedOperationException("Cannot pack keys using the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public byte[] pack(Object o) { + throw new UnsupportedOperationException("Cannot pack keys using the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public byte[] pack(Tuple tuple) { + throw new UnsupportedOperationException("Cannot pack keys using the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Tuple unpack(byte[] key) { + throw new UnsupportedOperationException("Cannot unpack keys using the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Range range() { + throw new UnsupportedOperationException("Cannot get range for the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Range range(Tuple tuple) { + throw new UnsupportedOperationException("Cannot get range for the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public boolean contains(byte[] key) { + throw new UnsupportedOperationException("Cannot check whether a key belongs to the root of a directory partition."); + } + + /** + * Raises an exception because DirectoryPartition cannot be used as a Subspace. + * + * @throws UnsupportedOperationException + */ + @Override + public Subspace subspace(Tuple tuple) { + throw new UnsupportedOperationException("Cannot open subspace in the root of a directory partition."); + } + + @Override + DirectoryLayer getLayerForPath(List path) { + if(path.size() == 0) + return parentDirectoryLayer; + else + return getDirectoryLayer(); + } + + /** + * Returns whether this {@code DirectoryPartition} is equal to {@code rhs}. + * Two {@code DirectoryPartition}s are equal if they were created by the same + * {@link DirectoryLayer} and have the same path, layer, and subspace prefix. + * + * @param rhs the {@code} Object to test for equality + * @return true if this is equal to {@code rhs} + */ + @Override + public boolean equals(Object rhs) { + if(this == rhs) { + return true; + } + if(rhs == null || getClass() != rhs.getClass()) { + return false; + } + + DirectoryPartition other = (DirectoryPartition)rhs; + return (getPath() == other.getPath() || getPath() == other.getPath()) && + parentDirectoryLayer.equals(other.parentDirectoryLayer) && + super.equals(rhs); + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectorySubspace.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectorySubspace.java new file mode 100644 index 0000000000..2366e16919 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectorySubspace.java @@ -0,0 +1,220 @@ +/* + * DirectorySubspace.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import static com.apple.cie.foundationdb.directory.DirectoryLayer.EMPTY_BYTES; +import static com.apple.cie.foundationdb.directory.DirectoryLayer.EMPTY_PATH; +import static com.apple.cie.foundationdb.tuple.ByteArrayUtil.printable; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.subspace.Subspace; +import com.apple.cie.foundationdb.ReadTransactionContext; +import com.apple.cie.foundationdb.async.Future; + +/** + * A DirectorySubspace represents the contents of a directory, but it + * also remembers the path with which it was opened and offers convenience + * methods to operate on the directory at that path. + * + *

    + * An instance of DirectorySubspace can be used for all the usual subspace + * operations. It can also be used to operate on the directory with which + * it was opened. + *

    + */ +public class DirectorySubspace extends Subspace implements Directory { + private final List path; + private final byte[] layer; + private final DirectoryLayer directoryLayer; + + DirectorySubspace(List path, byte[] prefix, DirectoryLayer directoryLayer) { + this(path, prefix, directoryLayer, EMPTY_BYTES); + } + + DirectorySubspace(List path, byte[] prefix, DirectoryLayer directoryLayer, byte[] layer) { + super(prefix); + this.path = path; + this.layer = layer; + this.directoryLayer = directoryLayer; + } + + /** + * @return a printable representation of this {@code DirectorySubspace} + */ + @Override + public String toString() { + return getClass().getSimpleName() + '(' + DirectoryUtil.pathStr(path) + ", " + printable(getKey()) + ')'; + } + + /** + * Returns whether this {@code DirectorySubspace} is equal to {@code rhs}. + * Two {@code DirectorySubspace}s are equal if they were created by the same + * {@link DirectoryLayer} and have the same path, layer, and subspace prefix. + * + * @param rhs the {@code} Object to test for equality + * @return true if this is equal to {@code rhs} + */ + @Override + public boolean equals(Object rhs) { + if(this == rhs) { + return true; + } + if(rhs == null || getClass() != rhs.getClass()) { + return false; + } + DirectorySubspace other = (DirectorySubspace)rhs; + return (path == other.path || path.equals(other.path)) && + Arrays.equals(layer, other.layer) && + directoryLayer.equals(other.directoryLayer) && + super.equals(rhs); + } + + @Override + public List getPath() { + return Collections.unmodifiableList(path); + } + + @Override + public byte[] getLayer() { + return Arrays.copyOf(layer, layer.length); + } + + @Override + public DirectoryLayer getDirectoryLayer() { + return directoryLayer; + } + + @Override + public Future createOrOpen(TransactionContext tcx, List subpath) { + return createOrOpen(tcx, subpath, EMPTY_BYTES); + } + + @Override + public Future createOrOpen(TransactionContext tcx, List subpath, byte[] otherLayer) { + return directoryLayer.createOrOpen(tcx, getPartitionSubpath(subpath), otherLayer); + } + + @Override + public Future open(ReadTransactionContext tcx, List subpath) { + return open(tcx, subpath, EMPTY_BYTES); + } + + @Override + public Future open(ReadTransactionContext tcx, List subpath, byte[] otherLayer) { + return directoryLayer.open(tcx, getPartitionSubpath(subpath), otherLayer); + } + + @Override + public Future create(TransactionContext tcx, List subpath) { + return create(tcx, subpath, EMPTY_BYTES, null); + } + + @Override + public Future create(TransactionContext tcx, List subpath, byte[] otherLayer) { + return create(tcx, subpath, otherLayer, null); + } + + @Override + public Future create(TransactionContext tcx, List subpath, byte[] otherLayer, byte[] prefix) { + return directoryLayer.create(tcx, getPartitionSubpath(subpath), otherLayer, prefix); + } + + @Override + public Future> list(ReadTransactionContext tcx) { + return list(tcx, EMPTY_PATH); + } + + @Override + public Future> list(ReadTransactionContext tcx, List subpath) { + return directoryLayer.list(tcx, getPartitionSubpath(subpath)); + } + + @Override + public Future move(TransactionContext tcx, List oldSubpath, List newSubpath) { + return directoryLayer.move(tcx, getPartitionSubpath(oldSubpath), getPartitionSubpath(newSubpath)); + } + + @Override + public Future moveTo(TransactionContext tcx, List newAbsolutePath) { + DirectoryLayer dir = getLayerForPath(EMPTY_PATH); + int partitionLen = dir.getPath().size(); + List partitionPath = newAbsolutePath.subList(0, Math.min(newAbsolutePath.size(), partitionLen)); + if(!partitionPath.equals(dir.getPath())) + throw new DirectoryMoveException("Cannot move between partitions", path, newAbsolutePath); + + return dir.move(tcx, + getPartitionSubpath(EMPTY_PATH, dir), + newAbsolutePath.subList(partitionLen, newAbsolutePath.size())); + } + + @Override + public Future remove(TransactionContext tcx) { + return remove(tcx, EMPTY_PATH); + } + + @Override + public Future remove(TransactionContext tcx, List subpath) { + DirectoryLayer dir = getLayerForPath(subpath); + return dir.remove(tcx, getPartitionSubpath(subpath, dir)); + } + + @Override + public Future removeIfExists(TransactionContext tcx) { + return removeIfExists(tcx, EMPTY_PATH); + } + + @Override + public Future removeIfExists(TransactionContext tcx, List subpath) { + DirectoryLayer dir = getLayerForPath(subpath); + return dir.removeIfExists(tcx, getPartitionSubpath(subpath, dir)); + } + + @Override + public Future exists(ReadTransactionContext tcx) { + return exists(tcx, EMPTY_PATH); + } + + @Override + public Future exists(ReadTransactionContext tcx, List subpath) { + DirectoryLayer dir = getLayerForPath(subpath); + return dir.exists(tcx, getPartitionSubpath(subpath, dir)); + } + + private List getPartitionSubpath(List path) { + return getPartitionSubpath(path, directoryLayer); + } + + private List getPartitionSubpath(List path, DirectoryLayer directoryLayer) { + return PathUtil.join(this.path.subList(directoryLayer.getPath().size(), this.path.size()), path); + } + + /** + * Called by all functions that could operate on this subspace directly (moveTo, remove, removeIfExists, exists). + * Subclasses can chooose to return a different directory layer to use for the operation if path is in fact empty. + */ + DirectoryLayer getLayerForPath(List path) { + return directoryLayer; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryUtil.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryUtil.java new file mode 100644 index 0000000000..2383f187e6 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryUtil.java @@ -0,0 +1,45 @@ +/* + * DirectoryUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +class DirectoryUtil { + static String pathStr(List t) { + if(t == null) { + return String.valueOf(t); + } + StringBuilder sb = new StringBuilder(); + sb.append('('); + boolean first = true; + for(String s : t) { + if(!first) { + sb.append(','); + } else { + first = false; + } + + sb.append(s); + } + sb.append(')'); + return sb.toString(); + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryVersionException.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryVersionException.java new file mode 100644 index 0000000000..406d66bd03 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/DirectoryVersionException.java @@ -0,0 +1,33 @@ +/* + * DirectoryVersionException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +/** + * An {@link Exception} that is thrown when the version of the directory layer + * used to create directories in the database is incompatible with this version + * of the directory layer. + */ +@SuppressWarnings("serial") +public class DirectoryVersionException extends RuntimeException { + DirectoryVersionException(String message) { + super(message); + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/MismatchedLayerException.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/MismatchedLayerException.java new file mode 100644 index 0000000000..feea71e3f3 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/MismatchedLayerException.java @@ -0,0 +1,48 @@ +/* + * MismatchedLayerException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +import java.util.List; + +/** + * A {@link DirectoryException} that is thrown when a directory is opened with an incompatible layer. + */ +@SuppressWarnings("serial") +public class MismatchedLayerException extends DirectoryException { + /** + * The layer byte string that the directory was created with. + */ + public final byte[] stored; + + /** + * The layer byte string that the directory was opened with. + */ + public final byte[] opened; + + MismatchedLayerException(List path, byte[] stored, byte[] opened) { + super("Mismatched layer: stored=" + ByteArrayUtil.printable(stored) + ", opened=" + ByteArrayUtil.printable(opened), path); + this.stored = stored; + this.opened = opened; + } +} + diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/NoSuchDirectoryException.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/NoSuchDirectoryException.java new file mode 100644 index 0000000000..00e8a6c5e5 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/NoSuchDirectoryException.java @@ -0,0 +1,34 @@ +/* + * NoSuchDirectoryException.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.List; + +/** + * A {@link DirectoryException} that is thrown when a directory that doesn't + * exist is used. + */ +@SuppressWarnings("serial") +public class NoSuchDirectoryException extends DirectoryException { + NoSuchDirectoryException(List path) { + super("No such directory", path); + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/PathUtil.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/PathUtil.java new file mode 100644 index 0000000000..5b69f6579a --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/PathUtil.java @@ -0,0 +1,95 @@ +/* + * PathUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.directory; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +/** + * The {@code PathUtil} class provides static helper functions useful for working + * with directory paths. + */ +public class PathUtil { + /** + * Joins two paths into one larger path. + * + * @param path1 The first path to join + * @param path2 The path to append to {@code path1} + * @return a new list which contains all the items in {@code path1} followed + * by all the items in {@code path2} + */ + public static List join(List path1, List path2) { + List newPath = new LinkedList(path1); + newPath.addAll(path2); + return newPath; + } + + /** + * Extends a path by an arbitrary number of elements. + * + * @param path The path to extend + * @param subPaths The items to append to path + * @return a new list which contains all the items in {@code path} followed + * by all additional items specified in {@code subPaths} + */ + public static List extend(List path, String... subPaths) { + return join(path, Arrays.asList(subPaths)); + } + + /** + * Creates a new path from an arbitrary number of elements. + * + * @param subPaths The items in the path + * @return a list which contains all the items specified in {@code subPaths} + */ + public static List from(String... subPaths) { + return new LinkedList(Arrays.asList(subPaths)); + } + + /** + * Removes the first item from a path. + * + * @param path the path whose first item is being popped + * @return a new list which contains all the items in {@code path} except + * for the first item + */ + public static List popFront(List path) { + if(path.isEmpty()) + throw new IllegalStateException("Path contains no elements."); + + return new LinkedList(path.subList(1, path.size())); + } + + /** + * Removes the last item from a path. + * + * @param path the path whose last item is being popped + * @return a new list which contains all the items in {@code path} except + * for the last item + */ + public static List popBack(List path) { + if(path.isEmpty()) + throw new IllegalStateException("Path contains no elements."); + + return new LinkedList(path.subList(0, path.size() - 1)); + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/directory/package-info.java b/bindings/java/src/main/com/apple/cie/foundationdb/directory/package-info.java new file mode 100644 index 0000000000..4616883692 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/directory/package-info.java @@ -0,0 +1,38 @@ +/* + * package-info.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides tools for managing hierarchically related key subspaces. + * Directories are a recommended approach for + * administering applications. Each application should create or + * open at least one directory to manage its subspaces.
    + *
    + * Directories are identified by hierarchical paths analogous to the + * paths in a Unix-like file system. A path is represented as a tuple + * of strings. Each directory has an associated subspace used to store + * its content. The directory layer maps each path to a short prefix + * used for the corresponding subspace. In effect, directories provide + * a level of indirection for access to subspaces.
    + *
    + * See general + * directory documentation for information about how directories work and + * interact with other parts of the built-in keyspace management features. + */ +package com.apple.cie.foundationdb.directory; \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/package-info.java b/bindings/java/src/main/com/apple/cie/foundationdb/package-info.java new file mode 100644 index 0000000000..ff4695b715 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/package-info.java @@ -0,0 +1,37 @@ +/* + * package-info.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides an API for the FoundationDB transactional key/value store. Clients operating + * on a {@link com.apple.cie.foundationdb.Database} should, in most cases, use the + * {@link com.apple.cie.foundationdb.TransactionContext#run(Function) run(Function)} + * or the + * {@link com.apple.cie.foundationdb.TransactionContext#runAsync(Function) runAsync(Function)} + * constructs. These two functions (and their two derivations) implement a proper + * retry loop around the work that needs to get done and, in the case of {@code Database}, + * assure that {@link com.apple.cie.foundationdb.Transaction#commit()} has returned successfully + * before itself returning. If you are not able to use these functions for some reason + * please closely read and understand the other + * developer + * documentation on FoundationDB transactions. + */ +package com.apple.cie.foundationdb; + +import com.apple.cie.foundationdb.async.Function; \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/subspace/Subspace.java b/bindings/java/src/main/com/apple/cie/foundationdb/subspace/Subspace.java new file mode 100644 index 0000000000..b2de6e2355 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/subspace/Subspace.java @@ -0,0 +1,240 @@ +/* + * Subspace.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.subspace; + +import static com.apple.cie.foundationdb.tuple.ByteArrayUtil.join; +import static com.apple.cie.foundationdb.tuple.ByteArrayUtil.printable; + +import java.util.Arrays; + +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * {@code Subspace} provide a convenient way to use {@link Tuple}s to define namespaces for + * different categories of data. The namespace is specified by a prefix {@link Tuple} + * which is prepended to all {@link Tuple}s packed by the {@code Subspace}. When unpacking a key + * with the {@code Subspace}, the prefix {@link Tuple} will be removed from the result. + * + *

    + * For general guidance on subspace usage, see the discussion in + * Developer Guide. + *

    + * + *

    + * As a best practice, API clients should use at least one subspace for application data. + *

    + */ +public class Subspace +{ + static final Tuple EMPTY_TUPLE = Tuple.from(); + static final byte[] EMPTY_BYTES = new byte[0]; + + private final byte[] rawPrefix; + + /** + * Constructor for a subspace formed with an empty prefix {@link Tuple}. + */ + public Subspace() { + this(EMPTY_TUPLE, EMPTY_BYTES); + } + + /** + * Constructor for a subspace formed with the specified prefix {@link Tuple}. + * + * @param prefix a {@link Tuple} used to form the subspace + */ + public Subspace(Tuple prefix) { + this(prefix, EMPTY_BYTES); + } + + /** + * Constructor for a subspace formed with the specified byte string, which will + * be prepended to all packed keys. + * + * @param rawPrefix a byte array used as the prefix for all packed keys + */ + public Subspace(byte[] rawPrefix) { + this(EMPTY_TUPLE, rawPrefix); + } + + /** + * Constructor for a subspace formed with both a prefix {@link Tuple} and a + * prefix byte string. The prefix {@code Tuple} will be prepended to all + * {@code Tuples} packed by the {@code Subspace}, and the byte string prefix + * will be prepended to the packed result. + * + * @param prefix a {@code Tuple} used to form the subspace + * @param rawPrefix a byte array used as the prefix for all packed keys + */ + public Subspace(Tuple prefix, byte[] rawPrefix) { + this.rawPrefix = join(rawPrefix, prefix.pack()); + } + + /** + * Returns true if this {@code Subspace} is equal to {@code rhs}. + * Two {@code Subspace}s are equal if they have the same prefix. + * + * @param rhs the object to check for equality + * @return {@code true} if this {@code Subspace} and {@code rhs} have equal prefixes + */ + @Override + public boolean equals(Object rhs) { + if(this == rhs) { + return true; + } + if(rhs == null || getClass() != rhs.getClass()) { + return false; + } + Subspace other = (Subspace)rhs; + return Arrays.equals(rawPrefix, other.rawPrefix) ; + } + + /** + * @return a printable representation of the subspace + */ + @Override + public String toString() { + return "Subspace(rawPrefix=" + printable(rawPrefix) + ")"; + } + + /** + * Gets a new subspace which is equivalent to this subspace with its prefix {@link Tuple} extended by + * the specified {@code Object}. The object will be inserted into a {@link Tuple} and passed to {@link #get(Tuple)}. + * + * @param obj an {@code Object} compatible with {@code Tuple}s + * @return a new subspace formed by joining this {@code Subspace}'s prefix to {@code obj} + */ + public Subspace get(Object obj) { + return get(Tuple.from(obj)); + } + + /** + * Gets a new subspace which is equivalent to this subspace with its prefix {@link Tuple} extended by + * the specified {@link Tuple}. + * + * @param tuple the {@link Tuple} used to form the new {@code Subspace} + * @return a new subspace formed by joining this {@code Subspace}'s prefix to {@code tuple} + */ + public Subspace get(Tuple tuple) { + return subspace(tuple); + } + + /** + * Gets the key encoding the prefix used for this {@code Subspace}. This is equivalent to + * {@link #pack}ing the empty {@link Tuple}. + * + * @return the key encoding the prefix used for this {@code Subspace} + */ + public byte[] getKey() { + return pack(); + } + + /** + * Gets the key encoding the prefix used for this {@code Subspace}. + * + * @return the key encoding the prefix used for this {@code Subspace} + */ + public byte[] pack() { + return Arrays.copyOf(rawPrefix, rawPrefix.length); + } + + /** + * Gets the key encoding the specified {@code Object} in this {@code Subspace}. {@code obj} is + * inserted into a {@link Tuple} and packed with {@link #pack(Tuple)}. + * + * @param obj an {@code Object} to be packed that is compatible with {@link Tuple}s + * @return the key encoding the tuple derived from {@code obj} + */ + public byte[] pack(Object obj) { + return pack(Tuple.from(obj)); + } + + /** + * Gets the key encoding the specified tuple in this {@code Subspace}. For example, if you have a {@code Subspace} + * with prefix {@link Tuple} {@code ("users")} and you use it to pack the {@link Tuple} {@code ("Smith")}, + * the result is the same as if you packed the {@link Tuple} {@code ("users", "Smith")}. + * + * @param tuple the {@code Tuple} to be packed + * @return the key encoding the specified tuple in this {@code Subspace} + */ + public byte[] pack(Tuple tuple) { + return join(rawPrefix, tuple.pack()); + } + + /** + * Gets the {@link Tuple} encoded by the given key, with this {@code Subspace}'s prefix {@link Tuple} and + * {@code raw prefix} removed. + * + * @param key The key being decoded + * @return the {@link Tuple} encoded by {@code key} with the prefix removed + */ + public Tuple unpack(byte[] key) { + if(!contains(key)) + throw new IllegalArgumentException("Cannot unpack key that is not contained in subspace."); + + return Tuple.fromBytes(Arrays.copyOfRange(key, rawPrefix.length, key.length)); + } + + /** + * Gets a {@link Range} respresenting all keys strictly in the {@code Subspace}. + * + * @return the {@link Range} of keyspace corresponding to this {@code Subspace} + */ + public Range range() { + return range(EMPTY_TUPLE); + } + + /** + * Gets a {@link Range} representing all keys in the {@code Subspace} strictly starting with + * the specified {@link Tuple}. + * + * @param tuple the {@code Tuple} whose sub-keys we are searching for + * @return the {@link Range} of keyspace corresponding to {@code tuple} + */ + public Range range(Tuple tuple) { + Range p = tuple.range(); + return new Range(join(rawPrefix, p.begin), join(rawPrefix, p.end)); + } + + /** + * Tests whether the specified key starts with this {@code Subspace}'s prefix, indicating that + * the {@code Subspace} logically contains key. + * + * @param key the key to be tested + * @return {@code true} if {@code key} starts with {@code Subspace.key()} + */ + public boolean contains(byte[] key) { + return ByteArrayUtil.startsWith(key, rawPrefix); + } + + /** + * Gets a new subspace which is equivalent to this subspace with its prefix {@link Tuple} extended by + * the specified {@link Tuple}. + * + * @param tuple the {@link Tuple} used to form the new {@code Subspace} + * @return a new subspace formed by joining this {@code Subspace}'s prefix to {@code tuple} + */ + public Subspace subspace(Tuple tuple) { + return new Subspace(tuple, rawPrefix); + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/subspace/package-info.java b/bindings/java/src/main/com/apple/cie/foundationdb/subspace/package-info.java new file mode 100644 index 0000000000..b632c787d9 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/subspace/package-info.java @@ -0,0 +1,33 @@ +/* + * package-info.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides a convenient way to define namespaces for different categories + * of data. The namespace is specified + * by a prefix tuple which is prepended to all tuples packed by the subspace. + * When unpacking a key with the subspace, the prefix tuple will be removed + * from the result. As a best practice, API clients should use at least one + * subspace for application data.
    + *
    + * See general + * subspace documentation for information about how subspaces work and + * interact with other parts of the built-in keyspace management features. + */ +package com.apple.cie.foundationdb.subspace; \ No newline at end of file diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/tuple/ByteArrayUtil.java b/bindings/java/src/main/com/apple/cie/foundationdb/tuple/ByteArrayUtil.java new file mode 100644 index 0000000000..2257ae997d --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/tuple/ByteArrayUtil.java @@ -0,0 +1,419 @@ +/* + * ByteArrayUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.tuple; + +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +import com.apple.cie.foundationdb.Transaction; + +/** + * Utility functions for operating on byte arrays. Although built for + * the FoundationDB tuple layer, some functions may be useful otherwise, such as use of + * {@link #printable(byte[])} for debugging non-text keys and values. + * + */ +public class ByteArrayUtil { + + /** + * Joins a set of byte arrays into a larger array. The {@code interlude} is placed + * between each of the elements, but not at the beginning or end. In the case that + * the list is empty or {@code null}, a zero-length byte array will be returned. + * + * @param interlude can be {@code null} or zero length. Placed internally between + * concatenated elements. + * @param parts the pieces to be joined. May be {@code null}, but does not allow + * for elements in the list to be {@code null}. + * + * @return a newly created concatenation of the input + */ + public static byte[] join(byte[] interlude, List parts) { + if(parts == null) + return new byte[0]; + int partCount = parts.size(); + if(partCount == 0) + return new byte[0]; + + if(interlude == null) + interlude = new byte[0]; + + int element_totals = 0; + int interludeSize = interlude.length; + for(byte[] e : parts) { + element_totals += e.length; + } + + byte[] dest = new byte[(interludeSize * (partCount - 1)) + element_totals]; + + //System.out.println(" interlude -> " + ArrayUtils.printable(interlude)); + + int start_byte = 0; + int index = 0; + for(byte[] part : parts) { + //System.out.println(" section -> " + ArrayUtils.printable(parts.get(i))); + int length = part.length; + if(length > 0) { + System.arraycopy(part, 0, dest, start_byte, length); + start_byte += length; + } + if(index < partCount - 1 && interludeSize > 0) { + // If this is not the last element, append the interlude + System.arraycopy(interlude, 0, dest, start_byte, interludeSize); + start_byte += interludeSize; + } + index++; + } + + //System.out.println(" complete -> " + ArrayUtils.printable(dest)); + return dest; + } + + /** + * Joins a variable number of byte arrays into one larger array. + * + * @param parts the elements to join. {@code null} elements are not allowed. + * + * @return a newly created concatenation of the input + */ + public static byte[] join(byte[] ... parts) { + return join(null, Arrays.asList(parts)); + } + + /** + * Tests for the presence of a specific sequence of bytes in a larger array at a + * specific location.
    + * If {@code src} is {@code null} there is a case for a match. First, if {@code start} + * is non-zero, an {@code IllegalArgumentException} will be thrown. If {@code start} + * is {@code 0}, will evaluate to {@code true} if {@code pattern} is {@code null}; + * {@code false} otherwise.
    + * In all other cases, a {@code null} pattern will never match. + * + * @param src the sequence of bytes in which to search for {@code pattern} + * @param start the index at which to look for a match. The length of {@code pattern} added + * to this index must not pass the end of {@code src.} + * @param pattern the series of {@code byte}s to match. If {@code null}, will only match + * a {@code null} {@code src} at position {@code 0}. + * + * @return {@code true} if {@code pattern} is found in {@code src} at {@code start}. + */ + static boolean regionEquals(byte[] src, int start, byte[] pattern) { + if(src == null) { + if(start == 0) { + return pattern == null; + } + throw new IllegalArgumentException("start index after end of src"); + } + if(pattern == null) + return false; + + // At this point neither src or pattern are null... + + if(start >= src.length) + throw new IllegalArgumentException("start index after end of src"); + + if(src.length < start + pattern.length) + return false; + + for(int i = 0; i < pattern.length; i++) + if(pattern[i] != src[start + i]) + return false; + + return true; + } + + /** + * Replaces occurrences of a pattern in a byte array. Does not mutate the contents + * of the parameter {@code src}. + * + * @param src the source to search for {@code pattern} + * @param pattern the pattern for which to search + * @param replacement the sequence of bytes to replace {@code pattern} with. + * + * @return a newly created array where {@code pattern} replaced with {@code replacement} + */ + public static byte[] replace(byte[] src, byte[] pattern, byte[] replacement) { + return join(replacement, split(src, pattern)); + } + + /** + * Replaces occurrences of a pattern in a byte array. Does not mutate the contents + * of the parameter {@code src}. + * + * @param src the source to search for {@code pattern} + * @param offset the location in {@code src} at which to start the operation + * @param length the number of bytes past {@code offset} to search for {@code pattern} + * @param pattern the pattern for which to search + * @param replacement the sequence of bytes to replace {@code pattern} with. + * + * @return a newly created array where {@code pattern} replaced with {@code replacement} + */ + public static byte[] replace(byte[] src, int offset, int length, + byte[] pattern, byte[] replacement) { + return join(replacement, split(src, offset, length, pattern)); + } + + /** + * Splits a byte array at each occurrence of a pattern. If the pattern is found at + * the beginning or end of the array the result will have a leading or trailing + * zero-length array. The delimiter is not included in the output array. Does not + * mutate the contents the source array. + * + * @param src the array to split + * @param delimiter the byte pattern on which to split + * + * @return a list of byte arrays from {@code src} now not containing {@code delimiter} + */ + public static List split(byte[] src, byte[] delimiter) { + return split(src, 0, src.length, delimiter); + } + + /** + * Splits a byte array at each occurrence of a pattern. If the pattern is found at + * the beginning or end of the array the result will have a leading or trailing + * zero-length array. The delimiter is not included in the output array. Does not + * mutate the contents the source array. + * + * @param src the array to split + * @param offset the location in the array at which to start the operation + * @param length the number of bytes to search, must not extend past the end of {@code src} + * @param delimiter the byte pattern on which to split + * + * @return a list of byte arrays from {@code src} now not containing {@code delimiter} + */ + public static List split(byte[] src, int offset, int length, byte[] delimiter) { + List parts = new LinkedList(); + int idx = offset; + int lastSplitEnd = offset; + while(idx <= (offset+length) - delimiter.length) { + if(regionEquals(src, idx, delimiter)) { + // copy the last region of bytes into "parts", copyOfRange is happy with zero-sized ranges + parts.add(Arrays.copyOfRange(src, lastSplitEnd, idx)); + idx += delimiter.length; + lastSplitEnd = idx; + } else { + idx++; + } + } + if(lastSplitEnd == offset + length) + // if the last replacement ended at the end of src, we need a tailing empty entry + parts.add(new byte[0]); + else { + parts.add(Arrays.copyOfRange(src, lastSplitEnd, offset + length)); + } + return parts; + } + + static int bisectLeft(BigInteger[] arr, BigInteger i) { + int n = Arrays.binarySearch(arr, i); + if(n >= 0) + return n; + int ip = (n + 1) * -1; + return ip; + } + + /** + * Compare byte arrays for equality and ordering purposes. Elements in the array + * are interpreted and compared as unsigned bytes. Neither parameter + * may be {@code null} + + * @return return -1, 0, or 1 if {@code l} is less than, equal to, or greater than + * {@code r}. + */ + public static int compareUnsigned(byte[] l, byte[] r) { + for(int idx = 0; idx < l.length && idx < r.length; ++idx) { + if(l[idx] != r[idx]) { + return (l[idx] & 0xFF) < (r[idx] & 0xFF) ? -1 : 1; + } + } + if(l.length == r.length) + return 0; + return l.length < r.length ? -1 : 1; + } + + /** + * Check if a byte array starts with another byte array. + * + * @param array the source byte array + * + * @param prefix the byte array that we are checking if {@code src} + * starts with. + * + * @return {@code true} if {@code array} starts with {@code prefix} + */ + public static boolean startsWith(byte[] array, byte[] prefix) { + if(array.length < prefix.length) { + return false; + } + for(int i = 0; i < prefix.length; ++i) { + if(prefix[i] != array[i]) { + return false; + } + } + return true; + } + + /** + * Scan through an array of bytes to find the first occurrence of a specific value. + * + * @param src array to scan. Must not be {@code null}. + * @param what the value for which to search. + * @param start the index at which to start the search. If this is at or after + * the end of {@code src}, the result will always be {@code -1}. + * @param end the index one past the last entry at which to search + * + * @return return the location of the first instance of {@code value}, or + * {@code -1} if not found. + */ + static int findNext(byte[] src, byte what, int start, int end) { + for(int i = start; i < end; i++) { + if(src[i] == what) + return i; + } + return -1; + } + + /** + * Gets the index of the first element after the next occurrence of the byte sequence [nm] + * @param v the bytes to scan through + * @param n first character to find + * @param m second character to find + * @param start the index at which to start the scan + * + * @return the index after the next occurrence of [nm] + */ + static int findTerminator(byte[] v, byte n, byte m, int start) { + return findTerminator(v, n, m, start, v.length); + } + + /** + * Gets the index of the first element after the next occurrence of the byte sequence [nm] + * @param v the bytes to scan through + * @param n first character to find + * @param m second character to find + * @param start the index at which to start the scan + * @param end the index at which to stop the search (exclusive) + * + * @return the index after the next occurrence of [nm] + */ + static int findTerminator(byte[] v, byte n, byte m, int start, int end) { + int pos = start; + while(true) { + pos = findNext(v, n, pos, end); + if(pos < 0) + return end; + if(pos + 1 == end || v[pos+1] != m) + return pos; + pos += 2; + } + } + + /** + * Computes the first key that would sort outside the range prefixed by {@code key}. + * {@code key} must be non-null, and contain at least some character this is not + * {@code \xFF} (255). + * + * @param key prefix key + * + * @return a newly created byte array + */ + public static byte[] strinc(byte[] key) { + byte[] copy = rstrip(key, (byte)0xff); + if(copy.length == 0) + throw new IllegalArgumentException("No key beyond supplied prefix"); + + // Since rstrip makes sure the last character is not \xff, we can be sure + // we're able to add 1 to it without overflow. + copy[copy.length -1] = (byte) (copy[copy.length - 1] + 1); + return copy; + } + + /** + * Get a copy of an array, with all matching characters stripped from trailing edge. + * @param input array to copy. Must not be null. + * @param target byte to exclude from copy. + * @return returns a copy of {@code input} excluding occurrences of {@code target} + * at the end. + */ + static byte[] rstrip(byte[] input, byte target) { + int i = input.length - 1; + for(; i >= 0; i--) { + if(input[i] != target) + break; + } + return Arrays.copyOfRange(input, 0, i + 1); + } + + /** + * Encode an 64-bit integer (long) into a byte array. Encodes the integer in little + * endian byte order. The result is valid for use with + * {@link Transaction#mutate(com.apple.cie.foundationdb.MutationType, byte[], byte[]) Transaction.mutate(...)}. + * + * @param i the number to encode + * @return an 8-byte array containing the + * + * @see Transaction#mutate(com.apple.cie.foundationdb.MutationType, byte[], byte[]) + */ + public static byte[] encodeInt(long i) { + return ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(i).array(); + } + + /** + * Decode a little-endian encoded long integer from an 8-byte array. + * + * @param src the non-null, 8-element byte array from which to decode + * @return a decoded 64-bit integer + */ + public static long decodeInt(byte[] src) { + if(src.length != 8) { + throw new IllegalArgumentException("Source array must be of length 8"); + } + return ByteBuffer.wrap(src).order(ByteOrder.LITTLE_ENDIAN).getLong(); + } + + /** + * Gets a human readable version of a byte array. The bytes that correspond with + * ASCII printable characters [32-127) are passed through. Other bytes are + * replaced with {@code \x} followed by a two character zero-padded hex code for the + * byte. + * + * @param val the byte array for which to create a human readable form + * + * @return a modification of the byte array with unprintable characters replaced. + */ + public static String printable(byte[] val) { + if(val == null) + return null; + StringBuilder s = new StringBuilder(); + for(int i=0; i= 32 && b < 127 && b != '\\') s.append((char)b); + else if (b == '\\') s.append("\\\\"); + else s.append(String.format("\\x%02x", b)); + } + return s.toString(); + } + + private ByteArrayUtil() {} +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/tuple/Tuple.java b/bindings/java/src/main/com/apple/cie/foundationdb/tuple/Tuple.java new file mode 100644 index 0000000000..5601285ba4 --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/tuple/Tuple.java @@ -0,0 +1,540 @@ +/* + * Tuple.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.tuple; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +import com.apple.cie.foundationdb.Range; + +/** + * Represents a set of elements that make up a sortable, typed key. This object + * is comparable with other {@code Tuple}s and will sort in Java in + * the same order in which they would sort in FoundationDB. {@code Tuple}s sort + * first by the first element, then by the second, etc. This makes the tuple layer + * ideal for building a variety of higher-level data models.
    + *

    Types

    + * A {@code Tuple} can + * contain byte arrays ({@code byte[]}), {@link String}s, {@link Number}s, and {@code null}. All + * {@code Number}s will be converted to a {@code long} integral value, so all + * floating point information will be lost and their range will be constrained to the range + * [{@code 2^63-1}, {@code -2^63}]. Note that for numbers outside this range the way that Java + * truncates integral values may yield unexpected results.
    + *

    {@code null} values

    + * The FoundationDB tuple specification has a special type-code for {@code None}; {@code nil}; or, + * as Java would understand it, {@code null}. + * The behavior of the layer in the presence of {@code null} varies by type with the intention + * of matching expected behavior in Java. {@code byte[]} and {@link String}s can be {@code null}, + * where integral numbers (i.e. {@code long}s) cannot. + * This means that the typed getters ({@link #getBytes(int) getBytes()} and {@link #getString(int) getString()}) + * will return {@code null} if the entry at that location was {@code null} and the typed adds + * ({@link #add(byte[])} and {@link #add(String)}) will accept {@code null}. The + * {@link #getLong(int) typed get for integers}, however, will throw a {@code NullPointerException} if + * the entry in the {@code Tuple} was {@code null} at that position.
    + *
    + * This class is not thread safe. + */ +public class Tuple implements Comparable, Iterable { + private List elements; + + private Tuple(List elements, Object newItem) { + this(new LinkedList(elements)); + this.elements.add(newItem); + } + + private Tuple(List elements) { + this.elements = new ArrayList(elements); + } + + /** + * Creates a copy of this {@code Tuple} with an appended last element. The parameter + * is untyped but only {@link String}, {@code byte[]}, {@link Number}s, and {@code null} are allowed. + * All {@code Number}s are converted to a 8 byte integral value, so all floating point + * information is lost. + * + * @param o the object to append. Must be {@link String}, {@code byte[]}, + * {@link Number}s, or {@code null}. + * + * @return a newly created {@code Tuple} + */ + public Tuple addObject(Object o) { + if(o != null && + !(o instanceof String) && + !(o instanceof byte[]) && + !(o instanceof Number)) { + throw new IllegalArgumentException("Parameter type (" + o.getClass().getName() + ") not recognized"); + } + return new Tuple(this.elements, o); + } + + /** + * Creates a copy of this {@code Tuple} with a {@code String} appended as the last element. + * + * @param s the {@code String} to append + * + * @return a newly created {@code Tuple} + */ + public Tuple add(String s) { + return new Tuple(this.elements, s); + } + + /** + * Creates a copy of this {@code Tuple} with a {@code long} appended as the last element. + * + * @param l the number to append + * + * @return a newly created {@code Tuple} + */ + public Tuple add(long l) { + return new Tuple(this.elements, l); + } + + /** + * Creates a copy of this {@code Tuple} with a {@code byte} array appended as the last element. + * + * @param b the {@code byte}s to append + * + * @return a newly created {@code Tuple} + */ + public Tuple add(byte[] b) { + return new Tuple(this.elements, b); + } + + /** + * Creates a copy of this {@code Tuple} with a {@code byte} array appended as the last element. + * + * @param b the {@code byte}s to append + * @param offset the starting index of {@code b} to add + * @param length the number of elements of {@code b} to copy into this {@code Tuple} + * + * @return a newly created {@code Tuple} + */ + public Tuple add(byte[] b, int offset, int length) { + return new Tuple(this.elements, Arrays.copyOfRange(b, offset, offset + length)); + } + + /** + * Create a copy of this {@code Tuple} with a list of items appended. + * + * @param o the list of objects to append. Elements must be {@link String}, {@code byte[]}, + * {@link Number}s, or {@code null}. + * + * @return a newly created {@code Tuple} + */ + public Tuple addAll(List o) { + List merged = new ArrayList(o.size() + this.elements.size()); + merged.addAll(this.elements); + merged.addAll(o); + return new Tuple(merged); + } + + /** + * Create a copy of this {@code Tuple} with all elements from anther {@code Tuple} appended. + * + * @param other the {@code Tuple} whose elements should be appended + * + * @return a newly created {@code Tuple} + */ + public Tuple addAll(Tuple other) { + List merged = new ArrayList(this.size() + other.size()); + merged.addAll(this.elements); + merged.addAll(other.peekItems()); + return new Tuple(merged); + } + + /** + * Get an encoded representation of this {@code Tuple}. Each element is encoded to + * {@code byte}s and concatenated. + * + * @return a serialized representation of this {@code Tuple}. + */ + public byte[] pack() { + return TupleUtil.pack(elements); + } + + /** + * Gets the unserialized contents of this {@code Tuple}. + * + * @return the elements that make up this {@code Tuple}. + */ + public List getItems() { + return new ArrayList(elements); + } + + /** + * Returns the internal elements that make up this tuple. For internal use only, as + * modifications to the result will mean that this Tuple is modified. + * + * @return the elements of this Tuple, without copying + */ + private List peekItems() { + return this.elements; + } + + /** + * Gets an {@code Iterator} over the {@code Objects} in this {@code Tuple}. This {@code Iterator} is + * unmodifiable and will throw an exception if {@link Iterator#remove() remove()} is called. + * + * @return an unmodifiable {@code Iterator} over the elements in the {@code Tuple}. + */ + @Override + public Iterator iterator() { + return Collections.unmodifiableList(this.elements).iterator(); + } + + /** + * Construct a new empty {@code Tuple}. After creation, items can be added + * with calls the the variations of {@code add()}. + * + * @see #from(Object...) + * @see #fromBytes(byte[]) + * @see #fromItems(Iterable) + */ + public Tuple() { + this.elements = new LinkedList(); + } + + /** + * Construct a new {@code Tuple} with elements decoded from a supplied {@code byte} array. + * + * @param bytes encoded {@code Tuple} source. Must not be {@code null} + * + * @return a newly constructed object. + */ + public static Tuple fromBytes(byte[] bytes) { + return fromBytes(bytes, 0, bytes.length); + } + + /** + * Construct a new {@code Tuple} with elements decoded from a supplied {@code byte} array. + * + * @param bytes encoded {@code Tuple} source. Must not be {@code null} + * + * @return a newly constructed object. + */ + public static Tuple fromBytes(byte[] bytes, int offset, int length) { + Tuple t = new Tuple(); + t.elements = TupleUtil.unpack(bytes, offset, length); + return t; + } + + /** + * Gets the number of elements in this {@code Tuple}. + * + * @return the count of elements + */ + public int size() { + return this.elements.size(); + } + + /** + * Determine if this {@code Tuple} contains no elements. + * + * @return {@code true} if this {@code Tuple} contains no elements, {@code false} otherwise + */ + public boolean isEmpty() { + return this.elements.isEmpty(); + } + + /** + * Gets an indexed item as a {@code long}. This function will not do type conversion + * and so will throw a {@code ClassCastException} if the element is not a number type. + * The element at the index may not be {@code null}. + * + * @param index the location of the item to return + * + * @return the item at {@code index} as a {@code long} + */ + public long getLong(int index) { + Object o = this.elements.get(index); + if(o == null) + throw new NullPointerException("Number types in Tuples may not be null"); + return ((Number)o).longValue(); + } + + /** + * Gets an indexed item as a {@code byte[]}. This function will not do type conversion + * and so will throw a {@code ClassCastException} if the tuple element is not a + * {@code byte} array. + * + * @param index the location of the element to return + * + * @return the item at {@code index} as a {@code byte[]} + */ + public byte[] getBytes(int index) { + Object o = this.elements.get(index); + // Check needed, since the null may be of type "Object" and may not be casted to byte[] + if(o == null) + return null; + return (byte[])o; + } + + /** + * Gets an indexed item as a {@code String}. This function will not do type conversion + * and so will throw a {@code ClassCastException} if the tuple element is not of + * {@code String} type. + * + * @param index the location of the element to return + * + * @return the item at {@code index} as a {@code String} + */ + public String getString(int index) { + Object o = this.elements.get(index); + // Check needed, since the null may be of type "Object" and may not be casted to byte[] + if(o == null) { + return null; + } + return (String)o; + } + + /** + * Gets an indexed item without forcing a type. + * + * @param index the index of the item to return + * + * @return an item from the list, without forcing type conversion + */ + public Object get(int index) { + return this.elements.get(index); + } + + /** + * Creates a new {@code Tuple} with the first item of this {@code Tuple} removed. + * + * @return a newly created {@code Tuple} + */ + public Tuple popFront() { + if(elements.size() == 0) + throw new IllegalStateException("Tuple contains no elements"); + + + List items = new ArrayList(elements.size() - 1); + for(int i = 1; i < this.elements.size(); i++) { + items.add(this.elements.get(i)); + } + return new Tuple(items); + } + + /** + * Creates a new {@code Tuple} with the last item of this {@code Tuple} removed. + * + * @return a newly created {@code Tuple} + */ + public Tuple popBack() { + if(elements.size() == 0) + throw new IllegalStateException("Tuple contains no elements"); + + + List items = new ArrayList(elements.size() - 1); + for(int i = 0; i < this.elements.size() - 1; i++) { + items.add(this.elements.get(i)); + } + return new Tuple(items); + } + + /** + * Returns a range representing all keys that encode {@code Tuple}s strictly starting + * with this {@code Tuple}. + *
    + *
    + * For example: + *
    +	 *   Tuple t = Tuple.from("a", "b");
    +	 *   Range r = t.range();
    + * {@code r} includes all tuples ("a", "b", ...) + * + * @return the keyspace range containing all {@code Tuple}s that have this {@code Tuple} + * as a prefix. + */ + public Range range() { + byte[] p = pack(); + //System.out.println("Packed tuple is: " + ByteArrayUtil.printable(p)); + return new Range(ByteArrayUtil.join(p, new byte[] {0x0}), + ByteArrayUtil.join(p, new byte[] {(byte)0xff})); + } + + /** + * Compare the byte-array representation of this {@code Tuple} against another. This method + * will sort {@code Tuple}s in the same order that they would be sorted as keys in + * FoundationDB. Returns a negative integer, zero, or a positive integer when this object's + * byte-array representation is found to be less than, equal to, or greater than the + * specified {@code Tuple}. + * + * @param t the {@code Tuple} against which to compare + * + * @return a negative integer, zero, or a positive integer when this {@code Tuple} is + * less than, equal, or greater than the parameter {@code t}. + */ + @Override + public int compareTo(Tuple t) { + return ByteArrayUtil.compareUnsigned(this.pack(), t.pack()); + } + + /** + * Returns a hash code value for this {@code Tuple}. + * {@inheritDoc} + * + * @return a hashcode + */ + @Override + public int hashCode() { + return Arrays.hashCode(this.pack()); + } + + /** + * Tests for equality with another {@code Tuple}. If the passed object is not a {@code Tuple} + * this returns false. If the object is a {@code Tuple}, this returns true if + * {@link Tuple#compareTo(Tuple) compareTo()} would return {@code 0}. + * + * @return {@code true} if {@code obj} is a {@code Tuple} and their binary representation + * is identical. + */ + @Override + public boolean equals(Object o) { + if(o == null) + return false; + if(o instanceof Tuple) { + return Arrays.equals(this.pack(), ((Tuple) o).pack()); + } + return false; + } + + /** + * Returns a string representing this {@code Tuple}. + * + * @return a string + */ + @Override + public String toString() { + StringBuilder s = new StringBuilder("("); + boolean first = true; + + for(Object o : elements) { + if(!first) { + s.append(", "); + } + + first = false; + if(o == null) { + s.append("null"); + } + else if(o instanceof String) { + s.append("\""); + s.append(o); + s.append("\""); + } + else if(o instanceof byte[]) { + s.append("b\""); + s.append(ByteArrayUtil.printable((byte[])o)); + s.append("\""); + } + else { + s.append(o); + } + } + + s.append(")"); + return s.toString(); + } + + /** + * Creates a new {@code Tuple} from a variable number of elements. The elements + * must follow the type guidelines from {@link Tuple#addObject(Object) add}, and so + * can only be {@link String}s, {@code byte[]}s, {@link Number}s, or {@code null}s. + * + * @param items the elements from which to create the {@code Tuple}. + * + * @return a newly created {@code Tuple} + */ + public static Tuple fromItems(Iterable items) { + Tuple t = new Tuple(); + for(Object o : items) { + t = t.addObject(o); + } + return t; + } + + /** + * Efficiently creates a new {@code Tuple} from a list of objects. The elements + * must follow the type guidelines from {@link Tuple#addObject(Object) add}, and so + * can only be {@link String}s, {@code byte[]}s, {@link Number}s, or {@code null}s. + * + * @param items the elements from which to create the {@code Tuple}. + * + * @return a newly created {@code Tuple} + */ + public static Tuple fromList(List items) { + return new Tuple(items); + } + + /** + * Creates a new {@code Tuple} from a variable number of elements. The elements + * must follow the type guidelines from {@link Tuple#addObject(Object) add}, and so + * can only be {@link String}s, {@code byte[]}s, {@link Number}s, or {@code null}s. + * + * @param items the elements from which to create the {@code Tuple}. + * + * @return a newly created {@code Tuple} + */ + public static Tuple from(Object ... items) { + return fromList(Arrays.asList(items)); + } + + static void main(String[] args) { + for( int i : new int[] {10, 100, 1000, 10000, 100000, 1000000} ) { + createTuple(i); + } + + Tuple t = new Tuple(); + t = t.add(Long.MAX_VALUE); + t = t.add(Long.MAX_VALUE - 1); + t = t.add(Long.MAX_VALUE - 2); + t = t.add(1); + t = t.add(0); + t = t.add(-1); + t = t.add(Long.MIN_VALUE + 2); + t = t.add(Long.MIN_VALUE + 1); + t = t.add(Long.MIN_VALUE); + t = t.add("foo"); + byte[] bytes = t.pack(); + System.out.println("Packed: " + ByteArrayUtil.printable(bytes)); + List items = Tuple.fromBytes(bytes).getItems(); + for(Object obj : items) { + System.out.println(" -> type: (" + obj.getClass().getName() + "): " + obj); + } + } + + private static Tuple createTuple(int items) { + List elements = new ArrayList(items); + for(int i = 0; i < items; i++) { + elements.add(new byte[]{99}); + } + long start = System.currentTimeMillis(); + Tuple t = Tuple.fromList(elements); + t.pack(); + System.out.println("Took " + (System.currentTimeMillis() - start) + " ms for " + items + " (" + elements.size() + ")"); + return t; + } +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/tuple/TupleUtil.java b/bindings/java/src/main/com/apple/cie/foundationdb/tuple/TupleUtil.java new file mode 100644 index 0000000000..6cdd93d2be --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/tuple/TupleUtil.java @@ -0,0 +1,233 @@ +/* + * TupleUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.tuple; + +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; + +class TupleUtil { + private static final byte nil = 0x0; + private static final byte[] nil_rep = new byte[] {nil, (byte)0xFF}; + private static final BigInteger[] size_limits; + private static final Charset UTF8; + + static { + size_limits = new BigInteger[9]; + for(int i = 0; i < 9; i++) { + size_limits[i] = (BigInteger.ONE).shiftLeft(i * 8).subtract(BigInteger.ONE); + } + UTF8 = Charset.forName("UTF-8"); + } + + static class DecodeResult { + final int end; + final Object o; + + DecodeResult(int pos, Object o) { + this.end = pos; + this.o = o; + } + } + + public static byte[] join(List items) { + return ByteArrayUtil.join(null, items); + } + + static byte[] encode(Object t) { + if(t == null) + return new byte[] {nil}; + if(t instanceof byte[]) + return encode((byte[])t); + if(t instanceof String) + return encode((String)t); + if(t instanceof Number) + return encode(((Number)t).longValue()); + throw new IllegalArgumentException("Unsupported data type: " + t.getClass().getName()); + } + + static byte[] encode(byte[] bytes) { + List list = new ArrayList(3); + list.add(new byte[] {0x1}); + list.add(ByteArrayUtil.replace(bytes, new byte[] {0x0}, nil_rep)); + list.add(new byte[] {0x0}); + + //System.out.println("Joining bytes..."); + return ByteArrayUtil.join(null, list); + } + + static byte[] encode(String s) { + List list = new ArrayList(3); + list.add(new byte[] {0x2}); + list.add(ByteArrayUtil.replace(s.getBytes(UTF8), new byte[] {0x0}, nil_rep)); + list.add(new byte[] {0x0}); + + //System.out.println("Joining string..."); + return ByteArrayUtil.join(null, list); + } + + static byte[] encode(long i) { + //System.out.println("Encoding integral " + i); + if(i == 0) { + return new byte[] { 20 }; + } + if(i > 0) { + int n = ByteArrayUtil.bisectLeft(size_limits, BigInteger.valueOf(i)); + assert n <= size_limits.length; + byte[] bytes = ByteBuffer.allocate(8).order(ByteOrder.BIG_ENDIAN).putLong(i).array(); + //System.out.println(" -- integral has 'n' of " + n + " and output bytes of " + bytes.length); + byte[] result = new byte[n+1]; + result[0] = (byte)(20 + n); + System.arraycopy(bytes, bytes.length - n, result, 1, n); + return result; + } + BigInteger bI = BigInteger.valueOf(i); + int n = ByteArrayUtil.bisectLeft(size_limits, bI.negate()); + + assert n >= 0 && n < size_limits.length; // can we do this? it seems to be required for the following statement + + long maxv = size_limits[n].add(bI).longValue(); + byte[] bytes = ByteBuffer.allocate(8).order(ByteOrder.BIG_ENDIAN).putLong(maxv).array(); + byte[] result = new byte[n+1]; + result[0] = (byte)(20 - n); + System.arraycopy(bytes, bytes.length - n, result, 1, n); + return result; + } + + static byte[] encode(Integer i) { + return encode(i.longValue()); + } + + static DecodeResult decode(byte[] rep, int pos, int last) { + //System.out.println("Decoding '" + ArrayUtils.printable(rep) + "' at " + pos); + + // SOMEDAY: codes over 127 will be a problem with the signed Java byte mess + int code = rep[pos]; + int start = pos + 1; + if(code == 0x0) { + return new DecodeResult(start, null); + } + if(code == 0x1) { + int end = ByteArrayUtil.findTerminator(rep, (byte)0x0, (byte)0xff, start, last); + //System.out.println("End of byte string: " + end); + byte[] range = ByteArrayUtil.replace(rep, start, end - start, nil_rep, new byte[] { nil }); + //System.out.println(" -> byte string contents: '" + ArrayUtils.printable(range) + "'"); + return new DecodeResult(end + 1, range); + } + if(code == 0x2) { + int end = ByteArrayUtil.findTerminator(rep, (byte)0x0, (byte)0xff, start, last); + //System.out.println("End of UTF8 string: " + end); + byte[] stringBytes = ByteArrayUtil.replace(rep, start, end - start, nil_rep, new byte[] { nil }); + String str = new String(stringBytes, UTF8); + //System.out.println(" -> UTF8 string contents: '" + str + "'"); + return new DecodeResult(end + 1, str); + } + if(code >=12 && code <=28) { + // decode a long + byte[] longBytes = new byte[9]; + Arrays.fill(longBytes, (byte)0); + boolean upper = code >= 20; + int n = upper ? code - 20 : 20 - code; + int end = start + n; + + if(rep.length < end) { + throw new RuntimeException("Invalid tuple (possible truncation)"); + } + + System.arraycopy(rep, start, longBytes, 9-n, n); + if (!upper) + for(int i=9-n; i<9; i++) + longBytes[i] = (byte)~longBytes[i]; + + BigInteger val = new BigInteger(longBytes); + if (!upper) val = val.negate(); + + if (val.compareTo(BigInteger.valueOf(Long.MIN_VALUE))<0 || + val.compareTo(BigInteger.valueOf(Long.MAX_VALUE))>0) + throw new RuntimeException("Value out of range for type long."); + + return new DecodeResult(end, val.longValue()); + } + throw new IllegalArgumentException("Unknown tuple data type " + code + " at index " + pos); + } + + static List unpack(byte[] bytes, int start, int length) { + List items = new LinkedList(); + int pos = start; + int end = start + length; + while(pos < bytes.length) { + DecodeResult decoded = decode(bytes, pos, end); + items.add(decoded.o); + pos = decoded.end; + } + return items; + } + + static byte[] pack(List items) { + if(items.size() == 0) + return new byte[0]; + + List parts = new ArrayList(items.size()); + for(Object t : items) { + //System.out.println("Starting encode: " + ArrayUtils.printable((byte[])t)); + byte[] encoded = encode(t); + //System.out.println(" encoded -> '" + ArrayUtils.printable(encoded) + "'"); + parts.add(encoded); + } + //System.out.println("Joining whole tuple..."); + return ByteArrayUtil.join(null, parts); + } + + public static void main(String[] args) { + try { + byte[] bytes = encode( 4 ); + assert 4 == (Integer)(decode( bytes, 0, bytes.length ).o); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Error " + e.getMessage()); + } + + try { + byte[] bytes = encode( "\u021Aest \u0218tring" ); + String string = (String)(decode( bytes, 0, bytes.length ).o); + System.out.println("contents -> " + string); + assert "\u021Aest \u0218tring" == string; + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Error " + e.getMessage()); + } + + /*Object[] a = new Object[] { "\u0000a", -2, "b\u0001", 12345, ""}; + List o = Arrays.asList(a); + byte[] packed = pack( o ); + System.out.println("packed length: " + packed.length); + o = unpack( packed ); + System.out.println("unpacked elements: " + packed); + for(Object obj : o) + System.out.println(" -> type: " + obj.getClass().getName());*/ + } + private TupleUtil() {} +} diff --git a/bindings/java/src/main/com/apple/cie/foundationdb/tuple/package-info.java b/bindings/java/src/main/com/apple/cie/foundationdb/tuple/package-info.java new file mode 100644 index 0000000000..3f1b37369a --- /dev/null +++ b/bindings/java/src/main/com/apple/cie/foundationdb/tuple/package-info.java @@ -0,0 +1,29 @@ +/* + * package-info.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Provides a set of utilities for serializing and deserializing typed data + * for use in FoundationDB. When packed together into a {@link com.apple.cie.foundationdb.tuple.Tuple} + * this data is suitable for use as an index or organizational structure within FoundationDB + * keyspace. See general Tuple + * documentation for information about how Tuples sort and can be used to efficiently + * model data. + */ +package com.apple.cie.foundationdb.tuple; \ No newline at end of file diff --git a/bindings/java/src/main/overview.html.in b/bindings/java/src/main/overview.html.in new file mode 100644 index 0000000000..1244f5860d --- /dev/null +++ b/bindings/java/src/main/overview.html.in @@ -0,0 +1,101 @@ + + +This documents the client API for using FoundationDB from Java.
    +
    +

    Installation

    +FoundationDB's Java bindings rely on native libraries that are installed as part of the +FoundationDB client binaries installation (see + +Installing FoundationDB client binaries). The FoundationDB Java bindings are available +through Artifactory. To use them in your Maven-enabled project, add a dependency to your +pom.xml like:
    +
    +{@code
    +
    +    com.apple.cie.foundationdb
    +    fdb-java
    +    VERSION
    +
    +}
    +
    +Alternatively, simply download the JAR from +Artifactory +and add it to your classpath.
    +
    +

    Getting started

    +To start using FoundationDB from Java, create an instance of the +{@link FDB FoundationDB API interface} with the version of the +API that you want to use (this release of the FoundationDB Java API supports only version {@code 500}). +With this API object you can then open {@link Cluster}s and +{@link Database}s and start using {@link Transaction}s. +Here we give an example. The example relies on a cluster file at the +default location +for your platform and a running server.
    +
    +
    +{@code
    +import com.apple.cie.foundationdb.*;
    +import Function;
    +import Tuple;
    +
    +public class Example {
    +  public static void main(String[] args) {
    +    FDB fdb = FDB.selectAPIVersion(500);
    +    Database db = fdb.open();
    +
    +    // Run an operation on the database
    +    db.run(new Function() {
    +      public Void apply(Transaction tr) {
    +        tr.set(Tuple.from("hello").pack(), Tuple.from("world").pack());
    +        return null;
    +      }
    +    });
    +
    +    // Get the value of 'hello' from the database
    +    String hello = db.run(new Function() {
    +      public String apply(Transaction tr) {
    +        byte[] result = tr.get(Tuple.from("hello").pack()).get();
    +        return Tuple.fromBytes(result).getString(0);
    +      }
    +    });
    +    System.out.println("Hello " + hello);
    +  }
    +}
    +}
    +
    +

    FoundationDB {@link com.apple.cie.foundationdb.tuple Tuple API}

    +The {@link com.apple.cie.foundationdb.tuple Tuple API} is provided with the core Java API for FoundationDB. +This layer is provided in some form in all official language bindings. It enables +cross-language support for storing and retrieving typed data from the +binary data that FoundationDB supports. And, just as importantly, data packed into +{@code Tuple}s and used as keys sort in predictable and useful ways. See the +{@link com.apple.cie.foundationdb.tuple Tuple class documentation} for information about use in Java +and general Tuple documentation +for information about how Tuples sort and can be used to efficiently model data. +
    +

    FoundationDB {@link com.apple.cie.foundationdb.directory Directory API}

    +The {@link com.apple.cie.foundationdb.directory Directory API} is provided with the core +Java API for FoundationDB. This layer is provided in some form in all official +language bindings. The FoundationDB API provides directories as a tool for +managing related {@link Subspace}s. Directories are a +recommended approach for administering applications. Each application should +create or open at least one directory to manage its subspaces. Directories are +identified by hierarchical paths analogous to the paths in a Unix-like file system. +A path is represented as a List of strings. Each directory has an associated +subspace used to store its content. The layer maps each path to a short prefix used +for the corresponding subspace. In effect, directories provide a level of indirection +for access to subspaces. +
    +

    {@link com.apple.cie.foundationdb.async.Future Future}s and asynchronous operation

    +Asynchronous FoundationDB operations return {@link Future}s. +A {@link Future} can be used in a blocking way using the +{@link Future#get() get()} method or in a +fully-asynchronous way using the +{@link Future#map(Function) map()} and +{@link Future#flatMap(Function) flatMap()} +methods. Generally, the blocking style is more straightforward and the asynchronous style +is more efficient. Mixing the two styles correctly can be tricky, so consider choosing +one or the other. See the {@linkplain com.apple.cie.foundationdb.async async Package documentation} +for more information. + + diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/AsListTest.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/AsListTest.java new file mode 100644 index 0000000000..af76bf2fe3 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/AsListTest.java @@ -0,0 +1,109 @@ +/* + * AsListTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.LocalityUtil; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.PartialFunction; +import com.apple.cie.foundationdb.async.PartialFuture; +import com.apple.cie.foundationdb.async.ReadyFuture; + +public class AsListTest { + /** + * When the database contains keys a, b, c, d, e -- this should return 5 items, + * a bug made the the addition of the clear into the result returning 0 items. + */ + public static void main(String[] args) { + FDB fdb = FDB.selectAPIVersion(500); + Database database = fdb.open("T:\\circus\\tags\\RebarCluster-bbc\\cluster_id.txt"); + database.options().setLocationCacheSize(42); + Transaction tr = database.createTransaction(); + //tr.clear("g".getBytes()); + /*tr.clear("bbb".getBytes()); + AsyncIterable query = tr.getRange( + KeySelector.firstGreaterOrEqual("a".getBytes()), + KeySelector.firstGreaterOrEqual("e".getBytes()), + Integer.MAX_VALUE); + //List list = query.asList().get(); + //System.out.println("List size: " + list.size()); +*/ + String[] keyAddresses = LocalityUtil.getAddressesForKey(tr, "a".getBytes()).get(); + for(String s : keyAddresses) { + System.out.println(" @ " + s); + } + + @SuppressWarnings("unused") + Future i = AsyncUtil.applySafely(new Function>() { + @Override + public Future apply(Exception o) { + return new ReadyFuture(3); + } + }, new RuntimeException()); + + Future f = null; + PartialFuture pf = null; + + @SuppressWarnings({ "unused", "null" }) + Future g = f.flatMap(new Function>() { + @Override + public Future apply(Integer o) { + return new ReadyFuture( o.toString() ); + } + }); + + @SuppressWarnings("unused") + PartialFuture h = f.flatMap(new PartialFunction>() { + @Override + public Future apply(Integer o) throws Exception { + if (o == null) throw new Exception("AHH"); + return new ReadyFuture( o.toString() ); + } + }); + + @SuppressWarnings("unused") + PartialFuture h2 = f.flatMap(new PartialFunction>() { + @Override + public PartialFuture apply(Integer o) throws Exception { + if (o == null) throw new Exception("AHH"); + return new ReadyFuture( o.toString() ); + } + }); + + @SuppressWarnings({ "unused", "null" }) + Future g2 = pf.flatMap(new Function>() { + @Override + public Future apply(Integer o) { + return new ReadyFuture( o.toString() ); + } + }).rescue(new Function>() { + @Override + public Future apply(Exception o) { + // TODO Auto-generated method stub + return null; + } + }); + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/AsyncDirectoryExtension.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/AsyncDirectoryExtension.java new file mode 100644 index 0000000000..8e1568b484 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/AsyncDirectoryExtension.java @@ -0,0 +1,445 @@ +/* + * AsyncDirectoryExtension.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.ReadyFuture; +import com.apple.cie.foundationdb.directory.Directory; +import com.apple.cie.foundationdb.directory.DirectoryLayer; +import com.apple.cie.foundationdb.directory.DirectorySubspace; +import com.apple.cie.foundationdb.subspace.Subspace; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +class AsyncDirectoryExtension { + List dirList = new ArrayList(); + int dirIndex = 0; + int errorIndex = 0; + + public AsyncDirectoryExtension() { + dirList.add(DirectoryLayer.getDefault()); + } + + Directory directory() { + return (Directory)dirList.get(dirIndex); + } + + Subspace subspace() { + return (Subspace)dirList.get(dirIndex); + } + + Future processInstruction(final Instruction inst) { + return executeInstruction(inst) + .rescueRuntime(new Function>() { + @Override + public Future apply(RuntimeException e) { + DirectoryUtil.pushError(inst, e, dirList); + return ReadyFuture.DONE; + } + }); + } + + Future executeInstruction(final Instruction inst) { + final DirectoryOperation op = DirectoryOperation.valueOf(inst.op); + + if(op == DirectoryOperation.DIRECTORY_CREATE_SUBSPACE) { + return DirectoryUtil.popTuple(inst) + .flatMap(new Function>() { + @Override + public Future apply(final Tuple prefix) { + return inst.popParam() + .map(new Function() { + @Override + public Void apply(Object rawPrefix) { + dirList.add(new Subspace(prefix, (byte[])rawPrefix)); + return null; + } + }); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE_LAYER) { + return inst.popParams(3) + .map(new Function, Void>() { + @Override + public Void apply(List params) { + Subspace nodeSubspace = (Subspace)dirList.get(StackUtils.getInt(params.get(0))); + Subspace contentSubspace = (Subspace)dirList.get(StackUtils.getInt(params.get(1))); + boolean allowManualPrefixes = StackUtils.getInt(params.get(2)) == 1; + + if(nodeSubspace == null || contentSubspace == null) + dirList.add(null); + else + dirList.add(new DirectoryLayer(nodeSubspace, contentSubspace, allowManualPrefixes)); + + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_CHANGE) { + return inst.popParam() + .map(new Function() { + @Override + public Void apply(Object index) { + dirIndex = StackUtils.getInt(index); + if(dirList.get(dirIndex) == null) + dirIndex = errorIndex; + + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_SET_ERROR_INDEX) { + return inst.popParam() + .map(new Function() { + @Override + public Void apply(Object index) { + errorIndex = StackUtils.getInt(index); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN || op == DirectoryOperation.DIRECTORY_OPEN) { + return DirectoryUtil.popPath(inst) + .flatMap(new Function, Future>() { + @Override + public Future apply(final List path) { + return inst.popParam() + .flatMap(new Function>() { + @Override + public Future apply(Object layer) { + Future dir; + if(layer == null) { + if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN) + dir = directory().createOrOpen(inst.tcx, path); + else + dir = directory().open(inst.readTcx, path); + } + else { + if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN) + dir = directory().createOrOpen(inst.tcx, path, (byte[])layer); + else + dir = directory().open(inst.readTcx, path, (byte[])layer); + } + + return dir.map(new Function() { + @Override + public Void apply(DirectorySubspace dirSubspace) { + dirList.add(dirSubspace); + return null; + } + }); + } + }); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE) { + return DirectoryUtil.popPath(inst) + .flatMap(new Function, Future>() { + @Override + public Future apply(final List path) { + return inst.popParams(2) + .flatMap(new Function, Future>() { + @Override + public Future apply(List params) { + byte[] layer = (byte[])params.get(0); + byte[] prefix = (byte[])params.get(1); + + Future dir; + if(layer == null && prefix == null) + dir = directory().create(inst.tcx, path); + else if(prefix == null) + dir = directory().create(inst.tcx, path, layer); + else { + if(layer == null) + layer = new byte[0]; + + dir = directory().create(inst.tcx, path, layer, prefix); + } + + return dir.map(new Function() { + @Override + public Void apply(DirectorySubspace dirSubspace) { + dirList.add(dirSubspace); + return null; + } + }); + } + }); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_MOVE) { + return DirectoryUtil.popPaths(inst, 2) + .flatMap(new Function>, Future>() { + @Override + public Future apply(List> paths) { + return directory().move(inst.tcx, paths.get(0), paths.get(1)); + } + }) + .map(new Function() { + @Override + public Void apply(DirectorySubspace dirSubspace) { + dirList.add(dirSubspace); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_MOVE_TO) { + return DirectoryUtil.popPath(inst) + .flatMap(new Function, Future>() { + @Override + public Future apply(List newAbsolutePath) { + return directory().moveTo(inst.tcx, newAbsolutePath); + } + }) + .map(new Function() { + @Override + public Void apply(DirectorySubspace dirSubspace) { + dirList.add(dirSubspace); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_REMOVE) { + return inst.popParam() + .flatMap(new Function>>>() { + @Override + public Future>> apply(Object count) { + return DirectoryUtil.popPaths(inst, StackUtils.getInt(count)); + } + }) + .flatMap(new Function>, Future>() { + @Override + public Future apply(List> path) { + if(path.size() == 0) + return directory().remove(inst.tcx); + else + return directory().remove(inst.tcx, path.get(0)); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_REMOVE_IF_EXISTS) { + return inst.popParam() + .flatMap(new Function>>>() { + @Override + public Future>> apply(Object count) { + return DirectoryUtil.popPaths(inst, StackUtils.getInt(count)); + } + }) + .flatMap(new Function>, Future>() { + @Override + public Future apply(List> path) { + if(path.size() == 0) + return AsyncUtil.success(directory().removeIfExists(inst.tcx)); + else + return AsyncUtil.success(directory().removeIfExists(inst.tcx, path.get(0))); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_LIST) { + return inst.popParam() + .flatMap(new Function>>>() { + @Override + public Future>> apply(Object count) { + return DirectoryUtil.popPaths(inst, StackUtils.getInt(count)); + } + }) + .flatMap(new Function>, Future>>() { + @Override + public Future> apply(List> path) { + if(path.size() == 0) + return directory().list(inst.readTcx); + else + return directory().list(inst.readTcx, path.get(0)); + } + }) + .map(new Function, Void>() { + @Override + public Void apply(List children) { + inst.push(Tuple.fromItems(children).pack()); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_EXISTS) { + return inst.popParam() + .flatMap(new Function>>>() { + @Override + public Future>> apply(Object count) { + return DirectoryUtil.popPaths(inst, StackUtils.getInt(count)); + } + }) + .flatMap(new Function>, Future>() { + @Override + public Future apply(List> path) { + if(path.size() == 0) + return directory().exists(inst.readTcx); + else + return directory().exists(inst.readTcx, path.get(0)); + } + }) + .map(new Function() { + @Override + public Void apply(Boolean exists){ + inst.push(exists ? 1 : 0); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_PACK_KEY) { + return DirectoryUtil.popTuple(inst) + .map(new Function() { + @Override + public Void apply(Tuple keyTuple) { + inst.push(subspace().pack(keyTuple)); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_UNPACK_KEY) { + return inst.popParam() + .map(new Function() { + @Override + public Void apply(Object key) { + Tuple tup = subspace().unpack((byte[])key); + for(Object o : tup) + inst.push(o); + + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_RANGE) { + return DirectoryUtil.popTuple(inst) + .map(new Function() { + @Override + public Void apply(Tuple tup) { + Range range = subspace().range(tup); + inst.push(range.begin); + inst.push(range.end); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_CONTAINS) { + return inst.popParam() + .map(new Function() { + @Override + public Void apply(Object key) { + inst.push(subspace().contains((byte[])key) ? 1 : 0); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_OPEN_SUBSPACE) { + return DirectoryUtil.popTuple(inst) + .map(new Function() { + @Override + public Void apply(Tuple prefix) { + dirList.add(subspace().subspace(prefix)); + return null; + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_LOG_SUBSPACE) { + return inst.popParam() + .map(new Function() { + @Override + public Void apply(final Object prefix) { + return inst.tcx.run(new Function() { + @Override + public Void apply(Transaction tr) { + tr.set(ByteArrayUtil.join((byte[])prefix, new Tuple().add(dirIndex).pack()), subspace().getKey()); + return null; + } + }); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_LOG_DIRECTORY) { + return inst.popParam() + .flatMap(new Function>() { + @Override + public Future apply(Object prefix) { + final Subspace logSubspace = new Subspace(new Tuple().add(dirIndex), (byte[])prefix); + return inst.tcx.run(new Function>() { + @Override + public Future apply(final Transaction tr) { + return directory().exists(tr) + .flatMap(new Function>>() { + @Override + public Future> apply(Boolean exists) { + tr.set(logSubspace.pack("path"), Tuple.fromItems(directory().getPath()).pack()); + tr.set(logSubspace.pack("layer"), new Tuple().add(directory().getLayer()).pack()); + tr.set(logSubspace.pack("exists"), new Tuple().add(exists ? 1 : 0).pack()); + if(exists) + return directory().list(tr); + else + return new ReadyFuture>(new ArrayList()); + } + }) + .map(new Function, Void>() { + @Override + public Void apply(List children) { + tr.set(logSubspace.pack("children"), Tuple.fromItems(children).pack()); + return null; + } + }); + } + }); + } + }); + } + else if(op == DirectoryOperation.DIRECTORY_STRIP_PREFIX) { + return inst.popParam() + .map(new Function() { + @Override + public Void apply(Object param) { + byte[] str = (byte[])param; + byte[] rawPrefix = subspace().getKey(); + + if(str.length < rawPrefix.length) + throw new RuntimeException("String does not start with raw prefix"); + + for(int i = 0; i < rawPrefix.length; ++i) + if(str[i] != rawPrefix[i]) + throw new RuntimeException("String does not start with raw prefix"); + + inst.push(Arrays.copyOfRange(str, rawPrefix.length, str.length)); + return null; + } + }); + } + else { + throw new RuntimeException("Unknown operation:" + inst.op); + } + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/AsyncStackTester.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/AsyncStackTester.java new file mode 100644 index 0000000000..f38c3e56a0 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/AsyncStackTester.java @@ -0,0 +1,858 @@ +/* + * AsyncStackTester.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.*; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.KeySelector; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.MutationType; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.ReadTransaction; +import com.apple.cie.foundationdb.StreamingMode; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.ReadyFuture; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +public class AsyncStackTester { + static final String DIRECTORY_PREFIX = "DIRECTORY_"; + + static class WaitEmpty implements Function> { + private final byte[] prefix; + WaitEmpty(byte[] prefix) { + this.prefix = prefix; + } + + @Override + public Future apply(Transaction tr) { + return tr.getRange(Range.startsWith(prefix)).asList().map(new Function, Void>() { + @Override + public Void apply(List list) { + if(list.size() > 0) { + //System.out.println(" - Throwing new fake commit error..."); + throw new FDBException("ERROR: Fake commit conflict", 1020); + } + return null; + } + }); + } + } + + static Future processInstruction(final Instruction inst) { + StackOperation op = StackOperation.valueOf(inst.op); + if(op == StackOperation.PUSH) { + Object item = inst.tokens.get(1); + inst.push(item); + /*if(item instanceof byte[]) + System.out.println(inst.context.preStr + " - " + "Pushing '" + ByteArrayUtil.printable((byte[]) item) + "'"); + else if(item instanceof Number) + System.out.println(inst.context.preStr + " - " + "Pushing " + ((Number)item).longValue()); + else if(item instanceof String) + System.out.println(inst.context.preStr + " - " + "Pushing (utf8) '" + item.toString() + "'"); + else if(item == null) + System.out.println(inst.context.preStr + " - " + "Pushing null"); + else + System.out.println(inst.context.preStr + " - " + "Pushing item of type " + item.getClass().getName());*/ + return ReadyFuture.DONE; + } + else if(op == StackOperation.POP) { + inst.pop(); + return ReadyFuture.DONE; + } + else if(op == StackOperation.DUP) { + if(inst.size() == 0) + throw new RuntimeException("No stack bro!! (" + inst.context.preStr + ")"); + StackEntry e = inst.pop(); + inst.push(e); + inst.push(e); + return ReadyFuture.DONE; + } + else if(op == StackOperation.EMPTY_STACK) { + inst.clear(); + return ReadyFuture.DONE; + } + else if(op == StackOperation.SWAP) { + return inst.popParam() + .flatMap(new Function>() { + @Override + public Future apply(Object param) { + int index = StackUtils.getInt(param); + if(index >= inst.size()) + throw new IllegalArgumentException("Stack index not valid"); + + inst.swap(index); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.WAIT_FUTURE) { + return popAndWait(inst) + .flatMap(new Function>() { + @Override + public Future apply(StackEntry e) { + inst.push(e); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.WAIT_EMPTY) { + return inst.popParam() + .flatMap(new Function>() { + @Override + public Future apply(Object param) { + WaitEmpty retryable = new WaitEmpty((byte[])param); + return inst.context.db.runAsync(retryable).map(new Function() { + @Override + public Void apply(Void o) { + inst.push( "WAITED_FOR_EMPTY".getBytes()); + return null; + } + }); + } + }); + } + else if(op == StackOperation.START_THREAD) { + return inst.popParam() + .flatMap(new Function>() { + @Override + public Future apply(Object param) { + //System.out.println(inst.context.preStr + " - " + "Starting new thread at prefix: " + ByteArrayUtil.printable((byte[]) params.get(0))); + inst.context.addContext((byte[])param); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.NEW_TRANSACTION) { + inst.context.newTransaction(); + return ReadyFuture.DONE; + } + else if(op == StackOperation.USE_TRANSACTION) { + return inst.popParam() + .map(new Function() { + public Void apply(Object param) { + inst.context.switchTransaction((byte[])param); + return null; + } + }); + } + else if(op == StackOperation.SET) { + return inst.popParams(2).flatMap(new Function, Future>() { + @Override + public Future apply(final List params) { + /*System.out.println(inst.context.preStr + " - " + "Setting '" + ByteArrayUtil.printable((byte[]) params.get(0)) + + "' to '" + ByteArrayUtil.printable((byte[]) params.get(1)) + "'"); */ + return executeMutation(inst, new Function>() { + @Override + public Future apply(Transaction tr) { + tr.set((byte[])params.get(0), (byte[])params.get(1)); + return ReadyFuture.DONE; + } + }); + } + }); + } + else if(op == StackOperation.CLEAR) { + return inst.popParam().flatMap(new Function>() { + @Override + public Future apply(final Object param) { + //System.out.println(inst.context.preStr + " - " + "Clearing: '" + ByteArrayUtil.printable((byte[])param) + "'"); + return executeMutation(inst, new Function>() { + @Override + public Future apply(Transaction tr) { + tr.clear((byte[])param); + return ReadyFuture.DONE; + } + }); + } + }); + } + else if(op == StackOperation.CLEAR_RANGE) { + return inst.popParams(2).flatMap(new Function, Future>() { + @Override + public Future apply(final List params) { + return executeMutation(inst, new Function>() { + @Override + public Future apply(Transaction tr) { + tr.clear((byte[])params.get(0), (byte[])params.get(1)); + return ReadyFuture.DONE; + } + }); + } + }); + } + else if(op == StackOperation.CLEAR_RANGE_STARTS_WITH) { + return inst.popParam().flatMap(new Function>() { + @Override + public Future apply(final Object param) { + return executeMutation(inst, new Function>() { + @Override + public Future apply(Transaction tr) { + tr.clear(Range.startsWith((byte[])param)); + return ReadyFuture.DONE; + } + }); + } + }); + } + else if(op == StackOperation.ATOMIC_OP) { + return inst.popParams(3).flatMap(new Function, Future>() { + @Override + public Future apply(final List params) { + final MutationType optype = MutationType.valueOf((String)params.get(0)); + return executeMutation(inst, + new Function>() { + @Override + public Future apply(Transaction tr) { + tr.mutate(optype, (byte[])params.get(1), (byte[])params.get(2)); + return ReadyFuture.DONE; + } + } + ); + } + }); + } + else if(op == StackOperation.COMMIT) { + inst.push(inst.tr.commit()); + return ReadyFuture.DONE; + } + else if(op == StackOperation.RESET) { + inst.setTransaction(inst.tr.reset()); + return ReadyFuture.DONE; + } + else if(op == StackOperation.CANCEL) { + inst.tr.cancel(); + return ReadyFuture.DONE; + } + else if(op == StackOperation.READ_CONFLICT_RANGE) { + return inst.popParams(2).flatMap(new Function, Future>() { + @Override + public Future apply(List params) { + inst.tr.addReadConflictRange((byte[])params.get(0), (byte[])params.get(1)); + inst.push("SET_CONFLICT_RANGE".getBytes()); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.WRITE_CONFLICT_RANGE) { + return inst.popParams(2).flatMap(new Function, Future>() { + @Override + public Future apply(List params) { + inst.tr.addWriteConflictRange((byte[])params.get(0), (byte[])params.get(1)); + inst.push("SET_CONFLICT_RANGE".getBytes()); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.READ_CONFLICT_KEY) { + return inst.popParam().flatMap(new Function>() { + @Override + public Future apply(Object param) { + inst.tr.addReadConflictKey((byte[])param); + inst.push("SET_CONFLICT_KEY".getBytes()); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.WRITE_CONFLICT_KEY) { + return inst.popParam().flatMap(new Function>() { + @Override + public Future apply(Object param) { + inst.tr.addWriteConflictKey((byte[])param); + inst.push("SET_CONFLICT_KEY".getBytes()); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.DISABLE_WRITE_CONFLICT) { + inst.tr.options().setNextWriteNoWriteConflictRange(); + return ReadyFuture.DONE; + } + else if(op == StackOperation.GET) { + return inst.popParam().flatMap(new Function>() { + @Override + public Future apply(final Object param) { + Future f = inst.readTcx.readAsync(new Function>() { + @Override + public Future apply(ReadTransaction readTr) { + return inst.readTr.get((byte[])param); + } + }); + + inst.push(f); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.GET_RANGE) { + return inst.popParams(5).flatMap(new Function, Future>() { + @Override + public Future apply(final List params) { + final int limit = StackUtils.getInt(params.get(2)); + final boolean reverse = StackUtils.getBoolean(params.get(3)); + final StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(4), StreamingMode.ITERATOR.code())); + + Future> range = inst.readTcx.readAsync(new Function>>() { + @Override + public Future> apply(ReadTransaction readTr) { + return readTr.getRange((byte[])params.get(0), (byte[])params.get(1), limit, reverse, mode).asList(); + } + }); + + return pushRange(inst, range); + } + }); + } + else if(op == StackOperation.GET_RANGE_SELECTOR) { + return inst.popParams(10).flatMap(new Function, Future>() { + @Override + public Future apply(final List params) { + final int limit = StackUtils.getInt(params.get(6)); + final boolean reverse = StackUtils.getBoolean(params.get(7)); + final StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(8), StreamingMode.ITERATOR.code())); + + final KeySelector start = StackUtils.createSelector(params.get(0),params.get(1), params.get(2)); + final KeySelector end = StackUtils.createSelector(params.get(3), params.get(4), params.get(5)); + + Future> range = inst.readTcx.readAsync(new Function>>() { + @Override + public Future> apply(ReadTransaction readTr) { + return readTr.getRange(start, end, limit, reverse, mode).asList(); + } + }); + + return pushRange(inst, range, (byte[])params.get(9)); + } + }); + } + else if(op == StackOperation.GET_RANGE_STARTS_WITH) { + return inst.popParams(4).flatMap(new Function, Future>() { + @Override + public Future apply(final List params) { + final int limit = StackUtils.getInt(params.get(1)); + final boolean reverse = StackUtils.getBoolean(params.get(2)); + final StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(3), StreamingMode.ITERATOR.code())); + + Future> range = inst.readTcx.readAsync(new Function>>() { + @Override + public Future> apply(ReadTransaction readTr) { + return readTr.getRange(Range.startsWith((byte[])params.get(0)), limit, reverse, mode).asList(); + } + }); + + return pushRange(inst, range); + } + }); + } + else if(op == StackOperation.GET_KEY) { + return inst.popParams(4).flatMap(new Function, Future>() { + @Override + public Future apply(List params) { + final KeySelector start = StackUtils.createSelector(params.get(0),params.get(1), params.get(2)); + Future key = inst.readTcx.readAsync(new Function>() { + @Override + public Future apply(ReadTransaction readTr) { + return inst.readTr.getKey(start); + } + }); + + inst.push(executeGetKey(key, (byte[])params.get(3))); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.GET_READ_VERSION) { + return inst.readTr.getReadVersion().map(new Function() { + @Override + public Void apply(Long readVersion) { + inst.context.lastVersion = readVersion; + inst.push("GOT_READ_VERSION".getBytes()); + return null; + } + }); + } + else if(op == StackOperation.GET_COMMITTED_VERSION) { + try { + inst.context.lastVersion = inst.tr.getCommittedVersion(); + inst.push("GOT_COMMITTED_VERSION".getBytes()); + } + catch(FDBException e) { + StackUtils.pushError(inst, e); + } + + return ReadyFuture.DONE; + } + else if(op == StackOperation.GET_VERSIONSTAMP) { + try { + inst.push(inst.tr.getVersionstamp()); + } + catch(FDBException e) { + StackUtils.pushError(inst, e); + } + + return ReadyFuture.DONE; + } + else if(op == StackOperation.SET_READ_VERSION) { + if(inst.context.lastVersion == null) + throw new IllegalArgumentException("Read version has not been read"); + inst.tr.setReadVersion(inst.context.lastVersion); + return ReadyFuture.DONE; + } + else if(op == StackOperation.ON_ERROR) { + return inst.popParam().flatMap(new Function>() { + @Override + public Future apply(Object param) { + int errorCode = StackUtils.getInt(param); + + // 1102 (future_released) is not an error to Java. This is never encountered by user code, + // so we have to do something rather messy here to get compatibility with other languages. + // + // First, try on error with a retryable error. If it fails, then the transaction is in + // a failed state and we should rethrow the error. Otherwise, throw the original error. + boolean filteredError = errorCode == 1102; + + FDBException err = new FDBException("Fake testing error", filteredError ? 1020 : errorCode); + + Future f = inst.tr.onError(err) + .map(new Function() { + @Override + public Void apply(final Transaction tr) { + inst.setTransaction(tr); + return null; + } + }) + .rescueRuntime(new Function>() { + @Override + public Future apply(RuntimeException ex) { + inst.context.newTransaction(); + throw ex; + } + }); + + if(filteredError) { + f.get(); + throw new FDBException("Fake testing error", errorCode); + } + + inst.push(f); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.SUB) { + return inst.popParams(2).flatMap(new Function, Future>() { + @Override + public Future apply(List params) { + long result = StackUtils.getNumber(params.get(0)).longValue() - + StackUtils.getNumber(params.get(1)).longValue(); + inst.push(result); + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.CONCAT) { + return inst.popParams(2).flatMap(new Function, Future>() { + @Override + public Future apply(List params) { + if(params.get(0) instanceof String) { + inst.push((String)params.get(0) + (String)params.get(1)); + } + else { + inst.push(ByteArrayUtil.join((byte[])params.get(0), (byte[])params.get(1))); + } + + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.TUPLE_PACK) { + return inst.popParam().flatMap(new Function>() { + @Override + public Future apply(Object param) { + int tupleSize = StackUtils.getInt(param); + //System.out.println(inst.context.preStr + " - " + "Packing top " + tupleSize + " items from stack"); + return inst.popParams(tupleSize).flatMap(new Function, Future>() { + @Override + public Future apply(List elements) { + byte[] coded = Tuple.fromItems(elements).pack(); + //System.out.println(inst.context.preStr + " - " + " -> result '" + ByteArrayUtil.printable(coded) + "'"); + inst.push(coded); + return ReadyFuture.DONE; + } + }); + } + }); + } + else if(op == StackOperation.TUPLE_UNPACK) { + return inst.popParam().flatMap(new Function>() { + @Override + public Future apply(Object param) { + /*System.out.println(inst.context.preStr + " - " + "Unpacking tuple code: " + + ByteArrayUtil.printable((byte[]) param)); */ + Tuple t = Tuple.fromBytes((byte[])param); + for(Object o : t.getItems()) { + byte[] itemBytes = Tuple.from(o).pack(); + inst.push(itemBytes); + } + return ReadyFuture.DONE; + } + }); + } + else if(op == StackOperation.TUPLE_RANGE) { + return inst.popParam().flatMap(new Function>() { + @Override + public Future apply(Object param) { + int tupleSize = StackUtils.getInt(param); + //System.out.println(inst.context.preStr + " - " + "Tuple range with top " + tupleSize + " items from stack"); + return inst.popParams(tupleSize).flatMap(new Function, Future>() { + @Override + public Future apply(List elements) { + Range range = Tuple.fromItems(elements).range(); + inst.push(range.begin); + inst.push(range.end); + return ReadyFuture.DONE; + } + }); + } + }); + } + else if(op == StackOperation.UNIT_TESTS) { + inst.context.db.options().setLocationCacheSize(100001); + + return inst.context.db.runAsync(new Function>() { + @Override + public Future apply(Transaction tr) { + tr.options().setPrioritySystemImmediate(); + tr.options().setPriorityBatch(); + tr.options().setCausalReadRisky(); + tr.options().setCausalWriteRisky(); + tr.options().setReadYourWritesDisable(); + tr.options().setReadAheadDisable(); + tr.options().setReadSystemKeys(); + tr.options().setAccessSystemKeys(); + tr.options().setDurabilityDevNullIsWebScale(); + tr.options().setTimeout(60*1000); + tr.options().setRetryLimit(50); + tr.options().setMaxRetryDelay(100); + tr.options().setUsedDuringCommitProtectionDisable(); + tr.options().setTransactionLoggingEnable("my_transaction"); + + if(!(new FDBException("Fake", 1020)).isRetryable() || + (new FDBException("Fake", 10)).isRetryable()) + throw new RuntimeException("Unit test failed: Error predicate incorrect"); + + byte[] test = {(byte)0xff}; + return tr.get(test).map(new Function() { + @Override + public Void apply(byte[] val) { + return null; + } + }); + } + }).rescue(new Function>() { + @Override + public Future apply(Exception t) { + throw new RuntimeException("Unit tests failed: " + t.getMessage()); + } + }); + } + else if(op == StackOperation.LOG_STACK) { + return inst.popParam().flatMap(new Function>() { + @Override + public Future apply(Object param) { + final byte[] prefix = (byte[])param; + return doLogStack(inst, prefix); + + } + }); + } + + throw new IllegalArgumentException("Unrecognized (or unimplemented) operation"); + } + + private static Future executeMutation(final Instruction inst, Function> r) { + // run this with a retry loop + return inst.tcx.runAsync(r).map(new Function() { + @Override + public Void apply(Void a) { + if(inst.isDatabase) + inst.push("RESULT_NOT_PRESENT".getBytes()); + return null; + } + }); + } + + private static Future executeGetKey(final Future keyFuture, final byte[] prefixFilter) { + return keyFuture.map(new Function() { + @Override + public byte[] apply(byte[] key) { + if(ByteArrayUtil.startsWith(key, prefixFilter)) { + return key; + } + else if(ByteArrayUtil.compareUnsigned(key, prefixFilter) < 0) { + return prefixFilter; + } + else { + return ByteArrayUtil.strinc(prefixFilter); + } + } + }); + } + + private static Future doLogStack(final Instruction inst, final byte[] prefix) { + Map entries = new HashMap(); + while(inst.size() > 0) { + entries.put(inst.size() - 1, inst.pop()); + if(entries.size() == 100) { + return logStack(inst.context.db, entries, prefix).flatMap(new Function>() { + @Override + public Future apply(Void v) { + return doLogStack(inst, prefix); + } + }); + } + + } + + return logStack(inst.context.db, entries, prefix); + } + + private static Future logStack(final Database db, final Map entries, final byte[] prefix) { + return db.runAsync(new Function>() { + @Override + public Future apply(Transaction tr) { + for(Map.Entry it : entries.entrySet()) { + byte[] pk = ByteArrayUtil.join(prefix, Tuple.from(it.getKey(), it.getValue().idx).pack()); + byte[] pv = Tuple.from(StackUtils.serializeFuture(it.getValue().value)).pack(); + tr.set(pk, pv.length < 40000 ? pv : Arrays.copyOfRange(pv, 0, 40000)); + } + + return ReadyFuture.DONE; + } + }); + } + + private static Future pushRange(Instruction inst, Future> range) { + return pushRange(inst, range, null); + } + + private static Future pushRange(Instruction inst, Future> range, byte[] prefixFilter) { + //System.out.println("Waiting on range data to push..."); + return range.map(new ListPusher(inst, prefixFilter)); + } + + /** + * Pushes the result of a range query onto the stack as a {@code Tuple} + */ + private static class ListPusher implements Function, Void> { + final Instruction inst; + final byte[] prefixFilter; + + ListPusher(Instruction inst, byte[] prefixFilter) { + this.inst = inst; + this.prefixFilter = prefixFilter; + } + @Override + public Void apply(List list) { + List o = new LinkedList(); + for(KeyValue kv : list) { + if(prefixFilter == null || ByteArrayUtil.startsWith(kv.getKey(), prefixFilter)) { + o.add(kv.getKey()); + o.add(kv.getValue()); + } + } + //System.out.println("Added " + o.size() / 2 + " pairs to stack/tuple"); + inst.push(Tuple.fromItems(o).pack()); + return null; + } + } + + static class AsynchronousContext extends Context { + List operations = null; + int currentOp = 0; + + AsyncDirectoryExtension directoryExtension = new AsyncDirectoryExtension(); + + AsynchronousContext(Database db, byte[] prefix) { + super(db, prefix); + } + + @Override + Context createContext(byte[] prefix) { + return new AsynchronousContext(this.db, prefix); + } + + Future processOp(byte[] operation) { + Tuple tokens = Tuple.fromBytes(operation); + final Instruction inst = new Instruction(this, tokens); + + /*if(!inst.op.equals("PUSH") && !inst.op.equals("SWAP")) { + System.out.println(inst.context.preStr + "\t- " + Thread.currentThread().getName() + + "\t- OP (" + inst.context.instructionIndex + "):" + inst.op); + }*/ + + if(inst.op.startsWith(DIRECTORY_PREFIX)) + return directoryExtension.processInstruction(inst); + else { + return processInstruction(inst) + .rescueRuntime(new Function>() { + @Override + public Future apply(RuntimeException e) { + if(e instanceof FDBException) { + StackUtils.pushError(inst, (FDBException)e); + return ReadyFuture.DONE; + } + else if(e instanceof IllegalStateException && e.getMessage().equals("Future not ready")) { + StackUtils.pushError(inst, new FDBException("", 2015)); + return ReadyFuture.DONE; + } + else + return new ReadyFuture(e); + } + }); + } + } + + @Override void executeOperations() throws Throwable { + executeRemainingOperations().get(); + } + + Future executeRemainingOperations() { + final Function> processNext = new Function>() { + @Override + public Future apply(Void ignore) { + instructionIndex++; + return executeRemainingOperations(); + } + }; + + if(operations == null || ++currentOp == operations.size()) { + return db.runAsync(new Function>>() { + @Override + public Future> apply(Transaction tr) { + return tr.getRange(nextKey, endKey, 1000).asList(); + } + }) + .flatMap(new Function, Future>() { + @Override + public Future apply(List next) { + if(next.size() < 1) { + //System.out.println("No key found after: " + ByteArrayUtil.printable(nextKey.getKey())); + return ReadyFuture.DONE; + } + + operations = next; + currentOp = 0; + nextKey = KeySelector.firstGreaterThan(next.get(next.size()-1).getKey()); + + return processOp(next.get(0).getValue()).flatMap(processNext); + } + }); + } + + return processOp(operations.get(currentOp).getValue()).flatMap(processNext); + } + } + + static Future popAndWait(Stack stack) { + StackEntry entry = stack.pop(); + Object item = entry.value; + if(!(item instanceof Future)) { + return new ReadyFuture(entry); + } + final int idx = entry.idx; + + @SuppressWarnings("unchecked") + final Future future = (Future)item; + Future flattened = flatten(future); + + return flattened.map(new Function() { + @Override + public StackEntry apply(Object o) { + return new StackEntry(idx, o); + } + }); + } + + private static Future flatten(final Future future) { + return future.map(new Function() { + @Override + public Object apply(Object o) { + if(o == null) + return "RESULT_NOT_PRESENT".getBytes(); + return o; + } + }).rescue(new Function>() { + @Override + public Future apply(Exception t) { + if(t instanceof FDBException) { + return new ReadyFuture(StackUtils.getErrorBytes((FDBException)t)); + } + else if(t instanceof IllegalStateException && t.getMessage().equals("Future not ready")) { + return new ReadyFuture(StackUtils.getErrorBytes(new FDBException("", 2015))); + } + return new ReadyFuture(t); + } + }); + } + + + /** + * Run a stack-machine based test. + */ + public static void main(String[] args) { + if(args.length < 1) + throw new IllegalArgumentException("StackTester needs parameters "); + + //System.out.println("Prefix: " + args[0]); + + byte[] prefix = args[0].getBytes(); + + FDB fdb = FDB.selectAPIVersion(Integer.parseInt(args[1])); + //ExecutorService executor = Executors.newFixedThreadPool(2); + Cluster cl = fdb.createCluster(args.length > 2 ? args[2] : null); + + Database db = cl.openDatabase(); + + Context c = new AsynchronousContext(db, prefix); + //System.out.println("Starting test..."); + c.run(); + //System.out.println("Done with test."); + + /*byte[] key = Tuple.from("test_results".getBytes(), 5).pack(); + byte[] bs = db.createTransaction().get(key).get(); + System.out.println("output of " + ByteArrayUtil.printable(key) + " as: " + ByteArrayUtil.printable(bs));*/ + + /*fdb.stopNetwork(); + executor.shutdown();*/ + } + +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/BlockingBenchmark.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/BlockingBenchmark.java new file mode 100644 index 0000000000..b10685da67 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/BlockingBenchmark.java @@ -0,0 +1,135 @@ +/* + * BlockingBenchmark.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; + +public class BlockingBenchmark { + private static final int REPS = 100000; + private static final int PARALLEL = 100; + + public static void main(String[] args) throws InterruptedException { + FDB fdb = FDB.selectAPIVersion(500); + + // The cluster file DOES NOT need to be valid, although it must exist. + // This is because the database is never really contacted in this test. + Database database = fdb.open("T:\\circus\\tags\\RebarCluster-bbc\\cluster_id.txt"); + + byte[] key = {0x1, 0x1, 0x1, 0x1, 0x1}; + byte[] val = {0x2, 0x2, 0x2, 0x2, 0x2}; + + Transaction tr = database.createTransaction(); + tr.setReadVersion(100000); + final Function identity = new Function() { + @Override + public Long apply(Long o) { + return o; + } + }; + + + System.out.println("readVersion().blockUntilReady():"); + runTests(tr, new Function, Void>() { + @Override + public Void apply(Future o) { + o.blockUntilReady(); + return null; + } + }); + + System.out.println("readVersion().blockInterruptibly():"); + runTests(tr, new Function, Void>() { + @Override + public Void apply(Future o) { + try { + o.blockInterruptibly(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + }); + + System.out.println("readVersion().map(identity).blockUntilReady():"); + runTests(tr, new Function, Void>() { + @Override + public Void apply(Future o) { + o.map(identity).blockUntilReady(); + return null; + } + }); + + System.out.println("readVersion().map^10(identity).blockUntilReady():"); + runTests(tr, new Function, Void>() { + @Override + public Void apply(Future o) { + for(int i=0; i<10; i++) + o = o.map(identity); + o.blockUntilReady(); + return null; + } + }); + + System.out.println("readVersion().blockUntilReady^100():"); + runTests(tr, new Function, Void>() { + @Override + public Void apply(Future o) { + for(int i=0; i<100; i++) + o.blockUntilReady(); + return null; + } + }); + + } + + private static void runTests(Transaction tr, Function, Void> blockMethod) { + for(int r=0; r<4; r++) { + long start = System.currentTimeMillis(); + for(int i = 0; i < REPS; i++) { + blockMethod.apply( tr.getReadVersion() ); + } + + long taken = System.currentTimeMillis() - start; + System.out.println(" " + REPS + " done in " + taken + "ms -> " + ((taken * 1000.0) / REPS) + " us latency"); + + ArrayList> futures = new ArrayList>(PARALLEL); + for(int j=0; j " + (REPS / (taken)) + " KHz"); + } + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/ConcurrentGetSetGet.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/ConcurrentGetSetGet.java new file mode 100644 index 0000000000..288b2bd9b5 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/ConcurrentGetSetGet.java @@ -0,0 +1,166 @@ +/* + * ConcurrentGetSetGet.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.nio.charset.Charset; +import java.security.SecureRandom; +import java.util.Random; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.ReadyFuture; + +public class ConcurrentGetSetGet { + public static final Charset UTF8 = Charset.forName("UTF-8"); + + final Semaphore semaphore = new Semaphore(CONCURRENCY); + final AtomicInteger errorCount = new AtomicInteger(); + final AtomicInteger attemptCount = new AtomicInteger(); + final AtomicInteger getCompleteCount = new AtomicInteger(); + + // Total trials + public static final int COUNT = 1000; + + // How many to have outstanding at once + public static final int CONCURRENCY = 100; + + private static byte[] $(String s) { + return s.getBytes(UTF8); + } + + public static void main(String[] args) { + Database database = FDB.selectAPIVersion(500).open(); + new ConcurrentGetSetGet().apply(database); + } + + public void apply(Database d) { + new Thread(new Runnable() { + @Override + public void run() { + int loops = 0; + try { + Thread.sleep(5000); + System.out.println("Loop " + loops++ + ":"); + System.out.println(" attempts: " + attemptCount.get()); + System.out.println(" gets complete: " + getCompleteCount.get()); + System.out.println(" errors: " + errorCount.get()); + System.out.println(" sem: " + semaphore); + System.out.println(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + }).start(); + final Random random = new SecureRandom(); + try { + long start = System.currentTimeMillis(); + long current = start; + for (int i = 0; i < COUNT; i++) { + semaphore.acquire(); + long wait = System.currentTimeMillis() - current; + if (wait > 100) { + System.out.println("Waited " + wait + "ms"); + } + current = System.currentTimeMillis(); + d.runAsync(new Function>() { + @Override + public Future apply(final Transaction r) { + attemptCount.addAndGet(1); + final String key = "test:" + random.nextInt(); + return r.get($(key)).flatMap(new Function>() { + @Override + public Future apply(byte[] o) { + r.set($(key), $("value")); + return r.get($(key)).map(new Function() { + @Override + public Void apply(byte[] o) { + getCompleteCount.addAndGet(1); + semaphore.release(); + return null; + } + }); + } + }).rescue(new Function>() { + @Override + public Future apply(Exception o) { + errorCount.addAndGet(1); + System.err.println("Fail (" + o.getMessage() + ")"); + semaphore.release(); + return ReadyFuture.DONE; + } + }); + } +/* @Override + public void apply(byte[] value) { + getCompleteCount.addAndGet(1); + r.set($(key), $("value")); + r.get($(key)).onSuccess(new Block() { + @Override + public void apply(byte[] value) { + getCompleteCount.addAndGet(1); + semaphore.release(); + } + }).onFailure(new Block() { + @Override + public void apply(Throwable value) { + errorCount.addAndGet(1); + System.err.println("Inner fail (" + value.getMessage() + ")"); + semaphore.release(); + } + }); + } + }).onFailure(new Block() { + @Override + public void apply(Throwable value) { + errorCount.addAndGet(1); + System.err.println("Outer fail (" + value.getMessage() + ")"); + semaphore.release(); + } + }); + //return ReadyFuture.DONE; + } +*/ }); + } + semaphore.acquire(CONCURRENCY); + long diff = System.currentTimeMillis() - start; + System.out.println("time taken (ms): " + diff); + System.out.println("tr/sec:" + COUNT * 1000l / diff); + System.out.println("attempts: " + attemptCount.get()); + System.out.println("gets complete: " + getCompleteCount.get()); + System.out.println("errors: " + errorCount.get()); + System.out.println(); + // Can be enabled in Database.java + //System.out.println("db success: " + d.commitSuccessCount.get()); + //System.out.println("db errors: " + d.commitErrorCount.get()); + System.exit(0); + } catch (Throwable throwable) { + throwable.printStackTrace(); + System.exit(1); + } + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/Context.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/Context.java new file mode 100644 index 0000000000..602cc30720 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/Context.java @@ -0,0 +1,191 @@ +/* + * Context.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.KeySelector; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.StreamingMode; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.Settable; +import com.apple.cie.foundationdb.async.SettableFuture; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +abstract class Context implements Runnable { + final Stack stack = new Stack(); + final Database db; + final String preStr; + int instructionIndex = 0; + String trName; + KeySelector nextKey, endKey; + Long lastVersion = null; + List children = new LinkedList(); + + static Map transactionMap = new HashMap(); + + Context(Database db, byte[] prefix) { + this.db = db; + Range r = Tuple.from(prefix).range(); + this.nextKey = KeySelector.firstGreaterOrEqual(r.begin); + this.endKey = KeySelector.firstGreaterOrEqual(r.end); + + this.trName = ByteArrayUtil.printable(prefix); + this.preStr = ByteArrayUtil.printable(prefix); + + newTransaction(); + } + + @Override + public void run() { + try { + executeOperations(); + } catch(Throwable t) { + // EAT + t.printStackTrace(); + } + while(children.size() > 0) { + //System.out.println("Shutting down...waiting on " + children.size() + " threads"); + final Thread t = children.get(0); + while(t.isAlive()) { + try { + t.join(); + } catch (InterruptedException e) { + // EAT + } + } + children.remove(0); + } + } + + public Transaction getCurrentTransaction() { + synchronized(Context.transactionMap) { + return Context.transactionMap.get(this.trName); + } + } + + public void updateCurrentTransaction(Transaction tr) { + synchronized(Context.transactionMap) { + Context.transactionMap.put(this.trName, tr); + } + } + + public void newTransaction() { + synchronized(Context.transactionMap) { + Context.transactionMap.put(this.trName, db.createTransaction()); + } + } + + public void switchTransaction(byte[] trName) { + synchronized(Context.transactionMap) { + this.trName = ByteArrayUtil.printable(trName); + if(!Context.transactionMap.containsKey(this.trName)) { + newTransaction(); + } + } + } + + abstract void executeOperations() throws Throwable; + abstract Context createContext(byte[] prefix); + + void addContext(byte[] prefix) { + Thread t = new Thread(createContext(prefix)); + t.start(); + children.add(t); + } + + StreamingMode streamingModeFromCode(int code) { + for(StreamingMode x : StreamingMode.values()) { + if(x.code() == code) { + return x; + } + } + throw new IllegalArgumentException("Invalid code: " + code); + } + + void popParams(int num, final List params, final Settable done) { + while(num-- > 0) { + Object item = stack.pop().value; + if(item instanceof Future) { + @SuppressWarnings("unchecked") + final Future future = (Future)item; + final int nextNum = num; + future.onReady(new Runnable() { + @Override + public void run() { + try { + Object o = future.get(); + if(o == null) + params.add("RESULT_NOT_PRESENT".getBytes()); + else + params.add(o); + + popParams(nextNum, params, done); + } catch(FDBException e) { + params.add(StackUtils.getErrorBytes(e)); + popParams(nextNum, params, done); + } catch(IllegalStateException e) { + if(e.getMessage().equals("Future not ready")) { + params.add(StackUtils.getErrorBytes(new FDBException("", 2015))); + popParams(nextNum, params, done); + } + else { + done.setError(e); + } + } catch(RuntimeException e) { + done.setError(e); + } catch(Error e) { + done.setError(e); + throw e; + } + } + }); + + return; + } + else + params.add(item); + } + + done.set(null); + } + + Future> popParams(int num) { + final List params = new LinkedList(); + SettableFuture done = new SettableFuture(); + popParams(num, params, done); + + return done.map(new Function>() { + @Override + public List apply(Void n) { + return params; + } + }); + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/ContinuousSample.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/ContinuousSample.java new file mode 100644 index 0000000000..d1550b33ac --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/ContinuousSample.java @@ -0,0 +1,95 @@ +/* + * ContinuousSample.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +public class ContinuousSample > { + public ContinuousSample( int sampleSize ) { + this.sampleSize = sampleSize; + this.samples = new ArrayList(sampleSize); + this.populationSize = 0; + this.sorted = true; + } + + public ContinuousSample addSample(T sample) { + if(populationSize == 0) + _min = _max = sample; + populationSize++; + sorted = false; + + if( populationSize <= sampleSize ) { + samples.add( sample ); + } else if( random.nextDouble() < ( (double)sampleSize / populationSize ) ) { + samples.add(random.nextInt(sampleSize), sample); + } + + _max = sample.compareTo(_max) > 0 ? sample : _max; + _min = sample.compareTo(_min) < 0 ? sample : _min; + return this; + } + + public double mean() { + if (samples.size() == 0) return 0; + double sum = 0; + for( int c = 0; c < samples.size(); c++ ) { + sum += samples.get(c).doubleValue(); + } + return sum / samples.size(); + } + + public T median() { + return percentile( 0.5 ); + } + + public T percentile( double percentile ) { + if( samples.size() == 0 || percentile < 0.0 || percentile > 1.0 ) + return null; + sort(); + int idx = (int)Math.floor( ( samples.size() - 1 ) * percentile ); + return samples.get(idx); + } + + public T min() { return _min; } + public T max() { return _max; } + + @Override + public String toString() { + return String.format("Mean: %.2f, Median: %.2f, 90%%: %.2f, 98%%: %.2f", + mean(), median(), percentile(0.90), percentile(0.98)); + } + + private Random random = new Random(); + private int sampleSize; + private long populationSize; + private boolean sorted; + private List samples; + private T _min, _max; + + private void sort() { + if( !sorted && samples.size() > 1 ) + Collections.sort(samples); + sorted = true; + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryExtension.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryExtension.java new file mode 100644 index 0000000000..38fd448673 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryExtension.java @@ -0,0 +1,229 @@ +/* + * DirectoryExtension.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.directory.Directory; +import com.apple.cie.foundationdb.directory.DirectoryLayer; +import com.apple.cie.foundationdb.directory.DirectorySubspace; +import com.apple.cie.foundationdb.subspace.Subspace; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +class DirectoryExtension { + List dirList = new ArrayList(); + int dirIndex = 0; + int errorIndex = 0; + + public DirectoryExtension() { + dirList.add(DirectoryLayer.getDefault()); + } + + Directory directory() { + return (Directory)dirList.get(dirIndex); + } + + Subspace subspace() { + return (Subspace)dirList.get(dirIndex); + } + + void processInstruction(final Instruction inst) { + try { + //System.out.println("Processing operation " + inst.op + " (" + inst.context.instructionIndex + ")"); + DirectoryOperation op = DirectoryOperation.valueOf(inst.op); + if(op == DirectoryOperation.DIRECTORY_CREATE_SUBSPACE) { + Tuple prefix = DirectoryUtil.popTuple(inst).get(); + byte[] rawPrefix = (byte[])inst.popParam().get(); + dirList.add(new Subspace(prefix, rawPrefix)); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE_LAYER) { + List params = inst.popParams(3).get(); + Subspace nodeSubspace = (Subspace)dirList.get(StackUtils.getInt(params.get(0))); + Subspace contentSubspace = (Subspace)dirList.get(StackUtils.getInt(params.get(1))); + boolean allowManualPrefixes = StackUtils.getInt(params.get(2)) == 1; + + if(nodeSubspace == null || contentSubspace == null) + dirList.add(null); + else + dirList.add(new DirectoryLayer(nodeSubspace, contentSubspace, allowManualPrefixes)); + } + else if(op == DirectoryOperation.DIRECTORY_CHANGE) { + dirIndex = StackUtils.getInt(inst.popParam().get()); + if(dirList.get(dirIndex) == null) + dirIndex = errorIndex; + } + else if(op == DirectoryOperation.DIRECTORY_SET_ERROR_INDEX) { + errorIndex = StackUtils.getInt(inst.popParam().get()); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN || op == DirectoryOperation.DIRECTORY_OPEN) { + List path = DirectoryUtil.popPath(inst).get(); + byte[] layer = (byte[])inst.popParam().get(); + + Future dir; + if(layer == null) { + if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN) + dir = directory().createOrOpen(inst.tcx, path); + else + dir = directory().open(inst.readTcx, path); + } + else { + if(op == DirectoryOperation.DIRECTORY_CREATE_OR_OPEN) + dir = directory().createOrOpen(inst.tcx, path, layer); + else + dir = directory().open(inst.readTcx, path, layer); + } + + dirList.add(dir.get()); + } + else if(op == DirectoryOperation.DIRECTORY_CREATE) { + List path = DirectoryUtil.popPath(inst).get(); + List params = inst.popParams(2).get(); + + byte[] layer = (byte[])params.get(0); + byte[] prefix = (byte[])params.get(1); + + Future dir; + if(layer == null && prefix == null) + dir = directory().create(inst.tcx, path); + else if(prefix == null) + dir = directory().create(inst.tcx, path, layer); + else { + if(layer == null) + layer = new byte[0]; + + dir = directory().create(inst.tcx, path, layer, prefix); + } + + dirList.add(dir.get()); + } + else if(op == DirectoryOperation.DIRECTORY_MOVE) { + List> paths = DirectoryUtil.popPaths(inst, 2).get(); + DirectorySubspace dir = directory().move(inst.tcx, paths.get(0), paths.get(1)).get(); + dirList.add(dir); + } + else if(op == DirectoryOperation.DIRECTORY_MOVE_TO) { + List newAbsolutePath = DirectoryUtil.popPath(inst).get(); + DirectorySubspace dir = directory().moveTo(inst.tcx, newAbsolutePath).get(); + dirList.add(dir); + } + else if(op == DirectoryOperation.DIRECTORY_REMOVE) { + int count = StackUtils.getInt(inst.popParam().get()); + List> path = DirectoryUtil.popPaths(inst, count).get(); + if(path.size() == 0) + directory().remove(inst.tcx).get(); + else + directory().remove(inst.tcx, path.get(0)).get(); + } + else if(op == DirectoryOperation.DIRECTORY_REMOVE_IF_EXISTS) { + int count = StackUtils.getInt(inst.popParam().get()); + List> path = DirectoryUtil.popPaths(inst, count).get(); + if(path.size() == 0) + directory().removeIfExists(inst.tcx).get(); + else + directory().removeIfExists(inst.tcx, path.get(0)).get(); + } + else if(op == DirectoryOperation.DIRECTORY_LIST) { + int count = StackUtils.getInt(inst.popParam().get()); + List> path = DirectoryUtil.popPaths(inst, count).get(); + List children; + if(path.size() == 0) + children = directory().list(inst.readTcx).get(); + else + children = directory().list(inst.readTcx, path.get(0)).get(); + + inst.push(Tuple.fromItems(children).pack()); + } + else if(op == DirectoryOperation.DIRECTORY_EXISTS) { + int count = StackUtils.getInt(inst.popParam().get()); + List> path = DirectoryUtil.popPaths(inst, count).get(); + boolean exists; + if(path.size() == 0) + exists = directory().exists(inst.readTcx).get(); + else + exists = directory().exists(inst.readTcx, path.get(0)).get(); + + inst.push(exists ? 1 : 0); + } + else if(op == DirectoryOperation.DIRECTORY_PACK_KEY) { + Tuple keyTuple = DirectoryUtil.popTuple(inst).get(); + inst.push(subspace().pack(keyTuple)); + } + else if(op == DirectoryOperation.DIRECTORY_UNPACK_KEY) { + byte[] key = (byte[])inst.popParam().get(); + Tuple tup = subspace().unpack(key); + for(Object o : tup) + inst.push(o); + } + else if(op == DirectoryOperation.DIRECTORY_RANGE) { + Tuple tup = DirectoryUtil.popTuple(inst).get(); + Range range = subspace().range(tup); + inst.push(range.begin); + inst.push(range.end); + } + else if(op == DirectoryOperation.DIRECTORY_CONTAINS) { + byte[] key = (byte[])inst.popParam().get(); + inst.push(subspace().contains(key) ? 1 : 0); + } + else if(op == DirectoryOperation.DIRECTORY_OPEN_SUBSPACE) { + Tuple prefix = DirectoryUtil.popTuple(inst).get(); + dirList.add(subspace().subspace(prefix)); + } + else if(op == DirectoryOperation.DIRECTORY_LOG_SUBSPACE) { + final byte[] prefix = (byte[])inst.popParam().get(); + inst.tr.set(ByteArrayUtil.join(prefix, new Tuple().add(dirIndex).pack()), subspace().getKey()); + } + else if(op == DirectoryOperation.DIRECTORY_LOG_DIRECTORY) { + final byte[] prefix = (byte[])inst.popParam().get(); + boolean exists = directory().exists(inst.tr).get(); + List children = exists ? directory().list(inst.tr).get() : new ArrayList(); + Subspace logSubspace = new Subspace(new Tuple().add(dirIndex), prefix); + inst.tr.set(logSubspace.pack("path"), Tuple.fromItems(directory().getPath()).pack()); + inst.tr.set(logSubspace.pack("layer"), new Tuple().add(directory().getLayer()).pack()); + inst.tr.set(logSubspace.pack("exists"), new Tuple().add(exists ? 1 : 0).pack()); + inst.tr.set(logSubspace.pack("children"), Tuple.fromItems(children).pack()); + } + else if(op == DirectoryOperation.DIRECTORY_STRIP_PREFIX) { + byte[] str = (byte[])inst.popParam().get(); + byte[] rawPrefix = subspace().getKey(); + + if(str.length < rawPrefix.length) + throw new RuntimeException("String does not start with raw prefix"); + + for(int i = 0; i < rawPrefix.length; ++i) + if(str[i] != rawPrefix[i]) + throw new RuntimeException("String does not start with raw prefix"); + + inst.push(Arrays.copyOfRange(str, rawPrefix.length, str.length)); + } + else { + throw new RuntimeException("Unknown operation:" + inst.op); + } + } + catch(Throwable t) { + DirectoryUtil.pushError(inst, t, dirList); + } + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryOperation.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryOperation.java new file mode 100644 index 0000000000..70b28051a3 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryOperation.java @@ -0,0 +1,57 @@ +/* + * DirectoryOperation.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +enum DirectoryOperation { + DIRECTORY_CREATE_SUBSPACE(true), + DIRECTORY_CREATE_LAYER(true), + DIRECTORY_CHANGE, + DIRECTORY_SET_ERROR_INDEX, + DIRECTORY_CREATE_OR_OPEN(true), + DIRECTORY_CREATE(true), + DIRECTORY_OPEN(true), + DIRECTORY_MOVE(true), + DIRECTORY_MOVE_TO(true), + DIRECTORY_REMOVE, + DIRECTORY_REMOVE_IF_EXISTS, + DIRECTORY_LIST, + DIRECTORY_EXISTS, + DIRECTORY_CHECK_LAYER, + DIRECTORY_PACK_KEY, + DIRECTORY_UNPACK_KEY, + DIRECTORY_RANGE, + DIRECTORY_CONTAINS, + DIRECTORY_OPEN_SUBSPACE(true), + DIRECTORY_LOG_SUBSPACE, + DIRECTORY_LOG_DIRECTORY, + DIRECTORY_STRIP_PREFIX; + + boolean createsDirectory; + + DirectoryOperation() { + this(false); + } + + DirectoryOperation(boolean createsDirectory) { + this.createsDirectory = createsDirectory; + } +} + diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryTest.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryTest.java new file mode 100644 index 0000000000..3fcbaf4598 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryTest.java @@ -0,0 +1,88 @@ +/* + * DirectoryTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.async.PartialFunction; +import com.apple.cie.foundationdb.directory.DirectoryLayer; +import com.apple.cie.foundationdb.directory.DirectorySubspace; + +public class DirectoryTest { + private static final String CLUSTER_FILE = "/home/ajb/fdb.cluster"; + + public static void main(String[] args) throws Exception { + try { + Cluster c = FDB.selectAPIVersion(500).createCluster(CLUSTER_FILE); + Database db = c.openDatabase(); + runTests(db); + } catch(Throwable t) { + t.printStackTrace(); + } + } + + private static void runTests(TransactionContext db) throws Exception { + System.out.println("Running tests..."); + final DirectoryLayer dir = new DirectoryLayer(); + + try { + db.run(new PartialFunction() { + @Override + public Void apply(Transaction tr) throws Exception { + List path = new ArrayList(); + path.add("foo"); + DirectorySubspace foo = dir.create(tr, path).get();//, "partition".getBytes("UTF-8")).get(); + System.out.println(foo.getPath()); + path.add("bar"); + DirectorySubspace bar = dir.create(tr, path).get();//, "partition".getBytes("UTF-8")).get(); + System.out.println(foo.getPath()); + path.add("baz"); + DirectorySubspace baz = dir.create(tr, path).get(); + System.out.println(foo.getPath()); + System.out.println("Created foo: " + foo.exists(tr).get()); + System.out.println("Created bar: " + bar.exists(tr).get()); + System.out.println("Created baz: " + baz.exists(tr).get()); + + DirectorySubspace bat = baz.moveTo(tr, Arrays.asList("foo", "bar", "bat")).get(); + + System.out.println("Moved baz to bat: " + bat.exists(tr).get()); + + foo.removeIfExists(tr).get(); + + System.out.println("Removed foo: " + foo.exists(tr).get()); + + return null; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + + System.exit(0); + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryUtil.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryUtil.java new file mode 100644 index 0000000000..f12c7df334 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/DirectoryUtil.java @@ -0,0 +1,126 @@ +/* + * DirectoryUtil.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.ArrayList; +import java.util.List; + +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.ReadyFuture; +import com.apple.cie.foundationdb.tuple.Tuple; + +class DirectoryUtil { + private static class TuplePopper { + private Instruction inst; + private int num; + private List tuples = new ArrayList(); + + TuplePopper(Instruction inst, int num) { + this.inst = inst; + this.num = num; + } + + Future> pop() { + return AsyncUtil.whileTrue(new Function>() { + @Override + public Future apply(Void ignore) { + if(num-- == 0) { + return new ReadyFuture(false); + } + return inst.popParam() + .flatMap(new Function>>() { + @Override + public Future> apply(Object count) { + return inst.popParams(StackUtils.getInt(count)); + } + }) + .map(new Function, Boolean>() { + @Override + public Boolean apply(List elements) { + tuples.add(Tuple.fromItems(elements)); + return num > 0; + } + }); + } + }) + .map(new Function>() { + @Override + public List apply(Void ignore) { + return tuples; + } + }); + } + } + + static Future> popTuples(Instruction inst, int num) { + return new TuplePopper(inst, num).pop(); + } + + static Future popTuple(Instruction inst) { + return popTuples(inst, 1) + .map(new Function, Tuple>() { + @Override + public Tuple apply(List tuples) { + return tuples.get(0); + } + }); + } + + static Future>> popPaths(Instruction inst, int num) { + return popTuples(inst, num) + .map(new Function, List>>() { + @Override + public List> apply(List tuples) { + List> paths = new ArrayList>(); + for(Tuple t : tuples) { + List path = new ArrayList(); + for(int i = 0; i < t.size(); ++i) + path.add(t.getString(i)); + + paths.add(path); + } + + return paths; + } + }); + } + + static Future> popPath(Instruction inst) { + return popPaths(inst, 1) + .map(new Function>, List>() { + @Override + public List apply(List> paths) { + return paths.get(0); + } + }); + } + + static void pushError(Instruction inst, Throwable t, List dirList) { + //System.err.println(t.getMessage()); + //t.printStackTrace(); + inst.push("DIRECTORY_ERROR".getBytes()); + DirectoryOperation op = DirectoryOperation.valueOf(inst.op); + if(op.createsDirectory) + dirList.add(null); + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/Example.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/Example.java new file mode 100644 index 0000000000..a5d9d3d678 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/Example.java @@ -0,0 +1,52 @@ +/* + * Example.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.*; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.tuple.Tuple; + +public class Example { + public static void main(String[] args) { + FDB fdb = FDB.selectAPIVersion(500); + Database db = fdb.open(); + + // Run an operation on the database + db.run(new Function() { + public Void apply(Transaction tr) { + tr.set(Tuple.from("hello").pack(), Tuple.from("world").pack()); + return null; + } + }); + + // Get the value of 'hello' from the database + String hello = db.run(new Function() { + public String apply(Transaction tr) { + byte[] result = tr.get(Tuple.from("hello").pack()).get(); + return Tuple.fromBytes(result).getString(0); + } + }); + System.out.println("Hello " + hello); + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/Instruction.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/Instruction.java new file mode 100644 index 0000000000..a23c9e8fba --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/Instruction.java @@ -0,0 +1,128 @@ +/* + * Instruction.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import com.apple.cie.foundationdb.ReadTransaction; +import com.apple.cie.foundationdb.ReadTransactionContext; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.tuple.Tuple; + +import java.util.List; + +class Instruction extends Stack { + private final static String SUFFIX_SNAPSHOT = "_SNAPSHOT"; + private final static String SUFFIX_DATABASE = "_DATABASE"; + + String op; + Tuple tokens; + Context context; + boolean isDatabase; + boolean isSnapshot; + Transaction tr; + ReadTransaction readTr; + TransactionContext tcx; + ReadTransactionContext readTcx; + + public Instruction(Context context, Tuple tokens) { + this.context = context; + this.tokens = tokens; + + op = tokens.getString(0); + isDatabase = op.endsWith(SUFFIX_DATABASE); + isSnapshot = op.endsWith(SUFFIX_SNAPSHOT); + + if(isDatabase) { + this.tr = context.db.createTransaction(); + readTr = this.tr; + op = op.substring(0, op.length() - SUFFIX_DATABASE.length()); + } + else if(isSnapshot) { + this.tr = context.getCurrentTransaction(); + readTr = this.tr.snapshot(); + op = op.substring(0, op.length() - SUFFIX_SNAPSHOT.length()); + } + else { + this.tr = context.getCurrentTransaction(); + readTr = this.tr; + } + + tcx = isDatabase ? context.db : this.tr; + readTcx = isDatabase ? context.db : this.readTr; + } + + void setTransaction(Transaction tr) { + this.tr = tr; + if(isSnapshot) { + readTr = this.tr.snapshot(); + } + else { + readTr = tr; + } + + if(!isDatabase) { + context.updateCurrentTransaction(tr); + } + } + + void push(Object o) { + context.stack.push(context.instructionIndex, o); + } + + void push(int idx, Object o) { + context.stack.push(idx, o); + } + + void push(StackEntry e) { + context.stack.push(e); + } + + StackEntry pop() { + return context.stack.pop(); + } + + void swap(int index) { + context.stack.swap(index); + } + + int size() { + return context.stack.size(); + } + + void clear() { + context.stack.clear(); + } + + Future> popParams(int num) { + return context.popParams(num); + } + + Future popParam() { + return popParams(1) + .map(new Function, Object>() { + public Object apply(List params) { + return params.get(0); + } + }); + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/IterableTest.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/IterableTest.java new file mode 100644 index 0000000000..e3662a2637 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/IterableTest.java @@ -0,0 +1,74 @@ +/* + * IterableTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.concurrent.atomic.AtomicInteger; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.async.Function; + +public class IterableTest { + private static final String CLUSTER_FILE = "C:\\Users\\Ben\\workspace\\fdb\\fdb.cluster"; + + public static void main(String[] args) throws InterruptedException { + final int reps = 1000; + try { + Cluster cluster = FDB.selectAPIVersion(500).createCluster(CLUSTER_FILE); + Database db = cluster.openDatabase(); + runTests(reps, db); + } catch(Throwable t) { + t.printStackTrace(); + } + } + + private static void runTests(final int reps, TransactionContext db) { + System.out.println("Running tests..."); + long start = System.currentTimeMillis(); + final AtomicInteger lastcount = new AtomicInteger(0); + try { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + for(KeyValue e : tr.getRange("vcount".getBytes(), "zz".getBytes())) { + System.out.println("K: " + new String(e.getKey()) + ", V: " + new String(e.getValue())); + } + return null; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + long end = System.currentTimeMillis(); + + double seconds = (end - start) / 1000.0; + System.out.println(" Transactions: " + reps); + System.out.println(" Total Time: " + seconds); + System.out.println(" Gets+Sets / sec: " + reps / seconds); + System.out.println(" Count: " + lastcount.get()); + + System.exit(0); + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/LocalityTests.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/LocalityTests.java new file mode 100644 index 0000000000..6a516ee4bc --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/LocalityTests.java @@ -0,0 +1,59 @@ +/* + * LocalityTests.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.List; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.LocalityUtil; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +public class LocalityTests { + + public static void main(String[] args) { + FDB fdb = FDB.selectAPIVersion(500); + Database database = fdb.open(args[0]); + + { + Transaction tr = database.createTransaction(); + String[] keyAddresses = LocalityUtil.getAddressesForKey(tr, "a".getBytes()).get(); + for(String s : keyAddresses) { + System.out.println(" @ " + s); + } + } + + long start = System.currentTimeMillis(); + AsyncIterable keys = LocalityUtil.getBoundaryKeys(database, new byte[0], new byte[] { (byte)255 } ); + Future> collection = AsyncUtil.collect(keys); + List list = collection.get(); + System.out.println("Took " + (System.currentTimeMillis() - start) + "ms to get " + + list.size() + " items"); + int i = 0; + for(byte[] key : collection.get()) { + System.out.println(i++ + ": " + ByteArrayUtil.printable(key)); + } + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/OSTest.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/OSTest.java new file mode 100644 index 0000000000..36577119dc --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/OSTest.java @@ -0,0 +1,38 @@ +/* + * OSTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.io.InputStream; + +public class OSTest { + + /** + * @param args + */ + public static void main(String[] args) { + System.out.println("OS name: " + System.getProperty("os.name")); + System.out.println("OS arch: " + System.getProperty("os.arch")); + + InputStream stream = OSTest.class.getResourceAsStream("/lib/linux/amd64/libfdb_java.so"); + System.out.println("Stream: " + stream); + } + +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/ParallelRandomScan.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/ParallelRandomScan.java new file mode 100644 index 0000000000..a07a136a75 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/ParallelRandomScan.java @@ -0,0 +1,136 @@ +/* + * ParallelRandomScan.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.nio.ByteBuffer; +import java.util.Random; +import java.util.concurrent.Semaphore; +import java.util.concurrent.atomic.AtomicInteger; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.StreamingMode; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.AsyncIterator; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; + +public class ParallelRandomScan { + private static final int ROWS = 1000000; + private static final int DURATION_MS = 2000; + private static final int PARALLELISM_MIN = 10; + private static final int PARALLELISM_MAX = 100; + private static final int PARALLELISM_STEP = 5; + + public static void main(String[] args) throws InterruptedException { + FDB api = FDB.selectAPIVersion(500); + Database database = api.open(args[0]); + + for(int i = PARALLELISM_MIN; i <= PARALLELISM_MAX; i += PARALLELISM_STEP) { + runTest(database, i, ROWS, DURATION_MS); + Thread.sleep(1000); + } + } + + private static void runTest(Database database, + int parallelism, int rows, int duration) throws InterruptedException { + final Random r = new Random(); + final AtomicInteger readsCompleted = new AtomicInteger(0); + final AtomicInteger errors = new AtomicInteger(0); + final Transaction tr = database.createTransaction(); + final Semaphore coordinator = new Semaphore(parallelism); + final ContinuousSample latencies = new ContinuousSample(1000); + + tr.options().setReadYourWritesDisable(); + + // Clearing the whole database before starting means all reads are local + /*ByteBuffer buf = ByteBuffer.allocate(4); + buf.putInt(0, Integer.MAX_VALUE); + tr.clear(new byte[0], buf.array());*/ + + // We use this for the key generation + ByteBuffer buf = ByteBuffer.allocate(4); + + // Eat the cost of the read version up-front + tr.getReadVersion().get(); + + final long start = System.currentTimeMillis(); + while(true) { + coordinator.acquire(); + if(System.currentTimeMillis() - start > duration) { + coordinator.release(); + break; + } + + int row = r.nextInt(rows - 1); + buf.putInt(0, row); + AsyncIterable range = tr.getRange( + buf.array(), ByteArrayUtil.strinc(buf.array()), 1, false, StreamingMode.SMALL); + + final long launch = System.nanoTime(); + + final AsyncIterator it = range.iterator(); + final Future f = it.onHasNext().map( + new Function() { + @Override + public KeyValue apply(Boolean o) { + if(!o) { + return null; + } + return it.next(); + } + } + ); + f.onReady(new Runnable() { + @Override + public void run() { + try { + @SuppressWarnings("unused") + KeyValue kv = f.get(); + readsCompleted.incrementAndGet(); + long timeTaken = System.nanoTime() - launch; + synchronized(latencies) { + latencies.addSample(timeTaken); + } + } catch(Throwable t) { + errors.incrementAndGet(); + } finally { + coordinator.release(); + } + } + }); + } + + // Block for ALL tasks to end! + coordinator.acquire(parallelism); + long end = System.currentTimeMillis(); + + double rowsPerSecond = readsCompleted.get() / ((end - start) / 1000.0); + System.out.println(parallelism + " ->\t" + rowsPerSecond); + System.out.println(String.format(" Reads: %d, errors: %d, time: %dms", + readsCompleted.get(), errors.get(), (int)(end - start))); + System.out.println(String.format(" Mean: %.2f, Median: %d, 98%%: %d", + latencies.mean(), latencies.median(), latencies.percentile(0.98))); + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/RangeTest.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/RangeTest.java new file mode 100644 index 0000000000..661ca941c1 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/RangeTest.java @@ -0,0 +1,187 @@ +/* + * RangeTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.List; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.KeySelector; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.Function; + +public class RangeTest { + private static final int API_VERSION = 500; + + public static void main(String[] args) throws Exception { + System.out.println("About to use version " + API_VERSION); + FDB fdb = FDB.selectAPIVersion(API_VERSION); + + /* + final String CLUSTER_FILE = "T:\\Ben\\cluster"; + String clusterFile = CLUSTER_FILE; + if(args.length > 0) { + clusterFile = args[0]; + } + + System.out.println("Using cluster file: " + clusterFile); + Cluster cluster = fdb.createCluster(clusterFile).get(); + Database db = cluster.openDatabase().get(); + */ + + Database db = fdb.open(); + + try { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + long version = tr.getReadVersion().get(); + System.out.println("DB version: " + version); + tr.get("apple1".getBytes()).get(); + tr.set("apple1".getBytes(), "crunchy1".getBytes()); + tr.set("apple2".getBytes(), "crunchy2".getBytes()); + tr.set("apple3".getBytes(), "crunchy3".getBytes()); + tr.set("apple4".getBytes(), "crunchy4".getBytes()); + tr.set("apple5".getBytes(), "crunchy5".getBytes()); + tr.set("apple6".getBytes(), "crunchy6".getBytes()); + System.out.println("Attempting to commit apple/crunchy pairs..."); + return null; + } + }); + } catch (Throwable e){ + e.printStackTrace(); + System.out.println("Non retryable exception caught..."); + } + + System.out.println("First transaction was successful"); + + checkRange(db.createTransaction()); + + Transaction tr = db.createTransaction(); + long version = tr.getReadVersion().get(); + System.out.println("DB version: " + version); + + byte[] bs = tr.get("apple3".getBytes()).get(); + System.out.println("Got apple3: " + new String(bs)); + + tr.cancel(); + try { + tr.get("apple3".getBytes()).get(); + throw new RuntimeException("The get() should have thrown an error!"); + } catch(FDBException e) { + if(e.getCode() != 1025) { + System.err.println("Transaction was not cancelled correctly (" + e.getCode() + ")"); + throw e; + } + System.out.println("Transaction was cancelled correctly"); + } + + tr = tr.reset(); + version = tr.getReadVersion().get(); + System.out.println("DB version: " + version); + + tr.clear("apple3".getBytes(), "apple6".getBytes()); + try { + tr.commit().get(); + System.out.println("Clear range transaction was successful"); + } catch(FDBException e) { + System.err.println("Error in the clear of a single value"); + e.printStackTrace(); + return; + } + //db.dispose(); + //cluster.dispose(); + + tr = db.createTransaction(); + checkRange(tr); + + Range r1 = new Range("apple".getBytes(), "banana".getBytes()); + Range r2 = new Range("apple".getBytes(), "banana".getBytes()); + Range r3 = new Range("apple".getBytes(), "crepe".getBytes()); + Range r4 = new Range(null, "banana".getBytes()); + Range r5 = new Range(new byte[]{0x15, 0x01}, null); + + System.out.println("ranges: " + r1 + ", " + r2 + ", " + r3 + ", " + r4 + ", " + r5); + + if(r1.equals(null)) { + System.err.println("range " + r1 + " equals null"); + } else if(!r1.equals(r1)) { + System.err.println("range equality not reflexive"); + } else if(r1.hashCode() != r1.hashCode()) { + System.err.println("range hashcode not reflexive"); + } else if(!r1.equals(r2)) { + System.err.println("range " + r1 + " and " + r2 + " not equal"); + } else if(r1.hashCode() != r2.hashCode()) { + System.err.println("ranges " + r1 + " and " + r2 + " do not have same hash codes"); + } else if(r1.equals(r3)) { + System.err.println("ranges " + r1 + " and " + r3 + " are equal"); + } else if (r1.hashCode() == r3.hashCode()) { + System.err.println("range " + r1 + " and " + r3 + " have same hash code"); + } else if(r1.equals(r4)) { + System.err.println("ranges " + r1 + " and " + r4 + " are equal"); + } else if(r1.hashCode() == r4.hashCode()) { + System.err.println("range " + r1 + " and " + r4 + " have same hash code"); + } else if(r1.equals(r5)) { + System.err.println("ranges " + r1 + " and " + r5 + " are equal"); + } else if(r1.hashCode() == r5.hashCode()) { + System.err.println("range " + r1 + " and " + r5 + " have same hash code"); + } else { + System.out.println("range comparisons okay"); + } + + db.dispose(); + //cluster.dispose(); + //fdb.stopNetwork(); + System.out.println("Done with test program"); + } + + private static void checkRange(Transaction tr) throws FDBException { + long version = tr.getReadVersion().get(); + System.out.println("DB version: " + version); + byte[] val = tr.get("apple4".getBytes()).get(); + System.out.println("Value is " + + (val != null ? new String(val) : "not present")); + + AsyncIterable entryList = tr.getRange( + KeySelector.firstGreaterOrEqual("apple".getBytes()), + KeySelector.firstGreaterOrEqual("banana".getBytes()),4); + List entries = entryList.asList().get(); + + System.out.println("List size is " + entries.size()); + for(int i=0; i < entries.size(); i++) { + String key = new String(entries.get(i).getKey()); + String value = new String(entries.get(i).getValue()); + System.out.println(" (" + i + ") -> " + key + ", " + value); + } + + System.out.println("\nAlso:"); + for(KeyValue kv : entryList) { + String key = new String(kv.getKey()); + String value = new String(kv.getValue()); + System.out.println(" -- " + key + " -> " + value); + } + + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/SerialInsertion.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/SerialInsertion.java new file mode 100644 index 0000000000..3533a2c90f --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/SerialInsertion.java @@ -0,0 +1,97 @@ +/* + * SerialInsertion.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; + +public class SerialInsertion { + private static final int THREAD_COUNT = 10; + private static final int BATCH_SIZE = 1000; + private static final int NODES = 1000000; + + public static void main(String[] args) { + FDB api = FDB.selectAPIVersion(500); + Database database = api.open("T:\\circus\\tags\\RebarCluster-bbc\\cluster_id.txt"); + long start = System.currentTimeMillis(); + + List threads = new ArrayList(THREAD_COUNT); + int nodesPerThread = NODES / THREAD_COUNT; + for(int i = 0; i < THREAD_COUNT; i++) { + // deal with non even division by adding remainder onto last thread's work + if(i == THREAD_COUNT - 1) { + nodesPerThread += (NODES % THREAD_COUNT); + } + InsertionThread t = new InsertionThread(database, nodesPerThread * i, nodesPerThread); + t.start(); + threads.add(t); + } + for(InsertionThread t : threads) { + try { + t.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + System.out.println("Time taken: " + (System.currentTimeMillis() - start) + "ms"); + } + + static class InsertionThread extends Thread { + private final Database db; + private final int insertionStart; + private final int insertionCount; + + InsertionThread(Database db, int insertionStart, int insertionCount) { + this.db = db; + this.insertionStart = insertionStart; + this.insertionCount = insertionCount; + } + + @Override + public void run() { + byte[] value = new byte[] { '.', '.', '.', '.' }; + int done = 0; + ByteBuffer buf = ByteBuffer.allocate(4); + Transaction tr = db.createTransaction(); + while(done < insertionCount) { + try { + int i = 0; + for(; i < BATCH_SIZE && done + i < insertionCount; i++) { + buf.putInt(0, insertionStart + done + i); + tr.set(buf.array(), value); + } + tr.commit().get(); + tr = tr.reset(); + done += i; + } catch(RuntimeException e) { + tr = tr.onError(e).get(); + } + } + } + } + +} \ No newline at end of file diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/SerialIteration.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/SerialIteration.java new file mode 100644 index 0000000000..fac1c93ca7 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/SerialIteration.java @@ -0,0 +1,136 @@ +/* + * SerialIteration.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.ReadTransaction; +import com.apple.cie.foundationdb.StreamingMode; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; + +public class SerialIteration { + private static final int ROWS = 1000000; + private static final int RUNS = 25; + private static final int THREAD_COUNT = 1; + + public static void main(String[] args) throws InterruptedException { + FDB api = FDB.selectAPIVersion(500); + Database database = api.open(args[0]); + + for(int i = 1; i <= THREAD_COUNT; i++) { + runThreadedTest(database, i); + Thread.sleep(1000); + } + } + + private static double runThreadedTest(Database database, int threadCount) { + List threads = new ArrayList(threadCount); + for(int i = 0; i < threadCount; i++) { + IterationThread thread = new IterationThread(database); + thread.start(); + threads.add(thread); + } + + double rowsPerSecond = 0; + for(IterationThread t : threads) { + try { + t.join(); + rowsPerSecond += t.getAverageRowsPerSecond(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + System.out.println("\n===================="); + System.out.println("Global rows/sec (over " + threadCount + " threads): " + rowsPerSecond); + return rowsPerSecond; + } + + private static class IterationThread extends Thread { + private static final Random r = new Random(); + private final Database db; + private double averageRowsPerSecond; + + IterationThread(Database database) { + this.db = database; + } + + double getAverageRowsPerSecond() { + return this.averageRowsPerSecond; + } + + @Override + public void run() { + try { + Thread.sleep(r.nextInt(1000)); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + long totalTime = 0; + int totalRows = 0; + for(int i = 0; i < RUNS; i++) { + long start = System.currentTimeMillis(); + int rowsRead = scanDatabase(db, ROWS); + long taken = System.currentTimeMillis() - start; + //System.out.println(i + ": time taken for " + ROWS + " rows: " + taken + "ms"); + System.out.print("."); + System.out.flush(); + if(i != 0) { + totalRows += rowsRead; + totalTime += taken; + } + } + averageRowsPerSecond = (totalRows / (double)totalTime) * 1000; + //System.out.println("Average rows/s: " + averageRowsPerSecond); + System.out.print("+"); + System.out.flush(); + } + } + + private static int scanDatabase(Database database, int rows) { + Transaction tr = database.createTransaction(); + tr.options().setReadYourWritesDisable(); + + ByteBuffer buf = ByteBuffer.allocate(4); + buf.putInt(0, Integer.MAX_VALUE); + AsyncIterable range = tr.getRange(new byte[0], buf.array(), + ReadTransaction.ROW_LIMIT_UNLIMITED, false, StreamingMode.WANT_ALL); + + int counter = 0; + try { + for(@SuppressWarnings("unused") KeyValue keys : range) { + counter++; + } + } catch(Exception e) { + e.printStackTrace(); + } + return counter; + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/SerialTest.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/SerialTest.java new file mode 100644 index 0000000000..99b14b90a4 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/SerialTest.java @@ -0,0 +1,85 @@ +/* + * SerialTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.concurrent.atomic.AtomicInteger; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.async.Function; + +public class SerialTest { + + private static final String CLUSTER_FILE = "T:\\Ben\\cluster"; + + public static void main(String[] args) throws InterruptedException { + final int reps = 1000; + try { + Cluster c = FDB.selectAPIVersion(500).createCluster(CLUSTER_FILE); + Database db = c.openDatabase(); + runTests(reps, db); + + /*Cluster fCluster = Cluster.create("C:\\Users\\Ben\\workspace\\fdb\\fdb.cluster").get(); + System.out.println("I now have the cluster"); + Database db = cluster.openDatabase().get(); + + runTests(reps, db);*/ + } catch(Throwable t) { + t.printStackTrace(); + } + } + + private static void runTests(int reps, TransactionContext db) { + System.out.println("Running tests..."); + long start = System.currentTimeMillis(); + final AtomicInteger lastcount = new AtomicInteger(0); + for(int i = 0; i < reps; i++) { + try { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + byte[] val = tr.get("count".getBytes()).get(); + //System.out.println("Got value"); + int count = Integer.parseInt(new String(val)); + tr.set("count".getBytes(), Integer.toString(count + 1).getBytes()); + lastcount.set(count); + return null; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + } + long end = System.currentTimeMillis(); + + double seconds = (end - start) / 1000.0; + System.out.println(" Transactions: " + reps); + System.out.println(" Total Time: " + seconds); + System.out.println(" Gets+Sets / sec: " + reps / seconds); + System.out.println(" Count: " + lastcount.get()); + + System.exit(0); + } + +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/Stack.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/Stack.java new file mode 100644 index 0000000000..ee3bfe0521 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/Stack.java @@ -0,0 +1,57 @@ +/* + * Stack.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +class Stack { + java.util.Stack stack = new java.util.Stack(); + + void push(int idx, Object o) { + stack.push(new StackEntry(idx, o)); + } + + void push(StackEntry e) { + stack.push(e); + } + + StackEntry pop() { + return stack.pop(); + } + + void swap(int index) { + if(index >= stack.size()) + throw new IllegalArgumentException("Stack index not valid"); + + if(index > 0) { + int fromBack = stack.size() - index - 1; + StackEntry tmp = stack.elementAt(fromBack); + stack.set(fromBack, stack.pop()); + stack.push(tmp); + } + } + + int size() { + return stack.size(); + } + + void clear() { + stack.clear(); + } +} \ No newline at end of file diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/StackEntry.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/StackEntry.java new file mode 100644 index 0000000000..1b6ff36634 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/StackEntry.java @@ -0,0 +1,30 @@ +/* + * StackEntry.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +class StackEntry { + int idx; + Object value; + public StackEntry(int idx, Object value) { + this.idx = idx; + this.value = value; + } +} \ No newline at end of file diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/StackOperation.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/StackOperation.java new file mode 100644 index 0000000000..10c8c5c253 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/StackOperation.java @@ -0,0 +1,68 @@ +/* + * StackOperation.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +enum StackOperation { + PUSH, + POP, + DUP, + EMPTY_STACK, + SWAP, + WAIT_EMPTY, + START_THREAD, + WAIT_FUTURE, + NEW_TRANSACTION, + USE_TRANSACTION, + SET, + CLEAR, + CLEAR_RANGE, + CLEAR_RANGE_STARTS_WITH, + ATOMIC_OP, + + // explicit conflict ranges... + READ_CONFLICT_RANGE, + WRITE_CONFLICT_RANGE, + READ_CONFLICT_KEY, + WRITE_CONFLICT_KEY, + DISABLE_WRITE_CONFLICT, + + COMMIT, + RESET, + CANCEL, + GET, + GET_RANGE, + GET_RANGE_SELECTOR, + GET_RANGE_STARTS_WITH, + GET_KEY, + GET_READ_VERSION, + GET_COMMITTED_VERSION, + GET_VERSIONSTAMP, + SET_READ_VERSION, + ON_ERROR, + SUB, + CONCAT, + TUPLE_PACK, + TUPLE_UNPACK, + TUPLE_RANGE, + UNIT_TESTS, /* Possibly unimplemented */ + + LOG_STACK +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/StackTester.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/StackTester.java new file mode 100644 index 0000000000..2d406006fb --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/StackTester.java @@ -0,0 +1,742 @@ +/* + * StackTester.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.*; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.KeySelector; +import com.apple.cie.foundationdb.KeyValue; +import com.apple.cie.foundationdb.LocalityUtil; +import com.apple.cie.foundationdb.MutationType; +import com.apple.cie.foundationdb.Range; +import com.apple.cie.foundationdb.ReadTransaction; +import com.apple.cie.foundationdb.StreamingMode; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.AsyncIterable; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.ReadyFuture; +import com.apple.cie.foundationdb.tuple.ByteArrayUtil; +import com.apple.cie.foundationdb.tuple.Tuple; + +/** + * Implements a cross-binding test of the FoundationDB API. + * + */ +public class StackTester { + static final String DIRECTORY_PREFIX = "DIRECTORY_"; + + static class WaitEmpty implements Function { + private final byte[] prefix; + WaitEmpty(byte[] prefix) { + this.prefix = prefix; + } + + @Override + public Void apply(Transaction tr) { + List asList = tr.getRange(Range.startsWith(prefix)).asList().get(); + if(asList.size() > 0) { + //System.out.println(" - Throwing new fake commit error..."); + throw new FDBException("ERROR: Fake commit conflict", 1020); + } + return null; + } + } + + static void processInstruction(final Instruction inst) throws Throwable { + try { + StackOperation op = StackOperation.valueOf(inst.op); + if(op == StackOperation.PUSH) { + Object item = inst.tokens.get(1); + inst.push(item); + /*if(item instanceof byte[]) + System.out.println(inst.context.preStr + " - " + "Pushing '" + ByteArrayUtil.printable((byte[]) item) + "'"); + else if(item instanceof Number) + System.out.println(inst.context.preStr + " - " + "Pushing " + ((Number)item).longValue()); + else if(item instanceof String) + System.out.println(inst.context.preStr + " - " + "Pushing (utf8) '" + item.toString() + "'"); + else if(item == null) + System.out.println(inst.context.preStr + " - " + "Pushing null"); + else + System.out.println(inst.context.preStr + " - " + "Pushing item of type " + item.getClass().getName());*/ + } + else if(op == StackOperation.POP) { + inst.pop(); + } + else if(op == StackOperation.DUP) { + if(inst.size() == 0) + throw new RuntimeException("No stack bro!! (" + inst.context.preStr + ")"); + StackEntry e = inst.pop(); + inst.push(e); + inst.push(e); + } + else if(op == StackOperation.EMPTY_STACK) { + inst.clear(); + } + else if(op == StackOperation.SWAP) { + List params = inst.popParams(1).get(); + int index = StackUtils.getInt(params, 0); + inst.swap(index); + } + else if(op == StackOperation.WAIT_FUTURE) { + StackEntry e = inst.pop(); + inst.push(e.idx, StackUtils.serializeFuture(e.value)); + } + else if(op == StackOperation.WAIT_EMPTY) { + List params = inst.popParams(1).get(); + inst.context.db.run(new WaitEmpty((byte [])params.get(0))); + inst.push("WAITED_FOR_EMPTY".getBytes()); + } + else if(op == StackOperation.START_THREAD) { + List params = inst.popParams(1).get(); + //System.out.println(inst.context.preStr + " - " + "Starting new thread at prefix: " + ByteArrayUtil.printable((byte[]) params.get(0))); + inst.context.addContext((byte[])params.get(0)); + } + else if(op == StackOperation.NEW_TRANSACTION) { + inst.context.newTransaction(); + } + else if (op == StackOperation.USE_TRANSACTION) { + List params = inst.popParams(1).get(); + inst.context.switchTransaction((byte[])params.get(0)); + } + else if(op == StackOperation.SET) { + final List params = inst.popParams(2).get(); + //System.out.println(inst.context.preStr + " - " + "Setting '" + ArrayUtils.printable((byte[]) params.get(0)) + + // "' to '" + ArrayUtils.printable((byte[]) params.get(1)) + "'"); + executeMutation(inst, + new Function() { + @Override + public Void apply(Transaction tr) { + tr.set((byte[])params.get(0), (byte[])params.get(1)); + return null; + } + }); + } + else if(op == StackOperation.CLEAR) { + final List params = inst.popParams(1).get(); + //System.out.println(inst.context.preStr + " - " + "Clearing: '" + ByteArrayUtil.printable((byte[]) params.get(0)) + "'"); + executeMutation(inst, + new Function() { + @Override + public Void apply(Transaction tr) { + tr.clear((byte[])params.get(0)); + return null; + } + } + ); + } + else if(op == StackOperation.CLEAR_RANGE) { + final List params = inst.popParams(2).get(); + executeMutation(inst, + new Function() { + @Override + public Void apply(Transaction tr) { + tr.clear((byte[])params.get(0), (byte[])params.get(1)); + return null; + } + }); + } + else if(op == StackOperation.CLEAR_RANGE_STARTS_WITH) { + final List params = inst.popParams(1).get(); + executeMutation(inst, + new Function() { + @Override + public Void apply(Transaction tr) { + tr.clear(Range.startsWith((byte[])params.get(0))); + return null; + } + }); + } + else if(op == StackOperation.ATOMIC_OP) { + final List params = inst.popParams(3).get(); + final MutationType optype = MutationType.valueOf((String)params.get(0)); + executeMutation(inst, + new Function() { + @Override + public Void apply(Transaction tr) { + tr.mutate(optype, (byte[])params.get(1), (byte[])params.get(2)); + return null; + } + } + ); + } + else if(op == StackOperation.COMMIT) { + inst.push(inst.tr.commit()); + } + else if(op == StackOperation.READ_CONFLICT_RANGE) { + List params = inst.popParams(2).get(); + inst.tr.addReadConflictRange((byte[])params.get(0), (byte[])params.get(1)); + inst.push("SET_CONFLICT_RANGE".getBytes()); + } + else if(op == StackOperation.WRITE_CONFLICT_RANGE) { + List params = inst.popParams(2).get(); + inst.tr.addWriteConflictRange((byte[])params.get(0), (byte[])params.get(1)); + inst.push("SET_CONFLICT_RANGE".getBytes()); + } + else if(op == StackOperation.READ_CONFLICT_KEY) { + List params = inst.popParams(1).get(); + inst.tr.addReadConflictKey((byte[])params.get(0)); + inst.push("SET_CONFLICT_KEY".getBytes()); + } + else if(op == StackOperation.WRITE_CONFLICT_KEY) { + List params = inst.popParams(1).get(); + inst.tr.addWriteConflictKey((byte[])params.get(0)); + inst.push("SET_CONFLICT_KEY".getBytes()); + } + else if(op == StackOperation.DISABLE_WRITE_CONFLICT) { + inst.tr.options().setNextWriteNoWriteConflictRange(); + } + else if(op == StackOperation.RESET) { + inst.setTransaction(inst.tr.reset()); + } + else if(op == StackOperation.CANCEL) { + inst.tr.cancel(); + } + else if(op == StackOperation.GET) { + final List params = inst.popParams(1).get(); + Future f = inst.readTcx.read(new Function>() { + @Override + public Future apply(ReadTransaction readTr) { + return inst.readTr.get((byte[])params.get(0)); + } + }); + + inst.push(f); + } + else if(op == StackOperation.GET_RANGE) { + List params = inst.popParams(5).get(); + + final byte[] begin = (byte[])params.get(0); + final byte[] end = (byte[])params.get(1); + final int limit = StackUtils.getInt(params.get(2)); + final boolean reverse = StackUtils.getBoolean(params.get(3)); + final StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(4), StreamingMode.ITERATOR.code())); + + /*System.out.println("GetRange: " + ByteArrayUtil.printable(begin) + + ", " + ByteArrayUtil.printable(end));*/ + + List items = inst.readTcx.read(new Function>() { + @Override + public List apply(ReadTransaction readTr) { + return executeRangeQuery(readTr.getRange(begin, end, limit, reverse, mode)); + } + }); + + inst.push(Tuple.fromItems(items).pack()); + } + else if(op == StackOperation.GET_RANGE_SELECTOR) { + final List params = inst.popParams(10).get(); + + final KeySelector start = StackUtils.createSelector(params.get(0),params.get(1), params.get(2)); + final KeySelector end = StackUtils.createSelector(params.get(3), params.get(4), params.get(5)); + final int limit = StackUtils.getInt(params.get(6)); + final boolean reverse = StackUtils.getBoolean(params.get(7)); + final StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(8), StreamingMode.ITERATOR.code())); + + List items = inst.readTcx.read(new Function>() { + @Override + public List apply(ReadTransaction readTr) { + return executeRangeQuery(readTr.getRange(start, end, limit, reverse, mode), (byte[])params.get(9)); + } + }); + + inst.push(Tuple.fromItems(items).pack()); + } + else if(op == StackOperation.GET_RANGE_STARTS_WITH) { + List params = inst.popParams(4).get(); + + final byte[] prefix = (byte[])params.get(0); + final int limit = StackUtils.getInt(params.get(1)); + final boolean reverse = StackUtils.getBoolean(params.get(2)); + final StreamingMode mode = inst.context.streamingModeFromCode( + StackUtils.getInt(params.get(3), StreamingMode.ITERATOR.code())); + + List items = inst.readTcx.read(new Function>() { + @Override + public List apply(ReadTransaction readTr) { + return executeRangeQuery(readTr.getRange(Range.startsWith(prefix), limit, reverse, mode)); + } + }); + + inst.push(Tuple.fromItems(items).pack()); + } + else if(op == StackOperation.GET_KEY) { + List params = inst.popParams(4).get(); + final KeySelector start = StackUtils.createSelector(params.get(0),params.get(1), params.get(2)); + + byte[] key = inst.readTcx.read(new Function() { + @Override + public byte[] apply(ReadTransaction readTr) { + return inst.readTr.getKey(start).get(); + } + }); + + inst.push(filterKeyResult(key, (byte[])params.get(3))); + } + else if(op == StackOperation.GET_READ_VERSION) { + inst.context.lastVersion = inst.readTr.getReadVersion().get(); + inst.push("GOT_READ_VERSION".getBytes()); + } + else if(op == StackOperation.GET_COMMITTED_VERSION) { + inst.context.lastVersion = inst.tr.getCommittedVersion(); + inst.push("GOT_COMMITTED_VERSION".getBytes()); + } + else if(op == StackOperation.GET_VERSIONSTAMP) { + inst.push(inst.tr.getVersionstamp()); + } + else if(op == StackOperation.SET_READ_VERSION) { + if(inst.context.lastVersion == null) + throw new IllegalArgumentException("Read version has not been read " + inst.context.preStr); + inst.tr.setReadVersion(inst.context.lastVersion); + } + else if(op == StackOperation.ON_ERROR) { + List params = inst.popParams(1).get(); + int errorCode = StackUtils.getInt(params, 0); + + // 1102 (future_released) is not an error to Java. This is never encountered by user code, + // so we have to do something rather messy here to get compatibility with other languages. + // + // First, try on error with a retryable error. If it fails, then the transaction is in + // a failed state and we should rethrow the error. Otherwise, throw the original error. + boolean filteredError = errorCode == 1102; + + FDBException err = new FDBException("Fake testing error", filteredError ? 1020 : errorCode); + + try { + inst.setTransaction(inst.tr.onError(err).get()); + } + catch(Throwable t) { + inst.context.newTransaction(); // Other bindings allow reuse of non-retryable transactions, so we need to emulate that behavior. + throw t; + } + + if(filteredError) { + throw new FDBException("Fake testing error", errorCode); + } + + inst.push(ReadyFuture.DONE); + } + else if(op == StackOperation.SUB) { + List params = inst.popParams(2).get(); + long result = StackUtils.getNumber(params.get(0)).longValue() - StackUtils.getNumber(params.get(1)).longValue(); + inst.push(result); + } + else if(op == StackOperation.CONCAT) { + List params = inst.popParams(2).get(); + if(params.get(0) instanceof String) { + inst.push((String)params.get(0) + (String)params.get(1)); + } + else { + inst.push(ByteArrayUtil.join((byte[])params.get(0), (byte[])params.get(1))); + } + } + else if(op == StackOperation.TUPLE_PACK) { + List params = inst.popParams(1).get(); + int tupleSize = StackUtils.getInt(params.get(0)); + //System.out.println(inst.context.preStr + " - " + "Packing top " + tupleSize + " items from stack"); + List elements = inst.popParams(tupleSize).get(); + byte[] coded = Tuple.fromItems(elements).pack(); + //System.out.println(inst.context.preStr + " - " + " -> result '" + ByteArrayUtil.printable(coded) + "'"); + inst.push(coded); + } + else if(op == StackOperation.TUPLE_UNPACK) { + List params = inst.popParams(1).get(); + /*System.out.println(inst.context.preStr + " - " + "Unpacking tuple code: " + + ByteArrayUtil.printable((byte[]) params.get(0)));*/ + Tuple t = Tuple.fromBytes((byte[])params.get(0)); + for(Object o : t.getItems()) { + byte[] itemBytes = Tuple.from(o).pack(); + inst.push(itemBytes); + } + } + else if(op == StackOperation.TUPLE_RANGE) { + List params = inst.popParams(1).get(); + int tupleSize = StackUtils.getInt(params, 0); + //System.out.println(inst.context.preStr + " - " + "Tuple range with top " + tupleSize + " items from stack"); + List elements = inst.popParams(tupleSize).get(); + Range range = Tuple.fromItems(elements).range(); + inst.push(range.begin); + inst.push(range.end); + } + else if(op == StackOperation.UNIT_TESTS) { + try { + inst.context.db.options().setLocationCacheSize(100001); + inst.context.db.run(new Function() { + @Override + public Void apply(Transaction tr) { + tr.options().setPrioritySystemImmediate(); + tr.options().setPriorityBatch(); + tr.options().setCausalReadRisky(); + tr.options().setCausalWriteRisky(); + tr.options().setReadYourWritesDisable(); + tr.options().setReadAheadDisable(); + tr.options().setReadSystemKeys(); + tr.options().setAccessSystemKeys(); + tr.options().setDurabilityDevNullIsWebScale(); + tr.options().setTimeout(60*1000); + tr.options().setRetryLimit(50); + tr.options().setMaxRetryDelay(100); + tr.options().setUsedDuringCommitProtectionDisable(); + tr.options().setTransactionLoggingEnable("my_transaction"); + + if(!(new FDBException("Fake", 1020)).isRetryable() || + (new FDBException("Fake", 10)).isRetryable()) + throw new RuntimeException("Unit test failed: Error predicate incorrect"); + + byte[] test = {(byte) 0xff}; + tr.get(test).get(); + + return null; + } + }); + + testWatches(inst.context.db); + testLocality(inst.context.db); + } + catch(Exception e) { + throw new Exception("Unit tests failed: " + e.getMessage()); + } + } + else if(op == StackOperation.LOG_STACK) { + List params = inst.popParams(1).get(); + byte[] prefix = (byte[]) params.get(0); + + Map entries = new HashMap(); + while(inst.size() > 0) { + entries.put(inst.size()-1, inst.pop()); + if(entries.size() == 100) { + logStack(inst.context.db, entries, prefix); + entries.clear(); + } + } + + logStack(inst.context.db, entries, prefix); + } + else { + throw new IllegalArgumentException("Unrecognized (or unimplemented) operation"); + } + } catch (FDBException e) { + //System.out.println(" Pushing error! (" + e.getMessage() + ")"); + StackUtils.pushError(inst, e); + //throw e; + } catch (IllegalStateException e) { + //Java throws this instead of an FDBException for error code 2015, so we have to translate it + if(e.getMessage().equals("Future not ready")) + StackUtils.pushError(inst, new FDBException("", 2015)); + else + throw e; + } + } + + static class SynchronousContext extends Context { + DirectoryExtension directoryExtension = new DirectoryExtension(); + + SynchronousContext(Database db, byte[] prefix) { + super(db, prefix); + } + + @Override + Context createContext(byte[] prefix) { + return new SynchronousContext(this.db, prefix); + } + + void processOp(byte[] operation) throws FDBException, Throwable { + Tuple tokens = Tuple.fromBytes(operation); + Instruction inst = new Instruction(this, tokens); + + //if(!inst.op.equals("PUSH") && !inst.op.equals("SWAP")) + // System.out.println(inst.context.preStr + " - " + "OP (" + inst.context.instructionIndex + "):" + inst.op); + /*for(Object o : op.tokens.getItems()) + System.out.print(", " + o);*/ + + if(inst.op.startsWith(DIRECTORY_PREFIX)) + directoryExtension.processInstruction(inst); + else + processInstruction(inst); + } + + @Override + void executeOperations() throws Throwable { + KeySelector begin = nextKey; + Transaction t = db.createTransaction(); + while(true) { + List keyValues = null; + try { + keyValues = t.getRange(begin, endKey/*, 1000*/).asList().get(); + } + catch(FDBException e) { + t = t.onError(e).get(); + continue; + } + + //System.out.println(" * Got " + keyValues.size() + " instructions"); + if(keyValues.size() == 0) + break; + + for(KeyValue next : keyValues) { + begin = KeySelector.firstGreaterThan(next.getKey()); + processOp(next.getValue()); + instructionIndex++; + } + } + //System.out.println(" * Completed " + instructionIndex + " instructions"); + } + } + + private static void executeMutation(Instruction inst, Function r) + throws FDBException, Exception { + // run this with a retry loop (and commit) + inst.tcx.run(r); + if(inst.isDatabase) + inst.push("RESULT_NOT_PRESENT".getBytes()); + } + + private static byte[] filterKeyResult(byte[] key, final byte[] prefixFilter) { + if(ByteArrayUtil.startsWith(key, prefixFilter)) { + return key; + } + else if(ByteArrayUtil.compareUnsigned(key, prefixFilter) < 0) { + return prefixFilter; + } + else { + return ByteArrayUtil.strinc(prefixFilter); + } + } + + private static List executeRangeQuery(AsyncIterable itr) { + return executeRangeQuery(itr, null); + } + + private static List executeRangeQuery(AsyncIterable itr, byte[] prefixFilter) { + if(Math.random() < 0.5) + return getRange(itr, prefixFilter); + else + return getRangeAsList(itr, prefixFilter); + } + + private static List getRange(AsyncIterable itr, byte[] prefixFilter) { + //System.out.println("GetRange"); + List o = new LinkedList(); + for(KeyValue kv : itr) { + if(prefixFilter == null || ByteArrayUtil.startsWith(kv.getKey(), prefixFilter)) { + o.add(kv.getKey()); + o.add(kv.getValue()); + } + } + + return o; + } + + private static List getRangeAsList(AsyncIterable itr, byte[] prefixFilter) { + //System.out.println("GetRangeAsList"); + List list = itr.asList().get(); + List o = new LinkedList(); + for(KeyValue kv : list) { + if(prefixFilter == null || ByteArrayUtil.startsWith(kv.getKey(), prefixFilter)) { + o.add(kv.getKey()); + o.add(kv.getValue()); + } + } + + return o; + } + + private static void logStack(final Database db, final Map entries, final byte[] prefix) { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + for(Map.Entry it : entries.entrySet()) { + byte[] pk = ByteArrayUtil.join(prefix, Tuple.from(it.getKey(), it.getValue().idx).pack()); + byte[] pv = Tuple.from(StackUtils.serializeFuture(it.getValue().value)).pack(); + tr.set(pk, pv.length < 40000 ? pv : Arrays.copyOfRange(pv, 0, 40000)); + } + + return null; + } + }); + } + + private static boolean checkWatches(List> watches, Database db, boolean expected) { + for(Future w : watches) { + if(w.isDone() || expected) { + try { + w.get(); + if(!expected) { + throw new IllegalStateException("A watch triggered too early"); + } + } + catch(FDBException e) { + Transaction tr = db.createTransaction(); + tr.onError(e).get(); + return false; + } + } + } + + return true; + } + + private static void testWatches(Database db) { + while(true) { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + tr.set("foo".getBytes(), "f".getBytes()); + tr.clear("bar".getBytes()); + return null; + } + }); + + List> watches = db.run(new Function>>() { + @Override + public List> apply(Transaction tr) { + List> watchList = new LinkedList>(); + watchList.add(tr.watch("foo".getBytes())); + watchList.add(tr.watch("bar".getBytes())); + tr.set("foo".getBytes(), "f".getBytes()); + return watchList; + } + }); + + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + tr.clear("bar".getBytes()); + return null; + } + }); + + try { + Thread.sleep(5000); + } + catch(InterruptedException e) { + e.printStackTrace(); + // continue... + } + + if(!checkWatches(watches, db, false)) { + continue; + } + + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + tr.set("foo".getBytes(), "bar".getBytes()); + tr.set("bar".getBytes(), "foo".getBytes()); + return null; + } + }); + + if(checkWatches(watches, db, true)) { + return; + } + } + } + + private static void testLocality(Database db) { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + tr.options().setTimeout(60*1000); + tr.options().setReadSystemKeys(); + tr.getReadVersion().get(); + + AsyncIterable boundaryKeys = LocalityUtil.getBoundaryKeys( + tr, new byte[0], new byte[]{(byte) 255, (byte) 255}); + + List keys = boundaryKeys.asList().get(); + for(int i = 0; i < keys.size() - 1; i++) { + byte[] start = keys.get(i); + byte[] end = tr.getKey(KeySelector.lastLessThan(keys.get(i + 1))).get(); + List startAddresses = Arrays.asList(LocalityUtil.getAddressesForKey(tr, start).get()); + List endAddresses = Arrays.asList(LocalityUtil.getAddressesForKey(tr, end).get()); + for(String a : startAddresses) { + if(!endAddresses.contains(a)) { + throw new RuntimeException("Locality not internally consistent."); + } + } + } + + return null; + } + }); + } + + /** + * Run a stack-machine based test. + * + * @param args 0: version, 1: snapshot + */ + public static void main(String[] args) { + if(args.length < 1) + throw new IllegalArgumentException("StackTester needs parameters "); + + /*Thread t = new Thread(new Runnable(){ + @Override + public void run() { + try { + Thread.sleep(1000 * 60 * 2); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + Map traces = Thread.getAllStackTraces(); + System.out.println("Printing traces for " + traces.size() + " threads."); + for(Thread thread : traces.keySet()) { + System.out.println(" Thread (" + thread.getName() + ")"); + StackTraceElement[] tr = traces.get(thread); + for(StackTraceElement e : tr) { + System.out.println(" " + e); + } + } + }}); + t.setDaemon(true); + t.start();*/ + + byte[] prefix = args[0].getBytes(); + + FDB fdb = FDB.selectAPIVersion(Integer.parseInt(args[1])); + Database db; + /*fdb.startNetwork(); + Cluster cluster = fdb.createCluster(args.length > 1 ? args[1] : null, new Executor() { + public void execute(Runnable r) { + r.run(); + } + }); + + db = cluster.openDatabase();*/ + if(args.length == 2) + db = fdb.open(); + else + db = fdb.open(args[2]); + + Context c = new SynchronousContext(db, prefix); + //System.out.println("Starting test..."); + c.run(); + //System.out.println("Done with test."); + } +} + diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/StackUtils.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/StackUtils.java new file mode 100644 index 0000000000..6b1bfb11d0 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/StackUtils.java @@ -0,0 +1,114 @@ +/* + * StackUtils.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.List; + +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.KeySelector; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.tuple.Tuple; + +public class StackUtils { + + static boolean pushError(Instruction inst, FDBException error) { + if(error.getCode() != 0) + inst.push(getErrorBytes(error)); + + return error.getCode() != 0; + } + + static byte[] getErrorBytes(FDBException error) { + Tuple t = new Tuple() + .add("ERROR".getBytes()) + .add(Integer.toString(error.getCode()).getBytes()); + + return t.pack(); + } + + static Object serializeFuture(Object item) { + try { + if(!(item instanceof Future)) { + return item; + } + Future future = (Future)item; + item = future.get(); + if(item == null) + item = "RESULT_NOT_PRESENT".getBytes(); + } + catch(FDBException e) { + item = getErrorBytes(e); + } + catch(IllegalStateException e) { + //Java throws this instead of an FDBException for error code 2015, so we have to translate it + if(e.getMessage().equals("Future not ready")) + item = getErrorBytes(new FDBException("", 2015)); + else + throw e; + } + return item; + } + + //////////////////////// + // Utilities for forcing Objects into various types + //////////////////////// + static int getInt(List params, int index) { + Object object = params.get(index); + return getInt(object); + } + + static int getInt(Object object) { + return getInt(object, null); + } + + static int getInt(Object object, Integer defaultValue) { + if(object == null) { + if(defaultValue == null) + throw new NullPointerException("Null input with no default"); + return defaultValue; + } + return ((Number)object).intValue(); + } + + static Number getNumber(Object object) { + return ((Number)object); + } + + static boolean getBoolean(Object o) { + return getBoolean(o, null); + } + + static boolean getBoolean(Object o, Boolean defaultValue) { + if(o == null) { + if(defaultValue == null) + throw new NullPointerException("Null input with no default"); + return defaultValue; + } + return getInt(o) != 0; + } + + static KeySelector createSelector(Object key, Object orEqualObj, Object offsetObj) { + boolean orEqual = getBoolean(orEqualObj, null); + int offset = getInt(offsetObj); + return new KeySelector((byte[])key, orEqual, offset); + } + +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/TestApp.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/TestApp.java new file mode 100644 index 0000000000..1058a816e6 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/TestApp.java @@ -0,0 +1,105 @@ +/* + * TestApp.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.Future; + +public class TestApp { + + public static void main(String[] args) throws Exception { + try { + Cluster cluster = FDB.selectAPIVersion(500).createCluster("C:\\Users\\Ben\\workspace\\fdb\\fdb.cluster"); + System.out.println("I now have the cluster"); + Database db = cluster.openDatabase(); + + Transaction tr = db.createTransaction(); + System.out.println("TR: " + tr); + + byte[] appleValue = tr.get("apple".getBytes()).get(); + System.out.println("Apple: " + (appleValue == null ? null : new String(appleValue))); + + tr.set("apple".getBytes(), "crunchy".getBytes()); + System.out.println("Attmepting to commit apple/crunchy..."); + tr.commit().get(); // FIXME: this is not an ok use of the API + tr = tr.reset(); + + long topTime = 0, blockTime = 0, getTime = 0, bottomTime = 0; + +// Future commit = null; + for(int i = 0; i < 1000; i++) { +/* if(commit != null) + commit.get();*/ + + long a = System.currentTimeMillis(); + final int idx = i; + final byte[] key = ("apple" + idx).getBytes(); + tr = db.createTransaction(); + //System.out.println("TR (" + i + "): " + tr); + Future future = tr.get(key); + long b = System.currentTimeMillis(); + future.blockUntilReady(); + long c = System.currentTimeMillis(); + appleValue = future.get(); + long d = System.currentTimeMillis(); + /*boolean present = appleValue != null; + System.out.println("Value is " + + (present ? "present" : "missing") ); + if(present) + System.out.println("Value is <" + new String(appleValue) + ">");*/ + tr.set(key, ("Apple" + i).getBytes()); + final Future commit = tr.commit(); + long e = System.currentTimeMillis(); + commit.onReady(new Runnable() { + @Override + public void run() { + try { + commit.get(); + } catch (FDBException e) { + e.printStackTrace(); + } + } + }); + + topTime += b - a; + blockTime += c - b; + getTime += d - c; + bottomTime += e - d; + } + + System.out.println(" Top: " + topTime); + System.out.println(" Block: " + blockTime); + System.out.println(" Get: " + getTime); + System.out.println(" Bottom: " + bottomTime); + + tr.dispose(); + db.dispose(); + cluster.dispose(); +// commit.get(); + } catch(Throwable t) { + t.printStackTrace(); + } + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/TupleTest.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/TupleTest.java new file mode 100644 index 0000000000..6313e52367 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/TupleTest.java @@ -0,0 +1,75 @@ +/* + * TupleTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import com.apple.cie.foundationdb.Cluster; +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.TransactionContext; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.tuple.Tuple; + +public class TupleTest { + private static final String CLUSTER_FILE = "C:\\Users\\Ben\\workspace\\fdb\\fdb.cluster"; + + public static void main(String[] args) throws InterruptedException { + final int reps = 1000; + try { + Cluster c = FDB.selectAPIVersion(500).createCluster(CLUSTER_FILE); + Database db = c.openDatabase(); + runTests(reps, db); + } catch(Throwable t) { + t.printStackTrace(); + } + } + + private static void runTests(final int reps, TransactionContext db) { + System.out.println("Running tests..."); + long start = System.currentTimeMillis(); + try { + db.run(new Function() { + @Override + public Void apply(Transaction tr) { + Tuple t = new Tuple(); + t.add(100230045000L); + t.add("Hello!"); + t.add("foo".getBytes()); + + /*for(Map.Entry e : tr.getRange("vcount".getBytes(), "zz".getBytes())) { + System.out.println("K: " + new String(e.getKey()) + ", V: " + new String(e.getValue())); + }*/ + return null; + } + }); + } catch (Throwable e) { + e.printStackTrace(); + } + long end = System.currentTimeMillis(); + + double seconds = (end - start) / 1000.0; + System.out.println(" Transactions: " + reps); + System.out.println(" Total Time: " + seconds); + System.out.println(" Gets+Sets / sec: " + reps / seconds); + + System.exit(0); + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/VersionstampExample.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/VersionstampExample.java new file mode 100644 index 0000000000..9d33540aaa --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/VersionstampExample.java @@ -0,0 +1,63 @@ +/* + * VersionstampExample.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.*; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.tuple.Tuple; +import com.apple.cie.foundationdb.async.Future; + +import java.lang.System; + +public class VersionstampExample { + final protected static char[] hexArray = "0123456789ABCDEF".toCharArray(); + public static String bytesToHex(byte[] bytes) { + char[] hexChars = new char[bytes.length * 2]; + for ( int j = 0; j < bytes.length; j++ ) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = hexArray[v >>> 4]; + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; + } + return new String(hexChars); + } + + public static void main(String[] args) { + FDB fdb = FDB.selectAPIVersion(500); + Database db = fdb.open(); + Transaction tr = db.createTransaction(); + tr.mutate(MutationType.SET_VERSIONSTAMPED_VALUE, "foo".getBytes(), "blahblahbl".getBytes()); + Future fvs = tr.getVersionstamp(); + tr.commit().get(); + byte[] vs = fvs.get(); + + // Get the value of 'hello' from the database + byte[] dbVs = db.run(new Function() { + public byte[] apply(Transaction tr) { + return tr.get("foo".getBytes()).get(); + } + }); + System.out.println("vs" + bytesToHex(vs)); + System.out.println("dbVs" + bytesToHex(dbVs)); + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/WatchTest.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/WatchTest.java new file mode 100644 index 0000000000..772cd049ac --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/WatchTest.java @@ -0,0 +1,123 @@ +/* + * WatchTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import java.util.Random; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; + +import com.apple.cie.foundationdb.Database; +import com.apple.cie.foundationdb.FDB; +import com.apple.cie.foundationdb.FDBException; +import com.apple.cie.foundationdb.Transaction; +import com.apple.cie.foundationdb.async.Future; + +public class WatchTest { + + public static void main(String[] args) { + FDB fdb = FDB.selectAPIVersion(500); + Database database = fdb.open(args[0]); + database.options().setLocationCacheSize(42); + Transaction tr = database.createTransaction(); + byte[] bs = tr.get("a".getBytes()).get(); + System.out.println("`a' -> " + (bs == null ? "" : new String(bs))); + final Future watch = tr.watch("a".getBytes()); + System.err.println("Watch started..."); + //System.exit(0); + tr.commit().get(); + /*watch.onReady(new Runnable() { + @Override + public void run() { + System.err.println("Watch is ready!"); + watch.get(); + } + });*/ + watch.cancel(); + try { + watch.get(); + System.out.println("`a' changed"); + } catch(FDBException e) { + System.out.println("`a' watch error -> " + e.getMessage()); + if(e.getCode() != 1101) + throw e; + } + + raceTest(database); + } + + public static void raceTest(Database db) { + ExecutorService e = Executors.newCachedThreadPool(); // Executors.newFixedThreadPool(2); + Random r = new Random(); + + Transaction tr = db.createTransaction(); + byte[] key = "hello".getBytes(); + + for(int i = 0; i < 10000; i++) { + final Future f = tr.watch(key); + final AtomicInteger a = new AtomicInteger(); + Runnable cancel = new Runnable() { + @Override + public void run() { + System.err.println("`f' cancel()..."); + f.cancel(); + a.incrementAndGet(); + } + }; + Runnable get = new Runnable() { + @Override + public void run() { + try { + System.err.println("`f' get()..."); + f.get(); + System.err.println("`f' changed"); + } catch(FDBException e) { + System.err.println("`f' watch error -> " + e.getMessage()); + if(e.getCode() != 1101) + throw e; + } finally { + a.incrementAndGet(); + } + } + }; + if(r.nextBoolean()) { + e.execute(cancel); + e.execute(get); + } else { + e.execute(get); + e.execute(cancel); + } + + while(a.get() != 2); { + try { + Thread.sleep(1); + } catch (InterruptedException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + } + + //if(i % 1000 == 0) { + System.out.println("Done with " + i); + //} + } + } +} diff --git a/bindings/java/src/test/com/apple/cie/foundationdb/test/WhileTrueTest.java b/bindings/java/src/test/com/apple/cie/foundationdb/test/WhileTrueTest.java new file mode 100644 index 0000000000..2e55439bb8 --- /dev/null +++ b/bindings/java/src/test/com/apple/cie/foundationdb/test/WhileTrueTest.java @@ -0,0 +1,50 @@ +/* + * WhileTrueTest.java + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.apple.cie.foundationdb.test; + +import com.apple.cie.foundationdb.async.AsyncUtil; +import com.apple.cie.foundationdb.async.Function; +import com.apple.cie.foundationdb.async.Future; +import com.apple.cie.foundationdb.async.ReadyFuture; + +public class WhileTrueTest { + private static int count; + + public static void main(String[] args) { + // This should caused memory issues using the old implementation within the + // completable implementation of whileTrue. This does not appear to be + // a problem with the non-completable implementation, which was more like + // the new implementation over there. + // Pro tip: Run with options -Xms16m -Xmx16m -XX:+HeadDumpOnOutOfMemoryError + count = 10000000; + + //AsyncUtil.whileTrue(v -> CompletableFuture.completedFuture(count.decrementAndGet()).thenApplyAsync(c -> c > 0)).join(); + AsyncUtil.whileTrue(new Function>() { + @Override + public Future apply(Void v) { + count -= 1; + return new ReadyFuture(count > 0); + } + }).get(); + + System.out.println("Final value: " + count); + } +} diff --git a/bindings/nodejs/README.md b/bindings/nodejs/README.md new file mode 100644 index 0000000000..c946e805db --- /dev/null +++ b/bindings/nodejs/README.md @@ -0,0 +1,3 @@ +Complete documentation of the FoundationDB Node.js API can be found at [https://foundationdb.org/documentation/api-node.html](https://foundationdb.org/documentation/api-node.html). + +These bindings require the FoundationDB client. The client can be obtained from [https://files.foundationdb.org/fdb-c/](https://files.foundationdb.org/fdb-c/). diff --git a/bindings/nodejs/binding.gyp b/bindings/nodejs/binding.gyp new file mode 100644 index 0000000000..d24034df76 --- /dev/null +++ b/bindings/nodejs/binding.gyp @@ -0,0 +1,25 @@ +{ + 'targets': [ + { + 'target_name': 'fdblib', + 'sources': [ 'src/FdbV8Wrapper.cpp', 'src/Database.cpp', 'src/Transaction.cpp', 'src/Cluster.cpp', 'src/FdbError.cpp', 'src/FdbOptions.cpp', 'src/FdbOptions.g.cpp' ], + 'include_dirs': ['../c'], + 'conditions': [ + ['OS=="linux"', { + 'link_settings': { 'libraries': ['-lfdb_c', '-L../../../lib'] }, + }], + ['OS=="mac"', { + 'xcode_settings': { + 'MACOSX_DEPLOYMENT_TARGET': '10.7', # -mmacosx-version-min=10.7 + 'OTHER_CFLAGS': ['-std=c++0x'] + }, + 'link_settings': { 'libraries': ['-lfdb_c', '-L../../../lib'] }, + }], + ['OS=="win"', { + 'link_settings': { 'libraries': ['../../../bin/Release/fdb_c.lib'] }, + }], + ], + 'cflags': ['-std=c++0x'], + } + ] +} diff --git a/bindings/nodejs/binding.gyp.npmsrc b/bindings/nodejs/binding.gyp.npmsrc new file mode 100644 index 0000000000..f6b6e5e691 --- /dev/null +++ b/bindings/nodejs/binding.gyp.npmsrc @@ -0,0 +1,23 @@ +{ + 'targets': [ + { + 'target_name': 'fdblib', + 'sources': [ 'src/FdbV8Wrapper.cpp', 'src/Database.cpp', 'src/Transaction.cpp', 'src/Cluster.cpp', 'src/FdbError.cpp', 'src/FdbOptions.cpp', 'src/FdbOptions.g.cpp' ], + 'conditions': [ + ['OS=="linux"', { + 'link_settings': { 'libraries': ['-lfdb_c'] }, + }], + ['OS=="mac"', { + 'xcode_settings': { 'OTHER_CFLAGS': ['-std=c++0x'] }, + 'include_dirs': ['/usr/local/include'], + 'link_settings': { 'libraries': ['-lfdb_c', '-L/usr/local/lib'] }, + }], + ['OS=="win"', { + 'link_settings': { 'libraries': [' + + + + + Debug + x64 + + + Release + x64 + + + + {9463CB25-DCA0-9D45-C46E-0A8E68EE7FAE} + Win32Proj + fdblib + + + + DynamicLibrary + + + + + + + + + true + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + false + false + $(SolutionDir)bin\$(Configuration)\ + $(ProjectName) + $(OutDir)$(ProjectName).node + .node + + + + "$(ProgramW6432)\nodejs\node_modules\npm\bin\node-gyp-bin\node-gyp.cmd" install --ensure -v --target=$(NodeVersion) + + + Download and install node-gyp header and lib files + + + +mkdir modules +mkdir "modules\$(NodeVersionDir)" +copy "$(TargetPath)" "modules\$(NodeVersionDir)\fdblib.node" + + + + + + $(USERPROFILE)\.node-gyp\$(NodeVersion)\src;$(USERPROFILE)\.node-gyp\$(NodeVersion)\deps\uv\include;$(USERPROFILE)\.node-gyp\$(NodeVersion)\deps\v8\include;..\c;%(AdditionalIncludeDirectories) + EnableFastChecks + true + ProgramDatabase + 4251;%(DisableSpecificWarnings) + Sync + false + false + Disabled + WIN32;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;BUILDING_V8_SHARED=1;BUILDING_UV_SHARED=1;DEBUG;_DEBUG;%(PreprocessorDefinitions) + MultiThreadedDebug + true + true + false + Level3 + + + $(USERPROFILE)\.node-gyp\$(NodeVersion)\$(Platform)\node.lib;..\..\bin\$(Configuration)\fdb_c.lib;%(AdditionalDependencies) + true + true + true + $(OutDir)$(ProjectName).node + true + true + MachineX64 + + + $(USERPROFILE)\.node-gyp\$(NodeVersion)\src;$(USERPROFILE)\.node-gyp\$(NodeVersion)\deps\uv\include;$(USERPROFILE)\.node-gyp\$(NodeVersion)\deps\v8\include;..\..\c;%(AdditionalIncludeDirectories) + WIN32;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;BUILDING_V8_SHARED=1;BUILDING_UV_SHARED=1;DEBUG;_DEBUG;%(PreprocessorDefinitions) + + + + + $(USERPROFILE)\.node-gyp\$(NodeVersion)\src;$(USERPROFILE)\.node-gyp\$(NodeVersion)\deps\uv\include;$(USERPROFILE)\.node-gyp\$(NodeVersion)\deps\v8\include;..\c;%(AdditionalIncludeDirectories) + /MP %(AdditionalOptions) + true + ProgramDatabase + 4251;%(DisableSpecificWarnings) + false + Speed + true + AnySuitable + true + true + Full + WIN32;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;BUILDING_V8_SHARED=1;BUILDING_UV_SHARED=1;%(PreprocessorDefinitions) + MultiThreaded + false + true + true + false + Level3 + true + + + /LTCG %(AdditionalOptions) + + + $(USERPROFILE)\.node-gyp\$(NodeVersion)\$(Platform)\node.lib;..\..\bin\$(Configuration)\fdb_c.lib;%(AdditionalDependencies) + true + true + true + true + UseLinkTimeCodeGeneration + true + $(OutDir)$(ProjectName).node + true + true + MachineX64 + + + $(USERPROFILE)\.node-gyp\$(NodeVersion)\src;$(USERPROFILE)\.node-gyp\$(NodeVersion)\deps\uv\include;$(USERPROFILE)\.node-gyp\$(NodeVersion)\deps\v8\include;..\..\c;%(AdditionalIncludeDirectories) + WIN32;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;BUILDING_V8_SHARED=1;BUILDING_UV_SHARED=1;%(PreprocessorDefinitions) + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/bindings/nodejs/fdb_node.vcxproj b/bindings/nodejs/fdb_node.vcxproj new file mode 100644 index 0000000000..dd9f8b9f28 --- /dev/null +++ b/bindings/nodejs/fdb_node.vcxproj @@ -0,0 +1,64 @@ + + + + + + -PRERELEASE + + + + + + + + + Debug + x64 + + + Release + x64 + + + + {9463CB25-DCA0-9D45-C46E-0A8E68EE7FAE} + Win32Proj + + + + v140_xp + + + + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + + + + + + del "$(SolutionDir)bin\$(Configuration)\fdb-node*.tar.gz" + + + + +c:\python27\python -c "print open(\"package.json.in\").read().replace(\"VERSION\",\"$(Version)$(PreReleaseDecoration)\")" > package.json +copy ..\..\LICENSE +c:\python27\python "$(SolutionDir)build/tarball.py" -r nodejs "$(SolutionDir)bin\$(Configuration)\fdb-node-$(Version)$(PreReleaseDecoration)-windows-$(Platform).tar.gz" lib modules package.json README.md LICENSE +del LICENSE + + + + + + + + false + + + false + + + + + + \ No newline at end of file diff --git a/bindings/nodejs/fdb_node_0_10.vcxproj b/bindings/nodejs/fdb_node_0_10.vcxproj new file mode 100644 index 0000000000..f47ce8ef11 --- /dev/null +++ b/bindings/nodejs/fdb_node_0_10.vcxproj @@ -0,0 +1,14 @@ + + + + {E22D4EF8-E75D-4281-93F9-A9F73936DE54} + + + v140_xp + + + 0.10.0 + 0.10 + + + \ No newline at end of file diff --git a/bindings/nodejs/fdb_node_0_8.vcxproj b/bindings/nodejs/fdb_node_0_8.vcxproj new file mode 100644 index 0000000000..e783b03ed1 --- /dev/null +++ b/bindings/nodejs/fdb_node_0_8.vcxproj @@ -0,0 +1,14 @@ + + + + {E936E200-689E-49FD-8463-32FE763F1860} + + + v140_xp + + + 0.8.22 + 0.8 + + + \ No newline at end of file diff --git a/bindings/nodejs/include.mk b/bindings/nodejs/include.mk new file mode 100644 index 0000000000..c6e7135f4c --- /dev/null +++ b/bindings/nodejs/include.mk @@ -0,0 +1,95 @@ +# +# include.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile-gmake; -*- + +TARGETS += fdb_node fdb_node_npm +CLEAN_TARGETS += fdb_node_clean fdb_node_npm_clean + +NODE_VERSIONS := 0.8.22 0.10.0 + +ifeq ($(RELEASE),true) + NPMVER = $(VERSION) +else + NPMVER = $(VERSION)-PRERELEASE +endif + +packages: fdb_node_npm + +packages_clean: fdb_node_npm_clean + +fdb_node: fdb_c bindings/nodejs/fdb_node.stamp + +bindings/nodejs/fdb_node.stamp: bindings/nodejs/src/FdbOptions.g.cpp bindings/nodejs/src/*.cpp bindings/nodejs/src/*.h bindings/nodejs/binding.gyp lib/libfdb_c.$(DLEXT) bindings/nodejs/package.json + @echo "Building $@" + @rm -f $@ + @cd bindings/nodejs && \ + mkdir -p modules && \ + rm -rf modules/* && \ + for ver in $(NODE_VERSIONS); do \ + MMVER=`echo $$ver | sed -e 's,\., ,g' | awk '{print $$1 "." $$2}'` && \ + mkdir modules/$$MMVER && \ + node-gyp configure --target=$$ver && \ + node-gyp -v build && \ + cp build/Release/fdblib.node modules/$${MMVER} ; \ + done + @touch $@ + +bindings/nodejs/src/FdbOptions.g.cpp: bin/vexillographer.exe fdbclient/vexillographer/fdb.options + @echo "Building $@" + @$(MONO) bin/vexillographer.exe fdbclient/vexillographer/fdb.options nodejs $@ + +fdb_node_clean: + @echo "Cleaning fdb_node" + @rm -f bindings/nodejs/src/FdbOptions.g.cpp + @rm -rf bindings/nodejs/modules + @(cd bindings/nodejs && node-gyp clean) + @rm -f bindings/nodejs/fdb_node.stamp + +bindings/nodejs/package.json: bindings/nodejs/package.json.in $(ALL_MAKEFILES) versions.target + @m4 -DVERSION=$(NPMVER) $< > $@ + @echo "Updating Node dependencies" + @cd bindings/nodejs && \ + npm update + +fdb_node_npm: fdb_node versions.target bindings/nodejs/README.md bindings/nodejs/lib/*.js bindings/nodejs/src/* bindings/nodejs/binding.gyp LICENSE + @echo "Packaging NPM" + @mkdir -p packages + @rm -f packages/fdb-node-* + @rm -rf packages/nodejs.tmp + @mkdir -p packages/nodejs.tmp/nodejs + @cp LICENSE packages/nodejs.tmp/nodejs/LICENSE + @tar -C bindings -czf packages/fdb-node-$(NPMVER)-$(PLATFORM)-$(ARCH).tar.gz nodejs/lib nodejs/modules nodejs/package.json nodejs/README.md -C ../packages/nodejs.tmp nodejs/LICENSE + @rm -rf packages/nodejs.tmp +ifeq ($(PLATFORM),linux) + @echo "Packaging NPM (unbuilt)" + @rm -rf packages/nodejs.tmp + @mkdir -p packages/nodejs.tmp/npmsrc/nodejs + @cat bindings/nodejs/package.json | grep -v private | grep -v engineStrict | awk '/"semver"/ {print " \"bindings\": \"*\""; next} {print}' > packages/nodejs.tmp/npmsrc/nodejs/package.json + @cp -r bindings/nodejs/lib bindings/nodejs/src bindings/nodejs/README.md LICENSE packages/nodejs.tmp/npmsrc/nodejs + @cp bindings/nodejs/binding.gyp.npmsrc packages/nodejs.tmp/npmsrc/nodejs/binding.gyp + @cp bindings/nodejs/fdbModule.js.npmsrc packages/nodejs.tmp/npmsrc/nodejs/lib/fdbModule.js + @tar -C packages/nodejs.tmp/npmsrc -czf packages/fdb-node-$(NPMVER).tar.gz nodejs + @rm -rf packages/nodejs.tmp +endif + +fdb_node_npm_clean: + @echo "Cleaning NPM" + @rm -f packages/fdb-node-* bindings/nodejs/package.json diff --git a/bindings/nodejs/lib/apiVersion.js b/bindings/nodejs/lib/apiVersion.js new file mode 100644 index 0000000000..6d4c6c560a --- /dev/null +++ b/bindings/nodejs/lib/apiVersion.js @@ -0,0 +1,26 @@ +/* + * apiVersion.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var apiVersion; +module.exports = { value: apiVersion }; + diff --git a/bindings/nodejs/lib/bufferConversion.js b/bindings/nodejs/lib/bufferConversion.js new file mode 100644 index 0000000000..1c149938ab --- /dev/null +++ b/bindings/nodejs/lib/bufferConversion.js @@ -0,0 +1,86 @@ +/* + * bufferConversion.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var toBuffer = function(obj) { + if(Buffer.isBuffer(obj)) + return obj; + + if(obj instanceof ArrayBuffer) + obj = new Uint8Array(obj); + + if(obj instanceof Uint8Array) { + var buf = new Buffer(obj.length); + for(var i = 0; i < obj.length; ++i) + buf[i] = obj[i]; + + return buf; + } + + if(typeof obj === 'string') + return new Buffer(obj, 'utf8'); + + throw new TypeError('toBuffer function expects a string, buffer, ArrayBuffer, or Uint8Array'); +}; + +toBuffer.fromByteLiteral = function(str) { + if(typeof str === 'string') { + var buf = new Buffer(str.length); + for(var i = 0; i < str.length; ++i) { + if(str[i] > 255) + throw new RangeError('fromByteLiteral string argument cannot have codepoints larger than 1 byte'); + buf[i] = str.charCodeAt(i); + } + return buf; + } + else + throw new TypeError('fromByteLiteral function expects a string'); +}; + +toBuffer.toByteLiteral = function(buf) { + if(Buffer.isBuffer(buf)) + return String.fromCharCode.apply(null, buf); + else + throw new TypeError('toByteLiteral function expects a buffer'); +}; + +toBuffer.printable = function(buf) { + buf = toBuffer(buf); + var out = ''; + for(var i = 0; i < buf.length; ++i) { + if(buf[i] >= 32 && buf[i] < 127 && buf[i] !== 92) + out += String.fromCharCode(buf[i]); + else if(buf[i] === 92) + out += '\\\\'; + else { + var str = buf[i].toString(16); + out += '\\x'; + if(str.length == 1) + out += '0'; + out += str; + } + } + + return out; +}; + +module.exports = toBuffer; diff --git a/bindings/nodejs/lib/cluster.js b/bindings/nodejs/lib/cluster.js new file mode 100644 index 0000000000..a4662c5e3b --- /dev/null +++ b/bindings/nodejs/lib/cluster.js @@ -0,0 +1,48 @@ +/* + * cluster.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var future = require('./future'); +var Database = require('./database'); +var apiVersion = require('./apiVersion'); + +var openDatabase = function(dbName) { + return new Database(this._cluster.openDatabase(dbName)); +}; + +var openDatabase_v22 = function(dbName, cb) { + var database = new Database(this._cluster.openDatabase(dbName)); + return future.resolve(database)(cb); +}; + +var Cluster = function(_cluster) { + this._cluster = _cluster; + this.options = _cluster.options; + + if(apiVersion.value < 23) + this.openDatabase = openDatabase_v22; + else + this.openDatabase = openDatabase; +}; + +module.exports = Cluster; + diff --git a/bindings/nodejs/lib/database.js b/bindings/nodejs/lib/database.js new file mode 100644 index 0000000000..9186895a0b --- /dev/null +++ b/bindings/nodejs/lib/database.js @@ -0,0 +1,179 @@ +/* + * database.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var Transaction = require('./transaction'); +var future = require('./future'); +var fdb = require('./fdbModule'); +var fdbUtil = require('./fdbUtil'); +var apiVersion = require('./apiVersion'); + +var onError = function(tr, err, func, cb) { + tr.onError(err, function(retryErr, retryRes) { + if(retryErr) + cb(retryErr, retryRes); + else + retryLoop(tr, func, cb); + }); +}; + +var retryLoop = function(tr, func, cb) { + func(tr, function(err, res) { + if(err) { + onError(tr, err, func, cb); + } + else { + tr.commit(function(commitErr, commitRes) { + if(commitErr) + onError(tr, commitErr, func, cb); + else + cb(commitErr, res); + }); + } + }); +}; + +var atomic = function(db, op) { + return function(key, value, cb) { + return db.doTransaction(function(tr, innerCb) { + fdb.atomic[op].call(tr.tr, fdbUtil.keyToBuffer(key), fdbUtil.valueToBuffer(value)); + innerCb(); + }, cb); + }; +}; + +var Database = function(_db) { + this._db = _db; + this.options = _db.options; + + for(var op in fdb.atomic) + this[op] = atomic(this, op); +}; + +Database.prototype.createTransaction = function() { + return new Transaction(this, this._db.createTransaction()); +}; + +Database.prototype.doTransaction = function(func, cb) { + var tr = this.createTransaction(); + + return future.create(function(futureCb) { + retryLoop(tr, func, futureCb); + }, cb); +}; + +Database.prototype.get = function(key, cb) { + return this.doTransaction(function(tr, innerCb) { + tr.get(key, innerCb); + }, cb); +}; + +Database.prototype.getKey = function(keySelector, cb) { + return this.doTransaction(function(tr, innerCb) { + tr.getKey(keySelector, innerCb); + }, cb); +}; + +Database.prototype.getRange = function(start, end, options, cb) { + return this.doTransaction(function(tr, innerCb) { + tr.getRange(start, end, options).toArray(innerCb); + }, cb); +}; + +Database.prototype.getRangeStartsWith = function(prefix, options, cb) { + return this.doTransaction(function(tr, innerCb) { + try { + tr.getRangeStartsWith(prefix, options).toArray(innerCb); + } + catch(e) { + innerCb(e); + } + }, cb); +}; + +Database.prototype.getAndWatch = function(key, cb) { + return this.doTransaction(function(tr, innerCb) { + tr.get(key, function(err, val) { + if(err) + innerCb(err); + else + innerCb(undefined, { value: val, watch: tr.watch(key) }); + }); + }, cb); +}; + +Database.prototype.setAndWatch = function(key, value, cb) { + return this.doTransaction(function(tr, innerCb) { + tr.set(key, value); + var watchObj = tr.watch(key); + if(apiVersion.value >= 200) + innerCb(undefined, { watch: watchObj }); + else + innerCb(undefined, watchObj); + }, cb); +}; + +Database.prototype.clearAndWatch = function(key, cb) { + return this.doTransaction(function(tr, innerCb) { + tr.clear(key); + var watchObj = tr.watch(key); + if(apiVersion.value >= 200) + innerCb(undefined, { watch: watchObj }); + else + innerCb(undefined, watchObj); + }, cb); +}; + +Database.prototype.set = function(key, value, cb) { + return this.doTransaction(function(tr, innerCb) { + tr.set(key, value); + innerCb(); + }, cb); +}; + +Database.prototype.clear = function(key, cb) { + return this.doTransaction(function(tr, innerCb) { + tr.clear(key); + innerCb(); + }, cb); +}; + +Database.prototype.clearRange = function(start, end, cb) { + return this.doTransaction(function(tr, innerCb) { + tr.clearRange(start, end); + innerCb(); + }, cb); +}; + +Database.prototype.clearRangeStartsWith = function(prefix, cb) { + return this.doTransaction(function(tr, innerCb) { + try { + tr.clearRangeStartsWith(prefix); + innerCb(); + } + catch(e) { + innerCb(e); + } + }, cb); +}; + +module.exports = Database; diff --git a/bindings/nodejs/lib/directory.js b/bindings/nodejs/lib/directory.js new file mode 100644 index 0000000000..0ff16839ac --- /dev/null +++ b/bindings/nodejs/lib/directory.js @@ -0,0 +1,838 @@ +/* + * directory.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/************* + * Utilities * + *************/ + +function whileLoop(func, cb) { + return future.create(function(futureCb) { + fdbUtil.whileLoop(function(f) { + func()(f); + }, futureCb); + }, cb); +} + +function startsWith(str, prefix) { + return str.length >= prefix.length && fdbUtil.buffersEqual(str.slice(0, prefix.length), prefix); +} + +function valueOrDefault(value, defaultValue) { + if(typeof value === 'undefined') + return defaultValue; + + return value; +} + +function pathsEqual(path1, path2) { + if(path1.length !== path2.length) + return false; + + for(var i = 0; i < path1.length; ++i) + if(path1[i] !== path2[i]) + return false; + + return true; +} + +function tuplifyPath(path) { + if(!(path instanceof Array)) + path = [path]; + + return path; +} + +function checkLayer(layer, required) { + if(layer && layer.length > 0 && !fdbUtil.buffersEqual(layer, required)) + throw new Error('The directory was created with an incompatible layer.'); +} + +/*************************** + * HighContentionAllocator * + ***************************/ + +var HighContentionAllocator = function(subspace) { + this.counters = subspace.subspace([0]); + this.recent = subspace.subspace([1]); +}; + +HighContentionAllocator.prototype.allocate = transactional(function(tr) { + var self = this; + var windowStart = 0; + return whileLoop(function() { + return tr.snapshot.getRange(self.counters.range().begin, self.counters.range().end, { limit: 1, reverse: true }) + .toArray() + .then(function(arr) { + if(arr.length > 0) { + windowStart = self.counters.unpack(arr[0].key)[0]; + } + }) + .then(function() { + return self.chooseWindow(tr, windowStart); + }) + .then(function(window) { + return self.choosePrefix(tr, window); + }) + .then(function(prefix) { + if(prefix !== null) { + prefix = tuple.pack([prefix]); // exit the loop + return prefix; + } + }); + }) +}); + +HighContentionAllocator.prototype.chooseWindow = function(tr, windowStart) { + var self = this; + + var increment = new Buffer(8); + increment.fill(0); + increment.writeUInt32LE(1, 0); + + var window = { start: windowStart, size: 0 }; + + return whileLoop(function() { + // Cannot yield to event loop in this block { + if(window.start > windowStart) { + tr.clearRange(self.counters, self.counters.get(window.start)); + tr.options.setNextWriteNoWriteConflictRange(); + tr.clearRange(self.recent, self.recent.get(window.start)); + } + + tr.add(self.counters.pack([window.start]), increment); + return tr.snapshot.get(self.counters.get(window.start)) + // } + .then(function(newCountBuffer) { + var newCount = (newCountBuffer === null) ? 0 : newCountBuffer.readUInt32LE(0); + window.size = windowSize(window.start); + if(newCount * 2 < window.size) { + return window; // exit the loop + } + + window.start += window.size; + }); + }); +}; + +HighContentionAllocator.prototype.choosePrefix = function(tr, window) { + var self = this; + + return whileLoop(function() { + var candidate = Math.floor(Math.random() * window.size) + window.start; + var allocationKey = self.recent.pack([candidate]); + + // Cannot yield to event loop in this block { + var counterRange = tr.snapshot.getRange(self.counters.range().begin, self.counters.range().end, { limit: 1, reverse: true }).toArray(); + var allocation = tr.get(allocationKey); + tr.options.setNextWriteNoWriteConflictRange(); + tr.set(allocationKey, buffer('')); + // } + + return future.all([counterRange, allocation]) + .then(function(vals) { + var currentWindowStart = vals[0].length > 0 ? self.counters.unpack(vals[0][0].key)[0] : 0; + if(currentWindowStart > window.start) { + return null; // exit the loop and force find() to retry + } + if(vals[1] === null) { + tr.addWriteConflictKey(allocationKey); + return candidate; // exit the loop + } + }); + }); +}; + +function windowSize(start) { + if(start < 255) + return 64; + if(start < 65535) + return 1024; + + return 8192; +} + +/****************** + * DirectoryLayer * +******************/ + +var VERSION = [1, 0, 0]; +var SUBDIRS = 0; + +var DirectoryLayer = function(options) { + options = valueOrDefault(options, {}); + + this._nodeSubspace = valueOrDefault(options.nodeSubspace, new Subspace([], buffer.fromByteLiteral('\xfe'))); + + // If specified, new automatically allocated prefixes will all fall within the contentSubspace + this._contentSubspace = valueOrDefault(options.contentSubspace, new Subspace()); + this._allowManualPrefixes = valueOrDefault(options.allowManualPrefixes, false); + + this._rootNode = this._nodeSubspace.subspace([this._nodeSubspace.key()]); + this._allocator = new HighContentionAllocator(this._rootNode.subspace([buffer('hca')])); + + this._path = []; +}; + +var createOrOpen = transactional(function(tr, self, path, options, allowCreate, allowOpen, cb) { + options = valueOrDefault(options, {}); + var layer = valueOrDefault(options.layer, buffer('')); + var prefix = options.prefix; + + allowCreate = valueOrDefault(allowCreate, true); + allowOpen = valueOrDefault(allowOpen, true); + + return checkVersion(self, tr, false) + .then(function() { + if(typeof prefix !== 'undefined') { + if(allowCreate && allowOpen) + throw new Error('Cannot specify a prefix when calling create_or_open.'); + else if(!self._allowManualPrefixes) { + if(self._path.length === 0) + throw new Error('Cannot specify a prefix unless manual prefixes are enabled.'); + else + throw new Error('Cannot specify a prefix in a partition.'); + } + } + + path = toUnicodePath(path); + if(path.length === 0) + throw new Error('The root directory cannot be opened.'); + + return find(self, tr, path).then(loadMetadata(tr)); + }) + .then(function(existingNode) { + if(existingNode.exists()) { + if(existingNode.isInPartition(false)) { + var subpath = existingNode.getPartitionSubpath(); + var directoryLayer = existingNode.getContents(self)._directoryLayer; + return createOrOpen(tr, + existingNode.getContents(self)._directoryLayer, + subpath, + options, + allowCreate, + allowOpen); + } + + return openDirectory(tr, self, path, layer, existingNode, allowOpen); + } + else + return createDirectory(tr, self, path, layer, prefix, allowCreate); + })(cb); +}); + +var openDirectory = function(tr, self, path, layer, existingNode, allowOpen) { + if(!allowOpen) + throw new Error('The directory already exists.'); + + checkLayer(layer, existingNode.layer); + + return existingNode.getContents(self); +}; + +var createDirectory = function(tr, self, path, layer, prefix, allowCreate) { + if(!allowCreate) + throw new Error('The directory does not exist.'); + + var prefixIsAllocated = typeof(prefix) === 'undefined'; + return checkVersion(self, tr, true) + .then(function() { + return getPrefix(self, tr, prefix); + }) + .then(function(prefix) { + return isPrefixFree(self, prefixIsAllocated ? tr.snapshot : tr, prefix) + .then(function(isFree) { + if(!isFree) { + if(prefixIsAllocated) + throw new Error('The directory layer has manually allocated prefixes that conflict with the automatic prefix allocator.'); + else + throw new Error('The given prefix is already in use.'); + } + + return getParentNode(self, tr, path); + }) + .then(function(parentNode) { + if(!parentNode) + throw new Error('The parent directory doesn\'t exist.'); + + var node = nodeWithPrefix(self, prefix); + tr.set(parentNode.subspace([SUBDIRS]).pack([path[path.length-1]]), prefix); + tr.set(node.pack([buffer('layer')]), layer); + + return contentsOfNode(self, node, path, layer); + }); + }); +}; + +DirectoryLayer.prototype.getLayer = function() { + return new Buffer(0); +}; + +DirectoryLayer.prototype.getPath = function() { + return this._path.slice(0); +}; + +DirectoryLayer.prototype.createOrOpen = function(databaseOrTransaction, path, options, cb) { + return createOrOpen(databaseOrTransaction, this, path, options, true, true, cb); +}; + +DirectoryLayer.prototype.open = function(databaseOrTransaction, path, options, cb) { + return createOrOpen(databaseOrTransaction, this, path, options, false, true, cb); +}; + +DirectoryLayer.prototype.create = function(databaseOrTransaction, path, options, cb) { + return createOrOpen(databaseOrTransaction, this, path, options, true, false, cb); +}; + +DirectoryLayer.prototype.moveTo = function(databaseOrTransaction, newAbsolutePath, cb) { + return future.reject(new Error('The root directory cannot be moved.'))(cb); +}; + +DirectoryLayer.prototype.move = transactional(function(tr, oldPath, newPath, cb) { + var self = this; + var oldNode, newNode; + + return checkVersion(self, tr, true) + .then(function() { + oldPath = toUnicodePath(oldPath); + newPath = toUnicodePath(newPath); + + if(pathsEqual(oldPath, newPath.slice(0, oldPath.length))) + throw new Error('The destination directory cannot be a subdirectory of the source directory.'); + + var oldNodeFuture = find(self, tr, oldPath).then(loadMetadata(tr)); + var newNodeFuture = find(self, tr, newPath).then(loadMetadata(tr)); + return future.all([oldNodeFuture, newNodeFuture]); + }) + .then(function(nodes) { + oldNode = nodes[0]; + newNode = nodes[1]; + + if(!oldNode.exists()) + throw new Error('The source directory does not exist.'); + + if(oldNode.isInPartition(false) || newNode.isInPartition(false)) { + if(!oldNode.isInPartition(false) || !newNode.isInPartition(false) || !pathsEqual(oldNode.path, newNode.path)) + throw new Error('Cannot move between partitions.'); + + return newNode.getContents(self).move(tr, oldNode.getPartitionSubpath(), newNode.getPartitionSubpath()); + } + + if(newNode.exists()) + throw new Error('The destination directory already exists. Remove it first.'); + + return find(self, tr, newPath.slice(0, newPath.length-1)) + .then(function(parentNode) { + if(!parentNode.exists()) + throw new Error('The parent of the destination directory does not exist. Create it first.'); + + tr.set(parentNode.subspace.subspace([SUBDIRS]).pack([newPath[newPath.length-1]]), + self._nodeSubspace.unpack(oldNode.subspace.key())[0]); + + return removeFromParent(self, tr, oldPath); + }) + .then(function() { + return contentsOfNode(self, oldNode.subspace, newPath, oldNode.layer); + }); + })(cb); +}); + +DirectoryLayer.prototype.remove = transactional(function(tr, path, cb) { + return removeInternal(this, tr, path, true)(cb); +}); + +DirectoryLayer.prototype.removeIfExists = transactional(function(tr, path, cb) { + return removeInternal(this, tr, path, false)(cb); +}); + +function removeInternal(self, tr, path, failOnNonexistent) { + return checkVersion(self, tr, true) + .then(function() { + path = valueOrDefault(path, []); + if(path.length === 0) + return future.reject(new Error('The root directory cannot be removed.')); + + path = toUnicodePath(path); + return find(self, tr, path).then(loadMetadata(tr)); + }) + .then(function(node) { + if(!node.exists()) { + if(failOnNonexistent) + throw new Error('The directory doesn\'t exist'); + else + return false; + } + + if(node.isInPartition(false)) { + return removeInternal(node.getContents(self)._directoryLayer, + tr, + node.getPartitionSubpath(), + failOnNonexistent); + } + + return removeRecursive(self, tr, node.subspace) + .then(function() { + return removeFromParent(self, tr, path); + }). + then(function() { + return true; + }); + }); +} + +DirectoryLayer.prototype.list = transactional(function(tr, path, cb) { + var self = this; + return checkVersion(self, tr, false) + .then(function() { + path = valueOrDefault(path, []); + path = toUnicodePath(path); + + return find(self, tr, path).then(loadMetadata(tr)); + }) + .then(function(node) { + if(!node.exists()) + throw new Error('The given directory does not exist'); + + if(node.isInPartition(true)) + return node.getContents(self).list(tr, node.getPartitionSubpath()); + + var subdir = node.subspace.subspace([SUBDIRS]); + + return tr.getRange(subdir.range().begin, subdir.range().end).toArray() + .then(function(arr) { + return arr.map(function(kv) { return subdir.unpack(kv.key)[0].toString('utf8'); }); + }); + })(cb); +}); + +DirectoryLayer.prototype.exists = transactional(function(tr, path, cb) { + var self = this; + return checkVersion(self, tr, false) + .then(function() { + path = valueOrDefault(path, []); + path = toUnicodePath(path); + return find(self, tr, path).then(loadMetadata(tr)); + }) + .then(function(node) { + if(!node.exists()) + return false; + + if(node.isInPartition(false)) + return node.getContents(self).exists(tr, node.getPartitionSubpath()); + + return true; + })(cb); +}); + +// Private functions: + +function checkVersion(self, tr, writeAccess) { + return tr.get(self._rootNode.pack([buffer('version')])) + .then(function(versionBuf) { + if(!versionBuf) { + if(writeAccess) + initializeDirectory(self, tr); + + return; + } + + var version = []; + for(var i = 0; i < 3; ++i) + version.push(versionBuf.readInt32LE(4*i)); + + var dirVersion = util.format('%d.%d.%d', version[0], version[1], version[2]); + var layerVersion = util.format('%d.%d.%d', VERSION[0], VERSION[1], VERSION[2]); + + if(version[0] > VERSION[0]) { + throw new Error(util.format('Cannot load directory with version %s using directory layer %s', + dirVersion, + layerVersion)); + } + + if(version[1] > VERSION[1]) { + throw new Error(util.format('Directory with version %s is read-only when opened using directory layer %s', + dirVersion, + layerVersion)); + } + }); +} + +function initializeDirectory(self, tr) { + var versionBuf = new Buffer(12); + for(var i = 0; i < 3; ++i) + versionBuf.writeUInt32LE(VERSION[i], i*4); + + tr.set(self._rootNode.pack([buffer('version')]), versionBuf); +} + +function nodeWithPrefix(self, prefix) { + if(prefix === null) + return null; + + return self._nodeSubspace.subspace([prefix]); +} + +function find(self, tr, path) { + var pathIndex = 0; + var node = new Node(self._rootNode, [], path); + + return whileLoop(function() { + if(pathIndex === path.length) + return future.resolve(node); + + return tr.get(node.subspace.subspace([SUBDIRS]).pack([path[pathIndex++]])) + .then(function(val) { + node = new Node(nodeWithPrefix(self, val), path.slice(0, pathIndex), path); + if(!node.exists()) + return node; + return node.loadMetadata(tr) + .then(function() { + if(fdbUtil.buffersEqual(node.layer, buffer('partition'))) + return node; + }); + }); + }); +} + +function contentsOfNode(self, node, path, layer) { + var prefix = self._nodeSubspace.unpack(node.key())[0]; + + if(fdbUtil.buffersEqual(layer, buffer('partition'))) + return new DirectoryPartition(self._path.concat(path), prefix, self); + else + return new DirectorySubspace(self._path.concat(path), prefix, self, layer); +} + +function getPrefix(self, tr, prefix) { + if(typeof prefix === 'undefined') { + return self._allocator.allocate(tr) + .then(function(prefix) { + var allocated = Buffer.concat([self._contentSubspace.key(), prefix], self._contentSubspace.key().length + prefix.length); + return tr.getRangeStartsWith(allocated, { limit: 1 }) + .toArray() + .then(function(arr) { + if(arr.length > 0) + throw new Error('The database has keys stored at the prefix chosen by the automatic prefix allocator: ' + prefix); + + return allocated; + }); + }); + } + else + return future.resolve(buffer(prefix)); +} + +function getNodeContainingKey(self, tr, key) { + if(self._nodeSubspace.contains(key)) + return future.resolve(self._rootNode); + + return tr.getRange(self._nodeSubspace.range([]).begin, + self._nodeSubspace.subspace([key]).range().begin, + { limit: 1, reverse: true }) + .toArray() + .then(function(arr) { + if(arr.length > 0) { + var prevPrefix = self._nodeSubspace.unpack(arr[0].key)[0]; + if(startsWith(key, prevPrefix)) + return nodeWithPrefix(self, prevPrefix); + } + + return null; + }); +} + +function isPrefixFree(self, tr, prefix) { + if(!prefix || prefix.length === 0) + return future.resolve(false); + + return getNodeContainingKey(self, tr, prefix) + .then(function(node) { + if(node) + return false; + + return tr.getRange(self._nodeSubspace.pack([prefix]), + self._nodeSubspace.pack([fdbUtil.strinc(prefix)]), + { limit: 1 }) + .toArray() + .then(function(arr) { + return arr.length === 0; + }); + }); +} + +function getParentNode(self, tr, path) { + if(path.length > 1) { + return self.createOrOpen(tr, path.slice(0, path.length-1)) + .then(function(dir) { + return nodeWithPrefix(self, dir.key()); + }); + } + else + return future.resolve(self._rootNode); +} + +function removeFromParent(self, tr, path) { + return find(self, tr, path.slice(0, path.length-1)) + .then(function(parentNode) { + tr.clear(parentNode.subspace.subspace([SUBDIRS]).pack([path[path.length-1]])); + }); +} + +function removeRecursive(self, tr, node) { + var subdir = node.subspace([SUBDIRS]); + return tr.getRange(subdir.range().begin, subdir.range().end) + .forEach(function(kv, loopCb) { + removeRecursive(self, tr, nodeWithPrefix(self, kv.value))(loopCb); + }) + .then(function() { + tr.clearRangeStartsWith(self._nodeSubspace.unpack(node.key())[0]); + tr.clearRange(node.range().begin, node.range().end); + }); +} + +function toUnicodePath(path) { + if(Buffer.isBuffer(path) || path instanceof ArrayBuffer || path instanceof Uint8Array) + path = buffer(path).toString('utf8'); + + if(typeof path === 'string') + return [path]; + + if(path instanceof Array) { + for(var i = 0; i < path.length; ++i) { + if(Buffer.isBuffer(path[i]) || path[i] instanceof ArrayBuffer || path[i] instanceof Uint8Array) + path[i] = buffer(path[i]).toString('utf8'); + if(typeof path[i] !== 'string') + throw new TypeError('Invalid path: must be a string, Buffer, ArrayBuffer, Uint8Array, or an array of such items'); + } + + return path; + } + + throw new TypeError('Invalid path: must be a string, Buffer, ArrayBuffer, Uint8Array, or an array of such items'); +} + +/********************* + * DirectorySubspace * + *********************/ + +var DirectorySubspace = function(path, prefix, directoryLayer, layer) { + Subspace.call(this, undefined, prefix); + this._path = path; + this._directoryLayer = directoryLayer; + this._layer = layer; +}; + +DirectorySubspace.prototype = new Subspace(); +DirectorySubspace.constructor = DirectorySubspace; + +DirectorySubspace.prototype.getLayer = function() { + return this._layer; +}; + +DirectorySubspace.prototype.getPath = function() { + return this._path.slice(0); +}; + +DirectorySubspace.prototype.createOrOpen = function(databaseOrTransaction, nameOrPath, options, cb) { + var path = tuplifyPath(nameOrPath); + return this._directoryLayer.createOrOpen(databaseOrTransaction, partitionSubpath(this, path), options, cb); +}; + +DirectorySubspace.prototype.open = function(databaseOrTransaction, nameOrPath, options, cb) { + var path = tuplifyPath(nameOrPath); + return this._directoryLayer.open(databaseOrTransaction, partitionSubpath(this, path), options, cb); +}; + +DirectorySubspace.prototype.create = function(databaseOrTransaction, nameOrPath, options, cb) { + var path = tuplifyPath(nameOrPath); + return this._directoryLayer.create(databaseOrTransaction, partitionSubpath(this, path), options, cb); +}; + +DirectorySubspace.prototype.list = function(databaseOrTransaction, nameOrPath, cb) { + var path = tuplifyPath(valueOrDefault(nameOrPath, [])); + return this._directoryLayer.list(databaseOrTransaction, partitionSubpath(this, path), cb); +}; + +DirectorySubspace.prototype.move = function(databaseOrTransaction, oldNameOrPath, newNameOrPath, cb) { + var oldPath = tuplifyPath(oldNameOrPath); + var newPath = tuplifyPath(newNameOrPath); + return this._directoryLayer.move(databaseOrTransaction, + partitionSubpath(this, oldPath), + partitionSubpath(this, newPath), + cb); +}; + +DirectorySubspace.prototype.moveTo = function(databaseOrTransaction, newAbsoluteNameOrPath, cb) { + var directoryLayer; + var newAbsolutePath; + try { + directoryLayer = getLayerForPath(this, []); + newAbsolutePath = toUnicodePath(newAbsoluteNameOrPath); + var partitionPath = newAbsolutePath.slice(0, directoryLayer._path.length); + if(!pathsEqual(partitionPath, directoryLayer._path)) + throw new Error('Cannot move between partitions.'); + } + catch(err) { + return future.reject(err)(cb); + } + + return directoryLayer.move(databaseOrTransaction, + this._path.slice(directoryLayer._path.length), + newAbsolutePath.slice(directoryLayer._path.length), + cb); +}; + +DirectorySubspace.prototype.remove = function(databaseOrTransaction, nameOrPath, cb) { + var path = tuplifyPath(valueOrDefault(nameOrPath, [])); + var directoryLayer = getLayerForPath(this, path); + return directoryLayer.remove(databaseOrTransaction, partitionSubpath(this, path, directoryLayer), cb); +}; + +DirectorySubspace.prototype.removeIfExists = function(databaseOrTransaction, nameOrPath, cb) { + var path = tuplifyPath(valueOrDefault(nameOrPath, [])); + var directoryLayer = getLayerForPath(this, path); + return directoryLayer.removeIfExists(databaseOrTransaction, partitionSubpath(this, path, directoryLayer), cb); +}; + +DirectorySubspace.prototype.exists = function(databaseOrTransaction, nameOrPath, cb) { + var path = tuplifyPath(valueOrDefault(nameOrPath, [])); + var directoryLayer = getLayerForPath(this, path); + return directoryLayer.exists(databaseOrTransaction, partitionSubpath(this, path, directoryLayer), cb); +}; + +var partitionSubpath = function(directorySubspace, path, directoryLayer) { + directoryLayer = valueOrDefault(directoryLayer, directorySubspace._directoryLayer); + return directorySubspace._path.slice(directoryLayer._path.length).concat(path); +}; + +/********************** + * DirectoryPartition * + **********************/ + +var DirectoryPartition = function(path, prefix, parentDirectoryLayer) { + var directoryLayer = new DirectoryLayer({ + nodeSubspace: new Subspace(undefined, Buffer.concat([prefix, buffer.fromByteLiteral('\xfe')], prefix.length+1)), + contentSubspace: new Subspace(undefined, prefix) + }); + + directoryLayer._path = path; + DirectorySubspace.call(this, path, prefix, directoryLayer, buffer('partition')); + this._parentDirectoryLayer = parentDirectoryLayer; +}; + +DirectoryPartition.prototype = new DirectorySubspace(); +DirectoryPartition.constructor = DirectoryPartition; + +DirectoryPartition.prototype.key = function() { + throw new Error('Cannot get key for the root of a directory partition.'); +}; + +DirectoryPartition.prototype.pack = function(arr) { + throw new Error('Cannot pack keys using the root of a directory partition.'); +}; + +DirectoryPartition.prototype.unpack = function(arr) { + throw new Error('Cannot unpack keys using the root of a directory partition.'); +}; + +DirectoryPartition.prototype.range = function(arr) { + throw new Error('Cannot get range for the root of a directory partition.'); +}; + +DirectoryPartition.prototype.contains = function(key) { + throw new Error('Cannot check whether a key belongs to the root of a directory partition.'); +}; + +DirectoryPartition.prototype.get = function(name) { + throw new Error('Cannot open subspace in the root of a directory partition.'); +}; + +DirectoryPartition.prototype.subspace = function(arr) { + throw new Error('Cannot open subspace in the root of a directory partition.'); +}; + +DirectoryPartition.prototype.asFoundationDBKey = function() { + throw new Error('Cannot use the root of a directory partition as a key.'); +}; + +var getLayerForPath = function(directorySubspace, path) { + if(directorySubspace instanceof DirectoryPartition && path.length === 0) + return directorySubspace._parentDirectoryLayer; + else + return directorySubspace._directoryLayer; +}; + +/******** + * Node * + ********/ + +var Node = function(subspace, path, targetPath) { + this.subspace = subspace; + this.path = path; + this.targetPath = targetPath; +}; + +Node.prototype.exists = function() { + return typeof(this.subspace) !== 'undefined' && this.subspace !== null; +}; + +Node.prototype.loadMetadata = function(tr) { + var self = this; + if(!self.exists()) { + self.loadedMetadata = true; + return future.resolve(self); + } + + return tr.get(self.subspace.pack([buffer('layer')])) + .then(function(layer) { + self.loadedMetadata = true; + self.layer = layer; + return self; + }); +}; + +Node.prototype.ensureMetadataLoaded = function() { + if(!this.loadedMetadata) + throw new Error('Metadata for node has not been loaded'); +}; + +Node.prototype.isInPartition = function(includeEmptySubpath) { + this.ensureMetadataLoaded(); + return this.exists() && + fdbUtil.buffersEqual(this.layer, buffer('partition')) && + (includeEmptySubpath || this.targetPath.length > this.path.length); +}; + +Node.prototype.getPartitionSubpath = function() { + this.ensureMetadataLoaded(); + return this.targetPath.slice(this.path.length); +}; + +Node.prototype.getContents = function(directoryLayer) { + this.ensureMetadataLoaded(); + return contentsOfNode(directoryLayer, this.subspace, this.path, this.layer); +}; + +var loadMetadata = function(tr) { + return function(node) { + return node.loadMetadata(tr); + }; +}; + +module.exports = { directory: new DirectoryLayer(), DirectoryLayer: DirectoryLayer }; diff --git a/bindings/nodejs/lib/error.js b/bindings/nodejs/lib/error.js new file mode 100644 index 0000000000..31ae9d61b3 --- /dev/null +++ b/bindings/nodejs/lib/error.js @@ -0,0 +1,34 @@ +/* + * error.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +function FDBError(description, code) { + Error.captureStackTrace(this, this.constructor); + this.message = description; + this.code = code; +} + +FDBError.prototype = new Error(); +FDBError.constructor = FDBError; +FDBError.prototype.name = "FDBError"; // affects error messages, also needed for compatibility with older bindings + +module.exports = FDBError; diff --git a/bindings/nodejs/lib/fdb.js b/bindings/nodejs/lib/fdb.js new file mode 100644 index 0000000000..ff8fa05826 --- /dev/null +++ b/bindings/nodejs/lib/fdb.js @@ -0,0 +1,168 @@ +/* + * fdb.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var KeySelector = require('./keySelector'); +var Cluster = require('./cluster'); +var future = require('./future'); +var Transactional = require('./retryDecorator'); +var tuple = require('./tuple'); +var buffer = require('./bufferConversion'); +var fdb = require('./fdbModule'); +var FDBError = require('./error'); +var locality = require('./locality'); +var directory = require('./directory'); +var Subspace = require('./subspace'); +var selectedApiVersion = require('./apiVersion'); + +var fdbModule = {}; + +module.exports = { + FDBError: FDBError, + apiVersion: function(version) { + if(selectedApiVersion.value && version !== selectedApiVersion.value) + throw new Error('Cannot select multiple different FDB API versions'); + if(version < 14) + throw new RangeError('FDB API versions before 14 are not supported'); + if(version > 500) + throw new RangeError('Latest known FDB API version is 500'); + + if(!selectedApiVersion.value) { + fdb.apiVersion(version); + + if(version < 23) + Cluster.prototype.createDatabase = Cluster.prototype.openDatabase; + + fdbModule.FDBError = this.FDBError; + fdbModule.KeySelector = KeySelector; + fdbModule.future = future; + fdbModule.transactional = Transactional; + fdbModule.tuple = tuple; + fdbModule.buffer = buffer; + fdbModule.locality = locality; + fdbModule.directory = directory.directory; + fdbModule.DirectoryLayer = directory.DirectoryLayer; + fdbModule.Subspace = Subspace; + + fdbModule.options = fdb.options; + fdbModule.streamingMode = fdb.streamingMode; + + var dbCache = {}; + var clusterCache = {}; + + var doInit = function() { + fdb.startNetwork(); + + process.on('exit', function() { + //Clearing out the caches makes memory debugging a little easier + dbCache = null; + clusterCache = null; + + fdb.stopNetwork(); + }); + + //Subsequent calls do nothing + doInit = function() { }; + }; + + fdbModule.init = function() { + doInit(); + }; + + fdbModule.createCluster = function(clusterFile, cb) { + if(!clusterFile) + clusterFile = ''; + + var cluster = new Cluster(fdb.createCluster(clusterFile)); + + if(version < 23) + return future.resolve(cluster)(cb); + else + return cluster; + }; + + fdbModule.open = function(clusterFile, databaseName, cb) { + if(!databaseName) + databaseName = 'DB'; + + if(clusterFile) + fdb.options.setClusterFile(clusterFile); + + this.init(); + + var finish = function(err, database) { + if(version >= 23) //err will be undefined if version >= 23 + return database; + else if(err) + return future.reject(err)(cb); + else + return future.resolve(database)(cb); + }; + + var updateDatabaseCacheAndFinish = function(err, database) { + if(!err) + dbCache[[clusterFile, databaseName]] = database; + + return finish(err, database); + }; + + var getDatabase = function(cluster) { + var database = dbCache[[clusterFile, databaseName]]; + if(database) + return finish(undefined, database); + else { + database = cluster.openDatabase(databaseName); + if(version >= 23) + return updateDatabaseCacheAndFinish(undefined, database); + else + return database(updateDatabaseCacheAndFinish); + } + }; + + var updateClusterCacheAndGetDatabase = function(err, cluster) { + if(err) + finish(err); + else { + clusterCache[clusterFile] = cluster; + return getDatabase(cluster); + } + }; + + var cluster = clusterCache[clusterFile]; + if(cluster) + return getDatabase(cluster); + else { + cluster = fdbModule.createCluster(clusterFile); + if(version >= 23) + return updateClusterCacheAndGetDatabase(undefined, cluster); + else + return cluster(updateClusterCacheAndGetDatabase); + } + }; + } + + selectedApiVersion.value = version; + return fdbModule; + } +}; + +fdb.FDBError = module.exports.FDBError; diff --git a/bindings/nodejs/lib/fdbModule.js b/bindings/nodejs/lib/fdbModule.js new file mode 100644 index 0000000000..595b750127 --- /dev/null +++ b/bindings/nodejs/lib/fdbModule.js @@ -0,0 +1,33 @@ +/* + * fdbModule.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var semver = require('semver'); + +if(semver.satisfies(process.version, '0.10.x')) + var fdb = require('../modules/0.10/fdblib'); +else if(semver.satisfies(process.version, '0.8.x')) + var fdb = require('../modules/0.8/fdblib'); +else + throw new Error('FoundationDB binary NPM does not support Node.js ' + process.version + '; only v0.8.x and v0.10.x are supported'); + +module.exports = fdb; diff --git a/bindings/nodejs/lib/fdbUtil.js b/bindings/nodejs/lib/fdbUtil.js new file mode 100644 index 0000000000..c3411cd3f1 --- /dev/null +++ b/bindings/nodejs/lib/fdbUtil.js @@ -0,0 +1,20 @@ +/* + * fdbUtil.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + diff --git a/bindings/nodejs/lib/future.js b/bindings/nodejs/lib/future.js new file mode 100644 index 0000000000..61210ca6a8 --- /dev/null +++ b/bindings/nodejs/lib/future.js @@ -0,0 +1,244 @@ +/* + * future.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; +var semver = require('semver'); + +function isFunction(f) { + return typeof(f) == 'function'; +} + +function isObject(o) { + return o === Object(o); +} + +var resolvePromise = function(promise, value) { + var called = false; + try { + if(promise === value) + promise._state.reject(new TypeError('promise.then cannot be fulfilled with itself as the argument.')); + + if(isObject(value)) { + var then = value.then; + if(isFunction(then)) { + then.call(value, function(res) { + if(!called) { + called = true; + resolvePromise(promise, res, promise); + } + }, function(err) { + if(!called) { + called = true; + promise._state.reject(err); + } + }); + } + else + promise._state.fulfill(value); + } + else + promise._state.fulfill(value); + } + catch(error) { + if(!called) + promise._state.reject(error); + } +}; + +var FuturePrototype = { + cancel: function() { + //cancel is not implemented for most futures + }, + + then: function(onFulfilled, onRejected) { + var self = this; + var future = create(); + this._state.addCallback(function(err, res) { + var setImmediateFunc; + if(semver.satisfies(process.version, '>=0.10.0')) { + setImmediateFunc = setImmediate; + } + else { + setImmediateFunc = process.nextTick; + } + setImmediateFunc(function() { + try { + if(self._state.rejected) { + if(isFunction(onRejected)) + res = onRejected(err); + else { + future._state.reject(err); + return; + } + } + else if(isFunction(onFulfilled)) + res = onFulfilled(res); + + resolvePromise(future, res); + } + catch(error) { + future._state.reject(error); + } + }); + }); + + return future; + }, + + "catch": function(onRejected) { + this.then(undefined, onRejected); + } +}; + +FuturePrototype.__proto__ = Function.__proto__; + +var FutureState = function() { + this.callbacks = []; + this.fulfilled = false; + this.rejected = false; +}; + +FutureState.prototype.triggerCallbacks = function() { + for(var i = 0; i < this.callbacks.length; ++i) + this.callbacks[i](this.error, this.value); + + this.callbacks = []; +}; + +FutureState.prototype.addCallback = function(cb) { + if(!this.rejected && !this.fulfilled) + this.callbacks.push(cb); + else + cb(this.error, this.value); +}; + +FutureState.prototype.fulfill = function(value) { + if(!this.fulfilled && !this.rejected) { + this.fulfilled = true; + this.value = value; + this.triggerCallbacks(); + } +}; + +FutureState.prototype.reject = function(reason) { + if(!this.fulfilled && !this.rejected) { + this.rejected = true; + this.error = reason; + this.triggerCallbacks(); + } +}; + +var getFutureCallback = function(futureState) { + return function(err, val) { + if(err) + futureState.reject(err); + else + futureState.fulfill(val); + }; +}; + +var create = function(func, cb) { + if(cb) + func(cb); + else { + // This object is used to break a reference cycle with C++ objects + var futureState = new FutureState(); + + var future = function(callback) { + if(typeof callback === 'undefined') + return future; + + future.then(function(val) { callback(undefined, val); }, callback); + }; + + future._state = futureState; + future.__proto__ = FuturePrototype; + + if(func) + func.call(future, getFutureCallback(futureState)); + + return future; + } +}; + +var resolve = function(value) { + var f = create(); + f._state.fulfill(value); + return f; +}; + +var reject = function(reason) { + var f = create(); + f._state.reject(reason); + return f; +}; + +var all = function(futures) { + var future = create(function(futureCb) { + var count = futures.length; + + if(count === 0) + futureCb(undefined, []); + + var successCallback = function() { + if(--count === 0) + futureCb(undefined, futures.map(function(f) { return f._state.value; })); + }; + + for(var i = 0; i < futures.length; ++i) { + if(futures[i] && isFunction(futures[i].then)) + futures[i].then(successCallback, futureCb); + else + successCallback(); + } + }); + + return future; +}; + +var race = function(futures) { + var future = create(function(futureCb) { + var successCallback = function(val) { + futureCb(undefined, val); + }; + + for(var i = 0; i < futures.length; ++i) { + if(futures[i] && isFunction(futures[i].then)) + futures[i].then(successCallback, futureCb); + else { + futureCb(undefined, futures[i]); + break; + } + } + }); + + return future; +}; + +module.exports = { + FuturePrototype: FuturePrototype, + create: create, + resolve: resolve, + reject: reject, + all: all, + race: race +}; + diff --git a/bindings/nodejs/lib/keySelector.js b/bindings/nodejs/lib/keySelector.js new file mode 100644 index 0000000000..e92a6d675e --- /dev/null +++ b/bindings/nodejs/lib/keySelector.js @@ -0,0 +1,65 @@ +/* + * keySelector.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var fdbUtil = require('./fdbUtil'); + +var KeySelector = function(key, orEqual, offset) { + this.key = fdbUtil.keyToBuffer(key); + this.orEqual = orEqual; + this.offset = offset; +}; + +KeySelector.prototype.next = function() { + return this.add(1); +}; + +KeySelector.prototype.prev = function() { + return this.add(-1); +}; + +KeySelector.prototype.add = function(addOffset) { + return new KeySelector(this.key, this.orEqual, this.offset + addOffset); +}; + +KeySelector.isKeySelector = function(sel) { + return sel instanceof KeySelector; +}; + +KeySelector.lastLessThan = function(key) { + return new KeySelector(key, false, 0); +}; + +KeySelector.lastLessOrEqual = function(key) { + return new KeySelector(key, true, 0); +}; + +KeySelector.firstGreaterThan = function(key) { + return new KeySelector(key, true, 1); +}; + +KeySelector.firstGreaterOrEqual = function(key) { + return new KeySelector(key, false, 1); +}; + +module.exports = KeySelector; + diff --git a/bindings/nodejs/lib/lazyIterator.js b/bindings/nodejs/lib/lazyIterator.js new file mode 100644 index 0000000000..11ca598885 --- /dev/null +++ b/bindings/nodejs/lib/lazyIterator.js @@ -0,0 +1,182 @@ +/* + * lazyIterator.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var fdbUtil = require('./fdbUtil'); +var future = require('./future'); + +function fetch(state, cb) { + if(cb) + state.fetchCallbacks.push(cb); + if(!state.fetching) { + state.fetching = true; + state.fetcher.fetch(function(err, res) { + var cbs = state.fetchCallbacks; + state.fetching = false; + state.fetchCallbacks = []; + state.results = res; + state.index = -1; + state.finished = !res || res.length === 0; + + for(var i = 0; i < cbs.length; ++i) + cbs[i](err); + }); + } +} + +function iterState(fetcher) { + return { + index: -1, + results: undefined, + + fetching: false, + fetchCallbacks: [], + fetcher: fetcher + }; +} + +var LazyIterator = function(Fetcher) { + this.Fetcher = Fetcher; + this.stateForNext = undefined; + + this.startState = iterState(new Fetcher()); + + var startState = this.startState; + fetch(this.startState); +}; + +function copyState(state, wantAll) { + var newState = iterState(); + newState.index = state.index; + newState.results = state.results; + newState.fetching = state.fetching; + + if(state.fetching) { + state.fetchCallbacks.push(function(err) { + var cbs = newState.fetchCallbacks; + newState.index = state.index; + newState.results = state.results; + newState.fetching = false; + newState.fetchCallbacks = []; + newState.finished = state.finished; + newState.fetcher = state.fetcher.clone(wantAll); + for(var i = 0; i < cbs.length; ++i) + cbs[i](err); + }); + } + else { + newState.fetcher = state.fetcher.clone(wantAll); + } + + return newState; +} + +function nextImpl(state, cb) { + if(state.finished) + cb(); + else if(state.results && (state.index + 1) < state.results.length) + cb(null, state.results[++state.index]); + else { + fetch(state, function(err) { + if(err) + cb(err); + else if(state.finished) + cb(); + else + nextImpl(state, cb); + }); + } +} + +LazyIterator.prototype.next = function(cb) { + var itr = this; + return future.create(function(futureCb) { + if(!itr.stateForNext) + itr.stateForNext = copyState(itr.startState); + + nextImpl(itr.stateForNext, futureCb); + }, cb); +}; + +LazyIterator.prototype.forEach = function(func, cb) { + var itr = this; + return future.create(function(futureCb) { + var state = copyState(itr.startState); + + fdbUtil.whileLoop(function(loopCb) { + nextImpl(state, function(err, res) { + if(err || !res) + loopCb(err, null); + else + func(res, loopCb); + }); + }, futureCb); + + }, cb); +}; + +function forEachBatchImpl(state, func, cb) { + function loopBody(loopCb) { + function processBatch(err) { + if(err || state.finished) + loopCb(err, null); + else { + state.index = state.results.length; + func(state.results, loopCb); + } + } + + if(!state.results || state.index === state.results.length) + fetch(state, processBatch); + else + processBatch(); + } + + fdbUtil.whileLoop(loopBody, cb); +} + +LazyIterator.prototype.forEachBatch = function(func, cb) { + var itr = this; + return future.create(function(futureCb) { + forEachBatchImpl(copyState(itr.startState), func, futureCb); + }, cb); +}; + +LazyIterator.prototype.toArray = function(cb) { + var itr = this; + return future.create(function(futureCb) { + var state = copyState(itr.startState, true); + var result = []; + + forEachBatchImpl(state, function(arr, itrCb) { + result = result.concat(arr); + itrCb(); + }, function(err, res) { + if(err) + futureCb(err); + else + futureCb(null, result); + }); + }, cb); +}; + +module.exports = LazyIterator; diff --git a/bindings/nodejs/lib/locality.js b/bindings/nodejs/lib/locality.js new file mode 100644 index 0000000000..84f1629196 --- /dev/null +++ b/bindings/nodejs/lib/locality.js @@ -0,0 +1,131 @@ +/* + * locality.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var buffer = require('./bufferConversion'); +var transactional = require('./retryDecorator'); +var Database = require('./database'); +var LazyIterator = require('./lazyIterator'); +var fdb = require('./fdbModule'); +var fdbUtil = require('./fdbUtil'); + +var KEY_SERVERS_PREFIX = buffer.fromByteLiteral('\xff/keyServers/'); +var PAST_VERSION_ERROR_CODE = 1007; + +function getBoundaryKeysImpl(tr, begin, end, callback) { + function BoundaryFetcher(wantAll) { + this.tr = tr; + this.begin = begin; + this.end = end; + this.lastBegin = undefined; + + var fetcher = this; + + if(wantAll) + this.streamingMode = fdb.streamingMode.wantAll; + else + this.streamingMode = fdb.streamingMode.iterator; + + function iteratorCb(err, res) { + if(err) { + if(err.code === PAST_VERSION_ERROR_CODE && fetcher.begin !== fetcher.lastBegin) { + fetcher.tr = fetcher.tr.db.createTransaction(); + readKeys(); + } + else { + fetcher.tr.onError(err, function(e) { + if(e) + fetcher.fetchCb(e); + else + readKeys(); + }); + } + } + else + fetcher.fetchCb(); + } + + function readKeys() { + fetcher.lastBegin = fetcher.begin; + fetcher.tr.options.setReadSystemKeys(); + fetcher.tr.options.setLockAware(); + fetcher.tr.snapshot.getRange(fetcher.begin, fetcher.end, {streamingMode: fetcher.streamingMode}).forEachBatch(function(kvs, innerCb) { + fetcher.forEachCb = innerCb; + var keys = kvs.map(function(kv) { return kv.key.slice(13); }); + var last = kvs[kvs.length-1].key; + fetcher.begin = Buffer.concat([last, buffer.fromByteLiteral('\x00')], last.length + 1); + fetcher.fetchCb(undefined, keys); + }, iteratorCb); + + fetcher.streamingMode = fdb.streamingMode.wantAll; + } + + this.fetch = function(cb) { + this.fetchCb = cb; + if(this.read) + this.forEachCb(); + else { + this.read = true; + readKeys(); + } + }; + + this.clone = function(wantAll) { + var clone = new BoundaryFetcher(wantAll); + + clone.tr = this.tr.db.createTransaction(); + clone.begin = this.begin; + clone.end = this.end; + clone.lastBegin = this.lastBegin; + + return clone; + }; + } + + callback(null, new LazyIterator(BoundaryFetcher)); +} + +function getBoundaryKeys(databaseOrTransaction, begin, end, callback) { + begin = fdbUtil.keyToBuffer(begin); + end = fdbUtil.keyToBuffer(end); + + begin = Buffer.concat([KEY_SERVERS_PREFIX, begin], KEY_SERVERS_PREFIX.length + begin.length); + end = Buffer.concat([KEY_SERVERS_PREFIX, end], KEY_SERVERS_PREFIX.length + end.length); + + if(databaseOrTransaction instanceof Database) { + getBoundaryKeysImpl(databaseOrTransaction.createTransaction(), begin, end, callback); + } + else { + var tr = databaseOrTransaction.db.createTransaction(); + databaseOrTransaction.getReadVersion(function(err, ver) { + tr.setReadVersion(ver); + getBoundaryKeysImpl(tr, begin, end, callback); + }); + } +} + +var getAddressesForKey = transactional(function (tr, key, cb) { + key = fdbUtil.keyToBuffer(key); + tr.tr.getAddressesForKey(key, cb); +}); + +module.exports = {getBoundaryKeys: getBoundaryKeys, getAddressesForKey: getAddressesForKey}; diff --git a/bindings/nodejs/lib/rangeIterator.js b/bindings/nodejs/lib/rangeIterator.js new file mode 100644 index 0000000000..4141204ac3 --- /dev/null +++ b/bindings/nodejs/lib/rangeIterator.js @@ -0,0 +1,106 @@ +/* + * rangeIterator.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var KeySelector = require('./keySelector'); +var Future = require('./future'); +var fdb = require('./fdbModule'); +var LazyIterator = require('./lazyIterator'); + +function getStreamingMode(requestedMode, limit, wantAll) { + if(wantAll && requestedMode === fdb.streamingMode.iterator) { + if(limit) + return fdb.streamingMode.exact; + else + return fdb.streamingMode.wantAll; + } + + return requestedMode; +} + +module.exports = function(tr, start, end, options, snapshot) { + if(!options) + options = {}; + + if(!options.limit) + options.limit = 0; + if(!options.reverse) + options.reverse = false; + if(!options.streamingMode && options.streamingMode !== 0) + options.streamingMode = fdb.streamingMode.iterator; + + var RangeFetcher = function(wantAll) { + this.finished = false; + this.limit = options.limit; + this.iterStart = start; + this.iterEnd = end; + this.iterationCount = 1; + this.streamingMode = getStreamingMode(options.streamingMode, this.limit, wantAll); + }; + + RangeFetcher.prototype.clone = function(wantAll) { + var clone = new RangeFetcher(wantAll); + + clone.finished = this.finished; + clone.limit = this.limit; + clone.iterStart = this.iterStart; + clone.iterEnd = this.iterEnd; + clone.iterationCount = this.iterationCount; + + return clone; + }; + + RangeFetcher.prototype.fetch = function(cb) { + var fetcher = this; + if(fetcher.finished) { + cb(); + } + else { + tr.getRange(fetcher.iterStart.key, fetcher.iterStart.orEqual, fetcher.iterStart.offset, fetcher.iterEnd.key, fetcher.iterEnd.orEqual, fetcher.iterEnd.offset, fetcher.limit, fetcher.streamingMode, fetcher.iterationCount++, snapshot, options.reverse, function(err, res) + { + if(!err) { + var results = res.array; + if(results.length > 0) { + if(!options.reverse) + fetcher.iterStart = KeySelector.firstGreaterThan(results[results.length-1].key); + else + fetcher.iterEnd = KeySelector.firstGreaterOrEqual(results[results.length-1].key); + } + + if(fetcher.limit !== 0) { + fetcher.limit -= results.length; + if(fetcher.limit <= 0) + fetcher.finished = true; + } + if(!res.more) + fetcher.finished = true; + cb(undefined, results); + } + else { + cb(err); + } + }); + } + }; + + return new LazyIterator(RangeFetcher); +}; diff --git a/bindings/nodejs/lib/retryDecorator.js b/bindings/nodejs/lib/retryDecorator.js new file mode 100644 index 0000000000..6983d4483e --- /dev/null +++ b/bindings/nodejs/lib/retryDecorator.js @@ -0,0 +1,50 @@ +/* + * retryDecorator.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var Database = require('./database'); +var Transaction = require('./transaction'); + +module.exports = function(func) { + return function(db) { + var self = this; + + if(db instanceof Database) { + var cb = arguments[func.length - 1]; + + if(typeof cb !== "undefined" && !(cb instanceof Function)) + throw new TypeError("fdb.transactional function must declare a callback function as last argument"); + else { + var args = Array.prototype.slice.call(arguments); + return db.doTransaction(function(tr, innerCb) { + args[0] = tr; + args[func.length - 1] = innerCb; + func.apply(self, args); + })(cb); + } + } + else if(db instanceof Transaction || db instanceof Transaction.SnapshotTransaction) + return func.apply(self, arguments); + else + throw new TypeError("fdb.transactional function must pass a Database, Transaction, or SnapshotTransaction as first argument"); + }; +}; diff --git a/bindings/nodejs/lib/subspace.js b/bindings/nodejs/lib/subspace.js new file mode 100644 index 0000000000..b92b6baaa2 --- /dev/null +++ b/bindings/nodejs/lib/subspace.js @@ -0,0 +1,20 @@ +/* + * subspace.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + diff --git a/bindings/nodejs/lib/transaction.js b/bindings/nodejs/lib/transaction.js new file mode 100644 index 0000000000..5f29f28395 --- /dev/null +++ b/bindings/nodejs/lib/transaction.js @@ -0,0 +1,203 @@ +/* + * transaction.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var future = require('./future'); +var KeySelector = require('./keySelector'); +var rangeIterator = require('./rangeIterator'); +var buffer = require('./bufferConversion'); +var FDBError = require('./error'); +var fdb = require('./fdbModule'); +var fdbUtil = require('./fdbUtil'); + +function addReadOperations(object, snapshot) { + object.prototype.get = function(key, cb) { + var tr = this.tr; + key = fdbUtil.keyToBuffer(key); + + return future.create(function(futureCb) { + tr.get(key, snapshot, futureCb); + }, cb); + }; + + object.prototype.getKey = function(keySelector, cb) { + var tr = this.tr; + return future.create(function(futureCb) { + tr.getKey(keySelector.key, keySelector.orEqual, keySelector.offset, snapshot, futureCb); + }, cb); + }; + + object.prototype.getRange = function(start, end, options) { + if(!KeySelector.isKeySelector(start)) + start = KeySelector.firstGreaterOrEqual(start); + if(!KeySelector.isKeySelector(end)) + end = KeySelector.firstGreaterOrEqual(end); + + return rangeIterator(this.tr, start, end, options, snapshot); + }; + + object.prototype.getRangeStartsWith = function(prefix, options) { + prefix = fdbUtil.keyToBuffer(prefix); + return this.getRange(prefix, fdbUtil.strinc(prefix), options, snapshot); + }; + + object.prototype.getReadVersion = function(cb) { + var tr = this.tr; + return future.create(function(futureCb) { + tr.getReadVersion(futureCb, snapshot); + }, cb); + }; +} + +var atomic = function(tr, op) { + return function(key, value) { fdb.atomic[op].call(tr, fdbUtil.keyToBuffer(key), fdbUtil.valueToBuffer(value)); }; +}; + +var Transaction = function(db, tr) { + this.db = db; + this.tr = tr; + + this.options = tr.options; + this.snapshot = new Transaction.SnapshotTransaction(tr); + + for(var op in fdb.atomic) + this[op] = atomic(tr, op); +}; + +Transaction.SnapshotTransaction = function(tr) { + this.tr = tr; +}; + +addReadOperations(Transaction, false); +addReadOperations(Transaction.SnapshotTransaction, true); + +Transaction.prototype.doTransaction = function(func, cb) { + var self = this; + return future.create(function(futureCb) { + func(self, futureCb); + }, cb); +}; + +Transaction.prototype.set = function(key, value) { + key = fdbUtil.keyToBuffer(key); + value = fdbUtil.valueToBuffer(value); + + this.tr.set(key, value); +}; + +Transaction.prototype.clear = function(key) { + key = fdbUtil.keyToBuffer(key); + + this.tr.clear(key); +}; + +Transaction.prototype.clearRange = function(start, end) { + start = fdbUtil.keyToBuffer(start); + end = fdbUtil.keyToBuffer(end); + + this.tr.clearRange(start, end); +}; + +Transaction.prototype.clearRangeStartsWith = function(prefix) { + prefix = fdbUtil.keyToBuffer(prefix); + this.clearRange(prefix, fdbUtil.strinc(prefix)); +}; + +Transaction.prototype.watch = function(key) { + key = fdbUtil.keyToBuffer(key); + + var self = this; + var watchFuture = future.create(function(futureCb) { + // 'this' is the future that is being created. + // We set its cancel method to cancel the watch. + this._watch = self.tr.watch(key, futureCb); + this.cancel = function() { this._watch.cancel(); }; + }); + + return watchFuture; +}; + +Transaction.prototype.addReadConflictRange = function(start, end) { + start = fdbUtil.keyToBuffer(start); + end = fdbUtil.keyToBuffer(end); + this.tr.addReadConflictRange(start, end); +}; + +Transaction.prototype.addReadConflictKey = function(key) { + key = fdbUtil.keyToBuffer(key); + this.tr.addReadConflictRange(key, Buffer.concat([key, buffer.fromByteLiteral('\x00')], key.length + 1)); +}; + +Transaction.prototype.addWriteConflictRange = function(start, end) { + start = fdbUtil.keyToBuffer(start); + end = fdbUtil.keyToBuffer(end); + + this.tr.addWriteConflictRange(start, end); +}; + +Transaction.prototype.addWriteConflictKey = function(key) { + key = fdbUtil.keyToBuffer(key); + this.tr.addWriteConflictRange(key, Buffer.concat([key, buffer.fromByteLiteral('\x00')], key.length + 1)); +}; + +Transaction.prototype.commit = function(cb) { + var tr = this.tr; + return future.create(function(futureCb) { + tr.commit(futureCb); + }, cb); +}; + +Transaction.prototype.onError = function(fdbError, cb) { + var tr = this.tr; + return future.create(function(futureCb) { + if(fdbError instanceof FDBError) + tr.onError(fdbError.code, futureCb); + else + futureCb(fdbError, null); + }, cb); +}; + +Transaction.prototype.reset = function() { + this.tr.reset(); +}; + +Transaction.prototype.setReadVersion = function(version) { + this.tr.setReadVersion(version); +}; + +Transaction.prototype.getCommittedVersion = function() { + return this.tr.getCommittedVersion(); +}; + +Transaction.prototype.getVersionstamp = function(cb) { + var tr = this.tr; + return future.create(function(futureCb) { + tr.getVersionstamp(futureCb); + }, cb); +}; + +Transaction.prototype.cancel = function() { + this.tr.cancel(); +}; + +module.exports = Transaction; + diff --git a/bindings/nodejs/lib/tuple.js b/bindings/nodejs/lib/tuple.js new file mode 100644 index 0000000000..aaed94ac62 --- /dev/null +++ b/bindings/nodejs/lib/tuple.js @@ -0,0 +1,255 @@ +/* + * tuple.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +"use strict"; + +var assert = require('assert'); +var buffer = require('./bufferConversion'); +var fdbUtil = require('./fdbUtil'); + +var sizeLimits = new Array(8); + +function setupSizeLimits() { + sizeLimits[0] = 1; + for(var i = 1; i < sizeLimits.length; i++) { + sizeLimits[i] = sizeLimits[i-1] * 256; + sizeLimits[i-1] -= 1; + } + sizeLimits[7] -= 1; +} + +setupSizeLimits(); + +var maxInt = Math.pow(2, 53) - 1; +var minInt = -Math.pow(2, 53); + +var nullByte = new Buffer('00', 'hex'); + +function findNullBytes(buf, pos, searchForTerminators) { + var nullBytes = []; + + var found; + for(pos; pos < buf.length; ++pos) { + if(searchForTerminators && found && buf[pos] !== 255) { + break; + } + + found = false; + if(buf[pos] === 0) { + found = true; + nullBytes.push(pos); + } + } + + if(!found && searchForTerminators) { + nullBytes.push(buf.length); + } + + return nullBytes; +} + +function encode(item, buf, pos) { + var encodedString; + if(typeof item === 'undefined') + throw new TypeError('Packed element cannot be undefined'); + + else if(item === null) + return nullByte; + + //byte string or unicode + else if(Buffer.isBuffer(item) || item instanceof ArrayBuffer || item instanceof Uint8Array || typeof item === 'string') { + var unicode = typeof item === 'string'; + + if(unicode) { + item = new Buffer(item, 'utf8'); + } + else { + item = buffer(item); + } + + var nullBytes = findNullBytes(item, 0); + + encodedString = new Buffer(2 + item.length + nullBytes.length); + encodedString[0] = unicode ? 2 : 1; + + var srcPos = 0; + var targetPos = 1; + for(var i = 0; i < nullBytes.length; ++i) { + item.copy(encodedString, targetPos, srcPos, nullBytes[i]+1); + targetPos += nullBytes[i]+1 - srcPos; + srcPos = nullBytes[i]+1; + encodedString[targetPos++] = 255; + } + + item.copy(encodedString, targetPos, srcPos); + encodedString[encodedString.length-1] = 0; + + return encodedString; + } + + //64-bit integer + else if(item % 1 === 0) { + var negative = item < 0; + var posItem = Math.abs(item); + + var length = 0; + for(; length < sizeLimits.length; ++length) { + if(posItem <= sizeLimits[length]) + break; + } + + if(item > maxInt || item < minInt) + throw new RangeError('Cannot pack signed integer larger than 54 bits'); + + var prefix = negative ? 20 - length : 20 + length; + + var outBuf = new Buffer(length+1); + outBuf[0] = prefix; + for(var byteIdx = length-1; byteIdx >= 0; --byteIdx) { + var b = posItem & 0xff; + if(negative) + outBuf[byteIdx+1] = ~b; + else { + outBuf[byteIdx+1] = b; + } + + posItem = (posItem - b) / 0x100; + } + + return outBuf; + } + + else + throw new TypeError('Packed element must either be a string, a buffer, an integer, or null'); +} + +function pack(arr) { + if(!(arr instanceof Array)) + throw new TypeError('fdb.tuple.pack must be called with a single array argument'); + + var totalLength = 0; + + var outArr = []; + for(var i = 0; i < arr.length; ++i) { + outArr.push(encode(arr[i])); + totalLength += outArr[i].length; + } + + return Buffer.concat(outArr, totalLength); +} + +function decodeNumber(buf, offset, bytes) { + var negative = bytes < 0; + bytes = Math.abs(bytes); + + var num = 0; + var mult = 1; + var odd; + for(var i = bytes-1; i >= 0; --i) { + var b = buf[offset+i]; + if(negative) + b = -(~b & 0xff); + + if(i == bytes-1) + odd = b & 0x01; + + num += b * mult; + mult *= 0x100; + } + + if(num > maxInt || num < minInt || (num === minInt && odd)) + throw new RangeError('Cannot unpack signed integers larger than 54 bits'); + + return num; +} + +function decode(buf, pos) { + var code = buf[pos]; + var value; + + if(code === 0) { + value = null; + pos++; + } + else if(code === 1 || code === 2) { + var nullBytes = findNullBytes(buf, pos+1, true); + + var start = pos+1; + var end = nullBytes[nullBytes.length-1]; + + if(code === 2 && nullBytes.length === 1) { + value = buf.toString('utf8', start, end); + } + else { + value = new Buffer(end-start-(nullBytes.length-1)); + var valuePos = 0; + + for(var i=0; i < nullBytes.length && start < end; ++i) { + buf.copy(value, valuePos, start, nullBytes[i]); + valuePos += nullBytes[i] - start; + start = nullBytes[i] + 2; + if(start <= end) { + value[valuePos++] = 0; + } + } + + if(code === 2) + value = value.toString('utf8'); + } + + pos = end + 1; + } + else if(Math.abs(code-20) <= 7) { + if(code === 20) + value = 0; + else + value = decodeNumber(buf, pos+1, code-20); + + pos += Math.abs(20-code) + 1; + } + else if(Math.abs(code-20) <= 8) + throw new RangeError('Cannot unpack signed integers larger than 54 bits'); + else + throw new TypeError('Unknown data type in DB: ' + buf + ' at ' + pos); + + return { pos: pos, value: value }; +} + +function unpack(key) { + var res = { pos: 0 }; + var arr = []; + + key = fdbUtil.keyToBuffer(key); + + while(res.pos < key.length) { + res = decode(key, res.pos); + arr.push(res.value); + } + + return arr; +} + +function range(arr) { + var packed = pack(arr); + return { begin: Buffer.concat([packed, nullByte]), end: Buffer.concat([packed, new Buffer('ff', 'hex')]) }; +} + +module.exports = {pack: pack, unpack: unpack, range: range}; diff --git a/bindings/nodejs/package.json.in b/bindings/nodejs/package.json.in new file mode 100644 index 0000000000..a8a5fa4a55 --- /dev/null +++ b/bindings/nodejs/package.json.in @@ -0,0 +1,26 @@ +{ + "name": "@apple/fdb", + "publishConfig": { + "registry": "https://registry.npmjs.org" + }, + "version": "VERSION", + "author": "FoundationDB (http://foundationdb.org)", + "description": "Node.js bindings for the FoundationDB database", + "keywords": [ "FoundationDB", "database", "NoSQL", "ACID" ], + "homepage": "http://17.199.145.104", + "license": "Apache v2", + "main": "./lib/fdb.js", + "cpu": [ "x64" ], + "dependencies": { + "semver": "~ 4.1" + }, + "devDependencies": { + "jshint": ">= 2.5.6", + "promises-aplus-tests": ">= 2.1.0" + }, + "engineStrict": true, + "private": true, + "engines": { + "node" : "0.8.x || 0.10.x" + } +} diff --git a/bindings/nodejs/src/Cluster.cpp b/bindings/nodejs/src/Cluster.cpp new file mode 100644 index 0000000000..0a6a5c10f5 --- /dev/null +++ b/bindings/nodejs/src/Cluster.cpp @@ -0,0 +1,96 @@ +/* + * Cluster.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include +#include +#include +#include + +#include "Cluster.h" +#include "Database.h" +#include "FdbOptions.h" +#include "NodeCallback.h" + +using namespace v8; +using namespace std; + +Cluster::Cluster() { } +Cluster::~Cluster() { + fdb_cluster_destroy(cluster); +} + +Persistent Cluster::constructor; + +Handle Cluster::OpenDatabase(const Arguments &args) { + HandleScope scope; + + Cluster *clusterPtr = ObjectWrap::Unwrap(args.Holder()); + + std::string dbName = *String::AsciiValue(args[0]->ToString()); + FDBFuture *f = fdb_cluster_create_database(clusterPtr->cluster, (uint8_t*)dbName.c_str(), (int)strlen(dbName.c_str())); + + fdb_error_t errorCode = fdb_future_block_until_ready(f); + + FDBDatabase *database; + if(errorCode == 0) + errorCode = fdb_future_get_database(f, &database); + + if(errorCode != 0) + return ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + + Handle jsValue = Database::NewInstance(database); + return scope.Close(jsValue); +} + +void Cluster::Init() { + HandleScope scope; + + Local tpl = FunctionTemplate::New(New); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + tpl->SetClassName(String::NewSymbol("Cluster")); + + tpl->PrototypeTemplate()->Set(String::NewSymbol("openDatabase"), FunctionTemplate::New(OpenDatabase)->GetFunction()); + + constructor = Persistent::New(tpl->GetFunction()); +} + +Handle Cluster::New(const Arguments &args) { + HandleScope scope; + + Cluster *c = new Cluster(); + c->Wrap(args.Holder()); + + return scope.Close(args.Holder()); +} + +Handle Cluster::NewInstance(FDBCluster *ptr) { + HandleScope scope; + + Local instance = constructor->NewInstance(0, NULL); + + Cluster *clusterObj = ObjectWrap::Unwrap(instance); + clusterObj->cluster = ptr; + + instance->Set(String::NewSymbol("options"), FdbOptions::CreateOptions(FdbOptions::ClusterOption, instance)); + + return scope.Close(instance); +} diff --git a/bindings/nodejs/src/Cluster.h b/bindings/nodejs/src/Cluster.h new file mode 100644 index 0000000000..6614336df4 --- /dev/null +++ b/bindings/nodejs/src/Cluster.h @@ -0,0 +1,47 @@ +/* + * Cluster.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef FDB_NODE_CLUSTER_H +#define FDB_NODE_CLUSTER_H + +#include "Version.h" + +#include +#include + +class Cluster : public node::ObjectWrap { + public: + static void Init(); + static v8::Handle NewInstance(FDBCluster *ptr); + static v8::Handle New(const v8::Arguments &args); + static v8::Handle OpenDatabase(const v8::Arguments &args); + static v8::Handle Destroy(const v8::Arguments &args); + + FDBCluster* GetCluster() { return cluster; } + + private: + Cluster(); + ~Cluster(); + static v8::Persistent constructor; + FDBCluster *cluster; +}; + +#endif diff --git a/bindings/nodejs/src/Database.cpp b/bindings/nodejs/src/Database.cpp new file mode 100644 index 0000000000..971909b3cd --- /dev/null +++ b/bindings/nodejs/src/Database.cpp @@ -0,0 +1,86 @@ +/* + * Database.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include +#include +#include + +#include "Database.h" +#include "FdbOptions.h" +#include "NodeCallback.h" + +using namespace v8; +using namespace std; + +Database::Database() { }; + +Database::~Database() { + fdb_database_destroy(db); +}; + +Persistent Database::constructor; + +void Database::Init() { + Local tpl = FunctionTemplate::New(New); + tpl->SetClassName(String::NewSymbol("Database")); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + + tpl->PrototypeTemplate()->Set(String::NewSymbol("createTransaction"), FunctionTemplate::New(CreateTransaction)->GetFunction()); + + constructor = Persistent::New(tpl->GetFunction()); +} + +Handle Database::CreateTransaction(const v8::Arguments &args) { + HandleScope scope; + + Database *dbPtr = node::ObjectWrap::Unwrap(args.Holder()); + FDBDatabase *db = dbPtr->db; + FDBTransaction *tr; + fdb_error_t err = fdb_database_create_transaction(db, &tr); + if (err) { + ThrowException(FdbError::NewInstance(err, fdb_get_error(err))); + return scope.Close(Undefined()); + } + + return scope.Close(Transaction::NewInstance(tr)); +} + +Handle Database::New(const Arguments &args) { + HandleScope scope; + + Database *db = new Database(); + db->Wrap(args.Holder()); + + return scope.Close(args.Holder()); +} + +Handle Database::NewInstance(FDBDatabase *ptr) { + HandleScope scope; + + Local instance = constructor->NewInstance(0, NULL); + Database *dbObj = ObjectWrap::Unwrap(instance); + dbObj->db = ptr; + + instance->Set(String::NewSymbol("options"), FdbOptions::CreateOptions(FdbOptions::DatabaseOption, instance)); + + return scope.Close(instance); +} diff --git a/bindings/nodejs/src/Database.h b/bindings/nodejs/src/Database.h new file mode 100644 index 0000000000..e459425ed0 --- /dev/null +++ b/bindings/nodejs/src/Database.h @@ -0,0 +1,49 @@ +/* + * Database.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef FDB_NODE_DATABASE_H +#define FDB_NODE_DATABASE_H + +#include "Version.h" +#include "Transaction.h" + +#include +#include + +class Database: public node::ObjectWrap { + public: + static void Init(); + static v8::Handle NewInstance(FDBDatabase *ptr); + static v8::Handle New(const v8::Arguments &args); + + FDBDatabase* GetDatabase() { return db; } + + private: + Database(); + ~Database(); + + static v8::Handle CreateTransaction(const v8::Arguments &args); + static v8::Persistent constructor; + + FDBDatabase *db; +}; + +#endif diff --git a/bindings/nodejs/src/FdbError.cpp b/bindings/nodejs/src/FdbError.cpp new file mode 100644 index 0000000000..0c6e20d8e2 --- /dev/null +++ b/bindings/nodejs/src/FdbError.cpp @@ -0,0 +1,48 @@ +/* + * FdbError.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include "FdbError.h" + +using namespace v8; +using namespace node; + +static Persistent module; + +void FdbError::Init( Handle module ) { + ::module = Persistent::New( module ); +} + +Handle FdbError::NewInstance(fdb_error_t code, const char *description) { + HandleScope scope; + + Local constructor = module->Get( String::NewSymbol("FDBError") ); + Local instance; + if (!constructor.IsEmpty() && constructor->IsFunction()) { + Local constructorArgs[] = { String::New(description), Integer::New(code) }; + instance = Local::Cast(constructor)->NewInstance(2, constructorArgs); + } else { + // We can't find the (javascript) FDBError class, so construct and throw *something* + instance = Exception::Error(String::New("FDBError class not found. Unable to deliver error."))->ToObject(); + } + + return scope.Close(instance); +} diff --git a/bindings/nodejs/src/FdbError.h b/bindings/nodejs/src/FdbError.h new file mode 100644 index 0000000000..c730284de1 --- /dev/null +++ b/bindings/nodejs/src/FdbError.h @@ -0,0 +1,40 @@ +/* + * FdbError.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef FDB_NODE_FDB_ERROR_H +#define FDB_NODE_FDB_ERROR_H + +#include "Version.h" + +#include +#include + +class FdbError { + public: + static v8::Handle NewInstance(fdb_error_t code, const char *description); + + static void Init( v8::Handle module ); + + private: + FdbError(); // not implemented by design +}; + +#endif diff --git a/bindings/nodejs/src/FdbOptions.cpp b/bindings/nodejs/src/FdbOptions.cpp new file mode 100644 index 0000000000..97fa49092a --- /dev/null +++ b/bindings/nodejs/src/FdbOptions.cpp @@ -0,0 +1,241 @@ +/* + * FdbOptions.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "FdbOptions.h" +#include "Cluster.h" +#include "Database.h" +#include "Transaction.h" +#include "FdbError.h" + +#include +#include + +#define INVALID_OPTION_VALUE_ERROR_CODE (fdb_error_t)2006 + +using namespace v8; +using namespace node; + +std::map> FdbOptions::optionTemplates; +std::map FdbOptions::scopeInfo; +std::map> FdbOptions::parameterTypes; + +FdbOptions::FdbOptions() { } + +void FdbOptions::InitOptionsTemplate(Persistent &tpl, const char *className) { + tpl = Persistent::New(FunctionTemplate::New(New)); + tpl->SetClassName(String::NewSymbol(className)); + tpl->InstanceTemplate()->SetInternalFieldCount(1); +} + +void FdbOptions::AddOption(Scope scope, std::string name, int value, ParameterType type) { + if(scope == NetworkOption || scope == ClusterOption || scope == DatabaseOption || scope == TransactionOption || scope == MutationType) { + bool isSetter = scope != MutationType; + optionTemplates[scope]->PrototypeTemplate()->Set(v8::String::NewSymbol(ToJavaScriptName(name, isSetter).c_str()), + v8::FunctionTemplate::New(scopeInfo[scope].optionFunction, v8::Integer::New(value))->GetFunction()); + parameterTypes[scope][value] = type; + } + else if(scope == StreamingMode) { + optionTemplates[scope]->PrototypeTemplate()->Set(v8::String::NewSymbol(ToJavaScriptName(name, false).c_str()), v8::Integer::New(value)); + } + else if(scope == ConflictRangeType) { + //Conflict range type enum is not exposed to JS code + } +} + +Handle FdbOptions::New(const Arguments &args) { + FdbOptions *options = new FdbOptions(); + options->Wrap(args.Holder()); + + return args.Holder(); +} + +void FdbOptions::WeakCallback(Persistent value, void *data) { } + +Handle FdbOptions::NewInstance(Persistent optionsTemplate, Handle source) { + HandleScope scope; + + Local instance = optionsTemplate->GetFunction()->NewInstance(); + + FdbOptions *optionsObj = ObjectWrap::Unwrap(instance); + optionsObj->source = Persistent::New(source); + optionsObj->source.MakeWeak(optionsObj, WeakCallback); + + return scope.Close(instance); +} + +Handle FdbOptions::CreateOptions(Scope scope, Handle source) { + return NewInstance(optionTemplates[scope], source); +} + +Handle FdbOptions::CreateEnum(Scope scope) { + return optionTemplates[scope]->GetFunction()->NewInstance(); +} + +Parameter GetStringParameter(const Arguments &args, int index) { + if(args.Length() <= index || (!Buffer::HasInstance(args[index]) && !args[index]->IsString())) + return INVALID_OPTION_VALUE_ERROR_CODE; + else if(args[index]->IsString()) { + String::Utf8Value val(args[index]); + return std::string(*val, val.length()); + } + else + return std::string(Buffer::Data(args[index]->ToObject()), Buffer::Length(args[index]->ToObject())); +}; + +Parameter FdbOptions::GetOptionParameter(const Arguments &args, Scope scope, int optionValue, int index) { + if(args.Length() > index) { + int64_t val; + switch(parameterTypes[scope][optionValue]) { + case FdbOptions::String: + return GetStringParameter(args, index); + + case FdbOptions::Bytes: + if(!Buffer::HasInstance(args[index])) + return INVALID_OPTION_VALUE_ERROR_CODE; + + return std::string(Buffer::Data(args[index]->ToObject()), Buffer::Length(args[index]->ToObject())); + + case FdbOptions::Int: + if(!args[index]->IsNumber()) + return INVALID_OPTION_VALUE_ERROR_CODE; + val = args[index]->IntegerValue(); + return std::string((const char*)&val, 8); + + + case FdbOptions::None: + return Parameter(); + } + } + + return Parameter(); +} + +v8::Handle SetNetworkOption(const Arguments &args) { + FDBNetworkOption op = (FDBNetworkOption)args.Data()->Uint32Value(); + + Parameter param = FdbOptions::GetOptionParameter(args, FdbOptions::NetworkOption, op); + fdb_error_t errorCode = param.errorCode; + if(errorCode == 0) + errorCode = fdb_network_set_option(op, param.getValue(), param.getLength()); + + if(errorCode) + return ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + + return Null(); +} + +v8::Handle SetClusterOption(const Arguments &args) { + FdbOptions *options = ObjectWrap::Unwrap(args.Holder()); + Cluster *cluster = ObjectWrap::Unwrap(options->GetSource()->ToObject()); + FDBClusterOption op = (FDBClusterOption)args.Data()->Uint32Value(); + + Parameter param = FdbOptions::GetOptionParameter(args, FdbOptions::ClusterOption, op); + fdb_error_t errorCode = param.errorCode; + if(errorCode == 0) + errorCode = fdb_cluster_set_option(cluster->GetCluster(), op, param.getValue(), param.getLength()); + + if(errorCode) + return ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + + return Null(); +} + +v8::Handle SetDatabaseOption(const Arguments &args) { + FdbOptions *options = ObjectWrap::Unwrap(args.Holder()); + Database *db = ObjectWrap::Unwrap(options->GetSource()->ToObject()); + FDBDatabaseOption op = (FDBDatabaseOption)args.Data()->Uint32Value(); + + Parameter param = FdbOptions::GetOptionParameter(args, FdbOptions::DatabaseOption, op); + fdb_error_t errorCode = param.errorCode; + if(errorCode == 0) + errorCode = fdb_database_set_option(db->GetDatabase(), op, param.getValue(), param.getLength()); + + if(errorCode) + return ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + + return Null(); +} + +v8::Handle SetTransactionOption(const Arguments &args) { + FdbOptions *options = ObjectWrap::Unwrap(args.Holder()); + Transaction *tr = ObjectWrap::Unwrap(options->GetSource()->ToObject()); + FDBTransactionOption op = (FDBTransactionOption)args.Data()->Uint32Value(); + + Parameter param = FdbOptions::GetOptionParameter(args, FdbOptions::TransactionOption, op); + fdb_error_t errorCode = param.errorCode; + if(errorCode == 0) + errorCode = fdb_transaction_set_option(tr->GetTransaction(), op, param.getValue(), param.getLength()); + + if(errorCode) + return ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + + return Null(); +} + +v8::Handle CallAtomicOperation(const Arguments &args) { + Transaction *tr = ObjectWrap::Unwrap(args.Holder()); + Parameter key = GetStringParameter(args, 0); + Parameter value = GetStringParameter(args, 1); + + fdb_error_t errorCode = key.errorCode > 0 ? key.errorCode : value.errorCode; + if(errorCode > 0) + return ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + + fdb_transaction_atomic_op(tr->GetTransaction(), key.getValue(), key.getLength(), value.getValue(), value.getLength(), (FDBMutationType)args.Data()->Uint32Value()); + + return Null(); +} + +//Converts names using underscores as word separators to camel case (but preserves existing capitalization, if present). If isSetter, prepends the word 'set' to each name +std::string FdbOptions::ToJavaScriptName(std::string optionName, bool isSetter) { + if(isSetter) + optionName = "set_" + optionName; + + size_t start = 0; + while(start < optionName.size()) { + if(start != 0) + optionName[start] = ::toupper(optionName[start]); + size_t index = optionName.find_first_of('_', start); + if(index == std::string::npos) + break; + + optionName.erase(optionName.begin() + index); + + start = index; + } + + return optionName; +} + +void FdbOptions::Init() { + scopeInfo[NetworkOption] = ScopeInfo("FdbNetworkOptions", SetNetworkOption); + scopeInfo[ClusterOption] = ScopeInfo("FdbClusterOptions", SetClusterOption); + scopeInfo[DatabaseOption] = ScopeInfo("FdbDatabaseOptions", SetDatabaseOption); + scopeInfo[TransactionOption] = ScopeInfo("FdbTransactionOptions", SetTransactionOption); + scopeInfo[StreamingMode] = ScopeInfo("FdbStreamingMode", NULL); + scopeInfo[MutationType] = ScopeInfo("AtomicOperations", CallAtomicOperation); + //scopeInfo[ConflictRangeType] = ScopeInfo("ConflictRangeType", NULL); + + for(auto itr = scopeInfo.begin(); itr != scopeInfo.end(); ++itr) + InitOptionsTemplate(optionTemplates[itr->first], itr->second.templateClassName.c_str()); + + InitOptions(); +} diff --git a/bindings/nodejs/src/FdbOptions.h b/bindings/nodejs/src/FdbOptions.h new file mode 100644 index 0000000000..35af0a0472 --- /dev/null +++ b/bindings/nodejs/src/FdbOptions.h @@ -0,0 +1,109 @@ +/* + * FdbOptions.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef FDB_NODE_FDB_OPTIONS_H +#define FDB_NODE_FDB_OPTIONS_H + +#define ADD_OPTION(scope, name, value, type) AddOption(scope, name, value, type) + +#include "Version.h" + +#include +#include +#include +#include + +struct Parameter { + Parameter() : isNull(true), errorCode(0) { } + Parameter(std::string param) : param(param), isNull(false), errorCode(0) { } + Parameter(fdb_error_t errorCode) : isNull(false), errorCode(errorCode) { } + + std::string param; + bool isNull; + fdb_error_t errorCode; + + uint8_t const* getValue() { return isNull ? NULL : (uint8_t const*)param.c_str(); } + int getLength() { return isNull ? 0 : (int)param.size(); } +}; + +struct ScopeInfo { + std::string templateClassName; + v8::Handle (*optionFunction) (const v8::Arguments &args); + + ScopeInfo() { } + ScopeInfo(std::string templateClassName, v8::Handle (*optionFunction) (const v8::Arguments &args)) { + this->templateClassName = templateClassName; + this->optionFunction = optionFunction; + } +}; + +class FdbOptions : node::ObjectWrap { + public: + static void Init(); + + enum ParameterType { + None, + Int, + String, + Bytes + }; + + enum Scope { + NetworkOption, + ClusterOption, + DatabaseOption, + TransactionOption, + StreamingMode, + MutationType, + ConflictRangeType + }; + + static v8::Handle CreateOptions(Scope scope, v8::Handle source = v8::Null()); + static v8::Handle CreateEnum(Scope scope); + + static Parameter GetOptionParameter(const v8::Arguments &args, Scope scope, int optionValue, int index = 0); + + v8::Persistent GetSource() { + return source; + } + + private: + static v8::Handle New(const v8::Arguments &args); + static v8::Handle NewInstance(v8::Persistent optionsTemplate, v8::Handle source); + + FdbOptions(); + + static void InitOptionsTemplate(v8::Persistent &tpl, const char *className); + static void InitOptions(); + + static void AddOption(Scope scope, std::string name, int value, ParameterType type); + static void WeakCallback(v8::Persistent value, void *data); + + static std::string ToJavaScriptName(std::string optionName, bool isSetter); + + static std::map scopeInfo; + static std::map> optionTemplates; + static std::map> parameterTypes; + + v8::Persistent source; +}; + +#endif diff --git a/bindings/nodejs/src/FdbV8Wrapper.cpp b/bindings/nodejs/src/FdbV8Wrapper.cpp new file mode 100644 index 0000000000..4b7eea8371 --- /dev/null +++ b/bindings/nodejs/src/FdbV8Wrapper.cpp @@ -0,0 +1,149 @@ +/* + * FdbV8Wrapper.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include "node.h" +#include +#include +#include +#include +#include + +#include "Database.h" +#include "NodeCallback.h" +#include "Cluster.h" +#include "Version.h" +#include "FdbError.h" +#include "FdbOptions.h" + +uv_thread_t fdbThread; + +using namespace v8; +using namespace std; + +bool networkStarted = false; + +Handle ApiVersion(const Arguments &args) { + int apiVersion = args[0]->Int32Value(); + fdb_error_t errorCode = fdb_select_api_version(apiVersion); + + if(errorCode != 0) { + if(errorCode == 2203) { + int maxSupportedVersion = fdb_get_max_api_version(); + + ostringstream errorStr; + if(FDB_API_VERSION > maxSupportedVersion) { + errorStr << "This version of the FoundationDB Node.js binding is not supported by the installed FoundationDB " + << "C library. The binding requires a library that supports API version " << FDB_API_VERSION + << ", but the installed library supports a maximum version of " << maxSupportedVersion << "."; + } + else { + errorStr << "API version " << apiVersion << " is not supported by the installed FoundationDB C library."; + } + + return ThrowException(FdbError::NewInstance(errorCode, errorStr.str().c_str())); + } + + return ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + } + + return Null(); +} + +static void networkThread(void *arg) { + fdb_error_t errorCode = fdb_run_network(); + if(errorCode != 0) + fprintf(stderr, "Unhandled error in FoundationDB network thread: %s (%d)\n", fdb_get_error(errorCode), errorCode); +} + +static Handle runNetwork() { + fdb_error_t errorCode = fdb_setup_network(); + + if(errorCode != 0) + return ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + + uv_thread_create(&fdbThread, networkThread, NULL); // FIXME: Return code? + + return Null(); +} + +Handle CreateCluster(const Arguments &args) { + HandleScope scope; + + FDBFuture *f = fdb_create_cluster(*String::AsciiValue(args[0]->ToString())); + fdb_error_t errorCode = fdb_future_block_until_ready(f); + + FDBCluster *cluster; + if(errorCode == 0) + errorCode = fdb_future_get_cluster(f, &cluster); + + if(errorCode != 0) + return ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + + Handle jsValue = Local::New(Cluster::NewInstance(cluster)); + return scope.Close(jsValue); +} + +Handle StartNetwork(const Arguments &args) { + if(!networkStarted) { + networkStarted = true; + return runNetwork(); + } + + return Null(); +} + +Handle StopNetwork(const Arguments &args) { + fdb_error_t errorCode = fdb_stop_network(); + + if(errorCode != 0) + return ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + + uv_thread_join(&fdbThread); + + //This line forces garbage collection. Useful for doing valgrind tests + //while(!V8::IdleNotification()); + + return Null(); +} + +void init(Handle target){ + FdbError::Init( target ); + Database::Init(); + Transaction::Init(); + Cluster::Init(); + FdbOptions::Init(); + Watch::Init(); + + target->Set(String::NewSymbol("apiVersion"), FunctionTemplate::New(ApiVersion)->GetFunction()); + target->Set(String::NewSymbol("createCluster"), FunctionTemplate::New(CreateCluster)->GetFunction()); + target->Set(String::NewSymbol("startNetwork"), FunctionTemplate::New(StartNetwork)->GetFunction()); + target->Set(String::NewSymbol("stopNetwork"), FunctionTemplate::New(StopNetwork)->GetFunction()); + target->Set(String::NewSymbol("options"), FdbOptions::CreateOptions(FdbOptions::NetworkOption)); + target->Set(String::NewSymbol("streamingMode"), FdbOptions::CreateEnum(FdbOptions::StreamingMode)); + target->Set(String::NewSymbol("atomic"), FdbOptions::CreateOptions(FdbOptions::MutationType)); +} + +#if NODE_VERSION_AT_LEAST(0, 8, 0) +NODE_MODULE(fdblib, init); +#else +#error "Node.js versions before v0.8.0 are not supported" +#endif diff --git a/bindings/nodejs/src/NodeCallback.h b/bindings/nodejs/src/NodeCallback.h new file mode 100644 index 0000000000..402fc11916 --- /dev/null +++ b/bindings/nodejs/src/NodeCallback.h @@ -0,0 +1,149 @@ +/* + * NodeCallback.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef FDB_NODE_NODE_CALLBACK_H +#define FDB_NODE_NODE_CALLBACK_H + +#include "FdbError.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#if NODE_VERSION_AT_LEAST(0, 7, 9) +#else +#error Node version too old +#endif + +using namespace std; +using namespace v8; +using namespace node; + +struct NodeCallback { + +public: + NodeCallback(FDBFuture *future, Persistent cbFunc) : future(future), cbFunc(cbFunc), refCount(1) { + uv_async_init(uv_default_loop(), &handle, &NodeCallback::nodeThreadCallback); + uv_ref((uv_handle_t*)&handle); + handle.data = this; + } + + void start() { + if (fdb_future_set_callback(future, &NodeCallback::futureReadyCallback, this)) { + fprintf(stderr, "fdb_future_set_callback failed.\n"); + abort(); + } + } + + virtual ~NodeCallback() { + cbFunc.Dispose(); + fdb_future_destroy(future); + } + + void addRef() { + ++refCount; + } + + void delRef() { + if(--refCount == 0) { + delete this; + } + } + + FDBFuture* getFuture() { + return future; + } + +private: + void close() { + uv_close((uv_handle_t*)&handle, &NodeCallback::closeCallback); + } + + static void closeCallback(uv_handle_s *handle) { + NodeCallback *nc = (NodeCallback*)((uv_async_t*)handle)->data; + nc->delRef(); + } + + static void futureReadyCallback(FDBFuture *f, void *ptr) { + NodeCallback *nc = (NodeCallback*)ptr; + uv_async_send(&nc->handle); + } + + static void nodeThreadCallback(uv_async_t *handle, int status) { + HandleScope scope; + + NodeCallback *nc = (NodeCallback*)handle->data; + FDBFuture *future = nc->future; + + uv_unref((uv_handle_t*)handle); + + Handle jsError; + Handle jsValue; + + fdb_error_t errorCode; + jsValue = nc->extractValue(future, errorCode); + if (errorCode == 0) + jsError = Null(); + else + jsError = FdbError::NewInstance(errorCode, fdb_get_error(errorCode)); + + Handle args[2] = { jsError, jsValue }; + + v8::TryCatch ex; + nc->cbFunc->Call(Context::GetCurrent()->Global(), 2, args); + + if(ex.HasCaught()) + fprintf(stderr, "\n%s\n", *String::AsciiValue(ex.StackTrace()->ToString())); + + nc->close(); + } + + FDBFuture* future; + uv_async_t handle; + Persistent cbFunc; + int refCount; + +protected: + virtual Handle extractValue(FDBFuture* future, fdb_error_t& outErr) = 0; + + static Handle makeBuffer(const char *arr, int length) { + HandleScope scope; + + Buffer *buf = Buffer::New(length); + Local slowBufferHandle = Local::New( buf->handle_ ); // Else the buffer, which has only a weak handle to itself, could be freed by GC in one of the below calls... + memcpy(Buffer::Data(buf), (const char*)arr, length); + + Local globalObj = Context::GetCurrent()->Global(); + Local bufferConstructor = Local::Cast(globalObj->Get(String::NewSymbol("Buffer"))); + + Handle constructorArgs[3] = { slowBufferHandle, Integer::New(length), Integer::New(0) }; + Handle actualBuffer = bufferConstructor->NewInstance(3, constructorArgs); + + return scope.Close(actualBuffer); + } +}; + +#endif diff --git a/bindings/nodejs/src/Transaction.cpp b/bindings/nodejs/src/Transaction.cpp new file mode 100644 index 0000000000..f26521d8d3 --- /dev/null +++ b/bindings/nodejs/src/Transaction.cpp @@ -0,0 +1,485 @@ +/* + * Transaction.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include +#include +#include +#include +#include +#include +#include + +#include "Transaction.h" +#include "NodeCallback.h" +#include "FdbError.h" +#include "FdbOptions.h" + +using namespace v8; +using namespace std; +using namespace node; + +// Transaction Implementation +Transaction::Transaction() { }; + +Transaction::~Transaction() { + fdb_transaction_destroy(tr); +}; + +Persistent Transaction::constructor; + +struct NodeValueCallback : NodeCallback { + + NodeValueCallback(FDBFuture *future, Persistent cbFunc) : NodeCallback(future, cbFunc) { } + + virtual Handle extractValue(FDBFuture* future, fdb_error_t& outErr) { + HandleScope scope; + + const char *value; + int valueLength; + int valuePresent; + + outErr = fdb_future_get_value(future, &valuePresent, (const uint8_t**)&value, &valueLength); + if (outErr) return scope.Close(Undefined()); + + Handle jsValue; + + if(!valuePresent) + jsValue = Null(); + else + jsValue = makeBuffer(value, valueLength); + + return scope.Close(jsValue); + } +}; + +struct NodeKeyCallback : NodeCallback { + + NodeKeyCallback(FDBFuture *future, Persistent cbFunc) : NodeCallback(future, cbFunc) { } + + virtual Handle extractValue(FDBFuture* future, fdb_error_t& outErr) { + HandleScope scope; + + const char *key; + int keyLength; + + outErr = fdb_future_get_key(future, (const uint8_t**)&key, &keyLength); + if (outErr) return scope.Close(Undefined()); + + Handle jsValue = makeBuffer(key, keyLength); + + return scope.Close(jsValue); + } +}; + +struct NodeVoidCallback : NodeCallback { + + NodeVoidCallback(FDBFuture *future, Persistent cbFunc) : NodeCallback(future, cbFunc) { } + + virtual Handle extractValue(FDBFuture* future, fdb_error_t& outErr) { + outErr = fdb_future_get_error(future); + return Undefined(); + } +}; + +struct NodeKeyValueCallback : NodeCallback { + + NodeKeyValueCallback(FDBFuture *future, Persistent cbFunc) : NodeCallback(future, cbFunc) { } + + virtual Handle extractValue(FDBFuture* future, fdb_error_t& outErr) { + HandleScope scope; + + const FDBKeyValue *kv; + int len; + fdb_bool_t more; + + outErr = fdb_future_get_keyvalue_array(future, &kv, &len, &more); + if (outErr) return scope.Close(Undefined()); + + /* + * Constructing a JavaScript array of KeyValue objects: + * { + * key: "some key", + * value: "some value" + * } + * + */ + + Handle returnObj = Object::New(); + Handle jsValueArray = Array::New(len); + + Handle keySymbol = String::NewSymbol("key"); + Handle valueSymbol = String::NewSymbol("value"); + + for(int i = 0; i < len; i++) { + Local jsKeyValue = Object::New(); + + Handle jsKeyBuffer = makeBuffer((const char*)kv[i].key, kv[i].key_length); + Handle jsValueBuffer = makeBuffer((const char*)kv[i].value, kv[i].value_length); + + jsKeyValue->Set(keySymbol, jsKeyBuffer); + jsKeyValue->Set(valueSymbol, jsValueBuffer); + jsValueArray->Set(Number::New(i), jsKeyValue); + } + + returnObj->Set(String::NewSymbol("array"), jsValueArray); + if(more) + returnObj->Set(String::NewSymbol("more"), Number::New(1)); + + return scope.Close(returnObj); + } +}; + +struct NodeVersionCallback : NodeCallback { + + NodeVersionCallback(FDBFuture *future, Persistent cbFunc) : NodeCallback(future, cbFunc) { } + + virtual Handle extractValue(FDBFuture* future, fdb_error_t& outErr) { + HandleScope scope; + + int64_t version; + + outErr = fdb_future_get_version(future, &version); + if (outErr) return scope.Close(Undefined()); + + //SOMEDAY: This limits the version to 53-bits. Do something different here? + Handle jsValue = Number::New((double)version); + + return scope.Close(jsValue); + } +}; + +struct NodeStringArrayCallback : NodeCallback { + + NodeStringArrayCallback(FDBFuture *future, Persistent cbFunc) : NodeCallback(future, cbFunc) { } + + virtual Handle extractValue(FDBFuture *future, fdb_error_t& outErr) { + HandleScope scope; + + const char **strings; + int stringCount; + + outErr = fdb_future_get_string_array(future, &strings, &stringCount); + if (outErr) return scope.Close(Undefined()); + + Handle jsArray = Array::New(stringCount); + for(int i = 0; i < stringCount; i++) + jsArray->Set(Number::New(i), makeBuffer(strings[i], (int)strlen(strings[i]))); + + return scope.Close(jsArray); + } +}; + +struct StringParams { + uint8_t *str; + int len; + + /* + * String arguments always have to be buffers to + * preserve bytes. Otherwise, stuff gets converted + * to UTF-8. + */ + StringParams(Handle keyVal) { + str = (uint8_t*)(Buffer::Data(keyVal->ToObject())); + len = (int)Buffer::Length(keyVal->ToObject()); + } +}; + +FDBTransaction* Transaction::GetTransactionFromArgs(const Arguments &args) { + return node::ObjectWrap::Unwrap(args.Holder())->tr; +} + +Persistent Transaction::GetCallback(Handle funcVal) { + return Persistent::New(Handle(Function::Cast(*funcVal))); +} + +Handle Transaction::Set(const Arguments &args){ + StringParams key(args[0]); + StringParams val(args[1]); + fdb_transaction_set(GetTransactionFromArgs(args), key.str, key.len, val.str, val.len); + + return Null(); +} + +Handle Transaction::Commit(const Arguments &args) { + FDBFuture *f = fdb_transaction_commit(GetTransactionFromArgs(args)); + (new NodeVoidCallback(f, GetCallback(args[0])))->start(); + return Null(); +} + +Handle Transaction::Clear(const Arguments &args) { + StringParams key(args[0]); + fdb_transaction_clear(GetTransactionFromArgs(args), key.str, key.len); + + return Null(); +} + +/* + * ClearRange takes two key strings. + */ +Handle Transaction::ClearRange(const Arguments &args) { + StringParams begin(args[0]); + StringParams end(args[1]); + fdb_transaction_clear_range(GetTransactionFromArgs(args), begin.str, begin.len, end.str, end.len); + + return Null(); +} + +/* + * This function takes a KeySelector and returns a future. + */ +Handle Transaction::GetKey(const Arguments &args) { + StringParams key(args[0]); + int selectorOrEqual = args[1]->Int32Value(); + int selectorOffset = args[2]->Int32Value(); + bool snapshot = args[3]->BooleanValue(); + + FDBFuture *f = fdb_transaction_get_key(GetTransactionFromArgs(args), key.str, key.len, (fdb_bool_t)selectorOrEqual, selectorOffset, snapshot); + (new NodeKeyCallback(f, GetCallback(args[4])))->start(); + return Null(); +} + +Handle Transaction::Get(const Arguments &args) { + StringParams key(args[0]); + bool snapshot = args[1]->BooleanValue(); + + FDBFuture *f = fdb_transaction_get(GetTransactionFromArgs(args), key.str, key.len, snapshot); + (new NodeValueCallback(f, GetCallback(args[2])))->start(); + return Null(); +} + +Handle Transaction::GetRange(const Arguments &args) { + StringParams start(args[0]); + int startOrEqual = args[1]->Int32Value(); + int startOffset = args[2]->Int32Value(); + + StringParams end(args[3]); + int endOrEqual = args[4]->Int32Value(); + int endOffset = args[5]->Int32Value(); + + int limit = args[6]->Int32Value(); + FDBStreamingMode mode = (FDBStreamingMode)args[7]->Int32Value(); + int iteration = args[8]->Int32Value(); + bool snapshot = args[9]->BooleanValue(); + bool reverse = args[10]->BooleanValue(); + + FDBFuture *f = fdb_transaction_get_range(GetTransactionFromArgs(args), start.str, start.len, (fdb_bool_t)startOrEqual, startOffset, + end.str, end.len, (fdb_bool_t)endOrEqual, endOffset, limit, 0, mode, iteration, snapshot, reverse); + + (new NodeKeyValueCallback(f, GetCallback(args[11])))->start(); + return Null(); +} + +Handle Transaction::Watch(const Arguments &args) { + HandleScope scope; + + Transaction *trPtr = node::ObjectWrap::Unwrap(args.Holder()); + + uint8_t *keyStr = (uint8_t*)(Buffer::Data(args[0]->ToObject())); + int keyLen = (int)Buffer::Length(args[0]->ToObject()); + + Persistent cb = Persistent::New(Handle(Function::Cast(*args[1]))); + + FDBFuture *f = fdb_transaction_watch(trPtr->tr, keyStr, keyLen); + NodeVoidCallback *callback = new NodeVoidCallback(f, cb); + Handle watch = Watch::NewInstance(callback); + + callback->start(); + return scope.Close(watch); +} + +Handle Transaction::AddConflictRange(const Arguments &args, FDBConflictRangeType type) { + StringParams start(args[0]); + StringParams end(args[1]); + + fdb_error_t errorCode = fdb_transaction_add_conflict_range(GetTransactionFromArgs(args), start.str, start.len, end.str, end.len, type); + + if(errorCode != 0) { + ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + return Undefined(); + } + + return Null(); +} + +Handle Transaction::AddReadConflictRange(const Arguments &args) { + return AddConflictRange(args, FDB_CONFLICT_RANGE_TYPE_READ); +} + +Handle Transaction::AddWriteConflictRange(const Arguments &args) { + return AddConflictRange(args, FDB_CONFLICT_RANGE_TYPE_WRITE); +} + +Handle Transaction::OnError(const Arguments &args) { + fdb_error_t errorCode = args[0]->Int32Value(); + FDBFuture *f = fdb_transaction_on_error(GetTransactionFromArgs(args), errorCode); + (new NodeVoidCallback(f, GetCallback(args[1])))->start(); + return Null(); +} + +Handle Transaction::Reset(const Arguments &args) { + fdb_transaction_reset(GetTransactionFromArgs(args)); + return Null(); +} + +Handle Transaction::SetReadVersion(const Arguments &args) { + int64_t version = args[0]->IntegerValue(); + fdb_transaction_set_read_version(GetTransactionFromArgs(args), version); + return Null(); +} + +Handle Transaction::GetReadVersion(const Arguments &args) { + FDBFuture *f = fdb_transaction_get_read_version(GetTransactionFromArgs(args)); + (new NodeVersionCallback(f, GetCallback(args[0])))->start(); + return Null(); +} + +Handle Transaction::GetCommittedVersion(const Arguments &args) { + HandleScope scope; + + int64_t version; + fdb_error_t errorCode = fdb_transaction_get_committed_version(GetTransactionFromArgs(args), &version); + + if(errorCode != 0) { + ThrowException(FdbError::NewInstance(errorCode, fdb_get_error(errorCode))); + return scope.Close(Undefined()); + } + + return scope.Close(Number::New((double)version)); +} + +Handle Transaction::GetVersionstamp(const Arguments &args) { + FDBFuture *f = fdb_transaction_get_versionstamp(GetTransactionFromArgs(args)); + (new NodeKeyCallback(f, GetCallback(args[0])))->start(); + return Null(); +} + +Handle Transaction::Cancel(const Arguments &args) { + fdb_transaction_cancel(GetTransactionFromArgs(args)); + return Null(); +} + +Handle Transaction::GetAddressesForKey(const Arguments &args) { + StringParams key(args[0]); + + FDBFuture *f = fdb_transaction_get_addresses_for_key(GetTransactionFromArgs(args), key.str, key.len); + (new NodeStringArrayCallback(f, GetCallback(args[1])))->start(); + return Null(); +} + +Handle Transaction::New(const Arguments &args) { + Transaction *tr = new Transaction(); + tr->Wrap(args.Holder()); + + return args.Holder(); +} + +Handle Transaction::NewInstance(FDBTransaction *ptr) { + HandleScope scope; + + Local instance = constructor->NewInstance(); + + Transaction *trObj = ObjectWrap::Unwrap(instance); + trObj->tr = ptr; + + instance->Set(String::NewSymbol("options"), FdbOptions::CreateOptions(FdbOptions::TransactionOption, instance)); + + return scope.Close(instance); +} + +void Transaction::Init() { + Local tpl = FunctionTemplate::New(New); + + tpl->SetClassName(String::NewSymbol("Transaction")); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + + tpl->PrototypeTemplate()->Set(String::NewSymbol("get"), FunctionTemplate::New(Get)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("getRange"), FunctionTemplate::New(GetRange)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("getKey"), FunctionTemplate::New(GetKey)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("watch"), FunctionTemplate::New(Watch)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("set"), FunctionTemplate::New(Set)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("commit"), FunctionTemplate::New(Commit)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("clear"), FunctionTemplate::New(Clear)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("clearRange"), FunctionTemplate::New(ClearRange)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("addReadConflictRange"), FunctionTemplate::New(AddReadConflictRange)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("addWriteConflictRange"), FunctionTemplate::New(AddWriteConflictRange)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("onError"), FunctionTemplate::New(OnError)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("reset"), FunctionTemplate::New(Reset)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("getReadVersion"), FunctionTemplate::New(GetReadVersion)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("setReadVersion"), FunctionTemplate::New(SetReadVersion)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("getCommittedVersion"), FunctionTemplate::New(GetCommittedVersion)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("getVersionstamp"), FunctionTemplate::New(GetVersionstamp)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("cancel"), FunctionTemplate::New(Cancel)->GetFunction()); + tpl->PrototypeTemplate()->Set(String::NewSymbol("getAddressesForKey"), FunctionTemplate::New(GetAddressesForKey)->GetFunction()); + + constructor = Persistent::New(tpl->GetFunction()); +} + +// Watch implementation +Watch::Watch() : callback(NULL) { }; + +Watch::~Watch() { + if(callback) { + if(callback->getFuture()) + fdb_future_cancel(callback->getFuture()); + + callback->delRef(); + } +}; + +Persistent Watch::constructor; + +Handle Watch::NewInstance(NodeCallback *callback) { + HandleScope scope; + + Local instance = constructor->NewInstance(); + + Watch *watchObj = ObjectWrap::Unwrap(instance); + watchObj->callback = callback; + callback->addRef(); + + return scope.Close(instance); +} + +Handle Watch::New(const Arguments &args) { + Watch *c = new Watch(); + c->Wrap(args.Holder()); + + return args.Holder(); +} + +Handle Watch::Cancel(const Arguments &args) { + NodeCallback *callback = node::ObjectWrap::Unwrap(args.Holder())->callback; + + if(callback && callback->getFuture()) + fdb_future_cancel(callback->getFuture()); + + return Null(); +} + +void Watch::Init() { + Local tpl = FunctionTemplate::New(New); + tpl->SetClassName(String::NewSymbol("Watch")); + tpl->InstanceTemplate()->SetInternalFieldCount(1); + + tpl->PrototypeTemplate()->Set(String::NewSymbol("cancel"), FunctionTemplate::New(Cancel)->GetFunction()); + + constructor = Persistent::New(tpl->GetFunction()); +} diff --git a/bindings/nodejs/src/Transaction.h b/bindings/nodejs/src/Transaction.h new file mode 100644 index 0000000000..5bfcc11301 --- /dev/null +++ b/bindings/nodejs/src/Transaction.h @@ -0,0 +1,92 @@ +/* + * Transaction.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef FDB_NODE_TRANSACTION_H +#define FDB_NODE_TRANSACTION_H + +#include "Version.h" + +#include +#include + +#include "NodeCallback.h" + +class Transaction: public node::ObjectWrap { + public: + static void Init(); + static v8::Handle NewInstance(FDBTransaction *ptr); + static v8::Handle New(const v8::Arguments &args); + + static v8::Handle Get(const v8::Arguments &args); + static v8::Handle GetKey(const v8::Arguments &args); + static v8::Handle Set(const v8::Arguments &args); + static v8::Handle Commit(const v8::Arguments &args); + static v8::Handle Clear(const v8::Arguments &args); + static v8::Handle ClearRange(const v8::Arguments &args); + static v8::Handle GetRange(const v8::Arguments &args); + static v8::Handle Watch(const v8::Arguments &args); + + static v8::Handle AddConflictRange(const v8::Arguments &args, FDBConflictRangeType type); + static v8::Handle AddReadConflictRange(const v8::Arguments &args); + static v8::Handle AddWriteConflictRange(const v8::Arguments &args); + + static v8::Handle OnError(const v8::Arguments &args); + static v8::Handle Reset(const v8::Arguments &args); + + static v8::Handle SetReadVersion(const v8::Arguments &args); + static v8::Handle GetReadVersion(const v8::Arguments &args); + static v8::Handle GetCommittedVersion(const v8::Arguments &args); + static v8::Handle GetVersionstamp(const v8::Arguments &args); + + static v8::Handle Cancel(const v8::Arguments &args); + + static v8::Handle GetAddressesForKey(const v8::Arguments &args); + + FDBTransaction* GetTransaction() { return tr; } + private: + Transaction(); + ~Transaction(); + + static v8::Persistent constructor; + FDBTransaction *tr; + + static FDBTransaction* GetTransactionFromArgs(const v8::Arguments &args); + static v8::Persistent GetCallback(const v8::Handle funcVal); +}; + +class Watch : public node::ObjectWrap { + public: + static void Init(); + + static v8::Handle NewInstance(NodeCallback *callback); + static v8::Handle New(const v8::Arguments &args); + + static v8::Handle Cancel(const v8::Arguments &args); + + private: + Watch(); + ~Watch(); + + static v8::Persistent constructor; + NodeCallback *callback; +}; + +#endif diff --git a/bindings/nodejs/src/Version.h b/bindings/nodejs/src/Version.h new file mode 100644 index 0000000000..52afea41d5 --- /dev/null +++ b/bindings/nodejs/src/Version.h @@ -0,0 +1,27 @@ +/* + * Version.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#ifndef FDB_NODE_VERSION_H +#define FDB_NODE_VERSION_H + +#define FDB_API_VERSION 500 + +#endif diff --git a/bindings/nodejs/tests/async_test.js b/bindings/nodejs/tests/async_test.js new file mode 100755 index 0000000000..4294c646f9 --- /dev/null +++ b/bindings/nodejs/tests/async_test.js @@ -0,0 +1,51 @@ +/* + * async_test.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var fdb = require('../lib/fdb').apiVersion(200); + +fdb.open(null, null, function(dbErr, dbVal) { + fdb.open(null, null, function(dbErr, dbVal) { + if(dbVal == null) + console.log("database is null"); + console.log("created database", dbErr); + console.log(JSON.stringify(dbVal)); + var tr = dbVal.createTransaction(); + console.log("created transaction"); + + tr.get('foo', function(err, val) { + console.log("get called", val, err); + tr.set('foo', 'bar'); + tr.commit(function(err) { + console.log("commit called", err); + var x = tr.get('foo'); + x(function(err, val) { + console.log("get called", val.toString(), err); + tr.clear('foo') + tr.commit(function(err) { + console.log("commit called", err); + tr.get('foo', function(err, val) { + console.log("get called", val, err); + }); + }); + }); + }); + }); + }); +}); diff --git a/bindings/nodejs/tests/directory_extension.js b/bindings/nodejs/tests/directory_extension.js new file mode 100644 index 0000000000..a7e0baa9fa --- /dev/null +++ b/bindings/nodejs/tests/directory_extension.js @@ -0,0 +1,304 @@ +/* + * directory_extension.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +"use strict"; + +var util = require('util'); +var fdb = require('../lib/fdb.js').apiVersion(parseInt(process.argv[3])); +var fdbUtil = require('../lib/fdbUtil.js'); +var dirUtil = require('./directory_util.js'); + +var logAll = false; + +var logInstructions = false; +var logOps = false; +var logDirs = false; +var logErrors = false; + +var logOp = function(message, force) { + if(logOps || logAll || force) + console.log(message); +}; + +var DirectoryExtension = function() { + this.dirList = [fdb.directory]; + this.dirIndex = 0; + this.errorIndex = 0; +}; + +DirectoryExtension.prototype.processInstruction = function(inst, cb) { + var self = this; + var directory = this.dirList[this.dirIndex]; + + var promiseCb = function(err) { + if(err && (logErrors || logAll)) { + console.log(err); + //console.log(err.stack); + } + + dirUtil.pushError(self, inst, err); + cb(); + }; + + var appendDir = function(dir) { + if(logDirs || logAll) + console.log(util.format('pushed at %d (op=%s)', self.dirList.length, inst.op)); + + self.dirList.push(dir); + }; + + if(logAll || logInstructions) + console.log(inst.context.instructionIndex, inst.tokens[0].toString()); + + if(inst.op === 'DIRECTORY_CREATE_SUBSPACE') { + dirUtil.popTuples(inst) + .then(function(path) { + return inst.pop() + .then(function(rawPrefix) { + logOp(util.format('created subspace at (%s): %s', path, fdb.buffer.printable(rawPrefix))); + appendDir(new fdb.Subspace(path, rawPrefix)); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_CREATE_LAYER') { + inst.pop({count: 3}) + .then(function(params) { + var index1 = params[0]; + var index2 = params[1]; + var allowManualPrefixes = params[2]; + + var dir1 = self.dirList[params[0]]; + var dir2 = self.dirList[params[1]]; + if(dir1 === null || dir2 === null) { + logOp('create directory layer: None'); + appendDir(null); + } + else { + logOp(util.format('create directory layer: node_subspace (%d) = %s, content_subspace (%d) = %s, allow_manual_prefixes = %d', index1, fdb.buffer.printable(dir1.rawPrefix), index2, fdb.buffer.printable(dir2.rawPrefix), allowManualPrefixes)); + appendDir(new fdb.DirectoryLayer({ nodeSubspace: dir1, + contentSubspace: dir2, + allowManualPrefixes: allowManualPrefixes === 1 })); + } + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_CHANGE') { + inst.pop() + .then(function(index) { + if(self.dirList[index] === null) + self.dirIndex = self.errorIndex; + else + self.dirIndex = index; + + if(logDirs || logAll) { + var dir = self.dirList[self.dirIndex]; + console.log(util.format('changed directory to %d ((%s))', self.dirIndex, dir._path)); + } + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_SET_ERROR_INDEX') { + inst.pop() + .then(function(errorIndex) { + self.errorIndex = errorIndex; + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_CREATE_OR_OPEN') { + dirUtil.popTuples(inst) + .then(function(path) { + return inst.pop() + .then(function(layer) { + logOp(util.format('create_or_open (%s): layer=%s', directory._path + path, fdb.buffer.printable(layer) || '')); + return directory.createOrOpen(inst.tr, path, {'layer': layer || undefined}); + }) + .then(function(dir) { + appendDir(dir); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_CREATE') { + dirUtil.popTuples(inst) + .then(function(path) { + return inst.pop({count: 2}) + .then(function(params) { + logOp(util.format('create (%s): layer=%s, prefix=%s', directory._path + path, fdb.buffer.printable(params[0]) || '', fdb.buffer.printable(params[1] || ''))); + return directory.create(inst.tr, path, {'layer': params[0] || undefined, 'prefix': params[1] || undefined}); + }) + .then(function(dir) { + appendDir(dir); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_OPEN') { + dirUtil.popTuples(inst) + .then(function(path) { + return inst.pop() + .then(function(layer) { + logOp(util.format('open (%s): layer=%s', directory._path + path, fdb.buffer.printable(layer) || '')); + return directory.open(inst.tr, path, {'layer': layer}); + }) + .then(function(dir) { + appendDir(dir); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_MOVE') { + dirUtil.popTuples(inst, 2) + .then(function(paths) { + logOp(util.format('move (%s) to (%s)', directory._path + paths[0], directory._path + paths[1])); + return directory.move(inst.tr, paths[0], paths[1]) + .then(function(dir) { + appendDir(dir); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_MOVE_TO') { + dirUtil.popTuples(inst) + .then(function(newAbsolutePath) { + logOp(util.format('move (%s) to (%s)', directory._path, newAbsolutePath)); + return directory.moveTo(inst.tr, newAbsolutePath) + .then(function(dir) { + appendDir(dir); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_REMOVE') { + inst.pop() + .then(function(count) { + return dirUtil.popTuples(inst, count) + .then(function(path) { + logOp(util.format('remove (%s)', directory._path + (path ? path : ''))); + return directory.remove(inst.tr, path); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_REMOVE_IF_EXISTS') { + inst.pop() + .then(function(count) { + return dirUtil.popTuples(inst, count) + .then(function(path) { + logOp(util.format('remove_if_exists (%s)', directory._path + (path ? path : ''))); + return directory.removeIfExists(inst.tr, path); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_LIST') { + inst.pop() + .then(function(count) { + return dirUtil.popTuples(inst, count) + .then(function(path) { + return directory.list(inst.tr, path); + }) + .then(function(children) { + inst.push(fdb.tuple.pack(children)); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_EXISTS') { + var path; + inst.pop() + .then(function(count) { + return dirUtil.popTuples(inst, count) + .then(function(p) { + path = p; + return directory.exists(inst.tr, path); + }) + .then(function(exists) { + logOp(util.format('exists (%s): %d', directory._path + (path ? path : ''), exists ? 1 : 0)); + inst.push(exists ? 1 : 0); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_PACK_KEY') { + dirUtil.popTuples(inst) + .then(function(keyTuple) { + inst.push(directory.pack(keyTuple)); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_UNPACK_KEY') { + inst.pop() + .then(function(key) { + logOp(util.format('unpack %s in subspace with prefix %s', fdb.buffer.printable(key), fdb.buffer.printable(directory.rawPrefix))); + var tup = directory.unpack(key); + for(var i = 0; i < tup.length; ++i) + inst.push(tup[i]); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_RANGE') { + dirUtil.popTuples(inst) + .then(function(tup) { + var rng = directory.range(tup); + inst.push(rng.begin); + inst.push(rng.end); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_CONTAINS') { + inst.pop() + .then(function(key) { + inst.push(directory.contains(key) ? 1 : 0); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_OPEN_SUBSPACE') { + dirUtil.popTuples(inst) + .then(function(path) { + logOp(util.format('open_subspace (%s)', path)); + appendDir(directory.subspace(path)); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_LOG_SUBSPACE') { + inst.pop() + .then(function(prefix) { + inst.tr.set(Buffer.concat([prefix, fdb.tuple.pack([self.dirIndex])]), directory.key()); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_LOG_DIRECTORY') { + inst.pop() + .then(function(prefix) { + var exists; + return directory.exists(inst.tr) + .then(function(e) { + exists = e; + if(exists) + return directory.list(inst.tr); + else + return []; + }) + .then(function(children) { + var logSubspace = new fdb.Subspace([self.dirIndex], prefix); + inst.tr.set(logSubspace.get('path'), fdb.tuple.pack(directory.getPath())); + inst.tr.set(logSubspace.get('layer'), fdb.tuple.pack([directory.getLayer()])); + inst.tr.set(logSubspace.get('exists'), fdb.tuple.pack([exists ? 1 : 0])); + inst.tr.set(logSubspace.get('children'), fdb.tuple.pack(children)); + }); + })(promiseCb); + } + else if(inst.op === 'DIRECTORY_STRIP_PREFIX') { + inst.pop() + .then(function(str) { + if(!fdbUtil.buffersEqual(fdb.buffer(str).slice(0, directory.key().length), directory.key())) + throw new Error('String ' + str + ' does not start with raw prefix ' + directory.key()); + + inst.push(str.slice(directory.key().length)); + })(promiseCb); + } + else { + throw new Error('Unknown op: ' + inst.op); + } +}; + +module.exports = DirectoryExtension; diff --git a/bindings/nodejs/tests/directory_util.js b/bindings/nodejs/tests/directory_util.js new file mode 100644 index 0000000000..110b053ba0 --- /dev/null +++ b/bindings/nodejs/tests/directory_util.js @@ -0,0 +1,77 @@ +/* + * directory_util.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +"use strict"; +var fdb = require('../lib/fdb.js').apiVersion(parseInt(process.argv[3])); +var util = require('../lib/fdbUtil.js'); + +var opsThatCreateDirs = [ + 'DIRECTORY_CREATE_SUBSPACE', + 'DIRECTORY_CREATE_LAYER', + 'DIRECTORY_CREATE_OR_OPEN', + 'DIRECTORY_CREATE', + 'DIRECTORY_OPEN', + 'DIRECTORY_MOVE', + 'DIRECTORY_MOVE_TO', + 'DIRECTORY_OPEN_SUBSPACE' +]; + +var popTuples = function(inst, num, cb) { + if(typeof num === 'undefined') + num = 1; + return fdb.future.create(function(futureCb) { + var tuples = []; + if(num === 0) return futureCb(); + util.whileLoop(function(loopCb) { + inst.pop() + .then(function(count) { + return inst.pop({count: count}) + .then(function(tuple) { + tuples.push(tuple); + if(--num === 0) + return null; + }); + })(loopCb); + }, function() { + if(tuples.length == 1) + futureCb(undefined, tuples[0]); + else + futureCb(undefined, tuples); + }); + })(cb); +}; + +var pushError = function(self, inst, err) { + if(err) { + //console.log(err.toString()); + //console.log(err.stack); + inst.push(fdb.buffer('DIRECTORY_ERROR')); + + if(opsThatCreateDirs.indexOf(inst.op) >= 0) + self.dirList.push(null); + } + + return err; +}; + +module.exports = { + popTuples: popTuples, + pushError: pushError +}; diff --git a/bindings/nodejs/tests/get_range.js b/bindings/nodejs/tests/get_range.js new file mode 100755 index 0000000000..96b604081c --- /dev/null +++ b/bindings/nodejs/tests/get_range.js @@ -0,0 +1,62 @@ +/* + * get_range.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var fdb = require('../lib/fdb').apiVersion(200); + +var db = fdb.open(null, null) + +var tr = db.createTransaction(); + +for(var i = 0; i < 10000; i++) + tr.set('foo' + i, 'bar' + i) + +tr.commit(function(err) { + if(err) + console.log('commit error', err); + + console.log('get range: foo-fooa'); + var itr = tr.getRange('foo', 'fooa', null); + + itr.forEach( + function(val, cb) { + console.log(val.key.toString(), val.value.toString()); + cb(); + }, + function(err, res) { + if(err) + console.log(err); + else { + itr.forEach( + function(val, cb) { + console.log('pass2: ' + val.key.toString(), val.value.toString()); + cb(); + }, + function(err, res) { + if(err) + console.log(err); + else { } + } + ); + } + } + ); +}); + + diff --git a/bindings/nodejs/tests/get_versionstamp.js b/bindings/nodejs/tests/get_versionstamp.js new file mode 100644 index 0000000000..6b27fca673 --- /dev/null +++ b/bindings/nodejs/tests/get_versionstamp.js @@ -0,0 +1,59 @@ +/* + * get_versionstamp.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var bufferEqual = function (a, b) { + if (!Buffer.isBuffer(a)) return undefined; + if (!Buffer.isBuffer(b)) return undefined; + if (typeof a.equals === 'function') return a.equals(b); + if (a.length !== b.length) return false; + + for (var i = 0; i < a.length; i++) { + if (a[i] !== b[i]) return false; + } + + return true; +}; + +var fdb = require('../lib/fdb').apiVersion(410); + +var db = fdb.open(null, null) + +var tr = db.createTransaction(); + +tr.getVersionstamp(function(error, vs) { + db.get('foo', function(error, val) { + if(bufferEqual(val, vs)) + console.log('versionstamps match!') + else { + console.log("versionstamps don't match!") + console.log("database verionstamp: " + val) + console.log("transaction versionstamp: " + vs) + } + }); +}); + +tr.setVersionstampedValue('foo', 'blahblahbl') + +tr.commit(function(err) { + if(err) + console.log('commit error', err); + + console.log(tr.getCommittedVersion()) +}); diff --git a/bindings/nodejs/tests/promise_aplus_test.js b/bindings/nodejs/tests/promise_aplus_test.js new file mode 100644 index 0000000000..7bc59251fa --- /dev/null +++ b/bindings/nodejs/tests/promise_aplus_test.js @@ -0,0 +1,58 @@ +/* + * promise_aplus_test.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#!/usr/bin/env node + +"use strict"; + +var promisesAplusTests = require('promises-aplus-tests'); +var future = require('../lib/future.js'); + +var adapter = { + resolved: function(value) { + var f = future.create(); + f._state.fulfill(value); + return f; + }, + + rejected: function(reason) { + var f = future.create(); + f._state.reject(reason); + return f; + }, + + deferred: function() { + var f = future.create(); + + return { + promise: f, + resolve: function(value) { + f._state.fulfill(value); + }, + reject: function(reason) { + f._state.reject(reason); + }, + }; + } +}; + +promisesAplusTests(adapter, function(err) { + console.log('Finished tests:', err); +}); diff --git a/bindings/nodejs/tests/retry_test.js b/bindings/nodejs/tests/retry_test.js new file mode 100755 index 0000000000..cea49ceb8f --- /dev/null +++ b/bindings/nodejs/tests/retry_test.js @@ -0,0 +1,58 @@ +/* + * retry_test.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var fdb = require('../lib/fdb').apiVersion(200); + +function set(db, key, value, cb) { + db.doTransaction(function(tr, cb2) { + console.log("setting key"); + tr.set(new Buffer(key), new Buffer(value)); + cb2(null); + }, cb); +}; + +function setTxn(tr, key, value, cb) { + console.log('calling set'); + tr.set(new Buffer(key), new Buffer(value)); + cb(); +}; +setTxn = fdb.transactional(setTxn); + +function getAndClear(db, key, cb) { + db.doTransaction(function(tr, cb2) { + console.log("getting key"); + tr.get(new Buffer(key), function(err, res) { + tr.clear(new Buffer(key)); + //setTimeout(function() { cb2(err, res); }, 6000); + cb2(err, res); + }); + }, cb); +}; + +fdb.open(null, null, function(dbErr, db) { + console.log("created database", dbErr); + setTxn(db, 'foo', 'bar', function(err) { + console.log("Called transactional function", err); + getAndClear(db, 'foo', function(err, res) { + console.log("Called get and clear", err, res.toString()); + }); + }); +}); + diff --git a/bindings/nodejs/tests/streamline_async_test._js b/bindings/nodejs/tests/streamline_async_test._js new file mode 100755 index 0000000000..6299769de3 --- /dev/null +++ b/bindings/nodejs/tests/streamline_async_test._js @@ -0,0 +1,33 @@ +var fdb = require('../lib/fdb').apiVersion(200); + +function doSomething(_){ + console.log("start"); + db = fdb.open(null, null); + db = db(_); + db = fdb.open(null, null); + db = db(_); + + a = db.get('foo'); + console.log('foo = ', a(_)); + db.clear('foo', 'bar', _); + console.log('foo = ', db.get('foo', _)); + b = db.set('foo', 'bar'); + b(_); + console.log('foo = ', db.get('foo', _)); + + /*var tr = db.createTransaction(); + tr.set(new Buffer('foo'), new Buffer('bar')); + tr.commit(_); + + var a = tr.get(new Buffer('foo')) + var b = tr.get(new Buffer('bar')) + + console.log(a(_)); + console.log(b(_)); + + var c = tr.get(new Buffer('a')); + console.log(c(_));*/ +} + +doSomething(_); +console.log("after"); diff --git a/bindings/nodejs/tests/streamline_directory_extension._js b/bindings/nodejs/tests/streamline_directory_extension._js new file mode 100644 index 0000000000..9c3356fcef --- /dev/null +++ b/bindings/nodejs/tests/streamline_directory_extension._js @@ -0,0 +1,148 @@ +"use strict"; + +var fdb = require('../lib/fdb.js').apiVersion(parseInt(process.argv[3])); +var fdbUtil = require('../lib/fdbUtil.js'); +var dirUtil = require('./directory_util.js'); + +var StreamlineDirectoryExtension = function() { + this.dirList = [fdb.directory]; + this.dirIndex = 0; + this.errorIndex = 0; +}; + +StreamlineDirectoryExtension.prototype.processInstruction = function(inst, _) { + var directory = this.dirList[this.dirIndex]; + + try { + //console.log(inst.op); + + if(inst.op === 'DIRECTORY_CREATE_SUBSPACE') { + var path = dirUtil.popTuples(inst)(_); + var rawPrefix = inst.pop()(_); + this.dirList.push(new fdb.Subspace(path, rawPrefix)); + } + else if(inst.op === 'DIRECTORY_CREATE_LAYER') { + var params = inst.pop({count: 3})(_); + if(this.dirList[params[0]] === null || this.dirList[params[1]] === null) + this.dirList.push(null); + else { + this.dirList.push(new fdb.DirectoryLayer({ nodeSubspace: this.dirList[params[0]], + contentSubspace: this.dirList[params[1]], + allowManualPrefixes: params[2] })); + } + } + else if(inst.op === 'DIRECTORY_CHANGE') { + var index = inst.pop()(_); + if(this.dirList[index] === null) + this.dirIndex = this.errorIndex; + else + this.dirIndex = index; + } + else if(inst.op === 'DIRECTORY_SET_ERROR_INDEX') { + this.errorIndex = inst.pop()(_); + } + else if(inst.op === 'DIRECTORY_CREATE_OR_OPEN') { + var path = dirUtil.popTuples(inst)(_); + var layer = inst.pop()(_); + var dir = directory.createOrOpen(inst.tr, path, {'layer': layer || undefined})(_); + this.dirList.push(dir); + } + else if(inst.op === 'DIRECTORY_CREATE') { + var path = dirUtil.popTuples(inst)(_); + var params = inst.pop({count: 2})(_); + var dir = directory.create(inst.tr, path, {'layer': params[0] || undefined, 'prefix': params[1] || undefined})(_); + this.dirList.push(dir); + } + else if(inst.op === 'DIRECTORY_OPEN') { + var path = dirUtil.popTuples(inst)(_); + var layer = inst.pop()(_); + var dir = directory.open(inst.tr, path, {'layer': layer || undefined})(_); + this.dirList.push(dir); + } + else if(inst.op === 'DIRECTORY_MOVE') { + var paths = dirUtil.popTuples(inst, 2)(_); + var movedDir = directory.move(inst.tr, paths[0], paths[1])(_); + this.dirList.push(movedDir); + } + else if(inst.op === 'DIRECTORY_MOVE_TO') { + var newAbsolutePath = dirUtil.popTuples(inst)(_); + var movedDir = directory.moveTo(inst.tr, newAbsolutePath)(_); + this.dirList.push(movedDir); + } + else if(inst.op === 'DIRECTORY_REMOVE') { + var count = inst.pop()(_); + var path = dirUtil.popTuples(inst, count)(_); + directory.remove(inst.tr, path)(_); + } + else if(inst.op === 'DIRECTORY_REMOVE_IF_EXISTS') { + var count = inst.pop()(_); + var path = dirUtil.popTuples(inst, count)(_); + directory.removeIfExists(inst.tr, path)(_); + } + else if(inst.op === 'DIRECTORY_LIST') { + var count = inst.pop()(_); + var path = dirUtil.popTuples(inst, count)(_); + var children = directory.list(inst.tr, path)(_); + inst.push(fdb.tuple.pack(children)); + } + else if(inst.op === 'DIRECTORY_EXISTS') { + var count = inst.pop()(_); + var path = dirUtil.popTuples(inst, count)(_); + var exists = directory.exists(inst.tr, path)(_); + inst.push(exists ? 1 : 0); + } + else if(inst.op === 'DIRECTORY_PACK_KEY') { + var keyTuple = dirUtil.popTuples(inst)(_); + inst.push(directory.pack(keyTuple)); + } + else if(inst.op === 'DIRECTORY_UNPACK_KEY') { + var key = inst.pop()(_); + var tup = directory.unpack(key); + for(var i = 0; i < tup.length; ++i) + inst.push(tup[i]); + } + else if(inst.op === 'DIRECTORY_RANGE') { + var tup = dirUtil.popTuples(inst)(_); + var rng = directory.range(tup); + inst.push(rng.begin); + inst.push(rng.end); + } + else if(inst.op === 'DIRECTORY_CONTAINS') { + var key = inst.pop()(_); + inst.push(directory.contains(key) ? 1 : 0); + } + else if(inst.op === 'DIRECTORY_OPEN_SUBSPACE') { + var path = dirUtil.popTuples(inst)(_); + this.dirList.push(directory.subspace(path)); + } + else if(inst.op === 'DIRECTORY_LOG_SUBSPACE') { + var prefix = inst.pop()(_); + inst.tr.set(Buffer.concat([prefix, fdb.tuple.pack([this.dirIndex])]), directory.key()); + } + else if(inst.op === 'DIRECTORY_LOG_DIRECTORY') { + var prefix = inst.pop()(_); + var exists = directory.exists(inst.tr)(_); + var children = exists ? directory.list(inst.tr)(_) : []; + var logSubspace = new fdb.Subspace([this.dirIndex], prefix); + inst.tr.set(logSubspace.get('path'), fdb.tuple.pack(directory.getPath())); + inst.tr.set(logSubspace.get('layer'), fdb.tuple.pack([directory.getLayer()])); + inst.tr.set(logSubspace.get('exists'), fdb.tuple.pack([exists ? 1 : 0])); + inst.tr.set(logSubspace.get('children'), fdb.tuple.pack(children)); + } + else if(inst.op === 'DIRECTORY_STRIP_PREFIX') { + var str = inst.pop()(_); + if(!fdbUtil.buffersEqual(fdb.buffer(str).slice(0, directory.key().length), directory.key())) + throw new Error('String ' + str + ' does not start with raw prefix ' + directory.key()); + + inst.push(str.slice(directory.key().length)); + } + else { + throw new Error('Unknown op: ' + inst.op); + } + } + catch(err) { + dirUtil.pushError(this, inst, err); + } +}; + +module.exports = StreamlineDirectoryExtension; diff --git a/bindings/nodejs/tests/streamline_get_range._js b/bindings/nodejs/tests/streamline_get_range._js new file mode 100755 index 0000000000..de9c64cbc3 --- /dev/null +++ b/bindings/nodejs/tests/streamline_get_range._js @@ -0,0 +1,41 @@ +var fdb = require('../lib/fdb').apiVersion(200); + +db = fdb.open(null, null, _); + +var tr = db.createTransaction(); + +tr.set('foo1', 'bar1'); +tr.set('foo2', 'bar2'); +tr.set('foo3', 'bar3'); +tr.set('foo4', 'bar4'); +tr.set('foo5', 'bar5'); +tr.set('bar1', 'foo1'); +tr.set('bar2', 'foo2'); + +tr.commit(_); + +console.log('get range: foo1-foo4'); +var itr = tr.getRange('foo1', 'foo4', null); + +a = itr.forEach(function(val, cb) { + console.log(val.key.toString(), val.value.toString()); + cb(null, null); +}); + +console.log('get range starts with: foo'); +itr = tr.getRangeStartsWith('foo'); + +b = itr.forEachBatch(function(arr, cb) { + console.log('processing array', arr.length); + for(var i in arr) + console.log(arr[i].key.toString(), arr[i].value.toString()); + cb(null, null); +}); +c = itr.forEachBatch(function(arr, cb) { + console.log('processing array concurrent', arr.length); + for(var i in arr) + console.log(arr[i].key.toString(), arr[i].value.toString()); + cb(null, null); +}); + +console.log(a(_) + b(_) + c(_)); diff --git a/bindings/nodejs/tests/streamline_retry._js b/bindings/nodejs/tests/streamline_retry._js new file mode 100755 index 0000000000..99126a46f5 --- /dev/null +++ b/bindings/nodejs/tests/streamline_retry._js @@ -0,0 +1,48 @@ +var fdb = require('../lib/fdb').apiVersion(200); + +function set(db, key, value, _) { + db.doTransaction(function(tr, _) { + console.log("setting key"); + tr.set(key, value); + return; + }, _); +}; + +function getAndClear(db, key, _) { + a = db.doTransaction(function(tr, _) { + console.log("getting key"); + res = tr.get(key, _); + tr.clear(key); + return res; + }); + + b = db.doTransaction(function(tr, _) { + console.log("getting key"); + res = tr.get(key, _); + tr.clear(key); + return res; + }); + + return a(_) + b(_); +}; + +getAndClearTxn = fdb.transactional(function(tr, key, _) { + console.log("getting key"); + tr.getKey(fdb.KeySelector.firstGreaterOrEqual(key)); + res = tr.get(key, _); + tr.clear(key); + return res; +}); + +db = fdb.open(null, null, _); +set(db, 'foo', 'bar', _); + +//res = getAndClear(db, 'foo'); +//console.log("Called get and clear", res(_).toString()); + +a = getAndClearTxn(db, 'foo'); +b = getAndClearTxn(db, 'foo'); + +res = a(_) + b(_); +console.log("Result:", res.toString()); + diff --git a/bindings/nodejs/tests/streamline_tester._js b/bindings/nodejs/tests/streamline_tester._js new file mode 100755 index 0000000000..1a23134fbd --- /dev/null +++ b/bindings/nodejs/tests/streamline_tester._js @@ -0,0 +1,464 @@ +#!/usr/bin/env _node + +"use strict"; + +//cmd line: _node streamline_tester._js +var startTestPrefix = process.argv[2]; + +var assert = require('assert'); +var fdb = require('../lib/fdb.js').apiVersion(parseInt(process.argv[3])); +var fdbUtil = require('../lib/fdbUtil.js'); +var testerUtil = require('./util.js'); +var DirectoryExtension = require('./streamline_directory_extension._js'); + +var db = fdb.open(process.argv[4]); + +function pushError(inst, err) { + if(err) { + if(!err.code) + throw err; + + inst.push(fdb.tuple.pack([fdb.buffer('ERROR'), fdb.buffer(err.code.toString())])); + } + + return err; +} + +var rangeChoice = 0; +function pushRange(itr, inst, prefixFilter, _) { + var outArray = []; + + function pushKV(kv) { + if(typeof prefixFilter === 'undefined' || prefixFilter === null || fdbUtil.buffersEqual(kv.key.slice(0, prefixFilter.length), prefixFilter)) { + outArray.push(kv.key); + outArray.push(kv.value); + } + } + + //Test the different methods for getting data from a range + if(inst.isDatabase) { + for(var i = 0; i < itr.length; ++i) + pushKV(itr[i]); + } + else if(rangeChoice % 4 === 0) { + itr.forEachBatch(function(res, _) { + for(var i = 0; i < res.length; ++i) + pushKV(res[i]); + + if(rangeChoice % 8 === 0) + setTimeout(_, 0); + }, _); + } + else if(rangeChoice % 4 === 1) { + itr.forEach(function(res, _) { + pushKV(res); + if(rangeChoice % 8 === 1) + setTimeout(_, 0); + }, _); + } + else if(rangeChoice % 4 === 2) { + var arr = itr.toArray(_); + for(var i = 0; i < arr.length; ++i) + pushKV(arr[i]); + } + else { + fdbUtil.whileLoop(function(_) { + var kv = itr.next(_); + if(!kv) + return null; + else + pushKV(kv); + }, _); + } + + rangeChoice++; + inst.push(fdb.tuple.pack(outArray)); +} + +var waitEmpty = fdb.transactional(function(tr, prefix, _) { + var itr = tr.getRangeStartsWith(prefix, { limit: 1 }); + var arr = itr.toArray(_); + + if(arr.length > 0) + throw new fdb.FDBError('', 1020); +}); + +var testWatches = function(db, _) { + db.set('w0', '0', _); + db.set('w3', '3', _); + + var ready = [ false, false, false, false ]; + + var watches = []; + watches[0] = db.doTransaction(function(tr, _) { + return tr.watch('w0'); + }, _); + + watches[1] = db.clearAndWatch('w1', _).watch; + watches[2] = db.setAndWatch('w2', '2', _).watch; + watches[3] = db.getAndWatch('w3', _); + + assert.strictEqual(watches[3].value.toString(), '3', 'get and watch'); + watches[3] = watches[3].watch; + + for(var i = 0; i < watches.length; ++i) { + (function(i) { + watches[i](function(err) { if(!err) ready[i] = true; }); + })(i); + } + + function checkWatches(expected, testName) { + for(var i = 0; i < watches.length; ++i) + assert.strictEqual(ready[i], expected, 'testName' + i); + } + + setTimeout(_, 1000); + checkWatches(false, 'test 1'); + + db.set('w0', '0', _); + db.clear('w1', _); + + setTimeout(_, 5000); + checkWatches(false, 'test 2'); + + db.set('w0', 'a', _); + db.set('w1', 'b', _); + db.clear('w2', _); + db.xor('w3', fdb.buffer.fromByteLiteral('\xff\xff'), _); + + setTimeout(_, 2000); + checkWatches(true, 'test 3'); +}; + +var testLocality = function(_) { + db.doTransaction(function(tr, _) { + tr.options.setTimeout(60*1000); + tr.options.setReadSystemKeys(); + + var boundaryKeys = fdb.locality.getBoundaryKeys(tr, '', fdb.buffer.fromByteLiteral('\xff\xff'), _).toArray(_); + var success = true; + + for(var i = 0; i < boundaryKeys.length-1; ++i) { + var start = boundaryKeys[i]; + var end = tr.getKey(fdb.KeySelector.lastLessThan(boundaryKeys[i+1]), _); + var startAddresses = fdb.locality.getAddressesForKey(tr, start, _); + var endAddresses = fdb.locality.getAddressesForKey(tr, end, _); + for(var j = 0; j < startAddresses.length; ++j) { + var found = false; + for(var k = 0; k < endAddresses.length; ++k) { + if(startAddresses[j].toString() === endAddresses[k].toString()) { + found = true; + break; + } + } + + if(!found) { + success = false; + break; + } + } + + if(!success) + break; + } + + if(!success) + throw(new Error('Locality not internally consistent')); + }, _); +} + +var numOperations = 0; +function processOperation(context, inst, _) { + //if(inst.op !== 'SWAP' && inst.op !== 'PUSH') + //console.log(context.prefix + ':', context.instructionIndex + '.', inst.op); + + var params, numParams, res, itr; + + try { + if(inst.op === 'PUSH') + inst.push(inst.tokens[1]); + else if(inst.op === 'POP') + inst.pop()(_); + else if(inst.op === 'DUP') + context.stack.pushEntry(context.stack.get(context.stack.length()-1)); + else if(inst.op === 'EMPTY_STACK') + context.stack = new testerUtil.Stack(); + else if(inst.op === 'SWAP') { + var index = inst.pop()(_); + assert.strictEqual(context.stack.length() > index, true, 'Cannot swap; stack too small'); + index = context.stack.length() - index - 1; + if(context.stack.length() > index + 1) { + var tmp = context.stack.get(index); + context.stack.set(index, context.stack.popEntry()); + context.stack.pushEntry(tmp); + } + } + else if(inst.op === 'WAIT_FUTURE') { + var stackEntry = inst.pop({withMetadata: true})(_); + context.stack.pushEntry(stackEntry); + } + else if(inst.op === 'WAIT_EMPTY') { + var waitKey = inst.pop()(_); + waitEmpty(db, waitKey, _); + inst.push('WAITED_FOR_EMPTY'); + } + else if(inst.op === 'START_THREAD') { + var prefix = inst.pop()(_); + processTest(prefix, function(err, res) { + if(err) { + console.error('ERROR in Thread', prefix + ':'); + console.error(err.stack); + process.exit(1); + } + }); + } + else if(inst.op === 'NEW_TRANSACTION') { + context.newTransaction(); + } + else if(inst.op === 'USE_TRANSACTION') { + var name = inst.pop()(_); + context.switchTransaction(name); + } + else if(inst.op === 'SET') { + params = inst.pop({count: 2})(_); + + res = inst.tr.set(params[0], params[1]); + if(inst.isDatabase) + inst.push(res, true); + } + else if(inst.op === 'CLEAR') { + var key = inst.pop()(_); + + res = inst.tr.clear(key); + if(inst.isDatabase) + inst.push(res, true); + } + else if(inst.op === 'CLEAR_RANGE') { + params = inst.pop({count: 2})(_); + + res = inst.tr.clearRange(params[0], params[1]); + if(inst.isDatabase) + inst.push(res, true); + } + else if(inst.op === 'CLEAR_RANGE_STARTS_WITH') { + var prefix = inst.pop()(_); + + res = inst.tr.clearRangeStartsWith(prefix); + if(inst.isDatabase) + inst.push(res, true); + } + else if(inst.op === 'ATOMIC_OP') { + params = inst.pop({count: 3})(_); + + res = inst.tr[testerUtil.toJavaScriptName(params[0])](params[1], params[2]); + if(inst.isDatabase) + inst.push(res, true); + } + else if(inst.op === 'COMMIT') { + inst.push(inst.tr.commit(), true); + } + else if(inst.op === 'RESET') + inst.tr.reset(); + else if(inst.op === 'CANCEL') + inst.tr.cancel(); + else if(inst.op === 'GET') { + var key = inst.pop()(_); + inst.push(inst.tr.get(key), true); + } + else if(inst.op === 'GET_RANGE') { + params = inst.pop({count: 5})(_); + + if(inst.isDatabase) + itr = inst.tr.getRange(params[0], params[1], { limit: params[2], reverse: params[3], streamingMode: params[4] }, _); + else + itr = inst.tr.getRange(params[0], params[1], { limit: params[2], reverse: params[3], streamingMode: params[4] }); + + pushRange(itr, inst, undefined, _); + } + else if(inst.op === 'GET_RANGE_SELECTOR') { + params = inst.pop({count: 10})(_); + + var start = new fdb.KeySelector(params[0], params[1], params[2]); + var end = new fdb.KeySelector(params[3], params[4], params[5]); + + if(inst.isDatabase) + itr = inst.tr.getRange(start, end, { limit: params[6], reverse: params[7], streamingMode: params[8] }, _); + else + itr = inst.tr.getRange(start, end, { limit: params[6], reverse: params[7], streamingMode: params[8] }); + + pushRange(itr, inst, params[9], _); + } + else if(inst.op === 'GET_RANGE_STARTS_WITH') { + params = inst.pop({count: 4})(_); + + if(inst.isDatabase) + itr = inst.tr.getRangeStartsWith(params[0], { limit: params[1], reverse: params[2], streamingMode: params[3] }, _); + else + itr = inst.tr.getRangeStartsWith(params[0], { limit: params[1], reverse: params[2], streamingMode: params[3] }); + + pushRange(itr, inst, undefined, _); + } + else if(inst.op === 'GET_KEY') { + params = inst.pop({count: 4})(_); + var key = inst.tr.getKey(new fdb.KeySelector(params[0], params[1], params[2]), _); + + if(fdbUtil.buffersEqual(key.slice(0, params[3].length), params[3])) { + inst.push(key); + } + else if(fdb.buffer.toByteLiteral(key) < fdb.buffer.toByteLiteral(params[3])) { + inst.push(params[3]); + } + else { + inst.push(fdbUtil.strinc(params[3])); + } + } + else if(inst.op === 'READ_CONFLICT_RANGE') { + params = inst.pop({count: 2})(_); + inst.tr.addReadConflictRange(params[0], params[1]); + inst.push(fdb.buffer('SET_CONFLICT_RANGE')); + } + else if(inst.op === 'WRITE_CONFLICT_RANGE') { + params = inst.pop({count: 2})(_); + inst.tr.addWriteConflictRange(params[0], params[1]); + inst.push(fdb.buffer('SET_CONFLICT_RANGE')); + } + else if(inst.op === 'READ_CONFLICT_KEY') { + var key = inst.pop()(_); + inst.tr.addReadConflictKey(key); + inst.push(fdb.buffer('SET_CONFLICT_KEY')); + } + else if(inst.op === 'WRITE_CONFLICT_KEY') { + var key = inst.pop()(_); + inst.tr.addWriteConflictKey(key); + inst.push(fdb.buffer('SET_CONFLICT_KEY')); + } + else if(inst.op === 'DISABLE_WRITE_CONFLICT') { + inst.tr.options.setNextWriteNoWriteConflictRange(); + } + else if(inst.op === 'GET_READ_VERSION') { + context.lastVersion = inst.tr.getReadVersion(_); + inst.push(fdb.buffer('GOT_READ_VERSION')); + } + else if(inst.op === 'GET_COMMITTED_VERSION') { + context.lastVersion = inst.tr.getCommittedVersion(); + inst.push(fdb.buffer('GOT_COMMITTED_VERSION')); + } + else if(inst.op === 'GET_VERSIONSTAMP') { + inst.push(inst.tr.getVersionstamp(), true); + } + else if(inst.op === 'SET_READ_VERSION') { + assert.notStrictEqual(typeof context.lastVersion, 'undefined', 'Cannot set read version; version has never been read'); + inst.tr.setReadVersion(context.lastVersion); + } + else if(inst.op === 'ON_ERROR') { + var errorCode = inst.pop()(_); + var testErr = new fdb.FDBError('', errorCode); + + inst.push(inst.tr.onError(testErr), true); + } + else if(inst.op === 'TUPLE_PACK') { + numParams = inst.pop()(_); + params = inst.pop({count: numParams})(_); + inst.push(fdb.tuple.pack(params)); + } + else if(inst.op === 'TUPLE_UNPACK') { + var packedTuple = inst.pop()(_); + var arr = fdb.tuple.unpack(packedTuple); + for(var i = 0; i < arr.length; ++i) + inst.push(fdb.tuple.pack([arr[i]])); + } + else if(inst.op === 'TUPLE_RANGE') { + numParams = inst.pop()(_); + params = inst.pop({count: numParams})(_); + var range = fdb.tuple.range(params); + inst.push(range.begin); + inst.push(range.end); + } + else if(inst.op === 'SUB') { + params = inst.pop({count: 2})(_); + inst.push(params[0] - params[1]); + } + else if(inst.op === 'CONCAT') { + params = inst.pop({count: 2})(_); + if(Buffer.isBuffer(params[0])) { + inst.push(Buffer.concat([params[0], params[1]])) + } + else { + inst.push(params[0] + params[1]); + } + } + else if(inst.op === 'LOG_STACK') { + var prefix = inst.pop()(_); + var items = inst.pop({count: context.stack.length(), withMetadata: true})(_); + + for(var i = 0; i < items.length; ++i) { + if(i % 100 === 0) + inst.tr.commit(_); + inst.tr.reset(); + + var entry = items[items.length - i -1]; + var packedSubKey = fdb.tuple.pack([i, entry.instructionIndex]); + + var packedValue = fdb.tuple.pack([entry.item]); + if(packedValue.length > 40000) + packedValue = packedValue.slice(0, 40000); + + inst.tr.set(Buffer.concat([prefix, packedSubKey], prefix.length + packedSubKey.length), packedValue); + } + + inst.tr.commit(_); + inst.tr.reset(); + } + else if(inst.op === 'UNIT_TESTS') { + try { + db.options.setLocationCacheSize(100001); + db.doTransaction(function(tr, _) { + tr.options.setPrioritySystemImmediate(); + tr.options.setPriorityBatch(); + tr.options.setCausalReadRisky(); + tr.options.setCausalWriteRisky(); + tr.options.setReadYourWritesDisable(); + tr.options.setReadAheadDisable(); + tr.options.setReadSystemKeys(); + tr.options.setAccessSystemKeys(); + tr.options.setDurabilityDevNullIsWebScale(); + tr.options.setTimeout(1000); + tr.options.setRetryLimit(5); + tr.options.setMaxRetryDelay(100); + tr.options.setUsedDuringCommitProtectionDisable(); + tr.options.setTransactionLoggingEnable('my_transaction'); + + tr.get(fdb.buffer.fromByteLiteral('\xff'), _); + }, _); + + testWatches(db, _); + testLocality(_); + } + catch(err) { + throw('Unit tests failed: ' + err); + } + } + else if(testerUtil.startsWith(inst.op, 'DIRECTORY_')) { + context.directoryExtension.processInstruction(inst, _); + } + else + throw new Error('Unrecognized operation'); + } + catch(err) { + pushError(inst, err); + } +} + +function processTest(prefix, _) { + var context = new testerUtil.Context(db, prefix, processOperation, new DirectoryExtension()); + try { + context.run(_); + } + catch(err) { + console.error('ERROR during operation \'' + context.ops[context.current].value.toString() + '\':'); + console.error(err.stack); + process.exit(1); + } +} + +processTest(startTestPrefix, _); diff --git a/bindings/nodejs/tests/tester.js b/bindings/nodejs/tests/tester.js new file mode 100755 index 0000000000..a0e93e934e --- /dev/null +++ b/bindings/nodejs/tests/tester.js @@ -0,0 +1,714 @@ +/* + * tester.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#!/usr/bin/env node + +"use strict"; + +//cmd line: node tester.js +var startTestPrefix = process.argv[2]; +if(process.argv.length === 5) + var clusterFile = process.argv[4]; +else + var clusterFile = ''; + +var assert = require('assert'); +var fdb = require('../lib/fdb.js').apiVersion(parseInt(process.argv[3])); +var fdbUtil = require('../lib/fdbUtil.js'); +var testerUtil = require('./util.js'); +var DirectoryExtension = require('./directory_extension.js'); +//fdb.options.setTraceEnable() + +var db = fdb.open(clusterFile); + +function pushError(inst, err) { + if(err) { + if(!err.code) { + console.error('ERROR during operation \'' + inst.op + '\':'); + console.error(err.stack); + context.cb(err); + } + + inst.push(fdb.tuple.pack([fdb.buffer('ERROR'), fdb.buffer(err.code.toString())])); + } + + return err; +} + +var rangeChoice = 0; +function pushRange(itr, inst, prefixFilter, cb) { + return fdb.future.create(function(futureCb) { + var outArray = []; + + function pushKV(kv) { + if(typeof prefixFilter === 'undefined' || prefixFilter === null || fdbUtil.buffersEqual(kv.key.slice(0, prefixFilter.length), prefixFilter)) { + outArray.push(kv.key); + outArray.push(kv.value); + } + } + + function finish(err) { + if(!pushError(inst, err)) + inst.push(fdb.tuple.pack(outArray)); + + futureCb(); + } + + //Test different methods for getting a range + if(inst.isDatabase) { + for(var i = 0; i < itr.length; ++i) + pushKV(itr[i]); + + finish(); + } + else if(rangeChoice % 4 === 0) { + itr.forEachBatch(function(res, itrCb) { + for(var i = 0 ; i < res.length; ++i) + pushKV(res[i]); + + if(rangeChoice % 8 === 0) + setTimeout(itrCb, 0); + else + itrCb(); + }, function(err, res) { + finish(err); + }); + } + else if(rangeChoice % 4 === 1) { + itr.forEach(function(res, itrCb) { + pushKV(res); + + if(rangeChoice % 8 === 1) + setTimeout(itrCb, 0); + else + itrCb(); + }, function(err, res) { + finish(err); + }); + } + else if(rangeChoice % 4 === 2) { + itr.toArray(function(err, arr) { + if(!err) { + for(var i = 0; i < arr.length; ++i) + pushKV(arr[i]); + } + + finish(err); + }); + } + else { + fdbUtil.whileLoop(function(loopCb) { + itr.next(function(err, res) { + if(err) + loopCb(err); + else if(!res) + loopCb(undefined, null); + else { + pushKV(res); + loopCb(); + } + }); + }, finish); + } + + rangeChoice++; + })(cb); +} + +var waitEmpty = fdb.transactional(function(tr, prefix, cb) { + var itr = tr.getRangeStartsWith(prefix, { limit: 1 }); + itr.toArray(function(err, res) { + if(err) + cb(err, null); + else if(res.length > 0) + cb(new fdb.FDBError('', 1020), null); + else + cb(null, null); + }); +}); + +var timeoutFuture = function(time) { + return fdb.future.create(function(futureCb) { + setTimeout(futureCb, time); + }); +}; + +var checkWatches = function(db, watches, ready, error, expected, cb) { + var i = 0; + return fdbUtil.whileLoop(function(loopCb) { + if(i == watches.length) return loopCb(undefined, true); // terminate loop + if(!ready[i] && expected) { + return watches[i] + .then(function() { + loopCb(); // Recheck this watch when it finishes + }) + .catch(function() { + loopCb(); // Check the error + }); + } + assert.strictEqual(!ready[i] || expected, true, 'watch shouldnt be ready: ' + i); + if(typeof error[i] !== 'undefined') { + var tr = db.createTransaction(); + return tr.onError(error[i]) + .then(function() { + return false; + })(loopCb); + } + + i++; + loopCb(); + })(cb); +} + +var testWatches = function(db, cb) { + return fdbUtil.whileLoop(function(loopCb) { + var ready = [ false, false, false, false ]; + var error = [ undefined, undefined, undefined, undefined ]; + var watches = []; + + db.doTransaction(function(tr, innerCb) { + tr.set('w0', '0') + tr.set('w3', '3'); + innerCb(); + }) + .then(function() { + return db.doTransaction(function(tr, innerCb) { + watches[0] = tr.watch('w0'); + innerCb(); + }); + }) + .then(function() { + return db.clearAndWatch('w1'); + }) + .then(function(w) { + watches[1] = w.watch; + return db.setAndWatch('w2', '2'); + }) + .then(function(w) { + watches[2] = w.watch; + return db.getAndWatch('w3'); + }) + .then(function(w) { + assert.strictEqual(w.value.toString(), '3', 'get and watch'); + watches[3] = w.watch; + + for(var i = 0; i < watches.length; ++i) { + (function(i) { + watches[i](function(err) { + if(!err) ready[i] = true; + else error[i] = err; + }); + })(i); + } + + return timeoutFuture(1000); + }) + .then(function() { + return checkWatches(db, watches, ready, error, false); + }) + .then(function(result) { + if(!result) return; // go around the loop again + return db.doTransaction(function(tr, innerCb) { + tr.set('w0', '0'); + innerCb(); + }) + .then(function() { + return db.clear('w1'); + }) + .then(function() { + return timeoutFuture(5000); + }) + .then(function() { + return checkWatches(db, watches, ready, error, false); + }) + .then(function(result) { + if(!result) return; // go around the loop again + return db.set('w0', 'a') + .then(function() { + return db.set('w1', 'b'); + }) + .then(function() { + return db.clear('w2'); + }) + .then(function() { + return db.xor('w3', fdb.buffer.fromByteLiteral('\xff\xff')); + }) + .then(function() { + return timeoutFuture(2000); + }) + .then(function() { + return checkWatches(db, watches, ready, error, true); + }) + .then(function(result) { + if(result) return null; //terminate loop + return; + }); + }); + })(loopCb); + })(cb); +}; + +var testLocality = function(db, cb) { + return db.doTransaction(function(tr, innerCb) { + tr.options.setTimeout(60*1000); + tr.options.setReadSystemKeys(); + + fdb.locality.getBoundaryKeys(tr, '', fdb.buffer.fromByteLiteral('\xff\xff'), function(err, itr) { + if(err) return innerCb(err); + + var index = 0; + var start; + var end; + itr.forEach(function(boundaryKey, loopCb) { + if(err) return loopCb(err); + + start = end; + end = boundaryKey; + if(index++ == 0) + return loopCb(); + + tr.getKey(fdb.KeySelector.lastLessThan(end), function(err, end) { + if(err) return loopCb(err); + + fdb.locality.getAddressesForKey(tr, start, function(err, startAddresses) { + if(err) return loopCb(err); + + fdb.locality.getAddressesForKey(tr, end, function(err, endAddresses) { + if(err) return loopCb(err); + + for(var j = 0; j < startAddresses.length; ++j) { + var found = false; + for(var k = 0; k < endAddresses.length; ++k) { + if(startAddresses[j].toString() === endAddresses[k].toString()) { + found = true; + break; + } + } + + if(!found) { + return loopCb(new Error('Locality not internally consistent')); + } + } + + loopCb(); + }); + }); + }); + }, innerCb); + }); + }, cb); +}; + +var numOperations = 0; +function processOperation(context, inst, cb) { + //if(inst.op !== 'SWAP' && inst.op !== 'PUSH') + //console.log(context.prefix + ':', context.instructionIndex + '.', inst.op); + + var promiseCb = function(err) { + pushError(inst, err); + cb(); + }; + + if(inst.op === 'PUSH') { + inst.push(inst.tokens[1]); + cb(); + } + else if(inst.op === 'POP') { + inst.pop()(promiseCb); + } + else if(inst.op === 'DUP') { + context.stack.pushEntry(context.stack.get(context.stack.length()-1)); + cb(); + } + else if(inst.op === 'EMPTY_STACK') { + context.stack = new testerUtil.Stack(); + cb(); + } + else if(inst.op === 'SWAP') { + inst.pop() + .then(function(index) { + assert.strictEqual(context.stack.length() > index, true, 'Cannot swap; stack too small'); + index = context.stack.length() - index - 1; + if(context.stack.length() > index + 1) { + var tmp = context.stack.get(index); + context.stack.set(index, context.stack.popEntry()); + context.stack.pushEntry(tmp); + } + })(promiseCb); + } + else if(inst.op === 'WAIT_FUTURE') { + inst.pop({withMetadata: true}) + .then(function(stackEntry) { + context.stack.pushEntry(stackEntry); + })(promiseCb); + } + else if(inst.op === 'WAIT_EMPTY') { + inst.pop() + .then(function(waitKey) { + return waitEmpty(db, waitKey) + .then(function() { + inst.push('WAITED_FOR_EMPTY'); + }); + })(promiseCb); + } + else if(inst.op === 'START_THREAD') { + inst.pop() + .then(function(prefix) { + processTest(prefix, function(err, res) { + if(err) { + console.error('ERROR in Thread', prefix + ':'); + console.error(err.stack); + process.exit(1); + } + }); + })(promiseCb); + } + else if(inst.op === 'NEW_TRANSACTION') { + context.newTransaction(); + cb(); + } + else if(inst.op === 'USE_TRANSACTION') { + inst.pop() + .then(function(name) { + context.switchTransaction(name); + })(promiseCb); + } + else if(inst.op === 'SET') { + inst.pop({count: 2}) + .then(function(params) { + var res = inst.tr.set(params[0], params[1]); + + if(inst.isDatabase) + inst.push(res, true); + })(promiseCb); + } + else if(inst.op === 'CLEAR') { + inst.pop() + .then(function(key) { + var res = inst.tr.clear(key); + + if(inst.isDatabase) + inst.push(res, true); + })(promiseCb); + } + else if(inst.op === 'CLEAR_RANGE') { + inst.pop({count: 2}) + .then(function(params) { + var res = inst.tr.clearRange(params[0], params[1]); + + if(inst.isDatabase) + inst.push(res, true); + })(promiseCb); + } + else if(inst.op === 'CLEAR_RANGE_STARTS_WITH') { + inst.pop() + .then(function(prefix) { + var res = inst.tr.clearRangeStartsWith(prefix); + + if(inst.isDatabase) + inst.push(res, true); + })(promiseCb); + } + else if(inst.op === 'ATOMIC_OP') { + inst.pop({count: 3}) + .then(function(params) { + var res = inst.tr[testerUtil.toJavaScriptName(params[0])](params[1], params[2]); + + if(inst.isDatabase) + inst.push(res, true); + })(promiseCb); + } + else if(inst.op === 'COMMIT') { + inst.push(inst.tr.commit(), true); + cb(); + } + else if(inst.op === 'RESET') { + inst.tr.reset(); + cb(); + } + else if(inst.op === 'CANCEL') { + inst.tr.cancel(); + cb(); + } + else if(inst.op === 'GET') { + inst.pop() + .then(function(key) { + inst.push(inst.tr.get(key), true); + })(promiseCb); + } + else if(inst.op === 'GET_RANGE') { + inst.pop({count: 5}) + .then(function(params) { + var itr = inst.tr.getRange(params[0], params[1], { limit: params[2], reverse: params[3], streamingMode: params[4] }); + if(inst.isDatabase) { + return itr.then(function(arr) { + return pushRange(arr, inst); + }); + } + else { + return pushRange(itr, inst); + } + })(promiseCb); + } + else if(inst.op === 'GET_RANGE_SELECTOR') { + inst.pop({count: 10}) + .then(function(params) { + var start = new fdb.KeySelector(params[0], params[1], params[2]); + var end = new fdb.KeySelector(params[3], params[4], params[5]); + var itr = inst.tr.getRange(start, end, { limit: params[6], reverse: params[7], streamingMode: params[8] }); + if(inst.isDatabase) { + return itr.then(function(arr) { + return pushRange(arr, inst, params[9]); + }); + } + else { + return pushRange(itr, inst, params[9]); + } + })(promiseCb); + } + else if(inst.op === 'GET_RANGE_STARTS_WITH') { + inst.pop({count: 4}) + .then(function(params) { + var itr = inst.tr.getRangeStartsWith(params[0], { limit: params[1], reverse: params[2], streamingMode: params[3] }); + if(inst.isDatabase) { + return itr.then(function(arr) { + return pushRange(arr, inst); + }); + } + else { + return pushRange(itr, inst); + } + })(promiseCb); + } + else if(inst.op === 'GET_KEY') { + inst.pop({count: 4}) + .then(function(params) { + var result = inst.tr.getKey(new fdb.KeySelector(params[0], params[1], params[2])) + .then(function(key) { + if(fdbUtil.buffersEqual(key.slice(0, params[3].length), params[3])) { + return key; + } + else if(fdb.buffer.toByteLiteral(key) < fdb.buffer.toByteLiteral(params[3])) { + return params[3]; + } + else { + return fdbUtil.strinc(params[3]); + } + }); + + inst.push(result, true); + })(promiseCb); + } + else if(inst.op === 'READ_CONFLICT_RANGE') { + inst.pop({count: 2}) + .then(function(params) { + inst.tr.addReadConflictRange(params[0], params[1]); + inst.push(fdb.buffer('SET_CONFLICT_RANGE')); + })(promiseCb); + } + else if(inst.op === 'WRITE_CONFLICT_RANGE') { + inst.pop({count: 2}) + .then(function(params) { + inst.tr.addWriteConflictRange(params[0], params[1]); + inst.push(fdb.buffer('SET_CONFLICT_RANGE')); + })(promiseCb); + } + else if(inst.op === 'READ_CONFLICT_KEY') { + inst.pop() + .then(function(key) { + inst.tr.addReadConflictKey(key); + inst.push(fdb.buffer('SET_CONFLICT_KEY')); + })(promiseCb); + } + else if(inst.op === 'WRITE_CONFLICT_KEY') { + inst.pop() + .then(function(key) { + inst.tr.addWriteConflictKey(key); + inst.push(fdb.buffer('SET_CONFLICT_KEY')); + })(promiseCb); + } + else if(inst.op === 'DISABLE_WRITE_CONFLICT') { + inst.tr.options.setNextWriteNoWriteConflictRange(); + cb(); + } + else if(inst.op === 'GET_READ_VERSION') { + inst.tr.getReadVersion(function(err, res) { + if(!pushError(inst, err)) { + context.lastVersion = res; + inst.push(fdb.buffer('GOT_READ_VERSION')); + } + cb(); + }); + } + else if(inst.op === 'GET_COMMITTED_VERSION') { + try { + context.lastVersion = inst.tr.getCommittedVersion(); + + inst.push(fdb.buffer('GOT_COMMITTED_VERSION')); + cb(); + } + catch(err) { + pushError(inst, err); + cb(); + } + } + else if(inst.op === 'GET_VERSIONSTAMP') { + try { + inst.push(inst.tr.getVersionstamp(), true) + cb(); + } + catch(err) { + pushError(inst, err); + cb(); + } + } + else if(inst.op === 'SET_READ_VERSION') { + assert.notStrictEqual(typeof context.lastVersion, 'undefined', 'Cannot set read version; version has never been read'); + inst.tr.setReadVersion(context.lastVersion); + cb(); + } + else if(inst.op === 'ON_ERROR') { + inst.pop() + .then(function(errorCode) { + var testErr = new fdb.FDBError('', errorCode); + + inst.push(inst.tr.onError(testErr), true); + })(promiseCb); + } + else if(inst.op === 'TUPLE_PACK') { + inst.pop() + .then(function(numParams) { + return inst.pop({count: numParams}) + .then(function(params) { + inst.push(fdb.tuple.pack(params)); + }); + })(promiseCb); + } + else if(inst.op === 'TUPLE_UNPACK') { + inst.pop() + .then(function(packedTuple) { + var arr = fdb.tuple.unpack(packedTuple); + for(var i = 0; i < arr.length; ++i) + inst.push(fdb.tuple.pack([arr[i]])); + })(promiseCb); + } + else if(inst.op === 'TUPLE_RANGE') { + inst.pop() + .then(function(numParams) { + return inst.pop({count: numParams}) + .then(function(params) { + var range = fdb.tuple.range(params); + inst.push(range.begin); + inst.push(range.end); + }); + })(promiseCb); + } + else if(inst.op === 'SUB') { + inst.pop({count: 2}) + .then(function(params) { + inst.push(params[0] - params[1]); + })(promiseCb); + } + else if(inst.op === 'CONCAT') { + inst.pop({count: 2}) + .then(function(params) { + if(Buffer.isBuffer(params[0])) { + inst.push(Buffer.concat([params[0], params[1]])) + } + else { + inst.push(params[0] + params[1]); + } + })(promiseCb); + } + else if(inst.op === 'LOG_STACK') { + inst.pop() + .then(function(prefix) { + return fdbUtil.whileLoop(function(loopCb) { + inst.pop({count: 100, withMetadata: true}) + .then(function(items) { + if(items.length == 0) { + return null + } + return db.doTransaction(function(tr, innerCb) { + for(var index = 0; index < items.length; ++index) { + var entry = items[items.length - index - 1]; + var packedSubKey = fdb.tuple.pack([context.stack.length() + index, entry.instructionIndex]); + + var packedValue = fdb.tuple.pack([entry.item]); + if(packedValue.length > 40000) + packedValue = packedValue.slice(0, 40000); + + tr.set(Buffer.concat([prefix, packedSubKey], prefix.length + packedSubKey.length), packedValue); + } + + innerCb(); + }); + })(loopCb); + }); + })(promiseCb); + } + else if(inst.op === 'UNIT_TESTS') { + db.options.setLocationCacheSize(100001); + db.doTransaction(function(tr, innerCb) { + tr.options.setPrioritySystemImmediate(); + tr.options.setPriorityBatch(); + tr.options.setCausalReadRisky(); + tr.options.setCausalWriteRisky(); + tr.options.setReadYourWritesDisable(); + tr.options.setReadAheadDisable(); + tr.options.setReadSystemKeys(); + tr.options.setAccessSystemKeys(); + tr.options.setDurabilityDevNullIsWebScale(); + tr.options.setTimeout(60*1000); + tr.options.setRetryLimit(50); + tr.options.setMaxRetryDelay(100); + tr.options.setUsedDuringCommitProtectionDisable(); + tr.options.setTransactionLoggingEnable('my_transaction'); + + tr.get(fdb.buffer.fromByteLiteral('\xff'), innerCb); + }) + .then(function() { + return testWatches(db); + }) + .then(function() { + return testLocality(db); + }) + .then(cb) + .catch(function(err) { + cb('Unit tests failed: ' + err + "\n" + err.stack); + }); + } + else if(testerUtil.startsWith(inst.op, 'DIRECTORY_')) { + context.directoryExtension.processInstruction(inst, cb); + } + else { + cb('Unrecognized operation'); + } +} + +function processTest(prefix, cb) { + var context = new testerUtil.Context(db, prefix, processOperation, new DirectoryExtension()); + context.run(cb); +} + +processTest(startTestPrefix, function(err, res) { + if(err) + process.exit(1); +}); diff --git a/bindings/nodejs/tests/tuple_test.js b/bindings/nodejs/tests/tuple_test.js new file mode 100755 index 0000000000..7edd08b3c9 --- /dev/null +++ b/bindings/nodejs/tests/tuple_test.js @@ -0,0 +1,40 @@ +/* + * tuple_test.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var fdb = require('../lib/fdb.js').apiVersion(200); + +console.log(fdb.tuple.pack([-Math.pow(2,53)])); +console.log(fdb.tuple.pack([-Math.pow(2,53)+1])); + +console.log(fdb.tuple.unpack(fdb.tuple.pack([-Math.pow(2,53)]))); +console.log(fdb.tuple.unpack(fdb.tuple.pack([-Math.pow(2,53)+1]))); + +try { + console.log(fdb.tuple.unpack(fdb.buffer.fromByteLiteral('\x0d\xdf\xff\xff\xff\xff\xff\xfe'))); +} +catch(err) { + console.log(err); +} + +console.log(fdb.tuple.pack([0xff * 0xff])); +console.log(fdb.tuple.pack([0xffffffff + 100 ])); +console.log(fdb.tuple.unpack(fdb.buffer.fromByteLiteral('\x1a\xff\xff\xff\xff\xff\xff'))); +console.log(fdb.tuple.unpack(fdb.tuple.pack(['TEST', 'herp', 1, -10, 393493, '\u0000abc', 0xffffffff + 100]))); +console.log(fdb.tuple.range(['TEST', 1])); diff --git a/bindings/nodejs/tests/util.js b/bindings/nodejs/tests/util.js new file mode 100644 index 0000000000..57e987d524 --- /dev/null +++ b/bindings/nodejs/tests/util.js @@ -0,0 +1,276 @@ +/* + * util.js + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +"use strict"; + +var fdb = require('../lib/fdb.js').apiVersion(parseInt(process.argv[3])); +var util = require('../lib/fdbUtil.js'); + +function Stack() { + this.stack = []; +} + +Stack.prototype.length = function() { + return this.stack.length; +}; + +Stack.prototype.get = function(index) { + return this.stack[index]; +}; + +Stack.prototype.set = function(index, val) { + this.stack[index] = val; +}; + +Stack.prototype.push = function(instructionIndex, item, isFuture) { + if(typeof isFuture === 'undefined') + isFuture = false; + + this.pushEntry({ instructionIndex: instructionIndex, item: item, isFuture: isFuture }); +}; + +Stack.prototype.pushEntry = function(entry) { + this.stack.push(entry); +}; + +Stack.prototype.pop = function(options, callback) { + var self = this; + return fdb.future.create(function(futureCb) { + if(typeof options === 'undefined') + options = {}; + + var count = options.count; + if(typeof count === 'undefined') + count = 1; + + var params = self.stack.slice(self.stack.length-count).reverse(); + self.stack = self.stack.slice(0, self.stack.length-count); + + var index = 0; + + var itemCallback = function(err, val) { + if(err) { + //console.log(err); + params[index].item = fdb.tuple.pack([fdb.buffer('ERROR'), fdb.buffer(err.code.toString())]); + } + else if(val) + params[index].item = val; + else + params[index].item = fdb.buffer('RESULT_NOT_PRESENT'); + + params[index].isFuture = false; + + if(!options.withMetadata) + params[index] = params[index].item; + + index++; + processNext(); + }; + + var processNext = function() { + while(true) { + if(index >= params.length) { + if(typeof options.count === 'undefined') + futureCb(undefined, params[0]); + else + futureCb(undefined, params); + + return; + } + + if(params[index].isFuture) { + params[index].item(itemCallback); + return; + } + + if(!options.withMetadata) + params[index] = params[index].item; + + index++; + } + }; + + processNext(); + })(callback); +}; + +Stack.prototype.popEntry = function() { + return this.stack.pop(); +}; + +function Context(db, prefix, processInstruction, directoryExtension) { + var range = fdb.tuple.range([fdb.buffer(prefix)]); + + this.prefix = prefix; + this.stack = new Stack(); + this.db = db; + this.next = range.begin; + this.end = range.end; + this.processInstruction = processInstruction; + this.instructionIndex = -1; + this.directoryExtension = directoryExtension; + this.trName = prefix; +} + +Context.trMap = {} + +Context.prototype.newTransaction = function() { + Context.trMap[this.trName] = this.db.createTransaction(); +}; + +Context.prototype.switchTransaction = function(name) { + this.trName = name; + if(typeof Context.trMap[this.trName] === 'undefined') { + this.newTransaction(); + } +}; + +Context.prototype.updateResults = function(results) { + this.ops = results; + this.current = 0; + this.next = fdb.KeySelector.firstGreaterThan(results[results.length-1].key); +}; + +var issueInstruction = function(context, cb) { + try { + var tokens = fdb.tuple.unpack(context.ops[context.current].value); + var op = tokens[0].toString(); + + var snapshotStr = '_SNAPSHOT'; + var databaseStr = '_DATABASE'; + + var isSnapshot = endsWith(op, snapshotStr); + var isDatabase = endsWith(op, databaseStr); + + var tr = Context.trMap[context.trName]; + if(isSnapshot) { + op = op.substr(0, op.length - snapshotStr.length); + tr = tr.snapshot; + } + else if(isDatabase) { + op = op.substr(0, op.length - databaseStr.length); + tr = context.db; + } + + var inst = new Instruction(context, tr, op, tokens, isDatabase, isSnapshot); + context.processInstruction(context, inst, cb); + } + catch(e) { + cb(e); + } +}; + +Context.prototype.run = function(cb) { + var self = this; + + function getInstructions(instCb) { + self.db.doTransaction(function(tr, trCb) { + tr.getRange(self.next, self.end, { limit: 1000 } ).toArray(function(rangeErr, rangeRes) { + if(rangeErr) return trCb(rangeErr); + + trCb(undefined, rangeRes); + }); + }, function(err, rangeRes) { + if(err) return instCb(err); + if(rangeRes.length > 0) + self.updateResults(rangeRes); + instCb(); + }); + } + + function readAndExecuteInstructions(loopCb) { + ++self.instructionIndex; + if(!self.ops || ++self.current === self.ops.length) { + getInstructions(function(err) { + if(err) return loopCb(err); + if(self.current < self.ops.length) + issueInstruction(self, loopCb); + else + loopCb(undefined, null); // terminate the loop + }); + } + else + issueInstruction(self, loopCb); + } + + util.whileLoop(readAndExecuteInstructions, function(err) { + if(err) { + if(self.ops && self.current < self.ops.length) + console.error('ERROR during operation \'' + self.ops[self.current].value.toString() + '\':'); + else + console.error('ERROR getting operations:'); + + if(err.stack) + console.error(err.stack); + else + console.error(err); + } + + cb(err); + }); +}; + +function Instruction(context, tr, op, tokens, isDatabase, isSnapshot) { + this.context = context; + this.tr = tr; + this.op = op; + this.tokens = tokens; + this.isDatabase = isDatabase; + this.isSnapshot = isSnapshot; +} + +Instruction.prototype.pop = function(options, callback) { + return this.context.stack.pop(options, callback); +}; + +Instruction.prototype.push = function(item, isFuture) { + this.context.stack.push(this.context.instructionIndex, item, isFuture); +}; + +function toJavaScriptName(name) { + name = name.toString().toLowerCase(); + var start = 0; + while(start < name.length) { + start = name.indexOf('_', start); + if(start === -1) + break; + + name = name.slice(0, start) + name[start+1].toUpperCase() + name.slice(start+2); + } + + return name.replace(/_/g, ''); +} + +function startsWith(str, prefixStr) { + return str.length >= prefixStr.length && str.substr(0, prefixStr.length) === prefixStr; +} + +function endsWith(str, endStr) { + return str.length >= endStr.length && str.substr(str.length - endStr.length) === endStr; +} + +module.exports = { + Stack: Stack, + Context: Context, + Instruction: Instruction, + toJavaScriptName: toJavaScriptName, + startsWith: startsWith, + endsWith: endsWith +}; diff --git a/bindings/python/MANIFEST.in b/bindings/python/MANIFEST.in new file mode 100644 index 0000000000..a5021c60e3 --- /dev/null +++ b/bindings/python/MANIFEST.in @@ -0,0 +1,2 @@ +include README.rst +include LICENSE diff --git a/bindings/python/README.rst b/bindings/python/README.rst new file mode 100644 index 0000000000..45c236f398 --- /dev/null +++ b/bindings/python/README.rst @@ -0,0 +1,3 @@ +Complete documentation of the FoundationDB Python API can be found at https://foundationdb.org/documentation/api-python.html. + +These bindings require the FoundationDB client. The client can be obtained from https://files.foundationdb.org/fdb-c/. diff --git a/bindings/python/fdb/__init__.py b/bindings/python/fdb/__init__.py new file mode 100644 index 0000000000..4beab564e4 --- /dev/null +++ b/bindings/python/fdb/__init__.py @@ -0,0 +1,120 @@ +# +# __init__.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# FoundationDB Python API + +"""Documentation for this API can be found at +https://foundationdb.org/documentation/api-python.html""" + +def open(*args, **kwargs): + raise RuntimeError('You must call api_version() before using any fdb methods') + +init = open + +def transactional(*args, **kwargs): + raise RuntimeError('You must call api_version() before using fdb.transactional') + +def _add_symbols(module, symbols): + for symbol in symbols: + globals()[symbol] = getattr(module, symbol) + +def api_version(ver): + header_version = 500 + + if '_version' in globals(): + if globals()['_version'] != ver: + raise RuntimeError('FDB API already loaded at version %d' % _version) + return + + if ver < 13: + raise RuntimeError('FDB API versions before 13 are not supported') + + if ver > header_version: + raise RuntimeError('Latest known FDB API version is %d' % header_version) + + import fdb.impl + + err = fdb.impl._capi.fdb_select_api_version_impl(ver, header_version) + if err == 2203: # api_version_not_supported, but that's not helpful to the user + max_supported_ver = fdb.impl._capi.fdb_get_max_api_version() + if header_version > max_supported_ver: + raise RuntimeError("This version of the FoundationDB Python binding is not supported by the installed FoundationDB C library. The binding requires a library that supports API version %d, but the installed library supports a maximum version of %d." % (header_version, max_supported_ver)) + + else: + raise RuntimeError("API version %d is not supported by the installed FoundationDB C library." % ver) + + elif err != 0: + raise RuntimeError('FoundationDB API error') + + list = ( + 'FDBError', + 'predicates', + 'Future', + 'Cluster', + 'Database', + 'Transaction', + 'KeyValue', + 'KeySelector', + 'init', + 'create_cluster', + 'open', + 'transactional', + 'options', + 'StreamingMode', + ) + + _add_symbols(fdb.impl, list) + + if ver > 22: + import fdb.locality + + if ver == 13: + globals()["open"] = getattr(fdb.impl, "open_v13") + globals()["init"] = getattr(fdb.impl, "init_v13") + + # Future.get got renamed to Future.wait in v14 to make room for + # Database.get, we have to undo that here + for name in dir(fdb.impl): + o = getattr(fdb.impl, name) + try: + if issubclass(o, fdb.impl.Future): + if hasattr(o, "wait"): + o.get = o.wait + except TypeError: pass + + # FDBRange used to be called FDBRangeIter and was an iterator, + # but it's now a container. In v13 we have to make it act like + # an iterator again. + def next(self): + if not hasattr(self, "__iterating"): + self.__iterating = iter(self) + return next(self.__iterating) + setattr(fdb.impl.FDBRange, "next", next) + + globals()['_version'] = ver + + import fdb.directory_impl + directory_symbols = ('directory', 'DirectoryLayer',) + _add_symbols(fdb.directory_impl, directory_symbols) + + import fdb.subspace_impl + subspace_symbols = ('Subspace',) + _add_symbols(fdb.subspace_impl, subspace_symbols) + diff --git a/bindings/python/fdb/directory_impl.py b/bindings/python/fdb/directory_impl.py new file mode 100755 index 0000000000..acaf93384d --- /dev/null +++ b/bindings/python/fdb/directory_impl.py @@ -0,0 +1,626 @@ +# +# directory_impl.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# FoundationDB Python API + +import random +import struct +import threading + +from fdb import impl as _impl +from fdb import six +import fdb.tuple +from .subspace_impl import Subspace + +class AllocatorTransactionState: + def __init__(self): + self.lock = threading.Lock() + +class HighContentionAllocator (object): + + def __init__(self, subspace): + self.counters = subspace[0] + self.recent = subspace[1] + self.lock = threading.Lock(); + + @_impl.transactional + def allocate(self, tr): + """Returns a byte string that + 1) has never and will never be returned by another call to this + method on the same subspace + 2) is nearly as short as possible given the above + """ + + # Get transaction-local state + if not hasattr(tr, "__fdb_directory_layer_hca_state__"): + with self.lock: + if not hasattr(tr, "__fdb_directory_layer_hca_state__"): + tr.__fdb_directory_layer_hca_state__ = AllocatorTransactionState(); + + tr_state = tr.__fdb_directory_layer_hca_state__ + + while True: + [start] = [self.counters.unpack(k)[0] for k,_ in tr.snapshot.get_range(self.counters.range().start, self.counters.range().stop, limit=1, reverse=True)] or [0] + + window_advanced = False + while True: + with tr_state.lock: + if window_advanced: + del tr[self.counters : self.counters[start]] + tr.options.set_next_write_no_write_conflict_range() + del tr[self.recent : self.recent[start]] + + # Increment the allocation count for the current window + tr.add(self.counters[start], struct.pack(" 0 and latest_counter[0] > start: + break + + if candidate_value == None: + tr.add_write_conflict_key(self.recent[candidate]) + return fdb.tuple.pack((candidate,)) + + def _window_size(self, start): + # Larger window sizes are better for high contention, smaller sizes for + # keeping the keys small. But if there are many allocations, the keys + # can't be too small. So start small and scale up. We don't want this + # to ever get *too* big because we have to store about window_size/2 + # recent items. + if start < 255: return 64 + if start < 65535: return 1024 + return 8192 + +class Directory(object): + def __init__(self, directory_layer, path=(), layer=b''): + self._directory_layer = directory_layer + self._path = path + self._layer = layer + + @_impl.transactional + def create_or_open(self, tr, path, layer=None): + path = self._tuplify_path(path) + return self._directory_layer.create_or_open(tr, self._partition_subpath(path), layer) + + @_impl.transactional + def open(self, tr, path, layer=None): + path = self._tuplify_path(path) + return self._directory_layer.open(tr, self._partition_subpath(path), layer) + + @_impl.transactional + def create(self, tr, path, layer=None, prefix=None): + path = self._tuplify_path(path) + return self._directory_layer.create(tr, self._partition_subpath(path), layer, prefix) + + @_impl.transactional + def list(self, tr, path=()): + path = self._tuplify_path(path) + return self._directory_layer.list(tr, self._partition_subpath(path)) + + @_impl.transactional + def move(self, tr, old_path, new_path): + old_path = self._tuplify_path(old_path) + new_path = self._tuplify_path(new_path) + return self._directory_layer.move(tr, self._partition_subpath(old_path), self._partition_subpath(new_path)) + + @_impl.transactional + def move_to(self, tr, new_absolute_path): + directory_layer = self._get_layer_for_path(()) + new_absolute_path = _to_unicode_path(new_absolute_path) + partition_len = len(directory_layer._path) + partition_path = new_absolute_path[:partition_len] + if partition_path != directory_layer._path: + raise ValueError("Cannot move between partitions.") + + return directory_layer.move(tr, self._path[partition_len:], new_absolute_path[partition_len:]) + + @_impl.transactional + def remove(self, tr, path=()): + path = self._tuplify_path(path) + directory_layer = self._get_layer_for_path(path) + return directory_layer.remove(tr, self._partition_subpath(path, directory_layer)) + + @_impl.transactional + def remove_if_exists(self, tr, path=()): + path = self._tuplify_path(path) + directory_layer = self._get_layer_for_path(path) + return directory_layer.remove_if_exists(tr, self._partition_subpath(path, directory_layer)) + + @_impl.transactional + def exists(self, tr, path=()): + path = self._tuplify_path(path) + directory_layer = self._get_layer_for_path(path) + return directory_layer.exists(tr, self._partition_subpath(path, directory_layer)) + + def get_layer(self): + return self._layer + + def get_path(self): + return self._path + + def _tuplify_path(self, path): + if not isinstance(path, tuple): + path = (path,) + return path + + def _partition_subpath(self, path, directory_layer = None): + directory_layer = directory_layer or self._directory_layer + return self._path[len(directory_layer._path):] + path + + # Called by all functions that could operate on this subspace directly (move_to, remove, remove_if_exists, exists) + # Subclasses can choose to return a different directory layer to use for the operation if path is in fact () + def _get_layer_for_path(self, path): + return self._directory_layer + +class DirectoryLayer(Directory): + + def __init__(self, node_subspace=Subspace(rawPrefix=b'\xfe'), content_subspace=Subspace(), allow_manual_prefixes=False): + Directory.__init__(self, self) + + # If specified, new automatically allocated prefixes will all fall within content_subspace + self._content_subspace = content_subspace + self._node_subspace = node_subspace + + self._allow_manual_prefixes = allow_manual_prefixes + + # The root node is the one whose contents are the node subspace + self._root_node = self._node_subspace[self._node_subspace.key()] + self._allocator = HighContentionAllocator(self._root_node[b'hca']) + + @_impl.transactional + def create_or_open(self, tr, path, layer=None): + """ Opens the directory with the given path. + + If the directory does not exist, it is created (creating parent + directories if necessary). + + If layer is specified, it is checked against the layer of an existing + directory or set as the layer of a new directory. + """ + return self._create_or_open_internal(tr, path, layer) + + def _create_or_open_internal(self, tr, path, layer=None, prefix=None, allow_create=True, allow_open=True): + self._check_version(tr, write_access=False) + + if prefix is not None and not self._allow_manual_prefixes: + if len(self._path) == 0: + raise ValueError("Cannot specify a prefix unless manual prefixes are enabled.") + else: + raise ValueError("Cannot specify a prefix in a partition.") + + path = _to_unicode_path(path) + + if not path: + # Root directory contains node metadata and so cannot be opened. + raise ValueError("The root directory cannot be opened.") + + existing_node = self._find(tr, path).prefetch_metadata(tr) + if existing_node.exists(): + if existing_node.is_in_partition(): + subpath = existing_node.get_partition_subpath() + return existing_node.get_contents(self)._directory_layer._create_or_open_internal(tr, subpath, layer, prefix, allow_create, allow_open) + + if not allow_open: + raise ValueError("The directory already exists.") + + if layer and existing_node.layer() != layer: + raise ValueError("The directory was created with an incompatible layer.") + + return existing_node.get_contents(self) + + if not allow_create: + raise ValueError("The directory does not exist.") + + self._check_version(tr) + + if prefix == None: + prefix = self._content_subspace.key() + self._allocator.allocate(tr) + + if len(list(tr.get_range_startswith(prefix, limit=1))) > 0: + raise Exception("The database has keys stored at the prefix chosen by the automatic prefix allocator: %r." % prefix) + + if not self._is_prefix_free(tr.snapshot, prefix): + raise Exception("The directory layer has manually allocated prefixes that conflict with the automatic prefix allocator.") + + elif not self._is_prefix_free(tr, prefix): + raise ValueError("The given prefix is already in use.") + + if len(path) > 1: + parent_node = self._node_with_prefix(self.create_or_open(tr, path[:-1]).key()) + else: + parent_node = self._root_node + if not parent_node: + # print repr(path[:-1]) + raise ValueError("The parent directory doesn't exist.") + + node = self._node_with_prefix(prefix) + tr[parent_node[self.SUBDIRS][path[-1]]] = prefix + if not layer: + layer = b'' + + tr[node[b'layer']] = layer + + return self._contents_of_node(node, path, layer) + + @_impl.transactional + def open(self, tr, path, layer=None): + """ Opens the directory with the given path. + + An error is raised if the directory does not exist, or if a layer is + specified and a different layer was specified when the directory was + created. + """ + return self._create_or_open_internal(tr, path, layer, allow_create=False) + + @_impl.transactional + def create(self, tr, path, layer=None, prefix=None): + """Creates a directory with the given path (creating parent directories + if necessary). + + An error is raised if the given directory already exists. + + If prefix is specified, the directory is created with the given physical + prefix; otherwise a prefix is allocated automatically. + + If layer is specified, it is recorded with the directory and will be + checked by future calls to open. + """ + return self._create_or_open_internal(tr, path, layer, prefix, allow_open=False) + + @_impl.transactional + def move_to(self, tr, new_absolute_path): + raise Exception('The root directory cannot be moved.') + + @_impl.transactional + def move(self, tr, old_path, new_path): + """Moves the directory found at `old_path` to `new_path`. + + There is no effect on the physical prefix of the given directory, or on + clients that already have the directory open. + + An error is raised if the old directory does not exist, a directory + already exists at `new_path`, or the parent directory of `new_path` does + not exist. + """ + self._check_version(tr) + + old_path = _to_unicode_path(old_path) + new_path = _to_unicode_path(new_path) + + if old_path == new_path[:len(old_path)]: + raise ValueError("The destination directory cannot be a subdirectory of the source directory.") + + old_node = self._find(tr, old_path).prefetch_metadata(tr) + new_node = self._find(tr, new_path).prefetch_metadata(tr) + + if not old_node.exists(): + raise ValueError("The source directory does not exist.") + + if old_node.is_in_partition() or new_node.is_in_partition(): + if not old_node.is_in_partition() or not new_node.is_in_partition() or old_node.path != new_node.path: + raise ValueError("Cannot move between partitions.") + + return new_node.get_contents(self).move(tr, old_node.get_partition_subpath(), new_node.get_partition_subpath()) + + if new_node.exists(): + raise ValueError("The destination directory already exists. Remove it first.") + + parent_node = self._find(tr, new_path[:-1]) + if not parent_node.exists(): + raise ValueError("The parent of the destination directory does not exist. Create it first.") + tr[parent_node.subspace[self.SUBDIRS][new_path[-1]]] = self._node_subspace.unpack(old_node.subspace.key())[0] + self._remove_from_parent(tr, old_path) + return self._contents_of_node(old_node.subspace, new_path, old_node.layer()) + + @_impl.transactional + def remove(self, tr, path=()): + """Removes the directory, its contents, and all subdirectories. + Throws an exception if the directory does not exist. + + Warning: Clients that have already opened the directory might still + insert data into its contents after it is removed. + """ + return self._remove_internal(tr, path, fail_on_nonexistent=True) + + @_impl.transactional + def remove_if_exists(self, tr, path=()): + """Removes the directory, its contents, and all subdirectories, if + it exists. Returns true if the directory existed and false otherwise. + + Warning: Clients that have already opened the directory might still + insert data into its contents after it is removed. + """ + return self._remove_internal(tr, path, fail_on_nonexistent=False) + + def _remove_internal(self, tr, path, fail_on_nonexistent): + self._check_version(tr) + + if not path: + raise ValueError("The root directory cannot be removed.") + + path = _to_unicode_path(path) + node = self._find(tr, path).prefetch_metadata(tr) + + if not node.exists(): + if fail_on_nonexistent: + raise ValueError("The directory does not exist.") + else: + return False + + if node.is_in_partition(): + return node.get_contents(self)._directory_layer._remove_internal(tr, node.get_partition_subpath(), fail_on_nonexistent) + + self._remove_recursive(tr, node.subspace) + self._remove_from_parent(tr, path) + return True + + @_impl.transactional + def list(self, tr, path=()): + """Returns the names of the specified directory's subdirectories as a + list of strings. + """ + self._check_version(tr, write_access=False) + + path = _to_unicode_path(path) + node = self._find(tr, path).prefetch_metadata(tr) + if not node.exists(): + raise ValueError("The directory does not exist.") + + if node.is_in_partition(include_empty_subpath=True): + return node.get_contents(self).list(tr, node.get_partition_subpath()) + + return [name for name, cnode in self._subdir_names_and_nodes(tr, node.subspace)] + + @_impl.transactional + def exists(self, tr, path=()): + """Returns whether or not the specified directory exists.""" + self._check_version(tr, write_access=False) + + path = _to_unicode_path(path) + node = self._find(tr, path).prefetch_metadata(tr) + + if not node.exists(): + return False + + if node.is_in_partition(): + return node.get_contents(self).exists(tr, node.get_partition_subpath()) + + return True + + ######################################## + ## Private methods for implementation ## + ######################################## + + SUBDIRS=0 + VERSION=(1,0,0) + + def _check_version(self, tr, write_access=True): + version = tr[self._root_node[b'version']] + + if not version.present(): + if write_access: + self._initialize_directory(tr) + + return + + version = struct.unpack(' self.VERSION[0]: + raise Exception("Cannot load directory with version %d.%d.%d using directory layer %d.%d.%d" % (version + self.VERSION)) + + if version[1] > self.VERSION[1] and write_access: + raise Exception("Directory with version %d.%d.%d is read-only when opened using directory layer %d.%d.%d" % (version + self.VERSION)) + + def _initialize_directory(self, tr): + tr[self._root_node[b'version']] = struct.pack(' len(self.path)) + + def get_partition_subpath(self): + return self.target_path[len(self.path):] + + def get_contents(self, directory_layer, tr=None): + return directory_layer._contents_of_node(self.subspace, self.path, self.layer(tr)) + diff --git a/bindings/python/fdb/fdb_c.dll.pth b/bindings/python/fdb/fdb_c.dll.pth new file mode 100644 index 0000000000..226b3946ec --- /dev/null +++ b/bindings/python/fdb/fdb_c.dll.pth @@ -0,0 +1 @@ +../../../bin/release/fdb_c.dll \ No newline at end of file diff --git a/bindings/python/fdb/impl.py b/bindings/python/fdb/impl.py new file mode 100644 index 0000000000..8f657138be --- /dev/null +++ b/bindings/python/fdb/impl.py @@ -0,0 +1,1625 @@ +# +# impl.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# FoundationDB Python API + +import ctypes +import ctypes.util +import functools +import threading +import traceback +import inspect +import datetime +import platform +import os +import sys + +from fdb import six + +_network_thread = None +_network_thread_reentrant_lock = threading.RLock() + +_open_file = open + +import weakref + +class _NetworkOptions(object): + def __init__(self, parent): + self._parent = parent + +class _ErrorPredicates(object): + def __init__(self, parent): + self._parent = parent + +class _ClusterOptions(object): + def __init__(self, cluster): + self._parent = weakref.proxy(cluster) + +class _DatabaseOptions(object): + def __init__(self, db): + self._parent = weakref.proxy(db) + +class _TransactionOptions(object): + def __init__(self, tr): + self._parent = weakref.proxy(tr) + +from fdb import fdboptions as _opts +import types +import struct + +def option_wrap(code): + def setfunc(self): + self._parent._set_option(code, None, 0) + return setfunc + +def option_wrap_string(code): + def setfunc(self, param=None): + param, length = optionalParamToBytes(param) + self._parent._set_option(code, param, length) + return setfunc + +def option_wrap_bytes(code): + def setfunc(self, param=None): + if param is None: + self._parent._set_option(code, None, 0) + elif isinstance(param, bytes): + self._parent._set_option(code, param, len(param)) + else: + raise TypeError('Value must be of type ' + bytes.__name__) + return setfunc + +def option_wrap_int(code): + def setfunc(self, param): + self._parent._set_option(code, struct.pack("= 1: + #td = now - start + #print ('fdb WARNING: long transaction (%gs elapsed in transactional function \'%s\' (%d retries, %s))' % (elapsed, func.__name__, retries, committed and 'committed' or 'not yet committed')) + #last = now + + #retries += 1 + return ret + return wrapper + + if not tr_args: + # Being called with parameters (possibly none); return a + # decorator + return decorate + elif len(tr_args) == 1 and not tr_kwargs: + # Being called as a decorator + return decorate(tr_args[0]) + else: + raise Exception('Invalid use of transactional decorator.') + +class FDBError(Exception): + """This exception is raised when an FDB API call returns an + error. The error code will be stored in the code attribute, and a + textual description of the error will be stored in the description + attribute. + + """ + def __init__(self, code): + self.code = code + self._description = None + + @property + def description(self): + if not self._description: + self._description = _capi.fdb_get_error(self.code) + return self._description + + def __str__(self): + return '%s (%d)' % (self.description, self.code) + + def __repr__(self): + return 'FDBError(%d)' % self.code + + +class _FDBBase(object): + # By inheriting from _FDBBase, every class gets access to self.capi + # (set below when we open libfdb_capi) + pass + + +class FDBRange(object): + """Iterates over the results of an FDB range query. Returns + KeyValue objects. + + """ + + def __init__(self, tr, begin, end, limit, reverse, streaming_mode): + self._tr = tr + + self._bsel = begin + self._esel = end + + self._limit = limit + self._reverse = reverse + self._mode = streaming_mode + + self._future = self._tr._get_range(begin, end, limit, streaming_mode, 1, reverse) + + def to_list(self): + if self._mode == StreamingMode.iterator: + if self._limit > 0: + mode = StreamingMode.exact + else: + mode = StreamingMode.want_all + else: + mode = self._mode + + return list(self.__iter__(mode=mode)) + + def __iter__(self, mode=None): + if mode is None: + mode = self._mode + bsel = self._bsel + esel = self._esel + limit = self._limit + + iteration = 1 # the first read was fired off when the FDBRange was initialized + future = self._future + + done = False + + while not done: + if future: + (kvs, count, more) = future.wait() + index = 0 + future = None + + if not count: + return + + result = kvs[index] + index += 1 + + if index == count: + if not more or limit == count: + done = True + else: + iteration += 1 + if limit > 0: + limit = limit - count + if self._reverse: + esel = KeySelector.first_greater_or_equal(kvs[-1].key) + else: + bsel = KeySelector.first_greater_than(kvs[-1].key) + future = self._tr._get_range(bsel, esel, limit, mode, iteration, self._reverse) + + yield result + + +class TransactionRead(_FDBBase): + def __init__(self, tpointer, db, snapshot): + self.tpointer = tpointer + self.db = db + self._snapshot = snapshot + + def __del__(self): + # print('Destroying transactionread 0x%x' % self.tpointer) + self.capi.fdb_transaction_destroy(self.tpointer) + + def get_read_version(self): + """Get the read version of the transaction.""" + return FutureVersion(self.capi.fdb_transaction_get_read_version(self.tpointer)) + + def get(self, key): + key = keyToBytes(key) + return Value(self.capi.fdb_transaction_get(self.tpointer, key, len(key), self._snapshot)) + + def get_key(self, key_selector): + key = keyToBytes(key_selector.key) + + return Key(self.capi.fdb_transaction_get_key( + self.tpointer, key, len(key), key_selector.or_equal, key_selector.offset, self._snapshot)) + + def _get_range(self, begin, end, limit, streaming_mode, iteration, reverse): + beginKey = keyToBytes(begin.key) + endKey = keyToBytes(end.key) + + return FutureKeyValueArray( + self.capi.fdb_transaction_get_range( + self.tpointer, beginKey, len(beginKey), begin.or_equal, begin.offset, + endKey, len(endKey), end.or_equal, end.offset, + limit, 0, streaming_mode, iteration, self._snapshot, reverse)) + + def _to_selector(self, key_or_selector): + if not isinstance(key_or_selector, KeySelector): + key_or_selector = KeySelector.first_greater_or_equal(key_or_selector) + return key_or_selector + + def get_range(self, begin, end, limit=0, reverse=False, streaming_mode=StreamingMode.iterator): + if begin is None: begin = b'' + if end is None: end = b'\xff' + begin = self._to_selector(begin) + end = self._to_selector(end) + return FDBRange(self, begin, end, limit, reverse, streaming_mode) + + def get_range_startswith(self, prefix, *args, **kwargs): + prefix = keyToBytes(prefix) + return self.get_range(prefix, strinc(prefix), *args, **kwargs) + + def __getitem__(self, key): + if isinstance(key, slice): + return self.get_range(key.start, key.stop, reverse=(key.step == -1)) + return self.get(key) + +class Transaction(TransactionRead): + """A modifiable snapshot of a Database. + + """ + + def __init__(self, tpointer, db): + super(Transaction, self).__init__(tpointer, db, False) + self.options = _TransactionOptions(self) + self.__snapshot = self.snapshot = TransactionRead(tpointer, db, True) + + def __del__(self): + pass + + def set_read_version(self, version): + """Set the read version of the transaction.""" + self.capi.fdb_transaction_set_read_version(self.tpointer, version) + + def _set_option(self, option, param, length): + self.capi.fdb_transaction_set_option(self.tpointer, option, param, length) + + def _atomic_operation(self, opcode, key, param): + paramBytes = valueToBytes(param) + paramLength = len(paramBytes) + keyBytes = keyToBytes(key) + keyLength = len(keyBytes) + self.capi.fdb_transaction_atomic_op(self.tpointer, keyBytes, keyLength, paramBytes, paramLength, opcode) + + def set(self, key, value): + key = keyToBytes(key) + value = valueToBytes(value) + self.capi.fdb_transaction_set(self.tpointer, key, len(key), value, len(value)) + + def clear(self, key): + if isinstance(key, KeySelector): + key = self.get_key(key) + + key = keyToBytes(key) + + self.capi.fdb_transaction_clear(self.tpointer, key, len(key)) + + def clear_range(self, begin, end): + if begin is None: begin = b'' + if end is None: end = b'\xff' + if isinstance(begin, KeySelector): + begin = self.get_key(begin) + if isinstance(end, KeySelector): + end = self.get_key(end) + + begin = keyToBytes(begin) + end = keyToBytes(end) + + self.capi.fdb_transaction_clear_range(self.tpointer, begin, len(begin), + end, len(end)) + + def clear_range_startswith(self, prefix): + prefix = keyToBytes(prefix) + return self.clear_range(prefix, strinc(prefix)) + + def watch(self, key): + key = keyToBytes(key) + return FutureVoid(self.capi.fdb_transaction_watch(self.tpointer, key, len(key))) + + def add_read_conflict_range(self, begin, end): + begin = keyToBytes(begin) + end = keyToBytes(end) + self.capi.fdb_transaction_add_conflict_range(self.tpointer, begin, len(begin), end, len(end), ConflictRangeType.read) + + def add_read_conflict_key(self, key): + key = keyToBytes(key) + self.add_read_conflict_range(key, key + b'\x00') + + def add_write_conflict_range(self, begin, end): + begin = keyToBytes(begin) + end = keyToBytes(end) + self.capi.fdb_transaction_add_conflict_range(self.tpointer, begin, len(begin), end, len(end), ConflictRangeType.write) + + def add_write_conflict_key(self, key): + key = keyToBytes(key) + self.add_write_conflict_range(key, key + b'\x00') + + def commit(self): + return FutureVoid(self.capi.fdb_transaction_commit(self.tpointer)) + + def get_committed_version(self): + version = ctypes.c_int64() + self.capi.fdb_transaction_get_committed_version(self.tpointer, ctypes.byref(version)) + return version.value + + def get_versionstamp(self): + return Key(self.capi.fdb_transaction_get_versionstamp(self.tpointer)) + + def on_error(self, error): + if isinstance(error, FDBError): + code = error.code + elif isinstance(error, int): + code = error + else: + raise error + return FutureVoid(self.capi.fdb_transaction_on_error(self.tpointer, code)) + + def reset(self): + self.capi.fdb_transaction_reset(self.tpointer) + + def cancel(self): + self.capi.fdb_transaction_cancel(self.tpointer) + + def __setitem__(self, key, value): + self.set(key, value) + + def __delitem__(self, key): + if isinstance(key, slice): + self.clear_range( key.start, key.stop ) + else: + self.clear(key) + +class Future(_FDBBase): + Event = threading.Event + _state = None #< Hack for trollius + + def __init__(self, fpointer): + # print('Creating future 0x%x' % fpointer) + self.fpointer = fpointer + + def __del__(self): + if self.fpointer: + # print('Destroying future 0x%x' % self.fpointer) + self.capi.fdb_future_destroy(self.fpointer) + self.fpointer = None + + def cancel(self): + self.capi.fdb_future_cancel(self.fpointer) + + def _release_memory(self): + self.capi.fdb_future_release_memory(self.fpointer) + + def wait(self): + raise NotImplementedError + + def is_ready(self): + return bool( self.capi.fdb_future_is_ready(self.fpointer) ) + + def block_until_ready(self): + self.capi.fdb_future_block_until_ready(self.fpointer) + + # Depending on the event_model, block_until_ready may be remapped to do something asynchronous or + # just fail. really_block_until_ready() is always fdb_future_block_until_ready() and is used e.g. + # for database and cluster futures that should always be available very quickly + really_block_until_ready = block_until_ready + + def on_ready(self, callback): + def cb_and_delref(ignore): + _unpin_callback(cbfunc[0]) + del cbfunc[:] + try: + callback(self) + except: + try: + sys.stderr.write("Discarding uncaught exception from user FDB callback:\n") + traceback.print_exception(*sys.exc_info(), file=sys.stderr) + except: + pass + cbfunc = [_CBFUNC(cb_and_delref)] + del cb_and_delref + _pin_callback(cbfunc[0]) + self.capi.fdb_future_set_callback(self.fpointer, cbfunc[0], None) + + @staticmethod + def wait_for_any(*futures): + """Does not return until at least one of the given futures is ready. + Returns the index in the parameter list of a ready future.""" + if not futures: + raise ValueError("wait_for_any requires at least one future") + d={} + ev = futures[0].Event() + for i,f in enumerate(futures): + def cb(ignore,i=i): + if d.setdefault('i', i) == i: + ev.set() + f.on_ready( cb ) + ev.wait() + return d['i'] + + # asyncio future protocol + def cancelled(self): + if not self.done(): return False + e = self.exception() + return getattr(e, 'code', 0) == 1101 + done = is_ready + def result(self): + if not self.done(): raise Exception("Future result not available") + return self.wait() + def exception(self): + if not self.done(): raise Exception("Future result not available") + try: + self.wait() + return None + except BaseException as e: + return e + def add_done_callback(self,fn): + self.on_ready(lambda f: self.call_soon_threadsafe(fn, f)) + def remove_done_callback(self,fn): + raise NotImplementedError() + + +class FutureVoid(Future): + def wait(self): + self.block_until_ready() + self.capi.fdb_future_get_error(self.fpointer) + return None + + +class FutureVersion(Future): + def wait(self): + self.block_until_ready() + version = ctypes.c_int64() + self.capi.fdb_future_get_version(self.fpointer, ctypes.byref(version)) + return version.value + + +class FutureKeyValueArray(Future): + def wait(self): + self.block_until_ready() + kvs = ctypes.pointer(KeyValueStruct()) + count = ctypes.c_int() + more = ctypes.c_int() + self.capi.fdb_future_get_keyvalue_array(self.fpointer, ctypes.byref(kvs), ctypes.byref(count), ctypes.byref(more)) + return ([KeyValue(ctypes.string_at(x.key, x.key_length), ctypes.string_at(x.value, x.value_length)) for x in kvs[0:count.value]], count.value, more.value) + + # Logically, we should self._release_memory() after extracting the + # KVs but before returning, but then we would have to store + # the KVs on the python side and in most cases we are about to + # destroy the future anyway + +class FutureStringArray(Future): + def wait(self): + self.block_until_ready() + strings = ctypes.pointer(ctypes.c_char_p()) + count = ctypes.c_int() + self.capi.fdb_future_get_string_array(self.fpointer, ctypes.byref(strings), ctypes.byref(count)) + return list(strings[0:count.value]) + +class replaceable_property(object): + def __get__(self, obj, cls=None): + return self.method(obj) + + def __init__(self, method): + self.method = method + +class LazyFuture(Future): + def __init__(self, *args, **kwargs): + super(LazyFuture, self).__init__(*args, **kwargs) + + def wait(self): + self.value + return self + + def _getter(self): + raise NotImplementedError + + @replaceable_property + def value(self): + self.block_until_ready() + + try: + self._getter() + self._release_memory() + + except: + e = sys.exc_info() + if not (isinstance(e[1], FDBError) and e[1].code == 1102): # future_released + raise + + return self.value + + +# This is a workaround to avoid a compiler issue as described here: +# http://bugs.python.org/issue12370 +_super = super + +class FutureString(LazyFuture): + def __init__(self, *args): + self._error = None + _super(FutureString, self).__init__(*args) + + def getclass(self): + return bytes + __class__ = property(getclass) + + def as_foundationdb_key(self): + return self.value + + def as_foundationdb_value(self): + return self.value + + def __str__(self): + return self.value.__str__() + + def __bytes__(self): + return self.value + + def __repr__(self): + return self.value.__repr__() + + def __add__(self, rhs): + if isinstance(rhs, FutureString): + rhs = rhs.value + return self.value + rhs + + def __radd__(self, lhs): + if isinstance(lhs, FutureString): + lhs = lhs.value + return lhs + self.value + + def __mul__(self, rhs): + return self.value * rhs + + def __rmul__(self, lhs): + return lhs * self.value + + def __lt__(self, rhs): + if isinstance(rhs, FutureString): + rhs = rhs.value + return self.value < rhs + + def __le__(self, rhs): + if isinstance(rhs, FutureString): + rhs = rhs.value + return self.value <= rhs + + def __gt__(self, rhs): + if isinstance(rhs, FutureString): + rhs = rhs.value + return self.value > rhs + + def __ge__(self, rhs): + if isinstance(rhs, FutureString): + rhs = rhs.value + return self.value >= rhs + + def __eq__(self, rhs): + if isinstance(rhs, FutureString): + rhs = rhs.value + return self.value == rhs + + def __ne__(self, rhs): + return not self == rhs + + def __nonzero__(self): + return bool(self.value) + + def __int__(self): + return int(self.value) + +def makewrapper(func): + def tmpfunc(self, *args): + return func(self.value, *args) + return tmpfunc + +for i in dir(bytes): + if not i.startswith('_') or i in ('__getitem__','__getslice__','__hash__', '__len__'): + setattr(FutureString, i, makewrapper(getattr(bytes, i))) + + +class Value(FutureString): + def _getter(self): + present = ctypes.c_int() + value = ctypes.pointer(ctypes.c_byte()) + value_length = ctypes.c_int() + self.capi.fdb_future_get_value(self.fpointer, ctypes.byref(present), + ctypes.byref(value), ctypes.byref(value_length)) + if present.value: + self.value = ctypes.string_at(value, value_length.value) + else: + self.value = None + + def present(self): + return self.value is not None + + +class Key(FutureString): + def _getter(self): + key = ctypes.pointer(ctypes.c_byte()) + key_length = ctypes.c_int() + self.capi.fdb_future_get_key(self.fpointer, ctypes.byref(key), ctypes.byref(key_length)) + self.value = ctypes.string_at(key, key_length.value) + + +class FormerFuture(_FDBBase): + def wait(self): + return self + + def is_ready(self): + return True + + def block_until_ready(self): + pass + + def on_ready(self, callback): + try: + callback(self) + except: + try: + sys.stderr.write("Discarding uncaught exception from user FDB callback:\n") + traceback.print_exception(*sys.exc_info(), file=sys.stderr) + except: + pass + + +class Database(FormerFuture): + def __init__(self, dpointer): + self.dpointer = dpointer + self.options = _DatabaseOptions(self) + + def __del__(self): + # print('Destroying database 0x%x' % self.dpointer) + self.capi.fdb_database_destroy(self.dpointer) + + def get(self, key): + return Database.__database_getitem(self, key) + + def __getitem__(self, key): + if isinstance(key, slice): + return self.get_range(key.start, key.stop, reverse=(key.step==-1)) + return Database.__database_getitem(self, key) + + + def get_key(self, key_selector): + return Database.__database_get_key(self, key_selector) + + def get_range(self, begin, end, limit=0, reverse=False, streaming_mode=StreamingMode.want_all): + return Database.__database_get_range(self, begin, end, limit, reverse, streaming_mode) + + def get_range_startswith(self, prefix, *args, **kwargs): + return Database.__database_get_range_startswith(self, prefix, *args, **kwargs) + + def set(self, key, value): + Database.__database_setitem(self, key, value) + + def __setitem__(self, key, value): + Database.__database_setitem(self, key, value) + + def clear(self, key): + Database.__database_delitem(self, key) + + def clear_range(self, begin, end): + Database.__database_delitem(self, slice(begin, end)) + + def __delitem__(self, key_or_slice): + Database.__database_delitem(self, key_or_slice) + + def clear_range_startswith(self, prefix): + Database.__database_clear_range_startswith(self, prefix) + + def get_and_watch(self, key): + return Database.__database_get_and_watch(self, key) + + def set_and_watch(self, key, value): + return Database.__database_set_and_watch(self, key, value) + + def clear_and_watch(self, key): + return Database.__database_clear_and_watch(self, key) + + def create_transaction(self): + pointer = ctypes.c_void_p() + self.capi.fdb_database_create_transaction(self.dpointer, ctypes.byref(pointer)) + return Transaction(pointer.value, self) + + def _set_option(self, option, param, length): + self.capi.fdb_database_set_option(self.dpointer, option, param, length) + + def _atomic_operation(self, opcode, key, param): + Database.__database_atomic_operation(self, opcode, key, param) + + #### Transaction implementations #### + @staticmethod + @transactional + def __database_getitem(tr, key): + return tr[key].value + + @staticmethod + @transactional + def __database_get_key(tr, key_selector): + return tr.get_key(key_selector).value + + @staticmethod + @transactional + def __database_get_range(tr, begin, end, limit, reverse, streaming_mode): + return tr.get_range(begin, end, limit, reverse, streaming_mode).to_list() + + @staticmethod + @transactional + def __database_get_range_startswith(tr, prefix, *args, **kwargs): + return tr.get_range_startswith(prefix, *args, **kwargs).to_list() + + @staticmethod + @transactional + def __database_setitem(tr, key, value): + tr[key] = value + + @staticmethod + @transactional + def __database_clear_range_startswith(tr, prefix): + tr.clear_range_startswith(prefix) + + @staticmethod + @transactional + def __database_get_and_watch(tr, key): + v = tr.get(key) + return v, tr.watch(key) + + @staticmethod + @transactional + def __database_set_and_watch(tr, key, value): + tr.set(key, value) + return tr.watch(key) + + @staticmethod + @transactional + def __database_clear_and_watch(tr, key): + del tr[key] + return tr.watch(key) + + @staticmethod + @transactional + def __database_delitem(tr, key_or_slice): + del tr[key_or_slice] + + @staticmethod + @transactional + def __database_atomic_operation(tr, opcode, key, param): + tr._atomic_operation(opcode, key, param) + + ### Asynchronous transactions + @staticmethod + def declare_asynchronous_transactions(): + Return = asyncio.Return + From = asyncio.From + coroutine = asyncio.coroutine + class Database: + @staticmethod + @transactional + @coroutine + def __database_getitem(tr, key): + #raise Return(( yield From( tr[key] ) )) + raise Return( tr[key] ) + yield None + + @staticmethod + @transactional + @coroutine + def __database_get_key(tr, key_selector): + raise Return( tr.get_key(key_selector) ) + yield None + + @staticmethod + @transactional + @coroutine + def __database_get_range(tr, begin, end, limit, reverse, streaming_mode): + raise Return(( yield From( tr.get_range(begin, end, limit, reverse, streaming_mode).to_list() ) )) + + @staticmethod + @transactional + @coroutine + def __database_get_range_startswith(tr, prefix, *args, **kwargs): + raise Return(( yield From( tr.get_range_startswith(prefix, *args, **kwargs).to_list() ))) + + @staticmethod + @transactional + @coroutine + def __database_setitem(tr, key, value): + tr[key] = value + raise Return() + yield None + + @staticmethod + @transactional + @coroutine + def __database_clear_range_startswith(tr, prefix): + tr.clear_range_startswith(prefix) + raise Return() + yield None + + @staticmethod + @transactional + @coroutine + def __database_get_and_watch(tr, key): + v = tr.get(key) + raise Return( v, tr.watch(key) ) + yield None + + @staticmethod + @transactional + @coroutine + def __database_set_and_watch(tr, key, value): + tr.set(key, value) + raise Return( tr.watch(key) ) + yield None + + @staticmethod + @transactional + @coroutine + def __database_clear_and_watch(tr, key): + del tr[key] + raise Return( tr.watch(key) ) + yield None + + @staticmethod + @transactional + @coroutine + def __database_delitem(tr, key_or_slice): + del tr[key_or_slice] + raise Return() + yield None + + @staticmethod + @transactional + @coroutine + def __database_atomic_operation(tr, opcode, key, param): + tr._atomic_operation(opcode, key, param) + raise Return() + yield None + return Database + +fill_operations() + +class Cluster(FormerFuture): + def __init__(self, cpointer): + self.cpointer = cpointer + self.options = _ClusterOptions(self) + + def __del__(self): + # print('Destroying cluster 0x%x' % self.cpointer) + self.capi.fdb_cluster_destroy(self.cpointer) + + def open_database(self, name): + name = paramToBytes(name) + f = Future(self.capi.fdb_cluster_create_database(self.cpointer, name, len(name))) + f.really_block_until_ready() + dpointer = ctypes.c_void_p() + self.capi.fdb_future_get_database(f.fpointer, ctypes.byref(dpointer)) + return Database(dpointer) + + def _set_option(self, option, param, length): + self.capi.fdb_cluster_set_option(self.cpointer, option, param, length) + + +def create_cluster(cluster_file=None): + f = Future(_FDBBase.capi.fdb_create_cluster(optionalParamToBytes(cluster_file)[0])) + cpointer = ctypes.c_void_p() + f.really_block_until_ready() + _FDBBase.capi.fdb_future_get_cluster(f.fpointer, ctypes.byref(cpointer)) + return Cluster(cpointer) + + +class KeySelector(object): + def __init__(self, key, or_equal, offset): + self.key = key + self.or_equal = or_equal + self.offset = offset + + def __add__(self, offset): + return KeySelector(self.key, self.or_equal, self.offset+offset) + + def __sub__(self, offset): + return KeySelector(self.key, self.or_equal, self.offset-offset) + + @classmethod + def last_less_than(cls, key): + return cls(key, False, 0) + + @classmethod + def last_less_or_equal(cls, key): + return cls(key, True, 0) + + @classmethod + def first_greater_than(cls, key): + return cls(key, True, 1) + + @classmethod + def first_greater_or_equal(cls, key): + return cls(key, False, 1) + + def __str__(self): + return 'KeySelector(%s, %r, %d)' % (self.key, self.or_equal, self.offset) + + +class KVIter(object): + def __init__(self, obj): + self.obj = obj + self.index = 0 + + def __iter__(self): + return self + + def next(self): + self.index += 1 + if self.index == 1: + return self.obj.key + elif self.index == 2: + return self.obj.value + else: + raise StopIteration + + def __next__(self): + return self.next() + + +class KeyValueStruct(ctypes.Structure): + _fields_ = [('key', ctypes.POINTER(ctypes.c_byte)), + ('key_length', ctypes.c_int), + ('value', ctypes.POINTER(ctypes.c_byte)), + ('value_length', ctypes.c_int)] + _pack_ = 4 + + +class KeyValue(object): + def __init__(self, key, value): + self.key = key + self.value = value + + def __repr__(self): + return '%s: %s' % (repr(self.key), repr(self.value)) + + def __iter__(self): + return KVIter(self) + +def check_error_code(code, func, arguments): + if code: + raise FDBError(code) + return None + +if sys.maxsize <= 2**32: + raise Exception("FoundationDB API requires a 64-bit python interpreter!") +if platform.system() == 'Windows': + capi_name = 'fdb_c.dll' +elif platform.system() == 'Linux': + capi_name = 'libfdb_c.so' +elif platform.system() == 'Darwin': + capi_name = 'libfdb_c.dylib' +elif sys.platform == 'win32': + capi_name = 'fdb_c.dll' +elif sys.platform.startswith('cygwin'): + capi_name = 'fdb_c.dll' +elif sys.platform.startswith('linux'): + capi_name = 'libfdb_c.so' +elif sys.platform == 'darwin': + capi_name = 'libfdb_c.dylib' +else: + raise Exception("Platform (%s) %s is not supported by the FoundationDB API!" % (sys.platform, platform.system())) +this_dir = os.path.dirname(__file__) + +# Preferred installation: The C API library or a symbolic link to the +# library should be in the same directory as this module. +# Failing that, a file named $(capi_name).pth should be in the same directory, +# and a relative path to the library (including filename) +# Failing that, we try to load the C API library without qualification, and +# the library should be on the platform's dynamic library search path +def read_pth_file(): + pth_file = os.path.join(this_dir, capi_name+'.pth') + if not os.path.exists(pth_file): return None + pth = _open_file(pth_file, "rt").read().strip() + if pth[0] != '/': + pth = os.path.join(this_dir, pth) + return pth + +for pth in [ + lambda: os.path.join(this_dir, capi_name), + #lambda: os.path.join(this_dir, '../../lib', capi_name), # For compatibility with existing unix installation process... should be removed + read_pth_file + ]: + p = pth() + if p and os.path.exists(p): + _capi = ctypes.CDLL(os.path.abspath(p)) + break +else: + try: + _capi = ctypes.CDLL(capi_name) + except: + # The system python on OS X can't find the library installed to /usr/local/lib if SIP is enabled + # find_library does find the location in /usr/local/lib, so if the above fails fallback to using it + lib_path = ctypes.util.find_library(capi_name) + if lib_path is not None: + try: + _capi = ctypes.CDLL(lib_path) + except: + raise Exception( "Unable to locate the FoundationDB API shared library!" ) + else: + raise Exception( "Unable to locate the FoundationDB API shared library!" ) + +def keyToBytes(k): + if hasattr(k, 'as_foundationdb_key'): + k = k.as_foundationdb_key() + if not isinstance(k, bytes): + raise TypeError('Key must be of type ' + bytes.__name__) + return k + +def valueToBytes(v): + if hasattr(v, 'as_foundationdb_value'): + v = v.as_foundationdb_value() + if not isinstance(v, bytes): + raise TypeError('Value must be of type ' + bytes.__name__) + return v + +def paramToBytes(v): + if isinstance(v, FutureString): + v = v.value + if not isinstance(v, bytes) and hasattr(v, 'encode'): + v = v.encode('utf8') + if not isinstance(v, bytes): + raise TypeError('Parameter must be a string') + return v + +def optionalParamToBytes(v): + if v is None: + return (None, 0) + else: + v = paramToBytes(v) + return (v, len(v)) + +_FDBBase.capi = _capi + +_capi.fdb_select_api_version_impl.argtypes = [ctypes.c_int, ctypes.c_int] +_capi.fdb_select_api_version_impl.restype = ctypes.c_int + +_capi.fdb_get_error.argtypes = [ctypes.c_int] +_capi.fdb_get_error.restype = ctypes.c_char_p + +_capi.fdb_error_predicate.argtypes = [ctypes.c_int, ctypes.c_int] +_capi.fdb_error_predicate.restype = ctypes.c_int + +_capi.fdb_setup_network.argtypes = [] +_capi.fdb_setup_network.restype = ctypes.c_int +_capi.fdb_setup_network.errcheck = check_error_code + +_capi.fdb_network_set_option.argtypes = [ctypes.c_int, ctypes.c_void_p, ctypes.c_int] +_capi.fdb_network_set_option.restype = ctypes.c_int +_capi.fdb_network_set_option.errcheck = check_error_code + +_capi.fdb_run_network.argtypes = [] +_capi.fdb_run_network.restype = ctypes.c_int +_capi.fdb_run_network.errcheck = check_error_code + +_capi.fdb_stop_network.argtypes = [] +_capi.fdb_stop_network.restype = ctypes.c_int +_capi.fdb_stop_network.errcheck = check_error_code + +_capi.fdb_future_destroy.argtypes = [ctypes.c_void_p] +_capi.fdb_future_destroy.restype = None + +_capi.fdb_future_release_memory.argtypes = [ctypes.c_void_p] +_capi.fdb_future_release_memory.restype = None + +_capi.fdb_future_cancel.argtypes = [ctypes.c_void_p] +_capi.fdb_future_cancel.restype = None + +_capi.fdb_future_block_until_ready.argtypes = [ctypes.c_void_p] +_capi.fdb_future_block_until_ready.restype = ctypes.c_int +_capi.fdb_future_block_until_ready.errcheck = check_error_code + +_capi.fdb_future_is_ready.argtypes = [ctypes.c_void_p] +_capi.fdb_future_is_ready.restype = ctypes.c_int + +_CBFUNC = ctypes.CFUNCTYPE(None, ctypes.c_void_p) + +_capi.fdb_future_set_callback.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p] +_capi.fdb_future_set_callback.restype = int +_capi.fdb_future_set_callback.errcheck = check_error_code + +_capi.fdb_future_get_error.argtypes = [ctypes.c_void_p] +_capi.fdb_future_get_error.restype = int +_capi.fdb_future_get_error.errcheck = check_error_code + +_capi.fdb_future_get_version.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int64)] +_capi.fdb_future_get_version.restype = ctypes.c_int +_capi.fdb_future_get_version.errcheck = check_error_code + +_capi.fdb_future_get_key.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.POINTER(ctypes.c_byte)), + ctypes.POINTER(ctypes.c_int)] +_capi.fdb_future_get_key.restype = ctypes.c_int +_capi.fdb_future_get_key.errcheck = check_error_code + +_capi.fdb_future_get_cluster.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)] +_capi.fdb_future_get_cluster.restype = ctypes.c_int +_capi.fdb_future_get_cluster.errcheck = check_error_code + +_capi.fdb_future_get_database.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)] +_capi.fdb_future_get_database.restype = ctypes.c_int +_capi.fdb_future_get_database.errcheck = check_error_code + +_capi.fdb_future_get_value.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int), + ctypes.POINTER(ctypes.POINTER(ctypes.c_byte)), ctypes.POINTER(ctypes.c_int)] +_capi.fdb_future_get_value.restype = ctypes.c_int +_capi.fdb_future_get_value.errcheck = check_error_code + +_capi.fdb_future_get_keyvalue_array.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.POINTER(KeyValueStruct)), ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)] +_capi.fdb_future_get_keyvalue_array.restype = int +_capi.fdb_future_get_keyvalue_array.errcheck = check_error_code + +_capi.fdb_future_get_string_array.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.POINTER(ctypes.c_char_p)), ctypes.POINTER(ctypes.c_int)] +_capi.fdb_future_get_string_array.restype = int +_capi.fdb_future_get_string_array.errcheck = check_error_code + +_capi.fdb_create_cluster.argtypes = [ctypes.c_char_p] +_capi.fdb_create_cluster.restype = ctypes.c_void_p + +_capi.fdb_cluster_destroy.argtypes = [ctypes.c_void_p] +_capi.fdb_cluster_destroy.restype = None + +_capi.fdb_cluster_create_database.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int] +_capi.fdb_cluster_create_database.restype = ctypes.c_void_p + +_capi.fdb_cluster_set_option.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_int] +_capi.fdb_cluster_set_option.restype = ctypes.c_int +_capi.fdb_cluster_set_option.errcheck = check_error_code + +_capi.fdb_database_destroy.argtypes = [ctypes.c_void_p] +_capi.fdb_database_destroy.restype = None + +_capi.fdb_database_create_transaction.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_void_p)] +_capi.fdb_database_create_transaction.restype = ctypes.c_int +_capi.fdb_database_create_transaction.errcheck = check_error_code + +_capi.fdb_database_set_option.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_int] +_capi.fdb_database_set_option.restype = ctypes.c_int +_capi.fdb_database_set_option.errcheck = check_error_code + +_capi.fdb_transaction_destroy.argtypes = [ctypes.c_void_p] +_capi.fdb_transaction_destroy.restype = None + +_capi.fdb_transaction_cancel.argtypes = [ctypes.c_void_p] +_capi.fdb_transaction_cancel.restype = None + +_capi.fdb_transaction_set_read_version.argtypes = [ctypes.c_void_p, ctypes.c_int64] +_capi.fdb_transaction_set_read_version.restype = None + +_capi.fdb_transaction_get_read_version.argtypes = [ctypes.c_void_p] +_capi.fdb_transaction_get_read_version.restype = ctypes.c_void_p + +_capi.fdb_transaction_get.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_int] +_capi.fdb_transaction_get.restype = ctypes.c_void_p + +_capi.fdb_transaction_get_key.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int] +_capi.fdb_transaction_get_key.restype = ctypes.c_void_p + +_capi.fdb_transaction_get_range.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int] +_capi.fdb_transaction_get_range.restype = ctypes.c_void_p + +_capi.fdb_transaction_add_conflict_range.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_int] +_capi.fdb_transaction_add_conflict_range.restype = ctypes.c_int +_capi.fdb_transaction_add_conflict_range.errcheck = check_error_code + +_capi.fdb_transaction_get_addresses_for_key.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int] +_capi.fdb_transaction_get_addresses_for_key.restype = ctypes.c_void_p + +_capi.fdb_transaction_set_option.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_int] +_capi.fdb_transaction_set_option.restype = ctypes.c_int +_capi.fdb_transaction_set_option.errcheck = check_error_code + +_capi.fdb_transaction_atomic_op.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_int, ctypes.c_int] +_capi.fdb_transaction_atomic_op.restype = None + +_capi.fdb_transaction_set.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_int] +_capi.fdb_transaction_set.restype = None + +_capi.fdb_transaction_clear.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int] +_capi.fdb_transaction_clear.restype = None + +_capi.fdb_transaction_clear_range.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_int] +_capi.fdb_transaction_clear_range.restype = None + +_capi.fdb_transaction_watch.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int] +_capi.fdb_transaction_watch.restype = ctypes.c_void_p + +_capi.fdb_transaction_commit.argtypes = [ctypes.c_void_p] +_capi.fdb_transaction_commit.restype = ctypes.c_void_p + +_capi.fdb_transaction_get_committed_version.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_int64)] +_capi.fdb_transaction_get_committed_version.restype = ctypes.c_int +_capi.fdb_transaction_get_committed_version.errcheck = check_error_code + +_capi.fdb_transaction_get_versionstamp.argtypes = [ctypes.c_void_p] +_capi.fdb_transaction_get_versionstamp.restype = ctypes.c_void_p + +_capi.fdb_transaction_on_error.argtypes = [ctypes.c_void_p, ctypes.c_int] +_capi.fdb_transaction_on_error.restype = ctypes.c_void_p + +_capi.fdb_transaction_reset.argtypes = [ctypes.c_void_p] +_capi.fdb_transaction_reset.restype = None + +if hasattr(ctypes.pythonapi, 'Py_IncRef'): + def _pin_callback(cb): + ctypes.pythonapi.Py_IncRef(ctypes.py_object(cb)) + def _unpin_callback(cb): + ctypes.pythonapi.Py_DecRef(ctypes.py_object(cb)) +else: + _active_callbacks = set() + _pin_callback = _active_callbacks.add + _unpin_callback = _active_callbacks.remove + +def init(event_model=None): + """Initialize the FDB interface. + + Consider using open() as a higher-level interface. + + Keyword arguments: + event_model -- the event model to support (default None, also 'gevent') + + """ + with _network_thread_reentrant_lock: + global _network_thread + + # init should only succeed once; if _network_thread is not + # None, someone has already successfully called init + if _network_thread: + raise FDBError(2000) + + try: + class NetworkThread(threading.Thread): + def run(self): + try: + _capi.fdb_run_network() + except FDBError as e: + sys.stderr.write('Unhandled error in FoundationDB network thread: %s\n' % e) + # print('Network stopped') + + _network_thread = NetworkThread() + _network_thread.daemon = True + + if event_model is not None: + if event_model == 'gevent': + import gevent + + if gevent.__version__[0] != '0': + def nullf(): pass + + class ThreadEvent(object): + def __init__(self): + self.async = gevent.get_hub().loop.async() + self.async.start(nullf) + + def set(self): + self.async.send() + + def wait(self): + gevent.get_hub().wait(self.async) + else: + # gevent 0.x doesn't have async, so use a pipe. This doesn't work on Windows. + if platform.system() == 'Windows': + raise Exception("The 'gevent' event_model requires gevent 1.0 on Windows.") + + import gevent.socket + + class ThreadEvent(object): + def __init__(self): + self.pair = os.pipe() + + def set(self): + os.write(self.pair[1], '!') + + def wait(self): + gevent.socket.wait_read(self.pair[0]) + + def __del__(self): + os.close(self.pair[0]) + os.close(self.pair[1]) + + Future.Event = ThreadEvent + + def _gevent_block_until_ready(self): + e = self.Event() + def is_ready_cb(future): + e.set() + self.on_ready(is_ready_cb) + e.wait() + + Future.block_until_ready = _gevent_block_until_ready + elif event_model == 'debug': + import time + + class DebugEvent(object): + def __init__(self): + self.ev = threading.Event() + def set(self): + self.ev.set() + def wait(self): + while not self.ev.isSet(): + self.ev.wait(.001) + Future.Event = DebugEvent + + def _debug_block_until_ready(self): + while not self.is_ready(): + time.sleep(.001) + Future.block_until_ready = _debug_block_until_ready + elif event_model == 'asyncio': + global asyncio + try: + import asyncio + except ImportError: + import trollius as asyncio + + if isinstance(asyncio.futures._FUTURE_CLASSES, type): + asyncio.futures._FUTURE_CLASSES = (asyncio.futures._FUTURE_CLASSES,) + asyncio.futures._FUTURE_CLASSES += (Future,) + + def _do_not_block(self): + if not self.is_ready(): raise Exception("Future not ready") + Future.block_until_ready = _do_not_block + Future.call_soon_threadsafe = asyncio.get_event_loop().call_soon_threadsafe + Future._loop = asyncio.get_event_loop() + def iterate(self): + """Usage: + fa = tr.get_range(...).iterate() + for k,v in (yield From(fa)): + print(k,v) + yield From(fa)""" + def it(): + yield asyncio.From(self._future) + raise asyncio.Return(self) + return it() + FDBRange.iterate = iterate + AT = Database.declare_asynchronous_transactions() + for name in dir(AT): + if name.startswith("_Database__database_"): + setattr(Database, name, getattr(AT, name)) + + def to_list(self): + if self._mode == StreamingMode.iterator: + if self._limit > 0: + mode = StreamingMode.exact + else: + mode = StreamingMode.want_all + else: + mode = self._mode + yield asyncio.From(self._future) + out = [] + for kv in self.__iter__(mode=mode): + out.append(kv) + yield asyncio.From(self._future) + raise asyncio.Return(out) + FDBRange.to_list = to_list + else: + # Hard coded error + raise FDBError(2000) + + _capi.fdb_setup_network() + + # Sketchy... the only error returned by fdb_run_network + # (invoked by _network_thread) is if the network hasn't + # been setup, so if we get here without exception we know + # it has been. + _network_thread.start() + except: + # We assigned _network_thread but didn't succeed in init, + # so clear it out so the next caller has a chance + _network_thread = None + raise + +def init_v13(local_address, event_model = None): + return init(event_model) + +open_clusters = {} +open_databases = {} + +cacheLock = threading.Lock() + +def open( cluster_file = None, database_name = b'DB', event_model = None ): + """Opens the given database (or the default database of the cluster indicated + by the fdb.cluster file in a platform-specific location, if no cluster_file + or database_name is provided). Initializes the FDB interface as required.""" + + with _network_thread_reentrant_lock: + if not _network_thread: + init(event_model = event_model) + + with cacheLock: + if not cluster_file in open_clusters: + open_clusters[cluster_file] = create_cluster( cluster_file ) + + if not (cluster_file, database_name) in open_databases: + open_databases[(cluster_file, database_name)] = open_clusters[cluster_file].open_database(database_name) + + return open_databases[(cluster_file, database_name)] + +def open_v13( cluster_id_path, database_name, local_address = None, event_model = None ): + return open(cluster_id_path, database_name, event_model) + +import atexit + +@atexit.register +def _stop_on_exit(): + if _network_thread: + _capi.fdb_stop_network() + _network_thread.join() + +def strinc(key): + key = key.rstrip(b'\xff') + if len(key) == 0: + raise ValueError('Key must contain at least one byte not equal to 0xFF.') + + return key[:-1] + six.int2byte(ord(key[-1:]) + 1) diff --git a/bindings/python/fdb/libfdb_c.dylib.pth b/bindings/python/fdb/libfdb_c.dylib.pth new file mode 100644 index 0000000000..4bab5a6869 --- /dev/null +++ b/bindings/python/fdb/libfdb_c.dylib.pth @@ -0,0 +1 @@ +../../../lib/libfdb_c.dylib diff --git a/bindings/python/fdb/libfdb_c.so.pth b/bindings/python/fdb/libfdb_c.so.pth new file mode 100644 index 0000000000..4ee4778861 --- /dev/null +++ b/bindings/python/fdb/libfdb_c.so.pth @@ -0,0 +1 @@ +../../../lib/libfdb_c.so \ No newline at end of file diff --git a/bindings/python/fdb/locality.py b/bindings/python/fdb/locality.py new file mode 100644 index 0000000000..d459752dec --- /dev/null +++ b/bindings/python/fdb/locality.py @@ -0,0 +1,71 @@ +# +# locality.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# FoundationDB Python API + +"""Documentation for this API can be found at +https://foundationdb.org/documentation/api-python.html""" + +from fdb import impl as _impl + +def _get_boundary_keys(db_or_tr, begin, end): + if isinstance(db_or_tr, _impl.Transaction): + tr = db_or_tr.db.create_transaction() + # This does not guarantee transactionality because of the exception handling below, + # but it does hide latency for the new transaction's start + tr.set_read_version(db_or_tr.get_read_version().wait()) + else: + tr = db_or_tr.create_transaction() + first_time = True + while begin < end: + try: + lastbegin = begin + tr.options.set_read_system_keys() + tr.options.set_lock_aware() + kvs = tr.snapshot.get_range(b'\xff'+b'/keyServers/'+begin, b'\xff'+b'/keyServers/'+end) + if first_time: + first_time = False + yield None # trick to get the above get_range to be asynchronously dispatched before get_boundary_keys() returns. + for kv in kvs: + yield kv.key[13:] + begin = kv.key[13:]+b'\x00' + begin = end + except _impl.FDBError as e: + if e.code == 1007 and begin != lastbegin: # if we get a past_version and *something* has happened, then we are no longer transactional + tr = tr.db.create_transaction() + else: + tr.on_error(e).wait() + +def get_boundary_keys(db_or_tr, begin, end): + begin = _impl.keyToBytes(begin) + end = _impl.keyToBytes(end) + + gen = _get_boundary_keys(db_or_tr, begin, end) + try: + next(gen) + except StopIteration: # if _get_boundary_keys() never yields a value, e.g. begin > end + return (x for x in list()) + return gen + + +@_impl.transactional +def get_addresses_for_key(tr, key): + keyBytes = _impl.keyToBytes(key) + return _impl.FutureStringArray(tr.capi.fdb_transaction_get_addresses_for_key(tr.tpointer, keyBytes, len(keyBytes))) diff --git a/bindings/python/fdb/six.py b/bindings/python/fdb/six.py new file mode 100644 index 0000000000..019130f7ba --- /dev/null +++ b/bindings/python/fdb/six.py @@ -0,0 +1,646 @@ +"""Utilities for writing code that runs on Python 2 and 3""" + +# Copyright (c) 2010-2014 Benjamin Peterson +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import operator +import sys +import types + +__author__ = "Benjamin Peterson " +__version__ = "1.6.1" + + +# Useful for very coarse version differentiation. +PY2 = sys.version_info[0] == 2 +PY3 = sys.version_info[0] == 3 + +if PY3: + string_types = str, + integer_types = int, + class_types = type, + text_type = str + binary_type = bytes + + MAXSIZE = sys.maxsize +else: + string_types = basestring, + integer_types = (int, long) + class_types = (type, types.ClassType) + text_type = unicode + binary_type = str + + if sys.platform.startswith("java"): + # Jython always uses 32 bits. + MAXSIZE = int((1 << 31) - 1) + else: + # It's possible to have sizeof(long) != sizeof(Py_ssize_t). + class X(object): + def __len__(self): + return 1 << 31 + try: + len(X()) + except OverflowError: + # 32-bit + MAXSIZE = int((1 << 31) - 1) + else: + # 64-bit + MAXSIZE = int((1 << 63) - 1) + del X + + +def _add_doc(func, doc): + """Add documentation to a function.""" + func.__doc__ = doc + + +def _import_module(name): + """Import module, returning the module after the last dot.""" + __import__(name) + return sys.modules[name] + + +class _LazyDescr(object): + + def __init__(self, name): + self.name = name + + def __get__(self, obj, tp): + try: + result = self._resolve() + except ImportError: + # See the nice big comment in MovedModule.__getattr__. + raise AttributeError("%s could not be imported " % self.name) + setattr(obj, self.name, result) # Invokes __set__. + # This is a bit ugly, but it avoids running this again. + delattr(obj.__class__, self.name) + return result + + +class MovedModule(_LazyDescr): + + def __init__(self, name, old, new=None): + super(MovedModule, self).__init__(name) + if PY3: + if new is None: + new = name + self.mod = new + else: + self.mod = old + + def _resolve(self): + return _import_module(self.mod) + + def __getattr__(self, attr): + # It turns out many Python frameworks like to traverse sys.modules and + # try to load various attributes. This causes problems if this is a + # platform-specific module on the wrong platform, like _winreg on + # Unixes. Therefore, we silently pretend unimportable modules do not + # have any attributes. See issues #51, #53, #56, and #63 for the full + # tales of woe. + # + # First, if possible, avoid loading the module just to look at __file__, + # __name__, or __path__. + if (attr in ("__file__", "__name__", "__path__") and + self.mod not in sys.modules): + raise AttributeError(attr) + try: + _module = self._resolve() + except ImportError: + raise AttributeError(attr) + value = getattr(_module, attr) + setattr(self, attr, value) + return value + + +class _LazyModule(types.ModuleType): + + def __init__(self, name): + super(_LazyModule, self).__init__(name) + self.__doc__ = self.__class__.__doc__ + + def __dir__(self): + attrs = ["__doc__", "__name__"] + attrs += [attr.name for attr in self._moved_attributes] + return attrs + + # Subclasses should override this + _moved_attributes = [] + + +class MovedAttribute(_LazyDescr): + + def __init__(self, name, old_mod, new_mod, old_attr=None, new_attr=None): + super(MovedAttribute, self).__init__(name) + if PY3: + if new_mod is None: + new_mod = name + self.mod = new_mod + if new_attr is None: + if old_attr is None: + new_attr = name + else: + new_attr = old_attr + self.attr = new_attr + else: + self.mod = old_mod + if old_attr is None: + old_attr = name + self.attr = old_attr + + def _resolve(self): + module = _import_module(self.mod) + return getattr(module, self.attr) + + + +class _MovedItems(_LazyModule): + """Lazy loading of moved objects""" + + +_moved_attributes = [ + MovedAttribute("cStringIO", "cStringIO", "io", "StringIO"), + MovedAttribute("filter", "itertools", "builtins", "ifilter", "filter"), + MovedAttribute("filterfalse", "itertools", "itertools", "ifilterfalse", "filterfalse"), + MovedAttribute("input", "__builtin__", "builtins", "raw_input", "input"), + MovedAttribute("map", "itertools", "builtins", "imap", "map"), + MovedAttribute("range", "__builtin__", "builtins", "xrange", "range"), + MovedAttribute("reload_module", "__builtin__", "imp", "reload"), + MovedAttribute("reduce", "__builtin__", "functools"), + MovedAttribute("StringIO", "StringIO", "io"), + MovedAttribute("UserString", "UserString", "collections"), + MovedAttribute("xrange", "__builtin__", "builtins", "xrange", "range"), + MovedAttribute("zip", "itertools", "builtins", "izip", "zip"), + MovedAttribute("zip_longest", "itertools", "itertools", "izip_longest", "zip_longest"), + + MovedModule("builtins", "__builtin__"), + MovedModule("configparser", "ConfigParser"), + MovedModule("copyreg", "copy_reg"), + MovedModule("dbm_gnu", "gdbm", "dbm.gnu"), + MovedModule("http_cookiejar", "cookielib", "http.cookiejar"), + MovedModule("http_cookies", "Cookie", "http.cookies"), + MovedModule("html_entities", "htmlentitydefs", "html.entities"), + MovedModule("html_parser", "HTMLParser", "html.parser"), + MovedModule("http_client", "httplib", "http.client"), + MovedModule("email_mime_multipart", "email.MIMEMultipart", "email.mime.multipart"), + MovedModule("email_mime_text", "email.MIMEText", "email.mime.text"), + MovedModule("email_mime_base", "email.MIMEBase", "email.mime.base"), + MovedModule("BaseHTTPServer", "BaseHTTPServer", "http.server"), + MovedModule("CGIHTTPServer", "CGIHTTPServer", "http.server"), + MovedModule("SimpleHTTPServer", "SimpleHTTPServer", "http.server"), + MovedModule("cPickle", "cPickle", "pickle"), + MovedModule("queue", "Queue"), + MovedModule("reprlib", "repr"), + MovedModule("socketserver", "SocketServer"), + MovedModule("_thread", "thread", "_thread"), + MovedModule("tkinter", "Tkinter"), + MovedModule("tkinter_dialog", "Dialog", "tkinter.dialog"), + MovedModule("tkinter_filedialog", "FileDialog", "tkinter.filedialog"), + MovedModule("tkinter_scrolledtext", "ScrolledText", "tkinter.scrolledtext"), + MovedModule("tkinter_simpledialog", "SimpleDialog", "tkinter.simpledialog"), + MovedModule("tkinter_tix", "Tix", "tkinter.tix"), + MovedModule("tkinter_ttk", "ttk", "tkinter.ttk"), + MovedModule("tkinter_constants", "Tkconstants", "tkinter.constants"), + MovedModule("tkinter_dnd", "Tkdnd", "tkinter.dnd"), + MovedModule("tkinter_colorchooser", "tkColorChooser", + "tkinter.colorchooser"), + MovedModule("tkinter_commondialog", "tkCommonDialog", + "tkinter.commondialog"), + MovedModule("tkinter_tkfiledialog", "tkFileDialog", "tkinter.filedialog"), + MovedModule("tkinter_font", "tkFont", "tkinter.font"), + MovedModule("tkinter_messagebox", "tkMessageBox", "tkinter.messagebox"), + MovedModule("tkinter_tksimpledialog", "tkSimpleDialog", + "tkinter.simpledialog"), + MovedModule("urllib_parse", __name__ + ".moves.urllib_parse", "urllib.parse"), + MovedModule("urllib_error", __name__ + ".moves.urllib_error", "urllib.error"), + MovedModule("urllib", __name__ + ".moves.urllib", __name__ + ".moves.urllib"), + MovedModule("urllib_robotparser", "robotparser", "urllib.robotparser"), + MovedModule("xmlrpc_client", "xmlrpclib", "xmlrpc.client"), + MovedModule("xmlrpc_server", "xmlrpclib", "xmlrpc.server"), + MovedModule("winreg", "_winreg"), +] +for attr in _moved_attributes: + setattr(_MovedItems, attr.name, attr) + if isinstance(attr, MovedModule): + sys.modules[__name__ + ".moves." + attr.name] = attr +del attr + +_MovedItems._moved_attributes = _moved_attributes + +moves = sys.modules[__name__ + ".moves"] = _MovedItems(__name__ + ".moves") + + +class Module_six_moves_urllib_parse(_LazyModule): + """Lazy loading of moved objects in six.moves.urllib_parse""" + + +_urllib_parse_moved_attributes = [ + MovedAttribute("ParseResult", "urlparse", "urllib.parse"), + MovedAttribute("SplitResult", "urlparse", "urllib.parse"), + MovedAttribute("parse_qs", "urlparse", "urllib.parse"), + MovedAttribute("parse_qsl", "urlparse", "urllib.parse"), + MovedAttribute("urldefrag", "urlparse", "urllib.parse"), + MovedAttribute("urljoin", "urlparse", "urllib.parse"), + MovedAttribute("urlparse", "urlparse", "urllib.parse"), + MovedAttribute("urlsplit", "urlparse", "urllib.parse"), + MovedAttribute("urlunparse", "urlparse", "urllib.parse"), + MovedAttribute("urlunsplit", "urlparse", "urllib.parse"), + MovedAttribute("quote", "urllib", "urllib.parse"), + MovedAttribute("quote_plus", "urllib", "urllib.parse"), + MovedAttribute("unquote", "urllib", "urllib.parse"), + MovedAttribute("unquote_plus", "urllib", "urllib.parse"), + MovedAttribute("urlencode", "urllib", "urllib.parse"), + MovedAttribute("splitquery", "urllib", "urllib.parse"), +] +for attr in _urllib_parse_moved_attributes: + setattr(Module_six_moves_urllib_parse, attr.name, attr) +del attr + +Module_six_moves_urllib_parse._moved_attributes = _urllib_parse_moved_attributes + +sys.modules[__name__ + ".moves.urllib_parse"] = sys.modules[__name__ + ".moves.urllib.parse"] = Module_six_moves_urllib_parse(__name__ + ".moves.urllib_parse") + + +class Module_six_moves_urllib_error(_LazyModule): + """Lazy loading of moved objects in six.moves.urllib_error""" + + +_urllib_error_moved_attributes = [ + MovedAttribute("URLError", "urllib2", "urllib.error"), + MovedAttribute("HTTPError", "urllib2", "urllib.error"), + MovedAttribute("ContentTooShortError", "urllib", "urllib.error"), +] +for attr in _urllib_error_moved_attributes: + setattr(Module_six_moves_urllib_error, attr.name, attr) +del attr + +Module_six_moves_urllib_error._moved_attributes = _urllib_error_moved_attributes + +sys.modules[__name__ + ".moves.urllib_error"] = sys.modules[__name__ + ".moves.urllib.error"] = Module_six_moves_urllib_error(__name__ + ".moves.urllib.error") + + +class Module_six_moves_urllib_request(_LazyModule): + """Lazy loading of moved objects in six.moves.urllib_request""" + + +_urllib_request_moved_attributes = [ + MovedAttribute("urlopen", "urllib2", "urllib.request"), + MovedAttribute("install_opener", "urllib2", "urllib.request"), + MovedAttribute("build_opener", "urllib2", "urllib.request"), + MovedAttribute("pathname2url", "urllib", "urllib.request"), + MovedAttribute("url2pathname", "urllib", "urllib.request"), + MovedAttribute("getproxies", "urllib", "urllib.request"), + MovedAttribute("Request", "urllib2", "urllib.request"), + MovedAttribute("OpenerDirector", "urllib2", "urllib.request"), + MovedAttribute("HTTPDefaultErrorHandler", "urllib2", "urllib.request"), + MovedAttribute("HTTPRedirectHandler", "urllib2", "urllib.request"), + MovedAttribute("HTTPCookieProcessor", "urllib2", "urllib.request"), + MovedAttribute("ProxyHandler", "urllib2", "urllib.request"), + MovedAttribute("BaseHandler", "urllib2", "urllib.request"), + MovedAttribute("HTTPPasswordMgr", "urllib2", "urllib.request"), + MovedAttribute("HTTPPasswordMgrWithDefaultRealm", "urllib2", "urllib.request"), + MovedAttribute("AbstractBasicAuthHandler", "urllib2", "urllib.request"), + MovedAttribute("HTTPBasicAuthHandler", "urllib2", "urllib.request"), + MovedAttribute("ProxyBasicAuthHandler", "urllib2", "urllib.request"), + MovedAttribute("AbstractDigestAuthHandler", "urllib2", "urllib.request"), + MovedAttribute("HTTPDigestAuthHandler", "urllib2", "urllib.request"), + MovedAttribute("ProxyDigestAuthHandler", "urllib2", "urllib.request"), + MovedAttribute("HTTPHandler", "urllib2", "urllib.request"), + MovedAttribute("HTTPSHandler", "urllib2", "urllib.request"), + MovedAttribute("FileHandler", "urllib2", "urllib.request"), + MovedAttribute("FTPHandler", "urllib2", "urllib.request"), + MovedAttribute("CacheFTPHandler", "urllib2", "urllib.request"), + MovedAttribute("UnknownHandler", "urllib2", "urllib.request"), + MovedAttribute("HTTPErrorProcessor", "urllib2", "urllib.request"), + MovedAttribute("urlretrieve", "urllib", "urllib.request"), + MovedAttribute("urlcleanup", "urllib", "urllib.request"), + MovedAttribute("URLopener", "urllib", "urllib.request"), + MovedAttribute("FancyURLopener", "urllib", "urllib.request"), + MovedAttribute("proxy_bypass", "urllib", "urllib.request"), +] +for attr in _urllib_request_moved_attributes: + setattr(Module_six_moves_urllib_request, attr.name, attr) +del attr + +Module_six_moves_urllib_request._moved_attributes = _urllib_request_moved_attributes + +sys.modules[__name__ + ".moves.urllib_request"] = sys.modules[__name__ + ".moves.urllib.request"] = Module_six_moves_urllib_request(__name__ + ".moves.urllib.request") + + +class Module_six_moves_urllib_response(_LazyModule): + """Lazy loading of moved objects in six.moves.urllib_response""" + + +_urllib_response_moved_attributes = [ + MovedAttribute("addbase", "urllib", "urllib.response"), + MovedAttribute("addclosehook", "urllib", "urllib.response"), + MovedAttribute("addinfo", "urllib", "urllib.response"), + MovedAttribute("addinfourl", "urllib", "urllib.response"), +] +for attr in _urllib_response_moved_attributes: + setattr(Module_six_moves_urllib_response, attr.name, attr) +del attr + +Module_six_moves_urllib_response._moved_attributes = _urllib_response_moved_attributes + +sys.modules[__name__ + ".moves.urllib_response"] = sys.modules[__name__ + ".moves.urllib.response"] = Module_six_moves_urllib_response(__name__ + ".moves.urllib.response") + + +class Module_six_moves_urllib_robotparser(_LazyModule): + """Lazy loading of moved objects in six.moves.urllib_robotparser""" + + +_urllib_robotparser_moved_attributes = [ + MovedAttribute("RobotFileParser", "robotparser", "urllib.robotparser"), +] +for attr in _urllib_robotparser_moved_attributes: + setattr(Module_six_moves_urllib_robotparser, attr.name, attr) +del attr + +Module_six_moves_urllib_robotparser._moved_attributes = _urllib_robotparser_moved_attributes + +sys.modules[__name__ + ".moves.urllib_robotparser"] = sys.modules[__name__ + ".moves.urllib.robotparser"] = Module_six_moves_urllib_robotparser(__name__ + ".moves.urllib.robotparser") + + +class Module_six_moves_urllib(types.ModuleType): + """Create a six.moves.urllib namespace that resembles the Python 3 namespace""" + parse = sys.modules[__name__ + ".moves.urllib_parse"] + error = sys.modules[__name__ + ".moves.urllib_error"] + request = sys.modules[__name__ + ".moves.urllib_request"] + response = sys.modules[__name__ + ".moves.urllib_response"] + robotparser = sys.modules[__name__ + ".moves.urllib_robotparser"] + + def __dir__(self): + return ['parse', 'error', 'request', 'response', 'robotparser'] + + +sys.modules[__name__ + ".moves.urllib"] = Module_six_moves_urllib(__name__ + ".moves.urllib") + + +def add_move(move): + """Add an item to six.moves.""" + setattr(_MovedItems, move.name, move) + + +def remove_move(name): + """Remove item from six.moves.""" + try: + delattr(_MovedItems, name) + except AttributeError: + try: + del moves.__dict__[name] + except KeyError: + raise AttributeError("no such move, %r" % (name,)) + + +if PY3: + _meth_func = "__func__" + _meth_self = "__self__" + + _func_closure = "__closure__" + _func_code = "__code__" + _func_defaults = "__defaults__" + _func_globals = "__globals__" + + _iterkeys = "keys" + _itervalues = "values" + _iteritems = "items" + _iterlists = "lists" +else: + _meth_func = "im_func" + _meth_self = "im_self" + + _func_closure = "func_closure" + _func_code = "func_code" + _func_defaults = "func_defaults" + _func_globals = "func_globals" + + _iterkeys = "iterkeys" + _itervalues = "itervalues" + _iteritems = "iteritems" + _iterlists = "iterlists" + + +try: + advance_iterator = next +except NameError: + def advance_iterator(it): + return it.next() +next = advance_iterator + + +try: + callable = callable +except NameError: + def callable(obj): + return any("__call__" in klass.__dict__ for klass in type(obj).__mro__) + + +if PY3: + def get_unbound_function(unbound): + return unbound + + create_bound_method = types.MethodType + + Iterator = object +else: + def get_unbound_function(unbound): + return unbound.im_func + + def create_bound_method(func, obj): + return types.MethodType(func, obj, obj.__class__) + + class Iterator(object): + + def next(self): + return type(self).__next__(self) + + callable = callable +_add_doc(get_unbound_function, + """Get the function out of a possibly unbound function""") + + +get_method_function = operator.attrgetter(_meth_func) +get_method_self = operator.attrgetter(_meth_self) +get_function_closure = operator.attrgetter(_func_closure) +get_function_code = operator.attrgetter(_func_code) +get_function_defaults = operator.attrgetter(_func_defaults) +get_function_globals = operator.attrgetter(_func_globals) + + +def iterkeys(d, **kw): + """Return an iterator over the keys of a dictionary.""" + return iter(getattr(d, _iterkeys)(**kw)) + +def itervalues(d, **kw): + """Return an iterator over the values of a dictionary.""" + return iter(getattr(d, _itervalues)(**kw)) + +def iteritems(d, **kw): + """Return an iterator over the (key, value) pairs of a dictionary.""" + return iter(getattr(d, _iteritems)(**kw)) + +def iterlists(d, **kw): + """Return an iterator over the (key, [values]) pairs of a dictionary.""" + return iter(getattr(d, _iterlists)(**kw)) + + +if PY3: + def b(s): + return s.encode("latin-1") + def u(s): + return s + unichr = chr + if sys.version_info[1] <= 1: + def int2byte(i): + return bytes((i,)) + else: + # This is about 2x faster than the implementation above on 3.2+ + int2byte = operator.methodcaller("to_bytes", 1, "big") + byte2int = operator.itemgetter(0) + indexbytes = operator.getitem + iterbytes = iter + import io + StringIO = io.StringIO + BytesIO = io.BytesIO +else: + def b(s): + return s + # Workaround for standalone backslash + def u(s): + return unicode(s.replace(r'\\', r'\\\\'), "unicode_escape") + unichr = unichr + int2byte = chr + def byte2int(bs): + return ord(bs[0]) + def indexbytes(buf, i): + return ord(buf[i]) + def iterbytes(buf): + return (ord(byte) for byte in buf) + import StringIO + StringIO = BytesIO = StringIO.StringIO +_add_doc(b, """Byte literal""") +_add_doc(u, """Text literal""") + + +if PY3: + exec_ = getattr(moves.builtins, "exec") + + + def reraise(tp, value, tb=None): + if value.__traceback__ is not tb: + raise value.with_traceback(tb) + raise value + +else: + def exec_(_code_, _globs_=None, _locs_=None): + """Execute code in a namespace.""" + if _globs_ is None: + frame = sys._getframe(1) + _globs_ = frame.f_globals + if _locs_ is None: + _locs_ = frame.f_locals + del frame + elif _locs_ is None: + _locs_ = _globs_ + exec("""exec _code_ in _globs_, _locs_""") + + + exec_("""def reraise(tp, value, tb=None): + raise tp, value, tb +""") + + +print_ = getattr(moves.builtins, "print", None) +if print_ is None: + def print_(*args, **kwargs): + """The new-style print function for Python 2.4 and 2.5.""" + fp = kwargs.pop("file", sys.stdout) + if fp is None: + return + def write(data): + if not isinstance(data, basestring): + data = str(data) + # If the file has an encoding, encode unicode with it. + if (isinstance(fp, file) and + isinstance(data, unicode) and + fp.encoding is not None): + errors = getattr(fp, "errors", None) + if errors is None: + errors = "strict" + data = data.encode(fp.encoding, errors) + fp.write(data) + want_unicode = False + sep = kwargs.pop("sep", None) + if sep is not None: + if isinstance(sep, unicode): + want_unicode = True + elif not isinstance(sep, str): + raise TypeError("sep must be None or a string") + end = kwargs.pop("end", None) + if end is not None: + if isinstance(end, unicode): + want_unicode = True + elif not isinstance(end, str): + raise TypeError("end must be None or a string") + if kwargs: + raise TypeError("invalid keyword arguments to print()") + if not want_unicode: + for arg in args: + if isinstance(arg, unicode): + want_unicode = True + break + if want_unicode: + newline = unicode("\n") + space = unicode(" ") + else: + newline = "\n" + space = " " + if sep is None: + sep = space + if end is None: + end = newline + for i, arg in enumerate(args): + if i: + write(sep) + write(arg) + write(end) + +_add_doc(reraise, """Reraise an exception.""") + + +def with_metaclass(meta, *bases): + """Create a base class with a metaclass.""" + return meta("NewBase", bases, {}) + +def add_metaclass(metaclass): + """Class decorator for creating a class with a metaclass.""" + def wrapper(cls): + orig_vars = cls.__dict__.copy() + orig_vars.pop('__dict__', None) + orig_vars.pop('__weakref__', None) + slots = orig_vars.get('__slots__') + if slots is not None: + if isinstance(slots, str): + slots = [slots] + for slots_var in slots: + orig_vars.pop(slots_var) + return metaclass(cls.__name__, cls.__bases__, orig_vars) + return wrapper diff --git a/bindings/python/fdb/subspace_impl.py b/bindings/python/fdb/subspace_impl.py new file mode 100644 index 0000000000..d06e9fdf57 --- /dev/null +++ b/bindings/python/fdb/subspace_impl.py @@ -0,0 +1,59 @@ +# +# subspace_impl.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# FoundationDB Python API + +import fdb.tuple + +class Subspace (object): + + def __init__(self, prefixTuple=tuple(), rawPrefix=b''): + self.rawPrefix = rawPrefix + fdb.tuple.pack(prefixTuple) + + def __repr__(self): + return 'Subspace(rawPrefix=' + repr(self.rawPrefix) + ')' + + def __getitem__(self, name): + return Subspace((name,), self.rawPrefix) + + def key(self): + return self.rawPrefix + + def pack(self, t=tuple()): + return self.rawPrefix + fdb.tuple.pack(t) + + def unpack(self, key): + if not self.contains(key): + raise ValueError('Cannot unpack key that is not in subspace.') + + return fdb.tuple.unpack(key[len(self.rawPrefix):]) + + def range(self, t=tuple()): + p = fdb.tuple.range(t) + return slice(self.rawPrefix + p.start, self.rawPrefix + p.stop) + + def contains(self, key): + return key.startswith(self.rawPrefix) + + def as_foundationdb_key(self): + return self.rawPrefix + + def subspace(self, tuple): + return Subspace(tuple, self.rawPrefix) diff --git a/bindings/python/fdb/tuple.py b/bindings/python/fdb/tuple.py new file mode 100644 index 0000000000..c64873956c --- /dev/null +++ b/bindings/python/fdb/tuple.py @@ -0,0 +1,142 @@ +# +# tuple.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# FoundationDB Python API + +import struct, math +from bisect import bisect_left + +from fdb import six + +_size_limits = tuple( (1 << (i*8))-1 for i in range(9) ) + +def _find_terminator( v, pos ): + # Finds the start of the next terminator [\x00]![\xff] or the end of v + while True: + pos = v.find(b'\x00', pos) + if pos < 0: + return len(v) + if pos+1 == len(v) or v[pos+1:pos+2] != b'\xff': + return pos + pos += 2 + +def _decode(v, pos): + code = six.indexbytes(v, pos) + if code == 0: + return None, pos+1 + elif code == 1: + end = _find_terminator(v, pos+1) + return v[pos+1:end].replace(b"\x00\xFF", b"\x00"), end+1 + elif code == 2: + end = _find_terminator(v, pos+1) + return v[pos+1:end].replace(b"\x00\xFF", b"\x00").decode("utf-8"), end+1 + elif code >= 20 and code <= 28: + n = code - 20 + end = pos + 1 + n + return struct.unpack(">Q", b'\x00'*(8-n) + v[pos+1:end])[0], end + elif code >= 12 and code < 20: + n = 20 - code + end = pos + 1 + n + return struct.unpack(">Q", b'\x00'*(8-n) + v[pos+1:end])[0]-_size_limits[n], end + elif code == 29: # 0x1d; Positive 9-255 byte integer + length = six.indexbytes(v, pos+1) + val = 0 + for i in _range(length): + val = val << 8 + val += six.indexbytes(v, pos+2+i) + return val, pos+2+length + elif code == 11: # 0x0b; Negative 9-255 byte integer + length = six.indexbytes(v, pos+1)^0xff + val = 0 + for i in _range(length): + val = val << 8 + val += six.indexbytes(v, pos+2+i) + return val - (1<<(length*8)) + 1, pos+2+length + else: + raise ValueError("Unknown data type in DB: " + repr(v)) + +def _encode(value): + # returns [code][data] (code != 0xFF) + # encoded values are self-terminating + # sorting need to work too! + if value == None: # ==, not is, because some fdb.impl.Value are equal to None + return b'\x00' + elif isinstance(value, bytes): # also gets non-None fdb.impl.Value + return b'\x01' + value.replace(b'\x00', b'\x00\xFF') + b'\x00' + elif isinstance(value, six.text_type): + return b'\x02' + value.encode('utf-8').replace(b'\x00', b'\x00\xFF') + b'\x00' + elif isinstance(value, six.integer_types): + if value == 0: + return b'\x14' + elif value > 0: + if value >= _size_limits[-1]: + length = (value.bit_length()+7)//8 + data = [b'\x1d', six.int2byte(length)] + for i in _range(length-1,-1,-1): + data.append(six.int2byte( (value>>(8*i))&0xff )) + return b''.join(data) + + n = bisect_left( _size_limits, value ) + return six.int2byte(20 + n) + struct.pack( ">Q", value )[-n:] + else: + if -value >= _size_limits[-1]: + length = (value.bit_length()+7)//8 + value += (1<<(length*8)) - 1 + data = [b'\x0b', six.int2byte(length^0xff)] + for i in _range(length-1,-1,-1): + data.append(six.int2byte( (value>>(8*i))&0xff )) + return b''.join(data) + + n = bisect_left( _size_limits, -value ) + maxv = _size_limits[n] + return six.int2byte(20 - n) + struct.pack( ">Q", maxv+value)[-n:] + else: + raise ValueError("Unsupported data type: " + str(type(value))) + +# packs the specified tuple into a key +def pack(t): + if not isinstance(t, tuple): + raise Exception("fdbtuple pack() expects a tuple, got a " + str(type(t))) + return b''.join([_encode(x) for x in t]) + +# unpacks the specified key into a tuple +def unpack(key): + pos = 0 + res = [] + while pos < len(key): + r, pos = _decode(key, pos) + res.append(r) + return tuple(res) + +_range = range +def range(t): + """Returns a slice of keys that includes all tuples of greater + length than the specified tuple that that start with the + specified elements. + + e.g. range(('a', 'b')) includes all tuples ('a', 'b', ...)""" + + if not isinstance(t, tuple): + raise Exception("fdbtuple range() expects a tuple, got a " + str(type(t))) + + p = pack(t) + return slice( + p+b'\x00', + p+b'\xff') diff --git a/bindings/python/include.mk b/bindings/python/include.mk new file mode 100644 index 0000000000..5199189c7f --- /dev/null +++ b/bindings/python/include.mk @@ -0,0 +1,70 @@ +# +# include.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile-gmake; -*- + +TARGETS += fdb_python +CLEAN_TARGETS += fdb_python_clean + +ifeq ($(RELEASE),true) + PYVER = $(VERSION) +else + PYVER = $(VERSION)a1 +endif + +fdb_python: bindings/python/fdb/fdboptions.py bindings/python/setup.py + +bindings/python/fdb/fdboptions.py: bin/vexillographer.exe fdbclient/vexillographer/fdb.options + @echo "Building $@" + @$(MONO) bin/vexillographer.exe fdbclient/vexillographer/fdb.options python $@ + +fdb_python_clean: + @echo "Cleaning fdb_python" + @rm -f bindings/python/fdb/fdboptions.py bindings/python/setup.py + +bindings/python/setup.py: bindings/python/setup.py.in $(ALL_MAKEFILES) versions.target + @echo "Generating $@" + @m4 -DVERSION=$(PYVER) $< > $@ + +fdb_python_sdist: fdb_python + @mkdir -p packages + @rm -rf bindings/python/dist + @cp LICENSE bindings/python/LICENSE + @cd bindings/python && python setup.py sdist + @rm bindings/python/LICENSE + @cp bindings/python/dist/*.tar.gz packages/ + +fdb_python_sdist_upload: fdb_python + @mkdir -p packages + @rm -rf bindings/python/dist + @cp LICENSE bindings/python/LICENSE + @cd bindings/python && python setup.py sdist upload -r apple-pypi + @rm bindings/python/LICENSE + @cp bindings/python/dist/*.tar.gz packages/ + +fdb_python_sdist_clean: + @echo "Cleaning fdb_python_sdist" + @rm -rf bindings/python/dist + @rm -f bindings/python/MANIFEST bindings/python/setup.py + @rm -f packages/foundationdb-*.tar.gz + +packages: fdb_python_sdist + +packages_clean: fdb_python_sdist_clean diff --git a/bindings/python/setup.py.in b/bindings/python/setup.py.in new file mode 100644 index 0000000000..6e59767a6d --- /dev/null +++ b/bindings/python/setup.py.in @@ -0,0 +1,38 @@ +from distutils.core import setup + +try: + with open("README.rst") as f: + long_desc = f.read() +except: + long_desc = "" + +setup(name = "foundationdb", + version = "VERSION", + author = "FoundationDB", + author_email = "fdbopensource@apple.com", + description = "Python bindings for the FoundationDB database", + url = "http://foundationdb.org", + packages = ['fdb'], + package_data = {'fdb' : ["fdb/*.py"]}, + long_description = long_desc, + classifiers = [ + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: Apache v2 License', + 'Operating System :: MacOS :: MacOS X', + 'Operating System :: Microsoft :: Windows', + 'Operating System :: POSIX :: Linux', + 'Programming Language :: Python :: 2', + 'Programming Language :: Python :: 2.6', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.0', + 'Programming Language :: Python :: 3.1', + 'Programming Language :: Python :: 3.2', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: Implementation :: CPython', + 'Topic :: Database', + 'Topic :: Database :: Front-Ends' + ] + ) diff --git a/bindings/python/tests/cancellation_timeout_tests.py b/bindings/python/tests/cancellation_timeout_tests.py new file mode 100755 index 0000000000..dd5acec2d3 --- /dev/null +++ b/bindings/python/tests/cancellation_timeout_tests.py @@ -0,0 +1,445 @@ +# +# cancellation_timeout_tests.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import time +import fdb + +TestError = Exception + +def retry_with_timeout(seconds): + def decorator(f): + def wrapper(db): + timeout_tr = db.create_transaction() + + tr = db.create_transaction() + while True: + try: + timeout_tr.options.set_timeout(seconds * 1000) + f(tr) + return + except fdb.FDBError as e: + timeout_tr.on_error(e).wait() + tr = db.create_transaction() + + return wrapper + return decorator + +default_timeout = 60 + +def test_cancellation(db): + # (1) Basic cancellation + @retry_with_timeout(default_timeout) + def txn1(tr): + tr.cancel() + try: + tr.commit().wait() # should throw + raise TestError('Basic cancellation unit test failed.') + except fdb.FDBError as e: + if e.code != 1025: + raise + + txn1(db) + + # (2) Cancellation does not survive reset + @retry_with_timeout(default_timeout) + def txn2(tr): + tr.cancel() + tr.reset() + try: + tr.commit().wait() # should not throw + except fdb.FDBError as e: + if e.code == 1025: + raise TestError('Cancellation survived reset.') + else: + raise + + txn2(db) + + # (3) Cancellation does survive on_error() + @retry_with_timeout(default_timeout) + def txn3(tr): + tr.cancel() + try: + tr.on_error(fdb.FDBError(1007)).wait() # should throw + raise TestError('on_error() did not notice cancellation.') + except fdb.FDBError as e: + if e.code != 1025: + raise + try: + tr.commit().wait() # should throw + raise TestError('Cancellation did not survive on_error().') + except fdb.FDBError as e: + if e.code != 1025: + raise + + txn3(db) + + # (4) Cancellation works with weird operations + @retry_with_timeout(default_timeout) + def txn4(tr): + tr[b'foo'] + tr.cancel() + try: + tr.get_read_version().wait() # should throw + raise TestError("Cancellation didn't throw on weird operation.") + except fdb.FDBError as e: + if e.code != 1025: + raise + + txn4(db) + + return + +def test_retry_limits(db): + err = fdb.FDBError(1007) + + # (1) Basic retry limits + @retry_with_timeout(default_timeout) + def txn1(tr): + tr.options.set_retry_limit(1) + tr[b'foo'] = b'bar' + tr.on_error(err).wait() # should not throw + tr[b'foo'] = b'bar' + tr.options.set_retry_limit(1) + try: + tr.on_error(err).wait() # should throw + raise TestError('(1) Retry limit was ignored.') + except fdb.FDBError as e: + if e.code != 1007: + raise + tr[b'foo'] = b'bar' + tr.options.set_retry_limit(1) + try: + tr.on_error(err).wait() # should throw + raise TestError('(1) Retry limit was ignored.') + except fdb.FDBError as e: + if e.code != 1007: + raise + + txn1(db) + + # (2) Number of retries accumulates even when limit is being hit + @retry_with_timeout(default_timeout) + def txn2(tr): + tr.options.set_retry_limit(0) + tr[b'foo'] = b'bar' + try: + tr.on_error(err).wait() # should throw + raise TestError('(2) Retry limit was ignored.') + except fdb.FDBError as e: + if e.code != 1007: + raise + tr.options.set_retry_limit(1) + tr[b'foo'] = b'bar' + try: + tr.on_error(err).wait() # should throw + raise TestError('(2) Retry limit was ignored.') + except fdb.FDBError as e: + if e.code != 1007: + raise + + txn2(db) + + # (3) Retry limits don't survive resets + @retry_with_timeout(default_timeout) + def txn3(tr): + tr.options.set_retry_limit(1) + tr[b'foo'] = b'bar' + tr.on_error(err).wait() # should not throw + tr.options.set_retry_limit(1) + tr[b'foo'] = b'bar' + try: + tr.on_error(err).wait() # should throw + raise TestError('(3) Retry limit was ignored.') + except fdb.FDBError as e: + if e.code != 1007: + raise + tr.reset() + tr[b'foo'] = b'bar' + tr.on_error(err).wait() # should not throw + + txn3(db) + + # (4) Number of retries does not survive resets + @retry_with_timeout(default_timeout) + def txn4(tr): + tr.options.set_retry_limit(0) + tr[b'foo'] = b'bar' + try: + tr.on_error(err).wait() # should throw + raise TestError('(4) Retry limit was ignored.') + except fdb.FDBError as e: + if e.code != 1007: + raise + tr.reset() + tr.options.set_retry_limit(1) + tr[b'foo'] = b'bar' + tr.on_error(err).wait() # should not throw + + txn4(db) + + # (5) Retries accumulate when limits are turned off, and are respected retroactively + @retry_with_timeout(default_timeout) + def txn5(tr): + tr[b'foo'] = b'bar' + tr.on_error(err).wait() # should not throw + tr[b'foo'] = b'bar' + tr.on_error(err).wait() # should not throw + tr.options.set_retry_limit(1) + try: + tr.on_error(err).wait() # should throw + raise TestError('(5) Retry limit was ignored.') + except fdb.FDBError as e: + if e.code != 1007: + raise + tr[b'foo'] = b'bar' + tr.options.set_retry_limit(-1) + tr.on_error(err).wait() # should not throw + tr.options.set_retry_limit(4) + try: + tr.on_error(err).wait() # should throw + raise TestError('(5) Retry limit was ignored.') + except fdb.FDBError as e: + if e.code != 1007: + raise + + txn5(db) + + # (6) Retry limits don't survive on_error() + @retry_with_timeout(default_timeout) + def txn6(tr): + tr.options.set_retry_limit(1) + tr[b'foo'] = b'bar' + tr.on_error(err).wait() # should not throw + tr[b'foo'] = b'bar' + tr.options.set_retry_limit(1) + try: + tr.on_error(err).wait() # should throw + raise TestError('(6) Retry limit was ignored.') + except fdb.FDBError as e: + if e.code != 1007: + raise + tr[b'foo'] = b'bar' + tr.on_error(err).wait() # should not throw + + txn6(db) + +def test_timeouts(db): + # (1) Basic timeouts + @retry_with_timeout(default_timeout) + def txn1(tr): + tr.options.set_timeout(10) + time.sleep(1) + try: + tr.commit().wait() # should throw + raise TestError("Timeout didn't fire.") + except fdb.FDBError as e: + if e.code != 1031: + raise + + txn1(db) + + # (2) Timeout does not survive on_error() + @retry_with_timeout(default_timeout) + def txn2(tr): + tr.options.set_timeout(100) + tr.on_error(fdb.FDBError(1007)).wait() # should not throw + time.sleep(1) + tr.commit().wait() # should not throw + + txn2(db) + + # (3) Timeout having fired survives on_error() + @retry_with_timeout(default_timeout) + def txn3(tr): + tr.options.set_timeout(100) + time.sleep(1) + try: + tr.on_error(fdb.FDBError(1007)).wait() # should throw + raise TestError("Timeout didn't fire.") + except fdb.FDBError as e: + if e.code != 1031: + raise + try: + tr.commit().wait() # should throw + raise TestError("Timeout didn't fire.") + except fdb.FDBError as e: + if e.code != 1031: + raise + + txn3(db) + + # (4) Timeout does not survive reset + @retry_with_timeout(default_timeout) + def txn4(tr): + tr.options.set_timeout(100) + tr.reset() + time.sleep(1) + tr.commit().wait() # should not throw + + txn4(db) + + # (5) Timeout having fired does not survive reset + @retry_with_timeout(default_timeout) + def txn5(tr): + tr.options.set_timeout(100) + time.sleep(1) + tr.reset() + tr.commit().wait() # should not throw + + txn5(db) + + # (6) Timeout will fire "retroactively" + @retry_with_timeout(default_timeout) + def txn6(tr): + tr[b'foo']=b'bar' + time.sleep(1) + tr.options.set_timeout(10) + try: + tr.commit().wait() # should throw + raise TestError("Timeout didn't fire.") + except fdb.FDBError as e: + if e.code != 1031: + raise + + txn6(db) + + # (7) Transaction reset also resets time from which timeout is measured + @retry_with_timeout(default_timeout) + def txn7(tr): + tr[b'foo']=b'bar' + time.sleep(1) + start = time.time() + tr.reset() + tr[b'foo']=b'bar' + tr.options.set_timeout(500) + try: + tr.commit().wait() #should not throw, but could if commit were slow: + except fdb.FDBError as e: + if e.code != 1031: + raise + if time.time() - start < 0.49: + raise + + + txn7(db) + + # (8) on_error() does not reset time from which timeout is measured + @retry_with_timeout(default_timeout) + def txn8(tr): + tr[b'foo']=b'bar' + time.sleep(1) + tr.on_error(fdb.FDBError(1007)).wait() # should not throw + tr[b'foo']=b'bar' + tr.options.set_timeout(100) + try: + tr.commit().wait() # should throw + raise TestError("Timeout didn't fire.") + except fdb.FDBError as e: + if e.code != 1031: + raise + + txn8(db) + + # (9) Timeouts can be unset + @retry_with_timeout(default_timeout) + def txn9(tr): + tr[b'foo']=b'bar' + tr.options.set_timeout(100) + tr[b'foo']=b'bar' + tr.options.set_timeout(0) + time.sleep(1) + tr.commit().wait() # should not throw + + txn9(db) + + # (10) Unsetting a timeout after it has fired doesn't help + @retry_with_timeout(default_timeout) + def txn10(tr): + tr[b'foo']=b'bar' + tr.options.set_timeout(100) + time.sleep(1) + tr.options.set_timeout(0) + try: + tr.commit().wait() # should throw + raise TestError("Timeout didn't fire.") + except fdb.FDBError as e: + if e.code != 1031: + raise + + txn10(db) + + # (11) An error thrown in commit does not reset time from which timeout is measured + @retry_with_timeout(default_timeout) + def txn11(tr): + for i in range(2): + tr.options.set_timeout(1500) + tr.set_read_version(0x7ffffffffffffff0) + x = tr[b'foo'] + try: + tr.commit().wait() + tr.reset() + except fdb.FDBError as e: + if i == 0: + if e.code != 1009: # future_version + raise fdb.FDBError(1007) # Something weird happened; raise a retryable error so we run this transaction again + else: + tr.on_error(e).wait() + elif i == 1 and e.code != 1031: + raise + + txn11(db) + +def test_combinations(db): + # (1) Hitting retry limit still clears timeouts + @retry_with_timeout(default_timeout) + def txn1(tr): + tr.options.set_retry_limit(0) + tr.options.set_timeout(100) + try: + tr.on_error(fdb.FDBError(1007)).wait() # should throw + raise TestError("Retry limit was ignored.") + except fdb.FDBError as e: + if e.code != 1007: + raise + time.sleep(1) + tr.commit().wait() # should not throw + + txn1(db) + + # (2) Cancellation does survive on_error() even when retry limit is hit + @retry_with_timeout(default_timeout) + def txn2(tr): + tr.options.set_retry_limit(0) + tr.cancel() + try: + tr.on_error(fdb.FDBError(1007)).wait() # should throw + raise TestError('on_error() did not notice cancellation.') + except fdb.FDBError as e: + if e.code != 1025: + raise + try: + tr.commit().wait() # should throw + raise TestError('Cancellation did not survive on_error().') + except fdb.FDBError as e: + if e.code != 1025: + raise + + txn2(db) diff --git a/bindings/python/tests/directory_extension.py b/bindings/python/tests/directory_extension.py new file mode 100644 index 0000000000..338d4f5a1d --- /dev/null +++ b/bindings/python/tests/directory_extension.py @@ -0,0 +1,228 @@ +# +# directory_extension.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import traceback +import sys + +import fdb +import fdb.directory_impl + +from fdb import six + +ops_that_create_dirs = [ + six.u('DIRECTORY_CREATE_SUBSPACE'), + six.u('DIRECTORY_CREATE_LAYER'), + six.u('DIRECTORY_CREATE_OR_OPEN'), + six.u('DIRECTORY_CREATE'), + six.u('DIRECTORY_OPEN'), + six.u('DIRECTORY_MOVE'), + six.u('DIRECTORY_MOVE_TO'), + six.u('DIRECTORY_OPEN_SUBSPACE'), +] + +log_all = False + +log_instructions = False +log_ops = False +log_dirs = False +log_errors = False + +def log_op(msg, force=False): + if log_ops or log_all or force: + print(msg) + +class DirectoryExtension(): + def __init__(self): + self.dir_list = [fdb.directory] + self.dir_index = 0 + self.error_index = 0 + + def pop_tuples(self, stack, num=None): + actual_num = num + if actual_num is None: + actual_num = 1 + + tuples = tuple([tuple(stack.pop(stack.pop())) for i in range(actual_num)]) + + if num is None: + return tuples[0] + + return tuples + + def append_dir(self, inst, dir): + if log_dirs or log_all: + print('pushed %s at %d (op=%s)' % (dir.__class__.__name__, len(self.dir_list), inst.op)) + + self.dir_list.append(dir) + + def process_instruction(self, inst): + try: + if log_all or log_instructions: + print("%d. %s" % (inst.index, inst.op)) + + directory = self.dir_list[self.dir_index] + if inst.op == six.u('DIRECTORY_CREATE_SUBSPACE'): + path = self.pop_tuples(inst.stack) + raw_prefix = inst.pop() + log_op('created subspace at %r: %r' % (path, raw_prefix)) + self.append_dir(inst, fdb.Subspace(path, raw_prefix)) + elif inst.op == six.u('DIRECTORY_CREATE_LAYER'): + index1, index2, allow_manual_prefixes = inst.pop(3) + if self.dir_list[index1] is None or self.dir_list[index2] is None: + log_op('create directory layer: None'); + self.append_dir(inst, None) + else: + log_op('create directory layer: node_subspace (%d) = %r, content_subspace (%d) = %r, allow_manual_prefixes = %d' % (index1, self.dir_list[index1].rawPrefix, index2, self.dir_list[index2].rawPrefix, allow_manual_prefixes)) + self.append_dir(inst, fdb.DirectoryLayer(self.dir_list[index1], self.dir_list[index2], allow_manual_prefixes == 1)) + elif inst.op == six.u('DIRECTORY_CHANGE'): + self.dir_index = inst.pop() + if not self.dir_list[self.dir_index]: + self.dir_index = self.error_index + if log_dirs or log_all: + new_dir = self.dir_list[self.dir_index] + clazz = new_dir.__class__.__name__ + new_path = repr(new_dir._path) if hasattr(new_dir, '_path') else "" + print('changed directory to %d (%s @%s)' % (self.dir_index, clazz, new_path)) + elif inst.op == six.u('DIRECTORY_SET_ERROR_INDEX'): + self.error_index = inst.pop() + elif inst.op == six.u('DIRECTORY_CREATE_OR_OPEN'): + path = self.pop_tuples(inst.stack) + layer = inst.pop() + log_op('create_or_open %s: layer=%s' % (repr(directory.get_path() + path), repr(layer))) + d = directory.create_or_open(inst.tr, path, layer or b'') + self.append_dir(inst, d) + elif inst.op == six.u('DIRECTORY_CREATE'): + path = self.pop_tuples(inst.stack) + layer, prefix = inst.pop(2) + log_op('create %s: layer=%s, prefix=%s' % (repr(directory.get_path() + path), repr(layer), repr(prefix))) + self.append_dir(inst, directory.create(inst.tr, path, layer or b'', prefix)) + elif inst.op == six.u('DIRECTORY_OPEN'): + path = self.pop_tuples(inst.stack) + layer = inst.pop() + log_op('open %s: layer=%s' % (repr(directory.get_path() + path), repr(layer))) + self.append_dir(inst, directory.open(inst.tr, path, layer or b'')) + elif inst.op == six.u('DIRECTORY_MOVE'): + old_path, new_path = self.pop_tuples(inst.stack, 2) + log_op('move %s to %s' % (repr(directory.get_path() + old_path), repr(directory.get_path() + new_path))) + self.append_dir(inst, directory.move(inst.tr, old_path, new_path)) + elif inst.op == six.u('DIRECTORY_MOVE_TO'): + new_absolute_path = self.pop_tuples(inst.stack) + log_op('move %s to %s' % (repr(directory.get_path()), repr(new_absolute_path))) + self.append_dir(inst, directory.move_to(inst.tr, new_absolute_path)) + elif inst.op == six.u('DIRECTORY_REMOVE'): + count = inst.pop() + if count == 0: + log_op('remove %s' % repr(directory.get_path())) + directory.remove(inst.tr) + else: + path = self.pop_tuples(inst.stack) + log_op('remove %s' % repr(directory.get_path() + path)) + directory.remove(inst.tr, path) + elif inst.op == six.u('DIRECTORY_REMOVE_IF_EXISTS'): + count = inst.pop() + if count == 0: + log_op('remove_if_exists %s' % repr(directory.get_path())) + directory.remove_if_exists(inst.tr) + else: + path = self.pop_tuples(inst.stack) + log_op('remove_if_exists %s' % repr(directory.get_path() + path)) + directory.remove_if_exists(inst.tr, path) + elif inst.op == six.u('DIRECTORY_LIST'): + count = inst.pop() + if count == 0: + result = directory.list(inst.tr) + log_op('list %s' % (repr(directory.get_path()))) + else: + path = self.pop_tuples(inst.stack) + result = directory.list(inst.tr, path) + log_op('list %s' % (repr(directory.get_path() + path))) + + inst.push(fdb.tuple.pack(tuple(result))) + elif inst.op == six.u('DIRECTORY_EXISTS'): + count = inst.pop() + if count == 0: + result = directory.exists(inst.tr) + log_op('exists %s: %d' % (repr(directory.get_path()), result)) + else: + path = self.pop_tuples(inst.stack) + result = directory.exists(inst.tr, path) + log_op('exists %s: %d' % (repr(directory.get_path() + path), result)) + + if result: + inst.push(1) + else: + inst.push(0) + elif inst.op == six.u('DIRECTORY_PACK_KEY'): + key_tuple = self.pop_tuples(inst.stack) + inst.push(directory.pack(key_tuple)) + elif inst.op == six.u('DIRECTORY_UNPACK_KEY'): + key = inst.pop() + log_op('unpack %s in subspace with prefix %s' % (repr(key), repr(directory.rawPrefix))) + tup = directory.unpack(key) + for t in tup: + inst.push(t) + elif inst.op == six.u('DIRECTORY_RANGE'): + tup = self.pop_tuples(inst.stack) + rng = directory.range(tup) + inst.push(rng.start) + inst.push(rng.stop) + elif inst.op == six.u('DIRECTORY_CONTAINS'): + key = inst.pop() + result = directory.contains(key) + if result: + inst.push(1) + else: + inst.push(0) + elif inst.op == six.u('DIRECTORY_OPEN_SUBSPACE'): + path = self.pop_tuples(inst.stack) + log_op('open_subspace %r (at %r)' % (path, directory.key())) + self.append_dir(inst, directory.subspace(path)) + elif inst.op == six.u('DIRECTORY_LOG_SUBSPACE'): + prefix = inst.pop() + inst.tr[prefix + fdb.tuple.pack((self.dir_index,))] = directory.key() + elif inst.op == six.u('DIRECTORY_LOG_DIRECTORY'): + prefix = inst.pop() + exists = directory.exists(inst.tr) + if exists: + children = tuple(directory.list(inst.tr)) + else: + children = () + logSubspace = fdb.Subspace((self.dir_index,), prefix) + inst.tr[logSubspace[six.u('path')]] = fdb.tuple.pack(directory.get_path()) + inst.tr[logSubspace[six.u('layer')]] = fdb.tuple.pack((directory.get_layer(),)) + inst.tr[logSubspace[six.u('exists')]] = fdb.tuple.pack((int(exists),)) + inst.tr[logSubspace[six.u('children')]] = fdb.tuple.pack(children) + elif inst.op == six.u('DIRECTORY_STRIP_PREFIX'): + s = inst.pop() + if not s.startswith(directory.key()): + raise Exception('String %s does not start with raw prefix %s' % (s, directory.key())) + + inst.push(s[len(directory.key()):]) + else: + raise Exception('Unknown op: %s' % inst.op) + except Exception as e: + if log_all or log_errors: + print(e) + #traceback.print_exc(file=sys.stdout) + + if inst.op in ops_that_create_dirs: + self.append_dir(inst, None) + + inst.push(b'DIRECTORY_ERROR') diff --git a/bindings/python/tests/tester.py b/bindings/python/tests/tester.py new file mode 100755 index 0000000000..b825872d28 --- /dev/null +++ b/bindings/python/tests/tester.py @@ -0,0 +1,505 @@ +#!/usr/bin/python +# +# tester.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +import sys +import os +import threading +import time +import random +import time +import traceback + +sys.path[:0]=[os.path.join(os.path.dirname(__file__), '..')] +import fdb +fdb.api_version(int(sys.argv[2])) + +from fdb import six +from fdb.impl import strinc +import fdb.tuple + +from directory_extension import DirectoryExtension + +from cancellation_timeout_tests import test_timeouts +from cancellation_timeout_tests import test_cancellation +from cancellation_timeout_tests import test_retry_limits +from cancellation_timeout_tests import test_combinations + +random.seed(0) + +if len(sys.argv) == 4: + db = fdb.open(sys.argv[3]) +else: + db = fdb.open() + +class Stack: + def __init__(self): + self.stack = [] + + def __repr__(self): + return repr(self.stack) + + def __str__(self): + return str(self.stack) + + def __len__(self): + return len(self.stack) + + def __getitem__(self, idx): + return self.stack[idx] + + def __setitem__(self, idx, val): + self.stack[idx] = val + + def push(self, idx, val): + self.stack.insert(0, (idx, val)) + + def pop(self, count=None, with_idx=False): + c = count + if c is None: + c = 1 + raw = self.stack[:c] + del self.stack[:c] + + for i in range(len(raw)): + if isinstance(raw[i][1], fdb.Future): + try: + val = raw[i][1].wait() + if val is None or (hasattr(val, 'present') and not val.present()): + raw[i] = (raw[i][0], b'RESULT_NOT_PRESENT') + else: + raw[i] = (raw[i][0], val) + except fdb.FDBError as e: + #print('ERROR: %s' % repr(e)) + raw[i] = (raw[i][0], fdb.tuple.pack((b'ERROR', str(e.code).encode('ascii')))) + + if count is None: + if with_idx: + return raw[0] + else: + return raw[0][1] + else: + if with_idx: + return raw + else: + return [item[1] for item in raw] + +class Instruction: + def __init__(self, tr, stack, op, index, isDatabase=False, isSnapshot=False): + self.tr = tr + self.stack = stack + self.op = op + self.index = index + self.isDatabase = isDatabase + self.isSnapshot = isSnapshot + + def pop(self, count=None, with_idx=False): + return self.stack.pop(count, with_idx) + + def push(self, val): + self.stack.push(self.index, val) + +@fdb.transactional +def test_options(tr): + tr.options.set_priority_system_immediate() + tr.options.set_priority_batch() + tr.options.set_causal_read_risky() + tr.options.set_causal_write_risky() + tr.options.set_read_your_writes_disable() + tr.options.set_read_ahead_disable() + tr.options.set_read_system_keys() + tr.options.set_access_system_keys() + tr.options.set_durability_dev_null_is_web_scale() + tr.options.set_timeout(60*1000); + tr.options.set_retry_limit(50); + tr.options.set_max_retry_delay(100); + tr.options.set_used_during_commit_protection_disable() + tr.options.set_transaction_logging_enable('my_transaction') + + tr.get(b'\xff').wait() + +def check_watches(db, watches, expected): + for i, watch in enumerate(watches): + if watch.is_ready() or expected: + try: + watch.wait() + if not expected: + assert False, "Watch %d is ready" % i + except fdb.FDBError as e: + tr = db.create_transaction() + tr.on_error(e).wait() + return False + + return True + +def test_watches(db): + while True: + db[b'w0'] = b'0' + db[b'w3'] = b'3' + + watches = [None] + + @fdb.transactional + def txn1(tr): + watches[0] = tr.watch(b'w0') + tr.set(b'w0', b'0') + assert not watches[0].is_ready() + + txn1(db) + + watches.append(db.clear_and_watch(b'w1')) + watches.append(db.set_and_watch(b'w2', b'2')) + watches.append(db.get_and_watch(b'w3')) + + assert watches[3][0] == b'3' + watches[3] = watches[3][1] + + time.sleep(1) + + if not check_watches(db, watches, False): + continue + + del db[b'w1'] + + time.sleep(5) + + if not check_watches(db, watches, False): + continue + + db[b'w0'] = b'a' + db[b'w1'] = b'b' + del db[b'w2'] + db.bit_xor(b'w3', b'\xff\xff') + + if check_watches(db, watches, True): + return + +@fdb.transactional +def test_locality(tr): + tr.options.set_timeout(60*1000) + tr.options.set_read_system_keys() # We do this because the last shard (for now, someday the last N shards) is in the /FF/ keyspace + + # This isn't strictly transactional, thought we expect it to be given the size of our database + boundary_keys = list(fdb.locality.get_boundary_keys(tr, b'', b'\xff\xff')) + [b'\xff\xff'] + end_keys = [tr.get_key(fdb.KeySelector.last_less_than(k)) for k in boundary_keys[1:]] + + start_addresses = [fdb.locality.get_addresses_for_key(tr, k) for k in boundary_keys[:-1]] + end_addresses = [fdb.locality.get_addresses_for_key(tr, k) for k in end_keys] + + if [set(s.wait()) for s in start_addresses] != [set(e.wait()) for e in end_addresses]: + raise Exception("Locality not internally consistent.") + +def test_predicates(): + assert fdb.predicates.is_retryable(fdb.FDBError(1020)) + assert not fdb.predicates.is_retryable(fdb.FDBError(10)) + +class Tester: + tr_map = { } + tr_map_lock = threading.RLock() + + def __init__(self, db, prefix): + self.db = db + + self.instructions = self.db[fdb.tuple.range((prefix,))] + + self.stack = Stack() + self.tr_name = prefix + Tester.tr_map[self.tr_name] = None + self.last_version = 0 + + self.threads = [] + self.directory_extension = DirectoryExtension() + + def push_range(self, inst, iter, prefix_filter=None): + kvs = [] + for k,v in iter: + if prefix_filter is None or k.startswith(prefix_filter): + kvs += [k,v] + + inst.push( fdb.tuple.pack( tuple(kvs) ) ) + + @staticmethod + @fdb.transactional + def wait_empty(tr, prefix): + res = tr.get_range_startswith(prefix, 1).to_list(); + if len(res) == 1: + raise fdb.FDBError(1020) + + @fdb.transactional + def log_stack(self, tr, prefix, entries): + for i, (idx, el) in entries.items(): + pk = prefix + fdb.tuple.pack((i, idx)) + pv = fdb.tuple.pack((el,)) + + tr.set(pk, pv[:40000]) + + + def current_transaction(self): + with Tester.tr_map_lock: + return Tester.tr_map[self.tr_name] + + def new_transaction(self): + with Tester.tr_map_lock: + Tester.tr_map[self.tr_name] = self.db.create_transaction() + + def switch_transaction(self, name): + self.tr_name = name + with Tester.tr_map_lock: + if not self.tr_name in Tester.tr_map: + self.new_transaction() + + def run(self): + for idx, i in enumerate(self.instructions): + op_tuple = fdb.tuple.unpack(i.value) + op = op_tuple[0] + + # print("Stack is %r" % self.stack) + # if op != "PUSH" and op != "SWAP": + # print("%d. Instruction is %s" % (idx, op)) + + isDatabase = op.endswith(six.u('_DATABASE')) + isSnapshot = op.endswith(six.u('_SNAPSHOT')) + + if isDatabase: + op = op[:-9] + obj = self.db + elif isSnapshot: + op = op[:-9] + obj = self.current_transaction().snapshot + else: + obj = self.current_transaction() + + inst = Instruction(obj, self.stack, op, idx, isDatabase, isSnapshot) + + try: + if inst.op == six.u("PUSH"): + inst.push(op_tuple[1]) + elif inst.op == six.u("DUP"): + inst.stack.push(*self.stack[0]) + elif inst.op == six.u("EMPTY_STACK"): + self.stack = Stack() + elif inst.op == six.u("SWAP"): + idx = inst.pop() + self.stack[0], self.stack[idx] = self.stack[idx], self.stack[0] + elif inst.op == six.u("POP"): + inst.pop() + elif inst.op == six.u("SUB"): + a, b = inst.pop(2) + inst.push(a - b) + elif inst.op == six.u("CONCAT"): + a, b = inst.pop(2) + inst.push(a + b) + elif inst.op == six.u("WAIT_FUTURE"): + old_idx, item = inst.pop(with_idx=True) + inst.stack.push(old_idx, item) + elif inst.op == six.u("NEW_TRANSACTION"): + self.new_transaction() + elif inst.op == six.u("USE_TRANSACTION"): + self.switch_transaction(inst.pop()) + elif inst.op == six.u("ON_ERROR"): + inst.push(inst.tr.on_error(inst.pop())) + elif inst.op == six.u("GET"): + key = inst.pop() + num = random.randint(0, 2) + if num == 0: + f = obj[key] + elif num == 1: + f = obj.get(key) + else: + f = obj.__getitem__(key) + + if f == None: + inst.push(b'RESULT_NOT_PRESENT') + else: + inst.push(f) + elif inst.op == six.u("GET_KEY"): + key, or_equal, offset, prefix = inst.pop(4) + result = obj.get_key(fdb.KeySelector(key, or_equal, offset)) + if result.startswith(prefix): + inst.push(result) + elif result < prefix: + inst.push(prefix) + else: + inst.push(strinc(prefix)) + + elif inst.op == six.u("GET_RANGE"): + begin, end, limit, reverse, mode = inst.pop(5) + if limit == 0 and mode == -1 and random.random() < 0.5: + if reverse: + r = obj[begin:end:-1] + else: + r = obj[begin:end] + else: + r = obj.get_range(begin, end, limit, reverse, mode) + + self.push_range(inst, r) + elif inst.op == six.u("GET_RANGE_STARTS_WITH"): + prefix, limit, reverse, mode = inst.pop(4) + self.push_range(inst, obj.get_range_startswith(prefix, limit, reverse, mode)) + elif inst.op == six.u("GET_RANGE_SELECTOR"): + begin_key, begin_or_equal, begin_offset, end_key, end_or_equal, end_offset, limit, reverse, mode, prefix = inst.pop(10) + beginSel = fdb.KeySelector(begin_key, begin_or_equal, begin_offset) + endSel = fdb.KeySelector(end_key, end_or_equal, end_offset) + if limit == 0 and mode == -1 and random.random() < 0.5: + if reverse: + r = obj[beginSel:endSel:-1] + else: + r = obj[beginSel:endSel] + else: + r = obj.get_range(beginSel, endSel, limit, reverse, mode) + + self.push_range(inst, r, prefix_filter=prefix) + elif inst.op == six.u("GET_READ_VERSION"): + self.last_version = obj.get_read_version().wait() + inst.push(b"GOT_READ_VERSION") + elif inst.op == six.u("SET"): + key, value = inst.pop(2) + if random.random() < 0.5: + obj[key] = value + else: + obj.set(key, value) + + if obj == self.db: + inst.push(b"RESULT_NOT_PRESENT") + elif inst.op == six.u("LOG_STACK"): + prefix = inst.pop() + entries = {} + while len(self.stack) > 0: + stack_index = len(self.stack)-1 + entries[stack_index] = inst.pop(with_idx=True) + if len(entries) == 100: + self.log_stack(self.db, prefix, entries) + entries = {} + + self.log_stack(self.db, prefix, entries) + elif inst.op == six.u("ATOMIC_OP"): + opType, key, value = inst.pop(3) + getattr(obj, opType.lower())(key, value) + + if obj == self.db: + inst.push(b"RESULT_NOT_PRESENT") + elif inst.op == six.u("SET_READ_VERSION"): + inst.tr.set_read_version( self.last_version ) + elif inst.op == six.u("CLEAR"): + if random.random() < 0.5: + del obj[ inst.pop() ] + else: + obj.clear(inst.pop()) + + if obj == self.db: + inst.push(b"RESULT_NOT_PRESENT") + elif inst.op == six.u("CLEAR_RANGE"): + begin, end = inst.pop(2) + num = random.randint(0, 2) + if num == 0: + del obj[begin:end] + elif num == 1: + obj.clear_range(begin, end) + else: + obj.__delitem__( slice( begin, end ) ) + + if obj == self.db: + inst.push(b"RESULT_NOT_PRESENT") + elif inst.op == six.u("CLEAR_RANGE_STARTS_WITH"): + obj.clear_range_startswith( inst.pop() ) + if obj == self.db: + inst.push(b"RESULT_NOT_PRESENT") + elif inst.op == six.u("READ_CONFLICT_RANGE"): + inst.tr.add_read_conflict_range( inst.pop(), inst.pop() ) + inst.push(b"SET_CONFLICT_RANGE") + elif inst.op == six.u("WRITE_CONFLICT_RANGE"): + inst.tr.add_write_conflict_range( inst.pop(), inst.pop() ) + inst.push(b"SET_CONFLICT_RANGE") + elif inst.op == six.u("READ_CONFLICT_KEY"): + inst.tr.add_read_conflict_key( inst.pop() ) + inst.push(b"SET_CONFLICT_KEY") + elif inst.op == six.u("WRITE_CONFLICT_KEY"): + inst.tr.add_write_conflict_key( inst.pop() ) + inst.push(b"SET_CONFLICT_KEY") + elif inst.op == six.u("DISABLE_WRITE_CONFLICT"): + inst.tr.options.set_next_write_no_write_conflict_range() + elif inst.op == six.u("COMMIT"): + inst.push(inst.tr.commit()) + elif inst.op == six.u("RESET"): + inst.tr.reset() + elif inst.op == six.u("CANCEL"): + inst.tr.cancel() + elif inst.op == six.u("GET_COMMITTED_VERSION"): + self.last_version = inst.tr.get_committed_version() + inst.push(b"GOT_COMMITTED_VERSION") + elif inst.op == six.u("GET_VERSIONSTAMP"): + inst.push(inst.tr.get_versionstamp()) + elif inst.op == six.u("TUPLE_PACK"): + count = inst.pop() + items = inst.pop(count) + inst.push(fdb.tuple.pack(tuple(items))) + elif inst.op == six.u("TUPLE_UNPACK"): + for i in fdb.tuple.unpack( inst.pop() ): + inst.push(fdb.tuple.pack((i,))) + elif inst.op == six.u("TUPLE_RANGE"): + count = inst.pop() + items = inst.pop(count) + r = fdb.tuple.range( tuple(items) ) + inst.push(r.start) + inst.push(r.stop) + elif inst.op == six.u("START_THREAD"): + t = Tester( self.db, inst.pop() ) + thr = threading.Thread(target=t.run) + thr.start() + self.threads.append(thr) + elif inst.op == six.u("WAIT_EMPTY"): + prefix = inst.pop() + Tester.wait_empty(self.db, prefix) + inst.push(b"WAITED_FOR_EMPTY") + elif inst.op == six.u("UNIT_TESTS"): + try: + db.options.set_location_cache_size(100001) + + test_options(db) + test_watches(db) + test_cancellation(db) + test_retry_limits(db) + test_timeouts(db) + test_combinations(db) + test_locality(db) + test_predicates() + + except fdb.FDBError as e: + print("Unit tests failed: %s" % e.description) + traceback.print_exc() + + raise Exception("Unit tests failed: %s" % e.description) + elif inst.op.startswith(six.u('DIRECTORY_')): + self.directory_extension.process_instruction(inst) + else: + raise Exception("Unknown op %s" % inst.op) + except fdb.FDBError as e: + # print('ERROR: %s' % repr(e)) + inst.stack.push( idx, fdb.tuple.pack( (b"ERROR", str(e.code).encode('ascii')) ) ) + + # print(" to %s" % self.stack) + # print() + + [thr.join() for thr in self.threads] + +if __name__ == '__main__': + t = Tester(db, sys.argv[1].encode('ascii')) + t.run() diff --git a/bindings/python/tests/tuple_tests.py b/bindings/python/tests/tuple_tests.py new file mode 100644 index 0000000000..4550da87a4 --- /dev/null +++ b/bindings/python/tests/tuple_tests.py @@ -0,0 +1,100 @@ +#!/usr/bin/python +# +# tuple_tests.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +import sys, random + +_range = range + +from fdb.tuple import pack, unpack, range +from fdb import six + +from fdb.six import u + +def randomElement(): + r = random.randint(0,4) + if r == 0: + chars = [b'\x00', b'\x01', b'a', b'7', b'\xfe', b'\ff'] + return b''.join([random.choice(chars) for c in _range(random.randint(0, 5))]) + elif r == 1: + chars = [u('\x00'), u('\x01'), u('a'), u('7'), u('\xfe'), u('\ff'), u('\u0000'), u('\u0001'), u('\uffff'), u('\uff00')] + return u('').join([random.choice(chars) for c in _range(random.randint(0, 10))]) + elif r == 2: + return random.choice([-1, 1]) * min(2**random.randint(0, 2040) + random.randint(-10, 10), 2**2040 - 1) + elif r == 3: + return random.choice([-1, 1]) * 2**random.randint(0, 64) + random.randint(-10, 10) + elif r == 4: + return None + +def randomTuple(): + return tuple( randomElement() for x in _range(random.randint(0,4)) ) + +def isprefix(a,b): + return tupleorder(a) == tupleorder(b[:len(a)]) + +def torder(x): + if x == None: + return 0 + elif type(x) == type(b''): + return 1 + elif isinstance(x, six.text_type): + return 2 + elif isinstance(x, six.integer_types): + return 3 + raise Exception("Unknown type") +def tupleorder(t): + return tuple( (torder(e),e) for e in t ) + +def tupleTest(N=10000): + someTuples = [ randomTuple() for i in _range(N) ] + a = sorted(someTuples, key=tupleorder) + b = sorted(someTuples, key=pack) + assert a == b + + print("Sort %d OK" % N) + + for i in _range(N): + t = randomTuple() + t2 = t + (randomElement(),) + t3 = randomTuple() + try: + assert(unpack(pack(t)) == t) + + r = range(t) + assert not (r.start <= pack(t) < r.stop) + assert (r.start <= pack(t2) < r.stop) + + if not isprefix(t, t3): + assert not (r.start <= pack(t3) <= r.stop) + + assert (tupleorder(t) < tupleorder(t3)) == (pack(t) < pack(t3)) + except: + print (repr(t), repr(t2), repr(t3)) + raise + + print ("Tuple check %d OK" % N) + +# test: +# a = ('\x00a', -2, 'b\x01', 12345, '') +# assert(a==fdbtuple.unpack(fdbtuple.pack(a))) + +if __name__=='__main__': + tupleTest() diff --git a/bindings/ruby/fdb.gemspec.in b/bindings/ruby/fdb.gemspec.in new file mode 100644 index 0000000000..f81015647e --- /dev/null +++ b/bindings/ruby/fdb.gemspec.in @@ -0,0 +1,22 @@ +# -*- mode: ruby; -*- + +Gem::Specification.new do |s| + s.name = 'fdb' + s.version = 'VERSION' + s.date = Time.new.strftime '%Y-%m-%d' + s.summary = "Ruby bindings for the FoundationDB database" + s.description = <<-EOF +Ruby bindings for the FoundationDB database. + +Complete documentation of the FoundationDB Ruby API can be found at: +https://foundationdb.org/documentation/api-ruby.html. +EOF + s.authors = ["FoundationDB"] + s.email = 'fdbopensource@apple.com' + s.files = ["LICENSE", "lib/fdb.rb", "lib/fdbdirectory.rb", "lib/fdbimpl.rb", "lib/fdblocality.rb", "lib/fdboptions.rb", "lib/fdbsubspace.rb", "lib/fdbtuple.rb"] + s.homepage = 'http://foundationdb.org' + s.license = 'Apache v2' + s.add_dependency('ffi', '>= 1.1.5') + s.required_ruby_version = '>= 1.9.3' + s.requirements << 'These bindings require the FoundationDB client. The client can be obtained from https://files.foundationdb.org/fdb-c/.' +end diff --git a/bindings/ruby/include.mk b/bindings/ruby/include.mk new file mode 100644 index 0000000000..becc43f011 --- /dev/null +++ b/bindings/ruby/include.mk @@ -0,0 +1,69 @@ +# +# include.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile-gmake; -*- + +TARGETS += fdb_ruby fdb_ruby_gem +CLEAN_TARGETS += fdb_ruby_clean fdb_ruby_gem_clean + +ifeq ($(PLATFORM),linux) + packages: fdb_ruby_gem + + packages_clean: fdb_ruby_gem_clean +endif + +ifeq ($(PLATFORM),linux) + GEM := gem +else ifeq ($(PLATFORM),osx) + GEM := gem +else + $(error Not prepared to build a gem on platform $(PLATFORM)) +endif + +ifeq ($(RELEASE),true) + GEMVER = $(VERSION) +else + GEMVER = $(VERSION)PRERELEASE +endif + +fdb_ruby: bindings/ruby/lib/fdboptions.rb + +bindings/ruby/lib/fdboptions.rb: bin/vexillographer.exe fdbclient/vexillographer/fdb.options + @echo "Building $@" + @$(MONO) bin/vexillographer.exe fdbclient/vexillographer/fdb.options ruby $@ + +fdb_ruby_clean: + @echo "Cleaning fdb_ruby" + @rm -f bindings/ruby/lib/fdboptions.rb + +fdb_ruby_gem_clean: + @echo "Cleaning RubyGem" + @rm -f packages/fdb-*.gem bindings/ruby/fdb.gemspec + +bindings/ruby/fdb.gemspec: bindings/ruby/fdb.gemspec.in $(ALL_MAKEFILES) versions.target + @m4 -DVERSION=$(GEMVER) $< > $@ + +fdb_ruby_gem: bindings/ruby/fdb.gemspec fdb_ruby + @echo "Packaging RubyGem" + @mkdir -p packages + @rm -f packages/fdb-*.gem + @cp LICENSE bindings/ruby/LICENSE + @(cd $(= 0 + if@@chosen_version != version + raise "FDB API already loaded at version #{@@chosen_version}." + end + return + end + + if version < 14 + raise "FDB API versions before 14 are not supported" + end + + if version > header_version + raise "Latest known FDB API version is #{header_version}" + end + + @@chosen_version = version + + require_relative 'fdbimpl' + + err = FDBC.fdb_select_api_version_impl(version, header_version) + if err.nonzero? + if err == 2203 + max_supported_version = FDBC.fdb_get_max_api_version() + if header_version > max_supported_version + raise "This version of the FoundationDB Ruby binding is not supported by the installed FoundationDB C library. The binding requires a library that supports API version #{header_version}, but the installed library supports a maximum version of #{max_supported_version}." + + else + raise "API version #{version} is not supported by the installed FoundationDB C library." + end + end + raise "FoundationDB API version error" + end + + require_relative 'fdbtuple' + require_relative 'fdbdirectory' + if version > 22 + require_relative 'fdblocality' + end + + return + end +end diff --git a/bindings/ruby/lib/fdbdirectory.rb b/bindings/ruby/lib/fdbdirectory.rb new file mode 100644 index 0000000000..cb4657adfa --- /dev/null +++ b/bindings/ruby/lib/fdbdirectory.rb @@ -0,0 +1,689 @@ +# +# fdbdirectory.rb +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#encoding: BINARY + +# FoundationDB Ruby API + +# Documentation for this API can be found at +# https://foundationdb.org/documentation/api-ruby.html + +require 'thread' + +require_relative 'fdbimpl' +require_relative 'fdbsubspace' + +module FDB + class AllocatorTransactionState + def initialize() + @lock = Mutex.new + end + + attr_reader :lock + end + + class HighContentionAllocator + def initialize(subspace) + @counters = subspace[0] + @recent = subspace[1] + @lock = Mutex.new + end + + def allocate(db_or_tr) + db_or_tr.transact do |tr| + if !tr.instance_variable_defined?(:@__fdb_directory_layer_hca_state__) + @lock.synchronize do + if !tr.instance_variable_defined?(:@__fdb_directory_layer_hca_state__) + tr.instance_variable_set(:@__fdb_directory_layer_hca_state__, AllocatorTransactionState.new) + end + end + end + + tr_state = tr.instance_variable_get(:@__fdb_directory_layer_hca_state__) + + loop do + start, count = + tr.snapshot.get_range(@counters.range[0], + @counters.range[1], + {:limit => 1, :reverse => true}) + .map { |kv| + [ @counters.unpack(kv.key)[0], kv.value.unpack('q<')[0] ] + }.first || [0,0] + + window = 0 + window_advanced = false + loop do + tr_state.lock.synchronize do + if window_advanced + tr.clear_range(@counters, @counters[start]) + tr.options.set_next_write_no_write_conflict_range() + tr.clear_range(@recent, @recent[start]) + end + + tr.add(@counters[start], [1].pack('q<')) + count = tr.snapshot[@counters[start]] + end + + count = count.nil? ? 0 : count.unpack('q<')[0] + window = window_size(start) + if count * 2 < window + break + end + + start += window + window_advanced = true + end + + candidate = 0 + found = false + loop do + candidate = rand(start...start+window) + latest_counter = nil + candidate_value = nil + + tr_state.lock.synchronize do + latest_counter = tr.snapshot.get_range(@counters.range[0], + @counters.range[1], + {:limit => 1, :reverse => true}) + candidate_value = tr[@recent[candidate]] + tr.options.set_next_write_no_write_conflict_range() + tr[@recent[candidate]] = '' + end + + latest_counter = latest_counter.map{ |kv| [ @counters.unpack(kv.key)[0] ] }.first || [0] + if latest_counter.length > 0 and latest_counter[0] > start + break + end + + if candidate_value.nil? + found = true + tr.add_write_conflict_key(@recent[candidate]) + break + end + end + + if found + break FDB::Tuple.pack([candidate]) + end + end + end + end + + def window_size(start) + if start < 255 + 64 + elsif start < 65535 + 1024 + else + 8192 + end + end + + private :window_size + end + + class DirectoryLayer + @@SUBDIRS = 0 + @@VERSION = [1,0,0] + + def initialize(options={}) + defaults = { :node_subspace => Subspace.new([], "\xfe"), + :content_subspace =>Subspace.new, + :allow_manual_prefixes => false } + + options = defaults.merge(options) + + @content_subspace = options[:content_subspace] + @node_subspace = options[:node_subspace] + @allow_manual_prefixes = options[:allow_manual_prefixes] + + @root_node = @node_subspace[@node_subspace.key] + @allocator = HighContentionAllocator.new(@root_node['hca']) + + @path = [] + @layer = '' + end + + def path + return @path.dup + end + + def layer + return @layer.dup + end + + attr_writer :path + private :path= + + def create_or_open(db_or_tr, path, options={}) + create_or_open_internal(db_or_tr, path, true, true, options) + end + + def open(db_or_tr, path, options={}) + create_or_open_internal(db_or_tr, path, false, true, options) + end + + def create(db_or_tr, path, options={}) + create_or_open_internal(db_or_tr, path, true, false, options) + end + + def move_to(db_or_tr, new_absolute_path) + raise 'The root directory cannot be moved.' + end + + def move(db_or_tr, old_path, new_path) + db_or_tr.transact do |tr| + check_version(tr, true) + + old_path = to_unicode_path(old_path) + new_path = to_unicode_path(new_path) + + if old_path == new_path[0...old_path.length] + raise ArgumentError, + 'The desination directory cannot be a subdirectory of the source directory.' + end + + old_node = find(tr, old_path).prefetch_metadata(tr) + new_node = find(tr, new_path).prefetch_metadata(tr) + + raise ArgumentError, 'The source directory does not exist.' unless old_node.exists? + + if old_node.is_in_partition? || new_node.is_in_partition? + if !old_node.is_in_partition? || + !new_node.is_in_partition? || + old_node.path != new_node.path + then + raise ArgumentError, 'Cannot move between partitions' + end + + next new_node + .get_contents(self) + .move(tr, old_node.get_partition_subpath, new_node.get_partition_subpath) + end + + if new_node.exists? + raise ArgumentError, 'The destination directory already exists. Remove it first.' + end + + parent_node = find(tr, new_path[0...-1]) + if !parent_node.exists? + raise ArgumentError, + 'The parent directory of the destination directory does not exist. Create it first.' + end + + tr[parent_node.subspace[@@SUBDIRS][new_path[-1]]] = + @node_subspace.unpack(old_node.subspace.key)[0] + remove_from_parent(tr, old_path) + + contents_of_node(old_node.subspace, new_path, old_node.layer) + end + end + + def remove(db_or_tr, path=[]) + remove_internal(db_or_tr, path, true) + end + + def remove_if_exists(db_or_tr, path=[]) + remove_internal(db_or_tr, path, false) + end + + def list(db_or_tr, path=[]) + db_or_tr.transact do |tr| + check_version(tr, false) + + path = to_unicode_path(path) + node = find(tr, path).prefetch_metadata(tr) + + raise ArgumentError, 'The directory does not exist.' unless node.exists? + + if node.is_in_partition?(nil, true) + next node.get_contents(self).list(tr, node.get_partition_subpath) + end + + subdir_names_and_nodes(tr, node.subspace).map { |name, node| name } + end + end + + def exists?(db_or_tr, path=[]) + db_or_tr.transact do |tr| + check_version(tr, false) + + path = to_unicode_path(path) + node = find(tr, path).prefetch_metadata(tr) + + next false if !node.exists? + + if node.is_in_partition? + next node.get_contents(self).exists?(tr, node.get_partition_subpath) + end + + true + end + end + + protected + + def create_or_open_internal(db_or_tr, path, allow_create, allow_open, options={}) + defaults = { :layer => '', :prefix => nil } + options = defaults.merge(options) + + if !options[:prefix].nil? and allow_open and allow_create + raise ArgumentError, 'Cannot specify a prefix when calling create_or_open.' + end + + if !options[:prefix].nil? and !@allow_manual_prefixes + if @path.length == 0 + raise ArgumentError, 'Cannot specify a prefix unless manual prefixes are enabled.' + else + raise ArgumentError, 'Cannot specify a prefix in a partition.' + end + end + + db_or_tr.transact do |tr| + check_version(tr, false) + path = to_unicode_path(path) + + raise ArgumentError, 'The root directory cannot be opened.' if path.length == 0 + + existing_node = find(tr, path).prefetch_metadata(tr) + + if existing_node.exists? + if existing_node.is_in_partition? + subpath = existing_node.get_partition_subpath + existing_node.get_contents(self).directory_layer.create_or_open_internal(tr, subpath, allow_create, allow_open, options) + else + raise ArgumentError, 'The directory already exists.' unless allow_open + open_directory(path, options, existing_node) + end + else + raise ArgumentError, 'The directory does not exist.' unless allow_create + create_directory(tr, path, options) + end + end + end + def open_directory(path, options, existing_node) + if options[:layer] and !options[:layer].empty? and options[:layer] != existing_node.layer + raise 'The directory was created with an incompatible layer.' + end + existing_node.get_contents(self) + end + + def create_directory(tr, path, options) + check_version(tr, true) + + prefix = options[:prefix] + if prefix.nil? + prefix = @content_subspace.key + @allocator.allocate(tr) + if !tr.get_range_start_with(prefix, { :limit => 1 }).to_a.empty? + raise "The database has keys stored at the prefix chosen by the automatic prefix allocator: #{prefix.dump}." + end + + if !is_prefix_free?(tr.snapshot, prefix) + raise 'The directory layer has manually allocated prefixes that conflict with the automatic prefix allocator.' + end + elsif !is_prefix_free?(tr, prefix) + raise ArgumentError, 'The given prefix is already in use.' + end + + parent_node = if path[0...-1].length > 0 + node_with_prefix(create_or_open(tr, path[0...-1]).key) + else + @root_node + end + + raise 'The parent directory does not exist.' unless parent_node + + node = node_with_prefix(prefix) + tr[parent_node[@@SUBDIRS][path[-1]]] = prefix + tr[node['layer']] = options[:layer] + + contents_of_node(node, path, options[:layer]) + end + + def remove_internal(db_or_tr, path, fail_on_nonexistent) + db_or_tr.transact do |tr| + check_version(tr, true) + + path = to_unicode_path(path) + + if path.empty? + raise ArgumentError, 'The root directory cannot be removed.' + end + + node = find(tr, path).prefetch_metadata(tr) + + if !node.exists? + raise ArgumentError, 'The directory does not exist.' if fail_on_nonexistent + next false + end + + if node.is_in_partition? + next node.get_contents(self).directory_layer + .remove_internal(tr, node.get_partition_subpath, fail_on_nonexistent) + end + + remove_recursive(tr, node.subspace) + remove_from_parent(tr, path) + true + end + end + + private + + def check_version(tr, write_access) + version = tr[@root_node['version']] + + initialize_directory(tr) if !version && write_access + return if !version + + version = version.to_s.unpack('III<') + + dir_ver = "#{version[0]}.#{version[1]}.#{version[2]}" + layer_ver = "#{@@VERSION[0]}.#{@@VERSION[1]}.#{@@VERSION[2]}" + + if version[0] != @@VERSION[0] + raise "Cannot load directory with version #{dir_ver} using directory layer #{layer_ver}" + elsif version[1] != @@VERSION[1] && write_access + raise "Directory with version #{dir_ver} is read-only + when opened using directory layer #{layer_ver}" + end + end + + def initialize_directory(tr) + tr[@root_node['version']] = @@VERSION.pack('III<') + end + + def node_containing_key(tr, key) + return @root_node if key.start_with?(@node_subspace.key) + + tr.get_range(@node_subspace.range[0], + @node_subspace.pack([key]) + "\x00", + { :reverse => true, :limit => 1}) + .map { |kv| + prev_prefix = @node_subspace.unpack(kv.key)[0] + node_with_prefix(prev_prefix) if key.start_with?(prev_prefix) + }[0] + end + + def node_with_prefix(prefix) + @node_subspace[prefix] if !prefix.nil? + end + + def contents_of_node(node, path, layer='') + prefix = @node_subspace.unpack(node.key)[0] + if layer == 'partition' + DirectoryPartition.new(@path + path, prefix, self) + else + DirectorySubspace.new(@path + path, prefix, self, layer) + end + end + + def find(tr, path) + node = Internal::Node.new(@root_node, [], path) + path.each_with_index do |name, index| + node = Internal::Node.new(node_with_prefix(tr[node.subspace[@@SUBDIRS][name]]), + path[0..index], path) + + return node unless node.exists? and node.layer(tr) != 'partition' + end + + node + end + + def subdir_names_and_nodes(tr, node) + subdir = node[@@SUBDIRS] + tr.get_range(subdir.range[0], subdir.range[1]).map { |kv| + [subdir.unpack(kv.key)[0], node_with_prefix(kv.value)] + } + end + + def remove_from_parent(tr, path) + parent = find(tr, path[0...-1]) + tr.clear(parent.subspace[@@SUBDIRS][path[-1]]) + end + + def remove_recursive(tr, node) + subdir_names_and_nodes(tr, node).each do |name, subnode| + remove_recursive(tr, subnode) + end + + tr.clear_range_start_with(@node_subspace.unpack(node.key)[0]) + tr.clear_range(node.range[0], node.range[1]) + end + + def is_prefix_free?(tr, prefix) + prefix && + prefix.length > 0 && + !node_containing_key(tr, prefix) && + tr.get_range(@node_subspace.pack([prefix]), @node_subspace.pack([tr.send(:strinc, prefix)]), + { :limit => 1 }).to_a.empty? + end + + def convert_path_element(name) + if !name.kind_of? String + raise TypeError, 'Invalid path: must be a unicode string or an array of unicode strings' + end + name.dup.force_encoding('UTF-8') + end + + def to_unicode_path(path) + if path.respond_to? 'each_with_index' + path.each_with_index { |name, index| path[index] = convert_path_element(name) } + else + [convert_path_element(path)] + end + end + end + + @@directory = DirectoryLayer.new + def self.directory + @@directory + end + + class DirectorySubspace < Subspace + def initialize(path, prefix, directory_layer=FDB::directory, layer='') + super([], prefix) + @path = path + @layer = layer + @directory_layer = directory_layer + end + + def path + return @path.dup + end + + def layer + return @layer.dup + end + + attr_reader :directory_layer + + def create_or_open(db_or_tr, name_or_path, options={}) + path = tuplify_path(name_or_path) + @directory_layer.create_or_open(db_or_tr, partition_subpath(path), options) + end + + def open(db_or_tr, name_or_path, options={}) + path = tuplify_path(name_or_path) + @directory_layer.open(db_or_tr, partition_subpath(path), options) + end + + def create(db_or_tr, name_or_path, options={}) + path = tuplify_path(name_or_path) + @directory_layer.create(db_or_tr, partition_subpath(path), options) + end + + def list(db_or_tr, name_or_path=[]) + path = tuplify_path(name_or_path) + @directory_layer.list(db_or_tr, partition_subpath(path)) + end + + def move(db_or_tr, old_name_or_path, new_name_or_path) + old_path = tuplify_path(old_name_or_path) + new_path = tuplify_path(new_name_or_path) + @directory_layer.move(db_or_tr, partition_subpath(old_path), partition_subpath(new_path)) + end + + def move_to(db_or_tr, new_absolute_name_or_path) + directory_layer = get_layer_for_path([]) + new_absolute_path = directory_layer.send(:to_unicode_path, new_absolute_name_or_path) + partition_len = directory_layer.path.length + partition_path = new_absolute_path[0...partition_len] + raise ArgumentError, 'Cannot move between partitions.' if partition_path != directory_layer.path + directory_layer.move(db_or_tr, @path[partition_len..-1], + new_absolute_path[partition_len..-1]) + end + + def remove(db_or_tr, name_or_path=[]) + path = tuplify_path(name_or_path) + directory_layer = get_layer_for_path(path) + directory_layer.remove(db_or_tr, partition_subpath(path, directory_layer)) + end + + def remove_if_exists(db_or_tr, name_or_path=[]) + path = tuplify_path(name_or_path) + directory_layer = get_layer_for_path(path) + directory_layer.remove_if_exists(db_or_tr, partition_subpath(path, directory_layer)) + end + + def exists?(db_or_tr, name_or_path=[]) + path = tuplify_path(name_or_path) + directory_layer = get_layer_for_path(path) + directory_layer.exists?(db_or_tr, partition_subpath(path, directory_layer)) + end + + def tuplify_path(path) + if path.is_a? String + [path] + else + path + end + end + private :tuplify_path + + def partition_subpath(path, directory_layer = @directory_layer) + self.path[directory_layer.path.length..-1] + path + end + private :partition_subpath + + def get_layer_for_path(path) + @directory_layer + end + private :get_layer_for_path + end + + class DirectoryPartition < DirectorySubspace + def initialize(path, prefix, parent_directory_layer) + directory_layer = DirectoryLayer.new(:node_subspace => Subspace.new([], prefix + "\xfe"), + :content_subspace => Subspace.new([], prefix)) + directory_layer.send(:path=, path) + super(path, prefix, directory_layer, 'partition') + @parent_directory_layer = parent_directory_layer + end + + def [](name) + raise 'Cannot open subspace in the root of a directory partition.' + end + + def key + raise 'Cannot get key for the root of a directory partition.' + end + + def pack(tuple) + raise 'Cannot pack keys using the root of a directory partition.' + end + + def unpack(key) + raise 'Cannot unpack keys using the root of a directory partition.' + end + + def range(tuple=[]) + raise 'Cannot get range for the root of a directory partition.' + end + + def contains?(key) + raise 'Cannot check whether a key belongs to the root of a directory partition.' + end + + def as_foundationdb_key + raise 'Cannot use the root of a directory partition as a key.' + end + + def subspace(tuple) + raise 'Cannot open subspace in the root of a directory partition.' + end + + def get_layer_for_path(path) + if path.length == 0 + @parent_directory_layer + else + @directory_layer + end + end + private :get_layer_for_path + end + + module Internal + class Node + def initialize(subspace, path, target_path) + @subspace = subspace + @path = path + @target_path = target_path + @layer = nil + end + + attr_reader :subspace + attr_reader :path + + def exists? + !@subspace.nil? + end + + def prefetch_metadata(tr) + layer(tr) if exists? + self + end + + def layer(tr=nil) + if tr + @layer = tr[@subspace['layer']] + else + raise 'Layer has not been read' unless @layer + end + + @layer + end + + def is_in_partition?(tr=nil, include_empty_subpath=false) + exists? && + @layer == 'partition' && + (include_empty_subpath || @path.length < @target_path.length) + end + + def get_partition_subpath(tr=nil) + @target_path[@path.length..-1] + end + + def get_contents(directory_layer, tr=nil) + directory_layer.send(:contents_of_node, @subspace, @path, layer(tr)) + end + end + end +end diff --git a/bindings/ruby/lib/fdbimpl.rb b/bindings/ruby/lib/fdbimpl.rb new file mode 100644 index 0000000000..27660e13c1 --- /dev/null +++ b/bindings/ruby/lib/fdbimpl.rb @@ -0,0 +1,1017 @@ +# +# fdbimpl.rb +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#encoding: BINARY + +# FoundationDB Ruby API + +# Documentation for this API can be found at +# https://foundationdb.org/documentation/api-ruby.html + +require 'ffi' + +require 'thread' +require 'monitor' + +require_relative 'fdboptions' + +module FDB + module FDBC + require 'rbconfig' + + if RbConfig::CONFIG['host_cpu'] != "x86_64" + raise LoadError, "FoundationDB API only supported on x86_64 (not #{RbConfig::CONFIG['host_cpu']})" + end + + case RbConfig::CONFIG['host_os'] + when /linux/ + dlobj = 'libfdb_c.so' + when /darwin/ + dlobj = 'libfdb_c.dylib' + when /mswin|mingw|cygwin/ + dlobj = 'fdb_c.dll' + if Gem.loaded_specs['ffi'].version < Gem::Version.create('1.7.0.dev') + raise LoadError, "You must install ffi >= 1.7.0.dev on 64-bit Windows (see https://github.com/ffi/ffi/issues/259)" + end + else + raise LoadError, "FoundationDB API is not supported on #{RbConfig::CONFIG['host_os']}" + end + + extend FFI::Library + begin + ffi_lib [File.join( File.dirname(__FILE__), dlobj ), dlobj] + rescue LoadError => e + raise $!, "#{$!} (is the FoundationDB client library installed?)" + end + + typedef :int, :fdb_error + typedef :int, :fdb_bool + + attach_function :fdb_get_error, [ :fdb_error ], :string + + attach_function :fdb_network_set_option, [ :int, :pointer, :int ], :fdb_error + attach_function :fdb_setup_network, [ ], :fdb_error + attach_function :fdb_run_network, [ ], :fdb_error, :blocking => true + attach_function :fdb_stop_network, [ ], :fdb_error + + attach_function :fdb_future_cancel, [ :pointer ], :void + attach_function :fdb_future_release_memory, [ :pointer ], :void + attach_function :fdb_future_destroy, [ :pointer ], :void + attach_function :fdb_future_block_until_ready, [ :pointer ], :fdb_error, :blocking => true + attach_function :fdb_future_is_ready, [ :pointer ], :fdb_bool + + callback :fdb_future_callback, [ :pointer, :pointer ], :void + attach_function :fdb_future_set_callback, [ :pointer, :fdb_future_callback, :pointer ], :fdb_error + + attach_function :fdb_future_get_error, [ :pointer ], :fdb_error + attach_function :fdb_future_get_version, [ :pointer, :pointer ], :fdb_error + attach_function :fdb_future_get_key, [ :pointer, :pointer, :pointer ], :fdb_error + attach_function :fdb_future_get_cluster, [ :pointer, :pointer ], :fdb_error + attach_function :fdb_future_get_database, [ :pointer, :pointer ], :fdb_error + attach_function :fdb_future_get_value, [ :pointer, :pointer, :pointer, :pointer ], :fdb_error + attach_function :fdb_future_get_keyvalue_array, [ :pointer, :pointer, :pointer, :pointer ], :fdb_error + attach_function :fdb_future_get_string_array, [ :pointer, :pointer, :pointer ], :fdb_error + + attach_function :fdb_create_cluster, [ :string ], :pointer + attach_function :fdb_cluster_destroy, [ :pointer ], :void + attach_function :fdb_cluster_set_option, [ :pointer, :int, :pointer, :int ], :fdb_error + + attach_function :fdb_cluster_create_database, [ :pointer, :pointer, :int ], :pointer + attach_function :fdb_database_destroy, [ :pointer ], :void + attach_function :fdb_database_set_option, [ :pointer, :int, :pointer, :int ], :fdb_error + + attach_function :fdb_database_create_transaction, [ :pointer, :pointer ], :fdb_error + attach_function :fdb_transaction_destroy, [ :pointer ], :void + attach_function :fdb_transaction_cancel, [ :pointer ], :void + attach_function :fdb_transaction_atomic_op, [ :pointer, :pointer, :int, :pointer, :int, :int ], :void + attach_function :fdb_transaction_add_conflict_range, [ :pointer, :pointer, :int, :pointer, :int, :int ], :int + attach_function :fdb_transaction_get_addresses_for_key, [ :pointer, :pointer, :int ], :pointer + attach_function :fdb_transaction_set_option, [ :pointer, :int, :pointer, :int ], :fdb_error + attach_function :fdb_transaction_set_read_version, [ :pointer, :int64 ], :void + attach_function :fdb_transaction_get_read_version, [ :pointer ], :pointer + attach_function :fdb_transaction_get, [ :pointer, :pointer, :int, :int ], :pointer + attach_function :fdb_transaction_get_key, [ :pointer, :pointer, :int, :int, :int, :int ], :pointer + attach_function :fdb_transaction_get_range, [ :pointer, :pointer, :int, :int, :int, :pointer, :int, :int, :int, :int, :int, :int, :int, :int, :int ], :pointer + attach_function :fdb_transaction_set, [ :pointer, :pointer, :int, :pointer, :int ], :void + attach_function :fdb_transaction_clear, [ :pointer, :pointer, :int ], :void + attach_function :fdb_transaction_clear_range, [ :pointer, :pointer, :int, :pointer, :int ], :void + attach_function :fdb_transaction_watch, [ :pointer, :pointer, :int ], :pointer + attach_function :fdb_transaction_commit, [ :pointer ], :pointer + attach_function :fdb_transaction_get_committed_version, [ :pointer, :pointer ], :fdb_error + attach_function :fdb_transaction_get_versionstamp, [ :pointer ], :pointer + attach_function :fdb_transaction_on_error, [ :pointer, :fdb_error ], :pointer + attach_function :fdb_transaction_reset, [ :pointer ], :void + + attach_function :fdb_select_api_version_impl, [ :int, :int ], :fdb_error + attach_function :fdb_get_max_api_version, [ ], :int + + class KeyValueStruct < FFI::Struct + pack 4 + layout :key, :pointer, + :key_length, :int, + :value, :pointer, + :value_length, :int + end + + def self.check_error(code) + raise Error.new(code) if code.nonzero? + nil + end + end + + @@cb_mutex = Mutex.new + def self.cb_mutex + @@cb_mutex + end + + class CallbackEntry + attr_accessor :callback + attr_accessor :index + + def initialize + @callback = nil + @index = nil + end + end + + @@ffi_callbacks = [] + def self.ffi_callbacks + @@ffi_callbacks + end + + [ "Network", "Cluster", "Database", "Transaction" ].each do |scope| + klass = FDB.const_set("#{scope}Options", Class.new) + klass.class_eval do + define_method(:initialize) do |setfunc| + instance_variable_set("@setfunc", setfunc) + end + end + class_variable_get("@@#{scope}Option").each_pair do |k,v| + p = + case v[2] + when NilClass then + Proc.new do || @setfunc.call(v[0], nil) end + when String then + Proc.new do |opt=nil| @setfunc.call(v[0], (opt.nil? ? opt : opt.encode('UTF-8')) ) end + when Fixnum then + Proc.new do |opt| @setfunc.call(v[0], [opt].pack("q<")) end + else + raise ArgumentError, "Don't know how to set options of type #{v[2].class}" + end + klass.send :define_method, "set_#{k.downcase}", p + end + end + + def self.key_to_bytes(k) + if k.respond_to? 'as_foundationdb_key' + k.as_foundationdb_key + else + k + end + end + + def self.value_to_bytes(v) + if v.respond_to? 'as_foundationdb_value' + v.as_foundationdb_value + else + v + end + end + + def self.strinc(key) + key = key.gsub(/\xff*\z/, '') + raise ArgumentError, 'Key must contain at least one byte not equal to 0xFF.' if key.length == 0 + + key[0..-2] + (key[-1].ord + 1).chr + end + + @@options = NetworkOptions.new lambda { |code, param| + FDBC.check_error FDBC.fdb_network_set_option(code, param, param.nil? ? 0 : param.bytesize) + } + def FDB.options + @@options + end + + @@network_thread = nil + @@network_thread_monitor = Monitor.new + + def self.init() + @@network_thread_monitor.synchronize do + if !@@network_thread.nil? + raise Error.new(2000) + end + + begin + @@network_thread = Thread.new do + @@network_thread_monitor.synchronize do + # Don't start running until init releases this + end + # puts "Starting FDB network" + begin + FDBC.check_error FDBC.fdb_run_network + rescue Error => e + $stderr.puts "Unhandled error in FoundationDB network thread: #{e.to_s}" + end + end + + FDBC.check_error FDBC.fdb_setup_network + rescue + @@network_thread.kill + @@network_thread = nil + raise + end + end + + nil + end + + def self.stop() + FDBC.check_error FDBC.fdb_stop_network + end + + at_exit do + if !@@network_thread.nil? + # puts "Stopping FDB network" + stop + @@network_thread.join + end + end + + @@open_clusters = {} + @@open_databases = {} + @@cache_lock = Mutex.new + + def self.open( cluster_file = nil, database_name = "DB" ) + @@network_thread_monitor.synchronize do + if ! @@network_thread + init + end + end + + @@cache_lock.synchronize do + if ! @@open_clusters.has_key? cluster_file + @@open_clusters[cluster_file] = create_cluster( cluster_file ) + end + + if ! @@open_databases.has_key? [cluster_file, database_name] + @@open_databases[[cluster_file, database_name]] = @@open_clusters[cluster_file].open_database(database_name) + end + + @@open_databases[[cluster_file, database_name]] + end + end + + class Error < StandardError + attr_reader :code + + def initialize(code) + @code = code + @description = nil + end + + def description + if !@description + @description = FDBC.fdb_get_error(@code) + end + @description + end + + def to_s + "#{description} (#{@code})" + end + end + + class Future + def self.finalize(ptr) + proc do + #puts "Destroying future #{ptr}" + FDBC.fdb_future_destroy(ptr) + end + end + + def initialize(fpointer) + @fpointer = fpointer + ObjectSpace.define_finalizer(self, FDB::Future.finalize(@fpointer)) + end + + def cancel + FDBC.fdb_future_cancel(@fpointer) + end + + def ready? + return !FDBC.fdb_future_is_ready(@fpointer).zero? + end + + def block_until_ready + FDBC.check_error FDBC.fdb_future_block_until_ready(@fpointer) + end + + def on_ready(&block) + def callback_wrapper(f, &block) + begin + yield f + rescue Exception + begin + $stderr.puts "Discarding uncaught exception from user callback:" + $stderr.puts "#{$@.first}: #{$!.message} (#{$!.class})", $@.drop(1).map{|s| "\t#{s}"} + rescue Exception + end + end + end + + entry = CallbackEntry.new + + FDB.cb_mutex.synchronize { + pos = FDB.ffi_callbacks.length + entry.index = pos + FDB.ffi_callbacks << entry + } + + entry.callback = FFI::Function.new(:void, [:pointer, :pointer]) do |ign1, ign2| + FDB.cb_mutex.synchronize { + FDB.ffi_callbacks[-1].index = entry.index + FDB.ffi_callbacks[entry.index] = FDB.ffi_callbacks[-1] + FDB.ffi_callbacks.pop + } + callback_wrapper(self, &block) + end + FDBC.fdb_future_set_callback(@fpointer, entry.callback, nil) + end + + def self.wait_for_any(*futures) + if futures.empty? + raise ArgumentError, "wait_for_any requires at least one future" + end + + mx = Mutex.new + cv = ConditionVariable.new + + ready_idx = -1 + + futures.each_with_index do |f, i| + f.on_ready do |f| + mx.synchronize { + if ready_idx < 0 + ready_idx = i + cv.signal + end + } + end + end + + mx.synchronize { + if ready_idx < 0 + cv.wait mx + end + } + + ready_idx + end + + private + + def release_memory + FDBC.fdb_future_release_memory(@fpointer) + end + end + + class FutureNil < Future + def wait + block_until_ready + FDBC.check_error FDBC.fdb_future_get_error(@fpointer) + end + end + + class LazyFuture < Future + instance_methods.each { |m| undef_method m unless (m =~ /^__/ or (LazyFuture.instance_methods - Object.instance_methods).include? m or m == :object_id)} + + def respond_to?(message) + message = message.to_sym + message == :__result__ or + message == :to_ptr or + value.respond_to? message + end + + def method_missing( *args, &block ) + value.__send__( *args, &block ) + end + + def initialize(fpointer) + super(fpointer) + @set = false + @value = nil + end + + def value + if !@set + block_until_ready + + begin + getter + release_memory + rescue Error => e + if e.code != 1102 # future_released + raise + end + end + + @set = true + end + + @value + end + end + + class LazyString < LazyFuture + def to_ptr + FFI::MemoryPointer.from_string(value) + end + end + + class Version < LazyFuture + def getter + version = FFI::MemoryPointer.new :int64 + FDBC.check_error FDBC.fdb_future_get_version(@fpointer, version) + @value = version.read_long_long + end + private :getter + end + + class FutureKeyValueArray < Future + def wait + block_until_ready + + kvs = FFI::MemoryPointer.new :pointer + count = FFI::MemoryPointer.new :int + more = FFI::MemoryPointer.new :int + FDBC.check_error FDBC.fdb_future_get_keyvalue_array(@fpointer, kvs, count, more) + kvs = kvs.read_pointer + + [(0..count.read_int-1).map{|i| + x = FDBC::KeyValueStruct.new(kvs + (i * FDBC::KeyValueStruct.size)) + KeyValue.new(x[:key].read_bytes(x[:key_length]), + x[:value].read_bytes(x[:value_length])) + }, count.read_int, more.read_int] + end + end + + class FutureStringArray < LazyFuture + def getter + strings = FFI::MemoryPointer.new :pointer + count = FFI::MemoryPointer.new :int + FDBC.check_error FDBC.fdb_future_get_string_array(@fpointer, strings, count) + + @value = strings.read_pointer.get_array_of_string(0, count.read_int).compact + end + end + + class FormerFuture + def ready? + true + end + + def block_until_ready + end + + def on_ready(&block) + begin + yield self + rescue Exception + begin + $stderr.puts "Discarding uncaught exception from user callback:" + $stderr.puts "#{$@.first}: #{$!.message} (#{$!.class})", $@.drop(1).map{|s| "\t#{s}"} + rescue Exception + end + end + end + end + + def self.create_cluster(cluster=nil) + f = FDBC.fdb_create_cluster(cluster) + cpointer = FFI::MemoryPointer.new :pointer + FDBC.check_error FDBC.fdb_future_block_until_ready(f) + FDBC.check_error FDBC.fdb_future_get_cluster(f, cpointer) + Cluster.new cpointer.get_pointer(0) + end + + class Cluster < FormerFuture + attr_reader :options + + def self.finalize(ptr) + proc do + # puts "Destroying cluster #{ptr}" + FDBC.fdb_cluster_destroy(ptr) + end + end + + def initialize(cpointer) + @cpointer = cpointer + @options = ClusterOptions.new lambda { |code, param| + FDBC.check_error FDBC.fdb_cluster_set_option(cpointer, code, param, param.nil? ? 0 : param.bytesize) + } + ObjectSpace.define_finalizer(self, self.class.finalize(@cpointer)) + end + + def open_database(name="DB") + f = FDBC.fdb_cluster_create_database(@cpointer, name, name.bytesize) + dpointer = FFI::MemoryPointer.new :pointer + FDBC.check_error FDBC.fdb_future_block_until_ready(f) + FDBC.check_error FDBC.fdb_future_get_database(f, dpointer) + Database.new dpointer.get_pointer(0) + end + end + + class Database < FormerFuture + attr_reader :options + + def self.finalize(ptr) + proc do + # puts "Destroying database #{ptr}" + FDBC.fdb_database_destroy(ptr) + end + end + + def initialize(dpointer) + @dpointer = dpointer + @options = DatabaseOptions.new lambda { |code, param| + FDBC.check_error FDBC.fdb_database_set_option(dpointer, code, param, param.nil? ? 0 : param.bytesize) + } + ObjectSpace.define_finalizer(self, self.class.finalize(@dpointer)) + end + + def create_transaction + tr = FFI::MemoryPointer.new :pointer + FDBC.check_error FDBC.fdb_database_create_transaction(@dpointer, tr) + Transaction.new(tr.read_pointer, self) + end + + def transact + tr = create_transaction + committed = false + begin + ret = yield tr + # puts ret + tr.commit.wait + committed = true + rescue Error => e + # puts "Rescued #{e}" + tr.on_error(e).wait + end until committed + ret + end + + def set(key, value) + transact do |tr| + tr[key] = value + end + end + alias []= set + + def get(key) + transact do |tr| + tr[key].value + end + end + alias [] get + + def get_range(bkeysel, ekeysel, options={}, &block) + transact do |tr| + a = tr.get_range(bkeysel, ekeysel, options).to_a + if block_given? + a.each &block + else + a + end + end + end + + def clear(key) + transact do |tr| + tr.clear(key) + end + end + + def clear_range(bkey, ekey) + transact do |tr| + tr.clear_range(bkey, ekey) + end + end + + def get_key(keysel) + transact do |tr| + tr.get_key(keysel).value + end + end + + def get_range_start_with(prefix, options={}, &block) + transact do |tr| + a = tr.get_range_start_with(prefix, options).to_a + if block_given? + a.each &block + else + a + end + end + end + + def clear_range_start_with(prefix) + transact do |tr| + tr.clear_range_start_with(prefix) + end + end + + def get_and_watch(key) + transact do |tr| + value = tr.get(key) + watch = tr.watch(key) + [value.value, watch] + end + end + + def set_and_watch(key, value) + transact do |tr| + tr.set(key, value) + tr.watch(key) + end + end + + def clear_and_watch(key) + transact do |tr| + tr.clear(key) + tr.watch(key) + end + end + + def watch(key) + transact do |tr| + tr.watch(key) + end + end + end + + class KeySelector + attr_reader :key, :or_equal, :offset + + def initialize(key, or_equal, offset) + @key = key + @or_equal = or_equal + @offset = offset + end + + def self.last_less_than(key) + self.new(key, 0, 0) + end + + def self.last_less_or_equal(key) + self.new(key, 1, 0) + end + + def self.first_greater_than(key) + self.new(key, 1, 1) + end + + def self.first_greater_or_equal(key) + self.new(key, 0, 1) + end + + def +(offset) + KeySelector.new(@key, @or_equal, @offset + offset) + end + + def -(offset) + KeySelector.new(@key, @or_equal, @offset - offset) + end + end + + class TransactionRead + attr_reader :db + attr_reader :tpointer + + def self.finalize(ptr) + proc do + #puts "Destroying transaction #{ptr}" + FDBC.fdb_transaction_destroy(ptr) + end + end + + def initialize(tpointer, db, is_snapshot) + @tpointer = tpointer + @db = db + @is_snapshot = is_snapshot + + ObjectSpace.define_finalizer(self, self.class.finalize(@tpointer)) + end + + def transact + yield self + end + + def get_read_version + Version.new(FDBC.fdb_transaction_get_read_version @tpointer) + end + + def get(key) + key = FDB.key_to_bytes(key) + Value.new(FDBC.fdb_transaction_get(@tpointer, key, key.bytesize, @is_snapshot)) + end + alias [] get + + def get_key(keysel) + key = FDB.key_to_bytes(keysel.key) + Key.new(FDBC.fdb_transaction_get_key(@tpointer, key, key.bytesize, keysel.or_equal, keysel.offset, @is_snapshot)) + end + + def to_selector(key_or_selector) + if key_or_selector.kind_of? KeySelector + key_or_selector + else + KeySelector.first_greater_or_equal key_or_selector + end + end + private :to_selector + + @@RangeEnum = Class.new do + include Enumerable + + def initialize(get_range, bsel, esel, limit, reverse, streaming_mode) + @get_range = get_range + + @bsel = bsel + @esel = esel + + @limit = limit + @reverse = reverse + @mode = streaming_mode + + @future = @get_range.call(@bsel, @esel, @limit, @mode, 1, @reverse) + end + + def to_a + o = self.dup + o.instance_eval do + if @mode == @@StreamingMode["ITERATOR"][0] + if @limit.zero? + @mode = @@StreamingMode["WANT_ALL"][0] + else + @mode = @@StreamingMode["EXACT"][0] + end + end + end + Enumerable.instance_method(:to_a).bind(o).call + end + + def each + bsel = @bsel + esel = @esel + limit = @limit + + iteration = 1 # the first read was fired off when the RangeEnum was initialized + future = @future + + done = false + + while !done + if future + kvs, count, more = future.wait + index = 0 + future = nil + + return if count.zero? + end + + result = kvs[index] + index += 1 + + if index == count + if more.zero? || limit == count + done = true + else + iteration += 1 + if limit.nonzero? + limit -= count + end + if @reverse.nonzero? + esel = KeySelector.first_greater_or_equal(kvs.last.key) + else + bsel = KeySelector.first_greater_than(kvs.last.key) + end + future = @get_range.call(bsel, esel, limit, @mode, iteration, @reverse) + end + end + + yield result + end + end + end + + def get_range(bkeysel, ekeysel, options={}, &block) + defaults = { :limit => 0, :reverse => false, :streaming_mode => :iterator } + options = defaults.merge options + bsel = to_selector bkeysel + esel = to_selector ekeysel + + if options[:streaming_mode].kind_of? Symbol + streaming_mode = @@StreamingMode[options[:streaming_mode].to_s.upcase] + raise ArgumentError, "#{options[:streaming_mode]} is not a valid streaming mode" if !streaming_mode + streaming_mode = streaming_mode[0] + else + streaming_mode = options[:streaming_mode] + end + + r = @@RangeEnum.new(lambda {|bsel, esel, limit, streaming_mode, iteration, reverse| + begin_key = FDB.key_to_bytes(bsel.key) + end_key = FDB.key_to_bytes(esel.key) + FDB::FutureKeyValueArray.new(FDBC.fdb_transaction_get_range(@tpointer, begin_key, begin_key.bytesize, bsel.or_equal, bsel.offset, end_key, end_key.bytesize, esel.or_equal, esel.offset, limit, 0, streaming_mode, iteration, @is_snapshot, reverse)) + }, bsel, esel, options[:limit], options[:reverse] ? 1 : 0, streaming_mode) + + if !block_given? + r + else + r.each &block + end + end + + def get_range_start_with(prefix, options={}, &block) + prefix = FDB.key_to_bytes(prefix) + prefix = prefix.dup.force_encoding "BINARY" + get_range(prefix, FDB.strinc(prefix), options, &block) + end + end + + TransactionRead.class_variable_set("@@StreamingMode", @@StreamingMode) + + class Transaction < TransactionRead + attr_reader :snapshot, :options + + def initialize(tpointer, db) + super(tpointer, db, 0) + @snapshot = TransactionRead.new(tpointer, db, 1) + + @options = TransactionOptions.new lambda { |code, param| + FDBC.check_error FDBC.fdb_transaction_set_option(@tpointer, code, param, param.nil? ? 0 : param.bytesize) + } + + ObjectSpace.undefine_finalizer self + end + + def set(key, value) + key = FDB.key_to_bytes(key) + value = FDB.value_to_bytes(value) + FDBC.fdb_transaction_set(@tpointer, key, key.bytesize, value, value.bytesize) + end + alias []= set + + def add_read_conflict_range(bkey, ekey) + bkey = FDB.key_to_bytes(bkey) + ekey = FDB.key_to_bytes(ekey) + FDBC.check_error FDBC.fdb_transaction_add_conflict_range(@tpointer, bkey, bkey.bytesize, ekey, ekey.bytesize, @@ConflictRangeType["READ"][0]) + end + + def add_read_conflict_key(key) + key = FDB.key_to_bytes(key) + add_read_conflict_range(key, key + "\x00") + end + + def add_write_conflict_range(bkey, ekey) + bkey = FDB.key_to_bytes(bkey) + ekey = FDB.key_to_bytes(ekey) + FDBC.check_error FDBC.fdb_transaction_add_conflict_range(@tpointer, bkey, bkey.bytesize, ekey, ekey.bytesize, @@ConflictRangeType["WRITE"][0]) + end + + def add_write_conflict_key(key) + key = FDB.key_to_bytes(key) + add_write_conflict_range(key, key + "\x00") + end + + def commit + FutureNil.new(FDBC.fdb_transaction_commit(@tpointer)) + end + + def watch(key) + key = FDB.key_to_bytes(key) + FutureNil.new(FDBC.fdb_transaction_watch(@tpointer, key, key.bytesize)) + end + + def on_error(e) + raise e if !e.kind_of? Error + FutureNil.new(FDBC.fdb_transaction_on_error(@tpointer, e.code)) + end + + def clear(key) + key = FDB.key_to_bytes(key) + FDBC.fdb_transaction_clear(@tpointer, key, key.bytesize) + end + + def clear_range(bkey, ekey) + bkey = FDB.key_to_bytes(bkey) + ekey = FDB.key_to_bytes(ekey) + FDBC.fdb_transaction_clear_range(@tpointer, bkey || "", bkey && bkey.bytesize || 0, ekey || "", ekey && ekey.bytesize || 0) + end + + def clear_range_start_with(prefix) + prefix = FDB.key_to_bytes(prefix) + prefix = prefix.dup.force_encoding "BINARY" + clear_range(prefix, FDB.strinc(prefix)) + end + + def set_read_version(version) + FDBC.fdb_transaction_set_read_version(@tpointer, version) + end + + def get_committed_version + version = FFI::MemoryPointer.new :int64 + FDBC.check_error FDBC.fdb_transaction_get_committed_version(@tpointer, version) + version.read_long_long + end + + def get_versionstamp + Key.new(FDBC.fdb_transaction_get_versionstamp(@tpointer)) + end + + def reset + FDBC.fdb_transaction_reset @tpointer + end + + def cancel + FDBC.fdb_transaction_cancel @tpointer + end + + def atomic_op(code, key, param) + key = FDB.key_to_bytes(key) + param = FDB.value_to_bytes(param) + FDBC.fdb_transaction_atomic_op(@tpointer, key, key.bytesize, param, param.bytesize, code) + end + private :atomic_op + end + + @@MutationType.each_pair do |k,v| + p = Proc.new do |key, param| atomic_op(v[0], key, param) end + Transaction.class_eval do + define_method("#{k.downcase}") do |key, param| + atomic_op(v[0], key, param) + end + end + Database.class_eval do + define_method("#{k.downcase}") do |key, param| + transact do |tr| + tr.send("#{k.downcase}", key, param) + end + end + end + end + + Transaction.class_variable_set("@@ConflictRangeType", @@ConflictRangeType) + + class Value < LazyString + def getter + present = FFI::MemoryPointer.new :int + value = FFI::MemoryPointer.new :pointer + length = FFI::MemoryPointer.new :int + FDBC.check_error FDBC.fdb_future_get_value(@fpointer, present, value, length) + if present.read_int > 0 + @value = value.read_pointer.read_bytes(length.read_int) + end + end + private :getter + end + + class Key < LazyString + def getter + key = FFI::MemoryPointer.new :pointer + key_length = FFI::MemoryPointer.new :int + FDBC.check_error FDBC.fdb_future_get_key(@fpointer, key, key_length) + if key_length.read_int.zero? + @value = '' + else + @value = key.read_pointer.read_bytes(key_length.read_int) + end + end + private :getter + end + + class KeyValue + attr_reader :key, :value + + def initialize(key, value) + @key = key + @value = value + end + end +end diff --git a/bindings/ruby/lib/fdblocality.rb b/bindings/ruby/lib/fdblocality.rb new file mode 100644 index 0000000000..a86459a6eb --- /dev/null +++ b/bindings/ruby/lib/fdblocality.rb @@ -0,0 +1,83 @@ +# +# fdblocality.rb +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#encoding: BINARY + +# FoundationDB Ruby API + +# Documentation for this API can be found at +# https://foundationdb.org/documentation/api-ruby.html + +module FDB + module Locality + def self.get_addresses_for_key(db_or_tr, key) + key = FDB.key_to_bytes(key) + db_or_tr.transact do |tr| + FutureStringArray.new(FDBC.fdb_transaction_get_addresses_for_key(tr.tpointer, key, key.bytesize)) + end + end + + def self.get_boundary_keys(db_or_tr, bkey, ekey) + bkey = FDB.key_to_bytes(bkey).dup.force_encoding('BINARY') + ekey = FDB.key_to_bytes(ekey).dup.force_encoding('BINARY') + + if db_or_tr.is_a? Transaction + tr = db_or_tr.db.create_transaction + tr.set_read_version db_or_tr.get_read_version + else + tr = db_or_tr.create_transaction + end + + tr.options.set_read_system_keys + tr.options.set_lock_aware + lastbkey = bkey + kvs = tr.snapshot.get_range("\xff/keyServers/"+bkey, "\xff/keyServers/"+ekey) + + y = Enumerator.new do |yielder| + _tr = tr + _bkey = bkey + _ekey = ekey + _lastbkey = lastbkey + _kvs = kvs + while _bkey < _ekey + begin + _kvs.each do |kv| + yielder.yield kv.key.byteslice(13..-1) + _bkey = kv.key.byteslice(13..-1) + "\x00" + end + _bkey = _ekey + rescue FDB::Error => e + if e.code == 1007 and _bkey != _lastbkey # if we get a past_version and *something* has happened, then we are no longer transactional + _tr = _tr.db.create_transaction + else + _tr.on_error(e).wait + end + # we either created a new transaction or (implicitly) reset the one we had... + _tr.options.set_read_system_keys + _lastbkey = _bkey + _kvs = _tr.snapshot.get_range("\xff/keyServers/" + _bkey, "\xff/keyServers/" + _ekey) + end + end + end + + return y + end + end +end diff --git a/bindings/ruby/lib/fdbsubspace.rb b/bindings/ruby/lib/fdbsubspace.rb new file mode 100644 index 0000000000..53efdcc934 --- /dev/null +++ b/bindings/ruby/lib/fdbsubspace.rb @@ -0,0 +1,71 @@ +# +# fdbsubspace.rb +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#encoding: BINARY + +# FoundationDB Ruby API + +# Documentation for this API can be found at +# https://foundationdb.org/documentation/api-ruby.html + +require_relative 'fdbtuple' + +module FDB + class Subspace + def initialize(prefix_tuple=[], raw_prefix='') + @raw_prefix = raw_prefix.dup.force_encoding('BINARY') + FDB::Tuple.pack(prefix_tuple) + end + attr_reader :raw_prefix + + def [](name) + Subspace.new([name], @raw_prefix) + end + + def key + @raw_prefix + end + + def pack(tuple) + @raw_prefix + FDB::Tuple.pack(tuple) + end + + def unpack(key) + raise ArgumentError, 'Cannot unpack key that is not in subspace.' if not contains? key + FDB::Tuple.unpack(key[@raw_prefix.length..-1]) + end + + def range(tuple=[]) + rng = FDB::Tuple.range(tuple) + [@raw_prefix + rng[0], @raw_prefix + rng[1]] + end + + def contains?(key) + key.start_with? @raw_prefix + end + + def as_foundationdb_key + key + end + + def subspace(tuple) + Subspace.new(tuple, @raw_prefix) + end + end +end diff --git a/bindings/ruby/lib/fdbtuple.rb b/bindings/ruby/lib/fdbtuple.rb new file mode 100644 index 0000000000..2349f367b3 --- /dev/null +++ b/bindings/ruby/lib/fdbtuple.rb @@ -0,0 +1,131 @@ +# +# fdbtuple.rb +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#encoding: BINARY + +# FoundationDB Ruby API + +# Documentation for this API can be found at +# https://foundationdb.org/documentation/api-ruby.html + +module FDB + module Tuple + @@size_limits = (0..8).map {|x| (1 << (x*8)) - 1} + + def self.find_terminator(v, pos) + while true + pos = v.index("\x00", pos) + if !pos + return v.length + elsif pos+1 == v.length || v[pos+1] != "\xff" + return pos + end + pos += 2 + end + end + private_class_method :find_terminator + + def self.decode(v, pos) + code = v.getbyte(pos) + if code == 0 + [nil, pos+1] + elsif code == 1 + epos = find_terminator(v, pos+1) + [v.slice(pos+1, epos-pos-1).gsub("\x00\xFF", "\x00"), epos+1] + elsif code == 2 + epos = find_terminator(v, pos+1) + [v.slice(pos+1, epos-pos-1).gsub("\x00\xFF", "\x00").force_encoding("UTF-8"), epos+1] + elsif code >= 20 && code <= 28 + n = code - 20 + [("\x00" * (8-n) + v.slice(pos+1, n)).unpack("Q>")[0], pos+n+1] + elsif code >= 12 and code < 20 + n = 20 - code + [("\x00" * (8-n) + v.slice(pos+1, n)).unpack("Q>")[0]-@@size_limits[n], pos+n+1] + else + raise "Unknown data type in DB: " + v + end + end + private_class_method :decode + + def self.bisect_left(list, item) + count = 0 + list.each{|i| + return count if i >= item + count += 1 + } + nil + end + private_class_method :bisect_left + + def self.encode(v) + if !v + "\x00" + elsif v.kind_of? String + if v.encoding == Encoding::BINARY || v.encoding == Encoding::ASCII + 1.chr + v.gsub("\x00", "\x00\xFF") + 0.chr + elsif v.encoding == Encoding::UTF_8 + 2.chr + v.dup.force_encoding("BINARY").gsub("\x00", "\x00\xFF") + 0.chr + else + raise ArgumentError, "unsupported encoding #{v.encoding.name}" + end + elsif v.kind_of? Integer + raise RangeError, "value outside inclusive range -2**64+1 to 2**64-1" if v < -2**64+1 || v > 2**64-1 + if v == 0 + 20.chr + elsif v > 0 + n = bisect_left( @@size_limits, v ) + (20+n).chr + [v].pack("Q>").slice(8-n, n) + else + n = bisect_left( @@size_limits, -v ) + (20-n).chr + [@@size_limits[n]+v].pack("Q>").slice(8-n, n) + end + else + raise ArgumentError, "unsupported type #{v.class}" + end + end + private_class_method :encode + + def self.pack(t) + (t.each_with_index.map {|el, i| + begin + (encode el).force_encoding("BINARY") + rescue + raise $!, "#{$!} at index #{i}", $!.backtrace + end + }).join + end + + def self.unpack(key) + key = key.dup.force_encoding("BINARY") + pos = 0 + res = [] + while pos < key.length + r, pos = decode(key, pos) + res << r + end + res + end + + def self.range(tuple=[]) + p = pack(tuple) + [p+"\x00", p+"\xFF"] + end + end +end diff --git a/bindings/ruby/tests/directory_extension.rb b/bindings/ruby/tests/directory_extension.rb new file mode 100644 index 0000000000..d7078b159a --- /dev/null +++ b/bindings/ruby/tests/directory_extension.rb @@ -0,0 +1,183 @@ +# +# directory_extension.rb +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#encoding:BINARY +require 'fdb' + +module DirectoryExtension + class DirectoryTester + @@ops_that_create_dirs = [ + 'DIRECTORY_CREATE_SUBSPACE', + 'DIRECTORY_CREATE_LAYER', + 'DIRECTORY_CREATE_OR_OPEN', + 'DIRECTORY_CREATE', + 'DIRECTORY_OPEN', + 'DIRECTORY_MOVE', + 'DIRECTORY_MOVE_TO', + 'DIRECTORY_OPEN_SUBSPACE', + ] + + def initialize + @dir_list = [FDB.directory] + @dir_index = 0 + @error_index = 0 + end + + def pop_tuples(inst, num=nil) + actual_num = num + if actual_num.nil? + actual_num = 1 + end + + tuples = (0...actual_num).map do + (0...inst.wait_and_pop).map do + inst.wait_and_pop + end + end + + if num.nil? + return tuples[0] + end + + return tuples + end + + def process_instruction(inst) + begin + #puts "#{inst.index} #{inst.op}" + directory = @dir_list[@dir_index] + if inst.op == 'DIRECTORY_CREATE_SUBSPACE' + path = pop_tuples(inst) + raw_prefix = inst.wait_and_pop + @dir_list << FDB::Subspace.new(path, raw_prefix) + elsif inst.op == 'DIRECTORY_CREATE_LAYER' + index1 = inst.wait_and_pop + index2 = inst.wait_and_pop + allow_manual_prefixes = inst.wait_and_pop + if @dir_list[index1].nil? or @dir_list[index2].nil? + @dir_list << nil + else + @dir_list << FDB::DirectoryLayer.new(:node_subspace => @dir_list[index1], + :content_subspace => @dir_list[index2], + :allow_manual_prefixes => allow_manual_prefixes == 1) + end + elsif inst.op == 'DIRECTORY_CHANGE' + @dir_index = inst.wait_and_pop + if @dir_list[@dir_index].nil? + @dir_index = @error_index + end + elsif inst.op == 'DIRECTORY_SET_ERROR_INDEX' + @error_index = inst.wait_and_pop + elsif inst.op == 'DIRECTORY_CREATE_OR_OPEN' + @dir_list << directory.create_or_open(inst.tr, pop_tuples(inst), :layer=>inst.wait_and_pop || '') + elsif inst.op == 'DIRECTORY_CREATE' + @dir_list << directory.create(inst.tr, pop_tuples(inst), :layer=>inst.wait_and_pop || '', :prefix=>inst.wait_and_pop) + elsif inst.op == 'DIRECTORY_OPEN' + @dir_list << directory.open(inst.tr, pop_tuples(inst), :layer=>inst.wait_and_pop || '') + elsif inst.op == 'DIRECTORY_MOVE' + @dir_list << directory.move(inst.tr, pop_tuples(inst), pop_tuples(inst)) + elsif inst.op == 'DIRECTORY_MOVE_TO' + @dir_list << directory.move_to(inst.tr, pop_tuples(inst)) + elsif inst.op == 'DIRECTORY_REMOVE' + count = inst.wait_and_pop + if count == 0 + directory.remove(inst.tr) + else + directory.remove(inst.tr, pop_tuples(inst)) + end + elsif inst.op == 'DIRECTORY_REMOVE_IF_EXISTS' + count = inst.wait_and_pop + if count == 0 + directory.remove_if_exists(inst.tr) + else + directory.remove_if_exists(inst.tr, pop_tuples(inst)) + end + elsif inst.op == 'DIRECTORY_LIST' + count = inst.wait_and_pop + results = + if count == 0 + directory.list(inst.tr) + else + directory.list(inst.tr, pop_tuples(inst)) + end + + inst.push(FDB::Tuple.pack(results)) + elsif inst.op == 'DIRECTORY_EXISTS' + count = inst.wait_and_pop + result = + if count == 0 + directory.exists?(inst.tr) + else + directory.exists?(inst.tr, pop_tuples(inst)) + end + + if result + inst.push(1) + else + inst.push(0) + end + elsif inst.op == 'DIRECTORY_PACK_KEY' + inst.push(directory.pack(pop_tuples(inst))) + elsif inst.op == 'DIRECTORY_UNPACK_KEY' + tup = directory.unpack(inst.wait_and_pop) + tup.each do |t| inst.push(t) end + elsif inst.op == 'DIRECTORY_RANGE' + rng = directory.range(pop_tuples(inst)) + inst.push(rng[0]) + inst.push(rng[1]) + elsif inst.op == 'DIRECTORY_CONTAINS' + if directory.contains?(inst.wait_and_pop) + inst.push(1) + else + inst.push(0) + end + elsif inst.op == 'DIRECTORY_OPEN_SUBSPACE' + @dir_list << directory.subspace(pop_tuples(inst)) + elsif inst.op == 'DIRECTORY_LOG_SUBSPACE' + inst.tr[inst.wait_and_pop + FDB::Tuple.pack([@dir_index])] = directory.key + elsif inst.op == 'DIRECTORY_LOG_DIRECTORY' + exists = directory.exists?(inst.tr) + children = exists ? directory.list(inst.tr) : [] + log_subspace = FDB::Subspace.new([@dir_index], inst.wait_and_pop) + inst.tr[log_subspace['path']] = FDB::Tuple.pack(directory.path) + inst.tr[log_subspace['layer']] = FDB::Tuple.pack([directory.layer]) + inst.tr[log_subspace['exists']] = FDB::Tuple.pack([exists ? 1 : 0]) + inst.tr[log_subspace['children']] = FDB::Tuple.pack(children) + elsif inst.op == 'DIRECTORY_STRIP_PREFIX' + str = inst.wait_and_pop + throw "String #{str} does not start with raw prefix #{directory.key}" if !str.start_with?(directory.key) + inst.push(str[directory.key.length..-1]) + else + raise "Unknown op #{inst.op}" + end + rescue Exception => e + #puts "#{e}" + #puts e.backtrace + #raise + if @@ops_that_create_dirs.include?(inst.op) + @dir_list << nil + end + + inst.push('DIRECTORY_ERROR') + end + end + end +end + diff --git a/bindings/ruby/tests/tester.rb b/bindings/ruby/tests/tester.rb new file mode 100755 index 0000000000..f67634158f --- /dev/null +++ b/bindings/ruby/tests/tester.rb @@ -0,0 +1,469 @@ +#!/usr/bin/env ruby +# +# tester.rb +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#encoding:BINARY + +require 'thread' + +$:.unshift( File.join( File.dirname(__FILE__), "../lib" ) ) + +require 'fdb' +FDB.api_version(ARGV[1].to_i) + +require_relative 'directory_extension' + +if ARGV.length == 2 + db = FDB.open +else + db = FDB.open( ARGV[2] ) +end + +class Stack + def initialize + @stack = [] + end + attr_reader :stack + + def length + @stack.length + end + + def [](idx) + @stack[idx] + end + + def []=(idx, val) + @stack[idx] = val + end + + def last + @stack.last + end + + def push(entry) + @stack.push(entry) + end + + def wait_and_pop(with_idx=false) + p = @stack.pop + + begin + # This works because the only futures we push currently are + # strings and nils. If we do other types, we will need to add + # them here or implement something more generic + if p[0].instance_of? String + p[0] = p[0].to_s + elsif p[0].instance_of? FDB::FutureNil + p[0].wait + p[0] = "RESULT_NOT_PRESENT" + end + + rescue FDB::Error => e + p[0] = FDB::Tuple.pack(["ERROR", e.code.to_s]) + end + + if with_idx + p + else + p[0] + end + end +end + +class Instruction + def initialize(tr, stack, op, index, is_database=false, is_snapshot=false) + @tr = tr + @stack = stack + @op = op + @index = index + @is_database = is_database + @is_snapshot = is_snapshot + end + + attr_reader :tr + attr_reader :stack + attr_reader :op + attr_reader :index + attr_reader :is_database + attr_reader :is_snapshot + + def push(val) + @stack.push([val, @index]) + end + + def wait_and_pop(with_idx=false) + @stack.wait_and_pop(with_idx) + end +end + +class Tester + class << self + attr_accessor :tr_map + attr_reader :tr_map_lock + end + + @tr_map = {} + @tr_map_lock = Monitor.new + + def initialize(db, prefix) + @db = db + @prefix = prefix + + @instructions = @db.get_range(*(FDB::Tuple.range([prefix]))) + + @stack = Stack.new + @tr_name = prefix + @last_version = nil + + @threads = [] + @directory_extension = DirectoryExtension::DirectoryTester.new + end + + def push_range(r, inst, prefix_filter=nil) + inst.push(FDB::Tuple.pack( + r.select do |kv| prefix_filter.nil? or kv.key.start_with? prefix_filter end + .flat_map do |kv| [kv.key, kv.value] end + )) + end + + def check_watches(watches, expected) + watches.each_with_index do |w,i| + if w.ready? or expected + begin + w.wait + raise "Watch #{i} unexpectedly triggered" if not expected + rescue FDB::Error => e + tr = @db.create_transaction + tr.on_error(e).wait + return false + end + end + end + + true + end + + def test_watches + while true do + @db['w0'] = '0' + @db['w3'] = '3' + + watches = Array.new(4) + watches[0] = @db.transact do |tr| + w = tr.watch('w0') + raise "Watch unexpectedly triggered" if w.ready? + tr['w0'] = '0' + w + end + + watches[1] = @db.clear_and_watch('w1') + watches[2] = @db.set_and_watch('w2', '2') + watches[3] = @db.get_and_watch('w3') + + raise "get_and_watch has wrong value" if watches[3][0] != '3' + watches[3] = watches[3][1] + + sleep 1 + + if not check_watches(watches, false) + next + end + + @db.clear 'w1' + + sleep 5 + + if not check_watches(watches, false) + next + end + + @db['w0'] = 'a' + @db['w1'] = 'b' + @db.clear 'w2' + @db.xor('w3', "\xff\xff") + + if check_watches(watches, true) + return + end + end + end + + def test_locality + @db.transact do |tr| + tr.options.set_timeout(60*1000) + tr.options.set_read_system_keys + boundary_keys = FDB::Locality.get_boundary_keys(tr, "", "\xFF\xFF").to_a + for i in 0..boundary_keys.length-2 + bkey = boundary_keys[i] + ekey = tr.get_key FDB::KeySelector.last_less_than(boundary_keys[i+1]) + bkey_addrs = FDB::Locality.get_addresses_for_key(tr, bkey) + ekey_addrs = FDB::Locality.get_addresses_for_key(tr, ekey) + if bkey_addrs.sort != ekey_addrs.sort + raise "Locality not internally consistent" + end + end + end + end + + def current_transaction() + Tester.tr_map_lock.synchronize { + Tester.tr_map[@tr_name] + } + end + + def new_transaction() + Tester.tr_map_lock.synchronize { + Tester.tr_map[@tr_name] = @db.create_transaction() + } + end + + def switch_transaction(name) + @tr_name = name + Tester.tr_map_lock.synchronize { + if !Tester.tr_map.has_key?(@tr_name) + new_transaction + end + } + end + + def log_stack(entries, prefix) + @db.transact do |tr| + for i, (el, idx) in entries + pk = prefix + FDB::Tuple.pack([i, idx]) + pv = FDB::Tuple.pack([el]) + + tr.set(pk, pv.byteslice(0, 40000)) + end + end + end + + def run + @instructions.each_with_index do |inst_str, index| + inst_tuple = FDB::Tuple.unpack inst_str.value + op = inst_tuple[0] + + # puts "#{@prefix} Instruction is #{inst_tuple}" + # puts "#{@prefix} Stack from #{@stack.stack.to_s}" + + is_database = op.end_with?("_DATABASE") + is_snapshot = op.end_with?("_SNAPSHOT") + + if is_database + op = op.slice( 0, op.length - 9 ) + obj = @db + elsif is_snapshot + op = op.slice( 0, op.length - 9 ) + obj = current_transaction.snapshot + else + obj = current_transaction + end + + inst = Instruction.new(obj, @stack, op, index, is_database, is_snapshot) + + begin + case op + when "PUSH" + inst.push(inst_tuple[1]) + when "DUP" + inst.stack.push inst.stack.last + when "EMPTY_STACK" + @stack = Stack.new + when "SWAP" + idx = inst.wait_and_pop + inst.stack[@stack.length-1], inst.stack[inst.stack.length-idx-1] = inst.stack[inst.stack.length-idx-1], inst.stack[inst.stack.length-1] + when "POP" + inst.wait_and_pop + when "SUB" + inst.push(inst.wait_and_pop - inst.wait_and_pop) + when "CONCAT" + inst.push(inst.wait_and_pop + inst.wait_and_pop) + when "WAIT_FUTURE" + inst.stack.push(inst.wait_and_pop(true)) + # @stack.push [wait_and_pop, index] + when "NEW_TRANSACTION" + new_transaction + when "USE_TRANSACTION" + switch_transaction(inst.wait_and_pop) + when "ON_ERROR" + inst.push(inst.tr.on_error( FDB::Error.new(inst.wait_and_pop) )) + when "GET" + res = obj[inst.wait_and_pop] + if res.nil? + inst.push("RESULT_NOT_PRESENT") + else + inst.push(res) + end + when "GET_KEY" + selector = FDB::KeySelector.new(inst.wait_and_pop, inst.wait_and_pop, inst.wait_and_pop) + prefix = inst.wait_and_pop + result = obj.get_key(selector) + if result.start_with? prefix + inst.push result + elsif result < prefix + inst.push prefix + else + inst.push FDB.strinc(prefix) + end + when "GET_RANGE" + push_range(obj.get_range( inst.wait_and_pop, inst.wait_and_pop, { :limit => inst.wait_and_pop, :reverse => !inst.wait_and_pop.zero?, :streaming_mode => inst.wait_and_pop } ), inst) + when "GET_RANGE_STARTS_WITH" + push_range(obj.get_range_start_with( inst.wait_and_pop, { :limit => inst.wait_and_pop, :reverse => !inst.wait_and_pop.zero?, :streaming_mode => inst.wait_and_pop } ), inst) + when "GET_RANGE_SELECTOR" + beginKey = FDB::KeySelector.new(inst.wait_and_pop, inst.wait_and_pop, inst.wait_and_pop) + endKey = FDB::KeySelector.new(inst.wait_and_pop, inst.wait_and_pop, inst.wait_and_pop) + params = { :limit => inst.wait_and_pop, :reverse => !inst.wait_and_pop.zero?, :streaming_mode => inst.wait_and_pop } + prefix = inst.wait_and_pop + push_range(obj.get_range(beginKey, endKey, params), inst, prefix) + when "GET_READ_VERSION" + @last_version = inst.tr.get_read_version.to_i + inst.push("GOT_READ_VERSION") + when "SET" + obj[inst.wait_and_pop] = inst.wait_and_pop + inst.push("RESULT_NOT_PRESENT") if obj == @db + when "ATOMIC_OP" + obj.send(inst.wait_and_pop.downcase, inst.wait_and_pop, inst.wait_and_pop) + inst.push("RESULT_NOT_PRESENT") if obj == @db + when "SET_READ_VERSION" + inst.tr.set_read_version @last_version + when "CLEAR" + obj.clear(inst.wait_and_pop) + inst.push("RESULT_NOT_PRESENT") if obj == @db + when "CLEAR_RANGE" + obj.clear_range( inst.wait_and_pop, inst.wait_and_pop ) + inst.push("RESULT_NOT_PRESENT") if obj == @db + when "CLEAR_RANGE_STARTS_WITH" + obj.clear_range_start_with( inst.wait_and_pop ) + inst.push("RESULT_NOT_PRESENT") if obj == @db + when "READ_CONFLICT_RANGE" + inst.tr.add_read_conflict_range( inst.wait_and_pop, inst.wait_and_pop ) + inst.push("SET_CONFLICT_RANGE") + when "WRITE_CONFLICT_RANGE" + inst.tr.add_write_conflict_range( inst.wait_and_pop, inst.wait_and_pop ) + inst.push("SET_CONFLICT_RANGE") + when "READ_CONFLICT_KEY" + inst.tr.add_read_conflict_key( inst.wait_and_pop ) + inst.push("SET_CONFLICT_KEY") + when "WRITE_CONFLICT_KEY" + inst.tr.add_write_conflict_key( inst.wait_and_pop ) + inst.push("SET_CONFLICT_KEY") + when "DISABLE_WRITE_CONFLICT" + inst.tr.options.set_next_write_no_write_conflict_range + when "COMMIT" + inst.push(inst.tr.commit) + when "RESET" + inst.tr.reset + when "CANCEL" + inst.tr.cancel + when "GET_COMMITTED_VERSION" + @last_version = inst.tr.get_committed_version + inst.push("GOT_COMMITTED_VERSION") + when "GET_VERSIONSTAMP" + inst.push(inst.tr.get_versionstamp) + when "TUPLE_PACK" + arr = [] + inst.wait_and_pop.times do |i| + arr.push(inst.wait_and_pop) + end + inst.push(FDB::Tuple.pack(arr)) + when "TUPLE_UNPACK" + FDB::Tuple.unpack( inst.wait_and_pop ).each do |i| inst.push(FDB::Tuple.pack([i])) end + when "TUPLE_RANGE" + arr = [] + inst.wait_and_pop.times do |i| + arr.push(inst.wait_and_pop) + end + (FDB::Tuple.range arr).each do |x| + inst.push(x) + end + when "START_THREAD" + t = Tester.new( @db, inst.wait_and_pop ) + thr = Thread.new do + t.run + end + @threads.push thr + when "WAIT_EMPTY" + prefix = inst.wait_and_pop + @db.transact do |tr| + raise FDB::Error.new(1020) if tr.get_range_start_with(prefix).any? + end + inst.push("WAITED_FOR_EMPTY") + when "UNIT_TESTS" + begin + @db.options.set_location_cache_size(100001) + + @db.transact do |tr| + tr.options.set_priority_system_immediate + tr.options.set_priority_batch + tr.options.set_causal_read_risky + tr.options.set_causal_write_risky + tr.options.set_read_your_writes_disable + tr.options.set_read_ahead_disable + tr.options.set_read_system_keys + tr.options.set_access_system_keys + tr.options.set_durability_dev_null_is_web_scale + tr.options.set_timeout(60*1000) + tr.options.set_retry_limit(50) + tr.options.set_max_retry_delay(100) + tr.options.set_used_during_commit_protection_disable + tr.options.set_transaction_logging_enable('my_transaction') + + tr.get("\xff").to_s + end + + test_watches + test_locality + rescue FDB::Error => e + raise "Unit tests failed: #{e.description.to_s}" + end + when "LOG_STACK" + prefix = inst.wait_and_pop + entries = {} + while inst.stack.length > 0 do + entries[inst.stack.length-1] = inst.wait_and_pop(true) + if entries.length == 100 + log_stack(entries, prefix) + entries = {} + end + end + + log_stack(entries, prefix) + else + if op.start_with?('DIRECTORY_') + @directory_extension.process_instruction(inst) + else + raise "Unknown op #{op}" + end + end + + rescue FDB::Error => e + inst.push(FDB::Tuple.pack(["ERROR", e.code.to_s])) + end + + # puts "#{@prefix} to #{@stack}" + # puts "" + end + + @threads.each do |thr| thr.join end + end +end + +t = Tester.new(db, ARGV[0].dup.force_encoding("BINARY")) +t.run diff --git a/build/Dockerfile b/build/Dockerfile new file mode 100644 index 0000000000..652aa6ac60 --- /dev/null +++ b/build/Dockerfile @@ -0,0 +1,59 @@ +FROM ubuntu:15.04 + +RUN sed -i -e 's/archive.ubuntu.com\|security.ubuntu.com/old-releases.ubuntu.com/g' /etc/apt/sources.list && apt-get clean && apt-get update + +RUN apt-get --no-install-recommends install -y libffi6:amd64=3.2.1-2 libgmp10:amd64=2:6.0.0+dfsg-6ubuntu1 libnettle4:amd64=2.7.1-5 libhogweed2:amd64=2.7.1-5 libp11-kit0:amd64=0.20.7-1 libtasn1-6:amd64=4.2-2ubuntu1 libgnutls-deb0-28:amd64=3.3.8-3ubuntu3 libsqlite3-0:amd64=3.8.7.4-1 libroken18-heimdal:amd64=1.6~rc2+dfsg-9 libasn1-8-heimdal:amd64=1.6~rc2+dfsg-9 libkrb5support0:amd64=1.12.1+dfsg-18 libk5crypto3:amd64=1.12.1+dfsg-18 libkeyutils1:amd64=1.5.9-5ubuntu1 libkrb5-3:amd64=1.12.1+dfsg-18 libgssapi-krb5-2:amd64=1.12.1+dfsg-18 libidn11:amd64=1.28-1ubuntu2 libhcrypto4-heimdal:amd64=1.6~rc2+dfsg-9 libheimbase1-heimdal:amd64=1.6~rc2+dfsg-9 libwind0-heimdal:amd64=1.6~rc2+dfsg-9 libhx509-5-heimdal:amd64=1.6~rc2+dfsg-9 libkrb5-26-heimdal:amd64=1.6~rc2+dfsg-9 libheimntlm0-heimdal:amd64=1.6~rc2+dfsg-9 libgssapi3-heimdal:amd64=1.6~rc2+dfsg-9 libsasl2-modules-db:amd64=2.1.26.dfsg1-13 libsasl2-2:amd64=2.1.26.dfsg1-13 libldap-2.4-2:amd64=2.4.31-1+nmu2ubuntu12 librtmp1:amd64=2.4+20131018.git79459a2-5 libcurl3-gnutls:amd64=7.38.0-3ubuntu2 apt-transport-https:amd64=1.0.9.7ubuntu4 bzip2=1.0.6-7 ca-certificates=20141019 && apt-get clean + +RUN echo "Acquire::CompressionTypes::Order \"bz2\";" > /etc/apt/apt.conf + +RUN apt-get clean && apt-get update + +RUN apt-get --no-install-recommends install -y --force-yes sudo=1.8.9p5-1ubuntu5 adduser=3.113+nmu3ubuntu3 apt=1.0.9.7ubuntu4 autoconf=2.69-8 automake=1:1.14.1-3ubuntu1 autotools-dev=20140911.1 base-files=7.2ubuntu9 base-passwd=3.5.37 bash=4.3-11ubuntu2 binutils=2.25-5ubuntu7 bison=2:3.0.2.dfsg-2 bsdutils=1:2.25.2-4ubuntu2 build-essential=11.6ubuntu6 ca-certificates-java=20140324 ccache=3.1.10-1 coreutils=8.23-3ubuntu1 cpp=4:4.9.2-2ubuntu2 cpp-4.9=4.9.2-10ubuntu13 dash=0.5.7-4ubuntu1 debconf=1.5.55ubuntu2 debianutils=4.4 debugedit=4.11.3-1.1 diffutils=1:3.3-1 dmsetup=2:1.02.90-2ubuntu1 dos2unix=6.0.4-1 dpkg=1.17.25ubuntu1 dpkg-dev=1.17.25ubuntu1 e2fslibs:amd64=1.42.12-1ubuntu2 e2fsprogs=1.42.12-1ubuntu2 fakeroot=1.20.2-1ubuntu1 file=1:5.20-1ubuntu2 findutils=4.4.2-9build1 flex=2.5.39-8 fontconfig=2.11.1-0ubuntu6 fontconfig-config=2.11.1-0ubuntu6 fonts-dejavu-core=2.34-1ubuntu1 g++=4:4.9.2-2ubuntu2 g++-4.9=4.9.2-10ubuntu13 gawk=1:4.1.1+dfsg-1 gcc=4:4.9.2-2ubuntu2 gcc-4.9=4.9.2-10ubuntu13 gcc-4.9-base:amd64=4.9.2-10ubuntu13 gcc-5-base:amd64=5.1~rc1-0ubuntu1 gdb=7.9-1ubuntu1 git=1:2.1.4-2.1 git-man=1:2.1.4-2.1 gnupg=1.4.18-7ubuntu1 golang=2:1.3.3-1ubuntu4 golang-doc=2:1.3.3-1ubuntu4 golang-go=2:1.3.3-1ubuntu4 golang-go-linux-amd64=2:1.3.3-1ubuntu4 golang-src=2:1.3.3-1ubuntu4 gperf=3.0.4-1 gpgv=1.4.18-7ubuntu1 grep=2.20-4.1 gyp=0.1~svn1729-3ubuntu1 gzip=1.6-4ubuntu1 hostname=3.15ubuntu2 init=1.22ubuntu11 init-system-helpers=1.22ubuntu11 initscripts=2.88dsf-53.2ubuntu12 insserv=1.14.0-5ubuntu3 java-common=0.52 libacl1:amd64=2.2.52-2 libapparmor1:amd64=2.9.1-0ubuntu9 libapt-pkg4.12:amd64=1.0.9.7ubuntu4 libasan1:amd64=4.9.2-10ubuntu13 libasn1-8-heimdal:amd64=1.6~rc2+dfsg-9 libasound2:amd64=1.0.28-1 libasound2-data=1.0.28-1 libasyncns0:amd64=0.8-5build1 libatk-wrapper-java=0.30.5-1 libatk-wrapper-java-jni:amd64=0.30.5-1 libatk1.0-0:amd64=2.14.0-1ubuntu1 libatk1.0-data=2.14.0-1ubuntu1 libatomic1:amd64=4.9.2-10ubuntu13 libattr1:amd64=1:2.4.47-2 libaudit-common=1:2.3.7-1ubuntu2 libaudit1:amd64=1:2.3.7-1ubuntu2 libavahi-client3:amd64=0.6.31-4ubuntu4 libavahi-common-data:amd64=0.6.31-4ubuntu4 libavahi-common3:amd64=0.6.31-4ubuntu4 libbison-dev:amd64=2:3.0.2.dfsg-2 libblkid1:amd64=2.25.2-4ubuntu2 libbsd-dev:amd64=0.7.0-2 libbsd0:amd64=0.7.0-2 libbz2-1.0:amd64=1.0.6-7 libc-ares-dev:amd64=1.10.0-2 libc-ares2:amd64=1.10.0-2 libc-bin=2.21-0ubuntu4 libc-dev-bin=2.21-0ubuntu4 libc6:amd64=2.21-0ubuntu4 libc6-dbg:amd64=2.21-0ubuntu4 libc6-dev:amd64=2.21-0ubuntu4 libcairo2:amd64=1.14.2-1ubuntu1 libcap2:amd64=1:2.24-6 libcap2-bin=1:2.24-6 libcilkrts5:amd64=4.9.2-10ubuntu13 libck-connector0:amd64=0.4.6-5 libcloog-isl4:amd64=0.18.2-3 libcomerr2:amd64=1.42.12-1ubuntu2 libcryptsetup4=2:1.6.1-1ubuntu7 libcups2:amd64=2.0.2-1ubuntu3 libcurl3-gnutls:amd64=7.38.0-3ubuntu2 libdatrie1:amd64=0.2.8-1 libdb5.3:amd64=5.3.28-9 libdbus-1-3:amd64=1.8.12-1ubuntu5 libdebconfclient0:amd64=0.192ubuntu1 libdevmapper1.02.1:amd64=2:1.02.90-2ubuntu1 libdpkg-perl=1.17.25ubuntu1 libdrm-intel1:amd64=2.4.60-2 libdrm-nouveau2:amd64=2.4.60-2 libdrm-radeon1:amd64=2.4.60-2 libdrm2:amd64=2.4.60-2 libedit-dev:amd64=3.1-20140620-2 libedit2:amd64=3.1-20140620-2 libelf1:amd64=0.160-0ubuntu3 liberror-perl=0.17-1.1 libexpat1:amd64=2.1.0-6ubuntu1 libfakeroot:amd64=1.20.2-1ubuntu1 libffi6:amd64=3.2.1-2 libfl-dev:amd64=2.5.39-8 libflac8:amd64=1.3.1-1ubuntu1 libfontconfig1:amd64=2.11.1-0ubuntu6 libfreetype6:amd64=2.5.2-2ubuntu3 libgcc-4.9-dev:amd64=4.9.2-10ubuntu13 libgcc1:amd64=1:5.1~rc1-0ubuntu1 libgcrypt20:amd64=1.6.2-4ubuntu2 libgdbm3:amd64=1.8.3-13.1 libgdk-pixbuf2.0-0:amd64=2.31.3-1 libgdk-pixbuf2.0-common=2.31.3-1 libgif4:amd64=4.1.6-11 libgl1-mesa-dri:amd64=10.5.2-0ubuntu1 libgl1-mesa-glx:amd64=10.5.2-0ubuntu1 libglapi-mesa:amd64=10.5.2-0ubuntu1 libglib2.0-0:amd64=2.44.0-1ubuntu3 libgmp10:amd64=2:6.0.0+dfsg-6ubuntu1 libgnutls-deb0-28:amd64=3.3.8-3ubuntu3 libgomp1:amd64=4.9.2-10ubuntu13 libgpg-error0:amd64=1.17-3ubuntu1 libgraphite2-3:amd64=1.2.4-3ubuntu1 libgssapi-krb5-2:amd64=1.12.1+dfsg-18 libgssapi3-heimdal:amd64=1.6~rc2+dfsg-9 libgtk2.0-0:amd64=2.24.27-0ubuntu1 libgtk2.0-common=2.24.27-0ubuntu1 libharfbuzz0b:amd64=0.9.37-1 libhcrypto4-heimdal:amd64=1.6~rc2+dfsg-9 libheimbase1-heimdal:amd64=1.6~rc2+dfsg-9 libheimntlm0-heimdal:amd64=1.6~rc2+dfsg-9 libhogweed2:amd64=2.7.1-5 libhx509-5-heimdal:amd64=1.6~rc2+dfsg-9 libicu52:amd64=52.1-8 libidn11:amd64=1.28-1ubuntu2 libintl-perl=1.23-1build2 libisl13:amd64=0.14-1 libitm1:amd64=4.9.2-10ubuntu13 libjasper1:amd64=1.900.1-debian1-2.4 libjbig0:amd64=2.1-3.1 libjpeg-turbo8:amd64=1.3.0-0ubuntu2 libjpeg8:amd64=8c-2ubuntu8 libjs-excanvas=0.r3-4 libjs-jquery=1.7.2+dfsg-3ubuntu2 libjs-node-uuid=1.4.0-1 libjs-underscore=1.7.0~dfsg-1ubuntu1 libjson-c2:amd64=0.11-4ubuntu2 libk5crypto3:amd64=1.12.1+dfsg-18 libkeyutils1:amd64=1.5.9-5ubuntu1 libkmod2:amd64=18-3ubuntu1 libkrb5-26-heimdal:amd64=1.6~rc2+dfsg-9 libkrb5-3:amd64=1.12.1+dfsg-18 libkrb5support0:amd64=1.12.1+dfsg-18 liblcms2-2:amd64=2.6-3ubuntu2 libldap-2.4-2:amd64=2.4.31-1+nmu2ubuntu12.3 libllvm3.6:amd64=1:3.6-2ubuntu1 liblsan0:amd64=4.9.2-10ubuntu13 liblua5.2-0:amd64=5.2.3-1.1 liblzma5:amd64=5.1.1alpha+20120614-2ubuntu2 libmagic1:amd64=1:5.20-1ubuntu2 libmono-corlib4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-corlib4.5-cil=3.2.8+dfsg-4ubuntu4 libmono-csharp4.0c-cil=3.2.8+dfsg-4ubuntu4 libmono-data-tds4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-microsoft-csharp4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-posix4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-security4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-configuration4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-core4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-data-datasetextensions4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-data-linq4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-data4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-enterpriseservices4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-runtime-serialization4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-security4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-transactions4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-xml-linq4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system-xml4.0-cil=3.2.8+dfsg-4ubuntu4 libmono-system4.0-cil=3.2.8+dfsg-4ubuntu4 libmount1:amd64=2.25.2-4ubuntu2 libmpc3:amd64=1.0.3-1 libmpfr4:amd64=3.1.2-3 libncurses5:amd64=5.9+20140712-2ubuntu2 libncurses5-dev:amd64=5.9+20140712-2ubuntu2 libncursesw5:amd64=5.9+20140712-2ubuntu2 libnettle4:amd64=2.7.1-5 libnspr4:amd64=2:4.10.7-1ubuntu1 libnss3:amd64=2:3.17.4-0ubuntu1 libnss3-nssdb=2:3.17.4-0ubuntu1 libogg0:amd64=1.3.2-1 libp11-kit0:amd64=0.20.7-1 libpam-modules:amd64=1.1.8-3.1ubuntu3 libpam-modules-bin=1.1.8-3.1ubuntu3 libpam-runtime=1.1.8-3.1ubuntu3 libpam0g:amd64=1.1.8-3.1ubuntu3 libpango-1.0-0:amd64=1.36.8-3 libpangocairo-1.0-0:amd64=1.36.8-3 libpangoft2-1.0-0:amd64=1.36.8-3 libpciaccess0:amd64=0.13.2-3build1 libpcre3:amd64=2:8.35-3.3ubuntu1 libpcre3-dev:amd64=2:8.35-3.3ubuntu1 libpcrecpp0:amd64=2:8.35-3.3ubuntu1 libpcsclite1:amd64=1.8.11-3ubuntu1 libpixman-1-0:amd64=0.32.6-3 libpng12-0:amd64=1.2.51-0ubuntu3 libpopt0:amd64=1.16-10 libprocps3:amd64=1:3.3.9-1ubuntu8 libpulse0:amd64=1:6.0-0ubuntu6 libpython-stdlib:amd64=2.7.9-1 libpython2.7-minimal:amd64=2.7.9-2ubuntu3 libpython2.7-stdlib:amd64=2.7.9-2ubuntu3 libquadmath0:amd64=4.9.2-10ubuntu13 libreadline6:amd64=6.3-8ubuntu1 libroken18-heimdal:amd64=1.6~rc2+dfsg-9 librpm3=4.11.3-1.1 librpmbuild3=4.11.3-1.1 librpmio3=4.11.3-1.1 librpmsign1=4.11.3-1.1 librtmp1:amd64=2.4+20131018.git79459a2-5 libruby2.1:amd64=2.1.2-2ubuntu3 libsasl2-2:amd64=2.1.26.dfsg1-13ubuntu0.1 libsasl2-modules-db:amd64=2.1.26.dfsg1-13ubuntu0.1 libselinux1:amd64=2.3-2 libsemanage-common=2.3-1build1 libsemanage1:amd64=2.3-1build1 libsepol1:amd64=2.3-2 libsigsegv2:amd64=2.10-4 libslang2:amd64=2.3.0-2ubuntu1 libsmartcols1:amd64=2.25.2-4ubuntu2 libsndfile1:amd64=1.0.25-9.1 libsqlite3-0:amd64=3.8.7.4-1 libss2:amd64=1.42.12-1ubuntu2 libssl-dev:amd64=1.0.1f-1ubuntu11 libssl1.0.0:amd64=1.0.1f-1ubuntu11 libstdc++-4.9-dev:amd64=4.9.2-10ubuntu13 libstdc++-4.9-pic:amd64=4.9.2-10ubuntu13 libstdc++6:amd64=4.9.2-10ubuntu13 libsystemd0:amd64=219-7ubuntu3 libtasn1-6:amd64=4.2-2ubuntu1 libtext-unidecode-perl=1.22-1 libthai-data=0.1.21-1 libthai0:amd64=0.1.21-1 libtiff5:amd64=4.0.3-12.3ubuntu2 libtimedate-perl=2.3000-2 libtinfo-dev:amd64=5.9+20140712-2ubuntu2 libtinfo5:amd64=5.9+20140712-2ubuntu2 libtool=2.4.2-1.11 libtool-bin=2.4.2-1.11 libtsan0:amd64=4.9.2-10ubuntu13 libubsan0:amd64=4.9.2-10ubuntu13 libudev1:amd64=219-7ubuntu3 libusb-0.1-4:amd64=2:0.1.12-25 libustr-1.0-1:amd64=1.0.4-3ubuntu2 libuuid1:amd64=2.25.2-4ubuntu2 libv8-3.14-dev=3.14.5.8-5ubuntu2 libv8-3.14.5=3.14.5.8-5ubuntu2 libvorbis0a:amd64=1.3.4-2 libvorbisenc2:amd64=1.3.4-2 libwind0-heimdal:amd64=1.6~rc2+dfsg-9 libwrap0:amd64=7.6.q-25 libx11-6:amd64=2:1.6.2-2ubuntu2 libx11-data=2:1.6.2-2ubuntu2 libx11-xcb1:amd64=2:1.6.2-2ubuntu2 libxau6:amd64=1:1.0.8-1 libxcb-dri2-0:amd64=1.10-2ubuntu1 libxcb-dri3-0:amd64=1.10-2ubuntu1 libxcb-glx0:amd64=1.10-2ubuntu1 libxcb-present0:amd64=1.10-2ubuntu1 libxcb-render0:amd64=1.10-2ubuntu1 libxcb-shm0:amd64=1.10-2ubuntu1 libxcb-sync1:amd64=1.10-2ubuntu1 libxcb1:amd64=1.10-2ubuntu1 libxcomposite1:amd64=1:0.4.4-1 libxcursor1:amd64=1:1.1.14-1 libxdamage1:amd64=1:1.1.4-2 libxdmcp6:amd64=1:1.1.1-1build1 libxext6:amd64=2:1.3.3-1 libxfixes3:amd64=1:5.0.1-2 libxi6:amd64=2:1.7.4-1 libxinerama1:amd64=2:1.1.3-1 libxml-libxml-perl=2.0116+dfsg-1ubuntu0.15.04.1 libxml-namespacesupport-perl=1.11-1 libxml-sax-base-perl=1.07-1 libxml-sax-perl=0.99+dfsg-2ubuntu1 libxml2:amd64=2.9.2+dfsg1-3 libxrandr2:amd64=2:1.4.2-1 libxrender1:amd64=1:0.9.8-1build1 libxshmfence1:amd64=1.1-4 libxtst6:amd64=2:1.2.2-1 libxxf86vm1:amd64=1:1.1.3-1 libyaml-0-2:amd64=0.1.6-3 linux-libc-dev:amd64=3.19.0-15.15 locales=2.13+git20120306-19 login=1:4.1.5.1-1.1ubuntu4 lsb-base=4.1+Debian11ubuntu8 lsof=4.86+dfsg-1ubuntu2 m4=1.4.17-4 make=4.0-8.1 makedev=2.3.1-93ubuntu1 mawk=1.3.3-17ubuntu2 mercurial=3.1.2-2 mercurial-common=3.1.2-2 mime-support=3.58ubuntu1 mono-4.0-gac=3.2.8+dfsg-4ubuntu4 mono-dmcs=3.2.8+dfsg-4ubuntu4 mono-gac=3.2.8+dfsg-4ubuntu4 mono-mcs=3.2.8+dfsg-4ubuntu4 mono-runtime=3.2.8+dfsg-4ubuntu4 mono-runtime-common=3.2.8+dfsg-4ubuntu4 mono-runtime-sgen=3.2.8+dfsg-4ubuntu4 mount=2.25.2-4ubuntu2 multiarch-support=2.21-0ubuntu4 ncurses-base=5.9+20140712-2ubuntu2 ncurses-bin=5.9+20140712-2ubuntu2 node-abbrev=1.0.5-2 node-ansi=0.3.0-2 node-ansi-color-table=1.0.0-1 node-archy=0.0.2-1 node-async=0.8.0-1 node-block-stream=0.0.7-1 node-combined-stream=0.0.5-1 node-cookie-jar=0.3.1-1 node-delayed-stream=0.0.5-1 node-forever-agent=0.5.1-1 node-form-data=0.1.0-1 node-fstream=0.1.24-1 node-fstream-ignore=0.0.6-2 node-github-url-from-git=1.1.1-1 node-glob=4.0.5-1 node-graceful-fs=3.0.2-1 node-gyp=0.12.2+ds-1 node-inherits=2.0.1-1 node-ini=1.1.0-1 node-json-stringify-safe=5.0.0-1 node-lockfile=0.4.1-1 node-lru-cache=2.3.1-1 node-mime=1.2.11-1 node-minimatch=1.0.0-1 node-mkdirp=0.5.0-1 node-mute-stream=0.0.4-1 node-node-uuid=1.4.0-1 node-nopt=3.0.1-1 node-normalize-package-data=0.2.2-1 node-npmlog=0.0.4-1 node-once=1.1.1-1 node-osenv=0.1.0-1 node-qs=2.2.4-1 node-read=1.0.5-1 node-read-package-json=1.2.4-1 node-request=2.26.1-1 node-retry=0.6.0-1 node-rimraf=2.2.8-1 node-semver=2.1.0-2 node-sha=1.2.3-1 node-sigmund=1.0.0-1 node-slide=1.1.4-1 node-tar=0.1.18-1 node-tunnel-agent=0.3.1-1 node-underscore=1.7.0~dfsg-1ubuntu1 node-which=1.0.5-2 nodejs=0.10.25~dfsg2-2ubuntu1 nodejs-dev=0.10.25~dfsg2-2ubuntu1 npm=1.4.21+ds-2 openjdk-8-jdk:amd64=8u45-b14-1 openjdk-8-jre:amd64=8u45-b14-1 openjdk-8-jre-headless:amd64=8u45-b14-1 openssh-client=1:6.7p1-5ubuntu1 openssh-server=1:6.7p1-5ubuntu1 openssh-sftp-server=1:6.7p1-5ubuntu1 openssl=1.0.1f-1ubuntu11 passwd=1:4.1.5.1-1.1ubuntu4 patch=2.7.5-1 perl=5.20.2-2 perl-base=5.20.2-2 perl-modules=5.20.2-2 php-pear=5.6.4+dfsg-4ubuntu6 php5-cli=5.6.4+dfsg-4ubuntu6 php5-common=5.6.4+dfsg-4ubuntu6 php5-dev=5.6.4+dfsg-4ubuntu6 php5-json=1.3.6-1build1 procps=1:3.3.9-1ubuntu8 psmisc=22.21-2build1 python=2.7.9-1 python-chardet=2.3.0-1 python-colorama=0.3.2-1 python-distlib=0.1.9-1 python-gevent=1.0.1-2 python-greenlet=0.4.5-1ubuntu1 python-html5lib=0.999-3 python-minimal=2.7.9-1 python-pip=1.5.6-5ubuntu2 python-pkg-resources=12.2-1 python-requests=2.4.3-6 python-setuptools=12.2-1 python-six=1.9.0-1 python-urllib3=1.9.1-3 python-yaml=3.11-2 python2.7=2.7.9-2ubuntu3 python2.7-minimal=2.7.9-2ubuntu3 readline-common=6.3-8ubuntu1 rpm=4.11.3-1.1 rpm-common=4.11.3-1.1 rpm2cpio=4.11.3-1.1 rsync=3.1.1-3 ruby=1:2.1.0.4ubuntu1 ruby2.1=2.1.2-2ubuntu3 rubygems-integration=1.8 sed=4.2.2-4ubuntu1 sensible-utils=0.0.9 shared-mime-info=1.3-1 shtool=2.0.8-6 ssh=1:6.7p1-5ubuntu1 systemd=219-7ubuntu3 systemd-sysv=219-7ubuntu3 sysv-rc=2.88dsf-53.2ubuntu12 sysvinit-utils=2.88dsf-53.2ubuntu12 tar=1.27.1-2 texinfo=5.2.0.dfsg.1-6 tzdata=2015c-1 tzdata-java=2015c-1 ubuntu-keyring=2012.05.19 ucf=3.0030 udev=219-7ubuntu3 unzip=6.0-13ubuntu3 util-linux=2.25.2-4ubuntu2 valgrind=1:3.10.1-1ubuntu2 wget=1.16.1-1ubuntu1 x11-common=1:7.7+7ubuntu4 xz-utils=5.1.1alpha+20120614-2ubuntu2 zlib1g:amd64=1:1.2.8.dfsg-2ubuntu1 zlib1g-dev:amd64=1:1.2.8.dfsg-2ubuntu1 golang-go.tools=0.0~hg20140703-4 curl=7.38.0-3ubuntu2.3 sphinx-common=1.2.3+dfsg-1ubuntu2 distcc=3.1-6.1 clang=1:3.6-26ubuntu1 && apt-get clean + +RUN adduser --disabled-password --gecos '' fdb + +RUN chown -R fdb /opt + +RUN chmod -R 0777 /opt + +USER fdb + +RUN cd /opt/ && wget http://downloads.sourceforge.net/project/boost/boost/1.52.0/boost_1_52_0.tar.bz2 -qO - | tar -xj + +RUN mkdir -p /home/fdb/.ssh /opt/ct /opt/cross-build /opt/x-toolchain + +RUN cd /opt/ && wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.21.0.tar.bz2 -qO - | tar -xj + +RUN cd /opt/crosstool-ng-1.21.0 && ./configure --prefix=/opt/ct && make && make install + +RUN mkdir -p /opt/cross-build/samples/x86_64-nptl-linux-gnu/ + +ADD ct.config.fdb /opt/cross-build/samples/x86_64-nptl-linux-gnu/crosstool.config + +RUN touch /opt/cross-build/samples/x86_64-nptl-linux-gnu/reported.by + +RUN cd /opt/cross-build && /opt/ct/bin/ct-ng x86_64-nptl-linux-gnu + +RUN mkdir -p /opt/cross-build/.tarballs + +RUN ((cd /opt/cross-build && /opt/ct/bin/ct-ng build && rm -rf /opt/cross-build /opt/crosstool-ng-1.21.0 /opt/ct) || (cat /opt/cross-build/build.log && false)) + +RUN cd /opt/ && wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz -qO - | tar -xz && cd /opt/ncurses-5.9 && ./configure CC=/opt/x-toolchain/bin/x86_64-nptl-linux-gnu-gcc CFLAGS=-fPIC --prefix=/opt/libncurses && make && make install && cd /opt && rm -rf ncurses-5.9 + +RUN cd /opt/ && wget http://thrysoee.dk/editline/libedit-20150325-3.1.tar.gz -qO - | tar -xz && cd /opt/libedit-20150325-3.1 && ./configure CC=/opt/x-toolchain/bin/x86_64-nptl-linux-gnu-cc CFLAGS=-fPIC LDFLAGS="-L/opt/libncurses/lib" --prefix=/opt/libedit && make && make install && cd /opt && rm -rf libedit-20150325-3.1 + +RUN ln -s /opt/libncurses/lib/libncurses.a /opt/libncurses/lib/libtinfo.a + +USER root + +RUN ln -s /opt/x-toolchain/lib/gcc/x86_64-nptl-linux-gnu/4.9.1/../../../../x86_64-nptl-linux-gnu/lib/../lib64/libstdc++.a /opt/x-toolchain/lib/gcc/x86_64-nptl-linux-gnu/4.9.1/../../../../x86_64-nptl-linux-gnu/lib/../lib64/libstdc++_pic.a + +RUN pip install boto3==1.1.1 + +RUN npm install -g npm@3.4.1 + +RUN ln -s /usr/bin/nodejs /usr/bin/node + +RUN LANGUAGE=en_US.UTF-8 LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 locale-gen en_US.UTF-8 + +RUN dpkg-reconfigure locales diff --git a/build/csproj.mk b/build/csproj.mk new file mode 100644 index 0000000000..8550ae362a --- /dev/null +++ b/build/csproj.mk @@ -0,0 +1,42 @@ +# +# csproj.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile; -*- + +TARGETS += GENNAME +CLEAN_TARGETS += GENNAME()_clean + +GENNAME()_REFERENCES=-r:GENREFERENCES +GENNAME()_SOURCES=$(addprefix GENDIR/,GENSOURCES) + +-include GENDIR/local.mk + +.PHONY: GENNAME()_clean GENNAME + +GENNAME: GENTARGET + +GENNAME()_clean: + @echo "Cleaning GENNAME" + @rm -f GENTARGET + +GENTARGET: $(GENNAME()_SOURCES) $(ALL_MAKEFILES) + @echo "Building $@" + @mkdir -p $(@D) + @$(MCS) $(GENNAME()_REFERENCES) $(GENNAME()_LOCAL_REFERENCES) $(GENNAME()_SOURCES) -target:GENOUTPUTTYPE -sdk:4 -out:$@ diff --git a/build/csprojtom4.py b/build/csprojtom4.py new file mode 100644 index 0000000000..80a4ae3c60 --- /dev/null +++ b/build/csprojtom4.py @@ -0,0 +1,59 @@ +#!/usr/bin/python +# +# csprojtom4.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +import sys + +if len(sys.argv) != 2: + print """Usage: + %s [input]""" % sys.argv[0] + sys.exit() + +csproj = sys.argv[1] + +from xml.dom.minidom import parse + +try: + dom = parse(csproj) +except: + print "ERROR: Unable to open CSProj file %s" % csproj + sys.exit() + +outputType = dom.getElementsByTagName("OutputType")[0].childNodes[0].data +assemblyName = dom.getElementsByTagName("AssemblyName")[0].childNodes[0].data + +if outputType == "Exe": + print "define(`GENTARGET', `bin/%s.exe')dnl" % assemblyName + print "define(`GENOUTPUTTYPE', `exe')dnl" +elif outputType == "Library": + print "define(`GENTARGET', `bin/%s.dll')dnl" % assemblyName + print "define(`GENOUTPUTTYPE', `library')dnl" +else: + print "ERROR: Unable to determine output type" + sys.exit() + +sources = [ node.getAttribute("Include").replace('\\', '/') for node in + dom.getElementsByTagName("Compile") ] +assemblies = [ node.getAttribute("Include") for node in + dom.getElementsByTagName("Reference") ] + +print "define(`GENSOURCES', `%s')dnl" % ' '.join(sources) +print "define(`GENREFERENCES', `%s')dnl" % ','.join(assemblies) diff --git a/build/ct.config.fdb b/build/ct.config.fdb new file mode 100644 index 0000000000..1db2002394 --- /dev/null +++ b/build/ct.config.fdb @@ -0,0 +1,532 @@ +# +# Automatically generated make config: don't edit +# crosstool-NG 1.20.0 Configuration +# Thu Apr 30 12:28:38 2015 +# +CT_CONFIGURE_has_xz=y +CT_MODULES=y + +# +# Paths and misc options +# + +# +# crosstool-NG behavior +# +# CT_OBSOLETE is not set +# CT_EXPERIMENTAL is not set +# CT_DEBUG_CT is not set + +# +# Paths +# +CT_LOCAL_TARBALLS_DIR="${CT_TOP_DIR}/.tarballs" +CT_SAVE_TARBALLS=y +CT_WORK_DIR="${CT_TOP_DIR}/.build" +CT_PREFIX_DIR="/opt/x-toolchain/" +CT_INSTALL_DIR="${CT_PREFIX_DIR}" +CT_RM_RF_PREFIX_DIR=y +CT_REMOVE_DOCS=y +CT_INSTALL_DIR_RO=y +CT_STRIP_ALL_TOOLCHAIN_EXECUTABLES=y + +# +# Downloading +# +# CT_FORBID_DOWNLOAD is not set +# CT_FORCE_DOWNLOAD is not set +CT_CONNECT_TIMEOUT=10 +# CT_ONLY_DOWNLOAD is not set +# CT_USE_MIRROR is not set + +# +# Extracting +# +# CT_FORCE_EXTRACT is not set +CT_OVERIDE_CONFIG_GUESS_SUB=y +# CT_ONLY_EXTRACT is not set +CT_PATCH_BUNDLED=y +# CT_PATCH_LOCAL is not set +# CT_PATCH_BUNDLED_LOCAL is not set +# CT_PATCH_LOCAL_BUNDLED is not set +# CT_PATCH_BUNDLED_FALLBACK_LOCAL is not set +# CT_PATCH_LOCAL_FALLBACK_BUNDLED is not set +# CT_PATCH_NONE is not set +CT_PATCH_ORDER="bundled" + +# +# Build behavior +# +CT_PARALLEL_JOBS=0 +CT_LOAD="" +CT_USE_PIPES=y +CT_EXTRA_CFLAGS_FOR_BUILD="" +CT_EXTRA_LDFLAGS_FOR_BUILD="" +CT_EXTRA_CFLAGS_FOR_HOST="" +CT_EXTRA_LDFLAGS_FOR_HOST="" +# CT_CONFIG_SHELL_SH is not set +# CT_CONFIG_SHELL_ASH is not set +CT_CONFIG_SHELL_BASH=y +# CT_CONFIG_SHELL_CUSTOM is not set +CT_CONFIG_SHELL="${bash}" + +# +# Logging +# +# CT_LOG_ERROR is not set +# CT_LOG_WARN is not set +# CT_LOG_INFO is not set +CT_LOG_EXTRA=y +# CT_LOG_ALL is not set +# CT_LOG_DEBUG is not set +CT_LOG_LEVEL_MAX="EXTRA" +# CT_LOG_SEE_TOOLS_WARN is not set +CT_LOG_PROGRESS_BAR=y +CT_LOG_TO_FILE=y +CT_LOG_FILE_COMPRESS=y + +# +# Target options +# +CT_ARCH="x86" +CT_ARCH_SUPPORTS_32=y +CT_ARCH_SUPPORTS_64=y +CT_ARCH_SUPPORTS_WITH_ARCH=y +CT_ARCH_SUPPORTS_WITH_CPU=y +CT_ARCH_SUPPORTS_WITH_TUNE=y +CT_ARCH_DEFAULT_32=y +CT_ARCH_ARCH="x86-64" +CT_ARCH_CPU="" +CT_ARCH_TUNE="k8" +# CT_ARCH_32 is not set +CT_ARCH_64=y +CT_ARCH_BITNESS=64 +CT_TARGET_CFLAGS="" +CT_TARGET_LDFLAGS="" +# CT_ARCH_alpha is not set +# CT_ARCH_arm is not set +# CT_ARCH_avr32 is not set +# CT_ARCH_blackfin is not set +# CT_ARCH_m68k is not set +# CT_ARCH_mips is not set +# CT_ARCH_powerpc is not set +# CT_ARCH_s390 is not set +# CT_ARCH_sh is not set +# CT_ARCH_sparc is not set +CT_ARCH_x86=y +CT_ARCH_alpha_AVAILABLE=y +CT_ARCH_arm_AVAILABLE=y +CT_ARCH_avr32_AVAILABLE=y +CT_ARCH_blackfin_AVAILABLE=y +CT_ARCH_m68k_AVAILABLE=y +CT_ARCH_microblaze_AVAILABLE=y +CT_ARCH_mips_AVAILABLE=y +CT_ARCH_powerpc_AVAILABLE=y +CT_ARCH_s390_AVAILABLE=y +CT_ARCH_sh_AVAILABLE=y +CT_ARCH_sparc_AVAILABLE=y +CT_ARCH_x86_AVAILABLE=y +CT_ARCH_SUFFIX="" + +# +# Generic target options +# +# CT_MULTILIB is not set +CT_ARCH_USE_MMU=y + +# +# Target optimisations +# +CT_ARCH_FLOAT="" + +# +# Toolchain options +# + +# +# General toolchain options +# +CT_FORCE_SYSROOT=y +CT_USE_SYSROOT=y +CT_SYSROOT_NAME="sysroot" +CT_SYSROOT_DIR_PREFIX="" +CT_WANTS_STATIC_LINK=y +# CT_STATIC_TOOLCHAIN is not set +CT_TOOLCHAIN_PKGVERSION="" +CT_TOOLCHAIN_BUGURL="" + +# +# Tuple completion and aliasing +# +CT_TARGET_VENDOR="nptl" +CT_TARGET_ALIAS_SED_EXPR="" +CT_TARGET_ALIAS="" + +# +# Toolchain type +# +CT_CROSS=y +# CT_CANADIAN is not set +CT_TOOLCHAIN_TYPE="cross" + +# +# Build system +# +CT_BUILD="" +CT_BUILD_PREFIX="" +CT_BUILD_SUFFIX="" + +# +# Misc options +# +# CT_TOOLCHAIN_ENABLE_NLS is not set + +# +# Operating System +# +CT_KERNEL_SUPPORTS_SHARED_LIBS=y +CT_KERNEL="linux" +CT_KERNEL_VERSION="3.8.13" +# CT_KERNEL_bare_metal is not set +CT_KERNEL_linux=y +# CT_KERNEL_windows is not set +CT_KERNEL_bare_metal_AVAILABLE=y +CT_KERNEL_linux_AVAILABLE=y +# CT_KERNEL_V_3_15 is not set +# CT_KERNEL_V_3_14 is not set +# CT_KERNEL_V_3_13 is not set +# CT_KERNEL_V_3_12 is not set +# CT_KERNEL_V_3_11 is not set +# CT_KERNEL_V_3_10 is not set +# CT_KERNEL_V_3_9 is not set +CT_KERNEL_V_3_8=y +# CT_KERNEL_V_3_7 is not set +# CT_KERNEL_V_3_6 is not set +# CT_KERNEL_V_3_5 is not set +# CT_KERNEL_V_3_4 is not set +# CT_KERNEL_V_3_3 is not set +# CT_KERNEL_V_3_2 is not set +# CT_KERNEL_V_3_1 is not set +# CT_KERNEL_V_3_0 is not set +# CT_KERNEL_V_2_6_39 is not set +# CT_KERNEL_V_2_6_38 is not set +# CT_KERNEL_V_2_6_37 is not set +# CT_KERNEL_V_2_6_36 is not set +# CT_KERNEL_V_2_6_33 is not set +# CT_KERNEL_V_2_6_32 is not set +# CT_KERNEL_V_2_6_31 is not set +# CT_KERNEL_V_2_6_27 is not set +# CT_KERNEL_LINUX_CUSTOM is not set +CT_KERNEL_windows_AVAILABLE=y + +# +# Common kernel options +# +CT_SHARED_LIBS=y + +# +# linux other options +# +CT_KERNEL_LINUX_VERBOSITY_0=y +# CT_KERNEL_LINUX_VERBOSITY_1 is not set +# CT_KERNEL_LINUX_VERBOSITY_2 is not set +CT_KERNEL_LINUX_VERBOSE_LEVEL=0 +CT_KERNEL_LINUX_INSTALL_CHECK=y + +# +# Binary utilities +# +CT_ARCH_BINFMT_ELF=y +CT_BINUTILS="binutils" +CT_BINUTILS_binutils=y + +# +# GNU binutils +# +# CT_BINUTILS_V_2_22 is not set +CT_BINUTILS_V_2_21_53=y +# CT_BINUTILS_V_2_21_1a is not set +# CT_BINUTILS_V_2_20_1a is not set +# CT_BINUTILS_V_2_19_1a is not set +# CT_BINUTILS_V_2_18a is not set +CT_BINUTILS_VERSION="2.21.53" +CT_BINUTILS_2_21_or_later=y +CT_BINUTILS_2_20_or_later=y +CT_BINUTILS_2_19_or_later=y +CT_BINUTILS_2_18_or_later=y +CT_BINUTILS_HAS_HASH_STYLE=y +CT_BINUTILS_HAS_GOLD=y +CT_BINUTILS_GOLD_SUPPORTS_ARCH=y +CT_BINUTILS_HAS_PLUGINS=y +CT_BINUTILS_HAS_PKGVERSION_BUGURL=y +CT_BINUTILS_FORCE_LD_BFD=y +CT_BINUTILS_LINKER_LD=y +# CT_BINUTILS_LINKER_LD_GOLD is not set +# CT_BINUTILS_LINKER_GOLD_LD is not set +CT_BINUTILS_LINKERS_LIST="ld" +CT_BINUTILS_LINKER_DEFAULT="bfd" +# CT_BINUTILS_PLUGINS is not set +CT_BINUTILS_EXTRA_CONFIG_ARRAY="" +# CT_BINUTILS_FOR_TARGET is not set + +# +# binutils other options +# + +# +# C-library +# +CT_LIBC="glibc" +CT_LIBC_VERSION="2.11" +# CT_LIBC_eglibc is not set +CT_LIBC_glibc=y +# CT_LIBC_musl is not set +# CT_LIBC_uClibc is not set +CT_LIBC_eglibc_AVAILABLE=y +CT_THREADS="nptl" +CT_LIBC_glibc_AVAILABLE=y +# CT_LIBC_GLIBC_V_2_19 is not set +# CT_LIBC_GLIBC_V_2_18 is not set +# CT_LIBC_GLIBC_V_2_17 is not set +# CT_LIBC_GLIBC_V_2_16_0 is not set +# CT_LIBC_GLIBC_V_2_15 is not set +# CT_LIBC_GLIBC_V_2_14_1 is not set +# CT_LIBC_GLIBC_V_2_14 is not set +# CT_LIBC_GLIBC_V_2_13 is not set +# CT_LIBC_GLIBC_V_2_12_2 is not set +# CT_LIBC_GLIBC_V_2_12_1 is not set +# CT_LIBC_GLIBC_V_2_11_1 is not set +CT_LIBC_GLIBC_V_2_11=y +# CT_LIBC_GLIBC_V_2_10_1 is not set +# CT_LIBC_GLIBC_V_2_9 is not set +# CT_LIBC_GLIBC_V_2_8 is not set +CT_LIBC_mingw_AVAILABLE=y +CT_LIBC_musl_AVAILABLE=y +CT_LIBC_newlib_AVAILABLE=y +CT_LIBC_none_AVAILABLE=y +CT_LIBC_uClibc_AVAILABLE=y +CT_LIBC_SUPPORT_THREADS_ANY=y +CT_LIBC_SUPPORT_THREADS_NATIVE=y + +# +# Common C library options +# +CT_THREADS_NATIVE=y +CT_LIBC_XLDD=y +CT_LIBC_GLIBC_PORTS_EXTERNAL=y +CT_LIBC_glibc_familly=y +CT_LIBC_GLIBC_EXTRA_CONFIG_ARRAY="" +CT_LIBC_GLIBC_CONFIGPARMS="" +CT_LIBC_GLIBC_EXTRA_CFLAGS="" +CT_LIBC_EXTRA_CC_ARGS="" +# CT_LIBC_DISABLE_VERSIONING is not set +CT_LIBC_OLDEST_ABI="" +CT_LIBC_GLIBC_FORCE_UNWIND=y +# CT_LIBC_GLIBC_USE_PORTS is not set +CT_LIBC_ADDONS_LIST="" +# CT_LIBC_LOCALES is not set +# CT_LIBC_GLIBC_KERNEL_VERSION_NONE is not set +CT_LIBC_GLIBC_KERNEL_VERSION_AS_HEADERS=y +# CT_LIBC_GLIBC_KERNEL_VERSION_CHOSEN is not set +CT_LIBC_GLIBC_MIN_KERNEL="3.8.13" + +# +# glibc other options +# + +# +# C compiler +# +CT_CC="gcc" +CT_CC_VERSION="4.9.1" +CT_CC_CORE_PASSES_NEEDED=y +CT_CC_CORE_PASS_1_NEEDED=y +CT_CC_CORE_PASS_2_NEEDED=y +CT_CC_gcc=y +# CT_CC_GCC_SHOW_LINARO is not set +CT_CC_V_4_9_1=y +# CT_CC_V_4_9_0 is not set +# CT_CC_V_4_8_3 is not set +# CT_CC_V_4_8_2 is not set +# CT_CC_V_4_8_1 is not set +# CT_CC_V_4_8_0 is not set +# CT_CC_V_4_7_4 is not set +# CT_CC_V_4_7_3 is not set +# CT_CC_V_4_7_2 is not set +# CT_CC_V_4_7_1 is not set +# CT_CC_V_4_7_0 is not set +# CT_CC_V_4_6_4 is not set +# CT_CC_V_4_6_3 is not set +# CT_CC_V_4_6_2 is not set +# CT_CC_V_4_6_1 is not set +# CT_CC_V_4_6_0 is not set +# CT_CC_V_4_5_3 is not set +# CT_CC_V_4_5_2 is not set +# CT_CC_V_4_5_1 is not set +# CT_CC_V_4_5_0 is not set +# CT_CC_V_4_4_7 is not set +# CT_CC_V_4_4_6 is not set +# CT_CC_V_4_4_5 is not set +# CT_CC_V_4_4_4 is not set +# CT_CC_V_4_4_3 is not set +# CT_CC_V_4_4_2 is not set +# CT_CC_V_4_4_1 is not set +# CT_CC_V_4_4_0 is not set +# CT_CC_V_4_3_6 is not set +# CT_CC_V_4_3_5 is not set +# CT_CC_V_4_3_4 is not set +# CT_CC_V_4_3_3 is not set +# CT_CC_V_4_3_2 is not set +# CT_CC_V_4_3_1 is not set +# CT_CC_V_4_2_4 is not set +# CT_CC_V_4_2_2 is not set +CT_CC_GCC_4_2_or_later=y +CT_CC_GCC_4_3_or_later=y +CT_CC_GCC_4_4_or_later=y +CT_CC_GCC_4_5_or_later=y +CT_CC_GCC_4_6_or_later=y +CT_CC_GCC_4_7_or_later=y +CT_CC_GCC_4_8_or_later=y +CT_CC_GCC_4_9=y +CT_CC_GCC_4_9_or_later=y +CT_CC_GCC_HAS_GRAPHITE=y +CT_CC_GCC_USE_GRAPHITE=y +CT_CC_GCC_HAS_LTO=y +CT_CC_GCC_USE_LTO=y +CT_CC_GCC_HAS_PKGVERSION_BUGURL=y +CT_CC_GCC_HAS_BUILD_ID=y +CT_CC_GCC_HAS_LNK_HASH_STYLE=y +CT_CC_GCC_USE_GMP_MPFR=y +CT_CC_GCC_USE_MPC=y +CT_CC_GCC_HAS_LIBQUADMATH=y +CT_CC_GCC_HAS_LIBSANITIZER=y +# CT_CC_LANG_FORTRAN is not set +CT_CC_SUPPORT_CXX=y +CT_CC_SUPPORT_FORTRAN=y +CT_CC_SUPPORT_JAVA=y +CT_CC_SUPPORT_ADA=y +CT_CC_SUPPORT_OBJC=y +CT_CC_SUPPORT_OBJCXX=y +CT_CC_SUPPORT_GOLANG=y + +# +# Additional supported languages: +# +CT_CC_LANG_CXX=y +# CT_CC_LANG_JAVA is not set + +# +# gcc other options +# +CT_CC_ENABLE_CXX_FLAGS="" +CT_CC_CORE_EXTRA_CONFIG_ARRAY="--with-pic" +CT_CC_EXTRA_CONFIG_ARRAY="--with-pic" +CT_CC_STATIC_LIBSTDCXX=y +# CT_CC_GCC_SYSTEM_ZLIB is not set + +# +# Optimisation features +# + +# +# Settings for libraries running on target +# +CT_CC_GCC_ENABLE_TARGET_OPTSPACE=y +# CT_CC_GCC_LIBMUDFLAP is not set +# CT_CC_GCC_LIBGOMP is not set +# CT_CC_GCC_LIBSSP is not set +# CT_CC_GCC_LIBQUADMATH is not set +# CT_CC_GCC_LIBSANITIZER is not set + +# +# Misc. obscure options. +# +CT_CC_CXA_ATEXIT=y +# CT_CC_GCC_DISABLE_PCH is not set +CT_CC_GCC_SJLJ_EXCEPTIONS=m +CT_CC_GCC_LDBL_128=m +# CT_CC_GCC_BUILD_ID is not set +CT_CC_GCC_LNK_HASH_STYLE_DEFAULT=y +# CT_CC_GCC_LNK_HASH_STYLE_SYSV is not set +# CT_CC_GCC_LNK_HASH_STYLE_GNU is not set +# CT_CC_GCC_LNK_HASH_STYLE_BOTH is not set +CT_CC_GCC_LNK_HASH_STYLE="" +CT_CC_GCC_DEC_FLOAT_AUTO=y +# CT_CC_GCC_DEC_FLOAT_BID is not set +# CT_CC_GCC_DEC_FLOAT_DPD is not set +# CT_CC_GCC_DEC_FLOATS_NO is not set + +# +# Debug facilities +# +# CT_DEBUG_dmalloc is not set +# CT_DEBUG_duma is not set +# CT_DEBUG_gdb is not set +# CT_DEBUG_ltrace is not set +# CT_DEBUG_strace is not set + +# +# Companion libraries +# +CT_COMPLIBS_NEEDED=y +CT_GMP_NEEDED=y +CT_MPFR_NEEDED=y +CT_ISL_NEEDED=y +CT_CLOOG_NEEDED=y +CT_MPC_NEEDED=y +CT_COMPLIBS=y +CT_GMP=y +CT_MPFR=y +CT_ISL=y +CT_CLOOG=y +CT_MPC=y +CT_GMP_V_5_1_3=y +# CT_GMP_V_5_1_1 is not set +# CT_GMP_V_5_0_2 is not set +# CT_GMP_V_5_0_1 is not set +# CT_GMP_V_4_3_2 is not set +# CT_GMP_V_4_3_1 is not set +# CT_GMP_V_4_3_0 is not set +CT_GMP_VERSION="5.1.3" +CT_MPFR_V_3_1_2=y +# CT_MPFR_V_3_1_0 is not set +# CT_MPFR_V_3_0_1 is not set +# CT_MPFR_V_3_0_0 is not set +# CT_MPFR_V_2_4_2 is not set +# CT_MPFR_V_2_4_1 is not set +# CT_MPFR_V_2_4_0 is not set +CT_MPFR_VERSION="3.1.2" +CT_ISL_V_0_12_2=y +# CT_ISL_V_0_11_1 is not set +CT_ISL_VERSION="0.12.2" +CT_CLOOG_V_0_18_1=y +# CT_CLOOG_V_0_18_0 is not set +CT_CLOOG_VERSION="0.18.1" +CT_CLOOG_0_18_or_later=y +CT_MPC_V_1_0_2=y +# CT_MPC_V_1_0_1 is not set +# CT_MPC_V_1_0 is not set +# CT_MPC_V_0_9 is not set +# CT_MPC_V_0_8_2 is not set +# CT_MPC_V_0_8_1 is not set +# CT_MPC_V_0_7 is not set +CT_MPC_VERSION="1.0.2" + +# +# Companion libraries common options +# +# CT_COMPLIBS_CHECK is not set + +# +# Companion tools +# + +# +# READ HELP before you say 'Y' below !!! +# +CT_COMP_TOOLS=y +CT_COMP_TOOLS_make=y +# CT_COMP_TOOLS_m4 is not set +# CT_COMP_TOOLS_autoconf is not set +# CT_COMP_TOOLS_automake is not set +# CT_COMP_TOOLS_libtool is not set diff --git a/build/link-validate.sh b/build/link-validate.sh new file mode 100755 index 0000000000..54b22192da --- /dev/null +++ b/build/link-validate.sh @@ -0,0 +1,39 @@ +#/bin/sh +# +# This script is used to validate the shared libraries + +verlte() { + [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ] +} + +ALLOWED_SHARED_LIBS=("libdl.so.2" "libpthread.so.0" "librt.so.1" "libm.so.6" "libc.so.6" "ld-linux-x86-64.so.2" "libfdb_c.so") + +if [ "$#" -ne 2 ]; then + echo "USAGE: link-validate.sh BINNAME GLIBC_VERSION" + exit 1 +fi + +# Step 1: glibc version + +for i in $(objdump -T "$1" | awk '{print $5}' | grep GLIBC | sed 's/ *$//g' | sed 's/GLIBC_//' | sort | uniq); do + if ! verlte "$i" "$2"; then + echo "!!! WARNING: DEPENDENCY ON NEWER LIBC DETECTED !!!" + exit 1 + fi +done + +# Step 2: Other dynamic dependencies + +for j in $(objdump -p "$1" | grep NEEDED | awk '{print $2}'); do + PRESENT=0 + for k in ${ALLOWED_SHARED_LIBS[@]}; do + if [[ "$k" == "$j" ]]; then + PRESENT=1 + break + fi + done + if ! [[ $PRESENT == 1 ]]; then + echo "!!! WARNING: UNKNOWN SHARED OBJECT DEPENDENCY DETECTED: $j !!!" + exit 1 + fi +done diff --git a/build/link-wrapper.sh b/build/link-wrapper.sh new file mode 100755 index 0000000000..c34aac9967 --- /dev/null +++ b/build/link-wrapper.sh @@ -0,0 +1,89 @@ +#!/bin/bash + +set -e + +case $1 in + Application | DynamicLibrary) + echo "Linking $3" + + if [ "$1" = "DynamicLibrary" ]; then + OPTIONS="-shared" + if [ "$PLATFORM" = "linux" ]; then + OPTIONS="$OPTIONS -Wl,-z,noexecstack -Wl,-soname,$( basename $3 )" + fi + if [ "$PLATFORM" = "osx" ]; then + OPTIONS="$OPTIONS -Wl,-dylib_install_name -Wl,$( basename $3 )" + fi + else + OPTIONS= + fi + + OPTIONS=$( eval echo "$OPTIONS $LDFLAGS \$$2_LDFLAGS \$$2_OBJECTS \$$2_LIBS \$$2_STATIC_LIBS_REAL -o $3" ) + + if echo $OPTIONS | grep -q -- -static-libstdc\+\+ ; then + OPTIONS=$( echo $OPTIONS | sed -e s,-static-libstdc\+\+,, -e s,\$,\ `$CC -print-file-name=libstdc++.a`\ -lm, ) + fi + + case $PLATFORM in + osx) + if echo $OPTIONS | grep -q -- -static-libgcc ; then + $( $CC -### $OPTIONS 2>&1 | grep '^ ' | sed -e s,^\ ,, -e s,-lgcc[^\ ]*,,g -e s,\",,g -e s,\$,\ `$CC -print-file-name=libgcc_eh.a`, -e s,10.8.2,10.6, ) + else + $CC $OPTIONS + fi + ;; + *) + $CC $OPTIONS + ;; + esac + + if [ -z "$UNSTRIPPED" ]; then + if [ -z "${NOSTRIP}" ]; then echo "Stripping $3"; else echo "Not stripping $3"; fi + + case $1 in + Application) + case $PLATFORM in + linux) + objcopy --only-keep-debug $3 $3.debug + if [ -z "${NOSTRIP}" ]; then strip --strip-debug --strip-unneeded $3; fi + objcopy --add-gnu-debuglink=$3.debug $3 + ./build/link-validate.sh $3 $4 + ;; + osx) + cp $3 $3.debug + if [ -z "${NOSTRIP}" ]; then strip $3; fi + ;; + *) + echo "I don't know how to strip a binary on $PLATFORM" + exit 1 + ;; + esac + ;; + DynamicLibrary) + cp $3 $3-debug + case $PLATFORM in + linux) + if [ -z "${NOSTRIP}" ]; then strip --strip-all $3; fi + ;; + osx) + if [ -z "${NOSTRIP}" ]; then strip -S -x $3; fi + ;; + *) + echo "I don't know how to strip a library on $PLATFORM" + exit 1 + ;; + esac + ;; + esac + fi + ;; + StaticLibrary) + echo "Archiving $3" + rm -f $3 + eval ar rcs $3 \$$2_OBJECTS + ;; + *) + echo "I don't know how to build a $1" + exit 1 + ;; +esac diff --git a/build/packages.mk b/build/packages.mk new file mode 100644 index 0000000000..00459f0196 --- /dev/null +++ b/build/packages.mk @@ -0,0 +1,148 @@ +# +# packages.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +TARGETS += packages +CLEAN_TARGETS += packages_clean + +PACKAGE_BINARIES = fdbcli fdbserver fdbbackup fdbmonitor fdbrestore fdbblob fdbdr dr_agent backup_agent +PACKAGE_CONTENTS := $(addprefix bin/, $(PACKAGE_BINARIES)) $(addprefix bin/, $(addsuffix .debug, $(PACKAGE_BINARIES))) lib/libfdb_c.$(DLEXT) bindings/python/fdb/fdboptions.py bindings/c/foundationdb/fdb_c_options.g.h + +packages: TGZ FDBSERVERAPI + +TGZ: $(PACKAGE_CONTENTS) versions.target + @echo "Archiving tgz" + @mkdir -p packages + @rm -f packages/FoundationDB-$(PLATFORM)-*.tar.gz + @bash -c "tar -czf packages/FoundationDB-$(PLATFORM)-$(VERSION)-$(PKGRELEASE).tar.gz bin/{fdbmonitor{,.debug},fdbcli{,.debug},fdbserver{,.debug},fdbbackup{,.debug},fdbblob{,.debug},fdbdr{,.debug},fdbrestore{,.debug},dr_agent{,.debug},coverage.{fdbclient,fdbserver,fdbrpc,flow}.xml} lib/libfdb_c.$(DLEXT){,-debug} lib/libfdb_java.$(DLEXT)* bindings/python/fdb/*.py bindings/c/*.h" + +packages_clean: + @echo "Cleaning packages" + @rm -f packages/FoundationDB-$(PLATFORM)-*.tar.gz packages/fdb-tests-$(VERSION).tar.gz packages/fdb-headers-$(VERSION).tar.gz packages/fdb-bindings-$(VERSION).tar.gz packages/fdb-server-$(VERSION)-$(PLATFORM).tar.gz + +packages/fdb-server-$(VERSION)-$(PLATFORM).tar.gz: bin/fdbserver bin/fdbcli lib/libfdb_c.$(DLEXT) + @echo "Packaging fdb server api" + @rm -rf packages/fdbserverapi + @mkdir -p packages/fdbserverapi/bin packages/fdbserverapi/lib + @cp bin/fdbserver bin/fdbcli packages/fdbserverapi/bin/ + @cp lib/libfdb_c.$(DLEXT) packages/fdbserverapi/lib/ + @tar czf packages/fdb-server-$(VERSION)-$(PLATFORM).tar.gz -C packages/fdbserverapi/ . + @rm -rf packages/fdbserverapi + +FDBSERVERAPI: packages/fdb-server-$(VERSION)-$(PLATFORM).tar.gz + +FDBSERVERAPI_clean: + @echo "Cleaning fdb server api" + @rm -rf packages/fdb-server-$(VERSION)-$(PLATFORM).tar.gz packages/fdbserverapi + +ifeq ($(PLATFORM),linux) + DEB: packages/foundationdb-clients_$(VERSION)-$(PKGRELEASE)_amd64.deb packages/foundationdb-server_$(VERSION)-$(PKGRELEASE)_amd64.deb + + DEB_clean: + @echo "Cleaning deb" + @rm -f packages/foundationdb-server_*.deb packages/foundationdb-clients_*.deb + + DEB_FILES := $(addprefix packaging/deb/,builddebs.sh foundationdb-clients.control.in foundationdb-init foundationdb-server.control.in DEBIAN-foundationdb-clients/postinst $(addprefix DEBIAN-foundationdb-server/,conffiles postinst postrm preinst prerm)) + + packages/foundationdb-server_%.deb packages/foundationdb-clients_%.deb: $(PACKAGE_CONTENTS) versions.target $(DEB_FILES) + @echo "Packaging deb" + @mkdir -p packages + @rm -f packages/foundationdb-server_*.deb packages/foundationdb-clients_*.deb + @mkdir -p packaging/deb/DEBIAN-foundationdb-server packaging/deb/DEBIAN-foundationdb-clients + @for i in server clients; do \ + m4 -DVERSION=$(VERSION) -DRELEASE=$(PKGRELEASE) packaging/deb/foundationdb-$$i.control.in > packaging/deb/DEBIAN-foundationdb-$$i/control; \ + done + @packaging/deb/builddebs.sh + @rm packaging/deb/DEBIAN-*/control + + RPM: packages/foundationdb-server-$(VERSION)-$(PKGRELEASE).el6.x86_64.rpm packages/foundationdb-clients-$(VERSION)-$(PKGRELEASE).el6.x86_64.rpm packages/foundationdb-server-$(VERSION)-$(PKGRELEASE).el7.x86_64.rpm packages/foundationdb-clients-$(VERSION)-$(PKGRELEASE).el7.x86_64.rpm + + RPM_clean: + @echo "Cleaning rpm" + @rm -f packages/foundationdb-server-*.rpm packages/foundationdb-clients-*.rpm + + RPM_FILES := $(addprefix packaging/rpm/,buildrpms.sh foundationdb-init foundationdb.service foundationdb.spec.in) + + JAVA_RELEASE: fdb_java_release + + JAVA_RELEASE_clean: fdb_java_release_clean + + FDBTESTS: + @echo "Archiving fdbtests" + @mkdir -p packages + @rm -f packages/fdb-tests-$(VERSION).tar.gz + @bash -c "tar -czf packages/fdb-tests-$(VERSION).tar.gz -C tests ." + + FDBTESTS_clean: + @echo "Cleaning fdbtests" + @rm -f packages/fdb-tests-$(VERSION).tgz + + FDBBINDINGS: bindings + @echo "Archiving fdbbindings" + @mkdir -p packages + @rm -f packages/fdb-bindings-$(VERSION).tar.gz + @bash -c "tar -czf packages/fdb-bindings-$(VERSION).tar.gz -C bindings ." + + FDBBINDINGS_clean: + @echo "Cleaning fdbbindings" + @rm -f packages/fdb-bindings-$(VERSION).tgz + + FDBHEADERS: bindings/python/fdb/fdboptions.py bindings/c/foundationdb/fdb_c_options.g.h fdbclient/vexillographer/fdb.options + @echo "Archiving fdbheaders" + @mkdir -p packages + @rm -f packages/fdb-headers-$(VERSION).tar.gz + @bash -c "tar -czf packages/fdb-headers-$(VERSION).tar.gz -C $(shell pwd)/bindings/c/foundationdb fdb_c.h -C $(shell pwd)/bindings/c/foundationdb fdb_c_options.g.h -C $(shell pwd)/fdbclient/vexillographer fdb.options" + + FDBHEADERS_clean: + @echo "Cleaning fdbheaders" + @rm -f packages/fdb-headers-$(VERSION).tgz + + packages/foundationdb-server-%.el6.x86_64.rpm packages/foundationdb-clients-%.el6.x86_64.rpm packages/foundationdb-server-%.el7.x86_64.rpm packages/foundationdb-clients-%.el7.x86_64.rpm: $(PACKAGE_CONTENTS) versions.target $(RPM_FILES) + + packages/foundationdb-server-%.el6.x86_64.rpm packages/foundationdb-clients-%.el6.x86_64.rpm packages/foundationdb-server-%.el7.x86_64.rpm packages/foundationdb-clients-%.el7.x86_64.rpm: $(PACKAGE_CONTENTS) versions.target $(RPM_FILES) + @echo "Packaging rpm" + @mkdir -p packages + @rm -f packages/foundationdb-server-*.rpm packages/foundationdb-clients-*.rpm + @packaging/rpm/buildrpms.sh $(VERSION) $(PKGRELEASE) + + packages: DEB RPM JAVA_RELEASE FDBTESTS FDBHEADERS + + packages_clean: DEB_clean RPM_clean JAVA_RELEASE_clean FDBHEADERS_clean + +endif + +ifeq ($(PLATFORM),osx) + ifeq ($(RELEASE),true) + PKGFILE := packages/FoundationDB-$(VERSION).pkg + else + PKGFILE := packages/FoundationDB-$(VERSION)-PRERELEASE.pkg + endif + + PKG: $(PACKAGE_CONTENTS) versions.target + @mkdir -p packages + @rm -f packages/*.pkg + @packaging/osx/buildpkg.sh $(PKGFILE) $(VERSION) $(PKGRELEASE) + + packages: PKG + + packages_clean: packages_osx_clean + + packages_osx_clean: + @rm -f packages/*.pkg +endif diff --git a/build/scver.mk b/build/scver.mk new file mode 100644 index 0000000000..18a1184827 --- /dev/null +++ b/build/scver.mk @@ -0,0 +1,166 @@ +# +# scver.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +######################################################################### +# +# This makefile will define the make variables related to source control +# variables, values, and settings +# +# +# Author: Alvin Moore +# Created: 15-08-01 +######################################################################### + + +# Retrieves the major version number from a version string +# Param: +# 1. String to parse in form 'major[.minor][.build]'. +MAJORVERFUNC = $(firstword $(subst ., ,$1)) + +# Retrieves the major version number from a version string +# If there is no minor part in the string, returns the second argument +# (if specified). +# Param: +# 1. String to parse in form 'major[.minor][.build]'. +# 2. (optional) Fallback value. +MINORVERFUNC = $(or $(word 2,$(subst ., ,$1)),$(value 2)) + +# Ensures that the specified directory is created +# Displays a creation message, if the directory does not exists +# Param: +# 1. Path to the directory to create +# 2. (optional) Display name of the directory. +CREATEDIRFUNC = if [ ! -d "$1" ]; then echo "`date +%F_%H-%M-%S` Creating $2 directory: $1"; mkdir -p "$1"; fi + + +# Make Environment Settings +# +ARCH := $(shell uname -m) +MAKEDIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) +FDBDIR := $(abspath $(MAKEDIR)/..) +FDBPARENTDIR := $(abspath $(FDBDIR)/..) +FDBDIRBASE := $(shell basename $(FDBDIR)) +USERID := $(shell id -u) +USER := $(shell whoami) +PROCESSID := $(shell echo "$$$$") + + +# +# Define the Java Variables +# + +# Determine the Java compiler, if not defined +ifndef JAVAC + JAVAC := $(shell which javac) + ifeq ($(JAVAC),) +$(warning JAVA compiler is not installed on $(PLATFORM) $(ARCH)) + endif +endif + +# Define the Java Flags based on Java version +ifdef JAVAC + JAVAVER := $(shell bash -c 'javac -version 2>&1 | cut -d\ -f2-') + JAVAVERMAJOR := $(call MAJORVERFUNC,$(JAVAVER)) + JAVAVERMINOR := $(call MINORVERFUNC,$(JAVAVER)) + ifneq ($(JAVAVERMAJOR),1) +$(warning Unable to compile source using Java version: $(JAVAVER) with compiler: $(JAVAC) on $(PLATFORM) $(ARCH)) + else + # Set specific flags for Java 1.8 + ifeq ($(JAVAVERMAJOR).$(JAVAVERMINOR),1.8) + JAVAFLAGS := -source 1.6 -target 1.6 + JAVA-completableFLAGS := + else + ifeq ($(JAVAVERMAJOR).$(JAVAVERMINOR),1.7) + JAVAFLAGS := -source 1.6 -target 1.6 + JAVA-completableFLAGS := + else + JAVAFLAGS := + JAVA-completableFLAGS := + endif + endif + endif +endif + + +# Determine active Version Control +# +GITPRESENT := $(wildcard $(FDBDIR)/.git) +HGPRESENT := $(wildcard $(FDBDIR)/.hg) + +# Use Git, if not missing +ifneq ($(GITPRESENT),) + SCVER := $(shell cd "$(FDBDIR)" && git --version 2>/dev/null) + ifneq ($(SCVER),) + VERSION_ID := $(shell cd "$(FDBDIR)" && git rev-parse --verify HEAD) + SOURCE_CONTROL := GIT + SCBRANCH := $(shell cd "$(FDBDIR)" && git rev-parse --abbrev-ref HEAD) + else +$(error Missing git executable on $(PLATFORM) ) + endif +# Otherwise, use Mercurial +else + # Otherwise, use Mercurial, if not missing + ifneq ($(HGPRESENT),) + SCVER := $(shell cd "$(FDBDIR)" && hg --version 2>/dev/null) + ifdef SCVER + VERSION_ID := $(shell cd "$(FDBDIR)" && hg id -n) + SOURCE_CONTROL := MERCURIAL + SCBRANCH := $(shell cd "$(FDBDIR)" && hg branch) + else +$(error Missing hg executable on $(PLATFORM)) + endif + else + FDBFILES := (shell ls -la $(FDBDIR)) +$(error Missing source control information for source on $(PLATFORM) in directory: $(FDBDIR) with files: $(FDBFILES)) + endif +endif + +# Set the RELEASE variable based on the KVRELEASE variable. +ifeq ($(KVRELEASE),1) + RELEASE := true +endif + +# Define the Package Release and the File Version +ifeq ($(RELEASE),true) + PKGRELEASE := 1 +else ifeq ($(PRERELEASE),true) + PKGRELEASE := 0.$(VERSION_ID).PRERELEASE +else + PKGRELEASE := 0INTERNAL +endif + + +info: + @echo "Displaying Make Information" + @echo "Version: $(VERSION)" + @echo "Package: $(PACKAGE_NAME)" + @echo "Version ID: $(VERSION_ID)" + @echo "Package ID: $(PKGRELEASE)" + @echo "Source Control: $(SOURCE_CONTROL)" + @echo "SC Branch: $(SCBRANCH)" + @echo "Git Dir: $(GITPRESENT)" + @echo "Mercurial Dir: $(HGPRESENT)" + @echo "Make Dir: $(MAKEDIR)" + @echo "Foundation Dir: $(FDBDIR)" + @echo "Fdb Dir Base: $(FDBDIRBASE)" + @echo "User: ($(USERID)) $(USER)" + @echo "Java Version: ($(JAVAVERMAJOR).$(JAVAVERMINOR)) $(JAVAVER)" + @echo "Platform: $(PLATFORM)" + @echo "" diff --git a/build/tarball.py b/build/tarball.py new file mode 100644 index 0000000000..ec66dd0121 --- /dev/null +++ b/build/tarball.py @@ -0,0 +1,45 @@ +#!/usr/bin/python +# +# tarball.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +import tarfile +import argparse +import glob + +def createTarball(outFile, rootDir, inFiles): + tar = tarfile.open(outFile, 'w:gz') + for fStr in inFiles: + for f in glob.glob(fStr): + if rootDir is None: + tar.add(f) + else: + tar.add(f, rootDir + "/" + f) + + tar.close() + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Python tar utility') + parser.add_argument('-r', dest='rootDir', type=str, help='The root directory for files in the tarball') + parser.add_argument('outFile', type=str, help='The output tarball') + parser.add_argument('inFile', type=str, nargs='+', help='The files to put into the tarball') + + args = parser.parse_args() + createTarball(args.outFile, args.rootDir, args.inFile) diff --git a/build/valgrind.mk b/build/valgrind.mk new file mode 100644 index 0000000000..c1f6af7e88 --- /dev/null +++ b/build/valgrind.mk @@ -0,0 +1,36 @@ +# +# valgrind.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#VALGRIND := 1 + +ifeq ($(VALGRIND), 1) +ifeq ($(DEBUGLEVEL), 1) +$(info Enabling Valgrind instrumentation) +endif + CFLAGS += -DVALGRIND=1 -DUSE_VALGRIND=1 + + ifeq ($(PLATFORM), linux) + CFLAGS += -I/usr/include/valgrind + else ifeq ($(PLATFORM), osx) + CFLAGS += -I/usr/local/include/valgrind + else + $(error valgrind not supported on platform $(PLATFORM)) + endif +endif diff --git a/build/vcxproj.mk b/build/vcxproj.mk new file mode 100644 index 0000000000..9a2482ed00 --- /dev/null +++ b/build/vcxproj.mk @@ -0,0 +1,114 @@ +# +# vcxproj.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile; -*- + +TARGETS += GENNAME +CLEAN_TARGETS += GENNAME()_clean + +GENNAME()_ALL_SOURCES := $(addprefix GENDIR/,GENSOURCES) + +GENNAME()_BUILD_SOURCES := $(patsubst %.actor.cpp,%.actor.g.cpp,$(filter-out %.h %.hpp,$(GENNAME()_ALL_SOURCES))) +GENNAME()_GENERATED_SOURCES := $(patsubst %.actor.h,%.actor.g.h,$(patsubst %.actor.cpp,%.actor.g.cpp,$(filter %.actor.h %.actor.cpp,$(GENNAME()_ALL_SOURCES)))) +GENERATED_SOURCES += $(GENNAME()_GENERATED_SOURCES) + +-include GENDIR/local.mk + +# If we have any static libs, we have to wrap them in the appropriate +# compiler flag magic +ifeq ($(GENNAME()_STATIC_LIBS),) + GENNAME()_STATIC_LIBS_REAL := +else + GENNAME()_STATIC_LIBS_REAL := -Wl,-Bstatic $(GENNAME()_STATIC_LIBS) -Wl,-Bdynamic +endif + +# If we have any -L directives in our LDFLAGS, we need to add those +# paths to the VPATH +VPATH += $(addprefix :,$(patsubst -L%,%,$(filter -L%,$(GENNAME()_LDFLAGS)))) + +IGNORE := $(shell echo $(VPATH)) + +GENNAME()_OBJECTS := $(addprefix $(OBJDIR)/,$(GENNAME()_BUILD_SOURCES:=.o)) +GENNAME()_DEPS := $(addprefix $(DEPSDIR)/,$(GENNAME()_BUILD_SOURCES:=.d)) + +.PHONY: GENNAME()_clean GENNAME + +GENNAME: GENTARGET + +-include $(GENNAME()_DEPS) + +GENDIR/%.actor.g.cpp: GENDIR/%.actor.cpp $(ACTORCOMPILER) + @echo "Actorcompiling $<" + @$(MONO) $(ACTORCOMPILER) $< $@ >/dev/null + +GENDIR/%.actor.g.h: GENDIR/%.actor.h $(ACTORCOMPILER) + @if [ -e $< ]; then echo "Actorcompiling $<" ; $(MONO) $(ACTORCOMPILER) $< $@ >/dev/null ; fi +.PRECIOUS: GENDIR/%.actor.g.cpp GENDIR/%.actor.g.h + +# The order-only dependency on the generated .h files is to force make +# to actor compile all headers before attempting compilation of any .c +# or .cpp files. We have no mechanism to detect dependencies on +# generated headers before compilation. + +$(OBJDIR)/GENDIR/%.cpp.o: GENDIR/%.cpp $(ALL_MAKEFILES) | $(filter %.h,$(GENERATED_SOURCES)) + @echo "Compiling $<" +ifeq ($(VERBOSE),1) + @echo "$(CCACHE_CXX) $(CFLAGS) $(CXXFLAGS) $(GENNAME()_CFLAGS) $(GENNAME()_CXXFLAGS) -MMD -MT $@ -MF $(DEPSDIR)/$<.d.tmp -c $< -o $@" +endif + @mkdir -p $(DEPSDIR)/$(> $(DEPSDIR)/$<.d && \ + rm $(DEPSDIR)/$<.d.tmp + +$(OBJDIR)/GENDIR/%.c.o: GENDIR/%.c $(ALL_MAKEFILES) | $(filter %.h,$(GENERATED_SOURCES)) + @echo "Compiling $<" +ifeq ($(VERBOSE),1) + @echo "$(CCACHE_CC) $(CFLAGS) $(GENNAME()_CFLAGS) -MMD -MT $@ -MF $(DEPSDIR)/$<.d.tmp -c $< -o $@" +endif + @mkdir -p $(DEPSDIR)/$(> $(DEPSDIR)/$<.d && \ + rm $(DEPSDIR)/$<.d.tmp + +$(OBJDIR)/GENDIR/%.S.o: GENDIR/%.S $(ALL_MAKEFILES) | $(filter %.h,$(GENERATED_SOURCES)) + @echo "Assembling $<" +ifeq ($(VERBOSE),1) + @echo "$(CCACHE_CC) $(CFLAGS) $(GENNAME()_CFLAGS) -MMD -MT $@ -MF $(DEPSDIR)/$<.d.tmp -c $< -o $@" +endif + @mkdir -p $(DEPSDIR)/$(> $(DEPSDIR)/$<.d && \ + rm $(DEPSDIR)/$<.d.tmp + +GENNAME()_clean: + @echo "Cleaning GENNAME" + @rm -f GENTARGET $(GENNAME()_GENERATED_SOURCES) GENTARGET().debug GENTARGET()-debug + @rm -rf $(DEPSDIR)/GENDIR + @rm -rf $(OBJDIR)/GENDIR + +GENTARGET: $(GENNAME()_OBJECTS) $(GENNAME()_LIBS) $(GENNAME()_STATIC_LIBS) $(ALL_MAKEFILES) build/link-wrapper.sh build/link-validate.sh + @mkdir -p GENOUTDIR + @./build/link-wrapper.sh GENCONFIGTYPE GENNAME $@ $(TARGET_LIBC_VERSION) diff --git a/build/vcxprojtom4.py b/build/vcxprojtom4.py new file mode 100644 index 0000000000..f51458b757 --- /dev/null +++ b/build/vcxprojtom4.py @@ -0,0 +1,73 @@ +#!/usr/bin/python +# +# vcxprojtom4.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +import sys + +if len(sys.argv) != 2: + print """Usage: + %s [input]""" % sys.argv[0] + sys.exit() + +vcxproj = sys.argv[1] + +from xml.dom.minidom import parse + +try: + dom = parse(vcxproj) +except: + print "ERROR: Unable to open VCXProj file %s" % vcxproj + sys.exit() + +# We need to find out what kind of project/configuration we're going +# to build. FIXME: Right now we're hardcoded to look for the +# Release|X64 configuration/platform. + +groups = dom.getElementsByTagName("PropertyGroup") +for group in groups: + if group.getAttribute("Label").lower() == "configuration" and \ + group.getAttribute("Condition").lower() == "'$(configuration)|$(platform)'=='release|x64'": + ctnodes = group.getElementsByTagName("ConfigurationType") + configType = ctnodes[0].childNodes[0].data + break + +print "define(`GENCONFIGTYPE', `%s')dnl" % configType + +if configType == "StaticLibrary": + print "define(`GENTARGET', `lib/lib`'GENNAME.a')dnl" + print "define(`GENOUTDIR', `lib')dnl" +elif configType == "DynamicLibrary": + print "define(`GENTARGET', `lib/lib`'GENNAME.$(DLEXT)')dnl" + print "define(`GENOUTDIR', `lib')dnl" +elif configType == "Application": + print "define(`GENTARGET', `bin/'`GENNAME')dnl" + print "define(`GENOUTDIR', `bin')dnl" +else: + print "ERROR: Unable to determine configuration type" + sys.exit() + +sources = [ node.getAttribute("Include").replace('\\', '/') for node in + dom.getElementsByTagName("ActorCompiler") + + dom.getElementsByTagName("ClCompile") + + dom.getElementsByTagName("ClInclude") + if not node.getElementsByTagName("ExcludedFromBuild") and node.hasAttribute("Include") ] + +print "define(`GENSOURCES', `%s')dnl" % ' '.join(sorted(sources)) diff --git a/fdbbackup/backup.actor.cpp b/fdbbackup/backup.actor.cpp new file mode 100644 index 0000000000..22c78f5227 --- /dev/null +++ b/fdbbackup/backup.actor.cpp @@ -0,0 +1,2580 @@ +/* + * backup.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/FastAlloc.h" +#include "flow/serialize.h" +#include "flow/IRandom.h" +#include "flow/genericactors.actor.h" + +#include "fdbclient/FDBTypes.h" +#include "fdbclient/BackupAgent.h" +#include "fdbclient/Status.h" +#include "fdbclient/BackupContainer.h" + +#include "fdbclient/RunTransaction.actor.h" +#include "fdbrpc/Platform.h" +#include "fdbclient/json_spirit/json_spirit_writer_template.h" + +#include +#include +#include // std::transform +#include +#include +using std::cout; +using std::endl; + +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef min +#undef max +#endif +#include + +#define BOOST_DATE_TIME_NO_LIB +#include + +#ifdef __linux__ +#include +#ifdef ALLOC_INSTRUMENTATION +#include +#endif +#endif + +#ifndef WIN32 +#include "versions.h" +#endif + +#include "flow/SimpleOpt.h" + + +// Type of program being executed +enum enumProgramExe { + EXE_AGENT, EXE_BACKUP, EXE_RESTORE, EXE_DR_AGENT, EXE_DB_BACKUP, EXE_BLOBMANAGER, EXE_UNDEFINED +}; + +enum enumBackupType { + BACKUP_UNDEFINED=0, BACKUP_START, BACKUP_STATUS, BACKUP_ABORT, BACKUP_WAIT, BACKUP_DISCONTINUE +}; + +enum enumDBType { + DB_UNDEFINED=0, DB_START, DB_STATUS, DB_SWITCH, DB_ABORT +}; + +enum enumRestoreType { + RESTORE_UNKNOWN, RESTORE_START, RESTORE_STATUS, RESTORE_ABORT, RESTORE_WAIT +}; + +// +enum { + // Backup constants + OPT_DESTCONTAINER, OPT_ERRORLIMIT, OPT_NOSTOPWHENDONE, + + // Backup and Restore constants + OPT_TAGNAME, OPT_BACKUPKEYS, OPT_WAITFORDONE, + + // Restore constants + OPT_RESTORECONTAINER, OPT_DBVERSION, OPT_PREFIX_ADD, OPT_PREFIX_REMOVE, + + // Shared constants + OPT_CLUSTERFILE, OPT_QUIET, OPT_DRYRUN, OPT_FORCE, + OPT_HELP, OPT_DEVHELP, OPT_VERSION, OPT_PARENTPID, OPT_CRASHONERROR, + OPT_NOBUFSTDOUT, OPT_BUFSTDOUTERR, OPT_TRACE, OPT_TRACE_DIR, + OPT_KNOB, OPT_TRACE_LOG_GROUP, + + //DB constants + OPT_SOURCE_CLUSTER, + OPT_DEST_CLUSTER, + OPT_CLEANUP +}; + +CSimpleOpt::SOption g_rgAgentOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_CLUSTERFILE, "-C", SO_REQ_SEP }, + { OPT_CLUSTERFILE, "--cluster_file", SO_REQ_SEP }, + { OPT_TRACE_LOG_GROUP, "--loggroup", SO_REQ_SEP }, + { OPT_KNOB, "--knob_", SO_REQ_SEP }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgBackupStartOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_CLUSTERFILE, "-C", SO_REQ_SEP }, + { OPT_CLUSTERFILE, "--cluster_file", SO_REQ_SEP }, + { OPT_WAITFORDONE, "-w", SO_NONE }, + { OPT_WAITFORDONE, "--waitfordone", SO_NONE }, + { OPT_NOSTOPWHENDONE, "-z", SO_NONE }, + { OPT_NOSTOPWHENDONE, "--no-stop-when-done",SO_NONE }, + { OPT_DESTCONTAINER, "-d", SO_REQ_SEP }, + { OPT_DESTCONTAINER, "--destcontainer", SO_REQ_SEP }, + { OPT_TAGNAME, "-t", SO_REQ_SEP }, + { OPT_TAGNAME, "--tagname", SO_REQ_SEP }, + { OPT_BACKUPKEYS, "-k", SO_REQ_SEP }, + { OPT_BACKUPKEYS, "--keys", SO_REQ_SEP }, + { OPT_DRYRUN, "-n", SO_NONE }, + { OPT_DRYRUN, "--dryrun", SO_NONE }, + { OPT_FORCE, "-f", SO_NONE }, + { OPT_FORCE, "--force", SO_NONE }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + { OPT_KNOB, "--knob_", SO_REQ_SEP }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgBackupStatusOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_CLUSTERFILE, "-C", SO_REQ_SEP }, + { OPT_CLUSTERFILE, "--cluster_file", SO_REQ_SEP }, + { OPT_ERRORLIMIT, "-e", SO_REQ_SEP }, + { OPT_ERRORLIMIT, "--errorlimit", SO_REQ_SEP }, + { OPT_TAGNAME, "-t", SO_REQ_SEP }, + { OPT_TAGNAME, "--tagname", SO_REQ_SEP }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgBackupAbortOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_CLUSTERFILE, "-C", SO_REQ_SEP }, + { OPT_CLUSTERFILE, "--cluster_file", SO_REQ_SEP }, + { OPT_TAGNAME, "-t", SO_REQ_SEP }, + { OPT_TAGNAME, "--tagname", SO_REQ_SEP }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgBackupDiscontinueOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_CLUSTERFILE, "-C", SO_REQ_SEP }, + { OPT_CLUSTERFILE, "--cluster_file", SO_REQ_SEP }, + { OPT_TAGNAME, "-t", SO_REQ_SEP }, + { OPT_TAGNAME, "--tagname", SO_REQ_SEP }, + { OPT_WAITFORDONE, "-w", SO_NONE }, + { OPT_WAITFORDONE, "--waitfordone", SO_NONE }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgBackupWaitOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_CLUSTERFILE, "-C", SO_REQ_SEP }, + { OPT_CLUSTERFILE, "--cluster_file", SO_REQ_SEP }, + { OPT_TAGNAME, "-t", SO_REQ_SEP }, + { OPT_TAGNAME, "--tagname", SO_REQ_SEP }, + { OPT_NOSTOPWHENDONE, "-z", SO_NONE }, + { OPT_NOSTOPWHENDONE, "--no-stop-when-done",SO_NONE }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgRestoreOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_CLUSTERFILE, "-C", SO_REQ_SEP }, + { OPT_KNOB, "--knob_", SO_REQ_SEP }, + { OPT_RESTORECONTAINER,"-r", SO_REQ_SEP }, + { OPT_PREFIX_ADD, "-add_prefix", SO_REQ_SEP }, + { OPT_PREFIX_REMOVE, "-remove_prefix", SO_REQ_SEP }, + { OPT_TAGNAME, "-t", SO_REQ_SEP }, + { OPT_TAGNAME, "--tagname", SO_REQ_SEP }, + { OPT_BACKUPKEYS, "-k", SO_REQ_SEP }, + { OPT_BACKUPKEYS, "--keys", SO_REQ_SEP }, + { OPT_WAITFORDONE, "-w", SO_NONE }, + { OPT_WAITFORDONE, "--waitfordone", SO_NONE }, + { OPT_CLUSTERFILE, "--cluster_file", SO_REQ_SEP }, + { OPT_DBVERSION, "--version", SO_REQ_SEP }, + { OPT_DBVERSION, "-v", SO_REQ_SEP }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_DRYRUN, "-n", SO_NONE }, + { OPT_DRYRUN, "--dryrun", SO_NONE }, + { OPT_FORCE, "-f", SO_NONE }, + { OPT_FORCE, "--force", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgDBAgentOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_TRACE_LOG_GROUP, "--loggroup", SO_REQ_SEP }, + { OPT_SOURCE_CLUSTER, "-s", SO_REQ_SEP }, + { OPT_SOURCE_CLUSTER, "--source", SO_REQ_SEP }, + { OPT_DEST_CLUSTER, "-d", SO_REQ_SEP }, + { OPT_DEST_CLUSTER, "--destination", SO_REQ_SEP }, + { OPT_KNOB, "--knob_", SO_REQ_SEP }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgDBStartOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_SOURCE_CLUSTER, "-s", SO_REQ_SEP }, + { OPT_SOURCE_CLUSTER, "--source", SO_REQ_SEP }, + { OPT_DEST_CLUSTER, "-d", SO_REQ_SEP }, + { OPT_DEST_CLUSTER, "--destination", SO_REQ_SEP }, + { OPT_TAGNAME, "-t", SO_REQ_SEP }, + { OPT_TAGNAME, "--tagname", SO_REQ_SEP }, + { OPT_BACKUPKEYS, "-k", SO_REQ_SEP }, + { OPT_BACKUPKEYS, "--keys", SO_REQ_SEP }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgDBStatusOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_SOURCE_CLUSTER, "-s", SO_REQ_SEP }, + { OPT_SOURCE_CLUSTER, "--source", SO_REQ_SEP }, + { OPT_DEST_CLUSTER, "-d", SO_REQ_SEP }, + { OPT_DEST_CLUSTER, "--destination", SO_REQ_SEP }, + { OPT_ERRORLIMIT, "-e", SO_REQ_SEP }, + { OPT_ERRORLIMIT, "--errorlimit", SO_REQ_SEP }, + { OPT_TAGNAME, "-t", SO_REQ_SEP }, + { OPT_TAGNAME, "--tagname", SO_REQ_SEP }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgDBSwitchOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_SOURCE_CLUSTER, "-s", SO_REQ_SEP }, + { OPT_SOURCE_CLUSTER, "--source", SO_REQ_SEP }, + { OPT_DEST_CLUSTER, "-d", SO_REQ_SEP }, + { OPT_DEST_CLUSTER, "--destination", SO_REQ_SEP }, + { OPT_TAGNAME, "-t", SO_REQ_SEP }, + { OPT_TAGNAME, "--tagname", SO_REQ_SEP }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgDBAbortOptions[] = { +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, +#endif + { OPT_SOURCE_CLUSTER, "-s", SO_REQ_SEP }, + { OPT_SOURCE_CLUSTER, "--source", SO_REQ_SEP }, + { OPT_DEST_CLUSTER, "-d", SO_REQ_SEP }, + { OPT_DEST_CLUSTER, "--destination", SO_REQ_SEP }, + { OPT_CLEANUP, "--cleanup", SO_NONE }, + { OPT_TAGNAME, "-t", SO_REQ_SEP }, + { OPT_TAGNAME, "--tagname", SO_REQ_SEP }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + { OPT_QUIET, "-q", SO_NONE }, + { OPT_QUIET, "--quiet", SO_NONE }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + + SO_END_OF_OPTIONS +}; + +CSimpleOpt::SOption g_rgBlobOptions[] = { + { OPT_KNOB, "--knob_", SO_REQ_SEP }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--logdir", SO_REQ_SEP }, + + SO_END_OF_OPTIONS +}; + +const KeyRef exeAgent = LiteralStringRef("backup_agent"); +const KeyRef exeBackup = LiteralStringRef("fdbbackup"); +const KeyRef exeRestore = LiteralStringRef("fdbrestore"); +const KeyRef exeDatabaseAgent = LiteralStringRef("dr_agent"); +const KeyRef exeDatabaseBackup = LiteralStringRef("fdbdr"); +const KeyRef exeBlobManager = LiteralStringRef("fdbblob"); + +extern void flushTraceFileVoid(); +extern const char* getHGVersion(); + +#ifdef _WIN32 +void parentWatcher(void *parentHandle) { + HANDLE parent = (HANDLE)parentHandle; + int signal = WaitForSingleObject(parent, INFINITE); + CloseHandle(parentHandle); + if (signal == WAIT_OBJECT_0) + criticalError(FDB_EXIT_SUCCESS, "ParentProcessExited", "Parent process exited"); + TraceEvent(SevError, "ParentProcessWaitFailed").detail("RetCode", signal).GetLastError(); +} + +#endif + +static void printVersion() { + printf("FoundationDB " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + printf("source version %s\n", getHGVersion()); + printf("protocol %llx\n", (long long) currentProtocolVersion); +} + +static void printHelpTeaser( const char *name ) { + fprintf(stderr, "Try `%s --help' for more information.\n", name); +} + +static void printAgentUsage(bool devhelp) { + printf("FoundationDB " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + printf("Usage: %s [OPTIONS]\n\n", exeAgent.toString().c_str()); + printf(" -C CONNFILE The path of a file containing the connection string for the\n" + " FoundationDB cluster. The default is first the value of the\n" + " FDB_CLUSTER_FILE environment variable, then `./fdb.cluster',\n" + " then `%s'.\n", platform::getDefaultClusterFilePath().c_str()); + printf(" --log Enables trace file logging for the CLI session.\n" + " --logdir PATH Specifes the output directory for trace files. If\n" + " unspecified, defaults to the current directory. Has\n" + " no effect unless --log is specified.\n"); + printf(" -v, --version Print version information and exit.\n"); + printf(" -h, --help Display this help and exit.\n"); + if (devhelp) { +#ifdef _WIN32 + printf(" -n Create a new console.\n"); + printf(" -q Disable error dialog on crash.\n"); + printf(" --parentpid PID\n"); + printf(" Specify a process after whose termination to exit.\n"); +#endif + } + + return; +} + +void printBlobStoreParameterInfo(const char *pad) { + printf("%sValid Blob Store parameters:\n\n", pad); + for(auto &f : BlobStoreEndpoint::BlobKnobs::getKnobDescriptions()) + printf("%s %s\n", pad, f.c_str()); +} + +void printBackupContainerInfo() { + printf(" Backup URL forms:\n\n"); + std::vector formats = IBackupContainer::getURLFormats(); + for(auto &f : formats) + printf(" %s\n", f.c_str()); + printf("\n"); + printBlobStoreParameterInfo(" "); +} + +static void printBackupUsage(bool devhelp) { + printf("FoundationDB " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + printf("Usage: %s (start | status | abort | wait | discontinue) [OPTIONS]\n\n", exeBackup.toString().c_str()); + printf(" -C CONNFILE The path of a file containing the connection string for the\n" + " FoundationDB cluster. The default is first the value of the\n" + " FDB_CLUSTER_FILE environment variable, then `./fdb.cluster',\n" + " then `%s'.\n", platform::getDefaultClusterFilePath().c_str()); + printf(" -d, --destcontainer URL\n" + " The Backup URL for the destination of this backup.\n"); + printBackupContainerInfo(); + printf(" -e ERRORLIMIT The maximum number of errors printed by status (default is 10).\n"); + printf(" -k KEYS List of key ranges to backup.\n" + " If not specified, the entire database will be backed up.\n"); + printf(" -n, --dry-run Perform a trial run with no changes made.\n"); + printf(" -v, --version Print version information and exit.\n"); + printf(" -w, --wait Wait for the backup to complete (allowed with `start' and `discontinue').\n"); + printf(" -z, --no-stop-when-done\n" + " Do not stop backup when restorable.\n"); + printf(" -h, --help Display this help and exit.\n"); + printf("\n" + " KEYS FORMAT: \" \" [...]\n"); + + if (devhelp) { +#ifdef _WIN32 + printf(" -n Create a new console.\n"); + printf(" -q Disable error dialog on crash.\n"); + printf(" --parentpid PID\n"); + printf(" Specify a process after whose termination to exit.\n"); +#endif + } + + return; +} + +static void printRestoreUsage(bool devhelp ) { + printf("FoundationDB " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + printf("Usage: %s (start | status | abort | wait) [OPTIONS]\n\n", exeRestore.toString().c_str()); + //printf(" FOLDERS Paths to folders containing the backup files.\n"); + printf("Options for all commands:\n\n"); + printf(" -C CONNFILE The path of a file containing the connection string for the\n" + " FoundationDB cluster. The default is first the value of the\n" + " FDB_CLUSTER_FILE environment variable, then `./fdb.cluster',\n" + " then `%s'.\n", platform::getDefaultClusterFilePath().c_str()); + printf(" -t TAGNAME The restore tag to act on. Default is 'default'\n"); + printf(" --tagname TAGNAME\n\n"); + printf(" Options for start:\n\n"); + printf(" -r URL The Backup URL for the restore to read from.\n"); + printBackupContainerInfo(); + printf(" -w Wait for the restore to complete before exiting. Prints progress updates.\n"); + printf(" --waitfordone\n"); + printf(" -k KEYS List of key ranges from the backup to restore\n"); + printf(" --remove_prefix PREFIX prefix to remove from the restored keys\n"); + printf(" --add_prefix PREFIX prefix to add to the restored keys\n"); + printf(" -n, --dry-run Perform a trial run with no changes made.\n"); + printf(" -v DBVERSION The version at which the database will be restored.\n"); + printf(" -h, --help Display this help and exit.\n"); + + if( devhelp ) { +#ifdef _WIN32 + printf(" -q Disable error dialog on crash.\n"); + printf(" --parentpid PID\n"); + printf(" Specify a process after whose termination to exit.\n"); +#endif + } + + return; +} + +static void printDBAgentUsage(bool devhelp) { + printf("FoundationDB " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + printf("Usage: %s [OPTIONS]\n\n", exeDatabaseAgent.toString().c_str()); + printf(" -d CONNFILE The path of a file containing the connection string for the\n" + " destination FoundationDB cluster.\n"); + printf(" -s CONNFILE The path of a file containing the connection string for the\n" + " source FoundationDB cluster.\n"); + printf(" --log Enables trace file logging for the CLI session.\n" + " --logdir PATH Specifes the output directory for trace files. If\n" + " unspecified, defaults to the current directory. Has\n" + " no effect unless --log is specified.\n"); + printf(" -v, --version Print version information and exit.\n"); + printf(" -h, --help Display this help and exit.\n"); + if (devhelp) { +#ifdef _WIN32 + printf(" -n Create a new console.\n"); + printf(" -q Disable error dialog on crash.\n"); + printf(" --parentpid PID\n"); + printf(" Specify a process after whose termination to exit.\n"); +#endif + } + + return; +} + +static void printDBBackupUsage(bool devhelp) { + printf("FoundationDB " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + printf("Usage: %s (start | status | switch | abort) [OPTIONS]\n\n", exeDatabaseBackup.toString().c_str()); + printf(" -d, --destination CONNFILE\n" + " The path of a file containing the connection string for the\n"); + printf(" destination FoundationDB cluster.\n"); + printf(" -s, --source CONNFILE\n" + " The path of a file containing the connection string for the\n" + " source FoundationDB cluster.\n"); + printf(" -e ERRORLIMIT The maximum number of errors printed by status (default is 10).\n"); + printf(" -k KEYS List of key ranges to backup.\n" + " If not specified, the entire database will be backed up.\n"); + printf(" --cleanup Abort will attempt to stop mutation logging on the source cluster.\n"); + printf(" -v, --version Print version information and exit.\n"); + printf(" -h, --help Display this help and exit.\n"); + printf("\n" + " KEYS FORMAT: \" \" [...]\n"); + + if (devhelp) { +#ifdef _WIN32 + printf(" -n Create a new console.\n"); + printf(" -q Disable error dialog on crash.\n"); + printf(" --parentpid PID\n"); + printf(" Specify a process after whose termination to exit.\n"); +#endif + } + + return; +} + +static void printBlobManagerUsage() { + printf("FoundationDB " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + printf("Usage: %s [options] )\n\n", exeBlobManager.toString().c_str()); + printf(" Commands:\n"); + printf(" list Lists the backups found at the given blob store URL. URL format is\n"); + printf(" %s\n", BlobStoreEndpoint::getURLFormat().c_str()); + printf(" listinfo Same as list but shows 'info' output for each backup.\n"); + printf(" info Scans the given blob store Backup URL and outputs size and object count. URL format is\n"); + printf(" %s\n", BackupContainerBlobStore::getURLFormat().c_str()); + printf(" dump Same as list but also lists all objects and their sizes.\n"); + printf(" delete Deletes the backup specified by the blob store Backup URL. URL format is\n"); + printf(" %s\n", BackupContainerBlobStore::getURLFormat().c_str()); + printf("\n"); + printBlobStoreParameterInfo(" "); + printf(" -v, --version Print version information and exit.\n"); + printf(" -h, --help Display this help and exit.\n"); + return; +} + +static void printUsage(enumProgramExe programExe, bool devhelp) +{ + + switch (programExe) + { + case EXE_AGENT: + printAgentUsage(devhelp); + break; + case EXE_BACKUP: + printBackupUsage(devhelp); + break; + case EXE_RESTORE: + printRestoreUsage(devhelp); + break; + case EXE_DR_AGENT: + printDBAgentUsage(devhelp); + break; + case EXE_DB_BACKUP: + printDBBackupUsage(devhelp); + break; + case EXE_BLOBMANAGER: + printBlobManagerUsage(); + break; + case EXE_UNDEFINED: + default: + break; + } + + return; +} + +extern bool g_crashOnError; + +// Return the type of program executable based on the name of executable file +enumProgramExe getProgramType(std::string programExe) +{ + enumProgramExe enProgramExe = EXE_UNDEFINED; + + // lowercase the string + std::transform(programExe.begin(), programExe.end(), programExe.begin(), ::tolower); + + // Remove the extension, if Windows +#ifdef _WIN32 + size_t lastDot = programExe.find_last_of("."); + if (lastDot != std::string::npos) { + size_t lastSlash = programExe.find_last_of("\\"); + + // Ensure last dot is after last slash, if present + if ((lastSlash == std::string::npos)|| + (lastSlash < lastDot) ) + { + programExe = programExe.substr(0, lastDot); + } + } +#endif + + // Check if backup agent + if ((programExe.length() >= exeAgent.size()) && + (programExe.compare(programExe.length()-exeAgent.size(), exeAgent.size(), (const char*) exeAgent.begin()) == 0) ) + { + enProgramExe = EXE_AGENT; + } + + // Check if backup + else if ((programExe.length() >= exeBackup.size()) && + (programExe.compare(programExe.length() - exeBackup.size(), exeBackup.size(), (const char*)exeBackup.begin()) == 0)) + { + enProgramExe = EXE_BACKUP; + } + + // Check if restore + else if ((programExe.length() >= exeRestore.size()) && + (programExe.compare(programExe.length() - exeRestore.size(), exeRestore.size(), (const char*)exeRestore.begin()) == 0)) + { + enProgramExe = EXE_RESTORE; + } + + // Check if db agent + else if ((programExe.length() >= exeDatabaseAgent.size()) && + (programExe.compare(programExe.length() - exeDatabaseAgent.size(), exeDatabaseAgent.size(), (const char*)exeDatabaseAgent.begin()) == 0)) + { + enProgramExe = EXE_DR_AGENT; + } + + // Check if db backup + else if ((programExe.length() >= exeDatabaseBackup.size()) && + (programExe.compare(programExe.length() - exeDatabaseBackup.size(), exeDatabaseBackup.size(), (const char*)exeDatabaseBackup.begin()) == 0)) + { + enProgramExe = EXE_DB_BACKUP; + } + + // Check if blob manager + else if ((programExe.length() >= exeBlobManager.size()) && + (programExe.compare(programExe.length() - exeBlobManager.size(), exeBlobManager.size(), (const char*)exeBlobManager.begin()) == 0)) + { + enProgramExe = EXE_BLOBMANAGER; + } + + return enProgramExe; +} + +enumBackupType getBackupType(std::string backupType) +{ + enumBackupType enBackupType = BACKUP_UNDEFINED; + + // lowercase the string + std::transform(backupType.begin(), backupType.end(), backupType.begin(), ::tolower); + + static std::map values; + if(values.empty()) { + values["start"] = BACKUP_START; + values["status"] = BACKUP_STATUS; + values["abort"] = BACKUP_ABORT; + values["wait"] = BACKUP_WAIT; + values["discontinue"] = BACKUP_DISCONTINUE; + } + + auto i = values.find(backupType); + if(i != values.end()) + enBackupType = i->second; + + return enBackupType; +} + +enumRestoreType getRestoreType(std::string name) { + if(name == "start") return RESTORE_START; + if(name == "abort") return RESTORE_ABORT; + if(name == "status") return RESTORE_STATUS; + if(name == "wait") return RESTORE_WAIT; + return RESTORE_UNKNOWN; +} + +enumDBType getDBType(std::string dbType) +{ + enumDBType enBackupType = DB_UNDEFINED; + + // lowercase the string + std::transform(dbType.begin(), dbType.end(), dbType.begin(), ::tolower); + + static std::map values; + if(values.empty()) { + values["start"] = DB_START; + values["status"] = DB_STATUS; + values["switch"] = DB_SWITCH; + values["abort"] = DB_ABORT; + } + + auto i = values.find(dbType); + if(i != values.end()) + enBackupType = i->second; + + return enBackupType; +} + +ACTOR Future getLayerStatus(Reference tr, std::string name, std::string id, enumProgramExe exe, Database dest) { + // This process will write a document that looks like this: + // { backup : { $expires : {}, version: } + // so that the value under 'backup' will eventually expire to null and thus be ignored by + // readers of status. This is because if all agents die then they can no longer clean up old + // status docs from other dead agents. + + state Version readVer = wait(tr->getReadVersion()); + + state json_spirit::mValue layersRootValue; // Will contain stuff that goes into the doc at the layers status root + JSONDoc layersRoot(layersRootValue); // Convenient mutator / accessor for the layers root + JSONDoc op = layersRoot.subDoc(name); // Operator object for the $expires operation + // Create the $expires key which is where the rest of the status output will go + + state JSONDoc layerRoot = op.subDoc("$expires"); + // Set the version argument in the $expires operator object. + op.create("version") = readVer + 120 * CLIENT_KNOBS->CORE_VERSIONSPERSECOND; + + layerRoot.create("instances_running.$sum") = 1; + layerRoot.create("total_workers.$sum") = CLIENT_KNOBS->BACKUP_TASKS_PER_AGENT; + layerRoot.create("last_updated.$max") = now(); + + state JSONDoc o = layerRoot.subDoc("instances." + id); + + o.create("version") = FDB_VT_VERSION; + o.create("id") = id; + o.create("last_updated") = now(); + o.create("memory_usage") = (int64_t)getMemoryUsage(); + o.create("resident_size") = (int64_t)getResidentMemoryUsage(); + o.create("main_thread_cpu_seconds") = getProcessorTimeThread(); + o.create("process_cpu_seconds") = getProcessorTimeProcess(); + o.create("workers") = CLIENT_KNOBS->BACKUP_TASKS_PER_AGENT; + + if(exe == EXE_AGENT) { + static BlobStoreEndpoint::Stats last_stats; + static double last_ts = 0; + BlobStoreEndpoint::Stats current_stats = BlobStoreEndpoint::s_stats; + JSONDoc blobstats = o.create("blob_stats"); + blobstats.create("total") = current_stats.getJSON(); + BlobStoreEndpoint::Stats diff = current_stats - last_stats; + json_spirit::mObject diffObj = diff.getJSON(); + if(last_ts > 0) + diffObj["bytes_per_second"] = double(current_stats.bytes_sent - last_stats.bytes_sent) / (now() - last_ts); + blobstats.create("recent") = diffObj; + last_stats = current_stats; + last_ts = now(); + + JSONDoc totalBlobStats = layerRoot.subDoc("blob_recent_io"); + for(auto &p : diffObj) + totalBlobStats.create(p.first + ".$sum") = p.second; + + state FileBackupAgent fba; + state Standalone backupTagNames = wait( tr->getRange(fba.tagNames.range(), 10000)); + state std::vector> tagLastRestorableVersions; + state std::vector> tagStates; + state std::vector> tagContainers; + state std::vector> tagRangeBytes; + state std::vector> tagLogBytes; + state int i = 0; + + for(i = 0; i < backupTagNames.size(); i++) { + Standalone tagName = fba.tagNames.unpack(backupTagNames[i].key).getString(0); + UID tagUID = BinaryReader::fromStringRef(backupTagNames[i].value, Unversioned()); + tagLastRestorableVersions.push_back(fba.getLastRestorable(tr, tagName)); + tagStates.push_back(fba.getStateValue(tr, tagUID)); + tagContainers.push_back(fba.getLastBackupContainer(tr, tagUID)); + tagRangeBytes.push_back(fba.getRangeBytesWritten(tr, tagUID)); + tagLogBytes.push_back(fba.getLogBytesWritten(tr, tagUID)); + } + + Void _ = wait( waitForAll(tagLastRestorableVersions) && waitForAll(tagStates) && waitForAll(tagContainers) && waitForAll(tagRangeBytes) && waitForAll(tagLogBytes)); + + JSONDoc tagsRoot = layerRoot.subDoc("tags.$latest"); + layerRoot.create("tags.timestamp") = now(); + + for (int j = 0; j < backupTagNames.size(); j++) { + std::string tagName = fba.tagNames.unpack(backupTagNames[j].key).getString(0).toString(); + + Version last_restorable_version = tagLastRestorableVersions[j].get(); + double last_restorable_seconds_behind = ((double)readVer - last_restorable_version) / CLIENT_KNOBS->CORE_VERSIONSPERSECOND; + BackupAgentBase::enumState status = (BackupAgentBase::enumState)tagStates[j].get(); + const char *statusText = fba.getStateText(status); + + // The object for this backup tag inside this instance's subdocument + JSONDoc tagRoot = tagsRoot.subDoc(tagName); + tagRoot.create("current_container") = tagContainers[j].get(); + tagRoot.create("current_status") = statusText; + tagRoot.create("last_restorable_version") = tagLastRestorableVersions[j].get(); + tagRoot.create("last_restorable_seconds_behind") = last_restorable_seconds_behind; + tagRoot.create("running_backup") = (status == BackupAgentBase::STATE_DIFFERENTIAL || status == BackupAgentBase::STATE_BACKUP); + tagRoot.create("running_backup_is_restorable") = (status == BackupAgentBase::STATE_DIFFERENTIAL); + tagRoot.create("range_bytes_written") = tagRangeBytes[j].get(); + tagRoot.create("mutation_log_bytes_written") = tagLogBytes[j].get(); + } + } + else if(exe == EXE_DR_AGENT) { + state DatabaseBackupAgent dba; + state Reference tr2(new ReadYourWritesTransaction(dest)); + tr2->setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS); + tr2->setOption(FDBTransactionOptions::LOCK_AWARE); + state Standalone tagNames = wait(tr2->getRange(dba.tagNames.range(), 10000)); + state std::vector>> backupVersion; + state std::vector> backupStatus; + state std::vector> tagRangeBytesDR; + state std::vector> tagLogBytesDR; + + for(int i = 0; i < tagNames.size(); i++) { + backupVersion.push_back(tr2->get(tagNames[i].value.withPrefix(applyMutationsBeginRange.begin))); + UID tagUID = BinaryReader::fromStringRef(tagNames[i].value, Unversioned()); + backupStatus.push_back(dba.getStateValue(tr2, tagUID)); + tagRangeBytesDR.push_back(dba.getRangeBytesWritten(tr2, tagUID)); + tagLogBytesDR.push_back(dba.getLogBytesWritten(tr2, tagUID)); + } + + Void _ = wait(waitForAll(backupStatus) && waitForAll(backupVersion) && waitForAll(tagRangeBytesDR) && waitForAll(tagLogBytesDR)); + + JSONDoc tagsRoot = layerRoot.subDoc("tags.$latest"); + layerRoot.create("tags.timestamp") = now(); + + for (int i = 0; i < tagNames.size(); i++) { + std::string tagName = dba.sourceTagNames.unpack(tagNames[i].key).getString(0).toString(); + + BackupAgentBase::enumState status = (BackupAgentBase::enumState)backupStatus[i].get(); + + JSONDoc tagRoot = tagsRoot.create(tagName); + tagRoot.create("running_backup") = (status == BackupAgentBase::STATE_DIFFERENTIAL || status == BackupAgentBase::STATE_BACKUP); + tagRoot.create("running_backup_is_restorable") = (status == BackupAgentBase::STATE_DIFFERENTIAL); + tagRoot.create("range_bytes_written") = tagRangeBytesDR[i].get(); + tagRoot.create("mutation_log_bytes_written") = tagLogBytesDR[i].get(); + + if (backupVersion[i].get().present()) { + double seconds_behind = ((double)readVer - BinaryReader::fromStringRef(backupVersion[i].get().get(), Unversioned())) / CLIENT_KNOBS->CORE_VERSIONSPERSECOND; + tagRoot.create("seconds_behind") = seconds_behind; + //TraceEvent("BackupMetrics").detail("secondsBehind", seconds_behind); + } + + tagRoot.create("backup_state") = BackupAgentBase::getStateText(status); + } + } + + std::string json = json_spirit::write_string(layersRootValue); + return json; +} + +// Check for unparseable or expired statuses and delete them. +// First checks the first doc in the key range, and if it is valid, alive and not "me" then +// returns. Otherwise, checks the rest of the range as well. +ACTOR Future cleanupStatus(Reference tr, std::string rootKey, std::string name, std::string id, int limit = 1) { + state Standalone docs = wait(tr->getRange(KeyRangeRef(rootKey, strinc(rootKey)), limit, true)); + state bool readMore = false; + state int i; + for(i = 0; i < docs.size(); ++i) { + json_spirit::mValue docValue; + try { + json_spirit::read_string(docs[i].value.toString(), docValue); + JSONDoc doc(docValue); + // Update the reference version for $expires + JSONDoc::expires_reference_version = tr->getReadVersion().get(); + // Evaluate the operators in the document, which will reduce to nothing if it is expired. + doc.cleanOps(); + if(!doc.has(name + ".last_updated")) + throw Error(); + + // Alive and valid. + // If limit == 1 and id is present then read more + if(limit == 1 && doc.has(name + ".instances." + id)) + readMore = true; + } catch(Error &e) { + // If doc can't be parsed or isn't alive, delete it. + TraceEvent(SevWarn, "RemovedDeadBackupLayerStatus").detail("Key", printable(docs[i].key)); + tr->clear(docs[i].key); + // If limit is 1 then read more. + if(limit == 1) + readMore = true; + } + if(readMore) { + limit = 10000; + Standalone docs2 = wait(tr->getRange(KeyRangeRef(rootKey, strinc(rootKey)), limit, true)); + docs = std::move(docs2); + readMore = false; + } + } + + return Void(); +} + +// Get layer status document for just this layer +ACTOR Future getLayerStatus(Database src, std::string rootKey) { + state Transaction tr(src); + + loop { + try { + tr.setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS); + tr.setOption(FDBTransactionOptions::LOCK_AWARE); + state Standalone kvPairs = wait(tr.getRange(KeyRangeRef(rootKey, strinc(rootKey)), CLIENT_KNOBS->ROW_LIMIT_UNLIMITED)); + json_spirit::mObject statusDoc; + JSONDoc modifier(statusDoc); + for(auto &kv : kvPairs) { + json_spirit::mValue docValue; + json_spirit::read_string(kv.value.toString(), docValue); + modifier.absorb(docValue); + } + JSONDoc::expires_reference_version = (uint64_t)tr.getReadVersion().get(); + modifier.cleanOps(); + return statusDoc; + } + catch (Error& e) { + Void _ = wait(tr.onError(e)); + } + } +} + +// Read layer status for this layer and get the total count of agent processes (instances) then adjust the poll delay based on that and BACKUP_AGGREGATE_POLL_RATE +ACTOR Future updateAgentPollRate(Database src, std::string rootKey, std::string name, double *pollDelay) { + loop { + try { + json_spirit::mObject status = wait(getLayerStatus(src, rootKey)); + int64_t processes = 0; + // If instances count is present and greater than 0 then update pollDelay + if(JSONDoc(status).tryGet(name + ".instances_running", processes) && processes > 0) { + // The aggregate poll rate is the target poll rate for all agent processes in the cluster + // The poll rate (polls/sec) for a single processes is aggregate poll rate / processes, and pollDelay is the inverse of that + *pollDelay = (double)processes / CLIENT_KNOBS->BACKUP_AGGREGATE_POLL_RATE; + } + } catch(Error &e) { + TraceEvent(SevWarn, "BackupAgentPollRateUpdateError").error(e); + } + Void _ = wait(delay(CLIENT_KNOBS->BACKUP_AGGREGATE_POLL_RATE_UPDATE_INTERVAL)); + } +} + +ACTOR Future statusUpdateActor(Database statusUpdateDest, std::string name, enumProgramExe exe, double *pollDelay, Database taskDest = Database() ) { + state std::string id = g_nondeterministic_random->randomUniqueID().toString(); + state std::string metaKey = layerStatusMetaPrefixRange.begin.toString() + "json/" + name; + state std::string rootKey = backupStatusPrefixRange.begin.toString() + name + "/json"; + state std::string instanceKey = rootKey + "/" + "agent-" + id; + state Reference tr(new ReadYourWritesTransaction(statusUpdateDest)); + state Future pollRateUpdater; + + // Register the existence of this layer in the meta key space + loop { + try { + tr->setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS); + tr->setOption(FDBTransactionOptions::LOCK_AWARE); + tr->set(metaKey, rootKey); + Void _ = wait(tr->commit()); + break; + } + catch (Error& e) { + Void _ = wait(tr->onError(e)); + } + } + + // Write status periodically + loop { + tr->reset(); + try { + loop{ + try { + tr->setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS); + tr->setOption(FDBTransactionOptions::LOCK_AWARE); + state Future futureStatusDoc = getLayerStatus(tr, name, id, exe, taskDest); + Void _ = wait(cleanupStatus(tr, rootKey, name, id)); + std::string statusdoc = wait(futureStatusDoc); + tr->set(instanceKey, statusdoc); + Void _ = wait(tr->commit()); + break; + } + catch (Error& e) { + Void _ = wait(tr->onError(e)); + } + } + + Void _ = wait(delay(CLIENT_KNOBS->BACKUP_STATUS_DELAY * ( ( 1.0 - CLIENT_KNOBS->BACKUP_STATUS_JITTER ) + 2 * g_random->random01() * CLIENT_KNOBS->BACKUP_STATUS_JITTER ))); + + // Now that status was written at least once by this process (and hopefully others), start the poll rate control updater if it wasn't started yet + if(!pollRateUpdater.isValid() && pollDelay != nullptr) + pollRateUpdater = updateAgentPollRate(statusUpdateDest, rootKey, name, pollDelay); + } + catch (Error& e) { + TraceEvent(SevWarnAlways, "UnableToWriteStatus").error(e); + Void _ = wait(delay(10.0)); + } + } +} + +ACTOR Future runDBAgent(Database src, Database dest) { + state double pollDelay = 1.0 / CLIENT_KNOBS->BACKUP_AGGREGATE_POLL_RATE; + state Future status = statusUpdateActor(src, "dr_backup", EXE_DR_AGENT, &pollDelay, dest); + state Future status_other = statusUpdateActor(dest, "dr_backup_dest", EXE_DR_AGENT, &pollDelay, dest); + + state DatabaseBackupAgent backupAgent(src); + + loop { + try { + state Void run = wait(backupAgent.run(dest, &pollDelay, CLIENT_KNOBS->BACKUP_TASKS_PER_AGENT)); + break; + } + catch (Error& e) { + if (e.code() == error_code_operation_cancelled) + throw; + + TraceEvent(SevError, "DA_runAgent").error(e); + fprintf(stderr, "ERROR: DR agent encountered fatal error `%s'\n", e.what()); + + Void _ = wait( delay(FLOW_KNOBS->PREVENT_FAST_SPIN_DELAY) ); + } + } + + return Void(); +} + +ACTOR Future runAgent(Database db) { + state double pollDelay = 1.0 / CLIENT_KNOBS->BACKUP_AGGREGATE_POLL_RATE; + state Future status = statusUpdateActor(db, "backup", EXE_AGENT, &pollDelay); + + state FileBackupAgent backupAgent; + + loop { + try { + state Void run = wait(backupAgent.run(db, &pollDelay, CLIENT_KNOBS->BACKUP_TASKS_PER_AGENT)); + break; + } + catch (Error& e) { + if (e.code() == error_code_operation_cancelled) + throw; + + TraceEvent(SevError, "BA_runAgent").error(e); + fprintf(stderr, "ERROR: backup agent encountered fatal error `%s'\n", e.what()); + + Void _ = wait( delay(FLOW_KNOBS->PREVENT_FAST_SPIN_DELAY) ); + } + } + + return Void(); +} + +ACTOR Future submitDBBackup(Database src, Database dest, Standalone> backupRanges, std::string tagName) { + try + { + state DatabaseBackupAgent backupAgent(src); + + // Backup everything, if no ranges were specified + if (backupRanges.size() == 0) { + backupRanges.push_back_deep(backupRanges.arena(), normalKeys); + } + + + Void _ = wait(backupAgent.submitBackup(dest, KeyRef(tagName), backupRanges, false, StringRef(), StringRef(), true)); + + // Check if a backup agent is running + bool agentRunning = wait(backupAgent.checkActive(dest)); + + if (!agentRunning) { + printf("The DR on tag `%s' was successfully submitted but no DR agents are responding.\n", printable(StringRef(tagName)).c_str()); + + // Throw an error that will not display any additional information + throw actor_cancelled(); + } + else { + printf("The DR on tag `%s' was successfully submitted.\n", printable(StringRef(tagName)).c_str()); + } + } + + catch (Error& e) { + if(e.code() == error_code_actor_cancelled) + throw; + switch (e.code()) + { + case error_code_backup_error: + fprintf(stderr, "ERROR: An error was encountered during submission\n"); + break; + case error_code_backup_duplicate: + fprintf(stderr, "ERROR: A DR is already running on tag `%s'\n", printable(StringRef(tagName)).c_str()); + break; + default: + fprintf(stderr, "ERROR: %s\n", e.what()); + break; + } + + throw backup_error(); + } + + return Void(); +} + +ACTOR Future submitBackup(Database db, std::string destinationDir, Standalone> backupRanges, std::string tagName, bool dryRun, bool waitForCompletion, bool stopWhenDone) { + try + { + state FileBackupAgent backupAgent; + + // Backup everything, if no ranges were specified + if (backupRanges.size() == 0) { + backupRanges.push_back_deep(backupRanges.arena(), normalKeys); + } + + if (dryRun) { + state UID logUid = wait(backupAgent.getLogUid(db, StringRef(tagName))); + state int backupStatus = wait(backupAgent.getStateValue(db, logUid)); + + // Throw error if a backup is currently running until we support parallel backups + if (BackupAgentBase::isRunnable((BackupAgentBase::enumState)backupStatus)) { + throw backup_duplicate(); + } + + if (waitForCompletion) { + printf("Submitted and now waiting for the backup on tag `%s' to complete. (DRY RUN)\n", printable(StringRef(tagName)).c_str()); + } + + else { + // Check if a backup agent is running + bool agentRunning = wait(backupAgent.checkActive(db)); + + if (!agentRunning) { + printf("The backup on tag `%s' was successfully submitted but no backup agents are responding. (DRY RUN)\n", printable(StringRef(tagName)).c_str()); + + // Throw an error that will not display any additional information + throw actor_cancelled(); + } + else { + printf("The backup on tag `%s' was successfully submitted. (DRY RUN)\n", printable(StringRef(tagName)).c_str()); + } + } + } + + else { + Void _ = wait(backupAgent.submitBackup(db, KeyRef(destinationDir), KeyRef(tagName), backupRanges, stopWhenDone)); + + // Wait for the backup to complete, if requested + if (waitForCompletion) { + printf("Submitted and now waiting for the backup on tag `%s' to complete.\n", printable(StringRef(tagName)).c_str()); + int _ = wait(backupAgent.waitBackup(db, StringRef(tagName))); + } + else { + // Check if a backup agent is running + bool agentRunning = wait(backupAgent.checkActive(db)); + + if (!agentRunning) { + printf("The backup on tag `%s' was successfully submitted but no backup agents are responding.\n", printable(StringRef(tagName)).c_str()); + + // Throw an error that will not display any additional information + throw actor_cancelled(); + } + else { + printf("The backup on tag `%s' was successfully submitted.\n", printable(StringRef(tagName)).c_str()); + } + } + } + } + catch (Error& e) { + if(e.code() == error_code_actor_cancelled) + throw; + switch (e.code()) + { + case error_code_backup_error: + fprintf(stderr, "ERROR: An error was encountered during submission\n"); + break; + case error_code_backup_duplicate: + fprintf(stderr, "ERROR: A backup is already running on tag `%s'\n", printable(StringRef(tagName)).c_str()); + break; + default: + fprintf(stderr, "ERROR: %s\n", e.what()); + break; + } + + throw backup_error(); + } + + return Void(); +} + +ACTOR Future switchDBBackup(Database src, Database dest, Standalone> backupRanges, std::string tagName) { + try + { + state DatabaseBackupAgent backupAgent(src); + + // Backup everything, if no ranges were specified + if (backupRanges.size() == 0) { + backupRanges.push_back_deep(backupRanges.arena(), normalKeys); + } + + + Void _ = wait(backupAgent.atomicSwitchover(dest, KeyRef(tagName), backupRanges, StringRef(), StringRef())); + printf("The DR on tag `%s' was successfully switched.\n", printable(StringRef(tagName)).c_str()); + } + + catch (Error& e) { + if(e.code() == error_code_actor_cancelled) + throw; + switch (e.code()) + { + case error_code_backup_error: + fprintf(stderr, "ERROR: An error was encountered during submission\n"); + break; + case error_code_backup_duplicate: + fprintf(stderr, "ERROR: A DR is already running on tag `%s'\n", printable(StringRef(tagName)).c_str()); + break; + default: + fprintf(stderr, "ERROR: %s\n", e.what()); + break; + } + + throw backup_error(); + } + + return Void(); +} + +ACTOR Future statusDBBackup(Database src, Database dest, std::string tagName, int errorLimit) { + try + { + state DatabaseBackupAgent backupAgent(src); + + std::string statusText = wait(backupAgent.getStatus(dest, errorLimit, StringRef(tagName))); + printf("%s\n", statusText.c_str()); + } + catch (Error& e) { + if(e.code() == error_code_actor_cancelled) + throw; + fprintf(stderr, "ERROR: %s\n", e.what()); + throw; + } + + return Void(); +} + +ACTOR Future statusBackup(Database db, std::string tagName, int errorLimit) { + try + { + state FileBackupAgent backupAgent; + + std::string statusText = wait(backupAgent.getStatus(db, errorLimit, StringRef(tagName))); + printf("%s\n", statusText.c_str()); + } + catch (Error& e) { + if(e.code() == error_code_actor_cancelled) + throw; + fprintf(stderr, "ERROR: %s\n", e.what()); + throw; + } + + return Void(); +} + +ACTOR Future abortDBBackup(Database src, Database dest, std::string tagName, bool partial) { + try + { + state DatabaseBackupAgent backupAgent(src); + + Void _ = wait(backupAgent.abortBackup(dest, Key(tagName), partial)); + Void _ = wait(backupAgent.unlockBackup(dest, Key(tagName))); + + printf("The DR on tag `%s' was successfully aborted.\n", printable(StringRef(tagName)).c_str()); + } + catch (Error& e) { + if(e.code() == error_code_actor_cancelled) + throw; + switch (e.code()) + { + case error_code_backup_error: + fprintf(stderr, "ERROR: An error was encountered during submission\n"); + break; + case error_code_backup_unneeded: + fprintf(stderr, "ERROR: A DR was not running on tag `%s'\n", printable(StringRef(tagName)).c_str()); + break; + default: + fprintf(stderr, "ERROR: %s\n", e.what()); + break; + } + throw; + } + + return Void(); +} + +ACTOR Future abortBackup(Database db, std::string tagName) { + try + { + state FileBackupAgent backupAgent; + + Void _ = wait(backupAgent.abortBackup(db, Key(tagName))); + + printf("The backup on tag `%s' was successfully aborted.\n", printable(StringRef(tagName)).c_str()); + } + catch (Error& e) { + if(e.code() == error_code_actor_cancelled) + throw; + switch (e.code()) + { + case error_code_backup_error: + fprintf(stderr, "ERROR: An error was encountered during submission\n"); + break; + case error_code_backup_unneeded: + fprintf(stderr, "ERROR: A backup was not running on tag `%s'\n", printable(StringRef(tagName)).c_str()); + break; + default: + fprintf(stderr, "ERROR: %s\n", e.what()); + break; + } + throw; + } + + return Void(); +} + +ACTOR Future waitBackup(Database db, std::string tagName, bool stopWhenDone) { + try + { + state FileBackupAgent backupAgent; + + int status = wait(backupAgent.waitBackup(db, StringRef(tagName), stopWhenDone)); + + printf("The backup on tag `%s' %s.\n", printable(StringRef(tagName)).c_str(), + BackupAgentBase::getStateText((BackupAgentBase::enumState) status)); + } + catch (Error& e) { + if(e.code() == error_code_actor_cancelled) + throw; + fprintf(stderr, "ERROR: %s\n", e.what()); + throw; + } + + return Void(); +} + +ACTOR Future discontinueBackup(Database db, std::string tagName, bool waitForCompletion) { + try + { + state FileBackupAgent backupAgent; + + Void _ = wait(backupAgent.discontinueBackup(db, StringRef(tagName))); + + // Wait for the backup to complete, if requested + if (waitForCompletion) { + printf("Discontinued and now waiting for the backup on tag `%s' to complete.\n", printable(StringRef(tagName)).c_str()); + int _ = wait(backupAgent.waitBackup(db, StringRef(tagName))); + } + else { + printf("The backup on tag `%s' was successfully discontinued.\n", printable(StringRef(tagName)).c_str()); + } + + } + catch (Error& e) { + if(e.code() == error_code_actor_cancelled) + throw; + switch (e.code()) + { + case error_code_backup_error: + fprintf(stderr, "ERROR: An encounter was error during submission\n"); + break; + case error_code_backup_unneeded: + fprintf(stderr, "ERROR: A backup in not running on tag `%s'\n", printable(StringRef(tagName)).c_str()); + break; + case error_code_backup_duplicate: + fprintf(stderr, "ERROR: The backup on tag `%s' is already discontinued\n", printable(StringRef(tagName)).c_str()); + break; + default: + fprintf(stderr, "ERROR: %s\n", e.what()); + break; + } + throw; + } + + return Void(); +} + +ACTOR Future runRestore(Database db, std::string tagName, std::string container, Standalone> ranges, Version dbVersion, bool performRestore, bool verbose, bool waitForDone, std::string addPrefix, std::string removePrefix) { + try + { + state FileBackupAgent backupAgent; + state int64_t restoreVersion = -1; + + if(ranges.size() > 1) { + fprintf(stderr, "Currently only a single restore range is supported!\n"); + throw restore_error(); + } + + KeyRange range = (ranges.size() == 0) ? normalKeys : ranges.front(); + + if (performRestore) { + Version _restoreVersion = wait(backupAgent.restore(db, KeyRef(tagName), KeyRef(container), waitForDone, dbVersion, verbose, range, KeyRef(addPrefix), KeyRef(removePrefix))); + restoreVersion = _restoreVersion; + } + else { + state Version defaultRestoreVersion = -1; + + // Get the folder information + std::string info = wait(FileBackupAgent::getBackupInfo(container, &defaultRestoreVersion)); + + restoreVersion = (int64_t) (dbVersion > 0) ? dbVersion : defaultRestoreVersion; + + // Display the restore information, if requested + if (verbose) { + printf("[DRY RUN] Restoring backup to version: %lld\n", (long long) restoreVersion); + printf("%s\n", info.c_str()); + } + } + + if(waitForDone && verbose) { + // If restore completed then report version restored + printf("Restored to version %lld%s\n", (long long) restoreVersion, (performRestore) ? "" : " (DRY RUN)"); + } + } + catch (Error& e) { + if(e.code() == error_code_actor_cancelled) + throw; + fprintf(stderr, "ERROR: %s\n", e.what()); + throw; + } + + return Void(); +} + +ACTOR Future doBlobDelete(std::string url) { + state std::string error; + try { + state Reference c = IBackupContainer::openContainer(url, &error); + } catch(Error &e) { + fprintf(stderr, "ERROR: Invalid blobstore URL: %s (%s) Format is: %s\n", url.c_str(), error.c_str(), BackupContainerBlobStore::getURLFormat().c_str()); + return FDB_EXIT_ERROR; + } + + state int pNumDeleted = 0; + state Future f = ((BackupContainerBlobStore *)c.getPtr())->deleteContainer(&pNumDeleted); + loop { + choose { + when(Void _ = wait(f)) { + break; + } + when(Void _ = wait(delay(3.0))) { + printf("%d objects deleted so far...\n", pNumDeleted); + } + } + } + printf("Done. %d objects deleted.\n", pNumDeleted); + return FDB_EXIT_SUCCESS; +} + +ACTOR Future doBlobInfo(std::string url, bool showObjects = false) { + state std::string error; + try { + state Reference c = IBackupContainer::openContainer(url, &error); + } catch(Error &e) { + fprintf(stderr, "ERROR: Invalid blobstore URL: %s (%s) Format is: %s\n", url.c_str(), error.c_str(), BackupContainerBlobStore::getURLFormat().c_str()); + return FDB_EXIT_ERROR; + } + + state BackupContainerBlobStore *bc = (BackupContainerBlobStore *)c.getPtr(); + state PromiseStream resultsStream; + state Future done = bc->listFilesStream(resultsStream); + state int64_t total_bytes = 0; + state int64_t total_objects = 0; + + try { + loop { + choose { + when(Void _ = wait(done)) { + break; + } + when(BlobStoreEndpoint::ObjectInfo info = waitNext(resultsStream.getFuture())) { + ++total_objects; + total_bytes += info.size; + if(showObjects) + printf("\t%lld\t%s/%s\n", info.size, info.bucket.c_str(), info.name.c_str()); + } + } + } + } catch(Error &e) { + printf("ERROR (%s) on %s\n", e.what(), url.c_str()); + return FDB_EXIT_ERROR; + } + + printf("%lld\t%lld\t%s\n", total_bytes, total_objects, url.c_str()); + + return FDB_EXIT_SUCCESS; +} + +ACTOR Future doBlobList(std::string url, bool deep = false) { + state Reference bse; + state std::string error; + try { + bse = BlobStoreEndpoint::fromString(url, NULL, &error); + } catch(Error &e) { + fprintf(stderr, "ERROR: Invalid blobstore endpoint: %s (%s). Must look like this: %s\n", url.c_str(), error.c_str(), BlobStoreEndpoint::getURLFormat().c_str()); + return FDB_EXIT_ERROR; + } + + state std::vector results = wait(BackupContainerBlobStore::listBackupContainers(bse)); + state std::vector::iterator i; + state int status = FDB_EXIT_SUCCESS; + for(i = results.begin(); i != results.end(); ++i) { + std::string url = bse->getResourceURL(*i); + if(!deep) + printf("%s\n", url.c_str()); + else { + int r = wait(doBlobInfo(url)); + if(status == FDB_EXIT_SUCCESS) + status = r; + } + } + + return status; +} + +ACTOR Future doBlobCommand(std::vector args) { + if(args.size() < 2) { + printBlobManagerUsage(); + return FDB_EXIT_ERROR; + } + + state std::string cmd = args[0]; + + if(cmd == "-h" || cmd == "--help") { + printBlobManagerUsage(); + return FDB_EXIT_ERROR; + } + + try { + if(cmd == "list") { + int r = wait(doBlobList(args[1])); + return r; + } + if(cmd == "listinfo") { + printf("BYTES\tOBJECTS\tURL\n"); + int r = wait(doBlobList(args[1], true)); + return r; + } + else if(cmd == "delete") { + int r = wait(doBlobDelete(args[1])); + return r; + } + else if(cmd == "info") { + printf("BYTES\tOBJECTS\tURL\n"); + int r = wait(doBlobInfo(args[1])); + return r; + } + else if(cmd == "dump") { + int r = wait(doBlobInfo(args[1], true)); + return r; + } + else { + printf("ERROR: Unknown command: '%s'\n", cmd.c_str()); + printBlobManagerUsage(); + return FDB_EXIT_ERROR; + } + } catch(Error &e) { + fprintf(stderr, "ERROR: Blob command '%s' failed: %s\n", cmd.c_str(), e.what()); + throw; + } +} + +static std::vector> parseLine(std::string &line, bool& err, bool& partial) +{ + err = false; + partial = false; + + bool quoted = false; + std::vector buf; + std::vector> ret; + + size_t i = line.find_first_not_of(' '); + size_t offset = i; + + bool forcetoken = false; + + while (i <= line.length()) { + switch (line[i]) { + case ';': + if (!quoted) { + if (i > offset) + buf.push_back(StringRef((uint8_t*)(line.data() + offset), i - offset)); + ret.push_back(std::move(buf)); + offset = i = line.find_first_not_of(' ', i + 1); + } + else + i++; + break; + case '"': + quoted = !quoted; + line.erase(i, 1); + if (quoted) + forcetoken = true; + break; + case ' ': + if (!quoted) { + buf.push_back(StringRef((uint8_t *)(line.data() + offset), + i - offset)); + offset = i = line.find_first_not_of(' ', i); + forcetoken = false; + } + else + i++; + break; + case '\\': + if (i + 2 > line.length()) { + err = true; + ret.push_back(std::move(buf)); + return ret; + } + switch (line[i + 1]) { + char ent, save; + case '"': + case '\\': + case ' ': + case ';': + line.erase(i, 1); + break; + case 'x': + if (i + 4 > line.length()) { + err = true; + ret.push_back(std::move(buf)); + return ret; + } + char *pEnd; + save = line[i + 4]; + line[i + 4] = 0; + ent = char(strtoul(line.data() + i + 2, &pEnd, 16)); + if (*pEnd) { + err = true; + ret.push_back(std::move(buf)); + return ret; + } + line[i + 4] = save; + line.replace(i, 4, 1, ent); + break; + default: + err = true; + ret.push_back(std::move(buf)); + return ret; + } + default: + i++; + } + } + + i -= 1; + if (i > offset || forcetoken) + buf.push_back(StringRef((uint8_t*)(line.data() + offset), i - offset)); + + ret.push_back(std::move(buf)); + + if (quoted) + partial = true; + + return ret; +} + +static void addKeyRange(std::string optionValue, Standalone>& keyRanges) +{ + bool err = false, partial = false; + int tokenArray = 0, tokenIndex = 0; + + auto parsed = parseLine(optionValue, err, partial); + + for (auto tokens : parsed) + { + tokenArray++; + tokenIndex = 0; + + /* + for (auto token : tokens) + { + tokenIndex++; + + printf("%4d token #%2d: %s\n", tokenArray, tokenIndex, printable(token).c_str()); + } + */ + + // Process the keys + // [end] + switch (tokens.size()) + { + // empty + case 0: + break; + + // single key range + case 1: + keyRanges.push_back_deep(keyRanges.arena(), KeyRangeRef(tokens.at(0), strinc(tokens.at(0)))); + break; + + // full key range + case 2: + try { + keyRanges.push_back_deep(keyRanges.arena(), KeyRangeRef(tokens.at(0), tokens.at(1))); + } + catch (Error& e) { + fprintf(stderr, "ERROR: Invalid key range `%s %s' reported error %s\n", + tokens.at(0).toString().c_str(), tokens.at(1).toString().c_str(), e.what()); + throw invalid_option_value(); + } + break; + + // Too many keys + default: + fprintf(stderr, "ERROR: Invalid key range identified with %ld keys", tokens.size()); + throw invalid_option_value(); + break; + } + } + + return; +} + +#ifdef ALLOC_INSTRUMENTATION +extern uint8_t *g_extra_memory; +#endif + +int main(int argc, char* argv[]) { + platformInit(); + + int status = FDB_EXIT_SUCCESS; + + try { +#ifdef ALLOC_INSTRUMENTATION + g_extra_memory = new uint8_t[1000000]; +#endif + registerCrashHandler(); + + // Set default of line buffering standard out and error + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + + enumProgramExe programExe = getProgramType(argv[0]); + enumBackupType backupType = BACKUP_UNDEFINED; + enumRestoreType restoreType = RESTORE_UNKNOWN; + enumDBType dbType = DB_UNDEFINED; + + CSimpleOpt* args = NULL; + + switch (programExe) + { + case EXE_AGENT: + args = new CSimpleOpt(argc, argv, g_rgAgentOptions, SO_O_EXACT); + break; + case EXE_DR_AGENT: + args = new CSimpleOpt(argc, argv, g_rgDBAgentOptions, SO_O_EXACT); + break; + case EXE_BACKUP: + // Display backup help, if no arguments + if (argc < 2) { + printBackupUsage(false); + return FDB_EXIT_ERROR; + } + else { + // Get the backup type + backupType = getBackupType(argv[1]); + + // Create the appropriate simple opt + switch (backupType) + { + case BACKUP_START: + args = new CSimpleOpt(argc-1, &argv[1], g_rgBackupStartOptions, SO_O_EXACT); + break; + case BACKUP_STATUS: + args = new CSimpleOpt(argc - 1, &argv[1], g_rgBackupStatusOptions, SO_O_EXACT); + break; + case BACKUP_ABORT: + args = new CSimpleOpt(argc - 1, &argv[1], g_rgBackupAbortOptions, SO_O_EXACT); + break; + case BACKUP_WAIT: + args = new CSimpleOpt(argc - 1, &argv[1], g_rgBackupWaitOptions, SO_O_EXACT); + break; + case BACKUP_DISCONTINUE: + args = new CSimpleOpt(argc - 1, &argv[1], g_rgBackupDiscontinueOptions, SO_O_EXACT); + break; + case BACKUP_UNDEFINED: + default: + // Display help, if requested + if ((strcmp(argv[1], "-h") == 0) || + (strcmp(argv[1], "--help") == 0) ) + { + printBackupUsage(false); + return FDB_EXIT_ERROR; + } + else { + fprintf(stderr, "ERROR: Unsupported backup action %s\n", argv[1]); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + break; + } + } + break; + case EXE_DB_BACKUP: + // Display backup help, if no arguments + if (argc < 2) { + printDBBackupUsage(false); + return FDB_EXIT_ERROR; + } + else { + // Get the backup type + dbType = getDBType(argv[1]); + + // Create the appropriate simple opt + switch (dbType) + { + case DB_START: + args = new CSimpleOpt(argc-1, &argv[1], g_rgDBStartOptions, SO_O_EXACT); + break; + case DB_STATUS: + args = new CSimpleOpt(argc - 1, &argv[1], g_rgDBStatusOptions, SO_O_EXACT); + break; + case DB_SWITCH: + args = new CSimpleOpt(argc - 1, &argv[1], g_rgDBSwitchOptions, SO_O_EXACT); + break; + case DB_ABORT: + args = new CSimpleOpt(argc - 1, &argv[1], g_rgDBAbortOptions, SO_O_EXACT); + break; + case DB_UNDEFINED: + default: + // Display help, if requested + if ((strcmp(argv[1], "-h") == 0) || + (strcmp(argv[1], "--help") == 0) ) + { + printDBBackupUsage(false); + return FDB_EXIT_ERROR; + } + else { + fprintf(stderr, "ERROR: Unsupported dr action %s %d\n", argv[1], dbType); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + break; + } + } + break; + case EXE_BLOBMANAGER: + args = new CSimpleOpt(argc, argv, g_rgBlobOptions, SO_O_NOERR); + break; + case EXE_RESTORE: + if (argc < 2) { + printRestoreUsage(false); + return FDB_EXIT_ERROR; + } + // Get the restore operation type + restoreType = getRestoreType(argv[1]); + if(restoreType == RESTORE_UNKNOWN) { + // Display help, if requested + if ((strcmp(argv[1], "-h") == 0) || + (strcmp(argv[1], "--help") == 0) ) + { + printRestoreUsage(false); + return FDB_EXIT_ERROR; + } + else { + fprintf(stderr, "ERROR: Unsupported restore command: '%s'\n", argv[1]); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + } + args = new CSimpleOpt(argc - 1, argv + 1, g_rgRestoreOptions, SO_O_EXACT); + break; + case EXE_UNDEFINED: + default: + fprintf(stderr, "FoundationDB " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + fprintf(stderr, "ERROR: Unable to determine program type based on executable `%s'\n", argv[0]); + return FDB_EXIT_ERROR; + break; + } + + std::string destinationContainer; + std::string clusterFile; + std::string sourceClusterFile; + std::vector> knobs; + std::string tagName = BackupAgentBase::getDefaultTag().toString(); + bool tagProvided = false; + std::string restoreContainer; + std::string addPrefix; + std::string removePrefix; + Standalone> backupKeys; + int maxErrors = 20; + Version dbVersion = 0; + bool waitForDone = false; + bool stopWhenDone = true; + bool forceAction = false; + bool trace = false; + bool quietDisplay = false; + bool dryRun = false; + std::string traceDir = ""; + std::string traceLogGroup; + ESOError lastError; + bool partial = true; + + std::vector blobArgs; + + if( argc == 1 ) { + printUsage(programExe, false); + return FDB_EXIT_ERROR; + } + + #ifdef _WIN32 + // Windows needs a gentle nudge to format floats correctly + //_set_output_format(_TWO_DIGIT_EXPONENT); + #endif + + while (args->Next()) { + lastError = args->LastError(); + + switch (lastError) + { + case SO_SUCCESS: + break; + + case SO_ARG_INVALID_DATA: + fprintf(stderr, "ERROR: invalid argument to option `%s'\n", args->OptionText()); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + break; + + case SO_ARG_INVALID: + fprintf(stderr, "ERROR: argument given for option `%s'\n", args->OptionText()); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + break; + + case SO_ARG_MISSING: + fprintf(stderr, "ERROR: missing argument for option `%s'\n", args->OptionText()); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + + case SO_OPT_INVALID: + fprintf(stderr, "ERROR: unknown option `%s'\n", args->OptionText()); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + break; + + default: + fprintf(stderr, "ERROR: argument given for option `%s'\n", args->OptionText()); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + break; + } + + switch (args->OptionId()) { + case OPT_HELP: + printUsage(programExe, false); + return FDB_EXIT_SUCCESS; + break; + case OPT_DEVHELP: + printUsage(programExe, true); + return FDB_EXIT_SUCCESS; + break; + case OPT_VERSION: + printVersion(); + return FDB_EXIT_SUCCESS; + break; + case OPT_NOBUFSTDOUT: + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + break; + case OPT_BUFSTDOUTERR: + setvbuf(stdout, NULL, _IOFBF, BUFSIZ); + setvbuf(stderr, NULL, _IOFBF, BUFSIZ); + break; + case OPT_QUIET: + quietDisplay = true; + break; + case OPT_DRYRUN: + dryRun = true; + break; + case OPT_FORCE: + forceAction = true; + break; + case OPT_TRACE: + trace = true; + break; + case OPT_TRACE_DIR: + trace = true; + traceDir = args->OptionArg(); + break; + case OPT_TRACE_LOG_GROUP: + traceLogGroup = args->OptionArg(); + break; + case OPT_CLUSTERFILE: + clusterFile = args->OptionArg(); + break; + case OPT_DEST_CLUSTER: + clusterFile = args->OptionArg(); + break; + case OPT_SOURCE_CLUSTER: + sourceClusterFile = args->OptionArg(); + break; + case OPT_CLEANUP: + partial = false; + break; + case OPT_KNOB: { + std::string syn = args->OptionSyntax(); + if (!StringRef(syn).startsWith(LiteralStringRef("--knob_"))) { + fprintf(stderr, "ERROR: unable to parse knob option '%s'\n", syn.c_str()); + return FDB_EXIT_ERROR; + } + syn = syn.substr(7); + knobs.push_back( std::make_pair( syn, args->OptionArg() ) ); + break; + } + case OPT_BACKUPKEYS: + try { + addKeyRange(args->OptionArg(), backupKeys); + } + catch (Error &) { + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + break; + case OPT_DESTCONTAINER: + destinationContainer = args->OptionArg(); + // If the url starts with '/' then prepend "file://" for backwards compatibility + if(StringRef(destinationContainer).startsWith(LiteralStringRef("/"))) + destinationContainer = std::string("file://") + destinationContainer; + break; + case OPT_WAITFORDONE: + waitForDone = true; + break; + case OPT_NOSTOPWHENDONE: + stopWhenDone = false; + break; + case OPT_RESTORECONTAINER: + restoreContainer = args->OptionArg(); + // If the url starts with '/' then prepend "file://" for backwards compatibility + if(StringRef(restoreContainer).startsWith(LiteralStringRef("/"))) + restoreContainer = std::string("file://") + restoreContainer; + break; + case OPT_PREFIX_ADD: + addPrefix = args->OptionArg(); + break; + case OPT_PREFIX_REMOVE: + removePrefix = args->OptionArg(); + break; + case OPT_ERRORLIMIT: { + const char* a = args->OptionArg(); + if (!sscanf(a, "%d", &maxErrors)) { + fprintf(stderr, "ERROR: Could not parse max number of errors `%s'\n", a); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + break; + } + case OPT_DBVERSION: { + const char* a = args->OptionArg(); + long long dbVersionValue = 0; + if (!sscanf(a, "%lld", &dbVersionValue)) { + fprintf(stderr, "ERROR: Could not parse database version `%s'\n", a); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + dbVersion = dbVersionValue; + break; + } + #ifdef _WIN32 + case OPT_PARENTPID: { + auto pid_str = args->OptionArg(); + int parent_pid = atoi(pid_str); + auto pHandle = OpenProcess( SYNCHRONIZE, FALSE, parent_pid ); + if( !pHandle ) { + TraceEvent("ParentProcessOpenError").GetLastError(); + fprintf(stderr, "Could not open parent process at pid %d (error %d)", parent_pid, GetLastError()); + throw platform_error(); + } + startThread(&parentWatcher, pHandle); + break; + } + #endif + case OPT_TAGNAME: + tagName = args->OptionArg(); + tagProvided = true; + break; + case OPT_CRASHONERROR: + g_crashOnError = true; + break; + } + } + + // Process the extra arguments + for (int argLoop = 0; argLoop < args->FileCount(); argLoop++) + { + switch (programExe) + { + case EXE_AGENT: + fprintf(stderr, "ERROR: Backup Agent does not support argument value `%s'\n", args->File(argLoop)); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + break; + + // Add the backup key range + case EXE_BACKUP: + // Error, if the keys option was not specified + if (backupKeys.size() == 0) { + fprintf(stderr, "ERROR: Unknown backup option value `%s'\n", args->File(argLoop)); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + // Otherwise, assume the item is a key range + else { + try { + addKeyRange(args->File(argLoop), backupKeys); + } + catch (Error& e) { + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + } + break; + + case EXE_RESTORE: + fprintf(stderr, "ERROR: FDB Restore does not support argument value `%s'\n", args->File(argLoop)); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + break; + + case EXE_DR_AGENT: + fprintf(stderr, "ERROR: DR Agent does not support argument value `%s'\n", args->File(argLoop)); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + break; + + case EXE_DB_BACKUP: + // Error, if the keys option was not specified + if (backupKeys.size() == 0) { + fprintf(stderr, "ERROR: Unknown DR option value `%s'\n", args->File(argLoop)); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + // Otherwise, assume the item is a key range + else { + try { + addKeyRange(args->File(argLoop), backupKeys); + } + catch (Error& e) { + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + } + break; + + case EXE_BLOBMANAGER: + blobArgs.push_back(args->File(argLoop)); + break; + + case EXE_UNDEFINED: + default: + return FDB_EXIT_ERROR; + } + } + + // Delete the simple option object, if defined + if (args) + { + delete args; + args = NULL; + } + + std::string commandLine; + for(int a=0; asetKnob( k->first, k->second )) { + fprintf(stderr, "Unrecognized knob option '%s'\n", k->first.c_str()); + return FDB_EXIT_ERROR; + } + } catch (Error& e) { + if (e.code() == error_code_invalid_option_value) { + fprintf(stderr, "Invalid value '%s' for option '%s'\n", k->second.c_str(), k->first.c_str()); + return FDB_EXIT_ERROR; + } + throw; + } + } + + if (trace) { + if(!traceLogGroup.empty()) + setNetworkOption(FDBNetworkOptions::TRACE_LOG_GROUP, StringRef(traceLogGroup)); + + if (traceDir.empty()) + setNetworkOption(FDBNetworkOptions::TRACE_ENABLE); + else + setNetworkOption(FDBNetworkOptions::TRACE_ENABLE, StringRef(traceDir)); + + setNetworkOption(FDBNetworkOptions::ENABLE_SLOW_TASK_PROFILING); + } + setNetworkOption(FDBNetworkOptions::DISABLE_CLIENT_STATISTICS_LOGGING); + Error::init(); + std::set_new_handler( &platform::outOfMemory ); + + int total = 0; + for(auto i = Error::errorCounts().begin(); i != Error::errorCounts().end(); ++i) + total += i->second; + if (total) + printf("%d errors:\n", total); + for(auto i = Error::errorCounts().begin(); i != Error::errorCounts().end(); ++i) + if (i->second > 0) + printf(" %d: %d %s\n", i->second, i->first, Error::fromCode(i->first).what()); + + + Reference cluster; + Reference ccf; + Database db; + Reference source_cluster; + Reference source_ccf; + Database source_db; + const KeyRef databaseKey = LiteralStringRef("DB"); + FileBackupAgent ba; + Key tag; + Future> f; + Future> fstatus; + + try { + setupNetwork(0, true); + } + catch (Error& e) { + fprintf(stderr, "ERROR: %s\n", e.what()); + return 1; + } + + // Ordinarily, this is done when the network is run. However, network thread should be set before TraceEvents are logged. This thread will eventually run the network, so call it now. + TraceEvent::setNetworkThread(); + + // Blob Manager mode does not require connecting to any cluster + if(programExe != EXE_BLOBMANAGER) { + auto resolvedClusterFile = ClusterConnectionFile::lookupClusterFileName(clusterFile); + try { + ccf = Reference(new ClusterConnectionFile(resolvedClusterFile.first)); + } + catch (Error& e) { + fprintf(stderr, "%s\n", ClusterConnectionFile::getErrorString(resolvedClusterFile, e).c_str()); + return 1; + } + + try { + cluster = Cluster::createCluster(ccf, -1); + } + catch (Error& e) { + fprintf(stderr, "ERROR: %s\n", e.what()); + fprintf(stderr, "ERROR: Unable to connect to cluster from `%s'\n", ccf->getFilename().c_str()); + return 1; + } + + TraceEvent("ProgramStart") + .detail("SourceVersion", getHGVersion()) + .detail("Version", FDB_VT_VERSION ) + .detail("PackageName", FDB_VT_PACKAGE_NAME) + .detailf("ActualTime", "%lld", DEBUG_DETERMINISM ? 0 : time(NULL)) + .detail("CommandLine", commandLine) + .trackLatest("ProgramStart"); + + db = cluster->createDatabase(databaseKey).get(); + + if(sourceClusterFile.size()) { + auto resolvedSourceClusterFile = ClusterConnectionFile::lookupClusterFileName(sourceClusterFile); + try { + source_ccf = Reference(new ClusterConnectionFile(resolvedSourceClusterFile.first)); + } + catch (Error& e) { + fprintf(stderr, "%s\n", ClusterConnectionFile::getErrorString(resolvedSourceClusterFile, e).c_str()); + return 1; + } + + try { + source_cluster = Cluster::createCluster(source_ccf, -1); + } + catch (Error& e) { + fprintf(stderr, "ERROR: %s\n", e.what()); + fprintf(stderr, "ERROR: Unable to connect to cluster from `%s'\n", source_ccf->getFilename().c_str()); + return 1; + } + + source_db = source_cluster->createDatabase(databaseKey).get(); + } + } + + switch (programExe) + { + case EXE_AGENT: + f = stopAfter(runAgent(db)); + break; + case EXE_BACKUP: + switch (backupType) + { + case BACKUP_START: + { + // Error, if no dest container was specified + if (destinationContainer.empty()) { + fprintf(stderr, "ERROR: No backup destination was specified.\n"); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + + // Test out the backup url to make sure it parses. Doesn't test to make sure it's actually writeable. + std::string error; + try { + Reference c = IBackupContainer::openContainer(destinationContainer, &error); + } + catch (Error& e) { + if(!error.empty()) + error = std::string("[") + error + "]"; + fprintf(stderr, "ERROR (%s) on %s %s\n", e.what(), destinationContainer.c_str(), error.c_str()); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + } + + f = stopAfter( submitBackup(db, destinationContainer, backupKeys, tagName, dryRun, waitForDone, stopWhenDone) ); + break; + } + + case BACKUP_STATUS: + f = stopAfter( statusBackup(db, tagName, maxErrors) ); + break; + + case BACKUP_ABORT: + f = stopAfter( abortBackup(db, tagName) ); + break; + + case BACKUP_WAIT: + f = stopAfter( waitBackup(db, tagName, stopWhenDone) ); + break; + + case BACKUP_DISCONTINUE: + f = stopAfter( discontinueBackup(db, tagName, waitForDone) ); + break; + + case BACKUP_UNDEFINED: + default: + fprintf(stderr, "ERROR: Unsupported backup action %s\n", argv[1]); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + break; + } + + break; + case EXE_RESTORE: + switch(restoreType) { + case RESTORE_START: + f = stopAfter( runRestore(db, tagName, restoreContainer, backupKeys, dbVersion, !dryRun, !quietDisplay, waitForDone, addPrefix, removePrefix) ); + break; + case RESTORE_WAIT: + f = stopAfter( success(ba.waitRestore(db, KeyRef(tagName), true)) ); + break; + case RESTORE_ABORT: + f = stopAfter( map(ba.abortRestore(db, KeyRef(tagName)), [tagName](FileBackupAgent::ERestoreState s) -> Void { + printf("Tag: %s State: %s\n", tagName.c_str(), FileBackupAgent::restoreStateText(s).toString().c_str()); + return Void(); + }) ); + break; + case RESTORE_STATUS: + + // If no tag is specifically provided then print all tag status, don't just use "default" + if(tagProvided) + tag = tagName; + f = stopAfter( map(ba.restoreStatus(db, KeyRef(tag)), [](std::string s) -> Void { + printf("%s\n", s.c_str()); + return Void(); + }) ); + break; + default: + throw restore_error(); + } + break; + case EXE_DR_AGENT: + f = stopAfter( runDBAgent(source_db, db) ); + break; + case EXE_DB_BACKUP: //DB_START, DB_STATUS, DB_SWITCH, DB_ABORT, DB_CLEANUP + switch (dbType) + { + case DB_START: + f = stopAfter( submitDBBackup(source_db, db, backupKeys, tagName) ); + break; + case DB_STATUS: + f = stopAfter( statusDBBackup(source_db, db, tagName, maxErrors) ); + break; + case DB_SWITCH: + f = stopAfter( switchDBBackup(source_db, db, backupKeys, tagName) ); + break; + case DB_ABORT: + f = stopAfter( abortDBBackup(source_db, db, tagName, partial) ); + break; + case DB_UNDEFINED: + default: + fprintf(stderr, "ERROR: Unsupported DR action %s\n", argv[1]); + printHelpTeaser(argv[0]); + return FDB_EXIT_ERROR; + break; + } + break; + case EXE_BLOBMANAGER: + fstatus = stopAfter( doBlobCommand(blobArgs) ); + break; + case EXE_UNDEFINED: + default: + return FDB_EXIT_ERROR; + } + + runNetwork(); + + if(f.isValid() && f.isReady() && !f.isError() && !f.get().present()) { + status = FDB_EXIT_ERROR; + } + + if(fstatus.isValid() && fstatus.isReady() && !fstatus.isError() && fstatus.get().present()) { + status = fstatus.get().get(); + } + + #ifdef ALLOC_INSTRUMENTATION + { + cout << "Page Counts: " + << FastAllocator<16>::pageCount << " " + << FastAllocator<32>::pageCount << " " + << FastAllocator<64>::pageCount << " " + << FastAllocator<128>::pageCount << " " + << FastAllocator<256>::pageCount << " " + << FastAllocator<512>::pageCount << " " + << FastAllocator<1024>::pageCount << " " + << FastAllocator<2048>::pageCount << " " + << FastAllocator<4096>::pageCount << endl; + + vector< std::pair > typeNames; + for( auto i = allocInstr.begin(); i != allocInstr.end(); ++i ) { + std::string s; + +#ifdef __linux__ + char *demangled = abi::__cxa_demangle(i->first, NULL, NULL, NULL); + if (demangled) { + s = demangled; + if (StringRef(s).startsWith(LiteralStringRef("(anonymous namespace)::"))) + s = s.substr(LiteralStringRef("(anonymous namespace)::").size()); + free(demangled); + } else + s = i->first; +#else + s = i->first; + if (StringRef(s).startsWith(LiteralStringRef("class `anonymous namespace'::"))) + s = s.substr(LiteralStringRef("class `anonymous namespace'::").size()); + else if (StringRef(s).startsWith(LiteralStringRef("class "))) + s = s.substr(LiteralStringRef("class ").size()); + else if (StringRef(s).startsWith(LiteralStringRef("struct "))) + s = s.substr(LiteralStringRef("struct ").size()); +#endif + + typeNames.push_back( std::make_pair(s, i->first) ); + } + std::sort(typeNames.begin(), typeNames.end()); + for(int i=0; i + + + + -PRERELEASE + + + + + FDB_CLEAN_BUILD;%(PreprocessorDefinitions) + + + + Debug + X64 + + + Release + X64 + + + + + + + {8E959DA5-5925-45CE-BFC4-C84EB632A29B} + v4.5 + Win32Proj + flow + + + $(SolutionDir)bin\$(Configuration)\ + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + $(IntDir)\$(MSBuildProjectName).log + + + + Application + MultiByte + v140_xp + + + Application + MultiByte + v140_xp + + + + + + + + + + true + $(IncludePath);../;C:\Program Files\boost_1_52_0 + + + false + $(IncludePath);../;C:\Program Files\boost_1_52_0 + PreBuildEvent + + + + $(TargetDir)fdbclient.lib + + + FDB_VT_VERSION="$(Version)$(PreReleaseDecoration)";FDB_VT_PACKAGE_NAME="$(PackageName)";%(PreprocessorDefinitions) + + + + + + + Level3 + false + ProgramDatabase + Disabled + EnableFastChecks + MultiThreadedDebug + WIN32;_WIN32_WINNT=0x0502;WINVER=0x0502;NTDDI_VERSION=0x05020000;_DEBUG;_HAS_ITERATOR_DEBUGGING=0;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) + true + /bigobj @../flow/no_intellisense.opt %(AdditionalOptions) + + + Console + true + $(SolutionDir)bin\$(Configuration)\fdbclient.lib;Advapi32.lib + + + + + Level3 + + + ProgramDatabase + Full + MultiThreaded + true + WIN32;_WIN32_WINNT=0x0502;WINVER=0x0502;NTDDI_VERSION=0x05020000;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) + NotSet + false + /bigobj @../flow/no_intellisense.opt %(AdditionalOptions) + true + Speed + false + false + + + Console + true + false + false + $(SolutionDir)bin\$(Configuration)\fdbclient.lib;Advapi32.lib + /LTCG %(AdditionalOptions) + + + + + + + + + + + + + + + + + diff --git a/fdbbackup/local.mk b/fdbbackup/local.mk new file mode 100644 index 0000000000..602daaf1d6 --- /dev/null +++ b/fdbbackup/local.mk @@ -0,0 +1,58 @@ +# +# local.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile; -*- + +fdbbackup_CFLAGS := $(fdbclient_CFLAGS) +fdbbackup_LDFLAGS := $(fdbrpc_LDFLAGS) +fdbbackup_LIBS := lib/libfdbclient.a lib/libfdbrpc.a lib/libflow.a + +ifeq ($(PLATFORM),linux) + fdbbackup_LIBS += -ldl -lpthread -lrt + fdbbackup_LDFLAGS += -static-libstdc++ -static-libgcc + + # GPerfTools profiler (uncomment to use) + # fdbbackup_CFLAGS += -I/opt/gperftools/include -DUSE_GPERFTOOLS=1 + # fdbbackup_LDFLAGS += -L/opt/gperftools/lib + # fdbbackup_STATIC_LIBS += -ltcmalloc -lunwind -lprofiler +else ifeq ($(PLATFORM),osx) + fdbbackup_LDFLAGS += -lc++ +endif + +fdbbackup_GENERATED_SOURCES += versions.h + +#ifeq ($(WORKLOADS),false) +# fdbbackup_ALL_SOURCES := $(filter-out fdbbackup/workloads/%,$(fdbbackup_ALL_SOURCES)) +# fdbbackup_BUILD_SOURCES := $(filter-out fdbbackup/workloads/%,$(fdbbackup_BUILD_SOURCES)) +#endif + +bin/fdbbackup: bin/coverage.fdbbackup.xml + +bin/fdbbackup.debug: bin/fdbbackup + +BACKUP_ALIASES = fdbrestore fdbblob fdbdr dr_agent backup_agent + +$(addprefix bin/, $(BACKUP_ALIASES)): bin/fdbbackup + @[ -f $@ ] || (echo "SymLinking $@" && ln -s fdbbackup $@) + +$(addprefix bin/, $(addsuffix .debug, $(BACKUP_ALIASES))): bin/fdbbackup.debug + @[ -f $@ ] || (echo "SymLinking $@" && ln -s fdbbackup.debug $@) + +FORCE: diff --git a/fdbcli/FlowLineNoise.actor.cpp b/fdbcli/FlowLineNoise.actor.cpp new file mode 100644 index 0000000000..8b5410bf2b --- /dev/null +++ b/fdbcli/FlowLineNoise.actor.cpp @@ -0,0 +1,181 @@ +/* + * FlowLineNoise.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "FlowLineNoise.h" +#include "flow/IThreadPool.h" + +#define BOOST_SYSTEM_NO_LIB +#define BOOST_DATE_TIME_NO_LIB +#define BOOST_REGEX_NO_LIB +#include "boost/asio.hpp" + +#include "flow/ThreadHelper.actor.h" + +#if __unixish__ + #define HAVE_LINENOISE 1 + #include "linenoise/linenoise.h" +#else + #define HAVE_LINENOISE 0 +#endif + +struct LineNoiseReader : IThreadPoolReceiver { + virtual void init() {} + + struct Read : TypedAction { + std::string prompt; + ThreadReturnPromise> result; + + virtual double getTimeEstimate() { return 0.0; } + explicit Read(std::string const& prompt) : prompt(prompt) {} + }; + + void action(Read& r) { + try { + r.result.send( read(r.prompt) ); + } catch (Error& e) { + r.result.sendError(e); + } catch (...) { + r.result.sendError(unknown_error()); + } + } + +private: + Optional read(std::string const& prompt) { + #if HAVE_LINENOISE + errno = 0; + char* line = linenoise(prompt.c_str()); + if (line) { + std::string s(line); + free(line); + return s; + } else { + if (errno == EAGAIN) // Ctrl-C + return std::string(); + return Optional(); + } + #else + std::string line; + std::fputs( prompt.c_str(), stdout ); + if (!std::getline( std::cin, line ).eof()) { + return line; + } else + return Optional(); + #endif + } +}; + +LineNoise::LineNoise( + std::function< void(std::string const&, std::vector&) > _completion_callback, + std::function< Hint(std::string const&) > _hint_callback, + int maxHistoryLines, + bool multiline ) + : threadPool( createGenericThreadPool() ) +{ + reader = new LineNoiseReader(); + + #if HAVE_LINENOISE + // It should be OK to call these functions from this thread, since read() can't be called yet + // The callbacks passed to linenoise*() will be invoked from the thread pool, and use onMainThread() to safely invoke the callbacks we've been given + + // linenoise doesn't provide any form of data parameter to callbacks, so we have to use static variables + static std::function< void(std::string const&, std::vector&) > completion_callback; + static std::function< Hint(std::string const&) > hint_callback; + completion_callback = _completion_callback; + hint_callback = _hint_callback; + + linenoiseHistorySetMaxLen( maxHistoryLines ); + linenoiseSetMultiLine( multiline ); + linenoiseSetCompletionCallback( [](const char* line, linenoiseCompletions* lc) { + // This code will run in the thread pool + std::vector completions; + onMainThread( [line, &completions]() -> Future { + completion_callback(line, completions); + return Void(); + }).getBlocking(); + for( auto const& c : completions ) + linenoiseAddCompletion( lc, c.c_str() ); + }); + /*linenoiseSetHintsCallback( [](const char* line, int* color, int*bold) -> const char* { + Hint h = onMainThread( [line]() -> Future { + return hint_callback(line); + }).getBlocking(); + if (!h.valid) return NULL; + *color = h.color; + *bold = h.bold; + return strdup( h.text.c_str() ); + }); + linenoiseSetFreeHintsCallback( free );*/ + #endif + + threadPool->addThread(reader); +} + +LineNoise::~LineNoise() { + threadPool.clear(); +} + +Future> LineNoise::read( std::string const& prompt ) { + auto r = new LineNoiseReader::Read(prompt); + auto f = r->result.getFuture(); + threadPool->post(r); + return f; +} + +ACTOR Future waitKeyboardInterrupt(boost::asio::io_service* ios) { + state boost::asio::signal_set signals(*ios, SIGINT); + Promise result; + signals.async_wait([result](const boost::system::error_code& error, int signal_number) { + if (error) { + result.sendError(io_error()); + } else { + result.send(Void()); + } + }); + + Void _ = wait(result.getFuture()); + return Void(); +} + +Future LineNoise::onKeyboardInterrupt() { + boost::asio::io_service* ios = (boost::asio::io_service*)g_network->global(INetwork::enASIOService); + if (!ios) return Never(); + return waitKeyboardInterrupt(ios); +} + +void LineNoise::historyAdd( std::string const& line ) { + #if HAVE_LINENOISE + linenoiseHistoryAdd( line.c_str() ); + #endif +} +void LineNoise::historyLoad( std::string const& filename ) { + #if HAVE_LINENOISE + if(linenoiseHistoryLoad(filename.c_str()) != 0) { + throw io_error(); + } + #endif +} +void LineNoise::historySave( std::string const& filename ) { + #if HAVE_LINENOISE + if(linenoiseHistorySave(filename.c_str()) != 0) { + throw io_error(); + } + #endif +} \ No newline at end of file diff --git a/fdbcli/FlowLineNoise.h b/fdbcli/FlowLineNoise.h new file mode 100644 index 0000000000..adecf472c2 --- /dev/null +++ b/fdbcli/FlowLineNoise.h @@ -0,0 +1,64 @@ +/* + * FlowLineNoise.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOWLINENOISE_H +#define FLOWLINENOISE_H +#pragma once + +#include "flow/flow.h" +#include + +struct LineNoise : NonCopyable { + // Wraps the linenoise library so that it can be called from asynchronous Flow code + // Only create one of these at a time; the linenoise library only supports one history + // + // The current implementation does not support calling read concurrently with any other + // function (or itself). + + struct Hint { + std::string text; + int color; + bool bold; + bool valid; + Hint() : text(), color(), bold(), valid() {} + Hint( std::string const& text, int color, bool bold ) : text(text), color(color), bold(bold), valid(true) {} + }; + + LineNoise( + std::function< void(std::string const&, std::vector&) > completion_callback, + std::function< Hint(std::string const&) > hint_callback, + int maxHistoryLines, + bool multiline + ); + ~LineNoise(); + + Future< Optional > read( std::string const& prompt ); // Returns "nothing" on EOF + void historyAdd( std::string const& line ); + + void historyLoad( std::string const& filename ); + void historySave( std::string const& filename ); + + static Future onKeyboardInterrupt(); // Returns when Ctrl-C is next pressed (i.e. SIGINT) + + Reference threadPool; + struct LineNoiseReader* reader; +}; + +#endif \ No newline at end of file diff --git a/fdbcli/fdbcli.actor.cpp b/fdbcli/fdbcli.actor.cpp new file mode 100644 index 0000000000..83d19b6abb --- /dev/null +++ b/fdbcli/fdbcli.actor.cpp @@ -0,0 +1,2924 @@ +/* + * fdbcli.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fdbclient/NativeAPI.h" +#include "fdbclient/Status.h" +#include "fdbclient/StatusClient.h" +#include "fdbclient/DatabaseContext.h" +#include "fdbclient/NativeAPI.h" +#include "fdbclient/ReadYourWrites.h" +#include "fdbclient/ClusterInterface.h" +#include "fdbclient/ManagementAPI.h" +#include "fdbclient/CoordinationInterface.h" +#include "fdbclient/FDBOptions.g.h" + +#include "flow/DeterministicRandom.h" +#include "fdbrpc/TLSConnection.h" +#include "fdbrpc/Platform.h" + +#include "flow/SimpleOpt.h" + +#include "FlowLineNoise.h" + +#include + +#ifdef __unixish__ +#include +#include "fdbcli/linenoise/linenoise.h" +#endif + +#ifndef WIN32 +#include "versions.h" +#endif + +extern const char* getHGVersion(); + +std::vector validOptions; + +enum { OPT_CONNFILE, OPT_DATABASE, OPT_HELP, OPT_TRACE, OPT_TRACE_DIR, OPT_TIMEOUT, OPT_EXEC, OPT_NO_STATUS, OPT_STATUS_FROM_JSON, OPT_VERSION }; + +CSimpleOpt::SOption g_rgOptions[] = { + { OPT_CONNFILE, "-C", SO_REQ_SEP }, + { OPT_CONNFILE, "--cluster_file", SO_REQ_SEP }, + { OPT_DATABASE, "-d", SO_REQ_SEP }, + { OPT_TRACE, "--log", SO_NONE }, + { OPT_TRACE_DIR, "--log-dir", SO_REQ_SEP }, + { OPT_TIMEOUT, "--timeout", SO_REQ_SEP }, + { OPT_EXEC, "--exec", SO_REQ_SEP }, + { OPT_NO_STATUS, "--no-status", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_STATUS_FROM_JSON, "--status-from-json", SO_REQ_SEP }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_VERSION, "-v", SO_NONE }, + + TLS_OPTION_FLAGS + + SO_END_OF_OPTIONS +}; + +void printAtCol(const char* text, int col) { + const char* iter = text; + const char* start = text; + const char* space = NULL; + + do { + iter++; + if (*iter == '\n' || *iter == ' ' || *iter == '\0') space = iter; + if (*iter == '\n' || *iter == '\0' || (iter - start == col)) { + if (!space) space = iter; + printf("%.*s\n", (int)(space - start), start); + start = space; + if (*start == ' ' || *start == '\n') start++; + space = NULL; + } + } while (*iter); +} + +std::string lineWrap(const char* text, int col) { + const char* iter = text; + const char* start = text; + const char* space = NULL; + std::string out = ""; + do { + iter++; + if (*iter == '\n' || *iter == ' ' || *iter == '\0') space = iter; + if (*iter == '\n' || *iter == '\0' || (iter - start == col)) { + if (!space) space = iter; + out += format("%.*s\n", (int)(space - start), start); + start = space; + if (*start == ' '/* || *start == '\n'*/) start++; + space = NULL; + } + } while (*iter); + return out; +} + +class FdbOptions { +public: + //Prints an error and throws invalid_option or invalid_option_value if the option could not be set + void setOption(Reference tr, StringRef optionStr, bool enabled, Optional arg, bool intrans) { + auto transactionItr = transactionOptions.legalOptions.find(optionStr.toString()); + if(transactionItr != transactionOptions.legalOptions.end()) + setTransactionOption(tr, transactionItr->second, enabled, arg, intrans); + else { + printf("ERROR: invalid option '%s'. Try `help options' for a list of available options.\n", optionStr.toString().c_str()); + throw invalid_option(); + } + } + + //Applies all enabled transaction options to the given transaction + void apply(Reference tr) { + for(auto itr = transactionOptions.options.begin(); itr != transactionOptions.options.end(); ++itr) + tr->setOption(itr->first, itr->second.cast_to()); + } + + //Returns true if any options have been set + bool hasAnyOptionsEnabled() { + return !transactionOptions.options.empty(); + } + + //Prints a list of enabled options, along with their parameters (if any) + void print() { + bool found = false; + found = found || transactionOptions.print(); + + if(!found) + printf("There are no options enabled\n"); + } + + //Returns a vector of the names of all documented options + std::vector getValidOptions() { + return transactionOptions.getValidOptions(); + } + + //Prints the help string obtained by invoking `help options' + void printHelpString() { + transactionOptions.printHelpString(); + } + +private: + //Sets a transaction option. If intrans == true, then this option is also applied to the passed in transaction. + void setTransactionOption(Reference tr, FDBTransactionOptions::Option option, bool enabled, Optional arg, bool intrans) { + if(enabled && arg.present() != FDBTransactionOptions::optionInfo[option].hasParameter) { + printf("ERROR: option %s a parameter\n", arg.present() ? "did not expect" : "expected"); + throw invalid_option_value(); + } + + if(intrans) + tr->setOption(option, arg); + + transactionOptions.setOption(option, enabled, arg.cast_to()); + } + + //A group of enabled options (of type T::Option) as well as a legal options map from string to T::Option + template + struct OptionGroup { + std::map>> options; + std::map legalOptions; + + OptionGroup() { } + OptionGroup(OptionGroup &base) : options(base.options.begin(), base.options.end()), legalOptions(base.legalOptions) { } + + //Enable or disable an option. Returns true if option value changed + bool setOption(typename T::Option option, bool enabled, Optional arg) { + auto optionItr = options.find(option); + if(enabled && (optionItr == options.end() || Optional>(optionItr->second).cast_to< StringRef >() != arg)) { + options[option] = arg.cast_to>(); + return true; + } + else if(!enabled && optionItr != options.end()) { + options.erase(optionItr); + return true; + } + + return false; + } + + //Prints a list of all enabled options in this group + bool print() { + bool found = false; + + for(auto itr = legalOptions.begin(); itr != legalOptions.end(); ++itr) { + auto optionItr = options.find(itr->second); + if(optionItr != options.end()) { + if(optionItr->second.present()) + printf("%s: `%s'\n", itr->first.c_str(), formatStringRef(optionItr->second.get()).c_str()); + else + printf("%s\n", itr->first.c_str()); + + found = true; + } + } + + return found; + } + + //Returns true if the specified option is documented + bool isDocumented(typename T::Option option) { + FDBOptionInfo info = T::optionInfo[option]; + + std::string deprecatedStr = "Deprecated"; + return !info.comment.empty() && info.comment.substr(0, deprecatedStr.size()) != deprecatedStr; + } + + //Returns a vector of the names of all documented options + std::vector getValidOptions() { + std::vector ret; + + for (auto itr = legalOptions.begin(); itr != legalOptions.end(); ++itr) + if(isDocumented(itr->second)) + ret.push_back(itr->first); + + return ret; + } + + //Prints a help string for each option in this group. Any options with no comment + //are excluded from this help string. Lines are wrapped to 80 characters. + void printHelpString() { + for(auto itr = legalOptions.begin(); itr != legalOptions.end(); ++itr) { + if(isDocumented(itr->second)) { + FDBOptionInfo info = T::optionInfo[itr->second]; + std::string helpStr = info.name + " - " + info.comment; + if(info.hasParameter) + helpStr += " " + info.parameterComment; + helpStr += "\n"; + + printAtCol(helpStr.c_str(), 80); + } + } + } + }; + + OptionGroup transactionOptions; + +public: + FdbOptions() { + for(auto itr = FDBTransactionOptions::optionInfo.begin(); itr != FDBTransactionOptions::optionInfo.end(); ++itr) + transactionOptions.legalOptions[itr->second.name] = itr->first; + } + + FdbOptions(FdbOptions &base) : transactionOptions(base.transactionOptions) { } +}; + +static std::string formatStringRef(StringRef item, bool fullEscaping = false) +{ + std::string ret; + + for (int i = 0; i < item.size(); i++) { + if (fullEscaping && item[i] == '\\') + ret += "\\\\"; + else if (fullEscaping && item[i] == '"') + ret += "\\\""; + else if (fullEscaping && item[i] == ' ') + ret += format("\\x%02x", item[i]); + else if (item[i] >= 32 && item[i] < 127) + ret += item[i]; + else + ret += format("\\x%02x", item[i]); + } + + return ret; +} + +static bool tokencmp(StringRef token, const char *command) +{ + if (token.size() != strlen(command)) + return false; + + return !memcmp(token.begin(), command, token.size()); +} + + +static std::vector> parseLine(std::string& line, bool& err, bool& partial) +{ + err = false; + partial = false; + + bool quoted = false; + std::vector buf; + std::vector> ret; + + size_t i = line.find_first_not_of(' '); + size_t offset = i; + + bool forcetoken = false; + + while (i <= line.length()) { + switch (line[i]) { + case ';': + if (!quoted) { + if (i > offset || (forcetoken && i == offset)) + buf.push_back(StringRef((uint8_t*)(line.data() + offset), i - offset)); + ret.push_back(std::move(buf)); + offset = i = line.find_first_not_of(' ', i+1); + forcetoken = false; + } else + i++; + break; + case '"': + quoted = !quoted; + line.erase(i, 1); + forcetoken = true; + break; + case ' ': + if (!quoted) { + if (i > offset || (forcetoken && i == offset)) + buf.push_back(StringRef((uint8_t*)(line.data() + offset), i - offset)); + offset = i = line.find_first_not_of(' ', i); + forcetoken = false; + } else + i++; + break; + case '\\': + if (i + 2 > line.length()) { + err = true; + ret.push_back(std::move(buf)); + return ret; + } + switch (line[i+1]) { + char ent, save; + case '"': + case '\\': + case ' ': + case ';': + line.erase(i, 1); + break; + case 'x': + if (i + 4 > line.length()) { + err = true; + ret.push_back(std::move(buf)); + return ret; + } + char *pEnd; + save = line[i + 4]; + line[i + 4] = 0; + ent = char(strtoul(line.data() + i + 2, &pEnd, 16)); + if (*pEnd) { + err = true; + ret.push_back(std::move(buf)); + return ret; + } + line[i + 4] = save; + line.replace(i, 4, 1, ent); + break; + default: + err = true; + ret.push_back(std::move(buf)); + return ret; + } + default: + i++; + } + } + + i -= 1; + if (i > offset || (forcetoken && i == offset)) + buf.push_back(StringRef((uint8_t*)(line.data() + offset), i - offset)); + + ret.push_back(std::move(buf)); + + if (quoted) + partial = true; + + return ret; +} + +static void printProgramUsage(const char* name) { + printf("FoundationDB CLI " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n" + "usage: %s [OPTIONS]\n" + "\n", name); + printf(" -C CONNFILE The path of a file containing the connection string for the\n" + " FoundationDB cluster. The default is first the value of the\n" + " FDB_CLUSTER_FILE environment variable, then `./fdb.cluster',\n" + " then `%s'.\n", platform::getDefaultClusterFilePath().c_str()); + printf(" --log Enables trace file logging for the CLI session.\n" + " --log-dir PATH Specifes the output directory for trace files. If\n" + " unspecified, defaults to the current directory. Has\n" + " no effect unless --log is specified.\n" + " --exec CMDS Immediately executes the semicolon separated CLI commands\n" + " and then exits.\n" + " --no-status Disables the initial status check done when starting\n" + " the CLI.\n" + TLS_HELP + " -v, --version Print FoundationDB CLI version information and exit.\n" + " -h, --help Display this help and exit.\n"); +} + + +struct CommandHelp { + std::string usage; + std::string short_desc; + std::string long_desc; + CommandHelp() {} + CommandHelp(const char* u, const char* s, const char* l) : usage(u), short_desc(s), long_desc(l) {} +}; + +std::map helpMap; +std::set hiddenCommands; + +#define ESCAPINGK "\n\nFor information on escaping keys, type `help escaping'." +#define ESCAPINGKV "\n\nFor information on escaping keys and values, type `help escaping'." + +void initHelp() { + helpMap["begin"] = CommandHelp( + "begin", + "begin a new transaction", + "By default, the fdbcli operates in autocommit mode. All operations are performed in their own transaction, and are automatically committed for you. By explicitly beginning a transaction, successive operations are all performed as part of a single transaction.\n\nTo commit the transaction, use the commit command. To discard the transaction, use the reset command."); + helpMap["commit"] = CommandHelp( + "commit", + "commit the current transaction", + "Any sets or clears executed after the start of the current transaction will be committed to the database. On success, the committed version number is displayed. If commit fails, the error is displayed and the transaction must be retried."); + helpMap["clear"] = CommandHelp( + "clear ", + "clear a key from the database", + "Clear succeeds even if the specified key is not present, but may fail because of conflicts." ESCAPINGK); + helpMap["clearrange"] = CommandHelp( + "clearrange ", + "clear a range of keys from the database", + "All keys between BEGINKEY (inclusive) and ENDKEY (exclusive) are cleared from the database. This command will succeed even if the specified range is empty, but may fail because of conflicts." ESCAPINGK); + helpMap["configure"] = CommandHelp( + "configure [new] |logs=|resolvers=>*", + "change database configuration", + "The `new' option, if present, initializes a new database with the given configuration rather than changing the configuration of an existing one. When used, both a redundancy mode and a storage engine must be specified.\n\nRedundancy mode:\n single - one copy of the data. Not fault tolerant.\n double - two copies of data (survive one failure).\n triple - three copies of data (survive two failures).\n three_data_hall - See the Admin Guide.\n three_datacenter - See the Admin Guide.\n fast_recovery_double - two copies of data on the storage servers, three copies of the data on the logs, non-copying recovery if one log is missing.\n fast_recovery_triple - three copies of data on the storage servers, four copies of the data on the logs, non-copying recovery if one log is missing.\n\nStorage engine:\n ssd - B-Tree storage engine optimized for solid state disks.\n memory - Durable in-memory storage engine for small datasets.\n\nproxies=: Sets the desired number of proxies in the cluster. Must be at least 1, or set to -1 which restores the number of proxies to the default value.\n\nlogs=: Sets the desired number of log servers in the cluster. Must be at least 1, or set to -1 which restores the number of logs to the default value.\n\nresolvers=: Sets the desired number of resolvers in the cluster. Must be at least 1, or set to -1 which restores the number of resolvers to the default value.\n\nSee the FoundationDB Administration Guide for more information."); + helpMap["coordinators"] = CommandHelp( + "coordinators auto|
    + [description=new_cluster_description]", + "change cluster coordinators or description", + "If 'auto' is specified, coordinator addresses will be choosen automatically to support the configured redundancy level. (If the current set of coordinators are healthy and already support the redundancy level, nothing will be changed.)\n\nOtherwise, sets the coordinators to the list of IP:port pairs specified by
    +. An fdbserver process must be running on each of the specified addresses.\n\ne.g. coordinators 10.0.0.1:4000 10.0.0.2:4000 10.0.0.3:4000\n\nIf 'description=desc' is specified then the description field in the cluster\nfile is changed to desc, which must match [A-Za-z0-9_]+."); + helpMap["exclude"] = CommandHelp( + "exclude
    *", + "exclude servers from the database", + "If no addresses are specified, lists the set of excluded servers.\n\nFor each IP address or IP:port pair in
    *, adds the address to the set of excluded servers then waits until all database state has been safely moved away from the specified servers."); + helpMap["include"] = CommandHelp( + "include all|
    *", + "permit previously-excluded servers to rejoin the database", + "If `all' is specified, the excluded servers list is cleared.\n\nFor each IP address or IP:port pair in
    *, removes any matching exclusions from the excluded servers list. (A specified IP will match all IP:* exclusion entries)"); + helpMap["setclass"] = CommandHelp( + "setclass
    ", + "change the class of a process", + "If no address and class are specified, lists the classes of all servers.\n\nSetting the class to `default' resets the process class to the class specified on the command line."); + helpMap["status"] = CommandHelp( + "status [minimal] [details] [json]", + "get the status of a FoundationDB cluster", + "If the cluster is down, this command will print a diagnostic which may be useful in figuring out what is wrong. If the cluster is running, this command will print cluster statistics.\n\nSpecifying 'minimal' will provide a minimal description of the status of your database.\n\nSpecifying 'details' will provide load information for individual workers.\n\nSpecifying 'json' will provide status information in a machine readable JSON format."); + helpMap["exit"] = CommandHelp("exit", "exit the CLI", ""); + helpMap["quit"] = CommandHelp(); + helpMap["waitconnected"] = CommandHelp(); + helpMap["waitopen"] = CommandHelp(); + helpMap["get"] = CommandHelp( + "get ", + "fetch the value for a given key", + "Displays the value of KEY in the database, or `not found' if KEY is not present." ESCAPINGK); + helpMap["getrange"] = CommandHelp( + "getrange [ENDKEY] [LIMIT]", + "fetch key/value pairs in a range of keys", + "Displays up to LIMIT keys and values for keys between BEGINKEY (inclusive) and ENDKEY (exclusive). If ENDKEY is omitted, then the range will include all keys starting with BEGINKEY. LIMIT defaults to 25 if omitted." ESCAPINGK); + helpMap["getrangekeys"] = CommandHelp( + "getrangekeys [ENDKEY] [LIMIT]", + "fetch keys in a range of keys", + "Displays up to LIMIT keys for keys between BEGINKEY (inclusive) and ENDKEY (exclusive). If ENDKEY is omitted, then the range will include all keys starting with BEGINKEY. LIMIT defaults to 25 if omitted." ESCAPINGK); + helpMap["reset"] = CommandHelp( + "reset", + "reset the current transaction", + "Any sets or clears executed after the start of the active transaction will be discarded."); + helpMap["rollback"] = CommandHelp( + "rollback", + "rolls back the current transaction", + "The active transaction will be discarded, including any sets or clears executed since the transaction was started."); + helpMap["set"] = CommandHelp( + "set ", + "set a value for a given key", + "If KEY is not already present in the database, it will be created." ESCAPINGKV); + helpMap["option"] = CommandHelp( + "option
    +
    Library SimpleIni +
    File SimpleIni.h +
    Author Brodie Thiesfield [code at jellycan dot com] +
    Source http://code.jellycan.com/simpleini/ +
    Version 4.16 +
    + + Jump to the @link CSimpleIniTempl CSimpleIni @endlink interface documentation. + + @section intro INTRODUCTION + + This component allows an INI-style configuration file to be used on both + Windows and Linux/Unix. It is fast, simple and source code using this + component will compile unchanged on either OS. + + + @section features FEATURES + + - MIT Licence allows free use in all software (including GPL and commercial) + - multi-platform (Windows 95/98/ME/NT/2K/XP/2003, Windows CE, Linux, Unix) + - loading and saving of INI-style configuration files + - configuration files can have any newline format on all platforms + - liberal acceptance of file format + - key/values with no section + - removal of whitespace around sections, keys and values + - support for multi-line values (values with embedded newline characters) + - optional support for multiple keys with the same name + - optional case-insensitive sections and keys (for ASCII characters only) + - saves files with sections and keys in the same order as they were loaded + - preserves comments on the file, section and keys where possible. + - supports both char or wchar_t programming interfaces + - supports both MBCS (system locale) and UTF-8 file encodings + - system locale does not need to be UTF-8 on Linux/Unix to load UTF-8 file + - support for non-ASCII characters in section, keys, values and comments + - support for non-standard character types or file encodings + via user-written converter classes + - support for adding/modifying values programmatically + - compiles cleanly in the following compilers: + - Windows/VC6 (warning level 3) + - Windows/VC.NET 2003 (warning level 4) + - Windows/VC 2005 (warning level 4) + - Linux/gcc (-Wall) + + + @section usage USAGE SUMMARY + + -# Define the appropriate symbol for the converter you wish to use and + include the SimpleIni.h header file. If no specific converter is defined + then the default converter is used. The default conversion mode uses + SI_CONVERT_WIN32 on Windows and SI_CONVERT_GENERIC on all other + platforms. If you are using ICU then SI_CONVERT_ICU is supported on all + platforms. + -# Declare an instance the appropriate class. Note that the following + definitions are just shortcuts for commonly used types. Other types + (PRUnichar, unsigned short, unsigned char) are also possible. + +
    Interface Case-sensitive Load UTF-8 Load MBCS Typedef +
    SI_CONVERT_GENERIC +
    char No Yes Yes #1 CSimpleIniA +
    char Yes Yes Yes CSimpleIniCaseA +
    wchar_t No Yes Yes CSimpleIniW +
    wchar_t Yes Yes Yes CSimpleIniCaseW +
    SI_CONVERT_WIN32 +
    char No No #2 Yes CSimpleIniA +
    char Yes Yes Yes CSimpleIniCaseA +
    wchar_t No Yes Yes CSimpleIniW +
    wchar_t Yes Yes Yes CSimpleIniCaseW +
    SI_CONVERT_ICU +
    char No Yes Yes CSimpleIniA +
    char Yes Yes Yes CSimpleIniCaseA +
    UChar No Yes Yes CSimpleIniW +
    UChar Yes Yes Yes CSimpleIniCaseW +
    + #1 On Windows you are better to use CSimpleIniA with SI_CONVERT_WIN32.
    + #2 Only affects Windows. On Windows this uses MBCS functions and + so may fold case incorrectly leading to uncertain results. + -# Call LoadData() or LoadFile() to load and parse the INI configuration file + -# Access and modify the data of the file using the following functions + +
    GetAllSections Return all section names +
    GetAllKeys Return all key names within a section +
    GetAllValues Return all values within a section & key +
    GetSection Return all key names and values in a section +
    GetSectionSize Return the number of keys in a section +
    GetValue Return a value for a section & key +
    SetValue Add or update a value for a section & key +
    Delete Remove a section, or a key from a section +
    + -# Call Save() or SaveFile() to save the INI configuration data + + @section iostreams IO STREAMS + + SimpleIni supports reading from and writing to STL IO streams. Enable this + by defining SI_SUPPORT_IOSTREAMS before including the SimpleIni.h header + file. Ensure that if the streams are backed by a file (e.g. ifstream or + ofstream) then the flag ios_base::binary has been used when the file was + opened. + + @section multiline MULTI-LINE VALUES + + Values that span multiple lines are created using the following format. + +
    +        key = <<
    +
    +    Note the following:
    +    - The text used for ENDTAG can be anything and is used to find
    +      where the multi-line text ends.
    +    - The newline after ENDTAG in the start tag, and the newline
    +      before ENDTAG in the end tag is not included in the data value.
    +    - The ending tag must be on it's own line with no whitespace before
    +      or after it.
    +    - The multi-line value is modified at load so that each line in the value
    +      is delimited by a single '\\n' character on all platforms. At save time
    +      it will be converted into the newline format used by the current
    +      platform.
    +
    +    @section comments COMMENTS
    +
    +    Comments are preserved in the file within the following restrictions:
    +    - Every file may have a single "file comment". It must start with the
    +      first character in the file, and will end with the first non-comment
    +      line in the file.
    +    - Every section may have a single "section comment". It will start
    +      with the first comment line following the file comment, or the last
    +      data entry. It ends at the beginning of the section.
    +    - Every key may have a single "key comment". This comment will start
    +      with the first comment line following the section start, or the file
    +      comment if there is no section name.
    +    - Comments are set at the time that the file, section or key is first
    +      created. The only way to modify a comment on a section or a key is to
    +      delete that entry and recreate it with the new comment. There is no
    +      way to change the file comment.
    +
    +    @section save SAVE ORDER
    +
    +    The sections and keys are written out in the same order as they were
    +    read in from the file. Sections and keys added to the data after the
    +    file has been loaded will be added to the end of the file when it is
    +    written. There is no way to specify the location of a section or key
    +    other than in first-created, first-saved order.
    +
    +    @section notes NOTES
    +
    +    - To load UTF-8 data on Windows 95, you need to use Microsoft Layer for
    +      Unicode, or SI_CONVERT_GENERIC, or SI_CONVERT_ICU.
    +    - When using SI_CONVERT_GENERIC, ConvertUTF.c must be compiled and linked.
    +    - When using SI_CONVERT_ICU, ICU header files must be on the include
    +      path and icuuc.lib must be linked in.
    +    - To load a UTF-8 file on Windows AND expose it with SI_CHAR == char,
    +      you should use SI_CONVERT_GENERIC.
    +    - The collation (sorting) order used for sections and keys returned from
    +      iterators is NOT DEFINED. If collation order of the text is important
    +      then it should be done yourself by either supplying a replacement
    +      SI_STRLESS class, or by sorting the strings external to this library.
    +    - Usage of the  header on Windows can be disabled by defining
    +      SI_NO_MBCS. This is defined automatically on Windows CE platforms.
    +
    +    @section contrib CONTRIBUTIONS
    +    
    +    - 2010/05/03: Tobias Gehrig: added GetDoubleValue()
    +
    +    @section licence MIT LICENCE
    +
    +    The licence text below is the boilerplate "MIT Licence" used from:
    +    http://www.opensource.org/licenses/mit-license.php
    +
    +    Copyright (c) 2006-2012, Brodie Thiesfield
    +
    +    Permission is hereby granted, free of charge, to any person obtaining a copy
    +    of this software and associated documentation files (the "Software"), to deal
    +    in the Software without restriction, including without limitation the rights
    +    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    +    copies of the Software, and to permit persons to whom the Software is furnished
    +    to do so, subject to the following conditions:
    +
    +    The above copyright notice and this permission notice shall be included in
    +    all copies or substantial portions of the Software.
    +
    +    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    +    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    +    FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    +    COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    +    IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    +    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    +*/
    +
    +#ifndef INCLUDED_SimpleIni_h
    +#define INCLUDED_SimpleIni_h
    +
    +#if defined(_MSC_VER) && (_MSC_VER >= 1020)
    +# pragma once
    +#endif
    +
    +// Disable these warnings in MSVC:
    +//  4127 "conditional expression is constant" as the conversion classes trigger
    +//  it with the statement if (sizeof(SI_CHAR) == sizeof(char)). This test will
    +//  be optimized away in a release build.
    +//  4503 'insert' : decorated name length exceeded, name was truncated
    +//  4702 "unreachable code" as the MS STL header causes it in release mode.
    +//  Again, the code causing the warning will be cleaned up by the compiler.
    +//  4786 "identifier truncated to 256 characters" as this is thrown hundreds
    +//  of times VC6 as soon as STL is used.
    +#ifdef _MSC_VER
    +# pragma warning (push)
    +# pragma warning (disable: 4127 4503 4702 4786)
    +#endif
    +
    +#include 
    +#include 
    +#include 
    +#include 
    +#include 
    +#include 
    +
    +#ifdef SI_SUPPORT_IOSTREAMS
    +# include 
    +#endif // SI_SUPPORT_IOSTREAMS
    +
    +#ifdef _DEBUG
    +# ifndef assert
    +#  include 
    +# endif
    +# define SI_ASSERT(x)   assert(x)
    +#else
    +# define SI_ASSERT(x)
    +#endif
    +
    +enum SI_Error {
    +    SI_OK       =  0,   //!< No error
    +    SI_UPDATED  =  1,   //!< An existing value was updated
    +    SI_INSERTED =  2,   //!< A new value was inserted
    +
    +    // note: test for any error with (retval < 0)
    +    SI_FAIL     = -1,   //!< Generic failure
    +    SI_NOMEM    = -2,   //!< Out of memory error
    +    SI_FILE     = -3    //!< File error (see errno for detail error)
    +};
    +
    +#define SI_UTF8_SIGNATURE     "\xEF\xBB\xBF"
    +
    +#ifdef _WIN32
    +# define SI_NEWLINE_A   "\r\n"
    +# define SI_NEWLINE_W   L"\r\n"
    +#else // !_WIN32
    +# define SI_NEWLINE_A   "\n"
    +# define SI_NEWLINE_W   L"\n"
    +#endif // _WIN32
    +
    +#if defined(SI_CONVERT_ICU)
    +# include 
    +#endif
    +
    +#if defined(_WIN32)
    +# define SI_HAS_WIDE_FILE
    +# define SI_WCHAR_T     wchar_t
    +#elif defined(SI_CONVERT_ICU)
    +# define SI_HAS_WIDE_FILE
    +# define SI_WCHAR_T     UChar
    +#endif
    +
    +
    +// ---------------------------------------------------------------------------
    +//                              MAIN TEMPLATE CLASS
    +// ---------------------------------------------------------------------------
    +
    +/** Simple INI file reader.
    +
    +    This can be instantiated with the choice of unicode or native characterset,
    +    and case sensitive or insensitive comparisons of section and key names.
    +    The supported combinations are pre-defined with the following typedefs:
    +
    +    
    +        
    Interface Case-sensitive Typedef +
    char No CSimpleIniA +
    char Yes CSimpleIniCaseA +
    wchar_t No CSimpleIniW +
    wchar_t Yes CSimpleIniCaseW +
    + + Note that using other types for the SI_CHAR is supported. For instance, + unsigned char, unsigned short, etc. Note that where the alternative type + is a different size to char/wchar_t you may need to supply new helper + classes for SI_STRLESS and SI_CONVERTER. + */ +template +class CSimpleIniTempl +{ +public: + /** key entry */ + struct Entry { + const SI_CHAR * pItem; + const SI_CHAR * pComment; + int nOrder; + + Entry(const SI_CHAR * a_pszItem = NULL, int a_nOrder = 0) + : pItem(a_pszItem) + , pComment(NULL) + , nOrder(a_nOrder) + { } + Entry(const SI_CHAR * a_pszItem, const SI_CHAR * a_pszComment, int a_nOrder) + : pItem(a_pszItem) + , pComment(a_pszComment) + , nOrder(a_nOrder) + { } + Entry(const Entry & rhs) { operator=(rhs); } + Entry & operator=(const Entry & rhs) { + pItem = rhs.pItem; + pComment = rhs.pComment; + nOrder = rhs.nOrder; + return *this; + } + +#if defined(_MSC_VER) && _MSC_VER <= 1200 + /** STL of VC6 doesn't allow me to specify my own comparator for list::sort() */ + bool operator<(const Entry & rhs) const { return LoadOrder()(*this, rhs); } + bool operator>(const Entry & rhs) const { return LoadOrder()(rhs, *this); } +#endif + + /** Strict less ordering by name of key only */ + struct KeyOrder : std::binary_function { + bool operator()(const Entry & lhs, const Entry & rhs) const { + const static SI_STRLESS isLess = SI_STRLESS(); + return isLess(lhs.pItem, rhs.pItem); + } + }; + + /** Strict less ordering by order, and then name of key */ + struct LoadOrder : std::binary_function { + bool operator()(const Entry & lhs, const Entry & rhs) const { + if (lhs.nOrder != rhs.nOrder) { + return lhs.nOrder < rhs.nOrder; + } + return KeyOrder()(lhs.pItem, rhs.pItem); + } + }; + }; + + /** map keys to values */ + typedef std::multimap TKeyVal; + + /** map sections to key/value map */ + typedef std::map TSection; + + /** set of dependent string pointers. Note that these pointers are + dependent on memory owned by CSimpleIni. + */ + typedef std::list TNamesDepend; + + /** interface definition for the OutputWriter object to pass to Save() + in order to output the INI file data. + */ + class OutputWriter { + public: + OutputWriter() { } + virtual ~OutputWriter() { } + virtual void Write(const char * a_pBuf) = 0; + private: + OutputWriter(const OutputWriter &); // disable + OutputWriter & operator=(const OutputWriter &); // disable + }; + + /** OutputWriter class to write the INI data to a file */ + class FileWriter : public OutputWriter { + FILE * m_file; + public: + FileWriter(FILE * a_file) : m_file(a_file) { } + void Write(const char * a_pBuf) { + fputs(a_pBuf, m_file); + } + private: + FileWriter(const FileWriter &); // disable + FileWriter & operator=(const FileWriter &); // disable + }; + + /** OutputWriter class to write the INI data to a string */ + class StringWriter : public OutputWriter { + std::string & m_string; + public: + StringWriter(std::string & a_string) : m_string(a_string) { } + void Write(const char * a_pBuf) { + m_string.append(a_pBuf); + } + private: + StringWriter(const StringWriter &); // disable + StringWriter & operator=(const StringWriter &); // disable + }; + +#ifdef SI_SUPPORT_IOSTREAMS + /** OutputWriter class to write the INI data to an ostream */ + class StreamWriter : public OutputWriter { + std::ostream & m_ostream; + public: + StreamWriter(std::ostream & a_ostream) : m_ostream(a_ostream) { } + void Write(const char * a_pBuf) { + m_ostream << a_pBuf; + } + private: + StreamWriter(const StreamWriter &); // disable + StreamWriter & operator=(const StreamWriter &); // disable + }; +#endif // SI_SUPPORT_IOSTREAMS + + /** Characterset conversion utility class to convert strings to the + same format as is used for the storage. + */ + class Converter : private SI_CONVERTER { + public: + using SI_CONVERTER::SizeToStore; + Converter(bool a_bStoreIsUtf8) : SI_CONVERTER(a_bStoreIsUtf8) { + m_scratch.resize(1024); + } + Converter(const Converter & rhs) { operator=(rhs); } + Converter & operator=(const Converter & rhs) { + m_scratch = rhs.m_scratch; + return *this; + } + bool ConvertToStore(const SI_CHAR * a_pszString) { + size_t uLen = SizeToStore(a_pszString); + if (uLen == (size_t)(-1)) { + return false; + } + while (uLen > m_scratch.size()) { + m_scratch.resize(m_scratch.size() * 2); + } + return SI_CONVERTER::ConvertToStore( + a_pszString, + const_cast(m_scratch.data()), + m_scratch.size()); + } + const char * Data() { return m_scratch.data(); } + private: + std::string m_scratch; + }; + +public: + /*-----------------------------------------------------------------------*/ + + /** Default constructor. + + @param a_bIsUtf8 See the method SetUnicode() for details. + @param a_bMultiKey See the method SetMultiKey() for details. + @param a_bMultiLine See the method SetMultiLine() for details. + */ + CSimpleIniTempl( + bool a_bIsUtf8 = false, + bool a_bMultiKey = false, + bool a_bMultiLine = false + ); + + /** Destructor */ + ~CSimpleIniTempl(); + + /** Deallocate all memory stored by this object */ + void Reset(); + + /** Has any data been loaded */ + bool IsEmpty() const { return m_data.empty(); } + + /*-----------------------------------------------------------------------*/ + /** @{ @name Settings */ + + /** Set the storage format of the INI data. This affects both the loading + and saving of the INI data using all of the Load/Save API functions. + This value cannot be changed after any INI data has been loaded. + + If the file is not set to Unicode (UTF-8), then the data encoding is + assumed to be the OS native encoding. This encoding is the system + locale on Linux/Unix and the legacy MBCS encoding on Windows NT/2K/XP. + If the storage format is set to Unicode then the file will be loaded + as UTF-8 encoded data regardless of the native file encoding. If + SI_CHAR == char then all of the char* parameters take and return UTF-8 + encoded data regardless of the system locale. + + \param a_bIsUtf8 Assume UTF-8 encoding for the source? + */ + void SetUnicode(bool a_bIsUtf8 = true) { + if (!m_pData) m_bStoreIsUtf8 = a_bIsUtf8; + } + + /** Get the storage format of the INI data. */ + bool IsUnicode() const { return m_bStoreIsUtf8; } + + /** Should multiple identical keys be permitted in the file. If set to false + then the last value encountered will be used as the value of the key. + If set to true, then all values will be available to be queried. For + example, with the following input: + +
    +        [section]
    +        test=value1
    +        test=value2
    +        
    + + Then with SetMultiKey(true), both of the values "value1" and "value2" + will be returned for the key test. If SetMultiKey(false) is used, then + the value for "test" will only be "value2". This value may be changed + at any time. + + \param a_bAllowMultiKey Allow multi-keys in the source? + */ + void SetMultiKey(bool a_bAllowMultiKey = true) { + m_bAllowMultiKey = a_bAllowMultiKey; + } + + /** Get the storage format of the INI data. */ + bool IsMultiKey() const { return m_bAllowMultiKey; } + + /** Should data values be permitted to span multiple lines in the file. If + set to false then the multi-line construct << + SI_CHAR FORMAT + char same format as when loaded (MBCS or UTF-8) + wchar_t UTF-8 + other UTF-8 + + + Note that comments from the original data is preserved as per the + documentation on comments. The order of the sections and values + from the original file will be preserved. + + Any data prepended or appended to the output device must use the the + same format (MBCS or UTF-8). You may use the GetConverter() method to + convert text to the correct format regardless of the output format + being used by SimpleIni. + + To add a BOM to UTF-8 data, write it out manually at the very beginning + like is done in SaveFile when a_bUseBOM is true. + + @param a_oOutput Output writer to write the data to. + + @param a_bAddSignature Prepend the UTF-8 BOM if the output data is in + UTF-8 format. If it is not UTF-8 then this value is + ignored. Do not set this to true if anything has + already been written to the OutputWriter. + + @return SI_Error See error definitions + */ + SI_Error Save( + OutputWriter & a_oOutput, + bool a_bAddSignature = false + ) const; + +#ifdef SI_SUPPORT_IOSTREAMS + /** Save the INI data to an ostream. See Save() for details. + + @param a_ostream String to have the INI data appended to. + + @param a_bAddSignature Prepend the UTF-8 BOM if the output data is in + UTF-8 format. If it is not UTF-8 then this value is + ignored. Do not set this to true if anything has + already been written to the stream. + + @return SI_Error See error definitions + */ + SI_Error Save( + std::ostream & a_ostream, + bool a_bAddSignature = false + ) const + { + StreamWriter writer(a_ostream); + return Save(writer, a_bAddSignature); + } +#endif // SI_SUPPORT_IOSTREAMS + + /** Append the INI data to a string. See Save() for details. + + @param a_sBuffer String to have the INI data appended to. + + @param a_bAddSignature Prepend the UTF-8 BOM if the output data is in + UTF-8 format. If it is not UTF-8 then this value is + ignored. Do not set this to true if anything has + already been written to the string. + + @return SI_Error See error definitions + */ + SI_Error Save( + std::string & a_sBuffer, + bool a_bAddSignature = false + ) const + { + StringWriter writer(a_sBuffer); + return Save(writer, a_bAddSignature); + } + + /*-----------------------------------------------------------------------*/ + /** @} + @{ @name Accessing INI Data */ + + /** Retrieve all section names. The list is returned as an STL vector of + names and can be iterated or searched as necessary. Note that the + sort order of the returned strings is NOT DEFINED. You can sort + the names into the load order if desired. Search this file for ".sort" + for an example. + + NOTE! This structure contains only pointers to strings. The actual + string data is stored in memory owned by CSimpleIni. Ensure that the + CSimpleIni object is not destroyed or Reset() while these pointers + are in use! + + @param a_names Vector that will receive all of the section + names. See note above! + */ + void GetAllSections( + TNamesDepend & a_names + ) const; + + /** Retrieve all unique key names in a section. The sort order of the + returned strings is NOT DEFINED. You can sort the names into the load + order if desired. Search this file for ".sort" for an example. Only + unique key names are returned. + + NOTE! This structure contains only pointers to strings. The actual + string data is stored in memory owned by CSimpleIni. Ensure that the + CSimpleIni object is not destroyed or Reset() while these strings + are in use! + + @param a_pSection Section to request data for + @param a_names List that will receive all of the key + names. See note above! + + @return true Section was found. + @return false Matching section was not found. + */ + bool GetAllKeys( + const SI_CHAR * a_pSection, + TNamesDepend & a_names + ) const; + + /** Retrieve all values for a specific key. This method can be used when + multiple keys are both enabled and disabled. Note that the sort order + of the returned strings is NOT DEFINED. You can sort the names into + the load order if desired. Search this file for ".sort" for an example. + + NOTE! The returned values are pointers to string data stored in memory + owned by CSimpleIni. Ensure that the CSimpleIni object is not destroyed + or Reset while you are using this pointer! + + @param a_pSection Section to search + @param a_pKey Key to search for + @param a_values List to return if the key is not found + + @return true Key was found. + @return false Matching section/key was not found. + */ + bool GetAllValues( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + TNamesDepend & a_values + ) const; + + /** Query the number of keys in a specific section. Note that if multiple + keys are enabled, then this value may be different to the number of + keys returned by GetAllKeys. + + @param a_pSection Section to request data for + + @return -1 Section does not exist in the file + @return >=0 Number of keys in the section + */ + int GetSectionSize( + const SI_CHAR * a_pSection + ) const; + + /** Retrieve all key and value pairs for a section. The data is returned + as a pointer to an STL map and can be iterated or searched as + desired. Note that multiple entries for the same key may exist when + multiple keys have been enabled. + + NOTE! This structure contains only pointers to strings. The actual + string data is stored in memory owned by CSimpleIni. Ensure that the + CSimpleIni object is not destroyed or Reset() while these strings + are in use! + + @param a_pSection Name of the section to return + @return boolean Was a section matching the supplied + name found. + */ + const TKeyVal * GetSection( + const SI_CHAR * a_pSection + ) const; + + /** Retrieve the value for a specific key. If multiple keys are enabled + (see SetMultiKey) then only the first value associated with that key + will be returned, see GetAllValues for getting all values with multikey. + + NOTE! The returned value is a pointer to string data stored in memory + owned by CSimpleIni. Ensure that the CSimpleIni object is not destroyed + or Reset while you are using this pointer! + + @param a_pSection Section to search + @param a_pKey Key to search for + @param a_pDefault Value to return if the key is not found + @param a_pHasMultiple Optionally receive notification of if there are + multiple entries for this key. + + @return a_pDefault Key was not found in the section + @return other Value of the key + */ + const SI_CHAR * GetValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + const SI_CHAR * a_pDefault = NULL, + bool * a_pHasMultiple = NULL + ) const; + + /** Retrieve a numeric value for a specific key. If multiple keys are enabled + (see SetMultiKey) then only the first value associated with that key + will be returned, see GetAllValues for getting all values with multikey. + + @param a_pSection Section to search + @param a_pKey Key to search for + @param a_nDefault Value to return if the key is not found + @param a_pHasMultiple Optionally receive notification of if there are + multiple entries for this key. + + @return a_nDefault Key was not found in the section + @return other Value of the key + */ + long GetLongValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + long a_nDefault = 0, + bool * a_pHasMultiple = NULL + ) const; + + /** Retrieve a numeric value for a specific key. If multiple keys are enabled + (see SetMultiKey) then only the first value associated with that key + will be returned, see GetAllValues for getting all values with multikey. + + @param a_pSection Section to search + @param a_pKey Key to search for + @param a_nDefault Value to return if the key is not found + @param a_pHasMultiple Optionally receive notification of if there are + multiple entries for this key. + + @return a_nDefault Key was not found in the section + @return other Value of the key + */ + double GetDoubleValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + double a_nDefault = 0, + bool * a_pHasMultiple = NULL + ) const; + + /** Retrieve a boolean value for a specific key. If multiple keys are enabled + (see SetMultiKey) then only the first value associated with that key + will be returned, see GetAllValues for getting all values with multikey. + + Strings starting with "t", "y", "on" or "1" are returned as logically true. + Strings starting with "f", "n", "of" or "0" are returned as logically false. + For all other values the default is returned. Character comparisons are + case-insensitive. + + @param a_pSection Section to search + @param a_pKey Key to search for + @param a_bDefault Value to return if the key is not found + @param a_pHasMultiple Optionally receive notification of if there are + multiple entries for this key. + + @return a_nDefault Key was not found in the section + @return other Value of the key + */ + bool GetBoolValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + bool a_bDefault = false, + bool * a_pHasMultiple = NULL + ) const; + + /** Add or update a section or value. This will always insert + when multiple keys are enabled. + + @param a_pSection Section to add or update + @param a_pKey Key to add or update. Set to NULL to + create an empty section. + @param a_pValue Value to set. Set to NULL to create an + empty section. + @param a_pComment Comment to be associated with the section or the + key. If a_pKey is NULL then it will be associated + with the section, otherwise the key. Note that a + comment may be set ONLY when the section or key is + first created (i.e. when this function returns the + value SI_INSERTED). If you wish to create a section + with a comment then you need to create the section + separately to the key. The comment string must be + in full comment form already (have a comment + character starting every line). + @param a_bForceReplace Should all existing values in a multi-key INI + file be replaced with this entry. This option has + no effect if not using multi-key files. The + difference between Delete/SetValue and SetValue + with a_bForceReplace = true, is that the load + order and comment will be preserved this way. + + @return SI_Error See error definitions + @return SI_UPDATED Value was updated + @return SI_INSERTED Value was inserted + */ + SI_Error SetValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + const SI_CHAR * a_pValue, + const SI_CHAR * a_pComment = NULL, + bool a_bForceReplace = false + ) + { + return AddEntry(a_pSection, a_pKey, a_pValue, a_pComment, a_bForceReplace, true); + } + + /** Add or update a numeric value. This will always insert + when multiple keys are enabled. + + @param a_pSection Section to add or update + @param a_pKey Key to add or update. + @param a_nValue Value to set. + @param a_pComment Comment to be associated with the key. See the + notes on SetValue() for comments. + @param a_bUseHex By default the value will be written to the file + in decimal format. Set this to true to write it + as hexadecimal. + @param a_bForceReplace Should all existing values in a multi-key INI + file be replaced with this entry. This option has + no effect if not using multi-key files. The + difference between Delete/SetLongValue and + SetLongValue with a_bForceReplace = true, is that + the load order and comment will be preserved this + way. + + @return SI_Error See error definitions + @return SI_UPDATED Value was updated + @return SI_INSERTED Value was inserted + */ + SI_Error SetLongValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + long a_nValue, + const SI_CHAR * a_pComment = NULL, + bool a_bUseHex = false, + bool a_bForceReplace = false + ); + + /** Add or update a double value. This will always insert + when multiple keys are enabled. + + @param a_pSection Section to add or update + @param a_pKey Key to add or update. + @param a_nValue Value to set. + @param a_pComment Comment to be associated with the key. See the + notes on SetValue() for comments. + @param a_bForceReplace Should all existing values in a multi-key INI + file be replaced with this entry. This option has + no effect if not using multi-key files. The + difference between Delete/SetDoubleValue and + SetDoubleValue with a_bForceReplace = true, is that + the load order and comment will be preserved this + way. + + @return SI_Error See error definitions + @return SI_UPDATED Value was updated + @return SI_INSERTED Value was inserted + */ + SI_Error SetDoubleValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + double a_nValue, + const SI_CHAR * a_pComment = NULL, + bool a_bForceReplace = false + ); + + /** Add or update a boolean value. This will always insert + when multiple keys are enabled. + + @param a_pSection Section to add or update + @param a_pKey Key to add or update. + @param a_bValue Value to set. + @param a_pComment Comment to be associated with the key. See the + notes on SetValue() for comments. + @param a_bForceReplace Should all existing values in a multi-key INI + file be replaced with this entry. This option has + no effect if not using multi-key files. The + difference between Delete/SetBoolValue and + SetBoolValue with a_bForceReplace = true, is that + the load order and comment will be preserved this + way. + + @return SI_Error See error definitions + @return SI_UPDATED Value was updated + @return SI_INSERTED Value was inserted + */ + SI_Error SetBoolValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + bool a_bValue, + const SI_CHAR * a_pComment = NULL, + bool a_bForceReplace = false + ); + + /** Delete an entire section, or a key from a section. Note that the + data returned by GetSection is invalid and must not be used after + anything has been deleted from that section using this method. + Note when multiple keys is enabled, this will delete all keys with + that name; there is no way to selectively delete individual key/values + in this situation. + + @param a_pSection Section to delete key from, or if + a_pKey is NULL, the section to remove. + @param a_pKey Key to remove from the section. Set to + NULL to remove the entire section. + @param a_bRemoveEmpty If the section is empty after this key has + been deleted, should the empty section be + removed? + + @return true Key or section was deleted. + @return false Key or section was not found. + */ + bool Delete( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + bool a_bRemoveEmpty = false + ); + + /*-----------------------------------------------------------------------*/ + /** @} + @{ @name Converter */ + + /** Return a conversion object to convert text to the same encoding + as is used by the Save(), SaveFile() and SaveString() functions. + Use this to prepare the strings that you wish to append or prepend + to the output INI data. + */ + Converter GetConverter() const { + return Converter(m_bStoreIsUtf8); + } + + /*-----------------------------------------------------------------------*/ + /** @} */ + +private: + // copying is not permitted + CSimpleIniTempl(const CSimpleIniTempl &); // disabled + CSimpleIniTempl & operator=(const CSimpleIniTempl &); // disabled + + /** Parse the data looking for a file comment and store it if found. + */ + SI_Error FindFileComment( + SI_CHAR *& a_pData, + bool a_bCopyStrings + ); + + /** Parse the data looking for the next valid entry. The memory pointed to + by a_pData is modified by inserting NULL characters. The pointer is + updated to the current location in the block of text. + */ + bool FindEntry( + SI_CHAR *& a_pData, + const SI_CHAR *& a_pSection, + const SI_CHAR *& a_pKey, + const SI_CHAR *& a_pVal, + const SI_CHAR *& a_pComment + ) const; + + /** Add the section/key/value to our data. + + @param a_pSection Section name. Sections will be created if they + don't already exist. + @param a_pKey Key name. May be NULL to create an empty section. + Existing entries will be updated. New entries will + be created. + @param a_pValue Value for the key. + @param a_pComment Comment to be associated with the section or the + key. If a_pKey is NULL then it will be associated + with the section, otherwise the key. This must be + a string in full comment form already (have a + comment character starting every line). + @param a_bForceReplace Should all existing values in a multi-key INI + file be replaced with this entry. This option has + no effect if not using multi-key files. The + difference between Delete/AddEntry and AddEntry + with a_bForceReplace = true, is that the load + order and comment will be preserved this way. + @param a_bCopyStrings Should copies of the strings be made or not. + If false then the pointers will be used as is. + */ + SI_Error AddEntry( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + const SI_CHAR * a_pValue, + const SI_CHAR * a_pComment, + bool a_bForceReplace, + bool a_bCopyStrings + ); + + /** Is the supplied character a whitespace character? */ + inline bool IsSpace(SI_CHAR ch) const { + return (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n'); + } + + /** Does the supplied character start a comment line? */ + inline bool IsComment(SI_CHAR ch) const { + return (ch == ';' || ch == '#'); + } + + + /** Skip over a newline character (or characters) for either DOS or UNIX */ + inline void SkipNewLine(SI_CHAR *& a_pData) const { + a_pData += (*a_pData == '\r' && *(a_pData+1) == '\n') ? 2 : 1; + } + + /** Make a copy of the supplied string, replacing the original pointer */ + SI_Error CopyString(const SI_CHAR *& a_pString); + + /** Delete a string from the copied strings buffer if necessary */ + void DeleteString(const SI_CHAR * a_pString); + + /** Internal use of our string comparison function */ + bool IsLess(const SI_CHAR * a_pLeft, const SI_CHAR * a_pRight) const { + const static SI_STRLESS isLess = SI_STRLESS(); + return isLess(a_pLeft, a_pRight); + } + + bool IsMultiLineTag(const SI_CHAR * a_pData) const; + bool IsMultiLineData(const SI_CHAR * a_pData) const; + bool LoadMultiLineText( + SI_CHAR *& a_pData, + const SI_CHAR *& a_pVal, + const SI_CHAR * a_pTagName, + bool a_bAllowBlankLinesInComment = false + ) const; + bool IsNewLineChar(SI_CHAR a_c) const; + + bool OutputMultiLineText( + OutputWriter & a_oOutput, + Converter & a_oConverter, + const SI_CHAR * a_pText + ) const; + +private: + /** Copy of the INI file data in our character format. This will be + modified when parsed to have NULL characters added after all + interesting string entries. All of the string pointers to sections, + keys and values point into this block of memory. + */ + SI_CHAR * m_pData; + + /** Length of the data that we have stored. Used when deleting strings + to determine if the string is stored here or in the allocated string + buffer. + */ + size_t m_uDataLen; + + /** File comment for this data, if one exists. */ + const SI_CHAR * m_pFileComment; + + /** Parsed INI data. Section -> (Key -> Value). */ + TSection m_data; + + /** This vector stores allocated memory for copies of strings that have + been supplied after the file load. It will be empty unless SetValue() + has been called. + */ + TNamesDepend m_strings; + + /** Is the format of our datafile UTF-8 or MBCS? */ + bool m_bStoreIsUtf8; + + /** Are multiple values permitted for the same key? */ + bool m_bAllowMultiKey; + + /** Are data values permitted to span multiple lines? */ + bool m_bAllowMultiLine; + + /** Should spaces be written out surrounding the equals sign? */ + bool m_bSpaces; + + /** Next order value, used to ensure sections and keys are output in the + same order that they are loaded/added. + */ + int m_nOrder; +}; + +// --------------------------------------------------------------------------- +// IMPLEMENTATION +// --------------------------------------------------------------------------- + +template +CSimpleIniTempl::CSimpleIniTempl( + bool a_bIsUtf8, + bool a_bAllowMultiKey, + bool a_bAllowMultiLine + ) + : m_pData(0) + , m_uDataLen(0) + , m_pFileComment(NULL) + , m_bStoreIsUtf8(a_bIsUtf8) + , m_bAllowMultiKey(a_bAllowMultiKey) + , m_bAllowMultiLine(a_bAllowMultiLine) + , m_bSpaces(true) + , m_nOrder(0) +{ } + +template +CSimpleIniTempl::~CSimpleIniTempl() +{ + Reset(); +} + +template +void +CSimpleIniTempl::Reset() +{ + // remove all data + delete[] m_pData; + m_pData = NULL; + m_uDataLen = 0; + m_pFileComment = NULL; + if (!m_data.empty()) { + m_data.erase(m_data.begin(), m_data.end()); + } + + // remove all strings + if (!m_strings.empty()) { + typename TNamesDepend::iterator i = m_strings.begin(); + for (; i != m_strings.end(); ++i) { + delete[] const_cast(i->pItem); + } + m_strings.erase(m_strings.begin(), m_strings.end()); + } +} + +template +SI_Error +CSimpleIniTempl::LoadFile( + const char * a_pszFile + ) +{ + FILE * fp = NULL; +#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE + fopen_s(&fp, a_pszFile, "rb"); +#else // !__STDC_WANT_SECURE_LIB__ + fp = fopen(a_pszFile, "rb"); +#endif // __STDC_WANT_SECURE_LIB__ + if (!fp) { + return SI_FILE; + } + SI_Error rc = LoadFile(fp); + fclose(fp); + return rc; +} + +#ifdef SI_HAS_WIDE_FILE +template +SI_Error +CSimpleIniTempl::LoadFile( + const SI_WCHAR_T * a_pwszFile + ) +{ +#ifdef _WIN32 + FILE * fp = NULL; +#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE + _wfopen_s(&fp, a_pwszFile, L"rb"); +#else // !__STDC_WANT_SECURE_LIB__ + fp = _wfopen(a_pwszFile, L"rb"); +#endif // __STDC_WANT_SECURE_LIB__ + if (!fp) return SI_FILE; + SI_Error rc = LoadFile(fp); + fclose(fp); + return rc; +#else // !_WIN32 (therefore SI_CONVERT_ICU) + char szFile[256]; + u_austrncpy(szFile, a_pwszFile, sizeof(szFile)); + return LoadFile(szFile); +#endif // _WIN32 +} +#endif // SI_HAS_WIDE_FILE + +template +SI_Error +CSimpleIniTempl::LoadFile( + FILE * a_fpFile + ) +{ + // load the raw file data + int retval = fseek(a_fpFile, 0, SEEK_END); + if (retval != 0) { + return SI_FILE; + } + long lSize = ftell(a_fpFile); + if (lSize < 0) { + return SI_FILE; + } + if (lSize == 0) { + return SI_OK; + } + + // allocate and ensure NULL terminated + char * pData = new char[lSize+1]; + if (!pData) { + return SI_NOMEM; + } + pData[lSize] = 0; + + // load data into buffer + fseek(a_fpFile, 0, SEEK_SET); + size_t uRead = fread(pData, sizeof(char), lSize, a_fpFile); + if (uRead != (size_t) lSize) { + delete[] pData; + return SI_FILE; + } + + // convert the raw data to unicode + SI_Error rc = LoadData(pData, uRead); + delete[] pData; + return rc; +} + +template +SI_Error +CSimpleIniTempl::LoadData( + const char * a_pData, + size_t a_uDataLen + ) +{ + SI_CONVERTER converter(m_bStoreIsUtf8); + + if (a_uDataLen == 0) { + return SI_OK; + } + + // consume the UTF-8 BOM if it exists + if (m_bStoreIsUtf8 && a_uDataLen >= 3) { + if (memcmp(a_pData, SI_UTF8_SIGNATURE, 3) == 0) { + a_pData += 3; + a_uDataLen -= 3; + } + } + + // determine the length of the converted data + size_t uLen = converter.SizeFromStore(a_pData, a_uDataLen); + if (uLen == (size_t)(-1)) { + return SI_FAIL; + } + + // allocate memory for the data, ensure that there is a NULL + // terminator wherever the converted data ends + SI_CHAR * pData = new SI_CHAR[uLen+1]; + if (!pData) { + return SI_NOMEM; + } + memset(pData, 0, sizeof(SI_CHAR)*(uLen+1)); + + // convert the data + if (!converter.ConvertFromStore(a_pData, a_uDataLen, pData, uLen)) { + delete[] pData; + return SI_FAIL; + } + + // parse it + const static SI_CHAR empty = 0; + SI_CHAR * pWork = pData; + const SI_CHAR * pSection = ∅ + const SI_CHAR * pItem = NULL; + const SI_CHAR * pVal = NULL; + const SI_CHAR * pComment = NULL; + + // We copy the strings if we are loading data into this class when we + // already have stored some. + bool bCopyStrings = (m_pData != NULL); + + // find a file comment if it exists, this is a comment that starts at the + // beginning of the file and continues until the first blank line. + SI_Error rc = FindFileComment(pWork, bCopyStrings); + if (rc < 0) return rc; + + // add every entry in the file to the data table + while (FindEntry(pWork, pSection, pItem, pVal, pComment)) { + rc = AddEntry(pSection, pItem, pVal, pComment, false, bCopyStrings); + if (rc < 0) return rc; + } + + // store these strings if we didn't copy them + if (bCopyStrings) { + delete[] pData; + } + else { + m_pData = pData; + m_uDataLen = uLen+1; + } + + return SI_OK; +} + +#ifdef SI_SUPPORT_IOSTREAMS +template +SI_Error +CSimpleIniTempl::LoadData( + std::istream & a_istream + ) +{ + std::string strData; + char szBuf[512]; + do { + a_istream.get(szBuf, sizeof(szBuf), '\0'); + strData.append(szBuf); + } + while (a_istream.good()); + return LoadData(strData); +} +#endif // SI_SUPPORT_IOSTREAMS + +template +SI_Error +CSimpleIniTempl::FindFileComment( + SI_CHAR *& a_pData, + bool a_bCopyStrings + ) +{ + // there can only be a single file comment + if (m_pFileComment) { + return SI_OK; + } + + // Load the file comment as multi-line text, this will modify all of + // the newline characters to be single \n chars + if (!LoadMultiLineText(a_pData, m_pFileComment, NULL, false)) { + return SI_OK; + } + + // copy the string if necessary + if (a_bCopyStrings) { + SI_Error rc = CopyString(m_pFileComment); + if (rc < 0) return rc; + } + + return SI_OK; +} + +template +bool +CSimpleIniTempl::FindEntry( + SI_CHAR *& a_pData, + const SI_CHAR *& a_pSection, + const SI_CHAR *& a_pKey, + const SI_CHAR *& a_pVal, + const SI_CHAR *& a_pComment + ) const +{ + a_pComment = NULL; + + SI_CHAR * pTrail = NULL; + while (*a_pData) { + // skip spaces and empty lines + while (*a_pData && IsSpace(*a_pData)) { + ++a_pData; + } + if (!*a_pData) { + break; + } + + // skip processing of comment lines but keep a pointer to + // the start of the comment. + if (IsComment(*a_pData)) { + LoadMultiLineText(a_pData, a_pComment, NULL, true); + continue; + } + + // process section names + if (*a_pData == '[') { + // skip leading spaces + ++a_pData; + while (*a_pData && IsSpace(*a_pData)) { + ++a_pData; + } + + // find the end of the section name (it may contain spaces) + // and convert it to lowercase as necessary + a_pSection = a_pData; + while (*a_pData && *a_pData != ']' && !IsNewLineChar(*a_pData)) { + ++a_pData; + } + + // if it's an invalid line, just skip it + if (*a_pData != ']') { + continue; + } + + // remove trailing spaces from the section + pTrail = a_pData - 1; + while (pTrail >= a_pSection && IsSpace(*pTrail)) { + --pTrail; + } + ++pTrail; + *pTrail = 0; + + // skip to the end of the line + ++a_pData; // safe as checked that it == ']' above + while (*a_pData && !IsNewLineChar(*a_pData)) { + ++a_pData; + } + + a_pKey = NULL; + a_pVal = NULL; + return true; + } + + // find the end of the key name (it may contain spaces) + // and convert it to lowercase as necessary + a_pKey = a_pData; + while (*a_pData && *a_pData != '=' && !IsNewLineChar(*a_pData)) { + ++a_pData; + } + + // if it's an invalid line, just skip it + if (*a_pData != '=') { + continue; + } + + // empty keys are invalid + if (a_pKey == a_pData) { + while (*a_pData && !IsNewLineChar(*a_pData)) { + ++a_pData; + } + continue; + } + + // remove trailing spaces from the key + pTrail = a_pData - 1; + while (pTrail >= a_pKey && IsSpace(*pTrail)) { + --pTrail; + } + ++pTrail; + *pTrail = 0; + + // skip leading whitespace on the value + ++a_pData; // safe as checked that it == '=' above + while (*a_pData && !IsNewLineChar(*a_pData) && IsSpace(*a_pData)) { + ++a_pData; + } + + // find the end of the value which is the end of this line + a_pVal = a_pData; + while (*a_pData && !IsNewLineChar(*a_pData)) { + ++a_pData; + } + + // remove trailing spaces from the value + pTrail = a_pData - 1; + if (*a_pData) { // prepare for the next round + SkipNewLine(a_pData); + } + while (pTrail >= a_pVal && IsSpace(*pTrail)) { + --pTrail; + } + ++pTrail; + *pTrail = 0; + + // check for multi-line entries + if (m_bAllowMultiLine && IsMultiLineTag(a_pVal)) { + // skip the "<<<" to get the tag that will end the multiline + const SI_CHAR * pTagName = a_pVal + 3; + return LoadMultiLineText(a_pData, a_pVal, pTagName); + } + + // return the standard entry + return true; + } + + return false; +} + +template +bool +CSimpleIniTempl::IsMultiLineTag( + const SI_CHAR * a_pVal + ) const +{ + // check for the "<<<" prefix for a multi-line entry + if (*a_pVal++ != '<') return false; + if (*a_pVal++ != '<') return false; + if (*a_pVal++ != '<') return false; + return true; +} + +template +bool +CSimpleIniTempl::IsMultiLineData( + const SI_CHAR * a_pData + ) const +{ + // data is multi-line if it has any of the following features: + // * whitespace prefix + // * embedded newlines + // * whitespace suffix + + // empty string + if (!*a_pData) { + return false; + } + + // check for prefix + if (IsSpace(*a_pData)) { + return true; + } + + // embedded newlines + while (*a_pData) { + if (IsNewLineChar(*a_pData)) { + return true; + } + ++a_pData; + } + + // check for suffix + if (IsSpace(*--a_pData)) { + return true; + } + + return false; +} + +template +bool +CSimpleIniTempl::IsNewLineChar( + SI_CHAR a_c + ) const +{ + return (a_c == '\n' || a_c == '\r'); +} + +template +bool +CSimpleIniTempl::LoadMultiLineText( + SI_CHAR *& a_pData, + const SI_CHAR *& a_pVal, + const SI_CHAR * a_pTagName, + bool a_bAllowBlankLinesInComment + ) const +{ + // we modify this data to strip all newlines down to a single '\n' + // character. This means that on Windows we need to strip out some + // characters which will make the data shorter. + // i.e. LINE1-LINE1\r\nLINE2-LINE2\0 will become + // LINE1-LINE1\nLINE2-LINE2\0 + // The pDataLine entry is the pointer to the location in memory that + // the current line needs to start to run following the existing one. + // This may be the same as pCurrLine in which case no move is needed. + SI_CHAR * pDataLine = a_pData; + SI_CHAR * pCurrLine; + + // value starts at the current line + a_pVal = a_pData; + + // find the end tag. This tag must start in column 1 and be + // followed by a newline. No whitespace removal is done while + // searching for this tag. + SI_CHAR cEndOfLineChar = *a_pData; + for(;;) { + // if we are loading comments then we need a comment character as + // the first character on every line + if (!a_pTagName && !IsComment(*a_pData)) { + // if we aren't allowing blank lines then we're done + if (!a_bAllowBlankLinesInComment) { + break; + } + + // if we are allowing blank lines then we only include them + // in this comment if another comment follows, so read ahead + // to find out. + SI_CHAR * pCurr = a_pData; + int nNewLines = 0; + while (IsSpace(*pCurr)) { + if (IsNewLineChar(*pCurr)) { + ++nNewLines; + SkipNewLine(pCurr); + } + else { + ++pCurr; + } + } + + // we have a comment, add the blank lines to the output + // and continue processing from here + if (IsComment(*pCurr)) { + for (; nNewLines > 0; --nNewLines) *pDataLine++ = '\n'; + a_pData = pCurr; + continue; + } + + // the comment ends here + break; + } + + // find the end of this line + pCurrLine = a_pData; + while (*a_pData && !IsNewLineChar(*a_pData)) ++a_pData; + + // move this line down to the location that it should be if necessary + if (pDataLine < pCurrLine) { + size_t nLen = (size_t) (a_pData - pCurrLine); + memmove(pDataLine, pCurrLine, nLen * sizeof(SI_CHAR)); + pDataLine[nLen] = '\0'; + } + + // end the line with a NULL + cEndOfLineChar = *a_pData; + *a_pData = 0; + + // if are looking for a tag then do the check now. This is done before + // checking for end of the data, so that if we have the tag at the end + // of the data then the tag is removed correctly. + if (a_pTagName && + (!IsLess(pDataLine, a_pTagName) && !IsLess(a_pTagName, pDataLine))) + { + break; + } + + // if we are at the end of the data then we just automatically end + // this entry and return the current data. + if (!cEndOfLineChar) { + return true; + } + + // otherwise we need to process this newline to ensure that it consists + // of just a single \n character. + pDataLine += (a_pData - pCurrLine); + *a_pData = cEndOfLineChar; + SkipNewLine(a_pData); + *pDataLine++ = '\n'; + } + + // if we didn't find a comment at all then return false + if (a_pVal == a_pData) { + a_pVal = NULL; + return false; + } + + // the data (which ends at the end of the last line) needs to be + // null-terminated BEFORE before the newline character(s). If the + // user wants a new line in the multi-line data then they need to + // add an empty line before the tag. + *--pDataLine = '\0'; + + // if looking for a tag and if we aren't at the end of the data, + // then move a_pData to the start of the next line. + if (a_pTagName && cEndOfLineChar) { + SI_ASSERT(IsNewLineChar(cEndOfLineChar)); + *a_pData = cEndOfLineChar; + SkipNewLine(a_pData); + } + + return true; +} + +template +SI_Error +CSimpleIniTempl::CopyString( + const SI_CHAR *& a_pString + ) +{ + size_t uLen = 0; + if (sizeof(SI_CHAR) == sizeof(char)) { + uLen = strlen((const char *)a_pString); + } + else if (sizeof(SI_CHAR) == sizeof(wchar_t)) { + uLen = wcslen((const wchar_t *)a_pString); + } + else { + for ( ; a_pString[uLen]; ++uLen) /*loop*/ ; + } + ++uLen; // NULL character + SI_CHAR * pCopy = new SI_CHAR[uLen]; + if (!pCopy) { + return SI_NOMEM; + } + memcpy(pCopy, a_pString, sizeof(SI_CHAR)*uLen); + m_strings.push_back(pCopy); + a_pString = pCopy; + return SI_OK; +} + +template +SI_Error +CSimpleIniTempl::AddEntry( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + const SI_CHAR * a_pValue, + const SI_CHAR * a_pComment, + bool a_bForceReplace, + bool a_bCopyStrings + ) +{ + SI_Error rc; + bool bInserted = false; + + SI_ASSERT(!a_pComment || IsComment(*a_pComment)); + + // if we are copying strings then make a copy of the comment now + // because we will need it when we add the entry. + if (a_bCopyStrings && a_pComment) { + rc = CopyString(a_pComment); + if (rc < 0) return rc; + } + + // create the section entry if necessary + typename TSection::iterator iSection = m_data.find(a_pSection); + if (iSection == m_data.end()) { + // if the section doesn't exist then we need a copy as the + // string needs to last beyond the end of this function + if (a_bCopyStrings) { + rc = CopyString(a_pSection); + if (rc < 0) return rc; + } + + // only set the comment if this is a section only entry + Entry oSection(a_pSection, ++m_nOrder); + if (a_pComment && (!a_pKey || !a_pValue)) { + oSection.pComment = a_pComment; + } + + typename TSection::value_type oEntry(oSection, TKeyVal()); + typedef typename TSection::iterator SectionIterator; + std::pair i = m_data.insert(oEntry); + iSection = i.first; + bInserted = true; + } + if (!a_pKey || !a_pValue) { + // section only entries are specified with pItem and pVal as NULL + return bInserted ? SI_INSERTED : SI_UPDATED; + } + + // check for existence of the key + TKeyVal & keyval = iSection->second; + typename TKeyVal::iterator iKey = keyval.find(a_pKey); + + // remove all existing entries but save the load order and + // comment of the first entry + int nLoadOrder = ++m_nOrder; + if (iKey != keyval.end() && m_bAllowMultiKey && a_bForceReplace) { + const SI_CHAR * pComment = NULL; + while (iKey != keyval.end() && !IsLess(a_pKey, iKey->first.pItem)) { + if (iKey->first.nOrder < nLoadOrder) { + nLoadOrder = iKey->first.nOrder; + pComment = iKey->first.pComment; + } + ++iKey; + } + if (pComment) { + DeleteString(a_pComment); + a_pComment = pComment; + CopyString(a_pComment); + } + Delete(a_pSection, a_pKey); + iKey = keyval.end(); + } + + // make string copies if necessary + bool bForceCreateNewKey = m_bAllowMultiKey && !a_bForceReplace; + if (a_bCopyStrings) { + if (bForceCreateNewKey || iKey == keyval.end()) { + // if the key doesn't exist then we need a copy as the + // string needs to last beyond the end of this function + // because we will be inserting the key next + rc = CopyString(a_pKey); + if (rc < 0) return rc; + } + + // we always need a copy of the value + rc = CopyString(a_pValue); + if (rc < 0) return rc; + } + + // create the key entry + if (iKey == keyval.end() || bForceCreateNewKey) { + Entry oKey(a_pKey, nLoadOrder); + if (a_pComment) { + oKey.pComment = a_pComment; + } + typename TKeyVal::value_type oEntry(oKey, static_cast(NULL)); + iKey = keyval.insert(oEntry); + bInserted = true; + } + iKey->second = a_pValue; + return bInserted ? SI_INSERTED : SI_UPDATED; +} + +template +const SI_CHAR * +CSimpleIniTempl::GetValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + const SI_CHAR * a_pDefault, + bool * a_pHasMultiple + ) const +{ + if (a_pHasMultiple) { + *a_pHasMultiple = false; + } + if (!a_pSection || !a_pKey) { + return a_pDefault; + } + typename TSection::const_iterator iSection = m_data.find(a_pSection); + if (iSection == m_data.end()) { + return a_pDefault; + } + typename TKeyVal::const_iterator iKeyVal = iSection->second.find(a_pKey); + if (iKeyVal == iSection->second.end()) { + return a_pDefault; + } + + // check for multiple entries with the same key + if (m_bAllowMultiKey && a_pHasMultiple) { + typename TKeyVal::const_iterator iTemp = iKeyVal; + if (++iTemp != iSection->second.end()) { + if (!IsLess(a_pKey, iTemp->first.pItem)) { + *a_pHasMultiple = true; + } + } + } + + return iKeyVal->second; +} + +template +long +CSimpleIniTempl::GetLongValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + long a_nDefault, + bool * a_pHasMultiple + ) const +{ + // return the default if we don't have a value + const SI_CHAR * pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple); + if (!pszValue || !*pszValue) return a_nDefault; + + // convert to UTF-8/MBCS which for a numeric value will be the same as ASCII + char szValue[64] = { 0 }; + SI_CONVERTER c(m_bStoreIsUtf8); + if (!c.ConvertToStore(pszValue, szValue, sizeof(szValue))) { + return a_nDefault; + } + + // handle the value as hex if prefaced with "0x" + long nValue = a_nDefault; + char * pszSuffix = szValue; + if (szValue[0] == '0' && (szValue[1] == 'x' || szValue[1] == 'X')) { + if (!szValue[2]) return a_nDefault; + nValue = strtol(&szValue[2], &pszSuffix, 16); + } + else { + nValue = strtol(szValue, &pszSuffix, 10); + } + + // any invalid strings will return the default value + if (*pszSuffix) { + return a_nDefault; + } + + return nValue; +} + +template +SI_Error +CSimpleIniTempl::SetLongValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + long a_nValue, + const SI_CHAR * a_pComment, + bool a_bUseHex, + bool a_bForceReplace + ) +{ + // use SetValue to create sections + if (!a_pSection || !a_pKey) return SI_FAIL; + + // convert to an ASCII string + char szInput[64]; +#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE + sprintf_s(szInput, a_bUseHex ? "0x%lx" : "%ld", a_nValue); +#else // !__STDC_WANT_SECURE_LIB__ + sprintf(szInput, a_bUseHex ? "0x%lx" : "%ld", a_nValue); +#endif // __STDC_WANT_SECURE_LIB__ + + // convert to output text + SI_CHAR szOutput[64]; + SI_CONVERTER c(m_bStoreIsUtf8); + c.ConvertFromStore(szInput, strlen(szInput) + 1, + szOutput, sizeof(szOutput) / sizeof(SI_CHAR)); + + // actually add it + return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true); +} + +template +double +CSimpleIniTempl::GetDoubleValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + double a_nDefault, + bool * a_pHasMultiple + ) const +{ + // return the default if we don't have a value + const SI_CHAR * pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple); + if (!pszValue || !*pszValue) return a_nDefault; + + // convert to UTF-8/MBCS which for a numeric value will be the same as ASCII + char szValue[64] = { 0 }; + SI_CONVERTER c(m_bStoreIsUtf8); + if (!c.ConvertToStore(pszValue, szValue, sizeof(szValue))) { + return a_nDefault; + } + + char * pszSuffix = NULL; + double nValue = strtod(szValue, &pszSuffix); + + // any invalid strings will return the default value + if (!pszSuffix || *pszSuffix) { + return a_nDefault; + } + + return nValue; +} + +template +SI_Error +CSimpleIniTempl::SetDoubleValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + double a_nValue, + const SI_CHAR * a_pComment, + bool a_bForceReplace + ) +{ + // use SetValue to create sections + if (!a_pSection || !a_pKey) return SI_FAIL; + + // convert to an ASCII string + char szInput[64]; +#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE + sprintf_s(szInput, "%f", a_nValue); +#else // !__STDC_WANT_SECURE_LIB__ + sprintf(szInput, "%f", a_nValue); +#endif // __STDC_WANT_SECURE_LIB__ + + // convert to output text + SI_CHAR szOutput[64]; + SI_CONVERTER c(m_bStoreIsUtf8); + c.ConvertFromStore(szInput, strlen(szInput) + 1, + szOutput, sizeof(szOutput) / sizeof(SI_CHAR)); + + // actually add it + return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true); +} + +template +bool +CSimpleIniTempl::GetBoolValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + bool a_bDefault, + bool * a_pHasMultiple + ) const +{ + // return the default if we don't have a value + const SI_CHAR * pszValue = GetValue(a_pSection, a_pKey, NULL, a_pHasMultiple); + if (!pszValue || !*pszValue) return a_bDefault; + + // we only look at the minimum number of characters + switch (pszValue[0]) { + case 't': case 'T': // true + case 'y': case 'Y': // yes + case '1': // 1 (one) + return true; + + case 'f': case 'F': // false + case 'n': case 'N': // no + case '0': // 0 (zero) + return false; + + case 'o': case 'O': + if (pszValue[1] == 'n' || pszValue[1] == 'N') return true; // on + if (pszValue[1] == 'f' || pszValue[1] == 'F') return false; // off + break; + } + + // no recognized value, return the default + return a_bDefault; +} + +template +SI_Error +CSimpleIniTempl::SetBoolValue( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + bool a_bValue, + const SI_CHAR * a_pComment, + bool a_bForceReplace + ) +{ + // use SetValue to create sections + if (!a_pSection || !a_pKey) return SI_FAIL; + + // convert to an ASCII string + const char * pszInput = a_bValue ? "true" : "false"; + + // convert to output text + SI_CHAR szOutput[64]; + SI_CONVERTER c(m_bStoreIsUtf8); + c.ConvertFromStore(pszInput, strlen(pszInput) + 1, + szOutput, sizeof(szOutput) / sizeof(SI_CHAR)); + + // actually add it + return AddEntry(a_pSection, a_pKey, szOutput, a_pComment, a_bForceReplace, true); +} + +template +bool +CSimpleIniTempl::GetAllValues( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + TNamesDepend & a_values + ) const +{ + a_values.clear(); + + if (!a_pSection || !a_pKey) { + return false; + } + typename TSection::const_iterator iSection = m_data.find(a_pSection); + if (iSection == m_data.end()) { + return false; + } + + auto entries = iSection->second.equal_range(a_pKey); + if (entries.first == entries.second) + return false; + + if (m_bAllowMultiKey) { + for(auto e = entries.first; e != entries.second; ++e) + a_values.push_back(Entry(e->second, e->first.pComment, e->first.nOrder)); + } else { + --entries.second; + a_values.push_back(Entry(entries.second->second, entries.second->first.pComment, entries.second->first.nOrder)); + } + + return true; +} + +template +int +CSimpleIniTempl::GetSectionSize( + const SI_CHAR * a_pSection + ) const +{ + if (!a_pSection) { + return -1; + } + + typename TSection::const_iterator iSection = m_data.find(a_pSection); + if (iSection == m_data.end()) { + return -1; + } + const TKeyVal & section = iSection->second; + + // if multi-key isn't permitted then the section size is + // the number of keys that we have. + if (!m_bAllowMultiKey || section.empty()) { + return (int) section.size(); + } + + // otherwise we need to count them + int nCount = 0; + const SI_CHAR * pLastKey = NULL; + typename TKeyVal::const_iterator iKeyVal = section.begin(); + for (int n = 0; iKeyVal != section.end(); ++iKeyVal, ++n) { + if (!pLastKey || IsLess(pLastKey, iKeyVal->first.pItem)) { + ++nCount; + pLastKey = iKeyVal->first.pItem; + } + } + return nCount; +} + +template +const typename CSimpleIniTempl::TKeyVal * +CSimpleIniTempl::GetSection( + const SI_CHAR * a_pSection + ) const +{ + if (a_pSection) { + typename TSection::const_iterator i = m_data.find(a_pSection); + if (i != m_data.end()) { + return &(i->second); + } + } + return 0; +} + +template +void +CSimpleIniTempl::GetAllSections( + TNamesDepend & a_names + ) const +{ + a_names.clear(); + typename TSection::const_iterator i = m_data.begin(); + for (int n = 0; i != m_data.end(); ++i, ++n ) { + a_names.push_back(i->first); + } +} + +template +bool +CSimpleIniTempl::GetAllKeys( + const SI_CHAR * a_pSection, + TNamesDepend & a_names + ) const +{ + a_names.clear(); + + if (!a_pSection) { + return false; + } + + typename TSection::const_iterator iSection = m_data.find(a_pSection); + if (iSection == m_data.end()) { + return false; + } + + const TKeyVal & section = iSection->second; + const SI_CHAR * pLastKey = NULL; + typename TKeyVal::const_iterator iKeyVal = section.begin(); + for (int n = 0; iKeyVal != section.end(); ++iKeyVal, ++n ) { + if (!pLastKey || IsLess(pLastKey, iKeyVal->first.pItem)) { + a_names.push_back(iKeyVal->first); + pLastKey = iKeyVal->first.pItem; + } + } + + return true; +} + +template +SI_Error +CSimpleIniTempl::SaveFile( + const char * a_pszFile, + bool a_bAddSignature + ) const +{ + FILE * fp = NULL; +#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE + fopen_s(&fp, a_pszFile, "wb"); +#else // !__STDC_WANT_SECURE_LIB__ + fp = fopen(a_pszFile, "wb"); +#endif // __STDC_WANT_SECURE_LIB__ + if (!fp) return SI_FILE; + SI_Error rc = SaveFile(fp, a_bAddSignature); + fclose(fp); + return rc; +} + +#ifdef SI_HAS_WIDE_FILE +template +SI_Error +CSimpleIniTempl::SaveFile( + const SI_WCHAR_T * a_pwszFile, + bool a_bAddSignature + ) const +{ +#ifdef _WIN32 + FILE * fp = NULL; +#if __STDC_WANT_SECURE_LIB__ && !_WIN32_WCE + _wfopen_s(&fp, a_pwszFile, L"wb"); +#else // !__STDC_WANT_SECURE_LIB__ + fp = _wfopen(a_pwszFile, L"wb"); +#endif // __STDC_WANT_SECURE_LIB__ + if (!fp) return SI_FILE; + SI_Error rc = SaveFile(fp, a_bAddSignature); + fclose(fp); + return rc; +#else // !_WIN32 (therefore SI_CONVERT_ICU) + char szFile[256]; + u_austrncpy(szFile, a_pwszFile, sizeof(szFile)); + return SaveFile(szFile, a_bAddSignature); +#endif // _WIN32 +} +#endif // SI_HAS_WIDE_FILE + +template +SI_Error +CSimpleIniTempl::SaveFile( + FILE * a_pFile, + bool a_bAddSignature + ) const +{ + FileWriter writer(a_pFile); + return Save(writer, a_bAddSignature); +} + +template +SI_Error +CSimpleIniTempl::Save( + OutputWriter & a_oOutput, + bool a_bAddSignature + ) const +{ + Converter convert(m_bStoreIsUtf8); + + // add the UTF-8 signature if it is desired + if (m_bStoreIsUtf8 && a_bAddSignature) { + a_oOutput.Write(SI_UTF8_SIGNATURE); + } + + // get all of the sections sorted in load order + TNamesDepend oSections; + GetAllSections(oSections); +#if defined(_MSC_VER) && _MSC_VER <= 1200 + oSections.sort(); +#elif defined(__BORLANDC__) + oSections.sort(Entry::LoadOrder()); +#else + oSections.sort(typename Entry::LoadOrder()); +#endif + + // write the file comment if we have one + bool bNeedNewLine = false; + if (m_pFileComment) { + if (!OutputMultiLineText(a_oOutput, convert, m_pFileComment)) { + return SI_FAIL; + } + bNeedNewLine = true; + } + + // iterate through our sections and output the data + typename TNamesDepend::const_iterator iSection = oSections.begin(); + for ( ; iSection != oSections.end(); ++iSection ) { + // write out the comment if there is one + if (iSection->pComment) { + if (bNeedNewLine) { + a_oOutput.Write(SI_NEWLINE_A); + a_oOutput.Write(SI_NEWLINE_A); + } + if (!OutputMultiLineText(a_oOutput, convert, iSection->pComment)) { + return SI_FAIL; + } + bNeedNewLine = false; + } + + if (bNeedNewLine) { + a_oOutput.Write(SI_NEWLINE_A); + a_oOutput.Write(SI_NEWLINE_A); + bNeedNewLine = false; + } + + // write the section (unless there is no section name) + if (*iSection->pItem) { + if (!convert.ConvertToStore(iSection->pItem)) { + return SI_FAIL; + } + a_oOutput.Write("["); + a_oOutput.Write(convert.Data()); + a_oOutput.Write("]"); + a_oOutput.Write(SI_NEWLINE_A); + } + + // get all of the keys sorted in load order + TNamesDepend oKeys; + GetAllKeys(iSection->pItem, oKeys); +#if defined(_MSC_VER) && _MSC_VER <= 1200 + oKeys.sort(); +#elif defined(__BORLANDC__) + oKeys.sort(Entry::LoadOrder()); +#else + oKeys.sort(typename Entry::LoadOrder()); +#endif + + // write all keys and values + typename TNamesDepend::const_iterator iKey = oKeys.begin(); + for ( ; iKey != oKeys.end(); ++iKey) { + // get all values for this key + TNamesDepend oValues; + GetAllValues(iSection->pItem, iKey->pItem, oValues); + + typename TNamesDepend::const_iterator iValue = oValues.begin(); + for ( ; iValue != oValues.end(); ++iValue) { + // write out the comment if there is one + if (iValue->pComment) { + a_oOutput.Write(SI_NEWLINE_A); + if (!OutputMultiLineText(a_oOutput, convert, iValue->pComment)) { + return SI_FAIL; + } + } + + // write the key + if (!convert.ConvertToStore(iKey->pItem)) { + return SI_FAIL; + } + a_oOutput.Write(convert.Data()); + + // write the value + if (!convert.ConvertToStore(iValue->pItem)) { + return SI_FAIL; + } + a_oOutput.Write(m_bSpaces ? " = " : "="); + if (m_bAllowMultiLine && IsMultiLineData(iValue->pItem)) { + // multi-line data needs to be processed specially to ensure + // that we use the correct newline format for the current system + a_oOutput.Write("<<pItem)) { + return SI_FAIL; + } + a_oOutput.Write("END_OF_TEXT"); + } + else { + a_oOutput.Write(convert.Data()); + } + a_oOutput.Write(SI_NEWLINE_A); + } + } + + bNeedNewLine = true; + } + + return SI_OK; +} + +template +bool +CSimpleIniTempl::OutputMultiLineText( + OutputWriter & a_oOutput, + Converter & a_oConverter, + const SI_CHAR * a_pText + ) const +{ + const SI_CHAR * pEndOfLine; + SI_CHAR cEndOfLineChar = *a_pText; + while (cEndOfLineChar) { + // find the end of this line + pEndOfLine = a_pText; + for (; *pEndOfLine && *pEndOfLine != '\n'; ++pEndOfLine) /*loop*/ ; + cEndOfLineChar = *pEndOfLine; + + // temporarily null terminate, convert and output the line + *const_cast(pEndOfLine) = 0; + if (!a_oConverter.ConvertToStore(a_pText)) { + return false; + } + *const_cast(pEndOfLine) = cEndOfLineChar; + a_pText += (pEndOfLine - a_pText) + 1; + a_oOutput.Write(a_oConverter.Data()); + a_oOutput.Write(SI_NEWLINE_A); + } + return true; +} + +template +bool +CSimpleIniTempl::Delete( + const SI_CHAR * a_pSection, + const SI_CHAR * a_pKey, + bool a_bRemoveEmpty + ) +{ + if (!a_pSection) { + return false; + } + + typename TSection::iterator iSection = m_data.find(a_pSection); + if (iSection == m_data.end()) { + return false; + } + + // remove a single key if we have a keyname + if (a_pKey) { + typename TKeyVal::iterator iKeyVal = iSection->second.find(a_pKey); + if (iKeyVal == iSection->second.end()) { + return false; + } + + // remove any copied strings and then the key + typename TKeyVal::iterator iDelete; + do { + iDelete = iKeyVal++; + + DeleteString(iDelete->first.pItem); + DeleteString(iDelete->second); + iSection->second.erase(iDelete); + } + while (iKeyVal != iSection->second.end() + && !IsLess(a_pKey, iKeyVal->first.pItem)); + + // done now if the section is not empty or we are not pruning away + // the empty sections. Otherwise let it fall through into the section + // deletion code + if (!a_bRemoveEmpty || !iSection->second.empty()) { + return true; + } + } + else { + // delete all copied strings from this section. The actual + // entries will be removed when the section is removed. + typename TKeyVal::iterator iKeyVal = iSection->second.begin(); + for ( ; iKeyVal != iSection->second.end(); ++iKeyVal) { + DeleteString(iKeyVal->first.pItem); + DeleteString(iKeyVal->second); + } + } + + // delete the section itself + DeleteString(iSection->first.pItem); + m_data.erase(iSection); + + return true; +} + +template +void +CSimpleIniTempl::DeleteString( + const SI_CHAR * a_pString + ) +{ + // strings may exist either inside the data block, or they will be + // individually allocated and stored in m_strings. We only physically + // delete those stored in m_strings. + if (a_pString < m_pData || a_pString >= m_pData + m_uDataLen) { + typename TNamesDepend::iterator i = m_strings.begin(); + for (;i != m_strings.end(); ++i) { + if (a_pString == i->pItem) { + delete[] const_cast(i->pItem); + m_strings.erase(i); + break; + } + } + } +} + +// --------------------------------------------------------------------------- +// CONVERSION FUNCTIONS +// --------------------------------------------------------------------------- + +// Defines the conversion classes for different libraries. Before including +// SimpleIni.h, set the converter that you wish you use by defining one of the +// following symbols. +// +// SI_CONVERT_GENERIC Use the Unicode reference conversion library in +// the accompanying files ConvertUTF.h/c +// SI_CONVERT_ICU Use the IBM ICU conversion library. Requires +// ICU headers on include path and icuuc.lib +// SI_CONVERT_WIN32 Use the Win32 API functions for conversion. + +#if !defined(SI_CONVERT_GENERIC) && !defined(SI_CONVERT_WIN32) && !defined(SI_CONVERT_ICU) +# ifdef _WIN32 +# define SI_CONVERT_WIN32 +# else +# define SI_CONVERT_GENERIC +# endif +#endif + +/** + * Generic case-sensitive less than comparison. This class returns numerically + * ordered ASCII case-sensitive text for all possible sizes and types of + * SI_CHAR. + */ +template +struct SI_GenericCase { + bool operator()(const SI_CHAR * pLeft, const SI_CHAR * pRight) const { + long cmp; + for ( ;*pLeft && *pRight; ++pLeft, ++pRight) { + cmp = (long) *pLeft - (long) *pRight; + if (cmp != 0) { + return cmp < 0; + } + } + return *pRight != 0; + } +}; + +/** + * Generic ASCII case-insensitive less than comparison. This class returns + * numerically ordered ASCII case-insensitive text for all possible sizes + * and types of SI_CHAR. It is not safe for MBCS text comparison where + * ASCII A-Z characters are used in the encoding of multi-byte characters. + */ +template +struct SI_GenericNoCase { + inline SI_CHAR locase(SI_CHAR ch) const { + return (ch < 'A' || ch > 'Z') ? ch : (ch - 'A' + 'a'); + } + bool operator()(const SI_CHAR * pLeft, const SI_CHAR * pRight) const { + long cmp; + for ( ;*pLeft && *pRight; ++pLeft, ++pRight) { + cmp = (long) locase(*pLeft) - (long) locase(*pRight); + if (cmp != 0) { + return cmp < 0; + } + } + return *pRight != 0; + } +}; + +/** + * Null conversion class for MBCS/UTF-8 to char (or equivalent). + */ +template +class SI_ConvertA { + bool m_bStoreIsUtf8; +protected: + SI_ConvertA() { } +public: + SI_ConvertA(bool a_bStoreIsUtf8) : m_bStoreIsUtf8(a_bStoreIsUtf8) { } + + /* copy and assignment */ + SI_ConvertA(const SI_ConvertA & rhs) { operator=(rhs); } + SI_ConvertA & operator=(const SI_ConvertA & rhs) { + m_bStoreIsUtf8 = rhs.m_bStoreIsUtf8; + return *this; + } + + /** Calculate the number of SI_CHAR required for converting the input + * from the storage format. The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData Data in storage format to be converted to SI_CHAR. + * @param a_uInputDataLen Length of storage format data in bytes. This + * must be the actual length of the data, including + * NULL byte if NULL terminated string is required. + * @return Number of SI_CHAR required by the string when + * converted. If there are embedded NULL bytes in the + * input data, only the string up and not including + * the NULL byte will be converted. + * @return -1 cast to size_t on a conversion error. + */ + size_t SizeFromStore( + const char * a_pInputData, + size_t a_uInputDataLen) + { + (void)a_pInputData; + SI_ASSERT(a_uInputDataLen != (size_t) -1); + + // ASCII/MBCS/UTF-8 needs no conversion + return a_uInputDataLen; + } + + /** Convert the input string from the storage format to SI_CHAR. + * The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData Data in storage format to be converted to SI_CHAR. + * @param a_uInputDataLen Length of storage format data in bytes. This + * must be the actual length of the data, including + * NULL byte if NULL terminated string is required. + * @param a_pOutputData Pointer to the output buffer to received the + * converted data. + * @param a_uOutputDataSize Size of the output buffer in SI_CHAR. + * @return true if all of the input data was successfully + * converted. + */ + bool ConvertFromStore( + const char * a_pInputData, + size_t a_uInputDataLen, + SI_CHAR * a_pOutputData, + size_t a_uOutputDataSize) + { + // ASCII/MBCS/UTF-8 needs no conversion + if (a_uInputDataLen > a_uOutputDataSize) { + return false; + } + memcpy(a_pOutputData, a_pInputData, a_uInputDataLen); + return true; + } + + /** Calculate the number of char required by the storage format of this + * data. The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData NULL terminated string to calculate the number of + * bytes required to be converted to storage format. + * @return Number of bytes required by the string when + * converted to storage format. This size always + * includes space for the terminating NULL character. + * @return -1 cast to size_t on a conversion error. + */ + size_t SizeToStore( + const SI_CHAR * a_pInputData) + { + // ASCII/MBCS/UTF-8 needs no conversion + return strlen((const char *)a_pInputData) + 1; + } + + /** Convert the input string to the storage format of this data. + * The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData NULL terminated source string to convert. All of + * the data will be converted including the + * terminating NULL character. + * @param a_pOutputData Pointer to the buffer to receive the converted + * string. + * @param a_uOutputDataSize Size of the output buffer in char. + * @return true if all of the input data, including the + * terminating NULL character was successfully + * converted. + */ + bool ConvertToStore( + const SI_CHAR * a_pInputData, + char * a_pOutputData, + size_t a_uOutputDataSize) + { + // calc input string length (SI_CHAR type and size independent) + size_t uInputLen = strlen((const char *)a_pInputData) + 1; + if (uInputLen > a_uOutputDataSize) { + return false; + } + + // ascii/UTF-8 needs no conversion + memcpy(a_pOutputData, a_pInputData, uInputLen); + return true; + } +}; + + +// --------------------------------------------------------------------------- +// SI_CONVERT_GENERIC +// --------------------------------------------------------------------------- +#ifdef SI_CONVERT_GENERIC + +#define SI_Case SI_GenericCase +#define SI_NoCase SI_GenericNoCase + +#include +#include "ConvertUTF.h" + +/** + * Converts UTF-8 to a wchar_t (or equivalent) using the Unicode reference + * library functions. This can be used on all platforms. + */ +template +class SI_ConvertW { + bool m_bStoreIsUtf8; +protected: + SI_ConvertW() { } +public: + SI_ConvertW(bool a_bStoreIsUtf8) : m_bStoreIsUtf8(a_bStoreIsUtf8) { } + + /* copy and assignment */ + SI_ConvertW(const SI_ConvertW & rhs) { operator=(rhs); } + SI_ConvertW & operator=(const SI_ConvertW & rhs) { + m_bStoreIsUtf8 = rhs.m_bStoreIsUtf8; + return *this; + } + + /** Calculate the number of SI_CHAR required for converting the input + * from the storage format. The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData Data in storage format to be converted to SI_CHAR. + * @param a_uInputDataLen Length of storage format data in bytes. This + * must be the actual length of the data, including + * NULL byte if NULL terminated string is required. + * @return Number of SI_CHAR required by the string when + * converted. If there are embedded NULL bytes in the + * input data, only the string up and not including + * the NULL byte will be converted. + * @return -1 cast to size_t on a conversion error. + */ + size_t SizeFromStore( + const char * a_pInputData, + size_t a_uInputDataLen) + { + SI_ASSERT(a_uInputDataLen != (size_t) -1); + + if (m_bStoreIsUtf8) { + // worst case scenario for UTF-8 to wchar_t is 1 char -> 1 wchar_t + // so we just return the same number of characters required as for + // the source text. + return a_uInputDataLen; + } + +#if defined(SI_NO_MBSTOWCS_NULL) || (!defined(_MSC_VER) && !defined(_linux)) + // fall back processing for platforms that don't support a NULL dest to mbstowcs + // worst case scenario is 1:1, this will be a sufficient buffer size + return a_uInputDataLen; +#else + // get the actual required buffer size + return mbstowcs(NULL, a_pInputData, a_uInputDataLen); +#endif + } + + /** Convert the input string from the storage format to SI_CHAR. + * The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData Data in storage format to be converted to SI_CHAR. + * @param a_uInputDataLen Length of storage format data in bytes. This + * must be the actual length of the data, including + * NULL byte if NULL terminated string is required. + * @param a_pOutputData Pointer to the output buffer to received the + * converted data. + * @param a_uOutputDataSize Size of the output buffer in SI_CHAR. + * @return true if all of the input data was successfully + * converted. + */ + bool ConvertFromStore( + const char * a_pInputData, + size_t a_uInputDataLen, + SI_CHAR * a_pOutputData, + size_t a_uOutputDataSize) + { + if (m_bStoreIsUtf8) { + // This uses the Unicode reference implementation to do the + // conversion from UTF-8 to wchar_t. The required files are + // ConvertUTF.h and ConvertUTF.c which should be included in + // the distribution but are publically available from unicode.org + // at http://www.unicode.org/Public/PROGRAMS/CVTUTF/ + ConversionResult retval; + const UTF8 * pUtf8 = (const UTF8 *) a_pInputData; + if (sizeof(wchar_t) == sizeof(UTF32)) { + UTF32 * pUtf32 = (UTF32 *) a_pOutputData; + retval = ConvertUTF8toUTF32( + &pUtf8, pUtf8 + a_uInputDataLen, + &pUtf32, pUtf32 + a_uOutputDataSize, + lenientConversion); + } + else if (sizeof(wchar_t) == sizeof(UTF16)) { + UTF16 * pUtf16 = (UTF16 *) a_pOutputData; + retval = ConvertUTF8toUTF16( + &pUtf8, pUtf8 + a_uInputDataLen, + &pUtf16, pUtf16 + a_uOutputDataSize, + lenientConversion); + } + return retval == conversionOK; + } + + // convert to wchar_t + size_t retval = mbstowcs(a_pOutputData, + a_pInputData, a_uOutputDataSize); + return retval != (size_t)(-1); + } + + /** Calculate the number of char required by the storage format of this + * data. The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData NULL terminated string to calculate the number of + * bytes required to be converted to storage format. + * @return Number of bytes required by the string when + * converted to storage format. This size always + * includes space for the terminating NULL character. + * @return -1 cast to size_t on a conversion error. + */ + size_t SizeToStore( + const SI_CHAR * a_pInputData) + { + if (m_bStoreIsUtf8) { + // worst case scenario for wchar_t to UTF-8 is 1 wchar_t -> 6 char + size_t uLen = 0; + while (a_pInputData[uLen]) { + ++uLen; + } + return (6 * uLen) + 1; + } + else { + size_t uLen = wcstombs(NULL, a_pInputData, 0); + if (uLen == (size_t)(-1)) { + return uLen; + } + return uLen + 1; // include NULL terminator + } + } + + /** Convert the input string to the storage format of this data. + * The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData NULL terminated source string to convert. All of + * the data will be converted including the + * terminating NULL character. + * @param a_pOutputData Pointer to the buffer to receive the converted + * string. + * @param a_uOutputDataSize Size of the output buffer in char. + * @return true if all of the input data, including the + * terminating NULL character was successfully + * converted. + */ + bool ConvertToStore( + const SI_CHAR * a_pInputData, + char * a_pOutputData, + size_t a_uOutputDataSize + ) + { + if (m_bStoreIsUtf8) { + // calc input string length (SI_CHAR type and size independent) + size_t uInputLen = 0; + while (a_pInputData[uInputLen]) { + ++uInputLen; + } + ++uInputLen; // include the NULL char + + // This uses the Unicode reference implementation to do the + // conversion from wchar_t to UTF-8. The required files are + // ConvertUTF.h and ConvertUTF.c which should be included in + // the distribution but are publically available from unicode.org + // at http://www.unicode.org/Public/PROGRAMS/CVTUTF/ + ConversionResult retval; + UTF8 * pUtf8 = (UTF8 *) a_pOutputData; + if (sizeof(wchar_t) == sizeof(UTF32)) { + const UTF32 * pUtf32 = (const UTF32 *) a_pInputData; + retval = ConvertUTF32toUTF8( + &pUtf32, pUtf32 + uInputLen, + &pUtf8, pUtf8 + a_uOutputDataSize, + lenientConversion); + } + else if (sizeof(wchar_t) == sizeof(UTF16)) { + const UTF16 * pUtf16 = (const UTF16 *) a_pInputData; + retval = ConvertUTF16toUTF8( + &pUtf16, pUtf16 + uInputLen, + &pUtf8, pUtf8 + a_uOutputDataSize, + lenientConversion); + } + return retval == conversionOK; + } + else { + size_t retval = wcstombs(a_pOutputData, + a_pInputData, a_uOutputDataSize); + return retval != (size_t) -1; + } + } +}; + +#endif // SI_CONVERT_GENERIC + + +// --------------------------------------------------------------------------- +// SI_CONVERT_ICU +// --------------------------------------------------------------------------- +#ifdef SI_CONVERT_ICU + +#define SI_Case SI_GenericCase +#define SI_NoCase SI_GenericNoCase + +#include + +/** + * Converts MBCS/UTF-8 to UChar using ICU. This can be used on all platforms. + */ +template +class SI_ConvertW { + const char * m_pEncoding; + UConverter * m_pConverter; +protected: + SI_ConvertW() : m_pEncoding(NULL), m_pConverter(NULL) { } +public: + SI_ConvertW(bool a_bStoreIsUtf8) : m_pConverter(NULL) { + m_pEncoding = a_bStoreIsUtf8 ? "UTF-8" : NULL; + } + + /* copy and assignment */ + SI_ConvertW(const SI_ConvertW & rhs) { operator=(rhs); } + SI_ConvertW & operator=(const SI_ConvertW & rhs) { + m_pEncoding = rhs.m_pEncoding; + m_pConverter = NULL; + return *this; + } + ~SI_ConvertW() { if (m_pConverter) ucnv_close(m_pConverter); } + + /** Calculate the number of UChar required for converting the input + * from the storage format. The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData Data in storage format to be converted to UChar. + * @param a_uInputDataLen Length of storage format data in bytes. This + * must be the actual length of the data, including + * NULL byte if NULL terminated string is required. + * @return Number of UChar required by the string when + * converted. If there are embedded NULL bytes in the + * input data, only the string up and not including + * the NULL byte will be converted. + * @return -1 cast to size_t on a conversion error. + */ + size_t SizeFromStore( + const char * a_pInputData, + size_t a_uInputDataLen) + { + SI_ASSERT(a_uInputDataLen != (size_t) -1); + + UErrorCode nError; + + if (!m_pConverter) { + nError = U_ZERO_ERROR; + m_pConverter = ucnv_open(m_pEncoding, &nError); + if (U_FAILURE(nError)) { + return (size_t) -1; + } + } + + nError = U_ZERO_ERROR; + int32_t nLen = ucnv_toUChars(m_pConverter, NULL, 0, + a_pInputData, (int32_t) a_uInputDataLen, &nError); + if (U_FAILURE(nError) && nError != U_BUFFER_OVERFLOW_ERROR) { + return (size_t) -1; + } + + return (size_t) nLen; + } + + /** Convert the input string from the storage format to UChar. + * The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData Data in storage format to be converted to UChar. + * @param a_uInputDataLen Length of storage format data in bytes. This + * must be the actual length of the data, including + * NULL byte if NULL terminated string is required. + * @param a_pOutputData Pointer to the output buffer to received the + * converted data. + * @param a_uOutputDataSize Size of the output buffer in UChar. + * @return true if all of the input data was successfully + * converted. + */ + bool ConvertFromStore( + const char * a_pInputData, + size_t a_uInputDataLen, + UChar * a_pOutputData, + size_t a_uOutputDataSize) + { + UErrorCode nError; + + if (!m_pConverter) { + nError = U_ZERO_ERROR; + m_pConverter = ucnv_open(m_pEncoding, &nError); + if (U_FAILURE(nError)) { + return false; + } + } + + nError = U_ZERO_ERROR; + ucnv_toUChars(m_pConverter, + a_pOutputData, (int32_t) a_uOutputDataSize, + a_pInputData, (int32_t) a_uInputDataLen, &nError); + if (U_FAILURE(nError)) { + return false; + } + + return true; + } + + /** Calculate the number of char required by the storage format of this + * data. The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData NULL terminated string to calculate the number of + * bytes required to be converted to storage format. + * @return Number of bytes required by the string when + * converted to storage format. This size always + * includes space for the terminating NULL character. + * @return -1 cast to size_t on a conversion error. + */ + size_t SizeToStore( + const UChar * a_pInputData) + { + UErrorCode nError; + + if (!m_pConverter) { + nError = U_ZERO_ERROR; + m_pConverter = ucnv_open(m_pEncoding, &nError); + if (U_FAILURE(nError)) { + return (size_t) -1; + } + } + + nError = U_ZERO_ERROR; + int32_t nLen = ucnv_fromUChars(m_pConverter, NULL, 0, + a_pInputData, -1, &nError); + if (U_FAILURE(nError) && nError != U_BUFFER_OVERFLOW_ERROR) { + return (size_t) -1; + } + + return (size_t) nLen + 1; + } + + /** Convert the input string to the storage format of this data. + * The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData NULL terminated source string to convert. All of + * the data will be converted including the + * terminating NULL character. + * @param a_pOutputData Pointer to the buffer to receive the converted + * string. + * @param a_pOutputDataSize Size of the output buffer in char. + * @return true if all of the input data, including the + * terminating NULL character was successfully + * converted. + */ + bool ConvertToStore( + const UChar * a_pInputData, + char * a_pOutputData, + size_t a_uOutputDataSize) + { + UErrorCode nError; + + if (!m_pConverter) { + nError = U_ZERO_ERROR; + m_pConverter = ucnv_open(m_pEncoding, &nError); + if (U_FAILURE(nError)) { + return false; + } + } + + nError = U_ZERO_ERROR; + ucnv_fromUChars(m_pConverter, + a_pOutputData, (int32_t) a_uOutputDataSize, + a_pInputData, -1, &nError); + if (U_FAILURE(nError)) { + return false; + } + + return true; + } +}; + +#endif // SI_CONVERT_ICU + + +// --------------------------------------------------------------------------- +// SI_CONVERT_WIN32 +// --------------------------------------------------------------------------- +#ifdef SI_CONVERT_WIN32 + +#define SI_Case SI_GenericCase + +// Windows CE doesn't have errno or MBCS libraries +#ifdef _WIN32_WCE +# ifndef SI_NO_MBCS +# define SI_NO_MBCS +# endif +#endif + +#include +#ifdef SI_NO_MBCS +# define SI_NoCase SI_GenericNoCase +#else // !SI_NO_MBCS +/** + * Case-insensitive comparison class using Win32 MBCS functions. This class + * returns a case-insensitive semi-collation order for MBCS text. It may not + * be safe for UTF-8 text returned in char format as we don't know what + * characters will be folded by the function! Therefore, if you are using + * SI_CHAR == char and SetUnicode(true), then you need to use the generic + * SI_NoCase class instead. + */ +#include +template +struct SI_NoCase { + bool operator()(const SI_CHAR * pLeft, const SI_CHAR * pRight) const { + if (sizeof(SI_CHAR) == sizeof(char)) { + return _mbsicmp((const unsigned char *)pLeft, + (const unsigned char *)pRight) < 0; + } + if (sizeof(SI_CHAR) == sizeof(wchar_t)) { + return _wcsicmp((const wchar_t *)pLeft, + (const wchar_t *)pRight) < 0; + } + return SI_GenericNoCase()(pLeft, pRight); + } +}; +#endif // SI_NO_MBCS + +/** + * Converts MBCS and UTF-8 to a wchar_t (or equivalent) on Windows. This uses + * only the Win32 functions and doesn't require the external Unicode UTF-8 + * conversion library. It will not work on Windows 95 without using Microsoft + * Layer for Unicode in your application. + */ +template +class SI_ConvertW { + UINT m_uCodePage; +protected: + SI_ConvertW() { } +public: + SI_ConvertW(bool a_bStoreIsUtf8) { + m_uCodePage = a_bStoreIsUtf8 ? CP_UTF8 : CP_ACP; + } + + /* copy and assignment */ + SI_ConvertW(const SI_ConvertW & rhs) { operator=(rhs); } + SI_ConvertW & operator=(const SI_ConvertW & rhs) { + m_uCodePage = rhs.m_uCodePage; + return *this; + } + + /** Calculate the number of SI_CHAR required for converting the input + * from the storage format. The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData Data in storage format to be converted to SI_CHAR. + * @param a_uInputDataLen Length of storage format data in bytes. This + * must be the actual length of the data, including + * NULL byte if NULL terminated string is required. + * @return Number of SI_CHAR required by the string when + * converted. If there are embedded NULL bytes in the + * input data, only the string up and not including + * the NULL byte will be converted. + * @return -1 cast to size_t on a conversion error. + */ + size_t SizeFromStore( + const char * a_pInputData, + size_t a_uInputDataLen) + { + SI_ASSERT(a_uInputDataLen != (size_t) -1); + + int retval = MultiByteToWideChar( + m_uCodePage, 0, + a_pInputData, (int) a_uInputDataLen, + 0, 0); + return (size_t)(retval > 0 ? retval : -1); + } + + /** Convert the input string from the storage format to SI_CHAR. + * The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData Data in storage format to be converted to SI_CHAR. + * @param a_uInputDataLen Length of storage format data in bytes. This + * must be the actual length of the data, including + * NULL byte if NULL terminated string is required. + * @param a_pOutputData Pointer to the output buffer to received the + * converted data. + * @param a_uOutputDataSize Size of the output buffer in SI_CHAR. + * @return true if all of the input data was successfully + * converted. + */ + bool ConvertFromStore( + const char * a_pInputData, + size_t a_uInputDataLen, + SI_CHAR * a_pOutputData, + size_t a_uOutputDataSize) + { + int nSize = MultiByteToWideChar( + m_uCodePage, 0, + a_pInputData, (int) a_uInputDataLen, + (wchar_t *) a_pOutputData, (int) a_uOutputDataSize); + return (nSize > 0); + } + + /** Calculate the number of char required by the storage format of this + * data. The storage format is always UTF-8. + * + * @param a_pInputData NULL terminated string to calculate the number of + * bytes required to be converted to storage format. + * @return Number of bytes required by the string when + * converted to storage format. This size always + * includes space for the terminating NULL character. + * @return -1 cast to size_t on a conversion error. + */ + size_t SizeToStore( + const SI_CHAR * a_pInputData) + { + int retval = WideCharToMultiByte( + m_uCodePage, 0, + (const wchar_t *) a_pInputData, -1, + 0, 0, 0, 0); + return (size_t) (retval > 0 ? retval : -1); + } + + /** Convert the input string to the storage format of this data. + * The storage format is always UTF-8 or MBCS. + * + * @param a_pInputData NULL terminated source string to convert. All of + * the data will be converted including the + * terminating NULL character. + * @param a_pOutputData Pointer to the buffer to receive the converted + * string. + * @param a_pOutputDataSize Size of the output buffer in char. + * @return true if all of the input data, including the + * terminating NULL character was successfully + * converted. + */ + bool ConvertToStore( + const SI_CHAR * a_pInputData, + char * a_pOutputData, + size_t a_uOutputDataSize) + { + int retval = WideCharToMultiByte( + m_uCodePage, 0, + (const wchar_t *) a_pInputData, -1, + a_pOutputData, (int) a_uOutputDataSize, 0, 0); + return retval > 0; + } +}; + +#endif // SI_CONVERT_WIN32 + + +// --------------------------------------------------------------------------- +// TYPE DEFINITIONS +// --------------------------------------------------------------------------- + +typedef CSimpleIniTempl,SI_ConvertA > CSimpleIniA; +typedef CSimpleIniTempl,SI_ConvertA > CSimpleIniCaseA; + +#if defined(SI_CONVERT_ICU) +typedef CSimpleIniTempl,SI_ConvertW > CSimpleIniW; +typedef CSimpleIniTempl,SI_ConvertW > CSimpleIniCaseW; +#else +typedef CSimpleIniTempl,SI_ConvertW > CSimpleIniW; +typedef CSimpleIniTempl,SI_ConvertW > CSimpleIniCaseW; +#endif + +#ifdef _UNICODE +# define CSimpleIni CSimpleIniW +# define CSimpleIniCase CSimpleIniCaseW +# define SI_NEWLINE SI_NEWLINE_W +#else // !_UNICODE +# define CSimpleIni CSimpleIniA +# define CSimpleIniCase CSimpleIniCaseA +# define SI_NEWLINE SI_NEWLINE_A +#endif // _UNICODE + +#ifdef _MSC_VER +# pragma warning (pop) +#endif + +#endif // INCLUDED_SimpleIni_h + diff --git a/fdbmonitor/fdbmonitor.cpp b/fdbmonitor/fdbmonitor.cpp new file mode 100644 index 0000000000..f9df4584d3 --- /dev/null +++ b/fdbmonitor/fdbmonitor.cpp @@ -0,0 +1,1298 @@ +/* + * fdbmonitor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#ifdef __linux__ +#include +#endif + +#include + +#ifdef __linux__ +#include +#include +#include +#endif + +#ifdef __APPLE__ +#include +#include +#include +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include + +#include + +#include +#include + +#include "flow/SimpleOpt.h" +#include "SimpleIni.h" + +#include "../versions.h" + +#ifdef __linux__ +typedef fd_set* fdb_fd_set; +#elif defined __APPLE__ +typedef int fdb_fd_set; +#endif + +#define CANONICAL_PATH_SEPARATOR '/' + +void monitor_fd( fdb_fd_set list, int fd, int* maxfd, void* cmd ) { +#ifdef __linux__ + FD_SET( fd, list ); + if ( fd > *maxfd ) + *maxfd = fd; +#elif defined __APPLE__ + /* ignore maxfd */ + struct kevent ev; + EV_SET( &ev, fd, EVFILT_READ, EV_ADD, 0, 0, cmd ); + kevent( list, &ev, 1, NULL, 0, NULL ); // FIXME: check? +#endif +} + +void unmonitor_fd( fdb_fd_set list, int fd ) { +#ifdef __linux__ + FD_CLR( fd, list ); +#elif defined __APPLE__ + struct kevent ev; + EV_SET( &ev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL ); + kevent( list, &ev, 1, NULL, 0, NULL ); // FIXME: check? +#endif +} + +void get_cur_timestamp(char *buf, int len) { + if(len <= 0) + return; + struct tm tm_info; + struct timeval tv; + gettimeofday(&tv, NULL); + localtime_r(&tv.tv_sec, &tm_info); + char *end = buf + len; + buf += strftime(buf, end - buf, "%Z %Y-%m-%d %H:%M:%S", &tm_info); + // Add fractional seconds + if(buf < end) + buf += snprintf(buf, end - buf, ".%06d", tv.tv_usec); + // Add epoch seconds after timestamp + if(buf < end) + buf += snprintf(buf, end - buf, " (%lld.%06d)", (long long int)tv.tv_sec, tv.tv_usec); +} + +bool daemonize = false; + +void log_msg(int priority, const char* format, ...) { + va_list args; + va_start(args, format); + + if (daemonize) { + vsyslog(priority, format, args); + } else { + char timebuf[64]; + get_cur_timestamp(timebuf, 64); + fprintf(stderr, "%s: ", timebuf); + vfprintf(stderr, format, args); + } + + va_end(args); +} + +void log_err(const char* func, int err, const char* format, ...) { + va_list args; + va_start(args, format); + + char buf[4096]; + + int len = vsnprintf( buf, 4096, format, args ); + + log_msg( LOG_ERR, "%.*s (%s error %d: %s)\n", len, buf, func, err, strerror(err) ); +} + +const char* get_value_multi(const CSimpleIni& ini, const char* key, ...) { + const char* ret = NULL; + const char* section = NULL; + + va_list ap; + va_start(ap, key); + + while (!ret && (section = va_arg(ap, const char *))) + ret = ini.GetValue(section, key, NULL); + + va_end(ap); + + return ret; +} + +double timer() { +#if defined(__linux__) + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return double(ts.tv_sec) + (ts.tv_nsec * 1e-9); +#elif defined(__APPLE__) + mach_timebase_info_data_t timebase_info; + mach_timebase_info(&timebase_info); + return ((mach_absolute_time() * timebase_info.numer) / timebase_info.denom) * 1e-9; +#else +#error Port me! +#endif +}; + +int randomInt(int min, int max) { + static std::random_device rd; + static std::mt19937 gen(rd()); + std::uniform_int_distribution<> dis(min, max); + + return dis(gen); +} + +std::string joinPath(std::string const& directory, std::string const& filename) { + auto d = directory; + auto f = filename; + while (f.size() && (f[0] == '/' || f[0] == CANONICAL_PATH_SEPARATOR)) + f = f.substr(1); + while (d.size() && (d.back() == '/' || d.back() == CANONICAL_PATH_SEPARATOR)) + d = d.substr(0, d.size()-1); + return d + CANONICAL_PATH_SEPARATOR + f; +} + +std::string abspath(std::string const& filename) { + // Returns an absolute path canonicalized to use only CANONICAL_PATH_SEPARATOR + char result[PATH_MAX]; + auto r = realpath( filename.c_str(), result ); + if (!r) { + if (errno == ENOENT) { + int sep = filename.find_last_of( CANONICAL_PATH_SEPARATOR ); + if (sep != std::string::npos) { + return joinPath( abspath( filename.substr(0, sep) ), filename.substr(sep) ); + } + else if (filename.find("~") == std::string::npos) { + return joinPath( abspath( "." ), filename ); + } + } + + perror("abspath"); + return ""; + } + return std::string(r); +} + +std::string parentDirectory(std::string const& filename) { + size_t sep = filename.find_last_of( CANONICAL_PATH_SEPARATOR ); + if (sep == std::string::npos) { + return ""; + } + + return filename.substr(0, sep); +} + +int mkdir(std::string const& directory) { + size_t sep = 0; + do { + sep = directory.find_first_of('/', sep + 1); + if ( mkdir( directory.substr(0, sep).c_str(), 0755 ) != 0 ) { + if (errno == EEXIST) + continue; + + return -1; + } + } while (sep != std::string::npos && sep != directory.length() - 1); + + return 0; +} + +struct Command { +private: + std::vector commands; + fdb_fd_set fds; +public: + const char** argv; + std::string section, ssection; + uint32_t initial_restart_delay; + uint32_t max_restart_delay; + double current_restart_delay; + double restart_backoff; + uint32_t restart_delay_reset_interval; + double last_start; + bool quiet; + bool delete_wd40_env; + bool deconfigured; + bool kill_on_configuration_change; + + // one pair for each of stdout and stderr + int pipes[2][2]; + + Command() : argv(NULL) { } + Command(const CSimpleIni& ini, std::string _section, uint64_t id, fdb_fd_set fds, int* maxfd) : section(_section), argv(NULL), quiet(false), delete_wd40_env(false), fds(fds), deconfigured(false), kill_on_configuration_change(true) { + char _ssection[strlen(section.c_str()) + 22]; + snprintf(_ssection, strlen(section.c_str()) + 22, "%s.%llu", section.c_str(), id); + ssection = _ssection; + + for ( auto p : pipes ) { + if ( (pipe(p) == 0) ) { + monitor_fd( fds, p[0], maxfd, this ); + } else { + log_err( "pipe", errno, "Unable to construct pipe for %s", ssection.c_str() ); + p[0] = -1; + p[1] = -1; + } + } + + CSimpleIniA::TNamesDepend keys, skeys, gkeys; + + ini.GetAllKeys(section.c_str(), keys); + ini.GetAllKeys(ssection.c_str(), skeys); + ini.GetAllKeys("general", gkeys); + + keys.splice(keys.end(), skeys, skeys.begin(), skeys.end()); + keys.splice(keys.end(), gkeys, gkeys.begin(), gkeys.end()); + keys.sort(CSimpleIniA::Entry::KeyOrder()); + keys.unique( [](const CSimpleIniA::Entry& lhs, const CSimpleIniA::Entry& rhs) -> bool { + return !CSimpleIniA::Entry::KeyOrder()(lhs, rhs); + } ); + + last_start = 0; + + char* endptr; + const char* rd = get_value_multi(ini, "restart_delay", ssection.c_str(), section.c_str(), "general", "fdbmonitor", NULL); + if (!rd) { + log_msg(LOG_ERR, "Unable to resolve restart delay for %s\n", ssection.c_str()); + return; + } + else { + max_restart_delay = strtoul(rd, &endptr, 10); + if (*endptr != '\0') { + log_msg(LOG_ERR, "Unable to parse restart delay for %s\n", ssection.c_str()); + return; + } + } + + const char* mrd = get_value_multi(ini, "initial_restart_delay", ssection.c_str(), section.c_str(), "general", "fdbmonitor", NULL); + if (!mrd) { + initial_restart_delay = 0; + } + else { + initial_restart_delay = std::min(max_restart_delay, strtoul(mrd, &endptr, 10)); + if (*endptr != '\0') { + log_msg(LOG_ERR, "Unable to parse initial restart delay for %s\n", ssection.c_str()); + return; + } + } + + current_restart_delay = initial_restart_delay; + + const char* rbo = get_value_multi(ini, "restart_backoff", ssection.c_str(), section.c_str(), "general", "fdbmonitor", NULL); + if(!rbo) { + restart_backoff = max_restart_delay; + } + else { + restart_backoff = strtod(rbo, &endptr); + if (*endptr != '\0') { + log_msg(LOG_ERR, "Unable to parse restart backoff for %s\n", ssection.c_str()); + return; + } + if (restart_backoff < 1.0) { + log_msg(LOG_ERR, "Invalid restart backoff value %lf for %s\n", restart_backoff, ssection.c_str()); + return; + } + } + + const char* rdri = get_value_multi(ini, "restart_delay_reset_interval", ssection.c_str(), section.c_str(), "general", "fdbmonitor", NULL); + if (!rdri) { + restart_delay_reset_interval = max_restart_delay; + } + else { + restart_delay_reset_interval = strtoul(rdri, &endptr, 10); + if (*endptr != '\0') { + log_msg(LOG_ERR, "Unable to parse restart delay reset interval for %s\n", ssection.c_str()); + return; + } + } + + const char* q = get_value_multi(ini, "disable_lifecycle_logging", ssection.c_str(), section.c_str(), "general", NULL); + if (q && !strcmp(q, "true")) + quiet = true; + + const char* dwe = get_value_multi(ini, "delete_wd40_env", ssection.c_str(), section.c_str(), "general", NULL); + if(dwe && !strcmp(dwe, "true")) { + delete_wd40_env = true; + } + + const char* kocc = get_value_multi(ini, "kill_on_configuration_change", ssection.c_str(), section.c_str(), "general", NULL); + if(kocc && strcmp(kocc, "true")) { + kill_on_configuration_change = false; + } + + const char* binary = get_value_multi(ini, "command", ssection.c_str(), section.c_str(), "general", NULL); + if (!binary) { + log_msg(LOG_ERR, "Unable to resolve command for %s\n", ssection.c_str()); + return; + } + std::stringstream ss(binary); + std::copy(std::istream_iterator (ss), std::istream_iterator(), std::back_inserter>(commands)); + + const char* id_s = ssection.c_str() + strlen(section.c_str()) + 1; + + for (auto i : keys) { + if (!strcmp(i.pItem, "command") || !strcmp(i.pItem, "restart_delay") || !strcmp(i.pItem, "initial_restart_delay") || !strcmp(i.pItem, "restart_backoff") || + !strcmp(i.pItem, "restart_delay_reset_interval") || !strcmp(i.pItem, "disable_lifecycle_logging") || !strcmp(i.pItem, "delete_wd40_env") || + !strcmp(i.pItem, "kill_on_configuration_change")) + { + continue; + } + + std::string opt = get_value_multi(ini, i.pItem, ssection.c_str(), section.c_str(), "general", NULL); + + std::size_t pos = 0; + + while ((pos = opt.find("$ID", pos)) != opt.npos) + opt.replace(pos, 3, id_s, strlen(id_s)); + + commands.push_back(std::string("--").append(i.pItem).append("=").append(opt)); + } + + argv = new const char* [commands.size() + 1]; + int i = 0; + for (auto itr : commands) { + argv[i++] = strdup(itr.c_str()); + } + argv[i] = NULL; + } + ~Command() { + delete[] argv; + for ( auto p : pipes ) { + if ( p[0] >= 0 && p[1] >= 0 ) { + unmonitor_fd( fds, p[0] ); + close( p[0] ); + close( p[1] ); + } + } + } + void update(const Command& other) { + quiet = other.quiet; + delete_wd40_env = other.delete_wd40_env; + initial_restart_delay = other.initial_restart_delay; + max_restart_delay = other.max_restart_delay; + restart_backoff = other.restart_backoff; + restart_delay_reset_interval = other.restart_delay_reset_interval; + deconfigured = other.deconfigured; + kill_on_configuration_change = other.kill_on_configuration_change; + + current_restart_delay = std::min(max_restart_delay, current_restart_delay); + current_restart_delay = std::max(initial_restart_delay, current_restart_delay); + } + bool operator!=(const Command& rhs) { + if (rhs.commands.size() != commands.size()) + return true; + + for (size_t i = 0; i < commands.size(); i++) { + if (commands[i].compare(rhs.commands[i]) != 0) + return true; + } + + return false; + } + + int get_and_update_current_restart_delay() { + if(timer() - last_start >= restart_delay_reset_interval) { + current_restart_delay = initial_restart_delay; + } + + int jitter = randomInt(floor(-0.1 * current_restart_delay), ceil(0.1 * current_restart_delay)); + int delay = std::max(0, round(current_restart_delay) + jitter); + current_restart_delay = std::min(max_restart_delay, restart_backoff * std::max(1.0, current_restart_delay)); + return delay; + } +}; + +std::unordered_map id_command; +std::unordered_map pid_id; +std::unordered_map id_pid; + +enum { OPT_CONFFILE, OPT_LOCKFILE, OPT_DAEMONIZE, OPT_HELP }; + +CSimpleOpt::SOption g_rgOptions[] = { + { OPT_CONFFILE, "--conffile", SO_REQ_SEP }, + { OPT_LOCKFILE, "--lockfile", SO_REQ_SEP }, + { OPT_DAEMONIZE, "--daemonize", SO_NONE }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + SO_END_OF_OPTIONS +}; + +void start_process(Command* cmd, uint64_t id, uid_t uid, gid_t gid, int delay, sigset_t* mask) { + if (!cmd->argv) + return; + + pid_t pid = fork(); + + if (pid < 0) { /* fork error */ + log_err("fork", errno, "Failed to launch new %s process", cmd->argv[0]); + return; + } else if (pid == 0) { /* we are the child */ + /* remove signal handlers from parent */ + signal(SIGHUP, SIG_DFL); + signal(SIGINT, SIG_DFL); + signal(SIGTERM, SIG_DFL); + + if(cmd->delete_wd40_env) { + /* remove WD40 environment variables */ + if(unsetenv("WD40_BV") || unsetenv("WD40_IS_MY_DADDY") || unsetenv("CONF_BUILD_VERSION")) { + log_err("unsetenv", errno, "Failed to remove parent environment variables"); + exit(1); + } + } + + dup2( cmd->pipes[0][1], fileno(stdout) ); + dup2( cmd->pipes[1][1], fileno(stderr) ); + +#ifdef __linux__ + signal(SIGCHLD, SIG_DFL); + + sigprocmask(SIG_SETMASK, mask, NULL); + + /* death of our parent raises SIGHUP */ + prctl(PR_SET_PDEATHSIG, SIGHUP); + if (getppid() == 1) /* parent already died before prctl */ + exit(0); +#endif + + if (delay) + while ((delay = sleep(delay)) > 0) {} + + if (getegid() != gid) + if (setgid(gid) != 0) { + log_err("setgid", errno, "Failed to set GID to %d", gid); + exit(1); + } + if (geteuid() != uid) + if (setuid(uid) != 0) { + log_err("setuid", errno, "Failed to set UID to %d", uid); + exit(1); + } + +#ifdef __linux__ + /* death of our parent raises SIGHUP */ + /* although not documented to this effect, setting uid/gid + appears to reset PDEATHSIG */ + prctl(PR_SET_PDEATHSIG, SIGHUP); + if (getppid() == 1) /* parent already died before prctl */ + exit(0); +#endif + + if (!cmd->quiet) + log_msg(LOG_INFO, "Launching %s (%d) for %s\n", cmd->argv[0], getpid(), cmd->ssection.c_str()); + execv(cmd->argv[0], (char* const*)cmd->argv); + log_err("execv", errno, "Failed to launch %s for %s", cmd->argv[0], cmd->ssection.c_str()); + _exit(0); + } + + cmd->last_start = timer() + delay; + pid_id[pid] = id; + id_pid[id] = pid; +} + +volatile int exit_signal = 0; + +#ifdef __linux__ +void signal_handler(int sig) { + if (sig > exit_signal) + exit_signal = sig; +} +#endif + +volatile bool child_exited = false; + +#ifdef __linux__ +void child_handler(int sig) { + child_exited = true; +} +#endif + +void print_usage(const char* name) { + printf( + "FoundationDB Process Monitor " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n" + "Usage: %s [OPTIONS]\n" + "\n" + " --conffile CONFFILE\n" + " The path of a file containing the connection string for the\n" + " FoundationDB cluster. The default is\n" + " `/etc/foundationdb/foundationdb.conf'.\n" + " --lockfile LOCKFILE\n" + " The path of the mutual exclusion file for this instance of\n" + " fdbmonitor. The default is `/var/run/fdbmonitor.pid'.\n" + " --daemonize Background the fdbmonitor process.\n" + " -h, --help Display this help and exit.\n", name); +} + +bool argv_equal(const char** a1, const char** a2) +{ + int i = 0; + + while (a1[i] && a2[i]) { + if (strcmp(a1[i], a2[i])) + return false; + i++; + } + + if (a1[i] != NULL || a2[i] != NULL) + return false; + return true; +} + +void kill_process(uint64_t id) { + pid_t pid = id_pid[id]; + + log_msg(LOG_INFO, "Killing process %d\n", pid); + + kill(pid, SIGTERM); + waitpid(pid, NULL, 0); + + pid_id.erase(pid); + id_pid.erase(id); +} + +void load_conf(const char* confpath, uid_t &uid, gid_t &gid, sigset_t* mask, fdb_fd_set rfds, int* maxfd) +{ + log_msg(LOG_INFO, "Loading configuration %s\n", confpath); + + CSimpleIniA ini; + ini.SetUnicode(); + + SI_Error err = ini.LoadFile(confpath); + if (err<0) { + log_msg(LOG_ERR, "Unable to load configuration file %s (SI_Error: %d, errno: %d)\n", confpath, err, errno); + return; + } + + uid_t _uid; + gid_t _gid; + + const char* user = ini.GetValue("fdbmonitor", "user", NULL); + const char* group = ini.GetValue("fdbmonitor", "group", NULL); + + if (user) { + errno = 0; + struct passwd* pw = getpwnam(user); + if (!pw) { + log_err( "getpwnam", errno, "Unable to lookup user %s", user ); + return; + } + _uid = pw->pw_uid; + } else + _uid = geteuid(); + + if (group) { + errno = 0; + struct group* gr = getgrnam(group); + if (!gr) { + log_err( "getgrnam", errno, "Unable to lookup group %s", group ); + return; + } + _gid = gr->gr_gid; + } else + _gid = getegid(); + + /* Any change to uid or gid requires the process to be restarted to take effect */ + if (uid != _uid || gid != _gid) { + std::vector kill_ids; + for (auto i : id_pid) { + if(id_command[i.first]->kill_on_configuration_change) { + kill_ids.push_back(i.first); + } + } + for (auto i : kill_ids) { + kill_process(i); + delete id_command[i]; + id_command.erase(i); + } + } + + uid = _uid; + gid = _gid; + + std::list kill_ids; + std::list> start_ids; + + for (auto i : id_pid) { + if (ini.GetSectionSize(id_command[i.first]->ssection.c_str()) == -1) { + /* Server on this port no longer configured; deconfigure it and kill it if required */ + log_msg(LOG_INFO, "Deconfigured %s\n", id_command[i.first]->ssection.c_str()); + + id_command[i.first]->deconfigured = true; + + if(id_command[i.first]->kill_on_configuration_change) { + kill_ids.push_back(i.first); + delete id_command[i.first]; + id_command.erase(i.first); + } + } else { + Command* cmd = new Command(ini, id_command[i.first]->section, i.first, rfds, maxfd); + + // If we just turned on 'kill_on_configuration_change', then kill the process to make sure we pick up any of its pending config changes + if (*(id_command[i.first]) != *cmd || (cmd->kill_on_configuration_change && !id_command[i.first]->kill_on_configuration_change)) { + log_msg(LOG_INFO, "Found new configuration for %s\n", id_command[i.first]->ssection.c_str()); + delete id_command[i.first]; + id_command[i.first] = cmd; + + if(id_command[i.first]->kill_on_configuration_change) { + kill_ids.push_back(i.first); + start_ids.push_back(std::make_pair(i.first, cmd)); + } + } else { + log_msg(LOG_INFO, "Updated configuration for %s\n", id_command[i.first]->ssection.c_str()); + id_command[i.first]->update(*cmd); + delete cmd; + } + } + } + + for (auto i : kill_ids) + kill_process(i); + + for (auto i : start_ids) { + start_process(i.second, i.first, uid, gid, 0, mask); + } + + /* We've handled deconfigured sections, now look for newly + configured sections */ + CSimpleIniA::TNamesDepend sections; + ini.GetAllSections(sections); + for (auto i : sections) { + if (auto dot = strrchr(i.pItem, '.')) { + char* strtol_end; + + uint64_t id = strtoull(dot + 1, &strtol_end, 10); + + if (*strtol_end != '\0' || !(id > 0)) { + log_msg(LOG_ERR, "Found bogus id in %s\n", i.pItem); + } else { + if (!id_pid.count(id)) { + /* Found something we haven't yet started */ + log_msg(LOG_INFO, "Starting %s\n", i.pItem); + std::string section(i.pItem, dot - i.pItem); + id_command[id] = new Command(ini, section, id, rfds, maxfd); + start_process(id_command[id], id, uid, gid, 0, mask); + } + } + } + } +} + +/* cmd->pipes[pipe_idx] *must* be ready to read without blocking */ +void read_child_output( Command* cmd, int pipe_idx, fdb_fd_set fds ) { + char buf[4096]; + + int len = read( cmd->pipes[pipe_idx][0], buf, 4096 ); + if ( len == -1 ) { + if ( errno != EINTR ) { + /* We shouldn't get EAGAIN or EWOULDBLOCK + here, and if it's not EINTR then all of + the other alternatives seem "bad". */ + log_err( "read", errno, "Error while reading from %s, no longer logging output", cmd->ssection.c_str() ); + unmonitor_fd( fds, cmd->pipes[pipe_idx][0] ); + } + return; + } + + // pipe_idx == 0 is stdout, pipe_idx == 1 is stderr + int priority = (pipe_idx == 0) ? LOG_INFO : LOG_ERR; + + int start = 0; + for ( int i = 0; i < len; i++ ) { + if ( buf[i] == '\n' ) { + log_msg( priority, "%s: %.*s", cmd->ssection.c_str(), i - start + 1, buf + start ); + start = i + 1; + } + } + + if ( start < len ) { + log_msg( priority, "%s: %.*s\n", cmd->ssection.c_str(), len - start, buf + start ); + } +} + +#ifdef __APPLE__ +void watch_conf_file( int kq, int* conff_fd, const char* confpath ) { + struct kevent ev; + + /* If already watching, drop it and close */ + if ( *conff_fd >= 0 ) { + EV_SET( &ev, *conff_fd, EVFILT_VNODE, EV_DELETE, NOTE_WRITE | NOTE_ATTRIB, 0, NULL ); + kevent( kq, &ev, 1, NULL, 0, NULL ); + close( *conff_fd ); + } + + /* Open and watch */ + *conff_fd = open( confpath, O_EVTONLY ); + if ( *conff_fd >= 0 ) { + EV_SET( &ev, *conff_fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE | NOTE_ATTRIB, 0, NULL ); + kevent( kq, &ev, 1, NULL, 0, NULL ); + } +} +#endif + +#ifdef __linux__ +void fdbmon_stat(const char *path, struct stat *path_stat, bool is_link) { + int result = is_link ? lstat(path, path_stat) : stat(path, path_stat); + if(result) { + perror(is_link ? "lstat" : "stat"); + exit(1); + } +} + +std::unordered_map> set_watches(std::string path, int ifd) { + std::unordered_map> additional_watch_wds; + struct stat path_stat; + + if(path.size() < 2) + return additional_watch_wds; + + int idx = 1; + while(idx != std::string::npos) { + idx = path.find_first_of('/', idx+1); + std::string subpath = path.substr(0, idx); + + int level = 0; + while(true) { + if(level++ == 100) { + log_msg(LOG_ERR, "Too many nested symlinks in path %s\n", path.c_str()); + exit(1); + } + + fdbmon_stat(subpath.c_str(), &path_stat, true); + if(!S_ISLNK(path_stat.st_mode)) { + break; + } + + std::string parent = parentDirectory(subpath); + + int wd = inotify_add_watch(ifd, parent.c_str(), IN_CREATE | IN_MOVED_TO); + if (wd < 0) { + perror("inotify_add_watch link"); + exit(1); + } + + log_msg(LOG_INFO, "Watching parent directory of symlink %s (%d)\n", subpath.c_str(), wd); + additional_watch_wds[wd].insert(subpath.substr(parent.size()+1)); + + char buf[PATH_MAX+1]; + ssize_t len = readlink(subpath.c_str(), buf, PATH_MAX); + if(len < 0) { + perror("readlink"); + exit(1); + } + + buf[len] = '\0'; + if(buf[0] == '/') { + subpath = buf; + } + else { + subpath = joinPath(parent, buf); + } + } + } + + return additional_watch_wds; +} +#endif + +int main(int argc, char** argv) { + std::string lockfile = "/var/run/fdbmonitor.pid"; + std::string _confpath = "/etc/foundationdb/foundationdb.conf"; + + std::vector additional_watch_paths; + + CSimpleOpt args(argc, argv, g_rgOptions, SO_O_NOERR); + + while (args.Next()) { + if (args.LastError() == SO_SUCCESS) { + switch (args.OptionId()) { + case OPT_CONFFILE: + _confpath = args.OptionArg(); + break; + case OPT_LOCKFILE: + lockfile = args.OptionArg(); + break; + case OPT_DAEMONIZE: + daemonize = true; + break; + case OPT_HELP: + print_usage(argv[0]); + exit(0); + } + } else { + print_usage(argv[0]); + exit(1); + } + } + + log_msg(LOG_INFO, "Started FoundationDB Process Monitor " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + + // Modify _confpath to be absolute for further traversals + if(!_confpath.empty() && _confpath[0] != '/') { + char buf[PATH_MAX]; + if( !getcwd(buf, PATH_MAX) ) { + perror("getcwd"); + exit(1); + } + + _confpath = joinPath(buf, _confpath); + } + + // Guaranteed (if non-NULL) to be an absolute path with no + // symbolic link, /./ or /../ components + const char *p = realpath(_confpath.c_str(), NULL); + if (!p) { + log_msg(LOG_ERR, "No configuration file at %s\n", _confpath.c_str()); + exit(1); + } + + std::string confpath = p; + + // Will always succeed given an absolute path + std::string confdir = parentDirectory(confpath); + std::string conffile = confpath.substr(confdir.size()+1); + +#ifdef __linux__ + // Watch for changes to our configuration file + int ifd = inotify_init(); + if (ifd < 0) { + perror("inotify_init"); + exit(1); + } + + int conffile_wd = inotify_add_watch(ifd, confpath.c_str(), IN_CLOSE_WRITE); + if (conffile_wd < 0) { + perror("inotify_add_watch conf file"); + exit(1); + } else { + log_msg(LOG_INFO, "Watching config file %s\n", confpath.c_str()); + } + + int confdir_wd = inotify_add_watch(ifd, confdir.c_str(), IN_CLOSE_WRITE | IN_MOVED_TO); + if (confdir_wd < 0) { + perror("inotify_add_watch conf dir"); + exit(1); + } else { + log_msg(LOG_INFO, "Watching config dir %s\n", confdir.c_str()); + } + + auto additional_watch_wds = set_watches(_confpath, ifd); +#endif + + /* fds we're blocking on via pselect or kevent */ + fdb_fd_set watched_fds; + /* only linux needs this, but... */ + int maxfd = 0; + +#ifdef __linux__ + fd_set rfds; + watched_fds = &rfds; + + FD_ZERO(&rfds); + FD_SET(ifd, &rfds); + maxfd = ifd; + + int nfds = 0; + fd_set srfds; +#endif + + CSimpleIniA* ini = NULL; + + if (daemonize) { +#ifdef __APPLE__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif + if (daemon(0, 0)) { +#ifdef __APPLE__ +#pragma GCC diagnostic pop +#endif + perror("daemon"); + exit(1); + } + + /* open syslog connection immediately, to be inherited by + forked children */ + openlog("fdbmonitor", LOG_PID | LOG_NDELAY, LOG_DAEMON); + + signal(SIGTSTP, SIG_IGN); + signal(SIGTTOU, SIG_IGN); + signal(SIGTTIN, SIG_IGN); + + /* new process group, no controlling terminal */ + /* unchecked since the only failure indicates we're already a + process group leader */ + setsid(); + } + + /* open and lock our lockfile for mutual exclusion */ + std::string lockfileDir = parentDirectory(abspath(lockfile)); + if(lockfileDir.size() == 0) { + log_msg(LOG_ERR, "Unable to determine parent directory of lockfile %s\n", lockfile.c_str()); + exit(1); + } + + if(mkdir(lockfileDir) < 0) { + log_err("mkdir", errno, "Unable to create parent directory for lockfile %s", lockfile.c_str()); + exit(1); + } + + int lockfile_fd = open(lockfile.c_str(), O_RDWR|O_CREAT, 0640); + if (lockfile_fd < 0) { + log_err("open", errno, "Unable to open fdbmonitor lockfile %s", lockfile.c_str()); + exit(1); + } + if (lockf(lockfile_fd, F_LOCK, 0) < 0) { + log_err("lockf", errno, "Unable to lock fdbmonitor lockfile %s (is fdbmonitor already running?)", lockfile.c_str()); + exit(0); + } + + if (chdir("/") < 0) { + log_err("chdir", errno, "Unable to change working directory"); + exit(1); + } + + /* write our pid to the lockfile for convenience */ + char pid_buf[16]; + snprintf(pid_buf, sizeof(pid_buf), "%d\n", getpid()); + ssize_t ign = write(lockfile_fd, pid_buf, strlen(pid_buf)); + +#ifdef __linux__ + /* attempt to do clean shutdown and remove lockfile when killed */ + signal(SIGHUP, signal_handler); + signal(SIGINT, signal_handler); + signal(SIGTERM, signal_handler); +#elif defined(__APPLE__) + int kq = kqueue(); + if ( kq < 0 ) { + log_err( "kqueue", errno, "Unable to create kqueue" ); + exit(1); + } + watched_fds = kq; + + signal(SIGHUP, SIG_IGN); + signal(SIGINT, SIG_IGN); + signal(SIGTERM, SIG_IGN); + + struct kevent ev; + + EV_SET( &ev, SIGHUP, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); + kevent( kq, &ev, 1, NULL, 0, NULL ); + EV_SET( &ev, SIGINT, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); + kevent( kq, &ev, 1, NULL, 0, NULL ); + EV_SET( &ev, SIGTERM, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); + kevent( kq, &ev, 1, NULL, 0, NULL ); + EV_SET( &ev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, NULL); + kevent( kq, &ev, 1, NULL, 0, NULL ); + + int confd_fd = open(confdir.c_str(), O_EVTONLY); + + // Watch the directory holding the configuration file + EV_SET( &ev, confd_fd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, NULL ); + kevent( kq, &ev, 1, NULL, 0, NULL ); + + int conff_fd = -1; + watch_conf_file( kq, &conff_fd, confpath.c_str() ); +#endif + +#ifdef __linux__ + signal(SIGCHLD, child_handler); +#endif + + uid_t uid = 0; + gid_t gid = 0; + + sigset_t normal_mask, full_mask; + sigfillset(&full_mask); + +#ifdef __linux__ + /* normal will be restored in our main loop in the call to + pselect, but none blocks all signals while processing events */ + sigprocmask(SIG_SETMASK, &full_mask, &normal_mask); +#elif defined(__APPLE__) + sigprocmask(0, NULL, &normal_mask); +#endif + +#ifdef __APPLE__ + struct stat st_buf; + struct timespec mtimespec; + + if (stat(confpath.c_str(), &st_buf) < 0) + perror("stat"); + + memcpy(&mtimespec, &(st_buf.st_mtimespec), sizeof(struct timespec)); +#endif + + load_conf(confpath.c_str(), uid, gid, &normal_mask, watched_fds, &maxfd); + + while (1) { +#ifdef __APPLE__ + int nev = kevent( kq, NULL, 0, &ev, 1, NULL ); + + if (nev > 0) { + switch (ev.filter) { + case EVFILT_VNODE: + struct kevent timeout; + // This could be the conf dir or conf file + if ( ev.ident == confd_fd ) { + /* Changes in the directory holding the conf file; schedule a future timeout to reset watches and reload the conf */ + EV_SET( &timeout, 1, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 200, NULL ); + kevent( kq, &timeout, 1, NULL, 0, NULL ); + } else { + /* Direct writes to the conf file; reload! */ + load_conf( confpath.c_str(), uid, gid, &normal_mask, watched_fds, &maxfd ); + watch_conf_file( kq, &conff_fd, confpath.c_str() ); + } + break; + case EVFILT_TIMER: + watch_conf_file( kq, &conff_fd, confpath.c_str() ); + load_conf( confpath.c_str(), uid, gid, &normal_mask, watched_fds, &maxfd ); + break; + case EVFILT_SIGNAL: + switch (ev.ident) { + case SIGHUP: + case SIGINT: + case SIGTERM: + exit_signal = ev.ident; + break; + case SIGCHLD: + child_exited = true; + break; + default: + break; + } + break; + case EVFILT_READ: + Command* cmd = (Command*)ev.udata; + for ( int i = 0; i < 2 ; i++ ) { + if ( ev.ident == cmd->pipes[i][0] ) { + read_child_output( cmd, i, watched_fds ); + } + } + break; + } + } +#endif + /* select() could have returned because received an exit signal */ + if (exit_signal > 0) { + switch(exit_signal) { + case SIGHUP: + log_msg(LOG_INFO, "Received signal %d (%s), doing nothing\n", exit_signal, strsignal(exit_signal)); + break; + case SIGINT: + case SIGTERM: + log_msg(LOG_NOTICE, "Received signal %d (%s), shutting down\n", exit_signal, strsignal(exit_signal)); + + /* Unblock signals */ + signal(SIGCHLD, SIG_IGN); + sigprocmask(SIG_SETMASK, &normal_mask, NULL); + + /* Send SIGHUP to all child processes */ + kill(0, SIGHUP); + + /* Wait for all child processes (says POSIX.1-2001) */ + /* POSIX.1-2001 specifies that if the disposition of SIGCHLD is set to SIG_IGN, then children that terminate do not become zombies and a call to wait() + will block until all children have terminated, and then fail with errno set to ECHILD */ + wait(NULL); + + unlink(lockfile.c_str()); + exit(0); + default: + break; + } + exit_signal = 0; + } + +#ifdef __linux__ + /* select() could have returned because we have a fd ready to + read (child output or inotify on conf file) */ + if (nfds > 0) { + int len, i = 0; + + char buf[4096]; + + for ( auto itr : id_command ) { + for ( int i = 0; i < 2; i++ ) { + if ( FD_ISSET( (itr.second)->pipes[i][0], &srfds ) ) { + read_child_output( itr.second, i, watched_fds ); + } + } + } + + if ( FD_ISSET( ifd, &srfds ) ) { + len = read(ifd, buf, 4096); + if (len < 0) + log_err("read", errno, "Error reading inotify message"); + + bool reload = false; + bool reload_additional_watches = false; + + while (i < len) { + struct inotify_event* event = (struct inotify_event*) &buf[i]; + + auto search = additional_watch_wds.find(event->wd); + if(event->wd != conffile_wd) { + if(search != additional_watch_wds.end() && event->len && search->second.count(event->name)) { + log_msg(LOG_INFO, "Changes detected on watched symlink `%s': (%d, %#010x)\n", event->name, event->wd, event->mask); + + char *redone_confpath = realpath(_confpath.c_str(), NULL); + if(!redone_confpath) { + log_msg(LOG_INFO, "Error calling realpath on `%s', continuing...\n", _confpath.c_str()); + perror("realpath"); + // exit(1); + i += sizeof(struct inotify_event) + event->len; + continue; + } + + confpath = redone_confpath; + + // Will always succeed given an absolute path + confdir = parentDirectory(confpath); + conffile = confpath.substr(confdir.size()); + + // Remove all the old watches + for(auto wd : additional_watch_wds) { + if(inotify_rm_watch(ifd, wd.first) < 0) { + // perror("inotify_rm_watch symlink"); + // exit(1); + log_msg(LOG_INFO, "Could not remove inotify watch %d, continuing...\n", wd.first); + } + } + + reload = true; + reload_additional_watches = true; + break; + } + else if(event->wd == confdir_wd && event->len && conffile == event->name) { + reload = true; + } + } + + else if (event->wd == conffile_wd) { + reload = true; + } + + i += sizeof(struct inotify_event) + event->len; + } + + if (reload) { + if(inotify_rm_watch(ifd, confdir_wd) < 0) { + log_msg(LOG_INFO, "Could not remove inotify conf dir watch, continuing...\n"); + } + if(inotify_rm_watch(ifd, conffile_wd) < 0) { + log_msg(LOG_INFO, "Could not remove inotify conf file watch, continuing...\n"); + } + conffile_wd = inotify_add_watch(ifd, confpath.c_str(), IN_CLOSE_WRITE); + if (conffile_wd < 0) { + perror("inotify_add_watch conf file"); + exit(1); // Deleting the conf file causes fdbmonitor to terminate + } else { + log_msg(LOG_INFO, "Watching config file %s\n", confpath.c_str()); + } + + confdir_wd = inotify_add_watch(ifd, confdir.c_str(), IN_CLOSE_WRITE | IN_MOVED_TO); + if (confdir_wd < 0) { + perror("inotify_add_watch conf dir"); + exit(1); + } else { + log_msg(LOG_INFO, "Watching config dir %s (%d)\n", confdir.c_str(), confdir_wd); + } + + if(reload_additional_watches) { + additional_watch_wds = set_watches(_confpath, ifd); + } + + load_conf(confpath.c_str(), uid, gid, &normal_mask, &rfds, &maxfd); + } + } + } +#endif + + /* select() could have returned because of one or more + SIGCHLDs */ + if (child_exited) { + pid_t pid; + int child_status; + while ((pid = waitpid(-1, &child_status, WNOHANG))) { + if (pid < 0) { + if (errno != ECHILD) + log_err("waitpid", errno, "Error while waiting for child process"); + break; + } + + uint64_t id = pid_id[pid]; + Command* cmd = id_command[id]; + + pid_id.erase(pid); + id_pid.erase(id); + + if(cmd->deconfigured) { + delete cmd; + id_command.erase(id); + } + else { + int delay = cmd->get_and_update_current_restart_delay(); + if (!cmd->quiet) { + if (WIFEXITED(child_status)) { + int priority = (WEXITSTATUS(child_status) == 0) ? LOG_NOTICE : LOG_ERR; + log_msg(priority, "Process %d exited %d, restarting %s in %d seconds\n", pid, WEXITSTATUS(child_status), cmd->ssection.c_str(), delay); + } else if (WIFSIGNALED(child_status)) + log_msg(LOG_NOTICE, "Process %d terminated by signal %d, restarting %s in %d seconds\n", pid, WTERMSIG(child_status), cmd->ssection.c_str(), delay); + else + log_msg(LOG_WARNING, "Process %d exited for unknown reason, restarting %s in %d seconds\n", pid, cmd->ssection.c_str(), delay); + } + + start_process(cmd, id, uid, gid, delay, &normal_mask); + } + } + child_exited = false; + } + +#ifdef __linux__ + /* Block until something interesting happens (while atomically + unblocking signals) */ + srfds = rfds; + nfds = pselect(maxfd+1, &srfds, NULL, NULL, NULL, &normal_mask); +#endif + } +} diff --git a/fdbmonitor/fdbmonitor.vcxproj b/fdbmonitor/fdbmonitor.vcxproj new file mode 100644 index 0000000000..9e0d16b797 --- /dev/null +++ b/fdbmonitor/fdbmonitor.vcxproj @@ -0,0 +1,79 @@ + + + + + Debug + x64 + + + Release + x64 + + + + {9A1D17A1-1B56-44D8-90C8-56F1726C1C4C} + fdbmonitor + + + + Application + true + MultiByte + v140_xp + + + Application + false + true + MultiByte + v140_xp + + + + + + + + + + + + + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + + + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + + + + Level3 + Disabled + + + true + + + + + Level3 + MaxSpeed + true + true + + + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/fdbmonitor/local.mk b/fdbmonitor/local.mk new file mode 100644 index 0000000000..187a30e926 --- /dev/null +++ b/fdbmonitor/local.mk @@ -0,0 +1,32 @@ +# +# local.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile; -*- + +# SimpleOpt.h lives here :-/ +fdbmonitor_CFLAGS := -I. + +ifeq ($(PLATFORM),linux) + fdbmonitor_LDFLAGS := -static-libstdc++ -static-libgcc -lrt +else ifeq ($(PLATFORM),osx) + fdbmonitor_LDFLAGS := -lc++ +endif + +bin/fdbmonitor.debug: bin/fdbmonitor diff --git a/fdbrpc/ActorFuzz.actor.cpp b/fdbrpc/ActorFuzz.actor.cpp new file mode 100644 index 0000000000..99e6c8d28a --- /dev/null +++ b/fdbrpc/ActorFuzz.actor.cpp @@ -0,0 +1,751 @@ +/* + * ActorFuzz.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// THIS FILE WAS GENERATED BY actorFuzz.py; DO NOT MODIFY IT DIRECTLY + +#include "ActorFuzz.h" +#ifndef WIN32 + +ACTOR Future actorFuzz0( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state std::vector list928815; + list928815.push_back(1); + list928815.push_back(2); + list928815.push_back(3); + for( auto i : list928815 ) { + outputStream.send( 472952 ); + return 424221; + } + return 136262; +} + +ACTOR Future actorFuzz1( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + int input = waitNext( inputStream ); + outputStream.send( input + 895041 ); + return 406985; +} + +ACTOR Future actorFuzz2( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + return 981337; +} + +ACTOR Future actorFuzz3( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + try { + outputStream.send( 169417 ); + try { + outputStream.send( 910046 ); + throw_operation_failed(); + outputStream.send( 128775 ); + try { + outputStream.send( 288265 ); + state std::vector list147355; + list147355.push_back(1); + list147355.push_back(2); + list147355.push_back(3); + for( auto i : list147355 ) { + outputStream.send( 884888 ); + try { + outputStream.send( 523929 ); + return 803295; + } catch (...) { + outputStream.send( 895092 ); + try { + outputStream.send( 103860 ); + return 189456; + } catch (...) { + outputStream.send( 636033 ); + continue; + } + } + } + outputStream.send( 827981 ); + } catch (...) { + outputStream.send( 530422 ); + int input = waitNext( inputStream ); + outputStream.send( input + 556758 ); + outputStream.send( 289564 ); + } + outputStream.send( 479534 ); + } catch (...) { + outputStream.send( 901015 ); + state int i276213; for(i276213 = 0; i276213 < 5; i276213++) { + outputStream.send( 238065 ); + continue; + } + outputStream.send( 216530 ); + } + outputStream.send( 721088 ); + state int i183102; for(i183102 = 0; i183102 < 5; i183102++) { + outputStream.send( 154168 ); + state int i424006; for(i424006 = 0; i424006 < 5; i424006++) { + outputStream.send( 509758 ); + break; + } + outputStream.send( 331070 ); + } + outputStream.send( 728291 ); + } catch (...) { + outputStream.send( 276663 ); + return 893442; + } + return 150168; +} + +ACTOR Future actorFuzz4( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + if ( (++ifstate&1) == 0 ) { + outputStream.send( 757514 ); + int input = waitNext( inputStream ); + outputStream.send( input + 991755 ); + outputStream.send( 397194 ); + } + return 924232; +} + +ACTOR Future actorFuzz5( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state int i442791; for(i442791 = 0; i442791 < 5; i442791++) { + outputStream.send( 793665 ); + state int i122946; for(i122946 = 0; i122946 < 5; i122946++) { + outputStream.send( 473530 ); + state int i880568; for(i880568 = 0; i880568 < 5; i880568++) { + outputStream.send( 728257 ); + return 239736; + } + outputStream.send( 923761 ); + } + outputStream.send( 713797 ); + state std::vector list811633; + list811633.push_back(1); + list811633.push_back(2); + list811633.push_back(3); + for( auto i : list811633 ) { + outputStream.send( 366087 ); + int input = waitNext( inputStream ); + outputStream.send( input + 538592 ); + outputStream.send( 222552 ); + } + outputStream.send( 974653 ); + } + return 738267; +} + +ACTOR Future actorFuzz6( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state std::vector list672004; + list672004.push_back(1); + list672004.push_back(2); + list672004.push_back(3); + for( auto i : list672004 ) { + outputStream.send( 228205 ); + state int i213703; for(i213703 = 0; i213703 < 5; i213703++) { + outputStream.send( 686961 ); + Void _ = wait( error ); // throw operation_failed() + outputStream.send( 453057 ); + } + outputStream.send( 318329 ); + } + outputStream.send( 878109 ); + if ( (++ifstate&1) == 0 ) { + outputStream.send( 947744 ); + return 770841; + } + outputStream.send( 484904 ); + throw operation_failed(); +} + +ACTOR Future actorFuzz7( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state int i865202; for(i865202 = 0; i865202 < 5; i865202++) { + outputStream.send( 388211 ); + continue; + } + return 215808; +} + +ACTOR Future actorFuzz8( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state std::vector list872196; + list872196.push_back(1); + list872196.push_back(2); + list872196.push_back(3); + for( auto i : list872196 ) { + outputStream.send( 906334 ); + continue; + } + return 190561; +} + +ACTOR Future actorFuzz9( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + if ( (++ifstate&1) == 0 ) { + outputStream.send( 862320 ); + try { + outputStream.send( 467528 ); + int input = waitNext( inputStream ); + outputStream.send( input + 454869 ); + outputStream.send( 170527 ); + } catch (...) { + outputStream.send( 599433 ); + int input = waitNext( inputStream ); + outputStream.send( input + 849051 ); + outputStream.send( 399140 ); + } + outputStream.send( 775108 ); + } + return 159221; +} + +ACTOR Future actorFuzz10( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state int i624986; for(i624986 = 0; i624986 < 5; i624986++) { + outputStream.send( 713197 ); + break; + } + return 195782; +} + +ACTOR Future actorFuzz11( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state std::vector list716397; + list716397.push_back(1); + list716397.push_back(2); + list716397.push_back(3); + for( auto i : list716397 ) { + outputStream.send( 715504 ); + state std::vector list893053; + list893053.push_back(1); + list893053.push_back(2); + list893053.push_back(3); + for( auto i : list893053 ) { + outputStream.send( 404836 ); + break; + } + outputStream.send( 687853 ); + } + return 416948; +} + +ACTOR Future actorFuzz12( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + return 955805; +} + +ACTOR Future actorFuzz13( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state int i543171; for(i543171 = 0; i543171 < 5; i543171++) { + outputStream.send( 619715 ); + if ( (++ifstate&1) == 1 ) { + outputStream.send( 528509 ); + try { + outputStream.send( 647852 ); + continue; + } catch (...) { + outputStream.send( 707540 ); + return 252364; + } + } + outputStream.send( 255314 ); + } + return 391920; +} + +ACTOR Future actorFuzz14( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state int i341633; for(i341633 = 0; i341633 < 5; i341633++) { + outputStream.send( 422579 ); + try { + outputStream.send( 665867 ); + continue; + } catch (...) { + outputStream.send( 934161 ); + throw_operation_failed(); + outputStream.send( 966369 ); + } + outputStream.send( 484274 ); + return 323308; + } + return 798692; +} + +ACTOR Future actorFuzz15( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + if ( (++ifstate&1) == 1 ) { + outputStream.send( 463317 ); + int input = waitNext( inputStream ); + outputStream.send( input + 862528 ); + outputStream.send( 144528 ); + throw operation_failed(); + } + return 348329; +} + +ACTOR Future actorFuzz16( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state std::vector list315206; + list315206.push_back(1); + list315206.push_back(2); + list315206.push_back(3); + for( auto i : list315206 ) { + outputStream.send( 488500 ); + int input = waitNext( inputStream ); + outputStream.send( input + 435335 ); + outputStream.send( 838746 ); + } + return 241009; +} + +ACTOR Future actorFuzz17( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + return 499302; +} + +ACTOR Future actorFuzz18( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + throw operation_failed(); +} + +ACTOR Future actorFuzz19( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + int input = waitNext( inputStream ); + outputStream.send( input + 216963 ); + outputStream.send( 928990 ); + try { + outputStream.send( 833320 ); + return 981835; + } catch (...) { + outputStream.send( 163601 ); + if ( (++ifstate&1) == 0 ) { + outputStream.send( 566353 ); + int input = waitNext( inputStream ); + outputStream.send( input + 898598 ); + outputStream.send( 489283 ); + } else { + outputStream.send( 302679 ); + state std::vector list598178; + list598178.push_back(1); + list598178.push_back(2); + list598178.push_back(3); + for( auto i : list598178 ) { + outputStream.send( 161921 ); + state std::vector list144442; + list144442.push_back(1); + list144442.push_back(2); + list144442.push_back(3); + for( auto i : list144442 ) { + outputStream.send( 383319 ); + if ( (++ifstate&1) == 0 ) { + outputStream.send( 510598 ); + if ( (++ifstate&1) == 0 ) { + outputStream.send( 507597 ); + if ( (++ifstate&1) == 0 ) { + outputStream.send( 165706 ); + Void _ = wait( error ); // throw operation_failed() + outputStream.send( 517934 ); + } else { + outputStream.send( 310975 ); + continue; + } + outputStream.send( 276549 ); + } else { + outputStream.send( 760246 ); + return 285921; + } + outputStream.send( 998271 ); + state int i844483; for(i844483 = 0; i844483 < 5; i844483++) { + outputStream.send( 116027 ); + try { + outputStream.send( 924660 ); + return 318962; + } catch (...) { + outputStream.send( 344097 ); + return 650093; + } + } + outputStream.send( 911361 ); + } else { + outputStream.send( 132280 ); + state int i765185; for(i765185 = 0; i765185 < 5; i765185++) { + outputStream.send( 304676 ); + try { + outputStream.send( 213797 ); + continue; + } catch (...) { + outputStream.send( 103092 ); + break; + } + } + outputStream.send( 186853 ); + throw operation_failed(); + } + outputStream.send( 111835 ); + } + outputStream.send( 556173 ); + } + outputStream.send( 946155 ); + } + outputStream.send( 933557 ); + } + return 291132; +} + +ACTOR Future actorFuzz20( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + return 668336; +} + +ACTOR Future actorFuzz21( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + int input = waitNext( inputStream ); + outputStream.send( input + 103331 ); + return 404486; +} + +ACTOR Future actorFuzz22( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + try { + outputStream.send( 173342 ); + if ( (++ifstate&1) == 1 ) { + outputStream.send( 133654 ); + throw operation_failed(); + } + outputStream.send( 390745 ); + } catch (...) { + outputStream.send( 317492 ); + try { + outputStream.send( 976956 ); + if ( (++ifstate&1) == 1 ) { + outputStream.send( 488021 ); + try { + outputStream.send( 379893 ); + int input = waitNext( inputStream ); + outputStream.send( input + 113044 ); + outputStream.send( 514763 ); + } catch (...) { + outputStream.send( 934231 ); + try { + outputStream.send( 897734 ); + if ( (++ifstate&1) == 1 ) { + outputStream.send( 217526 ); + if ( (++ifstate&1) == 0 ) { + outputStream.send( 483115 ); + state int i174159; for(i174159 = 0; i174159 < 5; i174159++) { + outputStream.send( 153107 ); + return 282773; + } + outputStream.send( 980876 ); + } else { + outputStream.send( 617607 ); + state std::vector list256809; + list256809.push_back(1); + list256809.push_back(2); + list256809.push_back(3); + for( auto i : list256809 ) { + outputStream.send( 939809 ); + state std::vector list645802; + list645802.push_back(1); + list645802.push_back(2); + list645802.push_back(3); + for( auto i : list645802 ) { + outputStream.send( 958333 ); + return 863292; + } + outputStream.send( 556033 ); + } + outputStream.send( 374285 ); + } + outputStream.send( 804200 ); + } + outputStream.send( 353758 ); + } catch (...) { + outputStream.send( 289122 ); + if ( (++ifstate&1) == 1 ) { + outputStream.send( 807371 ); + state int i990430; for(i990430 = 0; i990430 < 5; i990430++) { + outputStream.send( 415008 ); + try { + outputStream.send( 693494 ); + try { + outputStream.send( 281197 ); + int input = waitNext( inputStream ); + outputStream.send( input + 441269 ); + outputStream.send( 508213 ); + } catch (...) { + outputStream.send( 481511 ); + try { + outputStream.send( 902248 ); + continue; + } catch (...) { + outputStream.send( 801076 ); + state std::vector list833389; + list833389.push_back(1); + list833389.push_back(2); + list833389.push_back(3); + for( auto i : list833389 ) { + outputStream.send( 828677 ); + state int i413921; for(i413921 = 0; i413921 < 5; i413921++) { + outputStream.send( 972392 ); + state std::vector list209829; + list209829.push_back(1); + list209829.push_back(2); + list209829.push_back(3); + for( auto i : list209829 ) { + outputStream.send( 986318 ); + throw operation_failed(); + } + outputStream.send( 756646 ); + } + outputStream.send( 685268 ); + } + outputStream.send( 570856 ); + } + outputStream.send( 193955 ); + } + outputStream.send( 432512 ); + } catch (...) { + outputStream.send( 373686 ); + if ( (++ifstate&1) == 1 ) { + outputStream.send( 627374 ); + int input = waitNext( inputStream ); + outputStream.send( input + 597617 ); + outputStream.send( 837374 ); + } + outputStream.send( 297526 ); + } + outputStream.send( 683444 ); + } + outputStream.send( 871558 ); + state std::vector list684364; + list684364.push_back(1); + list684364.push_back(2); + list684364.push_back(3); + for( auto i : list684364 ) { + outputStream.send( 562891 ); + break; + } + outputStream.send( 568138 ); + } + outputStream.send( 510866 ); + } + outputStream.send( 760603 ); + } + outputStream.send( 396539 ); + } + outputStream.send( 294563 ); + } catch (...) { + outputStream.send( 341975 ); + throw_operation_failed(); + outputStream.send( 533359 ); + } + outputStream.send( 313661 ); + } + return 330816; +} + +ACTOR Future actorFuzz23( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + try { + outputStream.send( 803855 ); + try { + outputStream.send( 187873 ); + throw operation_failed(); + } catch (...) { + outputStream.send( 107262 ); + if ( (++ifstate&1) == 0 ) { + outputStream.send( 903833 ); + return 122479; + } + outputStream.send( 129084 ); + throw operation_failed(); + } + } catch (...) { + outputStream.send( 193646 ); + int input = waitNext( inputStream ); + outputStream.send( input + 123221 ); + outputStream.send( 336636 ); + } + return 823727; +} + +ACTOR Future actorFuzz24( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state std::vector list353113; + list353113.push_back(1); + list353113.push_back(2); + list353113.push_back(3); + for( auto i : list353113 ) { + outputStream.send( 904160 ); + try { + outputStream.send( 630322 ); + if ( (++ifstate&1) == 1 ) { + outputStream.send( 971556 ); + throw_operation_failed(); + outputStream.send( 693412 ); + } + outputStream.send( 102553 ); + continue; + } catch (...) { + outputStream.send( 284758 ); + state std::vector list964534; + list964534.push_back(1); + list964534.push_back(2); + list964534.push_back(3); + for( auto i : list964534 ) { + outputStream.send( 597252 ); + return 236723; + } + outputStream.send( 482123 ); + if ( (++ifstate&1) == 0 ) { + outputStream.send( 523339 ); + int input = waitNext( inputStream ); + outputStream.send( input + 223842 ); + outputStream.send( 423422 ); + } + outputStream.send( 683377 ); + } + outputStream.send( 415173 ); + } + return 660359; +} + +ACTOR Future actorFuzz25( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + try { + outputStream.send( 450503 ); + int input = waitNext( inputStream ); + outputStream.send( input + 146899 ); + outputStream.send( 754498 ); + } catch (...) { + outputStream.send( 898315 ); + return 869882; + } + return 861489; +} + +ACTOR Future actorFuzz26( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + return 355765; +} + +ACTOR Future actorFuzz27( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + int input = waitNext( inputStream ); + outputStream.send( input + 862579 ); + return 567151; +} + +ACTOR Future actorFuzz28( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + try { + outputStream.send( 190685 ); + state std::vector list488175; + list488175.push_back(1); + list488175.push_back(2); + list488175.push_back(3); + for( auto i : list488175 ) { + outputStream.send( 107570 ); + state std::vector list925612; + list925612.push_back(1); + list925612.push_back(2); + list925612.push_back(3); + for( auto i : list925612 ) { + outputStream.send( 430892 ); + throw_operation_failed(); + outputStream.send( 705871 ); + } + outputStream.send( 102606 ); + } + outputStream.send( 619003 ); + } catch (...) { + outputStream.send( 787855 ); + state std::vector list959628; + list959628.push_back(1); + list959628.push_back(2); + list959628.push_back(3); + for( auto i : list959628 ) { + outputStream.send( 934043 ); + state int i699601; for(i699601 = 0; i699601 < 5; i699601++) { + outputStream.send( 815071 ); + continue; + } + outputStream.send( 868540 ); + } + outputStream.send( 978175 ); + } + return 887701; +} + +ACTOR Future actorFuzz29( FutureStream inputStream, PromiseStream outputStream, Future error ) { + state int ifstate = 0; + state std::vector list757591; + list757591.push_back(1); + list757591.push_back(2); + list757591.push_back(3); + for( auto i : list757591 ) { + outputStream.send( 121079 ); + throw operation_failed(); + } + outputStream.send( 814445 ); + state int i743927; for(i743927 = 0; i743927 < 5; i743927++) { + outputStream.send( 548422 ); + return 324302; + } + return 853491; +} + +std::pair actorFuzzTests() { + int testsOK = 0; + testsOK += testFuzzActor( &actorFuzz0, "actorFuzz0", (vector(),472952,424221) ); + testsOK += testFuzzActor( &actorFuzz1, "actorFuzz1", (vector(),896041,406985) ); + testsOK += testFuzzActor( &actorFuzz2, "actorFuzz2", (vector(),981337) ); + testsOK += testFuzzActor( &actorFuzz3, "actorFuzz3", (vector(),169417,910046,901015,238065,238065,238065,238065,238065,216530,721088,154168,509758,331070,154168,509758,331070,154168,509758,331070,154168,509758,331070,154168,509758,331070,728291,150168) ); + testsOK += testFuzzActor( &actorFuzz4, "actorFuzz4", (vector(),924232) ); + testsOK += testFuzzActor( &actorFuzz5, "actorFuzz5", (vector(),793665,473530,728257,239736) ); + testsOK += testFuzzActor( &actorFuzz6, "actorFuzz6", (vector(),228205,686961,1000) ); + testsOK += testFuzzActor( &actorFuzz7, "actorFuzz7", (vector(),388211,388211,388211,388211,388211,215808) ); + testsOK += testFuzzActor( &actorFuzz8, "actorFuzz8", (vector(),906334,906334,906334,190561) ); + testsOK += testFuzzActor( &actorFuzz9, "actorFuzz9", (vector(),159221) ); + testsOK += testFuzzActor( &actorFuzz10, "actorFuzz10", (vector(),713197,195782) ); + testsOK += testFuzzActor( &actorFuzz11, "actorFuzz11", (vector(),715504,404836,687853,715504,404836,687853,715504,404836,687853,416948) ); + testsOK += testFuzzActor( &actorFuzz12, "actorFuzz12", (vector(),955805) ); + testsOK += testFuzzActor( &actorFuzz13, "actorFuzz13", (vector(),619715,528509,647852,619715,255314,619715,528509,647852,619715,255314,619715,528509,647852,391920) ); + testsOK += testFuzzActor( &actorFuzz14, "actorFuzz14", (vector(),422579,665867,422579,665867,422579,665867,422579,665867,422579,665867,798692) ); + testsOK += testFuzzActor( &actorFuzz15, "actorFuzz15", (vector(),463317,863528,144528,1000) ); + testsOK += testFuzzActor( &actorFuzz16, "actorFuzz16", (vector(),488500,436335,838746,488500,437335,838746,488500,438335,838746,241009) ); + testsOK += testFuzzActor( &actorFuzz17, "actorFuzz17", (vector(),499302) ); + testsOK += testFuzzActor( &actorFuzz18, "actorFuzz18", (vector(),1000) ); + testsOK += testFuzzActor( &actorFuzz19, "actorFuzz19", (vector(),217963,928990,833320,981835) ); + testsOK += testFuzzActor( &actorFuzz20, "actorFuzz20", (vector(),668336) ); + testsOK += testFuzzActor( &actorFuzz21, "actorFuzz21", (vector(),104331,404486) ); + testsOK += testFuzzActor( &actorFuzz22, "actorFuzz22", (vector(),173342,133654,317492,976956,294563,313661,330816) ); + testsOK += testFuzzActor( &actorFuzz23, "actorFuzz23", (vector(),803855,187873,107262,129084,193646,124221,336636,823727) ); + testsOK += testFuzzActor( &actorFuzz24, "actorFuzz24", (vector(),904160,630322,971556,284758,597252,236723) ); + testsOK += testFuzzActor( &actorFuzz25, "actorFuzz25", (vector(),450503,147899,754498,861489) ); + testsOK += testFuzzActor( &actorFuzz26, "actorFuzz26", (vector(),355765) ); + testsOK += testFuzzActor( &actorFuzz27, "actorFuzz27", (vector(),863579,567151) ); + testsOK += testFuzzActor( &actorFuzz28, "actorFuzz28", (vector(),190685,107570,430892,787855,934043,815071,815071,815071,815071,815071,868540,934043,815071,815071,815071,815071,815071,868540,934043,815071,815071,815071,815071,815071,868540,978175,887701) ); + testsOK += testFuzzActor( &actorFuzz29, "actorFuzz29", (vector(),121079,1000) ); + return std::make_pair(testsOK, 30); +} + +#endif // WIN32 diff --git a/fdbrpc/ActorFuzz.h b/fdbrpc/ActorFuzz.h new file mode 100644 index 0000000000..bb1ee4dd91 --- /dev/null +++ b/fdbrpc/ActorFuzz.h @@ -0,0 +1,42 @@ +/* + * ActorFuzz.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/flow.h" +#include + +using std::vector; + +inline vector& operator , (vector& v, int a) { + v.push_back(a); + return v; +} + +inline vector& operator , (vector const& v, int a) { + return (const_cast&>(v), a); +} +inline void throw_operation_failed() { throw operation_failed(); } + +// This is in dsltest.actor.cpp: +bool testFuzzActor( Future(*actor)(FutureStream const&, PromiseStream const&, Future const&), const char* desc, vector const& expectedOutput ); + +// This is defined by ActorFuzz.actor.cpp (generated by actorFuzz.py) +// Returns (tests passed, tests total) +std::pair actorFuzzTests(); diff --git a/fdbrpc/AsyncFileBlobStore.actor.cpp b/fdbrpc/AsyncFileBlobStore.actor.cpp new file mode 100644 index 0000000000..f405d06e6d --- /dev/null +++ b/fdbrpc/AsyncFileBlobStore.actor.cpp @@ -0,0 +1,81 @@ +/* + * AsyncFileBlobStore.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "AsyncFileBlobStore.actor.h" +#include "AsyncFileReadAhead.actor.h" +#include "flow/UnitTest.h" + +Future AsyncFileBlobStoreRead::size() { + if(!m_size.isValid()) + m_size = m_bstore->objectSize(m_bucket, m_object); + return m_size; +} + +Future AsyncFileBlobStoreRead::read( void *data, int length, int64_t offset ) { + return m_bstore->readObject(m_bucket, m_object, data, length, offset); +} + + +ACTOR Future sendStuff(int id, Reference t, int bytes) { + printf("Starting fake sender %d which will send send %d bytes.\n", id, bytes); + state double ts = timer(); + state int total = 0; + while(total < bytes) { + state int r = std::min(g_random->randomInt(0,1000), bytes - total); + Void _ = wait(t->getAllowance(r)); + total += r; + } + double dur = timer() - ts; + printf("Sender %d: Sent %d in %fs, %f/s\n", id, total, dur, total/dur); + return Void(); +} + +TEST_CASE("backup/throttling") { + // Test will not work in simulation. + if(g_network->isSimulated()) + return Void(); + + state int limit = 100000; + state Reference t(new SpeedLimit(limit, 1)); + + state int id = 1; + std::vector> f; + state double ts = timer(); + state int total = 0; + int s; + s = 500000; + f.push_back(sendStuff(id++, t, s)); total += s; + f.push_back(sendStuff(id++, t, s)); total += s; + s = 50000; + f.push_back(sendStuff(id++, t, s)); total += s; + f.push_back(sendStuff(id++, t, s)); total += s; + s = 5000; + f.push_back(sendStuff(id++, t, s)); total += s; + + Void _ = wait(waitForAll(f)); + double dur = timer() - ts; + int speed = int(total / dur); + printf("Speed limit was %d, measured speed was %d\n", limit, speed); + ASSERT(abs(speed - limit) / limit < .01); + + return Void(); +} + + diff --git a/fdbrpc/AsyncFileBlobStore.actor.h b/fdbrpc/AsyncFileBlobStore.actor.h new file mode 100644 index 0000000000..df02687685 --- /dev/null +++ b/fdbrpc/AsyncFileBlobStore.actor.h @@ -0,0 +1,283 @@ +/* + * AsyncFileBlobStore.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FDBRPC_ASYNCFILEBLOBSTORE_ACTOR_G_H) + #define FDBRPC_ASYNCFILEBLOBSTORE_ACTOR_G_H + #include "AsyncFileBlobStore.actor.g.h" +#elif !defined(FDBRPC_ASYNCFILEBLOBSTORE_ACTOR_H) + #define FDBRPC_ASYNCFILEBLOBSTORE_ACTOR_H + +#include +#include + +#include "IAsyncFile.h" +#include "flow/serialize.h" +#include "flow/Net2Packet.h" +#include "IRateControl.h" +#include "BlobStore.h" +#include "md5/md5.h" +#include "libb64/encode.h" + +// This class represents a write-only file that lives in an S3-style blob store. It writes using the REST API, +// using multi-part upload and beginning to transfer each part as soon as it is large enough. +// All write operations file operations must be sequential and contiguous. +// Limits on part sizes, upload speed, and concurrent uploads are taken from the BlobStoreEndpoint being used. +class AsyncFileBlobStoreWrite : public IAsyncFile, public ReferenceCounted { +public: + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + struct Part : ReferenceCounted { + Part(int n) : number(n), writer(content.getWriteBuffer(), NULL, Unversioned()), length(0) { + etag = std::string(); + ::MD5_Init(&content_md5_buf); + } + virtual ~Part() { + etag.cancel(); + } + Future etag; + int number; + UnsentPacketQueue content; + std::string md5string; + PacketWriter writer; + int length; + void write(const uint8_t *buf, int len) { + writer.serializeBytes(buf, len); + ::MD5_Update(&content_md5_buf, buf, len); + length += len; + } + // MD5 sum can only be finalized once, further calls will do nothing so new writes will be reflected in the sum. + void finalizeMD5() { + if(md5string.empty()) { + std::string sumBytes; + sumBytes.resize(16); + ::MD5_Final((unsigned char *)sumBytes.data(), &content_md5_buf); + md5string = base64::encoder::from_string(sumBytes); + md5string.resize(md5string.size() - 1); + } + } + + private: + MD5_CTX content_md5_buf; + }; + + virtual Future read( void *data, int length, int64_t offset ) { throw file_not_readable(); } + + ACTOR static Future write_impl(Reference f, const uint8_t *data, int length) { + state Part *p = f->m_parts.back().getPtr(); + // If this write will cause the part to cross the min part size boundary then write to the boundary and start a new part. + while(p->length + length >= f->m_bstore->knobs.multipart_min_part_size) { + // Finish off this part + int finishlen = f->m_bstore->knobs.multipart_min_part_size - p->length; + p->write((const uint8_t *)data, finishlen); + + // Adjust source buffer args + length -= finishlen; + data = (const uint8_t *)data + finishlen; + + // End current part (and start new one) + Void _ = wait(f->endCurrentPart(true)); + p = f->m_parts.back().getPtr(); + } + + p->write((const uint8_t *)data, length); + return Void(); + } + + virtual Future write( void const *data, int length, int64_t offset ) { + if(offset != m_cursor) + throw non_sequential_op(); + m_cursor += length; + + return write_impl(Reference::addRef(this), (const uint8_t *)data, length); + } + + virtual Future truncate( int64_t size ) { + if(size != m_cursor) + return non_sequential_op(); + return Void(); + } + + ACTOR static Future doPartUpload(Reference f, Reference p) { + try { + p->finalizeMD5(); + std::string upload_id = wait(f->getUploadID()); + std::string etag = wait(f->m_bstore->uploadPart(f->m_bucket, f->m_object, upload_id, p->number, &p->content, p->length, p->md5string)); + return etag; + } catch(Error &e) { + throw; + } + } + + ACTOR static Future doFinishUpload(Reference f) { + // If there is only 1 part then it has not yet been uploaded so just write the whole file at once. + if(f->m_parts.size() == 1) { + Reference part = f->m_parts.back(); + part->finalizeMD5(); + Void _ = wait(f->m_bstore->writeEntireFileFromBuffer(f->m_bucket, f->m_object, &part->content, part->length, part->md5string)); + return Void(); + } + + // There are at least 2 parts. End the last part (which could be empty) + Void _ = wait(f->endCurrentPart()); + + state BlobStoreEndpoint::MultiPartSetT partSet; + state std::vector>::iterator p; + + // Wait for all the parts to be done to get their ETags, populate the partSet required to finish the object upload. + for(p = f->m_parts.begin(); p != f->m_parts.end(); ++p) { + std::string tag = wait((*p)->etag); + if((*p)->length > 0) // The last part might be empty and has to be omitted. + partSet[(*p)->number] = tag; + } + + // No need to wait for the upload ID here because the above loop waited for all the parts and each part required the upload ID so it is ready + Void _ = wait(f->m_bstore->finishMultiPartUpload(f->m_bucket, f->m_object, f->m_upload_id.get(), partSet)); + + return Void(); + } + + // Ready once all data has been sent AND acknowledged from the remote side + virtual Future sync() { + if(m_cursor == 0) + throw file_not_writable(); + + // Only initiate the finish operation once, and also prevent further writing. + if(!m_finished.isValid()) { + m_finished = doFinishUpload(Reference::addRef(this)); + m_cursor = -1; // Cause future write attempts to fail + } + + return m_finished; + } + + // + // Flush can't really do what the caller would "want" for a blob store file. The caller would probably notionally want + // all bytes written to be at least in transit to the blob store, but that is not very feasible. The blob store + // has a minimum size requirement for all but the final part, and parts must be sent with a header that specifies + // their size. So in the case of a write buffer that does not meet the part minimum size the part could be sent + // but then if there is any more data written then that part needs to be sent again in its entirety. So a client + // that calls flush often could generate far more blob store write traffic than they intend to. + virtual Future flush() { return Void(); } + + virtual Future size() { return m_cursor; } + + virtual Future readZeroCopy( void** data, int* length, int64_t offset ) { + TraceEvent(SevError, "ReadZeroCopyNotSupported").detail("FileType", "BlobStoreWrite"); + return platform_error(); + } + virtual void releaseZeroCopy( void* data, int length, int64_t offset ) {} + + virtual int64_t debugFD() { return -1; } + + virtual ~AsyncFileBlobStoreWrite() { + m_upload_id.cancel(); + m_finished.cancel(); + for(auto &p : m_parts) + p->etag.cancel(); + } + + virtual std::string getFilename() { return m_object; } + +private: + Reference m_bstore; + std::string m_bucket; + std::string m_object; + + int64_t m_cursor; + + Future m_upload_id; + Future m_finished; + std::vector> m_parts; + + Future endCurrentPart(bool startNew = false) { + if(m_parts.back()->length == 0) + return Void(); + + // Start the upload + m_parts.back()->etag = doPartUpload(Reference::addRef(this), m_parts.back()); + + // Make a new part to write to + if(startNew) + m_parts.push_back(Reference(new Part(m_parts.size() + 1))); + + return Void(); + } + + Future getUploadID() { + if(!m_upload_id.isValid()) + m_upload_id = m_bstore->beginMultiPartUpload(m_bucket, m_object); + return m_upload_id; + } + +public: + AsyncFileBlobStoreWrite(Reference bstore, std::string bucket, std::string object) + : m_bstore(bstore), m_bucket(bucket), m_object(object), m_cursor(0) { + + // Add first part + m_parts.push_back(Reference(new Part(1))); + } + +}; + + +// This class represents a read-only file that lives in an S3-style blob store. It reads using the REST API. +class AsyncFileBlobStoreRead : public IAsyncFile, public ReferenceCounted { +public: + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + virtual Future read( void *data, int length, int64_t offset ); + + virtual Future write( void const *data, int length, int64_t offset ) { throw file_not_writable(); } + virtual Future truncate( int64_t size ) { throw file_not_writable(); } + + virtual Future sync() { return Void(); } + virtual Future flush() { return Void(); } + + virtual Future size(); + + virtual Future readZeroCopy( void** data, int* length, int64_t offset ) { + TraceEvent(SevError, "ReadZeroCopyNotSupported").detail("FileType", "BlobStoreRead"); + return platform_error(); + } + virtual void releaseZeroCopy( void* data, int length, int64_t offset ) {} + + virtual int64_t debugFD() { return -1; } + + virtual std::string getFilename() { return m_object; } + + virtual ~AsyncFileBlobStoreRead() {} + + Reference m_bstore; + std::string m_bucket; + std::string m_object; + Future m_size; + + AsyncFileBlobStoreRead(Reference bstore, std::string bucket, std::string object) + : m_bstore(bstore), m_bucket(bucket), m_object(object) { + } + +}; + +#endif diff --git a/fdbrpc/AsyncFileCached.actor.cpp b/fdbrpc/AsyncFileCached.actor.cpp new file mode 100644 index 0000000000..bbee87f2af --- /dev/null +++ b/fdbrpc/AsyncFileCached.actor.cpp @@ -0,0 +1,239 @@ +/* + * AsyncFileCached.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "AsyncFileCached.actor.h" + +//Page caches used in non-simulated environments +Optional> pc4k, pc64k; + +//The simulator needs to store separate page caches for each machine +static std::map, Reference>> simulatorPageCaches; + +EvictablePage::~EvictablePage() { + if (data) { + if (pageCache->pageSize == 4096) + FastAllocator<4096>::release(data); + else + aligned_free(data); + } + if (index > -1) { + pageCache->pages[index] = pageCache->pages.back(); + pageCache->pages[index]->index = index; + pageCache->pages.pop_back(); + } +} + +std::map< std::string, OpenFileInfo > AsyncFileCached::openFiles; + +void AsyncFileCached::remove_page( AFCPage* page ) { + pages.erase( page->pageOffset ); +} + +Future> AsyncFileCached::open_impl( std::string filename, int flags, int mode ) { + Reference pageCache; + + //In a simulated environment, each machine needs its own caches + if(g_network->isSimulated()) { + auto cacheItr = simulatorPageCaches.find(g_network->getLocalAddress()); + if(cacheItr == simulatorPageCaches.end()) { + int64_t pageCacheSize4k = (BUGGIFY) ? FLOW_KNOBS->BUGGIFY_SIM_PAGE_CACHE_4K : FLOW_KNOBS->SIM_PAGE_CACHE_4K; + int64_t pageCacheSize64k = (BUGGIFY) ? FLOW_KNOBS->BUGGIFY_SIM_PAGE_CACHE_64K : FLOW_KNOBS->SIM_PAGE_CACHE_64K; + auto caches = std::make_pair(Reference(new EvictablePageCache(4096, pageCacheSize4k)), Reference(new EvictablePageCache(65536, pageCacheSize64k))); + simulatorPageCaches[g_network->getLocalAddress()] = caches; + pageCache = (flags & IAsyncFile::OPEN_LARGE_PAGES) ? caches.second : caches.first; + } + else + pageCache = (flags & IAsyncFile::OPEN_LARGE_PAGES) ? cacheItr->second.second : cacheItr->second.first; + } + else { + if(flags & IAsyncFile::OPEN_LARGE_PAGES) { + if(!pc64k.present()) pc64k = Reference(new EvictablePageCache(65536, FLOW_KNOBS->PAGE_CACHE_64K)); + pageCache = pc64k.get(); + } else { + if(!pc4k.present()) pc4k = Reference(new EvictablePageCache(4096, FLOW_KNOBS->PAGE_CACHE_4K)); + pageCache = pc4k.get(); + } + } + + return open_impl(filename, flags, mode, pageCache); +} + +Future AsyncFileCached::read_write_impl( AsyncFileCached* self, void* data, int length, int64_t offset, bool writing ) { + if (writing) { + if (offset + length > self->length) + self->length = offset + length; + } + + std::vector> actors; + + uint8_t* cdata = static_cast(data); + + int offsetInPage = offset % self->pageCache->pageSize; + int64_t pageOffset = offset - offsetInPage; + + int remaining = length; + + while (remaining) { + ++self->countFileCacheFinds; + ++self->countCacheFinds; + auto p = self->pages.find( pageOffset ); + if ( p == self->pages.end() ) { + AFCPage* page = new AFCPage( self, pageOffset ); + p = self->pages.insert( std::make_pair(pageOffset, page) ).first; + } + + int bytesInPage = std::min(self->pageCache->pageSize - offsetInPage, remaining); + + auto w = writing + ? p->second->write( cdata, bytesInPage, offsetInPage ) + : p->second->read( cdata, bytesInPage, offsetInPage ); + if (!w.isReady() || w.isError()) + actors.push_back( w ); + + cdata += bytesInPage; + pageOffset += self->pageCache->pageSize; + offsetInPage = 0; + + remaining -= bytesInPage; + } + + //This is susceptible to the introduction of waits on the read/write path: no wait can occur prior to AFCPage::readThrough + //or prevLength will be set prematurely + self->prevLength = self->length; + + return waitForAll( actors ); +} + +Future AsyncFileCached::readZeroCopy( void** data, int* length, int64_t offset ) { + ++countFileCacheReads; + ++countCacheReads; + + // Only aligned page reads are zero-copy + if (*length != pageCache->pageSize || (offset & (pageCache->pageSize-1)) || offset + *length > this->length) + return io_error(); + + auto p = pages.find( offset ); + if ( p == pages.end() ) { + AFCPage* page = new AFCPage( this, offset ); + p = pages.insert( std::make_pair(offset, page) ).first; + } + + *data = p->second->data; + + return p->second->readZeroCopy(); +} +void AsyncFileCached::releaseZeroCopy( void* data, int length, int64_t offset ) { + ASSERT( length == pageCache->pageSize && !(offset & (pageCache->pageSize-1)) && offset + length <= this->length); + auto p = pages.find( offset ); + ASSERT( p != pages.end() && p->second->data == data ); + p->second->releaseZeroCopy(); +} + +Future AsyncFileCached::truncate( int64_t size ) { + ++countFileCacheWrites; + ++countCacheWrites; + + std::vector> actors; + + int offsetInPage = size % pageCache->pageSize; + int64_t pageOffset = size - offsetInPage; + + if(offsetInPage == 0 && size == length) { + return Void(); + } + + length = size; + prevLength = size; + + if (offsetInPage) { + TEST(true); // Truncating to the middle of a page + auto p = pages.find(pageOffset); + if (p != pages.end()) { + auto f = p->second->flush(); + if (!f.isReady() || f.isError()) + actors.push_back(f); + } + else { + TEST(true); // Truncating to the middle of a page that isn't in cache + } + + pageOffset += pageCache->pageSize; + } + /* + for ( auto p = pages.lower_bound( pageOffset ); p != pages.end(); p = pages.erase(p) ) { + auto f = p->second->truncate(); + if ( !f.isReady() || f.isError()) + actors.push_back( f ); + } + */ + + for ( auto p = pages.begin(); p != pages.end(); ) { + if ( p->first >= pageOffset ) { + auto f = p->second->truncate(); + if ( !f.isReady() || f.isError() ) + actors.push_back( f ); + auto last = p; + ++p; + pages.erase(last); + } else + ++p; + } + + return truncate_impl( this, size, waitForAll( actors ) ); +} + +Future AsyncFileCached::flush() { + ++countFileCacheWrites; + ++countCacheWrites; + + std::vector> unflushed; + + int debug_count = flushable.size(); + for(int i=0; iflush(); + if (!f.isReady() || f.isError()) unflushed.push_back( f ); + ASSERT( (i AsyncFileCached::quiesce() { + std::vector> unquiescent; + + for( auto i = pages.begin(); i != pages.end(); ++i ) { + auto f = i->second->quiesce(); + if( !f.isReady() ) unquiescent.push_back( f ); + } + + //Errors are absorbed because we need everything to finish + return waitForAllReady(unquiescent); +} + +AsyncFileCached::~AsyncFileCached() { + while ( !pages.empty() ) { + auto ok = pages.begin()->second->evict(); + ASSERT( ok ); + } + openFiles.erase( filename ); +} diff --git a/fdbrpc/AsyncFileCached.actor.h b/fdbrpc/AsyncFileCached.actor.h new file mode 100644 index 0000000000..273d5c2cba --- /dev/null +++ b/fdbrpc/AsyncFileCached.actor.h @@ -0,0 +1,496 @@ +/* + * AsyncFileCached.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FLOW_ASYNCFILECACHED_ACTOR_G_H) + #define FLOW_ASYNCFILECACHED_ACTOR_G_H + #include "AsyncFileCached.actor.g.h" +#elif !defined(FLOW_ASYNCFILECACHED_ACTOR_H) + #define FLOW_ASYNCFILECACHED_ACTOR_H + +#include "flow/flow.h" +#include "IAsyncFile.h" +#include "flow/Knobs.h" +#include "flow/TDMetric.actor.h" +#include "flow/network.h" + +struct EvictablePage { + void* data; + int index; + class Reference pageCache; + + virtual bool evict() = 0; // true if page was evicted, false if it isn't immediately evictable (but will be evicted regardless if possible) + + EvictablePage(Reference pageCache) : data(0), index(-1), pageCache(pageCache) {} + virtual ~EvictablePage(); +}; + +struct EvictablePageCache : ReferenceCounted { + EvictablePageCache() : pageSize(0), maxPages(0) {} + explicit EvictablePageCache(int pageSize, int64_t maxSize) : pageSize(pageSize), maxPages(maxSize / pageSize) {} + + void allocate(EvictablePage* page) { + try_evict(); + try_evict(); + page->data = pageSize == 4096 ? FastAllocator<4096>::allocate() : aligned_alloc(4096,pageSize); + page->index = pages.size(); + pages.push_back(page); + } + + void try_evict() { + if (pages.size() >= (uint64_t)maxPages && !pages.empty()) { + for (int i = 0; i < FLOW_KNOBS->MAX_EVICT_ATTEMPTS; i++) { // If we don't manage to evict anything, just go ahead and exceed the cache limit + int toEvict = g_random->randomInt(0, pages.size()); + if (pages[toEvict]->evict()) + break; + } + } + } + + std::vector pages; + int pageSize; + int64_t maxPages; +}; + +struct OpenFileInfo : NonCopyable { + IAsyncFile* f; + Future> opened; // Only valid until the file is fully opened + + OpenFileInfo() : f(0) {} + OpenFileInfo(OpenFileInfo && r) noexcept(true) : f(r.f), opened(std::move(r.opened)) { r.f = 0; } + + Future> get() { + if (f) return Reference::addRef(f); + else return opened; + } +}; + +struct AFCPage; + +class AsyncFileCached : public IAsyncFile, public ReferenceCounted { + friend struct AFCPage; + +public: + static Future> open( std::string filename, int flags, int mode ) { + //TraceEvent("AsyncFileCachedOpen").detail("Filename", filename); + if ( openFiles.find(filename) == openFiles.end() ) { + auto f = open_impl( filename, flags, mode ); + if ( f.isReady() && f.isError() ) + return f; + if( !f.isReady() ) + openFiles[filename].opened = f; + else + return f.get(); + } + return openFiles[filename].get(); + } + + virtual Future read( void* data, int length, int64_t offset ) { + ++countFileCacheReads; + ++countCacheReads; + if (offset + length > this->length) { + length = int(this->length - offset); + ASSERT(length >= 0); + } + auto f = read_write_impl(this, data, length, offset, false); + if( f.isReady() && !f.isError() ) return length; + ++countFileCacheReadsBlocked; + ++countCacheReadsBlocked; + return tag(f,length); + } + + virtual Future write( void const* data, int length, int64_t offset ) { + ++countFileCacheWrites; + ++countCacheWrites; + auto f = read_write_impl(this, const_cast(data), length, offset, true); + if (!f.isReady()) { + ++countFileCacheWritesBlocked; + ++countCacheWritesBlocked; + } + return f; + } + + virtual Future readZeroCopy( void** data, int* length, int64_t offset ); + virtual void releaseZeroCopy( void* data, int length, int64_t offset ); + + virtual Future truncate( int64_t size ); + + ACTOR Future truncate_impl( AsyncFileCached* self, int64_t size, Future truncates ) { + Void _ = wait( truncates ); + Void _ = wait( self->uncached->truncate( size ) ); + return Void(); + } + + virtual Future sync() { + return waitAndSync( this, flush() ); + } + + virtual Future size() { + return length; + } + + virtual int64_t debugFD() { + return uncached->debugFD(); + } + + virtual std::string getFilename() { + return filename; + } + + virtual void addref() { + ReferenceCounted::addref(); + //TraceEvent("AsyncFileCachedAddRef").detail("Filename", filename).detail("Refcount", debugGetReferenceCount()).backtrace(); + } + virtual void delref() { + if (delref_no_destroy()) { + // If this is ever ThreadSafeReferenceCounted... + // setrefCountUnsafe(0); + + auto f = quiesce(); + //TraceEvent("AsyncFileCachedDel").detail("Filename", filename) + // .detail("Refcount", debugGetReferenceCount()).detail("CanDie", f.isReady()).backtrace(); + if (f.isReady()) + delete this; + else + uncancellable( holdWhile( Reference::addRef( this ), f ) ); + } + } + + ~AsyncFileCached(); + +private: + static std::map< std::string, OpenFileInfo > openFiles; + std::string filename; + Reference uncached; + int64_t length; + int64_t prevLength; + std::unordered_map pages; + std::vector flushable; + Reference pageCache; + + Int64MetricHandle countFileCacheFinds; + Int64MetricHandle countFileCacheReads; + Int64MetricHandle countFileCacheWrites; + Int64MetricHandle countFileCacheReadsBlocked; + Int64MetricHandle countFileCacheWritesBlocked; + Int64MetricHandle countFileCachePageReadsMerged; + Int64MetricHandle countFileCacheReadBytes; + + Int64MetricHandle countCacheFinds; + Int64MetricHandle countCacheReads; + Int64MetricHandle countCacheWrites; + Int64MetricHandle countCacheReadsBlocked; + Int64MetricHandle countCacheWritesBlocked; + Int64MetricHandle countCachePageReadsMerged; + Int64MetricHandle countCacheReadBytes; + + AsyncFileCached( Reference uncached, const std::string& filename, int64_t length, Reference pageCache ) + : uncached(uncached), filename(filename), length(length), prevLength(length), pageCache(pageCache) { + if( !g_network->isSimulated() ) { + countFileCacheWrites.init( LiteralStringRef("AsyncFile.CountFileCacheWrites"), filename); + countFileCacheReads.init( LiteralStringRef("AsyncFile.CountFileCacheReads"), filename); + countFileCacheWritesBlocked.init( LiteralStringRef("AsyncFile.CountFileCacheWritesBlocked"), filename); + countFileCacheReadsBlocked.init( LiteralStringRef("AsyncFile.CountFileCacheReadsBlocked"), filename); + countFileCachePageReadsMerged.init(LiteralStringRef("AsyncFile.CountFileCachePageReadsMerged"), filename); + countFileCacheFinds.init( LiteralStringRef("AsyncFile.CountFileCacheFinds"), filename); + countFileCacheReadBytes.init( LiteralStringRef("AsyncFile.CountFileCacheReadBytes"), filename); + + countCacheWrites.init( LiteralStringRef("AsyncFile.CountCacheWrites")); + countCacheReads.init( LiteralStringRef("AsyncFile.CountCacheReads")); + countCacheWritesBlocked.init( LiteralStringRef("AsyncFile.CountCacheWritesBlocked")); + countCacheReadsBlocked.init( LiteralStringRef("AsyncFile.CountCacheReadsBlocked")); + countCachePageReadsMerged.init(LiteralStringRef("AsyncFile.CountCachePageReadsMerged")); + countCacheFinds.init( LiteralStringRef("AsyncFile.CountCacheFinds")); + countCacheReadBytes.init( LiteralStringRef("AsyncFile.CountCacheReadBytes")); + + } + } + + static Future> open_impl( std::string filename, int flags, int mode ); + + ACTOR static Future> open_impl( std::string filename, int flags, int mode, Reference pageCache ) { + try { + TraceEvent("AFCUnderlyingOpenBegin").detail("filename", filename); + if(flags & IAsyncFile::OPEN_CACHED_READ_ONLY) + flags = flags & ~IAsyncFile::OPEN_READWRITE | IAsyncFile::OPEN_READONLY; + else + flags = flags & ~IAsyncFile::OPEN_READONLY | IAsyncFile::OPEN_READWRITE; + state Reference f = wait( IAsyncFileSystem::filesystem()->open(filename, flags | IAsyncFile::OPEN_UNCACHED | IAsyncFile::OPEN_UNBUFFERED, mode) ); + TraceEvent("AFCUnderlyingOpenEnd").detail("filename", filename); + int64_t l = wait( f->size() ); + TraceEvent("AFCUnderlyingSize").detail("filename", filename).detail("size", l); + auto& of = openFiles[filename]; + of.f = new AsyncFileCached(f, filename, l, pageCache); + of.opened = Future>(); + return Reference( of.f ); + } catch (Error& e) { + if( e.code() != error_code_actor_cancelled ) + openFiles.erase( filename ); + throw e; + } + } + + virtual Future flush(); + + Future quiesce(); + + ACTOR static Future waitAndSync( AsyncFileCached* self, Future flush ) { + Void _ = wait( flush ); + Void _ = wait( self->uncached->sync() ); + return Void(); + } + + static Future read_write_impl( AsyncFileCached* self, void* data, int length, int64_t offset, bool writing ); + + static Future truncate_impl( AsyncFileCached* self, int64_t size ); + + void remove_page( AFCPage* page ); +}; + +struct AFCPage : public EvictablePage, public FastAllocated { + virtual bool evict() { + if ( notReading.isReady() && notFlushing.isReady() && !dirty && !zeroCopyRefCount && !truncated ) { + owner->remove_page( this ); + delete this; + return true; + } + + if (dirty) + flush(); + + return false; + } + + Future write( void const* data, int length, int offset ) { + ASSERT( !zeroCopyRefCount ); // overlapping zero-copy reads and writes are undefined behavior + setDirty(); + + if (valid || (offset == 0 && length == pageCache->pageSize)) { + valid = true; + memcpy( static_cast(this->data) + offset, data, length ); + return yield(); + } + + if (notReading.isReady()) { + notReading = readThrough( this ); + } + + notReading = waitAndWrite( this, data, length, offset ); + + return notReading; + } + + ACTOR static Future waitAndWrite( AFCPage* self, void const* data, int length, int offset ) { + Void _ = wait( self->notReading ); + memcpy( static_cast(self->data) + offset, data, length ); + return Void(); + } + + Future readZeroCopy() { + ++zeroCopyRefCount; + if (valid) return yield(); + + if (notReading.isReady()) { + notReading = readThrough( this ); + } else { + ++owner->countFileCachePageReadsMerged; + ++owner->countCachePageReadsMerged; + } + + return notReading; + } + void releaseZeroCopy() { + --zeroCopyRefCount; + ASSERT( zeroCopyRefCount >= 0 ); + } + + Future read( void* data, int length, int offset ) { + if (valid) { + owner->countFileCacheReadBytes += length; + owner->countCacheReadBytes += length; + memcpy( data, static_cast(this->data) + offset, length ); + return yield(); + } + + if (notReading.isReady()) { + notReading = readThrough( this ); + } else { + ++owner->countFileCachePageReadsMerged; + ++owner->countCachePageReadsMerged; + } + + notReading = waitAndRead( this, data, length, offset ); + + return notReading; + } + + ACTOR static Future waitAndRead( AFCPage* self, void* data, int length, int offset ) { + Void _ = wait( self->notReading ); + memcpy( data, static_cast(self->data) + offset, length ); + return Void(); + } + + ACTOR static Future readThrough( AFCPage* self ) { + ASSERT(!self->valid); + if ( self->pageOffset < self->owner->prevLength ) { + try { + int _ = wait( self->owner->uncached->read( self->data, self->pageCache->pageSize, self->pageOffset ) ); + if (_ != self->pageCache->pageSize) + TraceEvent("ReadThroughShortRead").detail("ReadAmount", _).detail("PageSize", self->pageCache->pageSize).detail("PageOffset", self->pageOffset); + } catch (Error& e) { + self->zeroCopyRefCount = 0; + TraceEvent("ReadThroughFailed").error(e); + throw; + } + } + self->valid = true; + return Void(); + } + + ACTOR static Future writeThrough( AFCPage* self, Promise writing ) { + // writeThrough can be called on a page that is not dirty, just to wait for a previous writeThrough to finish. In that + // case we don't want to do any disk I/O + try { + state bool dirty = self->dirty; + ++self->writeThroughCount; + self->updateFlushableIndex(); + + Void _ = wait( self->notReading && self->notFlushing ); + + if (dirty) { + if ( self->pageOffset + self->pageCache->pageSize > self->owner->length ) { + ASSERT(self->pageOffset < self->owner->length); + memset( static_cast(self->data) + self->owner->length - self->pageOffset, 0, self->pageCache->pageSize - (self->owner->length - self->pageOffset) ); + } + + auto f = self->owner->uncached->write( self->data, self->pageCache->pageSize, self->pageOffset ); + + Void _ = wait( f ); + } + } + catch(Error& e) { + --self->writeThroughCount; + self->setDirty(); + writing.sendError(e); + throw; + } + --self->writeThroughCount; + self->updateFlushableIndex(); + + writing.send(Void()); // FIXME: This could happen before the wait if AsyncFileKAIO dealt properly with overlapping write and sync operations + + self->pageCache->try_evict(); + + return Void(); + } + + Future flush() { + if (!dirty && notFlushing.isReady()) return Void(); + + ASSERT(valid || !notReading.isReady() || notReading.isError()); + + Promise writing; + + notFlushing = writeThrough( this, writing ); + + clearDirty(); // Do this last so that if writeThrough immediately calls try_evict, we can't be evicted before assigning notFlushing + return writing.getFuture(); + } + + Future quiesce() { + if (dirty) flush(); + + // If we are flushing, we will be quiescent when all flushes are finished + // Returning flush() isn't right, because flush can return before notFlushing.isReady() + if (!notFlushing.isReady()) { + return notFlushing; + } + + // else if we are reading, we will be quiescent when the read is finished + if ( !notReading.isReady() ) + return notReading; + + return Void(); + } + + Future truncate() { + ASSERT( !zeroCopyRefCount ); // overlapping zero-copy reads and writes are undefined behavior + truncated = true; + return truncate_impl( this ); + } + + ACTOR static Future truncate_impl( AFCPage* self ) { + Void _ = wait( self->notReading && self->notFlushing && yield() ); + delete self; + return Void(); + } + + AFCPage( AsyncFileCached* owner, int64_t offset ) : EvictablePage(owner->pageCache), owner(owner), pageOffset(offset), dirty(false), valid(false), truncated(false), notReading(Void()), notFlushing(Void()), zeroCopyRefCount(0), flushableIndex(-1), writeThroughCount(0) { + pageCache->allocate(this); + } + + virtual ~AFCPage() { + clearDirty(); + ASSERT( flushableIndex == -1 ); + } + + void setDirty() { + dirty = true; + updateFlushableIndex(); + } + + void clearDirty() { + dirty = false; + updateFlushableIndex(); + } + + void updateFlushableIndex() { + bool flushable = dirty || writeThroughCount; + if (flushable == (flushableIndex != -1)) return; + + if (flushable) { + flushableIndex = owner->flushable.size(); + owner->flushable.push_back(this); + } else { + ASSERT( owner->flushable[flushableIndex] == this ); + owner->flushable[flushableIndex] = owner->flushable.back(); + owner->flushable[flushableIndex]->flushableIndex = flushableIndex; + owner->flushable.pop_back(); + flushableIndex = -1; + } + } + + AsyncFileCached* owner; + int64_t pageOffset; + + Future notReading; // .isReady when a readThrough (or waitAndWrite) is not in progress + Future notFlushing; // .isReady when a writeThrough is not in progress + + bool dirty; // write has been called more recently than flush + bool valid; // data contains the file contents + bool truncated; // true if this page has been truncated + int writeThroughCount; // number of writeThrough actors that are in progress (potentially writing or waiting to write) + int flushableIndex; // index in owner->flushable[] + int zeroCopyRefCount; // references held by "zero-copy" reads +}; + +#endif diff --git a/fdbrpc/AsyncFileEIO.actor.h b/fdbrpc/AsyncFileEIO.actor.h new file mode 100644 index 0000000000..fc6dfeabec --- /dev/null +++ b/fdbrpc/AsyncFileEIO.actor.h @@ -0,0 +1,434 @@ +/* + * AsyncFileEIO.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#if defined(__unixish__) + +#define Net2AsyncFile AsyncFileEIO + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FLOW_ASYNCFILEEIO_ACTOR_G_H) + #define FLOW_ASYNCFILEEIO_ACTOR_G_H + #include "AsyncFileEIO.actor.g.h" +#elif !defined(FLOW_ASYNCFILEEIO_ACTOR_H) + #define FLOW_ASYNCFILEEIO_ACTOR_H + +#include "eio.h" +#include "flow/flow.h" +#include "flow/ThreadHelper.actor.h" +#include "IAsyncFile.h" +#include "flow/TDMetric.actor.h" + +#include +#include + +class AsyncFileEIO : public IAsyncFile, public ReferenceCounted { + +public: + static void init() { + if (eio_init( &eio_want_poll, NULL )) { + TraceEvent("EioInitError").detail("ErrorNo", errno); + throw platform_error(); + } + } + + static bool should_poll() { return want_poll; } + + static bool lock_fd( int fd ) { + // Acquire a "write" lock for the entire file + struct flock lockDesc; + lockDesc.l_type = F_WRLCK; + lockDesc.l_whence = SEEK_SET; + lockDesc.l_start = 0; + lockDesc.l_len = 0; // "Specifying 0 for l_len has the special meaning: lock all bytes starting at the location specified by l_whence and l_start through to the end of file, no matter how large the file grows." + lockDesc.l_pid = 0; + if (fcntl(fd, F_SETLK, &lockDesc) == -1) { + return false; + } + return true; + } + + ACTOR static Future> open( std::string filename, int flags, int mode, void* ignore ) { + std::string open_filename = filename; + if (flags & OPEN_ATOMIC_WRITE_AND_CREATE) { + ASSERT( (flags & OPEN_CREATE) && (flags & OPEN_READWRITE) && !(flags & OPEN_EXCLUSIVE) ); + open_filename = filename + ".part"; + } + + state Promise p; + state eio_req* r = eio_open( open_filename.c_str(), openFlags(flags), mode, 0, eio_callback, &p ); + try { Void _ = wait( p.getFuture() ); } catch (...) { eio_cancel(r); throw; } + if (r->result < 0) { + errno = r->errorno; + bool notFound = errno == ENOENT; + Error e = notFound ? file_not_found() : io_error(); + TraceEvent(notFound ? SevWarn : SevWarnAlways, "FileOpenError").error(e).GetLastError().detail("File", filename).detail("Flags", flags).detail("Mode", mode); + throw e; + } + TraceEvent("AsyncFileOpened").detail("Filename", filename).detail("fd", r->result).detail("Flags", flags); + + if ((flags & OPEN_LOCK) && !lock_fd(r->result)) { + TraceEvent(SevError, "UnableToLockFile").detail("filename", filename).GetLastError(); + throw io_error(); + } + + return Reference(new AsyncFileEIO( r->result, flags, filename )); + } + static Future deleteFile( std::string filename, bool mustBeDurable ) { + ::deleteFile(filename); + if (mustBeDurable) { + TEST(true); // deleteFile and fsync parent dir + return async_fsync_parent( filename ); + } else + return Void(); + } + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + virtual int64_t debugFD() { return fd; } + + virtual Future read( void* data, int length, int64_t offset ) { + ++countFileLogicalReads; + ++countLogicalReads; + return read_impl(fd, data, length, offset); + } + virtual Future write( void const* data, int length, int64_t offset ) // Copies data synchronously + { + ++countFileLogicalWrites; + ++countLogicalWrites; + //Standalone copy = StringRef((const uint8_t*)data, length); + return write_impl( fd, err, StringRef((const uint8_t*)data, length), offset ); + } + virtual Future truncate( int64_t size ) { + ++countFileLogicalWrites; + ++countLogicalWrites; + return truncate_impl( fd, err, size ); + } + virtual Future sync() { + ++countFileLogicalWrites; + ++countLogicalWrites; + auto fsync = sync_impl( fd, err ); + + if (flags & OPEN_ATOMIC_WRITE_AND_CREATE){ + flags &= ~OPEN_ATOMIC_WRITE_AND_CREATE; + + return waitAndAtomicRename( fsync, filename+".part", filename ); + } + + return fsync; + } + virtual Future size() { + ++countFileLogicalReads; + ++countLogicalReads; + + struct stat buf; + if (fstat( fd, &buf )) { + TraceEvent("AsyncFileEIOFStatError").detail("fd",fd).GetLastError(); + return io_error(); + } + return buf.st_size; + + //return size_impl(fd); + } + virtual std::string getFilename() { + return filename; + } + + ACTOR static Future async_fsync_parent( std::string filename ) { + std::string folder = parentDirectory( filename ); + TraceEvent("FSyncParentDir").detail("Folder", folder).detail("File", filename); + state int folderFD = ::open( folder.c_str(), O_DIRECTORY, 0 ); + if (folderFD<0) + throw io_error(); + try { + Void _ = wait( async_fsync(folderFD) ); // not sure if fdatasync on the folder has the same effect + } catch (...) { + close(folderFD); + throw; + } + close(folderFD); + return Void(); + } + + static Future async_fdatasync( int fd ) { + // Used by AsyncFileKAIO, since kernel AIO doesn't really implement fdatasync yet + return sync_impl( fd, Reference(new ErrorInfo) ); + } + static Future async_fsync( int fd ) { + // Used by AsyncFileKAIO, since kernel AIO doesn't really implement fsync yet + return sync_impl( fd, Reference(new ErrorInfo), true ); + } + ACTOR static Future waitAndAtomicRename( Future fsync, std::string part_filename, std::string final_filename ) { + // First wait for the data in the part file to be durable + Void _ = wait(fsync); + + // rename() is atomic + if (rename( part_filename.c_str(), final_filename.c_str() )) { + TraceEvent("AsyncFileEIORenameError").detail("filename", final_filename).GetLastError(); + throw io_error(); + } + + // fsync the parent directory to make it durable as well + Void _ = wait( async_fsync_parent(final_filename) ); + + return Void(); + } + + // Run the given function on the EIO thread pool and return its result + template static Future dispatch( std::function const& func ) { + return dispatch_impl(func); + } + + ~AsyncFileEIO() { close_impl( fd ); } + +private: + struct ErrorInfo : ReferenceCounted, FastAllocated { + Error err; + void set( const Error& e ) { + if (err.code() == invalid_error_code) + err = e; + } + void report() { + if (err.code() != invalid_error_code) + throw err; + } + }; + + template + struct Dispatch { + std::function func; + ErrorOr result; + Promise done; + explicit Dispatch( std::function const& func ) : func(func) {} + }; + + int fd, flags; + Reference err; + std::string filename; + Int64MetricHandle countFileLogicalWrites; + Int64MetricHandle countFileLogicalReads; + + Int64MetricHandle countLogicalWrites; + Int64MetricHandle countLogicalReads; + + AsyncFileEIO( int fd, int flags, std::string const& filename ) : fd(fd), flags(flags), filename(filename), err(new ErrorInfo) { + if( !g_network->isSimulated() ) { + countFileLogicalWrites.init(LiteralStringRef("AsyncFile.CountFileLogicalWrites"), filename); + countFileLogicalReads.init( LiteralStringRef("AsyncFile.CountFileLogicalReads"), filename); + + countLogicalWrites.init(LiteralStringRef("AsyncFile.CountLogicalWrites")); + countLogicalReads.init( LiteralStringRef("AsyncFile.CountLogicalReads")); + } + } + + static int openFlags(int flags) { + int oflags = 0; + ASSERT( bool(flags & OPEN_READONLY) != bool(flags & OPEN_READWRITE) ); // readonly xor readwrite + if( flags & OPEN_EXCLUSIVE ) oflags |= O_EXCL; + if( flags & OPEN_CREATE ) oflags |= O_CREAT; + if( flags & OPEN_READONLY ) oflags |= O_RDONLY; + if( flags & OPEN_READWRITE ) oflags |= O_RDWR; + if( flags & OPEN_ATOMIC_WRITE_AND_CREATE ) oflags |= O_TRUNC; + return oflags; + } + + static void error( const char* context, int fd, eio_req* r, Reference const& err = Reference() ) { + Error e = io_error(); + errno = r->errorno; + TraceEvent(context).detail("fd", fd).detail("Result", r->result).GetLastError().error(e); + if (err) err->set(e); + else throw e; + } + + ACTOR static void close_impl( int fd ) { + state Promise p; + state eio_req* r = eio_close(fd, 0, eio_callback, &p); + Void _ = wait( p.getFuture() ); + if (r->result) error( "CloseError", fd, r ); + TraceEvent("AsyncFileClosed").detail("fd", fd); + } + + ACTOR static Future read_impl( int fd, void* data, int length, int64_t offset ) { + state int taskID = g_network->getCurrentTask(); + state Promise p; + //fprintf(stderr, "eio_read (fd=%d length=%d offset=%lld)\n", fd, length, offset); + state eio_req* r = eio_read(fd, data, length, offset, 0, eio_callback, &p); + try { Void _ = wait( p.getFuture() ); } catch (...) { g_network->setCurrentTask( taskID ); eio_cancel(r); throw; } + try { + state int result = r->result; + //printf("eio read: %d/%d\n", r->result, length); + if (result == -1) { + error("ReadError", fd, r); + throw internal_error(); + } else { + Void _ = wait( delay(0, taskID) ); + return result; + } + } catch( Error &_e ) { + state Error e = _e; + Void _ = wait( delay(0, taskID) ); + throw e; + } + } + + ACTOR static Future write_impl( int fd, Reference err, StringRef data, int64_t offset ) { + state int taskID = g_network->getCurrentTask(); + state Promise p; + state eio_req* r = eio_write(fd, (void*)data.begin(), data.size(), offset, 0, eio_callback, &p); + try { Void _ = wait( p.getFuture() ); } catch (...) { g_network->setCurrentTask( taskID ); eio_cancel(r); throw; } + if (r->result != data.size()) error("WriteError", fd, r, err); + Void _ = wait( delay(0, taskID) ); + return Void(); + } + + ACTOR static Future truncate_impl( int fd, Reference err, int64_t size ) { + state int taskID = g_network->getCurrentTask(); + state Promise p; + state eio_req* r = eio_ftruncate(fd, size, 0, eio_callback, &p); + try { Void _ = wait( p.getFuture() ); } catch (...) { g_network->setCurrentTask( taskID ); eio_cancel(r); throw; } + if (r->result) error("TruncateError", fd, r, err); + Void _ = wait( delay(0, taskID) ); + return Void(); + } + + static eio_req* start_fsync( int fd, Promise& p, bool sync_metadata ) { + #ifdef __APPLE__ + // Neither fsync() nor fdatasync() do the right thing on OS X! + eio_req *req = (eio_req *)calloc (1, sizeof *req); + req->type = EIO_CUSTOM; + req->pri = 0; + req->finish = eio_callback; + req->data = &p; + req->destroy = free_req; + req->int1 = fd; + req->feed = apple_fsync; + eio_submit( req ); + return req; + #else + if (sync_metadata) + return eio_fsync( fd, 0, eio_callback, &p ); + else + return eio_fdatasync( fd, 0, eio_callback, &p ); + #endif + } + + ACTOR static Future sync_impl( int fd, Reference err, bool sync_metadata=false ) { + state int taskID = g_network->getCurrentTask(); + state Promise p; + state eio_req* r = start_fsync( fd, p, sync_metadata ); + + try { Void _ = wait( p.getFuture() ); } catch (...) { g_network->setCurrentTask( taskID ); eio_cancel(r); throw; } + try { + // Report any errors from prior write() or truncate() calls + err->report(); + + if (r->result) error("SyncError", fd, r); + Void _ = wait( delay(0, taskID) ); + return Void(); + } catch( Error &_e ) { + state Error e = _e; + Void _ = wait( delay(0, taskID) ); + throw e; + } + } + + ACTOR static Future size_impl( int fd ) { + state int taskID = g_network->getCurrentTask(); + state Promise p; + state eio_req* r = eio_fstat( fd, 0, eio_callback, &p ); + try { Void _ = wait( p.getFuture() ); } catch (...) { g_network->setCurrentTask( taskID ); eio_cancel(r); throw; } + if (r->result) error("StatError", fd, r); + EIO_STRUCT_STAT *statdata = (EIO_STRUCT_STAT *)r->ptr2; + if (!statdata) error("StatBufferError", fd, r); + state int64_t size = statdata->st_size; + free(statdata); + Void _ = wait( delay(0, taskID) ); + return size; + } + + ACTOR template static Future dispatch_impl( std::function func) { + state Dispatch data( func ); + state int taskID = g_network->getCurrentTask(); + + state eio_req* r = eio_custom( [](eio_req* req) { + // Runs on the eio thread pool + auto data = reinterpret_cast*>(req->data); + try { + data->result = data->func(); + req->result = 0; + } catch (Error& e) { + data->result = e; + req->result = -1; + } catch (...) { + data->result = unknown_error(); + req->result = -1; + } + }, 0, [](eio_req* req) { + // Runs on the main thread, in eio_poll() + if (EIO_CANCELLED (req)) return 0; + auto data = reinterpret_cast*>(req->data); + Promise p = std::move( data->done ); + p.send(Void()); + return 0; + }, &data); + try { Void _ = wait( data.done.getFuture() ); } catch (...) { g_network->setCurrentTask( taskID ); eio_cancel(r); throw; } + + Void _ = wait( delay(0, taskID) ); + if (data.result.isError()) throw data.result.getError(); + return data.result.get(); + } + + static volatile int32_t want_poll; + + ACTOR static void poll_eio() { + while (eio_poll() == -1) + Void _ = wait( yield() ); + want_poll = 0; + } + + static void eio_want_poll() { + want_poll = 1; + // SOMEDAY: NULL for deferred error, no analysis of correctness (itp) + onMainThreadVoid([](){ poll_eio(); }, NULL, TaskPollEIO); + } + + static int eio_callback( eio_req* req ) { + if (EIO_CANCELLED (req)) return 0; + Promise p = std::move( *(Promise*)req->data ); + p.send(Void()); + return 0; + } + + #ifdef __APPLE__ + static void apple_fsync( eio_req* req ) { + req->result = fcntl(req->int1, F_FULLFSYNC, 0); + } + static void free_req( eio_req* req ) { free(req); } + #endif +}; + +#ifdef FILESYSTEM_IMPL +volatile int32_t AsyncFileEIO::want_poll = 0; +#endif + +#endif +#endif diff --git a/fdbrpc/AsyncFileKAIO.actor.h b/fdbrpc/AsyncFileKAIO.actor.h new file mode 100644 index 0000000000..6e96cf942a --- /dev/null +++ b/fdbrpc/AsyncFileKAIO.actor.h @@ -0,0 +1,616 @@ +/* + * AsyncFileKAIO.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once +#ifdef __linux__ + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FLOW_ASYNCFILEKAIO_ACTOR_G_H) + #define FLOW_ASYNCFILEKAIO_ACTOR_G_H + #include "AsyncFileKAIO.actor.g.h" +#elif !defined(FLOW_ASYNCFILEKAIO_ACTOR_H) + #define FLOW_ASYNCFILEKAIO_ACTOR_H + +#include "IAsyncFile.h" +#include +#include +#include +#include +#include "fdbrpc/linux_kaio.h" +#include "flow/Knobs.h" +#include "flow/UnitTest.h" + +DESCR struct SlowAioSubmit { + int64_t submitDuration; // ns + int64_t truncateDuration; // ns + int64_t numTruncates; + int64_t truncateBytes; + int64_t largestTruncate; +}; + +class AsyncFileKAIO : public IAsyncFile, public ReferenceCounted { +public: + static Future> open( std::string filename, int flags, int mode, void* ignore ) { + ASSERT( flags & OPEN_UNBUFFERED ); + + if (flags & OPEN_LOCK) + mode |= 02000; // Enable mandatory locking for this file if it is supported by the filesystem + + std::string open_filename = filename; + if (flags & OPEN_ATOMIC_WRITE_AND_CREATE) { + ASSERT( (flags & OPEN_CREATE) && (flags & OPEN_READWRITE) && !(flags & OPEN_EXCLUSIVE) ); + open_filename = filename + ".part"; + } + + int fd = ::open( open_filename.c_str(), openFlags(flags) | O_DIRECT, mode ); + if (fd<0) { + Error e = errno==ENOENT ? file_not_found() : io_error(); + int ecode = errno; // Save errno in case it is modified before it is used below + TraceEvent ev("AsyncFileKAIOOpenFailed"); + ev.detail("Filename", filename).detailf("Flags", "%x", flags) + .detailf("OSFlags", "%x", openFlags(flags) | O_DIRECT).detailf("mode", "0%o", mode).error(e).GetLastError(); + if(ecode == EINVAL) + ev.detail("Description", "Invalid argument - Does the target filesystem support KAIO?"); + return e; + } else { + TraceEvent("AsyncFileKAIOOpen") + .detail("Filename", filename) + .detail("Flags", flags) + .detail("mode", mode) + .detail("fd", fd); + } + + Reference r(new AsyncFileKAIO( fd, flags, filename )); + + if (flags & OPEN_LOCK) { + // Acquire a "write" lock for the entire file + flock lockDesc; + lockDesc.l_type = F_WRLCK; + lockDesc.l_whence = SEEK_SET; + lockDesc.l_start = 0; + lockDesc.l_len = 0; // "Specifying 0 for l_len has the special meaning: lock all bytes starting at the location specified by l_whence and l_start through to the end of file, no matter how large the file grows." + lockDesc.l_pid = 0; + if (fcntl(fd, F_SETLK, &lockDesc) == -1) { + TraceEvent(SevError, "UnableToLockFile").detail("filename", filename).GetLastError(); + return io_error(); + } + } + + struct stat buf; + if (fstat( fd, &buf )) { + TraceEvent("AsyncFileKAIOFStatError").detail("fd",fd).detail("filename", filename).GetLastError(); + return io_error(); + } + + r->lastFileSize = r->nextFileSize = buf.st_size; + return Reference(std::move(r)); + } + + static void init( Reference ev, double ioTimeout ) { + if( !g_network->isSimulated() ) { + ctx.countAIOSubmit.init(LiteralStringRef("AsyncFile.CountAIOSubmit")); + ctx.countAIOCollect.init(LiteralStringRef("AsyncFile.CountAIOCollect")); + ctx.submitMetric.init(LiteralStringRef("AsyncFile.Submit")); + ctx.countPreSubmitTruncate.init(LiteralStringRef("AsyncFile.CountPreAIOSubmitTruncate")); + ctx.preSubmitTruncateBytes.init(LiteralStringRef("AsyncFile.PreAIOSubmitTruncateBytes")); + ctx.slowAioSubmitMetric.init(LiteralStringRef("AsyncFile.SlowAIOSubmit")); + } + + int rc = io_setup( FLOW_KNOBS->MAX_OUTSTANDING, &ctx.iocx ); + if (rc<0) { + TraceEvent("IOSetupError").GetLastError(); + throw io_error(); + } + setTimeout(ioTimeout); + ctx.evfd = ev->getFD(); + poll(ev); + + g_network->setGlobal(INetwork::enRunCycleFunc, (flowGlobalType) &AsyncFileKAIO::launch); + } + + static int get_eventfd() { return ctx.evfd; } + static void setTimeout(double ioTimeout) { ctx.setIOTimeout(ioTimeout); } + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + virtual Future read( void* data, int length, int64_t offset ) { + ++countFileLogicalReads; + ++countLogicalReads; + //printf("%p Begin logical read\n", getCurrentCoro()); + + if(failed) { + return io_timeout(); + } + + IOBlock *io = new IOBlock(IO_CMD_PREAD, fd); + io->buf = data; + io->nbytes = length; + io->offset = offset; + + enqueue(io, "read", this); + return io->result.getFuture(); + } + virtual Future write( void const* data, int length, int64_t offset ) { + ++countFileLogicalWrites; + ++countLogicalWrites; + //printf("%p Begin logical write\n", getCurrentCoro()); + + if(failed) { + return io_timeout(); + } + + IOBlock *io = new IOBlock(IO_CMD_PWRITE, fd); + io->buf = (void*)data; + io->nbytes = length; + io->offset = offset; + + nextFileSize = std::max( nextFileSize, offset+length ); + + enqueue(io, "write", this); + return success(io->result.getFuture()); + } + virtual Future truncate( int64_t size ) { + ++countFileLogicalWrites; + ++countLogicalWrites; + + if(failed) { + return io_timeout(); + } + + bool completed = false; + if( ctx.fallocateSupported && size >= lastFileSize ) { + if (fallocate( fd, 0, 0, size)) { + int fallocateErrCode = errno; + TraceEvent("AsyncFileKAIOAllocateError").detail("fd",fd).detail("filename", filename).GetLastError(); + if ( fallocateErrCode == EOPNOTSUPP ) { + // Mark fallocate as unsupported. Try again with truncate. + ctx.fallocateSupported = false; + } else { + return io_error(); + } + } else { + completed = true; + } + } + if ( !completed && ftruncate(fd, size) ) { + TraceEvent("AsyncFileKAIOTruncateError").detail("fd",fd).detail("filename", filename).GetLastError(); + return io_error(); + } + + lastFileSize = nextFileSize = size; + return Void(); + } + + ACTOR static Future throwErrorIfFailed( Reference self, Future sync ) { + Void _ = wait( sync ); + if(self->failed) { + throw io_timeout(); + } + return Void(); + } + + virtual Future sync() { + ++countFileLogicalWrites; + ++countLogicalWrites; + + if(failed) { + return io_timeout(); + } + + Future fsync = throwErrorIfFailed(Reference::addRef(this), AsyncFileEIO::async_fdatasync(fd)); // Don't close the file until the asynchronous thing is done + // Alas, AIO f(data)sync doesn't seem to actually be implemented by the kernel + /*IOBlock *io = new IOBlock(IO_CMD_FDSYNC, fd); + submit(io, "write"); + fsync=success(io->result.getFuture());*/ + + if (flags & OPEN_ATOMIC_WRITE_AND_CREATE) { + flags &= ~OPEN_ATOMIC_WRITE_AND_CREATE; + + return AsyncFileEIO::waitAndAtomicRename( fsync, filename+".part", filename ); + } + + return fsync; + } + virtual Future size() { return nextFileSize; } + virtual int64_t debugFD() { + return fd; + } + virtual std::string getFilename() { + return filename; + } + ~AsyncFileKAIO() { close(fd); } + + static void launch() { + if (ctx.queue.size() && ctx.outstanding < FLOW_KNOBS->MAX_OUTSTANDING - FLOW_KNOBS->MIN_SUBMIT) { + ctx.submitMetric = true; + + double begin = timer_monotonic(); + if (!ctx.outstanding) ctx.ioStallBegin = begin; + + IOBlock* toStart[FLOW_KNOBS->MAX_OUTSTANDING]; + int n = std::min(FLOW_KNOBS->MAX_OUTSTANDING - ctx.outstanding, ctx.queue.size()); + + int64_t previousTruncateCount = ctx.countPreSubmitTruncate; + int64_t previousTruncateBytes = ctx.preSubmitTruncateBytes; + int64_t largestTruncate = 0; + + for(int i=0; istartTime = now(); + + if(ctx.ioTimeout > 0) { + ctx.appendToRequestList(io); + } + + if (io->owner->lastFileSize != io->owner->nextFileSize) { + ++ctx.countPreSubmitTruncate; + int64_t truncateSize = io->owner->nextFileSize - io->owner->lastFileSize; + ASSERT(truncateSize > 0); + ctx.preSubmitTruncateBytes += truncateSize; + largestTruncate = std::max(largestTruncate, truncateSize); + io->owner->truncate(io->owner->nextFileSize); + } + } + double truncateComplete = timer_monotonic(); + int rc = io_submit( ctx.iocx, n, (linux_iocb**)toStart ); + double end = timer_monotonic(); + + if(end-begin > FLOW_KNOBS->SLOW_LOOP_CUTOFF) { + ctx.slowAioSubmitMetric->submitDuration = end-truncateComplete; + ctx.slowAioSubmitMetric->truncateDuration = truncateComplete-begin; + ctx.slowAioSubmitMetric->numTruncates = ctx.countPreSubmitTruncate - previousTruncateCount; + ctx.slowAioSubmitMetric->truncateBytes = ctx.preSubmitTruncateBytes - previousTruncateBytes; + ctx.slowAioSubmitMetric->largestTruncate = largestTruncate; + ctx.slowAioSubmitMetric->log(); + + if(g_nondeterministic_random->random01() < end-begin) { + TraceEvent("SlowKAIOLaunch") + .detail("IOSubmitTime", end-truncateComplete) + .detail("TruncateTime", truncateComplete-begin) + .detail("TruncateCount", ctx.countPreSubmitTruncate - previousTruncateCount) + .detail("TruncateBytes", ctx.preSubmitTruncateBytes - previousTruncateBytes) + .detail("LargestTruncate", largestTruncate); + } + } + + ctx.submitMetric = false; + ++ctx.countAIOSubmit; + + double elapsed = timer_monotonic() - begin; + g_network->networkMetrics.secSquaredSubmit += elapsed*elapsed/2; + + //TraceEvent("Launched").detail("n", rc).detail("queued", ctx.queue.size()).detail("ms", elapsed*1e3).detail("oustanding", ctx.outstanding+rc); + //printf("launched: %d/%d in %f us (%d outstanding; lowest prio %d)\n", rc, ctx.queue.size(), elapsed*1e6, ctx.outstanding + rc, toStart[n-1]->getTask()); + if (rc<0) { + if (errno == EAGAIN) { + rc = 0; + } else { + // Other errors are assumed to represent failure to issue the first I/O in the list + toStart[0]->setResult( errno ? -errno : -1000000 ); + rc = 1; + } + } else + ctx.outstanding += rc; + // Any unsubmitted I/Os need to be requeued + for(int i=rc; i { + Promise result; + Reference owner; + int64_t prio; + IOBlock *prev; + IOBlock *next; + double startTime; + + struct indirect_order_by_priority { bool operator () ( IOBlock* a, IOBlock* b ) { return a->prio < b->prio; } }; + + IOBlock(int op, int fd) : prev(nullptr), next(nullptr), startTime(0) { + memset((linux_iocb*)this, 0, sizeof(linux_iocb)); + aio_lio_opcode = op; + aio_fildes = fd; + } + + int getTask() const { return (prio>>32)+1; } + + ACTOR static void deliver( Promise result, bool failed, int r, int task ) { + Void _ = wait( delay(0, task) ); + if (failed) result.sendError(io_timeout()); + else if (r < 0) result.sendError(io_error()); + else result.send(r); + } + + void setResult( int r ) { + if (r<0) { + struct stat fst; + fstat( aio_fildes, &fst ); + + errno = -r; + TraceEvent("AsyncFileKAIOIOError").GetLastError().detail("fd", aio_fildes).detail("op", aio_lio_opcode).detail("nbytes", nbytes).detail("offset", offset).detail("ptr", int64_t(buf)) + .detail("Size", fst.st_size).detail("filename", owner->filename); + } + deliver( result, owner->failed, r, getTask() ); + delete this; + } + + void timeout(bool warnOnly) { + TraceEvent(SevWarnAlways, "AsyncFileKAIOTimeout").detail("fd", aio_fildes).detail("op", aio_lio_opcode).detail("nbytes", nbytes).detail("offset", offset).detail("ptr", int64_t(buf)) + .detail("filename", owner->filename); + + if(!warnOnly) + owner->failed = true; + } + }; + struct Context { + io_context_t iocx; + int evfd; + int outstanding; + double ioStallBegin; + bool fallocateSupported; + std::priority_queue, IOBlock::indirect_order_by_priority> queue; + Int64MetricHandle countAIOSubmit; + Int64MetricHandle countAIOCollect; + Int64MetricHandle submitMetric; + + double ioTimeout; + bool timeoutWarnOnly; + IOBlock *submittedRequestList; + + Int64MetricHandle countPreSubmitTruncate; + Int64MetricHandle preSubmitTruncateBytes; + + EventMetricHandle slowAioSubmitMetric; + + uint32_t opsIssued; + Context() : iocx(0), evfd(-1), outstanding(0), opsIssued(0), ioStallBegin(0), fallocateSupported(true), submittedRequestList(nullptr) { + setIOTimeout(0); + } + + void setIOTimeout(double timeout) { + ioTimeout = timeout; + timeoutWarnOnly = timeout < 0; + } + + void appendToRequestList(IOBlock *io) { + ASSERT(!io->next && !io->prev); + + if(submittedRequestList) { + io->prev = submittedRequestList->prev; + io->prev->next = io; + + submittedRequestList->prev = io; + io->next = submittedRequestList; + } + else { + submittedRequestList = io; + io->next = io->prev = io; + } + } + + void removeFromRequestList(IOBlock *io) { + if(io->next == nullptr) { + ASSERT(io->prev == nullptr); + return; + } + + ASSERT(io->prev != nullptr); + + if(io == io->next) { + ASSERT(io == submittedRequestList && io == io->prev); + submittedRequestList = nullptr; + } + else { + io->next->prev = io->prev; + io->prev->next = io->next; + + if(submittedRequestList == io) { + submittedRequestList = io->next; + } + } + + io->next = io->prev = nullptr; + } + }; + static Context ctx; + + explicit AsyncFileKAIO(int fd, int flags, std::string const& filename) : fd(fd), flags(flags), filename(filename), failed(false) { + if( !g_network->isSimulated() ) { + countFileLogicalWrites.init(LiteralStringRef("AsyncFile.CountFileLogicalWrites"), filename); + countFileLogicalReads.init( LiteralStringRef("AsyncFile.CountFileLogicalReads"), filename); + countLogicalWrites.init(LiteralStringRef("AsyncFile.CountLogicalWrites")); + countLogicalReads.init( LiteralStringRef("AsyncFile.CountLogicalReads")); + } + } + + void enqueue( IOBlock* io, const char* op, AsyncFileKAIO* owner ) { + ASSERT( int64_t(io->buf) % 4096 == 0 && io->offset % 4096 == 0 && io->nbytes % 4096 == 0 ); + io->flags |= 1; + io->eventfd = ctx.evfd; + io->prio = (int64_t(g_network->getCurrentTask())<<32) - (++ctx.opsIssued); + //io->prio = - (++ctx.opsIssued); + io->owner = Reference::addRef(owner); + + ctx.queue.push(io); + } + + static int openFlags(int flags) { + int oflags = 0; + ASSERT( bool(flags & OPEN_READONLY) != bool(flags & OPEN_READWRITE) ); // readonly xor readwrite + if( flags & OPEN_EXCLUSIVE ) oflags |= O_EXCL; + if( flags & OPEN_CREATE ) oflags |= O_CREAT; + if( flags & OPEN_READONLY ) oflags |= O_RDONLY; + if( flags & OPEN_READWRITE ) oflags |= O_RDWR; + if( flags & OPEN_ATOMIC_WRITE_AND_CREATE ) oflags |= O_TRUNC; + return oflags; + } + + ACTOR static void poll( Reference ev ) { + loop { + int64_t evfd_count = wait( ev->read() ); + + Void _ = wait(delay(0, TaskDiskIOComplete)); + + linux_ioresult ev[FLOW_KNOBS->MAX_OUTSTANDING]; + timespec tm; tm.tv_sec = 0; tm.tv_nsec = 0; + + int n; + + loop { + n = io_getevents( ctx.iocx, 0, FLOW_KNOBS->MAX_OUTSTANDING, ev, &tm ); + if (n>=0 || errno!=EINTR) break; + } + + ++ctx.countAIOCollect; + // printf("io_getevents: collected %d/%d in %f us (%d queued)\n", n, ctx.outstanding, (timer()-before)*1e6, ctx.queue.size()); + if (n<0) { + // printf("io_getevents failed: %d\n", errno); + TraceEvent("IOGetEventsError").GetLastError(); + throw io_error(); + } + if (n) { + double t = timer_monotonic(); + double elapsed = t - ctx.ioStallBegin; + ctx.ioStallBegin = t; + g_network->networkMetrics.secSquaredDiskStall += elapsed*elapsed/2; + } + + ctx.outstanding -= n; + + if(ctx.ioTimeout > 0) { + double currentTime = now(); + while(ctx.submittedRequestList && currentTime - ctx.submittedRequestList->startTime > ctx.ioTimeout) { + ctx.submittedRequestList->timeout(ctx.timeoutWarnOnly); + ctx.removeFromRequestList(ctx.submittedRequestList); + } + } + + for(int i=0; i(ev[i].iocb); + + if(ctx.ioTimeout > 0) { + ctx.removeFromRequestList(iob); + } + + iob->setResult( ev[i].result ); + } + } + } +}; + +ACTOR Future runTestOps(Reference f, int numIterations, int fileSize, bool expectedToSucceed) { + state void *buf = FastAllocator<4096>::allocate(); // we leak this if there is an error, but that shouldn't be a big deal + state int iteration = 0; + + state bool opTimedOut = false; + + for(; iteration < numIterations; ++iteration) { + state std::vector> futures; + state int numOps = g_random->randomInt(1, 20); + for(; numOps > 0; --numOps) { + if(g_random->coinflip()) { + futures.push_back(success(f->read(buf, 4096, g_random->randomInt(0, fileSize)/4096*4096))); + } + else { + futures.push_back(f->write(buf, 4096, g_random->randomInt(0, fileSize)/4096*4096)); + } + } + state int fIndex = 0; + for(; fIndex < futures.size(); ++fIndex) { + try { + Void _ = wait(futures[fIndex]); + } + catch(Error &e) { + ASSERT(!expectedToSucceed); + ASSERT(e.code() == error_code_io_timeout); + opTimedOut = true; + } + } + + try { + Void _ = wait(f->sync() && delay(0.1)); + ASSERT(expectedToSucceed); + } + catch(Error &e) { + ASSERT(!expectedToSucceed && e.code() == error_code_io_timeout); + } + } + + FastAllocator<4096>::release(buf); + + ASSERT(expectedToSucceed || opTimedOut); + return Void(); +} + +TEST_CASE("fdbrpc/AsyncFileKAIO/RequestList") { + if(!g_network->isSimulated()) { // This test does nothing in simulation because simulation doesn't support AsyncFileKAIO + try { + state Reference f = wait(AsyncFileKAIO::open("/tmp/__KAIO_TEST_FILE__", IAsyncFile::OPEN_UNBUFFERED | IAsyncFile::OPEN_READWRITE | IAsyncFile::OPEN_CREATE, 0666, nullptr)); + state int fileSize = 2<<27; // ~100MB + Void _ = wait(f->truncate(fileSize)); + + // Test that the request list works as intended with default timeout + AsyncFileKAIO::setTimeout(0.0); + Void _ = wait(runTestOps(f, 100, fileSize, true)); + ASSERT(!((AsyncFileKAIO*)f.getPtr())->failed); + + // Test that the request list works as intended with long timeout + AsyncFileKAIO::setTimeout(20.0); + Void _ = wait(runTestOps(f, 100, fileSize, true)); + ASSERT(!((AsyncFileKAIO*)f.getPtr())->failed); + + // Test that requests timeout correctly + AsyncFileKAIO::setTimeout(0.0001); + Void _ = wait(runTestOps(f, 10, fileSize, false)); + ASSERT(((AsyncFileKAIO*)f.getPtr())->failed); + } + catch(Error &e) { + state Error err = e; + if(f) { + Void _ = wait(AsyncFileEIO::deleteFile(f->getFilename(), true)); + } + throw err; + } + + Void _ = wait(AsyncFileEIO::deleteFile(f->getFilename(), true)); + } + + return Void(); +} + +AsyncFileKAIO::Context AsyncFileKAIO::ctx; + +#endif +#endif diff --git a/fdbrpc/AsyncFileNonDurable.actor.cpp b/fdbrpc/AsyncFileNonDurable.actor.cpp new file mode 100644 index 0000000000..e81c6f58f4 --- /dev/null +++ b/fdbrpc/AsyncFileNonDurable.actor.cpp @@ -0,0 +1,35 @@ +/* + * AsyncFileNonDurable.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "AsyncFileNonDurable.actor.h" + +std::map> AsyncFileNonDurable::filesBeingDeleted; + +ACTOR Future sendOnProcess( ISimulator::ProcessInfo* process, Promise promise, int taskID ) { + Void _ = wait( g_simulator.onProcess( process, taskID ) ); + promise.send(Void()); + return Void(); +} + +ACTOR Future sendErrorOnProcess( ISimulator::ProcessInfo* process, Promise promise, Error e, int taskID ) { + Void _ = wait( g_simulator.onProcess( process, taskID ) ); + promise.sendError(e); + return Void(); +} \ No newline at end of file diff --git a/fdbrpc/AsyncFileNonDurable.actor.h b/fdbrpc/AsyncFileNonDurable.actor.h new file mode 100644 index 0000000000..951147b162 --- /dev/null +++ b/fdbrpc/AsyncFileNonDurable.actor.h @@ -0,0 +1,746 @@ +/* + * AsyncFileNonDurable.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FLOW_ASYNCFILENONDURABLE_ACTOR_G_H) + #define FLOW_ASYNCFILENONDURABLE_ACTOR_G_H + #include "AsyncFileNonDurable.actor.g.h" +#elif !defined(FLOW_ASYNCFILENONDURABLE_ACTOR_H) + #define FLOW_ASYNCFILENONDURABLE_ACTOR_H + +#include "flow/flow.h" +#include "IAsyncFile.h" +#include "flow/ActorCollection.h" +#include "simulator.h" +#include "TraceFileIO.h" +#include "RangeMap.h" + +#undef max +#undef min + +Future sendOnProcess( ISimulator::ProcessInfo* const& process, Promise const& promise, int const& taskID ); +Future sendErrorOnProcess( ISimulator::ProcessInfo* const& process, Promise const& promise, Error const& e, int const& taskID ); + +ACTOR template +Future sendErrorOnShutdown( Future in ) { + choose { + when( ISimulator::KillType _ = wait( g_simulator.getCurrentProcess()->shutdownSignal.getFuture() ) ) { + throw io_error().asInjectedFault(); + } + when( T rep = wait( in ) ) { + return rep; + } + } +} + +class AsyncFileDetachable sealed : public IAsyncFile, public ReferenceCounted{ +private: + Reference file; + Future shutdown; + +public: + explicit AsyncFileDetachable( Reference file ) : file(file) { + shutdown = doShutdown(this); + } + + ACTOR Future doShutdown( AsyncFileDetachable* self ) { + ISimulator::KillType _ = wait( g_simulator.getCurrentProcess()->shutdownSignal.getFuture() ); + self->file = Reference(); + return Void(); + } + + ACTOR static Future> open( Future> wrappedFile ) { + choose { + when( ISimulator::KillType _ = wait( g_simulator.getCurrentProcess()->shutdownSignal.getFuture() ) ) { + throw io_error().asInjectedFault(); + } + when( Reference f = wait( wrappedFile ) ) { + return Reference( new AsyncFileDetachable(f) ); + } + } + } + + virtual void addref() { + ReferenceCounted::addref(); + } + virtual void delref() { + ReferenceCounted::delref(); + } + + Future read(void *data, int length, int64_t offset) { + if( !file.getPtr() || g_simulator.getCurrentProcess()->shutdownSignal.getFuture().isReady() ) + return io_error().asInjectedFault(); + return sendErrorOnShutdown( file->read( data, length, offset ) ); + } + + Future write(void const *data, int length, int64_t offset) { + if( !file.getPtr() || g_simulator.getCurrentProcess()->shutdownSignal.getFuture().isReady() ) + return io_error().asInjectedFault(); + return sendErrorOnShutdown( file->write( data, length, offset ) ); + } + + Future truncate(int64_t size) { + if( !file.getPtr() || g_simulator.getCurrentProcess()->shutdownSignal.getFuture().isReady() ) + return io_error().asInjectedFault(); + return sendErrorOnShutdown( file->truncate( size ) ); + } + + Future sync() { + if( !file.getPtr() || g_simulator.getCurrentProcess()->shutdownSignal.getFuture().isReady() ) + return io_error().asInjectedFault(); + return sendErrorOnShutdown( file->sync() ); + } + + Future size() { + if( !file.getPtr() || g_simulator.getCurrentProcess()->shutdownSignal.getFuture().isReady() ) + return io_error().asInjectedFault(); + return sendErrorOnShutdown( file->size() ); + } + + int64_t debugFD() { + if( !file.getPtr() ) + throw io_error().asInjectedFault(); + return file->debugFD(); + } + std::string getFilename() { + if( !file.getPtr() ) + throw io_error().asInjectedFault(); + return file->getFilename(); + } +}; + +//An async file implementation which wraps another async file and will randomly destroy sectors that it is writing when killed +//This is used to simulate a power failure which prevents all written data from being persisted to disk +class AsyncFileNonDurable sealed : public IAsyncFile, public ReferenceCounted{ +public: + UID id; + std::string filename; + + //An approximation of the size of the file; .size() should be used instead of this variable in most cases + int64_t approximateSize; + + //The address of the machine that opened the file + NetworkAddress openedAddress; + +private: + //The wrapped IAsyncFile + Reference file; + + //The maximum amount of time a write is delayed before being passed along to the underlying file + double maxWriteDelay; + + //Modifications which haven't been pushed to file, mapped by the location in the file that is being modified + RangeMap< uint64_t, Future > pendingModifications; + + //Will be blocked whenever kill is running + Promise killed; + Promise killComplete; + + //Used by sync (and kill) to force writes which have not yet been passed along. + //If true is sent, then writes will be durable. If false, then they may not be durable. + Promise startSyncPromise; + + //The performance parameters of the simulated disk + Reference diskParameters; + + //Set to true the first time sync is called on the file + bool hasBeenSynced; + + //Used to describe what corruption is allowed by the file as well as the type of corruption being used on a particular page + enum KillMode { NO_CORRUPTION = 0, DROP_ONLY = 1, FULL_CORRUPTION = 2 }; + + //Limits what types of corruption are applied to writes from this file + KillMode killMode; + + ActorCollection reponses; //cannot call getResult on this actor collection, since the actors will be on different processes + + AsyncFileNonDurable(const std::string& filename, Reference file, Reference diskParameters, NetworkAddress openedAddress) + : openedAddress(openedAddress), pendingModifications(uint64_t(-1)), approximateSize(0), reponses(false) { + + //This is only designed to work in simulation + ASSERT(g_network->isSimulated()); + this->id = g_random->randomUniqueID(); + + //TraceEvent("AsyncFileNonDurable_Create", id).detail("Filename", filename); + this->file = file; + this->filename = filename; + this->diskParameters = diskParameters; + maxWriteDelay = 5.0; + hasBeenSynced = false; + + killMode = (KillMode)g_random->randomInt(1, 3); + //TraceEvent("AsyncFileNonDurable_CreateEnd", id).detail("Filename", filename).backtrace(); + } + +public: + static std::map> filesBeingDeleted; + + //Creates a new AsyncFileNonDurable which wraps the provided IAsyncFile + ACTOR static Future> open(std::string filename, std::string actualFilename, Future> wrappedFile, Reference diskParameters) { + state ISimulator::ProcessInfo* currentProcess = g_simulator.getCurrentProcess(); + state int currentTaskID = g_network->getCurrentTask(); + state Future shutdown = success(currentProcess->shutdownSignal.getFuture()); + + //TraceEvent("AsyncFileNonDurableOpenBegin").detail("Filename", filename).detail("addr", g_simulator.getCurrentProcess()->address); + Void _ = wait( g_simulator.onMachine( currentProcess ) ); + try { + Void _ = wait(success(wrappedFile) || shutdown); + + if(shutdown.isReady()) + throw io_error().asInjectedFault(); + + state Reference file = wrappedFile.get(); + + //If we are in the process of deleting a file, we can't let someone else modify it at the same time. We therefore block the creation of new files until deletion is complete + state std::map>::iterator deletedFile = filesBeingDeleted.find(filename); + if(deletedFile != filesBeingDeleted.end()) { + //TraceEvent("AsyncFileNonDurableOpenWaitOnDelete").detail("Filename", filename); + Void _ = wait( deletedFile->second || shutdown ); + if(shutdown.isReady()) + throw io_error().asInjectedFault(); + } + + state Reference nonDurableFile( new AsyncFileNonDurable(filename, file, diskParameters, currentProcess->address) ); + + //Causes the approximateSize member to be set + state Future sizeFuture = nonDurableFile->size(); + Void _ = wait(success(sizeFuture) || shutdown); + + if(shutdown.isReady()) + throw io_error().asInjectedFault(); + + //TraceEvent("AsyncFileNonDurableOpenComplete").detail("Filename", filename); + + Void _ = wait( g_simulator.onProcess( currentProcess, currentTaskID ) ); + + return nonDurableFile; + } catch( Error &e ) { + state Error err = e; + std::string currentFilename = ( wrappedFile.isReady() && !wrappedFile.isError() ) ? wrappedFile.get()->getFilename() : actualFilename; + currentProcess->machine->openFiles.erase( currentFilename ); + //TraceEvent("AsyncFileNonDurableOpenError").detail("Filename", filename).detail("address", currentProcess->address).error(e, true).detail("addr", g_simulator.getCurrentProcess()->address); + Void _ = wait( g_simulator.onProcess( currentProcess, currentTaskID ) ); + throw err; + } + } + + ~AsyncFileNonDurable() { + //TraceEvent("AsyncFileNonDurable_Destroy", id).detail("Filename", filename); + } + + virtual void addref() { + ReferenceCounted::addref(); + } + virtual void delref() { + if(delref_no_destroy()) { + ASSERT(filesBeingDeleted.count(filename) == 0); + //TraceEvent("AsyncFileNonDurable_StartDelete", id).detail("Filename", filename); + Future deleteFuture = deleteFile(this); + if(!deleteFuture.isReady()) + filesBeingDeleted[filename] = deleteFuture; + } + } + + //Passes along reads straight to the underlying file, waiting for any outstanding changes that could affect the results + Future read(void *data, int length, int64_t offset) { + return read(this, data, length, offset); + } + + //Writes data to the file. Writes are delayed a random amount of time before being + //passed to the underlying file + Future write(void const *data, int length, int64_t offset) { + //TraceEvent("AsyncFileNonDurable_Write", id).detail("Filename", filename).detail("Offset", offset).detail("Length", length); + if(length == 0) { + TraceEvent(SevWarnAlways, "AsyncFileNonDurable_EmptyModification", id).detail("Filename", filename); + return Void(); + } + + debugFileSet("AsyncFileNonDurableWrite", filename, data, offset, length); + + Promise writeStarted; + Promise> writeEnded; + writeEnded.send(write(this, writeStarted, writeEnded.getFuture(), data, length, offset)); + return writeStarted.getFuture(); + } + + //Truncates the file. Truncates are delayed a random amount of time before being + //passed to the underlying file + Future truncate(int64_t size) { + //TraceEvent("AsyncFileNonDurable_Truncate", id).detail("Filename", filename).detail("Offset", size); + debugFileTruncate("AsyncFileNonDurableTruncate", filename, size); + + Promise truncateStarted; + Promise> truncateEnded; + truncateEnded.send(truncate(this, truncateStarted, truncateEnded.getFuture(), size)); + return truncateStarted.getFuture(); + } + + //Fsyncs the file. This allows all delayed modifications to the file to complete before + //syncing the underlying file + Future sync() { + //TraceEvent("AsyncFileNonDurable_Sync", id).detail("Filename", filename); + Future syncFuture = sync(this, true); + reponses.add( syncFuture ); + return syncFuture; + } + + //Passes along size requests to the underlying file, augmenting with any writes past the end of the file + Future size() { + return size(this); + } + + int64_t debugFD() { + return file->debugFD(); + } + + std::string getFilename() { + return file->getFilename(); + } + + //Forces a non-durable sync (some writes are not made or made incorrectly) + //This is used when the file should 'die' without first completing its operations + //(e.g. to simulate power failure) + Future kill() { + TraceEvent("AsyncFileNonDurable_Kill", id).detail("Filename", filename); + TEST(true); //AsyncFileNonDurable was killed + return sync(this, false); + } + +private: + + //Returns a future that is used to ensure the waiter ends up on the main thread + Future returnToMainThread() { + Promise p; + Future f = p.getFuture(); + g_network->onMainThread(std::move(p), g_network->getCurrentTask()); + return f; + } + + //Gets existing modifications that overlap the specified range. Optionally inserts a new modification into the map + std::vector> getModificationsAndInsert(int64_t offset, int64_t length, bool insertModification = false, Future value = Void()) { + auto modification = RangeMapRange(offset, length>=0 ? offset+length : uint64_t(-1)); + auto priorModifications = pendingModifications.intersectingRanges(modification); + + //Aggregate existing modifications in this range + std::vector> modificationFutures; + for(auto itr = priorModifications.begin(); itr != priorModifications.end(); ++itr) { + if(itr.value().isValid() && (!itr.value().isReady() || itr.value().isError())) { + modificationFutures.push_back(itr.value()); + } + } + + //Add the modification if we are doing a write or truncate + if(insertModification) + pendingModifications.insert(modification, value); + + return modificationFutures; + } + + //Checks if the file is killed. If so, then the current sync is completed if running and then an error is thrown + ACTOR Future checkKilled(AsyncFileNonDurable *self, std::string context) { + if(self->killed.isSet()) { + //TraceEvent("AsyncFileNonDurable_KilledInCheck", self->id).detail("In", context).detail("Filename", self->filename); + Void _ = wait(self->killComplete.getFuture()); + TraceEvent("AsyncFileNonDurable_KilledFileOperation", self->id).detail("In", context).detail("Filename", self->filename); + TEST(true); // AsyncFileNonDurable operation killed + throw io_error().asInjectedFault(); + } + + return Void(); + } + + //Passes along reads straight to the underlying file, waiting for any outstanding changes that could affect the results + ACTOR Future onRead(AsyncFileNonDurable *self, void *data, int length, int64_t offset) { + Void _ = wait(self->checkKilled(self, "Read")); + vector> priorModifications = self->getModificationsAndInsert(offset, length); + Void _ = wait(waitForAll(priorModifications)); + state Future readFuture = self->file->read(data, length, offset); + Void _ = wait( success( readFuture ) || self->killed.getFuture() ); + + // throws if we were killed + Void _ = wait(self->checkKilled(self, "ReadEnd")); + + debugFileCheck("AsyncFileNonDurableRead", self->filename, data, offset, length); + + //if(g_simulator.getCurrentProcess()->rebooting) + //TraceEvent("AsyncFileNonDurable_ReadEnd", self->id).detail("Filename", self->filename); + + return readFuture.get(); + } + + ACTOR Future read(AsyncFileNonDurable *self, void *data, int length, int64_t offset) { + state ISimulator::ProcessInfo* currentProcess = g_simulator.getCurrentProcess(); + state int currentTaskID = g_network->getCurrentTask(); + Void _ = wait( g_simulator.onMachine( currentProcess ) ); + + try { + state int rep = wait( self->onRead( self, data, length, offset ) ); + Void _ = wait( g_simulator.onProcess( currentProcess, currentTaskID ) ); + + return rep; + } catch( Error &e ) { + state Error err = e; + Void _ = wait( g_simulator.onProcess( currentProcess, currentTaskID ) ); + throw err; + } + } + + //Delays writes a random amount of time before passing them through to the underlying file. + //If a kill interrupts the delay, then the output could be the correct write, part of the write, + //or none of the write. It may also corrupt parts of sectors which have not been written correctly + ACTOR Future write(AsyncFileNonDurable *self, Promise writeStarted, Future> ownFuture, void const* data, int length, int64_t offset) { + state ISimulator::ProcessInfo* currentProcess = g_simulator.getCurrentProcess(); + state int currentTaskID = g_network->getCurrentTask(); + Void _ = wait( g_simulator.onMachine( currentProcess ) ); + + state double delayDuration = g_random->random01() * self->maxWriteDelay; + state Standalone dataCopy(StringRef((uint8_t*)data, length)); + + state Future startSyncFuture = self->startSyncPromise.getFuture(); + + try { + //TraceEvent("AsyncFileNonDurable_Write", self->id).detail("Delay", delayDuration).detail("Filename", self->filename).detail("WriteLength", length).detail("Offset", offset); + Void _ = wait(self->checkKilled(self, "Write")); + + Future writeEnded = wait(ownFuture); + std::vector> priorModifications = self->getModificationsAndInsert(offset, length, true, writeEnded); + + if(BUGGIFY_WITH_PROB(0.001)) + priorModifications.push_back(delay(g_random->random01() * FLOW_KNOBS->MAX_PRIOR_MODIFICATION_DELAY)); + else + priorModifications.push_back(waitUntilDiskReady(self->diskParameters, length) || self->killed.getFuture()); + + Void _ = wait(waitForAll(priorModifications)); + + self->approximateSize = std::max(self->approximateSize, length + offset); + + self->reponses.add( sendOnProcess( currentProcess, writeStarted, currentTaskID ) ); + } + catch(Error &e) { + self->reponses.add( sendErrorOnProcess( currentProcess, writeStarted, e, currentTaskID ) ); + throw; + } + + //TraceEvent("AsyncFileNonDurable_WriteDoneWithPreviousMods", self->id).detail("Delay", delayDuration).detail("Filename", self->filename).detail("WriteLength", length).detail("Offset", offset); + + //Wait a random amount of time or until a sync/kill is issued + state bool saveDurable = true; + choose { + when(Void _ = wait(delay(delayDuration))) { } + when(bool durable = wait(startSyncFuture)) { + saveDurable = durable; + } + } + + debugFileCheck("AsyncFileNonDurableWriteAfterWait", self->filename, dataCopy.begin(), offset, length); + + //Only page-aligned writes are supported + ASSERT(offset % 4096 == 0 && length % 4096 == 0); + + //Non-durable writes should introduce errors at the page level and corrupt at the sector level + //Otherwise, we can perform the entire write at once + int pageLength = saveDurable ? length : 4096; + int sectorLength = saveDurable ? length : 512; + + vector> writeFutures; + for(int writeOffset = 0; writeOffset < length; writeOffset += pageLength) { + //choose a random action to perform on this page write (write correctly, corrupt, or don't write) + KillMode pageKillMode = (KillMode)g_random->randomInt(0, self->killMode + 1); + + for(int pageOffset = 0; pageOffset < pageLength; pageOffset += sectorLength) { + //If saving durable, then perform the write correctly. Otherwise, perform the write correcly with a probability of 1/3. + //If corrupting the write, then this sector will be written correctly with a 1/4 chance + if(saveDurable || pageKillMode == NO_CORRUPTION || (pageKillMode == FULL_CORRUPTION && g_random->random01() < 0.25)) { + //if (!saveDurable) TraceEvent(SevInfo, "AsyncFileNonDurableWrite", self->id).detail("Filename", self->filename).detail("Offset", offset+writeOffset+pageOffset).detail("Length", sectorLength); + writeFutures.push_back(self->file->write(dataCopy.begin() + writeOffset + pageOffset, sectorLength, offset + writeOffset + pageOffset)); + } + + //If the write is not durable, then the write will either be corrupted or not written at all. If corrupted, there is 1/4 chance that a given + //sector will not be written + else if(pageKillMode == FULL_CORRUPTION && g_random->random01() < 0.66667) { + //The incorrect part of the write can be the rightmost bytes (side = 0), the leftmost bytes (side = 1), or the entire write (side = 2) + int side = g_random->randomInt(0, 3); + + //There is a 1/2 chance that a bad write will have garbage written into its bad portion + //The chance is increased to 1 if the entire write is bad + bool garbage = side == 2 || g_random->random01() < 0.5; + + int64_t goodStart = 0; + int64_t goodEnd = sectorLength; + int64_t badStart = 0; + int64_t badEnd = sectorLength; + + if(side == 0) { + goodEnd = g_random->randomInt(0, sectorLength); + badStart = goodEnd; + } + else if(side == 1) { + badEnd = g_random->randomInt(0, sectorLength); + goodStart = badEnd; + } + else + goodEnd = 0; + + //Write randomly generated bytes, if required + if(garbage && badStart != badEnd) { + uint8_t *badData = const_cast(&dataCopy.begin()[badStart + writeOffset + pageOffset]); + for(int i = 0; i < badEnd - badStart; i += sizeof(uint32_t)) { + uint32_t val = g_random->randomUInt32(); + memcpy(&badData[i], &val, std::min(badEnd - badStart - i, (int64_t)sizeof(uint32_t))); + } + + writeFutures.push_back(self->file->write(dataCopy.begin() + writeOffset + pageOffset, sectorLength, offset + writeOffset + pageOffset)); + debugFileSet("AsyncFileNonDurableBadWrite", self->filename, dataCopy.begin() + writeOffset + pageOffset, offset + writeOffset + pageOffset, sectorLength); + } + else if(goodStart != goodEnd) + writeFutures.push_back(self->file->write(dataCopy.begin() + goodStart + writeOffset + pageOffset, goodEnd - goodStart, goodStart + offset + writeOffset + pageOffset)); + + TraceEvent("AsyncFileNonDurable_BadWrite", self->id).detail("Offset", offset + writeOffset + pageOffset).detail("Length", sectorLength).detail("GoodStart", goodStart).detail("GoodEnd", goodEnd).detail("HasGarbage", garbage).detail("Side", side).detail("Filename", self->filename); + TEST(true); //AsyncFileNonDurable bad write + } + else { + TraceEvent("AsyncFileNonDurable_DroppedWrite", self->id).detail("Offset", offset + writeOffset + pageOffset).detail("Length", sectorLength).detail("Filename", self->filename); + TEST(true); //AsyncFileNonDurable dropped write + } + } + } + + Void _ = wait(waitForAll(writeFutures)); + //TraceEvent("AsyncFileNonDurable_WriteDone", self->id).detail("Delay", delayDuration).detail("Filename", self->filename).detail("WriteLength", length).detail("Offset", offset); + return Void(); + } + + //Delays truncates a random amount of time before passing them through to the underlying file. + //If a kill interrupts the delay, then the truncate may or may not be performed + ACTOR Future truncate(AsyncFileNonDurable *self, Promise truncateStarted, Future> ownFuture, int64_t size) { + state ISimulator::ProcessInfo* currentProcess = g_simulator.getCurrentProcess(); + state int currentTaskID = g_network->getCurrentTask(); + Void _ = wait( g_simulator.onMachine( currentProcess ) ); + + state double delayDuration = g_random->random01() * self->maxWriteDelay; + state Future startSyncFuture = self->startSyncPromise.getFuture(); + + try { + //TraceEvent("AsyncFileNonDurable_Truncate", self->id).detail("Delay", delayDuration).detail("Filename", self->filename); + Void _ = wait(self->checkKilled(self, "Truncate")); + + Future truncateEnded = wait(ownFuture); + std::vector> priorModifications = self->getModificationsAndInsert(size, -1, true, truncateEnded); + + if(BUGGIFY_WITH_PROB(0.001)) + priorModifications.push_back(delay(g_random->random01() * FLOW_KNOBS->MAX_PRIOR_MODIFICATION_DELAY)); + else + priorModifications.push_back(waitUntilDiskReady(self->diskParameters, 0) || self->killed.getFuture()); + + Void _ = wait(waitForAll(priorModifications)); + + self->approximateSize = size; + + self->reponses.add( sendOnProcess( currentProcess, truncateStarted, currentTaskID ) ); + } + catch(Error &e) { + self->reponses.add( sendErrorOnProcess( currentProcess, truncateStarted, e, currentTaskID ) ); + throw; + } + + //Wait a random amount of time or until a sync/kill is issued + state bool saveDurable = true; + choose { + when(Void _ = wait(delay(delayDuration))) { } + when(bool durable = wait(startSyncFuture)) { + saveDurable = durable; + } + } + + if(g_network->check_yield(TaskDefaultYield)) { + Void _ = wait(delay(0, TaskDefaultYield)); + } + + //If performing a durable truncate, then pass it through to the file. Otherwise, pass it through with a 1/2 chance + if(saveDurable || self->killMode == NO_CORRUPTION || g_random->random01() < 0.5) + Void _ = wait(self->file->truncate(size)); + else { + TraceEvent("AsyncFileNonDurable_DroppedTruncate", self->id).detail("Size", size); + TEST(true); //AsyncFileNonDurable dropped truncate + } + + return Void(); + } + + //Waits for delayed modifications to the file to complete and then syncs the underlying file + //If durable is false, then some of the delayed modifications will not be applied or will be + //applied incorrectly + ACTOR Future onSync(AsyncFileNonDurable *self, bool durable) { + //TraceEvent("AsyncFileNonDurable_ImplSync", self->id).detail("Filename", self->filename).detail("Durable", durable); + ASSERT(durable || !self->killed.isSet()); // this file is kill()ed only once + + if(durable) { + self->hasBeenSynced = true; + Void _ = wait(waitUntilDiskReady(self->diskParameters, 0, true) || self->killed.getFuture()); + } + + Void _ = wait(self->checkKilled(self, durable ? "Sync" : "Kill")); + + if(!durable) + self->killed.send( Void() ); + + //Get all outstanding modifications + std::vector> outstandingModifications; + std::vector> stillPendingModifications; + + auto rangeItr = self->pendingModifications.ranges(); + for(auto itr = rangeItr.begin(); itr != rangeItr.end(); ++itr) { + if(itr.value().isValid() && (!itr->value().isReady() || itr->value().isError())) { + outstandingModifications.push_back(itr->value()); + + if(!itr.value().isReady()) + stillPendingModifications.push_back(itr->range()); + } + } + + Future allModifications = waitForAll(outstandingModifications); + //Clear out the pending modifications map of all completed modifications + self->pendingModifications.insert(RangeMapRange(0, -1), Void()); + for(auto itr = stillPendingModifications.begin(); itr != stillPendingModifications.end(); ++itr) + self->pendingModifications.insert(*itr, success(allModifications)); //waitForAll cannot wait on the same future more than once, so wrap the future with success + + //Signal all modifications to end their delay and reset the startSyncPromise + Promise startSyncPromise = self->startSyncPromise; + self->startSyncPromise = Promise(); + + //Writes will be durable in a kill with a 10% probability + state bool writeDurable = durable || g_random->random01() < 0.1; + startSyncPromise.send(writeDurable); + + //Wait for outstanding writes to complete + if(durable) + Void _ = wait(allModifications); + else + ErrorOr _ = wait(errorOr(allModifications)); + + if(!durable) { + //Sometimes sync the file if writes were made durably. Before a file is first synced, it is stored in a temporary file and then renamed to the correct + //location once sync is called. By not calling sync, we simulate a failure to fsync the directory storing the file + if(self->hasBeenSynced && writeDurable && g_random->random01() < 0.5) { + TEST(true); //AsyncFileNonDurable kill was durable and synced + ErrorOr _ = wait(errorOr(self->file->sync())); + } + + //Setting this promise could trigger the deletion of the AsyncFileNonDurable; after this none of its members should be used + //TraceEvent("AsyncFileNonDurable_ImplSyncEnd", self->id).detail("Filename", self->filename).detail("Durable", durable); + self->killComplete.send(Void()); + } + //A killed file cannot be allowed to report that it successfully synced + else { + Void _ = wait(self->checkKilled(self, "SyncEnd")); + Void _ = wait(self->file->sync()); + //TraceEvent("AsyncFileNonDurable_ImplSyncEnd", self->id).detail("Filename", self->filename).detail("Durable", durable); + } + + return Void(); + } + + ACTOR Future sync(AsyncFileNonDurable *self, bool durable) { + state ISimulator::ProcessInfo* currentProcess = g_simulator.getCurrentProcess(); + state int currentTaskID = g_network->getCurrentTask(); + Void _ = wait( g_simulator.onMachine( currentProcess ) ); + + try { + Void _ = wait( self->onSync( self, durable ) ); + Void _ = wait( g_simulator.onProcess( currentProcess, currentTaskID ) ); + + return Void(); + } catch( Error &e ) { + state Error err = e; + Void _ = wait( g_simulator.onProcess( currentProcess, currentTaskID ) ); + throw err; + } + } + + //Passes along size requests to the underlying file, augmenting with any writes past the end of the file + ACTOR Future onSize(AsyncFileNonDurable *self) { + //TraceEvent("AsyncFileNonDurable_Size", self->id).detail("Filename", self->filename); + Void _ = wait(self->checkKilled(self, "Size")); + state Future sizeFuture = self->file->size(); + Void _ = wait( success( sizeFuture ) || self->killed.getFuture() ); + + Void _ = wait(self->checkKilled(self, "SizeEnd")); + + //Include any modifications which extend past the end of the file + uint64_t maxModification = self->pendingModifications.lastItem().begin(); + self->approximateSize = std::max(sizeFuture.get(), maxModification); + return self->approximateSize; + } + + ACTOR Future size(AsyncFileNonDurable *self) { + state ISimulator::ProcessInfo* currentProcess = g_simulator.getCurrentProcess(); + state int currentTaskID = g_network->getCurrentTask(); + + Void _ = wait( g_simulator.onMachine( currentProcess ) ); + + try { + state int64_t rep = wait( self->onSize( self ) ); + Void _ = wait( g_simulator.onProcess( currentProcess, currentTaskID ) ); + + return rep; + } catch( Error &e ) { + state Error err = e; + Void _ = wait( g_simulator.onProcess( currentProcess, currentTaskID ) ); + throw err; + } + } + + //Finishes all outstanding actors on an AsyncFileNonDurable and then deletes it + ACTOR Future deleteFile(AsyncFileNonDurable *self) { + //We must run on the main thread (instead of a SQLite coroutine). We don't want to signal any promises from a coroutine, so we switch at the beginning + //of this ACTOR + Void _ = wait(self->returnToMainThread()); + + //Make sure all writes have gone through. + Promise startSyncPromise = self->startSyncPromise; + self->startSyncPromise = Promise(); + startSyncPromise.send(true); + + std::vector> outstandingModifications; + + for(auto itr = self->pendingModifications.ranges().begin(); itr != self->pendingModifications.ranges().end(); ++itr) + if(itr->value().isValid() && !itr->value().isReady()) + outstandingModifications.push_back(itr->value()); + + //Ignore errors here so that all modifications can finish + Void _ = wait(waitForAllReady(outstandingModifications)); + + //Make sure we aren't in the process of killing the file + if(self->killed.isSet()) + Void _ = wait(self->killComplete.getFuture()); + + //Remove this file from the filesBeingDeleted map so that new files can be created with this filename + g_simulator.getMachineByNetworkAddress( self->openedAddress )->closingFiles.erase(self->getFilename()); + AsyncFileNonDurable::filesBeingDeleted.erase(self->filename); + //TraceEvent("AsyncFileNonDurable_FinishDelete", self->id).detail("Filename", self->filename); + + delete self; + return Void(); + } +}; + +#endif diff --git a/fdbrpc/AsyncFileReadAhead.actor.h b/fdbrpc/AsyncFileReadAhead.actor.h new file mode 100644 index 0000000000..a575240d9f --- /dev/null +++ b/fdbrpc/AsyncFileReadAhead.actor.h @@ -0,0 +1,191 @@ +/* + * AsyncFileReadAhead.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FDBRPC_ASYNCFILEREADAHEAD_ACTOR_G_H) + #define FDBRPC_ASYNCFILEREADAHEAD_ACTOR_G_H + #include "AsyncFileReadAhead.actor.g.h" +#elif !defined(FDBRPC_ASYNCFILEREADAHEAD_ACTOR_H) + #define FDBRPC_ASYNCFILEREADAHEAD_ACTOR_H + +#include "flow/flow.h" +#include "IAsyncFile.h" + +// Read-only file type that wraps another file instance, reads in large blocks, and reads ahead of the actual range requested +class AsyncFileReadAheadCache : public IAsyncFile, public ReferenceCounted { +public: + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + struct CacheBlock : ReferenceCounted { + CacheBlock(int size = 0) : data(new uint8_t[size]), len(size) {} + ~CacheBlock() { delete [] data; } + uint8_t *data; + int len; + }; + + // Read from the underlying file to a CacheBlock + ACTOR static Future> readBlock(Reference f, int length, int64_t offset) { + Void _ = wait(f->m_max_concurrent_reads.take(1)); + + state Reference block(new CacheBlock(length)); + try { + int len = wait(f->m_f->read(block->data, length, offset)); + block->len = len; + } catch(Error &e) { + f->m_max_concurrent_reads.release(1); + throw e; + } + + f->m_max_concurrent_reads.release(1); + return block; + } + + ACTOR static Future read_impl(Reference f, void *data, int length, int64_t offset) { + // Make sure range is valid for the file + int64_t fileSize = wait(f->size()); + if(offset >= fileSize) + return 0; // TODO: Should this throw since the input isn't really valid? + + // If reading past the end then clip length to just read to the end + if(offset + length > fileSize) + length = fileSize - offset; // Length is at least 1 since offset < fileSize + + // Calculate block range for the blocks that contain this data + state int firstBlockNum = offset / f->m_block_size; + state int lastBlockNum = (offset + length) / f->m_block_size; + state int blockNum; + + // Start reads (if needed) of the block range required for this read, plus the read ahead blocks + // The futures for the read started will be stored in the cache but since things can be evicted from + // the cache while we're wait()ing we also will keep a local cache of futures for the blocks + // we need (not the read ahead blocks). + state std::map>> localCache; + + // Start blocks up to the read ahead size beyond the last needed block but don't go past the end of the file + state int lastBlockNumInFile = ((fileSize + f->m_block_size - 1) / f->m_block_size) - 1; + int lastBlockToStart = std::min(lastBlockNum + f->m_read_ahead_blocks, lastBlockNumInFile); + + for(blockNum = firstBlockNum; blockNum <= lastBlockToStart; ++blockNum) { + Future> fblock; + + // Look in the per-file cache for the block's future + auto i = f->m_blocks.find(blockNum); + // If not found, start the read. + if(i == f->m_blocks.end() || (i->second.isValid() && i->second.isError())) { + //printf("starting read of %s block %d\n", f->getFilename().c_str(), blockNum); + fblock = readBlock(f, f->m_block_size, f->m_block_size * blockNum); + f->m_blocks[blockNum] = fblock; + } + else + fblock = i->second; + + // Only put blocks we actually need into our local cache + if(blockNum <= lastBlockNum) + localCache[blockNum] = fblock; + } + + // Read block(s) and copy data + state int wpos = 0; + for(blockNum = firstBlockNum; blockNum <= lastBlockNum; ++blockNum) { + // Wait for block to be ready + Reference block = wait(localCache[blockNum]); + + // Calculate the block-relative read range. It's a given that the offset / length range touches this block so readStart will never + // be greater than blocksize (though it could be past the actual end of a short block). + int64_t blockStart = blockNum * f->m_block_size; + int64_t readStart = std::max(0, offset - blockStart); + int64_t readEnd = std::min(f->m_block_size, offset + length - blockStart); + int rlen = readEnd - readStart; + memcpy((uint8_t *)data + wpos, block->data + readStart, rlen); + wpos += rlen; + } + + ASSERT(wpos == length); + localCache.clear(); + + // If the cache is too large then go through the cache in block number order and remove any entries whose future + // has a reference count of 1, stopping once the cache is no longer too big. There is no point in removing + // an entry from the cache if it has a reference count of > 1 because it will continue to exist and use memory + // anyway so it should be left in the cache so that other readers may benefit from it. + + //printf("cache block limit: %d Cache contents:\n", f->m_cache_block_limit); + //for(auto &m : f->m_blocks) printf("\tblock %d refcount %d\n", m.first, m.second.getFutureReferenceCount()); + + if(f->m_blocks.size() > f->m_cache_block_limit) { + auto i = f->m_blocks.begin(); + while(i != f->m_blocks.end()) { + if(i->second.getFutureReferenceCount() == 1) { + //printf("evicting block %d\n", i->first); + i = f->m_blocks.erase(i); + if(f->m_blocks.size() <= f->m_cache_block_limit) + break; + } + else + ++i; + } + } + + return wpos; + } + + virtual Future read( void *data, int length, int64_t offset ) { + return read_impl(Reference::addRef(this), data, length, offset); + } + + virtual Future write( void const *data, int length, int64_t offset ) { throw file_not_writable(); } + virtual Future truncate( int64_t size ) { throw file_not_writable(); } + + virtual Future sync() { return Void(); } + virtual Future flush() { return Void(); } + + virtual Future size() { return m_f->size(); } + + virtual Future readZeroCopy( void** data, int* length, int64_t offset ) { + TraceEvent(SevError, "ReadZeroCopyNotSupported").detail("FileType", "ReadAheadCache"); + return platform_error(); + } + virtual void releaseZeroCopy( void* data, int length, int64_t offset ) {} + + virtual int64_t debugFD() { return -1; } + + virtual std::string getFilename() { return m_f->getFilename(); } + + virtual ~AsyncFileReadAheadCache() {} + + Reference m_f; + int m_block_size; + int m_read_ahead_blocks; + int m_cache_block_limit; + FlowLock m_max_concurrent_reads; + + // Map block numbers to future + std::map>> m_blocks; + + AsyncFileReadAheadCache(Reference f, int blockSize, int readAheadBlocks, int maxConcurrentReads, int cacheSizeBlocks) + : m_f(f), m_block_size(blockSize), m_read_ahead_blocks(readAheadBlocks), m_max_concurrent_reads(maxConcurrentReads), + m_cache_block_limit(std::max(1, cacheSizeBlocks)) { + } + +}; + +#endif \ No newline at end of file diff --git a/fdbrpc/AsyncFileWinASIO.actor.h b/fdbrpc/AsyncFileWinASIO.actor.h new file mode 100644 index 0000000000..66adb28d4d --- /dev/null +++ b/fdbrpc/AsyncFileWinASIO.actor.h @@ -0,0 +1,177 @@ +/* + * AsyncFileWinASIO.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#ifdef WIN32 + +#define Net2AsyncFile AsyncFileWinASIO + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FLOW_ASYNCFILEWINASIO_ACTOR_G_H) + #define FLOW_ASYNCFILEWINASIO_ACTOR_G_H + #include "AsyncFileWinASIO.actor.g.h" +#elif !defined(FLOW_ASYNCFILEWINASIO_ACTOR_H) + #define FLOW_ASYNCFILEWINASIO_ACTOR_H + +#include +#undef min +#undef max + +class AsyncFileWinASIO : public IAsyncFile, public ReferenceCounted { +public: + static void init() {} + + static bool should_poll() { return false; } + // FIXME: This implementation isn't actually asynchronous - it just does operations synchronously! + + static Future> open( std::string filename, int flags, int mode, boost::asio::io_service* ios ) { + if (!(flags & OPEN_UNBUFFERED)) { + TraceEvent(SevError, "FileOpenError").detail("Reason", "Must be unbuffered").detail("Flags", flags).detail("File", filename); + return io_error(); + } + + std::string open_filename = filename; + if (flags & OPEN_ATOMIC_WRITE_AND_CREATE) { + ASSERT( (flags & OPEN_CREATE) && (flags & OPEN_READWRITE) && !(flags & OPEN_EXCLUSIVE) ); + open_filename = filename + ".part"; + } + + HANDLE h = CreateFile( open_filename.c_str(), + GENERIC_READ | ((flags&OPEN_READWRITE) ? GENERIC_WRITE : 0), + FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, + (flags&OPEN_EXCLUSIVE) ? CREATE_NEW : + (flags&OPEN_CREATE) ? OPEN_ALWAYS : + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING, + NULL ); + if (h == INVALID_HANDLE_VALUE) { + bool notFound = GetLastError() == ERROR_FILE_NOT_FOUND; + Error e = notFound ? file_not_found() : io_error(); + TraceEvent(notFound ? SevWarn : SevWarnAlways, "FileOpenError").error(e).GetLastError() + .detail("File", filename).detail("Flags", flags).detail("Mode", mode); + return e; + } + return Reference( new AsyncFileWinASIO( *ios, h, flags, filename ) ); + } + static Future deleteFile( std::string filename, bool mustBeDurable ) { + ::deleteFile(filename); + // SOMEDAY: What is necessary to implement mustBeDurable on Windows? Does DeleteFile take care of it? DeleteFileTransacted? + return Void(); + } + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + virtual int64_t debugFD() { return (int64_t)file.native_handle(); } + + static void onReadReady( Promise onReady, const boost::system::error_code& error, size_t bytesRead ) { + if (error) { + Error e = io_error(); + TraceEvent("AsyncReadError").GetLastError().error(e) + .detail("ASIOCode", error.value()) + .detail("ASIOMessage", error.message()); + onReady.sendError(e); + } else { + onReady.send( bytesRead ); + } + } + static void onWriteReady( Promise onReady, size_t bytesExpected, const boost::system::error_code& error, size_t bytesWritten ) { + if (error) { + Error e = io_error(); + TraceEvent("AsyncWriteError").GetLastError().error(e) + .detail("ASIOCode", error.value()) + .detail("ASIOMessage", error.message()); + onReady.sendError(e); + } else if (bytesWritten != bytesExpected) { + Error e = io_error(); + TraceEvent("AsyncWriteError").detail("BytesExpected", bytesExpected).detail("BytesWritten", bytesWritten); + onReady.sendError(io_error()); + } else { + onReady.send( Void() ); + } + } + + virtual Future read( void* data, int length, int64_t offset ) { + // the size call is set inline + auto end = this->size().get(); + //TraceEvent("WinAsyncRead").detail("Offset", offset).detail("Length", length).detail("FileSize", end).detail("FileName", filename); + if(offset >= end) + return 0; + + Promise result; + file.async_read_some_at(offset, boost::asio::mutable_buffers_1(data, length), boost::bind(&onReadReady, result, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + + return result.getFuture(); + } + virtual Future write( void const* data, int length, int64_t offset ) { + /* + FIXME + if ( length + offset >= fileValidData ) { + SetFileValidData( length+offset ); + fileValidData = length+offset; + }*/ + Promise result; + boost::asio::async_write_at( file, offset, boost::asio::const_buffers_1( data, length ), boost::bind( &onWriteReady, result, length, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred ) ); + return result.getFuture(); + } + virtual Future truncate( int64_t size ) { + // FIXME: Possibly use SetFileInformationByHandle( file.native_handle(), FileEndOfFileInfo, ... ) instead + if (!SetFilePointerEx( file.native_handle(), *(LARGE_INTEGER*)&size, NULL, FILE_BEGIN )) + throw io_error(); + if (!SetEndOfFile(file.native_handle())) + throw io_error(); + return Void(); + } + virtual Future sync() { + // FIXME: Do FlushFileBuffers in a worker thread (using g_network->createThreadPool)? + if (!FlushFileBuffers( file.native_handle() )) throw io_error(); + + if (flags & OPEN_ATOMIC_WRITE_AND_CREATE) { + flags &= ~OPEN_ATOMIC_WRITE_AND_CREATE; + // FIXME: MoveFileEx(..., MOVEFILE_WRITE_THROUGH) in thread? + MoveFile( (filename+".part").c_str(), filename.c_str() ); + } + + return Void(); + } + virtual Future size() { + LARGE_INTEGER s; + if (!GetFileSizeEx(file.native_handle(), &s)) throw io_error(); + return *(int64_t*)&s; + } + virtual std::string getFilename() { + return filename; + } + + ~AsyncFileWinASIO() { } + +private: + boost::asio::windows::random_access_handle file; + int flags; + std::string filename; + + AsyncFileWinASIO(boost::asio::io_service& ios, HANDLE h, int flags, std::string filename) + : file(ios, h), flags(flags), filename(filename) {} +}; + + +#endif +#endif \ No newline at end of file diff --git a/fdbrpc/BlobStore.actor.cpp b/fdbrpc/BlobStore.actor.cpp new file mode 100755 index 0000000000..4c55a6bf54 --- /dev/null +++ b/fdbrpc/BlobStore.actor.cpp @@ -0,0 +1,753 @@ +/* + * BlobStore.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "BlobStore.h" + +#include "md5/md5.h" +#include "libb64/encode.h" +#include "sha1/SHA1.h" +#include "time.h" +#include "fdbclient/json_spirit/json_spirit_reader_template.h" +#include +#include + +json_spirit::mObject BlobStoreEndpoint::Stats::getJSON() { + json_spirit::mObject o; + + o["requests_failed"] = requests_failed; + o["requests_successful"] = requests_successful; + o["bytes_sent"] = bytes_sent; + + return o; +} + +BlobStoreEndpoint::Stats BlobStoreEndpoint::Stats::operator-(const Stats &rhs) { + Stats r; + r.requests_failed = requests_failed - rhs.requests_failed; + r.requests_successful = requests_successful - rhs.requests_successful; + r.bytes_sent = bytes_sent - rhs.bytes_sent; + return r; +} + +BlobStoreEndpoint::Stats BlobStoreEndpoint::s_stats; + +BlobStoreEndpoint::BlobKnobs::BlobKnobs() { + connect_tries = CLIENT_KNOBS->BLOBSTORE_CONNECT_TRIES; + connect_timeout = CLIENT_KNOBS->BLOBSTORE_CONNECT_TIMEOUT; + request_tries = CLIENT_KNOBS->BLOBSTORE_REQUEST_TRIES; + request_timeout = CLIENT_KNOBS->BLOBSTORE_REQUEST_TIMEOUT; + requests_per_second = CLIENT_KNOBS->BLOBSTORE_REQUESTS_PER_SECOND; + concurrent_requests = CLIENT_KNOBS->BLOBSTORE_CONCURRENT_REQUESTS; + multipart_max_part_size = CLIENT_KNOBS->BLOBSTORE_MULTIPART_MAX_PART_SIZE; + multipart_min_part_size = CLIENT_KNOBS->BLOBSTORE_MULTIPART_MIN_PART_SIZE; + concurrent_uploads = CLIENT_KNOBS->BLOBSTORE_CONCURRENT_UPLOADS; + concurrent_reads_per_file = CLIENT_KNOBS->BLOBSTORE_CONCURRENT_READS_PER_FILE; + read_block_size = CLIENT_KNOBS->BLOBSTORE_READ_BLOCK_SIZE; + read_ahead_blocks = CLIENT_KNOBS->BLOBSTORE_READ_AHEAD_BLOCKS; + read_cache_blocks_per_file = CLIENT_KNOBS->BLOBSTORE_READ_CACHE_BLOCKS_PER_FILE; + max_send_bytes_per_second = CLIENT_KNOBS->BLOBSTORE_MAX_RECV_BYTES_PER_SECOND; + max_recv_bytes_per_second = CLIENT_KNOBS->BLOBSTORE_MAX_SEND_BYTES_PER_SECOND; + buckets_to_span = CLIENT_KNOBS->BLOBSTORE_BACKUP_BUCKETS; +} + +bool BlobStoreEndpoint::BlobKnobs::set(StringRef name, int value) { + #define TRY_PARAM(n, sn) if(name == LiteralStringRef(#n) || name == LiteralStringRef(#sn)) { n = value; return true; } + TRY_PARAM(buckets_to_span, bts); + TRY_PARAM(connect_tries, ct); + TRY_PARAM(connect_timeout, cto); + TRY_PARAM(request_tries, rt); + TRY_PARAM(request_timeout, rto); + TRY_PARAM(requests_per_second, rps); + TRY_PARAM(concurrent_requests, cr); + TRY_PARAM(multipart_max_part_size, maxps); + TRY_PARAM(multipart_min_part_size, minps); + TRY_PARAM(concurrent_uploads, cu); + TRY_PARAM(concurrent_reads_per_file, crpf); + TRY_PARAM(read_block_size, rbs); + TRY_PARAM(read_ahead_blocks, rab); + TRY_PARAM(read_cache_blocks_per_file, rcb); + TRY_PARAM(max_send_bytes_per_second, sbps); + TRY_PARAM(max_recv_bytes_per_second, rbps); + #undef TRY_PARAM + return false; +} + +// Returns a Blob URL parameter string that specifies all of the non-default options for the endpoint using option short names. +std::string BlobStoreEndpoint::BlobKnobs::getURLParameters() const { + static BlobKnobs defaults; + std::string r; + #define _CHECK_PARAM(n, sn) if(n != defaults. n) { r += format("%s%s=%d", r.empty() ? "" : "&", #sn, n); } + _CHECK_PARAM(buckets_to_span, bts); + _CHECK_PARAM(connect_tries, ct); + _CHECK_PARAM(connect_timeout, cto); + _CHECK_PARAM(request_tries, rt); + _CHECK_PARAM(request_timeout, rto); + _CHECK_PARAM(requests_per_second, rps); + _CHECK_PARAM(concurrent_requests, cr); + _CHECK_PARAM(multipart_max_part_size, maxps); + _CHECK_PARAM(multipart_min_part_size, minps); + _CHECK_PARAM(concurrent_uploads, cu); + _CHECK_PARAM(concurrent_reads_per_file, crpf); + _CHECK_PARAM(read_block_size, rbs); + _CHECK_PARAM(read_ahead_blocks, rab); + _CHECK_PARAM(read_cache_blocks_per_file, rcb); + _CHECK_PARAM(max_send_bytes_per_second, sbps); + _CHECK_PARAM(max_recv_bytes_per_second, rbps); + #undef _CHECK_PARAM + return r; +} + + +struct tokenizer { + tokenizer(StringRef s) : s(s) {} + StringRef tok(StringRef sep) { + for(int i = 0, iend = s.size() - sep.size(); i <= iend; ++i) { + if(s.substr(i, sep.size()) == sep) { + StringRef token = s.substr(0, i); + s = s.substr(i + sep.size()); + return token; + } + } + StringRef token = s; + s = StringRef(); + return token; + } + StringRef tok(const char *sep) { return tok(StringRef((const uint8_t *)sep, strlen(sep))); } + StringRef s; +}; + +Reference BlobStoreEndpoint::fromString(std::string const &url, std::string *resourceFromURL, std::string *error) { + if(resourceFromURL) + resourceFromURL->clear(); + + try { + tokenizer t(url); + StringRef prefix = t.tok("://"); + if(prefix != LiteralStringRef("blobstore")) + throw std::string("Invalid blobstore URL."); + StringRef key = t.tok(":"); + StringRef secret = t.tok("@"); + StringRef hosts = t.tok(":"); + StringRef port = t.tok("/"); + StringRef resource = t.tok("?"); + + // The host/ip list can have up to one text hostname, which should be first, and then 0 or more IP addresses. + // The items are comma separated. + std::string host; + std::vector addrs; + uint16_t portNum = (uint16_t)strtoul(port.toString().c_str(), NULL, 10); + + tokenizer h(hosts); + while(1) { + StringRef item = h.tok(","); + if(item.size() == 0) + break; + // Try to parse item as an IP with the given port, if it fails it will throw so then store it as host + try { + // Use an integer port number so the parse doesn't fail due to the port number being garbage + NetworkAddress na = NetworkAddress::parse(format("%s:%d", item.toString().c_str(), (int)portNum)); + addrs.push_back(na); + } catch(Error &e) { + host = item.toString(); + } + } + + BlobKnobs knobs; + while(1) { + StringRef name = t.tok("="); + if(name.size() == 0) + break; + StringRef value = t.tok("&"); + int ivalue = strtol(value.toString().c_str(), NULL, 10); + if(ivalue == 0) + throw format("%s is not a valid value for %s", value.toString().c_str(), name.toString().c_str()); + if(!knobs.set(name, ivalue)) + throw format("%s is not a valid parameter name", name.toString().c_str()); + } + + if(resourceFromURL != nullptr) + *resourceFromURL = resource.toString(); + + return Reference(new BlobStoreEndpoint(host, addrs, portNum, key.toString(), secret.toString(), knobs)); + + } catch(std::string &err) { + if(error != nullptr) + *error = err; + TraceEvent(SevWarn, "BlobStoreEndpoint").detail("Description", err).detail("Format", getURLFormat()).detail("URL", url); + throw file_not_found(); + } +} + +std::string BlobStoreEndpoint::getResourceURL(std::string resource) { + std::string hosts = host; + for(auto &na : addresses) { + if(hosts.size() != 0) + hosts.append(","); + hosts.append(toIPString(na.ip)); + } + std::string r = format("blobstore://%s:%s@%s:%d/%s", key.c_str(), secret.c_str(), hosts.c_str(), (int)port, resource.c_str()); + std::string p = knobs.getURLParameters(); + if(!p.empty()) + r.append("?").append(p); + return r; +} + +ACTOR Future resolveHostname_impl(Reference bstore) { + state std::vector ip_addresses; + + // TODO: Resolve host to get list of IPs into ip_addresses. Ideally this should be done using + // boost asio so that backup agents can re-resolve a blobstore endpoint if none of the IP addresses + // are reachable for some long amount of time that exceeds time-to-live for the hostname. + // However, if it is done using blocking calls instead then the command line tools that use + // blobstore URLs will have to resolve them at a safe time during input parsing / checking. + // + + // Don't modify the existing IP address list if resolution did not work + if(ip_addresses.empty()) + return Void(); + + // Resolve was successful so replace addresses with the new IPs found. + bstore->addresses.clear(); + for(auto &ip : ip_addresses) + bstore->addresses.push_back(NetworkAddress(ip, bstore->port)); + + return Void(); +} + +Future BlobStoreEndpoint::resolveHostname(bool only_if_unresolved) { + if(only_if_unresolved && !addresses.empty()) + return Void(); + + return resolveHostname_impl(Reference::addRef(this)); +} + +ACTOR Future objectExists_impl(Reference b, std::string bucket, std::string object) { + std::string resource = std::string("/") + bucket + "/" + object; + HTTP::Headers headers; + + Reference r = wait(b->doRequest("HEAD", resource, headers, NULL, 0)); + if(r->code == 200) + return true; + + if(r->code == 404) + return false; + throw http_bad_response(); +} + +Future BlobStoreEndpoint::objectExists(std::string const &bucket, std::string const &object) { + return objectExists_impl(Reference::addRef(this), bucket, object); +} + +ACTOR Future deleteObject_impl(Reference b, std::string bucket, std::string object) { + std::string resource = std::string("/") + bucket + "/" + object; + HTTP::Headers headers; + Reference r = wait(b->doRequest("DELETE", resource, headers, NULL, 0)); + // 200 means object deleted, 404 means it doesn't exist, so we'll call that success as well + if(r->code == 200 || r->code == 404) + return Void(); + throw http_bad_response(); +} + +Future BlobStoreEndpoint::deleteObject(std::string const &bucket, std::string const &object) { + return deleteObject_impl(Reference::addRef(this), bucket, object); +} + +ACTOR Future deleteBucket_impl(Reference b, std::string bucket, int *pNumDeleted) { + state PromiseStream resultStream; + state Future done = b->getBucketContentsStream(bucket, resultStream); + state std::vector> deleteFutures; + loop { + choose { + when(Void _ = wait(done)) { + break; + } + when(BlobStoreEndpoint::ObjectInfo info = waitNext(resultStream.getFuture())) { + if(pNumDeleted == nullptr) + deleteFutures.push_back(b->deleteObject(bucket, info.name)); + else + deleteFutures.push_back(map(b->deleteObject(bucket, info.name), [this](Void) -> Void { ++*pNumDeleted; return Void(); })); + } + } + } + + Void _ = wait(waitForAll(deleteFutures)); + return Void(); +} + +Future BlobStoreEndpoint::deleteBucket(std::string const &bucket, int *pNumDeleted) { + return deleteBucket_impl(Reference::addRef(this), bucket, pNumDeleted); +} + +ACTOR Future objectSize_impl(Reference b, std::string bucket, std::string object) { + std::string resource = std::string("/") + bucket + "/" + object; + HTTP::Headers headers; + + Reference r = wait(b->doRequest("HEAD", resource, headers, NULL, 0)); + if(r->code != 200) + throw io_error(); + return r->contentLen; +} + +Future BlobStoreEndpoint::objectSize(std::string const &bucket, std::string const &object) { + return objectSize_impl(Reference::addRef(this), bucket, object); +} + +ACTOR Future> connect_impl(Reference bstore) { + while(!bstore->connectionPool.empty()) { + BlobStoreEndpoint::ConnPoolEntry c = bstore->connectionPool.front(); + bstore->connectionPool.pop_front(); + + // If the connection was placed in the pool less than 10 seconds ago, reuse it. + if(c.second > now() - 10) { + //printf("Reusing blob store connection\n"); + return c.first; + } + } + + state Reference conn; + state int tries = bstore->knobs.connect_tries; + while(!conn && tries-- > 0) { + try { + if(bstore->addresses.size() == 0) + throw connection_string_invalid(); + Reference c = wait(INetworkConnections::net()->connect(bstore->addresses[g_random->randomInt(0, bstore->addresses.size())])); + conn = c; + } catch (Error &e) { + //TraceEvent(SevWarn, "BlobStoreConnectError").detail("Host", bstore->host).detail("Port", bstore->port).error(e); + throw; + } + } + + return conn; +} + +Future> BlobStoreEndpoint::connect() { + return connect_impl(Reference::addRef(this)); +} + +// Do a request, get a Response. +// Request content is provided as UnsentPacketQueue *pContent which will be depleted as bytes are sent but the queue itself must live for the life of this actor +// and be destroyed by the caller +ACTOR Future> doRequest_impl(Reference bstore, std::string verb, std::string resource, HTTP::Headers headers, UnsentPacketQueue *pContent, int contentLen) { + state UnsentPacketQueue contentCopy; + + // Set content length header if there is content + if(contentLen > 0) + headers["Content-Length"] = format("%d", contentLen); + + Void _ = wait(bstore->concurrentRequests.take(1)); + + try { + state int tries = bstore->knobs.request_tries; + state double retryDelay = 2.0; + loop { + try { + // Start connecting + Future> fconn = bstore->connect(); + + // Finish/update the request headers (which includes Date header) + bstore->setAuthHeaders(verb, resource, headers); + + // Make a shallow copy of the queue by calling addref() on each buffer in the chain and then prepending that chain to contentCopy + if(pContent != nullptr) { + contentCopy.discardAll(); + PacketBuffer *pFirst = pContent->getUnsent(); + PacketBuffer *pLast = nullptr; + for(PacketBuffer *p = pFirst; p != nullptr; p = p->nextPacketBuffer()) { + p->addref(); + // Also reset the sent count on each buffer + p->bytes_sent = 0; + pLast = p; + } + contentCopy.prependWriteBuffer(pFirst, pLast); + } + + // Finish connecting, do request + state Reference conn = wait(timeoutError(fconn, bstore->knobs.connect_timeout)); + Void _ = wait(bstore->requestRate->getAllowance(1)); + state Reference r = wait(timeoutError(HTTP::doRequest(conn, verb, resource, headers, &contentCopy, contentLen, bstore->sendRate, &bstore->s_stats.bytes_sent, bstore->recvRate), bstore->knobs.request_timeout)); + + std::string connectionHeader; + HTTP::Headers::iterator i = r->headers.find("Connection"); + if(i != r->headers.end()) + connectionHeader = i->second; + + // If the response parsed successfully (which is why we reached this point) and the connection can be reused, put the connection in the connection_pool + if(connectionHeader != "close") + bstore->connectionPool.push_back(BlobStoreEndpoint::ConnPoolEntry(conn, now())); + + // Handle retry-after response code + if(r->code == 429) { + bstore->s_stats.requests_failed++; + conn = Reference(); + double d = 60; + if(r->headers.count("Retry-After")) + d = atof(r->headers["Retry-After"].c_str()); + Void _ = wait(delay(d)); + // Just continue, don't throw an error, don't decrement tries + } + else if(r->code == 406) { + // Blob returns this when the account doesn't exist + throw http_not_accepted(); + } + else if(r->code == 500) { + // For error 500 just treat it like connection_failed + throw connection_failed(); + } + else + break; + } catch(Error &e) { + // If the error is connection failed and a retry is allowed then ignore the error + if((e.code() == error_code_connection_failed || e.code() == error_code_timed_out) && --tries > 0) { + bstore->s_stats.requests_failed++; + //TraceEvent(SevWarn, "BlobStoreHTTPConnectionFailed").detail("Verb", verb).detail("Resource", resource).detail("Host", bstore->host).detail("Port", bstore->port); + //printf("Retrying (%d left) %s %s\n", tries, verb.c_str(), resource.c_str()); + Void _ = wait(delay(retryDelay)); + retryDelay *= 2; + } + else + throw; + } + } + + } catch(Error &e) { + bstore->concurrentRequests.release(1); + throw; + } + + bstore->concurrentRequests.release(1); + bstore->s_stats.requests_successful++; + + return r; +} + +Future> BlobStoreEndpoint::doRequest(std::string const &verb, std::string const &resource, const HTTP::Headers &headers, UnsentPacketQueue *pContent, int contentLen) { + return doRequest_impl(Reference::addRef(this), verb, resource, headers, pContent, contentLen); +} + +ACTOR Future getBucketContentsStream_impl(Reference bstore, std::string bucket, PromiseStream results) { + // Request 1000 keys at a time, the maximum allowed + state std::string resource = std::string("/") + bucket + "/?max-keys=1000&marker="; + state std::string lastFile; + state bool more = true; + + while(more) { + HTTP::Headers headers; + Reference r = wait(bstore->doRequest("GET", resource + HTTP::urlEncode(lastFile), headers, NULL, 0)); + + try { + // Parse the json assuming it is valid and contains the right stuff. If any exceptions are thrown, throw http_bad_response + json_spirit::Value json; + json_spirit::read_string(r->content, json); + for(auto &i : json.get_obj()) { + if(i.name_ == "truncated") { + more = i.value_.get_bool(); + } + else if(i.name_ == "results") { + BlobStoreEndpoint::ObjectInfo info; + info.bucket = bucket; + for(auto &o : i.value_.get_array()) { + info.size = -1; + info.name.clear(); + for(auto &f : o.get_obj()) { + if(f.name_ == "size") + info.size = f.value_.get_int(); + else if(f.name_ == "key") + info.name = f.value_.get_str(); + } + if(info.size >= 0 && !info.name.empty()) { + lastFile = info.name; + results.send(std::move(info)); + } + } + } + } + } catch(Error &e) { + throw http_bad_response(); + } + } + + return Void(); +} + +Future BlobStoreEndpoint::getBucketContentsStream(std::string const &bucket, PromiseStream results) { + return getBucketContentsStream_impl(Reference::addRef(this), bucket, results); +} + +ACTOR Future getBucketContents_impl(Reference bstore, std::string bucket) { + state BlobStoreEndpoint::BucketContentsT results; + state PromiseStream resultStream; + state Future done = bstore->getBucketContentsStream(bucket, resultStream); + loop { + choose { + when(Void _ = wait(done)) { + break; + } + when(BlobStoreEndpoint::ObjectInfo info = waitNext(resultStream.getFuture())) { + results.push_back(info); + } + } + } + return results; +} + +Future BlobStoreEndpoint::getBucketContents(std::string const &bucket) { + return getBucketContents_impl(Reference::addRef(this), bucket); +} + +std::string BlobStoreEndpoint::hmac_sha1(std::string const &msg) { + std::string key = secret; + + // First pad the key to 64 bytes. + key.append(64 - key.size(), '\0'); + + std::string kipad = key; + for(int i = 0; i < 64; ++i) + kipad[i] ^= '\x36'; + + std::string kopad = key; + for(int i = 0; i < 64; ++i) + kopad[i] ^= '\x5c'; + + kipad.append(msg); + std::string hkipad = SHA1::from_string(kipad); + kopad.append(hkipad); + return SHA1::from_string(kopad); +} + +void BlobStoreEndpoint::setAuthHeaders(std::string const &verb, std::string const &resource, HTTP::Headers& headers) { + time_t ts; + time(&ts); + std::string &date = headers["Date"]; + date = std::string(asctime(gmtime(&ts)), 24) + " GMT"; // asctime() returns a 24 character string plus a \n and null terminator. + std::string msg; + StringRef x; + msg.append(verb); + msg.append("\n"); + auto contentMD5 = headers.find("Content-MD5"); + if(contentMD5 != headers.end()) + msg.append(contentMD5->second); + msg.append("\n"); + auto contentType = headers.find("Content-Type"); + if(contentType != headers.end()) + msg.append(contentType->second); + msg.append("\n"); + msg.append(date); + msg.append("\n"); + for(auto h : headers) { + StringRef name = h.first; + if(name.startsWith(LiteralStringRef("x-amz")) || + name.startsWith(LiteralStringRef("x-icloud"))) { + msg.append(h.first); + msg.append(":"); + msg.append(h.second); + msg.append("\n"); + } + } + + msg.append(resource); + if(verb == "GET") { + size_t q = resource.find_last_of('?'); + if(q != resource.npos) + msg.resize(msg.size() - (resource.size() - q)); + } + + std::string sig = base64::encoder::from_string(hmac_sha1(msg)); + // base64 encoded blocks end in \n so remove it. + sig.resize(sig.size() - 1); + std::string auth = key; + auth.append(":"); + auth.append(sig); + headers["Authorization"] = auth; +} + +ACTOR Future readEntireFile_impl(Reference bstore, std::string bucket, std::string object) { + std::string resource = std::string("/") + bucket + "/" + object; + HTTP::Headers headers; + Reference r = wait(bstore->doRequest("GET", resource, headers, NULL, 0)); + if(r->code == 200) + return r->content; + if(r->code == 404) + throw file_not_found(); + throw http_bad_response(); +} + +Future BlobStoreEndpoint::readEntireFile(std::string const &bucket, std::string const &object) { + return readEntireFile_impl(Reference::addRef(this), bucket, object); +} + +ACTOR Future writeEntireFileFromBuffer_impl(Reference bstore, std::string bucket, std::string object, UnsentPacketQueue *pContent, int contentLen, std::string contentMD5) { + if(contentLen == 0) + throw file_not_writable(); + + if(contentLen > bstore->knobs.multipart_max_part_size) + throw file_too_large(); + + try { + Void _ = wait(bstore->concurrentUploads.take(1)); + std::string resource = std::string("/") + bucket + "/" + object; + HTTP::Headers headers; + // Send MD5 sum for content so blobstore can verify it + headers["Content-MD5"] = contentMD5; + state Reference r = wait(bstore->doRequest("PUT", resource, headers, pContent, contentLen)); + + // For uploads, Blobstore returns an MD5 sum of uploaded content so check that too. + auto sum = r->headers.find("Content-MD5"); + if(sum == r->headers.end() || sum->second != contentMD5) + throw http_bad_response(); + + } catch(Error &e) { + bstore->concurrentUploads.release(1); + throw; + } + + bstore->concurrentUploads.release(1); + if(r->code == 200) + return Void(); + throw http_bad_response(); +} + +ACTOR Future writeEntireFile_impl(Reference bstore, std::string bucket, std::string object, std::string content) { + state UnsentPacketQueue packets; + PacketWriter pw(packets.getWriteBuffer(), NULL, Unversioned()); + pw.serializeBytes(content); + if(content.size() > bstore->knobs.multipart_max_part_size) + throw file_too_large(); + + // Yield because we may have just had to copy several MB's into packet buffer chain and next we have to calculate an MD5 sum of it. + // TODO: If this actor is used to send large files then combine the summing and packetization into a loop with a yield() every 20k or so. + Void _ = wait(yield()); + + MD5_CTX sum; + ::MD5_Init(&sum); + ::MD5_Update(&sum, content.data(), content.size()); + std::string sumBytes; + sumBytes.resize(16); + ::MD5_Final((unsigned char *)sumBytes.data(), &sum); + std::string contentMD5 = base64::encoder::from_string(sumBytes); + contentMD5.resize(contentMD5.size() - 1); + + Void _ = wait(writeEntireFileFromBuffer_impl(bstore, bucket, object, &packets, content.size(), contentMD5)); + return Void(); +} + +Future BlobStoreEndpoint::writeEntireFile(std::string const &bucket, std::string const &object, std::string const &content) { + return writeEntireFile_impl(Reference::addRef(this), bucket, object, content); +} + +Future BlobStoreEndpoint::writeEntireFileFromBuffer(std::string const &bucket, std::string const &object, UnsentPacketQueue *pContent, int contentLen, std::string const &contentMD5) { + return writeEntireFileFromBuffer_impl(Reference::addRef(this), bucket, object, pContent, contentLen, contentMD5); +} + +ACTOR Future readObject_impl(Reference bstore, std::string bucket, std::string object, void *data, int length, int64_t offset) { + if(length <= 0) + return 0; + std::string resource = std::string("/") + bucket + "/" + object; + HTTP::Headers headers; + headers["Range"] = format("bytes=%lld-%lld", offset, offset + length - 1); + Reference r = wait(bstore->doRequest("GET", resource, headers, NULL, 0)); + if(r->code != 200 && r->code != 206) + throw file_not_readable(); + if(r->contentLen != r->content.size()) // Double check that this wasn't a header-only response, probably unnecessary + throw io_error(); + // Copy the output bytes, server could have sent more or less bytes than requested so copy at most length bytes + memcpy(data, r->content.data(), std::min(r->contentLen, length)); + return r->contentLen; +} + +Future BlobStoreEndpoint::readObject(std::string const &bucket, std::string const &object, void *data, int length, int64_t offset) { + return readObject_impl(Reference::addRef(this), bucket, object, data, length, offset); +} + +ACTOR static Future beginMultiPartUpload_impl(Reference bstore, std::string bucket, std::string object) { + std::string resource = std::string("/") + bucket + "/" + object + "?uploads"; + HTTP::Headers headers; + Reference r = wait(bstore->doRequest("POST", resource, headers, NULL, 0)); + if(r->code != 200) + throw file_not_writable(); + int start = r->content.find(""); + if(start == std::string::npos) + throw http_bad_response(); + start += 10; + int end = r->content.find("", start); + if(end == std::string::npos) + throw http_bad_response(); + return r->content.substr(start, end - start); +} + +Future BlobStoreEndpoint::beginMultiPartUpload(std::string const &bucket, std::string const &object) { + return beginMultiPartUpload_impl(Reference::addRef(this), bucket, object); +} + +ACTOR Future uploadPart_impl(Reference bstore, std::string bucket, std::string object, std::string uploadID, unsigned int partNumber, UnsentPacketQueue *pContent, int contentLen, std::string contentMD5) { + try { + Void _ = wait(bstore->concurrentUploads.take(1)); + std::string resource = format("/%s/%s?partNumber=%d&uploadId=%s", bucket.c_str(), object.c_str(), partNumber, uploadID.c_str()); + HTTP::Headers headers; + // Send MD5 sum for content so blobstore can verify it + headers["Content-MD5"] = contentMD5; + state Reference r = wait(bstore->doRequest("PUT", resource, headers, pContent, contentLen)); + + // For uploads, Blobstore returns an MD5 sum of uploaded content so check that too. + auto sum = r->headers.find("Content-MD5"); + if(sum == r->headers.end() || sum->second != contentMD5) + throw http_bad_response(); + } catch(Error &e) { + bstore->concurrentUploads.release(1); + throw; + } + + bstore->concurrentUploads.release(1); + if(r->code != 200) + throw http_bad_response(); + + std::string etag = r->headers["ETag"]; + if(etag.empty()) + throw http_bad_response(); + + return etag; +} + +Future BlobStoreEndpoint::uploadPart(std::string const &bucket, std::string const &object, std::string const &uploadID, unsigned int partNumber, UnsentPacketQueue *pContent, int contentLen, std::string const &contentMD5) { + return uploadPart_impl(Reference::addRef(this), bucket, object, uploadID, partNumber, pContent, contentLen, contentMD5); +} + +ACTOR Future finishMultiPartUpload_impl(Reference bstore, std::string bucket, std::string object, std::string uploadID, BlobStoreEndpoint::MultiPartSetT parts) { + state UnsentPacketQueue part_list(); // NonCopyable state var so must be declared at top of actor + + std::string manifest = ""; + for(auto &p : parts) + manifest += format("%d%s\n", p.first, p.second.c_str()); + manifest += ""; + + std::string resource = format("/%s/%s?uploadId=%s", bucket.c_str(), object.c_str(), uploadID.c_str()); + HTTP::Headers headers; + PacketWriter pw(part_list.getWriteBuffer(), NULL, Unversioned()); + pw.serializeBytes(manifest); + Reference r = wait(bstore->doRequest("POST", resource, headers, &part_list, manifest.size())); + if(r->code != 200) + throw http_bad_response(); + + return Void(); +} + +Future BlobStoreEndpoint::finishMultiPartUpload(std::string const &bucket, std::string const &object, std::string const &uploadID, MultiPartSetT const &parts) { + return finishMultiPartUpload_impl(Reference::addRef(this), bucket, object, uploadID, parts); +} diff --git a/fdbrpc/BlobStore.h b/fdbrpc/BlobStore.h new file mode 100755 index 0000000000..99e3bb006e --- /dev/null +++ b/fdbrpc/BlobStore.h @@ -0,0 +1,189 @@ +/* + * BlobStore.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include "flow/flow.h" +#include "flow/Net2Packet.h" +#include "fdbclient/Knobs.h" +#include "IRateControl.h" +#include "HTTP.h" +#include "fdbclient/json_spirit/json_spirit_writer_template.h" + +// Representation of all the things you need to connect to a blob store instance with some credentials. +// Reference counted because a very large number of them could be needed. +class BlobStoreEndpoint : public ReferenceCounted { +public: + struct Stats { + Stats() : requests_successful(0), requests_failed(0), bytes_sent(0) {} + Stats operator-(const Stats &rhs); + void clear() { memset(this, sizeof(*this), 0); } + json_spirit::mObject getJSON(); + + int64_t requests_successful; + int64_t requests_failed; + int64_t bytes_sent; + }; + + static Stats s_stats; + + struct BlobKnobs { + BlobKnobs(); + int connect_tries, + connect_timeout, + request_tries, + request_timeout, + requests_per_second, + concurrent_requests, + multipart_max_part_size, + multipart_min_part_size, + concurrent_uploads, + concurrent_reads_per_file, + read_block_size, + read_ahead_blocks, + read_cache_blocks_per_file, + max_send_bytes_per_second, + max_recv_bytes_per_second, + buckets_to_span; + bool set(StringRef name, int value); + std::string getURLParameters() const; + static std::vector getKnobDescriptions() { + return { + "connect_tries (or ct) Number of times to try to connect for each request.", + "connect_timeout (or cto) Number of seconds to wait for a connect request to succeed.", + "request_tries (or rt) Number of times to try each request until a parseable HTTP response other than 429 is received.", + "request_timeout (or rto) Number of seconds to wait for a request to succeed after a connection is established.", + "requests_per_second (or rps) Max number of requests to start per second.", + "concurrent_requests (or cr) Max number of requests in progress at once.", + "multipart_max_part_size (or maxps) Max part size for multipart uploads.", + "multipart_min_part_size (or minps) Min part size for multipart uploads.", + "concurrent_uploads (or cu) Max concurrent uploads (part or whole) that can be in progress at once.", + "concurrent_reads_per_file (or crps) Max concurrent reads in progress for any one file.", + "read_block_size (or rbs) Block size in bytes to be used for reads.", + "read_ahead_blocks (or rab) Number of blocks to read ahead of requested offset.", + "read_cache_blocks_per_file (or rcb) Size of the read cache for a file in blocks.", + "max_send_bytes_per_second (or sbps) Max send bytes per second for all requests combined.", + "max_recv_bytes_per_second (or rbps) Max receive bytes per second for all requests combined (NOT YET USED).", + "buckets_to_span (or bts) Number of buckets that a new backup should distribute over." + }; + } + }; + + BlobStoreEndpoint(std::string const &host, std::vector const &addrs, uint16_t port, std::string const &key, std::string const &key_secret, BlobKnobs const &knobs = BlobKnobs()) + : host(host), port(port), addresses(addrs), key(key), secret(key_secret), knobs(knobs), + requestRate(new SpeedLimit(knobs.requests_per_second, 1)), + sendRate(new SpeedLimit(knobs.max_send_bytes_per_second, 1)), + recvRate(new SpeedLimit(knobs.max_recv_bytes_per_second, 1)), + concurrentRequests(knobs.concurrent_requests), + concurrentUploads(knobs.concurrent_uploads) { + } + + static std::string getURLFormat(bool withResource = false) { + const char *resource = ""; + if(withResource) + resource = ""; + return format("blobstore://:@<[host,][,]...>:/%s[?=[&=]...]", resource); + } + static Reference fromString(std::string const &url, std::string *resourceFromURL = nullptr, std::string *error = nullptr); + + // Resolve host, and if successful replace addrs with a list of NetworkAddresses created from the resolve results. + Future resolveHostname(bool only_if_unresolved = true); + + // Get a normalized version of this URL with the given resource, the host and any IP addresses (possibly from DNS + // if resolve was done) and any non-default BlobKnob values as URL parameters. + std::string getResourceURL(std::string resource); + Future> connect(); + + typedef std::pair, double> ConnPoolEntry; + std::list connectionPool; + + std::string host; + uint16_t port; + std::vector addresses; + std::string key; + std::string secret; + BlobKnobs knobs; + + // Speed and concurrency limits + Reference requestRate; + Reference sendRate; + Reference recvRate; + FlowLock concurrentRequests; + FlowLock concurrentUploads; + + // Calculates the authentication string from the secret key + std::string hmac_sha1(std::string const &msg); + + // Sets headers needed for Authorization (including Date which will be overwritten if present) + void setAuthHeaders(std::string const &verb, std::string const &resource, HTTP::Headers &headers); + + // Prepend the HTTP request header to the given PacketBuffer, returning the new head of the buffer chain + static PacketBuffer * writeRequestHeader(std::string const &request, HTTP::Headers const &headers, PacketBuffer *dest); + + // Do an HTTP request to the Blob Store, read the response. Handles authentication. + // Every blob store interaction should ultimately go through this function + Future> doRequest(std::string const &verb, std::string const &resource, const HTTP::Headers &headers, UnsentPacketQueue *pContent, int contentLen); + + struct ObjectInfo { + std::string bucket; + std::string name; + int64_t size; + }; + + // Get bucket contents via a stream, since listing large buckets will take many serial blob requests + Future getBucketContentsStream(std::string const &bucket, PromiseStream results); + + // Get a list of the files in a bucket + typedef std::vector BucketContentsT; + Future getBucketContents(std::string const &bucket); + + // Check if an object exists in a bucket + Future objectExists(std::string const &bucket, std::string const &object); + + // Get the size of an object in a bucket + Future objectSize(std::string const &bucket, std::string const &object); + + // Read an arbitrary segment of an object + Future readObject(std::string const &bucket, std::string const &object, void *data, int length, int64_t offset); + + // Delete an object in a bucket + Future deleteObject(std::string const &bucket, std::string const &object); + + // Delete a bucket - note this is not atomic as blob store does not support this operation directly. + // This method is just a convenience method that lists and deletes all of the objects in the bucket + // Since it can take a while, if a pNumDeleted is provided then it will be incremented every time + // a deletion of an object completes. + Future deleteBucket(std::string const &bucket, int *pNumDeleted = NULL); + + // Useful methods for working with tiny files + Future readEntireFile(std::string const &bucket, std::string const &object); + Future writeEntireFile(std::string const &bucket, std::string const &object, std::string const &content); + Future writeEntireFileFromBuffer(std::string const &bucket, std::string const &object, UnsentPacketQueue *pContent, int contentLen, std::string const &contentMD5); + + // MultiPart upload methods + // Returns UploadID + Future beginMultiPartUpload(std::string const &bucket, std::string const &object); + // Returns eTag + Future uploadPart(std::string const &bucket, std::string const &object, std::string const &uploadID, unsigned int partNumber, UnsentPacketQueue *pContent, int contentLen, std::string const &contentMD5); + typedef std::map MultiPartSetT; + Future finishMultiPartUpload(std::string const &bucket, std::string const &object, std::string const &uploadID, MultiPartSetT const &parts); +}; + diff --git a/fdbrpc/ContinuousSample.h b/fdbrpc/ContinuousSample.h new file mode 100644 index 0000000000..75b2205830 --- /dev/null +++ b/fdbrpc/ContinuousSample.h @@ -0,0 +1,96 @@ +/* + * ContinuousSample.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CONTINUOUSSAMPLE_H +#define CONTINUOUSSAMPLE_H +#pragma once + +#include "flow/Platform.h" +#include "flow/IRandom.h" +#include +#include + +template +class ContinuousSample { +public: + explicit ContinuousSample( int sampleSize ) : sampleSize( sampleSize ), populationSize( 0 ), sorted( true ), _min(T()), _max(T()) {} + + ContinuousSample& addSample(T sample) { + if( !populationSize ) + _min = _max = sample; + populationSize++; + sorted = false; + + if( populationSize <= sampleSize ) { + samples.push_back( sample ); + } else if( g_random->random01() < ( (double)sampleSize / populationSize ) ) { + samples[ g_random->randomInt( 0, sampleSize ) ] = sample; + } + + _max = std::max( _max, sample ); + _min = std::min( _min, sample ); + return *this; + } + + double mean() { + if (!samples.size()) return 0; + T sum = 0; + for( int c = 0; c < samples.size(); c++ ) + sum += samples[ c ]; + return (double)sum / samples.size(); + } + + T median() { + return percentile( 0.5 ); + } + + T percentile( double percentile ) { + if( !samples.size() || percentile < 0.0 || percentile > 1.0 ) + return T(); + sort(); + int idx = std::floor( ( samples.size() - 1 ) * percentile ); + return samples[ idx ]; + } + + T min() { return _min; } + T max() { return _max; } + + void clear() { + samples.clear(); + populationSize = 0; + sorted = true; + _min = _max = 0; // Doesn't work for all T + } + +private: + int sampleSize; + uint64_t populationSize; + bool sorted; + std::vector samples; + T _min, _max; + + void sort() { + if( !sorted && samples.size() > 1 ) + std::sort( samples.begin(), samples.end() ); + sorted = true; + } +}; + +#endif diff --git a/fdbrpc/EndpointGroup.h b/fdbrpc/EndpointGroup.h new file mode 100644 index 0000000000..a609142503 --- /dev/null +++ b/fdbrpc/EndpointGroup.h @@ -0,0 +1,74 @@ +/* + * EndpointGroup.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_ENDPOINT_GROUP_H +#define FLOW_ENDPOINT_GROUP_H +#pragma once + +#include "flow.h" + +// EndpointGroup makes it easier to implement backward compatibility for interface serialization +// It also provides a central place to implement more compact serialization for a group of related endpoints in the future. + +/* Typical usage: + +template +void serialize(Ar& ar) { + auto endpoints = endpointGroup(ar); + endpoints.require( ar.protocolVersion() <= currentProtocolVersion ); + endpoints & apple & banana; + endpoints.require( ar.protocolVersion() >= 0xabc ); // Following endpoints added in this version + endpoints & cherry; + endpoints.require( ar.protocolVersion() >= 0xdef ); // .. and then some more were added + endpoints & date; +} + +*/ + + +template +struct EndpointGroup : NonCopyable { + Ar& ar; + bool enabled; + + explicit EndpointGroup( Ar& ar ) : ar(ar), enabled(true) { + ASSERT( ar.protocolVersion() != 0 ); + } + EndpointGroup( EndpointGroup&& g ) : ar(g.ar), enabled(g.enabled) {} + + EndpointGroup& require( bool condition ) { + enabled = enabled && condition; + return *this; + } + + template + EndpointGroup& operator & (PromiseStream& stream) { + if (enabled) + ar & stream; + else if (Ar::isDeserializing) + stream.sendError( incompatible_protocol_version() ); + return *this; + } +}; + +template +EndpointGroup endpointGroup( Ar& ar ) { return EndpointGroup(ar); } + +#endif \ No newline at end of file diff --git a/fdbrpc/FailureMonitor.actor.cpp b/fdbrpc/FailureMonitor.actor.cpp new file mode 100644 index 0000000000..34818faebc --- /dev/null +++ b/fdbrpc/FailureMonitor.actor.cpp @@ -0,0 +1,154 @@ +/* + * FailureMonitor.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "FailureMonitor.h" + +ACTOR Future waitForStateEqual( IFailureMonitor* monitor, Endpoint endpoint, FailureStatus status ) { + loop { + Future change = monitor->onStateChanged(endpoint); + if (monitor->getState(endpoint) == status) + return Void(); + Void _ = wait( change ); + } +} + +ACTOR Future waitForContinuousFailure( IFailureMonitor* monitor, Endpoint endpoint, double sustainedFailureDuration, double slope ) { + state double startT = now(); + loop { + Void _ = wait( monitor->onFailed( endpoint ) ); + if(monitor->permanentlyFailed(endpoint)) + return Void(); + + // X == sustainedFailureDuration + slope * (now()-startT+X) + double waitDelay = (sustainedFailureDuration + slope * (now()-startT)) / (1-slope); + if(waitDelay < FLOW_KNOBS->CLIENT_REQUEST_INTERVAL) //We will not get a failure monitoring update in this amount of time, so there is no point in waiting for changes + waitDelay = 0; + choose { + when (Void _ = wait( monitor->onStateEqual( endpoint, FailureStatus(false) ) )) {} // SOMEDAY: Use onStateChanged() for efficiency + when (Void _ = wait( delay(waitDelay) )) { + return Void(); + } + } + } +} + +Future IFailureMonitor::onStateEqual( Endpoint const& endpoint, FailureStatus status ) { + if ( status == getState(endpoint) ) return Void(); + return waitForStateEqual(this, endpoint, status); +} + +Future IFailureMonitor::onFailedFor( Endpoint const& endpoint, double sustainedFailureDuration, double slope ) { + ASSERT( slope < 1.0 ); + return waitForContinuousFailure( this, endpoint, sustainedFailureDuration, slope ); +} + +void SimpleFailureMonitor::setStatus( NetworkAddress const& address, FailureStatus const& status ) { + + //if (status.failed) + // printf("On machine '%s': Machine '%s' is failed\n", g_network->getLocalAddress().toString().c_str(), address.toString().c_str()); + //printf("%s.setState(%s, %s) %p\n", g_network->getLocalAddress().toString(), address.toString(), status.failed ? "FAILED" : "OK", this); + //addressStatus.set( address, status ); + + // onStateChanged() will be waiting on endpointKnownFailed only where it is false, so if the address status + // for an endpoint that is waited on changes, the waiter sees its failure status change + auto it = addressStatus.find(address); + + //TraceEvent("NotifyFailureStatus").detail("Address", address).detail("Status", status.failed ? "Failed" : "OK").detail("Present", it == addressStatus.end()); + if (it == addressStatus.end()) { + if (status != FailureStatus()) { + addressStatus[address]=status; + endpointKnownFailed.triggerRange( Endpoint(address, UID()), Endpoint(address, UID(-1,-1)) ); + } + } else { + bool triggerEndpoint = status != it->value; + if (status != FailureStatus()) + it->value = status; + else + addressStatus.erase(it); + if(triggerEndpoint) + endpointKnownFailed.triggerRange( Endpoint(address, UID()), Endpoint(address, UID(-1,-1)) ); + } +} + +void SimpleFailureMonitor::endpointNotFound( Endpoint const& endpoint ) { + // SOMEDAY: Expiration (this "leaks" memory) + TraceEvent("EndpointNotFound").detail("Address", endpoint.address).detail("Token", endpoint.token); + endpointKnownFailed.set( endpoint, true ); +} + +void SimpleFailureMonitor::notifyDisconnect( NetworkAddress const& address ) { + //TraceEvent("NotifyDisconnect").detail("Address", address); + endpointKnownFailed.triggerRange( Endpoint(address, UID()), Endpoint(address, UID(-1,-1)) ); +} + +Future SimpleFailureMonitor::onDisconnectOrFailure( Endpoint const& endpoint ) { + // If the endpoint or address is already failed, return right away + auto i = addressStatus.find(endpoint.address); + if (i == addressStatus.end() || i->value.isFailed() || endpointKnownFailed.get(endpoint)) { + TraceEvent("AlreadyDisconnected").detail("Addr", endpoint.address).detail("Tok", endpoint.token); + return Void(); + } + + // Return when the endpoint is triggered, which means that either the endpoint has become known failed, or the + // address has changed state (and since it was previously not failed, it must now be failed), or notifyDisconnect() + // has been called. + return endpointKnownFailed.onChange(endpoint); +} + +Future SimpleFailureMonitor::onStateChanged( Endpoint const& endpoint ) { + // Wait on endpointKnownFailed if it is false, to pick up both endpointNotFound errors (which set it to true) + // and changes to addressStatus (which trigger a range). Don't wait on endpointKnownFailed if it is true, because + // failure status for that endpoint can never change (and we could be spuriously triggered by setStatus) + // Also returns spuriously when notifyDisconnect is called (which doesn't actually change the state), but callers + // check the state so it's OK + if (endpointKnownFailed.get(endpoint)) + return Never(); + else + return endpointKnownFailed.onChange(endpoint); +} + +FailureStatus SimpleFailureMonitor::getState( Endpoint const& endpoint ) { + if (endpointKnownFailed.get(endpoint)) + return FailureStatus(true); + else { + auto a = addressStatus.find(endpoint.address); + if (a == addressStatus.end()) return FailureStatus(); + else return a->value; + //printf("%s.getState(%s) = %s %p\n", g_network->getLocalAddress().toString(), endpoint.address.toString(), a.failed ? "FAILED" : "OK", this); + } +} + +bool SimpleFailureMonitor::onlyEndpointFailed( Endpoint const& endpoint ) { + if(!endpointKnownFailed.get(endpoint)) + return false; + auto a = addressStatus.find(endpoint.address); + if (a == addressStatus.end()) return true; + else return !a->value.failed; +} + +bool SimpleFailureMonitor::permanentlyFailed( Endpoint const& endpoint ) { + return endpointKnownFailed.get(endpoint); +} + +void SimpleFailureMonitor::reset() { + addressStatus = Map< NetworkAddress, FailureStatus >(); + endpointKnownFailed.resetNoWaiting(); +} diff --git a/fdbrpc/FailureMonitor.h b/fdbrpc/FailureMonitor.h new file mode 100644 index 0000000000..1952265558 --- /dev/null +++ b/fdbrpc/FailureMonitor.h @@ -0,0 +1,144 @@ +/* + * FailureMonitor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_FAILUREMONITOR_H +#define FLOW_FAILUREMONITOR_H +#pragma once + +#include "flow/flow.h" +#include "flow/IndexedSet.h" +#include "FlowTransport.h" // Endpoint + +using std::vector; + +/* + +IFailureMonitor is used by load balancing, data distribution and other components +to report on which other machines are unresponsive or experiencing other failures. +This is vital both to reconfigure the system in response to failures and to prevent +actors from waiting forever for replies from remote machines that are no longer +available. When waiting for a reply, clients should generally stop waiting and +try an alternative server when a failure is reported, rather than relying on timeouts. + +The information tracked for each machine is a FailureStatus, which +for the moment is just a boolean but might be richer in the future. + +Get an IFailureMonitor by calling g_network->failureMonitor(); the simulator keeps +one for each simulated machine and ASIONetwork keeps one for each process. + +The system attempts to ensure that failures are reported quickly, but may occasionally +report a working system as failed temporarily. Clients that intend to take very costly +actions as a result of a failure should probably wait a while to see if a machine becomes +unfailed first. If possible use onFailedFor() which in the future may react to 'permanent' +failures immediately. + +The information reported through this interface is actually supplied by failureMonitorClient, +which exchanges FailureMonitoringRequest/Reply pairs with the failureDetectionServer actor on +the ClusterController. This central repository of failure information has the opportunity +to take into account topology and global network conditions in identifying failures. In +the future it may be augmented with locally available information about failures (e.g. +TCP connection loss in ASIONetwork or unexpectedly long response times for application requests). + +Communications failures are tracked at NetworkAddress granularity. When a request is made to +a missing endpoint on a non-failed machine, this information is reported back to the requesting +machine and tracked at the endpoint level. + +*/ + +struct FailureStatus { + bool failed; + + FailureStatus() : failed(true) {} + explicit FailureStatus(bool failed) : failed(failed) {} + bool isFailed() { return failed; } + bool isAvailable() { return !failed; } + + bool operator == (FailureStatus const& r) const { return failed == r.failed; } + bool operator != (FailureStatus const& r) const { return failed != r.failed; } + template + void serialize(Ar& ar) { + ar & failed; + } +}; + +class IFailureMonitor { +public: + // Returns the currently known status for the endpoint + virtual FailureStatus getState( Endpoint const& endpoint ) = 0; + + // Only use this function when the endpoint is known to be failed + virtual void endpointNotFound( Endpoint const& ) = 0; + + // The next time the known status for the endpoint changes, returns the new status. + virtual Future onStateChanged( Endpoint const& endpoint ) = 0; + + // Returns when onFailed(endpoint) || transport().onDisconnect( endpoint.address ), but more efficiently + virtual Future onDisconnectOrFailure( Endpoint const& endpoint ) = 0; + + // Returns true if the endpoint is failed but the address of the endpoint is not failed. + virtual bool onlyEndpointFailed( Endpoint const& endpoint ) = 0; + + // Returns true if the endpoint will never become available. + virtual bool permanentlyFailed( Endpoint const& endpoint ) = 0; + + // Called by FlowTransport when a connection closes and a prior request or reply might be lost + virtual void notifyDisconnect( NetworkAddress const& ) = 0; + + // Returns when the known status of endpoint is next equal to status. Returns immediately + // if appropriate. + Future onStateEqual( Endpoint const& endpoint, FailureStatus status ); + + // Returns when the status of the given endpoint is next considered "failed" + Future onFailed( Endpoint const& endpoint ) { + return onStateEqual( endpoint, FailureStatus() ); + } + + static IFailureMonitor& failureMonitor() { return *static_cast((void*) g_network->global(INetwork::enFailureMonitor)); } + // Returns the failure monitor that the calling machine should use + + // Returns when the status of the given endpoint has continuously been "failed" for sustainedFailureDuration + (elapsedTime*sustainedFailureSlope) + Future onFailedFor( Endpoint const& endpoint, double sustainedFailureDuration, double sustainedFailureSlope = 0.0 ); +}; + +// SimpleFailureMonitor is the sole implementation of IFailureMonitor. It has no +// failure detection logic; it just implements the interface and reacts to setStatus() etc. +// Initially all addresses are considered failed, but all endpoints of a non-failed address are considered OK. +class SimpleFailureMonitor : public IFailureMonitor { +public: + SimpleFailureMonitor() : endpointKnownFailed() { } + void setStatus( NetworkAddress const& address, FailureStatus const& status ); + void endpointNotFound( Endpoint const& ); + virtual void notifyDisconnect( NetworkAddress const& ); + + virtual Future onStateChanged( Endpoint const& endpoint ); + virtual FailureStatus getState( Endpoint const& endpoint ); + virtual Future onDisconnectOrFailure( Endpoint const& endpoint ); + virtual bool onlyEndpointFailed( Endpoint const& endpoint ); + virtual bool permanentlyFailed( Endpoint const& endpoint ); + + void reset(); +private: + Map< NetworkAddress, FailureStatus > addressStatus; + YieldedAsyncMap< Endpoint, bool > endpointKnownFailed; + + friend class OnStateChangedActorActor; +}; + +#endif \ No newline at end of file diff --git a/fdbrpc/FlowTests.actor.cpp b/fdbrpc/FlowTests.actor.cpp new file mode 100644 index 0000000000..08a15e09eb --- /dev/null +++ b/fdbrpc/FlowTests.actor.cpp @@ -0,0 +1,1194 @@ +/* + * FlowTests.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Unit tests for the flow language and libraries + +#include "flow/actorcompiler.h" +#include "flow/UnitTest.h" +#include "flow/DeterministicRandom.h" +#include "flow/IThreadPool.h" +#include "fdbrpc.h" +#include "IAsyncFile.h" + +void forceLinkFlowTests() {} + +using std::vector; + +template +class LambdaCallback : public CallbackType, public FastAllocated> { + Func func; + ErrFunc errFunc; + + virtual void fire(T const& t) { CallbackType::remove(); func(t); delete this; } + virtual void error(Error e) { CallbackType::remove(); errFunc(e); delete this; } + +public: + LambdaCallback(Func&& f, ErrFunc&& e) : func(std::move(f)), errFunc(std::move(e)) {} +}; + +template +void onReady(Future&& f, Func&& func, ErrFunc&& errFunc) { + if (f.isReady()) { + if (f.isError()) + errFunc(f.getError()); + else + func(f.get()); + } + else + f.addCallbackAndClear(new LambdaCallback>(std::move(func), std::move(errFunc))); +} + +template +void onReady(FutureStream&& f, Func&& func, ErrFunc&& errFunc) { + if (f.isReady()) { + if (f.isError()) + errFunc(f.getError()); + else + func(f.pop()); + } + else + f.addCallbackAndClear(new LambdaCallback>(std::move(func), std::move(errFunc))); +} + +ACTOR static void emptyVoidActor() { +} + +ACTOR static Future emptyActor() { + return Void(); +} + +ACTOR static void oneWaitVoidActor(Future f) { + Void _ = wait(f); +} + +ACTOR static Future oneWaitActor(Future f) { + Void _ = wait(f); + return Void(); +} + +Future g_cheese; +ACTOR static Future cheeseWaitActor() { + Void _ = wait(g_cheese); + return Void(); +} + +ACTOR static void trivialVoidActor(int* result) { + *result = 1; +} + +ACTOR static Future return42Actor() { + return 42; +} + +ACTOR static void voidWaitActor(Future in, int* result) { + int i = wait(in); + *result = i; +} + +ACTOR static Future addOneActor(Future in) { + int i = wait(in); + return i + 1; +} + +ACTOR static Future chooseTwoActor(Future f, Future g) { + choose{ + when(Void _ = wait(f)) {} + when(Void _ = wait(g)) {} + } + return Void(); +} + +ACTOR static Future consumeOneActor(FutureStream in) { + int i = waitNext(in); + return i; +} + +ACTOR static Future sumActor(FutureStream in) { + state int total = 0; + try { + loop{ + int i = waitNext(in); + total += i; + } + } + catch (Error& e) { + if (e.code() != error_code_end_of_stream) + throw; + } + return total; +} + +ACTOR template static Future templateActor(T t) { + return t; +} + +static int destroy() { return 666; } +ACTOR static Future testHygeine() { + ASSERT(destroy() == 666); // Should fail to compile if SAV::destroy() is visible + return Void(); +} + +//bool expectActorCount(int x) { return actorCount == x; } +bool expectActorCount(int) { return true; } + +struct YieldMockNetwork : INetwork, ReferenceCounted { + int ticks; + Promise nextTick; + int nextYield; + INetwork* baseNetwork; + + virtual flowGlobalType global(int id) { return baseNetwork->global(id); } + virtual void setGlobal(size_t id, flowGlobalType v) { baseNetwork->setGlobal(id, v); return; } + + YieldMockNetwork() : ticks(0), nextYield(0) { + baseNetwork = g_network; + g_network = this; + } + ~YieldMockNetwork() { + g_network = baseNetwork; + } + + void tick() { + ticks++; + Promise t; + t.swap(nextTick); + t.send(Void()); + } + + virtual Future delay(double seconds, int taskID) { + return nextTick.getFuture(); + } + + virtual Future yield(int taskID) { + if (check_yield(taskID)) + return delay(0,taskID); + return Void(); + } + + virtual bool check_yield(int taskID) { + if (nextYield > 0) --nextYield; + return nextYield == 0; + } + + // Delegate everything else. TODO: Make a base class NetworkWrapper for delegating everything in INetwork + virtual int getCurrentTask() { return baseNetwork->getCurrentTask(); } + virtual void setCurrentTask(int taskID) { baseNetwork->setCurrentTask(taskID); } + virtual double now() { return baseNetwork->now(); } + virtual void stop() { return baseNetwork->stop(); } + virtual bool isSimulated() const { return baseNetwork->isSimulated(); } + virtual void onMainThread(Promise&& signal, int taskID) { return baseNetwork->onMainThread(std::move(signal), taskID); } + virtual THREAD_HANDLE startThread(THREAD_FUNC_RETURN(*func) (void *), void *arg) { return baseNetwork->startThread(func,arg); } + virtual Future< Reference > open(std::string filename, int64_t flags, int64_t mode) { return IAsyncFileSystem::filesystem()->open(filename,flags,mode); } + virtual Future< Void > deleteFile(std::string filename, bool mustBeDurable) { return IAsyncFileSystem::filesystem()->deleteFile(filename,mustBeDurable); } + virtual void run() { return baseNetwork->run(); } + virtual void getDiskBytes(std::string const& directory, int64_t& free, int64_t& total) { return baseNetwork->getDiskBytes(directory,free,total); } + virtual bool isAddressOnThisHost(NetworkAddress const& addr) { return baseNetwork->isAddressOnThisHost(addr); } +}; + +struct NonserializableThing {}; +ACTOR static Future testNonserializableThing() { + return NonserializableThing(); +} + +ACTOR Future testCancelled(bool *exits, Future f) { + try { + Void _ = wait(Future(Never())); + } catch( Error &e ) { + state Error err = e; + try { + Void _ = wait(Future(Never())); + } catch( Error &e ) { + *exits = true; + throw; + } + throw err; + } + return Void(); +} + +TEST_CASE("flow/flow/cancel1") +{ + bool exits = false; + Promise p; + Future test = testCancelled(&exits, p.getFuture()); + ASSERT(p.getPromiseReferenceCount() == 1 && p.getFutureReferenceCount() == 1); + test.cancel(); + ASSERT(exits); + ASSERT(test.getPromiseReferenceCount() == 0 && test.getFutureReferenceCount() == 1 && test.isReady() && test.isError() && test.getError().code() == error_code_actor_cancelled); + ASSERT(p.getPromiseReferenceCount() == 1 && p.getFutureReferenceCount() == 0); + + + return Void(); +} + +ACTOR static Future noteCancel(int* cancelled) { + *cancelled = 0; + try { + Void _ = wait(Future(Never())); + throw internal_error(); + } + catch (...) { + printf("Cancelled!\n"); + *cancelled = 1; + throw; + } +} + +TEST_CASE("flow/flow/cancel2") +{ + int c1 = 0, c2 = 0, c3 = 0; + + Future cf = noteCancel(&c1); + ASSERT(c1 == 0); + cf = Future(); + ASSERT(c1 == 1); + + cf = noteCancel(&c2) && noteCancel(&c3); + ASSERT(c2 == 0 && c3 == 0); + cf = Future(); + ASSERT(c2 == 1 && c3 == 1); + return Void(); +} + +TEST_CASE("flow/flow/nonserializable futures") +{ + // Types no longer need to be statically serializable to make futures, promises, actors + { + Future f = testNonserializableThing(); + Promise p; + p.send(NonserializableThing()); + f = p.getFuture(); + } + + // But this won't compile + //ReplyPromise rp; + + // ReplyPromise can be used like a normal promise + { + ReplyPromise rpInt; + Future f = rpInt.getFuture(); + ASSERT(!f.isReady()); + rpInt.send(123); + ASSERT(f.get() == 123); + } + + { + RequestStream rsInt; + FutureStream f = rsInt.getFuture(); + rsInt.send(1); + rsInt.send(2); + ASSERT(f.pop() == 1); + ASSERT(f.pop() == 2); + } + + return Void(); +} + +TEST_CASE("flow/flow/networked futures") +{ + // RequestStream can be serialized + { + RequestStream locInt; + BinaryWriter wr(IncludeVersion()); + wr << locInt; + + ASSERT(locInt.getEndpoint().isValid() && locInt.getEndpoint().isLocal() && locInt.getEndpoint().address == FlowTransport::transport().getLocalAddress()); + + BinaryReader rd(wr.toStringRef(), IncludeVersion()); + RequestStream remoteInt; + rd >> remoteInt; + + ASSERT(remoteInt.getEndpoint() == locInt.getEndpoint()); + } + + + // ReplyPromise can be serialized + // TODO: This needs to fiddle with g_currentDeliveryPeerAddress + if (0) { + ReplyPromise locInt; + BinaryWriter wr(IncludeVersion()); + wr << locInt; + + ASSERT(locInt.getEndpoint().isValid() && locInt.getEndpoint().isLocal()); + + BinaryReader rd(wr.toStringRef(), IncludeVersion()); + ReplyPromise remoteInt; + rd >> remoteInt; + + ASSERT(remoteInt.getEndpoint() == locInt.getEndpoint()); + } + + return Void(); +} + +TEST_CASE("flow/flow/quorum") +{ + vector> ps(5); + vector> fs; + vector> qs; + for (auto& p : ps) fs.push_back(p.getFuture()); + + for (int i = 0; i <= ps.size(); i++) + qs.push_back( quorum(fs, i) ); + + for (int i = 0; i < ps.size(); i++) { + ASSERT(qs[i].isReady()); + ASSERT(!qs[i + 1].isReady()); + ps[i].send(i); + } + ASSERT(qs[ps.size()].isReady()); + return Void(); +} + +TEST_CASE("flow/flow/trivial futures") +{ + Future invalid; + ASSERT(!invalid.isValid()); + + Future never = Never(); + ASSERT(never.isValid() && !never.isReady()); + + Future one = 1; + ASSERT(one.isValid() && one.isReady() && !one.isError()); + ASSERT(one.get() == 1); + ASSERT(one.getFutureReferenceCount() == 1); + return Void(); +} + +TEST_CASE("flow/flow/trivial promises") +{ + Future f; + + Promise p; + ASSERT(p.isValid()); + ASSERT(!p.isSet()); + p.send(1); + ASSERT(p.isSet()); + ASSERT(p.getFuture().get() == 1); + + Promise p2; + f = p2.getFuture(); + ASSERT(f.isValid() && !f.isReady()); + p2.send(2); + ASSERT(f.isValid() && f.isReady() && !f.isError()); + ASSERT(f.get() == 2); + + Promise p3; + f = p3.getFuture(); + p3.sendError(end_of_stream()); + ASSERT(f.isValid() && f.isReady() && f.isError()); + ASSERT(f.getError().code() == error_code_end_of_stream); + + Promise p4; + f = p4.getFuture(); + p4 = Promise(); + ASSERT(p4.isValid() && !p4.isSet()); + ASSERT(f.isValid() && f.isReady() && f.isError()); + ASSERT(f.getError().code() == error_code_broken_promise); + return Void(); +} + +TEST_CASE("flow/flow/trivial promisestreams") +{ + FutureStream f; + + PromiseStream p; + p.send(1); + ASSERT(p.getFuture().isReady()); + ASSERT(p.getFuture().pop() == 1); + + PromiseStream p2; + f = p2.getFuture(); + ASSERT(f.isValid() && !f.isReady()); + p2.send(2); + p2.send(3); + ASSERT(f.isValid() && f.isReady() && !f.isError()); + ASSERT(f.pop() == 2); + ASSERT(f.pop() == 3); + + PromiseStream p3; + f = p3.getFuture(); + p3.send(4); + p3.sendError(end_of_stream()); + ASSERT(f.isReady() && !f.isError()); + ASSERT(f.pop() == 4); + ASSERT(f.isError()); + ASSERT(f.getError().code() == error_code_end_of_stream); + + PromiseStream p4; + f = p4.getFuture(); + p4 = PromiseStream(); + ASSERT(f.isValid() && f.isReady() && f.isError()); + ASSERT(f.getError().code() == error_code_broken_promise); + return Void(); +} + +TEST_CASE("flow/flow/callbacks") +{ + Promise p; + Future f = p.getFuture(); + int result = 0; + bool happened = false; + + onReady(std::move(f), [&result](int x) { result = x; }, [&result](Error e) { result = -1; }); + onReady(p.getFuture(), [&happened](int) { happened = true; }, [&happened](Error){ happened = true; }); + ASSERT(!f.isValid()); + ASSERT(p.isValid() && !p.isSet() && p.getFutureReferenceCount()==1); + ASSERT(result == 0 && !happened); + + p.send(123); + ASSERT(result == 123 && happened); + ASSERT(p.isValid() && p.isSet() && p.getFutureReferenceCount() == 0 && p.getFuture().get() == 123); + + result = 0; + onReady(p.getFuture(), [&result](int x) { result = x; }, [&result](Error e) { result = -1; }); + ASSERT(result == 123); + ASSERT(p.isValid() && p.isSet() && p.getFutureReferenceCount() == 0 && p.getFuture().get() == 123); + + p = Promise(); + f = p.getFuture(); + result = 0; + onReady(std::move(f), [&result](int x) { result = x; }, [&result](Error e) { result = -e.code(); }); + ASSERT(!f.isValid()); + ASSERT(p.isValid() && !p.isSet() && p.getFutureReferenceCount() == 1); + ASSERT(result == 0); + + p = Promise(); + ASSERT(result == -error_code_broken_promise); + return Void(); +} + +TEST_CASE("flow/flow/promisestream callbacks") +{ + PromiseStream p; + + int result = 0; + + onReady(p.getFuture(), [&result](int x) { result = x; }, [&result](Error e){ result = -1; }); + + ASSERT(result == 0); + + p.send(123); + p.send(456); + + ASSERT(result == 123); + result = 0; + + onReady(p.getFuture(), [&result](int x) { result = x; }, [&result](Error e){ result = -1; }); + + ASSERT(result == 456); + result = 0; + + onReady(p.getFuture(), [&result](int x) { result = x; }, [&result](Error e){ result = -1; }); + + ASSERT(result == 0); + + p = PromiseStream(); + + ASSERT(result == -1); + return Void(); +} + +//Incompatible with --crash, so we are commenting it out for now +/* +TEST_CASE("flow/flow/promisestream multiple wait error") +{ + state int result = 0; + state PromiseStream p; + try { + onReady(p.getFuture(), [&result](int x) { result = x; }, [&result](Error e){ result = -1; }); + result = 100; + onReady(p.getFuture(), [&result](int x) { result = x; }, [&result](Error e){ result = -1; }); + ASSERT(false); + } + catch (Error& e) { + ASSERT(e.code() == error_code_internal_error); + } + ASSERT(result == 100); + p = PromiseStream(); + ASSERT(result == -1); + return Void(); +} +*/ + +TEST_CASE("flow/flow/trivial actors") +{ + ASSERT(expectActorCount(0)); + + int result = 0; + trivialVoidActor(&result); + ASSERT(result == 1); + ASSERT(expectActorCount(0)); + + Future f = return42Actor(); + ASSERT(f.isReady() && !f.isError() && f.get() == 42 && f.getFutureReferenceCount()==1 && f.getPromiseReferenceCount() == 0); + ASSERT(expectActorCount(1)); + f = Future(); + ASSERT(expectActorCount(0)); + + f = templateActor(24); + ASSERT(f.isReady() && !f.isError() && f.get() == 24 && f.getFutureReferenceCount() == 1 && f.getPromiseReferenceCount() == 0); + ASSERT(expectActorCount(1)); + f = Future(); + ASSERT(expectActorCount(0)); + + result = 0; + voidWaitActor(2, &result); + ASSERT(result == 2 && expectActorCount(0)); + + Promise p; + f = addOneActor(p.getFuture()); + ASSERT(!f.isReady() && expectActorCount(1)); + p.send(100); + ASSERT(f.isReady() && f.get() == 101); + ASSERT(expectActorCount(1)); //< hmm + f = Future(); + ASSERT(expectActorCount(0)); + + PromiseStream ps; + f = consumeOneActor(ps.getFuture()); + ASSERT(!f.isReady() && expectActorCount(1)); + ps.send(101); + ASSERT(f.get() == 101 && ps.isEmpty()); + ps.send(102); + ASSERT(!ps.isEmpty()); + f = consumeOneActor(ps.getFuture()); + ASSERT(f.get() == 102 && ps.isEmpty()); + + f = sumActor(ps.getFuture()); + ps.send(1); + ps.send(10); + ps.send(100); + ps.sendError(end_of_stream()); + ASSERT(f.get() == 111); + + ASSERT( testHygeine().isReady() ); + return Void(); +} + +TEST_CASE("flow/flow/yieldedFuture/progress") +{ + // Check that if check_yield always returns true, the yieldedFuture will do nothing immediately but will + // get one thing done per "tick" (per delay(0) returning). + + Reference yn( new YieldMockNetwork ); + + yn->nextYield = 0; + + Promise p; + Future u = p.getFuture(); + Future i = success(u); + + std::vector> v; + for(int i=0; i<5; i++) + v.push_back(yieldedFuture(u)); + auto numReady = [&v]() { + return std::count_if(v.begin(), v.end(), [](Future v) { return v.isReady(); }); + }; + + ASSERT( numReady()==0 ); + p.send(Void()); + ASSERT( u.isReady() && i.isReady() && numReady()==0 ); + + for(int i=0; i<5; i++) { + yn->tick(); + ASSERT( numReady() == i+1 ); + } + + for(int i=0; i<5; i++) { + ASSERT( v[i].getPromiseReferenceCount() == 0 && v[i].getFutureReferenceCount() == 1 ); + } + + return Void(); +} + +TEST_CASE("flow/flow/yieldedFuture/random") +{ + // Check expectations about exactly how yieldedFuture responds to check_yield results + + Reference yn( new YieldMockNetwork ); + + for(int r=0; r<100; r++) { + Promise p; + Future u = p.getFuture(); + Future i = success(u); + + std::vector> v; + for(int i=0; i<25; i++) + v.push_back(yieldedFuture(u)); + auto numReady = [&v]() { + return std::count_if(v.begin(), v.end(), [](Future v) { return v.isReady(); }); + }; + + Future j = success(u); + + ASSERT( numReady()==0 ); + + int expectYield = g_random->randomInt(0, 4); + int expectReady = expectYield; + yn->nextYield = 1 + expectYield; + + p.send(Void()); + ASSERT( u.isReady() && i.isReady() && j.isReady() && numReady()==expectReady ); + + while (numReady() != v.size()) { + expectYield = g_random->randomInt(0, 4); + yn->nextYield = 1 + expectYield; + expectReady += 1 + expectYield; + yn->tick(); + //printf("Yielding %d times, expect %d/%d ready, got %d\n", expectYield, expectReady, v.size(), numReady() ); + ASSERT( numReady() == std::min(expectReady, v.size()) ); + } + + for(int i=0; i yn( new YieldMockNetwork ); + + yn->nextYield = 2*N + 100; + + Promise p; + Future f = p.getFuture(); + vector> ys; + + start = timer(); + for (int i = 0; i < N; i++) + ys.push_back(yieldedFuture(f)); + printf("yieldedFuture(f) create: %0.1f M/sec\n", N / 1e6 / (timer() - start)); + p.send(Void()); + printf("yieldedFuture(f) total: %0.1f M/sec\n", N / 1e6 / (timer() - start)); + + for (auto& y : ys) + ASSERT(y.isReady()); + + p = Promise(); + f = p.getFuture(); + + start = timer(); + for (int i = 0; i < N; i++) + yieldedFuture(f); + printf("yieldedFuture(f) cancel: %0.1f M/sec\n", N / 1e6 / (timer() - start)); + + return Void(); +} + +TEST_CASE("flow/flow/chooseTwoActor") +{ + ASSERT(expectActorCount(0)); + + Promise a, b; + Future c = chooseTwoActor(a.getFuture(), b.getFuture()); + ASSERT(a.getFutureReferenceCount()==2 && b.getFutureReferenceCount()==2 && !c.isReady()); + b.send(Void()); + ASSERT(a.getFutureReferenceCount() == 0 && b.getFutureReferenceCount() == 0 && c.isReady() && !c.isError() && expectActorCount(1)); + c = Future(); + ASSERT(a.getFutureReferenceCount() == 0 && b.getFutureReferenceCount() == 0 && expectActorCount(0)); + return Void(); +} + +TEST_CASE("flow/flow/perf/actor patterns") +{ + double start; + int N = 1000000; + + ASSERT(expectActorCount(0)); + + start = timer(); + for (int i = 0; i < N; i++) + emptyVoidActor(); + printf("emptyVoidActor(): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + + ASSERT(expectActorCount(0)); + + start = timer(); + for (int i = 0; i < N; i++) { + emptyActor(); + } + printf("emptyActor(): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + + ASSERT(expectActorCount(0)); + + Promise neverSet; + Future never = neverSet.getFuture(); + Future already = Void(); + + start = timer(); + for (int i = 0; i < N; i++) + oneWaitVoidActor(already); + printf("oneWaitVoidActor(already): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + + ASSERT(expectActorCount(0)); + + /*start = timer(); + for (int i = 0; i < N; i++) + oneWaitVoidActor(never); + printf("oneWaitVoidActor(never): %0.1f M/sec\n", N / 1e6 / (timer() - start));*/ + + { + start = timer(); + for (int i = 0; i < N; i++) { + Future f = oneWaitActor(already); + ASSERT(f.isReady()); + } + printf("oneWaitActor(already): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + start = timer(); + for (int i = 0; i < N; i++) { + Future f = oneWaitActor(never); + ASSERT(!f.isReady()); + } + printf("(cancelled) oneWaitActor(never): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + ASSERT(expectActorCount(0)); + } + + { + start = timer(); + for (int i = 0; i < N; i++) { + Promise p; + Future f = oneWaitActor(p.getFuture()); + p.send(Void()); + ASSERT(f.isReady()); + } + printf("oneWaitActor(after): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + vector> pipe(N); + vector> out(N); + start = timer(); + for (int i = 0; i < N; i++) { + out[i] = oneWaitActor(pipe[i].getFuture()); + } + for (int i = 0; i < N; i++) { + pipe[i].send(Void()); + ASSERT(out[i].isReady()); + } + printf("oneWaitActor(fifo): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + vector> pipe(N); + vector> out(N); + start = timer(); + for (int i = 0; i < N; i++) { + out[i] = oneWaitActor(pipe[i].getFuture()); + } + for (int i = N - 1; i >= 0; i--) { + pipe[i].send(Void()); + ASSERT(out[i].isReady()); + } + printf("oneWaitActor(lifo): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + start = timer(); + for (int i = 0; i < N; i++) { + Future f = chooseTwoActor(already, already); + ASSERT(f.isReady()); + } + printf("chooseTwoActor(already, already): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + start = timer(); + for (int i = 0; i < N; i++) { + Future f = chooseTwoActor(already, never); + ASSERT(f.isReady()); + } + printf("chooseTwoActor(already, never): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + start = timer(); + for (int i = 0; i < N; i++) { + Future f = chooseTwoActor(never, already); + ASSERT(f.isReady()); + } + printf("chooseTwoActor(never, already): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + start = timer(); + for (int i = 0; i < N; i++) { + Future f = chooseTwoActor(never, never); + ASSERT(!f.isReady()); + } + //ASSERT(expectActorCount(0)); + printf("(cancelled) chooseTwoActor(never, never): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + start = timer(); + for (int i = 0; i < N; i++) { + Promise p; + Future f = chooseTwoActor(p.getFuture(), never); + p.send(Void()); + ASSERT(f.isReady()); + } + printf("chooseTwoActor(after, never): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + vector> pipe(N); + vector> out(N); + start = timer(); + for (int i = 0; i < N; i++) { + out[i] = chooseTwoActor(pipe[i].getFuture(), never); + } + for (int i = 0; i < N; i++) { + pipe[i].send(Void()); + ASSERT(out[i].isReady()); + } + printf("chooseTwoActor(fifo, never): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + vector> pipe(N); + vector> out(N); + start = timer(); + for (int i = 0; i < N; i++) { + out[i] = chooseTwoActor(pipe[i].getFuture(), pipe[i].getFuture()); + } + for (int i = 0; i < N; i++) { + pipe[i].send(Void()); + ASSERT(out[i].isReady()); + } + printf("chooseTwoActor(fifo, fifo): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + vector> pipe(N); + vector> out(N); + start = timer(); + for (int i = 0; i < N; i++) { + out[i] = chooseTwoActor(chooseTwoActor(pipe[i].getFuture(), never), never); + } + for (int i = 0; i < N; i++) { + pipe[i].send(Void()); + ASSERT(out[i].isReady()); + } + printf("chooseTwoActor^2((fifo, never), never): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + start = timer(); + for (int i = 0; i < N; i++) { + Promise p; + Future f = oneWaitActor(chooseTwoActor(p.getFuture(), never)); + p.send(Void()); + ASSERT(f.isReady()); + } + printf("oneWaitActor(chooseTwoActor(after, never)): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + vector> pipe(N); + vector> out(N); + start = timer(); + for (int i = 0; i < N; i++) { + out[i] = oneWaitActor(chooseTwoActor(pipe[i].getFuture(), never)); + } + for (int i = 0; i < N; i++) { + pipe[i].send(Void()); + ASSERT(out[i].isReady()); + } + printf("oneWaitActor(chooseTwoActor(fifo, never)): %0.1f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + start = timer(); + for (int i = 0; i < N; i++) { + Promise p; + Future f = chooseTwoActor(p.getFuture(), never); + Future a = oneWaitActor(f); + Future b = oneWaitActor(f); + p.send(Void()); + ASSERT(f.isReady()); + } + printf("2xoneWaitActor(chooseTwoActor(after, never)): %0.2f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + vector> pipe(N); + vector> out1(N); + vector> out2(N); + start = timer(); + for (int i = 0; i < N; i++) { + Future f = chooseTwoActor(pipe[i].getFuture(), never); + out1[i] = oneWaitActor(f); + out2[i] = oneWaitActor(f); + } + for (int i = 0; i < N; i++) { + pipe[i].send(Void()); + ASSERT(out2[i].isReady()); + } + printf("2xoneWaitActor(chooseTwoActor(fifo, never)): %0.2f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + vector> pipe(N); + vector> out1(N); + vector> out2(N); + start = timer(); + for (int i = 0; i < N; i++) { + Future f = chooseTwoActor(oneWaitActor(pipe[i].getFuture()), never); + out1[i] = oneWaitActor(f); + out2[i] = oneWaitActor(f); + } + for (int i = 0; i < N; i++) { + pipe[i].send(Void()); + ASSERT(out2[i].isReady()); + } + printf("2xoneWaitActor(chooseTwoActor(oneWaitActor(fifo), never)): %0.2f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + vector> pipe(N); + vector> out1(N); + vector> out2(N); + start = timer(); + for (int i = 0; i < N; i++) { + g_cheese = pipe[i].getFuture(); + Future f = chooseTwoActor(cheeseWaitActor(), never); + g_cheese = f; + out1[i] = cheeseWaitActor(); + out2[i] = cheeseWaitActor(); + } + for (int i = 0; i < N; i++) { + pipe[i].send(Void()); + ASSERT(out2[i].isReady()); + } + printf("2xcheeseActor(chooseTwoActor(cheeseActor(fifo), never)): %0.2f M/sec\n", N / 1e6 / (timer() - start)); + printf("sizeof(CheeseWaitActorActor) == %d\n", sizeof(CheeseWaitActorActor)); + } + + { + PromiseStream data; + start = timer(); + Future sum = sumActor(data.getFuture()); + for (int i = 0; i < N; i++) + data.send(1); + data.sendError(end_of_stream()); + ASSERT(sum.get() == N); + printf("sumActor: %0.2f M/sec\n", N / 1e6 / (timer() - start)); + } + + { + start = timer(); + vector> ps(3); + vector> fs(3); + + for (int i = 0; i < N; i++) { + ps.clear(); + ps.resize(3); + for (int j = 0; j < ps.size(); j++) + fs[j] = ps[j].getFuture(); + + Future q = quorum(fs, 2); + for (auto& p : ps) p.send(Void()); + } + printf("quorum(2/3): %0.2f M/sec\n", N / 1e6 / (timer() - start)); + } + + return Void(); +} + +template +struct YAMRandom { + YAM yam; + std::vector> onchanges; + int kmax; + + YAMRandom() : kmax(3) {} + + void randomOp() { + if (g_random->random01() < 0.01) + while (!check_yield()); + + int k = g_random->randomInt(0, kmax); + int op = g_random->randomInt(0, 7); + //printf("%d",op); + if (op == 0) { + onchanges.push_back(yam.onChange(k)); + } else if (op == 1) { + onchanges.push_back( trigger([this](){ this->randomOp(); }, yam.onChange(k)) ); + } else if (op == 2) { + if (onchanges.size()) { + int i = g_random->randomInt(0, onchanges.size()); + onchanges[i] = onchanges.back(); + onchanges.pop_back(); + } + } else if (op == 3) { + onchanges.clear(); + } else if (op == 4) { + int v = g_random->randomInt(0, 3); + yam.set(k, v); + } else if (op == 5) { + yam.trigger(k); + } else if (op == 6) { + int a = g_random->randomInt(0, kmax); + int b = g_random->randomInt(0, kmax); + yam.triggerRange(std::min(a,b), std::max(a,b)+1); + } + } +}; + +TEST_CASE("flow/flow/YieldedAsyncMap/randomized") +{ + state YAMRandom> yamr; + state int it; + for (it = 0; it < 100000; it++) { + yamr.randomOp(); + Void _ = wait(yield()); + } + return Void(); +} + +TEST_CASE("flow/flow/AsyncMap/randomized") +{ + state YAMRandom> yamr; + state int it; + for (it = 0; it < 100000; it++) { + yamr.randomOp(); + Void _ = wait(yield()); + } + return Void(); +} + +TEST_CASE("flow/flow/YieldedAsyncMap/basic") +{ + state YieldedAsyncMap yam; + state Future y0 = yam.onChange(1); + yam.setUnconditional(1, 0); + state Future y1 = yam.onChange(1); + state Future y1a = yam.onChange(1); + state Future y1b = yam.onChange(1); + yam.set(1, 1); + //while (!check_yield()) {} + //yam.triggerRange(0, 4); + + state Future y2 = yam.onChange(1); + Void _ = wait(reportErrors(y0, "y0")); + Void _ = wait(reportErrors(y1, "y1")); + Void _ = wait(reportErrors(y1a, "y1a")); + Void _ = wait(reportErrors(y1b, "y1b")); + Void _ = wait(reportErrors(timeout(y2, 5, Void()), "y2")); + + return Void(); +} + +TEST_CASE("flow/flow/YieldedAsyncMap/cancel") +{ + state YieldedAsyncMap yam; + //ASSERT(yam.count(1) == 0); + //state Future y0 = yam.onChange(1); + //ASSERT(yam.count(1) == 1); + //yam.setUnconditional(1, 0); + + ASSERT(yam.count(1) == 0); + state Future y1 = yam.onChange(1); + state Future y1a = yam.onChange(1); + state Future y1b = yam.onChange(1); + ASSERT(yam.count(1) == 1); + y1.cancel(); + ASSERT(!y1a.isReady()); + y1a.cancel(); + ASSERT(!y1b.isReady()); + ASSERT(yam.count(1) == 1); + y1b.cancel(); + ASSERT(y1b.getError().code() == error_code_actor_cancelled); + ASSERT(yam.count(1) == 0); + + return Void(); +} + +TEST_CASE("flow/flow/YieldedAsyncMap/cancel2") +{ + state YieldedAsyncMap yam; + + state Future y1 = yam.onChange(1); + state Future y2 = yam.onChange(2); + + auto* pyam = &yam; + uncancellable(trigger( + [pyam](){ + printf("Triggered\n"); + pyam->triggerAll(); + }, + delay(1))); + + Void _ = wait(y1); + printf("Got y1\n"); + y2.cancel(); + + return Void(); +} + +TEST_CASE("flow/flow/AsyncVar/basic") +{ + AsyncVar av; + Future ch = av.onChange(); + ASSERT(!ch.isReady()); + av.set(5); + ASSERT(ch.isReady()); + ASSERT(av.get() == 5); + + ch = av.onChange(); + ASSERT(!ch.isReady()); + av.set(6); + ASSERT(ch.isReady()); + ASSERT(av.get() == 6); + + return Void(); +} + +ACTOR static Future waitAfterCancel( int* output ) { + *output = 0; + try { + Void _ = wait( Never() ); + } catch (...) { + Void _ = wait( (*output=1, Future(Void())) ); + } + ASSERT(false); + return Void(); +} + +TEST_CASE("fdbrpc/flow/wait_expression_after_cancel") +{ + int a = -1; + Future f = waitAfterCancel(&a); + ASSERT( a == 0 ); + f.cancel(); + ASSERT( a == 1 ); + return Void(); +} \ No newline at end of file diff --git a/fdbrpc/FlowTransport.actor.cpp b/fdbrpc/FlowTransport.actor.cpp new file mode 100644 index 0000000000..786c805215 --- /dev/null +++ b/fdbrpc/FlowTransport.actor.cpp @@ -0,0 +1,860 @@ +/* + * FlowTransport.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "FlowTransport.h" +#include "genericactors.actor.h" +#include "fdbrpc.h" +#include "flow/Net2Packet.h" +#include "flow/ActorCollection.h" +#include "flow/TDMetric.actor.h" +#include "FailureMonitor.h" +#include "crc32c.h" +#include "simulator.h" + +static NetworkAddress g_currentDeliveryPeerAddress; + +const UID WLTOKEN_ENDPOINT_NOT_FOUND(-1, 0); +const UID WLTOKEN_PING_PACKET(-1, 1); +const UID TOKEN_IGNORE_PACKET(0, 2); +const uint64_t TOKEN_STREAM_FLAG = 1; + + +class EndpointMap : NonCopyable { +public: + EndpointMap(); + void insert( NetworkMessageReceiver* r, Endpoint::Token& token, uint32_t priority ); + NetworkMessageReceiver* get( Endpoint::Token const& token ); + uint32_t getPriority( Endpoint::Token const& token ); + void remove( Endpoint::Token const& token, NetworkMessageReceiver* r ); + +private: + void realloc(); + + struct Entry { + union { + uint64_t uid[2]; // priority packed into lower 32 bits; actual lower 32 bits of token are the index in data[] + uint32_t nextFree; + }; + NetworkMessageReceiver* receiver; + Endpoint::Token& token() { return *(Endpoint::Token*)uid; } + }; + std::vector data; + uint32_t firstFree; +}; + +EndpointMap::EndpointMap() + : firstFree(-1) +{ +} + +void EndpointMap::realloc() { + int oldSize = data.size(); + data.resize( std::max(128, oldSize*2) ); + for(int i=oldSize; i> e; + IFailureMonitor::failureMonitor().endpointNotFound(e); + } +}; + +struct PingReceiver : NetworkMessageReceiver { + PingReceiver(EndpointMap& endpoints) { + Endpoint::Token e = WLTOKEN_PING_PACKET; + endpoints.insert(this, e, TaskReadSocket); + ASSERT( e == WLTOKEN_PING_PACKET ); + } + virtual void receive( ArenaReader& reader ) { + ReplyPromise reply; reader >> reply; + reply.send(Void()); + } +}; + +class TransportData { +public: + TransportData(uint64_t transportId) + : endpointNotFoundReceiver(endpoints), + pingReceiver(endpoints), + warnAlwaysForLargePacket(true), + lastIncompatibleMessage(0), + transportId(transportId) + {} + + void initMetrics() { + bytesSent.init(LiteralStringRef("Net2.BytesSent")); + countPacketsReceived.init(LiteralStringRef("Net2.CountPacketsReceived")); + countPacketsGenerated.init(LiteralStringRef("Net2.CountPacketsGenerated")); + countConnEstablished.init(LiteralStringRef("Net2.CountConnEstablished")); + countConnClosedWithError.init(LiteralStringRef("Net2.CountConnClosedWithError")); + countConnClosedWithoutError.init(LiteralStringRef("Net2.CountConnClosedWithoutError")); + } + + struct Peer* getPeer( NetworkAddress const& address, bool doConnect = true ); + + NetworkAddress localAddress; + std::map peers; + Future listen; + bool warnAlwaysForLargePacket; + + // These declarations must be in exactly this order + EndpointMap endpoints; + EndpointNotFoundReceiver endpointNotFoundReceiver; + PingReceiver pingReceiver; + // End ordered declarations + + Int64MetricHandle bytesSent; + Int64MetricHandle countPacketsReceived; + Int64MetricHandle countPacketsGenerated; + Int64MetricHandle countConnEstablished; + Int64MetricHandle countConnClosedWithError; + Int64MetricHandle countConnClosedWithoutError; + + std::map> incompatiblePeers; + std::map multiVersionConnections; + double lastIncompatibleMessage; + uint64_t transportId; + + Future multiVersionCleanup; +}; + +#define CONNECT_PACKET_V0 0x0FDB00A444020001LL +#define CONNECT_PACKET_V1 0x0FDB00A446030001LL +#define CONNECT_PACKET_V0_SIZE 14 +#define CONNECT_PACKET_V1_SIZE 22 +#define CONNECT_PACKET_V2_SIZE 26 + +#pragma pack( push, 1 ) +struct ConnectPacket { + uint32_t connectPacketLength; // sizeof(ConnectPacket)-sizeof(uint32_t), or perhaps greater in later protocol versions + uint64_t protocolVersion; // Expect currentProtocolVersion + uint16_t canonicalRemotePort; // Port number to reconnect to the originating process + uint64_t connectionId; // Multi-version clients will use the same Id for both connections, other connections will set this to zero. Added at protocol Version 0x0FDB00A444020001. + uint32_t canonicalRemoteIp; // IP Address to reconnect to the originating process + + size_t minimumSize() { + if (protocolVersion < CONNECT_PACKET_V0) return CONNECT_PACKET_V0_SIZE; + if (protocolVersion < CONNECT_PACKET_V1) return CONNECT_PACKET_V1_SIZE; + return CONNECT_PACKET_V2_SIZE; + } +}; + +static_assert( sizeof(ConnectPacket) == CONNECT_PACKET_V2_SIZE, "ConnectPacket packed incorrectly" ); +#pragma pack( pop ) + +static Future connectionReader( TransportData* const& transport, Reference const& conn, bool const& isOutgoing, Promise const& onPeerAddress ); + +struct Peer : NonCopyable { + // FIXME: Peers don't die! + + TransportData* transport; + NetworkAddress destination; + UnsentPacketQueue unsent; + ReliablePacketList reliable; + AsyncTrigger dataToSend; // Triggered when unsent.empty() becomes false + Future connect; + bool outgoingConnectionIdle; // We don't actually have a connection open and aren't trying to open one because we don't have anything to send + double lastConnectTime; + double reconnectionDelay; + + explicit Peer( TransportData* transport, NetworkAddress const& destination, bool doConnect = true ) + : transport(transport), destination(destination), outgoingConnectionIdle(!doConnect), lastConnectTime(0.0), reconnectionDelay(FLOW_KNOBS->INITIAL_RECONNECTION_TIME) + { + if(doConnect) { + connect = connectionKeeper(this); + } + } + + void send(PacketBuffer* pb, ReliablePacket* rp, bool firstUnsent) { + unsent.setWriteBuffer(pb); + if (rp) reliable.insert(rp); + if (firstUnsent) dataToSend.trigger(); + } + + void prependConnectPacket() { + // Send the ConnectPacket expected at the beginning of a new connection + ConnectPacket pkt; + if (transport->localAddress.isTLS() != destination.isTLS()) { + pkt.canonicalRemotePort = 0; // a "mixed" TLS/non-TLS connection is like a client/server connection - there's no way to reverse it + pkt.canonicalRemoteIp = 0; + } + else { + pkt.canonicalRemotePort = transport->localAddress.port; + pkt.canonicalRemoteIp = transport->localAddress.ip; + } + pkt.connectPacketLength = sizeof(pkt)-sizeof(pkt.connectPacketLength); + pkt.protocolVersion = currentProtocolVersion; + pkt.connectionId = transport->transportId; + + PacketBuffer* pb_first = new PacketBuffer; + PacketWriter wr( pb_first, NULL, Unversioned() ); + wr.serializeBinaryItem(pkt); + unsent.prependWriteBuffer(pb_first, wr.finish()); + } + + void discardUnreliablePackets() { + // Throw away the current unsent list, dropping the reference count on each PacketBuffer that accounts for presence in the unsent list + unsent.discardAll(); + + // Compact reliable packets into a new unsent range + PacketBuffer* pb = unsent.getWriteBuffer(); + pb = reliable.compact(pb, NULL); + unsent.setWriteBuffer(pb); + } + + void onIncomingConnection( Reference conn, Future reader ) { + // In case two processes are trying to connect to each other simultaneously, the process with the larger canonical NetworkAddress + // gets to keep its outgoing connection. + if ( !destination.isPublic() && !outgoingConnectionIdle ) throw address_in_use(); + if ( !destination.isPublic() || outgoingConnectionIdle || destination > transport->localAddress ) { + // Keep the new connection + TraceEvent("IncomingConnection", conn->getDebugID()) + .detail("FromAddr", conn->getPeerAddress()) + .detail("CanonicalAddr", destination) + .detail("IsPublic", destination.isPublic()); + + connect.cancel(); + prependConnectPacket(); + connect = connectionKeeper( this, conn, reader ); + } else { + TraceEvent("RedundantConnection", conn->getDebugID()) + .detail("FromAddr", conn->getPeerAddress().toString()) + .detail("CanonicalAddr", destination); + + // Keep our prior connection + reader.cancel(); + conn->close(); + + // Send an (ignored) packet to make sure that, if our outgoing connection died before the peer made this connection attempt, + // we eventually find out that our connection is dead, close it, and then respond to the next connection reattempt from peer. + //sendPacket( self, SerializeSourceRaw(StringRef()), Endpoint(peer->address(), TOKEN_IGNORE_PACKET), false ); + } + } + + ACTOR static Future connectionMonitor( Peer* peer ) { + + state RequestStream< ReplyPromise > remotePing( Endpoint( peer->destination, WLTOKEN_PING_PACKET ) ); + + loop { + Void _ = wait( delayJittered( FLOW_KNOBS->CONNECTION_MONITOR_LOOP_TIME ) ); + + // SOMEDAY: Stop monitoring and close the connection after a long period of inactivity with no reliable or onDisconnect requests outstanding + + state ReplyPromise reply; + FlowTransport::transport().sendUnreliable( SerializeSource>(reply), remotePing.getEndpoint() ); + + choose { + when (Void _ = wait( delay( FLOW_KNOBS->CONNECTION_MONITOR_TIMEOUT ) )) { TraceEvent("ConnectionTimeout").detail("WithAddr", peer->destination); throw connection_failed(); } + when (Void _ = wait( reply.getFuture() )) {} + } + } + } + + ACTOR static Future connectionWriter( Peer* self, Reference conn ) { + state double lastWriteTime = now(); + loop { + //Void _ = wait( delay(0, TaskWriteSocket) ); + Void _ = wait( delayJittered(std::max(FLOW_KNOBS->MIN_COALESCE_DELAY, FLOW_KNOBS->MAX_COALESCE_DELAY - (now() - lastWriteTime)), TaskWriteSocket) ); + //Void _ = wait( delay(500e-6, TaskWriteSocket) ); + //Void _ = wait( yield(TaskWriteSocket) ); + + // Send until there is nothing left to send + loop { + lastWriteTime = now(); + + int sent = conn->write( self->unsent.getUnsent() ); + if (sent) { + self->transport->bytesSent += sent; + self->unsent.sent(sent); + } + if (self->unsent.empty()) break; + + TEST(true); // We didn't write everything, so apparently the write buffer is full. Wait for it to be nonfull. + Void _ = wait( conn->onWritable() ); + Void _ = wait( yield(TaskWriteSocket) ); + } + + // Wait until there is something to send + while ( self->unsent.empty() ) + Void _ = wait( self->dataToSend.onTrigger() ); + } + } + + ACTOR static Future connectionKeeper( Peer* self, + Reference conn = Reference(), + Future reader = Void()) { + TraceEvent(SevDebug, "ConnKeeper", conn ? conn->getDebugID() : UID()) + .detail("PeerAddr", self->destination) + .detail("ConnSet", (bool)conn); + loop { + try { + if (!conn) { // Always, except for the first loop with an incoming connection + self->outgoingConnectionIdle = true; + // Wait until there is something to send + while ( self->unsent.empty() ) + Void _ = wait( self->dataToSend.onTrigger() ); + ASSERT( self->destination.isPublic() ); + self->outgoingConnectionIdle = false; + Void _ = wait( delayJittered( std::max(0.0, self->lastConnectTime+self->reconnectionDelay - now()) ) ); // Don't connect() to the same peer more than once per 2 sec + self->lastConnectTime = now(); + + TraceEvent("ConnectingTo", conn ? conn->getDebugID() : UID()).detail("PeerAddr", self->destination); + Reference _conn = wait( timeout( INetworkConnections::net()->connect(self->destination), FLOW_KNOBS->CONNECTION_MONITOR_TIMEOUT, Reference() ) ); + if (_conn) { + conn = _conn; + TraceEvent("ConnEstablishedTo", conn->getDebugID()).detail("PeerAddr", self->destination); + self->prependConnectPacket(); + } else { + TraceEvent("ConnTimedOut", conn ? conn->getDebugID() : UID()).detail("PeerAddr", self->destination); + throw connection_failed(); + } + + reader = connectionReader( self->transport, conn, true, Promise() ); + } else { + self->outgoingConnectionIdle = false; + } + self->transport->countConnEstablished++; + + Void _ = wait( connectionWriter( self, conn ) || reader || connectionMonitor(self) ); + + ASSERT( false ); + } catch (Error& e) { + if(now() - self->lastConnectTime > FLOW_KNOBS->RECONNECTION_RESET_TIME) { + self->reconnectionDelay = FLOW_KNOBS->INITIAL_RECONNECTION_TIME; + } else { + self->reconnectionDelay = std::min(FLOW_KNOBS->MAX_RECONNECTION_TIME, self->reconnectionDelay * FLOW_KNOBS->RECONNECTION_TIME_GROWTH_RATE); + } + self->discardUnreliablePackets(); + reader = Future(); + bool ok = e.code() == error_code_connection_failed || e.code() == error_code_actor_cancelled || ( g_network->isSimulated() && e.code() == error_code_checksum_failed ); + TraceEvent(ok ? SevInfo : SevError, "ConnectionClosed", conn ? conn->getDebugID() : UID()).detail("PeerAddr", self->destination).error(e, true); + + if (ok) + self->transport->countConnClosedWithoutError++; + else + self->transport->countConnClosedWithError++; + + if (conn) { + conn->close(); + conn = Reference(); + } + IFailureMonitor::failureMonitor().notifyDisconnect( self->destination ); //< Clients might send more packets in response, which needs to go out on the next connection + if (e.code() == error_code_actor_cancelled) throw; + // Try to recover, even from serious errors, by retrying + } + } + } +}; + +static PacketID sendPacket( TransportData* self, ISerializeSource const& what, const Endpoint& destination, bool reliable ); + +ACTOR static void deliver( TransportData* self, Endpoint destination, ArenaReader reader, bool inReadSocket ) { + int priority = self->endpoints.getPriority(destination.token); + if (priority < TaskReadSocket || !inReadSocket) { + Void _ = wait( delay(0, priority) ); + } else { + g_network->setCurrentTask( priority ); + } + + auto receiver = self->endpoints.get(destination.token); + if (receiver) { + try { + g_currentDeliveryPeerAddress = destination.address; + receiver->receive( reader ); + g_currentDeliveryPeerAddress = NetworkAddress(); + } catch (Error& e) { + g_currentDeliveryPeerAddress = NetworkAddress(); + TraceEvent(SevError, "ReceiverError").error(e).detail("Token", destination.token.toString()).detail("Peer", destination.address); + throw; + } + } else if (destination.token.first() & TOKEN_STREAM_FLAG) { + // We don't have the (stream) endpoint 'token', notify the remote machine + if (destination.token.first() != -1) + sendPacket( self, + SerializeSource( Endpoint( self->localAddress, destination.token ) ), + Endpoint( destination.address, WLTOKEN_ENDPOINT_NOT_FOUND), + false ); + } + + if( inReadSocket ) + g_network->setCurrentTask( TaskReadSocket ); +} + +static void scanPackets( TransportData* transport, uint8_t*& unprocessed_begin, uint8_t* e, Arena& arena, NetworkAddress const& peerAddress, uint64_t peerProtocolVersion ) { + // Find each complete packet in the given byte range and queue a ready task to deliver it. + // Remove the complete packets from the range by increasing unprocessed_begin. + // There won't be more than 64K of data plus one packet, so this shouldn't take a long time. + uint8_t* p = unprocessed_begin; + loop { + if (e-p < sizeof(uint32_t) * 2) break; + + //Retrieve packet length and checksum + uint32_t packetLen = *(uint32_t*)p; p += sizeof(uint32_t); + uint32_t packetChecksum = *(uint32_t*)p; p += sizeof(uint32_t); + if (packetLen > FLOW_KNOBS->PACKET_LIMIT) { + TraceEvent(SevError, "Net2_PacketLimitExceeded").detail("FromPeer", peerAddress.toString()).detail("Length", (int)packetLen); + throw platform_error(); + } + else if (packetLen > FLOW_KNOBS->PACKET_WARNING) { + TraceEvent(transport->warnAlwaysForLargePacket ? SevWarnAlways : SevWarn, "Net2_LargePacket") + .detail("FromPeer", peerAddress.toString()) + .detail("Length", (int)packetLen) + .suppressFor(1.0); + + if(g_network->isSimulated()) + transport->warnAlwaysForLargePacket = false; + } + + if (e-p= sizeof(UID) ); + + bool isBuggifyEnabled = false; + if(g_network->isSimulated() && g_simulator.enableConnectionFailures && BUGGIFY_WITH_PROB(0.001)) { + isBuggifyEnabled = true; + TraceEvent(SevInfo, "BitsFlip"); + int flipBits = 32 - (int) floor(log2(g_random->randomUInt32())); + + uint32_t firstFlipByteLocation = g_random->randomUInt32() % packetLen; + int firstFlipBitLocation = g_random->randomInt(0, 8); + *(p + firstFlipByteLocation) ^= 1 << firstFlipBitLocation; + flipBits--; + + for (int i = 0; i < flipBits; i++) { + uint32_t byteLocation = g_random->randomUInt32() % packetLen; + int bitLocation = g_random->randomInt(0, 8); + if (byteLocation != firstFlipByteLocation || bitLocation != firstFlipBitLocation) { + *(p + byteLocation) ^= 1 << bitLocation; + } + } + } + + uint32_t calculatedChecksum = crc32c_append(0, p, packetLen); + if (calculatedChecksum != packetChecksum) { + if (isBuggifyEnabled) { + TraceEvent(SevInfo, "ChecksumMismatchExp").detail("packetChecksum", (int)packetChecksum).detail("calculatedChecksum", (int)calculatedChecksum); + } else { + TraceEvent(SevWarnAlways, "ChecksumMismatchUnexp").detail("packetChecksum", (int)packetChecksum).detail("calculatedChecksum", (int)calculatedChecksum); + } + throw checksum_failed(); + } else { + if (isBuggifyEnabled) { + TraceEvent(SevError, "ChecksumMatchUnexp").detail("packetChecksum", (int)packetChecksum).detail("calculatedChecksum", (int)calculatedChecksum); + } + } + + ArenaReader reader( arena, StringRef(p, packetLen), AssumeVersion(peerProtocolVersion) ); + UID token; reader >> token; + + ++transport->countPacketsReceived; + + deliver( transport, Endpoint( peerAddress, token ), std::move(reader), true ); + + unprocessed_begin = p = p + packetLen; + } +} + +ACTOR static Future connectionReader( + TransportData* transport, + Reference conn, + bool isOutgoing, Promise onPeerAddress ) { + // This actor exists whenever there is an open or opening connection, whether incoming or outgoing + // For incoming connections conn is set and peer is initially NULL; for outgoing connections it is the reverse + + state Arena arena; + state uint8_t* unprocessed_begin = NULL; + state uint8_t* unprocessed_end = NULL; + state uint8_t* buffer_end = NULL; + state bool expectConnectPacket = true; + state NetworkAddress peerAddress; + state uint64_t peerProtocolVersion = 0; + + peerAddress = conn->getPeerAddress(); + if (!isOutgoing) + ASSERT( !peerAddress.isPublic() ); + + loop { + loop { + int readAllBytes = buffer_end - unprocessed_end; + if (readAllBytes < 4096) { + Arena newArena; + int unproc_len = unprocessed_end - unprocessed_begin; + int len = std::max( 65536, unproc_len*2 ); + uint8_t* newBuffer = new (newArena) uint8_t[ len ]; + memcpy( newBuffer, unprocessed_begin, unproc_len ); + arena = newArena; + unprocessed_begin = newBuffer; + unprocessed_end = newBuffer + unproc_len; + buffer_end = newBuffer + len; + readAllBytes = buffer_end - unprocessed_end; + } + + int readBytes = conn->read( unprocessed_end, buffer_end ); + if (!readBytes) break; + state bool readWillBlock = readBytes != readAllBytes; + unprocessed_end += readBytes; + + if (expectConnectPacket && unprocessed_end-unprocessed_begin>=CONNECT_PACKET_V0_SIZE) { + // At the beginning of a connection, we expect to receive a packet containing the protocol version and the listening port of the remote process + ConnectPacket* p = (ConnectPacket*)unprocessed_begin; + + uint64_t connectionId = 0; + int32_t connectPacketSize = p->minimumSize(); + if ( unprocessed_end-unprocessed_begin >= connectPacketSize ) { + if(p->protocolVersion >= 0x0FDB00A444020001) { + connectionId = p->connectionId; + } + + if( (p->protocolVersion&compatibleProtocolVersionMask) != (currentProtocolVersion&compatibleProtocolVersionMask) ) { + NetworkAddress addr = p->canonicalRemotePort ? NetworkAddress( p->canonicalRemoteIp, p->canonicalRemotePort ) : conn->getPeerAddress(); + if(connectionId != 1) addr.port = 0; + + if(!transport->multiVersionConnections.count(connectionId)) { + if(now() - transport->lastIncompatibleMessage > FLOW_KNOBS->CONNECTION_REJECTED_MESSAGE_DELAY) { + TraceEvent(SevWarn, "ConnectionRejected", conn->getDebugID()) + .detail("Reason", "IncompatibleProtocolVersion") + .detail("LocalVersion", currentProtocolVersion) + .detail("RejectedVersion", p->protocolVersion) + .detail("VersionMask", compatibleProtocolVersionMask) + .detail("Peer", p->canonicalRemotePort ? NetworkAddress( p->canonicalRemoteIp, p->canonicalRemotePort ) : conn->getPeerAddress()); + transport->lastIncompatibleMessage = now(); + } + if(!transport->incompatiblePeers.count(addr)) { + transport->incompatiblePeers[ addr ] = std::make_pair(connectionId, now()); + } + } else if(connectionId > 1) { + transport->multiVersionConnections[connectionId] = now() + FLOW_KNOBS->CONNECTION_ID_TIMEOUT; + } + throw incompatible_protocol_version(); + } + + if(connectionId > 1) { + transport->multiVersionConnections[connectionId] = now() + FLOW_KNOBS->CONNECTION_ID_TIMEOUT; + } + unprocessed_begin += connectPacketSize; + expectConnectPacket = false; + + peerProtocolVersion = p->protocolVersion; + if (isOutgoing) { + // Outgoing connection; port information should be what we expect + TraceEvent("ConnectedOutgoing").detail("PeerAddr", NetworkAddress( p->canonicalRemoteIp, p->canonicalRemotePort ) ); + ASSERT( p->canonicalRemotePort == peerAddress.port ); + } else { + if (p->canonicalRemotePort) { + peerAddress = NetworkAddress( p->canonicalRemoteIp, p->canonicalRemotePort, true, peerAddress.isTLS() ); + } + onPeerAddress.send( peerAddress ); + Void _ = wait( delay(0) ); // Check for cancellation + } + } + } + if (!expectConnectPacket) + scanPackets( transport, unprocessed_begin, unprocessed_end, arena, peerAddress, peerProtocolVersion ); + + if (readWillBlock) + break; + + Void _ = wait(yield(TaskReadSocket)); + } + + Void _ = wait( conn->onReadable() ); + Void _ = wait(delay(0, TaskReadSocket)); // We don't want to call conn->read directly from the reactor - we could get stuck in the reactor reading 1 packet at a time + } +} + +ACTOR static Future connectionIncoming( TransportData* self, Reference conn ) { + try { + state Promise onPeerAddress; + state Future reader = connectionReader( self, conn, false, onPeerAddress ); + choose { + when( Void _ = wait( reader ) ) { ASSERT(false); return Void(); } + when( NetworkAddress pa = wait( onPeerAddress.getFuture() ) ) { + Peer* p = self->getPeer( pa, false ); + p->onIncomingConnection( conn, reader ); + } + when( Void _ = wait( delayJittered(FLOW_KNOBS->CONNECTION_MONITOR_TIMEOUT) ) ) { + TEST(true); // Incoming connection timed out + throw timed_out(); + } + } + return Void(); + } catch (Error& e) { + TraceEvent("IncomingConnectionError", conn->getDebugID()).error(e).detail("FromAddress", conn->getPeerAddress()); + conn->close(); + return Void(); + } +} + +ACTOR static Future listen( TransportData* self, NetworkAddress listenAddr ) { + state ActorCollectionNoErrors incoming; // Actors monitoring incoming connections that haven't yet been associated with a peer + state Reference listener = INetworkConnections::net()->listen( listenAddr ); + try { + loop { + Reference conn = wait( listener->accept() ); + TraceEvent("ConnectionFrom", conn->getDebugID()).detail("FromAddress", conn->getPeerAddress()); + incoming.add( connectionIncoming(self, conn) ); + } + } catch (Error& e) { + TraceEvent(SevError, "ListenError").error(e); + throw; + } +} + +Peer* TransportData::getPeer( NetworkAddress const& address, bool doConnect ) { + auto& peer = peers[address]; + if (!peer) peer = new Peer(this, address, doConnect); + return peer; +} + +ACTOR static Future multiVersionCleanupWorker( TransportData* self ) { + loop { + Void _ = wait(delay(FLOW_KNOBS->CONNECTION_CLEANUP_DELAY)); + for(auto it = self->incompatiblePeers.begin(); it != self->incompatiblePeers.end();) { + if( self->multiVersionConnections.count(it->second.first) ) { + it = self->incompatiblePeers.erase(it); + } else { + it++; + } + } + + for(auto it = self->multiVersionConnections.begin(); it != self->multiVersionConnections.end();) { + if( it->second < now() ) { + it = self->multiVersionConnections.erase(it); + } else { + it++; + } + } + } +} + +FlowTransport::FlowTransport( uint64_t transportId ) : self(new TransportData(transportId)) { + self->multiVersionCleanup = multiVersionCleanupWorker(self); +} + +FlowTransport::~FlowTransport() { delete self; } + +void FlowTransport::initMetrics() { self->initMetrics(); } + +NetworkAddress FlowTransport::getLocalAddress() { return self->localAddress; } + +std::map>* FlowTransport::getIncompatiblePeers() { + for(auto it = self->incompatiblePeers.begin(); it != self->incompatiblePeers.end();) { + if( self->multiVersionConnections.count(it->second.first) ) { + it = self->incompatiblePeers.erase(it); + } else { + it++; + } + } + return &self->incompatiblePeers; +} + +Future FlowTransport::bind( NetworkAddress publicAddress, NetworkAddress listenAddress ) { + ASSERT( publicAddress.isPublic() ); + self->localAddress = publicAddress; + TraceEvent("Binding").detail("PublicAddress", publicAddress).detail("ListenAddress", listenAddress); + self->listen = listen( self, listenAddress ); + return self->listen; +} + +void FlowTransport::loadedEndpoint( Endpoint& endpoint ) { + if (endpoint.address.isValid()) return; + ASSERT( !(endpoint.token.first() & TOKEN_STREAM_FLAG) ); // Only reply promises are supposed to be unaddressed + ASSERT( g_currentDeliveryPeerAddress.isValid() ); + endpoint.address = g_currentDeliveryPeerAddress; +} + +void FlowTransport::addEndpoint( Endpoint& endpoint, NetworkMessageReceiver* receiver, uint32_t taskID ) { + endpoint.token = g_random->randomUniqueID(); + if (receiver->isStream()) { + endpoint.address = getLocalAddress(); + endpoint.token = UID( endpoint.token.first() | TOKEN_STREAM_FLAG, endpoint.token.second() ); + } else { + endpoint.address = NetworkAddress(); + endpoint.token = UID( endpoint.token.first() & ~TOKEN_STREAM_FLAG, endpoint.token.second() ); + } + self->endpoints.insert( receiver, endpoint.token, taskID ); +} + +void FlowTransport::removeEndpoint( const Endpoint& endpoint, NetworkMessageReceiver* receiver ) { + self->endpoints.remove(endpoint.token, receiver); +} + +void FlowTransport::addWellKnownEndpoint( Endpoint& endpoint, NetworkMessageReceiver* receiver, uint32_t taskID ) { + endpoint.address = getLocalAddress(); + ASSERT( ((endpoint.token.first() & TOKEN_STREAM_FLAG)!=0) == receiver->isStream() ); + Endpoint::Token otoken = endpoint.token; + self->endpoints.insert( receiver, endpoint.token, taskID ); + ASSERT( endpoint.token == otoken ); +} + +static PacketID sendPacket( TransportData* self, ISerializeSource const& what, const Endpoint& destination, bool reliable ) { + if (destination.address == self->localAddress) { + TEST(true); // "Loopback" delivery + // SOMEDAY: Would it be better to avoid (de)serialization by doing this check in flow? + + BinaryWriter wr( AssumeVersion(currentProtocolVersion) ); + what.serializeBinaryWriter(wr); + Standalone copy = wr.toStringRef(); + + deliver( self, destination, ArenaReader(copy.arena(), copy, AssumeVersion(currentProtocolVersion)), false ); + + return (PacketID)NULL; + } else { + ++self->countPacketsGenerated; + + Peer* peer = self->getPeer(destination.address); + + // If there isn't an open connection or public address, we can't send + if (peer->outgoingConnectionIdle && !destination.address.isPublic()) { + TEST(true); // Can't send to private address without an open connection + return (PacketID)NULL; + } + + bool firstUnsent = peer->unsent.empty(); + + PacketBuffer* pb = peer->unsent.getWriteBuffer(); + ReliablePacket* rp = reliable ? new ReliablePacket : 0; + + void*p = pb->data+pb->bytes_written; + int prevBytesWritten = pb->bytes_written; + PacketBuffer* checksumPb = pb; + + PacketWriter wr(pb,rp,AssumeVersion(currentProtocolVersion)); // SOMEDAY: Can we downgrade to talk to older peers? + + // Reserve some space for packet length and checksum, write them after serializing data + SplitBuffer packetInfoBuffer; + uint32_t len, checksum = 0; + int packetInfoSize = sizeof(len) + sizeof(checksum); + wr.writeAhead(packetInfoSize , &packetInfoBuffer); + wr << destination.token; + what.serializePacketWriter(wr); + pb = wr.finish(); + + len = wr.size() - packetInfoSize; + + // Find the correct place to start calculating checksum + uint32_t checksumUnprocessedLength = len; + prevBytesWritten += packetInfoSize; + if (prevBytesWritten >= PacketBuffer::DATA_SIZE) { + prevBytesWritten -= PacketBuffer::DATA_SIZE; + checksumPb = checksumPb->nextPacketBuffer(); + } + + // Checksum calculation + while (checksumUnprocessedLength > 0) { + uint32_t processLength = std::min(checksumUnprocessedLength, (uint32_t)(PacketBuffer::DATA_SIZE - prevBytesWritten)); + checksum = crc32c_append(checksum, checksumPb->data + prevBytesWritten, processLength); + checksumUnprocessedLength -= processLength; + checksumPb = checksumPb->nextPacketBuffer(); + prevBytesWritten = 0; + } + + // Write packet length and checksum into packet buffer + packetInfoBuffer.write(&len, sizeof(len)); + packetInfoBuffer.write(&checksum, sizeof(checksum), sizeof(len)); + + if (len > FLOW_KNOBS->PACKET_LIMIT) { + TraceEvent(SevError, "Net2_PacketLimitExceeded").detail("ToPeer", destination.address).detail("Length", (int)len); + // throw platform_error(); // FIXME: How to recover from this situation? + } + else if (len > FLOW_KNOBS->PACKET_WARNING) { + TraceEvent(self->warnAlwaysForLargePacket ? SevWarnAlways : SevWarn, "Net2_LargePacket") + .detail("ToPeer", destination.address) + .detail("Length", (int)len) + .suppressFor(1.0); + + if(g_network->isSimulated()) + self->warnAlwaysForLargePacket = false; + } + + peer->send(pb, rp, firstUnsent); + + return (PacketID)rp; + } +} + +PacketID FlowTransport::sendReliable( ISerializeSource const& what, const Endpoint& destination ) { + return sendPacket( self, what, destination, true ); +} + +void FlowTransport::cancelReliable( PacketID pid ) { + ReliablePacket* p = (ReliablePacket*)pid; + if (p) p->remove(); + // SOMEDAY: Call reliable.compact() if a lot of memory is wasted in PacketBuffers by formerly reliable packets mixed with a few reliable ones. Don't forget to delref the new PacketBuffers since they are unsent. +} + +void FlowTransport::sendUnreliable( ISerializeSource const& what, const Endpoint& destination ) { + sendPacket( self, what, destination, false ); +} + +int FlowTransport::getEndpointCount() { + return -1; +} + +void FlowTransport::createInstance( uint64_t transportId ) +{ + g_network->setGlobal(INetwork::enFailureMonitor, (flowGlobalType) new SimpleFailureMonitor()); + g_network->setGlobal(INetwork::enFlowTransport, (flowGlobalType) new FlowTransport(transportId)); + g_network->setGlobal(INetwork::enNetworkAddressFunc, (flowGlobalType) &FlowTransport::getGlobalLocalAddress); +} diff --git a/fdbrpc/FlowTransport.h b/fdbrpc/FlowTransport.h new file mode 100644 index 0000000000..be426e5d55 --- /dev/null +++ b/fdbrpc/FlowTransport.h @@ -0,0 +1,129 @@ +/* + * FlowTransport.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_TRANSPORT_H +#define FLOW_TRANSPORT_H +#pragma once + +#include "flow/network.h" + +#pragma pack(push, 4) +class Endpoint { +public: + // Endpoint represents a particular service (e.g. a serialized Promise or PromiseStream) + // An endpoint is either "local" (used for receiving data) or "remote" (used for sending data) + typedef UID Token; + NetworkAddress address; + Token token; + + Endpoint() : address(0,0) {} + Endpoint( NetworkAddress const& address, Token token ) : address(address), token(token) {} + bool isValid() const { return token.isValid(); } + bool isLocal() const; + + bool operator == (Endpoint const& r) const { return address == r.address && token == r.token; } + bool operator != (Endpoint const& r) const { return address != r.address || token != r.token; } + bool operator < (Endpoint const& r) const { if (address != r.address) return address < r.address; else return token < r.token; } + + template + void serialize(Ar& ar) { + ar.serializeBinaryItem(*this); + } +}; +#pragma pack(pop) +BINARY_SERIALIZABLE( Endpoint ); + + +class NetworkMessageReceiver { +public: + virtual void receive( ArenaReader& ) = 0; + virtual bool isStream() const { return false; } +}; + + + +typedef struct NetworkPacket* PacketID; + +class FlowTransport { +public: + FlowTransport(uint64_t transportId); + ~FlowTransport(); + + static void createInstance(uint64_t transportId = 0); + // Creates a new FlowTransport and makes FlowTransport::transport() return it. This uses g_network->global() variables, + // so it will be private to a simulation. + + void initMetrics(); + // Metrics must be initialized after FlowTransport::createInstance has been called + + Future bind( NetworkAddress publicAddress, NetworkAddress listenAddress ); + // Starts a server listening on the given listenAddress, and sets publicAddress to be the public + // address of this server. Returns only errors. + + NetworkAddress getLocalAddress(); + // Returns the NetworkAddress that would be assigned by addEndpoint (the public address) + + std::map>* getIncompatiblePeers(); + // Returns the same of all peers that have attempted to connect, but have incompatible protocol versions + + void addEndpoint( Endpoint& endpoint, NetworkMessageReceiver*, uint32_t taskID ); + // Sets endpoint to be a new local endpoint which delivers messages to the given receiver + + void removeEndpoint( const Endpoint&, NetworkMessageReceiver* ); + // The given local endpoint no longer delivers messages to the given receiver or uses resources + + void addWellKnownEndpoint( Endpoint& endpoint, NetworkMessageReceiver*, uint32_t taskID ); + // Sets endpoint to a new local endpoint (without changing its token) which delivers messages to the given receiver + // Implementations may have limitations on when this function is called and what endpoint.token may be! + + PacketID sendReliable( ISerializeSource const& what, const Endpoint& destination ); + // sendReliable will keep trying to deliver the data to the destination until cancelReliable is + // called. It will retry sending if the connection is closed or the failure manager reports + // the destination become available (edge triggered). + + void cancelReliable( PacketID ); + // Makes PacketID "unreliable" (either the data or a connection close event will be delivered + // eventually). It can still be used safely to send a reply to a "reliable" request. + + void sendUnreliable( ISerializeSource const& what, const Endpoint& destination );// { cancelReliable(sendReliable(what,destination)); } + + int getEndpointCount(); + // for tracing only + + static FlowTransport& transport() { return *static_cast((void*) g_network->global(INetwork::enFlowTransport)); } + static NetworkAddress getGlobalLocalAddress() { return transport().getLocalAddress(); } + + template + void loadEndpoint(Ar& ar, Endpoint& e) { + ar >> e; + loadedEndpoint(e); + } + +private: + class TransportData* self; + + void loadedEndpoint(Endpoint&); +}; + +inline bool Endpoint::isLocal() const { + return address == FlowTransport::transport().getLocalAddress(); +} + +#endif \ No newline at end of file diff --git a/fdbrpc/HTTP.actor.cpp b/fdbrpc/HTTP.actor.cpp new file mode 100644 index 0000000000..1c2fc9662c --- /dev/null +++ b/fdbrpc/HTTP.actor.cpp @@ -0,0 +1,342 @@ +/* + * HTTP.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "HTTP.h" +#include "md5/md5.h" +#include "libb64/encode.h" +#include + +namespace HTTP { + + std::string urlEncode(const std::string &s) { + std::string o; + o.reserve(s.size() * 3); + char buf[4]; + for(auto c : s) + if(std::isalnum(c)) + o.append(&c, 1); + else { + sprintf(buf, "%%%.02X", c); + o.append(buf); + } + return o; + } + + bool Response::verifyMD5(bool fail_if_header_missing, Optional content_sum) { + auto i = headers.find("Content-MD5"); + if(i != headers.end()) { + // If a content sum is not provided, calculate one from the response content + if(!content_sum.present()) { + MD5_CTX sum; + ::MD5_Init(&sum); + ::MD5_Update(&sum, content.data(), content.size()); + std::string sumBytes; + sumBytes.resize(16); + ::MD5_Final((unsigned char *)sumBytes.data(), &sum); + std::string sumStr = base64::encoder::from_string(sumBytes); + sumStr.resize(sumStr.size() - 1); + content_sum = sumStr; + } + return i->second == content_sum.get(); + } + return !fail_if_header_missing; + } + + std::string Response::toString() { + std::string r = format("Response code: %d\n", code); + r += format("ContentLen: %lld\n", contentLen); + for(auto h : headers) + r += format("Header: %s: %s\n", h.first.c_str(), h.second.c_str()); + r.append("--CONTENT--\n"); + r.append(content); + r.append("--------\n"); + return r; + } + + PacketBuffer * writeRequestHeader(std::string const &verb, std::string const &resource, HTTP::Headers const &headers, PacketBuffer *dest) { + PacketWriter writer(dest, NULL, Unversioned()); + writer.serializeBytes(verb); + writer.serializeBytes(" ", 1); + writer.serializeBytes(resource); + writer.serializeBytes(LiteralStringRef(" HTTP/1.1\r\n")); + for(auto h : headers) { + writer.serializeBytes(h.first); + writer.serializeBytes(LiteralStringRef(": ")); + writer.serializeBytes(h.second); + writer.serializeBytes(LiteralStringRef("\r\n")); + } + writer.serializeBytes(LiteralStringRef("\r\n")); + return writer.finish(); + } + + // Read at least 1 bytes from conn and up to maxlen in a single read, append read data into *buf + // Returns the number of bytes read. + ACTOR Future read_into_string(Reference conn, std::string *buf, int maxlen) { + loop { + // Wait for connection to have something to read + Void _ = wait(conn->onReadable()); + Void _ = wait( delay( 0, TaskReadSocket ) ); + + // Read into buffer + int originalSize = buf->size(); + // TODO: resize is zero-initializing the space we're about to overwrite, so do something else, which probably means + // not using a string for this buffer + buf->resize(originalSize + maxlen); + uint8_t *wptr = (uint8_t *)buf->data() + originalSize; + int len = conn->read(wptr, wptr + maxlen); + buf->resize(originalSize + len); + + // Make sure data was actually read, it's possible for there to be none. + if(len > 0) + return len; + } + } + + // Returns the position of delim within buf, relative to pos. If delim is not found, continues to read from conn until + // either it is found or the connection ends, at which point connection_failed is thrown and buf contains + // everything that was read up to that point. + ACTOR Future read_delimited_into_string(Reference conn, const char *delim, std::string *buf, size_t pos) { + state size_t sPos = pos; + state int lookBack = strlen(delim) - 1; + ASSERT(lookBack >= 0); + + loop { + size_t endPos = buf->find(delim, sPos); + if(endPos != std::string::npos) + return endPos - pos; + // Next search will start at the current end of the buffer - delim size + 1 + if(sPos >= lookBack) + sPos -= lookBack; + int _ = wait(read_into_string(conn, buf, CLIENT_KNOBS->HTTP_READ_SIZE)); + } + } + + // Reads from conn (as needed) until there are at least len bytes starting at pos in buf + ACTOR Future read_fixed_into_string(Reference conn, int len, std::string *buf, size_t pos) { + state int stop_size = pos + len; + while(buf->size() < stop_size) + int _ = wait(read_into_string(conn, buf, CLIENT_KNOBS->HTTP_READ_SIZE)); + return Void(); + } + + ACTOR Future read_http_response_headers(Reference conn, Headers *headers, std::string *buf, size_t *pos) { + loop { + // Get a line, reading more data from conn if necessary + size_t lineLen = wait(read_delimited_into_string(conn, "\r\n", buf, *pos)); + + // If line is empty we have reached the end of the headers. + if(lineLen == 0) { + // Increment pos to move past the empty line. + *pos += 2; + return Void(); + } + + int nameEnd=-1, valueStart=-1, valueEnd=-1; + int len = -1; + + // Read header of the form "Name: Value\n" + // Note that multi line header values are not supported here. + // Format string breaks down as follows: + // %*[^:]%n Some characters other than ':' which are discarded, save the end position + // :%*[ \t]%n A colon followed by 0 or more spaces or tabs only, save the end position + // %*[^\r]%n Some characters other than \r which are discarded, save the end position + // %*1[\r] Exactly one \r + // %*1[\n] Exactly one \n + // %n Save final end position + if(sscanf(buf->c_str() + *pos, "%*[^:]%n:%*[ \t]%n%*[^\r]%n%*1[\r]%*1[\n]%n", &nameEnd, &valueStart, &valueEnd, &len) >= 0 && len > 0) { + const std::string name(buf->substr(*pos, nameEnd)); + const std::string value(buf->substr(*pos + valueStart, valueEnd - valueStart)); + (*headers)[name] = value; + *pos += len; + len = -1; + } + else // Malformed header line (at least according to this simple parsing) + throw http_bad_response(); + } + } + + // Reads an HTTP response from a network connection + // If the connection fails while being read the exception will emitted + // If the response is not parseable or complete in some way, http_bad_response will be thrown + ACTOR Future read_http_response(Reference r, Reference conn, bool header_only) { + state std::string buf; + state size_t pos = 0; + + // Read HTTP reponse code and version line + size_t lineLen = wait(read_delimited_into_string(conn, "\r\n", &buf, pos)); + + int reachedEnd = -1; + sscanf(buf.c_str() + pos, "HTTP/%f %d%n", &r->version, &r->code, &reachedEnd); + if(reachedEnd < 0) + throw http_bad_response(); + + // Move position past the line found and the delimiter length + pos += lineLen + 2; + + // Read headers + r->headers.clear(); + + Void _ = wait(read_http_response_headers(conn, &r->headers, &buf, &pos)); + + auto i = r->headers.find("Content-Length"); + if(i != r->headers.end()) + r->contentLen = atoi(i->second.c_str()); + else + r->contentLen = -1; // Content length unknown + + state std::string transferEncoding; + i = r->headers.find("Transfer-Encoding"); + if(i != r->headers.end()) + transferEncoding = i->second; + + r->content.clear(); + + // If this is supposed to be a header-only response and the buffer has been fully processed then stop. Otherwise, there must be response content. + if(header_only && pos == buf.size()) + return Void(); + + // There should be content (or at least metadata describing that there is no content. + // Chunked transfer and 'normal' mode (content length given, data in one segment after headers) are supported. + if(r->contentLen >= 0) { + // Use response content as the buffer so there's no need to copy it later. + r->content = buf.substr(pos); + pos = 0; + + // Read until there are at least contentLen bytes available at pos + Void _ = wait(read_fixed_into_string(conn, r->contentLen, &r->content, pos)); + + // There shouldn't be any bytes after content. + if(r->content.size() != r->contentLen) + throw http_bad_response(); + } + else if(transferEncoding == "chunked") { + // Copy remaining buffer data to content which will now be the read buffer for the chunk encoded data. + // Overall this will be fairly efficient since most bytes will only be written once but some bytes will + // have to be copied forward in the buffer when removing chunk overhead bytes. + r->content = buf.substr(pos); + pos = 0; + + loop { + // Read the line that contains the chunk length as text in hex + size_t lineLen = wait(read_delimited_into_string(conn, "\r\n", &r->content, pos)); + state int chunkLen = strtol(r->content.substr(pos, lineLen).c_str(), NULL, 16); + + // Instead of advancing pos, erase the chunk length header line (line length + delimiter size) from the content buffer + r->content.erase(pos, lineLen + 2); + + // If chunkLen is 0 then this marks the end of the content chunks. + if(chunkLen == 0) + break; + + // Read (if needed) until chunkLen bytes are available at pos, then advance pos by chunkLen + Void _ = wait(read_fixed_into_string(conn, chunkLen, &r->content, pos)); + pos += chunkLen; + + // Read the final empty line at the end of the chunk (the required "\r\n" after the chunk bytes) + size_t lineLen = wait(read_delimited_into_string(conn, "\r\n", &r->content, pos)); + if(lineLen != 0) + throw http_bad_response(); + + // Instead of advancing pos, erase the empty line from the content buffer + r->content.erase(pos, 2); + } + + // The content buffer now contains the de-chunked, contiguous content at position 0 to pos. Save this length. + r->contentLen = pos; + + // Next is the post-chunk header block, so read that. + Void _ = wait(read_http_response_headers(conn, &r->headers, &r->content, &pos)); + + // If the header parsing did not consume all of the buffer then something is wrong + if(pos != r->content.size()) + throw http_bad_response(); + + // Now truncate the buffer to just the dechunked contiguous content. + r->content.erase(r->contentLen); + } + else { + // Some unrecogize response content scheme is being used. + throw http_bad_response(); + } + + // If there is actual response content, check the MD5 sum against the Content-MD5 response header + if(r->content.size() > 0) + if(!r->verifyMD5(false)) // false arg means do not fail if the Content-MD5 header is missing. + throw http_bad_response(); + + return Void(); + } + + Future HTTP::Response::read(Reference conn, bool header_only) { + return read_http_response(Reference::addRef(this), conn, header_only); + } + + // Do a request, get a Response. + // Request content is provided as UnsentPacketQueue *pContent which will be depleted as bytes are sent but the queue itself must live for the life of this actor + // and be destroyed by the caller + // TODO: pSent is very hackish, do something better. + ACTOR Future> doRequest(Reference conn, std::string verb, std::string resource, HTTP::Headers headers, UnsentPacketQueue *pContent, int contentLen, Reference sendRate, int64_t *pSent, Reference recvRate) { + state UnsentPacketQueue empty; + if(pContent == NULL) + pContent = ∅ + + try { + // Write headers to a packet buffer chain + PacketBuffer *pFirst = new PacketBuffer(); + PacketBuffer *pLast = writeRequestHeader(verb, resource, headers, pFirst); + // Prepend headers to content packer buffer chain + pContent->prependWriteBuffer(pFirst, pLast); + + if(CLIENT_KNOBS->HTTP_VERBOSE_LEVEL > 1) + printf("[%s] HTTP starting %s %s\n", conn->getDebugID().toString().c_str(), verb.c_str(), resource.c_str()); + state double send_start = timer(); + state double total_sent = 0; + loop { + Void _ = wait(conn->onWritable()); + Void _ = wait( delay( 0, TaskWriteSocket ) ); + state int trySend = CLIENT_KNOBS->HTTP_SEND_SIZE; + Void _ = wait(sendRate->getAllowance(trySend)); + int len = conn->write(pContent->getUnsent(), trySend); + if(pSent != nullptr) + *pSent += len; + sendRate->returnUnused(trySend - len); + total_sent += len; + pContent->sent(len); + if(pContent->empty()) + break; + } + + state Reference r(new HTTP::Response()); + Void _ = wait(r->read(conn, verb == "HEAD")); + double elapsed = timer() - send_start; + if(CLIENT_KNOBS->HTTP_VERBOSE_LEVEL > 0) + printf("[%s] HTTP code=%d, time=%fs %s %s [%u out, response content len %d]\n", conn->getDebugID().toString().c_str(), r->code, elapsed, verb.c_str(), resource.c_str(), (int)total_sent, (int)r->contentLen); + if(CLIENT_KNOBS->HTTP_VERBOSE_LEVEL > 2) + printf("[%s] HTTP RESPONSE: %s %s\n%s\n", conn->getDebugID().toString().c_str(), verb.c_str(), resource.c_str(), r->toString().c_str()); + return r; + } catch(Error &e) { + double elapsed = timer() - send_start; + if(CLIENT_KNOBS->HTTP_VERBOSE_LEVEL > 0) + printf("[%s] HTTP *ERROR*=%s, time=%fs %s %s [%u out]\n", conn->getDebugID().toString().c_str(), e.what(), elapsed, verb.c_str(), resource.c_str(), (int)total_sent); + throw; + } + } + +} diff --git a/fdbrpc/HTTP.h b/fdbrpc/HTTP.h new file mode 100644 index 0000000000..d058af2805 --- /dev/null +++ b/fdbrpc/HTTP.h @@ -0,0 +1,49 @@ +/* + * HTTP.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/flow.h" +#include "flow/Net2Packet.h" +#include "IRateControl.h" +#include "fdbclient/Knobs.h" + +namespace HTTP { + typedef std::map Headers; + + std::string urlEncode(const std::string &s); + + struct Response : ReferenceCounted{ + Response() {} + Future read(Reference conn, bool header_only); + std::string toString(); + float version; + int code; + Headers headers; + std::string content; + int64_t contentLen; + + bool verifyMD5(bool fail_if_header_missing, Optional content_sum = Optional()); + }; + + // Prepend the HTTP request header to the given PacketBuffer, returning the new head of the buffer chain + PacketBuffer * writeRequestHeader(std::string const &verb, std::string const &resource, HTTP::Headers const &headers, PacketBuffer *dest); + + // Do an HTTP request to the blob store, parse the response. + Future> doRequest(Reference const &conn, std::string const &verb, std::string const &resource, HTTP::Headers const &headers, UnsentPacketQueue * const &pContent, int const &contentLen, Reference const &sendRate, int64_t * const &pSent, Reference const &recvRate); +} diff --git a/fdbrpc/IAsyncFile.actor.cpp b/fdbrpc/IAsyncFile.actor.cpp new file mode 100644 index 0000000000..a746e20bfd --- /dev/null +++ b/fdbrpc/IAsyncFile.actor.cpp @@ -0,0 +1,68 @@ +/* + * IAsyncFile.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "IAsyncFile.h" +#include "flow/Error.h" +#include "flow/Knobs.h" +#include "flow/Platform.h" +#include "flow/UnitTest.h" +#include + +IAsyncFile::IAsyncFile(){}; + +ACTOR static Future incrementalDeleteHelper( std::string filename, int64_t truncateAmt, double interval ){ + + state Reference f = wait( + IAsyncFileSystem::filesystem()->open(filename, IAsyncFile::OPEN_READWRITE, 0)); + state int64_t filesize = wait(f->size()); + state int64_t i = filesize; + + Void _ = wait(IAsyncFileSystem::filesystem()->deleteFile(filename, true)); + for( ;i > 0; i -= truncateAmt ){ + Void _ = wait(f->truncate(i)); + Void _ = wait(f->sync()); + Void _ = wait(delay(interval)); + } + return Void(); +} + +Future IAsyncFile::incrementalDelete( std::string filename){ + return uncancellable(incrementalDeleteHelper( + filename, + FLOW_KNOBS->INCREMENTAL_DELETE_TRUNCATE_AMOUNT, + FLOW_KNOBS->INCREMENTAL_DELETE_INTERVAL)); +} + +TEST_CASE( "fileio/incrementalDelete" ) { + //about 5GB + state int64_t fileSize = 5e9; + state std::string filename = "/tmp/__JUNK__"; + state Reference f = + wait(IAsyncFileSystem::filesystem()->open( + filename, + IAsyncFile::OPEN_ATOMIC_WRITE_AND_CREATE | IAsyncFile::OPEN_CREATE | IAsyncFile::OPEN_READWRITE, + 0)); + Void _ = wait(f->sync()); + Void _ = wait(f->truncate(fileSize)); + //close the file by deleting the reference + f.clear(); + Void _ = wait(IAsyncFile::incrementalDelete(filename)); + return Void(); +} diff --git a/fdbrpc/IAsyncFile.h b/fdbrpc/IAsyncFile.h new file mode 100644 index 0000000000..8f4ec32c71 --- /dev/null +++ b/fdbrpc/IAsyncFile.h @@ -0,0 +1,101 @@ +/* + * IAsyncFile.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_IASYNCFILE_H +#define FLOW_IASYNCFILE_H +#pragma once + +#include "flow/flow.h" + +//All outstanding operations must be cancelled before the destructor of IAsyncFile is called. +class IAsyncFile { +public: + IAsyncFile(); + // Pass these to g_network->open to get an IAsyncFile + enum { + // Implementation relies on the low bits being the same as the SQLite flags (this is validated by a static_assert there) + OPEN_READONLY = 0x1, + OPEN_READWRITE = 0x2, + OPEN_CREATE = 0x4, + OPEN_EXCLUSIVE = 0x10, + + // Further flag values are arbitrary bits + OPEN_UNBUFFERED = 0x10000, + OPEN_UNCACHED = 0x20000, + OPEN_LOCK = 0x40000, + OPEN_ATOMIC_WRITE_AND_CREATE = 0x80000, // A temporary file is opened, and on the first call to sync() it is atomically renamed to the given filename + OPEN_LARGE_PAGES = 0x100000, + OPEN_NO_AIO = 0x200000, // Don't use AsyncFileKAIO or similar implementations that rely on filesystem support for AIO + OPEN_CACHED_READ_ONLY = 0x400000 // AsyncFileCached opens files read/write even if you specify read only + }; + + virtual void addref() = 0; + virtual void delref() = 0; + + // For read() and write(), the data buffer must remain valid until the future is ready + virtual Future read( void* data, int length, int64_t offset ) = 0; // Returns number of bytes actually read (from [0,length]) + virtual Future write( void const* data, int length, int64_t offset ) = 0; + virtual Future truncate( int64_t size ) = 0; + virtual Future sync() = 0; + virtual Future flush() { return Void(); } // Sends previous writes to the OS if they have been buffered in memory, but does not make them power safe + virtual Future size() = 0; + virtual std::string getFilename() = 0; + //start an actor to truncate the file repeatedly so that the operating system doesn't delete it all at once + static Future incrementalDelete( std::string filename); + + // Attempt to read the *length bytes at offset without copying. If successful, a pointer to the + // requested bytes is written to *data, and the number of bytes successfully read is + // written to *length. If unsuccessful, *data and *length are undefined. + // readZeroCopy may fail (returning io_error) at any time, even if the requested bytes are readable. + // For example, an implementation of IAsyncFile may not implement readZeroCopy or may implement it + // only in certain cases (e.g. when the requested range does not cross a page boundary). So callers + // should always retry a failed readZeroCopy as a read(). + // Once readZeroCopy succeeds, the returned bytes will be pinned in memory until releaseZeroCopy is + // called, so the caller must always ensure that a matching call to releaseZeroCopy takes place. + // Between readZeroCopy and releaseZeroCopy, it is illegal (undefined behavior) to concurrently write + // to an overlapping range of bytes, whether or not using the same IAsyncFile handle. + virtual Future readZeroCopy( void** data, int* length, int64_t offset ) { return io_error(); } + virtual void releaseZeroCopy( void* data, int length, int64_t offset ) {} + + virtual int64_t debugFD() = 0; +}; + +typedef void (*runCycleFuncPtr)(); + +class IAsyncFileSystem { +public: + virtual Future< Reference > open( std::string filename, int64_t flags, int64_t mode ) = 0; + // Opens a file for asynchronous I/O + + virtual Future< Void > deleteFile( std::string filename, bool mustBeDurable ) = 0; + // Deletes the given file. If mustBeDurable, returns only when the file is guaranteed to be deleted even after a power failure. + + static IAsyncFileSystem* filesystem() { return filesystem(g_network); } + static runCycleFuncPtr runCycleFunc() { return reinterpret_cast(reinterpret_cast(g_network->global(INetwork::enRunCycleFunc))); } + + static IAsyncFileSystem* filesystem(INetwork* networkPtr) { return static_cast(networkPtr->global(INetwork::enFileSystem)); } + + +protected: + IAsyncFileSystem() {} + virtual ~IAsyncFileSystem() {} // Please don't try to delete through this interface! +}; + +#endif diff --git a/fdbrpc/IRateControl.h b/fdbrpc/IRateControl.h new file mode 100644 index 0000000000..259daa2390 --- /dev/null +++ b/fdbrpc/IRateControl.h @@ -0,0 +1,87 @@ +/* + * IRateControl.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "flow/flow.h" + +class IRateControl { +public: + virtual ~IRateControl() {} + // Future is Ready once you can use n units; + virtual Future getAllowance(unsigned int n) = 0; + // If all of the allowance is not used the unused units can be given back. + // For convenience, n can safely be negative. + virtual void returnUnused(int n) = 0; + virtual void addref() = 0; + virtual void delref() = 0; +}; + +// An IRateControl implemenation that allows at most hands out at most windowLimit units of 'credit' in windowSeconds seconds +class SpeedLimit : public IRateControl, ReferenceCounted { +public: + SpeedLimit(int windowLimit, int windowSeconds) : m_limit(windowLimit), m_seconds(windowSeconds), m_last_update(0), m_budget(0) { + m_budget_max = m_limit * m_seconds; + m_last_update = timer(); + } + virtual ~SpeedLimit() {} + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + virtual Future getAllowance(unsigned int n) { + // Replenish budget based on time since last update + double ts = timer(); + // returnUnused happens to do exactly what we want here + returnUnused((ts - m_last_update) / m_seconds * m_limit); + m_last_update = ts; + m_budget -= n; + // If budget is still >= 0 then it's safe to use the allowance right now. + if(m_budget >= 0) + return Void(); + // Otherise return the amount of time it will take for the budget to rise to 0. + return delay(m_seconds * -m_budget / m_limit); + } + + virtual void returnUnused(int n) { + if(n < 0) + return; + m_budget = std::min(m_budget + n, m_budget_max); + } + +private: + int m_limit; + double m_seconds; + double m_last_update; + int64_t m_budget; + int64_t m_budget_max; +}; + +// An IRateControl implemenation that enforces no limit +class Unlimited : public IRateControl, ReferenceCounted { +public: + Unlimited() {} + virtual ~Unlimited() {} + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + virtual Future getAllowance(unsigned int n) { return Void(); } + virtual void returnUnused(int n) {} +}; diff --git a/fdbrpc/ITLSPlugin.h b/fdbrpc/ITLSPlugin.h new file mode 100644 index 0000000000..cc05c4053d --- /dev/null +++ b/fdbrpc/ITLSPlugin.h @@ -0,0 +1,140 @@ +/* + * ITLSPlugin.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDB_ITLSPLUGIN_H +#define FDB_ITLSPLUGIN_H + +#pragma once + +#include + +struct ITLSSession { + enum { SUCCESS = 0, WANT_READ = -1, WANT_WRITE = -2, FAILED = -3 }; + + virtual void addref() = 0; + virtual void delref() = 0; + + // handshake should return SUCCESS if the handshake is complete, + // FAILED on fatal error, or one of WANT_READ or WANT_WRITE if the + // handshake should be reattempted after more data can be + // read/written on the underlying connection. + virtual int handshake() = 0; + + // read should return the (non-zero) number of bytes read, + // WANT_READ or WANT_WRITE if the operation is blocked by the + // underlying stream, or FAILED if there is an error (including a + // closed connection). + virtual int read(uint8_t* data, int length) = 0; + + // write should return the (non-zero) number of bytes written, or + // WANT_READ or WANT_WRITE if the operation is blocked by the + // underlying stream, or FAILED if there is an error. + virtual int write(const uint8_t* data, int length) = 0; +}; + +// Returns the number of bytes sent (possibly 0), or -1 on error +// (including connection close) +typedef int (*TLSSendCallbackFunc)(void* ctx, const uint8_t* buf, int len); + +// Returns the number of bytes read (possibly 0), or -1 on error +// (including connection close) +typedef int (*TLSRecvCallbackFunc)(void* ctx, uint8_t* buf, int len); + +struct ITLSPolicy { + virtual void addref() = 0; + virtual void delref() = 0; + + // set_cert_data should import the provided certificate list and + // associate it with this policy. cert_data will point to a PEM + // encoded certificate list, ordered such that each certificate + // certifies the one befor it. + // + // cert_data may additionally contain key information, which must + // be ignored. + // + // set_cert_data should return true if the operation succeeded, + // and false otherwise. After the first call to create_session for + // a given policy, set_cert_data should immediately return false + // if called. + virtual bool set_cert_data(const uint8_t* cert_data, int cert_len) = 0; + + // set_key_data should import the provided private key and + // associate it with this policy. key_data will point to a PEM + // encoded key. + // + // key_data may additionally contain certificate information, + // which must be ignored. + // + // set_key_data should return true if the operation succeeded, and + // false otherwise. After the first call to create_session for a + // given policy, set_key_data should immediately return false if + // called. + virtual bool set_key_data(const uint8_t* key_data, int key_len) = 0; + + // set_verify_peers should modify the validation rules for + // verifying a peer during connection handshake. The format of + // verify_peers is implementation specific. + // + // set_verify_peers should return true if the operation succeed, + // and false otherwise. After the first call to create_session for + // a given policy, set_verify_peers should immediately return + // false if called. + virtual bool set_verify_peers(const uint8_t* verify_peers, int verify_peers_len) = 0; + + // create_session should return a new object that implements + // ITLSSession, associated with this policy. After the first call + // to create_session for a given policy, further calls to + // ITLSPolicy::set_* will fail and return false. + // + // The newly created session should use send_func and recv_func to + // send and receive data on the underlying transport, and must + // provide send_ctx/recv_ctx to the callbacks. + // + // uid should only be provided when invoking an ITLSLogFunc, which + // will use it to identify this session. + virtual ITLSSession* create_session(bool is_client, TLSSendCallbackFunc send_func, void* send_ctx, TLSRecvCallbackFunc recv_func, void* recv_ctx, void* uid ) = 0; +}; + +// Logs a message/error to the appropriate trace log. +// +// event must be a valid XML attribute value. uid may be NULL or the +// uid provided to ITLSPolicy::create_session by the caller. is_error +// should be true for errors and false for informational messages. The +// remaining arguments must be pairs of (const char*); the first of +// each pair must be a valid XML attribute name, and the second a +// valid XML attribute value. The final parameter must be NULL. +typedef void (*ITLSLogFunc)(const char* event, void* uid, bool is_error, ...); + +struct ITLSPlugin { + virtual void addref() = 0; + virtual void delref() = 0; + + // create_policy should return a new object that implements + // ITLSPolicy. + // + // The newly created policy, and any session further created from + // the policy, should use logf to log any messages or errors that + // occur. + virtual ITLSPolicy* create_policy( ITLSLogFunc logf ) = 0; + + static inline const char* get_plugin_type_name_and_version() { return "ITLSPlugin"; } +}; + +#endif /* FDB_ITLSPLUGIN_H */ diff --git a/fdbrpc/LoadBalance.actor.h b/fdbrpc/LoadBalance.actor.h new file mode 100644 index 0000000000..555ef11919 --- /dev/null +++ b/fdbrpc/LoadBalance.actor.h @@ -0,0 +1,392 @@ +/* + * LoadBalance.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FLOW_LOADBALANCE_ACTOR_G_H) + #define FLOW_LOADBALANCE_ACTOR_G_H + #include "LoadBalance.actor.g.h" +#elif !defined(FLOW_LOADBALANCE_ACTOR_H) + #define FLOW_LOADBALANCE_ACTOR_H + +#include "flow/flow.h" +#include "flow/Knobs.h" + +#include "FailureMonitor.h" +#include "fdbrpc.h" +#include "Locality.h" +#include "QueueModel.h" +#include "MultiInterface.h" + +using std::vector; + +struct ModelHolder : NonCopyable, public ReferenceCounted { + QueueModel* model; + bool released; + double startTime; + double delta; + uint64_t token; + + ModelHolder( QueueModel* model, uint64_t token ) : model(model), token(token), released(false), startTime(now()) { + if(model) { + delta = model->addRequest(token); + } + } + + void release(bool clean, double penalty, bool measureLatency = true) { + if(model && !released) { + released = true; + double latency = (clean || measureLatency) ? now() - startTime : 0.0; + model->endRequest(token, latency, penalty, delta, clean); + } + } + + ~ModelHolder() { + release(false, -1.0, false); + } +}; + +struct LoadBalancedReply { + double penalty; + LoadBalancedReply() : penalty(1.0) {} + + template + void serialize(Ar &ar) { + ar & penalty; + } +}; + +Optional getLoadBalancedReply(LoadBalancedReply *reply); +Optional getLoadBalancedReply(void*); + +// Returns true if we got a value for our request +// Throws an error if the request returned an error that should bubble out +// Returns false if we got an error that should result in reissuing the request +template +bool checkAndProcessResult(ErrorOr result, Reference holder, bool atMostOnce) { + int errCode = result.isError() ? result.getError().code() : error_code_success; + bool maybeDelivered = errCode == error_code_broken_promise || errCode == error_code_request_maybe_delivered; + bool receivedResponse = result.present() || (!maybeDelivered && errCode != error_code_process_behind); + + Optional loadBalancedReply; + if(!result.isError()) { + loadBalancedReply = getLoadBalancedReply(&result.get()); + } + + holder->release(receivedResponse, loadBalancedReply.present() ? loadBalancedReply.get().penalty : -1.0); + + if(result.present()) { + return true; + } + + if(receivedResponse) { + throw result.getError(); + } + + if(atMostOnce && maybeDelivered) { + throw request_maybe_delivered(); + } + + return false; +} + +ACTOR template +Future> makeRequest(RequestStream const* stream, Request request, double backoff, Future requestUnneeded, QueueModel *model, bool isFirstRequest, bool atMostOnce) { + if(backoff > 0.0) { + Void _ = wait(delay(backoff) || requestUnneeded); + } + + if(requestUnneeded.isReady()) { + return Optional(); + } + + state Reference holder(new ModelHolder(model, stream->getEndpoint().token.first())); + + ErrorOr result = wait(stream->tryGetReply(request)); + if(checkAndProcessResult(result, holder, atMostOnce)) { + return result.get(); + } + else { + return Optional(); + } +} + +template +void addLaggingRequest(Future> reply, Promise requestFinished, QueueModel *model) { + requestFinished.send(Void()); + if(!reply.isReady()) { + if(model) { + if(model->laggingRequestCount > FLOW_KNOBS->MAX_LAGGING_REQUESTS_OUTSTANDING || model->laggingRequests.isReady()) { + model->laggingRequests.cancel(); + model->laggingRequestCount = 0; + model->addActor = PromiseStream>(); + model->laggingRequests = actorCollection( model->addActor.getFuture(), &model->laggingRequestCount ); + } + + model->addActor.send(success(errorOr(reply))); + } + } +} + +// Keep trying to get a reply from any of servers until success or cancellation; tries to take into account +// failMon's information for load balancing and avoiding failed servers +// If ALL the servers are failed and the list of servers is not fresh, throws an exception to let the caller refresh the list of servers +ACTOR template +Future< REPLY_TYPE(Request) > loadBalance( + Reference> alternatives, + RequestStream Interface::* channel, + Request request = Request(), + int taskID = TaskDefaultPromiseEndpoint, + bool atMostOnce = false, // if true, throws request_maybe_delivered() instead of retrying automatically + QueueModel* model = NULL) +{ + state Future> firstRequest; + state Optional firstRequestEndpoint; + state Future> secondRequest; + state Future secondDelay = Never(); + + state Promise requestFinished; + + setReplyPriority(request, taskID); + if (!alternatives) + return Never(); + + ASSERT( alternatives->size() ); + + state int bestAlt = g_random->randomInt(0, alternatives->countBest()); + state int nextAlt = g_random->randomInt(0, std::max(alternatives->size() - 1,1)); + if( nextAlt >= bestAlt ) + nextAlt++; + + if(model) { + double bestMetric = 1e9; + double nextMetric = 1e9; + double bestTime = 1e9; + double nextTime = 1e9; + for(int i=0; icountBest(); i++) { + RequestStream const* thisStream = &alternatives->get( i, channel ); + if (!IFailureMonitor::failureMonitor().getState( thisStream->getEndpoint() ).failed) { + auto& qd = model->getMeasurement(thisStream->getEndpoint().token.first()); + double thisMetric = qd.smoothOutstanding.smoothTotal(); + double thisTime = qd.latency; + + if(thisMetric < bestMetric) { + if(i != bestAlt) { + nextAlt = bestAlt; + nextMetric = bestMetric; + nextTime = bestTime; + } + bestAlt = i; + bestMetric = thisMetric; + bestTime = thisTime; + } else if( thisMetric < nextMetric ) { + nextAlt = i; + nextMetric = thisMetric; + nextTime = thisTime; + } + } + } + + if(nextTime < 1e9) { + if(bestTime > FLOW_KNOBS->INSTANT_SECOND_REQUEST_MULTIPLIER*(model->secondMultiplier*(nextTime) + FLOW_KNOBS->BASE_SECOND_REQUEST_TIME)) { + secondDelay = Void(); + } else { + secondDelay = delay( model->secondMultiplier*nextTime + FLOW_KNOBS->BASE_SECOND_REQUEST_TIME ); + } + } + else { + secondDelay = Never(); + } + } + + state int startAlt = nextAlt; + state int startDistance = (bestAlt+alternatives->size()-startAlt) % alternatives->size(); + + state int numAttempts = 0; + state double backoff = 0; + loop { + // Find an alternative, if any, that is not failed, starting with nextAlt + state RequestStream const* stream = NULL; + for(int alternativeNum=0; alternativeNumsize(); alternativeNum++) { + int useAlt = nextAlt; + if( nextAlt == startAlt ) + useAlt = bestAlt; + else if( (nextAlt+alternatives->size()-startAlt) % alternatives->size() <= startDistance ) + useAlt = (nextAlt+alternatives->size()-1) % alternatives->size(); + + stream = &alternatives->get( useAlt, channel ); + if (!IFailureMonitor::failureMonitor().getState( stream->getEndpoint() ).failed && (!firstRequestEndpoint.present() || stream->getEndpoint().token.first() != firstRequestEndpoint.get())) + break; + nextAlt = (nextAlt+1) % alternatives->size(); + stream=NULL; + } + + if(!stream && !firstRequest.isValid() ) { + // Everything is down! Wait for someone to be up. + if(now() - g_network->networkMetrics.newestAlternativesFailure > FLOW_KNOBS->ALTERNATIVES_FAILURE_RESET_TIME) { + g_network->networkMetrics.oldestAlternativesFailure = now(); + } + + double serversValidTime = alternatives->getRetrievedAt(); + double minDelay = std::min(FLOW_KNOBS->CACHE_REFRESH_INTERVAL_WHEN_ALL_ALTERNATIVES_FAILED - (now() - serversValidTime), FLOW_KNOBS->ALTERNATIVES_FAILURE_MIN_DELAY); + double delay = std::max(std::min((now()-g_network->networkMetrics.oldestAlternativesFailure)*FLOW_KNOBS->ALTERNATIVES_FAILURE_DELAY_RATIO, FLOW_KNOBS->ALTERNATIVES_FAILURE_MAX_DELAY), minDelay); + + if(serversValidTime == ALWAYS_FRESH) + delay = ALWAYS_FRESH; + + // Making this SevWarn means a lot of clutter + if(now() - g_network->networkMetrics.newestAlternativesFailure > 1 || g_random->random01() < 0.01) { + TraceEvent("AllAlternativesFailed") + .detail("Interval", FLOW_KNOBS->CACHE_REFRESH_INTERVAL_WHEN_ALL_ALTERNATIVES_FAILED) + .detail("ServersValidTime", serversValidTime) + .detail("Alternatives", alternatives->description()) + .detail("Delay", delay); + } + + g_network->networkMetrics.newestAlternativesFailure = now(); + + if (delay < 0) { + throw all_alternatives_failed(); + } + vector> ok( alternatives->size() ); + for(int i=0; iget(i, channel).getEndpoint(), FailureStatus(false) ); + choose { + when ( Void _ = wait( quorum( ok, 1 ) ) ) {} + when ( Void _ = wait( ::delayJittered( delay ) ) ) { + throw all_alternatives_failed(); + } + } + + numAttempts = 0; // now that we've got a server back, reset the backoff + } else if(!stream) { + //Only the first location is available. + Optional result = wait( firstRequest ); + if(result.present()) { + return result.get(); + } + + firstRequest = Future>(); + firstRequestEndpoint = Optional(); + } else if( firstRequest.isValid() ) { + //Issue a second request, the first one is taking a long time. + secondRequest = makeRequest(stream, request, backoff, requestFinished.getFuture(), model, false, atMostOnce); + state bool firstFinished = false; + + loop { + choose { + when(ErrorOr> result = wait( firstRequest.isValid() ? errorOr(firstRequest) : Never() )) { + if(result.isError() || result.get().present()) { + addLaggingRequest(secondRequest, requestFinished, model); + if(result.isError()) { + throw result.getError(); + } + else { + return result.get().get(); + } + } + + firstRequest = Future>(); + firstRequestEndpoint = Optional(); + firstFinished = true; + } + when(ErrorOr> result = wait( errorOr(secondRequest) )) { + if(result.isError() || result.get().present()) { + if(!firstFinished) { + addLaggingRequest(firstRequest, requestFinished, model); + } + if(result.isError()) { + throw result.getError(); + } + else { + return result.get().get(); + } + } + + break; + } + } + } + + if(++numAttempts >= alternatives->size()) { + backoff = std::min(FLOW_KNOBS->LOAD_BALANCE_MAX_BACKOFF, std::max(FLOW_KNOBS->LOAD_BALANCE_START_BACKOFF, backoff * FLOW_KNOBS->LOAD_BALANCE_BACKOFF_RATE)); + } + } else { + //Issue a request, if it takes too long to get a reply, go around the loop + firstRequest = makeRequest(stream, request, backoff, requestFinished.getFuture(), model, true, atMostOnce); + firstRequestEndpoint = stream->getEndpoint().token.first(); + + loop { + choose { + when(ErrorOr> result = wait( errorOr(firstRequest) )) { + if(model) { + model->secondMultiplier = std::max(model->secondMultiplier-FLOW_KNOBS->SECOND_REQUEST_MULTIPLIER_DECAY, 1.0); + model->secondBudget = std::min(model->secondBudget+FLOW_KNOBS->SECOND_REQUEST_BUDGET_GROWTH, FLOW_KNOBS->SECOND_REQUEST_MAX_BUDGET); + } + + if(result.isError()) { + throw result.getError(); + } + + if(result.get().present()) { + return result.get().get(); + } + + firstRequest = Future>(); + firstRequestEndpoint = Optional(); + break; + } + when(Void _ = wait(secondDelay)) { + secondDelay = Never(); + if(model && model->secondBudget >= 1.0) { + model->secondMultiplier += FLOW_KNOBS->SECOND_REQUEST_MULTIPLIER_GROWTH; + model->secondBudget -= 1.0; + break; + } + } + } + } + + if(++numAttempts >= alternatives->size()) { + backoff = std::min(FLOW_KNOBS->LOAD_BALANCE_MAX_BACKOFF, std::max(FLOW_KNOBS->LOAD_BALANCE_START_BACKOFF, backoff * FLOW_KNOBS->LOAD_BALANCE_BACKOFF_RATE)); + } + } + + nextAlt = (nextAlt+1) % alternatives->size(); + resetReply(request, taskID); + secondDelay = Never(); + } +} + +// This wrapper is just to help the compiler accept the coercesion to Reference +template +inline Future< REPLY_TYPE(Request) > loadBalance( + Reference alternatives, + RequestStream Interface::* channel, + Request request = Request(), + int taskID = TaskDefaultPromiseEndpoint, + bool atMostOnce = false, + QueueModel* model = NULL) +{ + return loadBalance( Reference>(alternatives), channel, request, taskID, atMostOnce, model ); +} + +#endif diff --git a/fdbrpc/LoadBalance.h b/fdbrpc/LoadBalance.h new file mode 100644 index 0000000000..cb1b9a117b --- /dev/null +++ b/fdbrpc/LoadBalance.h @@ -0,0 +1,21 @@ +/* + * LoadBalance.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "LoadBalance.actor.h" \ No newline at end of file diff --git a/fdbrpc/LoadPlugin.h b/fdbrpc/LoadPlugin.h new file mode 100644 index 0000000000..ba3723366d --- /dev/null +++ b/fdbrpc/LoadPlugin.h @@ -0,0 +1,32 @@ +/* + * LoadPlugin.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +template +Reference loadPlugin( std::string const& plugin_name ) { + void* plugin = loadLibrary( plugin_name.c_str() ); + void *(*get_plugin)(const char*) = (void*(*)(const char*))loadFunction( plugin, "get_plugin" ); + + if ( get_plugin ) + return Reference( (T*)get_plugin( T::get_plugin_type_name_and_version() ) ); + else + return Reference( NULL ); +} diff --git a/fdbrpc/Locality.cpp b/fdbrpc/Locality.cpp new file mode 100644 index 0000000000..4418a162d3 --- /dev/null +++ b/fdbrpc/Locality.cpp @@ -0,0 +1,126 @@ +/* + * Locality.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Locality.h" + +const UID LocalityData::UNSET_ID = UID(0x0ccb4e0feddb5583, 0x010f6b77d9d10ece); +const StringRef LocalityData::keyProcessId = LiteralStringRef("processid"); +const StringRef LocalityData::keyZoneId = LiteralStringRef("zoneid"); +const StringRef LocalityData::keyDcId = LiteralStringRef("dcid"); +const StringRef LocalityData::keyMachineId = LiteralStringRef("machineid"); +const StringRef LocalityData::keyDataHallId = LiteralStringRef("data_hall"); + +ProcessClass::Fitness ProcessClass::machineClassFitness( ClusterRole role ) { + switch( role ) { + case ProcessClass::Storage: + switch( _class ) { + case ProcessClass::StorageClass: + return ProcessClass::BestFit; + case ProcessClass::UnsetClass: + return ProcessClass::UnsetFit; + case ProcessClass::TransactionClass: + return ProcessClass::WorstFit; + case ProcessClass::LogClass: + return ProcessClass::WorstFit; + case ProcessClass::TesterClass: + return ProcessClass::NeverAssign; + default: + return ProcessClass::NeverAssign; + } + case ProcessClass::TLog: + switch( _class ) { + case ProcessClass::LogClass: + return ProcessClass::BestFit; + case ProcessClass::TransactionClass: + return ProcessClass::GoodFit; + case ProcessClass::UnsetClass: + return ProcessClass::UnsetFit; + case ProcessClass::StorageClass: + return ProcessClass::WorstFit; + case ProcessClass::TesterClass: + return ProcessClass::NeverAssign; + default: + return ProcessClass::NeverAssign; + } + case ProcessClass::Proxy: + switch( _class ) { + case ProcessClass::ProxyClass: + return ProcessClass::BestFit; + case ProcessClass::StatelessClass: + return ProcessClass::GoodFit; + case ProcessClass::ResolutionClass: + return ProcessClass::BestOtherFit; + case ProcessClass::TransactionClass: + return ProcessClass::BestOtherFit; + case ProcessClass::UnsetClass: + return ProcessClass::UnsetFit; + case ProcessClass::TesterClass: + return ProcessClass::NeverAssign; + default: + return ProcessClass::WorstFit; + } + case ProcessClass::Master: + switch( _class ) { + case ProcessClass::MasterClass: + return ProcessClass::BestFit; + case ProcessClass::StatelessClass: + return ProcessClass::GoodFit; + case ProcessClass::ResolutionClass: + return ProcessClass::BestOtherFit; + case ProcessClass::TransactionClass: + return ProcessClass::BestOtherFit; + case ProcessClass::UnsetClass: + return ProcessClass::UnsetFit; + case ProcessClass::TesterClass: + return ProcessClass::NeverAssign; + default: + return ProcessClass::WorstFit; + } + case ProcessClass::Resolver: + switch( _class ) { + case ProcessClass::ResolutionClass: + return ProcessClass::BestFit; + case ProcessClass::StatelessClass: + return ProcessClass::GoodFit; + case ProcessClass::TransactionClass: + return ProcessClass::BestOtherFit; + case ProcessClass::UnsetClass: + return ProcessClass::UnsetFit; + case ProcessClass::TesterClass: + return ProcessClass::NeverAssign; + default: + return ProcessClass::WorstFit; + } + default: + return ProcessClass::NeverAssign; + } +} + +LBDistance::Type loadBalanceDistance( LocalityData const& loc1, LocalityData const& loc2, NetworkAddress const& addr2 ) { + if ( loc1.zoneId().present() && loc1.zoneId() == loc2.zoneId() ) + return LBDistance::SAME_MACHINE; + + //FIXME: add this back in when load balancing works with local requests + //if ( g_network->isAddressOnThisHost( addr2 ) ) + // return LBDistance::SAME_MACHINE; + if ( loc1.dcId().present() && loc1.dcId() == loc2.dcId() ) + return LBDistance::SAME_DC; + return LBDistance::DISTANT; +} diff --git a/fdbrpc/Locality.h b/fdbrpc/Locality.h new file mode 100644 index 0000000000..bafae3f90b --- /dev/null +++ b/fdbrpc/Locality.h @@ -0,0 +1,286 @@ +/* + * Locality.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_LOCALITY_H +#define FLOW_LOCALITY_H +#pragma once + +#include "flow/flow.h" + +struct ProcessClass { + // This enum is stored in restartInfo.ini for upgrade tests, so be very careful about changing the existing items! + enum ClassType { UnsetClass, StorageClass, TransactionClass, ResolutionClass, TesterClass, ProxyClass, MasterClass, StatelessClass, LogClass, InvalidClass = -1 }; + enum Fitness { BestFit, GoodFit, BestOtherFit, UnsetFit, WorstFit, NeverAssign }; + enum ClusterRole { Storage, TLog, Proxy, Master, Resolver }; + enum ClassSource { CommandLineSource, AutoSource, DBSource, InvalidSource = -1 }; + int16_t _class; + int16_t _source; + +public: + ProcessClass() : _class( UnsetClass ), _source( CommandLineSource ) {} + ProcessClass( ClassType type, ClassSource source ) : _class( type ), _source( source ) {} + explicit ProcessClass( std::string s, ClassSource source ) : _source( source ) { + if (s=="storage") _class = StorageClass; + else if (s=="transaction") _class = TransactionClass; + else if (s=="resolution") _class = ResolutionClass; + else if (s=="proxy") _class = ProxyClass; + else if (s=="master") _class = MasterClass; + else if (s=="test") _class = TesterClass; + else if (s=="unset") _class = UnsetClass; + else if (s=="stateless") _class = StatelessClass; + else if (s=="log") _class = LogClass; + else _class = InvalidClass; + } + + ProcessClass( std::string classStr, std::string sourceStr ) { + if (classStr=="storage") _class = StorageClass; + else if (classStr=="transaction") _class = TransactionClass; + else if (classStr=="resolution") _class = ResolutionClass; + else if (classStr=="proxy") _class = ProxyClass; + else if (classStr=="master") _class = MasterClass; + else if (classStr=="test") _class = TesterClass; + else if (classStr=="unset") _class = UnsetClass; + else if (classStr=="stateless") _class = StatelessClass; + else if (classStr=="log") _class = LogClass; + else _class = InvalidClass; + + if (sourceStr=="command_line") _source = CommandLineSource; + else if (sourceStr=="configure_auto") _source = AutoSource; + else if (sourceStr=="set_class") _source = DBSource; + else _source = InvalidSource; + } + + ClassType classType() const { return (ClassType)_class; } + ClassSource classSource() const { return (ClassSource)_source; } + + bool operator == ( const ClassType& rhs ) const { return _class == rhs; } + bool operator != ( const ClassType& rhs ) const { return _class != rhs; } + + bool operator == ( const ProcessClass& rhs ) const { return _class == rhs._class && _source == rhs._source; } + bool operator != ( const ProcessClass& rhs ) const { return _class != rhs._class || _source != rhs._source; } + + std::string toString() const { + switch (_class) { + case UnsetClass: return "unset"; + case StorageClass: return "storage"; + case TransactionClass: return "transaction"; + case ResolutionClass: return "resolution"; + case ProxyClass: return "proxy"; + case MasterClass: return "master"; + case TesterClass: return "test"; + case StatelessClass: return "stateless"; + case LogClass: return "log"; + default: return "invalid"; + } + } + + std::string sourceString() const { + switch (_source) { + case CommandLineSource: return "command_line"; + case AutoSource: return "configure_auto"; + case DBSource: return "set_class"; + default: return "invalid"; + } + } + + Fitness machineClassFitness( ClusterRole role ); + + template + void serialize(Ar& ar) { + ar & _class & _source; + } +}; + +struct LocalityData { + std::map, Optional>> _data; + + static const StringRef keyProcessId; + static const StringRef keyZoneId; + static const StringRef keyDcId; + static const StringRef keyMachineId; + static const StringRef keyDataHallId; + +public: + LocalityData() {} + + LocalityData(Optional> processID, Optional> zoneID, Optional> MachineID, Optional> dcID ) { + _data[keyProcessId] = processID; + _data[keyZoneId] = zoneID; + _data[keyMachineId] = MachineID; + _data[keyDcId] = dcID; + } + + bool operator == (LocalityData const& rhs) const { + return ((_data.size() == rhs._data.size()) && + (std::equal(_data.begin(), _data.end(), rhs._data.begin()))); + } + + Optional> get(StringRef key) const { + auto pos = _data.find(key); + return (pos == _data.end()) ? Optional>() : pos->second; + } + + void set(StringRef key, Optional> value) { + _data[key] = value; + } + + bool isPresent(StringRef key) const { return (_data.find(key) != _data.end()); } + bool isPresent(StringRef key, Optional> value) const { + auto pos = _data.find(key); + return (pos != _data.end()) ? false : (pos->second == value); + } + + std::string describeValue(StringRef key) const { + auto value = get(key); + return (value.present()) ? value.get().toString() : "[unset]"; + } + + std::string describeZone() const { return describeValue(keyZoneId); } + std::string describeDataHall() const { return describeValue(keyDataHallId); } + + Optional> processId() const { return get(keyProcessId); } + Optional> zoneId() const { return get(keyZoneId); } + Optional> machineId() const { return get(keyMachineId); } + Optional> dcId() const { return get(keyDcId); } + Optional> dataHallId() const { return get(keyDataHallId); } + + std::string toString() const { + std::string infoString; + for (auto it = _data.rbegin(); !(it == _data.rend()); ++it) { + if (infoString.length()) { infoString += " "; } + infoString += it->first.printable() + "="; + infoString += (it->second.present()) ? it->second.get().printable() : "[unset]"; + } + return infoString; + } + + template + void serialize(Ar& ar) { + // Locality is persisted in the database inside StorageServerInterface, so changes here have to be + // versioned carefully! + if (ar.protocolVersion() >= 0x0FDB00A446020001LL) { + Standalone key; + Optional> value; + uint64_t mapSize = (uint64_t)_data.size(); + ar & mapSize; + if (ar.isDeserializing) { + for (size_t i = 0; i < mapSize; i++) { + ar & key & value; + _data[key] = value; + } + } + else { + for (auto it = _data.begin(); it != _data.end(); it++) { + key = it->first; + value = it->second; + ar & key & value; + } + } + } + else { + ASSERT(ar.isDeserializing); + UID zoneId, dcId, processId; + ar & zoneId & dcId; + set(keyZoneId, Standalone(zoneId.toString())); + set(keyDcId, Standalone(dcId.toString())); + + if (ar.protocolVersion() >= 0x0FDB00A340000001LL) { + ar & processId; + set(keyProcessId, Standalone(processId.toString())); + } + else { + int _machineClass = ProcessClass::UnsetClass; + ar & _machineClass; + } + } + } + + static const UID UNSET_ID; +}; + +static std::string describe( + std::vector const& items, + StringRef const key, + int max_items = -1 ) +{ + if(!items.size()) + return "[no items]"; + std::string s; + int count = 0; + for(auto const& item : items) { + if( ++count > max_items && max_items >= 0) + break; + if (count > 1) s += ","; + s += item.describeValue(key); + } + return s; +} +static std::string describeZones( std::vector const& items, int max_items = -1 ) { + return describe(items, LocalityData::keyZoneId, max_items); +} +static std::string describeDataHalls( std::vector const& items, int max_items = -1 ) { + return describe(items, LocalityData::keyDataHallId, max_items); +} + +struct ProcessData { + LocalityData locality; + ProcessClass processClass; + NetworkAddress address; + + ProcessData() {} + ProcessData( LocalityData locality, ProcessClass processClass, NetworkAddress address ) : locality(locality), processClass(processClass), address(address) {} + + template + void serialize(Ar& ar) { + ar & locality & processClass & address; + } + + struct sort_by_address { + bool operator ()(ProcessData const&a, ProcessData const& b) const { return a.address < b.address; } + }; +}; + +template +struct LBLocalityData { + enum { Present = 0 }; + static LocalityData getLocality( Interface const& ) { return LocalityData(); } + static NetworkAddress getAddress( Interface const& ) { return NetworkAddress(); } +}; + +// Template specialization that only works for interfaces with a .locality member. +// If an interface has a .locality it must also have a .address() +template +struct LBLocalityData::type> { + enum { Present = 1 }; + static LocalityData getLocality( Interface const& i ) { return i.locality; } + static NetworkAddress getAddress( Interface const& i ) { return i.address(); } +}; + +struct LBDistance { + enum Type { + SAME_MACHINE = 0, + SAME_DC = 1, + DISTANT = 2 + }; +}; + +LBDistance::Type loadBalanceDistance( LocalityData const& localLoc, LocalityData const& otherLoc, NetworkAddress const& otherAddr ); + +#endif diff --git a/fdbrpc/MultiInterface.h b/fdbrpc/MultiInterface.h new file mode 100644 index 0000000000..e022362755 --- /dev/null +++ b/fdbrpc/MultiInterface.h @@ -0,0 +1,102 @@ +/* + * MultiInterface.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_MULTIINTERFACE_H +#define FLOW_MULTIINTERFACE_H +#pragma once + +#define ALWAYS_FRESH 1e99 + +extern uint64_t debug_lastLoadBalanceResultEndpointToken; + +template +struct KVPair { + // KVPair is ordered only by K and described by V + K k; + V v; + KVPair() {} + KVPair( K const& k, V const& v ) : k(k), v(v) {} + KVPair(K && k, V && v) : k(std::move(k)), v(std::move(v)) {} +}; +template bool operator < ( KVPair const& l, KVPair const& r ) { return l.k < r.k; } +template bool operator < ( KVPair const& l, K const& r ) { return l.k < r; } +template bool operator < ( K const& l, KVPair const& r ) { return l < r.k; } + +template +std::string describe( KVPair const& p ) { return format("%d ", p.k) + describe(p.v); } + +template +class MultiInterface : public ReferenceCounted> { +public: + MultiInterface( const vector& v, LocalityData const& locality = LocalityData(), double timeNow = now() ) : retrievedAt( timeNow ), bestCount(0) { + for(int i=0; i(LBDistance::DISTANT,v[i])); + g_random->randomShuffle(alternatives); + if ( LBLocalityData::Present ) { + for(int a=0; a::getLocality( alternatives[a].v ), LBLocalityData::getAddress( alternatives[a].v ) ); + std::stable_sort( alternatives.begin(), alternatives.end() ); + } + if(size()) + bestCount = std::lower_bound( alternatives.begin()+1, alternatives.end(), alternatives[0].k+1 ) - alternatives.begin(); + } + + int size() const { return alternatives.size(); } + int countBest() const { + return bestCount; + } + LBDistance::Type bestDistance() const { + if( !size() ) + return LBDistance::DISTANT; + return (LBDistance::Type) alternatives[0].k; + } + + template + F const& get( int index, F T::*member ) const { + return alternatives[index].v.*member; + } + + T const& getInterface(int index) { return alternatives[index].v; } + UID getId( int index ) const { return alternatives[index].v.id(); } + + //vector const& get() { return alternatives; } + double getRetrievedAt() const { return retrievedAt; } + + virtual ~MultiInterface() {} + +// void alwaysFresh() { retrievedAt = FLOW_KNOBS->ALWAYS_FRESH; } +// void freshen() { retrievedAt = now(); } + + std::string description() { + return describe( alternatives ); + } + +protected: + vector> const& getAlternatives() { return alternatives; } + +private: + vector> alternatives; + double retrievedAt; + int bestCount; +}; + +template void load(Ar& ar, Reference>&) { ASSERT(false); } //< required for Future + +#endif \ No newline at end of file diff --git a/fdbrpc/Net2FileSystem.cpp b/fdbrpc/Net2FileSystem.cpp new file mode 100644 index 0000000000..5c661b79ee --- /dev/null +++ b/fdbrpc/Net2FileSystem.cpp @@ -0,0 +1,100 @@ +/* + * Net2FileSystem.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "Net2FileSystem.h" + +// Define boost::asio::io_service +#include +#define BOOST_SYSTEM_NO_LIB +#define BOOST_DATE_TIME_NO_LIB +#define BOOST_REGEX_NO_LIB +#include +#include + +#define FILESYSTEM_IMPL 1 + +#include "AsyncFileCached.actor.h" +#include "AsyncFileEIO.actor.h" +#include "AsyncFileWinASIO.actor.h" +#include "AsyncFileKAIO.actor.h" +#include "flow/AsioReactor.h" +#include "flow/Platform.h" + +// Opens a file for asynchronous I/O +Future< Reference > Net2FileSystem::open( std::string filename, int64_t flags, int64_t mode ) +{ +#ifdef __linux__ + if (checkFileSystem) { + dev_t fileDeviceId = getDeviceId(filename); + if (fileDeviceId != this->fileSystemDeviceId) { + TraceEvent(SevError, "DeviceIdMismatched").detail("FileSystemDeviceId", this->fileSystemDeviceId).detail("FileDeviceId", fileDeviceId); + throw io_error(); + } + } +#endif + + if ( (flags & IAsyncFile::OPEN_EXCLUSIVE) ) ASSERT( flags & IAsyncFile::OPEN_CREATE ); + if (!(flags & IAsyncFile::OPEN_UNCACHED)) + return AsyncFileCached::open(filename, flags, mode); +#ifdef __linux__ + if ( (flags & IAsyncFile::OPEN_UNBUFFERED) && !(flags & IAsyncFile::OPEN_NO_AIO) ) + return AsyncFileKAIO::open(filename, flags, mode, NULL); +#endif + + return Net2AsyncFile::open(filename, flags, mode, static_cast ((void*) g_network->global(INetwork::enASIOService))); +} + +// Deletes the given file. If mustBeDurable, returns only when the file is guaranteed to be deleted even after a power failure. +Future< Void > Net2FileSystem::deleteFile( std::string filename, bool mustBeDurable ) +{ + return Net2AsyncFile::deleteFile(filename, mustBeDurable); +} + +void Net2FileSystem::newFileSystem(double ioTimeout, std::string fileSystemPath) +{ + g_network->setGlobal(INetwork::enFileSystem, (flowGlobalType) new Net2FileSystem(ioTimeout, fileSystemPath)); +} + +Net2FileSystem::Net2FileSystem(double ioTimeout, std::string fileSystemPath) +{ + Net2AsyncFile::init(); +#ifdef __linux__ + AsyncFileKAIO::init( Reference(N2::ASIOReactor::getEventFD()), ioTimeout ); + + if (fileSystemPath.empty()) { + checkFileSystem = false; + } else { + checkFileSystem = true; + + try { + this->fileSystemDeviceId = getDeviceId(fileSystemPath); + if (fileSystemPath != "/") { + dev_t fileSystemParentDeviceId = getDeviceId(parentDirectory(fileSystemPath)); + if (this->fileSystemDeviceId == fileSystemParentDeviceId) { + criticalError(FDB_EXIT_ERROR, "FileSystemError", format("`%s' is not a mount point", fileSystemPath.c_str()).c_str()); + } + } + } catch (Error& e) { + criticalError(FDB_EXIT_ERROR, "FileSystemError", format("Could not get device id from `%s'", fileSystemPath.c_str()).c_str()); + } + } +#endif +} diff --git a/fdbrpc/Net2FileSystem.h b/fdbrpc/Net2FileSystem.h new file mode 100644 index 0000000000..4542c9b03c --- /dev/null +++ b/fdbrpc/Net2FileSystem.h @@ -0,0 +1,49 @@ +/* + * Net2FileSystem.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_NET2FILESYSTEM_H +#define FLOW_NET2FILESYSTEM_H +#pragma once + +#include "IAsyncFile.h" + +class Net2FileSystem : public IAsyncFileSystem { +public: + virtual Future< Reference > open( std::string filename, int64_t flags, int64_t mode ); + // Opens a file for asynchronous I/O + + virtual Future< Void > deleteFile( std::string filename, bool mustBeDurable ); + // Deletes the given file. If mustBeDurable, returns only when the file is guaranteed to be deleted even after a power failure. + + //void init(); + + Net2FileSystem(double ioTimeout=0.0, std::string fileSystemPath = ""); + + virtual ~Net2FileSystem() {} + + static void newFileSystem(double ioTimeout=0.0, std::string fileSystemPath = ""); + +#ifdef __linux__ + dev_t fileSystemDeviceId; + bool checkFileSystem; +#endif +}; + +#endif diff --git a/fdbrpc/PerfMetric.h b/fdbrpc/PerfMetric.h new file mode 100644 index 0000000000..f0eb262371 --- /dev/null +++ b/fdbrpc/PerfMetric.h @@ -0,0 +1,109 @@ +/* + * PerfMetric.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_PERFMETRIC_H +#define FLOW_PERFMETRIC_H +#pragma once + +#include +#include +#include "flow/flow.h" + +using std::vector; + +struct PerfMetric { + PerfMetric() : m_name(""), m_value(0), m_averaged(false), m_format_code( "%.3g" ) {} + PerfMetric( std::string name, double value, bool averaged ) : m_name(name), m_value(value), m_averaged(averaged), m_format_code( "%.3g" ) {} + PerfMetric( std::string name, double value, bool averaged, std::string format_code ) : m_name(name), m_value(value), m_averaged(averaged), m_format_code(format_code) {} + + std::string name() const { return m_name; } + double value() const { return m_value; } + std::string formatted() const { return format(m_format_code.c_str(), m_value); } + std::string format_code() const { return m_format_code; } + bool averaged() const { return m_averaged; } + + PerfMetric withPrefix( const std::string& pre ) { return PerfMetric(pre+name(), value(), averaged(), format_code()); } + + template + void serialize( Ar& ar ) { + ar & m_name & m_format_code & m_value & m_averaged; + } + +private: + std::string m_name, m_format_code; + double m_value; + bool m_averaged; +}; + +struct PerfIntCounter { + PerfIntCounter(std::string name) : name(name), value(0) {} + PerfIntCounter(std::string name, vector& v) : name(name), value(0) { v.push_back(this); } + void operator += (int64_t delta) { value += delta; } + void operator ++ () { value += 1; } + PerfMetric getMetric() { return PerfMetric( name, (double)value, false, "%.0lf" ); } + int64_t getValue() { return value; } + void clear() { value = 0; } + +private: + std::string name; + int64_t value; +}; + +struct PerfDoubleCounter { + PerfDoubleCounter(std::string name) : name(name), value(0) {} + PerfDoubleCounter(std::string name, vector& v) : name(name), value(0) { v.push_back(this); } + void operator += (double delta) { value += delta; } + void operator ++ () { value += 1.0; } + PerfMetric getMetric() { return PerfMetric( name, value, false ); } + double getValue() { return value; } + void clear() { value = 0.0; } + +private: + std::string name; + double value; +}; + +struct GlobalCounters { + vector ints; + vector doubles; + + PerfDoubleCounter conflictTime; + PerfIntCounter conflictBatches; + PerfIntCounter conflictKeys; + PerfIntCounter conflictTransactions; + + GlobalCounters() : + conflictTime("Conflict detection time", doubles), + conflictBatches("Conflict batches", ints), + conflictKeys("Conflict keys", ints), + conflictTransactions("Conflict transactions", ints) + { + } + void clear() { + for(int i=0; iclear(); + for(int i=0; iclear(); + } +}; + +extern GlobalCounters g_counters; + +#endif \ No newline at end of file diff --git a/fdbrpc/Platform.cpp b/fdbrpc/Platform.cpp new file mode 100644 index 0000000000..d04781bd5f --- /dev/null +++ b/fdbrpc/Platform.cpp @@ -0,0 +1,158 @@ +/* + * Platform.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Platform.h" +#include +#include "flow/ActorCollection.h" +#include "flow/FaultInjection.h" + + +#ifdef _WIN32 +#include +#undef max +#undef min +#include +#include +#include +#include +#include +#include +#pragma comment(lib, "pdh.lib") + +// for SHGetFolderPath +#include +#pragma comment(lib, "Shell32.lib") + +#define CANONICAL_PATH_SEPARATOR '\\' +#endif + +#ifdef __unixish__ +#define CANONICAL_PATH_SEPARATOR '/' + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __APPLE__ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#endif + +#endif + + + +extern bool onlyBeforeSimulatorInit(); + +namespace platform { + +void eraseDirectoryRecursive( std::string const& dir ) { + // Fault injection is disabled for this function because it is ONLY used by the simulator + // and only before the actual simulation! + ASSERT( onlyBeforeSimulatorInit() ); + INJECT_FAULT( platform_error, "eraseDirectoryRecursive" ); +#ifdef _WIN32 + system( ("rd /s /q \"" + dir + "\"").c_str() ); +#elif defined(__linux__) + int error = + nftw(dir.c_str(), + [](const char *fpath, const struct stat *sb, int typeflag, + struct FTW *ftwbuf) -> int { + if (remove(fpath)) + return FTW_STOP; + return FTW_CONTINUE; + }, 64, FTW_DEPTH | FTW_PHYS | FTW_ACTIONRETVAL); + /* Looks like calling code expects this to continue silently if + the directory we're deleting doesn't exist in the first + place */ + if (error && errno != ENOENT) { + TraceEvent(SevError, "nftw").detail("Directory", dir).GetLastError(); + throw platform_error(); + } +#elif defined(__APPLE__) + // const char* argv[2]; + // argv[0] = dir.c_str(); + // argv[1] = NULL; + // FTS* fts = fts_open(argv, FTS_PHYSICAL | FTS_SEEDOT | FTS_NOSTAT, NULL); + // while (FTSENT* ent = fts_read(fts)) { + // if (ent->fts_info + // } +#else +#error Port me! +#endif + //INJECT_FAULT( platform_error, "eraseDirectoryRecursive" ); +} + +std::string getDefaultConfigPath() { +#ifdef _WIN32 + TCHAR szPath[MAX_PATH]; + if( SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, szPath) != S_OK ) { + TraceEvent(SevError, "WindowsAppDataError").GetLastError(); + throw platform_error(); + } + std::string _filepath(szPath); + return _filepath + "\\foundationdb"; +#elif defined(__linux__) + return "/etc/foundationdb"; +#elif defined(__APPLE__) + return "/usr/local/etc/foundationdb"; +#else + #error Port me! +#endif +} + +bool isSse42Supported() +{ +#if defined(_WIN32) + int info[4]; + __cpuid(info, 1); + return (info[2] & (1 << 20)) != 0; +#elif defined(__unixish__) + uint32_t eax, ebx, ecx, edx, level = 1, count = 0; + __cpuid_count(level, count, eax, ebx, ecx, edx); + return ((ecx >> 20) & 1) != 0; +#else + #error Port me! +#endif +} + +std::string getDefaultClusterFilePath() { + return joinPath(platform::getDefaultConfigPath(), "fdb.cluster"); +} +}; // namespace platform diff --git a/fdbrpc/Platform.h b/fdbrpc/Platform.h new file mode 100644 index 0000000000..8afec3f86f --- /dev/null +++ b/fdbrpc/Platform.h @@ -0,0 +1,43 @@ +/* + * Platform.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBRPC_UTILS_H +#define FDBRPC_UTILS_H +#pragma once + +#include "flow/Platform.h" +#include + +namespace platform { + +// Avoid in production code: not atomic, not fast, not reliable in all environments +void eraseDirectoryRecursive( std::string const& directory ); + +// Returns the absolute platform-dependant path for the default fdb.cluster file +std::string getDefaultClusterFilePath(); + +// Returns the absolute platform-dependant path for server-based files +std::string getDefaultConfigPath(); + +bool isSse42Supported(); + +}; // namespace platform + +#endif diff --git a/fdbrpc/QueueModel.cpp b/fdbrpc/QueueModel.cpp new file mode 100644 index 0000000000..645eb1a23f --- /dev/null +++ b/fdbrpc/QueueModel.cpp @@ -0,0 +1,86 @@ +/* + * QueueModel.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "QueueModel.h" +#include "LoadBalance.h" + +void QueueModel::endRequest( uint64_t id, double latency, double penalty, double delta, bool clean ) { + auto& d = data[id]; + d.smoothOutstanding.addDelta(-delta); + + if(clean) { + d.latency = latency; + } else { + d.latency = std::max(d.latency, latency); + } + + if(penalty > 0) { + d.penalty = penalty; + } +} + +QueueData& QueueModel::getMeasurement( uint64_t id ) { + return data[id]; +} + +double QueueModel::addRequest( uint64_t id ) { + auto& d = data[id]; + d.smoothOutstanding.addDelta(d.penalty); + return d.penalty; +} + +Optional getLoadBalancedReply(LoadBalancedReply *reply) { + return *reply; +} + +Optional getLoadBalancedReply(void*) { + return Optional(); +} + +/* +void QueueModel::addMeasurement( uint64_t id, QueueDetails qd ){ + if (data[new_index].count(id)) + total_time[new_index] -= data[new_index][id].queryQueueSize; + data[new_index][id] = qd; + total_time[new_index] += qd.queryQueueSize; +} + +TimeEstimate QueueModel::getTimeEstimate( uint64_t id ){ + if (data[new_index].count(id)) // give the current estimate + return data[new_index][id].queryQueueSize; + else if (data[1-new_index].count(id)) // if not, old estimate + return data[1-new_index][id].queryQueueSize; + else // if not, the average? + return getAverageTimeEstimate(); +} + +TimeEstimate QueueModel::getAverageTimeEstimate(){ + if(data[new_index].size() + data[1-new_index].size() > 0) + return (total_time[new_index] + total_time[1-new_index]) / (data[new_index].size() + data[1-new_index].size()); + return 0; +} + +void QueueModel::expire(){ + data[1-new_index].clear(); + total_time[1-new_index] = 0; + + new_index = 1-new_index; +} +*/ diff --git a/fdbrpc/QueueModel.h b/fdbrpc/QueueModel.h new file mode 100644 index 0000000000..05fea75a82 --- /dev/null +++ b/fdbrpc/QueueModel.h @@ -0,0 +1,82 @@ +/* + * QueueModel.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_QUEUEMODEL_H +#define FLOW_QUEUEMODEL_H +#pragma once + +#include "flow/flow.h" +#include "Smoother.h" +#include "flow/Knobs.h" +#include "flow/ActorCollection.h" + + +struct QueueData { + Smoother smoothOutstanding; + double latency; + double penalty; + QueueData() : latency(0.001), penalty(1.0), smoothOutstanding(FLOW_KNOBS->QUEUE_MODEL_SMOOTHING_AMOUNT) {} +}; + +typedef double TimeEstimate; + +class QueueModel { +public: + void endRequest( uint64_t id, double latency, double penalty, double delta, bool clean ); + QueueData& getMeasurement( uint64_t id ); + double addRequest( uint64_t id ); + double secondMultiplier; + double secondBudget; + PromiseStream< Future > addActor; + Future laggingRequests; // requests for which a different recipient already answered + int laggingRequestCount; + + QueueModel() : secondMultiplier(1.0), secondBudget(0), laggingRequestCount(0) { + laggingRequests = actorCollection( addActor.getFuture(), &laggingRequestCount ); + } + + ~QueueModel() { + laggingRequests.cancel(); + } +private: + std::map data; +}; + +/* old queue model +class QueueModel { +public: + QueueModel() : new_index(0) { + total_time[0] = 0; + total_time[1] = 0; + } + void addMeasurement( uint64_t id, QueueDetails qd ); + TimeEstimate getTimeEstimate( uint64_t id ); + TimeEstimate getAverageTimeEstimate(); + QueueDetails getMeasurement( uint64_t id ); + void expire(); + +private: + std::map data[2]; + double total_time[2]; + int new_index; // data[new_index] is the new data +}; +*/ + +#endif \ No newline at end of file diff --git a/fdbrpc/RangeMap.h b/fdbrpc/RangeMap.h new file mode 100644 index 0000000000..27481732f3 --- /dev/null +++ b/fdbrpc/RangeMap.h @@ -0,0 +1,248 @@ +/* + * RangeMap.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_RANGEMAP_H +#define FLOW_RANGEMAP_H +#pragma once + +#include "flow/flow.h" +#include +#include "flow/IndexedSet.h" + +using boost::iterator_range; + +template +class RangeMapRange { +public: + Key begin, end; + RangeMapRange( Key const& begin, Key const& end ) : begin(begin), end(end) {} +}; + +template +RangeMapRange rangeMapRange( Key const& begin, Key const& end ) { return RangeMapRange(begin,end); } + +template +struct ConstantMetric { + template + Metric operator()(pair_type const& p) const { return Metric(1); } +}; + +template +struct KeyBytesMetric { + template + Metric operator()(pair_type const& p) const { return Metric(p.key.size() + sizeof(pair_type)); } +}; + +template +struct ValueBytesMetric { + template + Metric operator()(pair_type const& p) const { return Metric(p.value.size() + sizeof(pair_type)); } +}; + +template +struct KeyValueBytesMetric { + template + Metric operator()(pair_type const& p) const { return Metric(p.key.size() + p.value.size() + sizeof(pair_type)); } +}; + +template , class Metric = int, class MetricFunc = ConstantMetric> +class RangeMap { +private: + typedef MapPair pair_type; +public: + //Applications may decrement an iterator before ranges begin, or increment after ranges end, but once in this state cannot do further incrementing or decrementing + class Iterator { + public: + Iterator() {} // singular + Iterator( typename Map::iterator it ) : it(it) {} + + Key const& begin() { return it->key; } + Key const& end() { auto j = it; ++j; return j->key; } + + Range range() { return Range(begin(),end()); } + + Val& value() { + //ASSERT( it->key != allKeys.end ); + return it->value; + } + + void operator ++() { ++it; } + void operator --() { it.decrementNonEnd(); } + bool operator ==(Iterator const& r) const { return it == r.it; } + bool operator !=(Iterator const& r) const { return it != r.it; } + + // operator* and -> return this + Iterator& operator*() { return *this; } + Iterator* operator->() { return this; } + + typedef std::forward_iterator_tag iterator_category; + typedef Iterator value_type; + typedef int difference_type; + typedef Iterator* pointer; + typedef Iterator& reference; + private: + typename Map::iterator it; + }; + typedef iterator_range Ranges; + + explicit RangeMap(Key endKey, Val v=Val(), MetricFunc m = MetricFunc()) : mf(m) { + Key beginKey = Key(); + pair_type beginPair(beginKey, v); + map.insert(beginPair, true, mf(beginPair)); + pair_type endPair(endKey, Val()); + map.insert(endPair, true, mf(endPair)); + } + Val const& operator[]( const Key& k ) { return rangeContaining(k).value(); } + + Ranges ranges() { return Ranges( Iterator(map.begin()), Iterator(map.lastItem()) ); } + Ranges intersectingRanges( const Range& r ) { return Ranges(rangeContaining(r.begin), Iterator(map.lower_bound(r.end))); } + // containedRanges() will return all ranges that are fully contained by the passed range (note that a range fully contains itself) + Ranges containedRanges( const Range& r ) { + auto s = Iterator( map.lower_bound( r.begin ) ); + if ( s.begin() >= r.end ) return Ranges(s,s); + return Ranges(s, rangeContaining(r.end)); + } + template + Iterator rangeContaining( const ComparableToKey& k ) { + return Iterator(map.lastLessOrEqual(k)); + } + // Returns the range containing a key infinitesimally before k, or the first range if k==Key() + template + Iterator rangeContainingKeyBefore( const ComparableToKey& k ) { + Iterator i = map.lower_bound(k); + if ( !i->begin().size() ) return i; + --i; + return i; + } + Iterator lastItem() { + auto i = map.lastItem(); + i.decrementNonEnd(); + return Iterator(i); + } + int size() const { return map.size() - 1; } // We always have one range bounded by two entries + Iterator randomRange() { + return Iterator( map.index( g_random->randomInt(0, map.size()-1) ) ); + } + Iterator nthRange(int n) { return Iterator(map.index(n)); } + + bool allEqual( const Range& r, const Val& v ); + + template + void coalesce( const ComparableToKey& k ); + void coalesce( const Range& k ); + void validateCoalesced(); + + void operator=(RangeMap&& r) noexcept(true) { map = std::move(r.map); } + //void clear( const Val& value ) { ranges.clear(); ranges.insert(std::make_pair(Key(),value)); } + + void insert( const Range& keys, const Val& value ); + +protected: + Map map; + const MetricFunc mf; +}; + +template +template +void RangeMap::coalesce( const ComparableToKey& k ) { + auto begin = map.lastLessOrEqual(k); + auto end = begin; + const Val& compareVal = begin->value; + ASSERT( begin != map.end() ); + while( begin != map.begin() && begin->value == compareVal ) + begin.decrementNonEnd(); + while( end != map.lastItem() && end->value == compareVal ) + ++end; + if( begin->value != compareVal ) { + ++begin; + if( begin == end ) return; + } + ++begin; + map.erase(begin, end); +} + +template +void RangeMap::coalesce( const Range& k ) { + coalesce(k.begin); + auto it = map.lastLessOrEqual(k.begin); + Val* lastVal = &it->value; + ++it; + if( it == map.end() ) + return; + bool doCheck = true; + while( it != map.lastItem() && doCheck ) { + doCheck = it->key < k.end; + if( it->value == *lastVal ) { + doCheck = true; + auto begin = it; + ++it; + map.erase(begin, it); + } + else { + lastVal = &it->value; + ++it; + } + } + if( EXPENSIVE_VALIDATION ) + validateCoalesced(); +} + +template +void RangeMap::validateCoalesced() { + auto it = map.begin(); + Val* lastVal = &it->value; + ++it; + auto end = map.lastItem(); + for( ; it != end; ++it ) { + ASSERT( it->value != *lastVal ); + lastVal = &it->value; + } +} + +template +bool RangeMap::allEqual( const Range& keys, const Val& val ) { + auto r = intersectingRanges(keys); + for(auto i=r.begin(); i!=r.end(); ++i) + if (i.value() != val) + return false; + return true; +} + +template +void RangeMap::insert( const Range& keys, const Val& value ) { + if(keys.begin == keys.end) + return; + + auto end = map.lower_bound( keys.end ); + if( end->key != keys.end ) { + end.decrementNonEnd(); + const Val& valueAfterRange = end->value; + pair_type endPair(keys.end, valueAfterRange); + end = map.insert(endPair, true, mf(endPair)); + } + + auto begin = map.lower_bound( keys.begin ); + + map.erase(begin, end); + pair_type beginPair(keys.begin, value); + map.insert(beginPair, true, mf(beginPair)); +} + +#endif \ No newline at end of file diff --git a/fdbrpc/Replication.cpp b/fdbrpc/Replication.cpp new file mode 100644 index 0000000000..04b86ef3c6 --- /dev/null +++ b/fdbrpc/Replication.cpp @@ -0,0 +1,21 @@ +/* + * Replication.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Replication.h" diff --git a/fdbrpc/Replication.h b/fdbrpc/Replication.h new file mode 100644 index 0000000000..fffa4e5ae3 --- /dev/null +++ b/fdbrpc/Replication.h @@ -0,0 +1,655 @@ +/* + * Replication.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_REPLICATION_H +#define FLOW_REPLICATION_H +#pragma once + +#include "flow/flow.h" +#include "Locality.h" +#include "ReplicationPolicy.h" + +struct LocalitySet : public ReferenceCounted { +public: + LocalitySet(LocalitySet const& source):_entryArray(source._entryArray), _mutableEntryArray(source._mutableEntryArray), _keyValueArray(source._keyValueArray), _keyIndexArray(source._keyIndexArray), _cacheArray(source._cacheArray), _keymap(source._keymap), _localitygroup(source._localitygroup), _cachehits(source._cachehits), _cachemisses(source._cachemisses) {} + LocalitySet(LocalitySet& localityGroup):_localitygroup(&localityGroup), _keymap(new StringToIntMap()), _cachehits(0), _cachemisses(0) {} + virtual ~LocalitySet() {} + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + bool selectReplicas( + IRepPolicyRef const& policy, + std::vector const& alsoServers, + std::vector & results) + { + LocalitySetRef fromServers = LocalitySetRef::addRef(this); + return policy->selectReplicas(fromServers, alsoServers, results); + } + + bool selectReplicas( + IRepPolicyRef const& policy, + std::vector & results) + { return selectReplicas(policy, std::vector(), results); } + + bool validate( + IRepPolicyRef const& policy) const + { + LocalitySetRef const solutionSet = LocalitySetRef::addRef((LocalitySet*) this); + return policy->validate(solutionSet); + } + + virtual void clear() { + _entryArray.clear(); + _mutableEntryArray.clear(); + _keyValueArray.clear(); + _keyIndexArray.clear(); + _cacheArray.clear(); + _keymap->clear(); + } + + LocalitySet & copy(LocalitySet const& source) { + _entryArray = source._entryArray; + _mutableEntryArray = source._mutableEntryArray; + _keyValueArray = source._keyValueArray; + _keyIndexArray = source._keyIndexArray; + _cacheArray = source._cacheArray; + _cachehits = source._cachehits; + _cachemisses = source._cachemisses; + _keymap = source._keymap; + _localitygroup = (LocalitySet*) source._localitygroup; + return *this; + } + + LocalitySet & deep_copy(LocalitySet const& source) { + _entryArray = source._entryArray; + _mutableEntryArray = source._mutableEntryArray; + _keyValueArray = source._keyValueArray; + _keyIndexArray = source._keyIndexArray; + _cacheArray = source._cacheArray; + _cachehits = source._cachehits; + _cachemisses = source._cachemisses; + _keymap->copy(*source._keymap); + + ASSERT(source._localitygroup == (LocalitySet*) source._localitygroup); + _localitygroup = (LocalitySet*) this; + return *this; + } + + LocalityEntry const& getEntry(int localIndex) const { + ASSERT((localIndex >= 0) && (localIndex < _entryArray.size())); + return _entryArray[localIndex]; + } + + virtual Reference const& getRecord(int localIndex) const { + return _localitygroup->getRecord(getEntry(localIndex)._id); + } + + Reference const& getRecordViaEntry(LocalityEntry localEntry) const { + return _localitygroup->getRecord(localEntry._id); + } + + AttribKey getGroupKeyIndex(AttribKey indexKey) const + { return _localitygroup->keyIndex(keyText(indexKey)); } + + Optional const getValue(int recordIndex, AttribKey const& indexKey) const + { return getRecord(recordIndex)->getValue(getGroupKeyIndex(indexKey)); } + + Optional const getValueViaEntry(LocalityEntry recordEntry, AttribKey const& indexKey) const + { return getRecordViaEntry(recordEntry)->getValue(getGroupKeyIndex(indexKey)); } + + Optional const getValueViaGroupKey(int recordIndex, AttribKey const& indexKey) const + { return getRecord(recordIndex)->getValue(indexKey); } + + Optional const getValueViaGroupKey(LocalityEntry recordEntry, AttribKey const& indexKey) const + { return getRecordViaEntry(recordEntry)->getValue(indexKey); } + + std::vector const& getEntries() const + { return _entryArray; } + + std::vector const& getMutableEntries() const + { return _mutableEntryArray; } + + std::vector const& getGroupEntries() const + { return _localitygroup->_entryArray; } + + std::string getLocalEntryInfo(int localIndex) const { + const AttribKey machineKey = keyIndex("zoneid"); + auto& entry = getEntry(localIndex); + auto value = getValueViaEntry(entry, machineKey); + return format("(%3d) %-10s", entry._id, valueText(value.get()).c_str()); + } + + std::string getEntryInfo(LocalityEntry const& entry) const { + const AttribKey machineHash = keyIndex("zoneid"); + auto value = getValueViaEntry(entry, machineHash); + return format("(%3d) %-10s", entry._id, value.present() ? valueText(value.get()).c_str() : "(unset)"); + } + + static void staticDisplayEntries( + LocalitySet const& localitySet, + std::vector const& entryArray, + const char* name = "zone") + { + for (auto& entry : entryArray) { + printf(" %s: %s\n", name, localitySet.getEntryInfo(entry).c_str()); + } + } + + static void staticDisplayEntries( + LocalitySetRef const& fromServers, + std::vector const& entryArray, + const char* name = "zone") + { + staticDisplayEntries(*fromServers, entryArray, name); + } + + void DisplayEntries(const char* name = "zone") const { + staticDisplayEntries(*this, getEntries(), name); + } + + // This function is used to create an subset containing all of the entries within + // the specified value for the given key + LocalitySetRef restrict(AttribKey indexKey, AttribValue indexValue ) { + LocalitySetRef localitySet; + LocalityCacheRecord searchRecord(AttribRecord(indexKey, indexValue), localitySet); + auto itKeyValue = std::lower_bound(_cacheArray.begin(), _cacheArray.end(), searchRecord, LocalityCacheRecord::compareKeyValue); + + if ((itKeyValue != _cacheArray.end()) && (itKeyValue->_attribute == searchRecord._attribute)) { + if (g_replicationdebug > 2) printf("Cache Hit: (%2d) %-5s => (%4d) %-10s %3d from %3lu items\n", indexKey._id, keyText(indexKey).c_str(), indexValue._id, valueText(indexValue).c_str(), itKeyValue->_resultset->size(), _entryArray.size()); + _cachehits ++; + localitySet = itKeyValue->_resultset; + } + else { + localitySet = LocalitySetRef(new LocalitySet(*_localitygroup)); + _cachemisses ++; + // If the key is not within the current key set, skip it because no items within + // the current entry array has the key + if (indexKey._id >= _keyIndexArray.size()) { + if (g_replicationdebug > 2) { + printf("invalid key index:%3d array:%3lu value: (%3d) %-10s\n", indexKey._id, _keyIndexArray.size(), indexValue._id, valueText(indexValue).c_str()); + } + } + // Get the equivalent key index from the locality group + else { + auto& groupKeyIndex = _keyIndexArray[indexKey._id]; + localitySet->_entryArray.reserve(_entryArray.size()); + for (auto& entry : _entryArray) { + auto& record = getRecordViaEntry(entry); + if (record->isPresent(groupKeyIndex, indexValue)) { + localitySet->add(record, *this); + } + } + } + searchRecord._resultset = localitySet; + _cacheArray.insert(itKeyValue, searchRecord); + if (g_replicationdebug > 2) printf("Cache Miss: (%2d) %-5s => (%4d) %-10s %3d for %3lu items\n", indexKey._id, keyText(indexKey).c_str(), indexValue._id, valueText(indexValue).c_str(), localitySet->size(), _entryArray.size()); + } + return localitySet; + } + + // This function is used to create an subset containing the specified entries + LocalitySetRef restrict(std::vector const& entryArray) { + LocalitySetRef localitySet(new LocalitySet(*_localitygroup)); + for (auto& entry : entryArray) { + localitySet->add(getRecordViaEntry(entry), *this); + } + return localitySet; + } + + // This function will append all of the entries matching + // the specified value for the given key + std::vector & getMatches( + std::vector & entryArray, + AttribKey const& indexKey, + AttribValue const& indexValue) + { + for (auto& entry : _entryArray) { + auto& record = getRecordViaEntry(entry); + if (record->isPresent(indexKey, indexValue)) { + entryArray.push_back(entry); + } + } + return entryArray; + } + + // Return a random entry + LocalityEntry const& random() const + { return _entryArray[g_random->randomInt(0, _entryArray.size())]; } + + // Return a given number of random entries that are not within the + // specified exception array + bool random( + std::vector& randomEntries, + std::vector const& exceptionArray, + unsigned int nRandomItems) + { + bool bComplete = true; + int nItemsLeft = _mutableEntryArray.size(); + int nExceptions = exceptionArray.size(); + + while (nRandomItems > 0) + { + if (nItemsLeft <= 0) { + bComplete = false; + break; + } + while (nItemsLeft > 0) + { + auto itemIndex = g_random->randomInt(0, nItemsLeft); + auto item = _mutableEntryArray[itemIndex]; + + nItemsLeft --; + // Move the item to the end of the array, if not the last + if (itemIndex < nItemsLeft) { + _mutableEntryArray[itemIndex] = _mutableEntryArray[nItemsLeft]; + _mutableEntryArray[nItemsLeft] = item; + } + + // Break, if item is not in exception list + if (std::find(exceptionArray.begin(), exceptionArray.end(), item) != exceptionArray.end()) { + // Break, if unable to fulfil the request + if (nItemsLeft <= 0) { + bComplete = false; + break; + } + } + else { + randomEntries.push_back(item); + nRandomItems --; + break; + } + } + } + return bComplete; + } + + // Return a random value for a specific key + Optional randomValue(AttribKey const& indexKey, std::vector& usedArray) const + { + auto& valueArray = _keyValueArray[indexKey._id]; + int usedValues = usedArray.size(); + int checksLeft = valueArray.size(); + int indexValue; + bool validValue; + AttribValue valueValue; + Optional result; + + while (checksLeft > 0) { + indexValue = g_random->randomInt(0, checksLeft); + valueValue = valueArray[indexValue]; + validValue = true; + for (int usedLoop=0; usedLoop < usedValues; usedLoop ++) { + if (usedArray[usedLoop] == valueValue) { + validValue = false; + usedValues --; + if (usedLoop < usedValues-1) { + auto lastValue = usedArray[usedValues]; + usedArray[usedValues] = valueValue; + usedArray[usedLoop] = lastValue; + break; + } + } + } + if (validValue) { + result = valueValue; + break; + } + checksLeft --; + if (indexValue < checksLeft) { + auto lastValue = usedArray[checksLeft]; + usedArray[checksLeft] = valueValue; + usedArray[indexValue] = lastValue; + } + } + return result; + } + + void cacheReport() const { + printf("Cache: size:%5lu mem:%7ldKb hits:%9llu misses:%9llu records:%5lu\n", + _cacheArray.size(), getMemoryUsed()/1024L, _cachehits, _cachemisses, _entryArray.size()); + } + + void clearCache() { + _cacheArray.clear(); + } + + AttribKey keyIndex(std::string const& value) const + { return AttribKey(_keymap->convertString(value)); } + AttribKey keyIndex( char const* value) const + { return keyIndex(std::string(value)); } + AttribKey keyIndex( StringRef const& value) const + { return keyIndex(value.printable()); } + AttribKey keyIndex( Optional> const& value) const + { return keyIndex((value.present()) ? value.get().printable() : ""); } + + std::string keyText(AttribKey indexKey) const + { return _keymap->lookupString(indexKey._id); } + std::string keyText(Optional indexKey) const + { return (indexKey.present()) ? keyText(indexKey.get()._id) : ""; } + + AttribValue valueIndex(std::string const& value) const + { return AttribValue(getGroupValueMap()->convertString(value)); } + AttribValue valueIndex( char const* value) const + { return valueIndex(std::string(value)); } + AttribValue valueIndex( StringRef const& value) const + { return valueIndex(value.printable()); } + AttribValue valueIndex( Optional> const& value) const + { return valueIndex((value.present()) ? value.get().printable() : ""); } + + std::string valueText(AttribValue indexValue) const + { return getGroupValueMap()->lookupString(indexValue._id); } + std::string valueText(Optional indexValue) const + { return (indexValue.present()) ? valueText(indexValue.get()._id) : ""; } + + int size() const { return _entryArray.size(); } + int empty() const { return _entryArray.empty(); } + + virtual void swapMutableRecords(int recordIndex1, int recordIndex2) { + ASSERT((recordIndex1 >=0) && (recordIndex1 < _mutableEntryArray.size())); + ASSERT((recordIndex2 >=0) && (recordIndex2 < _mutableEntryArray.size())); + auto entry = _mutableEntryArray[recordIndex1]; + _mutableEntryArray[recordIndex1] = _mutableEntryArray[recordIndex2]; + _mutableEntryArray[recordIndex2] = entry; + } + + virtual int getMemoryUsed() const { + int memorySize = sizeof(_entryArray) + sizeof(LocalityEntry) * _entryArray.capacity() + sizeof(_cacheArray) + sizeof(LocalityCacheRecord) * _cacheArray.capacity() + sizeof(_keyIndexArray) + sizeof(AttribKey) * _keyIndexArray.capacity() + sizeof(_cachehits) + sizeof(_cachemisses) + _keymap->getMemoryUsed(); + for (auto& cacheRecord : _cacheArray) { + memorySize += cacheRecord.getMemoryUsed(); + } + for (auto& valueArray : _keyValueArray) { + memorySize += sizeof(AttribValue) * valueArray.capacity(); + } + return memorySize; + } + +protected: + LocalityEntry const& add(LocalityEntry const& entry, LocalityData const& data) { + _entryArray.push_back(entry); + _mutableEntryArray.push_back(entry); + ASSERT(data._data.size() > 0); + + // Ensure that the key value array is large enough to hold the values + if (_keyValueArray.capacity() < _keyValueArray.size() + data._data.size()) { + _keyValueArray.reserve(_keyValueArray.size() + data._data.size()); + } + for (auto& dataPair : data._data) { + auto indexKey = keyIndex(dataPair.first); + auto indexValue = valueIndex(dataPair.second); + if (indexKey._id >= _keyIndexArray.size()) { + _keyIndexArray.resize(indexKey._id+1); + _keyIndexArray[indexKey._id] = AttribKey(getGroupKeyMap()->convertString(dataPair.first)); + } + if (indexKey._id >= _keyValueArray.size()) { + _keyValueArray.resize(indexKey._id+1); + } + auto& valueArray = _keyValueArray[indexKey._id]; + auto lowerBound = std::lower_bound(valueArray.begin(), valueArray.end(), indexValue); + if ((lowerBound == valueArray.end()) || (*lowerBound != indexValue)) { + valueArray.insert(lowerBound, indexValue); + } + } + clearCache(); + return _entryArray.back(); + } + + LocalityEntry const& add(Reference const& record, LocalitySet const& localitySet) { + _entryArray.push_back(record->_entryIndex); + _mutableEntryArray.push_back(record->_entryIndex); + + // Ensure that the key value array is large enough to hold the values + if (_keyValueArray.capacity() < _keyValueArray.size() + record->_dataMap->size()) { + _keyValueArray.reserve(_keyValueArray.size() + record->_dataMap->size()); + } + ASSERT(record->_dataMap->_keyvaluearray.size() > 0); + + for (auto& keyValuePair : record->_dataMap->_keyvaluearray) { + auto keyString = _localitygroup->keyText(keyValuePair.first); + auto indexKey = keyIndex(keyString); + auto& indexValue = keyValuePair.second; + + if (indexKey._id >= _keyIndexArray.size()) { + _keyIndexArray.resize(indexKey._id+1); + _keyIndexArray[indexKey._id] = AttribKey(getGroupKeyMap()->convertString(keyString)); + } + + if (indexKey._id >= _keyValueArray.size()) { + _keyValueArray.resize(indexKey._id+1); + } + + auto& valueArray = _keyValueArray[indexKey._id]; + auto lowerBound = std::lower_bound(valueArray.begin(), valueArray.end(), indexValue); + if ((lowerBound == valueArray.end()) || (*lowerBound != indexValue)) { + valueArray.insert(lowerBound, indexValue); + } + } + clearCache(); + return _entryArray.back(); + } + + // This class stores the cache record for each entry within the locality set + struct LocalityCacheRecord { + AttribRecord _attribute; + LocalitySetRef _resultset; + LocalityCacheRecord(AttribRecord const& attribute, LocalitySetRef resultset):_attribute(attribute),_resultset(resultset){} + LocalityCacheRecord(LocalityCacheRecord const& source):_attribute(source._attribute),_resultset(source._resultset){} + virtual ~LocalityCacheRecord(){} + LocalityCacheRecord& operator=(LocalityCacheRecord const& source) { + _attribute = source._attribute; + _resultset = source._resultset; + return *this; + } + int getMemoryUsed() const { + return sizeof(_attribute) + sizeof(_resultset) + _resultset->getMemoryUsed(); + } + static bool compareKeyValue(const LocalityCacheRecord& lhs, const LocalityCacheRecord& rhs) + { return (lhs._attribute.first < rhs._attribute.first) || (!(rhs._attribute.first < lhs._attribute.first) && (lhs._attribute.second < rhs._attribute.second)); } + static bool compareKey(const LocalityCacheRecord& lhs, const LocalityCacheRecord& rhs) + { return (lhs._attribute.first < rhs._attribute.first); } + }; + +public: + virtual Reference const& getGroupValueMap() const + { return _localitygroup->getGroupValueMap(); } + + virtual Reference const& getGroupKeyMap() const + { return _localitygroup->getGroupKeyMap(); } + +protected: + virtual Reference& getGroupValueMap() + { return _localitygroup->getGroupValueMap(); } + + virtual Reference& getGroupKeyMap() + { return _localitygroup->getGroupKeyMap(); } + +protected: + std::vector _entryArray; + std::vector _mutableEntryArray; // Use to rearrange entries for fun + std::vector> _keyValueArray; + + std::vector _keyIndexArray; + std::vector _cacheArray; + Reference _keymap; + LocalitySet* _localitygroup; + long long unsigned int _cachehits; + long long unsigned int _cachemisses; +}; + +struct LocalityGroup : public LocalitySet { + LocalityGroup():LocalitySet(*this), _valuemap(new StringToIntMap()) {} + LocalityGroup(LocalityGroup const& source):LocalitySet(source), _recordArray(source._recordArray), _valuemap(source._valuemap) {} + virtual ~LocalityGroup() { } + + LocalityEntry const& add(LocalityData const& data) { + Reference record(new LocalityRecord(convertToAttribMap(data), _recordArray.size())); + _recordArray.push_back(record); + return LocalitySet::add(record, *this); + } + + virtual void clear() { + LocalitySet::clear(); + _valuemap->clear(); + _recordArray.clear(); + } + + LocalityGroup & copy(LocalityGroup const& source) { + LocalitySet::copy(source); + _valuemap = source._valuemap; + _recordArray = source._recordArray; + return *this; + } + + LocalityGroup & deep_copy(LocalityGroup const& source) { + LocalitySet::deep_copy(source); + _valuemap->copy(*source._valuemap); + _recordArray = source._recordArray; + return *this; + } + + virtual Reference const& getRecord(int recordIndex) const { + ASSERT((recordIndex >= 0) && (recordIndex < _recordArray.size())); + return _recordArray[recordIndex]; + } + + virtual int getMemoryUsed() const { + int memorySize = sizeof(_recordArray) + _keymap->getMemoryUsed(); + for (auto& record : _recordArray) { + memorySize += record->getMemoryUsed(); + } + return LocalitySet::getMemoryUsed() + memorySize; + } + + // Convert locality data to sorted vector of int pairs + Reference convertToAttribMap(LocalityData const& data) { + Reference attribHashMap(new KeyValueMap); + for (auto& dataPair : data._data) { + auto indexKey = keyIndex(dataPair.first); + auto indexValue = valueIndex(dataPair.second); + attribHashMap->_keyvaluearray.push_back(AttribRecord(indexKey,indexValue)); + } + // Sort the attribute array + std::sort(attribHashMap->_keyvaluearray.begin(), attribHashMap->_keyvaluearray.end(), KeyValueMap::compareKeyValue); + return attribHashMap; + } + + virtual Reference const& getGroupValueMap() const + { return _valuemap; } + + virtual Reference const& getGroupKeyMap() const + { return _keymap; } + +protected: + virtual Reference & getGroupValueMap() + { return _valuemap; } + + virtual Reference & getGroupKeyMap() + { return _keymap; } + +protected: + std::vector> _recordArray; + Reference _valuemap; +}; + +template +struct LocalityMap : public LocalityGroup { + LocalityMap():LocalityGroup() {} + LocalityMap(LocalityMap const& source):LocalityGroup(source), _objectArray(source._objectArray) {} + virtual ~LocalityMap() {} + + bool selectReplicas( + IRepPolicyRef const& policy, + std::vector const& alsoServers, + std::vector& entryResults, + std::vector & results) + { + bool result; + int entrySize = entryResults.size(); + int extraSpace = results.capacity() - results.size(); + if (extraSpace > 0) { + entryResults.reserve(entryResults.size() + extraSpace); + } + result = LocalityGroup::selectReplicas(policy, alsoServers, entryResults); + append(results, entryResults, entrySize); + return result; + } + + bool selectReplicas( + IRepPolicyRef const& policy, + std::vector const& alsoServers, + std::vector & results) + { + std::vector entryResults; + return selectReplicas(policy, alsoServers, entryResults, results); + } + + bool selectReplicas( + IRepPolicyRef const& policy, + std::vector & results) + { return selectReplicas(policy, std::vector(), results); } + + void append( + std::vector & objects, + std::vector const& entries, + int firstItem = 0) const + { + int newItems = entries.size() - firstItem; + if (newItems > 0) + objects.reserve(objects.size() + newItems + objects.size()); + for (auto i=firstItem; i= 0) && (recordEntry._id < _objectArray.size())); + return _objectArray[recordEntry._id]; + } + + V const* getObject(LocalityRecord const& record) const { + return getObject(record._entryIndex); + } + + V const* getObject(Reference const& record) const { + return getObject(record->_entryIndex); + } + + virtual void clear() { + LocalityGroup::clear(); + _objectArray.clear(); + } + + virtual int getMemoryUsed() const { + return LocalitySet::getMemoryUsed() + sizeof(_objectArray) + (sizeof(V*) * _objectArray.size()); + } + + std::vector const& getObjects() const + { return _objectArray; } + +protected: + std::vector _objectArray; +}; + +#endif diff --git a/fdbrpc/ReplicationPolicy.cpp b/fdbrpc/ReplicationPolicy.cpp new file mode 100644 index 0000000000..47ef1b9d80 --- /dev/null +++ b/fdbrpc/ReplicationPolicy.cpp @@ -0,0 +1,457 @@ +/* + * ReplicationPolicy.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ReplicationPolicy.h" +#include "Replication.h" +#include "flow/UnitTest.h" + + +bool IReplicationPolicy::selectReplicas( + LocalitySetRef & fromServers, + std::vector & results ) +{ + return selectReplicas(fromServers, std::vector(), results); +} + +bool IReplicationPolicy::validate( + LocalitySetRef const& solutionSet ) const +{ + return validate(solutionSet->getEntries(), solutionSet); +} + +bool IReplicationPolicy::validateFull( + bool solved, + std::vector const& solutionSet, + std::vector const& alsoServers, + LocalitySetRef const& fromServers ) +{ + bool valid = true; + std::vector totalSolution(solutionSet); + + // Append the also servers, if any + if (alsoServers.size()) { + totalSolution.reserve(totalSolution.size() + alsoServers.size()); + totalSolution.insert(totalSolution.end(), alsoServers.begin(), alsoServers.end()); + } + + if (!solved) { + if (validate(totalSolution, fromServers)) { + if (g_replicationdebug > 2) { + printf("Error: Validate unsolved policy with%3lu also servers and%3lu solution servers\n", alsoServers.size(), solutionSet.size()); + } + valid = false; + } + else if (validate(fromServers->getGroupEntries(), fromServers)) { + if (g_replicationdebug > 2) { + printf("Error: Validated unsolved policy with all%5lu servers\n", fromServers->size()); + } + valid = false; + } + } + else if (!validate(totalSolution, fromServers)) { + if (g_replicationdebug > 2) { + printf("Error: Failed to validate solved policy with%3lu also servers and%3lu solution servers\n", alsoServers.size(), solutionSet.size()); + } + valid = false; + } + else if (solutionSet.empty()) { + if (!validate(alsoServers, fromServers)) { + if (g_replicationdebug > 2) { + printf("Error: Failed to validate policy with only%3lu also servers\n", alsoServers.size()); + } + valid = false; + } + } + else { + auto lastSolutionIndex = solutionSet.size()-1; + auto missingEntry = totalSolution[lastSolutionIndex]; + totalSolution[lastSolutionIndex] = totalSolution.back(); + totalSolution.pop_back(); + for (int index = 0; index < solutionSet.size(); index ++) { + if (g_replicationdebug > 3) { + auto fromServer = fromServers->getRecordViaEntry(missingEntry); + printf("Test remove entry: %s test:%3d of%3lu\n", fromServers->getEntryInfo(missingEntry).c_str(), index+1, solutionSet.size()); + } + if (validate(totalSolution, fromServers)) { + if (g_replicationdebug > 2) { + printf("Invalid extra entry: %s\n", fromServers->getEntryInfo(missingEntry).c_str()); + } + valid = false; + break; + } + auto tempMissing = totalSolution[index]; + totalSolution[index] = missingEntry; + missingEntry = tempMissing; + } + } + return valid; +} + +bool PolicyOne::selectReplicas( + LocalitySetRef & fromServers, + std::vector const& alsoServers, + std::vector & results ) +{ + int totalUsed = 0; + int itemsUsed = 0; + if (alsoServers.size()) { + totalUsed ++; + } + else if (fromServers->size()) { + auto randomEntry = fromServers->random(); + results.push_back(randomEntry); + itemsUsed ++; + totalUsed ++; + if (g_replicationdebug > 5) { + printf("One added:%4d %33s entry: %s\n", itemsUsed, "", fromServers->getEntryInfo(randomEntry).c_str()); + } + } + if (g_replicationdebug > 2) { + printf("One used:%5d results:%3d from %3d servers\n", totalUsed, itemsUsed, fromServers->size()); + } + return (totalUsed > 0); +} + +bool PolicyOne::validate( + std::vector const& solutionSet, + LocalitySetRef const& fromServers ) const +{ + return ((solutionSet.size() > 0) && (fromServers->size() > 0)); +} + +PolicyAcross::PolicyAcross(int count, std::string const& attribKey, IRepPolicyRef const policy): + _count(count),_attribKey(attribKey),_policy(policy) +{ + return; +} + +PolicyAcross::~PolicyAcross() +{ + return; +} + +bool PolicyAcross::validate( + std::vector const& solutionSet, + LocalitySetRef const& fromServers ) const +{ + bool valid = true; + int count = 0; + AttribKey indexKey = fromServers->keyIndex(_attribKey); + auto groupIndexKey = fromServers->getGroupKeyIndex(indexKey); + std::map> validMap; + for (auto& item : solutionSet) { + auto value = fromServers->getValueViaGroupKey(item, groupIndexKey); + if (value.present()) { + auto itValue = validMap.find(value.get()); + if (itValue != validMap.end()) { + validMap[value.get()].push_back(item); + } + else { + validMap[value.get()] = {item}; + } + } + } + if (validMap.size() < _count) { + if (g_replicationdebug > 3) { + printf("Across too few values:%3lu <%2d key: %-7s policy: %-10s => %s\n", validMap.size(), _count, _attribKey.c_str(), _policy->name().c_str(), _policy->info().c_str()); + } + valid = false; + } + else { + if (g_replicationdebug > 3) { + printf("Across check values:%9lu key: %-7s solutions:%2lu count:%2d policy: %-10s => %s\n", validMap.size(), _attribKey.c_str(), solutionSet.size(), _count, _policy->name().c_str(), _policy->info().c_str()); + for (auto& itValue : validMap) { + printf(" value: (%3lu) %-10s\n", itValue.first._id, fromServers->valueText(itValue.first).c_str()); + } + } + for (auto& itValid : validMap) { + if (_policy->validate(itValid.second, fromServers)) { + if (g_replicationdebug > 4) { + printf("Across valid solution: %6lu key: %-7s count:%3d of%3d value: (%3lu) %-10s policy: %-10s => %s\n", itValid.second.size(), _attribKey.c_str(), count+1, _count, itValid.first._id, fromServers->valueText(itValid.first).c_str(), _policy->name().c_str(), _policy->info().c_str()); + if (g_replicationdebug > 5) { + for (auto& entry : itValid.second) { + printf(" entry: %s\n", fromServers->getEntryInfo(entry).c_str()); + } + } + } + count ++; + } + else if (g_replicationdebug > 4) { + printf("Across invalid solution:%5lu key: %-7s value: (%3lu) %-10s policy: %-10s => %s\n", itValid.second.size(), _attribKey.c_str(), itValid.first._id, fromServers->valueText(itValid.first).c_str(), _policy->name().c_str(), _policy->info().c_str()); + if (g_replicationdebug > 5) { + for (auto& entry : itValid.second) { + printf(" entry: %s\n", fromServers->getEntryInfo(entry).c_str()); + } + } + } + } + if (count < _count) { + if (g_replicationdebug > 3) { + printf("Across failed solution: %3lu key: %-7s values:%3lu count: %d=%d policy: %-10s => %s\n", solutionSet.size(), _attribKey.c_str(), validMap.size(), count, _count, _policy->name().c_str(), _policy->info().c_str()); + for (auto& entry : solutionSet) { + printf(" entry: %s\n", fromServers->getEntryInfo(entry).c_str()); + } + } + valid = false; + } + } + return valid; +} + +bool PolicyAcross::selectReplicas( + LocalitySetRef & fromServers, + std::vector const& alsoServers, + std::vector & results ) +{ + int count = 0; + AttribKey indexKey = fromServers->keyIndex(_attribKey); + auto groupIndexKey = fromServers->getGroupKeyIndex(indexKey); + int resultsSize, resultsAdded; + int resultsInit = results.size(); + + // Clear the member variables + _usedValues.clear(); + _newResults.clear(); + _addedResults.resize(_arena, 0); + + if ((g_replicationdebug > 3) && (alsoServers.size())) { + printf("Across !also:%4lu key: %-7s policy: %-10s => %s\n", alsoServers.size(), _attribKey.c_str(), _policy->name().c_str(), _policy->info().c_str()); + } + for (auto& alsoServer : alsoServers) { + auto value = fromServers->getValueViaGroupKey(alsoServer, groupIndexKey); + if (value.present()) { + auto lowerBound = std::lower_bound(_usedValues.begin(), _usedValues.end(), value.get()); + if ((lowerBound == _usedValues.end()) || (*lowerBound != value.get())) { + _selected = fromServers->restrict(indexKey, value.get()); + if (_selected->size()) { + // Pass only the also array item which are valid for the value + if (g_replicationdebug > 5) { + printf("Across !select key: %-7s value: (%3d) %-10s entry: %s\n", _attribKey.c_str(), value.get()._id, fromServers->valueText(value.get()).c_str(), fromServers->getEntryInfo(alsoServer).c_str()); + } + resultsSize = _newResults.size(); + if (_policy->selectReplicas(_selected, alsoServers, _newResults)) + { + resultsAdded = _newResults.size() - resultsSize; + if (!resultsAdded) { + count ++; + } + else { + _addedResults.push_back(_arena, std::pair(resultsAdded, resultsSize)); + } + if (g_replicationdebug > 5) { + printf("Across !added:%3d key: %-7s count:%3d of%3d value: (%3lu) %-10s entry: %s\n", resultsAdded, _attribKey.c_str(), count, _count, value.get()._id, fromServers->valueText(value.get()).c_str(), fromServers->getEntryInfo(alsoServer).c_str()); + } + if (count >= _count) break; + _usedValues.insert(lowerBound, value.get()); + } + else if (g_replicationdebug > 5) { + printf("Across !no answer key: %-7s value: (%3d) %-10s entry: %s\n", _attribKey.c_str(), value.get()._id, fromServers->valueText(value.get()).c_str(), fromServers->getEntryInfo(alsoServer).c_str()); + } + } + else if (g_replicationdebug > 5) { + printf("Across !empty set key: %-7s value: (%3d) %-10s entry: %s\n", _attribKey.c_str(), value.get()._id, fromServers->valueText(value.get()).c_str(), fromServers->getEntryInfo(alsoServer).c_str()); + } + + } + else if (g_replicationdebug > 5) { + printf("Across !duplicate key: %-7s value: (%3d) %-10s entry: %s\n", _attribKey.c_str(), value.get()._id, fromServers->valueText(value.get()).c_str(), fromServers->getEntryInfo(alsoServer).c_str()); + } + } + else if (g_replicationdebug > 5) { + printf("Across !no value key: %-7s %21s entry: %s\n", _attribKey.c_str(), "", fromServers->getEntryInfo(alsoServer).c_str()); + } + } + + // Process the remaining results, if present + if ((count < _count) && (_addedResults.size())) { + // Sort the added results array + std::sort(_addedResults.begin(), _addedResults.end(), PolicyAcross::compareAddedResults); + + if (g_replicationdebug > 2) { + printf("Across !add sets key: %-7s sets:%3d results:%3lu count:%3d of%3d\n", _attribKey.c_str(), _addedResults.size(), _newResults.size(), count, _count); + } + + if (g_replicationdebug > 6) { + LocalitySet::staticDisplayEntries(fromServers, alsoServers, "also"); + LocalitySet::staticDisplayEntries(fromServers, results, "results"); + LocalitySet::staticDisplayEntries(fromServers, _newResults, "add items"); + } + + for (auto& addedResult : _addedResults) { + count ++; + if (g_replicationdebug > 2) { + printf("Across !add set key: %-7s count:%3d of%3d results:%3d index:%3d\n", _attribKey.c_str(), count, _count, addedResult.first, addedResult.second); + } + results.reserve(results.size() + addedResult.first); + results.insert(results.end(), _newResults.begin()+addedResult.second, _newResults.begin()+addedResult.second+addedResult.first); + if (count >= _count) break; + } + if (g_replicationdebug > 7) { + LocalitySet::staticDisplayEntries(fromServers, results, "results"); + } + } + + // Process the remaining values + if (count < _count) { + if (g_replicationdebug > 3) { + printf("Across items:%4d key: %-7s policy: %-10s => %s count:%3d of%3d\n", fromServers->size(), _attribKey.c_str(), _policy->name().c_str(), _policy->info().c_str(), count, _count); + } + int recordIndex; + // Use mutable array so that swaps does not affect actual element array + auto& mutableArray = fromServers->getMutableEntries(); + for (int checksLeft = fromServers->size(); checksLeft > 0; checksLeft --) { + if (g_replicationdebug > 6) { + LocalitySet::staticDisplayEntries(fromServers, mutableArray, "mutable"); + } + recordIndex = g_random->randomInt(0, checksLeft); + auto& entry = mutableArray[recordIndex]; + auto value = fromServers->getValueViaGroupKey(entry, groupIndexKey); + if (value.present()) { + auto lowerBound = std::lower_bound(_usedValues.begin(), _usedValues.end(), value.get()); + if ((lowerBound == _usedValues.end()) || (*lowerBound != value.get())) { + _selected = fromServers->restrict(indexKey, value.get()); + if (_selected->size()) { + if (g_replicationdebug > 5) { + printf("Across select:%3d key: %-7s value: (%3d) %-10s entry: %s index:%4d\n", fromServers->size()-checksLeft+1, _attribKey.c_str(), value.get()._id, fromServers->valueText(value.get()).c_str(), fromServers->getEntryInfo(entry).c_str(), recordIndex); + } + if (_policy->selectReplicas(_selected, emptyEntryArray, results)) + { + if (g_replicationdebug > 5) { + printf("Across added:%4d key: %-7s value: (%3d) %-10s policy: %-10s => %s needed:%3d\n", count+1, _attribKey.c_str(), value.get()._id, fromServers->valueText(value.get()).c_str(), _policy->name().c_str(), _policy->info().c_str(), _count); + } + count ++; + if (count >= _count) break; + _usedValues.insert(lowerBound, value.get()); + } + else if (g_replicationdebug > 5) { + printf("Across no answer key: %-7s value: (%3d) %-10s entry: %s\n", _attribKey.c_str(), value.get()._id, fromServers->valueText(value.get()).c_str(), fromServers->getEntryInfo(entry).c_str()); + } + } + else if (g_replicationdebug > 5) { + printf("Across empty set:%3d key: %-7s value: (%3d) %-10s entry: %s index:%4d\n", fromServers->size()-checksLeft+1, _attribKey.c_str(), value.get()._id, fromServers->valueText(value.get()).c_str(), fromServers->getEntryInfo(entry).c_str(), recordIndex); + } + } + else if (g_replicationdebug > 5) { + printf("Across duplicate key: %-7s value: (%3d) %-10s entry: %s attempt:%3d index:%4d\n", _attribKey.c_str(), value.get()._id, fromServers->valueText(value.get()).c_str(), fromServers->getEntryInfo(entry).c_str(), fromServers->size()-checksLeft+1, recordIndex); + } + } + else if (g_replicationdebug > 5) { + printf("Across no value key: %-7s %21s entry: %s attempt:%3d index:%4d\n", _attribKey.c_str(), "", fromServers->getEntryInfo(entry).c_str(), fromServers->size()-checksLeft+1, recordIndex); + } + if (recordIndex != checksLeft-1) { + if (g_replicationdebug > 5) { + printf("Across swap key: %-7s index:%4d last:%4d entry: %s\n", _attribKey.c_str(), recordIndex, checksLeft-1, fromServers->getEntryInfo(entry).c_str()); + } + fromServers->swapMutableRecords(recordIndex, checksLeft-1); + } + } + } + // Clear the return array, if not satified + if (count < _count) { + if (g_replicationdebug > 4) printf("Across result count: %d < %d requested\n", count, _count); + results.resize(resultsInit); + count = 0; + } + if (g_replicationdebug > 2) { + printf("Across used:%5lu results:%3d from %3d items key: %-7s policy: %-10s => %s\n", results.size()-resultsInit, count, fromServers->size(), _attribKey.c_str(), _policy->name().c_str(), _policy->info().c_str()); + } + return (count >= _count); +} + +bool PolicyAnd::validate( + std::vector const& solutionSet, + LocalitySetRef const& fromServers ) const +{ + bool valid = true; + for (auto& policy : _policies) { + if (!policy->validate(solutionSet, fromServers)) { + valid = false; + break; + } + } + return valid; +} + +bool PolicyAnd::selectReplicas( + LocalitySetRef & fromServers, + std::vector const& alsoServers, + std::vector & results ) +{ + bool passed = true; + std::vector newResults(alsoServers); + + // Ensure that the results array is large enough + if (newResults.capacity() < fromServers->size()) { + newResults.reserve(fromServers->size()); + } + + for (auto& policy : _sortedPolicies) { + if (g_replicationdebug > 3) { + printf("And also:%5d used: %4lu from %3d items policy: %-10s => %s\n", newResults.size(), newResults.size()-alsoServers.size(), fromServers->size(), policy->name().c_str(), policy->info().c_str()); + } + if (!policy->selectReplicas(fromServers, newResults, newResults)) + { + if (g_replicationdebug > 3) { + printf("And failed set:%4d policy: %-10s => %s\n", fromServers->size(), policy->name().c_str(), policy->info().c_str()); + } + passed = false; + break; + } + } + if ((passed) && (newResults.size() > alsoServers.size())) { + results.reserve(results.size() + newResults.size() - alsoServers.size()); + results.insert(results.end(), newResults.begin()+alsoServers.size(), newResults.end()); + } + + if (g_replicationdebug > 2) { + printf("And used:%5d results:%3lu from %3d items\n", newResults.size()-alsoServers.size(), results.size(), fromServers->size()); + } + return passed; +} + +void testPolicySerialization(IRepPolicyRef& policy) { + std::string policyInfo = policy->info(); + + BinaryWriter writer(IncludeVersion()); + serializeReplicationPolicy(writer, policy); + + BinaryReader reader(writer.getData(), writer.getLength(), IncludeVersion()); + IRepPolicyRef copy; + serializeReplicationPolicy(reader, copy); + + ASSERT(policy->info() == copy->info()); +} + +void testReplicationPolicy(int nTests) { + IRepPolicyRef policy = IRepPolicyRef(new PolicyAcross(1, "data_hall", IRepPolicyRef(new PolicyOne()))); + testPolicySerialization(policy); + + policy = IRepPolicyRef(new PolicyAnd({ + IRepPolicyRef(new PolicyAcross(2, "data_center", IRepPolicyRef(new PolicyAcross(3, "rack", IRepPolicyRef(new PolicyOne()))))), + IRepPolicyRef(new PolicyAcross(2, "data_center", IRepPolicyRef(new PolicyAcross(2, "data_hall", IRepPolicyRef(new PolicyOne()))))) + })); + + testPolicySerialization(policy); +} + +TEST_CASE("ReplicationPolicy/Serialization") { + testReplicationPolicy(1); + return Void(); +} diff --git a/fdbrpc/ReplicationPolicy.h b/fdbrpc/ReplicationPolicy.h new file mode 100644 index 0000000000..43f8a34831 --- /dev/null +++ b/fdbrpc/ReplicationPolicy.h @@ -0,0 +1,265 @@ +/* + * ReplicationPolicy.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_REPLICATION_POLICY_H +#define FLOW_REPLICATION_POLICY_H +#pragma once + +#include "flow/flow.h" +#include "ReplicationTypes.h" + +template +void serializeReplicationPolicy(Ar& ar, IRepPolicyRef& policy); +extern void testReplicationPolicy(int nTests); + + +struct IReplicationPolicy : public ReferenceCounted { + IReplicationPolicy() {} + virtual ~IReplicationPolicy() {} + virtual std::string name() const = 0; + virtual std::string info() const = 0; + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + virtual int maxResults() const = 0; + virtual int depth() const = 0; + virtual bool selectReplicas( + LocalitySetRef & fromServers, + std::vector const& alsoServers, + std::vector & results ) = 0; + virtual bool validate( + std::vector const& solutionSet, + LocalitySetRef const& fromServers ) const = 0; + + bool operator == ( const IReplicationPolicy& r ) const { return info() == r.info(); } + bool operator != ( const IReplicationPolicy& r ) const { return info() != r.info(); } + + template + void serialize(Ar& ar) { + IRepPolicyRef refThis(this); + serializeReplicationPolicy(ar, refThis); + refThis->delref_no_destroy(); + } + + // Utility functions + bool selectReplicas( + LocalitySetRef & fromServers, + std::vector & results ); + bool validate( + LocalitySetRef const& solutionSet ) const; + bool validateFull( + bool solved, + std::vector const& solutionSet, + std::vector const& alsoServers, + LocalitySetRef const& fromServers ); +}; + +template +inline void load( Archive& ar, IRepPolicyRef& value ) { + bool present = (value.getPtr()); + ar >> present; + if (present) { + serializeReplicationPolicy(ar, value); + } + else { + value.clear(); + } +} + +template +inline void save( Archive& ar, const IRepPolicyRef& value ) { + bool present = (value.getPtr()); + ar << present; + if (present) { + serializeReplicationPolicy(ar, (IRepPolicyRef&) value); + } +} + +struct PolicyOne : IReplicationPolicy, public ReferenceCounted { + PolicyOne() {}; + virtual ~PolicyOne() {}; + virtual std::string name() const { return "One"; } + virtual std::string info() const { return "1"; } + virtual int maxResults() const { return 1; } + virtual int depth() const { return 1; } + virtual bool validate( + std::vector const& solutionSet, + LocalitySetRef const& fromServers ) const; + virtual bool selectReplicas( + LocalitySetRef & fromServers, + std::vector const& alsoServers, + std::vector & results ); + template + void serialize(Ar& ar) { + } +}; + +struct PolicyAcross : IReplicationPolicy, public ReferenceCounted { + PolicyAcross(int count, std::string const& attribKey, IRepPolicyRef const policy); + virtual ~PolicyAcross(); + virtual std::string name() const { return "Across"; } + virtual std::string info() const + { return format("%s^%d x ", _attribKey.c_str(), _count) + _policy->info(); } + virtual int maxResults() const { return _count * _policy->maxResults(); } + virtual int depth() const { return 1 + _policy->depth(); } + virtual bool validate( + std::vector const& solutionSet, + LocalitySetRef const& fromServers ) const; + virtual bool selectReplicas( + LocalitySetRef & fromServers, + std::vector const& alsoServers, + std::vector & results ); + + template + void serialize(Ar& ar) { + ar & _attribKey & _count; + serializeReplicationPolicy(ar, _policy); + } + + static bool compareAddedResults(const std::pair& rhs, const std::pair& lhs) + { return (rhs.first < lhs.first) || (!(lhs.first < rhs.first) && (rhs.second < lhs.second)); } + +protected: + int _count; + std::string _attribKey; + IRepPolicyRef _policy; + + // Cache temporary members + std::vector _usedValues; + std::vector _newResults; + LocalitySetRef _selected; + VectorRef> _addedResults; + Arena _arena; +}; + +struct PolicyAnd : IReplicationPolicy, public ReferenceCounted { + PolicyAnd(std::vector policies): _policies(policies), _sortedPolicies(policies) + { + // Sort the policy array + std::sort(_sortedPolicies.begin(), _sortedPolicies.end(), PolicyAnd::comparePolicy); + } + virtual ~PolicyAnd() {} + virtual std::string name() const { return "And"; } + virtual std::string info() const { + std::string infoText; + for (auto& policy : _policies) { + infoText += ((infoText.length()) ? " & (" : "(") + policy->info() + ")"; + } + if (_policies.size()) infoText = "(" + infoText + ")"; + return infoText; + } + virtual int maxResults() const { + int resultsMax = 0; + for (auto& policy : _policies) { + resultsMax += policy->maxResults(); + } + return resultsMax; + } + virtual int depth() const { + int policyDepth, depthMax = 0; + for (auto& policy : _policies) { + policyDepth = policy->depth(); + if (policyDepth > depthMax) { + depthMax = policyDepth; + } + } + return depthMax; + } + virtual bool validate( + std::vector const& solutionSet, + LocalitySetRef const& fromServers ) const; + + virtual bool selectReplicas( + LocalitySetRef & fromServers, + std::vector const& alsoServers, + std::vector & results ); + + static bool comparePolicy(const IRepPolicyRef& rhs, const IRepPolicyRef& lhs) + { return (lhs->maxResults() < rhs->maxResults()) || (!(rhs->maxResults() < lhs->maxResults()) && (lhs->depth() < rhs->depth())); } + + template + void serialize(Ar& ar) { + int count = _policies.size(); + ar & count; + _policies.resize(count); + for(int i = 0; i < count; i++) { + serializeReplicationPolicy(ar, _policies[i]); + } + if(Ar::isDeserializing) { + _sortedPolicies = _policies; + std::sort(_sortedPolicies.begin(), _sortedPolicies.end(), PolicyAnd::comparePolicy); + } + } + +protected: + std::vector _policies; + std::vector _sortedPolicies; +}; + +extern int testReplication(); + + +template +void serializeReplicationPolicy(Ar& ar, IRepPolicyRef& policy) { + if(Ar::isDeserializing) { + StringRef name; + ar & name; + + if(name == LiteralStringRef("One")) { + PolicyOne* pointer = new PolicyOne(); + pointer->serialize(ar); + policy = IRepPolicyRef(pointer); + } + else if(name == LiteralStringRef("Across")) { + PolicyAcross* pointer = new PolicyAcross(0, "", IRepPolicyRef()); + pointer->serialize(ar); + policy = IRepPolicyRef(pointer); + } + else if(name == LiteralStringRef("And")) { + PolicyAnd* pointer = new PolicyAnd({}); + pointer->serialize(ar); + policy = IRepPolicyRef(pointer); + } + else { + TraceEvent(SevError, "SerializingInvalidPolicyType") + .detailext("PolicyName", name); + } + } + else { + ASSERT(policy); + std::string name = policy->name(); + Standalone nameRef = StringRef(name); + ar & nameRef; + if(name == "One") { + ((PolicyOne*)policy.getPtr())->serialize(ar); + } + else if(name == "Across") { + ((PolicyAcross*)policy.getPtr())->serialize(ar); + } + else if(name == "And") { + ((PolicyAnd*)policy.getPtr())->serialize(ar); + } + else { + TraceEvent(SevError, "SerializingInvalidPolicyType") + .detail("PolicyName", name); + } + } +} + +#endif diff --git a/fdbrpc/ReplicationTypes.cpp b/fdbrpc/ReplicationTypes.cpp new file mode 100644 index 0000000000..ede5688245 --- /dev/null +++ b/fdbrpc/ReplicationTypes.cpp @@ -0,0 +1,25 @@ +/* + * ReplicationTypes.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ReplicationTypes.h" + +// Map of attribute hash to name +const std::vector emptyEntryArray; +int g_replicationdebug = 0; diff --git a/fdbrpc/ReplicationTypes.h b/fdbrpc/ReplicationTypes.h new file mode 100644 index 0000000000..69f0219539 --- /dev/null +++ b/fdbrpc/ReplicationTypes.h @@ -0,0 +1,203 @@ +/* + * ReplicationTypes.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_REPLICATION_TYPES_H +#define FLOW_REPLICATION_TYPES_H +#pragma once + +#include "flow/flow.h" +#include "Locality.h" + +class LocalityData; +struct LocalitySet; +struct LocalityGroup; +struct KeyValueMap; +struct LocalityRecord; +struct StringToIntMap; +struct IReplicationPolicy; + +typedef Reference LocalitySetRef; +typedef Reference IRepPolicyRef; + +extern int g_replicationdebug; + +struct AttribKey { + int _id; + explicit AttribKey():_id(-1) {} + explicit AttribKey(int id):_id(id) {} + bool operator==(AttribKey const& source) const + { return _id == source._id; } + bool operator<(AttribKey const& source) const + { return _id < source._id; } +}; +struct AttribValue { + int _id; + explicit AttribValue():_id(-1) {} + explicit AttribValue(int id):_id(id) {} + bool operator==(AttribValue const& source) const + { return _id == source._id; } + bool operator<(AttribValue const& source) const + { return _id < source._id; } +}; +struct LocalityEntry { + int _id; + explicit LocalityEntry():_id(-1) {} + explicit LocalityEntry(int id):_id(id) {} + bool operator==(LocalityEntry const& source) const + { return _id == source._id; } + bool operator<(LocalityEntry const& source) const + { return _id < source._id; } +}; +typedef std::pair AttribRecord; + + +// This structure represents the LocalityData class as an integer map +struct KeyValueMap : public ReferenceCounted { + std::vector _keyvaluearray; + + KeyValueMap() {} + KeyValueMap(const LocalityData& data); + KeyValueMap(const KeyValueMap& entry):_keyvaluearray(entry._keyvaluearray){} + KeyValueMap& operator=(KeyValueMap const& source) { + _keyvaluearray = source._keyvaluearray; + return *this; + } + + int size() const { + return _keyvaluearray.size(); + } + + int getMemoryUsed() const { + return sizeof(_keyvaluearray) + (_keyvaluearray.size() * sizeof(AttribRecord)); + } + + Optional getValue(AttribKey const& indexKey) const { + auto itKey = std::lower_bound(_keyvaluearray.begin(), _keyvaluearray.end(), AttribRecord(indexKey, AttribValue(0)), compareKey); + return ((itKey != _keyvaluearray.end()) && (itKey->first == indexKey)) ? itKey->second : Optional(); + } + + bool isPresent( AttribKey const& indexKey ) const { + auto lower = std::lower_bound(_keyvaluearray.begin(), _keyvaluearray.end(), AttribRecord(indexKey, AttribValue(0)), compareKey); + return ((lower != _keyvaluearray.end()) && (lower->first == indexKey)); + } + bool isPresent( AttribKey indexKey, AttribValue indexValue ) const { + auto lower = std::lower_bound(_keyvaluearray.begin(), _keyvaluearray.end(), AttribRecord(indexKey, indexValue), compareKeyValue); + return ((lower != _keyvaluearray.end()) && (lower->first == indexKey) && (lower->second == indexValue)); + } + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + static bool compareKeyValue(const AttribRecord& lhs, const AttribRecord& rhs) + { return (lhs.first < rhs.first) || (!(rhs.first < lhs.first) && (lhs.second < rhs.second)); } + + static bool compareKey(const AttribRecord& lhs, const AttribRecord& rhs) + { return (lhs.first < rhs.first); } +}; + + +// This class stores the information for each entry within the locality map +struct LocalityRecord : public ReferenceCounted { + Reference _dataMap; + LocalityEntry _entryIndex; + LocalityRecord(Reference const& dataMap, int arrayIndex): _dataMap(dataMap), _entryIndex(arrayIndex) {} + LocalityRecord(LocalityRecord const& entry) : _dataMap(entry._dataMap), _entryIndex(entry._entryIndex) {} + virtual ~LocalityRecord(){} + LocalityRecord& operator=(LocalityRecord const& source) { + _dataMap = source._dataMap; + _entryIndex = source._entryIndex; + return *this; + } + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + Optional getValue(AttribKey indexKey) const { + return _dataMap->getValue(indexKey); + } + + bool isPresent(AttribKey indexKey, AttribValue indexValue) const { + return _dataMap->isPresent(indexKey, indexValue); + } + + int getMemoryUsed() const { + return sizeof(_entryIndex) + sizeof(_dataMap) + _dataMap->getMemoryUsed(); + } +}; + +// This class stores the information for string to integer map for keys and values +struct StringToIntMap : public ReferenceCounted { + std::map _hashmap; + std::vector _lookuparray; + StringToIntMap() {} + StringToIntMap(StringToIntMap const& source):_hashmap(source._hashmap), _lookuparray(source._lookuparray){} + virtual ~StringToIntMap(){} + StringToIntMap& operator=(StringToIntMap const& source) { + _hashmap = source._hashmap; + _lookuparray = source._lookuparray; + return *this; + } + void clear() { + _hashmap.clear(); + _lookuparray.clear(); + } + void copy(StringToIntMap const& source) { + _hashmap = source._hashmap; + _lookuparray = source._lookuparray; + } + std::string lookupString(int hashValue) const + { return (hashValue < _lookuparray.size()) ? _lookuparray[hashValue] : ""; } + int convertString( std::string const& value) { + int hashValue; + auto itValue = _hashmap.find(value); + if (itValue != _hashmap.end()) { + hashValue = itValue->second; + } + else { + hashValue = _hashmap.size(); + _hashmap[value] = hashValue; + _lookuparray.push_back(value); + } + return hashValue; + } + int convertString( char const* value) + { return convertString(std::string(value)); } + int convertString( StringRef const& value) + { return convertString(value.printable()); } + int convertString( Optional> const& value) + { return convertString((value.present()) ? value.get().printable() : ""); } + + int getMemoryUsed() const { + int memSize = sizeof(_hashmap) + sizeof(_lookuparray); + for (auto& hashRecord : _hashmap) { + memSize += hashRecord.first.size() + sizeof(hashRecord.second); + } + for (auto& lookup : _lookuparray) { + memSize += lookup.size(); + } + return memSize; + } + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } +}; + +extern const std::vector emptyEntryArray; + +#endif diff --git a/fdbrpc/ReplicationUtils.cpp b/fdbrpc/ReplicationUtils.cpp new file mode 100644 index 0000000000..098b341307 --- /dev/null +++ b/fdbrpc/ReplicationUtils.cpp @@ -0,0 +1,818 @@ +/* + * ReplicationUtils.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ReplicationUtils.h" +#include "flow/Hash3.h" +#include "flow/UnitTest.h" +#include "flow/Platform.h" +#include "ReplicationPolicy.h" +#include "Replication.h" + + +double ratePolicy( + LocalitySetRef & localitySet, + IRepPolicyRef const& policy, + unsigned int nTestTotal) +{ + double rating = -1.0; + unsigned int uniqueResults = 0; + int uniqueSet; + std::map, int> setMap; + std::map counterMap; + std::vector results; + + for (auto testIndex = 0; testIndex < nTestTotal; testIndex ++) + { + results.clear(); + if (!policy->selectReplicas(localitySet, results)) { + printf("Failed to apply policy: %s to %d entries\n", policy->info().c_str(), localitySet->size()); + localitySet->DisplayEntries("rate"); + ASSERT(0); + continue; + } + + uniqueSet = setMap[std::set(results.begin(), results.end())] ++; + + if (!uniqueSet) + { + uniqueResults ++; + for (auto& result : results) { + counterMap[result] ++; + } + } + } + + if (uniqueResults) + { + int largestMode = 0; + LocalityEntry largestEntry; + + for (auto& counterItem : counterMap) { + if (counterItem.second > largestMode) { + largestMode = counterItem.second; + largestEntry = counterItem.first; + } + } + rating = (double) largestMode / (double) uniqueResults; + if (g_replicationdebug > 4) { + printf("Rate entries:\n"); + localitySet->DisplayEntries("rate"); + } + if (g_replicationdebug > 3) { + printf(" largest: (%5d) %7.5f %7d of%7u %s", largestMode, rating, uniqueResults, nTestTotal, localitySet->getEntryInfo(largestEntry).c_str()); + } + } + + return rating; +} + +bool findBestPolicySet( + std::vector& bestResults, + LocalitySetRef & localitySet, + IRepPolicyRef const& policy, + unsigned int nMinItems, + unsigned int nSelectTests, + unsigned int nPolicyTests) +{ + bool bSucceeded = true; + LocalitySetRef bestLocalitySet, testLocalitySet; + std::vector results; + double testRate, bestRate = -1.0; + + if (g_replicationdebug > 3) { + printf("Finding best from LocalitySet:\n"); + localitySet->DisplayEntries(); + } + + for (auto policyTest=0; policyTest < nPolicyTests; policyTest ++) + { + results.clear(); + if (!policy->selectReplicas(localitySet, results)) { + bSucceeded = false; + break; + } + + if (g_replicationdebug > 5) { + printf("policy set #%5d:\n", policyTest); + LocalitySet::staticDisplayEntries(localitySet, results, "result"); + } + + // Get some additional random items, if needed + if ((nMinItems > results.size()) && + (!localitySet->random(results, results, nMinItems-results.size()))) + { + bSucceeded = false; + break; + } + + if (g_replicationdebug > 4) { + printf("policy with extras #%5d:\n", policyTest); + LocalitySet::staticDisplayEntries(localitySet, results, "extra "); + } + + // Create the test locality Set + testLocalitySet = localitySet->restrict(results); + + // Get the test rate + testRate = ratePolicy(testLocalitySet, policy, nSelectTests); + + if (g_replicationdebug > 3) { + printf(" rate: %7.5\n", testRate); + } + + if (bestRate < 0.0) + { + bestResults = results; + bestRate = testRate; + bestLocalitySet = testLocalitySet; + } + // Allow the occasional bad comparison, if buggified + else if (!BUGGIFY ? (testRate < bestRate) : (testRate > bestRate)) + { + bestResults = results; + bestRate = testRate; + bestLocalitySet = testLocalitySet; + } + } + + if (g_replicationdebug > 2) { + printf("BestSet: %7.5f\n", bestRate); + if (bestRate >= 0.0) bestLocalitySet->DisplayEntries(); + } + + return bSucceeded; +} + +bool findBestUniquePolicySet( + std::vector& bestResults, + LocalitySetRef & localitySet, + IRepPolicyRef const& policy, + StringRef localityUniquenessKey, + unsigned int nMinItems, + unsigned int nSelectTests, + unsigned int nPolicyTests) +{ + bool bSucceeded = true; + LocalitySetRef bestLocalitySet, testLocalitySet; + std::vector results; + double testRate, bestRate = -1.0; + + if (g_replicationdebug > 3) { + printf("Finding best unique from LocalitySet: %3d\n", localitySet->size()); + localitySet->DisplayEntries(); + } + + for (auto policyTest=0; policyTest < nPolicyTests; policyTest ++) + { + results.clear(); + if (!policy->selectReplicas(localitySet, results)) { + bSucceeded = false; + break; + } + + if (g_replicationdebug > 5) { + printf("policy set #%5d:\n", policyTest); + LocalitySet::staticDisplayEntries(localitySet, results, "result"); + } + + // Get some additional random unique items, if needed + if (nMinItems > results.size()) + { + std::vector exclusionList; + auto keyIndex = localitySet->keyIndex(localityUniquenessKey); + + for (auto& result : results) { + auto& entryValue = localitySet->getValueViaEntry(result, keyIndex); + localitySet->getMatches(exclusionList, keyIndex, entryValue.get()); + } + + if (g_replicationdebug > 7) { + printf("Excluded: %3lu\n", exclusionList.size()); + LocalitySet::staticDisplayEntries(localitySet, exclusionList, "exclude "); + } + + while ((nMinItems > results.size()) && + (localitySet->random(results, exclusionList, 1))) + { + auto& entryValue = localitySet->getValueViaEntry(results.back(), keyIndex); + localitySet->getMatches(exclusionList, keyIndex, entryValue.get()); + } + + if (g_replicationdebug > 6) { + printf("Final: %3lu\n", results.size()); + LocalitySet::staticDisplayEntries(localitySet, results, "final "); + } + } + + if (g_replicationdebug > 4) { + printf("policy with extras #%5d:\n", policyTest); + LocalitySet::staticDisplayEntries(localitySet, results, "extra "); + } + + // Create the test locality Set + testLocalitySet = localitySet->restrict(results); + + // Get the test rate + testRate = ratePolicy(testLocalitySet, policy, nSelectTests); + + if (g_replicationdebug > 3) { + printf(" rate: %7.5f\n", testRate); + } + + if (bestRate < 0.0) + { + bestResults = results; + bestRate = testRate; + bestLocalitySet = testLocalitySet; + } + // Allow the occasional bad comparison, if buggified + else if (!BUGGIFY ? (testRate < bestRate) : (testRate > bestRate)) + { + bestResults = results; + bestRate = testRate; + bestLocalitySet = testLocalitySet; + } + } + + if (g_replicationdebug > 2) { + printf("BestSet: %7.5f\n", bestRate); + bestLocalitySet->DisplayEntries(); + } + + return bSucceeded; +} + +bool validateAllCombinations( + std::vector & offendingCombo, + LocalityGroup const& localitySet, + IRepPolicyRef const& policy, + std::vector const& newItems, + unsigned int nCombinationSize, + bool bCheckIfValid) +{ + bool bValid = true; + + if (newItems.size() < nCombinationSize) { + bValid = false; + } + // Ensure that the current set alone does not satisfy the + // specified policy + else if ((bCheckIfValid) && + (!localitySet.validate(policy))) + { + bValid = false; + } + else if ((!bCheckIfValid) && + (localitySet.validate(policy)) ) + { + bValid = false; + } + else + { + bool bIsValidGroup; + LocalityGroup localityGroup; + std::string bitmask(nCombinationSize, 1); // K leading 1's + + bitmask.resize(newItems.size(), 0); // N-K trailing 0's + + do + { + localityGroup.deep_copy(localitySet); + + // [0..N-1] integers + for (int i = 0; i < newItems.size(); ++i) { + if (bitmask[i]) { + localityGroup.add(newItems[i]); + } + } + + // Check if the group combination passes validation + bIsValidGroup = localityGroup.validate(policy); + + if (((bCheckIfValid) && + (!bIsValidGroup) ) || + ((!bCheckIfValid) && + (bIsValidGroup) ) ) + { + offendingCombo.reserve(nCombinationSize); + for (int i = 0; i < newItems.size(); ++i) { + if (bitmask[i]) { + offendingCombo.push_back(newItems[i]); + } + } + if (g_replicationdebug > 2) { + printf("Invalid group\n"); + localityGroup.DisplayEntries(); + } + if (g_replicationdebug > 3) { + printf("Full set\n"); + localitySet.DisplayEntries(); + } + bValid = false; + break; + } + } + // permute bitmask + while (std::prev_permutation(bitmask.begin(), bitmask.end())); + } + return bValid; +} + +bool validateAllCombinations( + LocalityGroup const& localitySet, + IRepPolicyRef const& policy, + std::vector const& newItems, + unsigned int nCombinationSize, + bool bCheckIfValid) +{ + std::vector invalidCombo; + return validateAllCombinations(invalidCombo, localitySet, + policy, newItems, nCombinationSize, bCheckIfValid); +} + +repTestType convertToTestType(int iValue) { + std::string sValue; + char cValue; + do { + cValue = char(int('A') + (iValue % 26)); + sValue += std::string(1, cValue); + iValue /= 26; + } while (iValue > 0); + return sValue; +} + +LocalitySetRef createTestLocalityMap(std::vector& indexes, int dcTotal, + int szTotal, int rackTotal, int slotTotal, int independentItems, int independentTotal) +{ + LocalitySetRef buildServer(new LocalityMap()); + LocalityMap* serverMap = (LocalityMap*) buildServer.getPtr(); + int serverValue, dcLoop, szLoop, rackLoop, slotLoop; + std::string dcText, szText, rackText, slotText, independentName, independentText; + + // Determine the total size + serverValue = dcTotal * ((szTotal * rackTotal) + (szTotal+2)*(rackTotal+2)) * slotTotal; + + if (g_replicationdebug > 0) { + printf("DC:%2d SZ:%2d AZ:%2d Rack:%2d Slot:%2d Extra:%2d Xitems:%2d Size:%4d\n", dcTotal, szTotal, szTotal+2, rackTotal, slotTotal, independentItems, independentTotal, serverValue); + } + indexes.reserve(serverValue); + + for (int dcLoop = 0; dcLoop < dcTotal; dcLoop ++) { + serverValue = dcLoop; + dcText = format("dc%d", dcLoop); + for (int szLoop = 0; szLoop < szTotal; szLoop ++) { + serverValue = dcLoop + szLoop * 10; + szText = format(".s%d", szLoop); + for (int rackLoop = 0; rackLoop < rackTotal; rackLoop ++) { + serverValue = dcLoop + szLoop * 10 + rackLoop * 100; + rackText = format(".%d", rackLoop); + for (int slotLoop = 0; slotLoop < slotTotal; slotLoop ++) { + serverValue = dcLoop + szLoop * 10 + rackLoop * 100 + slotLoop * 1000; + slotText = format(".%d", slotLoop); + LocalityData data; + data.set(LiteralStringRef("dc"), StringRef(dcText)); + data.set(LiteralStringRef("sz"), StringRef(dcText+szText)); + data.set(LiteralStringRef("rack"), StringRef(dcText+szText+rackText)); + data.set(LiteralStringRef("zoneid"), StringRef(dcText+szText+rackText+slotText)); + for (int independentLoop = 0; independentLoop < independentItems; independentLoop ++) { + independentName = format("indiv%02d", independentLoop+1); + for (int totalLoop = 0; totalLoop < independentTotal; totalLoop ++) { + independentText = format("i%02d", totalLoop+1); + data.set(StringRef(independentName), StringRef(independentText)); + } + } + indexes.push_back(convertToTestType(indexes.size())); + serverMap->add(data, &indexes.back()); + } + } + } + + for (int szLoop = 0; szLoop < szTotal+2; szLoop ++) { + serverValue = (dcLoop+2) + szLoop * 10; + szText = format(".a%d", szLoop); + for (int rackLoop = 0; rackLoop < rackTotal+2; rackLoop ++) { + serverValue = (dcLoop+2) + szLoop * 10 + rackLoop * 100; + rackText = format(".%d", rackLoop); + for (int slotLoop = 0; slotLoop < slotTotal; slotLoop ++) { + serverValue = (dcLoop+2) + szLoop * 10 + rackLoop * 100 + slotLoop * 1000; + slotText = format(".%d", slotLoop); + LocalityData data; + data.set(LiteralStringRef("dc"), StringRef(dcText)); + data.set(LiteralStringRef("az"), StringRef(dcText+szText)); + data.set(LiteralStringRef("rack"), StringRef(dcText+szText+rackText)); + data.set(LiteralStringRef("zoneid"), StringRef(dcText+szText+rackText+slotText)); + for (int independentLoop = 0; independentLoop < independentItems; independentLoop ++) { + independentName = format("indiv%02d", independentLoop); + for (int totalLoop = 0; totalLoop < independentTotal; totalLoop ++) { + independentText = format("i%02d", totalLoop); + data.set(StringRef(independentName), StringRef(independentText)); + } + } + indexes.push_back(convertToTestType(indexes.size())); + serverMap->add(data, &indexes.back()); + } + } + } + } + + if (g_replicationdebug > 1) printf("Created: %3d servers\n", buildServer->size()); + + if (g_replicationdebug > 1) { + buildServer->DisplayEntries(); + } + + return buildServer; +} + +bool testPolicy( + LocalitySetRef servers, + IRepPolicyRef const& policy, + std::vector const& including, + bool validate) +{ + LocalityMap* serverMap = (LocalityMap*) servers.getPtr(); + std::string outputText, includeText; + std::vector entryResults; + std::vector results; + int resultsTotal; + bool valid, solved; + + if (g_replicationdebug > 1) { + printf("Policy test: include:%4lu policy: %-10s => %s\n", including.size(), policy->name().c_str(), policy->info().c_str()); + } + if (g_replicationdebug > 2) { + for (auto& entry : including) { + printf(" also: %s\n", servers->getEntryInfo(entry).c_str()); + } + } + + solved = serverMap->selectReplicas(policy, including, entryResults, results); + + if (g_replicationdebug > 1) { + printf("%-10s solution:%3lu policy: %-10s => %s include:%4d\n", ((solved) ? "Solved" : "Unsolved"), results.size(), policy->name().c_str(), policy->info().c_str(), including.size()); + } + if (g_replicationdebug > 2) { + for (auto& entry : entryResults) { + printf(" item: %s\n", servers->getEntryInfo(entry).c_str()); + } + for (auto& entry : including) { + printf(" also: %s\n", servers->getEntryInfo(entry).c_str()); + } + } + + valid = (validate) ? policy->validateFull(solved, entryResults, including, servers) : true; + + if (g_replicationdebug > 0) { + if (including.size()) { + includeText = " with "; + for (auto& entry : including) { + includeText += " " + servers->getEntryInfo(entry); + } + } + + if (results.size()) { + outputText = policy->info() + includeText + " -> "; + int count=0; + for (auto& entry : entryResults) { + outputText += " " + *results[count] + "-" + servers->getEntryInfo(entry); + count ++; + } + } + else { + outputText = policy->info() + includeText + ((solved) ? " -> None" : " -> No solution"); + } + + printf("%-5s:%3d %s\n", (valid) ? "Valid" : "Error", resultsTotal, outputText.c_str()); + } + + return valid; +} + +bool testPolicy( + LocalitySetRef servers, + IRepPolicyRef const& policy, + bool validate) +{ + return testPolicy(servers, policy, emptyEntryArray, validate); +} + + +std::vector const& getStaticPolicies() +{ + static std::vector staticPolicies; + + if (staticPolicies.empty()) + { + staticPolicies = { + + IRepPolicyRef( new PolicyOne() ), + + // 1 'dc^2 x 1' + IRepPolicyRef( new PolicyAcross(2, "dc", IRepPolicyRef( new PolicyOne() ) ) ), + + // 2 'dc^3 x 1' + IRepPolicyRef( new PolicyAcross(3, "dc", IRepPolicyRef( new PolicyOne() ) ) ), + + // 3 'sz^3 x 1' + IRepPolicyRef( new PolicyAcross(3, "sz", IRepPolicyRef( new PolicyOne() ) ) ), + + // 4 'dc^1 x az^3 x 1' + IRepPolicyRef( new PolicyAcross(1, "dc", IRepPolicyRef( new PolicyAcross(3, "az", IRepPolicyRef( new PolicyOne() ))) ) ), + + // 5 '(sz^3 x rack^2 x 1) + (dc^2 x az^3 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(3, "sz", IRepPolicyRef(new PolicyAcross(2, "rack", IRepPolicyRef(new PolicyOne() ))))), IRepPolicyRef(new PolicyAcross(2, "dc", IRepPolicyRef(new PolicyAcross(3, "az", IRepPolicyRef(new PolicyOne()) ))) )} ) ), + + // 6 '(sz^1 x 1)' + IRepPolicyRef( new PolicyAcross(1, "sz", IRepPolicyRef(new PolicyOne())) ), + + // 7 '(sz^1 x 1) + (sz^1 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(1, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(1, "sz", IRepPolicyRef(new PolicyOne()))) } ) ), + + // 8 '(sz^2 x 1) + (sz^2 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))) } ) ), + + // 9 '(dc^1 x sz^2 x 1)' + IRepPolicyRef( new PolicyAcross(1, "dc", IRepPolicyRef( new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))))), + + //10 '(dc^2 x sz^2 x 1)' + IRepPolicyRef( new PolicyAcross(2, "dc", IRepPolicyRef( new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))))), + + //11 '(dc^1 x sz^2 x 1) + (dc^2 x sz^2 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(1, "dc", IRepPolicyRef( new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))))), IRepPolicyRef(new PolicyAcross(2, "dc", IRepPolicyRef( new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))))) } ) ), + + //12 '(dc^2 x sz^2 x 1) + (dc^1 x sz^2 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(2, "dc", IRepPolicyRef( new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))))), IRepPolicyRef(new PolicyAcross(1, "dc", IRepPolicyRef( new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))))) } ) ), + + //13 '(sz^2 x 1) + (dc^1 x sz^2 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(1, "dc", IRepPolicyRef( new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))))) } ) ), + + //14 '(sz^2 x 1) + (dc^2 x sz^2 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(2, "dc", IRepPolicyRef( new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))))) } ) ), + + //15 '(sz^3 x 1) + (dc^2 x sz^2 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(3, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(2, "dc", IRepPolicyRef( new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))))) } ) ), + + //16 '(sz^1 x 1) + (sz^2 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(1, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))) } ) ), + + //17 '(sz^2 x 1) + (sz^3 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(3, "sz", IRepPolicyRef(new PolicyOne()))) } ) ), + + //18 '(sz^1 x 1) + (sz^2 x 1) + (sz^3 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(1, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(3, "sz", IRepPolicyRef(new PolicyOne()))) } ) ), + + //19 '(sz^1 x 1) + (machine^1 x 1)' + IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(1, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(1, "zoneid", IRepPolicyRef(new PolicyOne()))) } ) ), + + // '(dc^1 x 1) + (sz^1 x 1) + (machine^1 x 1)' + // IRepPolicyRef( new PolicyAnd( { IRepPolicyRef(new PolicyAcross(1, "dc", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(1, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(1, "zoneid", IRepPolicyRef(new PolicyOne()))) } ) ), + + // '(dc^1 x sz^3 x 1)' + IRepPolicyRef( new PolicyAcross(1, "dc", IRepPolicyRef( new PolicyAcross(3, "sz", IRepPolicyRef(new PolicyOne())))) ), + + // '(dc^2 x sz^3 x 1)' + IRepPolicyRef( new PolicyAcross(2, "dc", IRepPolicyRef( new PolicyAcross(3, "sz", IRepPolicyRef(new PolicyOne())))) ), + + // '(dc^2 x az^3 x 1)' + IRepPolicyRef( new PolicyAcross(2, "dc", IRepPolicyRef( new PolicyAcross(3, "az", IRepPolicyRef(new PolicyOne())))) ), + + // '(sz^1 x 1) + (dc^2 x az^3 x 1)' + IRepPolicyRef( new PolicyAnd({IRepPolicyRef(new PolicyAcross(1, "sz", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(2, "dc", IRepPolicyRef( new PolicyAcross(3, "az", IRepPolicyRef(new PolicyOne())))))}) ), + + // 'dc^1 x (az^2 x 1) + (sz^2 x 1)' + // IRepPolicyRef( new PolicyAcross(1, "dc", IRepPolicyRef(new PolicyAnd({IRepPolicyRef(new PolicyAcross(2, "az", IRepPolicyRef(new PolicyOne()))), IRepPolicyRef(new PolicyAcross(2, "sz", IRepPolicyRef(new PolicyOne())))}))) ), + + // Require backtracking + IRepPolicyRef( new PolicyAcross(8, "zoneid", IRepPolicyRef(new PolicyAcross(1, "az", IRepPolicyRef(new PolicyOne()))) ) ), + IRepPolicyRef( new PolicyAcross(8, "zoneid", IRepPolicyRef(new PolicyAcross(1, "sz", IRepPolicyRef(new PolicyOne()))) ) ) + }; + } + return staticPolicies; +} + + +IRepPolicyRef const randomAcrossPolicy(LocalitySet const& serverSet) +{ + int usedKeyTotal, keysUsed, keyIndex, valueTotal, maxValueTotal, maxKeyTotal, skips, lastKeyIndex; + std::vector keyArray(serverSet.getGroupKeyMap()->_lookuparray); + std::set valueSet; + AttribKey indexKey; + Optional keyValue; + std::string keyText; + IRepPolicyRef policy(new PolicyOne()); + + // Determine the number of keys to used within the policy + usedKeyTotal = g_random->randomInt(1, keyArray.size()+1); + maxKeyTotal = g_random->randomInt(1, 4); + if ((usedKeyTotal > maxKeyTotal) && (g_random->random01() > .1)) { + usedKeyTotal = maxKeyTotal; + } + maxValueTotal = g_random->randomInt(1, 10); + keysUsed = skips = 0; + + if (g_replicationdebug > 6) { + keyIndex = 0; + for (auto& key : keyArray) { + keyIndex ++; + printf("%s key: (%2d) %-10s\n", ((keyIndex > 1) ? "" : "\n"), keyIndex, key.c_str()); + } + } + + if (g_replicationdebug > 2) printf("Policy using%3d of%3lu keys Max values:%3d\n", usedKeyTotal, keyArray.size(), maxValueTotal); + while (keysUsed < usedKeyTotal) { + keyIndex = g_random->randomInt(0, keyArray.size()-keysUsed); + keyText = keyArray[keyIndex]; + lastKeyIndex = keyArray.size() - 1 - keysUsed; + + // Do not allow az and sz within a policy, 90% of the time + if ((!keyText.compare("az")) && (g_random->random01() > .1) && + (std::find(keyArray.begin()+lastKeyIndex+1, keyArray.end(), "sz") != keyArray.end())) + { + skips ++; + } + else if ((!keyText.compare("sz")) && (g_random->random01() > .1) && + (std::find(keyArray.begin()+lastKeyIndex+1, keyArray.end(), "az") != keyArray.end())) + { + skips ++; + } + else + { + if (g_replicationdebug > 3) { + printf(" keys index:%3d value: %-10s used:%3d total:%3d size:%3lu\n", + keyIndex, keyText.c_str(), keysUsed, usedKeyTotal, keyArray.size()); + } + indexKey = serverSet.keyIndex(keyText); + valueSet.clear(); + // Determine all of the values for the key + for (auto& entry : serverSet.getEntries()) { + keyValue = serverSet.getValueViaEntry(entry, indexKey); + if (keyValue.present()) { + valueSet.insert(serverSet.valueText(keyValue.get())); + } + } + valueTotal = g_random->randomInt(1, valueSet.size()+2); + if ((valueTotal > maxValueTotal) && (g_random->random01() > .25)) valueTotal = maxValueTotal; + policy = IRepPolicyRef( new PolicyAcross(valueTotal, keyText, policy) ); + if (g_replicationdebug > 1) { + printf(" item%3d: (%3d =>%3d) %-10s =>%4d\n", keysUsed+1, keyIndex, indexKey._id, keyText.c_str(), valueTotal); + } + } + keysUsed ++; + // Move the used string to the end of the array + if (keyIndex < lastKeyIndex) { + if (g_replicationdebug > 2) { + printf(" Copying%3d into %3d\n", lastKeyIndex, keyIndex); + } + keyArray[keyIndex] = keyArray[lastKeyIndex]; + } + else if (g_replicationdebug > 2) { + printf(" Skip %3d into %3d\n", lastKeyIndex, keyIndex); + } + + if (g_replicationdebug > 6) { + keyIndex = 0; + for (auto& key : keyArray) { + keyIndex ++; + printf("%s key: (%2d) %-10s\n", ((keyIndex > 1) ? "" : "\n"), keyIndex, key.c_str()); + } + } + } + if (g_replicationdebug > 0) printf("Policy: %s\n", policy->info().c_str()); + return policy; +} + +int testReplication() +{ + const char* testTotalEnv = getenv("REPLICATION_TESTTOTAL"); + const char* debugLevelEnv = getenv("REPLICATION_DEBUGLEVEL"); + const char* policyTotalEnv = getenv("REPLICATION_POLICYTOTAL"); + const char* policyIndexEnv = getenv("REPLICATION_POLICYINDEX"); + const char* reportCacheEnv = getenv("REPLICATION_REPORTCACHE"); + const char* stopOnErrorEnv = getenv("REPLICATION_STOPONERROR"); + const char* skipTotalEnv = getenv("REPLICATION_SKIPTOTAL"); + const char* debugEntryEnv = getenv("REPLICATION_DEBUGENTRY"); + const char* validateEnv = getenv("REPLICATION_VALIDATE"); + const char* findBestEnv = getenv("REPLICATION_FINDBEST"); + const char* rateSampleEnv = getenv("REPLICATION_RATESAMPLE"); + const char* policySampleEnv = getenv("REPLICATION_POLICYSAMPLE"); + const char* policyMinEnv = getenv("REPLICATION_POLICYEXTRA"); + int totalTests = testTotalEnv ? atoi(testTotalEnv) : 10000; + int skipTotal = skipTotalEnv ? atoi(skipTotalEnv) : 0; + int findBest = findBestEnv ? atoi(findBestEnv) : 0; + int policyIndexStatic = policyIndexEnv ? atoi(policyIndexEnv) : -1; + int policyTotal = policyTotalEnv ? atoi(policyTotalEnv) : 100; + bool stopOnError = stopOnErrorEnv ? (atoi(stopOnErrorEnv) > 0) : false; + bool validate = validateEnv ? (atoi(validateEnv) > 0) : true; + int rateSample = rateSampleEnv ? atoi(rateSampleEnv) : 1000; + int policySample = policySampleEnv ? atoi(policySampleEnv) : 100; + int policyMin = policyMinEnv ? atoi(policyMinEnv) : 2; + int policyIndex, testCounter, alsoSize, debugBackup, maxAlsoSize; + std::vector serverIndexes; + LocalitySetRef testServers; + std::vector policies; + std::vector alsoServers, bestSet; + int totalErrors = 0; + + if (debugLevelEnv) g_replicationdebug = atoi(debugLevelEnv); + debugBackup = g_replicationdebug; + + testServers = createTestLocalityMap(serverIndexes, g_random->randomInt(1, 5), g_random->randomInt(1, 6), g_random->randomInt(1, 10), g_random->randomInt(1, 10), g_random->randomInt(0, 4), g_random->randomInt(1, 5)); + maxAlsoSize = testServers->size() / g_random->randomInt(2, 20); + + if (g_replicationdebug >= 0) printf("Running %d Replication test\n", totalTests); + + if ((!policyIndexEnv) || + (policyIndexStatic >= 0)) + { + policies = getStaticPolicies(); + } + else { + if (g_replicationdebug > 0) printf("Creating %3d random policies.\n", policyTotal); + policies.reserve(policyTotal); + for (auto i=0; i < policyTotal; i ++) { + if (g_replicationdebug > 0) printf(" (%3d) ", i+1); + policies.push_back(randomAcrossPolicy(*testServers)); + } + } + + for (testCounter = 0; testCounter < totalTests; testCounter ++) { + if (!skipTotal) { + } + else if (testCounter < skipTotal) { + g_replicationdebug = 1; + } + else { + g_replicationdebug = debugBackup; + skipTotal = 0; + } + alsoSize = g_random->randomInt(0, testServers->size()+1); + if ((alsoSize > maxAlsoSize) && (g_random->random01() > .2)) { + alsoSize = maxAlsoSize; + } + + if ((!alsoSize) && (alsoServers.size() > 0)) { + alsoServers.clear(); + } + else { + alsoServers = testServers->getEntries(); + g_random->randomShuffle(alsoServers); + if (alsoSize < testServers->size()) { + alsoServers.resize(alsoSize); + } + } + + policyIndex = (policyIndexStatic>=0) ? policyIndexStatic : g_random->randomInt(0, policies.size()); + + if (g_replicationdebug > 0) printf(" #%7d: (%3d) ", testCounter, policyIndex); + + if (findBest) + { + auto bSucceeded = findBestPolicySet(bestSet, testServers, policies[policyIndex], policyMin, rateSample, policySample); + + if (g_replicationdebug > 1) { + printf("BestSet:%4lu entries\n", bestSet.size()); + LocalitySet::staticDisplayEntries(testServers, bestSet, "best"); + } + + if (g_replicationdebug > 0) printf("%7lu %s\n", bestSet.size(), policies[policyIndex]->info().c_str()); + } + + else if (!testPolicy(testServers, policies[policyIndex], alsoServers, validate)) { + totalErrors ++; + if (stopOnError) break; + } + } + if (g_replicationdebug >= 0) printf("Succeeded in completing %d of %d policies\n", testCounter-totalErrors, totalTests); + if ((g_replicationdebug > 0) || ((reportCacheEnv) && (atoi(reportCacheEnv) > 0))) { + testServers->cacheReport(); + } + + return totalErrors; +} + +TEST_CASE("fdbrpc/Replication/test") { + printf("Running replication test\n"); + + platform::setEnvironmentVar("REPLICATION_STOPONERROR", "1", 0); + platform::setEnvironmentVar("REPLICATION_VALIDATE", "1", 0); + + ASSERT(testReplication() == 0); + return Void(); +} diff --git a/fdbrpc/ReplicationUtils.h b/fdbrpc/ReplicationUtils.h new file mode 100644 index 0000000000..27d7e284ac --- /dev/null +++ b/fdbrpc/ReplicationUtils.h @@ -0,0 +1,74 @@ +/* + * ReplicationUtils.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_REPLICATIONTEST_H +#define FLOW_REPLICATIONTEST_H +#pragma once + +#include "flow/flow.h" +#include "ReplicationTypes.h" + + +typedef std::string repTestType; + +extern repTestType convertToTestType(int iValue); + + +extern int testReplication(); + +extern double ratePolicy( + LocalitySetRef & localitySet, + IRepPolicyRef const& policy, + unsigned int nSelectTests); + +extern bool findBestPolicySet( + std::vector& bestResults, + LocalitySetRef & localitySet, + IRepPolicyRef const& policy, + unsigned int nMinItems, + unsigned int nSelectTests, + unsigned int nPolicyTests); + +extern bool findBestUniquePolicySet( + std::vector& bestResults, + LocalitySetRef & localitySet, + IRepPolicyRef const& policy, + StringRef localityUniquenessKey, + unsigned int nMinItems, + unsigned int nSelectTests, + unsigned int nPolicyTests); + +// The following function will return TRUE if all possible combinations +// of the new Item array will not pass the specified policy +extern bool validateAllCombinations( + std::vector & offendingCombo, + LocalityGroup const& localitySet, + IRepPolicyRef const& policy, + std::vector const& newItems, + unsigned int nCombinationSize, + bool bCheckIfValid = true); + +extern bool validateAllCombinations( + LocalityGroup const& localitySet, + IRepPolicyRef const& policy, + std::vector const& newItems, + unsigned int nCombinationSize, + bool bCheckIfValid = true); +#endif diff --git a/fdbrpc/Smoother.h b/fdbrpc/Smoother.h new file mode 100644 index 0000000000..1eb8d56ecf --- /dev/null +++ b/fdbrpc/Smoother.h @@ -0,0 +1,93 @@ +/* + * Smoother.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_SMOOTHER_H +#define FLOW_SMOOTHER_H +#pragma once + +#include "flow/flow.h" + +struct Smoother { + // Times (t) are expected to be nondecreasing + + explicit Smoother( double eFoldingTime ) : eFoldingTime(eFoldingTime) { reset(0); } + void reset(double value) { time = 0; total = value; estimate = value; } + + void setTotal( double total, double t = now() ) { addDelta( total - this->total, t); } + void addDelta( double delta, double t = now() ) { + update(t); + total += delta; + } + // smoothTotal() is a continuous (under)estimate of the sum of all addDeltas() + double smoothTotal( double t = now() ) { + update(t); + return estimate; + } + // smoothRate() is d/dt[smoothTotal], and is NOT continuous + double smoothRate( double t = now() ) { + update(t); + return (total-estimate) / eFoldingTime; + } + + void update(double t) { + double elapsed = t - time; + if(elapsed) { + time = t; + estimate += (total-estimate) * (1-exp( -elapsed/eFoldingTime )); + } + } + + double eFoldingTime; + double time, total, estimate; +}; + +struct TimerSmoother { + // Times (t) are expected to be nondecreasing + + explicit TimerSmoother( double eFoldingTime ) : eFoldingTime(eFoldingTime) { reset(0); } + void reset(double value) { time = 0; total = value; estimate = value; } + + void setTotal( double total, double t = timer() ) { addDelta( total - this->total, t); } + void addDelta( double delta, double t = timer() ) { + update(t); + total += delta; + } + // smoothTotal() is a continuous (under)estimate of the sum of all addDeltas() + double smoothTotal( double t = timer() ) { + update(t); + return estimate; + } + // smoothRate() is d/dt[smoothTotal], and is NOT continuous + double smoothRate( double t = timer() ) { + update(t); + return (total-estimate) / eFoldingTime; + } + + void update(double t) { + double elapsed = t - time; + time = t; + estimate += (total-estimate) * (1-exp( -elapsed/eFoldingTime )); + } + + double eFoldingTime; + double time, total, estimate; +}; + +#endif \ No newline at end of file diff --git a/fdbrpc/TLSConnection.actor.cpp b/fdbrpc/TLSConnection.actor.cpp new file mode 100644 index 0000000000..275f9fc6fc --- /dev/null +++ b/fdbrpc/TLSConnection.actor.cpp @@ -0,0 +1,323 @@ +/* + * TLSConnection.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/network.h" + +#include "TLSConnection.h" + +#include "ITLSPlugin.h" +#include "LoadPlugin.h" +#include "Platform.h" + +// Must not throw an exception from this function! +static int send_func(void* ctx, const uint8_t* buf, int len) { + TLSConnection* conn = (TLSConnection*)ctx; + + try { + SendBuffer sb; + sb.bytes_sent = 0; + sb.bytes_written = len; + sb.data = buf; + sb.next = 0; + + int w = conn->conn->write( &sb ); + return w; + } catch ( Error& e ) { + TraceEvent("TLSConnectionSendError", conn->getDebugID()).error(e); + return -1; + } catch ( ... ) { + TraceEvent("TLSConnectionSendError", conn->getDebugID()).error( unknown_error() ); + return -1; + } +} + +// Must not throw an exception from this function! +static int recv_func(void* ctx, uint8_t* buf, int len) { + TLSConnection* conn = (TLSConnection*)ctx; + + try { + int r = conn->conn->read( buf, buf + len ); + return r; + } catch ( Error& e ) { + TraceEvent("TLSConnectionRecvError", conn->getDebugID()).error(e); + return -1; + } catch ( ... ) { + TraceEvent("TLSConnectionRecvError", conn->getDebugID()).error( unknown_error() ); + return -1; + } +} + +ACTOR static Future handshake( TLSConnection* self ) { + loop { + int r = self->session->handshake(); + if ( r == ITLSSession::SUCCESS ) break; + if ( r == ITLSSession::FAILED ) { + TraceEvent("TLSConnectionHandshakeError", self->getDebugID()); + throw connection_failed(); + } + ASSERT( r == ITLSSession::WANT_WRITE || r == ITLSSession::WANT_READ ); + Void _ = wait( r == ITLSSession::WANT_WRITE ? self->conn->onWritable() : self->conn->onReadable() ); + } + + TraceEvent("TLSConnectionHandshakeSuccessful", self->getDebugID()) + .detail("Peer", self->getPeerAddress()); + + return Void(); +} + +TLSConnection::TLSConnection( Reference const& conn, Reference const& policy, bool is_client ) : conn(conn), write_wants(0), read_wants(0), uid(conn->getDebugID()) { + session = Reference( policy->create_session(is_client, send_func, this, recv_func, this, (void*)&uid) ); + if ( !session ) { + // If session is NULL, we're trusting policy->create_session + // to have used its provided logging function to have logged + // the error + throw internal_error(); + } + handshook = handshake(this); +} + +Future TLSConnection::onWritable() { + if ( !handshook.isReady() ) + return handshook; + return + write_wants == ITLSSession::WANT_READ ? conn->onReadable() : + write_wants == ITLSSession::WANT_WRITE ? conn->onWritable() : + Void(); +} + +Future TLSConnection::onReadable() { + if ( !handshook.isReady() ) + return handshook; + return + read_wants == ITLSSession::WANT_READ ? conn->onReadable() : + read_wants == ITLSSession::WANT_WRITE ? conn->onWritable() : + Void(); +} + +int TLSConnection::read( uint8_t* begin, uint8_t* end ) { + if ( !handshook.isReady() ) return 0; + handshook.get(); + + read_wants = 0; + int r = session->read( begin, end - begin ); + if ( r > 0 ) + return r; + + if ( r == ITLSSession::FAILED ) throw connection_failed(); + + ASSERT( r == ITLSSession::WANT_WRITE || r == ITLSSession::WANT_READ ); + + read_wants = r; + return 0; +} + +int TLSConnection::write( SendBuffer const* buffer, int limit ) { + ASSERT(limit > 0); + + if ( !handshook.isReady() ) return 0; + handshook.get(); + + write_wants = 0; + int toSend = std::min(limit, buffer->bytes_written - buffer->bytes_sent); + ASSERT(toSend); + int w = session->write( buffer->data + buffer->bytes_sent, toSend ); + if ( w > 0 ) + return w; + + if ( w == ITLSSession::FAILED ) throw connection_failed(); + + ASSERT( w == ITLSSession::WANT_WRITE || w == ITLSSession::WANT_READ ); + + write_wants = w; + return 0; +} + +ACTOR Future> wrap( Reference policy, bool is_client, Future> c ) { + Reference conn = wait(c); + return Reference(new TLSConnection( conn, policy, is_client )); +} + +Future> TLSListener::accept() { + return wrap( policy, false, listener->accept() ); +} + +TLSNetworkConnections::TLSNetworkConnections( Reference options ) : options(options) { + network = INetworkConnections::net(); + g_network->setGlobal(INetwork::enumGlobal::enNetworkConnections, (flowGlobalType) this); +} + +Future> TLSNetworkConnections::connect( NetworkAddress toAddr ) { + if ( toAddr.isTLS() ) { + NetworkAddress clearAddr( toAddr.ip, toAddr.port, toAddr.isPublic(), false ); + TraceEvent("TLSConnectionConnecting").detail("ToAddr", toAddr); + return wrap( options->get_policy(), true, network->connect( clearAddr ) ); + } + return network->connect( toAddr ); +} + +Reference TLSNetworkConnections::listen( NetworkAddress localAddr ) { + if ( localAddr.isTLS() ) { + NetworkAddress clearAddr( localAddr.ip, localAddr.port, localAddr.isPublic(), false ); + TraceEvent("TLSConnectionListening").detail("OnAddr", localAddr); + return Reference(new TLSListener( options->get_policy(), network->listen( clearAddr ) )); + } + return network->listen( localAddr ); +} + +// 5MB for loading files into memory +#define CERT_FILE_MAX_SIZE (5 * 1024 * 1024) + +void TLSOptions::set_plugin_name_or_path( std::string const& plugin_name_or_path ) { + if ( plugin ) + throw invalid_option(); + + init_plugin( plugin_name_or_path ); +} + +void TLSOptions::set_cert_file( std::string const& cert_file ) { + try { + TraceEvent("TLSConnectionSettingCertFile").detail("CertFilePath", cert_file); + set_cert_data( readFileBytes( cert_file, CERT_FILE_MAX_SIZE ) ); + } catch ( Error& ) { + TraceEvent(SevError, "TLSOptionsSetCertFileError").detail("Filename", cert_file); + throw; + } +} + +void TLSOptions::set_cert_data( std::string const& cert_data ) { + if ( !policy ) + init_plugin(); + + TraceEvent("TLSConnectionSettingCertData").detail("CertDataSize", cert_data.size()); + if ( !policy->set_cert_data( (const uint8_t*)&cert_data[0], cert_data.size() ) ) + throw tls_error(); + + certs_set = true; +} + +void TLSOptions::set_key_file( std::string const& key_file ) { + try { + TraceEvent("TLSConnectionSettingKeyFile").detail("KeyFilePath", key_file); + set_key_data( readFileBytes( key_file, CERT_FILE_MAX_SIZE ) ); + } catch ( Error& ) { + TraceEvent(SevError, "TLSOptionsSetKeyFileError").detail("Filename", key_file); + throw; + } +} + +void TLSOptions::set_key_data( std::string const& key_data ) { + if ( !policy ) + init_plugin(); + + TraceEvent("TLSConnectionSettingKeyData").detail("KeyDataSize", key_data.size()); + if ( !policy->set_key_data( (const uint8_t*)&key_data[0], key_data.size() ) ) + throw tls_error(); + + key_set = true; +} + +void TLSOptions::set_verify_peers( std::string const& verify_peers ) { + if ( !policy ) + init_plugin(); + + TraceEvent("TLSConnectionSettingVerifyPeers").detail("Value", verify_peers); + if ( !policy->set_verify_peers( (const uint8_t*)&verify_peers[0], verify_peers.size() ) ) + throw tls_error(); + + verify_peers_set = true; +} + +void TLSOptions::register_network() { + new TLSNetworkConnections( Reference::addRef( this ) ); +} + +const char *defaultCertFileName = "fdb.pem"; + +Reference TLSOptions::get_policy() { + if ( !certs_set ) { + std::string certFile; + if ( !platform::getEnvironmentVar( "FDB_TLS_CERTIFICATE_FILE", certFile ) ) + certFile = fileExists(defaultCertFileName) ? defaultCertFileName : joinPath(platform::getDefaultConfigPath(), defaultCertFileName); + set_cert_file( certFile ); + } + if ( !key_set ) { + std::string keyFile; + if ( !platform::getEnvironmentVar( "FDB_TLS_KEY_FILE", keyFile ) ) + keyFile = fileExists(defaultCertFileName) ? defaultCertFileName : joinPath(platform::getDefaultConfigPath(), defaultCertFileName); + set_key_file( keyFile ); + } + if( !verify_peers_set ) { + std::string verifyPeerString; + if ( platform::getEnvironmentVar( "FDB_TLS_VERIFY_PEERS", verifyPeerString ) ) + set_verify_peers( verifyPeerString ); + } + + return policy; +} + +static void TLSConnectionLogFunc( const char* event, void* uid_ptr, bool is_error, ... ) { + UID uid; + + if ( uid_ptr ) + uid = *(UID*)uid_ptr; + + Severity s = SevInfo; + if ( is_error ) + s = SevError; + + auto t = TraceEvent( s, event, uid ); + + va_list ap; + char* field; + + va_start( ap, is_error ); + while ( (field = va_arg( ap, char* )) ) { + t.detail( field, va_arg( ap, char* ) ); + } + va_end( ap ); +} + +void TLSOptions::init_plugin( std::string const& plugin_path ) { + std::string path; + + if ( plugin_path.length() ) { + path = plugin_path; + } else { + if ( !platform::getEnvironmentVar( "FDB_TLS_PLUGIN", path ) ) + // FIXME: should there be other fallbacks? + path = platform::getDefaultPluginPath("FDBGnuTLS"); + } + + TraceEvent("TLSConnectionLoadingPlugin").detail("PluginPath", path); + plugin = loadPlugin( path.c_str() ); + if ( !plugin ) { + // FIXME: allow? + TraceEvent(SevError, "TLSConnectionPluginInitError").detail("Plugin", path).GetLastError(); + throw tls_error(); + } + + policy = Reference( plugin->create_policy( TLSConnectionLogFunc ) ); + if ( !policy ) { + // Hopefully create_policy logged something with the log func + TraceEvent(SevError, "TLSConnectionCreatePolicyError"); + throw tls_error(); + } +} diff --git a/fdbrpc/TLSConnection.h b/fdbrpc/TLSConnection.h new file mode 100644 index 0000000000..3ddf32c300 --- /dev/null +++ b/fdbrpc/TLSConnection.h @@ -0,0 +1,146 @@ +/* + * TLSConnection.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_TLSCONNECTION_H +#define FLOW_TLSCONNECTION_H + +#pragma once + +#include "flow/Platform.h" + +#include "ITLSPlugin.h" + +struct TLSConnection : IConnection, ReferenceCounted { + Reference conn; + Reference session; + + Future handshook; + + int write_wants, read_wants; + + UID uid; + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + TLSConnection( Reference const& conn, Reference const& policy, bool is_client ); + ~TLSConnection() { + // Here for ordering to make sure we delref the ITLSSession + // which has a pointer to this object + session.clear(); + } + + virtual void close() { conn->close(); } + + virtual Future onWritable(); + + virtual Future onReadable(); + + virtual int read( uint8_t* begin, uint8_t* end ); + + virtual int write( SendBuffer const* buffer, int limit); + + virtual NetworkAddress getPeerAddress() { + NetworkAddress a = conn->getPeerAddress(); + return NetworkAddress(a.ip, a.port, a.isPublic(), true); + } + + virtual UID getDebugID() { return uid; } +}; + +struct TLSListener : IListener, ReferenceCounted { + Reference listener; + Reference policy; + + TLSListener( Reference policy, Reference listener ) : policy(policy), listener(listener) {} + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + virtual Future> accept(); + + virtual NetworkAddress getListenAddress() { return listener->getListenAddress(); } +}; + +struct TLSOptions : ReferenceCounted { + enum { OPT_TLS = 100000, OPT_TLS_PLUGIN, OPT_TLS_CERTIFICATES, OPT_TLS_KEY, OPT_TLS_VERIFY_PEERS }; + + TLSOptions() : certs_set(false), key_set(false), verify_peers_set(false) {} + + void set_plugin_name_or_path( std::string const& plugin_name_or_path ); + void set_cert_file( std::string const& cert_file ); + void set_cert_data( std::string const& cert_data ); + void set_key_file( std::string const& key_file ); + void set_key_data( std::string const& key_data ); + void set_verify_peers( std::string const& verify_peers ); + + void register_network(); + + Reference get_policy(); + +private: + void init_plugin( std::string const& plugin_path = "" ); + + Reference plugin; + Reference policy; + bool certs_set, key_set, verify_peers_set; +}; + +struct TLSNetworkConnections : INetworkConnections { + INetworkConnections *network; + + explicit TLSNetworkConnections( Reference options ); + + virtual Future> connect( NetworkAddress toAddr ); + + virtual Reference listen( NetworkAddress localAddr ); + +private: + Reference options; +}; + +#define TLS_PLUGIN_FLAG "--tls_plugin" +#define TLS_CERTIFICATE_FILE_FLAG "--tls_certificate_file" +#define TLS_KEY_FILE_FLAG "--tls_key_file" +#define TLS_VERIFY_PEERS_FLAG "--tls_verify_peers" + +#define TLS_OPTION_FLAGS \ + { TLSOptions::OPT_TLS_PLUGIN, TLS_PLUGIN_FLAG, SO_REQ_SEP }, \ + { TLSOptions::OPT_TLS_CERTIFICATES, TLS_CERTIFICATE_FILE_FLAG, SO_REQ_SEP }, \ + { TLSOptions::OPT_TLS_KEY, TLS_KEY_FILE_FLAG, SO_REQ_SEP }, \ + { TLSOptions::OPT_TLS_VERIFY_PEERS, TLS_VERIFY_PEERS_FLAG, SO_REQ_SEP }, + +#define TLS_HELP \ + " " TLS_PLUGIN_FLAG " PLUGIN\n" \ + " The name/path of a FoundationDB TLS plugin to be loaded.\n" \ + " PLUGIN will be opened using dlopen (or LoadLibrary on\n" \ + " Windows) and will be located using the search order\n" \ + " of dlopen or LoadLibrary on your platform.\n" \ + " " TLS_CERTIFICATE_FILE_FLAG " CERTFILE\n" \ + " The path of a file containing the TLS certificate and CA\n" \ + " chain.\n" \ + " " TLS_KEY_FILE_FLAG " KEYFILE\n" \ + " The path of a file containing the private key corresponding\n" \ + " to the TLS certificate.\n" \ + " " TLS_VERIFY_PEERS_FLAG " CONSTRAINTS\n" \ + " The constraints by which to validate TLS peers. The contents\n" \ + " and format of CONSTRAINTS are plugin-specific.\n" + +#endif /* FLOW_TLSCONNECTION_H */ diff --git a/fdbrpc/TraceFileIO.cpp b/fdbrpc/TraceFileIO.cpp new file mode 100644 index 0000000000..54d68ea50a --- /dev/null +++ b/fdbrpc/TraceFileIO.cpp @@ -0,0 +1,165 @@ +/* + * TraceFileIO.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "TraceFileIO.h" + +#if CENABLED(0, NOT_IN_CLEAN) +//TO RUN: Set the name of file you wish to track +std::string debugFileName = "storage-3807bf710afaa6d39d23db60d6e687c8.fdb-wal"; +std::map debugFileData; +std::map debugFileMask; +std::map debugFileRegions; +uint8_t *debugFileSetPage; + +//Setup memory data structures for file debugging. +//TO RUN: setup debugFileRegions map with the regions of the file you wish to track. Keys of the map are offsets in the file, +//and values are the lengths of the region +void debugFileSetup() { + debugFileRegions[0] = 1e8; + for(auto itr = debugFileRegions.begin(); itr != debugFileRegions.end(); ++itr) { + debugFileData[itr->first] = (uint8_t*)malloc(itr->second); + memset(debugFileData[itr->first], 0, itr->second); + + debugFileMask[itr->first] = (uint8_t*)malloc(itr->second); + memset(debugFileMask[itr->first], 0, itr->second); + } + + debugFileSetPage = (uint8_t*)malloc(4096); + memset(debugFileSetPage, 1, 4096); +} + +//Trim a file path to the file name +std::string debugFileTrim(std::string filename) { + int index = filename.find_last_of("/"); + if(index == filename.npos) + return filename; + else + return filename.substr(index + 1); +} + +//Checks if a block of memory has been written +bool debugFileIsSet(uint8_t *storeMask, int64_t offset, int64_t length) { + for(int64_t i = 0; i < length; i += 4096) + if(memcmp(&storeMask[offset + i], debugFileSetPage, std::min((int64_t)4096, length - i))) + return false; + + return true; +} + +//Checks that a given block of data is the same as what has been written by a call to debugFileSet +void debugFileCheck(std::string context, std::string file, const void *data, int64_t offset, int length) { + if(debugFileRegions.empty()) + debugFileSetup(); + if(debugFileTrim(file) == debugFileName) { + bool found = false; + for(auto itr = debugFileRegions.begin(); itr != debugFileRegions.end(); ++itr) { + if(offset + length > itr->first && offset < itr->first + itr->second) { + found = true; + uint8_t *storeData = debugFileData[itr->first]; + uint8_t *storeMask = debugFileMask[itr->first]; + + ASSERT(storeData && storeMask); + + int64_t dataOffset = std::max((int64_t)0, itr->first - offset); + int64_t dbgOffset = std::max((int64_t)0, offset - itr->first); + int64_t cmpLength = std::min(length - dataOffset, itr->second - dbgOffset); + ASSERT(cmpLength > 0); + TraceEvent("DebugFileCheck").detail("Context", context).detail("Filename", file).detail("Offset", offset).detail("Length", length); + bool success = true; + if(debugFileIsSet(storeMask, dbgOffset, cmpLength)) { + //TraceEvent("DebugFileCheckMemCmp").detail("Context", context).detail("Filename", file).detail("Offset", offset).detail("Length", length).detail("DataOffset", dataOffset).detail("DbgOffset", dbgOffset).detail("OverlapLength", cmpLength); + if(memcmp(&((uint8_t*)data)[dataOffset], &storeData[dbgOffset], cmpLength)) + success = false; + } + else { + TraceEvent("DebugFileUnsetCheck").detail("Context", context).detail("Filename", file).detail("Offset", offset).detail("Length", length); + for(int64_t i = 0; i < cmpLength; i++) { + if(storeMask[dbgOffset + i] && storeData[dbgOffset + i] != ((uint8_t*)data)[dataOffset + i]) { + success = false; + break; + } + } + } + + if(!success) + TraceEvent(SevWarnAlways, "DebugFileFail").detail("Context", context).detail("Filename", file).detail("Offset", offset).detail("Length", length); + } + } + if(!found) + TraceEvent("DebugFileSkippingCheck").detail("Context", context).detail("Filename", file).detail("Offset", offset).detail("Length", length); + } +} + +//Updates the in-memory copy of tracked data at a given offset +void debugFileSet(std::string context, std::string file, const void *data, int64_t offset, int length) { + if(debugFileRegions.empty()) + debugFileSetup(); + if(debugFileTrim(file) == debugFileName) { + bool found = false; + for(auto itr = debugFileRegions.begin(); itr != debugFileRegions.end(); ++itr) { + if(offset + length > itr->first && offset < itr->first + itr->second) { + found = true; + + uint8_t *storeData = debugFileData[itr->first]; + uint8_t *storeMask = debugFileMask[itr->first]; + ASSERT(storeData && storeMask); + + int64_t dataOffset = std::max((int64_t)0, itr->first - offset); + int64_t dbgOffset = std::max((int64_t)0, offset - itr->first); + int64_t cmpLength = std::min(length - dataOffset, itr->second - dbgOffset); + ASSERT(cmpLength > 0); + TraceEvent("DebugFileSet").detail("Context", context).detail("Filename", file).detail("Offset", offset).detail("Length", length); + //TraceEvent("DebugFileSetMemCpy").detail("Context", context).detail("Filename", file).detail("Offset", offset).detail("Length", length).detail("DataOffset", dataOffset).detail("DbgOffset", dbgOffset).detail("OverlapLength", std::min(length - dataOffset, itr->second - dbgOffset)); + memcpy(&storeData[dbgOffset], &((uint8_t*)data)[dataOffset], cmpLength); + memset(&storeMask[dbgOffset], 1, cmpLength); + } + } + if(!found) + TraceEvent("DebugFileSkippingSet").detail("Context", context).detail("Filename", file).detail("Offset", offset).detail("Length", length); + } +} + +//Updates the in-memory copy of tracked data to account for truncates (this simply invalidates any data after truncate point) +void debugFileTruncate(std::string context, std::string file, int64_t offset) { + if(debugFileRegions.empty()) + debugFileSetup(); + if(debugFileTrim(file) == debugFileName) { + bool found = false; + for(auto itr = debugFileRegions.begin(); itr != debugFileRegions.end(); ++itr) { + if(itr->first + itr->second > offset) { + found = true; + TraceEvent("DebugFileTruncate").detail("Context", context).detail("Filename", file).detail("Offset", offset); + uint8_t *storeMask = debugFileMask[itr->first]; + ASSERT(storeMask); + + int64_t dbgOffset = std::max((int64_t)0, offset - itr->first); + memset(&storeMask[dbgOffset], 0, itr->second - dbgOffset); + } + } + if(!found) + TraceEvent("DebugFileSkippingTruncate").detail("Context", context).detail("Filename", file).detail("Offset", offset); + } +} + +#else +void debugFileCheck(std::string context, std::string file, const void *data, int64_t offset, int length) { } +void debugFileSet(std::string context, std::string file, const void *data, int64_t offset, int length) { } +void debugFileTruncate(std::string context, std::string file, int64_t offset) { } +#endif diff --git a/fdbrpc/TraceFileIO.h b/fdbrpc/TraceFileIO.h new file mode 100644 index 0000000000..daa5c7329b --- /dev/null +++ b/fdbrpc/TraceFileIO.h @@ -0,0 +1,32 @@ +/* + * TraceFileIO.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include "flow/flow.h" + +//Checks that a given block of data is the same as what has been written by a call to debugFileSet +extern void debugFileCheck(std::string context, std::string file, const void *data, int64_t offset, int length); + +//Updates the in-memory copy of tracked data at a given offset +extern void debugFileSet(std::string context, std::string file, const void *data, int64_t offset, int length); + +//Updates the in-memory copy of tracked data to account for truncates (this simply invalidates any data after truncate point) +extern void debugFileTruncate(std::string context, std::string file, int64_t offset); diff --git a/fdbrpc/actorFuzz.py b/fdbrpc/actorFuzz.py new file mode 100644 index 0000000000..4c23ac2314 --- /dev/null +++ b/fdbrpc/actorFuzz.py @@ -0,0 +1,366 @@ +# +# actorFuzz.py +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import random +import copy + +class Context: + tok = 0 + inLoop = False + indent = 0 + def __init__(self): + self.random = random.Random() + def uniqueID(self): + return self.random.randint(100000, 999999) + +class InfiniteLoop (Exception): + pass + +class ExecContext: + iterationsLeft = 1000 + ifstate = 0 + def __init__(self, inputSeq): + self.input = iter(inputSeq) + self.output = [] + def inp(self): return next(self.input) + def out(self, x): self.output.append(x) + def infinityCheck(self): + self.iterationsLeft -= 1 + if self.iterationsLeft <= 0: + raise InfiniteLoop() + +OK = 1 +BREAK = 2 +THROW = 3 +RETURN = 4 +CONTINUE = 5 + +def indent(cx): + return "\t" * cx.indent + +class F (object): + def unreachable(self): + return False + def containsbreak(self): + return False + +class hashF (F): + def __init__(self, cx): + self.cx = cx + self.uniqueID = cx.uniqueID() + def __str__(self): + return indent(self.cx) + "outputStream.send( %d );\n" % self.uniqueID + def eval(self, ecx): + ecx.infinityCheck() + ecx.out( self.uniqueID ) + return OK + +class compoundF(F): + def __init__(self, cx, children): + self.cx = cx + self.children = [] + for c in children: + self.children.append(c) + if c.unreachable(): + self.unreachable = lambda: 1 + break + def __str__(self): + return "".join( str(c) for c in self.children ) + def eval(self, ecx): + for c in self.children: + ecx.infinityCheck() + result = c.eval( ecx ) + if result != OK: + break + return result + def containsbreak(self): + return any( c.containsbreak() for c in self.children ) + +class loopF (F): + def __init__(self, cx): + self.cx = cx + ccx = copy.copy(cx) + ccx.indent += 1 + ccx.inLoop = True + self.body = compoundF( ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)] ) + self.uniqueID = cx.uniqueID() + self.forever = cx.random.random() < 0.1 + + def __str__(self): + if self.forever: + return (indent(self.cx) + "loop {\n" + + str(self.body) + + indent(self.cx) + "}\n") + else: + return (indent(self.cx) + "state int i%d; for(i%d = 0; i%d < 5; i%d++) {\n" % ((self.uniqueID,)*4) + + str(self.body) + + indent(self.cx) + "}\n") + def eval(self, ecx): + if self.forever: + while True: + ecx.infinityCheck() + result = self.body.eval( ecx ) + if result == BREAK: + break + elif result not in (OK,CONTINUE): + return result + else: + for i in range(5): + ecx.infinityCheck() + result = self.body.eval( ecx ) + if result == BREAK: + break + elif result not in (OK,CONTINUE): + return result + return OK + def unreachable(self): + return self.forever and not self.body.containsbreak() + +class rangeForF (F): + def __init__(self, cx): + self.cx = cx + ccx = copy.copy(cx) + ccx.indent += 1 + ccx.inLoop = True + self.body = compoundF( ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)] ) + self.uniqueID = cx.uniqueID() + + def __str__(self): + return ( + indent(self.cx) + + ("\n"+indent(self.cx)).join([ + "state std::vector V;", + "V.push_back(1);", + "V.push_back(2);", + "V.push_back(3);", + "for( auto i : V ) {\n", + ]).replace("V", "list%d" % self.uniqueID) + + str(self.body) + + indent(self.cx) + "}\n") + + def eval(self, ecx): + for i in range(1,4): + ecx.infinityCheck() + result = self.body.eval( ecx ) + if result == BREAK: + break + elif result not in (OK,CONTINUE): + return result + return OK + + def unreachable(self): + return False + +class ifF (F): + def __init__(self, cx): + self.cx = cx + ccx = copy.copy(cx) + ccx.indent += 1 + self.toggle = cx.random.randint(0,1) + self.ifbody = compoundF( ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)] ) + if cx.random.random() < 0.5: + ccx = copy.copy(cx) + ccx.indent += 1 + self.elsebody = compoundF( ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)] ) + else: + self.elsebody = None + def __str__(self): + s = (indent(self.cx) + "if ( (++ifstate&1) == %d ) {\n" % self.toggle + + str(self.ifbody)) + if self.elsebody: + s += (indent(self.cx) + "} else {\n" + + str(self.elsebody)) + s += indent(self.cx) + "}\n" + return s + def eval(self, ecx): + ecx.infinityCheck() + ecx.ifstate = ecx.ifstate + 1 + if (ecx.ifstate&1) == self.toggle: + return self.ifbody.eval(ecx) + elif self.elsebody: + return self.elsebody.eval(ecx) + else: + return OK + def unreachable(self): + return self.elsebody and self.ifbody.unreachable() and self.elsebody.unreachable() + def containsbreak(self): + return self.ifbody.containsbreak() or (self.elsebody and self.elsebody.containsbreak()) + +class tryF (F): + def __init__(self, cx): + self.cx = cx + ccx = copy.copy(cx) + ccx.indent += 1 + self.body = compoundF( ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)] ) + ccx = copy.copy(cx) + ccx.indent += 1 + self.catch = compoundF( ccx, [hashF(ccx)] + [fuzzCode(ccx)(ccx)] + [hashF(ccx)] ) + def __str__(self): + return (indent(self.cx) + "try {\n" + + str( self.body ) + + indent(self.cx) + "} catch (...) {\n" + + str( self.catch ) + + indent(self.cx) + "}\n" + ) + def eval(self, ecx): + ecx.infinityCheck() + result = self.body.eval(ecx) + if result != THROW: return result + return self.catch.eval(ecx) + def unreachable(self): + return self.body.unreachable() and self.catch.unreachable() + def containsbreak(self): + return self.body.containsbreak() or self.catch.containsbreak() + +def doubleF( cx ): + return compoundF( cx, [fuzzCode(cx)(cx)] + [hashF(cx)] + [fuzzCode(cx)(cx)] ) + +class breakF(F): + def __init__(self, cx): + self.cx = cx + def __str__(self): + return indent(self.cx) + "break;\n" + def unreachable(self): + return True + def eval(self, ecx): + ecx.infinityCheck() + return BREAK + def containsbreak(self): + return True + +class continueF(F): + def __init__(self, cx): + self.cx = cx + def __str__(self): + return indent(self.cx) + "continue;\n" + def unreachable(self): + return True + def eval(self, ecx): + ecx.infinityCheck() + return CONTINUE + +class waitF( F ): + def __init__(self, cx): + self.cx = cx + self.uniqueID = cx.uniqueID() + def __str__(self): + return ( + indent(self.cx) + "int input = waitNext( inputStream );\n" + + indent(self.cx) + "outputStream.send( input + %d );\n" % self.uniqueID + ) + def eval(self, ecx): + ecx.infinityCheck() + input = ecx.inp() + ecx.out( (input + self.uniqueID)&0xffffffff ) + return OK + +class throwF( F ): + def __init__(self, cx): + self.cx = cx + def __str__(self): + return indent(self.cx) + "throw operation_failed();\n" + def unreachable(self): + return True + def eval(self, ecx): + ecx.infinityCheck() + return THROW + +class throwF2( throwF ): + def __str__(self): + return indent(self.cx) + "throw_operation_failed();\n" + def unreachable(self): + return False # The actor compiler doesn't know the function never returns + +class throwF3( throwF ): + def __str__(self): + return indent(self.cx) + "Void _ = wait( error ); // throw operation_failed()\n" + def unreachable(self): + return False # The actor compiler doesn't know that 'error' always contains an error + +class returnF( F ): + def __init__(self, cx): + self.cx = cx + self.uniqueID = cx.uniqueID() + def __str__(self): + return indent(self.cx) + "return %d;\n" % self.uniqueID + def unreachable(self): + return True + def eval(self, ecx): + ecx.infinityCheck() + ecx.returnValue = self.uniqueID + return RETURN + +def fuzzCode(cx): + choices = [loopF, rangeForF, tryF, doubleF, ifF] + if (cx.indent < 2): + choices = choices*2 + choices += [waitF, returnF] + if (cx.inLoop): + choices += [breakF, continueF] + choices = choices*3 + [throwF,throwF2, throwF3] + return cx.random.choice(choices) + +def randomActor(index): + while 1: + cx = Context() + cx.indent += 1 + actor = fuzzCode(cx)(cx) + actor = compoundF( cx, [actor, returnF(cx)] ) # Add a return at the end if the end is reachable + name = "actorFuzz%d" % index + text = ( "ACTOR Future %s( FutureStream inputStream, PromiseStream outputStream, Future error ) {\n" % name + + "\tstate int ifstate = 0;\n" + + str(actor) + + "}" ) + ecx = actor.ecx = ExecContext( (i+1)*1000 for i in range(1000000) ) + try: + result = actor.eval(ecx) + except InfiniteLoop: + print( "Infinite loop for actor %s" % name ) + continue + if result == RETURN: + ecx.out( ecx.returnValue ) + elif result == THROW: + ecx.out( 1000 ) + else: + print(text) + raise Exception( "Invalid eval result: " + str(result) ) + actor.name = name + actor.text = text + + return actor + +testCaseCount = 30 +outputFile = open( "ActorFuzz.actor.cpp", "wt" ) +print('// THIS FILE WAS GENERATED BY actorFuzz.py; DO NOT MODIFY IT DIRECTLY\n', file=outputFile) +print('#include "ActorFuzz.h"\n', file=outputFile) +print('#ifndef WIN32\n', file=outputFile) + +actors = [randomActor(i) for i in range(testCaseCount)] + +for actor in actors: + print(actor.text + "\n", file=outputFile) + +print("std::pair actorFuzzTests() {\n\tint testsOK = 0;", file=outputFile) +for actor in actors: + print('\ttestsOK += testFuzzActor( &%s, "%s", (vector(),%s) );' % (actor.name, actor.name, ','.join(str(e) for e in actor.ecx.output)), + file=outputFile) +print("\treturn std::make_pair(testsOK, %d);\n}" % len(actors), file=outputFile) +print('#endif // WIN32\n', file=outputFile) +outputFile.close() diff --git a/fdbrpc/batcher.actor.h b/fdbrpc/batcher.actor.h new file mode 100644 index 0000000000..3434fd7e38 --- /dev/null +++ b/fdbrpc/batcher.actor.h @@ -0,0 +1,98 @@ +/* + * batcher.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FLOW_BATCHER_ACTOR_G_H) + #define FLOW_BATCHER_ACTOR_G_H + #include "batcher.actor.g.h" +#elif !defined(FLOW_BATCHER_ACTOR_H) + #define FLOW_BATCHER_ACTOR_H + +#include "flow/actorcompiler.h" +#include "flow/flow.h" +#include "flow/Stats.h" + +template +void logOnReceive(X x) { } + +void logOnReceive(CommitTransactionRequest x) { + if(x.debugID.present()) + g_traceBatch.addEvent("CommitDebug", x.debugID.get().first(), "MasterProxyServer.batcher"); +} + +ACTOR template +Future batcher(PromiseStream> out, FutureStream in, double avgMinDelay, double* avgMaxDelay, double emptyBatchTimeout, int maxCount, int desiredBytes, int maxBytes, Optional> batchStartedStream, int taskID = TaskDefaultDelay, Counter* counter = 0) +{ + Void _ = wait( delayJittered(*avgMaxDelay, taskID) ); // smooth out + // This is set up to deliver even zero-size batches if emptyBatchTimeout elapses, because that's what master proxy wants. The source control history + // contains a version that does not. + + state double lastBatch = 0; + + loop { + state Future timeout; + state std::vector batch; + state int batchBytes = 0; + + if(emptyBatchTimeout <= 0) + timeout = Never(); + else + timeout = delayJittered(emptyBatchTimeout, taskID); + + while (!timeout.isReady() && !(batch.size() == maxCount || batchBytes >= desiredBytes)) { + choose { + when ( X x = waitNext(in) ) { + if (counter) ++*counter; + logOnReceive(x); + if (!batch.size()) { + if(batchStartedStream.present()) + batchStartedStream.get().send(Void()); + if (now() - lastBatch > *avgMaxDelay) + timeout = delayJittered(avgMinDelay, taskID); + else + timeout = delayJittered(*avgMaxDelay - (now() - lastBatch), taskID); + } + + int bytes = getBytes( x ); + if(batchBytes + bytes > maxBytes && batch.size()) { + out.send(batch); + lastBatch = now(); + if(batchStartedStream.present()) + batchStartedStream.get().send(Void()); + timeout = delayJittered(*avgMaxDelay, taskID); + batch = std::vector(); + batchBytes = 0; + } + + batch.push_back(x); + batchBytes += bytes; + } + when ( Void _ = wait( timeout ) ) {} + } + } + + out.send(batch); + lastBatch = now(); + } +} + +#endif \ No newline at end of file diff --git a/fdbrpc/crc32c.cpp b/fdbrpc/crc32c.cpp new file mode 100644 index 0000000000..fcfc5e20a8 --- /dev/null +++ b/fdbrpc/crc32c.cpp @@ -0,0 +1,323 @@ +/* + Copyright (c) 2013 - 2014 Mark Adler, Robert Vazan + + This software is provided 'as-is', without any express or implied + warranty. In no event will the author be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + + THIS CODE HAS BEEN ALTERED FROM THE ORIGINAL +*/ + +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include "crc32c.h" + +#define NOMINMAX + +#include +#include +#include +#include +#include +#include "Platform.h" +#include "generated-constants.cpp" +#pragma GCC target("sse4.2") + +static uint32_t append_trivial(uint32_t crc, const uint8_t * input, size_t length) +{ + for (size_t i = 0; i < length; ++i) + { + crc = crc ^ input[i]; + for (int j = 0; j < 8; j++) + crc = (crc >> 1) ^ 0x80000000 ^ ((~crc & 1) * POLY); + } + return crc; +} + +/* Table-driven software version as a fall-back. This is about 15 times slower + than using the hardware instructions. This assumes little-endian integers, + as is the case on Intel processors that the assembler code here is for. */ +static uint32_t append_adler_table(uint32_t crci, const uint8_t * input, size_t length) +{ + const uint8_t * next = input; + uint64_t crc; + + crc = crci ^ 0xffffffff; + while (length && ((uintptr_t)next & 7) != 0) + { + crc = table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); + --length; + } + while (length >= 8) + { + crc ^= *(uint64_t *)next; + crc = table[7][crc & 0xff] + ^ table[6][(crc >> 8) & 0xff] + ^ table[5][(crc >> 16) & 0xff] + ^ table[4][(crc >> 24) & 0xff] + ^ table[3][(crc >> 32) & 0xff] + ^ table[2][(crc >> 40) & 0xff] + ^ table[1][(crc >> 48) & 0xff] + ^ table[0][crc >> 56]; + next += 8; + length -= 8; + } + while (length) + { + crc = table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); + --length; + } + return (uint32_t)crc ^ 0xffffffff; +} + +/* Table-driven software version as a fall-back. This is about 15 times slower + than using the hardware instructions. This assumes little-endian integers, + as is the case on Intel processors that the assembler code here is for. */ +static uint32_t append_table(uint32_t crci, const uint8_t * input, size_t length) +{ + const uint8_t * next = input; +#ifdef _M_X64 + uint64_t crc; +#else + uint32_t crc; +#endif + + crc = crci ^ 0xffffffff; +#ifdef _M_X64 + while (length && ((uintptr_t)next & 7) != 0) + { + crc = table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); + --length; + } + while (length >= 16) + { + crc ^= *(uint64_t *)next; + uint64_t high = *(uint64_t *)(next + 8); + crc = table[15][crc & 0xff] + ^ table[14][(crc >> 8) & 0xff] + ^ table[13][(crc >> 16) & 0xff] + ^ table[12][(crc >> 24) & 0xff] + ^ table[11][(crc >> 32) & 0xff] + ^ table[10][(crc >> 40) & 0xff] + ^ table[9][(crc >> 48) & 0xff] + ^ table[8][crc >> 56] + ^ table[7][high & 0xff] + ^ table[6][(high >> 8) & 0xff] + ^ table[5][(high >> 16) & 0xff] + ^ table[4][(high >> 24) & 0xff] + ^ table[3][(high >> 32) & 0xff] + ^ table[2][(high >> 40) & 0xff] + ^ table[1][(high >> 48) & 0xff] + ^ table[0][high >> 56]; + next += 16; + length -= 16; + } +#else + while (length && ((uintptr_t)next & 3) != 0) + { + crc = table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); + --length; + } + while (length >= 12) + { + crc ^= *(uint32_t *)next; + uint32_t high = *(uint32_t *)(next + 4); + uint32_t high2 = *(uint32_t *)(next + 8); + crc = table[11][crc & 0xff] + ^ table[10][(crc >> 8) & 0xff] + ^ table[9][(crc >> 16) & 0xff] + ^ table[8][crc >> 24] + ^ table[7][high & 0xff] + ^ table[6][(high >> 8) & 0xff] + ^ table[5][(high >> 16) & 0xff] + ^ table[4][high >> 24] + ^ table[3][high2 & 0xff] + ^ table[2][(high2 >> 8) & 0xff] + ^ table[1][(high2 >> 16) & 0xff] + ^ table[0][high2 >> 24]; + next += 12; + length -= 12; + } +#endif + while (length) + { + crc = table[0][(crc ^ *next++) & 0xff] ^ (crc >> 8); + --length; + } + return (uint32_t)crc ^ 0xffffffff; +} + +/* Apply the zeros operator table to crc. */ +static inline uint32_t shift_crc(uint32_t shift_table[][256], uint32_t crc) +{ + return shift_table[0][crc & 0xff] + ^ shift_table[1][(crc >> 8) & 0xff] + ^ shift_table[2][(crc >> 16) & 0xff] + ^ shift_table[3][crc >> 24]; +} + +/* Compute CRC-32C using the Intel hardware instruction. */ +static uint32_t append_hw(uint32_t crc, const uint8_t * buf, size_t len) +{ + const uint8_t * next = buf; + const uint8_t * end; +#ifdef _M_X64 + uint64_t crc0, crc1, crc2; /* need to be 64 bits for crc32q */ +#else + uint32_t crc0, crc1, crc2; +#endif + + /* pre-process the crc */ + crc0 = crc ^ 0xffffffff; + + /* compute the crc for up to seven leading bytes to bring the data pointer + to an eight-byte boundary */ + while (len && ((uintptr_t)next & 7) != 0) + { + crc0 = _mm_crc32_u8(static_cast(crc0), *next); + ++next; + --len; + } + +#ifdef _M_X64 + /* compute the crc on sets of LONG_SHIFT*3 bytes, executing three independent crc + instructions, each on LONG_SHIFT bytes -- this is optimized for the Nehalem, + Westmere, Sandy Bridge, and Ivy Bridge architectures, which have a + throughput of one crc per cycle, but a latency of three cycles */ + while (len >= 3 * LONG_SHIFT) + { + crc1 = 0; + crc2 = 0; + end = next + LONG_SHIFT; + do + { + crc0 = _mm_crc32_u64(crc0, *reinterpret_cast(next)); + crc1 = _mm_crc32_u64(crc1, *reinterpret_cast(next + LONG_SHIFT)); + crc2 = _mm_crc32_u64(crc2, *reinterpret_cast(next + 2 * LONG_SHIFT)); + next += 8; + } while (next < end); + crc0 = shift_crc(long_shifts, static_cast(crc0)) ^ crc1; + crc0 = shift_crc(long_shifts, static_cast(crc0)) ^ crc2; + next += 2 * LONG_SHIFT; + len -= 3 * LONG_SHIFT; + } + + /* do the same thing, but now on SHORT_SHIFT*3 blocks for the remaining data less + than a LONG_SHIFT*3 block */ + while (len >= 3 * SHORT_SHIFT) + { + crc1 = 0; + crc2 = 0; + end = next + SHORT_SHIFT; + do + { + crc0 = _mm_crc32_u64(crc0, *reinterpret_cast(next)); + crc1 = _mm_crc32_u64(crc1, *reinterpret_cast(next + SHORT_SHIFT)); + crc2 = _mm_crc32_u64(crc2, *reinterpret_cast(next + 2 * SHORT_SHIFT)); + next += 8; + } while (next < end); + crc0 = shift_crc(short_shifts, static_cast(crc0)) ^ crc1; + crc0 = shift_crc(short_shifts, static_cast(crc0)) ^ crc2; + next += 2 * SHORT_SHIFT; + len -= 3 * SHORT_SHIFT; + } + + /* compute the crc on the remaining eight-byte units less than a SHORT_SHIFT*3 + block */ + end = next + (len - (len & 7)); + while (next < end) + { + crc0 = _mm_crc32_u64(crc0, *reinterpret_cast(next)); + next += 8; + } +#else + /* compute the crc on sets of LONG_SHIFT*3 bytes, executing three independent crc + instructions, each on LONG_SHIFT bytes -- this is optimized for the Nehalem, + Westmere, Sandy Bridge, and Ivy Bridge architectures, which have a + throughput of one crc per cycle, but a latency of three cycles */ + while (len >= 3 * LONG_SHIFT) + { + crc1 = 0; + crc2 = 0; + end = next + LONG_SHIFT; + do + { + crc0 = _mm_crc32_u32(crc0, *reinterpret_cast(next)); + crc1 = _mm_crc32_u32(crc1, *reinterpret_cast(next + LONG_SHIFT)); + crc2 = _mm_crc32_u32(crc2, *reinterpret_cast(next + 2 * LONG_SHIFT)); + next += 4; + } while (next < end); + crc0 = shift_crc(long_shifts, static_cast(crc0)) ^ crc1; + crc0 = shift_crc(long_shifts, static_cast(crc0)) ^ crc2; + next += 2 * LONG_SHIFT; + len -= 3 * LONG_SHIFT; + } + + /* do the same thing, but now on SHORT_SHIFT*3 blocks for the remaining data less + than a LONG_SHIFT*3 block */ + while (len >= 3 * SHORT_SHIFT) + { + crc1 = 0; + crc2 = 0; + end = next + SHORT_SHIFT; + do + { + crc0 = _mm_crc32_u32(crc0, *reinterpret_cast(next)); + crc1 = _mm_crc32_u32(crc1, *reinterpret_cast(next + SHORT_SHIFT)); + crc2 = _mm_crc32_u32(crc2, *reinterpret_cast(next + 2 * SHORT_SHIFT)); + next += 4; + } while (next < end); + crc0 = shift_crc(short_shifts, static_cast(crc0)) ^ crc1; + crc0 = shift_crc(short_shifts, static_cast(crc0)) ^ crc2; + next += 2 * SHORT_SHIFT; + len -= 3 * SHORT_SHIFT; + } + + /* compute the crc on the remaining eight-byte units less than a SHORT_SHIFT*3 + block */ + end = next + (len - (len & 7)); + while (next < end) + { + crc0 = _mm_crc32_u32(crc0, *reinterpret_cast(next)); + next += 4; + } +#endif + len &= 7; + + /* compute the crc for up to seven trailing bytes */ + while (len) + { + crc0 = _mm_crc32_u8(static_cast(crc0), *next); + ++next; + --len; + } + + /* return a post-processed crc */ + return static_cast(crc0) ^ 0xffffffff; +} + + +static bool hw_available = platform::isSse42Supported(); + +extern "C" uint32_t crc32c_append(uint32_t crc, const uint8_t * input, size_t length) +{ + if (hw_available) + return append_hw(crc, input, length); + else + return append_table(crc, input, length); +} diff --git a/fdbrpc/crc32c.h b/fdbrpc/crc32c.h new file mode 100644 index 0000000000..665af4b746 --- /dev/null +++ b/fdbrpc/crc32c.h @@ -0,0 +1,41 @@ +/* + Copyright (c) 2013 - 2014 Mark Adler, Robert Vazan + + This software is provided 'as-is', without any express or implied + warranty. In no event will the author be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + + THIS CODE HAS BEEN ALTERED FROM THE ORIGINAL +*/ + +#ifndef CRC32C_H +#define CRC32C_H + +#include +#include + +/* + Computes CRC-32C using Castagnoli polynomial of 0x82f63b78. + This polynomial is better at detecting errors than the more common CRC-32 polynomial. + CRC-32C is implemented in hardware on newer Intel processors. + This function will use the hardware if available and fall back to fast software implementation. +*/ +extern "C" uint32_t crc32c_append( + uint32_t crc, // initial CRC, typically 0, may be used to accumulate CRC from multiple buffers + const uint8_t *input, // data to be put through the CRC algorithm + size_t length); // length of the data in the input buffer + +#endif diff --git a/fdbrpc/dsltest.actor.cpp b/fdbrpc/dsltest.actor.cpp new file mode 100644 index 0000000000..bdfa2d0ee1 --- /dev/null +++ b/fdbrpc/dsltest.actor.cpp @@ -0,0 +1,1460 @@ +/* + * dsltest.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/FastRef.h" +#undef ERROR +#include "flow/actorcompiler.h" +#include "simulator.h" +#include "ActorFuzz.h" +#include "flow/DeterministicRandom.h" +#include "flow/ThreadHelper.actor.h" +#include +#include + +using std::cout; +using std::endl; + +using std::vector; + +void* allocateLargePages( int total ); + +bool testFuzzActor( Future(*actor)(FutureStream const&, PromiseStream const&, Future const&), const char* desc, vector const& expectedOutput ) { + // Run the test 5 times with different "timing" + int i, outCount; + bool ok = true; + for(int trial=0; trial<5; trial++) { + PromiseStream in, out; + Promise err; + int before = g_random->randomInt(0, 4); + int errorBefore = before + g_random->randomInt(0, 4); + //printf("\t\tTrial #%d: %d, %d\n", trial, before, errorBefore); + if (errorBefore <= before) err.sendError( operation_failed() ); + for(i=0; i ret = (*actor)(in.getFuture(), out, err.getFuture()); + while (i < 1000000 && !ret.isReady()) { + i++; + if (errorBefore == i) err.sendError( operation_failed() ); + in.send( i * 1000 ); + } + if (ret.isReady()) { + if (ret.isError()) + out.send( ret.getError().code() ); + else + out.send( ret.get() ); + } else { + printf("\tERROR: %s did not return after consuming %d input values\n", desc, i); + if (trial) printf("\t\tResult was inconsistent between runs! (Trial %d)\n", trial); + ok = false; + //return false; + } + + outCount = -1; + while (out.getFuture().isReady()) { + int o = out.getFuture().pop(); + outCount++; + if (outCount < expectedOutput.size() && expectedOutput[outCount] != o) { + printf("\tERROR: %s output #%d incorrect: %d != expected %d\n", desc, outCount, o, expectedOutput[outCount]); + if (trial) printf("\t\tResult was inconsistent between runs!\n"); + ok = false; + //return false; + } + } + if (outCount+1 != expectedOutput.size()) { + printf("\tERROR: %s output length incorrect: %d != expected %d\n", desc, outCount+1, expectedOutput.size()); + if (trial) printf("\t\tResult was inconsistent between runs!\n"); + ok = false; + //return false; + } + + // We might have put in values that weren't actually consumed... + while (in.getFuture().isReady()) { + in.getFuture().pop(); + i--; + } + } + //printf("\t%s: OK, %d input values -> %d output values\n", desc, i, outCount); + return ok; +} + +#if 0 +void memoryTest2() { + const int Size = 2000 << 20; + const int Reads = 4 << 20; + const int MaxThreads = 4; + + char* block = new char[Size]; + memset(block, 0, Size); + + char** random = new char*[ Reads * MaxThreads ]; + random[0] = block; + for(int i=1; irandomInt(0, Size) ]; + random[i++] = s; + /*for(int j=0; j<10 && irandomInt(0, 4096); + if (random[i] >= block+Size) random[i] -= Size; + }*/ + } + + for(int threads=1; threads<=MaxThreads; threads++) { + double tstart = timer(); + + vector> done; + for(int t=0; t( [r,Reads] () -> Void { + for(int i=0; i= 1; n--) { + int k = g_random->randomInt(0, n); //random.IRandomX(0, n-1); + std::swap( x[k], x[n] ); + } + } else { + cout <<" Sequential permutation" << endl; + // Sequential + for(int i=0; irandomInt(0,N) ]; + } + for(int T=1; T<=MT; T+=T) { + double start = timer(); + vector< Future > done; + for(int t=0; t( [start,N2,TraversalsPerThread] () -> double { + void **p[MaxTraversalsPerThread]; + for(int j=0; j 1) + _mm_prefetch( (const char*)p[j], _MM_HINT_T0 ); + } + for(int j=0; j +Future addN(Future in) { + X i = wait( in ); + return i + N; +} + +ACTOR template +Future switchTest( FutureStream
    as, Future oneb ) { + loop choose { + when (A a = waitNext( as )) { cout << "A " << a << endl; } + when (B b = wait( oneb )) { cout << "B " << b << endl; break; } + } + loop { + cout << "Done!" << endl; + return Void(); + } +} + +class TestBuffer : public ReferenceCounted { +public: + static TestBuffer* create( int length ) { + auto b = (TestBuffer*)new int[ (length+7)/4 ]; + new (b) TestBuffer(length); + return b; + } + void operator delete( void* buf ) { + cout << "Freeing buffer" << endl; + delete[] (int*)buf; + } + + int size() const { return length; } + uint8_t* begin() { return data; } + uint8_t* end() { return data+length; } + const uint8_t* begin() const { return data; } + const uint8_t* end() const { return data+length; } + +private: + TestBuffer(int length) throw () : length(length) {} + int length; + uint8_t data[1]; +}; + +int fastKeyCount = 0; + +class FastKey : public FastAllocated, public ReferenceCounted +{ +public: + FastKey() : length(0) {} + FastKey(char* b, int length) : length(length) { + ASSERT (length <= sizeof(data)); + memcpy(data, b, length); + } + ~FastKey() { fastKeyCount++; } + int size() const { return length; } + uint8_t* begin() { return data; } + uint8_t* end() { return data+length; } + const uint8_t* begin() const { return data; } + const uint8_t* end() const { return data+length; } + +private: + int length; + uint8_t data[252]; +}; + +struct TestB : FastAllocated { + char x[65]; +}; + +void fastAllocTest() { + double t; + + std::vector d; + for(int i=0; i<1000000;i++) { + d.push_back( FastAllocator<64>::allocate() ); + int r = g_random->randomInt(0,1000000); + if (r < d.size()) { + FastAllocator<64>::release(d[r]); + d[r] = d.back(); + d.pop_back(); + } + } + std::sort(d.begin(), d.end()); + if (std::unique(d.begin(),d.end())!=d.end()) + cout << "Pointer returned twice!?" << endl; + + for(int i=0; i<2; i++) { + void *p = FastAllocator<64>::allocate(); + void *q = FastAllocator<64>::allocate(); + cout << (intptr_t)p << " " << (intptr_t)q << endl; + FastAllocator<64>::release(p); + FastAllocator<64>::release(q); + } + + t = timer(); + for(int i=0; i<1000000; i++) + FastAllocator<64>::allocate(); + t = timer()-t; + cout << "Allocations: " << (1/t) << "M/sec" << endl; + + t = timer(); + for(int i=0; i<1000000; i++) + FastAllocator<64>::release( FastAllocator<64>::allocate() ); + t = timer()-t; + cout << "Allocate/Release pairs: " << (1/t) << "M/sec" << endl; + + t = timer(); + void* pp[100]; + for(int i=0; i<10000; i++) { + for(int j=0; j<100; j++) + pp[j] = FastAllocator<64>::allocate(); + for(int j=0; j<100; j++) + FastAllocator<64>::release(pp[j]); + } + t = timer()-t; + cout << "Allocate/Release interleaved(100): " << (1/t) << "M/sec" << endl; + + t = timer(); + for(int i=0; i<1000000; i++) + delete new TestB; + t = timer()-t; + cout << "Allocate/Release TestB pairs: " << (1/t) << "M/sec" << endl; + + #if FLOW_THREAD_SAFE + t = timer(); + std::vector< Future > results; + for(int i=0; i<4; i++) + results.push_back( + inThread( [] ()->bool { + TestB* pp[100]; + for(int i=0; i<10000; i++) { + for(int j=0; j<100; j++) + pp[j] = new TestB; + for(int j=0; j<100; j++) + delete pp[j]; + } + return true; + })); + Void _ = waitForAll( results ).getBlocking(); + t = timer()-t; + cout << "Threaded Allocate/Release TestB interleaved (100): " << results.size() << " x " << (1/t) << "M/sec" << endl; + #endif + + volatile int32_t v = 0; + + t = timer(); + for(int i=0; i<10000000; i++) + interlockedIncrement(&v); + t = timer()-t; + cout << "interlocked increment: " << 10.0/t << "M/sec " << v << endl; + + v = 5; + t = timer(); + for(int i=0; i<10000000; i++) { + interlockedCompareExchange(&v, 5, 5); + } + t = timer()-t; + cout << "1 state machine: " << 10.0/t << "M/sec " << v << endl; + + v=0; + t = timer(); + for(int i=0; i<10000000; i++) + v++; + t = timer()-t; + cout << "volatile increment: " << 10.0/t << "M/sec " << v << endl; + + { + Reference b( TestBuffer::create(1000) ); + memcpy(b->begin(), "Hello, world!", 14); + + t = timer(); + for(int i=0; i<10000000; i++) { + Reference r = std::move(b); + b = std::move(r); + } + t = timer()-t; + cout << "move Reference: " << 10.0/t << "M/sec " << endl; + + t = timer(); + for(int i=0; i<10000000; i++) { + Reference r = b; + } + t = timer()-t; + cout << "copy (1) Reference: " << 10.0/t << "M/sec " << endl; + + Reference c = b; + t = timer(); + for(int i=0; i<10000000; i++) { + Reference r = b; + } + t = timer()-t; + cout << "copy (2) Reference: " << 10.0/t << "M/sec " << endl; + + cout << (const char*)b->begin() << endl; + } + t = timer(); + for(int i=0; i<10000000; i++) { + delete new FastKey; + } + t = timer()-t; + cout << "delete new FastKey: " << 10.0/t << "M/sec " << fastKeyCount << endl; + + t = timer(); + for(int i=0; i<10000000; i++) { + Reference r( new FastKey ); + } + t = timer()-t; + cout << "new Reference: " << 10.0/t << "M/sec " << fastKeyCount << endl; +} + +template +Future threadSafetySender( vector& v, Event &start, Event &ready, int iterations ) { + for (int i = 0; i < iterations; i++) { + start.block(); + if (v.size() == 0) return Void(); + for(int i=0; i f, int32_t* count ) { + Void _ = wait(f); + interlockedIncrement(count); +} +ACTOR void threadSafetyWaiter( FutureStream f, int n, int32_t* count ) { + while (n--) { + Void _ = waitNext(f); + interlockedIncrement(count); + } +} + +#if 0 +void threadSafetyTest() { + double t = timer(); + + int N = 10000, V = 100; + + vector> v; + Event start, ready; + Future sender = inThread( [&] { return threadSafetySender( v, start, ready, N ); } ); + + for(int i=0; i()); + vector> f( v.size() ); + for(int i=0; i> streams( 100 ); + vector> streams; + for (int i = 0; i < 100; i++) + streams.push_back(PromiseStream()); + vector> v; + Event start, ready; + Future sender = inThread( [&] { return threadSafetySender( v, start, ready, N ); } ); + + for(int i=0; i counts( streams.size() ); + v.clear(); + for(int k=0; krandomInt(0, (int)streams.size()); + counts[i]++; + v.push_back( streams[i] ); + } + + start.set(); + int32_t count = 0; + for(int i=0; i returnCancelRacer( Future f ) { + try { + Void _ = wait(f); + } catch ( Error& ) { + interlockedIncrement( &cancelled ); + throw; + } + interlockedIncrement( &returned ); + return Void(); +} + +void returnCancelRaceTest() { + int N = 100, M = 100; + for(int i=0; i > promises; + vector< Future > futures; + for(int i=0; i < M; i++) { + promises.push_back( Promise() ); + futures.push_back( returnCancelRacer( promises.back().getFuture() ) ); + } + std::random_shuffle( futures.begin(), futures.end() ); + + // FIXME: Doesn't work as written with auto-reset + // events. Probably not particularly racy as written. Test may + // FAIL or PASS at whim. + + Event ev1, ev2; + ThreadFuture b = inThread( [&] ()->Void { + ev1.block(); + for(int i=0; i(); + return Void(); + } ); + ThreadFuture a = inThread([&]()->Void { + ev2.block(); + for(int i=0; i chooseTest( Future a, Future b ) { + choose { + when( int A = wait( a ) ) { return A; } + when( int B = wait( b ) ) { return B; } + } +} + +void showArena( ArenaBlock* a, ArenaBlock* parent) { + printf("ArenaBlock %p (<-%p): %d bytes, %d refs\n", a, parent, a->size(), a->debugGetReferenceCount()); + if (!a->isTiny()) { + int o = a->nextBlockOffset; + while (o) { + ArenaBlockRef* r = (ArenaBlockRef*)((char*)a->getData() + o); + showArena( r->next, a ); + o = r->nextBlockOffset; + } + } +} + +void arenaTest() { + BinaryWriter wr(AssumeVersion(currentProtocolVersion)); + { + Arena arena; + VectorRef test; + test.push_back( arena, StringRef(arena, LiteralStringRef("Hello")) ); + test.push_back( arena, StringRef(arena, LiteralStringRef(", ")) ); + test.push_back( arena, StringRef(arena, LiteralStringRef("World!")) ); + + for(auto i = test.begin(); i != test.end(); ++i) + for(auto j = i->begin(); j != i->end(); ++j) + cout << *j; + cout << endl; + + wr << test; + } + { + Arena arena2; + VectorRef test2; + BinaryReader reader(wr.getData(),wr.getLength(), AssumeVersion(currentProtocolVersion)); + reader >> test2 >> arena2; + + for(auto i = test2.begin(); i != test2.end(); ++i) + for(auto j = i->begin(); j != i->end(); ++j) + cout << *j; + cout << endl; + } + + double t = timer(); + for(int i=0; i<100; i++) { + Arena ar; + for(int i=0; i<10000000; i++) + new (ar) char[10]; + } + printf("100 x 10M x 10B allocated+freed from Arenas: %f sec\n", timer()-t); + + //printf("100M x 8bytes allocations: %d bytes used\n", 0);//ar.getSize()); + //showArena( ar.impl.getPtr(), 0 ); +}; + +ACTOR void testStream( FutureStream xs ) { + loop { + int x = waitNext(xs); + cout << x << endl; + } +} + +ACTOR Future actorTest1(bool b) { + printf("1"); + if (b) + throw future_version(); + return Void(); +} + +ACTOR void actorTest2(bool b) { + printf("2"); + if (b) + throw future_version(); +} + +ACTOR Future actorTest3(bool b) { + try { + if (b) + throw future_version(); + } catch (Error&) { + printf("3"); + return Void(); + } + printf("\nactorTest3 failed\n"); + return Void(); +} + +ACTOR Future actorTest4(bool b) { + state double tstart = now(); + try { + if (b) + throw operation_failed(); + } catch (...) { + Void _ = wait( delay(1) ); + } + if (now() < tstart + 1) + printf("actorTest4 failed"); + else + printf("4"); + return Void(); +} + +ACTOR Future actorTest5() { + state bool caught = false; + + loop { + loop { + state bool inloop = false; + if (caught) { + printf("5"); + return true; + } + try { + loop { + if (inloop) { printf("\nactorTest5 failed\n"); return false; } + inloop = true; + if (1) throw operation_failed(); + } + } catch (Error&) { + caught = true; + } + } + } +} + +ACTOR Future actorTest6() { + state bool caught = false; + loop { + if (caught) { printf("6"); return true; } + try { + if (1) throw operation_failed(); + } catch (Error&) { + caught = true; + } + } +} + +ACTOR Future actorTest7() { + try { + loop { + loop { + if (1) throw operation_failed(); + if (1) { printf("actorTest7 failed (1)\n"); return false; } + if (0) break; + } + if (1) { printf("actorTest7 failed (2)\n"); return false; } + } + } catch (Error&) { + printf("7"); + return true; + } +} + +ACTOR Future actorTest8() { + state bool caught = false; + state Future set = true; + + loop { + state bool inloop = false; + if (caught) { + printf("8"); + return true; + } + try { + loop { + if (inloop) { printf("\nactorTest8 failed\n"); return false; } + bool b = wait( set ); + inloop = true; + if (1) throw operation_failed(); + } + } catch (Error&) { + caught = true; + } + } +} + +ACTOR Future actorTest9A(Future setAfterCalling) { + state int count = 0; + loop { + if (count == 4) { printf("9"); return true; } + if (count && count!=4) { printf("\nactorTest9 failed\n"); return false; } + loop { + loop { + Void _ = wait( setAfterCalling ); + loop { + loop { + count++; + break; + } + Void _ = wait( Future(Void()) ); + count++; + break; + } + count++; + break; + } + count++; + break; + } + // loopDepth < 0 ??? + } +} + +Future actorTest9() { + Promise p; + Future f = actorTest9A(p.getFuture()); + p.send(Void()); + return f; +} + +ACTOR Future actorTest10A(FutureStream inputStream, Future go) { + state int i; + for(i = 0; i < 5; i++) { + Void _ = wait( go ); + int input = waitNext( inputStream ); + } + return Void(); +} + +void actorTest10() { + PromiseStream ins; + Promise go; + for(int x=0; x<2; x++) + ins.send(x); + Future a = actorTest10A( ins.getFuture(), go.getFuture() ); + go.send(Void()); + for(int x=0; x<3; x++) + ins.send(x); + if (!a.isReady()) + printf("\nactorTest10 failed\n"); + else + printf("10"); +} + +ACTOR Future cancellable() { + Void _ = wait( Never() ); + return Void(); +} + +ACTOR Future simple() { + return Void(); +} + +ACTOR Future simpleWait() { + Void _ = wait( Future(Void()) ); + return Void(); +} + +ACTOR Future simpleRet(Future x) { + int i = wait(x); + return i; +} + +template Future chain( Future const& x ); + +ACTOR template Future achain( Future x ) { + int k = wait( chain(x) ); + return k+1; +} + +template Future chain( Future const& x ) { + return achain(x); +} + +template<> Future chain<0>( Future const& x ) { + return x; +} + +Future chain2( Future const& x, int const& i ); + +ACTOR Future chain2( Future x, int i ) { + if (i>1) { + int k = wait( chain2(x, i-1) ); + return k+1; + } else { + int k = wait(x); + return k+i; + } +} + +ACTOR Future cancellable2() { + try { + Void _ = wait( Never() ); + return Void(); + } catch (Error& e) { + throw; + } +} + +using std::string; + +ACTOR Future introLoadValueFromDisk( Future filename ) { + string file = wait( filename ); + + if (file == "/dev/threes") + return 3; + else + ASSERT(false); + return 0; // does not happen +} + +ACTOR Future introAdd( Future a, Future b ) { + state int x = wait(a); + int y = wait(b); + return x + y; // x would be undefined here if it was not "state" +} + +ACTOR Future introFirst( Future a, Future b ) { + choose { + when( int x = wait(a) ) { + return x; + } + when( int x = wait(b) ) { + return x; + } + } +} + +struct AddReply { + int sum; + AddReply() {} + AddReply(int x) : sum(x) {} + + template + void serialize(Ar& ar) { + ar & sum; + } +}; + +struct AddRequest { + int a, b; + Promise< AddReply > reply; // Self-addressed envelope + + AddRequest() {} + AddRequest(int a, int b) : a(a), b(b) {} + + template + void serialize(Ar& ar) { + ar & a & b & reply; + } +}; + +ACTOR void introAddServer( PromiseStream add ) { + loop choose { + when ( AddRequest req = waitNext(add.getFuture()) ) { + printf("%d + %d = %d\n", req.a, req.b, req.a+req.b); + req.reply.send( req.a + req.b ); + } + } +} + +void introPromiseFuture() { + Promise myPromise; + + Future myFuture = myPromise.getFuture(); + + myPromise.send( 12345 ); + + ASSERT( myFuture.isReady() && myFuture.get() == 12345 ); +} + +void introActor() { + Future f = introLoadValueFromDisk( std::string("/dev/threes") ); + ASSERT( f.get() == 3 ); + + Promise a, b; + Future sum = introAdd(a.getFuture(), b.getFuture()); + b.send(3); + ASSERT( !sum.isReady() ); + a.send(2); + ASSERT( sum.get() == 5 ); + + Promise c,d; + Future first = introFirst( c.getFuture(), d.getFuture() ); + ASSERT( !first.isReady() ); + //d.send(100); + d.sendError( operation_failed() ); + ASSERT( first.isError() && first.getError().code() == error_code_operation_failed ); + //ASSERT( first.getBlocking() == 100 ); + + PromiseStream addInterface; + introAddServer( addInterface ); + + Future reply = addInterface.getReply( AddRequest(5,2) ); + ASSERT( reply.get().sum == 7 ); + + printf("OK\n"); +} + +template +void chainTest() { + auto startt = timer(); + for(int i=0; i<100000; i++) { + Promise p; + Future f = chain(p.getFuture()); + p.send(i); + ASSERT( f.get() == i+N ); + } + auto endt = timer(); + printf("chain<%d>: %0.3f M/sec\n", N, 0.1/(endt-startt)); + + startt = timer(); + for(int i=0; i<100000; i++) { + Promise p; + Future f = chain2(p.getFuture(), N); + p.send(i); + ASSERT( f.get() == i+N ); + } + endt = timer(); + printf("chain2<%d>: %0.3f M/sec\n", N, 0.1/(endt-startt)); + +} + +ACTOR void cycle(FutureStream in, PromiseStream out, int* ptotal){ + loop{ + Void _ = waitNext(in); + (*ptotal)++; + out.send(_); + } +} + +ACTOR Future cycleTime(int nodes, int times){ + state vector> n(nodes); + state int total = 0; + + // 1->2, 2->3, ..., n-1->0 + for (int i=1;i c; + + { + AsyncMap m1; + m1.set( 10, 1 ); + ASSERT( m1.get(10) == 1 ); + ASSERT( m1.get(20) == 0 ); + Future a = m1.onChange( 10 ); + Future b = m1.onChange( 20 ); + c = m1.onChange( 30 ); + ASSERT( !a.isReady() && !b.isReady() ); + m1.set(10, 0); + ASSERT( a.isReady() && !a.isError() && !b.isReady() && m1.get(10) == 0 ); + m1.set(20, 5); + ASSERT( b.isReady() && !b.isError() && m1.get(20)==5 ); + + a = m1.onChange(10); + b = m1.onChange(20); + m1.triggerRange( 15, 25 ); + ASSERT( !a.isReady() && b.isReady() && !b.isError() && m1.get(20) == 5 ); + } + ASSERT( c.isReady() && c.isError() && c.getError().code() == error_code_broken_promise ); + + printf("AsyncMap: OK\n"); + + double startt; + AsyncMap m2; + startt = timer(); + for(int i=0; i<1000000; i++) { + m2.set(5, 0); + m2.set(5, 1); + } + printf(" set(not present/present): %0.1fM/sec\n", 2.0 / (timer()-startt)); + startt = timer(); + for(int i=0; i<1000000; i++) { + m2.set(5, 1); + m2.set(5, 2); + } + printf(" set(present/present): %0.1fM/sec\n", 2.0 / (timer()-startt)); + startt = timer(); + for(int i=0; i<1000000; i++) { + m2.set(5, 1); + } + printf(" set(no change): %0.1fM/sec\n", 1.0 / (timer()-startt)); + + m2.set(5, 5); + startt = timer(); + for(int i=0; i<1000000; i++) + m2.onChange(5); + printf(" onChange(present, cancelled): %0.1fM/sec\n", 1.0 / (timer()-startt)); + startt = timer(); + for(int i=0; i<1000000; i++) + m2.onChange(10); + printf(" onChange(not present, cancelled): %0.1fM/sec\n", 1.0 / (timer()-startt)); + startt = timer(); + for(int i=0; i<1000000; i++) { + auto f = m2.onChange(10); + m2.set(10, 1); + m2.set(10, 0); + } + printf(" onChange(not present, set): %0.1fM/sec\n", 1.0 / (timer()-startt)); + startt = timer(); + for(int i=0; i<1000000; i++) { + auto f = m2.onChange(5); + m2.set(5, i+1); + } + printf(" onChange(present, set): %0.1fM/sec\n", 1.0 / (timer()-startt)); +} + +extern void net2_test(); + +void dsltest() { + double startt, endt; + + g_random = new DeterministicRandom(40); + + asyncMapTest(); + + net2_test(); + //sleeptest(); + + Future ctf = cycleTime(1000,1000); + ctf.get(); + + introPromiseFuture(); + introActor(); + // return; + + printf("Actor control flow tests: "); + actorTest1(true); + actorTest2(true); + actorTest3(true); + //if (g_network == &g_simulator) + //g_simulator.run( actorTest4(true) ); + actorTest5(); + actorTest6(); + actorTest7(); + actorTest8(); + actorTest9(); + actorTest10(); + + printf("\n"); + + printf("Running actor fuzz tests:\n"); + // Only include this test outside of Windows because of MSVC compiler bug +#ifndef WIN32 + auto afResults = actorFuzzTests(); +#else + std::pair afResults(0,0); +#endif + printf("Actor fuzz tests: %d/%d passed\n", afResults.first, afResults.second); + startt = timer(); + for(int i=0; i<1000000; i++) + g_random->random01(); + endt = timer(); + printf("Random01: %0.2f M/sec\n", 1.0/(endt-startt)); + + startt = timer(); + for(int i=0; i<1000000; i++) + Promise(); + endt = timer(); + printf("Promises: %0.2f M/sec\n", 1.0/(endt-startt)); + + startt = timer(); + for(int i=0; i<1000000; i++) + Promise().send(Void()); + endt = timer(); + printf("Promises (with send): %0.2f M/sec\n", 1.0/(endt-startt)); + + startt = timer(); + for(int i=0; i<1000000; i++) { + Promise p; + Future f = p.getFuture(); + p.send(Void()); + f.get(); + } + endt = timer(); + printf("Promise/Future/send roundtrip: %0.2f M/sec\n", 1.0/(endt-startt)); + + Promise p; + + startt = timer(); + for(int i=0; i<1000000; i++) + p.getFuture(); + endt = timer(); + printf("Futures: %0.2f M/sec\n", 1.0/(endt-startt)); + + startt = timer(); + for (int i = 0; i<1000000; i++) + PromiseStream(); + endt = timer(); + printf("PromiseStreams: %0.2f M/sec\n", 1.0 / (endt - startt)); + + startt = timer(); + for (int i = 0; i < 1000000; i++) + PromiseStream().send(Void()); + endt = timer(); + printf("PromiseStreams (with send): %0.2f M/sec\n", 1.0 / (endt - startt)); + + startt = timer(); + for (int i = 0; i<1000000; i++) { + PromiseStream p; + FutureStream f = p.getFuture(); + p.send(Void()); + f.pop(); + } + endt = timer(); + printf("PromiseStream/FutureStream/send/popBlocking roundtrip: %0.2f M/sec\n", 1.0 / (endt - startt)); + + startt = timer(); + { + PromiseStream ps; + for (int i = 0; i < 1000000; i++) { + ps.send(i); + } + } + endt = timer(); + printf("PromiseStream queued send: %0.2f M/sec\n", 1.0 / (endt - startt)); + + startt = timer(); + for(int i=0; i<1000000; i++) + cancellable(); + endt = timer(); + printf("Cancellations: %0.2f M/sec\n", 1.0/(endt-startt)); + + startt = timer(); + for(int i=0; i<1000000; i++) + cancellable2(); + endt = timer(); + printf("Cancellations with catch: %0.2f M/sec\n", 1.0/(endt-startt)); + + startt = timer(); + for(int i=0; i<1000000; i++) + simple(); + endt = timer(); + printf("Actor creation: %0.2f M/sec\n", 1.0/(endt-startt)); + + startt = timer(); + for(int i=0; i<1000000; i++) + simpleWait(); + endt = timer(); + printf("With trivial wait: %0.2f M/sec\n", 1.0/(endt-startt)); + + startt = timer(); + for(int i=0; i<1000000; i++) { + Promise p; + Future f = simpleRet(p.getFuture()); + p.send(i); + ASSERT( f.get() == i ); + } + endt = timer(); + printf("Bounce int through actor: %0.2f M/sec\n", 1.0/(endt-startt)); + + startt = timer(); + for(int i=0; i<1000000; i++) { + Promise p; + Future f = simpleRet(p.getFuture()); + Future g = simpleRet(p.getFuture()); + p.send(i); + ASSERT( f.get() == i ); + ASSERT( g.get() == i ); + } + endt = timer(); + printf("Bounce int through two actors in parallel: %0.2f M/sec\n", 1.0/(endt-startt)); + + /*chainTest<1>(); + chainTest<4>(); + chainTest<16>(); + chainTest<64>(); + + startt = timer(); + for(int i=0; i<1000000; i++) + try { + throw success(); + } catch (Error&) { + } + endt = timer(); + printf("C++ exception: %0.2f M/sec\n", 1.0/(endt-startt));*/ + + arenaTest(); + + { + Promise a, b; + Future c = chooseTest( a.getFuture(), b.getFuture() ); + a.send(1); + b.send(2); + cout << "c=" << c.get() << endl; + } + + { + Promise i; + Future d = addN<20>(i.getFuture()); + i.send( 1.1 ); + cout << d.get() << endl; + } + + { + Promise i; + i.sendError( operation_failed() ); + Future d = addN<20>(i.getFuture()); + if (d.isError() && d.getError().code() == error_code_operation_failed) + cout << "Error transmitted OK" << endl; + else + cout << "Error not transmitted!" << endl; + } + + /*{ + int na = Actor::allActors.size(); + PromiseStream t; + testStream(t.getFuture()); + if (Actor::allActors.size() != na+1) + cout << "Actor not created!" << endl; + t = PromiseStream(); + if (Actor::allActors.size() != na) + cout << "Actor not cleaned up!" << endl; + }*/ + + PromiseStream as; + Promise bs; + as.send( 4 ); + Future sT = switchTest( as.getFuture(), bs.getFuture() ); + as.send( 5 ); + //sT = move(Future()); + as.send( 6 ); + bs.send( 10.1 ); + as.send( 7 ); + + fastAllocTest(); + + #if FLOW_THREAD_SAFE + returnCancelRaceTest(); + threadSafetyTest(); + threadSafetyTest2(); + #else + printf("Thread safety disabled.\n"); + #endif +} + +/*ACTOR Future pingServer( FutureStream> requests, int rate ) { + state int count = 0; + loop { + Promise req = waitNext( requests ); + req.send( (++count)%rate != 0 ); + } +} + +ACTOR Future ping( PromiseStream> server ) { + state int count = 0; + loop { + bool result = wait( server.getReply() ); + + count++; + if (!result) + break; + } + return count; +} + +void pingtest() { + double start = timer(); + PromiseStream> serverInterface; + Future pS = pingServer( serverInterface.getFuture(), 5000000 ); + Future count = ping( serverInterface ); + double end = timer(); + cout << count.get() << " pings completed in " << (end-start) << " sec" << endl; +}*/ + +void copyTest() { + double start, elapsed; + + Arena arena; + StringRef s( new (arena) uint8_t[ 10<<20 ], 10<<20 ); + + { + start = timer(); + for(int i=0; i<100; i++) + StringRef k = s; + elapsed = timer() - start; + + printf("StringRef->StringRef: %fs/GB\n", elapsed); + } + + { + start = timer(); + for(int i=0; i<100; i++) + Standalone a = s; + elapsed = timer() - start; + + printf("StringRef->Standalone: %fs/GB\n", elapsed); + } + + { + Standalone sa = s; + start = timer(); + for(int i=0; i<100; i++) + Standalone a = sa; + elapsed = timer() - start; + + printf("Standalone->Standalone: %fs/GB\n", elapsed); + } + + { + Standalone sa = s, sb; + start = timer(); + for(int i=0; i<50; i++) { + sb = std::move(sa); + sa = std::move(sb); + } + elapsed = timer() - start; + printf("move(Standalone)->Standalone: %fs/GB\n", elapsed); + } +} + +/*ACTOR void badTest( FutureStream is ) { + state PromiseStream js; + + loop choose { + when( int j = waitNext( js.getFuture() ) ) { + cout << "J" << j << endl; + } + when( int i = waitNext( is ) ) { + cout << "I" << i << endl; + js.send( i ); + cout << "-I" << i << endl; + } + } +} + +void dsltest() { + PromiseStream is; + badTest( is.getFuture() ); + is.send(1); + is.send(2); + is.send(3); + throw not_implemented(); +} +void pingtest() {}*/ diff --git a/fdbrpc/fdbrpc.h b/fdbrpc/fdbrpc.h new file mode 100644 index 0000000000..d487b78dbf --- /dev/null +++ b/fdbrpc/fdbrpc.h @@ -0,0 +1,368 @@ +/* + * fdbrpc.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBRPC_FDBRPC_H +#define FDBRPC_FDBRPC_H +#pragma once + +#include "flow/flow.h" +#include "FlowTransport.h" // NetworkMessageReceiver Endpoint +#include "FailureMonitor.h" + + +struct FlowReceiver : private NetworkMessageReceiver { + // Common endpoint code for NetSAV<> and NetNotifiedQueue<> + + Endpoint endpoint; + bool m_isLocalEndpoint; + + FlowReceiver() : m_isLocalEndpoint(false) {} + FlowReceiver(Endpoint const& remoteEndpoint) : endpoint(remoteEndpoint), m_isLocalEndpoint(false) {} + ~FlowReceiver() { + if (m_isLocalEndpoint) + FlowTransport::transport().removeEndpoint(endpoint, this); + } + + bool isLocalEndpoint() { return m_isLocalEndpoint; } + bool isRemoteEndpoint() { return endpoint.isValid() && !m_isLocalEndpoint; } + + // If already a remote endpoint, returns that. Otherwise makes this + // a local endpoint and returns that. + const Endpoint& getEndpoint(int taskID) { + if (!endpoint.isValid()) { + m_isLocalEndpoint = true; + FlowTransport::transport().addEndpoint(endpoint, this, taskID); + } + return endpoint; + } + + void makeWellKnownEndpoint(Endpoint::Token token, int taskID) { + ASSERT(!endpoint.isValid()); + m_isLocalEndpoint = true; + endpoint.token = token; + FlowTransport::transport().addWellKnownEndpoint(endpoint, this, taskID); + } +}; + +template +struct NetSAV : SAV, FlowReceiver, FastAllocated> { + using FastAllocated>::operator new; + using FastAllocated>::operator delete; + + NetSAV(int futures, int promises) : SAV(futures, promises) {} + NetSAV(int futures, int promises, const Endpoint& remoteEndpoint) : SAV(futures, promises), FlowReceiver(remoteEndpoint) {} + + virtual void destroy() { delete this; } + virtual void receive(ArenaReader& reader) { + if (!SAV::canBeSet()) return; // load balancing and retries can result in the same request being answered twice + this->addPromiseRef(); + bool ok; + reader >> ok; + if (ok) { + T message; + reader >> message; + SAV::sendAndDelPromiseRef(message); + } + else { + Error error; + reader >> error; + SAV::sendErrorAndDelPromiseRef(error); + } + } +}; + + + +template +class ReplyPromise sealed +{ +public: + template + void send(U && value) const { + sav->send(std::forward(value)); + } + template + void sendError(const E& exc) const { sav->sendError(exc); } + + Future getFuture() const { sav->addFutureRef(); return Future(sav); } + bool isSet() { return sav->isSet(); } + bool isValid() const { return sav != NULL; } + ReplyPromise() : sav(new NetSAV(0, 1)) {} + ReplyPromise(const ReplyPromise& rhs) : sav(rhs.sav) { sav->addPromiseRef(); } + ReplyPromise(ReplyPromise&& rhs) noexcept(true) : sav(rhs.sav) { rhs.sav = 0; } + ~ReplyPromise() { if (sav) sav->delPromiseRef(); } + + ReplyPromise(const Endpoint& endpoint) : sav(new NetSAV(0, 1, endpoint)) {} + const Endpoint& getEndpoint(int taskID = TaskDefaultPromiseEndpoint) const { return sav->getEndpoint(taskID); } + + void operator=(const ReplyPromise& rhs) { + if (rhs.sav) rhs.sav->addPromiseRef(); + if (sav) sav->delPromiseRef(); + sav = rhs.sav; + } + void operator=(ReplyPromise && rhs) noexcept(true) { + if (sav != rhs.sav) { + if (sav) sav->delPromiseRef(); + sav = rhs.sav; + rhs.sav = 0; + } + } + void reset() { + *this = ReplyPromise(); + } + void swap(ReplyPromise& other) { + std::swap(sav, other.sav); + } + + // Beware, these operations are very unsafe + SAV* extractRawPointer() { auto ptr = sav; sav = NULL; return ptr; } + explicit ReplyPromise(SAV* ptr) : sav(ptr) {} + + int getFutureReferenceCount() const { return sav->getFutureReferenceCount(); } + int getPromiseReferenceCount() const { return sav->getPromiseReferenceCount(); } + +private: + NetSAV *sav; +}; + +template +void save(Ar& ar, const ReplyPromise& value) { + auto const& ep = value.getEndpoint(); + ar << ep; + ASSERT(!ep.address.isValid() || ep.address.isPublic()); // No re-serializing non-public addresses (the reply connection won't be available to any other process) +} + +template +void load(Ar& ar, ReplyPromise& value) { + Endpoint endpoint; + FlowTransport::transport().loadEndpoint(ar, endpoint); + value = ReplyPromise(endpoint); + networkSender(value.getFuture(), endpoint); +} + + +template +ReplyPromise const& getReplyPromise(ReplyPromise const& p) { return p; } + + + +template +void resetReply(Request& r) { r.reply.reset(); } + +template +void resetReply(ReplyPromise & p) { p.reset(); } + +template +void resetReply(Request& r, int taskID) { r.reply.reset(); r.reply.getEndpoint(taskID); } + +template +void resetReply(ReplyPromise & p, int taskID) { p.reset(); p.getEndpoint(taskID); } + +template +void setReplyPriority(Request& r, int taskID) { r.reply.getEndpoint(taskID); } + +template +void setReplyPriority(ReplyPromise & p, int taskID) { p.getEndpoint(taskID); } + +template +void setReplyPriority(const ReplyPromise & p, int taskID) { p.getEndpoint(taskID); } + + + + + +template +struct NetNotifiedQueue : NotifiedQueue, FlowReceiver, FastAllocated> { + using FastAllocated>::operator new; + using FastAllocated>::operator delete; + + NetNotifiedQueue(int futures, int promises) : NotifiedQueue(futures, promises) {} + NetNotifiedQueue(int futures, int promises, const Endpoint& remoteEndpoint) : NotifiedQueue(futures, promises), FlowReceiver(remoteEndpoint) {} + + virtual void destroy() { delete this; } + virtual void receive(ArenaReader& reader) { + this->addPromiseRef(); + T message; + reader >> message; + this->send(std::move(message)); + this->delPromiseRef(); + } + virtual bool isStream() const { return true; } +}; + + +template +class RequestStream { +public: + // stream.send( request ) + // Unreliable at most once delivery: Delivers request unless there is a connection failure (zero or one times) + + void send(const T& value) const { + if (queue->isRemoteEndpoint()) { + FlowTransport::transport().sendUnreliable(SerializeSource(value), getEndpoint()); + } + else + queue->send(value); + } + /*void sendError(const Error& error) const { + ASSERT( !queue->isRemoteEndpoint() ); + queue->sendError(error); + }*/ + + // stream.getReply( request ) + // Reliable at least once delivery: Eventually delivers request at least once and returns one of the replies if communication is possible. Might deliver request + // more than once. + // If a reply is returned, request was or will be delivered one or more times. + // If cancelled, request was or will be delivered zero or more times. + template + Future< REPLY_TYPE(X) > getReply(const X& value) const { + if (queue->isRemoteEndpoint()) { + return sendCanceler(getReplyPromise(value), FlowTransport::transport().sendReliable(SerializeSource(value), getEndpoint())); + } + send(value); + return getReplyPromise(value).getFuture(); + } + template + Future getReply(const X& value, int taskID) const { + setReplyPriority(value, taskID); + return getReply(value); + } + template + Future getReply() const { + return getReply(ReplyPromise()); + } + template + Future getReplyWithTaskID(int taskID) const { + ReplyPromise reply; + reply.getEndpoint(taskID); + return getReply(reply); + } + + // stream.tryGetReply( request ) + // Unreliable at most once delivery: Either delivers request and returns a reply, or returns failure (Optional()) eventually. + // If a reply is returned, request was delivered exactly once. + // If cancelled or returns failure, request was or will be delivered zero or one times. + // The caller must be capable of retrying if this request returns failure + template + Future> tryGetReply(const X& value, int taskID) const { + setReplyPriority(value, taskID); + if (queue->isRemoteEndpoint()) { + Future disc = makeDependent(IFailureMonitor::failureMonitor()).onDisconnectOrFailure(getEndpoint(taskID)); + if (disc.isReady()) { + return ErrorOr(request_maybe_delivered()); + } + FlowTransport::transport().sendUnreliable(SerializeSource(value), getEndpoint(taskID)); + auto& p = getReplyPromise(value); + return waitValueOrSignal(p.getFuture(), disc, getEndpoint(taskID), p); + } + send(value); + auto& p = getReplyPromise(value); + return waitValueOrSignal(p.getFuture(), Never(), getEndpoint(taskID), p); + } + + template + Future> tryGetReply(const X& value) const { + if (queue->isRemoteEndpoint()) { + Future disc = makeDependent(IFailureMonitor::failureMonitor()).onDisconnectOrFailure(getEndpoint()); + if (disc.isReady()) { + return ErrorOr(request_maybe_delivered()); + } + FlowTransport::transport().sendUnreliable(SerializeSource(value), getEndpoint()); + auto& p = getReplyPromise(value); + return waitValueOrSignal(p.getFuture(), disc, getEndpoint(), p); + } + else { + send(value); + auto& p = getReplyPromise(value); + return waitValueOrSignal(p.getFuture(), Never(), getEndpoint(), p); + } + } + + // stream.getReplyUnlessFailedFor( request, double sustainedFailureDuration, double sustainedFailureSlope ) + // Reliable at least once delivery: Like getReply, delivers request at least once and returns one of the replies. However, if + // the failure detector considers the endpoint failed permanently or for the given amount of time, returns failure instead. + // If a reply is returned, request was or will be delivered one or more times. + // If cancelled or returns failure, request was or will be delivered zero or more times. + // If it returns failure, the failure detector considers the endpoint failed permanently or for the given amount of time + // See IFailureMonitor::onFailedFor() for an explanation of the duration and slope parameters. + template + Future> getReplyUnlessFailedFor(const X& value, double sustainedFailureDuration, double sustainedFailureSlope, int taskID) const { + return waitValueOrSignal(getReply(value, taskID), makeDependent(IFailureMonitor::failureMonitor()).onFailedFor(getEndpoint(taskID), sustainedFailureDuration, sustainedFailureSlope), getEndpoint(taskID)); + } + + template + Future> getReplyUnlessFailedFor(const X& value, double sustainedFailureDuration, double sustainedFailureSlope) const { + return waitValueOrSignal(getReply(value), makeDependent(IFailureMonitor::failureMonitor()).onFailedFor(getEndpoint(), sustainedFailureDuration, sustainedFailureSlope), getEndpoint()); + } + + explicit RequestStream(const Endpoint& endpoint) : queue(new NetNotifiedQueue(0, 1, endpoint)) {} + + FutureStream getFuture() const { queue->addFutureRef(); return FutureStream(queue); } + RequestStream() : queue(new NetNotifiedQueue(0, 1)) {} + RequestStream(const RequestStream& rhs) : queue(rhs.queue) { queue->addPromiseRef(); } + RequestStream(RequestStream&& rhs) noexcept(true) : queue(rhs.queue) { rhs.queue = 0; } + void operator=(const RequestStream& rhs) { + rhs.queue->addPromiseRef(); + if (queue) queue->delPromiseRef(); + queue = rhs.queue; + } + void operator=(RequestStream&& rhs) noexcept(true) { + if (queue != rhs.queue) { + if (queue) queue->delPromiseRef(); + queue = rhs.queue; + rhs.queue = 0; + } + } + ~RequestStream() { + if (queue) + queue->delPromiseRef(); + //queue = (NetNotifiedQueue*)0xdeadbeef; + } + + Endpoint getEndpoint(int taskID = TaskDefaultEndpoint) const { return queue->getEndpoint(taskID); } + void makeWellKnownEndpoint(Endpoint::Token token, int taskID) { + queue->makeWellKnownEndpoint(token, taskID); + } + + bool operator == (const RequestStream& rhs) const { return queue == rhs.queue; } + bool isEmpty() const { return !queue->isReady(); } + +private: + NetNotifiedQueue* queue; +}; + +template +void save(Ar& ar, const RequestStream& value) { + auto const& ep = value.getEndpoint(); + ar << ep; + UNSTOPPABLE_ASSERT(ep.address.isValid()); // No serializing PromiseStreams on a client with no public address +} + +template +void load(Ar& ar, RequestStream& value) { + Endpoint endpoint; + FlowTransport::transport().loadEndpoint(ar, endpoint); + value = RequestStream(endpoint); +} + + + +#endif +#include "genericactors.actor.g.h" diff --git a/fdbrpc/fdbrpc.vcxproj b/fdbrpc/fdbrpc.vcxproj new file mode 100644 index 0000000000..03aed07ef7 --- /dev/null +++ b/fdbrpc/fdbrpc.vcxproj @@ -0,0 +1,249 @@ + + + + + Debug + X64 + + + Release + X64 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + false + + + false + + + false + + + false + + + false + + + false + + + false + + + false + + + + + + + false + + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {00AC9086-0377-4871-9991-DF267CF12ACA} + v4.5.2 + Win32Proj + fdbrpc + + + $(SolutionDir)bin\$(Configuration)\ + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + $(IntDir)\$(MSBuildProjectName).log + + + + StaticLibrary + MultiByte + v140_xp + + + StaticLibrary + MultiByte + v140_xp + + + + + + + + + + true + $(IncludePath);../;C:\Program Files\boost_1_52_0 + + + false + $(IncludePath);../;C:\Program Files\boost_1_52_0 + + + + echo const char *hgVersion = "Current version id not currently supported within Windows."; > hgVersion.temp.h && fc /b hgVersion.temp.h hgVersion.h > nul || copy hgVersion.temp.h hgVersion.h > nul + Checking HG source version + fake.out + + + + + + + Level3 + false + ProgramDatabase + Disabled + EnableFastChecks + MultiThreadedDebug + WIN32;_WIN32_WINNT=0x0502;WINVER=0x0502;NTDDI_VERSION=0x05020000;_DEBUG;_HAS_ITERATOR_DEBUGGING=0;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) + true + /bigobj @../flow/no_intellisense.opt %(AdditionalOptions) + + + Console + true + Advapi32.lib + + + + + + + + + Level3 + + + ProgramDatabase + Full + MultiThreaded + true + WIN32;_WIN32_WINNT=0x0502;WINVER=0x0502;NTDDI_VERSION=0x05020000;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;FDB_CLEAN_BUILD;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) + NotSet + false + /bigobj @../flow/no_intellisense.opt %(AdditionalOptions) + true + Speed + false + + + Console + true + false + false + Default + Advapi32.lib + /LTCG %(AdditionalOptions) + + + + + + + + + + + + + + diff --git a/fdbrpc/fdbrpc.vcxproj.filters b/fdbrpc/fdbrpc.vcxproj.filters new file mode 100644 index 0000000000..8a397eb32c --- /dev/null +++ b/fdbrpc/fdbrpc.vcxproj.filters @@ -0,0 +1,177 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + libcoroutine + + + libcoroutine + + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + + + + + + + + + + + + + + + + + + + libcoroutine + + + libcoroutine + + + libcoroutine + + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + zlib + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {c6db8910-449e-4436-8a6c-9e76b3e0ca1d} + + + {b79fbb2a-5d80-4135-b363-f6de83e62e73} + + + \ No newline at end of file diff --git a/fdbrpc/generated-constants.cpp b/fdbrpc/generated-constants.cpp new file mode 100644 index 0000000000..63a9986f6f --- /dev/null +++ b/fdbrpc/generated-constants.cpp @@ -0,0 +1,64 @@ +/* + Copyright (c) 2013 - 2014 Mark Adler, Robert Vazan + + This software is provided 'as-is', without any express or implied + warranty. In no event will the author be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + + THIS CODE HAS BEEN ALTERED FROM THE ORIGINAL +*/ + +#define POLY 0x82f63b78 +#define LONG_SHIFT 8192 +#define SHORT_SHIFT 256 + +#include + +static uint32_t table[16][256] = +{ + { 0x00000000, 0xf26b8303, 0xe13b70f7, 0x1350f3f4, 0xc79a971f, 0x35f1141c, 0x26a1e7e8, 0xd4ca64eb, 0x8ad958cf, 0x78b2dbcc, 0x6be22838, 0x9989ab3b, 0x4d43cfd0, 0xbf284cd3, 0xac78bf27, 0x5e133c24, 0x105ec76f, 0xe235446c, 0xf165b798, 0x030e349b, 0xd7c45070, 0x25afd373, 0x36ff2087, 0xc494a384, 0x9a879fa0, 0x68ec1ca3, 0x7bbcef57, 0x89d76c54, 0x5d1d08bf, 0xaf768bbc, 0xbc267848, 0x4e4dfb4b, 0x20bd8ede, 0xd2d60ddd, 0xc186fe29, 0x33ed7d2a, 0xe72719c1, 0x154c9ac2, 0x061c6936, 0xf477ea35, 0xaa64d611, 0x580f5512, 0x4b5fa6e6, 0xb93425e5, 0x6dfe410e, 0x9f95c20d, 0x8cc531f9, 0x7eaeb2fa, 0x30e349b1, 0xc288cab2, 0xd1d83946, 0x23b3ba45, 0xf779deae, 0x05125dad, 0x1642ae59, 0xe4292d5a, 0xba3a117e, 0x4851927d, 0x5b016189, 0xa96ae28a, 0x7da08661, 0x8fcb0562, 0x9c9bf696, 0x6ef07595, 0x417b1dbc, 0xb3109ebf, 0xa0406d4b, 0x522bee48, 0x86e18aa3, 0x748a09a0, 0x67dafa54, 0x95b17957, 0xcba24573, 0x39c9c670, 0x2a993584, 0xd8f2b687, 0x0c38d26c, 0xfe53516f, 0xed03a29b, 0x1f682198, 0x5125dad3, 0xa34e59d0, 0xb01eaa24, 0x42752927, 0x96bf4dcc, 0x64d4cecf, 0x77843d3b, 0x85efbe38, 0xdbfc821c, 0x2997011f, 0x3ac7f2eb, 0xc8ac71e8, 0x1c661503, 0xee0d9600, 0xfd5d65f4, 0x0f36e6f7, 0x61c69362, 0x93ad1061, 0x80fde395, 0x72966096, 0xa65c047d, 0x5437877e, 0x4767748a, 0xb50cf789, 0xeb1fcbad, 0x197448ae, 0x0a24bb5a, 0xf84f3859, 0x2c855cb2, 0xdeeedfb1, 0xcdbe2c45, 0x3fd5af46, 0x7198540d, 0x83f3d70e, 0x90a324fa, 0x62c8a7f9, 0xb602c312, 0x44694011, 0x5739b3e5, 0xa55230e6, 0xfb410cc2, 0x092a8fc1, 0x1a7a7c35, 0xe811ff36, 0x3cdb9bdd, 0xceb018de, 0xdde0eb2a, 0x2f8b6829, 0x82f63b78, 0x709db87b, 0x63cd4b8f, 0x91a6c88c, 0x456cac67, 0xb7072f64, 0xa457dc90, 0x563c5f93, 0x082f63b7, 0xfa44e0b4, 0xe9141340, 0x1b7f9043, 0xcfb5f4a8, 0x3dde77ab, 0x2e8e845f, 0xdce5075c, 0x92a8fc17, 0x60c37f14, 0x73938ce0, 0x81f80fe3, 0x55326b08, 0xa759e80b, 0xb4091bff, 0x466298fc, 0x1871a4d8, 0xea1a27db, 0xf94ad42f, 0x0b21572c, 0xdfeb33c7, 0x2d80b0c4, 0x3ed04330, 0xccbbc033, 0xa24bb5a6, 0x502036a5, 0x4370c551, 0xb11b4652, 0x65d122b9, 0x97baa1ba, 0x84ea524e, 0x7681d14d, 0x2892ed69, 0xdaf96e6a, 0xc9a99d9e, 0x3bc21e9d, 0xef087a76, 0x1d63f975, 0x0e330a81, 0xfc588982, 0xb21572c9, 0x407ef1ca, 0x532e023e, 0xa145813d, 0x758fe5d6, 0x87e466d5, 0x94b49521, 0x66df1622, 0x38cc2a06, 0xcaa7a905, 0xd9f75af1, 0x2b9cd9f2, 0xff56bd19, 0x0d3d3e1a, 0x1e6dcdee, 0xec064eed, 0xc38d26c4, 0x31e6a5c7, 0x22b65633, 0xd0ddd530, 0x0417b1db, 0xf67c32d8, 0xe52cc12c, 0x1747422f, 0x49547e0b, 0xbb3ffd08, 0xa86f0efc, 0x5a048dff, 0x8ecee914, 0x7ca56a17, 0x6ff599e3, 0x9d9e1ae0, 0xd3d3e1ab, 0x21b862a8, 0x32e8915c, 0xc083125f, 0x144976b4, 0xe622f5b7, 0xf5720643, 0x07198540, 0x590ab964, 0xab613a67, 0xb831c993, 0x4a5a4a90, 0x9e902e7b, 0x6cfbad78, 0x7fab5e8c, 0x8dc0dd8f, 0xe330a81a, 0x115b2b19, 0x020bd8ed, 0xf0605bee, 0x24aa3f05, 0xd6c1bc06, 0xc5914ff2, 0x37faccf1, 0x69e9f0d5, 0x9b8273d6, 0x88d28022, 0x7ab90321, 0xae7367ca, 0x5c18e4c9, 0x4f48173d, 0xbd23943e, 0xf36e6f75, 0x0105ec76, 0x12551f82, 0xe03e9c81, 0x34f4f86a, 0xc69f7b69, 0xd5cf889d, 0x27a40b9e, 0x79b737ba, 0x8bdcb4b9, 0x988c474d, 0x6ae7c44e, 0xbe2da0a5, 0x4c4623a6, 0x5f16d052, 0xad7d5351 }, + { 0x00000000, 0x13a29877, 0x274530ee, 0x34e7a899, 0x4e8a61dc, 0x5d28f9ab, 0x69cf5132, 0x7a6dc945, 0x9d14c3b8, 0x8eb65bcf, 0xba51f356, 0xa9f36b21, 0xd39ea264, 0xc03c3a13, 0xf4db928a, 0xe7790afd, 0x3fc5f181, 0x2c6769f6, 0x1880c16f, 0x0b225918, 0x714f905d, 0x62ed082a, 0x560aa0b3, 0x45a838c4, 0xa2d13239, 0xb173aa4e, 0x859402d7, 0x96369aa0, 0xec5b53e5, 0xfff9cb92, 0xcb1e630b, 0xd8bcfb7c, 0x7f8be302, 0x6c297b75, 0x58ced3ec, 0x4b6c4b9b, 0x310182de, 0x22a31aa9, 0x1644b230, 0x05e62a47, 0xe29f20ba, 0xf13db8cd, 0xc5da1054, 0xd6788823, 0xac154166, 0xbfb7d911, 0x8b507188, 0x98f2e9ff, 0x404e1283, 0x53ec8af4, 0x670b226d, 0x74a9ba1a, 0x0ec4735f, 0x1d66eb28, 0x298143b1, 0x3a23dbc6, 0xdd5ad13b, 0xcef8494c, 0xfa1fe1d5, 0xe9bd79a2, 0x93d0b0e7, 0x80722890, 0xb4958009, 0xa737187e, 0xff17c604, 0xecb55e73, 0xd852f6ea, 0xcbf06e9d, 0xb19da7d8, 0xa23f3faf, 0x96d89736, 0x857a0f41, 0x620305bc, 0x71a19dcb, 0x45463552, 0x56e4ad25, 0x2c896460, 0x3f2bfc17, 0x0bcc548e, 0x186eccf9, 0xc0d23785, 0xd370aff2, 0xe797076b, 0xf4359f1c, 0x8e585659, 0x9dface2e, 0xa91d66b7, 0xbabffec0, 0x5dc6f43d, 0x4e646c4a, 0x7a83c4d3, 0x69215ca4, 0x134c95e1, 0x00ee0d96, 0x3409a50f, 0x27ab3d78, 0x809c2506, 0x933ebd71, 0xa7d915e8, 0xb47b8d9f, 0xce1644da, 0xddb4dcad, 0xe9537434, 0xfaf1ec43, 0x1d88e6be, 0x0e2a7ec9, 0x3acdd650, 0x296f4e27, 0x53028762, 0x40a01f15, 0x7447b78c, 0x67e52ffb, 0xbf59d487, 0xacfb4cf0, 0x981ce469, 0x8bbe7c1e, 0xf1d3b55b, 0xe2712d2c, 0xd69685b5, 0xc5341dc2, 0x224d173f, 0x31ef8f48, 0x050827d1, 0x16aabfa6, 0x6cc776e3, 0x7f65ee94, 0x4b82460d, 0x5820de7a, 0xfbc3faf9, 0xe861628e, 0xdc86ca17, 0xcf245260, 0xb5499b25, 0xa6eb0352, 0x920cabcb, 0x81ae33bc, 0x66d73941, 0x7575a136, 0x419209af, 0x523091d8, 0x285d589d, 0x3bffc0ea, 0x0f186873, 0x1cbaf004, 0xc4060b78, 0xd7a4930f, 0xe3433b96, 0xf0e1a3e1, 0x8a8c6aa4, 0x992ef2d3, 0xadc95a4a, 0xbe6bc23d, 0x5912c8c0, 0x4ab050b7, 0x7e57f82e, 0x6df56059, 0x1798a91c, 0x043a316b, 0x30dd99f2, 0x237f0185, 0x844819fb, 0x97ea818c, 0xa30d2915, 0xb0afb162, 0xcac27827, 0xd960e050, 0xed8748c9, 0xfe25d0be, 0x195cda43, 0x0afe4234, 0x3e19eaad, 0x2dbb72da, 0x57d6bb9f, 0x447423e8, 0x70938b71, 0x63311306, 0xbb8de87a, 0xa82f700d, 0x9cc8d894, 0x8f6a40e3, 0xf50789a6, 0xe6a511d1, 0xd242b948, 0xc1e0213f, 0x26992bc2, 0x353bb3b5, 0x01dc1b2c, 0x127e835b, 0x68134a1e, 0x7bb1d269, 0x4f567af0, 0x5cf4e287, 0x04d43cfd, 0x1776a48a, 0x23910c13, 0x30339464, 0x4a5e5d21, 0x59fcc556, 0x6d1b6dcf, 0x7eb9f5b8, 0x99c0ff45, 0x8a626732, 0xbe85cfab, 0xad2757dc, 0xd74a9e99, 0xc4e806ee, 0xf00fae77, 0xe3ad3600, 0x3b11cd7c, 0x28b3550b, 0x1c54fd92, 0x0ff665e5, 0x759baca0, 0x663934d7, 0x52de9c4e, 0x417c0439, 0xa6050ec4, 0xb5a796b3, 0x81403e2a, 0x92e2a65d, 0xe88f6f18, 0xfb2df76f, 0xcfca5ff6, 0xdc68c781, 0x7b5fdfff, 0x68fd4788, 0x5c1aef11, 0x4fb87766, 0x35d5be23, 0x26772654, 0x12908ecd, 0x013216ba, 0xe64b1c47, 0xf5e98430, 0xc10e2ca9, 0xd2acb4de, 0xa8c17d9b, 0xbb63e5ec, 0x8f844d75, 0x9c26d502, 0x449a2e7e, 0x5738b609, 0x63df1e90, 0x707d86e7, 0x0a104fa2, 0x19b2d7d5, 0x2d557f4c, 0x3ef7e73b, 0xd98eedc6, 0xca2c75b1, 0xfecbdd28, 0xed69455f, 0x97048c1a, 0x84a6146d, 0xb041bcf4, 0xa3e32483 }, + { 0x00000000, 0xa541927e, 0x4f6f520d, 0xea2ec073, 0x9edea41a, 0x3b9f3664, 0xd1b1f617, 0x74f06469, 0x38513ec5, 0x9d10acbb, 0x773e6cc8, 0xd27ffeb6, 0xa68f9adf, 0x03ce08a1, 0xe9e0c8d2, 0x4ca15aac, 0x70a27d8a, 0xd5e3eff4, 0x3fcd2f87, 0x9a8cbdf9, 0xee7cd990, 0x4b3d4bee, 0xa1138b9d, 0x045219e3, 0x48f3434f, 0xedb2d131, 0x079c1142, 0xa2dd833c, 0xd62de755, 0x736c752b, 0x9942b558, 0x3c032726, 0xe144fb14, 0x4405696a, 0xae2ba919, 0x0b6a3b67, 0x7f9a5f0e, 0xdadbcd70, 0x30f50d03, 0x95b49f7d, 0xd915c5d1, 0x7c5457af, 0x967a97dc, 0x333b05a2, 0x47cb61cb, 0xe28af3b5, 0x08a433c6, 0xade5a1b8, 0x91e6869e, 0x34a714e0, 0xde89d493, 0x7bc846ed, 0x0f382284, 0xaa79b0fa, 0x40577089, 0xe516e2f7, 0xa9b7b85b, 0x0cf62a25, 0xe6d8ea56, 0x43997828, 0x37691c41, 0x92288e3f, 0x78064e4c, 0xdd47dc32, 0xc76580d9, 0x622412a7, 0x880ad2d4, 0x2d4b40aa, 0x59bb24c3, 0xfcfab6bd, 0x16d476ce, 0xb395e4b0, 0xff34be1c, 0x5a752c62, 0xb05bec11, 0x151a7e6f, 0x61ea1a06, 0xc4ab8878, 0x2e85480b, 0x8bc4da75, 0xb7c7fd53, 0x12866f2d, 0xf8a8af5e, 0x5de93d20, 0x29195949, 0x8c58cb37, 0x66760b44, 0xc337993a, 0x8f96c396, 0x2ad751e8, 0xc0f9919b, 0x65b803e5, 0x1148678c, 0xb409f5f2, 0x5e273581, 0xfb66a7ff, 0x26217bcd, 0x8360e9b3, 0x694e29c0, 0xcc0fbbbe, 0xb8ffdfd7, 0x1dbe4da9, 0xf7908dda, 0x52d11fa4, 0x1e704508, 0xbb31d776, 0x511f1705, 0xf45e857b, 0x80aee112, 0x25ef736c, 0xcfc1b31f, 0x6a802161, 0x56830647, 0xf3c29439, 0x19ec544a, 0xbcadc634, 0xc85da25d, 0x6d1c3023, 0x8732f050, 0x2273622e, 0x6ed23882, 0xcb93aafc, 0x21bd6a8f, 0x84fcf8f1, 0xf00c9c98, 0x554d0ee6, 0xbf63ce95, 0x1a225ceb, 0x8b277743, 0x2e66e53d, 0xc448254e, 0x6109b730, 0x15f9d359, 0xb0b84127, 0x5a968154, 0xffd7132a, 0xb3764986, 0x1637dbf8, 0xfc191b8b, 0x595889f5, 0x2da8ed9c, 0x88e97fe2, 0x62c7bf91, 0xc7862def, 0xfb850ac9, 0x5ec498b7, 0xb4ea58c4, 0x11abcaba, 0x655baed3, 0xc01a3cad, 0x2a34fcde, 0x8f756ea0, 0xc3d4340c, 0x6695a672, 0x8cbb6601, 0x29faf47f, 0x5d0a9016, 0xf84b0268, 0x1265c21b, 0xb7245065, 0x6a638c57, 0xcf221e29, 0x250cde5a, 0x804d4c24, 0xf4bd284d, 0x51fcba33, 0xbbd27a40, 0x1e93e83e, 0x5232b292, 0xf77320ec, 0x1d5de09f, 0xb81c72e1, 0xccec1688, 0x69ad84f6, 0x83834485, 0x26c2d6fb, 0x1ac1f1dd, 0xbf8063a3, 0x55aea3d0, 0xf0ef31ae, 0x841f55c7, 0x215ec7b9, 0xcb7007ca, 0x6e3195b4, 0x2290cf18, 0x87d15d66, 0x6dff9d15, 0xc8be0f6b, 0xbc4e6b02, 0x190ff97c, 0xf321390f, 0x5660ab71, 0x4c42f79a, 0xe90365e4, 0x032da597, 0xa66c37e9, 0xd29c5380, 0x77ddc1fe, 0x9df3018d, 0x38b293f3, 0x7413c95f, 0xd1525b21, 0x3b7c9b52, 0x9e3d092c, 0xeacd6d45, 0x4f8cff3b, 0xa5a23f48, 0x00e3ad36, 0x3ce08a10, 0x99a1186e, 0x738fd81d, 0xd6ce4a63, 0xa23e2e0a, 0x077fbc74, 0xed517c07, 0x4810ee79, 0x04b1b4d5, 0xa1f026ab, 0x4bdee6d8, 0xee9f74a6, 0x9a6f10cf, 0x3f2e82b1, 0xd50042c2, 0x7041d0bc, 0xad060c8e, 0x08479ef0, 0xe2695e83, 0x4728ccfd, 0x33d8a894, 0x96993aea, 0x7cb7fa99, 0xd9f668e7, 0x9557324b, 0x3016a035, 0xda386046, 0x7f79f238, 0x0b899651, 0xaec8042f, 0x44e6c45c, 0xe1a75622, 0xdda47104, 0x78e5e37a, 0x92cb2309, 0x378ab177, 0x437ad51e, 0xe63b4760, 0x0c158713, 0xa954156d, 0xe5f54fc1, 0x40b4ddbf, 0xaa9a1dcc, 0x0fdb8fb2, 0x7b2bebdb, 0xde6a79a5, 0x3444b9d6, 0x91052ba8 }, + { 0x00000000, 0xdd45aab8, 0xbf672381, 0x62228939, 0x7b2231f3, 0xa6679b4b, 0xc4451272, 0x1900b8ca, 0xf64463e6, 0x2b01c95e, 0x49234067, 0x9466eadf, 0x8d665215, 0x5023f8ad, 0x32017194, 0xef44db2c, 0xe964b13d, 0x34211b85, 0x560392bc, 0x8b463804, 0x924680ce, 0x4f032a76, 0x2d21a34f, 0xf06409f7, 0x1f20d2db, 0xc2657863, 0xa047f15a, 0x7d025be2, 0x6402e328, 0xb9474990, 0xdb65c0a9, 0x06206a11, 0xd725148b, 0x0a60be33, 0x6842370a, 0xb5079db2, 0xac072578, 0x71428fc0, 0x136006f9, 0xce25ac41, 0x2161776d, 0xfc24ddd5, 0x9e0654ec, 0x4343fe54, 0x5a43469e, 0x8706ec26, 0xe524651f, 0x3861cfa7, 0x3e41a5b6, 0xe3040f0e, 0x81268637, 0x5c632c8f, 0x45639445, 0x98263efd, 0xfa04b7c4, 0x27411d7c, 0xc805c650, 0x15406ce8, 0x7762e5d1, 0xaa274f69, 0xb327f7a3, 0x6e625d1b, 0x0c40d422, 0xd1057e9a, 0xaba65fe7, 0x76e3f55f, 0x14c17c66, 0xc984d6de, 0xd0846e14, 0x0dc1c4ac, 0x6fe34d95, 0xb2a6e72d, 0x5de23c01, 0x80a796b9, 0xe2851f80, 0x3fc0b538, 0x26c00df2, 0xfb85a74a, 0x99a72e73, 0x44e284cb, 0x42c2eeda, 0x9f874462, 0xfda5cd5b, 0x20e067e3, 0x39e0df29, 0xe4a57591, 0x8687fca8, 0x5bc25610, 0xb4868d3c, 0x69c32784, 0x0be1aebd, 0xd6a40405, 0xcfa4bccf, 0x12e11677, 0x70c39f4e, 0xad8635f6, 0x7c834b6c, 0xa1c6e1d4, 0xc3e468ed, 0x1ea1c255, 0x07a17a9f, 0xdae4d027, 0xb8c6591e, 0x6583f3a6, 0x8ac7288a, 0x57828232, 0x35a00b0b, 0xe8e5a1b3, 0xf1e51979, 0x2ca0b3c1, 0x4e823af8, 0x93c79040, 0x95e7fa51, 0x48a250e9, 0x2a80d9d0, 0xf7c57368, 0xeec5cba2, 0x3380611a, 0x51a2e823, 0x8ce7429b, 0x63a399b7, 0xbee6330f, 0xdcc4ba36, 0x0181108e, 0x1881a844, 0xc5c402fc, 0xa7e68bc5, 0x7aa3217d, 0x52a0c93f, 0x8fe56387, 0xedc7eabe, 0x30824006, 0x2982f8cc, 0xf4c75274, 0x96e5db4d, 0x4ba071f5, 0xa4e4aad9, 0x79a10061, 0x1b838958, 0xc6c623e0, 0xdfc69b2a, 0x02833192, 0x60a1b8ab, 0xbde41213, 0xbbc47802, 0x6681d2ba, 0x04a35b83, 0xd9e6f13b, 0xc0e649f1, 0x1da3e349, 0x7f816a70, 0xa2c4c0c8, 0x4d801be4, 0x90c5b15c, 0xf2e73865, 0x2fa292dd, 0x36a22a17, 0xebe780af, 0x89c50996, 0x5480a32e, 0x8585ddb4, 0x58c0770c, 0x3ae2fe35, 0xe7a7548d, 0xfea7ec47, 0x23e246ff, 0x41c0cfc6, 0x9c85657e, 0x73c1be52, 0xae8414ea, 0xcca69dd3, 0x11e3376b, 0x08e38fa1, 0xd5a62519, 0xb784ac20, 0x6ac10698, 0x6ce16c89, 0xb1a4c631, 0xd3864f08, 0x0ec3e5b0, 0x17c35d7a, 0xca86f7c2, 0xa8a47efb, 0x75e1d443, 0x9aa50f6f, 0x47e0a5d7, 0x25c22cee, 0xf8878656, 0xe1873e9c, 0x3cc29424, 0x5ee01d1d, 0x83a5b7a5, 0xf90696d8, 0x24433c60, 0x4661b559, 0x9b241fe1, 0x8224a72b, 0x5f610d93, 0x3d4384aa, 0xe0062e12, 0x0f42f53e, 0xd2075f86, 0xb025d6bf, 0x6d607c07, 0x7460c4cd, 0xa9256e75, 0xcb07e74c, 0x16424df4, 0x106227e5, 0xcd278d5d, 0xaf050464, 0x7240aedc, 0x6b401616, 0xb605bcae, 0xd4273597, 0x09629f2f, 0xe6264403, 0x3b63eebb, 0x59416782, 0x8404cd3a, 0x9d0475f0, 0x4041df48, 0x22635671, 0xff26fcc9, 0x2e238253, 0xf36628eb, 0x9144a1d2, 0x4c010b6a, 0x5501b3a0, 0x88441918, 0xea669021, 0x37233a99, 0xd867e1b5, 0x05224b0d, 0x6700c234, 0xba45688c, 0xa345d046, 0x7e007afe, 0x1c22f3c7, 0xc167597f, 0xc747336e, 0x1a0299d6, 0x782010ef, 0xa565ba57, 0xbc65029d, 0x6120a825, 0x0302211c, 0xde478ba4, 0x31035088, 0xec46fa30, 0x8e647309, 0x5321d9b1, 0x4a21617b, 0x9764cbc3, 0xf54642fa, 0x2803e842 }, + { 0x00000000, 0x38116fac, 0x7022df58, 0x4833b0f4, 0xe045beb0, 0xd854d11c, 0x906761e8, 0xa8760e44, 0xc5670b91, 0xfd76643d, 0xb545d4c9, 0x8d54bb65, 0x2522b521, 0x1d33da8d, 0x55006a79, 0x6d1105d5, 0x8f2261d3, 0xb7330e7f, 0xff00be8b, 0xc711d127, 0x6f67df63, 0x5776b0cf, 0x1f45003b, 0x27546f97, 0x4a456a42, 0x725405ee, 0x3a67b51a, 0x0276dab6, 0xaa00d4f2, 0x9211bb5e, 0xda220baa, 0xe2336406, 0x1ba8b557, 0x23b9dafb, 0x6b8a6a0f, 0x539b05a3, 0xfbed0be7, 0xc3fc644b, 0x8bcfd4bf, 0xb3debb13, 0xdecfbec6, 0xe6ded16a, 0xaeed619e, 0x96fc0e32, 0x3e8a0076, 0x069b6fda, 0x4ea8df2e, 0x76b9b082, 0x948ad484, 0xac9bbb28, 0xe4a80bdc, 0xdcb96470, 0x74cf6a34, 0x4cde0598, 0x04edb56c, 0x3cfcdac0, 0x51eddf15, 0x69fcb0b9, 0x21cf004d, 0x19de6fe1, 0xb1a861a5, 0x89b90e09, 0xc18abefd, 0xf99bd151, 0x37516aae, 0x0f400502, 0x4773b5f6, 0x7f62da5a, 0xd714d41e, 0xef05bbb2, 0xa7360b46, 0x9f2764ea, 0xf236613f, 0xca270e93, 0x8214be67, 0xba05d1cb, 0x1273df8f, 0x2a62b023, 0x625100d7, 0x5a406f7b, 0xb8730b7d, 0x806264d1, 0xc851d425, 0xf040bb89, 0x5836b5cd, 0x6027da61, 0x28146a95, 0x10050539, 0x7d1400ec, 0x45056f40, 0x0d36dfb4, 0x3527b018, 0x9d51be5c, 0xa540d1f0, 0xed736104, 0xd5620ea8, 0x2cf9dff9, 0x14e8b055, 0x5cdb00a1, 0x64ca6f0d, 0xccbc6149, 0xf4ad0ee5, 0xbc9ebe11, 0x848fd1bd, 0xe99ed468, 0xd18fbbc4, 0x99bc0b30, 0xa1ad649c, 0x09db6ad8, 0x31ca0574, 0x79f9b580, 0x41e8da2c, 0xa3dbbe2a, 0x9bcad186, 0xd3f96172, 0xebe80ede, 0x439e009a, 0x7b8f6f36, 0x33bcdfc2, 0x0badb06e, 0x66bcb5bb, 0x5eadda17, 0x169e6ae3, 0x2e8f054f, 0x86f90b0b, 0xbee864a7, 0xf6dbd453, 0xcecabbff, 0x6ea2d55c, 0x56b3baf0, 0x1e800a04, 0x269165a8, 0x8ee76bec, 0xb6f60440, 0xfec5b4b4, 0xc6d4db18, 0xabc5decd, 0x93d4b161, 0xdbe70195, 0xe3f66e39, 0x4b80607d, 0x73910fd1, 0x3ba2bf25, 0x03b3d089, 0xe180b48f, 0xd991db23, 0x91a26bd7, 0xa9b3047b, 0x01c50a3f, 0x39d46593, 0x71e7d567, 0x49f6bacb, 0x24e7bf1e, 0x1cf6d0b2, 0x54c56046, 0x6cd40fea, 0xc4a201ae, 0xfcb36e02, 0xb480def6, 0x8c91b15a, 0x750a600b, 0x4d1b0fa7, 0x0528bf53, 0x3d39d0ff, 0x954fdebb, 0xad5eb117, 0xe56d01e3, 0xdd7c6e4f, 0xb06d6b9a, 0x887c0436, 0xc04fb4c2, 0xf85edb6e, 0x5028d52a, 0x6839ba86, 0x200a0a72, 0x181b65de, 0xfa2801d8, 0xc2396e74, 0x8a0ade80, 0xb21bb12c, 0x1a6dbf68, 0x227cd0c4, 0x6a4f6030, 0x525e0f9c, 0x3f4f0a49, 0x075e65e5, 0x4f6dd511, 0x777cbabd, 0xdf0ab4f9, 0xe71bdb55, 0xaf286ba1, 0x9739040d, 0x59f3bff2, 0x61e2d05e, 0x29d160aa, 0x11c00f06, 0xb9b60142, 0x81a76eee, 0xc994de1a, 0xf185b1b6, 0x9c94b463, 0xa485dbcf, 0xecb66b3b, 0xd4a70497, 0x7cd10ad3, 0x44c0657f, 0x0cf3d58b, 0x34e2ba27, 0xd6d1de21, 0xeec0b18d, 0xa6f30179, 0x9ee26ed5, 0x36946091, 0x0e850f3d, 0x46b6bfc9, 0x7ea7d065, 0x13b6d5b0, 0x2ba7ba1c, 0x63940ae8, 0x5b856544, 0xf3f36b00, 0xcbe204ac, 0x83d1b458, 0xbbc0dbf4, 0x425b0aa5, 0x7a4a6509, 0x3279d5fd, 0x0a68ba51, 0xa21eb415, 0x9a0fdbb9, 0xd23c6b4d, 0xea2d04e1, 0x873c0134, 0xbf2d6e98, 0xf71ede6c, 0xcf0fb1c0, 0x6779bf84, 0x5f68d028, 0x175b60dc, 0x2f4a0f70, 0xcd796b76, 0xf56804da, 0xbd5bb42e, 0x854adb82, 0x2d3cd5c6, 0x152dba6a, 0x5d1e0a9e, 0x650f6532, 0x081e60e7, 0x300f0f4b, 0x783cbfbf, 0x402dd013, 0xe85bde57, 0xd04ab1fb, 0x9879010f, 0xa0686ea3 }, + { 0x00000000, 0xef306b19, 0xdb8ca0c3, 0x34bccbda, 0xb2f53777, 0x5dc55c6e, 0x697997b4, 0x8649fcad, 0x6006181f, 0x8f367306, 0xbb8ab8dc, 0x54bad3c5, 0xd2f32f68, 0x3dc34471, 0x097f8fab, 0xe64fe4b2, 0xc00c303e, 0x2f3c5b27, 0x1b8090fd, 0xf4b0fbe4, 0x72f90749, 0x9dc96c50, 0xa975a78a, 0x4645cc93, 0xa00a2821, 0x4f3a4338, 0x7b8688e2, 0x94b6e3fb, 0x12ff1f56, 0xfdcf744f, 0xc973bf95, 0x2643d48c, 0x85f4168d, 0x6ac47d94, 0x5e78b64e, 0xb148dd57, 0x370121fa, 0xd8314ae3, 0xec8d8139, 0x03bdea20, 0xe5f20e92, 0x0ac2658b, 0x3e7eae51, 0xd14ec548, 0x570739e5, 0xb83752fc, 0x8c8b9926, 0x63bbf23f, 0x45f826b3, 0xaac84daa, 0x9e748670, 0x7144ed69, 0xf70d11c4, 0x183d7add, 0x2c81b107, 0xc3b1da1e, 0x25fe3eac, 0xcace55b5, 0xfe729e6f, 0x1142f576, 0x970b09db, 0x783b62c2, 0x4c87a918, 0xa3b7c201, 0x0e045beb, 0xe13430f2, 0xd588fb28, 0x3ab89031, 0xbcf16c9c, 0x53c10785, 0x677dcc5f, 0x884da746, 0x6e0243f4, 0x813228ed, 0xb58ee337, 0x5abe882e, 0xdcf77483, 0x33c71f9a, 0x077bd440, 0xe84bbf59, 0xce086bd5, 0x213800cc, 0x1584cb16, 0xfab4a00f, 0x7cfd5ca2, 0x93cd37bb, 0xa771fc61, 0x48419778, 0xae0e73ca, 0x413e18d3, 0x7582d309, 0x9ab2b810, 0x1cfb44bd, 0xf3cb2fa4, 0xc777e47e, 0x28478f67, 0x8bf04d66, 0x64c0267f, 0x507ceda5, 0xbf4c86bc, 0x39057a11, 0xd6351108, 0xe289dad2, 0x0db9b1cb, 0xebf65579, 0x04c63e60, 0x307af5ba, 0xdf4a9ea3, 0x5903620e, 0xb6330917, 0x828fc2cd, 0x6dbfa9d4, 0x4bfc7d58, 0xa4cc1641, 0x9070dd9b, 0x7f40b682, 0xf9094a2f, 0x16392136, 0x2285eaec, 0xcdb581f5, 0x2bfa6547, 0xc4ca0e5e, 0xf076c584, 0x1f46ae9d, 0x990f5230, 0x763f3929, 0x4283f2f3, 0xadb399ea, 0x1c08b7d6, 0xf338dccf, 0xc7841715, 0x28b47c0c, 0xaefd80a1, 0x41cdebb8, 0x75712062, 0x9a414b7b, 0x7c0eafc9, 0x933ec4d0, 0xa7820f0a, 0x48b26413, 0xcefb98be, 0x21cbf3a7, 0x1577387d, 0xfa475364, 0xdc0487e8, 0x3334ecf1, 0x0788272b, 0xe8b84c32, 0x6ef1b09f, 0x81c1db86, 0xb57d105c, 0x5a4d7b45, 0xbc029ff7, 0x5332f4ee, 0x678e3f34, 0x88be542d, 0x0ef7a880, 0xe1c7c399, 0xd57b0843, 0x3a4b635a, 0x99fca15b, 0x76ccca42, 0x42700198, 0xad406a81, 0x2b09962c, 0xc439fd35, 0xf08536ef, 0x1fb55df6, 0xf9fab944, 0x16cad25d, 0x22761987, 0xcd46729e, 0x4b0f8e33, 0xa43fe52a, 0x90832ef0, 0x7fb345e9, 0x59f09165, 0xb6c0fa7c, 0x827c31a6, 0x6d4c5abf, 0xeb05a612, 0x0435cd0b, 0x308906d1, 0xdfb96dc8, 0x39f6897a, 0xd6c6e263, 0xe27a29b9, 0x0d4a42a0, 0x8b03be0d, 0x6433d514, 0x508f1ece, 0xbfbf75d7, 0x120cec3d, 0xfd3c8724, 0xc9804cfe, 0x26b027e7, 0xa0f9db4a, 0x4fc9b053, 0x7b757b89, 0x94451090, 0x720af422, 0x9d3a9f3b, 0xa98654e1, 0x46b63ff8, 0xc0ffc355, 0x2fcfa84c, 0x1b736396, 0xf443088f, 0xd200dc03, 0x3d30b71a, 0x098c7cc0, 0xe6bc17d9, 0x60f5eb74, 0x8fc5806d, 0xbb794bb7, 0x544920ae, 0xb206c41c, 0x5d36af05, 0x698a64df, 0x86ba0fc6, 0x00f3f36b, 0xefc39872, 0xdb7f53a8, 0x344f38b1, 0x97f8fab0, 0x78c891a9, 0x4c745a73, 0xa344316a, 0x250dcdc7, 0xca3da6de, 0xfe816d04, 0x11b1061d, 0xf7fee2af, 0x18ce89b6, 0x2c72426c, 0xc3422975, 0x450bd5d8, 0xaa3bbec1, 0x9e87751b, 0x71b71e02, 0x57f4ca8e, 0xb8c4a197, 0x8c786a4d, 0x63480154, 0xe501fdf9, 0x0a3196e0, 0x3e8d5d3a, 0xd1bd3623, 0x37f2d291, 0xd8c2b988, 0xec7e7252, 0x034e194b, 0x8507e5e6, 0x6a378eff, 0x5e8b4525, 0xb1bb2e3c }, + { 0x00000000, 0x68032cc8, 0xd0065990, 0xb8057558, 0xa5e0c5d1, 0xcde3e919, 0x75e69c41, 0x1de5b089, 0x4e2dfd53, 0x262ed19b, 0x9e2ba4c3, 0xf628880b, 0xebcd3882, 0x83ce144a, 0x3bcb6112, 0x53c84dda, 0x9c5bfaa6, 0xf458d66e, 0x4c5da336, 0x245e8ffe, 0x39bb3f77, 0x51b813bf, 0xe9bd66e7, 0x81be4a2f, 0xd27607f5, 0xba752b3d, 0x02705e65, 0x6a7372ad, 0x7796c224, 0x1f95eeec, 0xa7909bb4, 0xcf93b77c, 0x3d5b83bd, 0x5558af75, 0xed5dda2d, 0x855ef6e5, 0x98bb466c, 0xf0b86aa4, 0x48bd1ffc, 0x20be3334, 0x73767eee, 0x1b755226, 0xa370277e, 0xcb730bb6, 0xd696bb3f, 0xbe9597f7, 0x0690e2af, 0x6e93ce67, 0xa100791b, 0xc90355d3, 0x7106208b, 0x19050c43, 0x04e0bcca, 0x6ce39002, 0xd4e6e55a, 0xbce5c992, 0xef2d8448, 0x872ea880, 0x3f2bddd8, 0x5728f110, 0x4acd4199, 0x22ce6d51, 0x9acb1809, 0xf2c834c1, 0x7ab7077a, 0x12b42bb2, 0xaab15eea, 0xc2b27222, 0xdf57c2ab, 0xb754ee63, 0x0f519b3b, 0x6752b7f3, 0x349afa29, 0x5c99d6e1, 0xe49ca3b9, 0x8c9f8f71, 0x917a3ff8, 0xf9791330, 0x417c6668, 0x297f4aa0, 0xe6ecfddc, 0x8eefd114, 0x36eaa44c, 0x5ee98884, 0x430c380d, 0x2b0f14c5, 0x930a619d, 0xfb094d55, 0xa8c1008f, 0xc0c22c47, 0x78c7591f, 0x10c475d7, 0x0d21c55e, 0x6522e996, 0xdd279cce, 0xb524b006, 0x47ec84c7, 0x2fefa80f, 0x97eadd57, 0xffe9f19f, 0xe20c4116, 0x8a0f6dde, 0x320a1886, 0x5a09344e, 0x09c17994, 0x61c2555c, 0xd9c72004, 0xb1c40ccc, 0xac21bc45, 0xc422908d, 0x7c27e5d5, 0x1424c91d, 0xdbb77e61, 0xb3b452a9, 0x0bb127f1, 0x63b20b39, 0x7e57bbb0, 0x16549778, 0xae51e220, 0xc652cee8, 0x959a8332, 0xfd99affa, 0x459cdaa2, 0x2d9ff66a, 0x307a46e3, 0x58796a2b, 0xe07c1f73, 0x887f33bb, 0xf56e0ef4, 0x9d6d223c, 0x25685764, 0x4d6b7bac, 0x508ecb25, 0x388de7ed, 0x808892b5, 0xe88bbe7d, 0xbb43f3a7, 0xd340df6f, 0x6b45aa37, 0x034686ff, 0x1ea33676, 0x76a01abe, 0xcea56fe6, 0xa6a6432e, 0x6935f452, 0x0136d89a, 0xb933adc2, 0xd130810a, 0xccd53183, 0xa4d61d4b, 0x1cd36813, 0x74d044db, 0x27180901, 0x4f1b25c9, 0xf71e5091, 0x9f1d7c59, 0x82f8ccd0, 0xeafbe018, 0x52fe9540, 0x3afdb988, 0xc8358d49, 0xa036a181, 0x1833d4d9, 0x7030f811, 0x6dd54898, 0x05d66450, 0xbdd31108, 0xd5d03dc0, 0x8618701a, 0xee1b5cd2, 0x561e298a, 0x3e1d0542, 0x23f8b5cb, 0x4bfb9903, 0xf3feec5b, 0x9bfdc093, 0x546e77ef, 0x3c6d5b27, 0x84682e7f, 0xec6b02b7, 0xf18eb23e, 0x998d9ef6, 0x2188ebae, 0x498bc766, 0x1a438abc, 0x7240a674, 0xca45d32c, 0xa246ffe4, 0xbfa34f6d, 0xd7a063a5, 0x6fa516fd, 0x07a63a35, 0x8fd9098e, 0xe7da2546, 0x5fdf501e, 0x37dc7cd6, 0x2a39cc5f, 0x423ae097, 0xfa3f95cf, 0x923cb907, 0xc1f4f4dd, 0xa9f7d815, 0x11f2ad4d, 0x79f18185, 0x6414310c, 0x0c171dc4, 0xb412689c, 0xdc114454, 0x1382f328, 0x7b81dfe0, 0xc384aab8, 0xab878670, 0xb66236f9, 0xde611a31, 0x66646f69, 0x0e6743a1, 0x5daf0e7b, 0x35ac22b3, 0x8da957eb, 0xe5aa7b23, 0xf84fcbaa, 0x904ce762, 0x2849923a, 0x404abef2, 0xb2828a33, 0xda81a6fb, 0x6284d3a3, 0x0a87ff6b, 0x17624fe2, 0x7f61632a, 0xc7641672, 0xaf673aba, 0xfcaf7760, 0x94ac5ba8, 0x2ca92ef0, 0x44aa0238, 0x594fb2b1, 0x314c9e79, 0x8949eb21, 0xe14ac7e9, 0x2ed97095, 0x46da5c5d, 0xfedf2905, 0x96dc05cd, 0x8b39b544, 0xe33a998c, 0x5b3fecd4, 0x333cc01c, 0x60f48dc6, 0x08f7a10e, 0xb0f2d456, 0xd8f1f89e, 0xc5144817, 0xad1764df, 0x15121187, 0x7d113d4f }, + { 0x00000000, 0x493c7d27, 0x9278fa4e, 0xdb448769, 0x211d826d, 0x6821ff4a, 0xb3657823, 0xfa590504, 0x423b04da, 0x0b0779fd, 0xd043fe94, 0x997f83b3, 0x632686b7, 0x2a1afb90, 0xf15e7cf9, 0xb86201de, 0x847609b4, 0xcd4a7493, 0x160ef3fa, 0x5f328edd, 0xa56b8bd9, 0xec57f6fe, 0x37137197, 0x7e2f0cb0, 0xc64d0d6e, 0x8f717049, 0x5435f720, 0x1d098a07, 0xe7508f03, 0xae6cf224, 0x7528754d, 0x3c14086a, 0x0d006599, 0x443c18be, 0x9f789fd7, 0xd644e2f0, 0x2c1de7f4, 0x65219ad3, 0xbe651dba, 0xf759609d, 0x4f3b6143, 0x06071c64, 0xdd439b0d, 0x947fe62a, 0x6e26e32e, 0x271a9e09, 0xfc5e1960, 0xb5626447, 0x89766c2d, 0xc04a110a, 0x1b0e9663, 0x5232eb44, 0xa86bee40, 0xe1579367, 0x3a13140e, 0x732f6929, 0xcb4d68f7, 0x827115d0, 0x593592b9, 0x1009ef9e, 0xea50ea9a, 0xa36c97bd, 0x782810d4, 0x31146df3, 0x1a00cb32, 0x533cb615, 0x8878317c, 0xc1444c5b, 0x3b1d495f, 0x72213478, 0xa965b311, 0xe059ce36, 0x583bcfe8, 0x1107b2cf, 0xca4335a6, 0x837f4881, 0x79264d85, 0x301a30a2, 0xeb5eb7cb, 0xa262caec, 0x9e76c286, 0xd74abfa1, 0x0c0e38c8, 0x453245ef, 0xbf6b40eb, 0xf6573dcc, 0x2d13baa5, 0x642fc782, 0xdc4dc65c, 0x9571bb7b, 0x4e353c12, 0x07094135, 0xfd504431, 0xb46c3916, 0x6f28be7f, 0x2614c358, 0x1700aeab, 0x5e3cd38c, 0x857854e5, 0xcc4429c2, 0x361d2cc6, 0x7f2151e1, 0xa465d688, 0xed59abaf, 0x553baa71, 0x1c07d756, 0xc743503f, 0x8e7f2d18, 0x7426281c, 0x3d1a553b, 0xe65ed252, 0xaf62af75, 0x9376a71f, 0xda4ada38, 0x010e5d51, 0x48322076, 0xb26b2572, 0xfb575855, 0x2013df3c, 0x692fa21b, 0xd14da3c5, 0x9871dee2, 0x4335598b, 0x0a0924ac, 0xf05021a8, 0xb96c5c8f, 0x6228dbe6, 0x2b14a6c1, 0x34019664, 0x7d3deb43, 0xa6796c2a, 0xef45110d, 0x151c1409, 0x5c20692e, 0x8764ee47, 0xce589360, 0x763a92be, 0x3f06ef99, 0xe44268f0, 0xad7e15d7, 0x572710d3, 0x1e1b6df4, 0xc55fea9d, 0x8c6397ba, 0xb0779fd0, 0xf94be2f7, 0x220f659e, 0x6b3318b9, 0x916a1dbd, 0xd856609a, 0x0312e7f3, 0x4a2e9ad4, 0xf24c9b0a, 0xbb70e62d, 0x60346144, 0x29081c63, 0xd3511967, 0x9a6d6440, 0x4129e329, 0x08159e0e, 0x3901f3fd, 0x703d8eda, 0xab7909b3, 0xe2457494, 0x181c7190, 0x51200cb7, 0x8a648bde, 0xc358f6f9, 0x7b3af727, 0x32068a00, 0xe9420d69, 0xa07e704e, 0x5a27754a, 0x131b086d, 0xc85f8f04, 0x8163f223, 0xbd77fa49, 0xf44b876e, 0x2f0f0007, 0x66337d20, 0x9c6a7824, 0xd5560503, 0x0e12826a, 0x472eff4d, 0xff4cfe93, 0xb67083b4, 0x6d3404dd, 0x240879fa, 0xde517cfe, 0x976d01d9, 0x4c2986b0, 0x0515fb97, 0x2e015d56, 0x673d2071, 0xbc79a718, 0xf545da3f, 0x0f1cdf3b, 0x4620a21c, 0x9d642575, 0xd4585852, 0x6c3a598c, 0x250624ab, 0xfe42a3c2, 0xb77edee5, 0x4d27dbe1, 0x041ba6c6, 0xdf5f21af, 0x96635c88, 0xaa7754e2, 0xe34b29c5, 0x380faeac, 0x7133d38b, 0x8b6ad68f, 0xc256aba8, 0x19122cc1, 0x502e51e6, 0xe84c5038, 0xa1702d1f, 0x7a34aa76, 0x3308d751, 0xc951d255, 0x806daf72, 0x5b29281b, 0x1215553c, 0x230138cf, 0x6a3d45e8, 0xb179c281, 0xf845bfa6, 0x021cbaa2, 0x4b20c785, 0x906440ec, 0xd9583dcb, 0x613a3c15, 0x28064132, 0xf342c65b, 0xba7ebb7c, 0x4027be78, 0x091bc35f, 0xd25f4436, 0x9b633911, 0xa777317b, 0xee4b4c5c, 0x350fcb35, 0x7c33b612, 0x866ab316, 0xcf56ce31, 0x14124958, 0x5d2e347f, 0xe54c35a1, 0xac704886, 0x7734cfef, 0x3e08b2c8, 0xc451b7cc, 0x8d6dcaeb, 0x56294d82, 0x1f1530a5 }, + { 0x00000000, 0xf43ed648, 0xed91da61, 0x19af0c29, 0xdecfc233, 0x2af1147b, 0x335e1852, 0xc760ce1a, 0xb873f297, 0x4c4d24df, 0x55e228f6, 0xa1dcfebe, 0x66bc30a4, 0x9282e6ec, 0x8b2deac5, 0x7f133c8d, 0x750b93df, 0x81354597, 0x989a49be, 0x6ca49ff6, 0xabc451ec, 0x5ffa87a4, 0x46558b8d, 0xb26b5dc5, 0xcd786148, 0x3946b700, 0x20e9bb29, 0xd4d76d61, 0x13b7a37b, 0xe7897533, 0xfe26791a, 0x0a18af52, 0xea1727be, 0x1e29f1f6, 0x0786fddf, 0xf3b82b97, 0x34d8e58d, 0xc0e633c5, 0xd9493fec, 0x2d77e9a4, 0x5264d529, 0xa65a0361, 0xbff50f48, 0x4bcbd900, 0x8cab171a, 0x7895c152, 0x613acd7b, 0x95041b33, 0x9f1cb461, 0x6b226229, 0x728d6e00, 0x86b3b848, 0x41d37652, 0xb5eda01a, 0xac42ac33, 0x587c7a7b, 0x276f46f6, 0xd35190be, 0xcafe9c97, 0x3ec04adf, 0xf9a084c5, 0x0d9e528d, 0x14315ea4, 0xe00f88ec, 0xd1c2398d, 0x25fcefc5, 0x3c53e3ec, 0xc86d35a4, 0x0f0dfbbe, 0xfb332df6, 0xe29c21df, 0x16a2f797, 0x69b1cb1a, 0x9d8f1d52, 0x8420117b, 0x701ec733, 0xb77e0929, 0x4340df61, 0x5aefd348, 0xaed10500, 0xa4c9aa52, 0x50f77c1a, 0x49587033, 0xbd66a67b, 0x7a066861, 0x8e38be29, 0x9797b200, 0x63a96448, 0x1cba58c5, 0xe8848e8d, 0xf12b82a4, 0x051554ec, 0xc2759af6, 0x364b4cbe, 0x2fe44097, 0xdbda96df, 0x3bd51e33, 0xcfebc87b, 0xd644c452, 0x227a121a, 0xe51adc00, 0x11240a48, 0x088b0661, 0xfcb5d029, 0x83a6eca4, 0x77983aec, 0x6e3736c5, 0x9a09e08d, 0x5d692e97, 0xa957f8df, 0xb0f8f4f6, 0x44c622be, 0x4ede8dec, 0xbae05ba4, 0xa34f578d, 0x577181c5, 0x90114fdf, 0x642f9997, 0x7d8095be, 0x89be43f6, 0xf6ad7f7b, 0x0293a933, 0x1b3ca51a, 0xef027352, 0x2862bd48, 0xdc5c6b00, 0xc5f36729, 0x31cdb161, 0xa66805eb, 0x5256d3a3, 0x4bf9df8a, 0xbfc709c2, 0x78a7c7d8, 0x8c991190, 0x95361db9, 0x6108cbf1, 0x1e1bf77c, 0xea252134, 0xf38a2d1d, 0x07b4fb55, 0xc0d4354f, 0x34eae307, 0x2d45ef2e, 0xd97b3966, 0xd3639634, 0x275d407c, 0x3ef24c55, 0xcacc9a1d, 0x0dac5407, 0xf992824f, 0xe03d8e66, 0x1403582e, 0x6b1064a3, 0x9f2eb2eb, 0x8681bec2, 0x72bf688a, 0xb5dfa690, 0x41e170d8, 0x584e7cf1, 0xac70aab9, 0x4c7f2255, 0xb841f41d, 0xa1eef834, 0x55d02e7c, 0x92b0e066, 0x668e362e, 0x7f213a07, 0x8b1fec4f, 0xf40cd0c2, 0x0032068a, 0x199d0aa3, 0xeda3dceb, 0x2ac312f1, 0xdefdc4b9, 0xc752c890, 0x336c1ed8, 0x3974b18a, 0xcd4a67c2, 0xd4e56beb, 0x20dbbda3, 0xe7bb73b9, 0x1385a5f1, 0x0a2aa9d8, 0xfe147f90, 0x8107431d, 0x75399555, 0x6c96997c, 0x98a84f34, 0x5fc8812e, 0xabf65766, 0xb2595b4f, 0x46678d07, 0x77aa3c66, 0x8394ea2e, 0x9a3be607, 0x6e05304f, 0xa965fe55, 0x5d5b281d, 0x44f42434, 0xb0caf27c, 0xcfd9cef1, 0x3be718b9, 0x22481490, 0xd676c2d8, 0x11160cc2, 0xe528da8a, 0xfc87d6a3, 0x08b900eb, 0x02a1afb9, 0xf69f79f1, 0xef3075d8, 0x1b0ea390, 0xdc6e6d8a, 0x2850bbc2, 0x31ffb7eb, 0xc5c161a3, 0xbad25d2e, 0x4eec8b66, 0x5743874f, 0xa37d5107, 0x641d9f1d, 0x90234955, 0x898c457c, 0x7db29334, 0x9dbd1bd8, 0x6983cd90, 0x702cc1b9, 0x841217f1, 0x4372d9eb, 0xb74c0fa3, 0xaee3038a, 0x5addd5c2, 0x25cee94f, 0xd1f03f07, 0xc85f332e, 0x3c61e566, 0xfb012b7c, 0x0f3ffd34, 0x1690f11d, 0xe2ae2755, 0xe8b68807, 0x1c885e4f, 0x05275266, 0xf119842e, 0x36794a34, 0xc2479c7c, 0xdbe89055, 0x2fd6461d, 0x50c57a90, 0xa4fbacd8, 0xbd54a0f1, 0x496a76b9, 0x8e0ab8a3, 0x7a346eeb, 0x639b62c2, 0x97a5b48a }, + { 0x00000000, 0xcb567ba5, 0x934081bb, 0x5816fa1e, 0x236d7587, 0xe83b0e22, 0xb02df43c, 0x7b7b8f99, 0x46daeb0e, 0x8d8c90ab, 0xd59a6ab5, 0x1ecc1110, 0x65b79e89, 0xaee1e52c, 0xf6f71f32, 0x3da16497, 0x8db5d61c, 0x46e3adb9, 0x1ef557a7, 0xd5a32c02, 0xaed8a39b, 0x658ed83e, 0x3d982220, 0xf6ce5985, 0xcb6f3d12, 0x003946b7, 0x582fbca9, 0x9379c70c, 0xe8024895, 0x23543330, 0x7b42c92e, 0xb014b28b, 0x1e87dac9, 0xd5d1a16c, 0x8dc75b72, 0x469120d7, 0x3deaaf4e, 0xf6bcd4eb, 0xaeaa2ef5, 0x65fc5550, 0x585d31c7, 0x930b4a62, 0xcb1db07c, 0x004bcbd9, 0x7b304440, 0xb0663fe5, 0xe870c5fb, 0x2326be5e, 0x93320cd5, 0x58647770, 0x00728d6e, 0xcb24f6cb, 0xb05f7952, 0x7b0902f7, 0x231ff8e9, 0xe849834c, 0xd5e8e7db, 0x1ebe9c7e, 0x46a86660, 0x8dfe1dc5, 0xf685925c, 0x3dd3e9f9, 0x65c513e7, 0xae936842, 0x3d0fb592, 0xf659ce37, 0xae4f3429, 0x65194f8c, 0x1e62c015, 0xd534bbb0, 0x8d2241ae, 0x46743a0b, 0x7bd55e9c, 0xb0832539, 0xe895df27, 0x23c3a482, 0x58b82b1b, 0x93ee50be, 0xcbf8aaa0, 0x00aed105, 0xb0ba638e, 0x7bec182b, 0x23fae235, 0xe8ac9990, 0x93d71609, 0x58816dac, 0x009797b2, 0xcbc1ec17, 0xf6608880, 0x3d36f325, 0x6520093b, 0xae76729e, 0xd50dfd07, 0x1e5b86a2, 0x464d7cbc, 0x8d1b0719, 0x23886f5b, 0xe8de14fe, 0xb0c8eee0, 0x7b9e9545, 0x00e51adc, 0xcbb36179, 0x93a59b67, 0x58f3e0c2, 0x65528455, 0xae04fff0, 0xf61205ee, 0x3d447e4b, 0x463ff1d2, 0x8d698a77, 0xd57f7069, 0x1e290bcc, 0xae3db947, 0x656bc2e2, 0x3d7d38fc, 0xf62b4359, 0x8d50ccc0, 0x4606b765, 0x1e104d7b, 0xd54636de, 0xe8e75249, 0x23b129ec, 0x7ba7d3f2, 0xb0f1a857, 0xcb8a27ce, 0x00dc5c6b, 0x58caa675, 0x939cddd0, 0x7a1f6b24, 0xb1491081, 0xe95fea9f, 0x2209913a, 0x59721ea3, 0x92246506, 0xca329f18, 0x0164e4bd, 0x3cc5802a, 0xf793fb8f, 0xaf850191, 0x64d37a34, 0x1fa8f5ad, 0xd4fe8e08, 0x8ce87416, 0x47be0fb3, 0xf7aabd38, 0x3cfcc69d, 0x64ea3c83, 0xafbc4726, 0xd4c7c8bf, 0x1f91b31a, 0x47874904, 0x8cd132a1, 0xb1705636, 0x7a262d93, 0x2230d78d, 0xe966ac28, 0x921d23b1, 0x594b5814, 0x015da20a, 0xca0bd9af, 0x6498b1ed, 0xafceca48, 0xf7d83056, 0x3c8e4bf3, 0x47f5c46a, 0x8ca3bfcf, 0xd4b545d1, 0x1fe33e74, 0x22425ae3, 0xe9142146, 0xb102db58, 0x7a54a0fd, 0x012f2f64, 0xca7954c1, 0x926faedf, 0x5939d57a, 0xe92d67f1, 0x227b1c54, 0x7a6de64a, 0xb13b9def, 0xca401276, 0x011669d3, 0x590093cd, 0x9256e868, 0xaff78cff, 0x64a1f75a, 0x3cb70d44, 0xf7e176e1, 0x8c9af978, 0x47cc82dd, 0x1fda78c3, 0xd48c0366, 0x4710deb6, 0x8c46a513, 0xd4505f0d, 0x1f0624a8, 0x647dab31, 0xaf2bd094, 0xf73d2a8a, 0x3c6b512f, 0x01ca35b8, 0xca9c4e1d, 0x928ab403, 0x59dccfa6, 0x22a7403f, 0xe9f13b9a, 0xb1e7c184, 0x7ab1ba21, 0xcaa508aa, 0x01f3730f, 0x59e58911, 0x92b3f2b4, 0xe9c87d2d, 0x229e0688, 0x7a88fc96, 0xb1de8733, 0x8c7fe3a4, 0x47299801, 0x1f3f621f, 0xd46919ba, 0xaf129623, 0x6444ed86, 0x3c521798, 0xf7046c3d, 0x5997047f, 0x92c17fda, 0xcad785c4, 0x0181fe61, 0x7afa71f8, 0xb1ac0a5d, 0xe9baf043, 0x22ec8be6, 0x1f4def71, 0xd41b94d4, 0x8c0d6eca, 0x475b156f, 0x3c209af6, 0xf776e153, 0xaf601b4d, 0x643660e8, 0xd422d263, 0x1f74a9c6, 0x476253d8, 0x8c34287d, 0xf74fa7e4, 0x3c19dc41, 0x640f265f, 0xaf595dfa, 0x92f8396d, 0x59ae42c8, 0x01b8b8d6, 0xcaeec373, 0xb1954cea, 0x7ac3374f, 0x22d5cd51, 0xe983b6f4 }, + { 0x00000000, 0x9771f7c1, 0x2b0f9973, 0xbc7e6eb2, 0x561f32e6, 0xc16ec527, 0x7d10ab95, 0xea615c54, 0xac3e65cc, 0x3b4f920d, 0x8731fcbf, 0x10400b7e, 0xfa21572a, 0x6d50a0eb, 0xd12ece59, 0x465f3998, 0x5d90bd69, 0xcae14aa8, 0x769f241a, 0xe1eed3db, 0x0b8f8f8f, 0x9cfe784e, 0x208016fc, 0xb7f1e13d, 0xf1aed8a5, 0x66df2f64, 0xdaa141d6, 0x4dd0b617, 0xa7b1ea43, 0x30c01d82, 0x8cbe7330, 0x1bcf84f1, 0xbb217ad2, 0x2c508d13, 0x902ee3a1, 0x075f1460, 0xed3e4834, 0x7a4fbff5, 0xc631d147, 0x51402686, 0x171f1f1e, 0x806ee8df, 0x3c10866d, 0xab6171ac, 0x41002df8, 0xd671da39, 0x6a0fb48b, 0xfd7e434a, 0xe6b1c7bb, 0x71c0307a, 0xcdbe5ec8, 0x5acfa909, 0xb0aef55d, 0x27df029c, 0x9ba16c2e, 0x0cd09bef, 0x4a8fa277, 0xddfe55b6, 0x61803b04, 0xf6f1ccc5, 0x1c909091, 0x8be16750, 0x379f09e2, 0xa0eefe23, 0x73ae8355, 0xe4df7494, 0x58a11a26, 0xcfd0ede7, 0x25b1b1b3, 0xb2c04672, 0x0ebe28c0, 0x99cfdf01, 0xdf90e699, 0x48e11158, 0xf49f7fea, 0x63ee882b, 0x898fd47f, 0x1efe23be, 0xa2804d0c, 0x35f1bacd, 0x2e3e3e3c, 0xb94fc9fd, 0x0531a74f, 0x9240508e, 0x78210cda, 0xef50fb1b, 0x532e95a9, 0xc45f6268, 0x82005bf0, 0x1571ac31, 0xa90fc283, 0x3e7e3542, 0xd41f6916, 0x436e9ed7, 0xff10f065, 0x686107a4, 0xc88ff987, 0x5ffe0e46, 0xe38060f4, 0x74f19735, 0x9e90cb61, 0x09e13ca0, 0xb59f5212, 0x22eea5d3, 0x64b19c4b, 0xf3c06b8a, 0x4fbe0538, 0xd8cff2f9, 0x32aeaead, 0xa5df596c, 0x19a137de, 0x8ed0c01f, 0x951f44ee, 0x026eb32f, 0xbe10dd9d, 0x29612a5c, 0xc3007608, 0x547181c9, 0xe80fef7b, 0x7f7e18ba, 0x39212122, 0xae50d6e3, 0x122eb851, 0x855f4f90, 0x6f3e13c4, 0xf84fe405, 0x44318ab7, 0xd3407d76, 0xe75d06aa, 0x702cf16b, 0xcc529fd9, 0x5b236818, 0xb142344c, 0x2633c38d, 0x9a4dad3f, 0x0d3c5afe, 0x4b636366, 0xdc1294a7, 0x606cfa15, 0xf71d0dd4, 0x1d7c5180, 0x8a0da641, 0x3673c8f3, 0xa1023f32, 0xbacdbbc3, 0x2dbc4c02, 0x91c222b0, 0x06b3d571, 0xecd28925, 0x7ba37ee4, 0xc7dd1056, 0x50ace797, 0x16f3de0f, 0x818229ce, 0x3dfc477c, 0xaa8db0bd, 0x40ecece9, 0xd79d1b28, 0x6be3759a, 0xfc92825b, 0x5c7c7c78, 0xcb0d8bb9, 0x7773e50b, 0xe00212ca, 0x0a634e9e, 0x9d12b95f, 0x216cd7ed, 0xb61d202c, 0xf04219b4, 0x6733ee75, 0xdb4d80c7, 0x4c3c7706, 0xa65d2b52, 0x312cdc93, 0x8d52b221, 0x1a2345e0, 0x01ecc111, 0x969d36d0, 0x2ae35862, 0xbd92afa3, 0x57f3f3f7, 0xc0820436, 0x7cfc6a84, 0xeb8d9d45, 0xadd2a4dd, 0x3aa3531c, 0x86dd3dae, 0x11acca6f, 0xfbcd963b, 0x6cbc61fa, 0xd0c20f48, 0x47b3f889, 0x94f385ff, 0x0382723e, 0xbffc1c8c, 0x288deb4d, 0xc2ecb719, 0x559d40d8, 0xe9e32e6a, 0x7e92d9ab, 0x38cde033, 0xafbc17f2, 0x13c27940, 0x84b38e81, 0x6ed2d2d5, 0xf9a32514, 0x45dd4ba6, 0xd2acbc67, 0xc9633896, 0x5e12cf57, 0xe26ca1e5, 0x751d5624, 0x9f7c0a70, 0x080dfdb1, 0xb4739303, 0x230264c2, 0x655d5d5a, 0xf22caa9b, 0x4e52c429, 0xd92333e8, 0x33426fbc, 0xa433987d, 0x184df6cf, 0x8f3c010e, 0x2fd2ff2d, 0xb8a308ec, 0x04dd665e, 0x93ac919f, 0x79cdcdcb, 0xeebc3a0a, 0x52c254b8, 0xc5b3a379, 0x83ec9ae1, 0x149d6d20, 0xa8e30392, 0x3f92f453, 0xd5f3a807, 0x42825fc6, 0xfefc3174, 0x698dc6b5, 0x72424244, 0xe533b585, 0x594ddb37, 0xce3c2cf6, 0x245d70a2, 0xb32c8763, 0x0f52e9d1, 0x98231e10, 0xde7c2788, 0x490dd049, 0xf573befb, 0x6202493a, 0x8863156e, 0x1f12e2af, 0xa36c8c1d, 0x341d7bdc }, + { 0x00000000, 0x3171d430, 0x62e3a860, 0x53927c50, 0xc5c750c0, 0xf4b684f0, 0xa724f8a0, 0x96552c90, 0x8e62d771, 0xbf130341, 0xec817f11, 0xddf0ab21, 0x4ba587b1, 0x7ad45381, 0x29462fd1, 0x1837fbe1, 0x1929d813, 0x28580c23, 0x7bca7073, 0x4abba443, 0xdcee88d3, 0xed9f5ce3, 0xbe0d20b3, 0x8f7cf483, 0x974b0f62, 0xa63adb52, 0xf5a8a702, 0xc4d97332, 0x528c5fa2, 0x63fd8b92, 0x306ff7c2, 0x011e23f2, 0x3253b026, 0x03226416, 0x50b01846, 0x61c1cc76, 0xf794e0e6, 0xc6e534d6, 0x95774886, 0xa4069cb6, 0xbc316757, 0x8d40b367, 0xded2cf37, 0xefa31b07, 0x79f63797, 0x4887e3a7, 0x1b159ff7, 0x2a644bc7, 0x2b7a6835, 0x1a0bbc05, 0x4999c055, 0x78e81465, 0xeebd38f5, 0xdfccecc5, 0x8c5e9095, 0xbd2f44a5, 0xa518bf44, 0x94696b74, 0xc7fb1724, 0xf68ac314, 0x60dfef84, 0x51ae3bb4, 0x023c47e4, 0x334d93d4, 0x64a7604c, 0x55d6b47c, 0x0644c82c, 0x37351c1c, 0xa160308c, 0x9011e4bc, 0xc38398ec, 0xf2f24cdc, 0xeac5b73d, 0xdbb4630d, 0x88261f5d, 0xb957cb6d, 0x2f02e7fd, 0x1e7333cd, 0x4de14f9d, 0x7c909bad, 0x7d8eb85f, 0x4cff6c6f, 0x1f6d103f, 0x2e1cc40f, 0xb849e89f, 0x89383caf, 0xdaaa40ff, 0xebdb94cf, 0xf3ec6f2e, 0xc29dbb1e, 0x910fc74e, 0xa07e137e, 0x362b3fee, 0x075aebde, 0x54c8978e, 0x65b943be, 0x56f4d06a, 0x6785045a, 0x3417780a, 0x0566ac3a, 0x933380aa, 0xa242549a, 0xf1d028ca, 0xc0a1fcfa, 0xd896071b, 0xe9e7d32b, 0xba75af7b, 0x8b047b4b, 0x1d5157db, 0x2c2083eb, 0x7fb2ffbb, 0x4ec32b8b, 0x4fdd0879, 0x7eacdc49, 0x2d3ea019, 0x1c4f7429, 0x8a1a58b9, 0xbb6b8c89, 0xe8f9f0d9, 0xd98824e9, 0xc1bfdf08, 0xf0ce0b38, 0xa35c7768, 0x922da358, 0x04788fc8, 0x35095bf8, 0x669b27a8, 0x57eaf398, 0xc94ec098, 0xf83f14a8, 0xabad68f8, 0x9adcbcc8, 0x0c899058, 0x3df84468, 0x6e6a3838, 0x5f1bec08, 0x472c17e9, 0x765dc3d9, 0x25cfbf89, 0x14be6bb9, 0x82eb4729, 0xb39a9319, 0xe008ef49, 0xd1793b79, 0xd067188b, 0xe116ccbb, 0xb284b0eb, 0x83f564db, 0x15a0484b, 0x24d19c7b, 0x7743e02b, 0x4632341b, 0x5e05cffa, 0x6f741bca, 0x3ce6679a, 0x0d97b3aa, 0x9bc29f3a, 0xaab34b0a, 0xf921375a, 0xc850e36a, 0xfb1d70be, 0xca6ca48e, 0x99fed8de, 0xa88f0cee, 0x3eda207e, 0x0fabf44e, 0x5c39881e, 0x6d485c2e, 0x757fa7cf, 0x440e73ff, 0x179c0faf, 0x26eddb9f, 0xb0b8f70f, 0x81c9233f, 0xd25b5f6f, 0xe32a8b5f, 0xe234a8ad, 0xd3457c9d, 0x80d700cd, 0xb1a6d4fd, 0x27f3f86d, 0x16822c5d, 0x4510500d, 0x7461843d, 0x6c567fdc, 0x5d27abec, 0x0eb5d7bc, 0x3fc4038c, 0xa9912f1c, 0x98e0fb2c, 0xcb72877c, 0xfa03534c, 0xade9a0d4, 0x9c9874e4, 0xcf0a08b4, 0xfe7bdc84, 0x682ef014, 0x595f2424, 0x0acd5874, 0x3bbc8c44, 0x238b77a5, 0x12faa395, 0x4168dfc5, 0x70190bf5, 0xe64c2765, 0xd73df355, 0x84af8f05, 0xb5de5b35, 0xb4c078c7, 0x85b1acf7, 0xd623d0a7, 0xe7520497, 0x71072807, 0x4076fc37, 0x13e48067, 0x22955457, 0x3aa2afb6, 0x0bd37b86, 0x584107d6, 0x6930d3e6, 0xff65ff76, 0xce142b46, 0x9d865716, 0xacf78326, 0x9fba10f2, 0xaecbc4c2, 0xfd59b892, 0xcc286ca2, 0x5a7d4032, 0x6b0c9402, 0x389ee852, 0x09ef3c62, 0x11d8c783, 0x20a913b3, 0x733b6fe3, 0x424abbd3, 0xd41f9743, 0xe56e4373, 0xb6fc3f23, 0x878deb13, 0x8693c8e1, 0xb7e21cd1, 0xe4706081, 0xd501b4b1, 0x43549821, 0x72254c11, 0x21b73041, 0x10c6e471, 0x08f11f90, 0x3980cba0, 0x6a12b7f0, 0x5b6363c0, 0xcd364f50, 0xfc479b60, 0xafd5e730, 0x9ea43300 }, + { 0x00000000, 0x30d23865, 0x61a470ca, 0x517648af, 0xc348e194, 0xf39ad9f1, 0xa2ec915e, 0x923ea93b, 0x837db5d9, 0xb3af8dbc, 0xe2d9c513, 0xd20bfd76, 0x4035544d, 0x70e76c28, 0x21912487, 0x11431ce2, 0x03171d43, 0x33c52526, 0x62b36d89, 0x526155ec, 0xc05ffcd7, 0xf08dc4b2, 0xa1fb8c1d, 0x9129b478, 0x806aa89a, 0xb0b890ff, 0xe1ced850, 0xd11ce035, 0x4322490e, 0x73f0716b, 0x228639c4, 0x125401a1, 0x062e3a86, 0x36fc02e3, 0x678a4a4c, 0x57587229, 0xc566db12, 0xf5b4e377, 0xa4c2abd8, 0x941093bd, 0x85538f5f, 0xb581b73a, 0xe4f7ff95, 0xd425c7f0, 0x461b6ecb, 0x76c956ae, 0x27bf1e01, 0x176d2664, 0x053927c5, 0x35eb1fa0, 0x649d570f, 0x544f6f6a, 0xc671c651, 0xf6a3fe34, 0xa7d5b69b, 0x97078efe, 0x8644921c, 0xb696aa79, 0xe7e0e2d6, 0xd732dab3, 0x450c7388, 0x75de4bed, 0x24a80342, 0x147a3b27, 0x0c5c750c, 0x3c8e4d69, 0x6df805c6, 0x5d2a3da3, 0xcf149498, 0xffc6acfd, 0xaeb0e452, 0x9e62dc37, 0x8f21c0d5, 0xbff3f8b0, 0xee85b01f, 0xde57887a, 0x4c692141, 0x7cbb1924, 0x2dcd518b, 0x1d1f69ee, 0x0f4b684f, 0x3f99502a, 0x6eef1885, 0x5e3d20e0, 0xcc0389db, 0xfcd1b1be, 0xada7f911, 0x9d75c174, 0x8c36dd96, 0xbce4e5f3, 0xed92ad5c, 0xdd409539, 0x4f7e3c02, 0x7fac0467, 0x2eda4cc8, 0x1e0874ad, 0x0a724f8a, 0x3aa077ef, 0x6bd63f40, 0x5b040725, 0xc93aae1e, 0xf9e8967b, 0xa89eded4, 0x984ce6b1, 0x890ffa53, 0xb9ddc236, 0xe8ab8a99, 0xd879b2fc, 0x4a471bc7, 0x7a9523a2, 0x2be36b0d, 0x1b315368, 0x096552c9, 0x39b76aac, 0x68c12203, 0x58131a66, 0xca2db35d, 0xfaff8b38, 0xab89c397, 0x9b5bfbf2, 0x8a18e710, 0xbacadf75, 0xebbc97da, 0xdb6eafbf, 0x49500684, 0x79823ee1, 0x28f4764e, 0x18264e2b, 0x18b8ea18, 0x286ad27d, 0x791c9ad2, 0x49cea2b7, 0xdbf00b8c, 0xeb2233e9, 0xba547b46, 0x8a864323, 0x9bc55fc1, 0xab1767a4, 0xfa612f0b, 0xcab3176e, 0x588dbe55, 0x685f8630, 0x3929ce9f, 0x09fbf6fa, 0x1baff75b, 0x2b7dcf3e, 0x7a0b8791, 0x4ad9bff4, 0xd8e716cf, 0xe8352eaa, 0xb9436605, 0x89915e60, 0x98d24282, 0xa8007ae7, 0xf9763248, 0xc9a40a2d, 0x5b9aa316, 0x6b489b73, 0x3a3ed3dc, 0x0aecebb9, 0x1e96d09e, 0x2e44e8fb, 0x7f32a054, 0x4fe09831, 0xddde310a, 0xed0c096f, 0xbc7a41c0, 0x8ca879a5, 0x9deb6547, 0xad395d22, 0xfc4f158d, 0xcc9d2de8, 0x5ea384d3, 0x6e71bcb6, 0x3f07f419, 0x0fd5cc7c, 0x1d81cddd, 0x2d53f5b8, 0x7c25bd17, 0x4cf78572, 0xdec92c49, 0xee1b142c, 0xbf6d5c83, 0x8fbf64e6, 0x9efc7804, 0xae2e4061, 0xff5808ce, 0xcf8a30ab, 0x5db49990, 0x6d66a1f5, 0x3c10e95a, 0x0cc2d13f, 0x14e49f14, 0x2436a771, 0x7540efde, 0x4592d7bb, 0xd7ac7e80, 0xe77e46e5, 0xb6080e4a, 0x86da362f, 0x97992acd, 0xa74b12a8, 0xf63d5a07, 0xc6ef6262, 0x54d1cb59, 0x6403f33c, 0x3575bb93, 0x05a783f6, 0x17f38257, 0x2721ba32, 0x7657f29d, 0x4685caf8, 0xd4bb63c3, 0xe4695ba6, 0xb51f1309, 0x85cd2b6c, 0x948e378e, 0xa45c0feb, 0xf52a4744, 0xc5f87f21, 0x57c6d61a, 0x6714ee7f, 0x3662a6d0, 0x06b09eb5, 0x12caa592, 0x22189df7, 0x736ed558, 0x43bced3d, 0xd1824406, 0xe1507c63, 0xb02634cc, 0x80f40ca9, 0x91b7104b, 0xa165282e, 0xf0136081, 0xc0c158e4, 0x52fff1df, 0x622dc9ba, 0x335b8115, 0x0389b970, 0x11ddb8d1, 0x210f80b4, 0x7079c81b, 0x40abf07e, 0xd2955945, 0xe2476120, 0xb331298f, 0x83e311ea, 0x92a00d08, 0xa272356d, 0xf3047dc2, 0xc3d645a7, 0x51e8ec9c, 0x613ad4f9, 0x304c9c56, 0x009ea433 }, + { 0x00000000, 0x54075546, 0xa80eaa8c, 0xfc09ffca, 0x55f123e9, 0x01f676af, 0xfdff8965, 0xa9f8dc23, 0xabe247d2, 0xffe51294, 0x03eced5e, 0x57ebb818, 0xfe13643b, 0xaa14317d, 0x561dceb7, 0x021a9bf1, 0x5228f955, 0x062fac13, 0xfa2653d9, 0xae21069f, 0x07d9dabc, 0x53de8ffa, 0xafd77030, 0xfbd02576, 0xf9cabe87, 0xadcdebc1, 0x51c4140b, 0x05c3414d, 0xac3b9d6e, 0xf83cc828, 0x043537e2, 0x503262a4, 0xa451f2aa, 0xf056a7ec, 0x0c5f5826, 0x58580d60, 0xf1a0d143, 0xa5a78405, 0x59ae7bcf, 0x0da92e89, 0x0fb3b578, 0x5bb4e03e, 0xa7bd1ff4, 0xf3ba4ab2, 0x5a429691, 0x0e45c3d7, 0xf24c3c1d, 0xa64b695b, 0xf6790bff, 0xa27e5eb9, 0x5e77a173, 0x0a70f435, 0xa3882816, 0xf78f7d50, 0x0b86829a, 0x5f81d7dc, 0x5d9b4c2d, 0x099c196b, 0xf595e6a1, 0xa192b3e7, 0x086a6fc4, 0x5c6d3a82, 0xa064c548, 0xf463900e, 0x4d4f93a5, 0x1948c6e3, 0xe5413929, 0xb1466c6f, 0x18beb04c, 0x4cb9e50a, 0xb0b01ac0, 0xe4b74f86, 0xe6add477, 0xb2aa8131, 0x4ea37efb, 0x1aa42bbd, 0xb35cf79e, 0xe75ba2d8, 0x1b525d12, 0x4f550854, 0x1f676af0, 0x4b603fb6, 0xb769c07c, 0xe36e953a, 0x4a964919, 0x1e911c5f, 0xe298e395, 0xb69fb6d3, 0xb4852d22, 0xe0827864, 0x1c8b87ae, 0x488cd2e8, 0xe1740ecb, 0xb5735b8d, 0x497aa447, 0x1d7df101, 0xe91e610f, 0xbd193449, 0x4110cb83, 0x15179ec5, 0xbcef42e6, 0xe8e817a0, 0x14e1e86a, 0x40e6bd2c, 0x42fc26dd, 0x16fb739b, 0xeaf28c51, 0xbef5d917, 0x170d0534, 0x430a5072, 0xbf03afb8, 0xeb04fafe, 0xbb36985a, 0xef31cd1c, 0x133832d6, 0x473f6790, 0xeec7bbb3, 0xbac0eef5, 0x46c9113f, 0x12ce4479, 0x10d4df88, 0x44d38ace, 0xb8da7504, 0xecdd2042, 0x4525fc61, 0x1122a927, 0xed2b56ed, 0xb92c03ab, 0x9a9f274a, 0xce98720c, 0x32918dc6, 0x6696d880, 0xcf6e04a3, 0x9b6951e5, 0x6760ae2f, 0x3367fb69, 0x317d6098, 0x657a35de, 0x9973ca14, 0xcd749f52, 0x648c4371, 0x308b1637, 0xcc82e9fd, 0x9885bcbb, 0xc8b7de1f, 0x9cb08b59, 0x60b97493, 0x34be21d5, 0x9d46fdf6, 0xc941a8b0, 0x3548577a, 0x614f023c, 0x635599cd, 0x3752cc8b, 0xcb5b3341, 0x9f5c6607, 0x36a4ba24, 0x62a3ef62, 0x9eaa10a8, 0xcaad45ee, 0x3eced5e0, 0x6ac980a6, 0x96c07f6c, 0xc2c72a2a, 0x6b3ff609, 0x3f38a34f, 0xc3315c85, 0x973609c3, 0x952c9232, 0xc12bc774, 0x3d2238be, 0x69256df8, 0xc0ddb1db, 0x94dae49d, 0x68d31b57, 0x3cd44e11, 0x6ce62cb5, 0x38e179f3, 0xc4e88639, 0x90efd37f, 0x39170f5c, 0x6d105a1a, 0x9119a5d0, 0xc51ef096, 0xc7046b67, 0x93033e21, 0x6f0ac1eb, 0x3b0d94ad, 0x92f5488e, 0xc6f21dc8, 0x3afbe202, 0x6efcb744, 0xd7d0b4ef, 0x83d7e1a9, 0x7fde1e63, 0x2bd94b25, 0x82219706, 0xd626c240, 0x2a2f3d8a, 0x7e2868cc, 0x7c32f33d, 0x2835a67b, 0xd43c59b1, 0x803b0cf7, 0x29c3d0d4, 0x7dc48592, 0x81cd7a58, 0xd5ca2f1e, 0x85f84dba, 0xd1ff18fc, 0x2df6e736, 0x79f1b270, 0xd0096e53, 0x840e3b15, 0x7807c4df, 0x2c009199, 0x2e1a0a68, 0x7a1d5f2e, 0x8614a0e4, 0xd213f5a2, 0x7beb2981, 0x2fec7cc7, 0xd3e5830d, 0x87e2d64b, 0x73814645, 0x27861303, 0xdb8fecc9, 0x8f88b98f, 0x267065ac, 0x727730ea, 0x8e7ecf20, 0xda799a66, 0xd8630197, 0x8c6454d1, 0x706dab1b, 0x246afe5d, 0x8d92227e, 0xd9957738, 0x259c88f2, 0x719bddb4, 0x21a9bf10, 0x75aeea56, 0x89a7159c, 0xdda040da, 0x74589cf9, 0x205fc9bf, 0xdc563675, 0x88516333, 0x8a4bf8c2, 0xde4cad84, 0x2245524e, 0x76420708, 0xdfbadb2b, 0x8bbd8e6d, 0x77b471a7, 0x23b324e1 }, + { 0x00000000, 0x678efd01, 0xcf1dfa02, 0xa8930703, 0x9bd782f5, 0xfc597ff4, 0x54ca78f7, 0x334485f6, 0x3243731b, 0x55cd8e1a, 0xfd5e8919, 0x9ad07418, 0xa994f1ee, 0xce1a0cef, 0x66890bec, 0x0107f6ed, 0x6486e636, 0x03081b37, 0xab9b1c34, 0xcc15e135, 0xff5164c3, 0x98df99c2, 0x304c9ec1, 0x57c263c0, 0x56c5952d, 0x314b682c, 0x99d86f2f, 0xfe56922e, 0xcd1217d8, 0xaa9cead9, 0x020fedda, 0x658110db, 0xc90dcc6c, 0xae83316d, 0x0610366e, 0x619ecb6f, 0x52da4e99, 0x3554b398, 0x9dc7b49b, 0xfa49499a, 0xfb4ebf77, 0x9cc04276, 0x34534575, 0x53ddb874, 0x60993d82, 0x0717c083, 0xaf84c780, 0xc80a3a81, 0xad8b2a5a, 0xca05d75b, 0x6296d058, 0x05182d59, 0x365ca8af, 0x51d255ae, 0xf94152ad, 0x9ecfafac, 0x9fc85941, 0xf846a440, 0x50d5a343, 0x375b5e42, 0x041fdbb4, 0x639126b5, 0xcb0221b6, 0xac8cdcb7, 0x97f7ee29, 0xf0791328, 0x58ea142b, 0x3f64e92a, 0x0c206cdc, 0x6bae91dd, 0xc33d96de, 0xa4b36bdf, 0xa5b49d32, 0xc23a6033, 0x6aa96730, 0x0d279a31, 0x3e631fc7, 0x59ede2c6, 0xf17ee5c5, 0x96f018c4, 0xf371081f, 0x94fff51e, 0x3c6cf21d, 0x5be20f1c, 0x68a68aea, 0x0f2877eb, 0xa7bb70e8, 0xc0358de9, 0xc1327b04, 0xa6bc8605, 0x0e2f8106, 0x69a17c07, 0x5ae5f9f1, 0x3d6b04f0, 0x95f803f3, 0xf276fef2, 0x5efa2245, 0x3974df44, 0x91e7d847, 0xf6692546, 0xc52da0b0, 0xa2a35db1, 0x0a305ab2, 0x6dbea7b3, 0x6cb9515e, 0x0b37ac5f, 0xa3a4ab5c, 0xc42a565d, 0xf76ed3ab, 0x90e02eaa, 0x387329a9, 0x5ffdd4a8, 0x3a7cc473, 0x5df23972, 0xf5613e71, 0x92efc370, 0xa1ab4686, 0xc625bb87, 0x6eb6bc84, 0x09384185, 0x083fb768, 0x6fb14a69, 0xc7224d6a, 0xa0acb06b, 0x93e8359d, 0xf466c89c, 0x5cf5cf9f, 0x3b7b329e, 0x2a03aaa3, 0x4d8d57a2, 0xe51e50a1, 0x8290ada0, 0xb1d42856, 0xd65ad557, 0x7ec9d254, 0x19472f55, 0x1840d9b8, 0x7fce24b9, 0xd75d23ba, 0xb0d3debb, 0x83975b4d, 0xe419a64c, 0x4c8aa14f, 0x2b045c4e, 0x4e854c95, 0x290bb194, 0x8198b697, 0xe6164b96, 0xd552ce60, 0xb2dc3361, 0x1a4f3462, 0x7dc1c963, 0x7cc63f8e, 0x1b48c28f, 0xb3dbc58c, 0xd455388d, 0xe711bd7b, 0x809f407a, 0x280c4779, 0x4f82ba78, 0xe30e66cf, 0x84809bce, 0x2c139ccd, 0x4b9d61cc, 0x78d9e43a, 0x1f57193b, 0xb7c41e38, 0xd04ae339, 0xd14d15d4, 0xb6c3e8d5, 0x1e50efd6, 0x79de12d7, 0x4a9a9721, 0x2d146a20, 0x85876d23, 0xe2099022, 0x878880f9, 0xe0067df8, 0x48957afb, 0x2f1b87fa, 0x1c5f020c, 0x7bd1ff0d, 0xd342f80e, 0xb4cc050f, 0xb5cbf3e2, 0xd2450ee3, 0x7ad609e0, 0x1d58f4e1, 0x2e1c7117, 0x49928c16, 0xe1018b15, 0x868f7614, 0xbdf4448a, 0xda7ab98b, 0x72e9be88, 0x15674389, 0x2623c67f, 0x41ad3b7e, 0xe93e3c7d, 0x8eb0c17c, 0x8fb73791, 0xe839ca90, 0x40aacd93, 0x27243092, 0x1460b564, 0x73ee4865, 0xdb7d4f66, 0xbcf3b267, 0xd972a2bc, 0xbefc5fbd, 0x166f58be, 0x71e1a5bf, 0x42a52049, 0x252bdd48, 0x8db8da4b, 0xea36274a, 0xeb31d1a7, 0x8cbf2ca6, 0x242c2ba5, 0x43a2d6a4, 0x70e65352, 0x1768ae53, 0xbffba950, 0xd8755451, 0x74f988e6, 0x137775e7, 0xbbe472e4, 0xdc6a8fe5, 0xef2e0a13, 0x88a0f712, 0x2033f011, 0x47bd0d10, 0x46bafbfd, 0x213406fc, 0x89a701ff, 0xee29fcfe, 0xdd6d7908, 0xbae38409, 0x1270830a, 0x75fe7e0b, 0x107f6ed0, 0x77f193d1, 0xdf6294d2, 0xb8ec69d3, 0x8ba8ec25, 0xec261124, 0x44b51627, 0x233beb26, 0x223c1dcb, 0x45b2e0ca, 0xed21e7c9, 0x8aaf1ac8, 0xb9eb9f3e, 0xde65623f, 0x76f6653c, 0x1178983d }, + { 0x00000000, 0xf20c0dfe, 0xe1f46d0d, 0x13f860f3, 0xc604aceb, 0x3408a115, 0x27f0c1e6, 0xd5fccc18, 0x89e52f27, 0x7be922d9, 0x6811422a, 0x9a1d4fd4, 0x4fe183cc, 0xbded8e32, 0xae15eec1, 0x5c19e33f, 0x162628bf, 0xe42a2541, 0xf7d245b2, 0x05de484c, 0xd0228454, 0x222e89aa, 0x31d6e959, 0xc3dae4a7, 0x9fc30798, 0x6dcf0a66, 0x7e376a95, 0x8c3b676b, 0x59c7ab73, 0xabcba68d, 0xb833c67e, 0x4a3fcb80, 0x2c4c517e, 0xde405c80, 0xcdb83c73, 0x3fb4318d, 0xea48fd95, 0x1844f06b, 0x0bbc9098, 0xf9b09d66, 0xa5a97e59, 0x57a573a7, 0x445d1354, 0xb6511eaa, 0x63add2b2, 0x91a1df4c, 0x8259bfbf, 0x7055b241, 0x3a6a79c1, 0xc866743f, 0xdb9e14cc, 0x29921932, 0xfc6ed52a, 0x0e62d8d4, 0x1d9ab827, 0xef96b5d9, 0xb38f56e6, 0x41835b18, 0x527b3beb, 0xa0773615, 0x758bfa0d, 0x8787f7f3, 0x947f9700, 0x66739afe, 0x5898a2fc, 0xaa94af02, 0xb96ccff1, 0x4b60c20f, 0x9e9c0e17, 0x6c9003e9, 0x7f68631a, 0x8d646ee4, 0xd17d8ddb, 0x23718025, 0x3089e0d6, 0xc285ed28, 0x17792130, 0xe5752cce, 0xf68d4c3d, 0x048141c3, 0x4ebe8a43, 0xbcb287bd, 0xaf4ae74e, 0x5d46eab0, 0x88ba26a8, 0x7ab62b56, 0x694e4ba5, 0x9b42465b, 0xc75ba564, 0x3557a89a, 0x26afc869, 0xd4a3c597, 0x015f098f, 0xf3530471, 0xe0ab6482, 0x12a7697c, 0x74d4f382, 0x86d8fe7c, 0x95209e8f, 0x672c9371, 0xb2d05f69, 0x40dc5297, 0x53243264, 0xa1283f9a, 0xfd31dca5, 0x0f3dd15b, 0x1cc5b1a8, 0xeec9bc56, 0x3b35704e, 0xc9397db0, 0xdac11d43, 0x28cd10bd, 0x62f2db3d, 0x90fed6c3, 0x8306b630, 0x710abbce, 0xa4f677d6, 0x56fa7a28, 0x45021adb, 0xb70e1725, 0xeb17f41a, 0x191bf9e4, 0x0ae39917, 0xf8ef94e9, 0x2d1358f1, 0xdf1f550f, 0xcce735fc, 0x3eeb3802, 0xb13145f8, 0x433d4806, 0x50c528f5, 0xa2c9250b, 0x7735e913, 0x8539e4ed, 0x96c1841e, 0x64cd89e0, 0x38d46adf, 0xcad86721, 0xd92007d2, 0x2b2c0a2c, 0xfed0c634, 0x0cdccbca, 0x1f24ab39, 0xed28a6c7, 0xa7176d47, 0x551b60b9, 0x46e3004a, 0xb4ef0db4, 0x6113c1ac, 0x931fcc52, 0x80e7aca1, 0x72eba15f, 0x2ef24260, 0xdcfe4f9e, 0xcf062f6d, 0x3d0a2293, 0xe8f6ee8b, 0x1afae375, 0x09028386, 0xfb0e8e78, 0x9d7d1486, 0x6f711978, 0x7c89798b, 0x8e857475, 0x5b79b86d, 0xa975b593, 0xba8dd560, 0x4881d89e, 0x14983ba1, 0xe694365f, 0xf56c56ac, 0x07605b52, 0xd29c974a, 0x20909ab4, 0x3368fa47, 0xc164f7b9, 0x8b5b3c39, 0x795731c7, 0x6aaf5134, 0x98a35cca, 0x4d5f90d2, 0xbf539d2c, 0xacabfddf, 0x5ea7f021, 0x02be131e, 0xf0b21ee0, 0xe34a7e13, 0x114673ed, 0xc4babff5, 0x36b6b20b, 0x254ed2f8, 0xd742df06, 0xe9a9e704, 0x1ba5eafa, 0x085d8a09, 0xfa5187f7, 0x2fad4bef, 0xdda14611, 0xce5926e2, 0x3c552b1c, 0x604cc823, 0x9240c5dd, 0x81b8a52e, 0x73b4a8d0, 0xa64864c8, 0x54446936, 0x47bc09c5, 0xb5b0043b, 0xff8fcfbb, 0x0d83c245, 0x1e7ba2b6, 0xec77af48, 0x398b6350, 0xcb876eae, 0xd87f0e5d, 0x2a7303a3, 0x766ae09c, 0x8466ed62, 0x979e8d91, 0x6592806f, 0xb06e4c77, 0x42624189, 0x519a217a, 0xa3962c84, 0xc5e5b67a, 0x37e9bb84, 0x2411db77, 0xd61dd689, 0x03e11a91, 0xf1ed176f, 0xe215779c, 0x10197a62, 0x4c00995d, 0xbe0c94a3, 0xadf4f450, 0x5ff8f9ae, 0x8a0435b6, 0x78083848, 0x6bf058bb, 0x99fc5545, 0xd3c39ec5, 0x21cf933b, 0x3237f3c8, 0xc03bfe36, 0x15c7322e, 0xe7cb3fd0, 0xf4335f23, 0x063f52dd, 0x5a26b1e2, 0xa82abc1c, 0xbbd2dcef, 0x49ded111, 0x9c221d09, 0x6e2e10f7, 0x7dd67004, 0x8fda7dfa } +}; + +static uint32_t long_shifts[4][256] = +{ + { 0x00000000, 0xe040e0ac, 0xc56db7a9, 0x252d5705, 0x8f3719a3, 0x6f77f90f, 0x4a5aae0a, 0xaa1a4ea6, 0x1b8245b7, 0xfbc2a51b, 0xdeeff21e, 0x3eaf12b2, 0x94b55c14, 0x74f5bcb8, 0x51d8ebbd, 0xb1980b11, 0x37048b6e, 0xd7446bc2, 0xf2693cc7, 0x1229dc6b, 0xb83392cd, 0x58737261, 0x7d5e2564, 0x9d1ec5c8, 0x2c86ced9, 0xccc62e75, 0xe9eb7970, 0x09ab99dc, 0xa3b1d77a, 0x43f137d6, 0x66dc60d3, 0x869c807f, 0x6e0916dc, 0x8e49f670, 0xab64a175, 0x4b2441d9, 0xe13e0f7f, 0x017eefd3, 0x2453b8d6, 0xc413587a, 0x758b536b, 0x95cbb3c7, 0xb0e6e4c2, 0x50a6046e, 0xfabc4ac8, 0x1afcaa64, 0x3fd1fd61, 0xdf911dcd, 0x590d9db2, 0xb94d7d1e, 0x9c602a1b, 0x7c20cab7, 0xd63a8411, 0x367a64bd, 0x135733b8, 0xf317d314, 0x428fd805, 0xa2cf38a9, 0x87e26fac, 0x67a28f00, 0xcdb8c1a6, 0x2df8210a, 0x08d5760f, 0xe89596a3, 0xdc122db8, 0x3c52cd14, 0x197f9a11, 0xf93f7abd, 0x5325341b, 0xb365d4b7, 0x964883b2, 0x7608631e, 0xc790680f, 0x27d088a3, 0x02fddfa6, 0xe2bd3f0a, 0x48a771ac, 0xa8e79100, 0x8dcac605, 0x6d8a26a9, 0xeb16a6d6, 0x0b56467a, 0x2e7b117f, 0xce3bf1d3, 0x6421bf75, 0x84615fd9, 0xa14c08dc, 0x410ce870, 0xf094e361, 0x10d403cd, 0x35f954c8, 0xd5b9b464, 0x7fa3fac2, 0x9fe31a6e, 0xbace4d6b, 0x5a8eadc7, 0xb21b3b64, 0x525bdbc8, 0x77768ccd, 0x97366c61, 0x3d2c22c7, 0xdd6cc26b, 0xf841956e, 0x180175c2, 0xa9997ed3, 0x49d99e7f, 0x6cf4c97a, 0x8cb429d6, 0x26ae6770, 0xc6ee87dc, 0xe3c3d0d9, 0x03833075, 0x851fb00a, 0x655f50a6, 0x407207a3, 0xa032e70f, 0x0a28a9a9, 0xea684905, 0xcf451e00, 0x2f05feac, 0x9e9df5bd, 0x7edd1511, 0x5bf04214, 0xbbb0a2b8, 0x11aaec1e, 0xf1ea0cb2, 0xd4c75bb7, 0x3487bb1b, 0xbdc82d81, 0x5d88cd2d, 0x78a59a28, 0x98e57a84, 0x32ff3422, 0xd2bfd48e, 0xf792838b, 0x17d26327, 0xa64a6836, 0x460a889a, 0x6327df9f, 0x83673f33, 0x297d7195, 0xc93d9139, 0xec10c63c, 0x0c502690, 0x8acca6ef, 0x6a8c4643, 0x4fa11146, 0xafe1f1ea, 0x05fbbf4c, 0xe5bb5fe0, 0xc09608e5, 0x20d6e849, 0x914ee358, 0x710e03f4, 0x542354f1, 0xb463b45d, 0x1e79fafb, 0xfe391a57, 0xdb144d52, 0x3b54adfe, 0xd3c13b5d, 0x3381dbf1, 0x16ac8cf4, 0xf6ec6c58, 0x5cf622fe, 0xbcb6c252, 0x999b9557, 0x79db75fb, 0xc8437eea, 0x28039e46, 0x0d2ec943, 0xed6e29ef, 0x47746749, 0xa73487e5, 0x8219d0e0, 0x6259304c, 0xe4c5b033, 0x0485509f, 0x21a8079a, 0xc1e8e736, 0x6bf2a990, 0x8bb2493c, 0xae9f1e39, 0x4edffe95, 0xff47f584, 0x1f071528, 0x3a2a422d, 0xda6aa281, 0x7070ec27, 0x90300c8b, 0xb51d5b8e, 0x555dbb22, 0x61da0039, 0x819ae095, 0xa4b7b790, 0x44f7573c, 0xeeed199a, 0x0eadf936, 0x2b80ae33, 0xcbc04e9f, 0x7a58458e, 0x9a18a522, 0xbf35f227, 0x5f75128b, 0xf56f5c2d, 0x152fbc81, 0x3002eb84, 0xd0420b28, 0x56de8b57, 0xb69e6bfb, 0x93b33cfe, 0x73f3dc52, 0xd9e992f4, 0x39a97258, 0x1c84255d, 0xfcc4c5f1, 0x4d5ccee0, 0xad1c2e4c, 0x88317949, 0x687199e5, 0xc26bd743, 0x222b37ef, 0x070660ea, 0xe7468046, 0x0fd316e5, 0xef93f649, 0xcabea14c, 0x2afe41e0, 0x80e40f46, 0x60a4efea, 0x4589b8ef, 0xa5c95843, 0x14515352, 0xf411b3fe, 0xd13ce4fb, 0x317c0457, 0x9b664af1, 0x7b26aa5d, 0x5e0bfd58, 0xbe4b1df4, 0x38d79d8b, 0xd8977d27, 0xfdba2a22, 0x1dfaca8e, 0xb7e08428, 0x57a06484, 0x728d3381, 0x92cdd32d, 0x2355d83c, 0xc3153890, 0xe6386f95, 0x06788f39, 0xac62c19f, 0x4c222133, 0x690f7636, 0x894f969a }, + { 0x00000000, 0x7e7c2df3, 0xfcf85be6, 0x82847615, 0xfc1cc13d, 0x8260ecce, 0x00e49adb, 0x7e98b728, 0xfdd5f48b, 0x83a9d978, 0x012daf6d, 0x7f51829e, 0x01c935b6, 0x7fb51845, 0xfd316e50, 0x834d43a3, 0xfe479fe7, 0x803bb214, 0x02bfc401, 0x7cc3e9f2, 0x025b5eda, 0x7c277329, 0xfea3053c, 0x80df28cf, 0x03926b6c, 0x7dee469f, 0xff6a308a, 0x81161d79, 0xff8eaa51, 0x81f287a2, 0x0376f1b7, 0x7d0adc44, 0xf963493f, 0x871f64cc, 0x059b12d9, 0x7be73f2a, 0x057f8802, 0x7b03a5f1, 0xf987d3e4, 0x87fbfe17, 0x04b6bdb4, 0x7aca9047, 0xf84ee652, 0x8632cba1, 0xf8aa7c89, 0x86d6517a, 0x0452276f, 0x7a2e0a9c, 0x0724d6d8, 0x7958fb2b, 0xfbdc8d3e, 0x85a0a0cd, 0xfb3817e5, 0x85443a16, 0x07c04c03, 0x79bc61f0, 0xfaf12253, 0x848d0fa0, 0x060979b5, 0x78755446, 0x06ede36e, 0x7891ce9d, 0xfa15b888, 0x8469957b, 0xf72ae48f, 0x8956c97c, 0x0bd2bf69, 0x75ae929a, 0x0b3625b2, 0x754a0841, 0xf7ce7e54, 0x89b253a7, 0x0aff1004, 0x74833df7, 0xf6074be2, 0x887b6611, 0xf6e3d139, 0x889ffcca, 0x0a1b8adf, 0x7467a72c, 0x096d7b68, 0x7711569b, 0xf595208e, 0x8be90d7d, 0xf571ba55, 0x8b0d97a6, 0x0989e1b3, 0x77f5cc40, 0xf4b88fe3, 0x8ac4a210, 0x0840d405, 0x763cf9f6, 0x08a44ede, 0x76d8632d, 0xf45c1538, 0x8a2038cb, 0x0e49adb0, 0x70358043, 0xf2b1f656, 0x8ccddba5, 0xf2556c8d, 0x8c29417e, 0x0ead376b, 0x70d11a98, 0xf39c593b, 0x8de074c8, 0x0f6402dd, 0x71182f2e, 0x0f809806, 0x71fcb5f5, 0xf378c3e0, 0x8d04ee13, 0xf00e3257, 0x8e721fa4, 0x0cf669b1, 0x728a4442, 0x0c12f36a, 0x726ede99, 0xf0eaa88c, 0x8e96857f, 0x0ddbc6dc, 0x73a7eb2f, 0xf1239d3a, 0x8f5fb0c9, 0xf1c707e1, 0x8fbb2a12, 0x0d3f5c07, 0x734371f4, 0xebb9bfef, 0x95c5921c, 0x1741e409, 0x693dc9fa, 0x17a57ed2, 0x69d95321, 0xeb5d2534, 0x952108c7, 0x166c4b64, 0x68106697, 0xea941082, 0x94e83d71, 0xea708a59, 0x940ca7aa, 0x1688d1bf, 0x68f4fc4c, 0x15fe2008, 0x6b820dfb, 0xe9067bee, 0x977a561d, 0xe9e2e135, 0x979eccc6, 0x151abad3, 0x6b669720, 0xe82bd483, 0x9657f970, 0x14d38f65, 0x6aafa296, 0x143715be, 0x6a4b384d, 0xe8cf4e58, 0x96b363ab, 0x12daf6d0, 0x6ca6db23, 0xee22ad36, 0x905e80c5, 0xeec637ed, 0x90ba1a1e, 0x123e6c0b, 0x6c4241f8, 0xef0f025b, 0x91732fa8, 0x13f759bd, 0x6d8b744e, 0x1313c366, 0x6d6fee95, 0xefeb9880, 0x9197b573, 0xec9d6937, 0x92e144c4, 0x106532d1, 0x6e191f22, 0x1081a80a, 0x6efd85f9, 0xec79f3ec, 0x9205de1f, 0x11489dbc, 0x6f34b04f, 0xedb0c65a, 0x93cceba9, 0xed545c81, 0x93287172, 0x11ac0767, 0x6fd02a94, 0x1c935b60, 0x62ef7693, 0xe06b0086, 0x9e172d75, 0xe08f9a5d, 0x9ef3b7ae, 0x1c77c1bb, 0x620bec48, 0xe146afeb, 0x9f3a8218, 0x1dbef40d, 0x63c2d9fe, 0x1d5a6ed6, 0x63264325, 0xe1a23530, 0x9fde18c3, 0xe2d4c487, 0x9ca8e974, 0x1e2c9f61, 0x6050b292, 0x1ec805ba, 0x60b42849, 0xe2305e5c, 0x9c4c73af, 0x1f01300c, 0x617d1dff, 0xe3f96bea, 0x9d854619, 0xe31df131, 0x9d61dcc2, 0x1fe5aad7, 0x61998724, 0xe5f0125f, 0x9b8c3fac, 0x190849b9, 0x6774644a, 0x19ecd362, 0x6790fe91, 0xe5148884, 0x9b68a577, 0x1825e6d4, 0x6659cb27, 0xe4ddbd32, 0x9aa190c1, 0xe43927e9, 0x9a450a1a, 0x18c17c0f, 0x66bd51fc, 0x1bb78db8, 0x65cba04b, 0xe74fd65e, 0x9933fbad, 0xe7ab4c85, 0x99d76176, 0x1b531763, 0x652f3a90, 0xe6627933, 0x981e54c0, 0x1a9a22d5, 0x64e60f26, 0x1a7eb80e, 0x640295fd, 0xe686e3e8, 0x98face1b }, + { 0x00000000, 0xd29f092f, 0xa0d264af, 0x724d6d80, 0x4448bfaf, 0x96d7b680, 0xe49adb00, 0x3605d22f, 0x88917f5e, 0x5a0e7671, 0x28431bf1, 0xfadc12de, 0xccd9c0f1, 0x1e46c9de, 0x6c0ba45e, 0xbe94ad71, 0x14ce884d, 0xc6518162, 0xb41cece2, 0x6683e5cd, 0x508637e2, 0x82193ecd, 0xf054534d, 0x22cb5a62, 0x9c5ff713, 0x4ec0fe3c, 0x3c8d93bc, 0xee129a93, 0xd81748bc, 0x0a884193, 0x78c52c13, 0xaa5a253c, 0x299d109a, 0xfb0219b5, 0x894f7435, 0x5bd07d1a, 0x6dd5af35, 0xbf4aa61a, 0xcd07cb9a, 0x1f98c2b5, 0xa10c6fc4, 0x739366eb, 0x01de0b6b, 0xd3410244, 0xe544d06b, 0x37dbd944, 0x4596b4c4, 0x9709bdeb, 0x3d5398d7, 0xefcc91f8, 0x9d81fc78, 0x4f1ef557, 0x791b2778, 0xab842e57, 0xd9c943d7, 0x0b564af8, 0xb5c2e789, 0x675deea6, 0x15108326, 0xc78f8a09, 0xf18a5826, 0x23155109, 0x51583c89, 0x83c735a6, 0x533a2134, 0x81a5281b, 0xf3e8459b, 0x21774cb4, 0x17729e9b, 0xc5ed97b4, 0xb7a0fa34, 0x653ff31b, 0xdbab5e6a, 0x09345745, 0x7b793ac5, 0xa9e633ea, 0x9fe3e1c5, 0x4d7ce8ea, 0x3f31856a, 0xedae8c45, 0x47f4a979, 0x956ba056, 0xe726cdd6, 0x35b9c4f9, 0x03bc16d6, 0xd1231ff9, 0xa36e7279, 0x71f17b56, 0xcf65d627, 0x1dfadf08, 0x6fb7b288, 0xbd28bba7, 0x8b2d6988, 0x59b260a7, 0x2bff0d27, 0xf9600408, 0x7aa731ae, 0xa8383881, 0xda755501, 0x08ea5c2e, 0x3eef8e01, 0xec70872e, 0x9e3deaae, 0x4ca2e381, 0xf2364ef0, 0x20a947df, 0x52e42a5f, 0x807b2370, 0xb67ef15f, 0x64e1f870, 0x16ac95f0, 0xc4339cdf, 0x6e69b9e3, 0xbcf6b0cc, 0xcebbdd4c, 0x1c24d463, 0x2a21064c, 0xf8be0f63, 0x8af362e3, 0x586c6bcc, 0xe6f8c6bd, 0x3467cf92, 0x462aa212, 0x94b5ab3d, 0xa2b07912, 0x702f703d, 0x02621dbd, 0xd0fd1492, 0xa6744268, 0x74eb4b47, 0x06a626c7, 0xd4392fe8, 0xe23cfdc7, 0x30a3f4e8, 0x42ee9968, 0x90719047, 0x2ee53d36, 0xfc7a3419, 0x8e375999, 0x5ca850b6, 0x6aad8299, 0xb8328bb6, 0xca7fe636, 0x18e0ef19, 0xb2baca25, 0x6025c30a, 0x1268ae8a, 0xc0f7a7a5, 0xf6f2758a, 0x246d7ca5, 0x56201125, 0x84bf180a, 0x3a2bb57b, 0xe8b4bc54, 0x9af9d1d4, 0x4866d8fb, 0x7e630ad4, 0xacfc03fb, 0xdeb16e7b, 0x0c2e6754, 0x8fe952f2, 0x5d765bdd, 0x2f3b365d, 0xfda43f72, 0xcba1ed5d, 0x193ee472, 0x6b7389f2, 0xb9ec80dd, 0x07782dac, 0xd5e72483, 0xa7aa4903, 0x7535402c, 0x43309203, 0x91af9b2c, 0xe3e2f6ac, 0x317dff83, 0x9b27dabf, 0x49b8d390, 0x3bf5be10, 0xe96ab73f, 0xdf6f6510, 0x0df06c3f, 0x7fbd01bf, 0xad220890, 0x13b6a5e1, 0xc129acce, 0xb364c14e, 0x61fbc861, 0x57fe1a4e, 0x85611361, 0xf72c7ee1, 0x25b377ce, 0xf54e635c, 0x27d16a73, 0x559c07f3, 0x87030edc, 0xb106dcf3, 0x6399d5dc, 0x11d4b85c, 0xc34bb173, 0x7ddf1c02, 0xaf40152d, 0xdd0d78ad, 0x0f927182, 0x3997a3ad, 0xeb08aa82, 0x9945c702, 0x4bdace2d, 0xe180eb11, 0x331fe23e, 0x41528fbe, 0x93cd8691, 0xa5c854be, 0x77575d91, 0x051a3011, 0xd785393e, 0x6911944f, 0xbb8e9d60, 0xc9c3f0e0, 0x1b5cf9cf, 0x2d592be0, 0xffc622cf, 0x8d8b4f4f, 0x5f144660, 0xdcd373c6, 0x0e4c7ae9, 0x7c011769, 0xae9e1e46, 0x989bcc69, 0x4a04c546, 0x3849a8c6, 0xead6a1e9, 0x54420c98, 0x86dd05b7, 0xf4906837, 0x260f6118, 0x100ab337, 0xc295ba18, 0xb0d8d798, 0x6247deb7, 0xc81dfb8b, 0x1a82f2a4, 0x68cf9f24, 0xba50960b, 0x8c554424, 0x5eca4d0b, 0x2c87208b, 0xfe1829a4, 0x408c84d5, 0x92138dfa, 0xe05ee07a, 0x32c1e955, 0x04c43b7a, 0xd65b3255, 0xa4165fd5, 0x768956fa }, + { 0x00000000, 0x4904f221, 0x9209e442, 0xdb0d1663, 0x21ffbe75, 0x68fb4c54, 0xb3f65a37, 0xfaf2a816, 0x43ff7cea, 0x0afb8ecb, 0xd1f698a8, 0x98f26a89, 0x6200c29f, 0x2b0430be, 0xf00926dd, 0xb90dd4fc, 0x87fef9d4, 0xcefa0bf5, 0x15f71d96, 0x5cf3efb7, 0xa60147a1, 0xef05b580, 0x3408a3e3, 0x7d0c51c2, 0xc401853e, 0x8d05771f, 0x5608617c, 0x1f0c935d, 0xe5fe3b4b, 0xacfac96a, 0x77f7df09, 0x3ef32d28, 0x0a118559, 0x43157778, 0x9818611b, 0xd11c933a, 0x2bee3b2c, 0x62eac90d, 0xb9e7df6e, 0xf0e32d4f, 0x49eef9b3, 0x00ea0b92, 0xdbe71df1, 0x92e3efd0, 0x681147c6, 0x2115b5e7, 0xfa18a384, 0xb31c51a5, 0x8def7c8d, 0xc4eb8eac, 0x1fe698cf, 0x56e26aee, 0xac10c2f8, 0xe51430d9, 0x3e1926ba, 0x771dd49b, 0xce100067, 0x8714f246, 0x5c19e425, 0x151d1604, 0xefefbe12, 0xa6eb4c33, 0x7de65a50, 0x34e2a871, 0x14230ab2, 0x5d27f893, 0x862aeef0, 0xcf2e1cd1, 0x35dcb4c7, 0x7cd846e6, 0xa7d55085, 0xeed1a2a4, 0x57dc7658, 0x1ed88479, 0xc5d5921a, 0x8cd1603b, 0x7623c82d, 0x3f273a0c, 0xe42a2c6f, 0xad2ede4e, 0x93ddf366, 0xdad90147, 0x01d41724, 0x48d0e505, 0xb2224d13, 0xfb26bf32, 0x202ba951, 0x692f5b70, 0xd0228f8c, 0x99267dad, 0x422b6bce, 0x0b2f99ef, 0xf1dd31f9, 0xb8d9c3d8, 0x63d4d5bb, 0x2ad0279a, 0x1e328feb, 0x57367dca, 0x8c3b6ba9, 0xc53f9988, 0x3fcd319e, 0x76c9c3bf, 0xadc4d5dc, 0xe4c027fd, 0x5dcdf301, 0x14c90120, 0xcfc41743, 0x86c0e562, 0x7c324d74, 0x3536bf55, 0xee3ba936, 0xa73f5b17, 0x99cc763f, 0xd0c8841e, 0x0bc5927d, 0x42c1605c, 0xb833c84a, 0xf1373a6b, 0x2a3a2c08, 0x633ede29, 0xda330ad5, 0x9337f8f4, 0x483aee97, 0x013e1cb6, 0xfbccb4a0, 0xb2c84681, 0x69c550e2, 0x20c1a2c3, 0x28461564, 0x6142e745, 0xba4ff126, 0xf34b0307, 0x09b9ab11, 0x40bd5930, 0x9bb04f53, 0xd2b4bd72, 0x6bb9698e, 0x22bd9baf, 0xf9b08dcc, 0xb0b47fed, 0x4a46d7fb, 0x034225da, 0xd84f33b9, 0x914bc198, 0xafb8ecb0, 0xe6bc1e91, 0x3db108f2, 0x74b5fad3, 0x8e4752c5, 0xc743a0e4, 0x1c4eb687, 0x554a44a6, 0xec47905a, 0xa543627b, 0x7e4e7418, 0x374a8639, 0xcdb82e2f, 0x84bcdc0e, 0x5fb1ca6d, 0x16b5384c, 0x2257903d, 0x6b53621c, 0xb05e747f, 0xf95a865e, 0x03a82e48, 0x4aacdc69, 0x91a1ca0a, 0xd8a5382b, 0x61a8ecd7, 0x28ac1ef6, 0xf3a10895, 0xbaa5fab4, 0x405752a2, 0x0953a083, 0xd25eb6e0, 0x9b5a44c1, 0xa5a969e9, 0xecad9bc8, 0x37a08dab, 0x7ea47f8a, 0x8456d79c, 0xcd5225bd, 0x165f33de, 0x5f5bc1ff, 0xe6561503, 0xaf52e722, 0x745ff141, 0x3d5b0360, 0xc7a9ab76, 0x8ead5957, 0x55a04f34, 0x1ca4bd15, 0x3c651fd6, 0x7561edf7, 0xae6cfb94, 0xe76809b5, 0x1d9aa1a3, 0x549e5382, 0x8f9345e1, 0xc697b7c0, 0x7f9a633c, 0x369e911d, 0xed93877e, 0xa497755f, 0x5e65dd49, 0x17612f68, 0xcc6c390b, 0x8568cb2a, 0xbb9be602, 0xf29f1423, 0x29920240, 0x6096f061, 0x9a645877, 0xd360aa56, 0x086dbc35, 0x41694e14, 0xf8649ae8, 0xb16068c9, 0x6a6d7eaa, 0x23698c8b, 0xd99b249d, 0x909fd6bc, 0x4b92c0df, 0x029632fe, 0x36749a8f, 0x7f7068ae, 0xa47d7ecd, 0xed798cec, 0x178b24fa, 0x5e8fd6db, 0x8582c0b8, 0xcc863299, 0x758be665, 0x3c8f1444, 0xe7820227, 0xae86f006, 0x54745810, 0x1d70aa31, 0xc67dbc52, 0x8f794e73, 0xb18a635b, 0xf88e917a, 0x23838719, 0x6a877538, 0x9075dd2e, 0xd9712f0f, 0x027c396c, 0x4b78cb4d, 0xf2751fb1, 0xbb71ed90, 0x607cfbf3, 0x297809d2, 0xd38aa1c4, 0x9a8e53e5, 0x41834586, 0x0887b7a7 } +}; + +static uint32_t short_shifts[4][256] = +{ + { 0x00000000, 0xdcb17aa4, 0xbc8e83b9, 0x603ff91d, 0x7cf17183, 0xa0400b27, 0xc07ff23a, 0x1cce889e, 0xf9e2e306, 0x255399a2, 0x456c60bf, 0x99dd1a1b, 0x85139285, 0x59a2e821, 0x399d113c, 0xe52c6b98, 0xf629b0fd, 0x2a98ca59, 0x4aa73344, 0x961649e0, 0x8ad8c17e, 0x5669bbda, 0x365642c7, 0xeae73863, 0x0fcb53fb, 0xd37a295f, 0xb345d042, 0x6ff4aae6, 0x733a2278, 0xaf8b58dc, 0xcfb4a1c1, 0x1305db65, 0xe9bf170b, 0x350e6daf, 0x553194b2, 0x8980ee16, 0x954e6688, 0x49ff1c2c, 0x29c0e531, 0xf5719f95, 0x105df40d, 0xccec8ea9, 0xacd377b4, 0x70620d10, 0x6cac858e, 0xb01dff2a, 0xd0220637, 0x0c937c93, 0x1f96a7f6, 0xc327dd52, 0xa318244f, 0x7fa95eeb, 0x6367d675, 0xbfd6acd1, 0xdfe955cc, 0x03582f68, 0xe67444f0, 0x3ac53e54, 0x5afac749, 0x864bbded, 0x9a853573, 0x46344fd7, 0x260bb6ca, 0xfabacc6e, 0xd69258e7, 0x0a232243, 0x6a1cdb5e, 0xb6ada1fa, 0xaa632964, 0x76d253c0, 0x16edaadd, 0xca5cd079, 0x2f70bbe1, 0xf3c1c145, 0x93fe3858, 0x4f4f42fc, 0x5381ca62, 0x8f30b0c6, 0xef0f49db, 0x33be337f, 0x20bbe81a, 0xfc0a92be, 0x9c356ba3, 0x40841107, 0x5c4a9999, 0x80fbe33d, 0xe0c41a20, 0x3c756084, 0xd9590b1c, 0x05e871b8, 0x65d788a5, 0xb966f201, 0xa5a87a9f, 0x7919003b, 0x1926f926, 0xc5978382, 0x3f2d4fec, 0xe39c3548, 0x83a3cc55, 0x5f12b6f1, 0x43dc3e6f, 0x9f6d44cb, 0xff52bdd6, 0x23e3c772, 0xc6cfacea, 0x1a7ed64e, 0x7a412f53, 0xa6f055f7, 0xba3edd69, 0x668fa7cd, 0x06b05ed0, 0xda012474, 0xc904ff11, 0x15b585b5, 0x758a7ca8, 0xa93b060c, 0xb5f58e92, 0x6944f436, 0x097b0d2b, 0xd5ca778f, 0x30e61c17, 0xec5766b3, 0x8c689fae, 0x50d9e50a, 0x4c176d94, 0x90a61730, 0xf099ee2d, 0x2c289489, 0xa8c8c73f, 0x7479bd9b, 0x14464486, 0xc8f73e22, 0xd439b6bc, 0x0888cc18, 0x68b73505, 0xb4064fa1, 0x512a2439, 0x8d9b5e9d, 0xeda4a780, 0x3115dd24, 0x2ddb55ba, 0xf16a2f1e, 0x9155d603, 0x4de4aca7, 0x5ee177c2, 0x82500d66, 0xe26ff47b, 0x3ede8edf, 0x22100641, 0xfea17ce5, 0x9e9e85f8, 0x422fff5c, 0xa70394c4, 0x7bb2ee60, 0x1b8d177d, 0xc73c6dd9, 0xdbf2e547, 0x07439fe3, 0x677c66fe, 0xbbcd1c5a, 0x4177d034, 0x9dc6aa90, 0xfdf9538d, 0x21482929, 0x3d86a1b7, 0xe137db13, 0x8108220e, 0x5db958aa, 0xb8953332, 0x64244996, 0x041bb08b, 0xd8aaca2f, 0xc46442b1, 0x18d53815, 0x78eac108, 0xa45bbbac, 0xb75e60c9, 0x6bef1a6d, 0x0bd0e370, 0xd76199d4, 0xcbaf114a, 0x171e6bee, 0x772192f3, 0xab90e857, 0x4ebc83cf, 0x920df96b, 0xf2320076, 0x2e837ad2, 0x324df24c, 0xeefc88e8, 0x8ec371f5, 0x52720b51, 0x7e5a9fd8, 0xa2ebe57c, 0xc2d41c61, 0x1e6566c5, 0x02abee5b, 0xde1a94ff, 0xbe256de2, 0x62941746, 0x87b87cde, 0x5b09067a, 0x3b36ff67, 0xe78785c3, 0xfb490d5d, 0x27f877f9, 0x47c78ee4, 0x9b76f440, 0x88732f25, 0x54c25581, 0x34fdac9c, 0xe84cd638, 0xf4825ea6, 0x28332402, 0x480cdd1f, 0x94bda7bb, 0x7191cc23, 0xad20b687, 0xcd1f4f9a, 0x11ae353e, 0x0d60bda0, 0xd1d1c704, 0xb1ee3e19, 0x6d5f44bd, 0x97e588d3, 0x4b54f277, 0x2b6b0b6a, 0xf7da71ce, 0xeb14f950, 0x37a583f4, 0x579a7ae9, 0x8b2b004d, 0x6e076bd5, 0xb2b61171, 0xd289e86c, 0x0e3892c8, 0x12f61a56, 0xce4760f2, 0xae7899ef, 0x72c9e34b, 0x61cc382e, 0xbd7d428a, 0xdd42bb97, 0x01f3c133, 0x1d3d49ad, 0xc18c3309, 0xa1b3ca14, 0x7d02b0b0, 0x982edb28, 0x449fa18c, 0x24a05891, 0xf8112235, 0xe4dfaaab, 0x386ed00f, 0x58512912, 0x84e053b6 }, + { 0x00000000, 0x547df88f, 0xa8fbf11e, 0xfc860991, 0x541b94cd, 0x00666c42, 0xfce065d3, 0xa89d9d5c, 0xa837299a, 0xfc4ad115, 0x00ccd884, 0x54b1200b, 0xfc2cbd57, 0xa85145d8, 0x54d74c49, 0x00aab4c6, 0x558225c5, 0x01ffdd4a, 0xfd79d4db, 0xa9042c54, 0x0199b108, 0x55e44987, 0xa9624016, 0xfd1fb899, 0xfdb50c5f, 0xa9c8f4d0, 0x554efd41, 0x013305ce, 0xa9ae9892, 0xfdd3601d, 0x0155698c, 0x55289103, 0xab044b8a, 0xff79b305, 0x03ffba94, 0x5782421b, 0xff1fdf47, 0xab6227c8, 0x57e42e59, 0x0399d6d6, 0x03336210, 0x574e9a9f, 0xabc8930e, 0xffb56b81, 0x5728f6dd, 0x03550e52, 0xffd307c3, 0xabaeff4c, 0xfe866e4f, 0xaafb96c0, 0x567d9f51, 0x020067de, 0xaa9dfa82, 0xfee0020d, 0x02660b9c, 0x561bf313, 0x56b147d5, 0x02ccbf5a, 0xfe4ab6cb, 0xaa374e44, 0x02aad318, 0x56d72b97, 0xaa512206, 0xfe2cda89, 0x53e4e1e5, 0x0799196a, 0xfb1f10fb, 0xaf62e874, 0x07ff7528, 0x53828da7, 0xaf048436, 0xfb797cb9, 0xfbd3c87f, 0xafae30f0, 0x53283961, 0x0755c1ee, 0xafc85cb2, 0xfbb5a43d, 0x0733adac, 0x534e5523, 0x0666c420, 0x521b3caf, 0xae9d353e, 0xfae0cdb1, 0x527d50ed, 0x0600a862, 0xfa86a1f3, 0xaefb597c, 0xae51edba, 0xfa2c1535, 0x06aa1ca4, 0x52d7e42b, 0xfa4a7977, 0xae3781f8, 0x52b18869, 0x06cc70e6, 0xf8e0aa6f, 0xac9d52e0, 0x501b5b71, 0x0466a3fe, 0xacfb3ea2, 0xf886c62d, 0x0400cfbc, 0x507d3733, 0x50d783f5, 0x04aa7b7a, 0xf82c72eb, 0xac518a64, 0x04cc1738, 0x50b1efb7, 0xac37e626, 0xf84a1ea9, 0xad628faa, 0xf91f7725, 0x05997eb4, 0x51e4863b, 0xf9791b67, 0xad04e3e8, 0x5182ea79, 0x05ff12f6, 0x0555a630, 0x51285ebf, 0xadae572e, 0xf9d3afa1, 0x514e32fd, 0x0533ca72, 0xf9b5c3e3, 0xadc83b6c, 0xa7c9c3ca, 0xf3b43b45, 0x0f3232d4, 0x5b4fca5b, 0xf3d25707, 0xa7afaf88, 0x5b29a619, 0x0f545e96, 0x0ffeea50, 0x5b8312df, 0xa7051b4e, 0xf378e3c1, 0x5be57e9d, 0x0f988612, 0xf31e8f83, 0xa763770c, 0xf24be60f, 0xa6361e80, 0x5ab01711, 0x0ecdef9e, 0xa65072c2, 0xf22d8a4d, 0x0eab83dc, 0x5ad67b53, 0x5a7ccf95, 0x0e01371a, 0xf2873e8b, 0xa6fac604, 0x0e675b58, 0x5a1aa3d7, 0xa69caa46, 0xf2e152c9, 0x0ccd8840, 0x58b070cf, 0xa436795e, 0xf04b81d1, 0x58d61c8d, 0x0cabe402, 0xf02ded93, 0xa450151c, 0xa4faa1da, 0xf0875955, 0x0c0150c4, 0x587ca84b, 0xf0e13517, 0xa49ccd98, 0x581ac409, 0x0c673c86, 0x594fad85, 0x0d32550a, 0xf1b45c9b, 0xa5c9a414, 0x0d543948, 0x5929c1c7, 0xa5afc856, 0xf1d230d9, 0xf178841f, 0xa5057c90, 0x59837501, 0x0dfe8d8e, 0xa56310d2, 0xf11ee85d, 0x0d98e1cc, 0x59e51943, 0xf42d222f, 0xa050daa0, 0x5cd6d331, 0x08ab2bbe, 0xa036b6e2, 0xf44b4e6d, 0x08cd47fc, 0x5cb0bf73, 0x5c1a0bb5, 0x0867f33a, 0xf4e1faab, 0xa09c0224, 0x08019f78, 0x5c7c67f7, 0xa0fa6e66, 0xf48796e9, 0xa1af07ea, 0xf5d2ff65, 0x0954f6f4, 0x5d290e7b, 0xf5b49327, 0xa1c96ba8, 0x5d4f6239, 0x09329ab6, 0x09982e70, 0x5de5d6ff, 0xa163df6e, 0xf51e27e1, 0x5d83babd, 0x09fe4232, 0xf5784ba3, 0xa105b32c, 0x5f2969a5, 0x0b54912a, 0xf7d298bb, 0xa3af6034, 0x0b32fd68, 0x5f4f05e7, 0xa3c90c76, 0xf7b4f4f9, 0xf71e403f, 0xa363b8b0, 0x5fe5b121, 0x0b9849ae, 0xa305d4f2, 0xf7782c7d, 0x0bfe25ec, 0x5f83dd63, 0x0aab4c60, 0x5ed6b4ef, 0xa250bd7e, 0xf62d45f1, 0x5eb0d8ad, 0x0acd2022, 0xf64b29b3, 0xa236d13c, 0xa29c65fa, 0xf6e19d75, 0x0a6794e4, 0x5e1a6c6b, 0xf687f137, 0xa2fa09b8, 0x5e7c0029, 0x0a01f8a6 }, + { 0x00000000, 0x4a7ff165, 0x94ffe2ca, 0xde8013af, 0x2c13b365, 0x666c4200, 0xb8ec51af, 0xf293a0ca, 0x582766ca, 0x125897af, 0xccd88400, 0x86a77565, 0x7434d5af, 0x3e4b24ca, 0xe0cb3765, 0xaab4c600, 0xb04ecd94, 0xfa313cf1, 0x24b12f5e, 0x6ecede3b, 0x9c5d7ef1, 0xd6228f94, 0x08a29c3b, 0x42dd6d5e, 0xe869ab5e, 0xa2165a3b, 0x7c964994, 0x36e9b8f1, 0xc47a183b, 0x8e05e95e, 0x5085faf1, 0x1afa0b94, 0x6571edd9, 0x2f0e1cbc, 0xf18e0f13, 0xbbf1fe76, 0x49625ebc, 0x031dafd9, 0xdd9dbc76, 0x97e24d13, 0x3d568b13, 0x77297a76, 0xa9a969d9, 0xe3d698bc, 0x11453876, 0x5b3ac913, 0x85badabc, 0xcfc52bd9, 0xd53f204d, 0x9f40d128, 0x41c0c287, 0x0bbf33e2, 0xf92c9328, 0xb353624d, 0x6dd371e2, 0x27ac8087, 0x8d184687, 0xc767b7e2, 0x19e7a44d, 0x53985528, 0xa10bf5e2, 0xeb740487, 0x35f41728, 0x7f8be64d, 0xcae3dbb2, 0x809c2ad7, 0x5e1c3978, 0x1463c81d, 0xe6f068d7, 0xac8f99b2, 0x720f8a1d, 0x38707b78, 0x92c4bd78, 0xd8bb4c1d, 0x063b5fb2, 0x4c44aed7, 0xbed70e1d, 0xf4a8ff78, 0x2a28ecd7, 0x60571db2, 0x7aad1626, 0x30d2e743, 0xee52f4ec, 0xa42d0589, 0x56bea543, 0x1cc15426, 0xc2414789, 0x883eb6ec, 0x228a70ec, 0x68f58189, 0xb6759226, 0xfc0a6343, 0x0e99c389, 0x44e632ec, 0x9a662143, 0xd019d026, 0xaf92366b, 0xe5edc70e, 0x3b6dd4a1, 0x711225c4, 0x8381850e, 0xc9fe746b, 0x177e67c4, 0x5d0196a1, 0xf7b550a1, 0xbdcaa1c4, 0x634ab26b, 0x2935430e, 0xdba6e3c4, 0x91d912a1, 0x4f59010e, 0x0526f06b, 0x1fdcfbff, 0x55a30a9a, 0x8b231935, 0xc15ce850, 0x33cf489a, 0x79b0b9ff, 0xa730aa50, 0xed4f5b35, 0x47fb9d35, 0x0d846c50, 0xd3047fff, 0x997b8e9a, 0x6be82e50, 0x2197df35, 0xff17cc9a, 0xb5683dff, 0x902bc195, 0xda5430f0, 0x04d4235f, 0x4eabd23a, 0xbc3872f0, 0xf6478395, 0x28c7903a, 0x62b8615f, 0xc80ca75f, 0x8273563a, 0x5cf34595, 0x168cb4f0, 0xe41f143a, 0xae60e55f, 0x70e0f6f0, 0x3a9f0795, 0x20650c01, 0x6a1afd64, 0xb49aeecb, 0xfee51fae, 0x0c76bf64, 0x46094e01, 0x98895dae, 0xd2f6accb, 0x78426acb, 0x323d9bae, 0xecbd8801, 0xa6c27964, 0x5451d9ae, 0x1e2e28cb, 0xc0ae3b64, 0x8ad1ca01, 0xf55a2c4c, 0xbf25dd29, 0x61a5ce86, 0x2bda3fe3, 0xd9499f29, 0x93366e4c, 0x4db67de3, 0x07c98c86, 0xad7d4a86, 0xe702bbe3, 0x3982a84c, 0x73fd5929, 0x816ef9e3, 0xcb110886, 0x15911b29, 0x5feeea4c, 0x4514e1d8, 0x0f6b10bd, 0xd1eb0312, 0x9b94f277, 0x690752bd, 0x2378a3d8, 0xfdf8b077, 0xb7874112, 0x1d338712, 0x574c7677, 0x89cc65d8, 0xc3b394bd, 0x31203477, 0x7b5fc512, 0xa5dfd6bd, 0xefa027d8, 0x5ac81a27, 0x10b7eb42, 0xce37f8ed, 0x84480988, 0x76dba942, 0x3ca45827, 0xe2244b88, 0xa85bbaed, 0x02ef7ced, 0x48908d88, 0x96109e27, 0xdc6f6f42, 0x2efccf88, 0x64833eed, 0xba032d42, 0xf07cdc27, 0xea86d7b3, 0xa0f926d6, 0x7e793579, 0x3406c41c, 0xc69564d6, 0x8cea95b3, 0x526a861c, 0x18157779, 0xb2a1b179, 0xf8de401c, 0x265e53b3, 0x6c21a2d6, 0x9eb2021c, 0xd4cdf379, 0x0a4de0d6, 0x403211b3, 0x3fb9f7fe, 0x75c6069b, 0xab461534, 0xe139e451, 0x13aa449b, 0x59d5b5fe, 0x8755a651, 0xcd2a5734, 0x679e9134, 0x2de16051, 0xf36173fe, 0xb91e829b, 0x4b8d2251, 0x01f2d334, 0xdf72c09b, 0x950d31fe, 0x8ff73a6a, 0xc588cb0f, 0x1b08d8a0, 0x517729c5, 0xa3e4890f, 0xe99b786a, 0x371b6bc5, 0x7d649aa0, 0xd7d05ca0, 0x9dafadc5, 0x432fbe6a, 0x09504f0f, 0xfbc3efc5, 0xb1bc1ea0, 0x6f3c0d0f, 0x2543fc6a }, + { 0x00000000, 0x25bbf5db, 0x4b77ebb6, 0x6ecc1e6d, 0x96efd76c, 0xb35422b7, 0xdd983cda, 0xf823c901, 0x2833d829, 0x0d882df2, 0x6344339f, 0x46ffc644, 0xbedc0f45, 0x9b67fa9e, 0xf5abe4f3, 0xd0101128, 0x5067b052, 0x75dc4589, 0x1b105be4, 0x3eabae3f, 0xc688673e, 0xe33392e5, 0x8dff8c88, 0xa8447953, 0x7854687b, 0x5def9da0, 0x332383cd, 0x16987616, 0xeebbbf17, 0xcb004acc, 0xa5cc54a1, 0x8077a17a, 0xa0cf60a4, 0x8574957f, 0xebb88b12, 0xce037ec9, 0x3620b7c8, 0x139b4213, 0x7d575c7e, 0x58eca9a5, 0x88fcb88d, 0xad474d56, 0xc38b533b, 0xe630a6e0, 0x1e136fe1, 0x3ba89a3a, 0x55648457, 0x70df718c, 0xf0a8d0f6, 0xd513252d, 0xbbdf3b40, 0x9e64ce9b, 0x6647079a, 0x43fcf241, 0x2d30ec2c, 0x088b19f7, 0xd89b08df, 0xfd20fd04, 0x93ece369, 0xb65716b2, 0x4e74dfb3, 0x6bcf2a68, 0x05033405, 0x20b8c1de, 0x4472b7b9, 0x61c94262, 0x0f055c0f, 0x2abea9d4, 0xd29d60d5, 0xf726950e, 0x99ea8b63, 0xbc517eb8, 0x6c416f90, 0x49fa9a4b, 0x27368426, 0x028d71fd, 0xfaaeb8fc, 0xdf154d27, 0xb1d9534a, 0x9462a691, 0x141507eb, 0x31aef230, 0x5f62ec5d, 0x7ad91986, 0x82fad087, 0xa741255c, 0xc98d3b31, 0xec36ceea, 0x3c26dfc2, 0x199d2a19, 0x77513474, 0x52eac1af, 0xaac908ae, 0x8f72fd75, 0xe1bee318, 0xc40516c3, 0xe4bdd71d, 0xc10622c6, 0xafca3cab, 0x8a71c970, 0x72520071, 0x57e9f5aa, 0x3925ebc7, 0x1c9e1e1c, 0xcc8e0f34, 0xe935faef, 0x87f9e482, 0xa2421159, 0x5a61d858, 0x7fda2d83, 0x111633ee, 0x34adc635, 0xb4da674f, 0x91619294, 0xffad8cf9, 0xda167922, 0x2235b023, 0x078e45f8, 0x69425b95, 0x4cf9ae4e, 0x9ce9bf66, 0xb9524abd, 0xd79e54d0, 0xf225a10b, 0x0a06680a, 0x2fbd9dd1, 0x417183bc, 0x64ca7667, 0x88e56f72, 0xad5e9aa9, 0xc39284c4, 0xe629711f, 0x1e0ab81e, 0x3bb14dc5, 0x557d53a8, 0x70c6a673, 0xa0d6b75b, 0x856d4280, 0xeba15ced, 0xce1aa936, 0x36396037, 0x138295ec, 0x7d4e8b81, 0x58f57e5a, 0xd882df20, 0xfd392afb, 0x93f53496, 0xb64ec14d, 0x4e6d084c, 0x6bd6fd97, 0x051ae3fa, 0x20a11621, 0xf0b10709, 0xd50af2d2, 0xbbc6ecbf, 0x9e7d1964, 0x665ed065, 0x43e525be, 0x2d293bd3, 0x0892ce08, 0x282a0fd6, 0x0d91fa0d, 0x635de460, 0x46e611bb, 0xbec5d8ba, 0x9b7e2d61, 0xf5b2330c, 0xd009c6d7, 0x0019d7ff, 0x25a22224, 0x4b6e3c49, 0x6ed5c992, 0x96f60093, 0xb34df548, 0xdd81eb25, 0xf83a1efe, 0x784dbf84, 0x5df64a5f, 0x333a5432, 0x1681a1e9, 0xeea268e8, 0xcb199d33, 0xa5d5835e, 0x806e7685, 0x507e67ad, 0x75c59276, 0x1b098c1b, 0x3eb279c0, 0xc691b0c1, 0xe32a451a, 0x8de65b77, 0xa85daeac, 0xcc97d8cb, 0xe92c2d10, 0x87e0337d, 0xa25bc6a6, 0x5a780fa7, 0x7fc3fa7c, 0x110fe411, 0x34b411ca, 0xe4a400e2, 0xc11ff539, 0xafd3eb54, 0x8a681e8f, 0x724bd78e, 0x57f02255, 0x393c3c38, 0x1c87c9e3, 0x9cf06899, 0xb94b9d42, 0xd787832f, 0xf23c76f4, 0x0a1fbff5, 0x2fa44a2e, 0x41685443, 0x64d3a198, 0xb4c3b0b0, 0x9178456b, 0xffb45b06, 0xda0faedd, 0x222c67dc, 0x07979207, 0x695b8c6a, 0x4ce079b1, 0x6c58b86f, 0x49e34db4, 0x272f53d9, 0x0294a602, 0xfab76f03, 0xdf0c9ad8, 0xb1c084b5, 0x947b716e, 0x446b6046, 0x61d0959d, 0x0f1c8bf0, 0x2aa77e2b, 0xd284b72a, 0xf73f42f1, 0x99f35c9c, 0xbc48a947, 0x3c3f083d, 0x1984fde6, 0x7748e38b, 0x52f31650, 0xaad0df51, 0x8f6b2a8a, 0xe1a734e7, 0xc41cc13c, 0x140cd014, 0x31b725cf, 0x5f7b3ba2, 0x7ac0ce79, 0x82e30778, 0xa758f2a3, 0xc994ecce, 0xec2f1915 } +}; diff --git a/fdbrpc/genericactors.actor.cpp b/fdbrpc/genericactors.actor.cpp new file mode 100644 index 0000000000..dfd28627ec --- /dev/null +++ b/fdbrpc/genericactors.actor.cpp @@ -0,0 +1,39 @@ +/* + * genericactors.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "genericactors.actor.h" // Gets genericactors.actor.g.h indirectly +#include "flow/network.h" +#include "simulator.h" + +ACTOR void simDeliverDuplicate( Standalone data, Endpoint destination ) { + Void _ = wait( delay( g_random->random01() * FLOW_KNOBS->MAX_DELIVER_DUPLICATE_DELAY ) ); + FlowTransport::transport().sendUnreliable( SerializeSourceRaw(data), destination ); +} + +ACTOR Future disableConnectionFailuresAfter( double time, std::string context ) { + Void _ = wait( delay(time) ); + + if(g_network->isSimulated()) { + g_simulator.enableConnectionFailures = false; + g_simulator.speedUpSimulation = true; + TraceEvent(SevWarnAlways, ("DisableConnectionFailures_" + context).c_str()); + } + return Void(); +} diff --git a/fdbrpc/genericactors.actor.h b/fdbrpc/genericactors.actor.h new file mode 100644 index 0000000000..6194171206 --- /dev/null +++ b/fdbrpc/genericactors.actor.h @@ -0,0 +1,197 @@ +/* + * genericactors.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FDBRPC_GENERICACTORS_ACTOR_G_H) + #define FDBRPC_GENERICACTORS_ACTOR_G_H + #include "genericactors.actor.g.h" +#elif !defined(RPCGENERICACTORS_ACTOR_H) + #define RPCGENERICACTORS_ACTOR_H + +#include "flow/genericactors.actor.h" +#include "fdbrpc.h" + +ACTOR template +Future retryBrokenPromise( RequestStream to, Req request ) { + // Like to.getReply(request), except that a broken_promise exception results in retrying request immediately. + // Suitable for use with well known endpoints, which are likely to return to existence after the other process restarts. + // Not normally useful for ordinary endpoints, which conventionally are permanently destroyed after replying with broken_promise. + loop { + try { + REPLY_TYPE(Req) reply = wait( to.getReply( request ) ); + return reply; + } catch( Error& e ) { + if (e.code() != error_code_broken_promise) + throw; + resetReply( request ); + Void _ = wait( delayJittered(FLOW_KNOBS->PREVENT_FAST_SPIN_DELAY) ); + TEST(true); // retryBrokenPromise + } + } +} + +ACTOR template +Future retryBrokenPromise( RequestStream to, Req request, int taskID ) { + // Like to.getReply(request), except that a broken_promise exception results in retrying request immediately. + // Suitable for use with well known endpoints, which are likely to return to existence after the other process restarts. + // Not normally useful for ordinary endpoints, which conventionally are permanently destroyed after replying with broken_promise. + loop { + try { + REPLY_TYPE(Req) reply = wait( to.getReply( request, taskID ) ); + return reply; + } catch( Error& e ) { + if (e.code() != error_code_broken_promise) + throw; + resetReply( request ); + Void _ = wait( delayJittered(FLOW_KNOBS->PREVENT_FAST_SPIN_DELAY, taskID) ); + TEST(true); // retryBrokenPromise + } + } +} + +ACTOR template +Future timeoutWarning( Future what, double time, PromiseStream output ) { + state double start = now(); + state Future end = delay( time ); + loop choose { + when ( T t = wait( what ) ) { return t; } + when ( Void _ = wait( end ) ) { + output.send( Void() ); + end = delay( time ); + } + } +} + + +ACTOR template +Future sendCanceler( ReplyPromise reply, PacketID send ) { + try { + T t = wait( reply.getFuture() ); + FlowTransport::transport().cancelReliable(send); + return t; + } catch (...) { + FlowTransport::transport().cancelReliable(send); + throw; + } +} + +ACTOR template +void networkSender( Future input, Endpoint endpoint ) { + try { + T value = wait( input ); + FlowTransport::transport().sendUnreliable( SerializeBoolAnd(true, value), endpoint ); + } catch (Error& err) { + //if (err.code() == error_code_broken_promise) return; + ASSERT( err.code() != error_code_actor_cancelled ); + FlowTransport::transport().sendUnreliable( SerializeBoolAnd(false, err), endpoint ); + } +} + +ACTOR template +void forwardPromise( Promise output, Future input ) { + try { + T value = wait(input); + output.send(value); + } catch (Error& err) { + output.sendError(err); + } +} + +ACTOR template +void forwardPromise(ReplyPromise output, Future input) { + try { + T value = wait(input); + output.send(value); + } + catch (Error& err) { + output.sendError(err); + } +} + +ACTOR template +void forwardPromise( PromiseStream output, Future input ) { + try{ + T value = wait(input); + output.send(value); + } catch (Error& e) { + output.sendError(e); + } +} + + + +ACTOR template Future broadcast(Future input, std::vector> output) { + T value = wait(input); + for (int i = 0; i Future broadcast( Future input, std::vector> output ) { + T value = wait( input ); + for(int i=0; i +Future> waitValueOrSignal( Future value, Future signal, Endpoint endpoint, ReplyPromise holdme = ReplyPromise() ) { + loop { + try { + choose { + when ( X x = wait(value) ) { + return x; + } + when ( Void _ = wait(signal) ) { + return ErrorOr(request_maybe_delivered()); + } + } + } catch (Error& e) { + if (signal.isError()) { + TraceEvent(SevError, "WaitValueOrSignalError").error(signal.getError()); + return ErrorOr(internal_error()); + } + + if( e.code() == error_code_actor_cancelled ) + throw e; + + // broken_promise error normally means an endpoint failure, which in tryGetReply has the same semantics as receiving the failure signal + if (e.code() != error_code_broken_promise || signal.isError()) + return ErrorOr(e); + + IFailureMonitor::failureMonitor().endpointNotFound( endpoint ); + value = Never(); + } + } +} + + +Future disableConnectionFailuresAfter( double const& time, std::string const& context ); + + +#endif diff --git a/fdbrpc/libb64/cdecode.c b/fdbrpc/libb64/cdecode.c new file mode 100755 index 0000000000..7d3a9311fa --- /dev/null +++ b/fdbrpc/libb64/cdecode.c @@ -0,0 +1,108 @@ +/* + * cdecode.c + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* +cdecoder.c - c source to a base64 decoding algorithm implementation + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#include "cdecode.h" + +int base64_decode_value(char value_in) +{ + static const char decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51}; + static const char decoding_size = sizeof(decoding); + value_in -= 43; + if (value_in < 0 || value_in > decoding_size) return -1; + return decoding[(int)value_in]; +} + +void base64_init_decodestate(base64_decodestate* state_in) +{ + state_in->step = step_a; + state_in->plainchar = 0; +} + +int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in) +{ + const char* codechar = code_in; + char* plainchar = plaintext_out; + char fragment; + + *plainchar = state_in->plainchar; + + switch (state_in->step) + { + while (1) + { + case step_a: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_a; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar = (fragment & 0x03f) << 2; + case step_b: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_b; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar++ |= (fragment & 0x030) >> 4; + *plainchar = (fragment & 0x00f) << 4; + case step_c: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_c; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar++ |= (fragment & 0x03c) >> 2; + *plainchar = (fragment & 0x003) << 6; + case step_d: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_d; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar++ |= (fragment & 0x03f); + } + } + /* control should not reach here */ + return plainchar - plaintext_out; +} + diff --git a/fdbrpc/libb64/cdecode.h b/fdbrpc/libb64/cdecode.h new file mode 100755 index 0000000000..2bd51bfbcc --- /dev/null +++ b/fdbrpc/libb64/cdecode.h @@ -0,0 +1,48 @@ +/* + * cdecode.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* +cdecode.h - c header for a base64 decoding algorithm + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#ifndef BASE64_CDECODE_H +#define BASE64_CDECODE_H + +typedef enum +{ + step_a, step_b, step_c, step_d +} base64_decodestep; + +typedef struct +{ + base64_decodestep step; + char plainchar; +} base64_decodestate; + +void base64_init_decodestate(base64_decodestate* state_in); + +int base64_decode_value(char value_in); + +int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in); + +#endif /* BASE64_CDECODE_H */ diff --git a/fdbrpc/libb64/cencode.c b/fdbrpc/libb64/cencode.c new file mode 100644 index 0000000000..6f4c60b030 --- /dev/null +++ b/fdbrpc/libb64/cencode.c @@ -0,0 +1,129 @@ +/* + * cencode.c + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* +cencoder.c - c source to a base64 encoding algorithm implementation + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#include "cencode.h" + +const int CHARS_PER_LINE = 72; + +void base64_init_encodestate(base64_encodestate* state_in) +{ + state_in->step = step_A; + state_in->result = 0; + state_in->stepcount = 0; +} + +char base64_encode_value(char value_in) +{ + static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + if (value_in > 63) return '='; + return encoding[(int)value_in]; +} + +int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in) +{ + const char* plainchar = plaintext_in; + const char* const plaintextend = plaintext_in + length_in; + char* codechar = code_out; + char result; + char fragment; + + result = state_in->result; + + switch (state_in->step) + { + while (1) + { + case step_A: + if (plainchar == plaintextend) + { + state_in->result = result; + state_in->step = step_A; + return codechar - code_out; + } + fragment = *plainchar++; + result = (fragment & 0x0fc) >> 2; + *codechar++ = base64_encode_value(result); + result = (fragment & 0x003) << 4; + case step_B: + if (plainchar == plaintextend) + { + state_in->result = result; + state_in->step = step_B; + return (int)(codechar - code_out); + } + fragment = *plainchar++; + result |= (fragment & 0x0f0) >> 4; + *codechar++ = base64_encode_value(result); + result = (fragment & 0x00f) << 2; + case step_C: + if (plainchar == plaintextend) + { + state_in->result = result; + state_in->step = step_C; + return codechar - code_out; + } + fragment = *plainchar++; + result |= (fragment & 0x0c0) >> 6; + *codechar++ = base64_encode_value(result); + result = (fragment & 0x03f) >> 0; + *codechar++ = base64_encode_value(result); + + ++(state_in->stepcount); + if (state_in->stepcount == CHARS_PER_LINE/4) + { + *codechar++ = '\n'; + state_in->stepcount = 0; + } + } + } + /* control should not reach here */ + return codechar - code_out; +} + +int base64_encode_blockend(char* code_out, base64_encodestate* state_in) +{ + char* codechar = code_out; + + switch (state_in->step) + { + case step_B: + *codechar++ = base64_encode_value(state_in->result); + *codechar++ = '='; + *codechar++ = '='; + break; + case step_C: + *codechar++ = base64_encode_value(state_in->result); + *codechar++ = '='; + break; + case step_A: + break; + } + *codechar++ = '\n'; + + return codechar - code_out; +} + diff --git a/fdbrpc/libb64/cencode.h b/fdbrpc/libb64/cencode.h new file mode 100755 index 0000000000..65ef75dcc1 --- /dev/null +++ b/fdbrpc/libb64/cencode.h @@ -0,0 +1,51 @@ +/* + * cencode.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* +cencode.h - c header for a base64 encoding algorithm + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#ifndef BASE64_CENCODE_H +#define BASE64_CENCODE_H + +typedef enum +{ + step_A, step_B, step_C +} base64_encodestep; + +typedef struct +{ + base64_encodestep step; + char result; + int stepcount; +} base64_encodestate; + +void base64_init_encodestate(base64_encodestate* state_in); + +char base64_encode_value(char value_in); + +int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in); + +int base64_encode_blockend(char* code_out, base64_encodestate* state_in); + +#endif /* BASE64_CENCODE_H */ diff --git a/fdbrpc/libb64/decode.h b/fdbrpc/libb64/decode.h new file mode 100755 index 0000000000..f04aa9fb14 --- /dev/null +++ b/fdbrpc/libb64/decode.h @@ -0,0 +1,90 @@ +/* + * decode.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// :mode=c++: +/* +decode.h - c++ wrapper for a base64 decoding algorithm + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ +#ifndef BASE64_DECODE_H +#define BASE64_DECODE_H + +#include + +namespace base64 +{ + extern "C" + { + #include "cdecode.h" + } + + struct decoder + { + base64_decodestate _state; + int _buffersize; + + decoder(int buffersize_in = BUFFERSIZE) + : _buffersize(buffersize_in) + {} + + int decode(char value_in) + { + return base64_decode_value(value_in); + } + + int decode(const char* code_in, const int length_in, char* plaintext_out) + { + return base64_decode_block(code_in, length_in, plaintext_out, &_state); + } + + void decode(std::istream& istream_in, std::ostream& ostream_in) + { + base64_init_decodestate(&_state); + // + const int N = _buffersize; + char* code = new char[N]; + char* plaintext = new char[N]; + int codelength; + int plainlength; + + do + { + istream_in.read((char*)code, N); + codelength = istream_in.gcount(); + plainlength = decode(code, codelength, plaintext); + ostream_in.write((const char*)plaintext, plainlength); + } + while (istream_in.good() && codelength > 0); + // + base64_init_decodestate(&_state); + + delete [] code; + delete [] plaintext; + } + }; + +} // namespace base64 + + + +#endif // BASE64_DECODE_H + diff --git a/fdbrpc/libb64/encode.h b/fdbrpc/libb64/encode.h new file mode 100755 index 0000000000..9b25935113 --- /dev/null +++ b/fdbrpc/libb64/encode.h @@ -0,0 +1,109 @@ +/* + * encode.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// :mode=c++: +/* +encode.h - c++ wrapper for a base64 encoding algorithm + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ +#ifndef BASE64_ENCODE_H +#define BASE64_ENCODE_H + +#include +#include + +#define BUFFERSIZE 8192 + +namespace base64 +{ + extern "C" + { + #include "cencode.h" + } + + struct encoder + { + base64_encodestate _state; + int _buffersize; + + encoder(int buffersize_in = BUFFERSIZE) + : _buffersize(buffersize_in) + {} + + int encode(char value_in) + { + return base64_encode_value(value_in); + } + + int encode(const char* code_in, const int length_in, char* plaintext_out) + { + return base64_encode_block(code_in, length_in, plaintext_out, &_state); + } + + int encode_end(char* plaintext_out) + { + return base64_encode_blockend(plaintext_out, &_state); + } + + void encode(std::istream& istream_in, std::ostream& ostream_in) + { + base64_init_encodestate(&_state); + // + const int N = _buffersize; + char* plaintext = new char[N]; + char* code = new char[2*N]; + int plainlength; + int codelength; + + do + { + istream_in.read(plaintext, N); + plainlength = istream_in.gcount(); + // + codelength = encode(plaintext, plainlength, code); + ostream_in.write(code, codelength); + } + while (istream_in.good() && plainlength > 0); + + codelength = encode_end(code); + ostream_in.write(code, codelength); + // + base64_init_encodestate(&_state); + + delete [] code; + delete [] plaintext; + } + + static std::string from_string(std::string s) + { + std::stringstream in(s); + std::stringstream out; + encoder enc; + enc.encode(in, out); + return out.str(); + } + }; + +} // namespace base64 + +#endif // BASE64_ENCODE_H + diff --git a/fdbrpc/libcoroutine/386-ucontext.h b/fdbrpc/libcoroutine/386-ucontext.h new file mode 100644 index 0000000000..024a54498c --- /dev/null +++ b/fdbrpc/libcoroutine/386-ucontext.h @@ -0,0 +1,121 @@ +#define setcontext(u) setmcontext(&(u)->uc_mcontext) +#define getcontext(u) getmcontext(&(u)->uc_mcontext) +typedef struct mcontext mcontext_t; +typedef struct ucontext ucontext_t; + +extern int swapcontext(ucontext_t*, const ucontext_t*); +extern void makecontext(ucontext_t*, void(*)(void), int, ...); +extern int getmcontext(mcontext_t*); +extern void setmcontext(const mcontext_t*); + +/*- + * Copyright (c) 1999 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD: src/sys/sys/ucontext.h,v 1.4 1999/10/11 20:33:17 luoqi Exp $ + */ + +/* #include */ + +/*- + * Copyright (c) 1999 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD: src/sys/i386/include/ucontext.h,v 1.4 1999/10/11 20:33:09 luoqi Exp $ + */ + +struct mcontext { + /* + * The first 20 fields must match the definition of + * sigcontext. So that we can support sigcontext + * and ucontext_t at the same time. + */ + int mc_onstack; /* XXX - sigcontext compat. */ + int mc_gs; + int mc_fs; + int mc_es; + int mc_ds; + int mc_edi; + int mc_esi; + int mc_ebp; + int mc_isp; + int mc_ebx; + int mc_edx; + int mc_ecx; + int mc_eax; + int mc_trapno; + int mc_err; + int mc_eip; + int mc_cs; + int mc_eflags; + int mc_esp; /* machine state */ + int mc_ss; + + int mc_fpregs[28]; /* env87 + fpacc87 + u_long */ + int __spare__[17]; +}; + +struct ucontext { + /* + * Keep the order of the first two fields. Also, + * keep them the first two fields in the structure. + * This way we can have a union with struct + * sigcontext and ucontext_t. This allows us to + * support them both at the same time. + * note: the union is not defined, though. + */ + sigset_t uc_sigmask; + mcontext_t uc_mcontext; + + struct __ucontext *uc_link; + stack_t uc_stack; + int __spare__[8]; +}; + + diff --git a/fdbrpc/libcoroutine/Base.h b/fdbrpc/libcoroutine/Base.h new file mode 100644 index 0000000000..c332ffb050 --- /dev/null +++ b/fdbrpc/libcoroutine/Base.h @@ -0,0 +1,13 @@ +#ifndef IOBASE_DEFINED +#define IOBASE_DEFINED 1 + +#include +#include +#include +#include +#include +#include +#include +#include + +#endif diff --git a/fdbrpc/libcoroutine/Common.c b/fdbrpc/libcoroutine/Common.c new file mode 100644 index 0000000000..840d290d30 --- /dev/null +++ b/fdbrpc/libcoroutine/Common.c @@ -0,0 +1,311 @@ +//metadoc Common copyright Steve Dekorte 2002 +//metadoc Common license BSD revised + +#include "Common.h" +#include + +#ifdef IO_CHECK_ALLOC + +static long allocs = 0; +static long reallocs = 0; +static long allocatedBytes = 0; +static long maxAllocatedBytes = 0; +static long frees = 0; + +/* +typedef struct +{ + long allocs = 0; + long reallocs = 0; + long allocatedBytes = 0; + long maxAllocatedBytes = 0; + long frees = 0; +} Allocator; + +Allocator *Allocator_new(void) +{ + Allocator *self = calloc(1, sizeof(Allocator)); + return self; +} + +void Allocator_free(Allocator *self) +{ + free(self); +} + +size_t Allocator_allocs(Allocator *self) +{ + return self->allocs; +} + +size_t Allocator_frees(Allocator *self) +{ + return self->frees; +} + +size_t Allocator_allocatedBytes(Allocator *self) +{ + return self->allocatedBytes; +} + +size_t Allocator_maxAllocatedBytes(Allocator *self) +{ + return self->maxAllocatedBytes; +} + +void Allocator_resetMaxAllocatedBytes(Allocator *self) +{ + self->maxAllocatedBytes = self->allocatedBytes; +} + +void Allocator_show(Allocator *self) +{ + printf("allocs %i\n", self->allocs); + printf("reallocs %i\n", self->reallocs); + printf("frees %i\n", self->frees); + printf("allocsMinusfrees %i\n", self->allocs - self->frees); + printf("allocatedBytes %i\n", self->allocatedBytes); + printf("maxAllocatedBytes %i\n", self->maxAllocatedBytes); + //printf("allocs %i bytes %i\n", self->allocs, self->allocatedBytes); +} + +static Allocator *_globalAllocator; + +Allocator *globalAllocator(void) +{ + if(!_globalAllocator) Allocator_new(); + return _globalAllocator; +} +*/ + +// ------------------------------------------------------- + +typedef struct MemoryBlock MemoryBlock; + +struct MemoryBlock +{ + size_t size; + size_t allocNum; + char *file; + int line; + MemoryBlock *next; + MemoryBlock *prev; + char padding[40 - (sizeof(size_t) + sizeof(size_t) + sizeof(char *) + sizeof(int) + sizeof(void *) + sizeof(void *))]; +}; + +MemoryBlock *PtrToMemoryBlock(void *ptr) +{ + return (MemoryBlock *)(((char *)ptr) - sizeof(MemoryBlock)); +} + +void *MemoryBlockToPtr(MemoryBlock *self) +{ + return (void *)(((char *)self) + sizeof(MemoryBlock)); +} + +static MemoryBlock *_baseblock = NULL; + +//inline +MemoryBlock *baseblock(void) +{ + if(!_baseblock) _baseblock = calloc(1, sizeof(MemoryBlock)); + return _baseblock; +} + +void MemoryBlock_remove(MemoryBlock *self) +{ + if (self->next) self->next->prev = self->prev; + if (self->prev) self->prev->next = self->next; +} + +void MemoryBlock_insertAfter_(MemoryBlock *self, MemoryBlock *other) +{ + self->next = other->next; + self->prev = other; + other->next = self; + if (self->next) self->next->prev = self; +} + +MemoryBlock *MemoryBlock_newWithSize_file_line_(size_t size, char *file, int line) +{ + MemoryBlock *self = calloc(1, sizeof(MemoryBlock) + size); + self->size = size; + self->allocNum = allocs; + self->file = file; + self->line = line; + MemoryBlock_insertAfter_(self, baseblock()); + + allocs ++; + allocatedBytes += size; + if (allocatedBytes > maxAllocatedBytes) maxAllocatedBytes = allocatedBytes; + return self; +} + +MemoryBlock *MemoryBlock_reallocToSize_(MemoryBlock *self, size_t size) +{ + MemoryBlock *prev = self->prev; + MemoryBlock_remove(self); + allocatedBytes -= self->size; + allocatedBytes += size; + reallocs ++; + self = realloc(self, sizeof(MemoryBlock) + size); + self->size = size; + MemoryBlock_insertAfter_(self, prev); + return self; +} + +void MemoryBlock_free(MemoryBlock *self) +{ + MemoryBlock_remove(self); + allocatedBytes -= self->size; + frees ++; + free(self); +} + +size_t MemoryBlock_size(MemoryBlock *self) +{ + return self->size; +} + +void MemoryBlock_show(MemoryBlock *self) +{ + char *file = strrchr(self->file, '/'); + file = file ? file + 1 : self->file; + //printf(" MemoryBlock %p:\n", (void *)self); + //printf("\tsize %i\n", self->size); + //printf("\tfile %s\n", file); + //printf("\tline %i\n", self->line); + printf("\t%i %p %s:%i\t\t%i bytes\n", self->allocNum, MemoryBlockToPtr(self), file, self->line, self->size); +} + +// ---------------------------------------------------------------------------- + +void *io_real_malloc(size_t size, char *file, int line) +{ + MemoryBlock *m = MemoryBlock_newWithSize_file_line_(size, file, line); + return MemoryBlockToPtr(m); +} + +void *io_real_calloc(size_t count, size_t size, char *file, int line) +{ + return io_real_malloc(count * size, file, line); +} + +void *io_real_realloc(void *ptr, size_t size, char *file, int line) +{ + if (ptr) + { + MemoryBlock *m = MemoryBlock_reallocToSize_(PtrToMemoryBlock(ptr), size); + return MemoryBlockToPtr(m); + } + + return io_real_malloc(size, file, line); +} + +void io_free(void *ptr) +{ + MemoryBlock_free(PtrToMemoryBlock(ptr)); +} + +// -------------------------------------------------------------------------- + +void io_show_mem(char *s) +{ + printf("\n--- %s ---\n", s ? s : ""); + printf("allocs %i\n", allocs); + printf("reallocs %i\n", reallocs); + printf("frees %i\n", frees); + printf("allocsMinusfrees %i\n", allocs - frees); + printf("allocatedBytes %i\n", allocatedBytes); + printf("maxAllocatedBytes %i\n", maxAllocatedBytes); + //printf("allocs %i bytes %i\n", allocs, allocatedBytes); + //printf("\n"); +} + +size_t io_maxAllocatedBytes(void) +{ + return maxAllocatedBytes; +} + +void io_resetMaxAllocatedBytes(void) +{ + maxAllocatedBytes = allocatedBytes; +} + +size_t io_frees(void) +{ + return frees; +} + +size_t io_allocatedBytes(void) +{ + return allocatedBytes; +} + +size_t io_allocs(void) +{ + return allocs; +} + +void io_showUnfreed(void) +{ + MemoryBlock *m = baseblock()->next; + size_t sum = 0; + int n = 0; + + while (m) + { + MemoryBlock_show(m); + sum += m->size; + n ++; + m = m->next; + } + + printf("\n %i bytes in %i blocks\n", (int)sum, n); +} + +#endif + +void *cpalloc(const void *p, size_t size) +{ + void *n = io_malloc(size); + if(p) memcpy(n, p, size); + return n; +} + +void *io_freerealloc(void *p, size_t size) +{ + return realloc(p, size); + /* + void *n = io_malloc(size); + + if (p != NULL) + { + memcpy(n, p, size); + free(p); + } + + return n; + */ +} + +int io_isBigEndian(void) +{ + int i = 0x1; + uint8_t *s = (uint8_t *)(&i); + return s[0]; +} + +uint32_t io_uint32InBigEndian(uint32_t i) +{ + uint32_t o; + uint8_t *os = (uint8_t *)&o; + uint8_t *is = (uint8_t *)&i; + if (io_isBigEndian()) return i; + os[0] = is[3]; + os[1] = is[2]; + os[2] = is[1]; + os[3] = is[0]; + return o; +} + diff --git a/fdbrpc/libcoroutine/Common.h b/fdbrpc/libcoroutine/Common.h new file mode 100644 index 0000000000..3e43637498 --- /dev/null +++ b/fdbrpc/libcoroutine/Common.h @@ -0,0 +1,197 @@ + +//metadoc Common copyright Steve Dekorte 2002 +//metadoc Common license BSD revised +/*metadoc Common description +This is a header that all other source files should include. +These defines are helpful for doing OS specific checks in the code. + */ + + +#ifndef IOCOMMON_DEFINED +#define IOCOMMON_DEFINED 1 + +/*#define LOW_MEMORY_SYSTEM 1*/ +#include +#include +#include + + +#if defined (__SVR4) && defined (__sun) +#include +#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) +#include +#elif !defined(__SYMBIAN32__) && !defined(__NeXT__) +#include +#else +typedef unsigned char uint8_t; +typedef signed char int8_t; +typedef unsigned short uint16_t; +typedef signed short int16_t; +typedef unsigned long uint32_t; +typedef signed long int32_t; +/* + typedef unsigned long uint64_t; + typedef signed long int64_t; + */ +typedef unsigned long long uint64_t; +typedef long long int64_t; +#endif + +/* Windows stuff */ + +#if defined(WIN32) || defined(__WINS__) || defined(__MINGW32__) || defined(_MSC_VER) +#define inline __inline +//#define snprintf _snprintf +#define usleep(x) Sleep(((x)+999)/1000) + +#define HAS_FIBERS 1 + +#define ON_WINDOWS 1 + +// this also includes windows.h +#include + +// Enable fibers +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0400 +#endif + +#if !defined(__MINGW32__) && 0 +#if defined(BUILDING_BASEKIT_DLL) || defined(BUILDING_IOVMALL_DLL) +#define BASEKIT_API __declspec(dllexport) +#else +#define BASEKIT_API __declspec(dllimport) +#endif +#else +#define BASEKIT_API +#endif +/* +#ifndef _SYS_STDINT_H_ +#include "PortableStdint.h" +#endif + */ + +#if !defined(__MINGW32__) +/* disable compile warnings which are always treated +as errors in my dev settings */ + +#pragma warning( disable : 4244 ) +/* warning C4244: 'function' : conversion from 'double ' to 'int ', possible loss of data */ + +#pragma warning( disable : 4996 ) +/* warning C4996: 'function' : This function or variable may be unsafe. Consider using 'function_s' instead */ + +#pragma warning( disable : 4018 ) +/* warning C4018: 'operator' : signed/unsigned mismatch */ + +/*#pragma warning( disable : 4090 ) */ +/* warning C4090: 'function' : different 'const' qualifiers */ + +/*#pragma warning( disable : 4024 )*/ +/* warning C4024: different types for formal and actual parameter */ + +/*#pragma warning( disable : 4761 ) */ +/* warning C4761: integral size mismatch in argument; conversion supplied */ + +/*#pragma warning( disable : 4047 ) */ +/* warning C4047: '=' : 'char *' differs in levels of indirection from 'int ' */ +#define ARCHITECTURE_x86 1 +#endif + +/* io_malloc, io_realloc, io_free undefined */ +#if !defined(__SYMBIAN32__) +#include + +/* strlen undefined */ +#include +#include /* for calloc */ +#endif +#else + +// Not on windows so define this away +#define BASEKIT_API + +#endif + +/* + [DBCS Enabling] + + DBCS (Short for Double-Byte Character Set), a character set that uses two-byte (16-bit) characters. Some languages, such as Chinese, Japanese and Korean (CJK), have writing schemes with many different characters that cannot be represented with single-byte codes such as ASCII and EBCDIC. + + In CJK world, CES (Character Encoding Scheme) and CCS (Coded Character Set) are actually different concept(one CES may contain multiple CCS). + For example, EUC-JP is a CES which includes CCS of ASCII and JIS X 0208 (optionally JIS X 0201 Kana and JIS X 0212). + + In Japanese (because I am Japanese), + While EUC-JP and UTF-8 Map ASCII unchanged, ShiftJIS not (However ShiftJIS is de facto standard in Japan). For example, {0x95, 0x5c} represents one character. in ASCII, second byte(0x5c) is back slash character. + */ + +/* + check whether double-byte character. supported only ShiftJIS. + if you want to use ShiftJIS characters in string literal, set compiler option -DDBCS_ENABLED=1. + */ + +#if DBCS_ENABLED +#define ismbchar(c) ISSJIS((unsigned char)c) +#define mbcharlen(c) 2 +#define ISSJIS(c) ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)) +#else +#define ismbchar(c) 0 +#define mbcharlen(c) 1 +#endif /* DBCS_ENABLED */ + +#ifdef __cplusplus +extern "C" { +#endif + +//#define IO_CHECK_ALLOC ENABLED(NOT_IN_CLEAN) + +#ifdef IO_CHECK_ALLOC + BASEKIT_API size_t io_memsize(void *ptr); + + #define io_malloc(size) io_real_malloc(size, __FILE__, __LINE__) + BASEKIT_API void *io_real_malloc(size_t size, char *file, int line); + + #define io_calloc(count, size) io_real_calloc(count, size, __FILE__, __LINE__) + BASEKIT_API void *io_real_calloc(size_t count, size_t size, char *file, int line); + + #define io_realloc(ptr, size) io_real_realloc(ptr, size, __FILE__, __LINE__) + BASEKIT_API void *io_real_realloc(void *ptr, size_t newSize, char *file, int line); + + BASEKIT_API void io_free(void *ptr); + BASEKIT_API void io_show_mem(char *s); + BASEKIT_API size_t io_maxAllocatedBytes(void); + BASEKIT_API void io_resetMaxAllocatedBytes(void); + BASEKIT_API size_t io_frees(void); + BASEKIT_API size_t io_allocs(void); + BASEKIT_API size_t io_allocatedBytes(void); + + BASEKIT_API void io_showUnfreed(void); +#else + #define io_memsize + #define io_malloc malloc + #define io_calloc calloc + #define io_realloc io_freerealloc + #define io_free free + #define io_show_mem + + #define io_maxAllocatedBytes() 0 + #define io_frees() 0 + #define io_allocs() 0 + #define io_allocatedBytes() 0 + #define io_resetMaxAllocatedBytes() +#endif + +BASEKIT_API void *cpalloc(const void *p, size_t size); +BASEKIT_API void *io_freerealloc(void *p, size_t size); + +int io_isBigEndian(void); +BASEKIT_API uint32_t io_uint32InBigEndian(uint32_t i); + +#ifdef __cplusplus +} +#endif + +#endif + + + diff --git a/fdbrpc/libcoroutine/Coro.c b/fdbrpc/libcoroutine/Coro.c new file mode 100644 index 0000000000..cbfdc8fde0 --- /dev/null +++ b/fdbrpc/libcoroutine/Coro.c @@ -0,0 +1,800 @@ +/* + Credits + + Originally based on Edgar Toernig's Minimalistic cooperative multitasking + http://www.goron.de/~froese/ + reorg by Steve Dekorte and Chis Double + Symbian and Cygwin support by Chis Double + Linux/PCC, Linux/Opteron, Irix and FreeBSD/Alpha, ucontext support by Austin Kurahone + FreeBSD/Intel support by Faried Nawaz + Mingw support by Pit Capitain + Visual C support by Daniel Vollmer + Solaris support by Manpreet Singh + Fibers support by Jonas Eschenburg + Ucontext arg support by Olivier Ansaldi + Ucontext x86-64 support by James Burgess and Jonathan Wright + Russ Cox for the newer portable ucontext implementions. + + Notes + + This is the system dependent coro code. + Setup a jmp_buf so when we longjmp, it will invoke 'func' using 'stack'. + Important: 'func' must not return! + + Usually done by setting the program counter and stack pointer of a new, empty stack. + If you're adding a new platform, look in the setjmp.h for PC and SP members + of the stack structure + + If you don't see those members, Kentaro suggests writting a simple + test app that calls setjmp and dumps out the contents of the jmp_buf. + (The PC and SP should be in jmp_buf->__jmpbuf). + + Using something like GDB to be able to peek into register contents right + before the setjmp occurs would be helpful also. + */ + +#include "Common.h" +#include "flow/Platform.h" +#include "Base.h" +#include "Coro.h" +#include +#include +#include +#include + +#include + +#ifndef WIN32 +#include "taskimpl.h" +#endif + +#ifdef USE_VALGRIND +#include +#define STACK_REGISTER(coro) \ +{ \ + Coro *c = (coro); \ + c->valgrindStackId = VALGRIND_STACK_REGISTER( \ + c->stack, \ + c->stack + c->requestedStackSize); \ +} + +#define STACK_DEREGISTER(coro) \ +VALGRIND_STACK_DEREGISTER((coro)->valgrindStackId) + +#else +#define STACK_REGISTER(coro) +#define STACK_DEREGISTER(coro) +#endif + +// Define outside +extern intptr_t g_stackYieldLimit; + +typedef struct CallbackBlock +{ + void *context; + CoroStartCallback *func; +} CallbackBlock; + +static CallbackBlock globalCallbackBlock; + +Coro *Coro_new(void) +{ + Coro *self = (Coro *)io_calloc(1, sizeof(Coro)); + if (self == NULL) + { + errno = ENOMEM; + return NULL; + } + + self->requestedStackSize = CORO_DEFAULT_STACK_SIZE; + self->allocatedStackSize = 0; + +#ifdef USE_FIBERS + self->fiber = NULL; +#else + self->stack = NULL; +#endif + return self; +} + +int Coro_allocStackIfNeeded(Coro *self) +{ + if (self->stack && self->requestedStackSize < self->allocatedStackSize) + { + io_free(self->stack); + self->stack = NULL; + self->requestedStackSize = 0; + } + + if (!self->stack) + { + self->stack = (void *)io_calloc(1, self->requestedStackSize + 16); + if (self->stack == NULL) + { + errno = ENOMEM; + return ENOMEM; + } + + self->allocatedStackSize = self->requestedStackSize; + //printf("Coro_%p allocating stack size %i\n", (void *)self, self->requestedStackSize); + STACK_REGISTER(self); + } + + return 0; +} + +void Coro_free(Coro *self) +{ +#ifdef USE_FIBERS + // If this coro has a fiber, delete it. + // Don't delete the main fiber. We don't want to commit suicide. + if (self->fiber && !self->isMain) + { + DeleteFiber(self->fiber); + } +#else + STACK_DEREGISTER(self); +#endif + if (self->stack) + { + io_free(self->stack); + } + + //printf("Coro_%p io_free\n", (void *)self); + + io_free(self); +} + +// stack + +void *Coro_stack(Coro *self) +{ + return self->stack; +} + +size_t Coro_stackSize(Coro *self) +{ + return self->requestedStackSize; +} + +void Coro_setStackSize_(Coro *self, size_t sizeInBytes) +{ + self->requestedStackSize = sizeInBytes; + //self->stack = (void *)io_realloc(self->stack, sizeInBytes); + //printf("Coro_%p io_reallocating stack size %i\n", (void *)self, sizeInBytes); +} + +#if __GNUC__ == 4 +uint8_t *Coro_CurrentStackPointer(void) __attribute__ ((noinline)); +#endif + +uint8_t *Coro_CurrentStackPointer(void) +{ + uint8_t a; + uint8_t *b = &a; // to avoid compiler warning about unused variables + return b; +} + +size_t Coro_bytesLeftOnStack(Coro *self) +{ + unsigned char dummy; + ptrdiff_t p1 = (ptrdiff_t)(&dummy); + /*ptrdiff_t p2 = (ptrdiff_t)Coro_CurrentStackPointer(); + int stackMovesUp = p2 > p1;*/ + + ptrdiff_t start = ((ptrdiff_t)self->stack); + ptrdiff_t end = start + self->requestedStackSize; + + if (/*stackMovesUp*/ 0) + { + return end - p1; + } + else + { + return p1 - start; + } +} + +int Coro_stackSpaceAlmostGone(Coro *self) +{ + return Coro_bytesLeftOnStack(self) < CORO_STACK_SIZE_MIN; +} + +void Coro_initializeMainCoro(Coro *self) +{ + self->isMain = 1; + + // So that Coro_bytesLeftOnStack etc will do something on the main thread, guess that there is 800KB of stack + // "left" when this function is called. + + // FIXME: The commented code below could be adapted to find the actual stack size using pthreads + self->requestedStackSize = 800 << 10; + self->stack = (char*)&self - self->requestedStackSize; + self->allocatedStackSize = 0; + g_stackYieldLimit = (intptr_t) self->stack + 65536; + /* // Get the pthread attributes + pthread_attr_t Attributes; + void *StackAddress; + size_t StackSize; + memset (&Attributes, 0, sizeof (Attributes)); + pthread_getattr_np (pthread_self(), &Attributes); + + // From the attributes, get the stack info + pthread_attr_getstack (&Attributes, &StackAddress, &StackSize); + + // Done with the attributes + pthread_attr_destroy (&Attributes); + + printf ("Stack top: %p\n", StackAddress); + printf ("Stack size: %u bytes\n", StackSize); + printf ("Stack bottom: %p\n", (uint8_t*)StackAddress + StackSize);*/ + + +#ifdef USE_FIBERS + // We must convert the current thread into a fiber if it hasn't already been done. + if ((LPVOID) 0x1e00 == GetCurrentFiber()) // value returned when not a fiber + { + // Make this thread a fiber and set its data field to the main coro's address + ConvertThreadToFiber(self); + } + // Make the main coro represent the current fiber + self->fiber = GetCurrentFiber(); +#endif +} + +int Coro_startCoro_(Coro *self, Coro *other, void *context, CoroStartCallback *callback) +{ + CallbackBlock sblock; + int result; + + CallbackBlock *block = &sblock; + //CallbackBlock *block = malloc(sizeof(CallbackBlock)); // memory leak + block->context = context; + block->func = callback; + + result = Coro_allocStackIfNeeded(other); + if (result) + return result; + + Coro_setup(other, block); + Coro_switchTo_(self, other); + + return 0; +} + +/* +int Coro_startCoro_(Coro *self, Coro *other, void *context, CoroStartCallback *callback) +{ + int result; + globalCallbackBlock.context = context; + globalCallbackBlock.func = callback; + result = Coro_allocStackIfNeeded(other); + if (result) + return result; + + Coro_setup(other, &globalCallbackBlock); + Coro_switchTo_(self, other); + return 0; +} +*/ + +#if defined(USE_UCONTEXT) && defined(__x86_64__) +void Coro_StartWithArg(unsigned int hiArg, unsigned int loArg) +{ + setProfilingEnabled(1); + CallbackBlock *block = (CallbackBlock*)(((long long)hiArg << 32) | (long long)loArg); + (block->func)(block->context); + criticalError(FDB_EXIT_ABORT, "SchedulerError", "returned from coro start function"); +} + +/* +void Coro_Start(void) +{ + CallbackBlock block = globalCallbackBlock; + unsigned int hiArg = (unsigned int)(((long long)&block) >> 32); + unsigned int loArg = (unsigned int)(((long long)&block) & 0xFFFFFFFF); + Coro_StartWithArg(hiArg, loArg); +} +*/ +#else +void Coro_StartWithArg(CallbackBlock *block) +{ + setProfilingEnabled(1); + (block->func)(block->context); + criticalError(FDB_EXIT_ABORT, "SchedulerError", "returned from coro start function"); +} + +void Coro_Start(void) +{ + CallbackBlock block = globalCallbackBlock; + Coro_StartWithArg(&block); +} + +#endif + +// -------------------------------------------------------------------- + +void Coro_switchTo_(Coro *self, Coro *next) +{ + g_stackYieldLimit = (intptr_t) next->stack + 65536; +#if defined(__SYMBIAN32__) + ProcessUIEvent(); +#elif defined(USE_FIBERS) + SwitchToFiber(next->fiber); +#elif defined(USE_UCONTEXT) + setProfilingEnabled(0); + swapcontext(&self->env, &next->env); + setProfilingEnabled(1); +#elif defined(USE_SETJMP) + if (setjmp(self->env) == 0) + { + longjmp(next->env, 1); + } +#endif +} + +// ---- setup ------------------------------------------ + +#if defined(USE_SETJMP) && defined(__x86_64__) + +void Coro_setup(Coro *self, void *arg) +{ + /* since ucontext seems to be broken on amg64 */ + + setjmp(self->env); + /* This is probably not nice in that it deals directly with + * something with __ in front of it. + * + * Anyhow, Coro.h makes the member env of a struct Coro a + * jmp_buf. A jmp_buf, as defined in the amd64 setjmp.h + * is an array of one struct that wraps the actual __jmp_buf type + * which is the array of longs (on a 64 bit machine) that + * the programmer below expected. This struct begins with + * the __jmp_buf array of longs, so I think it was supposed + * to work like he originally had it, but for some reason + * it didn't. I don't know why. + * - Bryce Schroeder, 16 December 2006 + * + * Explaination of `magic' numbers: 6 is the stack pointer + * (RSP, the 64 bit equivalent of ESP), 7 is the program counter. + * This information came from this file on my Gentoo linux + * amd64 computer: + * /usr/include/gento-multilib/amd64/bits/setjmp.h + * Which was ultimatly included from setjmp.h in /usr/include. */ + self->env[0].__jmpbuf[6] = ((unsigned long)(Coro_stack(self))); + self->env[0].__jmpbuf[7] = ((long)Coro_Start); +} + +#elif defined(HAS_UCONTEXT_ON_PRE_SOLARIS_10) + +typedef void (*makecontext_func)(void); + +void Coro_setup(Coro *self, void *arg) +{ + ucontext_t *ucp = (ucontext_t *) &self->env; + + getcontext(ucp); + + ucp->uc_stack.ss_sp = Coro_stack(self) + Coro_stackSize(self) - 8; + ucp->uc_stack.ss_size = Coro_stackSize(self); + ucp->uc_stack.ss_flags = 0; + ucp->uc_link = NULL; + + makecontext(ucp, (makecontext_func)Coro_StartWithArg, 1, arg); } + + +#elif defined(USE_UCONTEXT) + +typedef void (*makecontext_func)(void); + +void Coro_setup(Coro *self, void *arg) +{ + ucontext_t *ucp = (ucontext_t *) &self->env; + + getcontext(ucp); + + ucp->uc_stack.ss_sp = Coro_stack(self); + ucp->uc_stack.ss_size = Coro_stackSize(self); +#if !defined(__APPLE__) + ucp->uc_stack.ss_flags = 0; + ucp->uc_link = NULL; +#endif + +#if defined(__x86_64__) + unsigned int hiArg = (unsigned int)((long long)arg >> 32); + unsigned int loArg = (unsigned int)((long long)arg & 0xFFFFFFFF); + makecontext(ucp, (makecontext_func)Coro_StartWithArg, 2, hiArg, loArg); +#else + makecontext(ucp, (makecontext_func)Coro_StartWithArg, 1, arg); +#endif +} + +#elif defined(USE_FIBERS) + +void Coro_setup(Coro *self, void *arg) +{ + // If this coro was recycled and already has a fiber, delete it. + // Don't delete the main fiber. We don't want to commit suicide. + + if (self->fiber && !self->isMain) + { + DeleteFiber(self->fiber); + } + + self->fiber = CreateFiber(Coro_stackSize(self), + (LPFIBER_START_ROUTINE)Coro_StartWithArg, + (LPVOID)arg); + if (!self->fiber) + criticalError(FDB_EXIT_ABORT, "SchedulerError", "unable to create fiber"); +} + +#elif defined(__CYGWIN__) + +#define buf (self->env) + +void Coro_setup(Coro *self, void *arg) +{ + setjmp(buf); + buf[7] = (long)(Coro_stack(self) + Coro_stackSize(self) - 16); + buf[8] = (long)Coro_Start; + globalCallbackBlock.context=((CallbackBlock*)arg)->context; + globalCallbackBlock.func=((CallbackBlock*)arg)->func; +} + +#elif defined(__SYMBIAN32__) + +void Coro_setup(Coro *self, void *arg) +{ + /* + setjmp/longjmp is flakey under Symbian. + If the setjmp is done inside the call then a crash occurs. + Inlining it here solves the problem + */ + + setjmp(self->env); + self->env[0] = 0; + self->env[1] = 0; + self->env[2] = 0; + self->env[3] = (unsigned long)(Coro_stack(self)) + + Coro_stackSize(self) - 64; + self->env[9] = (long)Coro_Start; + self->env[8] = self->env[3] + 32; +} + +#elif defined(_BSD_PPC_SETJMP_H_) + +#define buf (self->env) +#define setjmp _setjmp +#define longjmp _longjmp + +void Coro_setup(Coro *self, void *arg) +{ + size_t *sp = (size_t *)(((intptr_t)Coro_stack(self) + + Coro_stackSize(self) - 64 + 15) & ~15); + + setjmp(buf); + + //printf("self = %p\n", self); + //printf("sp = %p\n", sp); + buf[0] = (long)sp; + buf[21] = (long)Coro_Start; + globalCallbackBlock.context=((CallbackBlock*)arg)->context; + globalCallbackBlock.func=((CallbackBlock*)arg)->func; + //sp[-4] = (size_t)self; // for G5 10.3 + //sp[-6] = (size_t)self; // for G4 10.4 + + //printf("self = %p\n", (void *)self); + //printf("sp = %p\n", sp); +} + +/* +void Coro_setup(Coro *self, void *arg) +{ + size_t *sp = (size_t *)(((intptr_t)Coro_stack(self) + + Coro_stackSize(self) - 64 + 15) & ~15); + + setjmp(buf); + + //printf("self = %p\n", self); + //printf("sp = %p\n", sp); + buf[0] = (long)sp; + buf[21] = (long)Coro_Start; + //sp[-4] = (size_t)self; // for G5 10.3 + //sp[-6] = (size_t)self; // for G4 10.4 + + //printf("self = %p\n", (void *)self); + //printf("sp = %p\n", sp); +} +*/ + +#elif defined(__DragonFly__) + +#define buf (self->env) + +void Coro_setup(Coro *self, void *arg) +{ + void *stack = Coro_stack(self); + size_t stacksize = Coro_stackSize(self); + void *func = (void *)Coro_Start; + + setjmp(buf); + + buf->_jb[2] = (long)(stack + stacksize); + buf->_jb[0] = (long)func; + return; +} + +#elif defined(__arm__) +// contributed by Peter van Hardenberg + +#define buf (self->env) + +void Coro_setup(Coro *self, void *arg) +{ + setjmp(buf); + buf[8] = (int)Coro_stack(self) + (int)Coro_stackSize(self) - 16; + buf[9] = (int)Coro_Start; +} + +#else + +#error "Coro.c Error: Coro_setup() function needs to be defined for this platform." + +#endif + + +// old code + +/* + // APPLE coros are handled by PortableUContext now +#elif defined(_BSD_PPC_SETJMP_H_) + +#define buf (self->env) +#define setjmp _setjmp +#define longjmp _longjmp + + void Coro_setup(Coro *self, void *arg) + { + size_t *sp = (size_t *)(((intptr_t)Coro_stack(self) + Coro_stackSize(self) - 64 + 15) & ~15); + + setjmp(buf); + + //printf("self = %p\n", self); + //printf("sp = %p\n", sp); + buf[0] = (int)sp; + buf[21] = (int)Coro_Start; + //sp[-4] = (size_t)self; // for G5 10.3 + //sp[-6] = (size_t)self; // for G4 10.4 + + //printf("self = %p\n", (void *)self); + //printf("sp = %p\n", sp); + } + +#elif defined(_BSD_I386_SETJMP_H) + +#define buf (self->env) + + void Coro_setup(Coro *self, void *arg) + { + size_t *sp = (size_t *)((intptr_t)Coro_stack(self) + Coro_stackSize(self)); + + setjmp(buf); + + buf[9] = (int)(sp); // esp + buf[12] = (int)Coro_Start; // eip + //buf[8] = 0; // ebp + } + */ + +/* Solaris supports ucontext - so we don't need this stuff anymore + +void Coro_setup(Coro *self, void *arg) +{ + // this bit goes before the setjmp call + // Solaris 9 Sparc with GCC +#if defined(__SVR4) && defined (__sun) +#if defined(_JBLEN) && (_JBLEN == 12) && defined(__sparc) +#if defined(_LP64) || defined(_I32LPx) +#define JBTYPE long + JBTYPE x; +#else +#define JBTYPE int + JBTYPE x; + asm("ta 3"); // flush register window +#endif + +#define SUN_STACK_END_INDEX 1 +#define SUN_PROGRAM_COUNTER 2 +#define SUN_STACK_START_INDEX 3 + + // Solaris 9 i386 with GCC +#elif defined(_JBLEN) && (_JBLEN == 10) && defined(__i386) +#if defined(_LP64) || defined(_I32LPx) +#define JBTYPE long + JBTYPE x; +#else +#define JBTYPE int + JBTYPE x; +#endif +#define SUN_PROGRAM_COUNTER 5 +#define SUN_STACK_START_INDEX 3 +#define SUN_STACK_END_INDEX 4 +#endif +#endif + */ + +/* Irix supports ucontext - so we don't need this stuff anymore + +#elif defined(sgi) && defined(_IRIX4_SIGJBLEN) // Irix/SGI + +void Coro_setup(Coro *self, void *arg) +{ + setjmp(buf); + buf[JB_SP] = (__uint64_t)((char *)stack + stacksize - 8); + buf[JB_PC] = (__uint64_t)Coro_Start; +} +*/ + +/* Linux supports ucontext - so we don't need this stuff anymore + +#elif defined(linux) +// Various flavors of Linux. +#if defined(JB_GPR1) +// Linux/PPC +buf->__jmpbuf[JB_GPR1] = ((int) stack + stacksize - 64 + 15) & ~15; +buf->__jmpbuf[JB_LR] = (int) Coro_Start; +return; + +#elif defined(JB_RBX) +// Linux/Opteron +buf->__jmpbuf[JB_RSP] = (long int )stack + stacksize; +buf->__jmpbuf[JB_PC] = Coro_Start; +return; + +#elif defined(JB_SP) + +// Linux/x86 with glibc2 +buf->__jmpbuf[JB_SP] = (int)stack + stacksize; +buf->__jmpbuf[JB_PC] = (int)Coro_StartWithArg; +// Push the argument on the stack (stack grows downwards) +// note: stack is stacksize + 16 bytes long +((int *)stack)[stacksize/sizeof(int) + 1] = (int)self; +return; + +#elif defined(_I386_JMP_BUF_H) +// x86-linux with libc5 +buf->__sp = (int)stack + stacksize; +buf->__pc = Coro_Start; +return; + +#elif defined(__JMP_BUF_SP) +// arm-linux on the sharp zauras +buf->__jmpbuf[__JMP_BUF_SP] = (int)stack + stacksize; +buf->__jmpbuf[__JMP_BUF_SP+1] = (int)Coro_Start; +return; + +#else + +*/ + + +/* Windows supports fibers - so we don't need this stuff anymore + +#elif defined(__MINGW32__) + +void Coro_setup(Coro *self, void *arg) +{ + setjmp(buf); + buf[4] = (int)((unsigned char *)stack + stacksize - 16); // esp + buf[5] = (int)Coro_Start; // eip +} + +#elif defined(_MSC_VER) + +void Coro_setup(Coro *self, void *arg) +{ + setjmp(buf); + // win32 visual c + // should this be the same as __MINGW32__? + buf[4] = (int)((unsigned char *)stack + stacksize - 16); // esp + buf[5] = (int)Coro_Start; // eip +} +*/ + + +/* FreeBSD supports ucontext - so we don't need this stuff anymore + +#elif defined(__FreeBSD__) +// FreeBSD. +#if defined(_JBLEN) && (_JBLEN == 81) +// FreeBSD/Alpha +buf->_jb[2] = (long)Coro_Start; // sc_pc +buf->_jb[26+4] = (long)Coro_Start; // sc_regs[R_RA] +buf->_jb[27+4] = (long)Coro_Start; // sc_regs[R_T12] +buf->_jb[30+4] = (long)(stack + stacksize); // sc_regs[R_SP] +return; + +#elif defined(_JBLEN) +// FreeBSD on IA32 +buf->_jb[2] = (long)(stack + stacksize); +buf->_jb[0] = (long)Coro_Start; +return; + +#else +#error Unsupported platform +#endif +*/ + +/* NetBSD supports ucontext - so we don't need this stuff anymore + +#elif defined(__NetBSD__) + +void Coro_setup(Coro *self, void *arg) +{ + setjmp(buf); +#if defined(_JB_ATTRIBUTES) + // NetBSD i386 + buf[2] = (long)(stack + stacksize); + buf[0] = (long)Coro_Start; +#else +#error Unsupported platform +#endif +} +*/ + +/* Sun supports ucontext - so we don't need this stuff anymore + +// Solaris supports ucontext - so we don't need this stuff anymore + +void Coro_setup(Coro *self, void *arg) +{ + // this bit goes before the setjmp call + // Solaris 9 Sparc with GCC +#if defined(__SVR4) && defined (__sun) +#if defined(_JBLEN) && (_JBLEN == 12) && defined(__sparc) +#if defined(_LP64) || defined(_I32LPx) +#define JBTYPE long + JBTYPE x; +#else +#define JBTYPE int + JBTYPE x; + asm("ta 3"); // flush register window +#endif + +#define SUN_STACK_END_INDEX 1 +#define SUN_PROGRAM_COUNTER 2 +#define SUN_STACK_START_INDEX 3 + + // Solaris 9 i386 with GCC +#elif defined(_JBLEN) && (_JBLEN == 10) && defined(__i386) +#if defined(_LP64) || defined(_I32LPx) +#define JBTYPE long + JBTYPE x; +#else +#define JBTYPE int + JBTYPE x; +#endif +#define SUN_PROGRAM_COUNTER 5 +#define SUN_STACK_START_INDEX 3 +#define SUN_STACK_END_INDEX 4 +#endif +#endif + + +#elif defined(__SVR4) && defined(__sun) + // Solaris +#if defined(SUN_PROGRAM_COUNTER) + // SunOS 9 + buf[SUN_PROGRAM_COUNTER] = (JBTYPE)Coro_Start; + + x = (JBTYPE)stack; + while ((x % 8) != 0) x --; // align on an even boundary + buf[SUN_STACK_START_INDEX] = (JBTYPE)x; + x = (JBTYPE)((JBTYPE)stack-stacksize / 2 + 15); + while ((x % 8) != 0) x ++; // align on an even boundary + buf[SUN_STACK_END_INDEX] = (JBTYPE)x; + + */ + + + diff --git a/fdbrpc/libcoroutine/Coro.h b/fdbrpc/libcoroutine/Coro.h new file mode 100644 index 0000000000..b09ff129b3 --- /dev/null +++ b/fdbrpc/libcoroutine/Coro.h @@ -0,0 +1,122 @@ +/* +*/ + +#ifndef CORO_DEFINED +#define CORO_DEFINED 1 +#pragma once + +#ifndef WIN32 +#include "taskimpl.h" +#endif + +#if defined(__SYMBIAN32__) + #define CORO_STACK_SIZE 8192 + #define CORO_STACK_SIZE_MIN 1024 +#else + //#define CORO_DEFAULT_STACK_SIZE (65536/2) + //#define CORO_DEFAULT_STACK_SIZE (65536*4) + + //128k needed on PPC due to parser + #define CORO_DEFAULT_STACK_SIZE (128*1024) + #define CORO_STACK_SIZE_MIN 8192 +#endif + + +#if !defined(__MINGW32__) && defined(WIN32) && 0 +#if defined(BUILDING_CORO_DLL) || defined(BUILDING_IOVMALL_DLL) +#define CORO_API __declspec(dllexport) +#else +#define CORO_API __declspec(dllimport) +#endif + +#else +#define CORO_API +#endif + +/* +#if defined(__amd64__) && !defined(__x86_64__) + #define __x86_64__ 1 +#endif +*/ + +// Pick which coro implementation to use +// The make file can set -DUSE_FIBERS, -DUSE_UCONTEXT or -DUSE_SETJMP to force this choice. +#if !defined(USE_FIBERS) && !defined(USE_UCONTEXT) && !defined(USE_SETJMP) + +#if defined(WIN32) && defined(HAS_FIBERS) +# define USE_FIBERS +#elif defined(HAS_UCONTEXT) +//#elif defined(HAS_UCONTEXT) && !defined(__x86_64__) +# if !defined(USE_UCONTEXT) +# define USE_UCONTEXT +# endif +#else +# define USE_SETJMP +#endif + +#endif + +#if defined(USE_FIBERS) + #define CORO_IMPLEMENTATION "fibers" +#elif defined(USE_UCONTEXT) + #include + #define CORO_IMPLEMENTATION "ucontext" +#elif defined(USE_SETJMP) + #include + #define CORO_IMPLEMENTATION "setjmp" +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct Coro Coro; + +struct Coro +{ + size_t requestedStackSize; + size_t allocatedStackSize; + void *stack; + +#ifdef USE_VALGRIND + unsigned int valgrindStackId; +#endif + +#if defined(USE_FIBERS) + void *fiber; +#elif defined(USE_UCONTEXT) + ucontext_t env; +#elif defined(USE_SETJMP) + jmp_buf env; +#endif + + unsigned char isMain; +}; + +//Returns NULL if not enough memory was available to create the Coroutine +CORO_API Coro *Coro_new(void); + +CORO_API void Coro_free(Coro *self); + +// stack + +CORO_API void *Coro_stack(Coro *self); +CORO_API size_t Coro_stackSize(Coro *self); +CORO_API void Coro_setStackSize_(Coro *self, size_t sizeInBytes); +CORO_API size_t Coro_bytesLeftOnStack(Coro *self); +CORO_API int Coro_stackSpaceAlmostGone(Coro *self); + +CORO_API void Coro_initializeMainCoro(Coro *self); + +typedef void (CoroStartCallback)(void *); + +//Returns -1 if not enough memory is available to allocate a coroutine stack. Otherwise, returns 0 +CORO_API int Coro_startCoro_(Coro *self, Coro *other, void *context, CoroStartCallback *callback); + +CORO_API void Coro_switchTo_(Coro *self, Coro *next); +CORO_API void Coro_setup(Coro *self, void *arg); // private + +#ifdef __cplusplus +} +#endif +#endif diff --git a/fdbrpc/libcoroutine/amd64-ucontext.h b/fdbrpc/libcoroutine/amd64-ucontext.h new file mode 100644 index 0000000000..1156552b20 --- /dev/null +++ b/fdbrpc/libcoroutine/amd64-ucontext.h @@ -0,0 +1,138 @@ +#define setcontext(u) setmcontext(&(u)->uc_mcontext) +#define getcontext(u) getmcontext(&(u)->uc_mcontext) +typedef struct mcontext mcontext_t; +typedef struct ucontext ucontext_t; + + +extern int swapcontext(ucontext_t*, const ucontext_t*); +extern void makecontext(ucontext_t*, void(*)(void), int, ...); +extern int getmcontext(mcontext_t*); +extern void setmcontext(const mcontext_t*); + +/*- + * Copyright (c) 1999 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD: src/sys/sys/ucontext.h,v 1.4 1999/10/11 20:33:17 luoqi Exp $ + */ + +/* #include */ + +/*- + * Copyright (c) 1999 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD: src/sys/i386/include/ucontext.h,v 1.4 1999/10/11 20:33:09 luoqi Exp $ + */ + +struct mcontext { + /* + * The first 20 fields must match the definition of + * sigcontext. So that we can support sigcontext + * and ucontext_t at the same time. + */ + long mc_onstack; /* XXX - sigcontext compat. */ + long mc_rdi; /* machine state (struct trapframe) */ + long mc_rsi; + long mc_rdx; + long mc_rcx; + long mc_r8; + long mc_r9; + long mc_rax; + long mc_rbx; + long mc_rbp; + long mc_r10; + long mc_r11; + long mc_r12; + long mc_r13; + long mc_r14; + long mc_r15; + long mc_trapno; + long mc_addr; + long mc_flags; + long mc_err; + long mc_rip; + long mc_cs; + long mc_rflags; + long mc_rsp; + long mc_ss; + + long mc_len; /* sizeof(mcontext_t) */ +#define _MC_FPFMT_NODEV 0x10000 /* device not present or configured */ +#define _MC_FPFMT_XMM 0x10002 + long mc_fpformat; +#define _MC_FPOWNED_NONE 0x20000 /* FP state not used */ +#define _MC_FPOWNED_FPU 0x20001 /* FP state came from FPU */ +#define _MC_FPOWNED_PCB 0x20002 /* FP state came from PCB */ + long mc_ownedfp; + /* + * See for the internals of mc_fpstate[]. + */ + long mc_fpstate[64]; + long mc_spare[8]; +}; + +struct ucontext { + /* + * Keep the order of the first two fields. Also, + * keep them the first two fields in the structure. + * This way we can have a union with struct + * sigcontext and ucontext_t. This allows us to + * support them both at the same time. + * note: the union is not defined, though. + */ + sigset_t uc_sigmask; + mcontext_t uc_mcontext; + + struct __ucontext *uc_link; + stack_t uc_stack; + int __spare__[8]; +}; + + diff --git a/fdbrpc/libcoroutine/asm.S b/fdbrpc/libcoroutine/asm.S new file mode 100644 index 0000000000..f6afa46ebf --- /dev/null +++ b/fdbrpc/libcoroutine/asm.S @@ -0,0 +1,260 @@ +/* Copyright (c) 2005-2006 Russ Cox, MIT; see COPYRIGHT */ + +#if defined(__FreeBSD__) && defined(__i386__) && __FreeBSD__ < 5 +#define NEEDX86CONTEXT 1 +#define SET setmcontext +#define GET getmcontext +#endif + +#if defined(__OpenBSD__) && defined(__i386__) +#define NEEDX86CONTEXT 1 +#define SET setmcontext +#define GET getmcontext +#endif + +#if defined(__APPLE__) +#if defined(__i386__) +#define NEEDX86CONTEXT 1 +#define SET _setmcontext +#define GET _getmcontext +#elif defined(__x86_64__) +#define NEEDAMD64CONTEXT 1 +#define SET _setmcontext +#define GET _getmcontext +#else +#define NEEDPOWERCONTEXT 1 +#define SET __setmcontext +#define GET __getmcontext +#endif +#endif + +#if defined(__linux__) && defined(__arm__) +#define NEEDARMCONTEXT 1 +#define SET setmcontext +#define GET getmcontext +#endif + +#ifdef NEEDX86CONTEXT +.globl SET +SET: + movl 4(%esp), %eax + + movl 8(%eax), %fs + movl 12(%eax), %es + movl 16(%eax), %ds + movl 76(%eax), %ss + movl 20(%eax), %edi + movl 24(%eax), %esi + movl 28(%eax), %ebp + movl 36(%eax), %ebx + movl 40(%eax), %edx + movl 44(%eax), %ecx + + movl 72(%eax), %esp + pushl 60(%eax) /* new %eip */ + movl 48(%eax), %eax + ret + +.globl GET +GET: + movl 4(%esp), %eax + + movl %fs, 8(%eax) + movl %es, 12(%eax) + movl %ds, 16(%eax) + movl %ss, 76(%eax) + movl %edi, 20(%eax) + movl %esi, 24(%eax) + movl %ebp, 28(%eax) + movl %ebx, 36(%eax) + movl %edx, 40(%eax) + movl %ecx, 44(%eax) + + movl $1, 48(%eax) /* %eax */ + movl (%esp), %ecx /* %eip */ + movl %ecx, 60(%eax) + leal 4(%esp), %ecx /* %esp */ + movl %ecx, 72(%eax) + + movl 44(%eax), %ecx /* restore %ecx */ + movl $0, %eax + ret +#endif + +#ifdef NEEDAMD64CONTEXT +.globl SET +SET: + movq 16(%rdi), %rsi + movq 24(%rdi), %rdx + movq 32(%rdi), %rcx + movq 40(%rdi), %r8 + movq 48(%rdi), %r9 + movq 56(%rdi), %rax + movq 64(%rdi), %rbx + movq 72(%rdi), %rbp + movq 80(%rdi), %r10 + movq 88(%rdi), %r11 + movq 96(%rdi), %r12 + movq 104(%rdi), %r13 + movq 112(%rdi), %r14 + movq 120(%rdi), %r15 + movq 184(%rdi), %rsp + pushq 160(%rdi) /* new %eip */ + movq 8(%rdi), %rdi + ret + +.globl GET +GET: + movq %rdi, 8(%rdi) + movq %rsi, 16(%rdi) + movq %rdx, 24(%rdi) + movq %rcx, 32(%rdi) + movq %r8, 40(%rdi) + movq %r9, 48(%rdi) + movq $1, 56(%rdi) /* %rax */ + movq %rbx, 64(%rdi) + movq %rbp, 72(%rdi) + movq %r10, 80(%rdi) + movq %r11, 88(%rdi) + movq %r12, 96(%rdi) + movq %r13, 104(%rdi) + movq %r14, 112(%rdi) + movq %r15, 120(%rdi) + + movq (%rsp), %rcx /* %rip */ + movq %rcx, 160(%rdi) + leaq 8(%rsp), %rcx /* %rsp */ + movq %rcx, 184(%rdi) + + movq 32(%rdi), %rcx /* restore %rcx */ + movq $0, %rax + ret +#endif + +#ifdef NEEDPOWERCONTEXT +/* get FPR and VR use flags with sc 0x7FF3 */ +/* get vsave with mfspr reg, 256 */ + +.text +.align 2 + +.globl GET +GET: /* xxx: instruction scheduling */ + mflr r0 + mfcr r5 + mfctr r6 + mfxer r7 + stw r0, 0*4(r3) + stw r5, 1*4(r3) + stw r6, 2*4(r3) + stw r7, 3*4(r3) + + stw r1, 4*4(r3) + stw r2, 5*4(r3) + li r5, 1 /* return value for setmcontext */ + stw r5, 6*4(r3) + + stw r13, (0+7)*4(r3) /* callee-save GPRs */ + stw r14, (1+7)*4(r3) /* xxx: block move */ + stw r15, (2+7)*4(r3) + stw r16, (3+7)*4(r3) + stw r17, (4+7)*4(r3) + stw r18, (5+7)*4(r3) + stw r19, (6+7)*4(r3) + stw r20, (7+7)*4(r3) + stw r21, (8+7)*4(r3) + stw r22, (9+7)*4(r3) + stw r23, (10+7)*4(r3) + stw r24, (11+7)*4(r3) + stw r25, (12+7)*4(r3) + stw r26, (13+7)*4(r3) + stw r27, (14+7)*4(r3) + stw r28, (15+7)*4(r3) + stw r29, (16+7)*4(r3) + stw r30, (17+7)*4(r3) + stw r31, (18+7)*4(r3) + + li r3, 0 /* return */ + blr + +.globl SET +SET: + lwz r13, (0+7)*4(r3) /* callee-save GPRs */ + lwz r14, (1+7)*4(r3) /* xxx: block move */ + lwz r15, (2+7)*4(r3) + lwz r16, (3+7)*4(r3) + lwz r17, (4+7)*4(r3) + lwz r18, (5+7)*4(r3) + lwz r19, (6+7)*4(r3) + lwz r20, (7+7)*4(r3) + lwz r21, (8+7)*4(r3) + lwz r22, (9+7)*4(r3) + lwz r23, (10+7)*4(r3) + lwz r24, (11+7)*4(r3) + lwz r25, (12+7)*4(r3) + lwz r26, (13+7)*4(r3) + lwz r27, (14+7)*4(r3) + lwz r28, (15+7)*4(r3) + lwz r29, (16+7)*4(r3) + lwz r30, (17+7)*4(r3) + lwz r31, (18+7)*4(r3) + + lwz r1, 4*4(r3) + lwz r2, 5*4(r3) + + lwz r0, 0*4(r3) + mtlr r0 + lwz r0, 1*4(r3) + mtcr r0 /* mtcrf 0xFF, r0 */ + lwz r0, 2*4(r3) + mtctr r0 + lwz r0, 3*4(r3) + mtxer r0 + + lwz r3, 6*4(r3) + blr +#endif + +#ifdef NEEDARMCONTEXT +.globl GET +GET: + str r1, [r0,#4] + str r2, [r0,#8] + str r3, [r0,#12] + str r4, [r0,#16] + str r5, [r0,#20] + str r6, [r0,#24] + str r7, [r0,#28] + str r8, [r0,#32] + str r9, [r0,#36] + str r10, [r0,#40] + str r11, [r0,#44] + str r12, [r0,#48] + str r13, [r0,#52] + str r14, [r0,#56] + /* store 1 as r0-to-restore */ + mov r1, #1 + str r1, [r0] + /* return 0 */ + mov r0, #0 + mov pc, lr + +.globl SET +SET: + ldr r1, [r0,#4] + ldr r2, [r0,#8] + ldr r3, [r0,#12] + ldr r4, [r0,#16] + ldr r5, [r0,#20] + ldr r6, [r0,#24] + ldr r7, [r0,#28] + ldr r8, [r0,#32] + ldr r9, [r0,#36] + ldr r10, [r0,#40] + ldr r11, [r0,#44] + ldr r12, [r0,#48] + ldr r13, [r0,#52] + ldr r14, [r0,#56] + ldr r0, [r0] + mov pc, lr +#endif diff --git a/fdbrpc/libcoroutine/bsd_license.txt b/fdbrpc/libcoroutine/bsd_license.txt new file mode 100644 index 0000000000..2427fecb8c --- /dev/null +++ b/fdbrpc/libcoroutine/bsd_license.txt @@ -0,0 +1,14 @@ +(This is a BSD License) + +Copyright (c) 2002, 2003 Steve Dekorte +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +¥ Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +¥ Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. +¥ Neither the name of the author nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + diff --git a/fdbrpc/libcoroutine/context.c b/fdbrpc/libcoroutine/context.c new file mode 100644 index 0000000000..57cd502300 --- /dev/null +++ b/fdbrpc/libcoroutine/context.c @@ -0,0 +1,116 @@ +/* Copyright (c) 2005-2006 Russ Cox, MIT; see COPYRIGHT */ + +#include "taskimpl.h" + +#if defined(__APPLE__) +#if defined(__i386__) +#define NEEDX86MAKECONTEXT +#define NEEDSWAPCONTEXT +#elif defined(__x86_64__) +#define NEEDAMD64MAKECONTEXT +#define NEEDSWAPCONTEXT +#else +#define NEEDPOWERMAKECONTEXT +#define NEEDSWAPCONTEXT +#endif +#endif + +#if defined(__FreeBSD__) && defined(__i386__) && __FreeBSD__ < 5 +#define NEEDX86MAKECONTEXT +#define NEEDSWAPCONTEXT +#endif + +#if defined(__OpenBSD__) && defined(__i386__) +#define NEEDX86MAKECONTEXT +#define NEEDSWAPCONTEXT +#endif + +#if defined(__linux__) && defined(__arm__) +#define NEEDSWAPCONTEXT +#define NEEDARMMAKECONTEXT +#endif + +#ifdef NEEDPOWERMAKECONTEXT +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + ulong *sp, *tos; + va_list arg; + + tos = (ulong*)ucp->uc_stack.ss_sp+ucp->uc_stack.ss_size/sizeof(ulong); + sp = tos - 16; + ucp->mc.pc = (long)func; + ucp->mc.sp = (long)sp; + va_start(arg, argc); + ucp->mc.r3 = va_arg(arg, long); + va_end(arg); +} +#endif + +#ifdef NEEDX86MAKECONTEXT +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + int *sp; + + sp = (int*)ucp->uc_stack.ss_sp+ucp->uc_stack.ss_size/4; + sp -= argc; + sp = (void*)((uintptr_t)sp - (uintptr_t)sp%16); /* 16-align for OS X */ + memmove(sp, &argc+1, argc*sizeof(int)); + + *--sp = 0; /* return address */ + ucp->uc_mcontext.mc_eip = (long)func; + ucp->uc_mcontext.mc_esp = (int)sp; +} +#endif + +#ifdef NEEDAMD64MAKECONTEXT +void +makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) +{ + long *sp; + va_list va; + + memset(&ucp->uc_mcontext, 0, sizeof ucp->uc_mcontext); + if(argc != 2) + __builtin_trap(); + va_start(va, argc); + ucp->uc_mcontext.mc_rdi = va_arg(va, int); + ucp->uc_mcontext.mc_rsi = va_arg(va, int); + va_end(va); + sp = (long*)ucp->uc_stack.ss_sp+ucp->uc_stack.ss_size/sizeof(long); + sp -= argc; + sp = (void*)((uintptr_t)sp - (uintptr_t)sp%16); /* 16-align for OS X */ + *--sp = 0; /* return address */ + ucp->uc_mcontext.mc_rip = (long)func; + ucp->uc_mcontext.mc_rsp = (long)sp; +} +#endif + +#ifdef NEEDARMMAKECONTEXT +void +makecontext(ucontext_t *uc, void (*fn)(void), int argc, ...) +{ + int i, *sp; + va_list arg; + + sp = (int*)uc->uc_stack.ss_sp+uc->uc_stack.ss_size/4; + va_start(arg, argc); + for(i=0; i<4 && iuc_mcontext.gregs[i] = va_arg(arg, uint); + va_end(arg); + uc->uc_mcontext.gregs[13] = (uint)sp; + uc->uc_mcontext.gregs[14] = (uint)fn; +} +#endif + +#ifdef NEEDSWAPCONTEXT +int +swapcontext(ucontext_t *oucp, const ucontext_t *ucp) +{ + if(getcontext(oucp) == 0) + setcontext(ucp); + return 0; +} +#endif + diff --git a/fdbrpc/libcoroutine/power-ucontext.h b/fdbrpc/libcoroutine/power-ucontext.h new file mode 100644 index 0000000000..691f0f540f --- /dev/null +++ b/fdbrpc/libcoroutine/power-ucontext.h @@ -0,0 +1,37 @@ +#define setcontext(u) _setmcontext(&(u)->mc) +#define getcontext(u) _getmcontext(&(u)->mc) +typedef struct mcontext mcontext_t; +typedef struct ucontext ucontext_t; +struct mcontext +{ + ulong pc; /* lr */ + ulong cr; /* mfcr */ + ulong ctr; /* mfcr */ + ulong xer; /* mfcr */ + ulong sp; /* callee saved: r1 */ + ulong toc; /* callee saved: r2 */ + ulong r3; /* first arg to function, return register: r3 */ + ulong gpr[19]; /* callee saved: r13-r31 */ +/* +// XXX: currently do not save vector registers or floating-point state +// ulong pad; +// uvlong fpr[18]; / * callee saved: f14-f31 * / +// ulong vr[4*12]; / * callee saved: v20-v31, 256-bits each * / +*/ +}; + +struct ucontext +{ + struct { + void *ss_sp; + uint ss_size; + } uc_stack; + sigset_t uc_sigmask; + mcontext_t mc; +}; + +void makecontext(ucontext_t*, void(*)(void), int, ...); +int swapcontext(ucontext_t*, const ucontext_t*); +int _getmcontext(mcontext_t*); +void _setmcontext(const mcontext_t*); + diff --git a/fdbrpc/libcoroutine/taskimpl.h b/fdbrpc/libcoroutine/taskimpl.h new file mode 100644 index 0000000000..658866c381 --- /dev/null +++ b/fdbrpc/libcoroutine/taskimpl.h @@ -0,0 +1,185 @@ +#ifndef TASKIMPL_DEFINED +#define TASKIMPL_DEFINED 1 + +#include + +/* Copyright (c) 2005-2006 Russ Cox, MIT; see COPYRIGHT */ + +#if defined(__sun__) +# define __EXTENSIONS__ 1 /* SunOS */ +# if defined(__SunOS5_6__) || defined(__SunOS5_7__) || defined(__SunOS5_8__) + /* NOT USING #define __MAKECONTEXT_V2_SOURCE 1 / * SunOS */ +# else +# define __MAKECONTEXT_V2_SOURCE 1 +# endif +#endif + +//#define USE_UCONTEXT 1 + +#if defined(__OpenBSD__) +#undef USE_UCONTEXT +#define USE_UCONTEXT 0 +#endif + +#if defined(__APPLE__) +#include +#if defined(MAC_OS_X_VERSION_10_5) +#undef USE_UCONTEXT +#define USE_UCONTEXT 0 +#endif +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if USE_UCONTEXT +#include +#endif +#include +#include +//#include "task.h" + +#define nil ((void*)0) +#define nelem(x) (sizeof(x)/sizeof((x)[0])) + +/* +#define ulong task_ulong +#define uint task_uint +#define uchar task_uchar +#define ushort task_ushort +#define uvlong task_uvlong +#define vlong task_vlong + +typedef unsigned long ulong; +typedef unsigned int uint; +typedef unsigned char uchar; +typedef unsigned short ushort; +typedef unsigned long long uvlong; +typedef long long vlong; + +#define print task_print +#define fprint task_fprint +#define snprint task_snprint +#define seprint task_seprint +#define vprint task_vprint +#define vfprint task_vfprint +#define vsnprint task_vsnprint +#define vseprint task_vseprint +#define strecpy task_strecpy + +int print(char*, ...); +int fprint(int, char*, ...); +char *snprint(char*, uint, char*, ...); +char *seprint(char*, char*, char*, ...); +int vprint(char*, va_list); +int vfprint(int, char*, va_list); +char *vsnprint(char*, uint, char*, va_list); +char *vseprint(char*, char*, char*, va_list); +char *strecpy(char*, char*, char*); +*/ + +#if defined(__FreeBSD__) && __FreeBSD__ < 5 +extern int getmcontext(mcontext_t*); +extern void setmcontext(const mcontext_t*); +#define setcontext(u) setmcontext(&(u)->uc_mcontext) +#define getcontext(u) getmcontext(&(u)->uc_mcontext) +extern int swapcontext(ucontext_t*, const ucontext_t*); +extern void makecontext(ucontext_t*, void(*)(), int, ...); +#endif + +#if defined(__APPLE__) +# define mcontext libthread_mcontext +# define mcontext_t libthread_mcontext_t +# define ucontext libthread_ucontext +# define ucontext_t libthread_ucontext_t +# if defined(__i386__) +# include "386-ucontext.h" +# elif defined(__x86_64__) +# include "amd64-ucontext.h" +# else +# include "power-ucontext.h" +# endif +#endif + +#if defined(__OpenBSD__) +# define mcontext libthread_mcontext +# define mcontext_t libthread_mcontext_t +# define ucontext libthread_ucontext +# define ucontext_t libthread_ucontext_t +# if defined __i386__ +# include "386-ucontext.h" +# else +# include "power-ucontext.h" +# endif +extern pid_t rfork_thread(int, void*, int(*)(void*), void*); +#endif + +#if 0 && defined(__sun__) +# define mcontext libthread_mcontext +# define mcontext_t libthread_mcontext_t +# define ucontext libthread_ucontext +# define ucontext_t libthread_ucontext_t +# include "sparc-ucontext.h" +#endif + +#if defined(__arm__) +int getmcontext(mcontext_t*); +void setmcontext(const mcontext_t*); +#define setcontext(u) setmcontext(&(u)->uc_mcontext) +#define getcontext(u) getmcontext(&(u)->uc_mcontext) +#endif + +/* +typedef struct Context Context; + +enum +{ + STACK = 8192 +}; + +struct Context +{ + ucontext_t uc; +}; + +struct Task +{ + char name[256]; // offset known to acid + char state[256]; + Task *next; + Task *prev; + Task *allnext; + Task *allprev; + Context context; + uvlong alarmtime; + uint id; + uchar *stk; + uint stksize; + int exiting; + int alltaskslot; + int system; + int ready; + void (*startfn)(void*); + void *startarg; + void *udata; +}; + +void taskready(Task*); +void taskswitch(void); + +void addtask(Tasklist*, Task*); +void deltask(Tasklist*, Task*); + +extern Task *taskrunning; +extern int taskcount; +*/ + +#endif diff --git a/fdbrpc/libeio/config.h.linux b/fdbrpc/libeio/config.h.linux new file mode 100644 index 0000000000..84eb188692 --- /dev/null +++ b/fdbrpc/libeio/config.h.linux @@ -0,0 +1,137 @@ +/* config.h. Generated from config.h.in by configure. */ +/* config.h.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* fallocate(2) is available */ +#define HAVE_FALLOCATE 1 + +/* fdatasync(2) is available */ +#define HAVE_FDATASYNC 1 + +/* futimes(2) is available */ +#define HAVE_FUTIMES 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* posix_fadvise(2) is available */ +#define HAVE_POSIX_FADVISE 1 + +/* posix_madvise(2) is available */ +#define HAVE_POSIX_MADVISE 1 + +/* prctl(PR_SET_NAME) is available */ +#define HAVE_PRCTL_SET_NAME 1 + +/* pread(2) and pwrite(2) are available */ +#define HAVE_PREADWRITE 1 + +/* readahead(2) is available (linux) */ +#define HAVE_READAHEAD 1 + +/* sendfile(2) is available and supported */ +#define HAVE_SENDFILE 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* sync_file_range(2) is available */ +#define HAVE_SYNC_FILE_RANGE 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_PRCTL_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* syscall(__NR_syncfs) is available */ +#define HAVE_SYS_SYNCFS 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SYSCALL_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* utimes(2) is available */ +#define HAVE_UTIMES 1 + +/* Define to the sub-directory in which libtool stores uninstalled libraries. + */ +#define LT_OBJDIR ".libs/" + +/* Name of package */ +#define PACKAGE "libeio" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Enable extensions on AIX 3, Interix. */ +#ifndef _ALL_SOURCE +# define _ALL_SOURCE 1 +#endif +/* Enable GNU extensions on systems that have them. */ +#ifndef _GNU_SOURCE +# define _GNU_SOURCE 1 +#endif +/* Enable threading extensions on Solaris. */ +#ifndef _POSIX_PTHREAD_SEMANTICS +# define _POSIX_PTHREAD_SEMANTICS 1 +#endif +/* Enable extensions on HP NonStop. */ +#ifndef _TANDEM_SOURCE +# define _TANDEM_SOURCE 1 +#endif +/* Enable general extensions on Solaris. */ +#ifndef __EXTENSIONS__ +# define __EXTENSIONS__ 1 +#endif + + +/* Version number of package */ +#define VERSION "1.0" + +/* Define to 1 if on MINIX. */ +/* #undef _MINIX */ + +/* Define to 2 if the system does not provide POSIX.1 features except with + this defined. */ +/* #undef _POSIX_1_SOURCE */ + +/* Define to 1 if you need to in order for `stat' and other things to work. */ +/* #undef _POSIX_SOURCE */ diff --git a/fdbrpc/libeio/config.h.osx b/fdbrpc/libeio/config.h.osx new file mode 100644 index 0000000000..9a746a3d56 --- /dev/null +++ b/fdbrpc/libeio/config.h.osx @@ -0,0 +1,143 @@ +/* config.h. Generated from config.h.in by configure. */ +/* config.h.in. Generated from configure.ac by autoheader. */ + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* Contrary to what the author of libeio believed, fdatasync(2) is not implemented on OSX */ +#define HAVE_FDATASYNC 0 + +/* futimes(2) is available */ +#define HAVE_FUTIMES 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* fallocate(2) is available */ +/* #undef HAVE_LINUX_FALLOCATE */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LINUX_FIEMAP_H */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_LINUX_FS_H */ + +/* splice/vmsplice/tee(2) are available */ +/* #undef HAVE_LINUX_SPLICE */ + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* posix_fadvise(2) is available */ +/* #undef HAVE_POSIX_FADVISE */ + +/* posix_madvise(2) is available */ +#define HAVE_POSIX_MADVISE 1 + +/* prctl(PR_SET_NAME) is available */ +/* #undef HAVE_PRCTL_SET_NAME */ + +/* readahead(2) is available (linux) */ +/* #undef HAVE_READAHEAD */ + +/* sendfile(2) is available and supported */ +#define HAVE_SENDFILE 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* sync_file_range(2) is available */ +/* #undef HAVE_SYNC_FILE_RANGE */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_SYS_PRCTL_H */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* syscall(__NR_syncfs) is available */ +/* #undef HAVE_SYS_SYNCFS */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_SYSCALL_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* utimes(2) is available */ +#define HAVE_UTIMES 1 + +/* Define to the sub-directory in which libtool stores uninstalled libraries. + */ +#define LT_OBJDIR ".libs/" + +/* Name of package */ +#define PACKAGE "libeio" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "" + +/* Define to the home page for this package. */ +#define PACKAGE_URL "" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Enable extensions on AIX 3, Interix. */ +#ifndef _ALL_SOURCE +# define _ALL_SOURCE 1 +#endif +/* Enable GNU extensions on systems that have them. */ +#ifndef _GNU_SOURCE +# define _GNU_SOURCE 1 +#endif +/* Enable threading extensions on Solaris. */ +#ifndef _POSIX_PTHREAD_SEMANTICS +# define _POSIX_PTHREAD_SEMANTICS 1 +#endif +/* Enable extensions on HP NonStop. */ +#ifndef _TANDEM_SOURCE +# define _TANDEM_SOURCE 1 +#endif +/* Enable general extensions on Solaris. */ +#ifndef __EXTENSIONS__ +# define __EXTENSIONS__ 1 +#endif + + +/* Version number of package */ +#define VERSION "1.0" + +/* Define to 1 if on MINIX. */ +/* #undef _MINIX */ + +/* Define to 2 if the system does not provide POSIX.1 features except with + this defined. */ +/* #undef _POSIX_1_SOURCE */ + +/* Define to 1 if you need to in order for `stat' and other things to work. */ +/* #undef _POSIX_SOURCE */ diff --git a/fdbrpc/libeio/ecb.h b/fdbrpc/libeio/ecb.h new file mode 100644 index 0000000000..1a4829033e --- /dev/null +++ b/fdbrpc/libeio/ecb.h @@ -0,0 +1,406 @@ +/* + * libecb - http://software.schmorp.de/pkg/libecb + * + * Copyright (©) 2009-2011 Marc Alexander Lehmann + * Copyright (©) 2011 Emanuele Giaquinta + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ECB_H +#define ECB_H + +#ifdef _WIN32 + typedef signed char int8_t; + typedef unsigned char uint8_t; + typedef signed short int16_t; + typedef unsigned short uint16_t; + typedef signed int int32_t; + typedef unsigned int uint32_t; + #if __GNUC__ + typedef signed long long int64_t; + typedef unsigned long long uint64_t; + #else /* _MSC_VER || __BORLANDC__ */ + typedef signed __int64 int64_t; + typedef unsigned __int64 uint64_t; + #endif +#else + #include +#endif + +/* many compilers define _GNUC_ to some versions but then only implement + * what their idiot authors think are the "more important" extensions, + * causing enormous grief in return for some better fake benchmark numbers. + * or so. + * we try to detect these and simply assume they are not gcc - if they have + * an issue with that they should have done it right in the first place. + */ +#ifndef ECB_GCC_VERSION + #if !defined(__GNUC_MINOR__) || defined(__INTEL_COMPILER) || defined(__SUNPRO_C) || defined(__SUNPRO_CC) || defined(__llvm__) || defined(__clang__) + #define ECB_GCC_VERSION(major,minor) 0 + #else + #define ECB_GCC_VERSION(major,minor) (__GNUC__ > (major) || (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))) + #endif +#endif + +/*****************************************************************************/ + +/* ECB_NO_THREADS - ecb is not used by multiple threads, ever */ +/* ECB_NO_SMP - ecb might be used in multiple threads, but only on a single cpu */ + +#if ECB_NO_THREADS || ECB_NO_SMP + #define ECB_MEMORY_FENCE do { } while (0) +#endif + +#ifndef ECB_MEMORY_FENCE + #if ECB_GCC_VERSION(2,5) || defined(__INTEL_COMPILER) || defined(__clang__) + #if __i386__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("lock; orb $0, -1(%%esp)" : : : "memory") + #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE /* non-lock xchg might be enough */ + #define ECB_MEMORY_FENCE_RELEASE do { } while (0) /* unlikely to change in future cpus */ + #elif __amd64 + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mfence" : : : "memory") + #define ECB_MEMORY_FENCE_ACQUIRE __asm__ __volatile__ ("lfence" : : : "memory") + #define ECB_MEMORY_FENCE_RELEASE __asm__ __volatile__ ("sfence") /* play safe - not needed in any current cpu */ + #elif __powerpc__ || __ppc__ || __powerpc64__ || __ppc64__ + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("sync" : : : "memory") + #elif defined(__ARM_ARCH_6__ ) || defined(__ARM_ARCH_6J__ ) \ + || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__) + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("mcr p15,0,%0,c7,c10,5" : : "r" (0) : "memory") + #elif defined(__ARM_ARCH_7__ ) || defined(__ARM_ARCH_7A__ ) \ + || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7R__ ) + #define ECB_MEMORY_FENCE __asm__ __volatile__ ("dmb" : : : "memory") + #endif + #endif +#endif + +#ifndef ECB_MEMORY_FENCE + #if ECB_GCC_VERSION(4,4) || defined(__INTEL_COMPILER) || defined(__clang__) + #define ECB_MEMORY_FENCE __sync_synchronize () + /*#define ECB_MEMORY_FENCE_ACQUIRE ({ char dummy = 0; __sync_lock_test_and_set (&dummy, 1); }) */ + /*#define ECB_MEMORY_FENCE_RELEASE ({ char dummy = 1; __sync_lock_release (&dummy ); }) */ + #elif _MSC_VER >= 1400 /* VC++ 2005 */ + #pragma intrinsic(_ReadBarrier,_WriteBarrier,_ReadWriteBarrier) + #define ECB_MEMORY_FENCE _ReadWriteBarrier () + #define ECB_MEMORY_FENCE_ACQUIRE _ReadWriteBarrier () /* according to msdn, _ReadBarrier is not a load fence */ + #define ECB_MEMORY_FENCE_RELEASE _WriteBarrier () + #elif defined(_WIN32) + #include + #define ECB_MEMORY_FENCE MemoryBarrier () /* actually just xchg on x86... scary */ + #endif +#endif + +#ifndef ECB_MEMORY_FENCE + #if !ECB_AVOID_PTHREADS + /* + * if you get undefined symbol references to pthread_mutex_lock, + * or failure to find pthread.h, then you should implement + * the ECB_MEMORY_FENCE operations for your cpu/compiler + * OR provide pthread.h and link against the posix thread library + * of your system. + */ + #include + #define ECB_NEEDS_PTHREADS 1 + #define ECB_MEMORY_FENCE_NEEDS_PTHREADS 1 + + static pthread_mutex_t ecb_mf_lock = PTHREAD_MUTEX_INITIALIZER; + #define ECB_MEMORY_FENCE do { pthread_mutex_lock (&ecb_mf_lock); pthread_mutex_unlock (&ecb_mf_lock); } while (0) + #endif +#endif + +#if !defined(ECB_MEMORY_FENCE_ACQUIRE) && defined(ECB_MEMORY_FENCE) + #define ECB_MEMORY_FENCE_ACQUIRE ECB_MEMORY_FENCE +#endif + +#if !defined(ECB_MEMORY_FENCE_RELEASE) && defined(ECB_MEMORY_FENCE) + #define ECB_MEMORY_FENCE_RELEASE ECB_MEMORY_FENCE +#endif + +/*****************************************************************************/ + +#define ECB_C99 (__STDC_VERSION__ >= 199901L) + +#if __cplusplus + #define ecb_inline static inline +#elif ECB_GCC_VERSION(2,5) + #define ecb_inline static __inline__ +#elif ECB_C99 + #define ecb_inline static inline +#else + #define ecb_inline static +#endif + +#if ECB_GCC_VERSION(3,3) + #define ecb_restrict __restrict__ +#elif ECB_C99 + #define ecb_restrict restrict +#else + #define ecb_restrict +#endif + +typedef int ecb_bool; + +#define ECB_CONCAT_(a, b) a ## b +#define ECB_CONCAT(a, b) ECB_CONCAT_(a, b) +#define ECB_STRINGIFY_(a) # a +#define ECB_STRINGIFY(a) ECB_STRINGIFY_(a) + +#define ecb_function_ ecb_inline + +#if ECB_GCC_VERSION(3,1) + #define ecb_attribute(attrlist) __attribute__(attrlist) + #define ecb_is_constant(expr) __builtin_constant_p (expr) + #define ecb_expect(expr,value) __builtin_expect ((expr),(value)) + #define ecb_prefetch(addr,rw,locality) __builtin_prefetch (addr, rw, locality) +#else + #define ecb_attribute(attrlist) + #define ecb_is_constant(expr) 0 + #define ecb_expect(expr,value) (expr) + #define ecb_prefetch(addr,rw,locality) +#endif + +/* no emulation for ecb_decltype */ +#if ECB_GCC_VERSION(4,5) + #define ecb_decltype(x) __decltype(x) +#elif ECB_GCC_VERSION(3,0) + #define ecb_decltype(x) __typeof(x) +#endif + +#define ecb_noinline ecb_attribute ((__noinline__)) +#define ecb_noreturn ecb_attribute ((__noreturn__)) +#define ecb_unused ecb_attribute ((__unused__)) +#define ecb_const ecb_attribute ((__const__)) +#define ecb_pure ecb_attribute ((__pure__)) + +#if ECB_GCC_VERSION(4,3) + #define ecb_artificial ecb_attribute ((__artificial__)) + #define ecb_hot ecb_attribute ((__hot__)) + #define ecb_cold ecb_attribute ((__cold__)) +#else + #define ecb_artificial + #define ecb_hot + #define ecb_cold +#endif + +/* put around conditional expressions if you are very sure that the */ +/* expression is mostly true or mostly false. note that these return */ +/* booleans, not the expression. */ +#define ecb_expect_false(expr) ecb_expect (!!(expr), 0) +#define ecb_expect_true(expr) ecb_expect (!!(expr), 1) +/* for compatibility to the rest of the world */ +#define ecb_likely(expr) ecb_expect_true (expr) +#define ecb_unlikely(expr) ecb_expect_false (expr) + +/* count trailing zero bits and count # of one bits */ +#if ECB_GCC_VERSION(3,4) + /* we assume int == 32 bit, long == 32 or 64 bit and long long == 64 bit */ + #define ecb_ld32(x) (__builtin_clz (x) ^ 31) + #define ecb_ld64(x) (__builtin_clzll (x) ^ 63) + #define ecb_ctz32(x) __builtin_ctz (x) + #define ecb_ctz64(x) __builtin_ctzll (x) + #define ecb_popcount32(x) __builtin_popcount (x) + /* no popcountll */ +#else + ecb_function_ int ecb_ctz32 (uint32_t x) ecb_const; + ecb_function_ int + ecb_ctz32 (uint32_t x) + { + int r = 0; + + x &= ~x + 1; /* this isolates the lowest bit */ + +#if ECB_branchless_on_i386 + r += !!(x & 0xaaaaaaaa) << 0; + r += !!(x & 0xcccccccc) << 1; + r += !!(x & 0xf0f0f0f0) << 2; + r += !!(x & 0xff00ff00) << 3; + r += !!(x & 0xffff0000) << 4; +#else + if (x & 0xaaaaaaaa) r += 1; + if (x & 0xcccccccc) r += 2; + if (x & 0xf0f0f0f0) r += 4; + if (x & 0xff00ff00) r += 8; + if (x & 0xffff0000) r += 16; +#endif + + return r; + } + + ecb_function_ int ecb_ctz64 (uint64_t x) ecb_const; + ecb_function_ int + ecb_ctz64 (uint64_t x) + { + int shift = x & 0xffffffffU ? 0 : 32; + return ecb_ctz32 (x >> shift) + shift; + } + + ecb_function_ int ecb_popcount32 (uint32_t x) ecb_const; + ecb_function_ int + ecb_popcount32 (uint32_t x) + { + x -= (x >> 1) & 0x55555555; + x = ((x >> 2) & 0x33333333) + (x & 0x33333333); + x = ((x >> 4) + x) & 0x0f0f0f0f; + x *= 0x01010101; + + return x >> 24; + } + + ecb_function_ int ecb_ld32 (uint32_t x) ecb_const; + ecb_function_ int ecb_ld32 (uint32_t x) + { + int r = 0; + + if (x >> 16) { x >>= 16; r += 16; } + if (x >> 8) { x >>= 8; r += 8; } + if (x >> 4) { x >>= 4; r += 4; } + if (x >> 2) { x >>= 2; r += 2; } + if (x >> 1) { r += 1; } + + return r; + } + + ecb_function_ int ecb_ld64 (uint64_t x) ecb_const; + ecb_function_ int ecb_ld64 (uint64_t x) + { + int r = 0; + + if (x >> 32) { x >>= 32; r += 32; } + + return r + ecb_ld32 (x); + } +#endif + +/* popcount64 is only available on 64 bit cpus as gcc builtin */ +/* so for this version we are lazy */ +ecb_function_ int ecb_popcount64 (uint64_t x) ecb_const; +ecb_function_ int +ecb_popcount64 (uint64_t x) +{ + return ecb_popcount32 (x) + ecb_popcount32 (x >> 32); +} + +ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) ecb_const; +ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) ecb_const; +ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) ecb_const; +ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) ecb_const; +ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) ecb_const; +ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) ecb_const; +ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) ecb_const; +ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) ecb_const; + +ecb_inline uint8_t ecb_rotl8 (uint8_t x, unsigned int count) { return (x >> ( 8 - count)) | (x << count); } +ecb_inline uint8_t ecb_rotr8 (uint8_t x, unsigned int count) { return (x << ( 8 - count)) | (x >> count); } +ecb_inline uint16_t ecb_rotl16 (uint16_t x, unsigned int count) { return (x >> (16 - count)) | (x << count); } +ecb_inline uint16_t ecb_rotr16 (uint16_t x, unsigned int count) { return (x << (16 - count)) | (x >> count); } +ecb_inline uint32_t ecb_rotl32 (uint32_t x, unsigned int count) { return (x >> (32 - count)) | (x << count); } +ecb_inline uint32_t ecb_rotr32 (uint32_t x, unsigned int count) { return (x << (32 - count)) | (x >> count); } +ecb_inline uint64_t ecb_rotl64 (uint64_t x, unsigned int count) { return (x >> (64 - count)) | (x << count); } +ecb_inline uint64_t ecb_rotr64 (uint64_t x, unsigned int count) { return (x << (64 - count)) | (x >> count); } + +#if ECB_GCC_VERSION(4,3) + #define ecb_bswap16(x) (__builtin_bswap32 (x) >> 16) + #define ecb_bswap32(x) __builtin_bswap32 (x) + #define ecb_bswap64(x) __builtin_bswap64 (x) +#else + ecb_function_ uint16_t ecb_bswap16 (uint16_t x) ecb_const; + ecb_function_ uint16_t + ecb_bswap16 (uint16_t x) + { + return ecb_rotl16 (x, 8); + } + + ecb_function_ uint32_t ecb_bswap32 (uint32_t x) ecb_const; + ecb_function_ uint32_t + ecb_bswap32 (uint32_t x) + { + return (((uint32_t)ecb_bswap16 (x)) << 16) | ecb_bswap16 (x >> 16); + } + + ecb_function_ uint64_t ecb_bswap64 (uint64_t x) ecb_const; + ecb_function_ uint64_t + ecb_bswap64 (uint64_t x) + { + return (((uint64_t)ecb_bswap32 (x)) << 32) | ecb_bswap32 (x >> 32); + } +#endif + +#if ECB_GCC_VERSION(4,5) + #define ecb_unreachable() __builtin_unreachable () +#else + /* this seems to work fine, but gcc always emits a warning for it :/ */ + ecb_function_ void ecb_unreachable (void) ecb_noreturn; + ecb_function_ void ecb_unreachable (void) { } +#endif + +/* try to tell the compiler that some condition is definitely true */ +#define ecb_assume(cond) do { if (!(cond)) ecb_unreachable (); } while (0) + +ecb_function_ unsigned char ecb_byteorder_helper (void) ecb_const; +ecb_function_ unsigned char +ecb_byteorder_helper (void) +{ + const uint32_t u = 0x11223344; + return *(unsigned char *)&u; +} + +ecb_function_ ecb_bool ecb_big_endian (void) ecb_const; +ecb_function_ ecb_bool ecb_big_endian (void) { return ecb_byteorder_helper () == 0x11; } +ecb_function_ ecb_bool ecb_little_endian (void) ecb_const; +ecb_function_ ecb_bool ecb_little_endian (void) { return ecb_byteorder_helper () == 0x44; } + +#if ECB_GCC_VERSION(3,0) || ECB_C99 + #define ecb_mod(m,n) ((m) % (n) + ((m) % (n) < 0 ? (n) : 0)) +#else + #define ecb_mod(m,n) ((m) < 0 ? ((n) - 1 - ((-1 - (m)) % (n))) : ((m) % (n))) +#endif + +#if __cplusplus + template + static inline T ecb_div_rd (T val, T div) + { + return val < 0 ? - ((-val + div - 1) / div) : (val ) / div; + } + template + static inline T ecb_div_ru (T val, T div) + { + return val < 0 ? - ((-val ) / div) : (val + div - 1) / div; + } +#else + #define ecb_div_rd(val,div) ((val) < 0 ? - ((-(val) + (div) - 1) / (div)) : ((val) ) / (div)) + #define ecb_div_ru(val,div) ((val) < 0 ? - ((-(val) ) / (div)) : ((val) + (div) - 1) / (div)) +#endif + +#if ecb_cplusplus_does_not_suck + /* does not work for local types (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2657.htm) */ + template + static inline int ecb_array_length (const T (&arr)[N]) + { + return N; + } +#else + #define ecb_array_length(name) (sizeof (name) / sizeof (name [0])) +#endif + +#endif + diff --git a/fdbrpc/libeio/eio.c b/fdbrpc/libeio/eio.c new file mode 100644 index 0000000000..e961416690 --- /dev/null +++ b/fdbrpc/libeio/eio.c @@ -0,0 +1,2804 @@ +/* + * libeio implementation + * + * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#ifdef __linux__ +#include "config.h.linux" +#elif defined(__APPLE__) +#include "config.h.osx" +#endif + +#include "eio.h" +#include "ecb.h" + +#ifdef EIO_STACKSIZE +# define X_STACKSIZE EIO_STACKSIZE +#endif +#include "xthread.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* intptr_t comes from unistd.h, says POSIX/UNIX/tradition */ +/* intptr_t only comes from stdint.h, says idiot openbsd coder */ +#if HAVE_STDINT_H +# include +#endif + +#ifndef ECANCELED +# define ECANCELED EDOM +#endif +#ifndef ELOOP +# define ELOOP EDOM +#endif + +#if !defined(ENOTSOCK) && defined(WSAENOTSOCK) +# define ENOTSOCK WSAENOTSOCK +#endif + +static void eio_destroy (eio_req *req); + +#ifndef EIO_FINISH +# define EIO_FINISH(req) ((req)->finish) && !EIO_CANCELLED (req) ? (req)->finish (req) : 0 +#endif + +#ifndef EIO_DESTROY +# define EIO_DESTROY(req) do { if ((req)->destroy) (req)->destroy (req); } while (0) +#endif + +#ifndef EIO_FEED +# define EIO_FEED(req) do { if ((req)->feed ) (req)->feed (req); } while (0) +#endif + +#ifndef EIO_FD_TO_WIN32_HANDLE +# define EIO_FD_TO_WIN32_HANDLE(fd) _get_osfhandle (fd) +#endif +#ifndef EIO_WIN32_HANDLE_TO_FD +# define EIO_WIN32_HANDLE_TO_FD(handle) _open_osfhandle (handle, 0) +#endif + +#define EIO_ERRNO(errval,retval) ((errno = errval), retval) + +#define EIO_ENOSYS() EIO_ERRNO (ENOSYS, -1) + +#ifdef _WIN32 + + #undef PAGESIZE + #define PAGESIZE 4096 /* GetSystemInfo? */ + + /* TODO: look at how perl does stat (non-sloppy), unlink (ro-files), utime, link */ + + #ifdef EIO_STRUCT_STATI64 + /* look at perl's non-sloppy stat */ + #define stat(path,buf) _stati64 (path,buf) + #define fstat(fd,buf) _fstati64 (fd,buf) + #endif + #define lstat(path,buf) stat (path,buf) + #define fsync(fd) (FlushFileBuffers ((HANDLE)EIO_FD_TO_WIN32_HANDLE (fd)) ? 0 : EIO_ERRNO (EBADF, -1)) + #define mkdir(path,mode) _mkdir (path) + #define link(old,neu) (CreateHardLink (neu, old, 0) ? 0 : EIO_ERRNO (ENOENT, -1)) + + #define chmod(path,mode) _chmod (path, mode) + #define dup(fd) _dup (fd) + #define dup2(fd1,fd2) _dup2 (fd1, fd2) + + #define fchmod(fd,mode) EIO_ENOSYS () + #define chown(path,uid,gid) EIO_ENOSYS () + #define fchown(fd,uid,gid) EIO_ENOSYS () + #define truncate(path,offs) EIO_ENOSYS () /* far-miss: SetEndOfFile */ + #define ftruncate(fd,offs) EIO_ENOSYS () /* near-miss: SetEndOfFile */ + #define mknod(path,mode,dev) EIO_ENOSYS () + #define sync() EIO_ENOSYS () + #define readlink(path,buf,s) EIO_ENOSYS () + #define statvfs(path,buf) EIO_ENOSYS () + #define fstatvfs(fd,buf) EIO_ENOSYS () + + /* rename() uses MoveFile, which fails to overwrite */ + #define rename(old,neu) eio__rename (old, neu) + + static int + eio__rename (const char *old, const char *neu) + { + if (MoveFileEx (old, neu, MOVEFILE_REPLACE_EXISTING)) + return 0; + + /* should steal _dosmaperr */ + switch (GetLastError ()) + { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_DRIVE: + case ERROR_NO_MORE_FILES: + case ERROR_BAD_NETPATH: + case ERROR_BAD_NET_NAME: + case ERROR_BAD_PATHNAME: + case ERROR_FILENAME_EXCED_RANGE: + errno = ENOENT; + break; + + default: + errno = EACCES; + break; + } + + return -1; + } + + /* we could even stat and see if it exists */ + static int + symlink (const char *old, const char *neu) + { + #if WINVER >= 0x0600 + if (CreateSymbolicLink (neu, old, 1)) + return 0; + + if (CreateSymbolicLink (neu, old, 0)) + return 0; + #endif + + return EIO_ERRNO (ENOENT, -1); + } + + /* POSIX API only */ + #define CreateHardLink(neu,old,flags) 0 + #define CreateSymbolicLink(neu,old,flags) 0 + + struct statvfs + { + int dummy; + }; + + #define DT_DIR EIO_DT_DIR + #define DT_REG EIO_DT_REG + #define D_NAME(entp) entp.cFileName + #define D_TYPE(entp) (entp.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? DT_DIR : DT_REG) + +#else + + #include + #include + #include + #include + #include + #include + + #if _POSIX_MEMLOCK || _POSIX_MEMLOCK_RANGE || _POSIX_MAPPED_FILES + #include + #endif + + #define D_NAME(entp) entp->d_name + + /* POSIX_SOURCE is useless on bsd's, and XOPEN_SOURCE is unreliable there, too */ + #if __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ + #define _DIRENT_HAVE_D_TYPE /* sigh */ + #define D_INO(de) (de)->d_fileno + #define D_NAMLEN(de) (de)->d_namlen + #elif __linux || defined d_ino || _XOPEN_SOURCE >= 600 + #define D_INO(de) (de)->d_ino + #endif + + #ifdef _D_EXACT_NAMLEN + #undef D_NAMLEN + #define D_NAMLEN(de) _D_EXACT_NAMLEN (de) + #endif + + #ifdef _DIRENT_HAVE_D_TYPE + #define D_TYPE(de) (de)->d_type + #endif + + #ifndef EIO_STRUCT_DIRENT + #define EIO_STRUCT_DIRENT struct dirent + #endif + +#endif + +#if HAVE_UTIMES +# include +#endif + +#if HAVE_SYS_SYSCALL_H +# include +#endif + +#if HAVE_SYS_PRCTL_H +# include +#endif + +#if HAVE_SENDFILE +# if __linux +# include +# elif __FreeBSD__ || defined __APPLE__ +# include +# include +# elif __hpux +# include +# elif __solaris +# include +# else +# error sendfile support requested but not available +# endif +#endif + +#ifndef D_TYPE +# define D_TYPE(de) 0 +#endif +#ifndef D_INO +# define D_INO(de) 0 +#endif +#ifndef D_NAMLEN +# define D_NAMLEN(entp) strlen (D_NAME (entp)) +#endif + +/* used for struct dirent, AIX doesn't provide it */ +#ifndef NAME_MAX +# define NAME_MAX 4096 +#endif + +/* used for readlink etc. */ +#ifndef PATH_MAX +# define PATH_MAX 4096 +#endif + +/* buffer size for various temporary buffers */ +#define EIO_BUFSIZE 65536 + +#define dBUF \ + char *eio_buf = malloc (EIO_BUFSIZE); \ + errno = ENOMEM; \ + if (!eio_buf) \ + return -1 + +#define FUBd \ + free (eio_buf) + +#define EIO_TICKS ((1000000 + 1023) >> 10) + +/*****************************************************************************/ + +struct tmpbuf +{ + void *ptr; + int len; +}; + +static void * +tmpbuf_get (struct tmpbuf *buf, int len) +{ + if (buf->len < len) + { + free (buf->ptr); + buf->ptr = malloc (buf->len = len); + } + + return buf->ptr; +} + +struct tmpbuf; + +#if _POSIX_VERSION >= 200809L + #define HAVE_AT 1 + #define WD2FD(wd) ((wd) ? (wd)->fd : AT_FDCWD) + #ifndef O_SEARCH + #define O_SEARCH O_RDONLY + #endif +#else + #define HAVE_AT 0 + static const char *wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path); +#endif + +struct eio_pwd +{ +#if HAVE_AT + int fd; +#endif + int len; + char str[1]; /* actually, a 0-terminated canonical path */ +}; + +/*****************************************************************************/ + +#define ETP_PRI_MIN EIO_PRI_MIN +#define ETP_PRI_MAX EIO_PRI_MAX + +struct etp_worker; + +#define ETP_REQ eio_req +#define ETP_DESTROY(req) eio_destroy (req) +static int eio_finish (eio_req *req); +#define ETP_FINISH(req) eio_finish (req) +static void eio_execute (struct etp_worker *self, eio_req *req); +#define ETP_EXECUTE(wrk,req) eio_execute (wrk,req) + +/*****************************************************************************/ + +#define ETP_NUM_PRI (ETP_PRI_MAX - ETP_PRI_MIN + 1) + +/* calculate time difference in ~1/EIO_TICKS of a second */ +ecb_inline int +tvdiff (struct timeval *tv1, struct timeval *tv2) +{ + return (tv2->tv_sec - tv1->tv_sec ) * EIO_TICKS + + ((tv2->tv_usec - tv1->tv_usec) >> 10); +} + +static unsigned int started, idle, wanted = 4; + +static void (*want_poll_cb) (void); +static void (*done_poll_cb) (void); + +static unsigned int max_poll_time; /* reslock */ +static unsigned int max_poll_reqs; /* reslock */ + +static unsigned int nreqs; /* reqlock */ +static unsigned int nready; /* reqlock */ +static unsigned int npending; /* reqlock */ +static unsigned int max_idle = 4; /* maximum number of threads that can idle indefinitely */ +static unsigned int idle_timeout = 10; /* number of seconds after which an idle threads exit */ + +static xmutex_t wrklock; +static xmutex_t reslock; +static xmutex_t reqlock; +static xcond_t reqwait; + +#if !HAVE_PREADWRITE +/* + * make our pread/pwrite emulation safe against themselves, but not against + * normal read/write by using a mutex. slows down execution a lot, + * but that's your problem, not mine. + */ +static xmutex_t preadwritelock; +#endif + +typedef struct etp_worker +{ + struct tmpbuf tmpbuf; + + /* locked by wrklock */ + struct etp_worker *prev, *next; + + xthread_t tid; + +#ifdef ETP_WORKER_COMMON + ETP_WORKER_COMMON +#endif +} etp_worker; + +static etp_worker wrk_first; /* NOT etp */ + +#define ETP_WORKER_LOCK(wrk) X_LOCK (wrklock) +#define ETP_WORKER_UNLOCK(wrk) X_UNLOCK (wrklock) + +/* worker threads management */ + +static void +etp_worker_clear (etp_worker *wrk) +{ +} + +static void ecb_cold +etp_worker_free (etp_worker *wrk) +{ + free (wrk->tmpbuf.ptr); + + wrk->next->prev = wrk->prev; + wrk->prev->next = wrk->next; + + free (wrk); +} + +static unsigned int +etp_nreqs (void) +{ + int retval; + if (WORDACCESS_UNSAFE) X_LOCK (reqlock); + retval = nreqs; + if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); + return retval; +} + +static unsigned int +etp_nready (void) +{ + unsigned int retval; + + if (WORDACCESS_UNSAFE) X_LOCK (reqlock); + retval = nready; + if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); + + return retval; +} + +static unsigned int +etp_npending (void) +{ + unsigned int retval; + + if (WORDACCESS_UNSAFE) X_LOCK (reqlock); + retval = npending; + if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); + + return retval; +} + +static unsigned int +etp_nthreads (void) +{ + unsigned int retval; + + if (WORDACCESS_UNSAFE) X_LOCK (reqlock); + retval = started; + if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); + + return retval; +} + +/* + * a somewhat faster data structure might be nice, but + * with 8 priorities this actually needs <20 insns + * per shift, the most expensive operation. + */ +typedef struct { + ETP_REQ *qs[ETP_NUM_PRI], *qe[ETP_NUM_PRI]; /* qstart, qend */ + int size; +} etp_reqq; + +static etp_reqq req_queue; +static etp_reqq res_queue; + +static void ecb_noinline ecb_cold +reqq_init (etp_reqq *q) +{ + int pri; + + for (pri = 0; pri < ETP_NUM_PRI; ++pri) + q->qs[pri] = q->qe[pri] = 0; + + q->size = 0; +} + +static int ecb_noinline +reqq_push (etp_reqq *q, ETP_REQ *req) +{ + int pri = req->pri; + req->next = 0; + + if (q->qe[pri]) + { + q->qe[pri]->next = req; + q->qe[pri] = req; + } + else + q->qe[pri] = q->qs[pri] = req; + + return q->size++; +} + +static ETP_REQ * ecb_noinline +reqq_shift (etp_reqq *q) +{ + int pri; + + if (!q->size) + return 0; + + --q->size; + + for (pri = ETP_NUM_PRI; pri--; ) + { + eio_req *req = q->qs[pri]; + + if (req) + { + if (!(q->qs[pri] = (eio_req *)req->next)) + q->qe[pri] = 0; + + return req; + } + } + + abort (); +} + +static int ecb_cold +etp_init (void (*want_poll)(void), void (*done_poll)(void)) +{ + X_MUTEX_CREATE (wrklock); + X_MUTEX_CREATE (reslock); + X_MUTEX_CREATE (reqlock); + X_COND_CREATE (reqwait); + + reqq_init (&req_queue); + reqq_init (&res_queue); + + wrk_first.next = + wrk_first.prev = &wrk_first; + + started = 0; + idle = 0; + nreqs = 0; + nready = 0; + npending = 0; + + want_poll_cb = want_poll; + done_poll_cb = done_poll; + + return 0; +} + +X_THREAD_PROC (etp_proc); + +static void ecb_cold +etp_start_thread (void) +{ + etp_worker *wrk = calloc (1, sizeof (etp_worker)); + + /*TODO*/ + assert (("unable to allocate worker thread data", wrk)); + + X_LOCK (wrklock); + + if (thread_create (&wrk->tid, etp_proc, (void *)wrk)) + { + wrk->prev = &wrk_first; + wrk->next = wrk_first.next; + wrk_first.next->prev = wrk; + wrk_first.next = wrk; + ++started; + } + else + free (wrk); + + X_UNLOCK (wrklock); +} + +static void +etp_maybe_start_thread (void) +{ + if (ecb_expect_true (etp_nthreads () >= wanted)) + return; + + /* todo: maybe use idle here, but might be less exact */ + if (ecb_expect_true (0 <= (int)etp_nthreads () + (int)etp_npending () - (int)etp_nreqs ())) + return; + + etp_start_thread (); +} + +static void ecb_cold +etp_end_thread (void) +{ + eio_req *req = calloc (1, sizeof (eio_req)); /* will be freed by worker */ + + req->type = -1; + req->pri = ETP_PRI_MAX - ETP_PRI_MIN; + + X_LOCK (reqlock); + reqq_push (&req_queue, req); + X_COND_SIGNAL (reqwait); + X_UNLOCK (reqlock); + + X_LOCK (wrklock); + --started; + X_UNLOCK (wrklock); +} + +static int +etp_poll (void) +{ + unsigned int maxreqs; + unsigned int maxtime; + struct timeval tv_start, tv_now; + + X_LOCK (reslock); + maxreqs = max_poll_reqs; + maxtime = max_poll_time; + X_UNLOCK (reslock); + + if (maxtime) + gettimeofday (&tv_start, 0); + + for (;;) + { + ETP_REQ *req; + + etp_maybe_start_thread (); + + X_LOCK (reslock); + req = reqq_shift (&res_queue); + + if (req) + { + --npending; + + if (!res_queue.size && done_poll_cb) + done_poll_cb (); + } + + X_UNLOCK (reslock); + + if (!req) + return 0; + + X_LOCK (reqlock); + --nreqs; + X_UNLOCK (reqlock); + + if (ecb_expect_false (req->type == EIO_GROUP && req->size)) + { + req->int1 = 1; /* mark request as delayed */ + continue; + } + else + { + int res = ETP_FINISH (req); + if (ecb_expect_false (res)) + return res; + } + + if (ecb_expect_false (maxreqs && !--maxreqs)) + break; + + if (maxtime) + { + gettimeofday (&tv_now, 0); + + if (tvdiff (&tv_start, &tv_now) >= maxtime) + break; + } + } + + errno = EAGAIN; + return -1; +} + +static void +etp_cancel (ETP_REQ *req) +{ + req->cancelled = 1; + + eio_grp_cancel (req); +} + +static void +etp_submit (ETP_REQ *req) +{ + req->pri -= ETP_PRI_MIN; + + if (ecb_expect_false (req->pri < ETP_PRI_MIN - ETP_PRI_MIN)) req->pri = ETP_PRI_MIN - ETP_PRI_MIN; + if (ecb_expect_false (req->pri > ETP_PRI_MAX - ETP_PRI_MIN)) req->pri = ETP_PRI_MAX - ETP_PRI_MIN; + + if (ecb_expect_false (req->type == EIO_GROUP)) + { + /* I hope this is worth it :/ */ + X_LOCK (reqlock); + ++nreqs; + X_UNLOCK (reqlock); + + X_LOCK (reslock); + + ++npending; + + if (!reqq_push (&res_queue, req) && want_poll_cb) + want_poll_cb (); + + X_UNLOCK (reslock); + } + else + { + X_LOCK (reqlock); + ++nreqs; + ++nready; + reqq_push (&req_queue, req); + X_COND_SIGNAL (reqwait); + X_UNLOCK (reqlock); + + etp_maybe_start_thread (); + } +} + +static void ecb_cold +etp_set_max_poll_time (double nseconds) +{ + if (WORDACCESS_UNSAFE) X_LOCK (reslock); + max_poll_time = nseconds * EIO_TICKS; + if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); +} + +static void ecb_cold +etp_set_max_poll_reqs (unsigned int maxreqs) +{ + if (WORDACCESS_UNSAFE) X_LOCK (reslock); + max_poll_reqs = maxreqs; + if (WORDACCESS_UNSAFE) X_UNLOCK (reslock); +} + +static void ecb_cold +etp_set_max_idle (unsigned int nthreads) +{ + if (WORDACCESS_UNSAFE) X_LOCK (reqlock); + max_idle = nthreads; + if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); +} + +static void ecb_cold +etp_set_idle_timeout (unsigned int seconds) +{ + if (WORDACCESS_UNSAFE) X_LOCK (reqlock); + idle_timeout = seconds; + if (WORDACCESS_UNSAFE) X_UNLOCK (reqlock); +} + +static void ecb_cold +etp_set_min_parallel (unsigned int nthreads) +{ + if (wanted < nthreads) + wanted = nthreads; +} + +static void ecb_cold +etp_set_max_parallel (unsigned int nthreads) +{ + if (wanted > nthreads) + wanted = nthreads; + + while (started > wanted) + etp_end_thread (); +} + +/*****************************************************************************/ + +static void +grp_try_feed (eio_req *grp) +{ + while (grp->size < grp->int2 && !EIO_CANCELLED (grp)) + { + grp->flags &= ~EIO_FLAG_GROUPADD; + + EIO_FEED (grp); + + /* stop if no progress has been made */ + if (!(grp->flags & EIO_FLAG_GROUPADD)) + { + grp->feed = 0; + break; + } + } +} + +static int +grp_dec (eio_req *grp) +{ + --grp->size; + + /* call feeder, if applicable */ + grp_try_feed (grp); + + /* finish, if done */ + if (!grp->size && grp->int1) + return eio_finish (grp); + else + return 0; +} + +static void +eio_destroy (eio_req *req) +{ + if ((req)->flags & EIO_FLAG_PTR1_FREE) free (req->ptr1); + if ((req)->flags & EIO_FLAG_PTR2_FREE) free (req->ptr2); + + EIO_DESTROY (req); +} + +static int +eio_finish (eio_req *req) +{ + int res = EIO_FINISH (req); + + if (req->grp) + { + int res2; + eio_req *grp = req->grp; + + /* unlink request */ + if (req->grp_next) req->grp_next->grp_prev = req->grp_prev; + if (req->grp_prev) req->grp_prev->grp_next = req->grp_next; + + if (grp->grp_first == req) + grp->grp_first = req->grp_next; + + res2 = grp_dec (grp); + + if (!res) + res = res2; + } + + eio_destroy (req); + + return res; +} + +void +eio_grp_cancel (eio_req *grp) +{ + for (grp = grp->grp_first; grp; grp = grp->grp_next) + eio_cancel (grp); +} + +void +eio_cancel (eio_req *req) +{ + etp_cancel (req); +} + +void +eio_submit (eio_req *req) +{ + etp_submit (req); +} + +unsigned int +eio_nreqs (void) +{ + return etp_nreqs (); +} + +unsigned int +eio_nready (void) +{ + return etp_nready (); +} + +unsigned int +eio_npending (void) +{ + return etp_npending (); +} + +unsigned int ecb_cold +eio_nthreads (void) +{ + return etp_nthreads (); +} + +void ecb_cold +eio_set_max_poll_time (double nseconds) +{ + etp_set_max_poll_time (nseconds); +} + +void ecb_cold +eio_set_max_poll_reqs (unsigned int maxreqs) +{ + etp_set_max_poll_reqs (maxreqs); +} + +void ecb_cold +eio_set_max_idle (unsigned int nthreads) +{ + etp_set_max_idle (nthreads); +} + +void ecb_cold +eio_set_idle_timeout (unsigned int seconds) +{ + etp_set_idle_timeout (seconds); +} + +void ecb_cold +eio_set_min_parallel (unsigned int nthreads) +{ + etp_set_min_parallel (nthreads); +} + +void ecb_cold +eio_set_max_parallel (unsigned int nthreads) +{ + etp_set_max_parallel (nthreads); +} + +int eio_poll (void) +{ + return etp_poll (); +} + +/*****************************************************************************/ +/* work around various missing functions */ + +#if !HAVE_PREADWRITE +# undef pread +# undef pwrite +# define pread eio__pread +# define pwrite eio__pwrite + +static eio_ssize_t +eio__pread (int fd, void *buf, size_t count, off_t offset) +{ + eio_ssize_t res; + off_t ooffset; + + X_LOCK (preadwritelock); + ooffset = lseek (fd, 0, SEEK_CUR); + lseek (fd, offset, SEEK_SET); + res = read (fd, buf, count); + lseek (fd, ooffset, SEEK_SET); + X_UNLOCK (preadwritelock); + + return res; +} + +static eio_ssize_t +eio__pwrite (int fd, void *buf, size_t count, off_t offset) +{ + eio_ssize_t res; + off_t ooffset; + + X_LOCK (preadwritelock); + ooffset = lseek (fd, 0, SEEK_CUR); + lseek (fd, offset, SEEK_SET); + res = write (fd, buf, count); + lseek (fd, ooffset, SEEK_SET); + X_UNLOCK (preadwritelock); + + return res; +} +#endif + +#ifndef HAVE_UTIMES + +# undef utimes +# define utimes(path,times) eio__utimes (path, times) + +static int +eio__utimes (const char *filename, const struct timeval times[2]) +{ + if (times) + { + struct utimbuf buf; + + buf.actime = times[0].tv_sec; + buf.modtime = times[1].tv_sec; + + return utime (filename, &buf); + } + else + return utime (filename, 0); +} + +#endif + +#ifndef HAVE_FUTIMES + +# undef futimes +# define futimes(fd,times) eio__futimes (fd, times) + +static int +eio__futimes (int fd, const struct timeval tv[2]) +{ + errno = ENOSYS; + return -1; +} + +#endif + +#if !HAVE_FDATASYNC +# undef fdatasync +# define fdatasync(fd) fsync (fd) +#endif + +static int +eio__syncfs (int fd) +{ + int res; + +#if HAVE_SYS_SYNCFS + res = (int)syscall (__NR_syncfs, (int)(fd)); +#else + res = -1; + errno = ENOSYS; +#endif + + if (res < 0 && errno == ENOSYS && fd >= 0) + sync (); + + return res; +} + +/* sync_file_range always needs emulation */ +static int +eio__sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags) +{ +#if HAVE_SYNC_FILE_RANGE + int res; + + if (EIO_SYNC_FILE_RANGE_WAIT_BEFORE != SYNC_FILE_RANGE_WAIT_BEFORE + || EIO_SYNC_FILE_RANGE_WRITE != SYNC_FILE_RANGE_WRITE + || EIO_SYNC_FILE_RANGE_WAIT_AFTER != SYNC_FILE_RANGE_WAIT_AFTER) + { + flags = 0 + | (flags & EIO_SYNC_FILE_RANGE_WAIT_BEFORE ? SYNC_FILE_RANGE_WAIT_BEFORE : 0) + | (flags & EIO_SYNC_FILE_RANGE_WRITE ? SYNC_FILE_RANGE_WRITE : 0) + | (flags & EIO_SYNC_FILE_RANGE_WAIT_AFTER ? SYNC_FILE_RANGE_WAIT_AFTER : 0); + } + + res = sync_file_range (fd, offset, nbytes, flags); + + if (!res || errno != ENOSYS) + return res; +#endif + + /* even though we could play tricks with the flags, it's better to always + * call fdatasync, as that matches the expectation of its users best */ + return fdatasync (fd); +} + +static int +eio__fallocate (int fd, int mode, off_t offset, size_t len) +{ +#if HAVE_FALLOCATE + return fallocate (fd, mode, offset, len); +#else + errno = ENOSYS; + return -1; +#endif +} + +#if !HAVE_READAHEAD +# undef readahead +# define readahead(fd,offset,count) eio__readahead (fd, offset, count, self) + +static eio_ssize_t +eio__readahead (int fd, off_t offset, size_t count, etp_worker *self) +{ + size_t todo = count; + dBUF; + + while (todo > 0) + { + size_t len = todo < EIO_BUFSIZE ? todo : EIO_BUFSIZE; + + pread (fd, eio_buf, len, offset); + offset += len; + todo -= len; + } + + FUBd; + + errno = 0; + return count; +} + +#endif + +/* sendfile always needs emulation */ +static eio_ssize_t +eio__sendfile (int ofd, int ifd, off_t offset, size_t count) +{ + eio_ssize_t written = 0; + eio_ssize_t res; + + if (!count) + return 0; + + for (;;) + { +#ifdef __APPLE__ +# undef HAVE_SENDFILE /* broken, as everything on os x */ +#endif +#if HAVE_SENDFILE +# if __linux + off_t soffset = offset; + res = sendfile (ofd, ifd, &soffset, count); + +# elif __FreeBSD__ + /* + * Of course, the freebsd sendfile is a dire hack with no thoughts + * wasted on making it similar to other I/O functions. + */ + off_t sbytes; + res = sendfile (ifd, ofd, offset, count, 0, &sbytes, 0); + + #if 0 /* according to the manpage, this is correct, but broken behaviour */ + /* freebsd' sendfile will return 0 on success */ + /* freebsd 8 documents it as only setting *sbytes on EINTR and EAGAIN, but */ + /* not on e.g. EIO or EPIPE - sounds broken */ + if ((res < 0 && (errno == EAGAIN || errno == EINTR) && sbytes) || res == 0) + res = sbytes; + #endif + + /* according to source inspection, this is correct, and useful behaviour */ + if (sbytes) + res = sbytes; + +# elif defined (__APPLE__) + off_t sbytes = count; + res = sendfile (ifd, ofd, offset, &sbytes, 0, 0); + + /* according to the manpage, sbytes is always valid */ + if (sbytes) + res = sbytes; + +# elif __hpux + res = sendfile (ofd, ifd, offset, count, 0, 0); + +# elif __solaris + struct sendfilevec vec; + size_t sbytes; + + vec.sfv_fd = ifd; + vec.sfv_flag = 0; + vec.sfv_off = offset; + vec.sfv_len = count; + + res = sendfilev (ofd, &vec, 1, &sbytes); + + if (res < 0 && sbytes) + res = sbytes; + +# endif + +#elif defined (_WIN32) && 0 + /* does not work, just for documentation of what would need to be done */ + /* actually, cannot be done like this, as TransmitFile changes the file offset, */ + /* libeio guarantees that the file offset does not change, and windows */ + /* has no way to get an independent handle to the same file description */ + HANDLE h = TO_SOCKET (ifd); + SetFilePointer (h, offset, 0, FILE_BEGIN); + res = TransmitFile (TO_SOCKET (ofd), h, count, 0, 0, 0, 0); + +#else + res = -1; + errno = ENOSYS; +#endif + + /* we assume sendfile can copy at least 128mb in one go */ + if (res <= 128 * 1024 * 1024) + { + if (res > 0) + written += res; + + if (written) + return written; + + break; + } + else + { + /* if we requested more, then probably the kernel was lazy */ + written += res; + offset += res; + count -= res; + + if (!count) + return written; + } + } + + if (res < 0 + && (errno == ENOSYS || errno == EINVAL || errno == ENOTSOCK + /* BSDs */ +#ifdef ENOTSUP /* sigh, if the steenking pile called openbsd would only try to at least compile posix code... */ + || errno == ENOTSUP +#endif +#ifdef EOPNOTSUPP /* windows */ + || errno == EOPNOTSUPP /* BSDs */ +#endif +#if __solaris + || errno == EAFNOSUPPORT || errno == EPROTOTYPE +#endif + ) + ) + { + /* emulate sendfile. this is a major pain in the ass */ + dBUF; + + res = 0; + + while (count) + { + eio_ssize_t cnt; + + cnt = pread (ifd, eio_buf, count > EIO_BUFSIZE ? EIO_BUFSIZE : count, offset); + + if (cnt <= 0) + { + if (cnt && !res) res = -1; + break; + } + + cnt = write (ofd, eio_buf, cnt); + + if (cnt <= 0) + { + if (cnt && !res) res = -1; + break; + } + + offset += cnt; + res += cnt; + count -= cnt; + } + + FUBd; + } + + return res; +} + +#ifdef PAGESIZE +# define eio_pagesize() PAGESIZE +#else +static intptr_t +eio_pagesize (void) +{ + static intptr_t page; + + if (!page) + page = sysconf (_SC_PAGESIZE); + + return page; +} +#endif + +static void +eio_page_align (void **addr, size_t *length) +{ + intptr_t mask = eio_pagesize () - 1; + + /* round down addr */ + intptr_t adj = mask & (intptr_t)*addr; + + *addr = (void *)((intptr_t)*addr - adj); + *length += adj; + + /* round up length */ + *length = (*length + mask) & ~mask; +} + +#if !_POSIX_MEMLOCK +# define eio__mlockall(a) EIO_ENOSYS () +#else + +static int +eio__mlockall (int flags) +{ + #if __GLIBC__ == 2 && __GLIBC_MINOR__ <= 7 + extern int mallopt (int, int); + mallopt (-6, 238); /* http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=473812 */ + #endif + + if (EIO_MCL_CURRENT != MCL_CURRENT + || EIO_MCL_FUTURE != MCL_FUTURE) + { + flags = 0 + | (flags & EIO_MCL_CURRENT ? MCL_CURRENT : 0) + | (flags & EIO_MCL_FUTURE ? MCL_FUTURE : 0); + } + + return mlockall (flags); +} +#endif + +#if !_POSIX_MEMLOCK_RANGE +# define eio__mlock(a,b) EIO_ENOSYS () +#else + +static int +eio__mlock (void *addr, size_t length) +{ + eio_page_align (&addr, &length); + + return mlock (addr, length); +} + +#endif + +#if !(_POSIX_MAPPED_FILES && _POSIX_SYNCHRONIZED_IO) +# define eio__msync(a,b,c) EIO_ENOSYS () +#else + +static int +eio__msync (void *mem, size_t len, int flags) +{ + eio_page_align (&mem, &len); + + if (EIO_MS_ASYNC != MS_SYNC + || EIO_MS_INVALIDATE != MS_INVALIDATE + || EIO_MS_SYNC != MS_SYNC) + { + flags = 0 + | (flags & EIO_MS_ASYNC ? MS_ASYNC : 0) + | (flags & EIO_MS_INVALIDATE ? MS_INVALIDATE : 0) + | (flags & EIO_MS_SYNC ? MS_SYNC : 0); + } + + return msync (mem, len, flags); +} + +#endif + +static int +eio__mtouch (eio_req *req) +{ + void *mem = req->ptr2; + size_t len = req->size; + int flags = req->int1; + + eio_page_align (&mem, &len); + + { + intptr_t addr = (intptr_t)mem; + intptr_t end = addr + len; + intptr_t page = eio_pagesize (); + + if (addr < end) { + if (flags & EIO_MT_MODIFY) /* modify */ + do { *((volatile sig_atomic_t *)addr) |= 0; } while ((addr += page) < len && !EIO_CANCELLED (req)); + else + do { *((volatile sig_atomic_t *)addr) ; } while ((addr += page) < len && !EIO_CANCELLED (req)); + } + } + + return 0; +} + +/*****************************************************************************/ +/* requests implemented outside eio_execute, because they are so large */ + +/* result will always end up in tmpbuf, there is always space for adding a 0-byte */ +static int +eio__realpath (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) +{ + const char *rel = path; + char *res; + char *tmp1, *tmp2; +#if SYMLOOP_MAX > 32 + int symlinks = SYMLOOP_MAX; +#else + int symlinks = 32; +#endif + + errno = EINVAL; + if (!rel) + return -1; + + errno = ENOENT; + if (!*rel) + return -1; + + res = tmpbuf_get (tmpbuf, PATH_MAX * 3); + tmp1 = res + PATH_MAX; + tmp2 = tmp1 + PATH_MAX; + +#if 0 /* disabled, the musl way to do things is just too racy */ +#if __linux && defined(O_NONBLOCK) && defined(O_NOATIME) + /* on linux we may be able to ask the kernel */ + { + int fd = open (rel, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOATIME); + + if (fd >= 0) + { + sprintf (tmp1, "/proc/self/fd/%d", fd); + req->result = readlink (tmp1, res, PATH_MAX); + close (fd); + + /* here we should probably stat the open file and the disk file, to make sure they still match */ + + if (req->result > 0) + goto done; + } + else if (errno == ELOOP || errno == ENAMETOOLONG || errno == ENOENT || errno == ENOTDIR || errno == EIO) + return; + } +#endif +#endif + + if (*rel != '/') + { + int len; + + errno = ENOENT; + if (wd == EIO_INVALID_WD) + return -1; + + if (wd == EIO_CWD) + { + if (!getcwd (res, PATH_MAX)) + return -1; + + len = strlen (res); + } + else + memcpy (res, wd->str, len = wd->len); + + if (res [1]) /* only use if not / */ + res += len; + } + + while (*rel) + { + eio_ssize_t len, linklen; + const char *beg = rel; + + while (*rel && *rel != '/') + ++rel; + + len = rel - beg; + + if (!len) /* skip slashes */ + { + ++rel; + continue; + } + + if (beg [0] == '.') + { + if (len == 1) + continue; /* . - nop */ + + if (beg [1] == '.' && len == 2) + { + /* .. - back up one component, if possible */ + + while (res != tmpbuf->ptr) + if (*--res == '/') + break; + + continue; + } + } + + errno = ENAMETOOLONG; + if (res + 1 + len + 1 >= tmp1) + return -1; + + /* copy one component */ + *res = '/'; + memcpy (res + 1, beg, len); + + /* zero-terminate, for readlink */ + res [len + 1] = 0; + + /* now check if it's a symlink */ + linklen = readlink (tmpbuf->ptr, tmp1, PATH_MAX); + + if (linklen < 0) + { + if (errno != EINVAL) + return -1; + + /* it's a normal directory. hopefully */ + res += len + 1; + } + else + { + /* yay, it was a symlink - build new path in tmp2 */ + int rellen = strlen (rel); + + errno = ENAMETOOLONG; + if (linklen + 1 + rellen >= PATH_MAX) + return -1; + + errno = ELOOP; + if (!--symlinks) + return -1; + + if (*tmp1 == '/') + res = tmpbuf->ptr; /* symlink resolves to an absolute path */ + + /* we need to be careful, as rel might point into tmp2 already */ + memmove (tmp2 + linklen + 1, rel, rellen + 1); + tmp2 [linklen] = '/'; + memcpy (tmp2, tmp1, linklen); + + rel = tmp2; + } + } + + /* special case for the lone root path */ + if (res == tmpbuf->ptr) + *res++ = '/'; + + return res - (char *)tmpbuf->ptr; +} + +static signed char +eio_dent_cmp (const eio_dirent *a, const eio_dirent *b) +{ + return a->score - b->score ? a->score - b->score /* works because our signed char is always 0..100 */ + : a->inode < b->inode ? -1 + : a->inode > b->inode ? 1 + : 0; +} + +#define EIO_DENT_CMP(i,op,j) eio_dent_cmp (&i, &j) op 0 + +#define EIO_SORT_CUTOFF 30 /* quite high, but performs well on many filesystems */ +#define EIO_SORT_FAST 60 /* when to only use insertion sort */ + +static void +eio_dent_radix_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits) +{ + unsigned char bits [9 + sizeof (eio_ino_t) * 8]; + unsigned char *bit = bits; + + assert (CHAR_BIT == 8); + assert (sizeof (eio_dirent) * 8 < 256); + assert (offsetof (eio_dirent, inode)); /* we use bit #0 as sentinel */ + assert (offsetof (eio_dirent, score)); /* we use bit #0 as sentinel */ + + if (size <= EIO_SORT_FAST) + return; + + /* first prepare an array of bits to test in our radix sort */ + /* try to take endianness into account, as well as differences in eio_ino_t sizes */ + /* inode_bits must contain all inodes ORed together */ + /* which is used to skip bits that are 0 everywhere, which is very common */ + { + eio_ino_t endianness; + int i, j; + + /* we store the byte offset of byte n into byte n of "endianness" */ + for (i = 0; i < sizeof (eio_ino_t); ++i) + ((unsigned char *)&endianness)[i] = i; + + *bit++ = 0; + + for (i = 0; i < sizeof (eio_ino_t); ++i) + { + /* shifting off the byte offsets out of "endianness" */ + int offs = (offsetof (eio_dirent, inode) + (endianness & 0xff)) * 8; + endianness >>= 8; + + for (j = 0; j < 8; ++j) + if (inode_bits & (((eio_ino_t)1) << (i * 8 + j))) + *bit++ = offs + j; + } + + for (j = 0; j < 8; ++j) + if (score_bits & (1 << j)) + *bit++ = offsetof (eio_dirent, score) * 8 + j; + } + + /* now actually do the sorting (a variant of MSD radix sort) */ + { + eio_dirent *base_stk [9 + sizeof (eio_ino_t) * 8], *base; + eio_dirent *end_stk [9 + sizeof (eio_ino_t) * 8], *end; + unsigned char *bit_stk [9 + sizeof (eio_ino_t) * 8]; + int stk_idx = 0; + + base_stk [stk_idx] = dents; + end_stk [stk_idx] = dents + size; + bit_stk [stk_idx] = bit - 1; + + do + { + base = base_stk [stk_idx]; + end = end_stk [stk_idx]; + bit = bit_stk [stk_idx]; + + for (;;) + { + unsigned char O = *bit >> 3; + unsigned char M = 1 << (*bit & 7); + + eio_dirent *a = base; + eio_dirent *b = end; + + if (b - a < EIO_SORT_CUTOFF) + break; + + /* now bit-partition the array on the bit */ + /* this ugly asymmetric loop seems to perform much better than typical */ + /* partition algos found in the literature */ + do + if (!(((unsigned char *)a)[O] & M)) + ++a; + else if (!(((unsigned char *)--b)[O] & M)) + { + eio_dirent tmp = *a; *a = *b; *b = tmp; + ++a; + } + while (b > a); + + /* next bit, or stop, if no bits left in this path */ + if (!*--bit) + break; + + base_stk [stk_idx] = a; + end_stk [stk_idx] = end; + bit_stk [stk_idx] = bit; + ++stk_idx; + + end = a; + } + } + while (stk_idx--); + } +} + +static void +eio_dent_insertion_sort (eio_dirent *dents, int size) +{ + /* first move the smallest element to the front, to act as a sentinel */ + { + int i; + eio_dirent *min = dents; + + /* the radix pre-pass ensures that the minimum element is in the first EIO_SORT_CUTOFF + 1 elements */ + for (i = size > EIO_SORT_FAST ? EIO_SORT_CUTOFF + 1 : size; --i; ) + if (EIO_DENT_CMP (dents [i], <, *min)) + min = &dents [i]; + + /* swap elements 0 and j (minimum) */ + { + eio_dirent tmp = *dents; *dents = *min; *min = tmp; + } + } + + /* then do standard insertion sort, assuming that all elements are >= dents [0] */ + { + eio_dirent *i, *j; + + for (i = dents + 1; i < dents + size; ++i) + { + eio_dirent value = *i; + + for (j = i - 1; EIO_DENT_CMP (*j, >, value); --j) + j [1] = j [0]; + + j [1] = value; + } + } +} + +static void +eio_dent_sort (eio_dirent *dents, int size, signed char score_bits, eio_ino_t inode_bits) +{ + if (size <= 1) + return; /* our insertion sort relies on size > 0 */ + + /* first we use a radix sort, but only for dirs >= EIO_SORT_FAST */ + /* and stop sorting when the partitions are <= EIO_SORT_CUTOFF */ + eio_dent_radix_sort (dents, size, score_bits, inode_bits); + + /* use an insertion sort at the end, or for small arrays, */ + /* as insertion sort is more efficient for small partitions */ + eio_dent_insertion_sort (dents, size); +} + +/* read a full directory */ +static void +eio__scandir (eio_req *req, etp_worker *self) +{ + char *name, *names; + int namesalloc = 4096 - sizeof (void *) * 4; + int namesoffs = 0; + int flags = req->int1; + eio_dirent *dents = 0; + int dentalloc = 128; + int dentoffs = 0; + eio_ino_t inode_bits = 0; +#ifdef _WIN32 + HANDLE dirp; + WIN32_FIND_DATA entp; +#else + DIR *dirp; + EIO_STRUCT_DIRENT *entp; +#endif + + req->result = -1; + + if (!(flags & EIO_READDIR_DENTS)) + flags &= ~(EIO_READDIR_DIRS_FIRST | EIO_READDIR_STAT_ORDER); + +#ifdef _WIN32 + { + int len = strlen ((const char *)req->ptr1); + char *path = malloc (MAX_PATH); + const char *fmt; + const char *reqpath = wd_expand (&self->tmpbuf, req->wd, req->ptr1); + + if (!len) + fmt = "./*"; + else if (reqpath[len - 1] == '/' || reqpath[len - 1] == '\\') + fmt = "%s*"; + else + fmt = "%s/*"; + + _snprintf (path, MAX_PATH, fmt, reqpath); + dirp = FindFirstFile (path, &entp); + free (path); + + if (dirp == INVALID_HANDLE_VALUE) + { + /* should steal _dosmaperr */ + switch (GetLastError ()) + { + case ERROR_FILE_NOT_FOUND: + req->result = 0; + break; + + case ERROR_INVALID_NAME: + case ERROR_PATH_NOT_FOUND: + case ERROR_NO_MORE_FILES: + errno = ENOENT; + break; + + case ERROR_NOT_ENOUGH_MEMORY: + errno = ENOMEM; + break; + + default: + errno = EINVAL; + break; + } + + return; + } + } +#else + #if HAVE_AT + if (req->wd) + { + int fd = openat (WD2FD (req->wd), req->ptr1, O_CLOEXEC | O_SEARCH | O_DIRECTORY); + + if (fd < 0) + return; + + dirp = fdopendir (fd); + + if (!dirp) + close (fd); + } + else + dirp = opendir (req->ptr1); + #else + dirp = opendir (wd_expand (&self->tmpbuf, req->wd, req->ptr1)); + #endif + + if (!dirp) + return; +#endif + + if (req->flags & EIO_FLAG_PTR1_FREE) + free (req->ptr1); + + req->flags |= EIO_FLAG_PTR1_FREE | EIO_FLAG_PTR2_FREE; + req->ptr1 = dents = flags ? malloc (dentalloc * sizeof (eio_dirent)) : 0; + req->ptr2 = names = malloc (namesalloc); + + if (!names || (flags && !dents)) + return; + + for (;;) + { + int done; + +#ifdef _WIN32 + done = !dirp; +#else + errno = 0; + entp = readdir (dirp); + done = !entp; +#endif + + if (done) + { +#ifndef _WIN32 + int old_errno = errno; + closedir (dirp); + errno = old_errno; + + if (errno) + break; +#endif + + /* sort etc. */ + req->int1 = flags; + req->result = dentoffs; + + if (flags & EIO_READDIR_STAT_ORDER) + eio_dent_sort (dents, dentoffs, flags & EIO_READDIR_DIRS_FIRST ? 7 : 0, inode_bits); + else if (flags & EIO_READDIR_DIRS_FIRST) { + if (flags & EIO_READDIR_FOUND_UNKNOWN) + eio_dent_sort (dents, dentoffs, 7, inode_bits); /* sort by score and inode */ + else { + /* in this case, all is known, and we just put dirs first and sort them */ + eio_dirent *oth = dents + dentoffs; + eio_dirent *dir = dents; + + /* now partition dirs to the front, and non-dirs to the back */ + /* by walking from both sides and swapping if necessary */ + while (oth > dir) + { + if (dir->type == EIO_DT_DIR) + ++dir; + else if ((--oth)->type == EIO_DT_DIR) + { + eio_dirent tmp = *dir; *dir = *oth; *oth = tmp; + + ++dir; + } + } + + /* now sort the dirs only (dirs all have the same score) */ + eio_dent_sort (dents, dir - dents, 0, inode_bits); + } + } + + break; + } + + /* now add the entry to our list(s) */ + name = D_NAME (entp); + + /* skip . and .. entries */ + if (name [0] != '.' || (name [1] && (name [1] != '.' || name [2]))) + { + int len = D_NAMLEN (entp) + 1; + + while (ecb_expect_false (namesoffs + len > namesalloc)) + { + namesalloc *= 2; + req->ptr2 = names = realloc (names, namesalloc); + + if (!names) + break; + } + + memcpy (names + namesoffs, name, len); + + if (dents) + { + struct eio_dirent *ent; + + if (ecb_expect_false (dentoffs == dentalloc)) + { + dentalloc *= 2; + req->ptr1 = dents = realloc (dents, dentalloc * sizeof (eio_dirent)); + + if (!dents) + break; + } + + ent = dents + dentoffs; + + ent->nameofs = namesoffs; /* rather dirtily we store the offset in the pointer */ + ent->namelen = len - 1; + ent->inode = D_INO (entp); + + inode_bits |= ent->inode; + + switch (D_TYPE (entp)) + { + default: + ent->type = EIO_DT_UNKNOWN; + flags |= EIO_READDIR_FOUND_UNKNOWN; + break; + + #ifdef DT_FIFO + case DT_FIFO: ent->type = EIO_DT_FIFO; break; + #endif + #ifdef DT_CHR + case DT_CHR: ent->type = EIO_DT_CHR; break; + #endif + #ifdef DT_MPC + case DT_MPC: ent->type = EIO_DT_MPC; break; + #endif + #ifdef DT_DIR + case DT_DIR: ent->type = EIO_DT_DIR; break; + #endif + #ifdef DT_NAM + case DT_NAM: ent->type = EIO_DT_NAM; break; + #endif + #ifdef DT_BLK + case DT_BLK: ent->type = EIO_DT_BLK; break; + #endif + #ifdef DT_MPB + case DT_MPB: ent->type = EIO_DT_MPB; break; + #endif + #ifdef DT_REG + case DT_REG: ent->type = EIO_DT_REG; break; + #endif + #ifdef DT_NWK + case DT_NWK: ent->type = EIO_DT_NWK; break; + #endif + #ifdef DT_CMP + case DT_CMP: ent->type = EIO_DT_CMP; break; + #endif + #ifdef DT_LNK + case DT_LNK: ent->type = EIO_DT_LNK; break; + #endif + #ifdef DT_SOCK + case DT_SOCK: ent->type = EIO_DT_SOCK; break; + #endif + #ifdef DT_DOOR + case DT_DOOR: ent->type = EIO_DT_DOOR; break; + #endif + #ifdef DT_WHT + case DT_WHT: ent->type = EIO_DT_WHT; break; + #endif + } + + ent->score = 7; + + if (flags & EIO_READDIR_DIRS_FIRST) + { + if (ent->type == EIO_DT_UNKNOWN) + { + if (*name == '.') /* leading dots are likely directories, and, in any case, rare */ + ent->score = 1; + else if (!strchr (name, '.')) /* absense of dots indicate likely dirs */ + ent->score = len <= 2 ? 4 - len : len <= 4 ? 4 : len <= 7 ? 5 : 6; /* shorter == more likely dir, but avoid too many classes */ + } + else if (ent->type == EIO_DT_DIR) + ent->score = 0; + } + } + + namesoffs += len; + ++dentoffs; + } + + if (EIO_CANCELLED (req)) + { + errno = ECANCELED; + break; + } + +#ifdef _WIN32 + if (!FindNextFile (dirp, &entp)) + { + FindClose (dirp); + dirp = 0; + } +#endif + } +} + +/*****************************************************************************/ +/* working directory stuff */ +/* various deficiencies in the posix 2008 api force us to */ +/* keep the absolute path in string form at all times */ +/* fuck yeah. */ + +#if !HAVE_AT + +/* a bit like realpath, but usually faster because it doesn'T have to return */ +/* an absolute or canonical path */ +static const char * +wd_expand (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) +{ + if (!wd || *path == '/') + return path; + + if (path [0] == '.' && !path [1]) + return wd->str; + + { + int l1 = wd->len; + int l2 = strlen (path); + + char *res = tmpbuf_get (tmpbuf, l1 + l2 + 2); + + memcpy (res, wd->str, l1); + res [l1] = '/'; + memcpy (res + l1 + 1, path, l2 + 1); + + return res; + } +} + +#endif + +static eio_wd +eio__wd_open_sync (struct tmpbuf *tmpbuf, eio_wd wd, const char *path) +{ + int fd; + eio_wd res; + int len = eio__realpath (tmpbuf, wd, path); + + if (len < 0) + return EIO_INVALID_WD; + +#if HAVE_AT + fd = openat (WD2FD (wd), path, O_CLOEXEC | O_SEARCH | O_DIRECTORY); + + if (fd < 0) + return EIO_INVALID_WD; +#endif + + res = malloc (sizeof (*res) + len); /* one extra 0-byte */ + +#if HAVE_AT + res->fd = fd; +#endif + + res->len = len; + memcpy (res->str, tmpbuf->ptr, len); + res->str [len] = 0; + + return res; +} + +eio_wd +eio_wd_open_sync (eio_wd wd, const char *path) +{ + struct tmpbuf tmpbuf = { 0 }; + wd = eio__wd_open_sync (&tmpbuf, wd, path); + free (tmpbuf.ptr); + + return wd; +} + +void +eio_wd_close_sync (eio_wd wd) +{ + if (wd != EIO_INVALID_WD && wd != EIO_CWD) + { + #if HAVE_AT + close (wd->fd); + #endif + free (wd); + } +} + +#if HAVE_AT + +/* they forgot these */ + +static int +eio__truncateat (int dirfd, const char *path, off_t length) +{ + int fd = openat (dirfd, path, O_WRONLY | O_CLOEXEC); + int res; + + if (fd < 0) + return fd; + + res = ftruncate (fd, length); + close (fd); + return res; +} + +static int +eio__statvfsat (int dirfd, const char *path, struct statvfs *buf) +{ + int fd = openat (dirfd, path, O_SEARCH | O_CLOEXEC); + int res; + + if (fd < 0) + return fd; + + res = fstatvfs (fd, buf); + close (fd); + return res; + +} + +#endif + +/*****************************************************************************/ + +#define ALLOC(len) \ + if (!req->ptr2) \ + { \ + X_LOCK (wrklock); \ + req->flags |= EIO_FLAG_PTR2_FREE; \ + X_UNLOCK (wrklock); \ + req->ptr2 = malloc (len); \ + if (!req->ptr2) \ + { \ + errno = ENOMEM; \ + req->result = -1; \ + break; \ + } \ + } + +static void ecb_noinline ecb_cold +etp_proc_init (void) +{ +#if HAVE_PRCTL_SET_NAME + /* provide a more sensible "thread name" */ + char name[16 + 1]; + const int namelen = sizeof (name) - 1; + int len; + + prctl (PR_GET_NAME, (unsigned long)name, 0, 0, 0); + name [namelen] = 0; + len = strlen (name); + strcpy (name + (len <= namelen - 4 ? len : namelen - 4), "/eio"); + prctl (PR_SET_NAME, (unsigned long)name, 0, 0, 0); +#endif +} + +X_THREAD_PROC (etp_proc) +{ + ETP_REQ *req; + struct timespec ts; + etp_worker *self = (etp_worker *)thr_arg; + + etp_proc_init (); + + /* try to distribute timeouts somewhat evenly */ + ts.tv_nsec = ((unsigned long)self & 1023UL) * (1000000000UL / 1024UL); + + for (;;) + { + ts.tv_sec = 0; + + X_LOCK (reqlock); + + for (;;) + { + req = reqq_shift (&req_queue); + + if (req) + break; + + if (ts.tv_sec == 1) /* no request, but timeout detected, let's quit */ + { + X_UNLOCK (reqlock); + X_LOCK (wrklock); + --started; + X_UNLOCK (wrklock); + goto quit; + } + + ++idle; + + if (idle <= max_idle) + /* we are allowed to idle, so do so without any timeout */ + X_COND_WAIT (reqwait, reqlock); + else + { + /* initialise timeout once */ + if (!ts.tv_sec) + ts.tv_sec = time (0) + idle_timeout; + + if (X_COND_TIMEDWAIT (reqwait, reqlock, ts) == ETIMEDOUT) + ts.tv_sec = 1; /* assuming this is not a value computed above.,.. */ + } + + --idle; + } + + --nready; + + X_UNLOCK (reqlock); + + if (req->type < 0) + goto quit; + + ETP_EXECUTE (self, req); + + X_LOCK (reslock); + + ++npending; + + if (!reqq_push (&res_queue, req) && want_poll_cb) + want_poll_cb (); + + etp_worker_clear (self); + + X_UNLOCK (reslock); + } + +quit: + free (req); + + X_LOCK (wrklock); + etp_worker_free (self); + X_UNLOCK (wrklock); + + return 0; +} + +/*****************************************************************************/ + +int ecb_cold +eio_init (void (*want_poll)(void), void (*done_poll)(void)) +{ +#if !HAVE_PREADWRITE + X_MUTEX_CREATE (preadwritelock); +#endif + + return etp_init (want_poll, done_poll); +} + +ecb_inline void +eio_api_destroy (eio_req *req) +{ + free (req); +} + +#define REQ(rtype) \ + eio_req *req; \ + \ + req = (eio_req *)calloc (1, sizeof *req); \ + if (!req) \ + return 0; \ + \ + req->type = rtype; \ + req->pri = pri; \ + req->finish = cb; \ + req->data = data; \ + req->destroy = eio_api_destroy; + +#define SEND eio_submit (req); return req + +#define PATH \ + req->flags |= EIO_FLAG_PTR1_FREE; \ + req->ptr1 = strdup (path); \ + if (!req->ptr1) \ + { \ + eio_api_destroy (req); \ + return 0; \ + } + +static void +eio_execute (etp_worker *self, eio_req *req) +{ +#if HAVE_AT + int dirfd; +#else + const char *path; +#endif + + if (ecb_expect_false (EIO_CANCELLED (req))) + { + req->result = -1; + req->errorno = ECANCELED; + return; + } + + if (ecb_expect_false (req->wd == EIO_INVALID_WD)) + { + req->result = -1; + req->errorno = ENOENT; + return; + } + + if (req->type >= EIO_OPEN) + { + #if HAVE_AT + dirfd = WD2FD (req->wd); + #else + path = wd_expand (&self->tmpbuf, req->wd, req->ptr1); + #endif + } + + switch (req->type) + { + case EIO_WD_OPEN: req->wd = eio__wd_open_sync (&self->tmpbuf, req->wd, req->ptr1); + req->result = req->wd == EIO_INVALID_WD ? -1 : 0; + break; + case EIO_WD_CLOSE: req->result = 0; + eio_wd_close_sync (req->wd); break; + + case EIO_READ: ALLOC (req->size); + req->result = req->offs >= 0 + ? pread (req->int1, req->ptr2, req->size, req->offs) + : read (req->int1, req->ptr2, req->size); break; + case EIO_WRITE: req->result = req->offs >= 0 + ? pwrite (req->int1, req->ptr2, req->size, req->offs) + : write (req->int1, req->ptr2, req->size); break; + + case EIO_READAHEAD: req->result = readahead (req->int1, req->offs, req->size); break; + case EIO_SENDFILE: req->result = eio__sendfile (req->int1, req->int2, req->offs, req->size); break; + +#if HAVE_AT + + case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); + req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, 0); break; + case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); + req->result = fstatat (dirfd, req->ptr1, (EIO_STRUCT_STAT *)req->ptr2, AT_SYMLINK_NOFOLLOW); break; + case EIO_CHOWN: req->result = fchownat (dirfd, req->ptr1, req->int2, req->int3, 0); break; + case EIO_CHMOD: req->result = fchmodat (dirfd, req->ptr1, (mode_t)req->int2, 0); break; + case EIO_TRUNCATE: req->result = eio__truncateat (dirfd, req->ptr1, req->offs); break; + case EIO_OPEN: req->result = openat (dirfd, req->ptr1, req->int1, (mode_t)req->int2); break; + + case EIO_UNLINK: req->result = unlinkat (dirfd, req->ptr1, 0); break; + case EIO_RMDIR: req->result = unlinkat (dirfd, req->ptr1, AT_REMOVEDIR); break; + case EIO_MKDIR: req->result = mkdirat (dirfd, req->ptr1, (mode_t)req->int2); break; + case EIO_RENAME: req->result = renameat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2); break; + case EIO_LINK: req->result = linkat (dirfd, req->ptr1, WD2FD ((eio_wd)req->int3), req->ptr2, 0); break; + case EIO_SYMLINK: req->result = symlinkat (req->ptr1, dirfd, req->ptr2); break; + case EIO_MKNOD: req->result = mknodat (dirfd, req->ptr1, (mode_t)req->int2, (dev_t)req->offs); break; + case EIO_READLINK: ALLOC (PATH_MAX); + req->result = readlinkat (dirfd, req->ptr1, req->ptr2, PATH_MAX); break; + case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); + req->result = eio__statvfsat (dirfd, req->ptr1, (EIO_STRUCT_STATVFS *)req->ptr2); break; + case EIO_UTIME: + case EIO_FUTIME: + { + struct timespec ts[2]; + struct timespec *times; + + if (req->nv1 != -1. || req->nv2 != -1.) + { + ts[0].tv_sec = req->nv1; + ts[0].tv_nsec = (req->nv1 - ts[0].tv_sec) * 1e9; + ts[1].tv_sec = req->nv2; + ts[1].tv_nsec = (req->nv2 - ts[1].tv_sec) * 1e9; + + times = ts; + } + else + times = 0; + + req->result = req->type == EIO_FUTIME + ? futimens (req->int1, times) + : utimensat (dirfd, req->ptr1, times, 0); + } + break; + +#else + + case EIO_STAT: ALLOC (sizeof (EIO_STRUCT_STAT)); + req->result = stat (path , (EIO_STRUCT_STAT *)req->ptr2); break; + case EIO_LSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); + req->result = lstat (path , (EIO_STRUCT_STAT *)req->ptr2); break; + case EIO_CHOWN: req->result = chown (path , req->int2, req->int3); break; + case EIO_CHMOD: req->result = chmod (path , (mode_t)req->int2); break; + case EIO_TRUNCATE: req->result = truncate (path , req->offs); break; + case EIO_OPEN: req->result = open (path , req->int1, (mode_t)req->int2); break; + + case EIO_UNLINK: req->result = unlink (path ); break; + case EIO_RMDIR: req->result = rmdir (path ); break; + case EIO_MKDIR: req->result = mkdir (path , (mode_t)req->int2); break; + case EIO_RENAME: req->result = rename (path , req->ptr2); break; + case EIO_LINK: req->result = link (path , req->ptr2); break; + case EIO_SYMLINK: req->result = symlink (path , req->ptr2); break; + case EIO_MKNOD: req->result = mknod (path , (mode_t)req->int2, (dev_t)req->offs); break; + case EIO_READLINK: ALLOC (PATH_MAX); + req->result = readlink (path, req->ptr2, PATH_MAX); break; + case EIO_STATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); + req->result = statvfs (path , (EIO_STRUCT_STATVFS *)req->ptr2); break; + + case EIO_UTIME: + case EIO_FUTIME: + { + struct timeval tv[2]; + struct timeval *times; + + if (req->nv1 != -1. || req->nv2 != -1.) + { + tv[0].tv_sec = req->nv1; + tv[0].tv_usec = (req->nv1 - tv[0].tv_sec) * 1e6; + tv[1].tv_sec = req->nv2; + tv[1].tv_usec = (req->nv2 - tv[1].tv_sec) * 1e6; + + times = tv; + } + else + times = 0; + + req->result = req->type == EIO_FUTIME + ? futimes (req->int1, times) + : utimes (req->ptr1, times); + } + break; + +#endif + + case EIO_REALPATH: if (0 <= (req->result = eio__realpath (&self->tmpbuf, req->wd, req->ptr1))) + { + ALLOC (req->result); + memcpy (req->ptr2, self->tmpbuf.ptr, req->result); + } + break; + + case EIO_FSTAT: ALLOC (sizeof (EIO_STRUCT_STAT)); + req->result = fstat (req->int1, (EIO_STRUCT_STAT *)req->ptr2); break; + + case EIO_FSTATVFS: ALLOC (sizeof (EIO_STRUCT_STATVFS)); + req->result = fstatvfs (req->int1, (EIO_STRUCT_STATVFS *)req->ptr2); break; + + case EIO_FCHOWN: req->result = fchown (req->int1, req->int2, req->int3); break; + case EIO_FCHMOD: req->result = fchmod (req->int1, (mode_t)req->int2); break; + case EIO_FTRUNCATE: req->result = ftruncate (req->int1, req->offs); break; + + case EIO_CLOSE: req->result = close (req->int1); break; + case EIO_DUP2: req->result = dup2 (req->int1, req->int2); break; + case EIO_SYNC: req->result = 0; sync (); break; + case EIO_FSYNC: req->result = fsync (req->int1); break; + case EIO_FDATASYNC: req->result = fdatasync (req->int1); break; + case EIO_SYNCFS: req->result = eio__syncfs (req->int1); break; + case EIO_SYNC_FILE_RANGE: req->result = eio__sync_file_range (req->int1, req->offs, req->size, req->int2); break; + case EIO_MSYNC: req->result = eio__msync (req->ptr2, req->size, req->int1); break; + case EIO_MTOUCH: req->result = eio__mtouch (req); break; + case EIO_MLOCK: req->result = eio__mlock (req->ptr2, req->size); break; + case EIO_MLOCKALL: req->result = eio__mlockall (req->int1); break; + case EIO_FALLOCATE: req->result = eio__fallocate (req->int1, req->int2, req->offs, req->size); break; + + case EIO_READDIR: eio__scandir (req, self); break; + + case EIO_BUSY: +#ifdef _WIN32 + Sleep (req->nv1 * 1e3); +#else + { + struct timeval tv; + + tv.tv_sec = req->nv1; + tv.tv_usec = (req->nv1 - tv.tv_sec) * 1e6; + + req->result = select (0, 0, 0, 0, &tv); + } +#endif + break; + + case EIO_GROUP: + abort (); /* handled in eio_request */ + + case EIO_NOP: + req->result = 0; + break; + + case EIO_CUSTOM: + req->feed (req); + break; + + default: + errno = ENOSYS; + req->result = -1; + break; + } + + req->errorno = errno; +} + +#ifndef EIO_NO_WRAPPERS + +eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data) +{ + REQ (EIO_WD_OPEN); PATH; SEND; +} + +eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_WD_CLOSE); req->wd = wd; SEND; +} + +eio_req *eio_nop (int pri, eio_cb cb, void *data) +{ + REQ (EIO_NOP); SEND; +} + +eio_req *eio_busy (double delay, int pri, eio_cb cb, void *data) +{ + REQ (EIO_BUSY); req->nv1 = delay; SEND; +} + +eio_req *eio_sync (int pri, eio_cb cb, void *data) +{ + REQ (EIO_SYNC); SEND; +} + +eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FSYNC); req->int1 = fd; SEND; +} + +eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_MSYNC); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; +} + +eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FDATASYNC); req->int1 = fd; SEND; +} + +eio_req *eio_syncfs (int fd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_SYNCFS); req->int1 = fd; SEND; +} + +eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_SYNC_FILE_RANGE); req->int1 = fd; req->offs = offset; req->size = nbytes; req->int2 = flags; SEND; +} + +eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_MTOUCH); req->ptr2 = addr; req->size = length; req->int1 = flags; SEND; +} + +eio_req *eio_mlock (void *addr, size_t length, int pri, eio_cb cb, void *data) +{ + REQ (EIO_MLOCK); req->ptr2 = addr; req->size = length; SEND; +} + +eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_MLOCKALL); req->int1 = flags; SEND; +} + +eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FALLOCATE); req->int1 = fd; req->int2 = mode; req->offs = offset; req->size = len; SEND; +} + +eio_req *eio_close (int fd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_CLOSE); req->int1 = fd; SEND; +} + +eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data) +{ + REQ (EIO_READAHEAD); req->int1 = fd; req->offs = offset; req->size = length; SEND; +} + +eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) +{ + REQ (EIO_READ); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; +} + +eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data) +{ + REQ (EIO_WRITE); req->int1 = fd; req->offs = offset; req->size = length; req->ptr2 = buf; SEND; +} + +eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FSTAT); req->int1 = fd; SEND; +} + +eio_req *eio_fstatvfs (int fd, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FSTATVFS); req->int1 = fd; SEND; +} + +eio_req *eio_futime (int fd, double atime, double mtime, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FUTIME); req->int1 = fd; req->nv1 = atime; req->nv2 = mtime; SEND; +} + +eio_req *eio_ftruncate (int fd, off_t offset, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FTRUNCATE); req->int1 = fd; req->offs = offset; SEND; +} + +eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FCHMOD); req->int1 = fd; req->int2 = (long)mode; SEND; +} + +eio_req *eio_fchown (int fd, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data) +{ + REQ (EIO_FCHOWN); req->int1 = fd; req->int2 = (long)uid; req->int3 = (long)gid; SEND; +} + +eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data) +{ + REQ (EIO_DUP2); req->int1 = fd; req->int2 = fd2; SEND; +} + +eio_req *eio_sendfile (int out_fd, int in_fd, off_t in_offset, size_t length, int pri, eio_cb cb, void *data) +{ + REQ (EIO_SENDFILE); req->int1 = out_fd; req->int2 = in_fd; req->offs = in_offset; req->size = length; SEND; +} + +eio_req *eio_open (const char *path, int flags, mode_t mode, int pri, eio_cb cb, void *data) +{ + REQ (EIO_OPEN); PATH; req->int1 = flags; req->int2 = (long)mode; SEND; +} + +eio_req *eio_utime (const char *path, double atime, double mtime, int pri, eio_cb cb, void *data) +{ + REQ (EIO_UTIME); PATH; req->nv1 = atime; req->nv2 = mtime; SEND; +} + +eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data) +{ + REQ (EIO_TRUNCATE); PATH; req->offs = offset; SEND; +} + +eio_req *eio_chown (const char *path, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data) +{ + REQ (EIO_CHOWN); PATH; req->int2 = (long)uid; req->int3 = (long)gid; SEND; +} + +eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data) +{ + REQ (EIO_CHMOD); PATH; req->int2 = (long)mode; SEND; +} + +eio_req *eio_mkdir (const char *path, mode_t mode, int pri, eio_cb cb, void *data) +{ + REQ (EIO_MKDIR); PATH; req->int2 = (long)mode; SEND; +} + +static eio_req * +eio__1path (int type, const char *path, int pri, eio_cb cb, void *data) +{ + REQ (type); PATH; SEND; +} + +eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data) +{ + return eio__1path (EIO_READLINK, path, pri, cb, data); +} + +eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data) +{ + return eio__1path (EIO_REALPATH, path, pri, cb, data); +} + +eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data) +{ + return eio__1path (EIO_STAT, path, pri, cb, data); +} + +eio_req *eio_lstat (const char *path, int pri, eio_cb cb, void *data) +{ + return eio__1path (EIO_LSTAT, path, pri, cb, data); +} + +eio_req *eio_statvfs (const char *path, int pri, eio_cb cb, void *data) +{ + return eio__1path (EIO_STATVFS, path, pri, cb, data); +} + +eio_req *eio_unlink (const char *path, int pri, eio_cb cb, void *data) +{ + return eio__1path (EIO_UNLINK, path, pri, cb, data); +} + +eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data) +{ + return eio__1path (EIO_RMDIR, path, pri, cb, data); +} + +eio_req *eio_readdir (const char *path, int flags, int pri, eio_cb cb, void *data) +{ + REQ (EIO_READDIR); PATH; req->int1 = flags; SEND; +} + +eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data) +{ + REQ (EIO_MKNOD); PATH; req->int2 = (long)mode; req->offs = (off_t)dev; SEND; +} + +static eio_req * +eio__2path (int type, const char *path, const char *new_path, int pri, eio_cb cb, void *data) +{ + REQ (type); PATH; + + req->flags |= EIO_FLAG_PTR2_FREE; + req->ptr2 = strdup (new_path); + if (!req->ptr2) + { + eio_api_destroy (req); + return 0; + } + + SEND; +} + +eio_req *eio_link (const char *path, const char *new_path, int pri, eio_cb cb, void *data) +{ + return eio__2path (EIO_LINK, path, new_path, pri, cb, data); +} + +eio_req *eio_symlink (const char *path, const char *new_path, int pri, eio_cb cb, void *data) +{ + return eio__2path (EIO_SYMLINK, path, new_path, pri, cb, data); +} + +eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data) +{ + return eio__2path (EIO_RENAME, path, new_path, pri, cb, data); +} + +eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data) +{ + REQ (EIO_CUSTOM); req->feed = execute; SEND; +} + +#endif + +eio_req *eio_grp (eio_cb cb, void *data) +{ + const int pri = EIO_PRI_MAX; + + REQ (EIO_GROUP); SEND; +} + +#undef REQ +#undef PATH +#undef SEND + +/*****************************************************************************/ +/* grp functions */ + +void +eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit) +{ + grp->int2 = limit; + grp->feed = feed; + + grp_try_feed (grp); +} + +void +eio_grp_limit (eio_req *grp, int limit) +{ + grp->int2 = limit; + + grp_try_feed (grp); +} + +void +eio_grp_add (eio_req *grp, eio_req *req) +{ + assert (("cannot add requests to IO::AIO::GRP after the group finished", grp->int1 != 2)); + + grp->flags |= EIO_FLAG_GROUPADD; + + ++grp->size; + req->grp = grp; + + req->grp_prev = 0; + req->grp_next = grp->grp_first; + + if (grp->grp_first) + grp->grp_first->grp_prev = req; + + grp->grp_first = req; +} + +/*****************************************************************************/ +/* misc garbage */ + +eio_ssize_t +eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count) +{ + return eio__sendfile (ofd, ifd, offset, count); +} + diff --git a/fdbrpc/libeio/eio.h b/fdbrpc/libeio/eio.h new file mode 100644 index 0000000000..0ffde6a60f --- /dev/null +++ b/fdbrpc/libeio/eio.h @@ -0,0 +1,400 @@ +/* + * libeio API header + * + * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modifica- + * tion, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- + * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO + * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- + * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- + * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * + * Alternatively, the contents of this file may be used under the terms of + * the GNU General Public License ("GPL") version 2 or any later version, + * in which case the provisions of the GPL are applicable instead of + * the above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the BSD license, indicate your decision + * by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file under + * either the BSD or the GPL. + */ + +#ifndef EIO_H_ +#define EIO_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include "stdio.h" + +typedef struct eio_req eio_req; +typedef struct eio_dirent eio_dirent; + +typedef int (*eio_cb)(eio_req *req); + +#ifndef EIO_REQ_MEMBERS +# define EIO_REQ_MEMBERS +#endif + +#ifndef EIO_STRUCT_STAT +# ifdef _WIN32 +# define EIO_STRUCT_STAT struct _stati64 +# define EIO_STRUCT_STATI64 +# else +# define EIO_STRUCT_STAT struct stat +# endif +#endif + +#ifdef _WIN32 + typedef int eio_uid_t; + typedef int eio_gid_t; + #ifdef __MINGW32__ /* no intptr_t */ + typedef ssize_t eio_ssize_t; + #else + typedef intptr_t eio_ssize_t; /* or SSIZE_T */ + #endif + #if __GNUC__ + typedef long long eio_ino_t; /* signed for compatibility to msvc */ + #else + typedef __int64 eio_ino_t; /* unsigned not supported by msvc */ + #endif +#else + typedef uid_t eio_uid_t; + typedef gid_t eio_gid_t; + typedef ssize_t eio_ssize_t; + typedef ino_t eio_ino_t; +#endif + +#ifndef EIO_STRUCT_STATVFS +# define EIO_STRUCT_STATVFS struct statvfs +#endif + +/* managing working directories */ + +typedef struct eio_pwd *eio_wd; + +#define EIO_CWD 0 /* the current working directory of the process, guaranteed to be a null pointer */ +#define EIO_INVALID_WD ((eio_wd)(int)-1) /* failure return for eio_wd_open */ + +eio_wd eio_wd_open_sync (eio_wd wd, const char *path); +void eio_wd_close_sync (eio_wd wd); + +/* for readdir */ + +/* eio_readdir flags */ +enum +{ + EIO_READDIR_DENTS = 0x01, /* ptr2 contains eio_dirents, not just the (unsorted) names */ + EIO_READDIR_DIRS_FIRST = 0x02, /* dirents gets sorted into a good stat() ing order to find directories first */ + EIO_READDIR_STAT_ORDER = 0x04, /* dirents gets sorted into a good stat() ing order to quickly stat all files */ + EIO_READDIR_FOUND_UNKNOWN = 0x80, /* set by eio_readdir when *_ARRAY was set and any TYPE=UNKNOWN's were found */ + + EIO_READDIR_CUSTOM1 = 0x100, /* for use by apps */ + EIO_READDIR_CUSTOM2 = 0x200 /* for use by apps */ +}; + +/* using "typical" values in the hope that the compiler will do something sensible */ +enum eio_dtype +{ + EIO_DT_UNKNOWN = 0, + EIO_DT_FIFO = 1, + EIO_DT_CHR = 2, + EIO_DT_MPC = 3, /* multiplexed char device (v7+coherent) */ + EIO_DT_DIR = 4, + EIO_DT_NAM = 5, /* xenix special named file */ + EIO_DT_BLK = 6, + EIO_DT_MPB = 7, /* multiplexed block device (v7+coherent) */ + EIO_DT_REG = 8, + EIO_DT_NWK = 9, /* HP-UX network special */ + EIO_DT_CMP = 9, /* VxFS compressed */ + EIO_DT_LNK = 10, + /* DT_SHAD = 11,*/ + EIO_DT_SOCK = 12, + EIO_DT_DOOR = 13, /* solaris door */ + EIO_DT_WHT = 14, + EIO_DT_MAX = 15 /* highest DT_VALUE ever, hopefully */ +}; + +struct eio_dirent +{ + int nameofs; /* offset of null-terminated name string in (char *)req->ptr2 */ + unsigned short namelen; /* size of filename without trailing 0 */ + unsigned char type; /* one of EIO_DT_* */ + signed char score; /* internal use */ + eio_ino_t inode; /* the inode number, if available, otherwise unspecified */ +}; + +/* eio_msync flags */ +enum +{ + EIO_MS_ASYNC = 1, + EIO_MS_INVALIDATE = 2, + EIO_MS_SYNC = 4 +}; + +/* eio_mtouch flags */ +enum +{ + EIO_MT_MODIFY = 1 +}; + +/* eio_sync_file_range flags */ +enum +{ + EIO_SYNC_FILE_RANGE_WAIT_BEFORE = 1, + EIO_SYNC_FILE_RANGE_WRITE = 2, + EIO_SYNC_FILE_RANGE_WAIT_AFTER = 4 +}; + +/* eio_fallocate flags */ +enum +{ + EIO_FALLOC_FL_KEEP_SIZE = 1 /* MUST match the value in linux/falloc.h */ +}; + +/* timestamps and differences - feel free to use double in your code directly */ +typedef double eio_tstamp; + +/* the eio request structure */ +enum +{ + EIO_CUSTOM, + EIO_WD_OPEN, EIO_WD_CLOSE, + + EIO_CLOSE, EIO_DUP2, + EIO_READ, EIO_WRITE, + EIO_READAHEAD, EIO_SENDFILE, + EIO_FSTAT, EIO_FSTATVFS, + EIO_FTRUNCATE, EIO_FUTIME, EIO_FCHMOD, EIO_FCHOWN, + EIO_SYNC, EIO_FSYNC, EIO_FDATASYNC, EIO_SYNCFS, + EIO_MSYNC, EIO_MTOUCH, EIO_SYNC_FILE_RANGE, EIO_FALLOCATE, + EIO_MLOCK, EIO_MLOCKALL, + EIO_GROUP, EIO_NOP, + EIO_BUSY, + + /* these use wd + ptr1, but are emulated */ + EIO_REALPATH, + EIO_STATVFS, + EIO_READDIR, + + /* all the following requests use wd + ptr1 as path in xxxat functions */ + EIO_OPEN, + EIO_STAT, EIO_LSTAT, + EIO_TRUNCATE, + EIO_UTIME, + EIO_CHMOD, + EIO_CHOWN, + EIO_UNLINK, EIO_RMDIR, EIO_MKDIR, EIO_RENAME, + EIO_MKNOD, + EIO_LINK, EIO_SYMLINK, EIO_READLINK, + + EIO_REQ_TYPE_NUM +}; + +/* mlockall constants */ +enum +{ + EIO_MCL_CURRENT = 1, + EIO_MCL_FUTURE = 2, +}; + +/* request priorities */ + +enum { + EIO_PRI_MIN = -4, + EIO_PRI_MAX = 4, + EIO_PRI_DEFAULT = 0, +}; + +/* eio request structure */ +/* this structure is mostly read-only */ +/* when initialising it, all members must be zero-initialised */ +struct eio_req +{ + eio_req volatile *next; /* private ETP */ + + eio_wd wd; /* all applicable requests: working directory of pathname, old name; wd_open: return wd */ + + eio_ssize_t result; /* result of syscall, e.g. result = read (... */ + off_t offs; /* read, write, truncate, readahead, sync_file_range, fallocate: file offset, mknod: dev_t */ + size_t size; /* read, write, readahead, sendfile, msync, mlock, sync_file_range, fallocate: length */ + void *ptr1; /* all applicable requests: pathname, old name; readdir: optional eio_dirents */ + void *ptr2; /* all applicable requests: new name or memory buffer; readdir: name strings */ + eio_tstamp nv1; /* utime, futime: atime; busy: sleep time */ + eio_tstamp nv2; /* utime, futime: mtime */ + + int type; /* EIO_xxx constant ETP */ + int int1; /* all applicable requests: file descriptor; sendfile: output fd; open, msync, mlockall, readdir: flags */ + long int2; /* chown, fchown: uid; sendfile: input fd; open, chmod, mkdir, mknod: file mode, sync_file_range, fallocate: flags */ + long int3; /* chown, fchown: gid; rename, link: working directory of new name */ + int errorno; /* errno value on syscall return */ + +#if __i386 || __amd64 + unsigned char cancelled; +#else + sig_atomic_t cancelled; +#endif + + unsigned char flags; /* private */ + signed char pri; /* the priority */ + + void *data; + eio_cb finish; + void (*destroy)(eio_req *req); /* called when request no longer needed */ + void (*feed)(eio_req *req); /* only used for group requests */ + + EIO_REQ_MEMBERS + + eio_req *grp, *grp_prev, *grp_next, *grp_first; /* private */ +}; + +/* _private_ request flags */ +enum { + EIO_FLAG_PTR1_FREE = 0x01, /* need to free(ptr1) */ + EIO_FLAG_PTR2_FREE = 0x02, /* need to free(ptr2) */ + EIO_FLAG_GROUPADD = 0x04 /* some request was added to the group */ +}; + +/* undocumented/unsupported/private helper */ +/*void eio_page_align (void **addr, size_t *length);*/ + +/* returns < 0 on error, errno set + * need_poll, if non-zero, will be called when results are available + * and eio_poll_cb needs to be invoked (it MUST NOT call eio_poll_cb itself). + * done_poll is called when the need to poll is gone. + */ +int eio_init (void (*want_poll)(void), void (*done_poll)(void)); + +/* must be called regularly to handle pending requests */ +/* returns 0 if all requests were handled, -1 if not, or the value of EIO_FINISH if != 0 */ +int eio_poll (void); + +/* stop polling if poll took longer than duration seconds */ +void eio_set_max_poll_time (eio_tstamp nseconds); +/* do not handle more then count requests in one call to eio_poll_cb */ +void eio_set_max_poll_reqs (unsigned int nreqs); + +/* set minimum required number + * maximum wanted number + * or maximum idle number of threads */ +void eio_set_min_parallel (unsigned int nthreads); +void eio_set_max_parallel (unsigned int nthreads); +void eio_set_max_idle (unsigned int nthreads); +void eio_set_idle_timeout (unsigned int seconds); + +unsigned int eio_nreqs (void); /* number of requests in-flight */ +unsigned int eio_nready (void); /* number of not-yet handled requests */ +unsigned int eio_npending (void); /* number of finished but unhandled requests */ +unsigned int eio_nthreads (void); /* number of worker threads in use currently */ + +/*****************************************************************************/ +/* convenience wrappers */ + +#ifndef EIO_NO_WRAPPERS +eio_req *eio_wd_open (const char *path, int pri, eio_cb cb, void *data); /* result=wd */ +eio_req *eio_wd_close (eio_wd wd, int pri, eio_cb cb, void *data); +eio_req *eio_nop (int pri, eio_cb cb, void *data); /* does nothing except go through the whole process */ +eio_req *eio_busy (eio_tstamp delay, int pri, eio_cb cb, void *data); /* ties a thread for this long, simulating busyness */ +eio_req *eio_sync (int pri, eio_cb cb, void *data); +eio_req *eio_fsync (int fd, int pri, eio_cb cb, void *data); +eio_req *eio_fdatasync (int fd, int pri, eio_cb cb, void *data); +eio_req *eio_syncfs (int fd, int pri, eio_cb cb, void *data); +eio_req *eio_msync (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data); +eio_req *eio_mtouch (void *addr, size_t length, int flags, int pri, eio_cb cb, void *data); +eio_req *eio_mlock (void *addr, size_t length, int pri, eio_cb cb, void *data); +eio_req *eio_mlockall (int flags, int pri, eio_cb cb, void *data); +eio_req *eio_sync_file_range (int fd, off_t offset, size_t nbytes, unsigned int flags, int pri, eio_cb cb, void *data); +eio_req *eio_fallocate (int fd, int mode, off_t offset, size_t len, int pri, eio_cb cb, void *data); +eio_req *eio_close (int fd, int pri, eio_cb cb, void *data); +eio_req *eio_readahead (int fd, off_t offset, size_t length, int pri, eio_cb cb, void *data); +eio_req *eio_read (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data); +eio_req *eio_write (int fd, void *buf, size_t length, off_t offset, int pri, eio_cb cb, void *data); +eio_req *eio_fstat (int fd, int pri, eio_cb cb, void *data); /* stat buffer=ptr2 allocated dynamically */ +eio_req *eio_fstatvfs (int fd, int pri, eio_cb cb, void *data); /* stat buffer=ptr2 allocated dynamically */ +eio_req *eio_futime (int fd, eio_tstamp atime, eio_tstamp mtime, int pri, eio_cb cb, void *data); +eio_req *eio_ftruncate (int fd, off_t offset, int pri, eio_cb cb, void *data); +eio_req *eio_fchmod (int fd, mode_t mode, int pri, eio_cb cb, void *data); +eio_req *eio_fchown (int fd, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data); +eio_req *eio_dup2 (int fd, int fd2, int pri, eio_cb cb, void *data); +eio_req *eio_sendfile (int out_fd, int in_fd, off_t in_offset, size_t length, int pri, eio_cb cb, void *data); +eio_req *eio_open (const char *path, int flags, mode_t mode, int pri, eio_cb cb, void *data); +eio_req *eio_utime (const char *path, eio_tstamp atime, eio_tstamp mtime, int pri, eio_cb cb, void *data); +eio_req *eio_truncate (const char *path, off_t offset, int pri, eio_cb cb, void *data); +eio_req *eio_chown (const char *path, eio_uid_t uid, eio_gid_t gid, int pri, eio_cb cb, void *data); +eio_req *eio_chmod (const char *path, mode_t mode, int pri, eio_cb cb, void *data); +eio_req *eio_mkdir (const char *path, mode_t mode, int pri, eio_cb cb, void *data); +eio_req *eio_readdir (const char *path, int flags, int pri, eio_cb cb, void *data); /* result=ptr2 allocated dynamically */ +eio_req *eio_rmdir (const char *path, int pri, eio_cb cb, void *data); +eio_req *eio_unlink (const char *path, int pri, eio_cb cb, void *data); +eio_req *eio_readlink (const char *path, int pri, eio_cb cb, void *data); /* result=ptr2 allocated dynamically */ +eio_req *eio_realpath (const char *path, int pri, eio_cb cb, void *data); /* result=ptr2 allocated dynamically */ +eio_req *eio_stat (const char *path, int pri, eio_cb cb, void *data); /* stat buffer=ptr2 allocated dynamically */ +eio_req *eio_lstat (const char *path, int pri, eio_cb cb, void *data); /* stat buffer=ptr2 allocated dynamically */ +eio_req *eio_statvfs (const char *path, int pri, eio_cb cb, void *data); /* stat buffer=ptr2 allocated dynamically */ +eio_req *eio_mknod (const char *path, mode_t mode, dev_t dev, int pri, eio_cb cb, void *data); +eio_req *eio_link (const char *path, const char *new_path, int pri, eio_cb cb, void *data); +eio_req *eio_symlink (const char *path, const char *new_path, int pri, eio_cb cb, void *data); +eio_req *eio_rename (const char *path, const char *new_path, int pri, eio_cb cb, void *data); +eio_req *eio_custom (void (*execute)(eio_req *), int pri, eio_cb cb, void *data); +#endif + +/*****************************************************************************/ +/* groups */ + +eio_req *eio_grp (eio_cb cb, void *data); +void eio_grp_feed (eio_req *grp, void (*feed)(eio_req *req), int limit); +void eio_grp_limit (eio_req *grp, int limit); +void eio_grp_add (eio_req *grp, eio_req *req); +void eio_grp_cancel (eio_req *grp); /* cancels all sub requests but not the group */ + +/*****************************************************************************/ +/* request api */ + +/* true if the request was cancelled, useful in the invoke callback */ +#define EIO_CANCELLED(req) ((req)->cancelled) + +#define EIO_RESULT(req) ((req)->result) +/* returns a pointer to the result buffer allocated by eio */ +#define EIO_BUF(req) ((req)->ptr2) +#define EIO_STAT_BUF(req) ((EIO_STRUCT_STAT *)EIO_BUF(req)) +#define EIO_STATVFS_BUF(req) ((EIO_STRUCT_STATVFS *)EIO_BUF(req)) +#define EIO_PATH(req) ((char *)(req)->ptr1) + +/* submit a request for execution */ +void eio_submit (eio_req *req); +/* cancel a request as soon fast as possible, if possible */ +void eio_cancel (eio_req *req); + +/*****************************************************************************/ +/* convenience functions */ + +eio_ssize_t eio_sendfile_sync (int ofd, int ifd, off_t offset, size_t count); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/fdbrpc/libeio/xthread.h b/fdbrpc/libeio/xthread.h new file mode 100644 index 0000000000..14cfd22fb3 --- /dev/null +++ b/fdbrpc/libeio/xthread.h @@ -0,0 +1,166 @@ +#ifndef XTHREAD_H_ +#define XTHREAD_H_ + +/* whether word reads are potentially non-atomic. + * this is conservative, likely most arches this runs + * on have atomic word read/writes. + */ +#ifndef WORDACCESS_UNSAFE +# if __i386 || __x86_64 +# define WORDACCESS_UNSAFE 0 +# else +# define WORDACCESS_UNSAFE 1 +# endif +#endif + +///////////////////////////////////////////////////////////////////////////// + +#ifdef _WIN32 + +#define NTDDI_VERSION NTDDI_WIN2K // needed to get win2000 api calls +#define _WIN32_WINNT 0x400 +#include //D +#include +#include +#include +#include +#include +#include +#include +#define sigset_t int +#define sigfillset(a) +#define pthread_sigmask(a,b,c) +#define sigaddset(a,b) +#define sigemptyset(s) + +typedef pthread_mutex_t xmutex_t; +#define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER +#define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0) +#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) +#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) + +typedef pthread_cond_t xcond_t; +#define X_COND_INIT PTHREAD_COND_INITIALIZER +#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0) +#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) +#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) +#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) + +typedef pthread_t xthread_t; +#define X_THREAD_PROC(name) void *name (void *thr_arg) +#define X_THREAD_ATFORK(a,b,c) + +static int +thread_create (xthread_t *tid, void *(*proc)(void *), void *arg) +{ + int retval; + pthread_attr_t attr; + + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + + retval = pthread_create (tid, &attr, proc, arg) == 0; + + pthread_attr_destroy (&attr); + + return retval; +} + +#define respipe_read(a,b,c) PerlSock_recv ((a), (b), (c), 0) +#define respipe_write(a,b,c) send ((a), (b), (c), 0) +#define respipe_close(a) PerlSock_closesocket ((a)) + +#else +///////////////////////////////////////////////////////////////////////////// + +#if __linux && !defined(_GNU_SOURCE) +# define _GNU_SOURCE +#endif + +/* just in case */ +#define _REENTRANT 1 + +#if __solaris +# define _POSIX_PTHREAD_SEMANTICS 1 +/* try to bribe solaris headers into providing a current pthread API + * despite environment being configured for an older version. + */ +# define __EXTENSIONS__ 1 +#endif + +#include +#include +#include +#include +#include + +typedef pthread_mutex_t xmutex_t; +#if __linux && defined (PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP) +# define X_MUTEX_INIT PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP +# define X_MUTEX_CREATE(mutex) \ + do { \ + pthread_mutexattr_t attr; \ + pthread_mutexattr_init (&attr); \ + pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ADAPTIVE_NP); \ + pthread_mutex_init (&(mutex), &attr); \ + } while (0) +#else +# define X_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER +# define X_MUTEX_CREATE(mutex) pthread_mutex_init (&(mutex), 0) +#endif +#define X_LOCK(mutex) pthread_mutex_lock (&(mutex)) +#define X_UNLOCK(mutex) pthread_mutex_unlock (&(mutex)) + +typedef pthread_cond_t xcond_t; +#define X_COND_INIT PTHREAD_COND_INITIALIZER +#define X_COND_CREATE(cond) pthread_cond_init (&(cond), 0) +#define X_COND_SIGNAL(cond) pthread_cond_signal (&(cond)) +#define X_COND_WAIT(cond,mutex) pthread_cond_wait (&(cond), &(mutex)) +#define X_COND_TIMEDWAIT(cond,mutex,to) pthread_cond_timedwait (&(cond), &(mutex), &(to)) + +typedef pthread_t xthread_t; +#define X_THREAD_PROC(name) static void *name (void *thr_arg) +#define X_THREAD_ATFORK(prepare,parent,child) pthread_atfork (prepare, parent, child) + +// the broken bsd's once more +#ifndef PTHREAD_STACK_MIN +# define PTHREAD_STACK_MIN 0 +#endif + +#ifndef X_STACKSIZE +# define X_STACKSIZE sizeof (void *) * 4096 +#endif + +static int +thread_create (xthread_t *tid, void *(*proc)(void *), void *arg) +{ + int retval; + sigset_t fullsigset, oldsigset; + pthread_attr_t attr; + + pthread_attr_init (&attr); + pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); + pthread_attr_setstacksize (&attr, PTHREAD_STACK_MIN < X_STACKSIZE ? X_STACKSIZE : PTHREAD_STACK_MIN); +#ifdef PTHREAD_SCOPE_PROCESS + pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); +#endif + + sigfillset (&fullsigset); + + pthread_sigmask (SIG_SETMASK, &fullsigset, &oldsigset); + retval = pthread_create (tid, &attr, proc, arg) == 0; + pthread_sigmask (SIG_SETMASK, &oldsigset, 0); + + pthread_attr_destroy (&attr); + + return retval; +} + +#define respipe_read(a,b,c) read ((a), (b), (c)) +#define respipe_write(a,b,c) write ((a), (b), (c)) +#define respipe_close(a) close ((a)) + +#endif + +#endif + diff --git a/fdbrpc/linux_kaio.h b/fdbrpc/linux_kaio.h new file mode 100644 index 0000000000..1f65c5f832 --- /dev/null +++ b/fdbrpc/linux_kaio.h @@ -0,0 +1,57 @@ +/* + * linux_kaio.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// AIO system calls + +typedef struct io_context *io_context_t; + +enum { + IO_CMD_PREAD = 0, + IO_CMD_PWRITE = 1, + IO_CMD_FSYNC = 2, + IO_CMD_FDSYNC = 3 +}; + +struct linux_iocb { + void* data; + uint32_t key, unused; + + uint16_t aio_lio_opcode; + uint16_t aio_reqprio; + uint32_t aio_fildes; + + void* buf; + uint64_t nbytes; + int64_t offset; + uint64_t unused2; + uint32_t flags; + uint32_t eventfd; +}; + +struct linux_ioresult { + void* data; + linux_iocb* iocb; + unsigned long result; + unsigned long result2; +}; + +static int io_setup(unsigned nr_events, io_context_t *ctxp) { return syscall( __NR_io_setup, nr_events, ctxp ); } +static int io_submit(io_context_t ctx_id, long nrstruct, linux_iocb ** iocbpp ) { return syscall( __NR_io_submit, ctx_id, nrstruct, iocbpp ); } +static int io_getevents(io_context_t ctx_id, long min_nr, long nr, linux_ioresult *events, struct timespec * timeout ) { return syscall( __NR_io_getevents, ctx_id, min_nr, nr, events, timeout ); } diff --git a/fdbrpc/local.mk b/fdbrpc/local.mk new file mode 100644 index 0000000000..2d54297068 --- /dev/null +++ b/fdbrpc/local.mk @@ -0,0 +1,34 @@ +# +# local.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile; -*- + +fdbrpc_BUILD_SOURCES += fdbrpc/libeio/eio.c + +fdbrpc_CFLAGS := -I$(BOOSTDIR) -I. -Ifdbrpc -Ifdbrpc/libeio -DUSE_UCONTEXT +fdbrpc_LDFLAGS := + +ifeq ($(PLATFORM),osx) + fdbrpc_CFLAGS += -fasynchronous-unwind-tables -fno-omit-frame-pointer + fdbrpc_BUILD_SOURCES += fdbrpc/libcoroutine/asm.S fdbrpc/libcoroutine/context.c + fdbrpc_LDFLAGS += -framework CoreFoundation -framework IOKit +endif + +lib/libfdbrpc.a: bin/coverage.fdbrpc.xml diff --git a/fdbrpc/md5/md5.c b/fdbrpc/md5/md5.c new file mode 100644 index 0000000000..52d96accd3 --- /dev/null +++ b/fdbrpc/md5/md5.c @@ -0,0 +1,296 @@ +/* + * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. + * MD5 Message-Digest Algorithm (RFC 1321). + * + * Homepage: + * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 + * + * Author: + * Alexander Peslyak, better known as Solar Designer + * + * This software was written by Alexander Peslyak in 2001. No copyright is + * claimed, and the software is hereby placed in the public domain. + * In case this attempt to disclaim copyright and place the software in the + * public domain is deemed null and void, then the software is + * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the + * general public under the following terms: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * (This is a heavily cut-down "BSD license".) + * + * This differs from Colin Plumb's older public domain implementation in that + * no exactly 32-bit integer data type is required (any 32-bit or wider + * unsigned integer data type will do), there's no compile-time endianness + * configuration, and the function prototypes match OpenSSL's. No code from + * Colin Plumb's implementation has been reused; this comment merely compares + * the properties of the two independent implementations. + * + * The primary goals of this implementation are portability and ease of use. + * It is meant to be fast, but not as fast as possible. Some known + * optimizations are not included to reduce source code size and avoid + * compile-time configuration. + */ + +#ifndef HAVE_OPENSSL + +#include + +#include "md5.h" + +/* + * The basic MD5 functions. + * + * F and G are optimized compared to their RFC 1321 definitions for + * architectures that lack an AND-NOT instruction, just like in Colin Plumb's + * implementation. + */ +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) +#define H(x, y, z) (((x) ^ (y)) ^ (z)) +#define H2(x, y, z) ((x) ^ ((y) ^ (z))) +#define I(x, y, z) ((y) ^ ((x) | ~(z))) + +/* + * The MD5 transformation for all four rounds. + */ +#define STEP(f, a, b, c, d, x, t, s) \ + (a) += f((b), (c), (d)) + (x) + (t); \ + (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ + (a) += (b); + +/* + * SET reads 4 input bytes in little-endian byte order and stores them + * in a properly aligned word in host byte order. + * + * The check for little-endian architectures that tolerate unaligned + * memory accesses is just an optimization. Nothing will break if it + * doesn't work. + */ +#if defined(__i386__) || defined(__x86_64__) || defined(__vax__) +#define SET(n) \ + (*(MD5_u32plus *)&ptr[(n) * 4]) +#define GET(n) \ + SET(n) +#else +#define SET(n) \ + (ctx->block[(n)] = \ + (MD5_u32plus)ptr[(n) * 4] | \ + ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \ + ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \ + ((MD5_u32plus)ptr[(n) * 4 + 3] << 24)) +#define GET(n) \ + (ctx->block[(n)]) +#endif + +/* + * This processes one or more 64-byte data blocks, but does NOT update + * the bit counters. There are no alignment requirements. + */ +static const void *body(MD5_CTX *ctx, const void *data, unsigned long size) +{ + const unsigned char *ptr; + MD5_u32plus a, b, c, d; + MD5_u32plus saved_a, saved_b, saved_c, saved_d; + + ptr = (const unsigned char *)data; + + a = ctx->a; + b = ctx->b; + c = ctx->c; + d = ctx->d; + + do { + saved_a = a; + saved_b = b; + saved_c = c; + saved_d = d; + +/* Round 1 */ + STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) + STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) + STEP(F, c, d, a, b, SET(2), 0x242070db, 17) + STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22) + STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7) + STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12) + STEP(F, c, d, a, b, SET(6), 0xa8304613, 17) + STEP(F, b, c, d, a, SET(7), 0xfd469501, 22) + STEP(F, a, b, c, d, SET(8), 0x698098d8, 7) + STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12) + STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17) + STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22) + STEP(F, a, b, c, d, SET(12), 0x6b901122, 7) + STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) + STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) + STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) + +/* Round 2 */ + STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) + STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) + STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) + STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20) + STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5) + STEP(G, d, a, b, c, GET(10), 0x02441453, 9) + STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14) + STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20) + STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5) + STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9) + STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14) + STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20) + STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5) + STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) + STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) + STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) + +/* Round 3 */ + STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) + STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11) + STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) + STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23) + STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4) + STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11) + STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16) + STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23) + STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4) + STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11) + STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16) + STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23) + STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4) + STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11) + STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) + STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23) + +/* Round 4 */ + STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) + STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) + STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) + STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21) + STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6) + STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10) + STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15) + STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21) + STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6) + STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10) + STEP(I, c, d, a, b, GET(6), 0xa3014314, 15) + STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21) + STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6) + STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) + STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) + STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) + + a += saved_a; + b += saved_b; + c += saved_c; + d += saved_d; + + ptr += 64; + } while (size -= 64); + + ctx->a = a; + ctx->b = b; + ctx->c = c; + ctx->d = d; + + return ptr; +} + +void MD5_Init(MD5_CTX *ctx) +{ + ctx->a = 0x67452301; + ctx->b = 0xefcdab89; + ctx->c = 0x98badcfe; + ctx->d = 0x10325476; + + ctx->lo = 0; + ctx->hi = 0; +} + +void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size) +{ + MD5_u32plus saved_lo; + unsigned long used, available; + + saved_lo = ctx->lo; + if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) + ctx->hi++; + ctx->hi += size >> 29; + + used = saved_lo & 0x3f; + + if (used) { + available = 64 - used; + + if (size < available) { + memcpy(&ctx->buffer[used], data, size); + return; + } + + memcpy(&ctx->buffer[used], data, available); + data = (const unsigned char *)data + available; + size -= available; + body(ctx, ctx->buffer, 64); + } + + if (size >= 64) { + data = body(ctx, data, size & ~(unsigned long)0x3f); + size &= 0x3f; + } + + memcpy(ctx->buffer, data, size); +} + +void MD5_Final(unsigned char *result, MD5_CTX *ctx) +{ + unsigned long used, available; + + used = ctx->lo & 0x3f; + + ctx->buffer[used++] = 0x80; + + available = 64 - used; + + if (available < 8) { + memset(&ctx->buffer[used], 0, available); + body(ctx, ctx->buffer, 64); + used = 0; + available = 64; + } + + memset(&ctx->buffer[used], 0, available - 8); + + ctx->lo <<= 3; + ctx->buffer[56] = ctx->lo; + ctx->buffer[57] = ctx->lo >> 8; + ctx->buffer[58] = ctx->lo >> 16; + ctx->buffer[59] = ctx->lo >> 24; + ctx->buffer[60] = ctx->hi; + ctx->buffer[61] = ctx->hi >> 8; + ctx->buffer[62] = ctx->hi >> 16; + ctx->buffer[63] = ctx->hi >> 24; + + body(ctx, ctx->buffer, 64); + + result[0] = ctx->a; + result[1] = ctx->a >> 8; + result[2] = ctx->a >> 16; + result[3] = ctx->a >> 24; + result[4] = ctx->b; + result[5] = ctx->b >> 8; + result[6] = ctx->b >> 16; + result[7] = ctx->b >> 24; + result[8] = ctx->c; + result[9] = ctx->c >> 8; + result[10] = ctx->c >> 16; + result[11] = ctx->c >> 24; + result[12] = ctx->d; + result[13] = ctx->d >> 8; + result[14] = ctx->d >> 16; + result[15] = ctx->d >> 24; + + memset(ctx, 0, sizeof(*ctx)); +} + +#endif diff --git a/fdbrpc/md5/md5.h b/fdbrpc/md5/md5.h new file mode 100644 index 0000000000..a8e90b8b7a --- /dev/null +++ b/fdbrpc/md5/md5.h @@ -0,0 +1,51 @@ +/* + * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. + * MD5 Message-Digest Algorithm (RFC 1321). + * + * Homepage: + * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 + * + * Author: + * Alexander Peslyak, better known as Solar Designer + * + * This software was written by Alexander Peslyak in 2001. No copyright is + * claimed, and the software is hereby placed in the public domain. + * In case this attempt to disclaim copyright and place the software in the + * public domain is deemed null and void, then the software is + * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the + * general public under the following terms: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * See md5.c for more information. + */ + +#ifdef HAVE_OPENSSL +#include +#elif !defined(_MD5_H) +#define _MD5_H + +/* Any 32-bit or wider unsigned integer data type will do */ +typedef unsigned int MD5_u32plus; + +typedef struct { + MD5_u32plus lo, hi; + MD5_u32plus a, b, c, d; + unsigned char buffer[64]; + MD5_u32plus block[16]; +} MD5_CTX; + +#ifdef __cplusplus +extern "C" { +#endif + void MD5_Init(MD5_CTX *ctx); + void MD5_Update(MD5_CTX *ctx, const void *data, unsigned long size); + void MD5_Final(unsigned char *result, MD5_CTX *ctx); +#ifdef __cplusplus +} +#endif + +#endif diff --git a/fdbrpc/sha1/SHA1.cpp b/fdbrpc/sha1/SHA1.cpp new file mode 100644 index 0000000000..0668c397bb --- /dev/null +++ b/fdbrpc/sha1/SHA1.cpp @@ -0,0 +1,284 @@ +/* + * SHA1.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + ============ + SHA-1 in C++ + ============ + + 100% Public Domain. + + Original C Code + -- Steve Reid + Small changes to fit into bglibs + -- Bruce Guenter + Translation to simpler C++ Code + -- Volker Grabsch +*/ + +#include "SHA1.h" +#include +#include +#include + +/* Help macros */ +#define SHA1_ROL(value, bits) (((value) << (bits)) | (((value) & 0xffffffff) >> (32 - (bits)))) +#define SHA1_BLK(i) (block[i&15] = SHA1_ROL(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i&15],1)) + +/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ +#define SHA1_R0(v,w,x,y,z,i) z += ((w&(x^y))^y) + block[i] + 0x5a827999 + SHA1_ROL(v,5); w=SHA1_ROL(w,30); +#define SHA1_R1(v,w,x,y,z,i) z += ((w&(x^y))^y) + SHA1_BLK(i) + 0x5a827999 + SHA1_ROL(v,5); w=SHA1_ROL(w,30); +#define SHA1_R2(v,w,x,y,z,i) z += (w^x^y) + SHA1_BLK(i) + 0x6ed9eba1 + SHA1_ROL(v,5); w=SHA1_ROL(w,30); +#define SHA1_R3(v,w,x,y,z,i) z += (((w|x)&y)|(w&x)) + SHA1_BLK(i) + 0x8f1bbcdc + SHA1_ROL(v,5); w=SHA1_ROL(w,30); +#define SHA1_R4(v,w,x,y,z,i) z += (w^x^y) + SHA1_BLK(i) + 0xca62c1d6 + SHA1_ROL(v,5); w=SHA1_ROL(w,30); + +SHA1::SHA1() +{ + reset(); +} + + +void SHA1::update(const std::string &s) +{ + std::istringstream is(s); + update(is); +} + + +void SHA1::update(std::istream &is) +{ + std::string rest_of_buffer; + read(is, rest_of_buffer, BLOCK_BYTES - buffer.size()); + buffer += rest_of_buffer; + + while (is) + { + uint32 block[BLOCK_INTS]; + buffer_to_block(buffer, block); + transform(block); + read(is, buffer, BLOCK_BYTES); + } +} + + +/* + * Add padding and return the message digest. + */ + +std::string SHA1::final() +{ + /* Total number of hashed bits */ + uint64 total_bits = (transforms*BLOCK_BYTES + buffer.size()) * 8; + + /* Padding */ + buffer += 0x80; + std::string::size_type orig_size = (unsigned int)buffer.size(); + while (buffer.size() < BLOCK_BYTES) + { + buffer += (char)0x00; + } + + uint32 block[BLOCK_INTS]; + buffer_to_block(buffer, block); + + if (orig_size > BLOCK_BYTES - 8) + { + transform(block); + for (unsigned int i = 0; i < BLOCK_INTS - 2; i++) + { + block[i] = 0; + } + } + + /* Append total_bits, split this uint64 into two uint32 */ + block[BLOCK_INTS - 1] = (SHA1::uint32)total_bits; + block[BLOCK_INTS - 2] = (total_bits >> 32); + transform(block); + + // Modified from original to output a binary string + std::string result; + result.reserve(16); + + for (unsigned int i = 0; i < DIGEST_INTS; i++) + { + uint32 v = digest[i]; + result.append(1, (char)(v >> 24)); + result.append(1, (char)(v >> 16)); + result.append(1, (char)(v >> 8)); + result.append(1, (char)(v)); + } + + /* Reset for next run */ + reset(); + + return result; +} + +void SHA1::reset() +{ + /* SHA1 initialization constants */ + digest[0] = 0x67452301; + digest[1] = 0xefcdab89; + digest[2] = 0x98badcfe; + digest[3] = 0x10325476; + digest[4] = 0xc3d2e1f0; + + /* Reset counters */ + transforms = 0; + buffer = ""; +} + + +/* + * Hash a single 512-bit block. This is the core of the algorithm. + */ + +void SHA1::transform(uint32 block[BLOCK_BYTES]) +{ + /* Copy digest[] to working vars */ + uint32 a = digest[0]; + uint32 b = digest[1]; + uint32 c = digest[2]; + uint32 d = digest[3]; + uint32 e = digest[4]; + + + /* 4 rounds of 20 operations each. Loop unrolled. */ + SHA1_R0(a,b,c,d,e, 0); + SHA1_R0(e,a,b,c,d, 1); + SHA1_R0(d,e,a,b,c, 2); + SHA1_R0(c,d,e,a,b, 3); + SHA1_R0(b,c,d,e,a, 4); + SHA1_R0(a,b,c,d,e, 5); + SHA1_R0(e,a,b,c,d, 6); + SHA1_R0(d,e,a,b,c, 7); + SHA1_R0(c,d,e,a,b, 8); + SHA1_R0(b,c,d,e,a, 9); + SHA1_R0(a,b,c,d,e,10); + SHA1_R0(e,a,b,c,d,11); + SHA1_R0(d,e,a,b,c,12); + SHA1_R0(c,d,e,a,b,13); + SHA1_R0(b,c,d,e,a,14); + SHA1_R0(a,b,c,d,e,15); + SHA1_R1(e,a,b,c,d,16); + SHA1_R1(d,e,a,b,c,17); + SHA1_R1(c,d,e,a,b,18); + SHA1_R1(b,c,d,e,a,19); + SHA1_R2(a,b,c,d,e,20); + SHA1_R2(e,a,b,c,d,21); + SHA1_R2(d,e,a,b,c,22); + SHA1_R2(c,d,e,a,b,23); + SHA1_R2(b,c,d,e,a,24); + SHA1_R2(a,b,c,d,e,25); + SHA1_R2(e,a,b,c,d,26); + SHA1_R2(d,e,a,b,c,27); + SHA1_R2(c,d,e,a,b,28); + SHA1_R2(b,c,d,e,a,29); + SHA1_R2(a,b,c,d,e,30); + SHA1_R2(e,a,b,c,d,31); + SHA1_R2(d,e,a,b,c,32); + SHA1_R2(c,d,e,a,b,33); + SHA1_R2(b,c,d,e,a,34); + SHA1_R2(a,b,c,d,e,35); + SHA1_R2(e,a,b,c,d,36); + SHA1_R2(d,e,a,b,c,37); + SHA1_R2(c,d,e,a,b,38); + SHA1_R2(b,c,d,e,a,39); + SHA1_R3(a,b,c,d,e,40); + SHA1_R3(e,a,b,c,d,41); + SHA1_R3(d,e,a,b,c,42); + SHA1_R3(c,d,e,a,b,43); + SHA1_R3(b,c,d,e,a,44); + SHA1_R3(a,b,c,d,e,45); + SHA1_R3(e,a,b,c,d,46); + SHA1_R3(d,e,a,b,c,47); + SHA1_R3(c,d,e,a,b,48); + SHA1_R3(b,c,d,e,a,49); + SHA1_R3(a,b,c,d,e,50); + SHA1_R3(e,a,b,c,d,51); + SHA1_R3(d,e,a,b,c,52); + SHA1_R3(c,d,e,a,b,53); + SHA1_R3(b,c,d,e,a,54); + SHA1_R3(a,b,c,d,e,55); + SHA1_R3(e,a,b,c,d,56); + SHA1_R3(d,e,a,b,c,57); + SHA1_R3(c,d,e,a,b,58); + SHA1_R3(b,c,d,e,a,59); + SHA1_R4(a,b,c,d,e,60); + SHA1_R4(e,a,b,c,d,61); + SHA1_R4(d,e,a,b,c,62); + SHA1_R4(c,d,e,a,b,63); + SHA1_R4(b,c,d,e,a,64); + SHA1_R4(a,b,c,d,e,65); + SHA1_R4(e,a,b,c,d,66); + SHA1_R4(d,e,a,b,c,67); + SHA1_R4(c,d,e,a,b,68); + SHA1_R4(b,c,d,e,a,69); + SHA1_R4(a,b,c,d,e,70); + SHA1_R4(e,a,b,c,d,71); + SHA1_R4(d,e,a,b,c,72); + SHA1_R4(c,d,e,a,b,73); + SHA1_R4(b,c,d,e,a,74); + SHA1_R4(a,b,c,d,e,75); + SHA1_R4(e,a,b,c,d,76); + SHA1_R4(d,e,a,b,c,77); + SHA1_R4(c,d,e,a,b,78); + SHA1_R4(b,c,d,e,a,79); + + /* Add the working vars back into digest[] */ + digest[0] += a; + digest[1] += b; + digest[2] += c; + digest[3] += d; + digest[4] += e; + + /* Count the number of transformations */ + transforms++; +} + + +void SHA1::buffer_to_block(const std::string &buffer, uint32 block[BLOCK_BYTES]) +{ + /* Convert the std::string (byte buffer) to a uint32 array (MSB) */ + for (unsigned int i = 0; i < BLOCK_INTS; i++) + { + block[i] = (buffer[4*i+3] & 0xff) + | (buffer[4*i+2] & 0xff)<<8 + | (buffer[4*i+1] & 0xff)<<16 + | (buffer[4*i+0] & 0xff)<<24; + } +} + + +void SHA1::read(std::istream &is, std::string &s, int max) +{ + char* sbuf = new char[max]; + is.read(sbuf, max); + s.assign(sbuf, is.gcount()); + delete [] sbuf; +} + + +std::string SHA1::from_string(const std::string &string) +{ + SHA1 checksum; + checksum.update(string); + return checksum.final(); +} \ No newline at end of file diff --git a/fdbrpc/sha1/SHA1.h b/fdbrpc/sha1/SHA1.h new file mode 100644 index 0000000000..7af4bc6937 --- /dev/null +++ b/fdbrpc/sha1/SHA1.h @@ -0,0 +1,70 @@ +/* + * SHA1.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + ============ + SHA-1 in C++ + ============ + + 100% Public Domain. + + Original C Code + -- Steve Reid + Small changes to fit into bglibs + -- Bruce Guenter + Translation to simpler C++ Code + -- Volker Grabsch +*/ + +#pragma once + +#include +#include +#include + +class SHA1 +{ +public: + SHA1(); + void update(const std::string &s); + void update(std::istream &is); + std::string final(); + + static std::string from_string(const std::string &string); + +private: + typedef uint32_t uint32; /* just needs to be at least 32bit */ + typedef uint64_t uint64; /* just needs to be at least 64bit */ + + static const unsigned int DIGEST_INTS = 5; /* number of 32bit integers per SHA1 digest */ + static const unsigned int BLOCK_INTS = 16; /* number of 32bit integers per SHA1 block */ + static const unsigned int BLOCK_BYTES = BLOCK_INTS * 4; + + uint32 digest[DIGEST_INTS]; + std::string buffer; + uint64 transforms; + + void reset(); + void transform(uint32 block[BLOCK_BYTES]); + + static void buffer_to_block(const std::string &buffer, uint32 block[BLOCK_BYTES]); + static void read(std::istream &is, std::string &s, int max); +}; + diff --git a/fdbrpc/sim2.actor.cpp b/fdbrpc/sim2.actor.cpp new file mode 100644 index 0000000000..6d24b1a6fa --- /dev/null +++ b/fdbrpc/sim2.actor.cpp @@ -0,0 +1,1482 @@ +/* + * sim2.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "simulator.h" +#include "flow/IThreadPool.h" +#include "IAsyncFile.h" +#include "AsyncFileCached.actor.h" +#include "AsyncFileNonDurable.actor.h" +#include "flow/Hash3.h" +#include "TraceFileIO.h" +#include "flow/FaultInjection.h" +#include "flow/network.h" +#include "Net2FileSystem.h" +#include "fdbclient/FDBTypes.h" +#include "fdbrpc/Replication.h" +#include "fdbrpc/ReplicationUtils.h" + + +using std::min; +using std::max; +using std::pair; +using std::make_pair; + +bool simulator_should_inject_fault( const char* context, const char* file, int line, int error_code ) { + if (!g_network->isSimulated()) return false; + + auto p = g_simulator.getCurrentProcess(); + + if (p->fault_injection_p2 && g_random->random01() < p->fault_injection_p2 && !g_simulator.speedUpSimulation) { + uint32_t + h1 = line + (p->fault_injection_r>>32), + h2 = p->fault_injection_r; + + if (h1 < p->fault_injection_p1*std::numeric_limits::max()) { + TraceEvent(SevWarn, "FaultInjected").detail("Context", context).detail("File", file).detail("Line", line).detail("ErrorCode", error_code); + return true; + } + } + + return false; +} + + +namespace std { +template<> +class hash { +public: + size_t operator()(const Endpoint &s) const + { + return hashlittle(&s, sizeof(s), 0); + } +}; +} + +bool onlyBeforeSimulatorInit() { + return g_network->isSimulated() && g_simulator.getAllProcesses().empty(); +} + +const UID TOKEN_ENDPOINT_NOT_FOUND(-1, -1); +const uint64_t TOKEN_STREAM_FLAG = 1; + +ISimulator* g_pSimulator = 0; +thread_local ISimulator::ProcessInfo* ISimulator::currentProcess = 0; +int openCount = 0; + +struct SimClogging { + double getSendDelay( NetworkAddress from, NetworkAddress to ) { + return halfLatency(); + double tnow = now(); + double t = tnow + halfLatency(); + + if (!g_simulator.speedUpSimulation && clogSendUntil.count( to.ip )) + t = std::max( t, clogSendUntil[ to.ip ] ); + + return t - tnow; + } + + double getRecvDelay( NetworkAddress from, NetworkAddress to ) { + auto pair = make_pair( from.ip, to.ip ); + + double tnow = now(); + double t = tnow + halfLatency(); + if(!g_simulator.speedUpSimulation) + t += clogPairLatency[ pair ]; + + if (!g_simulator.speedUpSimulation && clogPairUntil.count( pair )) + t = std::max( t, clogPairUntil[ pair ] ); + + if (!g_simulator.speedUpSimulation && clogRecvUntil.count( to.ip )) + t = std::max( t, clogRecvUntil[ to.ip ] ); + + return t - tnow; + } + + void clogPairFor( uint32_t from, uint32_t to, double t ) { + auto& u = clogPairUntil[ make_pair( from, to ) ]; + u = std::max(u, now() + t); + } + void clogSendFor( uint32_t from, double t ) { + auto& u = clogSendUntil[from]; + u = std::max(u, now() + t); + } + void clogRecvFor( uint32_t from, double t ) { + auto& u = clogRecvUntil[from]; + u = std::max(u, now() + t); + } + double setPairLatencyIfNotSet( uint32_t from, uint32_t to, double t ) { + auto i = clogPairLatency.find( make_pair(from,to) ); + if (i == clogPairLatency.end()) + i = clogPairLatency.insert( make_pair( make_pair(from,to), t ) ).first; + return i->second; + } +private: + std::map< uint32_t, double > clogSendUntil, clogRecvUntil; + std::map< std::pair, double > clogPairUntil; + std::map< std::pair, double > clogPairLatency; + double halfLatency() { + double a = g_random->random01(); + const double pFast = 0.999; + if (a <= pFast) { + a = a / pFast; + return 0.5 * (FLOW_KNOBS->MIN_NETWORK_LATENCY * (1-a) + FLOW_KNOBS->FAST_NETWORK_LATENCY/pFast * a); // 0.5ms average + } else { + a = (a-pFast) / (1-pFast); // uniform 0-1 again + return 0.5 * (FLOW_KNOBS->MIN_NETWORK_LATENCY * (1-a) + FLOW_KNOBS->SLOW_NETWORK_LATENCY*a); // long tail up to X ms + } + } +}; + +SimClogging g_clogging; + +struct Sim2Conn : IConnection, ReferenceCounted { + Sim2Conn( ISimulator::ProcessInfo* process ) + : process(process), dbgid( g_random->randomUniqueID() ), opened(false), closedByCaller(false) + { + pipes = sender(this) && receiver(this); + } + + // connect() is called on a pair of connections immediately after creation; logically it is part of the constructor and no other method may be called previously! + void connect( Reference peer, NetworkAddress peerEndpoint ) { + this->peer = peer; + this->peerProcess = peer->process; + this->peerId = peer->dbgid; + this->peerEndpoint = peerEndpoint; + + // Every one-way connection gets a random permanent latency and a random send buffer for the duration of the connection + auto latency = g_clogging.setPairLatencyIfNotSet( peerProcess->address.ip, process->address.ip, FLOW_KNOBS->MAX_CLOGGING_LATENCY*g_random->random01() ); + sendBufSize = std::max( g_random->randomInt(0, 5000000), 25e6 * (latency + .002) ); + TraceEvent("Sim2Connection").detail("SendBufSize", sendBufSize).detail("Latency", latency); + } + + ~Sim2Conn() { + ASSERT_ABORT( !opened || closedByCaller ); + } + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + virtual void close() { closedByCaller = true; closeInternal(); } + + virtual Future onWritable() { return whenWritable(this); } + virtual Future onReadable() { return whenReadable(this); } + + bool isPeerGone() { + return !peer || peerProcess->failed; + } + + void peerClosed() { + leakedConnectionTracker = trackLeakedConnection(this); + } + + // Reads as many bytes as possible from the read buffer into [begin,end) and returns the number of bytes read (might be 0) + // (or may throw an error if the connection dies) + virtual int read( uint8_t* begin, uint8_t* end ) { + rollRandomClose(); + + int64_t avail = receivedBytes.get() - readBytes.get(); // SOMEDAY: random? + int toRead = std::min( end-begin, avail ); + ASSERT( toRead >= 0 && toRead <= recvBuf.size() && toRead <= end-begin ); + for(int i=0; i 0); + + int toSend = 0; + if (BUGGIFY) { + toSend = std::min(limit, buffer->bytes_written - buffer->bytes_sent); + } else { + for(auto p = buffer; p; p=p->next) { + toSend += p->bytes_written - p->bytes_sent; + if(toSend >= limit) { + if(toSend > limit) + toSend = limit; + break; + } + } + } + ASSERT(toSend); + if (BUGGIFY) toSend = std::min(toSend, g_random->randomInt(0, 1000)); + + if (!peer) return toSend; + toSend = std::min( toSend, peer->availableSendBufferForPeer() ); + ASSERT( toSend >= 0 ); + + int leftToSend = toSend; + for(auto p = buffer; p && leftToSend>0; p=p->next) { + int ts = std::min(leftToSend, p->bytes_written - p->bytes_sent); + peer->recvBuf.insert( peer->recvBuf.end(), p->data + p->bytes_sent, p->data + p->bytes_sent + ts ); + leftToSend -= ts; + } + ASSERT( leftToSend == 0 ); + peer->writtenBytes.set( peer->writtenBytes.get() + toSend ); + return toSend; + } + + // Returns the network address and port of the other end of the connection. In the case of an incoming connection, this may not + // be an address we can connect to! + virtual NetworkAddress getPeerAddress() { return peerEndpoint; } + virtual UID getDebugID() { return dbgid; } + + bool opened, closedByCaller; + +private: + ISimulator::ProcessInfo* process, *peerProcess; + UID dbgid, peerId; + NetworkAddress peerEndpoint; + std::deque< uint8_t > recvBuf; // Includes bytes written but not yet received! + AsyncVar readBytes, // bytes already pulled from recvBuf (location of the beginning of recvBuf) + receivedBytes, + sentBytes, + writtenBytes; // location of the end of recvBuf ( == recvBuf.size() + readBytes.get() ) + Reference peer; + int sendBufSize; + + Future leakedConnectionTracker; + + Future pipes; + + int availableSendBufferForPeer() const { return sendBufSize - (writtenBytes.get() - receivedBytes.get()); } // SOMEDAY: acknowledgedBytes instead of receivedBytes + + void closeInternal() { + if(peer) { + peer->peerClosed(); + } + leakedConnectionTracker.cancel(); + peer.clear(); + } + + ACTOR static Future sender( Sim2Conn* self ) { + loop { + Void _ = wait( self->writtenBytes.onChange() ); // takes place on peer! + ASSERT( g_simulator.getCurrentProcess() == self->peerProcess ); + Void _ = wait( delay( .002 * g_random->random01() ) ); + self->sentBytes.set( self->writtenBytes.get() ); // or possibly just some sometimes... + } + } + ACTOR static Future receiver( Sim2Conn* self ) { + loop { + if (self->sentBytes.get() != self->receivedBytes.get()) + Void _ = wait( g_simulator.onProcess( self->peerProcess ) ); + while ( self->sentBytes.get() == self->receivedBytes.get() ) + Void _ = wait( self->sentBytes.onChange() ); + ASSERT( g_simulator.getCurrentProcess() == self->peerProcess ); + state int64_t pos = g_random->random01() < .5 ? self->sentBytes.get() : g_random->randomInt64( self->receivedBytes.get(), self->sentBytes.get()+1 ); + Void _ = wait( delay( g_clogging.getSendDelay( self->process->address, self->peerProcess->address ) ) ); + Void _ = wait( g_simulator.onProcess( self->process ) ); + ASSERT( g_simulator.getCurrentProcess() == self->process ); + Void _ = wait( delay( g_clogging.getRecvDelay( self->process->address, self->peerProcess->address ) ) ); + ASSERT( g_simulator.getCurrentProcess() == self->process ); + self->receivedBytes.set( pos ); + Void _ = wait( Future(Void()) ); // Prior notification can delete self and cancel this actor + ASSERT( g_simulator.getCurrentProcess() == self->process ); + } + } + ACTOR static Future whenReadable( Sim2Conn* self ) { + try { + loop { + if (self->readBytes.get() != self->receivedBytes.get()) { + ASSERT( g_simulator.getCurrentProcess() == self->process ); + return Void(); + } + Void _ = wait( self->receivedBytes.onChange() ); + self->rollRandomClose(); + } + } catch (Error& e) { + ASSERT( g_simulator.getCurrentProcess() == self->process ); + throw; + } + } + ACTOR static Future whenWritable( Sim2Conn* self ) { + try { + loop { + if (!self->peer) return Void(); + if (self->peer->availableSendBufferForPeer() > 0) { + ASSERT( g_simulator.getCurrentProcess() == self->process ); + return Void(); + } + try { + Void _ = wait( self->peer->receivedBytes.onChange() ); + ASSERT( g_simulator.getCurrentProcess() == self->peerProcess ); + } catch (Error& e) { + if (e.code() != error_code_broken_promise) throw; + } + Void _ = wait( g_simulator.onProcess( self->process ) ); + } + } catch (Error& e) { + ASSERT( g_simulator.getCurrentProcess() == self->process ); + throw; + } + } + + void rollRandomClose() { + if (g_simulator.enableConnectionFailures && g_random->random01() < .00001) { + double a = g_random->random01(), b = g_random->random01(); + TEST(true); // Simulated connection failure + TraceEvent("ConnectionFailure", dbgid).detail("MyAddr", process->address).detail("PeerAddr", peerProcess->address).detail("SendClosed", a > .33).detail("RecvClosed", a < .66).detail("Explicit", b < .3); + if (a < .66 && peer) peer->closeInternal(); + if (a > .33) closeInternal(); + // At the moment, we occasionally notice the connection failed immediately. In principle, this could happen but only after a delay. + if (b < .3) + throw connection_failed(); + } + } + + ACTOR static Future trackLeakedConnection( Sim2Conn* self ) { + Void _ = wait( g_simulator.onProcess( self->process ) ); + // SOMEDAY: Make this value variable? Dependent on buggification status? + Void _ = wait( delay( 20.0 ) ); + TraceEvent(SevError, "LeakedConnection", self->dbgid).error(connection_leaked()).detail("MyAddr", self->process->address).detail("PeerAddr", self->peerEndpoint).detail("PeerId", self->peerId).detail("Opened", self->opened); + return Void(); + } +}; + +#include +#include + +int sf_open( const char* filename, int flags, int convFlags, int mode ); + +#if defined(_WIN32) +#include + +#elif defined(__unixish__) +#define _open ::open +#define _read ::read +#define _write ::write +#define _close ::close +#define _lseeki64 ::lseek +#define _commit ::fsync +#define _chsize ::ftruncate +#define O_BINARY 0 + +int sf_open( const char* filename, int flags, int convFlags, int mode ) { + return _open( filename, convFlags, mode ); +} + +#else +#error How do i open a file on a new platform? +#endif + +class SimpleFile : public IAsyncFile, public ReferenceCounted { +public: + static void init() {} + + static bool should_poll() { return false; } + + ACTOR static Future> open( std::string filename, int flags, int mode, + Reference diskParameters = Reference(new DiskParameters(25000, 150000000)), bool delayOnWrite = true ) { + state ISimulator::ProcessInfo* currentProcess = g_simulator.getCurrentProcess(); + state int currentTaskID = g_network->getCurrentTask(); + + if(++openCount >= 3000) { + TraceEvent(SevError, "TooManyFiles"); + ASSERT(false); + } + + if(openCount == 2000) { + TraceEvent(SevWarnAlways, "DisableConnectionFailures_TooManyFiles"); + g_simulator.speedUpSimulation = true; + g_simulator.enableConnectionFailures = false; + } + + Void _ = wait( g_simulator.onMachine( currentProcess ) ); + try { + Void _ = wait( delay(FLOW_KNOBS->MIN_OPEN_TIME + g_random->random01() * (FLOW_KNOBS->MAX_OPEN_TIME - FLOW_KNOBS->MIN_OPEN_TIME) ) ); + + std::string open_filename = filename; + if (flags & OPEN_ATOMIC_WRITE_AND_CREATE) { + ASSERT( (flags & OPEN_CREATE) && (flags & OPEN_READWRITE) && !(flags & OPEN_EXCLUSIVE) ); + open_filename = filename + ".part"; + } + + int h = sf_open( open_filename.c_str(), flags, flagConversion(flags), mode ); + if( h == -1 ) { + bool notFound = errno == ENOENT; + Error e = notFound ? file_not_found() : io_error(); + TraceEvent(notFound ? SevWarn : SevWarnAlways, "FileOpenError").error(e).GetLastError().detail("File", filename).detail("Flags", flags); + throw e; + } + + platform::makeTemporary(open_filename.c_str()); + SimpleFile *simpleFile = new SimpleFile( h, diskParameters, delayOnWrite, filename, open_filename, flags ); + state Reference file = Reference( simpleFile ); + Void _ = wait( g_simulator.onProcess( currentProcess, currentTaskID ) ); + return file; + } catch( Error &e ) { + state Error err = e; + Void _ = wait( g_simulator.onProcess( currentProcess, currentTaskID ) ); + throw err; + } + } + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + virtual int64_t debugFD() { return (int64_t)h; } + + virtual Future read( void* data, int length, int64_t offset ) { + return read_impl( this, data, length, offset ); + } + + virtual Future write( void const* data, int length, int64_t offset ) { + return write_impl( this, StringRef((const uint8_t*)data, length), offset ); + } + + virtual Future truncate( int64_t size ) { + return truncate_impl( this, size ); + } + + virtual Future sync() { + return sync_impl( this ); + } + + virtual Future size() { + return size_impl( this ); + } + + virtual std::string getFilename() { + return actualFilename; + } + + ~SimpleFile() { + _close( h ); + } + +private: + int h; + + //Performance parameters of simulated disk + Reference diskParameters; + + std::string filename, actualFilename; + int flags; + UID dbgId; + + //If true, then writes/truncates will be preceded by a delay (like other operations). If false, then they will not + //This is to support AsyncFileNonDurable, which issues its own delays for writes and truncates + bool delayOnWrite; + + SimpleFile(int h, Reference diskParameters, bool delayOnWrite, const std::string& filename, const std::string& actualFilename, int flags) + : h(h), diskParameters(diskParameters), delayOnWrite(delayOnWrite), filename(filename), actualFilename(actualFilename), dbgId(g_random->randomUniqueID()), flags(flags) {} + + static int flagConversion( int flags ) { + int outFlags = O_BINARY; + if( flags&OPEN_READWRITE ) outFlags |= O_RDWR; + if( flags&OPEN_CREATE ) outFlags |= O_CREAT; + if( flags&OPEN_READONLY ) outFlags |= O_RDONLY; + if( flags&OPEN_EXCLUSIVE ) outFlags |= O_EXCL; + if( flags&OPEN_ATOMIC_WRITE_AND_CREATE ) outFlags |= O_TRUNC; + + return outFlags; + } + + ACTOR static Future read_impl( SimpleFile* self, void* data, int length, int64_t offset ) { + state UID opId = g_random->randomUniqueID(); + if (randLog) + fprintf( randLog, "SFR1 %s %s %s %d %lld\n", self->dbgId.shortString().c_str(), self->filename.c_str(), opId.shortString().c_str(), length, offset ); + + Void _ = wait( waitUntilDiskReady( self->diskParameters, length ) ); + + if( _lseeki64( self->h, offset, SEEK_SET ) == -1 ) { + TraceEvent(SevWarn, "SimpleFileIOError").detail("Location", 1); + throw io_error(); + } + + unsigned int read_bytes = 0; + if( ( read_bytes = _read( self->h, data, (unsigned int) length ) ) == -1 ) { + TraceEvent(SevWarn, "SimpleFileIOError").detail("Location", 2); + throw io_error(); + } + + if (randLog) { + uint32_t a=0, b=0; + hashlittle2( data, read_bytes, &a, &b ); + fprintf( randLog, "SFR2 %s %s %s %d %d\n", self->dbgId.shortString().c_str(), self->filename.c_str(), opId.shortString().c_str(), read_bytes, a ); + } + + debugFileCheck("SimpleFileRead", self->filename, data, offset, length); + + INJECT_FAULT(io_error, "SimpleFile::read"); + INJECT_FAULT(io_timeout, "SimpleFile::read"); + + return read_bytes; + } + + ACTOR static Future write_impl( SimpleFile* self, StringRef data, int64_t offset ) { + state UID opId = g_random->randomUniqueID(); + if (randLog) { + uint32_t a=0, b=0; + hashlittle2( data.begin(), data.size(), &a, &b ); + fprintf( randLog, "SFW1 %s %s %s %d %d %lld\n", self->dbgId.shortString().c_str(), self->filename.c_str(), opId.shortString().c_str(), a, data.size(), offset ); + } + + if(self->delayOnWrite) + Void _ = wait( waitUntilDiskReady( self->diskParameters, data.size() ) ); + + if( _lseeki64( self->h, offset, SEEK_SET ) == -1 ) { + TraceEvent(SevWarn, "SimpleFileIOError").detail("Location", 3); + throw io_error(); + } + + unsigned int write_bytes = 0; + if ( ( write_bytes = _write( self->h, (void*)data.begin(), data.size() ) ) == -1 ) { + TraceEvent(SevWarn, "SimpleFileIOError").detail("Location", 4); + throw io_error(); + } + + if ( write_bytes != data.size() ) { + TraceEvent(SevWarn, "SimpleFileIOError").detail("Location", 5); + throw io_error(); + } + + if (randLog) { + fprintf( randLog, "SFW2 %s %s %s\n", self->dbgId.shortString().c_str(), self->filename.c_str(), opId.shortString().c_str()); + } + + debugFileCheck("SimpleFileWrite", self->filename, (void*)data.begin(), offset, data.size()); + INJECT_FAULT(io_error, "SimpleFile::write"); + INJECT_FAULT(io_timeout, "SimpleFile::write"); + + return Void(); + } + + ACTOR static Future truncate_impl( SimpleFile* self, int64_t size ) { + state UID opId = g_random->randomUniqueID(); + if (randLog) + fprintf( randLog, "SFT1 %s %s %s %lld\n", self->dbgId.shortString().c_str(), self->filename.c_str(), opId.shortString().c_str(), size ); + + if(self->delayOnWrite) + Void _ = wait( waitUntilDiskReady( self->diskParameters, 0 ) ); + + if( _chsize( self->h, (long) size ) == -1 ) { + TraceEvent(SevWarn, "SimpleFileIOError").detail("Location", 6); + throw io_error(); + } + + if (randLog) + fprintf( randLog, "SFT2 %s %s %s\n", self->dbgId.shortString().c_str(), self->filename.c_str(), opId.shortString().c_str()); + INJECT_FAULT( io_error, "SimpleFile::truncate" ); + + return Void(); + } + + ACTOR static Future sync_impl( SimpleFile* self ) { + state UID opId = g_random->randomUniqueID(); + if (randLog) + fprintf( randLog, "SFC1 %s %s %s\n", self->dbgId.shortString().c_str(), self->filename.c_str(), opId.shortString().c_str()); + + if(self->delayOnWrite) + Void _ = wait( waitUntilDiskReady( self->diskParameters, 0, true ) ); + + if (self->flags & OPEN_ATOMIC_WRITE_AND_CREATE) { + self->flags &= ~OPEN_ATOMIC_WRITE_AND_CREATE; + auto& machineCache = g_simulator.getCurrentProcess()->machine->openFiles; + std::string sourceFilename = self->filename + ".part"; + + if(machineCache.count(sourceFilename)) { + TraceEvent("SimpleFileRename").detail("From", sourceFilename).detail("To", self->filename).detail("sourceCount", machineCache.count(sourceFilename)).detail("fileCount", machineCache.count(self->filename)); + renameFile( sourceFilename.c_str(), self->filename.c_str() ); + + ASSERT(!machineCache.count(self->filename)); + machineCache[self->filename] = machineCache[sourceFilename]; + machineCache.erase(sourceFilename); + self->actualFilename = self->filename; + } + } + + if (randLog) + fprintf( randLog, "SFC2 %s %s %s\n", self->dbgId.shortString().c_str(), self->filename.c_str(), opId.shortString().c_str()); + INJECT_FAULT( io_error, "SimpleFile::sync" ); + + return Void(); + } + + ACTOR static Future size_impl( SimpleFile* self ) { + state UID opId = g_random->randomUniqueID(); + if (randLog) + fprintf(randLog, "SFS1 %s %s %s\n", self->dbgId.shortString().c_str(), self->filename.c_str(), opId.shortString().c_str()); + + Void _ = wait( waitUntilDiskReady( self->diskParameters, 0 ) ); + + int64_t pos = _lseeki64( self->h, 0L, SEEK_END ); + if( pos == -1 ) { + TraceEvent(SevWarn, "SimpleFileIOError").detail("Location", 8); + throw io_error(); + } + + if (randLog) + fprintf(randLog, "SFS2 %s %s %s %lld\n", self->dbgId.shortString().c_str(), self->filename.c_str(), opId.shortString().c_str(), pos); + INJECT_FAULT( io_error, "SimpleFile::size" ); + + return pos; + } +}; + +struct SimDiskSpace { + int64_t totalSpace; + int64_t baseFreeSpace; //The original free space of the disk + deltas from simulated external modifications + double lastUpdate; +}; + +void doReboot( ISimulator::ProcessInfo* const& p, ISimulator::KillType const& kt ); + +struct Sim2Listener : IListener, ReferenceCounted { + explicit Sim2Listener( ISimulator::ProcessInfo* process ) : process(process) {} + void incomingConnection( double seconds, Reference conn ) { // Called by another process! + incoming( Reference::addRef( this ), seconds, conn ); + } + + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + + virtual Future> accept() { + return popOne( nextConnection.getFuture() ); + } + + virtual NetworkAddress getListenAddress() { return process->address; } + +private: + ISimulator::ProcessInfo* process; + PromiseStream< Reference > nextConnection; + + ACTOR static void incoming( Reference self, double seconds, Reference conn ) { + Void _ = wait( g_simulator.onProcess(self->process) ); + Void _ = wait( delay( seconds ) ); + if (((Sim2Conn*)conn.getPtr())->isPeerGone() && g_random->random01()<0.5) + return; + TraceEvent("Sim2IncomingConn", conn->getDebugID()); + self->nextConnection.send( conn ); + } + ACTOR static Future> popOne( FutureStream< Reference > conns ) { + Reference c = waitNext( conns ); + ((Sim2Conn*)c.getPtr())->opened = true; + return c; + } +}; + +#define g_sim2 ((Sim2&)g_simulator) + +class Sim2 : public ISimulator, public INetworkConnections { +public: + // Implement INetwork interface + // Everything actually network related is delegated to the Sim2Net class; Sim2 is only concerned with simulating machines and time + virtual double now() { return time; } + + virtual Future delay( double seconds, int taskID ) { + ASSERT(taskID >= TaskMinPriority && taskID <= TaskMaxPriority); + return delay( seconds, taskID, currentProcess ); + } + Future delay( double seconds, int taskID, ProcessInfo* machine ) { + ASSERT( seconds >= -0.0001 ); + seconds = std::max(0.0, seconds); + Future f; + + if(!currentProcess->rebooting && machine == currentProcess && !currentProcess->shutdownSignal.isSet() && FLOW_KNOBS->MAX_BUGGIFIED_DELAY > 0 && g_random->random01() < 0.25) { //FIXME: why doesnt this work when we are changing machines? + seconds += FLOW_KNOBS->MAX_BUGGIFIED_DELAY*pow(g_random->random01(),1000.0); + } + + mutex.enter(); + tasks.push( Task( time + seconds, taskID, taskCount++, machine, f ) ); + mutex.leave(); + + return f; + } + ACTOR static Future checkShutdown(Sim2 *self, int taskID) { + ISimulator::KillType kt = wait( self->getCurrentProcess()->shutdownSignal.getFuture() ); + self->setCurrentTask(taskID); + return Void(); + } + virtual Future yield( int taskID ) { + if (taskID == TaskDefaultYield) taskID = currentTaskID; + if (check_yield(taskID)) { + // We want to check that yielders can handle actual time elapsing (it sometimes will outside simulation), but + // don't want to prevent instantaneous shutdown of "rebooted" machines. + return delay(getCurrentProcess()->rebooting ? 0 : .001,taskID) || checkShutdown(this, taskID); + } + setCurrentTask(taskID); + return Void(); + } + virtual bool check_yield( int taskID ) { + if (yielded) return true; + if (--yield_limit <= 0) { + yield_limit = g_random->randomInt(1, 150); // If yield returns false *too* many times in a row, there could be a stack overflow, since we can't deterministically check stack size as the real network does + return yielded = true; + } + return yielded = BUGGIFY_WITH_PROB(0.01); + } + virtual int getCurrentTask() { + return currentTaskID; + } + virtual void setCurrentTask(int taskID ) { + currentTaskID = taskID; + } + // Sets the taskID/priority of the current task, without yielding + virtual Future> connect( NetworkAddress toAddr ) { + ASSERT( !toAddr.isTLS() ); + if (!addressMap.count( toAddr )) { + return waitForProcessAndConnect( toAddr, this ); + } + auto peerp = getProcessByAddress(toAddr); + Reference myc( new Sim2Conn( getCurrentProcess() ) ); + Reference peerc( new Sim2Conn( peerp ) ); + + myc->connect(peerc, toAddr); peerc->connect(myc, NetworkAddress( getCurrentProcess()->address.ip + g_random->randomInt(0,256), g_random->randomInt(40000, 60000) )); + + ((Sim2Listener*)peerp->listener.getPtr())->incomingConnection( 0.5*g_random->random01(), Reference(peerc) ); + return onConnect( ::delay(0.5*g_random->random01()), myc ); + } + ACTOR static Future> onConnect( Future ready, Reference conn ) { + Void _ = wait(ready); + if (conn->isPeerGone() && g_random->random01()<0.5) { + conn.clear(); + Void _ = wait(Never()); + } + conn->opened = true; + return conn; + } + virtual Reference listen( NetworkAddress localAddr ) { + ASSERT( !localAddr.isTLS() ); + ASSERT( localAddr == getCurrentProcess()->address ); + return Reference( getCurrentProcess()->listener ); + } + ACTOR static Future> waitForProcessAndConnect( + NetworkAddress toAddr, INetworkConnections *self ) { + // We have to be able to connect to processes that don't yet exist, so we do some silly polling + loop { + Void _ = wait( ::delay( 0.1 * g_random->random01() ) ); + if (g_sim2.addressMap.count(toAddr)) { + Reference c = wait( self->connect( toAddr ) ); + return c; + } + } + } + + virtual void stop() { isStopped = true; } + virtual bool isSimulated() const { return true; } + + struct SimThreadArgs { + THREAD_FUNC_RETURN (*func) (void*); + void *arg; + + ISimulator::ProcessInfo *currentProcess; + + SimThreadArgs(THREAD_FUNC_RETURN (*func) (void*), void *arg) : func(func), arg(arg) { + ASSERT(g_network->isSimulated()); + currentProcess = g_simulator.getCurrentProcess(); + } + }; + + //Starts a new thread, making sure to set any thread local state + THREAD_FUNC simStartThread(void *arg) { + SimThreadArgs *simArgs = (SimThreadArgs*)arg; + ISimulator::currentProcess = simArgs->currentProcess; + simArgs->func(simArgs->arg); + + delete simArgs; + THREAD_RETURN; + } + + virtual THREAD_HANDLE startThread( THREAD_FUNC_RETURN (*func) (void*), void *arg ) { + SimThreadArgs *simArgs = new SimThreadArgs(func, arg); + return ::startThread(simStartThread, simArgs); + } + + virtual void getDiskBytes( std::string const& directory, int64_t& free, int64_t& total) { + ProcessInfo *proc = getCurrentProcess(); + SimDiskSpace &diskSpace = diskSpaceMap[proc->address.ip]; + + int64_t totalFileSize = 0; + int numFiles = 0; + + //Get the size of all files we've created on the server and subtract them from the free space + for(auto file = proc->machine->openFiles.begin(); file != proc->machine->openFiles.end(); ++file) { + if( file->second.isReady() ) { + totalFileSize += ((AsyncFileNonDurable*)file->second.get().getPtr())->approximateSize; + } + numFiles++; + } + + bool ok = false; + + if(diskSpace.totalSpace == 0) { + diskSpace.totalSpace = 5e9 + g_random->random01() * 100e9; //Total space between 5GB and 105GB + diskSpace.baseFreeSpace = std::min(diskSpace.totalSpace, std::max(5e9, (g_random->random01() * (1 - .075) + .075) * diskSpace.totalSpace) + totalFileSize); //Minimum 5GB or 7.5% total disk space, whichever is higher + + TraceEvent("Sim2DiskSpaceInitialization").detail("TotalSpace", diskSpace.totalSpace).detail("BaseFreeSpace", diskSpace.baseFreeSpace).detail("totalFileSize", totalFileSize).detail("NumFiles", numFiles); + } + else { + int64_t maxDelta = std::min(5.0, (now() - diskSpace.lastUpdate)) * (BUGGIFY ? 10e6 : 1e6); //External processes modifying the disk + int64_t delta = -maxDelta + g_random->random01() * maxDelta * 2; + diskSpace.baseFreeSpace = std::min(diskSpace.totalSpace, std::max(diskSpace.baseFreeSpace + delta, totalFileSize)); + } + + diskSpace.lastUpdate = now(); + + total = diskSpace.totalSpace; + free = std::max(0, diskSpace.baseFreeSpace - totalFileSize); + + if(free == 0) + TraceEvent(SevWarnAlways, "Sim2NoFreeSpace").detail("TotalSpace", diskSpace.totalSpace).detail("BaseFreeSpace", diskSpace.baseFreeSpace).detail("totalFileSize", totalFileSize).detail("NumFiles", numFiles); + } + virtual bool isAddressOnThisHost( NetworkAddress const& addr ) { + return addr.ip == getCurrentProcess()->address.ip; + } + + ACTOR static Future deleteFileImpl( Sim2* self, std::string filename, bool mustBeDurable ) { + // This is a _rudimentary_ simulation of the untrustworthiness of non-durable deletes and the possibility of + // rebooting during a durable one. It isn't perfect: for example, on real filesystems testing + // for the existence of a non-durably deleted file BEFORE a reboot will show that it apparently doesn't exist. + g_simulator.getCurrentProcess()->machine->openFiles.erase(filename); + if ( mustBeDurable || g_random->random01() < 0.5 ) { + Void _ = wait( ::delay(0.05 * g_random->random01()) ); + if (!self->getCurrentProcess()->rebooting) { + auto f = IAsyncFileSystem::filesystem(self->net2)->deleteFile(filename, false); + ASSERT( f.isReady() ); + Void _ = wait( ::delay(0.05 * g_random->random01()) ); + TEST( true ); // Simulated durable delete + } + return Void(); + } else { + TEST( true ); // Simulated non-durable delete + return Void(); + } + } + + ACTOR static Future runLoop(Sim2 *self) { + state ISimulator::ProcessInfo *callingMachine = self->currentProcess; + while ( !self->isStopped ) { + Void _ = wait( self->net2->yield(TaskDefaultYield) ); + + self->mutex.enter(); + if( self->tasks.size() == 0 ) { + self->mutex.leave(); + ASSERT(false); + } + //if (!randLog/* && now() >= 32.0*/) + // randLog = fopen("randLog.txt", "wt"); + Task t = std::move( self->tasks.top() ); // Unfortunately still a copy under gcc where .top() returns const& + self->currentTaskID = t.taskID; + self->tasks.pop(); + self->mutex.leave(); + + self->execTask(t); + self->yielded = false; + } + self->currentProcess = callingMachine; + self->net2->stop(); + return Void(); + } + + ACTOR Future _run(Sim2 *self) { + Future loopFuture = self->runLoop(self); + self->net2->run(); + Void _ = wait( loopFuture ); + return Void(); + } + + // Implement ISimulator interface + virtual void run() { + _run(this); + } + virtual ProcessInfo* newProcess(const char* name, uint32_t ip, uint16_t port, + LocalityData locality, ProcessClass startingClass, const char* dataFolder, const char* coordinationFolder) { + ASSERT( locality.zoneId().present() ); + MachineInfo& machine = machines[ locality.zoneId().get() ]; + if (!machine.zoneId.present()) + machine.zoneId = locality.zoneId(); + for( int i = 0; i < machine.processes.size(); i++ ) { + if( machine.processes[i]->locality.zoneId() != locality.zoneId() ) { // SOMEDAY: compute ip from locality to avoid this check + TraceEvent("Sim2Mismatch").detail("IP", format("%x", ip)) + .detailext("zoneId", locality.zoneId()).detail("NewName", name) + .detailext("ExistingmachineId", machine.processes[i]->locality.zoneId()).detail("ExistingName", machine.processes[i]->name); + ASSERT( false ); + } + ASSERT( machine.processes[i]->address.port != port ); + } + + // This is for async operations on non-durable files. + // These files must live on after process kills for sim purposes. + if( machine.machineProcess == 0 ) { + NetworkAddress machineAddress(ip, 0, false, false); + machine.machineProcess = new ProcessInfo("Machine", locality, startingClass, machineAddress, this, "", ""); + machine.machineProcess->machine = &machine; + } + + NetworkAddress address(ip, port, true, false); // SOMEDAY see above about becoming SSL! + ProcessInfo* m = new ProcessInfo(name, locality, startingClass, address, this, dataFolder, coordinationFolder); + m->listener = Reference( new Sim2Listener(m) ); + m->machine = &machine; + machine.processes.push_back(m); + currentlyRebootingProcesses.erase(address); + addressMap[ m->address ] = m; + + m->setGlobal(enTDMetrics, (flowGlobalType) &m->tdmetrics); + m->setGlobal(enNetworkConnections, (flowGlobalType) m->network); + + TraceEvent("NewMachine").detail("Name", name).detail("Address", m->address).detailext("zoneId", m->locality.zoneId()); + + // FIXME: Sometimes, connections to/from this process will explicitly close + + return m; + } + + // The following function will determine if the specified configuration of available and dead processes can allow the cluster to survive + virtual bool canKillProcesses(std::vector const& availableProcesses, std::vector const& deadProcesses, KillType kt, KillType* newKillType) + { + bool canSurvive = true; + KillType newKt = kt; + if ((kt == KillInstantly) || (kt == InjectFaults) || (kt == RebootAndDelete) || (kt == RebootProcessAndDelete)) + { + LocalityGroup processesLeft, processesDead; + std::vector localitiesDead, localitiesLeft, badCombo; + ASSERT(storagePolicy); + ASSERT(tLogPolicy); + for (auto processInfo : availableProcesses) { + processesLeft.add(processInfo->locality); + localitiesLeft.push_back(processInfo->locality); + } + for (auto processInfo : deadProcesses) { + processesDead.add(processInfo->locality); + localitiesDead.push_back(processInfo->locality); + } + // Reboot if dead machines do fulfill policies + if (processesDead.validate(tLogPolicy)) { + newKt = Reboot; + canSurvive = false; + TraceEvent("KillChanged").detail("KillType", kt).detail("NewKillType", newKt).detail("tLogPolicy", tLogPolicy->info()).detail("ProcessesLeft", processesLeft.size()).detail("ProcessesDead", processesDead.size()).detail("DeadZones", ::describeZones(localitiesDead)).detail("DeadDataHalls", ::describeDataHalls(localitiesDead)).detail("Reason", "tLogPolicy validates against dead processes."); + } + else if (processesDead.validate(storagePolicy)) { + newKt = Reboot; + canSurvive = false; + TraceEvent("KillChanged").detail("KillType", kt).detail("NewKillType", newKt).detail("storagePolicy", storagePolicy->info()).detail("ProcessesLeft", processesLeft.size()).detail("ProcessesDead", processesDead.size()).detail("DeadZones", ::describeZones(localitiesDead)).detail("DeadDataHalls", ::describeDataHalls(localitiesDead)).detail("Reason", "storagePolicy validates against dead processes."); + } + // Check all combinations of the AntiQuorum within the failed + else if ((tLogWriteAntiQuorum) && (!validateAllCombinations(badCombo, processesDead, tLogPolicy, localitiesLeft, tLogWriteAntiQuorum, false))) + { + newKt = Reboot; + canSurvive = false; + TraceEvent("KillChanged").detail("KillType", kt).detail("NewKillType", newKt).detail("storagePolicy", storagePolicy->info()).detail("ProcessesLeft", processesLeft.size()).detail("ProcessesDead", processesDead.size()).detail("BadZones", ::describeZones(badCombo)).detail("BadDataHalls", ::describeDataHalls(badCombo)).detail("Reason", "tLog AntiQuorum does not validates against dead processes."); + } + // Reboot and Delete if remaining machines do NOT fulfill policies + else if ((kt != RebootAndDelete) && (kt != RebootProcessAndDelete) && (!processesLeft.validate(tLogPolicy))) { + auto newKt = (g_random->random01() < 0.33) ? RebootAndDelete : Reboot; + canSurvive = false; + TraceEvent("KillChanged").detail("KillType", kt).detail("NewKillType", newKt).detail("tLogPolicy", tLogPolicy->info()).detail("ProcessesLeft", processesLeft.size()).detail("ProcessesDead", processesDead.size()).detail("RemainingZones", ::describeZones(localitiesLeft)).detail("RemainingDataHalls", ::describeDataHalls(localitiesLeft)).detail("Reason", "tLogPolicy does not validates against remaining processes."); + } + else if ((kt != RebootAndDelete) && (kt != RebootProcessAndDelete) && (!processesLeft.validate(storagePolicy))) { + auto newKt = (g_random->random01() < 0.33) ? RebootAndDelete : Reboot; + canSurvive = false; + TraceEvent("KillChanged").detail("KillType", kt).detail("NewKillType", newKt).detail("storagePolicy", storagePolicy->info()).detail("ProcessesLeft", processesLeft.size()).detail("ProcessesDead", processesDead.size()).detail("RemainingZones", ::describeZones(localitiesLeft)).detail("RemainingDataHalls", ::describeDataHalls(localitiesLeft)).detail("Reason", "storagePolicy does not validates against remaining processes."); + } + else { + TraceEvent("CanSurviveKills").detail("KillType", kt).detail("ProcessesLeft", processesLeft.size()).detail("ProcessesDead", processesDead.size()).detail("DeadZones", ::describeZones(localitiesDead)).detail("DeadDataHalls", ::describeDataHalls(localitiesDead)).detail("tLogPolicy", tLogPolicy->info()).detail("storagePolicy", storagePolicy->info()); + } + } + if (newKillType) *newKillType = newKt; + return canSurvive; + } + + virtual void destroyProcess( ISimulator::ProcessInfo *p ) { + TraceEvent("ProcessDestroyed").detail("Name", p->name).detail("Address", p->address).detailext("zoneId", p->locality.zoneId()).backtrace(); + currentlyRebootingProcesses.insert(std::pair(p->address, p)); + std::vector& processes = machines[ p->locality.zoneId().get() ].processes; + if( p != processes.back() ) { + auto it = std::find( processes.begin(), processes.end(), p ); + std::swap( *it, processes.back() ); + } + processes.pop_back(); + killProcess_internal( p, KillInstantly ); + } + void killProcess_internal( ProcessInfo* machine, KillType kt ) { + TEST( kt == KillInstantly ); // Simulated machine was killed instantly + TEST( kt == InjectFaults ); // Simulated machine was killed with faults + + if (kt == KillInstantly) { + TraceEvent(SevWarn, "FailMachine").detail("Name", machine->name).detail("Address", machine->address).detailext("ZoneId", machine->locality.zoneId()).backtrace(); + // This will remove all the "tracked" messages that came from the machine being killed + latestEventCache.clear(); + machine->failed = true; + } else if (kt == InjectFaults) { + TraceEvent(SevWarn, "FaultMachine").detail("Name", machine->name).detail("Address", machine->address).detailext("ZoneId", machine->locality.zoneId()).backtrace(); + should_inject_fault = simulator_should_inject_fault; + machine->fault_injection_r = g_random->randomUniqueID().first(); + machine->fault_injection_p1 = 0.1; + machine->fault_injection_p2 = g_random->random01(); + } else { + ASSERT( false ); + } + } + virtual void rebootProcess( ProcessInfo* process, KillType kt ) { + if( kt == RebootProcessAndDelete && protectedAddresses.count(process->address) ) + kt = RebootProcess; + doReboot( process, kt ); + } + virtual void rebootProcess(Optional> zoneId, bool allProcesses ) { + if( allProcesses ) { + auto processes = getAllProcesses(); + for( int i = 0; i < processes.size(); i++ ) + if( processes[i]->locality.zoneId() == zoneId && !processes[i]->rebooting ) + doReboot( processes[i], RebootProcess ); + } else { + auto processes = getAllProcesses(); + for( int i = 0; i < processes.size(); i++ ) { + if( processes[i]->locality.zoneId() != zoneId || processes[i]->rebooting ) { + std::swap(processes[i--], processes.back()); + processes.pop_back(); + } + } + if( processes.size() ) + doReboot( g_random->randomChoice( processes ), RebootProcess ); + } + } + virtual void killProcess( ProcessInfo* machine, KillType kt ) { + TraceEvent("attemptingKillProcess").detail("killedMachines", killedMachines).detail("killableMachines", killableMachines); + if (kt < RebootAndDelete ) { + killProcess_internal( machine, kt ); + killedMachines++; + } + } + virtual void killInterface( NetworkAddress address, KillType kt ) { + if (kt < RebootAndDelete ) { + std::vector& processes = machines[ addressMap[address]->locality.zoneId() ].processes; + for( int i = 0; i < processes.size(); i++ ) + killProcess_internal( processes[i], kt ); + killedMachines++; + } + } + virtual bool killMachine(Optional> zoneId, KillType kt, bool killIsSafe, bool forceKill ) { + auto ktOrig = kt; + if (killIsSafe) ASSERT( kt == ISimulator::RebootAndDelete ); // Only types of "safe" kill supported so far + + if(speedUpSimulation && !forceKill) { + return false; + } + + int processesOnMachine = 0; + + // Reboot if any of the processes are protected and count the number of processes not rebooting + for (auto& process : machines[zoneId].processes) { + if (protectedAddresses.count(process->address)) + kt = Reboot; + if (!process->rebooting) + processesOnMachine++; + } + + // Check if machine can be removed, if requested + if ((kt == KillInstantly) || (kt == InjectFaults) || (kt == RebootAndDelete) || (kt == RebootProcessAndDelete)) + { + std::vector processesLeft, processesDead; + + for (auto machineRec : machines) { + for (auto processInfo : machineRec.second.processes) { + // Add non-test processes (ie. datahall is not be set for test processes) + if (processInfo->startingClass != ProcessClass::TesterClass) { + // Add machine processes to dead group if dead or specified kill machine + if (processInfo->failed || machineRec.second.dead || (machineRec.second.zoneId == zoneId)) + processesDead.push_back(processInfo); + else + processesLeft.push_back(processInfo); + } + } + } + if (!canKillProcesses(processesLeft, processesLeft, kt, &kt)) { + if ((kt != Reboot) && (!killIsSafe)) { + kt = Reboot; + } + TraceEvent("ChangedKillMachine", zoneId).detailext("ZoneId", zoneId).detail("KillType", kt).detail("OrigKillType", ktOrig).detail("ProcessesLeft", processesLeft.size()).detail("ProcessesDead", processesDead.size()).detail("TotalZones", machines.size()).detail("processesPerMachine", processesPerMachine).detail("tLogPolicy", tLogPolicy->info()).detail("storagePolicy", storagePolicy->info()); + } + else if ((kt == KillInstantly) || (kt == InjectFaults)) { + TraceEvent("DeadMachine", zoneId).detailext("ZoneId", zoneId).detail("KillType", kt).detail("ProcessesLeft", processesLeft.size()).detail("ProcessesDead", processesDead.size()).detail("TotalZones", machines.size()).detail("processesPerMachine", processesPerMachine).detail("tLogPolicy", tLogPolicy->info()).detail("storagePolicy", storagePolicy->info()); + machines[zoneId].dead = true; + } + else { + TraceEvent("ClearMachine", zoneId).detailext("ZoneId", zoneId).detail("KillType", kt).detail("ProcessesLeft", processesLeft.size()).detail("ProcessesDead", processesDead.size()).detail("TotalZones", machines.size()).detail("processesPerMachine", processesPerMachine).detail("tLogPolicy", tLogPolicy->info()).detail("storagePolicy", storagePolicy->info()); + } + } + + // Check if any processes on machine are rebooting + if( processesOnMachine != processesPerMachine && kt >= RebootAndDelete ) { + TEST(true); //Attempted reboot, but the target did not have all of its processes running + TraceEvent(SevWarn, "AbortedReboot", zoneId).detailext("ZoneId", zoneId).detail("Reason", "The target did not have all of its processes running.").detail("processes", processesOnMachine).detail("processesPerMachine", processesPerMachine).backtrace(); + return false; + } + + TraceEvent("KillMachine", zoneId).detailext("ZoneId", zoneId).detail("Kt", kt).detail("KtOrig", ktOrig).detail("KilledMachines", killedMachines).detail("KillableMachines", processesOnMachine).detail("ProcessPerMachine", processesPerMachine).detail("KillChanged", kt == ktOrig).detail("killIsSafe", killIsSafe); + if (kt < RebootAndDelete ) { + for (auto& process : machines[zoneId].processes) { + TraceEvent("KillMachineProcess", zoneId).detail("KillType", kt).detail("Process", process->toString()).detail("startingClass", process->startingClass.toString()); + if (process->startingClass != ProcessClass::TesterClass) + killProcess_internal( process, kt ); + } + } + else if ( kt == Reboot || killIsSafe) { + for (auto& process : machines[zoneId].processes) { + TraceEvent("KillMachineProcess", zoneId).detail("KillType", kt).detail("Process", process->toString()).detail("startingClass", process->startingClass.toString()); + if (process->startingClass != ProcessClass::TesterClass) + doReboot(process, kt ); + } + } + + return true; + } + + virtual void killDataCenter(Optional> dcId, KillType kt ) { + auto ktOrig = kt; + auto processes = getAllProcesses(); + std::map>, int> datacenterZones; + int dcProcesses = 0; + + // Switch to a reboot, if anything protected on machine + for (auto& process : processes) { + auto processDcId = process->locality.dcId(); + auto processZoneId = process->locality.zoneId(); + ASSERT(processZoneId.present()); + if (processDcId.present() && (processDcId == dcId)) { + if (protectedAddresses.count(process->address)) + kt = Reboot; + datacenterZones[processZoneId.get()] ++; + dcProcesses ++; + } + } + + // Check if machine can be removed, if requested + if ((kt == KillInstantly) || (kt == InjectFaults) || (kt == RebootAndDelete) || (kt == RebootProcessAndDelete)) + { + std::vector processesLeft, processesDead; + for (auto machineRec : machines) { + for (auto processInfo : machineRec.second.processes) { + // Add non-test processes (ie. datahall is not be set for test processes) + if (processInfo->startingClass != ProcessClass::TesterClass) { + // Add processes from dead machines and datacenter machines to dead group + if (processInfo->failed || machineRec.second.dead || (datacenterZones.find(machineRec.second.zoneId) != datacenterZones.end())) + processesDead.push_back(processInfo); + else + processesLeft.push_back(processInfo); + } + } + } + + if (!canKillProcesses(processesLeft, processesLeft, kt, &kt)) { + TraceEvent(SevWarn, "DcKillChanged").detailext("DataCenter", dcId).detail("KillType", ktOrig).detail("NewKillType", kt); + } + else { + TraceEvent("DeadDataCenter").detailext("DataCenter", dcId).detail("KillType", kt).detail("DcZones", datacenterZones.size()).detail("DcProcesses", dcProcesses).detail("ProcessesDead", processesDead.size()).detail("ProcessesLeft", processesLeft.size()).detail("tLogPolicy", storagePolicy->info()).detail("storagePolicy", storagePolicy->info()); + } + } + + TraceEvent("killDataCenter") + .detail("killedMachines", killedMachines) + .detail("killableMachines", killableMachines) + .detail("killableDatacenters", killableDatacenters) + .detail("maxCoordinatorsInDatacenter", maxCoordinatorsInDatacenter) + .detail("DcZones", datacenterZones.size()) + .detail("DcProcesses", dcProcesses) + .detailext("DCID", dcId) + .detail("KillType", kt); + + for (auto& datacenterZone : datacenterZones) + killMachine( datacenterZone.first, kt, (kt == RebootAndDelete), true); + } + virtual void clogInterface( uint32_t ip, double seconds, ClogMode mode = ClogDefault ) { + if (mode == ClogDefault) { + double a = g_random->random01(); + if ( a < 0.3 ) mode = ClogSend; + else if (a < 0.6 ) mode = ClogReceive; + else mode = ClogAll; + } + TraceEvent("ClogInterface").detail("IP", toIPString(ip)).detail("Delay", seconds) + .detail("Queue", mode==ClogSend?"Send":mode==ClogReceive?"Receive":"All"); + + if (mode == ClogSend || mode==ClogAll) + g_clogging.clogSendFor( ip, seconds ); + if (mode == ClogReceive || mode==ClogAll) + g_clogging.clogRecvFor( ip, seconds ); + } + virtual void clogPair( uint32_t from, uint32_t to, double seconds ) { + g_clogging.clogPairFor( from, to, seconds ); + } + virtual std::vector getAllProcesses() { + std::vector processes; + for( auto c = machines.begin(); c != machines.end(); ++c ) + processes.insert( processes.end(), c->second.processes.begin(), c->second.processes.end() ); + return processes; + } + virtual ProcessInfo* getProcessByAddress( NetworkAddress const& address ) { + NetworkAddress normalizedAddress(address.ip, address.port, true, false); + ASSERT( addressMap.count( normalizedAddress ) ); + return addressMap[ normalizedAddress ]; + } + + virtual MachineInfo* getMachineByNetworkAddress(NetworkAddress const& address) { + return &machines[addressMap[address]->locality.zoneId()]; + } + + virtual MachineInfo* getMachineById(Optional> const& zoneId) { + return &machines[zoneId]; + } + + virtual void destroyMachine(Optional> const& zoneId ) { + auto& machine = machines[zoneId]; + for( auto process : machine.processes ) { + ASSERT( process->failed ); + } + machine.dead = true; + if( machine.machineProcess ) { + killProcess_internal( machine.machineProcess, KillInstantly ); + } + machines.erase(zoneId); + } + + Sim2() : time(0.0), taskCount(0), yielded(false), yield_limit(0), currentTaskID(-1) { + // Not letting currentProcess be NULL eliminates some annoying special cases + currentProcess = new ProcessInfo( "NoMachine", LocalityData(Optional>(), StringRef(), StringRef(), StringRef()), ProcessClass(), NetworkAddress(), this, "", "" ); + g_network = net2 = newNet2(NetworkAddress(), false, true); + Net2FileSystem::newFileSystem(); + check_yield(0); + } + + // Implementation + struct Task { + int taskID; + double time; + uint64_t stable; + ProcessInfo* machine; + Promise action; + Task( double time, int taskID, uint64_t stable, ProcessInfo* machine, Promise&& action ) : time(time), taskID(taskID), stable(stable), machine(machine), action(std::move(action)) {} + Task( double time, int taskID, uint64_t stable, ProcessInfo* machine, Future& future ) : time(time), taskID(taskID), stable(stable), machine(machine) { future = action.getFuture(); } + Task(Task&& rhs) noexcept(true) : time(rhs.time), taskID(rhs.taskID), stable(rhs.stable), machine(rhs.machine), action(std::move(rhs.action)) {} + void operator= ( Task const& rhs ) { taskID = rhs.taskID; time = rhs.time; stable = rhs.stable; machine = rhs.machine; action = rhs.action; } + Task( Task const& rhs ) : taskID(rhs.taskID), time(rhs.time), stable(rhs.stable), machine(rhs.machine), action(rhs.action) {} + void operator= (Task&& rhs) noexcept(true) { time = rhs.time; taskID = rhs.taskID; stable = rhs.stable; machine = rhs.machine; action = std::move(rhs.action); } + + bool operator < (Task const& rhs) const { + // Ordering is reversed for priority_queue + if (time != rhs.time) return time > rhs.time; + return stable > rhs.stable; + } + }; + + void execTask(struct Task& t) { + if (t.machine->failed) { + t.action.send(Never()); + } + else { + mutex.enter(); + this->time = t.time; + mutex.leave(); + + this->currentProcess = t.machine; + try { + //auto before = getCPUTicks(); + t.action.send(Void()); + ASSERT( this->currentProcess == t.machine ); + /*auto elapsed = getCPUTicks() - before; + currentProcess->cpuTicks += elapsed; + if (g_random->random01() < 0.01){ + TraceEvent("st").detail("cpu", currentProcess->cpuTicks); + currentProcess->cpuTicks = 0; + }*/ + } catch (Error& e) { + TraceEvent(SevError, "UnhandledSimulationEventError").error(e, true); + killProcess(t.machine, KillInstantly); + } + + //if( this->time > 45.522817 ) { + // printf("foo\n"); + //} + + if (randLog) + fprintf( randLog, "T %f %d %s %lld\n", this->time, int(g_random->peek() % 10000), t.machine ? t.machine->name : "none", t.stable); + } + } + + virtual void onMainThread( Promise&& signal, int taskID ) { + // This is presumably coming from either a "fake" thread pool thread, i.e. it is actually on this thread + // or a thread created with g_network->startThread + ASSERT(getCurrentProcess()); + + mutex.enter(); + ASSERT(taskID >= TaskMinPriority && taskID <= TaskMaxPriority); + tasks.push( Task( time, taskID, taskCount++, getCurrentProcess(), std::move(signal) ) ); + mutex.leave(); + } + virtual Future onProcess( ISimulator::ProcessInfo *process, int taskID ) { + return delay( 0, taskID, process ); + } + virtual Future onMachine( ISimulator::ProcessInfo *process, int taskID ) { + if( process->machine == 0 ) + return Void(); + return delay( 0, taskID, process->machine->machineProcess ); + } + + //time is guarded by ISimulator::mutex. It is not necessary to guard reads on the main thread because + //time should only be modified from the main thread. + double time; + int currentTaskID; + + //taskCount is guarded by ISimulator::mutex + uint64_t taskCount; + + std::map>, MachineInfo > machines; + std::map addressMap; + std::map> filesDeadMap; + + //tasks is guarded by ISimulator::mutex + std::priority_queue> tasks; + + //Sim2Net network; + INetwork *net2; + + //Map from machine IP -> machine disk space info + std::map diskSpaceMap; + + //Whether or not yield has returned true during the current iteration of the run loop + bool yielded; + int yield_limit; // how many more times yield may return false before next returning true +}; + +void startNewSimulator() { + ASSERT( !g_network ); + g_network = g_pSimulator = new Sim2(); + g_simulator.enableConnectionFailures = g_random->random01() < 0.5; +} + +static double networkLatency() { + double a = g_random->random01(); + const double pFast = 0.999; + if (a <= pFast) + return FLOW_KNOBS->MIN_NETWORK_LATENCY + FLOW_KNOBS->FAST_NETWORK_LATENCY/pFast * a; // 0.5ms average + else{ + a = (a-pFast) / (1-pFast); // uniform 0-1 again + return FLOW_KNOBS->MIN_NETWORK_LATENCY + FLOW_KNOBS->SLOW_NETWORK_LATENCY*a; // long tail up to X ms + } +} + +ACTOR void doReboot( ISimulator::ProcessInfo *p, ISimulator::KillType kt ) { + Void _ = wait( g_sim2.delay( 0, TaskDefaultDelay, p ) ); // Switch to the machine in question + + try { + ASSERT( kt == ISimulator::RebootProcess || kt == ISimulator::Reboot || kt == ISimulator::RebootAndDelete || kt == ISimulator::RebootProcessAndDelete ); + + TEST( kt == ISimulator::RebootProcess ); // Simulated process rebooted + TEST( kt == ISimulator::Reboot ); // Simulated machine rebooted + TEST( kt == ISimulator::RebootAndDelete ); // Simulated machine rebooted with data and coordination state deletion + TEST( kt == ISimulator::RebootProcessAndDelete ); // Simulated process rebooted with data and coordination state deletion + + if( p->rebooting ) + return; + TraceEvent("RebootingMachine").detail("KillType", kt).detail("Address", p->address).detailext("ZoneId", p->locality.zoneId()).detailext("DataHall", p->locality.dataHallId()).detail("Locality", p->locality.toString()); + p->rebooting = true; + p->shutdownSignal.send( kt ); + } catch (Error& e) { + TraceEvent(SevError, "RebootError").error(e); + p->shutdownSignal.sendError(e); // ? + throw; // goes nowhere! + } +} + +//Simulates delays for performing operations on disk +Future waitUntilDiskReady( Reference diskParameters, int64_t size, bool sync ) { + if(!g_simulator.enableConnectionFailures) + return delay(0.0001); + + if( diskParameters->nextOperation < now() ) diskParameters->nextOperation = now(); + diskParameters->nextOperation += ( 1.0 / diskParameters->iops ) + ( size / diskParameters->bandwidth ); + + double randomLatency; + if(sync) { + randomLatency = .005 + g_random->random01() * (BUGGIFY ? 1.0 : .010); + } else + randomLatency = 10 * g_random->random01() / diskParameters->iops; + + return delayUntil( diskParameters->nextOperation + randomLatency ); +} + +#if defined(_WIN32) + +/* Opening with FILE_SHARE_DELETE lets simulation actually work on windows - previously renames were always failing. + FIXME: Use an actual platform abstraction for this stuff! Is there any reason we can't use underlying net2 for example? */ + +#include + +int sf_open( const char* filename, int flags, int convFlags, int mode ) { + HANDLE wh = CreateFile( filename, GENERIC_READ | ((flags&IAsyncFile::OPEN_READWRITE) ? GENERIC_WRITE : 0), + FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, + (flags&IAsyncFile::OPEN_EXCLUSIVE) ? CREATE_NEW : + (flags&IAsyncFile::OPEN_CREATE) ? OPEN_ALWAYS : + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + NULL ); + int h = -1; + if (wh != INVALID_HANDLE_VALUE) h = _open_osfhandle( (intptr_t)wh, convFlags ); + else errno = GetLastError() == ERROR_FILE_NOT_FOUND ? ENOENT : EFAULT; + return h; +} + +#endif + +// Opens a file for asynchronous I/O +Future< Reference > Sim2FileSystem::open( std::string filename, int64_t flags, int64_t mode ) +{ + ASSERT( (flags & IAsyncFile::OPEN_ATOMIC_WRITE_AND_CREATE) || + !(flags & IAsyncFile::OPEN_CREATE) || + StringRef(filename).endsWith(LiteralStringRef(".fdb-lock")) ); // We don't use "ordinary" non-atomic file creation right now except for folder locking, and we don't have code to simulate its unsafeness. + + if ( (flags & IAsyncFile::OPEN_EXCLUSIVE) ) ASSERT( flags & IAsyncFile::OPEN_CREATE ); + + if (flags & IAsyncFile::OPEN_UNCACHED) { + auto& machineCache = g_simulator.getCurrentProcess()->machine->openFiles; + std::string actualFilename = filename; + if ( machineCache.find(filename) == machineCache.end() ) { + if(flags & IAsyncFile::OPEN_ATOMIC_WRITE_AND_CREATE) { + actualFilename = filename + ".part"; + auto partFile = machineCache.find(actualFilename); + if(partFile != machineCache.end()) { + return AsyncFileDetachable::open(partFile->second); + } + } + //Simulated disk parameters are shared by the AsyncFileNonDurable and the underlying SimpleFile. This way, they can both keep up with the time to start the next operation + Reference diskParameters(new DiskParameters(FLOW_KNOBS->SIM_DISK_IOPS, FLOW_KNOBS->SIM_DISK_BANDWIDTH)); + machineCache[actualFilename] = AsyncFileNonDurable::open(filename, actualFilename, SimpleFile::open(filename, flags, mode, diskParameters, false), diskParameters); + } + return AsyncFileDetachable::open( machineCache[actualFilename] ); + } + else + return AsyncFileCached::open(filename, flags, mode); +} + +// Deletes the given file. If mustBeDurable, returns only when the file is guaranteed to be deleted even after a power failure. +Future< Void > Sim2FileSystem::deleteFile( std::string filename, bool mustBeDurable ) +{ + return Sim2::deleteFileImpl(&g_sim2, filename, mustBeDurable); +} + +void Sim2FileSystem::newFileSystem() +{ + g_network->setGlobal(INetwork::enFileSystem, (flowGlobalType) new Sim2FileSystem()); +} diff --git a/fdbrpc/sim_validation.cpp b/fdbrpc/sim_validation.cpp new file mode 100644 index 0000000000..a1c8ed32e5 --- /dev/null +++ b/fdbrpc/sim_validation.cpp @@ -0,0 +1,115 @@ +/* + * sim_validation.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sim_validation.h" +#include "TraceFileIO.h" +#include "flow/network.h" +#include "fdbrpc/simulator.h" + +//used for simulation validations +static std::map< std::string, int64_t > validationData; +static std::set disabledMachines; + +void debug_setVersionCheckEnabled(UID uid, bool enabled) { + if(enabled) + disabledMachines.erase(uid); + else { + disabledMachines.insert(uid); + debug_removeVersions(uid); + } +} + +void debug_advanceCommittedVersions(UID id, int64_t minVersion, int64_t maxVersion) { + debug_advanceMinCommittedVersion(id, minVersion); + debug_advanceMaxCommittedVersion(id, maxVersion); +} + +void debug_advanceVersion(UID id, int64_t version, const char *suffix) { + if(!disabledMachines.count(id)) { + auto& entry = validationData[id.toString() + suffix]; + if( version > entry ) + entry = version; + } +} + +void debug_advanceMinCommittedVersion(UID id, int64_t version) { + if (!g_network->isSimulated() || g_simulator.extraDB) return; + debug_advanceVersion(id, version, "min"); +} + +void debug_advanceMaxCommittedVersion(UID id, int64_t version) { + if (!g_network->isSimulated() || g_simulator.extraDB) return; + debug_advanceVersion(id, version, "max"); +} + +bool debug_checkPartRestoredVersion( UID id, int64_t version, std::string context, std::string minormax, Severity sev = SevError ) { + if (!g_network->isSimulated() || g_simulator.extraDB) return false; + if( disabledMachines.count(id) ) return false; + if( !validationData.count( id.toString() + minormax ) ) { + TraceEvent(SevWarn, (context + "UnknownVersion").c_str(), id).detail("RestoredVersion", version); + return false; + } + int sign = minormax=="min" ? 1 : -1; + if( version*sign < validationData[id.toString() + minormax]*sign ) { + TraceEvent(sev, (context + "DurabilityError").c_str(), id) + .detail("RestoredVersion", version) + .detail("Checking", minormax) + .detail("MinVersion", validationData[id.toString() + "min"]) + .detail("MaxVersion", validationData[id.toString() + "max"]); + return true; + } + return false; +} + +bool debug_checkRestoredVersion( UID id, int64_t version, std::string context, Severity sev ) { + if (!g_network->isSimulated() || g_simulator.extraDB) return false; + return debug_checkPartRestoredVersion(id, version, context, "min", sev) || debug_checkPartRestoredVersion(id, version, context, "max", sev); +} + +void debug_removeVersions(UID id) { + if(!g_network->isSimulated() || g_simulator.extraDB) return; + validationData.erase( id.toString() + "min" ); + validationData.erase( id.toString() + "max" ); +} + +bool debug_versionsExist(UID id) { + if(!g_network->isSimulated() || g_simulator.extraDB) return false; + return validationData.count( id.toString() + "min" ) != 0 || validationData.count( id.toString() + "max" ) != 0; +} + +bool debug_checkMinRestoredVersion( UID id, int64_t version, std::string context, Severity sev ) { + if (!g_network->isSimulated() || g_simulator.extraDB) return false; + return debug_checkPartRestoredVersion(id, version, context, "min", sev); +} + +bool debug_checkMaxRestoredVersion( UID id, int64_t version, std::string context, Severity sev ) { + if (!g_network->isSimulated() || g_simulator.extraDB) return false; + return debug_checkPartRestoredVersion(id, version, context, "max", sev); +} + +static bool checkRelocationDuration; + +bool debug_isCheckRelocationDuration() { + return checkRelocationDuration; +} + +void debug_setCheckRelocationDuration(bool check) { + checkRelocationDuration = check; +} diff --git a/fdbrpc/sim_validation.h b/fdbrpc/sim_validation.h new file mode 100644 index 0000000000..0922179517 --- /dev/null +++ b/fdbrpc/sim_validation.h @@ -0,0 +1,51 @@ +/* + * sim_validation.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SIM_VALIDATION_H +#define SIM_VALIDATION_H +#pragma once + +#include "flow/IRandom.h" +#include "flow/Trace.h" + +// These functions track a value which is expected to be durable within a certain range of versions. +// They rely on the ability to magically store perfectly durable data in the simulator, so they have no +// effect outside simulation. +// Typical use is something like: +// debug_advanceMaxCommittedVersion( self->id, version ); +// Void _ = wait( commit(version) ); +// debug_advanceMinCommittedVersion( self->id, version ); +// and then a call to debug_checkRestoredVersion() after some kind of reboot or recovery event + +void debug_advanceCommittedVersions(UID id, int64_t minVersion, int64_t maxVersion); +void debug_advanceMaxCommittedVersion(UID id, int64_t version); +void debug_advanceMinCommittedVersion(UID id, int64_t version); + +void debug_setVersionCheckEnabled(UID id, bool enabled); +void debug_removeVersions(UID id); +bool debug_versionsExist(UID id); +bool debug_checkRestoredVersion(UID id, int64_t version, std::string context, Severity sev = SevError); +bool debug_checkMinRestoredVersion(UID id, int64_t version, std::string context, Severity sev = SevError); +bool debug_checkMaxRestoredVersion(UID id, int64_t version, std::string context, Severity sev = SevError); + +bool debug_isCheckRelocationDuration(); +void debug_setCheckRelocationDuration(bool check); + +#endif diff --git a/fdbrpc/simulator.h b/fdbrpc/simulator.h new file mode 100644 index 0000000000..f963014c19 --- /dev/null +++ b/fdbrpc/simulator.h @@ -0,0 +1,238 @@ +/* + * simulator.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FLOW_SIMULATOR_H +#define FLOW_SIMULATOR_H +#pragma once + +#include "flow/flow.h" +#include "FailureMonitor.h" +#include "Locality.h" +#include "IAsyncFile.h" +#include "flow/TDMetric.actor.h" +#include +#include "fdbrpc/ReplicationPolicy.h" + +enum ClogMode { ClogDefault, ClogAll, ClogSend, ClogReceive }; + +class ISimulator : public INetwork { +public: + ISimulator() : killedMachines(0), killableMachines(0), machinesNeededForProgress(3), neededDatacenters(1), killableDatacenters(0), killedDatacenters(0), maxCoordinatorsInDatacenter(0), desiredCoordinators(1), processesPerMachine(0), isStopped(false), enableConnectionFailures(true), speedUpSimulation(false), allSwapsDisabled(false), backupAgents(WaitForType), extraDB(NULL) {} + + // Order matters! + enum KillType { None, KillInstantly, InjectFaults, RebootAndDelete, Reboot, RebootProcessAndDelete, RebootProcess }; + + enum BackupAgentType { NoBackupAgents, WaitForType, BackupToFile, BackupToDB }; + + // Subclasses may subclass ProcessInfo as well + struct MachineInfo; + + struct ProcessInfo : NonCopyable { + const char* name; + const char* coordinationFolder; + const char* dataFolder; + MachineInfo* machine; + NetworkAddress address; + LocalityData locality; + ProcessClass startingClass; + TDMetricCollection tdmetrics; + Reference listener; + bool failed; + int64_t cpuTicks; + bool rebooting; + std::vector globals; + + INetworkConnections *network; + + uint64_t fault_injection_r; + double fault_injection_p1, fault_injection_p2; + + ProcessInfo(const char* name, LocalityData locality, ProcessClass startingClass, NetworkAddress address, + INetworkConnections *net, const char* dataFolder, const char* coordinationFolder ) + : name(name), locality(locality), startingClass(startingClass), address(address), dataFolder(dataFolder), + network(net), coordinationFolder(coordinationFolder), failed(false), cpuTicks(0), + rebooting(false), fault_injection_p1(0), fault_injection_p2(0), + fault_injection_r(0), machine(0) + {} + + Future onShutdown() { return shutdownSignal.getFuture(); } + + bool isReliable() { return !failed && fault_injection_p1 == 0 && fault_injection_p2 == 0; } + + inline flowGlobalType global(int id) { return (globals.size() > id) ? globals[id] : NULL; }; + inline void setGlobal(size_t id, flowGlobalType v) { globals.resize(std::max(globals.size(),id+1)); globals[id] = v; }; + + std::string toString() const { + return format("name: %s address: %d.%d.%d.%d:%d zone: %s datahall: %s class: %s coord: %s data: %s", + name, (address.ip>>24)&0xff, (address.ip>>16)&0xff, (address.ip>>8)&0xff, address.ip&0xff, address.port, (locality.zoneId().present() ? locality.zoneId().get().printable().c_str() : "[unset]"), (locality.dataHallId().present() ? locality.dataHallId().get().printable().c_str() : "[unset]"), startingClass.toString().c_str(), coordinationFolder, dataFolder); } + + // Members not for external use + Promise shutdownSignal; + }; + + struct MachineInfo { + ProcessInfo* machineProcess; + std::vector processes; + std::map>> openFiles; + std::set closingFiles; + Optional> zoneId; + bool dead; + + MachineInfo() : machineProcess(0), dead(false) {} + }; + + template + ProcessInfo* asNewProcess( const char* name, uint32_t ip, uint16_t port, LocalityData locality, ProcessClass startingClass, + Func func, const char* dataFolder, const char* coordinationFolder ) { + ProcessInfo* m = newProcess(name, ip, port, locality, startingClass, dataFolder, coordinationFolder); +// ProcessInfo* m = newProcess(name, ip, port, zoneId, machineId, dcId, startingClass, dataFolder, coordinationFolder); + std::swap(m, currentProcess); + try { + func(); + } catch (Error& e) { + TraceEvent(SevError, "NewMachineError").error(e); + killProcess(currentProcess, KillInstantly); + } catch (...) { + TraceEvent(SevError, "NewMachineError").error(unknown_error()); + killProcess(currentProcess, KillInstantly); + } + std::swap(m, currentProcess); + return m; + } + + ProcessInfo* getProcess( Endpoint const& endpoint ) { return getProcessByAddress(endpoint.address); } + ProcessInfo* getCurrentProcess() { return currentProcess; } + virtual Future onProcess( ISimulator::ProcessInfo *process, int taskID = -1 ) = 0; + virtual Future onMachine( ISimulator::ProcessInfo *process, int taskID = -1 ) = 0; + + virtual ProcessInfo* newProcess(const char* name, uint32_t ip, uint16_t port, LocalityData locality, ProcessClass startingClass, const char* dataFolder, const char* coordinationFolder) = 0; + virtual void killProcess( ProcessInfo* machine, KillType ) = 0; + virtual void rebootProcess(Optional> zoneId, bool allProcesses ) = 0; + virtual void rebootProcess( ProcessInfo* process, KillType kt ) = 0; + virtual void killInterface( NetworkAddress address, KillType ) = 0; + virtual bool killMachine(Optional> zoneId, KillType, bool killIsSafe = false, bool forceKill = false ) = 0; + virtual void killDataCenter(Optional> dcId, KillType ) = 0; + //virtual KillType getMachineKillState( UID zoneID ) = 0; + virtual bool canKillProcesses(std::vector const& availableProcesses, std::vector const& deadProcesses, KillType kt, KillType* newKillType) = 0; + + virtual void disableSwapToMachine(Optional> zoneId ) { + swapsDisabled.insert(zoneId); + } + virtual void enableSwapToMachine(Optional> zoneId ) { + swapsDisabled.erase(zoneId); + allSwapsDisabled = false; + } + virtual bool canSwapToMachine(Optional> zoneId ) { + return swapsDisabled.count( zoneId ) == 0 && !allSwapsDisabled && !extraDB; + } + virtual void enableSwapsToAll() { + swapsDisabled.clear(); + allSwapsDisabled = false; + } + virtual void disableSwapsToAll() { + swapsDisabled.clear(); + allSwapsDisabled = true; + } + + virtual void clogInterface( uint32_t ip, double seconds, ClogMode mode = ClogDefault ) = 0; + virtual void clogPair( uint32_t from, uint32_t to, double seconds ) = 0; + virtual std::vector getAllProcesses() = 0; + virtual ProcessInfo* getProcessByAddress( NetworkAddress const& address ) = 0; + virtual MachineInfo* getMachineByNetworkAddress(NetworkAddress const& address) = 0; + virtual MachineInfo* getMachineById(Optional> const& zoneId) = 0; + virtual void run() {} + virtual void destroyProcess( ProcessInfo *p ) = 0; + virtual void destroyMachine(Optional> const& zoneId ) = 0; + + // These are here for reasoning about whether it is possible to kill machines (or delete their data) + // and maintain the durability of the database. + int killedMachines; + int killableMachines; + int machinesNeededForProgress; + int desiredCoordinators; + int neededDatacenters; + int killedDatacenters; + int killableDatacenters; + int physicalDatacenters; + int maxCoordinatorsInDatacenter; + int processesPerMachine; + std::set protectedAddresses; + std::map currentlyRebootingProcesses; + class ClusterConnectionString* extraDB; + IRepPolicyRef storagePolicy; + IRepPolicyRef tLogPolicy; + int tLogWriteAntiQuorum; + + //Used by workloads that perform reconfigurations + int testerCount; + std::string connectionString; + + bool isStopped; + bool enableConnectionFailures; + bool speedUpSimulation; + BackupAgentType backupAgents; + + virtual flowGlobalType global(int id) { return getCurrentProcess()->global(id); }; + virtual void setGlobal(size_t id, flowGlobalType v) { getCurrentProcess()->setGlobal(id,v); }; + +protected: + static thread_local ProcessInfo* currentProcess; + Mutex mutex; + +private: + std::set>> swapsDisabled; + bool allSwapsDisabled; +}; + +// Quickly make existing code work that expects g_simulator to be of class type (not a pointer) +extern ISimulator* g_pSimulator; +#define g_simulator (*g_pSimulator) + +void startNewSimulator(); + +//Parameters used to simulate disk performance +struct DiskParameters : ReferenceCounted { + double nextOperation; + int64_t iops; + int64_t bandwidth; + + DiskParameters(int64_t iops, int64_t bandwidth) : nextOperation(0), iops(iops), bandwidth(bandwidth) { } +}; + +//Simulates delays for performing operations on disk +extern Future waitUntilDiskReady(Reference parameters, int64_t size, bool sync = false); + + +class Sim2FileSystem : public IAsyncFileSystem { +public: + virtual Future< Reference > open( std::string filename, int64_t flags, int64_t mode ); + // Opens a file for asynchronous I/O + + virtual Future< Void > deleteFile( std::string filename, bool mustBeDurable ); + // Deletes the given file. If mustBeDurable, returns only when the file is guaranteed to be deleted even after a power failure. + + Sim2FileSystem() {} + + virtual ~Sim2FileSystem() {} + + static void newFileSystem(); +}; + +#endif diff --git a/fdbrpc/zlib/adler32.c b/fdbrpc/zlib/adler32.c new file mode 100644 index 0000000000..a868f073d8 --- /dev/null +++ b/fdbrpc/zlib/adler32.c @@ -0,0 +1,179 @@ +/* adler32.c -- compute the Adler-32 checksum of a data stream + * Copyright (C) 1995-2011 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#include "zutil.h" + +#define local static + +local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2)); + +#define BASE 65521 /* largest prime smaller than 65536 */ +#define NMAX 5552 +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ + +#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} +#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); +#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); +#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); +#define DO16(buf) DO8(buf,0); DO8(buf,8); + +/* use NO_DIVIDE if your processor does not do division in hardware -- + try it both ways to see which is faster */ +#ifdef NO_DIVIDE +/* note that this assumes BASE is 65521, where 65536 % 65521 == 15 + (thank you to John Reiser for pointing this out) */ +# define CHOP(a) \ + do { \ + unsigned long tmp = a >> 16; \ + a &= 0xffffUL; \ + a += (tmp << 4) - tmp; \ + } while (0) +# define MOD28(a) \ + do { \ + CHOP(a); \ + if (a >= BASE) a -= BASE; \ + } while (0) +# define MOD(a) \ + do { \ + CHOP(a); \ + MOD28(a); \ + } while (0) +# define MOD63(a) \ + do { /* this assumes a is not negative */ \ + z_off64_t tmp = a >> 32; \ + a &= 0xffffffffL; \ + a += (tmp << 8) - (tmp << 5) + tmp; \ + tmp = a >> 16; \ + a &= 0xffffL; \ + a += (tmp << 4) - tmp; \ + tmp = a >> 16; \ + a &= 0xffffL; \ + a += (tmp << 4) - tmp; \ + if (a >= BASE) a -= BASE; \ + } while (0) +#else +# define MOD(a) a %= BASE +# define MOD28(a) a %= BASE +# define MOD63(a) a %= BASE +#endif + +/* ========================================================================= */ +uLong ZEXPORT adler32(adler, buf, len) + uLong adler; + const Bytef *buf; + uInt len; +{ + unsigned long sum2; + unsigned n; + + /* split Adler-32 into component sums */ + sum2 = (adler >> 16) & 0xffff; + adler &= 0xffff; + + /* in case user likes doing a byte at a time, keep it fast */ + if (len == 1) { + adler += buf[0]; + if (adler >= BASE) + adler -= BASE; + sum2 += adler; + if (sum2 >= BASE) + sum2 -= BASE; + return adler | (sum2 << 16); + } + + /* initial Adler-32 value (deferred check for len == 1 speed) */ + if (buf == Z_NULL) + return 1L; + + /* in case short lengths are provided, keep it somewhat fast */ + if (len < 16) { + while (len--) { + adler += *buf++; + sum2 += adler; + } + if (adler >= BASE) + adler -= BASE; + MOD28(sum2); /* only added so many BASE's */ + return adler | (sum2 << 16); + } + + /* do length NMAX blocks -- requires just one modulo operation */ + while (len >= NMAX) { + len -= NMAX; + n = NMAX / 16; /* NMAX is divisible by 16 */ + do { + DO16(buf); /* 16 sums unrolled */ + buf += 16; + } while (--n); + MOD(adler); + MOD(sum2); + } + + /* do remaining bytes (less than NMAX, still just one modulo) */ + if (len) { /* avoid modulos if none remaining */ + while (len >= 16) { + len -= 16; + DO16(buf); + buf += 16; + } + while (len--) { + adler += *buf++; + sum2 += adler; + } + MOD(adler); + MOD(sum2); + } + + /* return recombined sums */ + return adler | (sum2 << 16); +} + +/* ========================================================================= */ +local uLong adler32_combine_(adler1, adler2, len2) + uLong adler1; + uLong adler2; + z_off64_t len2; +{ + unsigned long sum1; + unsigned long sum2; + unsigned rem; + + /* for negative len, return invalid adler32 as a clue for debugging */ + if (len2 < 0) + return 0xffffffffUL; + + /* the derivation of this formula is left as an exercise for the reader */ + MOD63(len2); /* assumes len2 >= 0 */ + rem = (unsigned)len2; + sum1 = adler1 & 0xffff; + sum2 = rem * sum1; + MOD(sum2); + sum1 += (adler2 & 0xffff) + BASE - 1; + sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; + if (sum1 >= BASE) sum1 -= BASE; + if (sum1 >= BASE) sum1 -= BASE; + if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1); + if (sum2 >= BASE) sum2 -= BASE; + return sum1 | (sum2 << 16); +} + +/* ========================================================================= */ +uLong ZEXPORT adler32_combine(adler1, adler2, len2) + uLong adler1; + uLong adler2; + z_off_t len2; +{ + return adler32_combine_(adler1, adler2, len2); +} + +uLong ZEXPORT adler32_combine64(adler1, adler2, len2) + uLong adler1; + uLong adler2; + z_off64_t len2; +{ + return adler32_combine_(adler1, adler2, len2); +} diff --git a/fdbrpc/zlib/crc32.c b/fdbrpc/zlib/crc32.c new file mode 100644 index 0000000000..979a7190a3 --- /dev/null +++ b/fdbrpc/zlib/crc32.c @@ -0,0 +1,425 @@ +/* crc32.c -- compute the CRC-32 of a data stream + * Copyright (C) 1995-2006, 2010, 2011, 2012 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + * + * Thanks to Rodney Brown for his contribution of faster + * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing + * tables for updating the shift register in one step with three exclusive-ors + * instead of four steps with four exclusive-ors. This results in about a + * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. + */ + +/* @(#) $Id$ */ + +/* + Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore + protection on the static variables used to control the first-use generation + of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should + first call get_crc_table() to initialize the tables before allowing more than + one thread to use crc32(). + + DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. + */ + +#ifdef MAKECRCH +# include +# ifndef DYNAMIC_CRC_TABLE +# define DYNAMIC_CRC_TABLE +# endif /* !DYNAMIC_CRC_TABLE */ +#endif /* MAKECRCH */ + +#include "zutil.h" /* for STDC and FAR definitions */ + +#define local static + +/* Definitions for doing the crc four data bytes at a time. */ +#if !defined(NOBYFOUR) && defined(Z_U4) +# define BYFOUR +#endif +#ifdef BYFOUR + local unsigned long crc32_little OF((unsigned long, + const unsigned char FAR *, unsigned)); + local unsigned long crc32_big OF((unsigned long, + const unsigned char FAR *, unsigned)); +# define TBLS 8 +#else +# define TBLS 1 +#endif /* BYFOUR */ + +/* Local functions for crc concatenation */ +local unsigned long gf2_matrix_times OF((unsigned long *mat, + unsigned long vec)); +local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); +local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); + + +#ifdef DYNAMIC_CRC_TABLE + +local volatile int crc_table_empty = 1; +local z_crc_t FAR crc_table[TBLS][256]; +local void make_crc_table OF((void)); +#ifdef MAKECRCH + local void write_table OF((FILE *, const z_crc_t FAR *)); +#endif /* MAKECRCH */ +/* + Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: + x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. + + Polynomials over GF(2) are represented in binary, one bit per coefficient, + with the lowest powers in the most significant bit. Then adding polynomials + is just exclusive-or, and multiplying a polynomial by x is a right shift by + one. If we call the above polynomial p, and represent a byte as the + polynomial q, also with the lowest power in the most significant bit (so the + byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, + where a mod b means the remainder after dividing a by b. + + This calculation is done using the shift-register method of multiplying and + taking the remainder. The register is initialized to zero, and for each + incoming bit, x^32 is added mod p to the register if the bit is a one (where + x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by + x (which is shifting right by one and adding x^32 mod p if the bit shifted + out is a one). We start with the highest power (least significant bit) of + q and repeat for all eight bits of q. + + The first table is simply the CRC of all possible eight bit values. This is + all the information needed to generate CRCs on data a byte at a time for all + combinations of CRC register values and incoming bytes. The remaining tables + allow for word-at-a-time CRC calculation for both big-endian and little- + endian machines, where a word is four bytes. +*/ +local void make_crc_table() +{ + z_crc_t c; + int n, k; + z_crc_t poly; /* polynomial exclusive-or pattern */ + /* terms of polynomial defining this crc (except x^32): */ + static volatile int first = 1; /* flag to limit concurrent making */ + static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; + + /* See if another task is already doing this (not thread-safe, but better + than nothing -- significantly reduces duration of vulnerability in + case the advice about DYNAMIC_CRC_TABLE is ignored) */ + if (first) { + first = 0; + + /* make exclusive-or pattern from polynomial (0xedb88320UL) */ + poly = 0; + for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) + poly |= (z_crc_t)1 << (31 - p[n]); + + /* generate a crc for every 8-bit value */ + for (n = 0; n < 256; n++) { + c = (z_crc_t)n; + for (k = 0; k < 8; k++) + c = c & 1 ? poly ^ (c >> 1) : c >> 1; + crc_table[0][n] = c; + } + +#ifdef BYFOUR + /* generate crc for each value followed by one, two, and three zeros, + and then the byte reversal of those as well as the first table */ + for (n = 0; n < 256; n++) { + c = crc_table[0][n]; + crc_table[4][n] = ZSWAP32(c); + for (k = 1; k < 4; k++) { + c = crc_table[0][c & 0xff] ^ (c >> 8); + crc_table[k][n] = c; + crc_table[k + 4][n] = ZSWAP32(c); + } + } +#endif /* BYFOUR */ + + crc_table_empty = 0; + } + else { /* not first */ + /* wait for the other guy to finish (not efficient, but rare) */ + while (crc_table_empty) + ; + } + +#ifdef MAKECRCH + /* write out CRC tables to crc32.h */ + { + FILE *out; + + out = fopen("crc32.h", "w"); + if (out == NULL) return; + fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); + fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); + fprintf(out, "local const z_crc_t FAR "); + fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); + write_table(out, crc_table[0]); +# ifdef BYFOUR + fprintf(out, "#ifdef BYFOUR\n"); + for (k = 1; k < 8; k++) { + fprintf(out, " },\n {\n"); + write_table(out, crc_table[k]); + } + fprintf(out, "#endif\n"); +# endif /* BYFOUR */ + fprintf(out, " }\n};\n"); + fclose(out); + } +#endif /* MAKECRCH */ +} + +#ifdef MAKECRCH +local void write_table(out, table) + FILE *out; + const z_crc_t FAR *table; +{ + int n; + + for (n = 0; n < 256; n++) + fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", + (unsigned long)(table[n]), + n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); +} +#endif /* MAKECRCH */ + +#else /* !DYNAMIC_CRC_TABLE */ +/* ======================================================================== + * Tables of CRC-32s of all single-byte values, made by make_crc_table(). + */ +#include "crc32.h" +#endif /* DYNAMIC_CRC_TABLE */ + +/* ========================================================================= + * This function can be used by asm versions of crc32() + */ +const z_crc_t FAR * ZEXPORT get_crc_table() +{ +#ifdef DYNAMIC_CRC_TABLE + if (crc_table_empty) + make_crc_table(); +#endif /* DYNAMIC_CRC_TABLE */ + return (const z_crc_t FAR *)crc_table; +} + +/* ========================================================================= */ +#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) +#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 + +/* ========================================================================= */ +unsigned long ZEXPORT crc32(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + uInt len; +{ + if (buf == Z_NULL) return 0UL; + +#ifdef DYNAMIC_CRC_TABLE + if (crc_table_empty) + make_crc_table(); +#endif /* DYNAMIC_CRC_TABLE */ + +#ifdef BYFOUR + if (sizeof(void *) == sizeof(ptrdiff_t)) { + z_crc_t endian; + + endian = 1; + if (*((unsigned char *)(&endian))) + return crc32_little(crc, buf, len); + else + return crc32_big(crc, buf, len); + } +#endif /* BYFOUR */ + crc = crc ^ 0xffffffffUL; + while (len >= 8) { + DO8; + len -= 8; + } + if (len) do { + DO1; + } while (--len); + return crc ^ 0xffffffffUL; +} + +#ifdef BYFOUR + +/* ========================================================================= */ +#define DOLIT4 c ^= *buf4++; \ + c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ + crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] +#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 + +/* ========================================================================= */ +local unsigned long crc32_little(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + unsigned len; +{ + register z_crc_t c; + register const z_crc_t FAR *buf4; + + c = (z_crc_t)crc; + c = ~c; + while (len && ((ptrdiff_t)buf & 3)) { + c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); + len--; + } + + buf4 = (const z_crc_t FAR *)(const void FAR *)buf; + while (len >= 32) { + DOLIT32; + len -= 32; + } + while (len >= 4) { + DOLIT4; + len -= 4; + } + buf = (const unsigned char FAR *)buf4; + + if (len) do { + c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); + } while (--len); + c = ~c; + return (unsigned long)c; +} + +/* ========================================================================= */ +#define DOBIG4 c ^= *++buf4; \ + c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ + crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] +#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 + +/* ========================================================================= */ +local unsigned long crc32_big(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + unsigned len; +{ + register z_crc_t c; + register const z_crc_t FAR *buf4; + + c = ZSWAP32((z_crc_t)crc); + c = ~c; + while (len && ((ptrdiff_t)buf & 3)) { + c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); + len--; + } + + buf4 = (const z_crc_t FAR *)(const void FAR *)buf; + buf4--; + while (len >= 32) { + DOBIG32; + len -= 32; + } + while (len >= 4) { + DOBIG4; + len -= 4; + } + buf4++; + buf = (const unsigned char FAR *)buf4; + + if (len) do { + c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); + } while (--len); + c = ~c; + return (unsigned long)(ZSWAP32(c)); +} + +#endif /* BYFOUR */ + +#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ + +/* ========================================================================= */ +local unsigned long gf2_matrix_times(mat, vec) + unsigned long *mat; + unsigned long vec; +{ + unsigned long sum; + + sum = 0; + while (vec) { + if (vec & 1) + sum ^= *mat; + vec >>= 1; + mat++; + } + return sum; +} + +/* ========================================================================= */ +local void gf2_matrix_square(square, mat) + unsigned long *square; + unsigned long *mat; +{ + int n; + + for (n = 0; n < GF2_DIM; n++) + square[n] = gf2_matrix_times(mat, mat[n]); +} + +/* ========================================================================= */ +local uLong crc32_combine_(crc1, crc2, len2) + uLong crc1; + uLong crc2; + z_off64_t len2; +{ + int n; + unsigned long row; + unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ + unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ + + /* degenerate case (also disallow negative lengths) */ + if (len2 <= 0) + return crc1; + + /* put operator for one zero bit in odd */ + odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ + row = 1; + for (n = 1; n < GF2_DIM; n++) { + odd[n] = row; + row <<= 1; + } + + /* put operator for two zero bits in even */ + gf2_matrix_square(even, odd); + + /* put operator for four zero bits in odd */ + gf2_matrix_square(odd, even); + + /* apply len2 zeros to crc1 (first square will put the operator for one + zero byte, eight zero bits, in even) */ + do { + /* apply zeros operator for this bit of len2 */ + gf2_matrix_square(even, odd); + if (len2 & 1) + crc1 = gf2_matrix_times(even, crc1); + len2 >>= 1; + + /* if no more bits set, then done */ + if (len2 == 0) + break; + + /* another iteration of the loop with odd and even swapped */ + gf2_matrix_square(odd, even); + if (len2 & 1) + crc1 = gf2_matrix_times(odd, crc1); + len2 >>= 1; + + /* if no more bits set, then done */ + } while (len2 != 0); + + /* return combined crc */ + crc1 ^= crc2; + return crc1; +} + +/* ========================================================================= */ +uLong ZEXPORT crc32_combine(crc1, crc2, len2) + uLong crc1; + uLong crc2; + z_off_t len2; +{ + return crc32_combine_(crc1, crc2, len2); +} + +uLong ZEXPORT crc32_combine64(crc1, crc2, len2) + uLong crc1; + uLong crc2; + z_off64_t len2; +{ + return crc32_combine_(crc1, crc2, len2); +} diff --git a/fdbrpc/zlib/crc32.h b/fdbrpc/zlib/crc32.h new file mode 100644 index 0000000000..d39f76c755 --- /dev/null +++ b/fdbrpc/zlib/crc32.h @@ -0,0 +1,461 @@ +/* + * crc32.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* crc32.h -- tables for rapid CRC calculation + * Generated automatically by crc32.c + */ + +local const z_crc_t FAR crc_table[TBLS][256] = +{ + { + 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL, + 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL, + 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL, + 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL, + 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL, + 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL, + 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL, + 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL, + 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL, + 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL, + 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL, + 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL, + 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL, + 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL, + 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL, + 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL, + 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL, + 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL, + 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL, + 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL, + 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL, + 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL, + 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL, + 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL, + 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL, + 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL, + 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL, + 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL, + 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL, + 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL, + 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL, + 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL, + 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL, + 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL, + 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL, + 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL, + 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL, + 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL, + 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL, + 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL, + 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL, + 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL, + 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL, + 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL, + 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL, + 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL, + 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL, + 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL, + 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL, + 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, + 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, + 0x2d02ef8dUL +#ifdef BYFOUR + }, + { + 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL, + 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL, + 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL, + 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL, + 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL, + 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL, + 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL, + 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL, + 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL, + 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL, + 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL, + 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL, + 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL, + 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL, + 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL, + 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL, + 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL, + 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL, + 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL, + 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL, + 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL, + 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL, + 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL, + 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL, + 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL, + 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL, + 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL, + 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL, + 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL, + 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL, + 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL, + 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL, + 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL, + 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL, + 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL, + 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL, + 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL, + 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL, + 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL, + 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL, + 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL, + 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL, + 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL, + 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL, + 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL, + 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL, + 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL, + 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL, + 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL, + 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL, + 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL, + 0x9324fd72UL + }, + { + 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL, + 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL, + 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL, + 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL, + 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL, + 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL, + 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL, + 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL, + 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL, + 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL, + 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL, + 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL, + 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL, + 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL, + 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL, + 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL, + 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL, + 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL, + 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL, + 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL, + 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL, + 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL, + 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL, + 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL, + 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL, + 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL, + 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL, + 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL, + 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL, + 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL, + 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL, + 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL, + 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL, + 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL, + 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL, + 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL, + 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL, + 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL, + 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL, + 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL, + 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL, + 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL, + 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL, + 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL, + 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL, + 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL, + 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL, + 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL, + 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL, + 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL, + 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL, + 0xbe9834edUL + }, + { + 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL, + 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL, + 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL, + 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL, + 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL, + 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL, + 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL, + 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL, + 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL, + 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL, + 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL, + 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL, + 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL, + 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL, + 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL, + 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL, + 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL, + 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL, + 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL, + 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL, + 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL, + 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL, + 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL, + 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL, + 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL, + 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL, + 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL, + 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL, + 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL, + 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL, + 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL, + 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL, + 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL, + 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL, + 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL, + 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL, + 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL, + 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL, + 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL, + 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL, + 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL, + 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL, + 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL, + 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL, + 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL, + 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL, + 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL, + 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL, + 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL, + 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL, + 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL, + 0xde0506f1UL + }, + { + 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL, + 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL, + 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL, + 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL, + 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL, + 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL, + 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL, + 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL, + 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL, + 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL, + 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL, + 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL, + 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL, + 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL, + 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL, + 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL, + 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL, + 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL, + 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL, + 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL, + 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL, + 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL, + 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL, + 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL, + 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL, + 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL, + 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL, + 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL, + 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL, + 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL, + 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL, + 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL, + 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL, + 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL, + 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL, + 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL, + 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL, + 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL, + 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL, + 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL, + 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL, + 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL, + 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL, + 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL, + 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL, + 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL, + 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL, + 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL, + 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL, + 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL, + 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL, + 0x8def022dUL + }, + { + 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL, + 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL, + 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL, + 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL, + 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL, + 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL, + 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL, + 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL, + 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL, + 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL, + 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL, + 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL, + 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL, + 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL, + 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL, + 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL, + 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL, + 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL, + 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL, + 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL, + 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL, + 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL, + 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL, + 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL, + 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL, + 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL, + 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL, + 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL, + 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL, + 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL, + 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL, + 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL, + 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL, + 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL, + 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL, + 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL, + 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL, + 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL, + 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL, + 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL, + 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL, + 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL, + 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL, + 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL, + 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL, + 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL, + 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL, + 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL, + 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL, + 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL, + 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL, + 0x72fd2493UL + }, + { + 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL, + 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL, + 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL, + 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL, + 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL, + 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL, + 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL, + 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL, + 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL, + 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL, + 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL, + 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL, + 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL, + 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL, + 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL, + 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL, + 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL, + 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL, + 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL, + 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL, + 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL, + 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL, + 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL, + 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL, + 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL, + 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL, + 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL, + 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL, + 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL, + 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL, + 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL, + 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL, + 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL, + 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL, + 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL, + 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL, + 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL, + 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL, + 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL, + 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL, + 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL, + 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL, + 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL, + 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL, + 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL, + 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL, + 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL, + 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL, + 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL, + 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL, + 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL, + 0xed3498beUL + }, + { + 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL, + 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL, + 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL, + 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL, + 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL, + 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL, + 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL, + 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL, + 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL, + 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL, + 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL, + 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL, + 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL, + 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL, + 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL, + 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL, + 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL, + 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL, + 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL, + 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL, + 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL, + 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL, + 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL, + 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL, + 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL, + 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL, + 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL, + 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL, + 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL, + 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL, + 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL, + 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL, + 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL, + 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL, + 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL, + 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL, + 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL, + 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL, + 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL, + 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL, + 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL, + 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL, + 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL, + 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL, + 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL, + 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL, + 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL, + 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL, + 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL, + 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL, + 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL, + 0xf10605deUL +#endif + } +}; diff --git a/fdbrpc/zlib/deflate.c b/fdbrpc/zlib/deflate.c new file mode 100644 index 0000000000..696957705b --- /dev/null +++ b/fdbrpc/zlib/deflate.c @@ -0,0 +1,1967 @@ +/* deflate.c -- compress data using the deflation algorithm + * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + * ALGORITHM + * + * The "deflation" process depends on being able to identify portions + * of the input text which are identical to earlier input (within a + * sliding window trailing behind the input currently being processed). + * + * The most straightforward technique turns out to be the fastest for + * most input files: try all possible matches and select the longest. + * The key feature of this algorithm is that insertions into the string + * dictionary are very simple and thus fast, and deletions are avoided + * completely. Insertions are performed at each input character, whereas + * string matches are performed only when the previous match ends. So it + * is preferable to spend more time in matches to allow very fast string + * insertions and avoid deletions. The matching algorithm for small + * strings is inspired from that of Rabin & Karp. A brute force approach + * is used to find longer strings when a small match has been found. + * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze + * (by Leonid Broukhis). + * A previous version of this file used a more sophisticated algorithm + * (by Fiala and Greene) which is guaranteed to run in linear amortized + * time, but has a larger average cost, uses more memory and is patented. + * However the F&G algorithm may be faster for some highly redundant + * files if the parameter max_chain_length (described below) is too large. + * + * ACKNOWLEDGEMENTS + * + * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and + * I found it in 'freeze' written by Leonid Broukhis. + * Thanks to many people for bug reports and testing. + * + * REFERENCES + * + * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". + * Available in http://tools.ietf.org/html/rfc1951 + * + * A description of the Rabin and Karp algorithm is given in the book + * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. + * + * Fiala,E.R., and Greene,D.H. + * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 + * + */ + +/* @(#) $Id$ */ + +#include "deflate.h" + +const char deflate_copyright[] = + " deflate 1.2.8 Copyright 1995-2013 Jean-loup Gailly and Mark Adler "; +/* + If you use the zlib library in a product, an acknowledgment is welcome + in the documentation of your product. If for some reason you cannot + include such an acknowledgment, I would appreciate that you keep this + copyright string in the executable of your product. + */ + +/* =========================================================================== + * Function prototypes. + */ +typedef enum { + need_more, /* block not completed, need more input or more output */ + block_done, /* block flush performed */ + finish_started, /* finish started, need only more output at next deflate */ + finish_done /* finish done, accept no more input or output */ +} block_state; + +typedef block_state (*compress_func) OF((deflate_state *s, int flush)); +/* Compression function. Returns the block state after the call. */ + +local void fill_window OF((deflate_state *s)); +local block_state deflate_stored OF((deflate_state *s, int flush)); +local block_state deflate_fast OF((deflate_state *s, int flush)); +#ifndef FASTEST +local block_state deflate_slow OF((deflate_state *s, int flush)); +#endif +local block_state deflate_rle OF((deflate_state *s, int flush)); +local block_state deflate_huff OF((deflate_state *s, int flush)); +local void lm_init OF((deflate_state *s)); +local void putShortMSB OF((deflate_state *s, uInt b)); +local void flush_pending OF((z_streamp strm)); +local int read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); +#ifdef ASMV + void match_init OF((void)); /* asm code initialization */ + uInt longest_match OF((deflate_state *s, IPos cur_match)); +#else +local uInt longest_match OF((deflate_state *s, IPos cur_match)); +#endif + +#ifdef DEBUG +local void check_match OF((deflate_state *s, IPos start, IPos match, + int length)); +#endif + +/* =========================================================================== + * Local data + */ + +#define NIL 0 +/* Tail of hash chains */ + +#ifndef TOO_FAR +# define TOO_FAR 4096 +#endif +/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ + +/* Values for max_lazy_match, good_match and max_chain_length, depending on + * the desired pack level (0..9). The values given below have been tuned to + * exclude worst case performance for pathological files. Better values may be + * found for specific files. + */ +typedef struct config_s { + ush good_length; /* reduce lazy search above this match length */ + ush max_lazy; /* do not perform lazy search above this match length */ + ush nice_length; /* quit search above this match length */ + ush max_chain; + compress_func func; +} config; + +#ifdef FASTEST +local const config configuration_table[2] = { +/* good lazy nice chain */ +/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ +/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ +#else +local const config configuration_table[10] = { +/* good lazy nice chain */ +/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ +/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ +/* 2 */ {4, 5, 16, 8, deflate_fast}, +/* 3 */ {4, 6, 32, 32, deflate_fast}, + +/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ +/* 5 */ {8, 16, 32, 32, deflate_slow}, +/* 6 */ {8, 16, 128, 128, deflate_slow}, +/* 7 */ {8, 32, 128, 256, deflate_slow}, +/* 8 */ {32, 128, 258, 1024, deflate_slow}, +/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ +#endif + +/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 + * For deflate_fast() (levels <= 3) good is ignored and lazy has a different + * meaning. + */ + +#define EQUAL 0 +/* result of memcmp for equal strings */ + +#ifndef NO_DUMMY_DECL +struct static_tree_desc_s {int dummy;}; /* for buggy compilers */ +#endif + +/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ +#define RANK(f) (((f) << 1) - ((f) > 4 ? 9 : 0)) + +/* =========================================================================== + * Update a hash value with the given input byte + * IN assertion: all calls to to UPDATE_HASH are made with consecutive + * input characters, so that a running hash key can be computed from the + * previous key instead of complete recalculation each time. + */ +#define UPDATE_HASH(s,h,c) (h = (((h)<hash_shift) ^ (c)) & s->hash_mask) + + +/* =========================================================================== + * Insert string str in the dictionary and set match_head to the previous head + * of the hash chain (the most recent string with same hash key). Return + * the previous length of the hash chain. + * If this file is compiled with -DFASTEST, the compression level is forced + * to 1, and no hash chains are maintained. + * IN assertion: all calls to to INSERT_STRING are made with consecutive + * input characters and the first MIN_MATCH bytes of str are valid + * (except for the last MIN_MATCH-1 bytes of the input file). + */ +#ifdef FASTEST +#define INSERT_STRING(s, str, match_head) \ + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ + match_head = s->head[s->ins_h], \ + s->head[s->ins_h] = (Pos)(str)) +#else +#define INSERT_STRING(s, str, match_head) \ + (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ + match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ + s->head[s->ins_h] = (Pos)(str)) +#endif + +/* =========================================================================== + * Initialize the hash table (avoiding 64K overflow for 16 bit systems). + * prev[] will be initialized on the fly. + */ +#define CLEAR_HASH(s) \ + s->head[s->hash_size-1] = NIL; \ + zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); + +/* ========================================================================= */ +int ZEXPORT deflateInit_(strm, level, version, stream_size) + z_streamp strm; + int level; + const char *version; + int stream_size; +{ + return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, + Z_DEFAULT_STRATEGY, version, stream_size); + /* To do: ignore strm->next_in if we use it as window */ +} + +/* ========================================================================= */ +int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, + version, stream_size) + z_streamp strm; + int level; + int method; + int windowBits; + int memLevel; + int strategy; + const char *version; + int stream_size; +{ + deflate_state *s; + int wrap = 1; + static const char my_version[] = ZLIB_VERSION; + + ushf *overlay; + /* We overlay pending_buf and d_buf+l_buf. This works since the average + * output size for (length,distance) codes is <= 24 bits. + */ + + if (version == Z_NULL || version[0] != my_version[0] || + stream_size != sizeof(z_stream)) { + return Z_VERSION_ERROR; + } + if (strm == Z_NULL) return Z_STREAM_ERROR; + + strm->msg = Z_NULL; + if (strm->zalloc == (alloc_func)0) { +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zalloc = zcalloc; + strm->opaque = (voidpf)0; +#endif + } + if (strm->zfree == (free_func)0) +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zfree = zcfree; +#endif + +#ifdef FASTEST + if (level != 0) level = 1; +#else + if (level == Z_DEFAULT_COMPRESSION) level = 6; +#endif + + if (windowBits < 0) { /* suppress zlib wrapper */ + wrap = 0; + windowBits = -windowBits; + } +#ifdef GZIP + else if (windowBits > 15) { + wrap = 2; /* write gzip wrapper instead */ + windowBits -= 16; + } +#endif + if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || + windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || + strategy < 0 || strategy > Z_FIXED) { + return Z_STREAM_ERROR; + } + if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ + s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); + if (s == Z_NULL) return Z_MEM_ERROR; + strm->state = (struct internal_state FAR *)s; + s->strm = strm; + + s->wrap = wrap; + s->gzhead = Z_NULL; + s->w_bits = windowBits; + s->w_size = 1 << s->w_bits; + s->w_mask = s->w_size - 1; + + s->hash_bits = memLevel + 7; + s->hash_size = 1 << s->hash_bits; + s->hash_mask = s->hash_size - 1; + s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); + + s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); + s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); + s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); + + s->high_water = 0; /* nothing written to s->window yet */ + + s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ + + overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); + s->pending_buf = (uchf *) overlay; + s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); + + if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || + s->pending_buf == Z_NULL) { + s->status = FINISH_STATE; + strm->msg = ERR_MSG(Z_MEM_ERROR); + deflateEnd (strm); + return Z_MEM_ERROR; + } + s->d_buf = overlay + s->lit_bufsize/sizeof(ush); + s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; + + s->level = level; + s->strategy = strategy; + s->method = (Byte)method; + + return deflateReset(strm); +} + +/* ========================================================================= */ +int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) + z_streamp strm; + const Bytef *dictionary; + uInt dictLength; +{ + deflate_state *s; + uInt str, n; + int wrap; + unsigned avail; + z_const unsigned char *next; + + if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL) + return Z_STREAM_ERROR; + s = strm->state; + wrap = s->wrap; + if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) + return Z_STREAM_ERROR; + + /* when using zlib wrappers, compute Adler-32 for provided dictionary */ + if (wrap == 1) + strm->adler = adler32(strm->adler, dictionary, dictLength); + s->wrap = 0; /* avoid computing Adler-32 in read_buf */ + + /* if dictionary would fill window, just replace the history */ + if (dictLength >= s->w_size) { + if (wrap == 0) { /* already empty otherwise */ + CLEAR_HASH(s); + s->strstart = 0; + s->block_start = 0L; + s->insert = 0; + } + dictionary += dictLength - s->w_size; /* use the tail */ + dictLength = s->w_size; + } + + /* insert dictionary into window and hash */ + avail = strm->avail_in; + next = strm->next_in; + strm->avail_in = dictLength; + strm->next_in = (z_const Bytef *)dictionary; + fill_window(s); + while (s->lookahead >= MIN_MATCH) { + str = s->strstart; + n = s->lookahead - (MIN_MATCH-1); + do { + UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); +#ifndef FASTEST + s->prev[str & s->w_mask] = s->head[s->ins_h]; +#endif + s->head[s->ins_h] = (Pos)str; + str++; + } while (--n); + s->strstart = str; + s->lookahead = MIN_MATCH-1; + fill_window(s); + } + s->strstart += s->lookahead; + s->block_start = (long)s->strstart; + s->insert = s->lookahead; + s->lookahead = 0; + s->match_length = s->prev_length = MIN_MATCH-1; + s->match_available = 0; + strm->next_in = next; + strm->avail_in = avail; + s->wrap = wrap; + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateResetKeep (strm) + z_streamp strm; +{ + deflate_state *s; + + if (strm == Z_NULL || strm->state == Z_NULL || + strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) { + return Z_STREAM_ERROR; + } + + strm->total_in = strm->total_out = 0; + strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ + strm->data_type = Z_UNKNOWN; + + s = (deflate_state *)strm->state; + s->pending = 0; + s->pending_out = s->pending_buf; + + if (s->wrap < 0) { + s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ + } + s->status = s->wrap ? INIT_STATE : BUSY_STATE; + strm->adler = +#ifdef GZIP + s->wrap == 2 ? crc32(0L, Z_NULL, 0) : +#endif + adler32(0L, Z_NULL, 0); + s->last_flush = Z_NO_FLUSH; + + _tr_init(s); + + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateReset (strm) + z_streamp strm; +{ + int ret; + + ret = deflateResetKeep(strm); + if (ret == Z_OK) + lm_init(strm->state); + return ret; +} + +/* ========================================================================= */ +int ZEXPORT deflateSetHeader (strm, head) + z_streamp strm; + gz_headerp head; +{ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (strm->state->wrap != 2) return Z_STREAM_ERROR; + strm->state->gzhead = head; + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflatePending (strm, pending, bits) + unsigned *pending; + int *bits; + z_streamp strm; +{ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + if (pending != Z_NULL) + *pending = strm->state->pending; + if (bits != Z_NULL) + *bits = strm->state->bi_valid; + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflatePrime (strm, bits, value) + z_streamp strm; + int bits; + int value; +{ + deflate_state *s; + int put; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + s = strm->state; + if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) + return Z_BUF_ERROR; + do { + put = Buf_size - s->bi_valid; + if (put > bits) + put = bits; + s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); + s->bi_valid += put; + _tr_flush_bits(s); + value >>= put; + bits -= put; + } while (bits); + return Z_OK; +} + +/* ========================================================================= */ +int ZEXPORT deflateParams(strm, level, strategy) + z_streamp strm; + int level; + int strategy; +{ + deflate_state *s; + compress_func func; + int err = Z_OK; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + s = strm->state; + +#ifdef FASTEST + if (level != 0) level = 1; +#else + if (level == Z_DEFAULT_COMPRESSION) level = 6; +#endif + if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { + return Z_STREAM_ERROR; + } + func = configuration_table[s->level].func; + + if ((strategy != s->strategy || func != configuration_table[level].func) && + strm->total_in != 0) { + /* Flush the last buffer: */ + err = deflate(strm, Z_BLOCK); + if (err == Z_BUF_ERROR && s->pending == 0) + err = Z_OK; + } + if (s->level != level) { + s->level = level; + s->max_lazy_match = configuration_table[level].max_lazy; + s->good_match = configuration_table[level].good_length; + s->nice_match = configuration_table[level].nice_length; + s->max_chain_length = configuration_table[level].max_chain; + } + s->strategy = strategy; + return err; +} + +/* ========================================================================= */ +int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) + z_streamp strm; + int good_length; + int max_lazy; + int nice_length; + int max_chain; +{ + deflate_state *s; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + s = strm->state; + s->good_match = good_length; + s->max_lazy_match = max_lazy; + s->nice_match = nice_length; + s->max_chain_length = max_chain; + return Z_OK; +} + +/* ========================================================================= + * For the default windowBits of 15 and memLevel of 8, this function returns + * a close to exact, as well as small, upper bound on the compressed size. + * They are coded as constants here for a reason--if the #define's are + * changed, then this function needs to be changed as well. The return + * value for 15 and 8 only works for those exact settings. + * + * For any setting other than those defaults for windowBits and memLevel, + * the value returned is a conservative worst case for the maximum expansion + * resulting from using fixed blocks instead of stored blocks, which deflate + * can emit on compressed data for some combinations of the parameters. + * + * This function could be more sophisticated to provide closer upper bounds for + * every combination of windowBits and memLevel. But even the conservative + * upper bound of about 14% expansion does not seem onerous for output buffer + * allocation. + */ +uLong ZEXPORT deflateBound(strm, sourceLen) + z_streamp strm; + uLong sourceLen; +{ + deflate_state *s; + uLong complen, wraplen; + Bytef *str; + + /* conservative upper bound for compressed data */ + complen = sourceLen + + ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; + + /* if can't get parameters, return conservative bound plus zlib wrapper */ + if (strm == Z_NULL || strm->state == Z_NULL) + return complen + 6; + + /* compute wrapper length */ + s = strm->state; + switch (s->wrap) { + case 0: /* raw deflate */ + wraplen = 0; + break; + case 1: /* zlib wrapper */ + wraplen = 6 + (s->strstart ? 4 : 0); + break; + case 2: /* gzip wrapper */ + wraplen = 18; + if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ + if (s->gzhead->extra != Z_NULL) + wraplen += 2 + s->gzhead->extra_len; + str = s->gzhead->name; + if (str != Z_NULL) + do { + wraplen++; + } while (*str++); + str = s->gzhead->comment; + if (str != Z_NULL) + do { + wraplen++; + } while (*str++); + if (s->gzhead->hcrc) + wraplen += 2; + } + break; + default: /* for compiler happiness */ + wraplen = 6; + } + + /* if not default parameters, return conservative bound */ + if (s->w_bits != 15 || s->hash_bits != 8 + 7) + return complen + wraplen; + + /* default settings: return tight bound for that case */ + return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + + (sourceLen >> 25) + 13 - 6 + wraplen; +} + +/* ========================================================================= + * Put a short in the pending buffer. The 16-bit value is put in MSB order. + * IN assertion: the stream state is correct and there is enough room in + * pending_buf. + */ +local void putShortMSB (s, b) + deflate_state *s; + uInt b; +{ + put_byte(s, (Byte)(b >> 8)); + put_byte(s, (Byte)(b & 0xff)); +} + +/* ========================================================================= + * Flush as much pending output as possible. All deflate() output goes + * through this function so some applications may wish to modify it + * to avoid allocating a large strm->next_out buffer and copying into it. + * (See also read_buf()). + */ +local void flush_pending(strm) + z_streamp strm; +{ + unsigned len; + deflate_state *s = strm->state; + + _tr_flush_bits(s); + len = s->pending; + if (len > strm->avail_out) len = strm->avail_out; + if (len == 0) return; + + zmemcpy(strm->next_out, s->pending_out, len); + strm->next_out += len; + s->pending_out += len; + strm->total_out += len; + strm->avail_out -= len; + s->pending -= len; + if (s->pending == 0) { + s->pending_out = s->pending_buf; + } +} + +/* ========================================================================= */ +int ZEXPORT deflate (strm, flush) + z_streamp strm; + int flush; +{ + int old_flush; /* value of flush param for previous deflate call */ + deflate_state *s; + + if (strm == Z_NULL || strm->state == Z_NULL || + flush > Z_BLOCK || flush < 0) { + return Z_STREAM_ERROR; + } + s = strm->state; + + if (strm->next_out == Z_NULL || + (strm->next_in == Z_NULL && strm->avail_in != 0) || + (s->status == FINISH_STATE && flush != Z_FINISH)) { + ERR_RETURN(strm, Z_STREAM_ERROR); + } + if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); + + s->strm = strm; /* just in case */ + old_flush = s->last_flush; + s->last_flush = flush; + + /* Write the header */ + if (s->status == INIT_STATE) { +#ifdef GZIP + if (s->wrap == 2) { + strm->adler = crc32(0L, Z_NULL, 0); + put_byte(s, 31); + put_byte(s, 139); + put_byte(s, 8); + if (s->gzhead == Z_NULL) { + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, 0); + put_byte(s, s->level == 9 ? 2 : + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? + 4 : 0)); + put_byte(s, OS_CODE); + s->status = BUSY_STATE; + } + else { + put_byte(s, (s->gzhead->text ? 1 : 0) + + (s->gzhead->hcrc ? 2 : 0) + + (s->gzhead->extra == Z_NULL ? 0 : 4) + + (s->gzhead->name == Z_NULL ? 0 : 8) + + (s->gzhead->comment == Z_NULL ? 0 : 16) + ); + put_byte(s, (Byte)(s->gzhead->time & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); + put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); + put_byte(s, s->level == 9 ? 2 : + (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? + 4 : 0)); + put_byte(s, s->gzhead->os & 0xff); + if (s->gzhead->extra != Z_NULL) { + put_byte(s, s->gzhead->extra_len & 0xff); + put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); + } + if (s->gzhead->hcrc) + strm->adler = crc32(strm->adler, s->pending_buf, + s->pending); + s->gzindex = 0; + s->status = EXTRA_STATE; + } + } + else +#endif + { + uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; + uInt level_flags; + + if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) + level_flags = 0; + else if (s->level < 6) + level_flags = 1; + else if (s->level == 6) + level_flags = 2; + else + level_flags = 3; + header |= (level_flags << 6); + if (s->strstart != 0) header |= PRESET_DICT; + header += 31 - (header % 31); + + s->status = BUSY_STATE; + putShortMSB(s, header); + + /* Save the adler32 of the preset dictionary: */ + if (s->strstart != 0) { + putShortMSB(s, (uInt)(strm->adler >> 16)); + putShortMSB(s, (uInt)(strm->adler & 0xffff)); + } + strm->adler = adler32(0L, Z_NULL, 0); + } + } +#ifdef GZIP + if (s->status == EXTRA_STATE) { + if (s->gzhead->extra != Z_NULL) { + uInt beg = s->pending; /* start of bytes to update crc */ + + while (s->gzindex < (s->gzhead->extra_len & 0xffff)) { + if (s->pending == s->pending_buf_size) { + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + flush_pending(strm); + beg = s->pending; + if (s->pending == s->pending_buf_size) + break; + } + put_byte(s, s->gzhead->extra[s->gzindex]); + s->gzindex++; + } + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + if (s->gzindex == s->gzhead->extra_len) { + s->gzindex = 0; + s->status = NAME_STATE; + } + } + else + s->status = NAME_STATE; + } + if (s->status == NAME_STATE) { + if (s->gzhead->name != Z_NULL) { + uInt beg = s->pending; /* start of bytes to update crc */ + int val; + + do { + if (s->pending == s->pending_buf_size) { + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + flush_pending(strm); + beg = s->pending; + if (s->pending == s->pending_buf_size) { + val = 1; + break; + } + } + val = s->gzhead->name[s->gzindex++]; + put_byte(s, val); + } while (val != 0); + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + if (val == 0) { + s->gzindex = 0; + s->status = COMMENT_STATE; + } + } + else + s->status = COMMENT_STATE; + } + if (s->status == COMMENT_STATE) { + if (s->gzhead->comment != Z_NULL) { + uInt beg = s->pending; /* start of bytes to update crc */ + int val; + + do { + if (s->pending == s->pending_buf_size) { + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + flush_pending(strm); + beg = s->pending; + if (s->pending == s->pending_buf_size) { + val = 1; + break; + } + } + val = s->gzhead->comment[s->gzindex++]; + put_byte(s, val); + } while (val != 0); + if (s->gzhead->hcrc && s->pending > beg) + strm->adler = crc32(strm->adler, s->pending_buf + beg, + s->pending - beg); + if (val == 0) + s->status = HCRC_STATE; + } + else + s->status = HCRC_STATE; + } + if (s->status == HCRC_STATE) { + if (s->gzhead->hcrc) { + if (s->pending + 2 > s->pending_buf_size) + flush_pending(strm); + if (s->pending + 2 <= s->pending_buf_size) { + put_byte(s, (Byte)(strm->adler & 0xff)); + put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); + strm->adler = crc32(0L, Z_NULL, 0); + s->status = BUSY_STATE; + } + } + else + s->status = BUSY_STATE; + } +#endif + + /* Flush as much pending output as possible */ + if (s->pending != 0) { + flush_pending(strm); + if (strm->avail_out == 0) { + /* Since avail_out is 0, deflate will be called again with + * more output space, but possibly with both pending and + * avail_in equal to zero. There won't be anything to do, + * but this is not an error situation so make sure we + * return OK instead of BUF_ERROR at next call of deflate: + */ + s->last_flush = -1; + return Z_OK; + } + + /* Make sure there is something to do and avoid duplicate consecutive + * flushes. For repeated and useless calls with Z_FINISH, we keep + * returning Z_STREAM_END instead of Z_BUF_ERROR. + */ + } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && + flush != Z_FINISH) { + ERR_RETURN(strm, Z_BUF_ERROR); + } + + /* User must not provide more input after the first FINISH: */ + if (s->status == FINISH_STATE && strm->avail_in != 0) { + ERR_RETURN(strm, Z_BUF_ERROR); + } + + /* Start a new block or continue the current one. + */ + if (strm->avail_in != 0 || s->lookahead != 0 || + (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { + block_state bstate; + + bstate = s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : + (s->strategy == Z_RLE ? deflate_rle(s, flush) : + (*(configuration_table[s->level].func))(s, flush)); + + if (bstate == finish_started || bstate == finish_done) { + s->status = FINISH_STATE; + } + if (bstate == need_more || bstate == finish_started) { + if (strm->avail_out == 0) { + s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ + } + return Z_OK; + /* If flush != Z_NO_FLUSH && avail_out == 0, the next call + * of deflate should use the same flush parameter to make sure + * that the flush is complete. So we don't have to output an + * empty block here, this will be done at next call. This also + * ensures that for a very small output buffer, we emit at most + * one empty block. + */ + } + if (bstate == block_done) { + if (flush == Z_PARTIAL_FLUSH) { + _tr_align(s); + } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ + _tr_stored_block(s, (char*)0, 0L, 0); + /* For a full flush, this empty block will be recognized + * as a special marker by inflate_sync(). + */ + if (flush == Z_FULL_FLUSH) { + CLEAR_HASH(s); /* forget history */ + if (s->lookahead == 0) { + s->strstart = 0; + s->block_start = 0L; + s->insert = 0; + } + } + } + flush_pending(strm); + if (strm->avail_out == 0) { + s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ + return Z_OK; + } + } + } + Assert(strm->avail_out > 0, "bug2"); + + if (flush != Z_FINISH) return Z_OK; + if (s->wrap <= 0) return Z_STREAM_END; + + /* Write the trailer */ +#ifdef GZIP + if (s->wrap == 2) { + put_byte(s, (Byte)(strm->adler & 0xff)); + put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); + put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); + put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); + put_byte(s, (Byte)(strm->total_in & 0xff)); + put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); + put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); + put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); + } + else +#endif + { + putShortMSB(s, (uInt)(strm->adler >> 16)); + putShortMSB(s, (uInt)(strm->adler & 0xffff)); + } + flush_pending(strm); + /* If avail_out is zero, the application will call deflate again + * to flush the rest. + */ + if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ + return s->pending != 0 ? Z_OK : Z_STREAM_END; +} + +/* ========================================================================= */ +int ZEXPORT deflateEnd (strm) + z_streamp strm; +{ + int status; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + + status = strm->state->status; + if (status != INIT_STATE && + status != EXTRA_STATE && + status != NAME_STATE && + status != COMMENT_STATE && + status != HCRC_STATE && + status != BUSY_STATE && + status != FINISH_STATE) { + return Z_STREAM_ERROR; + } + + /* Deallocate in reverse order of allocations: */ + TRY_FREE(strm, strm->state->pending_buf); + TRY_FREE(strm, strm->state->head); + TRY_FREE(strm, strm->state->prev); + TRY_FREE(strm, strm->state->window); + + ZFREE(strm, strm->state); + strm->state = Z_NULL; + + return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; +} + +/* ========================================================================= + * Copy the source state to the destination state. + * To simplify the source, this is not supported for 16-bit MSDOS (which + * doesn't have enough memory anyway to duplicate compression states). + */ +int ZEXPORT deflateCopy (dest, source) + z_streamp dest; + z_streamp source; +{ +#ifdef MAXSEG_64K + return Z_STREAM_ERROR; +#else + deflate_state *ds; + deflate_state *ss; + ushf *overlay; + + + if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL) { + return Z_STREAM_ERROR; + } + + ss = source->state; + + zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); + + ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); + if (ds == Z_NULL) return Z_MEM_ERROR; + dest->state = (struct internal_state FAR *) ds; + zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); + ds->strm = dest; + + ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); + ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); + ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); + overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); + ds->pending_buf = (uchf *) overlay; + + if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || + ds->pending_buf == Z_NULL) { + deflateEnd (dest); + return Z_MEM_ERROR; + } + /* following zmemcpy do not work for 16-bit MSDOS */ + zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); + zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); + zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); + zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); + + ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); + ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); + ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; + + ds->l_desc.dyn_tree = ds->dyn_ltree; + ds->d_desc.dyn_tree = ds->dyn_dtree; + ds->bl_desc.dyn_tree = ds->bl_tree; + + return Z_OK; +#endif /* MAXSEG_64K */ +} + +/* =========================================================================== + * Read a new buffer from the current input stream, update the adler32 + * and total number of bytes read. All deflate() input goes through + * this function so some applications may wish to modify it to avoid + * allocating a large strm->next_in buffer and copying from it. + * (See also flush_pending()). + */ +local int read_buf(strm, buf, size) + z_streamp strm; + Bytef *buf; + unsigned size; +{ + unsigned len = strm->avail_in; + + if (len > size) len = size; + if (len == 0) return 0; + + strm->avail_in -= len; + + zmemcpy(buf, strm->next_in, len); + if (strm->state->wrap == 1) { + strm->adler = adler32(strm->adler, buf, len); + } +#ifdef GZIP + else if (strm->state->wrap == 2) { + strm->adler = crc32(strm->adler, buf, len); + } +#endif + strm->next_in += len; + strm->total_in += len; + + return (int)len; +} + +/* =========================================================================== + * Initialize the "longest match" routines for a new zlib stream + */ +local void lm_init (s) + deflate_state *s; +{ + s->window_size = (ulg)2L*s->w_size; + + CLEAR_HASH(s); + + /* Set the default configuration parameters: + */ + s->max_lazy_match = configuration_table[s->level].max_lazy; + s->good_match = configuration_table[s->level].good_length; + s->nice_match = configuration_table[s->level].nice_length; + s->max_chain_length = configuration_table[s->level].max_chain; + + s->strstart = 0; + s->block_start = 0L; + s->lookahead = 0; + s->insert = 0; + s->match_length = s->prev_length = MIN_MATCH-1; + s->match_available = 0; + s->ins_h = 0; +#ifndef FASTEST +#ifdef ASMV + match_init(); /* initialize the asm code */ +#endif +#endif +} + +#ifndef FASTEST +/* =========================================================================== + * Set match_start to the longest match starting at the given string and + * return its length. Matches shorter or equal to prev_length are discarded, + * in which case the result is equal to prev_length and match_start is + * garbage. + * IN assertions: cur_match is the head of the hash chain for the current + * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 + * OUT assertion: the match length is not greater than s->lookahead. + */ +#ifndef ASMV +/* For 80x86 and 680x0, an optimized version will be provided in match.asm or + * match.S. The code will be functionally equivalent. + */ +local uInt longest_match(s, cur_match) + deflate_state *s; + IPos cur_match; /* current match */ +{ + unsigned chain_length = s->max_chain_length;/* max hash chain length */ + register Bytef *scan = s->window + s->strstart; /* current string */ + register Bytef *match; /* matched string */ + register int len; /* length of current match */ + int best_len = s->prev_length; /* best match length so far */ + int nice_match = s->nice_match; /* stop if match long enough */ + IPos limit = s->strstart > (IPos)MAX_DIST(s) ? + s->strstart - (IPos)MAX_DIST(s) : NIL; + /* Stop when cur_match becomes <= limit. To simplify the code, + * we prevent matches with the string of window index 0. + */ + Posf *prev = s->prev; + uInt wmask = s->w_mask; + +#ifdef UNALIGNED_OK + /* Compare two bytes at a time. Note: this is not always beneficial. + * Try with and without -DUNALIGNED_OK to check. + */ + register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; + register ush scan_start = *(ushf*)scan; + register ush scan_end = *(ushf*)(scan+best_len-1); +#else + register Bytef *strend = s->window + s->strstart + MAX_MATCH; + register Byte scan_end1 = scan[best_len-1]; + register Byte scan_end = scan[best_len]; +#endif + + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. + * It is easy to get rid of this optimization if necessary. + */ + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); + + /* Do not waste too much time if we already have a good match: */ + if (s->prev_length >= s->good_match) { + chain_length >>= 2; + } + /* Do not look for matches beyond the end of the input. This is necessary + * to make deflate deterministic. + */ + if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead; + + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + + do { + Assert(cur_match < s->strstart, "no future"); + match = s->window + cur_match; + + /* Skip to next match if the match length cannot increase + * or if the match length is less than 2. Note that the checks below + * for insufficient lookahead only occur occasionally for performance + * reasons. Therefore uninitialized memory will be accessed, and + * conditional jumps will be made that depend on those values. + * However the length of the match is limited to the lookahead, so + * the output of deflate is not affected by the uninitialized values. + */ +#if (defined(UNALIGNED_OK) && MAX_MATCH == 258) + /* This code assumes sizeof(unsigned short) == 2. Do not use + * UNALIGNED_OK if your compiler uses a different size. + */ + if (*(ushf*)(match+best_len-1) != scan_end || + *(ushf*)match != scan_start) continue; + + /* It is not necessary to compare scan[2] and match[2] since they are + * always equal when the other bytes match, given that the hash keys + * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at + * strstart+3, +5, ... up to strstart+257. We check for insufficient + * lookahead only every 4th comparison; the 128th check will be made + * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is + * necessary to put more guard bytes at the end of the window, or + * to check more often for insufficient lookahead. + */ + Assert(scan[2] == match[2], "scan[2]?"); + scan++, match++; + do { + } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + *(ushf*)(scan+=2) == *(ushf*)(match+=2) && + scan < strend); + /* The funny "do {}" generates better code on most compilers */ + + /* Here, scan <= window+strstart+257 */ + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + if (*scan == *match) scan++; + + len = (MAX_MATCH - 1) - (int)(strend-scan); + scan = strend - (MAX_MATCH-1); + +#else /* UNALIGNED_OK */ + + if (match[best_len] != scan_end || + match[best_len-1] != scan_end1 || + *match != *scan || + *++match != scan[1]) continue; + + /* The check at best_len-1 can be removed because it will be made + * again later. (This heuristic is not always a win.) + * It is not necessary to compare scan[2] and match[2] since they + * are always equal when the other bytes match, given that + * the hash keys are equal and that HASH_BITS >= 8. + */ + scan += 2, match++; + Assert(*scan == *match, "match[2]?"); + + /* We check for insufficient lookahead only every 8th comparison; + * the 256th check will be made at strstart+258. + */ + do { + } while (*++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + scan < strend); + + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + + len = MAX_MATCH - (int)(strend - scan); + scan = strend - MAX_MATCH; + +#endif /* UNALIGNED_OK */ + + if (len > best_len) { + s->match_start = cur_match; + best_len = len; + if (len >= nice_match) break; +#ifdef UNALIGNED_OK + scan_end = *(ushf*)(scan+best_len-1); +#else + scan_end1 = scan[best_len-1]; + scan_end = scan[best_len]; +#endif + } + } while ((cur_match = prev[cur_match & wmask]) > limit + && --chain_length != 0); + + if ((uInt)best_len <= s->lookahead) return (uInt)best_len; + return s->lookahead; +} +#endif /* ASMV */ + +#else /* FASTEST */ + +/* --------------------------------------------------------------------------- + * Optimized version for FASTEST only + */ +local uInt longest_match(s, cur_match) + deflate_state *s; + IPos cur_match; /* current match */ +{ + register Bytef *scan = s->window + s->strstart; /* current string */ + register Bytef *match; /* matched string */ + register int len; /* length of current match */ + register Bytef *strend = s->window + s->strstart + MAX_MATCH; + + /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. + * It is easy to get rid of this optimization if necessary. + */ + Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); + + Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); + + Assert(cur_match < s->strstart, "no future"); + + match = s->window + cur_match; + + /* Return failure if the match length is less than 2: + */ + if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; + + /* The check at best_len-1 can be removed because it will be made + * again later. (This heuristic is not always a win.) + * It is not necessary to compare scan[2] and match[2] since they + * are always equal when the other bytes match, given that + * the hash keys are equal and that HASH_BITS >= 8. + */ + scan += 2, match += 2; + Assert(*scan == *match, "match[2]?"); + + /* We check for insufficient lookahead only every 8th comparison; + * the 256th check will be made at strstart+258. + */ + do { + } while (*++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + *++scan == *++match && *++scan == *++match && + scan < strend); + + Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); + + len = MAX_MATCH - (int)(strend - scan); + + if (len < MIN_MATCH) return MIN_MATCH - 1; + + s->match_start = cur_match; + return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; +} + +#endif /* FASTEST */ + +#ifdef DEBUG +/* =========================================================================== + * Check that the match at match_start is indeed a match. + */ +local void check_match(s, start, match, length) + deflate_state *s; + IPos start, match; + int length; +{ + /* check that the match is indeed a match */ + if (zmemcmp(s->window + match, + s->window + start, length) != EQUAL) { + fprintf(stderr, " start %u, match %u, length %d\n", + start, match, length); + do { + fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); + } while (--length != 0); + z_error("invalid match"); + } + if (z_verbose > 1) { + fprintf(stderr,"\\[%d,%d]", start-match, length); + do { putc(s->window[start++], stderr); } while (--length != 0); + } +} +#else +# define check_match(s, start, match, length) +#endif /* DEBUG */ + +/* =========================================================================== + * Fill the window when the lookahead becomes insufficient. + * Updates strstart and lookahead. + * + * IN assertion: lookahead < MIN_LOOKAHEAD + * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD + * At least one byte has been read, or avail_in == 0; reads are + * performed for at least two bytes (required for the zip translate_eol + * option -- not supported here). + */ +local void fill_window(s) + deflate_state *s; +{ + register unsigned n, m; + register Posf *p; + unsigned more; /* Amount of free space at the end of the window. */ + uInt wsize = s->w_size; + + Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); + + do { + more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); + + /* Deal with !@#$% 64K limit: */ + if (sizeof(int) <= 2) { + if (more == 0 && s->strstart == 0 && s->lookahead == 0) { + more = wsize; + + } else if (more == (unsigned)(-1)) { + /* Very unlikely, but possible on 16 bit machine if + * strstart == 0 && lookahead == 1 (input done a byte at time) + */ + more--; + } + } + + /* If the window is almost full and there is insufficient lookahead, + * move the upper half to the lower one to make room in the upper half. + */ + if (s->strstart >= wsize+MAX_DIST(s)) { + + zmemcpy(s->window, s->window+wsize, (unsigned)wsize); + s->match_start -= wsize; + s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ + s->block_start -= (long) wsize; + + /* Slide the hash table (could be avoided with 32 bit values + at the expense of memory usage). We slide even when level == 0 + to keep the hash table consistent if we switch back to level > 0 + later. (Using level 0 permanently is not an optimal usage of + zlib, so we don't care about this pathological case.) + */ + n = s->hash_size; + p = &s->head[n]; + do { + m = *--p; + *p = (Pos)(m >= wsize ? m-wsize : NIL); + } while (--n); + + n = wsize; +#ifndef FASTEST + p = &s->prev[n]; + do { + m = *--p; + *p = (Pos)(m >= wsize ? m-wsize : NIL); + /* If n is not on any hash chain, prev[n] is garbage but + * its value will never be used. + */ + } while (--n); +#endif + more += wsize; + } + if (s->strm->avail_in == 0) break; + + /* If there was no sliding: + * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && + * more == window_size - lookahead - strstart + * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) + * => more >= window_size - 2*WSIZE + 2 + * In the BIG_MEM or MMAP case (not yet supported), + * window_size == input_size + MIN_LOOKAHEAD && + * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. + * Otherwise, window_size == 2*WSIZE so more >= 2. + * If there was sliding, more >= WSIZE. So in all cases, more >= 2. + */ + Assert(more >= 2, "more < 2"); + + n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); + s->lookahead += n; + + /* Initialize the hash value now that we have some input: */ + if (s->lookahead + s->insert >= MIN_MATCH) { + uInt str = s->strstart - s->insert; + s->ins_h = s->window[str]; + UPDATE_HASH(s, s->ins_h, s->window[str + 1]); +#if MIN_MATCH != 3 + Call UPDATE_HASH() MIN_MATCH-3 more times +#endif + while (s->insert) { + UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); +#ifndef FASTEST + s->prev[str & s->w_mask] = s->head[s->ins_h]; +#endif + s->head[s->ins_h] = (Pos)str; + str++; + s->insert--; + if (s->lookahead + s->insert < MIN_MATCH) + break; + } + } + /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, + * but this is not important since only literal bytes will be emitted. + */ + + } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); + + /* If the WIN_INIT bytes after the end of the current data have never been + * written, then zero those bytes in order to avoid memory check reports of + * the use of uninitialized (or uninitialised as Julian writes) bytes by + * the longest match routines. Update the high water mark for the next + * time through here. WIN_INIT is set to MAX_MATCH since the longest match + * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. + */ + if (s->high_water < s->window_size) { + ulg curr = s->strstart + (ulg)(s->lookahead); + ulg init; + + if (s->high_water < curr) { + /* Previous high water mark below current data -- zero WIN_INIT + * bytes or up to end of window, whichever is less. + */ + init = s->window_size - curr; + if (init > WIN_INIT) + init = WIN_INIT; + zmemzero(s->window + curr, (unsigned)init); + s->high_water = curr + init; + } + else if (s->high_water < (ulg)curr + WIN_INIT) { + /* High water mark at or above current data, but below current data + * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up + * to end of window, whichever is less. + */ + init = (ulg)curr + WIN_INIT - s->high_water; + if (init > s->window_size - s->high_water) + init = s->window_size - s->high_water; + zmemzero(s->window + s->high_water, (unsigned)init); + s->high_water += init; + } + } + + Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, + "not enough room for search"); +} + +/* =========================================================================== + * Flush the current block, with given end-of-file flag. + * IN assertion: strstart is set to the end of the current match. + */ +#define FLUSH_BLOCK_ONLY(s, last) { \ + _tr_flush_block(s, (s->block_start >= 0L ? \ + (charf *)&s->window[(unsigned)s->block_start] : \ + (charf *)Z_NULL), \ + (ulg)((long)s->strstart - s->block_start), \ + (last)); \ + s->block_start = s->strstart; \ + flush_pending(s->strm); \ + Tracev((stderr,"[FLUSH]")); \ +} + +/* Same but force premature exit if necessary. */ +#define FLUSH_BLOCK(s, last) { \ + FLUSH_BLOCK_ONLY(s, last); \ + if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ +} + +/* =========================================================================== + * Copy without compression as much as possible from the input stream, return + * the current block state. + * This function does not insert new strings in the dictionary since + * uncompressible data is probably not useful. This function is used + * only for the level=0 compression option. + * NOTE: this function should be optimized to avoid extra copying from + * window to pending_buf. + */ +local block_state deflate_stored(s, flush) + deflate_state *s; + int flush; +{ + /* Stored blocks are limited to 0xffff bytes, pending_buf is limited + * to pending_buf_size, and each stored block has a 5 byte header: + */ + ulg max_block_size = 0xffff; + ulg max_start; + + if (max_block_size > s->pending_buf_size - 5) { + max_block_size = s->pending_buf_size - 5; + } + + /* Copy as much as possible from input to output: */ + for (;;) { + /* Fill the window as much as possible: */ + if (s->lookahead <= 1) { + + Assert(s->strstart < s->w_size+MAX_DIST(s) || + s->block_start >= (long)s->w_size, "slide too late"); + + fill_window(s); + if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more; + + if (s->lookahead == 0) break; /* flush the current block */ + } + Assert(s->block_start >= 0L, "block gone"); + + s->strstart += s->lookahead; + s->lookahead = 0; + + /* Emit a stored block if pending_buf will be full: */ + max_start = s->block_start + max_block_size; + if (s->strstart == 0 || (ulg)s->strstart >= max_start) { + /* strstart == 0 is possible when wraparound on 16-bit machine */ + s->lookahead = (uInt)(s->strstart - max_start); + s->strstart = (uInt)max_start; + FLUSH_BLOCK(s, 0); + } + /* Flush if we may have to slide, otherwise block_start may become + * negative and the data will be gone: + */ + if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) { + FLUSH_BLOCK(s, 0); + } + } + s->insert = 0; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if ((long)s->strstart > s->block_start) + FLUSH_BLOCK(s, 0); + return block_done; +} + +/* =========================================================================== + * Compress as much as possible from the input stream, return the current + * block state. + * This function does not perform lazy evaluation of matches and inserts + * new strings in the dictionary only for unmatched strings or for short + * matches. It is used only for the fast compression options. + */ +local block_state deflate_fast(s, flush) + deflate_state *s; + int flush; +{ + IPos hash_head; /* head of the hash chain */ + int bflush; /* set if current block must be flushed */ + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the next match, plus MIN_MATCH bytes to insert the + * string following the next match. + */ + if (s->lookahead < MIN_LOOKAHEAD) { + fill_window(s); + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { + return need_more; + } + if (s->lookahead == 0) break; /* flush the current block */ + } + + /* Insert the string window[strstart .. strstart+2] in the + * dictionary, and set hash_head to the head of the hash chain: + */ + hash_head = NIL; + if (s->lookahead >= MIN_MATCH) { + INSERT_STRING(s, s->strstart, hash_head); + } + + /* Find the longest match, discarding those <= prev_length. + * At this point we have always match_length < MIN_MATCH + */ + if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { + /* To simplify the code, we prevent matches with the string + * of window index 0 (in particular we have to avoid a match + * of the string with itself at the start of the input file). + */ + s->match_length = longest_match (s, hash_head); + /* longest_match() sets match_start */ + } + if (s->match_length >= MIN_MATCH) { + check_match(s, s->strstart, s->match_start, s->match_length); + + _tr_tally_dist(s, s->strstart - s->match_start, + s->match_length - MIN_MATCH, bflush); + + s->lookahead -= s->match_length; + + /* Insert new strings in the hash table only if the match length + * is not too large. This saves time but degrades compression. + */ +#ifndef FASTEST + if (s->match_length <= s->max_insert_length && + s->lookahead >= MIN_MATCH) { + s->match_length--; /* string at strstart already in table */ + do { + s->strstart++; + INSERT_STRING(s, s->strstart, hash_head); + /* strstart never exceeds WSIZE-MAX_MATCH, so there are + * always MIN_MATCH bytes ahead. + */ + } while (--s->match_length != 0); + s->strstart++; + } else +#endif + { + s->strstart += s->match_length; + s->match_length = 0; + s->ins_h = s->window[s->strstart]; + UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); +#if MIN_MATCH != 3 + Call UPDATE_HASH() MIN_MATCH-3 more times +#endif + /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not + * matter since it will be recomputed at next deflate call. + */ + } + } else { + /* No match, output a literal byte */ + Tracevv((stderr,"%c", s->window[s->strstart])); + _tr_tally_lit (s, s->window[s->strstart], bflush); + s->lookahead--; + s->strstart++; + } + if (bflush) FLUSH_BLOCK(s, 0); + } + s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->last_lit) + FLUSH_BLOCK(s, 0); + return block_done; +} + +#ifndef FASTEST +/* =========================================================================== + * Same as above, but achieves better compression. We use a lazy + * evaluation for matches: a match is finally adopted only if there is + * no better match at the next window position. + */ +local block_state deflate_slow(s, flush) + deflate_state *s; + int flush; +{ + IPos hash_head; /* head of hash chain */ + int bflush; /* set if current block must be flushed */ + + /* Process the input block. */ + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the next match, plus MIN_MATCH bytes to insert the + * string following the next match. + */ + if (s->lookahead < MIN_LOOKAHEAD) { + fill_window(s); + if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { + return need_more; + } + if (s->lookahead == 0) break; /* flush the current block */ + } + + /* Insert the string window[strstart .. strstart+2] in the + * dictionary, and set hash_head to the head of the hash chain: + */ + hash_head = NIL; + if (s->lookahead >= MIN_MATCH) { + INSERT_STRING(s, s->strstart, hash_head); + } + + /* Find the longest match, discarding those <= prev_length. + */ + s->prev_length = s->match_length, s->prev_match = s->match_start; + s->match_length = MIN_MATCH-1; + + if (hash_head != NIL && s->prev_length < s->max_lazy_match && + s->strstart - hash_head <= MAX_DIST(s)) { + /* To simplify the code, we prevent matches with the string + * of window index 0 (in particular we have to avoid a match + * of the string with itself at the start of the input file). + */ + s->match_length = longest_match (s, hash_head); + /* longest_match() sets match_start */ + + if (s->match_length <= 5 && (s->strategy == Z_FILTERED +#if TOO_FAR <= 32767 + || (s->match_length == MIN_MATCH && + s->strstart - s->match_start > TOO_FAR) +#endif + )) { + + /* If prev_match is also MIN_MATCH, match_start is garbage + * but we will ignore the current match anyway. + */ + s->match_length = MIN_MATCH-1; + } + } + /* If there was a match at the previous step and the current + * match is not better, output the previous match: + */ + if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { + uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; + /* Do not insert strings in hash table beyond this. */ + + check_match(s, s->strstart-1, s->prev_match, s->prev_length); + + _tr_tally_dist(s, s->strstart -1 - s->prev_match, + s->prev_length - MIN_MATCH, bflush); + + /* Insert in hash table all strings up to the end of the match. + * strstart-1 and strstart are already inserted. If there is not + * enough lookahead, the last two strings are not inserted in + * the hash table. + */ + s->lookahead -= s->prev_length-1; + s->prev_length -= 2; + do { + if (++s->strstart <= max_insert) { + INSERT_STRING(s, s->strstart, hash_head); + } + } while (--s->prev_length != 0); + s->match_available = 0; + s->match_length = MIN_MATCH-1; + s->strstart++; + + if (bflush) FLUSH_BLOCK(s, 0); + + } else if (s->match_available) { + /* If there was no match at the previous position, output a + * single literal. If there was a match but the current match + * is longer, truncate the previous match to a single literal. + */ + Tracevv((stderr,"%c", s->window[s->strstart-1])); + _tr_tally_lit(s, s->window[s->strstart-1], bflush); + if (bflush) { + FLUSH_BLOCK_ONLY(s, 0); + } + s->strstart++; + s->lookahead--; + if (s->strm->avail_out == 0) return need_more; + } else { + /* There is no previous match to compare with, wait for + * the next step to decide. + */ + s->match_available = 1; + s->strstart++; + s->lookahead--; + } + } + Assert (flush != Z_NO_FLUSH, "no flush?"); + if (s->match_available) { + Tracevv((stderr,"%c", s->window[s->strstart-1])); + _tr_tally_lit(s, s->window[s->strstart-1], bflush); + s->match_available = 0; + } + s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->last_lit) + FLUSH_BLOCK(s, 0); + return block_done; +} +#endif /* FASTEST */ + +/* =========================================================================== + * For Z_RLE, simply look for runs of bytes, generate matches only of distance + * one. Do not maintain a hash table. (It will be regenerated if this run of + * deflate switches away from Z_RLE.) + */ +local block_state deflate_rle(s, flush) + deflate_state *s; + int flush; +{ + int bflush; /* set if current block must be flushed */ + uInt prev; /* byte at distance one to match */ + Bytef *scan, *strend; /* scan goes up to strend for length of run */ + + for (;;) { + /* Make sure that we always have enough lookahead, except + * at the end of the input file. We need MAX_MATCH bytes + * for the longest run, plus one for the unrolled loop. + */ + if (s->lookahead <= MAX_MATCH) { + fill_window(s); + if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) { + return need_more; + } + if (s->lookahead == 0) break; /* flush the current block */ + } + + /* See how many times the previous byte repeats */ + s->match_length = 0; + if (s->lookahead >= MIN_MATCH && s->strstart > 0) { + scan = s->window + s->strstart - 1; + prev = *scan; + if (prev == *++scan && prev == *++scan && prev == *++scan) { + strend = s->window + s->strstart + MAX_MATCH; + do { + } while (prev == *++scan && prev == *++scan && + prev == *++scan && prev == *++scan && + prev == *++scan && prev == *++scan && + prev == *++scan && prev == *++scan && + scan < strend); + s->match_length = MAX_MATCH - (int)(strend - scan); + if (s->match_length > s->lookahead) + s->match_length = s->lookahead; + } + Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); + } + + /* Emit match if have run of MIN_MATCH or longer, else emit literal */ + if (s->match_length >= MIN_MATCH) { + check_match(s, s->strstart, s->strstart - 1, s->match_length); + + _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); + + s->lookahead -= s->match_length; + s->strstart += s->match_length; + s->match_length = 0; + } else { + /* No match, output a literal byte */ + Tracevv((stderr,"%c", s->window[s->strstart])); + _tr_tally_lit (s, s->window[s->strstart], bflush); + s->lookahead--; + s->strstart++; + } + if (bflush) FLUSH_BLOCK(s, 0); + } + s->insert = 0; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->last_lit) + FLUSH_BLOCK(s, 0); + return block_done; +} + +/* =========================================================================== + * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. + * (It will be regenerated if this run of deflate switches away from Huffman.) + */ +local block_state deflate_huff(s, flush) + deflate_state *s; + int flush; +{ + int bflush; /* set if current block must be flushed */ + + for (;;) { + /* Make sure that we have a literal to write. */ + if (s->lookahead == 0) { + fill_window(s); + if (s->lookahead == 0) { + if (flush == Z_NO_FLUSH) + return need_more; + break; /* flush the current block */ + } + } + + /* Output a literal byte */ + s->match_length = 0; + Tracevv((stderr,"%c", s->window[s->strstart])); + _tr_tally_lit (s, s->window[s->strstart], bflush); + s->lookahead--; + s->strstart++; + if (bflush) FLUSH_BLOCK(s, 0); + } + s->insert = 0; + if (flush == Z_FINISH) { + FLUSH_BLOCK(s, 1); + return finish_done; + } + if (s->last_lit) + FLUSH_BLOCK(s, 0); + return block_done; +} diff --git a/fdbrpc/zlib/deflate.h b/fdbrpc/zlib/deflate.h new file mode 100644 index 0000000000..ce0299edd1 --- /dev/null +++ b/fdbrpc/zlib/deflate.h @@ -0,0 +1,346 @@ +/* deflate.h -- internal compression state + * Copyright (C) 1995-2012 Jean-loup Gailly + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* @(#) $Id$ */ + +#ifndef DEFLATE_H +#define DEFLATE_H + +#include "zutil.h" + +/* define NO_GZIP when compiling if you want to disable gzip header and + trailer creation by deflate(). NO_GZIP would be used to avoid linking in + the crc code when it is not needed. For shared libraries, gzip encoding + should be left enabled. */ +#ifndef NO_GZIP +# define GZIP +#endif + +/* =========================================================================== + * Internal compression state. + */ + +#define LENGTH_CODES 29 +/* number of length codes, not counting the special END_BLOCK code */ + +#define LITERALS 256 +/* number of literal bytes 0..255 */ + +#define L_CODES (LITERALS+1+LENGTH_CODES) +/* number of Literal or Length codes, including the END_BLOCK code */ + +#define D_CODES 30 +/* number of distance codes */ + +#define BL_CODES 19 +/* number of codes used to transfer the bit lengths */ + +#define HEAP_SIZE (2*L_CODES+1) +/* maximum heap size */ + +#define MAX_BITS 15 +/* All codes must not exceed MAX_BITS bits */ + +#define Buf_size 16 +/* size of bit buffer in bi_buf */ + +#define INIT_STATE 42 +#define EXTRA_STATE 69 +#define NAME_STATE 73 +#define COMMENT_STATE 91 +#define HCRC_STATE 103 +#define BUSY_STATE 113 +#define FINISH_STATE 666 +/* Stream status */ + + +/* Data structure describing a single value and its code string. */ +typedef struct ct_data_s { + union { + ush freq; /* frequency count */ + ush code; /* bit string */ + } fc; + union { + ush dad; /* father node in Huffman tree */ + ush len; /* length of bit string */ + } dl; +} FAR ct_data; + +#define Freq fc.freq +#define Code fc.code +#define Dad dl.dad +#define Len dl.len + +typedef struct static_tree_desc_s static_tree_desc; + +typedef struct tree_desc_s { + ct_data *dyn_tree; /* the dynamic tree */ + int max_code; /* largest code with non zero frequency */ + static_tree_desc *stat_desc; /* the corresponding static tree */ +} FAR tree_desc; + +typedef ush Pos; +typedef Pos FAR Posf; +typedef unsigned IPos; + +/* A Pos is an index in the character window. We use short instead of int to + * save space in the various tables. IPos is used only for parameter passing. + */ + +typedef struct internal_state { + z_streamp strm; /* pointer back to this zlib stream */ + int status; /* as the name implies */ + Bytef *pending_buf; /* output still pending */ + ulg pending_buf_size; /* size of pending_buf */ + Bytef *pending_out; /* next pending byte to output to the stream */ + uInt pending; /* nb of bytes in the pending buffer */ + int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ + gz_headerp gzhead; /* gzip header information to write */ + uInt gzindex; /* where in extra, name, or comment */ + Byte method; /* can only be DEFLATED */ + int last_flush; /* value of flush param for previous deflate call */ + + /* used by deflate.c: */ + + uInt w_size; /* LZ77 window size (32K by default) */ + uInt w_bits; /* log2(w_size) (8..16) */ + uInt w_mask; /* w_size - 1 */ + + Bytef *window; + /* Sliding window. Input bytes are read into the second half of the window, + * and move to the first half later to keep a dictionary of at least wSize + * bytes. With this organization, matches are limited to a distance of + * wSize-MAX_MATCH bytes, but this ensures that IO is always + * performed with a length multiple of the block size. Also, it limits + * the window size to 64K, which is quite useful on MSDOS. + * To do: use the user input buffer as sliding window. + */ + + ulg window_size; + /* Actual size of window: 2*wSize, except when the user input buffer + * is directly used as sliding window. + */ + + Posf *prev; + /* Link to older string with same hash index. To limit the size of this + * array to 64K, this link is maintained only for the last 32K strings. + * An index in this array is thus a window index modulo 32K. + */ + + Posf *head; /* Heads of the hash chains or NIL. */ + + uInt ins_h; /* hash index of string to be inserted */ + uInt hash_size; /* number of elements in hash table */ + uInt hash_bits; /* log2(hash_size) */ + uInt hash_mask; /* hash_size-1 */ + + uInt hash_shift; + /* Number of bits by which ins_h must be shifted at each input + * step. It must be such that after MIN_MATCH steps, the oldest + * byte no longer takes part in the hash key, that is: + * hash_shift * MIN_MATCH >= hash_bits + */ + + long block_start; + /* Window position at the beginning of the current output block. Gets + * negative when the window is moved backwards. + */ + + uInt match_length; /* length of best match */ + IPos prev_match; /* previous match */ + int match_available; /* set if previous match exists */ + uInt strstart; /* start of string to insert */ + uInt match_start; /* start of matching string */ + uInt lookahead; /* number of valid bytes ahead in window */ + + uInt prev_length; + /* Length of the best match at previous step. Matches not greater than this + * are discarded. This is used in the lazy match evaluation. + */ + + uInt max_chain_length; + /* To speed up deflation, hash chains are never searched beyond this + * length. A higher limit improves compression ratio but degrades the + * speed. + */ + + uInt max_lazy_match; + /* Attempt to find a better match only when the current match is strictly + * smaller than this value. This mechanism is used only for compression + * levels >= 4. + */ +# define max_insert_length max_lazy_match + /* Insert new strings in the hash table only if the match length is not + * greater than this length. This saves time but degrades compression. + * max_insert_length is used only for compression levels <= 3. + */ + + int level; /* compression level (1..9) */ + int strategy; /* favor or force Huffman coding*/ + + uInt good_match; + /* Use a faster search when the previous match is longer than this */ + + int nice_match; /* Stop searching when current match exceeds this */ + + /* used by trees.c: */ + /* Didn't use ct_data typedef below to suppress compiler warning */ + struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ + struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ + struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ + + struct tree_desc_s l_desc; /* desc. for literal tree */ + struct tree_desc_s d_desc; /* desc. for distance tree */ + struct tree_desc_s bl_desc; /* desc. for bit length tree */ + + ush bl_count[MAX_BITS+1]; + /* number of codes at each bit length for an optimal tree */ + + int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ + int heap_len; /* number of elements in the heap */ + int heap_max; /* element of largest frequency */ + /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. + * The same heap array is used to build all trees. + */ + + uch depth[2*L_CODES+1]; + /* Depth of each subtree used as tie breaker for trees of equal frequency + */ + + uchf *l_buf; /* buffer for literals or lengths */ + + uInt lit_bufsize; + /* Size of match buffer for literals/lengths. There are 4 reasons for + * limiting lit_bufsize to 64K: + * - frequencies can be kept in 16 bit counters + * - if compression is not successful for the first block, all input + * data is still in the window so we can still emit a stored block even + * when input comes from standard input. (This can also be done for + * all blocks if lit_bufsize is not greater than 32K.) + * - if compression is not successful for a file smaller than 64K, we can + * even emit a stored file instead of a stored block (saving 5 bytes). + * This is applicable only for zip (not gzip or zlib). + * - creating new Huffman trees less frequently may not provide fast + * adaptation to changes in the input data statistics. (Take for + * example a binary file with poorly compressible code followed by + * a highly compressible string table.) Smaller buffer sizes give + * fast adaptation but have of course the overhead of transmitting + * trees more frequently. + * - I can't count above 4 + */ + + uInt last_lit; /* running index in l_buf */ + + ushf *d_buf; + /* Buffer for distances. To simplify the code, d_buf and l_buf have + * the same number of elements. To use different lengths, an extra flag + * array would be necessary. + */ + + ulg opt_len; /* bit length of current block with optimal trees */ + ulg static_len; /* bit length of current block with static trees */ + uInt matches; /* number of string matches in current block */ + uInt insert; /* bytes at end of window left to insert */ + +#ifdef DEBUG + ulg compressed_len; /* total bit length of compressed file mod 2^32 */ + ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ +#endif + + ush bi_buf; + /* Output buffer. bits are inserted starting at the bottom (least + * significant bits). + */ + int bi_valid; + /* Number of valid bits in bi_buf. All bits above the last valid bit + * are always zero. + */ + + ulg high_water; + /* High water mark offset in window for initialized bytes -- bytes above + * this are set to zero in order to avoid memory check warnings when + * longest match routines access bytes past the input. This is then + * updated to the new high water mark. + */ + +} FAR deflate_state; + +/* Output a byte on the stream. + * IN assertion: there is enough room in pending_buf. + */ +#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);} + + +#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) +/* Minimum amount of lookahead, except at the end of the input file. + * See deflate.c for comments about the MIN_MATCH+1. + */ + +#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) +/* In order to simplify the code, particularly on 16 bit machines, match + * distances are limited to MAX_DIST instead of WSIZE. + */ + +#define WIN_INIT MAX_MATCH +/* Number of bytes after end of data in window to initialize in order to avoid + memory checker errors from longest match routines */ + + /* in trees.c */ +void ZLIB_INTERNAL _tr_init OF((deflate_state *s)); +int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); +void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf, + ulg stored_len, int last)); +void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s)); +void ZLIB_INTERNAL _tr_align OF((deflate_state *s)); +void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, + ulg stored_len, int last)); + +#define d_code(dist) \ + ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) +/* Mapping from a distance to a distance code. dist is the distance - 1 and + * must not have side effects. _dist_code[256] and _dist_code[257] are never + * used. + */ + +#ifndef DEBUG +/* Inline versions of _tr_tally for speed: */ + +#if defined(GEN_TREES_H) || !defined(STDC) + extern uch ZLIB_INTERNAL _length_code[]; + extern uch ZLIB_INTERNAL _dist_code[]; +#else + extern const uch ZLIB_INTERNAL _length_code[]; + extern const uch ZLIB_INTERNAL _dist_code[]; +#endif + +# define _tr_tally_lit(s, c, flush) \ + { uch cc = (c); \ + s->d_buf[s->last_lit] = 0; \ + s->l_buf[s->last_lit++] = cc; \ + s->dyn_ltree[cc].Freq++; \ + flush = (s->last_lit == s->lit_bufsize-1); \ + } +# define _tr_tally_dist(s, distance, length, flush) \ + { uch len = (length); \ + ush dist = (distance); \ + s->d_buf[s->last_lit] = dist; \ + s->l_buf[s->last_lit++] = len; \ + dist--; \ + s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ + s->dyn_dtree[d_code(dist)].Freq++; \ + flush = (s->last_lit == s->lit_bufsize-1); \ + } +#else +# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) +# define _tr_tally_dist(s, distance, length, flush) \ + flush = _tr_tally(s, distance, length) +#endif + +#endif /* DEFLATE_H */ diff --git a/fdbrpc/zlib/gzclose.c b/fdbrpc/zlib/gzclose.c new file mode 100644 index 0000000000..caeb99a317 --- /dev/null +++ b/fdbrpc/zlib/gzclose.c @@ -0,0 +1,25 @@ +/* gzclose.c -- zlib gzclose() function + * Copyright (C) 2004, 2010 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "gzguts.h" + +/* gzclose() is in a separate file so that it is linked in only if it is used. + That way the other gzclose functions can be used instead to avoid linking in + unneeded compression or decompression routines. */ +int ZEXPORT gzclose(file) + gzFile file; +{ +#ifndef NO_GZCOMPRESS + gz_statep state; + + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + + return state->mode == GZ_READ ? gzclose_r(file) : gzclose_w(file); +#else + return gzclose_r(file); +#endif +} diff --git a/fdbrpc/zlib/gzguts.h b/fdbrpc/zlib/gzguts.h new file mode 100644 index 0000000000..8ce5d25cd6 --- /dev/null +++ b/fdbrpc/zlib/gzguts.h @@ -0,0 +1,211 @@ +/* gzguts.h -- zlib internal header definitions for gz* operations + * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#ifdef _LARGEFILE64_SOURCE +# ifndef _LARGEFILE_SOURCE +# define _LARGEFILE_SOURCE 1 +# endif +# ifdef _FILE_OFFSET_BITS +# undef _FILE_OFFSET_BITS +# endif +#endif + +#ifdef HAVE_HIDDEN +# define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) +#else +# define ZLIB_INTERNAL +#endif + +#include +#include "zlib.h" +#ifdef STDC +# include +# include +# include +#endif +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +#if defined(__TURBOC__) || defined(_MSC_VER) || defined(_WIN32) +# include +#endif + +#ifdef WINAPI_FAMILY +# define open _open +# define read _read +# define write _write +# define close _close +#endif + +#ifdef NO_DEFLATE /* for compatibility with old definition */ +# define NO_GZCOMPRESS +#endif + +#if defined(STDC99) || (defined(__TURBOC__) && __TURBOC__ >= 0x550) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif + +#if defined(__CYGWIN__) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif + +#if defined(MSDOS) && defined(__BORLANDC__) && (BORLANDC > 0x410) +# ifndef HAVE_VSNPRINTF +# define HAVE_VSNPRINTF +# endif +#endif + +#ifndef HAVE_VSNPRINTF +# ifdef MSDOS +/* vsnprintf may exist on some MS-DOS compilers (DJGPP?), + but for now we just assume it doesn't. */ +# define NO_vsnprintf +# endif +# ifdef __TURBOC__ +# define NO_vsnprintf +# endif +# ifdef WIN32 +/* In Win32, vsnprintf is available as the "non-ANSI" _vsnprintf. */ +# if !defined(vsnprintf) && !defined(NO_vsnprintf) +# if !defined(_MSC_VER) || ( defined(_MSC_VER) && _MSC_VER < 1500 ) +# define vsnprintf _vsnprintf +# endif +# endif +# endif +# ifdef __SASC +# define NO_vsnprintf +# endif +# ifdef VMS +# define NO_vsnprintf +# endif +# ifdef __OS400__ +# define NO_vsnprintf +# endif +# ifdef __MVS__ +# define NO_vsnprintf +# endif +#endif + +/* unlike snprintf (which is required in C99, yet still not supported by + Microsoft more than a decade later!), _snprintf does not guarantee null + termination of the result -- however this is only used in gzlib.c where + the result is assured to fit in the space provided */ +#ifdef _MSC_VER +# define snprintf _snprintf +#endif + +#ifndef local +# define local static +#endif +/* compile with -Dlocal if your debugger can't find static symbols */ + +/* gz* functions always use library allocation functions */ +#ifndef STDC + extern voidp malloc OF((uInt size)); + extern void free OF((voidpf ptr)); +#endif + +/* get errno and strerror definition */ +#if defined UNDER_CE +# include +# define zstrerror() gz_strwinerror((DWORD)GetLastError()) +#else +# ifndef NO_STRERROR +# include +# define zstrerror() strerror(errno) +# else +# define zstrerror() "stdio error (consult errno)" +# endif +#endif + +/* provide prototypes for these when building zlib without LFS */ +#if !defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); + ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); +#endif + +/* default memLevel */ +#if MAX_MEM_LEVEL >= 8 +# define DEF_MEM_LEVEL 8 +#else +# define DEF_MEM_LEVEL MAX_MEM_LEVEL +#endif + +/* default i/o buffer size -- double this for output when reading (this and + twice this must be able to fit in an unsigned type) */ +#define GZBUFSIZE 8192 + +/* gzip modes, also provide a little integrity check on the passed structure */ +#define GZ_NONE 0 +#define GZ_READ 7247 +#define GZ_WRITE 31153 +#define GZ_APPEND 1 /* mode set to GZ_WRITE after the file is opened */ + +/* values for gz_state how */ +#define LOOK 0 /* look for a gzip header */ +#define COPY 1 /* copy input directly */ +#define GZIP 2 /* decompress a gzip stream */ + +/* internal gzip file state data structure */ +typedef struct { + /* exposed contents for gzgetc() macro */ + struct gzFile_s x; /* "x" for exposed */ + /* x.have: number of bytes available at x.next */ + /* x.next: next output data to deliver or write */ + /* x.pos: current position in uncompressed data */ + /* used for both reading and writing */ + int mode; /* see gzip modes above */ + int fd; /* file descriptor */ + char *path; /* path or fd for error messages */ + unsigned size; /* buffer size, zero if not allocated yet */ + unsigned want; /* requested buffer size, default is GZBUFSIZE */ + unsigned char *in; /* input buffer */ + unsigned char *out; /* output buffer (double-sized when reading) */ + int direct; /* 0 if processing gzip, 1 if transparent */ + /* just for reading */ + int how; /* 0: get header, 1: copy, 2: decompress */ + z_off64_t start; /* where the gzip data started, for rewinding */ + int eof; /* true if end of input file reached */ + int past; /* true if read requested past end */ + /* just for writing */ + int level; /* compression level */ + int strategy; /* compression strategy */ + /* seek request */ + z_off64_t skip; /* amount to skip (already rewound if backwards) */ + int seek; /* true if seek request pending */ + /* error information */ + int err; /* error code */ + char *msg; /* error message */ + /* zlib inflate or deflate stream */ + z_stream strm; /* stream structure in-place (not a pointer) */ +} gz_state; +typedef gz_state FAR *gz_statep; + +/* shared functions */ +void ZLIB_INTERNAL gz_error OF((gz_statep, int, const char *)); +#if defined UNDER_CE +char ZLIB_INTERNAL *gz_strwinerror OF((DWORD error)); +#endif + +/* GT_OFF(x), where x is an unsigned value, is true if x > maximum z_off64_t + value -- needed when comparing unsigned to z_off64_t, which is signed + (possible z_off64_t types off_t, off64_t, and long are all signed) */ +#ifdef INT_MAX +# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > INT_MAX) +#else +unsigned ZLIB_INTERNAL gz_intmax OF((void)); +# define GT_OFF(x) (sizeof(int) == sizeof(z_off64_t) && (x) > gz_intmax()) +#endif diff --git a/fdbrpc/zlib/gzlib.c b/fdbrpc/zlib/gzlib.c new file mode 100644 index 0000000000..fae202ef89 --- /dev/null +++ b/fdbrpc/zlib/gzlib.c @@ -0,0 +1,634 @@ +/* gzlib.c -- zlib functions common to reading and writing gzip files + * Copyright (C) 2004, 2010, 2011, 2012, 2013 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "gzguts.h" + +#if defined(_WIN32) && !defined(__BORLANDC__) +# define LSEEK _lseeki64 +#else +#if defined(_LARGEFILE64_SOURCE) && _LFS64_LARGEFILE-0 +# define LSEEK lseek64 +#else +# define LSEEK lseek +#endif +#endif + +/* Local functions */ +local void gz_reset OF((gz_statep)); +local gzFile gz_open OF((const void *, int, const char *)); + +#if defined UNDER_CE + +/* Map the Windows error number in ERROR to a locale-dependent error message + string and return a pointer to it. Typically, the values for ERROR come + from GetLastError. + + The string pointed to shall not be modified by the application, but may be + overwritten by a subsequent call to gz_strwinerror + + The gz_strwinerror function does not change the current setting of + GetLastError. */ +char ZLIB_INTERNAL *gz_strwinerror (error) + DWORD error; +{ + static char buf[1024]; + + wchar_t *msgbuf; + DWORD lasterr = GetLastError(); + DWORD chars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_ALLOCATE_BUFFER, + NULL, + error, + 0, /* Default language */ + (LPVOID)&msgbuf, + 0, + NULL); + if (chars != 0) { + /* If there is an \r\n appended, zap it. */ + if (chars >= 2 + && msgbuf[chars - 2] == '\r' && msgbuf[chars - 1] == '\n') { + chars -= 2; + msgbuf[chars] = 0; + } + + if (chars > sizeof (buf) - 1) { + chars = sizeof (buf) - 1; + msgbuf[chars] = 0; + } + + wcstombs(buf, msgbuf, chars + 1); + LocalFree(msgbuf); + } + else { + sprintf(buf, "unknown win32 error (%ld)", error); + } + + SetLastError(lasterr); + return buf; +} + +#endif /* UNDER_CE */ + +/* Reset gzip file state */ +local void gz_reset(state) + gz_statep state; +{ + state->x.have = 0; /* no output data available */ + if (state->mode == GZ_READ) { /* for reading ... */ + state->eof = 0; /* not at end of file */ + state->past = 0; /* have not read past end yet */ + state->how = LOOK; /* look for gzip header */ + } + state->seek = 0; /* no seek request pending */ + gz_error(state, Z_OK, NULL); /* clear error */ + state->x.pos = 0; /* no uncompressed data yet */ + state->strm.avail_in = 0; /* no input data yet */ +} + +/* Open a gzip file either by name or file descriptor. */ +local gzFile gz_open(path, fd, mode) + const void *path; + int fd; + const char *mode; +{ + gz_statep state; + size_t len; + int oflag; +#ifdef O_CLOEXEC + int cloexec = 0; +#endif +#ifdef O_EXCL + int exclusive = 0; +#endif + + /* check input */ + if (path == NULL) + return NULL; + + /* allocate gzFile structure to return */ + state = (gz_statep)malloc(sizeof(gz_state)); + if (state == NULL) + return NULL; + state->size = 0; /* no buffers allocated yet */ + state->want = GZBUFSIZE; /* requested buffer size */ + state->msg = NULL; /* no error message yet */ + + /* interpret mode */ + state->mode = GZ_NONE; + state->level = Z_DEFAULT_COMPRESSION; + state->strategy = Z_DEFAULT_STRATEGY; + state->direct = 0; + while (*mode) { + if (*mode >= '0' && *mode <= '9') + state->level = *mode - '0'; + else + switch (*mode) { + case 'r': + state->mode = GZ_READ; + break; +#ifndef NO_GZCOMPRESS + case 'w': + state->mode = GZ_WRITE; + break; + case 'a': + state->mode = GZ_APPEND; + break; +#endif + case '+': /* can't read and write at the same time */ + free(state); + return NULL; + case 'b': /* ignore -- will request binary anyway */ + break; +#ifdef O_CLOEXEC + case 'e': + cloexec = 1; + break; +#endif +#ifdef O_EXCL + case 'x': + exclusive = 1; + break; +#endif + case 'f': + state->strategy = Z_FILTERED; + break; + case 'h': + state->strategy = Z_HUFFMAN_ONLY; + break; + case 'R': + state->strategy = Z_RLE; + break; + case 'F': + state->strategy = Z_FIXED; + break; + case 'T': + state->direct = 1; + break; + default: /* could consider as an error, but just ignore */ + ; + } + mode++; + } + + /* must provide an "r", "w", or "a" */ + if (state->mode == GZ_NONE) { + free(state); + return NULL; + } + + /* can't force transparent read */ + if (state->mode == GZ_READ) { + if (state->direct) { + free(state); + return NULL; + } + state->direct = 1; /* for empty file */ + } + + /* save the path name for error messages */ +#ifdef _WIN32 + if (fd == -2) { + len = wcstombs(NULL, path, 0); + if (len == (size_t)-1) + len = 0; + } + else +#endif + len = strlen((const char *)path); + state->path = (char *)malloc(len + 1); + if (state->path == NULL) { + free(state); + return NULL; + } +#ifdef _WIN32 + if (fd == -2) + if (len) + wcstombs(state->path, path, len + 1); + else + *(state->path) = 0; + else +#endif +#if !defined(NO_snprintf) && !defined(NO_vsnprintf) + snprintf(state->path, len + 1, "%s", (const char *)path); +#else + strcpy(state->path, path); +#endif + + /* compute the flags for open() */ + oflag = +#ifdef O_LARGEFILE + O_LARGEFILE | +#endif +#ifdef O_BINARY + O_BINARY | +#endif +#ifdef O_CLOEXEC + (cloexec ? O_CLOEXEC : 0) | +#endif + (state->mode == GZ_READ ? + O_RDONLY : + (O_WRONLY | O_CREAT | +#ifdef O_EXCL + (exclusive ? O_EXCL : 0) | +#endif + (state->mode == GZ_WRITE ? + O_TRUNC : + O_APPEND))); + + /* open the file with the appropriate flags (or just use fd) */ + state->fd = fd > -1 ? fd : ( +#ifdef _WIN32 + fd == -2 ? _wopen(path, oflag, 0666) : +#endif + open((const char *)path, oflag, 0666)); + if (state->fd == -1) { + free(state->path); + free(state); + return NULL; + } + if (state->mode == GZ_APPEND) + state->mode = GZ_WRITE; /* simplify later checks */ + + /* save the current position for rewinding (only if reading) */ + if (state->mode == GZ_READ) { + state->start = LSEEK(state->fd, 0, SEEK_CUR); + if (state->start == -1) state->start = 0; + } + + /* initialize stream */ + gz_reset(state); + + /* return stream */ + return (gzFile)state; +} + +/* -- see zlib.h -- */ +gzFile ZEXPORT gzopen(path, mode) + const char *path; + const char *mode; +{ + return gz_open(path, -1, mode); +} + +/* -- see zlib.h -- */ +gzFile ZEXPORT gzopen64(path, mode) + const char *path; + const char *mode; +{ + return gz_open(path, -1, mode); +} + +/* -- see zlib.h -- */ +gzFile ZEXPORT gzdopen(fd, mode) + int fd; + const char *mode; +{ + char *path; /* identifier for error messages */ + gzFile gz; + + if (fd == -1 || (path = (char *)malloc(7 + 3 * sizeof(int))) == NULL) + return NULL; +#if !defined(NO_snprintf) && !defined(NO_vsnprintf) + snprintf(path, 7 + 3 * sizeof(int), "", fd); /* for debugging */ +#else + sprintf(path, "", fd); /* for debugging */ +#endif + gz = gz_open(path, fd, mode); + free(path); + return gz; +} + +/* -- see zlib.h -- */ +#ifdef _WIN32 +gzFile ZEXPORT gzopen_w(path, mode) + const wchar_t *path; + const char *mode; +{ + return gz_open(path, -2, mode); +} +#endif + +/* -- see zlib.h -- */ +int ZEXPORT gzbuffer(file, size) + gzFile file; + unsigned size; +{ + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return -1; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return -1; + + /* make sure we haven't already allocated memory */ + if (state->size != 0) + return -1; + + /* check and set requested size */ + if (size < 2) + size = 2; /* need two bytes to check magic header */ + state->want = size; + return 0; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzrewind(file) + gzFile file; +{ + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + + /* check that we're reading and that there's no error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return -1; + + /* back up and start over */ + if (LSEEK(state->fd, state->start, SEEK_SET) == -1) + return -1; + gz_reset(state); + return 0; +} + +/* -- see zlib.h -- */ +z_off64_t ZEXPORT gzseek64(file, offset, whence) + gzFile file; + z_off64_t offset; + int whence; +{ + unsigned n; + z_off64_t ret; + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return -1; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return -1; + + /* check that there's no error */ + if (state->err != Z_OK && state->err != Z_BUF_ERROR) + return -1; + + /* can only seek from start or relative to current position */ + if (whence != SEEK_SET && whence != SEEK_CUR) + return -1; + + /* normalize offset to a SEEK_CUR specification */ + if (whence == SEEK_SET) + offset -= state->x.pos; + else if (state->seek) + offset += state->skip; + state->seek = 0; + + /* if within raw area while reading, just go there */ + if (state->mode == GZ_READ && state->how == COPY && + state->x.pos + offset >= 0) { + ret = LSEEK(state->fd, offset - state->x.have, SEEK_CUR); + if (ret == -1) + return -1; + state->x.have = 0; + state->eof = 0; + state->past = 0; + state->seek = 0; + gz_error(state, Z_OK, NULL); + state->strm.avail_in = 0; + state->x.pos += offset; + return state->x.pos; + } + + /* calculate skip amount, rewinding if needed for back seek when reading */ + if (offset < 0) { + if (state->mode != GZ_READ) /* writing -- can't go backwards */ + return -1; + offset += state->x.pos; + if (offset < 0) /* before start of file! */ + return -1; + if (gzrewind(file) == -1) /* rewind, then skip to offset */ + return -1; + } + + /* if reading, skip what's in output buffer (one less gzgetc() check) */ + if (state->mode == GZ_READ) { + n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > offset ? + (unsigned)offset : state->x.have; + state->x.have -= n; + state->x.next += n; + state->x.pos += n; + offset -= n; + } + + /* request skip (if not zero) */ + if (offset) { + state->seek = 1; + state->skip = offset; + } + return state->x.pos + offset; +} + +/* -- see zlib.h -- */ +z_off_t ZEXPORT gzseek(file, offset, whence) + gzFile file; + z_off_t offset; + int whence; +{ + z_off64_t ret; + + ret = gzseek64(file, (z_off64_t)offset, whence); + return ret == (z_off_t)ret ? (z_off_t)ret : -1; +} + +/* -- see zlib.h -- */ +z_off64_t ZEXPORT gztell64(file) + gzFile file; +{ + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return -1; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return -1; + + /* return position */ + return state->x.pos + (state->seek ? state->skip : 0); +} + +/* -- see zlib.h -- */ +z_off_t ZEXPORT gztell(file) + gzFile file; +{ + z_off64_t ret; + + ret = gztell64(file); + return ret == (z_off_t)ret ? (z_off_t)ret : -1; +} + +/* -- see zlib.h -- */ +z_off64_t ZEXPORT gzoffset64(file) + gzFile file; +{ + z_off64_t offset; + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return -1; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return -1; + + /* compute and return effective offset in file */ + offset = LSEEK(state->fd, 0, SEEK_CUR); + if (offset == -1) + return -1; + if (state->mode == GZ_READ) /* reading */ + offset -= state->strm.avail_in; /* don't count buffered input */ + return offset; +} + +/* -- see zlib.h -- */ +z_off_t ZEXPORT gzoffset(file) + gzFile file; +{ + z_off64_t ret; + + ret = gzoffset64(file); + return ret == (z_off_t)ret ? (z_off_t)ret : -1; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzeof(file) + gzFile file; +{ + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return 0; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return 0; + + /* return end-of-file state */ + return state->mode == GZ_READ ? state->past : 0; +} + +/* -- see zlib.h -- */ +const char * ZEXPORT gzerror(file, errnum) + gzFile file; + int *errnum; +{ + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return NULL; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return NULL; + + /* return error information */ + if (errnum != NULL) + *errnum = state->err; + return state->err == Z_MEM_ERROR ? "out of memory" : + (state->msg == NULL ? "" : state->msg); +} + +/* -- see zlib.h -- */ +void ZEXPORT gzclearerr(file) + gzFile file; +{ + gz_statep state; + + /* get internal structure and check integrity */ + if (file == NULL) + return; + state = (gz_statep)file; + if (state->mode != GZ_READ && state->mode != GZ_WRITE) + return; + + /* clear error and end-of-file */ + if (state->mode == GZ_READ) { + state->eof = 0; + state->past = 0; + } + gz_error(state, Z_OK, NULL); +} + +/* Create an error message in allocated memory and set state->err and + state->msg accordingly. Free any previous error message already there. Do + not try to free or allocate space if the error is Z_MEM_ERROR (out of + memory). Simply save the error message as a static string. If there is an + allocation failure constructing the error message, then convert the error to + out of memory. */ +void ZLIB_INTERNAL gz_error(state, err, msg) + gz_statep state; + int err; + const char *msg; +{ + /* free previously allocated message and clear */ + if (state->msg != NULL) { + if (state->err != Z_MEM_ERROR) + free(state->msg); + state->msg = NULL; + } + + /* if fatal, set state->x.have to 0 so that the gzgetc() macro fails */ + if (err != Z_OK && err != Z_BUF_ERROR) + state->x.have = 0; + + /* set error code, and if no message, then done */ + state->err = err; + if (msg == NULL) + return; + + /* for an out of memory error, return literal string when requested */ + if (err == Z_MEM_ERROR) + return; + + /* construct error message with path */ + if ((state->msg = (char *)malloc(strlen(state->path) + strlen(msg) + 3)) == + NULL) { + state->err = Z_MEM_ERROR; + return; + } +#if !defined(NO_snprintf) && !defined(NO_vsnprintf) + snprintf(state->msg, strlen(state->path) + strlen(msg) + 3, + "%s%s%s", state->path, ": ", msg); +#else + strcpy(state->msg, state->path); + strcat(state->msg, ": "); + strcat(state->msg, msg); +#endif + return; +} + +#ifndef INT_MAX +/* portably return maximum value for an int (when limits.h presumed not + available) -- we need to do this to cover cases where 2's complement not + used, since C standard permits 1's complement and sign-bit representations, + otherwise we could just use ((unsigned)-1) >> 1 */ +unsigned ZLIB_INTERNAL gz_intmax() +{ + unsigned p, q; + + p = 1; + do { + q = p; + p <<= 1; + p++; + } while (p > q); + return q >> 1; +} +#endif diff --git a/fdbrpc/zlib/gzread.c b/fdbrpc/zlib/gzread.c new file mode 100644 index 0000000000..bf4538eb27 --- /dev/null +++ b/fdbrpc/zlib/gzread.c @@ -0,0 +1,594 @@ +/* gzread.c -- zlib functions for reading gzip files + * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "gzguts.h" + +/* Local functions */ +local int gz_load OF((gz_statep, unsigned char *, unsigned, unsigned *)); +local int gz_avail OF((gz_statep)); +local int gz_look OF((gz_statep)); +local int gz_decomp OF((gz_statep)); +local int gz_fetch OF((gz_statep)); +local int gz_skip OF((gz_statep, z_off64_t)); + +/* Use read() to load a buffer -- return -1 on error, otherwise 0. Read from + state->fd, and update state->eof, state->err, and state->msg as appropriate. + This function needs to loop on read(), since read() is not guaranteed to + read the number of bytes requested, depending on the type of descriptor. */ +local int gz_load(state, buf, len, have) + gz_statep state; + unsigned char *buf; + unsigned len; + unsigned *have; +{ + int ret; + + *have = 0; + do { + ret = read(state->fd, buf + *have, len - *have); + if (ret <= 0) + break; + *have += ret; + } while (*have < len); + if (ret < 0) { + gz_error(state, Z_ERRNO, zstrerror()); + return -1; + } + if (ret == 0) + state->eof = 1; + return 0; +} + +/* Load up input buffer and set eof flag if last data loaded -- return -1 on + error, 0 otherwise. Note that the eof flag is set when the end of the input + file is reached, even though there may be unused data in the buffer. Once + that data has been used, no more attempts will be made to read the file. + If strm->avail_in != 0, then the current data is moved to the beginning of + the input buffer, and then the remainder of the buffer is loaded with the + available data from the input file. */ +local int gz_avail(state) + gz_statep state; +{ + unsigned got; + z_streamp strm = &(state->strm); + + if (state->err != Z_OK && state->err != Z_BUF_ERROR) + return -1; + if (state->eof == 0) { + if (strm->avail_in) { /* copy what's there to the start */ + unsigned char *p = state->in; + unsigned const char *q = strm->next_in; + unsigned n = strm->avail_in; + do { + *p++ = *q++; + } while (--n); + } + if (gz_load(state, state->in + strm->avail_in, + state->size - strm->avail_in, &got) == -1) + return -1; + strm->avail_in += got; + strm->next_in = state->in; + } + return 0; +} + +/* Look for gzip header, set up for inflate or copy. state->x.have must be 0. + If this is the first time in, allocate required memory. state->how will be + left unchanged if there is no more input data available, will be set to COPY + if there is no gzip header and direct copying will be performed, or it will + be set to GZIP for decompression. If direct copying, then leftover input + data from the input buffer will be copied to the output buffer. In that + case, all further file reads will be directly to either the output buffer or + a user buffer. If decompressing, the inflate state will be initialized. + gz_look() will return 0 on success or -1 on failure. */ +local int gz_look(state) + gz_statep state; +{ + z_streamp strm = &(state->strm); + + /* allocate read buffers and inflate memory */ + if (state->size == 0) { + /* allocate buffers */ + state->in = (unsigned char *)malloc(state->want); + state->out = (unsigned char *)malloc(state->want << 1); + if (state->in == NULL || state->out == NULL) { + if (state->out != NULL) + free(state->out); + if (state->in != NULL) + free(state->in); + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + state->size = state->want; + + /* allocate inflate memory */ + state->strm.zalloc = Z_NULL; + state->strm.zfree = Z_NULL; + state->strm.opaque = Z_NULL; + state->strm.avail_in = 0; + state->strm.next_in = Z_NULL; + if (inflateInit2(&(state->strm), 15 + 16) != Z_OK) { /* gunzip */ + free(state->out); + free(state->in); + state->size = 0; + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + } + + /* get at least the magic bytes in the input buffer */ + if (strm->avail_in < 2) { + if (gz_avail(state) == -1) + return -1; + if (strm->avail_in == 0) + return 0; + } + + /* look for gzip magic bytes -- if there, do gzip decoding (note: there is + a logical dilemma here when considering the case of a partially written + gzip file, to wit, if a single 31 byte is written, then we cannot tell + whether this is a single-byte file, or just a partially written gzip + file -- for here we assume that if a gzip file is being written, then + the header will be written in a single operation, so that reading a + single byte is sufficient indication that it is not a gzip file) */ + if (strm->avail_in > 1 && + strm->next_in[0] == 31 && strm->next_in[1] == 139) { + inflateReset(strm); + state->how = GZIP; + state->direct = 0; + return 0; + } + + /* no gzip header -- if we were decoding gzip before, then this is trailing + garbage. Ignore the trailing garbage and finish. */ + if (state->direct == 0) { + strm->avail_in = 0; + state->eof = 1; + state->x.have = 0; + return 0; + } + + /* doing raw i/o, copy any leftover input to output -- this assumes that + the output buffer is larger than the input buffer, which also assures + space for gzungetc() */ + state->x.next = state->out; + if (strm->avail_in) { + memcpy(state->x.next, strm->next_in, strm->avail_in); + state->x.have = strm->avail_in; + strm->avail_in = 0; + } + state->how = COPY; + state->direct = 1; + return 0; +} + +/* Decompress from input to the provided next_out and avail_out in the state. + On return, state->x.have and state->x.next point to the just decompressed + data. If the gzip stream completes, state->how is reset to LOOK to look for + the next gzip stream or raw data, once state->x.have is depleted. Returns 0 + on success, -1 on failure. */ +local int gz_decomp(state) + gz_statep state; +{ + int ret = Z_OK; + unsigned had; + z_streamp strm = &(state->strm); + + /* fill output buffer up to end of deflate stream */ + had = strm->avail_out; + do { + /* get more input for inflate() */ + if (strm->avail_in == 0 && gz_avail(state) == -1) + return -1; + if (strm->avail_in == 0) { + gz_error(state, Z_BUF_ERROR, "unexpected end of file"); + break; + } + + /* decompress and handle errors */ + ret = inflate(strm, Z_NO_FLUSH); + if (ret == Z_STREAM_ERROR || ret == Z_NEED_DICT) { + gz_error(state, Z_STREAM_ERROR, + "internal error: inflate stream corrupt"); + return -1; + } + if (ret == Z_MEM_ERROR) { + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + if (ret == Z_DATA_ERROR) { /* deflate stream invalid */ + gz_error(state, Z_DATA_ERROR, + strm->msg == NULL ? "compressed data error" : strm->msg); + return -1; + } + } while (strm->avail_out && ret != Z_STREAM_END); + + /* update available output */ + state->x.have = had - strm->avail_out; + state->x.next = strm->next_out - state->x.have; + + /* if the gzip stream completed successfully, look for another */ + if (ret == Z_STREAM_END) + state->how = LOOK; + + /* good decompression */ + return 0; +} + +/* Fetch data and put it in the output buffer. Assumes state->x.have is 0. + Data is either copied from the input file or decompressed from the input + file depending on state->how. If state->how is LOOK, then a gzip header is + looked for to determine whether to copy or decompress. Returns -1 on error, + otherwise 0. gz_fetch() will leave state->how as COPY or GZIP unless the + end of the input file has been reached and all data has been processed. */ +local int gz_fetch(state) + gz_statep state; +{ + z_streamp strm = &(state->strm); + + do { + switch(state->how) { + case LOOK: /* -> LOOK, COPY (only if never GZIP), or GZIP */ + if (gz_look(state) == -1) + return -1; + if (state->how == LOOK) + return 0; + break; + case COPY: /* -> COPY */ + if (gz_load(state, state->out, state->size << 1, &(state->x.have)) + == -1) + return -1; + state->x.next = state->out; + return 0; + case GZIP: /* -> GZIP or LOOK (if end of gzip stream) */ + strm->avail_out = state->size << 1; + strm->next_out = state->out; + if (gz_decomp(state) == -1) + return -1; + } + } while (state->x.have == 0 && (!state->eof || strm->avail_in)); + return 0; +} + +/* Skip len uncompressed bytes of output. Return -1 on error, 0 on success. */ +local int gz_skip(state, len) + gz_statep state; + z_off64_t len; +{ + unsigned n; + + /* skip over len bytes or reach end-of-file, whichever comes first */ + while (len) + /* skip over whatever is in output buffer */ + if (state->x.have) { + n = GT_OFF(state->x.have) || (z_off64_t)state->x.have > len ? + (unsigned)len : state->x.have; + state->x.have -= n; + state->x.next += n; + state->x.pos += n; + len -= n; + } + + /* output buffer empty -- return if we're at the end of the input */ + else if (state->eof && state->strm.avail_in == 0) + break; + + /* need more data to skip -- load up output buffer */ + else { + /* get more output, looking for header if required */ + if (gz_fetch(state) == -1) + return -1; + } + return 0; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzread(file, buf, len) + gzFile file; + voidp buf; + unsigned len; +{ + unsigned got, n; + gz_statep state; + z_streamp strm; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + strm = &(state->strm); + + /* check that we're reading and that there's no (serious) error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return -1; + + /* since an int is returned, make sure len fits in one, otherwise return + with an error (this avoids the flaw in the interface) */ + if ((int)len < 0) { + gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); + return -1; + } + + /* if len is zero, avoid unnecessary operations */ + if (len == 0) + return 0; + + /* process a skip request */ + if (state->seek) { + state->seek = 0; + if (gz_skip(state, state->skip) == -1) + return -1; + } + + /* get len bytes to buf, or less than len if at the end */ + got = 0; + do { + /* first just try copying data from the output buffer */ + if (state->x.have) { + n = state->x.have > len ? len : state->x.have; + memcpy(buf, state->x.next, n); + state->x.next += n; + state->x.have -= n; + } + + /* output buffer empty -- return if we're at the end of the input */ + else if (state->eof && strm->avail_in == 0) { + state->past = 1; /* tried to read past end */ + break; + } + + /* need output data -- for small len or new stream load up our output + buffer */ + else if (state->how == LOOK || len < (state->size << 1)) { + /* get more output, looking for header if required */ + if (gz_fetch(state) == -1) + return -1; + continue; /* no progress yet -- go back to copy above */ + /* the copy above assures that we will leave with space in the + output buffer, allowing at least one gzungetc() to succeed */ + } + + /* large len -- read directly into user buffer */ + else if (state->how == COPY) { /* read directly */ + if (gz_load(state, (unsigned char *)buf, len, &n) == -1) + return -1; + } + + /* large len -- decompress directly into user buffer */ + else { /* state->how == GZIP */ + strm->avail_out = len; + strm->next_out = (unsigned char *)buf; + if (gz_decomp(state) == -1) + return -1; + n = state->x.have; + state->x.have = 0; + } + + /* update progress */ + len -= n; + buf = (char *)buf + n; + got += n; + state->x.pos += n; + } while (len); + + /* return number of bytes read into user buffer (will fit in int) */ + return (int)got; +} + +/* -- see zlib.h -- */ +#ifdef Z_PREFIX_SET +# undef z_gzgetc +#else +# undef gzgetc +#endif +int ZEXPORT gzgetc(file) + gzFile file; +{ + int ret; + unsigned char buf[1]; + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + + /* check that we're reading and that there's no (serious) error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return -1; + + /* try output buffer (no need to check for skip request) */ + if (state->x.have) { + state->x.have--; + state->x.pos++; + return *(state->x.next)++; + } + + /* nothing there -- try gzread() */ + ret = gzread(file, buf, 1); + return ret < 1 ? -1 : buf[0]; +} + +int ZEXPORT gzgetc_(file) +gzFile file; +{ + return gzgetc(file); +} + +/* -- see zlib.h -- */ +int ZEXPORT gzungetc(c, file) + int c; + gzFile file; +{ + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + + /* check that we're reading and that there's no (serious) error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return -1; + + /* process a skip request */ + if (state->seek) { + state->seek = 0; + if (gz_skip(state, state->skip) == -1) + return -1; + } + + /* can't push EOF */ + if (c < 0) + return -1; + + /* if output buffer empty, put byte at end (allows more pushing) */ + if (state->x.have == 0) { + state->x.have = 1; + state->x.next = state->out + (state->size << 1) - 1; + state->x.next[0] = c; + state->x.pos--; + state->past = 0; + return c; + } + + /* if no room, give up (must have already done a gzungetc()) */ + if (state->x.have == (state->size << 1)) { + gz_error(state, Z_DATA_ERROR, "out of room to push characters"); + return -1; + } + + /* slide output data if needed and insert byte before existing data */ + if (state->x.next == state->out) { + unsigned char *src = state->out + state->x.have; + unsigned char *dest = state->out + (state->size << 1); + while (src > state->out) + *--dest = *--src; + state->x.next = dest; + } + state->x.have++; + state->x.next--; + state->x.next[0] = c; + state->x.pos--; + state->past = 0; + return c; +} + +/* -- see zlib.h -- */ +char * ZEXPORT gzgets(file, buf, len) + gzFile file; + char *buf; + int len; +{ + unsigned left, n; + char *str; + unsigned char *eol; + gz_statep state; + + /* check parameters and get internal structure */ + if (file == NULL || buf == NULL || len < 1) + return NULL; + state = (gz_statep)file; + + /* check that we're reading and that there's no (serious) error */ + if (state->mode != GZ_READ || + (state->err != Z_OK && state->err != Z_BUF_ERROR)) + return NULL; + + /* process a skip request */ + if (state->seek) { + state->seek = 0; + if (gz_skip(state, state->skip) == -1) + return NULL; + } + + /* copy output bytes up to new line or len - 1, whichever comes first -- + append a terminating zero to the string (we don't check for a zero in + the contents, let the user worry about that) */ + str = buf; + left = (unsigned)len - 1; + if (left) do { + /* assure that something is in the output buffer */ + if (state->x.have == 0 && gz_fetch(state) == -1) + return NULL; /* error */ + if (state->x.have == 0) { /* end of file */ + state->past = 1; /* read past end */ + break; /* return what we have */ + } + + /* look for end-of-line in current output buffer */ + n = state->x.have > left ? left : state->x.have; + eol = (unsigned char *)memchr(state->x.next, '\n', n); + if (eol != NULL) + n = (unsigned)(eol - state->x.next) + 1; + + /* copy through end-of-line, or remainder if not found */ + memcpy(buf, state->x.next, n); + state->x.have -= n; + state->x.next += n; + state->x.pos += n; + left -= n; + buf += n; + } while (left && eol == NULL); + + /* return terminated string, or if nothing, end of file */ + if (buf == str) + return NULL; + buf[0] = 0; + return str; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzdirect(file) + gzFile file; +{ + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return 0; + state = (gz_statep)file; + + /* if the state is not known, but we can find out, then do so (this is + mainly for right after a gzopen() or gzdopen()) */ + if (state->mode == GZ_READ && state->how == LOOK && state->x.have == 0) + (void)gz_look(state); + + /* return 1 if transparent, 0 if processing a gzip stream */ + return state->direct; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzclose_r(file) + gzFile file; +{ + int ret, err; + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + + /* check that we're reading */ + if (state->mode != GZ_READ) + return Z_STREAM_ERROR; + + /* free memory and close file */ + if (state->size) { + inflateEnd(&(state->strm)); + free(state->out); + free(state->in); + } + err = state->err == Z_BUF_ERROR ? Z_BUF_ERROR : Z_OK; + gz_error(state, Z_OK, NULL); + free(state->path); + ret = close(state->fd); + free(state); + return ret ? Z_ERRNO : err; +} diff --git a/fdbrpc/zlib/gzwrite.c b/fdbrpc/zlib/gzwrite.c new file mode 100644 index 0000000000..aa767fbf63 --- /dev/null +++ b/fdbrpc/zlib/gzwrite.c @@ -0,0 +1,577 @@ +/* gzwrite.c -- zlib functions for writing gzip files + * Copyright (C) 2004, 2005, 2010, 2011, 2012, 2013 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "gzguts.h" + +/* Local functions */ +local int gz_init OF((gz_statep)); +local int gz_comp OF((gz_statep, int)); +local int gz_zero OF((gz_statep, z_off64_t)); + +/* Initialize state for writing a gzip file. Mark initialization by setting + state->size to non-zero. Return -1 on failure or 0 on success. */ +local int gz_init(state) + gz_statep state; +{ + int ret; + z_streamp strm = &(state->strm); + + /* allocate input buffer */ + state->in = (unsigned char *)malloc(state->want); + if (state->in == NULL) { + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + + /* only need output buffer and deflate state if compressing */ + if (!state->direct) { + /* allocate output buffer */ + state->out = (unsigned char *)malloc(state->want); + if (state->out == NULL) { + free(state->in); + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + + /* allocate deflate memory, set up for gzip compression */ + strm->zalloc = Z_NULL; + strm->zfree = Z_NULL; + strm->opaque = Z_NULL; + ret = deflateInit2(strm, state->level, Z_DEFLATED, + MAX_WBITS + 16, DEF_MEM_LEVEL, state->strategy); + if (ret != Z_OK) { + free(state->out); + free(state->in); + gz_error(state, Z_MEM_ERROR, "out of memory"); + return -1; + } + } + + /* mark state as initialized */ + state->size = state->want; + + /* initialize write buffer if compressing */ + if (!state->direct) { + strm->avail_out = state->size; + strm->next_out = state->out; + state->x.next = strm->next_out; + } + return 0; +} + +/* Compress whatever is at avail_in and next_in and write to the output file. + Return -1 if there is an error writing to the output file, otherwise 0. + flush is assumed to be a valid deflate() flush value. If flush is Z_FINISH, + then the deflate() state is reset to start a new gzip stream. If gz->direct + is true, then simply write to the output file without compressing, and + ignore flush. */ +local int gz_comp(state, flush) + gz_statep state; + int flush; +{ + int ret, got; + unsigned have; + z_streamp strm = &(state->strm); + + /* allocate memory if this is the first time through */ + if (state->size == 0 && gz_init(state) == -1) + return -1; + + /* write directly if requested */ + if (state->direct) { + got = write(state->fd, strm->next_in, strm->avail_in); + if (got < 0 || (unsigned)got != strm->avail_in) { + gz_error(state, Z_ERRNO, zstrerror()); + return -1; + } + strm->avail_in = 0; + return 0; + } + + /* run deflate() on provided input until it produces no more output */ + ret = Z_OK; + do { + /* write out current buffer contents if full, or if flushing, but if + doing Z_FINISH then don't write until we get to Z_STREAM_END */ + if (strm->avail_out == 0 || (flush != Z_NO_FLUSH && + (flush != Z_FINISH || ret == Z_STREAM_END))) { + have = (unsigned)(strm->next_out - state->x.next); + if (have && ((got = write(state->fd, state->x.next, have)) < 0 || + (unsigned)got != have)) { + gz_error(state, Z_ERRNO, zstrerror()); + return -1; + } + if (strm->avail_out == 0) { + strm->avail_out = state->size; + strm->next_out = state->out; + } + state->x.next = strm->next_out; + } + + /* compress */ + have = strm->avail_out; + ret = deflate(strm, flush); + if (ret == Z_STREAM_ERROR) { + gz_error(state, Z_STREAM_ERROR, + "internal error: deflate stream corrupt"); + return -1; + } + have -= strm->avail_out; + } while (have); + + /* if that completed a deflate stream, allow another to start */ + if (flush == Z_FINISH) + deflateReset(strm); + + /* all done, no errors */ + return 0; +} + +/* Compress len zeros to output. Return -1 on error, 0 on success. */ +local int gz_zero(state, len) + gz_statep state; + z_off64_t len; +{ + int first; + unsigned n; + z_streamp strm = &(state->strm); + + /* consume whatever's left in the input buffer */ + if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) + return -1; + + /* compress len zeros (len guaranteed > 0) */ + first = 1; + while (len) { + n = GT_OFF(state->size) || (z_off64_t)state->size > len ? + (unsigned)len : state->size; + if (first) { + memset(state->in, 0, n); + first = 0; + } + strm->avail_in = n; + strm->next_in = state->in; + state->x.pos += n; + if (gz_comp(state, Z_NO_FLUSH) == -1) + return -1; + len -= n; + } + return 0; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzwrite(file, buf, len) + gzFile file; + voidpc buf; + unsigned len; +{ + unsigned put = len; + gz_statep state; + z_streamp strm; + + /* get internal structure */ + if (file == NULL) + return 0; + state = (gz_statep)file; + strm = &(state->strm); + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return 0; + + /* since an int is returned, make sure len fits in one, otherwise return + with an error (this avoids the flaw in the interface) */ + if ((int)len < 0) { + gz_error(state, Z_DATA_ERROR, "requested length does not fit in int"); + return 0; + } + + /* if len is zero, avoid unnecessary operations */ + if (len == 0) + return 0; + + /* allocate memory if this is the first time through */ + if (state->size == 0 && gz_init(state) == -1) + return 0; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return 0; + } + + /* for small len, copy to input buffer, otherwise compress directly */ + if (len < state->size) { + /* copy to input buffer, compress when full */ + do { + unsigned have, copy; + + if (strm->avail_in == 0) + strm->next_in = state->in; + have = (unsigned)((strm->next_in + strm->avail_in) - state->in); + copy = state->size - have; + if (copy > len) + copy = len; + memcpy(state->in + have, buf, copy); + strm->avail_in += copy; + state->x.pos += copy; + buf = (const char *)buf + copy; + len -= copy; + if (len && gz_comp(state, Z_NO_FLUSH) == -1) + return 0; + } while (len); + } + else { + /* consume whatever's left in the input buffer */ + if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) + return 0; + + /* directly compress user buffer to file */ + strm->avail_in = len; + strm->next_in = (z_const Bytef *)buf; + state->x.pos += len; + if (gz_comp(state, Z_NO_FLUSH) == -1) + return 0; + } + + /* input was all buffered or compressed (put will fit in int) */ + return (int)put; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzputc(file, c) + gzFile file; + int c; +{ + unsigned have; + unsigned char buf[1]; + gz_statep state; + z_streamp strm; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + strm = &(state->strm); + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return -1; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return -1; + } + + /* try writing to input buffer for speed (state->size == 0 if buffer not + initialized) */ + if (state->size) { + if (strm->avail_in == 0) + strm->next_in = state->in; + have = (unsigned)((strm->next_in + strm->avail_in) - state->in); + if (have < state->size) { + state->in[have] = c; + strm->avail_in++; + state->x.pos++; + return c & 0xff; + } + } + + /* no room in buffer or not initialized, use gz_write() */ + buf[0] = c; + if (gzwrite(file, buf, 1) != 1) + return -1; + return c & 0xff; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzputs(file, str) + gzFile file; + const char *str; +{ + int ret; + unsigned len; + + /* write string */ + len = (unsigned)strlen(str); + ret = gzwrite(file, str, len); + return ret == 0 && len != 0 ? -1 : ret; +} + +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +#include + +/* -- see zlib.h -- */ +int ZEXPORTVA gzvprintf(gzFile file, const char *format, va_list va) +{ + int size, len; + gz_statep state; + z_streamp strm; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + strm = &(state->strm); + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return 0; + + /* make sure we have some buffer space */ + if (state->size == 0 && gz_init(state) == -1) + return 0; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return 0; + } + + /* consume whatever's left in the input buffer */ + if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) + return 0; + + /* do the printf() into the input buffer, put length in len */ + size = (int)(state->size); + state->in[size - 1] = 0; +#ifdef NO_vsnprintf +# ifdef HAS_vsprintf_void + (void)vsprintf((char *)(state->in), format, va); + for (len = 0; len < size; len++) + if (state->in[len] == 0) break; +# else + len = vsprintf((char *)(state->in), format, va); +# endif +#else +# ifdef HAS_vsnprintf_void + (void)vsnprintf((char *)(state->in), size, format, va); + len = strlen((char *)(state->in)); +# else + len = vsnprintf((char *)(state->in), size, format, va); +# endif +#endif + + /* check that printf() results fit in buffer */ + if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) + return 0; + + /* update buffer and position, defer compression until needed */ + strm->avail_in = (unsigned)len; + strm->next_in = state->in; + state->x.pos += len; + return len; +} + +int ZEXPORTVA gzprintf(gzFile file, const char *format, ...) +{ + va_list va; + int ret; + + va_start(va, format); + ret = gzvprintf(file, format, va); + va_end(va); + return ret; +} + +#else /* !STDC && !Z_HAVE_STDARG_H */ + +/* -- see zlib.h -- */ +int ZEXPORTVA gzprintf (file, format, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, + a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) + gzFile file; + const char *format; + int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, + a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; +{ + int size, len; + gz_statep state; + z_streamp strm; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + strm = &(state->strm); + + /* check that can really pass pointer in ints */ + if (sizeof(int) != sizeof(void *)) + return 0; + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return 0; + + /* make sure we have some buffer space */ + if (state->size == 0 && gz_init(state) == -1) + return 0; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return 0; + } + + /* consume whatever's left in the input buffer */ + if (strm->avail_in && gz_comp(state, Z_NO_FLUSH) == -1) + return 0; + + /* do the printf() into the input buffer, put length in len */ + size = (int)(state->size); + state->in[size - 1] = 0; +#ifdef NO_snprintf +# ifdef HAS_sprintf_void + sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, + a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); + for (len = 0; len < size; len++) + if (state->in[len] == 0) break; +# else + len = sprintf((char *)(state->in), format, a1, a2, a3, a4, a5, a6, a7, a8, + a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); +# endif +#else +# ifdef HAS_snprintf_void + snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, a7, a8, + a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20); + len = strlen((char *)(state->in)); +# else + len = snprintf((char *)(state->in), size, format, a1, a2, a3, a4, a5, a6, + a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, + a19, a20); +# endif +#endif + + /* check that printf() results fit in buffer */ + if (len <= 0 || len >= (int)size || state->in[size - 1] != 0) + return 0; + + /* update buffer and position, defer compression until needed */ + strm->avail_in = (unsigned)len; + strm->next_in = state->in; + state->x.pos += len; + return len; +} + +#endif + +/* -- see zlib.h -- */ +int ZEXPORT gzflush(file, flush) + gzFile file; + int flush; +{ + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return -1; + state = (gz_statep)file; + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return Z_STREAM_ERROR; + + /* check flush parameter */ + if (flush < 0 || flush > Z_FINISH) + return Z_STREAM_ERROR; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return -1; + } + + /* compress remaining data with requested flush */ + gz_comp(state, flush); + return state->err; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzsetparams(file, level, strategy) + gzFile file; + int level; + int strategy; +{ + gz_statep state; + z_streamp strm; + + /* get internal structure */ + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + strm = &(state->strm); + + /* check that we're writing and that there's no error */ + if (state->mode != GZ_WRITE || state->err != Z_OK) + return Z_STREAM_ERROR; + + /* if no change is requested, then do nothing */ + if (level == state->level && strategy == state->strategy) + return Z_OK; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + return -1; + } + + /* change compression parameters for subsequent input */ + if (state->size) { + /* flush previous input with previous parameters before changing */ + if (strm->avail_in && gz_comp(state, Z_PARTIAL_FLUSH) == -1) + return state->err; + deflateParams(strm, level, strategy); + } + state->level = level; + state->strategy = strategy; + return Z_OK; +} + +/* -- see zlib.h -- */ +int ZEXPORT gzclose_w(file) + gzFile file; +{ + int ret = Z_OK; + gz_statep state; + + /* get internal structure */ + if (file == NULL) + return Z_STREAM_ERROR; + state = (gz_statep)file; + + /* check that we're writing */ + if (state->mode != GZ_WRITE) + return Z_STREAM_ERROR; + + /* check for seek request */ + if (state->seek) { + state->seek = 0; + if (gz_zero(state, state->skip) == -1) + ret = state->err; + } + + /* flush, free memory, and close file */ + if (gz_comp(state, Z_FINISH) == -1) + ret = state->err; + if (state->size) { + if (!state->direct) { + (void)deflateEnd(&(state->strm)); + free(state->out); + } + free(state->in); + } + gz_error(state, Z_OK, NULL); + free(state->path); + if (close(state->fd) == -1) + ret = Z_ERRNO; + free(state); + return ret; +} diff --git a/fdbrpc/zlib/infback.c b/fdbrpc/zlib/infback.c new file mode 100644 index 0000000000..f3833c2e43 --- /dev/null +++ b/fdbrpc/zlib/infback.c @@ -0,0 +1,640 @@ +/* infback.c -- inflate using a call-back interface + * Copyright (C) 1995-2011 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + This code is largely copied from inflate.c. Normally either infback.o or + inflate.o would be linked into an application--not both. The interface + with inffast.c is retained so that optimized assembler-coded versions of + inflate_fast() can be used with either inflate.c or infback.c. + */ + +#include "zutil.h" +#include "inftrees.h" +#include "inflate.h" +#include "inffast.h" + +/* function prototypes */ +local void fixedtables OF((struct inflate_state FAR *state)); + +/* + strm provides memory allocation functions in zalloc and zfree, or + Z_NULL to use the library memory allocation functions. + + windowBits is in the range 8..15, and window is a user-supplied + window and output buffer that is 2**windowBits bytes. + */ +int ZEXPORT inflateBackInit_(strm, windowBits, window, version, stream_size) +z_streamp strm; +int windowBits; +unsigned char FAR *window; +const char *version; +int stream_size; +{ + struct inflate_state FAR *state; + + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || + stream_size != (int)(sizeof(z_stream))) + return Z_VERSION_ERROR; + if (strm == Z_NULL || window == Z_NULL || + windowBits < 8 || windowBits > 15) + return Z_STREAM_ERROR; + strm->msg = Z_NULL; /* in case we return an error */ + if (strm->zalloc == (alloc_func)0) { +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zalloc = zcalloc; + strm->opaque = (voidpf)0; +#endif + } + if (strm->zfree == (free_func)0) +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zfree = zcfree; +#endif + state = (struct inflate_state FAR *)ZALLOC(strm, 1, + sizeof(struct inflate_state)); + if (state == Z_NULL) return Z_MEM_ERROR; + Tracev((stderr, "inflate: allocated\n")); + strm->state = (struct internal_state FAR *)state; + state->dmax = 32768U; + state->wbits = windowBits; + state->wsize = 1U << windowBits; + state->window = window; + state->wnext = 0; + state->whave = 0; + return Z_OK; +} + +/* + Return state with length and distance decoding tables and index sizes set to + fixed code decoding. Normally this returns fixed tables from inffixed.h. + If BUILDFIXED is defined, then instead this routine builds the tables the + first time it's called, and returns those tables the first time and + thereafter. This reduces the size of the code by about 2K bytes, in + exchange for a little execution time. However, BUILDFIXED should not be + used for threaded applications, since the rewriting of the tables and virgin + may not be thread-safe. + */ +local void fixedtables(state) +struct inflate_state FAR *state; +{ +#ifdef BUILDFIXED + static int virgin = 1; + static code *lenfix, *distfix; + static code fixed[544]; + + /* build fixed huffman tables if first call (may not be thread safe) */ + if (virgin) { + unsigned sym, bits; + static code *next; + + /* literal/length table */ + sym = 0; + while (sym < 144) state->lens[sym++] = 8; + while (sym < 256) state->lens[sym++] = 9; + while (sym < 280) state->lens[sym++] = 7; + while (sym < 288) state->lens[sym++] = 8; + next = fixed; + lenfix = next; + bits = 9; + inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); + + /* distance table */ + sym = 0; + while (sym < 32) state->lens[sym++] = 5; + distfix = next; + bits = 5; + inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); + + /* do this just once */ + virgin = 0; + } +#else /* !BUILDFIXED */ +# include "inffixed.h" +#endif /* BUILDFIXED */ + state->lencode = lenfix; + state->lenbits = 9; + state->distcode = distfix; + state->distbits = 5; +} + +/* Macros for inflateBack(): */ + +/* Load returned state from inflate_fast() */ +#define LOAD() \ + do { \ + put = strm->next_out; \ + left = strm->avail_out; \ + next = strm->next_in; \ + have = strm->avail_in; \ + hold = state->hold; \ + bits = state->bits; \ + } while (0) + +/* Set state from registers for inflate_fast() */ +#define RESTORE() \ + do { \ + strm->next_out = put; \ + strm->avail_out = left; \ + strm->next_in = next; \ + strm->avail_in = have; \ + state->hold = hold; \ + state->bits = bits; \ + } while (0) + +/* Clear the input bit accumulator */ +#define INITBITS() \ + do { \ + hold = 0; \ + bits = 0; \ + } while (0) + +/* Assure that some input is available. If input is requested, but denied, + then return a Z_BUF_ERROR from inflateBack(). */ +#define PULL() \ + do { \ + if (have == 0) { \ + have = in(in_desc, &next); \ + if (have == 0) { \ + next = Z_NULL; \ + ret = Z_BUF_ERROR; \ + goto inf_leave; \ + } \ + } \ + } while (0) + +/* Get a byte of input into the bit accumulator, or return from inflateBack() + with an error if there is no input available. */ +#define PULLBYTE() \ + do { \ + PULL(); \ + have--; \ + hold += (unsigned long)(*next++) << bits; \ + bits += 8; \ + } while (0) + +/* Assure that there are at least n bits in the bit accumulator. If there is + not enough available input to do that, then return from inflateBack() with + an error. */ +#define NEEDBITS(n) \ + do { \ + while (bits < (unsigned)(n)) \ + PULLBYTE(); \ + } while (0) + +/* Return the low n bits of the bit accumulator (n < 16) */ +#define BITS(n) \ + ((unsigned)hold & ((1U << (n)) - 1)) + +/* Remove n bits from the bit accumulator */ +#define DROPBITS(n) \ + do { \ + hold >>= (n); \ + bits -= (unsigned)(n); \ + } while (0) + +/* Remove zero to seven bits as needed to go to a byte boundary */ +#define BYTEBITS() \ + do { \ + hold >>= bits & 7; \ + bits -= bits & 7; \ + } while (0) + +/* Assure that some output space is available, by writing out the window + if it's full. If the write fails, return from inflateBack() with a + Z_BUF_ERROR. */ +#define ROOM() \ + do { \ + if (left == 0) { \ + put = state->window; \ + left = state->wsize; \ + state->whave = left; \ + if (out(out_desc, put, left)) { \ + ret = Z_BUF_ERROR; \ + goto inf_leave; \ + } \ + } \ + } while (0) + +/* + strm provides the memory allocation functions and window buffer on input, + and provides information on the unused input on return. For Z_DATA_ERROR + returns, strm will also provide an error message. + + in() and out() are the call-back input and output functions. When + inflateBack() needs more input, it calls in(). When inflateBack() has + filled the window with output, or when it completes with data in the + window, it calls out() to write out the data. The application must not + change the provided input until in() is called again or inflateBack() + returns. The application must not change the window/output buffer until + inflateBack() returns. + + in() and out() are called with a descriptor parameter provided in the + inflateBack() call. This parameter can be a structure that provides the + information required to do the read or write, as well as accumulated + information on the input and output such as totals and check values. + + in() should return zero on failure. out() should return non-zero on + failure. If either in() or out() fails, than inflateBack() returns a + Z_BUF_ERROR. strm->next_in can be checked for Z_NULL to see whether it + was in() or out() that caused in the error. Otherwise, inflateBack() + returns Z_STREAM_END on success, Z_DATA_ERROR for an deflate format + error, or Z_MEM_ERROR if it could not allocate memory for the state. + inflateBack() can also return Z_STREAM_ERROR if the input parameters + are not correct, i.e. strm is Z_NULL or the state was not initialized. + */ +int ZEXPORT inflateBack(strm, in, in_desc, out, out_desc) +z_streamp strm; +in_func in; +void FAR *in_desc; +out_func out; +void FAR *out_desc; +{ + struct inflate_state FAR *state; + z_const unsigned char FAR *next; /* next input */ + unsigned char FAR *put; /* next output */ + unsigned have, left; /* available input and output */ + unsigned long hold; /* bit buffer */ + unsigned bits; /* bits in bit buffer */ + unsigned copy; /* number of stored or match bytes to copy */ + unsigned char FAR *from; /* where to copy match bytes from */ + code here; /* current decoding table entry */ + code last; /* parent table entry */ + unsigned len; /* length to copy for repeats, bits to drop */ + int ret; /* return code */ + static const unsigned short order[19] = /* permutation of code lengths */ + {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; + + /* Check that the strm exists and that the state was initialized */ + if (strm == Z_NULL || strm->state == Z_NULL) + return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + + /* Reset the state */ + strm->msg = Z_NULL; + state->mode = TYPE; + state->last = 0; + state->whave = 0; + next = strm->next_in; + have = next != Z_NULL ? strm->avail_in : 0; + hold = 0; + bits = 0; + put = state->window; + left = state->wsize; + + /* Inflate until end of block marked as last */ + for (;;) + switch (state->mode) { + case TYPE: + /* determine and dispatch block type */ + if (state->last) { + BYTEBITS(); + state->mode = DONE; + break; + } + NEEDBITS(3); + state->last = BITS(1); + DROPBITS(1); + switch (BITS(2)) { + case 0: /* stored block */ + Tracev((stderr, "inflate: stored block%s\n", + state->last ? " (last)" : "")); + state->mode = STORED; + break; + case 1: /* fixed block */ + fixedtables(state); + Tracev((stderr, "inflate: fixed codes block%s\n", + state->last ? " (last)" : "")); + state->mode = LEN; /* decode codes */ + break; + case 2: /* dynamic block */ + Tracev((stderr, "inflate: dynamic codes block%s\n", + state->last ? " (last)" : "")); + state->mode = TABLE; + break; + case 3: + strm->msg = (char *)"invalid block type"; + state->mode = BAD; + } + DROPBITS(2); + break; + + case STORED: + /* get and verify stored block length */ + BYTEBITS(); /* go to byte boundary */ + NEEDBITS(32); + if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { + strm->msg = (char *)"invalid stored block lengths"; + state->mode = BAD; + break; + } + state->length = (unsigned)hold & 0xffff; + Tracev((stderr, "inflate: stored length %u\n", + state->length)); + INITBITS(); + + /* copy stored block from input to output */ + while (state->length != 0) { + copy = state->length; + PULL(); + ROOM(); + if (copy > have) copy = have; + if (copy > left) copy = left; + zmemcpy(put, next, copy); + have -= copy; + next += copy; + left -= copy; + put += copy; + state->length -= copy; + } + Tracev((stderr, "inflate: stored end\n")); + state->mode = TYPE; + break; + + case TABLE: + /* get dynamic table entries descriptor */ + NEEDBITS(14); + state->nlen = BITS(5) + 257; + DROPBITS(5); + state->ndist = BITS(5) + 1; + DROPBITS(5); + state->ncode = BITS(4) + 4; + DROPBITS(4); +#ifndef PKZIP_BUG_WORKAROUND + if (state->nlen > 286 || state->ndist > 30) { + strm->msg = (char *)"too many length or distance symbols"; + state->mode = BAD; + break; + } +#endif + Tracev((stderr, "inflate: table sizes ok\n")); + + /* get code length code lengths (not a typo) */ + state->have = 0; + while (state->have < state->ncode) { + NEEDBITS(3); + state->lens[order[state->have++]] = (unsigned short)BITS(3); + DROPBITS(3); + } + while (state->have < 19) + state->lens[order[state->have++]] = 0; + state->next = state->codes; + state->lencode = (code const FAR *)(state->next); + state->lenbits = 7; + ret = inflate_table(CODES, state->lens, 19, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid code lengths set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: code lengths ok\n")); + + /* get length and distance code code lengths */ + state->have = 0; + while (state->have < state->nlen + state->ndist) { + for (;;) { + here = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if (here.val < 16) { + DROPBITS(here.bits); + state->lens[state->have++] = here.val; + } + else { + if (here.val == 16) { + NEEDBITS(here.bits + 2); + DROPBITS(here.bits); + if (state->have == 0) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + len = (unsigned)(state->lens[state->have - 1]); + copy = 3 + BITS(2); + DROPBITS(2); + } + else if (here.val == 17) { + NEEDBITS(here.bits + 3); + DROPBITS(here.bits); + len = 0; + copy = 3 + BITS(3); + DROPBITS(3); + } + else { + NEEDBITS(here.bits + 7); + DROPBITS(here.bits); + len = 0; + copy = 11 + BITS(7); + DROPBITS(7); + } + if (state->have + copy > state->nlen + state->ndist) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + while (copy--) + state->lens[state->have++] = (unsigned short)len; + } + } + + /* handle error breaks in while */ + if (state->mode == BAD) break; + + /* check for end-of-block code (better have one) */ + if (state->lens[256] == 0) { + strm->msg = (char *)"invalid code -- missing end-of-block"; + state->mode = BAD; + break; + } + + /* build code tables -- note: do not change the lenbits or distbits + values here (9 and 6) without reading the comments in inftrees.h + concerning the ENOUGH constants, which depend on those values */ + state->next = state->codes; + state->lencode = (code const FAR *)(state->next); + state->lenbits = 9; + ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid literal/lengths set"; + state->mode = BAD; + break; + } + state->distcode = (code const FAR *)(state->next); + state->distbits = 6; + ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, + &(state->next), &(state->distbits), state->work); + if (ret) { + strm->msg = (char *)"invalid distances set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: codes ok\n")); + state->mode = LEN; + + case LEN: + /* use inflate_fast() if we have enough input and output */ + if (have >= 6 && left >= 258) { + RESTORE(); + if (state->whave < state->wsize) + state->whave = state->wsize - left; + inflate_fast(strm, state->wsize); + LOAD(); + break; + } + + /* get a literal, length, or end-of-block code */ + for (;;) { + here = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if (here.op && (here.op & 0xf0) == 0) { + last = here; + for (;;) { + here = state->lencode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + here.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + } + DROPBITS(here.bits); + state->length = (unsigned)here.val; + + /* process literal */ + if (here.op == 0) { + Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", here.val)); + ROOM(); + *put++ = (unsigned char)(state->length); + left--; + state->mode = LEN; + break; + } + + /* process end of block */ + if (here.op & 32) { + Tracevv((stderr, "inflate: end of block\n")); + state->mode = TYPE; + break; + } + + /* invalid code */ + if (here.op & 64) { + strm->msg = (char *)"invalid literal/length code"; + state->mode = BAD; + break; + } + + /* length code -- get extra bits, if any */ + state->extra = (unsigned)(here.op) & 15; + if (state->extra != 0) { + NEEDBITS(state->extra); + state->length += BITS(state->extra); + DROPBITS(state->extra); + } + Tracevv((stderr, "inflate: length %u\n", state->length)); + + /* get distance code */ + for (;;) { + here = state->distcode[BITS(state->distbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if ((here.op & 0xf0) == 0) { + last = here; + for (;;) { + here = state->distcode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + here.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + } + DROPBITS(here.bits); + if (here.op & 64) { + strm->msg = (char *)"invalid distance code"; + state->mode = BAD; + break; + } + state->offset = (unsigned)here.val; + + /* get distance extra bits, if any */ + state->extra = (unsigned)(here.op) & 15; + if (state->extra != 0) { + NEEDBITS(state->extra); + state->offset += BITS(state->extra); + DROPBITS(state->extra); + } + if (state->offset > state->wsize - (state->whave < state->wsize ? + left : 0)) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } + Tracevv((stderr, "inflate: distance %u\n", state->offset)); + + /* copy match from window to output */ + do { + ROOM(); + copy = state->wsize - state->offset; + if (copy < left) { + from = put + copy; + copy = left - copy; + } + else { + from = put - state->offset; + copy = left; + } + if (copy > state->length) copy = state->length; + state->length -= copy; + left -= copy; + do { + *put++ = *from++; + } while (--copy); + } while (state->length != 0); + break; + + case DONE: + /* inflate stream terminated properly -- write leftover output */ + ret = Z_STREAM_END; + if (left < state->wsize) { + if (out(out_desc, state->window, state->wsize - left)) + ret = Z_BUF_ERROR; + } + goto inf_leave; + + case BAD: + ret = Z_DATA_ERROR; + goto inf_leave; + + default: /* can't happen, but makes compilers happy */ + ret = Z_STREAM_ERROR; + goto inf_leave; + } + + /* Return unused input */ + inf_leave: + strm->next_in = next; + strm->avail_in = have; + return ret; +} + +int ZEXPORT inflateBackEnd(strm) +z_streamp strm; +{ + if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) + return Z_STREAM_ERROR; + ZFREE(strm, strm->state); + strm->state = Z_NULL; + Tracev((stderr, "inflate: end\n")); + return Z_OK; +} diff --git a/fdbrpc/zlib/inffast.c b/fdbrpc/zlib/inffast.c new file mode 100644 index 0000000000..bda59ceb6a --- /dev/null +++ b/fdbrpc/zlib/inffast.c @@ -0,0 +1,340 @@ +/* inffast.c -- fast decoding + * Copyright (C) 1995-2008, 2010, 2013 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "zutil.h" +#include "inftrees.h" +#include "inflate.h" +#include "inffast.h" + +#ifndef ASMINF + +/* Allow machine dependent optimization for post-increment or pre-increment. + Based on testing to date, + Pre-increment preferred for: + - PowerPC G3 (Adler) + - MIPS R5000 (Randers-Pehrson) + Post-increment preferred for: + - none + No measurable difference: + - Pentium III (Anderson) + - M68060 (Nikl) + */ +#ifdef POSTINC +# define OFF 0 +# define PUP(a) *(a)++ +#else +# define OFF 1 +# define PUP(a) *++(a) +#endif + +/* + Decode literal, length, and distance codes and write out the resulting + literal and match bytes until either not enough input or output is + available, an end-of-block is encountered, or a data error is encountered. + When large enough input and output buffers are supplied to inflate(), for + example, a 16K input buffer and a 64K output buffer, more than 95% of the + inflate execution time is spent in this routine. + + Entry assumptions: + + state->mode == LEN + strm->avail_in >= 6 + strm->avail_out >= 258 + start >= strm->avail_out + state->bits < 8 + + On return, state->mode is one of: + + LEN -- ran out of enough output space or enough available input + TYPE -- reached end of block code, inflate() to interpret next block + BAD -- error in block data + + Notes: + + - The maximum input bits used by a length/distance pair is 15 bits for the + length code, 5 bits for the length extra, 15 bits for the distance code, + and 13 bits for the distance extra. This totals 48 bits, or six bytes. + Therefore if strm->avail_in >= 6, then there is enough input to avoid + checking for available input while decoding. + + - The maximum bytes that a single length/distance pair can output is 258 + bytes, which is the maximum length that can be coded. inflate_fast() + requires strm->avail_out >= 258 for each loop to avoid checking for + output space. + */ +void ZLIB_INTERNAL inflate_fast(strm, start) +z_streamp strm; +unsigned start; /* inflate()'s starting value for strm->avail_out */ +{ + struct inflate_state FAR *state; + z_const unsigned char FAR *in; /* local strm->next_in */ + z_const unsigned char FAR *last; /* have enough input while in < last */ + unsigned char FAR *out; /* local strm->next_out */ + unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ + unsigned char FAR *end; /* while out < end, enough space available */ +#ifdef INFLATE_STRICT + unsigned dmax; /* maximum distance from zlib header */ +#endif + unsigned wsize; /* window size or zero if not using window */ + unsigned whave; /* valid bytes in the window */ + unsigned wnext; /* window write index */ + unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */ + unsigned long hold; /* local strm->hold */ + unsigned bits; /* local strm->bits */ + code const FAR *lcode; /* local strm->lencode */ + code const FAR *dcode; /* local strm->distcode */ + unsigned lmask; /* mask for first level of length codes */ + unsigned dmask; /* mask for first level of distance codes */ + code here; /* retrieved table entry */ + unsigned op; /* code bits, operation, extra bits, or */ + /* window position, window bytes to copy */ + unsigned len; /* match length, unused bytes */ + unsigned dist; /* match distance */ + unsigned char FAR *from; /* where to copy match from */ + + /* copy state to local variables */ + state = (struct inflate_state FAR *)strm->state; + in = strm->next_in - OFF; + last = in + (strm->avail_in - 5); + out = strm->next_out - OFF; + beg = out - (start - strm->avail_out); + end = out + (strm->avail_out - 257); +#ifdef INFLATE_STRICT + dmax = state->dmax; +#endif + wsize = state->wsize; + whave = state->whave; + wnext = state->wnext; + window = state->window; + hold = state->hold; + bits = state->bits; + lcode = state->lencode; + dcode = state->distcode; + lmask = (1U << state->lenbits) - 1; + dmask = (1U << state->distbits) - 1; + + /* decode literals and length/distances until end-of-block or not enough + input data or output space */ + do { + if (bits < 15) { + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + } + here = lcode[hold & lmask]; + dolen: + op = (unsigned)(here.bits); + hold >>= op; + bits -= op; + op = (unsigned)(here.op); + if (op == 0) { /* literal */ + Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", here.val)); + PUP(out) = (unsigned char)(here.val); + } + else if (op & 16) { /* length base */ + len = (unsigned)(here.val); + op &= 15; /* number of extra bits */ + if (op) { + if (bits < op) { + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + } + len += (unsigned)hold & ((1U << op) - 1); + hold >>= op; + bits -= op; + } + Tracevv((stderr, "inflate: length %u\n", len)); + if (bits < 15) { + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + } + here = dcode[hold & dmask]; + dodist: + op = (unsigned)(here.bits); + hold >>= op; + bits -= op; + op = (unsigned)(here.op); + if (op & 16) { /* distance base */ + dist = (unsigned)(here.val); + op &= 15; /* number of extra bits */ + if (bits < op) { + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + if (bits < op) { + hold += (unsigned long)(PUP(in)) << bits; + bits += 8; + } + } + dist += (unsigned)hold & ((1U << op) - 1); +#ifdef INFLATE_STRICT + if (dist > dmax) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } +#endif + hold >>= op; + bits -= op; + Tracevv((stderr, "inflate: distance %u\n", dist)); + op = (unsigned)(out - beg); /* max distance in output */ + if (dist > op) { /* see if copy from window */ + op = dist - op; /* distance back in window */ + if (op > whave) { + if (state->sane) { + strm->msg = + (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } +#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR + if (len <= op - whave) { + do { + PUP(out) = 0; + } while (--len); + continue; + } + len -= op - whave; + do { + PUP(out) = 0; + } while (--op > whave); + if (op == 0) { + from = out - dist; + do { + PUP(out) = PUP(from); + } while (--len); + continue; + } +#endif + } + from = window - OFF; + if (wnext == 0) { /* very common case */ + from += wsize - op; + if (op < len) { /* some from window */ + len -= op; + do { + PUP(out) = PUP(from); + } while (--op); + from = out - dist; /* rest from output */ + } + } + else if (wnext < op) { /* wrap around window */ + from += wsize + wnext - op; + op -= wnext; + if (op < len) { /* some from end of window */ + len -= op; + do { + PUP(out) = PUP(from); + } while (--op); + from = window - OFF; + if (wnext < len) { /* some from start of window */ + op = wnext; + len -= op; + do { + PUP(out) = PUP(from); + } while (--op); + from = out - dist; /* rest from output */ + } + } + } + else { /* contiguous in window */ + from += wnext - op; + if (op < len) { /* some from window */ + len -= op; + do { + PUP(out) = PUP(from); + } while (--op); + from = out - dist; /* rest from output */ + } + } + while (len > 2) { + PUP(out) = PUP(from); + PUP(out) = PUP(from); + PUP(out) = PUP(from); + len -= 3; + } + if (len) { + PUP(out) = PUP(from); + if (len > 1) + PUP(out) = PUP(from); + } + } + else { + from = out - dist; /* copy direct from output */ + do { /* minimum length is three */ + PUP(out) = PUP(from); + PUP(out) = PUP(from); + PUP(out) = PUP(from); + len -= 3; + } while (len > 2); + if (len) { + PUP(out) = PUP(from); + if (len > 1) + PUP(out) = PUP(from); + } + } + } + else if ((op & 64) == 0) { /* 2nd level distance code */ + here = dcode[here.val + (hold & ((1U << op) - 1))]; + goto dodist; + } + else { + strm->msg = (char *)"invalid distance code"; + state->mode = BAD; + break; + } + } + else if ((op & 64) == 0) { /* 2nd level length code */ + here = lcode[here.val + (hold & ((1U << op) - 1))]; + goto dolen; + } + else if (op & 32) { /* end-of-block */ + Tracevv((stderr, "inflate: end of block\n")); + state->mode = TYPE; + break; + } + else { + strm->msg = (char *)"invalid literal/length code"; + state->mode = BAD; + break; + } + } while (in < last && out < end); + + /* return unused bytes (on entry, bits < 8, so in won't go too far back) */ + len = bits >> 3; + in -= len; + bits -= len << 3; + hold &= (1U << bits) - 1; + + /* update state and return */ + strm->next_in = in + OFF; + strm->next_out = out + OFF; + strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last)); + strm->avail_out = (unsigned)(out < end ? + 257 + (end - out) : 257 - (out - end)); + state->hold = hold; + state->bits = bits; + return; +} + +/* + inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe): + - Using bit fields for code structure + - Different op definition to avoid & for extra bits (do & for table bits) + - Three separate decoding do-loops for direct, window, and wnext == 0 + - Special case for distance > 1 copies to do overlapped load and store copy + - Explicit branch predictions (based on measured branch probabilities) + - Deferring match copy and interspersed it with decoding subsequent codes + - Swapping literal/length else + - Swapping window/direct else + - Larger unrolled copy loops (three is about right) + - Moving len -= 3 statement into middle of loop + */ + +#endif /* !ASMINF */ diff --git a/fdbrpc/zlib/inffast.h b/fdbrpc/zlib/inffast.h new file mode 100644 index 0000000000..e5c1aa4ca8 --- /dev/null +++ b/fdbrpc/zlib/inffast.h @@ -0,0 +1,11 @@ +/* inffast.h -- header to use inffast.c + * Copyright (C) 1995-2003, 2010 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +void ZLIB_INTERNAL inflate_fast OF((z_streamp strm, unsigned start)); diff --git a/fdbrpc/zlib/inffixed.h b/fdbrpc/zlib/inffixed.h new file mode 100644 index 0000000000..acd4d8a205 --- /dev/null +++ b/fdbrpc/zlib/inffixed.h @@ -0,0 +1,114 @@ +/* + * inffixed.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + /* inffixed.h -- table for decoding fixed codes + * Generated automatically by makefixed(). + */ + + /* WARNING: this file should *not* be used by applications. + It is part of the implementation of this library and is + subject to change. Applications should only use zlib.h. + */ + + static const code lenfix[512] = { + {96,7,0},{0,8,80},{0,8,16},{20,8,115},{18,7,31},{0,8,112},{0,8,48}, + {0,9,192},{16,7,10},{0,8,96},{0,8,32},{0,9,160},{0,8,0},{0,8,128}, + {0,8,64},{0,9,224},{16,7,6},{0,8,88},{0,8,24},{0,9,144},{19,7,59}, + {0,8,120},{0,8,56},{0,9,208},{17,7,17},{0,8,104},{0,8,40},{0,9,176}, + {0,8,8},{0,8,136},{0,8,72},{0,9,240},{16,7,4},{0,8,84},{0,8,20}, + {21,8,227},{19,7,43},{0,8,116},{0,8,52},{0,9,200},{17,7,13},{0,8,100}, + {0,8,36},{0,9,168},{0,8,4},{0,8,132},{0,8,68},{0,9,232},{16,7,8}, + {0,8,92},{0,8,28},{0,9,152},{20,7,83},{0,8,124},{0,8,60},{0,9,216}, + {18,7,23},{0,8,108},{0,8,44},{0,9,184},{0,8,12},{0,8,140},{0,8,76}, + {0,9,248},{16,7,3},{0,8,82},{0,8,18},{21,8,163},{19,7,35},{0,8,114}, + {0,8,50},{0,9,196},{17,7,11},{0,8,98},{0,8,34},{0,9,164},{0,8,2}, + {0,8,130},{0,8,66},{0,9,228},{16,7,7},{0,8,90},{0,8,26},{0,9,148}, + {20,7,67},{0,8,122},{0,8,58},{0,9,212},{18,7,19},{0,8,106},{0,8,42}, + {0,9,180},{0,8,10},{0,8,138},{0,8,74},{0,9,244},{16,7,5},{0,8,86}, + {0,8,22},{64,8,0},{19,7,51},{0,8,118},{0,8,54},{0,9,204},{17,7,15}, + {0,8,102},{0,8,38},{0,9,172},{0,8,6},{0,8,134},{0,8,70},{0,9,236}, + {16,7,9},{0,8,94},{0,8,30},{0,9,156},{20,7,99},{0,8,126},{0,8,62}, + {0,9,220},{18,7,27},{0,8,110},{0,8,46},{0,9,188},{0,8,14},{0,8,142}, + {0,8,78},{0,9,252},{96,7,0},{0,8,81},{0,8,17},{21,8,131},{18,7,31}, + {0,8,113},{0,8,49},{0,9,194},{16,7,10},{0,8,97},{0,8,33},{0,9,162}, + {0,8,1},{0,8,129},{0,8,65},{0,9,226},{16,7,6},{0,8,89},{0,8,25}, + {0,9,146},{19,7,59},{0,8,121},{0,8,57},{0,9,210},{17,7,17},{0,8,105}, + {0,8,41},{0,9,178},{0,8,9},{0,8,137},{0,8,73},{0,9,242},{16,7,4}, + {0,8,85},{0,8,21},{16,8,258},{19,7,43},{0,8,117},{0,8,53},{0,9,202}, + {17,7,13},{0,8,101},{0,8,37},{0,9,170},{0,8,5},{0,8,133},{0,8,69}, + {0,9,234},{16,7,8},{0,8,93},{0,8,29},{0,9,154},{20,7,83},{0,8,125}, + {0,8,61},{0,9,218},{18,7,23},{0,8,109},{0,8,45},{0,9,186},{0,8,13}, + {0,8,141},{0,8,77},{0,9,250},{16,7,3},{0,8,83},{0,8,19},{21,8,195}, + {19,7,35},{0,8,115},{0,8,51},{0,9,198},{17,7,11},{0,8,99},{0,8,35}, + {0,9,166},{0,8,3},{0,8,131},{0,8,67},{0,9,230},{16,7,7},{0,8,91}, + {0,8,27},{0,9,150},{20,7,67},{0,8,123},{0,8,59},{0,9,214},{18,7,19}, + {0,8,107},{0,8,43},{0,9,182},{0,8,11},{0,8,139},{0,8,75},{0,9,246}, + {16,7,5},{0,8,87},{0,8,23},{64,8,0},{19,7,51},{0,8,119},{0,8,55}, + {0,9,206},{17,7,15},{0,8,103},{0,8,39},{0,9,174},{0,8,7},{0,8,135}, + {0,8,71},{0,9,238},{16,7,9},{0,8,95},{0,8,31},{0,9,158},{20,7,99}, + {0,8,127},{0,8,63},{0,9,222},{18,7,27},{0,8,111},{0,8,47},{0,9,190}, + {0,8,15},{0,8,143},{0,8,79},{0,9,254},{96,7,0},{0,8,80},{0,8,16}, + {20,8,115},{18,7,31},{0,8,112},{0,8,48},{0,9,193},{16,7,10},{0,8,96}, + {0,8,32},{0,9,161},{0,8,0},{0,8,128},{0,8,64},{0,9,225},{16,7,6}, + {0,8,88},{0,8,24},{0,9,145},{19,7,59},{0,8,120},{0,8,56},{0,9,209}, + {17,7,17},{0,8,104},{0,8,40},{0,9,177},{0,8,8},{0,8,136},{0,8,72}, + {0,9,241},{16,7,4},{0,8,84},{0,8,20},{21,8,227},{19,7,43},{0,8,116}, + {0,8,52},{0,9,201},{17,7,13},{0,8,100},{0,8,36},{0,9,169},{0,8,4}, + {0,8,132},{0,8,68},{0,9,233},{16,7,8},{0,8,92},{0,8,28},{0,9,153}, + {20,7,83},{0,8,124},{0,8,60},{0,9,217},{18,7,23},{0,8,108},{0,8,44}, + {0,9,185},{0,8,12},{0,8,140},{0,8,76},{0,9,249},{16,7,3},{0,8,82}, + {0,8,18},{21,8,163},{19,7,35},{0,8,114},{0,8,50},{0,9,197},{17,7,11}, + {0,8,98},{0,8,34},{0,9,165},{0,8,2},{0,8,130},{0,8,66},{0,9,229}, + {16,7,7},{0,8,90},{0,8,26},{0,9,149},{20,7,67},{0,8,122},{0,8,58}, + {0,9,213},{18,7,19},{0,8,106},{0,8,42},{0,9,181},{0,8,10},{0,8,138}, + {0,8,74},{0,9,245},{16,7,5},{0,8,86},{0,8,22},{64,8,0},{19,7,51}, + {0,8,118},{0,8,54},{0,9,205},{17,7,15},{0,8,102},{0,8,38},{0,9,173}, + {0,8,6},{0,8,134},{0,8,70},{0,9,237},{16,7,9},{0,8,94},{0,8,30}, + {0,9,157},{20,7,99},{0,8,126},{0,8,62},{0,9,221},{18,7,27},{0,8,110}, + {0,8,46},{0,9,189},{0,8,14},{0,8,142},{0,8,78},{0,9,253},{96,7,0}, + {0,8,81},{0,8,17},{21,8,131},{18,7,31},{0,8,113},{0,8,49},{0,9,195}, + {16,7,10},{0,8,97},{0,8,33},{0,9,163},{0,8,1},{0,8,129},{0,8,65}, + {0,9,227},{16,7,6},{0,8,89},{0,8,25},{0,9,147},{19,7,59},{0,8,121}, + {0,8,57},{0,9,211},{17,7,17},{0,8,105},{0,8,41},{0,9,179},{0,8,9}, + {0,8,137},{0,8,73},{0,9,243},{16,7,4},{0,8,85},{0,8,21},{16,8,258}, + {19,7,43},{0,8,117},{0,8,53},{0,9,203},{17,7,13},{0,8,101},{0,8,37}, + {0,9,171},{0,8,5},{0,8,133},{0,8,69},{0,9,235},{16,7,8},{0,8,93}, + {0,8,29},{0,9,155},{20,7,83},{0,8,125},{0,8,61},{0,9,219},{18,7,23}, + {0,8,109},{0,8,45},{0,9,187},{0,8,13},{0,8,141},{0,8,77},{0,9,251}, + {16,7,3},{0,8,83},{0,8,19},{21,8,195},{19,7,35},{0,8,115},{0,8,51}, + {0,9,199},{17,7,11},{0,8,99},{0,8,35},{0,9,167},{0,8,3},{0,8,131}, + {0,8,67},{0,9,231},{16,7,7},{0,8,91},{0,8,27},{0,9,151},{20,7,67}, + {0,8,123},{0,8,59},{0,9,215},{18,7,19},{0,8,107},{0,8,43},{0,9,183}, + {0,8,11},{0,8,139},{0,8,75},{0,9,247},{16,7,5},{0,8,87},{0,8,23}, + {64,8,0},{19,7,51},{0,8,119},{0,8,55},{0,9,207},{17,7,15},{0,8,103}, + {0,8,39},{0,9,175},{0,8,7},{0,8,135},{0,8,71},{0,9,239},{16,7,9}, + {0,8,95},{0,8,31},{0,9,159},{20,7,99},{0,8,127},{0,8,63},{0,9,223}, + {18,7,27},{0,8,111},{0,8,47},{0,9,191},{0,8,15},{0,8,143},{0,8,79}, + {0,9,255} + }; + + static const code distfix[32] = { + {16,5,1},{23,5,257},{19,5,17},{27,5,4097},{17,5,5},{25,5,1025}, + {21,5,65},{29,5,16385},{16,5,3},{24,5,513},{20,5,33},{28,5,8193}, + {18,5,9},{26,5,2049},{22,5,129},{64,5,0},{16,5,2},{23,5,385}, + {19,5,25},{27,5,6145},{17,5,7},{25,5,1537},{21,5,97},{29,5,24577}, + {16,5,4},{24,5,769},{20,5,49},{28,5,12289},{18,5,13},{26,5,3073}, + {22,5,193},{64,5,0} + }; diff --git a/fdbrpc/zlib/inflate.c b/fdbrpc/zlib/inflate.c new file mode 100644 index 0000000000..fc7e7b2f4e --- /dev/null +++ b/fdbrpc/zlib/inflate.c @@ -0,0 +1,1512 @@ +/* inflate.c -- zlib decompression + * Copyright (C) 1995-2012 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + * Change history: + * + * 1.2.beta0 24 Nov 2002 + * - First version -- complete rewrite of inflate to simplify code, avoid + * creation of window when not needed, minimize use of window when it is + * needed, make inffast.c even faster, implement gzip decoding, and to + * improve code readability and style over the previous zlib inflate code + * + * 1.2.beta1 25 Nov 2002 + * - Use pointers for available input and output checking in inffast.c + * - Remove input and output counters in inffast.c + * - Change inffast.c entry and loop from avail_in >= 7 to >= 6 + * - Remove unnecessary second byte pull from length extra in inffast.c + * - Unroll direct copy to three copies per loop in inffast.c + * + * 1.2.beta2 4 Dec 2002 + * - Change external routine names to reduce potential conflicts + * - Correct filename to inffixed.h for fixed tables in inflate.c + * - Make hbuf[] unsigned char to match parameter type in inflate.c + * - Change strm->next_out[-state->offset] to *(strm->next_out - state->offset) + * to avoid negation problem on Alphas (64 bit) in inflate.c + * + * 1.2.beta3 22 Dec 2002 + * - Add comments on state->bits assertion in inffast.c + * - Add comments on op field in inftrees.h + * - Fix bug in reuse of allocated window after inflateReset() + * - Remove bit fields--back to byte structure for speed + * - Remove distance extra == 0 check in inflate_fast()--only helps for lengths + * - Change post-increments to pre-increments in inflate_fast(), PPC biased? + * - Add compile time option, POSTINC, to use post-increments instead (Intel?) + * - Make MATCH copy in inflate() much faster for when inflate_fast() not used + * - Use local copies of stream next and avail values, as well as local bit + * buffer and bit count in inflate()--for speed when inflate_fast() not used + * + * 1.2.beta4 1 Jan 2003 + * - Split ptr - 257 statements in inflate_table() to avoid compiler warnings + * - Move a comment on output buffer sizes from inffast.c to inflate.c + * - Add comments in inffast.c to introduce the inflate_fast() routine + * - Rearrange window copies in inflate_fast() for speed and simplification + * - Unroll last copy for window match in inflate_fast() + * - Use local copies of window variables in inflate_fast() for speed + * - Pull out common wnext == 0 case for speed in inflate_fast() + * - Make op and len in inflate_fast() unsigned for consistency + * - Add FAR to lcode and dcode declarations in inflate_fast() + * - Simplified bad distance check in inflate_fast() + * - Added inflateBackInit(), inflateBack(), and inflateBackEnd() in new + * source file infback.c to provide a call-back interface to inflate for + * programs like gzip and unzip -- uses window as output buffer to avoid + * window copying + * + * 1.2.beta5 1 Jan 2003 + * - Improved inflateBack() interface to allow the caller to provide initial + * input in strm. + * - Fixed stored blocks bug in inflateBack() + * + * 1.2.beta6 4 Jan 2003 + * - Added comments in inffast.c on effectiveness of POSTINC + * - Typecasting all around to reduce compiler warnings + * - Changed loops from while (1) or do {} while (1) to for (;;), again to + * make compilers happy + * - Changed type of window in inflateBackInit() to unsigned char * + * + * 1.2.beta7 27 Jan 2003 + * - Changed many types to unsigned or unsigned short to avoid warnings + * - Added inflateCopy() function + * + * 1.2.0 9 Mar 2003 + * - Changed inflateBack() interface to provide separate opaque descriptors + * for the in() and out() functions + * - Changed inflateBack() argument and in_func typedef to swap the length + * and buffer address return values for the input function + * - Check next_in and next_out for Z_NULL on entry to inflate() + * + * The history for versions after 1.2.0 are in ChangeLog in zlib distribution. + */ + +#include "zutil.h" +#include "inftrees.h" +#include "inflate.h" +#include "inffast.h" + +#ifdef MAKEFIXED +# ifndef BUILDFIXED +# define BUILDFIXED +# endif +#endif + +/* function prototypes */ +local void fixedtables OF((struct inflate_state FAR *state)); +local int updatewindow OF((z_streamp strm, const unsigned char FAR *end, + unsigned copy)); +#ifdef BUILDFIXED + void makefixed OF((void)); +#endif +local unsigned syncsearch OF((unsigned FAR *have, const unsigned char FAR *buf, + unsigned len)); + +int ZEXPORT inflateResetKeep(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + strm->total_in = strm->total_out = state->total = 0; + strm->msg = Z_NULL; + if (state->wrap) /* to support ill-conceived Java test suite */ + strm->adler = state->wrap & 1; + state->mode = HEAD; + state->last = 0; + state->havedict = 0; + state->dmax = 32768U; + state->head = Z_NULL; + state->hold = 0; + state->bits = 0; + state->lencode = state->distcode = state->next = state->codes; + state->sane = 1; + state->back = -1; + Tracev((stderr, "inflate: reset\n")); + return Z_OK; +} + +int ZEXPORT inflateReset(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + state->wsize = 0; + state->whave = 0; + state->wnext = 0; + return inflateResetKeep(strm); +} + +int ZEXPORT inflateReset2(strm, windowBits) +z_streamp strm; +int windowBits; +{ + int wrap; + struct inflate_state FAR *state; + + /* get the state */ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + + /* extract wrap request from windowBits parameter */ + if (windowBits < 0) { + wrap = 0; + windowBits = -windowBits; + } + else { + wrap = (windowBits >> 4) + 1; +#ifdef GUNZIP + if (windowBits < 48) + windowBits &= 15; +#endif + } + + /* set number of window bits, free window if different */ + if (windowBits && (windowBits < 8 || windowBits > 15)) + return Z_STREAM_ERROR; + if (state->window != Z_NULL && state->wbits != (unsigned)windowBits) { + ZFREE(strm, state->window); + state->window = Z_NULL; + } + + /* update state and reset the rest of it */ + state->wrap = wrap; + state->wbits = (unsigned)windowBits; + return inflateReset(strm); +} + +int ZEXPORT inflateInit2_(strm, windowBits, version, stream_size) +z_streamp strm; +int windowBits; +const char *version; +int stream_size; +{ + int ret; + struct inflate_state FAR *state; + + if (version == Z_NULL || version[0] != ZLIB_VERSION[0] || + stream_size != (int)(sizeof(z_stream))) + return Z_VERSION_ERROR; + if (strm == Z_NULL) return Z_STREAM_ERROR; + strm->msg = Z_NULL; /* in case we return an error */ + if (strm->zalloc == (alloc_func)0) { +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zalloc = zcalloc; + strm->opaque = (voidpf)0; +#endif + } + if (strm->zfree == (free_func)0) +#ifdef Z_SOLO + return Z_STREAM_ERROR; +#else + strm->zfree = zcfree; +#endif + state = (struct inflate_state FAR *) + ZALLOC(strm, 1, sizeof(struct inflate_state)); + if (state == Z_NULL) return Z_MEM_ERROR; + Tracev((stderr, "inflate: allocated\n")); + strm->state = (struct internal_state FAR *)state; + state->window = Z_NULL; + ret = inflateReset2(strm, windowBits); + if (ret != Z_OK) { + ZFREE(strm, state); + strm->state = Z_NULL; + } + return ret; +} + +int ZEXPORT inflateInit_(strm, version, stream_size) +z_streamp strm; +const char *version; +int stream_size; +{ + return inflateInit2_(strm, DEF_WBITS, version, stream_size); +} + +int ZEXPORT inflatePrime(strm, bits, value) +z_streamp strm; +int bits; +int value; +{ + struct inflate_state FAR *state; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (bits < 0) { + state->hold = 0; + state->bits = 0; + return Z_OK; + } + if (bits > 16 || state->bits + bits > 32) return Z_STREAM_ERROR; + value &= (1L << bits) - 1; + state->hold += value << state->bits; + state->bits += bits; + return Z_OK; +} + +/* + Return state with length and distance decoding tables and index sizes set to + fixed code decoding. Normally this returns fixed tables from inffixed.h. + If BUILDFIXED is defined, then instead this routine builds the tables the + first time it's called, and returns those tables the first time and + thereafter. This reduces the size of the code by about 2K bytes, in + exchange for a little execution time. However, BUILDFIXED should not be + used for threaded applications, since the rewriting of the tables and virgin + may not be thread-safe. + */ +local void fixedtables(state) +struct inflate_state FAR *state; +{ +#ifdef BUILDFIXED + static int virgin = 1; + static code *lenfix, *distfix; + static code fixed[544]; + + /* build fixed huffman tables if first call (may not be thread safe) */ + if (virgin) { + unsigned sym, bits; + static code *next; + + /* literal/length table */ + sym = 0; + while (sym < 144) state->lens[sym++] = 8; + while (sym < 256) state->lens[sym++] = 9; + while (sym < 280) state->lens[sym++] = 7; + while (sym < 288) state->lens[sym++] = 8; + next = fixed; + lenfix = next; + bits = 9; + inflate_table(LENS, state->lens, 288, &(next), &(bits), state->work); + + /* distance table */ + sym = 0; + while (sym < 32) state->lens[sym++] = 5; + distfix = next; + bits = 5; + inflate_table(DISTS, state->lens, 32, &(next), &(bits), state->work); + + /* do this just once */ + virgin = 0; + } +#else /* !BUILDFIXED */ +# include "inffixed.h" +#endif /* BUILDFIXED */ + state->lencode = lenfix; + state->lenbits = 9; + state->distcode = distfix; + state->distbits = 5; +} + +#ifdef MAKEFIXED +#include + +/* + Write out the inffixed.h that is #include'd above. Defining MAKEFIXED also + defines BUILDFIXED, so the tables are built on the fly. makefixed() writes + those tables to stdout, which would be piped to inffixed.h. A small program + can simply call makefixed to do this: + + void makefixed(void); + + int main(void) + { + makefixed(); + return 0; + } + + Then that can be linked with zlib built with MAKEFIXED defined and run: + + a.out > inffixed.h + */ +void makefixed() +{ + unsigned low, size; + struct inflate_state state; + + fixedtables(&state); + puts(" /* inffixed.h -- table for decoding fixed codes"); + puts(" * Generated automatically by makefixed()."); + puts(" */"); + puts(""); + puts(" /* WARNING: this file should *not* be used by applications."); + puts(" It is part of the implementation of this library and is"); + puts(" subject to change. Applications should only use zlib.h."); + puts(" */"); + puts(""); + size = 1U << 9; + printf(" static const code lenfix[%u] = {", size); + low = 0; + for (;;) { + if ((low % 7) == 0) printf("\n "); + printf("{%u,%u,%d}", (low & 127) == 99 ? 64 : state.lencode[low].op, + state.lencode[low].bits, state.lencode[low].val); + if (++low == size) break; + putchar(','); + } + puts("\n };"); + size = 1U << 5; + printf("\n static const code distfix[%u] = {", size); + low = 0; + for (;;) { + if ((low % 6) == 0) printf("\n "); + printf("{%u,%u,%d}", state.distcode[low].op, state.distcode[low].bits, + state.distcode[low].val); + if (++low == size) break; + putchar(','); + } + puts("\n };"); +} +#endif /* MAKEFIXED */ + +/* + Update the window with the last wsize (normally 32K) bytes written before + returning. If window does not exist yet, create it. This is only called + when a window is already in use, or when output has been written during this + inflate call, but the end of the deflate stream has not been reached yet. + It is also called to create a window for dictionary data when a dictionary + is loaded. + + Providing output buffers larger than 32K to inflate() should provide a speed + advantage, since only the last 32K of output is copied to the sliding window + upon return from inflate(), and since all distances after the first 32K of + output will fall in the output data, making match copies simpler and faster. + The advantage may be dependent on the size of the processor's data caches. + */ +local int updatewindow(strm, end, copy) +z_streamp strm; +const Bytef *end; +unsigned copy; +{ + struct inflate_state FAR *state; + unsigned dist; + + state = (struct inflate_state FAR *)strm->state; + + /* if it hasn't been done already, allocate space for the window */ + if (state->window == Z_NULL) { + state->window = (unsigned char FAR *) + ZALLOC(strm, 1U << state->wbits, + sizeof(unsigned char)); + if (state->window == Z_NULL) return 1; + } + + /* if window not in use yet, initialize */ + if (state->wsize == 0) { + state->wsize = 1U << state->wbits; + state->wnext = 0; + state->whave = 0; + } + + /* copy state->wsize or less output bytes into the circular window */ + if (copy >= state->wsize) { + zmemcpy(state->window, end - state->wsize, state->wsize); + state->wnext = 0; + state->whave = state->wsize; + } + else { + dist = state->wsize - state->wnext; + if (dist > copy) dist = copy; + zmemcpy(state->window + state->wnext, end - copy, dist); + copy -= dist; + if (copy) { + zmemcpy(state->window, end - copy, copy); + state->wnext = copy; + state->whave = state->wsize; + } + else { + state->wnext += dist; + if (state->wnext == state->wsize) state->wnext = 0; + if (state->whave < state->wsize) state->whave += dist; + } + } + return 0; +} + +/* Macros for inflate(): */ + +/* check function to use adler32() for zlib or crc32() for gzip */ +#ifdef GUNZIP +# define UPDATE(check, buf, len) \ + (state->flags ? crc32(check, buf, len) : adler32(check, buf, len)) +#else +# define UPDATE(check, buf, len) adler32(check, buf, len) +#endif + +/* check macros for header crc */ +#ifdef GUNZIP +# define CRC2(check, word) \ + do { \ + hbuf[0] = (unsigned char)(word); \ + hbuf[1] = (unsigned char)((word) >> 8); \ + check = crc32(check, hbuf, 2); \ + } while (0) + +# define CRC4(check, word) \ + do { \ + hbuf[0] = (unsigned char)(word); \ + hbuf[1] = (unsigned char)((word) >> 8); \ + hbuf[2] = (unsigned char)((word) >> 16); \ + hbuf[3] = (unsigned char)((word) >> 24); \ + check = crc32(check, hbuf, 4); \ + } while (0) +#endif + +/* Load registers with state in inflate() for speed */ +#define LOAD() \ + do { \ + put = strm->next_out; \ + left = strm->avail_out; \ + next = strm->next_in; \ + have = strm->avail_in; \ + hold = state->hold; \ + bits = state->bits; \ + } while (0) + +/* Restore state from registers in inflate() */ +#define RESTORE() \ + do { \ + strm->next_out = put; \ + strm->avail_out = left; \ + strm->next_in = next; \ + strm->avail_in = have; \ + state->hold = hold; \ + state->bits = bits; \ + } while (0) + +/* Clear the input bit accumulator */ +#define INITBITS() \ + do { \ + hold = 0; \ + bits = 0; \ + } while (0) + +/* Get a byte of input into the bit accumulator, or return from inflate() + if there is no input available. */ +#define PULLBYTE() \ + do { \ + if (have == 0) goto inf_leave; \ + have--; \ + hold += (unsigned long)(*next++) << bits; \ + bits += 8; \ + } while (0) + +/* Assure that there are at least n bits in the bit accumulator. If there is + not enough available input to do that, then return from inflate(). */ +#define NEEDBITS(n) \ + do { \ + while (bits < (unsigned)(n)) \ + PULLBYTE(); \ + } while (0) + +/* Return the low n bits of the bit accumulator (n < 16) */ +#define BITS(n) \ + ((unsigned)hold & ((1U << (n)) - 1)) + +/* Remove n bits from the bit accumulator */ +#define DROPBITS(n) \ + do { \ + hold >>= (n); \ + bits -= (unsigned)(n); \ + } while (0) + +/* Remove zero to seven bits as needed to go to a byte boundary */ +#define BYTEBITS() \ + do { \ + hold >>= bits & 7; \ + bits -= bits & 7; \ + } while (0) + +/* + inflate() uses a state machine to process as much input data and generate as + much output data as possible before returning. The state machine is + structured roughly as follows: + + for (;;) switch (state) { + ... + case STATEn: + if (not enough input data or output space to make progress) + return; + ... make progress ... + state = STATEm; + break; + ... + } + + so when inflate() is called again, the same case is attempted again, and + if the appropriate resources are provided, the machine proceeds to the + next state. The NEEDBITS() macro is usually the way the state evaluates + whether it can proceed or should return. NEEDBITS() does the return if + the requested bits are not available. The typical use of the BITS macros + is: + + NEEDBITS(n); + ... do something with BITS(n) ... + DROPBITS(n); + + where NEEDBITS(n) either returns from inflate() if there isn't enough + input left to load n bits into the accumulator, or it continues. BITS(n) + gives the low n bits in the accumulator. When done, DROPBITS(n) drops + the low n bits off the accumulator. INITBITS() clears the accumulator + and sets the number of available bits to zero. BYTEBITS() discards just + enough bits to put the accumulator on a byte boundary. After BYTEBITS() + and a NEEDBITS(8), then BITS(8) would return the next byte in the stream. + + NEEDBITS(n) uses PULLBYTE() to get an available byte of input, or to return + if there is no input available. The decoding of variable length codes uses + PULLBYTE() directly in order to pull just enough bytes to decode the next + code, and no more. + + Some states loop until they get enough input, making sure that enough + state information is maintained to continue the loop where it left off + if NEEDBITS() returns in the loop. For example, want, need, and keep + would all have to actually be part of the saved state in case NEEDBITS() + returns: + + case STATEw: + while (want < need) { + NEEDBITS(n); + keep[want++] = BITS(n); + DROPBITS(n); + } + state = STATEx; + case STATEx: + + As shown above, if the next state is also the next case, then the break + is omitted. + + A state may also return if there is not enough output space available to + complete that state. Those states are copying stored data, writing a + literal byte, and copying a matching string. + + When returning, a "goto inf_leave" is used to update the total counters, + update the check value, and determine whether any progress has been made + during that inflate() call in order to return the proper return code. + Progress is defined as a change in either strm->avail_in or strm->avail_out. + When there is a window, goto inf_leave will update the window with the last + output written. If a goto inf_leave occurs in the middle of decompression + and there is no window currently, goto inf_leave will create one and copy + output to the window for the next call of inflate(). + + In this implementation, the flush parameter of inflate() only affects the + return code (per zlib.h). inflate() always writes as much as possible to + strm->next_out, given the space available and the provided input--the effect + documented in zlib.h of Z_SYNC_FLUSH. Furthermore, inflate() always defers + the allocation of and copying into a sliding window until necessary, which + provides the effect documented in zlib.h for Z_FINISH when the entire input + stream available. So the only thing the flush parameter actually does is: + when flush is set to Z_FINISH, inflate() cannot return Z_OK. Instead it + will return Z_BUF_ERROR if it has not reached the end of the stream. + */ + +int ZEXPORT inflate(strm, flush) +z_streamp strm; +int flush; +{ + struct inflate_state FAR *state; + z_const unsigned char FAR *next; /* next input */ + unsigned char FAR *put; /* next output */ + unsigned have, left; /* available input and output */ + unsigned long hold; /* bit buffer */ + unsigned bits; /* bits in bit buffer */ + unsigned in, out; /* save starting available input and output */ + unsigned copy; /* number of stored or match bytes to copy */ + unsigned char FAR *from; /* where to copy match bytes from */ + code here; /* current decoding table entry */ + code last; /* parent table entry */ + unsigned len; /* length to copy for repeats, bits to drop */ + int ret; /* return code */ +#ifdef GUNZIP + unsigned char hbuf[4]; /* buffer for gzip header crc calculation */ +#endif + static const unsigned short order[19] = /* permutation of code lengths */ + {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; + + if (strm == Z_NULL || strm->state == Z_NULL || strm->next_out == Z_NULL || + (strm->next_in == Z_NULL && strm->avail_in != 0)) + return Z_STREAM_ERROR; + + state = (struct inflate_state FAR *)strm->state; + if (state->mode == TYPE) state->mode = TYPEDO; /* skip check */ + LOAD(); + in = have; + out = left; + ret = Z_OK; + for (;;) + switch (state->mode) { + case HEAD: + if (state->wrap == 0) { + state->mode = TYPEDO; + break; + } + NEEDBITS(16); +#ifdef GUNZIP + if ((state->wrap & 2) && hold == 0x8b1f) { /* gzip header */ + state->check = crc32(0L, Z_NULL, 0); + CRC2(state->check, hold); + INITBITS(); + state->mode = FLAGS; + break; + } + state->flags = 0; /* expect zlib header */ + if (state->head != Z_NULL) + state->head->done = -1; + if (!(state->wrap & 1) || /* check if zlib header allowed */ +#else + if ( +#endif + ((BITS(8) << 8) + (hold >> 8)) % 31) { + strm->msg = (char *)"incorrect header check"; + state->mode = BAD; + break; + } + if (BITS(4) != Z_DEFLATED) { + strm->msg = (char *)"unknown compression method"; + state->mode = BAD; + break; + } + DROPBITS(4); + len = BITS(4) + 8; + if (state->wbits == 0) + state->wbits = len; + else if (len > state->wbits) { + strm->msg = (char *)"invalid window size"; + state->mode = BAD; + break; + } + state->dmax = 1U << len; + Tracev((stderr, "inflate: zlib header ok\n")); + strm->adler = state->check = adler32(0L, Z_NULL, 0); + state->mode = hold & 0x200 ? DICTID : TYPE; + INITBITS(); + break; +#ifdef GUNZIP + case FLAGS: + NEEDBITS(16); + state->flags = (int)(hold); + if ((state->flags & 0xff) != Z_DEFLATED) { + strm->msg = (char *)"unknown compression method"; + state->mode = BAD; + break; + } + if (state->flags & 0xe000) { + strm->msg = (char *)"unknown header flags set"; + state->mode = BAD; + break; + } + if (state->head != Z_NULL) + state->head->text = (int)((hold >> 8) & 1); + if (state->flags & 0x0200) CRC2(state->check, hold); + INITBITS(); + state->mode = TIME; + case TIME: + NEEDBITS(32); + if (state->head != Z_NULL) + state->head->time = hold; + if (state->flags & 0x0200) CRC4(state->check, hold); + INITBITS(); + state->mode = OS; + case OS: + NEEDBITS(16); + if (state->head != Z_NULL) { + state->head->xflags = (int)(hold & 0xff); + state->head->os = (int)(hold >> 8); + } + if (state->flags & 0x0200) CRC2(state->check, hold); + INITBITS(); + state->mode = EXLEN; + case EXLEN: + if (state->flags & 0x0400) { + NEEDBITS(16); + state->length = (unsigned)(hold); + if (state->head != Z_NULL) + state->head->extra_len = (unsigned)hold; + if (state->flags & 0x0200) CRC2(state->check, hold); + INITBITS(); + } + else if (state->head != Z_NULL) + state->head->extra = Z_NULL; + state->mode = EXTRA; + case EXTRA: + if (state->flags & 0x0400) { + copy = state->length; + if (copy > have) copy = have; + if (copy) { + if (state->head != Z_NULL && + state->head->extra != Z_NULL) { + len = state->head->extra_len - state->length; + zmemcpy(state->head->extra + len, next, + len + copy > state->head->extra_max ? + state->head->extra_max - len : copy); + } + if (state->flags & 0x0200) + state->check = crc32(state->check, next, copy); + have -= copy; + next += copy; + state->length -= copy; + } + if (state->length) goto inf_leave; + } + state->length = 0; + state->mode = NAME; + case NAME: + if (state->flags & 0x0800) { + if (have == 0) goto inf_leave; + copy = 0; + do { + len = (unsigned)(next[copy++]); + if (state->head != Z_NULL && + state->head->name != Z_NULL && + state->length < state->head->name_max) + state->head->name[state->length++] = len; + } while (len && copy < have); + if (state->flags & 0x0200) + state->check = crc32(state->check, next, copy); + have -= copy; + next += copy; + if (len) goto inf_leave; + } + else if (state->head != Z_NULL) + state->head->name = Z_NULL; + state->length = 0; + state->mode = COMMENT; + case COMMENT: + if (state->flags & 0x1000) { + if (have == 0) goto inf_leave; + copy = 0; + do { + len = (unsigned)(next[copy++]); + if (state->head != Z_NULL && + state->head->comment != Z_NULL && + state->length < state->head->comm_max) + state->head->comment[state->length++] = len; + } while (len && copy < have); + if (state->flags & 0x0200) + state->check = crc32(state->check, next, copy); + have -= copy; + next += copy; + if (len) goto inf_leave; + } + else if (state->head != Z_NULL) + state->head->comment = Z_NULL; + state->mode = HCRC; + case HCRC: + if (state->flags & 0x0200) { + NEEDBITS(16); + if (hold != (state->check & 0xffff)) { + strm->msg = (char *)"header crc mismatch"; + state->mode = BAD; + break; + } + INITBITS(); + } + if (state->head != Z_NULL) { + state->head->hcrc = (int)((state->flags >> 9) & 1); + state->head->done = 1; + } + strm->adler = state->check = crc32(0L, Z_NULL, 0); + state->mode = TYPE; + break; +#endif + case DICTID: + NEEDBITS(32); + strm->adler = state->check = ZSWAP32(hold); + INITBITS(); + state->mode = DICT; + case DICT: + if (state->havedict == 0) { + RESTORE(); + return Z_NEED_DICT; + } + strm->adler = state->check = adler32(0L, Z_NULL, 0); + state->mode = TYPE; + case TYPE: + if (flush == Z_BLOCK || flush == Z_TREES) goto inf_leave; + case TYPEDO: + if (state->last) { + BYTEBITS(); + state->mode = CHECK; + break; + } + NEEDBITS(3); + state->last = BITS(1); + DROPBITS(1); + switch (BITS(2)) { + case 0: /* stored block */ + Tracev((stderr, "inflate: stored block%s\n", + state->last ? " (last)" : "")); + state->mode = STORED; + break; + case 1: /* fixed block */ + fixedtables(state); + Tracev((stderr, "inflate: fixed codes block%s\n", + state->last ? " (last)" : "")); + state->mode = LEN_; /* decode codes */ + if (flush == Z_TREES) { + DROPBITS(2); + goto inf_leave; + } + break; + case 2: /* dynamic block */ + Tracev((stderr, "inflate: dynamic codes block%s\n", + state->last ? " (last)" : "")); + state->mode = TABLE; + break; + case 3: + strm->msg = (char *)"invalid block type"; + state->mode = BAD; + } + DROPBITS(2); + break; + case STORED: + BYTEBITS(); /* go to byte boundary */ + NEEDBITS(32); + if ((hold & 0xffff) != ((hold >> 16) ^ 0xffff)) { + strm->msg = (char *)"invalid stored block lengths"; + state->mode = BAD; + break; + } + state->length = (unsigned)hold & 0xffff; + Tracev((stderr, "inflate: stored length %u\n", + state->length)); + INITBITS(); + state->mode = COPY_; + if (flush == Z_TREES) goto inf_leave; + case COPY_: + state->mode = COPY; + case COPY: + copy = state->length; + if (copy) { + if (copy > have) copy = have; + if (copy > left) copy = left; + if (copy == 0) goto inf_leave; + zmemcpy(put, next, copy); + have -= copy; + next += copy; + left -= copy; + put += copy; + state->length -= copy; + break; + } + Tracev((stderr, "inflate: stored end\n")); + state->mode = TYPE; + break; + case TABLE: + NEEDBITS(14); + state->nlen = BITS(5) + 257; + DROPBITS(5); + state->ndist = BITS(5) + 1; + DROPBITS(5); + state->ncode = BITS(4) + 4; + DROPBITS(4); +#ifndef PKZIP_BUG_WORKAROUND + if (state->nlen > 286 || state->ndist > 30) { + strm->msg = (char *)"too many length or distance symbols"; + state->mode = BAD; + break; + } +#endif + Tracev((stderr, "inflate: table sizes ok\n")); + state->have = 0; + state->mode = LENLENS; + case LENLENS: + while (state->have < state->ncode) { + NEEDBITS(3); + state->lens[order[state->have++]] = (unsigned short)BITS(3); + DROPBITS(3); + } + while (state->have < 19) + state->lens[order[state->have++]] = 0; + state->next = state->codes; + state->lencode = (const code FAR *)(state->next); + state->lenbits = 7; + ret = inflate_table(CODES, state->lens, 19, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid code lengths set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: code lengths ok\n")); + state->have = 0; + state->mode = CODELENS; + case CODELENS: + while (state->have < state->nlen + state->ndist) { + for (;;) { + here = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if (here.val < 16) { + DROPBITS(here.bits); + state->lens[state->have++] = here.val; + } + else { + if (here.val == 16) { + NEEDBITS(here.bits + 2); + DROPBITS(here.bits); + if (state->have == 0) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + len = state->lens[state->have - 1]; + copy = 3 + BITS(2); + DROPBITS(2); + } + else if (here.val == 17) { + NEEDBITS(here.bits + 3); + DROPBITS(here.bits); + len = 0; + copy = 3 + BITS(3); + DROPBITS(3); + } + else { + NEEDBITS(here.bits + 7); + DROPBITS(here.bits); + len = 0; + copy = 11 + BITS(7); + DROPBITS(7); + } + if (state->have + copy > state->nlen + state->ndist) { + strm->msg = (char *)"invalid bit length repeat"; + state->mode = BAD; + break; + } + while (copy--) + state->lens[state->have++] = (unsigned short)len; + } + } + + /* handle error breaks in while */ + if (state->mode == BAD) break; + + /* check for end-of-block code (better have one) */ + if (state->lens[256] == 0) { + strm->msg = (char *)"invalid code -- missing end-of-block"; + state->mode = BAD; + break; + } + + /* build code tables -- note: do not change the lenbits or distbits + values here (9 and 6) without reading the comments in inftrees.h + concerning the ENOUGH constants, which depend on those values */ + state->next = state->codes; + state->lencode = (const code FAR *)(state->next); + state->lenbits = 9; + ret = inflate_table(LENS, state->lens, state->nlen, &(state->next), + &(state->lenbits), state->work); + if (ret) { + strm->msg = (char *)"invalid literal/lengths set"; + state->mode = BAD; + break; + } + state->distcode = (const code FAR *)(state->next); + state->distbits = 6; + ret = inflate_table(DISTS, state->lens + state->nlen, state->ndist, + &(state->next), &(state->distbits), state->work); + if (ret) { + strm->msg = (char *)"invalid distances set"; + state->mode = BAD; + break; + } + Tracev((stderr, "inflate: codes ok\n")); + state->mode = LEN_; + if (flush == Z_TREES) goto inf_leave; + case LEN_: + state->mode = LEN; + case LEN: + if (have >= 6 && left >= 258) { + RESTORE(); + inflate_fast(strm, out); + LOAD(); + if (state->mode == TYPE) + state->back = -1; + break; + } + state->back = 0; + for (;;) { + here = state->lencode[BITS(state->lenbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if (here.op && (here.op & 0xf0) == 0) { + last = here; + for (;;) { + here = state->lencode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + here.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + state->back += last.bits; + } + DROPBITS(here.bits); + state->back += here.bits; + state->length = (unsigned)here.val; + if ((int)(here.op) == 0) { + Tracevv((stderr, here.val >= 0x20 && here.val < 0x7f ? + "inflate: literal '%c'\n" : + "inflate: literal 0x%02x\n", here.val)); + state->mode = LIT; + break; + } + if (here.op & 32) { + Tracevv((stderr, "inflate: end of block\n")); + state->back = -1; + state->mode = TYPE; + break; + } + if (here.op & 64) { + strm->msg = (char *)"invalid literal/length code"; + state->mode = BAD; + break; + } + state->extra = (unsigned)(here.op) & 15; + state->mode = LENEXT; + case LENEXT: + if (state->extra) { + NEEDBITS(state->extra); + state->length += BITS(state->extra); + DROPBITS(state->extra); + state->back += state->extra; + } + Tracevv((stderr, "inflate: length %u\n", state->length)); + state->was = state->length; + state->mode = DIST; + case DIST: + for (;;) { + here = state->distcode[BITS(state->distbits)]; + if ((unsigned)(here.bits) <= bits) break; + PULLBYTE(); + } + if ((here.op & 0xf0) == 0) { + last = here; + for (;;) { + here = state->distcode[last.val + + (BITS(last.bits + last.op) >> last.bits)]; + if ((unsigned)(last.bits + here.bits) <= bits) break; + PULLBYTE(); + } + DROPBITS(last.bits); + state->back += last.bits; + } + DROPBITS(here.bits); + state->back += here.bits; + if (here.op & 64) { + strm->msg = (char *)"invalid distance code"; + state->mode = BAD; + break; + } + state->offset = (unsigned)here.val; + state->extra = (unsigned)(here.op) & 15; + state->mode = DISTEXT; + case DISTEXT: + if (state->extra) { + NEEDBITS(state->extra); + state->offset += BITS(state->extra); + DROPBITS(state->extra); + state->back += state->extra; + } +#ifdef INFLATE_STRICT + if (state->offset > state->dmax) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } +#endif + Tracevv((stderr, "inflate: distance %u\n", state->offset)); + state->mode = MATCH; + case MATCH: + if (left == 0) goto inf_leave; + copy = out - left; + if (state->offset > copy) { /* copy from window */ + copy = state->offset - copy; + if (copy > state->whave) { + if (state->sane) { + strm->msg = (char *)"invalid distance too far back"; + state->mode = BAD; + break; + } +#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR + Trace((stderr, "inflate.c too far\n")); + copy -= state->whave; + if (copy > state->length) copy = state->length; + if (copy > left) copy = left; + left -= copy; + state->length -= copy; + do { + *put++ = 0; + } while (--copy); + if (state->length == 0) state->mode = LEN; + break; +#endif + } + if (copy > state->wnext) { + copy -= state->wnext; + from = state->window + (state->wsize - copy); + } + else + from = state->window + (state->wnext - copy); + if (copy > state->length) copy = state->length; + } + else { /* copy from output */ + from = put - state->offset; + copy = state->length; + } + if (copy > left) copy = left; + left -= copy; + state->length -= copy; + do { + *put++ = *from++; + } while (--copy); + if (state->length == 0) state->mode = LEN; + break; + case LIT: + if (left == 0) goto inf_leave; + *put++ = (unsigned char)(state->length); + left--; + state->mode = LEN; + break; + case CHECK: + if (state->wrap) { + NEEDBITS(32); + out -= left; + strm->total_out += out; + state->total += out; + if (out) + strm->adler = state->check = + UPDATE(state->check, put - out, out); + out = left; + if (( +#ifdef GUNZIP + state->flags ? hold : +#endif + ZSWAP32(hold)) != state->check) { + strm->msg = (char *)"incorrect data check"; + state->mode = BAD; + break; + } + INITBITS(); + Tracev((stderr, "inflate: check matches trailer\n")); + } +#ifdef GUNZIP + state->mode = LENGTH; + case LENGTH: + if (state->wrap && state->flags) { + NEEDBITS(32); + if (hold != (state->total & 0xffffffffUL)) { + strm->msg = (char *)"incorrect length check"; + state->mode = BAD; + break; + } + INITBITS(); + Tracev((stderr, "inflate: length matches trailer\n")); + } +#endif + state->mode = DONE; + case DONE: + ret = Z_STREAM_END; + goto inf_leave; + case BAD: + ret = Z_DATA_ERROR; + goto inf_leave; + case MEM: + return Z_MEM_ERROR; + case SYNC: + default: + return Z_STREAM_ERROR; + } + + /* + Return from inflate(), updating the total counts and the check value. + If there was no progress during the inflate() call, return a buffer + error. Call updatewindow() to create and/or update the window state. + Note: a memory error from inflate() is non-recoverable. + */ + inf_leave: + RESTORE(); + if (state->wsize || (out != strm->avail_out && state->mode < BAD && + (state->mode < CHECK || flush != Z_FINISH))) + if (updatewindow(strm, strm->next_out, out - strm->avail_out)) { + state->mode = MEM; + return Z_MEM_ERROR; + } + in -= strm->avail_in; + out -= strm->avail_out; + strm->total_in += in; + strm->total_out += out; + state->total += out; + if (state->wrap && out) + strm->adler = state->check = + UPDATE(state->check, strm->next_out - out, out); + strm->data_type = state->bits + (state->last ? 64 : 0) + + (state->mode == TYPE ? 128 : 0) + + (state->mode == LEN_ || state->mode == COPY_ ? 256 : 0); + if (((in == 0 && out == 0) || flush == Z_FINISH) && ret == Z_OK) + ret = Z_BUF_ERROR; + return ret; +} + +int ZEXPORT inflateEnd(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + if (strm == Z_NULL || strm->state == Z_NULL || strm->zfree == (free_func)0) + return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (state->window != Z_NULL) ZFREE(strm, state->window); + ZFREE(strm, strm->state); + strm->state = Z_NULL; + Tracev((stderr, "inflate: end\n")); + return Z_OK; +} + +int ZEXPORT inflateGetDictionary(strm, dictionary, dictLength) +z_streamp strm; +Bytef *dictionary; +uInt *dictLength; +{ + struct inflate_state FAR *state; + + /* check state */ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + + /* copy dictionary */ + if (state->whave && dictionary != Z_NULL) { + zmemcpy(dictionary, state->window + state->wnext, + state->whave - state->wnext); + zmemcpy(dictionary + state->whave - state->wnext, + state->window, state->wnext); + } + if (dictLength != Z_NULL) + *dictLength = state->whave; + return Z_OK; +} + +int ZEXPORT inflateSetDictionary(strm, dictionary, dictLength) +z_streamp strm; +const Bytef *dictionary; +uInt dictLength; +{ + struct inflate_state FAR *state; + unsigned long dictid; + int ret; + + /* check state */ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (state->wrap != 0 && state->mode != DICT) + return Z_STREAM_ERROR; + + /* check for correct dictionary identifier */ + if (state->mode == DICT) { + dictid = adler32(0L, Z_NULL, 0); + dictid = adler32(dictid, dictionary, dictLength); + if (dictid != state->check) + return Z_DATA_ERROR; + } + + /* copy dictionary to window using updatewindow(), which will amend the + existing dictionary if appropriate */ + ret = updatewindow(strm, dictionary + dictLength, dictLength); + if (ret) { + state->mode = MEM; + return Z_MEM_ERROR; + } + state->havedict = 1; + Tracev((stderr, "inflate: dictionary set\n")); + return Z_OK; +} + +int ZEXPORT inflateGetHeader(strm, head) +z_streamp strm; +gz_headerp head; +{ + struct inflate_state FAR *state; + + /* check state */ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if ((state->wrap & 2) == 0) return Z_STREAM_ERROR; + + /* save header structure */ + state->head = head; + head->done = 0; + return Z_OK; +} + +/* + Search buf[0..len-1] for the pattern: 0, 0, 0xff, 0xff. Return when found + or when out of input. When called, *have is the number of pattern bytes + found in order so far, in 0..3. On return *have is updated to the new + state. If on return *have equals four, then the pattern was found and the + return value is how many bytes were read including the last byte of the + pattern. If *have is less than four, then the pattern has not been found + yet and the return value is len. In the latter case, syncsearch() can be + called again with more data and the *have state. *have is initialized to + zero for the first call. + */ +local unsigned syncsearch(have, buf, len) +unsigned FAR *have; +const unsigned char FAR *buf; +unsigned len; +{ + unsigned got; + unsigned next; + + got = *have; + next = 0; + while (next < len && got < 4) { + if ((int)(buf[next]) == (got < 2 ? 0 : 0xff)) + got++; + else if (buf[next]) + got = 0; + else + got = 4 - got; + next++; + } + *have = got; + return next; +} + +int ZEXPORT inflateSync(strm) +z_streamp strm; +{ + unsigned len; /* number of bytes to look at or looked at */ + unsigned long in, out; /* temporary to save total_in and total_out */ + unsigned char buf[4]; /* to restore bit buffer to byte string */ + struct inflate_state FAR *state; + + /* check parameters */ + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + if (strm->avail_in == 0 && state->bits < 8) return Z_BUF_ERROR; + + /* if first time, start search in bit buffer */ + if (state->mode != SYNC) { + state->mode = SYNC; + state->hold <<= state->bits & 7; + state->bits -= state->bits & 7; + len = 0; + while (state->bits >= 8) { + buf[len++] = (unsigned char)(state->hold); + state->hold >>= 8; + state->bits -= 8; + } + state->have = 0; + syncsearch(&(state->have), buf, len); + } + + /* search available input */ + len = syncsearch(&(state->have), strm->next_in, strm->avail_in); + strm->avail_in -= len; + strm->next_in += len; + strm->total_in += len; + + /* return no joy or set up to restart inflate() on a new block */ + if (state->have != 4) return Z_DATA_ERROR; + in = strm->total_in; out = strm->total_out; + inflateReset(strm); + strm->total_in = in; strm->total_out = out; + state->mode = TYPE; + return Z_OK; +} + +/* + Returns true if inflate is currently at the end of a block generated by + Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP + implementation to provide an additional safety check. PPP uses + Z_SYNC_FLUSH but removes the length bytes of the resulting empty stored + block. When decompressing, PPP checks that at the end of input packet, + inflate is waiting for these length bytes. + */ +int ZEXPORT inflateSyncPoint(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + return state->mode == STORED && state->bits == 0; +} + +int ZEXPORT inflateCopy(dest, source) +z_streamp dest; +z_streamp source; +{ + struct inflate_state FAR *state; + struct inflate_state FAR *copy; + unsigned char FAR *window; + unsigned wsize; + + /* check input */ + if (dest == Z_NULL || source == Z_NULL || source->state == Z_NULL || + source->zalloc == (alloc_func)0 || source->zfree == (free_func)0) + return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)source->state; + + /* allocate space */ + copy = (struct inflate_state FAR *) + ZALLOC(source, 1, sizeof(struct inflate_state)); + if (copy == Z_NULL) return Z_MEM_ERROR; + window = Z_NULL; + if (state->window != Z_NULL) { + window = (unsigned char FAR *) + ZALLOC(source, 1U << state->wbits, sizeof(unsigned char)); + if (window == Z_NULL) { + ZFREE(source, copy); + return Z_MEM_ERROR; + } + } + + /* copy state */ + zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); + zmemcpy((voidpf)copy, (voidpf)state, sizeof(struct inflate_state)); + if (state->lencode >= state->codes && + state->lencode <= state->codes + ENOUGH - 1) { + copy->lencode = copy->codes + (state->lencode - state->codes); + copy->distcode = copy->codes + (state->distcode - state->codes); + } + copy->next = copy->codes + (state->next - state->codes); + if (window != Z_NULL) { + wsize = 1U << state->wbits; + zmemcpy(window, state->window, wsize); + } + copy->window = window; + dest->state = (struct internal_state FAR *)copy; + return Z_OK; +} + +int ZEXPORT inflateUndermine(strm, subvert) +z_streamp strm; +int subvert; +{ + struct inflate_state FAR *state; + + if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR; + state = (struct inflate_state FAR *)strm->state; + state->sane = !subvert; +#ifdef INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR + return Z_OK; +#else + state->sane = 1; + return Z_DATA_ERROR; +#endif +} + +long ZEXPORT inflateMark(strm) +z_streamp strm; +{ + struct inflate_state FAR *state; + + if (strm == Z_NULL || strm->state == Z_NULL) return (long)(((unsigned long)0 - 1) << 16); + state = (struct inflate_state FAR *)strm->state; + return ((long)(state->back) << 16) + + (state->mode == COPY ? state->length : + (state->mode == MATCH ? state->was - state->length : 0)); +} diff --git a/fdbrpc/zlib/inflate.h b/fdbrpc/zlib/inflate.h new file mode 100644 index 0000000000..95f4986d40 --- /dev/null +++ b/fdbrpc/zlib/inflate.h @@ -0,0 +1,122 @@ +/* inflate.h -- internal inflate state definition + * Copyright (C) 1995-2009 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* define NO_GZIP when compiling if you want to disable gzip header and + trailer decoding by inflate(). NO_GZIP would be used to avoid linking in + the crc code when it is not needed. For shared libraries, gzip decoding + should be left enabled. */ +#ifndef NO_GZIP +# define GUNZIP +#endif + +/* Possible inflate modes between inflate() calls */ +typedef enum { + HEAD, /* i: waiting for magic header */ + FLAGS, /* i: waiting for method and flags (gzip) */ + TIME, /* i: waiting for modification time (gzip) */ + OS, /* i: waiting for extra flags and operating system (gzip) */ + EXLEN, /* i: waiting for extra length (gzip) */ + EXTRA, /* i: waiting for extra bytes (gzip) */ + NAME, /* i: waiting for end of file name (gzip) */ + COMMENT, /* i: waiting for end of comment (gzip) */ + HCRC, /* i: waiting for header crc (gzip) */ + DICTID, /* i: waiting for dictionary check value */ + DICT, /* waiting for inflateSetDictionary() call */ + TYPE, /* i: waiting for type bits, including last-flag bit */ + TYPEDO, /* i: same, but skip check to exit inflate on new block */ + STORED, /* i: waiting for stored size (length and complement) */ + COPY_, /* i/o: same as COPY below, but only first time in */ + COPY, /* i/o: waiting for input or output to copy stored block */ + TABLE, /* i: waiting for dynamic block table lengths */ + LENLENS, /* i: waiting for code length code lengths */ + CODELENS, /* i: waiting for length/lit and distance code lengths */ + LEN_, /* i: same as LEN below, but only first time in */ + LEN, /* i: waiting for length/lit/eob code */ + LENEXT, /* i: waiting for length extra bits */ + DIST, /* i: waiting for distance code */ + DISTEXT, /* i: waiting for distance extra bits */ + MATCH, /* o: waiting for output space to copy string */ + LIT, /* o: waiting for output space to write literal */ + CHECK, /* i: waiting for 32-bit check value */ + LENGTH, /* i: waiting for 32-bit length (gzip) */ + DONE, /* finished check, done -- remain here until reset */ + BAD, /* got a data error -- remain here until reset */ + MEM, /* got an inflate() memory error -- remain here until reset */ + SYNC /* looking for synchronization bytes to restart inflate() */ +} inflate_mode; + +/* + State transitions between above modes - + + (most modes can go to BAD or MEM on error -- not shown for clarity) + + Process header: + HEAD -> (gzip) or (zlib) or (raw) + (gzip) -> FLAGS -> TIME -> OS -> EXLEN -> EXTRA -> NAME -> COMMENT -> + HCRC -> TYPE + (zlib) -> DICTID or TYPE + DICTID -> DICT -> TYPE + (raw) -> TYPEDO + Read deflate blocks: + TYPE -> TYPEDO -> STORED or TABLE or LEN_ or CHECK + STORED -> COPY_ -> COPY -> TYPE + TABLE -> LENLENS -> CODELENS -> LEN_ + LEN_ -> LEN + Read deflate codes in fixed or dynamic block: + LEN -> LENEXT or LIT or TYPE + LENEXT -> DIST -> DISTEXT -> MATCH -> LEN + LIT -> LEN + Process trailer: + CHECK -> LENGTH -> DONE + */ + +/* state maintained between inflate() calls. Approximately 10K bytes. */ +struct inflate_state { + inflate_mode mode; /* current inflate mode */ + int last; /* true if processing last block */ + int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ + int havedict; /* true if dictionary provided */ + int flags; /* gzip header method and flags (0 if zlib) */ + unsigned dmax; /* zlib header max distance (INFLATE_STRICT) */ + unsigned long check; /* protected copy of check value */ + unsigned long total; /* protected copy of output count */ + gz_headerp head; /* where to save gzip header information */ + /* sliding window */ + unsigned wbits; /* log base 2 of requested window size */ + unsigned wsize; /* window size or zero if not using window */ + unsigned whave; /* valid bytes in the window */ + unsigned wnext; /* window write index */ + unsigned char FAR *window; /* allocated sliding window, if needed */ + /* bit accumulator */ + unsigned long hold; /* input bit accumulator */ + unsigned bits; /* number of bits in "in" */ + /* for string and stored block copying */ + unsigned length; /* literal or length of data to copy */ + unsigned offset; /* distance back to copy string from */ + /* for table and code decoding */ + unsigned extra; /* extra bits needed */ + /* fixed and dynamic code tables */ + code const FAR *lencode; /* starting table for length/literal codes */ + code const FAR *distcode; /* starting table for distance codes */ + unsigned lenbits; /* index bits for lencode */ + unsigned distbits; /* index bits for distcode */ + /* dynamic table building */ + unsigned ncode; /* number of code length code lengths */ + unsigned nlen; /* number of length code lengths */ + unsigned ndist; /* number of distance code lengths */ + unsigned have; /* number of code lengths in lens[] */ + code FAR *next; /* next available space in codes[] */ + unsigned short lens[320]; /* temporary storage for code lengths */ + unsigned short work[288]; /* work area for code table building */ + code codes[ENOUGH]; /* space for code tables */ + int sane; /* if false, allow invalid distance too far */ + int back; /* bits back of last unprocessed length/lit */ + unsigned was; /* initial length of match */ +}; diff --git a/fdbrpc/zlib/inftrees.c b/fdbrpc/zlib/inftrees.c new file mode 100644 index 0000000000..44d89cf24e --- /dev/null +++ b/fdbrpc/zlib/inftrees.c @@ -0,0 +1,306 @@ +/* inftrees.c -- generate Huffman trees for efficient decoding + * Copyright (C) 1995-2013 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +#include "zutil.h" +#include "inftrees.h" + +#define MAXBITS 15 + +const char inflate_copyright[] = + " inflate 1.2.8 Copyright 1995-2013 Mark Adler "; +/* + If you use the zlib library in a product, an acknowledgment is welcome + in the documentation of your product. If for some reason you cannot + include such an acknowledgment, I would appreciate that you keep this + copyright string in the executable of your product. + */ + +/* + Build a set of tables to decode the provided canonical Huffman code. + The code lengths are lens[0..codes-1]. The result starts at *table, + whose indices are 0..2^bits-1. work is a writable array of at least + lens shorts, which is used as a work area. type is the type of code + to be generated, CODES, LENS, or DISTS. On return, zero is success, + -1 is an invalid code, and +1 means that ENOUGH isn't enough. table + on return points to the next available entry's address. bits is the + requested root table index bits, and on return it is the actual root + table index bits. It will differ if the request is greater than the + longest code or if it is less than the shortest code. + */ +int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work) +codetype type; +unsigned short FAR *lens; +unsigned codes; +code FAR * FAR *table; +unsigned FAR *bits; +unsigned short FAR *work; +{ + unsigned len; /* a code's length in bits */ + unsigned sym; /* index of code symbols */ + unsigned min, max; /* minimum and maximum code lengths */ + unsigned root; /* number of index bits for root table */ + unsigned curr; /* number of index bits for current table */ + unsigned drop; /* code bits to drop for sub-table */ + int left; /* number of prefix codes available */ + unsigned used; /* code entries in table used */ + unsigned huff; /* Huffman code */ + unsigned incr; /* for incrementing code, index */ + unsigned fill; /* index for replicating entries */ + unsigned low; /* low bits for current root entry */ + unsigned mask; /* mask for low root bits */ + code here; /* table entry for duplication */ + code FAR *next; /* next available space in table */ + const unsigned short FAR *base; /* base value table to use */ + const unsigned short FAR *extra; /* extra bits table to use */ + int end; /* use base and extra for symbol > end */ + unsigned short count[MAXBITS+1]; /* number of codes of each length */ + unsigned short offs[MAXBITS+1]; /* offsets in table for each length */ + static const unsigned short lbase[31] = { /* Length codes 257..285 base */ + 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, + 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0}; + static const unsigned short lext[31] = { /* Length codes 257..285 extra */ + 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18, + 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 72, 78}; + static const unsigned short dbase[32] = { /* Distance codes 0..29 base */ + 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, + 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, + 8193, 12289, 16385, 24577, 0, 0}; + static const unsigned short dext[32] = { /* Distance codes 0..29 extra */ + 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, + 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, + 28, 28, 29, 29, 64, 64}; + + /* + Process a set of code lengths to create a canonical Huffman code. The + code lengths are lens[0..codes-1]. Each length corresponds to the + symbols 0..codes-1. The Huffman code is generated by first sorting the + symbols by length from short to long, and retaining the symbol order + for codes with equal lengths. Then the code starts with all zero bits + for the first code of the shortest length, and the codes are integer + increments for the same length, and zeros are appended as the length + increases. For the deflate format, these bits are stored backwards + from their more natural integer increment ordering, and so when the + decoding tables are built in the large loop below, the integer codes + are incremented backwards. + + This routine assumes, but does not check, that all of the entries in + lens[] are in the range 0..MAXBITS. The caller must assure this. + 1..MAXBITS is interpreted as that code length. zero means that that + symbol does not occur in this code. + + The codes are sorted by computing a count of codes for each length, + creating from that a table of starting indices for each length in the + sorted table, and then entering the symbols in order in the sorted + table. The sorted table is work[], with that space being provided by + the caller. + + The length counts are used for other purposes as well, i.e. finding + the minimum and maximum length codes, determining if there are any + codes at all, checking for a valid set of lengths, and looking ahead + at length counts to determine sub-table sizes when building the + decoding tables. + */ + + /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */ + for (len = 0; len <= MAXBITS; len++) + count[len] = 0; + for (sym = 0; sym < codes; sym++) + count[lens[sym]]++; + + /* bound code lengths, force root to be within code lengths */ + root = *bits; + for (max = MAXBITS; max >= 1; max--) + if (count[max] != 0) break; + if (root > max) root = max; + if (max == 0) { /* no symbols to code at all */ + here.op = (unsigned char)64; /* invalid code marker */ + here.bits = (unsigned char)1; + here.val = (unsigned short)0; + *(*table)++ = here; /* make a table to force an error */ + *(*table)++ = here; + *bits = 1; + return 0; /* no symbols, but wait for decoding to report error */ + } + for (min = 1; min < max; min++) + if (count[min] != 0) break; + if (root < min) root = min; + + /* check for an over-subscribed or incomplete set of lengths */ + left = 1; + for (len = 1; len <= MAXBITS; len++) { + left <<= 1; + left -= count[len]; + if (left < 0) return -1; /* over-subscribed */ + } + if (left > 0 && (type == CODES || max != 1)) + return -1; /* incomplete set */ + + /* generate offsets into symbol table for each length for sorting */ + offs[1] = 0; + for (len = 1; len < MAXBITS; len++) + offs[len + 1] = offs[len] + count[len]; + + /* sort symbols by length, by symbol order within each length */ + for (sym = 0; sym < codes; sym++) + if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym; + + /* + Create and fill in decoding tables. In this loop, the table being + filled is at next and has curr index bits. The code being used is huff + with length len. That code is converted to an index by dropping drop + bits off of the bottom. For codes where len is less than drop + curr, + those top drop + curr - len bits are incremented through all values to + fill the table with replicated entries. + + root is the number of index bits for the root table. When len exceeds + root, sub-tables are created pointed to by the root entry with an index + of the low root bits of huff. This is saved in low to check for when a + new sub-table should be started. drop is zero when the root table is + being filled, and drop is root when sub-tables are being filled. + + When a new sub-table is needed, it is necessary to look ahead in the + code lengths to determine what size sub-table is needed. The length + counts are used for this, and so count[] is decremented as codes are + entered in the tables. + + used keeps track of how many table entries have been allocated from the + provided *table space. It is checked for LENS and DIST tables against + the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in + the initial root table size constants. See the comments in inftrees.h + for more information. + + sym increments through all symbols, and the loop terminates when + all codes of length max, i.e. all codes, have been processed. This + routine permits incomplete codes, so another loop after this one fills + in the rest of the decoding tables with invalid code markers. + */ + + /* set up for code type */ + switch (type) { + case CODES: + base = extra = work; /* dummy value--not used */ + end = 19; + break; + case LENS: + base = lbase; + base -= 257; + extra = lext; + extra -= 257; + end = 256; + break; + default: /* DISTS */ + base = dbase; + extra = dext; + end = -1; + } + + /* initialize state for loop */ + huff = 0; /* starting code */ + sym = 0; /* starting code symbol */ + len = min; /* starting code length */ + next = *table; /* current table to fill in */ + curr = root; /* current table index bits */ + drop = 0; /* current bits to drop from code for index */ + low = (unsigned)(-1); /* trigger new sub-table when len > root */ + used = 1U << root; /* use root table entries */ + mask = used - 1; /* mask for comparing low */ + + /* check available table space */ + if ((type == LENS && used > ENOUGH_LENS) || + (type == DISTS && used > ENOUGH_DISTS)) + return 1; + + /* process all codes and make table entries */ + for (;;) { + /* create table entry */ + here.bits = (unsigned char)(len - drop); + if ((int)(work[sym]) < end) { + here.op = (unsigned char)0; + here.val = work[sym]; + } + else if ((int)(work[sym]) > end) { + here.op = (unsigned char)(extra[work[sym]]); + here.val = base[work[sym]]; + } + else { + here.op = (unsigned char)(32 + 64); /* end of block */ + here.val = 0; + } + + /* replicate for those indices with low len bits equal to huff */ + incr = 1U << (len - drop); + fill = 1U << curr; + min = fill; /* save offset to next table */ + do { + fill -= incr; + next[(huff >> drop) + fill] = here; + } while (fill != 0); + + /* backwards increment the len-bit code huff */ + incr = 1U << (len - 1); + while (huff & incr) + incr >>= 1; + if (incr != 0) { + huff &= incr - 1; + huff += incr; + } + else + huff = 0; + + /* go to next symbol, update count, len */ + sym++; + if (--(count[len]) == 0) { + if (len == max) break; + len = lens[work[sym]]; + } + + /* create new sub-table if needed */ + if (len > root && (huff & mask) != low) { + /* if first time, transition to sub-tables */ + if (drop == 0) + drop = root; + + /* increment past last table */ + next += min; /* here min is 1 << curr */ + + /* determine length of next table */ + curr = len - drop; + left = (int)(1 << curr); + while (curr + drop < max) { + left -= count[curr + drop]; + if (left <= 0) break; + curr++; + left <<= 1; + } + + /* check for enough space */ + used += 1U << curr; + if ((type == LENS && used > ENOUGH_LENS) || + (type == DISTS && used > ENOUGH_DISTS)) + return 1; + + /* point entry in root table to sub-table */ + low = huff & mask; + (*table)[low].op = (unsigned char)curr; + (*table)[low].bits = (unsigned char)root; + (*table)[low].val = (unsigned short)(next - *table); + } + } + + /* fill in remaining table entry if code is incomplete (guaranteed to have + at most one remaining entry, since if the code is incomplete, the + maximum code length that was allowed to get this far is one bit) */ + if (huff != 0) { + here.op = (unsigned char)64; /* invalid code marker */ + here.bits = (unsigned char)(len - drop); + here.val = (unsigned short)0; + next[huff] = here; + } + + /* set return parameters */ + *table += used; + *bits = root; + return 0; +} diff --git a/fdbrpc/zlib/inftrees.h b/fdbrpc/zlib/inftrees.h new file mode 100644 index 0000000000..baa53a0b1a --- /dev/null +++ b/fdbrpc/zlib/inftrees.h @@ -0,0 +1,62 @@ +/* inftrees.h -- header to use inftrees.c + * Copyright (C) 1995-2005, 2010 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* Structure for decoding tables. Each entry provides either the + information needed to do the operation requested by the code that + indexed that table entry, or it provides a pointer to another + table that indexes more bits of the code. op indicates whether + the entry is a pointer to another table, a literal, a length or + distance, an end-of-block, or an invalid code. For a table + pointer, the low four bits of op is the number of index bits of + that table. For a length or distance, the low four bits of op + is the number of extra bits to get after the code. bits is + the number of bits in this code or part of the code to drop off + of the bit buffer. val is the actual byte to output in the case + of a literal, the base length or distance, or the offset from + the current table to the next table. Each entry is four bytes. */ +typedef struct { + unsigned char op; /* operation, extra bits, table bits */ + unsigned char bits; /* bits in this part of the code */ + unsigned short val; /* offset in table or code value */ +} code; + +/* op values as set by inflate_table(): + 00000000 - literal + 0000tttt - table link, tttt != 0 is the number of table index bits + 0001eeee - length or distance, eeee is the number of extra bits + 01100000 - end of block + 01000000 - invalid code + */ + +/* Maximum size of the dynamic table. The maximum number of code structures is + 1444, which is the sum of 852 for literal/length codes and 592 for distance + codes. These values were found by exhaustive searches using the program + examples/enough.c found in the zlib distribtution. The arguments to that + program are the number of symbols, the initial root table size, and the + maximum bit length of a code. "enough 286 9 15" for literal/length codes + returns returns 852, and "enough 30 6 15" for distance codes returns 592. + The initial root table size (9 or 6) is found in the fifth argument of the + inflate_table() calls in inflate.c and infback.c. If the root table size is + changed, then these maximum sizes would be need to be recalculated and + updated. */ +#define ENOUGH_LENS 852 +#define ENOUGH_DISTS 592 +#define ENOUGH (ENOUGH_LENS+ENOUGH_DISTS) + +/* Type of code to build for inflate_table() */ +typedef enum { + CODES, + LENS, + DISTS +} codetype; + +int ZLIB_INTERNAL inflate_table OF((codetype type, unsigned short FAR *lens, + unsigned codes, code FAR * FAR *table, + unsigned FAR *bits, unsigned short FAR *work)); diff --git a/fdbrpc/zlib/trees.c b/fdbrpc/zlib/trees.c new file mode 100644 index 0000000000..1fd7759ef0 --- /dev/null +++ b/fdbrpc/zlib/trees.c @@ -0,0 +1,1226 @@ +/* trees.c -- output deflated data using Huffman coding + * Copyright (C) 1995-2012 Jean-loup Gailly + * detect_data_type() function provided freely by Cosmin Truta, 2006 + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* + * ALGORITHM + * + * The "deflation" process uses several Huffman trees. The more + * common source values are represented by shorter bit sequences. + * + * Each code tree is stored in a compressed form which is itself + * a Huffman encoding of the lengths of all the code strings (in + * ascending order by source values). The actual code strings are + * reconstructed from the lengths in the inflate process, as described + * in the deflate specification. + * + * REFERENCES + * + * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification". + * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc + * + * Storer, James A. + * Data Compression: Methods and Theory, pp. 49-50. + * Computer Science Press, 1988. ISBN 0-7167-8156-5. + * + * Sedgewick, R. + * Algorithms, p290. + * Addison-Wesley, 1983. ISBN 0-201-06672-6. + */ + +/* @(#) $Id$ */ + +/* #define GEN_TREES_H */ + +#include "deflate.h" + +#ifdef DEBUG +# include +#endif + +/* =========================================================================== + * Constants + */ + +#define MAX_BL_BITS 7 +/* Bit length codes must not exceed MAX_BL_BITS bits */ + +#define END_BLOCK 256 +/* end of block literal code */ + +#define REP_3_6 16 +/* repeat previous bit length 3-6 times (2 bits of repeat count) */ + +#define REPZ_3_10 17 +/* repeat a zero length 3-10 times (3 bits of repeat count) */ + +#define REPZ_11_138 18 +/* repeat a zero length 11-138 times (7 bits of repeat count) */ + +local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */ + = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0}; + +local const int extra_dbits[D_CODES] /* extra bits for each distance code */ + = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; + +local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */ + = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; + +local const uch bl_order[BL_CODES] + = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; +/* The lengths of the bit length codes are sent in order of decreasing + * probability, to avoid transmitting the lengths for unused bit length codes. + */ + +/* =========================================================================== + * Local data. These are initialized only once. + */ + +#define DIST_CODE_LEN 512 /* see definition of array dist_code below */ + +#if defined(GEN_TREES_H) || !defined(STDC) +/* non ANSI compilers may not accept trees.h */ + +local ct_data static_ltree[L_CODES+2]; +/* The static literal tree. Since the bit lengths are imposed, there is no + * need for the L_CODES extra codes used during heap construction. However + * The codes 286 and 287 are needed to build a canonical tree (see _tr_init + * below). + */ + +local ct_data static_dtree[D_CODES]; +/* The static distance tree. (Actually a trivial tree since all codes use + * 5 bits.) + */ + +uch _dist_code[DIST_CODE_LEN]; +/* Distance codes. The first 256 values correspond to the distances + * 3 .. 258, the last 256 values correspond to the top 8 bits of + * the 15 bit distances. + */ + +uch _length_code[MAX_MATCH-MIN_MATCH+1]; +/* length code for each normalized match length (0 == MIN_MATCH) */ + +local int base_length[LENGTH_CODES]; +/* First normalized length for each code (0 = MIN_MATCH) */ + +local int base_dist[D_CODES]; +/* First normalized distance for each code (0 = distance of 1) */ + +#else +# include "trees.h" +#endif /* GEN_TREES_H */ + +struct static_tree_desc_s { + const ct_data *static_tree; /* static tree or NULL */ + const intf *extra_bits; /* extra bits for each code or NULL */ + int extra_base; /* base index for extra_bits */ + int elems; /* max number of elements in the tree */ + int max_length; /* max bit length for the codes */ +}; + +local static_tree_desc static_l_desc = +{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; + +local static_tree_desc static_d_desc = +{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; + +local static_tree_desc static_bl_desc = +{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; + +/* =========================================================================== + * Local (static) routines in this file. + */ + +local void tr_static_init OF((void)); +local void init_block OF((deflate_state *s)); +local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); +local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); +local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count)); +local void build_tree OF((deflate_state *s, tree_desc *desc)); +local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code)); +local void send_tree OF((deflate_state *s, ct_data *tree, int max_code)); +local int build_bl_tree OF((deflate_state *s)); +local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, + int blcodes)); +local void compress_block OF((deflate_state *s, const ct_data *ltree, + const ct_data *dtree)); +local int detect_data_type OF((deflate_state *s)); +local unsigned bi_reverse OF((unsigned value, int length)); +local void bi_windup OF((deflate_state *s)); +local void bi_flush OF((deflate_state *s)); +local void copy_block OF((deflate_state *s, charf *buf, unsigned len, + int header)); + +#ifdef GEN_TREES_H +local void gen_trees_header OF((void)); +#endif + +#ifndef DEBUG +# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) + /* Send a code of the given tree. c and tree must not have side effects */ + +#else /* DEBUG */ +# define send_code(s, c, tree) \ + { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ + send_bits(s, tree[c].Code, tree[c].Len); } +#endif + +/* =========================================================================== + * Output a short LSB first on the stream. + * IN assertion: there is enough room in pendingBuf. + */ +#define put_short(s, w) { \ + put_byte(s, (uch)((w) & 0xff)); \ + put_byte(s, (uch)((ush)(w) >> 8)); \ +} + +/* =========================================================================== + * Send a value on a given number of bits. + * IN assertion: length <= 16 and value fits in length bits. + */ +#ifdef DEBUG +local void send_bits OF((deflate_state *s, int value, int length)); + +local void send_bits(s, value, length) + deflate_state *s; + int value; /* value to send */ + int length; /* number of bits */ +{ + Tracevv((stderr," l %2d v %4x ", length, value)); + Assert(length > 0 && length <= 15, "invalid length"); + s->bits_sent += (ulg)length; + + /* If not enough room in bi_buf, use (valid) bits from bi_buf and + * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid)) + * unused bits in value. + */ + if (s->bi_valid > (int)Buf_size - length) { + s->bi_buf |= (ush)value << s->bi_valid; + put_short(s, s->bi_buf); + s->bi_buf = (ush)value >> (Buf_size - s->bi_valid); + s->bi_valid += length - Buf_size; + } else { + s->bi_buf |= (ush)value << s->bi_valid; + s->bi_valid += length; + } +} +#else /* !DEBUG */ + +#define send_bits(s, value, length) \ +{ int len = length;\ + if (s->bi_valid > (int)Buf_size - len) {\ + int val = value;\ + s->bi_buf |= (ush)val << s->bi_valid;\ + put_short(s, s->bi_buf);\ + s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ + s->bi_valid += len - Buf_size;\ + } else {\ + s->bi_buf |= (ush)(value) << s->bi_valid;\ + s->bi_valid += len;\ + }\ +} +#endif /* DEBUG */ + + +/* the arguments must not have side effects */ + +/* =========================================================================== + * Initialize the various 'constant' tables. + */ +local void tr_static_init() +{ +#if defined(GEN_TREES_H) || !defined(STDC) + static int static_init_done = 0; + int n; /* iterates over tree elements */ + int bits; /* bit counter */ + int length; /* length value */ + int code; /* code value */ + int dist; /* distance index */ + ush bl_count[MAX_BITS+1]; + /* number of codes at each bit length for an optimal tree */ + + if (static_init_done) return; + + /* For some embedded targets, global variables are not initialized: */ +#ifdef NO_INIT_GLOBAL_POINTERS + static_l_desc.static_tree = static_ltree; + static_l_desc.extra_bits = extra_lbits; + static_d_desc.static_tree = static_dtree; + static_d_desc.extra_bits = extra_dbits; + static_bl_desc.extra_bits = extra_blbits; +#endif + + /* Initialize the mapping length (0..255) -> length code (0..28) */ + length = 0; + for (code = 0; code < LENGTH_CODES-1; code++) { + base_length[code] = length; + for (n = 0; n < (1< dist code (0..29) */ + dist = 0; + for (code = 0 ; code < 16; code++) { + base_dist[code] = dist; + for (n = 0; n < (1<>= 7; /* from now on, all distances are divided by 128 */ + for ( ; code < D_CODES; code++) { + base_dist[code] = dist << 7; + for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) { + _dist_code[256 + dist++] = (uch)code; + } + } + Assert (dist == 256, "tr_static_init: 256+dist != 512"); + + /* Construct the codes of the static literal tree */ + for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; + n = 0; + while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; + while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; + while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; + while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; + /* Codes 286 and 287 do not exist, but we must include them in the + * tree construction to get a canonical Huffman tree (longest code + * all ones) + */ + gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); + + /* The static distance tree is trivial: */ + for (n = 0; n < D_CODES; n++) { + static_dtree[n].Len = 5; + static_dtree[n].Code = bi_reverse((unsigned)n, 5); + } + static_init_done = 1; + +# ifdef GEN_TREES_H + gen_trees_header(); +# endif +#endif /* defined(GEN_TREES_H) || !defined(STDC) */ +} + +/* =========================================================================== + * Genererate the file trees.h describing the static trees. + */ +#ifdef GEN_TREES_H +# ifndef DEBUG +# include +# endif + +# define SEPARATOR(i, last, width) \ + ((i) == (last)? "\n};\n\n" : \ + ((i) % (width) == (width)-1 ? ",\n" : ", ")) + +void gen_trees_header() +{ + FILE *header = fopen("trees.h", "w"); + int i; + + Assert (header != NULL, "Can't open trees.h"); + fprintf(header, + "/* header created automatically with -DGEN_TREES_H */\n\n"); + + fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); + for (i = 0; i < L_CODES+2; i++) { + fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, + static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); + } + + fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n"); + for (i = 0; i < D_CODES; i++) { + fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, + static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); + } + + fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n"); + for (i = 0; i < DIST_CODE_LEN; i++) { + fprintf(header, "%2u%s", _dist_code[i], + SEPARATOR(i, DIST_CODE_LEN-1, 20)); + } + + fprintf(header, + "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); + for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) { + fprintf(header, "%2u%s", _length_code[i], + SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); + } + + fprintf(header, "local const int base_length[LENGTH_CODES] = {\n"); + for (i = 0; i < LENGTH_CODES; i++) { + fprintf(header, "%1u%s", base_length[i], + SEPARATOR(i, LENGTH_CODES-1, 20)); + } + + fprintf(header, "local const int base_dist[D_CODES] = {\n"); + for (i = 0; i < D_CODES; i++) { + fprintf(header, "%5u%s", base_dist[i], + SEPARATOR(i, D_CODES-1, 10)); + } + + fclose(header); +} +#endif /* GEN_TREES_H */ + +/* =========================================================================== + * Initialize the tree data structures for a new zlib stream. + */ +void ZLIB_INTERNAL _tr_init(s) + deflate_state *s; +{ + tr_static_init(); + + s->l_desc.dyn_tree = s->dyn_ltree; + s->l_desc.stat_desc = &static_l_desc; + + s->d_desc.dyn_tree = s->dyn_dtree; + s->d_desc.stat_desc = &static_d_desc; + + s->bl_desc.dyn_tree = s->bl_tree; + s->bl_desc.stat_desc = &static_bl_desc; + + s->bi_buf = 0; + s->bi_valid = 0; +#ifdef DEBUG + s->compressed_len = 0L; + s->bits_sent = 0L; +#endif + + /* Initialize the first block of the first file: */ + init_block(s); +} + +/* =========================================================================== + * Initialize a new block. + */ +local void init_block(s) + deflate_state *s; +{ + int n; /* iterates over tree elements */ + + /* Initialize the trees. */ + for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; + for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; + for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; + + s->dyn_ltree[END_BLOCK].Freq = 1; + s->opt_len = s->static_len = 0L; + s->last_lit = s->matches = 0; +} + +#define SMALLEST 1 +/* Index within the heap array of least frequent node in the Huffman tree */ + + +/* =========================================================================== + * Remove the smallest element from the heap and recreate the heap with + * one less element. Updates heap and heap_len. + */ +#define pqremove(s, tree, top) \ +{\ + top = s->heap[SMALLEST]; \ + s->heap[SMALLEST] = s->heap[s->heap_len--]; \ + pqdownheap(s, tree, SMALLEST); \ +} + +/* =========================================================================== + * Compares to subtrees, using the tree depth as tie breaker when + * the subtrees have equal frequency. This minimizes the worst case length. + */ +#define smaller(tree, n, m, depth) \ + (tree[n].Freq < tree[m].Freq || \ + (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) + +/* =========================================================================== + * Restore the heap property by moving down the tree starting at node k, + * exchanging a node with the smallest of its two sons if necessary, stopping + * when the heap property is re-established (each father smaller than its + * two sons). + */ +local void pqdownheap(s, tree, k) + deflate_state *s; + ct_data *tree; /* the tree to restore */ + int k; /* node to move down */ +{ + int v = s->heap[k]; + int j = k << 1; /* left son of k */ + while (j <= s->heap_len) { + /* Set j to the smallest of the two sons: */ + if (j < s->heap_len && + smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { + j++; + } + /* Exit if v is smaller than both sons */ + if (smaller(tree, v, s->heap[j], s->depth)) break; + + /* Exchange v with the smallest son */ + s->heap[k] = s->heap[j]; k = j; + + /* And continue down the tree, setting j to the left son of k */ + j <<= 1; + } + s->heap[k] = v; +} + +/* =========================================================================== + * Compute the optimal bit lengths for a tree and update the total bit length + * for the current block. + * IN assertion: the fields freq and dad are set, heap[heap_max] and + * above are the tree nodes sorted by increasing frequency. + * OUT assertions: the field len is set to the optimal bit length, the + * array bl_count contains the frequencies for each bit length. + * The length opt_len is updated; static_len is also updated if stree is + * not null. + */ +local void gen_bitlen(s, desc) + deflate_state *s; + tree_desc *desc; /* the tree descriptor */ +{ + ct_data *tree = desc->dyn_tree; + int max_code = desc->max_code; + const ct_data *stree = desc->stat_desc->static_tree; + const intf *extra = desc->stat_desc->extra_bits; + int base = desc->stat_desc->extra_base; + int max_length = desc->stat_desc->max_length; + int h; /* heap index */ + int n, m; /* iterate over the tree elements */ + int bits; /* bit length */ + int xbits; /* extra bits */ + ush f; /* frequency */ + int overflow = 0; /* number of elements with bit length too large */ + + for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0; + + /* In a first pass, compute the optimal bit lengths (which may + * overflow in the case of the bit length tree). + */ + tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ + + for (h = s->heap_max+1; h < HEAP_SIZE; h++) { + n = s->heap[h]; + bits = tree[tree[n].Dad].Len + 1; + if (bits > max_length) bits = max_length, overflow++; + tree[n].Len = (ush)bits; + /* We overwrite tree[n].Dad which is no longer needed */ + + if (n > max_code) continue; /* not a leaf node */ + + s->bl_count[bits]++; + xbits = 0; + if (n >= base) xbits = extra[n-base]; + f = tree[n].Freq; + s->opt_len += (ulg)f * (bits + xbits); + if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits); + } + if (overflow == 0) return; + + Trace((stderr,"\nbit length overflow\n")); + /* This happens for example on obj2 and pic of the Calgary corpus */ + + /* Find the first bit length which could increase: */ + do { + bits = max_length-1; + while (s->bl_count[bits] == 0) bits--; + s->bl_count[bits]--; /* move one leaf down the tree */ + s->bl_count[bits+1] += 2; /* move one overflow item as its brother */ + s->bl_count[max_length]--; + /* The brother of the overflow item also moves one step up, + * but this does not affect bl_count[max_length] + */ + overflow -= 2; + } while (overflow > 0); + + /* Now recompute all bit lengths, scanning in increasing frequency. + * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all + * lengths instead of fixing only the wrong ones. This idea is taken + * from 'ar' written by Haruhiko Okumura.) + */ + for (bits = max_length; bits != 0; bits--) { + n = s->bl_count[bits]; + while (n != 0) { + m = s->heap[--h]; + if (m > max_code) continue; + if ((unsigned) tree[m].Len != (unsigned) bits) { + Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); + s->opt_len += ((long)bits - (long)tree[m].Len) + *(long)tree[m].Freq; + tree[m].Len = (ush)bits; + } + n--; + } + } +} + +/* =========================================================================== + * Generate the codes for a given tree and bit counts (which need not be + * optimal). + * IN assertion: the array bl_count contains the bit length statistics for + * the given tree and the field len is set for all tree elements. + * OUT assertion: the field code is set for all tree elements of non + * zero code length. + */ +local void gen_codes (tree, max_code, bl_count) + ct_data *tree; /* the tree to decorate */ + int max_code; /* largest code with non zero frequency */ + ushf *bl_count; /* number of codes at each bit length */ +{ + ush next_code[MAX_BITS+1]; /* next code value for each bit length */ + ush code = 0; /* running code value */ + int bits; /* bit index */ + int n; /* code index */ + + /* The distribution counts are first used to generate the code values + * without bit reversal. + */ + for (bits = 1; bits <= MAX_BITS; bits++) { + next_code[bits] = code = (code + bl_count[bits-1]) << 1; + } + /* Check that the bit counts in bl_count are consistent. The last code + * must be all ones. + */ + Assert (code + bl_count[MAX_BITS]-1 == (1<dyn_tree; + const ct_data *stree = desc->stat_desc->static_tree; + int elems = desc->stat_desc->elems; + int n, m; /* iterate over heap elements */ + int max_code = -1; /* largest code with non zero frequency */ + int node; /* new node being created */ + + /* Construct the initial heap, with least frequent element in + * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. + * heap[0] is not used. + */ + s->heap_len = 0, s->heap_max = HEAP_SIZE; + + for (n = 0; n < elems; n++) { + if (tree[n].Freq != 0) { + s->heap[++(s->heap_len)] = max_code = n; + s->depth[n] = 0; + } else { + tree[n].Len = 0; + } + } + + /* The pkzip format requires that at least one distance code exists, + * and that at least one bit should be sent even if there is only one + * possible code. So to avoid special checks later on we force at least + * two codes of non zero frequency. + */ + while (s->heap_len < 2) { + node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); + tree[node].Freq = 1; + s->depth[node] = 0; + s->opt_len--; if (stree) s->static_len -= stree[node].Len; + /* node is 0 or 1 so it does not have extra bits */ + } + desc->max_code = max_code; + + /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, + * establish sub-heaps of increasing lengths: + */ + for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); + + /* Construct the Huffman tree by repeatedly combining the least two + * frequent nodes. + */ + node = elems; /* next internal node of the tree */ + do { + pqremove(s, tree, n); /* n = node of least frequency */ + m = s->heap[SMALLEST]; /* m = node of next least frequency */ + + s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */ + s->heap[--(s->heap_max)] = m; + + /* Create a new node father of n and m */ + tree[node].Freq = tree[n].Freq + tree[m].Freq; + s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ? + s->depth[n] : s->depth[m]) + 1); + tree[n].Dad = tree[m].Dad = (ush)node; +#ifdef DUMP_BL_TREE + if (tree == s->bl_tree) { + fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)", + node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); + } +#endif + /* and insert the new node in the heap */ + s->heap[SMALLEST] = node++; + pqdownheap(s, tree, SMALLEST); + + } while (s->heap_len >= 2); + + s->heap[--(s->heap_max)] = s->heap[SMALLEST]; + + /* At this point, the fields freq and dad are set. We can now + * generate the bit lengths. + */ + gen_bitlen(s, (tree_desc *)desc); + + /* The field len is now set, we can generate the bit codes */ + gen_codes ((ct_data *)tree, max_code, s->bl_count); +} + +/* =========================================================================== + * Scan a literal or distance tree to determine the frequencies of the codes + * in the bit length tree. + */ +local void scan_tree (s, tree, max_code) + deflate_state *s; + ct_data *tree; /* the tree to be scanned */ + int max_code; /* and its largest code of non zero frequency */ +{ + int n; /* iterates over all tree elements */ + int prevlen = -1; /* last emitted length */ + int curlen; /* length of current code */ + int nextlen = tree[0].Len; /* length of next code */ + int count = 0; /* repeat count of the current code */ + int max_count = 7; /* max repeat count */ + int min_count = 4; /* min repeat count */ + + if (nextlen == 0) max_count = 138, min_count = 3; + tree[max_code+1].Len = (ush)0xffff; /* guard */ + + for (n = 0; n <= max_code; n++) { + curlen = nextlen; nextlen = tree[n+1].Len; + if (++count < max_count && curlen == nextlen) { + continue; + } else if (count < min_count) { + s->bl_tree[curlen].Freq += count; + } else if (curlen != 0) { + if (curlen != prevlen) s->bl_tree[curlen].Freq++; + s->bl_tree[REP_3_6].Freq++; + } else if (count <= 10) { + s->bl_tree[REPZ_3_10].Freq++; + } else { + s->bl_tree[REPZ_11_138].Freq++; + } + count = 0; prevlen = curlen; + if (nextlen == 0) { + max_count = 138, min_count = 3; + } else if (curlen == nextlen) { + max_count = 6, min_count = 3; + } else { + max_count = 7, min_count = 4; + } + } +} + +/* =========================================================================== + * Send a literal or distance tree in compressed form, using the codes in + * bl_tree. + */ +local void send_tree (s, tree, max_code) + deflate_state *s; + ct_data *tree; /* the tree to be scanned */ + int max_code; /* and its largest code of non zero frequency */ +{ + int n; /* iterates over all tree elements */ + int prevlen = -1; /* last emitted length */ + int curlen; /* length of current code */ + int nextlen = tree[0].Len; /* length of next code */ + int count = 0; /* repeat count of the current code */ + int max_count = 7; /* max repeat count */ + int min_count = 4; /* min repeat count */ + + /* tree[max_code+1].Len = -1; */ /* guard already set */ + if (nextlen == 0) max_count = 138, min_count = 3; + + for (n = 0; n <= max_code; n++) { + curlen = nextlen; nextlen = tree[n+1].Len; + if (++count < max_count && curlen == nextlen) { + continue; + } else if (count < min_count) { + do { send_code(s, curlen, s->bl_tree); } while (--count != 0); + + } else if (curlen != 0) { + if (curlen != prevlen) { + send_code(s, curlen, s->bl_tree); count--; + } + Assert(count >= 3 && count <= 6, " 3_6?"); + send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2); + + } else if (count <= 10) { + send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3); + + } else { + send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7); + } + count = 0; prevlen = curlen; + if (nextlen == 0) { + max_count = 138, min_count = 3; + } else if (curlen == nextlen) { + max_count = 6, min_count = 3; + } else { + max_count = 7, min_count = 4; + } + } +} + +/* =========================================================================== + * Construct the Huffman tree for the bit lengths and return the index in + * bl_order of the last bit length code to send. + */ +local int build_bl_tree(s) + deflate_state *s; +{ + int max_blindex; /* index of last bit length code of non zero freq */ + + /* Determine the bit length frequencies for literal and distance trees */ + scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); + scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); + + /* Build the bit length tree: */ + build_tree(s, (tree_desc *)(&(s->bl_desc))); + /* opt_len now includes the length of the tree representations, except + * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. + */ + + /* Determine the number of bit length codes to send. The pkzip format + * requires that at least 4 bit length codes be sent. (appnote.txt says + * 3 but the actual value used is 4.) + */ + for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { + if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; + } + /* Update opt_len to include the bit length tree and counts */ + s->opt_len += 3*(max_blindex+1) + 5+5+4; + Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", + s->opt_len, s->static_len)); + + return max_blindex; +} + +/* =========================================================================== + * Send the header for a block using dynamic Huffman trees: the counts, the + * lengths of the bit length codes, the literal tree and the distance tree. + * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. + */ +local void send_all_trees(s, lcodes, dcodes, blcodes) + deflate_state *s; + int lcodes, dcodes, blcodes; /* number of codes for each tree */ +{ + int rank; /* index in bl_order */ + + Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); + Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, + "too many codes"); + Tracev((stderr, "\nbl counts: ")); + send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */ + send_bits(s, dcodes-1, 5); + send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */ + for (rank = 0; rank < blcodes; rank++) { + Tracev((stderr, "\nbl code %2d ", bl_order[rank])); + send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); + } + Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); + + send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */ + Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); + + send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */ + Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); +} + +/* =========================================================================== + * Send a stored block + */ +void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last) + deflate_state *s; + charf *buf; /* input block */ + ulg stored_len; /* length of input block */ + int last; /* one if this is the last block for a file */ +{ + send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */ +#ifdef DEBUG + s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; + s->compressed_len += (stored_len + 4) << 3; +#endif + copy_block(s, buf, (unsigned)stored_len, 1); /* with header */ +} + +/* =========================================================================== + * Flush the bits in the bit buffer to pending output (leaves at most 7 bits) + */ +void ZLIB_INTERNAL _tr_flush_bits(s) + deflate_state *s; +{ + bi_flush(s); +} + +/* =========================================================================== + * Send one empty static block to give enough lookahead for inflate. + * This takes 10 bits, of which 7 may remain in the bit buffer. + */ +void ZLIB_INTERNAL _tr_align(s) + deflate_state *s; +{ + send_bits(s, STATIC_TREES<<1, 3); + send_code(s, END_BLOCK, static_ltree); +#ifdef DEBUG + s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ +#endif + bi_flush(s); +} + +/* =========================================================================== + * Determine the best encoding for the current block: dynamic trees, static + * trees or store, and output the encoded block to the zip file. + */ +void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) + deflate_state *s; + charf *buf; /* input block, or NULL if too old */ + ulg stored_len; /* length of input block */ + int last; /* one if this is the last block for a file */ +{ + ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ + int max_blindex = 0; /* index of last bit length code of non zero freq */ + + /* Build the Huffman trees unless a stored block is forced */ + if (s->level > 0) { + + /* Check if the file is binary or text */ + if (s->strm->data_type == Z_UNKNOWN) + s->strm->data_type = detect_data_type(s); + + /* Construct the literal and distance trees */ + build_tree(s, (tree_desc *)(&(s->l_desc))); + Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, + s->static_len)); + + build_tree(s, (tree_desc *)(&(s->d_desc))); + Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, + s->static_len)); + /* At this point, opt_len and static_len are the total bit lengths of + * the compressed block data, excluding the tree representations. + */ + + /* Build the bit length tree for the above two trees, and get the index + * in bl_order of the last bit length code to send. + */ + max_blindex = build_bl_tree(s); + + /* Determine the best encoding. Compute the block lengths in bytes. */ + opt_lenb = (s->opt_len+3+7)>>3; + static_lenb = (s->static_len+3+7)>>3; + + Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", + opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, + s->last_lit)); + + if (static_lenb <= opt_lenb) opt_lenb = static_lenb; + + } else { + Assert(buf != (char*)0, "lost buf"); + opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ + } + +#ifdef FORCE_STORED + if (buf != (char*)0) { /* force stored block */ +#else + if (stored_len+4 <= opt_lenb && buf != (char*)0) { + /* 4: two words for the lengths */ +#endif + /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. + * Otherwise we can't have processed more than WSIZE input bytes since + * the last block flush, because compression would have been + * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to + * transform a block into a stored block. + */ + _tr_stored_block(s, buf, stored_len, last); + +#ifdef FORCE_STATIC + } else if (static_lenb >= 0) { /* force static trees */ +#else + } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) { +#endif + send_bits(s, (STATIC_TREES<<1)+last, 3); + compress_block(s, (const ct_data *)static_ltree, + (const ct_data *)static_dtree); +#ifdef DEBUG + s->compressed_len += 3 + s->static_len; +#endif + } else { + send_bits(s, (DYN_TREES<<1)+last, 3); + send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, + max_blindex+1); + compress_block(s, (const ct_data *)s->dyn_ltree, + (const ct_data *)s->dyn_dtree); +#ifdef DEBUG + s->compressed_len += 3 + s->opt_len; +#endif + } + Assert (s->compressed_len == s->bits_sent, "bad compressed size"); + /* The above check is made mod 2^32, for files larger than 512 MB + * and uLong implemented on 32 bits. + */ + init_block(s); + + if (last) { + bi_windup(s); +#ifdef DEBUG + s->compressed_len += 7; /* align on byte boundary */ +#endif + } + Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, + s->compressed_len-7*last)); +} + +/* =========================================================================== + * Save the match info and tally the frequency counts. Return true if + * the current block must be flushed. + */ +int ZLIB_INTERNAL _tr_tally (s, dist, lc) + deflate_state *s; + unsigned dist; /* distance of matched string */ + unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ +{ + s->d_buf[s->last_lit] = (ush)dist; + s->l_buf[s->last_lit++] = (uch)lc; + if (dist == 0) { + /* lc is the unmatched char */ + s->dyn_ltree[lc].Freq++; + } else { + s->matches++; + /* Here, lc is the match length - MIN_MATCH */ + dist--; /* dist = match distance - 1 */ + Assert((ush)dist < (ush)MAX_DIST(s) && + (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && + (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); + + s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++; + s->dyn_dtree[d_code(dist)].Freq++; + } + +#ifdef TRUNCATE_BLOCK + /* Try to guess if it is profitable to stop the current block here */ + if ((s->last_lit & 0x1fff) == 0 && s->level > 2) { + /* Compute an upper bound for the compressed length */ + ulg out_length = (ulg)s->last_lit*8L; + ulg in_length = (ulg)((long)s->strstart - s->block_start); + int dcode; + for (dcode = 0; dcode < D_CODES; dcode++) { + out_length += (ulg)s->dyn_dtree[dcode].Freq * + (5L+extra_dbits[dcode]); + } + out_length >>= 3; + Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", + s->last_lit, in_length, out_length, + 100L - out_length*100L/in_length)); + if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1; + } +#endif + return (s->last_lit == s->lit_bufsize-1); + /* We avoid equality with lit_bufsize because of wraparound at 64K + * on 16 bit machines and because stored blocks are restricted to + * 64K-1 bytes. + */ +} + +/* =========================================================================== + * Send the block data compressed using the given Huffman trees + */ +local void compress_block(s, ltree, dtree) + deflate_state *s; + const ct_data *ltree; /* literal tree */ + const ct_data *dtree; /* distance tree */ +{ + unsigned dist; /* distance of matched string */ + int lc; /* match length or unmatched char (if dist == 0) */ + unsigned lx = 0; /* running index in l_buf */ + unsigned code; /* the code to send */ + int extra; /* number of extra bits to send */ + + if (s->last_lit != 0) do { + dist = s->d_buf[lx]; + lc = s->l_buf[lx++]; + if (dist == 0) { + send_code(s, lc, ltree); /* send a literal byte */ + Tracecv(isgraph(lc), (stderr," '%c' ", lc)); + } else { + /* Here, lc is the match length - MIN_MATCH */ + code = _length_code[lc]; + send_code(s, code+LITERALS+1, ltree); /* send the length code */ + extra = extra_lbits[code]; + if (extra != 0) { + lc -= base_length[code]; + send_bits(s, lc, extra); /* send the extra length bits */ + } + dist--; /* dist is now the match distance - 1 */ + code = d_code(dist); + Assert (code < D_CODES, "bad d_code"); + + send_code(s, code, dtree); /* send the distance code */ + extra = extra_dbits[code]; + if (extra != 0) { + dist -= base_dist[code]; + send_bits(s, dist, extra); /* send the extra distance bits */ + } + } /* literal or match pair ? */ + + /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ + Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, + "pendingBuf overflow"); + + } while (lx < s->last_lit); + + send_code(s, END_BLOCK, ltree); +} + +/* =========================================================================== + * Check if the data type is TEXT or BINARY, using the following algorithm: + * - TEXT if the two conditions below are satisfied: + * a) There are no non-portable control characters belonging to the + * "black list" (0..6, 14..25, 28..31). + * b) There is at least one printable character belonging to the + * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). + * - BINARY otherwise. + * - The following partially-portable control characters form a + * "gray list" that is ignored in this detection algorithm: + * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). + * IN assertion: the fields Freq of dyn_ltree are set. + */ +local int detect_data_type(s) + deflate_state *s; +{ + /* black_mask is the bit mask of black-listed bytes + * set bits 0..6, 14..25, and 28..31 + * 0xf3ffc07f = binary 11110011111111111100000001111111 + */ + unsigned long black_mask = 0xf3ffc07fUL; + int n; + + /* Check for non-textual ("black-listed") bytes. */ + for (n = 0; n <= 31; n++, black_mask >>= 1) + if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0)) + return Z_BINARY; + + /* Check for textual ("white-listed") bytes. */ + if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 + || s->dyn_ltree[13].Freq != 0) + return Z_TEXT; + for (n = 32; n < LITERALS; n++) + if (s->dyn_ltree[n].Freq != 0) + return Z_TEXT; + + /* There are no "black-listed" or "white-listed" bytes: + * this stream either is empty or has tolerated ("gray-listed") bytes only. + */ + return Z_BINARY; +} + +/* =========================================================================== + * Reverse the first len bits of a code, using straightforward code (a faster + * method would use a table) + * IN assertion: 1 <= len <= 15 + */ +local unsigned bi_reverse(code, len) + unsigned code; /* the value to invert */ + int len; /* its bit length */ +{ + register unsigned res = 0; + do { + res |= code & 1; + code >>= 1, res <<= 1; + } while (--len > 0); + return res >> 1; +} + +/* =========================================================================== + * Flush the bit buffer, keeping at most 7 bits in it. + */ +local void bi_flush(s) + deflate_state *s; +{ + if (s->bi_valid == 16) { + put_short(s, s->bi_buf); + s->bi_buf = 0; + s->bi_valid = 0; + } else if (s->bi_valid >= 8) { + put_byte(s, (Byte)s->bi_buf); + s->bi_buf >>= 8; + s->bi_valid -= 8; + } +} + +/* =========================================================================== + * Flush the bit buffer and align the output on a byte boundary + */ +local void bi_windup(s) + deflate_state *s; +{ + if (s->bi_valid > 8) { + put_short(s, s->bi_buf); + } else if (s->bi_valid > 0) { + put_byte(s, (Byte)s->bi_buf); + } + s->bi_buf = 0; + s->bi_valid = 0; +#ifdef DEBUG + s->bits_sent = (s->bits_sent+7) & ~7; +#endif +} + +/* =========================================================================== + * Copy a stored block, storing first the length and its + * one's complement if requested. + */ +local void copy_block(s, buf, len, header) + deflate_state *s; + charf *buf; /* the input data */ + unsigned len; /* its length */ + int header; /* true if block header must be written */ +{ + bi_windup(s); /* align on byte boundary */ + + if (header) { + put_short(s, (ush)len); + put_short(s, (ush)~len); +#ifdef DEBUG + s->bits_sent += 2*16; +#endif + } +#ifdef DEBUG + s->bits_sent += (ulg)len<<3; +#endif + while (len--) { + put_byte(s, *buf++); + } +} diff --git a/fdbrpc/zlib/trees.h b/fdbrpc/zlib/trees.h new file mode 100644 index 0000000000..fa4f152b28 --- /dev/null +++ b/fdbrpc/zlib/trees.h @@ -0,0 +1,148 @@ +/* + * trees.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* header created automatically with -DGEN_TREES_H */ + +local const ct_data static_ltree[L_CODES+2] = { +{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}}, +{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}}, +{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}}, +{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}}, +{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}}, +{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}}, +{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}}, +{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}}, +{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}}, +{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}}, +{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}}, +{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}}, +{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}}, +{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}}, +{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}}, +{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}}, +{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}}, +{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}}, +{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}}, +{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}}, +{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}}, +{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}}, +{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}}, +{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}}, +{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}}, +{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}}, +{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}}, +{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}}, +{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}}, +{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}}, +{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}}, +{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}}, +{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}}, +{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}}, +{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}}, +{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}}, +{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}}, +{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}}, +{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}}, +{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}}, +{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}}, +{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}}, +{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}}, +{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}}, +{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}}, +{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}}, +{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}}, +{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}}, +{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}}, +{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}}, +{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}}, +{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}}, +{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}}, +{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}}, +{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}}, +{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}}, +{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}}, +{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}} +}; + +local const ct_data static_dtree[D_CODES] = { +{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}}, +{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}}, +{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}}, +{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}}, +{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}}, +{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}} +}; + +const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = { + 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, + 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, +10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, +11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, +12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, +13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, +13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, +14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, +15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17, +18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, +24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, +28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, +29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 +}; + +const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, +13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, +17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, +19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, +21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, +22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, +23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, +24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, +25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, +26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, +26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, +27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 +}; + +local const int base_length[LENGTH_CODES] = { +0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, +64, 80, 96, 112, 128, 160, 192, 224, 0 +}; + +local const int base_dist[D_CODES] = { + 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, + 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, + 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 +}; + diff --git a/fdbrpc/zlib/zconf.h b/fdbrpc/zlib/zconf.h new file mode 100644 index 0000000000..9987a77553 --- /dev/null +++ b/fdbrpc/zlib/zconf.h @@ -0,0 +1,511 @@ +/* zconf.h -- configuration of the zlib compression library + * Copyright (C) 1995-2013 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#ifndef ZCONF_H +#define ZCONF_H + +/* + * If you *really* need a unique prefix for all types and library functions, + * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it. + * Even better than compiling with -DZ_PREFIX would be to use configure to set + * this permanently in zconf.h using "./configure --zprefix". + */ +#ifdef Z_PREFIX /* may be set to #if 1 by ./configure */ +# define Z_PREFIX_SET + +/* all linked symbols */ +# define _dist_code z__dist_code +# define _length_code z__length_code +# define _tr_align z__tr_align +# define _tr_flush_bits z__tr_flush_bits +# define _tr_flush_block z__tr_flush_block +# define _tr_init z__tr_init +# define _tr_stored_block z__tr_stored_block +# define _tr_tally z__tr_tally +# define adler32 z_adler32 +# define adler32_combine z_adler32_combine +# define adler32_combine64 z_adler32_combine64 +# ifndef Z_SOLO +# define compress z_compress +# define compress2 z_compress2 +# define compressBound z_compressBound +# endif +# define crc32 z_crc32 +# define crc32_combine z_crc32_combine +# define crc32_combine64 z_crc32_combine64 +# define deflate z_deflate +# define deflateBound z_deflateBound +# define deflateCopy z_deflateCopy +# define deflateEnd z_deflateEnd +# define deflateInit2_ z_deflateInit2_ +# define deflateInit_ z_deflateInit_ +# define deflateParams z_deflateParams +# define deflatePending z_deflatePending +# define deflatePrime z_deflatePrime +# define deflateReset z_deflateReset +# define deflateResetKeep z_deflateResetKeep +# define deflateSetDictionary z_deflateSetDictionary +# define deflateSetHeader z_deflateSetHeader +# define deflateTune z_deflateTune +# define deflate_copyright z_deflate_copyright +# define get_crc_table z_get_crc_table +# ifndef Z_SOLO +# define gz_error z_gz_error +# define gz_intmax z_gz_intmax +# define gz_strwinerror z_gz_strwinerror +# define gzbuffer z_gzbuffer +# define gzclearerr z_gzclearerr +# define gzclose z_gzclose +# define gzclose_r z_gzclose_r +# define gzclose_w z_gzclose_w +# define gzdirect z_gzdirect +# define gzdopen z_gzdopen +# define gzeof z_gzeof +# define gzerror z_gzerror +# define gzflush z_gzflush +# define gzgetc z_gzgetc +# define gzgetc_ z_gzgetc_ +# define gzgets z_gzgets +# define gzoffset z_gzoffset +# define gzoffset64 z_gzoffset64 +# define gzopen z_gzopen +# define gzopen64 z_gzopen64 +# ifdef _WIN32 +# define gzopen_w z_gzopen_w +# endif +# define gzprintf z_gzprintf +# define gzvprintf z_gzvprintf +# define gzputc z_gzputc +# define gzputs z_gzputs +# define gzread z_gzread +# define gzrewind z_gzrewind +# define gzseek z_gzseek +# define gzseek64 z_gzseek64 +# define gzsetparams z_gzsetparams +# define gztell z_gztell +# define gztell64 z_gztell64 +# define gzungetc z_gzungetc +# define gzwrite z_gzwrite +# endif +# define inflate z_inflate +# define inflateBack z_inflateBack +# define inflateBackEnd z_inflateBackEnd +# define inflateBackInit_ z_inflateBackInit_ +# define inflateCopy z_inflateCopy +# define inflateEnd z_inflateEnd +# define inflateGetHeader z_inflateGetHeader +# define inflateInit2_ z_inflateInit2_ +# define inflateInit_ z_inflateInit_ +# define inflateMark z_inflateMark +# define inflatePrime z_inflatePrime +# define inflateReset z_inflateReset +# define inflateReset2 z_inflateReset2 +# define inflateSetDictionary z_inflateSetDictionary +# define inflateGetDictionary z_inflateGetDictionary +# define inflateSync z_inflateSync +# define inflateSyncPoint z_inflateSyncPoint +# define inflateUndermine z_inflateUndermine +# define inflateResetKeep z_inflateResetKeep +# define inflate_copyright z_inflate_copyright +# define inflate_fast z_inflate_fast +# define inflate_table z_inflate_table +# ifndef Z_SOLO +# define uncompress z_uncompress +# endif +# define zError z_zError +# ifndef Z_SOLO +# define zcalloc z_zcalloc +# define zcfree z_zcfree +# endif +# define zlibCompileFlags z_zlibCompileFlags +# define zlibVersion z_zlibVersion + +/* all zlib typedefs in zlib.h and zconf.h */ +# define Byte z_Byte +# define Bytef z_Bytef +# define alloc_func z_alloc_func +# define charf z_charf +# define free_func z_free_func +# ifndef Z_SOLO +# define gzFile z_gzFile +# endif +# define gz_header z_gz_header +# define gz_headerp z_gz_headerp +# define in_func z_in_func +# define intf z_intf +# define out_func z_out_func +# define uInt z_uInt +# define uIntf z_uIntf +# define uLong z_uLong +# define uLongf z_uLongf +# define voidp z_voidp +# define voidpc z_voidpc +# define voidpf z_voidpf + +/* all zlib structs in zlib.h and zconf.h */ +# define gz_header_s z_gz_header_s +# define internal_state z_internal_state + +#endif + +#if defined(__MSDOS__) && !defined(MSDOS) +# define MSDOS +#endif +#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2) +# define OS2 +#endif +#if defined(_WINDOWS) && !defined(WINDOWS) +# define WINDOWS +#endif +#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__) +# ifndef WIN32 +# define WIN32 +# endif +#endif +#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32) +# if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__) +# ifndef SYS16BIT +# define SYS16BIT +# endif +# endif +#endif + +/* + * Compile with -DMAXSEG_64K if the alloc function cannot allocate more + * than 64k bytes at a time (needed on systems with 16-bit int). + */ +#ifdef SYS16BIT +# define MAXSEG_64K +#endif +#ifdef MSDOS +# define UNALIGNED_OK +#endif + +#ifdef __STDC_VERSION__ +# ifndef STDC +# define STDC +# endif +# if __STDC_VERSION__ >= 199901L +# ifndef STDC99 +# define STDC99 +# endif +# endif +#endif +#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus)) +# define STDC +#endif +#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__)) +# define STDC +#endif +#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32)) +# define STDC +#endif +#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__)) +# define STDC +#endif + +#if defined(__OS400__) && !defined(STDC) /* iSeries (formerly AS/400). */ +# define STDC +#endif + +#ifndef STDC +# ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */ +# define const /* note: need a more gentle solution here */ +# endif +#endif + +#if defined(ZLIB_CONST) && !defined(z_const) +# define z_const const +#else +# define z_const +#endif + +/* Some Mac compilers merge all .h files incorrectly: */ +#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__) +# define NO_DUMMY_DECL +#endif + +/* Maximum value for memLevel in deflateInit2 */ +#ifndef MAX_MEM_LEVEL +# ifdef MAXSEG_64K +# define MAX_MEM_LEVEL 8 +# else +# define MAX_MEM_LEVEL 9 +# endif +#endif + +/* Maximum value for windowBits in deflateInit2 and inflateInit2. + * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files + * created by gzip. (Files created by minigzip can still be extracted by + * gzip.) + */ +#ifndef MAX_WBITS +# define MAX_WBITS 15 /* 32K LZ77 window */ +#endif + +/* The memory requirements for deflate are (in bytes): + (1 << (windowBits+2)) + (1 << (memLevel+9)) + that is: 128K for windowBits=15 + 128K for memLevel = 8 (default values) + plus a few kilobytes for small objects. For example, if you want to reduce + the default memory requirements from 256K to 128K, compile with + make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7" + Of course this will generally degrade compression (there's no free lunch). + + The memory requirements for inflate are (in bytes) 1 << windowBits + that is, 32K for windowBits=15 (default value) plus a few kilobytes + for small objects. +*/ + + /* Type declarations */ + +#ifndef OF /* function prototypes */ +# ifdef STDC +# define OF(args) args +# else +# define OF(args) () +# endif +#endif + +#ifndef Z_ARG /* function prototypes for stdarg */ +# if defined(STDC) || defined(Z_HAVE_STDARG_H) +# define Z_ARG(args) args +# else +# define Z_ARG(args) () +# endif +#endif + +/* The following definitions for FAR are needed only for MSDOS mixed + * model programming (small or medium model with some far allocations). + * This was tested only with MSC; for other MSDOS compilers you may have + * to define NO_MEMCPY in zutil.h. If you don't need the mixed model, + * just define FAR to be empty. + */ +#ifdef SYS16BIT +# if defined(M_I86SM) || defined(M_I86MM) + /* MSC small or medium model */ +# define SMALL_MEDIUM +# ifdef _MSC_VER +# define FAR _far +# else +# define FAR far +# endif +# endif +# if (defined(__SMALL__) || defined(__MEDIUM__)) + /* Turbo C small or medium model */ +# define SMALL_MEDIUM +# ifdef __BORLANDC__ +# define FAR _far +# else +# define FAR far +# endif +# endif +#endif + +#if defined(WINDOWS) || defined(WIN32) + /* If building or using zlib as a DLL, define ZLIB_DLL. + * This is not mandatory, but it offers a little performance increase. + */ +# ifdef ZLIB_DLL +# if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500)) +# ifdef ZLIB_INTERNAL +# define ZEXTERN extern __declspec(dllexport) +# else +# define ZEXTERN extern __declspec(dllimport) +# endif +# endif +# endif /* ZLIB_DLL */ + /* If building or using zlib with the WINAPI/WINAPIV calling convention, + * define ZLIB_WINAPI. + * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI. + */ +# ifdef ZLIB_WINAPI +# ifdef FAR +# undef FAR +# endif +# include + /* No need for _export, use ZLIB.DEF instead. */ + /* For complete Windows compatibility, use WINAPI, not __stdcall. */ +# define ZEXPORT WINAPI +# ifdef WIN32 +# define ZEXPORTVA WINAPIV +# else +# define ZEXPORTVA FAR CDECL +# endif +# endif +#endif + +#if defined (__BEOS__) +# ifdef ZLIB_DLL +# ifdef ZLIB_INTERNAL +# define ZEXPORT __declspec(dllexport) +# define ZEXPORTVA __declspec(dllexport) +# else +# define ZEXPORT __declspec(dllimport) +# define ZEXPORTVA __declspec(dllimport) +# endif +# endif +#endif + +#ifndef ZEXTERN +# define ZEXTERN extern +#endif +#ifndef ZEXPORT +# define ZEXPORT +#endif +#ifndef ZEXPORTVA +# define ZEXPORTVA +#endif + +#ifndef FAR +# define FAR +#endif + +#if !defined(__MACTYPES__) +typedef unsigned char Byte; /* 8 bits */ +#endif +typedef unsigned int uInt; /* 16 bits or more */ +typedef unsigned long uLong; /* 32 bits or more */ + +#ifdef SMALL_MEDIUM + /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */ +# define Bytef Byte FAR +#else + typedef Byte FAR Bytef; +#endif +typedef char FAR charf; +typedef int FAR intf; +typedef uInt FAR uIntf; +typedef uLong FAR uLongf; + +#ifdef STDC + typedef void const *voidpc; + typedef void FAR *voidpf; + typedef void *voidp; +#else + typedef Byte const *voidpc; + typedef Byte FAR *voidpf; + typedef Byte *voidp; +#endif + +#if !defined(Z_U4) && !defined(Z_SOLO) && defined(STDC) +# include +# if (UINT_MAX == 0xffffffffUL) +# define Z_U4 unsigned +# elif (ULONG_MAX == 0xffffffffUL) +# define Z_U4 unsigned long +# elif (USHRT_MAX == 0xffffffffUL) +# define Z_U4 unsigned short +# endif +#endif + +#ifdef Z_U4 + typedef Z_U4 z_crc_t; +#else + typedef unsigned long z_crc_t; +#endif + +#ifdef HAVE_UNISTD_H /* may be set to #if 1 by ./configure */ +# define Z_HAVE_UNISTD_H +#endif + +#ifdef HAVE_STDARG_H /* may be set to #if 1 by ./configure */ +# define Z_HAVE_STDARG_H +#endif + +#ifdef STDC +# ifndef Z_SOLO +# include /* for off_t */ +# endif +#endif + +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifndef Z_SOLO +# include /* for va_list */ +# endif +#endif + +#ifdef _WIN32 +# ifndef Z_SOLO +# include /* for wchar_t */ +# endif +#endif + +/* a little trick to accommodate both "#define _LARGEFILE64_SOURCE" and + * "#define _LARGEFILE64_SOURCE 1" as requesting 64-bit operations, (even + * though the former does not conform to the LFS document), but considering + * both "#undef _LARGEFILE64_SOURCE" and "#define _LARGEFILE64_SOURCE 0" as + * equivalently requesting no 64-bit operations + */ +#if defined(_LARGEFILE64_SOURCE) && -_LARGEFILE64_SOURCE - -1 == 1 +# undef _LARGEFILE64_SOURCE +#endif + +#if defined(__WATCOMC__) && !defined(Z_HAVE_UNISTD_H) +# define Z_HAVE_UNISTD_H +#endif +#ifndef Z_SOLO +# if defined(Z_HAVE_UNISTD_H) || defined(_LARGEFILE64_SOURCE) +# include /* for SEEK_*, off_t, and _LFS64_LARGEFILE */ +# ifdef VMS +# include /* for off_t */ +# endif +# ifndef z_off_t +# define z_off_t off_t +# endif +# endif +#endif + +#if defined(_LFS64_LARGEFILE) && _LFS64_LARGEFILE-0 +# define Z_LFS64 +#endif + +#if defined(_LARGEFILE64_SOURCE) && defined(Z_LFS64) +# define Z_LARGE64 +#endif + +#if defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS-0 == 64 && defined(Z_LFS64) +# define Z_WANT64 +#endif + +#if !defined(SEEK_SET) && !defined(Z_SOLO) +# define SEEK_SET 0 /* Seek from beginning of file. */ +# define SEEK_CUR 1 /* Seek from current position. */ +# define SEEK_END 2 /* Set file pointer to EOF plus "offset" */ +#endif + +#ifndef z_off_t +# define z_off_t long +#endif + +#if !defined(_WIN32) && defined(Z_LARGE64) +# define z_off64_t off64_t +#else +# if defined(_WIN32) && !defined(__GNUC__) && !defined(Z_SOLO) +# define z_off64_t __int64 +# else +# define z_off64_t z_off_t +# endif +#endif + +/* MVS linker does not support external names larger than 8 bytes */ +#if defined(__MVS__) + #pragma map(deflateInit_,"DEIN") + #pragma map(deflateInit2_,"DEIN2") + #pragma map(deflateEnd,"DEEND") + #pragma map(deflateBound,"DEBND") + #pragma map(inflateInit_,"ININ") + #pragma map(inflateInit2_,"ININ2") + #pragma map(inflateEnd,"INEND") + #pragma map(inflateSync,"INSY") + #pragma map(inflateSetDictionary,"INSEDI") + #pragma map(compressBound,"CMBND") + #pragma map(inflate_table,"INTABL") + #pragma map(inflate_fast,"INFA") + #pragma map(inflate_copyright,"INCOPY") +#endif + +#endif /* ZCONF_H */ diff --git a/fdbrpc/zlib/zlib.h b/fdbrpc/zlib/zlib.h new file mode 100644 index 0000000000..3e0c7672ac --- /dev/null +++ b/fdbrpc/zlib/zlib.h @@ -0,0 +1,1768 @@ +/* zlib.h -- interface of the 'zlib' general purpose compression library + version 1.2.8, April 28th, 2013 + + Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any damages + arising from the use of this software. + + Permission is granted to anyone to use this software for any purpose, + including commercial applications, and to alter it and redistribute it + freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + 3. This notice may not be removed or altered from any source distribution. + + Jean-loup Gailly Mark Adler + jloup@gzip.org madler@alumni.caltech.edu + + + The data format used by the zlib library is described by RFCs (Request for + Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 + (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). +*/ + +#ifndef ZLIB_H +#define ZLIB_H + +#include "zconf.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define ZLIB_VERSION "1.2.8" +#define ZLIB_VERNUM 0x1280 +#define ZLIB_VER_MAJOR 1 +#define ZLIB_VER_MINOR 2 +#define ZLIB_VER_REVISION 8 +#define ZLIB_VER_SUBREVISION 0 + +/* + The 'zlib' compression library provides in-memory compression and + decompression functions, including integrity checks of the uncompressed data. + This version of the library supports only one compression method (deflation) + but other algorithms will be added later and will have the same stream + interface. + + Compression can be done in a single step if the buffers are large enough, + or can be done by repeated calls of the compression function. In the latter + case, the application must provide more input and/or consume the output + (providing more output space) before each call. + + The compressed data format used by default by the in-memory functions is + the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped + around a deflate stream, which is itself documented in RFC 1951. + + The library also supports reading and writing files in gzip (.gz) format + with an interface similar to that of stdio using the functions that start + with "gz". The gzip format is different from the zlib format. gzip is a + gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. + + This library can optionally read and write gzip streams in memory as well. + + The zlib format was designed to be compact and fast for use in memory + and on communications channels. The gzip format was designed for single- + file compression on file systems, has a larger header than zlib to maintain + directory information, and uses a different, slower check method than zlib. + + The library does not install any signal handler. The decoder checks + the consistency of the compressed data, so the library should never crash + even in case of corrupted input. +*/ + +typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size)); +typedef void (*free_func) OF((voidpf opaque, voidpf address)); + +struct internal_state; + +typedef struct z_stream_s { + z_const Bytef *next_in; /* next input byte */ + uInt avail_in; /* number of bytes available at next_in */ + uLong total_in; /* total number of input bytes read so far */ + + Bytef *next_out; /* next output byte should be put there */ + uInt avail_out; /* remaining free space at next_out */ + uLong total_out; /* total number of bytes output so far */ + + z_const char *msg; /* last error message, NULL if no error */ + struct internal_state FAR *state; /* not visible by applications */ + + alloc_func zalloc; /* used to allocate the internal state */ + free_func zfree; /* used to free the internal state */ + voidpf opaque; /* private data object passed to zalloc and zfree */ + + int data_type; /* best guess about the data type: binary or text */ + uLong adler; /* adler32 value of the uncompressed data */ + uLong reserved; /* reserved for future use */ +} z_stream; + +typedef z_stream FAR *z_streamp; + +/* + gzip header information passed to and from zlib routines. See RFC 1952 + for more details on the meanings of these fields. +*/ +typedef struct gz_header_s { + int text; /* true if compressed data believed to be text */ + uLong time; /* modification time */ + int xflags; /* extra flags (not used when writing a gzip file) */ + int os; /* operating system */ + Bytef *extra; /* pointer to extra field or Z_NULL if none */ + uInt extra_len; /* extra field length (valid if extra != Z_NULL) */ + uInt extra_max; /* space at extra (only when reading header) */ + Bytef *name; /* pointer to zero-terminated file name or Z_NULL */ + uInt name_max; /* space at name (only when reading header) */ + Bytef *comment; /* pointer to zero-terminated comment or Z_NULL */ + uInt comm_max; /* space at comment (only when reading header) */ + int hcrc; /* true if there was or will be a header crc */ + int done; /* true when done reading gzip header (not used + when writing a gzip file) */ +} gz_header; + +typedef gz_header FAR *gz_headerp; + +/* + The application must update next_in and avail_in when avail_in has dropped + to zero. It must update next_out and avail_out when avail_out has dropped + to zero. The application must initialize zalloc, zfree and opaque before + calling the init function. All other fields are set by the compression + library and must not be updated by the application. + + The opaque value provided by the application will be passed as the first + parameter for calls of zalloc and zfree. This can be useful for custom + memory management. The compression library attaches no meaning to the + opaque value. + + zalloc must return Z_NULL if there is not enough memory for the object. + If zlib is used in a multi-threaded application, zalloc and zfree must be + thread safe. + + On 16-bit systems, the functions zalloc and zfree must be able to allocate + exactly 65536 bytes, but will not be required to allocate more than this if + the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS, pointers + returned by zalloc for objects of exactly 65536 bytes *must* have their + offset normalized to zero. The default allocation function provided by this + library ensures this (see zutil.c). To reduce memory requirements and avoid + any allocation of 64K objects, at the expense of compression ratio, compile + the library with -DMAX_WBITS=14 (see zconf.h). + + The fields total_in and total_out can be used for statistics or progress + reports. After compression, total_in holds the total size of the + uncompressed data and may be saved for use in the decompressor (particularly + if the decompressor wants to decompress everything in a single step). +*/ + + /* constants */ + +#define Z_NO_FLUSH 0 +#define Z_PARTIAL_FLUSH 1 +#define Z_SYNC_FLUSH 2 +#define Z_FULL_FLUSH 3 +#define Z_FINISH 4 +#define Z_BLOCK 5 +#define Z_TREES 6 +/* Allowed flush values; see deflate() and inflate() below for details */ + +#define Z_OK 0 +#define Z_STREAM_END 1 +#define Z_NEED_DICT 2 +#define Z_ERRNO (-1) +#define Z_STREAM_ERROR (-2) +#define Z_DATA_ERROR (-3) +#define Z_MEM_ERROR (-4) +#define Z_BUF_ERROR (-5) +#define Z_VERSION_ERROR (-6) +/* Return codes for the compression/decompression functions. Negative values + * are errors, positive values are used for special but normal events. + */ + +#define Z_NO_COMPRESSION 0 +#define Z_BEST_SPEED 1 +#define Z_BEST_COMPRESSION 9 +#define Z_DEFAULT_COMPRESSION (-1) +/* compression levels */ + +#define Z_FILTERED 1 +#define Z_HUFFMAN_ONLY 2 +#define Z_RLE 3 +#define Z_FIXED 4 +#define Z_DEFAULT_STRATEGY 0 +/* compression strategy; see deflateInit2() below for details */ + +#define Z_BINARY 0 +#define Z_TEXT 1 +#define Z_ASCII Z_TEXT /* for compatibility with 1.2.2 and earlier */ +#define Z_UNKNOWN 2 +/* Possible values of the data_type field (though see inflate()) */ + +#define Z_DEFLATED 8 +/* The deflate compression method (the only one supported in this version) */ + +#define Z_NULL 0 /* for initializing zalloc, zfree, opaque */ + +#define zlib_version zlibVersion() +/* for compatibility with versions < 1.0.2 */ + + + /* basic functions */ + +ZEXTERN const char * ZEXPORT zlibVersion OF((void)); +/* The application can compare zlibVersion and ZLIB_VERSION for consistency. + If the first character differs, the library code actually used is not + compatible with the zlib.h header file used by the application. This check + is automatically made by deflateInit and inflateInit. + */ + +/* +ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level)); + + Initializes the internal stream state for compression. The fields + zalloc, zfree and opaque must be initialized before by the caller. If + zalloc and zfree are set to Z_NULL, deflateInit updates them to use default + allocation functions. + + The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9: + 1 gives best speed, 9 gives best compression, 0 gives no compression at all + (the input data is simply copied a block at a time). Z_DEFAULT_COMPRESSION + requests a default compromise between speed and compression (currently + equivalent to level 6). + + deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if level is not a valid compression level, or + Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible + with the version assumed by the caller (ZLIB_VERSION). msg is set to null + if there is no error message. deflateInit does not perform any compression: + this will be done by deflate(). +*/ + + +ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush)); +/* + deflate compresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. deflate performs one or both of the + following actions: + + - Compress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), next_in and avail_in are updated and + processing will resume at this point for the next call of deflate(). + + - Provide more output starting at next_out and update next_out and avail_out + accordingly. This action is forced if the parameter flush is non zero. + Forcing flush frequently degrades the compression ratio, so this parameter + should be set only when necessary (in interactive applications). Some + output may be provided even if flush is not set. + + Before the call of deflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming more + output, and updating avail_in or avail_out accordingly; avail_out should + never be zero before the call. The application can consume the compressed + output when it wants, for example when the output buffer is full (avail_out + == 0), or after each call of deflate(). If deflate returns Z_OK and with + zero avail_out, it must be called again after making room in the output + buffer because there might be more output pending. + + Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to + decide how much data to accumulate before producing output, in order to + maximize compression. + + If the parameter flush is set to Z_SYNC_FLUSH, all pending output is + flushed to the output buffer and the output is aligned on a byte boundary, so + that the decompressor can get all input data available so far. (In + particular avail_in is zero after the call if enough output space has been + provided before the call.) Flushing may degrade compression for some + compression algorithms and so it should be used only when necessary. This + completes the current deflate block and follows it with an empty stored block + that is three bits plus filler bits to the next byte, followed by four bytes + (00 00 ff ff). + + If flush is set to Z_PARTIAL_FLUSH, all pending output is flushed to the + output buffer, but the output is not aligned to a byte boundary. All of the + input data so far will be available to the decompressor, as for Z_SYNC_FLUSH. + This completes the current deflate block and follows it with an empty fixed + codes block that is 10 bits long. This assures that enough bytes are output + in order for the decompressor to finish the block before the empty fixed code + block. + + If flush is set to Z_BLOCK, a deflate block is completed and emitted, as + for Z_SYNC_FLUSH, but the output is not aligned on a byte boundary, and up to + seven bits of the current block are held to be written as the next byte after + the next deflate block is completed. In this case, the decompressor may not + be provided enough bits at this point in order to complete decompression of + the data provided so far to the compressor. It may need to wait for the next + block to be emitted. This is for advanced applications that need to control + the emission of deflate blocks. + + If flush is set to Z_FULL_FLUSH, all output is flushed as with + Z_SYNC_FLUSH, and the compression state is reset so that decompression can + restart from this point if previous compressed data has been damaged or if + random access is desired. Using Z_FULL_FLUSH too often can seriously degrade + compression. + + If deflate returns with avail_out == 0, this function must be called again + with the same value of the flush parameter and more output space (updated + avail_out), until the flush is complete (deflate returns with non-zero + avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that + avail_out is greater than six to avoid repeated flush markers due to + avail_out == 0 on return. + + If the parameter flush is set to Z_FINISH, pending input is processed, + pending output is flushed and deflate returns with Z_STREAM_END if there was + enough output space; if deflate returns with Z_OK, this function must be + called again with Z_FINISH and more output space (updated avail_out) but no + more input data, until it returns with Z_STREAM_END or an error. After + deflate has returned Z_STREAM_END, the only possible operations on the stream + are deflateReset or deflateEnd. + + Z_FINISH can be used immediately after deflateInit if all the compression + is to be done in a single step. In this case, avail_out must be at least the + value returned by deflateBound (see below). Then deflate is guaranteed to + return Z_STREAM_END. If not enough output space is provided, deflate will + not return Z_STREAM_END, and it must be called again as described above. + + deflate() sets strm->adler to the adler32 checksum of all input read + so far (that is, total_in bytes). + + deflate() may update strm->data_type if it can make a good guess about + the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered + binary. This field is only for information purposes and does not affect the + compression algorithm in any manner. + + deflate() returns Z_OK if some progress has been made (more input + processed or more output produced), Z_STREAM_END if all input has been + consumed and all output has been produced (only when flush is set to + Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example + if next_in or next_out was Z_NULL), Z_BUF_ERROR if no progress is possible + (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not + fatal, and deflate() can be called again with more input and more output + space to continue compressing. +*/ + + +ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any pending + output. + + deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the + stream state was inconsistent, Z_DATA_ERROR if the stream was freed + prematurely (some input or output was discarded). In the error case, msg + may be set but then points to a static string (which must not be + deallocated). +*/ + + +/* +ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm)); + + Initializes the internal stream state for decompression. The fields + next_in, avail_in, zalloc, zfree and opaque must be initialized before by + the caller. If next_in is not Z_NULL and avail_in is large enough (the + exact value depends on the compression method), inflateInit determines the + compression method from the zlib header and allocates all data structures + accordingly; otherwise the allocation will be deferred to the first call of + inflate. If zalloc and zfree are set to Z_NULL, inflateInit updates them to + use default allocation functions. + + inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller, or Z_STREAM_ERROR if the parameters are + invalid, such as a null pointer to the structure. msg is set to null if + there is no error message. inflateInit does not perform any decompression + apart from possibly reading the zlib header if present: actual decompression + will be done by inflate(). (So next_in and avail_in may be modified, but + next_out and avail_out are unused and unchanged.) The current implementation + of inflateInit() does not process any header information -- that is deferred + until inflate() is called. +*/ + + +ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush)); +/* + inflate decompresses as much data as possible, and stops when the input + buffer becomes empty or the output buffer becomes full. It may introduce + some output latency (reading input without producing any output) except when + forced to flush. + + The detailed semantics are as follows. inflate performs one or both of the + following actions: + + - Decompress more input starting at next_in and update next_in and avail_in + accordingly. If not all input can be processed (because there is not + enough room in the output buffer), next_in is updated and processing will + resume at this point for the next call of inflate(). + + - Provide more output starting at next_out and update next_out and avail_out + accordingly. inflate() provides as much output as possible, until there is + no more input data or no more space in the output buffer (see below about + the flush parameter). + + Before the call of inflate(), the application should ensure that at least + one of the actions is possible, by providing more input and/or consuming more + output, and updating the next_* and avail_* values accordingly. The + application can consume the uncompressed output when it wants, for example + when the output buffer is full (avail_out == 0), or after each call of + inflate(). If inflate returns Z_OK and with zero avail_out, it must be + called again after making room in the output buffer because there might be + more output pending. + + The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH, Z_FINISH, + Z_BLOCK, or Z_TREES. Z_SYNC_FLUSH requests that inflate() flush as much + output as possible to the output buffer. Z_BLOCK requests that inflate() + stop if and when it gets to the next deflate block boundary. When decoding + the zlib or gzip format, this will cause inflate() to return immediately + after the header and before the first block. When doing a raw inflate, + inflate() will go ahead and process the first block, and will return when it + gets to the end of that block, or when it runs out of data. + + The Z_BLOCK option assists in appending to or combining deflate streams. + Also to assist in this, on return inflate() will set strm->data_type to the + number of unused bits in the last byte taken from strm->next_in, plus 64 if + inflate() is currently decoding the last block in the deflate stream, plus + 128 if inflate() returned immediately after decoding an end-of-block code or + decoding the complete header up to just before the first byte of the deflate + stream. The end-of-block will not be indicated until all of the uncompressed + data from that block has been written to strm->next_out. The number of + unused bits may in general be greater than seven, except when bit 7 of + data_type is set, in which case the number of unused bits will be less than + eight. data_type is set as noted here every time inflate() returns for all + flush options, and so can be used to determine the amount of currently + consumed input in bits. + + The Z_TREES option behaves as Z_BLOCK does, but it also returns when the + end of each deflate block header is reached, before any actual data in that + block is decoded. This allows the caller to determine the length of the + deflate block header for later use in random access within a deflate block. + 256 is added to the value of strm->data_type when inflate() returns + immediately after reaching the end of the deflate block header. + + inflate() should normally be called until it returns Z_STREAM_END or an + error. However if all decompression is to be performed in a single step (a + single call of inflate), the parameter flush should be set to Z_FINISH. In + this case all pending input is processed and all pending output is flushed; + avail_out must be large enough to hold all of the uncompressed data for the + operation to complete. (The size of the uncompressed data may have been + saved by the compressor for this purpose.) The use of Z_FINISH is not + required to perform an inflation in one step. However it may be used to + inform inflate that a faster approach can be used for the single inflate() + call. Z_FINISH also informs inflate to not maintain a sliding window if the + stream completes, which reduces inflate's memory footprint. If the stream + does not complete, either because not all of the stream is provided or not + enough output space is provided, then a sliding window will be allocated and + inflate() can be called again to continue the operation as if Z_NO_FLUSH had + been used. + + In this implementation, inflate() always flushes as much output as + possible to the output buffer, and always uses the faster approach on the + first call. So the effects of the flush parameter in this implementation are + on the return value of inflate() as noted below, when inflate() returns early + when Z_BLOCK or Z_TREES is used, and when inflate() avoids the allocation of + memory for a sliding window when Z_FINISH is used. + + If a preset dictionary is needed after this call (see inflateSetDictionary + below), inflate sets strm->adler to the Adler-32 checksum of the dictionary + chosen by the compressor and returns Z_NEED_DICT; otherwise it sets + strm->adler to the Adler-32 checksum of all output produced so far (that is, + total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described + below. At the end of the stream, inflate() checks that its computed adler32 + checksum is equal to that saved by the compressor and returns Z_STREAM_END + only if the checksum is correct. + + inflate() can decompress and check either zlib-wrapped or gzip-wrapped + deflate data. The header type is detected automatically, if requested when + initializing with inflateInit2(). Any information contained in the gzip + header is not retained, so applications that need that information should + instead use raw inflate, see inflateInit2() below, or inflateBack() and + perform their own processing of the gzip header and trailer. When processing + gzip-wrapped deflate data, strm->adler32 is set to the CRC-32 of the output + producted so far. The CRC-32 is checked against the gzip trailer. + + inflate() returns Z_OK if some progress has been made (more input processed + or more output produced), Z_STREAM_END if the end of the compressed data has + been reached and all uncompressed output has been produced, Z_NEED_DICT if a + preset dictionary is needed at this point, Z_DATA_ERROR if the input data was + corrupted (input stream not conforming to the zlib format or incorrect check + value), Z_STREAM_ERROR if the stream structure was inconsistent (for example + next_in or next_out was Z_NULL), Z_MEM_ERROR if there was not enough memory, + Z_BUF_ERROR if no progress is possible or if there was not enough room in the + output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and + inflate() can be called again with more input and more output space to + continue decompressing. If Z_DATA_ERROR is returned, the application may + then call inflateSync() to look for a good compression block if a partial + recovery of the data is desired. +*/ + + +ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm)); +/* + All dynamically allocated data structures for this stream are freed. + This function discards any unprocessed input and does not flush any pending + output. + + inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state + was inconsistent. In the error case, msg may be set but then points to a + static string (which must not be deallocated). +*/ + + + /* Advanced functions */ + +/* + The following functions are needed only in some special applications. +*/ + +/* +ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm, + int level, + int method, + int windowBits, + int memLevel, + int strategy)); + + This is another version of deflateInit with more compression options. The + fields next_in, zalloc, zfree and opaque must be initialized before by the + caller. + + The method parameter is the compression method. It must be Z_DEFLATED in + this version of the library. + + The windowBits parameter is the base two logarithm of the window size + (the size of the history buffer). It should be in the range 8..15 for this + version of the library. Larger values of this parameter result in better + compression at the expense of memory usage. The default value is 15 if + deflateInit is used instead. + + windowBits can also be -8..-15 for raw deflate. In this case, -windowBits + determines the window size. deflate() will then generate raw deflate data + with no zlib header or trailer, and will not compute an adler32 check value. + + windowBits can also be greater than 15 for optional gzip encoding. Add + 16 to windowBits to write a simple gzip header and trailer around the + compressed data instead of a zlib wrapper. The gzip header will have no + file name, no extra data, no comment, no modification time (set to zero), no + header crc, and the operating system will be set to 255 (unknown). If a + gzip stream is being written, strm->adler is a crc32 instead of an adler32. + + The memLevel parameter specifies how much memory should be allocated + for the internal compression state. memLevel=1 uses minimum memory but is + slow and reduces compression ratio; memLevel=9 uses maximum memory for + optimal speed. The default value is 8. See zconf.h for total memory usage + as a function of windowBits and memLevel. + + The strategy parameter is used to tune the compression algorithm. Use the + value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a + filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no + string match), or Z_RLE to limit match distances to one (run-length + encoding). Filtered data consists mostly of small values with a somewhat + random distribution. In this case, the compression algorithm is tuned to + compress them better. The effect of Z_FILTERED is to force more Huffman + coding and less string matching; it is somewhat intermediate between + Z_DEFAULT_STRATEGY and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as + fast as Z_HUFFMAN_ONLY, but give better compression for PNG image data. The + strategy parameter only affects the compression ratio but not the + correctness of the compressed output even if it is not set appropriately. + Z_FIXED prevents the use of dynamic Huffman codes, allowing for a simpler + decoder for special applications. + + deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_STREAM_ERROR if any parameter is invalid (such as an invalid + method), or Z_VERSION_ERROR if the zlib library version (zlib_version) is + incompatible with the version assumed by the caller (ZLIB_VERSION). msg is + set to null if there is no error message. deflateInit2 does not perform any + compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the compression dictionary from the given byte sequence + without producing any compressed output. When using the zlib format, this + function must be called immediately after deflateInit, deflateInit2 or + deflateReset, and before any call of deflate. When doing raw deflate, this + function must be called either before any call of deflate, or immediately + after the completion of a deflate block, i.e. after all input has been + consumed and all output has been delivered when using any of the flush + options Z_BLOCK, Z_PARTIAL_FLUSH, Z_SYNC_FLUSH, or Z_FULL_FLUSH. The + compressor and decompressor must use exactly the same dictionary (see + inflateSetDictionary). + + The dictionary should consist of strings (byte sequences) that are likely + to be encountered later in the data to be compressed, with the most commonly + used strings preferably put towards the end of the dictionary. Using a + dictionary is most useful when the data to be compressed is short and can be + predicted with good accuracy; the data can then be compressed better than + with the default empty dictionary. + + Depending on the size of the compression data structures selected by + deflateInit or deflateInit2, a part of the dictionary may in effect be + discarded, for example if the dictionary is larger than the window size + provided in deflateInit or deflateInit2. Thus the strings most likely to be + useful should be put at the end of the dictionary, not at the front. In + addition, the current implementation of deflate will use at most the window + size minus 262 bytes of the provided dictionary. + + Upon return of this function, strm->adler is set to the adler32 value + of the dictionary; the decompressor may later use this value to determine + which dictionary has been used by the compressor. (The adler32 value + applies to the whole dictionary even if only a subset of the dictionary is + actually used by the compressor.) If a raw deflate was requested, then the + adler32 value is not computed and strm->adler is not set. + + deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a + parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is + inconsistent (for example if deflate has already been called for this stream + or if not at a block boundary for raw deflate). deflateSetDictionary does + not perform any compression: this will be done by deflate(). +*/ + +ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when several compression strategies will be + tried, for example when there are several ways of pre-processing the input + data with a filter. The streams that will be discarded should then be freed + by calling deflateEnd. Note that deflateCopy duplicates the internal + compression state which can be quite large, so this strategy is slow and can + consume lots of memory. + + deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being Z_NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm)); +/* + This function is equivalent to deflateEnd followed by deflateInit, + but does not free and reallocate all the internal compression state. The + stream will keep the same compression level and any other attributes that + may have been set by deflateInit2. + + deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL). +*/ + +ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm, + int level, + int strategy)); +/* + Dynamically update the compression level and compression strategy. The + interpretation of level and strategy is as in deflateInit2. This can be + used to switch between compression and straight copy of the input data, or + to switch to a different kind of input data requiring a different strategy. + If the compression level is changed, the input available so far is + compressed with the old level (and may be flushed); the new level will take + effect only at the next call of deflate(). + + Before the call of deflateParams, the stream state must be set as for + a call of deflate(), since the currently available input may have to be + compressed and flushed. In particular, strm->avail_out must be non-zero. + + deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source + stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR if + strm->avail_out was zero. +*/ + +ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm, + int good_length, + int max_lazy, + int nice_length, + int max_chain)); +/* + Fine tune deflate's internal compression parameters. This should only be + used by someone who understands the algorithm used by zlib's deflate for + searching for the best matching string, and even then only by the most + fanatic optimizer trying to squeeze out the last compressed bit for their + specific input data. Read the deflate.c source code for the meaning of the + max_lazy, good_length, nice_length, and max_chain parameters. + + deflateTune() can be called after deflateInit() or deflateInit2(), and + returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream. + */ + +ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm, + uLong sourceLen)); +/* + deflateBound() returns an upper bound on the compressed size after + deflation of sourceLen bytes. It must be called after deflateInit() or + deflateInit2(), and after deflateSetHeader(), if used. This would be used + to allocate an output buffer for deflation in a single pass, and so would be + called before deflate(). If that first deflate() call is provided the + sourceLen input bytes, an output buffer allocated to the size returned by + deflateBound(), and the flush value Z_FINISH, then deflate() is guaranteed + to return Z_STREAM_END. Note that it is possible for the compressed size to + be larger than the value returned by deflateBound() if flush options other + than Z_FINISH or Z_NO_FLUSH are used. +*/ + +ZEXTERN int ZEXPORT deflatePending OF((z_streamp strm, + unsigned *pending, + int *bits)); +/* + deflatePending() returns the number of bytes and bits of output that have + been generated, but not yet provided in the available output. The bytes not + provided would be due to the available output space having being consumed. + The number of bits of output not provided are between 0 and 7, where they + await more bits to join them in order to fill out a full byte. If pending + or bits are Z_NULL, then those values are not set. + + deflatePending returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. + */ + +ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + deflatePrime() inserts bits in the deflate output stream. The intent + is that this function is used to start off the deflate output with the bits + leftover from a previous deflate stream when appending to it. As such, this + function can only be used for raw deflate, and must be used before the first + deflate() call after a deflateInit2() or deflateReset(). bits must be less + than or equal to 16, and that many of the least significant bits of value + will be inserted in the output. + + deflatePrime returns Z_OK if success, Z_BUF_ERROR if there was not enough + room in the internal buffer to insert the bits, or Z_STREAM_ERROR if the + source stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm, + gz_headerp head)); +/* + deflateSetHeader() provides gzip header information for when a gzip + stream is requested by deflateInit2(). deflateSetHeader() may be called + after deflateInit2() or deflateReset() and before the first call of + deflate(). The text, time, os, extra field, name, and comment information + in the provided gz_header structure are written to the gzip header (xflag is + ignored -- the extra flags are set according to the compression level). The + caller must assure that, if not Z_NULL, name and comment are terminated with + a zero byte, and that if extra is not Z_NULL, that extra_len bytes are + available there. If hcrc is true, a gzip header crc is included. Note that + the current versions of the command-line version of gzip (up through version + 1.3.x) do not support header crc's, and will report that it is a "multi-part + gzip file" and give up. + + If deflateSetHeader is not used, the default gzip header has text false, + the time set to zero, and os set to 255, with no extra, name, or comment + fields. The gzip header is returned to the default state by deflateReset(). + + deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm, + int windowBits)); + + This is another version of inflateInit with an extra parameter. The + fields next_in, avail_in, zalloc, zfree and opaque must be initialized + before by the caller. + + The windowBits parameter is the base two logarithm of the maximum window + size (the size of the history buffer). It should be in the range 8..15 for + this version of the library. The default value is 15 if inflateInit is used + instead. windowBits must be greater than or equal to the windowBits value + provided to deflateInit2() while compressing, or it must be equal to 15 if + deflateInit2() was not used. If a compressed stream with a larger window + size is given as input, inflate() will return with the error code + Z_DATA_ERROR instead of trying to allocate a larger window. + + windowBits can also be zero to request that inflate use the window size in + the zlib header of the compressed stream. + + windowBits can also be -8..-15 for raw inflate. In this case, -windowBits + determines the window size. inflate() will then process raw deflate data, + not looking for a zlib or gzip header, not generating a check value, and not + looking for any check values for comparison at the end of the stream. This + is for use with other formats that use the deflate compressed data format + such as zip. Those formats provide their own check values. If a custom + format is developed using the raw deflate format for compressed data, it is + recommended that a check value such as an adler32 or a crc32 be applied to + the uncompressed data as is done in the zlib, gzip, and zip formats. For + most applications, the zlib format should be used as is. Note that comments + above on the use in deflateInit2() applies to the magnitude of windowBits. + + windowBits can also be greater than 15 for optional gzip decoding. Add + 32 to windowBits to enable zlib and gzip decoding with automatic header + detection, or add 16 to decode only the gzip format (the zlib format will + return a Z_DATA_ERROR). If a gzip stream is being decoded, strm->adler is a + crc32 instead of an adler32. + + inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_VERSION_ERROR if the zlib library version is incompatible with the + version assumed by the caller, or Z_STREAM_ERROR if the parameters are + invalid, such as a null pointer to the structure. msg is set to null if + there is no error message. inflateInit2 does not perform any decompression + apart from possibly reading the zlib header if present: actual decompression + will be done by inflate(). (So next_in and avail_in may be modified, but + next_out and avail_out are unused and unchanged.) The current implementation + of inflateInit2() does not process any header information -- that is + deferred until inflate() is called. +*/ + +ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm, + const Bytef *dictionary, + uInt dictLength)); +/* + Initializes the decompression dictionary from the given uncompressed byte + sequence. This function must be called immediately after a call of inflate, + if that call returned Z_NEED_DICT. The dictionary chosen by the compressor + can be determined from the adler32 value returned by that call of inflate. + The compressor and decompressor must use exactly the same dictionary (see + deflateSetDictionary). For raw inflate, this function can be called at any + time to set the dictionary. If the provided dictionary is smaller than the + window and there is already data in the window, then the provided dictionary + will amend what's there. The application must insure that the dictionary + that was used for compression is provided. + + inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a + parameter is invalid (e.g. dictionary being Z_NULL) or the stream state is + inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the + expected one (incorrect adler32 value). inflateSetDictionary does not + perform any decompression: this will be done by subsequent calls of + inflate(). +*/ + +ZEXTERN int ZEXPORT inflateGetDictionary OF((z_streamp strm, + Bytef *dictionary, + uInt *dictLength)); +/* + Returns the sliding dictionary being maintained by inflate. dictLength is + set to the number of bytes in the dictionary, and that many bytes are copied + to dictionary. dictionary must have enough space, where 32768 bytes is + always enough. If inflateGetDictionary() is called with dictionary equal to + Z_NULL, then only the dictionary length is returned, and nothing is copied. + Similary, if dictLength is Z_NULL, then it is not set. + + inflateGetDictionary returns Z_OK on success, or Z_STREAM_ERROR if the + stream state is inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm)); +/* + Skips invalid compressed data until a possible full flush point (see above + for the description of deflate with Z_FULL_FLUSH) can be found, or until all + available input is skipped. No output is provided. + + inflateSync searches for a 00 00 FF FF pattern in the compressed data. + All full flush points have this pattern, but not all occurrences of this + pattern are full flush points. + + inflateSync returns Z_OK if a possible full flush point has been found, + Z_BUF_ERROR if no more input was provided, Z_DATA_ERROR if no flush point + has been found, or Z_STREAM_ERROR if the stream structure was inconsistent. + In the success case, the application may save the current current value of + total_in which indicates where valid compressed data was found. In the + error case, the application may repeatedly call inflateSync, providing more + input each time, until success or end of the input data. +*/ + +ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest, + z_streamp source)); +/* + Sets the destination stream as a complete copy of the source stream. + + This function can be useful when randomly accessing a large stream. The + first pass through the stream can periodically record the inflate state, + allowing restarting inflate at those points when randomly accessing the + stream. + + inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_STREAM_ERROR if the source stream state was inconsistent + (such as zalloc being Z_NULL). msg is left unchanged in both source and + destination. +*/ + +ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm)); +/* + This function is equivalent to inflateEnd followed by inflateInit, + but does not free and reallocate all the internal decompression state. The + stream will keep attributes that may have been set by inflateInit2. + + inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL). +*/ + +ZEXTERN int ZEXPORT inflateReset2 OF((z_streamp strm, + int windowBits)); +/* + This function is the same as inflateReset, but it also permits changing + the wrap and window size requests. The windowBits parameter is interpreted + the same as it is for inflateInit2. + + inflateReset2 returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent (such as zalloc or state being Z_NULL), or if + the windowBits parameter is invalid. +*/ + +ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm, + int bits, + int value)); +/* + This function inserts bits in the inflate input stream. The intent is + that this function is used to start inflating at a bit position in the + middle of a byte. The provided bits will be used before any bytes are used + from next_in. This function should only be used with raw inflate, and + should be used before the first inflate() call after inflateInit2() or + inflateReset(). bits must be less than or equal to 16, and that many of the + least significant bits of value will be inserted in the input. + + If bits is negative, then the input stream bit buffer is emptied. Then + inflatePrime() can be called again to put bits in the buffer. This is used + to clear out bits leftover after feeding inflate a block description prior + to feeding inflate codes. + + inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +ZEXTERN long ZEXPORT inflateMark OF((z_streamp strm)); +/* + This function returns two values, one in the lower 16 bits of the return + value, and the other in the remaining upper bits, obtained by shifting the + return value down 16 bits. If the upper value is -1 and the lower value is + zero, then inflate() is currently decoding information outside of a block. + If the upper value is -1 and the lower value is non-zero, then inflate is in + the middle of a stored block, with the lower value equaling the number of + bytes from the input remaining to copy. If the upper value is not -1, then + it is the number of bits back from the current bit position in the input of + the code (literal or length/distance pair) currently being processed. In + that case the lower value is the number of bytes already emitted for that + code. + + A code is being processed if inflate is waiting for more input to complete + decoding of the code, or if it has completed decoding but is waiting for + more output space to write the literal or match data. + + inflateMark() is used to mark locations in the input data for random + access, which may be at bit positions, and to note those cases where the + output of a code may span boundaries of random access blocks. The current + location in the input stream can be determined from avail_in and data_type + as noted in the description for the Z_BLOCK flush parameter for inflate. + + inflateMark returns the value noted above or -1 << 16 if the provided + source stream state was inconsistent. +*/ + +ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm, + gz_headerp head)); +/* + inflateGetHeader() requests that gzip header information be stored in the + provided gz_header structure. inflateGetHeader() may be called after + inflateInit2() or inflateReset(), and before the first call of inflate(). + As inflate() processes the gzip stream, head->done is zero until the header + is completed, at which time head->done is set to one. If a zlib stream is + being decoded, then head->done is set to -1 to indicate that there will be + no gzip header information forthcoming. Note that Z_BLOCK or Z_TREES can be + used to force inflate() to return immediately after header processing is + complete and before any actual data is decompressed. + + The text, time, xflags, and os fields are filled in with the gzip header + contents. hcrc is set to true if there is a header CRC. (The header CRC + was valid if done is set to one.) If extra is not Z_NULL, then extra_max + contains the maximum number of bytes to write to extra. Once done is true, + extra_len contains the actual extra field length, and extra contains the + extra field, or that field truncated if extra_max is less than extra_len. + If name is not Z_NULL, then up to name_max characters are written there, + terminated with a zero unless the length is greater than name_max. If + comment is not Z_NULL, then up to comm_max characters are written there, + terminated with a zero unless the length is greater than comm_max. When any + of extra, name, or comment are not Z_NULL and the respective field is not + present in the header, then that field is set to Z_NULL to signal its + absence. This allows the use of deflateSetHeader() with the returned + structure to duplicate the header. However if those fields are set to + allocated memory, then the application will need to save those pointers + elsewhere so that they can be eventually freed. + + If inflateGetHeader is not used, then the header information is simply + discarded. The header is always checked for validity, including the header + CRC if present. inflateReset() will reset the process to discard the header + information. The application would need to call inflateGetHeader() again to + retrieve the header from the next gzip stream. + + inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source + stream state was inconsistent. +*/ + +/* +ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits, + unsigned char FAR *window)); + + Initialize the internal stream state for decompression using inflateBack() + calls. The fields zalloc, zfree and opaque in strm must be initialized + before the call. If zalloc and zfree are Z_NULL, then the default library- + derived memory allocation routines are used. windowBits is the base two + logarithm of the window size, in the range 8..15. window is a caller + supplied buffer of that size. Except for special applications where it is + assured that deflate was used with small window sizes, windowBits must be 15 + and a 32K byte window must be supplied to be able to decompress general + deflate streams. + + See inflateBack() for the usage of these routines. + + inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of + the parameters are invalid, Z_MEM_ERROR if the internal state could not be + allocated, or Z_VERSION_ERROR if the version of the library does not match + the version of the header file. +*/ + +typedef unsigned (*in_func) OF((void FAR *, + z_const unsigned char FAR * FAR *)); +typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned)); + +ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm, + in_func in, void FAR *in_desc, + out_func out, void FAR *out_desc)); +/* + inflateBack() does a raw inflate with a single call using a call-back + interface for input and output. This is potentially more efficient than + inflate() for file i/o applications, in that it avoids copying between the + output and the sliding window by simply making the window itself the output + buffer. inflate() can be faster on modern CPUs when used with large + buffers. inflateBack() trusts the application to not change the output + buffer passed by the output function, at least until inflateBack() returns. + + inflateBackInit() must be called first to allocate the internal state + and to initialize the state with the user-provided window buffer. + inflateBack() may then be used multiple times to inflate a complete, raw + deflate stream with each call. inflateBackEnd() is then called to free the + allocated state. + + A raw deflate stream is one with no zlib or gzip header or trailer. + This routine would normally be used in a utility that reads zip or gzip + files and writes out uncompressed files. The utility would decode the + header and process the trailer on its own, hence this routine expects only + the raw deflate stream to decompress. This is different from the normal + behavior of inflate(), which expects either a zlib or gzip header and + trailer around the deflate stream. + + inflateBack() uses two subroutines supplied by the caller that are then + called by inflateBack() for input and output. inflateBack() calls those + routines until it reads a complete deflate stream and writes out all of the + uncompressed data, or until it encounters an error. The function's + parameters and return types are defined above in the in_func and out_func + typedefs. inflateBack() will call in(in_desc, &buf) which should return the + number of bytes of provided input, and a pointer to that input in buf. If + there is no input available, in() must return zero--buf is ignored in that + case--and inflateBack() will return a buffer error. inflateBack() will call + out(out_desc, buf, len) to write the uncompressed data buf[0..len-1]. out() + should return zero on success, or non-zero on failure. If out() returns + non-zero, inflateBack() will return with an error. Neither in() nor out() + are permitted to change the contents of the window provided to + inflateBackInit(), which is also the buffer that out() uses to write from. + The length written by out() will be at most the window size. Any non-zero + amount of input may be provided by in(). + + For convenience, inflateBack() can be provided input on the first call by + setting strm->next_in and strm->avail_in. If that input is exhausted, then + in() will be called. Therefore strm->next_in must be initialized before + calling inflateBack(). If strm->next_in is Z_NULL, then in() will be called + immediately for input. If strm->next_in is not Z_NULL, then strm->avail_in + must also be initialized, and then if strm->avail_in is not zero, input will + initially be taken from strm->next_in[0 .. strm->avail_in - 1]. + + The in_desc and out_desc parameters of inflateBack() is passed as the + first parameter of in() and out() respectively when they are called. These + descriptors can be optionally used to pass any information that the caller- + supplied in() and out() functions need to do their job. + + On return, inflateBack() will set strm->next_in and strm->avail_in to + pass back any unused input that was provided by the last in() call. The + return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR + if in() or out() returned an error, Z_DATA_ERROR if there was a format error + in the deflate stream (in which case strm->msg is set to indicate the nature + of the error), or Z_STREAM_ERROR if the stream was not properly initialized. + In the case of Z_BUF_ERROR, an input or output error can be distinguished + using strm->next_in which will be Z_NULL only if in() returned an error. If + strm->next_in is not Z_NULL, then the Z_BUF_ERROR was due to out() returning + non-zero. (in() will always be called before out(), so strm->next_in is + assured to be defined if out() returns non-zero.) Note that inflateBack() + cannot return Z_OK. +*/ + +ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm)); +/* + All memory allocated by inflateBackInit() is freed. + + inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream + state was inconsistent. +*/ + +ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void)); +/* Return flags indicating compile-time options. + + Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other: + 1.0: size of uInt + 3.2: size of uLong + 5.4: size of voidpf (pointer) + 7.6: size of z_off_t + + Compiler, assembler, and debug options: + 8: DEBUG + 9: ASMV or ASMINF -- use ASM code + 10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention + 11: 0 (reserved) + + One-time table building (smaller code, but not thread-safe if true): + 12: BUILDFIXED -- build static block decoding tables when needed + 13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed + 14,15: 0 (reserved) + + Library content (indicates missing functionality): + 16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking + deflate code when not needed) + 17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect + and decode gzip streams (to avoid linking crc code) + 18-19: 0 (reserved) + + Operation variations (changes in library functionality): + 20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate + 21: FASTEST -- deflate algorithm with only one, lowest compression level + 22,23: 0 (reserved) + + The sprintf variant used by gzprintf (zero is best): + 24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format + 25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure! + 26: 0 = returns value, 1 = void -- 1 means inferred string length returned + + Remainder: + 27-31: 0 (reserved) + */ + +#ifndef Z_SOLO + + /* utility functions */ + +/* + The following utility functions are implemented on top of the basic + stream-oriented functions. To simplify the interface, some default options + are assumed (compression level and memory usage, standard memory allocation + functions). The source code of these utility functions can be modified if + you need special options. +*/ + +ZEXTERN int ZEXPORT compress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Compresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total size + of the destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed buffer. + + compress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer. +*/ + +ZEXTERN int ZEXPORT compress2 OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen, + int level)); +/* + Compresses the source buffer into the destination buffer. The level + parameter has the same meaning as in deflateInit. sourceLen is the byte + length of the source buffer. Upon entry, destLen is the total size of the + destination buffer, which must be at least the value returned by + compressBound(sourceLen). Upon exit, destLen is the actual size of the + compressed buffer. + + compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough + memory, Z_BUF_ERROR if there was not enough room in the output buffer, + Z_STREAM_ERROR if the level parameter is invalid. +*/ + +ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen)); +/* + compressBound() returns an upper bound on the compressed size after + compress() or compress2() on sourceLen bytes. It would be used before a + compress() or compress2() call to allocate the destination buffer. +*/ + +ZEXTERN int ZEXPORT uncompress OF((Bytef *dest, uLongf *destLen, + const Bytef *source, uLong sourceLen)); +/* + Decompresses the source buffer into the destination buffer. sourceLen is + the byte length of the source buffer. Upon entry, destLen is the total size + of the destination buffer, which must be large enough to hold the entire + uncompressed data. (The size of the uncompressed data must have been saved + previously by the compressor and transmitted to the decompressor by some + mechanism outside the scope of this compression library.) Upon exit, destLen + is the actual size of the uncompressed buffer. + + uncompress returns Z_OK if success, Z_MEM_ERROR if there was not + enough memory, Z_BUF_ERROR if there was not enough room in the output + buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete. In + the case where there is not enough room, uncompress() will fill the output + buffer with the uncompressed data up to that point. +*/ + + /* gzip file access functions */ + +/* + This library supports reading and writing files in gzip (.gz) format with + an interface similar to that of stdio, using the functions that start with + "gz". The gzip format is different from the zlib format. gzip is a gzip + wrapper, documented in RFC 1952, wrapped around a deflate stream. +*/ + +typedef struct gzFile_s *gzFile; /* semi-opaque gzip file descriptor */ + +/* +ZEXTERN gzFile ZEXPORT gzopen OF((const char *path, const char *mode)); + + Opens a gzip (.gz) file for reading or writing. The mode parameter is as + in fopen ("rb" or "wb") but can also include a compression level ("wb9") or + a strategy: 'f' for filtered data as in "wb6f", 'h' for Huffman-only + compression as in "wb1h", 'R' for run-length encoding as in "wb1R", or 'F' + for fixed code compression as in "wb9F". (See the description of + deflateInit2 for more information about the strategy parameter.) 'T' will + request transparent writing or appending with no compression and not using + the gzip format. + + "a" can be used instead of "w" to request that the gzip stream that will + be written be appended to the file. "+" will result in an error, since + reading and writing to the same gzip file is not supported. The addition of + "x" when writing will create the file exclusively, which fails if the file + already exists. On systems that support it, the addition of "e" when + reading or writing will set the flag to close the file on an execve() call. + + These functions, as well as gzip, will read and decode a sequence of gzip + streams in a file. The append function of gzopen() can be used to create + such a file. (Also see gzflush() for another way to do this.) When + appending, gzopen does not test whether the file begins with a gzip stream, + nor does it look for the end of the gzip streams to begin appending. gzopen + will simply append a gzip stream to the existing file. + + gzopen can be used to read a file which is not in gzip format; in this + case gzread will directly read from the file without decompression. When + reading, this will be detected automatically by looking for the magic two- + byte gzip header. + + gzopen returns NULL if the file could not be opened, if there was + insufficient memory to allocate the gzFile state, or if an invalid mode was + specified (an 'r', 'w', or 'a' was not provided, or '+' was provided). + errno can be checked to determine if the reason gzopen failed was that the + file could not be opened. +*/ + +ZEXTERN gzFile ZEXPORT gzdopen OF((int fd, const char *mode)); +/* + gzdopen associates a gzFile with the file descriptor fd. File descriptors + are obtained from calls like open, dup, creat, pipe or fileno (if the file + has been previously opened with fopen). The mode parameter is as in gzopen. + + The next call of gzclose on the returned gzFile will also close the file + descriptor fd, just like fclose(fdopen(fd, mode)) closes the file descriptor + fd. If you want to keep fd open, use fd = dup(fd_keep); gz = gzdopen(fd, + mode);. The duplicated descriptor should be saved to avoid a leak, since + gzdopen does not close fd if it fails. If you are using fileno() to get the + file descriptor from a FILE *, then you will have to use dup() to avoid + double-close()ing the file descriptor. Both gzclose() and fclose() will + close the associated file descriptor, so they need to have different file + descriptors. + + gzdopen returns NULL if there was insufficient memory to allocate the + gzFile state, if an invalid mode was specified (an 'r', 'w', or 'a' was not + provided, or '+' was provided), or if fd is -1. The file descriptor is not + used until the next gz* read, write, seek, or close operation, so gzdopen + will not detect if fd is invalid (unless fd is -1). +*/ + +ZEXTERN int ZEXPORT gzbuffer OF((gzFile file, unsigned size)); +/* + Set the internal buffer size used by this library's functions. The + default buffer size is 8192 bytes. This function must be called after + gzopen() or gzdopen(), and before any other calls that read or write the + file. The buffer memory allocation is always deferred to the first read or + write. Two buffers are allocated, either both of the specified size when + writing, or one of the specified size and the other twice that size when + reading. A larger buffer size of, for example, 64K or 128K bytes will + noticeably increase the speed of decompression (reading). + + The new buffer size also affects the maximum length for gzprintf(). + + gzbuffer() returns 0 on success, or -1 on failure, such as being called + too late. +*/ + +ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy)); +/* + Dynamically update the compression level or strategy. See the description + of deflateInit2 for the meaning of these parameters. + + gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not + opened for writing. +*/ + +ZEXTERN int ZEXPORT gzread OF((gzFile file, voidp buf, unsigned len)); +/* + Reads the given number of uncompressed bytes from the compressed file. If + the input file is not in gzip format, gzread copies the given number of + bytes into the buffer directly from the file. + + After reaching the end of a gzip stream in the input, gzread will continue + to read, looking for another gzip stream. Any number of gzip streams may be + concatenated in the input file, and will all be decompressed by gzread(). + If something other than a gzip stream is encountered after a gzip stream, + that remaining trailing garbage is ignored (and no error is returned). + + gzread can be used to read a gzip file that is being concurrently written. + Upon reaching the end of the input, gzread will return with the available + data. If the error code returned by gzerror is Z_OK or Z_BUF_ERROR, then + gzclearerr can be used to clear the end of file indicator in order to permit + gzread to be tried again. Z_OK indicates that a gzip stream was completed + on the last gzread. Z_BUF_ERROR indicates that the input file ended in the + middle of a gzip stream. Note that gzread does not return -1 in the event + of an incomplete gzip stream. This error is deferred until gzclose(), which + will return Z_BUF_ERROR if the last gzread ended in the middle of a gzip + stream. Alternatively, gzerror can be used before gzclose to detect this + case. + + gzread returns the number of uncompressed bytes actually read, less than + len for end of file, or -1 for error. +*/ + +ZEXTERN int ZEXPORT gzwrite OF((gzFile file, + voidpc buf, unsigned len)); +/* + Writes the given number of uncompressed bytes into the compressed file. + gzwrite returns the number of uncompressed bytes written or 0 in case of + error. +*/ + +ZEXTERN int ZEXPORTVA gzprintf Z_ARG((gzFile file, const char *format, ...)); +/* + Converts, formats, and writes the arguments to the compressed file under + control of the format string, as in fprintf. gzprintf returns the number of + uncompressed bytes actually written, or 0 in case of error. The number of + uncompressed bytes written is limited to 8191, or one less than the buffer + size given to gzbuffer(). The caller should assure that this limit is not + exceeded. If it is exceeded, then gzprintf() will return an error (0) with + nothing written. In this case, there may also be a buffer overflow with + unpredictable consequences, which is possible only if zlib was compiled with + the insecure functions sprintf() or vsprintf() because the secure snprintf() + or vsnprintf() functions were not available. This can be determined using + zlibCompileFlags(). +*/ + +ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s)); +/* + Writes the given null-terminated string to the compressed file, excluding + the terminating null character. + + gzputs returns the number of characters written, or -1 in case of error. +*/ + +ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len)); +/* + Reads bytes from the compressed file until len-1 characters are read, or a + newline character is read and transferred to buf, or an end-of-file + condition is encountered. If any characters are read or if len == 1, the + string is terminated with a null character. If no characters are read due + to an end-of-file or len < 1, then the buffer is left untouched. + + gzgets returns buf which is a null-terminated string, or it returns NULL + for end-of-file or in case of error. If there was an error, the contents at + buf are indeterminate. +*/ + +ZEXTERN int ZEXPORT gzputc OF((gzFile file, int c)); +/* + Writes c, converted to an unsigned char, into the compressed file. gzputc + returns the value that was written, or -1 in case of error. +*/ + +ZEXTERN int ZEXPORT gzgetc OF((gzFile file)); +/* + Reads one byte from the compressed file. gzgetc returns this byte or -1 + in case of end of file or error. This is implemented as a macro for speed. + As such, it does not do all of the checking the other functions do. I.e. + it does not check to see if file is NULL, nor whether the structure file + points to has been clobbered or not. +*/ + +ZEXTERN int ZEXPORT gzungetc OF((int c, gzFile file)); +/* + Push one character back onto the stream to be read as the first character + on the next read. At least one character of push-back is allowed. + gzungetc() returns the character pushed, or -1 on failure. gzungetc() will + fail if c is -1, and may fail if a character has been pushed but not read + yet. If gzungetc is used immediately after gzopen or gzdopen, at least the + output buffer size of pushed characters is allowed. (See gzbuffer above.) + The pushed character will be discarded if the stream is repositioned with + gzseek() or gzrewind(). +*/ + +ZEXTERN int ZEXPORT gzflush OF((gzFile file, int flush)); +/* + Flushes all pending output into the compressed file. The parameter flush + is as in the deflate() function. The return value is the zlib error number + (see function gzerror below). gzflush is only permitted when writing. + + If the flush parameter is Z_FINISH, the remaining data is written and the + gzip stream is completed in the output. If gzwrite() is called again, a new + gzip stream will be started in the output. gzread() is able to read such + concatented gzip streams. + + gzflush should be called only when strictly necessary because it will + degrade compression if called too often. +*/ + +/* +ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile file, + z_off_t offset, int whence)); + + Sets the starting position for the next gzread or gzwrite on the given + compressed file. The offset represents a number of bytes in the + uncompressed data stream. The whence parameter is defined as in lseek(2); + the value SEEK_END is not supported. + + If the file is opened for reading, this function is emulated but can be + extremely slow. If the file is opened for writing, only forward seeks are + supported; gzseek then compresses a sequence of zeroes up to the new + starting position. + + gzseek returns the resulting offset location as measured in bytes from + the beginning of the uncompressed stream, or -1 in case of error, in + particular if the file is opened for writing and the new starting position + would be before the current position. +*/ + +ZEXTERN int ZEXPORT gzrewind OF((gzFile file)); +/* + Rewinds the given file. This function is supported only for reading. + + gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET) +*/ + +/* +ZEXTERN z_off_t ZEXPORT gztell OF((gzFile file)); + + Returns the starting position for the next gzread or gzwrite on the given + compressed file. This position represents a number of bytes in the + uncompressed data stream, and is zero when starting, even if appending or + reading a gzip stream from the middle of a file using gzdopen(). + + gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR) +*/ + +/* +ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile file)); + + Returns the current offset in the file being read or written. This offset + includes the count of bytes that precede the gzip stream, for example when + appending or when using gzdopen() for reading. When reading, the offset + does not include as yet unused buffered input. This information can be used + for a progress indicator. On error, gzoffset() returns -1. +*/ + +ZEXTERN int ZEXPORT gzeof OF((gzFile file)); +/* + Returns true (1) if the end-of-file indicator has been set while reading, + false (0) otherwise. Note that the end-of-file indicator is set only if the + read tried to go past the end of the input, but came up short. Therefore, + just like feof(), gzeof() may return false even if there is no more data to + read, in the event that the last read request was for the exact number of + bytes remaining in the input file. This will happen if the input file size + is an exact multiple of the buffer size. + + If gzeof() returns true, then the read functions will return no more data, + unless the end-of-file indicator is reset by gzclearerr() and the input file + has grown since the previous end of file was detected. +*/ + +ZEXTERN int ZEXPORT gzdirect OF((gzFile file)); +/* + Returns true (1) if file is being copied directly while reading, or false + (0) if file is a gzip stream being decompressed. + + If the input file is empty, gzdirect() will return true, since the input + does not contain a gzip stream. + + If gzdirect() is used immediately after gzopen() or gzdopen() it will + cause buffers to be allocated to allow reading the file to determine if it + is a gzip file. Therefore if gzbuffer() is used, it should be called before + gzdirect(). + + When writing, gzdirect() returns true (1) if transparent writing was + requested ("wT" for the gzopen() mode), or false (0) otherwise. (Note: + gzdirect() is not needed when writing. Transparent writing must be + explicitly requested, so the application already knows the answer. When + linking statically, using gzdirect() will include all of the zlib code for + gzip file reading and decompression, which may not be desired.) +*/ + +ZEXTERN int ZEXPORT gzclose OF((gzFile file)); +/* + Flushes all pending output if necessary, closes the compressed file and + deallocates the (de)compression state. Note that once file is closed, you + cannot call gzerror with file, since its structures have been deallocated. + gzclose must not be called more than once on the same file, just as free + must not be called more than once on the same allocation. + + gzclose will return Z_STREAM_ERROR if file is not valid, Z_ERRNO on a + file operation error, Z_MEM_ERROR if out of memory, Z_BUF_ERROR if the + last read ended in the middle of a gzip stream, or Z_OK on success. +*/ + +ZEXTERN int ZEXPORT gzclose_r OF((gzFile file)); +ZEXTERN int ZEXPORT gzclose_w OF((gzFile file)); +/* + Same as gzclose(), but gzclose_r() is only for use when reading, and + gzclose_w() is only for use when writing or appending. The advantage to + using these instead of gzclose() is that they avoid linking in zlib + compression or decompression code that is not used when only reading or only + writing respectively. If gzclose() is used, then both compression and + decompression code will be included the application when linking to a static + zlib library. +*/ + +ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum)); +/* + Returns the error message for the last error which occurred on the given + compressed file. errnum is set to zlib error number. If an error occurred + in the file system and not in the compression library, errnum is set to + Z_ERRNO and the application may consult errno to get the exact error code. + + The application must not modify the returned string. Future calls to + this function may invalidate the previously returned string. If file is + closed, then the string previously returned by gzerror will no longer be + available. + + gzerror() should be used to distinguish errors from end-of-file for those + functions above that do not distinguish those cases in their return values. +*/ + +ZEXTERN void ZEXPORT gzclearerr OF((gzFile file)); +/* + Clears the error and end-of-file flags for file. This is analogous to the + clearerr() function in stdio. This is useful for continuing to read a gzip + file that is being written concurrently. +*/ + +#endif /* !Z_SOLO */ + + /* checksum functions */ + +/* + These functions are not related to compression but are exported + anyway because they might be useful in applications using the compression + library. +*/ + +ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len)); +/* + Update a running Adler-32 checksum with the bytes buf[0..len-1] and + return the updated checksum. If buf is Z_NULL, this function returns the + required initial value for the checksum. + + An Adler-32 checksum is almost as reliable as a CRC32 but can be computed + much faster. + + Usage example: + + uLong adler = adler32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + adler = adler32(adler, buffer, length); + } + if (adler != original_adler) error(); +*/ + +/* +ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2, + z_off_t len2)); + + Combine two Adler-32 checksums into one. For two sequences of bytes, seq1 + and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for + each, adler1 and adler2. adler32_combine() returns the Adler-32 checksum of + seq1 and seq2 concatenated, requiring only adler1, adler2, and len2. Note + that the z_off_t type (like off_t) is a signed integer. If len2 is + negative, the result has no meaning or utility. +*/ + +ZEXTERN uLong ZEXPORT crc32 OF((uLong crc, const Bytef *buf, uInt len)); +/* + Update a running CRC-32 with the bytes buf[0..len-1] and return the + updated CRC-32. If buf is Z_NULL, this function returns the required + initial value for the crc. Pre- and post-conditioning (one's complement) is + performed within this function so it shouldn't be done by the application. + + Usage example: + + uLong crc = crc32(0L, Z_NULL, 0); + + while (read_buffer(buffer, length) != EOF) { + crc = crc32(crc, buffer, length); + } + if (crc != original_crc) error(); +*/ + +/* +ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2)); + + Combine two CRC-32 check values into one. For two sequences of bytes, + seq1 and seq2 with lengths len1 and len2, CRC-32 check values were + calculated for each, crc1 and crc2. crc32_combine() returns the CRC-32 + check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and + len2. +*/ + + + /* various hacks, don't look :) */ + +/* deflateInit and inflateInit are macros to allow checking the zlib version + * and the compiler's view of z_stream: + */ +ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int level, int method, + int windowBits, int memLevel, + int strategy, const char *version, + int stream_size)); +ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int windowBits, + const char *version, int stream_size)); +ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits, + unsigned char FAR *window, + const char *version, + int stream_size)); +#define deflateInit(strm, level) \ + deflateInit_((strm), (level), ZLIB_VERSION, (int)sizeof(z_stream)) +#define inflateInit(strm) \ + inflateInit_((strm), ZLIB_VERSION, (int)sizeof(z_stream)) +#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \ + deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\ + (strategy), ZLIB_VERSION, (int)sizeof(z_stream)) +#define inflateInit2(strm, windowBits) \ + inflateInit2_((strm), (windowBits), ZLIB_VERSION, \ + (int)sizeof(z_stream)) +#define inflateBackInit(strm, windowBits, window) \ + inflateBackInit_((strm), (windowBits), (window), \ + ZLIB_VERSION, (int)sizeof(z_stream)) + +#ifndef Z_SOLO + +/* gzgetc() macro and its supporting function and exposed data structure. Note + * that the real internal state is much larger than the exposed structure. + * This abbreviated structure exposes just enough for the gzgetc() macro. The + * user should not mess with these exposed elements, since their names or + * behavior could change in the future, perhaps even capriciously. They can + * only be used by the gzgetc() macro. You have been warned. + */ +struct gzFile_s { + unsigned have; + unsigned char *next; + z_off64_t pos; +}; +ZEXTERN int ZEXPORT gzgetc_ OF((gzFile file)); /* backward compatibility */ +#ifdef Z_PREFIX_SET +# undef z_gzgetc +# define z_gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : gzgetc(g)) +#else +# define gzgetc(g) \ + ((g)->have ? ((g)->have--, (g)->pos++, *((g)->next)++) : gzgetc(g)) +#endif + +/* provide 64-bit offset functions if _LARGEFILE64_SOURCE defined, and/or + * change the regular functions to 64 bits if _FILE_OFFSET_BITS is 64 (if + * both are true, the application gets the *64 functions, and the regular + * functions are changed to 64 bits) -- in case these are set on systems + * without large file support, _LFS64_LARGEFILE must also be true + */ +#ifdef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off64_t ZEXPORT gzseek64 OF((gzFile, z_off64_t, int)); + ZEXTERN z_off64_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off64_t ZEXPORT gzoffset64 OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off64_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off64_t)); +#endif + +#if !defined(ZLIB_INTERNAL) && defined(Z_WANT64) +# ifdef Z_PREFIX_SET +# define z_gzopen z_gzopen64 +# define z_gzseek z_gzseek64 +# define z_gztell z_gztell64 +# define z_gzoffset z_gzoffset64 +# define z_adler32_combine z_adler32_combine64 +# define z_crc32_combine z_crc32_combine64 +# else +# define gzopen gzopen64 +# define gzseek gzseek64 +# define gztell gztell64 +# define gzoffset gzoffset64 +# define adler32_combine adler32_combine64 +# define crc32_combine crc32_combine64 +# endif +# ifndef Z_LARGE64 + ZEXTERN gzFile ZEXPORT gzopen64 OF((const char *, const char *)); + ZEXTERN z_off_t ZEXPORT gzseek64 OF((gzFile, z_off_t, int)); + ZEXTERN z_off_t ZEXPORT gztell64 OF((gzFile)); + ZEXTERN z_off_t ZEXPORT gzoffset64 OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); +# endif +#else + ZEXTERN gzFile ZEXPORT gzopen OF((const char *, const char *)); + ZEXTERN z_off_t ZEXPORT gzseek OF((gzFile, z_off_t, int)); + ZEXTERN z_off_t ZEXPORT gztell OF((gzFile)); + ZEXTERN z_off_t ZEXPORT gzoffset OF((gzFile)); + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); +#endif + +#else /* Z_SOLO */ + + ZEXTERN uLong ZEXPORT adler32_combine OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine OF((uLong, uLong, z_off_t)); + +#endif /* !Z_SOLO */ + +/* hack for buggy compilers */ +#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL) + struct internal_state {int dummy;}; +#endif + +/* undocumented functions */ +ZEXTERN const char * ZEXPORT zError OF((int)); +ZEXTERN int ZEXPORT inflateSyncPoint OF((z_streamp)); +ZEXTERN const z_crc_t FAR * ZEXPORT get_crc_table OF((void)); +ZEXTERN int ZEXPORT inflateUndermine OF((z_streamp, int)); +ZEXTERN int ZEXPORT inflateResetKeep OF((z_streamp)); +ZEXTERN int ZEXPORT deflateResetKeep OF((z_streamp)); +#if defined(_WIN32) && !defined(Z_SOLO) +ZEXTERN gzFile ZEXPORT gzopen_w OF((const wchar_t *path, + const char *mode)); +#endif +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifndef Z_SOLO +ZEXTERN int ZEXPORTVA gzvprintf Z_ARG((gzFile file, + const char *format, + va_list va)); +# endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* ZLIB_H */ diff --git a/fdbrpc/zlib/zutil.c b/fdbrpc/zlib/zutil.c new file mode 100644 index 0000000000..7d2bf8052b --- /dev/null +++ b/fdbrpc/zlib/zutil.c @@ -0,0 +1,323 @@ +/* zutil.c -- target dependent utility functions for the compression library + * Copyright (C) 1995-2005, 2010, 2011, 2012 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* @(#) $Id$ */ + +#include "zutil.h" +#ifndef Z_SOLO +# include "gzguts.h" +#endif + +#ifndef NO_DUMMY_DECL +struct internal_state {int dummy;}; /* for buggy compilers */ +#endif + +z_const char * const z_errmsg[10] = { +"need dictionary", /* Z_NEED_DICT 2 */ +"stream end", /* Z_STREAM_END 1 */ +"", /* Z_OK 0 */ +"file error", /* Z_ERRNO (-1) */ +"stream error", /* Z_STREAM_ERROR (-2) */ +"data error", /* Z_DATA_ERROR (-3) */ +"insufficient memory", /* Z_MEM_ERROR (-4) */ +"buffer error", /* Z_BUF_ERROR (-5) */ +"incompatible version",/* Z_VERSION_ERROR (-6) */ +""}; + + +const char * ZEXPORT zlibVersion() +{ + return ZLIB_VERSION; +} + +uLong ZEXPORT zlibCompileFlags() +{ + uLong flags; + + flags = 0; + switch ((int)(sizeof(uInt))) { + case 2: break; + case 4: flags += 1; break; + case 8: flags += 2; break; + default: flags += 3; + } + switch ((int)(sizeof(uLong))) { + case 2: break; + case 4: flags += 1 << 2; break; + case 8: flags += 2 << 2; break; + default: flags += 3 << 2; + } + switch ((int)(sizeof(voidpf))) { + case 2: break; + case 4: flags += 1 << 4; break; + case 8: flags += 2 << 4; break; + default: flags += 3 << 4; + } + switch ((int)(sizeof(z_off_t))) { + case 2: break; + case 4: flags += 1 << 6; break; + case 8: flags += 2 << 6; break; + default: flags += 3 << 6; + } +#ifdef DEBUG + flags += 1 << 8; +#endif +#if defined(ASMV) || defined(ASMINF) + flags += 1 << 9; +#endif +#ifdef ZLIB_WINAPI + flags += 1 << 10; +#endif +#ifdef BUILDFIXED + flags += 1 << 12; +#endif +#ifdef DYNAMIC_CRC_TABLE + flags += 1 << 13; +#endif +#ifdef NO_GZCOMPRESS + flags += 1L << 16; +#endif +#ifdef NO_GZIP + flags += 1L << 17; +#endif +#ifdef PKZIP_BUG_WORKAROUND + flags += 1L << 20; +#endif +#ifdef FASTEST + flags += 1L << 21; +#endif +#if defined(STDC) || defined(Z_HAVE_STDARG_H) +# ifdef NO_vsnprintf + flags += 1L << 25; +# ifdef HAS_vsprintf_void + flags += 1L << 26; +# endif +# else +# ifdef HAS_vsnprintf_void + flags += 1L << 26; +# endif +# endif +#else + flags += 1L << 24; +# ifdef NO_snprintf + flags += 1L << 25; +# ifdef HAS_sprintf_void + flags += 1L << 26; +# endif +# else +# ifdef HAS_snprintf_void + flags += 1L << 26; +# endif +# endif +#endif + return flags; +} + +#ifdef DEBUG + +# ifndef verbose +# define verbose 0 +# endif +int ZLIB_INTERNAL z_verbose = verbose; + +void ZLIB_INTERNAL z_error (m) + char *m; +{ + criticalError(FDB_EXIT_ABORT, "zlibError", m); +} +#endif + +/* exported to allow conversion of error code to string for compress() and + * uncompress() + */ +const char * ZEXPORT zError(err) + int err; +{ + return ERR_MSG(err); +} + +#if defined(_WIN32_WCE) + /* The Microsoft C Run-Time Library for Windows CE doesn't have + * errno. We define it as a global variable to simplify porting. + * Its value is always 0 and should not be used. + */ + int errno = 0; +#endif + +#ifndef HAVE_MEMCPY + +void ZLIB_INTERNAL zmemcpy(dest, source, len) + Bytef* dest; + const Bytef* source; + uInt len; +{ + if (len == 0) return; + do { + *dest++ = *source++; /* ??? to be unrolled */ + } while (--len != 0); +} + +int ZLIB_INTERNAL zmemcmp(s1, s2, len) + const Bytef* s1; + const Bytef* s2; + uInt len; +{ + uInt j; + + for (j = 0; j < len; j++) { + if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1; + } + return 0; +} + +void ZLIB_INTERNAL zmemzero(dest, len) + Bytef* dest; + uInt len; +{ + if (len == 0) return; + do { + *dest++ = 0; /* ??? to be unrolled */ + } while (--len != 0); +} +#endif + +#ifndef Z_SOLO + +#ifdef SYS16BIT + +#ifdef __TURBOC__ +/* Turbo C in 16-bit mode */ + +# define MY_ZCALLOC + +/* Turbo C malloc() does not allow dynamic allocation of 64K bytes + * and farmalloc(64K) returns a pointer with an offset of 8, so we + * must fix the pointer. Warning: the pointer must be put back to its + * original form in order to free it, use zcfree(). + */ + +#define MAX_PTR 10 +/* 10*64K = 640K */ + +local int next_ptr = 0; + +typedef struct ptr_table_s { + voidpf org_ptr; + voidpf new_ptr; +} ptr_table; + +local ptr_table table[MAX_PTR]; +/* This table is used to remember the original form of pointers + * to large buffers (64K). Such pointers are normalized with a zero offset. + * Since MSDOS is not a preemptive multitasking OS, this table is not + * protected from concurrent access. This hack doesn't work anyway on + * a protected system like OS/2. Use Microsoft C instead. + */ + +voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, unsigned items, unsigned size) +{ + voidpf buf = opaque; /* just to make some compilers happy */ + ulg bsize = (ulg)items*size; + + /* If we allocate less than 65520 bytes, we assume that farmalloc + * will return a usable pointer which doesn't have to be normalized. + */ + if (bsize < 65520L) { + buf = farmalloc(bsize); + if (*(ush*)&buf != 0) return buf; + } else { + buf = farmalloc(bsize + 16L); + } + if (buf == NULL || next_ptr >= MAX_PTR) return NULL; + table[next_ptr].org_ptr = buf; + + /* Normalize the pointer to seg:0 */ + *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4; + *(ush*)&buf = 0; + table[next_ptr++].new_ptr = buf; + return buf; +} + +void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) +{ + int n; + if (*(ush*)&ptr != 0) { /* object < 64K */ + farfree(ptr); + return; + } + /* Find the original pointer */ + for (n = 0; n < next_ptr; n++) { + if (ptr != table[n].new_ptr) continue; + + farfree(table[n].org_ptr); + while (++n < next_ptr) { + table[n-1] = table[n]; + } + next_ptr--; + return; + } + ptr = opaque; /* just to make some compilers happy */ + Assert(0, "zcfree: ptr not found"); +} + +#endif /* __TURBOC__ */ + + +#ifdef M_I86 +/* Microsoft C in 16-bit mode */ + +# define MY_ZCALLOC + +#if (!defined(_MSC_VER) || (_MSC_VER <= 600)) +# define _halloc halloc +# define _hfree hfree +#endif + +voidpf ZLIB_INTERNAL zcalloc (voidpf opaque, uInt items, uInt size) +{ + if (opaque) opaque = 0; /* to make compiler happy */ + return _halloc((long)items, size); +} + +void ZLIB_INTERNAL zcfree (voidpf opaque, voidpf ptr) +{ + if (opaque) opaque = 0; /* to make compiler happy */ + _hfree(ptr); +} + +#endif /* M_I86 */ + +#endif /* SYS16BIT */ + + +#ifndef MY_ZCALLOC /* Any system without a special alloc function */ + +#ifndef STDC +extern voidp malloc OF((uInt size)); +extern voidp calloc OF((uInt items, uInt size)); +extern void free OF((voidpf ptr)); +#endif + +voidpf ZLIB_INTERNAL zcalloc (opaque, items, size) + voidpf opaque; + unsigned items; + unsigned size; +{ + if (opaque) items += size - size; /* make compiler happy */ + return sizeof(uInt) > 2 ? (voidpf)malloc(items * size) : + (voidpf)calloc(items, size); +} + +void ZLIB_INTERNAL zcfree (opaque, ptr) + voidpf opaque; + voidpf ptr; +{ + free(ptr); + if (opaque) return; /* make compiler happy */ +} + +#endif /* MY_ZCALLOC */ + +#endif /* !Z_SOLO */ diff --git a/fdbrpc/zlib/zutil.h b/fdbrpc/zlib/zutil.h new file mode 100644 index 0000000000..24ab06b1cf --- /dev/null +++ b/fdbrpc/zlib/zutil.h @@ -0,0 +1,253 @@ +/* zutil.h -- internal interface and configuration of the compression library + * Copyright (C) 1995-2013 Jean-loup Gailly. + * For conditions of distribution and use, see copyright notice in zlib.h + */ + +/* WARNING: this file should *not* be used by applications. It is + part of the implementation of the compression library and is + subject to change. Applications should only use zlib.h. + */ + +/* @(#) $Id$ */ + +#ifndef ZUTIL_H +#define ZUTIL_H + +#ifdef HAVE_HIDDEN +# define ZLIB_INTERNAL __attribute__((visibility ("hidden"))) +#else +# define ZLIB_INTERNAL +#endif + +#include "zlib.h" + +#if defined(STDC) && !defined(Z_SOLO) +# if !(defined(_WIN32_WCE) && defined(_MSC_VER)) +# include +# endif +# include +# include +#endif + +#ifdef Z_SOLO + typedef long ptrdiff_t; /* guess -- will be caught if guess is wrong */ +#endif + +#ifndef local +# define local static +#endif +/* compile with -Dlocal if your debugger can't find static symbols */ + +typedef unsigned char uch; +typedef uch FAR uchf; +typedef unsigned short ush; +typedef ush FAR ushf; +typedef unsigned long ulg; + +extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */ +/* (size given to avoid silly warnings with Visual C++) */ + +#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)] + +#define ERR_RETURN(strm,err) \ + return (strm->msg = ERR_MSG(err), (err)) +/* To be used only when the state is known to be valid */ + + /* common constants */ + +#ifndef DEF_WBITS +# define DEF_WBITS MAX_WBITS +#endif +/* default windowBits for decompression. MAX_WBITS is for compression only */ + +#if MAX_MEM_LEVEL >= 8 +# define DEF_MEM_LEVEL 8 +#else +# define DEF_MEM_LEVEL MAX_MEM_LEVEL +#endif +/* default memLevel */ + +#define STORED_BLOCK 0 +#define STATIC_TREES 1 +#define DYN_TREES 2 +/* The three kinds of block type */ + +#define MIN_MATCH 3 +#define MAX_MATCH 258 +/* The minimum and maximum match lengths */ + +#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */ + + /* target dependencies */ + +#if defined(MSDOS) || (defined(WINDOWS) && !defined(WIN32)) +# define OS_CODE 0x00 +# ifndef Z_SOLO +# if defined(__TURBOC__) || defined(__BORLANDC__) +# if (__STDC__ == 1) && (defined(__LARGE__) || defined(__COMPACT__)) + /* Allow compilation with ANSI keywords only enabled */ + void _Cdecl farfree( void *block ); + void *_Cdecl farmalloc( unsigned long nbytes ); +# else +# include +# endif +# else /* MSC or DJGPP */ +# include +# endif +# endif +#endif + +#ifdef AMIGA +# define OS_CODE 0x01 +#endif + +#if defined(VAXC) || defined(VMS) +# define OS_CODE 0x02 +# define F_OPEN(name, mode) \ + fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512") +#endif + +#if defined(ATARI) || defined(atarist) +# define OS_CODE 0x05 +#endif + +#ifdef OS2 +# define OS_CODE 0x06 +# if defined(M_I86) && !defined(Z_SOLO) +# include +# endif +#endif + +#if defined(MACOS) || defined(TARGET_OS_MAC) +# define OS_CODE 0x07 +# ifndef Z_SOLO +# if defined(__MWERKS__) && __dest_os != __be_os && __dest_os != __win32_os +# include /* for fdopen */ +# else +# ifndef fdopen +# define fdopen(fd,mode) NULL /* No fdopen() */ +# endif +# endif +# endif +#endif + +#ifdef TOPS20 +# define OS_CODE 0x0a +#endif + +#ifdef WIN32 +# ifndef __CYGWIN__ /* Cygwin is Unix, not Win32 */ +# define OS_CODE 0x0b +# endif +#endif + +#ifdef __50SERIES /* Prime/PRIMOS */ +# define OS_CODE 0x0f +#endif + +#if defined(_BEOS_) || defined(RISCOS) +# define fdopen(fd,mode) NULL /* No fdopen() */ +#endif + +#if (defined(_MSC_VER) && (_MSC_VER > 600)) && !defined __INTERIX +# if defined(_WIN32_WCE) +# define fdopen(fd,mode) NULL /* No fdopen() */ +# ifndef _PTRDIFF_T_DEFINED + typedef int ptrdiff_t; +# define _PTRDIFF_T_DEFINED +# endif +# else +# define fdopen(fd,type) _fdopen(fd,type) +# endif +#endif + +#if defined(__BORLANDC__) && !defined(MSDOS) + #pragma warn -8004 + #pragma warn -8008 + #pragma warn -8066 +#endif + +/* provide prototypes for these when building zlib without LFS */ +#if !defined(_WIN32) && \ + (!defined(_LARGEFILE64_SOURCE) || _LFS64_LARGEFILE-0 == 0) + ZEXTERN uLong ZEXPORT adler32_combine64 OF((uLong, uLong, z_off_t)); + ZEXTERN uLong ZEXPORT crc32_combine64 OF((uLong, uLong, z_off_t)); +#endif + + /* common defaults */ + +#ifndef OS_CODE +# define OS_CODE 0x03 /* assume Unix */ +#endif + +#ifndef F_OPEN +# define F_OPEN(name, mode) fopen((name), (mode)) +#endif + + /* functions */ + +#if defined(pyr) || defined(Z_SOLO) +# define NO_MEMCPY +#endif +#if defined(SMALL_MEDIUM) && !defined(_MSC_VER) && !defined(__SC__) + /* Use our own functions for small and medium model with MSC <= 5.0. + * You may have to use the same strategy for Borland C (untested). + * The __SC__ check is for Symantec. + */ +# define NO_MEMCPY +#endif +#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY) +# define HAVE_MEMCPY +#endif +#ifdef HAVE_MEMCPY +# ifdef SMALL_MEDIUM /* MSDOS small or medium model */ +# define zmemcpy _fmemcpy +# define zmemcmp _fmemcmp +# define zmemzero(dest, len) _fmemset(dest, 0, len) +# else +# define zmemcpy memcpy +# define zmemcmp memcmp +# define zmemzero(dest, len) memset(dest, 0, len) +# endif +#else + void ZLIB_INTERNAL zmemcpy OF((Bytef* dest, const Bytef* source, uInt len)); + int ZLIB_INTERNAL zmemcmp OF((const Bytef* s1, const Bytef* s2, uInt len)); + void ZLIB_INTERNAL zmemzero OF((Bytef* dest, uInt len)); +#endif + +/* Diagnostic functions */ +#ifdef DEBUG +# include + extern int ZLIB_INTERNAL z_verbose; + extern void ZLIB_INTERNAL z_error OF((char *m)); +# define Assert(cond,msg) {if(!(cond)) z_error(msg);} +# define Trace(x) {if (z_verbose>=0) fprintf x ;} +# define Tracev(x) {if (z_verbose>0) fprintf x ;} +# define Tracevv(x) {if (z_verbose>1) fprintf x ;} +# define Tracec(c,x) {if (z_verbose>0 && (c)) fprintf x ;} +# define Tracecv(c,x) {if (z_verbose>1 && (c)) fprintf x ;} +#else +# define Assert(cond,msg) +# define Trace(x) +# define Tracev(x) +# define Tracevv(x) +# define Tracec(c,x) +# define Tracecv(c,x) +#endif + +#ifndef Z_SOLO + voidpf ZLIB_INTERNAL zcalloc OF((voidpf opaque, unsigned items, + unsigned size)); + void ZLIB_INTERNAL zcfree OF((voidpf opaque, voidpf ptr)); +#endif + +#define ZALLOC(strm, items, size) \ + (*((strm)->zalloc))((strm)->opaque, (items), (size)) +#define ZFREE(strm, addr) (*((strm)->zfree))((strm)->opaque, (voidpf)(addr)) +#define TRY_FREE(s, p) {if (p) ZFREE(s, p);} + +/* Reverse the bytes in a 32-bit value */ +#define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \ + (((q) & 0xff00) << 8) + (((q) & 0xff) << 24)) + +#endif /* ZUTIL_H */ diff --git a/fdbserver/ApplyMetadataMutation.h b/fdbserver/ApplyMetadataMutation.h new file mode 100644 index 0000000000..4506d52270 --- /dev/null +++ b/fdbserver/ApplyMetadataMutation.h @@ -0,0 +1,346 @@ +/* + * ApplyMetadataMutation.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_APPLYMETADATAMUTATION_H +#define FDBSERVER_APPLYMETADATAMUTATION_H +#pragma once + +#include "fdbclient/MutationList.h" +#include "fdbclient/SystemData.h" +#include "fdbclient/BackupAgent.h" +#include "IKeyValueStore.h" +#include "LogSystem.h" +#include "LogProtocolMessage.h" +#include "flow/Notified.h" + +static bool isMetadataMutation(MutationRef const& m) { + // FIXME: This is conservative - not everything in system keyspace is necessarily processed by applyMetadataMutations + return (m.type == MutationRef::SetValue && m.param1.size() && m.param1[0] == systemKeys.begin[0] && !m.param1.startsWith(nonMetadataSystemKeys.begin)) || + (m.type == MutationRef::ClearRange && m.param2.size() && m.param2[0] == systemKeys.begin[0] && !nonMetadataSystemKeys.contains(KeyRangeRef(m.param1, m.param2)) ); +} + +struct applyMutationsData { + Future worker; + Version endVersion; + Reference> keyVersion; +}; + +static void applyMetadataMutations(UID const& dbgid, Arena &arena, VectorRef const& mutations, IKeyValueStore* txnStateStore, LogPushData* toCommit, bool *confChange, Reference logSystem = Reference(), Version popVersion = 0, + KeyRangeMap >* vecBackupKeys = NULL, KeyRangeMap>* keyTags = NULL, std::map* uid_applyMutationsData = NULL, + RequestStream commit = RequestStream(), Database cx = Database(), NotifiedVersion* commitVersion = NULL, std::map *tagCache = NULL, bool initialCommit = false) { + for (auto const& m : mutations) { + //TraceEvent("MetadataMutation", dbgid).detail("M", m.toString()); + + if (m.param1.size() && m.param1[0] == systemKeys.begin[0] && m.type == MutationRef::SetValue) { + if(m.param1.startsWith(keyServersPrefix)) { + if(keyTags) { + KeyRef k = m.param1.removePrefix(keyServersPrefix); + if(k != allKeys.end) { + KeyRef end = keyTags->rangeContaining(k).end(); + KeyRangeRef insertRange(k,end); + vector src, dest; + decodeKeyServersValue(m.param2, src, dest); + std::set tags; + + if(!tagCache) { + for(auto id : src) + tags.insert(decodeServerTagValue(txnStateStore->readValue(serverTagKeyFor(id)).get().get())); + for(auto id : dest) + tags.insert(decodeServerTagValue(txnStateStore->readValue(serverTagKeyFor(id)).get().get())); + } + else { + Tag tag; + for(auto id : src) { + auto tagItr = tagCache->find(id); + if(tagItr == tagCache->end()) { + tag = decodeServerTagValue( txnStateStore->readValue( serverTagKeyFor(id) ).get().get() ); + (*tagCache)[id] = tag; + } else { + tag = tagItr->second; + } + tags.insert( tag ); + } + for(auto id : dest) { + auto tagItr = tagCache->find(id); + if(tagItr == tagCache->end()) { + tag = decodeServerTagValue( txnStateStore->readValue( serverTagKeyFor(id) ).get().get() ); + (*tagCache)[id] = tag; + } else { + tag = tagItr->second; + } + tags.insert( tag ); + } + } + + keyTags->insert(insertRange,std::vector(tags.begin(), tags.end())); + } + } + if(!initialCommit) txnStateStore->set(KeyValueRef(m.param1, m.param2)); + } else if (m.param1.startsWith(serverKeysPrefix)) { + if(toCommit) { + MutationRef privatized = m; + privatized.param1 = m.param1.withPrefix(systemKeys.begin, arena); + TraceEvent(SevDebug, "SendingPrivateMutation", dbgid).detail("Original", m.toString()).detail("Privatized", privatized.toString()).detail("Server", serverKeysDecodeServer(m.param1)) + .detail("tagKey", printable(serverTagKeyFor( serverKeysDecodeServer(m.param1) ))).detail("tag", decodeServerTagValue( txnStateStore->readValue( serverTagKeyFor( serverKeysDecodeServer(m.param1) ) ).get().get() )); + + toCommit->addTag( decodeServerTagValue( txnStateStore->readValue( serverTagKeyFor( serverKeysDecodeServer(m.param1) ) ).get().get() ) ); + toCommit->addTypedMessage(privatized); + } + } else if (m.param1.startsWith(serverTagPrefix)) { + UID id = decodeServerTagKey(m.param1); + Tag tag = decodeServerTagValue(m.param2); + + if(toCommit) { + MutationRef privatized = m; + privatized.param1 = m.param1.withPrefix(systemKeys.begin, arena); + TraceEvent("ServerTag", dbgid).detail("server", id).detail("tag", tag); + + toCommit->addTag(tag); + toCommit->addTypedMessage(LogProtocolMessage()); + toCommit->addTag(tag); + toCommit->addTypedMessage(privatized); + } + if(!initialCommit) { + txnStateStore->set(KeyValueRef(m.param1, m.param2)); + if(tagCache) { + (*tagCache)[id] = tag; + } + } + } + else if (m.param1.startsWith(configKeysPrefix) || m.param1 == coordinatorsKey) { + if(Optional(m.param2) != txnStateStore->readValue(m.param1).get().cast_to()) { // FIXME: Make this check more specific, here or by reading configuration whenever there is a change + auto t = txnStateStore->readValue(m.param1).get(); + if (logSystem && m.param1.startsWith( excludedServersPrefix )) { + // If one of our existing tLogs is now excluded, we have to die and recover + auto addr = decodeExcludedServersKey(m.param1); + for( auto tl : logSystem->getLogSystemConfig().tLogs ) { + if(!tl.present() || addr.excludes(tl.interf().commit.getEndpoint().address)) { + TraceEvent("MutationRequiresRestart", dbgid).detail("M", m.toString()).detail("PrevValue", t.present() ? printable(t.get()) : "(none)").detail("toCommit", toCommit!=NULL).detail("addr", addr.toString()); + if(confChange) *confChange = true; + } + } + } else if(m.param1 != excludedServersVersionKey) { + TraceEvent("MutationRequiresRestart", dbgid).detail("M", m.toString()).detail("PrevValue", t.present() ? printable(t.get()) : "(none)").detail("toCommit", toCommit!=NULL); + if(confChange) *confChange = true; + } + } + if(!initialCommit) txnStateStore->set(KeyValueRef(m.param1, m.param2)); + } + else if (m.param1.startsWith(serverListPrefix) || m.param1 == databaseLockedKey || m.param1.startsWith(applyMutationsBeginRange.begin) || + m.param1.startsWith(applyMutationsAddPrefixRange.begin) || m.param1.startsWith(applyMutationsRemovePrefixRange.begin)) { + if(!initialCommit) txnStateStore->set(KeyValueRef(m.param1, m.param2)); + } + else if (m.param1.startsWith(applyMutationsEndRange.begin)) { + if(!initialCommit) txnStateStore->set(KeyValueRef(m.param1, m.param2)); + if(uid_applyMutationsData != NULL) { + Key uid = m.param1.removePrefix(applyMutationsEndRange.begin); + auto &p = (*uid_applyMutationsData)[uid]; + p.endVersion = BinaryReader::fromStringRef(m.param2, Unversioned()); + if(p.keyVersion == Reference>()) + p.keyVersion = Reference>( new KeyRangeMap() ); + if(!p.worker.isValid() || p.worker.isReady()) { + auto addPrefixValue = txnStateStore->readValue(uid.withPrefix(applyMutationsAddPrefixRange.begin)).get(); + auto removePrefixValue = txnStateStore->readValue(uid.withPrefix(applyMutationsRemovePrefixRange.begin)).get(); + auto beginValue = txnStateStore->readValue(uid.withPrefix(applyMutationsBeginRange.begin)).get(); + p.worker = applyMutations( cx, uid, addPrefixValue.present() ? addPrefixValue.get() : Key(), + removePrefixValue.present() ? removePrefixValue.get() : Key(), + beginValue.present() ? BinaryReader::fromStringRef(beginValue.get(), Unversioned()) : 0, + &p.endVersion, commit, commitVersion, p.keyVersion ); + } + } + } + else if (m.param1.startsWith(applyMutationsKeyVersionMapRange.begin)) { + if(!initialCommit) txnStateStore->set(KeyValueRef(m.param1, m.param2)); + if(uid_applyMutationsData != NULL) { + if(m.param1.size() >= applyMutationsKeyVersionMapRange.begin.size() + sizeof(UID)) { + Key uid = m.param1.substr(applyMutationsKeyVersionMapRange.begin.size(), sizeof(UID)); + Key k = m.param1.substr(applyMutationsKeyVersionMapRange.begin.size() + sizeof(UID)); + auto &p = (*uid_applyMutationsData)[uid]; + if(p.keyVersion == Reference>()) + p.keyVersion = Reference>( new KeyRangeMap() ); + p.keyVersion->rawInsert( k, BinaryReader::fromStringRef(m.param2, Unversioned()) ); + } + } + } + else if (m.param1.startsWith(logRangesRange.begin)) { + if(!initialCommit) txnStateStore->set(KeyValueRef(m.param1, m.param2)); + if (vecBackupKeys) { + Key logDestination; + KeyRef logRangeBegin = logRangesDecodeKey(m.param1, NULL); + Key logRangeEnd = logRangesDecodeValue(m.param2, &logDestination); + + // Insert the logDestination into each range of vecBackupKeys overlapping the decoded range + for (auto& logRange : vecBackupKeys->modify(KeyRangeRef(logRangeBegin, logRangeEnd))) { + logRange->value().insert(logDestination); + } + + // Log the modification + TraceEvent("LogRangeAdd").detail("logRanges", vecBackupKeys->size()).detail("mutationKey", printable(m.param1)) + .detail("logRangeBegin", printable(logRangeBegin)).detail("logRangeEnd", printable(logRangeEnd)); + } + } + else if (m.param1.startsWith(globalKeysPrefix)) { + if(toCommit) { + // Notifies all servers that a Master's server epoch ends + auto allServers = txnStateStore->readRange(serverTagKeys).get(); + + if (m.param1 == lastEpochEndKey) { + for (auto &kv : allServers) + toCommit->addTag(decodeServerTagValue(kv.value)); + toCommit->addTypedMessage(LogProtocolMessage()); + } + + MutationRef privatized = m; + privatized.param1 = m.param1.withPrefix(systemKeys.begin, arena); + for (auto &kv : allServers) + toCommit->addTag(decodeServerTagValue(kv.value)); + toCommit->addTypedMessage(privatized); + } + } + } + else if (m.param2.size() && m.param2[0] == systemKeys.begin[0] && m.type == MutationRef::ClearRange) { + KeyRangeRef range(m.param1, m.param2); + + if (keyServersKeys.intersects(range)) { + KeyRangeRef r = range & keyServersKeys; + if(keyTags) { + KeyRangeRef clearRange(r.begin.removePrefix(keyServersPrefix), r.end.removePrefix(keyServersPrefix)); + keyTags->insert(clearRange, clearRange.begin == StringRef() ? vector() : keyTags->rangeContainingKeyBefore(clearRange.begin).value()); + } + + if(!initialCommit) txnStateStore->clear(r); + } + if (configKeys.intersects(range)) { + if(!initialCommit) txnStateStore->clear(range & configKeys); + if(!excludedServersKeys.contains(range)) { + TraceEvent("MutationRequiresRestart", dbgid).detail("M", m.toString()); + if(confChange) *confChange = true; + } + } + if ( serverListKeys.intersects( range )) { + if(!initialCommit) txnStateStore->clear( range & serverListKeys ); + } + if ( serverTagKeys.intersects( range )) { + // Storage server removal always happens in a separate version from any prior writes (or any subsequent reuse of the tag) so we + // can safely destroy the tag here without any concern about intra-batch ordering + if (logSystem && popVersion) { + auto serverKeysCleared = txnStateStore->readRange( range & serverTagKeys ).get(); // read is expected to be immediately available + for(auto &kv : serverKeysCleared) { + TraceEvent("ServerTagRemove").detail("popVersion", popVersion).detail("tag", decodeServerTagValue(kv.value)).detail("server", decodeServerTagKey(kv.key)); + logSystem->pop( popVersion, decodeServerTagValue(kv.value) ); + + if(toCommit) { + MutationRef privatized = m; + privatized.param1 = kv.key.withPrefix(systemKeys.begin, arena); + privatized.param2 = keyAfter(kv.key, arena).withPrefix(systemKeys.begin, arena); + + toCommit->addTag(decodeServerTagValue(kv.value)); + toCommit->addTypedMessage(privatized); + } + } + } + if(!initialCommit) txnStateStore->clear( range & serverTagKeys ); + } + if (range.contains(coordinatorsKey)) { + if(!initialCommit) txnStateStore->clear(singleKeyRange(coordinatorsKey)); + } + if (range.contains(databaseLockedKey)) { + if(!initialCommit) txnStateStore->clear(singleKeyRange(databaseLockedKey)); + } + if(range.intersects(applyMutationsEndRange)) { + KeyRangeRef commonEndRange(range & applyMutationsEndRange); + if(!initialCommit) txnStateStore->clear(commonEndRange); + if(uid_applyMutationsData != NULL) { + uid_applyMutationsData->erase(uid_applyMutationsData->lower_bound(m.param1.substr(applyMutationsEndRange.begin.size())), + m.param2 == applyMutationsEndRange.end ? uid_applyMutationsData->end() : uid_applyMutationsData->lower_bound(m.param2.substr(applyMutationsEndRange.begin.size()))); + } + } + if(range.intersects(applyMutationsKeyVersionMapRange)) { + KeyRangeRef commonApplyRange(range & applyMutationsKeyVersionMapRange); + if(!initialCommit) txnStateStore->clear(commonApplyRange); + if(uid_applyMutationsData != NULL) { + if(m.param1.size() >= applyMutationsKeyVersionMapRange.begin.size() + sizeof(UID) && m.param2.size() >= applyMutationsKeyVersionMapRange.begin.size() + sizeof(UID)) { + Key uid = m.param1.substr(applyMutationsKeyVersionMapRange.begin.size(), sizeof(UID)); + Key uid2 = m.param2.substr(applyMutationsKeyVersionMapRange.begin.size(), sizeof(UID)); + + if(uid == uid2) { + auto &p = (*uid_applyMutationsData)[uid]; + if(p.keyVersion == Reference>()) + p.keyVersion = Reference>( new KeyRangeMap() ); + p.keyVersion->rawErase( KeyRangeRef( m.param1.substr(applyMutationsKeyVersionMapRange.begin.size() + sizeof(UID)), m.param2.substr(applyMutationsKeyVersionMapRange.begin.size() + sizeof(UID))) ); + } + } + } + } + if (range.intersects(logRangesRange)) { + KeyRangeRef commonLogRange(range & logRangesRange); + + TraceEvent("LogRangeClear") + .detail("rangeBegin", printable(range.begin)).detail("rangeEnd", printable(range.end)) + .detail("intersectBegin", printable(commonLogRange.begin)).detail("intersectEnd", printable(commonLogRange.end)); + + // Remove the key range from the vector, if defined + if (vecBackupKeys) { + KeyRef logKeyBegin; + Key logKeyEnd, logDestination; + + // Identify the backup keys being removed + // read is expected to be immediately available + auto logRangesAffected = txnStateStore->readRange(commonLogRange).get(); + + TraceEvent("LogRangeClearBegin").detail("affectedLogRanges", logRangesAffected.size()); + + // Add the backup name to the backup locations that do not have it + for (auto logRangeAffected : logRangesAffected) + { + // Parse the backup key and name + logKeyBegin = logRangesDecodeKey(logRangeAffected.key, NULL); + + // Decode the log destination and key value + logKeyEnd = logRangesDecodeValue(logRangeAffected.value, &logDestination); + + TraceEvent("LogRangeErase").detail("affectedKey", printable(logRangeAffected.key)).detail("affectedValue", printable(logRangeAffected.value)) + .detail("logKeyBegin", printable(logKeyBegin)).detail("logKeyEnd", printable(logKeyEnd)) + .detail("logDestination", printable(logDestination)); + + // Identify the locations to place the backup key + auto logRanges = vecBackupKeys->modify(KeyRangeRef(logKeyBegin, logKeyEnd)); + + // Remove the log prefix from the ranges which include it + for (auto logRange : logRanges) + { + auto &logRangeMap = logRange->value(); + + // Remove the backup name from the range + logRangeMap.erase(logDestination); + } + } + + // Coallesce the entire range + vecBackupKeys->coalesce(allKeys); + } + + if(!initialCommit) txnStateStore->clear(commonLogRange); + } + } + } +} + +#endif diff --git a/fdbserver/ClusterController.actor.cpp b/fdbserver/ClusterController.actor.cpp new file mode 100644 index 0000000000..d3589d9c8e --- /dev/null +++ b/fdbserver/ClusterController.actor.cpp @@ -0,0 +1,1598 @@ +/* + * ClusterController.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "fdbrpc/FailureMonitor.h" +#include "flow/ActorCollection.h" +#include "fdbclient/NativeAPI.h" +#include "fdbserver/CoordinationInterface.h" +#include "Knobs.h" +#include "MoveKeys.h" +#include "WorkerInterface.h" +#include "LeaderElection.h" +#include "WaitFailure.h" +#include "ClusterRecruitmentInterface.h" +#include "ServerDBInfo.h" +#include "Status.h" +#include +#include "fdbclient/DatabaseContext.h" +#include "RecoveryState.h" +#include "fdbclient/ReadYourWrites.h" +#include "fdbrpc/Replication.h" +#include "fdbrpc/ReplicationUtils.h" + +void failAfter( Future trigger, Endpoint e ); + +struct WorkerInfo : NonCopyable { + Future watcher; + ReplyPromise reply; + Generation gen; + int reboots; + WorkerInterface interf; + ProcessClass initialClass; + ProcessClass processClass; + + WorkerInfo() : gen(-1), reboots(0) {} + WorkerInfo( Future watcher, ReplyPromise reply, Generation gen, WorkerInterface interf, ProcessClass initialClass, ProcessClass processClass ) : + watcher(watcher), reply(reply), gen(gen), reboots(0), interf(interf), initialClass(initialClass), processClass(processClass) {} + + WorkerInfo( WorkerInfo&& r ) noexcept(true) : watcher(std::move(r.watcher)), reply(std::move(r.reply)), gen(r.gen), + reboots(r.reboots), interf(std::move(r.interf)), initialClass(r.initialClass), processClass(r.processClass) {} + void operator=( WorkerInfo&& r ) noexcept(true) { + watcher = std::move(r.watcher); + reply = std::move(r.reply); + gen = r.gen; + reboots = r.reboots; + interf = std::move(r.interf); + initialClass = r.initialClass; + processClass = r.processClass; + } +}; + +class ClusterControllerData { +public: + struct DBInfo { + Reference> clientInfo; + Reference> serverInfo; + ProcessIssuesMap clientsWithIssues, workersWithIssues; + std::map incompatibleConnections; + ClientVersionMap clientVersionMap; + Promise forceMasterFailure; + int64_t masterRegistrationCount; + DatabaseConfiguration config; // Asynchronously updated via master registration + Database db; + + DBInfo() : masterRegistrationCount(0), + clientInfo( new AsyncVar( ClientDBInfo() ) ), + serverInfo( new AsyncVar( ServerDBInfo( LiteralStringRef("DB") ) ) ), + db( DatabaseContext::create( clientInfo, Future(), LocalityData(), true, TaskDefaultEndpoint, true ) ) // SOMEDAY: Locality! + { + + } + }; + + struct UpdateWorkerList { + Future init( Database const& db ) { + return update(this, db); + } + + void set(Optional> processID, Optional data ) { + delta[processID] = data; + anyDelta.set(true); + } + + private: + std::map>, Optional> delta; + AsyncVar anyDelta; + + ACTOR static Future update( UpdateWorkerList* self, Database db ) { + // The Database we are using is based on worker registrations to this cluster controller, which come only + // from master servers that we started, so it shouldn't be possible for multiple cluster controllers to fight. + state Transaction tr(db); + loop { + try { + tr.clear( workerListKeys ); + Void _ = wait( tr.commit() ); + break; + } catch (Error& e) { + Void _ = wait( tr.onError(e) ); + } + } + + loop { + // Wait for some changes + while (!self->anyDelta.get()) + Void _ = wait( self->anyDelta.onChange() ); + self->anyDelta.set(false); + + state std::map>, Optional> delta; + delta.swap( self->delta ); + + TraceEvent("UpdateWorkerList").detail("DeltaCount", delta.size()); + + // Do a transaction to write the changes + loop { + try { + for(auto w = delta.begin(); w != delta.end(); ++w) { + if (w->second.present()) { + tr.set( workerListKeyFor( w->first.get() ), workerListValue( w->second.get()) ); + } else + tr.clear( workerListKeyFor( w->first.get() ) ); + } + Void _ = wait( tr.commit() ); + break; + } catch (Error& e) { + Void _ = wait( tr.onError(e) ); + } + } + } + } + }; + + bool workerAvailable( WorkerInfo const& worker, bool checkStable ) { + return IFailureMonitor::failureMonitor().getState(worker.interf.storage.getEndpoint()).isAvailable() && ( !checkStable || worker.reboots < 2 ); + } + + std::pair getStorageWorker( RecruitStorageRequest const& req ) { + std::set>> excludedMachines( req.excludeMachines.begin(), req.excludeMachines.end() ); + std::set>> excludedDCs( req.excludeDCs.begin(), req.excludeDCs.end() ); + std::set excludedAddresses( req.excludeAddresses.begin(), req.excludeAddresses.end() ); + + for( auto& it : id_worker ) + if( workerAvailable( it.second, false ) && + !excludedMachines.count(it.second.interf.locality.zoneId()) && + !excludedDCs.count(it.second.interf.locality.dcId()) && + !addressExcluded(excludedAddresses, it.second.interf.address()) && + it.second.processClass.machineClassFitness( ProcessClass::Storage ) <= ProcessClass::UnsetFit ) { + return std::make_pair(it.second.interf, it.second.processClass); + } + + if( req.criticalRecruitment ) { + ProcessClass::Fitness bestFit = ProcessClass::NeverAssign; + Optional> bestInfo; + for( auto& it : id_worker ) { + ProcessClass::Fitness fit = it.second.processClass.machineClassFitness( ProcessClass::Storage ); + if( workerAvailable( it.second, false ) && + !excludedMachines.count(it.second.interf.locality.zoneId()) && + !excludedDCs.count(it.second.interf.locality.dcId()) && + !addressExcluded(excludedAddresses, it.second.interf.address()) && + fit < bestFit ) { + bestFit = fit; + bestInfo = std::make_pair(it.second.interf, it.second.processClass); + } + } + + if( bestInfo.present() ) { + return bestInfo.get(); + } + } + + throw no_more_servers(); + } + + //FIXME: get master in the same datacenter as the proxies and resolvers for ratekeeper, however this is difficult because the master is recruited before we know the cluster's configuration + std::pair getMasterWorker( bool checkStable = false ) { + ProcessClass::Fitness bestFit = ProcessClass::NeverAssign; + Optional> bestInfo; + int numEquivalent = 1; + for( auto& it : id_worker ) { + if( workerAvailable( it.second, checkStable ) ) { + ProcessClass::Fitness fit = it.second.processClass.machineClassFitness( ProcessClass::Master ); + if( fit < bestFit ) { + bestInfo = std::make_pair(it.second.interf, it.second.processClass); + bestFit = fit; + numEquivalent = 1; + } + else if( fit != ProcessClass::NeverAssign && fit == bestFit && g_random->random01() < 1.0/++numEquivalent ) + bestInfo = std::make_pair(it.second.interf, it.second.processClass); + } + } + + if( bestInfo.present() ) + return bestInfo.get(); + + throw no_more_servers(); + } + +std::vector> getWorkersForTlogsAcrossDatacenters( DatabaseConfiguration const& conf, std::map< Optional>, int>& id_used, bool checkStable = false ) + { + std::map>> fitness_workers; + std::vector> results; + LocalitySetRef logServerSet; + LocalityMap>* logServerMap; + bool bCompleted = false; + + logServerSet = Reference(new LocalityMap>()); + logServerMap = (LocalityMap>*) logServerSet.getPtr(); + + for( auto& it : id_worker ) { + auto fitness = it.second.processClass.machineClassFitness( ProcessClass::TLog ); + if( workerAvailable(it.second, checkStable) && !conf.isExcludedServer(it.second.interf.address()) && fitness != ProcessClass::NeverAssign ) { + fitness_workers[ fitness ].push_back(std::make_pair(it.second.interf, it.second.processClass)); + } + } + + results.reserve(results.size() + id_worker.size()); + for (int fitness = ProcessClass::BestFit; fitness != ProcessClass::NeverAssign; fitness ++) + { + auto fitnessEnum = (ProcessClass::Fitness) fitness; + if (fitness_workers.find(fitnessEnum) == fitness_workers.end()) + continue; + for (auto& worker : fitness_workers[(ProcessClass::Fitness) fitness] ) { + logServerMap->add(worker.first.locality, &worker); + } + if (logServerSet->size() < conf.tLogReplicationFactor) { + TraceEvent(SevWarn,"GWFTADTooFew", id) + .detail("Fitness", fitness) + .detail("Processes", logServerSet->size()) + .detail("tLogReplicationFactor", conf.tLogReplicationFactor) + .detail("tLogPolicy", conf.tLogPolicy ? conf.tLogPolicy->info() : "[unset]") + .detail("DesiredLogs", conf.getDesiredLogs()); + } + else if (logServerSet->size() <= conf.getDesiredLogs()) { + ASSERT(conf.tLogPolicy); + if (logServerSet->validate(conf.tLogPolicy)) { + for (auto& object : logServerMap->getObjects()) { + results.push_back(*object); + } + bCompleted = true; + break; + } + else { + TraceEvent(SevWarn,"GWFTADNotAcceptable", id) + .detail("Fitness", fitness) + .detail("Processes", logServerSet->size()) + .detail("tLogReplicationFactor", conf.tLogReplicationFactor) + .detail("tLogPolicy", conf.tLogPolicy ? conf.tLogPolicy->info() : "[unset]") + .detail("DesiredLogs", conf.getDesiredLogs()); + } + } + // Try to select the desired size, if larger + else { + std::vector bestSet; + std::vector tLocalities; + ASSERT(conf.tLogPolicy); + + // Try to find the best team of servers to fulfill the policy + if (findBestPolicySet(bestSet, logServerSet, conf.tLogPolicy, conf.getDesiredLogs(), + SERVER_KNOBS->POLICY_RATING_TESTS, SERVER_KNOBS->POLICY_GENERATIONS)) + { + results.reserve(results.size() + bestSet.size()); + for (auto& entry : bestSet) { + auto object = logServerMap->getObject(entry); + ASSERT(object); + results.push_back(*object); + tLocalities.push_back(object->first.locality); + } + TraceEvent("GWFTADBestResults", id) + .detail("Fitness", fitness) + .detail("Processes", logServerSet->size()) + .detail("BestCount", bestSet.size()) + .detail("BestZones", ::describeZones(tLocalities)) + .detail("BestDataHalls", ::describeDataHalls(tLocalities)) + .detail("tLogPolicy", conf.tLogPolicy ? conf.tLogPolicy->info() : "[unset]") + .detail("TotalResults", results.size()) + .detail("DesiredLogs", conf.getDesiredLogs()); + bCompleted = true; + break; + } + else { + TraceEvent(SevWarn,"GWFTADNoBest", id) + .detail("Fitness", fitness) + .detail("Processes", logServerSet->size()) + .detail("tLogReplicationFactor", conf.tLogReplicationFactor) + .detail("tLogPolicy", conf.tLogPolicy ? conf.tLogPolicy->info() : "[unset]") + .detail("DesiredLogs", conf.getDesiredLogs()); + } + } + } + + // If policy cannot be satisfied + if (!bCompleted) + { + std::vector tLocalities; + for (auto& object : logServerMap->getObjects()) { + tLocalities.push_back(object->first.locality); + } + + TraceEvent(SevWarn, "GetTLogTeamFailed") + .detail("Policy", conf.tLogPolicy->info()) + .detail("Processes", logServerSet->size()) + .detail("Workers", id_worker.size()) + .detail("TLogZones", ::describeZones(tLocalities)) + .detail("TLogDataHalls", ::describeDataHalls(tLocalities)) + .detail("Replication", conf.tLogReplicationFactor) + .detail("DesiredLogs", conf.getDesiredLogs()) + .detail("RatingTests",SERVER_KNOBS->POLICY_RATING_TESTS) + .detail("PolicyGenerations",SERVER_KNOBS->POLICY_GENERATIONS).backtrace(); + + // Free the set + logServerSet->clear(); + logServerSet.clear(); + throw no_more_servers(); + } + + for (auto& result : results) { + id_used[result.first.locality.processId()]++; + } + + TraceEvent("GetTLogTeamDone") + .detail("Completed", bCompleted).detail("Policy", conf.tLogPolicy->info()) + .detail("Results", results.size()).detail("Processes", logServerSet->size()) + .detail("Workers", id_worker.size()) + .detail("Replication", conf.tLogReplicationFactor) + .detail("Desired", conf.getDesiredLogs()) + .detail("RatingTests",SERVER_KNOBS->POLICY_RATING_TESTS) + .detail("PolicyGenerations",SERVER_KNOBS->POLICY_GENERATIONS); + + // Free the set + logServerSet->clear(); + logServerSet.clear(); + + return results; + } + + struct WorkerFitnessInfo { + std::pair worker; + ProcessClass::Fitness fitness; + int used; + + WorkerFitnessInfo(std::pair worker, ProcessClass::Fitness fitness, int used) : worker(worker), fitness(fitness), used(used) {} + }; + + WorkerFitnessInfo getWorkerForRoleInDatacenter(Optional> const& dcId, ProcessClass::ClusterRole role, DatabaseConfiguration const& conf, std::map< Optional>, int>& id_used, bool checkStable = false ) { + std::map, vector>> fitness_workers; + + for( auto& it : id_worker ) { + auto fitness = it.second.processClass.machineClassFitness( role ); + if( workerAvailable(it.second, checkStable) && !conf.isExcludedServer(it.second.interf.address()) && fitness != ProcessClass::NeverAssign && it.second.interf.locality.dcId()==dcId ) { + fitness_workers[ std::make_pair(fitness, id_used[it.first]) ].push_back(std::make_pair(it.second.interf, it.second.processClass)); + } + } + + for( auto& it : fitness_workers ) { + auto& w = it.second; + g_random->randomShuffle(w); + for( int i=0; i < w.size(); i++ ) { + id_used[w[i].first.locality.processId()]++; + return WorkerFitnessInfo(w[i], it.first.first, it.first.second); + } + } + + //If we did not find enough workers in the primary data center, add workers from other data centers + fitness_workers.clear(); + for( auto& it : id_worker ) { + auto fitness = it.second.processClass.machineClassFitness( role ); + if( workerAvailable(it.second, checkStable) && !conf.isExcludedServer(it.second.interf.address()) && fitness != ProcessClass::NeverAssign && it.second.interf.locality.dcId()!=dcId ) { + fitness_workers[ std::make_pair(fitness, id_used[it.first]) ].push_back(std::make_pair(it.second.interf, it.second.processClass)); + } + } + + for( auto& it : fitness_workers ) { + auto& w = it.second; + g_random->randomShuffle(w); + for( int i=0; i < w.size(); i++ ) { + id_used[w[i].first.locality.processId()]++; + return WorkerFitnessInfo(w[i], it.first.first, it.first.second); + } + } + + throw no_more_servers(); + } + + vector> getWorkersForRoleInDatacenter(Optional> const& dcId, ProcessClass::ClusterRole role, int amount, DatabaseConfiguration const& conf, std::map< Optional>, int>& id_used, WorkerFitnessInfo minWorker, bool checkStable = false ) { + std::map, vector>> fitness_workers; + vector> results; + if (amount <= 0) + return results; + + for( auto& it : id_worker ) { + auto fitness = it.second.processClass.machineClassFitness( role ); + if( workerAvailable(it.second, checkStable) && !conf.isExcludedServer(it.second.interf.address()) && it.second.interf.id() != minWorker.worker.first.id() && (fitness < minWorker.fitness || (fitness == minWorker.fitness && id_used[it.first] <= minWorker.used)) && it.second.interf.locality.dcId()==dcId ) { + fitness_workers[ std::make_pair(fitness, id_used[it.first]) ].push_back(std::make_pair(it.second.interf, it.second.processClass)); + } + } + + for( auto& it : fitness_workers ) { + auto& w = it.second; + g_random->randomShuffle(w); + for( int i=0; i < w.size(); i++ ) { + results.push_back(w[i]); + id_used[w[i].first.locality.processId()]++; + if( results.size() == amount ) + return results; + } + } + + return results; + } + + struct InDatacenterFitness { + ProcessClass::Fitness proxyFit; + ProcessClass::Fitness resolverFit; + int proxyCount; + int resolverCount; + int datacenters; + + InDatacenterFitness( ProcessClass::Fitness proxyFit, ProcessClass::Fitness resolverFit, int proxyCount, int resolverCount, int datacenters) + : proxyFit(proxyFit), resolverFit(resolverFit), proxyCount(proxyCount), resolverCount(resolverCount), datacenters(datacenters) {} + + InDatacenterFitness() : proxyFit( ProcessClass::NeverAssign ), resolverFit( ProcessClass::NeverAssign ), datacenters(10000000) {} + + InDatacenterFitness( vector> proxies, vector> resolvers ) { + std::set>> dcs; + proxyFit = ProcessClass::BestFit; + resolverFit = ProcessClass::BestFit; + for(auto it: proxies) { + dcs.insert(it.first.locality.dcId()); + proxyFit = std::max(proxyFit, it.second.machineClassFitness( ProcessClass::Proxy )); + } + for(auto it: resolvers) { + dcs.insert(it.first.locality.dcId()); + resolverFit = std::max(resolverFit, it.second.machineClassFitness( ProcessClass::Resolver )); + } + proxyCount = proxies.size(); + resolverCount = resolvers.size(); + datacenters = dcs.size(); + } + + InDatacenterFitness( vector proxies, vector resolvers, vector proxyClasses, vector resolverClasses ) { + std::set>> dcs; + proxyFit = ProcessClass::BestFit; + resolverFit = ProcessClass::BestFit; + for(int i = 0; i < proxies.size(); i++) { + dcs.insert(proxies[i].locality.dcId()); + proxyFit = std::max(proxyFit, proxyClasses[i].machineClassFitness( ProcessClass::Proxy )); + } + for(int i = 0; i < resolvers.size(); i++) { + dcs.insert(resolvers[i].locality.dcId()); + resolverFit = std::max(resolverFit, resolverClasses[i].machineClassFitness( ProcessClass::Resolver )); + } + + proxyCount = proxies.size(); + resolverCount = resolvers.size(); + datacenters = dcs.size(); + } + + bool operator < (InDatacenterFitness const& r) const { + if(datacenters != r.datacenters) return datacenters < r.datacenters; + + int lmax = std::max(resolverFit,proxyFit); + int lmin = std::min(resolverFit,proxyFit); + int rmax = std::max(r.resolverFit,r.proxyFit); + int rmin = std::min(r.resolverFit,r.proxyFit); + + if( lmax != rmax ) return lmax < rmax; + if( lmin != rmin ) return lmin < rmin; + if(proxyCount != r.proxyCount) return proxyCount > r.proxyCount; + return resolverCount > r.resolverCount; + } + + bool operator == (InDatacenterFitness const& r) const { return proxyFit == r.proxyFit && resolverFit == r.resolverFit && datacenters == r.datacenters && proxyCount == r.proxyCount && resolverCount == r.resolverCount; } + }; + + struct AcrossDatacenterFitness { + ProcessClass::Fitness tlogFit; + int tlogCount; + int datacenters; + + AcrossDatacenterFitness( ProcessClass::Fitness tlogFit, int tlogCount, int datacenters ) : tlogFit(tlogFit), tlogCount(tlogCount), datacenters(datacenters) {} + + AcrossDatacenterFitness() : tlogFit( ProcessClass::NeverAssign ), datacenters(0), tlogCount(0) {} + + AcrossDatacenterFitness( vector> tlogs ) { + std::set>> dcs; + tlogFit = ProcessClass::BestFit; + for(auto it : tlogs) { + dcs.insert(it.first.locality.dcId()); + tlogFit = std::max(tlogFit, it.second.machineClassFitness( ProcessClass::TLog )); + } + datacenters = dcs.size(); + tlogCount = tlogs.size(); + } + + AcrossDatacenterFitness( vector> tlogs, std::vector processClasses ) { + std::set>> dcs; + tlogFit = ProcessClass::BestFit; + for(int i = 0; i < tlogs.size(); i++) { + ASSERT(tlogs[i].present()); + dcs.insert(tlogs[i].interf().locality.dcId()); + tlogFit = std::max(tlogFit, processClasses[i].machineClassFitness( ProcessClass::TLog )); + } + datacenters = dcs.size(); + tlogCount = tlogs.size(); + } + + bool operator < (AcrossDatacenterFitness const& r) const { + if(tlogFit != r.tlogFit) return tlogFit < r.tlogFit; + if(tlogCount != r.tlogCount) return tlogCount > r.tlogCount; + return datacenters > r.datacenters; + } + + bool operator == (AcrossDatacenterFitness const& r) const { return datacenters == r.datacenters && tlogFit == r.tlogFit && tlogCount == r.tlogCount; } + }; + + std::set>> getDatacenters( DatabaseConfiguration const& conf, bool checkStable = false ) { + std::set>> result; + for( auto& it : id_worker ) + if( workerAvailable( it.second, checkStable ) && !conf.isExcludedServer( it.second.interf.address() ) ) + result.insert(it.second.interf.locality.dcId()); + return result; + } + + RecruitFromConfigurationReply findWorkersForConfiguration( RecruitFromConfigurationRequest const& req ) { + RecruitFromConfigurationReply result; + std::map< Optional>, int> id_used; + + id_used[masterProcessId]++; + auto tlogs = getWorkersForTlogsAcrossDatacenters( req.configuration, id_used ); + for(int i = 0; i < tlogs.size(); i++) + result.tLogs.push_back(tlogs[i].first); + + auto datacenters = getDatacenters( req.configuration ); + + InDatacenterFitness bestFitness; + int numEquivalent = 1; + + for(auto dcId : datacenters ) { + auto used = id_used; + auto first_resolver = getWorkerForRoleInDatacenter( dcId, ProcessClass::Resolver, req.configuration, used ); + auto first_proxy = getWorkerForRoleInDatacenter( dcId, ProcessClass::Proxy, req.configuration, used ); + + auto proxies = getWorkersForRoleInDatacenter( dcId, ProcessClass::Proxy, req.configuration.getDesiredProxies()-1, req.configuration, used, first_proxy ); + auto resolvers = getWorkersForRoleInDatacenter( dcId, ProcessClass::Resolver, req.configuration.getDesiredResolvers()-1, req.configuration, used, first_resolver ); + + proxies.push_back(first_proxy.worker); + resolvers.push_back(first_resolver.worker); + + auto fitness = InDatacenterFitness(proxies, resolvers); + if(fitness < bestFitness) { + bestFitness = fitness; + numEquivalent = 1; + result.resolvers = vector(); + result.proxies = vector(); + for(int i = 0; i < resolvers.size(); i++) + result.resolvers.push_back(resolvers[i].first); + for(int i = 0; i < proxies.size(); i++) + result.proxies.push_back(proxies[i].first); + } else if( fitness == bestFitness && g_random->random01() < 1.0/++numEquivalent ) { + result.resolvers = vector(); + result.proxies = vector(); + for(int i = 0; i < resolvers.size(); i++) + result.resolvers.push_back(resolvers[i].first); + for(int i = 0; i < proxies.size(); i++) + result.proxies.push_back(proxies[i].first); + } + } + + ASSERT(bestFitness != InDatacenterFitness()); + + TraceEvent("findWorkersForConfig").detail("replication", req.configuration.tLogReplicationFactor) + .detail("desiredLogs", req.configuration.getDesiredLogs()).detail("actualLogs", result.tLogs.size()) + .detail("desiredProxies", req.configuration.getDesiredProxies()).detail("actualProxies", result.proxies.size()) + .detail("desiredResolvers", req.configuration.getDesiredResolvers()).detail("actualResolvers", result.resolvers.size()); + + if( now() - startTime < SERVER_KNOBS->WAIT_FOR_GOOD_RECRUITMENT_DELAY && + ( AcrossDatacenterFitness(tlogs) > AcrossDatacenterFitness((ProcessClass::Fitness)SERVER_KNOBS->EXPECTED_TLOG_FITNESS, req.configuration.getDesiredLogs(), req.configuration.minDataCenters) || + bestFitness > InDatacenterFitness((ProcessClass::Fitness)SERVER_KNOBS->EXPECTED_PROXY_FITNESS, (ProcessClass::Fitness)SERVER_KNOBS->EXPECTED_RESOLVER_FITNESS, req.configuration.getDesiredProxies(), req.configuration.getDesiredResolvers(), 1) ) ) { + throw operation_failed(); + } + + return result; + } + + bool betterMasterExists() { + ServerDBInfo dbi = db.serverInfo->get(); + std::map< Optional>, int> id_used; + + auto masterWorker = id_worker.find(dbi.master.locality.processId()); + if(masterWorker == id_worker.end()) + return false; + + id_used[masterProcessId]++; + + ProcessClass::Fitness oldMasterFit = masterWorker->second.processClass.machineClassFitness( ProcessClass::Master ); + ProcessClass::Fitness newMasterFit = getMasterWorker(true).second.machineClassFitness( ProcessClass::Master ); + + if(dbi.recoveryState < RecoveryState::FULLY_RECOVERED) { + if(oldMasterFit > newMasterFit) { + TEST(true); //Better master exists triggered before full recovery + TraceEvent("BetterMasterExists", id).detail("oldMasterFit", oldMasterFit).detail("newMasterFit", newMasterFit); + return true; + } + return false; + } + + if(oldMasterFit < newMasterFit) return false; + + std::vector tlogProcessClasses; + for(auto& it : dbi.logSystemConfig.tLogs ) { + auto tlogWorker = id_worker.find(it.interf().locality.processId()); + if ( tlogWorker == id_worker.end() ) + return false; + tlogProcessClasses.push_back(tlogWorker->second.processClass); + } + AcrossDatacenterFitness oldAcrossFit(dbi.logSystemConfig.tLogs, tlogProcessClasses); + AcrossDatacenterFitness newAcrossFit(getWorkersForTlogsAcrossDatacenters(db.config, id_used, true)); + + if(oldAcrossFit < newAcrossFit) return false; + + + std::vector proxyClasses; + for(auto& it : dbi.client.proxies ) { + auto proxyWorker = id_worker.find(it.locality.processId()); + if ( proxyWorker == id_worker.end() ) + return false; + proxyClasses.push_back(proxyWorker->second.processClass); + } + + std::vector resolverClasses; + for(auto& it : dbi.resolvers ) { + auto resolverWorker = id_worker.find(it.locality.processId()); + if ( resolverWorker == id_worker.end() ) + return false; + resolverClasses.push_back(resolverWorker->second.processClass); + } + + InDatacenterFitness oldInFit(dbi.client.proxies, dbi.resolvers, proxyClasses, resolverClasses); + + auto datacenters = getDatacenters( db.config, true ); + InDatacenterFitness newInFit; + + for(auto dcId : datacenters) { + auto used = id_used; + auto first_resolver = getWorkerForRoleInDatacenter( dcId, ProcessClass::Resolver, db.config, used ); + auto first_proxy = getWorkerForRoleInDatacenter( dcId, ProcessClass::Proxy, db.config, used ); + + auto proxies = getWorkersForRoleInDatacenter( dcId, ProcessClass::Proxy, db.config.getDesiredProxies()-1, db.config, used, first_proxy, true ); + auto resolvers = getWorkersForRoleInDatacenter( dcId, ProcessClass::Resolver, db.config.getDesiredResolvers()-1, db.config, used, first_resolver, true ); + proxies.push_back(first_proxy.worker); + resolvers.push_back(first_resolver.worker); + + auto fitness = InDatacenterFitness(proxies, resolvers); + if(fitness < newInFit) + newInFit = fitness; + } + + if(oldInFit < newInFit) return false; + if(oldMasterFit > newMasterFit || oldAcrossFit > newAcrossFit || oldInFit > newInFit) { + TraceEvent("BetterMasterExists", id).detail("oldMasterFit", oldMasterFit).detail("newMasterFit", newMasterFit) + .detail("oldAcrossFitD", oldAcrossFit.datacenters).detail("newAcrossFitD", newAcrossFit.datacenters) + .detail("oldAcrossFitC", oldAcrossFit.tlogCount).detail("newAcrossFitC", newAcrossFit.tlogCount) + .detail("oldAcrossFitT", oldAcrossFit.tlogFit).detail("newAcrossFitT", newAcrossFit.tlogFit) + .detail("oldInFitD", oldInFit.datacenters).detail("newInFitD", newInFit.datacenters) + .detail("oldInFitP", oldInFit.proxyFit).detail("newInFitP", newInFit.proxyFit) + .detail("oldInFitR", oldInFit.resolverFit).detail("newInFitR", newInFit.resolverFit) + .detail("oldInFitPC", oldInFit.proxyCount).detail("newInFitPC", newInFit.proxyCount) + .detail("oldInFitRC", oldInFit.resolverCount).detail("newInFitRC", newInFit.resolverCount); + return true; + } + return false; + } + + std::map< Optional>, WorkerInfo > id_worker; + std::map< Optional>, ProcessClass > id_class; //contains the mapping from process id to process class from the database + Standalone lastProcessClasses; + bool gotProcessClasses; + Optional> masterProcessId; + UID id; + std::vector outstandingRecruitmentRequests; + std::vector> outstandingStorageRequests; + ActorCollection ac; + UpdateWorkerList updateWorkerList; + Future betterMasterExistsChecker; + + DBInfo db; + Database cx; + double startTime; + + explicit ClusterControllerData( ClusterControllerFullInterface ccInterface ) + : id(ccInterface.id()), ac(false), betterMasterExistsChecker(Void()), gotProcessClasses(false), startTime(now()) + { + auto serverInfo = db.serverInfo->get(); + serverInfo.id = g_random->randomUniqueID(); + serverInfo.masterLifetime.ccID = id; + serverInfo.clusterInterface = ccInterface; + db.serverInfo->set( serverInfo ); + cx = openDBOnServer(db.serverInfo, TaskDefaultEndpoint, true, true); + } + + ~ClusterControllerData() { + ac.clear(false); + id_worker.clear(); + } +}; + +template +vector values( std::map const& map ) { + vector t; + for(auto i = map.begin(); i!=map.end(); ++i) + t.push_back(i->second); + return t; +} + +ACTOR Future clusterWatchDatabase( ClusterControllerData* cluster, ClusterControllerData::DBInfo* db ) +{ + state MasterInterface iMaster; + + // SOMEDAY: If there is already a non-failed master referenced by zkMasterInfo, use that one until it fails + // When this someday is implemented, make sure forced failures still cause the master to be recruited again + + loop { + TraceEvent("CCWDB", cluster->id); + try { + state double recoveryStart = now(); + TraceEvent("CCWDB", cluster->id).detail("Recruiting", "Master"); + state std::pair masterWorker = cluster->getMasterWorker(); + if( masterWorker.second.machineClassFitness( ProcessClass::Master ) > SERVER_KNOBS->EXPECTED_MASTER_FITNESS && now() - cluster->startTime < SERVER_KNOBS->WAIT_FOR_GOOD_RECRUITMENT_DELAY ) { + TraceEvent("CCWDB", cluster->id).detail("Fitness", masterWorker.second.machineClassFitness( ProcessClass::Master )); + Void _ = wait( delay(SERVER_KNOBS->ATTEMPT_RECRUITMENT_DELAY) ); + continue; + } + RecruitMasterRequest rmq; + rmq.lifetime = db->serverInfo->get().masterLifetime; + + cluster->masterProcessId = masterWorker.first.locality.processId(); + ErrorOr newMaster = wait( masterWorker.first.master.tryGetReply( rmq ) ); + if (newMaster.present()) { + TraceEvent("CCWDB", cluster->id).detail("Recruited", newMaster.get().id()); + + // for status tool + TraceEvent("RecruitedMasterWorker", cluster->id) + .detail("Address", newMaster.get().address()) + .trackLatest("DB/RecruitedMasterWorker"); + + iMaster = newMaster.get(); + + db->masterRegistrationCount = 0; + db->config = DatabaseConfiguration(); + db->forceMasterFailure = Promise(); + + auto dbInfo = ServerDBInfo( LiteralStringRef("DB") ); + dbInfo.master = iMaster; + dbInfo.id = g_random->randomUniqueID(); + dbInfo.masterLifetime = db->serverInfo->get().masterLifetime; + ++dbInfo.masterLifetime; + dbInfo.clusterInterface = db->serverInfo->get().clusterInterface; + + TraceEvent("CCWDB", cluster->id).detail("Lifetime", dbInfo.masterLifetime.toString()).detail("ChangeID", dbInfo.id); + db->serverInfo->set( dbInfo ); + + Void _ = wait( delay(SERVER_KNOBS->MASTER_SPIN_DELAY) ); // Don't retry master recovery more than once per second, but don't delay the "first" recovery after more than a second of normal operation + + TraceEvent("CCWDB", cluster->id).detail("Watching", iMaster.id()); + + // Master failure detection is pretty sensitive, but if we are in the middle of a very long recovery we really don't want to have to start over + loop choose { + when (Void _ = wait( waitFailureClient( iMaster.waitFailure, db->masterRegistrationCount ? + SERVER_KNOBS->MASTER_FAILURE_REACTION_TIME : (now() - recoveryStart) * SERVER_KNOBS->MASTER_FAILURE_SLOPE_DURING_RECOVERY, + db->masterRegistrationCount ? -SERVER_KNOBS->MASTER_FAILURE_REACTION_TIME/SERVER_KNOBS->SECONDS_BEFORE_NO_FAILURE_DELAY : SERVER_KNOBS->MASTER_FAILURE_SLOPE_DURING_RECOVERY ) || db->forceMasterFailure.getFuture() )) { break; } + when (Void _ = wait( db->serverInfo->onChange() )) {} + } + + TEST(true); // clusterWatchDatabase() master failed + TraceEvent(SevWarn,"DetectedFailedMaster", cluster->id).detail("OldMaster", iMaster.id()); + } else { + TEST(true); //clusterWatchDatabas() !newMaster.present() + Void _ = wait( delay(SERVER_KNOBS->MASTER_SPIN_DELAY) ); + } + } catch (Error& e) { + TraceEvent("CCWDB", cluster->id).error(e, true).detail("Master", iMaster.id()); + if (e.code() == error_code_actor_cancelled) throw; + + bool ok = e.code() == error_code_no_more_servers; + TraceEvent(ok ? SevWarn : SevError,"clusterWatchDatabaseRetrying", cluster->id).error(e); + if (!ok) + throw e; + Void _ = wait( delay(SERVER_KNOBS->ATTEMPT_RECRUITMENT_DELAY) ); + } + } +} + +void addIssue( ProcessIssuesMap& issueMap, NetworkAddress const& addr, std::string const& issue, UID& issueID ) { + auto& e = issueMap[addr]; + e.first = issue; + e.second = issueID = g_random->randomUniqueID(); + if (!issue.size()) issueMap.erase(addr); +} + +void removeIssue( ProcessIssuesMap& issueMap, NetworkAddress const& addr, std::string const& issue, UID& issueID ) { + if (!issue.size()) return; + if ( issueMap.count(addr) && issueMap[addr].second == issueID ) + issueMap.erase( addr ); +} + +ACTOR Future clusterGetServerInfo( + ClusterControllerData::DBInfo* db, + UID knownServerInfoID, + std::string issues, + std::vector incompatiblePeers, + ReplyPromise reply) +{ + state UID issueID; + addIssue( db->workersWithIssues, reply.getEndpoint().address, issues, issueID ); + for(auto it : incompatiblePeers) { + db->incompatibleConnections[it] = now() + SERVER_KNOBS->INCOMPATIBLE_PEERS_LOGGING_INTERVAL; + } + + while (db->serverInfo->get().id == knownServerInfoID) { + choose { + when (Void _ = wait( db->serverInfo->onChange() )) {} + when (Void _ = wait( delayJittered( 300 ) )) { break; } // The server might be long gone! + } + } + + removeIssue( db->workersWithIssues, reply.getEndpoint().address, issues, issueID ); + + TraceEvent("SendingServerInfo").detail("Dest", reply.getEndpoint().address ); + reply.send( db->serverInfo->get() ); + return Void(); +} + +ACTOR Future clusterOpenDatabase( + ClusterControllerData::DBInfo* db, + Standalone dbName, + UID knownClientInfoID, + std::string issues, + Standalone> supportedVersions, + ReplyPromise reply) +{ + // NOTE: The client no longer expects this function to return errors + state UID issueID; + addIssue( db->clientsWithIssues, reply.getEndpoint().address, issues, issueID ); + + if(supportedVersions.size() > 0) { + db->clientVersionMap[reply.getEndpoint().address] = supportedVersions; + } + + while (db->clientInfo->get().id == knownClientInfoID) { + choose { + when (Void _ = wait( db->clientInfo->onChange() )) {} + when (Void _ = wait( delayJittered( 300 ) )) { break; } // The client might be long gone! + } + } + + removeIssue( db->clientsWithIssues, reply.getEndpoint().address, issues, issueID ); + db->clientVersionMap.erase(reply.getEndpoint().address); + + reply.send( db->clientInfo->get() ); + return Void(); +} + +void checkOutstandingRecruitmentRequests( ClusterControllerData* self ) { + for( int i = 0; i < self->outstandingRecruitmentRequests.size(); i++ ) { + RecruitFromConfigurationRequest& req = self->outstandingRecruitmentRequests[i]; + try { + req.reply.send( self->findWorkersForConfiguration( req ) ); + std::swap( self->outstandingRecruitmentRequests[i--], self->outstandingRecruitmentRequests.back() ); + self->outstandingRecruitmentRequests.pop_back(); + } catch (Error& e) { + if (e.code() == error_code_no_more_servers || e.code() == error_code_operation_failed) { + TraceEvent(SevWarn, "RecruitTLogMatchingSetNotAvailable", self->id).error(e); + } else { + TraceEvent(SevError, "RecruitTLogsRequestError", self->id).error(e); + throw; + } + } + } +} + +void checkOutstandingStorageRequests( ClusterControllerData* self ) { + for( int i = 0; i < self->outstandingStorageRequests.size(); i++ ) { + auto& req = self->outstandingStorageRequests[i]; + try { + if(req.second < now()) { + req.first.reply.sendError(timed_out()); + std::swap( self->outstandingStorageRequests[i--], self->outstandingStorageRequests.back() ); + self->outstandingStorageRequests.pop_back(); + } else { + if(!self->gotProcessClasses && !req.first.criticalRecruitment) + throw no_more_servers(); + + auto worker = self->getStorageWorker(req.first); + RecruitStorageReply rep; + rep.worker = worker.first; + rep.processClass = worker.second; + req.first.reply.send( rep ); + std::swap( self->outstandingStorageRequests[i--], self->outstandingStorageRequests.back() ); + self->outstandingStorageRequests.pop_back(); + } + } catch (Error& e) { + if (e.code() == error_code_no_more_servers) { + TraceEvent(SevWarn, "RecruitStorageNotAvailable", self->id).error(e); + } else { + TraceEvent(SevError, "RecruitStorageError", self->id).error(e); + throw; + } + } + } +} + +ACTOR Future doCheckOutstandingMasterRequests( ClusterControllerData* self ) { + Void _ = wait( delay(SERVER_KNOBS->CHECK_BETTER_MASTER_INTERVAL) ); + if (self->betterMasterExists()) { + if (!self->db.forceMasterFailure.isSet()) { + self->db.forceMasterFailure.send( Void() ); + TraceEvent("MasterRegistrationKill", self->id).detail("MasterId", self->db.serverInfo->get().master.id()); + } + } + return Void(); +} + +void checkOutstandingMasterRequests( ClusterControllerData* self ) { + if( !self->betterMasterExistsChecker.isReady() ) + return; + + self->betterMasterExistsChecker = doCheckOutstandingMasterRequests(self); +} + +void checkOutstandingRequests( ClusterControllerData* self ) { + checkOutstandingRecruitmentRequests( self ); + checkOutstandingStorageRequests( self ); + checkOutstandingMasterRequests( self ); +} + +ACTOR Future rebootAndCheck( ClusterControllerData* cluster, Optional> processID ) { + auto watcher = cluster->id_worker.find(processID); + ASSERT(watcher != cluster->id_worker.end()); + + watcher->second.reboots++; + Void _ = wait( delay( g_network->isSimulated() ? SERVER_KNOBS->SIM_SHUTDOWN_TIMEOUT : SERVER_KNOBS->SHUTDOWN_TIMEOUT ) ); + + auto watcher = cluster->id_worker.find(processID); + if(watcher != cluster->id_worker.end()) { + watcher->second.reboots--; + if( watcher->second.reboots < 2 ) + checkOutstandingMasterRequests( cluster ); + } + + return Void(); +} + +ACTOR Future workerAvailabilityWatch( WorkerInterface worker, ProcessClass startingClass, ClusterControllerData* cluster ) { + state Future failed = waitFailureClient( worker.waitFailure, SERVER_KNOBS->WORKER_FAILURE_TIME ); + cluster->updateWorkerList.set( worker.locality.processId(), ProcessData(worker.locality, startingClass, worker.address()) ); + loop { + choose { + when( Void _ = wait( IFailureMonitor::failureMonitor().onStateEqual( worker.storage.getEndpoint(), FailureStatus(IFailureMonitor::failureMonitor().getState( worker.storage.getEndpoint() ).isAvailable()) ) ) ) { + if( IFailureMonitor::failureMonitor().getState( worker.storage.getEndpoint() ).isAvailable() ) { + cluster->ac.add( rebootAndCheck( cluster, worker.locality.processId() ) ); + checkOutstandingRequests( cluster ); + } + } + when( Void _ = wait( failed ) ) { // remove workers that have failed + cluster->id_worker[ worker.locality.processId() ].reply.send( Void() ); + cluster->id_worker.erase( worker.locality.processId() ); + + cluster->updateWorkerList.set( worker.locality.processId(), Optional() ); + return Void(); + } + } + } +} + +struct FailureStatusInfo { + FailureStatus status; + double lastRequestTime; + double penultimateRequestTime; + + FailureStatusInfo() : lastRequestTime(0), penultimateRequestTime(0) {} + + void insertRequest(double now) { + penultimateRequestTime = lastRequestTime; + lastRequestTime = now; + } + + double latency(double now) const { + return std::max( now - lastRequestTime, lastRequestTime - penultimateRequestTime ); + } +}; + +//The failure monitor client relies on the fact that the failure detection server will not declare itself failed +ACTOR Future failureDetectionServer( UID uniqueID, FutureStream< FailureMonitoringRequest > requests ) { + state Version currentVersion = 0; + state std::map currentStatus; // The status at currentVersion + state std::deque statusHistory; // The last change in statusHistory is from currentVersion-1 to currentVersion + state Future periodically = Void(); + state double lastT = 0; + state double clientRequestInterval = FLOW_KNOBS->CLIENT_REQUEST_INTERVAL; + + loop choose { + when ( FailureMonitoringRequest req = waitNext( requests ) ) { + if ( req.senderStatus.present() ) { + // Update the status of requester, if necessary + auto& address = req.reply.getEndpoint().address; + auto& stat = currentStatus[ address ]; + auto& newStat = req.senderStatus.get(); + + ASSERT( !newStat.failed || address != g_network->getLocalAddress() ); + + stat.insertRequest(now()); + if (req.senderStatus != stat.status) { + TraceEvent("FailureDetectionStatus", uniqueID).detail("System", address).detail("Status", newStat.failed ? "Failed" : "OK").detail("Why", "Request"); + statusHistory.push_back( SystemFailureStatus( address, newStat ) ); + ++currentVersion; + + if (req.senderStatus == FailureStatus()){ + // failureMonitorClient reports explicitly that it is failed + ASSERT(false); // This can't happen at the moment; if that changes, make this a TEST instead + currentStatus.erase(address); + } else { + TEST(true); + stat.status = newStat; + } + + while (statusHistory.size() > currentStatus.size()) + statusHistory.pop_front(); + } + } + + // Return delta-compressed status changes to requester + Version reqVersion = req.failureInformationVersion; + if (reqVersion > currentVersion){ + req.reply.sendError( future_version() ); + ASSERT(false); + } else { + TEST(true); // failureDetectionServer sending failure data to requester + FailureMonitoringReply reply; + reply.failureInformationVersion = currentVersion; + reply.clientRequestIntervalMS = clientRequestInterval * 1000; + reply.considerServerFailedTimeoutMS = CLIENT_KNOBS->FAILURE_TIMEOUT_DELAY * 1000; + + ASSERT( currentVersion >= (int64_t)statusHistory.size()); + + if (reqVersion < currentVersion - (int64_t)statusHistory.size() || reqVersion == 0) { + // Send everything + TEST(true); // failureDetectionServer sending all current data to requester + reply.allOthersFailed = true; + for(auto it = currentStatus.begin(); it != currentStatus.end(); ++it) + reply.changes.push_back( reply.arena, SystemFailureStatus( it->first, it->second.status ) ); + } else { + TEST(true); // failureDetectionServer sending delta-compressed data to requester + // SOMEDAY: Send only the last change for a given address? + reply.allOthersFailed = false; + for(int v = reqVersion - currentVersion + statusHistory.size(); v < statusHistory.size(); v++) { + reply.changes.push_back( reply.arena, statusHistory[v] ); + } + } + req.reply.send( reply ); + } + } + when ( Void _ = wait( periodically ) ) { + periodically = delay( FLOW_KNOBS->CLIENT_REQUEST_INTERVAL ); + double t = now(); + if (lastT != 0 && t - lastT > 1) + TraceEvent("LongDelayOnClusterController").detail("Duration", t - lastT); + lastT = t; + + // Adapt to global unresponsiveness + vector delays; + for(auto it=currentStatus.begin(); it!=currentStatus.end(); it++) + if (it->second.penultimateRequestTime) { + delays.push_back(it->second.latency(t)); + TraceEvent("FDData", uniqueID).detail("S", it->first.toString()).detail("L", it->second.latency(t)); + } + int pivot = std::max(0, (int)delays.size()-2); + double pivotDelay = 0; + if (delays.size()) { + std::nth_element(delays.begin(), delays.begin()+pivot, delays.end()); + pivotDelay = *(delays.begin()+pivot); + } + pivotDelay = std::max(0.0, pivotDelay - clientRequestInterval); + + TraceEvent("FailureDetectionPoll", uniqueID).detail("PivotDelay", pivotDelay).detail("Clients", currentStatus.size()); + //TraceEvent("FailureDetectionAcceptableDelay").detail("ms", acceptableDelay*1000); + + for(auto it = currentStatus.begin(); it != currentStatus.end(); ) { + double delay = t - it->second.lastRequestTime; + + if ( it->first != g_network->getLocalAddress() && ( delay > pivotDelay * 2 + clientRequestInterval + CLIENT_KNOBS->FAILURE_MIN_DELAY || delay > CLIENT_KNOBS->FAILURE_MAX_DELAY ) ) { + //printf("Failure Detection Server: Status of '%s' is now '%s' after %f sec\n", it->first.toString().c_str(), "Failed", now() - it->second.lastRequestTime); + TraceEvent("FailureDetectionStatus", uniqueID).detail("System", it->first).detail("Status","Failed").detail("Why", "Timeout").detail("LastRequestAge", delay) + .detail("PivotDelay", pivotDelay); + statusHistory.push_back( SystemFailureStatus( it->first, FailureStatus(true) ) ); + ++currentVersion; + it = currentStatus.erase(it); + while (statusHistory.size() > currentStatus.size()) + statusHistory.pop_front(); + } else { + ++it; + } + } + } + } +} + +ACTOR Future> requireAll( vector>>> in ) { + state vector out; + state int i; + for(i=0; i> x = wait(in[i]); + if (!x.present()) throw recruitment_failed(); + out.insert(out.end(), x.get().begin(), x.get().end()); + } + return out; +} + +void clusterRecruitStorage( ClusterControllerData* self, RecruitStorageRequest req ) { + try { + if(!self->gotProcessClasses && !req.criticalRecruitment) + throw no_more_servers(); + auto worker = self->getStorageWorker(req); + RecruitStorageReply rep; + rep.worker = worker.first; + rep.processClass = worker.second; + req.reply.send( rep ); + } catch ( Error& e ) { + if (e.code() == error_code_no_more_servers) { + self->outstandingStorageRequests.push_back( std::make_pair(req, now() + SERVER_KNOBS->RECRUITMENT_TIMEOUT) ); + TraceEvent(SevWarn, "RecruitStorageNotAvailable", self->id).error(e); + } else { + TraceEvent(SevError, "RecruitStorageError", self->id).error(e); + throw; // Any other error will bring down the cluster controller + } + } +} + +ACTOR Future clusterRecruitFromConfiguration( ClusterControllerData* self, RecruitFromConfigurationRequest req ) { + // At the moment this doesn't really need to be an actor (it always completes immediately) + TEST(true); //ClusterController RecruitTLogsRequest + loop { + try { + req.reply.send( self->findWorkersForConfiguration( req ) ); + return Void(); + } catch (Error& e) { + if (e.code() == error_code_no_more_servers && now() - self->startTime >= SERVER_KNOBS->WAIT_FOR_GOOD_RECRUITMENT_DELAY) { + self->outstandingRecruitmentRequests.push_back( req ); + TraceEvent(SevWarn, "RecruitFromConfigurationNotAvailable", self->id).error(e); + return Void(); + } else if(e.code() == error_code_operation_failed || e.code() == error_code_no_more_servers) { + //recruitment not good enough, try again + } + else { + TraceEvent(SevError, "RecruitFromConfigurationError", self->id).error(e); + throw; // goodbye, cluster controller + } + } + Void _ = wait( delay(SERVER_KNOBS->ATTEMPT_RECRUITMENT_DELAY) ); + } +} + +void clusterRegisterMaster( ClusterControllerData* self, RegisterMasterRequest const& req ) { + req.reply.send( Void() ); + + TraceEvent("MasterRegistrationReceived", self->id).detail("dbName", printable(req.dbName)).detail("MasterId", req.id).detail("Master", req.mi.toString()).detail("Tlogs", describe(req.logSystemConfig.tLogs)).detail("Resolvers", req.resolvers.size()) + .detail("RecoveryState", req.recoveryState).detail("RegistrationCount", req.registrationCount).detail("Proxies", req.proxies.size()).detail("RecoveryCount", req.recoveryCount); + + //make sure the request comes from an active database + auto db = &self->db; + if ( db->serverInfo->get().master.id() != req.id || req.registrationCount <= db->masterRegistrationCount ) { + TraceEvent("MasterRegistrationNotFound", self->id).detail("dbName", printable(req.dbName)).detail("MasterId", req.id).detail("existingId", db->serverInfo->get().master.id()).detail("RegCount", req.registrationCount).detail("ExistingRegCount", db->masterRegistrationCount); + return; + } + + db->masterRegistrationCount = req.registrationCount; + db->config = req.configuration; + + bool isChanged = false; + auto dbInfo = self->db.serverInfo->get(); + + if (dbInfo.recoveryState != req.recoveryState) { + dbInfo.recoveryState = req.recoveryState; + isChanged = true; + } + + if (dbInfo.priorCommittedLogServers != req.priorCommittedLogServers) { + dbInfo.priorCommittedLogServers = req.priorCommittedLogServers; + isChanged = true; + } + + // Construct the client information + if (db->clientInfo->get().proxies != req.proxies) { + isChanged = true; + ClientDBInfo clientInfo; + clientInfo.id = g_random->randomUniqueID(); + clientInfo.proxies = req.proxies; + clientInfo.clientTxnInfoSampleRate = db->clientInfo->get().clientTxnInfoSampleRate; + clientInfo.clientTxnInfoSizeLimit = db->clientInfo->get().clientTxnInfoSizeLimit; + db->clientInfo->set( clientInfo ); + dbInfo.client = db->clientInfo->get(); + } + + if( !dbInfo.logSystemConfig.isEqual(req.logSystemConfig) ) { + isChanged = true; + dbInfo.logSystemConfig = req.logSystemConfig; + } + + if( dbInfo.resolvers != req.resolvers ) { + isChanged = true; + dbInfo.resolvers = req.resolvers; + } + + if( dbInfo.recoveryCount != req.recoveryCount ) { + isChanged = true; + dbInfo.recoveryCount = req.recoveryCount; + } + + if( isChanged ) { + dbInfo.id = g_random->randomUniqueID(); + self->db.serverInfo->set( dbInfo ); + } + + checkOutstandingMasterRequests(self); +} + +void registerWorker( RegisterWorkerRequest req, ClusterControllerData *self ) { + WorkerInterface w = req.wi; + ProcessClass processClass = req.processClass; + + TraceEvent("ClusterControllerActualWorkers", self->id).detail("WorkerID",w.id()).detailext("ProcessID", w.locality.processId()).detailext("ZoneId", w.locality.zoneId()).detailext("DataHall", w.locality.dataHallId()).detail("pClass", req.processClass.toString()); + auto info = self->id_worker.find( w.locality.processId() ); + + if( info == self->id_worker.end() ) { + auto classIter = self->id_class.find(w.locality.processId()); + + if( classIter != self->id_class.end() && (classIter->second.classSource() == ProcessClass::DBSource || req.processClass.classType() == ProcessClass::UnsetClass) ) { + processClass = classIter->second; + } + + self->id_worker[w.locality.processId()] = WorkerInfo( workerAvailabilityWatch( w, req.processClass, self ), req.reply, req.generation, w, req.processClass, processClass ); + checkOutstandingRequests( self ); + + return; + } + + if( info->second.interf.id() != w.id() || req.generation >= info->second.gen ) { + if( info->second.processClass.classSource() == ProcessClass::CommandLineSource || + (info->second.processClass.classSource() == ProcessClass::AutoSource && req.processClass.classType() != ProcessClass::UnsetClass) ) { + info->second.processClass = req.processClass; + } + + info->second.initialClass = req.processClass; + info->second.reply.send( Never() ); + info->second.reply = req.reply; + info->second.gen = req.generation; + + if(info->second.interf.id() != w.id()) { + info->second.interf = w; + info->second.watcher = workerAvailabilityWatch( w, req.processClass, self ); + } + return; + } + + TEST(true); // Received an old worker registration request. +} + +ACTOR Future statusServer(FutureStream< StatusRequest> requests, + ClusterControllerData *self, + ServerCoordinators coordinators) +{ + // Seconds since the END of the last GetStatus executed + state double last_request_time = 0.0; + + // Place to accumulate a batch of requests to respond to + state std::vector requests_batch; + + loop { + try { + // Wait til first request is ready + StatusRequest req = waitNext(requests); + requests_batch.push_back(req); + + // Earliest time at which we may begin a new request + double next_allowed_request_time = last_request_time + SERVER_KNOBS->STATUS_MIN_TIME_BETWEEN_REQUESTS; + + // Wait if needed to satisfy min_time knob, also allows more requets to queue up. + double minwait = std::max(next_allowed_request_time - now(), 0.0); + Void _ = wait(delay(minwait)); + + // Get all requests that are ready right *now*, before GetStatus() begins. + // All of these requests will be responded to with the next GetStatus() result. + while (requests.isReady()) + requests_batch.push_back(requests.pop()); + + // Get status but trap errors to send back to client. + vector> workers; + for(auto& it : self->id_worker) + workers.push_back(std::make_pair(it.second.interf, it.second.processClass)); + + std::vector incompatibleConnections; + for(auto it = self->db.incompatibleConnections.begin(); it != self->db.incompatibleConnections.end();) { + if(it->second < now()) { + it = self->db.incompatibleConnections.erase(it); + } else { + incompatibleConnections.push_back(it->first); + it++; + } + } + + ErrorOr result = wait(errorOr(clusterGetStatus(self->db.serverInfo, self->cx, workers, self->db.workersWithIssues, self->db.clientsWithIssues, self->db.clientVersionMap, coordinators, incompatibleConnections))); + if (result.isError() && result.getError().code() == error_code_actor_cancelled) + throw result.getError(); + + // Update last_request_time now because GetStatus is finished and the delay is to be measured between requests + last_request_time = now(); + + while (!requests_batch.empty()) + { + if (result.isError()) + requests_batch.back().reply.sendError(result.getError()); + else + requests_batch.back().reply.send(result.get()); + requests_batch.pop_back(); + } + } + catch (Error &e) { + TraceEvent(SevError, "StatusServerError").error(e); + throw e; + } + } +} + +ACTOR Future monitorProcessClasses(ClusterControllerData *self) { + + state ReadYourWritesTransaction trVer( self->db.db ); + loop { + try { + trVer.setOption( FDBTransactionOptions::ACCESS_SYSTEM_KEYS ); + trVer.setOption( FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE ); + + Optional val = wait(trVer.get(processClassVersionKey)); + + if (val.present()) + break; + + Standalone processClasses = wait( trVer.getRange( processClassKeys, CLIENT_KNOBS->TOO_MANY ) ); + ASSERT( !processClasses.more && processClasses.size() < CLIENT_KNOBS->TOO_MANY ); + + trVer.clear(processClassKeys); + trVer.set(processClassVersionKey, processClassVersionValue); + for (auto it : processClasses) { + UID processUid = decodeProcessClassKeyOld(it.key); + trVer.set(processClassKeyFor(processUid.toString()), it.value); + } + + Void _ = wait(trVer.commit()); + TraceEvent("ProcessClassUpgrade"); + break; + } + catch(Error &e) { + Void _ = wait( trVer.onError(e) ); + } + } + + loop { + state ReadYourWritesTransaction tr( self->db.db ); + + loop { + try { + tr.setOption( FDBTransactionOptions::ACCESS_SYSTEM_KEYS ); + tr.setOption( FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE ); + Standalone processClasses = wait( tr.getRange( processClassKeys, CLIENT_KNOBS->TOO_MANY ) ); + ASSERT( !processClasses.more && processClasses.size() < CLIENT_KNOBS->TOO_MANY ); + + if(processClasses != self->lastProcessClasses || !self->gotProcessClasses) { + self->id_class.clear(); + for( int i = 0; i < processClasses.size(); i++ ) { + auto c = decodeProcessClassValue( processClasses[i].value ); + ASSERT( c.classSource() != ProcessClass::CommandLineSource ); + self->id_class[decodeProcessClassKey( processClasses[i].key )] = c; + } + + for( auto& w : self->id_worker ) { + auto classIter = self->id_class.find(w.first); + + if( classIter != self->id_class.end() && (classIter->second.classSource() == ProcessClass::DBSource || w.second.initialClass.classType() == ProcessClass::UnsetClass) ) { + w.second.processClass = classIter->second; + } else { + w.second.processClass = w.second.initialClass; + } + } + + self->lastProcessClasses = processClasses; + self->gotProcessClasses = true; + checkOutstandingRequests( self ); + } + + state Future watchFuture = tr.watch(processClassChangeKey); + Void _ = wait(tr.commit()); + Void _ = wait(watchFuture); + break; + } + catch(Error &e) { + Void _ = wait( tr.onError(e) ); + } + } + } +} + +ACTOR Future monitorClientTxnInfoConfigs(ClusterControllerData::DBInfo* db) { + state const Key sampleRate= LiteralStringRef("client_txn_sample_rate/").withPrefix(fdbClientInfoPrefixRange.begin); + state const Key sizeLimit = LiteralStringRef("client_txn_size_limit/").withPrefix(fdbClientInfoPrefixRange.begin); + loop { + state ReadYourWritesTransaction tr(db->db); + loop { + try { + tr.setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS); + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + state Optional rateVal = wait(tr.get(sampleRate)); + state Optional limitVal = wait(tr.get(sizeLimit)); + ClientDBInfo clientInfo = db->clientInfo->get(); + if (rateVal.present()) { + double rate = BinaryReader::fromStringRef(rateVal.get(), Unversioned()); + clientInfo.clientTxnInfoSampleRate = rate; + } + if (limitVal.present()) { + int64_t limit = BinaryReader::fromStringRef(limitVal.get(), Unversioned()); + clientInfo.clientTxnInfoSizeLimit = limit; + } + if (rateVal.present() || limitVal.present()) { + clientInfo.id = g_random->randomUniqueID(); + db->clientInfo->set(clientInfo); + } + + state Future watchRateFuture = tr.watch(sampleRate); + state Future watchLimitFuture = tr.watch(sizeLimit); + Void _ = wait(tr.commit()); + choose { + when(Void _ = wait(watchRateFuture)) { break; } + when (Void _ = wait(watchLimitFuture)) { break; } + } + } + catch (Error &e) { + Void _ = wait(tr.onError(e)); + } + } + } +} + +ACTOR Future clusterControllerCore( ClusterControllerFullInterface interf, Future leaderFail, ServerCoordinators coordinators ) { + state ClusterControllerData self( interf ); + state Future coordinationPingDelay = delay( SERVER_KNOBS->WORKER_COORDINATION_PING_DELAY ); + state uint64_t step = 0; + state PromiseStream> addActor; + state Future> error = errorOr( actorCollection( addActor.getFuture() ) ); + + auto pSelf = &self; + addActor.send( failureDetectionServer( self.id, interf.clientInterface.failureMonitoring.getFuture() ) ); + addActor.send( clusterWatchDatabase( &self, &self.db ) ); // Start the master database + addActor.send( self.updateWorkerList.init( self.db.db ) ); + addActor.send( statusServer( interf.clientInterface.databaseStatus.getFuture(), &self, coordinators)); + addActor.send( monitorProcessClasses(&self) ); + addActor.send( monitorClientTxnInfoConfigs(&self.db) ); + //printf("%s: I am the cluster controller\n", g_network->getLocalAddress().toString().c_str()); + + loop choose { + when( ErrorOr err = wait( error ) ) { + if (err.isError()) { + endRole(interf.id(), "ClusterController", "Stop Received Error", false, err.getError()); + } + else { + endRole(interf.id(), "ClusterController", "Stop Received Signal", true); + } + + // We shut down normally even if there was a serious error (so this fdbserver may be re-elected cluster controller) + return Void(); + } + when( OpenDatabaseRequest req = waitNext( interf.clientInterface.openDatabase.getFuture() ) ) { + addActor.send( clusterOpenDatabase( &self.db, req.dbName, req.knownClientInfoID, req.issues.toString(), req.supportedVersions, req.reply ) ); + } + when( RecruitFromConfigurationRequest req = waitNext( interf.recruitFromConfiguration.getFuture() ) ) { + addActor.send( clusterRecruitFromConfiguration( &self, req ) ); + } + when( RecruitStorageRequest req = waitNext( interf.recruitStorage.getFuture() ) ) { + clusterRecruitStorage( &self, req ); + } + when( RegisterWorkerRequest req = waitNext( interf.registerWorker.getFuture() ) ) { + registerWorker( req, &self ); + } + when( GetWorkersRequest req = waitNext( interf.getWorkers.getFuture() ) ) { + if ( req.flags & GetWorkersRequest::FLAG_TESTER_CLASS ) { + vector> testers; + for(auto& it : self.id_worker) + if (it.second.processClass.classType() == ProcessClass::TesterClass) + testers.push_back(std::make_pair(it.second.interf, it.second.processClass)); + req.reply.send( testers ); + } else { + vector> workers; + for(auto& it : self.id_worker) + workers.push_back(std::make_pair(it.second.interf, it.second.processClass)); + req.reply.send( workers ); + } + } + when( GetClientWorkersRequest req = waitNext( interf.clientInterface.getClientWorkers.getFuture() ) ) { + vector workers; + for(auto& it : self.id_worker) { + if (it.second.processClass.classType() != ProcessClass::TesterClass) { + workers.push_back(it.second.interf.clientInterface); + } + } + req.reply.send(workers); + } + when( Void _ = wait( coordinationPingDelay ) ) { + CoordinationPingMessage message(self.id, step++); + for(auto& it : self.id_worker) + it.second.interf.coordinationPing.send(message); + coordinationPingDelay = delay( SERVER_KNOBS->WORKER_COORDINATION_PING_DELAY ); + TraceEvent("CoordinationPingSent", self.id).detail("TimeStep", message.timeStep); + } + when( RegisterMasterRequest req = waitNext( interf.registerMaster.getFuture() ) ) { + clusterRegisterMaster( &self, req ); + } + when( GetServerDBInfoRequest req = waitNext( interf.getServerDBInfo.getFuture() ) ) { + addActor.send( clusterGetServerInfo( &self.db, req.knownServerInfoID, req.issues.toString(), req.incompatiblePeers, req.reply ) ); + } + when( Void _ = wait( leaderFail ) ) { + // We are no longer the leader if this has changed. + endRole(interf.id(), "ClusterController", "Leader Replaced", true); + TEST(true); // Lost Cluster Controller Role + return Void(); + } + when( ReplyPromise ping = waitNext( interf.clientInterface.ping.getFuture() ) ) { + ping.send( Void() ); + } + } +} + +ACTOR Future clusterController( ServerCoordinators coordinators, Reference>> currentCC, bool hasConnected ) { + loop { + state ClusterControllerFullInterface cci; + state bool inRole = false; + cci.initEndpoints(); + try { + //Register as a possible leader; wait to be elected + state Future leaderFail = tryBecomeLeader( coordinators, cci, currentCC, hasConnected ); + + while (!currentCC->get().present() || currentCC->get().get() != cci) { + choose { + when( Void _ = wait(currentCC->onChange()) ) {} + when( Void _ = wait(leaderFail) ) { ASSERT(false); throw internal_error(); } + } + } + + hasConnected = true; + startRole(cci.id(), UID(), "ClusterController"); + inRole = true; + + Void _ = wait( clusterControllerCore( cci, leaderFail, coordinators ) ); + } catch(Error& e) { + if (inRole) + endRole(cci.id(), "ClusterController", "Error", e.code() == error_code_actor_cancelled || e.code() == error_code_coordinators_changed, e); + else + TraceEvent( e.code() == error_code_coordinators_changed ? SevInfo : SevError, "ClusterControllerCandidateError", cci.id()).error(e); + throw; + } + } +} + +ACTOR Future clusterController( Reference connFile, Reference>> currentCC ) { + state bool hasConnected = false; + loop { + try { + ServerCoordinators coordinators( connFile ); + Void _ = wait( clusterController( coordinators, currentCC, hasConnected ) ); + } catch( Error &e ) { + if( e.code() != error_code_coordinators_changed ) + throw; // Expected to terminate fdbserver + } + + hasConnected = true; + } +} diff --git a/fdbserver/ClusterRecruitmentInterface.h b/fdbserver/ClusterRecruitmentInterface.h new file mode 100644 index 0000000000..cf988d05d7 --- /dev/null +++ b/fdbserver/ClusterRecruitmentInterface.h @@ -0,0 +1,182 @@ +/* + * ClusterRecruitmentInterface.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_CLUSTERRECRUITMENTINTERFACE_H +#define FDBSERVER_CLUSTERRECRUITMENTINTERFACE_H +#pragma once + +#include "fdbclient/ClusterInterface.h" +#include "fdbclient/StorageServerInterface.h" +#include "fdbclient/MasterProxyInterface.h" +#include "DatabaseConfiguration.h" +#include "MasterInterface.h" +#include "TLogInterface.h" +#include "WorkerInterface.h" +#include "Knobs.h" + +// This interface and its serialization depend on slicing, since the client will deserialize only the first part of this structure +struct ClusterControllerFullInterface { + ClusterInterface clientInterface; + RequestStream< struct RecruitFromConfigurationRequest > recruitFromConfiguration; + RequestStream< struct RecruitStorageRequest > recruitStorage; + RequestStream< struct RegisterWorkerRequest > registerWorker; + RequestStream< struct GetWorkersRequest > getWorkers; + RequestStream< struct RegisterMasterRequest > registerMaster; + RequestStream< struct GetServerDBInfoRequest > getServerDBInfo; + + UID id() const { return clientInterface.id(); } + bool operator == (ClusterControllerFullInterface const& r) const { return id() == r.id(); } + bool operator != (ClusterControllerFullInterface const& r) const { return id() != r.id(); } + + void initEndpoints() { + clientInterface.initEndpoints(); + recruitFromConfiguration.getEndpoint( TaskClusterController ); + recruitStorage.getEndpoint( TaskClusterController ); + registerWorker.getEndpoint( TaskClusterController ); + getWorkers.getEndpoint( TaskClusterController ); + registerMaster.getEndpoint( TaskClusterController ); + getServerDBInfo.getEndpoint( TaskClusterController ); + } + + template + void serialize( Ar& ar ) { + ASSERT( ar.protocolVersion() >= 0x0FDB00A200040001LL ); + ar & clientInterface & recruitFromConfiguration & recruitStorage & registerWorker & getWorkers & registerMaster & getServerDBInfo; + } +}; + +struct RecruitFromConfigurationRequest { + DatabaseConfiguration configuration; + ReplyPromise< struct RecruitFromConfigurationReply > reply; + + RecruitFromConfigurationRequest() {} + explicit RecruitFromConfigurationRequest(DatabaseConfiguration const& configuration) + : configuration(configuration) {} + + template + void serialize( Ar& ar ) { + ar & configuration & reply; + } +}; + +struct RecruitFromConfigurationReply { + vector tLogs; + vector proxies; + vector resolvers; + + template + void serialize( Ar& ar ) { + ar & tLogs & proxies & resolvers; + } +}; + +struct RecruitStorageReply { + WorkerInterface worker; + ProcessClass processClass; + + template + void serialize( Ar& ar ) { + ar & worker & processClass; + } +}; + +struct RecruitStorageRequest { + std::vector>> excludeMachines; //< Don't recruit any of these machines + std::vector excludeAddresses; //< Don't recruit any of these addresses + std::vector>> excludeDCs; //< Don't recruit from any of these data centers + bool criticalRecruitment; //< True if machine classes are to be ignored + ReplyPromise< RecruitStorageReply > reply; + + template + void serialize( Ar& ar ) { + ar & excludeMachines & excludeAddresses & excludeDCs & criticalRecruitment & reply; + } +}; + +struct RegisterWorkerRequest { + WorkerInterface wi; + ProcessClass processClass; + Generation generation; + ReplyPromise reply; + + RegisterWorkerRequest() {} + RegisterWorkerRequest(WorkerInterface wi, ProcessClass processClass, Generation generation) : + wi(wi), processClass(processClass), generation(generation) {} + + template + void serialize( Ar& ar ) { + ar & wi & processClass & generation & reply; + } +}; + +struct GetWorkersRequest { + enum { FLAG_TESTER_CLASS = 1 }; + + int flags; + ReplyPromise>> reply; + + GetWorkersRequest() : flags(0) {} + explicit GetWorkersRequest(int fl) : flags(fl) {} + + template + void serialize(Ar& ar) { + ar & flags & reply; + } +}; + +struct RegisterMasterRequest { + Standalone dbName; + UID id; + LocalityData mi; + LogSystemConfig logSystemConfig; + vector proxies; + vector resolvers; + DBRecoveryCount recoveryCount; + int64_t registrationCount; + DatabaseConfiguration configuration; + vector priorCommittedLogServers; + int recoveryState; + + ReplyPromise reply; + + RegisterMasterRequest() {} + + template + void serialize( Ar& ar ) { + ASSERT( ar.protocolVersion() >= 0x0FDB00A200040001LL ); + ar & dbName & id & mi & logSystemConfig & proxies & resolvers & recoveryCount & registrationCount & configuration & priorCommittedLogServers & recoveryState & reply; + } +}; + +struct GetServerDBInfoRequest { + UID knownServerInfoID; + Standalone issues; + std::vector incompatiblePeers; + ReplyPromise< struct ServerDBInfo > reply; + + template + void serialize(Ar& ar) { + ar & knownServerInfoID & issues & incompatiblePeers & reply; + } +}; + +#include "ServerDBInfo.h" // include order hack + +#endif diff --git a/fdbserver/ConflictSet.h b/fdbserver/ConflictSet.h new file mode 100644 index 0000000000..412bfbabba --- /dev/null +++ b/fdbserver/ConflictSet.h @@ -0,0 +1,62 @@ +/* + * ConflictSet.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CONFLICTSET_H +#define CONFLICTSET_H +#pragma once + +#include "fdbclient/CommitTransaction.h" + +struct ConflictSet; +ConflictSet* newConflictSet(); +void clearConflictSet( ConflictSet*, Version ); +void destroyConflictSet(ConflictSet*); + +struct ConflictBatch { + explicit ConflictBatch( ConflictSet* ); + ~ConflictBatch(); + + enum TransactionCommitResult { + TransactionConflict = 0, + TransactionTooOld, + TransactionCommitted, + }; + + void addTransaction( const CommitTransactionRef& transaction ); + void detectConflicts(Version now, Version newOldestVersion, vector& nonConflicting, vector* tooOldTransactions = NULL); + void GetTooOldTransactions(vector& tooOldTransactions); + +private: + ConflictSet* cs; + Standalone< VectorRef< struct TransactionInfo* > > transactionInfo; + vector points; + int transactionCount; + vector< pair > combinedWriteConflictRanges; + vector< struct ReadConflictRange > combinedReadConflictRanges; + bool* transactionConflictStatus; + + void checkIntraBatchConflicts(); + void combineWriteConflictRanges(); + void checkReadConflictRanges(); + void mergeWriteConflictRanges(Version now); + void addConflictRanges(Version now, vector< pair >::iterator begin, vector< pair >::iterator end, class SkipList* part); +}; + +#endif \ No newline at end of file diff --git a/fdbserver/CoordinatedState.actor.cpp b/fdbserver/CoordinatedState.actor.cpp new file mode 100644 index 0000000000..59f26cf87d --- /dev/null +++ b/fdbserver/CoordinatedState.actor.cpp @@ -0,0 +1,308 @@ +/* + * CoordinatedState.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CoordinatedState.h" +#include "CoordinationInterface.h" +#include "Knobs.h" +#include "flow/ActorCollection.h" +#include "LeaderElection.h" + +ACTOR Future waitAndSendRead( RequestStream to, GenerationRegReadRequest req ) { + if( SERVER_KNOBS->BUGGIFY_ALL_COORDINATION || BUGGIFY ) + Void _ = wait( delay( SERVER_KNOBS->BUGGIFIED_EVENTUAL_CONSISTENCY*g_random->random01() ) ); + state GenerationRegReadReply reply = wait( retryBrokenPromise( to, req ) ); + if( SERVER_KNOBS->BUGGIFY_ALL_COORDINATION || BUGGIFY ) + Void _ = wait( delay( SERVER_KNOBS->BUGGIFIED_EVENTUAL_CONSISTENCY*g_random->random01() ) ); + return reply; +} + +ACTOR Future waitAndSendWrite(RequestStream to, GenerationRegWriteRequest req) { + if( SERVER_KNOBS->BUGGIFY_ALL_COORDINATION || BUGGIFY ) + Void _ = wait( delay( SERVER_KNOBS->BUGGIFIED_EVENTUAL_CONSISTENCY*g_random->random01() ) ); + state UniqueGeneration reply = wait( retryBrokenPromise( to, req ) ); + if( SERVER_KNOBS->BUGGIFY_ALL_COORDINATION || BUGGIFY ) + Void _ = wait( delay( SERVER_KNOBS->BUGGIFIED_EVENTUAL_CONSISTENCY*g_random->random01() ) ); + return reply; +} + +ACTOR Future emptyToNever( Future f ) { + state GenerationRegReadReply r = wait(f); + if (r.gen.generation == 0) + Void _ = wait( Future(Never()) ); + return r; +} + +ACTOR Future nonemptyToNever( Future f ) { + state GenerationRegReadReply r = wait(f); + if (r.gen.generation != 0) + Void _ = wait( Future(Never()) ); + return r; +} + +struct CoordinatedStateImpl { + ServerCoordinators coordinators; + int stage; + UniqueGeneration gen; + uint64_t conflictGen; + bool doomed; + ActorCollection ac; //Errors are not reported + bool initial; + + CoordinatedStateImpl( ServerCoordinators const& c ) : coordinators(c), stage(0), conflictGen(0), doomed(false), ac(false), initial(false) {} + uint64_t getConflict() { return conflictGen; } + + bool isDoomed( GenerationRegReadReply const& rep ) { + return rep.gen > gen // setExclusive is doomed, because there was a write at least started at a higher generation, which means a read completed at that higher generation + // || rep.rgen > gen // setExclusive isn't absolutely doomed, but it may/probably will fail + ; + } + + ACTOR static Future read( CoordinatedStateImpl* self ) { + ASSERT( self->stage == 0 ); + + self->stage = 1; + GenerationRegReadReply rep = wait( self->replicatedRead( self, GenerationRegReadRequest( self->coordinators.clusterKey, UniqueGeneration() ) ) ); + self->conflictGen = std::max( self->conflictGen, std::max(rep.gen.generation, rep.rgen.generation) ) + 1; + self->gen = UniqueGeneration( self->conflictGen, g_random->randomUniqueID() ); + + self->stage = 2; + GenerationRegReadReply rep = wait( self->replicatedRead( self, GenerationRegReadRequest( self->coordinators.clusterKey, self->gen ) ) ); + self->stage = 3; + self->conflictGen = std::max(self->conflictGen, std::max( rep.gen.generation, rep.rgen.generation )); + if (self->isDoomed(rep)) + self->doomed = true; + self->initial = rep.gen.generation == 0; + + self->stage = 4; + return rep.value.present() ? rep.value.get() : Value(); + } + ACTOR static Future onConflict( CoordinatedStateImpl* self ) { + ASSERT( self->stage == 4 ); + if (self->doomed) return Void(); + loop { + Void _ = wait( delay( SERVER_KNOBS->COORDINATED_STATE_ONCONFLICT_POLL_INTERVAL ) ); + GenerationRegReadReply rep = wait( self->replicatedRead( self, GenerationRegReadRequest( self->coordinators.clusterKey, UniqueGeneration() ) ) ); + if (self->stage > 4) break; + self->conflictGen = std::max(self->conflictGen, std::max( rep.gen.generation, rep.rgen.generation )); + if (self->isDoomed(rep)) + return Void(); + } + Void _ = wait( Future(Never()) ); + return Void(); + } + ACTOR static Future setExclusive( CoordinatedStateImpl* self, Value v ) { + ASSERT( self->stage == 4 ); + self->stage = 5; + + UniqueGeneration wgen = wait( self->replicatedWrite( self, GenerationRegWriteRequest( KeyValueRef(self->coordinators.clusterKey, v), self->gen ) ) ); + self->stage = 6; + + TraceEvent("CoordinatedStateSet").detail("gen", self->gen.generation).detail("wgen", wgen.generation) + .detail("genu", self->gen.uid).detail("wgenu", wgen.uid) + .detail("cgen", self->conflictGen); + + if (wgen == self->gen) + return Void(); + else { + self->conflictGen = std::max(self->conflictGen, wgen.generation); + throw coordinated_state_conflict(); + } + } + + ACTOR static Future replicatedRead( CoordinatedStateImpl* self, GenerationRegReadRequest req ) { + state std::vector &replicas = self->coordinators.stateServers; + state vector< Future > rep_empty_reply; + state vector< Future > rep_reply; + for(int i=0; i reply = waitAndSendRead( replicas[i].read, GenerationRegReadRequest(req.key, req.gen) ); + rep_empty_reply.push_back( nonemptyToNever( reply ) ); + rep_reply.push_back( emptyToNever( reply ) ); + self->ac.add( success( reply ) ); + } + + state Future majorityEmpty = quorum( rep_empty_reply, (replicas.size()+1)/2 ); //enough empty to ensure we cannot achieve a majority non-empty + Void _ = wait( quorum( rep_reply, replicas.size()/2 + 1 ) || majorityEmpty ); + + if( majorityEmpty.isReady() ) { + int best = -1; + for(int i=0; i rep_empty_reply[best].get().rgen ) + best = i; + } + ASSERT( best >= 0 ); + auto result = rep_empty_reply[best].get(); + return result; + } else { + int best = -1; + for(int i=0; i rep_reply[best].get().gen || + ( rep_reply[i].get().gen == rep_reply[best].get().gen && rep_reply[i].get().rgen > rep_reply[best].get().rgen ) ) + best = i; + } + ASSERT( best >= 0 ); + auto result = rep_reply[best].get(); + return result; + } + } + + ACTOR static Future replicatedWrite( CoordinatedStateImpl* self, GenerationRegWriteRequest req ) { + state std::vector &replicas = self->coordinators.stateServers; + state vector< Future > wrep_reply; + for(int i=0; i reply = waitAndSendWrite( replicas[i].write, GenerationRegWriteRequest( req.kv, req.gen ) ); + wrep_reply.push_back( reply ); + self->ac.add( success( reply ) ); + } + + Void _ = wait( quorum( wrep_reply, self->initial ? replicas.size() : replicas.size()/2 + 1 ) ); + + UniqueGeneration maxGen; + for(int i=0; i CoordinatedState::read() { return CoordinatedStateImpl::read(impl); } +Future CoordinatedState::onConflict() { return CoordinatedStateImpl::onConflict(impl); } +Future CoordinatedState::setExclusive(Value v) { return CoordinatedStateImpl::setExclusive(impl,v); } +uint64_t CoordinatedState::getConflict() { return impl->getConflict(); } + +struct MovableValue { + enum MoveState { + MaybeTo = 1, + Active = 2, + MovingFrom = 3 + }; + + Value value; + int32_t mode; + Optional other; // a cluster connection string + + MovableValue() : mode( Active ) {} + MovableValue( Value const& v, int mode, Optional other = Optional() ) : value( v ), mode( mode ), other( other ) {} + + template + void serialize(Ar& ar) { + ASSERT( ar.protocolVersion() >= 0x0FDB00A2000D0001LL ); + ar & value & mode & other; + } +}; + +struct MovableCoordinatedStateImpl { + ServerCoordinators coordinators; + CoordinatedState cs; + Optional lastValue, // The value passed to setExclusive() + lastCSValue; // The value passed to cs.setExclusive() + + MovableCoordinatedStateImpl( ServerCoordinators const& c ) : coordinators(c), cs(c) {} + + ACTOR static Future read( MovableCoordinatedStateImpl* self ) { + state MovableValue moveState; + Value rawValue = wait( self->cs.read() ); + if( rawValue.size() ) { + BinaryReader r( rawValue, IncludeVersion() ); + if (r.protocolVersion() < 0x0FDB00A2000D0001LL) { + // Old coordinated state, not a MovableValue + moveState.value = rawValue; + } else + r >> moveState; + } + // SOMEDAY: If moveState.mode == MovingFrom, read (without locking) old state and assert that it corresponds with our state and is ReallyTo(coordinators) + if (moveState.mode == MovableValue::MaybeTo) { + TEST(true); + ASSERT( moveState.other.present() ); + Void _ = wait( self->moveTo( self, &self->cs, ClusterConnectionString( moveState.other.get().toString() ), moveState.value ) ); + } + return moveState.value; + } + + Future onConflict() { + return cs.onConflict(); + } + + Future setExclusive( Value v ) { + lastValue=v; + lastCSValue=BinaryWriter::toValue( MovableValue( v, MovableValue::Active ), IncludeVersion() ); + return cs.setExclusive( lastCSValue.get() ); + } + + ACTOR static Future move( MovableCoordinatedStateImpl* self, ClusterConnectionString nc ) { + // Call only after setExclusive returns. Attempts to move the coordinated state + // permanently to the new ServerCoordinators, which must be uninitialized. Returns when the process has + // reached the point where a leader elected by the new coordinators should be doing the rest of the work + // (and therefore the caller should die). + state CoordinatedState cs( self->coordinators ); + state CoordinatedState nccs( ServerCoordinators( Reference( new ClusterConnectionFile(nc) ) ) ); + state Future creationTimeout = delay(30); + ASSERT( self->lastValue.present() && self->lastCSValue.present() ); + TraceEvent("StartMove").detail("ConnectionString", nc.toString() ); + choose { + when (Void _ = wait(creationTimeout)) { throw new_coordinators_timed_out(); } + when (Value ncInitialValue = wait( nccs.read() )) { + ASSERT( !ncInitialValue.size() ); // The new coordinators must be uninitialized! + } + } + TraceEvent("FinishedRead").detail("ConnectionString", nc.toString() ); + + choose { + when (Void _ = wait(creationTimeout)) { throw new_coordinators_timed_out(); } + when ( Void _ = wait( nccs.setExclusive( BinaryWriter::toValue( MovableValue( self->lastValue.get(), MovableValue::MovingFrom, self->coordinators.ccf->getConnectionString().toString() ), IncludeVersion() ) ) ) ) {} + } + + if (BUGGIFY) Void _ = wait(delay(5)); + + Value oldQuorumState = wait( cs.read() ); + if ( oldQuorumState != self->lastCSValue.get() ) { + TEST(true); // Quorum change aborted by concurrent write to old coordination state + TraceEvent("QuorumChangeAbortedByConcurrency"); + throw coordinated_state_conflict(); + } + + Void _ = wait( self->moveTo( self, &cs, nc, self->lastValue.get() ) ); + + throw coordinators_changed(); + } + + ACTOR static Future moveTo( MovableCoordinatedStateImpl* self, CoordinatedState* coordinatedState, ClusterConnectionString nc, Value value ) { + Void _ = wait( coordinatedState->setExclusive( BinaryWriter::toValue( MovableValue( value, MovableValue::MaybeTo, nc.toString() ), IncludeVersion() ) ) ); + + if (BUGGIFY) Void _ = wait( delay(5) ); + + // SOMEDAY: If we are worried about someone magically getting the new cluster ID and interfering, do a second cs.setExclusive( encode( ReallyTo, ... ) ) + TraceEvent("ChangingQuorum").detail("ConnectionString", nc.toString()); + Void _ = wait( changeLeaderCoordinators( self->coordinators, StringRef(nc.toString()) ) ); + TraceEvent("ChangedQuorum").detail("ConnectionString", nc.toString()); + throw coordinators_changed(); + } +}; + +MovableCoordinatedState::MovableCoordinatedState( class ServerCoordinators const& coord ) : impl( new MovableCoordinatedStateImpl(coord) ) {} +MovableCoordinatedState::~MovableCoordinatedState() { delete impl; } +Future MovableCoordinatedState::read() { return MovableCoordinatedStateImpl::read(impl); } +Future MovableCoordinatedState::onConflict() { return impl->onConflict(); } +Future MovableCoordinatedState::setExclusive(Value v) { return impl->setExclusive(v); } +Future MovableCoordinatedState::move( ClusterConnectionString const& nc ) { return MovableCoordinatedStateImpl::move(impl, nc); } \ No newline at end of file diff --git a/fdbserver/CoordinatedState.h b/fdbserver/CoordinatedState.h new file mode 100644 index 0000000000..81c7c234bd --- /dev/null +++ b/fdbserver/CoordinatedState.h @@ -0,0 +1,81 @@ +/* + * CoordinatedState.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_COORDINATED_STATE_H +#define FDBSERVER_COORDINATED_STATE_H +#pragma once + +#include "fdbclient/FDBTypes.h" + +class CoordinatedState : NonCopyable { +public: + // Callers must ensure that any outstanding operations have been cancelled before destructing *this! + CoordinatedState( class ServerCoordinators const& ); + ~CoordinatedState(); + + Future read(); + // May only be called once. + // Returns the most recent state if there are no concurrent calls to setExclusive + // Otherwise might return the state passed to a concurrent call, even if that call ultimately fails. + // Don't count on the result of this read being part of the serialized history until a subsequent setExclusive has succeeded! + + Future onConflict(); + // May only be called once, and only after read returns. + // Eventually returns Void if a call to setExclusive would fail. + // May or may not return or throw an error after setExclusive is called. + // (Generally?) doesn't return unless there is some concurrent call to read or setExclusive. + + Future setExclusive(Value); + // read() must have been called and returned first, and this may only be called once. + // Attempts to change the state value, provided that the value returned by read is still the + // most recent. + // If it returns Void, the state was successfully changed and the state returned by read was + // the most recent before the new state. + // If it throws coordinated_state_conflict, the state may or may not have been changed, and the value + // returned from read may or may not ever have been a valid state. Probably there was a + // call to read() or setExclusive() concurrently with this pair. + + uint64_t getConflict(); +private: + struct CoordinatedStateImpl *impl; +}; + +class MovableCoordinatedState : NonCopyable { +public: + MovableCoordinatedState( class ServerCoordinators const& ); + ~MovableCoordinatedState(); + + Future read(); + + Future onConflict(); + + Future setExclusive(Value v); + + Future move( class ClusterConnectionString const& nc ); + // Call only after setExclusive returns. Attempts to move the coordinated state + // permanently to the new ServerCoordinators, which must be uninitialized. Returns when the process has + // reached the point where a leader elected by the new coordinators should be doing the rest of the work + // (and therefore the caller should die). + +private: + struct MovableCoordinatedStateImpl *impl; +}; + +#endif \ No newline at end of file diff --git a/fdbserver/Coordination.actor.cpp b/fdbserver/Coordination.actor.cpp new file mode 100644 index 0000000000..d7c1a90c4b --- /dev/null +++ b/fdbserver/Coordination.actor.cpp @@ -0,0 +1,416 @@ +/* + * Coordination.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "fdbserver/CoordinationInterface.h" +#include "IKeyValueStore.h" +#include "flow/ActorCollection.h" +#include "Knobs.h" +#include "flow/UnitTest.h" +#include "flow/IndexedSet.h" + +// This module implements coordinationServer() and the interfaces in CoordinationInterface.h + +struct GenerationRegVal { + UniqueGeneration readGen, writeGen; + Optional val; + template + void serialize(Ar& ar) { + ar & readGen & writeGen & val; + } +}; + +// UID WLTOKEN_CLIENTLEADERREG_GETLEADER( -1, 2 ); // from fdbclient/MonitorLeader.actor.cpp +UID WLTOKEN_LEADERELECTIONREG_CANDIDACY( -1, 3 ); +UID WLTOKEN_LEADERELECTIONREG_LEADERHEARTBEAT( -1, 4 ); +UID WLTOKEN_LEADERELECTIONREG_FORWARD( -1, 5 ); +UID WLTOKEN_GENERATIONREG_READ( -1, 6 ); +UID WLTOKEN_GENERATIONREG_WRITE( -1, 7 ); + +GenerationRegInterface::GenerationRegInterface( NetworkAddress remote ) + : read( Endpoint(remote, WLTOKEN_GENERATIONREG_READ) ), + write( Endpoint(remote, WLTOKEN_GENERATIONREG_WRITE) ) +{ +} + +GenerationRegInterface::GenerationRegInterface( INetwork* local ) +{ + read.makeWellKnownEndpoint( WLTOKEN_GENERATIONREG_READ, TaskCoordination ); + write.makeWellKnownEndpoint( WLTOKEN_GENERATIONREG_WRITE, TaskCoordination ); +} + +LeaderElectionRegInterface::LeaderElectionRegInterface(NetworkAddress remote) + : ClientLeaderRegInterface(remote), + candidacy( Endpoint(remote, WLTOKEN_LEADERELECTIONREG_CANDIDACY) ), + leaderHeartbeat( Endpoint(remote, WLTOKEN_LEADERELECTIONREG_LEADERHEARTBEAT) ), + forward( Endpoint(remote, WLTOKEN_LEADERELECTIONREG_FORWARD) ) +{ +} + +LeaderElectionRegInterface::LeaderElectionRegInterface(INetwork* local) + : ClientLeaderRegInterface(local) +{ + candidacy.makeWellKnownEndpoint( WLTOKEN_LEADERELECTIONREG_CANDIDACY, TaskCoordination ); + leaderHeartbeat.makeWellKnownEndpoint( WLTOKEN_LEADERELECTIONREG_LEADERHEARTBEAT, TaskCoordination ); + forward.makeWellKnownEndpoint( WLTOKEN_LEADERELECTIONREG_FORWARD, TaskCoordination ); +} + +ServerCoordinators::ServerCoordinators( Reference cf ) + : ClientCoordinators(cf) +{ + ClusterConnectionString cs = ccf->getConnectionString(); + for(auto s = cs.coordinators().begin(); s != cs.coordinators().end(); ++s) { + leaderElectionServers.push_back( LeaderElectionRegInterface( *s ) ); + stateServers.push_back( GenerationRegInterface( *s ) ); + } +} + +// The coordination server wants to create its key value store only if it is actually used +struct OnDemandStore { +public: + OnDemandStore( std::string folder, UID myID ) : folder(folder), store(NULL), myID(myID) {} + ~OnDemandStore() { if (store) store->close(); } + + IKeyValueStore* get() { + if (!store) open(); + return store; + } + + bool exists() { + if (store) + return true; + return fileExists( joinPath(folder, "coordination-0.fdq") ) || fileExists( joinPath(folder, "coordination-1.fdq") ) || fileExists( joinPath(folder, "coordination.fdb") ); + } + + IKeyValueStore* operator->() { return get(); } + + Future getError() { return onErr(err.getFuture()); } + +private: + std::string folder; + UID myID; + IKeyValueStore* store; + Promise> err; + + ACTOR static Future onErr( Future> e ) { + Future f = wait(e); + Void _ = wait(f); + return Void(); + } + + void open() { + platform::createDirectory( folder ); + store = keyValueStoreMemory( joinPath(folder, "coordination-"), myID, 500e6 ); + err.send( store->getError() ); + } +}; + +ACTOR Future localGenerationReg( GenerationRegInterface interf, OnDemandStore* pstore ) { + state GenerationRegVal v; + state OnDemandStore& store = *pstore; + // SOMEDAY: concurrent access to different keys? + loop choose { + when ( GenerationRegReadRequest _req = waitNext( interf.read.getFuture() ) ) { + TraceEvent("GenerationRegReadRequest").detail("From", _req.reply.getEndpoint().address).detail("K", printable(_req.key)); + state GenerationRegReadRequest req = _req; + Optional rawV = wait( store->readValue( req.key ) ); + v = rawV.present() ? BinaryReader::fromStringRef( rawV.get(), IncludeVersion() ) : GenerationRegVal(); + TraceEvent("GenerationRegReadReply").detail("RVSize", rawV.present() ? rawV.get().size() : -1).detail("VWG", v.writeGen.generation); + if (v.readGen < req.gen) { + v.readGen = req.gen; + store->set( KeyValueRef( req.key, BinaryWriter::toValue(v, IncludeVersion()) ) ); + Void _ = wait(store->commit()); + } + req.reply.send( GenerationRegReadReply( v.val, v.writeGen, v.readGen ) ); + } + when ( GenerationRegWriteRequest _wrq = waitNext( interf.write.getFuture() ) ) { + state GenerationRegWriteRequest wrq = _wrq; + Optional rawV = wait( store->readValue( wrq.kv.key ) ); + v = rawV.present() ? BinaryReader::fromStringRef( rawV.get(), IncludeVersion() ) : GenerationRegVal(); + if (v.readGen <= wrq.gen && v.writeGen < wrq.gen) { + v.writeGen = wrq.gen; + v.val = wrq.kv.value; + store->set( KeyValueRef( wrq.kv.key, BinaryWriter::toValue(v, IncludeVersion()) ) ); + Void _ = wait(store->commit()); + TraceEvent("GenerationRegWrote").detail("From", wrq.reply.getEndpoint().address).detail("Key", printable(wrq.kv.key)) + .detail("reqGen", wrq.gen.generation).detail("Returning", v.writeGen.generation); + wrq.reply.send( v.writeGen ); + } else { + TraceEvent("GenerationRegWriteFail").detail("From", wrq.reply.getEndpoint().address).detail("Key", printable(wrq.kv.key)) + .detail("reqGen", wrq.gen.generation).detail("readGen", v.readGen.generation).detail("writeGen", v.writeGen.generation); + wrq.reply.send( std::max( v.readGen, v.writeGen ) ); + } + } + } +}; + +TEST_CASE("fdbserver/Coordination/localGenerationReg/simple") { + state GenerationRegInterface reg; + state OnDemandStore store("simfdb/unittests/", //< FIXME + g_random->randomUniqueID()); + state Future actor = localGenerationReg(reg, &store); + state Key the_key = g_random->randomAlphaNumeric( g_random->randomInt(0, 10) ); + + state UniqueGeneration firstGen(0, g_random->randomUniqueID()); + + GenerationRegReadReply r = wait(reg.read.getReply(GenerationRegReadRequest(the_key, firstGen))); + // If there was no prior write(_,_,0) or a data loss fault, + // returns (Optional(),0,gen2) + ASSERT(!r.value.present()); + ASSERT(r.gen == UniqueGeneration()); + ASSERT(r.rgen == firstGen); + + UniqueGeneration g = wait(reg.write.getReply(GenerationRegWriteRequest(KeyValueRef(the_key, LiteralStringRef("Value1")), firstGen))); + // (gen1==gen is considered a "successful" write) + ASSERT(g == firstGen); + + GenerationRegReadReply r = wait(reg.read.getReply(GenerationRegReadRequest(the_key, UniqueGeneration()))); + // read(key,gen2) returns (value,gen,rgen). + // There was some earlier or concurrent write(key,value,gen). + ASSERT(r.value == LiteralStringRef("Value1")); + ASSERT(r.gen == firstGen); + // There was some earlier or concurrent read(key,rgen). + ASSERT(r.rgen == firstGen); + // If there is a write(key,_,gen1)=>gen1 s.t. gen1 < gen2 OR the write completed before this read started, then gen >= gen1. + ASSERT(r.gen >= firstGen); + // If there is a read(key,gen1) that completed before this read started, then rgen >= gen1 + ASSERT(r.rgen >= firstGen); + + ASSERT(!actor.isReady()); + return Void(); +} + +// This actor implements a *single* leader-election register (essentially, it ignores +// the .key member of each request). It returns any time the leader election is in the +// default state, so that only active registers consume memory. +ACTOR Future leaderRegister(LeaderElectionRegInterface interf, Key key) { + state std::set availableCandidates; + state std::set availableLeaders; + state Optional currentNominee; + state vector>> notify; + state Future nextInterval = delay( 0 ); + state double candidateDelay = SERVER_KNOBS->CANDIDATE_MIN_DELAY; + state int leaderIntervalCount = 0; + + loop choose { + when ( GetLeaderRequest req = waitNext( interf.getLeader.getFuture() ) ) { + if (currentNominee.present() && currentNominee.get().changeID != req.knownLeader) + req.reply.send( currentNominee.get() ); + else + notify.push_back( req.reply ); + } + when ( CandidacyRequest req = waitNext( interf.candidacy.getFuture() ) ) { + //TraceEvent("CandidacyRequest").detail("Nominee", req.myInfo.changeID ); + availableCandidates.insert( req.myInfo ); + if (currentNominee.present() && currentNominee.get().changeID != req.knownLeader) + req.reply.send( currentNominee.get() ); + else + notify.push_back( req.reply ); + } + when (LeaderHeartbeatRequest req = waitNext( interf.leaderHeartbeat.getFuture() ) ) { + //TODO: use notify to only send a heartbeat once per interval + availableLeaders.insert( req.myInfo ); + req.reply.send( currentNominee.present() && req.myInfo == currentNominee.get() ); + } + when (ForwardRequest req = waitNext( interf.forward.getFuture() ) ) { + LeaderInfo newInfo; + newInfo.forward = true; + newInfo.serializedInfo = req.conn.toString(); + for(int i=0; i nextNominee = + availableLeaders.size() ? *availableLeaders.rbegin() : + availableCandidates.size() ? *availableCandidates.rbegin() : Optional(); + + if (nextNominee != currentNominee || !availableLeaders.size()) { + TraceEvent("NominatingLeader").detail("Nominee", nextNominee.present() ? nextNominee.get().changeID : UID()) + .detail("Changed", nextNominee != currentNominee).detail("Key", printable(key)); + for(int i=0; iPOLLING_FREQUENCY ); + if(leaderIntervalCount++ > 5) { + candidateDelay = SERVER_KNOBS->CANDIDATE_MIN_DELAY; + } + } else { + nextInterval = delay( candidateDelay ); + candidateDelay = std::min(SERVER_KNOBS->CANDIDATE_MAX_DELAY, candidateDelay * SERVER_KNOBS->CANDIDATE_GROWTH_RATE); + leaderIntervalCount = 0; + } + + availableLeaders.clear(); + availableCandidates.clear(); + } + } + } +} + +// Generation register values are stored without prefixing in the coordinated state, but always begin with an alphanumeric character +// (they are always derived from a ClusterConnectionString key). +// Forwarding values are stored in this range: +const KeyRangeRef fwdKeys( LiteralStringRef( "\xff" "fwd" ), LiteralStringRef( "\xff" "fwe" ) ); + +struct LeaderRegisterCollection { + // SOMEDAY: Factor this into a generic tool? Extend ActorCollection to support removal actions? What? + ActorCollection actors; + Map registerInterfaces; + Map forward; + OnDemandStore *pStore; + + LeaderRegisterCollection( OnDemandStore *pStore ) : actors( false ), pStore( pStore ) {} + + ACTOR static Future init( LeaderRegisterCollection *self ) { + if( !self->pStore->exists() ) + return Void(); + OnDemandStore &store = *self->pStore; + Standalone> forwardingInfo = wait( store->readRange( fwdKeys ) ); + for( int i = 0; i < forwardingInfo.size(); i++ ) { + LeaderInfo forwardInfo; + forwardInfo.forward = true; + forwardInfo.serializedInfo = forwardingInfo[i].value; + self->forward[ forwardingInfo[i].key.removePrefix( fwdKeys.begin ) ] = forwardInfo; + } + return Void(); + } + + Future onError() { return actors.getResult(); } + + Optional getForward(KeyRef key) { + auto i = forward.find( key ); + if (i == forward.end()) + return Optional(); + return i->value; + } + + ACTOR static Future setForward(LeaderRegisterCollection *self, KeyRef key, ClusterConnectionString conn) { + LeaderInfo forwardInfo; + forwardInfo.forward = true; + forwardInfo.serializedInfo = conn.toString(); + self->forward[ key ] = forwardInfo; + OnDemandStore &store = *self->pStore; + store->set( KeyValueRef( key.withPrefix( fwdKeys.begin ), conn.toString() ) ); + Void _ = wait(store->commit()); + return Void(); + } + + LeaderElectionRegInterface& getInterface(KeyRef key) { + auto i = registerInterfaces.find( key ); + if (i == registerInterfaces.end()) { + Key k = key; + Future a = wrap(this, k, leaderRegister(registerInterfaces[k], k) ); + if (a.isError()) throw a.getError(); + ASSERT( !a.isReady() ); + actors.add( a ); + i = registerInterfaces.find( key ); + } + ASSERT( i != registerInterfaces.end() ); + return i->value; + } + + ACTOR static Future wrap( LeaderRegisterCollection* self, Key key, Future actor ) { + state Error e; + try { + Void _ = wait(actor); + } catch (Error& err) { + if (err.code() == error_code_actor_cancelled) + throw; + e = err; + } + self->registerInterfaces.erase(key); + if (e.code() != invalid_error_code) throw e; + return Void(); + } + +}; + +// leaderServer multiplexes multiple leaderRegisters onto a single LeaderElectionRegInterface, +// creating and destroying them on demand. +ACTOR Future leaderServer(LeaderElectionRegInterface interf, OnDemandStore *pStore) { + state LeaderRegisterCollection regs( pStore ); + state ActorCollection forwarders(false); + + Void _ = wait( LeaderRegisterCollection::init( ®s ) ); + + loop choose { + when ( GetLeaderRequest req = waitNext( interf.getLeader.getFuture() ) ) { + Optional forward = regs.getForward(req.key); + if( forward.present() ) + req.reply.send( forward.get() ); + else + regs.getInterface(req.key).getLeader.send( req ); + } + when ( CandidacyRequest req = waitNext( interf.candidacy.getFuture() ) ) { + Optional forward = regs.getForward(req.key); + if( forward.present() ) + req.reply.send( forward.get() ); + else + regs.getInterface(req.key).candidacy.send(req); + } + when ( LeaderHeartbeatRequest req = waitNext( interf.leaderHeartbeat.getFuture() ) ) { + Optional forward = regs.getForward(req.key); + if( forward.present() ) + req.reply.send( false ); + else + regs.getInterface(req.key).leaderHeartbeat.send(req); + } + when ( ForwardRequest req = waitNext( interf.forward.getFuture() ) ) { + Optional forward = regs.getForward(req.key); + if( forward.present() ) + req.reply.send( Void() ); + else { + forwarders.add( LeaderRegisterCollection::setForward( ®s, req.key, ClusterConnectionString(req.conn.toString()) ) ); + regs.getInterface(req.key).forward.send(req); + } + } + when( Void _ = wait( forwarders.getResult() ) ) { ASSERT(false); throw internal_error(); } + } +} + +ACTOR Future coordinationServer(std::string dataFolder) { + state UID myID = g_random->randomUniqueID(); + state LeaderElectionRegInterface myLeaderInterface( g_network ); + state GenerationRegInterface myInterface( g_network ); + state OnDemandStore store( dataFolder, myID ); + + TraceEvent("CoordinationServer", myID).detail("myInterfaceAddr", myInterface.read.getEndpoint().address).detail("Folder", dataFolder); + + try { + Void _ = wait( localGenerationReg(myInterface, &store) || leaderServer(myLeaderInterface, &store) || store.getError() ); + throw internal_error(); + } catch (Error& e) { + TraceEvent("CoordinationServerError", myID).error(e, true); + throw; + } +} \ No newline at end of file diff --git a/fdbserver/CoordinationInterface.h b/fdbserver/CoordinationInterface.h new file mode 100644 index 0000000000..8fe877c33b --- /dev/null +++ b/fdbserver/CoordinationInterface.h @@ -0,0 +1,171 @@ +/* + * CoordinationInterface.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_COORDINATIONINTERFACE_H +#define FDBSERVER_COORDINATIONINTERFACE_H +#pragma once + +#include "fdbclient/CoordinationInterface.h" + +struct GenerationRegInterface { + RequestStream< struct GenerationRegReadRequest > read; + RequestStream< struct GenerationRegWriteRequest > write; + + // read(key,gen2) returns (value,gen,rgen). + // If there was no prior write(_,_,0) or a data loss fault, + // returns (Optional(),0,0) //< FIXME: The returned rgen is not zero, but >= gen2! (Specification bug) + // Else + // There was some earlier or concurrent write(key,value,gen). + // There was some earlier or concurrent read(key,rgen). + // If there is a write(key,_,gen1)=>gen1 s.t. gen1 < gen2 OR the write completed before this read started, then gen >= gen1. + // If there is a read(key,gen1) that completed before this read started, then rgen >= gen1 + // write(key,value,gen) returns gen1. + // If gen>0 and there was no prior write(_,_,0) or a data loss fault, throws not_created()? + // (gen1==gen is considered a "successful" write) + // There is some earlier or concurrent read(key,gen1) or write(key,_,gen1). (In the successful case, the concurrent write is this one) + + // All instances of the pattern + // read(key, g)=>v1 and write(key, v2, g)=>true + // thus form a totally ordered sequence of modifications, in which + // the v2 of the previous generation is the v1 of the next. + + GenerationRegInterface() {} + GenerationRegInterface( NetworkAddress remote ); + GenerationRegInterface( INetwork* local ); +}; + +struct UniqueGeneration { + uint64_t generation; + UID uid; + UniqueGeneration() : generation(0) {} + UniqueGeneration( uint64_t generation, UID uid ) : generation(generation), uid(uid) {} + bool operator < (UniqueGeneration const& r) const { + if (generation < r.generation) return true; + if (r.generation < generation) return false; + return uid < r.uid; + } + bool operator == (UniqueGeneration const& r) const { + return generation == r.generation && uid == r.uid; + } + template + void serialize(Ar& ar) { + ar & generation & uid; + } +}; + +struct GenerationRegReadRequest { + Key key; + UniqueGeneration gen; + ReplyPromise reply; + GenerationRegReadRequest(){} + GenerationRegReadRequest( Key key, UniqueGeneration gen ) : key(key), gen(gen) {} + template + void serialize(Ar& ar) { + ar & key & gen & reply; + } +}; + +struct GenerationRegReadReply { + Optional value; + UniqueGeneration gen, rgen; + GenerationRegReadReply() {} + GenerationRegReadReply( Optional value, UniqueGeneration gen, UniqueGeneration rgen ) : value(value), gen(gen), rgen(rgen) {} + template + void serialize(Ar& ar) { + ar & value & gen & rgen; + } +}; + +struct GenerationRegWriteRequest { + KeyValue kv; + UniqueGeneration gen; + ReplyPromise< UniqueGeneration > reply; + GenerationRegWriteRequest() {} + GenerationRegWriteRequest(KeyValue kv, UniqueGeneration gen) : kv(kv), gen(gen) {} + template + void serialize(Ar& ar) { + ar & kv & gen & reply; + } +}; + +struct LeaderElectionRegInterface : ClientLeaderRegInterface { + RequestStream< struct CandidacyRequest > candidacy; + RequestStream< struct LeaderHeartbeatRequest > leaderHeartbeat; + RequestStream< struct ForwardRequest > forward; + + LeaderElectionRegInterface() {} + LeaderElectionRegInterface(NetworkAddress remote); + LeaderElectionRegInterface(INetwork* local); +}; + +struct CandidacyRequest { + Key key; + LeaderInfo myInfo; + UID knownLeader; + ReplyPromise> reply; + + CandidacyRequest() {} + CandidacyRequest(Key key, LeaderInfo const& myInfo, UID const& knownLeader) : key(key), myInfo(myInfo), knownLeader(knownLeader) {} + + template + void serialize(Ar& ar) { + ar & key & myInfo & knownLeader & reply; + } +}; + +struct LeaderHeartbeatRequest { + Key key; + LeaderInfo myInfo; + ReplyPromise reply; + + LeaderHeartbeatRequest() {} + explicit LeaderHeartbeatRequest( Key key, LeaderInfo const& myInfo ) : key(key), myInfo(myInfo) {} + + template + void serialize(Ar& ar) { + ar & key & myInfo & reply; + } +}; + +struct ForwardRequest { + Key key; + Value conn; // a cluster connection string + ReplyPromise reply; + + ForwardRequest() {} + ForwardRequest( Key key, Value conn ) : key(key), conn(conn) {} + + template + void serialize(Ar& ar) { + ar & key & conn & reply; + } +}; + +class ServerCoordinators : public ClientCoordinators { +public: + explicit ServerCoordinators( Reference ); + + vector leaderElectionServers; + vector stateServers; +}; + +Future coordinationServer( std::string const& dataFolder ); + +#endif \ No newline at end of file diff --git a/fdbserver/CoroFlow.actor.cpp b/fdbserver/CoroFlow.actor.cpp new file mode 100644 index 0000000000..324f2fb6cd --- /dev/null +++ b/fdbserver/CoroFlow.actor.cpp @@ -0,0 +1,306 @@ +/* + * CoroFlow.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "CoroFlow.h" +#include "flow/ActorCollection.h" +#include "fdbrpc/libcoroutine/Coro.h" +#include "flow/TDMetric.actor.h" +#include "fdbrpc/simulator.h" + + +Coro *current_coro = 0, *main_coro = 0; +Coro* swapCoro( Coro* n ) { + Coro* t = current_coro; + current_coro = n; + return t; +} + +/*struct IThreadlike { +public: + virtual void start() = 0; // Call at most once! Causes run() to be called on the 'thread'. + virtual ~IThreadlike() {} // Pre: start hasn't been called, or run() has returned + virtual void unblock() = 0; // Pre: block() has been called by run(). Causes block() to return. + +protected: + virtual void block() = 0; // Call only from run(). Returns when unblock() is called elsewhere. + virtual void run() = 0; // To be overridden by client. Returning causes the thread to block until it is destroyed. +};*/ + + +struct Coroutine /*: IThreadlike*/ { + Coroutine() { + coro = Coro_new(); + if (coro == NULL) + platform::outOfMemory(); + } + + ~Coroutine() { + Coro_free(coro); + } + + void start() { + int result = Coro_startCoro_( swapCoro(coro), coro, this, &entry ); + if (result == ENOMEM) + platform::outOfMemory(); + } + + void unblock() { + //Coro_switchTo_( swapCoro(coro), coro ); + blocked.send(Void()); + } + +protected: + void block() { + //Coro_switchTo_( swapCoro(main_coro), main_coro ); + blocked = Promise(); + double before = now(); + CoroThreadPool::waitFor( blocked.getFuture() ); + if (g_network->isSimulated() && g_simulator.getCurrentProcess()->rebooting) TraceEvent("CoroUnblocked").detail("After", now()-before); + } + + virtual void run() = 0; + +private: + void wrapRun() { + run(); + Coro_switchTo_( swapCoro(main_coro), main_coro ); + //block(); + } + + static void entry(void* _this) { + ((Coroutine*)_this)->wrapRun(); + } + + Coro* coro; + Promise blocked; +}; + +template +class WorkPool : public IThreadPool, public ReferenceCounted> { + struct Worker; + + // Pool can survive the destruction of WorkPool while it waits for workers to terminate + struct Pool : ReferenceCounted { + Mutex queueLock; + Deque work; + std::vector idle, workers; + ActorCollection anyError, allStopped; + Future m_holdRefUntilStopped; + + Pool() : anyError(false), allStopped(true) { + m_holdRefUntilStopped = holdRefUntilStopped(this); + } + + ~Pool() { + for(int c=0; c holdRefUntilStopped( Pool* p ) { + p->addref(); + Void _ = wait( p->allStopped.getResult() ); + p->delref(); + return Void(); + } + }; + + struct Worker : Threadlike { + Pool* pool; + IThreadPoolReceiver* userData; + bool stop; + ThreadReturnPromise stopped; + ThreadReturnPromise error; + + Worker( Pool* pool, IThreadPoolReceiver* userData ) : pool(pool), userData(userData), stop(false) { + } + + virtual void run() { + try { + if(!stop) + userData->init(); + + while (!stop) { + pool->queueLock.enter(); + if (pool->work.empty()) { + pool->idle.push_back( this ); + pool->queueLock.leave(); + Threadlike::block(); + } else { + PThreadAction a = pool->work.front(); + pool->work.pop_front(); + pool->queueLock.leave(); + (*a)(userData); + if(IS_CORO) CoroThreadPool::waitFor(yield()); + } + } + + TraceEvent("CoroStop"); + delete userData; + stopped.send(Void()); + return; + } catch (Error& e) { + TraceEvent("WorkPoolError").error(e, true); + error.sendError(e); + } catch (...) { + TraceEvent("WorkPoolError"); + error.sendError(unknown_error()); + } + + try { + delete userData; + } catch (...) { + TraceEvent(SevError, "WorkPoolErrorShutdownError"); + } + stopped.send(Void()); + } + }; + + Reference pool; + Future m_stopOnError; // must be last, because its cancellation calls stop()! + Error error; + + ACTOR Future stopOnError( WorkPool* w ) { + try { + Void _ = wait( w->getError() ); + } catch (Error& e) { + w->error = e; + } + w->stop(); + return Void(); + } + + void checkError() { + if (error.code() != invalid_error_code) { + ASSERT( error.code() != error_code_success ); // Calling post or addThread after stop is an error + throw error; + } + } + +public: + WorkPool() : pool( new Pool ) { + m_stopOnError = stopOnError( this ); + } + + virtual Future getError() { return pool->anyError.getResult(); } + virtual void addThread( IThreadPoolReceiver* userData ) { + checkError(); + + auto w = new Worker(pool.getPtr(), userData); + pool->queueLock.enter(); + pool->workers.push_back( w ); + pool->queueLock.leave(); + pool->anyError.add( w->error.getFuture() ); + pool->allStopped.add( w->stopped.getFuture() ); + startWorker(w); + } + ACTOR static void startWorker( Worker* w ) { + // We want to make sure that coroutines are always started after Net2::run() is called, so the main coroutine is + // initialized. + Void _ = wait( delay(0, g_network->getCurrentTask() )); + w->start(); + } + virtual void post( PThreadAction action ) { + checkError(); + + pool->queueLock.enter(); + pool->work.push_back(action); + if (!pool->idle.empty()) { + Worker* c = pool->idle.back(); + pool->idle.pop_back(); + pool->queueLock.leave(); + c->unblock(); + } else + pool->queueLock.leave(); + } + virtual Future stop() { + if (error.code() == invalid_error_code) error = success(); + + pool->queueLock.enter(); + TraceEvent("WorkPool_Stop").detail("Workers", pool->workers.size()).detail("Idle", pool->idle.size()) + .detail("Work", pool->work.size()); + + for(int i=0; iwork.size(); i++) + pool->work[i]->cancel(); // What if cancel() does something to this? + pool->work.clear(); + for(int i=0; iworkers.size(); i++) + pool->workers[i]->stop = true; + + std::vector idle; + std::swap(idle, pool->idle); + pool->queueLock.leave(); + + for(int i=0; iunblock(); + + pool->allStopped.add( Void() ); + + return pool->allStopped.getResult(); + } + virtual bool isCoro() const { return IS_CORO; } + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } +}; + +typedef WorkPool CoroPool; + + + +ACTOR void coroSwitcher( Future what, int taskID, Coro* coro ) { + try { + state double t = now(); + Void _ = wait(what); + //if (g_network->isSimulated() && g_simulator.getCurrentProcess()->rebooting && now()!=t) + // TraceEvent("NonzeroWaitDuringReboot").detail("TaskID", taskID).detail("Elapsed", now()-t).backtrace("Flow"); + } catch (Error&) {} + Void _ = wait( delay(0, taskID) ); + Coro_switchTo_( swapCoro(coro), coro ); +} + + + +void CoroThreadPool::waitFor( Future what ) { + ASSERT (current_coro != main_coro); + if (what.isReady()) return; + Coro* c = current_coro; + double t = now(); + coroSwitcher( what, g_network->getCurrentTask(), current_coro ); + Coro_switchTo_( swapCoro(main_coro), main_coro ); + //if (g_network->isSimulated() && g_simulator.getCurrentProcess()->rebooting && now()!=t) + // TraceEvent("NonzeroWaitDuringReboot").detail("TaskID", currentTaskID).detail("Elapsed", now()-t).backtrace("Coro"); + ASSERT( what.isReady() ); +} + +// Right After INet2::run +void CoroThreadPool::init() +{ + if (!current_coro) { + current_coro = main_coro = Coro_new(); + if (main_coro == NULL) + platform::outOfMemory(); + + Coro_initializeMainCoro(main_coro); + //printf("Main thread: %d bytes stack presumed available\n", Coro_bytesLeftOnStack(current_coro)); + } +} + + +Reference CoroThreadPool::createThreadPool() { + return Reference( new CoroPool ); +} diff --git a/fdbserver/CoroFlow.h b/fdbserver/CoroFlow.h new file mode 100644 index 0000000000..47668b1bdb --- /dev/null +++ b/fdbserver/CoroFlow.h @@ -0,0 +1,57 @@ +/* + * CoroFlow.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// CoroFlow.h - Tools for interoperating with flow (futures etc) from coroutines instead of actors + +#ifndef FDBSERVER_COROFLOW_H +#define FDBSERVER_COROFLOW_H +#pragma once + +#include "fdbrpc/fdbrpc.h" +#include "flow/IThreadPool.h" + +class CoroThreadPool { +public: + + static void init(); + static void waitFor( Future what ); + + static Reference createThreadPool(); + +protected: + CoroThreadPool() {} + ~CoroThreadPool() {} +}; + + +template +inline T waitForAndGet( Future f ) { + if( !f.isReady() ) + CoroThreadPool::waitFor( success(f) ); + return f.get(); +} + +inline void waitFor( Future f ) { + CoroThreadPool::waitFor(f); + if (f.isError()) throw f.getError(); +} + + +#endif \ No newline at end of file diff --git a/fdbserver/DBCoreState.h b/fdbserver/DBCoreState.h new file mode 100644 index 0000000000..26717ef1b6 --- /dev/null +++ b/fdbserver/DBCoreState.h @@ -0,0 +1,159 @@ +/* + * DBCoreState.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_DBCORESTATE_H +#define FDBSERVER_DBCORESTATE_H +#pragma once + +#include "fdbrpc/ReplicationPolicy.h" + +// This structure is stored persistently in CoordinatedState and must be versioned carefully! +// It records a synchronous replication topology which can be used in the absence of faults (or under a limited +// number of failures, in the case of less than full write quorums) to durably commit transactions. When faults or +// configuration changes require the topology to be changed, (a read quorum of) the old topology is locked, a new +// topology is constructed, and then committed to this coordinated state before becoming active. This process +// is called 'recovery'. +// At the moment, transaction logs are replicated but not partitioned, so the topology is as simple as a list of +// transaction log replicas and the write quorum that was used to commit to them. The read quorum required to +// ensure durability of locking and recovery is therefore tLogWriteAntiQuorum + 1. + +struct OldTLogCoreData { + vector< UID > tLogs; + int32_t tLogWriteAntiQuorum; + int32_t tLogReplicationFactor; + std::vector< LocalityData > tLogLocalities; // Stores the localities of the log servers + IRepPolicyRef tLogPolicy; + Version epochEnd; + + OldTLogCoreData() : tLogWriteAntiQuorum(0), tLogReplicationFactor(0), epochEnd(0) {} + + bool operator == (OldTLogCoreData const& rhs) const { return tLogs == rhs.tLogs && tLogWriteAntiQuorum == rhs.tLogWriteAntiQuorum && tLogReplicationFactor == rhs.tLogReplicationFactor && epochEnd == rhs.epochEnd && ((!tLogPolicy && !rhs.tLogPolicy) || (tLogPolicy && rhs.tLogPolicy && (tLogPolicy->info() == rhs.tLogPolicy->info()))); } + + template + void serialize(Archive& ar) { + ar & tLogs & tLogWriteAntiQuorum & tLogReplicationFactor & tLogPolicy & epochEnd & tLogLocalities; + } +}; + +struct DBCoreState { + vector< UID > tLogs; + int32_t tLogWriteAntiQuorum; // The write anti quorum previously used to write to tLogs, which might be different from the anti quorum suggested by the current configuration going forward! + int32_t tLogReplicationFactor; // The replication factor previously used to write to tLogs, which might be different from the current configuration + + IRepPolicyRef tLogPolicy; + DBRecoveryCount recoveryCount; // Increases with sequential successful recoveries. + int logSystemType; + std::vector< LocalityData > tLogLocalities; // Stores the localities of the log servers + + std::vector oldTLogData; + + DBCoreState() : recoveryCount(0), tLogWriteAntiQuorum(0), tLogReplicationFactor(0), + logSystemType(0) {} + + + vector getPriorCommittedLogServers() { + vector priorCommittedLogServers; + for(int i = 0; i < oldTLogData.size(); i++) { + for(auto it : oldTLogData[i].tLogs) { + priorCommittedLogServers.push_back(it); + } + } + return priorCommittedLogServers; + } + + bool isEqual(DBCoreState const& r) const { + if (logSystemType != r.logSystemType || recoveryCount != r.recoveryCount || tLogWriteAntiQuorum != r.tLogWriteAntiQuorum || tLogReplicationFactor != r.tLogReplicationFactor || tLogs.size() != r.tLogs.size() || oldTLogData.size() != r.oldTLogData.size() || tLogLocalities != r.tLogLocalities || tLogs != r.tLogs) + return false; + for(int i = 0; i < oldTLogData.size(); i++ ) { + if (oldTLogData[i] != r.oldTLogData[i]) + return false; + } + return true; + } + bool operator == ( const DBCoreState& rhs ) const { return isEqual(rhs); } + + template + void serialize(Archive& ar) { + UID functionId = g_nondeterministic_random->randomUniqueID(); + ASSERT( ar.protocolVersion() >= 0x0FDB00A320050001LL ); + ar & tLogs & tLogWriteAntiQuorum & recoveryCount & tLogReplicationFactor & logSystemType; + if( ar.protocolVersion() >= 0x0FDB00A460010001LL) { + uint64_t tLocalitySize = (uint64_t)tLogLocalities.size(); + ar & oldTLogData & tLogPolicy & tLocalitySize; + if (ar.isDeserializing) { + tLogLocalities.reserve(tLocalitySize); + for (size_t i = 0; i < tLocalitySize; i++) { + LocalityData locality; + ar & locality; + tLogLocalities.push_back(locality); + } + } + else { + for (auto& locality : tLogLocalities) { + ar & locality; + } + } + } + else if(ar.isDeserializing) { + oldTLogData.clear(); + oldTLogData.push_back(OldTLogCoreData()); + ar & oldTLogData[0].tLogs & oldTLogData[0].epochEnd & oldTLogData[0].tLogWriteAntiQuorum & oldTLogData[0].tLogReplicationFactor; + tLogPolicy = IRepPolicyRef(new PolicyAcross(tLogReplicationFactor, "zoneid", IRepPolicyRef(new PolicyOne()))); + if(!oldTLogData[0].tLogs.size()) { + oldTLogData.pop_back(); + } + else { + for(int i = 0; i < oldTLogData.size(); i++ ) { + oldTLogData[i].tLogPolicy = IRepPolicyRef(new PolicyAcross(oldTLogData[i].tLogReplicationFactor, "zoneid", IRepPolicyRef(new PolicyOne()))); + if (oldTLogData[i].tLogs.size()) + { + oldTLogData[i].tLogLocalities.reserve(oldTLogData[i].tLogs.size()); + for (auto& tLog : oldTLogData[i].tLogs) { + LocalityData locality; + locality.set(LocalityData::keyZoneId, g_random->randomUniqueID().toString()); + locality.set(LocalityData::keyDataHallId, LiteralStringRef("0")); + oldTLogData[i].tLogLocalities.push_back(locality); + } + } + } + } + tLogLocalities.reserve(tLogs.size()); + for (auto& tLog : tLogs) { + LocalityData locality; + locality.set(LocalityData::keyZoneId, g_random->randomUniqueID().toString()); + locality.set(LocalityData::keyDataHallId, LiteralStringRef("0")); + tLogLocalities.push_back(locality); + } + } + + TraceEvent("CoreStateSerialize").detail("AntiQuorum", tLogWriteAntiQuorum) + .detail("logSystemType", logSystemType).detail("recoveryCount", recoveryCount) + .detail("tLogReplicationFactor", tLogReplicationFactor) + .detail("tLogPolicy", (tLogPolicy.getPtr()) ? tLogPolicy->info() : "[unset]") + .detail("logs", describe(tLogs)).detail("procotol", ar.protocolVersion()) + .detail("oldTLogData", oldTLogData.size()) + .detail("deserializing", ar.isDeserializing); + } + + std::string toString() const { return format("type: %d anti: %d replication: %d policy: %s tLogs: %s oldGenerations: %d tlocalities: %s", + logSystemType, tLogWriteAntiQuorum, tLogReplicationFactor, (tLogPolicy ? tLogPolicy->info().c_str() : "[unset]"), describe(tLogs).c_str(), oldTLogData.size(), describe(tLogLocalities).c_str()); } +}; + +#endif diff --git a/fdbserver/DataDistribution.actor.cpp b/fdbserver/DataDistribution.actor.cpp new file mode 100644 index 0000000000..44ce52b1d3 --- /dev/null +++ b/fdbserver/DataDistribution.actor.cpp @@ -0,0 +1,2281 @@ +/* + * DataDistribution.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/ActorCollection.h" +#include "DataDistribution.h" +#include "fdbclient/SystemData.h" +#include "fdbclient/DatabaseContext.h" +#include "MoveKeys.h" +#include "Knobs.h" +#include +#include "WaitFailure.h" +#include "ServerDBInfo.h" +#include "IKeyValueStore.h" +#include "fdbclient/ManagementAPI.h" +#include "fdbrpc/Replication.h" +#include "flow/UnitTest.h" + +class TCTeamInfo; + +struct TCServerInfo : public ReferenceCounted { + UID id; + StorageServerInterface lastKnownInterface; + ProcessClass lastKnownClass; + vector> teams; + Future tracker; + int64_t dataInFlightToServer; + ErrorOr serverMetrics; + Promise> interfaceChanged; + Future> onInterfaceChanged; + Promise removed; + Future onRemoved; + Promise wakeUpTracker; + + TCServerInfo(StorageServerInterface ssi, ProcessClass processClass) : id(ssi.id()), lastKnownInterface(ssi), lastKnownClass(processClass), dataInFlightToServer(0), onInterfaceChanged(interfaceChanged.getFuture()), onRemoved(removed.getFuture()) {} +}; + +ACTOR Future updateServerMetrics( TCServerInfo *server ) { + state StorageServerInterface ssi = server->lastKnownInterface; + state Future> metricsRequest = ssi.getPhysicalMetrics.tryGetReply( GetPhysicalMetricsRequest(), TaskDataDistributionLaunch ); + state Future resetRequest = Never(); + state Future> interfaceChanged( server->onInterfaceChanged ); + state Future serverRemoved( server->onRemoved ); + + loop { + choose { + when( ErrorOr rep = wait( metricsRequest ) ) { + if( rep.present() ) { + server->serverMetrics = rep; + return Void(); + } + metricsRequest = Never(); + resetRequest = delay( SERVER_KNOBS->METRIC_DELAY, TaskDataDistributionLaunch ); + } + when( std::pair _ssi = wait( interfaceChanged ) ) { + ssi = _ssi.first; + interfaceChanged = server->onInterfaceChanged; + resetRequest = Void(); + } + when( Void _ = wait( serverRemoved ) ) { + return Void(); + } + when( Void _ = wait( resetRequest ) ) { //To prevent a tight spin loop + if(IFailureMonitor::failureMonitor().getState(ssi.getPhysicalMetrics.getEndpoint()).isFailed()) { + resetRequest = IFailureMonitor::failureMonitor().onStateEqual(ssi.getPhysicalMetrics.getEndpoint(), FailureStatus(false)); + } + else { + resetRequest = Never(); + metricsRequest = ssi.getPhysicalMetrics.tryGetReply( GetPhysicalMetricsRequest(), TaskDataDistributionLaunch ); + } + } + } + } +} + +ACTOR Future updateServerMetrics( Reference server ) { + Void _ = wait( updateServerMetrics( server.getPtr() ) ); + return Void(); +} + +class TCTeamInfo : public ReferenceCounted, public IDataDistributionTeam { +public: + vector< Reference > servers; + vector serverIDs; + Future tracker; + bool healthy; + bool wrongConfiguration; //True if any of the servers in the team have the wrong configuration + int priority; + + TCTeamInfo( vector< Reference > const& servers ) + : servers(servers), healthy(true), priority(PRIORITY_TEAM_HEALTHY), wrongConfiguration(false) + { + serverIDs.reserve(servers.size()); + for(int i=0; iid); + } + virtual vector getLastKnownServerInterfaces() { + vector v; + v.reserve(servers.size()); + for(int i=0; ilastKnownInterface); + return v; + } + virtual vector const& getServerIDs() { return serverIDs; } + virtual void addDataInFlightToTeam( int64_t delta ) { + for(int i=0; idataInFlightToServer += delta; + } + virtual int64_t getDataInFlightToTeam() { + int64_t dataInFlight = 0.0; + for(int i=0; idataInFlightToServer; + return dataInFlight; + } + + virtual int64_t getLoadBytes( bool includeInFlight = true, double inflightPenalty = 1.0 ) { + int64_t physicalBytes = getLoadAverage(); + double minFreeSpaceRatio = getMinFreeSpaceRatio(includeInFlight); + int64_t inFlightBytes = includeInFlight ? getDataInFlightToTeam() / servers.size() : 0; + double freeSpaceMultiplier = SERVER_KNOBS->FREE_SPACE_RATIO_CUTOFF / ( std::max( std::min( SERVER_KNOBS->FREE_SPACE_RATIO_CUTOFF, minFreeSpaceRatio ), 0.000001 ) ); + + if(freeSpaceMultiplier > 1 && g_random->random01() < 0.001) + TraceEvent(SevWarn, "DiskNearCapacity").detail("FreeSpaceRatio", minFreeSpaceRatio); + + return (physicalBytes + (inflightPenalty*inFlightBytes)) * freeSpaceMultiplier; + } + + virtual int64_t getMinFreeSpace( bool includeInFlight = true ) { + int64_t minFreeSpace = std::numeric_limits::max(); + for(int i=0; iserverMetrics.present() ) { + auto& replyValue = servers[i]->serverMetrics.get(); + + ASSERT(replyValue.free.bytes >= 0); + ASSERT(replyValue.capacity.bytes >= 0); + + int64_t bytesFree = replyValue.free.bytes; + if(includeInFlight) { + bytesFree -= servers[i]->dataInFlightToServer; + } + + minFreeSpace = std::min(bytesFree, minFreeSpace); + } + } + + return minFreeSpace; // Could be negative + } + + virtual double getMinFreeSpaceRatio( bool includeInFlight = true ) { + double minRatio = 1.0; + for(int i=0; iserverMetrics.present() ) { + auto& replyValue = servers[i]->serverMetrics.get(); + + ASSERT(replyValue.free.bytes >= 0); + ASSERT(replyValue.capacity.bytes >= 0); + + int64_t bytesFree = replyValue.free.bytes; + if(includeInFlight) { + bytesFree = std::max((int64_t)0, bytesFree - servers[i]->dataInFlightToServer); + } + + if(replyValue.capacity.bytes == 0) + minRatio = 0; + else + minRatio = std::min( minRatio, ((double)bytesFree) / replyValue.capacity.bytes ); + } + } + + return minRatio; + } + + virtual bool hasHealthyFreeSpace() { + return getMinFreeSpaceRatio() > SERVER_KNOBS->MIN_FREE_SPACE_RATIO && getMinFreeSpace() > SERVER_KNOBS->MIN_FREE_SPACE; + } + + virtual Future updatePhysicalMetrics() { + return doUpdatePhysicalMetrics( this ); + } + + virtual bool isOptimal() { + for(int i=0; ilastKnownClass.machineClassFitness( ProcessClass::Storage ) > ProcessClass::UnsetFit ) { + return false; + } + } + return true; + } + + virtual bool isWrongConfiguration() { return wrongConfiguration; } + virtual void setWrongConfiguration(bool wrongConfiguration) { this->wrongConfiguration = wrongConfiguration; } + virtual bool isHealthy() { return healthy; } + virtual void setHealthy(bool h) { healthy = h; } + virtual int getPriority() { return priority; } + virtual void setPriority(int p) { priority = p; } + virtual void addref() { ReferenceCounted::addref(); } + virtual void delref() { ReferenceCounted::delref(); } + +private: + // Calculate an "average" of the metrics replies that we received. Penalize teams from which we did not receieve all replies. + int64_t getLoadAverage() { + int64_t bytesSum = 0; + int added = 0; + for(int i=0; iserverMetrics.present() ) { + added++; + bytesSum += servers[i]->serverMetrics.get().load.bytes; + } + + if( added < servers.size() ) + bytesSum *= 2; + + return added == 0 ? 0 : bytesSum / added; + } + + // Calculate the max of the metrics replies that we received. + + + ACTOR Future doUpdatePhysicalMetrics( TCTeamInfo* self ) { + std::vector> updates; + for( int i = 0; i< self->servers.size(); i++ ) + updates.push_back( updateServerMetrics( self->servers[i] ) ); + Void _ = wait( waitForAll( updates ) ); + return Void(); + } +}; + +struct ServerStatus { + bool isFailed; + bool isUndesired; + bool isWrongConfiguration; + LocalityData locality; + ServerStatus() : isFailed(true), isUndesired(false), isWrongConfiguration(false) {} + ServerStatus( bool isFailed, bool isUndesired, LocalityData const& locality ) : isFailed(isFailed), isUndesired(isUndesired), locality(locality), isWrongConfiguration(false) {} + bool isUnhealthy() const { return isFailed || isUndesired; } + const char* toString() const { return isFailed ? "Failed" : isUndesired ? "Undesired" : "Healthy"; } + + bool operator == (ServerStatus const& r) const { return isFailed == r.isFailed && isUndesired == r.isUndesired && isWrongConfiguration == r.isWrongConfiguration && locality.zoneId() == r.locality.zoneId(); } + + //If a process has reappeared without the storage server that was on it (isFailed == true), we don't need to exclude it + //We also don't need to exclude processes who are in the wrong configuration (since those servers will be removed) + bool excludeOnRecruit() { return !isFailed && !isWrongConfiguration; } +}; +typedef AsyncMap ServerStatusMap; + +ACTOR Future waitForAllDataRemoved( Database cx, UID serverID ) { + state Transaction tr(cx); + loop { + try { + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + bool canRemove = wait( canRemoveStorageServer( &tr, serverID ) ); + if (canRemove) + return Void(); + + // Wait for any change to the serverKeys for this server + Void _ = wait( delay(SERVER_KNOBS->ALL_DATA_REMOVED_DELAY, TaskDataDistribution) ); + //Void _ = tr.waitForChanges( KeyRangeRef( serverKeysPrefixFor(serverID), + // serverKeysPrefixFor(serverID).toString() + allKeys.end.toString() ) ); + tr.reset(); + } catch (Error& e) { + Void _ = wait( tr.onError(e) ); + } + } +} + +ACTOR Future storageServerFailureTracker( + Database cx, + StorageServerInterface server, + ServerStatusMap *statusMap, + ServerStatus *status, + PromiseStream serverFailures, + int64_t *unhealthyServers, + UID masterId ) +{ + loop { + bool unhealthy = statusMap->count(server.id()) && statusMap->get(server.id()).isUnhealthy(); + if(unhealthy && !status->isUnhealthy()) { + (*unhealthyServers)--; + } + if(!unhealthy && status->isUnhealthy()) { + (*unhealthyServers)++; + } + + statusMap->set( server.id(), *status ); + if( status->isFailed ) + serverFailures.send( Void() ); + + choose { + when ( Void _ = wait( status->isFailed + ? IFailureMonitor::failureMonitor().onStateEqual( server.waitFailure.getEndpoint(), FailureStatus(false) ) + : waitFailureClient(server.waitFailure, SERVER_KNOBS->DATA_DISTRIBUTION_FAILURE_REACTION_TIME, 0, TaskDataDistribution) ) ) + { + status->isFailed = !status->isFailed; + TraceEvent("StatusMapChange", masterId).detail("ServerID", server.id()).detail("Status", status->toString()). + detail("Available", IFailureMonitor::failureMonitor().getState(server.waitFailure.getEndpoint()).isAvailable()); + } + when ( Void _ = wait( status->isUnhealthy() ? waitForAllDataRemoved(cx, server.id()) : Never() ) ) { break; } + } + } + + return Void(); +} + +// Read keyservers, return unique set of teams +ACTOR Future> getInitialDataDistribution( Database cx, UID masterId, MoveKeysLock moveKeysLock ) { + state Reference result = Reference(new InitialDataDistribution); + state Key beginKey = allKeys.begin; + + state bool succeeded; + + state Transaction tr( cx ); + + //Get the server list in its own try/catch block since it modifies result. We don't want a subsequent failure causing entries to be duplicated + loop { + succeeded = false; + try { + result->mode = 1; + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + Optional mode = wait( tr.get( dataDistributionModeKey ) ); + if (mode.present()) { + BinaryReader rd( mode.get(), Unversioned() ); + rd >> result->mode; + } + if(!result->mode) + return result; + + + state Future> workers = getWorkers(&tr); + state Future> serverList = tr.getRange( serverListKeys, CLIENT_KNOBS->TOO_MANY ); + Void _ = wait( success(workers) && success(serverList) ); + ASSERT( !serverList.get().more && serverList.get().size() < CLIENT_KNOBS->TOO_MANY ); + + std::map>, ProcessData> id_data; + for( int i = 0; i < workers.get().size(); i++ ) + id_data[workers.get()[i].locality.processId()] = workers.get()[i]; + + succeeded = true; + + for( int i = 0; i < serverList.get().size(); i++ ) { + auto ssi = decodeServerListValue( serverList.get()[i].value ); + result->allServers.push_back( std::make_pair(ssi, id_data[ssi.locality.processId()].processClass) ); + } + + break; + } + catch(Error &e) { + Void _ = wait( tr.onError(e) ); + + ASSERT(!succeeded); //We shouldn't be retrying if we have already started modifying result in this loop + TraceEvent("getInitialTeamsRetry", masterId); + } + } + + //If keyServers is too large to read in a single transaction, then we will have to break this process up into multiple transactions. + //In that case, each iteration should begin where the previous left off + while(beginKey < allKeys.end) { + TEST(beginKey > allKeys.begin); //Multi-transactional getInitialDataDistribution + loop { + succeeded = false; + try { + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + Void _ = wait(checkMoveKeysLockReadOnly(&tr, moveKeysLock)); + Standalone keyServers = wait(krmGetRanges(&tr, keyServersPrefix, KeyRangeRef(beginKey, allKeys.end), SERVER_KNOBS->MOVE_KEYS_KRM_LIMIT, SERVER_KNOBS->MOVE_KEYS_KRM_LIMIT_BYTES)); + succeeded = true; + + vector src, dest, last; + + // for each range + for(int i = 0; i < keyServers.size() - 1; i++) { + KeyRangeRef keys( keyServers[i].key, keyServers[i+1].key ); + decodeKeyServersValue( keyServers[i].value, src, dest ); + std::pair,vector> teams; + for(int j=0; jshards.push_back( keyRangeWith(keys, teams) ); + result->teams.insert( teams.first ); + if (dest.size()) + result->teams.insert( teams.second ); + } + + ASSERT(keyServers.size() > 0); + beginKey = keyServers.end()[-1].key; + break; + } catch (Error& e) { + Void _ = wait( tr.onError(e) ); + + ASSERT(!succeeded); //We shouldn't be retrying if we have already started modifying result in this loop + TraceEvent("getInitialTeamsKeyServersRetry", masterId); + } + } + + tr.reset(); + } + + // a dummy shard at the end with no keys or servers makes life easier for trackInitialShards() + result->shards.push_back( keyRangeWith(KeyRangeRef(allKeys.end,allKeys.end), std::pair, vector>()) ); + + return result; +} + +Future storageServerTracker( + struct DDTeamCollection* const& self, + Database const& cx, + TCServerInfo* const& server, + ServerStatusMap* const& statusMap, + MoveKeysLock const& lock, + UID const& masterId, + std::map>* const& other_servers, + PromiseStream< std::pair> > const& changes, + PromiseStream const& serverFailures, + Promise const& errorOut); + +Future teamTracker( struct DDTeamCollection* const& self, Reference const& team ); + +struct DDTeamCollection { + enum { REQUESTING_WORKER = 0, GETTING_WORKER = 1, GETTING_STORAGE = 2 }; + + PromiseStream> addActor; + Database cx; + UID masterId; + int teamSize; + int minDataCenters, desiredDataCenters; + IRepPolicyRef replicationPolicy; + KeyValueStoreType storeType; + + bool doBuildTeams; + Future teamBuilder; + AsyncTrigger restartTeamBuilder; + + MoveKeysLock lock; + PromiseStream output; + vector allServers; + ServerStatusMap server_status; + int64_t unhealthyServers; + std::map> server_info; + vector> teams; + Reference shardsAffectedByTeamFailure; + PromiseStream removedServers; + std::set recruitingIds; // The IDs of the SS which are being recruited + std::set recruitingLocalities; + PromiseStream< std::pair> > serverChanges; + PromiseStream serverFailures; + Future initialFailureReactionDelay; + Future initializationDoneActor; + Promise serverTrackerErrorOut; + AsyncVar recruitingStream; + Debouncer restartRecruiting; + + int healthyTeamCount; + PromiseStream zeroHealthyTeams; + + int optimalTeamCount; + PromiseStream optimalTeamChange; + Promise onOptimalTeamChange; + + AsyncMap< AddressExclusion, bool > excludedServers; // true if an address is in the excluded list in the database. Updated asynchronously (eventually) + + DDTeamCollection( + Database const& cx, + UID masterId, + MoveKeysLock const& lock, + PromiseStream const& output, + Reference const& shardsAffectedByTeamFailure, + int teamSize, + int minDataCenters, + int desiredDataCenters, + IRepPolicyRef replicationPolicy, + KeyValueStoreType storeType, + PromiseStream< std::pair> > const& serverChanges ) + :cx(cx), masterId(masterId), lock(lock), output(output), shardsAffectedByTeamFailure(shardsAffectedByTeamFailure), doBuildTeams( true ), teamBuilder( Void() ), + teamSize( teamSize ), minDataCenters( minDataCenters ), desiredDataCenters( desiredDataCenters ), replicationPolicy(replicationPolicy), storeType( storeType ), serverChanges(serverChanges), + initialFailureReactionDelay( delay( BUGGIFY ? 0 : SERVER_KNOBS->INITIAL_FAILURE_REACTION_DELAY, TaskDataDistribution ) ), healthyTeamCount( 0 ), + initializationDoneActor(logOnCompletion(initialFailureReactionDelay, this)), optimalTeamCount( 0 ), recruitingStream(0), restartRecruiting( SERVER_KNOBS->DEBOUNCE_RECRUITING_DELAY ), + unhealthyServers(0) + { + TraceEvent("DDTrackerStarting", masterId) + .detail( "State", "Inactive" ) + .trackLatest( format("%s/DDTrackerStarting", printable(cx->dbName).c_str() ).c_str() ); + } + + ~DDTeamCollection() { + // The following kills a reference cycle between the teamTracker actor and the TCTeamInfo that both holds and is held by the actor + // It also ensures that the trackers are done fiddling with healthyTeamCount before we free this + for(int i=0; i < teams.size(); i++) { + teams[i]->tracker.cancel(); + } + // The following makes sure that, even if a reference to a team is held in the DD Queue, the tracker will be stopped + // before the server_status map to which it has a pointer, is destroyed. + for(auto it = server_info.begin(); it != server_info.end(); ++it) { + it->second->tracker.cancel(); + } + + teamBuilder.cancel(); + } + + ACTOR Future logOnCompletion( Future signal, DDTeamCollection *self ) { + Void _ = wait(signal); + Void _ = wait(delay(SERVER_KNOBS->LOG_ON_COMPLETION_DELAY, TaskDataDistribution)); + + TraceEvent("DDTrackerStarting", self->masterId) + .detail( "State", "Active" ) + .trackLatest( format("%s/DDTrackerStarting", printable(self->cx->dbName).c_str() ).c_str() ); + + return Void(); + } + + ACTOR Future checkBuildTeams( DDTeamCollection* self ) { + state Promise restart; + + while( !self->teamBuilder.isReady() ) + Void _ = wait( self->teamBuilder ); + + if( self->doBuildTeams ) { + self->doBuildTeams = false; + try { + loop { + Promise oldRestart = restart; + restart = Promise(); + self->teamBuilder = self->buildTeams( self ) || restart.getFuture(); + oldRestart.send( Void() ); + choose { + when( Void _ = wait( self->teamBuilder ) ) { break; } + when( Void _ = wait( self->restartTeamBuilder.onTrigger() ) ) {} + } + } + } + catch(Error &e) { + if(!restart.isSet()) { + restart.send(Void()); + } + throw; + } + } + + return Void(); + } + + // SOMEDAY: Make bestTeam better about deciding to leave a shard where it is (e.g. in PRIORITY_TEAM_HEALTHY case) + // use keys, src, dest, metrics, priority, system load, etc.. to decide... + ACTOR Future getTeam( DDTeamCollection* self, GetTeamRequest req ) { + try { + Void _ = wait( self->checkBuildTeams( self ) ); + + // Select the best team + // Currently the metric is minimum used disk space (adjusted for data in flight) + // Only healthy teams may be selected. The team has to be healthy at the moment we update + // shardsAffectedByTeamFailure or we could be dropping a shard on the floor (since team + // tracking is "edge triggered") + // SOMEDAY: Account for capacity, load (when shardMetrics load is high) + + int64_t bestLoadBytes = 0; + Optional> bestOption; + std::vector>> randomTeams; + + if( !req.wantsNewServers ) { + std::set< UID > sources; + std::vector> similarTeams; + bool foundExact = false; + + for( int i = 0; i < req.sources.size(); i++ ) + sources.insert( req.sources[i] ); + + for( int i = 0; i < req.sources.size(); i++ ) { + if( !self->server_info.count( req.sources[i] ) ) { + TEST( true ); // GetSimilarTeams source server now unknown + } + else { + auto& teamList = self->server_info[ req.sources[i] ]->teams; + for( int j = 0; j < teamList.size(); j++ ) { + if( teamList[j]->isHealthy() && (!req.preferLowerUtilization || teamList[j]->hasHealthyFreeSpace())) { + int sharedMembers = 0; + for( int k = 0; k < teamList[j]->serverIDs.size(); k++ ) + if( sources.count( teamList[j]->serverIDs[k] ) ) + sharedMembers++; + + if( !foundExact && sharedMembers == teamList[j]->serverIDs.size() ) { + foundExact = true; + bestOption = Optional>(); + similarTeams.clear(); + } + + if( (sharedMembers == teamList[j]->serverIDs.size()) || (!foundExact && req.wantsTrueBest) ) { + int64_t loadBytes = SOME_SHARED * teamList[j]->getLoadBytes(true, req.inflightPenalty); + if( !bestOption.present() || ( req.preferLowerUtilization && loadBytes < bestLoadBytes ) || ( !req.preferLowerUtilization && loadBytes > bestLoadBytes ) ) { + bestLoadBytes = loadBytes; + bestOption = teamList[j]; + } + } + else if( !req.wantsTrueBest && !foundExact ) + similarTeams.push_back( teamList[j] ); + } + } + } + } + + if( foundExact || (req.wantsTrueBest && bestOption.present() ) ) { + TraceEvent("getTeam").detail("wantsVariety", req.wantsNewServers).detail("bestOption", bestOption.get()->getDesc()); + ASSERT( bestOption.present() ); + req.reply.send( bestOption ); + return Void(); + } + + if( !req.wantsTrueBest ) { + while( similarTeams.size() && randomTeams.size() < SERVER_KNOBS->BEST_TEAM_OPTION_COUNT ) { + int randomTeam = g_random->randomInt( 0, similarTeams.size() ); + randomTeams.push_back( std::make_pair( SOME_SHARED, similarTeams[randomTeam] ) ); + std::swap( similarTeams[randomTeam], similarTeams.back() ); + similarTeams.pop_back(); + } + } + } + + ASSERT( self->teams.size() ); + + if( req.wantsTrueBest ) { + ASSERT( !bestOption.present() ); + for( int i = 0; i < self->teams.size(); i++ ) { + if( self->teams[i]->isHealthy() && (!req.preferLowerUtilization || self->teams[i]->hasHealthyFreeSpace()) ) { + int64_t loadBytes = NONE_SHARED * self->teams[i]->getLoadBytes(true, req.inflightPenalty); + if( !bestOption.present() || ( req.preferLowerUtilization && loadBytes < bestLoadBytes ) || ( !req.preferLowerUtilization && loadBytes > bestLoadBytes ) ) { + bestLoadBytes = loadBytes; + bestOption = self->teams[i]; + } + } + } + } + else { + int nTries = 0; + while( randomTeams.size() < SERVER_KNOBS->BEST_TEAM_OPTION_COUNT && nTries < SERVER_KNOBS->BEST_TEAM_MAX_TEAM_TRIES ) { + Reference dest = g_random->randomChoice(self->teams); + + bool ok = dest->isHealthy() && (!req.preferLowerUtilization || dest->hasHealthyFreeSpace()); + for(int i=0; ok && igetServerIDs() == dest->getServerIDs()) + ok = false; + + if (ok) + randomTeams.push_back( std::make_pair( NONE_SHARED, dest ) ); + else + nTries++; + } + + for( int i = 0; i < randomTeams.size(); i++ ) { + int64_t loadBytes = randomTeams[i].first * randomTeams[i].second->getLoadBytes(true, req.inflightPenalty); + if( !bestOption.present() || ( req.preferLowerUtilization && loadBytes < bestLoadBytes ) || ( !req.preferLowerUtilization && loadBytes > bestLoadBytes ) ) { + bestLoadBytes = loadBytes; + bestOption = randomTeams[i].second; + } + } + } + req.reply.send( bestOption ); + return Void(); + } catch( Error &e ) { + if( e.code() != error_code_actor_cancelled) + req.reply.sendError( e ); + throw; + } + } + + int64_t getDebugTotalDataInFlight() { + int64_t total = 0; + for(auto itr = server_info.begin(); itr != server_info.end(); ++itr) + total += itr->second->dataInFlightToServer; + return total; + } + + void addSubsetOfEmergencyTeams() { + for( int i = 0; i < teams.size(); i++ ) { + if( teams[i]->servers.size() > teamSize ) { + auto& serverIds = teams[i]->getServerIDs(); + bool foundTeam = false; + for( int j = 0; j < std::max( 1, (int)(serverIds.size() - teamSize + 1) ) && !foundTeam; j++ ) { + auto& serverTeams = server_info[serverIds[j]]->teams; + for( int k = 0; k < serverTeams.size(); k++ ) { + auto &testTeam = serverTeams[k]->getServerIDs(); + bool allInTeam = true; + for( int l = 0; l < testTeam.size(); l++ ) { + if( std::find( serverIds.begin(), serverIds.end(), testTeam[l] ) == serverIds.end() ) { + allInTeam = false; + break; + } + } + if( allInTeam ) { + foundTeam = true; + break; + } + } + } + if( !foundTeam ) { + addTeam(serverIds.begin(), serverIds.begin() + teamSize ); + } + } + } + } + + void init( InitialDataDistribution const& initTeams ) { + // SOMEDAY: If some servers have teams and not others (or some servers have more data than others) and there is an address/locality collision, should + // we preferentially mark the least used server as undesirable? + for(auto i = initTeams.allServers.begin(); i != initTeams.allServers.end(); ++i) + addServer( i->first, i->second, serverTrackerErrorOut ); + + for(auto t = initTeams.teams.begin(); t != initTeams.teams.end(); ++t) { + addTeam(t->begin(), t->end() ); + } + + addSubsetOfEmergencyTeams(); + } + + void evaluateTeamQuality() { + int teamCount = teams.size(), serverCount = allServers.size(); + double teamsPerServer = (double)teamCount * teamSize / serverCount; + + ASSERT( serverCount == server_info.size() ); + + int minTeams = 100000, maxTeams = 0; + double varTeams = 0; + + std::map>, int> machineTeams; + for(auto s = server_info.begin(); s != server_info.end(); ++s) { + if(!server_status.get(s->first).isUndesired) { + int stc = s->second->teams.size(); + minTeams = std::min(minTeams, stc); + maxTeams = std::max(maxTeams, stc); + varTeams += (stc - teamsPerServer)*(stc - teamsPerServer); + machineTeams[s->second->lastKnownInterface.locality.zoneId()] += stc; + } + } + varTeams /= teamsPerServer*teamsPerServer; + + int minMachineTeams = 100000, maxMachineTeams = 0; + for( auto m = machineTeams.begin(); m != machineTeams.end(); ++m ) { + minMachineTeams = std::min( minMachineTeams, m->second ); + maxMachineTeams = std::max( maxMachineTeams, m->second ); + } + + TraceEvent( + minTeams>0 ? SevInfo : SevWarn, + "DataDistributionTeamQuality", masterId) + .detail("Servers", serverCount) + .detail("Teams", teamCount) + .detail("TeamsPerServer", teamsPerServer) + .detail("Variance", varTeams/serverCount) + .detail("ServerMinTeams", minTeams) + .detail("ServerMaxTeams", maxTeams) + .detail("MachineMinTeams", minMachineTeams) + .detail("MachineMaxTeams", maxMachineTeams); + } + + bool teamExists( vector &team ) { + bool exists = false; + for (int i=0;igetServerIDs() == team) { + exists = true; + break; + } + } + return exists; + } + + void addTeam( std::set const& team ) { + addTeam(team.begin(), team.end()); + } + + template + void addTeam( InputIt begin, InputIt end) { + vector< Reference > newTeamServers; + for(auto i = begin; i != end; ++i) { + if(server_info[*i]) { + newTeamServers.push_back( server_info[ *i ] ); + } + else { + TraceEvent(SevError, "DDTeamCollection_addedTeamNotInCollection"); + } + } + Reference teamInfo( new TCTeamInfo( newTeamServers ) ); + TraceEvent("TeamCreation", masterId).detail("Team", teamInfo->getDesc()); + teamInfo->tracker = teamTracker( this, teamInfo ); + teams.push_back( teamInfo ); + for (int i=0;iid ]->teams.push_back( teamInfo ); + } + } + + ACTOR Future addAllTeams( DDTeamCollection *self, int location, vector* history, Reference> processes, vector>* output, int teamLimit, int* addedTeams ) { + Void _ = wait( yield( TaskDataDistributionLaunch ) ); + + // Add team, if valid + if(history->size() == self->teamSize) { + auto valid = self->replicationPolicy->validate(*history, processes); + if(!valid) { + return Void(); + } + std::vector team; + for(auto it = history->begin(); it != history->end(); it++) { + team.push_back(*processes->getObject(*it)); + } + + if( !self->teamExists(team) && *addedTeams < teamLimit ) { + output->push_back(team); + (*addedTeams)++; + } + return Void(); + } + + //loop through remaining potential team members, add one and recursively call function + for(; location < processes->size(); location++) { + history->push_back(processes->getEntry(location)); + state int depth = history->size(); + Void _ = wait( self->addAllTeams( self, location + 1, history, processes, output, teamLimit, addedTeams ) ); + ASSERT( history->size() == depth); // the "stack" should be unchanged by this call + history->pop_back(); + if(*addedTeams > teamLimit) + break; + } + + return Void(); + } + + ACTOR Future addAllTeams( DDTeamCollection *self, vector input, vector>* output, int teamLimit ) { + state int addedTeams = 0; + state vector history; + state Reference> processes(new LocalityMap()); + for(auto it = input.begin(); it != input.end(); it++) { + if(self->server_info[*it]) { + processes->add(self->server_info[*it]->lastKnownInterface.locality, &*it); + } + } + Void _ = wait( self->addAllTeams( self, 0, &history, processes, output, teamLimit, &addedTeams ) ); + return addedTeams; + } + + int addTeamsBestOf( int teamsToBuild ) { + int addedTeams = 0; + + LocalityMap totalServers; + + for(auto i = server_info.begin(); i != server_info.end(); ++i) { + if (!server_status.get(i->first).isUndesired) { + auto& id = i->first; + auto& locality = i->second->lastKnownInterface.locality; + totalServers.add(locality, &id); + } + } + + if(totalServers.size() < teamSize ) { + TraceEvent(SevWarn, "DataDistributionBuildTeams", masterId).detail("Reason","Not enough servers for a team").detail("Servers",totalServers.size()).detail("teamSize", teamSize); + return addedTeams; + } + + int loopCount = 0; + // add teams + while( addedTeams < teamsToBuild ) { + std::vector leastUsedServers; + int minTeamCount = CLIENT_KNOBS->TOO_MANY; + for(int i = 0; i < totalServers.size(); i++) { + LocalityEntry process = totalServers.getEntry(i); + UID id = *totalServers.getObject(process); + int teamCount = server_info[id]->teams.size(); + if(teamCount < minTeamCount) { + leastUsedServers.clear(); + minTeamCount = teamCount; + } + if(teamCount <= minTeamCount) { + leastUsedServers.push_back(process); + } + } + + std::vector team; + std::vector forcedAttributes; + + if (leastUsedServers.size()) { + forcedAttributes.push_back(g_random->randomChoice(leastUsedServers)); + } + + std::vector bestTeam; + int bestScore = CLIENT_KNOBS->TOO_MANY; + + int maxAttempts = SERVER_KNOBS->BEST_OF_AMT; + for( int i = 0; i < maxAttempts && i < 100; i++) { + team.clear(); + auto success = totalServers.selectReplicas(replicationPolicy, forcedAttributes, team); + if(!success) { + break; + } + + if(forcedAttributes.size() > 0) { + team.push_back((UID*)totalServers.getObject(forcedAttributes[0])); + } + if( team.size() != teamSize) { + maxAttempts += 1; + } + + int score = 0; + for(auto process = team.begin(); process != team.end(); process++) { + score += server_info[**process]->teams.size(); + } + + if(score < bestScore) { + bestTeam = team; + bestScore = score; + } + } + + if( bestTeam.size() == teamSize) { + vector processIDs; + + for (auto process = bestTeam.begin(); process < bestTeam.end(); process++) { + processIDs.push_back(**process); + } + + std::sort(processIDs.begin(), processIDs.end()); + + if( !teamExists( processIDs ) ) { + addTeam(processIDs.begin(), processIDs.end()); + addedTeams++; + } + } + else { + TraceEvent(SevWarn, "DataDistributionBuildTeams", masterId).detail("Reason","Unable to make desiredTeams"); + break; + } + if(++loopCount > 2*teamsToBuild*(teamSize+1) ) { + break; + } + } + return addedTeams; + } + + // Use the current set of known processes (from server_info) to compute an optimized set of storage server teams. + // The following are guarantees of the process: + // - Each newly-built team will meet the replication policy + // - All newly-built teams will have exactly teamSize machines + // + // buildTeams() only ever adds teams to the list of teams. Teams are only removed from the list when all data has been removed. + // + // buildTeams will not count teams larger than teamSize against the desired teams. + ACTOR Future buildTeams( DDTeamCollection* self ) { + state int desiredTeams; + int serverCount = 0; + int uniqueDataCenters = 0; + int uniqueMachines = 0; + std::set>> dataCenters; + std::set>> machines; + + for(auto i = self->server_info.begin(); i != self->server_info.end(); ++i) { + if (!self->server_status.get(i->first).isUndesired) { + ++serverCount; + LocalityData& serverLocation = i->second->lastKnownInterface.locality; + machines.insert( serverLocation.zoneId() ); + // Only add the datacenter if it's set or we don't care (less than two datacenters targeted) + if( serverLocation.dcId().present() || self->desiredDataCenters < 2 ) + dataCenters.insert( serverLocation.dcId() ); + } + } + uniqueMachines = machines.size(); + + // If there are too few machines to even build teams or there are too few represented datacenters, build no new teams + if( uniqueMachines >= self->teamSize && dataCenters.size() >= self->minDataCenters ) { + desiredTeams = SERVER_KNOBS->DESIRED_TEAMS_PER_SERVER*serverCount; + + // Count only properly sized teams against the desired number of teams. This is to prevent "emergency" merged teams (see MoveKeys) + // from overwhelming the team count (since we really did not want that team in the first place). These larger teams will not be + // returned from getRandomTeam() (as used by bestTeam to find a new home for a shard). + // Also exclude teams who have members in the wrong configuration, since we don't want these teams either + int teamCount = 0; + for(int i = 0; i < self->teams.size(); i++) { + if( self->teams[i]->getServerIDs().size() == self->teamSize && !self->teams[i]->isWrongConfiguration() ) { + teamCount++; + } + } + + TraceEvent("BuildTeamsBegin", self->masterId).detail("DesiredTeams", desiredTeams).detail("UniqueMachines", uniqueMachines) + .detail("TeamSize", self->teamSize).detail("Servers", serverCount).detail("DataCenters", dataCenters.size()) + .detail("CurrentTrackedTeams", self->teams.size()).detail("TeamCount", teamCount); + + if( desiredTeams > teamCount ) { + std::set desiredServerSet; + for(auto i = self->server_info.begin(); i != self->server_info.end(); ++i) + if (!self->server_status.get(i->first).isUndesired) + desiredServerSet.insert(i->second->id); + + vector desiredServerVector( desiredServerSet.begin(), desiredServerSet.end() ); + + state int teamsToBuild = desiredTeams - teamCount; + + state vector> builtTeams; + int addedTeams = wait( self->addAllTeams( self, desiredServerVector, &builtTeams, teamsToBuild ) ); + + if( addedTeams < teamsToBuild ) { + for( int i = 0; i < builtTeams.size(); i++ ) { + std::sort(builtTeams[i].begin(), builtTeams[i].end()); + self->addTeam( builtTeams[i].begin(), builtTeams[i].end() ); + } + TraceEvent("AddAllTeams", self->masterId).detail("CurrentTeams", self->teams.size()).detail("AddedTeams", builtTeams.size()); + } + else { + int addedTeams = self->addTeamsBestOf( teamsToBuild ); + TraceEvent("AddTeamsBestOf", self->masterId).detail("CurrentTeams", self->teams.size()).detail("AddedTeams", addedTeams); + } + } + } + + self->evaluateTeamQuality(); + + //Building teams can cause servers to become undesired, which can make teams unhealthy. + //Let all of these changes get worked out before responding to the get team request + Void _ = wait( delay(0, TaskDataDistributionLaunch) ); + + return Void(); + } + + void noHealthyTeams() { + std::set desiredServerSet; + std::string desc; + for(auto i = server_info.begin(); i != server_info.end(); ++i) { + ASSERT(i->first == i->second->id); + if (!server_status.get(i->first).isFailed) { + desiredServerSet.insert(i->first); + desc += i->first.shortString() + " (" + i->second->lastKnownInterface.toString() + "), "; + } + } + vector desiredServerVector( desiredServerSet.begin(), desiredServerSet.end() ); + + TraceEvent(SevWarn, "NoHealthyTeams", masterId) + .detail("CurrentTeamCount", teams.size()) + .detail("ServerCount", server_info.size()) + .detail("NonFailedServerCount", desiredServerVector.size()); + } + + void countHealthyTeams() { + int healthy = 0; + for(auto it = teams.begin(); it != teams.end(); it++) { + if( (*it)->isHealthy() ) { + healthy++; + } + } + TraceEvent(healthy == healthyTeamCount ? SevInfo : SevWarnAlways, "HealthyTeamCheck", masterId) + .detail("ValidatedCount", healthy) + .detail("ProvidedCount", healthyTeamCount); + } + + void addServer( StorageServerInterface newServer, ProcessClass processClass, Promise errorOut ) { + allServers.push_back( newServer.id() ); + + TraceEvent("AddedStorageServer", masterId).detail("ServerID", newServer.id()).detail("ProcessClass", processClass.toString()).detail("WaitFailureToken", newServer.waitFailure.getEndpoint().token).detail("address", newServer.waitFailure.getEndpoint().address); + auto &r = server_info[newServer.id()] = Reference( new TCServerInfo( newServer, processClass ) ); + r->tracker = storageServerTracker( this, cx, r.getPtr(), &server_status, lock, masterId, &server_info, serverChanges, serverFailures, errorOut ); + restartTeamBuilder.trigger(); + } + + void removeServer( UID removedServer ) { + TraceEvent("RemovedStorageServer", masterId).detail("ServerID", removedServer); + // ASSERT( !shardsAffectedByTeamFailure->getServersForTeam( t ) for all t in teams that contain removedServer ) + + // Find all servers with which the removedServer shares teams + std::set serversWithAjoiningTeams; + auto& sharedTeams = server_info[ removedServer ]->teams; + for( int i = 0; i < sharedTeams.size(); i++ ) { + auto& teamIds = sharedTeams[i]->getServerIDs(); + serversWithAjoiningTeams.insert( teamIds.begin(), teamIds.end() ); + } + serversWithAjoiningTeams.erase( removedServer ); + + // For each server in a team with the removedServer, erase shared teams from the list of teams in that other server + for( auto it = serversWithAjoiningTeams.begin(); it != serversWithAjoiningTeams.end(); ++it ) { + auto& teams = server_info[ *it ]->teams; + for( int t = 0; t < teams.size(); t++ ) { + auto& serverIds = teams[t]->getServerIDs(); + if ( std::count( serverIds.begin(), serverIds.end(), removedServer ) ) { + teams[t--] = teams.back(); + teams.pop_back(); + } + } + } + + // remove removedServer from allServers, server_info + for(int s=0; sgetServerIDs().begin(), teams[t]->getServerIDs().end(), removedServer ) ) { + teams[t]->tracker.cancel(); + teams[t--] = teams.back(); + teams.pop_back(); + } + } + doBuildTeams = true; + restartTeamBuilder.trigger(); + + TraceEvent("DataDistributionTeamCollectionUpdate", masterId) + .detail("Teams", teams.size()) + .detail("Servers", allServers.size()); + } +}; + +// Track a team and issue RelocateShards when the level of degradation changes +ACTOR Future teamTracker( DDTeamCollection *self, Reference team) { + state int lastServersLeft = team->getServerIDs().size(); + state bool lastAnyUndesired = false; + state bool wrongSize = team->getServerIDs().size() != self->teamSize; + state bool lastReady = self->initialFailureReactionDelay.isReady(); + state bool lastHealthy = team->isHealthy(); + state bool lastOptimal = team->isOptimal(); + state bool lastWrongConfiguration = team->isWrongConfiguration(); + + if(lastHealthy) { + self->healthyTeamCount++; + + if(lastOptimal) { + self->optimalTeamCount++; + if( self->optimalTeamCount == 1 ) + self->optimalTeamChange.send(Void()); + } + } + + TraceEvent("TeamTrackerStarting", self->masterId).detail("Reason", "Initial wait complete (sc)").detail("Team", team->getDesc()); + + try { + loop { + TraceEvent("TeamHealthChangeDetected", self->masterId).detail("isReady", self->initialFailureReactionDelay.isReady() ); + // Check if the number of degraded machines has changed + state vector> change; + auto servers = team->getServerIDs(); + bool anyUndesired = false; + bool anyWrongConfiguration = false; + Reference teamLocality(new LocalityGroup()); + + for(auto s = servers.begin(); s != servers.end(); ++s) { + change.push_back( self->server_status.onChange( *s ) ); + auto& status = self->server_status.get(*s); + if (!status.isFailed) + teamLocality->add( status.locality ); + if (status.isUndesired) + anyUndesired = true; + if (status.isWrongConfiguration) + anyWrongConfiguration = true; + } + + int serversLeft = teamLocality->size(); + bool matchesPolicy = self->replicationPolicy->validate(teamLocality->getEntries(), teamLocality); + + if( !self->initialFailureReactionDelay.isReady() ) + change.push_back( self->initialFailureReactionDelay ); + + bool recheck = lastReady != self->initialFailureReactionDelay.isReady() && ( !matchesPolicy || anyUndesired || team->getServerIDs().size() != self->teamSize ); + lastReady = self->initialFailureReactionDelay.isReady(); + + if( serversLeft != lastServersLeft || anyUndesired != lastAnyUndesired || anyWrongConfiguration != lastWrongConfiguration || wrongSize || recheck ) { + TraceEvent("TeamHealthChanged", self->masterId) + .detail("Team", team->getDesc()).detail("serversLeft", serversLeft) + .detail("lastServersLeft", lastServersLeft).detail("ContainsUndesiredServer", anyUndesired) + .detail("HealthyTeamsCount", self->healthyTeamCount).detail("IsWrongConfiguration", anyWrongConfiguration); + + bool healthy = matchesPolicy && !anyUndesired && team->getServerIDs().size() == self->teamSize && team->getServerIDs().size() == serversLeft; + team->setHealthy( healthy ); // Unhealthy teams won't be chosen by bestTeam + + team->setWrongConfiguration( anyWrongConfiguration ); + + bool optimal = team->isOptimal(); + int lastOptimalCount = self->optimalTeamCount; + if( optimal != lastOptimal ) { + lastOptimal = optimal; + if( lastHealthy ) + self->optimalTeamCount += lastOptimal ? 1 : -1; + } + + if( lastHealthy != healthy ) { + lastHealthy = healthy; + self->healthyTeamCount += healthy ? 1 : -1; + + ASSERT( self->healthyTeamCount >= 0 ); + + if( self->healthyTeamCount == 0 ) { + TraceEvent(SevWarn, "ZeroTeamsHealthySignalling", self->masterId) + .detail("SignallingTeam", team->getDesc()); + self->zeroHealthyTeams.send( Void() ); + } + + if( lastOptimal ) + self->optimalTeamCount += lastHealthy ? 1 : -1; + + TraceEvent("TeamHealthDifference", self->masterId) + .detail("LastOptimal", lastOptimal) + .detail("LastHealthy", lastHealthy) + .detail("Optimal", optimal) + .detail("OptimalTeamCount", self->optimalTeamCount); + } + + if( lastOptimalCount != self->optimalTeamCount && ( self->optimalTeamCount == 0 || self->optimalTeamCount == 1 ) ) { + TraceEvent("OptimalTeamsChanging", self->masterId); + self->optimalTeamChange.send( Void() ); + } + + lastServersLeft = serversLeft; + lastAnyUndesired = anyUndesired; + lastWrongConfiguration = anyWrongConfiguration; + wrongSize = false; + + state int lastPriority = team->getPriority(); + if( serversLeft < self->teamSize ) { + if( serversLeft == 0 ) + team->setPriority( PRIORITY_TEAM_0_LEFT ); + else if( serversLeft == 1 ) + team->setPriority( PRIORITY_TEAM_1_LEFT ); + else if( serversLeft == 2 ) + team->setPriority( PRIORITY_TEAM_2_LEFT ); + else + team->setPriority( PRIORITY_TEAM_UNHEALTHY ); + } + else if ( team->getServerIDs().size() != self->teamSize ) + team->setPriority( PRIORITY_TEAM_UNHEALTHY ); + else if( anyUndesired ) + team->setPriority( PRIORITY_TEAM_CONTAINS_UNDESIRED_SERVER ); + else + team->setPriority( PRIORITY_TEAM_HEALTHY ); + TraceEvent("TeamPriorityChange", self->masterId).detail("Priority", team->getPriority()); + + if( self->initialFailureReactionDelay.isReady() ) { + vector shards = self->shardsAffectedByTeamFailure->getShardsFor( team->getServerIDs() ); + + for(int i=0; igetPriority(); + auto teams = self->shardsAffectedByTeamFailure->getTeamsFor( shards[i] ); + for( int t=0; tserver_info.count( teams[t][0] ) ) { + auto& info = self->server_info[teams[t][0]]; + + bool found = false; + for( int i = 0; i < info->teams.size(); i++ ) { + if( info->teams[i]->serverIDs == teams[t] ) { + maxPriority = std::max( maxPriority, info->teams[i]->getPriority() ); + found = true; + break; + } + } + + TEST(!found); // A removed team is still associated with a shard in SABTF + } else { + TEST(true); // A removed server is still associated with a team in SABTF + } + } + + if( maxPriority == team->getPriority() || lastPriority > maxPriority ) { + RelocateShard rs; + rs.keys = shards[i]; + rs.priority = maxPriority; + + self->output.send(rs); + if(g_random->random01() < 0.01) { + TraceEvent("SendRelocateToDDQx100", self->masterId) + .detail("Team", team->getDesc()) + .detail("KeyBegin", printable(rs.keys.begin)) + .detail("KeyEnd", printable(rs.keys.end)) + .detail("Priority", rs.priority) + .detail("TeamFailedMachines", team->getServerIDs().size()-serversLeft) + .detail("TeamOKMachines", serversLeft); + } + } else { + TraceEvent("RelocationNotSentToDDQ", self->masterId) + .detail("Team", team->getDesc()); + } + } + } else { + TraceEvent("TeamHealthNotReady", self->masterId); + } + } + + // Wait for any of the machines to change status + Void _ = wait( quorum( change, 1 ) ); + Void _ = wait( yield() ); + } + } catch(Error& e) { + if( team->isHealthy() ) { + self->healthyTeamCount--; + ASSERT( self->healthyTeamCount >= 0 ); + + if( self->healthyTeamCount == 0 ) { + TraceEvent(SevWarn, "ZeroTeamsHealthySignalling", self->masterId).detail("SignallingTeam", team->getDesc()); + self->zeroHealthyTeams.send( Void() ); + } + } + throw; + } +} + +ACTOR Future trackExcludedServers( DDTeamCollection *self, Database cx ) { + loop { + // Fetch the list of excluded servers + state Transaction tr(cx); + state Optional lastChangeID; + loop { + try { + state Future> fresults = tr.getRange( excludedServersKeys, CLIENT_KNOBS->TOO_MANY ); + state Future> fchid = tr.get( excludedServersVersionKey ); + Void _ = wait( success(fresults) && success(fchid) ); + + Standalone results = fresults.get(); + lastChangeID = fchid.get(); + ASSERT( !results.more && results.size() < CLIENT_KNOBS->TOO_MANY ); + + std::set excluded; + for(auto r = results.begin(); r != results.end(); ++r) { + AddressExclusion addr = decodeExcludedServersKey(r->key); + if (addr.isValid()) + excluded.insert( addr ); + } + + TraceEvent("DDExcludedServersChanged", self->masterId).detail("Rows", results.size()).detail("Exclusions", excluded.size()); + + // Reset and reassign self->excludedServers based on excluded, but weonly + // want to trigger entries that are different + auto old = self->excludedServers.getKeys(); + for(auto& o : old) + if (!excluded.count(o)) + self->excludedServers.set(o, false); + for(auto& n : excluded) + self->excludedServers.set(n, true); + self->restartRecruiting.trigger(); + break; + } catch (Error& e) { + Void _ = wait( tr.onError(e) ); + } + } + + // Wait for a change in the list of excluded servers + loop { + try { + Optional nchid = wait( tr.get( excludedServersVersionKey ) ); + if (nchid != lastChangeID) + break; + + Void _ = wait( delay( SERVER_KNOBS->SERVER_LIST_DELAY, TaskDataDistribution ) ); // FIXME: make this tr.watch( excludedServersVersionKey ) instead + tr = Transaction(cx); + } catch (Error& e) { + Void _ = wait( tr.onError(e) ); + } + } + } +} + +ACTOR Future>> getServerListAndProcessClasses( Transaction *tr ) { + state Future> workers = getWorkers(tr); + state Future> serverList = tr->getRange( serverListKeys, CLIENT_KNOBS->TOO_MANY ); + Void _ = wait( success(workers) && success(serverList) ); + ASSERT( !serverList.get().more && serverList.get().size() < CLIENT_KNOBS->TOO_MANY ); + + std::map>, ProcessData> id_data; + for( int i = 0; i < workers.get().size(); i++ ) + id_data[workers.get()[i].locality.processId()] = workers.get()[i]; + + vector> results; + for( int i = 0; i < serverList.get().size(); i++ ) { + auto ssi = decodeServerListValue( serverList.get()[i].value ); + results.push_back( std::make_pair(ssi, id_data[ssi.locality.processId()].processClass) ); + } + + return results; +} + +ACTOR Future waitServerListChange( DDTeamCollection *self, Database cx, FutureStream serverRemoved ) { + state Future checkSignal = delay(SERVER_KNOBS->SERVER_LIST_DELAY, TaskDataDistribution); + state Future>> serverListAndProcessClasses = Never(); + state bool isFetchingResults = false; + state Transaction tr(cx); + loop { + try { + choose { + when( Void _ = wait( checkSignal ) ) { + checkSignal = Never(); + isFetchingResults = true; + serverListAndProcessClasses = getServerListAndProcessClasses(&tr); + } + when( vector> results = wait( serverListAndProcessClasses ) ) { + serverListAndProcessClasses = Never(); + isFetchingResults = false; + + for( int i = 0; i < results.size(); i++ ) { + UID serverId = results[i].first.id(); + StorageServerInterface const& ssi = results[i].first; + ProcessClass const& processClass = results[i].second; + if( self->server_info.count( serverId ) ) { + auto& serverInfo = self->server_info[ serverId ]; + if (ssi.getValue.getEndpoint() != serverInfo->lastKnownInterface.getValue.getEndpoint() || processClass != serverInfo->lastKnownClass.classType()) { + Promise> currentInterfaceChanged = serverInfo->interfaceChanged; + serverInfo->interfaceChanged = Promise>(); + serverInfo->onInterfaceChanged = Future>( serverInfo->interfaceChanged.getFuture() ); + currentInterfaceChanged.send( std::make_pair(ssi,processClass) ); + } + } else if( !self->recruitingIds.count(ssi.id()) ) { + self->addServer( ssi, processClass, self->serverTrackerErrorOut ); + self->doBuildTeams = true; + } + } + + tr = Transaction(cx); + checkSignal = delay(SERVER_KNOBS->SERVER_LIST_DELAY, TaskDataDistribution); + } + when( Void _ = waitNext( serverRemoved ) ) { + if( isFetchingResults ) { + tr = Transaction(cx); + serverListAndProcessClasses = getServerListAndProcessClasses(&tr); + } + } + } + } catch(Error& e) { + Void _ = wait( tr.onError(e) ); + serverListAndProcessClasses = Never(); + isFetchingResults = false; + checkSignal = Void(); + } + } +} + +ACTOR Future serverMetricsPolling( TCServerInfo *server) { + state double lastUpdate = now(); + loop { + Void _ = wait( updateServerMetrics( server ) ); + Void _ = wait( delayUntil( lastUpdate + SERVER_KNOBS->STORAGE_METRICS_POLLING_DELAY + SERVER_KNOBS->STORAGE_METRICS_RANDOM_DELAY * g_random->random01(), TaskDataDistributionLaunch ) ); + lastUpdate = now(); + } +} + +//Returns the KeyValueStoreType of server if it is different from self->storeType +ACTOR Future keyValueStoreTypeTracker(DDTeamCollection *self, TCServerInfo *server) { + state KeyValueStoreType type = wait(brokenPromiseToNever(server->lastKnownInterface.getKeyValueStoreType.getReplyWithTaskID(TaskDataDistribution))); + if(type == self->storeType) + Void _ = wait(Never()); + + return type; +} + +ACTOR Future storageServerTracker( + DDTeamCollection *self, + Database cx, + TCServerInfo *server, //This actor is owned by this TCServerInfo + ServerStatusMap *statusMap, + MoveKeysLock lock, + UID masterId, + std::map>* other_servers, + PromiseStream< std::pair> > changes, + PromiseStream serverFailures, + Promise errorOut) +{ + state Future failureTracker; + state ServerStatus status( false, false, server->lastKnownInterface.locality ); + state bool lastIsUndesired = false; + state Future metricsTracker = serverMetricsPolling( server ); + state Future> interfaceChanged = server->onInterfaceChanged; + + state Future storeTracker = keyValueStoreTypeTracker( self, server ); + state bool hasWrongStoreType = false; + + changes.send( std::make_pair(server->id, server->lastKnownInterface) ); + + try { + loop { + status.isUndesired = false; + status.isWrongConfiguration = false; + + // If there is any other server on this exact NetworkAddress, this server is undesired and will eventually be eliminated + state std::vector> otherChanges; + std::vector> wakeUpTrackers; + for(auto i = other_servers->begin(); i != other_servers->end(); ++i) { + if (i->second.getPtr() != server && i->second->lastKnownInterface.address() == server->lastKnownInterface.address()) { + auto& statusInfo = statusMap->get( i->first ); + TraceEvent("SameAddress", masterId) + .detail("Failed", statusInfo.isFailed) + .detail("Undesired", statusInfo.isUndesired) + .detail("Server", server->id).detail("OtherServer", i->second->id) + .detail("Address", server->lastKnownInterface.address()) + .detail("NumShards", self->shardsAffectedByTeamFailure->getNumberOfShards(server->id)) + .detail("OtherNumShards", self->shardsAffectedByTeamFailure->getNumberOfShards(i->second->id)) + .detail("OtherHealthy", !statusMap->get( i->second->id ).isUnhealthy()); + otherChanges.push_back( statusMap->onChange( i->second->id ) ); + if(!statusMap->get( i->second->id ).isUnhealthy()) { + if(self->shardsAffectedByTeamFailure->getNumberOfShards(i->second->id) >= self->shardsAffectedByTeamFailure->getNumberOfShards(server->id)) + { + TraceEvent(SevWarn, "UndesiredStorageServer", masterId) + .detail("Server", server->id) + .detail("Address", server->lastKnownInterface.address()) + .detail("OtherServer", i->second->id) + .detail("NumShards", self->shardsAffectedByTeamFailure->getNumberOfShards(server->id)) + .detail("OtherNumShards", self->shardsAffectedByTeamFailure->getNumberOfShards(i->second->id)); + + status.isUndesired = true; + } + else + wakeUpTrackers.push_back(i->second->wakeUpTracker); + } + } + } + + for(auto& p : wakeUpTrackers) { + if( !p.isSet() ) + p.send(Void()); + } + + if( server->lastKnownClass.machineClassFitness( ProcessClass::Storage ) > ProcessClass::UnsetFit ) { + if( self->optimalTeamCount > 0 ) { + TraceEvent(SevWarn, "UndesiredStorageServer", masterId).detail("Server", server->id).detail("OptimalTeamCount", self->optimalTeamCount); + status.isUndesired = true; + } + otherChanges.push_back( self->onOptimalTeamChange.getFuture() ); + } + + //If this storage server has the wrong key-value store type, then mark it undesired so it will be replaced with a server having the correct type + if(hasWrongStoreType) { + TraceEvent(SevWarn, "UndesiredStorageServer", masterId).detail("Server", server->id).detail("StoreType", "?"); + status.isUndesired = true; + status.isWrongConfiguration = true; + } + + // If the storage server is in the excluded servers list, it is undesired + NetworkAddress a = server->lastKnownInterface.address(); + AddressExclusion addr( a.ip, a.port ); + AddressExclusion ipaddr( a.ip ); + if (self->excludedServers.get( addr ) || self->excludedServers.get( ipaddr )) { + TraceEvent(SevWarn, "UndesiredStorageServer", masterId).detail("Server", server->id) + .detail("Excluded", self->excludedServers.get( addr ) ? addr.toString() : ipaddr.toString()); + status.isUndesired = true; + status.isWrongConfiguration = true; + } + otherChanges.push_back( self->excludedServers.onChange( addr ) ); + otherChanges.push_back( self->excludedServers.onChange( ipaddr ) ); + + failureTracker = storageServerFailureTracker( cx, server->lastKnownInterface, statusMap, &status, serverFailures, &self->unhealthyServers, masterId ); + + //We need to recruit new storage servers if the key value store type has changed + if(hasWrongStoreType) + self->restartRecruiting.trigger(); + + if( lastIsUndesired && !status.isUndesired ) + self->doBuildTeams = true; + lastIsUndesired = status.isUndesired; + + choose { + when( Void _ = wait( failureTracker ) ) { + // The server is failed AND all data has been removed from it, so permanently remove it. + TraceEvent("StatusMapChange", masterId).detail("ServerID", server->id).detail("Status", "Removing"); + changes.send( std::make_pair(server->id, Optional()) ); + + // Remove server from FF/serverList + Void _ = wait( removeStorageServer( cx, server->id, lock ) ); + + TraceEvent("StatusMapChange", masterId).detail("ServerID", server->id).detail("Status", "Removed"); + // Sets removeSignal (alerting dataDistributionTeamCollection to remove the storage server from its own data structures) + server->removed.send( Void() ); + self->removedServers.send( server->id ); + return Void(); + } + when( std::pair newInterface = wait( interfaceChanged ) ) { + bool restartRecruiting = newInterface.first.waitFailure.getEndpoint().address != server->lastKnownInterface.waitFailure.getEndpoint().address; + TraceEvent("StorageServerInterfaceChanged", masterId).detail("ServerID", server->id) + .detail("NewWaitFailureToken", newInterface.first.waitFailure.getEndpoint().token) + .detail("OldWaitFailureToken", server->lastKnownInterface.waitFailure.getEndpoint().token); + server->lastKnownInterface = newInterface.first; + server->lastKnownClass = newInterface.second; + interfaceChanged = server->onInterfaceChanged; + changes.send( std::make_pair(server->id, server->lastKnownInterface) ); + // We rely on the old failureTracker being actorCancelled since the old actor now has a pointer to an invalid location + status = ServerStatus( status.isFailed, status.isUndesired, server->lastKnownInterface.locality ); + + //Restart the storeTracker for the new interface + storeTracker = keyValueStoreTypeTracker(self, server); + hasWrongStoreType = false; + self->restartTeamBuilder.trigger(); + if(restartRecruiting) + self->restartRecruiting.trigger(); + } + when( Void _ = wait( otherChanges.empty() ? Never() : quorum( otherChanges, 1 ) ) ) { + TraceEvent("SameAddressChangedStatus", masterId).detail("ServerID", server->id); + } + when( KeyValueStoreType type = wait( storeTracker ) ) { + TraceEvent("KeyValueStoreTypeChanged", masterId) + .detail("ServerID", server->id) + .detail("StoreType", type.toString()) + .detail("DesiredType", self->storeType.toString()); + TEST(true); //KeyValueStore type changed + + storeTracker = Never(); + hasWrongStoreType = true; + } + when( Void _ = wait( server->wakeUpTracker.getFuture() ) ) { + server->wakeUpTracker = Promise(); + } + } + } + } catch( Error &e ) { + if (e.code() != error_code_actor_cancelled) + errorOut.sendError(e); + throw; + } +} + +//Monitor whether or not storage servers are being recruited. If so, then a database cannot be considered quiet +ACTOR Future monitorStorageServerRecruitment(DDTeamCollection *self) { + state bool recruiting = false; + TraceEvent("StorageServerRecruitment", self->masterId) + .detail("State", "Idle") + .trackLatest((self->cx->dbName.toString() + "/StorageServerRecruitment_" + self->masterId.toString()).c_str()); + loop { + if( !recruiting ) { + while(self->recruitingStream.get() == 0) { + Void _ = wait( self->recruitingStream.onChange() ); + } + TraceEvent("StorageServerRecruitment", self->masterId) + .detail("State", "Recruiting") + .trackLatest((self->cx->dbName.toString() + "/StorageServerRecruitment_" + self->masterId.toString()).c_str()); + recruiting = true; + } else { + loop { + choose { + when( Void _ = wait( self->recruitingStream.onChange() ) ) {} + when( Void _ = wait( self->recruitingStream.get() == 0 ? delay(SERVER_KNOBS->RECRUITMENT_IDLE_DELAY, TaskDataDistribution) : Future(Never()) ) ) { break; } + } + } + TraceEvent("StorageServerRecruitment", self->masterId) + .detail("State", "Idle") + .trackLatest((self->cx->dbName.toString() + "/StorageServerRecruitment_" + self->masterId.toString()).c_str()); + recruiting = false; + } + } +} + +ACTOR Future initializeStorage( DDTeamCollection *self, RecruitStorageReply candidateWorker ) { + // SOMEDAY: Cluster controller waits for availability, retry quickly if a server's Locality changes + self->recruitingStream.set(self->recruitingStream.get()+1); + + state UID interfaceId = g_random->randomUniqueID(); + InitializeStorageRequest isr; + isr.storeType = self->storeType; + isr.seedTag = invalidTag; + isr.reqId = g_random->randomUniqueID(); + isr.interfaceId = interfaceId; + + TraceEvent("DDRecruiting").detail("State", "Sending request to worker").detail("WorkerID", candidateWorker.worker.id()) + .detail("WorkerLocality", candidateWorker.worker.locality.toString()).detail("interf", interfaceId).detail("addr", candidateWorker.worker.address()); + + self->recruitingIds.insert(interfaceId); + self->recruitingLocalities.insert(candidateWorker.worker.address()); + ErrorOr newServer = wait( candidateWorker.worker.storage.tryGetReply( isr, TaskDataDistribution ) ); + self->recruitingIds.erase(interfaceId); + self->recruitingLocalities.erase(candidateWorker.worker.address()); + + self->recruitingStream.set(self->recruitingStream.get()-1); + + TraceEvent("DDRecruiting").detail("State", "Finished request").detail("WorkerID", candidateWorker.worker.id()) + .detail("WorkerLocality", candidateWorker.worker.locality.toString()).detail("interf", interfaceId).detail("addr", candidateWorker.worker.address()); + + if( newServer.isError() ) { + TraceEvent(SevWarn, "DDRecruitmentError").error(newServer.getError()); + if( !newServer.isError( error_code_recruitment_failed ) && !newServer.isError( error_code_request_maybe_delivered ) ) + throw newServer.getError(); + Void _ = wait( delay(SERVER_KNOBS->STORAGE_RECRUITMENT_DELAY, TaskDataDistribution) ); + } + else if( newServer.present() ) { + if( !self->server_info.count( newServer.get().id() ) ) + self->addServer( newServer.get(), candidateWorker.processClass, self->serverTrackerErrorOut ); + else + TraceEvent(SevWarn, "DDRecruitmentError").detail("Reason", "Server ID already recruited"); + + self->doBuildTeams = true; + if( self->healthyTeamCount == 0 ) { + Void _ = wait( self->checkBuildTeams( self ) ); + } + } + + self->restartRecruiting.trigger(); + + return Void(); +} + +ACTOR Future storageRecruiter( DDTeamCollection *self, Reference> db ) { + state Future fCandidateWorker; + state RecruitStorageRequest lastRequest; + loop { + try { + RecruitStorageRequest rsr; + std::set exclusions; + for(auto s = self->server_info.begin(); s != self->server_info.end(); ++s) { + auto serverStatus = self->server_status.get( s->second->lastKnownInterface.id() ); + if( serverStatus.excludeOnRecruit() ) { + TraceEvent("DDRecruitExcl1").detail("Excluding", s->second->lastKnownInterface.address()); + auto addr = s->second->lastKnownInterface.address(); + exclusions.insert( AddressExclusion( addr.ip, addr.port ) ); + } + } + for(auto addr : self->recruitingLocalities) { + exclusions.insert( AddressExclusion(addr.ip, addr.port)); + } + + auto excl = self->excludedServers.getKeys(); + for(auto& s : excl) + if (self->excludedServers.get(s)) { + TraceEvent("DDRecruitExcl2").detail("Excluding", s.toString()); + exclusions.insert( s ); + } + rsr.criticalRecruitment = self->healthyTeamCount == 0; + for(auto it : exclusions) { + rsr.excludeAddresses.push_back(it); + } + + TraceEvent(rsr.criticalRecruitment ? SevWarn : SevInfo, "DDRecruiting").detail("State", "Sending request to CC") + .detail("Exclusions", rsr.excludeAddresses.size()).detail("Critical", rsr.criticalRecruitment); + + if( rsr.criticalRecruitment ) { + TraceEvent(SevWarn, "DDRecruitingEmergency", self->masterId); + } + + if(!fCandidateWorker.isValid() || fCandidateWorker.isReady() || rsr.excludeAddresses != lastRequest.excludeAddresses || rsr.criticalRecruitment != lastRequest.criticalRecruitment) { + lastRequest = rsr; + fCandidateWorker = brokenPromiseToNever( db->get().clusterInterface.recruitStorage.getReply( rsr, TaskDataDistribution ) ); + } + + choose { + when( RecruitStorageReply candidateWorker = wait( fCandidateWorker ) ) { + self->addActor.send(initializeStorage(self, candidateWorker)); + } + when( Void _ = wait( db->onChange() ) ) { // SOMEDAY: only if clusterInterface changes? + fCandidateWorker = Future(); + } + when( Void _ = wait( self->restartRecruiting.onTrigger() ) ) {} + } + } catch( Error &e ) { + if(e.code() != error_code_timed_out) { + throw; + } + TEST(true); //Storage recruitment timed out + } + } +} + +// Keep track of servers and teams -- serves requests for getRandomTeam +ACTOR Future dataDistributionTeamCollection( + Reference initData, + TeamCollectionInterface tci, + Database cx, + Reference> db, + Reference shardsAffectedByTeamFailure, + MoveKeysLock lock, + PromiseStream output, + UID masterId, int teamSize, int minDataCenters, int desiredDataCenters, + IRepPolicyRef replicationPolicy, + KeyValueStoreType storeType, + PromiseStream< std::pair> > serverChanges, + Future readyToStart ) +{ + state DDTeamCollection self( cx, masterId, lock, output, shardsAffectedByTeamFailure, teamSize, minDataCenters, + desiredDataCenters, replicationPolicy, storeType, serverChanges ); + + state Future loggingTrigger = Void(); + state PromiseStream serverRemoved; + state Future interfaceChanges; + state Future error = actorCollection( self.addActor.getFuture() ); + state Future storageServerRecruitment; + state Future storageServerRecruitmentMonitor; + state Future trackExcluded; + + Void _ = wait( readyToStart ); + + try { + self.init( *initData ); + initData = Reference(); + storageServerRecruitment = storageRecruiter( &self, db ); + storageServerRecruitmentMonitor = monitorStorageServerRecruitment( &self ); + interfaceChanges = waitServerListChange( &self, cx, serverRemoved.getFuture() ); + trackExcluded = trackExcludedServers( &self, cx ); + + // SOMEDAY: Monitor FF/serverList for (new) servers that aren't in allServers and add or remove them + + loop choose { + + when( GetTeamRequest req = waitNext(tci.getTeam.getFuture()) ) { + self.addActor.send( self.getTeam( &self, req ) ); + } + when( UID removedServer = waitNext( self.removedServers.getFuture() ) ) { + TEST(true); // Storage server removed from database + self.removeServer( removedServer ); + serverRemoved.send( Void() ); + + self.restartRecruiting.trigger(); + } + when( Void _ = waitNext( self.zeroHealthyTeams.getFuture() ) ) { + self.restartRecruiting.trigger(); + self.noHealthyTeams(); + } + when( Void _ = waitNext( self.optimalTeamChange.getFuture() ) ) { + Promise oldChange = self.onOptimalTeamChange; + self.onOptimalTeamChange = Promise(); + oldChange.send(Void()); + } + when( Void _ = waitNext( self.serverFailures.getFuture() ) ) { + self.restartRecruiting.trigger(); + } + when( Void _ = wait( loggingTrigger ) ) { + TraceEvent("TotalDataInFlight", masterId).detail("TotalBytes", self.getDebugTotalDataInFlight()).detail("UnhealthyServers", self.unhealthyServers).trackLatest( + (cx->dbName.toString() + "/TotalDataInFlight").c_str()); + loggingTrigger = delay( SERVER_KNOBS->DATA_DISTRIBUTION_LOGGING_INTERVAL ); + self.countHealthyTeams(); + } + when( Void _ = wait( self.serverTrackerErrorOut.getFuture() ) ) {} // Propagate errors from storageServerTracker + when( Void _ = wait( interfaceChanges ) ) {} + when( Void _ = wait( trackExcluded ) ) {} + when( Void _ = wait( error ) ) {} + when( Void _ = wait( storageServerRecruitment ) ) {} + } + } catch (Error& e) { + if (e.code() != error_code_movekeys_conflict) + TraceEvent(SevError, "dataDistributionTeamCollectionError", masterId).error(e); + throw e; + } +} + +ACTOR Future waitForDataDistributionEnabled( Database cx ) { + state Transaction tr(cx); + loop { + Void _ = wait(delay(SERVER_KNOBS->DD_ENABLED_CHECK_DELAY, TaskDataDistribution)); + + try { + Optional mode = wait( tr.get( dataDistributionModeKey ) ); + if (!mode.present()) return Void(); + if (mode.present()) { + BinaryReader rd( mode.get(), Unversioned() ); + int m; + rd >> m; + if (m) return Void(); + } + + tr.reset(); + } catch (Error& e) { + Void _ = wait( tr.onError(e) ); + } + } +} + +ACTOR Future isDataDistributionEnabled( Database cx ) { + state Transaction tr(cx); + loop { + try { + Optional mode = wait( tr.get( dataDistributionModeKey ) ); + if (!mode.present()) return true; + if (mode.present()) { + BinaryReader rd( mode.get(), Unversioned() ); + int m; + rd >> m; + if (m) return true; + } + // SOMEDAY: Write a wrapper in MoveKeys.h + Optional readVal = wait( tr.get( moveKeysLockOwnerKey ) ); + UID currentOwner = readVal.present() ? BinaryReader::fromStringRef(readVal.get(), Unversioned()) : UID(); + if( currentOwner != dataDistributionModeLock ) + return true; + return false; + } catch (Error& e) { + Void _ = wait( tr.onError(e) ); + } + } +} + +ACTOR Future disableDataDistribution( Database cx ) { + state Transaction tr(cx); + state int oldMode = -1; + state BinaryWriter wr(Unversioned()); + wr << 0; + + loop { + try { + Optional old = wait( tr.get( dataDistributionModeKey ) ); + if (oldMode < 0) { + oldMode = 1; + if (old.present()) { + BinaryReader rd(old.get(), Unversioned()); + rd >> oldMode; + } + } + // SOMEDAY: Write a wrapper in MoveKeys.h + BinaryWriter wrMyOwner(Unversioned()); wrMyOwner << dataDistributionModeLock; + tr.set( moveKeysLockOwnerKey, wrMyOwner.toStringRef() ); + tr.set( dataDistributionModeKey, wr.toStringRef() ); + + Void _ = wait( tr.commit() ); + return oldMode; + } catch (Error& e) { + TraceEvent("disableDDModeRetrying").error(e); + Void _ = wait ( tr.onError(e) ); + } + } +} + +ACTOR Future enableDataDistribution( Database cx, int mode ) { + state Transaction tr(cx); + state BinaryWriter wr(Unversioned()); + wr << mode; + + loop { + try { + Optional old = wait( tr.get( dataDistributionModeKey ) ); + tr.set( dataDistributionModeKey, wr.toStringRef() ); + Void _ = wait( tr.commit() ); + return Void(); + } catch (Error& e) { + TraceEvent("enableDDModeRetrying").error(e); + Void _ = wait( tr.onError(e) ); + } + } +} + +//Ensures that the serverKeys key space is properly coalesced +//This method is only used for testing and is not implemented in a manner that is safe for large databases +ACTOR Future debugCheckCoalescing(Database cx) { + state Transaction tr(cx); + loop { + try { + state Standalone serverList = wait(tr.getRange(serverListKeys, CLIENT_KNOBS->TOO_MANY)); + ASSERT( !serverList.more && serverList.size() < CLIENT_KNOBS->TOO_MANY); + + state int i; + for(i = 0; i < serverList.size(); i++) { + state UID id = decodeServerListValue(serverList[i].value).id(); + Standalone ranges = wait(krmGetRanges(&tr, serverKeysPrefixFor(id), allKeys)); + ASSERT(ranges.end()[-1].key == allKeys.end); + + for(int j = 0; j < ranges.size() - 2; j++) + if(ranges[j].value == ranges[j + 1].value) + TraceEvent(SevError, "UncoalescedValues", id).detail("Key1", printable(ranges[j].key)).detail("Key2", printable(ranges[j + 1].key)).detail("Value", printable(ranges[j].value)); + } + + TraceEvent("DoneCheckingCoalescing"); + return Void(); + } + catch(Error &e){ + Void _ = wait( tr.onError(e) ); + } + } +} + +static std::set const& normalDDQueueErrors() { + static std::set s; + if (s.empty()) { + s.insert( error_code_movekeys_conflict ); + s.insert( error_code_broken_promise ); + } + return s; +} + +ACTOR Future popOldTags( Database cx, Reference logSystem, Version recoveryCommitVersion ) { + state Transaction tr(cx); + + if( recoveryCommitVersion == 1 ) + return Void(); + + loop { + try { + Optional> val = wait( tr.get(serverTagMaxKey) ); + if(!val.present()) + return Void(); + + state Tag maxTag = decodeServerTagMaxValue( val.get() ); + + Standalone tags = wait( tr.getRange( serverTagKeys, CLIENT_KNOBS->TOO_MANY, true) ); + + std::set unusedTags; + for(int i = 0; i <= maxTag; i++) + unusedTags.insert(i); + + for(auto kv : tags) + unusedTags.erase(decodeServerTagValue( kv.value )); + + for(auto tag : unusedTags) + logSystem->pop(recoveryCommitVersion, tag); + + return Void(); + } catch( Error &e ) { + Void _ = wait( tr.onError(e) ); + } + } +} + +ACTOR Future pollMoveKeysLock( Database cx, MoveKeysLock lock ) { + loop { + Void _ = wait(delay(SERVER_KNOBS->MOVEKEYS_LOCK_POLLING_DELAY)); + state Transaction tr(cx); + loop { + try { + Void _ = wait( checkMoveKeysLockReadOnly(&tr, lock) ); + break; + } catch( Error &e ) { + Void _ = wait( tr.onError(e) ); + } + } + } +} + +ACTOR Future dataDistribution( + Reference> db, + MasterInterface mi, DatabaseConfiguration configuration, + PromiseStream< std::pair> > serverChanges, + Reference logSystem, + Version recoveryCommitVersion, + double* lastLimited) +{ + state Database cx = openDBOnServer(db, TaskDataDistributionLaunch, true, true); + cx->locationCacheSize = SERVER_KNOBS->DD_LOCATION_CACHE_SIZE; + + state Transaction trVer(cx); + loop { + try { + trVer.setOption( FDBTransactionOptions::ACCESS_SYSTEM_KEYS ); + trVer.setOption( FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE ); + + if( !g_network->isSimulated() ) { + UID id(g_random->randomUniqueID()); + TraceEvent("UpgradeProcessClassTransaction", mi.id()) + .detail("TransactionUID", id); + trVer.debugTransaction( id ); + } + + Optional val = wait(trVer.get(processClassVersionKey)); + + if (val.present()) + break; + + Standalone processClasses = wait( trVer.getRange( processClassKeys, CLIENT_KNOBS->TOO_MANY ) ); + ASSERT( !processClasses.more && processClasses.size() < CLIENT_KNOBS->TOO_MANY ); + + trVer.clear(processClassKeys); + trVer.set(processClassVersionKey, processClassVersionValue); + for (auto it : processClasses) { + UID processUid = decodeProcessClassKeyOld(it.key); + trVer.set(processClassKeyFor(processUid.toString()), it.value); + } + + Void _ = wait(trVer.commit()); + TraceEvent("ProcessClassUpgrade"); + break; + } + catch(Error &e) { + Void _ = wait( trVer.onError(e) ); + } + } + + + //cx->setOption( FDBDatabaseOptions::LOCATION_CACHE_SIZE, StringRef((uint8_t*) &SERVER_KNOBS->DD_LOCATION_CACHE_SIZE, 8) ); + //ASSERT( cx->locationCacheSize == SERVER_KNOBS->DD_LOCATION_CACHE_SIZE ); + + //Void _ = wait(debugCheckCoalescing(cx)); + + loop { + try { + loop { + TraceEvent("DDInitTakingMoveKeysLock", mi.id()); + state MoveKeysLock lock = wait( takeMoveKeysLock( cx, mi.id() ) ); + TraceEvent("DDInitTookMoveKeysLock", mi.id()); + state Reference initData = wait( getInitialDataDistribution(cx, mi.id(), lock) ); + if(initData->shards.size() > 1) { + TraceEvent("DDInitGotInitialDD", mi.id()).detail("b", printable(initData->shards.end()[-2].begin)).detail("e", printable(initData->shards.end()[-2].end)).detail("src", describe(initData->shards.end()[-2].value.first)).detail("dest", describe(initData->shards.end()[-2].value.second)).trackLatest("InitialDD"); + } else { + TraceEvent("DDInitGotInitialDD", mi.id()).detail("b","").detail("e", "").detail("src", "[no items]").detail("dest", "[no items]").trackLatest("InitialDD"); + } + + if (initData->mode) break; + TraceEvent("DataDistributionDisabled", mi.id()); + + TraceEvent("MovingData", mi.id()) + .detail( "InFlight", 0 ) + .detail( "InQueue", 0 ) + .detail( "AverageShardSize", -1 ) + .detail( "LowPriorityRelocations", 0 ) + .detail( "HighPriorityRelocations", 0 ) + .detail( "HighestPriority", 0 ) + .trackLatest( format("%s/MovingData", printable(cx->dbName).c_str() ).c_str() ); + + TraceEvent("TotalDataInFlight", mi.id()).detail("TotalBytes", 0) + .trackLatest((cx->dbName.toString() + "/TotalDataInFlight").c_str()); + + Void _ = wait( waitForDataDistributionEnabled(cx) ); + TraceEvent("DataDistributionEnabled"); + } + + // When/If this assertion fails, Evan owes Ben a pat on the back for his foresight + ASSERT(configuration.storageTeamSize > 0); + + state PromiseStream output; + state PromiseStream> getAverageShardBytes; + state PromiseStream getShardMetrics; + state Promise readyToStart; + TeamCollectionInterface tci; + Reference shardsAffectedByTeamFailure( new ShardsAffectedByTeamFailure ); + vector> actors; + + actors.push_back( pollMoveKeysLock(cx, lock) ); + actors.push_back( popOldTags( cx, logSystem, recoveryCommitVersion) ); + actors.push_back( reportErrorsExcept( dataDistributionTracker( initData, cx, shardsAffectedByTeamFailure, output, getShardMetrics, getAverageShardBytes.getFuture(), readyToStart, mi.id() ), "DDTracker", mi.id(), &normalDDQueueErrors() ) ); + actors.push_back( reportErrorsExcept( dataDistributionQueue( cx, output, getShardMetrics, tci, shardsAffectedByTeamFailure, lock, getAverageShardBytes, mi, configuration.storageTeamSize, configuration.durableStorageQuorum, lastLimited ), "DDQueue", mi.id(), &normalDDQueueErrors() ) ); + actors.push_back( reportErrorsExcept( dataDistributionTeamCollection( initData, tci, cx, db, shardsAffectedByTeamFailure, lock, output, mi.id(), configuration.storageTeamSize, configuration.minDataCenters, + configuration.desiredDataCenters, configuration.storagePolicy, configuration.storageServerStoreType, serverChanges, readyToStart.getFuture() ), "DDTeamCollection", mi.id(), &normalDDQueueErrors() ) ); + + Void _ = wait( waitForAll( actors ) ); + return Void(); + } + catch( Error &e ) { + state Error err = e; + if( e.code() != error_code_movekeys_conflict ) + throw err; + bool ddEnabled = wait( isDataDistributionEnabled(cx) ); + TraceEvent("DataDistributionMoveKeysConflict").detail("ddEnabled", ddEnabled); + if( ddEnabled ) + throw err; + } + } +} + +DDTeamCollection* testTeamCollection(int teamSize, IRepPolicyRef policy, int processCount) { + Database database = DatabaseContext::create( + Reference>(new AsyncVar()), + Never(), + LocalityData(), + false + ); + + DDTeamCollection* collection = new DDTeamCollection( + database, + UID(0, 0), + MoveKeysLock(), + PromiseStream(), + Reference(new ShardsAffectedByTeamFailure()), + teamSize, + -1, + -1, + policy, + KeyValueStoreType(), + PromiseStream>>() + ); + + for(int id = 1; id <= processCount; id++) { + UID uid(id, 0); + StorageServerInterface interface; + interface.uniqueID = uid; + interface.locality.set(LiteralStringRef("machineid"), Standalone(std::to_string(id))); + interface.locality.set(LiteralStringRef("zoneid"), Standalone(std::to_string(id % 5))); + interface.locality.set(LiteralStringRef("data_hall"), Standalone(std::to_string(id % 3))); + collection->server_info[uid] = Reference(new TCServerInfo( + interface, + ProcessClass() + )); + } + + return collection; +} + +TEST_CASE("DataDistribution/AddAllTeams/isExhaustive") { + IRepPolicyRef policy = IRepPolicyRef(new PolicyAcross(3, "zoneid", IRepPolicyRef(new PolicyOne()))); + state DDTeamCollection* collection = testTeamCollection(3, policy, 10); + + vector processes; + for(auto process = collection->server_info.begin(); process != collection->server_info.end(); process++) { + processes.push_back(process->first); + } + + state vector> teams; + int result = wait(collection->addAllTeams(collection, processes, &teams, 200)); + delete(collection); + + for(int i = 0; i < teams.size(); i++) { + auto team = teams[i]; + } + ASSERT(result == 80); + ASSERT(teams[0] == std::vector({ UID(1,0), UID(2,0), UID(3,0) })); + ASSERT(teams[1] == std::vector({ UID(1,0), UID(2,0), UID(4,0) })); + ASSERT(teams[2] == std::vector({ UID(1,0), UID(2,0), UID(5,0) })); + ASSERT(teams[3] == std::vector({ UID(1,0), UID(2,0), UID(8,0) })); + ASSERT(teams[4] == std::vector({ UID(1,0), UID(2,0), UID(9,0) })); + ASSERT(teams[5] == std::vector({ UID(1,0), UID(2,0), UID(10,0) })); + ASSERT(teams[6] == std::vector({ UID(1,0), UID(3,0), UID(4,0) })); + ASSERT(teams[7] == std::vector({ UID(1,0), UID(3,0), UID(5,0) })); + ASSERT(teams[8] == std::vector({ UID(1,0), UID(3,0), UID(7,0) })); + ASSERT(teams[9] == std::vector({ UID(1,0), UID(3,0), UID(9,0) })); + ASSERT(teams[10] == std::vector({ UID(1,0), UID(3,0), UID(10,0) })); + ASSERT(teams[79] == std::vector({ UID(8,0), UID(9,0), UID(10,0) })); + + return Void(); +} + +TEST_CASE("DataDistribution/AddAllTeams/withLimit") { + IRepPolicyRef policy = IRepPolicyRef(new PolicyAcross(3, "zoneid", IRepPolicyRef(new PolicyOne()))); + state DDTeamCollection* collection = testTeamCollection(3, policy, 10); + + vector processes; + for(auto process = collection->server_info.begin(); process != collection->server_info.end(); process++) { + processes.push_back(process->first); + } + + state vector> teams; + int result = wait(collection->addAllTeams(collection, processes, &teams, 10)); + delete(collection); + + for(int i = 0; i < teams.size(); i++) { + auto team = teams[i]; + } + ASSERT(result == 10); + ASSERT(teams[0] == std::vector({ UID(1,0), UID(2,0), UID(3,0) })); + ASSERT(teams[1] == std::vector({ UID(1,0), UID(2,0), UID(4,0) })); + ASSERT(teams[2] == std::vector({ UID(1,0), UID(2,0), UID(5,0) })); + ASSERT(teams[3] == std::vector({ UID(1,0), UID(2,0), UID(8,0) })); + ASSERT(teams[4] == std::vector({ UID(1,0), UID(2,0), UID(9,0) })); + ASSERT(teams[5] == std::vector({ UID(1,0), UID(2,0), UID(10,0) })); + ASSERT(teams[6] == std::vector({ UID(1,0), UID(3,0), UID(4,0) })); + ASSERT(teams[7] == std::vector({ UID(1,0), UID(3,0), UID(5,0) })); + ASSERT(teams[8] == std::vector({ UID(1,0), UID(3,0), UID(7,0) })); + ASSERT(teams[9] == std::vector({ UID(1,0), UID(3,0), UID(9,0) })); + + return Void(); +} + +TEST_CASE("DataDistribution/AddTeamsBestOf/SkippingBusyServers") { + Void _ = wait(Future(Void())); + IRepPolicyRef policy = IRepPolicyRef(new PolicyAcross(3, "zoneid", IRepPolicyRef(new PolicyOne()))); + state DDTeamCollection* collection = testTeamCollection(3, policy, 10); + + collection->addTeam(std::set({ UID(1,0), UID(2,0), UID(3,0) })); + collection->addTeam(std::set({ UID(1,0), UID(3,0), UID(4,0) })); + + int result = collection->addTeamsBestOf(8); + + ASSERT(result == 8); + + for(auto process = collection->server_info.begin(); process != collection->server_info.end(); process++) { + auto teamCount = process->second->teams.size(); + ASSERT(teamCount >= 1); + ASSERT(teamCount <= 5); + } + + delete(collection); + + return Void(); +} + +TEST_CASE("DataDistribution/AddTeamsBestOf/NotEnoughServers") { + Void _ = wait(Future(Void())); + + IRepPolicyRef policy = IRepPolicyRef(new PolicyAcross(3, "zoneid", IRepPolicyRef(new PolicyOne()))); + state DDTeamCollection* collection = testTeamCollection(3, policy, 5); + + collection->addTeam(std::set({ UID(1,0), UID(2,0), UID(3,0) })); + collection->addTeam(std::set({ UID(1,0), UID(3,0), UID(4,0) })); + + int result = collection->addTeamsBestOf(10); + delete(collection); + + ASSERT(result == 8); + + return Void(); +} diff --git a/fdbserver/DataDistribution.h b/fdbserver/DataDistribution.h new file mode 100644 index 0000000000..12396d7bd8 --- /dev/null +++ b/fdbserver/DataDistribution.h @@ -0,0 +1,216 @@ +/* + * DataDistribution.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fdbclient/NativeAPI.h" +#include "ClusterRecruitmentInterface.h" +#include "MoveKeys.h" +#include "LogSystem.h" + +struct RelocateShard { + KeyRange keys; + int priority; + + RelocateShard() {} + RelocateShard( KeyRange const& keys, int priority ) : keys(keys), priority(priority) {} +}; + +// Higher priorities are executed first +// Priority/100 is the "priority group"/"superpriority". Priority inversion +// is possible within but not between priority groups; fewer priority groups +// mean better worst case time bounds +enum { + PRIORITY_REBALANCE_SHARD = 100, + PRIORITY_RECOVER_MOVE = 110, + PRIORITY_REBALANCE_UNDERUTILIZED_TEAM = 120, + PRIORITY_REBALANCE_OVERUTILIZED_TEAM = 121, + PRIORITY_TEAM_HEALTHY = 140, + PRIORITY_TEAM_CONTAINS_UNDESIRED_SERVER = 150, + + PRIORITY_MERGE_SHARD = 240, + PRIORITY_SPLIT_SHARD = 250, + + PRIORITY_TEAM_UNHEALTHY = 800, + PRIORITY_TEAM_2_LEFT = 809, + + PRIORITY_TEAM_1_LEFT = 900, + + PRIORITY_TEAM_0_LEFT = 999 +}; + +enum { + SOME_SHARED = 2, + NONE_SHARED = 3 +}; + +struct IDataDistributionTeam { + virtual vector getLastKnownServerInterfaces() = 0; + virtual vector const& getServerIDs() = 0; + virtual void addDataInFlightToTeam( int64_t delta ) = 0; + virtual int64_t getDataInFlightToTeam() = 0; + virtual int64_t getLoadBytes( bool includeInFlight = true, double inflightPenalty = 1.0 ) = 0; + virtual int64_t getMinFreeSpace( bool includeInFlight = true ) = 0; + virtual double getMinFreeSpaceRatio( bool includeInFlight = true ) = 0; + virtual bool hasHealthyFreeSpace() = 0; + virtual Future updatePhysicalMetrics() = 0; + virtual void addref() = 0; + virtual void delref() = 0; + virtual bool isHealthy() = 0; + virtual void setHealthy(bool) = 0; + virtual int getPriority() = 0; + virtual void setPriority(int) = 0; + virtual bool isOptimal() = 0; + virtual bool isWrongConfiguration() = 0; + virtual void setWrongConfiguration(bool) = 0; + + std::string getDesc() { + const auto& servers = getLastKnownServerInterfaces(); + std::string s = format("Size %d; ", servers.size()); + for(int i=0; i sources; + Promise< Optional< Reference > > reply; + + GetTeamRequest() {} + GetTeamRequest( bool wantsNewServers, bool wantsTrueBest, bool preferLowerUtilization, double inflightPenalty = 1.0 ) : wantsNewServers( wantsNewServers ), wantsTrueBest( wantsTrueBest ), preferLowerUtilization( preferLowerUtilization ), inflightPenalty(inflightPenalty) {} +}; + +struct GetMetricsRequest { + KeyRange keys; + Promise< StorageMetrics > reply; + + GetMetricsRequest() {} + GetMetricsRequest( KeyRange const& keys ) : keys(keys) {} +}; + +struct TeamCollectionInterface { + PromiseStream< GetTeamRequest > getTeam; +}; + +class ShardsAffectedByTeamFailure : public ReferenceCounted { +public: + ShardsAffectedByTeamFailure() {} + typedef vector Team; // sorted + // This tracks the data distribution on the data distribution server so that teamTrackers can + // relocate the right shards when a team is degraded. + + // The following are important to make sure that failure responses don't revert splits or merges: + // - The shards boundaries in the two data structures reflect "queued" RelocateShard requests + // (i.e. reflects the desired set of shards being tracked by dataDistributionTracker, + // rather than the status quo). These boundaries are modified in defineShard and the content + // of what servers correspond to each shard is a copy or union of the shards already there + // - The teams associated with each shard reflect either the sources for non-moving shards + // or the destination team for in-flight shards (the change is atomic with respect to team selection). + // moveShard() changes the servers associated with a shard and will never adjust the shard + // boundaries. If a move is received for a shard that has been redefined (the exact shard is + // no longer in the map), the servers will be set for all contained shards and added to all + // intersecting shards. + + int getNumberOfShards( UID ssID ); + vector getShardsFor( Team team ); + vector> getTeamsFor( KeyRangeRef keys ); + void defineShard( KeyRangeRef keys ); + void moveShard( KeyRangeRef keys, Team destinationTeam ); + void check(); +private: + struct OrderByTeamKey { + bool operator()( const std::pair& lhs, const std::pair& rhs ) const { + if (lhs.first < rhs.first) return true; + if (lhs.first > rhs.first) return false; + return lhs.second.begin < rhs.second.begin; + } + }; + + KeyRangeMap< vector > shard_teams; // A shard can be affected by the failure of multiple teams if it is a queued merge + std::set< std::pair, OrderByTeamKey > team_shards; + std::map< UID, int > storageServerShards; + + void erase(Team team, KeyRange const& range); + void insert(Team team, KeyRange const& range); +}; + +struct InitialDataDistribution : ReferenceCounted { + typedef vector Team; // sorted + int mode; + vector> allServers; + std::set< Team > teams; + vector>> shards; +}; + +Future dataDistribution( + Reference> const& db, + MasterInterface const& mi, DatabaseConfiguration const& configuration, + PromiseStream< std::pair> > const& serverChanges, + Reference const& logSystem, + Version const& recoveryCommitVersion, + double* const& lastLimited); + +Future dataDistributionTracker( + Reference const& initData, + Database const& cx, + Reference const& shardsAffectedByTeamFailure, + PromiseStream const& output, + PromiseStream const& getShardMetrics, + FutureStream> const& getAverageShardBytes, + Promise const& readyToStart, + UID const& masterId); + +Future dataDistributionQueue( + Database const& cx, + PromiseStream const& input, + PromiseStream const& getShardMetrics, + TeamCollectionInterface const& teamCollection, + Reference const& shardsAffectedByTeamFailure, + MoveKeysLock const& lock, + PromiseStream> const& getAverageShardBytes, + MasterInterface const& mi, + int const& teamSize, + int const& durableStorageQuorum, + double* const& lastLimited ); + +//Holds the permitted size and IO Bounds for a shard +struct ShardSizeBounds { + StorageMetrics max; + StorageMetrics min; + StorageMetrics permittedError; + + bool operator == ( ShardSizeBounds const& rhs ) const { + return max == rhs.max && min == rhs.min && permittedError == rhs.permittedError; + } +}; + +//Gets the permitted size and IO bounds for a shard +ShardSizeBounds getShardSizeBounds(KeyRangeRef shard, int64_t maxShardSize); + +//Determines the maximum shard size based on the size of the database +int64_t getMaxShardSize( double dbSizeEstimate ); + +Future enableDataDistribution( Database const& cx, int const& mode ); +Future disableDataDistribution( Database const& cx ); \ No newline at end of file diff --git a/fdbserver/DataDistributionQueue.actor.cpp b/fdbserver/DataDistributionQueue.actor.cpp new file mode 100644 index 0000000000..43621a97f7 --- /dev/null +++ b/fdbserver/DataDistributionQueue.actor.cpp @@ -0,0 +1,1006 @@ +/* + * DataDistributionQueue.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/ActorCollection.h" +#include "fdbrpc/sim_validation.h" +#include "fdbclient/SystemData.h" +#include "DataDistribution.h" +#include "fdbclient/DatabaseContext.h" +#include "MoveKeys.h" +#include "Knobs.h" +#include "fdbrpc/simulator.h" + +#define WORK_FULL_UTILIZATION 10000 // This is not a knob; it is a fixed point scaling factor! + +struct RelocateData { + KeyRange keys; + int priority; + double startTime; + UID randomId; + int workFactor; + std::vector src; + bool wantsNewServers; + TraceInterval interval; + + RelocateData() : startTime(-1), priority(-1), workFactor(0), wantsNewServers(false), interval("QueuedRelocation") {} + RelocateData( RelocateShard const& rs ) : keys(rs.keys), priority(rs.priority), startTime(now()), randomId(g_random->randomUniqueID()), workFactor(0), + wantsNewServers( + rs.priority == PRIORITY_REBALANCE_SHARD || + rs.priority == PRIORITY_REBALANCE_OVERUTILIZED_TEAM || + rs.priority == PRIORITY_REBALANCE_UNDERUTILIZED_TEAM || + rs.priority == PRIORITY_SPLIT_SHARD ), interval("QueuedRelocation") {} + + bool operator> (const RelocateData& rhs) const { + return priority != rhs.priority ? priority > rhs.priority : ( startTime != rhs.startTime ? startTime < rhs.startTime : randomId > rhs.randomId ); + } + + bool operator== (const RelocateData& rhs) const { + return priority == rhs.priority && keys == rhs.keys && startTime == rhs.startTime && workFactor == rhs.workFactor && src == rhs.src && wantsNewServers == rhs.wantsNewServers && randomId == rhs.randomId; + } + + bool changesBoundaries() { + return priority == PRIORITY_MERGE_SHARD || + priority == PRIORITY_SPLIT_SHARD || + priority == PRIORITY_RECOVER_MOVE; + } +}; + +struct Busyness { + vector ledger; + + Busyness() : ledger( 10, 0 ) {} + + bool canLaunch( int prio, int work ) { + ASSERT( prio > 0 && prio < 1000 ); + return ledger[ prio / 100 ] <= WORK_FULL_UTILIZATION - work; // allow for rounding errors in double division + } + void addWork( int prio, int work ) { + ASSERT( prio > 0 && prio < 1000 ); + for( int i = 0; i <= (prio / 100); i++ ) + ledger[i] += work; + } + void removeWork( int prio, int work ) { + addWork( prio, -work ); + } + std::string toString() { + std::string result; + for(int i = 1; i < ledger.size();) { + int j = i+1; + while(j < ledger.size() && ledger[i] == ledger[j]) + j++; + if(i != 1) + result += ", "; + result += i+1 == j ? format("%03d", i*100) : format("%03d/%03d", i*100, (j-1)*100); + result += format("=%1.02f", (float)ledger[i] / WORK_FULL_UTILIZATION); + i = j; + } + return result; + } +}; + +// find the "workFactor" for this, were it launched now +int getWorkFactor( RelocateData const& relocation ) { + // Avoid the divide by 0! + ASSERT( relocation.src.size() ); + + if( relocation.priority >= PRIORITY_TEAM_1_LEFT ) + return WORK_FULL_UTILIZATION / SERVER_KNOBS->RELOCATION_PARALLELISM_PER_SOURCE_SERVER; + else if( relocation.priority >= PRIORITY_TEAM_2_LEFT ) + return WORK_FULL_UTILIZATION / 2 / SERVER_KNOBS->RELOCATION_PARALLELISM_PER_SOURCE_SERVER; + else // for now we assume that any message at a lower priority can best be assumed to have a full team left for work + return WORK_FULL_UTILIZATION / relocation.src.size() / SERVER_KNOBS->RELOCATION_PARALLELISM_PER_SOURCE_SERVER; +} + +// return true if servers are not too busy to launch the relocation +bool canLaunch( RelocateData & relocation, int teamSize, std::map & busymap, + std::vector cancellableRelocations ) { + // assert this has not already been launched + ASSERT( relocation.workFactor == 0 ); + ASSERT( relocation.src.size() != 0 ); + + // find the "workFactor" for this, were it launched now + int workFactor = getWorkFactor( relocation ); + int neededServers = std::max( 1, (int)relocation.src.size() - teamSize + 1 ); + // see if each of the SS can launch this task + for( int i = 0; i < relocation.src.size(); i++ ) { + // For each source server for this relocation, copy and modify its busyness to reflect work that WOULD be cancelled + auto busyCopy = busymap[ relocation.src[i] ]; + for( int j = 0; j < cancellableRelocations.size(); j++ ) { + auto& servers = cancellableRelocations[j].src; + if( std::count( servers.begin(), servers.end(), relocation.src[i] ) ) + busyCopy.removeWork( cancellableRelocations[j].priority, cancellableRelocations[j].workFactor ); + } + // Use this modified busyness to check if this relocation could be launched + if( busyCopy.canLaunch( relocation.priority, workFactor ) ) { + --neededServers; + if( neededServers == 0 ) + return true; + } + } + return false; +} + +// update busyness for each server +void launch( RelocateData & relocation, std::map & busymap ) { + // if we are here this means that we can launch and should adjust all the work the servers can do + relocation.workFactor = getWorkFactor( relocation ); + for( int i = 0; i < relocation.src.size(); i++ ) + busymap[ relocation.src[i] ].addWork( relocation.priority, relocation.workFactor ); +} + +void complete( RelocateData const& relocation, std::map & busymap ) { + ASSERT( relocation.workFactor > 0 ); + for( int i = 0; i < relocation.src.size(); i++ ) + busymap[ relocation.src[i] ].removeWork( relocation.priority, relocation.workFactor ); +} + +Future dataDistributionRelocator( struct DDQueueData* const& self, RelocateData const& rd ); + +struct DDQueueData { + MasterInterface mi; + MoveKeysLock lock; + Database cx; + + TeamCollectionInterface teamCollection; + Reference shardsAffectedByTeamFailure; + PromiseStream> getAverageShardBytes; + + FlowLock startMoveKeysParallelismLock; + FlowLock finishMoveKeysParallelismLock; + + int activeRelocations; + int queuedRelocations; + int bytesWritten; + std::map priority_relocations; + int teamSize; + int durableStorageQuorum; + + std::map busymap; + + KeyRangeMap< RelocateData > queueMap; + std::set> fetchingSourcesQueue; + std::set> fetchKeysComplete; + KeyRangeActorMap getSourceActors; + std::map>> queue; + + KeyRangeMap< RelocateData > inFlight; + KeyRangeActorMap inFlightActors; + + Promise error; + PromiseStream dataTransferComplete; + PromiseStream relocationComplete; + PromiseStream fetchSourceServersComplete; + + PromiseStream input; + PromiseStream getShardMetrics; + + double* lastLimited; + + DDQueueData( MasterInterface mi, MoveKeysLock lock, Database cx, TeamCollectionInterface teamCollection, + Reference sABTF, PromiseStream> getAverageShardBytes, + int teamSize, int durableStorageQuorum, PromiseStream input, + PromiseStream getShardMetrics, double* lastLimited ) : + activeRelocations( 0 ), queuedRelocations( 0 ), bytesWritten ( 0 ), teamCollection( teamCollection ), + shardsAffectedByTeamFailure( sABTF ), getAverageShardBytes( getAverageShardBytes ), mi( mi ), lock( lock ), + cx( cx ), teamSize( teamSize ), durableStorageQuorum( durableStorageQuorum ), input( input ), + getShardMetrics( getShardMetrics ), startMoveKeysParallelismLock( SERVER_KNOBS->DD_MOVE_KEYS_PARALLELISM ), + finishMoveKeysParallelismLock( SERVER_KNOBS->DD_MOVE_KEYS_PARALLELISM ), lastLimited(lastLimited) {} + + void validate() { + if( EXPENSIVE_VALIDATION ) { + for( auto it = fetchingSourcesQueue.begin(); it != fetchingSourcesQueue.end(); ++it ) { + // relocates in the fetching queue do not have src servers yet. + if( it->src.size() ) + TraceEvent(SevError, "DDQueueValidateError1").detail("Problem", "relocates in the fetching queue do not have src servers yet"); + + // relocates in the fetching queue do not have a work factor yet. + if( it->workFactor != 0.0 ) + TraceEvent(SevError, "DDQueueValidateError2").detail("Problem", "relocates in the fetching queue do not have a work factor yet"); + + // relocates in the fetching queue are in the queueMap. + auto range = queueMap.rangeContaining( it->keys.begin ); + if( range.value() != *it || range.range() != it->keys ) + TraceEvent(SevError, "DDQueueValidateError3").detail("Problem", "relocates in the fetching queue are in the queueMap"); + } + + /* + for( auto it = queue.begin(); it != queue.end(); ++it ) { + for( auto rdit = it->second.begin(); rdit != it->second.end(); ++rdit ) { + // relocates in the queue are in the queueMap exactly. + auto range = queueMap.rangeContaining( rdit->keys.begin ); + if( range.value() != *rdit || range.range() != rdit->keys ) + TraceEvent(SevError, "DDQueueValidateError4").detail("Problem", "relocates in the queue are in the queueMap exactly") + .detail("RangeBegin", printable(range.range().begin)) + .detail("RangeEnd", printable(range.range().end)) + .detail("RelocateBegin2", printable(range.value().keys.begin)) + .detail("RelocateEnd2", printable(range.value().keys.end)) + .detail("RelocateStart", range.value().startTime) + .detail("MapStart", rdit->startTime) + .detail("RelocateWork", range.value().workFactor) + .detail("MapWork", rdit->workFactor) + .detail("RelocateSrc", range.value().src.size()) + .detail("MapSrc", rdit->src.size()) + .detail("RelocatePrio", range.value().priority) + .detail("MapPrio", rdit->priority); + + // relocates in the queue have src servers + if( !rdit->src.size() ) + TraceEvent(SevError, "DDQueueValidateError5").detail("Problem", "relocates in the queue have src servers"); + + // relocates in the queue do not have a work factor yet. + if( rdit->workFactor != 0.0 ) + TraceEvent(SevError, "DDQueueValidateError6").detail("Problem", "relocates in the queue do not have a work factor yet"); + + bool contains = false; + for( int i = 0; i < rdit->src.size(); i++ ) { + if( rdit->src[i] == it->first ) { + contains = true; + break; + } + } + if( !contains ) + TraceEvent(SevError, "DDQueueValidateError7").detail("Problem", "queued relocate data does not include ss under which its filed"); + } + }*/ + + auto inFlightRanges = inFlight.ranges(); + for( auto it = inFlightRanges.begin(); it != inFlightRanges.end(); ++it ) { + for( int i = 0; i < it->value().src.size(); i++ ) { + // each server in the inFlight map is in the busymap + if( !busymap.count( it->value().src[i] ) ) + TraceEvent(SevError, "DDQueueValidateError8").detail("Problem", "each server in the inFlight map is in the busymap"); + + // relocate data that is inFlight is not also in the queue + if( queue[it->value().src[i]].count( it->value() ) ) + TraceEvent(SevError, "DDQueueValidateError9").detail("Problem", "relocate data that is inFlight is not also in the queue"); + } + + // in flight relocates have source servers + if( it->value().startTime != -1 && !it->value().src.size() ) + TraceEvent(SevError, "DDQueueValidateError10").detail("Problem", "in flight relocates have source servers"); + + if( inFlightActors.liveActorAt( it->range().begin ) ) { + // the key range in the inFlight map matches the key range in the RelocateData message + if( it->value().keys != it->range() ) + TraceEvent(SevError, "DDQueueValidateError11").detail("Problem", "the key range in the inFlight map matches the key range in the RelocateData message"); + } + } + + for( auto it = busymap.begin(); it != busymap.end(); ++it ) { + for( int i = 0; i < it->second.ledger.size() - 1; i++ ) { + if( it->second.ledger[i] < it->second.ledger[i+1] ) + TraceEvent(SevError, "DDQueueValidateError12").detail("Problem", "ascending ledger problem") + .detail("ledgerLevel", i).detail("ledgerValueA", it->second.ledger[i]).detail("ledgerValueB", it->second.ledger[i+1]); + if( it->second.ledger[i] < 0.0 ) + TraceEvent(SevError, "DDQueueValidateError13").detail("Problem", "negative ascending problem") + .detail("ledgerLevel", i).detail("ledgerValue", it->second.ledger[i]); + } + } + + std::set> queuedRelocationsMatch; + for(auto it = queue.begin(); it != queue.end(); ++it) + queuedRelocationsMatch.insert( it->second.begin(), it->second.end() ); + ASSERT( queuedRelocations == queuedRelocationsMatch.size() + fetchingSourcesQueue.size() ); + + int testActive = 0; + for(auto it = priority_relocations.begin(); it != priority_relocations.end(); ++it ) + testActive += it->second; + ASSERT( activeRelocations + queuedRelocations == testActive ); + } + } + + ACTOR Future getSourceServersForRange( Database cx, MasterInterface mi, RelocateData input, PromiseStream output ) { + state std::set servers; + state Transaction tr(cx); + + // FIXME: is the merge case needed + if( input.priority == PRIORITY_MERGE_SHARD ) { + Void _ = wait( delay( 0.5, TaskDataDistribution - 2 ) ); + } else { + Void _ = wait( delay( 0.0001, TaskDataDistributionLaunch ) ); + } + + loop { + servers.clear(); + tr.setOption( FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE ); + try { + Standalone keyServersEntries = wait( + tr.getRange( lastLessOrEqual( keyServersKey( input.keys.begin ) ), + firstGreaterOrEqual( keyServersKey( input.keys.end ) ), SERVER_KNOBS->DD_QUEUE_MAX_KEY_SERVERS ) ); + + if(keyServersEntries.size() < SERVER_KNOBS->DD_QUEUE_MAX_KEY_SERVERS) { + for( int shard = 0; shard < keyServersEntries.size(); shard++ ) { + vector src, dest; + decodeKeyServersValue( keyServersEntries[shard].value, src, dest ); + ASSERT( src.size() ); + for( int i = 0; i < src.size(); i++ ) + servers.insert( src[i] ); + } + + ASSERT(servers.size() > 0); + } + + //If the size of keyServerEntries is large, then just assume we are using all storage servers + else { + Standalone serverList = wait( tr.getRange( serverListKeys, CLIENT_KNOBS->TOO_MANY ) ); + ASSERT( !serverList.more && serverList.size() < CLIENT_KNOBS->TOO_MANY ); + + for(auto s = serverList.begin(); s != serverList.end(); ++s) + servers.insert(decodeServerListValue( s->value ).id()); + + ASSERT(servers.size() > 0); + } + + break; + } catch( Error& e ) { + Void _ = wait( tr.onError(e) ); + } + } + + input.src = std::vector( servers.begin(), servers.end() ); + output.send( input ); + return Void(); + } + + //This function cannot handle relocation requests which split a shard into three pieces + void queueRelocation( RelocateData rd, std::set &serversToLaunchFrom ) { + // Update sabtf for changes from DDTracker + if( rd.changesBoundaries() ) + shardsAffectedByTeamFailure->defineShard( rd.keys ); + + //TraceEvent("QueueRelocationBegin").detail("Begin", printable(rd.keys.begin)).detail("End", printable(rd.keys.end)); + + // remove all items from both queues that are fully contained in the new relocation (i.e. will be overwritten) + auto ranges = queueMap.intersectingRanges( rd.keys ); + for(auto r = ranges.begin(); r != ranges.end(); ++r ) { + RelocateData& rrs = r->value(); + + auto fetchingSourcesItr = fetchingSourcesQueue.find(rrs); + bool foundActiveFetching = fetchingSourcesItr != fetchingSourcesQueue.end(); + std::set>* firstQueue; + std::set>::iterator firstRelocationItr; + bool foundActiveRelocation = false; + + if( !foundActiveFetching && rrs.src.size() ) { + firstQueue = &queue[rrs.src[0]]; + firstRelocationItr = firstQueue->find( rrs ); + foundActiveRelocation = firstRelocationItr != firstQueue->end(); + } + + // If there is a queued job that wants data relocation which we are about to cancel/modify, + // make sure that we keep the relocation intent for the job that we queue up + if( foundActiveFetching || foundActiveRelocation ) { + rd.wantsNewServers |= rrs.wantsNewServers; + rd.startTime = std::min( rd.startTime, rrs.startTime ); + if( rrs.priority >= PRIORITY_TEAM_UNHEALTHY && rd.changesBoundaries() ) + rd.priority = std::max( rd.priority, rrs.priority ); + } + + if( rd.keys.contains( rrs.keys ) ) { + if(foundActiveFetching) + fetchingSourcesQueue.erase( fetchingSourcesItr ); + else if(foundActiveRelocation) { + firstQueue->erase( firstRelocationItr ); + for( int i = 1; i < rrs.src.size(); i++ ) + queue[rrs.src[i]].erase( rrs ); + } + } + + if( foundActiveFetching || foundActiveRelocation ) { + serversToLaunchFrom.insert( rrs.src.begin(), rrs.src.end() ); + /*TraceEvent(rrs.interval.end(), mi.id()).detail("Result","Cancelled") + .detail("WasFetching", foundActiveFetching).detail("Contained", rd.keys.contains( rrs.keys ));*/ + queuedRelocations--; + priority_relocations[ rrs.priority ]--; + } + } + + // determine the final state of the relocations map + auto affectedQueuedItems = queueMap.getAffectedRangesAfterInsertion( rd.keys, rd ); + + // put the new request into the global map of requests (modifies the ranges already present) + queueMap.insert( rd.keys, rd ); + + // cancel all the getSourceServers actors that intersect the new range that we will be getting + getSourceActors.cancel( KeyRangeRef( affectedQueuedItems.front().begin, affectedQueuedItems.back().end ) ); + + // update fetchingSourcesQueue and the per-server queue based on truncated ranges after insertion, (re-)launch getSourceServers + auto queueMapItr = queueMap.rangeContaining(affectedQueuedItems[0].begin); + for(int r = 0; r < affectedQueuedItems.size(); ++r, ++queueMapItr) { + //ASSERT(queueMapItr->value() == queueMap.rangeContaining(affectedQueuedItems[r].begin)->value()); + RelocateData& rrs = queueMapItr->value(); + + if( rrs.src.size() == 0 && ( rrs.keys == rd.keys || fetchingSourcesQueue.erase(rrs) > 0 ) ) { + rrs.keys = affectedQueuedItems[r]; + + rrs.interval = TraceInterval("QueuedRelocation"); + /*TraceEvent(rrs.interval.begin(), mi.id()); + .detail("KeyBegin", printable(rrs.keys.begin)).detail("KeyEnd", printable(rrs.keys.end)) + .detail("Priority", rrs.priority).detail("WantsNewServers", rrs.wantsNewServers);*/ + queuedRelocations++; + priority_relocations[rrs.priority]++; + + fetchingSourcesQueue.insert( rrs ); + getSourceActors.insert( rrs.keys, getSourceServersForRange( cx, mi, rrs, fetchSourceServersComplete ) ); + } else { + RelocateData newData( rrs ); + newData.keys = affectedQueuedItems[r]; + ASSERT( rrs.src.size() || rrs.startTime == -1 ); + + bool foundActiveRelocation = false; + for( int i = 0; i < rrs.src.size(); i++ ) { + auto& serverQueue = queue[rrs.src[i]]; + + if( serverQueue.erase(rrs) > 0 ) { + if( !foundActiveRelocation ) { + newData.interval = TraceInterval("QueuedRelocation"); + /*TraceEvent(newData.interval.begin(), mi.id()); + .detail("KeyBegin", printable(newData.keys.begin)).detail("KeyEnd", printable(newData.keys.end)) + .detail("Priority", newData.priority).detail("WantsNewServers", newData.wantsNewServers);*/ + queuedRelocations++; + priority_relocations[newData.priority]++; + foundActiveRelocation = true; + } + + serverQueue.insert( newData ); + } + else + break; + } + + // We update the keys of a relocation even if it is "dead" since it helps validate() + rrs.keys = affectedQueuedItems[r]; + rrs.interval = newData.interval; + } + } + + /*TraceEvent("ReceivedRelocateShard", mi.id()) + .detail("KeyBegin", printable(rd.keys.begin)) + .detail("KeyEnd", printable(rd.keys.end)) + .detail("Priority", rd.priority) + .detail("AffectedRanges", affectedQueuedItems.size()); */ + } + + void completeSourceFetch( RelocateData results ) { + ASSERT( fetchingSourcesQueue.count( results ) ); + + //logRelocation( results, "GotSourceServers" ); + + fetchingSourcesQueue.erase( results ); + queueMap.insert( results.keys, results ); + for( int i = 0; i < results.src.size(); i++ ) { + queue[results.src[i]].insert( results ); + } + } + + void logRelocation( RelocateData rd, const char *title ) { + std::string busyString; + for(int i = 0; i < rd.src.size() && i < teamSize * 2; i++) + busyString += describe(rd.src[i]) + " - (" + busymap[ rd.src[i] ].toString() + "); "; + + TraceEvent(title, mi.id()) + .detail("KeyBegin", printable(rd.keys.begin)) + .detail("KeyEnd", printable(rd.keys.end)) + .detail("Priority", rd.priority) + .detail("WorkFactor", rd.workFactor) + .detail("SourceServerCount", rd.src.size()) + .detail("SourceServers", describe(rd.src, teamSize * 2)) + .detail("SourceBusyness", busyString); + } + + void launchQueuedWork( KeyRange keys ) { + //combine all queued work in the key range and check to see if there is anything to launch + std::set> combined; + auto f = queueMap.intersectingRanges( keys ); + for(auto it = f.begin(); it != f.end(); ++it) { + if( it->value().src.size() && queue[it->value().src[0]].count( it->value() ) ) + combined.insert( it->value() ); + } + launchQueuedWork( combined ); + } + + void launchQueuedWork( std::set serversToLaunchFrom ) { + //combine all work from the source servers to see if there is anything new to launch + std::set> combined; + for( auto id : serversToLaunchFrom ) { + auto& queuedWork = queue[id]; + auto it = queuedWork.begin(); + for( int j = 0; j < teamSize && it != queuedWork.end(); j++) { + combined.insert( *it ); + ++it; + } + } + launchQueuedWork( combined ); + } + + void launchQueuedWork( RelocateData launchData ) { + //check a single RelocateData to see if it can be launched + std::set> combined; + combined.insert( launchData ); + launchQueuedWork( combined ); + } + + void launchQueuedWork( std::set> combined ) { + int startedHere = 0; + double startTime = now(); + // kick off relocators from items in the queue as need be + std::set>::iterator it = combined.begin(); + for(; it != combined.end(); it++ ) { + RelocateData rd( *it ); + + bool overlappingInFlight = false; + auto intersectingInFlight = inFlight.intersectingRanges( rd.keys ); + for(auto it = intersectingInFlight.begin(); it != intersectingInFlight.end(); ++it) { + if( fetchKeysComplete.count( it->value() ) && + inFlightActors.liveActorAt( it->range().begin ) && + !rd.keys.contains( it->range() ) && + it->value().priority >= rd.priority && + rd.priority < PRIORITY_TEAM_UNHEALTHY ) { + /*TraceEvent("OverlappingInFlight", mi.id()) + .detail("KeyBegin", printable(it->value().keys.begin)) + .detail("KeyEnd", printable(it->value().keys.end)) + .detail("Priority", it->value().priority); */ + overlappingInFlight = true; + break; + } + } + + if( overlappingInFlight ) { + //logRelocation( rd, "SkippingOverlappingInFlight" ); + continue; + } + + // Because the busyness of a server is decreased when a superseding relocation is issued, we + // need to consider what the busyness of a server WOULD be if + auto containedRanges = inFlight.containedRanges( rd.keys ); + std::vector cancellableRelocations; + for(auto it = containedRanges.begin(); it != containedRanges.end(); ++it) { + if( inFlightActors.liveActorAt( it->range().begin ) ) { + cancellableRelocations.push_back( it->value() ); + } + } + + // SOMEDAY: the list of source servers may be outdated since they were fetched when the work was put in the queue + // FIXME: we need spare capacity even when we're just going to be cancelling work via TEAM_HEALTHY + if( !canLaunch( rd, teamSize, busymap, cancellableRelocations ) ) { + //logRelocation( rd, "SkippingQueuedRelocation" ); + continue; + } + + //logRelocation( rd, "LaunchingRelocation" ); + + //TraceEvent(rd.interval.end(), mi.id()).detail("Result","Success"); + queuedRelocations--; + priority_relocations[rd.priority]--; + + // now we are launching: remove this entry from the queue of all the src servers + for( int i = 0; i < rd.src.size(); i++ ) { + ASSERT( queue[rd.src[i]].erase(rd) ); + } + + // If there is a job in flight that wants data relocation which we are about to cancel/modify, + // make sure that we keep the relocation intent for the job that we launch + auto f = inFlight.intersectingRanges( rd.keys ); + for(auto it = f.begin(); it != f.end(); ++it) { + if( inFlightActors.liveActorAt( it->range().begin ) ) { + rd.wantsNewServers |= it->value().wantsNewServers; + } + } + startedHere++; + + // update both inFlightActors and inFlight key range maps, cancelling deleted RelocateShards + vector ranges; + inFlightActors.getRangesAffectedByInsertion( rd.keys, ranges ); + inFlightActors.cancel( KeyRangeRef( ranges.front().begin, ranges.back().end ) ); + inFlight.insert( rd.keys, rd ); + for(int r=0; rvalue(); + rrs.keys = ranges[r]; + + launch( rrs, busymap ); + activeRelocations++; + priority_relocations[ rrs.priority ]++; + inFlightActors.insert( rrs.keys, dataDistributionRelocator( this, rrs ) ); + } + + //logRelocation( rd, "LaunchedRelocation" ); + } + if( now() - startTime > .001 && g_random->random01()<0.001 ) + TraceEvent(SevWarnAlways, "LaunchingQueueSlowx1000").detail("elapsed", now() - startTime ); + + /*if( startedHere > 0 ) { + TraceEvent("StartedDDRelocators", mi.id()) + .detail("QueueSize", queuedRelocations) + .detail("StartedHere", startedHere) + .detail("ActiveRelocations", activeRelocations); + } */ + + validate(); + } +}; + +extern bool noUnseed; + +// This actor relocates the specified keys to a good place. +// These live in the inFlightActor key range map. +ACTOR Future dataDistributionRelocator( DDQueueData *self, RelocateData rd ) +{ + state Promise errorOut( self->error ); + state TraceInterval relocateShardInterval("RelocateShard"); + state PromiseStream dataTransferComplete( self->dataTransferComplete ); + state PromiseStream relocationComplete( self->relocationComplete ); + state bool signalledTransferComplete = false; + state UID masterId = self->mi.id(); + state Reference destination; + + try { + TraceEvent(relocateShardInterval.begin(), masterId) + .detail("KeyBegin", printable(rd.keys.begin)).detail("KeyEnd", printable(rd.keys.end)) + .detail("Priority", rd.priority).detail("RelocationID", relocateShardInterval.pairID); + + state StorageMetrics metrics = wait( brokenPromiseToNever( self->getShardMetrics.getReply( GetMetricsRequest( rd.keys ) ) ) ); + + ASSERT( rd.src.size() ); + loop { + state int stuckCount = 0; + loop { + double inflightPenalty = SERVER_KNOBS->INFLIGHT_PENALTY_HEALTHY; + if(rd.priority >= PRIORITY_TEAM_UNHEALTHY) inflightPenalty = SERVER_KNOBS->INFLIGHT_PENALTY_UNHEALTHY; + if(rd.priority >= PRIORITY_TEAM_1_LEFT) inflightPenalty = SERVER_KNOBS->INFLIGHT_PENALTY_ONE_LEFT; + + auto req = GetTeamRequest( rd.wantsNewServers, rd.priority == PRIORITY_REBALANCE_UNDERUTILIZED_TEAM, true, inflightPenalty ); + req.sources = rd.src; + Optional> bestTeam = wait( brokenPromiseToNever( self->teamCollection.getTeam.getReply( req ) ) ); + if( bestTeam.present() ) { + destination = bestTeam.get(); + break; + } + TEST(true); //did not find a healthy destination team on the first attempt + stuckCount++; + TraceEvent(stuckCount > 50 ? SevWarnAlways : SevWarn, "BestTeamStuck", masterId).detail("Count", stuckCount); + if(stuckCount > 50 && g_network->isSimulated()) { //FIXME: known bug in simulation we are supressing + int unseed = noUnseed ? 0 : g_random->randomInt(0, 100001); + TraceEvent("ElapsedTime").detail("SimTime", now()).detail("RealTime", 0) + .detail("RandomUnseed", unseed); + flushAndExit(0); + } + Void _ = wait( delay( SERVER_KNOBS->BEST_TEAM_STUCK_DELAY, TaskDataDistributionLaunch ) ); + } + + ASSERT(destination->isHealthy()); // team failure tracking is edge triggered, so must never put something on an unhealthy team! + self->shardsAffectedByTeamFailure->moveShard( rd.keys, destination->getServerIDs() ); + + destination->addDataInFlightToTeam( +metrics.bytes ); + + TraceEvent("RelocateShardHasDestination", masterId) + .detail("PairId", relocateShardInterval.pairID) + .detail("DestinationTeam", destination->getDesc()); + + state Error error = success(); + state Promise dataMovementComplete; + state Future doMoveKeys = moveKeys( + self->cx, rd.keys, destination->getServerIDs(), self->lock, + self->durableStorageQuorum, dataMovementComplete, + &self->startMoveKeysParallelismLock, + &self->finishMoveKeysParallelismLock, + relocateShardInterval.pairID ); + state Future pollHealth = signalledTransferComplete ? Never() : delay( SERVER_KNOBS->HEALTH_POLL_TIME, TaskDataDistributionLaunch ); + try { + loop { + choose { + when( Void _ = wait( doMoveKeys ) ) { + self->fetchKeysComplete.insert( rd ); + break; + } + when( Void _ = wait( pollHealth ) ) { + if( !destination->isHealthy() ) { + if( !signalledTransferComplete ) { + signalledTransferComplete = true; + self->dataTransferComplete.send( rd ); + } + } + pollHealth = signalledTransferComplete ? Never() : delay( SERVER_KNOBS->HEALTH_POLL_TIME, TaskDataDistributionLaunch ); + } + when( Void _ = wait( signalledTransferComplete ? Never() : dataMovementComplete.getFuture() ) ) { + self->fetchKeysComplete.insert( rd ); + if( !signalledTransferComplete ) { + signalledTransferComplete = true; + self->dataTransferComplete.send( rd ); + } + } + } + } + } catch( Error& e ) { + error = e; + } + + //TraceEvent("RelocateShardFinished", masterId).detail("relocateId", relocateShardInterval.pairID); + + if( error.code() != error_code_move_to_removed_server ) { + if( !error.code() ) { + try { + Void _ = wait( destination->updatePhysicalMetrics() ); //prevent a gap between the polling for an increase in physical metrics and decrementing data in flight + } catch( Error& e ) { + error = e; + } + } + + destination->addDataInFlightToTeam( -metrics.bytes ); + + // onFinished.send( rs ); + if( !error.code() ) { + TraceEvent(relocateShardInterval.end(), masterId).detail("Result","Success"); + if(rd.keys.begin == keyServersPrefix) { + TraceEvent("MovedKeyServerKeys").detail("dest", destination->getDesc()).trackLatest("MovedKeyServers"); + } + + if( !signalledTransferComplete ) { + signalledTransferComplete = true; + dataTransferComplete.send( rd ); + } + + self->bytesWritten += metrics.bytes; + relocationComplete.send( rd ); + return Void(); + } else { + throw error; + } + } else { + TEST(true); // move to removed server + destination->addDataInFlightToTeam( -metrics.bytes ); + Void _ = wait( delay( SERVER_KNOBS->RETRY_RELOCATESHARD_DELAY, TaskDataDistributionLaunch ) ); + } + } + } catch (Error& e) { + TraceEvent(relocateShardInterval.end(), masterId).error(e, true); + if( !signalledTransferComplete ) + dataTransferComplete.send( rd ); + + relocationComplete.send( rd ); + + if( e.code() != error_code_actor_cancelled ) + errorOut.sendError(e); + throw; + } +} + +ACTOR Future rebalanceTeams( DDQueueData* self, int priority, Reference sourceTeam, Reference destTeam ) { + if(g_network->isSimulated() && g_simulator.speedUpSimulation) { + return false; + } + + std::vector shards = self->shardsAffectedByTeamFailure->getShardsFor( sourceTeam->getServerIDs() ); + + if( !shards.size() ) + return false; + + state KeyRange moveShard = g_random->randomChoice( shards ); + StorageMetrics metrics = wait( brokenPromiseToNever( self->getShardMetrics.getReply(GetMetricsRequest(moveShard)) ) ); + + int64_t sourceBytes = sourceTeam->getLoadBytes(false); + int64_t destBytes = destTeam->getLoadBytes(); + if( sourceBytes - destBytes <= 3 * std::max( SERVER_KNOBS->MIN_SHARD_BYTES, metrics.bytes ) || metrics.bytes == 0 ) + return false; + + //verify the shard is still in sabtf + std::vector shards = self->shardsAffectedByTeamFailure->getShardsFor( sourceTeam->getServerIDs() ); + for( int i = 0; i < shards.size(); i++ ) { + if( moveShard == shards[i] ) { + TraceEvent(priority == PRIORITY_REBALANCE_OVERUTILIZED_TEAM ? "BgDDMountainChopper" : "BgDDValleyFiller", self->mi.id()) + .detail("sourceBytes", sourceBytes) + .detail("destBytes", destBytes) + .detail("shardBytes", metrics.bytes) + .detail("sourceTeam", sourceTeam->getDesc()) + .detail("destTeam", destTeam->getDesc()); + + self->input.send( RelocateShard( moveShard, priority ) ); + return true; + } + } + + return false; +} + +ACTOR Future BgDDMountainChopper( DDQueueData* self ) { + state double checkDelay = SERVER_KNOBS->BG_DD_POLLING_INTERVAL; + state int resetCount = SERVER_KNOBS->DD_REBALANCE_RESET_AMOUNT; + loop { + Void _ = wait( delay(checkDelay, TaskDataDistributionLaunch) ); + if (self->priority_relocations[PRIORITY_REBALANCE_OVERUTILIZED_TEAM] < SERVER_KNOBS->DD_REBALANCE_PARALLELISM) { + state Optional> randomTeam = wait( brokenPromiseToNever( self->teamCollection.getTeam.getReply( GetTeamRequest( true, false, true ) ) ) ); + if( randomTeam.present() ) { + if( randomTeam.get()->getMinFreeSpaceRatio() > SERVER_KNOBS->FREE_SPACE_RATIO_DD_CUTOFF ) { + state Optional> loadedTeam = wait( brokenPromiseToNever( self->teamCollection.getTeam.getReply( GetTeamRequest( true, true, false ) ) ) ); + if( loadedTeam.present() ) { + bool moved = wait( rebalanceTeams( self, PRIORITY_REBALANCE_OVERUTILIZED_TEAM, loadedTeam.get(), randomTeam.get() ) ); + if(moved) { + resetCount = 0; + } else { + resetCount++; + } + } + } + } + } + + if( now() - (*self->lastLimited) < SERVER_KNOBS->BG_DD_SATURATION_DELAY ) { + checkDelay = std::min(SERVER_KNOBS->BG_DD_MAX_WAIT, checkDelay * SERVER_KNOBS->BG_DD_INCREASE_RATE); + } else { + checkDelay = std::max(SERVER_KNOBS->BG_DD_MIN_WAIT, checkDelay / SERVER_KNOBS->BG_DD_DECREASE_RATE); + } + + if(resetCount >= SERVER_KNOBS->DD_REBALANCE_RESET_AMOUNT && checkDelay < SERVER_KNOBS->BG_DD_POLLING_INTERVAL) { + checkDelay = SERVER_KNOBS->BG_DD_POLLING_INTERVAL; + resetCount = SERVER_KNOBS->DD_REBALANCE_RESET_AMOUNT; + } + } +} + +ACTOR Future BgDDValleyFiller( DDQueueData* self ) { + state double checkDelay = SERVER_KNOBS->BG_DD_POLLING_INTERVAL; + state int resetCount = SERVER_KNOBS->DD_REBALANCE_RESET_AMOUNT; + loop { + Void _ = wait( delay(checkDelay, TaskDataDistributionLaunch) ); + if (self->priority_relocations[PRIORITY_REBALANCE_UNDERUTILIZED_TEAM] < SERVER_KNOBS->DD_REBALANCE_PARALLELISM) { + state Optional> randomTeam = wait( brokenPromiseToNever( self->teamCollection.getTeam.getReply( GetTeamRequest( true, false, false ) ) ) ); + if( randomTeam.present() ) { + state Optional> unloadedTeam = wait( brokenPromiseToNever( self->teamCollection.getTeam.getReply( GetTeamRequest( true, true, true ) ) ) ); + if( unloadedTeam.present() ) { + if( unloadedTeam.get()->getMinFreeSpaceRatio() > SERVER_KNOBS->FREE_SPACE_RATIO_DD_CUTOFF ) { + bool moved = wait( rebalanceTeams( self, PRIORITY_REBALANCE_UNDERUTILIZED_TEAM, randomTeam.get(), unloadedTeam.get() ) ); + if(moved) { + resetCount = 0; + } else { + resetCount++; + } + } + } + } + } + + if( now() - (*self->lastLimited) < SERVER_KNOBS->BG_DD_SATURATION_DELAY ) { + checkDelay = std::min(SERVER_KNOBS->BG_DD_MAX_WAIT, checkDelay * SERVER_KNOBS->BG_DD_INCREASE_RATE); + } else { + checkDelay = std::max(SERVER_KNOBS->BG_DD_MIN_WAIT, checkDelay / SERVER_KNOBS->BG_DD_DECREASE_RATE); + } + + if(resetCount >= SERVER_KNOBS->DD_REBALANCE_RESET_AMOUNT && checkDelay < SERVER_KNOBS->BG_DD_POLLING_INTERVAL) { + checkDelay = SERVER_KNOBS->BG_DD_POLLING_INTERVAL; + resetCount = SERVER_KNOBS->DD_REBALANCE_RESET_AMOUNT; + } + } +} + +ACTOR Future dataDistributionQueue( + Database cx, + PromiseStream input, + PromiseStream getShardMetrics, + TeamCollectionInterface teamCollection, + Reference shardsAffectedByTeamFailure, + MoveKeysLock lock, + PromiseStream> getAverageShardBytes, + MasterInterface mi, + int teamSize, + int durableStorageQuorum, + double* lastLimited) +{ + state DDQueueData self( mi, lock, cx, teamCollection, shardsAffectedByTeamFailure, getAverageShardBytes, teamSize, durableStorageQuorum, input, getShardMetrics, lastLimited ); + state std::set serversToLaunchFrom; + state KeyRange keysToLaunchFrom; + state RelocateData launchData; + state Future recordMetrics = delay(SERVER_KNOBS->DD_QUEUE_LOGGING_INTERVAL); + state Future bgDDMountainChopper = BgDDMountainChopper( &self ); + state Future bgDDValleyFiller = BgDDValleyFiller( &self ); + + state ActorCollectionNoErrors actors; + state PromiseStream rangesComplete; + state Future launchQueuedWorkTimeout = Never(); + + try { + loop { + self.validate(); + + // For the given servers that caused us to go around the loop, find the next item(s) that can be launched. + if( launchData.startTime != -1 ) { + self.launchQueuedWork( launchData ); + launchData = RelocateData(); + } + else if( !keysToLaunchFrom.empty() ) { + self.launchQueuedWork( keysToLaunchFrom ); + keysToLaunchFrom = KeyRangeRef(); + } + + ASSERT( launchData.startTime == -1 && keysToLaunchFrom.empty() ); + + choose { + when ( RelocateShard rs = waitNext( self.input.getFuture() ) ) { + bool wasEmpty = serversToLaunchFrom.empty(); + self.queueRelocation( rs, serversToLaunchFrom ); + if(wasEmpty && !serversToLaunchFrom.empty()) + launchQueuedWorkTimeout = delay(0, TaskDataDistributionLaunch); + } + when ( Void _ = wait(launchQueuedWorkTimeout) ) { + self.launchQueuedWork( serversToLaunchFrom ); + serversToLaunchFrom = std::set(); + launchQueuedWorkTimeout = Never(); + } + when ( RelocateData results = waitNext( self.fetchSourceServersComplete.getFuture() ) ) { + self.completeSourceFetch( results ); + launchData = results; + } + when ( RelocateData done = waitNext( self.dataTransferComplete.getFuture() ) ) { + complete( done, self.busymap ); + if(serversToLaunchFrom.empty() && !done.src.empty()) + launchQueuedWorkTimeout = delay(0, TaskDataDistributionLaunch); + serversToLaunchFrom.insert(done.src.begin(), done.src.end()); + } + when ( RelocateData done = waitNext( self.relocationComplete.getFuture() ) ) { + self.activeRelocations--; + self.priority_relocations[ done.priority ]--; + self.fetchKeysComplete.erase( done ); + //self.logRelocation( done, "ShardRelocatorDone" ); + actors.add( tag( delay(0, TaskDataDistributionLaunch), done.keys, rangesComplete ) ); + if( g_network->isSimulated() && debug_isCheckRelocationDuration() && now() - done.startTime > 60 ) { + TraceEvent(SevWarnAlways, "RelocationDurationTooLong").detail("Duration", now() - done.startTime); + debug_setCheckRelocationDuration(false); + } + } + when ( KeyRange done = waitNext( rangesComplete.getFuture() ) ) { + keysToLaunchFrom = done; + } + when ( Void _ = wait( recordMetrics ) ) { + Promise req; + getAverageShardBytes.send( req ); + + recordMetrics = delay(SERVER_KNOBS->DD_QUEUE_LOGGING_INTERVAL); + + int lowPriorityRelocations = 0, highPriorityRelocations = 0, highestPriorityRelocation = 0; + for( auto it = self.priority_relocations.begin(); it != self.priority_relocations.end(); ++it ) { + if (it->second) + highestPriorityRelocation = std::max(highestPriorityRelocation, it->first); + if( it->first < 200 ) + lowPriorityRelocations += it->second; + else + highPriorityRelocations += it->second; + } + + TraceEvent("MovingData", mi.id()) + .detail( "InFlight", self.activeRelocations ) + .detail( "InQueue", self.queuedRelocations ) + .detail( "AverageShardSize", req.getFuture().isReady() ? req.getFuture().get() : -1 ) + .detail( "LowPriorityRelocations", lowPriorityRelocations ) + .detail( "HighPriorityRelocations", highPriorityRelocations ) + .detail( "HighestPriority", highestPriorityRelocation ) + .detail( "BytesWritten", self.bytesWritten ) + .trackLatest( format("%s/MovingData", printable(cx->dbName).c_str() ).c_str() ); + } + when ( Void _ = wait( self.error.getFuture() ) ) {} // Propagate errors from dataDistributionRelocator + when ( Void _ = wait( bgDDMountainChopper ) ) {} + when ( Void _ = wait( bgDDValleyFiller ) ) {} + } + } + } catch (Error& e) { + if (e.code() != error_code_broken_promise && // FIXME: Get rid of these broken_promise errors every time we are killed by the master dying + e.code() != error_code_movekeys_conflict) + TraceEvent(SevError, "dataDistributionQueueError", mi.id()).error(e); + throw e; + } +} diff --git a/fdbserver/DataDistributionTracker.actor.cpp b/fdbserver/DataDistributionTracker.actor.cpp new file mode 100644 index 0000000000..ddad94203d --- /dev/null +++ b/fdbserver/DataDistributionTracker.actor.cpp @@ -0,0 +1,862 @@ +/* + * DataDistributionTracker.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "fdbrpc/FailureMonitor.h" +#include "fdbclient/SystemData.h" +#include "DataDistribution.h" +#include "Knobs.h" +#include "fdbclient/DatabaseContext.h" +#include "flow/ActorCollection.h" + +enum BandwidthStatus { + BandwidthStatusLow, + BandwidthStatusNormal, + BandwidthStatusHigh +}; + +BandwidthStatus getBandwidthStatus( StorageMetrics const& metrics ) { + if( metrics.bytesPerKSecond > SERVER_KNOBS->SHARD_MAX_BYTES_PER_KSEC ) + return BandwidthStatusHigh; + else if( metrics.bytesPerKSecond < SERVER_KNOBS->SHARD_MIN_BYTES_PER_KSEC ) + return BandwidthStatusLow; + + return BandwidthStatusNormal; +} + +ACTOR Future updateMaxShardSize( Standalone dbName, Reference> dbSizeEstimate, Reference>> maxShardSize ) { + state int64_t lastDbSize = 0; + state int64_t granularity = g_network->isSimulated() ? + SERVER_KNOBS->DD_SHARD_SIZE_GRANULARITY_SIM : SERVER_KNOBS->DD_SHARD_SIZE_GRANULARITY; + loop { + auto sizeDelta = std::abs(dbSizeEstimate->get() - lastDbSize); + if( sizeDelta > granularity || !maxShardSize->get().present() ) { + auto v = getMaxShardSize( dbSizeEstimate->get() ); + maxShardSize->set( v ); + lastDbSize = dbSizeEstimate->get(); + } + Void _ = wait( dbSizeEstimate->onChange() ); + } +} + +struct ShardTrackedData { + Future trackShard; + Future trackBytes; + Reference>> stats; +}; + +struct DataDistributionTracker { + Database cx; + UID masterId; + KeyRangeMap< ShardTrackedData > shards; + ActorCollection sizeChanges; + + Reference> dbSizeEstimate; + Reference>> maxShardSize; + Future maxShardSizeUpdater; + + // CapacityTracker + PromiseStream output; + + Promise readyToStart; + + DataDistributionTracker(Database cx, UID masterId, Promise const& readyToStart, PromiseStream const& output) + : cx(cx), masterId( masterId ), dbSizeEstimate( new AsyncVar() ), + maxShardSize( new AsyncVar>() ), + sizeChanges(false), readyToStart(readyToStart), output( output ) {} + + ~DataDistributionTracker() + { + //Cancel all actors so they aren't waiting on sizeChanged broken promise + sizeChanges.clear(false); + shards.insert( allKeys, ShardTrackedData() ); + } +}; + +void restartShardTrackers( + DataDistributionTracker* self, + KeyRangeRef keys, + Optional startingSize = Optional()); + +// Gets the permitted size and IO bounds for a shard. A shard that starts at allKeys.begin +// (i.e. '') will have a permitted size of 0, since the database can contain no data. +ShardSizeBounds getShardSizeBounds(KeyRangeRef shard, int64_t maxShardSize) { + ShardSizeBounds bounds; + + bounds.max.bytes = maxShardSize; + bounds.max.bytesPerKSecond = bounds.max.infinity; + bounds.max.iosPerKSecond = bounds.max.infinity; + + //The first shard can have arbitrarily small size + if(shard.begin != allKeys.begin) + bounds.min.bytes = bounds.max.bytes / SERVER_KNOBS->SHARD_BYTES_RATIO; + else + bounds.min.bytes = 0; + + bounds.min.bytesPerKSecond = 0; + bounds.min.iosPerKSecond = 0; + + //The permitted error is 1/3 of the general-case minimum bytes (even in the special case where this is the last shard) + bounds.permittedError.bytes = bounds.max.bytes / SERVER_KNOBS->SHARD_BYTES_RATIO / 3; + bounds.permittedError.bytesPerKSecond = bounds.permittedError.infinity; + bounds.permittedError.iosPerKSecond = bounds.permittedError.infinity; + + return bounds; +} + +int64_t getMaxShardSize( double dbSizeEstimate ) { + return std::min((SERVER_KNOBS->MIN_SHARD_BYTES + (int64_t)std::sqrt( dbSizeEstimate )*SERVER_KNOBS->SHARD_BYTES_PER_SQRT_BYTES) * SERVER_KNOBS->SHARD_BYTES_RATIO, + (int64_t)SERVER_KNOBS->MAX_SHARD_BYTES); +} + +ACTOR Future trackShardBytes( + DataDistributionTracker* self, + KeyRange keys, + Reference>> shardSize, + UID trackerID, + bool addToSizeEstimate = true) +{ + state Transaction tr(self->cx); + + Void _ = wait( delay( 0, TaskDataDistribution ) ); + + /*TraceEvent("TrackShardBytesStarting") + .detail("TrackerID", trackerID) + .detail("Keys", printable(keys)) + .detail("TrackedBytesInitiallyPresent", shardSize->get().present()) + .detail("StartingSize", shardSize->get().present() ? shardSize->get().get().metrics.bytes : 0) + .detail("StartingMerges", shardSize->get().present() ? shardSize->get().get().merges : 0);*/ + + try { + loop { + try { + state ShardSizeBounds bounds; + if( shardSize->get().present() ) { + auto bytes = shardSize->get().get().bytes; + auto bandwidthStatus = getBandwidthStatus( shardSize->get().get() ); + bounds.max.bytes = std::max( int64_t(bytes * 1.1), (int64_t)SERVER_KNOBS->MIN_SHARD_BYTES ); + bounds.min.bytes = std::min( int64_t(bytes * 0.9), std::max(int64_t(bytes - (SERVER_KNOBS->MIN_SHARD_BYTES * 0.1)), (int64_t)0) ); + bounds.permittedError.bytes = bytes * 0.1; + if( bandwidthStatus == BandwidthStatusNormal ) { // Not high or low + bounds.max.bytesPerKSecond = SERVER_KNOBS->SHARD_MAX_BYTES_PER_KSEC; + bounds.min.bytesPerKSecond = SERVER_KNOBS->SHARD_MIN_BYTES_PER_KSEC; + bounds.permittedError.bytesPerKSecond = bounds.min.bytesPerKSecond / 4; + } else if( bandwidthStatus == BandwidthStatusHigh ) { // > 10MB/sec for 100MB shard, proportionally lower for smaller shard, > 200KB/sec no matter what + bounds.max.bytesPerKSecond = bounds.max.infinity; + bounds.min.bytesPerKSecond = SERVER_KNOBS->SHARD_MAX_BYTES_PER_KSEC; + bounds.permittedError.bytesPerKSecond = bounds.min.bytesPerKSecond / 4; + } else if( bandwidthStatus == BandwidthStatusLow ) { // < 10KB/sec + bounds.max.bytesPerKSecond = SERVER_KNOBS->SHARD_MIN_BYTES_PER_KSEC; + bounds.min.bytesPerKSecond = 0; + bounds.permittedError.bytesPerKSecond = bounds.max.bytesPerKSecond / 4; + } else + ASSERT( false ); + + } else { + bounds.max.bytes = -1; + bounds.min.bytes = -1; + bounds.permittedError.bytes = -1; + bounds.max.bytesPerKSecond = bounds.max.infinity; + bounds.min.bytesPerKSecond = 0; + bounds.permittedError.bytesPerKSecond = bounds.permittedError.infinity; + } + + bounds.max.iosPerKSecond = bounds.max.infinity; + bounds.min.iosPerKSecond = 0; + bounds.permittedError.iosPerKSecond = bounds.permittedError.infinity; + + StorageMetrics metrics = wait( tr.waitStorageMetrics( keys, bounds.min, bounds.max, bounds.permittedError, CLIENT_KNOBS->STORAGE_METRICS_SHARD_LIMIT ) ); + + /*TraceEvent("ShardSizeUpdate") + .detail("Keys", printable(keys)) + .detail("UpdatedSize", metrics.metrics.bytes) + .detail("Bandwidth", metrics.metrics.bytesPerKSecond) + .detail("BandwithStatus", getBandwidthStatus(metrics)) + .detail("BytesLower", bounds.min.bytes) + .detail("BytesUpper", bounds.max.bytes) + .detail("BandwidthLower", bounds.min.bytesPerKSecond) + .detail("BandwidthUpper", bounds.max.bytesPerKSecond) + .detail("ShardSizePresent", shardSize->get().present()) + .detail("OldShardSize", shardSize->get().present() ? shardSize->get().get().metrics.bytes : 0 ) + .detail("TrackerID", trackerID);*/ + + if( shardSize->get().present() && addToSizeEstimate ) + self->dbSizeEstimate->set( self->dbSizeEstimate->get() + metrics.bytes - shardSize->get().get().bytes ); + + shardSize->set( metrics ); + } catch( Error &e ) { + //TraceEvent("ShardSizeUpdateError").detail("Begin", printable(keys.begin)).detail("End", printable(keys.end)).detail("TrackerID", trackerID).error(e, true); + Void _ = wait( tr.onError(e) ); + } + } + } catch( Error &e ) { + if (e.code() != error_code_actor_cancelled) + self->output.sendError(e); // Propagate failure to dataDistributionTracker + throw e; + } +} + +/* +ACTOR Future extrapolateShardBytes( Reference>> inBytes, Reference>> outBytes ) { + state std::deque< std::pair > past; + loop { + Void _ = wait( inBytes->onChange() ); + if( inBytes->get().present() ) { + past.push_back( std::make_pair(now(),inBytes->get().get()) ); + if (past.size() < 2) + outBytes->set( inBytes->get() ); + else { + while (past.size() > 1 && past.end()[-1].first - past.begin()[1].first > 1.0) + past.pop_front(); + double rate = std::max(0.0, double(past.end()[-1].second-past.begin()[0].second)/(past.end()[-1].first - past.begin()[0].first)); + outBytes->set( inBytes->get().get() + rate * 10.0 ); + } + } + } +}*/ + +ACTOR Future>> getSplitKeys( DataDistributionTracker* self, KeyRange splitRange, StorageMetrics splitMetrics, StorageMetrics estimated ) { + loop { + state Transaction tr(self->cx); + try { + Standalone> keys = wait( tr.splitStorageMetrics( splitRange, splitMetrics, estimated ) ); + return keys; + } catch( Error &e ) { + Void _ = wait( tr.onError(e) ); + } + } +} + +ACTOR Future getFirstSize( Reference>> stats ) { + loop { + if(stats->get().present()) + return stats->get().get().bytes; + Void _ = wait( stats->onChange() ); + } +} + +ACTOR Future changeSizes( DataDistributionTracker* self, KeyRangeRef keys, int64_t oldShardsEndingSize ) { + state vector> sizes; + for (auto it : self->shards.intersectingRanges(keys) ) { + sizes.push_back( getFirstSize( it->value().stats ) ); + } + + Void _ = wait( waitForAll( sizes ) ); + Void _ = wait( yield(TaskDataDistribution) ); + + int64_t newShardsStartingSize = 0; + for ( int i = 0; i < sizes.size(); i++ ) + newShardsStartingSize += sizes[i].get(); + + int64_t totalSizeEstimate = self->dbSizeEstimate->get(); + /*TraceEvent("TrackerChangeSizes") + .detail("TotalSizeEstimate", totalSizeEstimate) + .detail("EndSizeOfOldShards", oldShardsEndingSize) + .detail("StartingSizeOfNewShards", newShardsStartingSize);*/ + self->dbSizeEstimate->set( totalSizeEstimate + newShardsStartingSize - oldShardsEndingSize ); + return Void(); +} + +struct HasBeenTrueFor : NonCopyable { + explicit HasBeenTrueFor( double seconds, bool value ) : enough( seconds ), trigger( value ? Void() : Future() ) {} + + Future set() { + if( !trigger.isValid() ) { + cleared = Promise(); + trigger = delay( enough, TaskDataDistribution - 1 ) || cleared.getFuture(); + } + return trigger; + } + void clear() { + if( !trigger.isValid() ) { + return; + } + trigger = Future(); + cleared.send( Void() ); + } + + // True if this->value is true and has been true for this->seconds + bool hasBeenTrueForLongEnough() const { + return trigger.isValid() && trigger.isReady(); + } + +private: + Future trigger; + Promise cleared; + const double enough; +}; + +ACTOR Future shardSplitter( + DataDistributionTracker* self, + UID trackerId, + KeyRange keys, + Reference>> shardSize, + ShardSizeBounds shardBounds ) +{ + state StorageMetrics metrics = shardSize->get().get(); + state BandwidthStatus bandwidthStatus = getBandwidthStatus( shardSize->get().get() ); + + //Split + TEST(true); // shard to be split + + StorageMetrics splitMetrics; + splitMetrics.bytes = shardBounds.max.bytes / 2; + splitMetrics.bytesPerKSecond = SERVER_KNOBS->SHARD_SPLIT_BYTES_PER_KSEC; + splitMetrics.iosPerKSecond = splitMetrics.infinity; + + state Standalone> splitKeys = wait( getSplitKeys(self, keys, splitMetrics, metrics ) ); + //fprintf(stderr, "split keys:\n"); + //for( int i = 0; i < splitKeys.size(); i++ ) { + // fprintf(stderr, " %s\n", printable(splitKeys[i]).c_str()); + //} + int numShards = splitKeys.size() - 1; + + if( g_random->random01() < 0.01 ) { + TraceEvent("RelocateShardStartSplitx100", self->masterId) + .detail("Begin", printable(keys.begin)) + .detail("End", printable(keys.end)) + .detail("TrackerID", trackerId) + .detail("MaxBytes", shardBounds.max.bytes) + .detail("MetricsBytes", metrics.bytes) + .detail("Bandwidth", bandwidthStatus == BandwidthStatusHigh ? "High" : bandwidthStatus == BandwidthStatusNormal ? "Normal" : "Low") + .detail("BytesPerKSec", metrics.bytesPerKSecond) + .detail("numShards", numShards); + } + + if( numShards > 1 ) { + int skipRange = g_random->randomInt(0, numShards); + // The queue can't deal with RelocateShard requests which split an existing shard into three pieces, so + // we have to send the unskipped ranges in this order (nibbling in from the edges of the old range) + for( int i = 0; i < skipRange; i++ ) + restartShardTrackers( self, KeyRangeRef(splitKeys[i], splitKeys[i+1]) ); + restartShardTrackers( self, KeyRangeRef( splitKeys[skipRange], splitKeys[skipRange+1] ) ); + for( int i = numShards-1; i > skipRange; i-- ) + restartShardTrackers( self, KeyRangeRef(splitKeys[i], splitKeys[i+1]) ); + + for( int i = 0; i < skipRange; i++ ) + self->output.send( RelocateShard( KeyRangeRef(splitKeys[i], splitKeys[i+1]), PRIORITY_SPLIT_SHARD) ); + for( int i = numShards-1; i > skipRange; i-- ) + self->output.send( RelocateShard( KeyRangeRef(splitKeys[i], splitKeys[i+1]), PRIORITY_SPLIT_SHARD) ); + + self->sizeChanges.add( changeSizes( self, keys, shardSize->get().get().bytes ) ); + } else { + Void _ = wait( delay(1.0, TaskDataDistribution) ); //In case the reason the split point was off was due to a discrepancy between storage servers + } + return Void(); +} + +Future shardMerger( + DataDistributionTracker* self, + UID trackerId, + KeyRange const& keys, + Reference>> shardSize ) +{ + int64_t maxShardSize = self->maxShardSize->get().get(); + + auto prevIter = self->shards.rangeContaining(keys.begin); + auto nextIter = self->shards.rangeContaining(keys.begin); + + TEST(true); // shard to be merged + ASSERT( keys.begin > allKeys.begin ); + + // We must not merge the keyServers shard + if (keys.begin == keyServersPrefix) { + TraceEvent(SevError, "LastShardMerge", self->masterId) + .detail("ShardKeyBegin", printable(keys.begin)) + .detail("ShardKeyEnd", printable(keys.end)) + .detail("TrackerID", trackerId); + ASSERT(false); + } + + // This will merge shards both before and after "this" shard in keyspace. + int shardsMerged = 1; + bool forwardComplete = false; + KeyRangeRef merged; + StorageMetrics endingStats = shardSize->get().get(); + + loop { + Optional newMetrics; + if( !forwardComplete ) { + if( nextIter->range().end == keyServersPrefix ) { + forwardComplete = true; + continue; + } + ++nextIter; + newMetrics = nextIter->value().stats->get(); + + // If going forward, give up when the next shard's stats are not yet present. + if( !newMetrics.present() ) { + --nextIter; + forwardComplete = true; + continue; + } + } else { + --prevIter; + newMetrics = prevIter->value().stats->get(); + + // If going backward, stop when the stats are not present or if the shard is already over the merge + // bounds. If this check triggers right away (if we have not merged anything) then return a trigger + // on the previous shard changing "size". + if( !newMetrics.present() ) { + if( shardsMerged == 1 ) { + TEST( true ); // shardMerger cannot merge anything + return prevIter->value().stats->onChange(); + } + + ++prevIter; + break; + } + } + + merged = KeyRangeRef( prevIter->range().begin, nextIter->range().end ); + endingStats += newMetrics.get(); + shardsMerged++; + + auto shardBounds = getShardSizeBounds( merged, maxShardSize ); + if( endingStats.bytes >= shardBounds.min.bytes || + getBandwidthStatus( endingStats ) != BandwidthStatusLow || + shardsMerged >= SERVER_KNOBS->DD_MERGE_LIMIT ) { + // The merged range is larger than the min bounds se we cannot continue merging in this direction. + // This means that: + // 1. If we were going forwards (the starting direction), we roll back the last speculative merge. + // In this direction we do not want to go above this boundary since we will merge at least one in + // the other direction, even when that goes over the bounds. + // 2. If we were going backwards we always want to merge one more shard on (to make sure we go over + // the shard min bounds) so we "break" without resetting the merged range. + if( forwardComplete ) + break; + + // If going forward, remove most recently added range + endingStats -= newMetrics.get(); + shardsMerged--; + --nextIter; + merged = KeyRangeRef( prevIter->range().begin, nextIter->range().end ); + forwardComplete = true; + } + } + + //restarting shard tracker will derefenced values in the shard map, so make a copy + KeyRange mergeRange = merged; + + TraceEvent("RelocateShardMergeMetrics", self->masterId) + .detail("OldKeys", printable(keys)) + .detail("NewKeys", printable(mergeRange)) + .detail("EndingSize", endingStats.bytes) + .detail("BatchedMerges", shardsMerged) + .detail("TrackerID", trackerId); + + restartShardTrackers( self, mergeRange, endingStats ); + self->output.send( RelocateShard( mergeRange, PRIORITY_MERGE_SHARD ) ); + + // We are about to be cancelled by the call to restartShardTrackers + return Void(); +} + +ACTOR Future shardEvaluator( + DataDistributionTracker* self, + KeyRange keys, + Reference>> shardSize, + HasBeenTrueFor *wantsToMerge, + UID trackerID) +{ + Future onChange = shardSize->onChange() || yieldedFuture(self->maxShardSize->onChange()); + + // There are the bounds inside of which we are happy with the shard size. + // getShardSizeBounds() will allways have shardBounds.min.bytes == 0 for shards that start at allKeys.begin, + // so will will never attempt to merge that shard with the one previous. + ShardSizeBounds shardBounds = getShardSizeBounds(keys, self->maxShardSize->get().get()); + StorageMetrics const& stats = shardSize->get().get(); + + bool shouldSplit = stats.bytes > shardBounds.max.bytes || + getBandwidthStatus( stats ) == BandwidthStatusHigh; + bool shouldMerge = stats.bytes < shardBounds.min.bytes && + getBandwidthStatus( stats ) == BandwidthStatusLow; + + // Every invocation must set this or clear it + if (shouldMerge) { + auto whenLongEnough = wantsToMerge->set(); + if( !wantsToMerge->hasBeenTrueForLongEnough() ) { + onChange = onChange || whenLongEnough; + } + } + else + wantsToMerge->clear(); + + /*TraceEvent("ShardEvaluator", self->masterId) + .detail("TrackerId", trackerID) + .detail("ShouldSplit", shouldSplit) + .detail("ShouldMerge", shouldMerge) + .detail("HasBeenTrueLongEnough", wantsToMerge->hasBeenTrueForLongEnough());*/ + + if(wantsToMerge->hasBeenTrueForLongEnough()) { + onChange = onChange || shardMerger( self, trackerID, keys, shardSize ); + } + if( shouldSplit ) { + onChange = onChange || shardSplitter( self, trackerID, keys, shardSize, shardBounds ); + } + + Void _ = wait( onChange ); + return Void(); +} + +ACTOR Future shardTracker( + DataDistributionTracker* self, + KeyRange keys, + Reference>> shardSize, + UID trackerID ) +{ + // Survives multiple calls to shardEvaluator and keeps merges from happening too quickly. + state HasBeenTrueFor wantsToMerge( SERVER_KNOBS->DD_MERGE_COALESCE_DELAY, shardSize->get().present() ); + + Void _ = wait( yieldedFuture(self->readyToStart.getFuture()) ); + + if( !shardSize->get().present() ) + Void _ = wait( shardSize->onChange() ); + + if( !self->maxShardSize->get().present() ) + Void _ = wait( yieldedFuture(self->maxShardSize->onChange()) ); + + // Since maxShardSize will become present for all shards at once, avoid slow tasks with a short delay + Void _ = wait( delay( 0, TaskDataDistribution ) ); + + /*TraceEvent("ShardTracker", self->masterId) + .detail("Begin", printable(keys.begin)) + .detail("End", printable(keys.end)) + .detail("TrackerID", trackerID) + .detail("MaxBytes", self->maxShardSize->get().get()) + .detail("ShardSize", shardSize->get().get().bytes) + .detail("BytesPerKSec", shardSize->get().get().bytesPerKSecond);*/ + + try { + loop { + // Use the current known size to check for (and start) splits and merges. + Void _ = wait( shardEvaluator( self, keys, shardSize, &wantsToMerge, trackerID ) ); + + // We could have a lot of actors being released from the previous wait at the same time. Immediately calling + // delay(0) mitigates the resulting SlowTask + Void _ = wait( delay(0, TaskDataDistribution) ); + } + } catch (Error& e) { + TraceEvent(SevWarn, "ShardTrackerError", self->masterId) + .detail("TrackerID", trackerID) + .detail("Keys", printable(keys)) + .error(e); + if (e.code() != error_code_actor_cancelled) + self->output.sendError(e); // Propagate failure to dataDistributionTracker + throw e; + } +} + +void restartShardTrackers( DataDistributionTracker* self, KeyRangeRef keys, Optional startingSize ) { + auto ranges = self->shards.getAffectedRangesAfterInsertion( keys, ShardTrackedData() ); + for(int i=0; ireadyToStart.isSet() ); + continue; + } + + Reference>> shardSize( new AsyncVar>() ); + + // For the case where the new tracker will take over at the boundaries of current shard(s) + // we can use the old size if it is available. This will be the case when merging shards. + if( startingSize.present() ) { + ASSERT( ranges.size() == 1 ); + /*TraceEvent("ShardTrackerSizePreset", self->masterId) + .detail("Keys", printable(keys)) + .detail("Size", startingSize.get().metrics.bytes) + .detail("Merges", startingSize.get().merges);*/ + TEST( true ); // shardTracker started with trackedBytes already set + shardSize->set( startingSize ); + } + + UID trackerID = g_random->randomUniqueID(); + ShardTrackedData data; + data.stats = shardSize; + data.trackShard = shardTracker( self, ranges[i], shardSize, trackerID ); + data.trackBytes = trackShardBytes( self, keys, shardSize, trackerID ); + self->shards.insert( ranges[i], data ); + } +} + +ACTOR Future trackInitialShards(DataDistributionTracker *self, + Reference initData, + Reference shardsAffectedByTeamFailure) +{ + TraceEvent("TrackInitialShards", self->masterId).detail("InitialShardCount", initData->shards.size()); + + //This line reduces the priority of shard initialization to prevent interference with failure monitoring. + //SOMEDAY: Figure out what this priority should actually be + Void _ = wait( delay( 0.0, TaskDataDistribution ) ); + + state int lastBegin = -1; + state vector last; + + //The ending shard does not have a shardTracker, so instead just track the size of the shard + Reference>> endShardSize( new AsyncVar>() ); + KeyRangeRef endShardRange( keyServersPrefix, allKeys.end ); + ShardTrackedData endShardData; + endShardData.stats = endShardSize; + endShardData.trackBytes = trackShardBytes( self, endShardRange, endShardSize, g_random->randomUniqueID(), false ); + self->shards.insert( endShardRange, endShardData ); + + state int s; + for(s=0; sshards.size(); s++) { + state InitialDataDistribution::Team src = initData->shards[s].value.first; + auto& dest = initData->shards[s].value.second; + if (dest.size()) { + // This shard is already in flight. Ideally we should use dest in sABTF and generate a dataDistributionRelocator directly in + // DataDistributionQueue to track it, but it's easier to just (with low priority) schedule it for movement. + self->output.send( RelocateShard( initData->shards[s], PRIORITY_RECOVER_MOVE ) ); + } + + // The following clause was here for no remembered reason. It was removed, however, because on resumption of stopped + // clusters (of size 3) it was grouping all the the shards in the system into one, and then splitting them all back out, + // causing unecessary data distribution. + //if (s==0 || s+1==initData.shards.size() || lastBegin<0 || src != last || initData.shards[s].begin == keyServersPrefix) { + // end current run, start a new shardTracker + // relies on the dummy shard at allkeysend + + if (lastBegin >= 0) { + state KeyRangeRef keys( initData->shards[lastBegin].begin, initData->shards[s].begin ); + if (keys.begin < keyServersPrefix) // disallow spliting of keyServers shard + restartShardTrackers( self, keys ); + shardsAffectedByTeamFailure->defineShard( keys ); + shardsAffectedByTeamFailure->moveShard( keys, last ); + } + lastBegin = s; + last = src; + //} + Void _ = wait( yield( TaskDataDistribution ) ); + } + + Future initialSize = changeSizes( self, KeyRangeRef(allKeys.begin, keyServersPrefix), 0 ); + self->readyToStart.send(Void()); + Void _ = wait( initialSize ); + self->maxShardSizeUpdater = updateMaxShardSize( self->cx->dbName, self->dbSizeEstimate, self->maxShardSize ); + + return Void(); +} + +ACTOR Future fetchShardMetrics_impl( DataDistributionTracker* self, GetMetricsRequest req ) { + try { + loop { + Future onChange; + StorageMetrics returnMetrics; + for( auto t : self->shards.intersectingRanges( req.keys ) ) { + auto &stats = t.value().stats; + if( !stats->get().present() ) { + onChange = stats->onChange(); + break; + } + returnMetrics += t.value().stats->get().get(); + } + + if( !onChange.isValid() ) { + req.reply.send( returnMetrics ); + return Void(); + } + + Void _ = wait( onChange ); + } + } catch( Error &e ) { + if( e.code() != error_code_actor_cancelled && !req.reply.isSet() ) + req.reply.sendError(e); + throw; + } +} + +ACTOR Future fetchShardMetrics( DataDistributionTracker* self, GetMetricsRequest req ) { + choose { + when( Void _ = wait( fetchShardMetrics_impl( self, req ) ) ) {} + when( Void _ = wait( delay( SERVER_KNOBS->DD_SHARD_METRICS_TIMEOUT ) ) ) { + TEST(true); // DD_SHARD_METRICS_TIMEOUT + StorageMetrics largeMetrics; + largeMetrics.bytes = SERVER_KNOBS->MAX_SHARD_BYTES; + req.reply.send( largeMetrics ); + } + } + return Void(); +} + +ACTOR Future dataDistributionTracker( + Reference initData, + Database cx, + Reference shardsAffectedByTeamFailure, + PromiseStream output, + PromiseStream getShardMetrics, + FutureStream> getAverageShardBytes, + Promise readyToStart, + UID masterId) +{ + state DataDistributionTracker self(cx, masterId, readyToStart, output); + state Future loggingTrigger = Void(); + try { + Void _ = wait( trackInitialShards( &self, initData, shardsAffectedByTeamFailure ) ); + initData = Reference(); + + loop choose { + when( Promise req = waitNext( getAverageShardBytes ) ) { + req.send( self.maxShardSize->get().get() / 2 ); + } + when( Void _ = wait( loggingTrigger ) ) { + TraceEvent("DDTrackerStats", self.masterId) + .detail("Shards", self.shards.size()) + .detail("TotalSizeBytes", self.dbSizeEstimate->get()) + .trackLatest( format("%s/DDTrackerStats", printable(cx->dbName).c_str() ).c_str() ); + + loggingTrigger = delay(SERVER_KNOBS->DATA_DISTRIBUTION_LOGGING_INTERVAL); + } + when( GetMetricsRequest req = waitNext( getShardMetrics.getFuture() ) ) { + self.sizeChanges.add( fetchShardMetrics( &self, req ) ); + } + when( Void _ = wait( self.sizeChanges.getResult() ) ) {} + } + } catch (Error& e) { + TraceEvent(SevError, "dataDistributionTrackerError", self.masterId).error(e); + throw e; + } +} + +vector ShardsAffectedByTeamFailure::getShardsFor( Team team ) { + vector r; + for(auto it = team_shards.lower_bound( std::pair( team, KeyRangeRef() ) ); + it != team_shards.end() && it->first == team; + ++it) + r.push_back( it->second ); + return r; +} + +int ShardsAffectedByTeamFailure::getNumberOfShards( UID ssID ) { + return storageServerShards[ssID]; +} + +vector> ShardsAffectedByTeamFailure::getTeamsFor( KeyRangeRef keys ) { + return shard_teams[keys.begin]; +} + +void ShardsAffectedByTeamFailure::erase(Team team, KeyRange const& range) { + if(team_shards.erase( std::pair(team, range) ) > 0) { + for(auto uid = team.begin(); uid != team.end(); ++uid) + storageServerShards[*uid]--; + } +} + +void ShardsAffectedByTeamFailure::insert(Team team, KeyRange const& range) { + if(team_shards.insert( std::pair( team, range ) ).second) { + for(auto uid = team.begin(); uid != team.end(); ++uid) + storageServerShards[*uid]++; + } +} + +void ShardsAffectedByTeamFailure::defineShard( KeyRangeRef keys ) { + std::set teams; + auto rs = shard_teams.intersectingRanges(keys); + for(auto it = rs.begin(); it != rs.end(); ++it) { + for(auto t=it->value().begin(); t!=it->value().end(); ++t) { + teams.insert( *t ); + erase(*t, it->range()); + } + } + + /*TraceEvent("ShardsAffectedByTeamFailureDefine") + .detail("KeyBegin", printable(keys.begin)) + .detail("KeyEnd", printable(keys.end)) + .detail("TeamCount", teams.size()); */ + + auto affectedRanges = shard_teams.getAffectedRangesAfterInsertion(keys); + shard_teams.insert( keys, vector(teams.begin(), teams.end()) ); + + for(auto r=affectedRanges.begin(); r != affectedRanges.end(); ++r) { + auto& teams = shard_teams[r->begin]; + for(auto t=teams.begin(); t!=teams.end(); ++t) { + insert(*t, *r); + } + } + check(); +} + +void ShardsAffectedByTeamFailure::moveShard( KeyRangeRef keys, Team destinationTeam ) { + /*TraceEvent("ShardsAffectedByTeamFailureMove") + .detail("KeyBegin", printable(keys.begin)) + .detail("KeyEnd", printable(keys.end)) + .detail("NewTeamSize", destinationTeam.size()) + .detail("NewTeam", describe(destinationTeam));*/ + + auto ranges = shard_teams.intersectingRanges( keys ); + std::vector< std::pair > modifiedShards; + for(auto it = ranges.begin(); it != ranges.end(); ++it) { + if( keys.contains( it->range() ) ) { + // erase the many teams that were assiciated with this one shard + for(auto t = it->value().begin(); t != it->value().end(); ++t) { + erase(*t, it->range()); + } + + // save this modification for later insertion + modifiedShards.push_back( std::pair( destinationTeam, it->range() ) ); + } else { + // for each range that touches this move, add our team as affecting this range + insert(destinationTeam, it->range()); + + // if we are not in the list of teams associated with this shard, add us in + auto& teams = it->value(); + if( std::find( teams.begin(), teams.end(), destinationTeam ) == teams.end() ) + teams.push_back( destinationTeam ); + } + } + + // we cannot modify the KeyRangeMap while iterating through it, so add saved modifications now + for( int i = 0; i < modifiedShards.size(); i++ ) { + insert(modifiedShards[i].first, modifiedShards[i].second); + shard_teams.insert( modifiedShards[i].second, vector( 1, modifiedShards[i].first ) ); + } + + check(); +} + +void ShardsAffectedByTeamFailure::check() { + if (EXPENSIVE_VALIDATION) { + for(auto t = team_shards.begin(); t != team_shards.end(); ++t) { + auto i = shard_teams.rangeContaining(t->second.begin); + if (i->range() != t->second || + !std::count(i->value().begin(), i->value().end(), t->first)) + { + ASSERT(false); + } + } + auto rs = shard_teams.ranges(); + for(auto i = rs.begin(); i != rs.end(); ++i) + for(vector::iterator t = i->value().begin(); t != i->value().end(); ++t) + if (!team_shards.count( make_pair( *t, i->range() ) )) { + std::string teamDesc, shards; + for(int k=0; ksize(); k++) + teamDesc += format("%llx ", (*t)[k].first()); + for(auto x = team_shards.lower_bound( make_pair( *t, KeyRangeRef() ) ); x != team_shards.end() && x->first == *t; ++x) + shards += printable(x->second.begin) + "-" + printable(x->second.end) + ","; + TraceEvent(SevError,"SATFInvariantError2") + .detail("KB", printable(i->begin())) + .detail("KE", printable(i->end())) + .detail("Team", teamDesc) + .detail("Shards", shards); + ASSERT(false); + } + } +} diff --git a/fdbserver/DatabaseConfiguration.cpp b/fdbserver/DatabaseConfiguration.cpp new file mode 100644 index 0000000000..9fb7ce998b --- /dev/null +++ b/fdbserver/DatabaseConfiguration.cpp @@ -0,0 +1,233 @@ +/* + * DatabaseConfiguration.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "DatabaseConfiguration.h" +#include "fdbclient/SystemData.h" + +DatabaseConfiguration::DatabaseConfiguration() +{ + resetInternal(); +} + +void DatabaseConfiguration::resetInternal() { + // does NOT reset rawConfiguration + initialized = false; + minDataCenters = desiredDataCenters = -1; + masterProxyCount = resolverCount = desiredTLogCount = tLogWriteAntiQuorum = tLogReplicationFactor = durableStorageQuorum = storageTeamSize = -1; + tLogDataStoreType = storageServerStoreType = KeyValueStoreType::END; + autoMasterProxyCount = CLIENT_KNOBS->DEFAULT_AUTO_PROXIES; + autoResolverCount = CLIENT_KNOBS->DEFAULT_AUTO_RESOLVERS; + autoDesiredTLogCount = CLIENT_KNOBS->DEFAULT_AUTO_LOGS; + storagePolicy = IRepPolicyRef(); + tLogPolicy = IRepPolicyRef(); +} + +void parse( int* i, ValueRef const& v ) { + // FIXME: Sanity checking + *i = atoi(v.toString().c_str()); +} + +void parseReplicationPolicy(IRepPolicyRef* policy, ValueRef const& v) { + BinaryReader reader(v, IncludeVersion()); + serializeReplicationPolicy(reader, *policy); +} + +void DatabaseConfiguration::setDefaultReplicationPolicy() { + storagePolicy = IRepPolicyRef(new PolicyAcross(storageTeamSize, "zoneid", IRepPolicyRef(new PolicyOne()))); + tLogPolicy = IRepPolicyRef(new PolicyAcross(tLogReplicationFactor, "zoneid", IRepPolicyRef(new PolicyOne()))); +} + +bool DatabaseConfiguration::isValid() const { + return initialized && + minDataCenters >= 1 && + desiredDataCenters >= 1 && + minDataCenters <= desiredDataCenters && + tLogWriteAntiQuorum >= 0 && + tLogReplicationFactor >= 1 && + durableStorageQuorum >= 1 && + storageTeamSize >= 1 && + getDesiredProxies() >= 1 && + getDesiredLogs() >= 1 && + getDesiredResolvers() >= 1 && + durableStorageQuorum <= storageTeamSize && + tLogDataStoreType != KeyValueStoreType::END && + storageServerStoreType != KeyValueStoreType::END && + autoMasterProxyCount >= 1 && + autoResolverCount >= 1 && + autoDesiredTLogCount >= 1 && + storagePolicy && + tLogPolicy + ; +} + +std::map DatabaseConfiguration::toMap() const { + std::map result; + + if( initialized ) { + if( tLogReplicationFactor == durableStorageQuorum && + durableStorageQuorum == storageTeamSize && + tLogWriteAntiQuorum == 0 ) { + if( durableStorageQuorum == 1 && desiredDataCenters == 1 && minDataCenters == 1 ) + result["redundancy_mode"] = "single"; + else if( durableStorageQuorum == 2 && desiredDataCenters == 1 && minDataCenters == 1 ) + result["redundancy_mode"] = "double"; + else if( durableStorageQuorum == 3 && desiredDataCenters == 1 && minDataCenters == 1 ) + result["redundancy_mode"] = "triple"; + else if( durableStorageQuorum == 3 && desiredDataCenters == 2 && minDataCenters == 1 ) + result["redundancy_mode"] = "two_datacenter"; + else if( durableStorageQuorum == 3 && desiredDataCenters == 3 && minDataCenters == 2 ) + result["redundancy_mode"] = "three_datacenter"; + else + result["redundancy_mode"] = "custom"; + } else + result["redundancy_mode"] = "custom"; + + if( tLogDataStoreType == KeyValueStoreType::SSD_BTREE_V1 && storageServerStoreType == KeyValueStoreType::SSD_BTREE_V1) + result["storage_engine"] = "ssd-1"; + else if (tLogDataStoreType == KeyValueStoreType::SSD_BTREE_V2 && storageServerStoreType == KeyValueStoreType::SSD_BTREE_V2) + result["storage_engine"] = "ssd-2"; + else if( tLogDataStoreType == KeyValueStoreType::MEMORY && storageServerStoreType == KeyValueStoreType::MEMORY ) + result["storage_engine"] = "memory"; + else + result["storage_engine"] = "custom"; + } + + return result; +} + +std::string DatabaseConfiguration::toString() const { + std::string result; + std::map config = toMap(); + + for(auto itr : config) { + result += itr.first + "=" + itr.second; + result += ";"; + } + + return result.substr(0, result.length()-1); +} + +bool DatabaseConfiguration::setInternal(KeyRef key, ValueRef value) { + KeyRef ck = key.removePrefix( configKeysPrefix ); + int type; + + if (ck == LiteralStringRef("initialized")) initialized = true; + else if (ck == LiteralStringRef("min_replica_datacenters")) parse(&minDataCenters, value); + else if (ck == LiteralStringRef("replica_datacenters")) parse(&desiredDataCenters, value); + else if (ck == LiteralStringRef("proxies")) parse(&masterProxyCount, value); + else if (ck == LiteralStringRef("resolvers")) parse(&resolverCount, value); + else if (ck == LiteralStringRef("logs")) parse(&desiredTLogCount, value); + else if (ck == LiteralStringRef("log_replicas")) parse(&tLogReplicationFactor, value); + else if (ck == LiteralStringRef("log_anti_quorum")) parse(&tLogWriteAntiQuorum, value); + else if (ck == LiteralStringRef("storage_quorum")) parse(&durableStorageQuorum, value); + else if (ck == LiteralStringRef("storage_replicas")) parse(&storageTeamSize, value); + else if (ck == LiteralStringRef("log_engine")) { parse((&type), value); tLogDataStoreType = (KeyValueStoreType::StoreType)type; } + else if (ck == LiteralStringRef("storage_engine")) { parse((&type), value); storageServerStoreType = (KeyValueStoreType::StoreType)type; } + else if (ck == LiteralStringRef("auto_proxies")) parse(&autoMasterProxyCount, value); + else if (ck == LiteralStringRef("auto_resolvers")) parse(&autoResolverCount, value); + else if (ck == LiteralStringRef("auto_logs")) parse(&autoDesiredTLogCount, value); + else if (ck == LiteralStringRef("storage_replication_policy")) parseReplicationPolicy(&storagePolicy, value); + else if (ck == LiteralStringRef("log_replication_policy")) parseReplicationPolicy(&tLogPolicy, value); + else return false; + return true; // All of the above options currently require recovery to take effect +} + +inline static KeyValueRef * lower_bound( VectorRef & config, KeyRef const& key ) { + return std::lower_bound( config.begin(), config.end(), KeyValueRef(key, ValueRef()), KeyValueRef::OrderByKey() ); +} +inline static KeyValueRef const* lower_bound( VectorRef const& config, KeyRef const& key ) { + return lower_bound( const_cast &>(config), key ); +} + +void DatabaseConfiguration::applyMutation( MutationRef m ) { + if( m.type == MutationRef::SetValue && m.param1.startsWith(configKeysPrefix) ) { + set(m.param1, m.param2); + } else if( m.type == MutationRef::ClearRange ) { + KeyRangeRef range(m.param1, m.param2); + if( range.intersects( configKeys ) ) { + clear(range & configKeys); + } + } +} + +bool DatabaseConfiguration::set(KeyRef key, ValueRef value) { + makeConfigurationMutable(); + mutableConfiguration.get()[ key.toString() ] = value.toString(); + return setInternal(key,value); +} + +bool DatabaseConfiguration::clear( KeyRangeRef keys ) { + makeConfigurationMutable(); + auto& mc = mutableConfiguration.get(); + mc.erase( mc.lower_bound( keys.begin.toString() ), mc.lower_bound( keys.end.toString() ) ); + + // FIXME: More efficient + bool wasValid = isValid(); + resetInternal(); + for(auto c = mc.begin(); c != mc.end(); ++c) + setInternal(c->first, c->second); + return wasValid && !isValid(); +} + +Optional DatabaseConfiguration::get( KeyRef key ) const { + if (mutableConfiguration.present()) { + auto i = mutableConfiguration.get().find(key.toString()); + if (i == mutableConfiguration.get().end()) return Optional(); + return ValueRef(i->second); + } else { + auto i = lower_bound(rawConfiguration, key); + if (i == rawConfiguration.end() || i->key != key) return Optional(); + return i->value; + } +} + +bool DatabaseConfiguration::isExcludedServer( NetworkAddress a ) const { + return get( encodeExcludedServersKey( AddressExclusion(a.ip, a.port) ) ).present() || + get( encodeExcludedServersKey( AddressExclusion(a.ip) ) ).present(); +} +std::set DatabaseConfiguration::getExcludedServers() const { + const_cast(this)->makeConfigurationImmutable(); + std::set addrs; + for( auto i = lower_bound(rawConfiguration, excludedServersKeys.begin); i != rawConfiguration.end() && i->key < excludedServersKeys.end; ++i ) { + AddressExclusion a = decodeExcludedServersKey( i->key ); + if (a.isValid()) addrs.insert(a); + } + return addrs; +} + +void DatabaseConfiguration::makeConfigurationMutable() { + if (mutableConfiguration.present()) return; + mutableConfiguration = std::map(); + auto& mc = mutableConfiguration.get(); + for(auto r = rawConfiguration.begin(); r != rawConfiguration.end(); ++r) + mc[ r->key.toString() ] = r->value.toString(); + rawConfiguration = Standalone>(); +} + +void DatabaseConfiguration::makeConfigurationImmutable() { + if (!mutableConfiguration.present()) return; + auto & mc = mutableConfiguration.get(); + rawConfiguration = Standalone>(); + rawConfiguration.resize( rawConfiguration.arena(), mc.size() ); + int i = 0; + for(auto r = mc.begin(); r != mc.end(); ++r) + rawConfiguration[i++] = KeyValueRef( rawConfiguration.arena(), KeyValueRef( r->first, r->second ) ); + mutableConfiguration = Optional>(); +} diff --git a/fdbserver/DatabaseConfiguration.h b/fdbserver/DatabaseConfiguration.h new file mode 100644 index 0000000000..0c74907815 --- /dev/null +++ b/fdbserver/DatabaseConfiguration.h @@ -0,0 +1,126 @@ +/* + * DatabaseConfiguration.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_DATABASECONFIGURATION_H +#define FDBSERVER_DATABASECONFIGURATION_H +#pragma once + +#include "fdbclient/FDBTypes.h" +#include "fdbclient/CommitTransaction.h" +#include "fdbrpc/ReplicationPolicy.h" + +// SOMEDAY: Buggify DatabaseConfiguration + +struct DatabaseConfiguration { + DatabaseConfiguration(); + + void applyMutation( MutationRef mutation ); + bool set( KeyRef key, ValueRef value ); // Returns true if a configuration option that requires recovery to take effect is changed + bool clear( KeyRangeRef keys ); + Optional get( KeyRef key ) const; + + bool isValid() const; + + bool initialized; + + std::string toString() const; + std::map toMap() const; + + // SOMEDAY: think about changing desiredDataCenters to minDataCenters + // SOMEDAY: think about changing storageTeamSize to durableStorageQuorum + int32_t minMachinesRequired() const { return std::max(tLogReplicationFactor, std::max(storageTeamSize, desiredDataCenters)); } + int32_t maxMachineFailuresTolerated() const { return std::min(tLogReplicationFactor - 1 - tLogWriteAntiQuorum, durableStorageQuorum - 1); } + + // Redundancy Levels + int32_t minDataCenters; + int32_t desiredDataCenters; + IRepPolicyRef storagePolicy; + + // MasterProxy Servers + int32_t masterProxyCount; + int32_t autoMasterProxyCount; + + // Resolvers + int32_t resolverCount; + int32_t autoResolverCount; + + // TLogs + int32_t desiredTLogCount; + int32_t autoDesiredTLogCount; + int32_t tLogWriteAntiQuorum; + int32_t tLogReplicationFactor; + KeyValueStoreType tLogDataStoreType; + IRepPolicyRef tLogPolicy; + + // Storage servers + int32_t durableStorageQuorum; + int32_t storageTeamSize; + KeyValueStoreType storageServerStoreType; + + // Excluded servers (no state should be here) + bool isExcludedServer( NetworkAddress ) const; + std::set getExcludedServers() const; + + int32_t getDesiredProxies() const { if(masterProxyCount == -1) return autoMasterProxyCount; return masterProxyCount; } + int32_t getDesiredResolvers() const { if(resolverCount == -1) return autoResolverCount; return resolverCount; } + int32_t getDesiredLogs() const { if(desiredTLogCount == -1) return autoDesiredTLogCount; return desiredTLogCount; } + + bool operator == ( DatabaseConfiguration const& rhs ) const { + const_cast(this)->makeConfigurationImmutable(); + const_cast(&rhs)->makeConfigurationImmutable(); + return rawConfiguration == rhs.rawConfiguration; + } + + template + void serialize(Ar& ar) { + if (!ar.isDeserializing) makeConfigurationImmutable(); + ar & rawConfiguration; + if (ar.isDeserializing) { + for(auto c=rawConfiguration.begin(); c!=rawConfiguration.end(); ++c) + setInternal(c->key, c->value); + if(!storagePolicy || !tLogPolicy) { + setDefaultReplicationPolicy(); + } + } + } + + void fromKeyValues( Standalone> rawConfig ) { + resetInternal(); + this->rawConfiguration = rawConfig; + for(auto c=rawConfiguration.begin(); c!=rawConfiguration.end(); ++c) + setInternal(c->key, c->value); + if(!storagePolicy || !tLogPolicy) { + setDefaultReplicationPolicy(); + } + } + +private: + Optional< std::map > mutableConfiguration; // If present, rawConfiguration is not valid + Standalone> rawConfiguration; // sorted by key + + void makeConfigurationMutable(); + void makeConfigurationImmutable(); + + bool setInternal( KeyRef key, ValueRef value ); + void resetInternal(); + void setDefaultReplicationPolicy(); +}; + +#endif diff --git a/fdbserver/DiskQueue.actor.cpp b/fdbserver/DiskQueue.actor.cpp new file mode 100644 index 0000000000..cb5ae12325 --- /dev/null +++ b/fdbserver/DiskQueue.actor.cpp @@ -0,0 +1,1102 @@ +/* + * DiskQueue.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "IDiskQueue.h" +#include "fdbrpc/IAsyncFile.h" +#include "Knobs.h" +#include "fdbrpc/simulator.h" + +typedef bool(*compare_pages)(void*,void*); +typedef int64_t loc_t; + +struct StringBuffer { + Standalone str; + int reserved; + UID id; + + StringBuffer(UID fromFileID) : reserved(0), id( fromFileID ) {} + + int size() const { return str.size(); } + StringRef& ref() { return str; } + void clear() { + str = Standalone(); + reserved = 0; + } + void clearReserve(int size) { + str = Standalone(); + reserved = size; + ref() = StringRef( new (str.arena()) uint8_t[size], 0 ); + } + void append( StringRef x ) { + memcpy( append(x.size()), x.begin(), x.size() ); + } + void* append(int bytes) { + ASSERT( str.size() + bytes <= reserved ); + void* p = const_cast(str.end()); + ref() = StringRef( str.begin(), str.size()+bytes ); + return p; + } + StringRef pop_front(int bytes) { + ASSERT( bytes <= str.size() ); + StringRef result = str.substr(0, bytes); + ref() = str.substr(bytes); + return result; + } + void alignReserve(int alignment, int size) { + ASSERT( alignment && (alignment & (alignment-1)) == 0 ); // alignment is a power of two + + if (size >= reserved) { + // SOMEDAY: Use a new arena and discard the old one after copying? + reserved = std::max( size, reserved*2 ); + if( reserved > 1e9 ) { + printf("WOAH! Huge allocation\n"); + TraceEvent(SevError, "StringBufferHugeAllocation", id).detail("Alignment", alignment).detail("Reserved", reserved).backtrace(); + } + uint8_t* b = new (str.arena()) uint8_t[reserved+alignment-1]; + uint8_t* e = b + (reserved+alignment-1); + + uint8_t* p = (uint8_t*)(int64_t(b+alignment-1) & ~(alignment-1)); // first multiple of alignment greater than or equal to b + ASSERT( p>=b && p+reserved<=e && int64_t(p)%alignment == 0 ); + + memcpy(p, str.begin(), str.size()); + ref() = StringRef( p, str.size() ); + } + } +}; + +struct SyncQueue : ReferenceCounted { + SyncQueue( int outstandingLimit, Reference file ) + : outstandingLimit(outstandingLimit), file(file) + { + for(int i=0; i onSync() { // Future is set when all writes completed before the call to onSync are complete + if (outstanding.size() <= outstandingLimit) + outstanding.push_back( waitAndSync(this) ); + return outstanding.back(); + } + +private: + int outstandingLimit; + Deque> outstanding; + Reference file; + + ACTOR static Future waitAndSync(SyncQueue* self) { + Void _ = wait( self->outstanding.front() ); + self->outstanding.pop_front(); + Void _ = wait( self->file->sync() ); + return Void(); + } +}; + +class RawDiskQueue_TwoFiles { +public: + RawDiskQueue_TwoFiles( std::string basename, UID dbgid ) + : basename(basename), onError(delayed(error.getFuture())), onStopped(stopped.getFuture()), + readingFile(-1), readingPage(-1), writingPos(-1), dbgid(dbgid), + dbg_file0BeginSeq(0), fileExtensionBytes(10<<20), readingBuffer( dbgid ), + readyToPush(Void()) + { + if(BUGGIFY) + fileExtensionBytes = 8<<10; + files[0].dbgFilename = filename(0); + files[1].dbgFilename = filename(1); + stallCount.init(LiteralStringRef("RawDiskQueue.StallCount")); + } + + Future pushAndCommit( StringRef pageData, StringBuffer* pageMem, uint64_t poppedPages ) { + return pushAndCommit( this, pageData, pageMem, poppedPages ); + } + + void stall() { + stallCount++; + readyToPush = lastCommit; + } + + Future> readFirstAndLastPages( compare_pages compare ) { return readFirstAndLastPages(this,compare); } + + void setStartPage( int file, int64_t page ) { + TraceEvent("RDQSetStart", dbgid).detail("f",file).detail("p",page).detail("file0name", files[0].dbgFilename); + readingFile = file; + readingPage = page; + } + + Future setPoppedPage( int file, int64_t page, int64_t debugSeq ) { return setPoppedPage(this, file, page, debugSeq); } + + Future> readNextPage() { return readNextPage(this); } + Future truncateBeforeLastReadPage() { return truncateBeforeLastReadPage(this); } + + Future getError() { return onError; } + Future onClosed() { return onStopped; } + void dispose() { shutdown(this, true); } + void close() { shutdown(this, false); } + + StorageBytes getStorageBytes() { + int64_t free; + int64_t total; + + g_network->getDiskBytes(parentDirectory(basename), free, total); + + return StorageBytes(free, total, files[0].size + files[1].size, free); // TODO: we could potentially do better in the available field by accounting for the unused pages at the end of the file + } + +//private: + struct Page { uint8_t data[_PAGE_SIZE]; }; + + struct File { + Reference f; + int64_t size; // always a multiple of _PAGE_SIZE, even if the physical file isn't for some reason + int64_t popped; + std::string dbgFilename; + Reference syncQueue; + + File() : size(-1), popped(-1) {} + + void setFile(Reference f) { + this->f = f; + this->syncQueue = Reference( new SyncQueue(1, f) ); + } + }; + File files[2]; // After readFirstAndLastPages(), files[0] is logically before files[1] (pushes are always into files[1]) + + std::string basename; + std::string filename(int i) const { return basename + format("%d.fdq", i); } + + UID dbgid; + int64_t dbg_file0BeginSeq; + + Promise error, stopped; + Future onError, onStopped; + + Future readyToPush; + AndFuture lastCommit; + + StringBuffer readingBuffer; // Pages that have been read and not yet returned + int readingFile; // i if the next page after readingBuffer should be read from files[i], 2 if recovery is complete + int64_t readingPage; // Page within readingFile that is the next page after readingBuffer + + int64_t writingPos; // Position within files[1] that will be next written + + int64_t fileExtensionBytes; + + AsyncMap recoveryActorCount; + + Int64MetricHandle stallCount; + + struct TrackMe : NonCopyable { + RawDiskQueue_TwoFiles* self; + TrackMe( RawDiskQueue_TwoFiles* self ) : self(self) { + self->recoveryActorCount.set(false, self->recoveryActorCount.get(false)+1); + } + ~TrackMe() { + self->recoveryActorCount.set(false, self->recoveryActorCount.get(false)-1); + } + }; + + Future truncateFile(int file, int64_t pos) { return truncateFile(this, file, pos); } + + Future push(StringRef pageData, vector>& toSync) { + // Write the given data to the queue files, swapping or extending them if necessary. + // Don't do any syncs, but push the modified file(s) onto toSync. + ASSERT( readingFile == 2 ); + ASSERT( pageData.size() % _PAGE_SIZE == 0 ); + ASSERT( int64_t(pageData.begin()) % _PAGE_SIZE == 0 ); + ASSERT( writingPos % _PAGE_SIZE == 0 ); + ASSERT( files[0].size % _PAGE_SIZE == 0 && files[1].size % _PAGE_SIZE == 0 ); + + vector> waitfor; + + if (pageData.size() + writingPos > files[1].size) { + if ( files[0].popped == files[0].size ) { + // Finish files[1] and swap + int p = files[1].size - writingPos; + if(p > 0) { + toSync.push_back( files[1].syncQueue ); + /*TraceEvent("RDQWriteAndSwap", this->dbgid).detail("File1name", files[1].dbgFilename).detail("File1size", files[1].size) + .detail("writingPos", writingPos).detail("writingBytes", p);*/ + waitfor.push_back( files[1].f->write( pageData.begin(), p, writingPos ) ); + pageData = pageData.substr( p ); + } + + dbg_file0BeginSeq += files[0].size; + std::swap(files[0], files[1]); + files[1].popped = 0; + writingPos = 0; + } else { + // Extend files[1] to accomodate the new write and about 10MB or 2x current size for future writes. + /*TraceEvent("RDQExtend", this->dbgid).detail("File1name", files[1].dbgFilename).detail("File1size", files[1].size) + .detail("extensionBytes", fileExtensionBytes);*/ + int64_t minExtension = pageData.size() + writingPos - files[1].size; + files[1].size += std::min(std::max(fileExtensionBytes, minExtension), files[0].size+files[1].size+minExtension); + waitfor.push_back( files[1].f->truncate( files[1].size ) ); + } + } + + /*TraceEvent("RDQWrite", this->dbgid).detail("File1name", files[1].dbgFilename).detail("File1size", files[1].size) + .detail("writingPos", writingPos).detail("writingBytes", pageData.size());*/ + files[1].size = std::max( files[1].size, writingPos + pageData.size() ); + toSync.push_back( files[1].syncQueue ); + waitfor.push_back( files[1].f->write( pageData.begin(), pageData.size(), writingPos ) ); + writingPos += pageData.size(); + + return waitForAll(waitfor); + } + + ACTOR static UNCANCELLABLE Future pushAndCommit(RawDiskQueue_TwoFiles* self, StringRef pageData, StringBuffer* pageMem, uint64_t poppedPages) { + state Promise pushing, committed; + state Promise errorPromise = self->error; + state std::string filename = self->files[0].dbgFilename; + state UID dbgid = self->dbgid; + state vector> syncFiles; + try { + // pushing might need to wait for previous pushes to start (to maintain order) or for + // a previous commit to finish if stall() was called + Future ready = self->readyToPush; + self->readyToPush = pushing.getFuture(); + self->lastCommit.add( committed.getFuture() ); + + Void _ = wait( ready ); + + TEST( pageData.size() > sizeof(Page) ); // push more than one page of data + + Future pushed = self->push( pageData, syncFiles ); + pushing.send(Void()); + ASSERT( syncFiles.size() >= 1 && syncFiles.size() <= 2 ); + TEST(2==syncFiles.size()); // push spans both files + Void _ = wait( pushed ); + + delete pageMem; + pageMem = 0; + + Future sync = syncFiles[0]->onSync(); + for(int i=1; ionSync(); + Void _ = wait( sync ); + + self->updatePopped( poppedPages*sizeof(Page) ); + + /*TraceEvent("RDQCommitEnd", self->dbgid).detail("DeltaPopped", poppedPages*sizeof(Page)).detail("PoppedCommitted", self->dbg_file0BeginSeq + self->files[0].popped + self->files[1].popped) + .detail("File0Size", self->files[0].size).detail("File1Size", self->files[1].size) + .detail("File0Name", self->files[0].dbgFilename).detail("SyncedFiles", syncFiles.size());*/ + + committed.send(Void()); + if(g_random->random01() < 0.01) { + //occasionally delete all the ready future in the AndFuture + self->lastCommit.cleanup(); + } + } catch (Error& e) { + delete pageMem; + TEST(true); // push error + TEST(2==syncFiles.size()); // push spanning both files error + TraceEvent(SevError, "RDQ_pushAndCommit_Error", dbgid).detail("InitialFilename0", filename).error(e, true); + + if (errorPromise.canBeSet()) errorPromise.sendError(e); + if (pushing.canBeSet()) pushing.sendError(e); + if (committed.canBeSet()) committed.sendError(e); + + throw e; + } + return Void(); + } + + void updatePopped( int64_t popped ) { + int64_t pop0 = std::min(popped, files[0].size - files[0].popped); + files[0].popped += pop0; + files[1].popped += popped - pop0; + } + + + ACTOR static Future setPoppedPage( RawDiskQueue_TwoFiles *self, int file, int64_t page, int64_t debugSeq ) { + self->files[file].popped = page*sizeof(Page); + if (file) self->files[0].popped = self->files[0].size; + else self->files[1].popped = 0; + self->dbg_file0BeginSeq = debugSeq - self->files[1].popped - self->files[0].popped; + + //If we are starting in file 1, we truncate file 0 in case it has been corrupted. + // In particular, we are trying to avoid a dropped or corrupted write to the first page of file 0 causing it to be sequenced before file 1, + // when in fact it contains many pages that follow file 1. These ok pages may be incorrectly read if the machine dies after overwritting the + // first page of file 0 and is then recovered + if(file == 1) + Void _ = wait(self->truncateFile(self, 0, 0)); + + return Void(); + } + + ACTOR static Future openFiles( RawDiskQueue_TwoFiles* self ) { + state vector>> fs; + for(int i=0; i<2; i++) + fs.push_back( IAsyncFileSystem::filesystem()->open( self->filename(i), IAsyncFile::OPEN_READWRITE | IAsyncFile::OPEN_UNCACHED | IAsyncFile::OPEN_UNBUFFERED | IAsyncFile::OPEN_LOCK, 0 ) ); + Void _ = wait( waitForAllReady(fs) ); + + // Treatment of errors here is important. If only one of the two files is present + // (due to a power failure during creation or deletion, or administrative error) we don't want to + // open the queue! + + if (!fs[0].isError() && !fs[1].isError()) { + // Both files were opened OK: success + } else if ( fs[0].isError() && fs[0].getError().code() == error_code_file_not_found && + fs[1].isError() && fs[1].getError().code() == error_code_file_not_found ) + { + // Neither file was found: we can create a new queue + // OPEN_ATOMIC_WRITE_AND_CREATE defers creation (using a .part file) until the calls to sync() below + TraceEvent("DiskQueueCreate").detail("File0", self->filename(0)); + for(int i=0; i<2; i++) + fs[i] = IAsyncFileSystem::filesystem()->open( self->filename(i), IAsyncFile::OPEN_ATOMIC_WRITE_AND_CREATE | IAsyncFile::OPEN_CREATE | IAsyncFile::OPEN_READWRITE | IAsyncFile::OPEN_UNCACHED | IAsyncFile::OPEN_UNBUFFERED | IAsyncFile::OPEN_LOCK, 0600 ); + + // Any error here is fatal + Void _ = wait( waitForAll(fs) ); + + // sync on each file to actually create it will be done below + } else { + // One file had a more serious error or one file is present and the other is not. Die. + if (!fs[0].isError() || (fs[1].isError() && fs[1].getError().code() != error_code_file_not_found)) + throw fs[1].getError(); + else + throw fs[0].getError(); + } + + // fsync both files. This is necessary to trigger atomic file creation in the creation case above. + // It also permits the recovery code to assume that whatever it reads is durable. Otherwise a prior + // process could have written (but not synchronized) data to the file which we will read but which + // might not survive a reboot. The recovery code assumes otherwise and could corrupt the disk. + vector> syncs; + for(int i=0; isync() ); + Void _ = wait(waitForAll(syncs)); + + // Successfully opened or created; fill in self->files[] + for(int i=0; i<2; i++) + self->files[i].setFile(fs[i].get()); + + return Void(); + } + + ACTOR static void shutdown( RawDiskQueue_TwoFiles* self, bool deleteFiles ) { + // Wait for all reads and writes on the file, and all actors referencing self, to be finished + state Error error = success(); + try { + ErrorOr _ = wait(errorOr(self->lastCommit.getFuture())); + while (self->recoveryActorCount.get(false)) + Void _ = wait( self->recoveryActorCount.onChange(false) ); + + for(int i=0; i<2; i++) + self->files[i].f.clear(); + + if (deleteFiles) { + TraceEvent("DiskQueueShutdownDeleting", self->dbgid) + .detail("File0", self->filename(0)) + .detail("File1", self->filename(1)); + Void _ = wait( IAsyncFileSystem::filesystem()->deleteFile( self->filename(0), false ) ); + Void _ = wait( IAsyncFileSystem::filesystem()->deleteFile( self->filename(1), true ) ); + } + TraceEvent("DiskQueueShutdownComplete", self->dbgid) + .detail("DeleteFiles", deleteFiles) + .detail("File0", self->filename(0)); + } catch( Error &e ) { + TraceEvent(SevError, "DiskQueueShutdownError", self->dbgid) + .detail("Reason", e.code() == error_code_platform_error ? "could not delete database" : "unknown") + .error(e,true); + error = e; + } + + if( error.code() != error_code_actor_cancelled ) { + if (!self->stopped.isSet()) self->stopped.send(Void()); + if (!self->error.isSet()) self->error.send(Never()); + delete self; + } + } + + ACTOR static UNCANCELLABLE Future> readFirstAndLastPages(RawDiskQueue_TwoFiles* self, compare_pages compare) { + state TrackMe trackMe(self); + state StringBuffer result( self->dbgid ); + + try { + result.alignReserve( sizeof(Page), sizeof(Page)*3 ); + state Page* firstPage = (Page*)result.append(sizeof(Page)*3); + + // Open both files or create both files + Void _ = wait( openFiles(self) ); + + // Get the file sizes + vector> fsize; + for(int i=0; i<2; i++) + fsize.push_back( self->files[i].f->size() ); + vector file_sizes = wait( getAll(fsize) ); + for(int i=0; i<2; i++) { + // SOMEDAY: If the file size is not a multiple of page size, it may never be shortened. Change this? + self->files[i].size = file_sizes[i] - file_sizes[i] % sizeof(Page); + ASSERT( self->files[i].size % sizeof(Page) == 0 ); + } + + // Read the first pages + memset(firstPage, 0, sizeof(Page)*2); + vector> reads; + for(int i=0; i<2; i++) + if( self->files[i].size > 0) + reads.push_back( self->files[i].f->read( &firstPage[i], sizeof(Page), 0 ) ); + Void _ = wait( waitForAll(reads) ); + + // Determine which file comes first + if ( compare( &firstPage[1], &firstPage[0] ) ) { + std::swap( firstPage[0], firstPage[1] ); + std::swap( self->files[0], self->files[1] ); + } + + if ( !compare( &firstPage[1], &firstPage[1] ) ) { + // Both files are invalid... the queue is empty! + // Begin pushing at the beginning of files[1] + + //Truncate both files, since perhaps only the first pages are corrupted. This avoids cases where overwritting the first page and then terminating makes + //subsequent pages valid upon recovery. + vector> truncates; + for(int i = 0; i < 2; ++i) + if(self->files[i].size > 0) + truncates.push_back(self->truncateFile(self, i, 0)); + + Void _ = wait(waitForAll(truncates)); + + + self->files[0].popped = self->files[0].size; + self->files[1].popped = 0; + self->writingPos = 0; + self->readingFile = 2; + return Standalone(); + } + + // A page in files[1] is "valid" iff compare(&firstPage[1], page) + // Binary search to find a page in files[1] that is "valid" but the next page is not valid + // Invariant: the page at begin is valid, and the page at end is invalid + state int64_t begin = 0; + state int64_t end = self->files[1].size/sizeof(Page); + state Page *middlePage = &firstPage[2]; + while ( begin + 1 != end ) { + state int64_t middle = (begin+end)/2; + ASSERT( middle > begin && middle < end ); // So the loop always changes begin or end + + int len = wait( self->files[1].f->read( middlePage, sizeof(Page), middle*sizeof(Page) ) ); + ASSERT( len == sizeof(Page) ); + + bool middleValid = compare( &firstPage[1], middlePage ); + + TraceEvent("RDQBS", self->dbgid).detail("b", begin).detail("e", end).detail("m", middle).detail("v", middleValid).detail("file0name", self->files[0].dbgFilename); + + if (middleValid) + begin = middle; + else + end = middle; + } + // Now by the invariant and the loop condition, begin is a valid page and begin+1 is an invalid page + // Check that begin+1 is invalid + int len = wait( self->files[1].f->read( &firstPage[2], sizeof(Page), (begin+1)*sizeof(Page) ) ); + ASSERT( !(len == sizeof(Page) && compare( &firstPage[1], &firstPage[2] )) ); + + // Read it + int len = wait( self->files[1].f->read( &firstPage[2], sizeof(Page), begin*sizeof(Page) ) ); + ASSERT( len == sizeof(Page) && compare( &firstPage[1], &firstPage[2] ) ); + + TraceEvent("RDQEndFound", self->dbgid).detail("File0Name", self->files[0].dbgFilename).detail("Pos", begin).detail("FileSize", self->files[1].size); + + return result.str; + } catch (Error& e) { + bool ok = e.code() == error_code_file_not_found; + TraceEvent(ok ? SevInfo : SevError, "RDQ_rfl_Error", self->dbgid).detail("file0name", self->files[0].dbgFilename).error(e, true); + if (!self->error.isSet()) self->error.sendError(e); + throw; + } + } + + Future fillReadingBuffer() { + // If we're right at the end of a file... + if ( readingPage*sizeof(Page) >= (size_t)files[readingFile].size ) { + readingFile++; + readingPage = 0; + if (readingFile>=2) { + // Recovery complete + readingBuffer.clear(); + writingPos = files[1].size; + return 0; + } + } + + // Read up to 1MB into readingBuffer + int len = std::min( (files[readingFile].size/sizeof(Page) - readingPage)*sizeof(Page), BUGGIFY_WITH_PROB(1.0) ? sizeof(Page)*g_random->randomInt(1,4) : (1<<20) ); + readingBuffer.clear(); + readingBuffer.alignReserve( sizeof(Page), len ); + void* p = readingBuffer.append(len); + + auto pos = readingPage * sizeof(Page); + readingPage += len / sizeof(Page); + ASSERT( int64_t(p) % sizeof(Page) == 0 ); + return files[readingFile].f->read( p, len, pos ); + } + + ACTOR static UNCANCELLABLE Future> readNextPage(RawDiskQueue_TwoFiles* self) { + state TrackMe trackMe(self); + + try { + ASSERT( self->readingFile < 2 ); + ASSERT( self->files[0].f && self->files[1].f ); + + + if (!self->readingBuffer.size()) { + state Future f = Void(); + //if (BUGGIFY) f = delay( g_random->random01() * 0.1 ); + + int read = wait( self->fillReadingBuffer() ); + ASSERT( read == self->readingBuffer.size() ); + + Void _ = wait(f); + } + if (!self->readingBuffer.size()) return Standalone(); + + ASSERT( self->readingBuffer.size() >= sizeof(Page) ); + Standalone result = self->readingBuffer.pop_front( sizeof(Page) ); + return result; + } catch (Error& e) { + TEST(true); // Read next page error + TraceEvent(SevError, "RDQ_rnp_Error", self->dbgid).detail("file0name", self->files[0].dbgFilename).error(e, true); + if (!self->error.isSet()) self->error.sendError(e); + throw; + } + } + + ACTOR static UNCANCELLABLE Future truncateFile(RawDiskQueue_TwoFiles* self, int file, int64_t pos) { + state TrackMe trackMe(self); + state StringBuffer zeros( self->dbgid ); + + TraceEvent("DQTruncateFile", self->dbgid).detail("File", file).detail("Pos", pos).detail("File0Name", self->files[0].dbgFilename); + + zeros.alignReserve( sizeof(Page), 1<<20 ); + memset( zeros.append(1<<20), 0, 1<<20 ); + + while(pos < self->files[file].size) { + state int len = std::min(zeros.size(), self->files[file].size-pos); + Void _ = wait( self->files[file].f->write( zeros.str.begin(), len, pos ) ); + pos += len; + } + + Void _ = wait(self->files[file].syncQueue->onSync()); + return Void(); + } + + ACTOR static Future truncateBeforeLastReadPage( RawDiskQueue_TwoFiles* self ) { + try { + state int file = self->readingFile; + state int64_t pos = (self->readingPage - self->readingBuffer.size()/sizeof(Page) - 1) * sizeof(Page); + state vector> commits; + state bool swap = file==0; + + TEST( file==0 ); // truncate before last read page on file 0 + TEST( file==1 && pos != self->files[1].size ); // truncate before last read page on file 1 + + self->readingFile = 2; + self->readingBuffer.clear(); + self->writingPos = pos; + + while (file < 2) { + commits.push_back(self->truncateFile(self, file, pos)); + file++; + pos = 0; + } + + Void _ = wait( waitForAll(commits) ); + + if (swap) { + std::swap(self->files[0], self->files[1]); + self->files[0].popped = self->files[0].size; + } + + return Void(); + } catch (Error& e) { + TraceEvent(SevError, "RDQ_tblrp_Error", self->dbgid).detail("file0name", self->files[0].dbgFilename).error(e); + if (!self->error.isSet()) self->error.sendError(e); + throw; + } + } +}; + +class DiskQueue : public IDiskQueue { +public: + DiskQueue( std::string basename, UID dbgid ) + : rawQueue( new RawDiskQueue_TwoFiles(basename, dbgid) ), dbgid(dbgid), anyPopped(false), nextPageSeq(0), poppedSeq(0), lastPoppedSeq(0), + nextReadLocation(-1), readBufPage(NULL), readBufPos(0), pushed_page_buffer(NULL), recovered(false), lastCommittedSeq(0), warnAlwaysForMemory(true) + { + } + + virtual location push( StringRef contents ) { + ASSERT( recovered ); + uint8_t const* begin = contents.begin(); + uint8_t const* end = contents.end(); + TEST( contents.size() && pushedPageCount() ); // More than one push between commits + TEST( contents.size()>=4 && pushedPageCount() && backPage().remainingCapacity()<4 ); // Push right at the end of a page, possibly splitting size + while (begin != end) { + if (!pushedPageCount() || !backPage().remainingCapacity()) addEmptyPage(); + + auto &p = backPage(); + int s = std::min( p.remainingCapacity(), end-begin ); + memcpy( p.payload + p.payloadSize, begin, s ); + p.payloadSize += s; + begin += s; + } + return endLocation(); + } + virtual void pop( location upTo ) { + ASSERT( !upTo.hi ); + ASSERT( !recovered || upTo.lo <= endLocation() ); + + // The following ASSERT is NOT part of the intended contract of IDiskQueue, but alerts the user to a known bug where popping + // into uncommitted pages can cause a durability failure. + // FIXME: Remove this ASSERT when popping into uncommitted pages is fixed + if( upTo.lo > lastCommittedSeq ) { + TraceEvent(SevError, "DQPopUncommittedData", dbgid) + .detail("UpTo", upTo) + .detail("LastCommittedSeq", lastCommittedSeq) + .detail("file0name", rawQueue->files[0].dbgFilename); + } + if (upTo.lo > poppedSeq) { + poppedSeq = upTo.lo; + anyPopped = true; + } + } + + int getMaxPayload() { + return Page::maxPayload; + } + + virtual int getCommitOverhead() { + if(!pushedPageCount()) { + if(!anyPopped) + return 0; + + return Page::maxPayload; + } + else + return backPage().remainingCapacity(); + } + + virtual Future commit() { + ASSERT( recovered ); + if (!pushedPageCount()) { + if (!anyPopped) return Void(); + anyPopped = false; + addEmptyPage(); + } + backPage().popped = poppedSeq; + backPage().zeroPad(); + backPage().updateHash(); + + if( pushedPageCount() >= 8000 ) { + TraceEvent( warnAlwaysForMemory ? SevWarnAlways : SevWarn, "DiskQueueMemoryWarning", dbgid) + .detail("pushed_pages", pushedPageCount()) + .detail("nextPageSeq", nextPageSeq) + .detail("Details", format("%d pages", pushedPageCount())) + .detail("file0name", rawQueue->files[0].dbgFilename) + .suppressFor(1.0); + if(g_network->isSimulated()) + warnAlwaysForMemory = false; + } + + /*TraceEvent("DQCommit", dbgid).detail("Pages", pushedPageCount()).detail("lastPoppedSeq", lastPoppedSeq).detail("poppedSeq", poppedSeq).detail("nextPageSeq", nextPageSeq) + .detail("RawFile0Size", rawQueue->files[0].size).detail("RawFile1Size", rawQueue->files[1].size).detail("writingPos", rawQueue->writingPos) + .detail("RawFile0Name", rawQueue->files[0].dbgFilename);*/ + + lastCommittedSeq = backPage().endSeq(); + auto f = rawQueue->pushAndCommit( pushed_page_buffer->ref(), pushed_page_buffer, poppedSeq/sizeof(Page) - lastPoppedSeq/sizeof(Page) ); + lastPoppedSeq = poppedSeq; + pushed_page_buffer = 0; + return f; + } + void stall() { + rawQueue->stall(); + } + + virtual Future> readNext( int bytes ) { return readNext(this, bytes); } + + virtual location getNextReadLocation() { return nextReadLocation; } + + virtual Future getError() { return rawQueue->getError(); } + virtual Future onClosed() { return rawQueue->onClosed(); } + virtual void dispose() { + TraceEvent("DQDestroy", dbgid).detail("lastPoppedSeq", lastPoppedSeq).detail("poppedSeq", poppedSeq).detail("nextPageSeq", nextPageSeq).detail("file0name", rawQueue->files[0].dbgFilename); + rawQueue->dispose(); + delete this; + } + virtual void close() { + TraceEvent("DQClose", dbgid) + .detail("lastPoppedSeq", lastPoppedSeq) + .detail("poppedSeq", poppedSeq) + .detail("nextPageSeq", nextPageSeq) + .detail("poppedCommitted", rawQueue->dbg_file0BeginSeq + rawQueue->files[0].popped + rawQueue->files[1].popped) + .detail("file0name", rawQueue->files[0].dbgFilename); + rawQueue->close(); + delete this; + } + + virtual StorageBytes getStorageBytes() { + return rawQueue->getStorageBytes(); + } + +private: + #pragma pack(push, 1) + struct PageHeader { + UID hash; + uint64_t seq; + uint64_t popped; + int payloadSize; + }; + + struct Page : PageHeader { + static const int maxPayload = _PAGE_SIZE - sizeof(PageHeader); + uint8_t payload[maxPayload]; + + int remainingCapacity() const { return maxPayload - payloadSize; } + uint64_t endSeq() const { return seq + sizeof(PageHeader) + payloadSize; } + void updateHash() { + // SOMEDAY: Better hash? + uint32_t part[2] = { 0x12345678, 0xbeefabcd }; + hashlittle2( &seq, sizeof(Page)-sizeof(hash), &part[0], &part[1] ); + hash = UID( (int64_t(part[0])<<32)+part[1], 0xfdb ); + } + bool checkHash() { + UID h = hash; + updateHash(); + if (h != hash) { std::swap(h, hash); return false; } + return true; + } + void zeroPad() { + memset( payload+payloadSize, 0, maxPayload-payloadSize ); + } + }; + #pragma pack(pop) + + loc_t endLocation() const { return pushedPageCount() ? backPage().endSeq() : nextPageSeq; } + + void addEmptyPage() { + if (pushedPageCount()) { + backPage().updateHash(); + ASSERT( backPage().payloadSize == Page::maxPayload ); + } + + //pushed_pages.resize( pushed_pages.arena(), pushed_pages.size()+1 ); + if (!pushed_page_buffer) pushed_page_buffer = new StringBuffer( dbgid ); + pushed_page_buffer->alignReserve( sizeof(Page), pushed_page_buffer->size() + sizeof(Page) ); + pushed_page_buffer->append( sizeof(Page) ); + + ASSERT( nextPageSeq%sizeof(Page)==0 ); + + auto& p = backPage(); + memset(&p, 0, sizeof(Page)); // FIXME: unnecessary? + p.payloadSize = 0; + p.seq = nextPageSeq; + nextPageSeq += sizeof(Page); + p.popped = poppedSeq; + + if (pushedPageCount() == 8000) { + TraceEvent("DiskQueueHighPageCount", dbgid) + .detail("pushed_pages", pushedPageCount()) + .detail("nextPageSeq", nextPageSeq) + .detail("file0name", rawQueue->files[0].dbgFilename); + } + } + + void readFromBuffer( StringBuffer& result, int& bytes ) { + // extract up to bytes from readBufPage into result + int len = std::min( readBufPage->payloadSize - readBufPos, bytes ); + if (len<=0) return; + + result.append( StringRef(readBufPage->payload+readBufPos, len) ); + + readBufPos += len; + bytes -= len; + nextReadLocation += len; + } + + ACTOR static Future> readNext( DiskQueue *self, int bytes ) { + state StringBuffer result( self->dbgid ); + ASSERT(bytes >= 0); + result.clearReserve(bytes); + + ASSERT( !self->recovered ); + + if (self->nextReadLocation < 0) { + bool nonempty = wait( findStart(self) ); + if (!nonempty) { + // The constructor has already put everything in the right state for an empty queue + self->recovered = true; + ASSERT( self->poppedSeq <= self->endLocation() ); + + //The next read location isn't necessarily the end of the last commit, but this is sufficient for helping us check an ASSERTion + self->lastCommittedSeq = self->nextReadLocation; + + return Standalone(); + } + self->readBufPos = self->nextReadLocation % sizeof(Page) - sizeof(PageHeader); + if (self->readBufPos < 0) { self->nextReadLocation -= self->readBufPos; self->readBufPos = 0; } + TraceEvent("DQRecStart", self->dbgid).detail("readBufPos", self->readBufPos).detail("nextReadLoc", self->nextReadLocation).detail("file0name", self->rawQueue->files[0].dbgFilename); + } + + loop { + if (self->readBufPage) { + self->readFromBuffer( result, bytes ); + // if done, return + if (!bytes) return result.str; + ASSERT( self->readBufPos == self->readBufPage->payloadSize ); + self->readBufPage = 0; + self->nextReadLocation += sizeof(Page) - self->readBufPos; + self->readBufPos = 0; + } + + Standalone page = wait( self->rawQueue->readNextPage() ); + if (!page.size()) { + TraceEvent("DQRecEOF", self->dbgid).detail("nextReadLocation", self->nextReadLocation).detail("file0name", self->rawQueue->files[0].dbgFilename); + break; + } + ASSERT( page.size() == sizeof(Page) ); + + self->readBufArena = page.arena(); + self->readBufPage = (Page*)page.begin(); + if (!self->readBufPage->checkHash() || self->readBufPage->seq < self->nextReadLocation/sizeof(Page)*sizeof(Page)) { + TraceEvent("DQRecInvalidPage", self->dbgid).detail("nextReadLocation", self->nextReadLocation).detail("hashCheck", self->readBufPage->checkHash()) + .detail("seq", self->readBufPage->seq).detail("expect", self->nextReadLocation/sizeof(Page)*sizeof(Page)).detail("file0name", self->rawQueue->files[0].dbgFilename); + Void _ = wait( self->rawQueue->truncateBeforeLastReadPage() ); + break; + } + //TraceEvent("DQRecPage", self->dbgid).detail("nextReadLoc", self->nextReadLocation).detail("Seq", self->readBufPage->seq).detail("Pop", self->readBufPage->popped).detail("Payload", self->readBufPage->payloadSize).detail("file0name", self->rawQueue->files[0].dbgFilename); + ASSERT( self->readBufPage->seq == self->nextReadLocation/sizeof(Page)*sizeof(Page) ); + self->lastPoppedSeq = self->readBufPage->popped; + } + + // Recovery complete. + // The fully durable popped point is self->lastPoppedSeq; tell the raw queue that. + int f; int64_t p; + TEST( self->lastPoppedSeq/sizeof(Page) != self->poppedSeq/sizeof(Page) ); // DiskQueue: Recovery popped position not fully durable + self->findPhysicalLocation( self->lastPoppedSeq, f, p, "lastPoppedSeq" ); + Void _ = wait(self->rawQueue->setPoppedPage( f, p, self->lastPoppedSeq/sizeof(Page)*sizeof(Page) )); + + // Writes go at the end of our reads (but on the next page) + self->nextPageSeq = self->nextReadLocation/sizeof(Page)*sizeof(Page); + if (self->nextReadLocation % sizeof(Page) > 36) self->nextPageSeq += sizeof(Page); + + TraceEvent("DQRecovered", self->dbgid).detail("lastPoppedSeq", self->lastPoppedSeq).detail("poppedSeq", self->poppedSeq).detail("nextPageSeq", self->nextPageSeq).detail("file0name", self->rawQueue->files[0].dbgFilename); + self->recovered = true; + ASSERT( self->poppedSeq <= self->endLocation() ); + self->recoveryFirstPages = Standalone(); + + TEST( result.size() == 0 ); // End of queue at border between reads + TEST( result.size() != 0 ); // Partial read at end of queue + + //The next read location isn't necessarily the end of the last commit, but this is sufficient for helping us check an ASSERTion + self->lastCommittedSeq = self->nextReadLocation; + + return result.str; + } + + ACTOR static Future findStart( DiskQueue* self ) { + Standalone epbuf = wait( self->rawQueue->readFirstAndLastPages( &comparePages ) ); + ASSERT( epbuf.size() % sizeof(Page) == 0 ); + self->recoveryFirstPages = epbuf; + + if (!epbuf.size()) { + // There are no valid pages, so apparently this is a completely empty queue + self->nextReadLocation = 0; + return false; + } + + int n = epbuf.size() / sizeof(Page); + Page* lastPage = (Page*)epbuf.end() - 1; + self->nextReadLocation = self->poppedSeq = lastPage->popped; + + /* + state std::auto_ptr testPage(new Page); + state int fileNum; + for( fileNum=0; fileNum<2; fileNum++) { + state int sizeNum; + for( sizeNum=0; sizeNum < self->rawQueue->files[fileNum].size; sizeNum += sizeof(Page) ) { + int _ = wait( self->rawQueue->files[fileNum].f->read( testPage.get(), sizeof(Page), sizeNum ) ); + TraceEvent("PageData").detail("file", self->rawQueue->files[fileNum].dbgFilename).detail("sizeNum", sizeNum).detail("seq", testPage->seq).detail("hash", testPage->checkHash()).detail("popped", testPage->popped); + } + } + */ + + int file; int64_t page; + self->findPhysicalLocation( self->poppedSeq, file, page, "poppedSeq" ); + self->rawQueue->setStartPage( file, page ); + + return true; + } + + void findPhysicalLocation( loc_t loc, int& file, int64_t& page, const char* context ) { + bool ok = false; + Page*p = (Page*)recoveryFirstPages.begin(); + + TraceEvent(SevInfo, "FindPhysicalLocation", dbgid) + .detail("RecoveryFirstPages", recoveryFirstPages.size()) + .detail("Page0Valid", p[0].checkHash()) + .detail("Page0Seq", p[0].seq) + .detail("Page1Valid", p[1].checkHash()) + .detail("Page1Seq", p[1].seq) + .detail("Location", loc) + .detail("Context", context) + .detail("file0name", rawQueue->files[0].dbgFilename); + + for(int i=recoveryFirstPages.size() / sizeof(Page) - 2; i>=0; i--) + if ( p[i].checkHash() && p[i].seq <= (size_t)loc ) { + file = i; + page = (loc - p[i].seq)/sizeof(Page); + TraceEvent("FoundPhysicalLocation", dbgid) + .detail("PageIndex", i) + .detail("PageLocation", page) + .detail("RecoveryFirstPagesSize", recoveryFirstPages.size()) + .detail("SizeofPage", sizeof(Page)) + .detail("PageSequence", p[i].seq) + .detail("Location", loc) + .detail("Context", context) + .detail("file0name", rawQueue->files[0].dbgFilename); + ok = true; + break; + } + if (!ok) + TraceEvent(SevError, "DiskQueueLocationError", dbgid) + .detail("RecoveryFirstPages", recoveryFirstPages.size()) + .detail("Page0Valid", p[0].checkHash()) + .detail("Page0Seq", p[0].seq) + .detail("Page1Valid", p[1].checkHash()) + .detail("Page1Seq", p[1].seq) + .detail("Location", loc) + .detail("Context", context) + .detail("file0name", rawQueue->files[0].dbgFilename); + ASSERT( ok ); + } + + // isValid(firstPage) == compare(firstPage, firstPage) + // isValid(otherPage) == compare(firstPage, otherPage) + // Swap file1, file2 if comparePages( file2.firstPage, file1.firstPage ) + static bool comparePages( void* v1, void* v2 ) { + Page* p1 = (Page*)v1; Page* p2 = (Page*)v2; + return p2->checkHash() && (p2->seq >= p1->seq || !p1->checkHash()); + } + + RawDiskQueue_TwoFiles *rawQueue; + UID dbgid; + + bool anyPopped; // pop() has been called since the most recent commit() + bool warnAlwaysForMemory; + loc_t nextPageSeq, poppedSeq; + loc_t lastPoppedSeq; // poppedSeq the last time commit was called + loc_t lastCommittedSeq; + + // Buffer of pushed pages that haven't been committed. The last one (backPage()) is still mutable. + StringBuffer* pushed_page_buffer; + Page& backPage() { + ASSERT( pushedPageCount() ); + return ((Page*)pushed_page_buffer->ref().end())[-1]; + } + Page const& backPage() const { return ((Page*)pushed_page_buffer->ref().end())[-1]; } + int pushedPageCount() const { return pushed_page_buffer ? pushed_page_buffer->size() / sizeof(Page) : 0; } + + // Recovery state + bool recovered; + loc_t nextReadLocation; + Arena readBufArena; + Page* readBufPage; + int readBufPos; + Standalone recoveryFirstPages; +}; + +//A class wrapping DiskQueue which durably allows uncommitted data to be popped +//This works by performing two commits when uncommitted data is popped: +// Commit 1 - pop only previously committed data and push new data +// Commit 2 - finish pop into uncommitted data +class DiskQueue_PopUncommitted : public IDiskQueue { + +public: + DiskQueue_PopUncommitted( std::string basename, UID dbgid ) : queue(new DiskQueue(basename, dbgid)), pushed(0), popped(0), committed(0) { }; + + //IClosable + Future getError() { return queue->getError(); } + Future onClosed() { return queue->onClosed(); } + void dispose() { queue->dispose(); delete this; } + void close() { queue->close(); delete this; } + + //IDiskQueue + Future> readNext( int bytes ) { return readNext(this, bytes); } + + virtual location getNextReadLocation() { return queue->getNextReadLocation(); } + + virtual location push( StringRef contents ) { + pushed = queue->push(contents); + return pushed; + } + + virtual void pop( location upTo ) { + popped = std::max(popped, upTo); + ASSERT_WE_THINK(committed >= popped); + queue->pop(std::min(committed, popped)); + } + + virtual int getCommitOverhead() { + return queue->getCommitOverhead() + (popped > committed ? queue->getMaxPayload() : 0); + } + + Future commit() { + location pushLocation = pushed; + location popLocation = popped; + + Future commitFuture = queue->commit(); + + bool updatePop = popLocation > committed; + committed = pushLocation; + + if(updatePop) { + ASSERT_WE_THINK(false); + ASSERT(popLocation <= committed); + + queue->stall(); // Don't permit this pipelined commit to write anything to disk until the previous commit is totally finished + pop(popLocation); + commitFuture = commitFuture && queue->commit(); + } + else + TEST(true); //No uncommitted data was popped + + return commitFuture; + } + + virtual StorageBytes getStorageBytes() { return queue->getStorageBytes(); } + +private: + DiskQueue *queue; + location pushed; + location popped; + location committed; + + ACTOR static Future> readNext( DiskQueue_PopUncommitted *self, int bytes ) { + Standalone str = wait(self->queue->readNext(bytes)); + if(str.size() < bytes) + self->pushed = self->getNextReadLocation(); + + return str; + } +}; + +IDiskQueue* openDiskQueue( std::string basename, UID dbgid ) { + return new DiskQueue_PopUncommitted( basename, dbgid ); +} diff --git a/fdbserver/IDiskQueue.h b/fdbserver/IDiskQueue.h new file mode 100644 index 0000000000..c2ef1afa9b --- /dev/null +++ b/fdbserver/IDiskQueue.h @@ -0,0 +1,60 @@ +/* + * IDiskQueue.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_IDISKQUEUE_H +#define FDBSERVER_IDISKQUEUE_H +#pragma once + +#include "fdbclient/FDBTypes.h" +#include "IKeyValueStore.h" + +class IDiskQueue : public IClosable { +public: + struct location { + int64_t hi, lo; + location() : hi(0), lo(0) {} + location(int64_t lo) : hi(0), lo(lo) {} + location(int64_t hi, int64_t lo) : hi(hi), lo(lo) {} + operator std::string() { return format("%lld.%lld", hi, lo); } // FIXME: Return a 'HumanReadableDescription' instead of std::string, make TraceEvent::detail accept that (for safety) + + bool operator < (location const& r) const { + if (hir.hi) return false; + return lo < r.lo; + } + }; + + // Before calling push or commit, the caller *must* perform recovery by calling readNext() until it returns less than the requested number of bytes. + // Thereafter it may not be called again. + virtual Future> readNext( int bytes ) = 0; // Return the next bytes in the queue (beginning, the first time called, with the first unpopped byte) + virtual location getNextReadLocation() = 0; // Returns a location >= the location of all bytes previously returned by readNext(), and <= the location of all bytes subsequently returned + + virtual location push( StringRef contents ) = 0; // Appends the given bytes to the byte stream. Returns a location token representing the *end* of the contents. + virtual void pop( location upTo ) = 0; // Removes all bytes before the given location token from the byte stream. + virtual Future commit() = 0; // returns when all prior pushes and pops are durable. If commit does not return (due to close or a crash), any prefix of the pushed bytes and any prefix of the popped bytes may be durable. + + virtual int getCommitOverhead() = 0; // returns the amount of unused space that would be written by a commit that immediately followed this call + + virtual StorageBytes getStorageBytes() = 0; +}; + +IDiskQueue* openDiskQueue( std::string basename, UID dbgid ); // opens basename+"0.fdq" and basename+"1.fdq" + +#endif diff --git a/fdbserver/IKeyValueStore.h b/fdbserver/IKeyValueStore.h new file mode 100644 index 0000000000..be6a9d2f44 --- /dev/null +++ b/fdbserver/IKeyValueStore.h @@ -0,0 +1,96 @@ +/* + * IKeyValueStore.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_IKEYVALUESTORE_H +#define FDBSERVER_IKEYVALUESTORE_H +#pragma once + +#include "fdbclient/FDBTypes.h" + +class IClosable { +public: + // IClosable is a base interface for any disk-backed data structure that needs to support asynchronous errors, shutdown and deletion + + virtual Future getError() = 0; // asynchronously throws an error if there is an internal error. Never set inside (on the stack of) a call to another API function on this object. + virtual Future onClosed() = 0; // the future is set to Void when this is totally shut down after dispose() or close(). But this function cannot be called after dispose or close! + virtual void dispose() = 0; // permanently delete the data AND invalidate this interface + virtual void close() = 0; // invalidate this interface, but do not delete the data. Outstanding operations may or may not take effect in the background. +}; + +class IKeyValueStore : public IClosable { +public: + virtual KeyValueStoreType getType() = 0; + virtual void set( KeyValueRef keyValue, const Arena* arena = NULL ) = 0; + virtual void clear( KeyRangeRef range, const Arena* arena = NULL ) = 0; + virtual Future commit(bool sequential = false) = 0; // returns when prior sets and clears are (atomically) durable + + virtual Future> readValue( KeyRef key, Optional debugID = Optional() ) = 0; + + // Like readValue(), but returns only the first maxLength bytes of the value if it is longer + virtual Future> readValuePrefix( KeyRef key, int maxLength, Optional debugID = Optional() ) = 0; + + // If rowLimit>=0, reads first rows sorted ascending, otherwise reads last rows sorted descending + // The total size of the returned value (less the last entry) will be less than byteLimit + virtual Future>> readRange( KeyRangeRef keys, int rowLimit = 1<<30, int byteLimit = 1<<30 ) = 0; + + //Returns the amount of free and total space for this store, in bytes + virtual StorageBytes getStorageBytes() = 0; + + virtual void resyncLog() {} + + virtual void enableSnapshot() {} + /* + Concurrency contract + Causal consistency: + A read which begins after a commit ends sees the effects of the commit. + A read which ends before a commit begins does not see the effects of the commit. + + Thus, a read returns a version as of a call to commit which began before the read ends such that no subsequent commit ended before the read begins: + + commit() // can't be this version (subsequent commit ends before read begins) + endcommit() + commit() // could be this or any later version (no subsequent commit ends before read begins) + endcommit() + commit() + read() + */ +protected: + virtual ~IKeyValueStore() {} +}; + +extern IKeyValueStore* keyValueStoreSQLite( std::string const& filename, UID logID, KeyValueStoreType storeType, bool validateFile=false ); +extern IKeyValueStore* keyValueStoreMemory( std::string const& basename, UID logID, int64_t memoryLimit ); +extern IKeyValueStore* keyValueStoreLogSystem( class IDiskQueue* queue, UID logID, int64_t memoryLimit, bool disableSnapshot ); + +inline IKeyValueStore* openKVStore( KeyValueStoreType storeType, std::string const& filename, UID logID, int64_t memoryLimit, bool validateFile=false ) { + switch( storeType ) { + case KeyValueStoreType::SSD_BTREE_V1: + return keyValueStoreSQLite( filename, logID, KeyValueStoreType::SSD_BTREE_V1); + case KeyValueStoreType::SSD_BTREE_V2: + return keyValueStoreSQLite(filename, logID, KeyValueStoreType::SSD_BTREE_V2, validateFile); + case KeyValueStoreType::MEMORY: + return keyValueStoreMemory( filename, logID, memoryLimit ); + default: + UNREACHABLE(); + } + UNREACHABLE(); // FIXME: is this right? +} + +#endif \ No newline at end of file diff --git a/fdbserver/KeyValueStoreCompressTestData.actor.cpp b/fdbserver/KeyValueStoreCompressTestData.actor.cpp new file mode 100644 index 0000000000..47a5a78661 --- /dev/null +++ b/fdbserver/KeyValueStoreCompressTestData.actor.cpp @@ -0,0 +1,131 @@ +/* + * KeyValueStoreCompressTestData.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "IKeyValueStore.h" + +// KeyValueStoreCompressTestData wraps an existing IKeyValueStore and +// implements the following rudimentary compression scheme: +// An arbitrarily long value which consists entirely of a single repeated nonzero byte is mapped to +// a 5-byte value consisting of that byte followed by a little-endian integer giving the number +// of repetitions. +// All other values are mapped to a zero byte followed by the value. +// This store is used in testing to let us simulate having much bigger disks than we actually +// have, in order to test really big databases. + +struct KeyValueStoreCompressTestData : IKeyValueStore { + IKeyValueStore* store; + + KeyValueStoreCompressTestData(IKeyValueStore* store) : store(store) {} + + virtual Future getError() { return store->getError(); } + virtual Future onClosed() { return store->onClosed(); } + virtual void dispose() { store->dispose(); delete this; } + virtual void close() { store->close(); delete this; } + + virtual KeyValueStoreType getType() { return store->getType(); } + virtual StorageBytes getStorageBytes() { return store->getStorageBytes(); } + + virtual void set( KeyValueRef keyValue, const Arena* arena = NULL ) { + store->set( KeyValueRef( keyValue.key, pack(keyValue.value) ), arena ); + } + virtual void clear( KeyRangeRef range, const Arena* arena = NULL ) { store->clear( range, arena ); } + virtual Future commit(bool sequential = false) { return store->commit(sequential); } + + virtual Future> readValue( KeyRef key, Optional debugID = Optional() ) { + return doReadValue(store, key, debugID); + } + ACTOR static Future> doReadValue( IKeyValueStore* store, Key key, Optional debugID ) { + Optional v = wait( store->readValue(key, debugID) ); + if (!v.present()) return v; + return unpack(v.get()); + } + + // Note that readValuePrefix doesn't do anything in this implementation of IKeyValueStore, so the "atomic bomb" problem is still + // present if you are using this storage interface, but this storage interface is not used by customers ever. However, if you want + // to try to test malicious atomic op workloads with compressed values for some reason, you will need to fix this. + virtual Future> readValuePrefix( KeyRef key, int maxLength, Optional debugID = Optional() ) { + return doReadValuePrefix( store, key, maxLength, debugID ); + } + ACTOR static Future> doReadValuePrefix( IKeyValueStore* store, Key key, int maxLength, Optional debugID ) { + Optional v = wait( doReadValue(store, key, debugID) ); + if (!v.present()) return v; + if (maxLength < v.get().size()) { + return v.get().substr(0, maxLength); + } + else { + return v; + } + } + + // If rowLimit>=0, reads first rows sorted ascending, otherwise reads last rows sorted descending + // The total size of the returned value (less the last entry) will be less than byteLimit + virtual Future>> readRange( KeyRangeRef keys, int rowLimit = 1<<30, int byteLimit = 1<<30 ) { + return doReadRange(store, keys, rowLimit, byteLimit); + } + ACTOR Future>> doReadRange( IKeyValueStore* store, KeyRangeRef keys, int rowLimit, int byteLimit ) { + Standalone> _vs = wait( store->readRange(keys, rowLimit, byteLimit) ); + Standalone> vs = _vs; // Get rid of implicit const& from wait statement + Arena& a = vs.arena(); + for(int i=0; i::Node, for a total size of 72 bytes. +struct KeyValueMapPair { + Arena arena; //8 Bytes (excluding arena memory) + KeyRef key; //12 Bytes + ValueRef value; //12 Bytes + + void operator= ( KeyValueMapPair const& rhs ) { arena = rhs.arena; key = rhs.key; value = rhs.value; } + KeyValueMapPair( KeyValueMapPair const& rhs ) : arena(rhs.arena), key(rhs.key), value(rhs.value) {} + + KeyValueMapPair(KeyRef key, ValueRef value) : arena(key.expectedSize() + value.expectedSize()), key(arena, key), value(arena, value) { } + + bool operator<(KeyValueMapPair const& r) const { return key < r.key; } + bool operator==(KeyValueMapPair const& r) const { return key == r.key; } + bool operator!=(KeyValueMapPair const& r) const { return key != r.key; } +}; + +template +bool operator<(KeyValueMapPair const& l, CompatibleWithKey const& r) { return l.key < r; } + +template +bool operator<(CompatibleWithKey const& l, KeyValueMapPair const& r) { return l < r.key; } + +extern bool noUnseed; + +class KeyValueStoreMemory : public IKeyValueStore, NonCopyable { +public: + KeyValueStoreMemory( IDiskQueue* log, UID id, int64_t memoryLimit, bool disableSnapshot ); + + // IClosable + virtual Future getError() { return log->getError(); } + virtual Future onClosed() { return log->onClosed(); } + virtual void dispose() { recovering.cancel(); log->dispose(); delete this; } + virtual void close() { recovering.cancel(); log->close(); delete this; } + + // IKeyValueStore + virtual KeyValueStoreType getType() { return KeyValueStoreType::MEMORY; } + + int64_t getAvailableSize() { + int64_t residentSize = + data.sumTo(data.end()) + + queue.totalSize() + // doesn't account for overhead in queue + transactionSize; + + return memoryLimit - residentSize; + } + + virtual StorageBytes getStorageBytes() { + StorageBytes diskQueueBytes = log->getStorageBytes(); + + // Try to bound how many in-memory bytes we might need to write to disk if we commit() now + int64_t uncommittedBytes = queue.totalSize() + transactionSize; + + //Check that we have enough space in memory and on disk + int64_t availableSize = std::min(getAvailableSize(), diskQueueBytes.free / 4 - uncommittedBytes); + int64_t totalSize = std::min(memoryLimit, diskQueueBytes.total / 4 - uncommittedBytes); + + return StorageBytes(std::max((int64_t)0, availableSize), std::max((int64_t)0, totalSize), diskQueueBytes.used, diskQueueBytes.available); + } + + void semiCommit() { + transactionSize += queue.totalSize(); + if(transactionSize > 0.5 * committedDataSize) { + transactionIsLarge = true; + TraceEvent("KVSMemSwitchingToLargeTransactionMode", id).detail("TransactionSize", transactionSize).detail("DataSize", committedDataSize); + TEST(true); // KeyValueStoreMemory switching to large transaction mode + TEST(committedDataSize > 1e3); // KeyValueStoreMemory switching to large transaction mode with committed data + } + + int64_t bytesWritten = commit_queue(queue, true); + committedWriteBytes += bytesWritten; + } + + virtual void set(KeyValueRef keyValue, const Arena* arena) { + //A commit that occurs with no available space returns Never, so we can throw out all modifications + if(getAvailableSize() <= 0) + return; + + if(transactionIsLarge) { + KeyValueMapPair pair(keyValue.key, keyValue.value); + data.insert(pair, pair.arena.getSize() + data.getElementBytes()); + } + else { + queue.set(keyValue, arena); + if(recovering.isReady() && !disableSnapshot) { + semiCommit(); + } + } + } + + virtual void clear(KeyRangeRef range, const Arena* arena) { + //A commit that occurs with no available space returns Never, so we can throw out all modifications + if(getAvailableSize() <= 0) + return; + + if(transactionIsLarge) { + data.erase(data.lower_bound(range.begin), data.lower_bound(range.end)); + } + else { + queue.clear(range, arena); + if(recovering.isReady() && !disableSnapshot) { + semiCommit(); + } + } + } + + virtual Future commit(bool sequential) { + if(getAvailableSize() <= 0) { + if(g_network->isSimulated()) { //FIXME: known bug in simulation we are supressing + int unseed = noUnseed ? 0 : g_random->randomInt(0, 100001); + TraceEvent(SevWarnAlways, "KeyValueStoreMemory_OutOfSpace", id); + TraceEvent("ElapsedTime").detail("SimTime", now()).detail("RealTime", 0) + .detail("RandomUnseed", unseed); + flushAndExit(0); + } + TraceEvent(SevError, "KeyValueStoreMemory_OutOfSpace", id); + return Never(); + } + + if(recovering.isError()) throw recovering.getError(); + if(!recovering.isReady()) + return waitAndCommit(this, sequential); + + if(transactionIsLarge) { + fullSnapshot(data); + resetSnapshot = true; + committedWriteBytes = notifiedCommittedWriteBytes.get(); + } + else { + int64_t bytesWritten = commit_queue(queue, !disableSnapshot, sequential); + if(!disableSnapshot) { + committedWriteBytes += bytesWritten + OP_DISK_OVERHEAD; //OP_DISK_OVERHEAD is for the following log_op(OpCommit) + } + + //If there have been no mutations since the last commit, do nothing + if( notifiedCommittedWriteBytes.get() == committedWriteBytes ) + return Void(); + + notifiedCommittedWriteBytes.set(committedWriteBytes); + } + + if(disableSnapshot) { + return Void(); + } + + log_op(OpCommit, StringRef(), StringRef()); + if(!transactionIsLarge) { + committedWriteBytes += log->getCommitOverhead(); + } + + auto c = log->commit(); + + committedDataSize = data.sumTo(data.end()); + transactionSize = 0; + transactionIsLarge = false; + + addActor.send( commitAndUpdateVersions( this, c, previousSnapshotEnd ) ); + return c; + } + + virtual Future> readValue( KeyRef key, Optional debugID = Optional() ) { + if(recovering.isError()) throw recovering.getError(); + if (!recovering.isReady()) return waitAndReadValue(this, key); + + auto it = data.find(key); + if (it == data.end()) return Optional(); + return Optional(it->value); + } + + virtual Future> readValuePrefix( KeyRef key, int maxLength, Optional debugID = Optional() ) { + if(recovering.isError()) throw recovering.getError(); + if (!recovering.isReady()) return waitAndReadValuePrefix(this, key, maxLength); + + auto it = data.find(key); + if (it == data.end()) return Optional(); + auto val = it->value; + if(maxLength < val.size()) { + return Optional(val.substr(0, maxLength)); + } + else { + return Optional(val); + } + } + + // If rowLimit>=0, reads first rows sorted ascending, otherwise reads last rows sorted descending + // The total size of the returned value (less the last entry) will be less than byteLimit + virtual Future>> readRange( KeyRangeRef keys, int rowLimit = 1<<30, int byteLimit = 1<<30 ) { + if(recovering.isError()) throw recovering.getError(); + if (!recovering.isReady()) return waitAndReadRange(this, keys, rowLimit, byteLimit); + + Standalone> result; + if (rowLimit >= 0) { + auto it = data.lower_bound(keys.begin); + while (it!=data.end() && it->key < keys.end && rowLimit && byteLimit>=0) { + byteLimit -= sizeof(KeyValueRef) + it->key.size() + it->value.size(); + result.push_back_deep( result.arena(), KeyValueRef(it->key, it->value) ); + ++it; + --rowLimit; + } + } else { + rowLimit = -rowLimit; + auto it = data.previous( data.lower_bound(keys.end) ); + while (it!=data.end() && it->key >= keys.begin && rowLimit && byteLimit>=0) { + byteLimit -= sizeof(KeyValueRef) + it->key.size() + it->value.size(); + result.push_back_deep( result.arena(), KeyValueRef(it->key, it->value) ); + it = data.previous(it); + --rowLimit; + } + } + return result; + } + + virtual void resyncLog() { + ASSERT( recovering.isReady() ); + resetSnapshot = true; + log_op(OpSnapshotAbort, StringRef(), StringRef()); + } + + virtual void enableSnapshot() { + disableSnapshot = false; + } + +private: + enum OpType { + OpSet, + OpClear, + OpClearToEnd, + OpSnapshotItem, + OpSnapshotEnd, + OpSnapshotAbort, // terminate an in progress snapshot in order to start a full snapshot + OpCommit, // only in log, not in queue + OpRollback // only in log, not in queue + }; + + struct OpRef { + OpType op; + StringRef p1, p2; + OpRef() {} + OpRef(Arena& a, OpRef const& o) : op(o.op), p1(a,o.p1), p2(a,o.p2) {} + size_t expectedSize() { + return p1.expectedSize() + p2.expectedSize(); + } + }; + struct OpHeader { + int op; + int len1, len2; + }; + + struct OpQueue { + OpQueue() : numBytes(0) { } + + int totalSize() const { return numBytes; } + + void clear() { + numBytes = 0; + operations = Standalone>(); + arenas.clear(); + } + + void rollback() { + clear(); + } + + void set( KeyValueRef keyValue, const Arena* arena = NULL ) { + queue_op(OpSet, keyValue.key, keyValue.value, arena); + } + + void clear( KeyRangeRef range, const Arena* arena = NULL ) { + queue_op(OpClear, range.begin, range.end, arena); + } + + void clear_to_end( StringRef fromKey, const Arena* arena = NULL ) { + queue_op(OpClearToEnd, fromKey, StringRef(), arena); + } + + void queue_op( OpType op, StringRef p1, StringRef p2, const Arena* arena ) { + numBytes += p1.size() + p2.size() + sizeof(OpHeader) + sizeof(OpRef); + + OpRef r; r.op = op; r.p1 = p1; r.p2 = p2; + if(arena == NULL) { + operations.push_back_deep( operations.arena(), r ); + } else { + operations.push_back( operations.arena(), r ); + arenas.push_back(*arena); + } + } + + const OpRef* begin() { + return operations.begin(); + } + + const OpRef* end() { + return operations.end(); + } + + private: + Standalone> operations; + uint64_t numBytes; + std::vector arenas; + }; + + UID id; + + IndexedSet< KeyValueMapPair, uint64_t > data; + + OpQueue queue; // mutations not yet commit()ted + IDiskQueue *log; + Future recovering, snapshotting; + int64_t committedWriteBytes; + NotifiedVersion notifiedCommittedWriteBytes; + Key recoveredSnapshotKey; // After recovery, the next key in the currently uncompleted snapshot + IDiskQueue::location currentSnapshotEnd; //The end of the most recently completed snapshot (this snapshot cannot be discarded) + IDiskQueue::location previousSnapshotEnd; //The end of the second most recently completed snapshot (on commit, this snapshot can be discarded) + PromiseStream> addActor; + Future commitActors; + + int64_t committedDataSize; + int64_t transactionSize; + bool transactionIsLarge; + + bool resetSnapshot; //Set to true after a fullSnapshot is performed. This causes the regular snapshot mechanism to restart + bool disableSnapshot; + + int64_t memoryLimit; //The upper limit on the memory used by the store (excluding, possibly, some clear operations) + std::vector> dataSets; + + int64_t commit_queue(OpQueue &ops, bool log, bool sequential = false) { + int64_t total = 0, count = 0; + IDiskQueue::location log_location = 0; + + for(auto o = ops.begin(); o != ops.end(); ++o) { + ++count; + total += o->p1.size() + o->p2.size() + OP_DISK_OVERHEAD; + if (o->op == OpSet) { + KeyValueMapPair pair(o->p1, o->p2); + if(sequential) { + dataSets.push_back(std::make_pair(pair, pair.arena.getSize() + data.getElementBytes())); + } else { + data.insert( pair, pair.arena.getSize() + data.getElementBytes() ); + } + } + else if (o->op == OpClear) { + if(sequential) { + data.insert(dataSets); + dataSets.clear(); + } + data.erase( data.lower_bound(o->p1), data.lower_bound(o->p2) ); + } + else if (o->op == OpClearToEnd) { + if(sequential) { + data.insert(dataSets); + dataSets.clear(); + } + data.erase( data.lower_bound(o->p1), data.end() ); + } + else ASSERT(false); + if ( log ) + log_location = log_op( o->op, o->p1, o->p2 ); + } + if(sequential) { + data.insert(dataSets); + dataSets.clear(); + } + + bool ok = count < 1e6; + if( !ok ) { + TraceEvent(/*ok ? SevInfo : */SevWarnAlways, "KVSMemCommit_queue", id) + .detail("bytes", total) + .detail("log", log) + .detail("ops", count) + .detail("LastLoggedLocation", log_location) + .detail("Details", count); + } + + ops.clear(); + return total; + } + + IDiskQueue::location log_op(OpType op, StringRef v1, StringRef v2) { + OpHeader h = {(int)op, v1.size(), v2.size()}; + log->push( StringRef((const uint8_t*)&h, sizeof(h)) ); + log->push( v1 ); + log->push( v2 ); + return log->push( LiteralStringRef("\x01") ); // Changes here should be reflected in OP_DISK_OVERHEAD + } + + ACTOR static Future recover( KeyValueStoreMemory* self ) { + // 'uncommitted' variables track something that might be rolled back by an OpRollback, and are copied into permanent variables + // (in self) in OpCommit. OpRollback does the reverse (copying the permanent versions over the uncommitted versions) + // the uncommitted and committed variables should be equal initially (to whatever makes sense if there are no committed transactions recovered) + state Key uncommittedNextKey = self->recoveredSnapshotKey; + state IDiskQueue::location uncommittedPrevSnapshotEnd = self->previousSnapshotEnd = self->log->getNextReadLocation(); // not really, but popping up to here does nothing + state IDiskQueue::location uncommittedSnapshotEnd = self->currentSnapshotEnd = uncommittedPrevSnapshotEnd; + + state int zeroFillSize = 0; + state int dbgSnapshotItemCount=0; + state int dbgSnapshotEndCount=0; + state int dbgMutationCount=0; + state int dbgCommitCount=0; + state double startt = now(); + state UID dbgid = self->id; + + state Future loggingDelay = delay(1.0); + + state OpQueue recoveryQueue; + state OpHeader h; + + TraceEvent("KVSMemRecoveryStarted", self->id) + .detail("SnapshotEndLocation", uncommittedSnapshotEnd); + + try { + loop { + Standalone data = wait( self->log->readNext( sizeof(OpHeader) ) ); + if (data.size() != sizeof(OpHeader)) { + if (data.size()) { + TEST(true); // zero fill partial header in KeyValueStoreMemory + memset(&h, 0, sizeof(OpHeader)); + memcpy(&h, data.begin(), data.size()); + zeroFillSize = sizeof(OpHeader)-data.size() + h.len1 + h.len2 + 1; + } + TraceEvent("KVSMemRecoveryComplete", self->id) + .detail("Reason", "Non-header sized data read") + .detail("DataSize", data.size()) + .detail("ZeroFillSize", zeroFillSize) + .detail("SnapshotEndLocation", uncommittedSnapshotEnd) + .detail("NextReadLoc", self->log->getNextReadLocation()); + break; + } + h = *(OpHeader*)data.begin(); + Standalone data = wait( self->log->readNext( h.len1 + h.len2+1 ) ); + if (data.size() != h.len1 + h.len2 + 1) { + zeroFillSize = h.len1 + h.len2 + 1 - data.size(); + TraceEvent("KVSMemRecoveryComplete", self->id) + .detail("Reason", "data specified by header does not exist") + .detail("DataSize", data.size()) + .detail("ZeroFillSize", zeroFillSize) + .detail("SnapshotEndLocation", uncommittedSnapshotEnd) + .detail("OpCode", h.op) + .detail("NextReadLoc", self->log->getNextReadLocation()); + break; + } + + if (data[data.size()-1]) { + StringRef p1 = data.substr(0, h.len1); + StringRef p2 = data.substr(h.len1, h.len2); + + if (h.op == OpSnapshotItem) { // snapshot data item + /*if (p1 < uncommittedNextKey) { + TraceEvent(SevError, "RecSnapshotBack", self->id) + .detail("nextKey", printable(uncommittedNextKey)) + .detail("p1", printable(p1)) + .detail("nextlocation", self->log->getNextReadLocation()); + } + ASSERT( p1 >= uncommittedNextKey );*/ + if( p1 >= uncommittedNextKey ) + recoveryQueue.clear( KeyRangeRef(uncommittedNextKey, p1), &uncommittedNextKey.arena() ); //FIXME: Not sure what this line is for, is it necessary? + recoveryQueue.set( KeyValueRef(p1, p2), &data.arena() ); + uncommittedNextKey = keyAfter(p1); + ++dbgSnapshotItemCount; + } else if (h.op == OpSnapshotEnd || h.op == OpSnapshotAbort) { // snapshot complete + TraceEvent("RecSnapshotEnd", self->id) + .detail("nextKey", printable(uncommittedNextKey)) + .detail("nextlocation", self->log->getNextReadLocation()) + .detail("isSnapshotEnd", h.op == OpSnapshotEnd); + + if(h.op == OpSnapshotEnd) { + uncommittedPrevSnapshotEnd = uncommittedSnapshotEnd; + uncommittedSnapshotEnd = self->log->getNextReadLocation(); + recoveryQueue.clear_to_end( uncommittedNextKey, &uncommittedNextKey.arena() ); + } + + uncommittedNextKey = Key(); + ++dbgSnapshotEndCount; + } else if (h.op == OpSet) { // set mutation + recoveryQueue.set( KeyValueRef(p1,p2), &data.arena() ); + ++dbgMutationCount; + } else if (h.op == OpClear) { // clear mutation + recoveryQueue.clear( KeyRangeRef(p1,p2), &data.arena() ); + ++dbgMutationCount; + } else if (h.op == OpClearToEnd) { //clear all data from begin key to end + recoveryQueue.clear_to_end( p1, &data.arena() ); + } else if (h.op == OpCommit) { // commit previous transaction + self->commit_queue(recoveryQueue, false); + ++dbgCommitCount; + self->recoveredSnapshotKey = uncommittedNextKey; + self->previousSnapshotEnd = uncommittedPrevSnapshotEnd; + self->currentSnapshotEnd = uncommittedSnapshotEnd; + } else if (h.op == OpRollback) { // rollback previous transaction + recoveryQueue.rollback(); + TraceEvent("KVSMemRecSnapshotRollback", self->id) + .detail("nextKey", printable(uncommittedNextKey)); + uncommittedNextKey = self->recoveredSnapshotKey; + uncommittedPrevSnapshotEnd = self->previousSnapshotEnd; + uncommittedSnapshotEnd = self->currentSnapshotEnd; + } else + ASSERT(false); + } else { + TraceEvent("KVSMemRecoverySkippedZeroFill", self->id) + .detail("PayloadSize", data.size()) + .detail("ExpectedSize", h.len1 + h.len2 + 1) + .detail("OpCode", h.op) + .detail("EndsAt", self->log->getNextReadLocation()); + } + + if (loggingDelay.isReady()) { + TraceEvent("KVSMemRecoveryLogSnap", self->id) + .detail("SnapshotItems", dbgSnapshotItemCount) + .detail("SnapshotEnd", dbgSnapshotEndCount) + .detail("Mutations", dbgMutationCount) + .detail("Commits", dbgCommitCount) + .detail("EndsAt", self->log->getNextReadLocation()); + loggingDelay = delay(1.0); + } + + Void _ = wait( yield() ); + } + + if (zeroFillSize) { + TEST( true ); // Fixing a partial commit at the end of the KeyValueStoreMemory log + for(int i=0; ilog->push( StringRef((const uint8_t*)"",1) ); + } + //self->rollback(); not needed, since we are about to discard anything left in the recoveryQueue + //TraceEvent("KVSMemRecRollback", self->id).detail("QueueEmpty", data.size() == 0); + // make sure that before any new operations are added to the log that all uncommitted operations are "rolled back" + self->log_op( OpRollback, StringRef(), StringRef() ); // rollback previous transaction + + self->committedDataSize = self->data.sumTo(self->data.end()); + + TraceEvent("KVSMemRecovered", self->id) + .detail("SnapshotItems", dbgSnapshotItemCount) + .detail("SnapshotEnd", dbgSnapshotEndCount) + .detail("Mutations", dbgMutationCount) + .detail("Commits", dbgCommitCount) + .detail("TimeTaken", now()-startt); + + self->semiCommit(); + return Void(); + } catch( Error &e ) { + bool ok = e.code() == error_code_operation_cancelled || e.code() == error_code_file_not_found; + TraceEvent(ok ? SevInfo : SevError, "ErrorDuringRecovery", dbgid).error(e, true); + throw e; + } + } + + //Snapshots an entire data set + void fullSnapshot( IndexedSet< KeyValueMapPair, uint64_t> &snapshotData ) { + previousSnapshotEnd = log_op(OpSnapshotAbort, StringRef(), StringRef()); + + //Clear everything since we are about to write the whole database + log_op(OpClearToEnd, allKeys.begin, StringRef()); + + int count = 0; + int64_t snapshotSize = 0; + for(auto kv = snapshotData.begin(); kv != snapshotData.end(); ++kv) { + log_op(OpSnapshotItem, kv->key, kv->value); + snapshotSize += kv->key.size() + kv->value.size() + OP_DISK_OVERHEAD; + ++count; + } + + TraceEvent("FullSnapshotEnd", id) + .detail("PreviousSnapshotEndLoc", previousSnapshotEnd) + .detail("SnapshotSize", snapshotSize) + .detail("SnapshotElements", count); + + currentSnapshotEnd = log_op(OpSnapshotEnd, StringRef(), StringRef()); + } + + ACTOR static Future snapshot( KeyValueStoreMemory* self ) { + Void _ = wait(self->recovering); + + state Key nextKey = self->recoveredSnapshotKey; + state bool nextKeyAfter = false; //setting this to true is equilvent to setting nextKey = keyAfter(nextKey) + state uint64_t snapshotTotalWrittenBytes = 0; + state int lastDiff = 0; + state int snapItems = 0; + state uint64_t snapshotBytes = 0; + + TraceEvent("KVSMemStartingSnapshot", self->id).detail("StartKey", printable(nextKey)); + + loop { + Void _ = wait( self->notifiedCommittedWriteBytes.whenAtLeast( snapshotTotalWrittenBytes + 1 ) ); + + if(self->resetSnapshot) { + nextKey = Key(); + nextKeyAfter = false; + snapItems = 0; + snapshotBytes = 0; + self->resetSnapshot = false; + } + + auto next = nextKeyAfter ? self->data.upper_bound(nextKey) : self->data.lower_bound(nextKey); + int diff = self->notifiedCommittedWriteBytes.get() - snapshotTotalWrittenBytes; + if( diff > lastDiff && diff > 5e7 ) + TraceEvent(SevWarnAlways, "ManyWritesAtOnce", self->id) + .detail("CommittedWrites", self->notifiedCommittedWriteBytes.get()) + .detail("SnapshotWrites", snapshotTotalWrittenBytes) + .detail("Diff", diff) + .detail("LastOperationWasASnapshot", nextKey == Key() && !nextKeyAfter); + lastDiff = diff; + + if (next == self->data.end()) { + auto thisSnapshotEnd = self->log_op( OpSnapshotEnd, StringRef(), StringRef() ); + //TraceEvent("SnapshotEnd", self->id) + // .detail("lastKey", printable(lastKey.present() ? lastKey.get() : LiteralStringRef(""))) + // .detail("currentSnapshotEndLoc", self->currentSnapshotEnd) + // .detail("previousSnapshotEndLoc", self->previousSnapshotEnd) + // .detail("thisSnapshotEnd", thisSnapshotEnd) + // .detail("Items", snapItems) + // .detail("CommittedWrites", self->notifiedCommittedWriteBytes.get()) + // .detail("SnapshotSize", snapshotBytes); + + ASSERT(thisSnapshotEnd >= self->currentSnapshotEnd); + self->previousSnapshotEnd = self->currentSnapshotEnd; + self->currentSnapshotEnd = thisSnapshotEnd; + nextKey = Key(); + nextKeyAfter = false; + snapItems = 0; + + snapshotBytes = 0; + + snapshotTotalWrittenBytes += OP_DISK_OVERHEAD; + } else { + self->log_op( OpSnapshotItem, next->key, next->value ); + nextKey = next->key; + nextKeyAfter = true; + snapItems++; + uint64_t opBytes = next->key.size() + next->value.size() + OP_DISK_OVERHEAD; + snapshotBytes += opBytes; + snapshotTotalWrittenBytes += opBytes; + } + } + } + + ACTOR static Future> waitAndReadValue( KeyValueStoreMemory* self, Key key ) { + Void _ = wait( self->recovering ); + return self->readValue(key).get(); + } + ACTOR static Future> waitAndReadValuePrefix( KeyValueStoreMemory* self, Key key, int maxLength) { + Void _ = wait( self->recovering ); + return self->readValuePrefix(key, maxLength).get(); + } + ACTOR static Future>> waitAndReadRange( KeyValueStoreMemory* self, KeyRange keys, int rowLimit, int byteLimit ) { + Void _ = wait( self->recovering ); + return self->readRange(keys, rowLimit, byteLimit).get(); + } + ACTOR static Future waitAndCommit(KeyValueStoreMemory* self, bool sequential) { + Void _ = wait(self->recovering); + Void _ = wait(self->commit(sequential)); + return Void(); + } + ACTOR static Future commitAndUpdateVersions( KeyValueStoreMemory* self, Future commit, IDiskQueue::location location ) { + Void _ = wait( commit ); + self->log->pop(location); + return Void(); + } +}; + +KeyValueStoreMemory::KeyValueStoreMemory( IDiskQueue* log, UID id, int64_t memoryLimit, bool disableSnapshot ) + : log(log), id(id), previousSnapshotEnd(-1), currentSnapshotEnd(-1), + resetSnapshot(false), memoryLimit(memoryLimit), committedWriteBytes(0), + committedDataSize(0), transactionSize(0), transactionIsLarge(false), disableSnapshot(disableSnapshot) +{ + recovering = recover( this ); + snapshotting = snapshot( this ); + commitActors = actorCollection( addActor.getFuture() ); +} + +IKeyValueStore* keyValueStoreMemory( std::string const& basename, UID logID, int64_t memoryLimit ) { + TraceEvent("KVSMemOpening", logID).detail("Basename", basename).detail("MemoryLimit", memoryLimit); + IDiskQueue *log = openDiskQueue( basename, logID ); + return new KeyValueStoreMemory( log, logID, memoryLimit, false ); +} + +IKeyValueStore* keyValueStoreLogSystem( class IDiskQueue* queue, UID logID, int64_t memoryLimit, bool disableSnapshot ) { + return new KeyValueStoreMemory( queue, logID, memoryLimit, disableSnapshot ); +} \ No newline at end of file diff --git a/fdbserver/KeyValueStoreSQLite.actor.cpp b/fdbserver/KeyValueStoreSQLite.actor.cpp new file mode 100644 index 0000000000..e902a2a399 --- /dev/null +++ b/fdbserver/KeyValueStoreSQLite.actor.cpp @@ -0,0 +1,1971 @@ +/* + * KeyValueStoreSQLite.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#define SQLITE_THREADSAFE 0 // also in sqlite3.amalgamation.c! +#include "flow/actorcompiler.h" +#include "IKeyValueStore.h" +#include "CoroFlow.h" +#include "Knobs.h" +#include "flow/Hash3.h" + +extern "C" { +#include "sqlite/sqliteInt.h" +u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*); +} +#include "flow/ThreadPrimitives.h" +#include "template_fdb.h" +#include "fdbrpc/simulator.h" + +#if SQLITE_THREADSAFE == 0 + #define sqlite3_mutex_enter(x) + #define sqlite3_mutex_leave(x) +#endif + +void hexdump(FILE *fout, StringRef val); + +/*#undef state +#include */ + +/*uint64_t getFileSize( const char* filename ) { + HANDLE f = CreateFile( filename, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL); + if (f == INVALID_HANDLE_VALUE) return 0; + DWORD hi,lo; + lo = GetFileSize(f, &hi); + CloseHandle(f); + return (uint64_t(hi)<<32) + lo; +}*/ + +struct SpringCleaningStats { + int64_t springCleaningCount; + int64_t lazyDeletePages; + int64_t vacuumedPages; + double springCleaningTime; + double vacuumTime; + double lazyDeleteTime; + + SpringCleaningStats() : springCleaningCount(0), lazyDeletePages(0), vacuumedPages(0), springCleaningTime(0.0), vacuumTime(0.0), lazyDeleteTime(0.0) {} +}; + +struct PageChecksumCodec { + PageChecksumCodec(std::string const &filename, int verifyChecksumHistorySize) : pageSize(0), reserveSize(0), filename(filename), silent(false) { + checksumHistory.resize(verifyChecksumHistorySize); + } + + int pageSize; + int reserveSize; + std::string filename; + bool silent; + + struct SumType { + bool operator==(const SumType &rhs) const { return part1 == rhs.part1 && part2 == rhs.part2; } + uint32_t part1; + uint32_t part2; + std::string toString() { return format("0x%08x%08x", part1, part2); } + }; + std::vector> checksumHistory; + + // Calculates and then either stores or verifies a checksum. + // The checksum is read/stored at the end of the page buffer. + // Page size is passed in as pageLen because this->pageSize is not always appropriate. + // If write is true then the checksum is written into the page and true is returned. + // If write is false then the checksum is compared to the in-page sum and the return value + // is whether or not the checksums were equal. + bool checksum(Pgno pageNumber, void *data, int pageLen, bool write) { + ASSERT(pageLen > sizeof(SumType)); + + char *pData = (char *)data; + int dataLen = pageLen - sizeof(SumType); + SumType sum; + SumType *pSumInPage = (SumType *)(pData + dataLen); + + // Write sum directly to page or to sum variable based on mode + SumType *sumOut = write ? pSumInPage : ∑ + sumOut->part1 = pageNumber; //DO NOT CHANGE + sumOut->part2 = 0x5ca1ab1e; + hashlittle2(pData, dataLen, &sumOut->part1, &sumOut->part2); + + // Verify if not in write mode + if(!write && sum != *pSumInPage) { + if(!silent) + TraceEvent (SevError, "SQLitePageChecksumFailure") + .detail("CodecPageSize", pageSize) + .detail("CodecReserveSize", reserveSize) + .detail("Filename", filename) + .detail("PageNumber", pageNumber) + .detail("PageSize", pageLen) + .detail("ChecksumInPage", pSumInPage->toString()) + .detail("ChecksumCalculated", sum.toString()) + .error(checksum_failed()); + + return false; + } + + // Update or check sum in history if the history buffer isn't empty and if we're not in a simulated injected fault situation + if(!checksumHistory.empty() && + (!g_network->isSimulated() || (!g_simulator.getCurrentProcess()->fault_injection_p1 && !g_simulator.getCurrentProcess()->rebooting)) + ) { + auto &bucket = checksumHistory[pageNumber % checksumHistory.size()]; + if(write) { + // For writes, put this pagenumber and sum into the bucket + bucket.first = pageNumber; + bucket.second = *sumOut; + } + else { + // For reads, see if the bucket has the right page number, if so then verify sum + if(bucket.first == pageNumber && bucket.second != *pSumInPage) { + TraceEvent (SevError, "SQLitePageChecksumDetectedLostWrite") + .detail("CodecPageSize", pageSize) + .detail("CodecReserveSize", reserveSize) + .detail("Filename", filename) + .detail("PageNumber", pageNumber) + .detail("PageSize", pageLen) + .detail("ChecksumInPage", pSumInPage->toString()) + .detail("ChecksumHistory", bucket.second.toString()) + .error(checksum_failed()); + return false; + } + } + } + + return true; + } + + static void * codec(void *vpSelf, void *data, Pgno pageNumber, int op) { + PageChecksumCodec *self = (PageChecksumCodec *)vpSelf; + + // Page write operations are 6 for DB page and 7 for journal page + bool write = (op == 6 || op == 7); + // Page read is operation 3, which must be the operation if it's not a write. + ASSERT(write || op == 3); + + // Page 1 is special. It contains the database configuration including Page Size and Reserve Size. + // SQLite can't get authoritative values for these things until the Pager Codec has validated (and + // potentially decrypted) Page 1 itself, so it can't tell the Pager Codec what those things are before + // Page 1 is handled. It will guess a Page Size of SQLITE_DEFAULT_PAGE_SIZE, and a Reserve Size based + // on the pre-verified (and perhaps still encrypted) header in the Page 1 data that it will then pass + // to the Pager Codec. + // + // So, Page 1 must be written and verifiable as a SQLITE_DEFAULT_PAGE_SIZE sized page as well as + // the actual configured page size for the database, if it is larger. A configured page size lower + // than the default (in other words 512) results in undefined behavior. + if(pageNumber == 1) { + if(write && self->pageSize > SQLITE_DEFAULT_PAGE_SIZE) { + self->checksum(pageNumber, data, SQLITE_DEFAULT_PAGE_SIZE, write); + } + } + else { + // For Page Numbers other than 1, reserve size must be the size of the checksum. + if(self->reserveSize != sizeof(SumType)) { + if(!self->silent) + TraceEvent(SevWarnAlways, "SQLitePageChecksumFailureBadReserveSize") + .detail("CodecPageSize", self->pageSize) + .detail("CodecReserveSize", self->reserveSize) + .detail("Filename", self->filename) + .detail("PageNumber", pageNumber); + + return NULL; + } + } + + if(!self->checksum(pageNumber, data, self->pageSize, write)) + return NULL; + + return data; + } + + static void sizeChange(void *vpSelf, int new_pageSize, int new_reserveSize) { + PageChecksumCodec *self = (PageChecksumCodec *)vpSelf; + self->pageSize = new_pageSize; + self->reserveSize = new_reserveSize; + } + + static void free(void *vpSelf) { + PageChecksumCodec *self = (PageChecksumCodec *)vpSelf; + delete self; + } +}; + +struct SQLiteDB : NonCopyable { + std::string filename; + sqlite3* db; + Btree* btree; + int table, freetable; + bool haveMutex; + Reference dbFile, walFile; + bool page_checksums; + bool fragment_values; + PageChecksumCodec *pPagerCodec; // we do NOT own this pointer, db does. + + void beginTransaction(bool write) { + checkError("BtreeBeginTrans", sqlite3BtreeBeginTrans(btree, write)); + } + void endTransaction() { + checkError("BtreeCommit", sqlite3BtreeCommit(btree)); + } + void rollback() { + checkError("BtreeRollback", sqlite3BtreeRollback(btree)); + } + + void open(bool writable); + void createFromScratch(); + + SQLiteDB( std::string filename, bool page_checksums, bool fragment_values): filename(filename), db(NULL), btree(NULL), table(-1), freetable(-1), haveMutex(false), page_checksums(page_checksums), fragment_values(fragment_values) {} + + ~SQLiteDB() { + if (db) { + if (haveMutex) { + sqlite3_mutex_leave(db->mutex); + } + sqlite3_close( db ); + } + } + + void initPagerCodec() { + if(page_checksums) { + int r = sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, db, sizeof(PageChecksumCodec::SumType)); + if(r != 0) { + TraceEvent(SevError, "BtreePageReserveSizeSetError").detail("Filename", filename).detail("ErrorCode", r); + ASSERT(false); + } + // Always start with a new pager codec with default options. + pPagerCodec = new PageChecksumCodec(filename, SERVER_KNOBS->SQLITE_PAGER_CHECKSUM_HISTORY); + sqlite3BtreePagerSetCodec(btree, PageChecksumCodec::codec, PageChecksumCodec::sizeChange, PageChecksumCodec::free, pPagerCodec); + } + } + + void checkError( const char* context, int rc ) { + //if (g_random->random01() < .001) rc = SQLITE_INTERRUPT; + if (rc) { + // Our exceptions don't propagate through sqlite, so we don't know for sure if the error that caused this was + // an injected fault. Assume that if fault injection is happening, this is an injected fault. + Error err = io_error(); + if (g_network->isSimulated() && (g_simulator.getCurrentProcess()->fault_injection_p1 || g_simulator.getCurrentProcess()->rebooting)) + err = err.asInjectedFault(); + + if (db) + db->errCode = rc; + if (rc == SQLITE_NOMEM) platform::outOfMemory(); // SOMEDAY: Trap out of memory errors at allocation time; check out different allocation options in sqlite + + TraceEvent(SevError, "DiskError").error(err).detail("In", context).detail("File", filename).detail("SQLiteError", sqlite3ErrStr(rc)).detail("SQLiteErrorCode", rc).GetLastError(); + throw err; + } + } + void checkpoint( bool restart ) { + int logSize=0, checkpointCount=0; + //double t = timer(); + while (true) { + int rc = sqlite3_wal_checkpoint_v2(db, 0, restart ? SQLITE_CHECKPOINT_RESTART : SQLITE_CHECKPOINT_FULL, &logSize, &checkpointCount); + if (!rc) break; + if ((sqlite3_errcode(db)&0xff) == SQLITE_BUSY) { + //printf("#"); + //threadSleep(.010); + sqlite3_sleep(10); + } else + checkError("checkpoint", rc); + } + //printf("Checkpoint (%0.1f ms): %d frames in log, %d checkpointed\n", (timer()-t)*1000, logSize, checkpointCount); + } + uint32_t freePages() { + u32 fp = 0; + sqlite3BtreeGetMeta(btree, BTREE_FREE_PAGE_COUNT, &fp); + return fp; + } + bool vacuum() { // Returns true if vacuum is complete or stalled by a lazy free root + int rc = sqlite3BtreeIncrVacuum(btree); + if (rc && rc != SQLITE_DONE) checkError("vacuum", rc); + return rc == SQLITE_DONE; + } + void check(bool verbose) { + int errors = 0; + int tables[] = {1, table, freetable}; + TraceEvent("BTreeIntegrityCheckBegin").detail("Filename", filename); + char* e = sqlite3BtreeIntegrityCheck(btree, tables, 3, 100, &errors, verbose); + if (!(g_network->isSimulated() && (g_simulator.getCurrentProcess()->fault_injection_p1 || g_simulator.getCurrentProcess()->rebooting))) { + TraceEvent((errors||e) ? SevError : SevInfo, "BTreeIntegrityCheck").detail("filename", filename).detail("ErrorTotal", errors).detail("ErrorDetail", e ? e : ""); + TEST(true); // BTree integrity checked + } + if (e) sqlite3_free(e); + } + int checkAllPageChecksums(); +}; + +class Statement : NonCopyable { + SQLiteDB& db; + sqlite3_stmt *stmt; + +public: + Statement( SQLiteDB& db, const char* sql ) + : db(db), stmt(NULL) + { + db.checkError("prepare", sqlite3_prepare_v2( db.db, sql, -1, &stmt, NULL)); + } + ~Statement() { + try { + db.checkError("finalize", sqlite3_finalize( stmt )); + } catch (...) { + } + } + Statement& reset() { + db.checkError("reset", sqlite3_reset(stmt)); + return *this; + } + Statement& param(int i, StringRef value) { + db.checkError("bind", sqlite3_bind_blob( stmt, i, value.begin(), value.size(), SQLITE_STATIC )); + return *this; + } + Statement& param(int i, int value) { + db.checkError("bind", sqlite3_bind_int( stmt, i, value )); + return *this; + } + Statement& execute() { + int r = sqlite3_step( stmt ); + if (r == SQLITE_ROW) db.checkError("execute called on statement that returns rows", r); + if (r != SQLITE_DONE) db.checkError("execute", r); + return *this; + } + bool nextRow() { + int r = sqlite3_step( stmt ); + if (r == SQLITE_ROW) return true; + if (r == SQLITE_DONE) return false; + db.checkError( "nextRow", r ); + __assume(false); // NOT REACHED + } + StringRef column( int i ) { + return StringRef( + (const uint8_t*)sqlite3_column_blob(stmt, i), + sqlite3_column_bytes(stmt, i) ); + } +}; + +void hexdump(FILE *fout, StringRef val) { + int buflen = val.size(); + const unsigned char *buf = val.begin(); + int i, j; + for (i=0; i>= 8; + } + // An increment is required if the high bit of the N-byte index value is set, since it is + // positive number but SQLite only stores signed values and would interpret it as negative. + if(index >> (8 * indexCode - 1)) + ++indexCode; + + int header_size = sqlite3VarintLen(keyCode) + sizeof(indexCode) + sqlite3VarintLen(valCode); + int hh = sqlite3VarintLen(header_size); + header_size += hh; + if (hh < sqlite3VarintLen(header_size)) + header_size++; + int size = header_size + kv.key.size() + indexCode + kv.value.size(); + + Value v; + uint8_t* d = new (v.arena()) uint8_t[size]; + ((ValueRef&)v) = KeyRef(d, size); + d += sqlite3PutVarint( d, header_size ); + d += sqlite3PutVarint( d, keyCode ); + *d++ = indexCode; + d += sqlite3PutVarint( d, valCode ); + + // Write key + memcpy(d, kv.key.begin(), kv.key.size()); + d += kv.key.size(); + + // Write index bytes, if any + for(int i = indexCode - 1; i >= 0; --i) { + d[i] = (uint8_t)index; + index >>= 8; + } + d += indexCode; + + // Write value + memcpy(d, kv.value.begin(), kv.value.size()); + d += kv.value.size(); + ASSERT( d == v.begin()+size ); + return v; +} + +int getEncodedSize( int keySize, int valuePrefixSize ) { + int keyCode = keySize*2 + 12; + int header_size = sqlite3VarintLen(keyCode) + 8; // 8 is the maximum return value of sqlite3VarintLen(), so this is our worst case header size (for values larger than allowable database values) + int hh = sqlite3VarintLen(header_size); + header_size += hh; + if (hh < sqlite3VarintLen(header_size)) + header_size++; + return header_size + keySize + valuePrefixSize; +} + + +KeyValueRef decodeKV( StringRef encoded) { + uint8_t const* d = encoded.begin(); + uint64_t h, len1, len2; + d += sqlite3GetVarint( d, (u64*)&h ); + d += sqlite3GetVarint( d, (u64*)&len1 ); + d += sqlite3GetVarint( d, (u64*)&len2 ); + ASSERT( d == encoded.begin() + h ); + ASSERT( len1 >= 12 && !(len1&1) ); + ASSERT( len2 >= 12 && !(len2&1) ); + len1 = (len1-12)/2; + len2 = (len2-12)/2; + ASSERT( d + len1 + len2 == encoded.end() ); + return KeyValueRef( KeyRef(d, len1), KeyRef(d+len1, len2) ); +} + +// Given a key size and value prefix size, get the minimum bytes that must be read from the underlying +// btree tuple to safely read the prefix length from the value bytes (if the value is long enough) +int getEncodedKVFragmentSize( int keySize, int valuePrefixSize ) { + int keyCode = keySize*2 + 12; + int header_size = sqlite3VarintLen(keyCode) + + 1 // index code length + + 8; // worst case for value size (larger than fdb api allows) + int hh = sqlite3VarintLen(header_size); + header_size += hh; + if (hh < sqlite3VarintLen(header_size)) + header_size++; + return header_size + keySize + + 4 // Max width allowed of index value + + valuePrefixSize; +} + +// Decode (key, index, value) tuple. +// A present() Optional will always be returned UNLESS partial is true. +// If partial is true then the return will not be present() unless at least +// the full key and index were in the encoded buffer. The value returned will be 0 or +// more value bytes, however many were available. +// Note that a short encoded buffer must at *least* contain the header length varint. +Optional decodeKVFragment( StringRef encoded, uint32_t *index = NULL, bool partial = false) { + uint8_t const* d = encoded.begin(); + uint64_t h, len1, len2; + d += sqlite3GetVarint( d, (u64*)&h ); + + // Make sure entire header is present, else return nothing + if(partial && encoded.size() < h) + return Optional(); + + d += sqlite3GetVarint( d, (u64*)&len1 ); + const uint8_t indexLen = *d++; + ASSERT(indexLen <= 4); + d += sqlite3GetVarint( d, (u64*)&len2 ); + ASSERT( d == encoded.begin() + h ); + ASSERT( len1 >= 12 && !(len1&1) ); + ASSERT( len2 >= 12 && !(len2&1) ); + len1 = (len1-12)/2; + len2 = (len2-12)/2; + + if(partial) { + // If the key and index aren't complete, return nothing. + if(d + len1 + indexLen > encoded.end()) + return Optional(); + // Encoded size shouldn't be *larger* than the record described by the header no matter what. + ASSERT( d + len1 + indexLen + len2 >= encoded.end() ); + // Shorten value length to be whatever bytes remain after the header/key/index + len2 = std::min(len2, (uint64_t)(encoded.end() - indexLen - len1 - d)); + } + else { + // But for non partial records encoded size should be exactly the size of the described record. + ASSERT( d + len1 + indexLen + len2 == encoded.end() ); + } + + // Decode big endian index + if(index != nullptr) { + if(indexLen == 0) + *index = 0; + else { + const uint8_t *begin = d + len1; + const uint8_t *end = begin + indexLen; + *index = (uint8_t)*begin++; + while(begin < end) { + *index <<= 8; + *index |= *begin++; + } + } + } + return KeyValueRef( KeyRef(d, len1), KeyRef(d+len1+indexLen, len2) ); +} + +KeyValueRef decodeKVPrefix( StringRef encoded, int maxLength ) { + uint8_t const* d = encoded.begin(); + uint64_t h, len1, len2; + d += sqlite3GetVarint( d, (u64*)&h ); + d += sqlite3GetVarint( d, (u64*)&len1 ); + d += sqlite3GetVarint( d, (u64*)&len2 ); + ASSERT( d == encoded.begin() + h ); + ASSERT( len1 >= 12 && !(len1&1) ); + ASSERT( len2 >= 12 && !(len2&1) ); + len1 = (len1-12)/2; + len2 = (len2-12)/2; + len2 = std::min(len2, (uint64_t)maxLength); + ASSERT( d + len1 + len2 <= encoded.end() ); + return KeyValueRef( KeyRef(d, len1), KeyRef(d+len1, len2) ); +} + +Value encodeKey( KeyRef key, bool using_fragments) { + int keyCode = key.size()*2 + 12; + int header_size = sqlite3VarintLen(keyCode); + if(using_fragments) // will be encoded as key, 0 (where 0 is really a null) + ++header_size; + int hh = sqlite3VarintLen(header_size); + header_size += hh; + if (hh < sqlite3VarintLen(header_size)) + header_size++; + int size = header_size + key.size(); + Value v; + uint8_t* d = new (v.arena()) uint8_t[size]; + ((ValueRef&)v) = KeyRef(d, size); + d += sqlite3PutVarint( d, header_size ); + d += sqlite3PutVarint( d, keyCode ); + if(using_fragments) + *d++ = 0; + memcpy(d, key.begin(), key.size()); + d += key.size(); + ASSERT( d == v.begin()+size ); + return v; +} + +struct SQLiteTransaction { + SQLiteDB& db; + bool shouldCommit; + SQLiteTransaction(SQLiteDB& db, bool write) : db(db), shouldCommit(false) { + db.beginTransaction(write); + } + void commit() { + shouldCommit = true; + } + ~SQLiteTransaction() { + try { + if (shouldCommit) + db.endTransaction(); + else + db.rollback(); + } catch (...) {} + } +}; + +struct IntKeyCursor { + SQLiteDB& db; + BtCursor *cursor; + IntKeyCursor( SQLiteDB& db, int table, bool write ) : cursor(0), db(db) { + cursor = (BtCursor*)new char[sqlite3BtreeCursorSize()]; + sqlite3BtreeCursorZero(cursor); + db.checkError("BtreeCursor", sqlite3BtreeCursor(db.btree, table, write, NULL, cursor)); + } + ~IntKeyCursor() { + if (cursor) { + try { + db.checkError("BtreeCloseCursor", sqlite3BtreeCloseCursor(cursor)); + } catch (...) {} + delete[] (char*)cursor; + } + } +}; + +struct RawCursor { + SQLiteDB& db; + BtCursor *cursor; + KeyInfo keyInfo; + bool valid; + + operator bool() const { return valid; } + + RawCursor( SQLiteDB& db, int table, bool write) : cursor(0), db(db), valid(false) { + keyInfo.db = db.db; + keyInfo.enc = db.db->aDb[0].pSchema->enc; + keyInfo.aColl[0] = db.db->pDfltColl; + keyInfo.aSortOrder = 0; + keyInfo.nField = 1; + + try { + cursor = (BtCursor*)new char[sqlite3BtreeCursorSize()]; + sqlite3BtreeCursorZero(cursor); + db.checkError("BtreeCursor", sqlite3BtreeCursor(db.btree, table, write, &keyInfo, cursor)); + } catch (...) { + destroyCursor(); + throw; + } + } + ~RawCursor() { + destroyCursor(); + } + void destroyCursor() { + if (cursor) { + try { + db.checkError("BtreeCloseCursor", sqlite3BtreeCloseCursor(cursor)); + } catch (...) { + TraceEvent(SevError,"RawCursorDestructionError"); + } + delete[] (char*)cursor; + } + } + void moveFirst() { + int empty=1; + db.checkError("BtreeFirst", sqlite3BtreeFirst(cursor, &empty)); + valid = !empty; + } + void moveNext() { + int empty=1; + db.checkError("BtreeNext", sqlite3BtreeNext(cursor, &empty)); + valid = !empty; + } + void movePrevious() { + int empty=1; + db.checkError("BtreePrevious", sqlite3BtreePrevious(cursor, &empty)); + valid = !empty; + } + int size() { + int64_t size; + db.checkError("BtreeKeySize", sqlite3BtreeKeySize(cursor, (i64*)&size)); + ASSERT( size < (1<<30) ); + return size; + } + Value getEncodedRow() { + int s = size(); + Value v; + uint8_t* d = new (v.arena()) uint8_t[s]; + db.checkError("BtreeKey", sqlite3BtreeKey(cursor, 0, s, d)); + ((ValueRef&)v) = KeyRef(d, s); + return v; + } + ValueRef getEncodedRow( Arena& arena ) { + int s = size(); + uint8_t* d = new (arena) uint8_t[s]; + db.checkError("BtreeKey", sqlite3BtreeKey(cursor, 0, s, d)); + return KeyRef(d, s); + } + ValueRef getEncodedRowPrefix( Arena& arena, int maxEncodedSize ) { + int s = std::min(size(), maxEncodedSize); + uint8_t* d = new (arena) uint8_t[s]; + db.checkError("BtreeKey", sqlite3BtreeKey(cursor, 0, s, d)); + return KeyRef(d, s); + } + void insertFragment( KeyValueRef kv, uint32_t index, int seekResult ) { + Value v = encodeKVFragment(kv, index); + db.checkError("BtreeInsert", sqlite3BtreeInsert(cursor, v.begin(), v.size(), NULL, 0, 0, 0, seekResult)); + } + void remove() { + db.checkError("BtreeDelete", sqlite3BtreeDelete(cursor)); + } + void set( KeyValueRef kv ) { + if(db.fragment_values) { + // Unlike a read, where we need to access fragments in fully forward or reverse order, + // here we just want to delete any existing fragments for the key. It does not matter + // what order we delete them in, and SQLite requires us to seek after every delete, so + // the fastest way to do this is to repeatedly seek to the tuple prefix (key, ) and + // delete the current fragment until nothing is there. + // This should result in almost identical performance to non-fragmenting mode for single fragment kv pairs. + int seekResult = moveTo(kv.key, true); // second arg means to ignore fragmenting and seek to (key, ) + while(seekResult == 0) { + remove(); + seekResult = moveTo(kv.key, true); + } + + const int primaryPageUsable = SERVER_KNOBS->SQLITE_FRAGMENT_PRIMARY_PAGE_USABLE; + const int overflowPageUsable = SERVER_KNOBS->SQLITE_FRAGMENT_OVERFLOW_PAGE_USABLE; + + int fragments = 1; + int valuePerFragment = kv.value.size(); + + // Figure out if we would benefit from fragmenting this kv pair. The key size must be less than + // primary page usable size, and the value and key size together must exceeed the primary page usable size. + if( (kv.key.size() + kv.value.size()) > primaryPageUsable + && kv.key.size() < primaryPageUsable) { + + // Just the part of the value that would be in a partially-filled overflow page + int overflowPartialBytes = (kv.expectedSize() - primaryPageUsable) % overflowPageUsable; + + // Number of bytes wasted in the unfragmented case + int unfragmentedWaste = overflowPageUsable - overflowPartialBytes; + + // Total space used for unfragmented form + int unfragmentedTotal = kv.expectedSize() + unfragmentedWaste; + + // Value bytes that can fit in the primary page for each fragment + int primaryPageValueBytes = primaryPageUsable - kv.key.size(); + + // Calculate how many total fragments it would take to spread the partial overflow page bytes and the first fragment's primary + // page value bytes evenly over multiple tuples that fit in primary pages. + fragments = (primaryPageValueBytes + overflowPartialBytes + primaryPageValueBytes - 1) / primaryPageValueBytes; + + // Number of bytes wasted in the fragmented case (for the extra key copies) + int fragmentedWaste = kv.key.size() * (fragments - 1); + + // Total bytes used for the fragmented case + int fragmentedTotal = kv.expectedSize() + fragmentedWaste; + + // Calculate bytes saved by having extra key instances stored vs the original partial overflow page bytes. + int savings = unfragmentedWaste - fragmentedWaste; + + double reduction = (double)savings / unfragmentedTotal; + + //printf("K: %5d V: %6d OVERFLOW: %5d FRAGMENTS: %3d SAVINGS: %4d FRAG: %7d UNFRAG: %7d REDUCTION: %.3f\n", + //kv.key.size(), kv.value.size(), overflowPartialBytes, fragments, savings, fragmentedTotal, unfragmentedTotal, reduction); + if(reduction < SERVER_KNOBS->SQLITE_FRAGMENT_MIN_SAVINGS) + fragments = 1; + else + valuePerFragment = (primaryPageValueBytes + overflowPartialBytes + fragments - 1) / fragments; + } + + if(fragments == 1) { + insertFragment(kv, 0, seekResult); + return; + } + + // First index is ceiling(value_size / KV_FRAGMENT_INDEX_SIZE_HINT_FACTOR) + uint32_t nextIndex = (kv.value.size() + KV_FRAGMENT_INDEX_SIZE_HINT_FACTOR - 1) / KV_FRAGMENT_INDEX_SIZE_HINT_FACTOR; + // Last index is ceiling(value_size / (KV_FRAGMENT_INDEX_SIZE_HINT_FACTOR / 2) ) + uint32_t finalIndex = (kv.value.size() + (KV_FRAGMENT_INDEX_SIZE_HINT_FACTOR / 2) - 1) / (KV_FRAGMENT_INDEX_SIZE_HINT_FACTOR / 2); + int bytesLeft = kv.value.size(); + int readPos = 0; + while(bytesLeft > 0) { + --fragments; // remaining ideal fragment count + int fragSize = (fragments == 0) ? bytesLeft : std::min(bytesLeft, valuePerFragment); + + // The last fragment must have an index of finalIndex or higher. + if(fragSize == bytesLeft && nextIndex < finalIndex) + nextIndex = finalIndex; + //printf("insert ks %d vs %d fragment %d, %dbytes\n", kv.key.size(), kv.value.size(), nextIndex, fragSize); + insertFragment(KeyValueRef(kv.key, kv.value.substr(readPos, fragSize)), nextIndex, seekResult); + // seekResult can only be used for the first insertion. + if(seekResult != 0) + seekResult = 0; + readPos += fragSize; + bytesLeft -= fragSize; + ++nextIndex; + } + } + else { + int r = moveTo( kv.key ); + if (!r) remove(); + Value v = encode(kv); + db.checkError("BTreeInsert", sqlite3BtreeInsert(cursor, v.begin(), v.size(), NULL, 0, 0, 0, r)); + } + } + void clearOne( KeyRangeRef keys ) { + ASSERT(!db.fragment_values); + int r = moveTo( keys.begin ); + if (r < 0) moveNext(); + ASSERT( valid && decodeKV(getEncodedRow()).key < keys.end ); + remove(); + } + void clear( KeyRangeRef keys ) { + // TODO: This is really slow! + while (true) { + int r = moveTo( keys.begin ); + if (r < 0) moveNext(); + if (!valid || (db.fragment_values ? decodeKVFragment(getEncodedRow()).get().key : decodeKV(getEncodedRow()).key) >= keys.end) + break; + remove(); + } + } + void fastClear( KeyRangeRef keys, bool& freeTableEmpty ) { + vector clearBuffer( SERVER_KNOBS->CLEAR_BUFFER_SIZE ); + clearBuffer[0] = 0; + + while (true) { + if (moveTo( keys.begin )<0) moveNext(); + RawCursor endCursor(db, db.table, false); + if (endCursor.moveTo( keys.end )>=0) endCursor.movePrevious(); + + if (!valid || !endCursor + || (db.fragment_values ? (decodeKVFragment(getEncodedRow()).get().key >= decodeKVFragment(endCursor.getEncodedRow()).get().key) + : (decodeKV(getEncodedRow()).key > decodeKV(endCursor.getEncodedRow()).key) + ) + ) + break; // If empty stop! + + int rc = sqlite3BtreeDeleteRange(cursor, endCursor.cursor, &clearBuffer[0], &clearBuffer[0]+clearBuffer.size()); + if (rc == 201) continue; + if (!rc) break; + db.checkError("BtreeDeleteRange", rc); + } + + if (clearBuffer[0]) { + //printf("fastClear(%s,%s): %d pages freed\n", printable(keys.begin).c_str(), printable(keys.end).c_str(), clearBuffer[0]); + IntKeyCursor fc(db, db.freetable, true); + int pagesDeleted = 0; + db.checkError( "BtreeLazyDelete", sqlite3BtreeLazyDelete( fc.cursor, &clearBuffer[0], &clearBuffer[0]+clearBuffer.size(), 0, &pagesDeleted ) ); + ASSERT(pagesDeleted == 0); + freeTableEmpty = false; + } + } + int lazyDelete( int desiredPages ) { + vector clearBuffer( SERVER_KNOBS->CLEAR_BUFFER_SIZE ); + clearBuffer[0] = 0; + + IntKeyCursor fc(db, db.freetable, true); + int pagesDeleted = 0; + db.checkError( "BtreeLazyDelete", sqlite3BtreeLazyDelete( fc.cursor, &clearBuffer[0], &clearBuffer[0]+clearBuffer.size(), desiredPages, &pagesDeleted )); + return pagesDeleted; + } + + // Reads and reconstitutes kv fragments given cursor, an arena to allocate in, and a direction to move the cursor. + // getNext() returns the next KV pair, if there is one + // peek() returns the next key that would be read by getNext(), if there is one + // Both methods return Optionals. + // Once either method returns a non-present value, using the DefragmentingReader again is undefined behavior. + struct DefragmentingReader { + // Use this constructor for forward/backward range reads + DefragmentingReader(RawCursor &cur, Arena &m, bool forward) : cur(cur), arena(m), forward(forward), fragmentReadLimit(-1) { + parse(); + } + + // Use this constructor to read a SINGLE partial value from the current cursor position for an expected key. + // This exists to support IKeyValueStore::getPrefix(). + // The reader will return exactly one KV pair if its key matches expectedKey, otherwise no KV pairs. + DefragmentingReader(RawCursor &cur, Arena &m, KeyRef expectedKey, int maxValueLen): cur(cur), arena(m), forward(true), maxValueLen(maxValueLen) { + fragmentReadLimit = getEncodedKVFragmentSize(expectedKey.size(), maxValueLen); + parse(); + // If a key was found but it wasn't the expected key then + // clear the current kv pair and invalidate the cursor. + if(kv.present() && kv.get().key != expectedKey) { + kv = Optional(); + cur.valid = false; + } + } + + private: + Optional kv; // key and latest value fragment read + uint32_t index; // index of latest value fragment read + RawCursor &cur; // Cursor to read from + Arena &arena; // Arena to allocate key and value bytes in + bool forward; // true for forward iteration, false for reverse + int maxValueLen; // truncated value length to return + int fragmentReadLimit; // If >= 0, only read and *attempt* to decode this many fragment bytes + + // Update kv with whatever is at the current cursor position if the position is valid. + void parse() { + if(cur.valid) { + // The read is either not partial or it is but the fragment read limit is at least 4 (the size of a minimal header). + bool partial = fragmentReadLimit >= 0; + ASSERT(!partial || fragmentReadLimit >= 4); + // Read full or part of fragment + ValueRef encoded = (partial) ? cur.getEncodedRowPrefix(arena, fragmentReadLimit) : cur.getEncodedRow(arena); + kv = decodeKVFragment(encoded, &index, partial); + // If this was a partial fragment then if successful update the next fragment read size, and if not + // then invalidate the cursor. + if(partial) { + if(kv.present()) + fragmentReadLimit -= kv.get().value.size(); + else + cur.valid = false; + } + } + else + kv = Optional(); + } + + // advance cursor, parse and return key if valid + Optional advance() { + if(cur.valid) { + forward ? cur.moveNext() : cur.movePrevious(); + parse(); + } + return kv.present() ? kv.get().key : Optional(); + } + + public: + // Get the next key that would be returned by getNext(), if there is one + // This is more efficient than getNext() if the caller is not sure if it wants the next KV pair + Optional peek() { + if(kv.present()) + return kv.get().key; + return advance(); + } + + Optional getNext() { + if(!peek().present()) + return Optional(); + + bool partial = fragmentReadLimit >= 0; + + // Start out with the next KV fragment as the pair to return + KeyValueRef resultKV = kv.get(); + + // If index is 0 then this is an unfragmented key. It is unnecessary to advance the cursor so + // we won't, but we will clear kv so that the next peek/getNext will have to advance. + if(index == 0) + kv = Optional(); + else { + // First and last indexes in fragment group are size hints. + // First index is ceil(total_value_size / 4) + // Last index is ceil(total_value_size / 2) + // Set size depending on which of these will be first encountered and allocate buffer in arena. + // Note that if these index hints are wrong (such as if the index scheme changes) then asserts + // below will fail. They will have to be changed to expand the buffer as needed. + int size = forward ? (index * KV_FRAGMENT_INDEX_SIZE_HINT_FACTOR) : (index * (KV_FRAGMENT_INDEX_SIZE_HINT_FACTOR / 2)); + uint8_t *buf = new (arena) uint8_t[size]; + uint8_t *bufEnd = buf + size; + // For forward iteration wptr is the place to write to next, for reverse it's where the last write started. + uint8_t *wptr = forward ? buf : bufEnd; + int fragments = 0; + do { + ++fragments; + const ValueRef &val = kv.get().value; + if(forward) { + uint8_t *w = wptr; + wptr += val.size(); + ASSERT(wptr <= bufEnd); + memcpy(w, val.begin(), val.size()); + // If this is a partial value get and we have enough bytes we can stop since we are forward iterating. + if(partial && wptr - buf >= maxValueLen) { + resultKV.value = ValueRef(buf, maxValueLen); + // To make further calls to peek() or getNext() return nothing, reset kv and invalidate cursor + kv = Optional(); + cur.valid = false; + return resultKV; + } + } + else { + wptr -= val.size(); + ASSERT(wptr >= buf); + memcpy(wptr, val.begin(), val.size()); + } + } while(advance().present() && kv.get().key == resultKV.key); + + // If there was only 1 fragment, it should have been index 0 and handled above, + ASSERT(fragments != 1); + // Set final value based on direction of buffer fill + resultKV.value = forward ? ValueRef(buf, wptr - buf) : ValueRef(wptr, bufEnd - wptr); + } + + // In partial value mode, we could end up here if there was only 1 fragments or maxValueLen + // was greater than the total unfragmented value size. + if(partial) + resultKV.value = resultKV.value.substr(0, std::min(resultKV.value.size(), maxValueLen)); + return resultKV; + } + }; + + Optional get( KeyRef key ) { + int r = moveTo(key); + if(db.fragment_values) { + // Optimization - moveTo seeks to fragment (key, 0) so if it was exactly found then we + // know we have a single fragment for key and can return it. + if(r == 0) { + Value result; + ((ValueRef&)result) = decodeKVFragment(getEncodedRow(result.arena())).get().value; + return result; + } + + // Otherwise see if the fragments immediately after (key, 0) are for the key we want. + if(r < 0) + moveNext(); + Arena m; + DefragmentingReader i(*this, m, true); + if(i.peek() == key) { + Optional kv = i.getNext(); + return Value(kv.get().value, m); + } + } + else if (r == 0) { + Value result; + KeyValueRef kv = decodeKV( getEncodedRow( result.arena() ) ); + ((ValueRef&)result) = kv.value; + return result; + } + + return Optional(); + } + Optional getPrefix( KeyRef key, int maxLength ) { + if(db.fragment_values) { + int r = moveTo(key); + if (r < 0) + moveNext(); + Arena m; + DefragmentingReader i(*this, m, getEncodedKVFragmentSize(key.size(), maxLength)); + if(i.peek() == key) { + Optional kv = i.getNext(); + return Value(kv.get().value, m); + } + } + else if (!moveTo(key)) { + if(maxLength == 0) { + return Value(); + } + Value result; + int maxEncodedSize = getEncodedSize( key.size(), maxLength ); + KeyValueRef kv = decodeKVPrefix( getEncodedRowPrefix( result.arena(), maxEncodedSize ), maxLength ); + ((ValueRef&)result) = kv.value; + return result; + } + return Optional(); + } + Standalone> getRange( KeyRangeRef keys, int rowLimit, int byteLimit ) { + Standalone> result; + int accumulatedBytes = 0; + ASSERT( byteLimit > 0 ); + if(db.fragment_values) { + if(rowLimit >= 0) { + int r = moveTo(keys.begin); + if (r < 0) + moveNext(); + + DefragmentingReader i(*this, result.arena(), true); + Optional nextKey = i.peek(); + while(nextKey.present() && nextKey.get() < keys.end && rowLimit-- && accumulatedBytes < byteLimit) { + Optional kv = i.getNext(); + result.push_back(result.arena(), kv.get()); + accumulatedBytes += sizeof(KeyValueRef) + kv.get().expectedSize(); + nextKey = i.peek(); + } + } + else { + int r = moveTo(keys.end); + if (r >= 0) + movePrevious(); + DefragmentingReader i(*this, result.arena(), false); + Optional nextKey = i.peek(); + while(nextKey.present() && nextKey.get() >= keys.begin && rowLimit++ && accumulatedBytes < byteLimit) { + Optional kv = i.getNext(); + result.push_back(result.arena(), kv.get()); + accumulatedBytes += sizeof(KeyValueRef) + kv.get().expectedSize(); + nextKey = i.peek(); + } + } + } + else { + if (rowLimit >= 0) { + int r = moveTo( keys.begin ); + if (r < 0) moveNext(); + while (this->valid && rowLimit-- && accumulatedBytes < byteLimit) { + KeyValueRef kv = decodeKV( getEncodedRow( result.arena() ) ); + accumulatedBytes += sizeof(KeyValueRef) + kv.expectedSize(); + if (kv.key >= keys.end) break; + result.push_back( result.arena(), kv ); + moveNext(); + } + } else { + int r = moveTo( keys.end ); + if (r >= 0) movePrevious(); + while (this->valid && rowLimit++ && accumulatedBytes < byteLimit) { + KeyValueRef kv = decodeKV( getEncodedRow( result.arena() ) ); + accumulatedBytes += sizeof(KeyValueRef) + kv.expectedSize(); + if (kv.key < keys.begin) break; + result.push_back( result.arena(), kv ); + movePrevious(); + } + } + } + return result; + } + + int moveTo( KeyRef key, bool ignore_fragment_mode = false ) { + UnpackedRecord r; + r.pKeyInfo = &keyInfo; + r.flags = UNPACKED_PREFIX_MATCH; // This record [key] can be considered equal to a record [key,value] for any value + Mem tupleValues[2]; + r.aMem = tupleValues; + + // Set field 1 of tuple to key, which is a string type with typecode 12 + 2*len + tupleValues[0].db = keyInfo.db; + tupleValues[0].enc = keyInfo.enc; + tupleValues[0].zMalloc = NULL; + ASSERT(sqlite3VdbeSerialGet(key.begin(), 12 + (2 * key.size()), &tupleValues[0]) == key.size()); + + // In fragmenting mode, seek is to (k, 0, ), otherwise just (k, ). + if(ignore_fragment_mode || !db.fragment_values) + r.nField = 1; + else { + // Set field 2 of tuple to the null type which is typecode 0 + tupleValues[1].db = keyInfo.db; + tupleValues[1].enc = keyInfo.enc; + tupleValues[1].zMalloc = NULL; + ASSERT(sqlite3VdbeSerialGet(NULL, 0, &tupleValues[1]) == 0); + + r.nField = 2; + } + + int result; + db.checkError("BtreeMovetoUnpacked", sqlite3BtreeMovetoUnpacked(cursor, &r, 0, 0, &result)); + valid = result >= 0 || !sqlite3BtreeEof(cursor); + return result; + } +}; +struct Cursor : SQLiteTransaction, RawCursor { + Cursor( SQLiteDB& db, bool write) : SQLiteTransaction(db, write), RawCursor(db, db.table, write) { + + } +}; + +struct ReadCursor : ReferenceCounted, FastAllocated { + // Readers need to be reset (forced to move to a new snapshot) when the writer thread does a checkpoint. + // ReadCursor is reference counted so that the writer can clear the persistent reference (readCursors[n]) and + // readers can hold an additional reference when they actually have a read happening. + // ReadCursor lazily constructs its actual Cursor (and hence transaction) because it's vital that readCursors[n] be + // assigned before the transaction is opened. + + ReadCursor() : valid(false) {} + + void init(SQLiteDB& db) { new (&cursor) Cursor(db, false); valid = true; } + ~ReadCursor() { if (valid) get().~Cursor(); } + + Cursor& get() { return *((Cursor*)&cursor); } + +private: + std::aligned_storage< sizeof(Cursor), __alignof(Cursor) >::type cursor; + bool valid; +}; + +extern bool vfsAsyncIsOpen( std::string filename ); + +// Returns number of pages which failed checksum. +int SQLiteDB::checkAllPageChecksums() { + ASSERT( !haveMutex ); + ASSERT( page_checksums ); // This should never be called on SQLite databases that do not have page checksums. + + double startT = timer(); + + // First try to open an existing file + std::string apath = abspath(filename); + std::string walpath = apath + "-wal"; + + /* REMOVE THIS BEFORE CHECKIN */ if(!fileExists(apath)) return 0; + + TraceEvent("SQLitePageChecksumScanBegin").detail("File", apath); + + ErrorOr> dbFile = waitForAndGet( errorOr( IAsyncFileSystem::filesystem()->open( apath, IAsyncFile::OPEN_READONLY | IAsyncFile::OPEN_LOCK, 0 ) ) ); + ErrorOr> walFile = waitForAndGet( errorOr( IAsyncFileSystem::filesystem()->open( walpath, IAsyncFile::OPEN_READONLY | IAsyncFile::OPEN_LOCK, 0 ) ) ); + + if (dbFile.isError()) throw dbFile.getError(); // If we've failed to open the file, throw an exception + if (walFile.isError()) throw walFile.getError(); // If we've failed to open the file, throw an exception + + // Now that the file itself is open and locked, let sqlite open the database + // Note that VFSAsync will also call g_network->open (including for the WAL), so its flags are important, too + // TODO: If better performance is needed, make AsyncFileReadAheadCache work and be enabled by SQLITE_OPEN_READAHEAD which was added for that purpose. + int result = sqlite3_open_v2(apath.c_str(), &db, SQLITE_OPEN_READONLY, NULL); + checkError("open", result); + + // This check has the useful side effect of actually opening/reading the database. If we were not doing this, + // then we could instead open a read cursor for the same effect, as currently tryReadEveryDbPage() requires it. + Statement *jm = new Statement(*this, "PRAGMA journal_mode"); + ASSERT( jm->nextRow() ); + if (jm->column(0) != LiteralStringRef("wal")){ + TraceEvent(SevError, "JournalModeError").detail("Filename", filename).detail("Mode", printable(jm->column(0))); + ASSERT( false ); + } + delete jm; + + btree = db->aDb[0].pBt; + initPagerCodec(); + sqlite3_extended_result_codes(db, 1); + + sqlite3_mutex_enter(db->mutex); + haveMutex = true; + + pPagerCodec->silent = true; + Pgno p = 1; + int readErrors = 0; + int corruptPages = 0; + int totalErrors = 0; + + while(1) { + int type; + int zero; + int rc = tryReadEveryDbPage(db, p, &p, &type, &zero); + if(rc == SQLITE_OK) + break; + if(rc == SQLITE_CORRUPT) { + TraceEvent(SevWarnAlways, "SQLitePageChecksumScanCorruptPage") + .detail("File", filename) + .detail("PageNumber", p) + .detail("PageType", type) + .detail("PageWasZeroed", zero); + ++corruptPages; + } else { + TraceEvent(SevWarnAlways, "SQLitePageChecksumScanReadFailed") + .detail("File", filename) + .detail("PageNumber", p) + .detail("SQLiteError", sqlite3ErrStr(rc)) + .detail("SQLiteErrorCode", rc); + ++readErrors; + } + ++p; + if(++totalErrors >= SERVER_KNOBS->SQLITE_PAGE_SCAN_ERROR_LIMIT) + break; + } + pPagerCodec->silent = false; + + haveMutex = false; + sqlite3_mutex_leave(db->mutex); + sqlite3_close(db); + + TraceEvent("SQLitePageChecksumScanEnd") + .detail("Elapsed", DEBUG_DETERMINISM ? 0 : timer()-startT) + .detail("filename", filename) + .detail("CorruptPages", corruptPages) + .detail("ReadErrors", readErrors) + .detail("TotalErrors", totalErrors); + + ASSERT(!vfsAsyncIsOpen(filename)); + + return totalErrors; +} + +void SQLiteDB::open(bool writable) { + ASSERT( !haveMutex ); + double startT = timer(); + //TraceEvent("KVThreadInitStage").detail("Stage",1).detail("filename", filename).detail("writable", writable); + + // First try to open an existing file + std::string apath = abspath(filename); + std::string walpath = apath + "-wal"; + ErrorOr> dbFile = waitForAndGet( errorOr( IAsyncFileSystem::filesystem()->open( apath, IAsyncFile::OPEN_READWRITE | IAsyncFile::OPEN_LOCK, 0 ) ) ); + ErrorOr> walFile = waitForAndGet( errorOr( IAsyncFileSystem::filesystem()->open( walpath, IAsyncFile::OPEN_READWRITE | IAsyncFile::OPEN_LOCK, 0 ) ) ); + + //TraceEvent("KVThreadInitStage").detail("Stage",15).detail("filename", apath).detail("writable", writable).detail("isErr", dbFile.isError()); + + if (writable) { + if (dbFile.isError() && dbFile.getError().code() == error_code_file_not_found && !fileExists(apath) && // db file is missing + !walFile.isError() && fileExists(walpath)) // ..but WAL file is present + { + // Either we died partway through creating this DB, or died partway through deleting it, or someone is monkeying with our files + // Create a new blank DB by backing up the WAL file (just in case it is important) and then hitting the next case + walFile = file_not_found(); + renameFile( walpath, walpath + "-old-" + g_random->randomUniqueID().toString() ); + ASSERT_WE_THINK(false); //< This code should not be hit in FoundationDB at the moment, because worker looks for databases to open by listing .fdb files, not .fdb-wal files + //TEST(true); // Replace a partially constructed or destructed DB + } + + if (dbFile.isError() && walFile.isError() && writable && + dbFile.getError().code() == error_code_file_not_found && + walFile.getError().code() == error_code_file_not_found && + !fileExists(apath) && !fileExists(walpath)) + { + // The file doesn't exist, try to create a new one + // Creating the WAL before the database ensures we will not try to open a database with no WAL + walFile = waitForAndGet( IAsyncFileSystem::filesystem()->open( walpath, IAsyncFile::OPEN_ATOMIC_WRITE_AND_CREATE | IAsyncFile::OPEN_CREATE | IAsyncFile::OPEN_READWRITE | IAsyncFile::OPEN_LOCK, 0600 ) ); + waitFor( walFile.get()->sync() ); + dbFile = waitForAndGet( IAsyncFileSystem::filesystem()->open( apath, IAsyncFile::OPEN_ATOMIC_WRITE_AND_CREATE | IAsyncFile::OPEN_CREATE | IAsyncFile::OPEN_READWRITE | IAsyncFile::OPEN_LOCK, 0600 ) ); + if(page_checksums) + waitFor( dbFile.get()->write( template_fdb_with_page_checksums, sizeof(template_fdb_with_page_checksums), 0 ) ); + else + waitFor( dbFile.get()->write( template_fdb_without_page_checksums, sizeof(template_fdb_without_page_checksums), 0 ) ); + waitFor( dbFile.get()->sync() ); // renames filename.part to filename, fsyncs data and directory + TraceEvent("CreatedDBFile").detail("Filename", apath); + } + } + if (dbFile.isError()) throw dbFile.getError(); // If we've failed to open the file, throw an exception + if (walFile.isError()) throw walFile.getError(); // If we've failed to open the file, throw an exception + + //TraceEvent("KVThreadInitStage").detail("Stage",2).detail("filename", filename).detail("writable", writable); + + // Now that the file itself is open and locked, let sqlite open the database + // Note that VFSAsync will also call g_network->open (including for the WAL), so its flags are important, too + int result = sqlite3_open_v2(apath.c_str(), &db, (writable ? SQLITE_OPEN_READWRITE : SQLITE_OPEN_READONLY), NULL); + checkError("open", result); + + btree = db->aDb[0].pBt; + initPagerCodec(); + + sqlite3_extended_result_codes(db, 1); + + //TraceEvent("KVThreadInitStage").detail("Stage",3).detail("filename", filename).detail("writable", writable); + + //Statement(*this, "PRAGMA cache_size = 100").execute(); + + Statement jm(*this, "PRAGMA journal_mode"); + ASSERT( jm.nextRow() ); + if (jm.column(0) != LiteralStringRef("wal")){ + TraceEvent(SevError, "JournalModeError").detail("Filename", filename).detail("Mode", printable(jm.column(0))); + ASSERT( false ); + } + + if (writable) { + Statement(*this, "PRAGMA synchronous = NORMAL").execute(); // OFF, NORMAL, FULL + Statement(*this, "PRAGMA wal_autocheckpoint = -1").nextRow(); + } + + //TraceEvent("KVThreadInitStage").detail("Stage",4).detail("filename", filename).detail("writable", writable); + + sqlite3_mutex_enter(db->mutex); + haveMutex = true; + + table = 3; + freetable = 4; + this->dbFile = dbFile.get(); + this->walFile = walFile.get(); + + TraceEvent("KVThreadInitTime").detail("Elapsed", DEBUG_DETERMINISM ? 0 : timer()-startT).detail("filename", filename).detail("writable", writable); + ASSERT(vfsAsyncIsOpen(filename)); +} + +void SQLiteDB::createFromScratch() { + int sqliteFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; + checkError("open", sqlite3_open_v2(filename.c_str(), &db, sqliteFlags, NULL)); + + Statement(*this, "PRAGMA page_size = 4096").nextRow(); //fast + btree = db->aDb[0].pBt; + initPagerCodec(); + + Statement(*this, "PRAGMA auto_vacuum = 2").nextRow(); //slow all the time + Statement(*this, "PRAGMA journal_mode = WAL").nextRow(); // sometimes slow + sqlite3_extended_result_codes(db, 1); + + sqlite3_mutex_enter(db->mutex); + haveMutex = true; + + beginTransaction(true); + u32 pgnoRoot = -1; + sqlite3BtreeGetMeta(btree, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot); + + // We expect our tables are #3, #4 (since autovacuum is enabled, there is a pointer map page at #2) + if (pgnoRoot == 4) { + table = pgnoRoot-1; + freetable = pgnoRoot; + rollback(); + } else if (pgnoRoot == 1){ + // The database is empty; create tables + checkError("BtreeCreateTable", sqlite3BtreeCreateTable( btree, &table, BTREE_BLOBKEY )); + ASSERT( table == 3 ); + checkError("BtreeCreateTable2", sqlite3BtreeCreateTable( btree, &freetable, BTREE_INTKEY )); + ASSERT( freetable == table+1 ); + endTransaction(); + } else { + TraceEvent("pgnoRoot").detail("value", pgnoRoot); + checkError("CheckTables", SQLITE_CORRUPT); + } +} + +struct ThreadSafeCounter { + volatile int64_t counter; + ThreadSafeCounter() : counter(0) {} + void operator ++() { interlockedIncrement64(&counter); } + void operator --() { interlockedDecrement64(&counter); } + operator const int64_t() const { return counter; } +}; + +class KeyValueStoreSQLite : public IKeyValueStore { +public: + virtual void dispose() { + doClose(this, true); + } + virtual void close() { + doClose(this, false); + } + + virtual Future getError() { return delayed( readThreads->getError() || writeThread->getError() ); } + virtual Future onClosed() { return stopped.getFuture(); } + + virtual KeyValueStoreType getType() { return type; } + virtual StorageBytes getStorageBytes(); + + virtual void set( KeyValueRef keyValue, const Arena* arena = NULL ); + virtual void clear( KeyRangeRef range, const Arena* arena = NULL ); + virtual Future commit(bool sequential = false); + + virtual Future> readValue( KeyRef key, Optional debugID ); + virtual Future> readValuePrefix( KeyRef key, int maxLength, Optional debugID ); + virtual Future>> readRange( KeyRangeRef keys, int rowLimit = 1<<30, int byteLimit = 1<<30 ); + + KeyValueStoreSQLite(std::string const& filename, UID logID, KeyValueStoreType type, bool validateFile); + ~KeyValueStoreSQLite(); + + Future doClean(); + void startReadThreads(); + +private: + KeyValueStoreType type; + UID logID; + std::string filename; + Reference readThreads, writeThread; + Promise stopped; + Future cleaning, logging, starting, stopOnErr; + bool validateFile; + + int64_t readsRequested, writesRequested; + ThreadSafeCounter readsComplete; + volatile int64_t writesComplete; + volatile SpringCleaningStats springCleaningStats; + volatile int64_t diskBytesUsed; + volatile int64_t freeListPages; + + vector< Reference > readCursors; + + struct Reader : IThreadPoolReceiver { + SQLiteDB conn; + ThreadSafeCounter& counter; + UID dbgid; + Reference* ppReadCursor; + + explicit Reader( std::string const& filename, bool is_btree_v2, ThreadSafeCounter& counter, UID dbgid, Reference* ppReadCursor ) + : conn( filename, is_btree_v2, is_btree_v2 ), counter(counter), dbgid(dbgid), ppReadCursor(ppReadCursor) + { + } + ~Reader() { + ppReadCursor->clear(); + } + + virtual void init() { + conn.open(false); + } + + Reference getCursor() { + Reference cursor = *ppReadCursor; + if (!cursor) { + *ppReadCursor = cursor = Reference(new ReadCursor); + cursor->init(conn); + } + return cursor; + } + + struct ReadValueAction : TypedAction, FastAllocated { + Key key; + Optional debugID; + ThreadReturnPromise> result; + ReadValueAction(Key key, Optional debugID) : key(key), debugID(debugID) {}; + virtual double getTimeEstimate() { return SERVER_KNOBS->READ_VALUE_TIME_ESTIMATE; } + }; + void action( ReadValueAction& rv ) { + //double t = timer(); + if (rv.debugID.present()) g_traceBatch.addEvent("GetValueDebug", rv.debugID.get().first(), "Reader.Before"); //.detail("TaskID", g_network->getCurrentTask()); + + rv.result.send( getCursor()->get().get(rv.key) ); + ++counter; + + if (rv.debugID.present()) g_traceBatch.addEvent("GetValueDebug", rv.debugID.get().first(), "Reader.After"); //.detail("TaskID", g_network->getCurrentTask()); + //t = timer()-t; + //if (t >= 1.0) TraceEvent("ReadValueActionSlow",dbgid).detail("Elapsed", t); + } + + struct ReadValuePrefixAction : TypedAction, FastAllocated { + Key key; + int maxLength; + Optional debugID; + ThreadReturnPromise> result; + ReadValuePrefixAction(Key key, int maxLength, Optional debugID) : key(key), maxLength(maxLength), debugID(debugID) {}; + virtual double getTimeEstimate() { return SERVER_KNOBS->READ_VALUE_TIME_ESTIMATE; } + }; + void action( ReadValuePrefixAction& rv ) { + //double t = timer(); + if (rv.debugID.present()) g_traceBatch.addEvent("GetValuePrefixDebug", rv.debugID.get().first(), "Reader.Before"); //.detail("TaskID", g_network->getCurrentTask()); + + rv.result.send( getCursor()->get().getPrefix(rv.key, rv.maxLength) ); + ++counter; + + if (rv.debugID.present()) g_traceBatch.addEvent("GetValuePrefixDebug", rv.debugID.get().first(), "Reader.After"); //.detail("TaskID", g_network->getCurrentTask()); + //t = timer()-t; + //if (t >= 1.0) TraceEvent("ReadValuePrefixActionSlow",dbgid).detail("Elapsed", t); + } + + struct ReadRangeAction : TypedAction, FastAllocated { + KeyRange keys; + int rowLimit, byteLimit; + ThreadReturnPromise>> result; + ReadRangeAction(KeyRange keys, int rowLimit, int byteLimit) : keys(keys), rowLimit(rowLimit), byteLimit(byteLimit) {} + virtual double getTimeEstimate() { return SERVER_KNOBS->READ_RANGE_TIME_ESTIMATE; } + }; + void action( ReadRangeAction& rr ) { + rr.result.send( getCursor()->get().getRange(rr.keys, rr.rowLimit, rr.byteLimit) ); + ++counter; + } + }; + + struct Writer : IThreadPoolReceiver { + SQLiteDB conn; + Cursor* cursor; + int commits; + int setsThisCommit; + bool freeTableEmpty; // true if we are sure the freetable (pages pending lazy deletion) is empty + volatile int64_t& writesComplete; + volatile SpringCleaningStats& springCleaningStats; + volatile int64_t& diskBytesUsed; + volatile int64_t& freeListPages; + UID dbgid; + vector>& readThreads; + bool checkAllChecksumsOnOpen; + + explicit Writer( std::string const& filename, bool isBtreeV2, bool checkAllChecksumsOnOpen, volatile int64_t& writesComplete, volatile SpringCleaningStats& springCleaningStats, volatile int64_t& diskBytesUsed, volatile int64_t& freeListPages, UID dbgid, vector>* pReadThreads ) + : conn( filename, isBtreeV2, isBtreeV2 ), + commits(), setsThisCommit(), + freeTableEmpty(false), + writesComplete(writesComplete), + springCleaningStats(springCleaningStats), + diskBytesUsed(diskBytesUsed), + freeListPages(freeListPages), + cursor(NULL), + dbgid(dbgid), + readThreads(*pReadThreads), + checkAllChecksumsOnOpen(checkAllChecksumsOnOpen) + { + } + ~Writer() { + TraceEvent("KVWriterDestroying", dbgid); + delete cursor; + TraceEvent("KVWriterDestroyed", dbgid); + } + virtual void init() { + if(checkAllChecksumsOnOpen) + conn.checkAllPageChecksums(); + conn.open(true); + + //If a wal file fails during the commit process before finishing a checkpoint, then it is possible that our wal file will be non-empty + //when we reload it. We execute a checkpoint here to remedy that situation. This call must come before before creating a cursor because + //it will fail if there are any outstanding transactions. + fullCheckpoint(); + + cursor = new Cursor(conn, true); + + if (EXPENSIVE_VALIDATION) + conn.check(false); + } + + struct InitAction : TypedAction, FastAllocated { + ThreadReturnPromise result; + virtual double getTimeEstimate() { return 0; } + }; + void action(InitAction& a) { + // init() has already been called + a.result.send(Void()); + } + + struct SetAction : TypedAction, FastAllocated { + KeyValue kv; + SetAction( KeyValue kv ) : kv(kv) {} + virtual double getTimeEstimate() { return SERVER_KNOBS->SET_TIME_ESTIMATE; } + }; + void action(SetAction& a) { + double s = now(); + checkFreePages(); + cursor->set(a.kv); + ++setsThisCommit; + ++writesComplete; + if (g_network->isSimulated() && g_simulator.getCurrentProcess()->rebooting) + TraceEvent("SetActionFinished", dbgid).detail("Elapsed", now()-s); + } + + struct ClearAction : TypedAction, FastAllocated { + KeyRange range; + ClearAction( KeyRange range ) : range(range) {} + virtual double getTimeEstimate() { return SERVER_KNOBS->CLEAR_TIME_ESTIMATE; } + }; + void action(ClearAction& a) { + double s = now(); + cursor->fastClear(a.range, freeTableEmpty); + cursor->clear(a.range); // TODO: at most one + ++writesComplete; + if (g_network->isSimulated() && g_simulator.getCurrentProcess()->rebooting) + TraceEvent("ClearActionFinished", dbgid).detail("Elapsed", now()-s); + } + + struct CommitAction : TypedAction, FastAllocated { + double issuedTime; + ThreadReturnPromise result; + CommitAction() : issuedTime(now()) {} + virtual double getTimeEstimate() { return SERVER_KNOBS->COMMIT_TIME_ESTIMATE; } + }; + void action(CommitAction& a) { + double t1 = now(); + cursor->commit(); + delete cursor; + cursor = NULL; + + double t2 = now(); + + fullCheckpoint(); + + double t3 = now(); + + ++commits; + //if ( !(commits % 100) ) + //printf("dbf=%lld bytes, wal=%lld bytes\n", getFileSize((kv->filename+".fdb").c_str()), getFileSize((kv->filename+".fdb-wal").c_str())); + + a.result.send(Void()); + + cursor = new Cursor(conn, true); + checkFreePages(); + ++writesComplete; + if (t3-a.issuedTime > 10.0*g_random->random01()) + TraceEvent("KVCommit10s_sample", dbgid).detail("Queued", t1-a.issuedTime).detail("Commit", t2-t1).detail("Checkpoint", t3-t2); + + diskBytesUsed = waitForAndGet( conn.dbFile->size() ) + waitForAndGet( conn.walFile->size() ); + + if (g_network->isSimulated() && g_simulator.getCurrentProcess()->rebooting) + TraceEvent("CommitActionFinished", dbgid).detail("Elapsed", now()-t1); + } + + //Checkpoints the database and resets the wal file back to the beginning + void fullCheckpoint() { + //A checkpoint cannot succeed while there is an outstanding transaction + ASSERT(cursor == NULL); + + resetReaders(); + conn.checkpoint(false); + + resetReaders(); + conn.checkpoint(true); + } + + void resetReaders() { + for(int i=0; iCHECK_FREE_PAGE_AMOUNT) { + int deletedPages = cursor->lazyDelete(SERVER_KNOBS->CHECK_FREE_PAGE_AMOUNT); + freeTableEmpty = (deletedPages != SERVER_KNOBS->CHECK_FREE_PAGE_AMOUNT); + springCleaningStats.lazyDeletePages += deletedPages; + ++iterations; + + freeListSize = conn.freePages(); + } + + freeListPages = freeListSize; + //if (iterations) printf("Lazy free: %d pages on freelist, %d iterations, freeTableEmpty=%d\n", freeListPages, iterationsi, freeTableEmpty); + } + + struct SpringCleaningAction : TypedAction, FastAllocated { + ThreadReturnPromise result; + virtual double getTimeEstimate() { return SERVER_KNOBS->SPRING_CLEANING_TIME_ESTIMATE; } + }; + void action(SpringCleaningAction& a) { + double s = now(); + double end = now() + SERVER_KNOBS->SPRING_CLEANING_TIME_ESTIMATE; + + int lazyDeletePages = 0; + int vacuumedPages = 0; + + double lazyDeleteTime = 0; + double vacuumTime = 0; + + const double lazyDeleteBatchProbability = 1.0 / (1 + SERVER_KNOBS->SPRING_CLEANING_VACUUMS_PER_LAZY_DELETE_PAGE * std::max(1, SERVER_KNOBS->SPRING_CLEANING_LAZY_DELETE_BATCH_SIZE)); + bool vacuumFinished = false; + + loop { + double begin = now(); + bool canDelete = !freeTableEmpty && (now() < end || lazyDeletePages < SERVER_KNOBS->SPRING_CLEANING_MIN_LAZY_DELETE_PAGES) && lazyDeletePages < SERVER_KNOBS->SPRING_CLEANING_MAX_LAZY_DELETE_PAGES; + bool canVacuum = !vacuumFinished && (now() < end || vacuumedPages < SERVER_KNOBS->SPRING_CLEANING_MIN_VACUUM_PAGES) && vacuumedPages < SERVER_KNOBS->SPRING_CLEANING_MAX_VACUUM_PAGES; + + if(!canDelete && !canVacuum) { + break; + } + + if(canDelete && (!canVacuum || g_random->random01() < lazyDeleteBatchProbability)) { + TEST(canVacuum); // SQLite lazy deletion when vacuuming is active + TEST(!canVacuum); // SQLite lazy deletion when vacuuming is inactive + + int pagesToDelete = std::max(1, std::min(SERVER_KNOBS->SPRING_CLEANING_LAZY_DELETE_BATCH_SIZE, SERVER_KNOBS->SPRING_CLEANING_MAX_LAZY_DELETE_PAGES - lazyDeletePages)); + int pagesDeleted = cursor->lazyDelete(pagesToDelete) ; + freeTableEmpty = (pagesDeleted != pagesToDelete); + lazyDeletePages += pagesDeleted; + lazyDeleteTime += now() - begin; + } + else { + ASSERT(canVacuum); + TEST(canDelete); // SQLite vacuuming when lazy delete is active + TEST(!canDelete); // SQLite vacuuming when lazy delete is inactive + TEST(SERVER_KNOBS->SPRING_CLEANING_VACUUMS_PER_LAZY_DELETE_PAGE != 0); //SQLite vacuuming with nonzero vacuums_per_lazy_delete_page + + vacuumFinished = conn.vacuum(); + if(!vacuumFinished) { + ++vacuumedPages; + } + + vacuumTime += now() - begin; + } + + CoroThreadPool::waitFor(yield()); + } + + freeListPages = conn.freePages(); + + TEST(lazyDeletePages > 0); // Pages lazily deleted + TEST(vacuumedPages > 0); // Pages vacuumed + TEST(vacuumTime > 0); // Time spent vacuuming + TEST(lazyDeleteTime > 0); // Time spent lazy deleting + + ++springCleaningStats.springCleaningCount; + springCleaningStats.lazyDeletePages += lazyDeletePages; + springCleaningStats.vacuumedPages += vacuumedPages; + springCleaningStats.springCleaningTime += now() - s; + springCleaningStats.vacuumTime += vacuumTime; + springCleaningStats.lazyDeleteTime += lazyDeleteTime; + + a.result.send(Void()); + ++writesComplete; + if (g_network->isSimulated() && g_simulator.getCurrentProcess()->rebooting) + TraceEvent("SpringCleaningActionFinished", dbgid).detail("Elapsed", now()-s); + } + }; + + + ACTOR static Future logPeriodically( KeyValueStoreSQLite* self ) { + state int64_t lastReadsComplete = 0; + state int64_t lastWritesComplete = 0; + loop { + Void _ = wait( delay(SERVER_KNOBS->DISK_METRIC_LOGGING_INTERVAL) ); + + int64_t rc = self->readsComplete, wc = self->writesComplete; + TraceEvent("DiskMetrics", self->logID) + .detail("ReadOps", rc - lastReadsComplete) + .detail("WriteOps", wc - lastWritesComplete) + .detail("ReadQueue", self->readsRequested - rc) + .detail("WriteQueue", self->writesRequested - wc) + .detail("GlobalSQLiteMemoryHighWater", (int64_t)sqlite3_memory_highwater(1)); + + TraceEvent("SpringCleaningMetrics", self->logID) + .detail("SpringCleaningCount", self->springCleaningStats.springCleaningCount) + .detail("LazyDeletePages", self->springCleaningStats.lazyDeletePages) + .detail("VacuumedPages", self->springCleaningStats.vacuumedPages) + .detail("SpringCleaningTime", self->springCleaningStats.springCleaningTime) + .detail("LazyDeleteTime", self->springCleaningStats.lazyDeleteTime) + .detail("VacuumTime", self->springCleaningStats.vacuumTime); + + lastReadsComplete = self->readsComplete; + lastWritesComplete = self->writesComplete; + } + } + + ACTOR static Future stopOnError( KeyValueStoreSQLite* self ) { + try { + Void _ = wait( self->readThreads->getError() || self->writeThread->getError() ); + } catch (Error& e) { + if (e.code() == error_code_actor_cancelled) + throw; + } + + self->readThreads->stop(); + self->writeThread->stop(); + return Void(); + } + + ACTOR static void doClose( KeyValueStoreSQLite* self, bool deleteOnClose ) { + state Error error = success(); + try { + TraceEvent("KVClose", self->logID).detail("Del", deleteOnClose); + self->starting.cancel(); + self->cleaning.cancel(); + self->logging.cancel(); + Void _ = wait( self->readThreads->stop() && self->writeThread->stop() ); + if (deleteOnClose) { + Void _ = wait( IAsyncFileSystem::filesystem()->deleteFile( self->filename, true ) ); + Void _ = wait( IAsyncFileSystem::filesystem()->deleteFile( self->filename + "-wal", false ) ); + } + } catch (Error& e) { + TraceEvent(SevError, "KVDoCloseError", self->logID) + .detail("Reason", e.code() == error_code_platform_error ? "could not delete database" : "unknown") + .error(e,true); + error = e; + } + + TraceEvent("KVClosed", self->logID); + if( error.code() != error_code_actor_cancelled ) { + self->stopped.send(Void()); + delete self; + } + } +}; +IKeyValueStore* keyValueStoreSQLite( std::string const& filename, UID logID, KeyValueStoreType storeType, bool validateFile ) { + return new KeyValueStoreSQLite(filename, logID, storeType, validateFile); +} + +ACTOR Future cleanPeriodically( KeyValueStoreSQLite* self ) { + loop { + Void _ = wait( delayJittered(SERVER_KNOBS->CLEANING_INTERVAL) ); + Void _ = wait( self->doClean() ); + } +} + +ACTOR static Future startReadThreadsWhen( KeyValueStoreSQLite* kv, Future onReady, UID id ) { + Void _ = wait(onReady); + kv->startReadThreads(); + return Void(); +} + +sqlite3_vfs *vfsAsync(); +static int vfs_registered = 0; + +KeyValueStoreSQLite::KeyValueStoreSQLite(std::string const& filename, UID id, KeyValueStoreType storeType, bool validateFile) + : type(storeType), + filename(filename), + logID(id), + readThreads(CoroThreadPool::createThreadPool()), + writeThread(CoroThreadPool::createThreadPool()), + readsRequested(0), writesRequested(0), writesComplete(0), diskBytesUsed(0), freeListPages(0), + validateFile(validateFile) +{ + stopOnErr = stopOnError(this); + + #if SQLITE_THREADSAFE == 0 + ASSERT( writeThread->isCoro() ); + #endif + + if (!vfs_registered && writeThread->isCoro()) + if (sqlite3_vfs_register( vfsAsync(), true ) != SQLITE_OK) + ASSERT(false); + + //The DB file should not already be open + ASSERT(!vfsAsyncIsOpen(filename)); + + readCursors.resize(64); //< number of read threads + + sqlite3_soft_heap_limit64( SERVER_KNOBS->SOFT_HEAP_LIMIT ); // SOMEDAY: Is this a performance issue? Should we drop the cache sizes for individual threads? + int taskId = g_network->getCurrentTask(); + g_network->setCurrentTask(TaskDiskWrite); + writeThread->addThread( new Writer(filename, type==KeyValueStoreType::SSD_BTREE_V2, validateFile, writesComplete, springCleaningStats, diskBytesUsed, freeListPages, id, &readCursors) ); + g_network->setCurrentTask(taskId); + auto p = new Writer::InitAction(); + auto f = p->result.getFuture(); + writeThread->post( p ); + starting = startReadThreadsWhen( this, f, logID ); + cleaning = cleanPeriodically(this); + logging = logPeriodically(this); +} +KeyValueStoreSQLite::~KeyValueStoreSQLite() { + //printf("dbf=%lld bytes, wal=%lld bytes\n", getFileSize((filename+".fdb").c_str()), getFileSize((filename+".fdb-wal").c_str())); +} + +StorageBytes KeyValueStoreSQLite::getStorageBytes() { + int64_t free; + int64_t total; + + g_network->getDiskBytes(parentDirectory(filename), free, total); + + return StorageBytes(free, total, diskBytesUsed, free + _PAGE_SIZE * freeListPages); +} + +void KeyValueStoreSQLite::startReadThreads() { + int nReadThreads = readCursors.size(); + int taskId = g_network->getCurrentTask(); + g_network->setCurrentTask(TaskDiskRead); + for(int i=0; iaddThread( new Reader(filename, type==KeyValueStoreType::SSD_BTREE_V2, readsComplete, logID, &readCursors[i]) ); + g_network->setCurrentTask(taskId); +} + +void KeyValueStoreSQLite::set( KeyValueRef keyValue, const Arena* arena ) { + ++writesRequested; + writeThread->post( new Writer::SetAction(keyValue) ); +} +void KeyValueStoreSQLite::clear( KeyRangeRef range, const Arena* arena ) { + ++writesRequested; + writeThread->post( new Writer::ClearAction(range) ); +} +Future KeyValueStoreSQLite::commit(bool sequential) { + ++writesRequested; + auto p = new Writer::CommitAction; + auto f = p->result.getFuture(); + writeThread->post(p); + return f; +} +Future> KeyValueStoreSQLite::readValue( KeyRef key, Optional debugID ) { + ++readsRequested; + auto p = new Reader::ReadValueAction(key, debugID); + auto f = p->result.getFuture(); + readThreads->post(p); + return f; +} +Future> KeyValueStoreSQLite::readValuePrefix( KeyRef key, int maxLength, Optional debugID ) { + ++readsRequested; + auto p = new Reader::ReadValuePrefixAction(key, maxLength, debugID); + auto f = p->result.getFuture(); + readThreads->post(p); + return f; +} +Future>> KeyValueStoreSQLite::readRange( KeyRangeRef keys, int rowLimit, int byteLimit ) { + ++readsRequested; + auto p = new Reader::ReadRangeAction(keys, rowLimit, byteLimit); + auto f = p->result.getFuture(); + readThreads->post(p); + return f; +} +Future KeyValueStoreSQLite::doClean() { + ++writesRequested; + auto p = new Writer::SpringCleaningAction; + auto f = p->result.getFuture(); + writeThread->post(p); + return f; +} + +void createTemplateDatabase() { + ASSERT( !vfs_registered ); + SQLiteDB db1("template.fdb", false, false); + SQLiteDB db2("template.sqlite", true, true); + db1.createFromScratch(); + db2.createFromScratch(); +} diff --git a/fdbserver/Knobs.cpp b/fdbserver/Knobs.cpp new file mode 100644 index 0000000000..f315410d89 --- /dev/null +++ b/fdbserver/Knobs.cpp @@ -0,0 +1,366 @@ +/* + * Knobs.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Knobs.h" +#include "fdbrpc/Locality.h" + +ServerKnobs const* SERVER_KNOBS = new ServerKnobs(); + +#define init( knob, value ) initKnob( knob, value, #knob ) + +ServerKnobs::ServerKnobs(bool randomize, ClientKnobs* clientKnobs) { + // TLogs + init( TLOG_TIMEOUT, 0.4 ); //cannot buggify because of availability + init( RECOVERY_TLOG_SMART_QUORUM_DELAY, 0.25 ); if( randomize && BUGGIFY ) RECOVERY_TLOG_SMART_QUORUM_DELAY = 0.0; // smaller might be better for bug amplification + init( TLOG_STORAGE_MIN_UPDATE_INTERVAL, 0.5 ); + init( BUGGIFY_TLOG_STORAGE_MIN_UPDATE_INTERVAL, 30 ); + init( UNFLUSHED_DATA_RATIO, 0.05 ); if( randomize && BUGGIFY ) UNFLUSHED_DATA_RATIO = 0.0; + init( DESIRED_TOTAL_BYTES, 150000 ); if( randomize && BUGGIFY ) DESIRED_TOTAL_BYTES = 10000; + init( MAXIMUM_PEEK_BYTES, 10e6 ); + init( APPLY_MUTATION_BYTES, 1e6 ); + init( RECOVERY_DATA_BYTE_LIMIT, 100000 ); + init( BUGGIFY_RECOVERY_DATA_LIMIT, 1000 ); + init( LONG_TLOG_COMMIT_TIME, 0.25 ); //cannot buggify because of recovery time + init( LARGE_TLOG_COMMIT_BYTES, 4<<20 ); + init( BUGGIFY_RECOVER_MEMORY_LIMIT, 1e6 ); + init( BUGGIFY_WORKER_REMOVED_MAX_LAG, 30 ); + init( UPDATE_STORAGE_BYTE_LIMIT, 1e6 ); + init( TLOG_PEEK_DELAY, 0.00005 ); + init( LEGACY_TLOG_UPGRADE_ENTRIES_PER_VERSION, 100 ); + init( VERSION_MESSAGES_OVERHEAD_FACTOR_1024THS, 1072 ); // Based on a naive interpretation of the gcc version of std::deque, we would expect this to be 16 bytes overhead per 512 bytes data. In practice, it seems to be 24 bytes overhead per 512. + init( LOG_SYSTEM_PUSHED_DATA_BLOCK_SIZE, 1e5 ); + init( MAX_MESSAGE_SIZE, std::max(LOG_SYSTEM_PUSHED_DATA_BLOCK_SIZE, 1e5 + 2e4 + 1) + 8 ); // VALUE_SIZE_LIMIT + SYSTEM_KEY_SIZE_LIMIT + 9 bytes (4 bytes for length, 4 bytes for sequence number, and 1 byte for mutation type) + init( TLOG_MESSAGE_BLOCK_BYTES, 10e6 ); + init( TLOG_MESSAGE_BLOCK_OVERHEAD_FACTOR, double(TLOG_MESSAGE_BLOCK_BYTES) / (TLOG_MESSAGE_BLOCK_BYTES - MAX_MESSAGE_SIZE) ); + init( PEEK_TRACKER_EXPIRATION_TIME, 600 ); if( randomize && BUGGIFY ) PEEK_TRACKER_EXPIRATION_TIME = 0.1; + init( PARALLEL_GET_MORE_REQUESTS, 32 ); if( randomize && BUGGIFY ) PARALLEL_GET_MORE_REQUESTS = 2; + init( MAX_QUEUE_COMMIT_BYTES, 15e6 ); if( randomize && BUGGIFY ) MAX_QUEUE_COMMIT_BYTES = 5000; + + // Versions + init( MAX_VERSIONS_IN_FLIGHT, 100000000 ); + init( VERSIONS_PER_SECOND, 1000000 ); + init( MAX_READ_TRANSACTION_LIFE_VERSIONS, 5 * VERSIONS_PER_SECOND ); if (randomize && BUGGIFY) MAX_READ_TRANSACTION_LIFE_VERSIONS=std::max(1, 0.1 * VERSIONS_PER_SECOND); else if( randomize && BUGGIFY ) MAX_READ_TRANSACTION_LIFE_VERSIONS = 10 * VERSIONS_PER_SECOND; + init( MAX_WRITE_TRANSACTION_LIFE_VERSIONS, 5 * VERSIONS_PER_SECOND ); if (randomize && BUGGIFY) MAX_WRITE_TRANSACTION_LIFE_VERSIONS=std::max(1, 0.5 * VERSIONS_PER_SECOND); + init( MAX_COMMIT_BATCH_INTERVAL, 0.5 ); if( randomize && BUGGIFY ) MAX_COMMIT_BATCH_INTERVAL = 2.0; // Each master proxy generates a CommitTransactionBatchRequest at least this often, so that versions always advance smoothly + + // Data distribution queue + init( HEALTH_POLL_TIME, 1.0 ); + init( BEST_TEAM_STUCK_DELAY, 1.0 ); + init( BG_DD_POLLING_INTERVAL, 10.0 ); + init( DD_QUEUE_LOGGING_INTERVAL, 5.0 ); + init( RELOCATION_PARALLELISM_PER_SOURCE_SERVER, 4 ); if( randomize && BUGGIFY ) RELOCATION_PARALLELISM_PER_SOURCE_SERVER = 1; + init( DD_QUEUE_MAX_KEY_SERVERS, 100 ); if( randomize && BUGGIFY ) DD_QUEUE_MAX_KEY_SERVERS = 1; + init( DD_REBALANCE_PARALLELISM, 50 ); + init( DD_REBALANCE_RESET_AMOUNT, 30 ); + init( BG_DD_MAX_WAIT, 120.0 ); + init( BG_DD_MIN_WAIT, 0.1 ); + init( BG_DD_INCREASE_RATE, 1.10 ); + init( BG_DD_DECREASE_RATE, 1.02 ); + init( BG_DD_SATURATION_DELAY, 1.0 ); + init( INFLIGHT_PENALTY_HEALTHY, 1.0 ); + init( INFLIGHT_PENALTY_UNHEALTHY, 10.0 ); + init( INFLIGHT_PENALTY_ONE_LEFT, 1000.0 ); + + // Data distribution + init( RETRY_RELOCATESHARD_DELAY, 0.1 ); + init( DATA_DISTRIBUTION_FAILURE_REACTION_TIME, 10.0 ); if( randomize && BUGGIFY ) DATA_DISTRIBUTION_FAILURE_REACTION_TIME = 1.0; + bool buggifySmallShards = randomize && BUGGIFY; + init( MIN_SHARD_BYTES, 200000 ); if( buggifySmallShards ) MIN_SHARD_BYTES = 40000; //FIXME: data distribution tracker (specifically StorageMetrics) relies on this number being larger than the maximum size of a key value pair + init( SHARD_BYTES_RATIO, 4 ); + init( SHARD_BYTES_PER_SQRT_BYTES, 45 ); if( buggifySmallShards ) SHARD_BYTES_PER_SQRT_BYTES = 0;//Approximately 10000 bytes per shard + init( MAX_SHARD_BYTES, 500000000 ); + bool buggifySmallBandwidthSplit = randomize && BUGGIFY; + init( SHARD_MAX_BYTES_PER_KSEC, 1LL*1000000*1000 ); if( buggifySmallBandwidthSplit ) SHARD_MAX_BYTES_PER_KSEC = 10LL*1000*1000; + /* 10*1MB/sec * 1000sec/ksec + Shards with more than this bandwidth will be split immediately. + For a large shard (100MB), splitting it costs ~100MB of work or about 10MB/sec over a 10 sec sampling window. + If the sampling window is too much longer, the MVCC window will fill up while we wait. + If SHARD_MAX_BYTES_PER_KSEC is too much lower, we could do a lot of data movement work in response to a small impulse of bandwidth. + If SHARD_MAX_BYTES_PER_KSEC is too high relative to the I/O bandwidth of a given server, a workload can remain concentrated on a single + team indefinitely, limiting performance. + */ + + init( SHARD_MIN_BYTES_PER_KSEC, 100 * 1000 * 1000 ); if( buggifySmallBandwidthSplit ) SHARD_MIN_BYTES_PER_KSEC = 200*1*1000; + /* 200*1KB/sec * 1000sec/ksec + Shards with more than this bandwidth will not be merged. + Obviously this needs to be significantly less than SHARD_MAX_BYTES_PER_KSEC, else we will repeatedly merge and split. + It should probably be significantly less than SHARD_SPLIT_BYTES_PER_KSEC, else we will merge right after splitting. + + The number of extra shards in the database because of bandwidth splitting can't be more than about W/SHARD_MIN_BYTES_PER_KSEC, where + W is the maximum bandwidth of the entire database in bytes/ksec. For 250MB/sec write bandwidth, (250MB/sec)/(200KB/sec) = 1250 extra + shards. + + The bandwidth sample maintained by the storage server needs to be accurate enough to reliably measure this minimum bandwidth. See + BANDWIDTH_UNITS_PER_SAMPLE. If this number is too low, the storage server needs to spend more memory and time on sampling. + */ + + init( SHARD_SPLIT_BYTES_PER_KSEC, 250 * 1000 * 1000 ); if( buggifySmallBandwidthSplit ) SHARD_SPLIT_BYTES_PER_KSEC = 50 * 1000 * 1000; + /* 500*1KB/sec * 1000sec/ksec + When splitting a shard, it is split into pieces with less than this bandwidth. + Obviously this should be less than half of SHARD_MAX_BYTES_PER_KSEC. + + Smaller values mean that high bandwidth shards are split into more pieces, more quickly utilizing large numbers of servers to handle the + bandwidth. + + Too many pieces (too small a value) may stress data movement mechanisms (see e.g. RELOCATION_PARALLELISM_PER_SOURCE_SERVER). + + If this value is too small relative to SHARD_MIN_BYTES_PER_KSEC immediate merging work will be generated. + */ + + init( STORAGE_METRIC_TIMEOUT, 600.0 ); if( randomize && BUGGIFY ) STORAGE_METRIC_TIMEOUT = 10.0; + init( METRIC_DELAY, 0.1 ); if( randomize && BUGGIFY ) METRIC_DELAY = 1.0; + init( ALL_DATA_REMOVED_DELAY, 1.0 ); + init( INITIAL_FAILURE_REACTION_DELAY, 30.0 ); if( randomize && BUGGIFY ) INITIAL_FAILURE_REACTION_DELAY = 0.0; + init( LOG_ON_COMPLETION_DELAY, DD_QUEUE_LOGGING_INTERVAL ); + init( BEST_TEAM_MAX_TEAM_TRIES, 10 ); + init( BEST_TEAM_OPTION_COUNT, 4 ); + init( BEST_OF_AMT, 4 ); + init( SERVER_LIST_DELAY, 1.0 ); + init( RECRUITMENT_IDLE_DELAY, 1.0 ); + init( STORAGE_RECRUITMENT_DELAY, 0.5 ); + init( DATA_DISTRIBUTION_LOGGING_INTERVAL, 5.0 ); + init( DD_ENABLED_CHECK_DELAY, 1.0 ); + init( DD_MERGE_COALESCE_DELAY, 120.0 ); if( randomize && BUGGIFY ) DD_MERGE_COALESCE_DELAY = 0.001; + init( STORAGE_METRICS_POLLING_DELAY, 2.0 ); if( randomize && BUGGIFY ) STORAGE_METRICS_POLLING_DELAY = 15.0; + init( STORAGE_METRICS_RANDOM_DELAY, 0.2 ); + init( FREE_SPACE_RATIO_CUTOFF, 0.1 ); + init( FREE_SPACE_RATIO_DD_CUTOFF, 0.2 ); + init( DESIRED_TEAMS_PER_SERVER, 5 ); if( randomize && BUGGIFY ) DESIRED_TEAMS_PER_SERVER = 1; + init( DD_SHARD_SIZE_GRANULARITY, 5000000 ); + init( DD_SHARD_SIZE_GRANULARITY_SIM, 500000 ); if( randomize && BUGGIFY ) DD_SHARD_SIZE_GRANULARITY_SIM = 0; + init( DD_MOVE_KEYS_PARALLELISM, 20 ); if( randomize && BUGGIFY ) DD_MOVE_KEYS_PARALLELISM = 1; + init( DD_MERGE_LIMIT, 2000 ); if( randomize && BUGGIFY ) DD_MERGE_LIMIT = 2; + init( DD_SHARD_METRICS_TIMEOUT, 60.0 ); if( randomize && BUGGIFY ) DD_SHARD_METRICS_TIMEOUT = 0.1; + init( DD_LOCATION_CACHE_SIZE, 2000000 ); if( randomize && BUGGIFY ) DD_LOCATION_CACHE_SIZE = 3; + init( MOVEKEYS_LOCK_POLLING_DELAY, 5.0 ); + init( DEBOUNCE_RECRUITING_DELAY, 5.0 ); + + // KeyValueStore SQLITE + init( CLEAR_BUFFER_SIZE, 20000 ); + init( READ_VALUE_TIME_ESTIMATE, .00005 ); + init( READ_RANGE_TIME_ESTIMATE, .00005 ); + init( SET_TIME_ESTIMATE, .00005 ); + init( CLEAR_TIME_ESTIMATE, .00005 ); + init( COMMIT_TIME_ESTIMATE, .005 ); + init( CHECK_FREE_PAGE_AMOUNT, 100 ); if( randomize && BUGGIFY ) CHECK_FREE_PAGE_AMOUNT = 5; + init( DISK_METRIC_LOGGING_INTERVAL, 5.0 ); + init( SOFT_HEAP_LIMIT, 300e6 ); + + init( SQLITE_PAGER_CHECKSUM_HISTORY, 0 ); + init( SQLITE_PAGE_SCAN_ERROR_LIMIT, 10000 ); + init( SQLITE_BTREE_PAGE_USABLE, 4096 - 8); // pageSize - reserveSize for page checksum + + // Maximum and minimum cell payload bytes allowed on primary page as calculated in SQLite. + // These formulas are copied from SQLite, using its hardcoded constants, so if you are + // changing this you should also be changing SQLite. + init( SQLITE_BTREE_CELL_MAX_LOCAL, (SQLITE_BTREE_PAGE_USABLE - 12) * 64/255 - 23 ); + init( SQLITE_BTREE_CELL_MIN_LOCAL, (SQLITE_BTREE_PAGE_USABLE - 12) * 32/255 - 23 ); + + // Maximum FDB fragment key and value bytes that can fit in a primary btree page + init( SQLITE_FRAGMENT_PRIMARY_PAGE_USABLE, + SQLITE_BTREE_CELL_MAX_LOCAL + - 1 // vdbeRecord header length size + - 2 // max key length size + - 4 // max index length size + - 2 // max value fragment length size + ); + + // Maximum FDB fragment value bytes in an overflow page + init( SQLITE_FRAGMENT_OVERFLOW_PAGE_USABLE, + SQLITE_BTREE_PAGE_USABLE + - 4 // next pageNumber size + ); + init( SQLITE_FRAGMENT_MIN_SAVINGS, 0.20 ); + + // KeyValueStoreSqlite spring cleaning + init( CLEANING_INTERVAL, 1.0 ); + init( SPRING_CLEANING_TIME_ESTIMATE, .010 ); + init( SPRING_CLEANING_VACUUMS_PER_LAZY_DELETE_PAGE, 0.0 ); if( randomize && BUGGIFY ) SPRING_CLEANING_VACUUMS_PER_LAZY_DELETE_PAGE = g_random->coinflip() ? 1e9 : g_random->random01() * 5; + init( SPRING_CLEANING_MIN_LAZY_DELETE_PAGES, 0 ); if( randomize && BUGGIFY ) SPRING_CLEANING_MIN_LAZY_DELETE_PAGES = g_random->randomInt(1, 100); + init( SPRING_CLEANING_MAX_LAZY_DELETE_PAGES, 1e9 ); if( randomize && BUGGIFY ) SPRING_CLEANING_MAX_LAZY_DELETE_PAGES = g_random->coinflip() ? 0 : g_random->randomInt(1, 1e4); + init( SPRING_CLEANING_LAZY_DELETE_BATCH_SIZE, 100 ); if( randomize && BUGGIFY ) SPRING_CLEANING_LAZY_DELETE_BATCH_SIZE = g_random->randomInt(1, 1000); + init( SPRING_CLEANING_MIN_VACUUM_PAGES, 1 ); if( randomize && BUGGIFY ) SPRING_CLEANING_MIN_VACUUM_PAGES = g_random->randomInt(0, 100); + init( SPRING_CLEANING_MAX_VACUUM_PAGES, 1e9 ); if( randomize && BUGGIFY ) SPRING_CLEANING_MAX_VACUUM_PAGES = g_random->coinflip() ? 0 : g_random->randomInt(1, 1e4); + + // Leader election + bool longLeaderElection = randomize && BUGGIFY; + init( CANDIDATE_MIN_DELAY, 0.05 ); + init( CANDIDATE_MAX_DELAY, 1.0 ); + init( CANDIDATE_GROWTH_RATE, 1.2 ); + init( POLLING_FREQUENCY, 1.0 ); if( longLeaderElection ) POLLING_FREQUENCY = 8.0; + init( HEARTBEAT_FREQUENCY, 0.25 ); if( longLeaderElection ) HEARTBEAT_FREQUENCY = 1.0; + + // Master Proxy + init( START_TRANSACTION_BATCH_INTERVAL_MIN, 1e-6 ); + init( START_TRANSACTION_BATCH_INTERVAL_MAX, 0.010 ); + init( START_TRANSACTION_BATCH_INTERVAL_LATENCY_FRACTION, 0.5 ); + init( START_TRANSACTION_BATCH_INTERVAL_SMOOTHER_ALPHA, 0.1 ); + init( START_TRANSACTION_BATCH_QUEUE_CHECK_INTERVAL, 0.001 ); + init( START_TRANSACTION_MAX_TRANSACTIONS_TO_START, 10000 ); + init( START_TRANSACTION_MAX_BUDGET_SIZE, 20 ); // Currently set to match CLIENT_KNOBS->MAX_BATCH_SIZE + + init( COMMIT_TRANSACTION_BATCH_INTERVAL_FROM_IDLE, 0.0005 ); if( randomize && BUGGIFY ) COMMIT_TRANSACTION_BATCH_INTERVAL_FROM_IDLE = 0.005; + init( COMMIT_TRANSACTION_BATCH_INTERVAL_MIN, 0.001 ); if( randomize && BUGGIFY ) COMMIT_TRANSACTION_BATCH_INTERVAL_MIN = 0.1; + init( COMMIT_TRANSACTION_BATCH_INTERVAL_MAX, 0.020 ); + init( COMMIT_TRANSACTION_BATCH_INTERVAL_LATENCY_FRACTION, 0.1 ); + init( COMMIT_TRANSACTION_BATCH_INTERVAL_SMOOTHER_ALPHA, 0.1 ); + init( COMMIT_TRANSACTION_BATCH_COUNT_MAX, 32768 ); if( randomize && BUGGIFY ) COMMIT_TRANSACTION_BATCH_COUNT_MAX = 1000; // Do NOT increase this number beyond 32768, as CommitIds only budget 2 bytes for storing transaction id within each batch + + // these settings disable batch bytes scaling. Try COMMIT_TRANSACTION_BATCH_BYTES_MAX=1e6, COMMIT_TRANSACTION_BATCH_BYTES_SCALE_BASE=50000, COMMIT_TRANSACTION_BATCH_BYTES_SCALE_POWER=0.5? + init( COMMIT_TRANSACTION_BATCH_BYTES_MIN, 100000 ); + init( COMMIT_TRANSACTION_BATCH_BYTES_MAX, 100000 ); if( randomize && BUGGIFY ) { COMMIT_TRANSACTION_BATCH_BYTES_MIN = COMMIT_TRANSACTION_BATCH_BYTES_MAX = 1000000; } + init( COMMIT_TRANSACTION_BATCH_BYTES_SCALE_BASE, 100000 ); + init( COMMIT_TRANSACTION_BATCH_BYTES_SCALE_POWER, 0.0 ); + + init( TRANSACTION_BUDGET_TIME, 0.050 ); if( randomize && BUGGIFY ) TRANSACTION_BUDGET_TIME = 0.0; + init( RESOLVER_COALESCE_TIME, 1.0 ); + init( BUGGIFIED_ROW_LIMIT, APPLY_MUTATION_BYTES ); if( randomize && BUGGIFY ) BUGGIFIED_ROW_LIMIT = g_random->randomInt(3, 30); + init( PROXY_SPIN_DELAY, 0.01 ); + + // Master Server + init( MASTER_LOGGING_DELAY, 1.0 ); + // masterCommitter() in the master server will allow lower priority tasks (e.g. DataDistibution) + // by delay()ing for this amount of time between accepted batches of TransactionRequests. + init( COMMIT_SLEEP_TIME, 0.0001 ); if( randomize && BUGGIFY ) COMMIT_SLEEP_TIME = 0; + init( MIN_BALANCE_TIME, 0.2 ); + init( MIN_BALANCE_DIFFERENCE, 10000 ); + init( SECONDS_BEFORE_NO_FAILURE_DELAY, 8 * 3600 ); + init( MAX_TXS_SEND_MEMORY, 1e7 ); if( randomize && BUGGIFY ) MAX_TXS_SEND_MEMORY = 1e5; + + // Resolver + init( SAMPLE_OFFSET_PER_KEY, 100 ); + init( SAMPLE_EXPIRATION_TIME, 1.0 ); + init( SAMPLE_POLL_TIME, 0.1 ); + init( RESOLVER_STATE_MEMORY_LIMIT, 1e6 ); + init( LAST_LIMITED_RATIO, 0.6 ); + + //Cluster Controller + init( MASTER_FAILURE_REACTION_TIME, 0.4 ); if( randomize && BUGGIFY ) MASTER_FAILURE_REACTION_TIME = 10.0; + init( MASTER_FAILURE_SLOPE_DURING_RECOVERY, 0.1 ); + init( WORKER_COORDINATION_PING_DELAY, 60 ); + init( SIM_SHUTDOWN_TIMEOUT, 10 ); + init( SHUTDOWN_TIMEOUT, 600 ); + init( MASTER_SPIN_DELAY, 1.0 ); if( randomize && BUGGIFY ) MASTER_SPIN_DELAY = 10.0; + init( WAIT_FOR_GOOD_RECRUITMENT_DELAY, 1.0 ); + init( ATTEMPT_RECRUITMENT_DELAY, 0.05 ); + init( WORKER_FAILURE_TIME, 1.0 ); if( randomize && BUGGIFY ) WORKER_FAILURE_TIME = 10.0; + init( CHECK_BETTER_MASTER_INTERVAL, 1.0 ); if( randomize && BUGGIFY ) CHECK_BETTER_MASTER_INTERVAL = 0.001; + init( INCOMPATIBLE_PEERS_LOGGING_INTERVAL, 600 ); + init( EXPECTED_MASTER_FITNESS, ProcessClass::GoodFit ); + init( EXPECTED_TLOG_FITNESS, ProcessClass::GoodFit ); + init( EXPECTED_PROXY_FITNESS, ProcessClass::GoodFit ); + init( EXPECTED_RESOLVER_FITNESS, ProcessClass::GoodFit ); + init( RECRUITMENT_TIMEOUT, 600 ); if( randomize && BUGGIFY ) RECRUITMENT_TIMEOUT = 1.0; + + init( POLICY_RATING_TESTS, 200 ); if( randomize && BUGGIFY ) POLICY_RATING_TESTS = 20; + init( POLICY_GENERATIONS, 100 ); if( randomize && BUGGIFY ) POLICY_GENERATIONS = 10; + + //Move Keys + init( SHARD_READY_DELAY, 0.25 ); + init( SERVER_READY_QUORUM_INTERVAL, std::min(1.0, std::min(MAX_READ_TRANSACTION_LIFE_VERSIONS, MAX_WRITE_TRANSACTION_LIFE_VERSIONS)/(5.0*VERSIONS_PER_SECOND)) ); + init( SERVER_READY_QUORUM_TIMEOUT, 15.0 ); if( randomize && BUGGIFY ) SERVER_READY_QUORUM_TIMEOUT = 1.0; + init( REMOVE_RETRY_DELAY, 1.0 ); + init( MOVE_KEYS_KRM_LIMIT, 2000 ); if( randomize && BUGGIFY ) MOVE_KEYS_KRM_LIMIT = 2; + init( MOVE_KEYS_KRM_LIMIT_BYTES, 1e5 ); if( randomize && BUGGIFY ) MOVE_KEYS_KRM_LIMIT_BYTES = 5e4; //This must be sufficiently larger than CLIENT_KNOBS->KEY_SIZE_LIMIT (fdbclient/Knobs.h) to ensure that at least two entries will be returned from an attempt to read a key range map + init( SKIP_TAGS_GROWTH_RATE, 2.0 ); + init( MAX_SKIP_TAGS, 100 ); + + + //FdbServer + bool longReboots = randomize && BUGGIFY; + init( MIN_REBOOT_TIME, 4.0 ); if( longReboots ) MIN_REBOOT_TIME = 10.0; + init( MAX_REBOOT_TIME, 5.0 ); if( longReboots ) MAX_REBOOT_TIME = 20.0; + + //Ratekeeper + bool slowRateKeeper = randomize && BUGGIFY; + init( SMOOTHING_AMOUNT, 1.0 ); if( slowRateKeeper ) SMOOTHING_AMOUNT = 5.0; + init( SLOW_SMOOTHING_AMOUNT, 10.0 ); if( slowRateKeeper ) SLOW_SMOOTHING_AMOUNT = 50.0; + init( RATEKEEPER_LOGGING_INTERVAL, 5.0 ); + init( METRIC_UPDATE_RATE, .1 ); if( slowRateKeeper ) METRIC_UPDATE_RATE = 0.5; + + bool smallStorageTarget = randomize && BUGGIFY; + init( TARGET_BYTES_PER_STORAGE_SERVER, 1000e6 ); if( smallStorageTarget ) TARGET_BYTES_PER_STORAGE_SERVER = 1000e3; + init( SPRING_BYTES_STORAGE_SERVER, 100e6 ); if( smallStorageTarget ) SPRING_BYTES_STORAGE_SERVER = 100e3; + + bool smallTlogTarget = randomize && BUGGIFY; + init( TARGET_BYTES_PER_TLOG, 2000e6 ); if( smallTlogTarget ) TARGET_BYTES_PER_TLOG = 2000e3; + init( SPRING_BYTES_TLOG, 400e6 ); if( smallTlogTarget ) SPRING_BYTES_TLOG = 200e3; + init( TLOG_SPILL_THRESHOLD, 1500e6 ); if( smallTlogTarget ) TLOG_SPILL_THRESHOLD = 1500e3; if( randomize && BUGGIFY ) TLOG_SPILL_THRESHOLD = 0; + + init( MAX_TRANSACTIONS_PER_BYTE, 1000 ); + + init( MIN_FREE_SPACE, 1e8 ); + init( MIN_FREE_SPACE_RATIO, 0.05 ); + + init( MAX_TL_SS_VERSION_DIFFERENCE, 1e99 ); // if( randomize && BUGGIFY ) MAX_TL_SS_VERSION_DIFFERENCE = std::max(1.0, 0.25 * VERSIONS_PER_SECOND); // spring starts at half this value //FIXME: this knob causes ratekeeper to clamp on idle cluster in simulation that have a large number of logs + init( MAX_MACHINES_FALLING_BEHIND, 1 ); + + //Storage Metrics + init( STORAGE_METRICS_AVERAGE_INTERVAL, 120.0 ); + init( STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS, 1000.0 / STORAGE_METRICS_AVERAGE_INTERVAL ); // milliHz! + init( SPLIT_JITTER_AMOUNT, 0.05 ); if( randomize && BUGGIFY ) SPLIT_JITTER_AMOUNT = 0.2; + init( IOPS_UNITS_PER_SAMPLE, 10000 * 1000 / STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS / 100 ); + init( BANDWIDTH_UNITS_PER_SAMPLE, SHARD_MIN_BYTES_PER_KSEC / STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS / 25 ); + + //Storage Server + init( STORAGE_LOGGING_DELAY, 5.0 ); + init( STORAGE_SERVER_POLL_METRICS_DELAY, 1.0 ); + init( FUTURE_VERSION_DELAY, 1.0 ); if( randomize && BUGGIFY ) FUTURE_VERSION_DELAY = 0.001; + init( STORAGE_LIMIT_BYTES, 500000 ); + init( BUGGIFY_LIMIT_BYTES, 1000 ); + init( FETCH_BLOCK_BYTES, 2e6 ); + init( FETCH_KEYS_PARALLELISM_BYTES, 5e6 ); if( randomize && BUGGIFY ) FETCH_KEYS_PARALLELISM_BYTES = 4e6; + init( BUGGIFY_BLOCK_BYTES, 10000 ); + init( STORAGE_HARD_LIMIT_BYTES, 1500e6 ); if( randomize && BUGGIFY ) STORAGE_HARD_LIMIT_BYTES = 1500e3; + init( STORAGE_COMMIT_BYTES, 10000000 ); if( randomize && BUGGIFY ) STORAGE_COMMIT_BYTES = 2000000; + init( STORAGE_COMMIT_INTERVAL, 0.5 ); if( randomize && BUGGIFY ) STORAGE_COMMIT_INTERVAL = 2.0; + init( UPDATE_SHARD_VERSION_INTERVAL, 0.25 ); if( randomize && BUGGIFY ) UPDATE_SHARD_VERSION_INTERVAL = 1.0; + init( BYTE_SAMPLING_FACTOR, 250 ); //cannot buggify because of differences in restarting tests + init( BYTE_SAMPLING_OVERHEAD, 100 ); + init( MAX_STORAGE_SERVER_WATCH_BYTES, 100e6 ); if( randomize && BUGGIFY ) MAX_STORAGE_SERVER_WATCH_BYTES = 10e3; + init( MAX_BYTE_SAMPLE_CLEAR_MAP_SIZE, 1e9 ); if( randomize && BUGGIFY ) MAX_BYTE_SAMPLE_CLEAR_MAP_SIZE = 1e3; + init( LONG_BYTE_SAMPLE_RECOVERY_DELAY, 60.0 ); + + //Wait Failure + init( BUGGIFY_OUTSTANDING_WAIT_FAILURE_REQUESTS, 2 ); + init( MAX_OUTSTANDING_WAIT_FAILURE_REQUESTS, 250 ); if( randomize && BUGGIFY ) MAX_OUTSTANDING_WAIT_FAILURE_REQUESTS = 2; + init( WAIT_FAILURE_DELAY_LIMIT, 1.0 ); if( randomize && BUGGIFY ) WAIT_FAILURE_DELAY_LIMIT = 5.0; + + //Worker + init( WORKER_LOGGING_INTERVAL, 5.0 ); + init( INCOMPATIBLE_PEER_DELAY_BEFORE_LOGGING, 5.0 ); + + // Test harness + init( WORKER_POLL_DELAY, 1.0 ); + + // Coordination + init( COORDINATED_STATE_ONCONFLICT_POLL_INTERVAL, 1.0 ); if( randomize && BUGGIFY ) COORDINATED_STATE_ONCONFLICT_POLL_INTERVAL = 10.0; + + // Buggification + init( BUGGIFIED_EVENTUAL_CONSISTENCY, 1.0 ); + BUGGIFY_ALL_COORDINATION = false; if( randomize && BUGGIFY ) { BUGGIFY_ALL_COORDINATION = true; TraceEvent("BuggifyAllCoordination"); } + + // Status + init( STATUS_MIN_TIME_BETWEEN_REQUESTS, 0.0 ); + init( CONFIGURATION_ROWS_TO_FETCH, 20000 ); + + if(clientKnobs) + clientKnobs->IS_ACCEPTABLE_DELAY = clientKnobs->IS_ACCEPTABLE_DELAY*std::min(MAX_READ_TRANSACTION_LIFE_VERSIONS, MAX_WRITE_TRANSACTION_LIFE_VERSIONS)/(5.0*VERSIONS_PER_SECOND); +} diff --git a/fdbserver/Knobs.h b/fdbserver/Knobs.h new file mode 100644 index 0000000000..c89b94e7b5 --- /dev/null +++ b/fdbserver/Knobs.h @@ -0,0 +1,311 @@ +/* + * Knobs.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_KNOBS_H +#define FDBSERVER_KNOBS_H +#pragma once + +#include "flow/Knobs.h" +#include "fdbrpc/fdbrpc.h" +#include "fdbclient/Knobs.h" + +// Disk queue +static const int _PAGE_SIZE = 4096; + +class ServerKnobs : public Knobs { +public: + // TLogs + double TLOG_TIMEOUT; // tlog OR master proxy failure - master's reaction time + double RECOVERY_TLOG_SMART_QUORUM_DELAY; // smaller might be better for bug amplification + double TLOG_STORAGE_MIN_UPDATE_INTERVAL; + double BUGGIFY_TLOG_STORAGE_MIN_UPDATE_INTERVAL; + double UNFLUSHED_DATA_RATIO; + int DESIRED_TOTAL_BYTES; + int MAXIMUM_PEEK_BYTES; + int APPLY_MUTATION_BYTES; + int RECOVERY_DATA_BYTE_LIMIT; + int BUGGIFY_RECOVERY_DATA_LIMIT; + double LONG_TLOG_COMMIT_TIME; + int64_t LARGE_TLOG_COMMIT_BYTES; + double BUGGIFY_RECOVER_MEMORY_LIMIT; + double BUGGIFY_WORKER_REMOVED_MAX_LAG; + int64_t UPDATE_STORAGE_BYTE_LIMIT; + double TLOG_PEEK_DELAY; + int LEGACY_TLOG_UPGRADE_ENTRIES_PER_VERSION; + int VERSION_MESSAGES_OVERHEAD_FACTOR_1024THS; // Multiplicative factor to bound total space used to store a version message (measured in 1/1024ths, e.g. a value of 2048 yields a factor of 2). + double TLOG_MESSAGE_BLOCK_OVERHEAD_FACTOR; + int64_t TLOG_MESSAGE_BLOCK_BYTES; + int64_t MAX_MESSAGE_SIZE; + int LOG_SYSTEM_PUSHED_DATA_BLOCK_SIZE; + double PEEK_TRACKER_EXPIRATION_TIME; + int PARALLEL_GET_MORE_REQUESTS; + int64_t MAX_QUEUE_COMMIT_BYTES; + + // Versions + int MAX_VERSIONS_IN_FLIGHT; + int MAX_READ_TRANSACTION_LIFE_VERSIONS; + int MAX_WRITE_TRANSACTION_LIFE_VERSIONS; + + int VERSIONS_PER_SECOND; + double MAX_COMMIT_BATCH_INTERVAL; // Each master proxy generates a CommitTransactionBatchRequest at least this often, so that versions always advance smoothly + + // Data distribution queue + double HEALTH_POLL_TIME; + double BEST_TEAM_STUCK_DELAY; + double BG_DD_POLLING_INTERVAL; + double DD_QUEUE_LOGGING_INTERVAL; + double RELOCATION_PARALLELISM_PER_SOURCE_SERVER; + int DD_QUEUE_MAX_KEY_SERVERS; + int DD_REBALANCE_PARALLELISM; + int DD_REBALANCE_RESET_AMOUNT; + double BG_DD_MAX_WAIT; + double BG_DD_MIN_WAIT; + double BG_DD_INCREASE_RATE; + double BG_DD_DECREASE_RATE; + double BG_DD_SATURATION_DELAY; + double INFLIGHT_PENALTY_HEALTHY; + double INFLIGHT_PENALTY_UNHEALTHY; + double INFLIGHT_PENALTY_ONE_LEFT; + + // Data distribution + double RETRY_RELOCATESHARD_DELAY; + double DATA_DISTRIBUTION_FAILURE_REACTION_TIME; + int MIN_SHARD_BYTES, SHARD_BYTES_RATIO, SHARD_BYTES_PER_SQRT_BYTES, MAX_SHARD_BYTES; + int64_t SHARD_MAX_BYTES_PER_KSEC, // Shards with more than this bandwidth will be split immediately + SHARD_MIN_BYTES_PER_KSEC, // Shards with more than this bandwidth will not be merged + SHARD_SPLIT_BYTES_PER_KSEC; // When splitting a shard, it is split into pieces with less than this bandwidth + double STORAGE_METRIC_TIMEOUT; + double METRIC_DELAY; + double ALL_DATA_REMOVED_DELAY; + double INITIAL_FAILURE_REACTION_DELAY; + double LOG_ON_COMPLETION_DELAY; + int BEST_TEAM_MAX_TEAM_TRIES; + int BEST_TEAM_OPTION_COUNT; + int BEST_OF_AMT; + double SERVER_LIST_DELAY; + double RECRUITMENT_IDLE_DELAY; + double STORAGE_RECRUITMENT_DELAY; + double DATA_DISTRIBUTION_LOGGING_INTERVAL; + double DD_ENABLED_CHECK_DELAY; + double DD_MERGE_COALESCE_DELAY; + double STORAGE_METRICS_POLLING_DELAY; + double STORAGE_METRICS_RANDOM_DELAY; + double FREE_SPACE_RATIO_CUTOFF; + double FREE_SPACE_RATIO_DD_CUTOFF; + int DESIRED_TEAMS_PER_SERVER; + int64_t DD_SHARD_SIZE_GRANULARITY; + int64_t DD_SHARD_SIZE_GRANULARITY_SIM; + int DD_MOVE_KEYS_PARALLELISM; + int DD_MERGE_LIMIT; + double DD_SHARD_METRICS_TIMEOUT; + int64_t DD_LOCATION_CACHE_SIZE; + double MOVEKEYS_LOCK_POLLING_DELAY; + double DEBOUNCE_RECRUITING_DELAY; + + // KeyValueStore SQLITE + int CLEAR_BUFFER_SIZE; + double READ_VALUE_TIME_ESTIMATE; + double READ_RANGE_TIME_ESTIMATE; + double SET_TIME_ESTIMATE; + double CLEAR_TIME_ESTIMATE; + double COMMIT_TIME_ESTIMATE; + int CHECK_FREE_PAGE_AMOUNT; + double DISK_METRIC_LOGGING_INTERVAL; + int64_t SOFT_HEAP_LIMIT; + + int SQLITE_PAGE_SCAN_ERROR_LIMIT; + int SQLITE_BTREE_PAGE_USABLE; + int SQLITE_BTREE_CELL_MAX_LOCAL; + int SQLITE_BTREE_CELL_MIN_LOCAL; + int SQLITE_FRAGMENT_PRIMARY_PAGE_USABLE; + int SQLITE_FRAGMENT_OVERFLOW_PAGE_USABLE; + double SQLITE_FRAGMENT_MIN_SAVINGS; + int SQLITE_PAGER_CHECKSUM_HISTORY; + + // KeyValueStoreSqlite spring cleaning + double CLEANING_INTERVAL; + double SPRING_CLEANING_TIME_ESTIMATE; + double SPRING_CLEANING_VACUUMS_PER_LAZY_DELETE_PAGE; + int SPRING_CLEANING_MIN_LAZY_DELETE_PAGES; + int SPRING_CLEANING_MAX_LAZY_DELETE_PAGES; + int SPRING_CLEANING_LAZY_DELETE_BATCH_SIZE; + int SPRING_CLEANING_MIN_VACUUM_PAGES; + int SPRING_CLEANING_MAX_VACUUM_PAGES; + + // Leader election + double CANDIDATE_MIN_DELAY; + double CANDIDATE_MAX_DELAY; + double CANDIDATE_GROWTH_RATE; + double POLLING_FREQUENCY; + double HEARTBEAT_FREQUENCY; + + // Master Proxy + double START_TRANSACTION_BATCH_INTERVAL_MIN; + double START_TRANSACTION_BATCH_INTERVAL_MAX; + double START_TRANSACTION_BATCH_INTERVAL_LATENCY_FRACTION; + double START_TRANSACTION_BATCH_INTERVAL_SMOOTHER_ALPHA; + double START_TRANSACTION_BATCH_QUEUE_CHECK_INTERVAL; + double START_TRANSACTION_MAX_TRANSACTIONS_TO_START; + double START_TRANSACTION_MAX_BUDGET_SIZE; + + double COMMIT_TRANSACTION_BATCH_INTERVAL_FROM_IDLE; + double COMMIT_TRANSACTION_BATCH_INTERVAL_MIN; + double COMMIT_TRANSACTION_BATCH_INTERVAL_MAX; + double COMMIT_TRANSACTION_BATCH_INTERVAL_LATENCY_FRACTION; + double COMMIT_TRANSACTION_BATCH_INTERVAL_SMOOTHER_ALPHA; + int COMMIT_TRANSACTION_BATCH_COUNT_MAX; + int COMMIT_TRANSACTION_BATCH_BYTES_MIN; + int COMMIT_TRANSACTION_BATCH_BYTES_MAX; + double COMMIT_TRANSACTION_BATCH_BYTES_SCALE_BASE; + double COMMIT_TRANSACTION_BATCH_BYTES_SCALE_POWER; + + double TRANSACTION_BUDGET_TIME; + double RESOLVER_COALESCE_TIME; + int BUGGIFIED_ROW_LIMIT; + double PROXY_SPIN_DELAY; + + // Master Server + double MASTER_LOGGING_DELAY; + double COMMIT_SLEEP_TIME; + double MIN_BALANCE_TIME; + int64_t MIN_BALANCE_DIFFERENCE; + double SECONDS_BEFORE_NO_FAILURE_DELAY; + int64_t MAX_TXS_SEND_MEMORY; + + // Resolver + int64_t SAMPLE_OFFSET_PER_KEY; + double SAMPLE_EXPIRATION_TIME; + double SAMPLE_POLL_TIME; + int64_t RESOLVER_STATE_MEMORY_LIMIT; + + //Cluster Controller + double MASTER_FAILURE_REACTION_TIME; + double MASTER_FAILURE_SLOPE_DURING_RECOVERY; + int WORKER_COORDINATION_PING_DELAY; + double SIM_SHUTDOWN_TIMEOUT; + double SHUTDOWN_TIMEOUT; + double MASTER_SPIN_DELAY; + double WAIT_FOR_GOOD_RECRUITMENT_DELAY; + double ATTEMPT_RECRUITMENT_DELAY; + double WORKER_FAILURE_TIME; + double CHECK_BETTER_MASTER_INTERVAL; + double INCOMPATIBLE_PEERS_LOGGING_INTERVAL; + + // Knobs used to select the best policy (via monte carlo) + int POLICY_RATING_TESTS; // number of tests per policy (in order to compare) + int POLICY_GENERATIONS; // number of policies to generate + + int EXPECTED_MASTER_FITNESS; + int EXPECTED_TLOG_FITNESS; + int EXPECTED_PROXY_FITNESS; + int EXPECTED_RESOLVER_FITNESS; + double RECRUITMENT_TIMEOUT; + + //Move Keys + double SHARD_READY_DELAY; + double SERVER_READY_QUORUM_INTERVAL; + double SERVER_READY_QUORUM_TIMEOUT; + double REMOVE_RETRY_DELAY; + int MOVE_KEYS_KRM_LIMIT; + int MOVE_KEYS_KRM_LIMIT_BYTES; //This must be sufficiently larger than CLIENT_KNOBS->KEY_SIZE_LIMIT (fdbclient/Knobs.h) to ensure that at least two entries will be returned from an attempt to read a key range map + double SKIP_TAGS_GROWTH_RATE; + int MAX_SKIP_TAGS; + + //FdbServer + double MIN_REBOOT_TIME; + double MAX_REBOOT_TIME; + + //Ratekeeper + double SMOOTHING_AMOUNT; + double SLOW_SMOOTHING_AMOUNT; + double RATEKEEPER_LOGGING_INTERVAL; + double METRIC_UPDATE_RATE; + double LAST_LIMITED_RATIO; + + int64_t TARGET_BYTES_PER_STORAGE_SERVER; + double SPRING_BYTES_STORAGE_SERVER; + + int64_t TARGET_BYTES_PER_TLOG; + double SPRING_BYTES_TLOG; + int64_t TLOG_SPILL_THRESHOLD; + + double MAX_TRANSACTIONS_PER_BYTE; + + int64_t MIN_FREE_SPACE; + double MIN_FREE_SPACE_RATIO; + + double MAX_TL_SS_VERSION_DIFFERENCE; // spring starts at half this value + int MAX_MACHINES_FALLING_BEHIND; + + //Storage Metrics + double STORAGE_METRICS_AVERAGE_INTERVAL; + double STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS; + double SPLIT_JITTER_AMOUNT; + int64_t IOPS_UNITS_PER_SAMPLE; + int64_t BANDWIDTH_UNITS_PER_SAMPLE; + + //Storage Server + double STORAGE_LOGGING_DELAY; + double STORAGE_SERVER_POLL_METRICS_DELAY; + double FUTURE_VERSION_DELAY; + int STORAGE_LIMIT_BYTES; + int BUGGIFY_LIMIT_BYTES; + int FETCH_BLOCK_BYTES; + int FETCH_KEYS_PARALLELISM_BYTES; + int BUGGIFY_BLOCK_BYTES; + int64_t STORAGE_HARD_LIMIT_BYTES; + int STORAGE_COMMIT_BYTES; + double STORAGE_COMMIT_INTERVAL; + double UPDATE_SHARD_VERSION_INTERVAL; + int BYTE_SAMPLING_FACTOR; + int BYTE_SAMPLING_OVERHEAD; + int MAX_STORAGE_SERVER_WATCH_BYTES; + int MAX_BYTE_SAMPLE_CLEAR_MAP_SIZE; + double LONG_BYTE_SAMPLE_RECOVERY_DELAY; + + //Wait Failure + int BUGGIFY_OUTSTANDING_WAIT_FAILURE_REQUESTS; + int MAX_OUTSTANDING_WAIT_FAILURE_REQUESTS; + double WAIT_FAILURE_DELAY_LIMIT; + + //Worker + double WORKER_LOGGING_INTERVAL; + double INCOMPATIBLE_PEER_DELAY_BEFORE_LOGGING; + + // Test harness + double WORKER_POLL_DELAY; + + // Coordination + double COORDINATED_STATE_ONCONFLICT_POLL_INTERVAL; + + // Buggification + double BUGGIFIED_EVENTUAL_CONSISTENCY; + bool BUGGIFY_ALL_COORDINATION; + + // Status + double STATUS_MIN_TIME_BETWEEN_REQUESTS; + int CONFIGURATION_ROWS_TO_FETCH; + + ServerKnobs(bool randomize = false, ClientKnobs* clientKnobs = NULL); +}; + +extern ServerKnobs const* SERVER_KNOBS; + +#endif diff --git a/fdbserver/LeaderElection.actor.cpp b/fdbserver/LeaderElection.actor.cpp new file mode 100644 index 0000000000..e20f5eaefb --- /dev/null +++ b/fdbserver/LeaderElection.actor.cpp @@ -0,0 +1,197 @@ +/* + * LeaderElection.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "fdbrpc/FailureMonitor.h" +#include "ClusterRecruitmentInterface.h" +#include "fdbserver/CoordinationInterface.h" +#include "fdbclient/MonitorLeader.h" + +extern Optional getLeader( vector> nominees ); + +ACTOR Future submitCandidacy( Key key, LeaderElectionRegInterface coord, LeaderInfo myInfo, Reference>>> nominees, int index ) { + loop { + auto const& nom = nominees->get()[index]; + Optional li = wait( retryBrokenPromise( coord.candidacy, CandidacyRequest( key, myInfo, nom.present() ? nom.get().changeID : UID() ), TaskCoordinationReply ) ); + + if (li != nominees->get()[index]) { + vector> v = nominees->get(); + v[index] = li; + nominees->set(v); + + if( li.present() && li.get().forward ) + Void _ = wait( Future(Never()) ); + + Void _ = wait( Future(Void()) ); // Make sure we weren't cancelled + } + } +} + +ACTOR template Future buggifyDelayedAsyncVar( Reference> in, Reference> out ) { + try { + loop { + Void _ = wait( delay( SERVER_KNOBS->BUGGIFIED_EVENTUAL_CONSISTENCY * g_random->random01() ) ); + out->set( in->get() ); + Void _ = wait( in->onChange() ); + } + } catch (Error& e) { + out->set( in->get() ); + throw; + } +} + +template +Future buggifyDelayedAsyncVar( Reference> &var ) { + Reference> in( new AsyncVar ); + auto f = buggifyDelayedAsyncVar(in, var); + var = in; + return f; +} + +ACTOR Future changeLeaderCoordinators( ServerCoordinators coordinators, Value forwardingInfo ) { + std::vector> forwardRequests; + for( int i = 0; i < coordinators.leaderElectionServers.size(); i++ ) + forwardRequests.push_back( retryBrokenPromise( coordinators.leaderElectionServers[i].forward, ForwardRequest( coordinators.clusterKey, forwardingInfo ) ) ); + int quorum_size = forwardRequests.size()/2 + 1; + Void _ = wait( quorum( forwardRequests, quorum_size ) ); + return Void(); +} + +ACTOR Future tryBecomeLeaderInternal( ServerCoordinators coordinators, Value proposedSerializedInterface, Reference> outSerializedLeader, bool hasConnected ) { + state Reference>>> nominees( new AsyncVar>>() ); + state LeaderInfo myInfo; + state Future candidacies; + state bool iAmLeader = false; + + nominees->set( vector>( coordinators.clientLeaderServers.size() ) ); + + myInfo.serializedInfo = proposedSerializedInterface; + outSerializedLeader->set( Value() ); + + state Future buggifyDelay = (SERVER_KNOBS->BUGGIFY_ALL_COORDINATION || BUGGIFY) ? buggifyDelayedAsyncVar( outSerializedLeader ) : Void(); + + while (!iAmLeader) { + state Future badCandidateTimeout; + + myInfo.changeID = g_random->randomUniqueID(); + + vector> cand; + for(int i=0; i leader = getLeader( nominees->get() ); + if( leader.present() && leader.get().forward ) { + // These coordinators are forwarded to another set. But before we change our own cluster file, we need to make + // sure that a majority of coordinators know that. + // SOMEDAY: Wait briefly to see if other coordinators will tell us they already know, to save communication? + Void _ = wait( changeLeaderCoordinators( coordinators, leader.get().serializedInfo ) ); + + if(!hasConnected) { + TraceEvent(SevWarnAlways, "IncorrectClusterFileContentsAtConnection").detail("Filename", coordinators.ccf->getFilename()) + .detail("ConnectionStringFromFile", coordinators.ccf->getConnectionString().toString()) + .detail("CurrentConnectionString", leader.get().serializedInfo.toString()); + } + coordinators.ccf->setConnectionString( ClusterConnectionString( leader.get().serializedInfo.toString() ) ); + TraceEvent("LeaderForwarding").detail("ConnStr", coordinators.ccf->getConnectionString().toString()); + throw coordinators_changed(); + } + + if (leader.present()) { + hasConnected = true; + coordinators.ccf->notifyConnected(); + } + + if (leader.present() && leader.get().changeID == myInfo.changeID) { + TraceEvent("BecomingLeader", myInfo.changeID); + ASSERT( leader.get().serializedInfo == proposedSerializedInterface ); + outSerializedLeader->set( leader.get().serializedInfo ); + iAmLeader = true; + break; + } + if (leader.present()) { + TraceEvent("LeaderChanged", myInfo.changeID).detail("ToID", leader.get().changeID); + if (leader.get().serializedInfo != proposedSerializedInterface) // We never set outSerializedLeader to our own interface unless we are ready to become leader! + outSerializedLeader->set( leader.get().serializedInfo ); + } + + // If more than 2*SERVER_KNOBS->POLLING_FREQUENCY elapses while we are nominated by some coordinator but there is no leader, + // we might be breaking the leader election process for someone with better communications but lower ID, so change IDs. + if (!leader.present() && std::count( nominees->get().begin(), nominees->get().end(), myInfo )) { + if (!badCandidateTimeout.isValid()) + badCandidateTimeout = delay( SERVER_KNOBS->POLLING_FREQUENCY*2, TaskCoordinationReply ); + } else + badCandidateTimeout = Future(); + + choose { + when (Void _ = wait( nominees->onChange() )) {} + when (Void _ = wait( badCandidateTimeout.isValid() ? badCandidateTimeout : Never() )) { + TEST(true); // Bad candidate timeout + TraceEvent("LeaderBadCandidateTimeout", myInfo.changeID); + break; + } + when (Void _ = wait(candidacies)) { ASSERT(false); } + } + } + + candidacies.cancel(); + } + + ASSERT( iAmLeader && outSerializedLeader->get() == proposedSerializedInterface ); + + loop { + state vector> true_heartbeats; + state vector> false_heartbeats; + for(int i=0; i hb = retryBrokenPromise( coordinators.leaderElectionServers[i].leaderHeartbeat, LeaderHeartbeatRequest( coordinators.clusterKey, myInfo ), TaskCoordinationReply ); + true_heartbeats.push_back( onEqual(hb, true) ); + false_heartbeats.push_back( onEqual(hb, false) ); + } + + state Future rate = delay( SERVER_KNOBS->HEARTBEAT_FREQUENCY, TaskCoordinationReply ); // SOMEDAY: Move to server side? + + choose { + when ( Void _ = wait( quorum( true_heartbeats, true_heartbeats.size()/2+1 ) ) ) { + //TraceEvent("StillLeader", myInfo.changeID); + } // We are still leader + when ( Void _ = wait( quorum( false_heartbeats, false_heartbeats.size()/2+1 ) ) ) { + TraceEvent("ReplacedAsLeader", myInfo.changeID); + break; } // We are definitely not leader + when ( Void _ = wait( delay(SERVER_KNOBS->POLLING_FREQUENCY) ) ) { + for(int i = 0; i < coordinators.leaderElectionServers.size(); ++i) { + if(true_heartbeats[i].isReady()) + TraceEvent("LeaderTrueHeartbeat", myInfo.changeID).detail("Coordinator", coordinators.leaderElectionServers[i].candidacy.getEndpoint().address); + else if(false_heartbeats[i].isReady()) + TraceEvent("LeaderFalseHeartbeat", myInfo.changeID).detail("Coordinator", coordinators.leaderElectionServers[i].candidacy.getEndpoint().address); + else + TraceEvent("LeaderNoHeartbeat", myInfo.changeID).detail("Coordinator", coordinators.leaderElectionServers[i].candidacy.getEndpoint().address); + } + TraceEvent("ReleasingLeadership", myInfo.changeID); + break; } // Give up on being leader, because we apparently have poor communications + } + + Void _ = wait( rate ); + } + + if (SERVER_KNOBS->BUGGIFY_ALL_COORDINATION || BUGGIFY) Void _ = wait( delay( SERVER_KNOBS->BUGGIFIED_EVENTUAL_CONSISTENCY * g_random->random01() ) ); + + return Void(); // We are no longer leader +} \ No newline at end of file diff --git a/fdbserver/LeaderElection.h b/fdbserver/LeaderElection.h new file mode 100644 index 0000000000..6390db523f --- /dev/null +++ b/fdbserver/LeaderElection.h @@ -0,0 +1,64 @@ +/* + * LeaderElection.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_LEADERELECTION_H +#define FDBSERVER_LEADERELECTION_H +#pragma once + +#include "fdbrpc/fdbrpc.h" + +class ServerCoordinators; + +template +Future tryBecomeLeader( ServerCoordinators const& coordinators, + LeaderInterface const& proposedInterface, + Reference>> const& outKnownLeader, + bool hasConnected); + +// Participates in the given coordination group's leader election process, nominating the given +// LeaderInterface (presumed to be a local interface) as leader. The leader election process is +// "sticky" - once a leader becomes leader, as long as its communications with other processes are +// good it will remain leader. The outKnownLeader variable is updated to reflect a best guess of +// the current leader. If the proposed interface becomes the leader, the outKnownLeader will be +// set to the proposedInterface, and then if it is displaced by another leader, the return value will +// eventually be set. If the return value is cancelled, the candidacy or leadership of the proposedInterface +// will eventually end. + +Future changeLeaderCoordinators( ServerCoordinators const& coordinators, Value const& forwardingInfo ); +// Inform all the coordinators that they have been replaced with a new connection string + +#pragma region Implementation + +Future tryBecomeLeaderInternal( ServerCoordinators const& coordinators, Value const& proposedSerializedInterface, Reference> const& outSerializedLeader, bool const& hasConnected ); + +template +Future tryBecomeLeader( ServerCoordinators const& coordinators, + LeaderInterface const& proposedInterface, + Reference>> const& outKnownLeader, + bool hasConnected) +{ + Reference> serializedInfo( new AsyncVar ); + Future m = tryBecomeLeaderInternal( coordinators, BinaryWriter::toValue(proposedInterface, IncludeVersion()), serializedInfo, hasConnected ); + return m || asyncDeserialize( serializedInfo, outKnownLeader ); +} + +#pragma endregion + +#endif diff --git a/fdbserver/LogProtocolMessage.h b/fdbserver/LogProtocolMessage.h new file mode 100644 index 0000000000..330c29cbff --- /dev/null +++ b/fdbserver/LogProtocolMessage.h @@ -0,0 +1,69 @@ +/* + * LogProtocolMessage.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_LOGPROTOCOLMESSAGE_H +#define FDBSERVER_LOGPROTOCOLMESSAGE_H +#pragma once + +#include "fdbclient/FDBTypes.h" +#include "fdbclient/CommitTransaction.h" + +template +typename Ar::READER& applyVersionStartingHere(Ar& ar, VersionOptions vo) { + vo.read(ar); + return ar; +} + +template +typename Ar::WRITER& applyVersionStartingHere(Ar& ar, VersionOptions vo) { + vo.write(ar); + return ar; +} + +struct LogProtocolMessage { + // This message is pushed into the log system tag for each storage server to inform it what protocol version + // should be used to deserialize subsequent MutationRefs. + + // It's legitimate to add extra information here (using the protocol version for backward compatibility) but + // currently there is none. + + // This mechanism passes various ASSERTs in simulation, but has never been used in anger (because MutationRef's + // serialization format has never changed) so think about testing when it does change. + + // Storage servers merging these messages with MutationRefs need to distinguish the two, so the first byte of + // the serialization of this message is a type code which is reserved in the MutationRef::Type enum and will thus + // never be the first byte of a MutationRef message. + + LogProtocolMessage() {} + + template + void serialize(Ar& ar) { + uint8_t poly = MutationRef::Reserved_For_LogProtocolMessage; + ar & poly; + applyVersionStartingHere(ar, IncludeVersion()); + } + + static bool startsLogProtocolMessage(uint8_t byte) { + return byte == MutationRef::Reserved_For_LogProtocolMessage; + } + template static bool isNextIn(Ar& ar) { return startsLogProtocolMessage(*(const uint8_t*)ar.peekBytes(1)); } +}; + +#endif \ No newline at end of file diff --git a/fdbserver/LogSystem.h b/fdbserver/LogSystem.h new file mode 100644 index 0000000000..8876765d20 --- /dev/null +++ b/fdbserver/LogSystem.h @@ -0,0 +1,418 @@ +/* + * LogSystem.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_LOGSYSTEM_H +#define FDBSERVER_LOGSYSTEM_H +#pragma once + +#include "TLogInterface.h" +#include "WorkerInterface.h" +#include "DatabaseConfiguration.h" +#include "flow/IndexedSet.h" +#include "fdbrpc/ReplicationPolicy.h" +#include "fdbrpc/Locality.h" +#include "fdbrpc/Replication.h" + +struct DBCoreState; + +struct ILogSystem { + // Represents a particular (possibly provisional) epoch of the log subsystem + + + struct IPeekCursor { + //clones the peek cursor, however you cannot call getMore() on the cloned cursor. + virtual Reference cloneNoMore() = 0; + + virtual void setProtocolVersion( uint64_t version ) = 0; + + //if hasMessage() returns true, getMessage() or reader() can be called. + //does not modify the cursor + virtual bool hasMessage() = 0; + + //pre: only callable if hasMessage() returns true + //returns the arena containing the contents of getMessage() and reader() + virtual Arena& arena() = 0; + + //pre: only callable if hasMessage() returns true + //returns an arena reader for the next message + //caller cannot call both getMessage() and reader() + //the caller must advance the reader before calling nextMessage() + virtual ArenaReader* reader() = 0; + + //pre: only callable if hasMessage() returns true + //caller cannot call both getMessage() and reader() + //return the contents of the message for the current sequence + virtual StringRef getMessage() = 0; + + //pre: only callable after getMessage() or reader() + //post: hasMessage() and version() have been updated + //hasMessage() will never return false "in the middle" of a version (that is, if it does return false, version().subsequence will be zero) < FIXME: Can we lose this property? + virtual void nextMessage() = 0; + + //advances the cursor to the supplied LogMessageVersion, and updates hasMessage + virtual void advanceTo(LogMessageVersion n) = 0; + + //returns immediately if hasMessage() returns true. + //returns when either the result of hasMessage() or version() has changed. + virtual Future getMore() = 0; + + //returns when the failure monitor detects that the servers associated with the cursor are failed + virtual Future onFailed() = 0; + + //returns false if: + // (1) the failure monitor detects that the servers associated with the cursor is failed + // (2) the interface is not present + // (3) the cursor cannot return any more results + virtual bool isActive() = 0; + + // Returns the smallest possible message version which the current message (if any) or a subsequent message might have + // (If hasMessage(), this is therefore the message version of the current message) + virtual LogMessageVersion version() = 0; + + //So far, the cursor has returned all messages which both satisfy the criteria passed to peek() to create the cursor AND have (popped(),0) <= message version number <= version() + //Other messages might have been skipped + virtual Version popped() = 0; + + // Returns the maximum version known to have been pushed (not necessarily durably) into the log system (0 is always a possible result!) + virtual Version getMaxKnownVersion() { return 0; } + + virtual void addref() = 0; + + virtual void delref() = 0; + }; + + struct ServerPeekCursor : IPeekCursor, ReferenceCounted { + Reference>> interf; + Tag tag; + + TLogPeekReply results; + ArenaReader rd; + LogMessageVersion messageVersion, end; + Version poppedVersion; + int32_t messageLength; + bool hasMsg; + Future more; + UID randomID; + bool returnIfBlocked; + + bool parallelGetMore; + int sequence; + Deque> futureResults; + + ServerPeekCursor( Reference>> const& interf, Tag tag, Version begin, Version end, bool returnIfBlocked, bool parallelGetMore ); + + ServerPeekCursor( TLogPeekReply const& results, LogMessageVersion const& messageVersion, LogMessageVersion const& end, int32_t messageLength, bool hasMsg, Version poppedVersion ); + + virtual Reference cloneNoMore(); + + virtual void setProtocolVersion( uint64_t version ); + + virtual Arena& arena(); + + virtual ArenaReader* reader(); + + virtual bool hasMessage(); + + virtual void nextMessage(); + + virtual StringRef getMessage(); + + virtual void advanceTo(LogMessageVersion n); + + virtual Future getMore(); + + virtual Future onFailed(); + + virtual bool isActive(); + + virtual LogMessageVersion version(); + + virtual Version popped(); + + virtual void addref() { + ReferenceCounted::addref(); + } + + virtual void delref() { + ReferenceCounted::delref(); + } + + virtual Version getMaxKnownVersion() { return results.maxKnownVersion; } + }; + + struct MergedPeekCursor : IPeekCursor, ReferenceCounted { + LocalityGroup localityGroup; + std::vector< std::pair > sortedVersions; + vector< Reference > serverCursors; + Tag tag; + int bestServer, currentCursor, readQuorum; + Optional nextVersion; + LogMessageVersion messageVersion; + bool hasNextMessage; + UID randomID; + int tLogReplicationFactor; + IRepPolicyRef tLogPolicy; + std::vector< LocalityData > tLogLocalities; + + MergedPeekCursor( std::vector>>> const& logServers, int bestServer, int readQuorum, Tag tag, Version begin, Version end, bool parallelGetMore, std::vector< LocalityData > const& tLogLocalities, IRepPolicyRef const tLogPolicy, int tLogReplicationFactor ); + + MergedPeekCursor( vector< Reference > const& serverCursors, LogMessageVersion const& messageVersion, int bestServer, int readQuorum, Optional nextVersion, std::vector< LocalityData > const& tLogLocalities, IRepPolicyRef const tLogPolicy, int tLogReplicationFactor ); + + // if server_cursors[c]->hasMessage(), then nextSequence <= server_cursors[c]->sequence() and there are no messages known to that server with sequences in [nextSequence,server_cursors[c]->sequence()) + + virtual Reference cloneNoMore(); + + virtual void setProtocolVersion( uint64_t version ); + + virtual Arena& arena(); + + virtual ArenaReader* reader(); + + void calcHasMessage(); + + void updateMessage(bool usePolicy); + + virtual bool hasMessage(); + + virtual void nextMessage(); + + virtual StringRef getMessage(); + + virtual void advanceTo(LogMessageVersion n); + + virtual Future getMore(); + + virtual Future onFailed(); + + virtual bool isActive(); + + virtual LogMessageVersion version(); + + virtual Version popped(); + + virtual void addref() { + ReferenceCounted::addref(); + } + + virtual void delref() { + ReferenceCounted::delref(); + } + }; + + struct MultiCursor : IPeekCursor, ReferenceCounted { + std::vector> cursors; + std::vector epochEnds; + Version poppedVersion; + + MultiCursor( std::vector> cursors, std::vector epochEnds ); + + virtual Reference cloneNoMore(); + + virtual void setProtocolVersion( uint64_t version ); + + virtual Arena& arena(); + + virtual ArenaReader* reader(); + + virtual bool hasMessage(); + + virtual void nextMessage(); + + virtual StringRef getMessage(); + + virtual void advanceTo(LogMessageVersion n); + + virtual Future getMore(); + + virtual Future onFailed(); + + virtual bool isActive(); + + virtual LogMessageVersion version(); + + virtual Version popped(); + + virtual void addref() { + ReferenceCounted::addref(); + } + + virtual void delref() { + ReferenceCounted::delref(); + } + }; + + virtual void addref() = 0; + virtual void delref() = 0; + + virtual std::string describe() = 0; + virtual UID getDebugID() = 0; + + virtual void toCoreState( DBCoreState& ) = 0; + + virtual Future onCoreStateChanged() = 0; + // Returns if and when the output of toCoreState() would change (for example, when older logs can be discarded from the state) + + virtual void coreStateWritten( DBCoreState const& newState ) = 0; + // Called when a core state has been written to the coordinators + + virtual Future onError() = 0; + // Never returns normally, but throws an error if the subsystem stops working + + //Future push( UID bundle, int64_t seq, VectorRef messages ); + virtual Future push( Version prevVersion, Version version, Version knownCommittedVersion, struct LogPushData& data, Optional debugID = Optional() ) = 0; + // Waits for the version number of the bundle (in this epoch) to be prevVersion (i.e. for all pushes ordered earlier) + // Puts the given messages into the bundle, each with the given tags, and with message versions (version, 0) - (version, N) + // Changes the version number of the bundle to be version (unblocking the next push) + // Returns when the preceding changes are durable. (Later we will need multiple return signals for diffferent durability levels) + // If the current epoch has ended, push will not return, and the pushed messages will not be visible in any subsequent epoch (but may become visible in this epoch) + + //Future peek( int64_t begin_epoch, int64_t begin_seq, int tag ); + virtual Reference peek( Version begin, Tag tag, bool parallelGetMore = false ) = 0; + // Returns (via cursor interface) a stream of messages with the given tag and message versions >= (begin, 0), ordered by message version + // If pop was previously or concurrently called with upTo > begin, the cursor may not return all such messages. In that case cursor->popped() will + // be greater than begin to reflect that. + + virtual Reference peekSingle( Version begin, Tag tag ) = 0; + // Same contract as peek(), but blocks until the preferred log server(s) for the given tag are available (and is correspondingly less expensive) + + virtual void pop( Version upTo, Tag tag ) = 0; + // Permits, but does not require, the log subsystem to strip `tag` from any or all messages with message versions < (upTo,0) + // The popping of any given message may be arbitrarily delayed. + + virtual Future confirmEpochLive( Optional debugID = Optional() ) = 0; + // Returns success after confirming that pushes in the current epoch are still possible + + static Reference fromServerDBInfo( UID const& dbgid, struct ServerDBInfo const& db ); + static Reference fromLogSystemConfig( UID const& dbgid, struct LocalityData const&, struct LogSystemConfig const& ); + // Constructs a new ILogSystem implementation from the given ServerDBInfo/LogSystemConfig. Might return a null reference if there isn't a fully recovered log system available. + // The caller can peek() the returned log system and can push() if it has version numbers reserved for it and prevVersions + + static Reference fromOldLogSystemConfig( UID const& dbgid, struct LocalityData const&, struct LogSystemConfig const& ); + // Constructs a new ILogSystem implementation from the old log data within a ServerDBInfo/LogSystemConfig. Might return a null reference if there isn't a fully recovered log system available. + + static Future recoverAndEndEpoch(Reference>> const& outLogSystem, UID const& dbgid, DBCoreState const& oldState, FutureStream const& rejoins, LocalityData const& locality); + // Constructs a new ILogSystem implementation based on the given oldState and rejoining log servers + // Ensures that any calls to push or confirmEpochLive in the current epoch but strictly later than change_epoch will not return + // Whenever changes in the set of available log servers require restarting recovery with a different end sequence, outLogSystem will be changed to a new ILogSystem + + virtual Version getEnd() = 0; + // Call only on an ILogSystem obtained from recoverAndEndEpoch() + // Returns the first unreadable version number of the recovered epoch (i.e. message version numbers < (get_end(), 0) will be readable) + + virtual Future> newEpoch( vector availableLogServers, DatabaseConfiguration const& config, LogEpoch recoveryCount ) = 0; + // Call only on an ILogSystem obtained from recoverAndEndEpoch() + // Returns an ILogSystem representing a new epoch immediately following this one. The new epoch is only provisional until the caller updates the coordinated DBCoreState + + virtual LogSystemConfig getLogSystemConfig() = 0; + // Returns the physical configuration of this LogSystem, that could be used to construct an equivalent LogSystem using fromLogSystemConfig() + + virtual Standalone getLogsValue() = 0; + + virtual Future onLogSystemConfigChange() = 0; + // Returns when the log system configuration has changed due to a tlog rejoin. + + virtual int getLogServerCount() = 0; + // Used by LogPushData; returns the number of log servers + + virtual void getPushLocations( std::vector const& tags, vector& locations ) = 0; + + virtual void stopRejoins() = 0; +}; + +struct LogPushData : NonCopyable { + // Log subsequences have to start at 1 (the MergedPeekCursor relies on this to make sure we never have !hasMessage() in the middle of data for a version + + explicit LogPushData(Reference logSystem) : logSystem(logSystem), subsequence(1) { + tags.resize( logSystem->getLogSystemConfig().tLogs.size() ); + for(int i = 0; i < tags.size(); i++) { + messagesWriter.push_back( BinaryWriter( AssumeVersion(currentProtocolVersion) ) ); + } + } + + // addTag() adds a tag for the *next* message to be added + void addTag( Tag tag ) { + next_message_tags.push_back( tag ); + } + + void addMessage( StringRef rawMessageWithoutLength, bool usePreviousLocations = false ) { + if( !usePreviousLocations ) { + msg_locations.clear(); + logSystem->getPushLocations( next_message_tags, msg_locations ); + } + uint32_t subseq = this->subsequence++; + for(int loc : msg_locations) { + for(auto& tag : next_message_tags) + addTagToLoc( tag, loc ); + + messagesWriter[loc] << uint32_t(rawMessageWithoutLength.size() + sizeof(subseq)) << subseq; + messagesWriter[loc].serializeBytes(rawMessageWithoutLength); + } + next_message_tags.clear(); + } + + template + void addTypedMessage( T const& item ) { + msg_locations.clear(); + logSystem->getPushLocations( next_message_tags, msg_locations ); + uint32_t subseq = this->subsequence++; + for(int loc : msg_locations) { + for(auto& tag : next_message_tags) + addTagToLoc( tag, loc ); + + // FIXME: memcpy after the first time + BinaryWriter& wr = messagesWriter[loc]; + int offset = wr.getLength(); + wr << uint32_t(0) << subseq << item; + *(uint32_t*)((uint8_t*)wr.getData() + offset) = wr.getLength() - offset - sizeof(uint32_t); + } + next_message_tags.clear(); + } + + Arena getArena() { return arena; } + StringRef getMessages(int loc) { + return StringRef( arena, messagesWriter[loc].toStringRef() ); // FIXME: Unnecessary copy! + } + VectorRef getTags(int loc) { + VectorRef r; + for(auto& t : tags[loc]) + r.push_back( arena, t.value ); + return r; + } + +private: + void addTagToLoc( Tag tag, int loc ) { + auto it = tags[loc].find(tag); + if (it == tags[loc].end()) { + it = tags[loc].insert(mapPair( tag, TagMessagesRef() )); + it->value.tag = it->key; + } + it->value.messageOffsets.push_back( arena, messagesWriter[loc].getLength() ); + } + + Reference logSystem; + Arena arena; + vector next_message_tags; + vector> tags; + vector messagesWriter; + vector msg_locations; + uint32_t subsequence; +}; + +#endif diff --git a/fdbserver/LogSystemConfig.h b/fdbserver/LogSystemConfig.h new file mode 100644 index 0000000000..92d864e58d --- /dev/null +++ b/fdbserver/LogSystemConfig.h @@ -0,0 +1,189 @@ +/* + * LogSystemConfig.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_LOGSYSTEMCONFIG_H +#define FDBSERVER_LOGSYSTEMCONFIG_H +#pragma once + +#include "TLogInterface.h" +#include "fdbrpc/ReplicationPolicy.h" +#include "DatabaseConfiguration.h" + +template +struct OptionalInterface { + // Represents an interface with a known id() and possibly known actual endpoints. + // For example, an OptionalInterface represents a particular tlog by id, which you might or might not presently know how to communicate with + + UID id() const { return ident; } + bool present() const { return iface.present(); } + Interface const& interf() const { return iface.get(); } + + explicit OptionalInterface( UID id ) : ident(id) {} + explicit OptionalInterface( Interface const& i ) : ident(i.id()), iface(i) {} + OptionalInterface() {} + + std::string toString() const { return ident.toString(); } + + bool operator==(UID const& r) const { return ident == r; } + + template + void serialize( Ar& ar ) { + ar & iface; + if( !iface.present() ) ar & ident; + else ident = iface.get().id(); + } + +protected: + UID ident; + Optional iface; +}; + +struct OldTLogConf { + vector> tLogs; + int32_t tLogWriteAntiQuorum, tLogReplicationFactor; + std::vector< LocalityData > tLogLocalities; // Stores the localities of the log servers + IRepPolicyRef tLogPolicy; + Version epochEnd; + + OldTLogConf() : tLogWriteAntiQuorum(0), tLogReplicationFactor(0), epochEnd(0) {} + + std::string toString() const { + return format("anti: %d replication: %d tLogs: %s tlocalities: %s", + tLogWriteAntiQuorum, tLogReplicationFactor, epochEnd, describe(tLogs).c_str(), describe(tLogLocalities).c_str()); } + + bool operator == ( const OldTLogConf& rhs ) const { + bool bIsEqual = true; + if (tLogWriteAntiQuorum != rhs.tLogWriteAntiQuorum) { + bIsEqual = false; + } + else if (tLogReplicationFactor != rhs.tLogReplicationFactor) { + bIsEqual = false; + } + else if (tLogs.size() != rhs.tLogs.size()) { + bIsEqual = false; + } + if (bIsEqual) { + for(int j = 0; j < tLogs.size(); j++ ) { + if (tLogs[j].id() != rhs.tLogs[j].id()) { + bIsEqual = false; + break; + } + else if (tLogs[j].present() != rhs.tLogs[j].present()) { + bIsEqual = false; + break; + } + else if (tLogs[j].present() && tLogs[j].interf().commit.getEndpoint().token != rhs.tLogs[j].interf().commit.getEndpoint().token ) { + bIsEqual = false; + break; + } + } + } + return bIsEqual; + } + + template + void serialize( Ar& ar ) { + ar & tLogs & tLogWriteAntiQuorum & tLogReplicationFactor & tLogPolicy & tLogLocalities & epochEnd; + } +}; + +struct LogSystemConfig { + int logSystemType; + std::vector> tLogs; + std::vector< LocalityData > tLogLocalities; + std::vector oldTLogs; + int32_t tLogWriteAntiQuorum, tLogReplicationFactor; + IRepPolicyRef tLogPolicy; + //LogEpoch epoch; + + LogSystemConfig() : tLogWriteAntiQuorum(0), tLogReplicationFactor(0), logSystemType(0) {} + + std::string toString() const { return format("type: %d anti: %d replication: %d tLogs: %s oldGenerations: %d tlocalities: %s", + logSystemType, tLogWriteAntiQuorum, tLogReplicationFactor, describe(tLogs).c_str(), oldTLogs.size(), describe(tLogLocalities).c_str()); } + + std::vector allPresentLogs() const { + std::vector results; + for( int i = 0; i < tLogs.size(); i++ ) + if( tLogs[i].present() ) + results.push_back(tLogs[i].interf()); + return results; + } + + bool operator == ( const LogSystemConfig& rhs ) const { return isEqual(rhs); } + + bool isEqual(LogSystemConfig const& r) const { + if (logSystemType != r.logSystemType || tLogWriteAntiQuorum != r.tLogWriteAntiQuorum || tLogReplicationFactor != r.tLogReplicationFactor || tLogs.size() != r.tLogs.size() || oldTLogs.size() != r.oldTLogs.size()) + return false; + else if ((tLogPolicy && !r.tLogPolicy) || (!tLogPolicy && r.tLogPolicy) || (tLogPolicy && (tLogPolicy->info() != r.tLogPolicy->info()))) + return false; + for(int i = 0; i < tLogs.size(); i++ ) { + if( tLogs[i].id() != r.tLogs[i].id() || tLogs[i].present() != r.tLogs[i].present() ) + return false; + if( tLogs[i].present() && tLogs[i].interf().commit.getEndpoint().token != r.tLogs[i].interf().commit.getEndpoint().token ) + return false; + } + for(int i = 0; i < oldTLogs.size(); i++ ) { + if (oldTLogs[i] != r.oldTLogs[i]) + return false; + } + return true; + } + + bool isEqualIds(LogSystemConfig const& r) const { + if(logSystemType!=r.logSystemType || tLogWriteAntiQuorum!=r.tLogWriteAntiQuorum || tLogReplicationFactor!=r.tLogReplicationFactor || + tLogs.size() != r.tLogs.size() || oldTLogs.size() != r.oldTLogs.size()) + return false; + else if ((tLogPolicy && !r.tLogPolicy) || (!tLogPolicy && r.tLogPolicy) || (tLogPolicy && (tLogPolicy->info() != r.tLogPolicy->info()))) { + return false; + } + for(int i = 0; i < tLogs.size(); i++ ) { + if( tLogs[i].id() != r.tLogs[i].id() ) + return false; + } + + for(int i = 0; i < oldTLogs.size(); i++ ) { + if (oldTLogs[i].tLogWriteAntiQuorum != r.oldTLogs[i].tLogWriteAntiQuorum || oldTLogs[i].tLogReplicationFactor != r.oldTLogs[i].tLogReplicationFactor || oldTLogs[i].tLogs.size() != r.oldTLogs[i].tLogs.size()) + return false; + + for(int j = 0; j < oldTLogs[i].tLogs.size(); j++ ) { + if( oldTLogs[i].tLogs[j].id() != r.oldTLogs[i].tLogs[j].id() ) + return false; + } + } + return true; + } + + bool isNextGenerationOf(LogSystemConfig const& r) const { + if( !oldTLogs.size() || oldTLogs[0].tLogWriteAntiQuorum!=r.tLogWriteAntiQuorum || oldTLogs[0].tLogReplicationFactor!=r.tLogReplicationFactor || oldTLogs[0].tLogs.size() != r.tLogs.size()) + return false; + for(int i = 0; i < oldTLogs[0].tLogs.size(); i++ ) { + if( oldTLogs[0].tLogs[i].id() != r.tLogs[i].id() ) + return false; + } + return true; + } + + template + void serialize( Ar& ar ) { + ar & logSystemType & tLogs & oldTLogs & tLogWriteAntiQuorum & tLogReplicationFactor & tLogPolicy & tLogLocalities; + } +}; + +#endif diff --git a/fdbserver/LogSystemDiskQueueAdapter.actor.cpp b/fdbserver/LogSystemDiskQueueAdapter.actor.cpp new file mode 100644 index 0000000000..51c0a6c26f --- /dev/null +++ b/fdbserver/LogSystemDiskQueueAdapter.actor.cpp @@ -0,0 +1,149 @@ +/* + * LogSystemDiskQueueAdapter.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "IDiskQueue.h" +#include "LogSystem.h" +#include "LogSystemDiskQueueAdapter.h" +#include "Knobs.h" + +class LogSystemDiskQueueAdapterImpl { +public: + ACTOR static Future> readNext( LogSystemDiskQueueAdapter* self, int bytes ) { + while (self->recoveryQueueDataSize < bytes) { + if (self->recoveryLoc == self->logSystem->getEnd()) { + // Recovery will be complete once the current recoveryQueue is consumed, so we no longer need self->logSystem + TraceEvent("PeekNextEnd").detail("queue", self->recoveryQueue.size()).detail("bytes", bytes).detail("loc", self->recoveryLoc).detail("end", self->logSystem->getEnd()); + self->logSystem.clear(); + break; + } + + if(!self->cursor->hasMessage()) { + Void _ = wait( self->cursor->getMore() ); + TraceEvent("PeekNextGetMore").detail("queue", self->recoveryQueue.size()).detail("bytes", bytes).detail("loc", self->recoveryLoc).detail("end", self->logSystem->getEnd()); + if(self->recoveryQueueDataSize == 0) { + self->recoveryQueueLoc = self->recoveryLoc; + } + if(!self->cursor->hasMessage()) { + self->recoveryLoc = self->cursor->version().version; + continue; + } + } + + self->recoveryQueue.push_back( Standalone(self->cursor->getMessage(), self->cursor->arena()) ); + self->recoveryQueueDataSize += self->recoveryQueue.back().size(); + self->cursor->nextMessage(); + if(!self->cursor->hasMessage()) self->recoveryLoc = self->cursor->version().version; + + //TraceEvent("PeekNextResults").detail("from", self->recoveryLoc).detail("queue", self->recoveryQueue.size()).detail("bytes", bytes).detail("has", self->cursor->hasMessage()).detail("end", self->logSystem->getEnd()); + } + if(self->recoveryQueue.size() > 1) { + self->recoveryQueue[0] = concatenate(self->recoveryQueue.begin(), self->recoveryQueue.end()); + self->recoveryQueue.resize(1); + } + + if(self->recoveryQueueDataSize == 0) + return Standalone(); + + ASSERT(self->recoveryQueue[0].size() == self->recoveryQueueDataSize); + + //TraceEvent("PeekNextReturn").detail("bytes", bytes).detail("queueSize", self->recoveryQueue.size()); + bytes = std::min(bytes, self->recoveryQueue[0].size()); + Standalone result( self->recoveryQueue[0].substr(0,bytes), self->recoveryQueue[0].arena() ); + self->recoveryQueue[0].contents() = self->recoveryQueue[0].substr(bytes); + self->recoveryQueueDataSize = self->recoveryQueue[0].size(); + if(self->recoveryQueue[0].size() == 0) { + self->recoveryQueue.clear(); + } + return result; + } +}; + +Future> LogSystemDiskQueueAdapter::readNext( int bytes ) { + if (!enableRecovery) return Standalone(); + return LogSystemDiskQueueAdapterImpl::readNext(this, bytes); +} + +IDiskQueue::location LogSystemDiskQueueAdapter::getNextReadLocation() { + return IDiskQueue::location( 0, recoveryQueueLoc ); +} + +IDiskQueue::location LogSystemDiskQueueAdapter::push( StringRef contents ) { + while(contents.size()) { + int remainder = pushedData.size() == 0 ? 0 : pushedData.back().capacity() - pushedData.back().size(); + + if(remainder == 0) { + VectorRef block; + block.reserve(pushedData.arena(), SERVER_KNOBS->LOG_SYSTEM_PUSHED_DATA_BLOCK_SIZE); + pushedData.push_back(pushedData.arena(), block); + remainder = block.capacity(); + } + + pushedData.back().append(pushedData.arena(), contents.begin(), std::min(remainder, contents.size())); + contents = contents.substr(std::min(remainder, contents.size())); + } + + return IDiskQueue::location( 0, nextCommit ); +} + +void LogSystemDiskQueueAdapter::pop( location upTo ) { + ASSERT( upTo.hi == 0 ); + poppedUpTo = std::max( upTo.lo, poppedUpTo ); +} + +Future LogSystemDiskQueueAdapter::commit() { + ASSERT( !commitMessages.empty() ); + + auto promise = commitMessages.front(); + commitMessages.pop_front(); + + CommitMessage cm; + cm.messages = this->pushedData; + this->pushedData = Standalone>>(); + cm.popTo = poppedUpTo; + promise.send(cm); + + return cm.acknowledge.getFuture(); +} + +Future LogSystemDiskQueueAdapter::getError() { + return Void(); +} + +Future LogSystemDiskQueueAdapter::onClosed() { + return Void(); +} + +void LogSystemDiskQueueAdapter::dispose() { + delete this; +} + +void LogSystemDiskQueueAdapter::close() { + delete this; +} + +Future LogSystemDiskQueueAdapter::getCommitMessage() { + Promise pcm; + commitMessages.push_back( pcm ); + return pcm.getFuture(); +} + +LogSystemDiskQueueAdapter* openDiskQueueAdapter( Reference logSystem, Tag tag ) { + return new LogSystemDiskQueueAdapter( logSystem, tag ); +} \ No newline at end of file diff --git a/fdbserver/LogSystemDiskQueueAdapter.h b/fdbserver/LogSystemDiskQueueAdapter.h new file mode 100644 index 0000000000..d094fad1a9 --- /dev/null +++ b/fdbserver/LogSystemDiskQueueAdapter.h @@ -0,0 +1,98 @@ +/* + * LogSystemDiskQueueAdapter.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_LOGSYSTEMDISKQUEUEADAPTER_H +#define FDBSERVER_LOGSYSTEMDISKQUEUEADAPTER_H +#pragma once + +#include "fdbclient/FDBTypes.h" +#include "IDiskQueue.h" + +class LogSystemDiskQueueAdapter : public IDiskQueue { +public: + // This adapter is designed to let KeyValueStoreMemory use ILogSystem + // as a backing store, so that the transaction subsystem can in + // turn use KeyValueStoreMemory to track configuration information as of + // the database version and recover it from the logging subsystem as necessary. + + // Because the transaction subsystem will need to control the actual pushing of + // committed information to the ILogSystem, commit() in this interface doesn't directly + // call ILogSystem::push(). Instead it makes a commit message available through + // getCommitMessage(), and doesn't return until its acknowledge promise is set. + // The caller is responsible for calling ILogSystem::push() and ILogSystem::pop() with the results. + + // It does, however, peek the specified tag directly at recovery time. + + LogSystemDiskQueueAdapter( Reference logSystem, Tag tag, bool recover=true ) : logSystem(logSystem), tag(tag), enableRecovery(recover), recoveryLoc(1), recoveryQueueLoc(1), poppedUpTo(0), nextCommit(1), recoveryQueueDataSize(0) { + if (enableRecovery) + cursor = logSystem->peek( 0, tag, true ); + } + + struct CommitMessage { + Standalone>> messages; // push this into the logSystem with `tag` + Version popTo; // pop this from the logSystem with `tag` + Promise acknowledge; // then send Void to this, so commit() can return + }; + + // Set the version of the next push or commit (or a lower version) + // If lower, locations returned by the IDiskQueue interface will be conservative, so things that could be popped might not be + void setNextVersion( Version next ) { nextCommit = next; } + + // Return the next commit message resulting from a call to commit(). + Future getCommitMessage(); + + // IClosable interface + virtual Future getError(); + virtual Future onClosed(); + virtual void dispose(); + virtual void close(); + + // IDiskQueue interface + virtual Future> readNext( int bytes ); + virtual IDiskQueue::location getNextReadLocation(); + virtual IDiskQueue::location push( StringRef contents ); + virtual void pop( IDiskQueue::location upTo ); + virtual Future commit(); + virtual StorageBytes getStorageBytes() { ASSERT(false); throw internal_error(); } + virtual int getCommitOverhead() { return 0; } //SOMEDAY: could this be more accurate? + +private: + Reference cursor; + Tag tag; + + // Recovery state (used while readNext() is being called repeatedly) + bool enableRecovery; + Reference logSystem; + Version recoveryLoc, recoveryQueueLoc; + std::vector> recoveryQueue; + int recoveryQueueDataSize; + + // State for next commit() call + Standalone>> pushedData; // SOMEDAY: better representation? + Version poppedUpTo; + std::deque< Promise > commitMessages; + Version nextCommit; + + friend class LogSystemDiskQueueAdapterImpl; +}; + +LogSystemDiskQueueAdapter* openDiskQueueAdapter( Reference logSystem, Tag tag ); + +#endif \ No newline at end of file diff --git a/fdbserver/LogSystemPeekCursor.actor.cpp b/fdbserver/LogSystemPeekCursor.actor.cpp new file mode 100644 index 0000000000..3d32e6b808 --- /dev/null +++ b/fdbserver/LogSystemPeekCursor.actor.cpp @@ -0,0 +1,480 @@ +/* + * LogSystemPeekCursor.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "LogSystem.h" +#include "fdbrpc/FailureMonitor.h" +#include "Knobs.h" + +ILogSystem::ServerPeekCursor::ServerPeekCursor( Reference>> const& interf, Tag tag, Version begin, Version end, bool returnIfBlocked, bool parallelGetMore ) + : interf(interf), tag(tag), messageVersion(begin), end(end), hasMsg(false), rd(results.arena, results.messages, Unversioned()), randomID(g_random->randomUniqueID()), poppedVersion(0), returnIfBlocked(returnIfBlocked), sequence(0), parallelGetMore(parallelGetMore) { + this->results.maxKnownVersion = 0; + //TraceEvent("SPC_starting", randomID).detail("tag", printable(tag)).detail("begin", begin).detail("end", end); +} + +ILogSystem::ServerPeekCursor::ServerPeekCursor( TLogPeekReply const& results, LogMessageVersion const& messageVersion, LogMessageVersion const& end, int32_t messageLength, bool hasMsg, Version poppedVersion ) + : results(results), rd(results.arena, results.messages, Unversioned()), messageVersion(messageVersion), end(end), messageLength(messageLength), hasMsg(hasMsg), randomID(g_random->randomUniqueID()), poppedVersion(poppedVersion), returnIfBlocked(false), sequence(0), parallelGetMore(false) +{ + //TraceEvent("SPC_clone", randomID); + this->results.maxKnownVersion = 0; + if(hasMsg) + nextMessage(); + + advanceTo(messageVersion); +} + +Reference ILogSystem::ServerPeekCursor::cloneNoMore() { + return Reference( new ILogSystem::ServerPeekCursor( results, messageVersion, end, messageLength, hasMsg, poppedVersion ) ); +} + +void ILogSystem::ServerPeekCursor::setProtocolVersion( uint64_t version ) { + rd.setProtocolVersion(version); +} + +Arena& ILogSystem::ServerPeekCursor::arena() { return results.arena; } + +ArenaReader* ILogSystem::ServerPeekCursor::reader() { + return &rd; +} + +bool ILogSystem::ServerPeekCursor::hasMessage() { + //TraceEvent("SPC_hasMessage", randomID).detail("hasMsg", hasMsg); + return hasMsg; +} + +void ILogSystem::ServerPeekCursor::nextMessage() { + //TraceEvent("SPC_nextMessage", randomID).detail("messageVersion", messageVersion.toString()); + ASSERT(hasMsg); + if (rd.empty()) { + messageVersion.reset(std::min(results.end, end.version)); + hasMsg = false; + return; + } + if (*(int32_t*)rd.peekBytes(4) == -1) { + // A version + int32_t dummy; + Version ver; + rd >> dummy >> ver; + + //TraceEvent("SPC_process_Seq", randomID).detail("messageVersion", messageVersion.toString()).detail("ver", ver); + //ASSERT( ver >= messageVersion.version ); + + messageVersion.reset(ver); + + if( messageVersion >= end ) { + messageVersion = end; + hasMsg = false; + return; + } + ASSERT(!rd.empty()); + } + + rd >> messageLength >> messageVersion.sub; + messageLength -= sizeof(messageVersion.sub); + hasMsg = true; + //TraceEvent("SPC_nextMessageB", randomID).detail("messageVersion", messageVersion.toString()); +} + +StringRef ILogSystem::ServerPeekCursor::getMessage() { + //TraceEvent("SPC_getMessage", randomID); + return StringRef( (uint8_t const*)rd.readBytes(messageLength), messageLength); +} + +void ILogSystem::ServerPeekCursor::advanceTo(LogMessageVersion n) { + //TraceEvent("SPC_advanceTo", randomID).detail("n", n.toString()); + while( messageVersion < n && hasMessage() ) { + getMessage(); + nextMessage(); + } + + if( hasMessage() ) + return; + + //if( more.isValid() && !more.isReady() ) more.cancel(); + + if( messageVersion < n ) { + messageVersion = n; + } +} + +ACTOR Future serverPeekParallelGetMore( ILogSystem::ServerPeekCursor* self ) { + if( !self->interf || self->messageVersion >= self->end ) { + Void _ = wait( Future(Never())); + throw internal_error(); + } + + loop { + try { + while(self->futureResults.size() < SERVER_KNOBS->PARALLEL_GET_MORE_REQUESTS && self->interf->get().present()) { + self->futureResults.push_back( brokenPromiseToNever( self->interf->get().interf().peekMessages.getReply(TLogPeekRequest(self->messageVersion.version,self->tag,self->returnIfBlocked, std::make_pair(self->randomID, self->sequence++)), TaskTLogPeekReply) ) ); + } + + choose { + when( TLogPeekReply res = wait( self->interf->get().present() ? self->futureResults.front() : Never() ) ) { + self->futureResults.pop_front(); + self->results = res; + if(res.popped.present()) + self->poppedVersion = std::min( std::max(self->poppedVersion, res.popped.get()), self->end.version ); + self->rd = ArenaReader( self->results.arena, self->results.messages, Unversioned() ); + LogMessageVersion skipSeq = self->messageVersion; + self->hasMsg = true; + self->nextMessage(); + self->advanceTo(skipSeq); + //TraceEvent("SPC_getMoreB", self->randomID).detail("has", self->hasMessage()).detail("end", res.end).detail("popped", res.popped.present() ? res.popped.get() : 0); + return Void(); + } + when( Void _ = wait( self->interf->onChange() ) ) { + self->sequence = 0; + self->futureResults.clear(); + } + } + } catch( Error &e ) { + if(e.code() == error_code_end_of_stream) { + self->end.reset( self->messageVersion.version ); + return Void(); + } else if(e.code() == error_code_timed_out) { + TraceEvent("PeekCursorTimedOut", self->randomID); + self->randomID = g_random->randomUniqueID(); + self->sequence = 0; + self->futureResults.clear(); + } else { + throw e; + } + } + } +} + +ACTOR Future serverPeekGetMore( ILogSystem::ServerPeekCursor* self ) { + if( !self->interf || self->messageVersion >= self->end ) { + Void _ = wait( Future(Never())); + throw internal_error(); + } + try { + loop { + choose { + when( TLogPeekReply res = wait( self->interf->get().present() ? + brokenPromiseToNever( self->interf->get().interf().peekMessages.getReply(TLogPeekRequest(self->messageVersion.version,self->tag,self->returnIfBlocked), TaskTLogPeekReply) ) : Never() ) ) { + self->results = res; + if(res.popped.present()) + self->poppedVersion = std::min( std::max(self->poppedVersion, res.popped.get()), self->end.version ); + self->rd = ArenaReader( self->results.arena, self->results.messages, Unversioned() ); + LogMessageVersion skipSeq = self->messageVersion; + self->hasMsg = true; + self->nextMessage(); + self->advanceTo(skipSeq); + //TraceEvent("SPC_getMoreB", self->randomID).detail("has", self->hasMessage()).detail("end", res.end).detail("popped", res.popped.present() ? res.popped.get() : 0); + return Void(); + } + when( Void _ = wait( self->interf->onChange() ) ) {} + } + } + } catch( Error &e ) { + if(e.code() == error_code_end_of_stream) { + self->end.reset( self->messageVersion.version ); + return Void(); + } + throw e; + } +} + +Future ILogSystem::ServerPeekCursor::getMore() { + //TraceEvent("SPC_getMore", randomID).detail("hasMessage", hasMessage()).detail("more", !more.isValid() || more.isReady()).detail("messageVersion", messageVersion.toString()).detail("end", end.toString()); + if( hasMessage() ) + return Void(); + if( !more.isValid() || more.isReady() ) { + more = parallelGetMore ? serverPeekParallelGetMore(this) : serverPeekGetMore(this); + } + return more; +} + +ACTOR Future serverPeekOnFailed( ILogSystem::ServerPeekCursor* self ) { + loop { + choose { + when( Void _ = wait( self->interf->get().present() ? IFailureMonitor::failureMonitor().onDisconnectOrFailure( self->interf->get().interf().peekMessages.getEndpoint() ) : Never() ) ) { return Void(); } + when( Void _ = wait( self->interf->onChange() ) ) {} + } + } +} + +Future ILogSystem::ServerPeekCursor::onFailed() { + return serverPeekOnFailed(this); +} + +bool ILogSystem::ServerPeekCursor::isActive() { + if( !interf->get().present() ) + return false; + if( messageVersion >= end ) + return false; + return IFailureMonitor::failureMonitor().getState( interf->get().interf().peekMessages.getEndpoint() ).isAvailable(); +} + +LogMessageVersion ILogSystem::ServerPeekCursor::version() { return messageVersion; } // Call only after nextMessage(). The sequence of the current message, or results.end if nextMessage() has returned false. + +Version ILogSystem::ServerPeekCursor::popped() { return poppedVersion; } + +ILogSystem::MergedPeekCursor::MergedPeekCursor( std::vector>>> const& logServers, int bestServer, int readQuorum, Tag tag, Version begin, Version end, bool parallelGetMore, std::vector< LocalityData > const& tLogLocalities, IRepPolicyRef const tLogPolicy, int tLogReplicationFactor ) + : bestServer(bestServer), readQuorum(readQuorum), tag(tag), currentCursor(0), hasNextMessage(false), messageVersion(begin), randomID(g_random->randomUniqueID()), tLogLocalities(tLogLocalities), tLogPolicy(tLogPolicy), tLogReplicationFactor(tLogReplicationFactor) { + for( int i = 0; i < logServers.size(); i++ ) { + Reference cursor( new ILogSystem::ServerPeekCursor( logServers[i], tag, begin, end, true, parallelGetMore ) ); + //TraceEvent("MPC_starting", randomID).detail("cursor", cursor->randomID).detail("end", end); + serverCursors.push_back( cursor ); + } + sortedVersions.resize(serverCursors.size()); +} + +ILogSystem::MergedPeekCursor::MergedPeekCursor( vector< Reference > const& serverCursors, LogMessageVersion const& messageVersion, int bestServer, int readQuorum, Optional nextVersion, std::vector< LocalityData > const& tLogLocalities, IRepPolicyRef const tLogPolicy, int tLogReplicationFactor ) + : serverCursors(serverCursors), bestServer(bestServer), readQuorum(readQuorum), currentCursor(0), hasNextMessage(false), messageVersion(messageVersion), nextVersion(nextVersion), randomID(g_random->randomUniqueID()), tLogLocalities(tLogLocalities), tLogPolicy(tLogPolicy), tLogReplicationFactor(tLogReplicationFactor) { + sortedVersions.resize(serverCursors.size()); + calcHasMessage(); +} + +Reference ILogSystem::MergedPeekCursor::cloneNoMore() { + vector< Reference > cursors; + for( auto it : serverCursors ) { + cursors.push_back(it->cloneNoMore()); + } + return Reference( new ILogSystem::MergedPeekCursor( cursors, messageVersion, bestServer, readQuorum, nextVersion, tLogLocalities, tLogPolicy, tLogReplicationFactor ) ); +} + +void ILogSystem::MergedPeekCursor::setProtocolVersion( uint64_t version ) { + for( auto it : serverCursors ) + if( it->hasMessage() ) + it->setProtocolVersion( version ); +} + +Arena& ILogSystem::MergedPeekCursor::arena() { return serverCursors[currentCursor]->arena(); } + +ArenaReader* ILogSystem::MergedPeekCursor::reader() { return serverCursors[currentCursor]->reader(); } + + +void ILogSystem::MergedPeekCursor::calcHasMessage() { + if(nextVersion.present()) serverCursors[bestServer]->advanceTo( nextVersion.get() ); + if( serverCursors[bestServer]->hasMessage() ) { + messageVersion = serverCursors[bestServer]->version(); + currentCursor = bestServer; + hasNextMessage = true; + + for (auto& c : serverCursors) + c->advanceTo(messageVersion); + + return; + } + + auto bestVersion = serverCursors[bestServer]->version(); + for (auto& c : serverCursors) + c->advanceTo(bestVersion); + + hasNextMessage = false; + updateMessage(false); // Use Quorum logic + + if(!hasNextMessage) { + updateMessage(true); + } +} + +void ILogSystem::MergedPeekCursor::updateMessage(bool usePolicy) { + loop { + bool advancedPast = false; + sortedVersions.clear(); + for(int i = 0; i < serverCursors.size(); i++) { + auto& serverCursor = serverCursors[i]; + if (nextVersion.present()) serverCursor->advanceTo(nextVersion.get()); + sortedVersions.push_back(std::pair(serverCursor->version(), i)); + } + + if(usePolicy) { + ASSERT(tLogPolicy); + localityGroup.clear(); + std::sort(sortedVersions.begin(), sortedVersions.end()); + + for(auto sortedVersion : sortedVersions) { + auto& locality = tLogLocalities[sortedVersion.second]; + localityGroup.add(locality); + + if( localityGroup.size() >= tLogReplicationFactor && localityGroup.validate(tLogPolicy) ) { + messageVersion = sortedVersion.first; + break; + } + } + } else { + std::nth_element(sortedVersions.begin(), sortedVersions.end()-readQuorum, sortedVersions.end()); + messageVersion = sortedVersions[sortedVersions.size()-readQuorum].first; + } + + for(int i = 0; i < serverCursors.size(); i++) { + auto& c = serverCursors[i]; + auto start = c->version(); + c->advanceTo(messageVersion); + if( start < messageVersion && messageVersion < c->version() ) { + advancedPast = true; + TEST(true); //Merge peek cursor advanced past desired sequence + } + } + + if(!advancedPast) + break; + } + + for(int i = 0; i < serverCursors.size(); i++) { + auto& c = serverCursors[i]; + ASSERT_WE_THINK( !c->hasMessage() || c->version() >= messageVersion ); // Seems like the loop above makes this unconditionally true + if (c->version() == messageVersion && c->hasMessage()) { + hasNextMessage = true; + currentCursor = i; + break; + } + } +} + +bool ILogSystem::MergedPeekCursor::hasMessage() { + return hasNextMessage; +} + +void ILogSystem::MergedPeekCursor::nextMessage() { + nextVersion = version(); + nextVersion.get().sub++; + serverCursors[currentCursor]->nextMessage(); + calcHasMessage(); + ASSERT(hasMessage() || !version().sub); +} + +StringRef ILogSystem::MergedPeekCursor::getMessage() { return serverCursors[currentCursor]->getMessage(); } + +void ILogSystem::MergedPeekCursor::advanceTo(LogMessageVersion n) { + for (auto& c : serverCursors) + c->advanceTo(n); + calcHasMessage(); +} + +ACTOR Future mergedPeekGetMore(ILogSystem::MergedPeekCursor* self, LogMessageVersion startVersion) { + loop { + //TraceEvent("MPC_getMoreA", self->randomID).detail("start", startVersion.toString()); + if(self->serverCursors[self->bestServer]->isActive()) { + ASSERT(!self->serverCursors[self->bestServer]->hasMessage()); + Void _ = wait( self->serverCursors[self->bestServer]->getMore() || self->serverCursors[self->bestServer]->onFailed() ); + } else { + vector> q; + for (auto& c : self->serverCursors) + if (!c->hasMessage()) + q.push_back(c->getMore()); + Void _ = wait(quorum(q, 1)); + } + self->calcHasMessage(); + //TraceEvent("MPC_getMoreB", self->randomID).detail("hasMessage", self->hasMessage()).detail("start", startVersion.toString()).detail("seq", self->version().toString()); + if (self->hasMessage() || self->version() > startVersion) + return Void(); + } +} + +Future ILogSystem::MergedPeekCursor::getMore() { + auto startVersion = version(); + calcHasMessage(); + if( hasMessage() ) + return Void(); + if (nextVersion.present()) + advanceTo(nextVersion.get()); + ASSERT(!hasMessage()); + if (version() > startVersion) + return Void(); + + return mergedPeekGetMore(this, startVersion); +} + +Future ILogSystem::MergedPeekCursor::onFailed() { + ASSERT(false); + return Never(); +} + +bool ILogSystem::MergedPeekCursor::isActive() { + ASSERT(false); + return false; +} + +LogMessageVersion ILogSystem::MergedPeekCursor::version() { return messageVersion; } + +Version ILogSystem::MergedPeekCursor::popped() { + Version poppedVersion = 0; + for (auto& c : serverCursors) + poppedVersion = std::max(poppedVersion, c->popped()); + return poppedVersion; +} + +ILogSystem::MultiCursor::MultiCursor( std::vector> cursors, std::vector epochEnds ) : cursors(cursors), epochEnds(epochEnds), poppedVersion(0) {} + +Reference ILogSystem::MultiCursor::cloneNoMore() { + return cursors.back()->cloneNoMore(); +} + +void ILogSystem::MultiCursor::setProtocolVersion( uint64_t version ) { + cursors.back()->setProtocolVersion(version); +} + +Arena& ILogSystem::MultiCursor::arena() { + return cursors.back()->arena(); +} + +ArenaReader* ILogSystem::MultiCursor::reader() { + return cursors.back()->reader(); +} + +bool ILogSystem::MultiCursor::hasMessage() { + return cursors.back()->hasMessage(); +} + +void ILogSystem::MultiCursor::nextMessage() { + cursors.back()->nextMessage(); +} + +StringRef ILogSystem::MultiCursor::getMessage() { + return cursors.back()->getMessage(); +} + +void ILogSystem::MultiCursor::advanceTo(LogMessageVersion n) { + while( cursors.size() > 1 && n >= epochEnds.back() ) { + poppedVersion = std::max(poppedVersion, cursors.back()->popped()); + cursors.pop_back(); + epochEnds.pop_back(); + } + cursors.back()->advanceTo(n); +} + +Future ILogSystem::MultiCursor::getMore() { + while( cursors.size() > 1 && cursors.back()->version() >= epochEnds.back() ) { + poppedVersion = std::max(poppedVersion, cursors.back()->popped()); + cursors.pop_back(); + epochEnds.pop_back(); + } + return cursors.back()->getMore(); +} + +Future ILogSystem::MultiCursor::onFailed() { + return cursors.back()->onFailed(); +} + +bool ILogSystem::MultiCursor::isActive() { + return cursors.back()->isActive(); +} + +LogMessageVersion ILogSystem::MultiCursor::version() { + return cursors.back()->version(); +} + +Version ILogSystem::MultiCursor::popped() { + return std::max(poppedVersion, cursors.back()->popped()); +} diff --git a/fdbserver/MasterInterface.h b/fdbserver/MasterInterface.h new file mode 100644 index 0000000000..7720b4ccf3 --- /dev/null +++ b/fdbserver/MasterInterface.h @@ -0,0 +1,181 @@ +/* + * MasterInterface.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_MASTERINTERFACE_H +#define FDBSERVER_MASTERINTERFACE_H +#pragma once + +#include "fdbclient/FDBTypes.h" +#include "fdbclient/StorageServerInterface.h" +#include "fdbclient/CommitTransaction.h" +#include "TLogInterface.h" + +typedef uint64_t DBRecoveryCount; + +struct MasterInterface { + LocalityData locality; + RequestStream< ReplyPromise > waitFailure; + RequestStream< struct GetRateInfoRequest > getRateInfo; + RequestStream< struct TLogRejoinRequest > tlogRejoin; // sent by tlog (whether or not rebooted) to communicate with a new master + RequestStream< struct ChangeCoordinatorsRequest > changeCoordinators; + RequestStream< struct GetCommitVersionRequest > getCommitVersion; + + NetworkAddress address() const { return changeCoordinators.getEndpoint().address; } + + UID id() const { return changeCoordinators.getEndpoint().token; } + template + void serialize(Archive& ar) { + ASSERT( ar.protocolVersion() >= 0x0FDB00A200040001LL ); + ar & locality & waitFailure & getRateInfo & tlogRejoin & changeCoordinators & getCommitVersion; + } + + void initEndpoints() { + getCommitVersion.getEndpoint( TaskProxyGetConsistentReadVersion ); + } +}; + +struct GetRateInfoRequest { + UID requesterID; + int64_t totalReleasedTransactions; + ReplyPromise reply; + + GetRateInfoRequest() {} + GetRateInfoRequest( UID const& requesterID, int64_t totalReleasedTransactions ) : requesterID(requesterID), totalReleasedTransactions(totalReleasedTransactions) {} + + template + void serialize(Ar& ar) { + ar & requesterID & totalReleasedTransactions & reply; + } +}; + +struct GetRateInfoReply { + double transactionRate; + double leaseDuration; + + template + void serialize(Ar& ar) { + ar & transactionRate & leaseDuration; + } +}; + +struct TLogRejoinRequest { + TLogInterface myInterface; + DBRecoveryCount recoveryCount; + ReplyPromise reply; // false means someone else registered, so we should re-register. true means this master is recovered, so don't send again to the same master. + + template + void serialize(Ar& ar) { + ar & myInterface & reply; + } +}; + +struct ChangeCoordinatorsRequest { + Standalone newConnectionString; + ReplyPromise reply; // normally throws even on success! + + ChangeCoordinatorsRequest() {} + ChangeCoordinatorsRequest(Standalone newConnectionString) : newConnectionString(newConnectionString) {} + + template + void serialize(Ar& ar) { + ar & newConnectionString & reply; + } +}; + +struct ResolverMoveRef { + KeyRangeRef range; + int dest; + + ResolverMoveRef() : dest(0) {} + ResolverMoveRef(KeyRangeRef const& range, int dest) : range(range), dest(dest) {} + ResolverMoveRef( Arena& a, const ResolverMoveRef& copyFrom ) : range(a, copyFrom.range), dest(copyFrom.dest) {} + + bool operator == ( ResolverMoveRef const& rhs ) const { + return range == rhs.range && dest == rhs.dest; + } + bool operator != ( ResolverMoveRef const& rhs ) const { + return range != rhs.range || dest != rhs.dest; + } + + size_t expectedSize() const { + return range.expectedSize(); + } + + template + void serialize( Ar& ar ) { + ar & range & dest; + } +}; + +struct GetCommitVersionReply { + Standalone> resolverChanges; + Version resolverChangesVersion; + Version version; + Version prevVersion; + uint64_t requestNum; + + GetCommitVersionReply() : resolverChangesVersion(0), version(0), prevVersion(0), requestNum(0) {} + explicit GetCommitVersionReply( Version version, Version prevVersion, uint64_t requestNum ) : version(version), prevVersion(prevVersion), resolverChangesVersion(0), requestNum(requestNum) {} + + template + void serialize(Ar& ar) { + ar & resolverChanges & resolverChangesVersion & version & prevVersion & requestNum; + } +}; + +struct GetCommitVersionRequest { + uint64_t requestNum; + uint64_t mostRecentProcessedRequestNum; + UID requestingProxy; + ReplyPromise reply; + + GetCommitVersionRequest() { } + GetCommitVersionRequest(uint64_t requestNum, uint64_t mostRecentProcessedRequestNum, UID requestingProxy) + : requestNum(requestNum), mostRecentProcessedRequestNum(mostRecentProcessedRequestNum), requestingProxy(requestingProxy) {} + + template + void serialize(Ar& ar) { + ar & requestNum & mostRecentProcessedRequestNum & requestingProxy & reply; + } +}; + +struct LifetimeToken { + UID ccID; + int64_t count; + + LifetimeToken() : count(0) {} + + bool isStillValid( LifetimeToken const& latestToken, bool isLatestID ) const { + return ccID == latestToken.ccID && (count >= latestToken.count || isLatestID); + } + std::string toString() const { + return ccID.shortString() + format("#%lld", count); + } + void operator++() { + ++count; + } + + template + void serialize(Ar& ar) { + ar & ccID & count; + } +}; + +#endif \ No newline at end of file diff --git a/fdbserver/MasterProxyServer.actor.cpp b/fdbserver/MasterProxyServer.actor.cpp new file mode 100644 index 0000000000..de7fd4d89d --- /dev/null +++ b/fdbserver/MasterProxyServer.actor.cpp @@ -0,0 +1,1217 @@ +/* + * MasterProxyServer.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/ActorCollection.h" +#include "fdbclient/MasterProxyInterface.h" +#include "fdbclient/NativeAPI.h" +#include "MasterInterface.h" +#include "WorkerInterface.h" +#include "WaitFailure.h" +#include "Knobs.h" +#include "ServerDBInfo.h" +#include "LogSystem.h" +#include "LogSystemDiskQueueAdapter.h" +#include "IKeyValueStore.h" +#include "fdbclient/SystemData.h" +#include "flow/Notified.h" +#include "fdbrpc/sim_validation.h" +#include "fdbrpc/batcher.actor.h" +#include "fdbclient/KeyRangeMap.h" +#include "ConflictSet.h" +#include "flow/Stats.h" +#include "ApplyMetadataMutation.h" +#include "RecoveryState.h" +#include "fdbclient/Atomic.h" +#include "flow/TDMetric.actor.h" + +struct ProxyStats { + CounterCollection cc; + Counter txnStartIn, txnStartOut, txnStartBatch; + Counter txnSystemPriorityStartIn, txnSystemPriorityStartOut; + Counter txnBatchPriorityStartIn, txnBatchPriorityStartOut; + Counter txnDefaultPriorityStartIn, txnDefaultPriorityStartOut; + Counter txnCommitIn, txnCommitVersionAssigned, txnCommitResolving, txnCommitResolved, txnCommitOut, txnCommitOutSuccess; + Counter txnConflicts; + Counter commitBatchIn, commitBatchOut; + Counter mutationBytes; + Counter mutations; + Counter conflictRanges; + Version lastCommitVersionAssigned; + + Future logger; + + explicit ProxyStats(UID id, Version* pVersion, NotifiedVersion* pCommittedVersion) + : cc("ProxyStats", id.toString()), + txnStartIn("txnStartIn", cc), txnStartOut("txnStartOut", cc), txnStartBatch("txnStartBatch", cc), txnSystemPriorityStartIn("txnSystemPriorityStartIn", cc), txnSystemPriorityStartOut("txnSystemPriorityStartOut", cc), txnBatchPriorityStartIn("txnBatchPriorityStartIn", cc), txnBatchPriorityStartOut("txnBatchPriorityStartOut", cc), + txnDefaultPriorityStartIn("txnDefaultPriorityStartIn", cc), txnDefaultPriorityStartOut("txnDefaultPriorityStartOut", cc), txnCommitIn("txnCommitIn", cc), txnCommitVersionAssigned("txnCommitVersionAssigned", cc), txnCommitResolving("txnCommitResolving", cc), txnCommitResolved("txnCommitResolved", cc), txnCommitOut("txnCommitOut", cc), + txnCommitOutSuccess("txnCommitOutSuccess", cc), txnConflicts("txnConflicts", cc), commitBatchIn("commitBatchIn", cc), commitBatchOut("commitBatchOut", cc), mutationBytes("mutationBytes", cc), mutations("mutations", cc), conflictRanges("conflictRanges", cc), lastCommitVersionAssigned(0) + { + specialCounter(cc, "lastAssignedCommitVersion", [this](){return this->lastCommitVersionAssigned;}); + specialCounter(cc, "version", [pVersion](){return *pVersion; }); + specialCounter(cc, "committedVersion", [pCommittedVersion](){ return pCommittedVersion->get(); }); + logger = traceCounters("ProxyMetrics", id, SERVER_KNOBS->WORKER_LOGGING_INTERVAL, &cc, "ProxyMetrics"); + } +}; + +ACTOR template +Future forwardValue(Promise out, Future in) +{ + // Like forwardPromise, but throws on error + T t = wait(in); + out.send(t); + return Void(); +} + +int getBytes(Promise const& r) { return 0; } + +ACTOR Future getRate(UID myID, MasterInterface master, int64_t* inTransactionCount, double* outTransactionRate) { + state Future nextRequestTimer = Void(); + state Future leaseTimeout = Never(); + state Future reply; + state int64_t lastTC = 0; + + loop choose{ + when(Void _ = wait(nextRequestTimer)) { + nextRequestTimer = Never(); + reply = brokenPromiseToNever(master.getRateInfo.getReply(GetRateInfoRequest(myID, *inTransactionCount))); + } + when(GetRateInfoReply rep = wait(reply)) { + reply = Never(); + *outTransactionRate = rep.transactionRate; + TraceEvent("MasterProxyRate", myID).detail("Rate", rep.transactionRate).detail("Lease", rep.leaseDuration).detail("ReleasedTransactions", *inTransactionCount - lastTC); + lastTC = *inTransactionCount; + leaseTimeout = delay(rep.leaseDuration); + nextRequestTimer = delayJittered(rep.leaseDuration / 2); + } + when(Void _ = wait(leaseTimeout)) { + *outTransactionRate = 0; + TraceEvent("MasterProxyRate", myID).detail("Rate", 0).detail("Lease", "Expired"); + leaseTimeout = Never(); + } + } +} + +ACTOR Future queueTransactionStartRequests(std::priority_queue< std::pair, + std::vector< std::pair > > *transactionQueue, + FutureStream readVersionRequests, + PromiseStream GRVTimer, double *lastGRVTime, + double *GRVBatchTime, FutureStream replyTimes, + ProxyStats* stats) { + state int64_t counter = 0; + loop choose{ + when(GetReadVersionRequest req = waitNext(readVersionRequests)) { + if (req.debugID.present()) + g_traceBatch.addEvent("TransactionDebug", req.debugID.get().first(), "MasterProxyServer.queueTransactionStartRequests.Before"); + + stats->txnStartIn += req.transactionCount; + if (req.priority() >= GetReadVersionRequest::PRIORITY_SYSTEM_IMMEDIATE) + stats->txnSystemPriorityStartIn += req.transactionCount; + else if (req.priority() >= GetReadVersionRequest::PRIORITY_DEFAULT) + stats->txnDefaultPriorityStartIn += req.transactionCount; + else + stats->txnBatchPriorityStartIn += req.transactionCount; + + if (transactionQueue->empty()) { + if (now() - *lastGRVTime > *GRVBatchTime) + *lastGRVTime = now() - *GRVBatchTime; + + forwardPromise(GRVTimer, delayJittered(*GRVBatchTime - (now() - *lastGRVTime), TaskProxyGRVTimer)); + } + + transactionQueue->push(std::make_pair(req, counter--)); + } + // dynamic batching monitors reply latencies + when(double reply_latency = waitNext(replyTimes)) { + double target_latency = reply_latency * SERVER_KNOBS->START_TRANSACTION_BATCH_INTERVAL_LATENCY_FRACTION; + *GRVBatchTime = + std::max(SERVER_KNOBS->START_TRANSACTION_BATCH_INTERVAL_MIN, + std::min(SERVER_KNOBS->START_TRANSACTION_BATCH_INTERVAL_MAX, + target_latency * SERVER_KNOBS->START_TRANSACTION_BATCH_INTERVAL_SMOOTHER_ALPHA + *GRVBatchTime * (1-SERVER_KNOBS->START_TRANSACTION_BATCH_INTERVAL_SMOOTHER_ALPHA))); + } + } +} + +ACTOR void discardCommit(UID id, Future fcm, Future dummyCommitState) { + ASSERT(!dummyCommitState.isReady()); + LogSystemDiskQueueAdapter::CommitMessage cm = wait(fcm); + TraceEvent("Discarding", id).detail("count", cm.messages.size()); + cm.acknowledge.send(Void()); + ASSERT(dummyCommitState.isReady()); +} + +DESCR struct SingleKeyMutation { + Standalone shardBegin; + Standalone shardEnd; + int64_t tag1; + int64_t tag2; + int64_t tag3; +}; + +struct ProxyCommitData { + UID dbgid; + ProxyStats stats; + MasterInterface master; + vector resolvers; + LogSystemDiskQueueAdapter* logAdapter; + Reference logSystem; + IKeyValueStore* txnStateStore; + NotifiedVersion committedVersion; // Provided that this recovery has succeeded or will succeed, this version is fully committed (durable) + Version version; // The version at which txnStateStore is up to date + Promise validState; // Set once txnStateStore and version are valid + double lastVersionTime; + KeyRangeMap> vecBackupKeys; + uint64_t commitVersionRequestNumber; + uint64_t mostRecentProcessedRequestNumber; + KeyRangeMap>> keyResolvers; + KeyRangeMap> keyTags; + std::map uid_applyMutationsData; + bool firstProxy; + double lastCoalesceTime; + bool locked; + + int64_t localCommitBatchesStarted; + NotifiedVersion latestLocalCommitBatchResolving; + NotifiedVersion latestLocalCommitBatchLogging; + + PromiseStream commitBatchStartNotifications; + PromiseStream> commitBatchVersions; // 1:1 with commitBatchStartNotifications + RequestStream getConsistentReadVersion; + RequestStream commit; + Database cx; + EventMetricHandle singleKeyMutationEvent; + + std::map tagCache; + + ProxyCommitData(UID dbgid, MasterInterface master, RequestStream getConsistentReadVersion, Version recoveryTransactionVersion, RequestStream commit, Reference> db, bool firstProxy) + : dbgid(dbgid), stats(dbgid, &version, &committedVersion), master(master), + logAdapter(NULL), txnStateStore(NULL), + committedVersion(recoveryTransactionVersion), version(0), + lastVersionTime(0), commitVersionRequestNumber(1), mostRecentProcessedRequestNumber(0), + getConsistentReadVersion(getConsistentReadVersion), commit(commit), lastCoalesceTime(0), + localCommitBatchesStarted(0), locked(false), firstProxy(firstProxy), + cx(openDBOnServer(db, TaskDefaultEndpoint, true, true)), singleKeyMutationEvent(LiteralStringRef("SingleKeyMutation")) + {} +}; + +struct ResolutionRequestBuilder { + ProxyCommitData* self; + vector requests; + vector> transactionResolverMap; + vector outTr; + + ResolutionRequestBuilder( ProxyCommitData* self, Version version, Version prevVersion, Version lastReceivedVersion) : self(self), requests(self->resolvers.size()) { + for(auto& req : requests) { + req.prevVersion = prevVersion; + req.version = version; + req.lastReceivedVersion = lastReceivedVersion; + } + } + + CommitTransactionRef& getOutTransaction(int resolver, Version read_snapshot) { + CommitTransactionRef *& out = outTr[resolver]; + if (!out) { + ResolveTransactionBatchRequest& request = requests[resolver]; + request.transactions.resize(request.arena, request.transactions.size() + 1); + out = &request.transactions.back(); + out->read_snapshot = read_snapshot; + } + return *out; + } + + void addTransaction(CommitTransactionRef& trIn, int transactionNumberInBatch) { + // SOMEDAY: There are a couple of unnecessary O( # resolvers ) steps here + outTr.assign(requests.size(), NULL); + ASSERT( transactionNumberInBatch >= 0 && transactionNumberInBatch < 32768 ); + + bool isTXNStateTransaction = false; + for (auto & m : trIn.mutations) { + if (m.type == MutationRef::SetVersionstampedKey) { + transformSetVersionstampedKey( m, requests[0].version, transactionNumberInBatch ); + trIn.write_conflict_ranges.push_back( requests[0].arena, singleKeyRange( m.param1, requests[0].arena ) ); + } else if (m.type == MutationRef::SetVersionstampedValue ) { + transformSetVersionstampedValue( m, requests[0].version, transactionNumberInBatch ); + } + if (isMetadataMutation(m)) { + isTXNStateTransaction = true; + getOutTransaction(0, trIn.read_snapshot).mutations.push_back(requests[0].arena, m); + } + } + for(auto& r : trIn.read_conflict_ranges) { + auto ranges = self->keyResolvers.intersectingRanges( r ); + std::set resolvers; + for(auto &ir : ranges) { + auto& version_resolver = ir.value(); + for(int i = version_resolver.size()-1; i >= 0; i--) { + resolvers.insert(version_resolver[i].second); + if( version_resolver[i].first < trIn.read_snapshot ) + break; + } + } + ASSERT(resolvers.size()); + for(int resolver : resolvers) + getOutTransaction( resolver, trIn.read_snapshot ).read_conflict_ranges.push_back( requests[resolver].arena, r ); + } + for(auto& r : trIn.write_conflict_ranges) { + auto ranges = self->keyResolvers.intersectingRanges( r ); + std::set resolvers; + for(auto &ir : ranges) + resolvers.insert(ir.value().back().second); + ASSERT(resolvers.size()); + for(int resolver : resolvers) + getOutTransaction( resolver, trIn.read_snapshot ).write_conflict_ranges.push_back( requests[resolver].arena, r ); + } + if (isTXNStateTransaction) + for (int r = 0; r resolversUsed; + for (int r = 0; r commitBatch( + ProxyCommitData* self, + vector trs, + double *commitBatchTime) +{ + state int64_t localBatchNumber = ++self->localCommitBatchesStarted; + state LogPushData toCommit(self->logSystem); + state double t1 = now(); + state Optional debugID; + state bool forceRecovery = false; + + ASSERT(SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS <= SERVER_KNOBS->MAX_VERSIONS_IN_FLIGHT); // since we are using just the former to limit the number of versions actually in flight! + + self->lastVersionTime = t1; + + ++self->stats.commitBatchIn; + + for (int t = 0; trandomUniqueID(); + g_traceBatch.addAttach("CommitAttachID", trs[t].debugID.get().first(), debugID.get().first()); + } + } + + if(localBatchNumber == 2 && !debugID.present() && self->firstProxy && !g_network->isSimulated()) { + debugID = g_random->randomUniqueID(); + TraceEvent("SecondCommitBatch", self->dbgid).detail("debugID", debugID.get()); + } + + if (debugID.present()) + g_traceBatch.addEvent("CommitDebug", debugID.get().first(), "MasterProxyServer.commitBatch.Before"); + + if (trs.empty()) { + // We are sending an empty batch, so we have to trigger the version fetcher + self->commitBatchStartNotifications.send(Void()); + } + + /////// Phase 1: Pre-resolution processing (CPU bound except waiting for a version # which is separately pipelined and *should* be available by now (unless empty commit); ordered; currently atomic but could yield) + TEST(self->latestLocalCommitBatchResolving.get() < localBatchNumber-1); // Queuing pre-resolution commit processing + Void _ = wait(self->latestLocalCommitBatchResolving.whenAtLeast(localBatchNumber-1)); + Void _ = wait(yield()); + + if (debugID.present()) + g_traceBatch.addEvent("CommitDebug", debugID.get().first(), "MasterProxyServer.commitBatch.GettingCommitVersion"); + + Future fVersionReply = waitNext(self->commitBatchVersions.getFuture()); + GetCommitVersionReply versionReply = wait(fVersionReply); + self->mostRecentProcessedRequestNumber = versionReply.requestNum; + + self->stats.txnCommitVersionAssigned += trs.size(); + self->stats.lastCommitVersionAssigned = versionReply.version; + + state Version commitVersion = versionReply.version; + state Version prevVersion = versionReply.prevVersion; + + for(auto it : versionReply.resolverChanges) { + auto rs = self->keyResolvers.modify(it.range); + for(auto r = rs.begin(); r != rs.end(); ++r) + r->value().push_back(std::make_pair(versionReply.resolverChangesVersion,it.dest)); + } + + //TraceEvent("ProxyGotVer", self->dbgid).detail("commit", commitVersion).detail("prev", prevVersion); + + if (debugID.present()) + g_traceBatch.addEvent("CommitDebug", debugID.get().first(), "MasterProxyServer.commitBatch.GotCommitVersion"); + + ResolutionRequestBuilder requests( self, commitVersion, prevVersion, self->version ); + int conflictRangeCount = 0; + for (int t = 0; tdbgid).detail("Snapshot", trs[t].transaction.read_snapshot); + //for(auto& m : trs[t].transaction.mutations) + // TraceEvent("MPTransactionsDump", self->dbgid).detail("Mutation", m.toString()); + } + self->stats.conflictRanges += conflictRangeCount; + + for (int r = 1; rresolvers.size(); r++) + ASSERT(requests.requests[r].txnStateTransactions.size() == requests.requests[0].txnStateTransactions.size()); + + // Sending these requests is the fuzzy border between phase 1 and phase 2; it could conceivably overlap with resolution processing but is still using CPU + self->stats.txnCommitResolving += trs.size(); + vector< Future > replies; + for (int r = 0; rresolvers.size(); r++) { + requests.requests[r].debugID = debugID; + replies.push_back(brokenPromiseToNever(self->resolvers[r].resolve.getReply(requests.requests[r], TaskProxyResolverReply))); + } + + state vector> transactionResolverMap = std::move( requests.transactionResolverMap ); + + ASSERT(self->latestLocalCommitBatchResolving.get() == localBatchNumber-1); + self->latestLocalCommitBatchResolving.set(localBatchNumber); + + /////// Phase 2: Resolution (waiting on the network; pipelined) + state vector resolution = wait( getAll(replies) ); + + if (debugID.present()) + g_traceBatch.addEvent("CommitDebug", debugID.get().first(), "MasterProxyServer.commitBatch.AfterResolution"); + + ////// Phase 3: Post-resolution processing (CPU bound except for very rare situations; ordered; currently atomic but doesn't need to be) + TEST(self->latestLocalCommitBatchLogging.get() < localBatchNumber-1); // Queuing post-resolution commit processing + Void _ = wait(self->latestLocalCommitBatchLogging.whenAtLeast(localBatchNumber-1)); + Void _ = wait(yield()); + + self->stats.txnCommitResolved += trs.size(); + + if (debugID.present()) + g_traceBatch.addEvent("CommitDebug", debugID.get().first(), "MasterProxyServer.commitBatch.ProcessingMutations"); + + state Arena arena; + state bool isMyFirstBatch = !self->version; + state Optional oldCoordinators = self->txnStateStore->readValue(coordinatorsKey).get(); + + //TraceEvent("ResolutionResult", self->dbgid).detail("Sequence", sequence).detail("Version", commitVersion).detail("StateMutationProxies", resolution[0].stateMutations.size()).detail("WaitForResolution", now()-t1).detail("R0Committed", resolution[0].committed.size()) + // .detail("Transactions", trs.size()); + + for(int r=1; r, Future> > storeCommits; + for (int versionIndex = 0; versionIndex < resolution[0].stateMutations.size(); versionIndex++) { + // self->logAdapter->setNextVersion( ??? ); << Ideally we would be telling the log adapter that the pushes in this commit will be in the version at which these state mutations were committed by another proxy, but at present we don't have that information here. So the disk queue may be unnecessarily conservative about popping. + + for (int transactionIndex = 0; transactionIndex < resolution[0].stateMutations[versionIndex].size() && !forceRecovery; transactionIndex++) { + bool committed = true; + for (int resolver = 0; resolver < resolution.size(); resolver++) + committed = committed && resolution[resolver].stateMutations[versionIndex][transactionIndex].committed; + if (committed) + applyMetadataMutations( self->dbgid, arena, resolution[0].stateMutations[versionIndex][transactionIndex].mutations, self->txnStateStore, NULL, &forceRecovery, self->logSystem, 0, &self->vecBackupKeys, &self->keyTags, self->firstProxy ? &self->uid_applyMutationsData : NULL, self->commit, self->cx, &self->committedVersion, &self->tagCache ); + + if( resolution[0].stateMutations[versionIndex][transactionIndex].mutations.size() && firstStateMutations ) { + ASSERT(committed); + firstStateMutations = false; + forceRecovery = false; + } + //TraceEvent("MetadataTransaction", self->dbgid).detail("Committed", committed).detail("Mutations", resolution[0].stateMutations[versionIndex][transactionIndex].second.size()).detail("R1Mutations", resolution.back().stateMutations[versionIndex][transactionIndex].second.size()); + } + //TraceEvent("MetadataBatch", self->dbgid).detail("Transactions", resolution[0].stateMutations[versionIndex].size()); + + // These changes to txnStateStore will be committed by the other proxy, so we simply discard the commit message + auto fcm = self->logAdapter->getCommitMessage(); + storeCommits.push_back(std::make_pair(fcm, self->txnStateStore->commit())); + //discardCommit( dbgid, fcm, txnStateStore->commit() ); + + if (initialState) { + //TraceEvent("ResyncLog", dbgid); + initialState = false; + forceRecovery = false; + self->txnStateStore->resyncLog(); + + for (auto &p : storeCommits) { + ASSERT(!p.second.isReady()); + p.first.get().acknowledge.send(Void()); + ASSERT(p.second.isReady()); + } + storeCommits.clear(); + } + } + + // Determine which transactions actually committed (conservatively) by combining results from the resolvers + state vector committed(trs.size()); + ASSERT(transactionResolverMap.size() == committed.size()); + vector nextTr(resolution.size()); + for (int t = 0; tlogAdapter->setNextVersion(commitVersion); + + state Optional lockedKey = self->txnStateStore->readValue(databaseLockedKey).get(); + state bool locked = lockedKey.present() && lockedKey.get().size(); + + if(forceRecovery) { + Void _ = wait( Future(Never()) ); + } + + // This first pass through committed transactions deals with "metadata" effects (modifications of txnStateStore, changes to storage servers' responsibilities) + int t; + state int commitCount = 0; + for (t = 0; t < trs.size() && !forceRecovery; t++) + { + if (committed[t] == ConflictBatch::TransactionCommitted && (!locked || trs[t].isLockAware)) { + commitCount++; + applyMetadataMutations(self->dbgid, arena, trs[t].transaction.mutations, self->txnStateStore, &toCommit, &forceRecovery, self->logSystem, commitVersion+1, &self->vecBackupKeys, &self->keyTags, self->firstProxy ? &self->uid_applyMutationsData : NULL, self->commit, self->cx, &self->committedVersion, &self->tagCache); + } + if(firstStateMutations) { + ASSERT(committed[t] == ConflictBatch::TransactionCommitted); + firstStateMutations = false; + forceRecovery = false; + } + } + if (forceRecovery) { + for (; tdbgid).detail("Stage", "AwaitCommit"); + } + + lockedKey = self->txnStateStore->readValue(databaseLockedKey).get(); + state bool lockedAfter = lockedKey.present() && lockedKey.get().size(); + + auto fcm = self->logAdapter->getCommitMessage(); + storeCommits.push_back(std::make_pair(fcm, self->txnStateStore->commit())); + self->version = commitVersion; + if (!self->validState.isSet()) self->validState.send(Void()); + ASSERT(commitVersion); + + if (!isMyFirstBatch && self->txnStateStore->readValue( coordinatorsKey ).get().get() != oldCoordinators.get()) { + Void _ = wait( brokenPromiseToNever( self->master.changeCoordinators.getReply( ChangeCoordinatorsRequest( self->txnStateStore->readValue( coordinatorsKey ).get().get() ) ) ) ); + ASSERT(false); // ChangeCoordinatorsRequest should always throw + } + + // This second pass through committed transactions assigns the actual mutations to the appropriate storage servers' tags + int mutationCount = 0, mutationBytes = 0; + + state std::map logRangeMutations; + state Arena logRangeMutationsArena; + state uint32_t v = commitVersion / CLIENT_KNOBS->LOG_RANGE_BLOCK_SIZE; + + for (int t = 0; tkeyTags[m.param1]; + + if(self->singleKeyMutationEvent->enabled) { + KeyRangeRef shard = self->keyTags.rangeContaining(m.param1).range(); + self->singleKeyMutationEvent->tag1 = (int64_t)tags[0]; + self->singleKeyMutationEvent->tag2 = (int64_t)tags[1]; + self->singleKeyMutationEvent->tag3 = (int64_t)tags[2]; + self->singleKeyMutationEvent->shardBegin = shard.begin; + self->singleKeyMutationEvent->shardEnd = shard.end; + self->singleKeyMutationEvent->log(); + } + + if (debugMutation("ProxyCommit", commitVersion, m)) + TraceEvent("ProxyCommitTo", self->dbgid).detail("To", describe(tags)).detail("Mutation", m.toString()).detail("Version", commitVersion); + for (auto& tag : tags) + toCommit.addTag(tag); + toCommit.addTypedMessage(m); + } + else if (m.type == MutationRef::ClearRange) { + auto ranges = self->keyTags.intersectingRanges(KeyRangeRef(m.param1, m.param2)); + auto firstRange = ranges.begin(); + ++firstRange; + if (firstRange == ranges.end()) { + // Fast path + if (debugMutation("ProxyCommit", commitVersion, m)) + TraceEvent("ProxyCommitTo", self->dbgid).detail("To", describe(ranges.begin().value())).detail("Mutation", m.toString()).detail("Version", commitVersion); + for (auto& tag : ranges.begin().value()) + toCommit.addTag(tag); + } + else { + TEST(true); //A clear range extends past a shard boundary + std::set allSources; + for (auto r : ranges) + allSources.insert(r.value().begin(), r.value().end()); + if (debugMutation("ProxyCommit", commitVersion, m)) + TraceEvent("ProxyCommitTo", self->dbgid).detail("To", describe(allSources)).detail("Mutation", m.toString()).detail("Version", commitVersion); + for (auto& tag : allSources) + toCommit.addTag(tag); + } + toCommit.addTypedMessage(m); + } + else + UNREACHABLE(); + + // Check on backing up key, if backup ranges are defined and a normal key + if ((self->vecBackupKeys.size() > 1) && normalKeys.contains(m.param1)) { + + if (isAtomicOp((MutationRef::Type)m.type)) { + // Add the mutation to the relevant backup tag + for (auto backupName : self->vecBackupKeys[m.param1]) { + logRangeMutations[backupName].push_back_deep(logRangeMutationsArena, m); + } + } + else { + switch (m.type) + { + // Backup the mutation, if within a backup range + case MutationRef::Type::SetValue: + // Add the mutation to the relevant backup tag + for (auto backupName : self->vecBackupKeys[m.param1]) { + logRangeMutations[backupName].push_back_deep(logRangeMutationsArena, m); + } + break; + + case MutationRef::Type::ClearRange: + { + KeyRangeRef mutationRange(m.param1, m.param2); + KeyRangeRef intersectionRange; + + // Identify and add the intersecting ranges of the mutation to the array of mutations to serialize + for (auto backupRange : self->vecBackupKeys.intersectingRanges(mutationRange)) + { + // Get the backup sub range + const auto& backupSubrange = backupRange.range(); + + // Determine the intersecting range + intersectionRange = mutationRange & backupSubrange; + + // Create the custom mutation for the specific backup tag + MutationRef backupMutation(MutationRef::Type::ClearRange, intersectionRange.begin, intersectionRange.end); + + // Add the mutation to the relevant backup tag + for (auto backupName : backupRange.value()) { + logRangeMutations[backupName].push_back_deep(logRangeMutationsArena, backupMutation); + } + } + } + break; + + default: + UNREACHABLE(); + break; + } + } + } + } + } + } + + // Serialize and backup the mutations as a single mutation + if ((self->vecBackupKeys.size() > 1) && logRangeMutations.size()) { + + Key val; + MutationRef backupMutation; + uint32_t* partBuffer = NULL; + + // Serialize the log range mutations within the map + for (auto& logRangeMutation : logRangeMutations) + { + BinaryWriter wr(Unversioned()); + + // Serialize the log destination + wr.serializeBytes( logRangeMutation.first ); + + // Write the log keys and version information + wr << (uint8_t)hashlittle(&v, sizeof(v), 0); + wr << bigEndian64(commitVersion); + + backupMutation.type = MutationRef::SetValue; + partBuffer = NULL; + + val = BinaryWriter::toValue(logRangeMutation.second, IncludeVersion()); + + for (int part = 0; part * CLIENT_KNOBS->MUTATION_BLOCK_SIZE < val.size(); part++) { + + // Assign the second parameter as the part + backupMutation.param2 = val.substr(part * CLIENT_KNOBS->MUTATION_BLOCK_SIZE, + std::min(val.size() - part * CLIENT_KNOBS->MUTATION_BLOCK_SIZE, CLIENT_KNOBS->MUTATION_BLOCK_SIZE)); + + // Write the last part of the mutation to the serialization, if the buffer is not defined + if (!partBuffer) { + // Serialize the part to the writer + wr << bigEndian32(part); + + // Define the last buffer part + partBuffer = (uint32_t*) ((char*) wr.getData() + wr.getLength() - sizeof(uint32_t)); + } + else { + *partBuffer = bigEndian32(part); + } + + // Define the mutation type and and location + backupMutation.param1 = wr.toStringRef(); + ASSERT( backupMutation.param1.startsWith(logRangeMutation.first) ); // We are writing into the configured destination + + auto& tags = self->keyTags[backupMutation.param1]; + for (auto& tag : tags) + toCommit.addTag(tag); + toCommit.addTypedMessage(backupMutation); + +// if (debugMutation("BackupProxyCommit", commitVersion, backupMutation)) { +// TraceEvent("BackupProxyCommitTo", self->dbgid).detail("To", describe(tags)).detail("BackupMutation", backupMutation.toString()) +// .detail("BackupMutationSize", val.size()).detail("Version", commitVersion).detail("destPath", printable(logRangeMutation.first)) +// .detail("partIndex", part).detail("partIndexEndian", bigEndian32(part)).detail("partData", printable(backupMutation.param1)); +// } + } + } + } + + self->stats.mutations += mutationCount; + self->stats.mutationBytes += mutationBytes; + + // Storage servers mustn't make durable versions which are not fully committed (because then they are impossible to roll back) + // We prevent this by limiting the number of versions which are semi-committed but not fully committed to be less than the MVCC window + while (self->committedVersion.get() < commitVersion - SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS) { + // This should be *extremely* rare in the real world, but knob buggification should make it happen in simulation + TEST(true); // Semi-committed pipeline limited by MVCC window + //TraceEvent("ProxyWaitingForCommitted", self->dbgid).detail("CommittedVersion", self->committedVersion.get()).detail("NeedToCommit", commitVersion); + choose{ + when(Void _ = wait(self->committedVersion.whenAtLeast(commitVersion - SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS))) { + Void _ = wait(yield()); + break; + } + when(GetReadVersionReply v = wait(self->getConsistentReadVersion.getReply(GetReadVersionRequest(0, GetReadVersionRequest::PRIORITY_SYSTEM_IMMEDIATE | GetReadVersionRequest::FLAG_CAUSAL_READ_RISKY)))) { + if(v.version > self->committedVersion.get()) { + self->locked = v.locked; + self->committedVersion.set(v.version); + } + + if (self->committedVersion.get() < commitVersion - SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS) + Void _ = wait(delay(SERVER_KNOBS->PROXY_SPIN_DELAY)); + } + } + } + + LogSystemDiskQueueAdapter::CommitMessage msg = wait(storeCommits.back().first); // Should just be doing yields + + if (debugID.present()) + g_traceBatch.addEvent("CommitDebug", debugID.get().first(), "MasterProxyServer.commitBatch.AfterStoreCommits"); + + // txnState (transaction subsystem state) tag: message extracted from log adapter + bool firstMessage = true; + for(auto m : msg.messages) { + toCommit.addTag(txsTag); + toCommit.addMessage(StringRef(m.begin(), m.size()), !firstMessage); + firstMessage = false; + } + + self->logSystem->pop(msg.popTo, txsTag); + + if ( prevVersion && commitVersion - prevVersion < SERVER_KNOBS->MAX_VERSIONS_IN_FLIGHT/2 ) + debug_advanceMaxCommittedVersion( UID(), commitVersion ); //< Is this valid? + + //TraceEvent("ProxyPush", self->dbgid).detail("PrevVersion", prevVersion).detail("Version", commitVersion) + // .detail("TransactionsSubmitted", trs.size()).detail("TransactionsCommitted", commitCount) + // .detail("txsBytes", msg.message.size()).detail("TxsPopTo", msg.popTo); + + if ( prevVersion && commitVersion - prevVersion < SERVER_KNOBS->MAX_VERSIONS_IN_FLIGHT/2 ) + debug_advanceMaxCommittedVersion(UID(), commitVersion); + + Future loggingComplete = self->logSystem->push( prevVersion, commitVersion, self->committedVersion.get(), toCommit, debugID ) + || self->committedVersion.whenAtLeast( commitVersion+1 ); + + if (!forceRecovery) { + ASSERT(self->latestLocalCommitBatchLogging.get() == localBatchNumber-1); + self->latestLocalCommitBatchLogging.set(localBatchNumber); + } + + /////// Phase 4: Logging (network bound; pipelined up to MAX_READ_TRANSACTION_LIFE_VERSIONS (limited by loop above)) + Void _ = wait(loggingComplete); + Void _ = wait(yield()); + + /////// Phase 5: Replies (CPU bound; no particular order required, though ordered execution would be best for latency) + if ( prevVersion && commitVersion - prevVersion < SERVER_KNOBS->MAX_VERSIONS_IN_FLIGHT/2 ) + debug_advanceMinCommittedVersion(UID(), commitVersion); + + //TraceEvent("ProxyPushed", self->dbgid).detail("PrevVersion", prevVersion).detail("Version", commitVersion); + if (debugID.present()) + g_traceBatch.addEvent("CommitDebug", debugID.get().first(), "MasterProxyServer.commitBatch.AfterLogPush"); + + for (auto &p : storeCommits) { + ASSERT(!p.second.isReady()); + p.first.get().acknowledge.send(Void()); + ASSERT(p.second.isReady()); + } + + TEST(self->committedVersion.get() > commitVersion); // A later version was reported committed first + if( commitVersion > self->committedVersion.get() ) { + self->locked = lockedAfter; + self->committedVersion.set(commitVersion); + } + + if (forceRecovery) { + TraceEvent(SevWarn, "RestartingTxnSubsystem", self->dbgid).detail("Stage", "ProxyShutdown"); + throw worker_removed(); + } + + // Send replies to clients + for (int t = 0; t < trs.size(); t++) + { + if (committed[t] == ConflictBatch::TransactionCommitted && (!locked || trs[t].isLockAware)) + trs[t].reply.send(CommitID(commitVersion, t)); + else if (committed[t] == ConflictBatch::TransactionTooOld) + trs[t].reply.sendError(past_version()); + else + trs[t].reply.sendError(not_committed()); + } + + ++self->stats.commitBatchOut; + self->stats.txnCommitOut += trs.size(); + self->stats.txnConflicts += trs.size() - commitCount; + self->stats.txnCommitOutSuccess += commitCount; + + if(now() - self->lastCoalesceTime > SERVER_KNOBS->RESOLVER_COALESCE_TIME) { + self->lastCoalesceTime = now(); + int lastSize = self->keyResolvers.size(); + auto rs = self->keyResolvers.ranges(); + Version oldestVersion = prevVersion - SERVER_KNOBS->MAX_WRITE_TRANSACTION_LIFE_VERSIONS; + for(auto r = rs.begin(); r != rs.end(); ++r) { + while(r->value().size() > 1 && r->value()[1].first < oldestVersion) + r->value().pop_front(); + if(r->value().size() && r->value().front().first < oldestVersion) + r->value().front().first = 0; + } + self->keyResolvers.coalesce(allKeys); + if(self->keyResolvers.size() != lastSize) + TraceEvent("KeyResolverSize", self->dbgid).detail("size", self->keyResolvers.size()); + } + + // Dynamic batching for commits + double target_latency = (now() - t1) * SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_INTERVAL_LATENCY_FRACTION; + *commitBatchTime = + std::max(SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_INTERVAL_MIN, + std::min(SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_INTERVAL_MAX, + target_latency * SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_INTERVAL_SMOOTHER_ALPHA + *commitBatchTime * (1-SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_INTERVAL_SMOOTHER_ALPHA))); + + + return Void(); +} + + +ACTOR Future getLiveCommittedVersion(ProxyCommitData* commitData, uint32_t flags, vector *otherProxies, Optional debugID, int transactionCount, int systemTransactionCount, int defaultPriTransactionCount, int batchPriTransactionCount) +{ + // Returns a version which (1) is committed, and (2) is >= the latest version reported committed (by a commit response) when this request was sent + // (1) The version returned is the committedVersion of some proxy at some point before the request returns, so it is committed. + // (2) No proxy on our list reported committed a higher version before this request was received, because then its committedVersion would have been higher, + // and no other proxy could have already committed anything without first ending the epoch + ++commitData->stats.txnStartBatch; + + state vector> proxyVersions; + for (auto const& p : *otherProxies) + proxyVersions.push_back(brokenPromiseToNever(p.getRawCommittedVersion.getReply(GetRawCommittedVersionRequest(debugID), TaskTLogConfirmRunningReply))); + + if (!(flags&GetReadVersionRequest::FLAG_CAUSAL_READ_RISKY)) + { + Void _ = wait(commitData->logSystem->confirmEpochLive(debugID)); + } + + if (debugID.present()) + g_traceBatch.addEvent("TransactionDebug", debugID.get().first(), "MasterProxyServer.getLiveCommittedVersion.confirmEpochLive"); + + vector versions = wait(getAll(proxyVersions)); + GetReadVersionReply rep; + rep.version = commitData->committedVersion.get(); + rep.locked = commitData->locked; + + for (auto v : versions) { + if(v.version > rep.version) { + rep = v; + } + } + + if (debugID.present()) + g_traceBatch.addEvent("TransactionDebug", debugID.get().first(), "MasterProxyServer.getLiveCommittedVersion.After"); + + commitData->stats.txnStartOut += transactionCount; + commitData->stats.txnSystemPriorityStartOut += systemTransactionCount; + commitData->stats.txnDefaultPriorityStartOut += defaultPriTransactionCount; + commitData->stats.txnBatchPriorityStartOut += batchPriTransactionCount; + + return rep; +} + +ACTOR Future fetchVersions(ProxyCommitData *commitData) { + loop { + Void _ = waitNext(commitData->commitBatchStartNotifications.getFuture()); + GetCommitVersionRequest req(commitData->commitVersionRequestNumber++, commitData->mostRecentProcessedRequestNumber, commitData->dbgid); + commitData->commitBatchVersions.send(brokenPromiseToNever(commitData->master.getCommitVersion.getReply(req))); + } +} + +ACTOR static Future transactionStarter( + MasterProxyInterface proxy, + MasterInterface master, + Reference> db, + PromiseStream> addActor, + ProxyCommitData* commitData + ) +{ + state double lastGRVTime = 0; + state PromiseStream GRVTimer; + state double GRVBatchTime = SERVER_KNOBS->START_TRANSACTION_BATCH_INTERVAL_MIN; + + state int64_t transactionCount = 0; + state double transactionBudget = 0; + state double transactionRate = 10; + state std::priority_queue, std::vector>> transactionQueue; + state vector otherProxies; + + state PromiseStream replyTimes; + addActor.send(getRate(proxy.id(), master, &transactionCount, &transactionRate)); + addActor.send(queueTransactionStartRequests(&transactionQueue, proxy.getConsistentReadVersion.getFuture(), GRVTimer, &lastGRVTime, &GRVBatchTime, replyTimes.getFuture(), &commitData->stats)); + + // Get a list of the other proxies that go together with us + while (std::find(db->get().client.proxies.begin(), db->get().client.proxies.end(), proxy) == db->get().client.proxies.end()) + Void _ = wait(db->onChange()); + for (MasterProxyInterface mp : db->get().client.proxies) { + if (mp != proxy) + otherProxies.push_back(mp); + } + + ASSERT(db->get().recoveryState == RecoveryState::FULLY_RECOVERED); // else potentially we could return uncommitted read versions (since self->committedVersion is only a committed version if this recovery succeeds) + + TraceEvent("ProxyReadyForTxnStarts", proxy.id()); + + loop{ + Void _ = waitNext(GRVTimer.getFuture()); + // Select zero or more transactions to start + double t = now(); + double elapsed = std::min(now() - lastGRVTime, SERVER_KNOBS->START_TRANSACTION_BATCH_INTERVAL_MAX); + lastGRVTime = t; + + if(elapsed == 0) elapsed = 1e-15; // resolve a possible indeterminant multiplication with infinite transaction rate + double nTransactionsToStart = std::min(transactionRate * elapsed, SERVER_KNOBS->START_TRANSACTION_MAX_TRANSACTIONS_TO_START) + transactionBudget; + + int transactionsStarted[2] = {0,0}; + int systemTransactionsStarted[2] = {0,0}; + int defaultPriTransactionsStarted[2] = { 0, 0 }; + int batchPriTransactionsStarted[2] = { 0, 0 }; + + vector>> start(2); // start[0] is transactions starting with !(flags&CAUSAL_READ_RISKY), start[1] is transactions starting with flags&CAUSAL_READ_RISKY + Optional debugID; + + double leftToStart = 0; + while (!transactionQueue.empty()) { + auto& req = transactionQueue.top().first; + int tc = req.transactionCount; + leftToStart = nTransactionsToStart - transactionsStarted[0] - transactionsStarted[1]; + + bool startNext = tc < leftToStart || req.priority() >= GetReadVersionRequest::PRIORITY_SYSTEM_IMMEDIATE || tc * g_random->random01() < leftToStart - std::max(0.0, transactionBudget); + if (!startNext) break; + + if (req.debugID.present()) { + if (!debugID.present()) debugID = g_nondeterministic_random->randomUniqueID(); + g_traceBatch.addAttach("TransactionAttachID", req.debugID.get().first(), debugID.get().first()); + } + start[req.flags & 1].push_back(std::move(req.reply)); static_assert(GetReadVersionRequest::FLAG_CAUSAL_READ_RISKY == 1, "Implementation dependent on flag value"); + + transactionsStarted[req.flags&1] += tc; + if (req.priority() >= GetReadVersionRequest::PRIORITY_SYSTEM_IMMEDIATE) + systemTransactionsStarted[req.flags & 1] += tc; + else if (req.priority() >= GetReadVersionRequest::PRIORITY_DEFAULT) + defaultPriTransactionsStarted[req.flags & 1] += tc; + else + batchPriTransactionsStarted[req.flags & 1] += tc; + + transactionQueue.pop(); + } + + if (!transactionQueue.empty()) + forwardPromise(GRVTimer, delayJittered(SERVER_KNOBS->START_TRANSACTION_BATCH_QUEUE_CHECK_INTERVAL, TaskProxyGRVTimer)); + + /*TraceEvent("GRVBatch", proxy.id()) + .detail("elapsed", elapsed) + .detail("nTransactionToStart", nTransactionsToStart) + .detail("transactionRate", transactionRate) + .detail("transactionQueueSize", transactionQueue.size()) + .detail("numTransactionsStarted", transactionsStarted[0] + transactionsStarted[1]) + .detail("numSystemTransactionsStarted", systemTransactionsStarted[0] + systemTransactionsStarted[1]) + .detail("numNonSystemTransactionsStarted", transactionsStarted[0] + transactionsStarted[1] - systemTransactionsStarted[0] - systemTransactionsStarted[1]) + .detail("transactionBudget", transactionBudget) + .detail("lastLeftToStart", leftToStart);*/ + + // dynamic batching + ReplyPromise GRVReply; + if (start[0].size()){ + start[0].push_back(GRVReply); // for now, base dynamic batching on the time for normal requests (not read_risky) + addActor.send(timeReply(GRVReply.getFuture(), replyTimes)); + } + + transactionCount += transactionsStarted[0] + transactionsStarted[1]; + transactionBudget = std::max(std::min(nTransactionsToStart - transactionsStarted[0] - transactionsStarted[1], SERVER_KNOBS->START_TRANSACTION_MAX_BUDGET_SIZE), -SERVER_KNOBS->START_TRANSACTION_MAX_BUDGET_SIZE); + if (debugID.present()) + g_traceBatch.addEvent("TransactionDebug", debugID.get().first(), "MasterProxyServer.masterProxyServerCore.Broadcast"); + for (int i = 0; i readRequestServer( + MasterProxyInterface proxy, + ProxyCommitData* commitData + ) +{ + // Implement read-only parts of the proxy interface + + // We can't respond to these requests until we have valid txnStateStore + Void _ = wait(commitData->validState.getFuture()); + + TraceEvent("ProxyReadyForReads", proxy.id()); + + loop choose{ + when(ReplyPromise> req = waitNext(proxy.getKeyServersLocations.getFuture())) { + // SOMEDAY: keep ssis around? + vector src, dest; + decodeKeyServersValue(commitData->txnStateStore->readValue(keyServersKeyServersKey).get().get(), src, dest); + vector ssis; + ssis.reserve(src.size()); + for (auto const& id : src) { + ssis.push_back(decodeServerListValue(commitData->txnStateStore->readValue(serverListKeyFor(id)).get().get())); + } + + req.send(ssis); + } + when(GetStorageServerRejoinInfoRequest req = waitNext(proxy.getStorageServerRejoinInfo.getFuture())) { + if (commitData->txnStateStore->readValue(serverListKeyFor(req.id)).get().present()) { + GetStorageServerRejoinInfoReply rep; + rep.version = commitData->version; + rep.tag = decodeServerTagValue( commitData->txnStateStore->readValue(serverTagKeyFor(req.id)).get().get() ); + req.reply.send(rep); + } else + req.reply.sendError(worker_removed()); + } + } +} + +ACTOR Future masterProxyServerCore( + MasterProxyInterface proxy, + MasterInterface master, + Reference> db, + LogEpoch epoch, + Version recoveryTransactionVersion, + bool firstProxy) +{ + state ProxyCommitData commitData(proxy.id(), master, proxy.getConsistentReadVersion, recoveryTransactionVersion, proxy.commit, db, firstProxy); + + state Future sequenceFuture = (Sequence)0; + state PromiseStream< vector > batchedCommits; + state Future commitBatcher; + + state Future lastCommitComplete = Void(); + + state PromiseStream> addActor; + state Future onError = actorCollection(addActor.getFuture()); + state double lastCommit = 0; + state std::set txnSequences; + state Sequence maxSequence = std::numeric_limits::max(); + state double commitBatchInterval = SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_INTERVAL_MIN; + + addActor.send( fetchVersions(&commitData) ); + addActor.send( waitFailureServer(proxy.waitFailure.getFuture()) ); + + //TraceEvent("ProxyInit1", proxy.id()); + + // Wait until we can load the "real" logsystem, since we don't support switching them currently + while (!(db->get().master.id() == master.id() && db->get().recoveryState >= RecoveryState::RECOVERY_TRANSACTION)) { + //TraceEvent("ProxyInit2", proxy.id()).detail("LSEpoch", db->get().logSystemConfig.epoch).detail("Need", epoch); + Void _ = wait(db->onChange()); + } + + //TraceEvent("ProxyInit3", proxy.id()); + + commitData.resolvers = db->get().resolvers; + ASSERT(commitData.resolvers.size() != 0); + + auto rs = commitData.keyResolvers.modify(allKeys); + for(auto r = rs.begin(); r != rs.end(); ++r) + r->value().push_back(std::make_pair(0,0)); + + commitData.logSystem = ILogSystem::fromServerDBInfo(proxy.id(), db->get()); + commitData.logAdapter = new LogSystemDiskQueueAdapter(commitData.logSystem, txsTag, false); + commitData.txnStateStore = keyValueStoreLogSystem(commitData.logAdapter, proxy.id(), 2e9, true); + onError = onError || commitData.logSystem->onError(); + + addActor.send(transactionStarter(proxy, master, db, addActor, &commitData)); + addActor.send(readRequestServer(proxy, &commitData)); + + // wait for txnStateStore recovery + Optional _ = wait(commitData.txnStateStore->readValue(StringRef())); + + int commitBatchByteLimit = + (int)std::min(SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_BYTES_MAX, + std::max(SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_BYTES_MIN, + SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_BYTES_SCALE_BASE * pow(db->get().client.proxies.size(), SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_BYTES_SCALE_POWER))); + commitBatcher = batcher(batchedCommits, proxy.commit.getFuture(), SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_INTERVAL_FROM_IDLE, &commitBatchInterval, SERVER_KNOBS->MAX_COMMIT_BATCH_INTERVAL, SERVER_KNOBS->COMMIT_TRANSACTION_BATCH_COUNT_MAX, commitBatchByteLimit, CLIENT_KNOBS->TRANSACTION_SIZE_LIMIT, commitData.commitBatchStartNotifications, TaskProxyCommitBatcher, &commitData.stats.txnCommitIn); + loop choose{ + when(Void _ = wait(onError)) {} + when(vector trs = waitNext(batchedCommits.getFuture())) { + //TraceEvent("MasterProxyCTR", proxy.id()).detail("CommitTransactions", trs.size()).detail("TransactionRate", transactionRate).detail("TransactionQueue", transactionQueue.size()).detail("ReleasedTransactionCount", transactionCount); + if (trs.size() || (db->get().recoveryState == RecoveryState::FULLY_RECOVERED && now() - lastCommit >= SERVER_KNOBS->MAX_COMMIT_BATCH_INTERVAL)) { + lastCommit = now(); + + if (trs.size() || lastCommitComplete.isReady()) { + lastCommitComplete = commitBatch(&commitData, trs, &commitBatchInterval); + addActor.send(lastCommitComplete); + } + } + } + when(GetRawCommittedVersionRequest req = waitNext(proxy.getRawCommittedVersion.getFuture())) { + //TraceEvent("ProxyGetRCV", proxy.id()); + if (req.debugID.present()) + g_traceBatch.addEvent("TransactionDebug", req.debugID.get().first(), "MasterProxyServer.masterProxyServerCore.GetRawCommittedVersion"); + GetReadVersionReply rep; + rep.locked = commitData.locked; + rep.version = commitData.committedVersion.get(); + req.reply.send(rep); + } + when(TxnStateRequest req = waitNext(proxy.txnState.getFuture())) { + state ReplyPromise reply = req.reply; + if(req.last) maxSequence = req.sequence + 1; + if (!txnSequences.count(req.sequence)) { + txnSequences.insert(req.sequence); + + ASSERT(!commitData.validState.isSet()); // Although we may receive the CommitTransactionRequest for the recovery transaction before all of the TxnStateRequest, we will not get a resolution result from any resolver until the master has submitted its initial (sequence 0) resolution request, which it doesn't do until we have acknowledged all TxnStateRequests + + for(auto& kv : req.data) + commitData.txnStateStore->set(kv, &req.arena); + commitData.txnStateStore->commit(true); + + if(txnSequences.size() == maxSequence) { + state KeyRange txnKeys = allKeys; + loop { + Void _ = wait(yield()); + Standalone> data = commitData.txnStateStore->readRange(txnKeys, SERVER_KNOBS->BUGGIFIED_ROW_LIMIT, SERVER_KNOBS->APPLY_MUTATION_BYTES).get(); + if(!data.size()) break; + ((KeyRangeRef&)txnKeys) = KeyRangeRef( keyAfter(data.back().key, txnKeys.arena()), txnKeys.end ); + + Standalone> mutations; + std::vector>,int>> keyTagData; + vector src, dest; + std::set tags; + Tag tag; + for(auto &kv : data) { + if( kv.key.startsWith(keyServersPrefix) ) { + KeyRef k = kv.key.removePrefix(keyServersPrefix); + if(k != allKeys.end) { + decodeKeyServersValue(kv.value, src, dest); + tags.clear(); + for(auto id : src) { + auto tagItr = commitData.tagCache.find(id); + if(tagItr == commitData.tagCache.end()) { + tag = decodeServerTagValue( commitData.txnStateStore->readValue( serverTagKeyFor(id) ).get().get() ); + commitData.tagCache[id] = tag; + } else { + tag = tagItr->second; + } + tags.insert( tag ); + } + for(auto id : dest) { + auto tagItr = commitData.tagCache.find(id); + if(tagItr == commitData.tagCache.end()) { + tag = decodeServerTagValue( commitData.txnStateStore->readValue( serverTagKeyFor(id) ).get().get() ); + commitData.tagCache[id] = tag; + } else { + tag = tagItr->second; + } + tags.insert( tag ); + } + keyTagData.push_back( std::make_pair(MapPair>(k, std::vector(tags.begin(), tags.end())), 1) ); + } + } else { + mutations.push_back(mutations.arena(), MutationRef(MutationRef::SetValue, kv.key, kv.value)); + } + } + + //insert keyTag data separately from metadata mutations so that we can do one bulk insert which avoids a lot of map lookups. + commitData.keyTags.rawInsert(keyTagData); + + Arena arena; + bool confChanges; + applyMetadataMutations(commitData.dbgid, arena, mutations, commitData.txnStateStore, NULL, &confChanges, Reference(), 0, &commitData.vecBackupKeys, &commitData.keyTags, commitData.firstProxy ? &commitData.uid_applyMutationsData : NULL, commitData.commit, commitData.cx, &commitData.committedVersion, &commitData.tagCache, true); + } + + auto lockedKey = commitData.txnStateStore->readValue(databaseLockedKey).get(); + commitData.locked = lockedKey.present() && lockedKey.get().size(); + + commitData.txnStateStore->enableSnapshot(); + } + } + reply.send(Void()); + Void _ = wait(yield()); + } + } +} + +ACTOR Future checkRemoved(Reference> db, uint64_t recoveryCount, MasterProxyInterface myInterface) { + loop{ + if (db->get().recoveryCount >= recoveryCount && !std::count(db->get().client.proxies.begin(), db->get().client.proxies.end(), myInterface)) + throw worker_removed(); + Void _ = wait(db->onChange()); + } +} + +ACTOR Future masterProxyServer( + MasterProxyInterface proxy, + InitializeMasterProxyRequest req, + Reference> db) +{ + try { + state Future core = masterProxyServerCore(proxy, req.master, db, req.recoveryCount, req.recoveryTransactionVersion, req.firstProxy); + loop choose{ + when(Void _ = wait(core)) { return Void(); } + when(Void _ = wait(checkRemoved(db, req.recoveryCount, proxy))) {} + } + } + catch (Error& e) { + if (e.code() == error_code_actor_cancelled || e.code() == error_code_worker_removed || e.code() == error_code_tlog_stopped || + e.code() == error_code_master_tlog_failed || e.code() == error_code_coordinators_changed || e.code() == error_code_coordinated_state_conflict || + e.code() == error_code_new_coordinators_timed_out) + { + TraceEvent("MasterProxyTerminated", proxy.id()).error(e, true); + return Void(); + } + throw; + } +} diff --git a/fdbserver/MetricLogger.actor.cpp b/fdbserver/MetricLogger.actor.cpp new file mode 100644 index 0000000000..4cdbb278f7 --- /dev/null +++ b/fdbserver/MetricLogger.actor.cpp @@ -0,0 +1,494 @@ +/* + * MetricLogger.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/UnitTest.h" +#include "flow/TDMetric.actor.h" +#include "fdbclient/DatabaseContext.h" +#include "fdbclient/ReadYourWrites.h" +#include "fdbclient/KeyBackedTypes.h" +#include + +struct MetricsRule { + MetricsRule(bool enabled = false, int minLevel = 0, StringRef const &name = StringRef()) : enabled(enabled), minLevel(minLevel), namePattern(name) {} + + Standalone typePattern; + Standalone namePattern; + Standalone addressPattern; + Standalone idPattern; + + bool enabled; + int minLevel; + + Tuple pack() const { + return Tuple().append(namePattern).append(typePattern).append(addressPattern).append(idPattern).append(enabled ? 1 : 0).append(minLevel); + } + + static inline MetricsRule unpack(Tuple const &t) { + MetricsRule r; + int i = 0; + if(i < t.size()) + r.namePattern = t.getString(i++); + if(i < t.size()) + r.typePattern = t.getString(i++); + if(i < t.size()) + r.addressPattern = t.getString(i++); + if(i < t.size()) + r.idPattern = t.getString(i++); + if(i < t.size()) + r.enabled = t.getInt(i++) != 0; + if(i < t.size()) + r.minLevel = (int)t.getInt(i++); + return r; + } + + // For now this just returns true if pat is in subject. Returns true if pat is empty. + // TODO: Support more complex patterns? + static inline bool patternMatch(StringRef const &pat, StringRef const &subject) { + if(pat.size() == 0) + return true; + for(int i = 0, iend = subject.size() - pat.size() + 1; i < iend; ++i) + if(subject.substr(i, pat.size()) == pat) + return true; + return false; + } + + bool applyTo(BaseMetric *m, StringRef const &address) const { + if(!patternMatch(addressPattern, address)) + return false; + if(!patternMatch(namePattern, m->metricName.name)) + return false; + if(!patternMatch(typePattern, m->metricName.type)) + return false; + if(!patternMatch(idPattern, m->metricName.id)) + return false; + + m->setConfig(enabled, minLevel); + return true; + } +}; + +struct MetricsConfig { + MetricsConfig(Key prefix = KeyRef()) + : space(prefix), + ruleMap(space.get(LiteralStringRef("Rules")).key()), + addressMap(space.get(LiteralStringRef("Enum")).get(LiteralStringRef("Address")).key()), + nameAndTypeMap(space.get(LiteralStringRef("Enum")).get(LiteralStringRef("NameType")).key()), + ruleChangeKey(space.get(LiteralStringRef("RulesChanged")).key()), + enumsChangeKey(space.get(LiteralStringRef("EnumsChanged")).key()), + fieldChangeKey(space.get(LiteralStringRef("FieldsChanged")).key()) + { + } + + Subspace space; + + typedef KeyBackedMap RuleMapT; + RuleMapT ruleMap; + RuleMapT::PairsType rules; + + KeyBackedMap addressMap; + KeyBackedMap, int64_t> nameAndTypeMap; + + Key ruleChangeKey; + Key enumsChangeKey; + Key fieldChangeKey; +}; + +/* + Rule Updater: + + For now: + Read and store all rules locally. + For each metric + Disable the metric + For each rule in reverse order, apply the rule to the metric and stop if it returns true + Wait for rule change, repeat. + + If this gets too slow, yields can be added but at the cost of potentially missing a few data points + because a metric was disabled and not yet re-enabled before it was logged. + + Or, rules and metrics can be stored for more efficient matching and rule updates can be applied + differentially. + Read all rules, store latest version + Clear all configs for each registered metric + For each rule in order, + Efficiently select matching metrics and set config + Loop + Wait for rule change + If rule version skipped, go back to start of rule updater + Read only new rules. + For each new rule, in order + Remove any old rules that new rule completely covers + Efficiently select matching metrics and set config + Go back to wait for rule change +*/ +ACTOR Future metricRuleUpdater(Database cx, MetricsConfig *config, TDMetricCollection *collection) { + + state Reference tr(new ReadYourWritesTransaction(cx)); + + loop { + state Future newMetric = collection->metricAdded.onTrigger(); + try { + tr->setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS); + MetricsConfig::RuleMapT::PairsType rules = wait(config->ruleMap.getRange(tr, 0, {}, 1e6)); + + for(auto &it : collection->metricMap) { + it.value->setConfig(false); + for(auto i = rules.rbegin(); !(i == rules.rend()); ++i) + if(i->second.applyTo(it.value.getPtr(), collection->address)) + break; + } + config->rules = std::move(rules); + + state Future rulesChanged = tr->watch(config->ruleChangeKey); + Void _ = wait(tr->commit()); + Void _ = wait(rulesChanged || newMetric); + tr->reset(); + + } catch(Error &e) { + Void _ = wait(tr->onError(e)); + } + } +} + +// Implementation of IMetricDB +class MetricDB : public IMetricDB { +public: + MetricDB(ReadYourWritesTransaction *tr = NULL) : tr(tr) {} + ~MetricDB() {} + + // levelKey is the prefix for the entire level, no timestamp at the end + ACTOR static Future>> getLastBlock_impl(ReadYourWritesTransaction *tr, Standalone levelKey) { + Standalone results = wait(tr->getRange(normalKeys.withPrefix(levelKey), 1, true, true)); + if(results.size() == 1) + return results[0].value; + return Optional>(); + } + + Future>> getLastBlock(Standalone key) { + return getLastBlock_impl(tr, key); + } + + ReadYourWritesTransaction *tr; +}; + +ACTOR Future dumpMetrics(Database cx, MetricsConfig *config, TDMetricCollection *collection) { + state MetricUpdateBatch batch; + state Standalone mk; + ASSERT(collection != nullptr); + mk.prefix = StringRef(mk.arena(), config->space.key()); + mk.address = StringRef(mk.arena(), collection->address); + + loop { + batch.clear(); + uint64_t rollTime = std::numeric_limits::max(); + if(collection->rollTimes.size()) { + rollTime = collection->rollTimes.front(); + collection->rollTimes.pop_front(); + } + + // Are any metrics enabled? + state bool enabled = false; + + // Flush data for each metric, track if any are enabled. + for( auto &it : collection->metricMap) { + // If this metric was ever enabled at all then flush it + if(it.value->pCollection != nullptr) { + mk.name = it.value->metricName; + it.value->flushData(mk, rollTime, batch); + } + enabled = enabled || it.value->enabled; + } + + if(rollTime == std::numeric_limits::max()) { + collection->currentTimeBytes = 0; + } + + state ReadYourWritesTransaction cbtr(cx); + state MetricDB mdb(&cbtr); + + state std::map> results; + // Call all of the callbacks, map each index to its resulting future + for(int i = 0, iend = batch.callbacks.size(); i < iend; ++i) + results[i] = batch.callbacks[i](&mdb, &batch); + + loop { + state std::map>::iterator cb = results.begin(); + // Wait for each future, return the ones that succeeded + state Error lastError; + while(cb != results.end()) { + try { + Void _ = wait(cb->second); + cb = results.erase(cb); + } catch(Error &e) { + ++cb; + lastError = e; + } + } + + // If all the callbacks completed then we're done. + if(results.empty()) + break; + + // Otherwise, wait to retry + Void _ = wait(cbtr.onError(lastError)); + for(auto &cb : results) + cb.second = batch.callbacks[cb.first](&mdb, &batch); + } + + // If there are more rolltimes then next dump is now, otherwise if no metrics are enabled then it is + // whenever the next metric is enabled but if there are metrics enabled then it is in 1 second. + state Future nextDump; + if(collection->rollTimes.size() > 0) + nextDump = Void(); + else { + nextDump = collection->metricEnabled.onTrigger(); + if(enabled) + nextDump = nextDump || delay(1.0); + } + + state Transaction tr( cx ); + loop { + tr.setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS); + try { + for(auto &i : batch.inserts) { + //fprintf(stderr, "%s: dump insert: %s\n", collection->address.toString().c_str(), printable(allInsertions[i].key).c_str()); + tr.set(i.key, i.value() ); + } + + for(auto &a : batch.appends) { + //fprintf(stderr, "%s: dump append: %s\n", collection->address.toString().c_str(), printable(allAppends[i].key).c_str()); + tr.atomicOp(a.key, a.value(), MutationRef::AppendIfFits); + } + + for(auto &u : batch.updates) { + //fprintf(stderr, "%s: dump update: %s\n", collection->address.toString().c_str(), printable(allUpdates[i].first).c_str()); + tr.set(u.first, u.second); + } + + Void _ = wait( tr.commit() ); + break; + } catch( Error &e ) { + Void _ = wait( tr.onError( e ) ); + } + } + Void _ = wait( nextDump ); + } +} + +// Push metric field registrations to database. +ACTOR Future updateMetricRegistration(Database cx, MetricsConfig *config, TDMetricCollection *collection) { + state Standalone mk; + mk.prefix = StringRef(mk.arena(), config->space.key()); + mk.address = StringRef(mk.arena(), collection->address); + + state bool addressRegistered = false; + + loop { + state Future registrationChange = collection->metricRegistrationChanged.onTrigger(); + state Future newMetric = collection->metricAdded.onTrigger(); + state vector> keys; + state bool fieldsChanged = false; + state bool enumsChanged = false; + + // Register each metric that isn't already registered + for( auto &it : collection->metricMap) { + if(!it.value->registered) { + // Register metric so it can create its field keys + mk.name = it.value->metricName; + it.value->registerFields(mk, keys); + + // Also set keys for the metric's (name,type) pair in the type-and-name map + keys.push_back(config->nameAndTypeMap.getProperty({it.value->metricName.name, it.value->metricName.type}).key); + + it.value->registered = true; + fieldsChanged = true; + enumsChanged = true; + } + } + + // Set a key for this collection's address in the address map if it hasn't been done. + if(!addressRegistered) { + keys.push_back(config->addressMap.getProperty(collection->address).key); + addressRegistered = true; + enumsChanged = true; + } + + if(enumsChanged) + keys.push_back(config->enumsChangeKey); + if(fieldsChanged) + keys.push_back(config->fieldChangeKey); + + // Write keys collected to database + state Transaction tr(cx); + loop { + tr.setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS); + try { + Value timestamp = BinaryWriter::toValue(CompressedInt(now()), AssumeVersion(currentProtocolVersion)); + for(auto &key : keys) { + //fprintf(stderr, "%s: register: %s\n", collection->address.toString().c_str(), printable(key).c_str()); + tr.set(key, timestamp); + } + + Void _ = wait(tr.commit()); + break; + } catch(Error &e) { + Void _ = wait(tr.onError(e)); + } + } + + // Wait for a metric to require registration or a new metric to be added + Void _ = wait(registrationChange || newMetric); + } +} + +ACTOR Future runMetrics( Future fcx, Key prefix ) { + // Never log to an empty prefix, it's pretty much always a bad idea. + if(prefix.size() == 0) { + TraceEvent(SevWarnAlways, "TDMetricsRefusingEmptyPrefix"); + return Void(); + } + + // Wait until the collection has been created and initialized. + state TDMetricCollection *metrics = nullptr; + loop { + metrics = TDMetricCollection::getTDMetrics(); + if(metrics != nullptr) + if(metrics->init()) + break; + Void _ = wait(delay(1.0)); + } + + state MetricsConfig config(prefix); + + try { + Database cx = wait( fcx ); + Future conf = metricRuleUpdater(cx, &config, metrics); + Future dump = dumpMetrics(cx, &config, metrics); + Future reg = updateMetricRegistration(cx, &config, metrics); + + Void _ = wait( conf || dump || reg); + } catch( Error &e ) { + if( e.code() != error_code_actor_cancelled ) { + // Disable all metrics + for( auto &it : metrics->metricMap) + it.value->setConfig(false); + } + + TraceEvent(SevWarnAlways, "TDMetricsStopped").error(e); + throw e; + } + return Void(); +} + +TEST_CASE("fdbserver/metrics/TraceEvents") { + auto getenv2 = [](const char *s) -> const char * {s = getenv(s); return s ? s : ""; }; + std::string metricsConnFile = getenv2("METRICS_CONNFILE"); + std::string metricsPrefix = getenv2("METRICS_PREFIX"); + if(metricsConnFile == "") { + fprintf(stdout, "Metrics cluster file must be specified in environment variable METRICS_CONNFILE\n"); + return Void(); + } + fprintf(stdout, "Using environment variables METRICS_CONNFILE and METRICS_PREFIX.\n"); + + state Reference metricsCluster = Cluster::createCluster( metricsConnFile, Cluster::API_VERSION_LATEST ); + TDMetricCollection::getTDMetrics()->address = LiteralStringRef("0.0.0.0:0"); + state Future metrics = runMetrics(metricsCluster->createDatabase(LiteralStringRef("DB")), KeyRef(metricsPrefix)); + state int64_t x = 0; + + state double w = 0.5; + state int chunk = 4000; + state int total = 200000; + + fprintf(stdout, "Writing trace event named Dummy with fields a, b, c, d, j, k, s, x, y, z.\n"); + fprintf(stdout, " There is a %f second pause every %d events\n", w, chunk); + fprintf(stdout, " %d events will be logged.\n", total); + fprintf(stdout, " a is always present. It starts with = 0 and increments by 1 with each event.\n"); + fprintf(stdout, " b, if present, is always a*2.\n"); + fprintf(stdout, " c, if present, is always a*3.\n"); + fprintf(stdout, " b and c are never present in the same event.\n"); + fprintf(stdout, " x, y, and z, if present, are doubles and equal to 1.5 * a, b, and c, respectively\n"); + fprintf(stdout, " d is always present, is a string, and rotates through the values 'one', 'two', and ''.\n"); + fprintf(stdout, " Plotting j on the x axis and k on the y axis should look like x=sin(2t), y=sin(3t)\n"); + + state Int64MetricHandle intMetric = Int64MetricHandle(LiteralStringRef("DummyInt")); + state BoolMetricHandle boolMetric = BoolMetricHandle(LiteralStringRef("DummyBool")); + state StringMetricHandle stringMetric = StringMetricHandle(LiteralStringRef("DummyString")); + + static const char * dStrings[] = {"one", "two", ""}; + state const char **d = dStrings; + state Arena arena; + + loop { + double sstart = x; + for(int i = 0; i < chunk; ++i, ++x) { + intMetric = x; + boolMetric = (x % 2) > 0; + const char *s = d[x % 3]; + // s doesn't actually require an arena + stringMetric = Standalone(StringRef((uint8_t *)s, strlen(s)), arena); + + TraceEvent("Dummy") + .detail("a", x) + .detail("x", 1.5 * x) + .detail("d", s) + .detail("j", sin(2.0 * x)) + .detail("k", sin(3.0 * x)) + .detail("s", sstart + (double)chunk * sin(10.0 * i / chunk)); + } + Void _ = wait(delay(w)); + + double sstart = x; + for(int i = 0; i < chunk; ++i, ++x) { + intMetric = x; + boolMetric = x % 2 > 0; + TraceEvent("Dummy") + .detail("a", x) + .detail("x", 1.5 * x) + .detail("b", x*2) + .detail("y", 3.0 * x) + .detail("d", d[x % 3]) + .detail("j", sin(2.0 * x)) + .detail("k", sin(3.0 * x)) + .detail("s", sstart + (double)chunk * sin(40.0 * i / chunk)); + } + Void _ = wait(delay(w)); + + double sstart = x; + for(int i = 0; i < chunk; ++i, ++x) { + intMetric = x; + boolMetric = x % 2 > 0; + TraceEvent("Dummy") + .detail("a", x) + .detail("x", 1.5 * x) + .detail("c", x*3) + .detail("z", 4.5 * x) + .detail("d", d[x % 3]) + .detail("j", sin(2.0 * x)) + .detail("k", sin(3.0 * x)) + .detail("s", sstart + (double)chunk * sin(160.0 * i / chunk)); + } + Void _ = wait(delay(w)); + + if(x >= total) + return Void(); + } +} + diff --git a/fdbserver/MoveKeys.actor.cpp b/fdbserver/MoveKeys.actor.cpp new file mode 100644 index 0000000000..c3f1809423 --- /dev/null +++ b/fdbserver/MoveKeys.actor.cpp @@ -0,0 +1,836 @@ +/* + * MoveKeys.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "fdbrpc/FailureMonitor.h" +#include "fdbclient/SystemData.h" +#include "MoveKeys.h" +#include "Knobs.h" + +using std::min; +using std::max; + +ACTOR Future takeMoveKeysLock( Database cx, UID masterId ) { + state Transaction tr(cx); + loop { + try { + state MoveKeysLock lock; + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + if( !g_network->isSimulated() ) { + UID id(g_random->randomUniqueID()); + TraceEvent("TakeMoveKeysLockTransaction", masterId) + .detail("TransactionUID", id); + tr.debugTransaction( id ); + } + Optional readVal = wait( tr.get( moveKeysLockOwnerKey ) ); + lock.prevOwner = readVal.present() ? BinaryReader::fromStringRef(readVal.get(), Unversioned()) : UID(); + Optional readVal = wait( tr.get( moveKeysLockWriteKey ) ); + lock.prevWrite = readVal.present() ? BinaryReader::fromStringRef(readVal.get(), Unversioned()) : UID(); + lock.myOwner = g_random->randomUniqueID(); + return lock; + } catch (Error &e){ + Void _ = wait(tr.onError(e)); + TEST(true); // takeMoveKeysLock retry + } + } +} + +ACTOR Future checkMoveKeysLock( Transaction* tr, MoveKeysLock lock, bool isWrite = true ) { + Optional readVal = wait( tr->get( moveKeysLockOwnerKey ) ); + UID currentOwner = readVal.present() ? BinaryReader::fromStringRef(readVal.get(), Unversioned()) : UID(); + + if (currentOwner == lock.prevOwner) { + // Check that the previous owner hasn't touched the lock since we took it + Optional readVal = wait( tr->get( moveKeysLockWriteKey ) ); + UID lastWrite = readVal.present() ? BinaryReader::fromStringRef(readVal.get(), Unversioned()) : UID(); + if (lastWrite != lock.prevWrite) { + TEST(true); // checkMoveKeysLock: Conflict with previous owner + throw movekeys_conflict(); + } + + // Take the lock + if(isWrite) { + BinaryWriter wrMyOwner(Unversioned()); wrMyOwner << lock.myOwner; + tr->set( moveKeysLockOwnerKey, wrMyOwner.toStringRef() ); + BinaryWriter wrLastWrite(Unversioned()); wrLastWrite << g_random->randomUniqueID(); + tr->set( moveKeysLockWriteKey, wrLastWrite.toStringRef() ); + } + + return Void(); + } else if (currentOwner == lock.myOwner) { + if(isWrite) { + // Touch the lock, preventing overlapping attempts to take it + BinaryWriter wrLastWrite(Unversioned()); wrLastWrite << g_random->randomUniqueID(); + tr->set( moveKeysLockWriteKey, wrLastWrite.toStringRef() ); + // Make this transaction self-conflicting so the database will not execute it twice with the same write key + tr->makeSelfConflicting(); + } + + return Void(); + } else { + TEST(true); // checkMoveKeysLock: Conflict with new owner + throw movekeys_conflict(); + } +} + +Future checkMoveKeysLockReadOnly( Transaction* tr, MoveKeysLock lock ) { + return checkMoveKeysLock(tr, lock, false); +} + +ACTOR Future checkReadWrite( Future< ErrorOr > fReply ) { + ErrorOr reply = wait( fReply ); + if (!reply.present()) + return false; + return true; +} + +Future removeOldDestinations(Transaction *tr, UID oldDest, VectorRef shards, KeyRangeRef currentKeys) { + KeyRef beginKey = currentKeys.begin; + + vector> actors; + for(int i = 0; i < shards.size(); i++) { + if(beginKey < shards[i].begin) + actors.push_back(krmSetRangeCoalescing(tr, serverKeysPrefixFor(oldDest), KeyRangeRef(beginKey, shards[i].begin), allKeys, serverKeysFalse)); + + beginKey = shards[i].end; + } + + if(beginKey < currentKeys.end) + actors.push_back(krmSetRangeCoalescing(tr, serverKeysPrefixFor(oldDest), KeyRangeRef(beginKey, currentKeys.end), allKeys, serverKeysFalse)); + + return waitForAll(actors); +} + +//Returns a vector with as many elements as the shards vector +//Result[i] == true if there are enough src servers for shards[i] that have the state isReadWrite +ACTOR Future>> checkEnoughServersOk(Standalone shards, int durableStorageQuorum, UID relocationIntervalId, Transaction* tr) { + state std::vector> enoughServersOk; + state std::map ssiMap; + state vector>> serverListEntries; + + state int i = 0; + for(i = 0; i < shards.size() - 1; ++i) { + KeyRangeRef rangeIntersectKeys( shards[i].key, shards[i+1].key ); + vector src; + vector dest; + + decodeKeyServersValue( shards[i].value, src, dest ); + + for(int s=0; sget( serverListKeyFor(src[s]) ) ); + } + } + + state vector> serverListValues = wait( getAll(serverListEntries) ); + + for(int s=0; s src; + vector dest; + vector storageServerInterfaces; + Future shardServersOk = true; + + decodeKeyServersValue( shards[i].value, src, dest ); + + for(int s=0; s > checks; + for(int s=0; sSERVER_READY_QUORUM_INTERVAL, 0, TaskMoveKeys ) ) ); + } + + shardServersOk = quorumEqualsTrue( checks, durableStorageQuorum ); + } + } + + enoughServersOk.push_back(shardServersOk); + } + + Void _ = wait(waitForAll(enoughServersOk)); + Standalone> result; + + for(int i = 0; i < enoughServersOk.size(); ++i) + result.push_back(result.arena(), enoughServersOk[i].get()); + + return result; +} + +// Set keyServers[keys].dest = servers +// Set serverKeys[servers][keys] = active for each subrange of keys that the server did not already have, complete for each subrange that it already has +// Set serverKeys[dest][keys] = "" for the dest servers of each existing shard in keys (unless that destination is a member of servers OR if the source list is sufficiently degraded) +ACTOR Future startMoveKeys( Database occ, KeyRange keys, vector servers, + MoveKeysLock lock, int durableStorageQuorum, + FlowLock *startMoveKeysLock, UID relocationIntervalId ) { + state TraceInterval interval("RelocateShard_StartMoveKeys"); + //state TraceInterval waitInterval(""); + + Void _ = wait( startMoveKeysLock->take( TaskDataDistributionLaunch ) ); + state FlowLock::Releaser releaser( *startMoveKeysLock ); + + TraceEvent(SevDebug, interval.begin(), relocationIntervalId); + + try { + state Key begin = keys.begin; + state int batches = 0; + state int shards = 0; + state int maxRetries = 0; + + //This process can be split up into multiple transactions if there are too many existing overlapping shards + //In that case, each iteration of this loop will have begin set to the end of the last processed shard + while(begin < keys.end) { + TEST(begin > keys.begin); //Multi-transactional startMoveKeys + batches++; + + state Transaction tr( occ ); + state int retries = 0; + + loop { + try { + retries++; + + //Keep track of old dests that may need to have ranges removed from serverKeys + state std::set oldDests; + + //Keep track of shards for all src servers so that we can preserve their values in serverKeys + state Map> shardMap; + + tr.info.taskID = TaskMoveKeys; + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + + Void _ = wait( checkMoveKeysLock(&tr, lock) ); + + vector< Future< Optional > > serverListEntries; + for(int s=0; s> serverListValues = wait( getAll(serverListEntries) ); + + for(int s=0; s old = wait( krmGetRanges( &tr, keyServersPrefix, currentKeys, SERVER_KNOBS->MOVE_KEYS_KRM_LIMIT, SERVER_KNOBS->MOVE_KEYS_KRM_LIMIT_BYTES) ); + + //Determine the last processed key (which will be the beginning for the next iteration) + state Key endKey = old.end()[-1].key; + currentKeys = KeyRangeRef(currentKeys.begin, endKey); + + /*TraceEvent("StartMoveKeysBatch", relocationIntervalId) + .detail("KeyBegin", printable(currentKeys.begin).c_str()) + .detail("KeyEnd", printable(currentKeys.end).c_str());*/ + + //printf("Moving '%s'-'%s' (%d) to %d servers\n", keys.begin.toString().c_str(), keys.end.toString().c_str(), old.size(), servers.size()); + //for(int i=0; i> enoughServersOk = wait(checkEnoughServersOk(old, durableStorageQuorum, relocationIntervalId, &tr)); + + // For each intersecting range, update keyServers[range] dest to be servers and clear existing dest servers from serverKeys + for(int i = 0; i < old.size() - 1; ++i) { + KeyRangeRef rangeIntersectKeys( old[i].key, old[i+1].key ); + vector src; + vector dest; + decodeKeyServersValue( old[i].value, src, dest ); + + /*TraceEvent("StartMoveKeysOldRange", relocationIntervalId) + .detail("KeyBegin", printable(rangeIntersectKeys.begin).c_str()) + .detail("KeyEnd", printable(rangeIntersectKeys.end).c_str()) + .detail("OldSrc", describe(src)) + .detail("OldDest", describe(dest)) + .detail("ReadVersion", tr.getReadVersion().get());*/ + + if ( !enoughServersOk[i] ) { + // The 'src' servers for this shard are seriously degraded. + // For safety, we combine the src and dest servers into a temporary + // team and make that the new src, to minimize the chance of losing + // data during this move. + TEST( true ); + src.insert( src.end(), dest.begin(), dest.end() ); + std::sort( src.begin(), src.end() ); + src.resize( std::unique( src.begin(), src.end() ) - src.begin() ); + dest.clear(); + TraceEvent(SevWarn,"StartMoveKeysShardDegraded", relocationIntervalId) + .detail("KeyBegin", printable(rangeIntersectKeys.begin)) + .detail("KeyEnd", printable(rangeIntersectKeys.end)) + .detail("CombinedTeamSize", src.size()); + } + + //Update dest servers for this range to be equal to servers + krmSetPreviouslyEmptyRange( &tr, keyServersPrefix, rangeIntersectKeys, keyServersValue(src, servers), old[i+1].value ); + + //Track old destination servers. They may be removed from serverKeys soon, since they are about to be overwritten in keyServers + for(auto s = dest.begin(); s != dest.end(); ++s) { + oldDests.insert(*s); + /*TraceEvent("StartMoveKeysOldDestAdd", relocationIntervalId) + .detail("Server", s->id());*/ + } + + //Keep track of src shards so that we can preserve their values when we overwrite serverKeys + std::set sources; + for(auto s = src.begin(); s != src.end(); ++s) + sources.insert(*s); + for(auto s = sources.begin(); s != sources.end(); ++s) { + shardMap[*s].push_back(old.arena(), rangeIntersectKeys); + /*TraceEvent("StartMoveKeysShardMapAdd", relocationIntervalId) + .detail("Server", *s);*/ + } + } + + state std::set::iterator oldDest; + + //Remove old dests from serverKeys. In order for krmSetRangeCoalescing to work correctly in the same prefix for a single transaction, we must + //do most of the coalescing ourselves. Only the shards on the boundary of currentRange are actually coalesced with the ranges outside of currentRange. + //For all shards internal to currentRange, we overwrite all consecutive keys whose value is or should be serverKeysFalse in a single write + vector> actors; + for(oldDest = oldDests.begin(); oldDest != oldDests.end(); ++oldDest) + if( std::find(servers.begin(), servers.end(), *oldDest) == servers.end() ) + actors.push_back( removeOldDestinations( &tr, *oldDest, shardMap[*oldDest], currentKeys ) ); + + //Update serverKeys to include keys (or the currently processed subset of keys) for each SS in servers + for(int i = 0; i < servers.size(); i++ ) { + // Since we are setting this for the entire range, serverKeys and keyServers aren't guaranteed to have the same shard boundaries + // If that invariant was important, we would have to move this inside the loop above and also set it for the src servers + actors.push_back( krmSetRangeCoalescing( &tr, serverKeysPrefixFor( servers[i] ), currentKeys, allKeys, serverKeysTrue) ); + } + + Void _ = wait( waitForAll( actors ) ); + + Void _ = wait( tr.commit() ); + + /*TraceEvent("StartMoveKeysCommitDone", relocationIntervalId) + .detail("CommitVersion", tr.getCommittedVersion()) + .detail("ShardsInBatch", old.size() - 1);*/ + begin = endKey; + shards += old.size() - 1; + break; + } catch (Error& e) { + state Error err = e; + if (err.code() == error_code_move_to_removed_server) + throw; + Void _ = wait( tr.onError(e) ); + TraceEvent(retries == 50 ? SevWarnAlways : SevWarn, "startMoveKeysRetrying", relocationIntervalId) + .detail("Keys", printable(keys)) + .detail("BeginKey", printable(begin)) + .detail("NumTries", retries) + .error(err); + } + } + + if(retries > maxRetries) { + maxRetries = retries; + } + } + + //printf("Committed moving '%s'-'%s' (version %lld)\n", keys.begin.toString().c_str(), keys.end.toString().c_str(), tr.getCommittedVersion()); + TraceEvent(SevDebug, interval.end(), relocationIntervalId) + .detail("Batches", batches) + .detail("Shards", shards) + .detail("MaxRetries", maxRetries); + } catch( Error& e ) { + TraceEvent(SevDebug, interval.end(), relocationIntervalId).error(e, true); + throw; + } + + return Void(); +} + +ACTOR Future waitForShardReady( StorageServerInterface server, KeyRange keys, Version minVersion, GetShardStateRequest::waitMode mode){ + loop { + try { + Version rep = wait( server.getShardState.getReply( GetShardStateRequest(keys, mode), TaskMoveKeys ) ); + if (rep >= minVersion) { + return Void(); + } + Void _ = wait( delayJittered( SERVER_KNOBS->SHARD_READY_DELAY, TaskMoveKeys ) ); + } + catch (Error& e) { + if( e.code() != error_code_timed_out ) { + if (e.code() != error_code_broken_promise) + throw e; + Void _ = wait(Never()); // Never return: A storage server which has failed will never be ready + throw internal_error(); // does not happen + } + } + } +} + +ACTOR Future checkFetchingState( Database cx, vector dest, KeyRange keys, + Promise dataMovementComplete, UID relocationIntervalId ) { + state Transaction tr(cx); + + loop { + try { + if (BUGGIFY) Void _ = wait(delay(5)); + + tr.info.taskID = TaskMoveKeys; + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + + vector< Future< Optional > > serverListEntries; + for(int s=0; s> serverListValues = wait( getAll(serverListEntries) ); + vector> requests; + for(int s=0; sSERVER_READY_QUORUM_TIMEOUT, TaskMoveKeys ) ); + + dataMovementComplete.send(Void()); + return Void(); + } catch( Error& e ) { + if( e.code() == error_code_timed_out ) + tr.reset(); + else + Void _ = wait( tr.onError(e) ); + } + } +} + +// Set keyServers[keys].src = keyServers[keys].dest and keyServers[keys].dest=[], return when successful +// keyServers[k].dest must be the same for all k in keys +// Set serverKeys[dest][keys] = true; serverKeys[src][keys] = false for all src not in dest +// Should be cancelled and restarted if keyServers[keys].dest changes (?so this is no longer true?) +ACTOR Future finishMoveKeys( Database occ, KeyRange keys, vector destinationTeam, + MoveKeysLock lock, int durableStorageQuorum, FlowLock *finishMoveKeysParallelismLock, UID relocationIntervalId ) +{ + state TraceInterval interval("RelocateShard_FinishMoveKeys"); + state TraceInterval waitInterval(""); + state Key begin = keys.begin; + state int retries = 0; + state FlowLock::Releaser releaser; + + ASSERT (!destinationTeam.empty()); + + try { + TraceEvent(SevDebug, interval.begin(), relocationIntervalId).detail("KeyBegin", printable(keys.begin)).detail("KeyEnd", printable(keys.end)); + + //This process can be split up into multiple transactions if there are too many existing overlapping shards + //In that case, each iteration of this loop will have begin set to the end of the last processed shard + while(begin < keys.end) { + TEST(begin > keys.begin); //Multi-transactional finishMoveKeys + + state Transaction tr( occ ); + + //printf("finishMoveKeys( '%s'-'%s' )\n", keys.begin.toString().c_str(), keys.end.toString().c_str()); + loop { + try { + tr.info.taskID = TaskMoveKeys; + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + + releaser.release(); + Void _ = wait( finishMoveKeysParallelismLock->take( TaskDataDistributionLaunch ) ); + releaser = FlowLock::Releaser( *finishMoveKeysParallelismLock ); + + Void _ = wait( checkMoveKeysLock(&tr, lock) ); + + state KeyRange currentKeys = KeyRangeRef(begin, keys.end); + state Standalone keyServers = wait( krmGetRanges( &tr, keyServersPrefix, currentKeys, SERVER_KNOBS->MOVE_KEYS_KRM_LIMIT, SERVER_KNOBS->MOVE_KEYS_KRM_LIMIT_BYTES ) ); + + //Determine the last processed key (which will be the beginning for the next iteration) + state Key endKey = keyServers.end()[-1].key; + currentKeys = KeyRangeRef(currentKeys.begin, endKey); + + //printf(" finishMoveKeys( '%s'-'%s' ): read keyServers at %lld\n", keys.begin.toString().c_str(), keys.end.toString().c_str(), tr.getReadVersion().get()); + + // Decode and sanity check the result (dest must be the same for all ranges) + bool alreadyMoved = true; + + state vector dest; + state std::set allServers; + state std::set intendedTeam(destinationTeam.begin(), destinationTeam.end()); + state vector src; + + //Iterate through the beginning of keyServers until we find one that hasn't already been processed + int currentIndex; + for(currentIndex = 0; currentIndex < keyServers.size() - 1 && alreadyMoved; currentIndex++) { + decodeKeyServersValue( keyServers[currentIndex].value, src, dest ); + + std::set srcSet; + for(int s = 0; s < src.size(); s++) + srcSet.insert(src[s]); + + std::set destSet; + for(int s = 0; s < dest.size(); s++) + destSet.insert(dest[s]); + + allServers.insert(srcSet.begin(), srcSet.end()); + allServers.insert(destSet.begin(), destSet.end()); + + alreadyMoved = destSet.empty() && srcSet == intendedTeam; + if(destSet != intendedTeam && !alreadyMoved) { + TraceEvent(SevWarn, "MoveKeysDestTeamNotIntended", relocationIntervalId) + .detail("KeyBegin", printable(keys.begin)) + .detail("KeyEnd", printable(keys.end)) + .detail("IterationBegin", printable(begin)) + .detail("IterationEnd", printable(endKey)) + .detail("DestSet", describe(destSet)) + .detail("IntendedTeam", describe(intendedTeam)) + .detail("KeyServers", printable(keyServers)); + //ASSERT( false ); + + ASSERT(!dest.empty()); //The range has already been moved, but to a different dest (or maybe dest was cleared) + + intendedTeam.clear(); + for(int i = 0; i < dest.size(); i++) + intendedTeam.insert(dest[i]); + } + else if(alreadyMoved) { + dest.clear(); + src.clear(); + TEST(true); //FinishMoveKeys first key in iteration sub-range has already been processed + } + } + + //Process the rest of the key servers + for(; currentIndex < keyServers.size() - 1; currentIndex++) { + vector src2, dest2; + decodeKeyServersValue( keyServers[currentIndex].value, src2, dest2 ); + + std::set srcSet; + for(int s = 0; s < src2.size(); s++) + srcSet.insert(src2[s]); + + allServers.insert(srcSet.begin(), srcSet.end()); + + alreadyMoved = dest2.empty() && srcSet == intendedTeam; + if (dest2 != dest && !alreadyMoved) { + TraceEvent(SevError,"FinishMoveKeysError", relocationIntervalId) + .detail("Reason", "dest mismatch") + .detail("Dest", describe(dest)) + .detail("Dest2", describe(dest2)); + ASSERT(false); + } + } + if (!dest.size()) { + TEST(true); // A previous finishMoveKeys for this range committed just as it was cancelled to start this one? + TraceEvent("FinishMoveKeysNothingToDo", relocationIntervalId) + .detail("KeyBegin", printable(keys.begin)) + .detail("KeyEnd", printable(keys.end)) + .detail("IterationBegin", printable(begin)) + .detail("IterationEnd", printable(endKey)); + begin = keyServers.end()[-1].key; + break; + } + + if (dest.size() < durableStorageQuorum) { + TraceEvent(SevError,"FinishMoveKeysError", relocationIntervalId) + .detailf("Reason", "dest size too small (%d)", dest.size()); + ASSERT(false); + } + + waitInterval = TraceInterval("RelocateShard_FinishMoveKeys_WaitDurable"); + TraceEvent(SevDebug, waitInterval.begin(), relocationIntervalId) + .detail("KeyBegin", printable(keys.begin)) + .detail("KeyEnd", printable(keys.end)); + + // Wait for a durable quorum of servers in destServers to have keys available (readWrite) + // They must also have at least the transaction read version so they can't "forget" the shard between + // now and when this transaction commits. + state vector< Future > serverReady; // only for count below + + // for smartQuorum + state vector storageServerInterfaces; + vector< Future< Optional > > serverListEntries; + for(int s=0; s> serverListValues = wait( getAll(serverListEntries) ); + + releaser.release(); + + for(int s=0; sSERVER_READY_QUORUM_INTERVAL, TaskMoveKeys ), + SERVER_KNOBS->SERVER_READY_QUORUM_TIMEOUT, Void(), TaskMoveKeys ) ); + int count = 0; + for(int s=0; s= durableStorageQuorum ) { + // update keyServers, serverKeys + // SOMEDAY: Doing these in parallel is safe because none of them overlap or touch (one per server) + Void _ = wait( krmSetRangeCoalescing( &tr, keyServersPrefix, currentKeys, keys, keyServersValue( dest ) ) ); + + std::set::iterator asi = allServers.begin(); + std::vector> actors; + while (asi != allServers.end()) { + bool destHasServer = std::find(dest.begin(), dest.end(), *asi) != dest.end(); + actors.push_back( krmSetRangeCoalescing( &tr, serverKeysPrefixFor(*asi), currentKeys, allKeys, destHasServer ? serverKeysTrue : serverKeysFalse ) ); + ++asi; + } + + Void _ = wait(waitForAll(actors)); + + //printf(" fMK: committing\n"); + + Void _ = wait( tr.commit() ); + + begin = endKey; + break; + } + tr.reset(); + } catch (Error& error) { + if (error.code() == error_code_actor_cancelled) throw; + state Error err = error; + Void _ = wait( tr.onError(error) ); + TraceEvent(retries++ == 15 ? SevWarnAlways : SevWarn, "RelocateShard_finishMoveKeysRetrying", relocationIntervalId) + .error(err) + .detail("KeyBegin", printable(keys.begin)) + .detail("KeyEnd", printable(keys.end)) + .detail("IterationBegin", printable(begin)) + .detail("IterationEnd", printable(endKey)); + } + } + } + + TraceEvent(SevDebug, interval.end(), relocationIntervalId); + } catch(Error &e) { + TraceEvent(SevDebug, interval.end(), relocationIntervalId).error(e, true); + throw; + } + //printf("Moved keys: ( '%s'-'%s' )\n", keys.begin.toString().c_str(), keys.end.toString().c_str()); + + return Void(); +} + +ACTOR Future> addStorageServer( Database cx, StorageServerInterface server ) +{ + state Transaction tr( cx ); + state int maxSkipTags = 1; + loop { + try { + state Future> fv = tr.get( serverListKeyFor(server.id()) ); + state Future> fExclProc = tr.get( + StringRef(encodeExcludedServersKey( AddressExclusion( server.address().ip, server.address().port ))) ); + state Future> fExclIP = tr.get( + StringRef(encodeExcludedServersKey( AddressExclusion( server.address().ip ))) ); + state Future> fTags( tr.getRange( serverTagKeys, CLIENT_KNOBS->TOO_MANY, true) ); + + Void _ = wait( success(fv) && success(fExclProc) && success(fExclIP) && success(fTags) ); + + // If we have been added to the excluded state servers list, we have to fail + if (fExclProc.get().present() || fExclIP.get().present()) + throw recruitment_failed(); + + if(fTags.get().more) + ASSERT(false); + + int skipTags = g_random->randomInt(0, maxSkipTags); + + state Tag tag = 0; + std::vector usedTags; + for(auto it : fTags.get()) + usedTags.push_back(decodeServerTagValue( it.value )); + std::sort(usedTags.begin(), usedTags.end()); + + int usedIdx = 0; + for(; tag <= usedTags.end()[-1]; tag++) { + if(tag < usedTags[usedIdx]) { + if(skipTags == 0) + break; + skipTags--; + } else { + usedIdx++; + } + } + tag += skipTags; + + tr.set( serverTagKeyFor(server.id()), serverTagValue(tag) ); + tr.set( serverListKeyFor(server.id()), serverListValue(server) ); + KeyRange conflictRange = singleKeyRange(serverTagConflictKeyFor(tag)); + tr.addReadConflictRange( conflictRange ); + tr.addWriteConflictRange( conflictRange ); + tr.atomicOp( serverTagMaxKey, serverTagMaxValue(tag), MutationRef::Max ); + + Void _ = wait( tr.commit() ); + return std::make_pair(tr.getCommittedVersion(), tag); + } catch (Error& e) { + if(e.code() == error_code_commit_unknown_result) + throw recruitment_failed(); // There is a remote possibility that we successfully added ourselves and then someone removed us, so we have to fail + + Void _ = wait( tr.onError(e) ); + + maxSkipTags = std::min(maxSkipTags * SERVER_KNOBS->SKIP_TAGS_GROWTH_RATE, SERVER_KNOBS->MAX_SKIP_TAGS); + } + } +} + +ACTOR Future canRemoveStorageServer( Transaction* tr, UID serverID ) { + Standalone keys = wait( krmGetRanges( tr, serverKeysPrefixFor(serverID), allKeys, 2 ) ); + + ASSERT(keys.size() >= 2); + + if(keys[0].value == keys[1].value && keys[1].key != allKeys.end) { + TraceEvent("ServerKeysCoalescingError", serverID).detail("Key1", printable(keys[0].key)).detail("Key2", printable(keys[1].key)).detail("Value", printable(keys[0].value)); + ASSERT(false); + } + + //Return true if the entire range is false. Since these values are coalesced, we can return false if there is more than one result + return keys[0].value == serverKeysFalse && keys[1].key == allKeys.end; +} + +ACTOR Future removeStorageServer( Database cx, UID serverID, MoveKeysLock lock ) +{ + state Transaction tr( cx ); + state bool retry = false; + state int noCanRemoveCount = 0; + loop { + try { + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + Void _ = wait( checkMoveKeysLock(&tr, lock) ); + TraceEvent("RemoveStorageServerLocked").detail("ServerID", serverID).detail("Version", tr.getReadVersion().get()); + + state bool canRemove = wait( canRemoveStorageServer( &tr, serverID ) ); + if (!canRemove) { + TEST(true); // The caller had a transaction in flight that assigned keys to the server. Wait for it to reverse its mistake. + TraceEvent(SevWarn,"NoCanRemove").detail("Count", noCanRemoveCount++).detail("ServerID", serverID); + Void _ = wait( delayJittered(SERVER_KNOBS->REMOVE_RETRY_DELAY, TaskDataDistributionLaunch) ); + tr.reset(); + TraceEvent("RemoveStorageServerRetrying").detail("canRemove", canRemove); + } else { + Optional v = wait( tr.get( serverListKeyFor(serverID) ) ); + if (!v.present()) { + if (retry) { + TEST(true); // Storage server already removed after retrying transaction + return Void(); + } + ASSERT(false); // Removing an already-removed server? A never added server? + } + + tr.clear( serverListKeyFor(serverID) ); + tr.clear( serverTagKeyFor(serverID) ); + retry = true; + Void _ = wait( tr.commit() ); + return Void(); + } + } catch (Error& e) { + state Error err = e; + Void _ = wait( tr.onError(e) ); + TraceEvent("RemoveStorageServerRetrying").error(err); + } + } +} + +ACTOR Future moveKeys( + Database cx, + KeyRange keys, + vector destinationTeam, + MoveKeysLock lock, + int durableStorageQuorum, + Promise dataMovementComplete, + FlowLock *startMoveKeysParallelismLock, + FlowLock *finishMoveKeysParallelismLock, + UID relocationIntervalId) +{ + ASSERT( destinationTeam.size() ); + std::sort( destinationTeam.begin(), destinationTeam.end() ); + Void _ = wait( startMoveKeys( cx, keys, destinationTeam, lock, durableStorageQuorum, startMoveKeysParallelismLock, relocationIntervalId ) ); + + state Future completionSignaller = checkFetchingState( cx, destinationTeam, keys, dataMovementComplete, relocationIntervalId ); + + Void _ = wait( finishMoveKeys( cx, keys, destinationTeam, lock, durableStorageQuorum, finishMoveKeysParallelismLock, relocationIntervalId ) ); + + //This is defensive, but make sure that we always say that the movement is complete before moveKeys completes + completionSignaller.cancel(); + if(!dataMovementComplete.isSet()) + dataMovementComplete.send(Void()); + + return Void(); +} + +void seedShardServers( + Arena& arena, + CommitTransactionRef &tr, + vector servers ) +{ + std::map server_tag; + for(Tag s=0; s serverIds; + for(int i=0;i + void serialize(Ar& ar) { ar & prevOwner & myOwner & prevWrite; } +}; + +Future takeMoveKeysLock( Database const& cx, UID const& masterId ); +// Calling moveKeys, etc with the return value of this actor ensures that no movekeys, etc +// has been executed by a different locker since takeMoveKeysLock(). +// takeMoveKeysLock itself is a read-only operation - it does not conflict with other +// attempts to take the lock. + +Future checkMoveKeysLockReadOnly( Transaction* tr, MoveKeysLock lock ); +// Checks that the a moveKeysLock has not changed since having taken it +// This does not modify the moveKeysLock + +void seedShardServers( + Arena& trArena, + CommitTransactionRef &tr, + vector servers ); +// Called by the master server to write the very first transaction to the database +// establishing a set of shard servers and all invariants of the systemKeys. + +Future moveKeys( + Database const& occ, + KeyRange const& keys, + vector const& destinationTeam, + MoveKeysLock const& lock, + int const& durableStorageQuorum, + Promise const& dataMovementComplete, + FlowLock* const& startMoveKeysParallelismLock, + FlowLock* const& finishMoveKeysParallelismLock, + UID const& relocationIntervalId); // for logging only +// Eventually moves the given keys to the given destination team +// Caller is responsible for cancelling it before issuing an overlapping move, +// for restarting the remainder, and for not otherwise cancelling it before +// it returns (since it needs to execute the finishMoveKeys transaction). + +Future> addStorageServer( + Database const& cx, + StorageServerInterface const& server ); +// Adds a newly recruited storage server to a database (e.g. adding it to FF/serverList) +// Returns a Version in which the storage server is in the database +// This doesn't need to be called for the "seed" storage servers (see seedShardServers above) + +Future removeStorageServer( + Database const& cx, + UID const& serverID, + MoveKeysLock const& lock ); +// Removes the given storage server permanently from the database. It must already +// have no shards assigned to it. The storage server MUST NOT be added again after this +// (though a new storage server with a new unique ID may be recruited from the same fdbserver). + +Future canRemoveStorageServer( Transaction* const& tr, UID const& serverID ); +// Returns true if the given storage server has no keys assigned to it and may be safely removed +// Obviously that could change later! + +#endif \ No newline at end of file diff --git a/fdbserver/NetworkTest.h b/fdbserver/NetworkTest.h new file mode 100644 index 0000000000..8af9c63d12 --- /dev/null +++ b/fdbserver/NetworkTest.h @@ -0,0 +1,61 @@ +/* + * NetworkTest.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_NETWORKTEST_H +#define FDBSERVER_NETWORKTEST_H +#pragma once + +#include "fdbclient/FDBTypes.h" +#include "fdbrpc/fdbrpc.h" + +struct NetworkTestInterface { + RequestStream< struct NetworkTestRequest > test; + NetworkTestInterface() {} + NetworkTestInterface( NetworkAddress remote ); + NetworkTestInterface( INetwork* local ); +}; + +struct NetworkTestRequest { + Key key; + uint32_t replySize; + ReplyPromise reply; + NetworkTestRequest(){} + NetworkTestRequest( Key key, uint32_t replySize ) : key(key), replySize(replySize) {} + template + void serialize(Ar& ar) { + ar & key & replySize & reply; + } +}; + +struct NetworkTestReply { + Value value; + NetworkTestReply() {} + NetworkTestReply( Value value ) : value(value) {} + template + void serialize(Ar& ar) { + ar & value; + } +}; + +Future networkTestServer(); + +Future networkTestClient( std:: string const& testServers ); + +#endif \ No newline at end of file diff --git a/fdbserver/OldTLogServer.actor.cpp b/fdbserver/OldTLogServer.actor.cpp new file mode 100644 index 0000000000..c8903a4b36 --- /dev/null +++ b/fdbserver/OldTLogServer.actor.cpp @@ -0,0 +1,1451 @@ +/* + * OldTLogServer.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/Hash3.h" +#include "flow/Stats.h" +#include "flow/UnitTest.h" +#include "fdbclient/NativeAPI.h" +#include "fdbclient/KeyRangeMap.h" +#include "fdbclient/SystemData.h" +#include "WorkerInterface.h" +#include "TLogInterface.h" +#include "flow/Notified.h" +#include "Knobs.h" +#include "IKeyValueStore.h" +#include "flow/ActorCollection.h" +#include "fdbrpc/FailureMonitor.h" +#include "IDiskQueue.h" +#include "fdbrpc/sim_validation.h" +#include "ServerDBInfo.h" +#include "LogSystem.h" +#include "WaitFailure.h" + +using std::pair; +using std::make_pair; +using std::min; +using std::max; + +namespace oldTLog { + struct TLogQueueEntryRef { + Version version; + Version knownCommittedVersion; + StringRef messages; + VectorRef< TagMessagesRef > tags; + + TLogQueueEntryRef() : version(0), knownCommittedVersion(0) {} + TLogQueueEntryRef(Arena &a, TLogQueueEntryRef const &from) + : version(from.version), knownCommittedVersion(from.knownCommittedVersion), messages(a, from.messages), tags(a, from.tags) { + } + + template + void serialize(Ar& ar) { + if( ar.protocolVersion() >= 0x0FDB00A460010001) { + ar & version & messages & tags & knownCommittedVersion; + } else if(ar.isDeserializing) { + ar & version & messages & tags; + knownCommittedVersion = 0; + } + } + size_t expectedSize() const { + return messages.expectedSize() + tags.expectedSize(); + } + }; + + typedef Standalone TLogQueueEntry; + + struct TLogQueue : public IClosable { + public: + TLogQueue( IDiskQueue* queue, UID dbgid ) : queue(queue), debugNextReadVersion(1), dbgid(dbgid) {} + + // Each packet in the queue is + // uint32_t payloadSize + // uint8_t payload[payloadSize] (begins with uint64_t protocolVersion via IncludeVersion) + // uint8_t validFlag + + // TLogQueue is a durable queue of TLogQueueEntry objects with an interface similar to IDiskQueue + + // TLogQueue pushes (but not commits) are atomic - after commit fails to return, a prefix of entire calls to push are durable. This is + // implemented on top of the weaker guarantee of IDiskQueue::commit (that a prefix of bytes is durable) using validFlag and by + // padding any incomplete packet with zeros after recovery. + + // Before calling push, pop, or commit, the user must call readNext() until it throws + // end_of_stream(). It may not be called again thereafter. + Future readNext() { + return readNext( this ); + } + + void push( TLogQueueEntryRef const& qe ) { + ASSERT( version_location.empty() || version_location.lastItem()->key < qe.version ); + BinaryWriter wr( Unversioned() ); // outer framing is not versioned + wr << uint32_t(0); + IncludeVersion().write(wr); // payload is versioned + wr << qe; + wr << uint8_t(1); + *(uint32_t*)wr.getData() = wr.getLength() - sizeof(uint32_t) - sizeof(uint8_t); + auto loc = queue->push( wr.toStringRef() ); + //TraceEvent("TLogQueueVersionWritten", dbgid).detail("Size", wr.getLength() - sizeof(uint32_t) - sizeof(uint8_t)).detail("Loc", loc); + version_location[qe.version] = loc; + } + void pop( Version upTo ) { + // Keep only the given and all subsequent version numbers + // Find the first version >= upTo + auto v = version_location.lower_bound(upTo); + if (v == version_location.begin()) return; + + if(v == version_location.end()) { + v = version_location.lastItem(); + } + else { + v.decrementNonEnd(); + } + + queue->pop( v->value ); + version_location.erase( version_location.begin(), v ); // ... and then we erase that previous version and all prior versions + } + Future commit() { return queue->commit(); } + + // Implements IClosable + virtual Future getError() { return queue->getError(); } + virtual Future onClosed() { return queue->onClosed(); } + virtual void dispose() { queue->dispose(); delete this; } + virtual void close() { queue->close(); delete this; } + + private: + IDiskQueue* queue; + Map version_location; // For the version of each entry that was push()ed, the end location of the serialized bytes + Version debugNextReadVersion; + UID dbgid; + + ACTOR static Future readNext( TLogQueue* self ) { + state TLogQueueEntry result; + state int zeroFillSize = 0; + + loop { + Standalone h = wait( self->queue->readNext( sizeof(uint32_t) ) ); + if (h.size() != sizeof(uint32_t)) { + if (h.size()) { + TEST( true ); // Zero fill within size field + int payloadSize = 0; + memcpy(&payloadSize, h.begin(), h.size()); + zeroFillSize = sizeof(uint32_t)-h.size(); // zero fill the size itself + zeroFillSize += payloadSize+1; // and then the contents and valid flag + } + break; + } + + state uint32_t payloadSize = *(uint32_t*)h.begin(); + ASSERT( payloadSize < (100<<20) ); + + Standalone e = wait( self->queue->readNext( payloadSize+1 ) ); + if (e.size() != payloadSize+1) { + TEST( true ); // Zero fill within payload + zeroFillSize = payloadSize+1 - e.size(); + break; + } + + if (e[payloadSize]) { + Arena a = e.arena(); + ArenaReader ar( a, e.substr(0, payloadSize), IncludeVersion() ); + ar >> result; + ASSERT( result.version >= self->debugNextReadVersion ); + self->debugNextReadVersion = result.version + 1; + self->version_location[result.version] = self->queue->getNextReadLocation(); + return result; + } + } + if (zeroFillSize) { + TEST( true ); // Fixing a partial commit at the end of the tlog queue + for(int i=0; iqueue->push( StringRef((const uint8_t*)"",1) ); + } + throw end_of_stream(); + } + }; + + struct LengthPrefixedStringRef { + // Represents a pointer to a string which is prefixed by a 4-byte length + // A LengthPrefixedStringRef is only pointer-sized (8 bytes vs 12 bytes for StringRef), but the corresponding string is 4 bytes bigger, and + // substring operations aren't efficient as they are with StringRef. It's a good choice when there might be lots of references to the same + // exact string. + + uint32_t* length; + + StringRef toStringRef() const { ASSERT(length); return StringRef( (uint8_t*)(length+1), *length ); } + int expectedSize() const { ASSERT(length); return *length; } + uint32_t* getLengthPtr() const { return length; } + + LengthPrefixedStringRef() : length(NULL) {} + LengthPrefixedStringRef(uint32_t* length) : length(length) {} + }; + + template + struct CompareFirst { + bool operator() (T const& lhs, T const& rhs) const { + return lhs.first < rhs.first; + } + }; + + struct TLogData : NonCopyable { + struct TagData { + std::deque> version_messages; + bool nothing_persistent; // true means tag is *known* to have no messages in persistentData. false means nothing. + bool popped_recently; // `popped` has changed since last updatePersistentData + Version popped; // see popped version tracking contract below + bool update_version_sizes; + + TagData( Version popped, bool nothing_persistent, bool popped_recently, Tag tag ) : nothing_persistent(nothing_persistent), popped(popped), popped_recently(popped_recently), update_version_sizes(tag != txsTag) {} + + TagData(TagData&& r) noexcept(true) : version_messages(std::move(r.version_messages)), nothing_persistent(r.nothing_persistent), popped_recently(r.popped_recently), popped(r.popped), update_version_sizes(r.update_version_sizes) {} + void operator= (TagData&& r) noexcept(true) { + version_messages = std::move(r.version_messages); + nothing_persistent = r.nothing_persistent; + popped_recently = r.popped_recently; + popped = r.popped; + update_version_sizes = r.update_version_sizes; + } + + // Erase messages not needed to update *from* versions >= before (thus, messages with toversion <= before) + ACTOR Future eraseMessagesBefore( TagData *self, Version before, Counter* bytesErased, TLogData *tlogData, int taskID ) { + while(!self->version_messages.empty() && self->version_messages.front().first < before) { + Version version = self->version_messages.front().first; + std::pair &sizes = tlogData->version_sizes[version]; + int64_t messagesErased = 0; + + while(!self->version_messages.empty() && self->version_messages.front().first == version) { + auto const& m = self->version_messages.front(); + ++messagesErased; + + if(self->update_version_sizes) { + sizes.first -= m.second.expectedSize(); + } + + self->version_messages.pop_front(); + } + + *bytesErased += (messagesErased * sizeof(std::pair) * SERVER_KNOBS->VERSION_MESSAGES_OVERHEAD_FACTOR_1024THS) >> 10; + Void _ = wait(yield(taskID)); + } + + return Void(); + } + + Future eraseMessagesBefore(Version before, Counter* bytesErased, TLogData *tlogData, int taskID) { + return eraseMessagesBefore(this, before, bytesErased, tlogData, taskID); + } + }; + + /* + Popped version tracking contract needed by log system to implement ILogCursor::popped(): + + - Log server tracks for each (possible) tag a popped_version + Impl: TagData::popped (in memory) and persistTagPoppedKeys (in persistentData) + - popped_version(tag) is <= the maximum version for which log server (or a predecessor) is ever asked to pop the tag + Impl: Only increased by tLogPop() in response to either a pop request or recovery from a predecessor + - popped_version(tag) is > the maximum version for which log server is unable to peek messages due to previous pops (on this server or a predecessor) + Impl: Increased by tLogPop() atomically with erasing messages from memory; persisted by updatePersistentData() atomically with erasing messages from store; messages are not erased from queue where popped_version is not persisted + - LockTLogReply returns all tags which either have messages, or which have nonzero popped_versions + Impl: tag_data is present for all such tags + - peek(tag, v) returns the popped_version for tag if that is greater than v + Impl: Check tag_data->popped (after all waits) + */ + + struct peekTrackerData { + std::map> sequence_version; + double lastUpdate; + }; + + std::map peekTracker; + + UID dbgid; + bool coreStarted; + bool stopped; + DBRecoveryCount recoveryCount; + + IKeyValueStore* persistentData; + IDiskQueue* rawPersistentQueue; + TLogQueue *persistentQueue; + VersionMetricHandle persistentDataVersion, persistentDataDurableVersion; // The last version number in the portion of the log (written|durable) to persistentData + NotifiedVersion version, queueCommittedVersion, queueCommitEnd; + Version queueCommitBegin, queueCommittingVersion; + int64_t diskQueueCommitBytes; + AsyncVar largeDiskQueueCommitBytes; //becomes true when diskQueueCommitBytes is greater than MAX_QUEUE_COMMIT_BYTES + Version prevVersion, knownCommittedVersion; + + Deque>>> messageBlocks; + Map< Tag, TagData > tag_data; + + Map> version_sizes; + + int64_t instanceID; + CounterCollection cc; + Counter bytesInput; + Counter bytesDurable; + + Reference> dbInfo; + Future updatePersist; //SOMEDAY: integrate the recovery and update storage so that only one of them is committing to persistant data. + PromiseStream> addActor; + + TLogData(UID dbgid, IKeyValueStore* persistentData, IDiskQueue * persistentQueue, Reference> const& dbInfo) + : dbgid(dbgid), + persistentData(persistentData), rawPersistentQueue(persistentQueue), persistentQueue(new TLogQueue(persistentQueue, dbgid)), + prevVersion(0), knownCommittedVersion(0), + dbInfo(dbInfo), + updatePersist(Void()), + instanceID(g_random->randomUniqueID().first()), + cc("TLog", dbgid.toString()), + bytesInput("bytesInput", cc), + bytesDurable("bytesDurable", cc), + // These are initialized differently on init() or recovery + recoveryCount(), coreStarted(false), stopped(false), queueCommitBegin(0), queueCommitEnd(0), diskQueueCommitBytes(0), largeDiskQueueCommitBytes(false), queueCommittingVersion(0) + { + persistentDataVersion.init(LiteralStringRef("TLog.PersistentDataVersion"), cc.id); + persistentDataDurableVersion.init(LiteralStringRef("TLog.PersistentDataDurableVersion"), cc.id); + version.initMetric(LiteralStringRef("TLog.Version"), cc.id); + queueCommittedVersion.initMetric(LiteralStringRef("TLog.QueueCommittedVersion"), cc.id); + + specialCounter(cc, "version", [this](){ return this->version.get(); }); + specialCounter(cc, "kvstoreBytesUsed", [this](){ return this->persistentData->getStorageBytes().used; }); + specialCounter(cc, "kvstoreBytesFree", [this](){ return this->persistentData->getStorageBytes().free; }); + specialCounter(cc, "kvstoreBytesAvailable", [this](){ return this->persistentData->getStorageBytes().available; }); + specialCounter(cc, "kvstoreBytesTotal", [this](){ return this->persistentData->getStorageBytes().total; }); + specialCounter(cc, "queueDiskBytesUsed", [this](){ return this->rawPersistentQueue->getStorageBytes().used; }); + specialCounter(cc, "queueDiskBytesFree", [this](){ return this->rawPersistentQueue->getStorageBytes().free; }); + specialCounter(cc, "queueDiskBytesAvailable", [this](){ return this->rawPersistentQueue->getStorageBytes().available; }); + specialCounter(cc, "queueDiskBytesTotal", [this](){ return this->rawPersistentQueue->getStorageBytes().total; }); + } + + LogEpoch epoch() const { return recoveryCount; } + }; + + ACTOR Future tLogLock( TLogData* self, ReplyPromise< TLogLockResult > reply ) { + state Version stopVersion = self->version.get(); + + TEST(true); // TLog stopped by recovering master + TEST( self->stopped ); + TEST( !self->stopped ); + + TraceEvent("TLogStop", self->dbgid).detail("Ver", stopVersion).detail("isStopped", self->stopped); + + self->stopped = true; + + // Lock once the current version has been committed + Void _ = wait( self->queueCommittedVersion.whenAtLeast( stopVersion ) ); + + ASSERT(stopVersion == self->version.get()); + + TLogLockResult result; + result.end = stopVersion; + result.knownCommittedVersion = self->knownCommittedVersion; + for( auto & tag : self->tag_data ) + result.tags.push_back( tag.key ); + + reply.send( result ); + return Void(); + } + + KeyRange prefixRange( KeyRef prefix ) { + Key end = prefix; + UNSTOPPABLE_ASSERT( end.size() && end.end()[-1] != 0xFF ); + ++const_cast( end.end()[-1] ); + return KeyRangeRef( prefix, end ); + } + + ////// Persistence format (for self->persistentData) + + // Immutable keys + static const KeyValueRef persistFormat( LiteralStringRef( "Format" ), LiteralStringRef("FoundationDB/LogServer/2/2") ); + static const KeyRangeRef persistFormatReadableRange( LiteralStringRef("FoundationDB/LogServer/2/2"), LiteralStringRef("FoundationDB/LogServer/2/3") ); + static const KeyRef persistID = LiteralStringRef( "ID" ); + static const KeyRef persistRecoveryCountKey = LiteralStringRef("DbRecoveryCount"); + + // Updated on updatePersistentData() + static const KeyRef persistCurrentVersionKey = LiteralStringRef("version"); + static const KeyRange persistTagMessagesKeys = prefixRange(LiteralStringRef("TagMsg/")); + static const KeyRange persistTagPoppedKeys = prefixRange(LiteralStringRef("TagPop/")); + + // Only present during network recovery process + static const KeyValueRef persistRecoveryInProgress( LiteralStringRef("RecoveryInProgress"), LiteralStringRef("1") ); + + static Key persistTagMessagesKey( Tag tag, Version version ) { + BinaryWriter wr( Unversioned() ); + wr.serializeBytes(persistTagMessagesKeys.begin); + wr << tag; + wr << bigEndian64( version ); + return wr.toStringRef(); + } + + static Key persistTagPoppedKey( Tag tag ) { + BinaryWriter wr(Unversioned()); + wr.serializeBytes( persistTagPoppedKeys.begin ); + wr << tag; + return wr.toStringRef(); + } + + static Value persistTagPoppedValue( Version popped ) { + return BinaryWriter::toValue( popped, Unversioned() ); + } + + static Tag decodeTagPoppedKey( KeyRef key ) { + Tag s; + BinaryReader rd( key.removePrefix(persistTagPoppedKeys.begin), Unversioned() ); + rd >> s; + return s; + } + + static Version decodeTagPoppedValue( ValueRef value ) { + return BinaryReader::fromStringRef( value, Unversioned() ); + } + + static StringRef stripTagMessagesKey( StringRef key ) { + return key.substr( sizeof(Tag) + persistTagMessagesKeys.begin.size() ); + } + + static Version decodeTagMessagesKey( StringRef key ) { + return bigEndian64( BinaryReader::fromStringRef( stripTagMessagesKey(key), Unversioned() ) ); + } + + static Standalone decodeTagMessagesKeyTag( StringRef key ) { + key = key.removePrefix( persistTagMessagesKeys.begin ); // \x00\xff + BinaryWriter wr( Unversioned() ); + for(auto c = key.begin(); c != key.end(); ++c) { + if (*c) + wr << *c; + else { + ASSERT( c+1 != key.end() ); + if (c[1] == 0xff) { + wr << uint8_t(0); + c++; + } else if (c[1] == 0) + break; + else + throw internal_error(); + } + } + return wr.toStringRef(); + } + + void validate( TLogData* self, bool force = false ) { + } + + void updatePersistentPopped( TLogData* self, Tag tag, TLogData::TagData& data ) { + if (!data.popped_recently) return; + self->persistentData->set(KeyValueRef( persistTagPoppedKey(tag), persistTagPoppedValue(data.popped) )); + data.popped_recently = false; + + if (data.nothing_persistent) return; + + self->persistentData->clear( KeyRangeRef( + persistTagMessagesKey( tag, Version(0) ), + persistTagMessagesKey( tag, data.popped ) ) ); + if (data.popped > self->persistentDataVersion) + data.nothing_persistent = true; + //TraceEvent("TLogPopWrite", self->dbgid).detail("Tag", tag).detail("To", data.popped); + } + + ACTOR Future updatePersistentData( TLogData* self, Version newPersistentDataVersion ) { + // PERSIST: Changes self->persistentDataVersion and writes and commits the relevant changes + ASSERT( newPersistentDataVersion <= self->version.get() ); + ASSERT( newPersistentDataVersion <= self->queueCommittedVersion.get() ); + ASSERT( newPersistentDataVersion > self->persistentDataVersion ); + ASSERT( self->persistentDataVersion == self->persistentDataDurableVersion ); + + //TraceEvent("updatePersistentData", self->dbgid).detail("seq", newPersistentDataSeq); + + state bool anyData = false; + state Map::iterator tag; + // For all existing tags + for(tag = self->tag_data.begin(); tag != self->tag_data.end(); ++tag) { + state Version currentVersion = 0; + // Clear recently popped versions from persistentData if necessary + updatePersistentPopped( self, tag->key, tag->value ); + // Transfer unpopped messages with version numbers less than newPersistentDataVersion to persistentData + state std::deque>::iterator msg = tag->value.version_messages.begin(); + while(msg != tag->value.version_messages.end() && msg->first <= newPersistentDataVersion) { + currentVersion = msg->first; + anyData = true; + tag->value.nothing_persistent = false; + BinaryWriter wr( Unversioned() ); + + for(; msg != tag->value.version_messages.end() && msg->first == currentVersion; ++msg) + wr << msg->second.toStringRef(); + + self->persistentData->set( KeyValueRef( persistTagMessagesKey( tag->key, currentVersion ), wr.toStringRef() ) ); + + Future f = yield(TaskUpdateStorage); + if(!f.isReady()) { + Void _ = wait(f); + msg = std::upper_bound(tag->value.version_messages.begin(), tag->value.version_messages.end(), std::make_pair(currentVersion, LengthPrefixedStringRef()), CompareFirst>()); + } + } + + Void _ = wait(yield(TaskUpdateStorage)); + } + + self->persistentData->set( KeyValueRef( persistCurrentVersionKey, BinaryWriter::toValue(newPersistentDataVersion, Unversioned()) ) ); + self->persistentDataVersion = newPersistentDataVersion; + + Void _ = wait( self->persistentData->commit() ); // SOMEDAY: This seems to be running pretty often, should we slow it down??? + Void _ = wait( delay(0, TaskUpdateStorage) ); + + // Now that the changes we made to persistentData are durable, erase the data we moved from memory and the queue, increase bytesDurable accordingly, and update persistentDataDurableVersion. + + TEST(anyData); // TLog moved data to persistentData + self->persistentDataDurableVersion = newPersistentDataVersion; + + for(tag = self->tag_data.begin(); tag != self->tag_data.end(); ++tag) { + Void _ = wait(tag->value.eraseMessagesBefore( newPersistentDataVersion+1, &self->bytesDurable, self, TaskUpdateStorage )); + Void _ = wait(yield(TaskUpdateStorage)); + } + + self->version_sizes.erase(self->version_sizes.begin(), self->version_sizes.lower_bound(self->persistentDataDurableVersion)); + + Void _ = wait(yield(TaskUpdateStorage)); + + while(!self->messageBlocks.empty() && self->messageBlocks.front().first <= newPersistentDataVersion) { + self->bytesDurable += self->messageBlocks.front().second.size() * SERVER_KNOBS->TLOG_MESSAGE_BLOCK_OVERHEAD_FACTOR; + self->messageBlocks.pop_front(); + Void _ = wait(yield(TaskUpdateStorage)); + } + + ASSERT(self->bytesDurable.getValue() <= self->bytesInput.getValue()); + + if( self->queueCommitEnd.get() > 0 ) + self->persistentQueue->pop( newPersistentDataVersion+1 ); // SOMEDAY: this can cause a slow task (~0.5ms), presumably from erasing too many versions. Should we limit the number of versions cleared at a time? + + return Void(); + } + + // This function (and updatePersistentData, which is called by this function) run at a low priority and can soak up all CPU resources. + // For this reason, they employ aggressive use of yields to avoid causing slow tasks that could introduce latencies for more important + // work (e.g. commits). + ACTOR Future updateStorage( TLogData* self ) { + Void _ = wait(delay(0, TaskUpdateStorage)); + loop { + state Version prevVersion = 0; + state Version nextVersion = 0; + state int totalSize = 0; + + state Map>::iterator sizeItr = self->version_sizes.begin(); + while( totalSize < SERVER_KNOBS->UPDATE_STORAGE_BYTE_LIMIT && sizeItr != self->version_sizes.end() + && (self->bytesInput.getValue() - self->bytesDurable.getValue() - totalSize >= SERVER_KNOBS->TLOG_SPILL_THRESHOLD || sizeItr->value.first == 0) ) + { + Void _ = wait( yield(TaskUpdateStorage) ); + + ++sizeItr; + nextVersion = sizeItr == self->version_sizes.end() ? self->version.get() : sizeItr->key; + + state Map::iterator tag; + for(tag = self->tag_data.begin(); tag != self->tag_data.end(); ++tag) { + auto it = std::lower_bound(tag->value.version_messages.begin(), tag->value.version_messages.end(), std::make_pair(prevVersion, LengthPrefixedStringRef()), CompareFirst>()); + for(; it != tag->value.version_messages.end() && it->first < nextVersion; ++it) { + totalSize += it->second.expectedSize(); + } + + Void _ = wait(yield(TaskUpdateStorage)); + } + + prevVersion = nextVersion; + } + + nextVersion = std::max(nextVersion, self->persistentDataVersion); + + TraceEvent("UpdateStorageVer", self->dbgid).detail("nextVersion", nextVersion).detail("persistentDataVersion", self->persistentDataVersion).detail("totalSize", totalSize); + + Void _ = wait( self->queueCommittedVersion.whenAtLeast( nextVersion ) ); + Void _ = wait( delay(0, TaskUpdateStorage) ); + + if (nextVersion > self->persistentDataVersion) { + self->updatePersist = updatePersistentData(self, nextVersion); + Void _ = wait( self->updatePersist ); + } + + if( totalSize < SERVER_KNOBS->UPDATE_STORAGE_BYTE_LIMIT ) { + Void _ = wait( delay(BUGGIFY ? SERVER_KNOBS->BUGGIFY_TLOG_STORAGE_MIN_UPDATE_INTERVAL : SERVER_KNOBS->TLOG_STORAGE_MIN_UPDATE_INTERVAL, TaskUpdateStorage) ); + } + else { + //recovery wants to commit to persistant data when updatePersistentData is not active, this delay ensures that immediately after + //updatePersist returns another one has not been started yet. + Void _ = wait( delay(0.0, TaskUpdateStorage) ); + } + } + } + + void commitMessages( TLogData* self, Version version, Arena arena, StringRef messages, VectorRef< TagMessagesRef > tags) { + // SOMEDAY: This method of copying messages is reasonably memory efficient, but it's still a lot of bytes copied. Find a + // way to do the memory allocation right as we receive the messages in the network layer. + + int64_t addedBytes = 0; + int64_t expectedBytes = 0; + + if(!messages.size()) { + return; + } + + StringRef messages1; // the first block of messages, if they aren't all stored contiguously. otherwise empty + + // Grab the last block in the blocks list so we can share its arena + // We pop all of the elements of it to create a "fresh" vector that starts at the end of the previous vector + Standalone> block; + if(self->messageBlocks.empty()) { + block = Standalone>(); + block.reserve(block.arena(), std::max(SERVER_KNOBS->TLOG_MESSAGE_BLOCK_BYTES, messages.size())); + } + else { + block = self->messageBlocks.back().second; + } + + block.pop_front(block.size()); + + // If the current batch of messages doesn't fit entirely in the remainder of the last block in the list + if(messages.size() + block.size() > block.capacity()) { + // Find how many messages will fit + LengthPrefixedStringRef r((uint32_t*)messages.begin()); + uint8_t const* end = messages.begin() + block.capacity() - block.size(); + while(r.toStringRef().end() <= end) { + r = LengthPrefixedStringRef( (uint32_t*)r.toStringRef().end() ); + } + + // Fill up the rest of this block + int bytes = (uint8_t*)r.getLengthPtr()-messages.begin(); + if (bytes) { + TEST(true); // Splitting commit messages across multiple blocks + messages1 = StringRef(block.end(), bytes); + block.append(block.arena(), messages.begin(), bytes); + self->messageBlocks.push_back( std::make_pair(version, block) ); + addedBytes += int64_t(block.size()) * SERVER_KNOBS->TLOG_MESSAGE_BLOCK_OVERHEAD_FACTOR; + messages = messages.substr(bytes); + } + + // Make a new block + block = Standalone>(); + block.reserve(block.arena(), std::max(SERVER_KNOBS->TLOG_MESSAGE_BLOCK_BYTES, messages.size())); + } + + // Copy messages into block + ASSERT(messages.size() <= block.capacity() - block.size()); + block.append(block.arena(), messages.begin(), messages.size()); + self->messageBlocks.push_back( std::make_pair(version, block) ); + addedBytes += int64_t(block.size()) * SERVER_KNOBS->TLOG_MESSAGE_BLOCK_OVERHEAD_FACTOR; + messages = StringRef(block.end()-messages.size(), messages.size()); + + for(auto tag = tags.begin(); tag != tags.end(); ++tag) { + int64_t tagMessages = 0; + + auto tsm = self->tag_data.find(tag->tag); + if (tsm == self->tag_data.end()) { + tsm = self->tag_data.insert( mapPair(std::move(Tag(tag->tag)), TLogData::TagData(Version(0), true, true, tag->tag) ), false ); + } + + if (version >= tsm->value.popped) { + for(int m = 0; m < tag->messageOffsets.size(); ++m) { + int offs = tag->messageOffsets[m]; + uint8_t const* p = offs < messages1.size() ? messages1.begin() + offs : messages.begin() + offs - messages1.size(); + tsm->value.version_messages.push_back(std::make_pair(version, LengthPrefixedStringRef((uint32_t*)p))); + if(tsm->value.version_messages.back().second.expectedSize() > SERVER_KNOBS->MAX_MESSAGE_SIZE) { + TraceEvent(SevWarnAlways, "LargeMessage").detail("Size", tsm->value.version_messages.back().second.expectedSize()); + } + if (tag->tag != txsTag) + expectedBytes += tsm->value.version_messages.back().second.expectedSize(); + + ++tagMessages; + } + } + + // The factor of VERSION_MESSAGES_OVERHEAD is intended to be an overestimate of the actual memory used to store this data in a std::deque. + // In practice, this number is probably something like 528/512 ~= 1.03, but this could vary based on the implementation. + // There will also be a fixed overhead per std::deque, but its size should be trivial relative to the size of the TLog + // queue and can be thought of as increasing the capacity of the queue slightly. + addedBytes += (tagMessages * sizeof(std::pair) * SERVER_KNOBS->VERSION_MESSAGES_OVERHEAD_FACTOR_1024THS) >> 10; + } + + self->version_sizes[version] = make_pair(expectedBytes, expectedBytes); + self->bytesInput += addedBytes; + + //TraceEvent("TLogPushed", self->dbgid).detail("Bytes", addedBytes).detail("MessageBytes", messages.size()).detail("Tags", tags.size()).detail("expectedBytes", expectedBytes).detail("mCount", mCount).detail("tCount", tCount); + } + + Version poppedVersion( TLogData* self, Tag tag) { + auto mapIt = self->tag_data.find(tag); + if (mapIt == self->tag_data.end()) + return Version(0); + return mapIt->value.popped; + } + + std::deque> & get_version_messages( TLogData* self, Tag tag ) { + auto mapIt = self->tag_data.find(tag); + if (mapIt == self->tag_data.end()) { + static std::deque> empty; + return empty; + } + return mapIt->value.version_messages; + }; + + ACTOR Future tLogPop( TLogData* self, TLogPopRequest req ) { + auto ti = self->tag_data.find(req.tag); + if (ti == self->tag_data.end()) { + ti = self->tag_data.insert( mapPair(std::move(Tag(req.tag)), TLogData::TagData(req.to, true, true, req.tag)) ); + } else if (req.to > ti->value.popped) { + ti->value.popped = req.to; + ti->value.popped_recently = true; + //if (to.epoch == self->epoch()) + if ( req.to > self->persistentDataDurableVersion ) + Void _ = wait(ti->value.eraseMessagesBefore( req.to, &self->bytesDurable, self, TaskTLogPop )); + //TraceEvent("TLogPop", self->dbgid).detail("Tag", req.tag).detail("To", req.to); + } + + req.reply.send(Void()); + return Void(); + } + + void peekMessagesFromMemory( TLogData* self, TLogPeekRequest const& req, BinaryWriter& messages, Version& endVersion ) { + ASSERT( !messages.getLength() ); + + auto& deque = get_version_messages(self, req.tag); + //TraceEvent("tLogPeekMem", self->dbgid).detail("Tag", printable(req.tag1)).detail("pDS", self->persistentDataSequence).detail("pDDS", self->persistentDataDurableSequence).detail("Oldest", map1.empty() ? 0 : map1.begin()->key ).detail("OldestMsgCount", map1.empty() ? 0 : map1.begin()->value.size()); + + Version begin = std::max( req.begin, self->persistentDataDurableVersion+1 ); + auto it = std::lower_bound(deque.begin(), deque.end(), std::make_pair(begin, LengthPrefixedStringRef()), CompareFirst>()); + + Version currentVersion = -1; + for(; it != deque.end(); ++it) { + if(it->first != currentVersion) { + if (messages.getLength() >= SERVER_KNOBS->DESIRED_TOTAL_BYTES) { + endVersion = it->first; + //TraceEvent("tLogPeekMessagesReached2", self->dbgid); + break; + } + + currentVersion = it->first; + messages << int32_t(-1) << currentVersion; + } + + messages << it->second.toStringRef(); + } + } + + ACTOR Future tLogPeekMessages( TLogData* self, TLogPeekRequest req ) { + state BinaryWriter messages(Unversioned()); + state BinaryWriter messages2(Unversioned()); + state int sequence = -1; + state UID peekId; + + if(req.sequence.present()) { + try { + peekId = req.sequence.get().first; + sequence = req.sequence.get().second; + if(sequence > 0) { + auto& trackerData = self->peekTracker[peekId]; + trackerData.lastUpdate = now(); + Version ver = wait(trackerData.sequence_version[sequence].getFuture()); + req.begin = ver; + Void _ = wait(yield()); + } + } catch( Error &e ) { + if(e.code() == error_code_timed_out) { + req.reply.sendError(timed_out()); + return Void(); + } else { + throw; + } + } + } + + if( req.returnIfBlocked && self->version.get() < req.begin ) { + req.reply.sendError(end_of_stream()); + return Void(); + } + + //TraceEvent("tLogPeekMessages0", self->dbgid).detail("reqBeginEpoch", req.begin.epoch).detail("reqBeginSeq", req.begin.sequence).detail("epoch", self->epoch()).detail("persistentDataSeq", self->persistentDataSequence).detail("Tag1", printable(req.tag1)).detail("Tag2", printable(req.tag2)); + // Wait until we have something to return that the caller doesn't already have + if( self->version.get() < req.begin ) { + Void _ = wait( self->version.whenAtLeast( req.begin ) ); + Void _ = wait( delay(SERVER_KNOBS->TLOG_PEEK_DELAY, g_network->getCurrentTask()) ); + } + + state Version endVersion = self->version.get() + 1; + + //grab messages from disk + //TraceEvent("tLogPeekMessages", self->dbgid).detail("reqBeginEpoch", req.begin.epoch).detail("reqBeginSeq", req.begin.sequence).detail("epoch", self->epoch()).detail("persistentDataSeq", self->persistentDataSequence).detail("Tag1", printable(req.tag1)).detail("Tag2", printable(req.tag2)); + if( req.begin <= self->persistentDataDurableVersion ) { + // Just in case the durable version changes while we are waiting for the read, we grab this data from memory. We may or may not actually send it depending on + // whether we get enough data from disk. + // SOMEDAY: Only do this if an initial attempt to read from disk results in insufficient data and the required data is no longer in memory + // SOMEDAY: Should we only send part of the messages we collected, to actually limit the size of the result? + + peekMessagesFromMemory( self, req, messages2, endVersion ); + + Standalone> kvs = wait( + self->persistentData->readRange(KeyRangeRef( + persistTagMessagesKey(req.tag, req.begin), + persistTagMessagesKey(req.tag, self->persistentDataDurableVersion + 1)), SERVER_KNOBS->DESIRED_TOTAL_BYTES, SERVER_KNOBS->DESIRED_TOTAL_BYTES)); + + //TraceEvent("TLogPeekResults", self->dbgid).detail("ForAddress", req.reply.getEndpoint().address).detail("Tag1Results", s1).detail("Tag2Results", s2).detail("Tag1ResultsLim", kv1.size()).detail("Tag2ResultsLim", kv2.size()).detail("Tag1ResultsLast", kv1.size() ? printable(kv1[0].key) : "").detail("Tag2ResultsLast", kv2.size() ? printable(kv2[0].key) : "").detail("Limited", limited).detail("NextEpoch", next_pos.epoch).detail("NextSeq", next_pos.sequence).detail("NowEpoch", self->epoch()).detail("NowSeq", self->sequence.getNextSequence()); + + for (auto &kv : kvs) { + auto ver = decodeTagMessagesKey(kv.key); + messages << int32_t(-1) << ver; + messages.serializeBytes(kv.value); + } + + if (kvs.expectedSize() >= SERVER_KNOBS->DESIRED_TOTAL_BYTES) + endVersion = decodeTagMessagesKey(kvs.end()[-1].key) + 1; + else + messages.serializeBytes( messages2.toStringRef() ); + } else { + peekMessagesFromMemory( self, req, messages, endVersion ); + //TraceEvent("TLogPeekResults", self->dbgid).detail("ForAddress", req.reply.getEndpoint().address).detail("MessageBytes", messages.getLength()).detail("NextEpoch", next_pos.epoch).detail("NextSeq", next_pos.sequence).detail("NowSeq", self->sequence.getNextSequence()); + } + + Version poppedVer = poppedVersion(self, req.tag); + + TLogPeekReply reply; + reply.maxKnownVersion = self->version.get(); + if(poppedVer > req.begin) { + reply.popped = poppedVer; + reply.end = poppedVer; + } else { + reply.messages = messages.toStringRef(); + reply.end = endVersion; + } + //TraceEvent("TlogPeek", self->dbgid).detail("endVer", reply.end).detail("msgBytes", reply.messages.expectedSize()).detail("ForAddress", req.reply.getEndpoint().address); + + if(req.sequence.present()) { + auto& trackerData = self->peekTracker[peekId]; + trackerData.lastUpdate = now(); + auto& sequenceData = trackerData.sequence_version[sequence+1]; + if(sequenceData.isSet()) { + if(sequenceData.getFuture().get() != reply.end) { + TEST(true); //tlog peek second attempt ended at a different version + req.reply.sendError(timed_out()); + return Void(); + } + } else { + sequenceData.send(reply.end); + } + } + + req.reply.send( reply ); + return Void(); + } + + ACTOR Future doQueueCommit( TLogData* self ) { + state Version ver = self->version.get(); + state Version commitNumber = self->queueCommitBegin+1; + self->queueCommitBegin = commitNumber; + self->queueCommittingVersion = ver; + + Future c = self->persistentQueue->commit(); + self->diskQueueCommitBytes = 0; + self->largeDiskQueueCommitBytes.set(false); + + Void _ = wait(c); + Void _ = wait(self->queueCommitEnd.whenAtLeast(commitNumber-1)); + + //Calling check_yield instead of yield to avoid a destruction ordering problem in simulation + if(g_network->check_yield(g_network->getCurrentTask())) { + Void _ = wait(delay(0, g_network->getCurrentTask())); + } + + ASSERT( ver > self->queueCommittedVersion.get() ); + + self->queueCommittedVersion.set(ver); + self->queueCommitEnd.set(commitNumber); + + TraceEvent("TLogCommitDurable", self->dbgid).detail("Version", ver); + + return Void(); + } + + ACTOR Future commitQueue( TLogData* self ) { + loop { + Void _ = wait( self->version.whenAtLeast( std::max(self->queueCommittingVersion, self->queueCommittedVersion.get()) + 1 ) ); + while( self->queueCommitBegin != self->queueCommitEnd.get() && !self->largeDiskQueueCommitBytes.get() ) { + Void _ = wait( self->queueCommitEnd.whenAtLeast(self->queueCommitBegin) || self->largeDiskQueueCommitBytes.onChange() ); + } + self->addActor.send(doQueueCommit(self)); + } + } + + ACTOR Future tLogCommit( + TLogData* self, + TLogCommitRequest req, + PromiseStream warningCollectorInput ) { + + state Optional tlogDebugID; + if(req.debugID.present()) + { + tlogDebugID = g_nondeterministic_random->randomUniqueID(); + g_traceBatch.addAttach("CommitAttachID", req.debugID.get().first(), tlogDebugID.get().first()); + g_traceBatch.addEvent("CommitDebug", tlogDebugID.get().first(), "TLog.tLogCommit.BeforeWaitForVersion"); + } + + self->knownCommittedVersion = std::max(self->knownCommittedVersion, req.knownCommittedVersion); + + Void _ = wait( self->version.whenAtLeast( req.prevVersion ) ); + + //Calling check_yield instead of yield to avoid a destruction ordering problem in simulation + if(g_network->check_yield(g_network->getCurrentTask())) { + Void _ = wait(delay(0, g_network->getCurrentTask())); + } + + if(self->stopped) { + req.reply.sendError( tlog_stopped() ); + return Void(); + } + + if (self->version.get() == req.prevVersion) { // Not a duplicate (check relies on no waiting between here and self->version.set() below!) + if(req.debugID.present()) + g_traceBatch.addEvent("CommitDebug", tlogDebugID.get().first(), "TLog.tLogCommit.Before"); + + TraceEvent("TLogCommit", self->dbgid).detail("Version", req.version); + commitMessages(self, req.version, req.arena, req.messages, req.tags); + + // Log the changes to the persistent queue, to be committed by commitQueue() + TLogQueueEntryRef qe; + qe.version = req.version; + qe.knownCommittedVersion = req.knownCommittedVersion; + qe.messages = req.messages; + qe.tags = req.tags; + self->persistentQueue->push( qe ); + + self->diskQueueCommitBytes += qe.expectedSize(); + if( self->diskQueueCommitBytes > SERVER_KNOBS->MAX_QUEUE_COMMIT_BYTES ) { + self->largeDiskQueueCommitBytes.set(true); + } + + // Notifies the commitQueue actor to commit persistentQueue, and also unblocks tLogPeekMessages actors + self->prevVersion = self->version.get(); + self->version.set( req.version ); + + if(req.debugID.present()) + g_traceBatch.addEvent("CommitDebug", tlogDebugID.get().first(), "TLog.tLogCommit.AfterTLogCommit"); + } + // Send replies only once all prior messages have been received and committed. + Void _ = wait( timeoutWarning( self->queueCommittedVersion.whenAtLeast( req.version ), 0.1, warningCollectorInput ) ); + + if(req.debugID.present()) + g_traceBatch.addEvent("CommitDebug", tlogDebugID.get().first(), "TLog.tLogCommit.After"); + + req.reply.send( Void() ); + return Void(); + } + + ACTOR Future initPersistentState( TLogData* self ) { + // PERSIST: Initial setup of persistentData for a brand new tLog for a new database + IKeyValueStore *storage = self->persistentData; + storage->set( persistFormat ); + storage->set( KeyValueRef( persistID, BinaryWriter::toValue( self->dbgid, Unversioned() ) ) ); + storage->set( KeyValueRef( persistCurrentVersionKey, BinaryWriter::toValue(self->version.get(), Unversioned()) ) ); + storage->set( KeyValueRef( persistRecoveryCountKey, BinaryWriter::toValue(self->recoveryCount, Unversioned()) ) ); + + TraceEvent("TLogInitCommit", self->dbgid).detail("Version", self->version.get()); + Void _ = wait( storage->commit() ); + return Void(); + } + + ACTOR Future restorePersistentState( TLogData* self, Promise outRecoveryCount, bool processQueue, TLogInterface myInterface ) { + state double startt = now(); + // PERSIST: Read basic state from persistentData; replay persistentQueue but don't erase it + IKeyValueStore *storage = self->persistentData; + + TraceEvent("TLogRestorePersistentState", self->dbgid).detail("pq", processQueue); + + state Future> fFormat = storage->readValue(persistFormat.key); + state Future> fID = storage->readValue(persistID); + state Future> fVer = storage->readValue(persistCurrentVersionKey); + state Future> fRecoveryCount = storage->readValue(persistRecoveryCountKey); + state Future> fRecoveryInProgress = storage->readValue( persistRecoveryInProgress.key ); + + // FIXME: metadata in queue? + + Void _ = wait( waitForAll( (vector>>(), fFormat, fID, fVer, fRecoveryCount, fRecoveryInProgress) ) ); + + if (fFormat.get().present() && !persistFormatReadableRange.contains( fFormat.get().get() )) { + TraceEvent(SevError, "UnsupportedDBFormat", self->dbgid).detail("Format", printable(fFormat.get().get())).detail("Expected", persistFormat.value.toString()); + throw worker_recovery_failed(); + } + + if (fRecoveryInProgress.get().present()) { + TEST(true); // We must have rebooted during network recovery; the master recovery that depended on us will fail and we can permanently delete our (incomplete) storage + TraceEvent("RestartedDuringNetworkRecovery", self->dbgid); + throw worker_removed(); + } + + if (!fFormat.get().present()) { + Standalone> v = wait( self->persistentData->readRange( KeyRangeRef(StringRef(), LiteralStringRef("\xff")), 1 ) ); + if (!v.size()) { + TEST(true); // The DB is completely empty, so it was never initialized. Delete it. + throw worker_removed(); + } else { + // This should never happen + TraceEvent(SevError, "NoDBFormatKey", self->dbgid).detail("FirstKey", printable(v[0].key)); + ASSERT( false ); + throw worker_recovery_failed(); + } + } + + + ASSERT( self->dbgid == BinaryReader::fromStringRef(fID.get().get(), Unversioned()) ); + + Version ver = BinaryReader::fromStringRef( fVer.get().get(), Unversioned() ); + self->persistentDataVersion = ver; + self->persistentDataDurableVersion = ver; + self->version.set( ver ); + + TraceEvent("TLogRestorePersistentStateVer", self->dbgid).detail("ver", self->version.get()); + + self->recoveryCount = BinaryReader::fromStringRef( fRecoveryCount.get().get(), Unversioned() ); + + outRecoveryCount.send( self->recoveryCount ); // This might cancel this actor (if the recovery count is ancient) and destroy self + Void _ = wait(Future(Void())); // ... so check for cancellation + + // Restore popped keys. Pop operations that took place after the last (committed) updatePersistentDataVersion might be lost, but + // that is fine because we will get the corresponding data back, too. + state KeyRange tagKeys = persistTagPoppedKeys; + loop { + Standalone> data = wait( self->persistentData->readRange( tagKeys, BUGGIFY ? 3 : 1<<30, 1<<20 ) ); + if (!data.size()) break; + ((KeyRangeRef&)tagKeys) = KeyRangeRef( keyAfter(data.back().key, tagKeys.arena()), tagKeys.end ); + + for(auto &kv : data) { + Tag tag = decodeTagPoppedKey(kv.key); + Version popped = decodeTagPoppedValue(kv.value); + TraceEvent("TLogRestorePop", self->dbgid).detail("Tag", tag).detail("To", popped); + ASSERT( self->tag_data.find(tag) == self->tag_data.end() ); + self->tag_data.insert( mapPair( std::move(Tag(tag)), TLogData::TagData( popped, false, false, tag )) ); + } + } + + // PERSIST: Apply changes from queue + if (processQueue) { + state Version lastVer = 0; + state double recoverMemoryLimit = SERVER_KNOBS->TARGET_BYTES_PER_TLOG + SERVER_KNOBS->SPRING_BYTES_TLOG; + if (BUGGIFY) recoverMemoryLimit = SERVER_KNOBS->BUGGIFY_RECOVER_MEMORY_LIMIT; + try { + loop { + TLogQueueEntry qe = wait( self->persistentQueue->readNext() ); + //TraceEvent("TLogRecoveredQE", self->dbgid).detail("ver", qe.version).detail("MessageBytes", qe.messages.size()).detail("Tags", qe.tags.size()) + // .detail("Tag0", qe.tags.size() ? qe.tags[0].tag : invalidTag); + + ASSERT( qe.version > lastVer ); + lastVer = qe.version; + self->knownCommittedVersion = std::max(self->knownCommittedVersion, qe.knownCommittedVersion); + if( qe.version > self->version.get() ) { + commitMessages(self, qe.version, qe.arena(), qe.messages, qe.tags); + self->version.set( qe.version ); + self->queueCommittedVersion.set( qe.version ); + + if (self->bytesInput.getValue() - self->bytesDurable.getValue() > recoverMemoryLimit) { + TEST(true); // Flush excess data during TLog queue recovery + TraceEvent("FlushLargeQueueDuringRecovery", self->dbgid).detail("BytesInput", self->bytesInput.getValue()).detail("BytesDurable", self->bytesDurable.getValue()).detail("Version", self->version.get()).detail("PVer", self->persistentDataVersion); + + while(self->persistentDataDurableVersion != self->version.get()) { + Version nextVersion; + int totalSize = 0; + std::vector>::iterator, std::deque>::iterator>> iters; + for(auto tag = self->tag_data.begin(); tag != self->tag_data.end(); ++tag) + iters.push_back(std::make_pair(tag->value.version_messages.begin(), tag->value.version_messages.end())); + + while( totalSize < SERVER_KNOBS->UPDATE_STORAGE_BYTE_LIMIT ) { + nextVersion = self->version.get(); + for( auto &it : iters ) + if(it.first != it.second) + nextVersion = std::min( nextVersion, it.first->first + 1 ); + + if(nextVersion == self->version.get()) + break; + + for( auto &it : iters ) { + while (it.first != it.second && it.first->first < nextVersion) { + totalSize += it.first->second.expectedSize(); + ++it.first; + } + } + } + + Void _ = wait( updatePersistentData(self, nextVersion ) ); + } + } + } + } + } catch (Error& e) { + if (e.code() != error_code_end_of_stream) throw; + } + } + + TraceEvent("TLogRestorePersistentStateDone", self->dbgid) + .detail("pq", processQueue).detail("version", self->version.get()).detail("durableVer", self->persistentDataDurableVersion) + .detail("Took", now()-startt); + TEST( now()-startt >= 1.0 ); // TLog recovery took more than 1 second + TEST( processQueue ); // TLog recovered from disk queue + + return Void(); + } + + void getQueuingMetrics( TLogData* self, TLogQueuingMetricsRequest const& req ) { + TLogQueuingMetricsReply reply; + reply.localTime = now(); + reply.instanceID = self->instanceID; + reply.bytesInput = self->bytesInput.getValue(); + reply.bytesDurable = self->bytesDurable.getValue(); + reply.storageBytes = self->persistentData->getStorageBytes(); + reply.v = self->prevVersion; + req.reply.send( reply ); + } + + ACTOR Future respondToRecovered( TLogInterface tli, Future recovery ) { + Void _ = wait( recovery ); + + loop { + TLogRecoveryFinishedRequest req = waitNext( tli.recoveryFinished.getFuture() ); + req.reply.send(Void()); + } + } + + ACTOR Future cleanupPeekTrackers( TLogData* self ) { + loop { + double minExpireTime = SERVER_KNOBS->PEEK_TRACKER_EXPIRATION_TIME; + auto it = self->peekTracker.begin(); + while(it != self->peekTracker.end()) { + double expireTime = SERVER_KNOBS->PEEK_TRACKER_EXPIRATION_TIME - now()-it->second.lastUpdate; + if(expireTime < 1.0e-6) { + for(auto seq : it->second.sequence_version) { + if(!seq.second.isSet()) { + seq.second.sendError(timed_out()); + } + } + it = self->peekTracker.erase(it); + } else { + minExpireTime = std::min(minExpireTime, expireTime); + ++it; + } + } + + Void _ = wait( delay(minExpireTime) ); + } + } + + ACTOR Future serveTLogInterface( TLogData* self, TLogInterface tli, PromiseStream warningCollectorInput ) { + loop choose { + when( TLogPeekRequest req = waitNext( tli.peekMessages.getFuture() ) ) { + self->addActor.send( tLogPeekMessages( self, req ) ); + } + when( TLogPopRequest req = waitNext( tli.popMessages.getFuture() ) ) { + self->addActor.send( tLogPop( self, req ) ); + } + when( TLogCommitRequest req = waitNext( tli.commit.getFuture() ) ) { + TEST(self->stopped); // TLogCommitRequest while stopped + if (!self->stopped) + self->addActor.send( tLogCommit( self, req, warningCollectorInput ) ); + else + req.reply.sendError( tlog_stopped() ); + } + when( ReplyPromise< TLogLockResult > reply = waitNext( tli.lock.getFuture() ) ) { + self->addActor.send( tLogLock(self, reply) ); + } + when (TLogQueuingMetricsRequest req = waitNext(tli.getQueuingMetrics.getFuture())) { + getQueuingMetrics(self, req); + } + when (TLogConfirmRunningRequest req = waitNext(tli.confirmRunning.getFuture())){ + if (req.debugID.present() ) { + UID tlogDebugID = g_nondeterministic_random->randomUniqueID(); + g_traceBatch.addAttach("TransactionAttachID", req.debugID.get().first(), tlogDebugID.first()); + g_traceBatch.addEvent("TransactionDebug", tlogDebugID.first(), "TLogServer.TLogConfirmRunningRequest"); + } + if (!self->stopped) + req.reply.send(Void()); + else + req.reply.sendError( tlog_stopped() ); + } + } + } + + ACTOR Future tLogCore( TLogData* self, TLogInterface tli, Future recovery ) { + state PromiseStream warningCollectorInput; + state Future warningCollector = timeoutWarningCollector( warningCollectorInput.getFuture(), 1.0, "TLogQueueCommitSlow", self->dbgid ); + state Future error = actorCollection( self->addActor.getFuture() ); + + self->addActor.send( updateStorage(self) ); + self->addActor.send( commitQueue(self) ); + self->addActor.send( waitFailureServer(tli.waitFailure.getFuture()) ); + self->addActor.send( respondToRecovered(tli, recovery) ); + self->addActor.send( traceCounters("TLogMetrics", self->dbgid, SERVER_KNOBS->STORAGE_LOGGING_DELAY, &self->cc, self->dbgid.toString() + "/TLogMetrics")); + self->addActor.send( cleanupPeekTrackers(self) ); + + if( recovery.isValid() && !recovery.isReady()) { + self->addActor.send( recovery ); + } + + self->coreStarted = true; + + Void _ = wait( serveTLogInterface(self, tli, warningCollectorInput) || error ); + throw internal_error(); + }; + + ACTOR Future checkEmptyQueue(TLogData* self) { + TraceEvent("TLogCheckEmptyQueueBegin", self->dbgid); + try { + TLogQueueEntry r = wait( self->persistentQueue->readNext() ); + throw internal_error(); + } catch (Error& e) { + if (e.code() != error_code_end_of_stream) throw; + TraceEvent("TLogCheckEmptyQueueEnd", self->dbgid); + return Void(); + } + } + + ACTOR Future recoverTagFromLogSystem( TLogData* self, Version beginVersion, Version endVersion, Tag tag, Reference> uncommittedBytes, Reference>> logSystem ) { + state Future dbInfoChange = Void(); + state Reference r; + state Version tagAt = beginVersion; + state Version tagPopped = 0; + state Version lastVer = 0; + + TraceEvent("LogRecoveringTagBegin", self->dbgid).detail("Tag", tag).detail("recoverAt", endVersion); + + while (tagAt <= endVersion) { + loop { + choose { + when(Void _ = wait( r ? r->getMore() : Never() ) ) { + break; + } + when( Void _ = wait( dbInfoChange ) ) { + if(r) tagPopped = std::max(tagPopped, r->popped()); + if( logSystem->get() ) + r = logSystem->get()->peek( tagAt, tag ); + else + r = Reference(); + dbInfoChange = logSystem->onChange(); + } + } + } + + TraceEvent("LogRecoveringTagResults", self->dbgid).detail("Tag", tag); + + Version ver = 0; + BinaryWriter wr( Unversioned() ); + int writtenBytes = 0; + while (true) { + bool foundMessage = r->hasMessage(); + //TraceEvent("LogRecoveringMsg").detail("Tag", tag).detail("foundMessage", foundMessage).detail("ver", r->version().toString()); + if (!foundMessage || r->version().version != ver) { + ASSERT(r->version().version > lastVer); + if (ver) { + //TraceEvent("LogRecoveringTagVersion", self->dbgid).detail("Tag", tag).detail("Ver", ver).detail("Bytes", wr.getLength()); + writtenBytes += 100 + wr.getLength(); + self->persistentData->set( KeyValueRef( persistTagMessagesKey( tag, ver ), wr.toStringRef() ) ); + } + lastVer = ver; + ver = r->version().version; + wr = BinaryWriter( Unversioned() ); + if (!foundMessage || ver > endVersion) + break; + } + + // FIXME: This logic duplicates stuff in LogPushData::addMessage(), and really would be better in PeekResults or somewhere else. Also unnecessary copying. + StringRef msg = r->getMessage(); + wr << uint32_t( msg.size() + sizeof(uint32_t) ) << r->version().sub; + wr.serializeBytes( msg ); + r->nextMessage(); + } + + tagAt = r->version().version; + + if(writtenBytes) + uncommittedBytes->set(uncommittedBytes->get() + writtenBytes); + + while(uncommittedBytes->get() >= SERVER_KNOBS->LARGE_TLOG_COMMIT_BYTES) { + Void _ = wait(uncommittedBytes->onChange()); + } + } + if(r) tagPopped = std::max(tagPopped, r->popped()); + + auto tsm = self->tag_data.find(tag); + if (tsm == self->tag_data.end()) { + self->tag_data.insert( mapPair(std::move(Tag(tag)), TLogData::TagData(tagPopped, false, true, tag)) ); + } + + Void _ = wait(tLogPop( self, TLogPopRequest(tagPopped, tag) )); + + updatePersistentPopped( self, tag, self->tag_data.find(tag)->value ); + return Void(); + } + + ACTOR Future updateLogSystem(TLogData* self, LogSystemConfig recoverFrom, Reference>> logSystem) { + loop { + TraceEvent("TLogUpdate", self->dbgid).detail("recoverFrom", recoverFrom.toString()).detail("dbInfo", self->dbInfo->get().logSystemConfig.toString()); + if( self->dbInfo->get().logSystemConfig.isEqualIds(recoverFrom) ) { + logSystem->set(ILogSystem::fromLogSystemConfig( self->dbgid, self->dbInfo->get().myLocality, self->dbInfo->get().logSystemConfig )); + } else if( self->dbInfo->get().logSystemConfig.isNextGenerationOf(recoverFrom) && std::count( self->dbInfo->get().logSystemConfig.tLogs.begin(), self->dbInfo->get().logSystemConfig.tLogs.end(), self->dbgid ) ) { + logSystem->set(ILogSystem::fromOldLogSystemConfig( self->dbgid, self->dbInfo->get().myLocality, self->dbInfo->get().logSystemConfig )); + } else { + logSystem->set(Reference()); + } + Void _ = wait( self->dbInfo->onChange() ); + } + } + + ACTOR Future recoverFromLogSystem( TLogData* self, LogSystemConfig recoverFrom, Version recoverAt, Version knownCommittedVersion, std::vector recoverTags, Promise copyComplete ) { + state Future committing = Void(); + state double lastCommitT = now(); + state Reference> uncommittedBytes = Reference>(new AsyncVar()); + state std::vector> recoverFutures; + state Reference>> logSystem = Reference>>(new AsyncVar>()); + state Future updater = updateLogSystem(self, recoverFrom, logSystem); + + for(auto tag : recoverTags ) + recoverFutures.push_back(recoverTagFromLogSystem(self, knownCommittedVersion, recoverAt, tag, uncommittedBytes, logSystem)); + + state Future copyDone = waitForAll(recoverFutures); + state Future recoveryDone = Never(); + state Future commitTimeout = delay(SERVER_KNOBS->LONG_TLOG_COMMIT_TIME); + + loop { + choose { + when(Void _ = wait(copyDone)) { + recoverFutures.clear(); + for(auto tag : recoverTags ) + recoverFutures.push_back(recoverTagFromLogSystem(self, 0, knownCommittedVersion, tag, uncommittedBytes, logSystem)); + copyDone = Never(); + recoveryDone = waitForAll(recoverFutures); + + Void __ = wait( committing ); + Void __ = wait( self->updatePersist ); + committing = self->persistentData->commit(); + commitTimeout = delay(SERVER_KNOBS->LONG_TLOG_COMMIT_TIME); + uncommittedBytes->set(0); + Void __ = wait( committing ); + TraceEvent("TLogCommitCopyData", self->dbgid); + + if(!copyComplete.isSet()) + copyComplete.send(Void()); + } + when(Void _ = wait(recoveryDone)) { break; } + when(Void _ = wait(commitTimeout)) { + TEST(true); // We need to commit occasionally if this process is long to avoid running out of memory. + // We let one, but not more, commits pipeline with the network transfer + Void __ = wait( committing ); + Void __ = wait( self->updatePersist ); + committing = self->persistentData->commit(); + commitTimeout = delay(SERVER_KNOBS->LONG_TLOG_COMMIT_TIME); + uncommittedBytes->set(0); + TraceEvent("TLogCommitRecoveryData", self->dbgid).detail("MemoryUsage", DEBUG_DETERMINISM ? 0 : getMemoryUsage()); + } + when(Void _ = wait(uncommittedBytes->onChange())) { + if(uncommittedBytes->get() >= SERVER_KNOBS->LARGE_TLOG_COMMIT_BYTES) + commitTimeout = Void(); + } + } + } + + Void _ = wait( committing ); + Void _ = wait( self->updatePersist ); + Void _ = wait( self->persistentData->commit() ); + + TraceEvent("TLogRecoveryComplete", self->dbgid).detail("Locality", self->dbInfo->get().myLocality.toString()); + TEST(true); // tLog restore from old log system completed + + return Void(); + } + + ACTOR Future tLogStart( TLogData* self, LogSystemConfig recoverFrom, Version recoverAt, Version knownCommittedVersion, std::vector recoverTags, bool recoverFromDisk, + TLogInterface tli, ReplyPromise outInterface, Promise outRecoveryCount ) { + state Future recovery = Void(); + if (recoverFrom.logSystemType == 1) { + ASSERT(false); + } else if (recoverFrom.logSystemType == 2) { + Void _ = wait( checkEmptyQueue(self) ); + + self->persistentDataVersion = recoverAt; + self->persistentDataDurableVersion = recoverAt; // durable is a white lie until initPersistentState() commits the store + self->queueCommittedVersion.set( recoverAt ); + self->version.set( recoverAt ); + + Void _ = wait( initPersistentState( self ) ); + + state Promise copyComplete; + recovery = recoverFromLogSystem( self, recoverFrom, recoverAt, knownCommittedVersion, recoverTags, copyComplete ); + Void _ = wait(copyComplete.getFuture()); + } else if (recoverFromDisk) { + Void _ = wait( restorePersistentState( self, outRecoveryCount, true, tli ) ); + TEST(true); // tLog restore from disk completed + } else { + // Brand new tlog, initialization has already been done by caller + Void _ = wait( checkEmptyQueue(self) ); + Void _ = wait( initPersistentState( self ) ); + } + + TraceEvent("TLogReady", self->dbgid); + + validate(self); + //dump(self); + + outInterface.send( tli ); + + Void _ = wait( tLogCore( self, tli, recovery ) ); + throw internal_error(); // tLogCore() shouldn't return without an error + } + + ACTOR Future rejoinMasters( TLogData* self, TLogInterface tli, Future fRecoveryCount ) { + state DBRecoveryCount recoveryCount = wait( fRecoveryCount ); + state UID lastMasterID(0,0); + loop { + auto const& inf = self->dbInfo->get(); + bool isDisplaced = inf.recoveryCount >= recoveryCount && inf.recoveryState != 0 && + !std::count( inf.logSystemConfig.tLogs.begin(), inf.logSystemConfig.tLogs.end(), tli.id() ) && + !std::count( inf.priorCommittedLogServers.begin(), inf.priorCommittedLogServers.end(), tli.id() ); + for(int i = 0; i < inf.logSystemConfig.oldTLogs.size() && isDisplaced; i++) { + isDisplaced = !std::count( inf.logSystemConfig.oldTLogs[i].tLogs.begin(), inf.logSystemConfig.oldTLogs[i].tLogs.end(), tli.id() ); + } + if ( isDisplaced ) + { + TraceEvent("TLogDisplaced", tli.id()).detail("Reason", "DBInfoDoesNotContain"); + if (BUGGIFY) Void _ = wait( delay( SERVER_KNOBS->BUGGIFY_WORKER_REMOVED_MAX_LAG * g_random->random01() ) ); + throw worker_removed(); + } + + if (self->dbInfo->get().master.id() != lastMasterID) { + // The TLogRejoinRequest is needed to establish communications with a new master, which doesn't have our TLogInterface + TLogRejoinRequest req; + req.myInterface = tli; + TraceEvent("TLogRejoining", self->dbgid).detail("Master", self->dbInfo->get().master.id()); + choose { + when ( bool success = wait( brokenPromiseToNever( self->dbInfo->get().master.tlogRejoin.getReply( req ) ) ) ) { + if (success) + lastMasterID = self->dbInfo->get().master.id(); + } + when ( Void _ = wait( self->dbInfo->onChange() ) ) { } + } + } else + Void _ = wait( self->dbInfo->onChange() ); + } + } + + // Restore from disk + ACTOR Future tLog( IKeyValueStore* persistentData, IDiskQueue* persistentQueue, TLogInterface tli, Reference> db ) { + state TLogData self( tli.id(), persistentData, persistentQueue, db ); + state Promise recoveryCount; + state Future removed = rejoinMasters(&self, tli, recoveryCount.getFuture()); + + Void _ = wait( tLogStart( &self, LogSystemConfig(), Version(0), Version(0), std::vector(), true, tli, ReplyPromise(), recoveryCount ) || removed ); + throw internal_error(); // tLogStart doesn't return without an error + } +} \ No newline at end of file diff --git a/fdbserver/Orderer.actor.h b/fdbserver/Orderer.actor.h new file mode 100644 index 0000000000..bcd5ee5cf2 --- /dev/null +++ b/fdbserver/Orderer.actor.h @@ -0,0 +1,74 @@ +/* + * Orderer.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +// When actually compiled (NO_INTELLISENSE), include the generated version of this file. In intellisense use the source version. +#if defined(NO_INTELLISENSE) && !defined(FDBSERVER_ORDERER_ACTOR_G_H) + #define FDBSERVER_ORDERER_ACTOR_G_H + #include "Orderer.actor.g.h" +#elif !defined(FDBSERVER_ORDERER_ACTOR_H) + #define FDBSERVER_ORDERER_ACTOR_H + +#include "flow/actorcompiler.h" +#include "flow/Notified.h" + +template +class Orderer { +public: + explicit Orderer( Seq s ) : ready(s), started(false) {} + void reset( Seq s ) { + ready = NotifiedVersion(s); + started = false; + } + Future order( Seq s, int taskID = TaskDefaultYield ) { + if ( ready.get() < s ) + return waitAndOrder( this, s, taskID ); + else + return dedup(s); + } + void complete( Seq s ) { + ASSERT( s == ready.get() && started ); + started = false; + ready.set(s+1); + } + Seq getNextSequence() { return ready.get(); } // Returns the next sequence number which has *not* been returned from order() + Future whenNextSequenceAtLeast( Seq v ) { + return ready.whenAtLeast(v); + } +private: + ACTOR static Future waitAndOrder( Orderer* self, Seq s, int taskID ) { + Void _ = wait( self->ready.whenAtLeast(s) ); + Void _ = wait( yield( taskID ) || self->shutdown.getFuture() ); + return self->dedup(s); + } + bool dedup( Seq s ) { + if (s != ready.get() || started) + return false; + started = true; + return true; + } + + bool started; + NotifiedVersion ready; // FIXME: Notified + Promise shutdown; // Never set, only broken on destruction +}; + +#endif \ No newline at end of file diff --git a/fdbserver/QuietDatabase.actor.cpp b/fdbserver/QuietDatabase.actor.cpp new file mode 100644 index 0000000000..38f295f9e0 --- /dev/null +++ b/fdbserver/QuietDatabase.actor.cpp @@ -0,0 +1,350 @@ +/* + * QuietDatabase.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/ActorCollection.h" +#include "fdbrpc/simulator.h" +#include "flow/Trace.h" +#include "fdbclient/NativeAPI.h" +#include "fdbclient/DatabaseContext.h" +#include "TesterInterface.h" +#include "WorkerInterface.h" +#include "ServerDBInfo.h" +#include "Status.h" +#include "fdbclient/ManagementAPI.h" + +ACTOR Future>> getWorkers( Reference> dbInfo ) { + loop { + choose { + when( vector> w = wait( brokenPromiseToNever( dbInfo->get().clusterInterface.getWorkers.getReply( GetWorkersRequest() ) ) ) ) { + return w; + } + when( Void _ = wait( dbInfo->onChange() ) ) {} + } + } +} + +//Gets the WorkerInterface representing the Master server. +ACTOR Future getMasterWorker( Database cx, Reference> dbInfo ) { + TraceEvent("GetMasterWorker").detail("Database", printable(cx->dbName)).detail("Stage", "GettingWorkers"); + + loop { + state vector> workers = wait( getWorkers( dbInfo ) ); + + for( int i = 0; i < workers.size(); i++ ) { + if( workers[i].first.address() == dbInfo->get().master.address() ) { + TraceEvent("GetMasterWorker").detail("Database", printable(cx->dbName)).detail("Stage", "GotWorkers").detail("masterId", dbInfo->get().master.id()).detail("workerId", workers[i].first.id()); + return workers[i].first; + } + } + + TraceEvent(SevWarn, "GetMasterWorkerError") + .detail("Database", printable(cx->dbName)).detail("Error", "MasterWorkerNotFound") + .detail("Master", dbInfo->get().master.id()).detail("MasterAddress", dbInfo->get().master.address()) + .detail("WorkerCount", workers.size()); + + Void _ = wait(delay(1.0)); + } +} + +//Gets the number of bytes in flight from the master +ACTOR Future getDataInFlight( Database cx, WorkerInterface masterWorker ) { + try { + TraceEvent("DataInFlight").detail("Database", printable(cx->dbName)).detail("Stage", "ContactingMaster"); + Standalone md = wait( timeoutError(masterWorker.eventLogRequest.getReply( + EventLogRequest( StringRef( cx->dbName.toString() + "/TotalDataInFlight" ) ) ), 1.0 ) ); + int64_t dataInFlight; + sscanf(extractAttribute(md.toString(), "TotalBytes").c_str(), "%lld", &dataInFlight); + return dataInFlight; + } catch( Error &e ) { + TraceEvent("QuietDatabaseFailure", masterWorker.id()).detail("Reason", "Failed to extract DataInFlight"); + throw; + } + +} + +//Gets the number of bytes in flight from the master +//Convenience method that first finds the master worker from a zookeeper interface +ACTOR Future getDataInFlight( Database cx, Reference> dbInfo ) { + WorkerInterface masterWorker = wait(getMasterWorker(cx, dbInfo)); + int64_t dataInFlight = wait(getDataInFlight(cx, masterWorker)); + return dataInFlight; +} + +//Computes the queue size for storage servers and tlogs using the BytesInput and BytesDurable attributes +//For now, we must ignore invalid keys on storage servers because of a bug that can cause them to be orphaned +int64_t getQueueSize( Standalone md ) { + int64_t bytesInput, bytesDurable; + + sscanf(extractAttribute(md.toString(), "BytesInput").c_str(), "%lld", &bytesInput); + sscanf(extractAttribute(md.toString(), "BytesDurable").c_str(), "%lld", &bytesDurable); + + return bytesInput - bytesDurable; +} + +// This is not robust in the face of a TLog failure +ACTOR Future getMaxTLogQueueSize( Database cx, Reference> dbInfo, WorkerInterface masterWorker ) { + TraceEvent("MaxTLogQueueSize").detail("Database", printable(cx->dbName)) + .detail("Stage", "ContactingMaster"); + + state std::vector>> messages; + state std::vector tlogs = dbInfo->get().logSystemConfig.allPresentLogs(); + for(int i = 0; i < tlogs.size(); i++) { + messages.push_back( timeoutError(masterWorker.eventLogRequest.getReply( + EventLogRequest( StringRef( "TLogQueueSize/" + tlogs[i].id().toString() ) ) ), 1.0 ) ); + } + Void _ = wait( waitForAll( messages ) ); + + TraceEvent("MaxTLogQueueSize").detail("Database", printable(cx->dbName)) + .detail("Stage", "ComputingMax").detail("MessageCount", messages.size()); + + state int64_t maxQueueSize = 0; + state int i = 0; + for(; i < messages.size(); i++) { + try { + maxQueueSize = std::max( maxQueueSize, getQueueSize( messages[i].get() ) ); + } catch( Error &e ) { + TraceEvent("QuietDatabaseFailure", masterWorker.id()).detail("Reason", "Failed to extract MaxTLogQueue").detail("Tlog", tlogs[i].id()); + throw; + } + } + + return maxQueueSize; +} + +ACTOR Future getMaxTLogQueueSize( Database cx, Reference> dbInfo ) { + WorkerInterface masterWorker = wait(getMasterWorker(cx, dbInfo)); + int64_t maxQueueSize = wait(getMaxTLogQueueSize(cx, dbInfo, masterWorker)); + return maxQueueSize; +} + +ACTOR Future> getStorageServers( Database cx, bool use_system_priority = false) { + state Transaction tr( cx ); + if (use_system_priority) + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + loop { + try { + Standalone serverList = wait( tr.getRange( serverListKeys, CLIENT_KNOBS->TOO_MANY ) ); + ASSERT( !serverList.more && serverList.size() < CLIENT_KNOBS->TOO_MANY ); + + vector servers; + for( int i = 0; i < serverList.size(); i++ ) + servers.push_back( decodeServerListValue( serverList[i].value ) ); + return servers; + } + catch(Error &e) { + Void _ = wait( tr.onError(e) ); + } + } +} + +//Gets the maximum size of all the storage server queues +ACTOR Future getMaxStorageServerQueueSize( Database cx, Reference> dbInfo, WorkerInterface masterWorker ) { + TraceEvent("MaxStorageServerQueueSize").detail("Database", printable(cx->dbName)).detail("Stage", "ContactingMaster"); + + state vector servers = wait( getStorageServers( cx ) ); + state std::vector>> messages; + for(int i = 0; i < servers.size(); i++) { + messages.push_back( timeoutError(masterWorker.eventLogRequest.getReply( + EventLogRequest( StringRef( "StorageServerQueueSize/" + servers[i].id().toString() ) ) ), 1.0 ) ); + } + + Void _ = wait( waitForAll(messages) ); + + TraceEvent("MaxStorageServerQueueSize").detail("Database", printable(cx->dbName)).detail("Stage", "ComputingMax").detail("MessageCount", messages.size()); + + state int64_t maxQueueSize = 0; + state int i = 0; + for(; i < messages.size(); i++) { + try { + maxQueueSize = std::max( maxQueueSize, getQueueSize( messages[i].get() ) ); + } catch( Error &e ) { + TraceEvent("QuietDatabaseFailure", masterWorker.id()).detail("Reason", "Failed to extract MaxStorageServerQueue").detail("SS", servers[i].id()); + throw; + } + } + + return maxQueueSize; +} + +//Gets the maximum size of all the storage server queues +//Convenience method that first gets the master worker and system map from a zookeeper interface +ACTOR Future getMaxStorageServerQueueSize( Database cx, Reference> dbInfo ) { + WorkerInterface masterWorker = wait(getMasterWorker(cx, dbInfo)); + int64_t maxQueueSize = wait(getMaxStorageServerQueueSize(cx, dbInfo, masterWorker)); + return maxQueueSize; +} + +//Gets the size of the data distribution queue. If reportInFlight is true, then data in flight is considered part of the queue +ACTOR Future getDataDistributionQueueSize( Database cx, WorkerInterface masterWorker, bool reportInFlight) { + try { + TraceEvent("DataDistributionQueueSize").detail("Database", printable(cx->dbName)).detail("Stage", "ContactingMaster"); + + Standalone movingDataMessage = wait( timeoutError(masterWorker.eventLogRequest.getReply( + EventLogRequest( StringRef( cx->dbName.toString() + "/MovingData") ) ), 1.0 ) ); + + TraceEvent("DataDistributionQueueSize").detail("Database", printable(cx->dbName)).detail("Stage", "GotString").detail("Result", printable(movingDataMessage)).detail("TrackLatest", printable( StringRef( cx->dbName.toString() + "/MovingData") ) ); + + int64_t inQueue; + sscanf(extractAttribute(movingDataMessage.toString(), "InQueue").c_str(), "%lld", &inQueue); + + if(reportInFlight) { + int64_t inFlight; + sscanf(extractAttribute(movingDataMessage.toString(), "InFlight").c_str(), "%lld", &inFlight); + inQueue += inFlight; + } + + return inQueue; + } catch( Error &e ) { + TraceEvent("QuietDatabaseFailure", masterWorker.id()).detail("Reason", "Failed to extract DataDistributionQueueSize"); + throw; + } +} + +//Gets the size of the data distribution queue. If reportInFlight is true, then data in flight is considered part of the queue +//Convenience method that first finds the master worker from a zookeeper interface +ACTOR Future getDataDistributionQueueSize( Database cx, Reference> dbInfo, bool reportInFlight ) { + WorkerInterface masterWorker = wait(getMasterWorker(cx, dbInfo)); + int64_t inQueue = wait(getDataDistributionQueueSize( cx, masterWorker, reportInFlight)); + return inQueue; +} + +//Checks that data distribution is active +ACTOR Future getDataDistributionActive( Database cx, WorkerInterface masterWorker ) { + try { + TraceEvent("DataDistributionActive").detail("Database", printable(cx->dbName)).detail("Stage", "ContactingMaster"); + + Standalone activeMessage = wait( timeoutError(masterWorker.eventLogRequest.getReply( + EventLogRequest( StringRef( cx->dbName.toString() + "/DDTrackerStarting") ) ), 1.0 ) ); + + return extractAttribute(activeMessage.toString(), "State") == "Active"; + } catch( Error &e ) { + TraceEvent("QuietDatabaseFailure", masterWorker.id()).detail("Reason", "Failed to extract DataDistributionActive"); + throw; + } +} + +//Checks to see if any storage servers are being recruited +ACTOR Future getStorageServersRecruiting( Database cx, Reference> dbInfo, WorkerInterface masterWorker ) { + try { + TraceEvent("StorageServersRecruiting").detail("Database", printable(cx->dbName)).detail("Stage", "ContactingMaster"); + + Standalone recruitingMessage = wait( timeoutError(masterWorker.eventLogRequest.getReply( + EventLogRequest( StringRef( cx->dbName.toString() + "/StorageServerRecruitment_" + dbInfo->get().master.id().toString()) ) ), 1.0 ) ); + + return extractAttribute(recruitingMessage.toString(), "State") == "Recruiting"; + } catch( Error &e ) { + TraceEvent("QuietDatabaseFailure", masterWorker.id()).detail("Reason", "Failed to extract StorageServersRecruiting").detail("MasterID", dbInfo->get().master.id()); + throw; + } +} + +ACTOR Future waitForQuietDatabase( Database cx, Reference> dbInfo, std::string phase, int64_t dataInFlightGate = 2e6, + int64_t maxTLogQueueGate = 5e6, int64_t maxStorageServerQueueGate = 5e6, int64_t maxDataDistributionQueueSize = 0 ) { + + TraceEvent(("QuietDatabase" + phase + "Begin").c_str()); + + //In a simulated environment, wait 5 seconds so that workers can move to their optimal locations + if(g_network->isSimulated()) + Void _ = wait(delay(5.0)); + + //Require 2 consecutive successful quiet database checks spaced 1 second apart + state int numSuccesses = 0; + + loop { + try { + TraceEvent("QuietDatabaseWaitingOnMaster"); + WorkerInterface masterWorker = wait(getMasterWorker( cx, dbInfo )); + TraceEvent("QuietDatabaseGotMaster"); + + state Future dataInFlight = getDataInFlight( cx, masterWorker); + state Future tLogQueueSize = getMaxTLogQueueSize( cx, dbInfo, masterWorker ); + state Future dataDistributionQueueSize = getDataDistributionQueueSize( cx, masterWorker, dataInFlightGate == 0); + state Future storageQueueSize = getMaxStorageServerQueueSize( cx, dbInfo, masterWorker ); + state Future dataDistributionActive = getDataDistributionActive( cx, masterWorker ); + state Future storageServersRecruiting = getStorageServersRecruiting ( cx, dbInfo, masterWorker ); + + Void _ = wait( success( dataInFlight ) && success( tLogQueueSize ) && success( dataDistributionQueueSize ) + && success( storageQueueSize ) && success( dataDistributionActive ) && success( storageServersRecruiting ) ); + TraceEvent(("QuietDatabase" + phase).c_str()) + .detail("dataInFlight", dataInFlight.get()).detail("maxTLogQueueSize", tLogQueueSize.get()).detail("dataDistributionQueueSize", dataDistributionQueueSize.get()) + .detail("maxStorageQueueSize", storageQueueSize.get()).detail("dataDistributionActive", dataDistributionActive.get()) + .detail("storageServersRecruiting", storageServersRecruiting.get()); + + if( dataInFlight.get() > dataInFlightGate || tLogQueueSize.get() > maxTLogQueueGate + || dataDistributionQueueSize.get() > maxDataDistributionQueueSize || storageQueueSize.get() > maxStorageServerQueueGate + || dataDistributionActive.get() == false || storageServersRecruiting.get() == true) { + + Void _ = wait( delay( 1.0 ) ); + numSuccesses = 0; + } else { + if(++numSuccesses == 2) { + TraceEvent(("QuietDatabase" + phase + "Done").c_str()); + break; + } + else + Void _ = wait(delay( 2.0 ) ); + } + } catch (Error& e) { + if( e.code() != error_code_actor_cancelled && e.code() != error_code_attribute_not_found && e.code() != error_code_timed_out) + TraceEvent(("QuietDatabase" + phase + "Error").c_str()).error(e); + + //Client invalid operation occurs if we don't get back a message from one of the servers, often corrected by retrying + if(e.code() != error_code_attribute_not_found && e.code() != error_code_timed_out) + throw; + + TraceEvent(("QuietDatabase" + phase + "Retry").c_str()).error(e); + Void _ = wait(delay(1.0)); + numSuccesses = 0; + } + } + + return Void(); +} + +//Waits for f to complete. If simulated, disables connection failures after waiting a specified amount of time +ACTOR Future disableConnectionFailuresAfter( Future f, double disableTime, std::string context ) { + if(!g_network->isSimulated()) { + Void _ = wait(f); + return Void(); + } + + choose { + when(Void _ = wait(f)) { + return Void(); + } + when(Void _ = wait(delay(disableTime))) { + g_simulator.speedUpSimulation = true; + g_simulator.enableConnectionFailures = false; + TraceEvent(SevWarnAlways, ("DisableConnectionFailures_" + context).c_str()); + } + } + + Void _ = wait(f); + return Void(); +} + + +Future quietDatabase( Database const& cx, Reference> const& dbInfo, std::string phase, int64_t dataInFlightGate, + int64_t maxTLogQueueGate, int64_t maxStorageServerQueueGate, int64_t maxDataDistributionQueueSize ) { + + Future quiet = waitForQuietDatabase(cx, dbInfo, phase, dataInFlightGate, maxTLogQueueGate, maxStorageServerQueueGate, maxDataDistributionQueueSize); + return disableConnectionFailuresAfter(quiet, 300.0, "QuietDatabase" + phase); +} diff --git a/fdbserver/QuietDatabase.h b/fdbserver/QuietDatabase.h new file mode 100644 index 0000000000..5838f6ebcd --- /dev/null +++ b/fdbserver/QuietDatabase.h @@ -0,0 +1,42 @@ +/* + * QuietDatabase.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_QUIETDATABASE_H +#define FDBSERVER_QUIETDATABASE_H +#pragma once + +#include "flow/actorcompiler.h" +#include "fdbclient/NativeAPI.h" +#include "fdbclient/DatabaseContext.h" // for clone() +#include "fdbserver/TesterInterface.h" +#include "fdbserver/WorkerInterface.h" + +Future getDataInFlight( Database const& cx, Reference> const& ); +Future getMaxTLogQueueSize( Database const& cx, Reference> const& ); +Future getMaxStorageServerQueueSize( Database const& cx, Reference> const& ); +Future getDataDistributionQueueSize( Database const &cx, Reference> const&, bool const& reportInFlight ); +Future> getStorageServers( Database const& cx, bool const &use_system_priority = false); +Future>> getWorkers( Reference> const& dbInfo ); +Future getMasterWorker( Database const& cx, Reference> const& dbInfo ); + +//Waits for f to complete. If simulated, disables connection failures after waiting a specified amount of time +Future disableConnectionFailuresAfter( Future const& f, double const& disableTime, std::string const& context ); + +#endif \ No newline at end of file diff --git a/fdbserver/Ratekeeper.actor.cpp b/fdbserver/Ratekeeper.actor.cpp new file mode 100644 index 0000000000..15f56338e7 --- /dev/null +++ b/fdbserver/Ratekeeper.actor.cpp @@ -0,0 +1,611 @@ +/* + * Ratekeeper.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/IndexedSet.h" +#include "Ratekeeper.h" +#include "fdbrpc/FailureMonitor.h" +#include "Knobs.h" +#include "fdbrpc/Smoother.h" +#include "ServerDBInfo.h" +#include "fdbrpc/simulator.h" + +enum limitReason_t { + unlimited, // TODO: rename to workload? + storage_server_write_queue_size, + storage_server_write_bandwidth_mvcc, + storage_server_readable_behind, + log_server_mvcc_write_bandwidth, + log_server_write_queue, + storage_server_min_free_space, // a storage server's normal limits are being reduced by low free space + storage_server_min_free_space_ratio, // a storage server's normal limits are being reduced by a low free space ratio + log_server_min_free_space, + log_server_min_free_space_ratio, + limitReason_t_end +}; + +int limitReasonEnd = limitReason_t_end; + +const char* limitReasonName[] = { + "workload", + "storage_server_write_queue_size", + "storage_server_write_bandwidth_mvcc", + "storage_server_readable_behind", + "log_server_mvcc_write_bandwidth", + "log_server_write_queue", + "storage_server_min_free_space", + "storage_server_min_free_space_ratio", + "log_server_min_free_space", + "log_server_min_free_space_ratio" +}; +static_assert(sizeof(limitReasonName) / sizeof(limitReasonName[0]) == limitReason_t_end, "limitReasonDesc table size"); + +// NOTE: This has a corresponding table in Script.cs (see RatekeeperReason graph) +// IF UPDATING THIS ARRAY, UPDATE SCRIPT.CS! +const char* limitReasonDesc[] = { + "Workload or read performance.", + "Storage server performance (storage queue).", + "Storage server MVCC memory.", + "Storage server version falling behind.", + "Log server MVCC memory.", + "Storage server performance (log queue).", + "Storage server running out of space (approaching 100MB limit).", + "Storage server running out of space (approaching 5% limit).", + "Log server running out of space (approaching 100MB limit).", + "Log server running out of space (approaching 5% limit).", +}; + +static_assert(sizeof(limitReasonDesc) / sizeof(limitReasonDesc[0]) == limitReason_t_end, "limitReasonDesc table size"); + +struct StorageQueueInfo { + bool valid; + UID id; + LocalityData locality; + StorageQueuingMetricsReply lastReply; + StorageQueuingMetricsReply prevReply; + Smoother smoothDurableBytes, smoothInputBytes, verySmoothDurableBytes; + Smoother smoothDurableVersion, smoothLatestVersion; + Smoother smoothFreeSpace; + Smoother smoothTotalSpace; + double readReplyRate; + limitReason_t limitReason; + StorageQueueInfo(UID id, LocalityData locality) : valid(false), id(id), locality(locality), smoothDurableBytes(SERVER_KNOBS->SMOOTHING_AMOUNT), + smoothInputBytes(SERVER_KNOBS->SMOOTHING_AMOUNT), verySmoothDurableBytes(SERVER_KNOBS->SLOW_SMOOTHING_AMOUNT), + smoothDurableVersion(1.), smoothLatestVersion(1.), smoothFreeSpace(SERVER_KNOBS->SMOOTHING_AMOUNT), + smoothTotalSpace(SERVER_KNOBS->SMOOTHING_AMOUNT), readReplyRate(0.0), limitReason(limitReason_t::unlimited) + { + // FIXME: this is a tacky workaround for a potential unitialized use in trackStorageServerQueueInfo + lastReply.instanceID = -1; + } +}; + +struct TLogQueueInfo { + bool valid; + UID id; + TLogQueuingMetricsReply lastReply; + TLogQueuingMetricsReply prevReply; + Smoother smoothDurableBytes, smoothInputBytes, verySmoothDurableBytes; + Smoother smoothFreeSpace; + Smoother smoothTotalSpace; + TLogQueueInfo(UID id) : valid(false), id(id), smoothDurableBytes(SERVER_KNOBS->SMOOTHING_AMOUNT), smoothInputBytes(SERVER_KNOBS->SMOOTHING_AMOUNT), + verySmoothDurableBytes(SERVER_KNOBS->SLOW_SMOOTHING_AMOUNT), smoothFreeSpace(SERVER_KNOBS->SMOOTHING_AMOUNT), + smoothTotalSpace(SERVER_KNOBS->SMOOTHING_AMOUNT) { + // FIXME: this is a tacky workaround for a potential unitialized use in trackTLogQueueInfo (copied from storageQueueInfO) + lastReply.instanceID = -1; + } +}; + +struct Ratekeeper { + Map storageQueueInfo; + Map tlogQueueInfo; + std::map > proxy_transactionCountAndTime; + Smoother smoothReleasedTransactions, smoothTotalDurableBytes; + double TPSLimit; + Standalone dbName; + DatabaseConfiguration configuration; + + Int64MetricHandle tpsLimitMetric; + Int64MetricHandle actualTpsMetric; + Int64MetricHandle reasonMetric; + double lastWarning; + double* lastLimited; + + Ratekeeper() : smoothReleasedTransactions(SERVER_KNOBS->SMOOTHING_AMOUNT), smoothTotalDurableBytes(SERVER_KNOBS->SLOW_SMOOTHING_AMOUNT), TPSLimit(std::numeric_limits::infinity()), + tpsLimitMetric(LiteralStringRef("Ratekeeper.TPSLimit")), + actualTpsMetric(LiteralStringRef("Ratekeeper.ActualTPS")), + reasonMetric(LiteralStringRef("Ratekeeper.Reason")), + lastWarning(0) + {} +}; + +//SOMEDAY: template trackStorageServerQueueInfo and trackTLogQueueInfo into one function +ACTOR Future trackStorageServerQueueInfo( Ratekeeper* self, StorageServerInterface ssi ) { + state double debug_lastTraceTime = 0; + self->storageQueueInfo.insert( mapPair(ssi.id(), StorageQueueInfo(ssi.id(), ssi.locality) ) ); + state Map::iterator myQueueInfo = self->storageQueueInfo.find(ssi.id()); + TraceEvent("RkTracking", ssi.id()); + try { + loop { + ErrorOr reply = wait( ssi.getQueuingMetrics.getReplyUnlessFailedFor( StorageQueuingMetricsRequest(), 0, 0 ) ); // SOMEDAY: or tryGetReply? + if (reply.present()) { + myQueueInfo->value.valid = true; + myQueueInfo->value.prevReply = myQueueInfo->value.lastReply; + myQueueInfo->value.lastReply = reply.get(); + myQueueInfo->value.readReplyRate = reply.get().readReplyRate; + if (myQueueInfo->value.prevReply.instanceID != reply.get().instanceID) { + myQueueInfo->value.smoothDurableBytes.reset(reply.get().bytesDurable); + myQueueInfo->value.verySmoothDurableBytes.reset(reply.get().bytesDurable); + myQueueInfo->value.smoothInputBytes.reset(reply.get().bytesInput); + myQueueInfo->value.smoothFreeSpace.reset(reply.get().storageBytes.available); + myQueueInfo->value.smoothTotalSpace.reset(reply.get().storageBytes.total); + } else { + self->smoothTotalDurableBytes.addDelta( reply.get().bytesDurable - myQueueInfo->value.prevReply.bytesDurable ); + myQueueInfo->value.smoothDurableBytes.setTotal( reply.get().bytesDurable ); + myQueueInfo->value.verySmoothDurableBytes.setTotal( reply.get().bytesDurable ); + myQueueInfo->value.smoothInputBytes.setTotal( reply.get().bytesInput ); + myQueueInfo->value.smoothFreeSpace.setTotal( reply.get().storageBytes.available ); + myQueueInfo->value.smoothTotalSpace.setTotal( reply.get().storageBytes.total ); + } + if (now() > debug_lastTraceTime + SERVER_KNOBS->RATEKEEPER_LOGGING_INTERVAL){ + TraceEvent("RkServerQueueInfo", ssi.id()) + .detail("LocalTime", reply.get().localTime) + .detail("BytesDurable", reply.get().bytesDurable) + .detail("BytesInput", reply.get().bytesInput) + .detail("BytesDurableSmooth", myQueueInfo->value.smoothDurableBytes.smoothTotal()) + .detail("BytesInputSmooth", myQueueInfo->value.smoothInputBytes.smoothTotal()) + .detail("BytesDurableRate", myQueueInfo->value.verySmoothDurableBytes.smoothRate()) + .detail("BytesInputRate", myQueueInfo->value.smoothInputBytes.smoothRate()) + .detail("FreeSpaceSmooth", myQueueInfo->value.smoothFreeSpace.smoothTotal()).detail("TotalSpaceSmooth", myQueueInfo->value.smoothTotalSpace.smoothTotal()) + .detail("Version", reply.get().v) + .trackLatest(("StorageServerQueueSize/" + ssi.id().toString()).c_str()); + debug_lastTraceTime = now(); + } + } else { + //If the SS didn't respond, clear the queue info so that we know it might have failed + if(myQueueInfo->value.valid) + TraceEvent("RkServerQueueInfo", ssi.id()).trackLatest(("StorageServerQueueSize/" + ssi.id().toString()).c_str()); + + myQueueInfo->value.valid = false; + } + + Void _ = wait(delayJittered(SERVER_KNOBS->METRIC_UPDATE_RATE) && IFailureMonitor::failureMonitor().onStateEqual(ssi.getQueuingMetrics.getEndpoint(), FailureStatus(false))); + } + } catch (...) { + // including cancellation + self->storageQueueInfo.erase( myQueueInfo ); + throw; + } +} + +ACTOR Future trackTLogQueueInfo( Ratekeeper* self, TLogInterface tli ) { + state double debug_lastTraceTime = 0; + self->tlogQueueInfo.insert( mapPair(tli.id(), TLogQueueInfo(tli.id()) ) ); + state Map::iterator myQueueInfo = self->tlogQueueInfo.find(tli.id()); + TraceEvent("RkTracking", tli.id()); + try { + loop { + ErrorOr reply = wait( tli.getQueuingMetrics.getReplyUnlessFailedFor( TLogQueuingMetricsRequest(), 0, 0 ) ); // SOMEDAY: or tryGetReply? + if (reply.present()) { + myQueueInfo->value.valid = true; + myQueueInfo->value.prevReply = myQueueInfo->value.lastReply; + myQueueInfo->value.lastReply = reply.get(); + if (myQueueInfo->value.prevReply.instanceID != reply.get().instanceID) { + myQueueInfo->value.smoothDurableBytes.reset(reply.get().bytesDurable); + myQueueInfo->value.verySmoothDurableBytes.reset(reply.get().bytesDurable); + myQueueInfo->value.smoothInputBytes.reset(reply.get().bytesInput); + myQueueInfo->value.smoothFreeSpace.reset(reply.get().storageBytes.available); + myQueueInfo->value.smoothTotalSpace.reset(reply.get().storageBytes.total); + } else { + self->smoothTotalDurableBytes.addDelta( reply.get().bytesDurable - myQueueInfo->value.prevReply.bytesDurable ); + myQueueInfo->value.smoothDurableBytes.setTotal(reply.get().bytesDurable); + myQueueInfo->value.verySmoothDurableBytes.setTotal(reply.get().bytesDurable); + myQueueInfo->value.smoothInputBytes.setTotal(reply.get().bytesInput); + myQueueInfo->value.smoothFreeSpace.setTotal(reply.get().storageBytes.available); + myQueueInfo->value.smoothTotalSpace.setTotal(reply.get().storageBytes.total); + } + if (now() > debug_lastTraceTime + SERVER_KNOBS->RATEKEEPER_LOGGING_INTERVAL){ + TraceEvent("RkTLogQueueInfo", tli.id()).detail("LocalTime", reply.get().localTime).detail("BytesDurable", reply.get().bytesDurable).detail("BytesInput", reply.get().bytesInput) + .detail("BytesDurableSmooth", myQueueInfo->value.smoothDurableBytes.smoothTotal()).detail("BytesInputSmooth", myQueueInfo->value.smoothInputBytes.smoothTotal()) + .detail("BytesDurableRate", myQueueInfo->value.verySmoothDurableBytes.smoothRate()).detail("BytesInputRate", myQueueInfo->value.smoothInputBytes.smoothRate()) + .detail("FreeSpaceSmooth", myQueueInfo->value.smoothFreeSpace.smoothTotal()).detail("TotalSpaceSmooth", myQueueInfo->value.smoothTotalSpace.smoothTotal()) + .detail("Version", reply.get().v) + .trackLatest(("TLogQueueSize/" + tli.id().toString()).c_str()); + debug_lastTraceTime = now(); + } + } else { + //If the TLog didn't respond, clear the queue info so that we know it might have failed + if(myQueueInfo->value.valid) + TraceEvent("RkTLogQueueInfo", tli.id()).trackLatest(("TLogQueueSize/" + tli.id().toString()).c_str()); + + myQueueInfo->value.valid = false; + } + + Void _ = wait(delayJittered(SERVER_KNOBS->METRIC_UPDATE_RATE) && IFailureMonitor::failureMonitor().onStateEqual(tli.getQueuingMetrics.getEndpoint(), FailureStatus(false))); + } + } catch (...) { + // including cancellation + self->tlogQueueInfo.erase( myQueueInfo ); + throw; + } +} + +ACTOR Future splitError( Future in, Promise errOut ) { + try { + Void _ = wait( in ); + return Void(); + } catch (Error& e) { + if (e.code() != error_code_actor_cancelled && !errOut.isSet()) + errOut.sendError(e); + throw; + } +} + +ACTOR Future trackEachStorageServer( + Ratekeeper* self, + FutureStream< std::pair> > serverChanges ) +{ + state Map> actors; + state Promise err; + loop choose { + when (state std::pair< UID, Optional > change = waitNext(serverChanges) ) { + Void _ = wait(delay(0)); // prevent storageServerTracker from getting cancelled while on the call stack + if (change.second.present()) { + auto& a = actors[ change.first ]; + a = Future(); + a = splitError( trackStorageServerQueueInfo(self, change.second.get()), err ); + } else + actors.erase( change.first ); + } + when (Void _ = wait(err.getFuture())) {} + } +} + +void updateRate( Ratekeeper* self ) { + //double controlFactor = ; // dt / eFoldingTime + + double actualTPS = self->smoothReleasedTransactions.smoothRate(); + self->actualTpsMetric = (int64_t)actualTPS; + // SOMEDAY: Remove the max( 1.0, ... ) since the below calculations _should_ be able to recover back up from this value + actualTPS = std::max( std::max( 1.0, actualTPS ), self->smoothTotalDurableBytes.smoothRate() / CLIENT_KNOBS->TRANSACTION_SIZE_LIMIT ); + + self->TPSLimit = std::numeric_limits::infinity(); + UID reasonID = UID(); + limitReason_t limitReason = limitReason_t::unlimited; + + int sscount = 0; + double readReplyRateSum=0.0; + + int64_t worstFreeSpaceStorageServer = std::numeric_limits::max(); + int64_t worstStorageQueueStorageServer = 0; + int64_t limitingStorageQueueStorageServer = 0; + + std::multimap storageTPSLimitReverseIndex; + + // Look at each storage server's write queue, compute and store the desired rate ratio + for(auto i = self->storageQueueInfo.begin(); i != self->storageQueueInfo.end(); ++i) { + auto& ss = i->value; + if (!ss.valid) continue; + ++sscount; + + ss.limitReason = limitReason_t::unlimited; + + readReplyRateSum += ss.readReplyRate; + + int64_t minFreeSpace = std::max(SERVER_KNOBS->MIN_FREE_SPACE, (int64_t)(SERVER_KNOBS->MIN_FREE_SPACE_RATIO * ss.smoothTotalSpace.smoothTotal())); + + worstFreeSpaceStorageServer = std::min(worstFreeSpaceStorageServer, (int64_t)ss.smoothFreeSpace.smoothTotal() - minFreeSpace); + + int64_t springBytes = std::max(1, std::min(SERVER_KNOBS->SPRING_BYTES_STORAGE_SERVER, (ss.smoothFreeSpace.smoothTotal() - minFreeSpace) * 0.2)); + int64_t targetBytes = std::max(1, std::min(SERVER_KNOBS->TARGET_BYTES_PER_STORAGE_SERVER, (int64_t)ss.smoothFreeSpace.smoothTotal() - minFreeSpace)); + if (targetBytes != SERVER_KNOBS->TARGET_BYTES_PER_STORAGE_SERVER) { + if (minFreeSpace == SERVER_KNOBS->MIN_FREE_SPACE) { + ss.limitReason = limitReason_t::storage_server_min_free_space; + } else { + ss.limitReason = limitReason_t::storage_server_min_free_space_ratio; + } + } + + int64_t storageQueue = ss.lastReply.bytesInput - ss.smoothDurableBytes.smoothTotal(); + worstStorageQueueStorageServer = std::max(worstStorageQueueStorageServer, storageQueue); + int64_t b = storageQueue - targetBytes; + double targetRateRatio = std::min(( b + springBytes ) / (double)springBytes, 2.0); + + double inputRate = ss.smoothInputBytes.smoothRate(); + //inputRate = std::max( inputRate, actualTPS / SERVER_KNOBS->MAX_TRANSACTIONS_PER_BYTE ); + + /*if( g_random->random01() < 0.1 ) { + TraceEvent("RateKeeperUpdateRate", ss.id) + .detail("MinFreeSpace", minFreeSpace) + .detail("SpringBytes", springBytes) + .detail("TargetBytes", targetBytes) + .detail("SmoothTotalSpaceTotal", ss.smoothTotalSpace.smoothTotal()) + .detail("SmoothFreeSpaceTotal", ss.smoothFreeSpace.smoothTotal()) + .detail("LastReplyBytesInput", ss.lastReply.bytesInput) + .detail("SmoothDurableBytesTotal", ss.smoothDurableBytes.smoothTotal()) + .detail("TargetRateRatio", targetRateRatio) + .detail("SmoothInputBytesRate", ss.smoothInputBytes.smoothRate()) + .detail("ActualTPS", actualTPS) + .detail("InputRate", inputRate) + .detail("VerySmoothDurableBytesRate", ss.verySmoothDurableBytes.smoothRate()) + .detail("b", b); + }*/ + + // Don't let any storage server use up its target bytes faster than its MVCC window! + double maxBytesPerSecond = (targetBytes - springBytes) / ((((double)SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS)/SERVER_KNOBS->VERSIONS_PER_SECOND) + 2.0); + double limitTPS = std::min(actualTPS * maxBytesPerSecond / std::max(1.0e-8, inputRate), maxBytesPerSecond * SERVER_KNOBS->MAX_TRANSACTIONS_PER_BYTE); + if (ss.limitReason == limitReason_t::unlimited) + ss.limitReason = limitReason_t::storage_server_write_bandwidth_mvcc; + + if (targetRateRatio > 0 && inputRate > 0) { + ASSERT(inputRate != 0); + double smoothedRate = std::max( ss.verySmoothDurableBytes.smoothRate(), actualTPS / SERVER_KNOBS->MAX_TRANSACTIONS_PER_BYTE ); + double x = smoothedRate / (inputRate * targetRateRatio); + double lim = actualTPS * x; + if (lim < limitTPS) { + limitTPS = lim; + if (ss.limitReason == limitReason_t::unlimited || ss.limitReason == limitReason_t::storage_server_write_bandwidth_mvcc) + ss.limitReason = limitReason_t::storage_server_write_queue_size; + } + } + + storageTPSLimitReverseIndex.insert(std::make_pair(limitTPS, &ss)); + + if(limitTPS < self->TPSLimit && (ss.limitReason == limitReason_t::storage_server_min_free_space || ss.limitReason == limitReason_t::storage_server_min_free_space_ratio)) { + reasonID = ss.id; + self->TPSLimit = limitTPS; + limitReason = ss.limitReason; + } + } + + std::set>> ignoredMachines; + for(auto ss = storageTPSLimitReverseIndex.begin(); ss != storageTPSLimitReverseIndex.end() && ss->first < self->TPSLimit; ++ss) { + if(ignoredMachines.size() < std::min(self->configuration.storageTeamSize - 1, SERVER_KNOBS->MAX_MACHINES_FALLING_BEHIND)) { + ignoredMachines.insert(ss->second->locality.zoneId()); + continue; + } + if(ignoredMachines.count(ss->second->locality.zoneId()) > 0) { + continue; + } + + limitingStorageQueueStorageServer = ss->second->lastReply.bytesInput - ss->second->smoothDurableBytes.smoothTotal(); + self->TPSLimit = ss->first; + limitReason = storageTPSLimitReverseIndex.begin()->second->limitReason; + reasonID = storageTPSLimitReverseIndex.begin()->second->id; // Although we aren't controlling based on the worst SS, we still report it as the limiting process + + break; + } + + double writeToReadLatencyLimit = 0; + Version worstVersionLag = 0; + Version limitingVersionLag = 0; + + { + Version minSSVer = std::numeric_limits::max(); + Version minLimitingSSVer = std::numeric_limits::max(); + for(auto i = self->storageQueueInfo.begin(); i != self->storageQueueInfo.end(); ++i) { + auto& ss = i->value; + if (!ss.valid) continue; + + minSSVer = std::min(minSSVer, ss.lastReply.v); + + // Machines that ratekeeper isn't controlling can fall arbitrarily far behind + if(ignoredMachines.count(i->value.locality.zoneId()) == 0) { + minLimitingSSVer = std::min(minLimitingSSVer, ss.lastReply.v); + } + } + + Version maxTLVer = std::numeric_limits::min(); + for(auto i = self->tlogQueueInfo.begin(); i != self->tlogQueueInfo.end(); ++i) { + auto& tl = i->value; + if (!tl.valid) continue; + maxTLVer = std::max(maxTLVer, tl.lastReply.v); + } + + // writeToReadLatencyLimit: 0 = infinte speed; 1 = TL durable speed ; 2 = half TL durable speed + writeToReadLatencyLimit = ((maxTLVer - minLimitingSSVer) - SERVER_KNOBS->MAX_TL_SS_VERSION_DIFFERENCE/2) / (SERVER_KNOBS->MAX_TL_SS_VERSION_DIFFERENCE/4); + worstVersionLag = std::max((Version)0, maxTLVer - minSSVer); + limitingVersionLag = std::max((Version)0, maxTLVer - minLimitingSSVer); + } + + int64_t worstFreeSpaceTLog = std::numeric_limits::max(); + int64_t worstStorageQueueTLog = 0; + int tlcount = 0; + for(auto i = self->tlogQueueInfo.begin(); i != self->tlogQueueInfo.end(); ++i) { + auto& tl = i->value; + if (!tl.valid) continue; + ++tlcount; + + limitReason_t tlogLimitReason = limitReason_t::log_server_write_queue; + + int64_t minFreeSpace = std::max( SERVER_KNOBS->MIN_FREE_SPACE, (int64_t)(SERVER_KNOBS->MIN_FREE_SPACE_RATIO * tl.smoothTotalSpace.smoothTotal())); + + worstFreeSpaceTLog = std::min(worstFreeSpaceTLog, (int64_t)tl.smoothFreeSpace.smoothTotal() - minFreeSpace); + + int64_t springBytes = std::max(1, std::min(SERVER_KNOBS->SPRING_BYTES_TLOG, (tl.smoothFreeSpace.smoothTotal() - minFreeSpace) * 0.2)); + int64_t targetBytes = std::max(1, std::min(SERVER_KNOBS->TARGET_BYTES_PER_TLOG, (int64_t)tl.smoothFreeSpace.smoothTotal() - minFreeSpace)); + if (targetBytes != SERVER_KNOBS->TARGET_BYTES_PER_TLOG) { + if (minFreeSpace == SERVER_KNOBS->MIN_FREE_SPACE) { + tlogLimitReason = limitReason_t::log_server_min_free_space; + } else { + tlogLimitReason = limitReason_t::log_server_min_free_space_ratio; + } + } + + int64_t queue = tl.lastReply.bytesInput - tl.smoothDurableBytes.smoothTotal(); + int64_t b = queue - targetBytes; + worstStorageQueueTLog = std::max(worstStorageQueueTLog, queue); + + if( tl.lastReply.bytesInput - tl.lastReply.bytesDurable > tl.lastReply.storageBytes.free - minFreeSpace / 2 ) { + if(now() - self->lastWarning > 5.0) { + self->lastWarning = now(); + TraceEvent(SevWarnAlways, "RkTlogMinFreeSpaceZero").detail("reasonId", tl.id); + } + reasonID = tl.id; + limitReason = limitReason_t::log_server_min_free_space; + self->TPSLimit = 0.0; + } + + double targetRateRatio = std::min( ( b + springBytes ) / (double)springBytes, 2.0 ); + + if (writeToReadLatencyLimit > targetRateRatio){ + targetRateRatio = writeToReadLatencyLimit; + tlogLimitReason = limitReason_t::storage_server_readable_behind; + } + + double inputRate = tl.smoothInputBytes.smoothRate(); + + if (targetRateRatio > 0) { + double smoothedRate = std::max( tl.verySmoothDurableBytes.smoothRate(), actualTPS / SERVER_KNOBS->MAX_TRANSACTIONS_PER_BYTE ); + double x = smoothedRate / (inputRate * targetRateRatio); + if (targetRateRatio < .75) //< FIXME: KNOB for 2.0 + x = std::max(x, 0.95); + double lim = actualTPS * x; + if (lim < self->TPSLimit){ + self->TPSLimit = lim; + reasonID = tl.id; + limitReason = tlogLimitReason; + } + } + if (inputRate > 0) { + // Don't let any tlogs use up its target bytes faster than its MVCC window! + double x = ((targetBytes - springBytes) / ((((double)SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS)/SERVER_KNOBS->VERSIONS_PER_SECOND) + 2.0)) / inputRate; + double lim = actualTPS * x; + if (lim < self->TPSLimit){ + self->TPSLimit = lim; + reasonID = tl.id; + limitReason = limitReason_t::log_server_mvcc_write_bandwidth; + } + } + } + + self->TPSLimit = std::max(self->TPSLimit, 0.0); + + if(g_network->isSimulated() && g_simulator.speedUpSimulation) { + self->TPSLimit = std::max(self->TPSLimit, 100.0); + } + + int64_t totalDiskUsageBytes = 0; + for(auto & t : self->tlogQueueInfo) + if (t.value.valid) + totalDiskUsageBytes += t.value.lastReply.storageBytes.used; + for(auto & s : self->storageQueueInfo) + if (s.value.valid) + totalDiskUsageBytes += s.value.lastReply.storageBytes.used; + + self->tpsLimitMetric = std::min(self->TPSLimit, 1e6); + self->reasonMetric = limitReason; + + if( self->smoothReleasedTransactions.smoothRate() > SERVER_KNOBS->LAST_LIMITED_RATIO * self->TPSLimit ) { + (*self->lastLimited) = now(); + } + + if (g_random->random01() < 0.1){ + TraceEvent("RkUpdate") + .detail("TPSLimit", self->TPSLimit) + .detail("Reason", limitReason) + .detail("ReasonServerID", reasonID) + .detail("ReleasedTPS", self->smoothReleasedTransactions.smoothRate()) + .detail("StorageServers", sscount) + .detail("Proxies", self->proxy_transactionCountAndTime.size()) + .detail("TLogs", tlcount) + .detail("ReadReplyRate", readReplyRateSum) + .detail("WorstFreeSpaceStorageServer", worstFreeSpaceStorageServer) + .detail("WorstFreeSpaceTLog", worstFreeSpaceTLog) + .detail("WorstStorageServerQueue", worstStorageQueueStorageServer) + .detail("LimitingStorageServerQueue", limitingStorageQueueStorageServer) + .detail("WorstTLogQueue", worstStorageQueueTLog) + .detail("TotalDiskUsageBytes", totalDiskUsageBytes) + .detail("WorstStorageServerVersionLag", worstVersionLag) + .detail("LimitingStorageServerVersionLag", limitingVersionLag) + .trackLatest(format("%s/RkUpdate", printable(self->dbName).c_str() ).c_str()); + } +} + +ACTOR Future rateKeeper( + Reference> dbInfo, + PromiseStream< std::pair> > serverChanges, + FutureStream< struct GetRateInfoRequest > getRateInfo, + Standalone dbName, + DatabaseConfiguration configuration, + double* lastLimited) +{ + state Ratekeeper self; + state Future track = trackEachStorageServer( &self, serverChanges.getFuture() ); + state Future timeout = Void(); + state std::vector> actors; + state std::vector> tlogTrackers; + state std::vector tlogInterfs; + state Promise err; + self.dbName = dbName; + self.configuration = configuration; + self.lastLimited = lastLimited; + + TraceEvent("RkTLogQueueSizeParameters").detail("Target", SERVER_KNOBS->TARGET_BYTES_PER_TLOG).detail("Spring", SERVER_KNOBS->SPRING_BYTES_TLOG) + .detail("Rate", (SERVER_KNOBS->TARGET_BYTES_PER_TLOG - SERVER_KNOBS->SPRING_BYTES_TLOG) / ((((double)SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS) / SERVER_KNOBS->VERSIONS_PER_SECOND) + 2.0)); + + TraceEvent("RkStorageServerQueueSizeParameters").detail("Target", SERVER_KNOBS->TARGET_BYTES_PER_STORAGE_SERVER).detail("Spring", SERVER_KNOBS->SPRING_BYTES_STORAGE_SERVER).detail("EBrake", SERVER_KNOBS->STORAGE_HARD_LIMIT_BYTES) + .detail("Rate", (SERVER_KNOBS->TARGET_BYTES_PER_STORAGE_SERVER - SERVER_KNOBS->SPRING_BYTES_STORAGE_SERVER) / ((((double)SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS) / SERVER_KNOBS->VERSIONS_PER_SECOND) + 2.0)); + + tlogInterfs = dbInfo->get().logSystemConfig.allPresentLogs(); + for( int i = 0; i < tlogInterfs.size(); i++ ) + tlogTrackers.push_back( splitError( trackTLogQueueInfo(&self, tlogInterfs[i]), err ) ); + + loop{ + choose { + when (Void _ = wait( track )) { break; } + when (Void _ = wait( timeout )) { + updateRate( &self ); + double tooOld = now() - 1.0; + for(auto p=self.proxy_transactionCountAndTime.begin(); p!=self.proxy_transactionCountAndTime.end(); ) { + if (p->second.second < tooOld) + p = self.proxy_transactionCountAndTime.erase(p); + else + ++p; + } + timeout = delayJittered(SERVER_KNOBS->METRIC_UPDATE_RATE); + } + when (GetRateInfoRequest req = waitNext(getRateInfo)) { + GetRateInfoReply reply; + + auto& p = self.proxy_transactionCountAndTime[ req.requesterID ]; + //TraceEvent("RKMPU", req.requesterID).detail("TRT", req.totalReleasedTransactions).detail("Last", p.first).detail("Delta", req.totalReleasedTransactions - p.first); + if (p.first > 0) + self.smoothReleasedTransactions.addDelta( req.totalReleasedTransactions - p.first ); + + p.first = req.totalReleasedTransactions; + p.second = now(); + + reply.transactionRate = self.TPSLimit / self.proxy_transactionCountAndTime.size(); + reply.leaseDuration = SERVER_KNOBS->METRIC_UPDATE_RATE; + req.reply.send( reply ); + } + when (Void _ = wait(err.getFuture())) {} + when (Void _ = wait(dbInfo->onChange())) { + if( tlogInterfs != dbInfo->get().logSystemConfig.allPresentLogs() ) { + tlogInterfs = dbInfo->get().logSystemConfig.allPresentLogs(); + tlogTrackers = std::vector>(); + for( int i = 0; i < tlogInterfs.size(); i++ ) + tlogTrackers.push_back( splitError( trackTLogQueueInfo(&self, tlogInterfs[i]), err ) ); + } + } + } + } + return Void(); +} diff --git a/fdbserver/Ratekeeper.h b/fdbserver/Ratekeeper.h new file mode 100644 index 0000000000..03441daf86 --- /dev/null +++ b/fdbserver/Ratekeeper.h @@ -0,0 +1,37 @@ +/* + * Ratekeeper.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_RATEKEEPER_H +#define FDBSERVER_RATEKEEPER_H +#pragma once + +#include "MasterInterface.h" +#include "TLogInterface.h" +#include "DatabaseConfiguration.h" + +Future rateKeeper( + Reference> const& dbInfo, + PromiseStream< std::pair> > const& serverChanges, // actually an input, but we don't want broken_promise + FutureStream< struct GetRateInfoRequest > const& getRateInfo, + Standalone const& dbName, + DatabaseConfiguration const& configuration, + double* const& lastLimited); + +#endif \ No newline at end of file diff --git a/fdbserver/RecoveryState.h b/fdbserver/RecoveryState.h new file mode 100644 index 0000000000..b74d22c352 --- /dev/null +++ b/fdbserver/RecoveryState.h @@ -0,0 +1,54 @@ +/* + * RecoveryState.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_RECOVERYSTATE_H +#define FDBSERVER_RECOVERYSTATE_H +#pragma once + +// RecoveryState and RecoveryStatus should probably be merged. The former is passed through ServerDBInfo and used for "real" decisions in the system; the latter +// is slightly more detailed and is used by the status infrastructure. But I'm scared to make changes to the former so close to 1.0 release, so I'm making the latter. + +namespace RecoveryState { + enum RecoveryState { READING_CSTATE = 1, LOCKING_CSTATE = 2, RECRUITING = 3, RECOVERY_TRANSACTION = 4, WRITING_CSTATE = 5, FULLY_RECOVERED = 6 }; +}; + +namespace RecoveryStatus { + enum RecoveryStatus { + reading_coordinated_state, + locking_coordinated_state, + locking_old_transaction_servers, + reading_transaction_system_state, + configuration_missing, + configuration_never_created, + configuration_invalid, + recruiting_transaction_servers, + initializing_transaction_servers, + recovery_transaction, + writing_coordinated_state, + fully_recovered, + END + }; + + // in Status.actor.cpp + extern const char* names[]; + extern const char* descriptions[]; +}; + +#endif \ No newline at end of file diff --git a/fdbserver/Resolver.actor.cpp b/fdbserver/Resolver.actor.cpp new file mode 100644 index 0000000000..8fbcf51be7 --- /dev/null +++ b/fdbserver/Resolver.actor.cpp @@ -0,0 +1,319 @@ +/* + * Resolver.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/ActorCollection.h" +#include "fdbclient/NativeAPI.h" +#include "ResolverInterface.h" +#include "MasterInterface.h" +#include "WorkerInterface.h" +#include "WaitFailure.h" +#include "Knobs.h" +#include "ServerDBInfo.h" +#include "Orderer.actor.h" +#include "ConflictSet.h" +#include "StorageMetrics.h" +#include "fdbclient/SystemData.h" + +namespace { +struct ProxyRequestsInfo { + std::map outstandingBatches; + Version lastVersion; + + ProxyRequestsInfo() : lastVersion(-1) {} +}; +} + +namespace{ +struct Resolver : ReferenceCounted { + Resolver( UID dbgid, int proxyCount, int resolverCount ) + : dbgid(dbgid), proxyCount(proxyCount), resolverCount(resolverCount), version(-1), conflictSet( newConflictSet() ), iopsSample( SERVER_KNOBS->IOPS_UNITS_PER_SAMPLE ), debugMinRecentStateVersion(0) + { + } + ~Resolver() { + destroyConflictSet( conflictSet ); + } + + UID dbgid; + int proxyCount, resolverCount; + NotifiedVersion version; + AsyncVar neededVersion; + + Map>> recentStateTransactions; + Deque> recentStateTransactionSizes; + AsyncVar totalStateBytes; + AsyncTrigger checkNeededVersion; + std::map proxyInfoMap; + ConflictSet *conflictSet; + TransientStorageMetricSample iopsSample; + + Version debugMinRecentStateVersion; +}; +} + +ACTOR Future resolveBatch( + Reference self, + ResolveTransactionBatchRequest req) +{ + state Optional debugID; + + // The first request (prevVersion < 0) comes from the master + state NetworkAddress proxyAddress = req.prevVersion >= 0 ? req.reply.getEndpoint().address : NetworkAddress(); + state ProxyRequestsInfo &proxyInfo = self->proxyInfoMap[proxyAddress]; + + if(req.debugID.present()) { + debugID = g_nondeterministic_random->randomUniqueID(); + g_traceBatch.addAttach("CommitAttachID", req.debugID.get().first(), debugID.get().first()); + g_traceBatch.addEvent("CommitDebug",debugID.get().first(),"Resolver.resolveBatch.Before"); + } + + /*TraceEvent("ResolveBatchStart", self->dbgid).detail("From", proxyAddress).detail("Version", req.version).detail("PrevVersion", req.prevVersion).detail("StateTransactions", req.txnStateTransactions.size()) + .detail("RecentStateTransactions", self->recentStateTransactionSizes.size()).detail("LastVersion", proxyInfo.lastVersion).detail("FirstVersion", self->recentStateTransactionSizes.empty() ? -1 : self->recentStateTransactionSizes.front().first) + .detail("ResolverVersion", self->version.get());*/ + + while( self->totalStateBytes.get() > SERVER_KNOBS->RESOLVER_STATE_MEMORY_LIMIT && self->recentStateTransactionSizes.size() && + proxyInfo.lastVersion > self->recentStateTransactionSizes.front().first && req.version > self->neededVersion.get() ) { + /*TraceEvent("ResolveBatchDelay").detail("From", proxyAddress).detail("stateBytes", self->totalStateBytes.get()).detail("RecentStateTransactionSize", self->recentStateTransactionSizes.size()) + .detail("lastVersion", proxyInfo.lastVersion).detail("RequestVersion", req.version).detail("NeededVersion", self->neededVersion.get()) + .detail("recentStateVer", self->recentStateTransactions.begin()->key);*/ + + Void _ = wait( self->totalStateBytes.onChange() || self->neededVersion.onChange() ); + } + + if(debugID.present()) { + g_traceBatch.addEvent("CommitDebug",debugID.get().first(),"Resolver.resolveBatch.AfterQueueSizeCheck"); + } + + loop { + if( self->recentStateTransactionSizes.size() && proxyInfo.lastVersion <= self->recentStateTransactionSizes.front().first ) { + self->neededVersion.set( std::max(self->neededVersion.get(), req.prevVersion) ); + } + + choose { + when(Void _ = wait(self->version.whenAtLeast(req.prevVersion))) { + break; + } + when(Void _ = wait(self->checkNeededVersion.onTrigger())) { } + } + } + + if (check_yield(TaskDefaultEndpoint)) { + Void _ = wait( delay( 0, TaskLowPriority ) || delay( SERVER_KNOBS->COMMIT_SLEEP_TIME ) ); // FIXME: Is this still right? + g_network->setCurrentTask(TaskDefaultEndpoint); + } + + if (self->version.get() == req.prevVersion) { // Not a duplicate (check relies on no waiting between here and self->version.set() below!) + if(proxyInfo.lastVersion > 0) { + proxyInfo.outstandingBatches.erase(proxyInfo.outstandingBatches.begin(), proxyInfo.outstandingBatches.upper_bound(req.lastReceivedVersion)); + } + + Version firstUnseenVersion = proxyInfo.lastVersion + 1; + proxyInfo.lastVersion = req.version; + + if(req.debugID.present()) + g_traceBatch.addEvent("CommitDebug", debugID.get().first(), "Resolver.resolveBatch.AfterOrderer"); + + vector commitList; + vector tooOldList; + double commitTime = now(); + + // Detect conflicts + double expire = now() + SERVER_KNOBS->SAMPLE_EXPIRATION_TIME; + double tstart = timer(); + ConflictBatch conflictBatch( self->conflictSet ); + int keys = 0; + for(int t=0; tresolverCount > 1) { + for(auto it : req.transactions[t].write_conflict_ranges) + self->iopsSample.addAndExpire( it.begin, SERVER_KNOBS->SAMPLE_OFFSET_PER_KEY + it.begin.size(), expire ); + for(auto it : req.transactions[t].read_conflict_ranges) + self->iopsSample.addAndExpire( it.begin, SERVER_KNOBS->SAMPLE_OFFSET_PER_KEY + it.begin.size(), expire ); + } + } + conflictBatch.detectConflicts( req.version, req.version - SERVER_KNOBS->MAX_WRITE_TRANSACTION_LIFE_VERSIONS, commitList, &tooOldList); + g_counters.conflictTime += timer() - tstart; + ++g_counters.conflictBatches; + g_counters.conflictTransactions += req.transactions.size(); + g_counters.conflictKeys += keys; + + ResolveTransactionBatchReply &reply = proxyInfo.outstandingBatches[req.version]; + reply.debugID = req.debugID; + reply.committed.resize( reply.arena, req.transactions.size() ); + for(int c=0; c= 0 || req.txnStateTransactions.size() == 0); // The master's request should not have any state transactions + + auto& stateTransactions = self->recentStateTransactions[ req.version ]; + int64_t stateBytes = 0; + for(int t : req.txnStateTransactions) { + stateBytes += req.transactions[t].mutations.expectedSize(); + stateTransactions.push_back_deep(stateTransactions.arena(), StateTransactionRef(reply.committed[t] == ConflictBatch::TransactionCommitted, req.transactions[t].mutations)); + } + + if(stateBytes > 0) + self->recentStateTransactionSizes.push_back(std::make_pair(req.version, stateBytes)); + + ASSERT(req.version >= firstUnseenVersion); + ASSERT(firstUnseenVersion >= self->debugMinRecentStateVersion); + + TEST(firstUnseenVersion == req.version); // Resolver first unseen version is current version + + auto stateTransactionItr = self->recentStateTransactions.lower_bound(firstUnseenVersion); + auto endItr = self->recentStateTransactions.lower_bound(req.version); + for(; stateTransactionItr != endItr; ++stateTransactionItr) { + reply.stateMutations.push_back( reply.arena, stateTransactionItr->value); + reply.arena.dependsOn( stateTransactionItr->value.arena() ); + } + + //TraceEvent("ResolveBatch", self->dbgid).detail("PrevVersion", req.prevVersion).detail("Version", req.version).detail("StateTransactionVersions", self->recentStateTransactionSizes.size()).detail("StateBytes", stateBytes).detail("FirstVersion", self->recentStateTransactionSizes.empty() ? -1 : self->recentStateTransactionSizes.front().first).detail("StateMutationsIn", req.txnStateTransactions.size()).detail("StateMutationsOut", reply.stateMutations.size()).detail("From", proxyAddress); + + ASSERT(!proxyInfo.outstandingBatches.empty()); + ASSERT(self->proxyInfoMap.size() <= self->proxyCount+1); + + // SOMEDAY: This is O(n) in number of proxies. O(log n) solution using appropriate data structure? + Version oldestProxyVersion = req.version; + for(auto itr = self->proxyInfoMap.begin(); itr != self->proxyInfoMap.end(); ++itr) { + //TraceEvent("ResolveBatchProxyVersion", self->dbgid).detail("Proxy", itr->first).detail("Version", itr->second.lastVersion); + if(itr->first.isValid()) { // Don't consider the first master request + oldestProxyVersion = std::min(itr->second.lastVersion, oldestProxyVersion); + } + else { + // The master's request version should never prevent us from clearing recentStateTransactions + ASSERT(self->debugMinRecentStateVersion == 0 || self->debugMinRecentStateVersion > itr->second.lastVersion); + } + } + + TEST(oldestProxyVersion == req.version); // The proxy that sent this request has the oldest current version + TEST(oldestProxyVersion != req.version); // The proxy that sent this request does not have the oldest current version + + bool anyPopped = false; + if(firstUnseenVersion <= oldestProxyVersion && self->proxyInfoMap.size() == self->proxyCount+1) { + TEST(true); // Deleting old state transactions + self->recentStateTransactions.erase( self->recentStateTransactions.begin(), self->recentStateTransactions.upper_bound( oldestProxyVersion ) ); + self->debugMinRecentStateVersion = oldestProxyVersion + 1; + + while(self->recentStateTransactionSizes.size() && self->recentStateTransactionSizes.front().first <= oldestProxyVersion) { + anyPopped = true; + stateBytes -= self->recentStateTransactionSizes.front().second; + self->recentStateTransactionSizes.pop_front(); + } + } + + self->version.set( req.version ); + bool breachedLimit = self->totalStateBytes.get() <= SERVER_KNOBS->RESOLVER_STATE_MEMORY_LIMIT && self->totalStateBytes.get() + stateBytes > SERVER_KNOBS->RESOLVER_STATE_MEMORY_LIMIT; + self->totalStateBytes.setUnconditional(self->totalStateBytes.get() + stateBytes); + if(anyPopped || breachedLimit) { + self->checkNeededVersion.trigger(); + } + + if(req.debugID.present()) + g_traceBatch.addEvent("CommitDebug", debugID.get().first(), "Resolver.resolveBatch.After"); + } + else { + TEST(true); // Duplicate resolve batch request + //TraceEvent("DupResolveBatchReq", self->dbgid).detail("From", proxyAddress); + } + + auto proxyInfoItr = self->proxyInfoMap.find(proxyAddress); + + if(proxyInfoItr != self->proxyInfoMap.end()) { + auto batchItr = proxyInfoItr->second.outstandingBatches.find(req.version); + if(batchItr != proxyInfoItr->second.outstandingBatches.end()) { + req.reply.send(batchItr->second); + } + else { + TEST(true); // No outstanding batches for version on proxy + req.reply.send(Never()); + } + } + else { + ASSERT_WE_THINK(false); // The first non-duplicate request with this proxyAddress, including this one, should have inserted this item in the map! + //TEST(true); // No prior proxy requests + req.reply.send(Never()); + } + + return Void(); +} + +ACTOR Future resolverCore( + ResolverInterface resolver, + InitializeResolverRequest initReq) +{ + state Reference self( new Resolver(resolver.id(), initReq.proxyCount, initReq.resolverCount) ); + state ActorCollection actors(false); + state Future doPollMetrics = self->resolverCount > 1 ? Void() : Future(Never()); + actors.add( waitFailureServer(resolver.waitFailure.getFuture()) ); + + TraceEvent("ResolverInit", resolver.id()).detail("RecoveryCount", initReq.recoveryCount); + loop choose { + when ( ResolveTransactionBatchRequest batch = waitNext( resolver.resolve.getFuture() ) ) { + actors.add( resolveBatch(self, batch) ); + } + when ( ResolutionMetricsRequest req = waitNext( resolver.metrics.getFuture() ) ) { + req.reply.send(self->iopsSample.getEstimate(allKeys)); + } + when ( ResolutionSplitRequest req = waitNext( resolver.split.getFuture() ) ) { + ResolutionSplitReply rep; + rep.key = self->iopsSample.splitEstimate(req.range, req.offset, req.front); + rep.used = self->iopsSample.getEstimate(req.front ? KeyRangeRef(req.range.begin, rep.key) : KeyRangeRef(rep.key, req.range.end)); + req.reply.send(rep); + } + when ( Void _ = wait( actors.getResult() ) ) {} + when (Void _ = wait(doPollMetrics) ) { + self->iopsSample.poll(); + doPollMetrics = delay(SERVER_KNOBS->SAMPLE_POLL_TIME); + } + } +} + +ACTOR Future checkRemoved( Reference> db, uint64_t recoveryCount, ResolverInterface myInterface ) { + loop { + if ( db->get().recoveryCount >= recoveryCount && !std::count(db->get().resolvers.begin(), db->get().resolvers.end(), myInterface) ) + throw worker_removed(); + Void _ = wait( db->onChange() ); + } +} + +ACTOR Future resolver( + ResolverInterface resolver, + InitializeResolverRequest initReq, + Reference> db ) +{ + try { + state Future core = resolverCore( resolver, initReq ); + loop choose { + when( Void _ = wait( core ) ) { return Void(); } + when( Void _ = wait( checkRemoved( db, initReq.recoveryCount, resolver ) ) ) {} + } + } catch (Error& e) { + if (e.code() == error_code_actor_cancelled || e.code() == error_code_worker_removed) { + TraceEvent("ResolverTerminated", resolver.id()).error(e,true); + return Void(); + } + throw; + } +} diff --git a/fdbserver/ResolverInterface.h b/fdbserver/ResolverInterface.h new file mode 100644 index 0000000000..784c8b4263 --- /dev/null +++ b/fdbserver/ResolverInterface.h @@ -0,0 +1,131 @@ +/* + * ResolverInterface.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_RESOLVERINTERFACE_H +#define FDBSERVER_RESOLVERINTERFACE_H +#pragma once + +#include "fdbclient/FDBTypes.h" + +struct ResolverInterface { + enum { LocationAwareLoadBalance = 1 }; + LocalityData locality; + UID uniqueID; + RequestStream< struct ResolveTransactionBatchRequest > resolve; + RequestStream< struct ResolutionMetricsRequest > metrics; + RequestStream< struct ResolutionSplitRequest > split; + + RequestStream> waitFailure; + + ResolverInterface() : uniqueID( g_random->randomUniqueID() ) {} + UID id() const { return uniqueID; } + std::string toString() const { return id().shortString(); } + bool operator == ( ResolverInterface const& r ) const { return id() == r.id(); } + bool operator != ( ResolverInterface const& r ) const { return id() != r.id(); } + NetworkAddress address() const { return resolve.getEndpoint().address; } + void initEndpoints() { + metrics.getEndpoint( TaskResolutionMetrics ); + split.getEndpoint( TaskResolutionMetrics ); + } + + template + void serialize( Ar& ar ) { + ar & uniqueID & locality & resolve & metrics & split & waitFailure; + } +}; + +struct StateTransactionRef { + StateTransactionRef() {} + StateTransactionRef(const bool committed, VectorRef const& mutations) : committed(committed), mutations(mutations) {} + StateTransactionRef(Arena &p, const StateTransactionRef &toCopy) : committed(toCopy.committed), mutations(p, toCopy.mutations) {} + bool committed; + VectorRef mutations; + size_t expectedSize() const { + return mutations.expectedSize(); + } + + template + void serialize(Archive& ar) { + ar & committed & mutations; + } +}; + +struct ResolveTransactionBatchReply { + Arena arena; + VectorRef committed; + Optional debugID; + VectorRef> stateMutations; // [version][transaction#] -> (committed, [mutation#]) + + template + void serialize(Archive& ar) { + ar & committed & stateMutations & arena & debugID; + } + +}; + +struct ResolveTransactionBatchRequest { + Arena arena; + + Version prevVersion; + Version version; // FIXME: ? + Version lastReceivedVersion; + VectorRef transactions; + VectorRef txnStateTransactions; // Offsets of elements of transactions that have (transaction subsystem state) mutations + ReplyPromise reply; + Optional debugID; + + template + void serialize(Archive& ar) { + ar & prevVersion & version & lastReceivedVersion & transactions & txnStateTransactions & reply & arena & debugID; + } +}; + +struct ResolutionMetricsRequest { + ReplyPromise reply; + + template + void serialize(Archive& ar) { + ar & reply; + } +}; + +struct ResolutionSplitReply { + Key key; + int64_t used; + template + void serialize(Archive& ar) { + ar & key & used; + } + +}; + +struct ResolutionSplitRequest { + KeyRange range; + int64_t offset; + bool front; + ReplyPromise reply; + + template + void serialize(Archive& ar) { + ar & range & offset & front & reply; + } +}; + +#endif diff --git a/fdbserver/ServerDBInfo.h b/fdbserver/ServerDBInfo.h new file mode 100644 index 0000000000..c8edce8e20 --- /dev/null +++ b/fdbserver/ServerDBInfo.h @@ -0,0 +1,59 @@ +/* + * ServerDBInfo.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_SERVERDBINFO_H +#define FDBSERVER_SERVERDBINFO_H +#pragma once + +#include "ClusterRecruitmentInterface.h" +#include "MasterInterface.h" +#include "LogSystemConfig.h" + +struct ServerDBInfo { + // This structure contains transient information which is broadcast to all workers for a database, + // permitting them to communicate with each other. It is not available to the client. This mechanism + // (see GetServerDBInfoRequest) is closely parallel to OpenDatabaseRequest for the client. + + UID id; // Changes each time any other member changes + ClusterControllerFullInterface clusterInterface; + ClientDBInfo client; // After a successful recovery, eventually proxies that communicate with it + MasterInterface master; // The best guess as to the most recent master, which might still be recovering + vector resolvers; + Standalone dbName; + DBRecoveryCount recoveryCount; // A recovery count from DBCoreState. A successful master recovery increments it twice; unsuccessful recoveries may increment it once. Depending on where the current master is in its recovery process, this might not have been written by the current master. + int recoveryState; + LifetimeToken masterLifetime; // Used by masterserver to detect not being the currently chosen master + LocalityData myLocality; // (Not serialized) Locality information, if available, for the *local* process + LogSystemConfig logSystemConfig; + std::vector priorCommittedLogServers; // If !fullyRecovered and logSystemConfig refers to a new log system which may not have been committed to the coordinated state yet, then priorCommittedLogServers are the previous, fully committed generation which need to stay alive in case this recovery fails + + ServerDBInfo() : recoveryCount(0), recoveryState(0) {} + explicit ServerDBInfo(StringRef const& dbName) : dbName(dbName), recoveryCount(0), recoveryState(0) {} + + bool operator == (ServerDBInfo const& r) const { return id == r.id; } + bool operator != (ServerDBInfo const& r) const { return id != r.id; } + + template + void serialize( Ar& ar ) { + ar & id & clusterInterface & client & master & resolvers & dbName & recoveryCount & masterLifetime & logSystemConfig & priorCommittedLogServers & recoveryState; + } +}; + +#endif diff --git a/fdbserver/SimulatedCluster.actor.cpp b/fdbserver/SimulatedCluster.actor.cpp new file mode 100644 index 0000000000..0103e94aac --- /dev/null +++ b/fdbserver/SimulatedCluster.actor.cpp @@ -0,0 +1,953 @@ +/* + * SimulatedCluster.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "flow/actorcompiler.h" +#include "fdbrpc/simulator.h" +#include "fdbclient/FailureMonitorClient.h" +#include "fdbclient/DatabaseContext.h" +#include "TesterInterface.h" +#include "WorkerInterface.h" +#include "fdbclient/ClusterInterface.h" +#include "Knobs.h" +#include "ClusterRecruitmentInterface.h" +#include "fdbserver/CoordinationInterface.h" +#include "fdbmonitor/SimpleIni.h" +#include "fdbrpc/AsyncFileNonDurable.actor.h" +#include "fdbrpc/TLSConnection.h" +#include "fdbclient/ManagementAPI.h" +#include "fdbclient/NativeAPI.h" +#include "fdbclient/BackupAgent.h" + +#ifndef WIN32 +#include "versions.h" +#endif + +#undef max +#undef min + +extern bool buggifyActivated; +extern "C" int g_expect_full_pointermap; +extern const char* getHGVersion(); + +const int PROCESS_START_TIME = 4; +const int MACHINE_REBOOT_TIME = 10; + +bool destructed = false; + +static const char* certBytes = + "-----BEGIN CERTIFICATE-----\n" + "MIIEGzCCAwOgAwIBAgIJANUQj1rRA2XMMA0GCSqGSIb3DQEBBQUAMIGjMQswCQYD\n" + "VQQGEwJVUzELMAkGA1UECAwCVkExDzANBgNVBAcMBlZpZW5uYTEaMBgGA1UECgwR\n" + "Rm91bmRhdGlvbkRCLCBMTEMxGTAXBgNVBAsMEFRlc3QgZW5naW5lZXJpbmcxFTAT\n" + "BgNVBAMMDE1yLiBCaWcgVHVuYTEoMCYGCSqGSIb3DQEJARYZYmlnLnR1bmFAZm91\n" + "bmRhdGlvbmRiLmNvbTAeFw0xNDEyMDUxNTEyMjFaFw0yNDEyMDIxNTEyMjFaMIGj\n" + "MQswCQYDVQQGEwJVUzELMAkGA1UECAwCVkExDzANBgNVBAcMBlZpZW5uYTEaMBgG\n" + "A1UECgwRRm91bmRhdGlvbkRCLCBMTEMxGTAXBgNVBAsMEFRlc3QgZW5naW5lZXJp\n" + "bmcxFTATBgNVBAMMDE1yLiBCaWcgVHVuYTEoMCYGCSqGSIb3DQEJARYZYmlnLnR1\n" + "bmFAZm91bmRhdGlvbmRiLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC\n" + "ggEBAKZTL2edDkiet4HBTZnjysn6gOVZH2MP02KVBIv/H7e+3w7ZOIRvcPzhZe9M\n" + "3cGH1t/pkr9DSXvzIb42EffMVlpLD2VQn2H8VC2QSdJCIQcf802u+Taf+XtW6K1h\n" + "p/YPL1uhdopUs3c1oon8ykKwnOfrQYgv5pUa7jQdMkltI2MQJU3uFq3Z/LHTvIKe\n" + "FN+bqK0iYhZthwMG7Rld4+RgKZoT4u1B6w/duEWk9KLjgs7fTf3Oe6JHCYNqwBJi\n" + "78sJalwXz9Wf8wmMaYSG0XNA7vBOdpTFhVPSsh6e3rkydf5HydMade/II98MWpMe\n" + "hFg7FFMaJP6ig8p5iL+9QP2VMCkCAwEAAaNQME4wHQYDVR0OBBYEFIXGmIcKptBP\n" + "v3i9WS/mK78o5E/MMB8GA1UdIwQYMBaAFIXGmIcKptBPv3i9WS/mK78o5E/MMAwG\n" + "A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAJkVgNGOXT+ZHCNEYLjr/6OM\n" + "UCHvwlMeaEyqxaOmK26J2kAADPhjBZ7lZOHWb2Wzb+BiQUIFGwNIMoRvsg8skpJa\n" + "OCqpVciHVXY/U8BiYY70DKozRza93Ab9om3pySGDJ/akdCjqbMT1Cb7Kloyw+hNh\n" + "XD4MML0lYiUE9KK35xyK6FgTx4A7IXl4b3lWBgglqTh4+P5J1+xy8AYJ0VfPoP7y\n" + "OoZgwAmkpkMnalReNkN7LALHGqMzv/qH04ODlkU/HUGgExtnINMxK9VEDIe/yLGm\n" + "DHy7gcQMj5Hyymack/d4ZF8CSrYpGZQeZGXoxOmTDwWcXgnYA+2o7lOYPb5Uu08=\n" + "-----END CERTIFICATE-----\n" + "-----BEGIN PRIVATE KEY-----\n" + "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCmUy9nnQ5InreB\n" + "wU2Z48rJ+oDlWR9jD9NilQSL/x+3vt8O2TiEb3D84WXvTN3Bh9bf6ZK/Q0l78yG+\n" + "NhH3zFZaSw9lUJ9h/FQtkEnSQiEHH/NNrvk2n/l7VuitYaf2Dy9boXaKVLN3NaKJ\n" + "/MpCsJzn60GIL+aVGu40HTJJbSNjECVN7hat2fyx07yCnhTfm6itImIWbYcDBu0Z\n" + "XePkYCmaE+LtQesP3bhFpPSi44LO3039znuiRwmDasASYu/LCWpcF8/Vn/MJjGmE\n" + "htFzQO7wTnaUxYVT0rIent65MnX+R8nTGnXvyCPfDFqTHoRYOxRTGiT+ooPKeYi/\n" + "vUD9lTApAgMBAAECggEBAIYCmDtfq9aPK0P8v82yX/4FPD2OZV+nrKXNc3BpCuE9\n" + "hPOtyX/LWrol0b/Rqwr3rAWVaIt6Z4bbCuD7J9cEaL8voyP6pbCJYjmj/BbQ+VOI\n" + "Rrzcsid1Fcpu5+JqwK3c5kdp/NzQChmOuXt8lmrNal7iilZ0YdDZdfu/WnkW2mBB\n" + "oQHkujlnWr4PNYdwMOnBU6TwdOuz+inPVMLohOO0Vr585OxPsGzG2Ud3yQ/t34Cq\n" + "F9nmOXQoszftGKsL1yuh/3fGj/O86g/CRsUy05qZhDDBEYQD6qZCvD5+yp8oOWIR\n" + "SljM3GXDBnJqRPhP+Nyf6e6/GoQtfVZ9MPRzDDPzIBECgYEA2kX/zAs6taOiNqCb\n" + "6nVGe7/3uQJz/CkmOSKIFKUu7lCEUjmMYpK3Xzp26RTUR9cT+g9y+cnJO1Vbaxtf\n" + "Qidje6K+Oi1pQyUGQ6W+U8cPJHz43PVa7IB5Az5i/sS2tu0BGhvGo9G6iYQjxXeD\n" + "1197DRACgnm5AORQMum616XvSPMCgYEAwxKbkAzJzfZF6A3Ys+/0kycNfDP8xZoC\n" + "1zV3d1b2JncsdAPCHYSKtpniRrQN9ASa3RMdkh+wrMN/KlbtU9Ddoc4NHxSTFV7F\n" + "wypFMzLZslqkQ6uHnVVewHV7prfoKsMci2c9iHO7W8TEv4aqW8XDd8OozP3/q2j4\n" + "hvL7VIAVqXMCgYEAwAFnfOQ75uBkp00tGlfDgsRhc5vWz3CbMRNRRWfxGq41V+dL\n" + "uMJ7EAfr5ijue6uU5RmF+HkqzUjOvC894oGnn3CPibm8qNX+5q7799JZXa2ZdTVX\n" + "oEd7LAFLL/V3DP77Qy4/1Id/Ycydcu0pSuGw6tK0gnX06fXtHnxAYcaT8UUCgYAE\n" + "MytcP5o8r/ezVlD7Fsh6PpYAvZHMo1M6VPFchWfJTjmLyeTtA8SEx+1iPlAql8rJ\n" + "xbaWRc5k+dSMEdEMQ+vxpuELcUL1a9PwLsHMp2SefWsZ9eB2l7bxh9YAsebyvL6p\n" + "lbBydqNrB2KBCSIz1Z8uveytdS6C/0CSjzqwCA3vVwKBgQDAXqjo3xrzMlHeXm5o\n" + "qH/OjajjqbnPXHolHDitbLubyQ4E6KhMBMxfChBe/8VptB/Gs0efVbMVGuabxY7Q\n" + "iastGId8HyONy3UPGPxCn4b95cIxKvdpt+hvWtYHIBCfHXluQK7zsDMgvtXjYNiz\n" + "peZRikYlwmu1K2YRTf7oLE2Ogw==\n" + "-----END PRIVATE KEY-----\n"; + +template +T simulate( const T& in ) { + BinaryWriter writer(AssumeVersion(currentProtocolVersion)); + writer << in; + BinaryReader reader( writer.getData(), writer.getLength(), AssumeVersion(currentProtocolVersion) ); + T out; + reader >> out; + return out; +} + +static void simInitTLS() { + Reference options( new TLSOptions ); + options->set_cert_data( certBytes ); + options->set_key_data( certBytes ); + options->register_network(); +} + +ACTOR Future runBackup( Reference connFile ) { + state std::vector> agentFutures; + + while (g_simulator.backupAgents == ISimulator::WaitForType) { + Void _ = wait(delay(1.0)); + } + + if (g_simulator.backupAgents == ISimulator::BackupToFile) { + Reference cluster = Cluster::createCluster(connFile, -1); + Database cx = cluster->createDatabase(LiteralStringRef("DB")).get(); + + state FileBackupAgent fileAgent; + state double backupPollDelay = 1.0 / CLIENT_KNOBS->BACKUP_AGGREGATE_POLL_RATE; + agentFutures.push_back(fileAgent.run(cx, &backupPollDelay, CLIENT_KNOBS->SIM_BACKUP_TASKS_PER_AGENT)); + + while (g_simulator.backupAgents == ISimulator::BackupToFile) { + Void _ = wait(delay(1.0)); + } + + for(auto it : agentFutures) { + it.cancel(); + } + } + else if (g_simulator.backupAgents == ISimulator::BackupToDB) { + Reference cluster = Cluster::createCluster(connFile, -1); + Database cx = cluster->createDatabase(LiteralStringRef("DB")).get(); + + Reference extraFile(new ClusterConnectionFile(*g_simulator.extraDB)); + Reference extraCluster = Cluster::createCluster(extraFile, -1); + state Database extraDB = extraCluster->createDatabase(LiteralStringRef("DB")).get(); + + TraceEvent("StartingBackupAgents").detail("connFile", connFile->getConnectionString().toString()).detail("extraString", extraFile->getConnectionString().toString()); + + state DatabaseBackupAgent dbAgent = DatabaseBackupAgent(cx); + state DatabaseBackupAgent extraAgent = DatabaseBackupAgent(extraDB); + + state double dr1PollDelay = 1.0 / CLIENT_KNOBS->BACKUP_AGGREGATE_POLL_RATE; + state double dr2PollDelay = 1.0 / CLIENT_KNOBS->BACKUP_AGGREGATE_POLL_RATE; + + agentFutures.push_back(extraAgent.run(cx, &dr1PollDelay, CLIENT_KNOBS->SIM_BACKUP_TASKS_PER_AGENT)); + agentFutures.push_back(dbAgent.run(extraDB, &dr2PollDelay, CLIENT_KNOBS->SIM_BACKUP_TASKS_PER_AGENT)); + + while (g_simulator.backupAgents == ISimulator::BackupToDB) { + Void _ = wait(delay(1.0)); + } + + TraceEvent("StoppingBackupAgents"); + + for(auto it : agentFutures) { + it.cancel(); + } + } + + Void _= wait(Future(Never())); + throw internal_error(); +} + +// SOMEDAY: when a process can be rebooted in isolation from the other on that machine, +// a loop{} will be needed around the waiting on simulatedFDBD(). For now this simply +// takes care of house-keeping such as context switching and file closing. +ACTOR Future simulatedFDBDRebooter( + Reference connFile, + uint32_t ip, + bool useSSL, + uint16_t port, + LocalityData localities, + ProcessClass processClass, + std::string* dataFolder, + std::string* coordFolder, + std::string baseFolder, + ClusterConnectionString connStr, + bool useSeedFile, + bool runBackupAgents) +{ + state ISimulator::ProcessInfo *simProcess = g_simulator.getCurrentProcess(); + state int cycles =0; + + loop { + auto waitTime = SERVER_KNOBS->MIN_REBOOT_TIME + (SERVER_KNOBS->MAX_REBOOT_TIME - SERVER_KNOBS->MIN_REBOOT_TIME) * g_random->random01(); + cycles ++; + TraceEvent("SimulatedFDBDWait").detail("Cycles", cycles) + .detail("ProcessAddress", NetworkAddress(ip, port, true, false)) + .detailext("ZoneId", localities.zoneId()) + .detail("waitTime", waitTime); + + Void _ = wait( delay( waitTime ) ); + + state ISimulator::ProcessInfo *process = g_simulator.newProcess( "Server", ip, port, localities, processClass, dataFolder->c_str(), coordFolder->c_str() ); + Void _ = wait( g_simulator.onProcess(process, TaskDefaultYield) ); // Now switch execution to the process on which we will run + state Future onShutdown = process->onShutdown(); + + try { + TraceEvent("SimulatedRebooterStarting", localities.zoneId()).detail("Cycles", cycles) + .detailext("ZoneId", localities.zoneId()) + .detailext("DataHall", localities.dataHallId()) + .detail("ProcessAddress", process->address.toString()) + .detail("UsingSSL", useSSL); + TraceEvent("ProgramStart").detail("Cycles", cycles) + .detail("SourceVersion", getHGVersion()) + .detail("Version", FDB_VT_VERSION) + .detail("PackageName", FDB_VT_PACKAGE_NAME) + .detail("DataFolder", *dataFolder) + .detail("ConnectionString", connFile ? connFile->getConnectionString().toString() : "") + .detailf("ActualTime", "%lld", DEBUG_DETERMINISM ? 0 : time(NULL)) + .detail("CommandLine", "fdbserver -r simulation") + .detail("BuggifyEnabled", buggifyActivated) + .detail("Simulated", true) + .trackLatest("ProgramStart"); + + try { + //SOMEDAY: test lower memory limits, without making them too small and causing the database to stop making progress + FlowTransport::createInstance(1); + Sim2FileSystem::newFileSystem(); + simInitTLS(); + NetworkAddress n(ip, port, true, useSSL); + Future listen = FlowTransport::transport().bind( n, n ); + Future fd = fdbd( connFile, localities, processClass, *dataFolder, *coordFolder, 500e6, "", ""); + Future backup = runBackupAgents ? runBackup(connFile) : Future(Never()); + + Void _ = wait(listen || fd || success(onShutdown) || backup); + } catch (Error& e) { + if (onShutdown.isReady() && onShutdown.isError()) throw onShutdown.getError(); + if(e.code() != error_code_actor_cancelled) + printf("SimulatedFDBDTerminated: %s\n", e.what()); + ASSERT( destructed || g_simulator.getCurrentProcess() == process ); // simulatedFDBD catch called on different process + TraceEvent(e.code() == error_code_actor_cancelled || e.code() == error_code_file_not_found || destructed ? SevInfo : SevError, "SimulatedFDBDTerminated").error(e, true); + } + + TraceEvent("SimulatedFDBDDone", localities.zoneId()).detail("Cycles", cycles) + .detail("ProcessAddress", process->address) + .detailext("ZoneId", localities.zoneId()) + .detail("KillType", onShutdown.isReady() ? onShutdown.get() : ISimulator::None); + + if (!onShutdown.isReady()) + onShutdown = ISimulator::InjectFaults; + } catch (Error& e) { + TraceEvent(destructed ? SevInfo : SevError, "SimulatedFDBDRebooterError").error(e, true); + onShutdown = e; + } + + ASSERT( destructed || g_simulator.getCurrentProcess() == process ); + + if( !process->shutdownSignal.isSet() && !destructed ) { + process->rebooting = true; + process->shutdownSignal.send(ISimulator::None); + } + Void _ = wait( g_simulator.onProcess( simProcess ) ); + + Void _ = wait(delay(0.00001 + FLOW_KNOBS->MAX_BUGGIFIED_DELAY)); // One last chance for the process to clean up? + + g_simulator.destroyProcess( process ); // Leak memory here; the process may be used in other parts of the simulation + + auto shutdownResult = onShutdown.get(); + TraceEvent("SimulatedFDBDShutdown", localities.zoneId()).detail("Cycles", cycles) + .detail("ProcessAddress", process->address) + .detailext("ZoneId", localities.zoneId()) + .detail("KillType", shutdownResult); + + if( shutdownResult < ISimulator::RebootProcessAndDelete ) { + TraceEvent("SimulatedFDBDLowerReboot", localities.zoneId()).detail("Cycles", cycles) + .detail("ProcessAddress", process->address) + .detailext("ZoneId", localities.zoneId()) + .detail("KillType", shutdownResult); + return onShutdown.get(); + } + + if( onShutdown.get() == ISimulator::RebootProcessAndDelete ) { + TraceEvent("SimulatedFDBDRebootAndDelete", localities.zoneId()).detail("Cycles", cycles) + .detail("ProcessAddress", process->address) + .detailext("ZoneId", localities.zoneId()) + .detail("KillType", shutdownResult); + *coordFolder = joinPath(baseFolder, g_random->randomUniqueID().toString()); + *dataFolder = joinPath(baseFolder, g_random->randomUniqueID().toString()); + platform::createDirectory( *dataFolder ); + + if(!useSeedFile) { + writeFile(joinPath(*dataFolder, "fdb.cluster"), connStr.toString()); + connFile = Reference( new ClusterConnectionFile( joinPath( *dataFolder, "fdb.cluster" ))); + } + else { + connFile = Reference( new ClusterConnectionFile( joinPath( *dataFolder, "fdb.cluster" ), connStr.toString() ) ); + } + } + else { + TraceEvent("SimulatedFDBDJustRepeat", localities.zoneId()).detail("Cycles", cycles) + .detail("ProcessAddress", process->address) + .detailext("ZoneId", localities.zoneId()) + .detail("KillType", shutdownResult); + } + } +} + +template<> +std::string describe(bool const& val) { + return val ? "true" : "false"; +} + +template<> +std::string describe(int const& val) { + return format("%d", val); +} + +// Since a datacenter kill is considered to be the same as killing a machine, files cannot be swapped across datacenters +std::map< Optional>, std::vector< std::vector< std::string > > > availableFolders; +// process count is no longer needed because it is now the length of the vector of ip's, because it was one ip per process +ACTOR Future simulatedMachine( + ClusterConnectionString connStr, + std::vector ips, + bool sslEnabled, + LocalityData localities, + ProcessClass processClass, + std::string baseFolder, + bool restarting, + bool useSeedFile, + bool runBackupAgents) +{ + state int bootCount = 0; + state std::vector myFolders; + state std::vector coordFolders; + + try { + CSimpleIni ini; + ini.SetUnicode(); + ini.LoadFile(joinPath(baseFolder, "restartInfo.ini").c_str()); + + for (int i = 0; i < ips.size(); i++) { + if (restarting) { + myFolders.push_back( ini.GetValue(printable(localities.zoneId()).c_str(), format("%d", i).c_str(), joinPath(baseFolder, g_random->randomUniqueID().toString()).c_str()) ); + + if(i == 0) { + std::string coordinationFolder = ini.GetValue(printable(localities.zoneId()).c_str(), "coordinationFolder", ""); + if(!coordinationFolder.size()) + coordinationFolder = ini.GetValue(printable(localities.zoneId()).c_str(), format("c%d", i).c_str(), joinPath(baseFolder, g_random->randomUniqueID().toString()).c_str()); + coordFolders.push_back(coordinationFolder); + } else { + coordFolders.push_back( ini.GetValue(printable(localities.zoneId()).c_str(), format("c%d", i).c_str(), joinPath(baseFolder, g_random->randomUniqueID().toString()).c_str()) ); + } + } + else { + coordFolders.push_back( joinPath(baseFolder, g_random->randomUniqueID().toString()) ); + std::string thisFolder = g_random->randomUniqueID().toString(); + myFolders.push_back( joinPath(baseFolder, thisFolder ) ); + platform::createDirectory( myFolders[i] ); + + if (!useSeedFile) + writeFile(joinPath(myFolders[i], "fdb.cluster"), connStr.toString()); + } + } + + loop { + state std::vector< Future > processes; + for( int i = 0; i < ips.size(); i++ ) { + std::string path = joinPath(myFolders[i], "fdb.cluster"); + Reference clusterFile(useSeedFile ? new ClusterConnectionFile(path, connStr.toString()) : new ClusterConnectionFile(path)); + processes.push_back(simulatedFDBDRebooter(clusterFile, ips[i], sslEnabled, i + 1, localities, processClass, &myFolders[i], &coordFolders[i], baseFolder, connStr, useSeedFile, runBackupAgents)); + } + + TEST( bootCount >= 1 ); // Simulated machine rebooted + TEST( bootCount >= 2 ); // Simulated machine rebooted twice + TEST( bootCount >= 3 ); // Simulated machine rebooted three times + ++bootCount; + + TraceEvent("SimulatedMachineStart") + .detail("Folder0", myFolders[0]) + .detail("CFolder0", coordFolders[0]) + .detail("MachineIPs", toIPVectorString(ips)) + .detail("SSL", sslEnabled) + .detail("processes", processes.size()) + .detail("bootCount", bootCount) + .detail("ProcessClass", processClass.toString()) + .detail("Restarting", restarting) + .detail("UseSeedFile", useSeedFile) + .detailext("ZoneId", localities.zoneId()) + .detailext("DataHall", localities.dataHallId()) + .detail("Locality", localities.toString()); + + Void _ = wait( waitForAll( processes ) ); + + TraceEvent("SimulatedMachineRebootStart") + .detail("Folder0", myFolders[0]) + .detail("CFolder0", coordFolders[0]) + .detail("MachineIPs", toIPVectorString(ips)) + .detailext("ZoneId", localities.zoneId()) + .detailext("DataHall", localities.dataHallId()); + + //Kill all open files, which may cause them to write invalid data. + auto& machineCache = g_simulator.getMachineById(localities.zoneId())->openFiles; + + //Copy the file pointers to a vector because the map may be modified while we are killing files + std::vector files; + for(auto fileItr = machineCache.begin(); fileItr != machineCache.end(); ++fileItr) { + ASSERT( fileItr->second.isReady() ); + files.push_back( (AsyncFileNonDurable*)fileItr->second.get().getPtr() ); + } + + std::vector> killFutures; + for(auto fileItr = files.begin(); fileItr != files.end(); ++fileItr) + killFutures.push_back((*fileItr)->kill()); + + Void _ = wait( waitForAll( killFutures ) ); + + state std::set filenames; + state std::string closingStr; + auto& machineCache = g_simulator.getMachineById(localities.zoneId())->openFiles; + for( auto it : machineCache ) { + filenames.insert( it.first ); + closingStr += it.first + ", "; + ASSERT( it.second.isReady() && !it.second.isError() ); + } + + TraceEvent("SimulatedMachineRebootAfterKills") + .detail("Folder0", myFolders[0]) + .detail("CFolder0", coordFolders[0]) + .detail("MachineIPs", toIPVectorString(ips)) + .detail("Closing", closingStr) + .detailext("ZoneId", localities.zoneId()) + .detailext("DataHall", localities.dataHallId()); + + ISimulator::MachineInfo* machine = g_simulator.getMachineById(localities.zoneId()); + machine->closingFiles = filenames; + g_simulator.getMachineById(localities.zoneId())->openFiles.clear(); + + // During a reboot: + // The process is expected to close all files and be inactive in zero time, but not necessarily + // without delay(0)-equivalents, so delay(0) a few times waiting for it to achieve that goal. + // After an injected fault: + // The process is expected to shut down eventually, but not necessarily instantly. Wait up to 60 seconds. + state int shutdownDelayCount = 0; + state double backoff = 0; + loop { + auto& machineCache = g_simulator.getMachineById(localities.zoneId())->closingFiles; + + if( !machineCache.empty() ) { + std::string openFiles; + int i = 0; + for( auto it = machineCache.begin(); it != machineCache.end() && i < 5; ++it ) { + openFiles += *it + ", "; + i++; + } + TraceEvent("MachineFilesOpen").detail("PAddr", toIPVectorString(ips)).detail("OpenFiles", openFiles); + } else + break; + + if( shutdownDelayCount++ >= 50 ) { // Worker doesn't shut down instantly on reboot + TraceEvent(SevError, "SimulatedFDBDFilesCheck") + .detail("PAddrs", toIPVectorString(ips)) + .detailext("ZoneId", localities.zoneId()) + .detailext("DataHall", localities.dataHallId()); + ASSERT( false ); + } + + Void _ = wait( delay( backoff ) ); + backoff = std::min( backoff + 1.0, 6.0 ); + } + + TraceEvent("SimulatedFDBDFilesClosed") + .detail("ProcessAddress", toIPVectorString(ips)) + .detailext("ZoneId", localities.zoneId()) + .detailext("DataHall", localities.dataHallId()); + + g_simulator.destroyMachine(localities.zoneId()); + + // SOMEDAY: when processes can be rebooted, this check will be needed + //ASSERT( this machine is rebooting ); + + // Since processes can end with different codes, take the highest (least severe) to detmine what to do + state ISimulator::KillType killType = processes[0].get(); + for( int i = 1; i < ips.size(); i++ ) + killType = std::max( processes[i].get(), killType ); + + TEST( true ); // Simulated machine has been rebooted + + state bool swap = killType == ISimulator::Reboot && BUGGIFY_WITH_PROB(0.75) && g_simulator.canSwapToMachine( localities.zoneId() ); + if( swap ) + availableFolders[localities.dcId()].push_back( myFolders ); + + auto rebootTime = g_random->random01() * MACHINE_REBOOT_TIME; + + TraceEvent("SimulatedMachineShutdown") + .detail("Swap", swap) + .detail("KillType", killType) + .detail("RebootTime", rebootTime) + .detailext("ZoneId", localities.zoneId()) + .detailext("DataHall", localities.dataHallId()) + .detail("MachineIPs", toIPVectorString(ips)); + + Void _ = wait( delay( rebootTime ) ); + + if( swap ) { + auto& avail = availableFolders[localities.dcId()]; + int i = g_random->randomInt(0, avail.size()); + if( i != avail.size() - 1 ) + std::swap( avail[i], avail.back() ); + auto toRebootFrom = avail.back(); + avail.pop_back(); + + if( myFolders != toRebootFrom ) { + TEST( true ); // Simulated machine swapped data folders + TraceEvent("SimulatedMachineFolderSwap") + .detail("OldFolder0", myFolders[0]).detail("NewFolder0", toRebootFrom[0]) + .detail("MachineIPs", toIPVectorString(ips)); + } + myFolders = toRebootFrom; + if(!useSeedFile) { + for(auto f : toRebootFrom) { + if(!fileExists(joinPath(f, "fdb.cluster"))) { + writeFile(joinPath(f, "fdb.cluster"), connStr.toString()); + } + } + } + } else if( killType == ISimulator::RebootAndDelete ) { + for( int i = 0; i < ips.size(); i++ ) { + coordFolders[i] = joinPath(baseFolder, g_random->randomUniqueID().toString()); + myFolders[i] = joinPath(baseFolder, g_random->randomUniqueID().toString()); + platform::createDirectory( myFolders[i] ); + + if(!useSeedFile) { + writeFile(joinPath(myFolders[i], "fdb.cluster"), connStr.toString()); + } + } + + TEST( true ); // Simulated machine rebooted with data loss + } + + //this machine is rebooting = false; + } + } catch( Error &e ) { + g_simulator.getMachineById(localities.zoneId())->openFiles.clear(); + throw; + } +} + +#include "fdbclient/MonitorLeader.h" + +ACTOR Future restartSimulatedSystem(vector> *systemActors, std::string baseFolder, + int* pTesterCount, Optional *pConnString) { + CSimpleIni ini; + ini.SetUnicode(); + ini.LoadFile(joinPath(baseFolder, "restartInfo.ini").c_str()); + + // allows multiple ipAddr entries + ini.SetMultiKey(); + + try { + int dataCenters = atoi(ini.GetValue("META", "dataCenters")); + int killableMachines = atoi(ini.GetValue("META", "killableMachines")); + int machineCount = atoi(ini.GetValue("META", "machineCount")); + int machinesNeededForProgress = atoi(ini.GetValue("META", "machinesNeededForProgress")); + int processesPerMachine = atoi(ini.GetValue("META", "processesPerMachine")); + int desiredCoordinators = atoi(ini.GetValue("META", "desiredCoordinators")); + int testerCount = atoi(ini.GetValue("META", "testerCount")); + ClusterConnectionString conn(ini.GetValue("META", "connectionString")); + *pConnString = conn; + *pTesterCount = testerCount; + bool usingSSL = conn.toString().find(":tls") != std::string::npos; + int useSeedForMachine = g_random->randomInt(0, machineCount); + for( int i = 0; i < machineCount; i++) { + Optional> dcUID; + std::string zoneIdString = ini.GetValue("META", format("%d", i).c_str()); + Standalone zoneId = StringRef(zoneIdString); + std::string dcUIDini = ini.GetValue(zoneIdString.c_str(), "dcUID"); + if (!dcUIDini.empty()) dcUID = StringRef(dcUIDini); + ProcessClass processClass = ProcessClass((ProcessClass::ClassType)atoi(ini.GetValue(zoneIdString.c_str(), "mClass")), ProcessClass::CommandLineSource); + + std::vector ipAddrs; + int processes = atoi(ini.GetValue(zoneIdString.c_str(), "processes")); + + auto ip = ini.GetValue(zoneIdString.c_str(), "ipAddr"); + + if( ip == NULL ) { + for (int i = 0; i < processes; i++){ + ipAddrs.push_back(strtoul(ini.GetValue(zoneIdString.c_str(), format("ipAddr%d", i).c_str()), NULL, 10)); + } + } + else { + // old way + ipAddrs.push_back(strtoul(ip, NULL, 10)); + for (int i = 1; i < processes; i++){ + ipAddrs.push_back(ipAddrs.back() + 1); + } + } + + LocalityData localities(Optional>(), zoneId, zoneId, dcUID); + localities.set(LiteralStringRef("data_hall"), dcUID); + + systemActors->push_back( reportErrors( simulatedMachine( + conn, ipAddrs, usingSSL, localities, processClass, baseFolder, true, i == useSeedForMachine, false ), + processClass == ProcessClass::TesterClass ? "SimulatedTesterMachine" : "SimulatedMachine") ); + } + + g_simulator.killableMachines = killableMachines; + g_simulator.neededDatacenters = dataCenters; + g_simulator.maxCoordinatorsInDatacenter = ((desiredCoordinators-1)/dataCenters) + 1; + g_simulator.killableDatacenters = 0; + g_simulator.machinesNeededForProgress = machinesNeededForProgress; + g_simulator.desiredCoordinators = desiredCoordinators; + g_simulator.processesPerMachine = processesPerMachine; + } + catch (Error& e) { + TraceEvent(SevError, "restartSimulationError").error(e); + } + + TraceEvent("RestartSimulatorSettings") + .detail("killableMachines", g_simulator.killableMachines) + .detail("neededDatacenters", g_simulator.neededDatacenters) + .detail("killableDatacenters", g_simulator.killableDatacenters) + .detail("machinesNeededForProgress", g_simulator.machinesNeededForProgress) + .detail("maxCoordinatorsInDatacenter", g_simulator.maxCoordinatorsInDatacenter) + .detail("desiredCoordinators", g_simulator.desiredCoordinators) + .detail("processesPerMachine", g_simulator.processesPerMachine); + + Void _ = wait(delay(1.0)); + + return Void(); +} + +std::string randomConfiguration( int physicalDatacenters ) { + int r = std::min(g_random->randomInt(0, 6), 3); +// r = 1; //ahm + + // See also random configuration choices in ConfigureDatabase workload + + std::string startingConfig = "new"; + if (r == 0) { + TEST( true ); // Simulated cluster using custom redundancy mode + int storage_replicas = g_random->randomInt(1,5); + startingConfig += " storage_replicas:=" + format("%d", storage_replicas); + startingConfig += " storage_quorum:=" + format("%d", storage_replicas); + int log_replicas = g_random->randomInt(1,5); + startingConfig += " log_replicas:=" + format("%d", log_replicas); + int log_anti_quorum = g_random->randomInt(0, log_replicas); + startingConfig += " log_anti_quorum:=" + format("%d", log_anti_quorum); + startingConfig += " replica_datacenters:=1"; + startingConfig += " min_replica_datacenters:=1"; + } + else if (r == 1) { + TEST( true ); // Simulated cluster running in single redundancy mode + startingConfig += " single"; + } + else if( r == 2 ) { + TEST( true ); // Simulated cluster running in double redundancy mode + startingConfig += " double"; + } + else if( r == 3 ) { + if( physicalDatacenters == 1 ) { + TEST( true ); // Simulated cluster running in triple redundancy mode + startingConfig += " triple"; + } + else if( physicalDatacenters == 2 ) { + TEST( true ); // Simulated cluster running in 2 datacenter mode + startingConfig += " two_datacenter"; + } + else if( physicalDatacenters == 3 ) { + TEST( true ); // Simulated cluster running in 3 data-hall mode + startingConfig += " three_data_hall"; + } + else { + ASSERT( false ); + } + } + + if (g_random->random01() < 0.25) startingConfig += " logs=" + format("%d", g_random->randomInt(1,7)); + if (g_random->random01() < 0.25) startingConfig += " proxies=" + format("%d", g_random->randomInt(1,7)); + if (g_random->random01() < 0.25) startingConfig += " resolvers=" + format("%d", g_random->randomInt(1,7)); + + startingConfig += g_random->random01() < 0.5 ? " ssd" : " memory"; + return startingConfig; +} + +void setupSimulatedSystem( vector> *systemActors, std::string baseFolder, + int* pTesterCount, Optional *pConnString, + Standalone *pStartingConfiguration, int extraDB) +{ + int dataCenters = g_random->randomInt( 1, 4 ); + + // SOMEDAY: this does not test multi-interface configurations + std::string startingConfigString = randomConfiguration(dataCenters); + std::map startingConfigMap; + ASSERT( buildConfiguration( startingConfigString, startingConfigMap ) == ConfigurationResult::SUCCESS ); + + DatabaseConfiguration startingConfig; + for(auto kv : startingConfigMap) startingConfig.set( kv.first, kv.second ); + g_simulator.storagePolicy = startingConfig.storagePolicy; + g_simulator.tLogPolicy = startingConfig.tLogPolicy; + g_simulator.tLogWriteAntiQuorum = startingConfig.tLogWriteAntiQuorum; + ASSERT(g_simulator.storagePolicy); + ASSERT(g_simulator.tLogPolicy); + TraceEvent("simulatorConfig").detail("tLogPolicy", g_simulator.tLogPolicy->info()).detail("storagePolicy", g_simulator.storagePolicy->info()).detail("tLogWriteAntiQuorum", g_simulator.tLogWriteAntiQuorum).detail("ConfigString", startingConfigString); + + int machineCount = g_random->randomInt( std::max( 2+dataCenters, startingConfig.minMachinesRequired() ), extraDB ? 6 : 10 ); + + // half the time, when we have more than 4 machines that are not the first in their dataCenter, assign classes + bool assignClasses = machineCount - dataCenters > 4 && g_random->random01() < 0.5; + int processesPerMachine = g_random->randomInt(1, (extraDB ? 14 : 28)/machineCount + 2 ); + + // Use SSL half the time + bool sslEnabled = g_random->random01() < 0.05; + TEST( sslEnabled ); // SSL enabled + TEST( !sslEnabled ); // SSL disabled + + // Pick coordination processes. + int coordinatorCount = BUGGIFY ? g_random->randomInt(1, machineCount+1) : std::min( machineCount, startingConfig.maxMachineFailuresTolerated()*2 + 1 ); + + vector coordinatorAddresses; + for( int dc = 0; dc < dataCenters; dc++ ) { + int machines = machineCount / dataCenters + (dc < machineCount % dataCenters); // add remainder of machines to first datacenter + int dcCoordinators = coordinatorCount / dataCenters + (dc < coordinatorCount%dataCenters); + + for(int m = 0; m < dcCoordinators; m++) { + uint32_t ip = 2<<24 | dc<<16 | 1<<8 | m; + coordinatorAddresses.push_back(NetworkAddress(ip, 1, true, sslEnabled)); + TraceEvent("SelectedCoordinator").detail("Address", coordinatorAddresses.back()); + } + } + + g_random->randomShuffle(coordinatorAddresses); + for(int i = 0; i < (coordinatorAddresses.size()/2)+1; i++) { + g_simulator.protectedAddresses.insert(coordinatorAddresses[i]); + } + g_random->randomShuffle(coordinatorAddresses); + + ASSERT( coordinatorAddresses.size() == coordinatorCount ); + ClusterConnectionString conn(coordinatorAddresses, LiteralStringRef("TestCluster:0")); + g_simulator.extraDB = extraDB ? new ClusterConnectionString(coordinatorAddresses, ((extraDB==1 && BUGGIFY) ? LiteralStringRef("TestCluster:0") : LiteralStringRef("ExtraCluster:0"))) : NULL; + + *pConnString = conn; + + TraceEvent("SimulatedConnectionString").detail("String", conn.toString()).detail("ConfigString", startingConfigString); + + int assignedMachines = 0, nonVersatileMachines = 0; + for( int dc = 0; dc < dataCenters; dc++ ) { + Optional> dcUID; + if ((dc > 0) || (g_random->random01() > 0.01)) dcUID = StringRef(format("%d", dc)); + std::vector machineIdentities; + int machines = machineCount / dataCenters + (dc < machineCount % dataCenters); // add remainder of machines to first datacenter + int dcCoordinators = coordinatorCount / dataCenters + (dc < coordinatorCount%dataCenters); + printf("Datacenter %d: %d/%d machines, %d/%d coordinators\n", dc, machines, machineCount, dcCoordinators, coordinatorCount); + ASSERT( dcCoordinators <= machines ); + int useSeedForMachine = g_random->randomInt(0, machines); + for( int machine = 0; machine < machines; machine++ ) { + Standalone zoneId(g_random->randomUniqueID().toString()); + + //Choose a machine class + ProcessClass processClass = ProcessClass(ProcessClass::UnsetClass, ProcessClass::CommandLineSource); + if(assignClasses) { + if(assignedMachines < 4) + processClass = ProcessClass((ProcessClass::ClassType) g_random->randomInt(0, 2), ProcessClass::CommandLineSource); //Unset or Storage + else if(assignedMachines == 4) + processClass = ProcessClass((ProcessClass::ClassType) (g_random->randomInt(0, 2) * ProcessClass::ResolutionClass), ProcessClass::CommandLineSource); //Unset or Resolution + else + processClass = ProcessClass((ProcessClass::ClassType) g_random->randomInt(0, 3), ProcessClass::CommandLineSource); //Unset, Storage, or Transaction + if (processClass == ProcessClass::ResolutionClass) // *can't* be assigned to other roles, even in an emergency + nonVersatileMachines++; + } + + std::vector ips; + for (int i = 0; i < processesPerMachine; i++){ + ips.push_back(2 << 24 | dc << 16 | g_random->randomInt(1, i+2) << 8 | machine); + } + // check the sslEnablementMap using only one ip( + LocalityData localities(Optional>(), zoneId, zoneId, dcUID); + localities.set(LiteralStringRef("data_hall"), dcUID); + systemActors->push_back(reportErrors(simulatedMachine(conn, ips, sslEnabled, + localities, processClass, baseFolder, false, machine == useSeedForMachine, true ), "SimulatedMachine")); + + if (extraDB) { + std::vector extraIps; + for (int i = 0; i < processesPerMachine; i++){ + extraIps.push_back(4 << 24 | dc << 16 | g_random->randomInt(1, i + 2) << 8 | machine); + } + + Standalone newZoneId = Standalone(g_random->randomUniqueID().toString()); + LocalityData localities(Optional>(), newZoneId, newZoneId, dcUID); + localities.set(LiteralStringRef("data_hall"), dcUID); + systemActors->push_back(reportErrors(simulatedMachine(*g_simulator.extraDB, extraIps, sslEnabled, + localities, + processClass, baseFolder, false, machine == useSeedForMachine, false ), "SimulatedMachine")); + } + + assignedMachines++; + } + } + + g_simulator.desiredCoordinators = coordinatorCount; + g_simulator.killableMachines = startingConfig.maxMachineFailuresTolerated(); + g_simulator.neededDatacenters = startingConfig.minDataCenters; + g_simulator.killableDatacenters = startingConfig.minDataCenters - 1; + g_simulator.physicalDatacenters = dataCenters; + g_simulator.maxCoordinatorsInDatacenter = ((coordinatorCount-1)/dataCenters) + 1; + g_simulator.machinesNeededForProgress = startingConfig.minMachinesRequired() + nonVersatileMachines; + g_simulator.processesPerMachine = processesPerMachine; + + TraceEvent("SetupSimulatorSettings") + .detail("killableMachines", g_simulator.killableMachines) + .detail("neededDatacenters", g_simulator.neededDatacenters) + .detail("killableDatacenters", g_simulator.killableDatacenters) + .detail("machinesNeededForProgress", g_simulator.machinesNeededForProgress) + .detail("maxCoordinatorsInDatacenter", g_simulator.maxCoordinatorsInDatacenter) + .detail("desiredCoordinators", g_simulator.desiredCoordinators) + .detail("processesPerMachine", g_simulator.processesPerMachine); + + // SOMEDAY: add locality for testers to simulate network topology + // FIXME: Start workers with tester class instead, at least sometimes run tests with the testers-only flag + int testerCount = *pTesterCount = g_random->randomInt(4, 9); + int useSeedForMachine = g_random->randomInt(0, testerCount); + for(int i=0; i ips; + ips.push_back(0x03040301 + i); + Standalone newZoneId = Standalone(g_random->randomUniqueID().toString()); + LocalityData localities(Optional>(), newZoneId, newZoneId, Optional>()); + systemActors->push_back( reportErrors( simulatedMachine( + conn, ips, sslEnabled, + localities, ProcessClass(ProcessClass::TesterClass, ProcessClass::CommandLineSource), + baseFolder, false, i == useSeedForMachine, false ), + "SimulatedTesterMachine") ); + } + + /*int testerCount = g_random->randomInt(4, 9); + for(int i=0; irandomUniqueID().toString(), Optional>()), [&] { + vector> v; + + Reference> cc( new AsyncVar ); + Reference> ci( new AsyncVar ); + v.push_back( monitorLeader( coordinators, cc ) ); + v.push_back( extractClusterInterface(cc,ci) ); + v.push_back( failureMonitorClient( ci ) ); + v.push_back( testerServer( cc ) ); + systemActors->push_back( waitForAll(v) ); + });*/ + + + *pStartingConfiguration = startingConfigString; + + // save some state that we only need when restarting the simulator. + g_simulator.connectionString = conn.toString(); + g_simulator.testerCount = testerCount; + + TraceEvent("SimulatedClusterStarted") + .detail("KillableMachines", g_simulator.killableMachines) + .detail("DataCenters", dataCenters) + .detail("NeededDataCenters", g_simulator.neededDatacenters) + .detail("ServerMachineCount", machineCount) + .detail("ServersNeededForProgress", g_simulator.machinesNeededForProgress) + .detail("ProcessesPerServer", processesPerMachine) + .detail("SSLEnabled", sslEnabled) + .detail("ClassesAssigned", assignClasses) + .detail("StartingConfiguration", pStartingConfiguration->toString()) + //.detail("TesterCount", testerCount) + ; + + // FIXME + /*Void _ = wait( DatabaseContext::configureDatabase( *pZookeeper, ClusterInterface::DEFAULT, mode ) ); + Void _ = wait( DatabaseContext::configureDatabase( *pZookeeper, ClusterInterface::ALL, mode ) );*/ +} + +int checkExtraDB(const char *testFile) { + std::ifstream ifs; + ifs.open(testFile, std::ifstream::in); + if (!ifs.good()) + return 0; + + std::string cline; + + while (ifs.good()) { + getline(ifs, cline); + std::string line = removeWhitespace(std::string(cline)); + if (!line.size() || line.find(';') == 0) + continue; + + size_t found = line.find('='); + if (found == std::string::npos) + // hmmm, not good + continue; + std::string attrib = removeWhitespace(line.substr(0, found)); + std::string value = removeWhitespace(line.substr(found + 1)); + + if (attrib == "extraDB") { + int v = 0; + sscanf( value.c_str(), "%d", &v ); + ifs.close(); + return v; + } + } + + ifs.close(); + return 0; +} + +ACTOR void setupAndRun(std::string dataFolder, const char *testFile, bool rebooting ) { + state vector> systemActors; + state Optional connFile; + state Standalone startingConfiguration; + state int testerCount = 1; + state int extraDB = checkExtraDB(testFile); + + Void _ = wait( g_simulator.onProcess( g_simulator.newProcess( + "TestSystem", 0x01010101, 1, LocalityData(Optional>(), Standalone(g_random->randomUniqueID().toString()), Optional>(), Optional>()), ProcessClass(), "", "" ), TaskDefaultYield ) ); + Sim2FileSystem::newFileSystem(); + FlowTransport::createInstance(1); + simInitTLS(); + + TEST(true); // Simulation start + + try { + //systemActors.push_back( startSystemMonitor(dataFolder) ); + if (rebooting) { + Void _ = wait( timeoutError( restartSimulatedSystem( &systemActors, dataFolder, &testerCount, &connFile), 100.0 ) ); + } + else { + g_expect_full_pointermap = 1; + setupSimulatedSystem( &systemActors, dataFolder, &testerCount, &connFile, &startingConfiguration, extraDB ); + Void _ = wait( delay(1.0) ); // FIXME: WHY!!! //wait for machines to boot + } + std::string clusterFileDir = joinPath( dataFolder, g_random->randomUniqueID().toString() ); + platform::createDirectory( clusterFileDir ); + writeFile(joinPath(clusterFileDir, "fdb.cluster"), connFile.get().toString()); + Void _ = wait(timeoutError(runTests(Reference(new ClusterConnectionFile(joinPath(clusterFileDir, "fdb.cluster"))), TEST_TYPE_FROM_FILE, TEST_ON_TESTERS, testerCount, testFile, startingConfiguration), buggifyActivated ? 36000.0 : 5400.0)); + } catch (Error& e) { + TraceEvent(SevError, "setupAndRunError").error(e); + } + + TraceEvent("SimulatedSystemDestruct"); + destructed = true; + systemActors.clear(); + + g_simulator.stop(); +} diff --git a/fdbserver/SimulatedCluster.h b/fdbserver/SimulatedCluster.h new file mode 100644 index 0000000000..3b027d9141 --- /dev/null +++ b/fdbserver/SimulatedCluster.h @@ -0,0 +1,27 @@ +/* + * SimulatedCluster.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_SIMULATEDCLUSTER_H +#define FDBSERVER_SIMULATEDCLUSTER_H +#pragma once + +void setupAndRun(std::string const& dataFolder, const char* const& testFile, bool const& rebooting); + +#endif \ No newline at end of file diff --git a/fdbserver/SkipList.cpp b/fdbserver/SkipList.cpp new file mode 100644 index 0000000000..0ca183f1a4 --- /dev/null +++ b/fdbserver/SkipList.cpp @@ -0,0 +1,1551 @@ +/* + * SkipList.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include + +/* +#ifdef __GNUG__ +#include +#endif +*/ + +#include "flow/Platform.h" +#include "fdbrpc/fdbrpc.h" +#include "fdbrpc/PerfMetric.h" +#include "fdbclient/FDBTypes.h" +#include "fdbclient/KeyRangeMap.h" +#include "fdbclient/SystemData.h" +#include "Knobs.h" + +#define PARALLEL_THREAD_COUNT 0 // FIXME: When >1, program execution (e.g. random numbers) is/was nondeterministic. Why? + +using std::min; +using std::max; +using std::make_pair; + +static vector skc; + +static thread_local uint32_t g_seed = 0; + +static inline int skfastrand() { + g_seed = g_seed * 1664525L + 1013904223L; + return g_seed; +} + +void setAffinity(int proc); + +class SlowConflictSet { +public: + bool is_conflict( const VectorRef& readRanges, Version read_snapshot ); + void add( const VectorRef& clearRanges, const VectorRef& setValues, Version now ); + void clear( Version now ); + +private: + KeyRangeMap age; +}; + +bool SlowConflictSet::is_conflict( const VectorRef& readRanges, Version read_snapshot ) { + for(auto range = readRanges.begin(); range != readRanges.end(); ++range) { + auto intersecting = age.intersectingRanges( *range ); + for(auto it = intersecting.begin(); it != intersecting.end(); ++it) + if ( it.value() > read_snapshot ) + return true; + } + return false; +} + +void SlowConflictSet::clear( Version now ) { + age.insert(allKeys, now); +} + +void SlowConflictSet::add( const VectorRef& clearRanges, const VectorRef& setValues, Version now ) { + for(auto c = clearRanges.begin(); c != clearRanges.end(); ++c) + age.insert( *c, now ); + for(auto s = setValues.begin(); s != setValues.end(); ++s) + age.insert( s->key, now ); +} + + +PerfDoubleCounter + g_buildTest("Build", skc), + g_add("Add", skc), + g_add_sort("A.Sort", skc), + g_detectConflicts("Detect", skc), + g_sort("D.Sort", skc), + g_combine("D.Combine", skc), + g_checkRead("D.CheckRead", skc), + g_checkBatch("D.CheckIntraBatch", skc), + g_merge("D.MergeWrite", skc), + g_merge_launch("D.Merge.Launch", skc), + g_merge_fork("D.Merge.Fork", skc), + g_merge_start_var("D.Merge.StartVariance", skc), + g_merge_end_var("D.Merge.EndVariance", skc), + g_merge_run_var("D.Merge.RunVariance", skc), + g_merge_run_shortest("D.Merge.ShortestRun", skc), + g_merge_run_longest("D.Merge.LongestRun", skc), + g_merge_run_total("D.Merge.TotalRun", skc), + g_merge_join("D.Merge.Join", skc), + g_removeBefore("D.RemoveBefore", skc) + ; + +static force_inline int compare( const StringRef& a, const StringRef& b ) { + int c = memcmp( a.begin(), b.begin(), min(a.size(), b.size()) ); + if (c<0) return -1; + if (c>0) return +1; + if (a.size() < b.size()) return -1; + if (a.size() == b.size()) return 0; + return +1; +} + +struct ReadConflictRange { + StringRef begin, end; + Version version; + int transaction; + ReadConflictRange( StringRef begin, StringRef end, Version version, int transaction ) + : begin(begin), end(end), version(version), transaction(transaction) + { + } + bool operator<(const ReadConflictRange& rhs) const { return compare(begin, rhs.begin)<0; } +}; + + +struct KeyInfo { + StringRef key; + int* pIndex; + bool nextKey; + bool begin; + bool write; + int transaction; + + KeyInfo() {}; + KeyInfo( StringRef key, bool nextKey, bool begin, bool write, int transaction, int* pIndex ) : key(key), nextKey(nextKey), begin(begin), write(write), transaction(transaction), pIndex(pIndex) {} +}; + +// returns true if done with string +force_inline bool getCharacter(const KeyInfo& ki, int character, int &outputCharacter){ + // normal case + if (character < ki.key.size()){ + outputCharacter = 5 + ki.key.begin()[character]; + return false; + } + + // nextKey append a zero + if (ki.nextKey && character >= ki.key.size()){ + if (character == ki.key.size()){ + outputCharacter = 5; // extra '0' character + return false; + } + character--; + } + + // termination + if (character == ki.key.size()){ + outputCharacter = 0; + return false; + } + + if (character == ki.key.size()+1) { + // end/begin+read/write relative sorting + outputCharacter = ki.begin*2 + (ki.write ^ ki.begin); + return false; + } + + outputCharacter = 0; + return true; +} + +bool operator < ( const KeyInfo& lhs, const KeyInfo& rhs ) { + int i = min(lhs.key.size(), rhs.key.size()); + int c = memcmp( lhs.key.begin(), rhs.key.begin(), i ); + if (c!=0) return c<0; + + // SOMEDAY: This is probably not very fast. Slows D.Sort by ~20% relative to previous (incorrect) version. + + bool lDone, rDone; + int lc, rc; + while (true) { + lDone = getCharacter(lhs, i, lc); + rDone = getCharacter(rhs, i, rc); + if (lDone && rDone) return false; // equality + if (lc < rc) return true; + if (lc > rc) return false; + i++; + } +} + +bool operator == (const KeyInfo& lhs, const KeyInfo& rhs ) { + return !(lhs& points, int a, int b){ + if (points[b] < points[a]){ + KeyInfo temp; + temp = points[a]; + points[a] = points[b]; + points[b] = temp; + } +} + +void smallSort(vector& points, int start, int N){ + for (int i=1;i0;j-=2) + swapSort(points, start+j-1, start+j); + for (int i=N-2;i>0;i--) + for (int j=i;j>0;j-=2) + swapSort(points, start+j-1, start+j); +} + +struct SortTask { + int begin; + int size; + int character; + SortTask(int begin, int size, int character) : begin(begin), size(size), character(character) {} +}; + +void sortPoints(vector& points){ + vector tasks; + vector newPoints; + vector counts; + + tasks.push_back( SortTask(0, points.size(), 0) ); + + while (tasks.size()){ + SortTask st = tasks.back(); + tasks.pop_back(); + + if (st.size < 10){ + //smallSort(points, st.begin, st.size); + std::sort(points.begin() + st.begin, points.begin() + st.begin + st.size ); + continue; + } + + newPoints.resize(st.size); + counts.assign(256+5, 0); + + // get counts + int c; + bool allDone = true; + for (int i=st.begin; i 1) + tasks.push_back(SortTask(st.begin+total, temp, st.character+1)); + counts[i] = total; + total += temp; + } + + // put in their places + for (int i=st.begin; irandom01() < 0.5 && l < MaxLevels-1) l++; + return l; */ + + //g_random->randomInt(0, 1<<(MaxLevels-1)); + uint32_t i = uint32_t(skfastrand()) >> (32-(MaxLevels-1)); + int level = 0; + while (i&1) { + i>>=1; + level++; + } + ASSERT( level < MaxLevels ); + return level; + } + + /* + struct Node { + int nPointers, valueLength; + Node *pointers[nPointers]; + Version maxVersions[nPointers]; + char value[valueLength]; + }; + */ + + struct Node { + int level() { return nPointers-1; } + uint8_t* value() { return end() + nPointers*(sizeof(Node*)+sizeof(Version)); } + int length() { return valueLength; } + Node* getNext(int i) { return *((Node**)end() + i); } + void setNext(int i, Node* n) { + *((Node**)end() + i) = n; + #if defined(_DEBUG) || 1 + /*if (n && n->level() < i) + *(volatile int*)0 = 0;*/ + #endif + } + + Version getMaxVersion(int i) { return ((Version*)(end() + nPointers*sizeof(Node*)))[i]; } + void setMaxVersion(int i, Version v) { ((Version*)(end() + nPointers*sizeof(Node*)))[i] = v; } + + // Return a node with initialized value but uninitialized pointers + static Node* create( const StringRef& value, int level ) { + int nodeSize = sizeof(Node) + value.size() + (level+1)*(sizeof(Node*)+sizeof(Version)); + + Node* n; + if (nodeSize <= 64) { + n = (Node*)FastAllocator<64>::allocate(); + INSTRUMENT_ALLOCATE("SkipListNode64"); + } else if (nodeSize <= 128) { + n = (Node*)FastAllocator<128>::allocate(); + INSTRUMENT_ALLOCATE("SkipListNode128"); + } else { + n = (Node*)new char[ nodeSize ]; + INSTRUMENT_ALLOCATE("SkipListNodeLarge"); + } + + n->nPointers = level+1; + + n->valueLength = value.size(); + memcpy(n->value(), value.begin(), value.size()); + return n; + } + + // pre: level>0, all lower level nodes between this and getNext(level) have correct maxversions + void calcVersionForLevel(int level){ + Node *end = getNext(level); + Version v = getMaxVersion(level-1); + for(Node *x = getNext(level-1); x != end; x = x->getNext(level-1)) + v = max(v, x->getMaxVersion(level-1)); + setMaxVersion(level, v); + } + + void destroy() { + int nodeSize = getNodeSize(); + if (nodeSize <= 64) { + FastAllocator<64>::release(this); + INSTRUMENT_RELEASE("SkipListNode64"); + } else if (nodeSize <= 128) { + FastAllocator<128>::release(this); + INSTRUMENT_RELEASE("SkipListNode128"); + } else { + delete[] (char*)this; + INSTRUMENT_RELEASE("SkipListNodeLarge"); + } + } + private: + int getNodeSize() { return sizeof(Node) + valueLength + nPointers*(sizeof(Node*)+sizeof(Version)); } + uint8_t* end() { return (uint8_t*)(this+1); } + int nPointers, + valueLength; + }; + + static force_inline bool less( const uint8_t* a, int aLen, const uint8_t* b, int bLen ) { + int len = min(aLen, bLen); + for(int i=0; i b[i]) + return false; + + /*int c = memcmp(a,b,min(aLen,bLen)); + if (c<0) return true; + if (c>0) return false;*/ + return aLen < bLen; + } + + Node *header; + + void destroy() { + Node *next, *x; + for(x = header; x; x = next) { + next = x->getNext(0); + x->destroy(); + } + } +public: + struct Finger{ + Node *finger[MaxLevels]; // valid for levels >= level + int level; + Node* x; + Node *alreadyChecked; + StringRef value; + + Finger() : level(MaxLevels), x(NULL), alreadyChecked(NULL) {} + + Finger( Node* header, const StringRef& ptr ) : + value(ptr), level(MaxLevels), + alreadyChecked(NULL), x(header) + { + } + + void init(const StringRef& value, Node *header){ + this->value = value; + x = header; + alreadyChecked = NULL; + level = MaxLevels; + } + + // pre: !finished() + force_inline void prefetch() { + Node* next = x->getNext(level-1); + _mm_prefetch( (const char*)next, _MM_HINT_T0 ); + //if ( (((intptr_t)next) & 64) == 0 ) + _mm_prefetch( (const char*)next+64, _MM_HINT_T0 ); + //_mm_prefetch( (const char*)next+128, _MM_HINT_T0 ); + //_mm_prefetch( (const char*)next+192, _MM_HINT_T0 ); + //_mm_prefetch( (const char*)next+256, _MM_HINT_T0 ); + //_mm_prefetch( (const char*)next+320, _MM_HINT_T0 ); + } + + // pre: !finished() + // Returns true if we have advanced to the next level + force_inline bool advance() { + Node* next = x->getNext(level-1); + + if (next == alreadyChecked || !less(next->value(), next->length(), value.begin(), value.size())) { + alreadyChecked = next; + level--; + finger[level] = x; + return true; + } else { + x = next; + return false; + } + } + + // pre: !finished() + force_inline void nextLevel() { + while (!advance()); + } + + force_inline bool finished(){ + return level == 0; + } + + force_inline Node* found() const { + // valid after finished returns true + Node *n = finger[0]->getNext(0); // or alreadyChecked, but that is more easily invalidated + if (n && n->length() == value.size() && !memcmp(n->value(), value.begin(), value.size())) + return n; + else + return NULL; + } + + StringRef getValue() const { + Node* n = finger[0]->getNext(0); + return n ? StringRef( n->value(), n->length() ) : StringRef(); + } + }; + + int count() { + int count = 0; + Node* x = header->getNext(0); + while (x) { + x = x->getNext(0); + count++; + } + return count; + } + + explicit SkipList( Version version = 0 ) { + header = Node::create(StringRef(), MaxLevels-1); + for(int l=0; lsetNext(l, NULL); + header->setMaxVersion(l, version); + } + } + ~SkipList() { + destroy(); + } + SkipList(SkipList&& other) noexcept(true) + : header(other.header) + { + other.header = NULL; + } + void operator=(SkipList&& other) noexcept(true) { + destroy(); + header = other.header; + other.header = NULL; + } + void swap( SkipList& other ) { + std::swap(header, other.header); + } + + void addConflictRanges( const Finger* fingers, int rangeCount, Version version ) { + for(int r=rangeCount-1; r>=0; r--) { + const Finger& startF = fingers[r*2]; + const Finger& endF = fingers[r*2+1]; + + if (endF.found()==NULL) + insert(endF, endF.finger[0]->getMaxVersion(0)); + + remove( startF, endF ); + insert( startF, version ); + } + } + + void detectConflicts( ReadConflictRange* ranges, int count, bool* transactionConflictStatus ) { + const int M = 16; + int nextJob[M]; + CheckMax inProgress[ M ]; + if (!count) return; + + int started = min(M,count); + for(int i=0; ipartition + // is intended to be followed by a call to this->concatenate() recombining the same + // partitions. In between, operations on each partition must not touch any keys outside + // the partition. Specifically, the partition to the left of 'key' must not have a range + // [...,key) inserted, since that would insert an entry at 'key'. + void partition( StringRef* begin, int splitCount, SkipList* output ) { + for(int i=splitCount-1; i>=0; i--) { + Finger f( header, begin[i] ); + while (!f.finished()) + f.nextLevel(); + split(f, output[i+1]); + } + swap(output[0]); + } + + void concatenate( SkipList* input, int count ) { + vector ends( count-1 ); + for(int i=0; i=0; i--) { + ends[i].finger[l]->setNext( l, input[i+1].header->getNext(l) ); + if (l && (!i || ends[i].finger[l] != input[i].header)) + ends[i].finger[l]->calcVersionForLevel(l); + input[i+1].header->setNext( l, NULL ); + } + } + swap(input[0]); + } + + void find( const StringRef* values, Finger* results, int* temp, int count ) { + // Relying on the ordering of values, descend until the values aren't all in the + // same part of the tree + + // vtune: 11 parts + results[0].init( values[0], header ); + const StringRef& endValue = values[count-1]; + while ( results[0].level > 1 ) { + results[0].nextLevel(); + Node* ac = results[0].alreadyChecked; + if (ac && less(ac->value(), ac->length(), endValue.begin(), endValue.size())) + break; + } + + // Init all the other fingers to start descending where we stopped + // the first one + + // SOMEDAY: this loop showed up on vtune, could be faster? + // vtune: 8 parts + int startLevel = results[0].level+1; + Node *x = startLeveladvance(); + if (f->finished()) { + if (prevJob == job) break; + nextJob[prevJob] = nextJob[job]; + } + else { + f->prefetch(); + prevJob = job; + } + job = nextJob[job]; + } + } + + /*Finger randomFinger() { + // Written, not exactly uniform, not tested + Finger f( header, StringRef() ); + Node* begin = header, *end = 0; + for(int lev = MaxLevels-1; lev>=0; lev--) { + int length = 0; + for( Node* x = begin; x != end; x=x->getNext(lev) ) + length++; + if (length == 1) { // forced down + f.finger[lev] = begin; + } else { + int c = g_random->randomInt(0, length); + for( Node* x = begin; x != end; x=x->getNext(lev) ) + if (!c--) { + f.finger[lev] = begin = x; + end = x->getNext(lev); + break; + } + } + } + f.level = 0; + return f; + }*/ + + int removeBefore( Version v, Finger& f, int nodeCount ) { + /*Finger f( header, StringRef() ); + for(int i=0; igetNext(0); + if (!x) break; + + // double prefetch gives +25% speed (single threaded) + Node* next = x->getNext(0); + _mm_prefetch( (const char*)next, _MM_HINT_T0 ); + //_mm_prefetch( (const char*)next+64, _MM_HINT_T0 ); + next = x->getNext(1); + _mm_prefetch( (const char*)next, _MM_HINT_T0 ); + //_mm_prefetch( (const char*)next+64, _MM_HINT_T0 ); + + bool isAbove = x->getMaxVersion(0) >= v; + if (isAbove || wasAbove) { // f.nextItem + for(int l=0; l<=x->level(); l++) + f.finger[l] = x; + } else { // f.eraseItem + removedCount++; + for(int l=0; l<=x->level(); l++) + f.finger[l]->setNext(l, x->getNext(l)); + for(int i=1; i<=x->level(); i++) + f.finger[i]->setMaxVersion( i, max(f.finger[i]->getMaxVersion(i), x->getMaxVersion(i)) ); + x->destroy(); + } + wasAbove = isAbove; + } + + return removedCount; + } + +private: + void remove( const Finger& start, const Finger& end ) { + if (start.finger[0] == end.finger[0]) + return; + + Node *x = start.finger[0]->getNext(0); + + // vtune says: this loop is the expensive parts (6 parts) + for(int i=0; isetNext(i, end.finger[i]->getNext(i)); + + while (true) { + Node* next = x->getNext(0); + x->destroy(); + if (x == end.finger[0]) break; + x = next; + } + } + + //void insert( const std::string& v, Version version ) { insert(StringRef(v), version); } + + void insert( const Finger& f, Version version ) { + int level = randomLevel(); + //cout << std::string((const char*)value,length) << " level: " << level << endl; + Node *x = Node::create( f.value, level ); + x->setMaxVersion(0, version); + for(int i=0; i<=level; i++) { + x->setNext(i, f.finger[i]->getNext(i)); + f.finger[i]->setNext(i, x); + } + // vtune says: this loop is the costly part of this function + for(int i=1; i<=level; i++) { + f.finger[i]->calcVersionForLevel(i); + x->calcVersionForLevel(i); + } + for(int i=level+1; igetMaxVersion(i); + if (v >= version) break; + f.finger[i]->setMaxVersion(i, version); + } + } + + void insert( const StringRef& value, Version version ) { + Finger f(header, value); + while (!f.finished()) + f.nextLevel(); + // SOMEDAY: equality? + insert( f, version ); + } + + struct CheckMax { + Finger start, end; + Version version; + bool *result; + int state; + + void init( const ReadConflictRange& r, Node* header, bool* tCS ) { + this->start.init( r.begin, header ); + this->end.init( r.end, header ); + this->version = r.version; + result = &tCS[ r.transaction ]; + this->state = 0; + } + + bool noConflict() { return true; } + bool conflict() { *result = true; return true; } + + // Return true if finished + force_inline bool advance() { + switch (state) { + case 0: + // find where start and end fingers diverge + while (true) { + if (!start.advance()) { + start.prefetch(); + return false; + } + end.x = start.x; + while (!end.advance()); + + int l = start.level; + if (start.finger[l] != end.finger[l]) + break; + // accept if the range spans the check range, but does not have a greater version + if (start.finger[l]->getMaxVersion(l) <= version) + return noConflict(); + if (l==0) + return conflict(); + } + state = 1; + case 1: + { + // check the end side of the pyramid + Node *e = end.finger[end.level]; + while (e->getMaxVersion(end.level) > version) { + if (end.finished()) + return conflict(); + end.nextLevel(); + Node *f = end.finger[end.level]; + while (e != f){ + if (e->getMaxVersion(end.level) > version) + return conflict(); + e = e->getNext(end.level); + } + } + + // check the start side of the pyramid + Node *s = end.finger[start.level]; + while (true){ + Node *nextS = start.finger[start.level]->getNext(start.level); + Node *p = nextS; + while (p != s){ + if (p->getMaxVersion(start.level) > version) + return conflict(); + p = p->getNext(start.level); + } + if (start.finger[start.level]->getMaxVersion(start.level) <= version) + return noConflict(); + s = nextS; + if (start.finished()) { + if (nextS->length() == start.value.size() && !memcmp(nextS->value(), start.value.begin(), start.value.size())) + return noConflict(); + else + return conflict(); + } + start.nextLevel(); + } + } + default: + __assume(false); + } + } + }; + + void split( const Finger& f, SkipList& right ) { + ASSERT( !right.header->getNext(0) ); // right must be empty + right.header->setMaxVersion(0, f.finger[0]->getMaxVersion(0)); + for(int l=0; lsetNext(l, f.finger[l]->getNext(l)); + f.finger[l]->setNext(l, NULL); + /*if (l) { + // SOMEDAY: Do we actually need these? + right.header->calcVersionForLevel(l); + f.finger[l]->calcVersionForLevel(l); + }*/ + } + } + + void getEnd( Finger& end ) { + Node* node = header; + for(int l=MaxLevels-1; l>=0; l--) { + Node* next; + while ( (next=node->getNext(l)) != NULL ) + node = next; + end.finger[l] = node; + } + end.level = 0; + // SOMEDAY: end.x? end.alreadyChecked? + /*end = Finger(header, (const uint8_t*)"\xff\xff\xff\xff\xff\xff", 6); + while (!end.finished()) + end.nextLevel();*/ + } +}; + +struct Action { + virtual void operator()() = 0; // self-destructs +}; +typedef Action* PAction; + +template +PAction action( F && f ) { + struct FAction : Action, F, FastAllocated { + FAction( F&& f ) : F(std::move(f)) {} + virtual void operator()() { F::operator()(); delete this; } + }; + return new FAction( std::move(f) ); +}; + +void workerThread( PAction* nextAction, Event* nextActionReady, int index, Event* whenFinished ) { + ASSERT(false); + /* + inThread( [nextAction,nextActionReady,index,whenFinished]()->Void { + g_seed = index*123; fastrand(); + setAffinity( index ); + while (true) { + try { + nextActionReady->block(); // auto-reset + Action* action = *nextAction; + *nextAction = 0; + if (!action) break; + + (*action)(); + } catch (Error& e) { + fprintf(stderr, "Error in worker thread: %s\n", e.what()); + } catch (...) { + fprintf(stderr, "Error in worker thread: %s\n", unknown_error().what()); + } + } + //cout << "Worker thread finished" << endl; + whenFinished->set(); + return Void(); + });*/ +} + +StringRef setK( Arena& arena, int i ) { + char t[ sizeof(i) ]; + *(int*)t = i; + + const int keySize = 16; + + char* ss = new (arena) char[ keySize ]; + for(int c=0; cset(); + } + // Wait for workers to terminate; otherwise can get crashes at shutdown time + for(int i=0; iblock(); + } + + SkipList versionHistory; + Key removalKey; + Version oldestVersion; + vector worker_nextAction; + vector worker_ready; + vector worker_finished; +}; + +ConflictSet* newConflictSet() { return new ConflictSet; } +void clearConflictSet( ConflictSet* cs, Version v ) { + SkipList(v).swap( cs->versionHistory ); +} +void destroyConflictSet(ConflictSet* cs) { + delete cs; +} + +ConflictBatch::ConflictBatch( ConflictSet* cs ) + : cs(cs), transactionCount(0) +{ +} + +ConflictBatch::~ConflictBatch() +{ +} + +struct TransactionInfo { + VectorRef< std::pair > readRanges; + VectorRef< std::pair > writeRanges; + bool tooOld; +}; + +void ConflictBatch::addTransaction( const CommitTransactionRef& tr ) { + int t = transactionCount++; + + Arena& arena = transactionInfo.arena(); + TransactionInfo* info = new (arena) TransactionInfo; + + if (tr.read_snapshot < cs->oldestVersion && tr.read_conflict_ranges.size()) { + info->tooOld = true; + } else { + info->tooOld = false; + info->readRanges.resize( arena, tr.read_conflict_ranges.size() ); + info->writeRanges.resize( arena, tr.write_conflict_ranges.size() ); + + vector &points = this->points; + for(int r=0; rreadRanges[r].first ) ); + //points.back().keyEnd = StringRef(buf,range.second); + points.push_back( KeyInfo( range.end, false, false, false, t, &info->readRanges[r].second ) ); + combinedReadConflictRanges.push_back( ReadConflictRange( range.begin, range.end, tr.read_snapshot, t ) ); + } + for(int r=0; rwriteRanges[r].first ) ); + points.push_back( KeyInfo( range.end, false, false, true, t, &info->writeRanges[r].second ) ); + } + } + + this->transactionInfo.push_back( arena, info ); +} + +class MiniConflictSet2 : NonCopyable { + vector values; +public: + explicit MiniConflictSet2( int size ) { + values.assign( size, false ); + } + void set( int begin, int end ) { + for(int i=begin; i values; // undefined when andValues is true for a range of values + vector orValues; + vector andValues; + MiniConflictSet2 debug; // SOMEDAY: Test on big ranges, eliminate this + + uint64_t bitMask(unsigned int bit){ // computes results for bit%word + return (((wordType)1) << ( bit & bucketMask )); // '&' unnecesary? + } + void setNthBit(vector &v, const unsigned int bit){ + v[bit>>bucketShift] |= bitMask(bit); + } + void clearNthBit(vector &v, const unsigned int bit){ + v[bit>>bucketShift] &= ~(bitMask(bit)); + } + bool getNthBit(const vector &v, const unsigned int bit){ + return (v[bit>>bucketShift] & bitMask(bit)) != 0; + } + int wordsForNBits(unsigned int bits){ + return (bits+((1<>bucketShift; + } + wordType highBits(int b){ // bits (b&bucketMask) and higher are 1 + #pragma warning(disable: 4146) + return -(wordType(1) << b); + #pragma warning(default: 4146) + } + wordType lowBits(int b){ // bits lower than b are 1 + return (wordType(1)< &v, int bitBegin, int bitEnd, bool fillMiddle){ + if (bitBegin >= bitEnd) return; + int beginWord = bitBegin>>bucketShift; + int lastWord = ((bitEnd+bucketMask) >> bucketShift) - 1; + if (beginWord == lastWord){ + v[beginWord] |= highBits(bitBegin) & lowBits2(bitEnd); + } else { + v[beginWord] |= highBits(bitBegin); + if (fillMiddle) + for(int w=beginWord+1;w &v, int bitBegin, int bitEnd, bool getMiddle) { + if (bitBegin >= bitEnd) return false; + int beginWord = bitBegin >> bucketShift; + int lastWord = ((bitEnd+bucketMask) >> bucketShift) - 1; + if (beginWord == lastWord) + return (v[beginWord] & highBits(bitBegin) & lowBits2(bitEnd)) != 0; + else { + if (getMiddle) + for(int w=beginWord+1; w>bucketShift; + int lastWord = ((end+bucketMask) >> bucketShift) - 1; + + setBits(values, begin, end, false); + setBits(andValues, beginWord+1, lastWord, true); + setBits(orValues, beginWord, lastWord+1, true); + } + + bool any(int begin, int end) { + bool a = orImpl(begin,end); + bool b = debug.any(begin,end); + ASSERT( a == b ); + return b; + } + + bool orImpl( int begin, int end ) { + if (begin == end) return false; + int beginWord = begin>>bucketShift; + int lastWord = ((end+bucketMask) >> bucketShift) - 1; + + return orBits( orValues, beginWord+1, lastWord, true ) || + getNthBit( andValues, beginWord ) || getNthBit( andValues, lastWord ) || + orBits( values, begin, end, false ); + } +}; + + +void ConflictBatch::checkIntraBatchConflicts() { + int index = 0; + for(int p=0; p& tooOldTransactions) { + for (int i = 0; itooOld) { + tooOldTransactions.push_back(i); + } + } +} + +void ConflictBatch::detectConflicts(Version now, Version newOldestVersion, vector& nonConflicting, vector* tooOldTransactions) { + double t = timer(); + sortPoints( points ); + //std::sort( combinedReadConflictRanges.begin(), combinedReadConflictRanges.end() ); + g_sort += timer()-t; + + transactionConflictStatus = new bool[ transactionCount ]; + memset(transactionConflictStatus, 0, transactionCount*sizeof(bool)); + + t = timer(); + checkReadConflictRanges(); + g_checkRead += timer()-t; + + t = timer(); + checkIntraBatchConflicts(); + g_checkBatch += timer()-t; + + t = timer(); + combineWriteConflictRanges(); + g_combine += timer()-t; + + t = timer(); + mergeWriteConflictRanges(now); + g_merge += timer()-t; + + for (int i = 0; i < transactionCount; i++) + { + if (!transactionConflictStatus[i]) + nonConflicting.push_back( i ); + if (tooOldTransactions && transactionInfo[i]->tooOld) + tooOldTransactions->push_back(i); + } + + delete[] transactionConflictStatus; + + t = timer(); + if (newOldestVersion > cs->oldestVersion) { + cs->oldestVersion = newOldestVersion; + SkipList::Finger finger; + int temp; + cs->versionHistory.find( &cs->removalKey, &finger, &temp, 1 ); + cs->versionHistory.removeBefore( cs->oldestVersion, finger, combinedWriteConflictRanges.size()*3 + 10 ); + cs->removalKey = finger.getValue(); + } + g_removeBefore += timer()-t; +} + +void ConflictBatch::checkReadConflictRanges() { + if (!combinedReadConflictRanges.size()) + return; + + if (PARALLEL_THREAD_COUNT) { + Event done[PARALLEL_THREAD_COUNT?PARALLEL_THREAD_COUNT:1]; + for(int t=0; tworker_nextAction[t] = action( [&,t] { +#pragma GCC diagnostic push +DISABLE_ZERO_DIVISION_FLAG + auto begin = &combinedReadConflictRanges[0] + t*combinedReadConflictRanges.size()/PARALLEL_THREAD_COUNT; + auto end = &combinedReadConflictRanges[0] + (t+1)*combinedReadConflictRanges.size()/PARALLEL_THREAD_COUNT; +#pragma GCC diagnostic pop + cs->versionHistory.detectConflicts( begin, end-begin, transactionConflictStatus ); + done[t].set(); + }); + cs->worker_ready[t]->set(); + } + for(int i=0; iversionHistory.detectConflicts( &combinedReadConflictRanges[0], combinedReadConflictRanges.size(), transactionConflictStatus ); + } +} + +void ConflictBatch::addConflictRanges(Version now, vector< pair >::iterator begin, vector< pair >::iterator end,SkipList* part) { + int count = end-begin; +#if 0 + //for(auto w = begin; w != end; ++w) + for(auto w = end-1; w != begin-1; --w) + part->addConflictRange( w->first, w->second, now ); +#else + static_assert( sizeof( begin[0] ) == sizeof(StringRef)*2, "Write Conflict Range type not convertible to two StringPtrs" ); + const StringRef* strings = reinterpret_cast( &*begin ); + int stringCount = count*2; + + static const int stripeSize = 16; + SkipList::Finger fingers[ stripeSize ]; + int temp[ stripeSize ]; + int stripes = (stringCount+stripeSize-1)/stripeSize; + + int ss = stringCount - (stripes-1)*stripeSize; + for(int s=stripes-1; s>=0; s--) { + part->find( &strings[s * stripeSize], &fingers[0], temp, ss ); + part->addConflictRanges( &fingers[0], ss/2, now ); + ss = stripeSize; + } +#endif +} + +void ConflictBatch::mergeWriteConflictRanges(Version now) { + if (!combinedWriteConflictRanges.size()) + return; + + if (PARALLEL_THREAD_COUNT) { + vector parts; + for (int i = 0; i < PARALLEL_THREAD_COUNT; i++) + parts.push_back(SkipList()); + + vector splits( parts.size()-1 ); + for(int s=0; sversionHistory.partition( splits.size() ? &splits[0] : NULL, splits.size(), &parts[0] ); + vector tstart(PARALLEL_THREAD_COUNT), tend(PARALLEL_THREAD_COUNT); + Event done[PARALLEL_THREAD_COUNT ? PARALLEL_THREAD_COUNT : 1]; + double before = timer(); + for(int t=0; tworker_nextAction[t] = action( [&,t] { + tstart[t] = timer(); + auto begin = combinedWriteConflictRanges.begin() + (t*combinedWriteConflictRanges.size()/parts.size()); + auto end = combinedWriteConflictRanges.begin() + ((t+1)*combinedWriteConflictRanges.size()/parts.size()); + + addConflictRanges(now, begin, end, &parts[t]); + + tend[t] = timer(); + done[t].set(); + }); + cs->worker_ready[t]->set(); + } + double launch = timer(); + for(int i=0; iversionHistory.concatenate( &parts[0], parts.size() ); + } else { + addConflictRanges( now, combinedWriteConflictRanges.begin(), combinedWriteConflictRanges.end(), &cs->versionHistory ); + } + + //for(auto w = combinedWriteConflictRanges.begin(); w != combinedWriteConflictRanges.end(); ++w) + // versionHistory.addConflictRange( w->first.begin(), w->first.size(), w->second.begin(), w->second.size(), now ); +} + +void ConflictBatch::combineWriteConflictRanges() +{ + int activeWriteCount = 0; + for(int i=0; i= %s\n", a, b); +} + +void sse4Test(){ + + tless("hello", "world"); + tless("a", "a"); + tless("world", "hello"); + tless("world", "worry"); + tless("worry", "world"); + tless("hello", "hello1"); + tless("hello1", "hello"); + + tless("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahello", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaworld"); + tless("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaworld", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahello"); + tless("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaworld", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaworry"); + tless("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaworry", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaworld"); + tless("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahello", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahello1"); + tless("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahello1", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahello"); + + char *a = "hello1worldthisisalonglonglongstring"; + char *b = "hello"; + __m128i aa = *(__m128i*)a; + __m128i bb = *(__m128i*)a; + + int res = _mm_cmpestri(aa, 2, bb, 2, _SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_EACH | _SIDD_NEGATIVE_POLARITY | _SIDD_LEAST_SIGNIFICANT ); + + cout << res << endl; + +} +*/ + +void miniConflictSetTest() { + for(int i=0; i<2000000; i++) { + int size = 64*5; // Also run 64*64*5 to test multiple words of andValues and orValues + MiniConflictSet mini(size); + for(int j=0; j<2; j++) { + int a = g_random->randomInt(0, size); + int b = g_random->randomInt(a, size); + mini.set( a, b ); + } + for(int j=0; j<4; j++) { + int a = g_random->randomInt(0, size); + int b = g_random->randomInt(a, size); + mini.any( a, b ); // Tests correctness internally + } + } + printf("miniConflictSetTest complete\n"); +} + +void skipListTest() { + printf("Skip list test\n"); + + //sse4Test(); + + //A test case that breaks the old operator< + //KeyInfo a( LiteralStringRef("hello"), true, false, true, -1 ); + //KeyInfo b( LiteralStringRef("hello\0"), false, false, false, 0 ); + + miniConflictSetTest(); + + + setAffinity(0); + //showNumaStatus(); + + double start; + + ConflictSet* cs = newConflictSet(); + + Arena testDataArena; + VectorRef< VectorRef > testData; + testData.resize(testDataArena, 500); + vector> success( testData.size() ); + vector> success2( testData.size() ); + for(int i=0; irandomInt(0, 20000000); + int key2 = key + 1 + g_random->randomInt(0, 10); + testData[i][j] = KeyRangeRef( + setK( testDataArena, key ), + setK( testDataArena, key2 ) ); + } + } + printf("Test data generated (%d)\n", g_random->randomInt(0,100000)); + printf(" %d threads, %d batches, %d/batch\n", PARALLEL_THREAD_COUNT, testData.size(), testData[0].size()); + + printf("Running\n"); + + int readCount = 1, writeCount = 1; + int cranges = 0, tcount = 0; + + start = timer(); + vector> nonConflict( testData.size() ); + for(int i=0; i trs; + double t = timer(); + for(int j=0; j+readCount+writeCount<=testData[i].size(); j+=readCount+writeCount) { + CommitTransactionRef tr; + for(int k=0; kgetMetric().name().c_str(), skc[c]->getMetric().formatted().c_str()); + } + + //showNumaStatus(); + + printf("%d entries in version history\n", cs->versionHistory.count()); + + /*start = timer(); + vector> nonConflict2( testData.size() ); + SlowConflictSet scs; + Standalone> ranges; + ranges.resize( ranges.arena(), 1 ); + + for(int i=0; i(), i + 50 ); + } + } + } + printf("Old conflict set: %0.3f sec\n", timer()-start); + + int aminusb=0, bminusa=0, atotal=0; + for(int i=0; i a( testData[i].size() ), b( testData[i].size() ); + for(int j=0; j +#include "CoordinationInterface.h" +#include "DataDistribution.h" +#include "flow/UnitTest.h" +#include "QuietDatabase.h" +#include "RecoveryState.h" + +const char* RecoveryStatus::names[] = { + "reading_coordinated_state", "locking_coordinated_state", "locking_old_transaction_servers", "reading_transaction_system_state", + "configuration_missing", "configuration_never_created", "configuration_invalid", + "recruiting_transaction_servers", "initializing_transaction_servers", "recovery_transaction", + "writing_coordinated_state", "fully_recovered" +}; +static_assert( sizeof(RecoveryStatus::names) == sizeof(RecoveryStatus::names[0])*RecoveryStatus::END, "RecoveryStatus::names[] size" ); +const char* RecoveryStatus::descriptions[] = { + // reading_coordinated_state + "Requesting information from coordination servers. Verify that a majority of coordination server processes are active.", + // locking_coordinated_state + "Locking coordination state. Verify that a majority of coordination server processes are active.", + // locking_old_transaction_servers + "Locking old transaction servers. Verify that a least one transaction server from the previous generation is running.", + // reading_transaction_system_state + "Recovering transaction server state. Verify that the transaction server processes are active.", + // configuration_missing + "There appears to be a database, but its configuration does not appear to be initialized.", + // configuration_never_created + "The coordinator(s) have no record of this database. Either the coordinator addresses are incorrect, the coordination state on those machines is missing, or no database has been created.", + // configuration_invalid + "The database configuration is invalid. Set a new, valid configuration to recover the database.", + // recruiting_transaction_servers + "Recruiting new transaction servers.", + // initializing_transaction_servers + "Initializing new transaction servers and recovering transaction logs.", + // recovery_transaction + "Performing recovery transaction.", + // writing_coordinated_state + "Writing coordinated state. Verify that a majority of coordination server processes are active.", + // fully_recovered + "Recovery complete." +}; +static_assert( sizeof(RecoveryStatus::descriptions) == sizeof(RecoveryStatus::descriptions[0])*RecoveryStatus::END, "RecoveryStatus::descriptions[] size" ); + +// From Ratekeeper.actor.cpp +extern int limitReasonEnd; +extern const char* limitReasonName[]; +extern const char* limitReasonDesc[]; + +// Returns -1 if it fails to find a quoted string at the start of xml; returns the position beyond the close quote +// If decoded is not NULL, writes the decoded attribute value there +int decodeQuotedAttributeValue( StringRef xml, std::string* decoded ) { + if (decoded) decoded->clear(); + if (!xml.size() || xml[0] != '"') return -1; + int pos = 1; + + loop { + if (pos == xml.size()) return -1; // No closing quote + if (xml[pos]=='"') { pos++; break; } // Success + + uint8_t out = xml[pos]; + if (xml[pos] == '&') { + if (xml.substr(pos).startsWith(LiteralStringRef("&"))) { out = '&'; pos += 5; } + else if (xml.substr(pos).startsWith(LiteralStringRef("<"))) { out = '<'; pos += 4; } + else if (xml.substr(pos).startsWith(LiteralStringRef("""))) { out = '"'; pos += 6; } + else return -1; + } else + pos++; + if (decoded) decoded->push_back(out); + } + + return pos; +} + +// return false on failure; outputs decoded attribute value to `ret` +bool tryExtractAttribute( StringRef expanded, StringRef attributeToExtract, std::string& ret ) { + // This is only expected to parse the XML that Trace.cpp actually generates; we haven't looked at the standard to even find out what it doesn't try to do + + int pos = 0; + // Consume '<' + if (pos == expanded.size() || expanded[pos] != '<') return false; + pos++; + // Consume tag name + while (pos != expanded.size() && expanded[pos] != ' ' && expanded[pos] != '/' && expanded[pos] != '>') pos++; + + while (pos != expanded.size() && expanded[pos] != '>' && expanded[pos] != '/') { + // Consume whitespace + while (pos != expanded.size() && expanded[pos] == ' ') pos++; + + // We should be looking at an attribute or the end of the string; find '=' at the end of the attribute, if any + int eq_or_end = pos; + while (eq_or_end != expanded.size() && expanded[eq_or_end]!='=' && expanded[eq_or_end]!='>') eq_or_end++; + + if ( expanded.substr(pos, eq_or_end-pos) == attributeToExtract ) { + // Found the attribute we want; decode the value + int end = decodeQuotedAttributeValue(expanded.substr(eq_or_end+1), &ret); + if (end<0) { ret.clear(); return false; } + return true; + } + + // We don't want this attribute, but we need to skip over its value + // It looks like this *could* just be a scan for '"' characters + int end = decodeQuotedAttributeValue(expanded.substr(eq_or_end+1), NULL); + if (end<0) return false; + pos = (eq_or_end+1)+end; + } + return false; +} + +// Throws attribute_not_found if the key is not found +std::string extractAttribute( StringRef expanded, StringRef attributeToExtract ) { + std::string ret; + if (!tryExtractAttribute(expanded, attributeToExtract, ret)) + throw attribute_not_found(); + return ret; +} +std::string extractAttribute( std::string const& expanded, std::string const& attributeToExtract ) { + return extractAttribute(StringRef(expanded), StringRef(attributeToExtract)); +} + +TEST_CASE("fdbserver/Status/extractAttribute/basic") { + std::string a; + + ASSERT( tryExtractAttribute( + LiteralStringRef(""), + LiteralStringRef("A"), + a) && a == LiteralStringRef("\"a\"")); + + ASSERT( tryExtractAttribute( + LiteralStringRef(""), + LiteralStringRef("B"), + a) && a == LiteralStringRef("\\") ); + + ASSERT( tryExtractAttribute( + LiteralStringRef(""), + LiteralStringRef("Version"), + a) && a == LiteralStringRef("3.0.0-PRERELEASE") ); + + ASSERT( !tryExtractAttribute( + LiteralStringRef(""), + LiteralStringRef("ersion"), + a) ); + + return Void(); +} + +TEST_CASE("fdbserver/Status/extractAttribute/fuzz") { + // This is just looking for anything that crashes or infinite loops + std::string out; + for(int i=0; i<100000; i++) + { + std::string s = ""; + s[ g_random->randomInt(0, s.size()) ] = g_random->randomChoice(LiteralStringRef("\" =q0\\&")); + tryExtractAttribute(s, LiteralStringRef("Version"), out); + } + return Void(); +} + +struct WorkerEvents : std::map {}; + +ACTOR static Future< Optional > latestEventOnWorker(WorkerInterface worker, std::string eventName) { + try { + EventLogRequest req = eventName.size() > 0 ? EventLogRequest(Standalone(eventName)) : EventLogRequest(); + ErrorOr> eventTrace = wait( errorOr(timeoutError(worker.eventLogRequest.getReply(req), 2.0))); + + if (eventTrace.isError()){ + return Optional(); + } + return eventTrace.get().toString(); + } + catch (Error &e){ + if (e.code() == error_code_actor_cancelled) + throw; + return Optional(); + } +} + +ACTOR static Future< Optional< std::pair> > > latestEventOnWorkers(std::vector> workers, std::string eventName) { + try { + state vector>>> eventTraces; + for (int c = 0; c < workers.size(); c++) { + EventLogRequest req = eventName.size() > 0 ? EventLogRequest(Standalone(eventName)) : EventLogRequest(); + eventTraces.push_back(errorOr(timeoutError(workers[c].first.eventLogRequest.getReply(req), 2.0))); + } + + Void _ = wait(waitForAll(eventTraces)); + + std::set failed; + WorkerEvents results; + + for (int i = 0; i < eventTraces.size(); i++) { + ErrorOr> v = eventTraces[i].get(); + if (v.isError()){ + failed.insert(workers[i].first.address().toString()); + results[workers[i].first.address()] = ""; + } + else { + results[workers[i].first.address()] = v.get().toString(); + } + } + + std::pair> val; + val.first = results; + val.second = failed; + + return val; + } + catch (Error &e){ + ASSERT(e.code() == error_code_actor_cancelled); // All errors should be filtering through the errorOr actor above + throw; + } +} +static Future< Optional< std::pair> > > latestErrorOnWorkers(std::vector> workers) { + return latestEventOnWorkers( workers, "" ); +} + +static Optional> getWorker(std::vector> const& workers, NetworkAddress const& address) { + try { + for (int c = 0; c < workers.size(); c++) + if (address == workers[c].first.address()) + return workers[c]; + return Optional>(); + } + catch (Error &e){ + return Optional>(); + } +} + +static Optional> getWorker(std::map> const& workersMap, NetworkAddress const& address) { + auto itr = workersMap.find(address); + if(itr == workersMap.end()) { + return Optional>(); + } + + return itr->second; +} + +static StatusObject makeCounter(double hz=0.0, double r=0.0, int64_t c=0) { + StatusObject out; + out["hz"] = hz; + out["roughness"] = r; + out["counter"] = c; + return out; +} + +static StatusObject parseCounter(std::string const& s) { + // Parse what traceCounters() in Stats.actor.cpp formats + double hz = 0.0, roughness = 0.0; + long long counter = 0; + sscanf(s.c_str(), "%lf %lf %lld", &hz, &roughness, &counter); + return makeCounter(hz, roughness, counter); +} + +static StatusObject addCounters(StatusObject c1, StatusObject c2) { + // "add" the given counter objects. Roughness is averaged weighted by rate. + + double c1hz = c1["hz"].get_real(); + double c2hz = c2["hz"].get_real(); + double c1r = c1["roughness"].get_real(); + double c2r = c2["roughness"].get_real(); + double c1c = c1["counter"].get_real(); + double c2c = c2["counter"].get_real(); + + return makeCounter( + c1hz+c2hz, + (c1hz + c2hz) ? (c1r*c1hz + c2r*c2hz) / (c1hz + c2hz) : 0.0, + c1c+c2c + ); +} + +static double parseDouble(std::string const& s, bool permissive = false) { + double d = 0; + int consumed = 0; + int r = sscanf(s.c_str(), "%lf%n", &d, &consumed); + if (r == 1 && (consumed == s.size() || permissive)) + return d; + throw attribute_not_found(); +} + +static int parseInt(std::string const& s, bool permissive = false) { + long long int iLong = 0; + int consumed = 0; + int r = sscanf(s.c_str(), "%lld%n", &iLong, &consumed); + if (r == 1 && (consumed == s.size() || permissive)){ + if (std::numeric_limits::min() <= iLong && iLong <= std::numeric_limits::max()) + return (int)iLong; // Downcast definitely safe + else + throw attribute_too_large(); + } + throw attribute_not_found(); +} + +static int64_t parseInt64(std::string const& s, bool permissive = false) { + long long int i = 0; + int consumed = 0; + int r = sscanf(s.c_str(), "%lld%n", &i, &consumed); + if (r == 1 && (consumed == s.size() || permissive)) + return i; + throw attribute_not_found(); +} + +static StatusObject getLocalityInfo(const LocalityData& locality) { + StatusObject localityObj; + + for(auto it = locality._data.begin(); it != locality._data.end(); it++) { + if(it->second.present()) { + localityObj[it->first.toString()] = it->second.get().toString(); + } + else { + localityObj[it->first.toString()] = json_spirit::mValue(); + } + } + + return localityObj; +} + +static StatusObject getError(std::string error) { + StatusObject statusObj; + try { + if (error.size()) { + double time = atof(extractAttribute(error, "Time").c_str()); + statusObj["time"] = time; + + statusObj["raw_log_message"] = error; + + std::string type = extractAttribute(error, "Type"); + statusObj["type"] = type; + + std::string description = type; + std::string errorName; + if (tryExtractAttribute(error, LiteralStringRef("Error"), errorName)) { + statusObj["name"] = errorName; + description += ": " + errorName; + } + else + statusObj["name"] = "process_error"; + + struct tm* timeinfo; + time_t t = (time_t)time; + timeinfo = localtime(&t); + char buffer[128]; + strftime(buffer, 128, "%c", timeinfo); + description += " at " + std::string(buffer); + + statusObj["description"] = description; + } + } + catch (Error &e){ + TraceEvent(SevError, "StatusGetErrorError").error(e).detail("RawError", error); + } + return statusObj; +} + +static StatusObject machineStatusFetcher(WorkerEvents mMetrics, vector> workers, Optional configuration, std::set *incomplete_reasons) { + StatusObject machineMap; + double metric; + int failed = 0; + + // map from machine networkAddress to datacenter ID + WorkerEvents dcIds; + std::map locality; + + for (auto worker : workers){ + locality[worker.first.address()] = worker.first.locality; + if (worker.first.locality.dcId().present()) + dcIds[worker.first.address()] = worker.first.locality.dcId().get().printable(); + } + + for(auto it = mMetrics.begin(); it != mMetrics.end(); it++) { + + if (!it->second.size()){ + continue; + } + + StatusObject statusObj; // Represents the status for a machine + std::string event = it->second; + + try { + std::string address = toIPString(it->first.ip); + // We will use the "physical" caluculated machine ID here to limit exposure to machineID repurposing + std::string machineId = extractAttribute(event, "MachineID"); + + // If this machine ID does not already exist in the machineMap, add it + if (!machineMap.count(machineId)) { + statusObj["machine_id"] = machineId; + + if (dcIds.count(it->first)){ + statusObj["datacenter_id"] = dcIds[it->first]; + } + + if(locality.count(it->first)) { + statusObj["locality"] = getLocalityInfo(locality[it->first]); + } + + statusObj["address"] = address; + + StatusObject memoryObj; + + metric = parseDouble(extractAttribute(event, "TotalMemory")); + memoryObj["total_bytes"] = metric; + + metric = parseDouble(extractAttribute(event, "CommittedMemory")); + memoryObj["committed_bytes"] = metric; + + metric = parseDouble(extractAttribute(event, "AvailableMemory")); + memoryObj["free_bytes"] = metric; + + statusObj["memory"] = memoryObj; + + StatusObject cpuObj; + + metric = parseDouble(extractAttribute(event, "CPUSeconds")); + double cpu_seconds = metric; + + metric = parseDouble(extractAttribute(event, "Elapsed")); + double elapsed = metric; + + if (elapsed > 0){ + cpuObj["logical_core_utilization"] = std::max(0.0, std::min(cpu_seconds / elapsed, 1.0)); + } + + statusObj["cpu"] = cpuObj; + + StatusObject networkObj; + + metric = parseDouble(extractAttribute(event, "MbpsSent")); + StatusObject megabits_sent; + megabits_sent["hz"] = metric; + networkObj["megabits_sent"] = megabits_sent; + + metric = parseDouble(extractAttribute(event, "MbpsReceived")); + StatusObject megabits_received; + megabits_received["hz"] = metric; + networkObj["megabits_received"] = megabits_received; + + metric = parseDouble(extractAttribute(event, "RetransSegs")); + StatusObject retransSegsObj; + if (elapsed > 0){ + retransSegsObj["hz"] = metric / elapsed; + } + networkObj["tcp_segments_retransmitted"] = retransSegsObj; + + statusObj["network"] = networkObj; + + if (configuration.present()){ + statusObj["excluded"] = true; // Will be set to false below if this or any later process is not excluded + } + + statusObj["contributing_workers"] = 0; + + machineMap[machineId] = statusObj; + } + if (configuration.present() && !configuration.get().isExcludedServer(it->first)) + machineMap[machineId].get_obj()["excluded"] = false; + + machineMap[machineId].get_obj()["contributing_workers"] = machineMap[machineId].get_obj()["contributing_workers"].get_int() + 1; + } + catch (Error& e) { + ++failed; + } + } + + if(failed > 0) + incomplete_reasons->insert("Cannot retrieve all machine status information."); + + return machineMap; +} + +struct MachineMemoryInfo { + double memoryUsage; + double numProcesses; + + MachineMemoryInfo() : memoryUsage(0), numProcesses(0) {} + + bool valid() { return memoryUsage >= 0; } + void invalidate() { memoryUsage = -1; } +}; + +struct RolesInfo { + std::multimap roles; + StatusObject& addRole( NetworkAddress address, std::string const& role, UID id) { + StatusObject obj; + obj["id"] = id.shortString(); + obj["role"] = role; + return roles.insert( make_pair(address, obj ))->second; + } + StatusObject& addRole(std::string const& role, StorageServerInterface& iface, std::string const& metrics, Version maxTLogVersion) { + StatusObject obj; + obj["id"] = iface.id().shortString(); + obj["role"] = role; + try { + obj["stored_bytes"] = parseInt64(extractAttribute(metrics, "bytesStored")); + obj["kvstore_used_bytes"] = parseInt64(extractAttribute(metrics, "kvstoreBytesUsed")); + obj["kvstore_free_bytes"] = parseInt64(extractAttribute(metrics, "kvstoreBytesFree")); + obj["kvstore_available_bytes"] = parseInt64(extractAttribute(metrics, "kvstoreBytesAvailable")); + obj["kvstore_total_bytes"] = parseInt64(extractAttribute(metrics, "kvstoreBytesTotal")); + obj["input_bytes"] = parseCounter(extractAttribute(metrics, "bytesInput")); + obj["durable_bytes"] = parseCounter(extractAttribute(metrics, "bytesDurable")); + obj["query_queue_max"] = parseInt(extractAttribute(metrics, "QueryQueueMax")); + obj["finished_queries"] = parseCounter(extractAttribute(metrics, "finishedQueries")); + + Version version = parseInt64(extractAttribute(metrics, "version")); + obj["data_version"] = version; + + if(maxTLogVersion > 0) { + obj["data_version_lag"] = std::max(0, maxTLogVersion - version); + } + + } catch (Error& e) { + if(e.code() != error_code_attribute_not_found) + throw e; + } + return roles.insert( make_pair(iface.address(), obj ))->second; + } + StatusObject& addRole(std::string const& role, TLogInterface& iface, std::string const& metrics) { + StatusObject obj; + obj["id"] = iface.id().shortString(); + obj["role"] = role; + try { + obj["kvstore_used_bytes"] = parseInt64(extractAttribute(metrics, "kvstoreBytesUsed")); + obj["kvstore_free_bytes"] = parseInt64(extractAttribute(metrics, "kvstoreBytesFree")); + obj["kvstore_available_bytes"] = parseInt64(extractAttribute(metrics, "kvstoreBytesAvailable")); + obj["kvstore_total_bytes"] = parseInt64(extractAttribute(metrics, "kvstoreBytesTotal")); + obj["queue_disk_used_bytes"] = parseInt64(extractAttribute(metrics, "queueDiskBytesUsed")); + obj["queue_disk_free_bytes"] = parseInt64(extractAttribute(metrics, "queueDiskBytesFree")); + obj["queue_disk_available_bytes"] = parseInt64(extractAttribute(metrics, "queueDiskBytesAvailable")); + obj["queue_disk_total_bytes"] = parseInt64(extractAttribute(metrics, "queueDiskBytesTotal")); + obj["input_bytes"] = parseCounter(extractAttribute(metrics, "bytesInput")); + obj["durable_bytes"] = parseCounter(extractAttribute(metrics, "bytesDurable")); + obj["data_version"] = parseInt64(extractAttribute(metrics, "version")); + } catch (Error& e) { + if(e.code() != error_code_attribute_not_found) + throw e; + } + return roles.insert( make_pair(iface.address(), obj ))->second; + } + template + StatusObject& addRole(std::string const& role, InterfaceType& iface) { + return addRole(iface.address(), role, iface.id()); + } + StatusArray getStatusForAddress( NetworkAddress a ) { + StatusArray v; + auto it = roles.lower_bound(a); + while (it != roles.end() && it->first == a) { + v.push_back(it->second); + ++it; + } + return v; + } +}; + +ACTOR static Future processStatusFetcher( + Reference> db, + std::vector> workers, + WorkerEvents pMetrics, + WorkerEvents mMetrics, + WorkerEvents errors, + WorkerEvents traceFileOpenErrors, + WorkerEvents programStarts, + std::map processIssues, + vector> storageServers, + vector> tLogs, + Database cx, + Optional configuration, + std::set *incomplete_reasons) { + + // Array to hold one entry for each process + state StatusObject processMap; + state double metric; + + // construct a map from a process address to a status object containing a trace file open error + // this is later added to the messages subsection + state std::map tracefileOpenErrorMap; + state WorkerEvents::iterator traceFileErrorsItr; + for(traceFileErrorsItr = traceFileOpenErrors.begin(); traceFileErrorsItr != traceFileOpenErrors.end(); ++traceFileErrorsItr) { + Void _ = wait(yield()); + if (traceFileErrorsItr->second.size()){ + try { + // Have event string, parse it and turn it into a message object describing the trace file opening error + std::string event = traceFileErrorsItr->second; + std::string fileName = extractAttribute(event, "Filename"); + StatusObject msgObj = makeMessage("file_open_error", format("Could not open file '%s' (%s).", fileName.c_str(), extractAttribute(event, "Error").c_str()).c_str()); + msgObj["file_name"] = fileName; + + // Map the address of the worker to the error message object + tracefileOpenErrorMap[traceFileErrorsItr->first.toString()] = msgObj; + } + catch(Error &e) { + incomplete_reasons->insert("file_open_error details could not be retrieved"); + } + } + } + + state std::map>, MachineMemoryInfo> machineMemoryUsage; + state std::vector>::iterator workerItr; + for(workerItr = workers.begin(); workerItr != workers.end(); ++workerItr) { + Void _ = wait(yield()); + state std::map>, MachineMemoryInfo>::iterator memInfo = machineMemoryUsage.insert(std::make_pair(workerItr->first.locality.machineId(), MachineMemoryInfo())).first; + try { + ASSERT(pMetrics.count(workerItr->first.address())); + std::string processMetrics = pMetrics[workerItr->first.address()]; + + if(memInfo->second.valid()) { + if(processMetrics.size() > 0) { + memInfo->second.memoryUsage += parseDouble(extractAttribute(processMetrics, "Memory")); + ++memInfo->second.numProcesses; + } + else + memInfo->second.invalidate(); + } + } + catch(Error &e) { + memInfo->second.invalidate(); + } + } + + state RolesInfo roles; + + roles.addRole("master", db->get().master); + roles.addRole("cluster_controller", db->get().clusterInterface.clientInterface); + + state Reference proxies = cx->getMasterProxies(); + if (proxies) { + state int proxyIndex; + for(proxyIndex = 0; proxyIndex < proxies->size(); proxyIndex++) { + roles.addRole( "proxy", proxies->getInterface(proxyIndex) ); + Void _ = wait(yield()); + } + } + + state std::vector>::iterator log; + state Version maxTLogVersion = 0; + for(log = tLogs.begin(); log != tLogs.end(); ++log) { + StatusObject const& roleStatus = roles.addRole( "log", log->first, log->second ); + if(roleStatus.count("data_version") > 0) { + maxTLogVersion = std::max(maxTLogVersion, roleStatus.at("data_version").get_int64()); + } + Void _ = wait(yield()); + } + + state std::vector>::iterator ss; + state std::map ssLag; + for(ss = storageServers.begin(); ss != storageServers.end(); ++ss) { + StatusObject const& roleStatus = roles.addRole( "storage", ss->first, ss->second, maxTLogVersion ); + if(roleStatus.count("data_version_lag") > 0) { + ssLag[ss->first.address()] = roleStatus.at("data_version_lag").get_int64(); + } + Void _ = wait(yield()); + } + + state std::vector::const_iterator res; + state std::vector resolvers = db->get().resolvers; + for(res = resolvers.begin(); res != resolvers.end(); ++res) { + roles.addRole( "resolver", *res ); + Void _ = wait(yield()); + } + + for(workerItr = workers.begin(); workerItr != workers.end(); ++workerItr) { + Void _ = wait(yield()); + state StatusObject statusObj; + try { + ASSERT(pMetrics.count(workerItr->first.address())); + + processMap[printable(workerItr->first.locality.processId())] = StatusObject(); + + NetworkAddress address = workerItr->first.address(); + std::string event = pMetrics[workerItr->first.address()]; + statusObj["address"] = address.toString(); + StatusObject memoryObj; + + if (event.size() > 0) { + std::string zoneID = extractAttribute(event, "ZoneID"); + statusObj["fault_domain"] = zoneID; + + std::string MachineID = extractAttribute(event, "MachineID"); + statusObj["machine_id"] = MachineID; + + statusObj["locality"] = getLocalityInfo(workerItr->first.locality); + + statusObj["uptime_seconds"] = parseDouble(extractAttribute(event, "UptimeSeconds")); + + metric = parseDouble(extractAttribute(event, "CPUSeconds")); + double cpu_seconds = metric; + + // rates are calculated over the last elapsed seconds + metric = parseDouble(extractAttribute(event, "Elapsed")); + double elapsed = metric; + + metric = parseDouble(extractAttribute(event, "DiskIdleSeconds")); + double diskIdleSeconds = metric; + + metric = parseDouble(extractAttribute(event, "DiskReads")); + double diskReads = metric; + + metric = parseDouble(extractAttribute(event, "DiskWrites")); + double diskWrites = metric; + + uint64_t diskReadsCount = parseInt64(extractAttribute(event, "DiskReadsCount")); + + uint64_t diskWritesCount = parseInt64(extractAttribute(event, "DiskWritesCount")); + + metric = parseDouble(extractAttribute(event, "DiskWriteSectors")); + double diskWriteSectors = metric; + + metric = parseDouble(extractAttribute(event, "DiskReadSectors")); + double diskReadSectors = metric; + + StatusObject diskObj; + if (elapsed > 0){ + StatusObject cpuObj; + cpuObj["usage_cores"] = std::max(0.0, cpu_seconds / elapsed); + statusObj["cpu"] = cpuObj; + + diskObj["busy"] = std::max(0.0, std::min((elapsed - diskIdleSeconds) / elapsed, 1.0)); + + StatusObject readsObj; + readsObj["counter"] = diskReadsCount; + if (elapsed > 0) + readsObj["hz"] = diskReads / elapsed; + readsObj["sectors"] = diskReadSectors; + + StatusObject writesObj; + writesObj["counter"] = diskWritesCount; + if (elapsed > 0) + writesObj["hz"] = diskWrites / elapsed; + writesObj["sectors"] = diskWriteSectors; + + diskObj["reads"] = readsObj; + diskObj["writes"] = writesObj; + } + + diskObj["total_bytes"] = parseInt64(extractAttribute(event, "DiskTotalBytes")); + diskObj["free_bytes"] = parseInt64(extractAttribute(event, "DiskFreeBytes")); + statusObj["disk"] = diskObj; + + StatusObject networkObj; + + networkObj["current_connections"] = parseInt64(extractAttribute(event, "CurrentConnections")); + StatusObject connections_established; + connections_established["hz"] = parseDouble(extractAttribute(event, "ConnectionsEstablished")); + networkObj["connections_established"] = connections_established; + StatusObject connections_closed; + connections_closed["hz"] = parseDouble(extractAttribute(event, "ConnectionsClosed")); + networkObj["connections_closed"] = connections_closed; + StatusObject connection_errors; + connection_errors["hz"] = parseDouble(extractAttribute(event, "ConnectionErrors")); + networkObj["connection_errors"] = connection_errors; + + metric = parseDouble(extractAttribute(event, "MbpsSent")); + StatusObject megabits_sent; + megabits_sent["hz"] = metric; + networkObj["megabits_sent"] = megabits_sent; + + metric = parseDouble(extractAttribute(event, "MbpsReceived")); + StatusObject megabits_received; + megabits_received["hz"] = metric; + networkObj["megabits_received"] = megabits_received; + + statusObj["network"] = networkObj; + + metric = parseDouble(extractAttribute(event, "Memory")); + memoryObj["used_bytes"] = metric; + } + + if (programStarts.count(address)) { + auto const& psxml = programStarts.at(address); + int64_t memLimit = parseInt64(extractAttribute(psxml, "MemoryLimit")); + memoryObj["limit_bytes"] = memLimit; + } + + // if this process address is in the machine metrics + if (mMetrics.count(address) && mMetrics[address].size()){ + double availableMemory; + availableMemory = parseDouble(extractAttribute(mMetrics[address], "AvailableMemory")); + + auto machineMemInfo = machineMemoryUsage[workerItr->first.locality.machineId()]; + if (machineMemInfo.valid()) { + ASSERT(machineMemInfo.numProcesses > 0); + int64_t memory = (availableMemory + machineMemInfo.memoryUsage) / machineMemInfo.numProcesses; + memoryObj["available_bytes"] = std::max(memory, 0); + } + } + + statusObj["memory"] = memoryObj; + + StatusArray messages; + + if (errors.count(address) && errors[address].size()) + // returns status object with type and time of error + messages.push_back(getError(errors.at(address))); + + // string of address used so that other fields of a NetworkAddress are not compared + std::string strAddress = address.toString(); + + // If this process has a process issue, identified by strAddress, then add it to messages array + if (processIssues.count(strAddress)){ + messages.push_back(processIssues[strAddress]); + } + + // If this process had a trace file open error, identified by strAddress, then add it to messages array + if (tracefileOpenErrorMap.count(strAddress)){ + messages.push_back(tracefileOpenErrorMap[strAddress]); + } + + if(ssLag[address] > 60 * SERVER_KNOBS->VERSIONS_PER_SECOND) { + messages.push_back(makeMessage("storage_server_lagging", format("Storage server lagging by %ld seconds.", ssLag[address] / SERVER_KNOBS->VERSIONS_PER_SECOND).c_str())); + } + + // Store the message array into the status object that represents the worker process + statusObj["messages"] = messages; + + // Get roles for the worker's address as an array of objects + statusObj["roles"] = roles.getStatusForAddress(address); + + if (programStarts.count(address)) { + auto const& psxml = programStarts.at(address); + + std::string version; + if (tryExtractAttribute(psxml, LiteralStringRef("Version"), version)) + statusObj["version"] = version; + + std::string commandLine; + if (tryExtractAttribute(psxml, LiteralStringRef("CommandLine"), commandLine)) + statusObj["command_line"] = commandLine; + } + + if (configuration.present()){ + statusObj["excluded"] = configuration.get().isExcludedServer(address); + } + + statusObj["class_type"] = workerItr->second.toString(); + statusObj["class_source"] = workerItr->second.sourceString(); + + } + catch (Error& e){ + // Something strange occurred, process list is incomplete but what was built so far, if anything, will be returned. + incomplete_reasons->insert("Cannot retrieve all process status information."); + } + + processMap[printable(workerItr->first.locality.processId())] = statusObj; + } + return processMap; +} + +static StatusObject clientStatusFetcher(ClientVersionMap clientVersionMap) { + StatusObject clientStatus; + + clientStatus["count"] = (int64_t)clientVersionMap.size(); + + std::map> clientVersions; + for(auto client : clientVersionMap) { + for(auto ver : client.second) { + clientVersions[ver].insert(client.first); + } + } + + StatusArray versionsArray = StatusArray(); + for(auto cv : clientVersions) { + StatusObject ver; + ver["count"] = (int64_t)cv.second.size(); + ver["client_version"] = cv.first.clientVersion.toString(); + ver["protocol_version"] = cv.first.protocolVersion.toString(); + ver["source_version"] = cv.first.sourceVersion.toString(); + + StatusArray clients = StatusArray(); + for(auto client : cv.second) { + clients.push_back(client.toString()); + } + + ver["clients"] = clients; + versionsArray.push_back(ver); + } + + if(versionsArray.size() > 0) { + clientStatus["supported_versions"] = versionsArray; + } + + return clientStatus; +} + +ACTOR static Future recoveryStateStatusFetcher(std::pair mWorker, std::string dbName, int workerCount, std::set *incomplete_reasons) { + state StatusObject message; + + try { + Standalone md = wait( timeoutError(mWorker.first.eventLogRequest.getReply( EventLogRequest(StringRef(dbName+"/MasterRecoveryState") ) ), 1.0) ); + state int mStatusCode = parseInt( extractAttribute(md, LiteralStringRef("StatusCode")) ); + if (mStatusCode < 0 || mStatusCode >= RecoveryStatus::END) + throw attribute_not_found(); + + message = makeMessage(RecoveryStatus::names[mStatusCode], RecoveryStatus::descriptions[mStatusCode]); + + // Add additional metadata for certain statuses + if (mStatusCode == RecoveryStatus::recruiting_transaction_servers) { + int requiredLogs = atoi( extractAttribute(md, LiteralStringRef("RequiredTLogs")).c_str() ); + int requiredProxies = atoi( extractAttribute(md, LiteralStringRef("RequiredProxies")).c_str() ); + int requiredResolvers = atoi( extractAttribute(md, LiteralStringRef("RequiredResolvers")).c_str() ); + //int requiredProcesses = std::max(requiredLogs, std::max(requiredResolvers, requiredProxies)); + //int requiredMachines = std::max(requiredLogs, 1); + + message["required_logs"] = requiredLogs; + message["required_proxies"] = requiredProxies; + message["required_resolvers"] = requiredResolvers; + } + // TODO: time_in_recovery: 0.5 + // time_in_state: 0.1 + + } catch (Error &e){ + if (e.code() == error_code_actor_cancelled) + throw; + } + + // If recovery status name is not know, status is incomplete + if (!message.count("name")) + incomplete_reasons->insert("Recovery Status unavailable."); + + return message; +} + +ACTOR static Future doGrvProbe(Transaction *tr, Optional priority = Optional()) { + state double start = timer_monotonic(); + + loop { + try { + if(priority.present()) { + tr->setOption(priority.get()); + } + + Version _ = wait(tr->getReadVersion()); + return timer_monotonic() - start; + } + catch(Error &e) { + Void _ = wait(tr->onError(e)); + } + } +} + +ACTOR static Future doReadProbe(Future grvProbe, Transaction *tr) { + ErrorOr grv = wait(errorOr(grvProbe)); + if(grv.isError()) { + throw grv.getError(); + } + + state double start = timer_monotonic(); + + loop { + try { + Optional > _ = wait(tr->get(LiteralStringRef("\xff/StatusJsonTestKey62793"))); + return timer_monotonic() - start; + } + catch(Error &e) { + Void _ = wait(tr->onError(e)); + tr->setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + } + } +} + +ACTOR static Future doCommitProbe(Future grvProbe, Transaction *sourceTr, Transaction *tr) { + ErrorOr grv = wait(errorOr(grvProbe)); + if(grv.isError()) { + throw grv.getError(); + } + + ASSERT(sourceTr->getReadVersion().isReady()); + tr->setVersion(sourceTr->getReadVersion().get()); + + state double start = timer_monotonic(); + + loop { + try { + tr->setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + tr->makeSelfConflicting(); + Void _ = wait(tr->commit()); + return timer_monotonic() - start; + } + catch(Error &e) { + Void _ = wait(tr->onError(e)); + } + } +} + +ACTOR static Future doProbe(Future probe, int timeoutSeconds, const char* prefix, const char* description, StatusObject *probeObj, StatusArray *messages, std::set *incomplete_reasons) { + choose { + when(ErrorOr result = wait(errorOr(probe))) { + if(result.isError()) { + incomplete_reasons->insert(format("Unable to retrieve latency probe information (%s: %s).", description, result.getError().what())); + } + else { + (*probeObj)[format("%s_seconds", prefix).c_str()] = result.get(); + } + } + when(Void _ = wait(delay(timeoutSeconds))) { + messages->push_back(makeMessage(format("%s_probe_timeout", prefix).c_str(), format("Unable to %s after %d seconds.", description, timeoutSeconds).c_str())); + } + } + + return Void(); +} + +ACTOR static Future latencyProbeFetcher(Reference> db, StatusArray *messages, std::set *incomplete_reasons) { + Database cx = openDBOnServer(db, TaskDefaultEndpoint, true, true); // Open a new database connection that is lock-aware + + state Transaction trImmediate(cx); + state Transaction trDefault(cx); + state Transaction trBatch(cx); + state Transaction trWrite(cx); + + state StatusObject statusObj; + + try { + Future immediateGrvProbe = doGrvProbe(&trImmediate, FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + Future defaultGrvProbe = doGrvProbe(&trDefault); + Future batchGrvProbe = doGrvProbe(&trBatch, FDBTransactionOptions::PRIORITY_BATCH); + + Future readProbe = doReadProbe(immediateGrvProbe, &trImmediate); + Future commitProbe = doCommitProbe(immediateGrvProbe, &trImmediate, &trWrite); + + int timeoutSeconds = 5; + + std::vector> probes; + probes.push_back(doProbe(immediateGrvProbe, timeoutSeconds, "immediate_priority_transaction_start", "start immediate priority transaction", &statusObj, messages, incomplete_reasons)); + probes.push_back(doProbe(defaultGrvProbe, timeoutSeconds, "transaction_start", "start default priority transaction", &statusObj, messages, incomplete_reasons)); + probes.push_back(doProbe(batchGrvProbe, timeoutSeconds, "batch_priority_transaction_start", "start batch priority transaction", &statusObj, messages, incomplete_reasons)); + probes.push_back(doProbe(readProbe, timeoutSeconds, "read", "read", &statusObj, messages, incomplete_reasons)); + probes.push_back(doProbe(commitProbe, timeoutSeconds, "commit", "commit", &statusObj, messages, incomplete_reasons)); + + Void _ = wait(waitForAll(probes)); + } + catch (Error &e) { + incomplete_reasons->insert(format("Unable to retrieve latency probe information (%s).", e.what())); + } + + return statusObj; +} + +ACTOR static Future> loadConfiguration(Database cx, StatusArray *messages, std::set *status_incomplete_reasons){ + state Optional result; + state Transaction tr(cx); + state Future getConfTimeout = delay(5.0); + + loop{ + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + tr.setOption(FDBTransactionOptions::CAUSAL_READ_RISKY); + try { + choose{ + when(Standalone res = wait(tr.getRange(configKeys, SERVER_KNOBS->CONFIGURATION_ROWS_TO_FETCH))) { + DatabaseConfiguration configuration; + if (res.size() == SERVER_KNOBS->CONFIGURATION_ROWS_TO_FETCH) { + status_incomplete_reasons->insert("Too many configuration parameters set."); + } + else { + for (int i = 0; i < res.size(); i++) { + configuration.set(res[i].key, res[i].value); + } + } + + result = configuration; + } + when(Void _ = wait(getConfTimeout)) { + messages->push_back(makeMessage("unreadable_configuration", "Unable to read database configuration.")); + } + } + break; + } + catch (Error &e) { + Void _ = wait(tr.onError(e)); + } + } + return result; +} + +static StatusObject configurationFetcher(Optional conf, ServerCoordinators coordinators, std::set *incomplete_reasons) { + StatusObject statusObj; + try { + StatusArray coordinatorLeaderServersArr; + vector< ClientLeaderRegInterface > coordinatorLeaderServers = coordinators.clientLeaderServers; + int count = coordinatorLeaderServers.size(); + statusObj["coordinators_count"] = count; + + if(conf.present()) { + DatabaseConfiguration configuration = conf.get(); + std::map configMap = configuration.toMap(); + for (auto it = configMap.begin(); it != configMap.end(); it++) { + if (it->first == "redundancy_mode") + { + StatusObject redundancyStatusObj; + redundancyStatusObj["factor"] = it->second; + statusObj["redundancy"] = redundancyStatusObj; + } + else { + statusObj[it->first] = it->second; + } + } + + StatusArray excludedServersArr; + std::set excludedServers = configuration.getExcludedServers(); + for (std::set::iterator it = excludedServers.begin(); it != excludedServers.end(); it++) { + StatusObject statusObj; + statusObj["address"] = it->toString(); + excludedServersArr.push_back(statusObj); + } + statusObj["excluded_servers"] = excludedServersArr; + + if (configuration.masterProxyCount != -1) + statusObj["proxies"] = configuration.getDesiredProxies(); + else if (configuration.autoMasterProxyCount != CLIENT_KNOBS->DEFAULT_AUTO_PROXIES) + statusObj["auto_proxies"] = configuration.autoMasterProxyCount; + + if (configuration.resolverCount != -1) + statusObj["resolvers"] = configuration.getDesiredResolvers(); + else if (configuration.autoResolverCount != CLIENT_KNOBS->DEFAULT_AUTO_RESOLVERS) + statusObj["auto_resolvers"] = configuration.autoResolverCount; + + if (configuration.desiredTLogCount != -1) + statusObj["logs"] = configuration.getDesiredLogs(); + else if (configuration.autoDesiredTLogCount != CLIENT_KNOBS->DEFAULT_AUTO_LOGS) + statusObj["auto_logs"] = configuration.autoDesiredTLogCount; + + if(configuration.storagePolicy) { + statusObj["storage_policy"] = configuration.storagePolicy->info(); + } + if(configuration.tLogPolicy) { + statusObj["tlog_policy"] = configuration.tLogPolicy->info(); + } + } + } + catch (Error &e){ + incomplete_reasons->insert("Could not retrieve all configuration status information."); + } + return statusObj; +} + +ACTOR static Future dataStatusFetcher(std::pair mWorker, std::string dbName, int *minReplicasRemaining) { + state StatusObject stateSectionObj; + state StatusObject statusObjData; + + try { + std::vector>> futures; + + // TODO: Should this be serial? + futures.push_back(timeoutError(mWorker.first.eventLogRequest.getReply(EventLogRequest(StringRef(dbName + "/DDTrackerStarting"))), 1.0)); + futures.push_back(timeoutError(mWorker.first.eventLogRequest.getReply(EventLogRequest(StringRef(dbName + "/DDTrackerStats"))), 1.0)); + + std::vector> dataInfo = wait(getAll(futures)); + + Standalone startingStats = dataInfo[0]; + state Standalone dataStats = dataInfo[1]; + + if (startingStats.size() && extractAttribute(startingStats, LiteralStringRef("State")) != "Active") { + stateSectionObj["name"] = "initializing"; + stateSectionObj["description"] = "(Re)initializing automatic data distribution"; + } + else { + state Standalone md = wait(timeoutError(mWorker.first.eventLogRequest.getReply(EventLogRequest(StringRef(dbName + "/MovingData"))), 1.0)); + + // If we have a MovingData message, parse it. + if (md.size()) + { + int64_t partitionsInQueue = parseInt64(extractAttribute(md, LiteralStringRef("InQueue"))); + int64_t partitionsInFlight = parseInt64(extractAttribute(md, LiteralStringRef("InFlight"))); + int64_t averagePartitionSize = parseInt64(extractAttribute(md, LiteralStringRef("AverageShardSize"))); + int64_t totalBytesWritten = parseInt64(extractAttribute(md, LiteralStringRef("BytesWritten"))); + int highestPriority = parseInt(extractAttribute(md, LiteralStringRef("HighestPriority"))); + + if( averagePartitionSize >= 0 ) { + StatusObject moving_data; + moving_data["in_queue_bytes"] = partitionsInQueue * averagePartitionSize; + moving_data["in_flight_bytes"] = partitionsInFlight * averagePartitionSize; + moving_data["total_written_bytes"] = totalBytesWritten; + + // TODO: moving_data["rate_bytes"] = makeCounter(hz, c, r); + statusObjData["moving_data"] = moving_data; + + statusObjData["average_partition_size_bytes"] = averagePartitionSize; + } + + if (highestPriority >= PRIORITY_TEAM_0_LEFT) { + stateSectionObj["healthy"] = false; + stateSectionObj["name"] = "missing_data"; + stateSectionObj["description"] = "No replicas remain of some data"; + stateSectionObj["min_replicas_remaining"] = 0; + *minReplicasRemaining = 0; + } + else if (highestPriority >= PRIORITY_TEAM_1_LEFT) { + stateSectionObj["healthy"] = false; + stateSectionObj["name"] = "healing"; + stateSectionObj["description"] = "Only one replica remains of some data"; + stateSectionObj["min_replicas_remaining"] = 1; + *minReplicasRemaining = 1; + } + else if (highestPriority >= PRIORITY_TEAM_2_LEFT) { + stateSectionObj["healthy"] = false; + stateSectionObj["name"] = "healing"; + stateSectionObj["description"] = "Only two replicas remain of some data"; + stateSectionObj["min_replicas_remaining"] = 2; + *minReplicasRemaining = 2; + } + else if (highestPriority >= PRIORITY_TEAM_UNHEALTHY) { + stateSectionObj["healthy"] = false; + stateSectionObj["name"] = "healing"; + stateSectionObj["description"] = "Restoring replication factor"; + } + else if (highestPriority >= PRIORITY_MERGE_SHARD) { + stateSectionObj["healthy"] = true; + stateSectionObj["name"] = "healthy_repartitioning"; + stateSectionObj["description"] = "Repartitioning."; + } + else if (highestPriority >= PRIORITY_TEAM_CONTAINS_UNDESIRED_SERVER) { + stateSectionObj["healthy"] = true; + stateSectionObj["name"] = "healthy_removing_server"; + stateSectionObj["description"] = "Removing storage server"; + } + else if (highestPriority >= PRIORITY_REBALANCE_SHARD) { + stateSectionObj["healthy"] = true; + stateSectionObj["name"] = "healthy_rebalancing"; + stateSectionObj["description"] = "Rebalancing"; + } + else if (highestPriority >= 0) { + stateSectionObj["healthy"] = true; + stateSectionObj["name"] = "healthy"; + } + } + + if (dataStats.size()) + { + int64_t totalDBBytes = parseInt64(extractAttribute(dataStats, LiteralStringRef("TotalSizeBytes"))); + statusObjData["total_kv_size_bytes"] = totalDBBytes; + int shards = parseInt(extractAttribute(dataStats, LiteralStringRef("Shards"))); + statusObjData["partitions_count"] = shards; + } + + } + } + catch (Error &e) { + if (e.code() == error_code_actor_cancelled) + throw; + // The most likely reason to be here is a timeout, either way we have no idea if the data state is healthy or not + // from the "cluster" perspective - from the client perspective it is not but that is indicated elsewhere. + } + + if (!stateSectionObj.empty()) + statusObjData["state"] = stateSectionObj; + + return statusObjData; +} + +namespace std +{ + template <> + struct hash + { + size_t operator()(const NetworkAddress& na) const + { + return (na.ip << 16) + na.port; + } + }; +} + +ACTOR template +static Future>> getServerMetrics(vector servers, std::unordered_map address_workers, std::string suffix) { + state vector>> futures; + for (auto s : servers) { + futures.push_back(latestEventOnWorker(address_workers[s.address()], s.id().toString() + suffix)); + } + + Void _ = wait(waitForAll(futures)); + + vector> results; + for (int i = 0; i < servers.size(); i++) { + results.push_back(std::make_pair(servers[i], futures[i].get().present() ? futures[i].get().get() : "")); + } + return results; +} + +ACTOR static Future>> getStorageServersAndMetrics(Database cx, std::unordered_map address_workers) { + vector servers = wait(timeoutError(getStorageServers(cx, true), 5.0)); + vector> results = wait(getServerMetrics(servers, address_workers, "/StorageMetrics")); + return results; +} + +ACTOR static Future>> getTLogsAndMetrics(Reference> db, std::unordered_map address_workers) { + vector servers = db->get().logSystemConfig.allPresentLogs(); + vector> results = wait(getServerMetrics(servers, address_workers, "/TLogMetrics")); + return results; +} + +static std::set getTLogEligibleMachines(vector> workers, DatabaseConfiguration configuration) { + std::set tlogEligibleMachines; + for(auto worker : workers) { + if(worker.second.machineClassFitness(ProcessClass::TLog) < ProcessClass::NeverAssign + && !configuration.isExcludedServer(worker.first.address())) + { + tlogEligibleMachines.insert(worker.first.locality.zoneId().get()); + } + } + + return tlogEligibleMachines; +} + +ACTOR static Future workloadStatusFetcher(Reference> db, vector> workers, std::pair mWorker, std::string dbName, StatusObject *qos, StatusObject *data_overlay, std::set *incomplete_reasons) { + state StatusObject statusObj; + state StatusObject operationsObj; + + // Writes and conflicts + try { + vector>> proxyStatFutures; + std::map> workersMap; + for (auto w : workers) { + workersMap[w.first.address()] = w; + } + for (auto &p : db->get().client.proxies) { + auto worker = getWorker(workersMap, p.address()); + if (worker.present()) + proxyStatFutures.push_back(timeoutError(worker.get().first.eventLogRequest.getReply(EventLogRequest(LiteralStringRef("ProxyMetrics"))), 1.0)); + else + throw all_alternatives_failed(); // We need data from all proxies for this result to be trustworthy + } + vector> proxyStats = wait(getAll(proxyStatFutures)); + + StatusObject mutations=makeCounter(), mutationBytes=makeCounter(), txnConflicts=makeCounter(), txnStartOut=makeCounter(), txnCommitOutSuccess=makeCounter(); + + for (auto &ps : proxyStats) { + mutations = addCounters( mutations, parseCounter(extractAttribute(ps, LiteralStringRef("mutations"))) ); + mutationBytes = addCounters( mutationBytes, parseCounter(extractAttribute(ps, LiteralStringRef("mutationBytes"))) ); + txnConflicts = addCounters( txnConflicts, parseCounter(extractAttribute(ps, LiteralStringRef("txnConflicts"))) ); + txnStartOut = addCounters( txnStartOut, parseCounter(extractAttribute(ps, LiteralStringRef("txnStartOut"))) ); + txnCommitOutSuccess = addCounters( txnCommitOutSuccess, parseCounter(extractAttribute(ps, LiteralStringRef("txnCommitOutSuccess"))) ); + } + + operationsObj["writes"] = mutations; + + StatusObject bytesObj; + bytesObj["written"] = mutationBytes; + statusObj["bytes"] = bytesObj; + + StatusObject transactions; + transactions["conflicted"] = txnConflicts; + transactions["started"] = txnStartOut; + transactions["committed"] = txnCommitOutSuccess; + + statusObj["transactions"] = transactions; + } + catch (Error& e) { + if (e.code() == error_code_actor_cancelled) + throw; + incomplete_reasons->insert("Unknown mutations, conflicts, and transactions state."); + } + + // Transactions and reads + try { + Standalone md = wait( timeoutError(mWorker.first.eventLogRequest.getReply( EventLogRequest(StringRef(dbName+"/RkUpdate") ) ), 1.0) ); + double tpsLimit = parseDouble(extractAttribute(md, LiteralStringRef("TPSLimit"))); + double transPerSec = parseDouble(extractAttribute(md, LiteralStringRef("ReleasedTPS"))); + double readReplyRate = parseDouble(extractAttribute(md, LiteralStringRef("ReadReplyRate"))); + int ssCount = parseInt(extractAttribute(md, LiteralStringRef("StorageServers"))); + int tlogCount = parseInt(extractAttribute(md, LiteralStringRef("TLogs"))); + int64_t worstFreeSpaceStorageServer = parseInt64(extractAttribute(md, LiteralStringRef("WorstFreeSpaceStorageServer"))); + int64_t worstFreeSpaceTLog = parseInt64(extractAttribute(md, LiteralStringRef("WorstFreeSpaceTLog"))); + int64_t worstStorageServerQueue = parseInt64(extractAttribute(md, LiteralStringRef("WorstStorageServerQueue"))); + int64_t limitingStorageServerQueue = parseInt64(extractAttribute(md, LiteralStringRef("LimitingStorageServerQueue"))); + int64_t worstTLogQueue = parseInt64(extractAttribute(md, LiteralStringRef("WorstTLogQueue"))); + int64_t totalDiskUsageBytes = parseInt64(extractAttribute(md, LiteralStringRef("TotalDiskUsageBytes"))); + int64_t worstVersionLag = parseInt64(extractAttribute(md, LiteralStringRef("WorstStorageServerVersionLag"))); + int64_t limitingVersionLag = parseInt64(extractAttribute(md, LiteralStringRef("LimitingStorageServerVersionLag"))); + + StatusObject readsObj; + readsObj["hz"] = readReplyRate; + operationsObj["reads"] = readsObj; + + (*data_overlay)["total_disk_used_bytes"] = totalDiskUsageBytes; + + if(ssCount > 0) { + (*data_overlay)["least_operating_space_bytes_storage_server"] = std::max(worstFreeSpaceStorageServer, (int64_t)0); + (*qos)["worst_queue_bytes_storage_server"] = worstStorageServerQueue; + (*qos)["limiting_queue_bytes_storage_server"] = limitingStorageServerQueue; + (*qos)["worst_version_lag_storage_server"] = worstVersionLag; + (*qos)["limiting_version_lag_storage_server"] = limitingVersionLag; + } + + if(tlogCount > 0) { + (*data_overlay)["least_operating_space_bytes_log_server"] = std::max(worstFreeSpaceTLog, (int64_t)0); + (*qos)["worst_queue_bytes_log_server"] = worstTLogQueue; + } + + (*qos)["transactions_per_second_limit"] = tpsLimit; + (*qos)["released_transactions_per_second"] = transPerSec; + + int reason = parseInt(extractAttribute(md, LiteralStringRef("Reason"))); + StatusObject perfLimit; + if (transPerSec > tpsLimit * 0.8) { + // If reason is known, set qos.performance_limited_by, otherwise omit + if (reason >= 0 && reason < limitReasonEnd) { + perfLimit = makeMessage(limitReasonName[reason], limitReasonDesc[reason]); + std::string reason_server_id = extractAttribute(md, LiteralStringRef("ReasonServerID")); + if (!reason_server_id.empty()) + perfLimit["reason_server_id"] = reason_server_id; + } + } + else { + perfLimit = makeMessage("workload", "The database is not being saturated by the workload."); + } + + if(!perfLimit.empty()) { + perfLimit["reason_id"] = reason; + (*qos)["performance_limited_by"] = perfLimit; + } + } catch (Error &e){ + if (e.code() == error_code_actor_cancelled) + throw; + incomplete_reasons->insert("Unknown read and performance state."); + } + statusObj["operations"] = operationsObj; + + return statusObj; +} + +static StatusArray oldTlogFetcher(int* oldLogFaultTolerance, Reference> db, std::unordered_map const& address_workers) { + StatusArray oldTlogsArray; + + if(db->get().recoveryState == RecoveryState::FULLY_RECOVERED) { + for(auto it : db->get().logSystemConfig.oldTLogs) { + StatusObject statusObj; + int failedLogs = 0; + StatusArray logsObj; + for(auto log : it.tLogs) { + StatusObject logObj; + bool failed = !log.present() || !address_workers.count(log.interf().address()); + logObj["id"] = log.id().shortString(); + logObj["healthy"] = !failed; + if(log.present()) { + logObj["address"] = log.interf().address().toString(); + } + logsObj.push_back(logObj); + if(failed) { + failedLogs++; + } + } + *oldLogFaultTolerance = std::min(*oldLogFaultTolerance, it.tLogReplicationFactor - 1 - it.tLogWriteAntiQuorum - failedLogs); + statusObj["logs"] = logsObj; + statusObj["log_replication_factor"] = it.tLogReplicationFactor; + statusObj["log_write_anti_quorum"] = it.tLogWriteAntiQuorum; + statusObj["log_fault_tolerance"] = it.tLogReplicationFactor - 1 - it.tLogWriteAntiQuorum - failedLogs; + oldTlogsArray.push_back(statusObj); + } + } + + return oldTlogsArray; +} + +/* +static StatusObject faultToleranceStatusFetcher(DatabaseConfiguration configuration, ServerCoordinators coordinators, int numTLogEligibleMachines, int minReplicasRemaining, int oldLogFaultTolerance) { +======= +static StatusObject faultToleranceStatusFetcher(DatabaseConfiguration configuration, ServerCoordinators coordinators, std::vector>& workers, int numTLogEligibleMachines, int minReplicasRemaining) { +*/ + +static StatusObject faultToleranceStatusFetcher(DatabaseConfiguration configuration, ServerCoordinators coordinators, std::vector>& workers, int numTLogEligibleMachines, int minReplicasRemaining) { + StatusObject statusObj; + + // without losing data + int32_t maxMachineFailures = configuration.maxMachineFailuresTolerated(); + int maxCoordinatorFailures = (coordinators.clientLeaderServers.size() - 1) / 2; + + std::map workerZones; + for(auto& worker : workers) { + workerZones[worker.first.address()] = worker.first.locality.zoneId().orDefault(LiteralStringRef("")); + } + std::map coordinatorZoneCounts; + for(auto& coordinator : coordinators.ccf->getConnectionString().coordinators()) { + auto zone = workerZones[coordinator]; + coordinatorZoneCounts[zone] += 1; + } + std::vector> coordinatorZones(coordinatorZoneCounts.begin(), coordinatorZoneCounts.end()); + std::sort(coordinatorZones.begin(), coordinatorZones.end(), [] (const std::pair& lhs, const std::pair& rhs) { + return lhs.second > rhs.second; + }); + int lostCoordinators = 0; + int maxCoordinatorZoneFailures = 0; + for(auto zone : coordinatorZones) { + lostCoordinators += zone.second; + if(lostCoordinators > maxCoordinatorFailures) { + break; + } + maxCoordinatorZoneFailures += 1; + } + + int machineFailuresWithoutLosingData = std::min(maxMachineFailures, maxCoordinatorZoneFailures); + + if (minReplicasRemaining >= 0){ + machineFailuresWithoutLosingData = std::min(machineFailuresWithoutLosingData, minReplicasRemaining - 1); + } + // ahm + // machineFailuresWithoutLosingData = std::min(machineFailuresWithoutLosingData, oldLogFaultTolerance); + + statusObj["max_machine_failures_without_losing_data"] = std::max(machineFailuresWithoutLosingData, 0); + + // without losing availablity + statusObj["max_machine_failures_without_losing_availability"] = std::max(std::min(numTLogEligibleMachines - configuration.minMachinesRequired(), machineFailuresWithoutLosingData), 0); + return statusObj; +} + +static std::string getIssueDescription(std::string name) { + if(name == "unable_to_write_cluster_file") { + return "Unable to update cluster file."; + } + + // FIXME: name and description will be the same unless the message is 'unable_to_write_cluster_file', which is currently the only possible message + return name; +} + +static std::map getProcessIssuesAsMessages( ProcessIssuesMap const& _issues ) { + std::map issuesMap; + + try { + ProcessIssuesMap issues = _issues; + for (auto i : issues) { + StatusObject message = makeMessage(i.second.first.c_str(), getIssueDescription(i.second.first).c_str()); + issuesMap[i.first.toString()] = message; + } + } + catch (Error &e) { + TraceEvent(SevError, "ErrorParsingProcessIssues").error(e); + // swallow + } + + return issuesMap; +} + +static StatusArray getClientIssuesAsMessages( ProcessIssuesMap const& _issues) { + StatusArray issuesList; + + try { + ProcessIssuesMap issues = _issues; + std::map> deduplicatedIssues; + + for(auto i : issues) { + deduplicatedIssues[i.second.first].push_back(format("%s:%d", toIPString(i.first.ip).c_str(), i.first.port)); + } + + for (auto i : deduplicatedIssues) { + StatusObject message = makeMessage(i.first.c_str(), getIssueDescription(i.first).c_str()); + StatusArray addresses; + for(auto addr : i.second) { + addresses.push_back(addr); + } + + message["addresses"] = addresses; + issuesList.push_back(message); + } + } + catch (Error &e) { + TraceEvent(SevError, "ErrorParsingClientIssues").error(e); + // swallow + } + + return issuesList; +} + +ACTOR Future layerStatusFetcher(Database cx, StatusArray *messages, std::set *incomplete_reasons) { + state StatusObject result; + state JSONDoc json(result); + + try { + state ReadYourWritesTransaction tr(cx); + loop { + try { + tr.setOption(FDBTransactionOptions::ACCESS_SYSTEM_KEYS); + int64_t timeout_ms = 3000; + tr.setOption(FDBTransactionOptions::TIMEOUT, StringRef((uint8_t *)&timeout_ms, sizeof(int64_t))); + + std::string jsonPrefix = layerStatusMetaPrefixRange.begin.toString() + "json/"; + Standalone jsonLayers = wait(tr.getRange(KeyRangeRef(jsonPrefix, strinc(jsonPrefix)), 1000)); + // TODO: Also fetch other linked subtrees of meta keys + + state std::vector>> docFutures; + state int i; + for(i = 0; i < jsonLayers.size(); ++i) + docFutures.push_back(tr.getRange(KeyRangeRef(jsonLayers[i].value, strinc(jsonLayers[i].value)), 1000)); + + result.clear(); + JSONDoc::expires_reference_version = (uint64_t)tr.getReadVersion().get(); + + for(i = 0; i < docFutures.size(); ++i) { + state Standalone docs = wait(docFutures[i]); + state int j; + for(j = 0; j < docs.size(); ++j) { + state json_spirit::mValue doc; + try { + json_spirit::read_string(docs[j].value.toString(), doc); + Void _ = wait(yield()); + json.absorb(doc.get_obj()); + Void _ = wait(yield()); + } catch(Error &e) { + TraceEvent(SevWarn, "LayerStatusBadJSON").detail("Key", printable(docs[j].key)); + } + } + } + json.create("_valid") = true; + break; + } catch(Error &e) { + Void _ = wait(tr.onError(e)); + } + } + } catch(Error &e) { + TraceEvent(SevWarn, "LayerStatusError").error(e); + incomplete_reasons->insert(format("Unable to retrieve layer status (%s).", e.what())); + json.create("_error") = format("Unable to retrieve layer status (%s).", e.what()); + json.create("_valid") = false; + } + + json.cleanOps(); + return result; +} + +ACTOR Future lockedStatusFetcher(Reference> db, StatusArray *messages, std::set *incomplete_reasons) { + state StatusObject statusObj; + + state Database cx = openDBOnServer(db, TaskDefaultEndpoint, true, false); // Open a new database connection that isn't lock-aware + state Transaction tr(cx); + state int timeoutSeconds = 5; + state Future getTimeout = delay(timeoutSeconds); + + loop { + tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); + try { + choose{ + when(Version f = wait(tr.getReadVersion())) { + statusObj["database_locked"] = false; + } + + when(Void _ = wait(getTimeout)) { + incomplete_reasons->insert(format("Unable to determine if database is locked after %d seconds.", timeoutSeconds)); + } + } + break; + } + catch (Error &e) { + if (e.code() == error_code_database_locked) { + statusObj["database_locked"] = true; + break; + } + else { + try { + Void _ = wait(tr.onError(e)); + } + catch (Error &e) { + incomplete_reasons->insert(format("Unable to determine if database is locked (%s).", e.what())); + break; + } + } + } + } + return statusObj; +} + +// constructs the cluster section of the json status output +ACTOR Future clusterGetStatus( + Reference> db, + Database cx, + vector> workers, + ProcessIssuesMap workerIssues, + ProcessIssuesMap clientIssues, + ClientVersionMap clientVersionMap, + ServerCoordinators coordinators, + std::vector incompatibleConnections ) +{ + // since we no longer offer multi-database support, all databases must be named DB + state std::string dbName = "DB"; + + // Check if master worker is present + state StatusArray messages; + state std::set status_incomplete_reasons; + state std::pair mWorker; + + try { + // Get the master Worker interface + Optional> _mWorker = getWorker( workers, db->get().master.address() ); + if (_mWorker.present()) { + mWorker = _mWorker.get(); + } else { + messages.push_back(makeMessage("unreachable_master_worker", "Unable to locate the master worker.")); + } + + // Get latest events for various event types from ALL workers + // WorkerEvents is a map of worker's NetworkAddress to its event string + // The pair represents worker responses and a set of worker NetworkAddress strings which did not respond + std::vector< Future< Optional >> > > futures; + futures.push_back(latestEventOnWorkers(workers, "MachineMetrics")); + futures.push_back(latestEventOnWorkers(workers, "ProcessMetrics")); + futures.push_back(latestErrorOnWorkers(workers)); + futures.push_back(latestEventOnWorkers(workers, "TraceFileOpenError")); + futures.push_back(latestEventOnWorkers(workers, "ProgramStart")); + + // Wait for all response pairs. + state std::vector< Optional >> > workerEventsVec = wait(getAll(futures)); + + // Create a unique set of all workers who were unreachable for 1 or more of the event requests above. + // Since each event request is independent and to all workers, workers can have responded to some + // event requests but still end up in the unreachable set. + std::set mergeUnreachable; + + // For each (optional) pair, if the pair is present and not empty then add the unreachable workers to the set. + for (auto pair : workerEventsVec) + { + if (pair.present() && pair.get().second.size()) + mergeUnreachable.insert(pair.get().second.begin(), pair.get().second.end()); + } + + // We now have a unique set of workers who were in some way unreachable. If there is anything in that set, create a message + // for it and include the list of unreachable processes. + if (mergeUnreachable.size()){ + StatusObject message = makeMessage("unreachable_processes", "The cluster has some unreachable processes."); + StatusArray unreachableProcs; + for (auto m : mergeUnreachable){ + unreachableProcs.push_back(StatusObject({ {"address", m} })); + } + message["unreachable_processes"] = unreachableProcs; + messages.push_back(message); + } + + // construct status information for cluster subsections + state StatusObject recoveryStateStatus = wait(recoveryStateStatusFetcher(mWorker, dbName, workers.size(), &status_incomplete_reasons)); + + // machine metrics + state WorkerEvents mMetrics = workerEventsVec[0].present() ? workerEventsVec[0].get().first : WorkerEvents(); + // process metrics + state WorkerEvents pMetrics = workerEventsVec[1].present() ? workerEventsVec[1].get().first : WorkerEvents(); + state WorkerEvents latestError = workerEventsVec[2].present() ? workerEventsVec[2].get().first : WorkerEvents(); + state WorkerEvents traceFileOpenErrors = workerEventsVec[3].present() ? workerEventsVec[3].get().first : WorkerEvents(); + state WorkerEvents programStarts = workerEventsVec[4].present() ? workerEventsVec[4].get().first : WorkerEvents(); + + state StatusObject statusObj; + if(db->get().recoveryCount > 0) { + statusObj["generation"] = db->get().recoveryCount; + } + + state std::map processIssues = getProcessIssuesAsMessages(workerIssues); + state vector> storageServers; + state vector> tLogs; + state StatusObject qos; + state StatusObject data_overlay; + + statusObj["protocol_version"] = format("%llx", currentProtocolVersion); + + state Optional configuration = Optional(); + + if(!(recoveryStateStatus.count("name") && recoveryStateStatus["name"] == RecoveryStatus::names[RecoveryStatus::configuration_missing])) { + Optional _configuration = wait(loadConfiguration(cx, &messages, &status_incomplete_reasons)); + configuration = _configuration; + } + + statusObj["machines"] = machineStatusFetcher(mMetrics, workers, configuration, &status_incomplete_reasons); + + if (configuration.present()){ + // Do the latency probe by itself to avoid interference from other status activities + state Future latencyProbe = latencyProbeFetcher(db, &messages, &status_incomplete_reasons); + + StatusObject latencyProbeResults = wait(latencyProbe); + + statusObj["database_available"] = latencyProbeResults.count("immediate_priority_transaction_start_seconds") && latencyProbeResults.count("read_seconds") && latencyProbeResults.count("commit_seconds"); + if (!latencyProbeResults.empty()) { + statusObj["latency_probe"] = latencyProbeResults; + } + + state int minReplicasRemaining = -1; + std::vector> futures2; + futures2.push_back(dataStatusFetcher(mWorker, dbName, &minReplicasRemaining)); + futures2.push_back(workloadStatusFetcher(db, workers, mWorker, dbName, &qos, &data_overlay, &status_incomplete_reasons)); + futures2.push_back(layerStatusFetcher(cx, &messages, &status_incomplete_reasons)); + futures2.push_back(lockedStatusFetcher(db, &messages, &status_incomplete_reasons)); + + // Start getting storage servers now (using system priority) concurrently. Using sys priority because having storage servers + // in status output is important to give context to error messages in status that reference a storage server role ID. + state std::unordered_map address_workers; + for (auto worker : workers) + address_workers[worker.first.address()] = worker.first; + state Future>>> storageServerFuture = errorOr(getStorageServersAndMetrics(cx, address_workers)); + state Future>>> tLogFuture = errorOr(getTLogsAndMetrics(db, address_workers)); + + state std::vector workerStatuses = wait(getAll(futures2)); + + int oldLogFaultTolerance = 100; + if(db->get().recoveryState == RecoveryState::FULLY_RECOVERED && db->get().logSystemConfig.oldTLogs.size() > 0) { + statusObj["old_logs"] = oldTlogFetcher(&oldLogFaultTolerance, db, address_workers); + } + + if(configuration.present()) { + std::set tlogEligibleMachines = getTLogEligibleMachines(workers, configuration.get()); + statusObj["fault_tolerance"] = faultToleranceStatusFetcher(configuration.get(), coordinators, workers, tlogEligibleMachines.size(), minReplicasRemaining); + } + + StatusObject configObj = configurationFetcher(configuration, coordinators, &status_incomplete_reasons); + + // configArr could be empty + if (!configObj.empty()) + statusObj["configuration"] = configObj; + + // workloadStatusFetcher returns the workload section but also optionally writes the qos section and adds to the data_overlay object + if (!workerStatuses[1].empty()) + statusObj["workload"] = workerStatuses[1]; + + statusObj["layers"] = workerStatuses[2]; + + // Add qos section if it was populated + if (!qos.empty()) + statusObj["qos"] = qos; + + // Merge data_overlay into data + StatusObject &clusterDataSection = workerStatuses[0]; + clusterDataSection.insert(data_overlay.begin(), data_overlay.end()); + + // If data section not empty, add it to statusObj + if (!clusterDataSection.empty()) + statusObj["data"] = clusterDataSection; + + // Insert database_locked section + if(!workerStatuses[3].empty()) { + statusObj.insert(workerStatuses[3].begin(), workerStatuses[3].end()); + } + + // Need storage servers now for processStatusFetcher() below. + ErrorOr>> _storageServers = wait(storageServerFuture); + if (_storageServers.present()) { + storageServers = _storageServers.get(); + } + else + messages.push_back(makeMessage("storage_servers_error", "Timed out trying to retrieve storage servers.")); + + // ...also tlogs + ErrorOr>> _tLogs = wait(tLogFuture); + if (_tLogs.present()) { + tLogs = _tLogs.get(); + } + else + messages.push_back(makeMessage("log_servers_error", "Timed out trying to retrieve log servers.")); + } + else { + // Set layers status to { _valid: false, error: "configurationMissing"} + statusObj["layers"] = json_spirit::mObject({{"_valid", false}, {"_error", "configurationMissing"}}); + } + + StatusObject processStatus = wait(processStatusFetcher(db, workers, pMetrics, mMetrics, latestError, traceFileOpenErrors, programStarts, processIssues, storageServers, tLogs, cx, configuration, &status_incomplete_reasons)); + statusObj["processes"] = processStatus; + statusObj["clients"] = clientStatusFetcher(clientVersionMap); + + StatusArray incompatibleConnectionsArray; + for(auto it : incompatibleConnections) { + incompatibleConnectionsArray.push_back(it.toString()); + } + statusObj["incompatible_connections"] = incompatibleConnectionsArray; + + if (!recoveryStateStatus.empty()) + statusObj["recovery_state"] = recoveryStateStatus; + + // cluster messages subsection; + StatusArray clientIssuesArr = getClientIssuesAsMessages(clientIssues); + if (clientIssuesArr.size() > 0) { + StatusObject clientIssueMessage = makeMessage("client_issues", "Some clients of this cluster have issues."); + clientIssueMessage["issues"] = clientIssuesArr; + messages.push_back(clientIssueMessage); + } + + // Create the status_incomplete message if there were any reasons that the status is incomplete. + if (!status_incomplete_reasons.empty()) + { + StatusObject incomplete_message = makeMessage("status_incomplete", "Unable to retrieve all status information."); + // Make a JSON array of all of the reasons in the status_incomplete_reasons set. + StatusArray reasons; + for (auto i : status_incomplete_reasons) + reasons.push_back(StatusObject({ { "description", i } })); + incomplete_message["reasons"] = reasons; + messages.push_back(incomplete_message); + } + + statusObj["messages"] = messages; + + int64_t clusterTime = time(0); + if (clusterTime != -1){ + statusObj["cluster_controller_timestamp"] = clusterTime; + } + + return statusObj; + } catch( Error&e ) { + TraceEvent(SevError, "StatusError").error(e); + throw; + } +} + +TEST_CASE("status/json/merging") { + StatusObject objA, objB, objC; + JSONDoc a(objA), b(objB), c(objC); + + a.create("int_one") = 1; + a.create("int_unmatched") = 2; + a.create("int_total_30.$sum") = 10; + a.create("bool_true.$and") = true; + a.create("string") = "test"; + a.create("subdoc.int_11") = 11; + a.create("a") = "justA"; + a.create("subdoc.double_max_5.$max") = 2.0; + a.create("subdoc.double_min_2.$min") = 2.0; + a.create("subdoc.obj_count_3.$count_keys.one") = 1; + a.create("subdoc.obj_count_3.$count_keys.two") = 2; + a.create("expired.$expires") = "I should have expired."; + a.create("expired.version") = 1; + a.create("not_expired_and_merged.$expires.seven.$sum") = 1; + a.create("not_expired_and_merged.$expires.one.$min") = 3; + a.create("not_expired_and_merged.version") = 3; + + b.create("int_one") = 1; + b.create("int_unmatched") = 3; + b.create("int_total_30.$sum") = 20; + b.create("bool_true.$and") = true; + b.create("string") = "test"; + b.create("subdoc.int_11") = 11; + b.create("b") = "justB"; + b.create("subdoc.double_max_5.$max") = 5.0; + b.create("subdoc.double_min_2.$min") = 5.0; + b.create("subdoc.obj_count_3.$count_keys.three") = 3; + b.create("expired.$expires") = "I should have also expired."; + b.create("expired.version") = 1; + b.create("not_expired_and_merged.$expires.seven.$sum") = 2; + b.create("not_expired_and_merged.$expires.one.$min") = 1; + b.create("not_expired_and_merged.version") = 3; + b.create("last_hello.$last") = "blah"; + b.create("latest_obj.$latest.a") = 0; + b.create("latest_obj.$latest.b") = 0; + b.create("latest_obj.$latest.c") = 0; + b.create("latest_obj.timestamp") = 2; + b.create("latest_int_5.$latest") = 7; + b.create("latest_int_5.timestamp") = 2; + + c.create("int_total_30.$sum") = 0; + c.create("not_expired.$expires") = "I am still valid"; + c.create("not_expired.version") = 3; + c.create("not_expired_and_merged.$expires.seven.$sum") = 4; + c.create("not_expired_and_merged.$expires.one.$min") = 2; + c.create("not_expired_and_merged.version") = 3; + c.create("last_hello.$last") = "hello"; + c.create("latest_obj.$latest.a.$max") = "a"; + c.create("latest_obj.$latest.b.$min") = "b"; + c.create("latest_obj.$latest.expired.$expires") = "I should not be here."; + c.create("latest_obj.$latest.expired.version") = 1; + c.create("latest_obj.$latest.not_expired.$expires") = "Still alive."; + c.create("latest_obj.$latest.not_expired.version") = 3; + c.create("latest_obj.timestamp") = 3; + b.create("latest_int_5.$latest") = 5; + b.create("latest_int_5.timestamp") = 3; + + JSONDoc::expires_reference_version = 2; + a.absorb(b); + a.absorb(c); + a.cleanOps(); + std::string result = json_spirit::write_string(json_spirit::mValue(objA)); + std::string expected = "{\"a\":\"justA\",\"b\":\"justB\",\"bool_true\":true,\"expired\":null,\"int_one\":1,\"int_total_30\":30,\"int_unmatched\":{\"ERROR\":\"Values do not match.\",\"a\":2,\"b\":3},\"last_hello\":\"hello\",\"latest_int_5\":5,\"latest_obj\":{\"a\":\"a\",\"b\":\"b\",\"not_expired\":\"Still alive.\"},\"not_expired\":\"I am still valid\",\"not_expired_and_merged\":{\"one\":1,\"seven\":7},\"string\":\"test\",\"subdoc\":{\"double_max_5\":5,\"double_min_2\":2,\"int_11\":11,\"obj_count_3\":3}}"; + + if(result != expected) { + printf("ERROR: Combined doc does not match expected.\nexpected: %s\nresult: %s\n", expected.c_str(), result.c_str()); + ASSERT(false); + } + + return Void(); +} diff --git a/fdbserver/Status.h b/fdbserver/Status.h new file mode 100644 index 0000000000..1dbff4c0de --- /dev/null +++ b/fdbserver/Status.h @@ -0,0 +1,37 @@ +/* + * Status.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_STATUS_H +#define FDBSERVER_STATUS_H +#pragma once + +#include "fdbrpc/fdbrpc.h" +#include "WorkerInterface.h" +#include "MasterInterface.h" +#include "fdbclient/ClusterInterface.h" + +typedef std::map< NetworkAddress, std::pair > ProcessIssuesMap; +typedef std::map< NetworkAddress, Standalone> > ClientVersionMap; + +std::string extractAttribute( std::string const& expanded, std::string const& attributeToExtract ); +Future clusterGetStatus( Reference> const& db, Database const& cx, vector> const& workers, + ProcessIssuesMap const& workerIssues, ProcessIssuesMap const& clientIssues, ClientVersionMap const& clientVersionMap, ServerCoordinators const& coordinators, std::vector const& incompatibleConnections ); + +#endif diff --git a/fdbserver/StorageMetrics.actor.h b/fdbserver/StorageMetrics.actor.h new file mode 100644 index 0000000000..d4407af686 --- /dev/null +++ b/fdbserver/StorageMetrics.actor.h @@ -0,0 +1,394 @@ +/* + * StorageMetrics.actor.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Included via StorageMetrics.h +#include "fdbclient/FDBTypes.h" +#include "fdbrpc/simulator.h" +#include "flow/UnitTest.h" +#include "fdbclient/StorageServerInterface.h" +#include "fdbclient/KeyRangeMap.h" +#include "Knobs.h" + +struct StorageMetricSample { + IndexedSet sample; + int64_t metricUnitsPerSample; + + StorageMetricSample( int64_t metricUnitsPerSample ) : metricUnitsPerSample(metricUnitsPerSample) {} + + int64_t getEstimate( KeyRangeRef keys ) const { + return sample.sumRange( keys.begin, keys.end ); + } + KeyRef splitEstimate( KeyRangeRef range, int64_t offset, bool front = true ) const { + auto fwd_split = sample.index( front ? sample.sumTo(sample.lower_bound(range.begin)) + offset : sample.sumTo(sample.lower_bound(range.end)) - offset ); + + if( fwd_split == sample.end() || *fwd_split >= range.end ) + return range.end; + + if( !front && *fwd_split <= range.begin ) + return range.begin; + + auto bck_split = fwd_split; + + // Butterfly search - start at midpoint then go in both directions. + while ((fwd_split != sample.end() && *fwd_split < range.end) || + (bck_split != sample.begin() && *bck_split > range.begin)) { + if (bck_split != sample.begin() && *bck_split > range.begin) { + auto it = bck_split; + bck_split.decrementNonEnd(); + + KeyRef split = keyBetween(KeyRangeRef(bck_split != sample.begin() ? std::max(*bck_split,range.begin) : range.begin, *it)); + if( split.size() <= CLIENT_KNOBS->SPLIT_KEY_SIZE_LIMIT ) + return split; + } + + if (fwd_split != sample.end() && *fwd_split < range.end) { + auto it = fwd_split; + ++it; + + KeyRef split = keyBetween(KeyRangeRef(*fwd_split, it != sample.end() ? std::min(*it, range.end) : range.end)); + if( split.size() <= CLIENT_KNOBS->SPLIT_KEY_SIZE_LIMIT ) + return split; + + fwd_split = it; + } + + } + + // If we didn't return above, we didn't find anything. + TraceEvent(SevWarnAlways, "CannotSplitLastSampleKey").detail("range", printable(range)).detail("offset", offset); + return front ? range.end : range.begin; + } +}; + +TEST_CASE("fdbserver/StorageMetricSample/simple") { + StorageMetricSample s( 1000 ); + s.sample.insert(LiteralStringRef("Apple"), 1000); + s.sample.insert(LiteralStringRef("Banana"), 2000); + s.sample.insert(LiteralStringRef("Cat"), 1000); + s.sample.insert(LiteralStringRef("Cathode"), 1000); + s.sample.insert(LiteralStringRef("Dog"), 1000); + + ASSERT(s.getEstimate(KeyRangeRef(LiteralStringRef("A"), LiteralStringRef("D"))) == 5000); + ASSERT(s.getEstimate(KeyRangeRef(LiteralStringRef("A"), LiteralStringRef("E"))) == 6000); + ASSERT(s.getEstimate(KeyRangeRef(LiteralStringRef("B"), LiteralStringRef("C"))) == 2000); + + //ASSERT(s.splitEstimate(KeyRangeRef(LiteralStringRef("A"), LiteralStringRef("D")), 3500) == LiteralStringRef("Cat")); + + return Void(); +} + +struct TransientStorageMetricSample : StorageMetricSample { + Deque< std::pair> > queue; + + TransientStorageMetricSample( int64_t metricUnitsPerSample ) : StorageMetricSample(metricUnitsPerSample) {} + + bool roll( KeyRef key, int64_t metric ) { + return g_random->random01() < (double)metric / metricUnitsPerSample; //< SOMEDAY: Better randomInt64? + } + + // Returns the sampled metric value (possibly 0, possibly increased by the sampling factor) + int64_t addAndExpire( KeyRef key, int64_t metric, double expiration ) { + int64_t x = add( key, metric ); + if (x) + queue.push_back( std::make_pair( expiration, std::make_pair( *sample.find(key), -x ) ) ); + return x; + } + + //FIXME: both versions of erase are broken, because they do not remove items in the queue with will subtract a metric from the value sometime in the future + int64_t erase( KeyRef key ) { + auto it = sample.find(key); + if (it == sample.end()) return 0; + int64_t x = sample.getMetric(it); + sample.erase(it); + return x; + } + void erase( KeyRangeRef keys ) { + sample.erase( keys.begin, keys.end ); + } + + void poll(KeyRangeMap< vector< PromiseStream< StorageMetrics > > > & waitMap, StorageMetrics m) { + double now = ::now(); + while (queue.size() && + queue.front().first <= now ) + { + KeyRef key = queue.front().second.first; + int64_t delta = queue.front().second.second; + ASSERT( delta != 0 ); + + if( sample.addMetric( Key(key), delta ) == 0 ) + sample.erase( key ); + + StorageMetrics deltaM = m * delta; + auto v = waitMap[key]; + for(int i=0; i > > waitMetricsMap; + StorageMetricSample byteSample; + TransientStorageMetricSample iopsSample, bandwidthSample; // FIXME: iops and bandwidth calculations are not effectively tested, since they aren't currently used by data distribution + + StorageServerMetrics() + : byteSample( 0 ), iopsSample( SERVER_KNOBS->IOPS_UNITS_PER_SAMPLE ), bandwidthSample( SERVER_KNOBS->BANDWIDTH_UNITS_PER_SAMPLE ) + { + } + + // Get the current estimated metrics for the given keys + StorageMetrics getMetrics( KeyRangeRef const& keys ) { + StorageMetrics result; + result.bytes = byteSample.getEstimate( keys ); + result.bytesPerKSecond = bandwidthSample.getEstimate( keys ) * SERVER_KNOBS->STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS; + result.iosPerKSecond = iopsSample.getEstimate( keys ) * SERVER_KNOBS->STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS; + return result; + } + + // Called when metrics should change (IO for a given key) + // Notifies waiting WaitMetricsRequests through waitMetricsMap, and updates metricsAverageQueue and metricsSampleMap + void notify( KeyRef key, StorageMetrics& metrics ) { + ASSERT (metrics.bytes == 0); // ShardNotifyMetrics + TEST (metrics.bytesPerKSecond != 0); // ShardNotifyMetrics + TEST (metrics.iosPerKSecond != 0); // ShardNotifyMetrics + + double expire = now() + SERVER_KNOBS->STORAGE_METRICS_AVERAGE_INTERVAL; + + StorageMetrics notifyMetrics; + + if (metrics.bytesPerKSecond) + notifyMetrics.bytesPerKSecond = bandwidthSample.addAndExpire( key, metrics.bytesPerKSecond, expire ) * SERVER_KNOBS->STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS; + if (metrics.iosPerKSecond) + notifyMetrics.iosPerKSecond = iopsSample.addAndExpire( key, metrics.iosPerKSecond, expire ) * SERVER_KNOBS->STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS; + if (!notifyMetrics.allZero()) { + auto& v = waitMetricsMap[key]; + for(int i=0; i>, KeyRangeRef>::Iterator shard, int64_t bytes ) { + ASSERT(shard.end() <= allKeys.end); + + StorageMetrics notifyMetrics; + notifyMetrics.bytes = bytes; + for(int i=0; i < shard.value().size(); i++) { + TEST( true ); // notifyBytes + shard.value()[i].send( notifyMetrics ); + } + } + + // Called by StorageServerDisk when the size of a key in byteSample changes, to notify WaitMetricsRequest + void notifyBytes( KeyRef key, int64_t bytes ) { + if( key >= allKeys.end ) //Do not notify on changes to internal storage server state + return; + + notifyBytes(waitMetricsMap.rangeContaining(key), bytes); + } + + // Called when a range of keys becomes unassigned (and therefore not readable), to notify waiting WaitMetricsRequests (also other types of wait + // requests in the future?) + void notifyNotReadable( KeyRangeRef keys ) { + auto rs = waitMetricsMap.intersectingRanges(keys); + for (auto r = rs.begin(); r != rs.end(); ++r){ + auto &v = r->value(); + TEST( v.size() ); // notifyNotReadable() sending errors to intersecting ranges + for (int n=0; nSTORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS; bandwidthSample.poll(waitMetricsMap, m); } + { StorageMetrics m; m.iosPerKSecond = SERVER_KNOBS->STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS; iopsSample.poll(waitMetricsMap, m); } + // bytesSample doesn't need polling because we never call addExpire() on it + } + + //static void waitMetrics( StorageServerMetrics* const& self, WaitMetricsRequest const& req ); + + KeyRef getSplitKey( int64_t remaining, int64_t estimated, int64_t limits, int64_t used, int64_t infinity, + bool isLastShard, StorageMetricSample& sample, double divisor, KeyRef const& lastKey, KeyRef const& key ) { + if( limits < infinity / 2 ) { + int64_t expectedSize; + if( isLastShard || remaining > estimated ) + expectedSize = remaining / ( ( double( remaining ) / limits ) + 0.5 ); + else + expectedSize = estimated / ( ( double( estimated ) / limits ) + 0.5 ); + + if( remaining > expectedSize ) { + // This does the conversion from native units to bytes using the divisor. + double offset = (expectedSize - used) / divisor; + if( offset <= 0 ) + return lastKey; + return sample.splitEstimate( KeyRangeRef(lastKey, key), offset * ( ( 1.0 - SERVER_KNOBS->SPLIT_JITTER_AMOUNT ) + 2 * g_random->random01() * SERVER_KNOBS->SPLIT_JITTER_AMOUNT ) ); + } + } + + return key; + } + + void splitMetrics( SplitMetricsRequest req ) { + try { + SplitMetricsReply reply; + KeyRef lastKey = req.keys.begin; + StorageMetrics used = req.used; + StorageMetrics estimated = req.estimated; + StorageMetrics remaining = getMetrics( req.keys ) + used; + + //TraceEvent("SplitMetrics").detail("Begin", printable(req.keys.begin)).detail("End", printable(req.keys.end)).detail("Remaining", remaining.bytes).detail("Used", used.bytes); + + while( true ) { + if( remaining.bytes < 2*SERVER_KNOBS->MIN_SHARD_BYTES ) + break; + KeyRef key = req.keys.end; + + key = getSplitKey( remaining.bytes, estimated.bytes, req.limits.bytes, used.bytes, + req.limits.infinity, req.isLastShard, byteSample, 1, lastKey, key ); + if( used.bytes < SERVER_KNOBS->MIN_SHARD_BYTES ) + key = std::max( key, byteSample.splitEstimate( KeyRangeRef(lastKey, req.keys.end), SERVER_KNOBS->MIN_SHARD_BYTES - used.bytes ) ); + key = getSplitKey( remaining.iosPerKSecond, estimated.iosPerKSecond, req.limits.iosPerKSecond, used.iosPerKSecond, + req.limits.infinity, req.isLastShard, iopsSample, SERVER_KNOBS->STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS, lastKey, key ); + key = getSplitKey( remaining.bytesPerKSecond, estimated.bytesPerKSecond, req.limits.bytesPerKSecond, used.bytesPerKSecond, + req.limits.infinity, req.isLastShard, bandwidthSample, SERVER_KNOBS->STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS, lastKey, key ); + ASSERT( key != lastKey || used.bytes != 0 || used.bytesPerKSecond != 0 || used.iosPerKSecond != 0); + if( key == req.keys.end ) + break; + reply.splits.push_back_deep( reply.splits.arena(), key ); + + StorageMetrics diff = (getMetrics( KeyRangeRef(lastKey, key) ) + used); + remaining -= diff; + estimated -= diff; + + used = StorageMetrics(); + lastKey = key; + } + + reply.used = getMetrics( KeyRangeRef(lastKey, req.keys.end) ) + used; + req.reply.send(reply); + } catch (Error& e) { + req.reply.sendError(e); + } + } + + void getPhysicalMetrics( GetPhysicalMetricsRequest req, StorageBytes sb ){ + GetPhysicalMetricsReply rep; + + // SOMEDAY: make bytes dynamic with hard disk space + rep.load = getMetrics(allKeys); + + if (sb.free < 1e9 && g_random->random01() < 0.1) + TraceEvent(SevWarn, "PhysicalDiskMetrics") + .detail("free", sb.free) + .detail("total", sb.total) + .detail("available", sb.available) + .detail("load", rep.load.bytes); + + rep.free.bytes = sb.free; + rep.free.iosPerKSecond = 10e6; + rep.free.bytesPerKSecond = 100e9; + + rep.capacity.bytes = sb.total; + rep.capacity.iosPerKSecond = 10e6; + rep.capacity.bytesPerKSecond = 100e9; + + req.reply.send(rep); + } + + Future waitMetrics(WaitMetricsRequest req, Future delay); + +private: + static void collapse( KeyRangeMap& map, KeyRef const& key ) { + auto range = map.rangeContaining(key); + if (range == map.ranges().begin() || range == map.ranges().end()) return; + int value = range->value(); + auto prev = range; --prev; + if (prev->value() != value) return; + KeyRange keys = KeyRangeRef( prev->begin(), range->end() ); + map.insert( keys, value ); + } + + static void add( KeyRangeMap& map, KeyRangeRef const& keys, int delta ) { + auto rs = map.modify(keys); + for(auto r = rs.begin(); r != rs.end(); ++r) + r->value() += delta; + collapse( map, keys.begin ); + collapse( map, keys.end ); + } +}; + +//Contains information about whether or not a key-value pair should be included in a byte sample +//Also contains size information about the byte sample +struct ByteSampleInfo { + bool inSample; + + //Actual size of the key value pair + int64_t size; + + //The recorded size of the sample (max of bytesPerSample, size) + int64_t sampledSize; +}; + +//Determines whether a key-value pair should be included in a byte sample +//Also returns size information about the sample +ByteSampleInfo isKeyValueInSample(KeyValueRef keyValue); diff --git a/fdbserver/StorageMetrics.h b/fdbserver/StorageMetrics.h new file mode 100644 index 0000000000..9a9e611059 --- /dev/null +++ b/fdbserver/StorageMetrics.h @@ -0,0 +1,25 @@ +/* + * StorageMetrics.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#if defined(NO_INTELLISENSE) + #include "StorageMetrics.actor.g.h" +#else + #include "StorageMetrics.actor.h" +#endif \ No newline at end of file diff --git a/fdbserver/TLogInterface.h b/fdbserver/TLogInterface.h new file mode 100644 index 0000000000..4fa0de5531 --- /dev/null +++ b/fdbserver/TLogInterface.h @@ -0,0 +1,234 @@ +/* + * TLogInterface.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_TLOGINTERFACE_H +#define FDBSERVER_TLOGINTERFACE_H +#pragma once + +#include "fdbclient/FDBTypes.h" +#include "fdbclient/CommitTransaction.h" +#include "fdbclient/MutationList.h" +#include "fdbclient/StorageServerInterface.h" +#include + +struct TLogInterface { + enum { LocationAwareLoadBalance = 1 }; + LocalityData locality; + UID uniqueID; + RequestStream< struct TLogPeekRequest > peekMessages; + RequestStream< struct TLogPopRequest > popMessages; + + RequestStream< struct TLogCommitRequest > commit; + RequestStream< ReplyPromise< struct TLogLockResult > > lock; // first stage of database recovery + RequestStream< struct TLogQueuingMetricsRequest > getQueuingMetrics; + RequestStream< struct TLogConfirmRunningRequest > confirmRunning; // used for getReadVersion requests from client + RequestStream> waitFailure; + RequestStream< struct TLogRecoveryFinishedRequest > recoveryFinished; + + TLogInterface() : uniqueID( g_random->randomUniqueID() ) {} + UID id() const { return uniqueID; } + std::string toString() const { return id().shortString(); } + bool operator == ( TLogInterface const& r ) const { return id() == r.id(); } + NetworkAddress address() const { return peekMessages.getEndpoint().address; } + void initEndpoints() { + getQueuingMetrics.getEndpoint( TaskTLogQueuingMetrics ); + popMessages.getEndpoint( TaskTLogPop ); + peekMessages.getEndpoint( TaskTLogPeek ); + confirmRunning.getEndpoint( TaskTLogConfirmRunning ); + commit.getEndpoint( TaskTLogCommit ); + } + + template + void serialize( Ar& ar ) { + ar & uniqueID & locality & peekMessages & popMessages + & commit & lock & getQueuingMetrics & confirmRunning & waitFailure & recoveryFinished; + } +}; + +struct TLogRecoveryFinishedRequest { + ReplyPromise reply; + + TLogRecoveryFinishedRequest() {} + + template + void serialize( Ar& ar ) { + ar & reply; + } +}; + +struct TLogLockResult { + Version end; + Version knownCommittedVersion; + std::vector tags; + + template + void serialize( Ar& ar ) { + ar & end & knownCommittedVersion & tags; + } +}; + +struct TLogConfirmRunningRequest { + Optional debugID; + ReplyPromise reply; + + TLogConfirmRunningRequest() {} + TLogConfirmRunningRequest( Optional debugID ) : debugID(debugID) {} + + template + void serialize( Ar& ar ) { + ar & debugID & reply; + } +}; + +struct VersionUpdateRef { + Version version; + MutationListRef mutations; + bool isPrivateData; + + VersionUpdateRef() : isPrivateData(false), version(invalidVersion) {} + VersionUpdateRef( Arena& to, const VersionUpdateRef& from ) : version(from.version), mutations( to, from.mutations ), isPrivateData( from.isPrivateData ) {} + int totalSize() const { return mutations.totalSize(); } + int expectedSize() const { return mutations.expectedSize(); } + + template + void serialize( Ar& ar ) { + ar & version & mutations & isPrivateData; + } +}; + +struct VerUpdateRef { + Version version; + VectorRef mutations; + bool isPrivateData; + + VerUpdateRef() : isPrivateData(false), version(invalidVersion) {} + VerUpdateRef( Arena& to, const VerUpdateRef& from ) : version(from.version), mutations( to, from.mutations ), isPrivateData( from.isPrivateData ) {} + int expectedSize() const { return mutations.expectedSize(); } + + template + void serialize( Ar& ar ) { + ar & version & mutations & isPrivateData; + } +}; + +struct TLogPeekReply { + Arena arena; + StringRef messages; + Version end; + Optional popped; + Version maxKnownVersion; + + template + void serialize(Ar& ar) { + ar & arena & messages & end & popped & maxKnownVersion; + } +}; + +struct TLogPeekRequest { + Arena arena; + Version begin; + Tag tag; + bool returnIfBlocked; + Optional> sequence; + ReplyPromise reply; + + TLogPeekRequest( Version begin, Tag tag, bool returnIfBlocked, Optional> sequence = Optional>() ) : begin(begin), tag(tag), returnIfBlocked(returnIfBlocked), sequence(sequence) {} + TLogPeekRequest() {} + + template + void serialize(Ar& ar) { + ar & arena & begin & tag & returnIfBlocked & sequence & reply; + } +}; + +struct TLogPopRequest { + Arena arena; + Version to; + Tag tag; + ReplyPromise reply; + + TLogPopRequest( Version to, Tag tag ) : to(to), tag(tag) {} + TLogPopRequest() {} + + template + void serialize(Ar& ar) { + ar & arena & to & tag & reply; + } +}; + +struct TagMessagesRef { + Tag tag; + VectorRef messageOffsets; + + TagMessagesRef() {} + TagMessagesRef(Arena &a, const TagMessagesRef &from) : tag(from.tag), messageOffsets(a, from.messageOffsets) {} + + size_t expectedSize() const { + return messageOffsets.expectedSize(); + } + + template + void serialize(Ar& ar) { + ar & tag & messageOffsets; + } +}; + +struct TLogCommitRequest { + Arena arena; + Version prevVersion, version, knownCommittedVersion; + + StringRef messages; // Each message prefixed by a 4-byte length + VectorRef< TagMessagesRef > tags; + + ReplyPromise reply; + Optional debugID; + + TLogCommitRequest() {} + TLogCommitRequest( const Arena& a, Version prevVersion, Version version, Version knownCommittedVersion, StringRef messages, VectorRef< TagMessagesRef > tags, Optional debugID ) + : arena(a), prevVersion(prevVersion), version(version), knownCommittedVersion(knownCommittedVersion), messages(messages), tags(tags), debugID(debugID) {} + template + void serialize( Ar& ar ) { + ar & prevVersion & version & knownCommittedVersion & messages & tags & reply & arena & debugID; + } +}; + +struct TLogQueuingMetricsRequest { + ReplyPromise reply; + + template + void serialize(Ar& ar) { + ar & reply; + } +}; + +struct TLogQueuingMetricsReply { + double localTime; + int64_t instanceID; // changes if bytesDurable and bytesInput reset + int64_t bytesDurable, bytesInput; + StorageBytes storageBytes; + Version v; // committed version + + template + void serialize(Ar& ar) { + ar & localTime & instanceID & bytesDurable & bytesInput & storageBytes & v; + } +}; + +#endif diff --git a/fdbserver/TLogServer.actor.cpp b/fdbserver/TLogServer.actor.cpp new file mode 100644 index 0000000000..9b0dc08652 --- /dev/null +++ b/fdbserver/TLogServer.actor.cpp @@ -0,0 +1,1833 @@ +/* + * TLogServer.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/Hash3.h" +#include "flow/Stats.h" +#include "flow/UnitTest.h" +#include "fdbclient/NativeAPI.h" +#include "fdbclient/KeyRangeMap.h" +#include "fdbclient/SystemData.h" +#include "WorkerInterface.h" +#include "TLogInterface.h" +#include "flow/Notified.h" +#include "Knobs.h" +#include "IKeyValueStore.h" +#include "flow/ActorCollection.h" +#include "fdbrpc/FailureMonitor.h" +#include "IDiskQueue.h" +#include "fdbrpc/sim_validation.h" +#include "ServerDBInfo.h" +#include "LogSystem.h" +#include "WaitFailure.h" + +using std::pair; +using std::make_pair; +using std::min; +using std::max; + +//FIXME: also defined in worker, move to someplace available to both +#define DUMPTOKEN( name ) TraceEvent("DumpToken", recruited.id()).detail("Name", #name).detail("Token", name.getEndpoint().token) + +struct TLogQueueEntryRef { + UID id; + Version version; + Version knownCommittedVersion; + StringRef messages; + VectorRef< TagMessagesRef > tags; + + TLogQueueEntryRef() : version(0), knownCommittedVersion(0) {} + TLogQueueEntryRef(Arena &a, TLogQueueEntryRef const &from) + : version(from.version), knownCommittedVersion(from.knownCommittedVersion), id(from.id), messages(a, from.messages), tags(a, from.tags) { + } + + template + void serialize(Ar& ar) { + if( ar.protocolVersion() >= 0x0FDB00A460010001) { + ar & version & messages & tags & knownCommittedVersion & id; + } else if(ar.isDeserializing) { + ar & version & messages & tags; + knownCommittedVersion = 0; + id = UID(); + } + } + size_t expectedSize() const { + return messages.expectedSize() + tags.expectedSize(); + } +}; + +typedef Standalone TLogQueueEntry; + +struct TLogQueue : public IClosable { +public: + TLogQueue( IDiskQueue* queue, UID dbgid ) : queue(queue), dbgid(dbgid) {} + + // Each packet in the queue is + // uint32_t payloadSize + // uint8_t payload[payloadSize] (begins with uint64_t protocolVersion via IncludeVersion) + // uint8_t validFlag + + // TLogQueue is a durable queue of TLogQueueEntry objects with an interface similar to IDiskQueue + + // TLogQueue pushes (but not commits) are atomic - after commit fails to return, a prefix of entire calls to push are durable. This is + // implemented on top of the weaker guarantee of IDiskQueue::commit (that a prefix of bytes is durable) using validFlag and by + // padding any incomplete packet with zeros after recovery. + + // Before calling push, pop, or commit, the user must call readNext() until it throws + // end_of_stream(). It may not be called again thereafter. + Future readNext() { + return readNext( this ); + } + + void push( TLogQueueEntryRef const& qe ) { + BinaryWriter wr( Unversioned() ); // outer framing is not versioned + wr << uint32_t(0); + IncludeVersion().write(wr); // payload is versioned + wr << qe; + wr << uint8_t(1); + *(uint32_t*)wr.getData() = wr.getLength() - sizeof(uint32_t) - sizeof(uint8_t); + auto loc = queue->push( wr.toStringRef() ); + //TraceEvent("TLogQueueVersionWritten", dbgid).detail("Size", wr.getLength() - sizeof(uint32_t) - sizeof(uint8_t)).detail("Loc", loc); + version_location[qe.version] = loc; + } + void pop( Version upTo ) { + // Keep only the given and all subsequent version numbers + // Find the first version >= upTo + auto v = version_location.lower_bound(upTo); + if (v == version_location.begin()) return; + + if(v == version_location.end()) { + v = version_location.lastItem(); + } + else { + v.decrementNonEnd(); + } + + queue->pop( v->value ); + version_location.erase( version_location.begin(), v ); // ... and then we erase that previous version and all prior versions + } + Future commit() { return queue->commit(); } + + // Implements IClosable + virtual Future getError() { return queue->getError(); } + virtual Future onClosed() { return queue->onClosed(); } + virtual void dispose() { queue->dispose(); delete this; } + virtual void close() { queue->close(); delete this; } + +private: + IDiskQueue* queue; + Map version_location; // For the version of each entry that was push()ed, the end location of the serialized bytes + UID dbgid; + + ACTOR static Future readNext( TLogQueue* self ) { + state TLogQueueEntry result; + state int zeroFillSize = 0; + + loop { + Standalone h = wait( self->queue->readNext( sizeof(uint32_t) ) ); + if (h.size() != sizeof(uint32_t)) { + if (h.size()) { + TEST( true ); // Zero fill within size field + int payloadSize = 0; + memcpy(&payloadSize, h.begin(), h.size()); + zeroFillSize = sizeof(uint32_t)-h.size(); // zero fill the size itself + zeroFillSize += payloadSize+1; // and then the contents and valid flag + } + break; + } + + state uint32_t payloadSize = *(uint32_t*)h.begin(); + ASSERT( payloadSize < (100<<20) ); + + Standalone e = wait( self->queue->readNext( payloadSize+1 ) ); + if (e.size() != payloadSize+1) { + TEST( true ); // Zero fill within payload + zeroFillSize = payloadSize+1 - e.size(); + break; + } + + if (e[payloadSize]) { + Arena a = e.arena(); + ArenaReader ar( a, e.substr(0, payloadSize), IncludeVersion() ); + ar >> result; + self->version_location[result.version] = self->queue->getNextReadLocation(); + return result; + } + } + if (zeroFillSize) { + TEST( true ); // Fixing a partial commit at the end of the tlog queue + for(int i=0; iqueue->push( StringRef((const uint8_t*)"",1) ); + } + throw end_of_stream(); + } +}; + +struct LengthPrefixedStringRef { + // Represents a pointer to a string which is prefixed by a 4-byte length + // A LengthPrefixedStringRef is only pointer-sized (8 bytes vs 12 bytes for StringRef), but the corresponding string is 4 bytes bigger, and + // substring operations aren't efficient as they are with StringRef. It's a good choice when there might be lots of references to the same + // exact string. + + uint32_t* length; + + StringRef toStringRef() const { ASSERT(length); return StringRef( (uint8_t*)(length+1), *length ); } + int expectedSize() const { ASSERT(length); return *length; } + uint32_t* getLengthPtr() const { return length; } + + LengthPrefixedStringRef() : length(NULL) {} + LengthPrefixedStringRef(uint32_t* length) : length(length) {} +}; + +template +struct CompareFirst { + bool operator() (T const& lhs, T const& rhs) const { + return lhs.first < rhs.first; + } +}; + +struct LogData : NonCopyable, public ReferenceCounted { + struct TagData { + std::deque> version_messages; + bool nothing_persistent; // true means tag is *known* to have no messages in persistentData. false means nothing. + bool popped_recently; // `popped` has changed since last updatePersistentData + Version popped; // see popped version tracking contract below + bool update_version_sizes; + + TagData( Version popped, bool nothing_persistent, bool popped_recently, Tag tag ) : nothing_persistent(nothing_persistent), popped(popped), popped_recently(popped_recently), update_version_sizes(tag != txsTag) {} + + TagData(TagData&& r) noexcept(true) : version_messages(std::move(r.version_messages)), nothing_persistent(r.nothing_persistent), popped_recently(r.popped_recently), popped(r.popped), update_version_sizes(r.update_version_sizes) {} + void operator= (TagData&& r) noexcept(true) { + version_messages = std::move(r.version_messages); + nothing_persistent = r.nothing_persistent; + popped_recently = r.popped_recently; + popped = r.popped; + update_version_sizes = r.update_version_sizes; + } + + // Erase messages not needed to update *from* versions >= before (thus, messages with toversion <= before) + ACTOR Future eraseMessagesBefore( TagData *self, Version before, int64_t* gBytesErased, Reference tlogData, int taskID ) { + while(!self->version_messages.empty() && self->version_messages.front().first < before) { + Version version = self->version_messages.front().first; + std::pair &sizes = tlogData->version_sizes[version]; + int64_t messagesErased = 0; + + while(!self->version_messages.empty() && self->version_messages.front().first == version) { + auto const& m = self->version_messages.front(); + ++messagesErased; + + if(self->update_version_sizes) { + sizes.first -= m.second.expectedSize(); + } + + self->version_messages.pop_front(); + } + + tlogData->bytesDurable += (messagesErased * sizeof(std::pair) * SERVER_KNOBS->VERSION_MESSAGES_OVERHEAD_FACTOR_1024THS) >> 10; + *gBytesErased += (messagesErased * sizeof(std::pair) * SERVER_KNOBS->VERSION_MESSAGES_OVERHEAD_FACTOR_1024THS) >> 10; + Void _ = wait(yield(taskID)); + } + + return Void(); + } + + Future eraseMessagesBefore(Version before, int64_t* gBytesErased, Reference tlogData, int taskID) { + return eraseMessagesBefore(this, before, gBytesErased, tlogData, taskID); + } + }; + + /* + Popped version tracking contract needed by log system to implement ILogCursor::popped(): + + - Log server tracks for each (possible) tag a popped_version + Impl: TagData::popped (in memory) and persistTagPoppedKeys (in persistentData) + - popped_version(tag) is <= the maximum version for which log server (or a predecessor) is ever asked to pop the tag + Impl: Only increased by tLogPop() in response to either a pop request or recovery from a predecessor + - popped_version(tag) is > the maximum version for which log server is unable to peek messages due to previous pops (on this server or a predecessor) + Impl: Increased by tLogPop() atomically with erasing messages from memory; persisted by updatePersistentData() atomically with erasing messages from store; messages are not erased from queue where popped_version is not persisted + - LockTLogReply returns all tags which either have messages, or which have nonzero popped_versions + Impl: tag_data is present for all such tags + - peek(tag, v) returns the popped_version for tag if that is greater than v + Impl: Check tag_data->popped (after all waits) + */ + + bool stopped, initialized; + DBRecoveryCount recoveryCount; + + VersionMetricHandle persistentDataVersion, persistentDataDurableVersion; // The last version number in the portion of the log (written|durable) to persistentData + NotifiedVersion version, queueCommittedVersion; + Version queueCommittingVersion; + Version knownCommittedVersion; + + Deque>>> messageBlocks; + Map< Tag, TagData > tag_data; + + Map> version_sizes; + + CounterCollection cc; + Counter bytesInput; + Counter bytesDurable; + + UID logId; + Version newPersistentDataVersion; + Future removed; + TLogInterface tli; + PromiseStream> addActor; + + explicit LogData(TLogInterface interf) : knownCommittedVersion(0), tli(interf), logId(interf.id()), + cc("TLog", interf.id().toString()), + bytesInput("bytesInput", cc), + bytesDurable("bytesDurable", cc), + // These are initialized differently on init() or recovery + recoveryCount(), stopped(false), initialized(false), queueCommittingVersion(0), newPersistentDataVersion(invalidVersion) + { + persistentDataVersion.init(LiteralStringRef("TLog.PersistentDataVersion"), cc.id); + persistentDataDurableVersion.init(LiteralStringRef("TLog.PersistentDataDurableVersion"), cc.id); + version.initMetric(LiteralStringRef("TLog.Version"), cc.id); + queueCommittedVersion.initMetric(LiteralStringRef("TLog.QueueCommittedVersion"), cc.id); + + specialCounter(cc, "version", [this](){ return this->version.get(); }); + } + + LogEpoch epoch() const { return recoveryCount; } +}; + +struct TLogData : NonCopyable { + AsyncTrigger newLogData; + Deque queueOrder; + std::map> id_data; + + UID dbgid; + + IKeyValueStore* persistentData; + IDiskQueue* rawPersistentQueue; + TLogQueue *persistentQueue; + + int64_t diskQueueCommitBytes; + AsyncVar largeDiskQueueCommitBytes; //becomes true when diskQueueCommitBytes is greater than MAX_QUEUE_COMMIT_BYTES + + Reference> dbInfo; + + NotifiedVersion queueCommitEnd; + Version queueCommitBegin; + AsyncTrigger newVersion; + + int64_t instanceID; + int64_t bytesInput; + int64_t bytesDurable; + + Version prevVersion; + + struct peekTrackerData { + std::map> sequence_version; + double lastUpdate; + }; + + std::map peekTracker; + WorkerCache tlogCache; + + Future updatePersist; //SOMEDAY: integrate the recovery and update storage so that only one of them is committing to persistant data. + Future oldLogServer; + + PromiseStream> sharedActors; + + TLogData(UID dbgid, IKeyValueStore* persistentData, IDiskQueue * persistentQueue, Reference> const& dbInfo) + : dbgid(dbgid), instanceID(g_random->randomUniqueID().first()), + persistentData(persistentData), rawPersistentQueue(persistentQueue), persistentQueue(new TLogQueue(persistentQueue, dbgid)), + dbInfo(dbInfo), queueCommitBegin(0), queueCommitEnd(0), prevVersion(0), + diskQueueCommitBytes(0), largeDiskQueueCommitBytes(false), + bytesInput(0), bytesDurable(0), updatePersist(Void()) + { + //specialCounter(cc, "kvstoreBytesUsed", [this](){ return this->persistentData->getStorageBytes().used; }); + //specialCounter(cc, "kvstoreBytesFree", [this](){ return this->persistentData->getStorageBytes().free; }); + //specialCounter(cc, "kvstoreBytesAvailable", [this](){ return this->persistentData->getStorageBytes().available; }); + //specialCounter(cc, "kvstoreBytesTotal", [this](){ return this->persistentData->getStorageBytes().total; }); + //specialCounter(cc, "queueDiskBytesUsed", [this](){ return this->rawPersistentQueue->getStorageBytes().used; }); + //specialCounter(cc, "queueDiskBytesFree", [this](){ return this->rawPersistentQueue->getStorageBytes().free; }); + //specialCounter(cc, "queueDiskBytesAvailable", [this](){ return this->rawPersistentQueue->getStorageBytes().available; }); + //specialCounter(cc, "queueDiskBytesTotal", [this](){ return this->rawPersistentQueue->getStorageBytes().total; }); + } +}; + +ACTOR Future tLogLock( TLogData* self, ReplyPromise< TLogLockResult > reply, Reference logData ) { + state Version stopVersion = logData->version.get(); + + TEST(true); // TLog stopped by recovering master + TEST( logData->stopped ); + TEST( !logData->stopped ); + + TraceEvent("TLogStop", logData->logId).detail("Ver", stopVersion).detail("isStopped", logData->stopped).detail("queueCommitted", logData->queueCommittedVersion.get()); + + logData->stopped = true; + + // Lock once the current version has been committed + Void _ = wait( logData->queueCommittedVersion.whenAtLeast( stopVersion ) ); + + ASSERT(stopVersion == logData->version.get()); + + TLogLockResult result; + result.end = stopVersion; + result.knownCommittedVersion = logData->knownCommittedVersion; + for( auto & tag : logData->tag_data ) + result.tags.push_back( tag.key ); + + TraceEvent("TLogStop2", self->dbgid).detail("logId", logData->logId).detail("Ver", stopVersion).detail("isStopped", logData->stopped).detail("queueCommitted", logData->queueCommittedVersion.get()).detail("tags", describe(result.tags)); + + + reply.send( result ); + return Void(); +} + +KeyRange prefixRange( KeyRef prefix ) { + Key end = strinc(prefix); + return KeyRangeRef( prefix, end ); +} + +////// Persistence format (for self->persistentData) + +// Immutable keys +static const KeyValueRef persistFormat( LiteralStringRef( "Format" ), LiteralStringRef("FoundationDB/LogServer/2/3") ); +static const KeyRangeRef persistFormatReadableRange( LiteralStringRef("FoundationDB/LogServer/2/2"), LiteralStringRef("FoundationDB/LogServer/2/4") ); +static const KeyRangeRef persistRecoveryCountKeys = KeyRangeRef( LiteralStringRef( "DbRecoveryCount/" ), LiteralStringRef( "DbRecoveryCount0" ) ); + +// Updated on updatePersistentData() +static const KeyRangeRef persistCurrentVersionKeys = KeyRangeRef( LiteralStringRef( "version/" ), LiteralStringRef( "version0" ) ); +static const KeyRange persistTagMessagesKeys = prefixRange(LiteralStringRef("TagMsg/")); +static const KeyRange persistTagPoppedKeys = prefixRange(LiteralStringRef("TagPop/")); + +static Key persistTagMessagesKey( UID id, Tag tag, Version version ) { + BinaryWriter wr( Unversioned() ); + wr.serializeBytes(persistTagMessagesKeys.begin); + wr << id; + wr << tag; + wr << bigEndian64( version ); + return wr.toStringRef(); +} + +static Key persistTagPoppedKey( UID id, Tag tag ) { + BinaryWriter wr(Unversioned()); + wr.serializeBytes( persistTagPoppedKeys.begin ); + wr << id; + wr << tag; + return wr.toStringRef(); +} + +static Value persistTagPoppedValue( Version popped ) { + return BinaryWriter::toValue( popped, Unversioned() ); +} + +static Tag decodeTagPoppedKey( KeyRef id, KeyRef key ) { + Tag s; + BinaryReader rd( key.removePrefix(persistTagPoppedKeys.begin).removePrefix(id), Unversioned() ); + rd >> s; + return s; +} + +static Version decodeTagPoppedValue( ValueRef value ) { + return BinaryReader::fromStringRef( value, Unversioned() ); +} + +static StringRef stripTagMessagesKey( StringRef key ) { + return key.substr( sizeof(UID) + sizeof(Tag) + persistTagMessagesKeys.begin.size() ); +} + +static Version decodeTagMessagesKey( StringRef key ) { + return bigEndian64( BinaryReader::fromStringRef( stripTagMessagesKey(key), Unversioned() ) ); +} + +void updatePersistentPopped( TLogData* self, Reference logData, Tag tag, LogData::TagData& data ) { + if (!data.popped_recently) return; + self->persistentData->set(KeyValueRef( persistTagPoppedKey(logData->logId, tag), persistTagPoppedValue(data.popped) )); + data.popped_recently = false; + + if (data.nothing_persistent) return; + + self->persistentData->clear( KeyRangeRef( + persistTagMessagesKey( logData->logId, tag, Version(0) ), + persistTagMessagesKey( logData->logId, tag, data.popped ) ) ); + if (data.popped > logData->persistentDataVersion) + data.nothing_persistent = true; +} + +ACTOR Future updatePersistentData( TLogData* self, Reference logData, Version newPersistentDataVersion ) { + // PERSIST: Changes self->persistentDataVersion and writes and commits the relevant changes + ASSERT( newPersistentDataVersion <= logData->version.get() ); + ASSERT( newPersistentDataVersion <= logData->queueCommittedVersion.get() ); + ASSERT( newPersistentDataVersion > logData->persistentDataVersion ); + ASSERT( logData->persistentDataVersion == logData->persistentDataDurableVersion ); + + //TraceEvent("updatePersistentData", self->dbgid).detail("seq", newPersistentDataSeq); + + state bool anyData = false; + state Map::iterator tag; + // For all existing tags + for(tag = logData->tag_data.begin(); tag != logData->tag_data.end(); ++tag) { + state Version currentVersion = 0; + // Clear recently popped versions from persistentData if necessary + updatePersistentPopped( self, logData, tag->key, tag->value ); + // Transfer unpopped messages with version numbers less than newPersistentDataVersion to persistentData + state std::deque>::iterator msg = tag->value.version_messages.begin(); + while(msg != tag->value.version_messages.end() && msg->first <= newPersistentDataVersion) { + currentVersion = msg->first; + anyData = true; + tag->value.nothing_persistent = false; + BinaryWriter wr( Unversioned() ); + + for(; msg != tag->value.version_messages.end() && msg->first == currentVersion; ++msg) + wr << msg->second.toStringRef(); + + self->persistentData->set( KeyValueRef( persistTagMessagesKey( logData->logId, tag->key, currentVersion ), wr.toStringRef() ) ); + + Future f = yield(TaskUpdateStorage); + if(!f.isReady()) { + Void _ = wait(f); + msg = std::upper_bound(tag->value.version_messages.begin(), tag->value.version_messages.end(), std::make_pair(currentVersion, LengthPrefixedStringRef()), CompareFirst>()); + } + } + + Void _ = wait(yield(TaskUpdateStorage)); + } + + self->persistentData->set( KeyValueRef( BinaryWriter::toValue(logData->logId,Unversioned()).withPrefix(persistCurrentVersionKeys.begin), BinaryWriter::toValue(newPersistentDataVersion, Unversioned()) ) ); + logData->persistentDataVersion = newPersistentDataVersion; + + Void _ = wait( self->persistentData->commit() ); // SOMEDAY: This seems to be running pretty often, should we slow it down??? + Void _ = wait( delay(0, TaskUpdateStorage) ); + + // Now that the changes we made to persistentData are durable, erase the data we moved from memory and the queue, increase bytesDurable accordingly, and update persistentDataDurableVersion. + + TEST(anyData); // TLog moved data to persistentData + logData->persistentDataDurableVersion = newPersistentDataVersion; + + for(tag = logData->tag_data.begin(); tag != logData->tag_data.end(); ++tag) { + Void _ = wait(tag->value.eraseMessagesBefore( newPersistentDataVersion+1, &self->bytesDurable, logData, TaskUpdateStorage )); + Void _ = wait(yield(TaskUpdateStorage)); + } + + logData->version_sizes.erase(logData->version_sizes.begin(), logData->version_sizes.lower_bound(logData->persistentDataDurableVersion)); + + Void _ = wait(yield(TaskUpdateStorage)); + + while(!logData->messageBlocks.empty() && logData->messageBlocks.front().first <= newPersistentDataVersion) { + logData->bytesDurable += logData->messageBlocks.front().second.size() * SERVER_KNOBS->TLOG_MESSAGE_BLOCK_OVERHEAD_FACTOR; + self->bytesDurable += logData->messageBlocks.front().second.size() * SERVER_KNOBS->TLOG_MESSAGE_BLOCK_OVERHEAD_FACTOR; + logData->messageBlocks.pop_front(); + Void _ = wait(yield(TaskUpdateStorage)); + } + + ASSERT(logData->bytesDurable.getValue() <= logData->bytesInput.getValue()); + + if( self->queueCommitEnd.get() > 0 ) + self->persistentQueue->pop( newPersistentDataVersion+1 ); // SOMEDAY: this can cause a slow task (~0.5ms), presumably from erasing too many versions. Should we limit the number of versions cleared at a time? + + return Void(); +} + +// This function (and updatePersistentData, which is called by this function) run at a low priority and can soak up all CPU resources. +// For this reason, they employ aggressive use of yields to avoid causing slow tasks that could introduce latencies for more important +// work (e.g. commits). +ACTOR Future updateStorage( TLogData* self ) { + while(self->queueOrder.size() && !self->id_data.count(self->queueOrder.front())) { + self->queueOrder.pop_front(); + } + + if(!self->queueOrder.size()) { + Void _ = wait( delay(BUGGIFY ? SERVER_KNOBS->BUGGIFY_TLOG_STORAGE_MIN_UPDATE_INTERVAL : SERVER_KNOBS->TLOG_STORAGE_MIN_UPDATE_INTERVAL, TaskUpdateStorage) ); + return Void(); + } + + state Reference logData = self->id_data[self->queueOrder.front()]; + state Version prevVersion = 0; + state Version nextVersion = 0; + state int totalSize = 0; + + if(logData->stopped) { + //FIXME: do we really want to make everything durable for this generation once we pass the threshold? + //FIXME: do we need yields? + if (self->bytesInput - self->bytesDurable >= SERVER_KNOBS->TLOG_SPILL_THRESHOLD) { + while(logData->persistentDataDurableVersion != logData->version.get()) { + std::vector>::iterator, std::deque>::iterator>> iters; + for(auto tag = logData->tag_data.begin(); tag != logData->tag_data.end(); ++tag) + iters.push_back(std::make_pair(tag->value.version_messages.begin(), tag->value.version_messages.end())); + + nextVersion = 0; + while( totalSize < SERVER_KNOBS->UPDATE_STORAGE_BYTE_LIMIT || nextVersion <= logData->persistentDataVersion ) { + nextVersion = logData->version.get(); + for( auto &it : iters ) + if(it.first != it.second) + nextVersion = std::min( nextVersion, it.first->first + 1 ); + + if(nextVersion == logData->version.get()) + break; + + for( auto &it : iters ) { + while (it.first != it.second && it.first->first < nextVersion) { + totalSize += it.first->second.expectedSize(); + ++it.first; + } + } + } + + Void _ = wait( logData->queueCommittedVersion.whenAtLeast( nextVersion ) ); + Void _ = wait( delay(0, TaskUpdateStorage) ); + + //TraceEvent("TlogUpdatePersist", self->dbgid).detail("logId", logData->logId).detail("nextVersion", nextVersion).detail("version", logData->version.get()).detail("persistentDataDurableVer", logData->persistentDataDurableVersion).detail("queueCommitVer", logData->queueCommittedVersion.get()).detail("persistDataVer", logData->persistentDataVersion); + if (nextVersion > logData->persistentDataVersion) { + self->updatePersist = updatePersistentData(self, logData, nextVersion); + Void _ = wait( self->updatePersist ); + } else { + Void _ = wait( delay(BUGGIFY ? SERVER_KNOBS->BUGGIFY_TLOG_STORAGE_MIN_UPDATE_INTERVAL : SERVER_KNOBS->TLOG_STORAGE_MIN_UPDATE_INTERVAL, TaskUpdateStorage) ); + } + } + + self->queueOrder.pop_front(); + Void _ = wait( delay(0.0, TaskUpdateStorage) ); + } else { + Void _ = wait( delay(BUGGIFY ? SERVER_KNOBS->BUGGIFY_TLOG_STORAGE_MIN_UPDATE_INTERVAL : SERVER_KNOBS->TLOG_STORAGE_MIN_UPDATE_INTERVAL, TaskUpdateStorage) ); + } + } + else if(logData->initialized) { + ASSERT(self->queueOrder.size() == 1); + state Map>::iterator sizeItr = logData->version_sizes.begin(); + while( totalSize < SERVER_KNOBS->UPDATE_STORAGE_BYTE_LIMIT && sizeItr != logData->version_sizes.end() + && (logData->bytesInput.getValue() - logData->bytesDurable.getValue() - totalSize >= SERVER_KNOBS->TLOG_SPILL_THRESHOLD || sizeItr->value.first == 0) ) + { + Void _ = wait( yield(TaskUpdateStorage) ); + + ++sizeItr; + nextVersion = sizeItr == logData->version_sizes.end() ? logData->version.get() : sizeItr->key; + + state Map::iterator tag; + for(tag = logData->tag_data.begin(); tag != logData->tag_data.end(); ++tag) { + auto it = std::lower_bound(tag->value.version_messages.begin(), tag->value.version_messages.end(), std::make_pair(prevVersion, LengthPrefixedStringRef()), CompareFirst>()); + for(; it != tag->value.version_messages.end() && it->first < nextVersion; ++it) { + totalSize += it->second.expectedSize(); + } + + Void _ = wait(yield(TaskUpdateStorage)); + } + + prevVersion = nextVersion; + } + + nextVersion = std::max(nextVersion, logData->persistentDataVersion); + + TraceEvent("UpdateStorageVer", logData->logId).detail("nextVersion", nextVersion).detail("persistentDataVersion", logData->persistentDataVersion).detail("totalSize", totalSize); + + Void _ = wait( logData->queueCommittedVersion.whenAtLeast( nextVersion ) ); + Void _ = wait( delay(0, TaskUpdateStorage) ); + + if (nextVersion > logData->persistentDataVersion) { + self->updatePersist = updatePersistentData(self, logData, nextVersion); + Void _ = wait( self->updatePersist ); + } + + if( totalSize < SERVER_KNOBS->UPDATE_STORAGE_BYTE_LIMIT ) { + Void _ = wait( delay(BUGGIFY ? SERVER_KNOBS->BUGGIFY_TLOG_STORAGE_MIN_UPDATE_INTERVAL : SERVER_KNOBS->TLOG_STORAGE_MIN_UPDATE_INTERVAL, TaskUpdateStorage) ); + } + else { + //recovery wants to commit to persistant data when updatePersistentData is not active, this delay ensures that immediately after + //updatePersist returns another one has not been started yet. + Void _ = wait( delay(0.0, TaskUpdateStorage) ); + } + } else { + Void _ = wait( delay(BUGGIFY ? SERVER_KNOBS->BUGGIFY_TLOG_STORAGE_MIN_UPDATE_INTERVAL : SERVER_KNOBS->TLOG_STORAGE_MIN_UPDATE_INTERVAL, TaskUpdateStorage) ); + } + return Void(); +} + +ACTOR Future updateStorageLoop( TLogData* self ) { + Void _ = wait(delay(0, TaskUpdateStorage)); + + loop { + Void _ = wait( updateStorage(self) ); + } +} + +void commitMessages( Reference self, Version version, Arena arena, StringRef messages, VectorRef< TagMessagesRef > tags, int64_t& bytesInput) { + // SOMEDAY: This method of copying messages is reasonably memory efficient, but it's still a lot of bytes copied. Find a + // way to do the memory allocation right as we receive the messages in the network layer. + + int64_t addedBytes = 0; + int64_t expectedBytes = 0; + + if(!messages.size()) { + return; + } + + StringRef messages1; // the first block of messages, if they aren't all stored contiguously. otherwise empty + + // Grab the last block in the blocks list so we can share its arena + // We pop all of the elements of it to create a "fresh" vector that starts at the end of the previous vector + Standalone> block; + if(self->messageBlocks.empty()) { + block = Standalone>(); + block.reserve(block.arena(), std::max(SERVER_KNOBS->TLOG_MESSAGE_BLOCK_BYTES, messages.size())); + } + else { + block = self->messageBlocks.back().second; + } + + block.pop_front(block.size()); + + // If the current batch of messages doesn't fit entirely in the remainder of the last block in the list + if(messages.size() + block.size() > block.capacity()) { + // Find how many messages will fit + LengthPrefixedStringRef r((uint32_t*)messages.begin()); + uint8_t const* end = messages.begin() + block.capacity() - block.size(); + while(r.toStringRef().end() <= end) { + r = LengthPrefixedStringRef( (uint32_t*)r.toStringRef().end() ); + } + + // Fill up the rest of this block + int bytes = (uint8_t*)r.getLengthPtr()-messages.begin(); + if (bytes) { + TEST(true); // Splitting commit messages across multiple blocks + messages1 = StringRef(block.end(), bytes); + block.append(block.arena(), messages.begin(), bytes); + self->messageBlocks.push_back( std::make_pair(version, block) ); + addedBytes += int64_t(block.size()) * SERVER_KNOBS->TLOG_MESSAGE_BLOCK_OVERHEAD_FACTOR; + messages = messages.substr(bytes); + } + + // Make a new block + block = Standalone>(); + block.reserve(block.arena(), std::max(SERVER_KNOBS->TLOG_MESSAGE_BLOCK_BYTES, messages.size())); + } + + // Copy messages into block + ASSERT(messages.size() <= block.capacity() - block.size()); + block.append(block.arena(), messages.begin(), messages.size()); + self->messageBlocks.push_back( std::make_pair(version, block) ); + addedBytes += int64_t(block.size()) * SERVER_KNOBS->TLOG_MESSAGE_BLOCK_OVERHEAD_FACTOR; + messages = StringRef(block.end()-messages.size(), messages.size()); + + for(auto tag = tags.begin(); tag != tags.end(); ++tag) { + int64_t tagMessages = 0; + + auto tsm = self->tag_data.find(tag->tag); + if (tsm == self->tag_data.end()) { + tsm = self->tag_data.insert( mapPair(std::move(Tag(tag->tag)), LogData::TagData(Version(0), true, true, tag->tag) ), false ); + } + + if (version >= tsm->value.popped) { + for(int m = 0; m < tag->messageOffsets.size(); ++m) { + int offs = tag->messageOffsets[m]; + uint8_t const* p = offs < messages1.size() ? messages1.begin() + offs : messages.begin() + offs - messages1.size(); + tsm->value.version_messages.push_back(std::make_pair(version, LengthPrefixedStringRef((uint32_t*)p))); + if(tsm->value.version_messages.back().second.expectedSize() > SERVER_KNOBS->MAX_MESSAGE_SIZE) { + TraceEvent(SevWarnAlways, "LargeMessage").detail("Size", tsm->value.version_messages.back().second.expectedSize()); + } + if (tag->tag != txsTag) + expectedBytes += tsm->value.version_messages.back().second.expectedSize(); + + ++tagMessages; + } + } + + // The factor of VERSION_MESSAGES_OVERHEAD is intended to be an overestimate of the actual memory used to store this data in a std::deque. + // In practice, this number is probably something like 528/512 ~= 1.03, but this could vary based on the implementation. + // There will also be a fixed overhead per std::deque, but its size should be trivial relative to the size of the TLog + // queue and can be thought of as increasing the capacity of the queue slightly. + addedBytes += (tagMessages * sizeof(std::pair) * SERVER_KNOBS->VERSION_MESSAGES_OVERHEAD_FACTOR_1024THS) >> 10; + } + + self->version_sizes[version] = make_pair(expectedBytes, expectedBytes); + self->bytesInput += addedBytes; + bytesInput += addedBytes; + + //TraceEvent("TLogPushed", self->dbgid).detail("Bytes", addedBytes).detail("MessageBytes", messages.size()).detail("Tags", tags.size()).detail("expectedBytes", expectedBytes).detail("mCount", mCount).detail("tCount", tCount); +} + +Version poppedVersion( Reference self, Tag tag) { + auto mapIt = self->tag_data.find(tag); + if (mapIt == self->tag_data.end()) + return Version(0); + return mapIt->value.popped; +} + +std::deque> & get_version_messages( Reference self, Tag tag ) { + auto mapIt = self->tag_data.find(tag); + if (mapIt == self->tag_data.end()) { + static std::deque> empty; + return empty; + } + return mapIt->value.version_messages; +}; + +ACTOR Future tLogPop( TLogData* self, TLogPopRequest req, Reference logData ) { + auto ti = logData->tag_data.find(req.tag); + if (ti == logData->tag_data.end()) { + ti = logData->tag_data.insert( mapPair(std::move(Tag(req.tag)), LogData::TagData(req.to, true, true, req.tag)) ); + } else if (req.to > ti->value.popped) { + ti->value.popped = req.to; + ti->value.popped_recently = true; + //if (to.epoch == self->epoch()) + if ( req.to > logData->persistentDataDurableVersion ) + Void _ = wait(ti->value.eraseMessagesBefore( req.to, &self->bytesDurable, logData, TaskTLogPop )); + //TraceEvent("TLogPop", self->dbgid).detail("Tag", req.tag).detail("To", req.to); + } + + req.reply.send(Void()); + return Void(); +} + +void peekMessagesFromMemory( Reference self, TLogPeekRequest const& req, BinaryWriter& messages, Version& endVersion ) { + ASSERT( !messages.getLength() ); + + auto& deque = get_version_messages(self, req.tag); + //TraceEvent("tLogPeekMem", self->dbgid).detail("Tag", printable(req.tag1)).detail("pDS", self->persistentDataSequence).detail("pDDS", self->persistentDataDurableSequence).detail("Oldest", map1.empty() ? 0 : map1.begin()->key ).detail("OldestMsgCount", map1.empty() ? 0 : map1.begin()->value.size()); + + Version begin = std::max( req.begin, self->persistentDataDurableVersion+1 ); + auto it = std::lower_bound(deque.begin(), deque.end(), std::make_pair(begin, LengthPrefixedStringRef()), CompareFirst>()); + + Version currentVersion = -1; + for(; it != deque.end(); ++it) { + if(it->first != currentVersion) { + if (messages.getLength() >= SERVER_KNOBS->DESIRED_TOTAL_BYTES) { + endVersion = it->first; + //TraceEvent("tLogPeekMessagesReached2", self->dbgid); + break; + } + + currentVersion = it->first; + messages << int32_t(-1) << currentVersion; + } + + messages << it->second.toStringRef(); + } +} + +ACTOR Future tLogPeekMessages( TLogData* self, TLogPeekRequest req, Reference logData ) { + state BinaryWriter messages(Unversioned()); + state BinaryWriter messages2(Unversioned()); + state int sequence = -1; + state UID peekId; + + if(req.sequence.present()) { + try { + peekId = req.sequence.get().first; + sequence = req.sequence.get().second; + if(sequence > 0) { + auto& trackerData = self->peekTracker[peekId]; + trackerData.lastUpdate = now(); + Version ver = wait(trackerData.sequence_version[sequence].getFuture()); + req.begin = ver; + Void _ = wait(yield()); + } + } catch( Error &e ) { + if(e.code() == error_code_timed_out) { + req.reply.sendError(timed_out()); + return Void(); + } else { + throw; + } + } + } + + if( req.returnIfBlocked && logData->version.get() < req.begin ) { + req.reply.sendError(end_of_stream()); + return Void(); + } + + //TraceEvent("tLogPeekMessages0", self->dbgid).detail("reqBeginEpoch", req.begin.epoch).detail("reqBeginSeq", req.begin.sequence).detail("epoch", self->epoch()).detail("persistentDataSeq", self->persistentDataSequence).detail("Tag1", printable(req.tag1)).detail("Tag2", printable(req.tag2)); + // Wait until we have something to return that the caller doesn't already have + if( logData->version.get() < req.begin ) { + Void _ = wait( logData->version.whenAtLeast( req.begin ) ); + Void _ = wait( delay(SERVER_KNOBS->TLOG_PEEK_DELAY, g_network->getCurrentTask()) ); + } + + state Version endVersion = logData->version.get() + 1; + + //grab messages from disk + //TraceEvent("tLogPeekMessages", self->dbgid).detail("reqBeginEpoch", req.begin.epoch).detail("reqBeginSeq", req.begin.sequence).detail("epoch", self->epoch()).detail("persistentDataSeq", self->persistentDataSequence).detail("Tag1", printable(req.tag1)).detail("Tag2", printable(req.tag2)); + if( req.begin <= logData->persistentDataDurableVersion ) { + // Just in case the durable version changes while we are waiting for the read, we grab this data from memory. We may or may not actually send it depending on + // whether we get enough data from disk. + // SOMEDAY: Only do this if an initial attempt to read from disk results in insufficient data and the required data is no longer in memory + // SOMEDAY: Should we only send part of the messages we collected, to actually limit the size of the result? + + peekMessagesFromMemory( logData, req, messages2, endVersion ); + + Standalone> kvs = wait( + self->persistentData->readRange(KeyRangeRef( + persistTagMessagesKey(logData->logId, req.tag, req.begin), + persistTagMessagesKey(logData->logId, req.tag, logData->persistentDataDurableVersion + 1)), SERVER_KNOBS->DESIRED_TOTAL_BYTES, SERVER_KNOBS->DESIRED_TOTAL_BYTES)); + + //TraceEvent("TLogPeekResults", self->dbgid).detail("ForAddress", req.reply.getEndpoint().address).detail("Tag1Results", s1).detail("Tag2Results", s2).detail("Tag1ResultsLim", kv1.size()).detail("Tag2ResultsLim", kv2.size()).detail("Tag1ResultsLast", kv1.size() ? printable(kv1[0].key) : "").detail("Tag2ResultsLast", kv2.size() ? printable(kv2[0].key) : "").detail("Limited", limited).detail("NextEpoch", next_pos.epoch).detail("NextSeq", next_pos.sequence).detail("NowEpoch", self->epoch()).detail("NowSeq", self->sequence.getNextSequence()); + + for (auto &kv : kvs) { + auto ver = decodeTagMessagesKey(kv.key); + messages << int32_t(-1) << ver; + messages.serializeBytes(kv.value); + } + + if (kvs.expectedSize() >= SERVER_KNOBS->DESIRED_TOTAL_BYTES) + endVersion = decodeTagMessagesKey(kvs.end()[-1].key) + 1; + else + messages.serializeBytes( messages2.toStringRef() ); + } else { + peekMessagesFromMemory( logData, req, messages, endVersion ); + //TraceEvent("TLogPeekResults", self->dbgid).detail("ForAddress", req.reply.getEndpoint().address).detail("MessageBytes", messages.getLength()).detail("NextEpoch", next_pos.epoch).detail("NextSeq", next_pos.sequence).detail("NowSeq", self->sequence.getNextSequence()); + } + + Version poppedVer = poppedVersion(logData, req.tag); + + TLogPeekReply reply; + reply.maxKnownVersion = logData->version.get(); + if(poppedVer > req.begin) { + reply.popped = poppedVer; + reply.end = poppedVer; + } else { + reply.messages = messages.toStringRef(); + reply.end = endVersion; + } + //TraceEvent("TlogPeek", self->dbgid).detail("logId", logData->logId).detail("endVer", reply.end).detail("msgBytes", reply.messages.expectedSize()).detail("ForAddress", req.reply.getEndpoint().address); + + if(req.sequence.present()) { + auto& trackerData = self->peekTracker[peekId]; + trackerData.lastUpdate = now(); + auto& sequenceData = trackerData.sequence_version[sequence+1]; + if(sequenceData.isSet()) { + if(sequenceData.getFuture().get() != reply.end) { + TEST(true); //tlog peek second attempt ended at a different version + req.reply.sendError(timed_out()); + return Void(); + } + } else { + sequenceData.send(reply.end); + } + } + + req.reply.send( reply ); + return Void(); +} + +ACTOR Future doQueueCommit( TLogData* self, Reference logData ) { + state Version ver = logData->version.get(); + state Version commitNumber = self->queueCommitBegin+1; + self->queueCommitBegin = commitNumber; + logData->queueCommittingVersion = ver; + + Future c = self->persistentQueue->commit(); + self->diskQueueCommitBytes = 0; + self->largeDiskQueueCommitBytes.set(false); + + Void _ = wait(c); + Void _ = wait(self->queueCommitEnd.whenAtLeast(commitNumber-1)); + + //Calling check_yield instead of yield to avoid a destruction ordering problem in simulation + if(g_network->check_yield(g_network->getCurrentTask())) { + Void _ = wait(delay(0, g_network->getCurrentTask())); + } + + ASSERT( ver > logData->queueCommittedVersion.get() ); + + logData->queueCommittedVersion.set(ver); + self->queueCommitEnd.set(commitNumber); + + TraceEvent("TLogCommitDurable", self->dbgid).detail("Version", ver); + + return Void(); +} + +ACTOR Future commitQueue( TLogData* self ) { + state Reference logData; + + loop { + bool foundCount = 0; + for(auto it : self->id_data) { + if(!it.second->stopped) { + logData = it.second; + foundCount++; + } + } + + ASSERT(foundCount < 2); + if(!foundCount) { + Void _ = wait( self->newLogData.onTrigger() ); + continue; + } + + TraceEvent("commitQueueNewLog", self->dbgid).detail("logId", logData->logId).detail("version", logData->version.get()).detail("committing", logData->queueCommittingVersion).detail("commmitted", logData->queueCommittedVersion.get()); + + loop { + if(logData->stopped && logData->version.get() == std::max(logData->queueCommittingVersion, logData->queueCommittedVersion.get())) { + Void _ = wait( logData->queueCommittedVersion.whenAtLeast(logData->version.get() ) ); + break; + } + + choose { + when(Void _ = wait( logData->version.whenAtLeast( std::max(logData->queueCommittingVersion, logData->queueCommittedVersion.get()) + 1 ) ) ) { + while( self->queueCommitBegin != self->queueCommitEnd.get() && !self->largeDiskQueueCommitBytes.get() ) { + Void _ = wait( self->queueCommitEnd.whenAtLeast(self->queueCommitBegin) || self->largeDiskQueueCommitBytes.onChange() ); + } + self->sharedActors.send(doQueueCommit(self, logData)); + } + when(Void _ = wait(self->newLogData.onTrigger())) {} + } + } + } +} + +ACTOR Future tLogCommit( + TLogData* self, + TLogCommitRequest req, + Reference logData, + PromiseStream warningCollectorInput ) { + state Optional tlogDebugID; + if(req.debugID.present()) + { + tlogDebugID = g_nondeterministic_random->randomUniqueID(); + g_traceBatch.addAttach("CommitAttachID", req.debugID.get().first(), tlogDebugID.get().first()); + g_traceBatch.addEvent("CommitDebug", tlogDebugID.get().first(), "TLog.tLogCommit.BeforeWaitForVersion"); + } + + logData->knownCommittedVersion = std::max(logData->knownCommittedVersion, req.knownCommittedVersion); + + Void _ = wait( logData->version.whenAtLeast( req.prevVersion ) ); + + //Calling check_yield instead of yield to avoid a destruction ordering problem in simulation + if(g_network->check_yield(g_network->getCurrentTask())) { + Void _ = wait(delay(0, g_network->getCurrentTask())); + } + + if(logData->stopped) { + req.reply.sendError( tlog_stopped() ); + return Void(); + } + + if (logData->version.get() == req.prevVersion) { // Not a duplicate (check relies on no waiting between here and self->version.set() below!) + if(req.debugID.present()) + g_traceBatch.addEvent("CommitDebug", tlogDebugID.get().first(), "TLog.tLogCommit.Before"); + + TraceEvent("TLogCommit", logData->logId).detail("Version", req.version); + commitMessages(logData, req.version, req.arena, req.messages, req.tags, self->bytesInput); + + // Log the changes to the persistent queue, to be committed by commitQueue() + TLogQueueEntryRef qe; + qe.version = req.version; + qe.knownCommittedVersion = req.knownCommittedVersion; + qe.messages = req.messages; + qe.tags = req.tags; + qe.id = logData->logId; + self->persistentQueue->push( qe ); + + self->diskQueueCommitBytes += qe.expectedSize(); + if( self->diskQueueCommitBytes > SERVER_KNOBS->MAX_QUEUE_COMMIT_BYTES ) { + self->largeDiskQueueCommitBytes.set(true); + } + + // Notifies the commitQueue actor to commit persistentQueue, and also unblocks tLogPeekMessages actors + self->prevVersion = logData->version.get(); + logData->version.set( req.version ); + self->newVersion.trigger(); + + if(req.debugID.present()) + g_traceBatch.addEvent("CommitDebug", tlogDebugID.get().first(), "TLog.tLogCommit.AfterTLogCommit"); + } + // Send replies only once all prior messages have been received and committed. + Void _ = wait( timeoutWarning( logData->queueCommittedVersion.whenAtLeast( req.version ), 0.1, warningCollectorInput ) ); + + if(req.debugID.present()) + g_traceBatch.addEvent("CommitDebug", tlogDebugID.get().first(), "TLog.tLogCommit.After"); + + req.reply.send( Void() ); + return Void(); +} + +ACTOR Future initPersistentState( TLogData* self, Reference logData ) { + // PERSIST: Initial setup of persistentData for a brand new tLog for a new database + IKeyValueStore *storage = self->persistentData; + storage->set( persistFormat ); + storage->set( KeyValueRef( BinaryWriter::toValue(logData->logId,Unversioned()).withPrefix(persistCurrentVersionKeys.begin), BinaryWriter::toValue(logData->version.get(), Unversioned()) ) ); + storage->set( KeyValueRef( BinaryWriter::toValue(logData->logId,Unversioned()).withPrefix(persistRecoveryCountKeys.begin), BinaryWriter::toValue(logData->recoveryCount, Unversioned()) ) ); + + TraceEvent("TLogInitCommit", logData->logId); + Void _ = wait( self->updatePersist ); + Void _ = wait( self->persistentData->commit() ); + return Void(); +} + +ACTOR Future rejoinMasters( TLogData* self, TLogInterface tli, DBRecoveryCount recoveryCount ) { + state UID lastMasterID(0,0); + loop { + auto const& inf = self->dbInfo->get(); + bool isDisplaced = inf.recoveryCount >= recoveryCount && inf.recoveryState != 0 && + !std::count( inf.logSystemConfig.tLogs.begin(), inf.logSystemConfig.tLogs.end(), tli.id() ) && + !std::count( inf.priorCommittedLogServers.begin(), inf.priorCommittedLogServers.end(), tli.id() ); + for(int i = 0; i < inf.logSystemConfig.oldTLogs.size() && isDisplaced; i++) { + isDisplaced = !std::count( inf.logSystemConfig.oldTLogs[i].tLogs.begin(), inf.logSystemConfig.oldTLogs[i].tLogs.end(), tli.id() ); + } + if ( isDisplaced ) + { + TraceEvent("TLogDisplaced", tli.id()).detail("Reason", "DBInfoDoesNotContain").detail("recoveryCount", recoveryCount).detail("infRecoveryCount", inf.recoveryCount).detail("recoveryState", inf.recoveryState) + .detail("logSysConf", describe(inf.logSystemConfig.tLogs)).detail("priorLogs", describe(inf.priorCommittedLogServers)).detail("oldLogGens", inf.logSystemConfig.oldTLogs.size()); + if (BUGGIFY) Void _ = wait( delay( SERVER_KNOBS->BUGGIFY_WORKER_REMOVED_MAX_LAG * g_random->random01() ) ); + throw worker_removed(); + } + + if (self->dbInfo->get().master.id() != lastMasterID) { + // The TLogRejoinRequest is needed to establish communications with a new master, which doesn't have our TLogInterface + TLogRejoinRequest req; + req.myInterface = tli; + TraceEvent("TLogRejoining", self->dbgid).detail("Master", self->dbInfo->get().master.id()); + choose { + when ( bool success = wait( brokenPromiseToNever( self->dbInfo->get().master.tlogRejoin.getReply( req ) ) ) ) { + if (success) + lastMasterID = self->dbInfo->get().master.id(); + } + when ( Void _ = wait( self->dbInfo->onChange() ) ) { } + } + } else + Void _ = wait( self->dbInfo->onChange() ); + } +} + +ACTOR Future respondToRecovered( TLogInterface tli, Future recovery ) { + Void _ = wait( recovery ); + + loop { + TLogRecoveryFinishedRequest req = waitNext( tli.recoveryFinished.getFuture() ); + req.reply.send(Void()); + } +} + +ACTOR Future cleanupPeekTrackers( TLogData* self ) { + loop { + double minExpireTime = SERVER_KNOBS->PEEK_TRACKER_EXPIRATION_TIME; + auto it = self->peekTracker.begin(); + while(it != self->peekTracker.end()) { + double expireTime = SERVER_KNOBS->PEEK_TRACKER_EXPIRATION_TIME - now()-it->second.lastUpdate; + if(expireTime < 1.0e-6) { + for(auto seq : it->second.sequence_version) { + if(!seq.second.isSet()) { + seq.second.sendError(timed_out()); + } + } + it = self->peekTracker.erase(it); + } else { + minExpireTime = std::min(minExpireTime, expireTime); + ++it; + } + } + + Void _ = wait( delay(minExpireTime) ); + } +} + +void getQueuingMetrics( TLogData* self, TLogQueuingMetricsRequest const& req ) { + TLogQueuingMetricsReply reply; + reply.localTime = now(); + reply.instanceID = self->instanceID; + reply.bytesInput = self->bytesInput; + reply.bytesDurable = self->bytesDurable; + reply.storageBytes = self->persistentData->getStorageBytes(); + reply.v = self->prevVersion; + req.reply.send( reply ); +} + +ACTOR Future serveTLogInterface( TLogData* self, TLogInterface tli, Reference logData, PromiseStream warningCollectorInput ) { + loop choose { + when( TLogPeekRequest req = waitNext( tli.peekMessages.getFuture() ) ) { + logData->addActor.send( tLogPeekMessages( self, req, logData ) ); + } + when( TLogPopRequest req = waitNext( tli.popMessages.getFuture() ) ) { + logData->addActor.send( tLogPop( self, req, logData ) ); + } + when( TLogCommitRequest req = waitNext( tli.commit.getFuture() ) ) { + TEST(logData->stopped); // TLogCommitRequest while stopped + if (!logData->stopped) + logData->addActor.send( tLogCommit( self, req, logData, warningCollectorInput ) ); + else + req.reply.sendError( tlog_stopped() ); + } + when( ReplyPromise< TLogLockResult > reply = waitNext( tli.lock.getFuture() ) ) { + logData->addActor.send( tLogLock(self, reply, logData) ); + } + when (TLogQueuingMetricsRequest req = waitNext(tli.getQueuingMetrics.getFuture())) { + getQueuingMetrics(self, req); + } + when (TLogConfirmRunningRequest req = waitNext(tli.confirmRunning.getFuture())){ + if (req.debugID.present() ) { + UID tlogDebugID = g_nondeterministic_random->randomUniqueID(); + g_traceBatch.addAttach("TransactionAttachID", req.debugID.get().first(), tlogDebugID.first()); + g_traceBatch.addEvent("TransactionDebug", tlogDebugID.first(), "TLogServer.TLogConfirmRunningRequest"); + } + if (!logData->stopped) + req.reply.send(Void()); + else + req.reply.sendError( tlog_stopped() ); + } + } +} + +ACTOR Future tLogCore( TLogData* self, Reference logData, Future recovery ) { + if(logData->removed.isReady()) { + Void _ = wait(delay(0)); //to avoid iterator invalidation in restorePersistentState when removed is already ready + ASSERT(logData->removed.isError()); + + if(logData->removed.getError().code() != error_code_worker_removed) { + throw logData->removed.getError(); + } + + double inputSum = 0; + double durableSum = 0; + for(auto it : self->id_data) { + inputSum += it.second->bytesInput.getValue(); + durableSum += it.second->bytesDurable.getValue(); + TraceEvent("QueueMetrics1", self->dbgid).detail("logId", it.first).detail("input", it.second->bytesInput.getValue()).detail("durable", it.second->bytesDurable.getValue()); + } + + TraceEvent("RemoveMetrics1", self->dbgid).detail("logId", logData->logId).detail("inputSum", inputSum).detail("durableSum", durableSum).detail("nonDurableSum", inputSum - durableSum) + .detail("input", self->bytesInput).detail("durable", self->bytesDurable).detail("nonDurable", self->bytesInput - self->bytesDurable).detail("durableIncrease", logData->bytesInput.getValue() - logData->bytesDurable.getValue()); + + self->bytesDurable += logData->bytesInput.getValue() - logData->bytesDurable.getValue(); + logData->stopped = true; + self->id_data.erase(logData->logId); + TraceEvent("TLogRemoved", logData->logId); + + if(self->id_data.size() || (self->oldLogServer.isValid() && !self->oldLogServer.isReady())) { + return Void(); + } else { + throw logData->removed.getError(); + } + } + + TraceEvent("newLogData", self->dbgid).detail("logId", logData->logId); + logData->initialized = true; + self->newLogData.trigger(); + + state PromiseStream warningCollectorInput; + state Future warningCollector = timeoutWarningCollector( warningCollectorInput.getFuture(), 1.0, "TLogQueueCommitSlow", self->dbgid ); + state Future error = actorCollection( logData->addActor.getFuture() ); + + if( recovery.isValid() && !recovery.isReady()) { + logData->addActor.send( recovery ); + } + + logData->addActor.send( waitFailureServer(logData->tli.waitFailure.getFuture()) ); + logData->addActor.send( respondToRecovered(logData->tli, recovery) ); + logData->addActor.send( logData->removed ); + //FIXME: update tlogMetrics to include new information, or possibly only have one copy for the shared instance + logData->addActor.send( traceCounters("TLogMetrics", logData->logId, SERVER_KNOBS->STORAGE_LOGGING_DELAY, &logData->cc, self->dbgid.toString() + "/TLogMetrics")); + logData->addActor.send( serveTLogInterface(self, logData->tli, logData, warningCollectorInput) ); + + try { + Void _ = wait( error ); + throw internal_error(); + } catch( Error &e ) { + if( e.code() != error_code_worker_removed ) + throw; + + double inputSum = 0; + double durableSum = 0; + for(auto it : self->id_data) { + inputSum += it.second->bytesInput.getValue(); + durableSum += it.second->bytesDurable.getValue(); + TraceEvent("QueueMetrics2", self->dbgid).detail("logId", it.first).detail("input", it.second->bytesInput.getValue()).detail("durable", it.second->bytesDurable.getValue()); + } + + TraceEvent("RemoveMetrics2", self->dbgid).detail("logId", logData->logId).detail("inputSum", inputSum).detail("durableSum", durableSum).detail("nonDurableSum", inputSum - durableSum) + .detail("input", self->bytesInput).detail("durable", self->bytesDurable).detail("nonDurable", self->bytesInput - self->bytesDurable).detail("durableIncrease", logData->bytesInput.getValue() - logData->bytesDurable.getValue()); + + self->bytesDurable += logData->bytesInput.getValue() - logData->bytesDurable.getValue(); + logData->stopped = true; + + logData->addActor = PromiseStream>(); //there could be items still in the promise stream if one of the actors threw an error immediately + self->id_data.erase(logData->logId); + TraceEvent("TLogRemoved", logData->logId); + + if(self->id_data.size() || (self->oldLogServer.isValid() && !self->oldLogServer.isReady())) { + return Void(); + } else { + throw; + } + } +} + +ACTOR Future checkEmptyQueue(TLogData* self) { + TraceEvent("TLogCheckEmptyQueueBegin", self->dbgid); + try { + TLogQueueEntry r = wait( self->persistentQueue->readNext() ); + throw internal_error(); + } catch (Error& e) { + if (e.code() != error_code_end_of_stream) throw; + TraceEvent("TLogCheckEmptyQueueEnd", self->dbgid); + return Void(); + } +} + +ACTOR Future restorePersistentState( TLogData* self, LocalityData locality, Promise oldLog, PromiseStream tlogRequests ) { + state double startt = now(); + state Reference logData; + state KeyRange tagKeys; + // PERSIST: Read basic state from persistentData; replay persistentQueue but don't erase it + + TraceEvent("TLogRestorePersistentState", self->dbgid); + + IKeyValueStore *storage = self->persistentData; + state Future> fFormat = storage->readValue(persistFormat.key); + state Future>> fVers = storage->readRange(persistCurrentVersionKeys); + state Future>> fRecoverCounts = storage->readRange(persistRecoveryCountKeys); + + // FIXME: metadata in queue? + + Void _ = wait( waitForAll( (vector>>(), fFormat ) ) ); + Void _ = wait( waitForAll( (vector>>>(), fVers, fRecoverCounts) ) ); + + if (fFormat.get().present() && !persistFormatReadableRange.contains( fFormat.get().get() )) { + TraceEvent(SevError, "UnsupportedDBFormat", self->dbgid).detail("Format", printable(fFormat.get().get())).detail("Expected", persistFormat.value.toString()); + throw worker_recovery_failed(); + } + + if (!fFormat.get().present()) { + Standalone> v = wait( self->persistentData->readRange( KeyRangeRef(StringRef(), LiteralStringRef("\xff")), 1 ) ); + if (!v.size()) { + TEST(true); // The DB is completely empty, so it was never initialized. Delete it. + throw worker_removed(); + } else { + // This should never happen + TraceEvent(SevError, "NoDBFormatKey", self->dbgid).detail("FirstKey", printable(v[0].key)); + ASSERT( false ); + throw worker_recovery_failed(); + } + } + + state std::vector>> removed; + + if(fFormat.get().get() == LiteralStringRef("FoundationDB/LogServer/2/2")) { + TLogInterface recruited; + recruited.uniqueID = self->dbgid; + recruited.locality = locality; + recruited.initEndpoints(); + + DUMPTOKEN( recruited.peekMessages ); + DUMPTOKEN( recruited.popMessages ); + DUMPTOKEN( recruited.commit ); + DUMPTOKEN( recruited.lock ); + DUMPTOKEN( recruited.getQueuingMetrics ); + DUMPTOKEN( recruited.confirmRunning ); + + //FIXME: need for upgrades from 4.X to 5.0, remove once this upgrade path is no longer needed + oldLog.send(Void()); + while(!tlogRequests.isEmpty()) { + tlogRequests.getFuture().pop().reply.sendError(recruitment_failed()); + } + + Void _ = wait( oldTLog::tLog(self->persistentData, self->rawPersistentQueue, recruited, self->dbInfo) ); + throw internal_error(); + } + + ASSERT(fVers.get().size() == fRecoverCounts.get().size()); + + state int idx = 0; + for(idx = 0; idx < fVers.get().size(); idx++) { + state KeyRef rawId = fVers.get()[idx].key.removePrefix(persistCurrentVersionKeys.begin); + UID id1 = BinaryReader::fromStringRef( rawId, Unversioned() ); + UID id2 = BinaryReader::fromStringRef( fRecoverCounts.get()[idx].key.removePrefix(persistRecoveryCountKeys.begin), Unversioned() ); + ASSERT(id1 == id2); + + TLogInterface recruited; + recruited.uniqueID = id1; + recruited.locality = locality; + recruited.initEndpoints(); + + DUMPTOKEN( recruited.peekMessages ); + DUMPTOKEN( recruited.popMessages ); + DUMPTOKEN( recruited.commit ); + DUMPTOKEN( recruited.lock ); + DUMPTOKEN( recruited.getQueuingMetrics ); + DUMPTOKEN( recruited.confirmRunning ); + + logData = Reference( new LogData(recruited) ); + logData->stopped = true; + self->id_data[id1] = logData; + + Version ver = BinaryReader::fromStringRef( fVers.get()[idx].value, Unversioned() ); + logData->persistentDataVersion = ver; + logData->persistentDataDurableVersion = ver; + logData->version.set(ver); + logData->recoveryCount = BinaryReader::fromStringRef( fRecoverCounts.get()[idx].value, Unversioned() ); + logData->removed = rejoinMasters(self, recruited, logData->recoveryCount); + removed.push_back(errorOr(logData->removed)); + + TraceEvent("TLogRestorePersistentStateVer", id1).detail("ver", ver); + + // Restore popped keys. Pop operations that took place after the last (committed) updatePersistentDataVersion might be lost, but + // that is fine because we will get the corresponding data back, too. + tagKeys = prefixRange( rawId.withPrefix(persistTagPoppedKeys.begin) ); + loop { + if(logData->removed.isReady()) break; + Standalone> data = wait( self->persistentData->readRange( tagKeys, BUGGIFY ? 3 : 1<<30, 1<<20 ) ); + if (!data.size()) break; + ((KeyRangeRef&)tagKeys) = KeyRangeRef( keyAfter(data.back().key, tagKeys.arena()), tagKeys.end ); + + for(auto &kv : data) { + Tag tag = decodeTagPoppedKey(rawId, kv.key); + Version popped = decodeTagPoppedValue(kv.value); + TraceEvent("TLogRestorePop", logData->logId).detail("Tag", tag).detail("To", popped); + ASSERT( logData->tag_data.find(tag) == logData->tag_data.end() ); + logData->tag_data.insert( mapPair( std::move(Tag(tag)), LogData::TagData( popped, false, false, tag )) ); + } + } + } + + state Future allRemoved = waitForAll(removed); + state Version lastVer = 0; + state UID lastId = UID(1,1); //initialized so it will not compare equal to a default UID + state double recoverMemoryLimit = SERVER_KNOBS->TARGET_BYTES_PER_TLOG + SERVER_KNOBS->SPRING_BYTES_TLOG; + if (BUGGIFY) recoverMemoryLimit = std::max(SERVER_KNOBS->BUGGIFY_RECOVER_MEMORY_LIMIT, SERVER_KNOBS->TLOG_SPILL_THRESHOLD); + try { + loop { + if(allRemoved.isReady()) { + TEST(true); //all tlogs removed during queue recovery + throw worker_removed(); + } + choose { + when( TLogQueueEntry qe = wait( self->persistentQueue->readNext() ) ) { + if(!self->queueOrder.size() || self->queueOrder.back() != qe.id) self->queueOrder.push_back(qe.id); + if(qe.id != lastId) { + logData = self->id_data[qe.id]; + lastId = qe.id; + } else { + ASSERT( qe.version >= lastVer ); + lastVer = qe.version; + } + + //TraceEvent("TLogRecoveredQE", self->dbgid).detail("logId", qe.id).detail("ver", qe.version).detail("MessageBytes", qe.messages.size()).detail("Tags", qe.tags.size()) + // .detail("Tag0", qe.tags.size() ? qe.tags[0].tag : invalidTag).detail("version", logData->version.get()); + + logData->knownCommittedVersion = std::max(logData->knownCommittedVersion, qe.knownCommittedVersion); + if( qe.version > logData->version.get() ) { + commitMessages(logData, qe.version, qe.arena(), qe.messages, qe.tags, self->bytesInput); + logData->version.set( qe.version ); + logData->queueCommittedVersion.set( qe.version ); + + while (self->bytesInput - self->bytesDurable >= recoverMemoryLimit) { + TEST(true); // Flush excess data during TLog queue recovery + TraceEvent("FlushLargeQueueDuringRecovery", self->dbgid).detail("BytesInput", self->bytesInput).detail("BytesDurable", self->bytesDurable).detail("Version", logData->version.get()).detail("PVer", logData->persistentDataVersion); + + choose { + when( Void _ = wait( updateStorage(self) ) ) {} + when( Void _ = wait( allRemoved ) ) { throw worker_removed(); } + } + } + } + } + when( Void _ = wait( allRemoved ) ) { throw worker_removed(); } + } + } + } catch (Error& e) { + if (e.code() != error_code_end_of_stream) throw; + } + + TraceEvent("TLogRestorePersistentStateDone", self->dbgid).detail("Took", now()-startt); + TEST( now()-startt >= 1.0 ); // TLog recovery took more than 1 second + + for(auto it : self->id_data) { + if(it.second->queueCommittedVersion.get() == 0) { + TraceEvent("TLogZeroVersion", self->dbgid).detail("logId", it.first); + it.second->queueCommittedVersion.set(it.second->version.get()); + } + self->sharedActors.send( tLogCore( self, it.second, Void() ) ); + } + + return Void(); +} + +bool tlogTerminated( TLogData* self, IKeyValueStore* persistentData, TLogQueue* persistentQueue, Error const& e ) { + // Dispose the IKVS (destroying its data permanently) only if this shutdown is definitely permanent. Otherwise just close it. + if (e.code() == error_code_worker_removed || e.code() == error_code_recruitment_failed) { + persistentData->dispose(); + persistentQueue->dispose(); + } else { + persistentData->close(); + persistentQueue->close(); + } + + if ( e.code() == error_code_worker_removed || + e.code() == error_code_recruitment_failed || + e.code() == error_code_file_not_found ) + { + TraceEvent("TLogTerminated", self->dbgid).error(e, true); + return true; + } else + return false; +} + +ACTOR Future recoverTagFromLogSystem( TLogData* self, Reference logData, Version beginVersion, Version endVersion, Tag tag, Reference> uncommittedBytes, Reference>> logSystem ) { + state Future dbInfoChange = Void(); + state Reference r; + state Version tagAt = beginVersion; + state Version tagPopped = 0; + state Version lastVer = 0; + + TraceEvent("LogRecoveringTagBegin", self->dbgid).detail("Tag", tag).detail("recoverAt", endVersion); + + while (tagAt <= endVersion) { + loop { + choose { + when(Void _ = wait( r ? r->getMore() : Never() ) ) { + break; + } + when( Void _ = wait( dbInfoChange ) ) { + if(r) tagPopped = std::max(tagPopped, r->popped()); + if( logSystem->get() ) + r = logSystem->get()->peek( tagAt, tag ); + else + r = Reference(); + dbInfoChange = logSystem->onChange(); + } + } + } + + TraceEvent("LogRecoveringTagResults", logData->logId).detail("Tag", tag); + + Version ver = 0; + BinaryWriter wr( Unversioned() ); + int writtenBytes = 0; + while (true) { + bool foundMessage = r->hasMessage(); + //TraceEvent("LogRecoveringMsg").detail("Tag", tag).detail("foundMessage", foundMessage).detail("ver", r->version().toString()); + if (!foundMessage || r->version().version != ver) { + ASSERT(r->version().version > lastVer); + if (ver) { + //TraceEvent("LogRecoveringTagVersion", self->dbgid).detail("Tag", tag).detail("Ver", ver).detail("Bytes", wr.getLength()); + writtenBytes += 100 + wr.getLength(); + self->persistentData->set( KeyValueRef( persistTagMessagesKey( logData->logId, tag, ver ), wr.toStringRef() ) ); + } + lastVer = ver; + ver = r->version().version; + wr = BinaryWriter( Unversioned() ); + if (!foundMessage || ver > endVersion) + break; + } + + // FIXME: This logic duplicates stuff in LogPushData::addMessage(), and really would be better in PeekResults or somewhere else. Also unnecessary copying. + StringRef msg = r->getMessage(); + wr << uint32_t( msg.size() + sizeof(uint32_t) ) << r->version().sub; + wr.serializeBytes( msg ); + r->nextMessage(); + } + + tagAt = r->version().version; + + if(writtenBytes) + uncommittedBytes->set(uncommittedBytes->get() + writtenBytes); + + while(uncommittedBytes->get() >= SERVER_KNOBS->LARGE_TLOG_COMMIT_BYTES) { + Void _ = wait(uncommittedBytes->onChange()); + } + } + if(r) tagPopped = std::max(tagPopped, r->popped()); + + auto tsm = logData->tag_data.find(tag); + if (tsm == logData->tag_data.end()) { + logData->tag_data.insert( mapPair(std::move(Tag(tag)), LogData::TagData(tagPopped, false, true, tag)) ); + } + + Void _ = wait(tLogPop( self, TLogPopRequest(tagPopped, tag), logData )); + + updatePersistentPopped( self, logData, tag, logData->tag_data.find(tag)->value ); + return Void(); +} + +ACTOR Future updateLogSystem(TLogData* self, Reference logData, LogSystemConfig recoverFrom, Reference>> logSystem) { + loop { + TraceEvent("TLogUpdate", self->dbgid).detail("logId", logData->logId).detail("recoverFrom", recoverFrom.toString()).detail("dbInfo", self->dbInfo->get().logSystemConfig.toString()); + if( self->dbInfo->get().logSystemConfig.isEqualIds(recoverFrom) ) { + logSystem->set(ILogSystem::fromLogSystemConfig( logData->logId, self->dbInfo->get().myLocality, self->dbInfo->get().logSystemConfig )); + } else if( self->dbInfo->get().logSystemConfig.isNextGenerationOf(recoverFrom) && std::count( self->dbInfo->get().logSystemConfig.tLogs.begin(), self->dbInfo->get().logSystemConfig.tLogs.end(), logData->logId ) ) { + logSystem->set(ILogSystem::fromOldLogSystemConfig( logData->logId, self->dbInfo->get().myLocality, self->dbInfo->get().logSystemConfig )); + } else { + logSystem->set(Reference()); + } + Void _ = wait( self->dbInfo->onChange() ); + } +} + +ACTOR Future recoverFromLogSystem( TLogData* self, Reference logData, LogSystemConfig recoverFrom, Version recoverAt, Version knownCommittedVersion, std::vector recoverTags, Promise copyComplete ) { + state Future committing = Void(); + state double lastCommitT = now(); + state Reference> uncommittedBytes = Reference>(new AsyncVar()); + state std::vector> recoverFutures; + state Reference>> logSystem = Reference>>(new AsyncVar>()); + state Future updater = updateLogSystem(self, logData, recoverFrom, logSystem); + + for(auto tag : recoverTags ) + recoverFutures.push_back(recoverTagFromLogSystem(self, logData, knownCommittedVersion, recoverAt, tag, uncommittedBytes, logSystem)); + + state Future copyDone = waitForAll(recoverFutures); + state Future recoveryDone = Never(); + state Future commitTimeout = delay(SERVER_KNOBS->LONG_TLOG_COMMIT_TIME); + + loop { + choose { + when(Void _ = wait(copyDone)) { + recoverFutures.clear(); + for(auto tag : recoverTags ) + recoverFutures.push_back(recoverTagFromLogSystem(self, logData, 0, knownCommittedVersion, tag, uncommittedBytes, logSystem)); + copyDone = Never(); + recoveryDone = waitForAll(recoverFutures); + + Void __ = wait( committing ); + Void __ = wait( self->updatePersist ); + committing = self->persistentData->commit(); + commitTimeout = delay(SERVER_KNOBS->LONG_TLOG_COMMIT_TIME); + uncommittedBytes->set(0); + Void __ = wait( committing ); + TraceEvent("TLogCommitCopyData", self->dbgid); + + if(!copyComplete.isSet()) + copyComplete.send(Void()); + } + when(Void _ = wait(recoveryDone)) { break; } + when(Void _ = wait(commitTimeout)) { + TEST(true); // We need to commit occasionally if this process is long to avoid running out of memory. + // We let one, but not more, commits pipeline with the network transfer + Void __ = wait( committing ); + Void __ = wait( self->updatePersist ); + committing = self->persistentData->commit(); + commitTimeout = delay(SERVER_KNOBS->LONG_TLOG_COMMIT_TIME); + uncommittedBytes->set(0); + TraceEvent("TLogCommitRecoveryData", self->dbgid).detail("MemoryUsage", DEBUG_DETERMINISM ? 0 : getMemoryUsage()); + } + when(Void _ = wait(uncommittedBytes->onChange())) { + if(uncommittedBytes->get() >= SERVER_KNOBS->LARGE_TLOG_COMMIT_BYTES) + commitTimeout = Void(); + } + } + } + + Void _ = wait( committing ); + Void _ = wait( self->updatePersist ); + Void _ = wait( self->persistentData->commit() ); + + TraceEvent("TLogRecoveryComplete", self->dbgid).detail("Locality", self->dbInfo->get().myLocality.toString()); + TEST(true); // tLog restore from old log system completed + + return Void(); +} + +ACTOR Future tLogStart( TLogData* self, InitializeTLogRequest req, LocalityData locality ) { + state Future recovery = Void(); + state TLogInterface recruited; + recruited.locality = locality; + recruited.initEndpoints(); + + DUMPTOKEN( recruited.peekMessages ); + DUMPTOKEN( recruited.popMessages ); + DUMPTOKEN( recruited.commit ); + DUMPTOKEN( recruited.lock ); + DUMPTOKEN( recruited.getQueuingMetrics ); + DUMPTOKEN( recruited.confirmRunning ); + + for(auto it : self->id_data) { + it.second->stopped = true; + } + + state Reference logData = Reference( new LogData(recruited) ); + self->id_data[recruited.id()] = logData; + logData->recoveryCount = req.epoch; + logData->removed = rejoinMasters(self, recruited, req.epoch); + self->queueOrder.push_back(recruited.id()); + + Void _ = wait( delay(0.0) ); // if multiple recruitment requests were already in the promise stream make sure they are all started before any are removed + + TraceEvent("TLogStart", logData->logId); + + try { + if( logData->removed.isReady() ) { + throw logData->removed.getError(); + } + + if (req.recoverFrom.logSystemType == 1) { + ASSERT(false); + } else if (req.recoverFrom.logSystemType == 2) { + logData->persistentDataVersion = req.recoverAt; + logData->persistentDataDurableVersion = req.recoverAt; // durable is a white lie until initPersistentState() commits the store + logData->queueCommittedVersion.set( req.recoverAt ); + logData->version.set( req.recoverAt ); + + Void _ = wait( initPersistentState( self, logData ) || logData->removed ); + + state Promise copyComplete; + TraceEvent("TLogRecover", self->dbgid).detail("logId", logData->logId).detail("at", req.recoverAt).detail("known", req.knownCommittedVersion).detail("tags", describe(req.recoverTags)); + recovery = recoverFromLogSystem( self, logData, req.recoverFrom, req.recoverAt, req.knownCommittedVersion, req.recoverTags, copyComplete ); + Void _ = wait(copyComplete.getFuture() || logData->removed ); + } else { + // Brand new tlog, initialization has already been done by caller + Void _ = wait( initPersistentState( self, logData ) || logData->removed ); + } + } catch( Error &e ) { + if(e.code() != error_code_actor_cancelled) { + req.reply.sendError(e); + } + + if( e.code() != error_code_worker_removed ) { + throw; + } + + double inputSum = 0; + double durableSum = 0; + for(auto it : self->id_data) { + inputSum += it.second->bytesInput.getValue(); + durableSum += it.second->bytesDurable.getValue(); + TraceEvent("QueueMetrics3", self->dbgid).detail("logId", it.first).detail("input", it.second->bytesInput.getValue()).detail("durable", it.second->bytesDurable.getValue()); + } + + TraceEvent("RemoveMetrics3", self->dbgid).detail("logId", logData->logId).detail("inputSum", inputSum).detail("durableSum", durableSum).detail("nonDurableSum", inputSum - durableSum) + .detail("input", self->bytesInput).detail("durable", self->bytesDurable).detail("nonDurable", self->bytesInput - self->bytesDurable).detail("durableIncrease", logData->bytesInput.getValue() - logData->bytesDurable.getValue()); + + //FIXME: check that bytes durable cannot be increased by a dead log, check that bytes durable never becomes larger that bytes input + //FIXME: factor error handling code + self->bytesDurable += logData->bytesInput.getValue() - logData->bytesDurable.getValue(); + logData->stopped = true; + + logData->addActor = PromiseStream>(); //there could be items still in the promise stream if one of the actors threw an error immediately + self->id_data.erase(logData->logId); + TraceEvent("TLogRemoved", logData->logId); + + if(self->id_data.size() || (self->oldLogServer.isValid() && !self->oldLogServer.isReady())) { + return Void(); + } else { + throw; + } + } + + TraceEvent("TLogReady", logData->logId); + + req.reply.send( recruited ); + + Void _ = wait( tLogCore( self, logData, recovery ) ); + return Void(); +} + +// New tLog (if !recoverFrom.size()) or restore from network +ACTOR Future tLog( IKeyValueStore* persistentData, IDiskQueue* persistentQueue, Reference> db, LocalityData locality, PromiseStream tlogRequests, UID tlogId, bool restoreFromDisk, Promise oldLog ) +{ + state TLogData self( tlogId, persistentData, persistentQueue, db ); + state Future error = actorCollection( self.sharedActors.getFuture() ); + + TraceEvent("SharedTlog", tlogId); + + try { + if(restoreFromDisk) { + Void _ = wait( restorePersistentState( &self, locality, oldLog, tlogRequests ) ); + } else { + Void _ = wait( checkEmptyQueue(&self) ); + } + + self.sharedActors.send( cleanupPeekTrackers(&self) ); + self.sharedActors.send( commitQueue(&self) ); + self.sharedActors.send( updateStorageLoop(&self) ); + + loop { + choose { + when ( InitializeTLogRequest req = waitNext(tlogRequests.getFuture() ) ) { + if( !self.tlogCache.exists( req.recruitmentID ) ) { + self.tlogCache.set( req.recruitmentID, req.reply.getFuture() ); + self.sharedActors.send( self.tlogCache.removeOnReady( req.recruitmentID, tLogStart( &self, req, locality ) ) ); + } else { + forwardPromise( req.reply, self.tlogCache.get( req.recruitmentID ) ); + } + } + when ( Void _ = wait( error ) ) { throw internal_error(); } + } + } + } catch (Error& e) { + TraceEvent("TLogError", tlogId).error(e); + if(e.code() != error_code_actor_cancelled) { + while(!tlogRequests.isEmpty()) { + tlogRequests.getFuture().pop().reply.sendError(e); + } + } + + if (tlogTerminated( &self, persistentData, self.persistentQueue, e )) { + return Void(); + } else { + throw; + } + } +} + +// UNIT TESTS +struct DequeAllocatorStats { + static int64_t allocatedBytes; +}; + +int64_t DequeAllocatorStats::allocatedBytes = 0; + +template +struct DequeAllocator : std::allocator { + template + struct rebind { + typedef DequeAllocator other; + }; + + DequeAllocator() {} + + template + DequeAllocator(DequeAllocator const& u) : std::allocator(u) {} + + T* allocate(std::size_t n, std::allocator::const_pointer hint = 0) { + DequeAllocatorStats::allocatedBytes += n * sizeof(T); + //fprintf(stderr, "Allocating %lld objects for %lld bytes (total allocated: %lld)\n", n, n * sizeof(T), DequeAllocatorStats::allocatedBytes); + return std::allocator::allocate(n, hint); + } + void deallocate(T* p, std::size_t n) { + DequeAllocatorStats::allocatedBytes -= n * sizeof(T); + //fprintf(stderr, "Deallocating %lld objects for %lld bytes (total allocated: %lld)\n", n, n * sizeof(T), DequeAllocatorStats::allocatedBytes); + return std::allocator::deallocate(p, n); + } +}; + +TEST_CASE( "fdbserver/tlogserver/VersionMessagesOverheadFactor" ) { + + typedef std::pair TestType; // type used by versionMessages + + for(int i = 1; i < 9; ++i) { + for(int j = 0; j < 20; ++j) { + DequeAllocatorStats::allocatedBytes = 0; + DequeAllocator allocator; + std::deque> d(allocator); + + int numElements = g_random->randomInt(pow(10, i-1), pow(10, i)); + for(int k = 0; k < numElements; ++k) { + d.push_back(TestType()); + } + + int removedElements = 0;//g_random->randomInt(0, numElements); // FIXME: the overhead factor does not accurately account for removal! + for(int k = 0; k < removedElements; ++k) { + d.pop_front(); + } + + int64_t dequeBytes = DequeAllocatorStats::allocatedBytes + sizeof(std::deque); + int64_t insertedBytes = (numElements-removedElements) * sizeof(TestType); + double overheadFactor = std::max(insertedBytes, dequeBytes-10000) / insertedBytes; // We subtract 10K here as an estimated upper bound for the fixed cost of an std::deque + //fprintf(stderr, "%d elements (%d inserted, %d removed):\n", numElements-removedElements, numElements, removedElements); + //fprintf(stderr, "Allocated %lld bytes to store %lld bytes (%lf overhead factor)\n", dequeBytes, insertedBytes, overheadFactor); + ASSERT(overheadFactor * 1024 <= SERVER_KNOBS->VERSION_MESSAGES_OVERHEAD_FACTOR_1024THS); + } + } + + return Void(); +} diff --git a/fdbserver/TagPartitionedLogSystem.actor.cpp b/fdbserver/TagPartitionedLogSystem.actor.cpp new file mode 100644 index 0000000000..bd43d10fb3 --- /dev/null +++ b/fdbserver/TagPartitionedLogSystem.actor.cpp @@ -0,0 +1,916 @@ +/* + * TagPartitionedLogSystem.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/ActorCollection.h" +#include "LogSystem.h" +#include "ServerDBInfo.h" +#include "DBCoreState.h" +#include "WaitFailure.h" +#include "fdbclient/SystemData.h" +#include "fdbrpc/simulator.h" +#include "fdbrpc/Replication.h" +#include "fdbrpc/ReplicationUtils.h" + +template +void uniquify( Collection& c ) { + std::sort(c.begin(), c.end()); + c.resize( std::unique(c.begin(), c.end()) - c.begin() ); +} + +ACTOR static Future reportTLogCommitErrors( Future commitReply, UID debugID ) { + try { + Void _ = wait(commitReply); + return Void(); + } catch (Error& e) { + if (e.code() == error_code_broken_promise) + throw master_tlog_failed(); + else if (e.code() != error_code_actor_cancelled && e.code() != error_code_tlog_stopped) + TraceEvent(SevError, "MasterTLogCommitRequestError", debugID).error(e); + throw; + } +} + +struct OldLogData { + std::vector>>> logServers; + int32_t tLogWriteAntiQuorum; + int32_t tLogReplicationFactor; + std::vector< LocalityData > tLogLocalities; // Stores the localities of the log servers + IRepPolicyRef tLogPolicy; + + Version epochEnd; + + OldLogData() : tLogWriteAntiQuorum(0), tLogReplicationFactor(0), epochEnd(0) {} +}; + +struct TagPartitionedLogSystem : ILogSystem, ReferenceCounted { + UID dbgid; + int tLogWriteAntiQuorum, tLogReplicationFactor, logSystemType; + LocalitySetRef logServerSet; + std::vector logIndexArray; + std::map logEntryMap; + IRepPolicyRef tLogPolicy; + std::vector< LocalityData > tLogLocalities; + + // new members + Future rejoins; + Future recoveryComplete; + bool recoveryCompleteWrittenToCoreState; + + Optional epochEndVersion; + std::set< Tag > epochEndTags; + Version knownCommittedVersion; + LocalityData locality; + std::map< std::pair, Version > outstandingPops; // For each currently running popFromLog actor, (log server #, tag)->popped version + ActorCollection actors; + std::vector oldLogData; + std::vector>>> logServers; + + TagPartitionedLogSystem( UID dbgid, LocalityData locality ) : dbgid(dbgid), locality(locality), actors(false), recoveryCompleteWrittenToCoreState(false), tLogWriteAntiQuorum(0), tLogReplicationFactor(0), logSystemType(0) {} + + virtual void stopRejoins() { + rejoins = Future(); + } + + virtual void addref() { + ReferenceCounted::addref(); + } + + virtual void delref() { + ReferenceCounted::delref(); + } + + virtual std::string describe() { + std::string result; + for( auto it : logServers ) { + result = result + it->get().id().toString() + ", "; + } + return result; + } + + virtual UID getDebugID() { + return dbgid; + } + + static Future recoverAndEndEpoch(Reference>> const& outLogSystem, UID const& dbgid, DBCoreState const& oldState, FutureStream const& rejoins, LocalityData const& locality) { + return epochEnd( outLogSystem, dbgid, oldState, rejoins, locality ); + } + + static Reference fromLogSystemConfig( UID const& dbgid, LocalityData const& locality, LogSystemConfig const& lsConf ) { + ASSERT( lsConf.logSystemType == 2 || (lsConf.logSystemType == 0 && !lsConf.tLogs.size()) ); + //ASSERT(lsConf.epoch == epoch); //< FIXME + Reference logSystem( new TagPartitionedLogSystem(dbgid, locality) ); + + for( int i = 0; i < lsConf.tLogs.size(); i++ ) + logSystem->logServers.push_back( Reference>>( new AsyncVar>( lsConf.tLogs[i] ) ) ); + + logSystem->oldLogData.resize(lsConf.oldTLogs.size()); + for( int i = 0; i < lsConf.oldTLogs.size(); i++ ) { + for( int j = 0; j < lsConf.oldTLogs[i].tLogs.size(); j++) { + logSystem->oldLogData[i].logServers.push_back( Reference>>( new AsyncVar>( lsConf.oldTLogs[i].tLogs[j] ) ) ); + } + logSystem->oldLogData[i].tLogWriteAntiQuorum = lsConf.oldTLogs[i].tLogWriteAntiQuorum; + logSystem->oldLogData[i].tLogReplicationFactor = lsConf.oldTLogs[i].tLogReplicationFactor; + logSystem->oldLogData[i].tLogPolicy = lsConf.oldTLogs[i].tLogPolicy; + logSystem->oldLogData[i].tLogLocalities = lsConf.oldTLogs[i].tLogLocalities; + logSystem->oldLogData[i].epochEnd = lsConf.oldTLogs[i].epochEnd; + } + + //logSystem->epoch = lsConf.epoch; + logSystem->tLogWriteAntiQuorum = lsConf.tLogWriteAntiQuorum; + logSystem->tLogReplicationFactor = lsConf.tLogReplicationFactor; + logSystem->tLogPolicy = lsConf.tLogPolicy; + logSystem->tLogLocalities = lsConf.tLogLocalities; + logSystem->logSystemType = lsConf.logSystemType; + logSystem->UpdateLocalitySet(lsConf.tLogs); + + return logSystem; + } + + static Reference fromOldLogSystemConfig( UID const& dbgid, LocalityData const& locality, LogSystemConfig const& lsConf ) { + ASSERT( lsConf.logSystemType == 2 || (lsConf.logSystemType == 0 && !lsConf.tLogs.size()) ); + //ASSERT(lsConf.epoch == epoch); //< FIXME + Reference logSystem( new TagPartitionedLogSystem(dbgid, locality) ); + + if(lsConf.oldTLogs.size()) { + for( int i = 0; i < lsConf.oldTLogs[0].tLogs.size(); i++ ) + logSystem->logServers.push_back( Reference>>( new AsyncVar>( lsConf.oldTLogs[0].tLogs[i] ) ) ); + + //logSystem->epoch = lsConf.epoch; + logSystem->tLogWriteAntiQuorum = lsConf.oldTLogs[0].tLogWriteAntiQuorum; + logSystem->tLogReplicationFactor = lsConf.oldTLogs[0].tLogReplicationFactor; + logSystem->tLogPolicy = lsConf.oldTLogs[0].tLogPolicy; + logSystem->tLogLocalities = lsConf.oldTLogs[0].tLogLocalities; + + logSystem->oldLogData.resize(lsConf.oldTLogs.size()-1); + for( int i = 1; i < lsConf.oldTLogs.size(); i++ ) { + for( int j = 0; j < lsConf.oldTLogs[i].tLogs.size(); j++) { + logSystem->oldLogData[i-1].logServers.push_back( Reference>>( new AsyncVar>( lsConf.oldTLogs[i].tLogs[j] ) ) ); + } + logSystem->oldLogData[i-1].tLogWriteAntiQuorum = lsConf.oldTLogs[i].tLogWriteAntiQuorum; + logSystem->oldLogData[i-1].tLogReplicationFactor = lsConf.oldTLogs[i].tLogReplicationFactor; + logSystem->oldLogData[i-1].tLogPolicy = lsConf.oldTLogs[i].tLogPolicy; + logSystem->oldLogData[i-1].tLogLocalities = lsConf.oldTLogs[i].tLogLocalities; + logSystem->oldLogData[i-1].epochEnd = lsConf.oldTLogs[i].epochEnd; + } + } + logSystem->logSystemType = lsConf.logSystemType; + + return logSystem; + } + + virtual void toCoreState( DBCoreState& newState ) { + if( recoveryComplete.isValid() && recoveryComplete.isError() ) + throw recoveryComplete.getError(); + + newState.tLogs.clear(); + tLogLocalities.clear(); + for(auto &t : logServers) { + newState.tLogs.push_back(t->get().id()); + tLogLocalities.push_back(t->get().interf().locality); + } + + newState.oldTLogData.clear(); + if(!recoveryComplete.isValid() || !recoveryComplete.isReady()) { + newState.oldTLogData.resize(oldLogData.size()); + for(int i = 0; i < oldLogData.size(); i++) { + for(auto &t : oldLogData[i].logServers) + newState.oldTLogData[i].tLogs.push_back(t->get().id()); + newState.oldTLogData[i].tLogWriteAntiQuorum = oldLogData[i].tLogWriteAntiQuorum; + newState.oldTLogData[i].tLogReplicationFactor = oldLogData[i].tLogReplicationFactor; + newState.oldTLogData[i].tLogPolicy = oldLogData[i].tLogPolicy; + newState.oldTLogData[i].tLogLocalities = oldLogData[i].tLogLocalities; + newState.oldTLogData[i].epochEnd = oldLogData[i].epochEnd; + } + } + + newState.tLogWriteAntiQuorum = tLogWriteAntiQuorum; + newState.tLogReplicationFactor = tLogReplicationFactor; + newState.tLogPolicy = tLogPolicy; + newState.tLogLocalities = tLogLocalities; + newState.logSystemType = logSystemType; + } + + virtual Future onCoreStateChanged() { + ASSERT(recoveryComplete.isValid()); + if( recoveryComplete.isReady() ) + return Never(); + return recoveryComplete; + } + + virtual void coreStateWritten( DBCoreState const& newState ) { + if( !newState.oldTLogData.size() ) + recoveryCompleteWrittenToCoreState = true; + } + + virtual Future onError() { + // Never returns normally, but throws an error if the subsystem stops working + // FIXME: Run waitFailureClient on the master instead of these onFailedFor? + if (!logServers.size()) return Never(); + vector> failed; + + for(auto &t : logServers) + if( t->get().present() ) + failed.push_back( waitFailureClient( t->get().interf().waitFailure, SERVER_KNOBS->TLOG_TIMEOUT, -SERVER_KNOBS->TLOG_TIMEOUT/SERVER_KNOBS->SECONDS_BEFORE_NO_FAILURE_DELAY ) ); + + ASSERT( failed.size() >= 1 ); + return tagError( quorum( failed, 1 ), master_tlog_failed() ) || actors.getResult(); + } + + virtual Future push( Version prevVersion, Version version, Version knownCommittedVersion, LogPushData& data, Optional debugID ) { + // FIXME: Randomize request order as in LegacyLogSystem? + vector> tLogCommitResults; + for(int loc=0; loc commitMessage = reportTLogCommitErrors( + logServers[loc]->get().interf().commit.getReply( + TLogCommitRequest( data.getArena(), prevVersion, version, knownCommittedVersion, data.getMessages(loc), data.getTags(loc), debugID ), TaskTLogCommitReply ), + getDebugID()); + actors.add(commitMessage); + tLogCommitResults.push_back(commitMessage); + } + return quorum( tLogCommitResults, tLogCommitResults.size() - tLogWriteAntiQuorum ); + } + + virtual Reference peek( Version begin, Tag tag, bool parallelGetMore ) { + if(oldLogData.size() == 0 || begin >= oldLogData[0].epochEnd) { + return Reference( new ILogSystem::MergedPeekCursor( logServers, logServers.size() ? bestLocationFor( tag ) : -1, + (int)logServers.size() + 1 - tLogReplicationFactor, tag, begin, getPeekEnd(), parallelGetMore, tLogLocalities, tLogPolicy, tLogReplicationFactor)); + } else { + std::vector< Reference > cursors; + std::vector< LogMessageVersion > epochEnds; + cursors.push_back( Reference( new ILogSystem::MergedPeekCursor( logServers, logServers.size() ? bestLocationFor( tag ) : -1, + (int)logServers.size() + 1 - tLogReplicationFactor, tag, oldLogData[0].epochEnd, getPeekEnd(), parallelGetMore, tLogLocalities, tLogPolicy, tLogReplicationFactor)) ); + for(int i = 0; i < oldLogData.size() && begin < oldLogData[i].epochEnd; i++) { + cursors.push_back( Reference( new ILogSystem::MergedPeekCursor( oldLogData[i].logServers, oldLogData[i].logServers.size() ? oldBestLocationFor( tag, i ) : -1, + (int)oldLogData[i].logServers.size() + 1 - oldLogData[i].tLogReplicationFactor, tag, i+1 == oldLogData.size() ? begin : std::max(oldLogData[i+1].epochEnd, begin), oldLogData[i].epochEnd, parallelGetMore, oldLogData[i].tLogLocalities, oldLogData[i].tLogPolicy, oldLogData[i].tLogReplicationFactor)) ); + epochEnds.push_back(LogMessageVersion(oldLogData[i].epochEnd)); + } + + return Reference( new ILogSystem::MultiCursor(cursors, epochEnds) ); + } + } + + virtual Reference peekSingle( Version begin, Tag tag ) { + if(oldLogData.size() == 0 || begin >= oldLogData[0].epochEnd) { + return Reference( new ILogSystem::ServerPeekCursor( logServers.size() ? + logServers[bestLocationFor( tag )] : + Reference>>(), tag, begin, getPeekEnd(), false, false ) ); + } else { + TEST(true); //peekSingle used during non-copying tlog recovery + std::vector< Reference > cursors; + std::vector< LogMessageVersion > epochEnds; + cursors.push_back( Reference( new ILogSystem::ServerPeekCursor( logServers.size() ? + logServers[bestLocationFor( tag )] : + Reference>>(), tag, oldLogData[0].epochEnd, getPeekEnd(), false, false) ) ); + for(int i = 0; i < oldLogData.size() && begin < oldLogData[i].epochEnd; i++) { + cursors.push_back( Reference( new ILogSystem::MergedPeekCursor( oldLogData[i].logServers, oldLogData[i].logServers.size() ? oldBestLocationFor( tag, i ) : -1, + (int)oldLogData[i].logServers.size() + 1 - oldLogData[i].tLogReplicationFactor, tag, i+1 == oldLogData.size() ? begin : std::max(oldLogData[i+1].epochEnd, begin), oldLogData[i].epochEnd, false, + oldLogData[i].tLogLocalities, oldLogData[i].tLogPolicy, oldLogData[i].tLogReplicationFactor)) ); + epochEnds.push_back(LogMessageVersion(oldLogData[i].epochEnd)); + } + + return Reference( new ILogSystem::MultiCursor(cursors, epochEnds) ); + } + } + + virtual void pop( Version upTo, Tag _tag ) { + if (!logServers.size() || !upTo) return; + Tag tag = _tag; + for(auto log=0; log popFromLog( TagPartitionedLogSystem* self, int log, Tag tag ) { + state Version last = 0; + loop { + Void _ = wait( delay(1.0) ); //< FIXME: knob + + state Version to = self->outstandingPops[ std::make_pair(log,tag) ]; + + if (to <= last) { + self->outstandingPops.erase( std::make_pair(log,tag) ); + return Void(); + } + + try { + auto& interf = self->logServers[log]; + if( !interf->get().present() ) + return Void(); + Void _ = wait(interf->get().interf().popMessages.getReply( TLogPopRequest( to, tag ) ) ); + + last = to; + } catch (Error& e) { + if (e.code() == error_code_actor_cancelled) throw; + TraceEvent( (e.code() == error_code_broken_promise) ? SevInfo : SevError, "LogPopError", self->dbgid ).detail("Log", self->logServers[log]->get().id()).error(e); + return Void(); // Leaving outstandingPops filled in means no further pop requests to this tlog from this logSystem + } + } + } + + virtual Future confirmEpochLive(Optional debugID) { + // Returns success after confirming that pushes in the current epoch are still possible + // FIXME: This is way too conservative? + vector> alive; + for(auto& t : logServers) { + if( t->get().present() ) alive.push_back( brokenPromiseToNever( t->get().interf().confirmRunning.getReply(TLogConfirmRunningRequest(debugID), TaskTLogConfirmRunningReply ) ) ); + else alive.push_back( Never() ); + } + return quorum( alive, alive.size() - tLogWriteAntiQuorum ); + } + + virtual Future> newEpoch( vector availableLogServers, DatabaseConfiguration const& config, LogEpoch recoveryCount ) { + // Call only after end_epoch() has successfully completed. Returns a new epoch immediately following this one. The new epoch + // is only provisional until the caller updates the coordinated DBCoreState + return newEpoch( Reference::addRef(this), availableLogServers, config, recoveryCount ); + } + + virtual LogSystemConfig getLogSystemConfig() { + LogSystemConfig logSystemConfig; + logSystemConfig.logSystemType = logSystemType; + logSystemConfig.tLogWriteAntiQuorum = tLogWriteAntiQuorum; + logSystemConfig.tLogReplicationFactor = tLogReplicationFactor; + logSystemConfig.tLogPolicy = tLogPolicy; + logSystemConfig.tLogLocalities = tLogLocalities; + + for( int i = 0; i < logServers.size(); i++ ) + logSystemConfig.tLogs.push_back(logServers[i]->get()); + + if(!recoveryCompleteWrittenToCoreState) { + for( int i = 0; i < oldLogData.size(); i++ ) { + logSystemConfig.oldTLogs.push_back(OldTLogConf()); + for( int j = 0; j < oldLogData[i].logServers.size(); j++ ) { + logSystemConfig.oldTLogs[i].tLogs.push_back(oldLogData[i].logServers[j]->get()); + } + logSystemConfig.oldTLogs[i].tLogWriteAntiQuorum = oldLogData[i].tLogWriteAntiQuorum; + logSystemConfig.oldTLogs[i].tLogReplicationFactor = oldLogData[i].tLogReplicationFactor; + logSystemConfig.oldTLogs[i].tLogPolicy = oldLogData[i].tLogPolicy; + logSystemConfig.oldTLogs[i].tLogLocalities = oldLogData[i].tLogLocalities; + logSystemConfig.oldTLogs[i].epochEnd = oldLogData[i].epochEnd; + } + } + return logSystemConfig; + } + + virtual Standalone getLogsValue() { + vector> logs; + for( int i = 0; i < logServers.size(); i++ ) { + logs.push_back(std::make_pair(logServers[i]->get().id(), logServers[i]->get().present() ? logServers[i]->get().interf().address() : NetworkAddress())); + } + + vector> oldLogs; + if(!recoveryCompleteWrittenToCoreState) { + for( int i = 0; i < oldLogData.size(); i++ ) { + for( int j = 0; j < oldLogData[i].logServers.size(); j++ ) { + oldLogs.push_back(std::make_pair(oldLogData[i].logServers[j]->get().id(), oldLogData[i].logServers[j]->get().present() ? oldLogData[i].logServers[j]->get().interf().address() : NetworkAddress())); + } + } + } + + return logsValue( logs, oldLogs ); + } + + virtual Future onLogSystemConfigChange() { + std::vector> changes; + changes.push_back(Never()); + for( int i = 0; i < logServers.size(); i++ ) + changes.push_back( logServers[i]->onChange() ); + for( int i = 0; i < oldLogData.size(); i++ ) { + for( int j = 0; j < oldLogData[i].logServers.size(); j++ ) { + changes.push_back( oldLogData[i].logServers[j]->onChange() ); + } + } + + return waitForAny(changes); + } + + virtual int getLogServerCount() { return logServers.size(); } + + virtual Version getEnd() { + ASSERT( epochEndVersion.present() ); + return epochEndVersion.get() + 1; + } + + Version getPeekEnd() { + if (epochEndVersion.present()) + return getEnd(); + else + return std::numeric_limits::max(); + } + + int bestLocationFor( Tag tag ) { + return tag % logServers.size(); + } + + int oldBestLocationFor( Tag tag, int idx ) { + return tag % oldLogData[idx].logServers.size(); + } + + virtual void getPushLocations( std::vector const& tags, std::vector& locations ) { + // Ensure that the replication server set and replication policy + // have been defined + ASSERT(logServerSet.getPtr()); + ASSERT(tLogPolicy.getPtr()); + + std::vector alsoServers, resultEntries; + + for(auto& t : tags) { + locations.push_back(bestLocationFor(t)); + } + + uniquify( locations ); + + if (locations.size()) + alsoServers.reserve(locations.size()); + + // Convert locations to the also servers + for (auto location : locations) { + ASSERT(logEntryMap[location]._id == location); + alsoServers.push_back(logEntryMap[location]); + } + + // Run the policy, assert if unable to satify + bool result = logServerSet->selectReplicas(tLogPolicy, alsoServers, resultEntries); + ASSERT(result); + + // Add the new servers to the location array + LocalityMap* logServerMap = (LocalityMap*) logServerSet.getPtr(); + for (auto entry : resultEntries) { + locations.push_back(*logServerMap->getObject(entry)); + } + +// TraceEvent("getPushLocations").detail("Policy", tLogPolicy->info()) +// .detail("Results", locations.size()).detail("Selection", logServerSet->size()) +// .detail("Included", alsoServers.size()).detail("Duration", timer() - t); + } + + void UpdateLocalitySet(vector> const& tlogs) + { + LocalityMap* logServerMap; + logServerSet = LocalitySetRef(new LocalityMap()); + logServerMap = (LocalityMap*) logServerSet.getPtr(); + + logEntryMap.clear(); + logIndexArray.clear(); + logIndexArray.reserve(tlogs.size()); + + for( int i = 0; i < tlogs.size(); i++ ) { + if (tlogs[i].present()) { + logIndexArray.push_back(i); + ASSERT(logEntryMap.find(i) == logEntryMap.end()); + logEntryMap[logIndexArray.back()] = logServerMap->add(tlogs[i].interf().locality, &logIndexArray.back()); + } + } + } + + void UpdateLocalitySet( + vector const& workers, + vector const& reqs) + { + LocalityMap* logServerMap; + + logServerSet = LocalitySetRef(new LocalityMap()); + logServerMap = (LocalityMap*) logServerSet.getPtr(); + + logEntryMap.clear(); + logIndexArray.clear(); + logIndexArray.reserve(workers.size()); + + for( int i = 0; i < workers.size(); i++ ) { + ASSERT(logEntryMap.find(i) == logEntryMap.end()); + logIndexArray.push_back(i); + logEntryMap[logIndexArray.back()] = logServerMap->add(workers[i].locality, &logIndexArray.back()); + } + } + + std::set< Tag > const& getEpochEndTags() const { return epochEndTags; } + + ACTOR static Future monitorLog(Reference>> logServer, Reference> failed) { + state Future waitFailure; + loop { + if(logServer->get().present()) + waitFailure = waitFailureTracker( logServer->get().interf().waitFailure, failed ); + else + failed->set(true); + Void _ = wait( logServer->onChange() ); + } + } + + ACTOR static Future epochEnd( Reference>> outLogSystem, UID dbgid, DBCoreState prevState, FutureStream rejoinRequests, LocalityData locality ) { + // Stops a co-quorum of tlogs so that no further versions can be committed until the DBCoreState coordination state is changed + // Creates a new logSystem representing the (now frozen) epoch + // No other important side effects. + // The writeQuorum in the master info is from the previous configuration + state vector> tLogReply; + + if (!prevState.tLogs.size()) { + // This is a brand new database + Reference logSystem( new TagPartitionedLogSystem(dbgid, locality) ); + logSystem->tLogWriteAntiQuorum = prevState.tLogWriteAntiQuorum; + logSystem->tLogReplicationFactor = prevState.tLogReplicationFactor; + logSystem->tLogPolicy = prevState.tLogPolicy; + logSystem->tLogLocalities = prevState.tLogLocalities; + logSystem->logSystemType = prevState.logSystemType; + + logSystem->epochEndVersion = 0; + logSystem->knownCommittedVersion = 0; + outLogSystem->set(logSystem); + Void _ = wait( Future(Never()) ); + throw internal_error(); + } + + TEST( true ); // Master recovery from pre-existing database + + // To ensure consistent recovery, the number of servers NOT in the write quorum plus the number of servers NOT in the read quorum + // have to be strictly less than the replication factor. Otherwise there could be a replica set consistent entirely of servers that + // are out of date due to not being in the write quorum or unavailable due to not being in the read quorum. + // So (N - W) + (N - R) < F, and optimally (N-W)+(N-R)=F-1. Thus R=2N+1-F-W. + state int requiredCount = (int)prevState.tLogs.size()+1 - prevState.tLogReplicationFactor + prevState.tLogWriteAntiQuorum; + ASSERT( requiredCount > 0 && requiredCount <= prevState.tLogs.size() ); + ASSERT( prevState.tLogReplicationFactor >= 1 && prevState.tLogReplicationFactor <= prevState.tLogs.size() ); + ASSERT( prevState.tLogWriteAntiQuorum >= 0 && prevState.tLogWriteAntiQuorum < prevState.tLogs.size() ); + + // trackRejoins listens for rejoin requests from the tLogs that we are recovering from, to learn their TLogInterfaces + state std::vector>>> logServers; + state std::vector>>> allLogServers; + state std::vector oldLogData; + state std::vector>> logFailed; + state std::vector> failureTrackers; + for( int i = 0; i < prevState.tLogs.size(); i++ ) { + Reference>> logVar = Reference>>( new AsyncVar>( OptionalInterface(prevState.tLogs[i]) ) ); + logServers.push_back( logVar ); + allLogServers.push_back( logVar ); + logFailed.push_back( Reference>( new AsyncVar() ) ); + failureTrackers.push_back( monitorLog(logServers[i], logFailed[i] ) ); + } + for( int i = 0; i < prevState.oldTLogData.size(); i++ ) { + oldLogData.push_back(OldLogData()); + for(int j = 0; j < prevState.oldTLogData[i].tLogs.size(); j++) { + Reference>> logVar = Reference>>( new AsyncVar>( OptionalInterface(prevState.oldTLogData[i].tLogs[j]) ) ); + oldLogData[i].logServers.push_back( logVar ); + allLogServers.push_back( logVar ); + } + oldLogData[i].tLogReplicationFactor = prevState.oldTLogData[i].tLogReplicationFactor; + oldLogData[i].tLogWriteAntiQuorum = prevState.oldTLogData[i].tLogWriteAntiQuorum; + oldLogData[i].epochEnd = prevState.oldTLogData[i].epochEnd; + oldLogData[i].tLogPolicy = prevState.oldTLogData[i].tLogPolicy; + oldLogData[i].tLogLocalities = prevState.oldTLogData[i].tLogLocalities; + } + state Future rejoins = trackRejoins( dbgid, allLogServers, rejoinRequests ); + + for(int t=0; t last_end; + + state bool lastWaitForRecovery = true; + state int cycles = 0; + + loop { + std::vector availableItems, badCombo; + std::vector results; + std::string sServerState; + LocalityGroup unResponsiveSet; + double t = timer(); + cycles ++; + + for(int t=0; tget()) { + results.push_back(tLogReply[t].get()); + availableItems.push_back(prevState.tLogLocalities[t]); + sServerState += 'a'; + } + else { + unResponsiveSet.add(prevState.tLogLocalities[t]); + sServerState += 'f'; + } + } + + // Check if the list of results is not larger than the anti quorum + bool bTooManyFailures = (results.size() <= prevState.tLogWriteAntiQuorum); + + // Check if failed logs complete the policy + bTooManyFailures = bTooManyFailures || + ((unResponsiveSet.size() >= prevState.tLogReplicationFactor) && + (unResponsiveSet.validate(prevState.tLogPolicy)) ); + + // Check all combinations of the AntiQuorum within the failed + if ((!bTooManyFailures) && + (prevState.tLogWriteAntiQuorum) && + (!validateAllCombinations(badCombo, unResponsiveSet, prevState.tLogPolicy, availableItems, prevState.tLogWriteAntiQuorum, false))) + { + TraceEvent("EpochEndBadCombo", dbgid).detail("Cycles", cycles) + .detail("Required", requiredCount) + .detail("Present", results.size()) + .detail("Available", availableItems.size()) + .detail("Absent", logServers.size() - results.size()) + .detail("ServerState", sServerState) + .detail("ReplicationFactor", prevState.tLogReplicationFactor) + .detail("AntiQuorum", prevState.tLogWriteAntiQuorum) + .detail("Policy", prevState.tLogPolicy->info()) + .detail("TooManyFailures", bTooManyFailures) + .detail("LogZones", ::describeZones(prevState.tLogLocalities)) + .detail("LogDataHalls", ::describeDataHalls(prevState.tLogLocalities)); + bTooManyFailures = true; + } + + // If too many TLogs are failed for recovery to be possible, we could wait forever here. + //Void _ = wait( smartQuorum( tLogReply, requiredCount, SERVER_KNOBS->RECOVERY_TLOG_SMART_QUORUM_DELAY ) || rejoins ); + + ASSERT(logServers.size() == tLogReply.size()); + if (!bTooManyFailures) { + std::sort( results.begin(), results.end(), sort_by_end() ); + int absent = logServers.size() - results.size(); + int safe_range_begin = prevState.tLogWriteAntiQuorum; + int new_safe_range_begin = std::min(prevState.tLogWriteAntiQuorum, (int)(results.size()-1)); + int safe_range_end = prevState.tLogReplicationFactor - absent; + + Version end = results[ new_safe_range_begin ].end; + Version knownCommittedVersion = end - (g_network->isSimulated() ? 10*SERVER_KNOBS->VERSIONS_PER_SECOND : SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS); //In simulation this must be the maximum MAX_READ_TRANSACTION_LIFE_VERSIONS + for(int i = 0; i < results.size(); i++) { + knownCommittedVersion = std::max(knownCommittedVersion, results[i].knownCommittedVersion); + } + + if( ( prevState.logSystemType == 2 && (!last_end.present() || ((safe_range_end > 0) && (safe_range_end-1 < results.size()) && results[ safe_range_end-1 ].end < last_end.get())) ) ) { + TEST( last_end.present() ); // Restarting recovery at an earlier point + + Reference logSystem( new TagPartitionedLogSystem(dbgid, locality) ); + + TraceEvent("LogSystemRecovery", dbgid).detail("Cycles", cycles) + .detail("TotalServers", logServers.size()) + .detail("Required", requiredCount) + .detail("Present", results.size()) + .detail("Available", availableItems.size()) + .detail("Absent", logServers.size() - results.size()) + .detail("ServerState", sServerState) + .detail("ReplicationFactor", prevState.tLogReplicationFactor) + .detail("AntiQuorum", prevState.tLogWriteAntiQuorum) + .detail("Policy", prevState.tLogPolicy->info()) + .detail("TooManyFailures", bTooManyFailures) + .detail("LastVersion", (last_end.present()) ? last_end.get() : -1L) + .detail("RecoveryVersion", ((safe_range_end > 0) && (safe_range_end-1 < results.size())) ? results[ safe_range_end-1 ].end : -1) + .detail("EndVersion", end) + .detail("SafeBegin", safe_range_begin) + .detail("SafeEnd", safe_range_end) + .detail("NewSafeBegin", new_safe_range_begin) + .detail("LogZones", ::describeZones(prevState.tLogLocalities)) + .detail("LogDataHalls", ::describeDataHalls(prevState.tLogLocalities)) + .detail("tLogs", (int)prevState.tLogs.size()) + .detail("oldTlogsSize", (int)prevState.oldTLogData.size()) + .detail("logSystemType", prevState.logSystemType) + .detail("At", end).detail("AvailableServers", results.size()) + .detail("knownCommittedVersion", knownCommittedVersion); + + last_end = end; + logSystem->logServers = logServers; + logSystem->oldLogData = oldLogData; + logSystem->tLogReplicationFactor = prevState.tLogReplicationFactor; + logSystem->tLogWriteAntiQuorum = prevState.tLogWriteAntiQuorum; + logSystem->tLogPolicy = prevState.tLogPolicy; + logSystem->tLogLocalities = prevState.tLogLocalities; + logSystem->logSystemType = prevState.logSystemType; + logSystem->rejoins = rejoins; + logSystem->epochEndVersion = end; + logSystem->knownCommittedVersion = knownCommittedVersion; + + for(auto &r : results) + logSystem->epochEndTags.insert( r.tags.begin(), r.tags.end() ); + + outLogSystem->set(logSystem); + } + else { + TraceEvent("LogSystemUnchangedRecovery", dbgid).detail("Cycles", cycles) + .detail("TotalServers", logServers.size()) + .detail("Required", requiredCount) + .detail("Present", results.size()) + .detail("Available", availableItems.size()) + .detail("Absent", logServers.size() - results.size()) + .detail("ServerState", sServerState) + .detail("ReplicationFactor", prevState.tLogReplicationFactor) + .detail("AntiQuorum", prevState.tLogWriteAntiQuorum) + .detail("Policy", prevState.tLogPolicy->info()) + .detail("TooManyFailures", bTooManyFailures) + .detail("LastVersion", (last_end.present()) ? last_end.get() : -1L) + .detail("RecoveryVersion", ((safe_range_end > 0) && (safe_range_end-1 < results.size())) ? results[ safe_range_end-1 ].end : -1) + .detail("EndVersion", end) + .detail("SafeBegin", safe_range_begin) + .detail("SafeEnd", safe_range_end) + .detail("NewSafeBegin", new_safe_range_begin) + .detail("LogZones", ::describeZones(prevState.tLogLocalities)) + .detail("LogDataHalls", ::describeDataHalls(prevState.tLogLocalities)); + } + } + // Too many failures + else { + TraceEvent("LogSystemWaitingForRecovery", dbgid).detail("Cycles", cycles) + .detail("AvailableServers", results.size()) + .detail("RequiredServers", requiredCount) + .detail("TotalServers", logServers.size()) + .detail("Required", requiredCount) + .detail("Present", results.size()) + .detail("Available", availableItems.size()) + .detail("Absent", logServers.size() - results.size()) + .detail("ServerState", sServerState) + .detail("ReplicationFactor", prevState.tLogReplicationFactor) + .detail("AntiQuorum", prevState.tLogWriteAntiQuorum) + .detail("Policy", prevState.tLogPolicy->info()) + .detail("TooManyFailures", bTooManyFailures) + .detail("LogZones", ::describeZones(prevState.tLogLocalities)) + .detail("LogDataHalls", ::describeDataHalls(prevState.tLogLocalities)); + } + + // Wait for anything relevant to change + std::vector> changes; + for(int i=0; ionChange() ); + changes.push_back( logFailed[i]->onChange() ); + } + } + ASSERT(changes.size()); + Void _ = wait(waitForAny(changes)); + } + } + + ACTOR static Future> newEpoch( + Reference oldLogSystem, vector workers, DatabaseConfiguration configuration, LogEpoch recoveryCount ) + { + state double startTime = now(); + state Reference logSystem( new TagPartitionedLogSystem(oldLogSystem->getDebugID(), oldLogSystem->locality) ); + state UID recruitmentID = g_random->randomUniqueID(); + + logSystem->tLogWriteAntiQuorum = configuration.tLogWriteAntiQuorum; + logSystem->tLogReplicationFactor = configuration.tLogReplicationFactor; + logSystem->tLogPolicy = configuration.tLogPolicy; + logSystem->logSystemType = 2; + + if(oldLogSystem->logServers.size()) { + logSystem->oldLogData.push_back(OldLogData()); + logSystem->oldLogData[0].tLogWriteAntiQuorum = oldLogSystem->tLogWriteAntiQuorum; + logSystem->oldLogData[0].tLogReplicationFactor = oldLogSystem->tLogReplicationFactor; + logSystem->oldLogData[0].tLogPolicy = oldLogSystem->tLogPolicy; + logSystem->oldLogData[0].tLogLocalities = oldLogSystem->tLogLocalities; + logSystem->oldLogData[0].epochEnd = oldLogSystem->knownCommittedVersion + 1; + logSystem->oldLogData[0].logServers = oldLogSystem->logServers; + } + + for(int i = 0; i < oldLogSystem->oldLogData.size(); i++) { + logSystem->oldLogData.push_back(oldLogSystem->oldLogData[i]); + } + + state vector> initializationReplies; + vector< InitializeTLogRequest > reqs( workers.size() ); + + for( int i = 0; i < workers.size(); i++ ) { + InitializeTLogRequest &req = reqs[i]; + req.recruitmentID = recruitmentID; + req.storeType = configuration.tLogDataStoreType; + req.recoverFrom = oldLogSystem->getLogSystemConfig(); + req.recoverAt = oldLogSystem->epochEndVersion.get(); + req.knownCommittedVersion = oldLogSystem->knownCommittedVersion; + req.epoch = recoveryCount; + } + + logSystem->tLogLocalities.resize( workers.size() ); + logSystem->logServers.resize( workers.size() ); // Dummy interfaces, so that logSystem->getPushLocations() below uses the correct size + + // Send requests array (reqs) also + logSystem->UpdateLocalitySet(workers, reqs); + + std::vector locations; + for( Tag tag : oldLogSystem->getEpochEndTags() ) { + locations.clear(); + logSystem->getPushLocations( vector(1, tag), locations ); + for(int loc : locations) + reqs[ loc ].recoverTags.push_back( tag ); + } + + for( int i = 0; i < workers.size(); i++ ) + initializationReplies.push_back( transformErrors( throwErrorOr( workers[i].tLog.getReplyUnlessFailedFor( reqs[i], SERVER_KNOBS->TLOG_TIMEOUT, SERVER_KNOBS->MASTER_FAILURE_SLOPE_DURING_RECOVERY ) ), master_recovery_failed() ) ); + + Void _ = wait( waitForAll( initializationReplies ) ); + + for( int i = 0; i < initializationReplies.size(); i++ ) { + logSystem->logServers[i] = Reference>>( new AsyncVar>( OptionalInterface(initializationReplies[i].get()) ) ); + logSystem->tLogLocalities[i] = workers[i].locality; + } + + //Don't force failure of recovery if it took us a long time to recover. This avoids multiple long running recoveries causing tests to timeout + if (BUGGIFY && now() - startTime < 300 && g_network->isSimulated() && g_simulator.speedUpSimulation) throw master_recovery_failed(); + + std::vector> recoveryComplete; + for( int i = 0; i < logSystem->logServers.size(); i++) + recoveryComplete.push_back( transformErrors( throwErrorOr( logSystem->logServers[i]->get().interf().recoveryFinished.getReplyUnlessFailedFor( TLogRecoveryFinishedRequest(), SERVER_KNOBS->TLOG_TIMEOUT, SERVER_KNOBS->MASTER_FAILURE_SLOPE_DURING_RECOVERY ) ), master_recovery_failed() ) ); + logSystem->recoveryComplete = waitForAll(recoveryComplete); + + return logSystem; + } + + ACTOR static Future trackRejoins( UID dbgid, std::vector>>> logServers, FutureStream< struct TLogRejoinRequest > rejoinRequests ) { + state std::map> lastReply; + + try { + loop { + TLogRejoinRequest req = waitNext( rejoinRequests ); + int pos = -1; + for( int i = 0; i < logServers.size(); i++ ) { + if( logServers[i]->get().id() == req.myInterface.id() ) { + pos = i; + break; + } + } + if ( pos != -1 ) { + TraceEvent("TLogJoinedMe", dbgid).detail("TLog", req.myInterface.id()).detail("Address", req.myInterface.commit.getEndpoint().address.toString()); + if( !logServers[pos]->get().present() || req.myInterface.commit.getEndpoint() != logServers[pos]->get().interf().commit.getEndpoint()) + logServers[pos]->setUnconditional( OptionalInterface(req.myInterface) ); + lastReply[req.myInterface.id()].send(false); + lastReply[req.myInterface.id()] = req.reply; + } + else { + TraceEvent("TLogJoinedMeUnknown", dbgid).detail("TLog", req.myInterface.id()).detail("Address", req.myInterface.commit.getEndpoint().address.toString()); + req.reply.send(true); + } + } + } catch (...) { + for( auto it = lastReply.begin(); it != lastReply.end(); ++it) + it->second.send(true); + throw; + } + } + + ACTOR static Future lockTLog( UID myID, Reference>> tlog ) { + TraceEvent("TLogLockStarted", myID).detail("TLog", tlog->get().id()); + loop { + choose { + when (TLogLockResult data = wait( tlog->get().present() ? brokenPromiseToNever( tlog->get().interf().lock.getReply() ) : Never() )) { + TraceEvent("TLogLocked", myID).detail("TLog", tlog->get().id()).detail("end", data.end); + return data; + } + when (Void _ = wait(tlog->onChange())) {} + } + } + } + + template + static vector getReadyNonError( vector> const& futures ) { + // Return the values of those futures which have (non-error) values ready + std::vector result; + for(auto& f : futures) + if (f.isReady() && !f.isError()) + result.push_back(f.get()); + return result; + } + + struct sort_by_end { + bool operator ()(TLogLockResult const&a, TLogLockResult const& b) const { return a.end < b.end; } + }; +}; + +Future ILogSystem::recoverAndEndEpoch(Reference>> const& outLogSystem, UID const& dbgid, DBCoreState const& oldState, FutureStream const& rejoins, LocalityData const& locality ) { + return TagPartitionedLogSystem::recoverAndEndEpoch( outLogSystem, dbgid, oldState, rejoins, locality ); +} + +Reference ILogSystem::fromLogSystemConfig( UID const& dbgid, struct LocalityData const& locality, struct LogSystemConfig const& conf ) { + if (conf.logSystemType == 0) + return Reference(); + else if (conf.logSystemType == 2) + return TagPartitionedLogSystem::fromLogSystemConfig( dbgid, locality, conf ); + else + throw internal_error(); +} + +Reference ILogSystem::fromOldLogSystemConfig( UID const& dbgid, struct LocalityData const& locality, struct LogSystemConfig const& conf ) { + if (conf.logSystemType == 0) + return Reference(); + else if (conf.logSystemType == 2) + return TagPartitionedLogSystem::fromOldLogSystemConfig( dbgid, locality, conf ); + else + throw internal_error(); +} + +Reference ILogSystem::fromServerDBInfo( UID const& dbgid, ServerDBInfo const& dbInfo ) { + return fromLogSystemConfig( dbgid, dbInfo.myLocality, dbInfo.logSystemConfig ); +} diff --git a/fdbserver/TesterInterface.h b/fdbserver/TesterInterface.h new file mode 100644 index 0000000000..09241e0a87 --- /dev/null +++ b/fdbserver/TesterInterface.h @@ -0,0 +1,93 @@ +/* + * TesterInterface.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_TESTERINTERFACE_H +#define FDBSERVER_TESTERINTERFACE_H +#pragma once + +#include "fdbrpc/fdbrpc.h" +#include "fdbrpc/PerfMetric.h" +#include "fdbclient/NativeAPI.h" + +struct WorkloadInterface { + RequestStream> setup; + RequestStream> start; + RequestStream> check; + RequestStream > > metrics; + RequestStream> stop; + + UID id() const { return setup.getEndpoint().token; } + + template + void serialize( Ar& ar ) { + ar & setup & start & check & metrics & stop; + } +}; + +struct WorkloadRequest { + Arena arena; + StringRef title; + StringRef database; + int timeout; + double databasePingDelay; + int64_t sharedRandomNumber; + + // The vector of option lists are to construct compound workloads. If there + // is only one workload to be run...pass just one list of options! + // + // Options are well-defined...and each workload has different defaults + // Parameter Description + // - testName the name of the test to run + // - testDuration in seconds + // - transactionsPerSecond + // - actorsPerClient + // - nodeCount + + VectorRef< VectorRef > options; + + int clientId; // the "id" of the client recieving the request (0 indexed) + int clientCount; // the total number of test clients participating in the workload + ReplyPromise< struct WorkloadInterface > reply; + + template + void serialize( Ar& ar ) { + ar & title & database & timeout & databasePingDelay & sharedRandomNumber & options & clientId & clientCount & reply & arena; + } +}; + +struct TesterInterface { + RequestStream recruitments; + + UID id() const { return recruitments.getEndpoint().token; } + + template + void serialize(Ar& ar) { + ar & recruitments; + } +}; + +Future testerServerCore( TesterInterface const& interf, Reference const& ccf, Reference> const& ); + +enum test_location_t { TEST_HERE, TEST_ON_SERVERS, TEST_ON_TESTERS }; +enum test_type_t { TEST_TYPE_FROM_FILE, TEST_TYPE_CONSISTENCY_CHECK }; + +Future runTests( Reference const& connFile, test_type_t const& whatToRun, test_location_t const& whereToRun, int const& minTestersExpected, std::string const& fileName = std::string(), StringRef const& startingConfiguration = StringRef() ); + +#endif diff --git a/fdbserver/VFSAsync.cpp b/fdbserver/VFSAsync.cpp new file mode 100755 index 0000000000..fd8f233906 --- /dev/null +++ b/fdbserver/VFSAsync.cpp @@ -0,0 +1,783 @@ +/* + * VFSAsync.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sqlite/sqlite3.h" +#include +#include +#include +#include "fdbrpc/fdbrpc.h" +#include "fdbrpc/IAsyncFile.h" +#include "CoroFlow.h" +#include "fdbrpc/simulator.h" +#include "fdbrpc/AsyncFileReadAhead.actor.h" + +#include +#include + +#ifdef WIN32 +#include "Windows.h" +#endif + +#ifdef __unixish__ +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +/* +** The maximum pathname length supported by this VFS. +*/ +#define MAXPATHNAME 512 + +#define NO_LOCK 0 +#define SHARED_LOCK 1 +#define RESERVED_LOCK 2 +#define PENDING_LOCK 3 +#define EXCLUSIVE_LOCK 4 +const uint32_t RESERVED_COUNT = 1U<<29; +const uint32_t PENDING_COUNT = 1U<<30; +const uint32_t EXCLUSIVE_COUNT = 1U<<31; + +/* +** When using this VFS, the sqlite3_file* handles that SQLite uses are +** actually pointers to instances of type VFSAsyncFile. +*/ +typedef struct VFSAsyncFile VFSAsyncFile; +struct VFSAsyncFile { + sqlite3_file base; /* Base class. Must be first. */ + int flags; + std::string filename; + Reference file; + + uint32_t * const pLockCount; // +1 for each SHARED_LOCK, or 1+X_COUNT for lock level X + int lockLevel; // NO_LOCK, SHARED_LOCK, RESERVED_LOCK, PENDING_LOCK, or EXCLUSIVE_LOCK + + struct SharedMemoryInfo *sharedMemory; + int sharedMemorySharedLocks; + int sharedMemoryExclusiveLocks; + + int debug_zcrefs, debug_zcreads, debug_reads; + + VFSAsyncFile(std::string const& filename, int flags) : filename(filename), flags(flags), pLockCount(&filename_lockCount_openCount[filename].first), debug_zcrefs(0), debug_zcreads(0), debug_reads(0) { + filename_lockCount_openCount[filename].second++; + } + ~VFSAsyncFile(); + + static std::map> filename_lockCount_openCount; +}; +std::map> VFSAsyncFile::filename_lockCount_openCount; + +static int asyncClose(sqlite3_file *pFile){ + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + + /*TraceEvent("VFSAsyncClose").detail("fd", p->file->debugFD()) + .detail("Filename", p->filename).detail("ZCRefs", p->debug_zcrefs) + .detail("ZCReads", p->debug_zcreads).detail("NormalReads", p->debug_reads).backtrace();*/ + //printf("Closing %s: %d zcrefs, %d/%d reads zc\n", filename.c_str(), debug_zcrefs, debug_zcreads, debug_zcreads+debug_reads); + ASSERT( !p->debug_zcrefs ); + + p->~VFSAsyncFile(); + return SQLITE_OK; +} + +static int asyncRead(sqlite3_file *pFile, void *zBuf, int iAmt, sqlite_int64 iOfst) { + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + try { + ++p->debug_reads; + int readBytes = waitForAndGet( p->file->read( zBuf, iAmt, iOfst ) ); + if (readBytes < iAmt) { + memset((uint8_t*)zBuf + readBytes, 0, iAmt-readBytes); // When reading past the EOF, sqlite expects the extra portion of the buffer to be zeroed + return SQLITE_IOERR_SHORT_READ; + } + return SQLITE_OK; + } catch (Error& e) { + return SQLITE_IOERR_READ; + } +} + +#if 1 +static int asyncReleaseZeroCopy(sqlite3_file* pFile, void* data, int iAmt, sqlite_int64 iOfst) { + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + try{ + --p->debug_zcrefs; + p->file->releaseZeroCopy( data, iAmt, iOfst ); + } catch (Error& e) { + return SQLITE_IOERR; + } + return SQLITE_OK; +} + +static int asyncReadZeroCopy(sqlite3_file *pFile, void **data, int iAmt, sqlite_int64 iOfst, int *pDataWasCached) { + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + try { + int readBytes = iAmt; + Future readFuture = p->file->readZeroCopy( data, &readBytes, iOfst ); + if(pDataWasCached) + *pDataWasCached = readFuture.isReady() ? 1 : 0; + waitFor(readFuture); + ++p->debug_zcrefs; + if (readBytes < iAmt) { + // When reading past the EOF, sqlite expects the extra portion of the buffer to be zeroed. We can't do that, so return and sqlite will use the slow path. + asyncReleaseZeroCopy(pFile, *data, readBytes, iOfst); + return SQLITE_IOERR_SHORT_READ; + } + ++p->debug_zcreads; + return SQLITE_OK; + } catch (Error& e) { + return SQLITE_IOERR_READ; + } +} + +#else +static int asyncReadZeroCopy(sqlite3_file *pFile, void **data, int iAmt, sqlite_int64 iOfst) { + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + try { + *data = new char[iAmt]; + int readBytes = waitForAndGet( p->file->read( *data, iAmt, iOfst ) ); + //printf("+asyncReadRef %p +%lld %d/%d = %p\n", pFile, iOfst, readBytes, iAmt, *data); + if (readBytes < iAmt) { + memset((uint8_t*)*data + readBytes, 0, iAmt-readBytes); // When reading past the EOF, sqlite expects the extra portion of the buffer to be zeroed + return SQLITE_IOERR_SHORT_READ; + } + return SQLITE_OK; + } catch (Error& e) { + return SQLITE_IOERR_READ; + } +} +static int asyncReleaseZeroCopy(sqlite3_file* pFile, void* data, int iAmt, sqlite_int64 iOfst) { + //printf("-asyncReleaseRef %p +%lld %d <= %p\n", pFile, iOfst, iAmt, data); + delete[] (char*)data; + return SQLITE_OK; +} +#endif + +static int asyncWrite(sqlite3_file *pFile, const void *zBuf, int iAmt, sqlite_int64 iOfst) { + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + try { + waitFor( p->file->write( zBuf, iAmt, iOfst ) ); + return SQLITE_OK; + } catch(Error& e) { + return SQLITE_IOERR_WRITE; + } +} + +static int asyncTruncate(sqlite3_file *pFile, sqlite_int64 size){ + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + try { + waitFor( p->file->truncate( size ) ); + return SQLITE_OK; + } catch(Error& e) { + return SQLITE_IOERR_TRUNCATE; + } +} + +static int asyncSync(sqlite3_file *pFile, int flags){ + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + try { + waitFor( p->file->sync() ); + return SQLITE_OK; + } catch (Error& e) { + TraceEvent("VFSSyncError") + .detail("Filename", p->filename) + .detail("sqlite3_file", (int64_t)pFile) + .detail("IAsyncFile", (int64_t)p->file.getPtr()) + .error(e); + + return SQLITE_IOERR_FSYNC; + } +} + +/* +** Write the size of the file in bytes to *pSize. +*/ +static int VFSAsyncFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){ + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + try { + *pSize = waitForAndGet( p->file->size() ); + return SQLITE_OK; + } catch (Error& e) { + return SQLITE_IOERR_FSTAT; + } +} + +static int asyncLock(sqlite3_file *pFile, int eLock){ + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + + //TraceEvent("FileLock").detail("File", p->filename).detail("fd", p->file->debugFD()).detail("PrevLockLevel", p->lockLevel).detail("Op", eLock).detail("LockCount", *p->pLockCount); + + return eLock == EXCLUSIVE_LOCK ? SQLITE_BUSY : SQLITE_OK; +} +static int asyncUnlock(sqlite3_file *pFile, int eLock) { + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + assert( eLock <= SHARED_LOCK ); + + return SQLITE_OK; +} +static int asyncCheckReservedLock(sqlite3_file *pFile, int *pResOut){ + VFSAsyncFile *p = (VFSAsyncFile*)pFile; + *pResOut = *p->pLockCount >= RESERVED_COUNT; + return SQLITE_OK; +} + +/* +** No xFileControl() verbs are implemented by this VFS. +*/ +static int VFSAsyncFileControl(sqlite3_file *pFile, int op, void *pArg){ + return SQLITE_NOTFOUND; +} + +static int asyncSectorSize(sqlite3_file *pFile){ return 512; } // SOMEDAY: Would 4K be better? +static int asyncDeviceCharacteristics(sqlite3_file *pFile){ return 0; } + +#if 1 + struct SharedMemoryInfo { // for a file + std::string filename; + std::vector regions; + int regionSize; + int refcount; // Number of connections with this open + int sharedLocks[SQLITE_SHM_NLOCK]; + int exclusiveLocks[SQLITE_SHM_NLOCK]; + + SharedMemoryInfo() : regionSize(0), refcount(0) { + memset(sharedLocks, 0, sizeof(sharedLocks)); + memset(exclusiveLocks, 0, sizeof(exclusiveLocks)); + } + void cleanup(){ + for(int i=0; i table; + }; + Mutex SharedMemoryInfo::mutex; + std::map< std::string, SharedMemoryInfo > SharedMemoryInfo::table; + + /* + ** This function is called to obtain a pointer to region iRegion of the + ** shared-memory associated with the database file fd. Shared-memory regions + ** are numbered starting from zero. Each shared-memory region is szRegion + ** bytes in size. + ** + ** If an error occurs, an error code is returned and *pp is set to NULL. + ** + ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory + ** region has not been allocated (by any client, including one running in a + ** separate process), then *pp is set to NULL and SQLITE_OK returned. If + ** bExtend is non-zero and the requested shared-memory region has not yet + ** been allocated, it is allocated by this function. + ** + ** If the shared-memory region has already been allocated or is allocated by + ** this call as described above, then it is mapped into this processes + ** address space (if it is not already), *pp is set to point to the mapped + ** memory and SQLITE_OK returned. + */ + static int asyncShmMap( + sqlite3_file *fd, /* Handle open on database file */ + int iRegion, /* Region to retrieve */ + int szRegion, /* Size of regions */ + int bExtend, /* True to extend file if necessary */ + void volatile **pp /* OUT: Mapped memory */ + ) + { + MutexHolder hold( SharedMemoryInfo::mutex ); + + VFSAsyncFile *pDbFd = (VFSAsyncFile*)fd; + SharedMemoryInfo* memInfo = pDbFd->sharedMemory; + if (!memInfo) { + std::string filename = pDbFd->filename; + memInfo = pDbFd->sharedMemory = &SharedMemoryInfo::table[ filename ]; + memInfo->filename = filename; + memInfo->regionSize = szRegion; + ++memInfo->refcount; + //printf("Shared memory for: '%s' (%d refs)\n", filename.c_str(), memInfo->refcount); + } else { + assert( memInfo->regionSize == szRegion ); + } + + if (iRegion >= memInfo->regions.size()) { + if (!bExtend) { *pp = NULL; return SQLITE_OK; } + while (memInfo->regions.size() <= iRegion) { + void *mem = new uint8_t[ szRegion ]; + memset( mem, 0, szRegion ); + memInfo->regions.push_back( mem ); + } + } + *pp = memInfo->regions[ iRegion ]; + return SQLITE_OK; + } + + /* + ** Change the lock state for a shared-memory segment. + ** + ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little + ** different here than in posix. In xShmLock(), one can go from unlocked + ** to shared and back or from unlocked to exclusive and back. But one may + ** not go from shared to exclusive or from exclusive to shared. + */ + // sqlite doesn't seem to match these up correctly - it happily calls unlock on locks it doesn't hold. + // So we have to keep track of which locks are held by a given sqlite3_file + static int asyncShmLock( + sqlite3_file *fd, /* Database file holding the shared memory */ + int ofst, /* First lock to acquire or release */ + int n, /* Number of locks to acquire or release */ + int flags /* What to do with the lock */ + ){ + assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); + assert( n>=1 ); + assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) + || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) + || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) + || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); + assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); + + MutexHolder hold( SharedMemoryInfo::mutex ); + + VFSAsyncFile *pDbFd = (VFSAsyncFile*)fd; + SharedMemoryInfo* memInfo = pDbFd->sharedMemory; + + if (flags & SQLITE_SHM_UNLOCK) { + for(int i=ofst; isharedMemorySharedLocks & (1<sharedMemorySharedLocks &= ~(1<sharedLocks[i]; + } + if ( pDbFd->sharedMemoryExclusiveLocks & (1<sharedMemoryExclusiveLocks &= ~(1<exclusiveLocks[i]; + } + } + } else if (flags & SQLITE_SHM_SHARED) { + for(int i=ofst; iexclusiveLocks[i] != ((pDbFd->sharedMemoryExclusiveLocks>>i)&1) ) { + //TraceEvent("ShmLocked").detail("File", DEBUG_DETERMINISM ? 0 : (int64_t)pDbFd).detail("Acquiring", "Shared").detail("i", i).detail("Exclusive", memInfo->exclusiveLocks[i]).detail("MyExclusive", pDbFd->sharedMemoryExclusiveLocks); + return SQLITE_BUSY; + } + for(int i=ofst; isharedMemorySharedLocks & (1<sharedMemorySharedLocks |= 1<sharedLocks[i]++; + } + } else { + for(int i=ofst; iexclusiveLocks[i] != ((pDbFd->sharedMemoryExclusiveLocks>>i)&1) || + memInfo->sharedLocks[i] != ((pDbFd->sharedMemorySharedLocks>>i)&1) ) + { + //TraceEvent("ShmLocked").detail("File", DEBUG_DETERMINISM ? 0 : (int64_t)pDbFd).detail("Acquiring", "Exclusive").detail("i", i).detail("Exclusive", memInfo->exclusiveLocks[i]).detail("MyExclusive", pDbFd->sharedMemoryExclusiveLocks).detail("Shared", memInfo->sharedLocks[i]).detail("MyShared", pDbFd->sharedMemorySharedLocks); + return SQLITE_BUSY; + } + for(int i=ofst; isharedMemoryExclusiveLocks & (1<sharedMemoryExclusiveLocks |= 1<exclusiveLocks[i]++; + } + } + return SQLITE_OK; + } + + /* + ** Implement a memory barrier or memory fence on shared memory. + ** + ** All loads and stores begun before the barrier must complete before + ** any load or store begun after the barrier. + */ + static void asyncShmBarrier(sqlite3_file*){ +#if WIN32 + _ReadWriteBarrier(); +#else + __sync_synchronize(); +#endif + } + + /* + ** Close a connection to shared-memory. Delete the underlying + ** storage if deleteFlag is true. + ** + ** If there is no shared memory associated with the connection then this + ** routine is a harmless no-op. + */ + static int asyncShmUnmap( + sqlite3_file *fd, /* The underlying database file */ + int deleteFlag /* Delete shared-memory if true */ + ){ + MutexHolder hold( SharedMemoryInfo::mutex ); + + VFSAsyncFile *pDbFd = (VFSAsyncFile*)fd; + SharedMemoryInfo* memInfo = pDbFd->sharedMemory; + if (!memInfo) return SQLITE_OK; + pDbFd->sharedMemory = 0; + + //printf("Connection %p closed shared memory\n", fd); + + if (!--memInfo->refcount) { + //printf("Cleanup shared memory for: '%s' (%d refs; deleteFlag=%d)\n", memInfo->filename.c_str(), memInfo->refcount, deleteFlag); + //printf(" Shared locks: "); for(int i=0; i<8; i++) printf("%d ", memInfo->sharedLocks[i]); printf("\n"); + //printf(" Exclusive locks: "); for(int i=0; i<8; i++) printf("%d ", memInfo->exclusiveLocks[i]); printf("\n"); + + //TraceEvent("CleanupSharedMemory").detail("Filename", memInfo->filename.c_str()).detail("RefCount", memInfo->refcount).detail("DeleteFlag", deleteFlag); + //for(int i = 0; i < 8; i++) + //TraceEvent("CleanupSharedMemory_Locks").detail("Filename", memInfo->filename.c_str()).detail("Num", i).detail("Shared", memInfo->sharedLocks[i]).detail("Exclusive", memInfo->exclusiveLocks[i]); + + //We don't think deleteFlag will ever be set + ASSERT(!deleteFlag); + } + return SQLITE_OK; + } + + VFSAsyncFile::~VFSAsyncFile() { + //TraceEvent("VFSAsyncFileDel").detail("filename", filename); + if (!--filename_lockCount_openCount[filename].second) { + filename_lockCount_openCount.erase(filename); + + //Always delete the shared memory when the last copy of the file is deleted. In simulation, this is helpful because "killing" a file without properly closing + //it can result in a shared memory state that causes corruption when reopening the killed file. The only expected penalty from doing this + //is a potentially slower open operation on a database, but that should happen infrequently. + // + //We can't do this in ShmUnmap when refcount is 0 because it seems that SQLite sometimes subsequently tries to reopen the WAL from multiple locations simultaneously, + //resulting in a locking error + auto itr = SharedMemoryInfo::table.find(filename); + if(itr != SharedMemoryInfo::table.end()) { + ASSERT(itr->second.refcount == 0); + itr->second.cleanup(); + } + } + } + +#endif + +/* +** Open a file handle. +*/ +static int asyncOpen( + sqlite3_vfs *pVfs, /* VFS */ + const char *zName, /* File to open, or 0 for a temp file */ + sqlite3_file *pFile, /* Pointer to VFSAsyncFile struct to populate */ + int flags, /* Input SQLITE_OPEN_XXX flags */ + int *pOutFlags /* Output SQLITE_OPEN_XXX flags (or NULL) */ +){ + static const sqlite3_io_methods asyncio = { + 3, /* iVersion */ + asyncClose, /* xClose */ + asyncRead, /* xRead */ + asyncWrite, /* xWrite */ + asyncTruncate, /* xTruncate */ + asyncSync, /* xSync */ + VFSAsyncFileSize, /* xFileSize */ + asyncLock, /* xLock */ + asyncUnlock, /* xUnlock */ + asyncCheckReservedLock, /* xCheckReservedLock */ + VFSAsyncFileControl, /* xFileControl */ + asyncSectorSize, /* xSectorSize */ + asyncDeviceCharacteristics, /* xDeviceCharacteristics */ + asyncShmMap, + asyncShmLock, + asyncShmBarrier, + asyncShmUnmap, + asyncReadZeroCopy, + asyncReleaseZeroCopy + }; + + VFSAsyncFile *p = (VFSAsyncFile*)pFile; /* Populate this structure */ + + if( zName==0 ) + return SQLITE_IOERR; + + static_assert( SQLITE_OPEN_EXCLUSIVE == IAsyncFile::OPEN_EXCLUSIVE && + SQLITE_OPEN_CREATE == IAsyncFile::OPEN_CREATE && + SQLITE_OPEN_READONLY == IAsyncFile::OPEN_READONLY && + SQLITE_OPEN_READWRITE == IAsyncFile::OPEN_READWRITE, "SQLite flag values don't match IAsyncFile flag values" ); + + // File creation here is disabled because we always create the files first in KeyValueStoreSQLite, using atomic creation + int oflags = flags & (/*SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_CREATE |*/ SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE); + if (flags & SQLITE_OPEN_WAL) oflags |= IAsyncFile::OPEN_LARGE_PAGES; + oflags |= IAsyncFile::OPEN_LOCK; + + memset(p, 0, sizeof(VFSAsyncFile)); + new (p) VFSAsyncFile(zName, flags); + try { + // Note that SQLiteDB::open also opens the db file, so its flags and modes are important, too + p->file = waitForAndGet( IAsyncFileSystem::filesystem()->open( p->filename, oflags, 0600 ) ); + + /*TraceEvent("VFSOpened") + .detail("Filename", p->filename) + .detail("fd", DEBUG_DETERMINISM ? 0 : p->file->debugFD()) + .detail("Flags", flags) + .detail("sqlite3_file", DEBUG_DETERMINISM ? 0 : (int64_t)pFile) + .detail("IAsyncFile", DEBUG_DETERMINISM ? 0 : (int64_t)p->file.getPtr());*/ + } catch (Error& e) { + TraceEvent("SQLiteOpenFail").detail("Filename", p->filename).error(e); + p->~VFSAsyncFile(); + return SQLITE_CANTOPEN; + } + + if( pOutFlags ){ + *pOutFlags = flags; + } + p->base.pMethods = &asyncio; + return SQLITE_OK; +} + +// The next few functions, which perform filesystem operations by path rather than by file, have +// OS-specific implementations. + +/* +** Delete the file identified by argument zPath. If the dirSync parameter +** is non-zero, then ensure the file-system modification to delete the +** file has been synced to disk before returning. +*/ +static int asyncDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){ + ASSERT( false ); // At the moment this isn't used; hence isn't under test. Could easily use IAsyncFileSystem::filesystem()->deleteFile(). + return SQLITE_IOERR_DELETE; +} + +/* +** Query the file-system to see if the named file exists, is readable or +** is both readable and writable. For an exists query, treat a zero-length file +** as if it does not exist. +*/ +static int asyncAccess( + sqlite3_vfs *pVfs, + const char *zPath, + int flags, + int *pResOut +){ +#ifdef __unixish__ + #ifndef F_OK + # define F_OK 0 + #endif + #ifndef R_OK + # define R_OK 4 + #endif + #ifndef W_OK + # define W_OK 2 + #endif + int rc; /* access() return code */ + int eAccess = F_OK; /* Second argument to access() */ + + assert(flags==SQLITE_ACCESS_EXISTS /* access(zPath, F_OK) */ + || flags==SQLITE_ACCESS_READ /* access(zPath, R_OK) */ + || flags==SQLITE_ACCESS_READWRITE /* access(zPath, R_OK|W_OK) */ + ); + + if( flags==SQLITE_ACCESS_READWRITE ) eAccess = R_OK|W_OK; + if( flags==SQLITE_ACCESS_READ ) eAccess = R_OK; + + rc = access(zPath, eAccess); + *pResOut = (rc==0); + + if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ + struct stat buf; + if( 0==stat(zPath, &buf) && buf.st_size==0 ){ + *pResOut = 0; + } + } + return SQLITE_OK; +#else + WIN32_FILE_ATTRIBUTE_DATA data; + DWORD attr = INVALID_FILE_ATTRIBUTES; + memset(&data, 0, sizeof(data)); + if (GetFileAttributesEx(zPath, GetFileExInfoStandard, &data)) { + if (!(flags == SQLITE_ACCESS_EXISTS && data.nFileSizeHigh==0 && data.nFileSizeLow==0)) + attr = data.dwFileAttributes; + } else if (GetLastError()!=ERROR_FILE_NOT_FOUND) + return SQLITE_IOERR_ACCESS; + + if (flags == SQLITE_ACCESS_READWRITE) + *pResOut = (attr & FILE_ATTRIBUTE_READONLY)==0; + else + *pResOut = attr != INVALID_FILE_ATTRIBUTES; + return SQLITE_OK; +#endif +} + +/* +** Argument zPath points to a nul-terminated string containing a file path. +** If zPath is an absolute path, then it is copied as is into the output +** buffer. Otherwise, if it is a relative path, then the equivalent full +** path is written to the output buffer. +*/ +static int asyncFullPathname( + sqlite3_vfs *pVfs, /* VFS */ + const char *zPath, /* Input path (possibly a relative path) */ + int nPathOut, /* Size of output buffer in bytes */ + char *zPathOut /* Pointer to output buffer */ +){ + try { + auto s = abspath( zPath ); + if (s.size() >= nPathOut) + return SQLITE_IOERR; + memcpy(zPathOut, s.c_str(), s.size()+1); + return SQLITE_OK; + } catch (Error& e) { + TraceEvent(SevError,"VFSAsyncFullPathnameError").detail("PathIn", (std::string)zPath).error(e); + return SQLITE_IOERR; + } catch(...) { + TraceEvent(SevError,"VFSAsyncFullPathnameError").detail("PathIn", (std::string)zPath).error(unknown_error()); + return SQLITE_IOERR; + } +} + +/* +** Returns true if there is a shared memory entry for the specified filename, +** and false otherwise. +*/ +bool vfsAsyncIsOpen( std::string filename ) { + return SharedMemoryInfo::table.count( abspath(filename) ); +} + +/* +** The following four VFS methods: +** +** xDlOpen +** xDlError +** xDlSym +** xDlClose +** +** are supposed to implement the functionality needed by SQLite to load +** extensions compiled as shared objects. This simple VFS does not support +** this functionality, so the following functions are no-ops. +*/ +static void *asyncDlOpen(sqlite3_vfs *pVfs, const char *zPath){ + return 0; +} +static void asyncDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){ + sqlite3_snprintf(nByte, zErrMsg, "Loadable extensions are not supported"); + zErrMsg[nByte-1] = '\0'; +} +static void (*asyncDlSym(sqlite3_vfs *pVfs, void *pH, const char *z))(void){ + return 0; +} +static void asyncDlClose(sqlite3_vfs *pVfs, void *pHandle){ + return; +} + +/* +** Parameter zByte points to a buffer nByte bytes in size. Populate this +** buffer with pseudo-random data. +*/ +static int asyncRandomness(sqlite3_vfs *pVfs, int nByte, char *zByte){ + for(int i=0; irandomInt(0,256); + return SQLITE_OK; +} + +/* +** Sleep for at least nMicro microseconds. Return the (approximate) number +** of microseconds slept for. +*/ +static int asyncSleep(sqlite3_vfs *pVfs, int microseconds){ + try { + Future simCancel = Never(); + if( g_network->isSimulated() ) + simCancel = success( g_simulator.getCurrentProcess()->shutdownSignal.getFuture() ); + if( simCancel.isReady() ) { + waitFor( delay(FLOW_KNOBS->MAX_BUGGIFIED_DELAY) ); + return 0; + } + waitFor( g_network->delay( microseconds*1e-6, TaskDefaultDelay ) || simCancel ); + return microseconds; + } catch( Error &e ) { + TraceEvent(SevError, "asyncSleepError").error(e,true); + return 0; + } +} + +/* +** Find the current time (in Universal Coordinated Time). Write into *piNow +** the current time and date as a Julian Day number times 86_400_000. In +** other words, write into *piNow the number of milliseconds since the Julian +** epoch of noon in Greenwich on November 24, 4714 B.C according to the +** proleptic Gregorian calendar. +** +** On success, return 0. Return 1 if the time and date cannot be found. +*/ +static int asyncCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ +#if __unixish__ + static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; + struct timeval sNow; + gettimeofday(&sNow, NULL); + *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; +#elif defined(_WIN32) + static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000; + int64_t ft = 0; + GetSystemTimeAsFileTime( (FILETIME*)&ft ); + *piNow = winFiletimeEpoch + ft / 10000; +#else +#error Port me! +#endif + return 0; +} + +/* +** Set *pTime to the current UTC time expressed as a Julian day. Return +** SQLITE_OK if successful, or an error code otherwise. +** +** http://en.wikipedia.org/wiki/Julian_day +*/ +static int asyncCurrentTime(sqlite3_vfs *pVfs, double *pTime){ + sqlite3_int64 t = 0; + int rc = asyncCurrentTimeInt64(pVfs, &t); + if (rc) return rc; + *pTime = t / 86400000.0; + return SQLITE_OK; +} + +static int asyncGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ return 0; } + +/* +** This function returns a pointer to the VFS implemented in this file. +** To make the VFS available to SQLite: +** +** sqlite3_vfs_register(sqlite3_asyncvfs(), 0); +*/ +sqlite3_vfs *vfsAsync(){ + static sqlite3_vfs asyncvfs = { + 3, /* iVersion */ + sizeof(VFSAsyncFile), /* szOsFile */ + MAXPATHNAME, /* mxPathname */ + 0, /* pNext */ + "fdb_async", /* zName */ + 0, /* pAppData */ + asyncOpen, /* xOpen */ + asyncDelete, /* xDelete */ + asyncAccess, /* xAccess */ + asyncFullPathname, /* xFullPathname */ + asyncDlOpen, /* xDlOpen */ + asyncDlError, /* xDlError */ + asyncDlSym, /* xDlSym */ + asyncDlClose, /* xDlClose */ + asyncRandomness, /* xRandomness */ + asyncSleep, /* xSleep */ + asyncCurrentTime, /* xCurrentTime */ + asyncGetLastError, /* xGetLastError */ + asyncCurrentTimeInt64, /* xCurrentTimeInt64 */ + 0, /* xSetSystemCall */ + 0, /* xGetSystemCall */ + 0, /* xNextSystemCall */ + + }; + return &asyncvfs; +} diff --git a/fdbserver/WaitFailure.actor.cpp b/fdbserver/WaitFailure.actor.cpp new file mode 100644 index 0000000000..c16dffb6fe --- /dev/null +++ b/fdbserver/WaitFailure.actor.cpp @@ -0,0 +1,81 @@ +/* + * WaitFailure.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "fdbrpc/FailureMonitor.h" +#include "flow/Deque.h" +#include "Knobs.h" + +ACTOR Future waitFailureServer(FutureStream> waitFailure){ + // when this actor is cancelled, the promises in the queue will send broken_promise + state Deque> queue; + state int limit = BUGGIFY ? SERVER_KNOBS->BUGGIFY_OUTSTANDING_WAIT_FAILURE_REQUESTS : SERVER_KNOBS->MAX_OUTSTANDING_WAIT_FAILURE_REQUESTS; + loop { + ReplyPromise P = waitNext(waitFailure); + queue.push_back(P); + if (queue.size()>SERVER_KNOBS->MAX_OUTSTANDING_WAIT_FAILURE_REQUESTS){ + TEST(true); // wait server queue full + queue.front().send(Void()); + queue.pop_front(); + } + } +} + +ACTOR Future waitFailureClient(RequestStream> waitFailure, double reactionTime, double reactionSlope, int taskID){ + loop { + try { + state double start = now(); + ErrorOr x = wait(waitFailure.getReplyUnlessFailedFor(ReplyPromise(), reactionTime, reactionSlope, taskID)); + if (!x.present()) return Void(); + double w = start + SERVER_KNOBS->WAIT_FAILURE_DELAY_LIMIT - now(); + if (w > 0) + Void _ = wait( delay( w, taskID ) ); + } catch (Error &e){ + if (e.code() == error_code_actor_cancelled) + throw; + TraceEvent(SevError, "waitFailureClientError").error(e); + ASSERT(false); // unknown error from waitFailureServer + } + } +} + +ACTOR Future waitFailureTracker(RequestStream> waitFailure, Reference> failed, double reactionTime, double reactionSlope, int taskID){ + loop { + try { + failed->set( IFailureMonitor::failureMonitor().getState(waitFailure.getEndpoint()).isFailed() ); + if( failed->get() ) { + Void _ = wait( IFailureMonitor::failureMonitor().onStateChanged(waitFailure.getEndpoint()) ); + } else { + state double start = now(); + ErrorOr x = wait(waitFailure.getReplyUnlessFailedFor(ReplyPromise(), reactionTime, reactionSlope, taskID)); + if (x.present()) { + double w = start + SERVER_KNOBS->WAIT_FAILURE_DELAY_LIMIT - now(); + if (w > 0) + Void _ = wait( delay( w, taskID ) ); + } + } + } catch (Error &e){ + if (e.code() == error_code_actor_cancelled) + throw; + TraceEvent(SevError, "waitFailureClientError").error(e); + ASSERT(false); // unknown error from waitFailureServer + } + } +} \ No newline at end of file diff --git a/fdbserver/WaitFailure.h b/fdbserver/WaitFailure.h new file mode 100644 index 0000000000..164c469870 --- /dev/null +++ b/fdbserver/WaitFailure.h @@ -0,0 +1,35 @@ +/* + * WaitFailure.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WAIT_FAILURE_SERVER_H +#define WAIT_FAILURE_SERVER_H +#pragma once + +Future waitFailureServer(const FutureStream>& waitFailure); + +// talks to a wait failure server, returns Void on failure +Future waitFailureClient(const RequestStream>& waitFailure, + double const& failureReactionTime=0, double const& failureReactionSlope=0, int const& taskID=TaskDefaultEndpoint); + +// talks to a wait failure server, updates failed to be true or false based on failure status. +Future waitFailureTracker(const RequestStream>& waitFailure, Reference> const& failed, + double const& failureReactionTime=0, double const& failureReactionSlope=0, int const& taskID=TaskDefaultEndpoint); + +#endif \ No newline at end of file diff --git a/fdbserver/WorkerInterface.h b/fdbserver/WorkerInterface.h new file mode 100644 index 0000000000..079d58e044 --- /dev/null +++ b/fdbserver/WorkerInterface.h @@ -0,0 +1,304 @@ +/* + * WorkerInterface.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FDBSERVER_WORKERINTERFACE_H +#define FDBSERVER_WORKERINTERFACE_H +#pragma once + +#include "MasterInterface.h" +#include "TLogInterface.h" +#include "ResolverInterface.h" +#include "fdbclient/StorageServerInterface.h" +#include "TesterInterface.h" +#include "fdbclient/FDBTypes.h" +#include "LogSystemConfig.h" +#include "fdbrpc/MultiInterface.h" +#include "fdbclient/ClientWorkerInterface.h" + +struct WorkerInterface { + ClientWorkerInterface clientInterface; + LocalityData locality; + RequestStream< struct InitializeTLogRequest > tLog; + RequestStream< struct RecruitMasterRequest > master; + RequestStream< struct InitializeMasterProxyRequest > masterProxy; + RequestStream< struct InitializeResolverRequest > resolver; + RequestStream< struct InitializeStorageRequest > storage; + + RequestStream< struct DebugQueryRequest > debugQuery; + RequestStream< struct LoadedPingRequest > debugPing; + RequestStream< struct CoordinationPingMessage > coordinationPing; + RequestStream< ReplyPromise > waitFailure; + RequestStream< struct SetMetricsLogRateRequest > setMetricsRate; + RequestStream< struct EventLogRequest > eventLogRequest; + RequestStream< struct TraceBatchDumpRequest > traceBatchDumpRequest; + RequestStream< struct DiskStoreRequest > diskStoreRequest; + + //A stream used to start or stop CPU profiling on a worker + RequestStream< struct ProfilerRequest > cpuProfilerRequest; + + TesterInterface testerInterface; + + UID id() const { return tLog.getEndpoint().token; } + NetworkAddress address() const { return tLog.getEndpoint().address; } + + WorkerInterface() {} + WorkerInterface( LocalityData locality ) : locality( locality ) {} + + template + void serialize(Ar& ar) { + ar & clientInterface & locality & tLog & master & masterProxy & resolver & storage & debugQuery & debugPing & coordinationPing & waitFailure & setMetricsRate & eventLogRequest & traceBatchDumpRequest & cpuProfilerRequest & testerInterface & diskStoreRequest; + } +}; + +struct InitializeTLogRequest { + UID recruitmentID; + LogSystemConfig recoverFrom; + Version recoverAt; + Version knownCommittedVersion; + LogEpoch epoch; + std::vector recoverTags; + KeyValueStoreType storeType; + ReplyPromise< struct TLogInterface > reply; + + InitializeTLogRequest() {} + + template + void serialize( Ar& ar ) { + ar & recruitmentID & recoverFrom & recoverAt & knownCommittedVersion & epoch & recoverTags & storeType & reply; + } +}; + +// FIXME: Rename to InitializeMasterRequest, etc +struct RecruitMasterRequest { + Arena arena; + LifetimeToken lifetime; + ReplyPromise< struct MasterInterface> reply; + + template + void serialize(Ar& ar) { + ASSERT( ar.protocolVersion() >= 0x0FDB00A200040001LL ); + ar & lifetime & reply & arena; + } +}; + +struct InitializeMasterProxyRequest { + MasterInterface master; + uint64_t recoveryCount; + Version recoveryTransactionVersion; + bool firstProxy; + ReplyPromise reply; + + template + void serialize(Ar& ar) { + ar & master & recoveryCount & recoveryTransactionVersion & firstProxy & reply; + } +}; + +struct InitializeResolverRequest { + uint64_t recoveryCount; + int proxyCount; + int resolverCount; + ReplyPromise reply; + + template + void serialize(Ar& ar) { + ar & recoveryCount & proxyCount & resolverCount & reply; + } +}; + +struct InitializeStorageRequest { + Tag seedTag; //< If this server will be passed to seedShardServers, this will be a tag, otherwise it is invalidTag + UID reqId; + UID interfaceId; + KeyValueStoreType storeType; + ReplyPromise< struct StorageServerInterface > reply; + + template + void serialize( Ar& ar ) { + ar & seedTag & reqId & interfaceId & storeType & reply; + } +}; + +struct TraceBatchDumpRequest { + ReplyPromise reply; + + template + void serialize( Ar& ar ) { + ar & reply; + } +}; + +//A request to start or stop CPU profiling on a worker +struct ProfilerRequest { + ReplyPromise reply; + + bool enabled; + Standalone outputFile; + + template + void serialize( Ar& ar ) { + ar & reply & enabled & outputFile; + } +}; + +struct LoadedReply { + Standalone payload; + UID id; + + template + void serialize(Ar& ar) { + ar & payload & id; + } +}; + +struct LoadedPingRequest { + UID id; + bool loadReply; + Standalone payload; + ReplyPromise reply; + + template + void serialize(Ar& ar) { + ar & id & loadReply & payload & reply; + } +}; + +struct CoordinationPingMessage { + UID clusterControllerId; + int64_t timeStep; + + CoordinationPingMessage() : timeStep(0) {} + CoordinationPingMessage(UID ccId, uint64_t step) : clusterControllerId( ccId ), timeStep( step ) {} + + template + void serialize(Ar& ar) { + ar & clusterControllerId & timeStep; + } +}; + +struct SetMetricsLogRateRequest { + uint32_t metricsLogsPerSecond; + + SetMetricsLogRateRequest() : metricsLogsPerSecond( 1 ) {} + explicit SetMetricsLogRateRequest(uint32_t logsPerSecond) : metricsLogsPerSecond( logsPerSecond ) {} + + template + void serialize(Ar& ar) { + ar & metricsLogsPerSecond; + } +}; + +struct EventLogRequest { + bool getLastError; + Standalone eventName; + ReplyPromise< Standalone > reply; + + EventLogRequest() : getLastError(true) {} + explicit EventLogRequest( Standalone eventName ) : eventName( eventName ), getLastError( false ) {} + + template + void serialize(Ar& ar) { + ar & getLastError & eventName & reply; + } +}; + +struct DebugQueryRequest { + Standalone search; + ReplyPromise< Standalone< VectorRef > > reply; + + template + void serialize(Ar& ar) { + ar & search & reply; + } +}; + +struct DebugEntryRef { + double time; + NetworkAddress address; + StringRef context; + Version version; + MutationRef mutation; + DebugEntryRef() {} + DebugEntryRef( const char* c, Version v, MutationRef const& m ) : context((const uint8_t*)c,strlen(c)), version(v), mutation(m), time(now()), address( g_network->getLocalAddress() ) {} + DebugEntryRef( Arena& a, DebugEntryRef const& d ) : time(d.time), address(d.address), context(d.context), version(d.version), mutation(a, d.mutation) {} + + size_t expectedSize() const { + return context.expectedSize() + mutation.expectedSize(); + } + + template + void serialize(Ar& ar) { + ar & time & address & context & version & mutation; + } +}; + +struct DiskStoreRequest { + bool includePartialStores; + ReplyPromise>> reply; + + DiskStoreRequest(bool includePartialStores=false) : includePartialStores(includePartialStores) {} + + template + void serialize(Ar& ar) { + ar & includePartialStores & reply; + } +}; + +void startRole(UID roleId, UID workerId, std::string as, std::map details = std::map(), std::string origination = "Recruited"); +void endRole(UID id, std::string as, std::string reason, bool ok = true, Error e = Error()); + +struct ServerDBInfo; + +class Database openDBOnServer( Reference> const& db, int taskID = TaskDefaultEndpoint, bool enableLocalityLoadBalance = true, bool lockAware = false ); +Future extractClusterInterface( Reference>> const& a, Reference>> const& b ); + +Future fdbd( Reference const&, LocalityData const& localities, ProcessClass const& processClass, std::string const& dataFolder, std::string const& coordFolder, int64_t const& memoryLimit, std::string const& metricsConnFile, std::string const& metricsPrefix ); +Future workerServer( Reference const&, Reference>> const& ccInterface, LocalityData const& localities, ProcessClass const& processClass, std::string const& filename, int64_t const& memoryLimit, Future const& forceFailure, std::string const& metricsConnFile, std::string const& metricsPrefix ); +Future clusterController( Reference const&, Reference>> const& currentCC ); + +// These servers are started by workerServer +Future storageServer( + class IKeyValueStore* const& persistentData, + StorageServerInterface const& ssi, + Tag const& seedTag, + ReplyPromise const& recruitReply, + Reference> const& db, + std::string const& folder ); +Future storageServer( + class IKeyValueStore* const& persistentData, + StorageServerInterface const& ssi, + Reference> const& db, + std::string const& folder ); // changes pssi->id() to be the recovered ID +Future masterServer( MasterInterface const& mi, Reference> const& db, class ServerCoordinators const&, LifetimeToken const& lifetime ); +Future masterProxyServer(MasterProxyInterface const& proxy, InitializeMasterProxyRequest const& req, Reference> const& db); +Future tLog( class IKeyValueStore* const& persistentData, class IDiskQueue* const& persistentQueue, Reference> const& db, LocalityData const& locality, PromiseStream const& tlogRequests, UID const& tlogId, bool const& restoreFromDisk, Promise const& oldLog ); // changes tli->id() to be the recovered ID +Future debugQueryServer( DebugQueryRequest const& req ); +Future monitorServerDBInfo( Reference>> const& ccInterface, Reference const&, LocalityData const&, Reference> const& dbInfo ); +Future resolver( ResolverInterface const& proxy, InitializeResolverRequest const&, Reference> const& db ); +Future runMetrics( Future const& fcx, Key const& metricsPrefix ); + +void registerThreadForProfiling(); +void updateCpuProfiler(ProfilerRequest req); + +namespace oldTLog { + Future tLog( IKeyValueStore* const& persistentData, IDiskQueue* const& persistentQueue, TLogInterface const& tli, Reference> const& db ); +} + +#endif diff --git a/fdbserver/fdbserver.actor.cpp b/fdbserver/fdbserver.actor.cpp new file mode 100644 index 0000000000..811890eede --- /dev/null +++ b/fdbserver/fdbserver.actor.cpp @@ -0,0 +1,1730 @@ +/* + * fdbserver.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "fdbrpc/simulator.h" +#include "flow/DeterministicRandom.h" +#include "fdbrpc/PerfMetric.h" +#include "flow/Platform.h" +#include "flow/SystemMonitor.h" +#include "fdbclient/NativeAPI.h" +#include "fdbclient/SystemData.h" +#include "fdbclient/FailureMonitorClient.h" +#include "CoordinationInterface.h" +#include "WorkerInterface.h" +#include "ClusterRecruitmentInterface.h" +#include "ServerDBInfo.h" +#include "MoveKeys.h" +#include "ConflictSet.h" +#include "DataDistribution.h" +#include "NetworkTest.h" +#include +#include +#include "pubsub.h" +#ifdef _WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef min +#undef max +#endif +#include "SimulatedCluster.h" +#include "TesterInterface.h" +#include "workloads/workloads.h" +#include +#include "Status.h" +#include "fdbrpc/TLSConnection.h" +#include "fdbrpc/Net2FileSystem.h" +#include "fdbrpc/Platform.h" +#include "CoroFlow.h" +#include "flow/SignalSafeUnwind.h" + +#define BOOST_DATE_TIME_NO_LIB +#include + +#ifdef __linux__ +#include +#include +#ifdef ALLOC_INSTRUMENTATION +#include +#endif +#endif + +#ifndef WIN32 +#include "versions.h" +#endif + +#include "flow/SimpleOpt.h" + +enum { + OPT_CONNFILE, OPT_SEEDCONNFILE, OPT_SEEDCONNSTRING, OPT_ROLE, OPT_LISTEN, OPT_PUBLICADDR, OPT_DATAFOLDER, OPT_LOGFOLDER, OPT_PARENTPID, OPT_NEWCONSOLE, OPT_NOBOX, OPT_TESTFILE, OPT_RESTARTING, OPT_RANDOMSEED, OPT_KEY, OPT_MEMLIMIT, OPT_STORAGEMEMLIMIT, OPT_MACHINEID, OPT_DCID, OPT_MACHINE_CLASS, OPT_BUGGIFY, OPT_VERSION, OPT_CRASHONERROR, OPT_HELP, OPT_NETWORKIMPL, OPT_NOBUFSTDOUT, OPT_BUFSTDOUTERR, OPT_TRACECLOCK, OPT_NUMTESTERS, OPT_DEVHELP, OPT_ROLLSIZE, OPT_MAXLOGS, OPT_MAXLOGSSIZE, OPT_KNOB, OPT_TESTSERVERS, OPT_TEST_ON_SERVERS, OPT_METRICSCONNFILE, OPT_METRICSPREFIX, + OPT_LOGGROUP, OPT_LOCALITY, OPT_IO_TRUST_SECONDS, OPT_IO_TRUST_WARN_ONLY, OPT_FILESYSTEM }; + +CSimpleOpt::SOption g_rgOptions[] = { + { OPT_CONNFILE, "-C", SO_REQ_SEP }, + { OPT_CONNFILE, "--cluster_file", SO_REQ_SEP }, + { OPT_SEEDCONNFILE, "--seed_cluster_file", SO_REQ_SEP }, + { OPT_SEEDCONNSTRING, "--seed_connection_string", SO_REQ_SEP }, + { OPT_ROLE, "-r", SO_REQ_SEP }, + { OPT_ROLE, "--role", SO_REQ_SEP }, + { OPT_PUBLICADDR, "-p", SO_REQ_SEP }, + { OPT_PUBLICADDR, "--public_address", SO_REQ_SEP }, + { OPT_LISTEN, "-l", SO_REQ_SEP }, + { OPT_LISTEN, "--listen_address", SO_REQ_SEP }, +#ifdef __linux__ + { OPT_FILESYSTEM, "--data_filesystem", SO_REQ_SEP }, +#endif + { OPT_DATAFOLDER, "-d", SO_REQ_SEP }, + { OPT_DATAFOLDER, "--datadir", SO_REQ_SEP }, + { OPT_LOGFOLDER, "-L", SO_REQ_SEP }, + { OPT_LOGFOLDER, "--logdir", SO_REQ_SEP }, + { OPT_ROLLSIZE, "-Rs", SO_REQ_SEP }, + { OPT_ROLLSIZE, "--logsize", SO_REQ_SEP }, + { OPT_MAXLOGS, "--maxlogs", SO_REQ_SEP }, + { OPT_MAXLOGSSIZE, "--maxlogssize", SO_REQ_SEP }, + { OPT_LOGGROUP, "--loggroup", SO_REQ_SEP }, +#ifdef _WIN32 + { OPT_PARENTPID, "--parentpid", SO_REQ_SEP }, + { OPT_NEWCONSOLE, "-n", SO_NONE }, + { OPT_NEWCONSOLE, "--newconsole", SO_NONE }, + { OPT_NOBOX, "-q", SO_NONE }, + { OPT_NOBOX, "--no_dialog", SO_NONE }, +#endif + { OPT_TESTFILE, "-f", SO_REQ_SEP }, + { OPT_TESTFILE, "--testfile", SO_REQ_SEP }, + { OPT_RESTARTING, "-R", SO_NONE }, + { OPT_RESTARTING, "--restarting", SO_NONE }, + { OPT_RANDOMSEED, "-s", SO_REQ_SEP }, + { OPT_RANDOMSEED, "--seed", SO_REQ_SEP }, + { OPT_KEY, "-k", SO_REQ_SEP }, + { OPT_KEY, "--key", SO_REQ_SEP }, + { OPT_MEMLIMIT, "-m", SO_REQ_SEP }, + { OPT_MEMLIMIT, "--memory", SO_REQ_SEP }, + { OPT_STORAGEMEMLIMIT, "-M", SO_REQ_SEP }, + { OPT_STORAGEMEMLIMIT, "--storage_memory", SO_REQ_SEP }, + { OPT_MACHINEID, "-i", SO_REQ_SEP }, + { OPT_MACHINEID, "--machine_id", SO_REQ_SEP }, + { OPT_DCID, "-a", SO_REQ_SEP }, + { OPT_DCID, "--datacenter_id", SO_REQ_SEP }, + { OPT_MACHINE_CLASS, "-c", SO_REQ_SEP }, + { OPT_MACHINE_CLASS, "--class", SO_REQ_SEP }, + { OPT_BUGGIFY, "-b", SO_REQ_SEP }, + { OPT_BUGGIFY, "--buggify", SO_REQ_SEP }, + { OPT_VERSION, "-v", SO_NONE }, + { OPT_VERSION, "--version", SO_NONE }, + { OPT_CRASHONERROR, "--crash", SO_NONE }, + { OPT_NETWORKIMPL, "-N", SO_REQ_SEP }, + { OPT_NETWORKIMPL, "--network", SO_REQ_SEP }, + { OPT_NOBUFSTDOUT, "--unbufferedout", SO_NONE }, + { OPT_BUFSTDOUTERR, "--bufferedout", SO_NONE }, + { OPT_TRACECLOCK, "--traceclock", SO_REQ_SEP }, + { OPT_NUMTESTERS, "--num_testers", SO_REQ_SEP }, + { OPT_HELP, "-?", SO_NONE }, + { OPT_HELP, "-h", SO_NONE }, + { OPT_HELP, "--help", SO_NONE }, + { OPT_DEVHELP, "--dev-help", SO_NONE }, + { OPT_KNOB, "--knob_", SO_REQ_SEP }, + { OPT_LOCALITY, "--locality_", SO_REQ_SEP }, + { OPT_TESTSERVERS, "--testservers", SO_REQ_SEP }, + { OPT_TEST_ON_SERVERS, "--testonservers", SO_NONE }, + { OPT_METRICSCONNFILE, "--metrics_cluster", SO_REQ_SEP }, + { OPT_METRICSPREFIX, "--metrics_prefix", SO_REQ_SEP }, + { OPT_IO_TRUST_SECONDS, "--io_trust_seconds", SO_REQ_SEP }, + { OPT_IO_TRUST_WARN_ONLY, "--io_trust_warn_only", SO_NONE }, + + TLS_OPTION_FLAGS + + SO_END_OF_OPTIONS +}; + +GlobalCounters g_counters; + +extern void dsltest(); +extern void pingtest(); +extern void copyTest(); +extern void versionedMapTest(); +extern void createTemplateDatabase(); +// FIXME: this really belongs in a header somewhere since it is actually used. +extern uint32_t determinePublicIPAutomatically( ClusterConnectionString const& ccs ); + +extern const char* getHGVersion(); + +extern IRandom* trace_random; +extern void flushTraceFileVoid(); + +extern bool noUnseed; +extern const int MAX_CLUSTER_FILE_BYTES; + +#ifdef ALLOC_INSTRUMENTATION +extern uint8_t *g_extra_memory; +#endif + +bool enableFailures = true; + +#define test_assert(x) if (!(x)) { cout << "Test failed: " #x << endl; return false; } + +template vector vec( X x ) { vector v; v.push_back(x); return v; } +template vector vec( X x, X y ) { vector v; v.push_back(x); v.push_back(y); return v; } +template vector vec( X x, X y, X z ) { vector v; v.push_back(x); v.push_back(y); v.push_back(z); return v; } + +//KeyRange keyRange( const Key& a, const Key& b ) { return std::make_pair(a,b); } + +vector< Standalone> > debugEntries; +int64_t totalDebugEntriesSize = 0; + +#if CENABLED(0, NOT_IN_CLEAN) +StringRef debugKey = LiteralStringRef( "" ); +StringRef debugKey2 = LiteralStringRef( "\xff\xff\xff\xff" ); + +bool debugMutation( const char* context, Version version, MutationRef const& mutation ) { + if ((mutation.type == mutation.SetValue || mutation.type == mutation.AddValue || mutation.type==mutation.DebugKey) && (mutation.param1 == debugKey || mutation.param1 == debugKey2)) + ;//TraceEvent("MutationTracking").detail("At", context).detail("Version", version).detail("MutationType", "SetValue").detail("Key", printable(mutation.param1)).detail("Value", printable(mutation.param2)); + else if ((mutation.type == mutation.ClearRange || mutation.type == mutation.DebugKeyRange) && ((mutation.param1<=debugKey && mutation.param2>debugKey) || (mutation.param1<=debugKey2 && mutation.param2>debugKey2))) + ;//TraceEvent("MutationTracking").detail("At", context).detail("Version", version).detail("MutationType", "ClearRange").detail("KeyBegin", printable(mutation.param1)).detail("KeyEnd", printable(mutation.param2)); + else + return false; + const char* type = + mutation.type == MutationRef::SetValue ? "SetValue" : + mutation.type == MutationRef::ClearRange ? "ClearRange" : + mutation.type == MutationRef::AddValue ? "AddValue" : + mutation.type == MutationRef::DebugKeyRange ? "DebugKeyRange" : + mutation.type == MutationRef::DebugKey ? "DebugKey" : + "UnknownMutation"; + printf("DEBUGMUTATION:\t%.6f\t%s\t%s\t%lld\t%s\t%s\t%s\n", now(), g_network->getLocalAddress().toString().c_str(), context, version, type, printable(mutation.param1).c_str(), printable(mutation.param2).c_str()); + + return true; +} + +bool debugKeyRange( const char* context, Version version, KeyRangeRef const& keys ) { + if (keys.contains(debugKey) || keys.contains(debugKey2)) { + debugMutation(context, version, MutationRef(MutationRef::DebugKeyRange, keys.begin, keys.end) ); + //TraceEvent("MutationTracking").detail("At", context).detail("Version", version).detail("KeyBegin", printable(keys.begin)).detail("KeyEnd", printable(keys.end)); + return true; + } else + return false; +} + +#elif CENABLED(0, NOT_IN_CLEAN) +bool debugMutation( const char* context, Version version, MutationRef const& mutation ) { + if (!debugEntries.size() || debugEntries.back().size() >= 1000) { + if (debugEntries.size()) totalDebugEntriesSize += debugEntries.back().arena().getSize() + sizeof(debugEntries.back()); + debugEntries.push_back(Standalone>()); + TraceEvent("DebugMutationBuffer").detail("Bytes", totalDebugEntriesSize); + } + auto& v = debugEntries.back(); + v.push_back_deep( v.arena(), DebugEntryRef(context, version, mutation) ); + + return false; // No auxiliary logging +} + +bool debugKeyRange( const char* context, Version version, KeyRangeRef const& keys ) { + return debugMutation( context, version, MutationRef(MutationRef::DebugKeyRange, keys.begin, keys.end) ); +} + +#else // Default implementation. +bool debugMutation( const char* context, Version version, MutationRef const& mutation ) { return false; } +bool debugKeyRange( const char* context, Version version, KeyRangeRef const& keys ) { return false; } +#endif + +Future debugQueryServer( DebugQueryRequest const& req ) { + Standalone> reply; + + for(auto v = debugEntries.begin(); v != debugEntries.end(); ++v) + for(auto m = v->begin(); m != v->end(); ++m) { + if (m->mutation.type == m->mutation.ClearRange || m->mutation.type == m->mutation.DebugKeyRange) { + if (!KeyRangeRef(m->mutation.param1, m->mutation.param2).contains( req.search )) + continue; + } else if (m->mutation.type == m->mutation.SetValue) { + if (m->mutation.param1 != req.search) + continue; + } + reply.push_back( reply.arena(), *m ); + } + + req.reply.send(reply); + return Void(); +} + +auto sortByTime = [](DebugEntryRef const& a, DebugEntryRef const& b) { return a.time < b.time; }; + +/*ACTOR Future debugSearchMutationCluster( ZookeeperInterface zk, Key key ) { + state ZKWatch ccWatch(zk, LiteralStringRef("ClusterController")); + state ClusterControllerFullInterface cc = wait( ccWatch.get() ); + + ASSERT( ccWatch.getLastVersion() ); + + Optional> workerList = wait( cc.getWorkers.tryGetReply( GetWorkersRequest() ) ); + if( !workerList.present() ) { + printf("ERROR: CC interface not in ZK\n"); + return Void(); + } + state vector workers = workerList.get(); + + state vector>>> replies( workers.size() ); + for(int w=0; w>> result = wait( getAll( replies ) ); + Void _ = wait(waitForAllReady( replies )); + state vector>> result( workers.size() ); + for(int r=0; r> all; + for(int r=0; rmutation.type == MutationRef::SetValue ? "SetValue" : + e->mutation.type == MutationRef::ClearRange ? "ClearRange" : + e->mutation.type == MutationRef::DebugKeyRange ? "DebugKeyRange" : + "UnknownMutation"; + printf("%.6f\t%s\t%s\t%lld\t%s\t%s\t%s\n", e->time, e->address.toString().c_str(), e->context.toString().c_str(), e->version, type, printable(e->mutation.param1).c_str(), printable(e->mutation.param2).c_str()); + } + printf("\n\n"); + + return Void(); +}*/ + +#ifdef _WIN32 +#include + +// It is your +// responsibility to properly initialize the +// structure and to free the structure's +// lpSecurityDescriptor member when you have +// finished using it. To free the structure's +// lpSecurityDescriptor member, call the +// LocalFree function. +BOOL CreatePermissiveReadWriteDACL(SECURITY_ATTRIBUTES * pSA) +{ + UNSTOPPABLE_ASSERT( pSA != NULL ); + + TCHAR * szSD = TEXT("D:") // Discretionary ACL + TEXT("(A;OICI;GR;;;AU)") // Allow read/write/execute to authenticated users + TEXT("(A;OICI;GA;;;BA)"); // Allow full control to administrators + + return ConvertStringSecurityDescriptorToSecurityDescriptor( + szSD, + SDDL_REVISION_1, + &(pSA->lpSecurityDescriptor), + NULL); +} +#endif + +class WorldReadablePermissions { +public: + WorldReadablePermissions() { +#ifdef _WIN32 + sa.nLength = sizeof(SECURITY_ATTRIBUTES); + sa.bInheritHandle = FALSE; + if( !CreatePermissiveReadWriteDACL(&sa) ) { + TraceEvent("Win32DACLCreationFail").GetLastError(); + throw platform_error(); + } + permission.set_permissions( &sa ); +#elif (defined(__linux__) || defined(__APPLE__)) + // There is nothing to do here, since the default permissions are fine +#else + #error Port me! +#endif + } + + virtual ~WorldReadablePermissions() { +#ifdef _WIN32 + LocalFree( sa.lpSecurityDescriptor ); +#elif (defined(__linux__) || defined(__APPLE__)) + // There is nothing to do here, since the default permissions are fine +#else + #error Port me! +#endif + } + + boost::interprocess::permissions permission; + +private: + WorldReadablePermissions(const WorldReadablePermissions &rhs) {} +#ifdef _WIN32 + SECURITY_ATTRIBUTES sa; +#endif +}; + +UID getSharedMemoryMachineId() { + UID *machineId = NULL; + int numTries = 0; + + // Permissions object defaults to 0644 on *nix, but on windows defaults to allowing access to only the creator. + // On windows, this means that we have to create an elaborate workaround for DACLs + WorldReadablePermissions p; + + loop { + try { + // "0" is the default parameter "addr" + boost::interprocess::managed_shared_memory segment(boost::interprocess::open_or_create, "fdbserver", 1000, 0, p.permission); + machineId = segment.find_or_construct("machineId")(g_random->randomUniqueID()); + if (!machineId) + criticalError(FDB_EXIT_ERROR, "SharedMemoryError", "Could not locate or create shared memory - 'machineId'"); + return *machineId; + } + catch (boost::interprocess::interprocess_exception &) { + try { + //If the shared memory already exists, open it read-only in case it was created by another user + boost::interprocess::managed_shared_memory segment(boost::interprocess::open_read_only, "fdbserver"); + machineId = segment.find("machineId").first; + if (!machineId) + criticalError(FDB_EXIT_ERROR, "SharedMemoryError", "Could not locate shared memory - 'machineId'"); + return *machineId; + } + catch (boost::interprocess::interprocess_exception &ex) { + //Retry in case the shared memory was deleted in between the call to open_or_create and open_read_only + //Don't keep trying forever in case this is caused by some other problem + if (++numTries == 10) + criticalError(FDB_EXIT_ERROR, "SharedMemoryError", format("Could not open shared memory - %s", ex.what()).c_str()); + } + } + } +} + + +ACTOR void failAfter( Future trigger, ISimulator::ProcessInfo* m = g_simulator.getCurrentProcess() ) { + Void _ = wait( trigger ); + if (enableFailures) { + printf("Killing machine: %s at %f\n", m->address.toString().c_str(), now()); + g_simulator.killProcess( m, ISimulator::KillInstantly ); + } +} + +void failAfter( Future trigger, Endpoint e ) { + if (g_network == &g_simulator) + failAfter( trigger, g_simulator.getProcess( e ) ); +} + +void testSerializationSpeed() { + double tstart; + double build = 0, serialize = 0, deserialize = 0, copy = 0, deallocate = 0; + double bytes = 0; + double testBegin = timer(); + for(int a=0; a<10000; a++) { + { + tstart = timer(); + + Arena batchArena; + VectorRef< CommitTransactionRef > batch; + batch.resize( batchArena, 1000 ); + for(int t=0; t batch2; + rd >> arena >> batch2; + + deserialize += timer() - tstart; + } + + tstart = timer(); + } + deallocate += timer() - tstart; + } + double elapsed = (timer()-testBegin); + printf("Test speed: %0.1f MB/sec (%0.0f/sec)\n", bytes/1e6/elapsed, 1000000/elapsed); + printf(" Build: %0.1f MB/sec\n", bytes/1e6/build); + printf(" Serialize: %0.1f MB/sec\n", bytes/1e6/serialize); + printf(" Copy: %0.1f MB/sec\n", bytes/1e6/copy); + printf(" Deserialize: %0.1f MB/sec\n", bytes/1e6/deserialize); + printf(" Deallocate: %0.1f MB/sec\n", bytes/1e6/deallocate); + printf(" Bytes: %0.1f MB\n", bytes/1e6); + printf("\n"); +} + +std::string toHTML( const StringRef& binaryString ) { + std::string s; + + for(int i=0; i') s += ">"; + else if (c == '&') s += "&"; + else if (c == '"') s += """; + else if (c == ' ') s += " "; + else if (c > 32 && c < 127) s += c; + else s += format("[%02x]", c); + } + + return s; +} + +ACTOR Future dumpDatabase( Database cx, std::string outputFilename, KeyRange range = allKeys ) { + try { + state Transaction tr( cx ); + loop { + state FILE* output = fopen(outputFilename.c_str(), "wt"); + try { + state KeySelectorRef iter = firstGreaterOrEqual( range.begin ); + state Arena arena; + fprintf(output, "\n"); + Version ver = wait( tr.getReadVersion() ); + fprintf(output, "

    Database version: %lld

    ", ver); + + loop { + Standalone results = wait( + tr.getRange( iter, firstGreaterOrEqual( range.end ), 1000 ) ); + for(int r=0; r%s := %s

    \n", key.c_str(), value.c_str() ); + } + if (results.size() < 1000) break; + iter = firstGreaterThan( KeyRef(arena, results[ results.size()-1 ].key) ); + } + fprintf(output, ""); + fclose(output); + TraceEvent("DatabaseDumped").detail("Filename", outputFilename); + return Void(); + } catch (Error& e) { + fclose(output); + Void _ = wait( tr.onError(e) ); + } + } + } catch (Error& e) { + TraceEvent(SevError,"dumpDatabaseError").error(e).detail("Filename", outputFilename); + throw; + } +} + +void memoryTest(); +void skipListTest(); + +Future startSystemMonitor(std::string dataFolder, Optional> zoneId, Optional> machineId) { + initializeSystemMonitorMachineState(SystemMonitorMachineState(dataFolder, zoneId, machineId, g_network->getLocalAddress().ip)); + + systemMonitor(); + return recurring( &systemMonitor, 5.0, TaskFlushTrace ); +} + +void testIndexedSet(); + +#ifdef _WIN32 +void parentWatcher(void *parentHandle) { + HANDLE parent = (HANDLE)parentHandle; + int signal = WaitForSingleObject( parent, INFINITE ); + CloseHandle( parentHandle ); + if( signal == WAIT_OBJECT_0 ) + criticalError( FDB_EXIT_SUCCESS, "ParentProcessExited", "Parent process exited" ); + TraceEvent(SevError, "ParentProcessWaitFailed").detail("RetCode", signal).GetLastError(); +} +#endif + +static void printVersion() { + printf("FoundationDB " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + printf("source version %s\n", getHGVersion()); + printf("protocol %llx\n", currentProtocolVersion); +} + +static void printHelpTeaser( const char *name ) { + fprintf(stderr, "Try `%s --help' for more information.\n", name); +} + +static void printUsage( const char *name, bool devhelp ) { + printf("FoundationDB " FDB_VT_PACKAGE_NAME " (v" FDB_VT_VERSION ")\n"); + printf("Usage: %s -p ADDRESS [OPTIONS]\n\n", name); + printf(" -p ADDRESS, --public_address ADDRESS\n" + " Public address, specified as `IP_ADDRESS:PORT' or `auto:PORT'.\n"); + printf(" -l ADDRESS, --listen_address ADDRESS\n" + " Listen address, specified as `IP_ADDRESS:PORT' (defaults to\n"); + printf(" public address).\n"); + printf(" -C CONNFILE, --cluster_file CONNFILE\n" + " The path of a file containing the connection string for the\n" + " FoundationDB cluster. The default is first the value of the\n" + " FDB_CLUSTER_FILE environment variable, then `./fdb.cluster',\n" + " then `%s'.\n", platform::getDefaultClusterFilePath().c_str()); + printf(" --seed_cluster_file SEEDCONNFILE\n" + " The path of a seed cluster file which will be used to connect\n" + " if the -C cluster file does not exist. If the server connects\n" + " successfully using the seed file, then it copies the file to\n" + " the -C file location.\n"); + printf(" --seed_connection_string SEEDCONNSTRING\n" + " The path of a seed connection string which will be used to connect\n" + " if the -C cluster file does not exist. If the server connects\n" + " successfully using the seed string, then it copies the string to\n" + " the -C file location.\n"); +#ifdef __linux__ + printf(" --data_filesystem PATH\n" + " Turns on validation that all data files are written to a drive\n" + " mounted at the specified PATH. This checks that the device at PATH\n" + " is currently mounted and that any data files get written to the\n" + " same device.\n"); +#endif + printf(" -d PATH, --datadir PATH\n" + " Store data files in the given folder (must be unique for each\n"); + printf(" fdbserver instance on a given machine).\n"); + printf(" -L PATH, --logdir PATH\n" + " Store log files in the given folder (default is `.').\n"); + printf(" --logsize SIZE Roll over to a new log file after the current log file\n" + " exceeds SIZE bytes. The default value is 10MiB.\n"); + printf(" --maxlogs SIZE, --maxlogssize SIZE\n" + " Delete the oldest log file when the total size of all log\n" + " files exceeds SIZE bytes. If set to 0, old log files will not\n" + " be deleted. The default value is 100MiB.\n"); + printf(" -i ID, --machine_id ID\n" + " Machine identifier key (up to 16 hex characters). Defaults\n" + " to a random value shared by all fdbserver processes on this\n" + " machine.\n"); + printf(" -a ID, --datacenter_id ID\n" + " Data center identifier key (up to 16 hex characters).\n"); + printf(" -c CLASS, --class CLASS\n" + " Machine class (valid options are storage, transaction,\n"); + printf(" resolution, proxy, master, test, unset, stateless, log).\n"); + printf(TLS_HELP); + printf(" -v, --version Print version information and exit.\n"); + printf(" -h, -?, --help Display this help and exit.\n"); + if( devhelp ) { + printf(" -r ROLE, --role ROLE\n" + " Server role (valid options are fdbd, test, multitest,\n"); + printf(" simulation, networktestclient, networktestserver,\n"); + printf(" consistencycheck). The default is `fdbd'.\n"); +#ifdef _WIN32 + printf(" -n, --newconsole\n" + " Create a new console.\n"); + printf(" -q, --no_dialog\n" + " Disable error dialog on crash.\n"); + printf(" --parentpid PID\n"); + printf(" Specify a process after whose termination to exit.\n"); +#endif + printf(" -f TESTFILE, --testfile\n" + " Testfile to run, defaults to `tests/default.txt'.\n"); + printf(" -R, --restarting\n"); + printf(" Restart a previous simulation that was cleanly shut down.\n"); + printf(" -s SEED, --seed SEED\n" + " Random seed.\n"); + printf(" -k KEY, --key KEY Target key for search role.\n"); + printf(" -m SIZE, --memory SIZE\n" + " Memory limit. The default value is 8GiB. When specified\n" + " without a unit, MiB is assumed.\n"); + printf(" -M SIZE, --storage_memory SIZE\n" + " Maximum amount of memory used for storage. The default\n" + " value is 1GiB. When specified without a unit, MB is\n" + " assumed.\n"); + printf(" -b [on,off], --buggify [on,off]\n" + " Sets Buggify system state, defaults to `off'.\n"); + printf(" --crash Crash on serious errors instead of continuing.\n"); + printf(" -N NETWORKIMPL, --network NETWORKIMPL\n" + " Select network implementation, `net2' (default),\n"); + printf(" `net2-threadpool'.\n"); + printf(" --unbufferedout\n"); + printf(" Do not buffer stdout and stderr.\n"); + printf(" --bufferedout\n"); + printf(" Buffer stdout and stderr.\n"); + printf(" --traceclock CLOCKIMPL\n"); + printf(" Select clock source for trace files, `now' (default) or\n"); + printf(" `realtime'.\n"); + printf(" --num_testers NUM\n"); + printf(" A multitester will wait for NUM testers before starting\n"); + printf(" (defaults to 1).\n"); + printf(" --testservers ADDRESSES\n"); + printf(" The addresses of networktestservers\n"); + printf(" specified as ADDRESS:PORT,ADDRESS:PORT...\n"); + printf(" --testonservers\n"); + printf(" Testers are recruited on servers.\n"); + printf(" --metrics_cluster CONNFILE\n"); + printf(" The cluster file designating where this process will\n"); + printf(" store its metric data. By default metrics will be stored\n"); + printf(" in the same database the process is participating in.\n"); + printf(" --metrics_prefix PREFIX\n"); + printf(" The prefix where this process will store its metric data.\n"); + printf(" Must be specified if using a different database for metrics.\n"); + printf(" --knob_KNOBNAME KNOBVALUE\n"); + printf(" Changes a database knob. KNOBNAME should be lowercase.\n"); + printf(" --locality_LOCALITYKEY LOCALITYVALUE\n"); + printf(" Define a locality key. LOCALITYKEY is case-insensitive though LOCALITYVALUE is not.\n"); + printf(" --io_trust_seconds SECONDS\n"); + printf(" Sets the time in seconds that a read or write operation is allowed to take\n" + " before timing out with an error. If an operation times out, all future\n" + " operations on that file will fail with an error as well. Only has an effect\n" + " when using AsyncFileKAIO in Linux.\n"); + printf(" --io_trust_warn_only\n"); + printf(" Instead of failing when an I/O operation exceeds io_trust_seconds, just\n" + " log a warning to the trace log. Has no effect if io_trust_seconds is unspecified.\n"); + } else { + printf(" --dev-help Display developer-specific help and exit.\n"); + } + + printf("\n" + "SIZE parameters may use one of the multiplicative suffixes B=1, KB=10^3,\n" + "KiB=2^10, MB=10^6, MiB=2^20, GB=10^9, GiB=2^30, TB=10^12, or TiB=2^40.\n"); +} + +Optional parse_with_suffix(std::string toparse, std::string default_unit = "") { + char *endptr; + + uint64_t ret = strtoull(toparse.c_str(), &endptr, 10); + + if (endptr == toparse.c_str()) { + return Optional(); + } + + std::string unit; + + if (*endptr == '\0') { + if (!default_unit.empty()) { + unit = default_unit; + } else { + return Optional(); + } + } else { + unit = endptr; + } + + if (!unit.compare("B")) { + // Nothing to do + } else if (!unit.compare("KB")) { + ret *= int64_t(1e3); + } else if (!unit.compare("KiB")) { + ret *= 1LL << 10; + } else if (!unit.compare("MB")) { + ret *= int64_t(1e6); + } else if (!unit.compare("MiB")) { + ret *= 1LL << 20; + } else if (!unit.compare("GB")) { + ret *= int64_t(1e9); + } else if (!unit.compare("GiB")) { + ret *= 1LL << 30; + } else if (!unit.compare("TB")) { + ret *= int64_t(1e12); + } else if (!unit.compare("TiB")) { + ret *= 1LL << 40; + } else { + return Optional(); + } + + return ret; +} + +extern bool g_crashOnError; + +#if defined(ALLOC_INSTRUMENTATION) || defined(ALLOC_INSTRUMENTATION_STDOUT) + void* operator new (std::size_t size) throw(std::bad_alloc) { + void* p = malloc(size); + if(!p) + throw std::bad_alloc(); + recordAllocation( p, size ); + return p; + } + void operator delete (void* ptr) throw() { + recordDeallocation( ptr ); + free( ptr ); + } + + //scalar, nothrow new and it matching delete + void* operator new (std::size_t size,const std::nothrow_t&) throw() { + void* p = malloc(size); + recordAllocation( p, size ); + return p; + } + void operator delete (void* ptr, const std::nothrow_t&) throw() { + recordDeallocation( ptr ); + free( ptr ); + } + + //array throwing new and matching delete[] + void* operator new [](std::size_t size) throw(std::bad_alloc) { + void* p = malloc(size); + if(!p) + throw std::bad_alloc(); + recordAllocation( p, size ); + return p; + } + void operator delete[](void* ptr) throw() { + recordDeallocation( ptr ); + free( ptr ); + } + + //array, nothrow new and matching delete[] + void* operator new [](std::size_t size, const std::nothrow_t&) throw() { + void* p = malloc(size); + recordAllocation( p, size ); + return p; + } + void operator delete[](void* ptr, const std::nothrow_t&) throw() { + recordDeallocation( ptr ); + free( ptr ); + } +#endif + +int main(int argc, char* argv[]) { + try { + platformInit(); + initSignalSafeUnwind(); + +#ifdef ALLOC_INSTRUMENTATION + g_extra_memory = new uint8_t[1000000]; +#endif + registerCrashHandler(); + + // Set default of line buffering standard out and error + setvbuf(stdout, NULL, _IOLBF, BUFSIZ); + setvbuf(stderr, NULL, _IOLBF, BUFSIZ); + + //Enables profiling on this thread (but does not start it) + registerThreadForProfiling(); + + std::string commandLine; + for (int a = 0; a> zoneId; + Optional> dcId; + ProcessClass processClass = ProcessClass( ProcessClass::UnsetClass, ProcessClass::CommandLineSource ); + bool useNet2 = true; + bool useThreadPool = false; + uint64_t rollsize = TRACE_DEFAULT_ROLL_SIZE; + uint64_t maxLogsSize = TRACE_DEFAULT_MAX_LOGS_SIZE; + bool maxLogsSizeSet = false; + int maxLogs = 0; + bool maxLogsSet = false; + std::vector> knobs; + LocalityData localities; + int minTesterCount = 1; + bool testOnServers = false; + + Reference tlsOptions = Reference( new TLSOptions ); + std::string tlsCertPath, tlsKeyPath, tlsVerifyPeers; + double fileIoTimeout = 0.0; + bool fileIoWarnOnly = false; + + if( argc == 1 ) { + printUsage(argv[0], false); + flushAndExit(FDB_EXIT_ERROR); + } + + #ifdef _WIN32 + // Windows needs a gentle nudge to format floats correctly + //_set_output_format(_TWO_DIGIT_EXPONENT); + #endif + + while (args.Next()) { + if (args.LastError() == SO_ARG_INVALID_DATA) { + fprintf(stderr, "ERROR: invalid argument to option `%s'\n", args.OptionText()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + if (args.LastError() == SO_ARG_INVALID) { + fprintf(stderr, "ERROR: argument given for option `%s'\n", args.OptionText()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + if (args.LastError() == SO_ARG_MISSING) { + fprintf(stderr, "ERROR: missing argument for option `%s'\n", args.OptionText()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + if (args.LastError() == SO_OPT_INVALID) { + fprintf(stderr, "ERROR: unknown option: `%s'\n", args.OptionText()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + if (args.LastError() != SO_SUCCESS) { + fprintf(stderr, "ERROR: error parsing options\n"); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + const char *sRole; + Optional ti; + + switch (args.OptionId()) { + case OPT_HELP: + printUsage(argv[0], false); + flushAndExit(FDB_EXIT_SUCCESS); + break; + case OPT_DEVHELP: + printUsage(argv[0], true); + flushAndExit(FDB_EXIT_SUCCESS); + break; + case OPT_KNOB: { + std::string syn = args.OptionSyntax(); + if (!StringRef(syn).startsWith(LiteralStringRef("--knob_"))) { + fprintf(stderr, "ERROR: unable to parse knob option '%s'\n", syn.c_str()); + flushAndExit(FDB_EXIT_ERROR); + } + syn = syn.substr(7); + knobs.push_back( std::make_pair( syn, args.OptionArg() ) ); + break; + } + case OPT_LOCALITY: { + std::string syn = args.OptionSyntax(); + if (!StringRef(syn).startsWith(LiteralStringRef("--locality_"))) { + fprintf(stderr, "ERROR: unable to parse locality key '%s'\n", syn.c_str()); + flushAndExit(FDB_EXIT_ERROR); + } + syn = syn.substr(11); + std::transform(syn.begin(), syn.end(), syn.begin(), ::tolower); + localities.set(Standalone(syn), Standalone(std::string(args.OptionArg()))); + break; + } + case OPT_VERSION: + printVersion(); + flushAndExit(FDB_EXIT_SUCCESS); + break; + case OPT_NOBUFSTDOUT: + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + break; + case OPT_BUFSTDOUTERR: + setvbuf(stdout, NULL, _IOFBF, BUFSIZ); + setvbuf(stderr, NULL, _IOFBF, BUFSIZ); + break; + case OPT_ROLE: + sRole = args.OptionArg(); + if (!strcmp(sRole, "fdbd")) role = FDBD; + else if (!strcmp(sRole, "simulation")) role = Simulation; + else if (!strcmp(sRole, "test")) role = Test; + else if (!strcmp(sRole, "multitest")) role = MultiTester; + else if (!strcmp(sRole, "skiplisttest")) role = SkipListTest; + else if (!strcmp(sRole, "search")) role = SearchMutations; + else if (!strcmp(sRole, "dsltest")) role = DSLTest; + else if (!strcmp(sRole, "versionedmaptest")) role = VersionedMapTest; + else if (!strcmp(sRole, "createtemplatedb")) role = CreateTemplateDatabase; + else if (!strcmp(sRole, "networktestclient")) role = NetworkTestClient; + else if (!strcmp(sRole, "networktestserver")) role = NetworkTestServer; + else if (!strcmp(sRole, "consistencycheck")) role = ConsistencyCheck; + else { + fprintf(stderr, "ERROR: Unknown role `%s'\n", sRole); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + break; + case OPT_PUBLICADDR: + publicAddressStr = args.OptionArg(); + break; + case OPT_LISTEN: + listenAddressStr = args.OptionArg(); + break; + case OPT_CONNFILE: + connFile = args.OptionArg(); + break; + case OPT_LOGGROUP: + logGroup = args.OptionArg(); + break; + case OPT_SEEDCONNFILE: + seedConnFile = args.OptionArg(); + break; + case OPT_SEEDCONNSTRING: + seedConnString = args.OptionArg(); + break; + #ifdef __linux__ + case OPT_FILESYSTEM: { + fileSystemPath = args.OptionArg(); + break; + } + #endif + case OPT_DATAFOLDER: + dataFolder = args.OptionArg(); + break; + case OPT_LOGFOLDER: + logFolder = args.OptionArg(); + break; + case OPT_NETWORKIMPL: { + const char* a = args.OptionArg(); + if (!strcmp(a, "net2")) useNet2 = true; + else if (!strcmp(a, "net2-threadpool")) { useNet2 = true; useThreadPool = true; } + else { + fprintf(stderr, "ERROR: Unknown network implementation `%s'\n", a); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + break; + } + case OPT_TRACECLOCK: { + const char* a = args.OptionArg(); + if (!strcmp(a, "realtime")) g_trace_clock = TRACE_CLOCK_REALTIME; + else if (!strcmp(a, "now")) g_trace_clock = TRACE_CLOCK_NOW; + else { + fprintf(stderr, "ERROR: Unknown clock source `%s'\n", a); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + break; + } + case OPT_NUMTESTERS: { + const char* a = args.OptionArg(); + if( !sscanf(a, "%d", &minTesterCount) ) { + fprintf(stderr, "ERROR: Could not parse numtesters `%s'\n", a); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + break; + } + case OPT_ROLLSIZE: { + const char* a = args.OptionArg(); + ti = parse_with_suffix(a); + if (!ti.present()) { + fprintf(stderr, "ERROR: Could not parse logsize `%s'\n", a); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + rollsize = ti.get(); + break; + } + case OPT_MAXLOGSSIZE: { + const char *a = args.OptionArg(); + ti = parse_with_suffix(a); + if (!ti.present()) { + fprintf(stderr, "ERROR: Could not parse maxlogssize `%s'\n", a); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + maxLogsSize = ti.get(); + maxLogsSizeSet = true; + break; + } + case OPT_MAXLOGS: { + const char *a = args.OptionArg(); + char *end; + maxLogs = strtoull(a, &end, 10); + if(*end) { + fprintf(stderr, "ERROR: Unrecognized maximum number of logs `%s'\n", a); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + maxLogsSet = true; + break; + } + #ifdef _WIN32 + case OPT_PARENTPID: { + auto pid_str = args.OptionArg(); + int parent_pid = atoi(pid_str); + auto pHandle = OpenProcess( SYNCHRONIZE, FALSE, parent_pid ); + if( !pHandle ) { + TraceEvent("ParentProcessOpenError").GetLastError(); + fprintf(stderr, "Could not open parent process at pid %d (error %d)", parent_pid, GetLastError()); + throw platform_error(); + } + startThread(&parentWatcher, pHandle); + break; + } + case OPT_NEWCONSOLE: + FreeConsole(); + AllocConsole(); + freopen("CONIN$","rb",stdin); + freopen("CONOUT$","wb",stdout); + freopen("CONOUT$","wb",stderr); + break; + case OPT_NOBOX: + SetErrorMode(SetErrorMode(0) | SEM_NOGPFAULTERRORBOX); + break; + #endif + case OPT_TESTFILE: + testFile = args.OptionArg(); + break; + case OPT_RESTARTING: + restarting = true; + break; + case OPT_RANDOMSEED: { + char* end; + randomSeed = (uint32_t)strtoul( args.OptionArg(), &end, 10 ); + if( *end ) { + fprintf(stderr, "ERROR: Could not parse random seed `%s'\n", args.OptionArg()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + break; + } + case OPT_MACHINEID: { + zoneId = std::string(args.OptionArg()); + break; + } + case OPT_DCID: { + dcId = std::string(args.OptionArg()); + break; + } + case OPT_MACHINE_CLASS: + sRole = args.OptionArg(); + processClass = ProcessClass( sRole, ProcessClass::CommandLineSource ); + if (processClass == ProcessClass::InvalidClass) { + fprintf(stderr, "ERROR: Unknown machine class `%s'\n", sRole); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + break; + case OPT_KEY: + targetKey = args.OptionArg(); + break; + case OPT_MEMLIMIT: + ti = parse_with_suffix(args.OptionArg(), "MiB"); + if (!ti.present()) { + fprintf(stderr, "ERROR: Could not parse memory limit from `%s'\n", args.OptionArg()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + memLimit = ti.get(); + break; + case OPT_STORAGEMEMLIMIT: + ti = parse_with_suffix(args.OptionArg(), "MB"); + if (!ti.present()) { + fprintf(stderr, "ERROR: Could not parse storage memory limit from `%s'\n", args.OptionArg()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + storageMemLimit = ti.get(); + break; + case OPT_BUGGIFY: + if( !strcmp( args.OptionArg(), "on" ) ) + buggifyEnabled = true; + else if( !strcmp( args.OptionArg(), "off" ) ) + buggifyEnabled = false; + else { + fprintf(stderr, "ERROR: Unknown buggify state `%s'\n", args.OptionArg()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + break; + case OPT_CRASHONERROR: + g_crashOnError = true; + break; + case OPT_TESTSERVERS: + testServersStr = args.OptionArg(); + break; + case OPT_TEST_ON_SERVERS: + testOnServers = true; + break; + case OPT_METRICSCONNFILE: + metricsConnFile = args.OptionArg(); + break; + case OPT_METRICSPREFIX: + metricsPrefix = args.OptionArg(); + break; + case OPT_IO_TRUST_SECONDS: { + const char* a = args.OptionArg(); + if( !sscanf(a, "%lf", &fileIoTimeout) ) { + fprintf(stderr, "ERROR: Could not parse io_trust_seconds `%s'\n", a); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + break; + } + case OPT_IO_TRUST_WARN_ONLY: + fileIoWarnOnly = true; + break; + case TLSOptions::OPT_TLS_PLUGIN: + try { + tlsOptions->set_plugin_name_or_path( args.OptionArg() ); + } catch (Error& e) { + fprintf(stderr, "ERROR: cannot load TLS plugin `%s' (%s)\n", args.OptionArg(), e.what()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + break; + case TLSOptions::OPT_TLS_CERTIFICATES: + tlsCertPath = args.OptionArg(); + break; + case TLSOptions::OPT_TLS_KEY: + tlsKeyPath = args.OptionArg(); + break; + case TLSOptions::OPT_TLS_VERIFY_PEERS: + tlsVerifyPeers = args.OptionArg(); + break; + } + } + + if (seedConnString.length() && seedConnFile.length()) { + fprintf(stderr, "%s\n", "--seed_cluster_file and --seed_connection_string may not both be specified at once."); + return FDB_EXIT_ERROR; + } + + bool seedSpecified = seedConnFile.length() || seedConnString.length(); + + if (seedSpecified && !connFile.length()){ + fprintf(stderr, "%s\n", "If -seed_cluster_file or --seed_connection_string is specified, -C must be specified as well."); + return FDB_EXIT_ERROR; + } + + if( metricsConnFile == connFile ) + metricsConnFile = ""; + + if( metricsConnFile != "" && metricsPrefix == "" ) { + fprintf(stderr, "If a metrics cluster file is specified, a metrics prefix is required.\n"); + return FDB_EXIT_ERROR; + } + + bool autoPublicAddress = StringRef(publicAddressStr).startsWith(LiteralStringRef("auto:")); + + Reference connectionFile; + if ( (role != Simulation && role != CreateTemplateDatabase) || autoPublicAddress ) { + + if (seedSpecified && !fileExists(connFile)){ + std::string connectionString = seedConnString.length() ? seedConnString : ""; + ClusterConnectionString ccs; + if(seedConnFile.length()) { + try { + connectionString = readFileBytes(seedConnFile, MAX_CLUSTER_FILE_BYTES); + } + catch (Error& e) { + fprintf(stderr, "%s\n", ClusterConnectionFile::getErrorString(std::make_pair(seedConnFile, false), e).c_str()); + throw; + } + } + + try { + ccs = ClusterConnectionString(connectionString); + } + catch (Error& e) { + fprintf(stderr, "%s\n", ClusterConnectionString::getErrorString(connectionString, e).c_str()); + throw; + } + connectionFile = Reference(new ClusterConnectionFile(connFile, ccs)); + } + else { + std::pair resolvedClusterFile; + try { + resolvedClusterFile = ClusterConnectionFile::lookupClusterFileName(connFile); + connectionFile = Reference(new ClusterConnectionFile(resolvedClusterFile.first)); + } catch (Error& e) { + fprintf(stderr, "%s\n", ClusterConnectionFile::getErrorString(resolvedClusterFile, e).c_str()); + throw; + } + } + + // failmon? + } + + if (publicAddressStr != "") { + if (autoPublicAddress) { + try { + NetworkAddress parsedAddress = NetworkAddress::parse("0.0.0.0:" + publicAddressStr.substr(5)); + auto publicIP = determinePublicIPAutomatically( connectionFile->getConnectionString() ); + publicAddress = NetworkAddress( publicIP, parsedAddress.port, true, parsedAddress.isTLS() ); + } catch (Error& e) { + fprintf(stderr, "ERROR: could not determine public address automatically from `%s': %s\n", publicAddressStr.c_str(), e.what()); + throw; + } + } else { + try { + publicAddress = NetworkAddress::parse(publicAddressStr); + } catch (Error&) { + fprintf(stderr, "ERROR: Could not parse network address `%s' (specify as IP_ADDRESS:PORT)\n", publicAddressStr.c_str()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + } + + bool clusterIsTLS = connectionFile->getConnectionString().coordinators()[0].isTLS(); + + // Decide whether or not to use TLS based on if we see any of the coordinators have TLS enabled. + // Note that we are not supporting mixed clusters, but are defaulting to using TLS based on + // the contents of the cluster file. Note that we look at all the servers in the cluster file + // and if ANY of them are TLS, we turn TLS on. + if( !StringRef(publicAddressStr).endsWith(LiteralStringRef(":tls")) ) { + publicAddress = NetworkAddress( publicAddress.ip, publicAddress.port, true, clusterIsTLS ); + } else if( publicAddress.isTLS() != clusterIsTLS ) { + fprintf(stderr, "ERROR: public address must not specify TLS if coordinators are non-TLS\n"); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + } + + if( role == FDBD && publicAddress.ip == 0 ) { + if (publicAddressStr == "") + fprintf(stderr, "ERROR: The -p or --public_address option is required\n"); + else + fprintf(stderr, "ERROR: cannot use 0.0.0.0 as a public ip address\n"); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + + if (listenAddressStr == "public") + listenAddress = publicAddress; + else { + try { + listenAddress = NetworkAddress::parse(listenAddressStr); + } catch (Error&) { + fprintf(stderr, "ERROR: Could not parse network address `%s' (specify as IP_ADDRESS:PORT)\n", listenAddressStr.c_str()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + } + + if (role == FDBD && !publicAddress.isValid()) { + fprintf(stderr, "ERROR: Public address not specified\n"); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + + if (role==Simulation) + printf("Random seed is %u...\n", randomSeed); + + if( zoneId.present() ) + printf("ZoneId set to %s, dcId to %s\n", printable(zoneId).c_str(), printable(dcId).c_str()); + + g_random = new DeterministicRandom(randomSeed); + if (role == Simulation) + trace_random = new DeterministicRandom(1); + else + trace_random = new DeterministicRandom(platform::getRandomSeed()); + if (role == Simulation) + g_nondeterministic_random = new DeterministicRandom(2); + else + g_nondeterministic_random = new DeterministicRandom(platform::getRandomSeed()); + if (role == Simulation) + g_debug_random = new DeterministicRandom(3); + else + g_debug_random = new DeterministicRandom(platform::getRandomSeed()); + + enableBuggify( buggifyEnabled ); + + delete FLOW_KNOBS; + delete SERVER_KNOBS; + delete CLIENT_KNOBS; + FlowKnobs* flowKnobs = new FlowKnobs(true, role == Simulation); + ClientKnobs* clientKnobs = new ClientKnobs(true); + ServerKnobs* serverKnobs = new ServerKnobs(true, clientKnobs); + FLOW_KNOBS = flowKnobs; + SERVER_KNOBS = serverKnobs; + CLIENT_KNOBS = clientKnobs; + + for(auto k=knobs.begin(); k!=knobs.end(); ++k) { + try { + if (!flowKnobs->setKnob( k->first, k->second ) && + !clientKnobs->setKnob( k->first, k->second ) && + !serverKnobs->setKnob( k->first, k->second )) + { + fprintf(stderr, "Unrecognized knob option '%s'\n", k->first.c_str()); + flushAndExit(FDB_EXIT_ERROR); + } + } catch (Error& e) { + if (e.code() == error_code_invalid_option_value) { + fprintf(stderr, "Invalid value '%s' for option '%s'\n", k->second.c_str(), k->first.c_str()); + flushAndExit(FDB_EXIT_ERROR); + } + throw; + } + } + + if (role == SkipListTest) { + skipListTest(); + flushAndExit(FDB_EXIT_SUCCESS); + } + + if (role == DSLTest) { + dsltest(); + flushAndExit(FDB_EXIT_SUCCESS); + } + + if (role == VersionedMapTest) { + versionedMapTest(); + flushAndExit(FDB_EXIT_SUCCESS); + } + + if (role == SearchMutations && !targetKey) { + fprintf(stderr, "ERROR: please specify a target key\n"); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + + if (role == NetworkTestClient && !testServersStr.size() ) { + fprintf(stderr, "ERROR: please specify --testservers\n"); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + + Future listenError; + + // Interpret legacy "maxLogs" option in the most sensible and unsurprising way we can while eliminating its code path + if (maxLogsSet) { + if (maxLogsSizeSet) { + // This is the case where both options are set and we must deconflict. + auto maxLogsAsSize = maxLogs * rollsize; + + // If either was unlimited, then the safe option here is to take the larger one. + // This means that is one of the two options specified a limited amount of logging + // then the option that specified "unlimited" will be ignored. + if( maxLogsSize == 0 || maxLogs == 0 ) + maxLogsSize = std::max(maxLogsSize, maxLogsAsSize); + else + maxLogsSize = std::min(maxLogsSize, maxLogs * rollsize); + } + else { + maxLogsSize = maxLogs * rollsize; + } + } + + // Initialize the thread pool + CoroThreadPool::init(); + // Ordinarily, this is done when the network is run. However, network thread should be set before TraceEvents are logged. This thread will eventually run the network, so call it now. + TraceEvent::setNetworkThread(); + + if (role == Simulation || role == CreateTemplateDatabase) { + //startOldSimulator(); + startNewSimulator(); + openTraceFile(NetworkAddress(), rollsize, maxLogsSize, logFolder, "trace", logGroup); + } else { + g_network = newNet2(NetworkAddress(), useThreadPool, true); + FlowTransport::createInstance(1); + + openTraceFile(publicAddress, rollsize, maxLogsSize, logFolder, "trace", logGroup); + + if ( tlsCertPath.size() ) + tlsOptions->set_cert_file( tlsCertPath ); + if ( tlsKeyPath.size() ) + tlsOptions->set_key_file( tlsKeyPath ); + if ( tlsVerifyPeers.size() ) + tlsOptions->set_verify_peers( tlsVerifyPeers ); + + tlsOptions->register_network(); + + if (role == FDBD || role == NetworkTestServer) { + try { + listenError = FlowTransport::transport().bind(publicAddress, listenAddress); + if (listenError.isReady()) listenError.get(); + } catch (Error& e) { + TraceEvent("BindError").error(e); + fprintf(stderr, "Error initializing networking with public address %s and listen address %s\n", publicAddress.toString().c_str(), listenAddress.toString().c_str()); + printHelpTeaser(argv[0]); + flushAndExit(FDB_EXIT_ERROR); + } + } + + // Use a negative ioTimeout to indicate warn-only + Net2FileSystem::newFileSystem(fileIoWarnOnly ? -fileIoTimeout : fileIoTimeout, fileSystemPath); + g_network->initMetrics(); + FlowTransport::transport().initMetrics(); + initTraceEventMetrics(); + } + + double start = timer(), startNow = now(); + + std::string cwd = ""; + try { + cwd = platform::getWorkingDirectory(); + } catch(Error &e) { + // Allow for platform error by rethrowing all _other_ errors + if( e.code() != error_code_platform_error ) + throw; + } + + TraceEvent("ProgramStart") + .detail("RandomSeed", randomSeed) + .detail("SourceVersion", getHGVersion()) + .detail("Version", FDB_VT_VERSION ) + .detail("PackageName", FDB_VT_PACKAGE_NAME) + .detail("FileSystem", fileSystemPath) + .detail("DataFolder", dataFolder) + .detail("WorkingDirectory", cwd) + .detail("ClusterFile", connectionFile ? connectionFile->getFilename().c_str() : "") + .detail("ConnectionString", connectionFile ? connectionFile->getConnectionString().toString() : "") + .detailf("ActualTime", "%lld", DEBUG_DETERMINISM ? 0 : time(NULL)) + .detail("CommandLine", commandLine) + .detail("BuggifyEnabled", buggifyEnabled) + .detail("MemoryLimit", memLimit) + .trackLatest("ProgramStart"); + + // Test for TraceEvent length limits + /*std::string foo(4096, 'x'); + TraceEvent("TooLongDetail").detail("Contents", foo); + + TraceEvent("TooLongEvent") + .detail("Contents1", foo) + .detail("Contents2", foo) + .detail("Contents3", foo) + .detail("Contents4", foo) + .detail("Contents5", foo) + .detail("Contents6", foo) + .detail("Contents7", foo) + .detail("Contents8", foo) + .detail("ExtraTest", 1776);*/ + + Error::init(); + std::set_new_handler( &platform::outOfMemory ); + setMemoryQuota( memLimit ); + + Future> f; + + if (role == Simulation) { + TraceEvent("Simulation").detail("TestFile", testFile); + + clientKnobs->trace(); + flowKnobs->trace(); + serverKnobs->trace(); + + if (!dataFolder.size()) + dataFolder = "simfdb"; + + std::vector directories = platform::listDirectories( dataFolder ); + for(int i = 0; i < directories.size(); i++) + if( directories[i].size() != 32 && directories[i] != "." && directories[i] != ".." && directories[i] != "backups") { + TraceEvent(SevError, "IncompatibleDirectoryFound").detail("DataFolder", dataFolder).detail("SuspiciousFile", directories[i]); + fprintf(stderr, "ERROR: Data folder `%s' had non fdb file `%s'; please use clean, fdb-only folder\n", dataFolder.c_str(), directories[i].c_str()); + flushAndExit(FDB_EXIT_ERROR); + } + std::vector files = platform::listFiles( dataFolder ); + if( (files.size()>1 || (files.size()==1 && files[0] != "restartInfo.ini" )) && !restarting ) { + TraceEvent(SevError, "IncompatibleFileFound").detail("DataFolder", dataFolder); + fprintf(stderr, "ERROR: Data folder `%s' is non-empty; please use clean, fdb-only folder\n", dataFolder.c_str()); + flushAndExit(FDB_EXIT_ERROR); + } + else if ( files.empty() && restarting ) { + TraceEvent(SevWarnAlways, "FileNotFound").detail("DataFolder", dataFolder); + printf("ERROR: Data folder `%s' is empty, but restarting option selected. Run Phase 1 test first\n", dataFolder.c_str()); + flushAndExit(FDB_EXIT_ERROR); + } + + if (!restarting) { + platform::eraseDirectoryRecursive( dataFolder ); + platform::createDirectory( dataFolder ); + } + + setupAndRun( dataFolder, testFile, restarting ); + g_simulator.run(); + } else if (role == FDBD) { + ASSERT( connectionFile ); + + setupSlowTaskProfiler(); + + if (!dataFolder.size()) + dataFolder = format("fdb/%d/", publicAddress.port); // SOMEDAY: Better default + + vector> actors; + actors.push_back( listenError ); + Standalone machineId(getSharedMemoryMachineId().toString()); + + if (!localities.isPresent(LocalityData::keyZoneId)) + localities.set(LocalityData::keyZoneId, zoneId.present() ? zoneId : machineId); + + if (!localities.isPresent(LocalityData::keyMachineId)) + localities.set(LocalityData::keyMachineId, machineId); + + if (!localities.isPresent(LocalityData::keyDcId) && dcId.present()) + localities.set(LocalityData::keyDcId, dcId); + + actors.push_back( fdbd(connectionFile, localities, processClass, dataFolder, dataFolder, storageMemLimit, metricsConnFile, metricsPrefix) ); + //actors.push_back( recurring( []{}, .001 ) ); // for ASIO latency measurement + + f = stopAfter( waitForAll(actors) ); + g_network->run(); + } else if (role == MultiTester) { + f = stopAfter( runTests( connectionFile, TEST_TYPE_FROM_FILE, testOnServers ? TEST_ON_SERVERS : TEST_ON_TESTERS, minTesterCount, testFile ) ); + g_network->run(); + } else if (role == Test || role == ConsistencyCheck) { + auto m = startSystemMonitor(dataFolder, zoneId, zoneId); + f = stopAfter( runTests( connectionFile, role == ConsistencyCheck ? TEST_TYPE_CONSISTENCY_CHECK : TEST_TYPE_FROM_FILE, TEST_HERE, 1, testFile ) ); + g_network->run(); + } else if (role == CreateTemplateDatabase) { + createTemplateDatabase(); + } else if (role == NetworkTestClient) { + f = stopAfter( networkTestClient( testServersStr ) ); + g_network->run(); + } else if (role == NetworkTestServer) { + f = stopAfter( networkTestServer() ); + g_network->run(); + } + + int rc = FDB_EXIT_SUCCESS; + if(f.isValid() && f.isReady() && !f.isError() && !f.get().present()) { + rc = FDB_EXIT_ERROR; + } + + int unseed = noUnseed ? 0 : g_random->randomInt(0, 100001); + TraceEvent("ElapsedTime").detail("SimTime", now()-startNow).detail("RealTime", timer()-start) + .detail("RandomUnseed", unseed); + + if (role==Simulation){ + printf("Unseed: %d\n", unseed); + printf("Elapsed: %f simsec, %f real seconds\n", now()-startNow, timer()-start); + //cout << format(" %d endpoints left\n", transport().getEndpointCount()); + } + + //IFailureMonitor::failureMonitor().address_info.clear(); + + // we should have shut down ALL actors associated with this machine; let's list all of the ones still live + /*{ + auto living = Actor::all; + printf("%d surviving actors:\n", living.size()); + for(auto a = living.begin(); a != living.end(); ++a) + printf(" #%lld %s %p\n", (*a)->creationIndex, (*a)->getName(), (*a)); + } + + { + auto living = DatabaseContext::all; + printf("%d surviving DatabaseContexts:\n", living.size()); + for(auto a = living.begin(); a != living.end(); ++a) + printf(" #%lld %p\n", (*a)->creationIndex, (*a)); + } + + { + auto living = TransactionData::all; + printf("%d surviving TransactionData(s):\n", living.size()); + for(auto a = living.begin(); a != living.end(); ++a) + printf(" #%lld %p\n", (*a)->creationIndex, (*a)); + }*/ + + /*cout << Actor::allActors.size() << " surviving actors:" << endl; + std::map actorCount; + for(int i=0; igetName()]; + for(auto i = actorCount.rbegin(); !(i == actorCount.rend()); ++i) + cout << " " << i->second << " " << i->first << endl;*/ + // cout << " " << Actor::allActors[i]->getName() << endl; + + int total = 0; + for(auto i = Error::errorCounts().begin(); i != Error::errorCounts().end(); ++i) + total += i->second; + if (total) + printf("%d errors:\n", total); + for(auto i = Error::errorCounts().begin(); i != Error::errorCounts().end(); ++i) + if (i->second > 0) + printf(" %d: %d %s\n", i->second, i->first, Error::fromCode(i->first).what()); + + if (&g_simulator == g_network) { + auto processes = g_simulator.getAllProcesses(); + for(auto i = processes.begin(); i != processes.end(); ++i) + printf("%s %s: %0.3f Mclocks\n", (*i)->name, (*i)->address.toString().c_str(), (*i)->cpuTicks / 1e6); + } + + //g_simulator.run(); + + #ifdef ALLOC_INSTRUMENTATION + { + std::cout << "Page Counts: " + << FastAllocator<16>::pageCount << " " + << FastAllocator<32>::pageCount << " " + << FastAllocator<64>::pageCount << " " + << FastAllocator<128>::pageCount << " " + << FastAllocator<256>::pageCount << " " + << FastAllocator<512>::pageCount << " " + << FastAllocator<1024>::pageCount << " " + << FastAllocator<2048>::pageCount << " " + << FastAllocator<4096>::pageCount << std::endl; + + vector< std::pair > typeNames; + for( auto i = allocInstr.begin(); i != allocInstr.end(); ++i ) { + std::string s; + +#ifdef __linux__ + char *demangled = abi::__cxa_demangle(i->first, NULL, NULL, NULL); + if (demangled) { + s = demangled; + if (StringRef(s).startsWith(LiteralStringRef("(anonymous namespace)::"))) + s = s.substr(LiteralStringRef("(anonymous namespace)::").size()); + free(demangled); + } else + s = i->first; +#else + s = i->first; + if (StringRef(s).startsWith(LiteralStringRef("class `anonymous namespace'::"))) + s = s.substr(LiteralStringRef("class `anonymous namespace'::").size()); + else if (StringRef(s).startsWith(LiteralStringRef("class "))) + s = s.substr(LiteralStringRef("class ").size()); + else if (StringRef(s).startsWith(LiteralStringRef("struct "))) + s = s.substr(LiteralStringRef("struct ").size()); +#endif + + typeNames.push_back( std::make_pair(s, i->first) ); + } + std::sort(typeNames.begin(), typeNames.end()); + for(int i=0; i::Present, "Storage server interface should be load balanced" ); + static_assert( LBLocalityData::Present, "Master proxy interface should be load balanced" ); + static_assert( LBLocalityData::Present, "TLog interface should be load balanced" ); + static_assert( !LBLocalityData::Present, "Master interface should not be load balanced" ); +} diff --git a/fdbserver/fdbserver.vcxproj b/fdbserver/fdbserver.vcxproj new file mode 100644 index 0000000000..3cd3b54214 --- /dev/null +++ b/fdbserver/fdbserver.vcxproj @@ -0,0 +1,297 @@ + + + + + -PRERELEASE + + + + + FDB_CLEAN_BUILD;%(PreprocessorDefinitions) + + + + Debug + X64 + + + Release + X64 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + false + + + + + false + + + + + + + + + + + + + + + + + + + + + false + + + + + + + + {8E959DA5-5925-45CE-BFC4-C84EB632A29A} + v4.5.2 + Win32Proj + flow + + + $(SolutionDir)bin\$(Configuration)\ + $(SystemDrive)\temp\msvcfdb\$(Platform)$(Configuration)\$(MSBuildProjectName)\ + $(IntDir)\$(MSBuildProjectName).log + + + + Application + MultiByte + v140_xp + + + Application + MultiByte + v140_xp + + + + + + + + + + true + $(IncludePath);../;C:\Program Files\boost_1_52_0 + + + false + $(IncludePath);../;C:\Program Files\boost_1_52_0 + PreBuildEvent + + + + $(TargetDir)fdbclient.lib + + + FDB_VT_VERSION="$(Version)$(PreReleaseDecoration)";FDB_VT_PACKAGE_NAME="$(PackageName)";%(PreprocessorDefinitions) + + + + + + + Level3 + false + ProgramDatabase + Disabled + EnableFastChecks + MultiThreadedDebug + WIN32;_WIN32_WINNT=0x0502;WINVER=0x0502;NTDDI_VERSION=0x05020000;_DEBUG;_HAS_ITERATOR_DEBUGGING=0;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) + true + /bigobj @../flow/no_intellisense.opt %(AdditionalOptions) + false + + + Console + true + $(SolutionDir)bin\$(Configuration)\fdbclient.lib;Advapi32.lib + + + + + Level3 + + + ProgramDatabase + Full + MultiThreaded + true + WIN32;_WIN32_WINNT=0x0502;WINVER=0x0502;NTDDI_VERSION=0x05020000;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) + NotSet + false + /bigobj @../flow/no_intellisense.opt %(AdditionalOptions) + true + Speed + false + false + + + Console + true + false + false + $(SolutionDir)bin\$(Configuration)\fdbclient.lib;Advapi32.lib + /LTCG %(AdditionalOptions) + + + + + + + + + + \ No newline at end of file diff --git a/fdbserver/fdbserver.vcxproj.filters b/fdbserver/fdbserver.vcxproj.filters new file mode 100644 index 0000000000..4996e5a50b --- /dev/null +++ b/fdbserver/fdbserver.vcxproj.filters @@ -0,0 +1,342 @@ + + + + + + + + + + + + + + + + + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + + workloads + + + workloads + + + workloads + + + workloads + + + + + workloads + + + workloads + + + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + + workloads + + + workloads + + + workloads + + + + + + + + workloads + + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + + + + workloads + + + + + workloads + + + workloads + + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + + workloads + + + workloads + + + workloads + + + workloads + + + workloads + + + + + + + workloads + + + sqlite + + + sqlite + + + + + workloads + + + + workloads + + + + + + + + + + + + + workloads + + + + + + + + sqlite + + + sqlite + + + sqlite + + + sqlite + + + sqlite + + + sqlite + + + + + + + + + + + + + + + workloads + + + workloads + + + + + + + + + + + + + {6a79fc02-2f89-451d-9dd5-999d753b3159} + + + {de5e282f-8d97-4054-b795-0a75b772326f} + + + \ No newline at end of file diff --git a/fdbserver/local.mk b/fdbserver/local.mk new file mode 100644 index 0000000000..321dad779d --- /dev/null +++ b/fdbserver/local.mk @@ -0,0 +1,52 @@ +# +# local.mk +# +# This source file is part of the FoundationDB open source project +# +# Copyright 2013-2018 Apple Inc. and the FoundationDB project authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# -*- mode: makefile; -*- + +fdbserver_CFLAGS := $(fdbclient_CFLAGS) +fdbserver_LDFLAGS := $(fdbrpc_LDFLAGS) +fdbserver_LIBS := lib/libfdbclient.a lib/libfdbrpc.a lib/libflow.a + +ifeq ($(PLATFORM),linux) + fdbserver_LIBS += -ldl -lpthread -lrt + fdbserver_LDFLAGS += -static-libstdc++ -static-libgcc + + # GPerfTools profiler (uncomment to use) + # fdbserver_CFLAGS += -I/opt/gperftools/include -DUSE_GPERFTOOLS=1 + # fdbserver_LDFLAGS += -L/opt/gperftools/lib + # fdbserver_STATIC_LIBS += -ltcmalloc -lunwind -lprofiler +else ifeq ($(PLATFORM),osx) + fdbserver_LDFLAGS += -lc++ +endif + +ifeq ($(WORKLOADS),false) + fdbserver_ALL_SOURCES := $(filter-out fdbserver/workloads/%,$(fdbserver_ALL_SOURCES)) + fdbserver_BUILD_SOURCES := $(filter-out fdbserver/workloads/%,$(fdbserver_BUILD_SOURCES)) +endif + +bin/fdbserver: bin/coverage.fdbserver.xml + +bin/fdbserver.debug: bin/fdbserver + +FORCE: + +createtemplatedb: bin/fdbserver + bin/fdbserver -r createtemplatedb + python -c 'import textwrap; s=open("template.fdb", "rb").read().encode("hex").upper(); t="".join(["\\x"+x+y for (x,y) in zip(s[0::2], s[1::2])]) ; open("fdbserver/template_fdb.h","wb").write("static const char template_fdb[] = \\\n\t\"%s\";"%"\" \\\n\t\"".join(textwrap.wrap(t,80)))' diff --git a/fdbserver/masterserver.actor.cpp b/fdbserver/masterserver.actor.cpp new file mode 100644 index 0000000000..cb90ab83e4 --- /dev/null +++ b/fdbserver/masterserver.actor.cpp @@ -0,0 +1,1214 @@ +/* + * masterserver.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "flow/ActorCollection.h" +#include "fdbrpc/PerfMetric.h" +#include "flow/Trace.h" +#include "fdbrpc/FailureMonitor.h" +#include "fdbclient/NativeAPI.h" +#include "fdbclient/SystemData.h" +#include "ConflictSet.h" +#include "flow/Notified.h" +#include "DataDistribution.h" +#include "Knobs.h" +#include +#include "WaitFailure.h" +#include "WorkerInterface.h" +#include "Ratekeeper.h" +#include "ClusterRecruitmentInterface.h" +#include "ServerDBInfo.h" +#include "CoordinatedState.h" +#include "fdbserver/CoordinationInterface.h" // copy constructors for ServerCoordinators class +#include "fdbrpc/sim_validation.h" +#include "DBCoreState.h" +#include "LogSystem.h" +#include "LogSystemDiskQueueAdapter.h" +#include "IKeyValueStore.h" +#include "ApplyMetadataMutation.h" +#include "RecoveryState.h" + +using std::vector; +using std::min; +using std::max; + +struct ProxyVersionReplies { + std::map replies; + NotifiedVersion latestRequestNum; + + ProxyVersionReplies(ProxyVersionReplies&& r) noexcept(true) : replies(std::move(r.replies)), latestRequestNum(std::move(r.latestRequestNum)) {} + void operator=(ProxyVersionReplies&& r) noexcept(true) { replies = std::move(r.replies); latestRequestNum = std::move(r.latestRequestNum); } + + ProxyVersionReplies() : latestRequestNum(0) {} +}; + +struct MasterData : NonCopyable, ReferenceCounted { + UID dbgid; + + AsyncTrigger registrationTrigger; + Version lastEpochEnd, // The last version in the old epoch not (to be) rolled back in this recovery + recoveryTransactionVersion; // The first version in this epoch + double lastCommitTime; + + DBCoreState prevDBState; + // prevDBState is the coordinated state (contents of this->cstate) for the database as of the + // beginning of recovery. If our recovery succeeds, it will be the penultimate state in the consistent chain. + + Optional myDBState; + // myDBState present only after recovery succeeds. It is the state that we wrote to this->cstate + // after recovering and is the final state in the consistent chain. + + DatabaseConfiguration originalConfiguration; + DatabaseConfiguration configuration; + + ServerCoordinators coordinators; + + Reference< ILogSystem > logSystem; + Version version; // The last version assigned to a proxy by getVersion() + double lastVersionTime; + LogSystemDiskQueueAdapter* txnStateLogAdapter; + IKeyValueStore* txnStateStore; + int64_t memoryLimit; + + vector< MasterProxyInterface > proxies; + vector< MasterProxyInterface > provisionalProxies; + vector< ResolverInterface > resolvers; + + std::map lastProxyVersionReplies; + + Standalone dbName; + Standalone dbId; + + MasterInterface myInterface; + ClusterControllerFullInterface clusterController; // If the cluster controller changes, this master will die, so this is immutable. + + MovableCoordinatedState cstate1; // Stores serialized DBCoreState, this one kills previous tlog before recruiting new ones + MovableCoordinatedState cstate2; // Stores serialized DBCoreState, this one contains oldTlogs while we recover from them + MovableCoordinatedState cstate3; // Stores serialized DBCoreState, this is the final one + Reference> dbInfo; + int64_t registrationCount; // Number of different MasterRegistrationRequests sent to clusterController + + RecoveryState::RecoveryState recoveryState; + + AsyncVar>> resolverChanges; + Version resolverChangesVersion; + std::set resolverNeedingChanges; + + Promise fullyRecovered; + + MasterData( + Reference> const& dbInfo, + MasterInterface const& myInterface, + ServerCoordinators const& coordinators, + ClusterControllerFullInterface const& clusterController, + Standalone const& dbName, + Standalone const& dbId + ) + : dbgid( myInterface.id() ), + myInterface(myInterface), + dbInfo(dbInfo), + cstate1(coordinators), + cstate2(coordinators), + cstate3(coordinators), + coordinators(coordinators), + clusterController(clusterController), + dbName( dbName ), + dbId( dbId ), + lastEpochEnd(invalidVersion), + recoveryTransactionVersion(invalidVersion), + lastCommitTime(0), + registrationCount(0), + version(invalidVersion), + lastVersionTime(0), + txnStateStore(0), + memoryLimit(2e9) + { + } + ~MasterData() { if(txnStateStore) txnStateStore->close(); } +}; + +ACTOR Future writeTransitionMasterState( Reference self, bool skipTransition ) { + state DBCoreState newState; + self->logSystem->toCoreState( newState ); + newState.recoveryCount = self->prevDBState.recoveryCount + 1; + + ASSERT( newState.tLogWriteAntiQuorum == self->configuration.tLogWriteAntiQuorum && newState.tLogReplicationFactor == self->configuration.tLogReplicationFactor ); + + try { + Void _ = wait( self->cstate2.setExclusive( BinaryWriter::toValue(newState, IncludeVersion()) ) ); + } catch (Error& e) { + TEST(true); // Master displaced during writeMasterState + throw; + } + + if( !skipTransition ) { + Value rereadDBStateRaw = wait( self->cstate3.read() ); + DBCoreState readState; + if( rereadDBStateRaw.size() ) + readState = BinaryReader::fromStringRef(rereadDBStateRaw, IncludeVersion()); + + if( readState != newState ) { + TraceEvent("MasterTerminated", self->dbgid).detail("Reason", "CStateChanged"); + TEST(true); // Coordinated state changed between writing and reading, master dying + throw worker_removed(); + } + } + + self->logSystem->coreStateWritten(newState); + self->myDBState = newState; + + return Void(); +} + +ACTOR Future writeRecoveredMasterState( Reference self ) { + state DBCoreState newState = self->myDBState.get(); + self->logSystem->toCoreState( newState ); + + ASSERT( newState.tLogWriteAntiQuorum == self->configuration.tLogWriteAntiQuorum && newState.tLogReplicationFactor == self->configuration.tLogReplicationFactor ); + + try { + Void _ = wait( self->cstate3.setExclusive( BinaryWriter::toValue(newState, IncludeVersion()) ) ); + } catch (Error& e) { + TEST(true); // Master displaced during writeMasterState + throw; + } + + self->logSystem->coreStateWritten(newState); + self->myDBState = newState; + + return Void(); +} + +ACTOR Future newProxies( Reference self, Future< RecruitFromConfigurationReply > recruits ) { + self->proxies.clear(); + + RecruitFromConfigurationReply recr = wait( recruits ); + state std::vector workers = recr.proxies; + + state vector> initializationReplies; + for( int i = 0; i < workers.size(); i++ ) { + InitializeMasterProxyRequest req; + req.master = self->myInterface; + req.recoveryCount = self->prevDBState.recoveryCount + 1; + req.recoveryTransactionVersion = self->recoveryTransactionVersion; + req.firstProxy = i == 0; + TraceEvent("ProxyReplies",self->dbgid).detail("workerID",workers[i].id()); + initializationReplies.push_back( transformErrors( throwErrorOr( workers[i].masterProxy.getReplyUnlessFailedFor( req, SERVER_KNOBS->TLOG_TIMEOUT, SERVER_KNOBS->MASTER_FAILURE_SLOPE_DURING_RECOVERY ) ), master_recovery_failed() ) ); + } + + vector newRecruits = wait( getAll( initializationReplies ) ); + self->proxies = newRecruits; + + return Void(); +} + +ACTOR Future newResolvers( Reference self, Future< RecruitFromConfigurationReply > recruits ) { + self->resolvers.clear(); + + RecruitFromConfigurationReply recr = wait( recruits ); + state std::vector workers = recr.resolvers; + + state vector> initializationReplies; + for( int i = 0; i < workers.size(); i++ ) { + InitializeResolverRequest req; + req.recoveryCount = self->prevDBState.recoveryCount + 1; + req.proxyCount = recr.proxies.size(); + req.resolverCount = recr.resolvers.size(); + TraceEvent("ResolverReplies",self->dbgid).detail("workerID",workers[i].id()); + initializationReplies.push_back( transformErrors( throwErrorOr( workers[i].resolver.getReplyUnlessFailedFor( req, SERVER_KNOBS->TLOG_TIMEOUT, SERVER_KNOBS->MASTER_FAILURE_SLOPE_DURING_RECOVERY ) ), master_recovery_failed() ) ); + } + + vector newRecruits = wait( getAll( initializationReplies ) ); + self->resolvers = newRecruits; + + return Void(); +} + +ACTOR Future newTLogServers( Reference self, Future< RecruitFromConfigurationReply > recruits, Reference oldLogSystem ) { + RecruitFromConfigurationReply recr = wait( recruits ); + + Reference newLogSystem = wait( oldLogSystem->newEpoch( recr.tLogs, self->configuration, self->prevDBState.recoveryCount + 1 ) ); + self->logSystem = newLogSystem; + + return Void(); +} + +ACTOR Future newSeedServers( Reference self, vector* servers ) { + // This is only necessary if the database is at version 0 + servers->clear(); + if (self->lastEpochEnd) return Void(); + + state Tag tag = 0; + state std::set>> dataCenters; + while( servers->size() < self->configuration.storageTeamSize ) { + try { + RecruitStorageRequest req; + req.criticalRecruitment = true; + for(auto s = servers->begin(); s != servers->end(); ++s) + req.excludeMachines.push_back(s->locality.zoneId()); + if( dataCenters.size() < self->configuration.minDataCenters ) { + for(auto dc = dataCenters.begin(); dc != dataCenters.end(); ++dc) + req.excludeDCs.push_back(*dc); + } + + TraceEvent("MasterRecruitingInitialStorageServer", self->dbgid) + .detail("ExcludingMachines", req.excludeMachines.size()) + .detail("ExcludingDataCenters", req.excludeDCs.size()); + + RecruitStorageReply candidateWorker = wait( brokenPromiseToNever( self->clusterController.recruitStorage.getReply( req ) ) ); + + TraceEvent("MasterRecruitingInitialStorageServer", self->dbgid) + .detail("CandidateWorker", candidateWorker.worker.locality.toString()); + + InitializeStorageRequest isr; + isr.seedTag = tag; + isr.storeType = self->configuration.storageServerStoreType; + isr.reqId = g_random->randomUniqueID(); + isr.interfaceId = g_random->randomUniqueID(); + + ErrorOr newServer = wait( candidateWorker.worker.storage.tryGetReply( isr ) ); + + if( newServer.isError() ) { + if( !newServer.isError( error_code_recruitment_failed ) && !newServer.isError( error_code_request_maybe_delivered ) ) + throw newServer.getError(); + + TEST( true ); // masterserver initial storage recuitment loop failed to get new server + Void _ = wait( delay(SERVER_KNOBS->STORAGE_RECRUITMENT_DELAY) ); + } + else { + servers->push_back( newServer.get() ); + dataCenters.insert( newServer.get().locality.dcId() ); + tag++; + } + } catch ( Error &e ) { + if(e.code() != error_code_timed_out) { + throw; + } + } + } + + TraceEvent("MasterRecruitedInitialStorageServers", self->dbgid) + .detail("TargetCount", self->configuration.storageTeamSize) + .detail("Servers", describe(*servers)); + + return Void(); +} + +Future waitProxyFailure( vector const& proxies ) { + vector> failed; + for(int i=0; iTLOG_TIMEOUT, -SERVER_KNOBS->TLOG_TIMEOUT/SERVER_KNOBS->SECONDS_BEFORE_NO_FAILURE_DELAY ) ); + ASSERT( failed.size() >= 1 ); + return tagError(quorum( failed, 1 ), master_proxy_failed()); +} + +Future waitResolverFailure( vector const& resolvers ) { + vector> failed; + for(int i=0; iTLOG_TIMEOUT, -SERVER_KNOBS->TLOG_TIMEOUT/SERVER_KNOBS->SECONDS_BEFORE_NO_FAILURE_DELAY ) ); + ASSERT( failed.size() >= 1 ); + return tagError(quorum( failed, 1 ), master_resolver_failed()); +} + +ACTOR Future masterTerminateOnConflict( Reference self, Future onConflict ) { + Void _ = wait( onConflict ); + if (!self->fullyRecovered.isSet()) { + TraceEvent("MasterTerminated", self->dbgid).detail("Reason", "Conflict"); + TEST(true); // Coordinated state conflict, master dying + throw worker_removed(); + } + return Void(); +} + +ACTOR Future updateLogsValue( Reference self, Database cx ) { + state Transaction tr(cx); + loop { + try { + Optional> value = wait( tr.get(logsKey) ); + ASSERT(value.present()); + + auto logConf = self->logSystem->getLogSystemConfig(); + auto logs = decodeLogsValue(value.get()); + + bool match = (logs.first.size() == logConf.tLogs.size()); + if(match) { + for(int i = 0; i < logs.first.size(); i++) { + if(logs.first[i].first != logConf.tLogs[i].id()) { + match = false; + break; + } + } + } + + if(!match) { + TEST(true); //old master attempted to change logsKey + return Void(); + } + + tr.set(logsKey, self->logSystem->getLogsValue()); + Void _ = wait( tr.commit() ); + return Void(); + } catch( Error &e ) { + Void _ = wait( tr.onError(e) ); + } + } +} + +Future sendMasterRegistration( MasterData* self, LogSystemConfig const& logSystemConfig, vector proxies, vector resolvers, DBRecoveryCount recoveryCount, vector priorCommittedLogServers ) { + RegisterMasterRequest masterReq; + masterReq.dbName = self->dbName; + masterReq.id = self->myInterface.id(); + masterReq.mi = self->myInterface.locality; + masterReq.logSystemConfig = logSystemConfig; + masterReq.proxies = proxies; + masterReq.resolvers = resolvers; + masterReq.recoveryCount = recoveryCount; + masterReq.configuration = self->configuration; + masterReq.registrationCount = ++self->registrationCount; + masterReq.priorCommittedLogServers = priorCommittedLogServers; + masterReq.recoveryState = self->recoveryState; + return brokenPromiseToNever( self->clusterController.registerMaster.getReply( masterReq ) ); +} + +ACTOR Future updateRegistration( Reference self, Reference logSystem ) { + state Database cx = openDBOnServer(self->dbInfo, TaskDefaultEndpoint, true, true); + state Future trigger = self->registrationTrigger.onTrigger(); + state Future updateLogsKey; + + loop { + Void _ = wait( trigger ); + Void _ = wait( delay( .001 ) ); // Coalesce multiple changes + + trigger = self->registrationTrigger.onTrigger(); + + TraceEvent("MasterUpdateRegistration", self->dbgid).detail("RecoveryCount", self->myDBState.present() ? self->myDBState.get().recoveryCount : self->prevDBState.recoveryCount).detail("logs", describe(logSystem->getLogSystemConfig().tLogs)); + + if (!self->myDBState.present()) { + Void _ = wait(sendMasterRegistration(self.getPtr(), logSystem->getLogSystemConfig(), self->provisionalProxies, self->resolvers, self->prevDBState.recoveryCount, self->prevDBState.getPriorCommittedLogServers() )); + } else { + updateLogsKey = updateLogsValue(self, cx); + Void _ = wait( sendMasterRegistration( self.getPtr(), logSystem->getLogSystemConfig(), self->proxies, self->resolvers, self->myDBState.get().recoveryCount, vector() ) ); + } + } +} + +ACTOR Future> provisionalMaster( Reference parent, Future activate ) { + Void _ = wait(activate); + + // Register a fake master proxy (to be provided right here) to make ourselves available to clients + parent->provisionalProxies = vector(1); + parent->provisionalProxies[0].locality = parent->myInterface.locality; + state Future waitFailure = waitFailureServer(parent->provisionalProxies[0].waitFailure.getFuture()); + parent->registrationTrigger.trigger(); + + auto lockedKey = parent->txnStateStore->readValue(databaseLockedKey).get(); + state bool locked = lockedKey.present() && lockedKey.get().size(); + + // We respond to a minimal subset of the master proxy protocol. Our sole purpose is to receive a single write-only transaction + // which might repair our configuration, and return it. + loop choose { + when ( GetReadVersionRequest req = waitNext( parent->provisionalProxies[0].getConsistentReadVersion.getFuture() ) ) { + if ( req.flags & GetReadVersionRequest::FLAG_CAUSAL_READ_RISKY && parent->lastEpochEnd ) { + GetReadVersionReply rep; + rep.version = parent->lastEpochEnd; + rep.locked = locked; + req.reply.send( rep ); + } else + req.reply.send(Never()); // We can't perform causally consistent reads without recovering + } + when ( CommitTransactionRequest req = waitNext( parent->provisionalProxies[0].commit.getFuture() ) ) { + req.reply.send(Never()); // don't reply (clients always get commit_unknown_result) + auto t = &req.transaction; + TraceEvent("PM_CTC", parent->dbgid).detail("Snapshot", t->read_snapshot).detail("Now", parent->lastEpochEnd); + if (t->read_snapshot == parent->lastEpochEnd && //< So no transactions can fall between the read snapshot and the recovery transaction this (might) be merged with + // vvv and also the changes we will make in the recovery transaction (most notably to lastEpochEndKey) BEFORE we merge initialConfChanges won't conflict + !std::any_of(t->read_conflict_ranges.begin(), t->read_conflict_ranges.end(), [](KeyRangeRef const& r){return r.contains(lastEpochEndKey);})) + { + for(auto m = t->mutations.begin(); m != t->mutations.end(); ++m) { + TraceEvent("PM_CTM", parent->dbgid).detail("MType", m->type).detail("Param1", printable(m->param1)).detail("Param2", printable(m->param2)); + if (isMetadataMutation(*m)) { + // We keep the mutations and write conflict ranges from this transaction, but not its read conflict ranges + Standalone out; + out.read_snapshot = invalidVersion; + out.mutations.append_deep(out.arena(), t->mutations.begin(), t->mutations.size()); + out.write_conflict_ranges.append_deep(out.arena(), t->write_conflict_ranges.begin(), t->write_conflict_ranges.size()); + return out; + } + } + } + } + when ( ReplyPromise> req = waitNext( parent->provisionalProxies[0].getKeyServersLocations.getFuture() ) ) { + req.send(Never()); + } + when ( Void _ = wait( waitFailure ) ) { throw worker_removed(); } + } +} + +ACTOR Future recruitEverything( Reference self, vector* seedServers, Reference oldLogSystem ) { + if (!self->configuration.isValid()) { + RecoveryStatus::RecoveryStatus status; + if (self->configuration.initialized) + status = RecoveryStatus::configuration_invalid; + else if (!self->prevDBState.tLogs.size()) + status = RecoveryStatus::configuration_never_created; + else + status = RecoveryStatus::configuration_missing; + TraceEvent("MasterRecoveryState", self->dbgid) + .detail("StatusCode", status) + .detail("Status", RecoveryStatus::names[status]) + .trackLatest(format("%s/MasterRecoveryState", printable(self->dbName).c_str() ).c_str()); + return Never(); + } else + TraceEvent("MasterRecoveryState", self->dbgid) + .detail("StatusCode", RecoveryStatus::recruiting_transaction_servers) + .detail("Status", RecoveryStatus::names[RecoveryStatus::recruiting_transaction_servers]) + .detail("RequiredTLogs", self->configuration.tLogReplicationFactor) + .detail("DesiredTLogs", self->configuration.getDesiredLogs()) + .detail("RequiredProxies", 1) + .detail("DesiredProxies", self->configuration.getDesiredProxies()) + .detail("RequiredResolvers", 1) + .detail("DesiredResolvers", self->configuration.getDesiredResolvers()) + .detail("storeType", self->configuration.storageServerStoreType) + .trackLatest(format("%s/MasterRecoveryState", printable(self->dbName).c_str() ).c_str()); + + RecruitFromConfigurationReply recruits = wait( + brokenPromiseToNever( self->clusterController.recruitFromConfiguration.getReply( + RecruitFromConfigurationRequest( self->configuration ) ) ) ); + + TraceEvent("MasterRecoveryState", self->dbgid) + .detail("StatusCode", RecoveryStatus::initializing_transaction_servers) + .detail("Status", RecoveryStatus::names[RecoveryStatus::initializing_transaction_servers]) + .detail("Proxies", recruits.proxies.size()) + .detail("TLogs", recruits.tLogs.size()) + .detail("Resolvers", recruits.resolvers.size()) + .trackLatest(format("%s/MasterRecoveryState", printable(self->dbName).c_str() ).c_str()); + + // Actually, newSeedServers does both the recruiting and initialization of the seed servers; so if this is a brand new database we are sort of lying that we are + // past the recruitment phase. In a perfect world we would split that up so that the recruitment part happens above (in parallel with recruiting the transaction servers?). + + Void _ = wait( newProxies( self, recruits ) && newResolvers( self, recruits ) && newTLogServers( self, recruits, oldLogSystem ) && newSeedServers( self, seedServers ) ); + return Void(); +} + +ACTOR Future rewriteMasterState( Reference self ) { + state DBCoreState newState = self->prevDBState; + + newState.recoveryCount++; + try { + Void _ = wait( self->cstate1.setExclusive( BinaryWriter::toValue(newState, IncludeVersion()) ) ); + } catch (Error& e) { + TEST(true); // Master displaced during rewriteMasterState + throw; + } + + self->prevDBState = newState; + + Value rereadDBStateRaw = wait( self->cstate2.read() ); + DBCoreState readState; + if( rereadDBStateRaw.size() ) + readState = BinaryReader::fromStringRef(rereadDBStateRaw, IncludeVersion()); + + if( readState != newState ) { + TraceEvent("MasterTerminated", self->dbgid).detail("Reason", "CStateChanged"); + TEST(true); // Coordinated state changed between writing and reading, master dying + throw worker_removed(); + } + + return Void(); +} + +ACTOR Future readTransactionSystemState( Reference self, Reference oldLogSystem ) { + // Peek the txnStateTag in oldLogSystem and recover self->txnStateStore + + // For now, we also obtain the recovery metadata that the log system obtained during the end_epoch process for comparison + + // Sets self->lastEpochEnd and self->recoveryTransactionVersion + // Sets self->configuration to the configuration (FF/conf/ keys) at self->lastEpochEnd + + // Recover transaction state store + if(self->txnStateStore) self->txnStateStore->close(); + self->txnStateLogAdapter = openDiskQueueAdapter( oldLogSystem, txsTag ); + self->txnStateStore = keyValueStoreLogSystem( self->txnStateLogAdapter, self->dbgid, self->memoryLimit, false ); + + // Recover version info + self->lastEpochEnd = oldLogSystem->getEnd() - 1; + if (self->lastEpochEnd == 0) + self->recoveryTransactionVersion = 1; + else + self->recoveryTransactionVersion = self->lastEpochEnd + SERVER_KNOBS->MAX_VERSIONS_IN_FLIGHT; + + TraceEvent("MasterRecovering", self->dbgid).detail("lastEpochEnd", self->lastEpochEnd).detail("recoveryTransactionVersion", self->recoveryTransactionVersion); + + Standalone> rawConf = wait( self->txnStateStore->readRange( configKeys ) ); + self->configuration.fromKeyValues( rawConf ); + self->originalConfiguration = self->configuration; + TraceEvent("MasterRecoveredConfig", self->dbgid).detail("conf", self->configuration.toString()).trackLatest("RecoveredConfig"); + + //auto kvs = self->txnStateStore->readRange( systemKeys ); + //for( auto & kv : kvs.get() ) + // TraceEvent("MasterRecoveredTXS", self->dbgid).detail("K", printable(kv.key)).detail("V", printable(kv.value)); + + self->txnStateLogAdapter->setNextVersion( oldLogSystem->getEnd() ); //< FIXME: (1) the log adapter should do this automatically after recovery; (2) if we make KeyValueStoreMemory guarantee immediate reads, we should be able to get rid of the discardCommit() below and not need a writable log adapter + + TraceEvent("RTSSComplete", self->dbgid); + + return Void(); +} + +ACTOR Future sendInitialCommitToResolvers( Reference self ) { + state KeyRange txnKeys = allKeys; + state Sequence txnSequence = 0; + ASSERT(self->recoveryTransactionVersion); + + state Standalone> data = self->txnStateStore->readRange(txnKeys, BUGGIFY ? 3 : SERVER_KNOBS->DESIRED_TOTAL_BYTES, SERVER_KNOBS->DESIRED_TOTAL_BYTES).get(); + state vector> txnReplies; + state int64_t dataOutstanding = 0; + loop { + if(!data.size()) break; + ((KeyRangeRef&)txnKeys) = KeyRangeRef( keyAfter(data.back().key, txnKeys.arena()), txnKeys.end ); + Standalone> nextData = self->txnStateStore->readRange(txnKeys, BUGGIFY ? 3 : SERVER_KNOBS->DESIRED_TOTAL_BYTES, SERVER_KNOBS->DESIRED_TOTAL_BYTES).get(); + + for(auto& r : self->proxies) { + TxnStateRequest req; + req.arena = data.arena(); + req.data = data; + req.sequence = txnSequence; + req.last = !nextData.size(); + txnReplies.push_back( brokenPromiseToNever( r.txnState.getReply( req ) ) ); + dataOutstanding += data.arena().getSize(); + } + data = nextData; + txnSequence++; + + if(dataOutstanding > SERVER_KNOBS->MAX_TXS_SEND_MEMORY) { + Void _ = wait( waitForAll(txnReplies) ); + txnReplies = vector>(); + dataOutstanding = 0; + } + + Void _ = wait(yield()); + } + Void _ = wait( waitForAll(txnReplies) ); + + vector> replies; + for(auto& r : self->resolvers) { + ResolveTransactionBatchRequest req; + req.prevVersion = -1; + req.version = self->lastEpochEnd; + req.lastReceivedVersion = -1; + + replies.push_back( brokenPromiseToNever( r.resolve.getReply( req ) ) ); + } + + Void _ = wait(waitForAll(replies)); + return Void(); +} + +ACTOR Future triggerUpdates( Reference self, Reference oldLogSystem ) { + loop { + Void _ = wait( oldLogSystem->onLogSystemConfigChange() || self->fullyRecovered.getFuture() ); + if(self->fullyRecovered.isSet()) + return Void(); + + self->registrationTrigger.trigger(); + } +} + +ACTOR Future discardCommit(IKeyValueStore* store, LogSystemDiskQueueAdapter* adapter) { + state Future fcm = adapter->getCommitMessage(); + state Future committed = store->commit(); + LogSystemDiskQueueAdapter::CommitMessage cm = wait(fcm); + ASSERT(!committed.isReady()); + cm.acknowledge.send(Void()); + ASSERT(committed.isReady()); + return Void(); +} + +ACTOR Future recoverFrom( Reference self, Reference oldLogSystem, vector* seedServers, vector>* initialConfChanges ) { + TraceEvent("MasterRecoveryState", self->dbgid) + .detail("StatusCode", RecoveryStatus::reading_transaction_system_state) + .detail("Status", RecoveryStatus::names[RecoveryStatus::reading_transaction_system_state]) + .trackLatest(format("%s/MasterRecoveryState", printable(self->dbName).c_str() ).c_str()); + + if(BUGGIFY) + Void _ = wait( delay(10.0) ); + + Void _ = wait( readTransactionSystemState( self, oldLogSystem ) ); + for (auto& itr : *initialConfChanges) { + for(auto& m : itr.mutations) { + self->configuration.applyMutation( m ); + } + } + + debug_checkMaxRestoredVersion( UID(), self->lastEpochEnd, "DBRecovery" ); + + // Ordinarily we pass through this loop once and recover. We go around the loop if recovery stalls for more than a second, + // a provisional master is initialized, and an "emergency transaction" is submitted that might change the configuration so that we can + // finish recovery. + state Future recruitments = recruitEverything( self, seedServers, oldLogSystem ); + loop { + state Future> provisional = provisionalMaster(self, delay(1.0)); + + choose { + when (Void _ = wait( recruitments )) { + provisional.cancel(); + break; + } + when (Standalone _req = wait( provisional )) { + state Standalone req = _req; // mutable + TEST(true); // Emergency transaction processing during recovery + TraceEvent("EmergencyTransaction", self->dbgid); + for (auto m = req.mutations.begin(); m != req.mutations.end(); ++m) + TraceEvent("EmergencyTransactionMutation", self->dbgid).detail("MType", m->type).detail("P1", printable(m->param1)).detail("P2", printable(m->param2)); + + DatabaseConfiguration oldConf = self->configuration; + self->configuration = self->originalConfiguration; + for(auto& m : req.mutations) + self->configuration.applyMutation( m ); + + initialConfChanges->clear(); + initialConfChanges->push_back(req); + + if(self->configuration != oldConf) { //confChange does not trigger when including servers + recruitments = recruitEverything( self, seedServers, oldLogSystem ); + } + } + } + + provisional.cancel(); + } + + return Void(); +} + +ACTOR Future getVersion(Reference self, GetCommitVersionRequest req) { + state std::map::iterator proxyItr = self->lastProxyVersionReplies.find(req.requestingProxy); // lastProxyVersionReplies never changes + + if (proxyItr == self->lastProxyVersionReplies.end()) { + // Request from invalid proxy (e.g. from duplicate recruitment request) + req.reply.send(Never()); + return Void(); + } + + TEST(proxyItr->second.latestRequestNum.get() < req.requestNum - 1); // Commit version request queued up + Void _ = wait(proxyItr->second.latestRequestNum.whenAtLeast(req.requestNum-1)); + + auto itr = proxyItr->second.replies.find(req.requestNum); + if (itr != proxyItr->second.replies.end()) { + TEST(true); // Duplicate request for sequence + req.reply.send(itr->second); + } + else if(req.requestNum <= proxyItr->second.latestRequestNum.get()) { + TEST(true); // Old request for previously acknowledged sequence - may be impossible with current FlowTransport implementation + ASSERT( req.requestNum < proxyItr->second.latestRequestNum.get() ); // The latest request can never be acknowledged + req.reply.send(Never()); + } + else { + GetCommitVersionReply rep; + + if(self->version == invalidVersion) { + self->lastVersionTime = now(); + self->version = self->recoveryTransactionVersion; + rep.prevVersion = self->lastEpochEnd; + } + else { + double t1 = now(); + if(BUGGIFY) { + t1 = self->lastVersionTime; + } + rep.prevVersion = self->version; + self->version += std::max(1, std::min(SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS, int(SERVER_KNOBS->VERSIONS_PER_SECOND*(t1-self->lastVersionTime)))); + + TEST( self->version - rep.prevVersion == 1 ); // Minimum possible version gap + TEST( self->version - rep.prevVersion == SERVER_KNOBS->MAX_READ_TRANSACTION_LIFE_VERSIONS ); // Maximum possible version gap + self->lastVersionTime = t1; + + if(self->resolverNeedingChanges.count(req.requestingProxy)) { + rep.resolverChanges = self->resolverChanges.get(); + rep.resolverChangesVersion = self->resolverChangesVersion; + self->resolverNeedingChanges.erase(req.requestingProxy); + + if(self->resolverNeedingChanges.empty()) + self->resolverChanges.set(Standalone>()); + } + } + + rep.version = self->version; + rep.requestNum = req.requestNum; + + proxyItr->second.replies.erase(proxyItr->second.replies.begin(), proxyItr->second.replies.upper_bound(req.mostRecentProcessedRequestNum)); + proxyItr->second.replies[req.requestNum] = rep; + ASSERT(rep.prevVersion >= 0); + req.reply.send(rep); + + ASSERT(proxyItr->second.latestRequestNum.get() == req.requestNum - 1); + proxyItr->second.latestRequestNum.set(req.requestNum); + } + + return Void(); +} + +ACTOR Future provideVersions(Reference self) { + state ActorCollection versionActors(false); + + for (auto& p : self->proxies) + self->lastProxyVersionReplies[p.id()] = ProxyVersionReplies(); + + loop { + choose { + when(GetCommitVersionRequest req = waitNext(self->myInterface.getCommitVersion.getFuture())) { + versionActors.add(getVersion(self, req)); + } + when(Void _ = wait(versionActors.getResult())) { } + } + } +} + +std::pair findRange( CoalescedKeyRangeMap& key_resolver, Standalone>& movedRanges, int src, int dest ) { + auto ranges = key_resolver.ranges(); + auto prev = ranges.begin(); + auto it = ranges.begin(); + ++it; + if(it==ranges.end()) { + if(ranges.begin().value() != src || std::find(movedRanges.begin(), movedRanges.end(), ResolverMoveRef(ranges.begin()->range(), dest)) != movedRanges.end()) + throw operation_failed(); + return std::make_pair(ranges.begin().range(), true); + } + + std::set borders; + //If possible expand an existing boundary between the two resolvers + for(; it != ranges.end(); ++it) { + if(it->value() == src && prev->value() == dest && std::find(movedRanges.begin(), movedRanges.end(), ResolverMoveRef(it->range(), dest)) == movedRanges.end()) { + return std::make_pair(it->range(), true); + } + if(it->value() == dest && prev->value() == src && std::find(movedRanges.begin(), movedRanges.end(), ResolverMoveRef(prev->range(), dest)) == movedRanges.end()) { + return std::make_pair(prev->range(), false); + } + if(it->value() == dest) + borders.insert(prev->value()); + if(prev->value() == dest) + borders.insert(it->value()); + ++prev; + } + + prev = ranges.begin(); + it = ranges.begin(); + ++it; + //If possible create a new boundry which doesn't exist yet + for(; it != ranges.end(); ++it) { + if(it->value() == src && !borders.count(prev->value()) && std::find(movedRanges.begin(), movedRanges.end(), ResolverMoveRef(it->range(), dest)) == movedRanges.end()) { + return std::make_pair(it->range(), true); + } + if(prev->value() == src && !borders.count(it->value()) && std::find(movedRanges.begin(), movedRanges.end(), ResolverMoveRef(prev->range(), dest)) == movedRanges.end()) { + return std::make_pair(prev->range(), false); + } + ++prev; + } + + it = ranges.begin(); + for(; it != ranges.end(); ++it) { + if(it->value() == src && std::find(movedRanges.begin(), movedRanges.end(), ResolverMoveRef(it->range(), dest)) == movedRanges.end()) { + return std::make_pair(it->range(), true); + } + } + throw operation_failed(); //we are already attempting to move all of the data one resolver is assigned, so do not move anything +} + +ACTOR Future resolutionBalancing(Reference self) { + state CoalescedKeyRangeMap key_resolver; + key_resolver.insert(allKeys, 0); + loop { + Void _ = wait(delay(SERVER_KNOBS->MIN_BALANCE_TIME, TaskResolutionMetrics)); + while(self->resolverChanges.get().size()) + Void _ = wait(self->resolverChanges.onChange()); + state std::vector> futures; + for (auto& p : self->resolvers) + futures.push_back(brokenPromiseToNever(p.metrics.getReply(ResolutionMetricsRequest(), TaskResolutionMetrics))); + Void _ = wait( waitForAll(futures) ); + state IndexedSet, NoMetric> metrics; + + int64_t total = 0; + for (int i = 0; i < futures.size(); i++) { + total += futures[i].get(); + metrics.insert(std::make_pair(futures[i].get(), i), NoMetric()); + //TraceEvent("ResolverMetric").detail("i", i).detail("metric", futures[i].get()); + } + if( metrics.lastItem()->first - metrics.begin()->first > SERVER_KNOBS->MIN_BALANCE_DIFFERENCE ) { + try { + state int src = metrics.lastItem()->second; + state int dest = metrics.begin()->second; + state int64_t amount = std::min( metrics.lastItem()->first - total/self->resolvers.size(), total/self->resolvers.size() - metrics.begin()->first ) / 2; + state Standalone> movedRanges; + + loop { + state std::pair range = findRange( key_resolver, movedRanges, src, dest ); + + ResolutionSplitRequest req; + req.front = range.second; + req.offset = amount; + req.range = range.first; + + ResolutionSplitReply split = wait( brokenPromiseToNever(self->resolvers[metrics.lastItem()->second].split.getReply(req, TaskResolutionMetrics)) ); + KeyRangeRef moveRange = range.second ? KeyRangeRef( range.first.begin, split.key ) : KeyRangeRef( split.key, range.first.end ); + movedRanges.push_back_deep(movedRanges.arena(), ResolverMoveRef(moveRange, dest)); + TraceEvent("MovingResolutionRange").detail("src", src).detail("dest", dest).detail("amount", amount).detail("startRange", printable(range.first)).detail("moveRange", printable(moveRange)).detail("used", split.used).detail("KeyResolverRanges", key_resolver.size()); + amount -= split.used; + if(moveRange != range.first || amount <= 0 ) + break; + } + for(auto& it : movedRanges) + key_resolver.insert(it.range, it.dest); + //for(auto& it : key_resolver.ranges()) + // TraceEvent("KeyResolver").detail("range", printable(it.range())).detail("value", it.value()); + + self->resolverChangesVersion = self->version + 1; + for (auto& p : self->proxies) + self->resolverNeedingChanges.insert(p.id()); + self->resolverChanges.set(movedRanges); + } catch( Error&e ) { + if(e.code() != error_code_operation_failed) + throw; + } + } + } +} + +static std::set const& normalMasterErrors() { + static std::set s; + if (s.empty()) { + s.insert( error_code_tlog_stopped ); + s.insert( error_code_master_tlog_failed ); + s.insert( error_code_master_proxy_failed ); + s.insert( error_code_master_resolver_failed ); + s.insert( error_code_recruitment_failed ); + s.insert( error_code_no_more_servers ); + s.insert( error_code_master_recovery_failed ); + s.insert( error_code_coordinated_state_conflict ); + s.insert( error_code_movekeys_conflict ); + s.insert( error_code_master_max_versions_in_flight ); + s.insert( error_code_worker_removed ); + s.insert( error_code_new_coordinators_timed_out ); + } + return s; +} + +ACTOR Future changeCoordinators( Reference self, bool skippedTransition ) { + Void _ = wait( self->fullyRecovered.getFuture() ); + + loop { + ChangeCoordinatorsRequest req = waitNext( self->myInterface.changeCoordinators.getFuture() ); + state ChangeCoordinatorsRequest changeCoordinatorsRequest = req; + try { + if( skippedTransition ) { + Void _ = wait( self->cstate2.move( ClusterConnectionString( changeCoordinatorsRequest.newConnectionString.toString() ) ) ); + } else { + Void _ = wait( self->cstate3.move( ClusterConnectionString( changeCoordinatorsRequest.newConnectionString.toString() ) ) ); + } + } + catch(Error &e) { + if(e.code() != error_code_actor_cancelled) + changeCoordinatorsRequest.reply.sendError(e); + + throw; + } + + throw internal_error(); + } +} + +ACTOR Future rejoinRequestHandler( Reference self ) { + loop { + TLogRejoinRequest req = waitNext( self->myInterface.tlogRejoin.getFuture() ); + req.reply.send(true); + } +} + +ACTOR Future trackTlogRecovery( Reference self, Reference>> oldLogSystems, bool skipTransition ) { + state Future rejoinRequests = Never(); + + loop { + DBCoreState coreState; + self->logSystem->toCoreState( coreState ); + if( !self->fullyRecovered.isSet() && !coreState.oldTLogData.size() ) { + if( !skipTransition ) { + Void _ = wait( writeRecoveredMasterState(self) ); + self->registrationTrigger.trigger(); + } + TraceEvent("MasterFullyRecovered", self->dbgid); + oldLogSystems->get()->stopRejoins(); + rejoinRequests = rejoinRequestHandler(self); + self->fullyRecovered.send(Void()); + } + + Void _ = wait( self->logSystem->onCoreStateChanged() ); + } +} + +ACTOR Future masterCore( Reference self, PromiseStream> addActor ) +{ + state TraceInterval recoveryInterval("MasterRecovery"); + + addActor.send( waitFailureServer(self->myInterface.waitFailure.getFuture()) ); + + TraceEvent( recoveryInterval.begin(), self->dbgid ); + + self->recoveryState = RecoveryState::READING_CSTATE; + TraceEvent("MasterRecoveryState", self->dbgid) + .detail("StatusCode", RecoveryStatus::reading_coordinated_state) + .detail("Status", RecoveryStatus::names[RecoveryStatus::reading_coordinated_state]) + .trackLatest(format("%s/MasterRecoveryState", printable(self->dbName).c_str() ).c_str()); + + Value prevDBStateRaw = wait( self->cstate1.read() ); + addActor.send( masterTerminateOnConflict( self, self->cstate1.onConflict() ) ); + + if( prevDBStateRaw.size() ) + self->prevDBState = BinaryReader::fromStringRef(prevDBStateRaw, IncludeVersion()); + + self->recoveryState = RecoveryState::LOCKING_CSTATE; + TraceEvent("MasterRecoveryState", self->dbgid) + .detail("StatusCode", RecoveryStatus::locking_coordinated_state) + .detail("Status", RecoveryStatus::names[RecoveryStatus::locking_coordinated_state]) + .detail("TLogs", self->prevDBState.tLogs.size()) + .detail("MyRecoveryCount", self->prevDBState.recoveryCount+2) + .detail("StateSize", prevDBStateRaw.size()) + .trackLatest(format("%s/MasterRecoveryState", printable(self->dbName).c_str() ).c_str()); + + state Reference>> oldLogSystems( new AsyncVar> ); + state Future recoverAndEndEpoch = ILogSystem::recoverAndEndEpoch(oldLogSystems, self->dbgid, self->prevDBState, self->myInterface.tlogRejoin.getFuture(), self->myInterface.locality); + + Void _ = wait( rewriteMasterState( self ) || recoverAndEndEpoch ); + + self->recoveryState = RecoveryState::RECRUITING; + + addActor.send( masterTerminateOnConflict( self, self->cstate2.onConflict() ) ); + + state vector seedServers; + state vector> initialConfChanges; + state Future logChanges; + + TraceEvent("MasterRecoveryState", self->dbgid) + .detail("StatusCode", RecoveryStatus::locking_old_transaction_servers) + .detail("Status", RecoveryStatus::names[RecoveryStatus::locking_old_transaction_servers]) + .trackLatest(format("%s/MasterRecoveryState", printable(self->dbName).c_str() ).c_str()); + + loop { + Reference oldLogSystem = oldLogSystems->get(); + if(oldLogSystem) logChanges = triggerUpdates(self, oldLogSystem); + + state Future reg = oldLogSystem ? updateRegistration(self, oldLogSystem) : Never(); + self->registrationTrigger.trigger(); + + choose { + when (Void _ = wait( oldLogSystem ? recoverFrom(self, oldLogSystem, &seedServers, &initialConfChanges) : Never() )) { reg.cancel(); break; } + when (Void _ = wait( oldLogSystems->onChange() )) {} + when (Void _ = wait( reg )) { throw internal_error(); } + when (Void _ = wait( recoverAndEndEpoch )) {} + } + } + + recoverAndEndEpoch.cancel(); + + ASSERT( self->proxies.size() <= self->configuration.getDesiredProxies() ); + ASSERT( self->resolvers.size() <= self->configuration.getDesiredResolvers() ); + + self->recoveryState = RecoveryState::RECOVERY_TRANSACTION; + TraceEvent("MasterRecoveryState", self->dbgid) + .detail("StatusCode", RecoveryStatus::recovery_transaction) + .detail("Status", RecoveryStatus::names[RecoveryStatus::recovery_transaction]) + .trackLatest(format("%s/MasterRecoveryState", printable(self->dbName).c_str() ).c_str()); + + // Recovery transaction + state bool debugResult = debug_checkMinRestoredVersion( UID(), self->lastEpochEnd, "DBRecovery", SevWarn ); + + CommitTransactionRequest recoveryCommitRequest; + recoveryCommitRequest.isLockAware = true; + CommitTransactionRef &tr = recoveryCommitRequest.transaction; + int mmApplied = 0; // The number of mutations in tr.mutations that have been applied to the txnStateStore so far + if (self->lastEpochEnd != 0) { + // This transaction sets \xff/lastEpochEnd, which the shard servers can use to roll back speculatively + // processed semi-committed transactions from the previous epoch. + // It also guarantees the shard servers and tlog servers eventually get versions in the new epoch, which + // clients might rely on. + // This transaction is by itself in a batch (has its own version number), which simplifies storage servers slightly (they assume there are no modifications to serverKeys in the same batch) + // The proxy also expects the lastEpochEndKey mutation to be first in the transaction + BinaryWriter bw(Unversioned()); + tr.set(recoveryCommitRequest.arena, lastEpochEndKey, (bw << self->lastEpochEnd).toStringRef()); + } else { + // Recruit and seed initial shard servers + // This transaction must be the very first one in the database (version 1) + seedShardServers(recoveryCommitRequest.arena, tr, seedServers); + } + // initialConfChanges have not been conflict checked against any earlier writes in the recovery transaction, so do this as early as possible in the recovery transaction + // but see above comments as to why it can't be absolutely first. Theoretically emergency transactions should conflict check against the lastEpochEndKey. + for (auto& itr : initialConfChanges) { + tr.mutations.append_deep(recoveryCommitRequest.arena, itr.mutations.begin(), itr.mutations.size()); + tr.write_conflict_ranges.append_deep(recoveryCommitRequest.arena, itr.write_conflict_ranges.begin(), itr.write_conflict_ranges.size()); + } + + tr.set(recoveryCommitRequest.arena, backupVersionKey, backupVersionValue); + tr.set(recoveryCommitRequest.arena, coordinatorsKey, self->coordinators.ccf->getConnectionString().toString()); + tr.set(recoveryCommitRequest.arena, logsKey, self->logSystem->getLogsValue()); + + //FIXME: upgrade code for 4.4, remove for 4.5 + tr.clear(recoveryCommitRequest.arena, KeyRangeRef(LiteralStringRef("\xff/status/"), LiteralStringRef("\xff/status0"))); + tr.clear(recoveryCommitRequest.arena, KeyRangeRef(LiteralStringRef("\xff/backupstatus/"), LiteralStringRef("\xff/backupstatus0"))); + tr.clear(recoveryCommitRequest.arena, KeyRangeRef(LiteralStringRef("\xff/backup-agent/"), LiteralStringRef("\xff/backup-agent0"))); + tr.clear(recoveryCommitRequest.arena, KeyRangeRef(LiteralStringRef("\xff/db-backup-agent/"), LiteralStringRef("\xff/db-backup-agent0"))); + tr.clear(recoveryCommitRequest.arena, KeyRangeRef(LiteralStringRef("\xff/cplog/"), LiteralStringRef("\xff/cplog0"))); + tr.clear(recoveryCommitRequest.arena, KeyRangeRef(LiteralStringRef("\xff/bklog/"), LiteralStringRef("\xff/bklog0"))); + + applyMetadataMutations(self->dbgid, recoveryCommitRequest.arena, tr.mutations.slice(mmApplied, tr.mutations.size()), self->txnStateStore, NULL, NULL); + mmApplied = tr.mutations.size(); + + tr.read_snapshot = self->recoveryTransactionVersion; // lastEpochEnd would make more sense, but isn't in the initial window of the resolver(s) + + TraceEvent("MasterRecoveryCommit", self->dbgid); + state Future> recoveryCommit = self->proxies[0].commit.tryGetReply(recoveryCommitRequest); + state Future tlogFailure = self->logSystem->onError(); + state Future resolverFailure = waitResolverFailure( self->resolvers ); + state Future proxyFailure = waitProxyFailure( self->proxies ); + state Future providingVersions = provideVersions(self); + + addActor.send( reportErrors(updateRegistration(self, self->logSystem), "updateRegistration", self->dbgid) ); + self->registrationTrigger.trigger(); + + Void _ = wait(discardCommit(self->txnStateStore, self->txnStateLogAdapter)); + + // Wait for the recovery transaction to complete. + // SOMEDAY: For faster recovery, do this and setDBState asynchronously and don't wait for them + // unless we want to change TLogs + Void _ = wait((success(recoveryCommit) && sendInitialCommitToResolvers(self)) || tlogFailure || resolverFailure || proxyFailure ); + if(recoveryCommit.isReady() && recoveryCommit.get().isError()) { + TEST(true); // Master recovery failed because of the initial commit failed + throw master_recovery_failed(); + } + + ASSERT( self->recoveryTransactionVersion != 0 ); + + self->recoveryState = RecoveryState::WRITING_CSTATE; + TraceEvent("MasterRecoveryState", self->dbgid) + .detail("StatusCode", RecoveryStatus::writing_coordinated_state) + .detail("Status", RecoveryStatus::names[RecoveryStatus::writing_coordinated_state]) + .detail("TLogs", self->logSystem->getLogServerCount()) + .detail("TLogList", self->logSystem->describe()) + .trackLatest(format("%s/MasterRecoveryState", printable(self->dbName).c_str() ).c_str()); + + // Multiple masters prevent conflicts between themselves via CoordinatedState (self->cstate) + // 1. If SetMaster succeeds, then by CS's contract, these "new" Tlogs are the immediate + // successors of the "old" ones we are replacing + // 2. logSystem->recoverAndEndEpoch ensured that a co-quorum of the "old" tLogs were stopped at + // versions <= self->lastEpochEnd, so no versions > self->lastEpochEnd could be (fully) committed to them. + // 3. No other master will attempt to commit anything to our "new" Tlogs + // because it didn't recruit them + // 4. Therefore, no full commit can come between self->lastEpochEnd and the first commit + // we made to the new Tlogs (self->recoveryTransactionVersion), and only our own semi-commits can come between our + // first commit and the next new TLogs + + DBCoreState coreState; + self->logSystem->toCoreState( coreState ); + state bool skipTransition = !coreState.oldTLogData.size(); + + debug_advanceMaxCommittedVersion(UID(), self->recoveryTransactionVersion); + Void _ = wait( writeTransitionMasterState( self, skipTransition ) ); + debug_advanceMinCommittedVersion(UID(), self->recoveryTransactionVersion); + + if( !skipTransition ) + addActor.send( masterTerminateOnConflict( self, self->cstate3.onConflict() ) ); + + if( debugResult ) + TraceEvent(SevError, "DBRecoveryDurabilityError"); + + TraceEvent("MasterCommittedTLogs", self->dbgid).detail("TLogs", self->logSystem->describe()).detail("RecoveryCount", self->myDBState.get().recoveryCount).detail("RecoveryTransactionVersion", self->recoveryTransactionVersion); + + TraceEvent(recoveryInterval.end(), self->dbgid).detail("RecoveryTransactionVersion", self->recoveryTransactionVersion); + + self->recoveryState = RecoveryState::FULLY_RECOVERED; + TraceEvent("MasterRecoveryState", self->dbgid) + .detail("StatusCode", RecoveryStatus::fully_recovered) + .detail("Status", RecoveryStatus::names[RecoveryStatus::fully_recovered]) + .detail("storeType", self->configuration.storageServerStoreType) + .trackLatest(format("%s/MasterRecoveryState", printable(self->dbName).c_str() ).c_str()); + + // Now that recovery is complete, we register ourselves with the cluster controller, so that the client and server information + // it hands out can be updated + self->registrationTrigger.trigger(); + + // Now that the master is recovered we can start auxiliary services that happen to run here + { + PromiseStream< std::pair> > ddStorageServerChanges; + state double lastLimited = 0; + addActor.send( reportErrorsExcept( dataDistribution( self->dbInfo, self->myInterface, self->configuration, ddStorageServerChanges, self->logSystem, self->recoveryTransactionVersion, &lastLimited ), "DataDistribution", self->dbgid, &normalMasterErrors() ) ); + addActor.send( reportErrors( rateKeeper( self->dbInfo, ddStorageServerChanges, self->myInterface.getRateInfo.getFuture(), self->dbName, self->configuration, &lastLimited ), "Ratekeeper", self->dbgid) ); + } + + if( self->resolvers.size() > 1 ) + addActor.send( resolutionBalancing(self) ); + + addActor.send( changeCoordinators(self, skipTransition) ); + addActor.send( trackTlogRecovery(self, oldLogSystems, skipTransition) ); + + loop choose { + when( Void _ = wait( tlogFailure ) ) { throw internal_error(); } + when( Void _ = wait( proxyFailure ) ) { throw internal_error(); } + when( Void _ = wait( resolverFailure ) ) { throw internal_error(); } + when (Void _ = wait(providingVersions)) { throw internal_error(); } + } +} + +ACTOR Future masterServer( MasterInterface mi, Reference> db, ServerCoordinators coordinators, LifetimeToken lifetime ) +{ + state PromiseStream> addActor; + state Future collection = actorCollection( addActor.getFuture() ); + + state Future onDBChange = Void(); + state Reference self( new MasterData( db, mi, coordinators, db->get().clusterInterface, db->get().dbName, LiteralStringRef("") ) ); + + TEST( !lifetime.isStillValid( db->get().masterLifetime, mi.id()==db->get().master.id() ) ); // Master born doomed + TraceEvent("MasterLifetime", self->dbgid).detail("LifetimeToken", lifetime.toString()); + + try { + state Future core = masterCore( self, addActor ); + loop choose { + when (Void _ = wait( core )) { break; } + when (Void _ = wait( onDBChange )) { + onDBChange = db->onChange(); + if (!lifetime.isStillValid( db->get().masterLifetime, mi.id()==db->get().master.id() )) { + TraceEvent("MasterTerminated", mi.id()).detail("Reason", "LifetimeToken").detail("MyToken", lifetime.toString()).detail("CurrentToken", db->get().masterLifetime.toString()); + TEST(true); // Master replaced, dying + if (BUGGIFY) Void _ = wait( delay(5) ); + throw worker_removed(); + } + } + when (Void _ = wait(collection) ) { ASSERT(false); throw internal_error(); } + } + } catch (Error& e) { + TEST(e.code() == error_code_master_tlog_failed); // Master: terminated because of a tLog failure + TEST(e.code() == error_code_master_proxy_failed); // Master: terminated because of a proxy failure + TEST(e.code() == error_code_master_resolver_failed); // Master: terminated because of a resolver failure + + if (normalMasterErrors().count(e.code())) + { + TraceEvent("MasterTerminated", mi.id()).error(e); + return Void(); + } + throw; + } + return Void(); +} diff --git a/fdbserver/networktest.actor.cpp b/fdbserver/networktest.actor.cpp new file mode 100644 index 0000000000..4d09a255f1 --- /dev/null +++ b/fdbserver/networktest.actor.cpp @@ -0,0 +1,159 @@ +/* + * networktest.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "NetworkTest.h" + +UID WLTOKEN_NETWORKTEST( -1, 2 ); + +NetworkTestInterface::NetworkTestInterface( NetworkAddress remote ) + : test( Endpoint(remote, WLTOKEN_NETWORKTEST) ) +{ +} + +NetworkTestInterface::NetworkTestInterface( INetwork* local ) +{ + test.makeWellKnownEndpoint( WLTOKEN_NETWORKTEST, TaskDefaultEndpoint ); +} + +ACTOR Future networkTestServer() { + state NetworkTestInterface interf( g_network ); + state Future logging = delay( 1.0 ); + state double lastTime = now(); + state int sent = 0; + + loop { + choose { + when( NetworkTestRequest req = waitNext( interf.test.getFuture() ) ) { + req.reply.send( NetworkTestReply( Value( std::string( req.replySize, '.' ) ) ) ); + sent++; + } + when( Void _ = wait( logging ) ) { + auto spd = sent / (now() - lastTime); + fprintf( stderr, "responses per second: %f (%f us)\n", spd, 1e6/spd ); + lastTime = now(); + sent = 0; + logging = delay( 1.0 ); + } + } + } +} + +ACTOR Future testClient( std::vector interfs, int* sent ) { + state double lastTime = now(); + + loop { + NetworkTestReply rep = wait( retryBrokenPromise(interfs[g_random->randomInt(0, interfs.size())].test, NetworkTestRequest( LiteralStringRef("."), 600000 ) ) ); + (*sent)++; + } +} + +ACTOR Future logger( int* sent ) { + state double lastTime = now(); + loop { + Void _ = wait( delay(1.0) ); + auto spd = *sent / (now() - lastTime); + fprintf( stderr, "messages per second: %f\n", spd); + lastTime = now(); + *sent = 0; + } +} + +static void networkTestnanosleep() +{ + printf("nanosleep speed test\n"); + +#ifdef __linux__ + printf("\nnanosleep(10) latencies:"); + for (int i = 0; i < 10; i++) { + + double before = timer_monotonic(); + timespec tv; + tv.tv_sec = 0; + tv.tv_nsec = 10; + nanosleep(&tv, NULL); + double after = timer_monotonic(); + + printf(" %0.3lf", (after - before)*1e6); + } + + printf("\nnanosleep(10) latency after 5ms spin:"); + for (int i = 0; i < 10; i++) { + double a = timer_monotonic() + 5e-3; + while (timer_monotonic() < a) 0; + + double before = timer_monotonic(); + timespec tv; + tv.tv_sec = 0; + tv.tv_nsec = 10; + nanosleep(&tv, NULL); + double after = timer_monotonic(); + + printf(" %0.3lf", (after - before)*1e6); + } + + printf("\nnanosleep(20000) latency:"); + for (int i = 0; i < 10; i++) { + double before = timer_monotonic(); + timespec tv; + tv.tv_sec = 0; + tv.tv_nsec = 20000; + nanosleep(&tv, NULL); + double after = timer_monotonic(); + + printf(" %0.3lf", (after - before)*1e6); + } + printf("\n"); + + printf("nanosleep(20000) loop\n"); + while (true) { + timespec tv; + tv.tv_sec = 0; + tv.tv_nsec = 20000; + nanosleep(&tv, NULL); + } +#endif + + return; +} + +ACTOR Future networkTestClient( std:: string testServers ) { + if (testServers == "nanosleep") { + networkTestnanosleep(); + //return Void(); + } + + + state std::vector interfs; + state std::vector servers = NetworkAddress::parseList(testServers); + state int sent = 0; + + for( int i = 0; i < servers.size(); i++ ) { + interfs.push_back( NetworkTestInterface( servers[i] ) ); + } + + state std::vector> clients; + for( int i = 0; i < 30; i++ ) + clients.push_back( testClient( interfs, &sent ) ); + clients.push_back( logger( &sent ) ); + + Void _ = wait( waitForAll( clients ) ); + return Void(); +} \ No newline at end of file diff --git a/fdbserver/pubsub.actor.cpp b/fdbserver/pubsub.actor.cpp new file mode 100644 index 0000000000..1fb84a1a9b --- /dev/null +++ b/fdbserver/pubsub.actor.cpp @@ -0,0 +1,512 @@ +/* + * pubsub.actor.cpp + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "flow/actorcompiler.h" +#include "fdbclient/NativeAPI.h" +#include "pubsub.h" + +Value uInt64ToValue( uint64_t v ) { + return StringRef(format("%016llx", v)); +} +uint64_t valueToUInt64( const StringRef& v ) { + uint64_t x = 0; + sscanf( v.toString().c_str(), "%llx", &x ); + return x; +} + +Key keyForInbox(uint64_t inbox) { + return StringRef(format("i/%016llx", inbox)); +} +Key keyForInboxSubcription(uint64_t inbox, uint64_t feed) { + return StringRef(format("i/%016llx/subs/%016llx", inbox, feed)); +} +Key keyForInboxSubcriptionCount(uint64_t inbox) { + return StringRef(format("i/%016llx/subsCnt", inbox)); +} +Key keyForInboxStalePrefix(uint64_t inbox) { + return StringRef(format("i/%016llx/stale/", inbox)); +} +Key keyForInboxStaleFeed(uint64_t inbox, uint64_t feed) { + return StringRef(format("i/%016llx/stale/%016llx", inbox, feed)); +} +Key keyForInboxCacheByIDPrefix(uint64_t inbox) { + return StringRef(format("i/%016llx/cid/", inbox)); +} +Key keyForInboxCacheByID(uint64_t inbox, uint64_t messageId) { + return StringRef(format("i/%016llx/cid/%016llx", inbox, messageId)); +} +Key keyForInboxCacheByFeedPrefix(uint64_t inbox) { + return StringRef(format("i/%016llx/cf/", inbox)); +} +Key keyForInboxCacheByFeed(uint64_t inbox, uint64_t feed) { + return StringRef(format("i/%016llx/cf/%016llx", inbox, feed)); +} + +Key keyForFeed(uint64_t feed) { + return StringRef(format("f/%016llx", feed)); +} +Key keyForFeedSubcriber(uint64_t feed, uint64_t inbox) { + return StringRef(format("f/%016llx/subs/%016llx", feed, inbox)); +} +Key keyForFeedSubcriberCount(uint64_t feed) { + return StringRef(format("f/%016llx/subscCnt", feed)); +} +Key keyForFeedMessage(uint64_t feed, uint64_t message) { + return StringRef(format("f/%016llx/m/%016llx", feed, message)); +} +Key keyForFeedMessagePrefix(uint64_t feed) { + return StringRef(format("f/%016llx/m/", feed)); +} +Key keyForFeedMessageCount(uint64_t feed) { + return StringRef(format("f/%016llx/messCount", feed)); +} +// the following should go at some point: change over to range query of count 1 from feed message list +Key keyForFeedLatestMessage(uint64_t feed) { + return StringRef(format("f/%016llx/latestMessID", feed)); +} +Key keyForFeedWatcherPrefix(uint64_t feed) { + return StringRef(format("f/%016llx/watchers/", feed)); +} +Key keyForFeedWatcher(uint64_t feed, uint64_t inbox) { + return StringRef(format("f/%016llx/watchers/%016llx", feed, inbox)); +} + +Standalone messagePrefix(LiteralStringRef("m/")); + +Key keyForMessage(uint64_t message) { + return StringRef(format("m/%016llx", message)); +} + +Key keyForDisptchEntry(uint64_t message) { + return StringRef(format("d/%016llx", message)); +} + +PubSub::PubSub(Database _cx) + : cx(_cx) +{ +} + +ACTOR Future _createFeed(Database cx, Standalone metadata) { + state uint64_t id(g_random->randomUniqueID().first()); // SOMEDAY: this should be an atomic increment + TraceEvent("PubSubCreateFeed").detail("Feed", id); + state Transaction tr(cx); + loop { + try { + state Optional val = wait(tr.get(keyForFeed(id))); + while(val.present()) { + id = id + g_random->randomInt(1, 100); + Optional v = wait(tr.get(keyForFeed(id))); + val = v; + } + tr.set(keyForFeed(id), metadata); + tr.set(keyForFeedSubcriberCount(id), uInt64ToValue(0)); + tr.set(keyForFeedMessageCount(id), uInt64ToValue(0)); + Void _ = wait(tr.commit()); + break; + } catch(Error& e) { + Void _ = wait( tr.onError(e) ); + } + } + return id; +} + +Future PubSub::createFeed(Standalone metadata) { + return _createFeed(cx, metadata); +} + +ACTOR Future _createInbox(Database cx, Standalone metadata) { + state uint64_t id = g_random->randomUniqueID().first(); + TraceEvent("PubSubCreateInbox").detail("Inbox", id); + state Transaction tr(cx); + loop { + try { + state Optional val = wait(tr.get(keyForInbox(id))); + while(val.present()) { + id += g_random->randomInt(1, 100); + Optional v = wait(tr.get(keyForFeed(id))); + val = v; + } + tr.set(keyForInbox(id), metadata); + tr.set(keyForInboxSubcriptionCount(id), uInt64ToValue(0)); + Void _ = wait( tr.commit() ); + break; + } catch(Error& e) { + Void _ = wait( tr.onError(e) ); + } + } + return id; +} + +Future PubSub::createInbox(Standalone metadata) { + return _createInbox(cx, metadata); +} + +ACTOR Future _createSubcription(Database cx, uint64_t feed, uint64_t inbox) { + state Transaction tr(cx); + TraceEvent("PubSubCreateSubscription").detail("Feed", feed).detail("Inbox", inbox); + loop { + try { + Optional subcription = wait(tr.get(keyForInboxSubcription(inbox, feed))); + if(subcription.present()) { + // For idempotency, this could exist from a previous transaction from us that succeeded + return true; + } + Optional inboxVal = wait(tr.get(keyForInbox(inbox))); + if(!inboxVal.present()) { + return false; + } + Optional feedVal = wait(tr.get(keyForFeed(feed))); + if(!feedVal.present()) { + return false; + } + + // Update the subscriptions of the inbox + Optional subcriptionCountVal = wait(tr.get(keyForInboxSubcriptionCount(inbox))); + uint64_t subcriptionCount = valueToUInt64(subcriptionCountVal.get()); // throws if count not present + tr.set(keyForInboxSubcription(inbox, feed), StringRef()); + tr.set(keyForInboxSubcriptionCount(inbox), uInt64ToValue(subcriptionCount + 1)); + + // Update the subcribers of the feed + Optional subcriberCountVal = wait(tr.get(keyForFeedSubcriberCount(feed))); + uint64_t subcriberCount = valueToUInt64(subcriberCountVal.get()); // throws if count not present + tr.set(keyForFeedSubcriber(feed, inbox), StringRef()); + tr.set(keyForFeedSubcriberCount(inbox), uInt64ToValue(subcriberCount + 1)); + + // Add inbox as watcher of feed. + tr.set(keyForFeedWatcher(feed, inbox), StringRef()); + + Void _ = wait( tr.commit() ); + break; + } catch(Error& e) { + Void _ = wait( tr.onError(e) ); + } + } + return true; +} + +Future PubSub::createSubcription(uint64_t feed, uint64_t inbox) { + return _createSubcription(cx, feed, inbox); +} + +// Since we are not relying on "read-your-own-writes", we need to keep track of +// the highest-numbered inbox that we've cleared from the watchers list and +// make sure that further requests start after this inbox. +ACTOR Future updateFeedWatchers(Transaction *tr, uint64_t feed) { + state StringRef watcherPrefix = keyForFeedWatcherPrefix(feed); + state uint64_t highestInbox; + state bool first = true; + loop { + // Grab watching inboxes in swaths of 100 + state Standalone watchingInboxes = wait( (*tr).getRange( + firstGreaterOrEqual(keyForFeedWatcher(feed, first ? 0 : highestInbox + 1)), + firstGreaterOrEqual(keyForFeedWatcher(feed, UINT64_MAX)), 100 ) ); // REVIEW: does 100 make sense? + if(!watchingInboxes.size()) + // If there are no watchers, return. + return Void(); + first = false; + state int idx = 0; + for(; idx < watchingInboxes.size(); idx++) { + KeyRef key = watchingInboxes[idx].key; + StringRef inboxStr = key.removePrefix(watcherPrefix); + uint64_t inbox = valueToUInt64(inboxStr); + // add this feed to the stale list of inbox + (*tr).set(keyForInboxStaleFeed(inbox, feed), StringRef()); + // remove the inbox from the list of watchers on this feed + (*tr).clear(key); + highestInbox = inbox; + } + if(watchingInboxes.size() < 100) + // If there were fewer watchers returned that we asked for, we're done. + return Void(); + } +} + +/* + * Posts a message to a feed. This updates the list of stale feeds to all watchers. + * Return: a per-feed (non-global) message ID. + * + * This needs many additions to make it "real" + * SOMEDAY: create better global message table to enforce cross-feed ordering. + * SOMEDAY: create a global "dispatching" list for feeds that have yet to fully update inboxes. + * Move feed in and remove watchers in one transaction, possibly + * SOMEDAY: create a global list of the most-subscribed-to feeds that all inbox reads check + */ +ACTOR Future _postMessage(Database cx, uint64_t feed, Standalone data) { + state Transaction tr(cx); + state uint64_t messageId = UINT64_MAX - (uint64_t)now(); + TraceEvent("PubSubPost").detail("Feed", feed).detail("Message", messageId); + loop { + try { + Optional feedValue = wait(tr.get(keyForFeed(feed))); + if(!feedValue.present()) { + // No such feed!! + return uint64_t(0); + } + + // Get globally latest message, set our ID to that less one + state Standalone latestMessage = wait( tr.getRange( + firstGreaterOrEqual(keyForMessage(0)), + firstGreaterOrEqual(keyForMessage(UINT64_MAX)), 1 ) ); + if(!latestMessage.size()) { + messageId = UINT64_MAX - 1; + } else { + StringRef messageStr = latestMessage[0].key.removePrefix(messagePrefix); + messageId = valueToUInt64(messageStr) - 1; + } + + tr.set(keyForMessage(messageId), StringRef()); + tr.set(keyForDisptchEntry(messageId), StringRef()); + + Void _ = wait(tr.commit()); + break; + } catch(Error& e) { + Void _ = wait( tr.onError(e) ); + } + } + tr = Transaction(cx); + loop { + try { + // Record this ID as the "latest message" for a feed + tr.set(keyForFeedLatestMessage(feed), uInt64ToValue(messageId)); + + // Store message in list of feed's messages + tr.set(keyForFeedMessage(feed, messageId), StringRef()); + + // Update the count of message that this feed has published + Optional cntValue = wait(tr.get(keyForFeedMessageCount(feed))); + uint64_t messageCount(valueToUInt64(cntValue.get()) + 1); + tr.set(keyForFeedMessageCount(feed), uInt64ToValue(messageCount)); + + // Go through the list of watching inboxes + Void _ = wait(updateFeedWatchers(&tr, feed)); + + // Post the real message data; clear the "dispatching" entry + tr.set(keyForMessage(messageId), data); + tr.clear(keyForDisptchEntry(messageId)); + + Void _ = wait(tr.commit()); + break; + } catch(Error& e) { + Void _ = wait( tr.onError(e) ); + } + } + return messageId; +} + +Future PubSub::postMessage(uint64_t feed, Standalone data) { + return _postMessage(cx, feed, data); +} + +ACTOR Future singlePassInboxCacheUpdate(Database cx, uint64_t inbox, int swath) { + state Transaction tr(cx); + loop { + try { + // For each stale feed, update cache with latest message id + state Standalone staleFeeds = wait( tr.getRange( + firstGreaterOrEqual(keyForInboxStaleFeed(inbox, 0)), + firstGreaterOrEqual(keyForInboxStaleFeed(inbox, UINT64_MAX)), swath ) ); //REVIEW: does 100 make sense? + //printf(" --> stale feeds list size: %d\n", staleFeeds.size()); + if(!staleFeeds.size()) + // If there are no stale feeds, return. + return 0; + state StringRef stalePrefix = keyForInboxStalePrefix(inbox); + state int idx = 0; + for(; idx < staleFeeds.size(); idx++) { + StringRef feedStr = staleFeeds[idx].key.removePrefix(stalePrefix); + //printf(" --> clearing stale entry: %s\n", feedStr.toString().c_str()); + state uint64_t feed = valueToUInt64(feedStr); + + // SOMEDAY: change this to be a range query for the highest #'ed message + Optional v = wait(tr.get(keyForFeedLatestMessage(feed))); + state Value latestMessageValue = v.get(); + //printf(" --> latest message from feed: %s\n", latestMessageValue.toString().c_str()); + + // find the messageID which is currently cached for this feed + Optional lastCachedValue = wait(tr.get(keyForInboxCacheByFeed(inbox, feed))); + if(lastCachedValue.present()) { + uint64_t lastCachedId = valueToUInt64(lastCachedValue.get()); + // clear out the cache entry in the "by-ID" list for this feed + // SOMEDAY: should we leave this in there in some way, or should we pull a better/more recent cache? + tr.clear(keyForInboxCacheByID(inbox, lastCachedId)); + } + //printf(" --> caching message by ID: %s\n", keyForInboxCacheByID(inbox, valueToUInt64(latestMessageValue)).toString().c_str()); + tr.set(keyForInboxCacheByID(inbox, valueToUInt64(latestMessageValue)), uInt64ToValue(feed)); + + // set the latest message + tr.set(keyForInboxCacheByFeed(inbox, feed), latestMessageValue); + tr.clear(staleFeeds[idx].key); + // place watch back on feed + tr.set(keyForFeedWatcher(feed, inbox), StringRef()); + //printf(" --> adding watch to feed: %s\n", keyForFeedWatcher(feed, inbox).toString().c_str()); + } + Void _ = wait(tr.commit()); + return staleFeeds.size(); + } catch(Error& e) { + Void _ = wait( tr.onError(e) ); + } + } +} + +// SOMEDAY: evaluate if this could lead to painful loop if there's one frequent feed. +ACTOR Future updateInboxCache(Database cx, uint64_t inbox) { + state int swath = 100; + state int updatedEntries = swath; + while(updatedEntries >= swath) { + int retVal = wait(singlePassInboxCacheUpdate(cx, inbox, swath)); + updatedEntries = retVal; + } + return Void(); +} + +ACTOR Future getFeedLatestAtOrAfter(Transaction *tr, Feed feed, MessageId position) { + state Standalone lastMessageRange = wait( (*tr).getRange( + firstGreaterOrEqual(keyForFeedMessage(feed, position)), + firstGreaterOrEqual(keyForFeedMessage(feed, UINT64_MAX)), 1 ) ); + if(!lastMessageRange.size()) + return uint64_t(0); + KeyValueRef m = lastMessageRange[0]; + StringRef prefix = keyForFeedMessagePrefix(feed); + StringRef mIdStr = m.key.removePrefix(prefix); + return valueToUInt64(mIdStr); +} + +ACTOR Future getMessage(Transaction *tr, Feed feed, MessageId id) { + state Message m; + m.originatorFeed = feed; + m.messageId = id; + Optional data = wait(tr->get(keyForMessage(id))); + m.data = data.get(); + return m; +} + +Future> _listInboxMessages(Database const& cx, + uint64_t const& inbox, int const& count, uint64_t const& cursor); + +// inboxes with MANY fast feeds may be punished by the following checks +// SOMEDAY: add a check on global lists (or on dispatching list) +ACTOR Future> _listInboxMessages(Database cx, uint64_t inbox, int count, uint64_t cursor) +{ + TraceEvent("PubSubListInbox").detail("Inbox", inbox).detail("Count", count).detail("Cursor", cursor); + Void _ = wait(updateInboxCache(cx, inbox)); + state StringRef perIdPrefix = keyForInboxCacheByIDPrefix(inbox); + loop { + state Transaction tr(cx); + state std::vector messages; + state std::map feedLatest; + try { + // Fetch all cached entries for all the feeds to which we are subscribed + Optional cntValue = wait(tr.get(keyForInboxSubcriptionCount(inbox))); + uint64_t subscriptions = valueToUInt64(cntValue.get()); + state Standalone feeds = wait( tr.getRange( + firstGreaterOrEqual(keyForInboxCacheByID(inbox, 0)), + firstGreaterOrEqual(keyForInboxCacheByID(inbox, UINT64_MAX)), subscriptions ) ); + if(!feeds.size()) + return messages; + + // read cache into map, replace entries newer than cursor with the newest older than cursor + state int idx = 0; + for(; idx < feeds.size(); idx++) { + StringRef mIdStr = feeds[idx].key.removePrefix(perIdPrefix); + MessageId messageId = valueToUInt64(mIdStr); + state Feed feed = valueToUInt64(feeds[idx].value); + //printf(" -> cached message %016llx from feed %016llx\n", messageId, feed); + if(messageId >= cursor) { + //printf(" -> entering message %016llx from feed %016llx\n", messageId, feed); + feedLatest.insert(pair(messageId, feed)); + } else { + // replace this with the first message older than the cursor + MessageId mId = wait(getFeedLatestAtOrAfter(&tr, feed, cursor)); + if(mId) { + feedLatest.insert(pair(mId, feed)); + } + } + } + // There were some cached feeds, but none with messages older than "cursor" + if(!feedLatest.size()) + return messages; + + // Check the list of dispatching messages to make sure there are no older ones than ours + state MessageId earliestMessage = feedLatest.begin()->first; + Standalone dispatching = wait( tr.getRange( + firstGreaterOrEqual(keyForDisptchEntry(earliestMessage)), + firstGreaterOrEqual(keyForDisptchEntry(UINT64_MAX)), 1 ) ); + // If there are messages "older" than ours, try this again + // (with a new transaction and a flush of the "stale" feeds + if(dispatching.size()) { + std::vector r = wait( _listInboxMessages(cx, inbox, count, earliestMessage) ); + return r; + } + + while (messages.size() < count && feedLatest.size() > 0) { + std::map::iterator latest = feedLatest.begin(); + state MessageId id = latest->first; + state Feed f = latest->second; + feedLatest.erase(latest); + + Message m = wait( getMessage( &tr, f, id ) ); + messages.push_back( m ); + + MessageId nextMessage = wait(getFeedLatestAtOrAfter(&tr, f, id + 1)); + if(nextMessage) { + feedLatest.insert(pair(nextMessage, f)); + } + } + + return messages; + } catch(Error& e) { + Void _ = wait( tr.onError(e) ); + } + } +} + +Future> PubSub::listInboxMessages(uint64_t inbox, int count, uint64_t cursor) { + return _listInboxMessages(cx, inbox, count, cursor); +} + +ACTOR Future> _listFeedMessages(Database cx, Feed feed, int count, uint64_t cursor) { + state std::vector messages; + state Transaction tr(cx); + TraceEvent("PubSubListFeed").detail("Feed", feed).detail("Count", count).detail("Cursor", cursor); + loop { + try { + state Standalone messageIds = wait( tr.getRange( + firstGreaterOrEqual(keyForFeedMessage(feed, cursor)), + firstGreaterOrEqual(keyForFeedMessage(feed, UINT64_MAX)), count ) ); + if(!messageIds.size()) + return messages; + + state int idx = 0; + for(; idx < messageIds.size(); idx++) { + StringRef mIdStr = messageIds[idx].key.removePrefix(keyForFeedMessagePrefix(feed)); + MessageId messageId = valueToUInt64(mIdStr); + Message m = wait( getMessage(&tr, feed, messageId) ); + messages.push_back( m ); + } + return messages; + } catch(Error& e) { + Void _ = wait( tr.onError(e) ); + } + } +} + +Future> PubSub::listFeedMessages(Feed feed, int count, uint64_t cursor) { + return _listFeedMessages(cx, feed, count, cursor); +} + diff --git a/fdbserver/pubsub.h b/fdbserver/pubsub.h new file mode 100644 index 0000000000..215473eaf4 --- /dev/null +++ b/fdbserver/pubsub.h @@ -0,0 +1,95 @@ +/* + * pubsub.h + * + * This source file is part of the FoundationDB open source project + * + * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "fdbclient/NativeAPI.h" + +/* + * /////////////// + * General FDB data model of Pub/Sub model + * Feeds, Inboxes, Messages + * + * Message - publisher (feed), data + * + * Inbox - message cache, dirty list, list of subscribed-to feeds + * + * Feed - messages, list of inboxes to notify + * + * Structures for scalability: global list of dispatching feeds, feeds to consider + * dirty for all operation + * + * /////////////// + * Basic Processes: post message, list messages + * + * Post (in one transaction): + * 1. Add message to list of message in feed history + * 2. For each Inbox of list of watching inboxes + * a. remove Inbox from list of watchers + * b. mark self as 'dirty' in the Inbox's dirty list + * + * List recent message in Inbox (in one transaction), up to n messages: + * 1. For each dirty Feed, update cache with latest message + * 2. Triage list by getting latest m messages from top m feeds + * note: these messages can be cached in memory on the server, and on paging + * operations reused, so long as step 1 is repeated, IF there is no message deletion. + * + * //////////////// + * Assumptions: + * 1. Subscriptions are "retroactive". If a subscription is in place, the messages + * from that feed will start to appear in the listing of that inboxes messages + * and appear in historical lists as well. This could lead to odd behaviour + * if paging through the contents of an inbox while a new subscrption was added + */ + +typedef uint64_t Feed; +typedef uint64_t Inbox; +typedef uint64_t MessageId; + +class Message { +public: + Feed originatorFeed; + MessageId messageId; + Standalone data; + + template + void serialize( Ar& ar ) { + ar & originatorFeed & messageId & data & data.arena(); + } +}; + +class PubSub { +public: + PubSub(Database cx); + + Future createFeed(Standalone metadata); + + Future createInbox(Standalone metadata); + + Future createSubcription(Feed feed, Inbox inbox); + + Future postMessage(Feed feed, Standalone data); + + Future> listFeedMessages(Feed feed, int count, MessageId cursor = 0); + + Future> listInboxMessages(Inbox inbox, int count, MessageId cursor = 0); + +private: + Database cx; +}; + diff --git a/fdbserver/sqlite/btree.c b/fdbserver/sqlite/btree.c new file mode 100644 index 0000000000..28390d6163 --- /dev/null +++ b/fdbserver/sqlite/btree.c @@ -0,0 +1,8739 @@ +/************** Begin file btree.c *******************************************/ +/* +** 2004 April 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file implements a external (disk-based) database using BTrees. +** See the header comment on "btreeInt.h" for additional information. +** Including a description of file format and an overview of operation. +*/ + +/* +** The header string that appears at the beginning of every +** SQLite database. +*/ +static const char zMagicHeader[] = SQLITE_FILE_HEADER; + +int g_expect_full_pointermap = 0; + +#define PTRMAP_LAZYFREE 20 + +/* +** Set this global variable to 1 to enable tracing using the TRACE +** macro. +*/ +#if 0 +int sqlite3BtreeTrace=1; /* True to enable tracing */ +# define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);} +#else +# define TRACE(X) +#endif + +/* +** Extract a 2-byte big-endian integer from an array of unsigned bytes. +** But if the value is zero, make it 65536. +** +** This routine is used to extract the "offset to cell content area" value +** from the header of a btree page. If the page size is 65536 and the page +** is empty, the offset should be 65536, but the 2-byte value stores zero. +** This routine makes the necessary adjustment to 65536. +*/ +#define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1) + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** A list of BtShared objects that are eligible for participation +** in shared cache. This variable has file scope during normal builds, +** but the test harness needs to access it so we make it global for +** test builds. +** +** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER. +*/ +#ifdef SQLITE_TEST +SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; +#else +static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; +#endif +#endif /* SQLITE_OMIT_SHARED_CACHE */ + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** Enable or disable the shared pager and schema features. +** +** This routine has no effect on existing database connections. +** The shared cache setting effects only future calls to +** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). +*/ +SQLITE_API int sqlite3_enable_shared_cache(int enable){ + sqlite3GlobalConfig.sharedCacheEnabled = enable; + return SQLITE_OK; +} +#endif + + + +#ifdef SQLITE_OMIT_SHARED_CACHE + /* + ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(), + ** and clearAllSharedCacheTableLocks() + ** manipulate entries in the BtShared.pLock linked list used to store + ** shared-cache table level locks. If the library is compiled with the + ** shared-cache feature disabled, then there is only ever one user + ** of each BtShared structure and so this locking is not necessary. + ** So define the lock related functions as no-ops. + */ + #define querySharedCacheTableLock(a,b,c) SQLITE_OK + #define setSharedCacheTableLock(a,b,c) SQLITE_OK + #define clearAllSharedCacheTableLocks(a) + #define downgradeAllSharedCacheTableLocks(a) + #define hasSharedCacheTableLock(a,b,c,d) 1 + #define hasReadConflicts(a, b) 0 +#endif + +#ifndef SQLITE_OMIT_SHARED_CACHE + +#ifdef SQLITE_DEBUG +/* +**** This function is only used as part of an assert() statement. *** +** +** Check to see if pBtree holds the required locks to read or write to the +** table with root page iRoot. Return 1 if it does and 0 if not. +** +** For example, when writing to a table with root-page iRoot via +** Btree connection pBtree: +** +** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) ); +** +** When writing to an index that resides in a sharable database, the +** caller should have first obtained a lock specifying the root page of +** the corresponding table. This makes things a bit more complicated, +** as this module treats each table as a separate structure. To determine +** the table corresponding to the index being written, this +** function has to search through the database schema. +** +** Instead of a lock on the table/index rooted at page iRoot, the caller may +** hold a write-lock on the schema table (root page 1). This is also +** acceptable. +*/ +static int hasSharedCacheTableLock( + Btree *pBtree, /* Handle that must hold lock */ + Pgno iRoot, /* Root page of b-tree */ + int isIndex, /* True if iRoot is the root of an index b-tree */ + int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */ +){ + Schema *pSchema = (Schema *)pBtree->pBt->pSchema; + Pgno iTab = 0; + BtLock *pLock; + + /* If this database is not shareable, or if the client is reading + ** and has the read-uncommitted flag set, then no lock is required. + ** Return true immediately. + */ + if( (pBtree->sharable==0) + || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted)) + ){ + return 1; + } + + /* If the client is reading or writing an index and the schema is + ** not loaded, then it is too difficult to actually check to see if + ** the correct locks are held. So do not bother - just return true. + ** This case does not come up very often anyhow. + */ + if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){ + return 1; + } + + /* Figure out the root-page that the lock should be held on. For table + ** b-trees, this is just the root page of the b-tree being read or + ** written. For index b-trees, it is the root page of the associated + ** table. */ + if( isIndex ){ + HashElem *p; + for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){ + Index *pIdx = (Index *)sqliteHashData(p); + if( pIdx->tnum==(int)iRoot ){ + iTab = pIdx->pTable->tnum; + } + } + }else{ + iTab = iRoot; + } + + /* Search for the required lock. Either a write-lock on root-page iTab, a + ** write-lock on the schema table, or (if the client is reading) a + ** read-lock on iTab will suffice. Return 1 if any of these are found. */ + for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){ + if( pLock->pBtree==pBtree + && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1)) + && pLock->eLock>=eLockType + ){ + return 1; + } + } + + /* Failed to find the required lock. */ + return 0; +} +#endif /* SQLITE_DEBUG */ + +#ifdef SQLITE_DEBUG +/* +**** This function may be used as part of assert() statements only. **** +** +** Return true if it would be illegal for pBtree to write into the +** table or index rooted at iRoot because other shared connections are +** simultaneously reading that same table or index. +** +** It is illegal for pBtree to write if some other Btree object that +** shares the same BtShared object is currently reading or writing +** the iRoot table. Except, if the other Btree object has the +** read-uncommitted flag set, then it is OK for the other object to +** have a read cursor. +** +** For example, before writing to any part of the table or index +** rooted at page iRoot, one should call: +** +** assert( !hasReadConflicts(pBtree, iRoot) ); +*/ +static int hasReadConflicts(Btree *pBtree, Pgno iRoot){ + BtCursor *p; + for(p=pBtree->pBt->pCursor; p; p=p->pNext){ + if( p->pgnoRoot==iRoot + && p->pBtree!=pBtree + && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted) + ){ + return 1; + } + } + return 0; +} +#endif /* #ifdef SQLITE_DEBUG */ + +/* +** Query to see if Btree handle p may obtain a lock of type eLock +** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return +** SQLITE_OK if the lock may be obtained (by calling +** setSharedCacheTableLock()), or SQLITE_LOCKED if not. +*/ +static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ + BtShared *pBt = p->pBt; + BtLock *pIter; + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); + assert( p->db!=0 ); + assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 ); + + /* If requesting a write-lock, then the Btree must have an open write + ** transaction on this file. And, obviously, for this to be so there + ** must be an open write transaction on the file itself. + */ + assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) ); + assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE ); + + /* This routine is a no-op if the shared-cache is not enabled */ + if( !p->sharable ){ + return SQLITE_OK; + } + + /* If some other connection is holding an exclusive lock, the + ** requested lock may not be obtained. + */ + if( pBt->pWriter!=p && pBt->isExclusive ){ + sqlite3ConnectionBlocked(p->db, pBt->pWriter->db); + return SQLITE_LOCKED_SHAREDCACHE; + } + + for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ + /* The condition (pIter->eLock!=eLock) in the following if(...) + ** statement is a simplification of: + ** + ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK) + ** + ** since we know that if eLock==WRITE_LOCK, then no other connection + ** may hold a WRITE_LOCK on any table in this file (since there can + ** only be a single writer). + */ + assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK ); + assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK); + if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){ + sqlite3ConnectionBlocked(p->db, pIter->pBtree->db); + if( eLock==WRITE_LOCK ){ + assert( p==pBt->pWriter ); + pBt->isPending = 1; + } + return SQLITE_LOCKED_SHAREDCACHE; + } + } + return SQLITE_OK; +} +#endif /* !SQLITE_OMIT_SHARED_CACHE */ + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** Add a lock on the table with root-page iTable to the shared-btree used +** by Btree handle p. Parameter eLock must be either READ_LOCK or +** WRITE_LOCK. +** +** This function assumes the following: +** +** (a) The specified Btree object p is connected to a sharable +** database (one with the BtShared.sharable flag set), and +** +** (b) No other Btree objects hold a lock that conflicts +** with the requested lock (i.e. querySharedCacheTableLock() has +** already been called and returned SQLITE_OK). +** +** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM +** is returned if a malloc attempt fails. +*/ +static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ + BtShared *pBt = p->pBt; + BtLock *pLock = 0; + BtLock *pIter; + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); + assert( p->db!=0 ); + + /* A connection with the read-uncommitted flag set will never try to + ** obtain a read-lock using this function. The only read-lock obtained + ** by a connection in read-uncommitted mode is on the sqlite_master + ** table, and that lock is obtained in BtreeBeginTrans(). */ + assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK ); + + /* This function should only be called on a sharable b-tree after it + ** has been determined that no other b-tree holds a conflicting lock. */ + assert( p->sharable ); + assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) ); + + /* First search the list for an existing lock on this table. */ + for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ + if( pIter->iTable==iTable && pIter->pBtree==p ){ + pLock = pIter; + break; + } + } + + /* If the above search did not find a BtLock struct associating Btree p + ** with table iTable, allocate one and link it into the list. + */ + if( !pLock ){ + pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); + if( !pLock ){ + return SQLITE_NOMEM; + } + pLock->iTable = iTable; + pLock->pBtree = p; + pLock->pNext = pBt->pLock; + pBt->pLock = pLock; + } + + /* Set the BtLock.eLock variable to the maximum of the current lock + ** and the requested lock. This means if a write-lock was already held + ** and a read-lock requested, we don't incorrectly downgrade the lock. + */ + assert( WRITE_LOCK>READ_LOCK ); + if( eLock>pLock->eLock ){ + pLock->eLock = eLock; + } + + return SQLITE_OK; +} +#endif /* !SQLITE_OMIT_SHARED_CACHE */ + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** Release all the table locks (locks obtained via calls to +** the setSharedCacheTableLock() procedure) held by Btree object p. +** +** This function assumes that Btree p has an open read or write +** transaction. If it does not, then the BtShared.isPending variable +** may be incorrectly cleared. +*/ +static void clearAllSharedCacheTableLocks(Btree *p){ + BtShared *pBt = p->pBt; + BtLock **ppIter = &pBt->pLock; + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( p->sharable || 0==*ppIter ); + assert( p->inTrans>0 ); + + while( *ppIter ){ + BtLock *pLock = *ppIter; + assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree ); + assert( pLock->pBtree->inTrans>=pLock->eLock ); + if( pLock->pBtree==p ){ + *ppIter = pLock->pNext; + assert( pLock->iTable!=1 || pLock==&p->lock ); + if( pLock->iTable!=1 ){ + sqlite3_free(pLock); + } + }else{ + ppIter = &pLock->pNext; + } + } + + assert( pBt->isPending==0 || pBt->pWriter ); + if( pBt->pWriter==p ){ + pBt->pWriter = 0; + pBt->isExclusive = 0; + pBt->isPending = 0; + }else if( pBt->nTransaction==2 ){ + /* This function is called when Btree p is concluding its + ** transaction. If there currently exists a writer, and p is not + ** that writer, then the number of locks held by connections other + ** than the writer must be about to drop to zero. In this case + ** set the isPending flag to 0. + ** + ** If there is not currently a writer, then BtShared.isPending must + ** be zero already. So this next line is harmless in that case. + */ + pBt->isPending = 0; + } +} + +/* +** This function changes all write-locks held by Btree p into read-locks. +*/ +static void downgradeAllSharedCacheTableLocks(Btree *p){ + BtShared *pBt = p->pBt; + if( pBt->pWriter==p ){ + BtLock *pLock; + pBt->pWriter = 0; + pBt->isExclusive = 0; + pBt->isPending = 0; + for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){ + assert( pLock->eLock==READ_LOCK || pLock->pBtree==p ); + pLock->eLock = READ_LOCK; + } + } +} + +#endif /* SQLITE_OMIT_SHARED_CACHE */ + +static void releasePage(MemPage *pPage); /* Forward reference */ + +/* +***** This routine is used inside of assert() only **** +** +** Verify that the cursor holds the mutex on its BtShared +*/ +#ifdef SQLITE_DEBUG +static int cursorHoldsMutex(BtCursor *p){ + return sqlite3_mutex_held(p->pBt->mutex); +} +#endif + + +#ifndef SQLITE_OMIT_INCRBLOB +/* +** Invalidate the overflow page-list cache for cursor pCur, if any. +*/ +static void invalidateOverflowCache(BtCursor *pCur){ + assert( cursorHoldsMutex(pCur) ); + sqlite3_free(pCur->aOverflow); + pCur->aOverflow = 0; +} + +/* +** Invalidate the overflow page-list cache for all cursors opened +** on the shared btree structure pBt. +*/ +static void invalidateAllOverflowCache(BtShared *pBt){ + BtCursor *p; + assert( sqlite3_mutex_held(pBt->mutex) ); + for(p=pBt->pCursor; p; p=p->pNext){ + invalidateOverflowCache(p); + } +} + +/* +** This function is called before modifying the contents of a table +** to invalidate any incrblob cursors that are open on the +** row or one of the rows being modified. +** +** If argument isClearTable is true, then the entire contents of the +** table is about to be deleted. In this case invalidate all incrblob +** cursors open on any row within the table with root-page pgnoRoot. +** +** Otherwise, if argument isClearTable is false, then the row with +** rowid iRow is being replaced or deleted. In this case invalidate +** only those incrblob cursors open on that specific row. +*/ +static void invalidateIncrblobCursors( + Btree *pBtree, /* The database file to check */ + i64 iRow, /* The rowid that might be changing */ + int isClearTable /* True if all rows are being deleted */ +){ + BtCursor *p; + BtShared *pBt = pBtree->pBt; + assert( sqlite3BtreeHoldsMutex(pBtree) ); + for(p=pBt->pCursor; p; p=p->pNext){ + if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){ + p->eState = CURSOR_INVALID; + } + } +} + +#else + /* Stub functions when INCRBLOB is omitted */ + #define invalidateOverflowCache(x) + #define invalidateAllOverflowCache(x) + #define invalidateIncrblobCursors(x,y,z) +#endif /* SQLITE_OMIT_INCRBLOB */ + +/* +** Set bit pgno of the BtShared.pHasContent bitvec. This is called +** when a page that previously contained data becomes a free-list leaf +** page. +** +** The BtShared.pHasContent bitvec exists to work around an obscure +** bug caused by the interaction of two useful IO optimizations surrounding +** free-list leaf pages: +** +** 1) When all data is deleted from a page and the page becomes +** a free-list leaf page, the page is not written to the database +** (as free-list leaf pages contain no meaningful data). Sometimes +** such a page is not even journalled (as it will not be modified, +** why bother journalling it?). +** +** 2) When a free-list leaf page is reused, its content is not read +** from the database or written to the journal file (why should it +** be, if it is not at all meaningful?). +** +** By themselves, these optimizations work fine and provide a handy +** performance boost to bulk delete or insert operations. However, if +** a page is moved to the free-list and then reused within the same +** transaction, a problem comes up. If the page is not journalled when +** it is moved to the free-list and it is also not journalled when it +** is extracted from the free-list and reused, then the original data +** may be lost. In the event of a rollback, it may not be possible +** to restore the database to its original configuration. +** +** The solution is the BtShared.pHasContent bitvec. Whenever a page is +** moved to become a free-list leaf page, the corresponding bit is +** set in the bitvec. Whenever a leaf page is extracted from the free-list, +** optimization 2 above is omitted if the corresponding bit is already +** set in BtShared.pHasContent. The contents of the bitvec are cleared +** at the end of every transaction. +*/ +static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ + int rc = SQLITE_OK; + if( !pBt->pHasContent ){ + assert( pgno<=pBt->nPage ); + pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage); + if( !pBt->pHasContent ){ + rc = SQLITE_NOMEM; + } + } + if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ + rc = sqlite3BitvecSet(pBt->pHasContent, pgno); + } + return rc; +} + +/* +** Query the BtShared.pHasContent vector. +** +** This function is called when a free-list leaf page is removed from the +** free-list for reuse. It returns false if it is safe to retrieve the +** page from the pager layer with the 'no-content' flag set. True otherwise. +*/ +static int btreeGetHasContent(BtShared *pBt, Pgno pgno){ + Bitvec *p = pBt->pHasContent; + return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno))); +} + +/* +** Clear (destroy) the BtShared.pHasContent bitvec. This should be +** invoked at the conclusion of each write-transaction. +*/ +static void btreeClearHasContent(BtShared *pBt){ + sqlite3BitvecDestroy(pBt->pHasContent); + pBt->pHasContent = 0; +} + +/* +** Save the current cursor position in the variables BtCursor.nKey +** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. +** +** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID) +** prior to calling this routine. +*/ +static int saveCursorPosition(BtCursor *pCur){ + int rc; + + assert( CURSOR_VALID==pCur->eState ); + assert( 0==pCur->pKey ); + assert( cursorHoldsMutex(pCur) ); + + rc = sqlite3BtreeKeySize(pCur, &pCur->nKey); + assert( rc==SQLITE_OK ); /* KeySize() cannot fail */ + + /* If this is an intKey table, then the above call to BtreeKeySize() + ** stores the integer key in pCur->nKey. In this case this value is + ** all that is required. Otherwise, if pCur is not open on an intKey + ** table, then malloc space for and store the pCur->nKey bytes of key + ** data. + */ + if( 0==pCur->apPage[0]->intKey ){ + void *pKey = sqlite3Malloc( (int)pCur->nKey ); + if( pKey ){ + rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey); + if( rc==SQLITE_OK ){ + pCur->pKey = pKey; + }else{ + sqlite3_free(pKey); + } + }else{ + rc = SQLITE_NOMEM; + } + } + assert( !pCur->apPage[0]->intKey || !pCur->pKey ); + + if( rc==SQLITE_OK ){ + int i; + for(i=0; i<=pCur->iPage; i++){ + releasePage(pCur->apPage[i]); + pCur->apPage[i] = 0; + } + pCur->iPage = -1; + pCur->eState = CURSOR_REQUIRESEEK; + } + + invalidateOverflowCache(pCur); + return rc; +} + +/* +** Save the positions of all cursors (except pExcept) that are open on +** the table with root-page iRoot. Usually, this is called just before cursor +** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()). +*/ +static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ + BtCursor *p; + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( pExcept==0 || pExcept->pBt==pBt ); + for(p=pBt->pCursor; p; p=p->pNext){ + if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) && + p->eState==CURSOR_VALID ){ + int rc = saveCursorPosition(p); + if( SQLITE_OK!=rc ){ + return rc; + } + } + } + return SQLITE_OK; +} + +/* +** Clear the current cursor position. +*/ +SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){ + assert( cursorHoldsMutex(pCur) ); + sqlite3_free(pCur->pKey); + pCur->pKey = 0; + pCur->eState = CURSOR_INVALID; +} + +/* +** In this version of BtreeMoveto, pKey is a packed index record +** such as is generated by the OP_MakeRecord opcode. Unpack the +** record and then call BtreeMovetoUnpacked() to do the work. +*/ +static int btreeMoveto( + BtCursor *pCur, /* Cursor open on the btree to be searched */ + const void *pKey, /* Packed key if the btree is an index */ + i64 nKey, /* Integer key for tables. Size of pKey for indices */ + int bias, /* Bias search to the high end */ + int *pRes /* Write search results here */ +){ + int rc; /* Status code */ + UnpackedRecord *pIdxKey; /* Unpacked index key */ + char aSpace[150]; /* Temp space for pIdxKey - to avoid a malloc */ + + if( pKey ){ + assert( nKey==(i64)(int)nKey ); + pIdxKey = sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, + aSpace, sizeof(aSpace)); + if( pIdxKey==0 ) return SQLITE_NOMEM; + }else{ + pIdxKey = 0; + } + rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); + if( pKey ){ + sqlite3VdbeDeleteUnpackedRecord(pIdxKey); + } + return rc; +} + +/* +** Restore the cursor to the position it was in (or as close to as possible) +** when saveCursorPosition() was called. Note that this call deletes the +** saved position info stored by saveCursorPosition(), so there can be +** at most one effective restoreCursorPosition() call after each +** saveCursorPosition(). +*/ +static int btreeRestoreCursorPosition(BtCursor *pCur){ + int rc; + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState>=CURSOR_REQUIRESEEK ); + if( pCur->eState==CURSOR_FAULT ){ + return pCur->skipNext; + } + pCur->eState = CURSOR_INVALID; + rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext); + if( rc==SQLITE_OK ){ + sqlite3_free(pCur->pKey); + pCur->pKey = 0; + assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); + } + return rc; +} + +#define restoreCursorPosition(p) \ + (p->eState>=CURSOR_REQUIRESEEK ? \ + btreeRestoreCursorPosition(p) : \ + SQLITE_OK) + +/* +** Determine whether or not a cursor has moved from the position it +** was last placed at. Cursors can move when the row they are pointing +** at is deleted out from under them. +** +** This routine returns an error code if something goes wrong. The +** integer *pHasMoved is set to one if the cursor has moved and 0 if not. +*/ +SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){ + int rc; + + rc = restoreCursorPosition(pCur); + if( rc ){ + *pHasMoved = 1; + return rc; + } + if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){ + *pHasMoved = 1; + }else{ + *pHasMoved = 0; + } + return SQLITE_OK; +} + +#ifndef SQLITE_OMIT_AUTOVACUUM +/* +** Given a page number of a regular database page, return the page +** number for the pointer-map page that contains the entry for the +** input page number. +** +** Return 0 (not a valid page) for pgno==1 since there is +** no pointer map associated with page 1. The integrity_check logic +** requires that ptrmapPageno(*,1)!=1. +*/ +SQLITE_PRIVATE Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ + int nPagesPerMapPage; + Pgno iPtrMap, ret; + assert( sqlite3_mutex_held(pBt->mutex) ); + if( pgno<2 ) return 0; + nPagesPerMapPage = (pBt->usableSize/5)+1; + iPtrMap = (pgno-2)/nPagesPerMapPage; + ret = (iPtrMap*nPagesPerMapPage) + 2; + if( ret==PENDING_BYTE_PAGE(pBt) ){ + ret++; + } + return ret; +} + +/* +** Write an entry into the pointer map. +** +** This routine updates the pointer map entry for page number 'key' +** so that it maps to type 'eType' and parent page number 'pgno'. +** +** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is +** a no-op. If an error occurs, the appropriate error code is written +** into *pRC. +*/ +static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ + DbPage *pDbPage; /* The pointer map page */ + u8 *pPtrmap; /* The pointer map data */ + Pgno iPtrmap; /* The pointer map page number */ + int offset; /* Offset in pointer map page */ + int rc; /* Return code from subfunctions */ + + if( *pRC ) return; + + assert( sqlite3_mutex_held(pBt->mutex) ); + /* The master-journal page number must never be used as a pointer map page */ + assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); + + assert( pBt->autoVacuum ); + if( key==0 ){ + *pRC = SQLITE_CORRUPT_BKPT; + return; + } + iPtrmap = PTRMAP_PAGENO(pBt, key); + rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); + if( rc!=SQLITE_OK ){ + *pRC = rc; + return; + } + offset = PTRMAP_PTROFFSET(iPtrmap, key); + if( offset<0 ){ + *pRC = SQLITE_CORRUPT_BKPT; + goto ptrmap_exit; + } + pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); + + if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ + TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); + *pRC= rc = sqlite3PagerWrite(pDbPage); + if( rc==SQLITE_OK ){ + pPtrmap[offset] = eType; + put4byte(&pPtrmap[offset+1], parent); + } + } + +ptrmap_exit: + sqlite3PagerUnref(pDbPage); +} + +/* +** Read an entry from the pointer map. +** +** This routine retrieves the pointer map entry for page 'key', writing +** the type and parent page number to *pEType and *pPgno respectively. +** An error code is returned if something goes wrong, otherwise SQLITE_OK. +*/ +static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ + DbPage *pDbPage; /* The pointer map page */ + int iPtrmap; /* Pointer map page index */ + u8 *pPtrmap; /* Pointer map page data */ + int offset; /* Offset of entry in pointer map */ + int rc; + + assert( sqlite3_mutex_held(pBt->mutex) ); + + iPtrmap = PTRMAP_PAGENO(pBt, key); + rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage); + if( rc!=0 ){ + return rc; + } + pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); + + offset = PTRMAP_PTROFFSET(iPtrmap, key); + assert( pEType!=0 ); + *pEType = pPtrmap[offset]; + if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); + + sqlite3PagerUnref(pDbPage); + if( *pEType<1 || *pEType>PTRMAP_LAZYFREE ) return SQLITE_CORRUPT_BKPT; + return SQLITE_OK; +} + +#else /* if defined SQLITE_OMIT_AUTOVACUUM */ + #define ptrmapPut(w,x,y,z,rc) + #define ptrmapGet(w,x,y,z) SQLITE_OK + #define ptrmapPutOvflPtr(x, y, rc) +#endif + +/* +** Given a btree page and a cell index (0 means the first cell on +** the page, 1 means the second cell, and so forth) return a pointer +** to the cell content. +** +** This routine works only for pages that do not contain overflow cells. +*/ +#define findCell(P,I) \ + ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)]))) + +// Make sure the pointer map entry for child points to parent +#if 0 +static int verifyParentChildLink(BtShared *pBt, Pgno parent, Pgno child) { + Pgno pgno; + u8 eType; + int rc = ptrmapGet(pBt, child, &eType, &pgno); + if( (rc != SQLITE_OK) || (pgno != parent) ) + return SQLITE_CORRUPT_BKPT; + + return SQLITE_OK; +} +#else +#define verifyParentChildLink(x,y,z) SQLITE_OK +#endif + +/* +** This a more complex version of findCell() that works for +** pages that do contain overflow cells. +*/ +static u8 *findOverflowCell(MemPage *pPage, int iCell){ + int i; + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + for(i=pPage->nOverflow-1; i>=0; i--){ + int k; + struct _OvflCell *pOvfl; + pOvfl = &pPage->aOvfl[i]; + k = pOvfl->idx; + if( k<=iCell ){ + if( k==iCell ){ + return pOvfl->pCell; + } + iCell--; + } + } + return findCell(pPage, iCell); +} + +/* +** Parse a cell content block and fill in the CellInfo structure. There +** are two versions of this function. btreeParseCell() takes a +** cell index as the second argument and btreeParseCellPtr() +** takes a pointer to the body of the cell as its second argument. +** +** Within this file, the parseCell() macro can be called instead of +** btreeParseCellPtr(). Using some compilers, this will be faster. +*/ +static void btreeParseCellPtr( + MemPage *pPage, /* Page containing the cell */ + u8 *pCell, /* Pointer to the cell text. */ + CellInfo *pInfo /* Fill in this structure */ +){ + u16 n; /* Number bytes in cell content header */ + u32 nPayload; /* Number of bytes of cell payload */ + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + + pInfo->pCell = pCell; + assert( pPage->leaf==0 || pPage->leaf==1 ); + n = pPage->childPtrSize; + assert( n==4-4*pPage->leaf ); + if( pPage->intKey ){ + if( pPage->hasData ){ + n += getVarint32(&pCell[n], nPayload); + }else{ + nPayload = 0; + } + n += getVarint(&pCell[n], (u64*)&pInfo->nKey); + pInfo->nData = nPayload; + }else{ + pInfo->nData = 0; + n += getVarint32(&pCell[n], nPayload); + pInfo->nKey = nPayload; + } + pInfo->nPayload = nPayload; + pInfo->nHeader = n; + testcase( nPayload==pPage->maxLocal ); + testcase( nPayload==pPage->maxLocal+1 ); + if( likely(nPayload<=pPage->maxLocal) ){ + /* This is the (easy) common case where the entire payload fits + ** on the local page. No overflow is required. + */ + if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4; + pInfo->nLocal = (u16)nPayload; + pInfo->iOverflow = 0; + }else{ + /* If the payload will not fit completely on the local page, we have + ** to decide how much to store locally and how much to spill onto + ** overflow pages. The strategy is to minimize the amount of unused + ** space on overflow pages while keeping the amount of local storage + ** in between minLocal and maxLocal. + ** + ** Warning: changing the way overflow payload is distributed in any + ** way will result in an incompatible file format. + */ + int minLocal; /* Minimum amount of payload held locally */ + int maxLocal; /* Maximum amount of payload held locally */ + int surplus; /* Overflow payload available for local storage */ + + minLocal = pPage->minLocal; + maxLocal = pPage->maxLocal; + surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4); + testcase( surplus==maxLocal ); + testcase( surplus==maxLocal+1 ); + if( surplus <= maxLocal ){ + pInfo->nLocal = (u16)surplus; + }else{ + pInfo->nLocal = (u16)minLocal; + } + pInfo->iOverflow = (u16)(pInfo->nLocal + n); + pInfo->nSize = pInfo->iOverflow + 4; + } +} +#define parseCell(pPage, iCell, pInfo) \ + btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo)) +static void btreeParseCell( + MemPage *pPage, /* Page containing the cell */ + int iCell, /* The cell index. First cell is 0 */ + CellInfo *pInfo /* Fill in this structure */ +){ + parseCell(pPage, iCell, pInfo); +} + +/* +** Compute the total number of bytes that a Cell needs in the cell +** data area of the btree-page. The return number includes the cell +** data header and the local payload, but not any overflow page or +** the space used by the cell pointer. +*/ +static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ + u8 *pIter = &pCell[pPage->childPtrSize]; + u32 nSize; + +#ifdef SQLITE_DEBUG + /* The value returned by this function should always be the same as + ** the (CellInfo.nSize) value found by doing a full parse of the + ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of + ** this function verifies that this invariant is not violated. */ + CellInfo debuginfo; + btreeParseCellPtr(pPage, pCell, &debuginfo); +#endif + + if( pPage->intKey ){ + u8 *pEnd; + if( pPage->hasData ){ + pIter += getVarint32(pIter, nSize); + }else{ + nSize = 0; + } + + /* pIter now points at the 64-bit integer key value, a variable length + ** integer. The following block moves pIter to point at the first byte + ** past the end of the key value. */ + pEnd = &pIter[9]; + while( (*pIter++)&0x80 && pItermaxLocal ); + testcase( nSize==pPage->maxLocal+1 ); + if( nSize>pPage->maxLocal ){ + int minLocal = pPage->minLocal; + nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); + testcase( nSize==pPage->maxLocal ); + testcase( nSize==pPage->maxLocal+1 ); + if( nSize>pPage->maxLocal ){ + nSize = minLocal; + } + nSize += 4; + } + nSize += (u32)(pIter - pCell); + + /* The minimum size of any cell is 4 bytes. */ + if( nSize<4 ){ + nSize = 4; + } + + assert( nSize==debuginfo.nSize ); + return (u16)nSize; +} + +#ifdef SQLITE_DEBUG +/* This variation on cellSizePtr() is used inside of assert() statements +** only. */ +static u16 cellSize(MemPage *pPage, int iCell){ + return cellSizePtr(pPage, findCell(pPage, iCell)); +} +#endif + +#ifndef SQLITE_OMIT_AUTOVACUUM +/* +** If the cell pCell, part of page pPage contains a pointer +** to an overflow page, insert an entry into the pointer-map +** for the overflow page. +*/ +static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ + CellInfo info; + if( *pRC ) return; + assert( pCell!=0 ); + btreeParseCellPtr(pPage, pCell, &info); + assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload ); + if( info.iOverflow ){ + Pgno ovfl = get4byte(&pCell[info.iOverflow]); + ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); + } +} +#endif + + +/* +** Defragment the page given. All Cells are moved to the +** end of the page and all free space is collected into one +** big FreeBlk that occurs in between the header and cell +** pointer array and the cell content area. +*/ +static int defragmentPage(MemPage *pPage){ + int i; /* Loop counter */ + int pc; /* Address of a i-th cell */ + int hdr; /* Offset to the page header */ + int size; /* Size of a cell */ + int usableSize; /* Number of usable bytes on a page */ + int cellOffset; /* Offset to the cell pointer array */ + int cbrk; /* Offset to the cell content area */ + int nCell; /* Number of cells on the page */ + unsigned char *data; /* The page data */ + unsigned char *temp; /* Temp area for cell content */ + int iCellFirst; /* First allowable cell index */ + int iCellLast; /* Last possible cell index */ + + + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( pPage->pBt!=0 ); + assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); + assert( pPage->nOverflow==0 ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + temp = sqlite3PagerTempSpace(pPage->pBt->pPager); + data = pPage->aData; + hdr = pPage->hdrOffset; + cellOffset = pPage->cellOffset; + nCell = pPage->nCell; + assert( nCell==get2byte(&data[hdr+3]) ); + usableSize = pPage->pBt->usableSize; + cbrk = get2byte(&data[hdr+5]); + memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk); + cbrk = usableSize; + iCellFirst = cellOffset + 2*nCell; + iCellLast = usableSize - 4; + for(i=0; iiCellLast ){ + return SQLITE_CORRUPT_BKPT; + } +#endif + assert( pc>=iCellFirst && pc<=iCellLast ); + size = cellSizePtr(pPage, &temp[pc]); + cbrk -= size; +#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) + if( cbrkusableSize ){ + return SQLITE_CORRUPT_BKPT; + } +#endif + assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); + testcase( cbrk+size==usableSize ); + testcase( pc+size==usableSize ); + memcpy(&data[cbrk], &temp[pc], size); + put2byte(pAddr, cbrk); + } + assert( cbrk>=iCellFirst ); + put2byte(&data[hdr+5], cbrk); + data[hdr+1] = 0; + data[hdr+2] = 0; + data[hdr+7] = 0; + memset(&data[iCellFirst], 0, cbrk-iCellFirst); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + if( cbrk-iCellFirst!=pPage->nFree ){ + return SQLITE_CORRUPT_BKPT; + } + return SQLITE_OK; +} + +/* +** Allocate nByte bytes of space from within the B-Tree page passed +** as the first argument. Write into *pIdx the index into pPage->aData[] +** of the first byte of allocated space. Return either SQLITE_OK or +** an error code (usually SQLITE_CORRUPT). +** +** The caller guarantees that there is sufficient space to make the +** allocation. This routine might need to defragment in order to bring +** all the space together, however. This routine will avoid using +** the first two bytes past the cell pointer area since presumably this +** allocation is being made in order to insert a new cell, so we will +** also end up needing a new cell pointer. +*/ +static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ + const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ + u8 * const data = pPage->aData; /* Local cache of pPage->aData */ + int nFrag; /* Number of fragmented bytes on pPage */ + int top; /* First byte of cell content area */ + int gap; /* First byte of gap between cell pointers and cell content */ + int rc; /* Integer return code */ + int usableSize; /* Usable size of the page */ + + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( pPage->pBt ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( nByte>=0 ); /* Minimum cell size is 4 */ + assert( pPage->nFree>=nByte ); + assert( pPage->nOverflow==0 ); + usableSize = pPage->pBt->usableSize; + assert( nByte < usableSize-8 ); + + nFrag = data[hdr+7]; + assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); + gap = pPage->cellOffset + 2*pPage->nCell; + top = get2byteNotZero(&data[hdr+5]); + if( gap>top ) return SQLITE_CORRUPT_BKPT; + testcase( gap+2==top ); + testcase( gap+1==top ); + testcase( gap==top ); + + if( nFrag>=60 ){ + /* Always defragment highly fragmented pages */ + rc = defragmentPage(pPage); + if( rc ) return rc; + top = get2byteNotZero(&data[hdr+5]); + }else if( gap+2<=top ){ + /* Search the freelist looking for a free slot big enough to satisfy + ** the request. The allocation is made from the first free slot in + ** the list that is large enough to accomadate it. + */ + int pc, addr; + for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){ + int size; /* Size of the free slot */ + if( pc>usableSize-4 || pc=nByte ){ + int x = size - nByte; + testcase( x==4 ); + testcase( x==3 ); + if( x<4 ){ + /* Remove the slot from the free-list. Update the number of + ** fragmented bytes within the page. */ + memcpy(&data[addr], &data[pc], 2); + data[hdr+7] = (u8)(nFrag + x); + }else if( size+pc > usableSize ){ + return SQLITE_CORRUPT_BKPT; + }else{ + /* The slot remains on the free-list. Reduce its size to account + ** for the portion used by the new allocation. */ + put2byte(&data[pc+2], x); + } + *pIdx = pc + x; + return SQLITE_OK; + } + } + } + + /* Check to make sure there is enough space in the gap to satisfy + ** the allocation. If not, defragment. + */ + testcase( gap+2+nByte==top ); + if( gap+2+nByte>top ){ + rc = defragmentPage(pPage); + if( rc ) return rc; + top = get2byteNotZero(&data[hdr+5]); + assert( gap+nByte<=top ); + } + + + /* Allocate memory from the gap in between the cell pointer array + ** and the cell content area. The btreeInitPage() call has already + ** validated the freelist. Given that the freelist is valid, there + ** is no way that the allocation can extend off the end of the page. + ** The assert() below verifies the previous sentence. + */ + top -= nByte; + put2byte(&data[hdr+5], top); + assert( top+nByte <= pPage->pBt->usableSize ); + *pIdx = top; + return SQLITE_OK; +} + +/* +** Return a section of the pPage->aData to the freelist. +** The first byte of the new free block is pPage->aDisk[start] +** and the size of the block is "size" bytes. +** +** Most of the effort here is involved in coalesing adjacent +** free blocks into a single big free block. +*/ +static int freeSpace(MemPage *pPage, int start, int size){ + int addr, pbegin, hdr; + int iLast; /* Largest possible freeblock offset */ + unsigned char *data = pPage->aData; + + assert( pPage->pBt!=0 ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( start>=pPage->hdrOffset+6+pPage->childPtrSize ); + assert( (start + size)<=pPage->pBt->usableSize ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( size>=0 ); /* Minimum cell size is 4 */ + + if( pPage->pBt->secureDelete ){ + /* Overwrite deleted information with zeros when the secure_delete + ** option is enabled */ + memset(&data[start], 0, size); + } + + /* Add the space back into the linked list of freeblocks. Note that + ** even though the freeblock list was checked by btreeInitPage(), + ** btreeInitPage() did not detect overlapping cells or + ** freeblocks that overlapped cells. Nor does it detect when the + ** cell content area exceeds the value in the page header. If these + ** situations arise, then subsequent insert operations might corrupt + ** the freelist. So we do need to check for corruption while scanning + ** the freelist. + */ + hdr = pPage->hdrOffset; + addr = hdr + 1; + iLast = pPage->pBt->usableSize - 4; + assert( start<=iLast ); + while( (pbegin = get2byte(&data[addr]))0 ){ + if( pbeginiLast ){ + return SQLITE_CORRUPT_BKPT; + } + assert( pbegin>addr || pbegin==0 ); + put2byte(&data[addr], start); + put2byte(&data[start], pbegin); + put2byte(&data[start+2], size); + pPage->nFree = pPage->nFree + (u16)size; + + /* Coalesce adjacent free blocks */ + addr = hdr + 1; + while( (pbegin = get2byte(&data[addr]))>0 ){ + int pnext, psize, x; + assert( pbegin>addr ); + assert( pbegin<=pPage->pBt->usableSize-4 ); + pnext = get2byte(&data[pbegin]); + psize = get2byte(&data[pbegin+2]); + if( pbegin + psize + 3 >= pnext && pnext>0 ){ + int frag = pnext - (pbegin+psize); + if( (frag<0) || (frag>(int)data[hdr+7]) ){ + return SQLITE_CORRUPT_BKPT; + } + data[hdr+7] -= (u8)frag; + x = get2byte(&data[pnext]); + put2byte(&data[pbegin], x); + x = pnext + get2byte(&data[pnext+2]) - pbegin; + put2byte(&data[pbegin+2], x); + }else{ + addr = pbegin; + } + } + + /* If the cell content area begins with a freeblock, remove it. */ + if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){ + int top; + pbegin = get2byte(&data[hdr+1]); + memcpy(&data[hdr+1], &data[pbegin], 2); + top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]); + put2byte(&data[hdr+5], top); + } + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + return SQLITE_OK; +} + +/* +** Decode the flags byte (the first byte of the header) for a page +** and initialize fields of the MemPage structure accordingly. +** +** Only the following combinations are supported. Anything different +** indicates a corrupt database files: +** +** PTF_ZERODATA +** PTF_ZERODATA | PTF_LEAF +** PTF_LEAFDATA | PTF_INTKEY +** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF +*/ +static int decodeFlags(MemPage *pPage, int flagByte){ + BtShared *pBt; /* A copy of pPage->pBt */ + + assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); + flagByte &= ~PTF_LEAF; + pPage->childPtrSize = 4-4*pPage->leaf; + pBt = pPage->pBt; + if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ + pPage->intKey = 1; + pPage->hasData = pPage->leaf; + pPage->maxLocal = pBt->maxLeaf; + pPage->minLocal = pBt->minLeaf; + }else if( flagByte==PTF_ZERODATA ){ + pPage->intKey = 0; + pPage->hasData = 0; + pPage->maxLocal = pBt->maxLocal; + pPage->minLocal = pBt->minLocal; + }else{ + return SQLITE_CORRUPT_BKPT; + } + return SQLITE_OK; +} + +/* +** Initialize the auxiliary information for a disk block. +** +** Return SQLITE_OK on success. If we see that the page does +** not contain a well-formed database page, then return +** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not +** guarantee that the page is well-formed. It only shows that +** we failed to detect any corruption. +*/ +static int btreeInitPage(MemPage *pPage){ + + assert( pPage->pBt!=0 ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); + assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); + assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); + + if( !pPage->isInit ){ + u16 pc; /* Address of a freeblock within pPage->aData[] */ + u8 hdr; /* Offset to beginning of page header */ + u8 *data; /* Equal to pPage->aData */ + BtShared *pBt; /* The main btree structure */ + int usableSize; /* Amount of usable space on each page */ + u16 cellOffset; /* Offset from start of page to first cell pointer */ + int nFree; /* Number of unused bytes on the page */ + int top; /* First byte of the cell content area */ + int iCellFirst; /* First allowable cell or freeblock offset */ + int iCellLast; /* Last possible cell or freeblock offset */ + + pBt = pPage->pBt; + + hdr = pPage->hdrOffset; + data = pPage->aData; + if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT; + assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); + pPage->maskPage = (u16)(pBt->pageSize - 1); + pPage->nOverflow = 0; + usableSize = pBt->usableSize; + pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf; + top = get2byteNotZero(&data[hdr+5]); + pPage->nCell = get2byte(&data[hdr+3]); + if( pPage->nCell>MX_CELL(pBt) ){ + /* To many cells for a single page. The page must be corrupt */ + return SQLITE_CORRUPT_BKPT; + } + testcase( pPage->nCell==MX_CELL(pBt) ); + + /* A malformed database page might cause us to read past the end + ** of page when parsing a cell. + ** + ** The following block of code checks early to see if a cell extends + ** past the end of a page boundary and causes SQLITE_CORRUPT to be + ** returned if it does. + */ + iCellFirst = cellOffset + 2*pPage->nCell; + iCellLast = usableSize - 4; +#if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK) + { + int i; /* Index into the cell pointer array */ + int sz; /* Size of a cell */ + + if( !pPage->leaf ) iCellLast--; + for(i=0; inCell; i++){ + pc = get2byte(&data[cellOffset+i*2]); + testcase( pc==iCellFirst ); + testcase( pc==iCellLast ); + if( pciCellLast ){ + return SQLITE_CORRUPT_BKPT; + } + sz = cellSizePtr(pPage, &data[pc]); + testcase( pc+sz==usableSize ); + if( pc+sz>usableSize ){ + return SQLITE_CORRUPT_BKPT; + } + } + if( !pPage->leaf ) iCellLast++; + } +#endif + + /* Compute the total free space on the page */ + pc = get2byte(&data[hdr+1]); + nFree = data[hdr+7] + top; + while( pc>0 ){ + u16 next, size; + if( pciCellLast ){ + /* Start of free block is off the page */ + return SQLITE_CORRUPT_BKPT; + } + next = get2byte(&data[pc]); + size = get2byte(&data[pc+2]); + if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){ + /* Free blocks must be in ascending order. And the last byte of + ** the free-block must lie on the database page. */ + return SQLITE_CORRUPT_BKPT; + } + nFree = nFree + size; + pc = next; + } + + /* At this point, nFree contains the sum of the offset to the start + ** of the cell-content area plus the number of free bytes within + ** the cell-content area. If this is greater than the usable-size + ** of the page, then the page must be corrupted. This check also + ** serves to verify that the offset to the start of the cell-content + ** area, according to the page header, lies within the page. + */ + if( nFree>usableSize ){ + return SQLITE_CORRUPT_BKPT; + } + pPage->nFree = (u16)(nFree - iCellFirst); + pPage->isInit = 1; + } + return SQLITE_OK; +} + +/* +** Set up a raw page so that it looks like a database page holding +** no entries. +*/ +static void zeroPage(MemPage *pPage, int flags){ + unsigned char *data = pPage->aData; + BtShared *pBt = pPage->pBt; + u8 hdr = pPage->hdrOffset; + u16 first; + + assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); + assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); + assert( sqlite3PagerGetData(pPage->pDbPage) == data ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( sqlite3_mutex_held(pBt->mutex) ); + if( pBt->secureDelete ){ + memset(&data[hdr], 0, pBt->usableSize - hdr); + } + data[hdr] = (char)flags; + first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0); + memset(&data[hdr+1], 0, 4); + data[hdr+7] = 0; + put2byte(&data[hdr+5], pBt->usableSize); + pPage->nFree = (u16)(pBt->usableSize - first); + decodeFlags(pPage, flags); + pPage->hdrOffset = hdr; + pPage->cellOffset = first; + pPage->nOverflow = 0; + assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); + pPage->maskPage = (u16)(pBt->pageSize - 1); + pPage->nCell = 0; + pPage->isInit = 1; +} + + +/* +** Convert a DbPage obtained from the pager into a MemPage used by +** the btree layer. +*/ +static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ + MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); + pPage->aData = sqlite3PagerGetData(pDbPage); + pPage->pDbPage = pDbPage; + pPage->pBt = pBt; + pPage->pgno = pgno; + pPage->hdrOffset = pPage->pgno==1 ? 100 : 0; + return pPage; +} + +/* +** Get a page from the pager. Initialize the MemPage.pBt and +** MemPage.aData elements if needed. +** +** If the noContent flag is set, it means that we do not care about +** the content of the page at this time. So do not go to the disk +** to fetch the content. Just fill in the content with zeros for now. +** If in the future we call sqlite3PagerWrite() on this page, that +** means we have started to be concerned about content and the disk +** read should occur at that point. +*/ +static int btreeGetPage( + BtShared *pBt, /* The btree */ + Pgno pgno, /* Number of the page to fetch */ + MemPage **ppPage, /* Return the page in this parameter */ + int noContent /* Do not load page content if true */ +){ + int rc; + DbPage *pDbPage; + + assert( sqlite3_mutex_held(pBt->mutex) ); + rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent); + if( rc ) return rc; + *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); + return SQLITE_OK; +} + +/* +** Retrieve a page from the pager cache. If the requested page is not +** already in the pager cache return NULL. Initialize the MemPage.pBt and +** MemPage.aData elements if needed. +*/ +static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){ + DbPage *pDbPage; + assert( sqlite3_mutex_held(pBt->mutex) ); + pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); + if( pDbPage ){ + return btreePageFromDbPage(pDbPage, pgno, pBt); + } + return 0; +} + +/* +** Return the size of the database file in pages. If there is any kind of +** error, return ((unsigned int)-1). +*/ +static Pgno btreePagecount(BtShared *pBt){ + return pBt->nPage; +} +SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){ + assert( sqlite3BtreeHoldsMutex(p) ); + assert( ((p->pBt->nPage)&0x8000000)==0 ); + return (int)btreePagecount(p->pBt); +} + +/* +** Get a page from the pager and initialize it. This routine is just a +** convenience wrapper around separate calls to btreeGetPage() and +** btreeInitPage(). +** +** If an error occurs, then the value *ppPage is set to is undefined. It +** may remain unchanged, or it may be set to an invalid value. +*/ +static int getAndInitPage( + BtShared *pBt, /* The database file */ + Pgno pgno, /* Number of the page to get */ + MemPage **ppPage /* Write the page pointer here */ +){ + int rc; + assert( sqlite3_mutex_held(pBt->mutex) ); + + if( pgno>btreePagecount(pBt) ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + rc = btreeGetPage(pBt, pgno, ppPage, 0); + if( rc==SQLITE_OK ){ + rc = btreeInitPage(*ppPage); + if( rc!=SQLITE_OK ){ + releasePage(*ppPage); + } + } + } + + testcase( pgno==0 ); + assert( pgno!=0 || rc==SQLITE_CORRUPT ); + return rc; +} + +/* +** Release a MemPage. This should be called once for each prior +** call to btreeGetPage. +*/ +static void releasePage(MemPage *pPage){ + if( pPage ){ + assert( pPage->aData ); + assert( pPage->pBt ); + assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); + assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + sqlite3PagerUnref(pPage->pDbPage); + } +} + +/* +** During a rollback, when the pager reloads information into the cache +** so that the cache is restored to its original state at the start of +** the transaction, for each page restored this routine is called. +** +** This routine needs to reset the extra data section at the end of the +** page to agree with the restored data. +*/ +static void pageReinit(DbPage *pData){ + MemPage *pPage; + pPage = (MemPage *)sqlite3PagerGetExtra(pData); + assert( sqlite3PagerPageRefcount(pData)>0 ); + if( pPage->isInit ){ + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + pPage->isInit = 0; + if( sqlite3PagerPageRefcount(pData)>1 ){ + /* pPage might not be a btree page; it might be an overflow page + ** or ptrmap page or a free page. In those cases, the following + ** call to btreeInitPage() will likely return SQLITE_CORRUPT. + ** But no harm is done by this. And it is very important that + ** btreeInitPage() be called on every btree page so we make + ** the call for every page that comes in for re-initing. */ + btreeInitPage(pPage); + } + } +} + +/* +** Invoke the busy handler for a btree. +*/ +static int btreeInvokeBusyHandler(void *pArg){ + BtShared *pBt = (BtShared*)pArg; + assert( pBt->db ); + assert( sqlite3_mutex_held(pBt->db->mutex) ); + return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); +} + +/* +** Open a database file. +** +** zFilename is the name of the database file. If zFilename is NULL +** then an ephemeral database is created. The ephemeral database might +** be exclusively in memory, or it might use a disk-based memory cache. +** Either way, the ephemeral database will be automatically deleted +** when sqlite3BtreeClose() is called. +** +** If zFilename is ":memory:" then an in-memory database is created +** that is automatically destroyed when it is closed. +** +** The "flags" parameter is a bitmask that might contain bits +** BTREE_OMIT_JOURNAL and/or BTREE_NO_READLOCK. The BTREE_NO_READLOCK +** bit is also set if the SQLITE_NoReadlock flags is set in db->flags. +** These flags are passed through into sqlite3PagerOpen() and must +** be the same values as PAGER_OMIT_JOURNAL and PAGER_NO_READLOCK. +** +** If the database is already opened in the same database connection +** and we are in shared cache mode, then the open will fail with an +** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared +** objects in the same database connection since doing so will lead +** to problems with locking. +*/ +SQLITE_PRIVATE int sqlite3BtreeOpen( + const char *zFilename, /* Name of the file containing the BTree database */ + sqlite3 *db, /* Associated database handle */ + Btree **ppBtree, /* Pointer to new Btree object written here */ + int flags, /* Options */ + int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ +){ + sqlite3_vfs *pVfs; /* The VFS to use for this btree */ + BtShared *pBt = 0; /* Shared part of btree structure */ + Btree *p; /* Handle to return */ + sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */ + int rc = SQLITE_OK; /* Result code from this function */ + u8 nReserve; /* Byte of unused space on each page */ + unsigned char zDbHeader[100]; /* Database header content */ + + /* True if opening an ephemeral, temporary database */ + const int isTempDb = zFilename==0 || zFilename[0]==0; + + /* Set the variable isMemdb to true for an in-memory database, or + ** false for a file-based database. + */ +#ifdef SQLITE_OMIT_MEMORYDB + const int isMemdb = 0; +#else + const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0) + || (isTempDb && sqlite3TempInMemory(db)); +#endif + + assert( db!=0 ); + assert( sqlite3_mutex_held(db->mutex) ); + assert( (flags&0xff)==flags ); /* flags fit in 8 bits */ + + /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */ + assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 ); + + /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ + assert( (flags & BTREE_SINGLE)==0 || isTempDb ); + + if( db->flags & SQLITE_NoReadlock ){ + flags |= BTREE_NO_READLOCK; + } + if( isMemdb ){ + flags |= BTREE_MEMORY; + } + if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){ + vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; + } + pVfs = db->pVfs; + p = sqlite3MallocZero(sizeof(Btree)); + if( !p ){ + return SQLITE_NOMEM; + } + p->inTrans = TRANS_NONE; + p->db = db; +#ifndef SQLITE_OMIT_SHARED_CACHE + p->lock.pBtree = p; + p->lock.iTable = 1; +#endif + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) + /* + ** If this Btree is a candidate for shared cache, try to find an + ** existing BtShared object that we can share with + */ + if( isMemdb==0 && isTempDb==0 ){ + if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ + int nFullPathname = pVfs->mxPathname+1; + char *zFullPathname = sqlite3Malloc(nFullPathname); + sqlite3_mutex *mutexShared; + p->sharable = 1; + if( !zFullPathname ){ + sqlite3_free(p); + return SQLITE_NOMEM; + } + sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname); + mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN); + sqlite3_mutex_enter(mutexOpen); + mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex_enter(mutexShared); + for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ + assert( pBt->nRef>0 ); + if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager)) + && sqlite3PagerVfs(pBt->pPager)==pVfs ){ + int iDb; + for(iDb=db->nDb-1; iDb>=0; iDb--){ + Btree *pExisting = db->aDb[iDb].pBt; + if( pExisting && pExisting->pBt==pBt ){ + sqlite3_mutex_leave(mutexShared); + sqlite3_mutex_leave(mutexOpen); + sqlite3_free(zFullPathname); + sqlite3_free(p); + return SQLITE_CONSTRAINT; + } + } + p->pBt = pBt; + pBt->nRef++; + break; + } + } + sqlite3_mutex_leave(mutexShared); + sqlite3_free(zFullPathname); + } +#ifdef SQLITE_DEBUG + else{ + /* In debug mode, we mark all persistent databases as sharable + ** even when they are not. This exercises the locking code and + ** gives more opportunity for asserts(sqlite3_mutex_held()) + ** statements to find locking problems. + */ + p->sharable = 1; + } +#endif + } +#endif + if( pBt==0 ){ + /* + ** The following asserts make sure that structures used by the btree are + ** the right size. This is to guard against size changes that result + ** when compiling on a different architecture. + */ + assert( sizeof(i64)==8 || sizeof(i64)==4 ); + assert( sizeof(u64)==8 || sizeof(u64)==4 ); + assert( sizeof(u32)==4 ); + assert( sizeof(u16)==2 ); + assert( sizeof(Pgno)==4 ); + + pBt = sqlite3MallocZero( sizeof(*pBt) ); + if( pBt==0 ){ + rc = SQLITE_NOMEM; + goto btree_open_out; + } + rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, + EXTRA_SIZE, flags, vfsFlags, pageReinit); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); + } + if( rc!=SQLITE_OK ){ + goto btree_open_out; + } + pBt->openFlags = (u8)flags; + pBt->db = db; + sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt); + p->pBt = pBt; + + pBt->pCursor = 0; + pBt->pPage1 = 0; + pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager); +#ifdef SQLITE_SECURE_DELETE + pBt->secureDelete = 1; +#endif + + // The database header just read could be corrupt but a valid header could exist in the WAL + // in a page1 frame. The original code below will accept and use any valid-looking page size + // in the potentially corrupt header, and use 0 otherwise. In either case, once Page1 is read + // using the pager (which will read the page from the WAL if the page is valid and present there) + // its header is parsed any any incorrect parameters obtained from the bad header will be + // corrected. However, in the former case where the page size in the initial header appears to be + // valid but is not actually correct, then the Pager Codec will be told the wrong page size. This + // causes a checksumming pager codec to fail the check on Page1, so a valid Page1 and db header + // cannot be read, and the database cannot be used. Since in the latter case the pager codec will + // be given a default page which can be used to read and validate Page1 (which can be read as a + // default sized page or the configured size), it is better here to just assume that the page + // size in the initial db header is not valid. This also causes two vacuum related parameters to + // use defaults but that will be corrected once page1 is read. + // + //pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); + //if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE + // || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ + if(1) { + pBt->pageSize = 0; +#ifndef SQLITE_OMIT_AUTOVACUUM + /* If the magic name ":memory:" will create an in-memory database, then + ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if + ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if + ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a + ** regular file-name. In this case the auto-vacuum applies as per normal. + */ + if( zFilename && !isMemdb ){ + pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); + pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); + } +#endif + nReserve = 0; + }else{ + nReserve = zDbHeader[20]; + pBt->pageSizeFixed = 1; +#ifndef SQLITE_OMIT_AUTOVACUUM + pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); + pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); +#endif + } + rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); + if( rc ) goto btree_open_out; + pBt->usableSize = pBt->pageSize - nReserve; + assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) + /* Add the new BtShared object to the linked list sharable BtShareds. + */ + if( p->sharable ){ + sqlite3_mutex *mutexShared; + pBt->nRef = 1; + mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ + pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); + if( pBt->mutex==0 ){ + rc = SQLITE_NOMEM; + db->mallocFailed = 0; + goto btree_open_out; + } + } + sqlite3_mutex_enter(mutexShared); + pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); + GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; + sqlite3_mutex_leave(mutexShared); + } +#endif + } + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) + /* If the new Btree uses a sharable pBtShared, then link the new + ** Btree into the list of all sharable Btrees for the same connection. + ** The list is kept in ascending order by pBt address. + */ + if( p->sharable ){ + int i; + Btree *pSib; + for(i=0; inDb; i++){ + if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ + while( pSib->pPrev ){ pSib = pSib->pPrev; } + if( p->pBtpBt ){ + p->pNext = pSib; + p->pPrev = 0; + pSib->pPrev = p; + }else{ + while( pSib->pNext && pSib->pNext->pBtpBt ){ + pSib = pSib->pNext; + } + p->pNext = pSib->pNext; + p->pPrev = pSib; + if( p->pNext ){ + p->pNext->pPrev = p; + } + pSib->pNext = p; + } + break; + } + } + } +#endif + *ppBtree = p; + +btree_open_out: + if( rc!=SQLITE_OK ){ + if( pBt && pBt->pPager ){ + sqlite3PagerClose(pBt->pPager); + } + sqlite3_free(pBt); + sqlite3_free(p); + *ppBtree = 0; + }else{ + /* If the B-Tree was successfully opened, set the pager-cache size to the + ** default value. Except, when opening on an existing shared pager-cache, + ** do not change the pager-cache size. + */ + if( sqlite3BtreeSchema(p, 0, 0)==0 ){ + sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE); + } + } + if( mutexOpen ){ + assert( sqlite3_mutex_held(mutexOpen) ); + sqlite3_mutex_leave(mutexOpen); + } + return rc; +} + +/* +** Decrement the BtShared.nRef counter. When it reaches zero, +** remove the BtShared structure from the sharing list. Return +** true if the BtShared.nRef counter reaches zero and return +** false if it is still positive. +*/ +static int removeFromSharingList(BtShared *pBt){ +#ifndef SQLITE_OMIT_SHARED_CACHE + sqlite3_mutex *pMaster; + BtShared *pList; + int removed = 0; + + assert( sqlite3_mutex_notheld(pBt->mutex) ); + pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex_enter(pMaster); + pBt->nRef--; + if( pBt->nRef<=0 ){ + if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ + GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; + }else{ + pList = GLOBAL(BtShared*,sqlite3SharedCacheList); + while( ALWAYS(pList) && pList->pNext!=pBt ){ + pList=pList->pNext; + } + if( ALWAYS(pList) ){ + pList->pNext = pBt->pNext; + } + } + if( SQLITE_THREADSAFE ){ + sqlite3_mutex_free(pBt->mutex); + } + removed = 1; + } + sqlite3_mutex_leave(pMaster); + return removed; +#else + return 1; +#endif +} + +/* +** Make sure pBt->pTmpSpace points to an allocation of +** MX_CELL_SIZE(pBt) bytes. +*/ +static void allocateTempSpace(BtShared *pBt){ + if( !pBt->pTmpSpace ){ + pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); + } +} + +/* +** Free the pBt->pTmpSpace allocation +*/ +static void freeTempSpace(BtShared *pBt){ + sqlite3PageFree( pBt->pTmpSpace); + pBt->pTmpSpace = 0; +} + +/* +** Close an open database and invalidate all cursors. +*/ +SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){ + BtShared *pBt = p->pBt; + BtCursor *pCur; + + /* Close all cursors opened via this handle. */ + assert( sqlite3_mutex_held(p->db->mutex) ); + sqlite3BtreeEnter(p); + pCur = pBt->pCursor; + while( pCur ){ + BtCursor *pTmp = pCur; + pCur = pCur->pNext; + if( pTmp->pBtree==p ){ + sqlite3BtreeCloseCursor(pTmp); + } + } + + /* Rollback any active transaction and free the handle structure. + ** The call to sqlite3BtreeRollback() drops any table-locks held by + ** this handle. + */ + sqlite3BtreeRollback(p); + sqlite3BtreeLeave(p); + + /* If there are still other outstanding references to the shared-btree + ** structure, return now. The remainder of this procedure cleans + ** up the shared-btree. + */ + assert( p->wantToLock==0 && p->locked==0 ); + if( !p->sharable || removeFromSharingList(pBt) ){ + /* The pBt is no longer on the sharing list, so we can access + ** it without having to hold the mutex. + ** + ** Clean out and delete the BtShared object. + */ + assert( !pBt->pCursor ); + sqlite3PagerClose(pBt->pPager); + if( pBt->xFreeSchema && pBt->pSchema ){ + pBt->xFreeSchema(pBt->pSchema); + } + sqlite3DbFree(0, pBt->pSchema); + freeTempSpace(pBt); + sqlite3_free(pBt); + } + +#ifndef SQLITE_OMIT_SHARED_CACHE + assert( p->wantToLock==0 ); + assert( p->locked==0 ); + if( p->pPrev ) p->pPrev->pNext = p->pNext; + if( p->pNext ) p->pNext->pPrev = p->pPrev; +#endif + + sqlite3_free(p); + return SQLITE_OK; +} + +/* +** Change the limit on the number of pages allowed in the cache. +** +** The maximum number of cache pages is set to the absolute +** value of mxPage. If mxPage is negative, the pager will +** operate asynchronously - it will not stop to do fsync()s +** to insure data is written to the disk surface before +** continuing. Transactions still work if synchronous is off, +** and the database cannot be corrupted if this program +** crashes. But if the operating system crashes or there is +** an abrupt power failure when synchronous is off, the database +** could be left in an inconsistent and unrecoverable state. +** Synchronous is on by default so database corruption is not +** normally a worry. +*/ +SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ + BtShared *pBt = p->pBt; + assert( sqlite3_mutex_held(p->db->mutex) ); + sqlite3BtreeEnter(p); + sqlite3PagerSetCachesize(pBt->pPager, mxPage); + sqlite3BtreeLeave(p); + return SQLITE_OK; +} + +/* +** Change the way data is synced to disk in order to increase or decrease +** how well the database resists damage due to OS crashes and power +** failures. Level 1 is the same as asynchronous (no syncs() occur and +** there is a high probability of damage) Level 2 is the default. There +** is a very low but non-zero probability of damage. Level 3 reduces the +** probability of damage to near zero but with a write performance reduction. +*/ +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel( + Btree *p, /* The btree to set the safety level on */ + int level, /* PRAGMA synchronous. 1=OFF, 2=NORMAL, 3=FULL */ + int fullSync, /* PRAGMA fullfsync. */ + int ckptFullSync /* PRAGMA checkpoint_fullfync */ +){ + BtShared *pBt = p->pBt; + assert( sqlite3_mutex_held(p->db->mutex) ); + assert( level>=1 && level<=3 ); + sqlite3BtreeEnter(p); + sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync); + sqlite3BtreeLeave(p); + return SQLITE_OK; +} +#endif + +/* +** Return TRUE if the given btree is set to safety level 1. In other +** words, return TRUE if no sync() occurs on the disk files. +*/ +SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){ + BtShared *pBt = p->pBt; + int rc; + assert( sqlite3_mutex_held(p->db->mutex) ); + sqlite3BtreeEnter(p); + assert( pBt && pBt->pPager ); + rc = sqlite3PagerNosync(pBt->pPager); + sqlite3BtreeLeave(p); + return rc; +} + +#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) +/* +** Change the default pages size and the number of reserved bytes per page. +** Or, if the page size has already been fixed, return SQLITE_READONLY +** without changing anything. +** +** The page size must be a power of 2 between 512 and 65536. If the page +** size supplied does not meet this constraint then the page size is not +** changed. +** +** Page sizes are constrained to be a power of two so that the region +** of the database file used for locking (beginning at PENDING_BYTE, +** the first byte past the 1GB boundary, 0x40000000) needs to occur +** at the beginning of a page. +** +** If parameter nReserve is less than zero, then the number of reserved +** bytes per page is left unchanged. +** +** If the iFix!=0 then the pageSizeFixed flag is set so that the page size +** and autovacuum mode can no longer be changed. +*/ +SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ + int rc = SQLITE_OK; + BtShared *pBt = p->pBt; + assert( nReserve>=-1 && nReserve<=255 ); + sqlite3BtreeEnter(p); + if( pBt->pageSizeFixed ){ + sqlite3BtreeLeave(p); + return SQLITE_READONLY; + } + if( nReserve<0 ){ + nReserve = pBt->pageSize - pBt->usableSize; + } + assert( nReserve>=0 && nReserve<=255 ); + if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && + ((pageSize-1)&pageSize)==0 ){ + assert( (pageSize & 7)==0 ); + assert( !pBt->pPage1 && !pBt->pCursor ); + pBt->pageSize = (u32)pageSize; + freeTempSpace(pBt); + } + rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); + pBt->usableSize = pBt->pageSize - (u16)nReserve; + if( iFix ) pBt->pageSizeFixed = 1; + sqlite3BtreeLeave(p); + return rc; +} + +/* +** Return the currently defined page size +*/ +SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){ + return p->pBt->pageSize; +} + +/* +** Return the number of bytes of space at the end of every page that +** are intentually left unused. This is the "reserved" space that is +** sometimes used by extensions. +*/ +SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){ + int n; + sqlite3BtreeEnter(p); + n = p->pBt->pageSize - p->pBt->usableSize; + sqlite3BtreeLeave(p); + return n; +} + +/* +** Set the maximum page count for a database if mxPage is positive. +** No changes are made if mxPage is 0 or negative. +** Regardless of the value of mxPage, return the maximum page count. +*/ +SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ + int n; + sqlite3BtreeEnter(p); + n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); + sqlite3BtreeLeave(p); + return n; +} + +/* +** Set the secureDelete flag if newFlag is 0 or 1. If newFlag is -1, +** then make no changes. Always return the value of the secureDelete +** setting after the change. +*/ +SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ + int b; + if( p==0 ) return 0; + sqlite3BtreeEnter(p); + if( newFlag>=0 ){ + p->pBt->secureDelete = (newFlag!=0) ? 1 : 0; + } + b = p->pBt->secureDelete; + sqlite3BtreeLeave(p); + return b; +} +#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */ + +/* +** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' +** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it +** is disabled. The default value for the auto-vacuum property is +** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. +*/ +SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ +#ifdef SQLITE_OMIT_AUTOVACUUM + return SQLITE_READONLY; +#else + BtShared *pBt = p->pBt; + int rc = SQLITE_OK; + u8 av = (u8)autoVacuum; + + sqlite3BtreeEnter(p); + if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){ + rc = SQLITE_READONLY; + }else{ + pBt->autoVacuum = av ?1:0; + pBt->incrVacuum = av==2 ?1:0; + } + sqlite3BtreeLeave(p); + return rc; +#endif +} + +/* +** Return the value of the 'auto-vacuum' property. If auto-vacuum is +** enabled 1 is returned. Otherwise 0. +*/ +SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){ +#ifdef SQLITE_OMIT_AUTOVACUUM + return BTREE_AUTOVACUUM_NONE; +#else + int rc; + sqlite3BtreeEnter(p); + rc = ( + (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: + (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: + BTREE_AUTOVACUUM_INCR + ); + sqlite3BtreeLeave(p); + return rc; +#endif +} + + +/* +** Get a reference to pPage1 of the database file. This will +** also acquire a readlock on that file. +** +** SQLITE_OK is returned on success. If the file is not a +** well-formed database file, then SQLITE_CORRUPT is returned. +** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM +** is returned if we run out of memory. +*/ +static int lockBtree(BtShared *pBt){ + int rc; /* Result code from subfunctions */ + MemPage *pPage1; /* Page 1 of the database file */ + int nPage; /* Number of pages in the database */ + int nPageFile = 0; /* Number of pages in the database file */ + int nPageHeader; /* Number of pages in the database according to hdr */ + + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( pBt->pPage1==0 ); + rc = sqlite3PagerSharedLock(pBt->pPager); + if( rc!=SQLITE_OK ) return rc; + rc = btreeGetPage(pBt, 1, &pPage1, 0); + if( rc!=SQLITE_OK ) return rc; + + /* Do some checking to help insure the file we opened really is + ** a valid database file. + */ + nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); + sqlite3PagerPagecount(pBt->pPager, &nPageFile); + if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){ + nPage = nPageFile; + } + if( nPage>0 ){ + u32 pageSize; + u32 usableSize; + u8 *page1 = pPage1->aData; + rc = SQLITE_NOTADB; + if( memcmp(page1, zMagicHeader, 16)!=0 ){ + goto page1_init_failed; + } + +#ifdef SQLITE_OMIT_WAL + if( page1[18]>1 ){ + pBt->readOnly = 1; + } + if( page1[19]>1 ){ + goto page1_init_failed; + } +#else + if( page1[18]>2 ){ + pBt->readOnly = 1; + } + if( page1[19]>2 ){ + goto page1_init_failed; + } + + /* If the write version is set to 2, this database should be accessed + ** in WAL mode. If the log is not already open, open it now. Then + ** return SQLITE_OK and return without populating BtShared.pPage1. + ** The caller detects this and calls this function again. This is + ** required as the version of page 1 currently in the page1 buffer + ** may not be the latest version - there may be a newer one in the log + ** file. + */ + if( page1[19]==2 && pBt->doNotUseWAL==0 ){ + int isOpen = 0; + rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen); + if( rc!=SQLITE_OK ){ + goto page1_init_failed; + }else if( isOpen==0 ){ + releasePage(pPage1); + return SQLITE_OK; + } + rc = SQLITE_NOTADB; + } +#endif + + /* The maximum embedded fraction must be exactly 25%. And the minimum + ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data. + ** The original design allowed these amounts to vary, but as of + ** version 3.6.0, we require them to be fixed. + */ + if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ + goto page1_init_failed; + } + pageSize = (page1[16]<<8) | (page1[17]<<16); + if( ((pageSize-1)&pageSize)!=0 + || pageSize>SQLITE_MAX_PAGE_SIZE + || pageSize<=256 + ){ + goto page1_init_failed; + } + assert( (pageSize & 7)==0 ); + usableSize = pageSize - page1[20]; + if( (u32)pageSize!=pBt->pageSize || (u32)usableSize!=pBt->usableSize){ + /* After reading the first page of the database assuming a page size + ** of BtShared.pageSize, we have discovered that the page-size is + ** actually pageSize OR that the reserveSize (and therefore usableSize) + ** previously read from the potentially corrupt database header was wrong. + ** Set the new values, unlock the database, leave pBt->pPage1 at + ** zero and return SQLITE_OK. The caller will call this function + ** again with the correct page-size. + */ + releasePage(pPage1); + pBt->usableSize = usableSize; + pBt->pageSize = pageSize; + freeTempSpace(pBt); + rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, + pageSize-usableSize); + return rc; + } + if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){ + rc = SQLITE_CORRUPT_BKPT; + goto page1_init_failed; + } + if( usableSize<480 ){ + goto page1_init_failed; + } + pBt->pageSize = pageSize; + pBt->usableSize = usableSize; +#ifndef SQLITE_OMIT_AUTOVACUUM + pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); + pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); +#endif + } + + /* maxLocal is the maximum amount of payload to store locally for + ** a cell. Make sure it is small enough so that at least minFanout + ** cells can will fit on one page. We assume a 10-byte page header. + ** Besides the payload, the cell must store: + ** 2-byte pointer to the cell + ** 4-byte child pointer + ** 9-byte nKey value + ** 4-byte nData value + ** 4-byte overflow page pointer + ** So a cell consists of a 2-byte pointer, a header which is as much as + ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow + ** page pointer. + */ + pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23); + pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23); + pBt->maxLeaf = (u16)(pBt->usableSize - 35); + pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23); + assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); + pBt->pPage1 = pPage1; + pBt->nPage = nPage; + return SQLITE_OK; + +page1_init_failed: + releasePage(pPage1); + pBt->pPage1 = 0; + return rc; +} + +/* +** If there are no outstanding cursors and we are not in the middle +** of a transaction but there is a read lock on the database, then +** this routine unrefs the first page of the database file which +** has the effect of releasing the read lock. +** +** If there is a transaction in progress, this routine is a no-op. +*/ +static void unlockBtreeIfUnused(BtShared *pBt){ + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( pBt->pCursor==0 || pBt->inTransaction>TRANS_NONE ); + if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){ + assert( pBt->pPage1->aData ); + assert( sqlite3PagerRefcount(pBt->pPager)==1 ); + assert( pBt->pPage1->aData ); + releasePage(pBt->pPage1); + pBt->pPage1 = 0; + } +} + +/* +** If pBt points to an empty file then convert that empty file +** into a new empty database by initializing the first page of +** the database. +*/ +static int newDatabase(BtShared *pBt){ + MemPage *pP1; + unsigned char *data; + int rc; + + assert( sqlite3_mutex_held(pBt->mutex) ); + if( pBt->nPage>0 ){ + return SQLITE_OK; + } + pP1 = pBt->pPage1; + assert( pP1!=0 ); + data = pP1->aData; + rc = sqlite3PagerWrite(pP1->pDbPage); + if( rc ) return rc; + memcpy(data, zMagicHeader, sizeof(zMagicHeader)); + assert( sizeof(zMagicHeader)==16 ); + data[16] = (u8)((pBt->pageSize>>8)&0xff); + data[17] = (u8)((pBt->pageSize>>16)&0xff); + data[18] = 1; + data[19] = 1; + assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); + data[20] = (u8)(pBt->pageSize - pBt->usableSize); + data[21] = 64; + data[22] = 32; + data[23] = 32; + memset(&data[24], 0, 100-24); + zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); + pBt->pageSizeFixed = 1; +#ifndef SQLITE_OMIT_AUTOVACUUM + assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); + assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); + put4byte(&data[36 + 4*4], pBt->autoVacuum); + put4byte(&data[36 + 7*4], pBt->incrVacuum); +#endif + pBt->nPage = 1; + data[31] = 1; + return SQLITE_OK; +} + +/* +** Attempt to start a new transaction. A write-transaction +** is started if the second argument is nonzero, otherwise a read- +** transaction. If the second argument is 2 or more and exclusive +** transaction is started, meaning that no other process is allowed +** to access the database. A preexisting transaction may not be +** upgraded to exclusive by calling this routine a second time - the +** exclusivity flag only works for a new transaction. +** +** A write-transaction must be started before attempting any +** changes to the database. None of the following routines +** will work unless a transaction is started first: +** +** sqlite3BtreeCreateTable() +** sqlite3BtreeCreateIndex() +** sqlite3BtreeClearTable() +** sqlite3BtreeDropTable() +** sqlite3BtreeInsert() +** sqlite3BtreeDelete() +** sqlite3BtreeUpdateMeta() +** +** If an initial attempt to acquire the lock fails because of lock contention +** and the database was previously unlocked, then invoke the busy handler +** if there is one. But if there was previously a read-lock, do not +** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is +** returned when there is already a read-lock in order to avoid a deadlock. +** +** Suppose there are two processes A and B. A has a read lock and B has +** a reserved lock. B tries to promote to exclusive but is blocked because +** of A's read lock. A tries to promote to reserved but is blocked by B. +** One or the other of the two processes must give way or there can be +** no progress. By returning SQLITE_BUSY and not invoking the busy callback +** when A already has a read lock, we encourage A to give up and let B +** proceed. +*/ +SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ + sqlite3 *pBlock = 0; + BtShared *pBt = p->pBt; + int rc = SQLITE_OK; + + sqlite3BtreeEnter(p); + btreeIntegrity(p); + + /* If the btree is already in a write-transaction, or it + ** is already in a read-transaction and a read-transaction + ** is requested, this is a no-op. + */ + if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ + goto trans_begun; + } + + /* Write transactions are not possible on a read-only database */ + if( pBt->readOnly && wrflag ){ + rc = SQLITE_READONLY; + goto trans_begun; + } + +#ifndef SQLITE_OMIT_SHARED_CACHE + /* If another database handle has already opened a write transaction + ** on this shared-btree structure and a second write transaction is + ** requested, return SQLITE_LOCKED. + */ + if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){ + pBlock = pBt->pWriter->db; + }else if( wrflag>1 ){ + BtLock *pIter; + for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ + if( pIter->pBtree!=p ){ + pBlock = pIter->pBtree->db; + break; + } + } + } + if( pBlock ){ + sqlite3ConnectionBlocked(p->db, pBlock); + rc = SQLITE_LOCKED_SHAREDCACHE; + goto trans_begun; + } +#endif + + /* Any read-only or read-write transaction implies a read-lock on + ** page 1. So if some other shared-cache client already has a write-lock + ** on page 1, the transaction cannot be opened. */ + rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); + if( SQLITE_OK!=rc ) goto trans_begun; + + pBt->initiallyEmpty = (u8)(pBt->nPage==0); + do { + /* Call lockBtree() until either pBt->pPage1 is populated or + ** lockBtree() returns something other than SQLITE_OK. lockBtree() + ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after + ** reading page 1 it discovers that the page-size of the database + ** file is not pBt->pageSize. In this case lockBtree() will update + ** pBt->pageSize to the page-size of the file on disk. + */ + while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) ); + + if( rc==SQLITE_OK && wrflag ){ + if( pBt->readOnly ){ + rc = SQLITE_READONLY; + }else{ + rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); + if( rc==SQLITE_OK ){ + rc = newDatabase(pBt); + } + } + } + + if( rc!=SQLITE_OK ){ + unlockBtreeIfUnused(pBt); + } + }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && + btreeInvokeBusyHandler(pBt) ); + + if( rc==SQLITE_OK ){ + if( p->inTrans==TRANS_NONE ){ + pBt->nTransaction++; +#ifndef SQLITE_OMIT_SHARED_CACHE + if( p->sharable ){ + assert( p->lock.pBtree==p && p->lock.iTable==1 ); + p->lock.eLock = READ_LOCK; + p->lock.pNext = pBt->pLock; + pBt->pLock = &p->lock; + } +#endif + } + p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); + if( p->inTrans>pBt->inTransaction ){ + pBt->inTransaction = p->inTrans; + } + if( wrflag ){ + MemPage *pPage1 = pBt->pPage1; +#ifndef SQLITE_OMIT_SHARED_CACHE + assert( !pBt->pWriter ); + pBt->pWriter = p; + pBt->isExclusive = (u8)(wrflag>1); +#endif + + /* If the db-size header field is incorrect (as it may be if an old + ** client has been writing the database file), update it now. Doing + ** this sooner rather than later means the database size can safely + ** re-read the database size from page 1 if a savepoint or transaction + ** rollback occurs within the transaction. + */ + if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){ + rc = sqlite3PagerWrite(pPage1->pDbPage); + if( rc==SQLITE_OK ){ + put4byte(&pPage1->aData[28], pBt->nPage); + } + } + } + } + + +trans_begun: + if( rc==SQLITE_OK && wrflag ){ + /* This call makes sure that the pager has the correct number of + ** open savepoints. If the second parameter is greater than 0 and + ** the sub-journal is not already open, then it will be opened here. + */ + rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); + } + + btreeIntegrity(p); + sqlite3BtreeLeave(p); + return rc; +} + +#ifndef SQLITE_OMIT_AUTOVACUUM + +/* +** Set the pointer-map entries for all children of page pPage. Also, if +** pPage contains cells that point to overflow pages, set the pointer +** map entries for the overflow pages as well. +*/ +static int setChildPtrmaps(MemPage *pPage){ + int i; /* Counter variable */ + int nCell; /* Number of cells in page pPage */ + int rc; /* Return code */ + BtShared *pBt = pPage->pBt; + u8 isInitOrig = pPage->isInit; + Pgno pgno = pPage->pgno; + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + rc = btreeInitPage(pPage); + if( rc!=SQLITE_OK ){ + goto set_child_ptrmaps_out; + } + nCell = pPage->nCell; + + for(i=0; ileaf ){ + Pgno childPgno = get4byte(pCell); + ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); + } + } + + if( !pPage->leaf ){ + Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); + ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); + } + +set_child_ptrmaps_out: + pPage->isInit = isInitOrig; + return rc; +} + +/* +** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so +** that it points to iTo. Parameter eType describes the type of pointer to +** be modified, as follows: +** +** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child +** page of pPage. +** +** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow +** page pointed to by one of the cells on pPage. +** +** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next +** overflow page in the list. +*/ +static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + if( eType==PTRMAP_OVERFLOW2 ){ + /* The pointer is always the first 4 bytes of the page in this case. */ + if( get4byte(pPage->aData)!=iFrom ){ + return SQLITE_CORRUPT_BKPT; + } + put4byte(pPage->aData, iTo); + }else{ + u8 isInitOrig = pPage->isInit; + int i; + int nCell; + + btreeInitPage(pPage); + nCell = pPage->nCell; + + for(i=0; iaData[pPage->hdrOffset+8])!=iFrom ){ + return SQLITE_CORRUPT_BKPT; + } + put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); + } + + pPage->isInit = isInitOrig; + } + return SQLITE_OK; +} + + +/* +** Move the open database page pDbPage to location iFreePage in the +** database. The pDbPage reference remains valid. +** +** The isCommit flag indicates that there is no need to remember that +** the journal needs to be sync()ed before database page pDbPage->pgno +** can be written to. The caller has already promised not to write to that +** page. +*/ +static int relocatePage( + BtShared *pBt, /* Btree */ + MemPage *pDbPage, /* Open page to move */ + u8 eType, /* Pointer map 'type' entry for pDbPage */ + Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ + Pgno iFreePage, /* The location to move pDbPage to */ + int isCommit /* isCommit flag passed to sqlite3PagerMovepage */ +){ + MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ + Pgno iDbPage = pDbPage->pgno; + Pager *pPager = pBt->pPager; + int rc; + + assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || + eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( pDbPage->pBt==pBt ); + + /* Move page iDbPage from its current location to page number iFreePage */ + TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", + iDbPage, iFreePage, iPtrPage, eType)); + rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); + if( rc!=SQLITE_OK ){ + return rc; + } + pDbPage->pgno = iFreePage; + + /* If pDbPage was a btree-page, then it may have child pages and/or cells + ** that point to overflow pages. The pointer map entries for all these + ** pages need to be changed. + ** + ** If pDbPage is an overflow page, then the first 4 bytes may store a + ** pointer to a subsequent overflow page. If this is the case, then + ** the pointer map needs to be updated for the subsequent overflow page. + */ + if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ + rc = setChildPtrmaps(pDbPage); + if( rc!=SQLITE_OK ){ + return rc; + } + }else{ + Pgno nextOvfl = get4byte(pDbPage->aData); + if( nextOvfl!=0 ){ + ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc); + if( rc!=SQLITE_OK ){ + return rc; + } + } + } + + /* Fix the database pointer on page iPtrPage that pointed at iDbPage so + ** that it points at iFreePage. Also fix the pointer map entry for + ** iPtrPage. + */ + if( eType!=PTRMAP_ROOTPAGE ){ + rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + rc = sqlite3PagerWrite(pPtrPage->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(pPtrPage); + return rc; + } + rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); + releasePage(pPtrPage); + if( rc==SQLITE_OK ){ + ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc); + } + } + return rc; +} + +/* Forward declaration required by incrVacuumStep(). */ +static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); + +/* +** Perform a single step of an incremental-vacuum. If successful, +** return SQLITE_OK. If there is no work to do (and therefore no +** point in calling this function again), return SQLITE_DONE. +** +** More specificly, this function attempts to re-organize the +** database so that the last page of the file currently in use +** is no longer in use. +** +** If the nFin parameter is non-zero, this function assumes +** that the caller will keep calling incrVacuumStep() until +** it returns SQLITE_DONE or an error, and that nFin is the +** number of pages the database file will contain after this +** process is complete. If nFin is zero, it is assumed that +** incrVacuumStep() will be called a finite amount of times +** which may or may not empty the freelist. A full autovacuum +** has nFin>0. A "PRAGMA incremental_vacuum" has nFin==0. +*/ +static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg){ + Pgno nFreeList; /* Number of pages still on the free-list */ + int rc; + + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( iLastPg>nFin ); + + if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ + u8 eType; + Pgno iPtrPage; + + nFreeList = get4byte(&pBt->pPage1->aData[36]); + if( nFreeList==0 ){ + return SQLITE_DONE; + } + + rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); + if( rc!=SQLITE_OK ){ + return rc; + } + if (eType==PTRMAP_LAZYFREE) + return SQLITE_DONE; + if( eType==PTRMAP_ROOTPAGE ){ + return SQLITE_CORRUPT_BKPT; + } + + if( eType == PTRMAP_FREELEAF ){ + /* We are just going to truncate the file to remove this free page. + ** We leave this page in the free list to be removed on a subsequent + ** call to allocate. + */ + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + if( rc!=SQLITE_OK ){ + return rc; + } + + put4byte(&pBt->pPage1->aData[36], nFreeList-1); + + // Mark the last page as writable since we are about to truncate it + MemPage *pFreeLeaf; + rc = btreeGetPage(pBt, iLastPg, &pFreeLeaf, 0); + if( rc != SQLITE_OK ){ + return rc; + } + + rc = sqlite3PagerWrite(pFreeLeaf->pDbPage); + releasePage(pFreeLeaf); + if( rc != SQLITE_OK ){ + return rc; + } + } + else if( eType==PTRMAP_FREEPAGE ){ + if( nFin==0 ){ + /* Remove the page from the files free-list. This is not required + ** if nFin is non-zero. In that case, the free-list will be + ** truncated to zero after this function returns, so it doesn't + ** matter if it still contains some garbage entries. + */ + Pgno iFreePg; + MemPage *pFreePg; + rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, 1); + if( rc!=SQLITE_OK ){ + return rc; + } + assert( iFreePg==iLastPg ); + releasePage(pFreePg); + } + } else { + Pgno iFreePg; /* Index of free page to move pLastPg to */ + MemPage *pLastPg; + + rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + /* If nFin is zero, this loop runs exactly once and page pLastPg + ** is swapped with the first free page pulled off the free list. + ** + ** On the other hand, if nFin is greater than zero, then keep + ** looping until a free-page located within the first nFin pages + ** of the file is found. + */ + do { + MemPage *pFreePg; + rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, 0, 0); + if( rc!=SQLITE_OK ){ + releasePage(pLastPg); + return rc; + } + releasePage(pFreePg); + }while( nFin!=0 && iFreePg>nFin ); + assert( iFreePgpDbPage); + if( rc==SQLITE_OK ){ + rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, nFin!=0); + } + releasePage(pLastPg); + if( rc!=SQLITE_OK ){ + return rc; + } + } + } + + if( nFin==0 ){ + iLastPg--; + while( iLastPg==PENDING_BYTE_PAGE(pBt)||PTRMAP_ISPAGE(pBt, iLastPg) ){ + if( PTRMAP_ISPAGE(pBt, iLastPg) ){ + MemPage *pPg; + rc = btreeGetPage(pBt, iLastPg, &pPg, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + rc = sqlite3PagerWrite(pPg->pDbPage); + releasePage(pPg); + if( rc!=SQLITE_OK ){ + return rc; + } + } + iLastPg--; + } + sqlite3PagerTruncateImage(pBt->pPager, iLastPg); + pBt->nPage = iLastPg; + } + return SQLITE_OK; +} + +/* +** A write-transaction must be opened before calling this function. +** It performs a single unit of work towards an incremental vacuum. +** +** If the incremental vacuum is finished after this function has run, +** SQLITE_DONE is returned. If it is not finished, but no error occurred, +** SQLITE_OK is returned. Otherwise an SQLite error code. +*/ +SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){ + int rc; + BtShared *pBt = p->pBt; + + sqlite3BtreeEnter(p); + assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); + if( !pBt->autoVacuum ){ + rc = SQLITE_DONE; + }else{ + invalidateAllOverflowCache(pBt); + rc = incrVacuumStep(pBt, 0, btreePagecount(pBt)); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + put4byte(&pBt->pPage1->aData[28], pBt->nPage); + } + } + sqlite3BtreeLeave(p); + return rc; +} + +/* +** This routine is called prior to sqlite3PagerCommit when a transaction +** is commited for an auto-vacuum database. +** +** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages +** the database file should be truncated to during the commit process. +** i.e. the database has been reorganized so that only the first *pnTrunc +** pages are in use. +*/ +static int autoVacuumCommit(BtShared *pBt){ + int rc = SQLITE_OK; + Pager *pPager = pBt->pPager; + VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) ); + + assert( sqlite3_mutex_held(pBt->mutex) ); + invalidateAllOverflowCache(pBt); + assert(pBt->autoVacuum); + if( !pBt->incrVacuum ){ + Pgno nFin; /* Number of pages in database after autovacuuming */ + Pgno nFree; /* Number of pages on the freelist initially */ + Pgno nPtrmap; /* Number of PtrMap pages to be freed */ + Pgno iFree; /* The next page to be freed */ + int nEntry; /* Number of entries on one ptrmap page */ + Pgno nOrig; /* Database size before freeing */ + + nOrig = btreePagecount(pBt); + if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ + /* It is not possible to create a database for which the final page + ** is either a pointer-map page or the pending-byte page. If one + ** is encountered, this indicates corruption. + */ + return SQLITE_CORRUPT_BKPT; + } + + nFree = get4byte(&pBt->pPage1->aData[36]); + nEntry = pBt->usableSize/5; + nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry; + nFin = nOrig - nFree - nPtrmap; + if( nOrig>PENDING_BYTE_PAGE(pBt) && nFinnOrig ) return SQLITE_CORRUPT_BKPT; + + for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ + rc = incrVacuumStep(pBt, nFin, iFree); + } + if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + put4byte(&pBt->pPage1->aData[32], 0); + put4byte(&pBt->pPage1->aData[36], 0); + put4byte(&pBt->pPage1->aData[28], nFin); + sqlite3PagerTruncateImage(pBt->pPager, nFin); + pBt->nPage = nFin; + } + if( rc!=SQLITE_OK ){ + sqlite3PagerRollback(pPager); + } + } + + assert( nRef==sqlite3PagerRefcount(pPager) ); + return rc; +} + +#else /* ifndef SQLITE_OMIT_AUTOVACUUM */ +# define setChildPtrmaps(x) SQLITE_OK +#endif + +/* +** This routine does the first phase of a two-phase commit. This routine +** causes a rollback journal to be created (if it does not already exist) +** and populated with enough information so that if a power loss occurs +** the database can be restored to its original state by playing back +** the journal. Then the contents of the journal are flushed out to +** the disk. After the journal is safely on oxide, the changes to the +** database are written into the database file and flushed to oxide. +** At the end of this call, the rollback journal still exists on the +** disk and we are still holding all locks, so the transaction has not +** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the +** commit process. +** +** This call is a no-op if no write-transaction is currently active on pBt. +** +** Otherwise, sync the database file for the btree pBt. zMaster points to +** the name of a master journal file that should be written into the +** individual journal file, or is NULL, indicating no master journal file +** (single database transaction). +** +** When this is called, the master journal should already have been +** created, populated with this journal pointer and synced to disk. +** +** Once this is routine has returned, the only thing required to commit +** the write-transaction for this database file is to delete the journal. +*/ +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ + int rc = SQLITE_OK; + if( p->inTrans==TRANS_WRITE ){ + BtShared *pBt = p->pBt; + sqlite3BtreeEnter(p); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->autoVacuum ){ + rc = autoVacuumCommit(pBt); + if( rc!=SQLITE_OK ){ + sqlite3BtreeLeave(p); + return rc; + } + } +#endif + rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0); + sqlite3BtreeLeave(p); + } + return rc; +} + +/* +** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback() +** at the conclusion of a transaction. +*/ +static void btreeEndTransaction(Btree *p){ + BtShared *pBt = p->pBt; + assert( sqlite3BtreeHoldsMutex(p) ); + + btreeClearHasContent(pBt); + if( p->inTrans>TRANS_NONE && p->db->activeVdbeCnt>1 ){ + /* If there are other active statements that belong to this database + ** handle, downgrade to a read-only transaction. The other statements + ** may still be reading from the database. */ + downgradeAllSharedCacheTableLocks(p); + p->inTrans = TRANS_READ; + }else{ + /* If the handle had any kind of transaction open, decrement the + ** transaction count of the shared btree. If the transaction count + ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused() + ** call below will unlock the pager. */ + if( p->inTrans!=TRANS_NONE ){ + clearAllSharedCacheTableLocks(p); + pBt->nTransaction--; + if( 0==pBt->nTransaction ){ + pBt->inTransaction = TRANS_NONE; + } + } + + /* Set the current transaction state to TRANS_NONE and unlock the + ** pager if this call closed the only read or write transaction. */ + p->inTrans = TRANS_NONE; + unlockBtreeIfUnused(pBt); + } + + btreeIntegrity(p); +} + +/* +** Commit the transaction currently in progress. +** +** This routine implements the second phase of a 2-phase commit. The +** sqlite3BtreeCommitPhaseOne() routine does the first phase and should +** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne() +** routine did all the work of writing information out to disk and flushing the +** contents so that they are written onto the disk platter. All this +** routine has to do is delete or truncate or zero the header in the +** the rollback journal (which causes the transaction to commit) and +** drop locks. +** +** This will release the write lock on the database file. If there +** are no active cursors, it also releases the read lock. +*/ +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p){ + + if( p->inTrans==TRANS_NONE ) return SQLITE_OK; + sqlite3BtreeEnter(p); + btreeIntegrity(p); + + /* If the handle has a write-transaction open, commit the shared-btrees + ** transaction and set the shared state to TRANS_READ. + */ + if( p->inTrans==TRANS_WRITE ){ + int rc; + BtShared *pBt = p->pBt; + assert( pBt->inTransaction==TRANS_WRITE ); + assert( pBt->nTransaction>0 ); + rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); + if( rc!=SQLITE_OK ){ + sqlite3BtreeLeave(p); + return rc; + } + pBt->inTransaction = TRANS_READ; + } + + btreeEndTransaction(p); + sqlite3BtreeLeave(p); + return SQLITE_OK; +} + +/* +** Do both phases of a commit. +*/ +SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){ + int rc; + sqlite3BtreeEnter(p); + rc = sqlite3BtreeCommitPhaseOne(p, 0); + if( rc==SQLITE_OK ){ + rc = sqlite3BtreeCommitPhaseTwo(p); + } + sqlite3BtreeLeave(p); + return rc; +} + +#ifndef NDEBUG +/* +** Return the number of write-cursors open on this handle. This is for use +** in assert() expressions, so it is only compiled if NDEBUG is not +** defined. +** +** For the purposes of this routine, a write-cursor is any cursor that +** is capable of writing to the databse. That means the cursor was +** originally opened for writing and the cursor has not be disabled +** by having its state changed to CURSOR_FAULT. +*/ +static int countWriteCursors(BtShared *pBt){ + BtCursor *pCur; + int r = 0; + for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ + if( pCur->wrFlag && pCur->eState!=CURSOR_FAULT ) r++; + } + return r; +} +#endif + +/* +** This routine sets the state to CURSOR_FAULT and the error +** code to errCode for every cursor on BtShared that pBtree +** references. +** +** Every cursor is tripped, including cursors that belong +** to other database connections that happen to be sharing +** the cache with pBtree. +** +** This routine gets called when a rollback occurs. +** All cursors using the same cache must be tripped +** to prevent them from trying to use the btree after +** the rollback. The rollback may have deleted tables +** or moved root pages, so it is not sufficient to +** save the state of the cursor. The cursor must be +** invalidated. +*/ +SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){ + BtCursor *p; + sqlite3BtreeEnter(pBtree); + for(p=pBtree->pBt->pCursor; p; p=p->pNext){ + int i; + sqlite3BtreeClearCursor(p); + p->eState = CURSOR_FAULT; + p->skipNext = errCode; + for(i=0; i<=p->iPage; i++){ + releasePage(p->apPage[i]); + p->apPage[i] = 0; + } + } + sqlite3BtreeLeave(pBtree); +} + +/* +** Rollback the transaction in progress. All cursors will be +** invalided by this operation. Any attempt to use a cursor +** that was open at the beginning of this operation will result +** in an error. +** +** This will release the write lock on the database file. If there +** are no active cursors, it also releases the read lock. +*/ +SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p){ + int rc; + BtShared *pBt = p->pBt; + MemPage *pPage1; + + sqlite3BtreeEnter(p); + rc = saveAllCursors(pBt, 0, 0); +#ifndef SQLITE_OMIT_SHARED_CACHE + if( rc!=SQLITE_OK ){ + /* This is a horrible situation. An IO or malloc() error occurred whilst + ** trying to save cursor positions. If this is an automatic rollback (as + ** the result of a constraint, malloc() failure or IO error) then + ** the cache may be internally inconsistent (not contain valid trees) so + ** we cannot simply return the error to the caller. Instead, abort + ** all queries that may be using any of the cursors that failed to save. + */ + sqlite3BtreeTripAllCursors(p, rc); + } +#endif + btreeIntegrity(p); + + if( p->inTrans==TRANS_WRITE ){ + int rc2; + + assert( TRANS_WRITE==pBt->inTransaction ); + rc2 = sqlite3PagerRollback(pBt->pPager); + if( rc2!=SQLITE_OK ){ + rc = rc2; + } + + /* The rollback may have destroyed the pPage1->aData value. So + ** call btreeGetPage() on page 1 again to make + ** sure pPage1->aData is set correctly. */ + if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ + int nPage = get4byte(28+(u8*)pPage1->aData); + testcase( nPage==0 ); + if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); + testcase( pBt->nPage!=nPage ); + pBt->nPage = nPage; + releasePage(pPage1); + } + assert( countWriteCursors(pBt)==0 ); + pBt->inTransaction = TRANS_READ; + } + + btreeEndTransaction(p); + sqlite3BtreeLeave(p); + return rc; +} + +/* +** Start a statement subtransaction. The subtransaction can can be rolled +** back independently of the main transaction. You must start a transaction +** before starting a subtransaction. The subtransaction is ended automatically +** if the main transaction commits or rolls back. +** +** Statement subtransactions are used around individual SQL statements +** that are contained within a BEGIN...COMMIT block. If a constraint +** error occurs within the statement, the effect of that one statement +** can be rolled back without having to rollback the entire transaction. +** +** A statement sub-transaction is implemented as an anonymous savepoint. The +** value passed as the second parameter is the total number of savepoints, +** including the new anonymous savepoint, open on the B-Tree. i.e. if there +** are no active savepoints and no other statement-transactions open, +** iStatement is 1. This anonymous savepoint can be released or rolled back +** using the sqlite3BtreeSavepoint() function. +*/ +SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){ + int rc; + BtShared *pBt = p->pBt; + sqlite3BtreeEnter(p); + assert( p->inTrans==TRANS_WRITE ); + assert( pBt->readOnly==0 ); + assert( iStatement>0 ); + assert( iStatement>p->db->nSavepoint ); + assert( pBt->inTransaction==TRANS_WRITE ); + /* At the pager level, a statement transaction is a savepoint with + ** an index greater than all savepoints created explicitly using + ** SQL statements. It is illegal to open, release or rollback any + ** such savepoints while the statement transaction savepoint is active. + */ + rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); + sqlite3BtreeLeave(p); + return rc; +} + +/* +** The second argument to this function, op, is always SAVEPOINT_ROLLBACK +** or SAVEPOINT_RELEASE. This function either releases or rolls back the +** savepoint identified by parameter iSavepoint, depending on the value +** of op. +** +** Normally, iSavepoint is greater than or equal to zero. However, if op is +** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the +** contents of the entire transaction are rolled back. This is different +** from a normal transaction rollback, as no locks are released and the +** transaction remains open. +*/ +SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ + int rc = SQLITE_OK; + if( p && p->inTrans==TRANS_WRITE ){ + BtShared *pBt = p->pBt; + assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); + assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); + sqlite3BtreeEnter(p); + rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); + if( rc==SQLITE_OK ){ + if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0; + rc = newDatabase(pBt); + pBt->nPage = get4byte(28 + pBt->pPage1->aData); + + /* The database size was written into the offset 28 of the header + ** when the transaction started, so we know that the value at offset + ** 28 is nonzero. */ + assert( pBt->nPage>0 ); + } + sqlite3BtreeLeave(p); + } + return rc; +} + +/* +** Create a new cursor for the BTree whose root is on the page +** iTable. If a read-only cursor is requested, it is assumed that +** the caller already has at least a read-only transaction open +** on the database already. If a write-cursor is requested, then +** the caller is assumed to have an open write transaction. +** +** If wrFlag==0, then the cursor can only be used for reading. +** If wrFlag==1, then the cursor can be used for reading or for +** writing if other conditions for writing are also met. These +** are the conditions that must be met in order for writing to +** be allowed: +** +** 1: The cursor must have been opened with wrFlag==1 +** +** 2: Other database connections that share the same pager cache +** but which are not in the READ_UNCOMMITTED state may not have +** cursors open with wrFlag==0 on the same table. Otherwise +** the changes made by this write cursor would be visible to +** the read cursors in the other database connection. +** +** 3: The database must be writable (not on read-only media) +** +** 4: There must be an active transaction. +** +** No checking is done to make sure that page iTable really is the +** root page of a b-tree. If it is not, then the cursor acquired +** will not work correctly. +** +** It is assumed that the sqlite3BtreeCursorZero() has been called +** on pCur to initialize the memory space prior to invoking this routine. +*/ +static int btreeCursor( + Btree *p, /* The btree */ + int iTable, /* Root page of table to open */ + int wrFlag, /* 1 to write. 0 read-only */ + struct KeyInfo *pKeyInfo, /* First arg to comparison function */ + BtCursor *pCur /* Space for new cursor */ +){ + BtShared *pBt = p->pBt; /* Shared b-tree handle */ + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( wrFlag==0 || wrFlag==1 ); + + /* The following assert statements verify that if this is a sharable + ** b-tree database, the connection is holding the required table locks, + ** and that no other connection has any open cursor that conflicts with + ** this lock. */ + assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) ); + assert( wrFlag==0 || !hasReadConflicts(p, iTable) ); + + /* Assert that the caller has opened the required transaction. */ + assert( p->inTrans>TRANS_NONE ); + assert( wrFlag==0 || p->inTrans==TRANS_WRITE ); + assert( pBt->pPage1 && pBt->pPage1->aData ); + + if( NEVER(wrFlag && pBt->readOnly) ){ + return SQLITE_READONLY; + } + if( iTable==1 && btreePagecount(pBt)==0 ){ + return SQLITE_EMPTY; + } + + /* Now that no other errors can occur, finish filling in the BtCursor + ** variables and link the cursor into the BtShared list. */ + pCur->pgnoRoot = (Pgno)iTable; + pCur->iPage = -1; + pCur->pKeyInfo = pKeyInfo; + pCur->pBtree = p; + pCur->pBt = pBt; + pCur->wrFlag = (u8)wrFlag; + pCur->pNext = pBt->pCursor; + if( pCur->pNext ){ + pCur->pNext->pPrev = pCur; + } + pBt->pCursor = pCur; + pCur->eState = CURSOR_INVALID; + pCur->cachedRowid = 0; + return SQLITE_OK; +} +SQLITE_PRIVATE int sqlite3BtreeCursor( + Btree *p, /* The btree */ + int iTable, /* Root page of table to open */ + int wrFlag, /* 1 to write. 0 read-only */ + struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ + BtCursor *pCur /* Write new cursor here */ +){ + int rc; + sqlite3BtreeEnter(p); + rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); + sqlite3BtreeLeave(p); + return rc; +} + +/* +** Return the size of a BtCursor object in bytes. +** +** This interfaces is needed so that users of cursors can preallocate +** sufficient storage to hold a cursor. The BtCursor object is opaque +** to users so they cannot do the sizeof() themselves - they must call +** this routine. +*/ +SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){ + return ROUND8(sizeof(BtCursor)); +} + +/* +** Initialize memory that will be converted into a BtCursor object. +** +** The simple approach here would be to memset() the entire object +** to zero. But it turns out that the apPage[] and aiIdx[] arrays +** do not need to be zeroed and they are large, so we can save a lot +** of run-time by skipping the initialization of those elements. +*/ +SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){ + memset(p, 0, offsetof(BtCursor, iPage)); +} + +/* +** Set the cached rowid value of every cursor in the same database file +** as pCur and having the same root page number as pCur. The value is +** set to iRowid. +** +** Only positive rowid values are considered valid for this cache. +** The cache is initialized to zero, indicating an invalid cache. +** A btree will work fine with zero or negative rowids. We just cannot +** cache zero or negative rowids, which means tables that use zero or +** negative rowids might run a little slower. But in practice, zero +** or negative rowids are very uncommon so this should not be a problem. +*/ +SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){ + BtCursor *p; + for(p=pCur->pBt->pCursor; p; p=p->pNext){ + if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid; + } + assert( pCur->cachedRowid==iRowid ); +} + +/* +** Return the cached rowid for the given cursor. A negative or zero +** return value indicates that the rowid cache is invalid and should be +** ignored. If the rowid cache has never before been set, then a +** zero is returned. +*/ +SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){ + return pCur->cachedRowid; +} + +/* +** Close a cursor. The read lock on the database file is released +** when the last cursor is closed. +*/ +SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){ + Btree *pBtree = pCur->pBtree; + if( pBtree ){ + int i; + BtShared *pBt = pCur->pBt; + sqlite3BtreeEnter(pBtree); + sqlite3BtreeClearCursor(pCur); + if( pCur->pPrev ){ + pCur->pPrev->pNext = pCur->pNext; + }else{ + pBt->pCursor = pCur->pNext; + } + if( pCur->pNext ){ + pCur->pNext->pPrev = pCur->pPrev; + } + for(i=0; i<=pCur->iPage; i++){ + releasePage(pCur->apPage[i]); + } + unlockBtreeIfUnused(pBt); + invalidateOverflowCache(pCur); + /* sqlite3_free(pCur); */ + sqlite3BtreeLeave(pBtree); + } + return SQLITE_OK; +} + +/* +** Make sure the BtCursor* given in the argument has a valid +** BtCursor.info structure. If it is not already valid, call +** btreeParseCell() to fill it in. +** +** BtCursor.info is a cache of the information in the current cell. +** Using this cache reduces the number of calls to btreeParseCell(). +** +** 2007-06-25: There is a bug in some versions of MSVC that cause the +** compiler to crash when getCellInfo() is implemented as a macro. +** But there is a measureable speed advantage to using the macro on gcc +** (when less compiler optimizations like -Os or -O0 are used and the +** compiler is not doing agressive inlining.) So we use a real function +** for MSVC and a macro for everything else. Ticket #2457. +*/ +#ifndef NDEBUG + static void assertCellInfo(BtCursor *pCur){ + CellInfo info; + int iPage = pCur->iPage; + memset(&info, 0, sizeof(info)); + btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info); + assert( memcmp(&info, &pCur->info, sizeof(info))==0 ); + } +#else + #define assertCellInfo(x) +#endif +#ifdef _MSC_VER + /* Use a real function in MSVC to work around bugs in that compiler. */ + static void getCellInfo(BtCursor *pCur){ + if( pCur->info.nSize==0 ){ + int iPage = pCur->iPage; + btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); + pCur->validNKey = 1; + }else{ + assertCellInfo(pCur); + } + } +#else /* if not _MSC_VER */ + /* Use a macro in all other compilers so that the function is inlined */ +#define getCellInfo(pCur) \ + if( pCur->info.nSize==0 ){ \ + int iPage = pCur->iPage; \ + btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \ + pCur->validNKey = 1; \ + }else{ \ + assertCellInfo(pCur); \ + } +#endif /* _MSC_VER */ + +#ifndef NDEBUG /* The next routine used only within assert() statements */ +/* +** Return true if the given BtCursor is valid. A valid cursor is one +** that is currently pointing to a row in a (non-empty) table. +** This is a verification routine is used only within assert() statements. +*/ +SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){ + return pCur && pCur->eState==CURSOR_VALID; +} +#endif /* NDEBUG */ + +/* +** Set *pSize to the size of the buffer needed to hold the value of +** the key for the current entry. If the cursor is not pointing +** to a valid entry, *pSize is set to 0. +** +** For a table with the INTKEY flag set, this routine returns the key +** itself, not the number of bytes in the key. +** +** The caller must position the cursor prior to invoking this routine. +** +** This routine cannot fail. It always returns SQLITE_OK. +*/ +SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID ); + if( pCur->eState!=CURSOR_VALID ){ + *pSize = 0; + }else{ + getCellInfo(pCur); + *pSize = pCur->info.nKey; + } + return SQLITE_OK; +} + +/* +** Set *pSize to the number of bytes of data in the entry the +** cursor currently points to. +** +** The caller must guarantee that the cursor is pointing to a non-NULL +** valid entry. In other words, the calling procedure must guarantee +** that the cursor has Cursor.eState==CURSOR_VALID. +** +** Failure is not possible. This function always returns SQLITE_OK. +** It might just as well be a procedure (returning void) but we continue +** to return an integer result code for historical reasons. +*/ +SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + getCellInfo(pCur); + *pSize = pCur->info.nData; + return SQLITE_OK; +} + +/* +** Given the page number of an overflow page in the database (parameter +** ovfl), this function finds the page number of the next page in the +** linked list of overflow pages. If possible, it uses the auto-vacuum +** pointer-map data instead of reading the content of page ovfl to do so. +** +** If an error occurs an SQLite error code is returned. Otherwise: +** +** The page number of the next overflow page in the linked list is +** written to *pPgnoNext. If page ovfl is the last page in its linked +** list, *pPgnoNext is set to zero. +** +** If ppPage is not NULL, and a reference to the MemPage object corresponding +** to page number pOvfl was obtained, then *ppPage is set to point to that +** reference. It is the responsibility of the caller to call releasePage() +** on *ppPage to free the reference. In no reference was obtained (because +** the pointer-map was used to obtain the value for *pPgnoNext), then +** *ppPage is set to zero. +*/ +static int getOverflowPage( + BtShared *pBt, /* The database file */ + Pgno ovfl, /* Current overflow page number */ + MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */ + Pgno *pPgnoNext /* OUT: Next overflow page number */ +){ + Pgno next = 0; + MemPage *pPage = 0; + int rc = SQLITE_OK; + + assert( sqlite3_mutex_held(pBt->mutex) ); + assert(pPgnoNext); + +#if 0 // This shortcut is not allowed since we want to validate child->parent links when traversing +//#ifndef SQLITE_OMIT_AUTOVACUUM + /* Try to find the next page in the overflow list using the + ** autovacuum pointer-map pages. Guess that the next page in + ** the overflow list is page number (ovfl+1). If that guess turns + ** out to be wrong, fall back to loading the data of page + ** number ovfl to determine the next page number. + */ + if( pBt->autoVacuum ){ + Pgno pgno; + Pgno iGuess = ovfl+1; + u8 eType; + + while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ + iGuess++; + } + + if( iGuess<=btreePagecount(pBt) ){ + rc = ptrmapGet(pBt, iGuess, &eType, &pgno); + if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ + next = iGuess; + rc = SQLITE_DONE; + } + } + } +#endif + + assert( next==0 || rc==SQLITE_DONE ); + if( rc==SQLITE_OK ){ + rc = btreeGetPage(pBt, ovfl, &pPage, 0); + assert( rc==SQLITE_OK || pPage==0 ); + if( rc==SQLITE_OK ){ + next = get4byte(pPage->aData); + } + } + + *pPgnoNext = next; + if( ppPage ){ + *ppPage = pPage; + }else{ + releasePage(pPage); + } + return (rc==SQLITE_DONE ? SQLITE_OK : rc); +} + +/* +** Copy data from a buffer to a page, or from a page to a buffer. +** +** pPayload is a pointer to data stored on database page pDbPage. +** If argument eOp is false, then nByte bytes of data are copied +** from pPayload to the buffer pointed at by pBuf. If eOp is true, +** then sqlite3PagerWrite() is called on pDbPage and nByte bytes +** of data are copied from the buffer pBuf to pPayload. +** +** SQLITE_OK is returned on success, otherwise an error code. +*/ +static int copyPayload( + void *pPayload, /* Pointer to page data */ + void *pBuf, /* Pointer to buffer */ + int nByte, /* Number of bytes to copy */ + int eOp, /* 0 -> copy from page, 1 -> copy to page */ + DbPage *pDbPage /* Page containing pPayload */ +){ + if( eOp ){ + /* Copy data from buffer to page (a write operation) */ + int rc = sqlite3PagerWrite(pDbPage); + if( rc!=SQLITE_OK ){ + return rc; + } + memcpy(pPayload, pBuf, nByte); + }else{ + /* Copy data from page to buffer (a read operation) */ + memcpy(pBuf, pPayload, nByte); + } + return SQLITE_OK; +} + +/* +** This function is used to read or overwrite payload information +** for the entry that the pCur cursor is pointing to. If the eOp +** parameter is 0, this is a read operation (data copied into +** buffer pBuf). If it is non-zero, a write (data copied from +** buffer pBuf). +** +** A total of "amt" bytes are read or written beginning at "offset". +** Data is read to or from the buffer pBuf. +** +** The content being read or written might appear on the main page +** or be scattered out on multiple overflow pages. +** +** If the BtCursor.isIncrblobHandle flag is set, and the current +** cursor entry uses one or more overflow pages, this function +** allocates space for and lazily popluates the overflow page-list +** cache array (BtCursor.aOverflow). Subsequent calls use this +** cache to make seeking to the supplied offset more efficient. +** +** Once an overflow page-list cache has been allocated, it may be +** invalidated if some other cursor writes to the same table, or if +** the cursor is moved to a different row. Additionally, in auto-vacuum +** mode, the following events may invalidate an overflow page-list cache. +** +** * An incremental vacuum, +** * A commit in auto_vacuum="full" mode, +** * Creating a table (may require moving an overflow page). +*/ +static int accessPayload( + BtCursor *pCur, /* Cursor pointing to entry to read from */ + u32 offset, /* Begin reading this far into payload */ + u32 amt, /* Read this many bytes */ + unsigned char *pBuf, /* Write the bytes into this buffer */ + int eOp /* zero to read. non-zero to write. */ +){ + unsigned char *aPayload; + int rc = SQLITE_OK; + u32 nKey; + int iIdx = 0; + MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */ + BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ + + assert( pPage ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->aiIdx[pCur->iPage]nCell ); + assert( cursorHoldsMutex(pCur) ); + + getCellInfo(pCur); + aPayload = pCur->info.pCell + pCur->info.nHeader; + nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey); + + if( NEVER(offset+amt > nKey+pCur->info.nData) + || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] + ){ + /* Trying to read or write past the end of the data is an error */ + return SQLITE_CORRUPT_BKPT; + } + + /* Check if data must be read/written to/from the btree page itself. */ + if( offsetinfo.nLocal ){ + int a = amt; + if( a+offset>pCur->info.nLocal ){ + a = pCur->info.nLocal - offset; + } + rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); + offset = 0; + pBuf += a; + amt -= a; + }else{ + offset -= pCur->info.nLocal; + } + + if( rc==SQLITE_OK && amt>0 ){ + const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ + Pgno nextPage; + + nextPage = get4byte(&aPayload[pCur->info.nLocal]); + Pgno nextPageParent = pPage->pgno; // Expected parent of nextPage, to be verified before using the page + +#ifndef SQLITE_OMIT_INCRBLOB + /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[] + ** has not been allocated, allocate it now. The array is sized at + ** one entry for each overflow page in the overflow chain. The + ** page number of the first overflow page is stored in aOverflow[0], + ** etc. A value of 0 in the aOverflow[] array means "not yet known" + ** (the cache is lazily populated). + */ + if( pCur->isIncrblobHandle && !pCur->aOverflow ){ + int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; + pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl); + /* nOvfl is always positive. If it were zero, fetchPayload would have + ** been used instead of this routine. */ + if( ALWAYS(nOvfl) && !pCur->aOverflow ){ + rc = SQLITE_NOMEM; + } + } + + /* If the overflow page-list cache has been allocated and the + ** entry for the first required overflow page is valid, skip + ** directly to it. + */ + if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){ + iIdx = (offset/ovflSize); + nextPage = pCur->aOverflow[iIdx]; + nextPageParent = 0; // No need to verify link since page was from cache was from cache + offset = (offset%ovflSize); + } +#endif + + for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){ + + // Verify the child to parent link of the next page to read, if necessary + if(nextPageParent != 0) { + rc = verifyParentChildLink(pBt, nextPageParent, nextPage); + if(rc != SQLITE_OK) + return rc; + } + +#ifndef SQLITE_OMIT_INCRBLOB + /* If required, populate the overflow page-list cache. */ + if( pCur->aOverflow ){ + assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage); + pCur->aOverflow[iIdx] = nextPage; + } +#endif + + if( offset>=ovflSize ){ + /* The only reason to read this page is to obtain the page + ** number for the next page in the overflow chain. The page + ** data is not required. So first try to lookup the overflow + ** page-list cache, if any, then fall back to the getOverflowPage() + ** function. + */ +#ifndef SQLITE_OMIT_INCRBLOB + if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){ + nextPage = pCur->aOverflow[iIdx+1]; + nextPageParent = 0; // No need to verify link since page was from cache + } else +#endif + { + nextPageParent = nextPage; + rc = getOverflowPage(pBt, nextPage, 0, &nextPage); + } + offset -= ovflSize; + }else{ + /* Need to read this page properly. It contains some of the + ** range of data that is being read (eOp==0) or written (eOp!=0). + */ + DbPage *pDbPage; + int a = amt; + rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage); + if( rc==SQLITE_OK ){ + aPayload = sqlite3PagerGetData(pDbPage); + nextPageParent = nextPage; + nextPage = get4byte(aPayload); + if( a + offset > ovflSize ){ + a = ovflSize - offset; + } + rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); + sqlite3PagerUnref(pDbPage); + offset = 0; + amt -= a; + pBuf += a; + } + } + } + } + + if( rc==SQLITE_OK && amt>0 ){ + return SQLITE_CORRUPT_BKPT; + } + return rc; +} + +/* +** Read part of the key associated with cursor pCur. Exactly +** "amt" bytes will be transfered into pBuf[]. The transfer +** begins at "offset". +** +** The caller must ensure that pCur is pointing to a valid row +** in the table. +** +** Return SQLITE_OK on success or an error code if anything goes +** wrong. An error is returned if "offset+amt" is larger than +** the available payload. +*/ +SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); + assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); + return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); +} + +/* +** Read part of the data associated with cursor pCur. Exactly +** "amt" bytes will be transfered into pBuf[]. The transfer +** begins at "offset". +** +** Return SQLITE_OK on success or an error code if anything goes +** wrong. An error is returned if "offset+amt" is larger than +** the available payload. +*/ +SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ + int rc; + +#ifndef SQLITE_OMIT_INCRBLOB + if ( pCur->eState==CURSOR_INVALID ){ + return SQLITE_ABORT; + } +#endif + + assert( cursorHoldsMutex(pCur) ); + rc = restoreCursorPosition(pCur); + if( rc==SQLITE_OK ){ + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] ); + assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); + rc = accessPayload(pCur, offset, amt, pBuf, 0); + } + return rc; +} + +/* +** Return a pointer to payload information from the entry that the +** pCur cursor is pointing to. The pointer is to the beginning of +** the key if skipKey==0 and it points to the beginning of data if +** skipKey==1. The number of bytes of available key/data is written +** into *pAmt. If *pAmt==0, then the value returned will not be +** a valid pointer. +** +** This routine is an optimization. It is common for the entire key +** and data to fit on the local page and for there to be no overflow +** pages. When that is so, this routine can be used to access the +** key and data without making a copy. If the key and/or data spills +** onto overflow pages, then accessPayload() must be used to reassemble +** the key/data and copy it into a preallocated buffer. +** +** The pointer returned by this routine looks directly into the cached +** page of the database. The data might change or move the next time +** any btree routine is called. +*/ +static const unsigned char *fetchPayload( + BtCursor *pCur, /* Cursor pointing to entry to read from */ + int *pAmt, /* Write the number of available bytes here */ + int skipKey /* read beginning at data if this is true */ +){ + unsigned char *aPayload; + MemPage *pPage; + u32 nKey; + u32 nLocal; + + assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]); + assert( pCur->eState==CURSOR_VALID ); + assert( cursorHoldsMutex(pCur) ); + pPage = pCur->apPage[pCur->iPage]; + assert( pCur->aiIdx[pCur->iPage]nCell ); + if( NEVER(pCur->info.nSize==0) ){ + btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage], + &pCur->info); + } + aPayload = pCur->info.pCell; + aPayload += pCur->info.nHeader; + if( pPage->intKey ){ + nKey = 0; + }else{ + nKey = (int)pCur->info.nKey; + } + if( skipKey ){ + aPayload += nKey; + nLocal = pCur->info.nLocal - nKey; + }else{ + nLocal = pCur->info.nLocal; + assert( nLocal<=nKey ); + } + *pAmt = nLocal; + return aPayload; +} + + +/* +** For the entry that cursor pCur is point to, return as +** many bytes of the key or data as are available on the local +** b-tree page. Write the number of available bytes into *pAmt. +** +** The pointer returned is ephemeral. The key/data may move +** or be destroyed on the next call to any Btree routine, +** including calls from other threads against the same cache. +** Hence, a mutex on the BtShared should be held prior to calling +** this routine. +** +** These routines is used to get quick access to key and data +** in the common case where no overflow pages are used. +*/ +SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, int *pAmt){ + const void *p = 0; + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + assert( cursorHoldsMutex(pCur) ); + if( ALWAYS(pCur->eState==CURSOR_VALID) ){ + p = (const void*)fetchPayload(pCur, pAmt, 0); + } + return p; +} +SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, int *pAmt){ + const void *p = 0; + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + assert( cursorHoldsMutex(pCur) ); + if( ALWAYS(pCur->eState==CURSOR_VALID) ){ + p = (const void*)fetchPayload(pCur, pAmt, 1); + } + return p; +} + + +/* +** Move the cursor down to a new child page. The newPgno argument is the +** page number of the child page to move to. +** +** This function returns SQLITE_CORRUPT if the page-header flags field of +** the new child page does not match the flags field of the parent (i.e. +** if an intkey page appears to be the parent of a non-intkey page, or +** vice-versa). +*/ +static int moveToChild(BtCursor *pCur, u32 newPgno){ + int rc; + int i = pCur->iPage; + MemPage *pNewPage; + BtShared *pBt = pCur->pBt; + + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPageiPage>=(BTCURSOR_MAX_DEPTH-1) ){ + return SQLITE_CORRUPT_BKPT; + } + + rc = verifyParentChildLink(pBt, pCur->apPage[i]->pgno, newPgno); + if(rc != SQLITE_OK) + return rc; + + rc = getAndInitPage(pBt, newPgno, &pNewPage); + if( rc ) return rc; + pCur->apPage[i+1] = pNewPage; + pCur->aiIdx[i+1] = 0; + pCur->iPage++; + + pCur->info.nSize = 0; + pCur->validNKey = 0; + if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){ + return SQLITE_CORRUPT_BKPT; + } + return SQLITE_OK; +} + +#ifndef NDEBUG +/* +** Page pParent is an internal (non-leaf) tree page. This function +** asserts that page number iChild is the left-child if the iIdx'th +** cell in page pParent. Or, if iIdx is equal to the total number of +** cells in pParent, that page number iChild is the right-child of +** the page. +*/ +static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ + assert( iIdx<=pParent->nCell ); + if( iIdx==pParent->nCell ){ + assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); + }else{ + assert( get4byte(findCell(pParent, iIdx))==iChild ); + } +} +#else +# define assertParentIndex(x,y,z) +#endif + +/* +** Move the cursor up to the parent page. +** +** pCur->idx is set to the cell index that contains the pointer +** to the page we are coming from. If we are coming from the +** right-most child page then pCur->idx is set to one more than +** the largest cell index. +*/ +static void moveToParent(BtCursor *pCur){ + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + assert( pCur->iPage>0 ); + assert( pCur->apPage[pCur->iPage] ); + assertParentIndex( + pCur->apPage[pCur->iPage-1], + pCur->aiIdx[pCur->iPage-1], + pCur->apPage[pCur->iPage]->pgno + ); + releasePage(pCur->apPage[pCur->iPage]); + pCur->iPage--; + pCur->info.nSize = 0; + pCur->validNKey = 0; +} + +/* +** Move the cursor to point to the root page of its b-tree structure. +** +** If the table has a virtual root page, then the cursor is moved to point +** to the virtual root page instead of the actual root page. A table has a +** virtual root page when the actual root page contains no cells and a +** single child page. This can only happen with the table rooted at page 1. +** +** If the b-tree structure is empty, the cursor state is set to +** CURSOR_INVALID. Otherwise, the cursor is set to point to the first +** cell located on the root (or virtual root) page and the cursor state +** is set to CURSOR_VALID. +** +** If this function returns successfully, it may be assumed that the +** page-header flags indicate that the [virtual] root-page is the expected +** kind of b-tree page (i.e. if when opening the cursor the caller did not +** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D, +** indicating a table b-tree, or if the caller did specify a KeyInfo +** structure the flags byte is set to 0x02 or 0x0A, indicating an index +** b-tree). +*/ +static int moveToRoot(BtCursor *pCur){ + MemPage *pRoot; + int rc = SQLITE_OK; + Btree *p = pCur->pBtree; + BtShared *pBt = p->pBt; + + assert( cursorHoldsMutex(pCur) ); + assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); + assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); + assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); + if( pCur->eState>=CURSOR_REQUIRESEEK ){ + if( pCur->eState==CURSOR_FAULT ){ + assert( pCur->skipNext!=SQLITE_OK ); + return pCur->skipNext; + } + sqlite3BtreeClearCursor(pCur); + } + + if( pCur->iPage>=0 ){ + int i; + for(i=1; i<=pCur->iPage; i++){ + releasePage(pCur->apPage[i]); + } + pCur->iPage = 0; + }else{ + rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]); + if( rc!=SQLITE_OK ){ + pCur->eState = CURSOR_INVALID; + return rc; + } + pCur->iPage = 0; + + /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor + ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is + ** NULL, the caller expects a table b-tree. If this is not the case, + ** return an SQLITE_CORRUPT error. */ + assert( pCur->apPage[0]->intKey==1 || pCur->apPage[0]->intKey==0 ); + if( (pCur->pKeyInfo==0)!=pCur->apPage[0]->intKey ){ + return SQLITE_CORRUPT_BKPT; + } + } + + /* Assert that the root page is of the correct type. This must be the + ** case as the call to this function that loaded the root-page (either + ** this call or a previous invocation) would have detected corruption + ** if the assumption were not true, and it is not possible for the flags + ** byte to have been modified while this cursor is holding a reference + ** to the page. */ + pRoot = pCur->apPage[0]; + assert( pRoot->pgno==pCur->pgnoRoot ); + assert( pRoot->isInit && (pCur->pKeyInfo==0)==pRoot->intKey ); + + pCur->aiIdx[0] = 0; + pCur->info.nSize = 0; + pCur->atLast = 0; + pCur->validNKey = 0; + + if( pRoot->nCell==0 && !pRoot->leaf ){ + Pgno subpage; + if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT; + subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); + pCur->eState = CURSOR_VALID; + rc = moveToChild(pCur, subpage); + }else{ + pCur->eState = ((pRoot->nCell>0)?CURSOR_VALID:CURSOR_INVALID); + } + return rc; +} + +/* +** Move the cursor down to the left-most leaf entry beneath the +** entry to which it is currently pointing. +** +** The left-most leaf is the one with the smallest key - the first +** in ascending order. +*/ +static int moveToLeftmost(BtCursor *pCur){ + Pgno pgno; + int rc = SQLITE_OK; + MemPage *pPage; + + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ + assert( pCur->aiIdx[pCur->iPage]nCell ); + pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage])); + rc = moveToChild(pCur, pgno); + } + return rc; +} + +/* +** Move the cursor down to the right-most leaf entry beneath the +** page to which it is currently pointing. Notice the difference +** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() +** finds the left-most entry beneath the *entry* whereas moveToRightmost() +** finds the right-most entry beneath the *page*. +** +** The right-most entry is the one with the largest key - the last +** key in ascending order. +*/ +static int moveToRightmost(BtCursor *pCur){ + Pgno pgno; + int rc = SQLITE_OK; + MemPage *pPage = 0; + + assert( cursorHoldsMutex(pCur) ); + assert( pCur->eState==CURSOR_VALID ); + while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){ + pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); + pCur->aiIdx[pCur->iPage] = pPage->nCell; + rc = moveToChild(pCur, pgno); + } + if( rc==SQLITE_OK ){ + pCur->aiIdx[pCur->iPage] = pPage->nCell-1; + pCur->info.nSize = 0; + pCur->validNKey = 0; + } + return rc; +} + +/* Move the cursor to the first entry in the table. Return SQLITE_OK +** on success. Set *pRes to 0 if the cursor actually points to something +** or set *pRes to 1 if the table is empty. +*/ +SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ + int rc; + + assert( cursorHoldsMutex(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + rc = moveToRoot(pCur); + if( rc==SQLITE_OK ){ + if( pCur->eState==CURSOR_INVALID ){ + assert( pCur->apPage[pCur->iPage]->nCell==0 ); + *pRes = 1; + }else{ + assert( pCur->apPage[pCur->iPage]->nCell>0 ); + *pRes = 0; + rc = moveToLeftmost(pCur); + } + } + return rc; +} + +/* Move the cursor to the last entry in the table. Return SQLITE_OK +** on success. Set *pRes to 0 if the cursor actually points to something +** or set *pRes to 1 if the table is empty. +*/ +SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ + int rc; + + assert( cursorHoldsMutex(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + + /* If the cursor already points to the last entry, this is a no-op. */ + if( CURSOR_VALID==pCur->eState && pCur->atLast ){ +#ifdef SQLITE_DEBUG + /* This block serves to assert() that the cursor really does point + ** to the last entry in the b-tree. */ + int ii; + for(ii=0; iiiPage; ii++){ + assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell ); + } + assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 ); + assert( pCur->apPage[pCur->iPage]->leaf ); +#endif + return SQLITE_OK; + } + + rc = moveToRoot(pCur); + if( rc==SQLITE_OK ){ + if( CURSOR_INVALID==pCur->eState ){ + assert( pCur->apPage[pCur->iPage]->nCell==0 ); + *pRes = 1; + }else{ + assert( pCur->eState==CURSOR_VALID ); + *pRes = 0; + rc = moveToRightmost(pCur); + pCur->atLast = rc==SQLITE_OK ?1:0; + } + } + return rc; +} + +#include + +void hexdump(FILE *fout, int buflen, void *ptr) { + unsigned char *buf = (unsigned char*)ptr; + int i, j; + for (i=0; i0 The cursor is left pointing at an entry that +** is larger than intKey/pIdxKey. +** +*/ +SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + BtCursor *pCur, /* The cursor to be moved */ + UnpackedRecord *pIdxKey, /* Unpacked index key */ + i64 intKey, /* The table key */ + int biasRight, /* If true, bias the search to the high end */ + int *pRes /* Write search results here */ +){ + int rc; + + assert( cursorHoldsMutex(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + assert( pRes ); + assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); + + /* If the cursor is already positioned at the point we are trying + ** to move to, then just return without doing any work */ + if( pCur->eState==CURSOR_VALID && pCur->validNKey + && pCur->apPage[0]->intKey + ){ + if( pCur->info.nKey==intKey ){ + *pRes = 0; + return SQLITE_OK; + } + if( pCur->atLast && pCur->info.nKeyapPage[pCur->iPage] ); + assert( pCur->apPage[pCur->iPage]->isInit ); + assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID ); + if( pCur->eState==CURSOR_INVALID ){ + *pRes = -1; + assert( pCur->apPage[pCur->iPage]->nCell==0 ); + return SQLITE_OK; + } + assert( pCur->apPage[0]->intKey || pIdxKey ); + for(;;){ + int lwr, upr; + Pgno chldPg; + MemPage *pPage = pCur->apPage[pCur->iPage]; + int c; + + /* pPage->nCell must be greater than zero. If this is the root-page + ** the cursor would have been INVALID above and this for(;;) loop + ** not run. If this is not the root-page, then the moveToChild() routine + ** would have already detected db corruption. Similarly, pPage must + ** be the right kind (index or table) of b-tree page. Otherwise + ** a moveToChild() or moveToRoot() call would have detected corruption. */ + assert( pPage->nCell>0 ); + assert( pPage->intKey==(pIdxKey==0) ); + lwr = 0; + upr = pPage->nCell-1; + if( biasRight ){ + pCur->aiIdx[pCur->iPage] = (u16)upr; + }else{ + pCur->aiIdx[pCur->iPage] = (u16)((upr+lwr)/2); + } + for(;;){ + int idx = pCur->aiIdx[pCur->iPage]; /* Index of current cell in pPage */ + u8 *pCell; /* Pointer to current cell in pPage */ + + pCur->info.nSize = 0; + pCell = findCell(pPage, idx) + pPage->childPtrSize; + if( pPage->intKey ){ + i64 nCellKey; + if( pPage->hasData ){ + u32 dummy; + pCell += getVarint32(pCell, dummy); + } + getVarint(pCell, (u64*)&nCellKey); + if( nCellKey==intKey ){ + c = 0; + }else if( nCellKeyintKey ); + c = +1; + } + pCur->validNKey = 1; + pCur->info.nKey = nCellKey; + }else{ + /* The maximum supported page-size is 65536 bytes. This means that + ** the maximum number of record bytes stored on an index B-Tree + ** page is less than 16384 bytes and may be stored as a 2-byte + ** varint. This information is used to attempt to avoid parsing + ** the entire cell by checking for the cases where the record is + ** stored entirely within the b-tree page by inspecting the first + ** 2 bytes of the cell. + */ + int nCell = pCell[0]; + if( !(nCell & 0x80) && nCell<=pPage->maxLocal ){ + /* This branch runs if the record-size field of the cell is a + ** single byte varint and the record fits entirely on the main + ** b-tree page. */ + c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey, 0, NULL); + }else if( !(pCell[1] & 0x80) + && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal + ){ + /* The record-size field is a 2 byte varint and the record + ** fits entirely on the main b-tree page. */ + c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey, 0, NULL); + }else{ + /* The record flows over onto one or more overflow pages. In + ** this case the whole cell needs to be parsed, a buffer allocated + ** and accessPayload() used to retrieve the record into the + ** buffer before VdbeRecordCompare() can be called. */ + + /* pCellBody is pCell adjustd back to the start of the full cell */ + u8 * const pCellBody = pCell - pPage->childPtrSize; + btreeParseCellPtr(pPage, pCellBody, &pCur->info); + + /* OPTIMIZATION: + * Perhaps the comparison result can be determined from the partial record in pPage. + * Try the comparison with the partial record first. */ + int nextStartField; + c = sqlite3VdbeRecordCompare(pCur->info.nLocal, pCur->info.pCell + pCur->info.nHeader, pIdxKey, 0, &nextStartField); + + /* If c is 0 and startField is valid then we must load more record data (we'll load all of it) */ + int moreDataRequired = (c == 0 && nextStartField >= 0); + + /* Change this to nonzero to force full comparisons and verify partial comparison result */ + #define SQLITE3_BTREE_FORCE_FULL_COMPARISONS 0 + + if(moreDataRequired || SQLITE3_BTREE_FORCE_FULL_COMPARISONS) { + /* Load the entire cell payload */ + nCell = (int)pCur->info.nKey; + void *pCellKey = sqlite3Malloc( nCell ); + if( pCellKey==0 ){ + rc = SQLITE_NOMEM; + goto moveto_finish; + } + rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0); + if( rc ){ + sqlite3_free(pCellKey); + goto moveto_finish; + } + + int partial_c = c; + c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey, (SQLITE3_BTREE_FORCE_FULL_COMPARISONS ? 0 : nextStartField), NULL); + + #if SQLITE3_BTREE_FORCE_FULL_COMPARISONS + /* If more data was NOT required but the partial comparison produced a different result than full + * then something is wrong, log stuff and abort */ + if(!moreDataRequired && partial_c != c) { + fprintf(stderr, "MISMATCH c=%d partial=%d\n", c, partial_c); + fprintf(stderr, "SHORT BUFFER size=%d\n", pCur->info.nLocal); + hexdump(stderr, pCur->info.nLocal, pCur->info.pCell + pCur->info.nHeader); + fprintf(stderr, "FULL BUFFER size=%d\n", nCell); + hexdump(stderr, nCell, pCellKey); + assert(0); + } + #endif + + sqlite3_free(pCellKey); + } else { + //printf("+"); + } + } + } + if( c==0 ){ + if( pPage->intKey && !pPage->leaf ){ + lwr = idx; + upr = lwr - 1; + break; + }else{ + *pRes = 0; + rc = SQLITE_OK; + goto moveto_finish; + } + } + if( c<0 ){ + lwr = idx+1; + }else{ + upr = idx-1; + } + if( lwr>upr ){ + break; + } + pCur->aiIdx[pCur->iPage] = (u16)((lwr+upr)/2); + } + assert( lwr==upr+1 ); + assert( pPage->isInit ); + if( pPage->leaf ){ + chldPg = 0; + }else if( lwr>=pPage->nCell ){ + chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); + }else{ + chldPg = get4byte(findCell(pPage, lwr)); + } + if( chldPg==0 ){ + assert( pCur->aiIdx[pCur->iPage]apPage[pCur->iPage]->nCell ); + *pRes = c; + rc = SQLITE_OK; + goto moveto_finish; + } + pCur->aiIdx[pCur->iPage] = (u16)lwr; + pCur->info.nSize = 0; + pCur->validNKey = 0; + rc = moveToChild(pCur, chldPg); + if( rc ) goto moveto_finish; + } +moveto_finish: + return rc; +} + + +/* +** Return TRUE if the cursor is not pointing at an entry of the table. +** +** TRUE will be returned after a call to sqlite3BtreeNext() moves +** past the last entry in the table or sqlite3BtreePrev() moves past +** the first entry. TRUE is also returned if the table is empty. +*/ +SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){ + /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries + ** have been deleted? This API will need to change to return an error code + ** as well as the boolean result value. + */ + return (CURSOR_VALID!=pCur->eState); +} + +/* +** Advance the cursor to the next entry in the database. If +** successful then set *pRes=0. If the cursor +** was already pointing to the last entry in the database before +** this routine was called, then set *pRes=1. +*/ +SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){ + int rc; + int idx; + MemPage *pPage; + + assert( cursorHoldsMutex(pCur) ); + rc = restoreCursorPosition(pCur); + if( rc!=SQLITE_OK ){ + return rc; + } + assert( pRes!=0 ); + if( CURSOR_INVALID==pCur->eState ){ + *pRes = 1; + return SQLITE_OK; + } + if( pCur->skipNext>0 ){ + pCur->skipNext = 0; + *pRes = 0; + return SQLITE_OK; + } + pCur->skipNext = 0; + + pPage = pCur->apPage[pCur->iPage]; + idx = ++pCur->aiIdx[pCur->iPage]; + assert( pPage->isInit ); + assert( idx<=pPage->nCell ); + + pCur->info.nSize = 0; + pCur->validNKey = 0; + if( idx>=pPage->nCell ){ + if( !pPage->leaf ){ + rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); + if( rc ) return rc; + rc = moveToLeftmost(pCur); + *pRes = 0; + return rc; + } + do{ + if( pCur->iPage==0 ){ + *pRes = 1; + pCur->eState = CURSOR_INVALID; + return SQLITE_OK; + } + moveToParent(pCur); + pPage = pCur->apPage[pCur->iPage]; + }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell ); + *pRes = 0; + if( pPage->intKey ){ + rc = sqlite3BtreeNext(pCur, pRes); + }else{ + rc = SQLITE_OK; + } + return rc; + } + *pRes = 0; + if( pPage->leaf ){ + return SQLITE_OK; + } + rc = moveToLeftmost(pCur); + return rc; +} + + +/* +** Step the cursor to the back to the previous entry in the database. If +** successful then set *pRes=0. If the cursor +** was already pointing to the first entry in the database before +** this routine was called, then set *pRes=1. +*/ +SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){ + int rc; + MemPage *pPage; + + assert( cursorHoldsMutex(pCur) ); + rc = restoreCursorPosition(pCur); + if( rc!=SQLITE_OK ){ + return rc; + } + pCur->atLast = 0; + if( CURSOR_INVALID==pCur->eState ){ + *pRes = 1; + return SQLITE_OK; + } + if( pCur->skipNext<0 ){ + pCur->skipNext = 0; + *pRes = 0; + return SQLITE_OK; + } + pCur->skipNext = 0; + + pPage = pCur->apPage[pCur->iPage]; + assert( pPage->isInit ); + if( !pPage->leaf ){ + int idx = pCur->aiIdx[pCur->iPage]; + rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); + if( rc ){ + return rc; + } + rc = moveToRightmost(pCur); + }else{ + while( pCur->aiIdx[pCur->iPage]==0 ){ + if( pCur->iPage==0 ){ + pCur->eState = CURSOR_INVALID; + *pRes = 1; + return SQLITE_OK; + } + moveToParent(pCur); + } + pCur->info.nSize = 0; + pCur->validNKey = 0; + + pCur->aiIdx[pCur->iPage]--; + pPage = pCur->apPage[pCur->iPage]; + if( pPage->intKey && !pPage->leaf ){ + rc = sqlite3BtreePrevious(pCur, pRes); + }else{ + rc = SQLITE_OK; + } + } + *pRes = 0; + return rc; +} + +/* +** Allocate a new page from the database file. +** +** The new page is marked as dirty. (In other words, sqlite3PagerWrite() +** has already been called on the new page.) The new page has also +** been referenced and the calling routine is responsible for calling +** sqlite3PagerUnref() on the new page when it is done. +** +** SQLITE_OK is returned on success. Any other return value indicates +** an error. *ppPage and *pPgno are undefined in the event of an error. +** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned. +** +** If the "nearby" parameter is not 0, then a (feeble) effort is made to +** locate a page close to the page number "nearby". This can be used in an +** attempt to keep related pages close to each other in the database file, +** which in turn can make database access faster. +** +** If the "exact" parameter is not 0, and the page-number nearby exists +** anywhere on the free-list, then it is guaranteed to be returned. +** +** The original comment in the vendor sqlite source said +** "[the "exact" parameter] is only used by auto-vacuum databases when allocating a new table. +** In actuality, it is and was *also* used by incremental vacuum to allocate +** the last page of the file when it is a freelist page (now only when a trunk page) +*/ +static int allocateBtreePage( + BtShared *pBt, + MemPage **ppPage, + Pgno *pPgno, + Pgno nearby, + u8 exact +){ + MemPage *pPage1; + int rc; + u32 n; /* Number of pages on the freelist */ + u32 k; /* Number of leaves on the trunk of the freelist */ + MemPage *pTrunk = 0; + MemPage *pPrevTrunk = 0; + Pgno mxPage; /* Total size of the database file */ + VVA_ONLY( int dbgTries=0 ); + + assert( sqlite3_mutex_held(pBt->mutex) ); + pPage1 = pBt->pPage1; + mxPage = btreePagecount(pBt); + n = get4byte(&pPage1->aData[36]); + testcase( n==mxPage-1 ); + if( n>=mxPage ){ + return SQLITE_CORRUPT_BKPT; + } + if( n>0 ){ + /* There are pages on the freelist. Reuse one of those pages. */ + Pgno iTrunk = 0; + Pgno iPrevTrunk = 0; + u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ + u8 firstPassOrEmptiedTrunk = 1; /* Set to false after the first run through the free-list loop; set back to true by a special case below */ + + /* If the 'exact' parameter was true and a query of the pointer-map + ** shows that the page 'nearby' is somewhere on the free-list, then + ** the entire-list will be searched for that page. + */ +#ifndef SQLITE_OMIT_AUTOVACUUM + if( exact && nearby<=mxPage ){ + u8 eType; + assert( nearby>0 ); + assert( pBt->autoVacuum ); + rc = ptrmapGet(pBt, nearby, &eType, &iTrunk); + if( rc ) return rc; + + if (eType == PTRMAP_FREEPAGE) { + /* If the ptr map for the free page had a pointer to its parent trunk, then + ** we can skip ahead in the list. + **/ + if( iTrunk>mxPage ){ + rc = SQLITE_CORRUPT_BKPT; + } + else if( iTrunk!=0 ){ + rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); + } + else { + assert( !g_expect_full_pointermap || nearby == get4byte(&pPage1->aData[32]) ); + } + if( rc ){ + pTrunk = 0; + goto end_allocate_page; + } + } + + if( eType==PTRMAP_FREEPAGE || eType==PTRMAP_FREELEAF ) { + searchList = 1; + } + *pPgno = nearby; + } +#endif + + /* Decrement the free-list count by 1. Set iTrunk to the index of the + ** first free-list trunk page. iPrevTrunk is initially 1. + */ + rc = sqlite3PagerWrite(pPage1->pDbPage); + if( rc ) return rc; + put4byte(&pPage1->aData[36], n-1); + + /* If we are searching the list, and we have full 3.0 augmented pointer map data, this + ** loop will execute only once because we are starting with pTrunk pointing to the trunk + ** page immediately before the trunk page we are searching for. + ** + ** If we are not searching the list, the loop will normally run only once, but may + ** run a second time (by setting firstPassOrEmptiedTrunk=1) if the first trunk page + ** contained only truncated leaves (the second pass will then extract the trunk page + ** itself as the allocated page). + ** + ** If we are searching the list and this is a legacy database with incomplete pointer map + ** data, this loop may still have to scan the entire freelist until the page 'nearby' is + ** located. + */ + while( searchList || firstPassOrEmptiedTrunk ) { + assert(!g_expect_full_pointermap || dbgTries++ < 2); // When we have full pointermap data, we should never iterate more than twice + firstPassOrEmptiedTrunk = 0; + pPrevTrunk = pTrunk; + iPrevTrunk = iTrunk; + if( pPrevTrunk ){ + iTrunk = get4byte(&pPrevTrunk->aData[0]); + }else{ + /* Set iTrunk to the index of the first free-list trunk page. */ + iTrunk = get4byte(&pPage1->aData[32]); + } + testcase( iTrunk==mxPage ); + if( iTrunk>mxPage ){ + rc = SQLITE_CORRUPT_BKPT; + }else{ + rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); + } + if( rc ){ + pTrunk = 0; + goto end_allocate_page; + } + + k = get4byte(&pTrunk->aData[4]); + const int origNumLeaves = k; + + if( k==0 && !searchList ){ + /* The trunk has no leaves and the list is not being searched. + ** So extract the trunk page itself and use it as the newly + ** allocated page */ + assert( pPrevTrunk==0 ); + rc = sqlite3PagerWrite(pTrunk->pDbPage); + if( rc ){ + goto end_allocate_page; + } + *pPgno = iTrunk; + memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); + + Pgno iNextTrunk = get4byte(&pTrunk->aData[0]); + if( iNextTrunk!=0 ){ + ptrmapPut(pBt, iNextTrunk, PTRMAP_FREEPAGE, 0, &rc); // We aren't searching the list, so are at the beginning of the list, so there is no previous trunk + if( rc!=SQLITE_OK ){ + goto end_allocate_page; + } + } + + *ppPage = pTrunk; + pTrunk = 0; + TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); + }else if( k>(u32)(pBt->usableSize/4 - 2) ){ + /* Value of k is out of range. Database corruption */ + rc = SQLITE_CORRUPT_BKPT; + goto end_allocate_page; +#ifndef SQLITE_OMIT_AUTOVACUUM + }else if( searchList && nearby==iTrunk ){ + /* The list is being searched and this trunk page is the page + ** to allocate, regardless of whether it has leaves. + */ + assert( *pPgno==iTrunk ); + Pgno iNextTrunk = get4byte(&pTrunk->aData[0]); + *ppPage = pTrunk; + searchList = 0; + rc = sqlite3PagerWrite(pTrunk->pDbPage); + if( rc ){ + goto end_allocate_page; + } + if( k>0 ){ + /* The trunk page is required by the caller but it contains + ** pointers to free-list leaves. The last (non-truncated) leaf becomes a trunk + ** page in this case. + */ + MemPage *pNewTrunk; + Pgno iNewTrunk; + // Find the last leaf page that isn't already truncated (beyond EOF) + do { + iNewTrunk = get4byte(&pTrunk->aData[8 + (origNumLeaves-k)*4]); + } while(iNewTrunk > mxPage && --k > 0); + + if( k>0 ) { + if( iNewTrunk>mxPage ){ + rc = SQLITE_CORRUPT_BKPT; + goto end_allocate_page; + } + testcase( iNewTrunk==mxPage ); + + /* If iNewTrunk was previously a PTRMAP_FREELEAF, it needs to + ** be changed to a PTRMAP_FREEPAGE */ + ptrmapPut(pBt, iNewTrunk, PTRMAP_FREEPAGE, iPrevTrunk, &rc); + if( rc!=SQLITE_OK ){ + goto end_allocate_page; + } + + if( iNextTrunk!=0 ) { + ptrmapPut(pBt, iNextTrunk, PTRMAP_FREEPAGE, iNewTrunk, &rc); + if( rc!=SQLITE_OK ){ + goto end_allocate_page; + } + } + + rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0); + if( rc!=SQLITE_OK ){ + goto end_allocate_page; + } + rc = sqlite3PagerWrite(pNewTrunk->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(pNewTrunk); + goto end_allocate_page; + } + memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); + put4byte(&pNewTrunk->aData[4], k-1); + memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12+(origNumLeaves-k)*4], (k-1)*4); + releasePage(pNewTrunk); + if( !pPrevTrunk ){ + assert( sqlite3PagerIswriteable(pPage1->pDbPage) ); + put4byte(&pPage1->aData[32], iNewTrunk); + }else{ + rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); + if( rc ){ + goto end_allocate_page; + } + put4byte(&pPrevTrunk->aData[0], iNewTrunk); + } + } + } + if( k==0 ){ + // All of the leaves in the trunk were already truncated / beyond EOF + // So we can just unlink the trunk and allocate it; there is no data left in it + // that needs to go somewhere else + if( iNextTrunk!=0 ){ + ptrmapPut(pBt, iNextTrunk, PTRMAP_FREEPAGE, iPrevTrunk, &rc); + if( rc!=SQLITE_OK ){ + goto end_allocate_page; + } + } + + if( !pPrevTrunk ){ + memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); + }else{ + rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); + if( rc!=SQLITE_OK ){ + goto end_allocate_page; + } + memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); + } + } + pTrunk = 0; + TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); +#endif + }else if( k>0 ){ + /* Extract a leaf from the trunk */ + u32 closest; + Pgno iPage; + unsigned char *aData = pTrunk->aData; + rc = sqlite3PagerWrite(pTrunk->pDbPage); + if( rc ){ + goto end_allocate_page; + } + + u32 i=0; + int dist=-1; + Pgno leaf; + + while( imxPage && --k>i ) { + leaf = get4byte(&aData[8+k*4]); + if( leaf<=mxPage ) + put4byte(&aData[8+i*4], leaf); + } + + if( leaf <= mxPage ){ + int d2 = sqlite3AbsInt32(leaf - nearby); + if( dist<0 || d2mxPage ){ + rc = SQLITE_CORRUPT_BKPT; + goto end_allocate_page; + } + testcase( iPage==mxPage ); + if( !searchList || iPage==nearby ){ + int noContent; + *pPgno = iPage; + TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" + ": %d more free pages\n", + *pPgno, closest+1, k, pTrunk->pgno, n-1)); + if( closestpDbPage) ); + noContent = !btreeGetHasContent(pBt, *pPgno); + rc = btreeGetPage(pBt, *pPgno, ppPage, noContent); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerWrite((*ppPage)->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(*ppPage); + } + } + searchList = 0; + }else if(k < origNumLeaves){ + put4byte(&aData[4], k); + } + } + } + releasePage(pPrevTrunk); + pPrevTrunk = 0; + } + } else { + /* There are no pages on the freelist, so create a new page at the + ** end of the file */ + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + if( rc ) return rc; + pBt->nPage++; + if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; + +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){ + /* If *pPgno refers to a pointer-map page, allocate two new pages + ** at the end of the file instead of one. The first allocated page + ** becomes a new pointer-map page, the second is used by the caller. + */ + MemPage *pPg = 0; + TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); + assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); + rc = btreeGetPage(pBt, pBt->nPage, &pPg, 1); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerWrite(pPg->pDbPage); + releasePage(pPg); + } + if( rc ) return rc; + pBt->nPage++; + if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; } + } +#endif + put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage); + *pPgno = pBt->nPage; + + assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); + rc = btreeGetPage(pBt, *pPgno, ppPage, 1); + if( rc ) return rc; + rc = sqlite3PagerWrite((*ppPage)->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(*ppPage); + } + TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); + } + + assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); + +end_allocate_page: + releasePage(pTrunk); + releasePage(pPrevTrunk); + if( rc==SQLITE_OK ){ + if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ + releasePage(*ppPage); + return SQLITE_CORRUPT_BKPT; + } + (*ppPage)->isInit = 0; + }else{ + *ppPage = 0; + } + return rc; +} + +/* +** This function is used to add page iPage to the database file free-list. +** It is assumed that the page is not already a part of the free-list. +** +** The value passed as the second argument to this function is optional. +** If the caller happens to have a pointer to the MemPage object +** corresponding to page iPage handy, it may pass it as the second value. +** Otherwise, it may pass NULL. +** +** If a pointer to a MemPage object is passed as the second argument, +** its reference count is not altered by this function. +*/ +static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ + MemPage *pTrunk = 0; /* Free-list trunk page */ + Pgno iTrunk = 0; /* Page number of free-list trunk page */ + MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ + MemPage *pPage; /* Page being freed. May be NULL. */ + int rc; /* Return Code */ + int nFree; /* Initial number of pages on free-list */ + + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( iPage>1 ); + assert( !pMemPage || pMemPage->pgno==iPage ); + + if( pMemPage ){ + pPage = pMemPage; + sqlite3PagerRef(pPage->pDbPage); + }else{ + pPage = btreePageLookup(pBt, iPage); + } + + /* Increment the free page count on pPage1 */ + rc = sqlite3PagerWrite(pPage1->pDbPage); + if( rc ) goto freepage_out; + nFree = get4byte(&pPage1->aData[36]); + put4byte(&pPage1->aData[36], nFree+1); + + if( pBt->secureDelete ){ + /* If the secure_delete option is enabled, then + ** always fully overwrite deleted information with zeros. + */ + if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) ) + || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0) + ){ + goto freepage_out; + } + memset(pPage->aData, 0, pPage->pBt->pageSize); + } + + /* If the database supports auto-vacuum, write an entry in the pointer-map + ** to indicate that the page is free. + */ + if( ISAUTOVACUUM ){ + ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc); + if( rc ) goto freepage_out; + } + + /* Now manipulate the actual database free-list structure. There are two + ** possibilities. If the free-list is currently empty, or if the first + ** trunk page in the free-list is full, then this page will become a + ** new free-list trunk page. Otherwise, it will become a leaf of the + ** first trunk page in the current free-list. This block tests if it + ** is possible to add the page as a new free-list leaf. + */ + if( nFree!=0 ){ + u32 nLeaf; /* Initial number of leaf cells on trunk page */ + + iTrunk = get4byte(&pPage1->aData[32]); + rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); + if( rc!=SQLITE_OK ){ + goto freepage_out; + } + + nLeaf = get4byte(&pTrunk->aData[4]); + assert( pBt->usableSize>32 ); + if( nLeaf > (u32)pBt->usableSize/4 - 2 ){ + rc = SQLITE_CORRUPT_BKPT; + goto freepage_out; + } + if( nLeaf < (u32)pBt->usableSize/4 - 8 ){ + /* In this case there is room on the trunk page to insert the page + ** being freed as a new leaf. + ** + ** Note that the trunk page is not really full until it contains + ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have + ** coded. But due to a coding error in versions of SQLite prior to + ** 3.6.0, databases with freelist trunk pages holding more than + ** usableSize/4 - 8 entries will be reported as corrupt. In order + ** to maintain backwards compatibility with older versions of SQLite, + ** we will continue to restrict the number of entries to usableSize/4 - 8 + ** for now. At some point in the future (once everyone has upgraded + ** to 3.6.0 or later) we should consider fixing the conditional above + ** to read "usableSize/4-2" instead of "usableSize/4-8". + */ + rc = sqlite3PagerWrite(pTrunk->pDbPage); + if( rc==SQLITE_OK ){ + put4byte(&pTrunk->aData[4], nLeaf+1); + put4byte(&pTrunk->aData[8+nLeaf*4], iPage); + if( pPage && !pBt->secureDelete ){ + sqlite3PagerDontWrite(pPage->pDbPage); + } + rc = btreeSetHasContent(pBt, iPage); + } + + /* We know this entry is a leaf, so mark it as such in the PTRMAP */ + if( ISAUTOVACUUM ){ + ptrmapPut(pBt, iPage, PTRMAP_FREELEAF, 0, &rc); + if( rc ) goto freepage_out; + } + + TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno)); + goto freepage_out; + } + } + + /* If control flows to this point, then it was not possible to add the + ** the page being freed as a leaf page of the first trunk in the free-list. + ** Possibly because the free-list is empty, or possibly because the + ** first trunk in the free-list is full. Either way, the page being freed + ** will become the new first trunk page in the free-list. + */ + if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){ + goto freepage_out; + } + rc = sqlite3PagerWrite(pPage->pDbPage); + if( rc!=SQLITE_OK ){ + goto freepage_out; + } + put4byte(pPage->aData, iTrunk); + put4byte(&pPage->aData[4], 0); + put4byte(&pPage1->aData[32], iPage); + + /* The previous head of the free list should now point to the new + ** head of the free list in the ptr map + */ + if( ISAUTOVACUUM && nFree>0 && iTrunk!=0 ){ + ptrmapPut(pBt, iTrunk, PTRMAP_FREEPAGE, iPage, &rc); + if( rc ) goto freepage_out; + } + + TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); + +freepage_out: + if( pPage ){ + pPage->isInit = 0; + } + releasePage(pPage); + releasePage(pTrunk); + return rc; +} +static void freePage(MemPage *pPage, int *pRC){ + if( (*pRC)==SQLITE_OK ){ + *pRC = freePage2(pPage->pBt, pPage, pPage->pgno); + } +} + +/* +** Free any overflow pages associated with the given Cell. +*/ +static int clearCell(MemPage *pPage, unsigned char *pCell){ + BtShared *pBt = pPage->pBt; + CellInfo info; + Pgno ovflPgno; + int rc; + int nOvfl; + u32 ovflPageSize; + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + btreeParseCellPtr(pPage, pCell, &info); + if( info.iOverflow==0 ){ + return SQLITE_OK; /* No overflow pages. Return without doing anything */ + } + ovflPgno = get4byte(&pCell[info.iOverflow]); + Pgno ovflParent = pPage->pgno; // Expected parent of ovfl, to be verified before using the page + + assert( pBt->usableSize > 4 ); + ovflPageSize = pBt->usableSize - 4; + nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize; + assert( ovflPgno==0 || nOvfl>0 ); + while( nOvfl-- ){ + // Validate link to overflow page before using it + rc = verifyParentChildLink(pBt, ovflParent, ovflPgno); + if(rc != SQLITE_OK) + return rc; + + Pgno iNext = 0; + MemPage *pOvfl = 0; + if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){ + /* 0 is not a legal page number and page 1 cannot be an + ** overflow page. Therefore if ovflPgno<2 or past the end of the + ** file the database must be corrupt. */ + return SQLITE_CORRUPT_BKPT; + } + if( nOvfl ){ + rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext); + if( rc ) return rc; + } + + if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) ) + && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1 + ){ + /* There is no reason any cursor should have an outstanding reference + ** to an overflow page belonging to a cell that is being deleted/updated. + ** So if there exists more than one reference to this page, then it + ** must not really be an overflow page and the database must be corrupt. + ** It is helpful to detect this before calling freePage2(), as + ** freePage2() may zero the page contents if secure-delete mode is + ** enabled. If this 'overflow' page happens to be a page that the + ** caller is iterating through or using in some other way, this + ** can be problematic. + */ + rc = SQLITE_CORRUPT_BKPT; + }else{ + rc = freePage2(pBt, pOvfl, ovflPgno); + } + + if( pOvfl ){ + sqlite3PagerUnref(pOvfl->pDbPage); + } + if( rc ) return rc; + ovflParent = ovflPgno; + ovflPgno = iNext; + } + return SQLITE_OK; +} + +/* +** Create the byte sequence used to represent a cell on page pPage +** and write that byte sequence into pCell[]. Overflow pages are +** allocated and filled in as necessary. The calling procedure +** is responsible for making sure sufficient space has been allocated +** for pCell[]. +** +** Note that pCell does not necessary need to point to the pPage->aData +** area. pCell might point to some temporary storage. The cell will +** be constructed in this temporary area then copied into pPage->aData +** later. +*/ +static int fillInCell( + MemPage *pPage, /* The page that contains the cell */ + unsigned char *pCell, /* Complete text of the cell */ + const void *pKey, i64 nKey, /* The key */ + const void *pData,int nData, /* The data */ + int nZero, /* Extra zero bytes to append to pData */ + int *pnSize /* Write cell size here */ +){ + int nPayload; + const u8 *pSrc; + int nSrc, n, rc; + int spaceLeft; + MemPage *pOvfl = 0; + MemPage *pToRelease = 0; + unsigned char *pPrior; + unsigned char *pPayload; + BtShared *pBt = pPage->pBt; + Pgno pgnoOvfl = 0; + int nHeader; + CellInfo info; + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + + /* pPage is not necessarily writeable since pCell might be auxiliary + ** buffer space that is separate from the pPage buffer area */ + assert( pCellaData || pCell>=&pPage->aData[pBt->pageSize] + || sqlite3PagerIswriteable(pPage->pDbPage) ); + + /* Fill in the header. */ + nHeader = 0; + if( !pPage->leaf ){ + nHeader += 4; + } + if( pPage->hasData ){ + nHeader += putVarint(&pCell[nHeader], nData+nZero); + }else{ + nData = nZero = 0; + } + nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey); + btreeParseCellPtr(pPage, pCell, &info); + assert( info.nHeader==nHeader ); + assert( info.nKey==nKey ); + assert( info.nData==(u32)(nData+nZero) ); + + /* Fill in the payload */ + nPayload = nData + nZero; + if( pPage->intKey ){ + pSrc = pData; + nSrc = nData; + nData = 0; + }else{ + if( NEVER(nKey>0x7fffffff || pKey==0) ){ + return SQLITE_CORRUPT_BKPT; + } + nPayload += (int)nKey; + pSrc = pKey; + nSrc = (int)nKey; + } + *pnSize = info.nSize; + spaceLeft = info.nLocal; + pPayload = &pCell[nHeader]; + pPrior = &pCell[info.iOverflow]; + + while( nPayload>0 ){ + if( spaceLeft==0 ){ +#ifndef SQLITE_OMIT_AUTOVACUUM + Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ + if( pBt->autoVacuum ){ + do{ + pgnoOvfl++; + } while( + PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) + ); + } +#endif + rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); +#ifndef SQLITE_OMIT_AUTOVACUUM + /* If the database supports auto-vacuum, and the second or subsequent + ** overflow page is being allocated, add an entry to the pointer-map + ** for that page now. + ** + ** If this is the first overflow page, then write a partial entry + ** to the pointer-map. If we write nothing to this pointer-map slot, + ** then the optimistic overflow chain processing in clearCell() + ** may misinterpret the uninitialised values and delete the + ** wrong pages from the database. + */ + if( pBt->autoVacuum && rc==SQLITE_OK ){ + u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); + ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc); + if( rc ){ + releasePage(pOvfl); + } + } +#endif + if( rc ){ + releasePage(pToRelease); + return rc; + } + + /* If pToRelease is not zero than pPrior points into the data area + ** of pToRelease. Make sure pToRelease is still writeable. */ + assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); + + /* If pPrior is part of the data area of pPage, then make sure pPage + ** is still writeable */ + assert( pPrioraData || pPrior>=&pPage->aData[pBt->pageSize] + || sqlite3PagerIswriteable(pPage->pDbPage) ); + + put4byte(pPrior, pgnoOvfl); + releasePage(pToRelease); + pToRelease = pOvfl; + pPrior = pOvfl->aData; + put4byte(pPrior, 0); + pPayload = &pOvfl->aData[4]; + spaceLeft = pBt->usableSize - 4; + } + n = nPayload; + if( n>spaceLeft ) n = spaceLeft; + + /* If pToRelease is not zero than pPayload points into the data area + ** of pToRelease. Make sure pToRelease is still writeable. */ + assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); + + /* If pPayload is part of the data area of pPage, then make sure pPage + ** is still writeable */ + assert( pPayloadaData || pPayload>=&pPage->aData[pBt->pageSize] + || sqlite3PagerIswriteable(pPage->pDbPage) ); + + if( nSrc>0 ){ + if( n>nSrc ) n = nSrc; + assert( pSrc ); + memcpy(pPayload, pSrc, n); + }else{ + memset(pPayload, 0, n); + } + nPayload -= n; + pPayload += n; + pSrc += n; + nSrc -= n; + spaceLeft -= n; + if( nSrc==0 ){ + nSrc = nData; + pSrc = pData; + } + } + releasePage(pToRelease); + return SQLITE_OK; +} + +/* +** Remove the i-th cell from pPage. This routine effects pPage only. +** The cell content is not freed or deallocated. It is assumed that +** the cell content has been copied someplace else. This routine just +** removes the reference to the cell from pPage. +** +** "sz" must be the number of bytes in the cell. +*/ +static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ + int i; /* Loop counter */ + u32 pc; /* Offset to cell content of cell being deleted */ + u8 *data; /* pPage->aData */ + u8 *ptr; /* Used to move bytes around within data[] */ + int rc; /* The return code */ + int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ + + if( *pRC ) return; + + assert( idx>=0 && idxnCell ); + assert( sz==cellSize(pPage, idx) ); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + data = pPage->aData; + ptr = &data[pPage->cellOffset + 2*idx]; + pc = get2byte(ptr); + hdr = pPage->hdrOffset; + testcase( pc==get2byte(&data[hdr+5]) ); + testcase( pc+sz==pPage->pBt->usableSize ); + if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){ + *pRC = SQLITE_CORRUPT_BKPT; + return; + } + rc = freeSpace(pPage, pc, sz); + if( rc ){ + *pRC = rc; + return; + } + for(i=idx+1; inCell; i++, ptr+=2){ + ptr[0] = ptr[2]; + ptr[1] = ptr[3]; + } + pPage->nCell--; + put2byte(&data[hdr+3], pPage->nCell); + pPage->nFree += 2; +} + +/* +** Insert a new cell on pPage at cell index "i". pCell points to the +** content of the cell. +** +** If the cell content will fit on the page, then put it there. If it +** will not fit, then make a copy of the cell content into pTemp if +** pTemp is not null. Regardless of pTemp, allocate a new entry +** in pPage->aOvfl[] and make it point to the cell content (either +** in pTemp or the original pCell) and also record its index. +** Allocating a new entry in pPage->aCell[] implies that +** pPage->nOverflow is incremented. +** +** If nSkip is non-zero, then do not copy the first nSkip bytes of the +** cell. The caller will overwrite them after this function returns. If +** nSkip is non-zero, then pCell may not point to an invalid memory location +** (but pCell+nSkip is always valid). +*/ +static void insertCell( + MemPage *pPage, /* Page into which we are copying */ + int i, /* New cell becomes the i-th cell of the page */ + u8 *pCell, /* Content of the new cell */ + int sz, /* Bytes of content in pCell */ + u8 *pTemp, /* Temp storage space for pCell, if needed */ + Pgno iChild, /* If non-zero, replace first 4 bytes with this value */ + int *pRC /* Read and write return code from here */ +){ + int idx = 0; /* Where to write new cell content in data[] */ + int j; /* Loop counter */ + int end; /* First byte past the last cell pointer in data[] */ + int ins; /* Index in data[] where new cell pointer is inserted */ + int cellOffset; /* Address of first cell pointer in data[] */ + u8 *data; /* The content of the whole page */ + u8 *ptr; /* Used for moving information around in data[] */ + + int nSkip = (iChild ? 4 : 0); + + if( *pRC ) return; + + assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); + assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 ); + assert( pPage->nOverflow<=ArraySize(pPage->aOvfl) ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + /* The cell should normally be sized correctly. However, when moving a + ** malformed cell from a leaf page to an interior page, if the cell size + ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size + ** might be less than 8 (leaf-size + pointer) on the interior node. Hence + ** the term after the || in the following assert(). */ + assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) ); + if( pPage->nOverflow || sz+2>pPage->nFree ){ + if( pTemp ){ + memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip); + pCell = pTemp; + } + if( iChild ){ + put4byte(pCell, iChild); + } + j = pPage->nOverflow++; + assert( j<(int)(sizeof(pPage->aOvfl)/sizeof(pPage->aOvfl[0])) ); + pPage->aOvfl[j].pCell = pCell; + pPage->aOvfl[j].idx = (u16)i; + }else{ + int rc = sqlite3PagerWrite(pPage->pDbPage); + if( rc!=SQLITE_OK ){ + *pRC = rc; + return; + } + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + data = pPage->aData; + cellOffset = pPage->cellOffset; + end = cellOffset + 2*pPage->nCell; + ins = cellOffset + 2*i; + rc = allocateSpace(pPage, sz, &idx); + if( rc ){ *pRC = rc; return; } + /* The allocateSpace() routine guarantees the following two properties + ** if it returns success */ + assert( idx >= end+2 ); + assert( idx+sz <= pPage->pBt->usableSize ); + pPage->nCell++; + pPage->nFree -= (u16)(2 + sz); + memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip); + if( iChild ){ + put4byte(&data[idx], iChild); + } + for(j=end, ptr=&data[j]; j>ins; j-=2, ptr-=2){ + ptr[0] = ptr[-2]; + ptr[1] = ptr[-1]; + } + put2byte(&data[ins], idx); + put2byte(&data[pPage->hdrOffset+3], pPage->nCell); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pPage->pBt->autoVacuum ){ + /* The cell may contain a pointer to an overflow page. If so, write + ** the entry for the overflow page into the pointer map. + */ + ptrmapPutOvflPtr(pPage, pCell, pRC); + } +#endif + } +} + +/* +** Add a list of cells to a page. The page should be initially empty. +** The cells are guaranteed to fit on the page. +*/ +static void assemblePage( + MemPage *pPage, /* The page to be assemblied */ + int nCell, /* The number of cells to add to this page */ + u8 **apCell, /* Pointers to cell bodies */ + u16 *aSize /* Sizes of the cells */ +){ + int i; /* Loop counter */ + u8 *pCellptr; /* Address of next cell pointer */ + int cellbody; /* Address of next cell body */ + u8 * const data = pPage->aData; /* Pointer to data for pPage */ + const int hdr = pPage->hdrOffset; /* Offset of header on pPage */ + const int nUsable = pPage->pBt->usableSize; /* Usable size of page */ + + assert( pPage->nOverflow==0 ); + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( nCell>=0 && nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921); + assert( sqlite3PagerIswriteable(pPage->pDbPage) ); + + /* Check that the page has just been zeroed by zeroPage() */ + assert( pPage->nCell==0 ); + assert( get2byteNotZero(&data[hdr+5])==nUsable ); + + pCellptr = &data[pPage->cellOffset + nCell*2]; + cellbody = nUsable; + for(i=nCell-1; i>=0; i--){ + pCellptr -= 2; + cellbody -= aSize[i]; + put2byte(pCellptr, cellbody); + memcpy(&data[cellbody], apCell[i], aSize[i]); + } + put2byte(&data[hdr+3], nCell); + put2byte(&data[hdr+5], cellbody); + pPage->nFree -= (nCell*2 + nUsable - cellbody); + pPage->nCell = (u16)nCell; +} + +/* +** The following parameters determine how many adjacent pages get involved +** in a balancing operation. NN is the number of neighbors on either side +** of the page that participate in the balancing operation. NB is the +** total number of pages that participate, including the target page and +** NN neighbors on either side. +** +** The minimum value of NN is 1 (of course). Increasing NN above 1 +** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance +** in exchange for a larger degradation in INSERT and UPDATE performance. +** The value of NN appears to give the best results overall. +*/ +#define NN 1 /* Number of neighbors on either side of pPage */ +#define NB (NN*2+1) /* Total pages involved in the balance */ + + +#ifndef SQLITE_OMIT_QUICKBALANCE +/* +** This version of balance() handles the common special case where +** a new entry is being inserted on the extreme right-end of the +** tree, in other words, when the new entry will become the largest +** entry in the tree. +** +** Instead of trying to balance the 3 right-most leaf pages, just add +** a new page to the right-hand side and put the one new entry in +** that page. This leaves the right side of the tree somewhat +** unbalanced. But odds are that we will be inserting new entries +** at the end soon afterwards so the nearly empty page will quickly +** fill up. On average. +** +** pPage is the leaf page which is the right-most page in the tree. +** pParent is its parent. pPage must have a single overflow entry +** which is also the right-most entry on the page. +** +** The pSpace buffer is used to store a temporary copy of the divider +** cell that will be inserted into pParent. Such a cell consists of a 4 +** byte page number followed by a variable length integer. In other +** words, at most 13 bytes. Hence the pSpace buffer must be at +** least 13 bytes in size. +*/ +static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ + BtShared *const pBt = pPage->pBt; /* B-Tree Database */ + MemPage *pNew; /* Newly allocated page */ + int rc; /* Return Code */ + Pgno pgnoNew; /* Page number of pNew */ + + assert( sqlite3_mutex_held(pPage->pBt->mutex) ); + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + assert( pPage->nOverflow==1 ); + + /* This error condition is now caught prior to reaching this function */ + if( pPage->nCell<=0 ) return SQLITE_CORRUPT_BKPT; + + /* Allocate a new page. This page will become the right-sibling of + ** pPage. Make the parent page writable, so that the new divider cell + ** may be inserted. If both these operations are successful, proceed. + */ + rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); + + if( rc==SQLITE_OK ){ + + u8 *pOut = &pSpace[4]; + u8 *pCell = pPage->aOvfl[0].pCell; + u16 szCell = cellSizePtr(pPage, pCell); + u8 *pStop; + + assert( sqlite3PagerIswriteable(pNew->pDbPage) ); + assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); + zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); + assemblePage(pNew, 1, &pCell, &szCell); + + /* If this is an auto-vacuum database, update the pointer map + ** with entries for the new page, and any pointer from the + ** cell on the page to an overflow page. If either of these + ** operations fails, the return code is set, but the contents + ** of the parent page are still manipulated by thh code below. + ** That is Ok, at this point the parent page is guaranteed to + ** be marked as dirty. Returning an error code will cause a + ** rollback, undoing any changes made to the parent page. + */ + if( ISAUTOVACUUM ){ + ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc); + if( szCell>pNew->minLocal ){ + ptrmapPutOvflPtr(pNew, pCell, &rc); + } + } + + /* Create a divider cell to insert into pParent. The divider cell + ** consists of a 4-byte page number (the page number of pPage) and + ** a variable length key value (which must be the same value as the + ** largest key on pPage). + ** + ** To find the largest key value on pPage, first find the right-most + ** cell on pPage. The first two fields of this cell are the + ** record-length (a variable length integer at most 32-bits in size) + ** and the key value (a variable length integer, may have any value). + ** The first of the while(...) loops below skips over the record-length + ** field. The second while(...) loop copies the key value from the + ** cell on pPage into the pSpace buffer. + */ + pCell = findCell(pPage, pPage->nCell-1); + pStop = &pCell[9]; + while( (*(pCell++)&0x80) && pCellnCell, pSpace, (int)(pOut-pSpace), + 0, pPage->pgno, &rc); + + /* Set the right-child pointer of pParent to point to the new page. */ + put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); + + /* Release the reference to the new page. */ + releasePage(pNew); + } + + return rc; +} +#endif /* SQLITE_OMIT_QUICKBALANCE */ + +#if 0 +/* +** This function does not contribute anything to the operation of SQLite. +** it is sometimes activated temporarily while debugging code responsible +** for setting pointer-map entries. +*/ +static int ptrmapCheckPages(MemPage **apPage, int nPage){ + int i, j; + for(i=0; ipBt; + assert( pPage->isInit ); + + for(j=0; jnCell; j++){ + CellInfo info; + u8 *z; + + z = findCell(pPage, j); + btreeParseCellPtr(pPage, z, &info); + if( info.iOverflow ){ + Pgno ovfl = get4byte(&z[info.iOverflow]); + ptrmapGet(pBt, ovfl, &e, &n); + assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 ); + } + if( !pPage->leaf ){ + Pgno child = get4byte(z); + ptrmapGet(pBt, child, &e, &n); + assert( n==pPage->pgno && e==PTRMAP_BTREE ); + } + } + if( !pPage->leaf ){ + Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]); + ptrmapGet(pBt, child, &e, &n); + assert( n==pPage->pgno && e==PTRMAP_BTREE ); + } + } + return 1; +} +#endif + +/* +** This function is used to copy the contents of the b-tree node stored +** on page pFrom to page pTo. If page pFrom was not a leaf page, then +** the pointer-map entries for each child page are updated so that the +** parent page stored in the pointer map is page pTo. If pFrom contained +** any cells with overflow page pointers, then the corresponding pointer +** map entries are also updated so that the parent page is page pTo. +** +** If pFrom is currently carrying any overflow cells (entries in the +** MemPage.aOvfl[] array), they are not copied to pTo. +** +** Before returning, page pTo is reinitialized using btreeInitPage(). +** +** The performance of this function is not critical. It is only used by +** the balance_shallower() and balance_deeper() procedures, neither of +** which are called often under normal circumstances. +*/ +static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ + if( (*pRC)==SQLITE_OK ){ + BtShared * const pBt = pFrom->pBt; + u8 * const aFrom = pFrom->aData; + u8 * const aTo = pTo->aData; + int const iFromHdr = pFrom->hdrOffset; + int const iToHdr = ((pTo->pgno==1) ? 100 : 0); + int rc; + int iData; + + + assert( pFrom->isInit ); + assert( pFrom->nFree>=iToHdr ); + assert( get2byte(&aFrom[iFromHdr+5])<=pBt->usableSize ); + + /* Copy the b-tree node content from page pFrom to page pTo. */ + iData = get2byte(&aFrom[iFromHdr+5]); + memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData); + memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell); + + /* Reinitialize page pTo so that the contents of the MemPage structure + ** match the new data. The initialization of pTo can actually fail under + ** fairly obscure circumstances, even though it is a copy of initialized + ** page pFrom. + */ + pTo->isInit = 0; + rc = btreeInitPage(pTo); + if( rc!=SQLITE_OK ){ + *pRC = rc; + return; + } + + /* If this is an auto-vacuum database, update the pointer-map entries + ** for any b-tree or overflow pages that pTo now contains the pointers to. + */ + if( ISAUTOVACUUM ){ + *pRC = setChildPtrmaps(pTo); + } + } +} + +/* +** This routine redistributes cells on the iParentIdx'th child of pParent +** (hereafter "the page") and up to 2 siblings so that all pages have about the +** same amount of free space. Usually a single sibling on either side of the +** page are used in the balancing, though both siblings might come from one +** side if the page is the first or last child of its parent. If the page +** has fewer than 2 siblings (something which can only happen if the page +** is a root page or a child of a root page) then all available siblings +** participate in the balancing. +** +** The number of siblings of the page might be increased or decreased by +** one or two in an effort to keep pages nearly full but not over full. +** +** Note that when this routine is called, some of the cells on the page +** might not actually be stored in MemPage.aData[]. This can happen +** if the page is overfull. This routine ensures that all cells allocated +** to the page and its siblings fit into MemPage.aData[] before returning. +** +** In the course of balancing the page and its siblings, cells may be +** inserted into or removed from the parent page (pParent). Doing so +** may cause the parent page to become overfull or underfull. If this +** happens, it is the responsibility of the caller to invoke the correct +** balancing routine to fix this problem (see the balance() routine). +** +** If this routine fails for any reason, it might leave the database +** in a corrupted state. So if this routine fails, the database should +** be rolled back. +** +** The third argument to this function, aOvflSpace, is a pointer to a +** buffer big enough to hold one page. If while inserting cells into the parent +** page (pParent) the parent page becomes overfull, this buffer is +** used to store the parent's overflow cells. Because this function inserts +** a maximum of four divider cells into the parent page, and the maximum +** size of a cell stored within an internal node is always less than 1/4 +** of the page-size, the aOvflSpace[] buffer is guaranteed to be large +** enough for all overflow cells. +** +** If aOvflSpace is set to a null pointer, this function returns +** SQLITE_NOMEM. +*/ +static int balance_nonroot( + MemPage *pParent, /* Parent page of siblings being balanced */ + int iParentIdx, /* Index of "the page" in pParent */ + u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */ + int isRoot /* True if pParent is a root-page */ +){ + BtShared *pBt; /* The whole database */ + int nCell = 0; /* Number of cells in apCell[] */ + int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ + int nNew = 0; /* Number of pages in apNew[] */ + int nOld; /* Number of pages in apOld[] */ + int i, j, k; /* Loop counters */ + int nxDiv; /* Next divider slot in pParent->aCell[] */ + int rc = SQLITE_OK; /* The return code */ + u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */ + int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ + int usableSpace; /* Bytes in pPage beyond the header */ + int pageFlags; /* Value of pPage->aData[0] */ + int subtotal; /* Subtotal of bytes in cells on one page */ + int iSpace1 = 0; /* First unused byte of aSpace1[] */ + int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ + int szScratch; /* Size of scratch memory requested */ + MemPage *apOld[NB]; /* pPage and up to two siblings */ + MemPage *apCopy[NB]; /* Private copies of apOld[] pages */ + MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ + u8 *pRight; /* Location in parent of right-sibling pointer */ + u8 *apDiv[NB-1]; /* Divider cells in pParent */ + int cntNew[NB+2]; /* Index in aCell[] of cell after i-th page */ + int szNew[NB+2]; /* Combined size of cells place on i-th page */ + u8 **apCell = 0; /* All cells begin balanced */ + u16 *szCell; /* Local size of all cells in apCell[] */ + u8 *aSpace1; /* Space for copies of dividers cells */ + Pgno pgno; /* Temp var to store a page number in */ + + pBt = pParent->pBt; + assert( sqlite3_mutex_held(pBt->mutex) ); + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + +#if 0 + TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); +#endif + + /* At this point pParent may have at most one overflow cell. And if + ** this overflow cell is present, it must be the cell with + ** index iParentIdx. This scenario comes about when this function + ** is called (indirectly) from sqlite3BtreeDelete(). + */ + assert( pParent->nOverflow==0 || pParent->nOverflow==1 ); + assert( pParent->nOverflow==0 || pParent->aOvfl[0].idx==iParentIdx ); + + if( !aOvflSpace ){ + return SQLITE_NOMEM; + } + + /* Find the sibling pages to balance. Also locate the cells in pParent + ** that divide the siblings. An attempt is made to find NN siblings on + ** either side of pPage. More siblings are taken from one side, however, + ** if there are fewer than NN siblings on the other side. If pParent + ** has NB or fewer children then all children of pParent are taken. + ** + ** This loop also drops the divider cells from the parent page. This + ** way, the remainder of the function does not have to deal with any + ** overflow cells in the parent page, since if any existed they will + ** have already been removed. + */ + i = pParent->nOverflow + pParent->nCell; + if( i<2 ){ + nxDiv = 0; + nOld = i+1; + }else{ + nOld = 3; + if( iParentIdx==0 ){ + nxDiv = 0; + }else if( iParentIdx==i ){ + nxDiv = i-2; + }else{ + nxDiv = iParentIdx-1; + } + i = 2; + } + if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){ + pRight = &pParent->aData[pParent->hdrOffset+8]; + }else{ + pRight = findCell(pParent, i+nxDiv-pParent->nOverflow); + } + pgno = get4byte(pRight); + while( 1 ){ + rc = getAndInitPage(pBt, pgno, &apOld[i]); + if( rc ){ + memset(apOld, 0, (i+1)*sizeof(MemPage*)); + goto balance_cleanup; + } + nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; + if( (i--)==0 ) break; + + if( i+nxDiv==pParent->aOvfl[0].idx && pParent->nOverflow ){ + apDiv[i] = pParent->aOvfl[0].pCell; + pgno = get4byte(apDiv[i]); + szNew[i] = cellSizePtr(pParent, apDiv[i]); + pParent->nOverflow = 0; + }else{ + apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow); + pgno = get4byte(apDiv[i]); + szNew[i] = cellSizePtr(pParent, apDiv[i]); + + /* Drop the cell from the parent page. apDiv[i] still points to + ** the cell within the parent, even though it has been dropped. + ** This is safe because dropping a cell only overwrites the first + ** four bytes of it, and this function does not need the first + ** four bytes of the divider cell. So the pointer is safe to use + ** later on. + ** + ** Unless SQLite is compiled in secure-delete mode. In this case, + ** the dropCell() routine will overwrite the entire cell with zeroes. + ** In this case, temporarily copy the cell into the aOvflSpace[] + ** buffer. It will be copied out again as soon as the aSpace[] buffer + ** is allocated. */ + if( pBt->secureDelete ){ + int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); + if( (iOff+szNew[i])>(int)pBt->usableSize ){ + rc = SQLITE_CORRUPT_BKPT; + memset(apOld, 0, (i+1)*sizeof(MemPage*)); + goto balance_cleanup; + }else{ + memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]); + apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData]; + } + } + dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc); + } + } + + /* Make nMaxCells a multiple of 4 in order to preserve 8-byte + ** alignment */ + nMaxCells = (nMaxCells + 3)&~3; + + /* + ** Allocate space for memory structures + */ + k = pBt->pageSize + ROUND8(sizeof(MemPage)); + szScratch = + nMaxCells*sizeof(u8*) /* apCell */ + + nMaxCells*sizeof(u16) /* szCell */ + + pBt->pageSize /* aSpace1 */ + + k*nOld; /* Page copies (apCopy) */ + apCell = sqlite3ScratchMalloc( szScratch ); + if( apCell==0 ){ + rc = SQLITE_NOMEM; + goto balance_cleanup; + } + szCell = (u16*)&apCell[nMaxCells]; + aSpace1 = (u8*)&szCell[nMaxCells]; + assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); + + /* + ** Load pointers to all cells on sibling pages and the divider cells + ** into the local apCell[] array. Make copies of the divider cells + ** into space obtained from aSpace1[] and remove the the divider Cells + ** from pParent. + ** + ** If the siblings are on leaf pages, then the child pointers of the + ** divider cells are stripped from the cells before they are copied + ** into aSpace1[]. In this way, all cells in apCell[] are without + ** child pointers. If siblings are not leaves, then all cell in + ** apCell[] include child pointers. Either way, all cells in apCell[] + ** are alike. + ** + ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. + ** leafData: 1 if pPage holds key+data and pParent holds only keys. + */ + leafCorrection = apOld[0]->leaf*4; + leafData = apOld[0]->hasData; + for(i=0; ipageSize + k*i]; + memcpy(pOld, apOld[i], sizeof(MemPage)); + pOld->aData = (void*)&pOld[1]; + memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize); + + limit = pOld->nCell+pOld->nOverflow; + for(j=0; jmaxLocal+23 ); + assert( iSpace1<=pBt->pageSize ); + memcpy(pTemp, apDiv[i], sz); + apCell[nCell] = pTemp+leafCorrection; + assert( leafCorrection==0 || leafCorrection==4 ); + szCell[nCell] = szCell[nCell] - leafCorrection; + if( !pOld->leaf ){ + assert( leafCorrection==0 ); + assert( pOld->hdrOffset==0 ); + /* The right pointer of the child page pOld becomes the left + ** pointer of the divider cell */ + memcpy(apCell[nCell], &pOld->aData[8], 4); + }else{ + assert( leafCorrection==4 ); + if( szCell[nCell]<4 ){ + /* Do not allow any cells smaller than 4 bytes. */ + szCell[nCell] = 4; + } + } + nCell++; + } + } + + /* + ** Figure out the number of pages needed to hold all nCell cells. + ** Store this number in "k". Also compute szNew[] which is the total + ** size of all cells on the i-th page and cntNew[] which is the index + ** in apCell[] of the cell that divides page i from page i+1. + ** cntNew[k] should equal nCell. + ** + ** Values computed by this block: + ** + ** k: The total number of sibling pages + ** szNew[i]: Spaced used on the i-th sibling page. + ** cntNew[i]: Index in apCell[] and szCell[] for the first cell to + ** the right of the i-th sibling page. + ** usableSpace: Number of bytes of space available on each sibling. + ** + */ + usableSpace = pBt->usableSize - 12 + leafCorrection; + for(subtotal=k=i=0; i usableSpace ){ + szNew[k] = subtotal - szCell[i]; + cntNew[k] = i; + if( leafData ){ i--; } + subtotal = 0; + k++; + if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } + } + } + szNew[k] = subtotal; + cntNew[k] = nCell; + k++; + + /* + ** The packing computed by the previous block is biased toward the siblings + ** on the left side. The left siblings are always nearly full, while the + ** right-most sibling might be nearly empty. This block of code attempts + ** to adjust the packing of siblings to get a better balance. + ** + ** This adjustment is more than an optimization. The packing above might + ** be so out of balance as to be illegal. For example, the right-most + ** sibling might be completely empty. This adjustment is not optional. + */ + for(i=k-1; i>0; i--){ + int szRight = szNew[i]; /* Size of sibling on the right */ + int szLeft = szNew[i-1]; /* Size of sibling on the left */ + int r; /* Index of right-most cell in left sibling */ + int d; /* Index of first cell to the left of right sibling */ + + r = cntNew[i-1] - 1; + d = r + 1 - leafData; + assert( d0) or pPage is + ** a virtual root page. A virtual root page is when the real root + ** page is page 1 and we are the only child of that page. + */ + assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) ); + + TRACE(("BALANCE: old: %d %d %d ", + apOld[0]->pgno, + nOld>=2 ? apOld[1]->pgno : 0, + nOld>=3 ? apOld[2]->pgno : 0 + )); + + /* + ** Allocate k new pages. Reuse old pages where possible. + */ + if( apOld[0]->pgno<=1 ){ + rc = SQLITE_CORRUPT_BKPT; + goto balance_cleanup; + } + pageFlags = apOld[0]->aData[0]; + for(i=0; ipDbPage); + nNew++; + if( rc ) goto balance_cleanup; + }else{ + assert( i>0 ); + rc = allocateBtreePage(pBt, &pNew, &pgno, pgno, 0); + if( rc ) goto balance_cleanup; + apNew[i] = pNew; + nNew++; + + /* Set the pointer-map entry for the new sibling page. */ + if( ISAUTOVACUUM ){ + ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); + if( rc!=SQLITE_OK ){ + goto balance_cleanup; + } + } + } + } + + /* Free any old pages that were not reused as new pages. + */ + while( ipgno; + int minI = i; + for(j=i+1; jpgno<(unsigned)minV ){ + minI = j; + minV = apNew[j]->pgno; + } + } + if( minI>i ){ + MemPage *pT; + pT = apNew[i]; + apNew[i] = apNew[minI]; + apNew[minI] = pT; + } + } + TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n", + apNew[0]->pgno, szNew[0], + nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, + nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, + nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, + nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0)); + + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + put4byte(pRight, apNew[nNew-1]->pgno); + + /* + ** Evenly distribute the data in apCell[] across the new pages. + ** Insert divider cells into pParent as necessary. + */ + j = 0; + for(i=0; inCell>0 || (nNew==1 && cntNew[0]==0) ); + assert( pNew->nOverflow==0 ); + + j = cntNew[i]; + + /* If the sibling page assembled above was not the right-most sibling, + ** insert a divider cell into the parent page. + */ + assert( ileaf ){ + memcpy(&pNew->aData[8], pCell, 4); + }else if( leafData ){ + /* If the tree is a leaf-data tree, and the siblings are leaves, + ** then there is no divider cell in apCell[]. Instead, the divider + ** cell consists of the integer key for the right-most cell of + ** the sibling-page assembled above only. + */ + CellInfo info; + j--; + btreeParseCellPtr(pNew, apCell[j], &info); + pCell = pTemp; + sz = 4 + putVarint(&pCell[4], info.nKey); + pTemp = 0; + }else{ + pCell -= 4; + /* Obscure case for non-leaf-data trees: If the cell at pCell was + ** previously stored on a leaf node, and its reported size was 4 + ** bytes, then it may actually be smaller than this + ** (see btreeParseCellPtr(), 4 bytes is the minimum size of + ** any cell). But it is important to pass the correct size to + ** insertCell(), so reparse the cell now. + ** + ** Note that this can never happen in an SQLite data file, as all + ** cells are at least 4 bytes. It only happens in b-trees used + ** to evaluate "IN (SELECT ...)" and similar clauses. + */ + if( szCell[j]==4 ){ + assert(leafCorrection==4); + sz = cellSizePtr(pParent, pCell); + } + } + iOvflSpace += sz; + assert( sz<=pBt->maxLocal+23 ); + assert( iOvflSpace<=pBt->pageSize ); + insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc); + if( rc!=SQLITE_OK ) goto balance_cleanup; + assert( sqlite3PagerIswriteable(pParent->pDbPage) ); + + j++; + nxDiv++; + } + } + assert( j==nCell ); + assert( nOld>0 ); + assert( nNew>0 ); + if( (pageFlags & PTF_LEAF)==0 ){ + u8 *zChild = &apCopy[nOld-1]->aData[8]; + memcpy(&apNew[nNew-1]->aData[8], zChild, 4); + } + + if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ + /* The root page of the b-tree now contains no cells. The only sibling + ** page is the right-child of the parent. Copy the contents of the + ** child page into the parent, decreasing the overall height of the + ** b-tree structure by one. This is described as the "balance-shallower" + ** sub-algorithm in some documentation. + ** + ** If this is an auto-vacuum database, the call to copyNodeContent() + ** sets all pointer-map entries corresponding to database image pages + ** for which the pointer is stored within the content being copied. + ** + ** The second assert below verifies that the child page is defragmented + ** (it must be, as it was just reconstructed using assemblePage()). This + ** is important if the parent page happens to be page 1 of the database + ** image. */ + assert( nNew==1 ); + assert( apNew[0]->nFree == + (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) + ); + copyNodeContent(apNew[0], pParent, &rc); + freePage(apNew[0], &rc); + }else if( ISAUTOVACUUM ){ + /* Fix the pointer-map entries for all the cells that were shifted around. + ** There are several different types of pointer-map entries that need to + ** be dealt with by this routine. Some of these have been set already, but + ** many have not. The following is a summary: + ** + ** 1) The entries associated with new sibling pages that were not + ** siblings when this function was called. These have already + ** been set. We don't need to worry about old siblings that were + ** moved to the free-list - the freePage() code has taken care + ** of those. + ** + ** 2) The pointer-map entries associated with the first overflow + ** page in any overflow chains used by new divider cells. These + ** have also already been taken care of by the insertCell() code. + ** + ** 3) If the sibling pages are not leaves, then the child pages of + ** cells stored on the sibling pages may need to be updated. + ** + ** 4) If the sibling pages are not internal intkey nodes, then any + ** overflow pages used by these cells may need to be updated + ** (internal intkey nodes never contain pointers to overflow pages). + ** + ** 5) If the sibling pages are not leaves, then the pointer-map + ** entries for the right-child pages of each sibling may need + ** to be updated. + ** + ** Cases 1 and 2 are dealt with above by other code. The next + ** block deals with cases 3 and 4 and the one after that, case 5. Since + ** setting a pointer map entry is a relatively expensive operation, this + ** code only sets pointer map entries for child or overflow pages that have + ** actually moved between pages. */ + MemPage *pNew = apNew[0]; + MemPage *pOld = apCopy[0]; + int nOverflow = pOld->nOverflow; + int iNextOld = pOld->nCell + nOverflow; + int iOverflow = (nOverflow ? pOld->aOvfl[0].idx : -1); + j = 0; /* Current 'old' sibling page */ + k = 0; /* Current 'new' sibling page */ + for(i=0; inCell + pOld->nOverflow; + if( pOld->nOverflow ){ + nOverflow = pOld->nOverflow; + iOverflow = i + !leafData + pOld->aOvfl[0].idx; + } + isDivider = !leafData; + } + + assert(nOverflow>0 || iOverflowaOvfl[0].idx==pOld->aOvfl[1].idx-1); + assert(nOverflow<3 || pOld->aOvfl[1].idx==pOld->aOvfl[2].idx-1); + if( i==iOverflow ){ + isDivider = 1; + if( (--nOverflow)>0 ){ + iOverflow++; + } + } + + if( i==cntNew[k] ){ + /* Cell i is the cell immediately following the last cell on new + ** sibling page k. If the siblings are not leaf pages of an + ** intkey b-tree, then cell i is a divider cell. */ + pNew = apNew[++k]; + if( !leafData ) continue; + } + assert( jpgno!=pNew->pgno ){ + if( !leafCorrection ){ + ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc); + } + if( szCell[i]>pNew->minLocal ){ + ptrmapPutOvflPtr(pNew, apCell[i], &rc); + } + } + } + + if( !leafCorrection ){ + for(i=0; iaData[8]); + ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); + } + } + +#if 0 + /* The ptrmapCheckPages() contains assert() statements that verify that + ** all pointer map pages are set correctly. This is helpful while + ** debugging. This is usually disabled because a corrupt database may + ** cause an assert() statement to fail. */ + ptrmapCheckPages(apNew, nNew); + ptrmapCheckPages(&pParent, 1); +#endif + } + + assert( pParent->isInit ); + TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", + nOld, nNew, nCell)); + + /* + ** Cleanup before returning. + */ +balance_cleanup: + sqlite3ScratchFree(apCell); + for(i=0; ipBt; /* The BTree */ + + assert( pRoot->nOverflow>0 ); + assert( sqlite3_mutex_held(pBt->mutex) ); + + /* Make pRoot, the root page of the b-tree, writable. Allocate a new + ** page that will become the new right-child of pPage. Copy the contents + ** of the node stored on pRoot into the new child page. + */ + rc = sqlite3PagerWrite(pRoot->pDbPage); + if( rc==SQLITE_OK ){ + rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0); + copyNodeContent(pRoot, pChild, &rc); + if( ISAUTOVACUUM ){ + ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc); + } + } + if( rc ){ + *ppChild = 0; + releasePage(pChild); + return rc; + } + assert( sqlite3PagerIswriteable(pChild->pDbPage) ); + assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); + assert( pChild->nCell==pRoot->nCell ); + + TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno)); + + /* Copy the overflow cells from pRoot to pChild */ + memcpy(pChild->aOvfl, pRoot->aOvfl, pRoot->nOverflow*sizeof(pRoot->aOvfl[0])); + pChild->nOverflow = pRoot->nOverflow; + + /* Zero the contents of pRoot. Then install pChild as the right-child. */ + zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF); + put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild); + + *ppChild = pChild; + return SQLITE_OK; +} + +/* +** The page that pCur currently points to has just been modified in +** some way. This function figures out if this modification means the +** tree needs to be balanced, and if so calls the appropriate balancing +** routine. Balancing routines are: +** +** balance_quick() +** balance_deeper() +** balance_nonroot() +*/ +static int balance(BtCursor *pCur){ + int rc = SQLITE_OK; + const int nMin = pCur->pBt->usableSize * 2 / 3; + u8 aBalanceQuickSpace[13]; + u8 *pFree = 0; + + TESTONLY( int balance_quick_called = 0 ); + TESTONLY( int balance_deeper_called = 0 ); + + do { + int iPage = pCur->iPage; + MemPage *pPage = pCur->apPage[iPage]; + + if( iPage==0 ){ + if( pPage->nOverflow ){ + /* The root page of the b-tree is overfull. In this case call the + ** balance_deeper() function to create a new child for the root-page + ** and copy the current contents of the root-page to it. The + ** next iteration of the do-loop will balance the child page. + */ + assert( (balance_deeper_called++)==0 ); + rc = balance_deeper(pPage, &pCur->apPage[1]); + if( rc==SQLITE_OK ){ + pCur->iPage = 1; + pCur->aiIdx[0] = 0; + pCur->aiIdx[1] = 0; + assert( pCur->apPage[1]->nOverflow ); + } + }else{ + break; + } + }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ + break; + }else{ + MemPage * const pParent = pCur->apPage[iPage-1]; + int const iIdx = pCur->aiIdx[iPage-1]; + + rc = sqlite3PagerWrite(pParent->pDbPage); + if( rc==SQLITE_OK ){ +#ifndef SQLITE_OMIT_QUICKBALANCE + if( pPage->hasData + && pPage->nOverflow==1 + && pPage->aOvfl[0].idx==pPage->nCell + && pParent->pgno!=1 + && pParent->nCell==iIdx + ){ + /* Call balance_quick() to create a new sibling of pPage on which + ** to store the overflow cell. balance_quick() inserts a new cell + ** into pParent, which may cause pParent overflow. If this + ** happens, the next interation of the do-loop will balance pParent + ** use either balance_nonroot() or balance_deeper(). Until this + ** happens, the overflow cell is stored in the aBalanceQuickSpace[] + ** buffer. + ** + ** The purpose of the following assert() is to check that only a + ** single call to balance_quick() is made for each call to this + ** function. If this were not verified, a subtle bug involving reuse + ** of the aBalanceQuickSpace[] might sneak in. + */ + assert( (balance_quick_called++)==0 ); + rc = balance_quick(pParent, pPage, aBalanceQuickSpace); + }else +#endif + { + /* In this case, call balance_nonroot() to redistribute cells + ** between pPage and up to 2 of its sibling pages. This involves + ** modifying the contents of pParent, which may cause pParent to + ** become overfull or underfull. The next iteration of the do-loop + ** will balance the parent page to correct this. + ** + ** If the parent page becomes overfull, the overflow cell or cells + ** are stored in the pSpace buffer allocated immediately below. + ** A subsequent iteration of the do-loop will deal with this by + ** calling balance_nonroot() (balance_deeper() may be called first, + ** but it doesn't deal with overflow cells - just moves them to a + ** different page). Once this subsequent call to balance_nonroot() + ** has completed, it is safe to release the pSpace buffer used by + ** the previous call, as the overflow cell data will have been + ** copied either into the body of a database page or into the new + ** pSpace buffer passed to the latter call to balance_nonroot(). + */ + u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize); + rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1); + if( pFree ){ + /* If pFree is not NULL, it points to the pSpace buffer used + ** by a previous call to balance_nonroot(). Its contents are + ** now stored either on real database pages or within the + ** new pSpace buffer, so it may be safely freed here. */ + sqlite3PageFree(pFree); + } + + /* The pSpace buffer will be freed after the next call to + ** balance_nonroot(), or just before this function returns, whichever + ** comes first. */ + pFree = pSpace; + } + } + + pPage->nOverflow = 0; + + /* The next iteration of the do-loop balances the parent page. */ + releasePage(pPage); + pCur->iPage--; + } + }while( rc==SQLITE_OK ); + + if( pFree ){ + sqlite3PageFree(pFree); + } + return rc; +} + + +/* +** Insert a new record into the BTree. The key is given by (pKey,nKey) +** and the data is given by (pData,nData). The cursor is used only to +** define what table the record should be inserted into. The cursor +** is left pointing at a random location. +** +** For an INTKEY table, only the nKey value of the key is used. pKey is +** ignored. For a ZERODATA table, the pData and nData are both ignored. +** +** If the seekResult parameter is non-zero, then a successful call to +** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already +** been performed. seekResult is the search result returned (a negative +** number if pCur points at an entry that is smaller than (pKey, nKey), or +** a positive value if pCur points at an etry that is larger than +** (pKey, nKey)). +** +** If the seekResult parameter is non-zero, then the caller guarantees that +** cursor pCur is pointing at the existing copy of a row that is to be +** overwritten. If the seekResult parameter is 0, then cursor pCur may +** point to any entry or to no entry at all and so this function has to seek +** the cursor before the new key can be inserted. +*/ +SQLITE_PRIVATE int sqlite3BtreeInsert( + BtCursor *pCur, /* Insert data into the table of this cursor */ + const void *pKey, i64 nKey, /* The key of the new record */ + const void *pData, int nData, /* The data of the new record */ + int nZero, /* Number of extra 0 bytes to append to data */ + int appendBias, /* True if this is likely an append */ + int seekResult /* Result of prior MovetoUnpacked() call */ +){ + int rc; + int loc = seekResult; /* -1: before desired location +1: after */ + int szNew = 0; + int idx; + MemPage *pPage; + Btree *p = pCur->pBtree; + BtShared *pBt = p->pBt; + unsigned char *oldCell; + unsigned char *newCell = 0; + + if( pCur->eState==CURSOR_FAULT ){ + assert( pCur->skipNext!=SQLITE_OK ); + return pCur->skipNext; + } + + assert( cursorHoldsMutex(pCur) ); + assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE && !pBt->readOnly ); + assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); + + /* Assert that the caller has been consistent. If this cursor was opened + ** expecting an index b-tree, then the caller should be inserting blob + ** keys with no associated data. If the cursor was opened expecting an + ** intkey table, the caller should be inserting integer keys with a + ** blob of associated data. */ + assert( (pKey==0)==(pCur->pKeyInfo==0) ); + + /* If this is an insert into a table b-tree, invalidate any incrblob + ** cursors open on the row being replaced (assuming this is a replace + ** operation - if it is not, the following is a no-op). */ + if( pCur->pKeyInfo==0 ){ + invalidateIncrblobCursors(p, nKey, 0); + } + + /* Save the positions of any other cursors open on this table. + ** + ** In some cases, the call to btreeMoveto() below is a no-op. For + ** example, when inserting data into a table with auto-generated integer + ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the + ** integer key to use. It then calls this function to actually insert the + ** data into the intkey B-Tree. In this case btreeMoveto() recognizes + ** that the cursor is already where it needs to be and returns without + ** doing any work. To avoid thwarting these optimizations, it is important + ** not to clear the cursor here. + */ + rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); + if( rc ) return rc; + if( !loc ){ + rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc); + if( rc ) return rc; + } + assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) ); + + pPage = pCur->apPage[pCur->iPage]; + assert( pPage->intKey || nKey>=0 ); + assert( pPage->leaf || !pPage->intKey ); + + TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", + pCur->pgnoRoot, nKey, nData, pPage->pgno, + loc==0 ? "overwrite" : "new entry")); + assert( pPage->isInit ); + allocateTempSpace(pBt); + newCell = pBt->pTmpSpace; + if( newCell==0 ) return SQLITE_NOMEM; + rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew); + if( rc ) goto end_insert; + assert( szNew==cellSizePtr(pPage, newCell) ); + assert( szNew<=MX_CELL_SIZE(pBt) ); + idx = pCur->aiIdx[pCur->iPage]; + if( loc==0 ){ + u16 szOld; + assert( idxnCell ); + rc = sqlite3PagerWrite(pPage->pDbPage); + if( rc ){ + goto end_insert; + } + oldCell = findCell(pPage, idx); + if( !pPage->leaf ){ + memcpy(newCell, oldCell, 4); + } + szOld = cellSizePtr(pPage, oldCell); + rc = clearCell(pPage, oldCell); + dropCell(pPage, idx, szOld, &rc); + if( rc ) goto end_insert; + }else if( loc<0 && pPage->nCell>0 ){ + assert( pPage->leaf ); + idx = ++pCur->aiIdx[pCur->iPage]; + }else{ + assert( pPage->leaf ); + } + insertCell(pPage, idx, newCell, szNew, 0, 0, &rc); + assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 ); + + /* If no error has occured and pPage has an overflow cell, call balance() + ** to redistribute the cells within the tree. Since balance() may move + ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey + ** variables. + ** + ** Previous versions of SQLite called moveToRoot() to move the cursor + ** back to the root page as balance() used to invalidate the contents + ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that, + ** set the cursor state to "invalid". This makes common insert operations + ** slightly faster. + ** + ** There is a subtle but important optimization here too. When inserting + ** multiple records into an intkey b-tree using a single cursor (as can + ** happen while processing an "INSERT INTO ... SELECT" statement), it + ** is advantageous to leave the cursor pointing to the last entry in + ** the b-tree if possible. If the cursor is left pointing to the last + ** entry in the table, and the next row inserted has an integer key + ** larger than the largest existing key, it is possible to insert the + ** row without seeking the cursor. This can be a big performance boost. + */ + pCur->info.nSize = 0; + pCur->validNKey = 0; + if( rc==SQLITE_OK && pPage->nOverflow ){ + rc = balance(pCur); + + /* Must make sure nOverflow is reset to zero even if the balance() + ** fails. Internal data structure corruption will result otherwise. + ** Also, set the cursor state to invalid. This stops saveCursorPosition() + ** from trying to save the current position of the cursor. */ + pCur->apPage[pCur->iPage]->nOverflow = 0; + pCur->eState = CURSOR_INVALID; + } + assert( pCur->apPage[pCur->iPage]->nOverflow==0 ); + +end_insert: + return rc; +} + +void dumpCursor(BtCursor* c) { + int i; + printf(" Depth %d\n", c->iPage); + for(i=0; i<=c->iPage; i++) + printf(" Page %d Cell %d\n", c->apPage[i]->pgno, c->aiIdx[i]); + +} + +static int stackPush( int* stackBegin, int* stackEnd, int root ) { + // Push root onto the stack + stackBegin[0]++; + if (stackBegin + stackBegin[0] >= stackEnd) + return SQLITE_FULL; + stackBegin[stackBegin[0]] = root; + return SQLITE_OK; +} + +SQLITE_PRIVATE int sqlite3BtreeLazyDelete(BtCursor* cursor, int* stackBegin, int* stackEnd, int desiredPages, int* pagesDeleted) { + int pageNumber, cell, rc, subtree, count; + MemPage* page; + int empty; + const void* ptr; + i64 tableKey=0; + + *pagesDeleted = 0; + + while (desiredPages--) { + if (!stackBegin[0]) { + // Read one or more items from the back of cursor table into stack + rc = sqlite3BtreeLast(cursor, &empty); + if (rc) return rc; + if (empty) { + // Cursor table is empty + return SQLITE_OK; + } + + rc = sqlite3BtreeKeySize( cursor, &tableKey ); // actually returns the key, not the key size, in an intkey table! + if (rc) return rc; + + ptr = sqlite3BtreeDataFetch( cursor, &count ); + if (count != sizeof(int)) return SQLITE_CORRUPT_BKPT; + pageNumber = *(int*)ptr; + + rc = sqlite3BtreeDelete(cursor); + if (rc) return rc; + } else { + // Pop (height, pageNumber) from the stack + pageNumber = stackBegin[stackBegin[0]]; + stackBegin[0]--; + } + + // Read the item + rc = getAndInitPage(cursor->pBt, pageNumber, &page); + if (rc) return rc; + + // Put its child pages on the stack + if (!page->leaf) + for(cell = page->nCell; cell >= 0; --cell) { + if (cell == page->nCell) + subtree = get4byte( &page->aData[page->hdrOffset+8] ); + else + subtree = get4byte( findCell(page, cell) ); + + rc = stackPush( stackBegin, stackEnd, subtree ); + if (rc) { releasePage(page); return rc; } + } + + // Free overflow pages + for(cell=0; cellnCell; cell++) { + rc = clearCell(page, findCell(page, cell)); + if( rc ) { releasePage(page); return rc; } + } + + // Free it + rc = freePage2(cursor->pBt, page, pageNumber); + if (rc) { releasePage(page); return rc; } + + releasePage(page); // Required after getAndInitPage() above + ++(*pagesDeleted); + } + + if (stackBegin[0]) { + // Get tableKey if we haven't already + if (!tableKey) { + rc = sqlite3BtreeLast(cursor, &empty); + if (rc) return rc; + + if (empty) + tableKey = 1; + else { + rc = sqlite3BtreeKeySize( cursor, &tableKey ); + if (rc) return rc; + ++tableKey; // We aren't consuming this item so we mustn't overwrite it + } + } + + // If autovacuum is enabled, update the pointer map for the root pages we are putting back in the lazy freelist table + if( cursor->pBt ){ + for(count=0; countpBt, stackBegin[1+count], PTRMAP_LAZYFREE, 0, &rc); + if( rc ) return rc; + } + } + + // Write stack onto the back of the cursor table + for(count=0; countpDbPage); + if( rc ) return rc; + + //printf("deleteCellRange: Page %d, [%d,%d)\n", page->pgno, beginCell, endCell); + + // Cells [begin, end) and their (left) subtrees will be completely deleted + // We go backward because dropCell(c) changes cell numbers >c + for(cell = endCell-1; cell >= beginCell; --cell) { + pCell = findCell(page, cell); + if (!page->leaf) { + rc = stackPush( stackBegin, stackEnd, get4byte(pCell) ); + if (rc) return rc; + } + rc = clearCell(page, pCell); // free the overflow list + dropCell(page, cell, cellSizePtr(page, pCell), &rc); // free the actual cell + if( rc ) return rc; + } + return 0; +} + +void swapChildren( u8* c1, u8* c2 ) { + int subtree = get4byte(c1); + put4byte( c1, get4byte(c2) ); + put4byte( c2, subtree ); +} + +SQLITE_PRIVATE int sqlite3BtreeDeleteRange(BtCursor *begin, BtCursor *end, int* stackBegin, int* stackEnd) { + int level, cellBegin, cellEnd, rc; + MemPage *page; + BtCursor* modified = 0; + + /*printf("DeleteRange\n"); + printf("Begin:\n"); dumpCursor(begin); + printf("End:\n"); dumpCursor(end);*/ + + //if( pCur->pKeyInfo==0 ) invalidateIncrblobCursors(p, pCur->info.nKey, 0); + //rc = saveAllCursors(begin->pBt, begin->pgnoRoot, begin); + //if( rc ) return rc; + + assert( begin->pgnoRoot == end->pgnoRoot ); + + for(level=0; level<=begin->iPage || level<=end->iPage; level++) { + if (level<=begin->iPage && level<=end->iPage && begin->apPage[level] == end->apPage[level]) { + // begin and end are still on the same page at this level. If they are on the same cell, we don't have to do anything + if (begin->aiIdx[level] != end->aiIdx[level]) { + // Don't erase the begin element in an internal node (we would have to replace it with an element from its subtree) + //cellBegin = begin->aiIdx[level] + 1; + cellBegin = begin->aiIdx[level] + !begin->apPage[level]->leaf; + cellEnd = end->aiIdx[level] + (end->iPage==level); + if (cellBegin != cellEnd) { + rc = deleteCellRange( begin->apPage[level], cellBegin, cellEnd, stackBegin, stackEnd ); + if (rc) return rc; + modified = begin; + break; + } + } + } else { + if (level<=begin->iPage) { + // Erase all cells from [begin, infinity) + cellBegin = begin->aiIdx[level]; + cellEnd = begin->apPage[level]->nCell; + if (cellBegin != cellEnd) { + page = begin->apPage[level]; + if (!page->leaf) { + // The rightmost child pointer is located at offset 8 in the header of a non-leaf node. Swap it with the rightmost child that will remain after the erase. + swapChildren( findCell(page, cellBegin), &page->aData[page->hdrOffset+8] ); + } + rc = deleteCellRange( page, cellBegin, cellEnd, stackBegin, stackEnd ); + if (rc) return rc; + + modified = begin; + break; + } + } + if (level<=end->iPage) { + // Erase all cells from (-infinity, end] + cellBegin = 0; + cellEnd = end->aiIdx[level]+(end->iPage==level); + if (cellBegin != cellEnd) { + rc = deleteCellRange( end->apPage[level], cellBegin, cellEnd, stackBegin, stackEnd ); + if (rc) return rc; + modified = end; + break; + } + } + //assert(0); + } + } + + if (!modified) { + moveToRoot(begin); + moveToRoot(end); + return SQLITE_OK; + } else { + moveToRoot( modified==begin ? end : begin ); + while (modified->iPage > level) { + releasePage(modified->apPage[modified->iPage]); + --modified->iPage; + } + //printf("Balancing at page %d\n", modified->apPage[modified->iPage]->pgno); + rc = balance(modified); + if (rc) return rc; + moveToRoot(modified); + return 201; + } +} + + +/* +** Delete the entry that the cursor is pointing to. The cursor +** is left pointing at a arbitrary location. +*/ +SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){ + Btree *p = pCur->pBtree; + BtShared *pBt = p->pBt; + int rc; /* Return code */ + MemPage *pPage; /* Page to delete cell from */ + unsigned char *pCell; /* Pointer to cell to delete */ + int iCellIdx; /* Index of cell to delete */ + int iCellDepth; /* Depth of node containing pCell */ + + assert( cursorHoldsMutex(pCur) ); + assert( pBt->inTransaction==TRANS_WRITE ); + assert( !pBt->readOnly ); + assert( pCur->wrFlag ); + assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); + assert( !hasReadConflicts(p, pCur->pgnoRoot) ); + + if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell) + || NEVER(pCur->eState!=CURSOR_VALID) + ){ + return SQLITE_ERROR; /* Something has gone awry. */ + } + + /* If this is a delete operation to remove a row from a table b-tree, + ** invalidate any incrblob cursors open on the row being deleted. */ + if( pCur->pKeyInfo==0 ){ + invalidateIncrblobCursors(p, pCur->info.nKey, 0); + } + + iCellDepth = pCur->iPage; + iCellIdx = pCur->aiIdx[iCellDepth]; + pPage = pCur->apPage[iCellDepth]; + pCell = findCell(pPage, iCellIdx); + + /* If the page containing the entry to delete is not a leaf page, move + ** the cursor to the largest entry in the tree that is smaller than + ** the entry being deleted. This cell will replace the cell being deleted + ** from the internal node. The 'previous' entry is used for this instead + ** of the 'next' entry, as the previous entry is always a part of the + ** sub-tree headed by the child page of the cell being deleted. This makes + ** balancing the tree following the delete operation easier. */ + if( !pPage->leaf ){ + int notUsed; + rc = sqlite3BtreePrevious(pCur, ¬Used); + if( rc ) return rc; + } + + /* Save the positions of any other cursors open on this table before + ** making any modifications. Make the page containing the entry to be + ** deleted writable. Then free any overflow pages associated with the + ** entry and finally remove the cell itself from within the page. + */ + rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); + if( rc ) return rc; + rc = sqlite3PagerWrite(pPage->pDbPage); + if( rc ) return rc; + rc = clearCell(pPage, pCell); + dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc); + if( rc ) return rc; + + /* If the cell deleted was not located on a leaf page, then the cursor + ** is currently pointing to the largest entry in the sub-tree headed + ** by the child-page of the cell that was just deleted from an internal + ** node. The cell from the leaf node needs to be moved to the internal + ** node to replace the deleted cell. */ + if( !pPage->leaf ){ + MemPage *pLeaf = pCur->apPage[pCur->iPage]; + int nCell; + Pgno n = pCur->apPage[iCellDepth+1]->pgno; + unsigned char *pTmp; + + pCell = findCell(pLeaf, pLeaf->nCell-1); + nCell = cellSizePtr(pLeaf, pCell); + assert( MX_CELL_SIZE(pBt)>=nCell ); + + allocateTempSpace(pBt); + pTmp = pBt->pTmpSpace; + + rc = sqlite3PagerWrite(pLeaf->pDbPage); + insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); + dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc); + if( rc ) return rc; + } + + /* Balance the tree. If the entry deleted was located on a leaf page, + ** then the cursor still points to that page. In this case the first + ** call to balance() repairs the tree, and the if(...) condition is + ** never true. + ** + ** Otherwise, if the entry deleted was on an internal node page, then + ** pCur is pointing to the leaf page from which a cell was removed to + ** replace the cell deleted from the internal node. This is slightly + ** tricky as the leaf node may be underfull, and the internal node may + ** be either under or overfull. In this case run the balancing algorithm + ** on the leaf node first. If the balance proceeds far enough up the + ** tree that we can be sure that any problem in the internal node has + ** been corrected, so be it. Otherwise, after balancing the leaf node, + ** walk the cursor up the tree to the internal node and balance it as + ** well. */ + rc = balance(pCur); + if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){ + while( pCur->iPage>iCellDepth ){ + releasePage(pCur->apPage[pCur->iPage--]); + } + rc = balance(pCur); + } + + if( rc==SQLITE_OK ){ + moveToRoot(pCur); + } + return rc; +} + +/* +** Create a new BTree table. Write into *piTable the page +** number for the root page of the new table. +** +** The type of type is determined by the flags parameter. Only the +** following values of flags are currently in use. Other values for +** flags might not work: +** +** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys +** BTREE_ZERODATA Used for SQL indices +*/ +static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ + BtShared *pBt = p->pBt; + MemPage *pRoot; + Pgno pgnoRoot; + int rc; + int ptfFlags; /* Page-type flage for the root page of new table */ + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( pBt->inTransaction==TRANS_WRITE ); + assert( !pBt->readOnly ); + +#ifdef SQLITE_OMIT_AUTOVACUUM + rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); + if( rc ){ + return rc; + } +#else + if( pBt->autoVacuum ){ + Pgno pgnoMove; /* Move a page here to make room for the root-page */ + MemPage *pPageMove; /* The page to move to. */ + + /* Creating a new table may probably require moving an existing database + ** to make room for the new tables root page. In case this page turns + ** out to be an overflow page, delete all overflow page-map caches + ** held by open cursors. + */ + invalidateAllOverflowCache(pBt); + + /* Read the value of meta[3] from the database to determine where the + ** root page of the new table should go. meta[3] is the largest root-page + ** created so far, so the new root-page is (meta[3]+1). + */ + sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot); + pgnoRoot++; + + /* The new root-page may not be allocated on a pointer-map page, or the + ** PENDING_BYTE page. + */ + while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || + pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ + pgnoRoot++; + } + assert( pgnoRoot>=3 ); + + /* Allocate a page. The page that currently resides at pgnoRoot will + ** be moved to the allocated page (unless the allocated page happens + ** to reside at pgnoRoot). + */ + rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1); + if( rc!=SQLITE_OK ){ + return rc; + } + + if( pgnoMove!=pgnoRoot ){ + /* pgnoRoot is the page that will be used for the root-page of + ** the new table (assuming an error did not occur). But we were + ** allocated pgnoMove. If required (i.e. if it was not allocated + ** by extending the file), the current page at position pgnoMove + ** is already journaled. + */ + u8 eType = 0; + Pgno iPtrPage = 0; + + releasePage(pPageMove); + + /* Move the page currently at pgnoRoot to pgnoMove. */ + rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); + if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE || eType==PTRMAP_FREELEAF ){ + rc = SQLITE_CORRUPT_BKPT; + } + if( rc!=SQLITE_OK ){ + releasePage(pRoot); + return rc; + } + assert( eType!=PTRMAP_ROOTPAGE ); + assert( eType!=PTRMAP_FREEPAGE ); + assert( eType!=PTRMAP_FREELEAF ); + rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); + releasePage(pRoot); + + /* Obtain the page at pgnoRoot */ + if( rc!=SQLITE_OK ){ + return rc; + } + rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + rc = sqlite3PagerWrite(pRoot->pDbPage); + if( rc!=SQLITE_OK ){ + releasePage(pRoot); + return rc; + } + }else{ + pRoot = pPageMove; + } + + /* Update the pointer-map and meta-data with the new root-page number. */ + ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc); + if( rc ){ + releasePage(pRoot); + return rc; + } + + /* When the new root page was allocated, page 1 was made writable in + ** order either to increase the database filesize, or to decrement the + ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail. + */ + assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) ); + rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); + if( NEVER(rc) ){ + releasePage(pRoot); + return rc; + } + + }else{ + rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); + if( rc ) return rc; + } +#endif + assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); + if( createTabFlags & BTREE_INTKEY ){ + ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF; + }else{ + ptfFlags = PTF_ZERODATA | PTF_LEAF; + } + zeroPage(pRoot, ptfFlags); + sqlite3PagerUnref(pRoot->pDbPage); + assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 ); + *piTable = (int)pgnoRoot; + return SQLITE_OK; +} +SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ + int rc; + sqlite3BtreeEnter(p); + rc = btreeCreateTable(p, piTable, flags); + sqlite3BtreeLeave(p); + return rc; +} + +/* +** Erase the given database page and all its children. Return +** the page to the freelist. +*/ +static int clearDatabasePage( + BtShared *pBt, /* The BTree that contains the table */ + Pgno pgno, /* Page number to clear */ + int freePageFlag, /* Deallocate page if true */ + int *pnChange /* Add number of Cells freed to this counter */ +){ + MemPage *pPage; + int rc; + unsigned char *pCell; + int i; + + assert( sqlite3_mutex_held(pBt->mutex) ); + if( pgno>btreePagecount(pBt) ){ + return SQLITE_CORRUPT_BKPT; + } + + rc = getAndInitPage(pBt, pgno, &pPage); + if( rc ) return rc; + for(i=0; inCell; i++){ + pCell = findCell(pPage, i); + if( !pPage->leaf ){ + rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); + if( rc ) goto cleardatabasepage_out; + } + rc = clearCell(pPage, pCell); + if( rc ) goto cleardatabasepage_out; + } + if( !pPage->leaf ){ + rc = clearDatabasePage(pBt, get4byte(&pPage->aData[8]), 1, pnChange); + if( rc ) goto cleardatabasepage_out; + }else if( pnChange ){ + assert( pPage->intKey ); + *pnChange += pPage->nCell; + } + if( freePageFlag ){ + freePage(pPage, &rc); + }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ + zeroPage(pPage, pPage->aData[0] | PTF_LEAF); + } + +cleardatabasepage_out: + releasePage(pPage); + return rc; +} + +/* +** Delete all information from a single table in the database. iTable is +** the page number of the root of the table. After this routine returns, +** the root page is empty, but still exists. +** +** This routine will fail with SQLITE_LOCKED if there are any open +** read cursors on the table. Open write cursors are moved to the +** root of the table. +** +** If pnChange is not NULL, then table iTable must be an intkey table. The +** integer value pointed to by pnChange is incremented by the number of +** entries in the table. +*/ +SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ + int rc; + BtShared *pBt = p->pBt; + sqlite3BtreeEnter(p); + assert( p->inTrans==TRANS_WRITE ); + + /* Invalidate all incrblob cursors open on table iTable (assuming iTable + ** is the root of a table b-tree - if it is not, the following call is + ** a no-op). */ + invalidateIncrblobCursors(p, 0, 1); + + rc = saveAllCursors(pBt, (Pgno)iTable, 0); + if( SQLITE_OK==rc ){ + rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); + } + sqlite3BtreeLeave(p); + return rc; +} + +/* +** Erase all information in a table and add the root of the table to +** the freelist. Except, the root of the principle table (the one on +** page 1) is never added to the freelist. +** +** This routine will fail with SQLITE_LOCKED if there are any open +** cursors on the table. +** +** If AUTOVACUUM is enabled and the page at iTable is not the last +** root page in the database file, then the last root page +** in the database file is moved into the slot formerly occupied by +** iTable and that last slot formerly occupied by the last root page +** is added to the freelist instead of iTable. In this say, all +** root pages are kept at the beginning of the database file, which +** is necessary for AUTOVACUUM to work right. *piMoved is set to the +** page number that used to be the last root page in the file before +** the move. If no page gets moved, *piMoved is set to 0. +** The last root page is recorded in meta[3] and the value of +** meta[3] is updated by this procedure. +*/ +static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ + int rc; + MemPage *pPage = 0; + BtShared *pBt = p->pBt; + + assert( sqlite3BtreeHoldsMutex(p) ); + assert( p->inTrans==TRANS_WRITE ); + + /* It is illegal to drop a table if any cursors are open on the + ** database. This is because in auto-vacuum mode the backend may + ** need to move another root-page to fill a gap left by the deleted + ** root page. If an open cursor was using this page a problem would + ** occur. + ** + ** This error is caught long before control reaches this point. + */ + if( NEVER(pBt->pCursor) ){ + sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db); + return SQLITE_LOCKED_SHAREDCACHE; + } + + rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0); + if( rc ) return rc; + rc = sqlite3BtreeClearTable(p, iTable, 0); + if( rc ){ + releasePage(pPage); + return rc; + } + + *piMoved = 0; + + if( iTable>1 ){ +#ifdef SQLITE_OMIT_AUTOVACUUM + freePage(pPage, &rc); + releasePage(pPage); +#else + if( pBt->autoVacuum ){ + Pgno maxRootPgno; + sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno); + + if( iTable==maxRootPgno ){ + /* If the table being dropped is the table with the largest root-page + ** number in the database, put the root page on the free list. + */ + freePage(pPage, &rc); + releasePage(pPage); + if( rc!=SQLITE_OK ){ + return rc; + } + }else{ + /* The table being dropped does not have the largest root-page + ** number in the database. So move the page that does into the + ** gap left by the deleted root-page. + */ + MemPage *pMove; + releasePage(pPage); + rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); + releasePage(pMove); + if( rc!=SQLITE_OK ){ + return rc; + } + pMove = 0; + rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); + freePage(pMove, &rc); + releasePage(pMove); + if( rc!=SQLITE_OK ){ + return rc; + } + *piMoved = maxRootPgno; + } + + /* Set the new 'max-root-page' value in the database header. This + ** is the old value less one, less one more if that happens to + ** be a root-page number, less one again if that is the + ** PENDING_BYTE_PAGE. + */ + maxRootPgno--; + while( maxRootPgno==PENDING_BYTE_PAGE(pBt) + || PTRMAP_ISPAGE(pBt, maxRootPgno) ){ + maxRootPgno--; + } + assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); + + rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); + }else{ + freePage(pPage, &rc); + releasePage(pPage); + } +#endif + }else{ + /* If sqlite3BtreeDropTable was called on page 1. + ** This really never should happen except in a corrupt + ** database. + */ + zeroPage(pPage, PTF_INTKEY|PTF_LEAF ); + releasePage(pPage); + } + return rc; +} +SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ + int rc; + sqlite3BtreeEnter(p); + rc = btreeDropTable(p, iTable, piMoved); + sqlite3BtreeLeave(p); + return rc; +} + + +/* +** This function may only be called if the b-tree connection already +** has a read or write transaction open on the database. +** +** Read the meta-information out of a database file. Meta[0] +** is the number of free pages currently in the database. Meta[1] +** through meta[15] are available for use by higher layers. Meta[0] +** is read-only, the others are read/write. +** +** The schema layer numbers meta values differently. At the schema +** layer (and the SetCookie and ReadCookie opcodes) the number of +** free pages is not visible. So Cookie[0] is the same as Meta[1]. +*/ +SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ + BtShared *pBt = p->pBt; + + sqlite3BtreeEnter(p); + assert( p->inTrans>TRANS_NONE ); + assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) ); + assert( pBt->pPage1 ); + assert( idx>=0 && idx<=15 ); + + *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]); + + /* If auto-vacuum is disabled in this build and this is an auto-vacuum + ** database, mark the database as read-only. */ +#ifdef SQLITE_OMIT_AUTOVACUUM + if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ) pBt->readOnly = 1; +#endif + + sqlite3BtreeLeave(p); +} + +/* +** Write meta-information back into the database. Meta[0] is +** read-only and may not be written. +*/ +SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ + BtShared *pBt = p->pBt; + unsigned char *pP1; + int rc; + assert( idx>=1 && idx<=15 ); + sqlite3BtreeEnter(p); + assert( p->inTrans==TRANS_WRITE ); + assert( pBt->pPage1!=0 ); + pP1 = pBt->pPage1->aData; + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + if( rc==SQLITE_OK ){ + put4byte(&pP1[36 + idx*4], iMeta); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( idx==BTREE_INCR_VACUUM ){ + assert( pBt->autoVacuum || iMeta==0 ); + assert( iMeta==0 || iMeta==1 ); + pBt->incrVacuum = (u8)iMeta; + } +#endif + } + sqlite3BtreeLeave(p); + return rc; +} + +#ifndef SQLITE_OMIT_BTREECOUNT +/* +** The first argument, pCur, is a cursor opened on some b-tree. Count the +** number of entries in the b-tree and write the result to *pnEntry. +** +** SQLITE_OK is returned if the operation is successfully executed. +** Otherwise, if an error is encountered (i.e. an IO error or database +** corruption) an SQLite error code is returned. +*/ +SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ + i64 nEntry = 0; /* Value to return in *pnEntry */ + int rc; /* Return code */ + rc = moveToRoot(pCur); + + /* Unless an error occurs, the following loop runs one iteration for each + ** page in the B-Tree structure (not including overflow pages). + */ + while( rc==SQLITE_OK ){ + int iIdx; /* Index of child node in parent */ + MemPage *pPage; /* Current page of the b-tree */ + + /* If this is a leaf page or the tree is not an int-key tree, then + ** this page contains countable entries. Increment the entry counter + ** accordingly. + */ + pPage = pCur->apPage[pCur->iPage]; + if( pPage->leaf || !pPage->intKey ){ + nEntry += pPage->nCell; + } + + /* pPage is a leaf node. This loop navigates the cursor so that it + ** points to the first interior cell that it points to the parent of + ** the next page in the tree that has not yet been visited. The + ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell + ** of the page, or to the number of cells in the page if the next page + ** to visit is the right-child of its parent. + ** + ** If all pages in the tree have been visited, return SQLITE_OK to the + ** caller. + */ + if( pPage->leaf ){ + do { + if( pCur->iPage==0 ){ + /* All pages of the b-tree have been visited. Return successfully. */ + *pnEntry = nEntry; + return SQLITE_OK; + } + moveToParent(pCur); + }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell ); + + pCur->aiIdx[pCur->iPage]++; + pPage = pCur->apPage[pCur->iPage]; + } + + /* Descend to the child node of the cell that the cursor currently + ** points at. This is the right-child if (iIdx==pPage->nCell). + */ + iIdx = pCur->aiIdx[pCur->iPage]; + if( iIdx==pPage->nCell ){ + rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); + }else{ + rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx))); + } + } + + /* An error has occurred. Return an error code. */ + return rc; +} +#endif + +/* +** Return the pager associated with a BTree. This routine is used for +** testing and debugging only. +*/ +SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){ + return p->pBt->pPager; +} + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK +/* +** Append a message to the error message string. +*/ +static void checkAppendMsg( + IntegrityCk *pCheck, + char *zMsg1, + const char *zFormat, + ... +){ + va_list ap; + if( !pCheck->mxErr ) return; + pCheck->mxErr--; + pCheck->nErr++; + va_start(ap, zFormat); + if( pCheck->errMsg.nChar ){ + sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); + } + if( zMsg1 ){ + sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1); + } + sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap); + va_end(ap); + if( pCheck->errMsg.mallocFailed ){ + pCheck->mallocFailed = 1; + } +} +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK +/* +** Add 1 to the reference count for page iPage. If this is the second +** reference to the page, add an error message to pCheck->zErrMsg. +** Return 1 if there are 2 ore more references to the page and 0 if +** if this is the first reference to the page. +** +** Also check that the page number is in bounds. +*/ +static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){ + if( iPage==0 ) return 1; + if( iPage>pCheck->nPage ){ + checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage); + return 1; + } + if( pCheck->anRef[iPage]==1 ){ + checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage); + return 1; + } + return (pCheck->anRef[iPage]++)>1; +} + +#ifndef SQLITE_OMIT_AUTOVACUUM +/* +** Check that the entry in the pointer-map for page iChild maps to +** page iParent, pointer type ptrType. If not, append an error message +** to pCheck. +*/ +static void checkPtrmap( + IntegrityCk *pCheck, /* Integrity check context */ + Pgno iChild, /* Child page number */ + u8 eType, /* Expected pointer map type */ + Pgno iParent, /* Expected pointer map parent page number */ + char *zContext /* Context description (used for error msg) */ +){ + int rc; + u8 ePtrmapType; + Pgno iPtrmapParent; + + rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1; + checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild); + return; + } + + if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ + checkAppendMsg(pCheck, zContext, + "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", + iChild, eType, iParent, ePtrmapType, iPtrmapParent); + } +} +#endif + +/* +** Check the integrity of the freelist or of an overflow page list. +** Verify that the number of pages on the list is N. +*/ +static void checkList( + IntegrityCk *pCheck, /* Integrity checking context */ + int isFreeList, /* True for a freelist. False for overflow page list */ + int iPage, /* Page number for first page in the list */ + int N, /* Expected number of pages in the list */ + char *zContext /* Context for error messages */ +){ + int i; + int expected = N; + int iFirst = iPage; + int prevPage = 0; + while( N > 0 && pCheck->mxErr ){ + N--; // this trunk page is free + DbPage *pOvflPage; + unsigned char *pOvflData; + if( iPage<1 ){ + checkAppendMsg(pCheck, zContext, + "%d of %d pages missing from overflow list starting at %d", + N+1, expected, iFirst); + break; + } + if( checkRef(pCheck, iPage, zContext) ) break; + if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){ + checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage); + break; + } + pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); + if( isFreeList ){ + int n = get4byte(&pOvflData[4]); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pCheck->pBt->autoVacuum && g_expect_full_pointermap ){ + checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, prevPage, zContext); + } +#endif + if( n>(int)pCheck->pBt->usableSize/4-2 ){ + checkAppendMsg(pCheck, zContext, + "freelist leaf count too big on page %d", iPage); + }else{ + for(i=0; inPage) { +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pCheck->pBt->autoVacuum && g_expect_full_pointermap ){ + checkPtrmap(pCheck, iFreePage, PTRMAP_FREELEAF, 0, zContext); + } +#endif + checkRef(pCheck, iFreePage, zContext); + N--; // this leaf page is free + } + } + } + } +#ifndef SQLITE_OMIT_AUTOVACUUM + else{ + /* If this database supports auto-vacuum and iPage is not the last + ** page in this overflow list, check that the pointer-map entry for + ** the following page matches iPage. + */ + if( pCheck->pBt->autoVacuum && N>0 ){ + i = get4byte(pOvflData); + checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext); + } + } +#endif + prevPage = iPage; + iPage = get4byte(pOvflData); + sqlite3PagerUnref(pOvflPage); + } + if (iPage>=1) + checkAppendMsg(pCheck, zContext, "extra trunk pages on freelist"); + if (N < 0) + checkAppendMsg(pCheck, zContext, "too many pages found on freelist"); +} +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK +/* +** Do various sanity checks on a single page of a tree. Return +** the tree depth. Root pages return 0. Parents of root pages +** return 1, and so forth. +** +** These checks are done: +** +** 1. Make sure that cells and freeblocks do not overlap +** but combine to completely cover the page. +** NO 2. Make sure cell keys are in order. +** NO 3. Make sure no key is less than or equal to zLowerBound. +** NO 4. Make sure no key is greater than or equal to zUpperBound. +** 5. Check the integrity of overflow pages. +** 6. Recursively call checkTreePage on all children. +** 7. Verify that the depth of all children is the same. +** 8. Make sure this page is at least 33% full or else it is +** the root of the tree. +*/ +static int checkTreePage( + IntegrityCk *pCheck, /* Context for the sanity check */ + int iPage, /* Page number of the page to check */ + char *zParentContext, /* Parent context */ + i64 *pnParentMinKey, + i64 *pnParentMaxKey, + int verbose, + int isRoot, + int iRootPage +){ + MemPage *pPage; + int i, rc, depth, d2, pgno, cnt; + int hdr, cellStart; + int nCell; + u8 *data; + BtShared *pBt; + int usableSize; + char zContext[100]; + char *hit = 0; + i64 nMinKey = 0; + i64 nMaxKey = 0; + + sqlite3_snprintf(sizeof(zContext), zContext, "Page %d of tree %d: ", iPage, iRootPage); + //if (verbose) printf("Page %d\n", iPage); + + /* Check that the page exists + */ + pBt = pCheck->pBt; + usableSize = pBt->usableSize; + if( iPage==0 ) return 0; + if( checkRef(pCheck, iPage, zParentContext) ) return 0; + if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ + checkAppendMsg(pCheck, zContext, + "unable to get the page. error code=%d", rc); + return 0; + } + + /* Clear MemPage.isInit to make sure the corruption detection code in + ** btreeInitPage() is executed. */ + pPage->isInit = 0; + if( (rc = btreeInitPage(pPage))!=0 ){ + assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ + checkAppendMsg(pCheck, zContext, + "btreeInitPage() returns error code %d", rc); + releasePage(pPage); + return 0; + } + + /* Check that the page is balanced unless it is the root */ + // FIXME: This check fails for us, probably it is a (performance) bug! + /*if (!isRoot && + pPage->nFree > pCheck->pBt->usableSize * 2 / 3) { + checkAppendMsg(pCheck, zContext, + "Page underfull (%d/%d free)", pPage->nFree, pCheck->pBt->usableSize); + }*/ + //pPage->nOverflow==0 && pPage->nFree<=nMin + + /* Check out all the cells. + */ + depth = 0; + for(i=0; inCell && pCheck->mxErr; i++){ + u8 *pCell; + u32 sz; + CellInfo info; + + /* Check payload overflow pages + */ + sqlite3_snprintf(sizeof(zContext), zContext, + "On tree page %d cell %d: ", iPage, i); + pCell = findCell(pPage,i); + btreeParseCellPtr(pPage, pCell, &info); + sz = info.nData; + if( !pPage->intKey ) sz += (int)info.nKey; + /* For intKey pages, check that the keys are in order. + */ + else if( i==0 ) nMinKey = nMaxKey = info.nKey; + else{ + if( info.nKey <= nMaxKey ){ + checkAppendMsg(pCheck, zContext, + "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey); + } + nMaxKey = info.nKey; + } + assert( sz==info.nPayload ); + if( (sz>info.nLocal) + && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize]) + ){ + int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4); + Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->autoVacuum ){ + checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext); + } +#endif + checkList(pCheck, 0, pgnoOvfl, nPage, zContext); + } + + /* Check sanity of left child page. + */ + if( !pPage->leaf ){ + pgno = get4byte(pCell); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->autoVacuum ){ + checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); + } +#endif + d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey, verbose, 0, iRootPage); + if( i>0 && d2!=depth ){ + checkAppendMsg(pCheck, zContext, "Child page depth differs"); + } + depth = d2; + } + if (verbose) { + for(d2=0; d2leaf ){ + pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); + sqlite3_snprintf(sizeof(zContext), zContext, + "On page %d at right child: ", iPage); +#ifndef SQLITE_OMIT_AUTOVACUUM + if( pBt->autoVacuum ){ + checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext); + } +#endif + checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey, verbose, 0, iRootPage); + } + + /* For intKey leaf pages, check that the min/max keys are in order + ** with any left/parent/right pages. + */ + if( pPage->leaf && pPage->intKey ){ + /* if we are a left child page */ + if( pnParentMinKey ){ + /* if we are the left most child page */ + if( !pnParentMaxKey ){ + if( nMaxKey > *pnParentMinKey ){ + checkAppendMsg(pCheck, zContext, + "Rowid %lld out of order (max larger than parent min of %lld)", + nMaxKey, *pnParentMinKey); + } + }else{ + if( nMinKey <= *pnParentMinKey ){ + checkAppendMsg(pCheck, zContext, + "Rowid %lld out of order (min less than parent min of %lld)", + nMinKey, *pnParentMinKey); + } + if( nMaxKey > *pnParentMaxKey ){ + checkAppendMsg(pCheck, zContext, + "Rowid %lld out of order (max larger than parent max of %lld)", + nMaxKey, *pnParentMaxKey); + } + *pnParentMinKey = nMaxKey; + } + /* else if we're a right child page */ + } else if( pnParentMaxKey ){ + if( nMinKey <= *pnParentMaxKey ){ + checkAppendMsg(pCheck, zContext, + "Rowid %lld out of order (min less than parent max of %lld)", + nMinKey, *pnParentMaxKey); + } + } + } + + /* Check for complete coverage of the page + */ + data = pPage->aData; + hdr = pPage->hdrOffset; + hit = sqlite3PageMalloc( pBt->pageSize ); + if( hit==0 ){ + pCheck->mallocFailed = 1; + }else{ + int contentOffset = get2byteNotZero(&data[hdr+5]); + assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ + memset(hit+contentOffset, 0, usableSize-contentOffset); + memset(hit, 1, contentOffset); + nCell = get2byte(&data[hdr+3]); + cellStart = hdr + 12 - 4*pPage->leaf; + for(i=0; i=usableSize ){ + checkAppendMsg(pCheck, 0, + "Corruption detected in cell %d on page %d",i,iPage); + }else{ + for(j=pc+size-1; j>=pc; j--) hit[j]++; + } + } + i = get2byte(&data[hdr+1]); + while( i>0 ){ + int size, j; + assert( i<=usableSize-4 ); /* Enforced by btreeInitPage() */ + size = get2byte(&data[i+2]); + assert( i+size<=usableSize ); /* Enforced by btreeInitPage() */ + for(j=i+size-1; j>=i; j--) hit[j]++; + j = get2byte(&data[i]); + assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ + assert( j<=usableSize-4 ); /* Enforced by btreeInitPage() */ + i = j; + } + for(i=cnt=0; i1 ){ + checkAppendMsg(pCheck, 0, + "Multiple uses for byte %d of page %d", i, iPage); + break; + } + } + if( cnt!=data[hdr+7] ){ + checkAppendMsg(pCheck, 0, + "Fragmentation of %d bytes reported as %d on page %d", + cnt, data[hdr+7], iPage); + } + } + sqlite3PageFree(hit); + releasePage(pPage); + return depth+1; +} +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK +static int checkLazyDeleteTable(Btree* bt, IntegrityCk *ck, int tableRoot) { + BtCursor cursor; + int empty; + int rc; + i64 tableKey; + int count; + const void* ptr; + int pageNumber; + + sqlite3BtreeCursorZero(&cursor); + + if ((rc=sqlite3BtreeCursor(bt, tableRoot, 0, NULL, &cursor))) { + checkAppendMsg(ck, 0, "Unable to open cursor for lazy delete table check: %d", rc); + return rc; + } + + if ((rc=sqlite3BtreeFirst(&cursor, &empty))) { + checkAppendMsg(ck, 0, "sqlite3BtreeFirst: %d", rc); + sqlite3BtreeCloseCursor(&cursor); + return rc; + } + + while (!empty) { + rc = sqlite3BtreeKeySize( &cursor, &tableKey ); // actually returns the key, not the key size, in an intkey table! + if (rc) { + checkAppendMsg(ck, 0, "sqlite3BtreeKeySize: %d", rc); + sqlite3BtreeCloseCursor(&cursor); + return rc; + } + + ptr = sqlite3BtreeDataFetch( &cursor, &count ); + if (count != sizeof(int)) return SQLITE_CORRUPT_BKPT; + pageNumber = *(int*)ptr; + + checkPtrmap(ck, pageNumber, PTRMAP_LAZYFREE, 0, "lazy delete"); + checkTreePage(ck, pageNumber, "lazily deleted", NULL, NULL, 0, 0, pageNumber); + + if ((rc=sqlite3BtreeNext(&cursor, &empty))) { + checkAppendMsg(ck, 0, "sqlite3BtreeNext: %d", rc); + sqlite3BtreeCloseCursor(&cursor); + return rc; + } + } + + sqlite3BtreeCloseCursor(&cursor); + return 0; +} +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK +/* +** This routine does a complete check of the given BTree file. aRoot[] is +** an array of pages numbers were each page number is the root page of +** a table. nRoot is the number of entries in aRoot. +** +** A read-only or read-write transaction must be opened before calling +** this function. +** +** Write the number of error seen in *pnErr. Except for some memory +** allocation errors, an error message held in memory obtained from +** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is +** returned. If a memory allocation error occurs, NULL is returned. +*/ +SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck( + Btree *p, /* The btree to be checked */ + int *aRoot, /* An array of root pages numbers for individual trees */ + int nRoot, /* Number of entries in aRoot[] */ + int mxErr, /* Stop reporting errors after this many */ + int *pnErr, /* Write number of errors seen to this variable */ + int verbose /* Nonzero to print entire tree */ +){ + Pgno i; + int nRef; + IntegrityCk sCheck; + BtShared *pBt = p->pBt; + char zErr[100]; + u8 eType; + Pgno iPtrPage; + + + sqlite3BtreeEnter(p); + assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); + nRef = sqlite3PagerRefcount(pBt->pPager); + sCheck.pBt = pBt; + sCheck.pPager = pBt->pPager; + sCheck.nPage = btreePagecount(sCheck.pBt); + sCheck.mxErr = mxErr; + sCheck.nErr = 0; + sCheck.mallocFailed = 0; + *pnErr = 0; + if( sCheck.nPage==0 ){ + sqlite3BtreeLeave(p); + return 0; + } + sCheck.anRef = sqlite3Malloc( (sCheck.nPage+1)*sizeof(sCheck.anRef[0]) ); + if( !sCheck.anRef ){ + *pnErr = 1; + sqlite3BtreeLeave(p); + return 0; + } + for(i=0; i<=sCheck.nPage; i++){ sCheck.anRef[i] = 0; } + i = PENDING_BYTE_PAGE(pBt); + if( i<=sCheck.nPage ){ + sCheck.anRef[i] = 1; + } + sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), 20000); + sCheck.errMsg.useMalloc = 2; + + /* Check the integrity of the freelist + */ + checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), + get4byte(&pBt->pPage1->aData[36]), "Main freelist: "); + + /* Check all the tables. + */ + for(i=0; (int)iautoVacuum && aRoot[i]>1 ){ + checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0); + } +#endif + checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL, verbose, 1, aRoot[i]); + } + + /* Check the lazy delete freetable + */ + checkLazyDeleteTable(p, &sCheck, aRoot[nRoot-1]); + + /* Make sure every page in the file is referenced + */ + for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ +#ifdef SQLITE_OMIT_AUTOVACUUM + if( sCheck.anRef[i]==0 ){ + checkAppendMsg(&sCheck, 0, "Page %d is never used", i); + } +#else + /* If the database supports auto-vacuum, make sure no tables contain + ** references to pointer-map pages. + */ + if( sCheck.anRef[i]==0 && + (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ + if (ptrmapGet(pBt, i, &eType, &iPtrPage)) + checkAppendMsg(&sCheck, 0, "Page %d unused, no ptrmap", i); + else + checkAppendMsg(&sCheck, 0, "Page %d unused, type %d ptr %d", i, eType, iPtrPage); + } + if( sCheck.anRef[i]!=0 && + (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ + checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i); + } +#endif + } + + /* Make sure this analysis did not leave any unref() pages. + ** This is an internal consistency check; an integrity check + ** of the integrity check. + */ + if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){ + checkAppendMsg(&sCheck, 0, + "Outstanding page count goes from %d to %d during this analysis", + nRef, sqlite3PagerRefcount(pBt->pPager) + ); + } + + /* Clean up and report errors. + */ + sqlite3BtreeLeave(p); + sqlite3_free(sCheck.anRef); + if( sCheck.mallocFailed ){ + sqlite3StrAccumReset(&sCheck.errMsg); + *pnErr = sCheck.nErr+1; + return 0; + } + *pnErr = sCheck.nErr; + if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); + return sqlite3StrAccumFinish(&sCheck.errMsg); +} +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +/* +** Return the full pathname of the underlying database file. +** +** The pager filename is invariant as long as the pager is +** open so it is safe to access without the BtShared mutex. +*/ +SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){ + assert( p->pBt->pPager!=0 ); + return sqlite3PagerFilename(p->pBt->pPager); +} + +/* +** Return the pathname of the journal file for this database. The return +** value of this routine is the same regardless of whether the journal file +** has been created or not. +** +** The pager journal filename is invariant as long as the pager is +** open so it is safe to access without the BtShared mutex. +*/ +SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){ + assert( p->pBt->pPager!=0 ); + return sqlite3PagerJournalname(p->pBt->pPager); +} + +/* +** Return non-zero if a transaction is active. +*/ +SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){ + assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); + return (p && (p->inTrans==TRANS_WRITE)); +} + +#ifndef SQLITE_OMIT_WAL +/* +** Run a checkpoint on the Btree passed as the first argument. +** +** Return SQLITE_LOCKED if this or any other connection has an open +** transaction on the shared-cache the argument Btree is connected to. +** +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. +*/ +SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){ + int rc = SQLITE_OK; + if( p ){ + BtShared *pBt = p->pBt; + sqlite3BtreeEnter(p); + if( pBt->inTransaction!=TRANS_NONE ){ + rc = SQLITE_LOCKED; + }else{ + rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt); + } + sqlite3BtreeLeave(p); + } + return rc; +} +#endif + +/* +** Return non-zero if a read (or write) transaction is active. +*/ +SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){ + assert( p ); + assert( sqlite3_mutex_held(p->db->mutex) ); + return p->inTrans!=TRANS_NONE; +} + +SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){ + assert( p ); + assert( sqlite3_mutex_held(p->db->mutex) ); + return p->nBackup!=0; +} + +/* +** This function returns a pointer to a blob of memory associated with +** a single shared-btree. The memory is used by client code for its own +** purposes (for example, to store a high-level schema associated with +** the shared-btree). The btree layer manages reference counting issues. +** +** The first time this is called on a shared-btree, nBytes bytes of memory +** are allocated, zeroed, and returned to the caller. For each subsequent +** call the nBytes parameter is ignored and a pointer to the same blob +** of memory returned. +** +** If the nBytes parameter is 0 and the blob of memory has not yet been +** allocated, a null pointer is returned. If the blob has already been +** allocated, it is returned as normal. +** +** Just before the shared-btree is closed, the function passed as the +** xFree argument when the memory allocation was made is invoked on the +** blob of allocated memory. This function should not call sqlite3_free() +** on the memory, the btree layer does that. +*/ +SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ + BtShared *pBt = p->pBt; + sqlite3BtreeEnter(p); + if( !pBt->pSchema && nBytes ){ + pBt->pSchema = sqlite3DbMallocZero(0, nBytes); + pBt->xFreeSchema = xFree; + } + sqlite3BtreeLeave(p); + return pBt->pSchema; +} + +/* +** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared +** btree as the argument handle holds an exclusive lock on the +** sqlite_master table. Otherwise SQLITE_OK. +*/ +SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){ + int rc; + assert( sqlite3_mutex_held(p->db->mutex) ); + sqlite3BtreeEnter(p); + rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); + assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE ); + sqlite3BtreeLeave(p); + return rc; +} + + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** Obtain a lock on the table whose root page is iTab. The +** lock is a write lock if isWritelock is true or a read lock +** if it is false. +*/ +SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ + int rc = SQLITE_OK; + assert( p->inTrans!=TRANS_NONE ); + if( p->sharable ){ + u8 lockType = READ_LOCK + isWriteLock; + assert( READ_LOCK+1==WRITE_LOCK ); + assert( isWriteLock==0 || isWriteLock==1 ); + + sqlite3BtreeEnter(p); + rc = querySharedCacheTableLock(p, iTab, lockType); + if( rc==SQLITE_OK ){ + rc = setSharedCacheTableLock(p, iTab, lockType); + } + sqlite3BtreeLeave(p); + } + return rc; +} +#endif + +#ifndef SQLITE_OMIT_INCRBLOB +/* +** Argument pCsr must be a cursor opened for writing on an +** INTKEY table currently pointing at a valid table entry. +** This function modifies the data stored as part of that entry. +** +** Only the data content may only be modified, it is not possible to +** change the length of the data stored. If this function is called with +** parameters that attempt to write past the end of the existing data, +** no modifications are made and SQLITE_CORRUPT is returned. +*/ +SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ + int rc; + assert( cursorHoldsMutex(pCsr) ); + assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); + assert( pCsr->isIncrblobHandle ); + + rc = restoreCursorPosition(pCsr); + if( rc!=SQLITE_OK ){ + return rc; + } + assert( pCsr->eState!=CURSOR_REQUIRESEEK ); + if( pCsr->eState!=CURSOR_VALID ){ + return SQLITE_ABORT; + } + + /* Check some assumptions: + ** (a) the cursor is open for writing, + ** (b) there is a read/write transaction open, + ** (c) the connection holds a write-lock on the table (if required), + ** (d) there are no conflicting read-locks, and + ** (e) the cursor points at a valid row of an intKey table. + */ + if( !pCsr->wrFlag ){ + return SQLITE_READONLY; + } + assert( !pCsr->pBt->readOnly && pCsr->pBt->inTransaction==TRANS_WRITE ); + assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) ); + assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) ); + assert( pCsr->apPage[pCsr->iPage]->intKey ); + + return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1); +} + +/* +** Set a flag on this cursor to cache the locations of pages from the +** overflow list for the current row. This is used by cursors opened +** for incremental blob IO only. +** +** This function sets a flag only. The actual page location cache +** (stored in BtCursor.aOverflow[]) is allocated and used by function +** accessPayload() (the worker function for sqlite3BtreeData() and +** sqlite3BtreePutData()). +*/ +SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){ + assert( cursorHoldsMutex(pCur) ); + assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); + invalidateOverflowCache(pCur); + pCur->isIncrblobHandle = 1; +} +#endif + +/* +** Set both the "read version" (single byte at byte offset 18) and +** "write version" (single byte at byte offset 19) fields in the database +** header to iVersion. +*/ +SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ + BtShared *pBt = pBtree->pBt; + int rc; /* Return code */ + + assert( pBtree->inTrans==TRANS_NONE ); + assert( iVersion==1 || iVersion==2 ); + + /* If setting the version fields to 1, do not automatically open the + ** WAL connection, even if the version fields are currently set to 2. + */ + pBt->doNotUseWAL = (u8)(iVersion==1); + + rc = sqlite3BtreeBeginTrans(pBtree, 0); + if( rc==SQLITE_OK ){ + u8 *aData = pBt->pPage1->aData; + if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){ + rc = sqlite3BtreeBeginTrans(pBtree, 2); + if( rc==SQLITE_OK ){ + rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); + if( rc==SQLITE_OK ){ + aData[18] = (u8)iVersion; + aData[19] = (u8)iVersion; + } + } + } + } + + pBt->doNotUseWAL = 0; + return rc; +} + +/************** End of btree.c ***********************************************/ diff --git a/fdbserver/sqlite/btree.h b/fdbserver/sqlite/btree.h new file mode 100644 index 0000000000..b0c748673d --- /dev/null +++ b/fdbserver/sqlite/btree.h @@ -0,0 +1,240 @@ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface that the sqlite B-Tree file +** subsystem. See comments in the source code for a detailed description +** of what each interface routine does. +*/ +#ifndef _BTREE_H_ +#define _BTREE_H_ + +/* TODO: This definition is just included so other modules compile. It +** needs to be revisited. +*/ +#define SQLITE_N_BTREE_META 10 + +/* +** If defined as non-zero, auto-vacuum is enabled by default. Otherwise +** it must be turned on for each database using "PRAGMA auto_vacuum = 1". +*/ +#ifndef SQLITE_DEFAULT_AUTOVACUUM + #define SQLITE_DEFAULT_AUTOVACUUM 0 +#endif + +#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */ +#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */ +#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */ + +/* +** Forward declarations of structure +*/ +typedef struct Btree Btree; +typedef struct BtCursor BtCursor; +typedef struct BtShared BtShared; + + +int sqlite3BtreeOpen( + const char *zFilename, /* Name of database file to open */ + sqlite3 *db, /* Associated database connection */ + Btree **ppBtree, /* Return open Btree* here */ + int flags, /* Flags */ + int vfsFlags /* Flags passed through to VFS open */ +); + +/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the +** following values. +** +** NOTE: These values must match the corresponding PAGER_ values in +** pager.h. +*/ +#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */ +#define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */ +#define BTREE_MEMORY 4 /* This is an in-memory DB */ +#define BTREE_SINGLE 8 /* The file contains at most 1 b-tree */ +#define BTREE_UNORDERED 16 /* Use of a hash implementation is OK */ + +int sqlite3BtreeClose(Btree*); +int sqlite3BtreeSetCacheSize(Btree*,int); +int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int); +int sqlite3BtreeSyncDisabled(Btree*); +int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix); +int sqlite3BtreeGetPageSize(Btree*); +int sqlite3BtreeMaxPageCount(Btree*,int); +u32 sqlite3BtreeLastPage(Btree*); +int sqlite3BtreeSecureDelete(Btree*,int); +int sqlite3BtreeGetReserve(Btree*); +int sqlite3BtreeSetAutoVacuum(Btree *, int); +int sqlite3BtreeGetAutoVacuum(Btree *); +int sqlite3BtreeBeginTrans(Btree*,int); +int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster); +int sqlite3BtreeCommitPhaseTwo(Btree*, int); +int sqlite3BtreeCommit(Btree*); +int sqlite3BtreeRollback(Btree*); +int sqlite3BtreeBeginStmt(Btree*,int); +int sqlite3BtreeCreateTable(Btree*, int*, int flags); +int sqlite3BtreeIsInTrans(Btree*); +int sqlite3BtreeIsInReadTrans(Btree*); +int sqlite3BtreeIsInBackup(Btree*); +void *sqlite3BtreeSchema(Btree *, int, void(*)(void *)); +int sqlite3BtreeSchemaLocked(Btree *pBtree); +int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock); +int sqlite3BtreeSavepoint(Btree *, int, int); + +const char *sqlite3BtreeGetFilename(Btree *); +const char *sqlite3BtreeGetJournalname(Btree *); +int sqlite3BtreeCopyFile(Btree *, Btree *); + +int sqlite3BtreeIncrVacuum(Btree *); + +/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR +** of the flags shown below. +** +** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set. +** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data +** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With +** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored +** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL +** indices.) +*/ +#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */ +#define BTREE_BLOBKEY 2 /* Table has keys only - no data */ + +int sqlite3BtreeDropTable(Btree*, int, int*); +int sqlite3BtreeClearTable(Btree*, int, int*); +void sqlite3BtreeTripAllCursors(Btree*, int); + +void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue); +int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value); + +/* +** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta +** should be one of the following values. The integer values are assigned +** to constants so that the offset of the corresponding field in an +** SQLite database header may be found using the following formula: +** +** offset = 36 + (idx * 4) +** +** For example, the free-page-count field is located at byte offset 36 of +** the database file header. The incr-vacuum-flag field is located at +** byte offset 64 (== 36+4*7). +*/ +#define BTREE_FREE_PAGE_COUNT 0 +#define BTREE_SCHEMA_VERSION 1 +#define BTREE_FILE_FORMAT 2 +#define BTREE_DEFAULT_CACHE_SIZE 3 +#define BTREE_LARGEST_ROOT_PAGE 4 +#define BTREE_TEXT_ENCODING 5 +#define BTREE_USER_VERSION 6 +#define BTREE_INCR_VACUUM 7 + +int sqlite3BtreeCursor( + Btree*, /* BTree containing table to open */ + int iTable, /* Index of root page */ + int wrFlag, /* 1 for writing. 0 for read-only */ + struct KeyInfo*, /* First argument to compare function */ + BtCursor *pCursor /* Space to write cursor structure */ +); +int sqlite3BtreeCursorSize(void); +void sqlite3BtreeCursorZero(BtCursor*); + +int sqlite3BtreeCloseCursor(BtCursor*); +int sqlite3BtreeMovetoUnpacked( + BtCursor*, + UnpackedRecord *pUnKey, + i64 intKey, + int bias, + int *pRes +); +int sqlite3BtreeCursorHasMoved(BtCursor*, int*); +int sqlite3BtreeDelete(BtCursor*); +int sqlite3BtreeDeleteRange(BtCursor*, BtCursor*, int* stackBegin, int* stackEnd); +int sqlite3BtreeLazyDelete(BtCursor*, int* stackBegin, int* stackEnd, int desiredPages, int* pagesDeleted); +int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey, + const void *pData, int nData, + int nZero, int bias, int seekResult); +int sqlite3BtreeFirst(BtCursor*, int *pRes); +int sqlite3BtreeLast(BtCursor*, int *pRes); +int sqlite3BtreeNext(BtCursor*, int *pRes); +int sqlite3BtreeEof(BtCursor*); +int sqlite3BtreePrevious(BtCursor*, int *pRes); +int sqlite3BtreeKeySize(BtCursor*, i64 *pSize); +int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*); +const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt); +const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt); +int sqlite3BtreeDataSize(BtCursor*, u32 *pSize); +int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*); +void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64); +sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*); + +char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*, int); +struct Pager *sqlite3BtreePager(Btree*); + +int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*); +void sqlite3BtreeCacheOverflow(BtCursor *); +void sqlite3BtreeClearCursor(BtCursor *); + +int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); + +#ifndef NDEBUG +int sqlite3BtreeCursorIsValid(BtCursor*); +#endif + +#ifndef SQLITE_OMIT_BTREECOUNT +int sqlite3BtreeCount(BtCursor *, i64 *); +#endif + +#ifdef SQLITE_TEST +int sqlite3BtreeCursorInfo(BtCursor*, int*, int); +void sqlite3BtreeCursorList(Btree*); +#endif + +#ifndef SQLITE_OMIT_WAL + int sqlite3BtreeCheckpoint(Btree*, int, int *, int *); +#endif + +/* +** If we are not using shared cache, then there is no need to +** use mutexes to access the BtShared structures. So make the +** Enter and Leave procedures no-ops. +*/ +#ifndef SQLITE_OMIT_SHARED_CACHE + void sqlite3BtreeEnter(Btree*); + void sqlite3BtreeEnterAll(sqlite3*); +#else +# define sqlite3BtreeEnter(X) +# define sqlite3BtreeEnterAll(X) +#endif + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE + void sqlite3BtreeLeave(Btree*); + void sqlite3BtreeEnterCursor(BtCursor*); + void sqlite3BtreeLeaveCursor(BtCursor*); + void sqlite3BtreeLeaveAll(sqlite3*); +#ifndef NDEBUG + /* These routines are used inside assert() statements only. */ + int sqlite3BtreeHoldsMutex(Btree*); + int sqlite3BtreeHoldsAllMutexes(sqlite3*); + int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*); +#endif +#else + +# define sqlite3BtreeLeave(X) +# define sqlite3BtreeEnterCursor(X) +# define sqlite3BtreeLeaveCursor(X) +# define sqlite3BtreeLeaveAll(X) + +# define sqlite3BtreeHoldsMutex(X) 1 +# define sqlite3BtreeHoldsAllMutexes(X) 1 +# define sqlite3SchemaMutexHeld(X,Y,Z) 1 +#endif + + +#endif /* _BTREE_H_ */ diff --git a/fdbserver/sqlite/hash.h b/fdbserver/sqlite/hash.h new file mode 100644 index 0000000000..990a2d6e22 --- /dev/null +++ b/fdbserver/sqlite/hash.h @@ -0,0 +1,96 @@ +/* +** 2001 September 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This is the header file for the generic hash-table implemenation +** used in SQLite. +*/ +#ifndef _SQLITE_HASH_H_ +#define _SQLITE_HASH_H_ + +/* Forward declarations of structures. */ +typedef struct Hash Hash; +typedef struct HashElem HashElem; + +/* A complete hash table is an instance of the following structure. +** The internals of this structure are intended to be opaque -- client +** code should not attempt to access or modify the fields of this structure +** directly. Change this structure only by using the routines below. +** However, some of the "procedures" and "functions" for modifying and +** accessing this structure are really macros, so we can't really make +** this structure opaque. +** +** All elements of the hash table are on a single doubly-linked list. +** Hash.first points to the head of this list. +** +** There are Hash.htsize buckets. Each bucket points to a spot in +** the global doubly-linked list. The contents of the bucket are the +** element pointed to plus the next _ht.count-1 elements in the list. +** +** Hash.htsize and Hash.ht may be zero. In that case lookup is done +** by a linear search of the global list. For small tables, the +** Hash.ht table is never allocated because if there are few elements +** in the table, it is faster to do a linear search than to manage +** the hash table. +*/ +struct Hash { + unsigned int htsize; /* Number of buckets in the hash table */ + unsigned int count; /* Number of entries in this table */ + HashElem *first; /* The first element of the array */ + struct _ht { /* the hash table */ + int count; /* Number of entries with this hash */ + HashElem *chain; /* Pointer to first entry with this hash */ + } *ht; +}; + +/* Each element in the hash table is an instance of the following +** structure. All elements are stored on a single doubly-linked list. +** +** Again, this structure is intended to be opaque, but it can't really +** be opaque because it is used by macros. +*/ +struct HashElem { + HashElem *next, *prev; /* Next and previous elements in the table */ + void *data; /* Data associated with this element */ + const char *pKey; int nKey; /* Key associated with this element */ +}; + +/* +** Access routines. To delete, insert a NULL pointer. +*/ +void sqlite3HashInit(Hash*); +void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData); +void *sqlite3HashFind(const Hash*, const char *pKey, int nKey); +void sqlite3HashClear(Hash*); + +/* +** Macros for looping over all elements of a hash table. The idiom is +** like this: +** +** Hash h; +** HashElem *p; +** ... +** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){ +** SomeStructure *pData = sqliteHashData(p); +** // do something with pData +** } +*/ +#define sqliteHashFirst(H) ((H)->first) +#define sqliteHashNext(E) ((E)->next) +#define sqliteHashData(E) ((E)->data) +/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */ +/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */ + +/* +** Number of entries in a hash table +*/ +/* #define sqliteHashCount(H) ((H)->count) // NOT USED */ + +#endif /* _SQLITE_HASH_H_ */ diff --git a/fdbserver/sqlite/shell.c b/fdbserver/sqlite/shell.c new file mode 100644 index 0000000000..9d51aa95dc --- /dev/null +++ b/fdbserver/sqlite/shell.c @@ -0,0 +1,2887 @@ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code to implement the "sqlite" command line +** utility for accessing SQLite databases. +*/ +#if defined(_WIN32) || defined(WIN32) +/* This needs to come before any includes for MSVC compiler */ +#define _CRT_SECURE_NO_WARNINGS +#endif + +#include +#include +#include +#include +#include "sqlite3.h" +#include +#include + +#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) +# include +# if !defined(__RTP__) && !defined(_WRS_KERNEL) +# include +# endif +# include +# include +#endif + +#ifdef __OS2__ +# include +#endif + +#ifdef HAVE_EDITLINE +# include +#endif +#if defined(HAVE_READLINE) && HAVE_READLINE==1 +# include +# include +#endif +#if !defined(HAVE_EDITLINE) && (!defined(HAVE_READLINE) || HAVE_READLINE!=1) +# define readline(p) local_getline(p,stdin) +# define add_history(X) +# define read_history(X) +# define write_history(X) +# define stifle_history(X) +#endif + +#if defined(_WIN32) || defined(WIN32) +# include +#define isatty(h) _isatty(h) +#define access(f,m) _access((f),(m)) +#else +/* Make sure isatty() has a prototype. +*/ +extern int isatty(); +#endif + +#if defined(_WIN32_WCE) +/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty() + * thus we always assume that we have a console. That can be + * overridden with the -batch command line option. + */ +#define isatty(x) 1 +#endif + +#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL) +#include +#include + +/* Saved resource information for the beginning of an operation */ +static struct rusage sBegin; + +/* True if the timer is enabled */ +static int enableTimer = 0; + +/* +** Begin timing an operation +*/ +static void beginTimer(void){ + if( enableTimer ){ + getrusage(RUSAGE_SELF, &sBegin); + } +} + +/* Return the difference of two time_structs in seconds */ +static double timeDiff(struct timeval *pStart, struct timeval *pEnd){ + return (pEnd->tv_usec - pStart->tv_usec)*0.000001 + + (double)(pEnd->tv_sec - pStart->tv_sec); +} + +/* +** Print the timing results. +*/ +static void endTimer(void){ + if( enableTimer ){ + struct rusage sEnd; + getrusage(RUSAGE_SELF, &sEnd); + printf("CPU Time: user %f sys %f\n", + timeDiff(&sBegin.ru_utime, &sEnd.ru_utime), + timeDiff(&sBegin.ru_stime, &sEnd.ru_stime)); + } +} + +#define BEGIN_TIMER beginTimer() +#define END_TIMER endTimer() +#define HAS_TIMER 1 + +#elif (defined(_WIN32) || defined(WIN32)) + +#include + +/* Saved resource information for the beginning of an operation */ +static HANDLE hProcess; +static FILETIME ftKernelBegin; +static FILETIME ftUserBegin; +typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME, LPFILETIME, LPFILETIME); +static GETPROCTIMES getProcessTimesAddr = NULL; + +/* True if the timer is enabled */ +static int enableTimer = 0; + +/* +** Check to see if we have timer support. Return 1 if necessary +** support found (or found previously). +*/ +static int hasTimer(void){ + if( getProcessTimesAddr ){ + return 1; + } else { + /* GetProcessTimes() isn't supported in WIN95 and some other Windows versions. + ** See if the version we are running on has it, and if it does, save off + ** a pointer to it and the current process handle. + */ + hProcess = GetCurrentProcess(); + if( hProcess ){ + HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll")); + if( NULL != hinstLib ){ + getProcessTimesAddr = (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes"); + if( NULL != getProcessTimesAddr ){ + return 1; + } + FreeLibrary(hinstLib); + } + } + } + return 0; +} + +/* +** Begin timing an operation +*/ +static void beginTimer(void){ + if( enableTimer && getProcessTimesAddr ){ + FILETIME ftCreation, ftExit; + getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelBegin, &ftUserBegin); + } +} + +/* Return the difference of two FILETIME structs in seconds */ +static double timeDiff(FILETIME *pStart, FILETIME *pEnd){ + sqlite_int64 i64Start = *((sqlite_int64 *) pStart); + sqlite_int64 i64End = *((sqlite_int64 *) pEnd); + return (double) ((i64End - i64Start) / 10000000.0); +} + +/* +** Print the timing results. +*/ +static void endTimer(void){ + if( enableTimer && getProcessTimesAddr){ + FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd; + getProcessTimesAddr(hProcess, &ftCreation, &ftExit, &ftKernelEnd, &ftUserEnd); + printf("CPU Time: user %f sys %f\n", + timeDiff(&ftUserBegin, &ftUserEnd), + timeDiff(&ftKernelBegin, &ftKernelEnd)); + } +} + +#define BEGIN_TIMER beginTimer() +#define END_TIMER endTimer() +#define HAS_TIMER hasTimer() + +#else +#define BEGIN_TIMER +#define END_TIMER +#define HAS_TIMER 0 +#endif + +/* +** Used to prevent warnings about unused parameters +*/ +#define UNUSED_PARAMETER(x) (void)(x) + +/* +** If the following flag is set, then command execution stops +** at an error if we are not interactive. +*/ +static int bail_on_error = 0; + +/* +** Threat stdin as an interactive input if the following variable +** is true. Otherwise, assume stdin is connected to a file or pipe. +*/ +static int stdin_is_interactive = 1; + +/* +** The following is the open SQLite database. We make a pointer +** to this database a static variable so that it can be accessed +** by the SIGINT handler to interrupt database processing. +*/ +static sqlite3 *db = 0; + +/* +** True if an interrupt (Control-C) has been received. +*/ +static volatile int seenInterrupt = 0; + +/* +** This is the name of our program. It is set in main(), used +** in a number of other places, mostly for error messages. +*/ +static char *Argv0; + +/* +** Prompt strings. Initialized in main. Settable with +** .prompt main continue +*/ +static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/ +static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */ + +/* +** Write I/O traces to the following stream. +*/ +#ifdef SQLITE_ENABLE_IOTRACE +static FILE *iotrace = 0; +#endif + +/* +** This routine works like printf in that its first argument is a +** format string and subsequent arguments are values to be substituted +** in place of % fields. The result of formatting this string +** is written to iotrace. +*/ +#ifdef SQLITE_ENABLE_IOTRACE +static void iotracePrintf(const char *zFormat, ...){ + va_list ap; + char *z; + if( iotrace==0 ) return; + va_start(ap, zFormat); + z = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + fprintf(iotrace, "%s", z); + sqlite3_free(z); +} +#endif + + +/* +** Determines if a string is a number of not. +*/ +static int isNumber(const char *z, int *realnum){ + if( *z=='-' || *z=='+' ) z++; + if( !isdigit(*z) ){ + return 0; + } + z++; + if( realnum ) *realnum = 0; + while( isdigit(*z) ){ z++; } + if( *z=='.' ){ + z++; + if( !isdigit(*z) ) return 0; + while( isdigit(*z) ){ z++; } + if( realnum ) *realnum = 1; + } + if( *z=='e' || *z=='E' ){ + z++; + if( *z=='+' || *z=='-' ) z++; + if( !isdigit(*z) ) return 0; + while( isdigit(*z) ){ z++; } + if( realnum ) *realnum = 1; + } + return *z==0; +} + +/* +** A global char* and an SQL function to access its current value +** from within an SQL statement. This program used to use the +** sqlite_exec_printf() API to substitue a string into an SQL statement. +** The correct way to do this with sqlite3 is to use the bind API, but +** since the shell is built around the callback paradigm it would be a lot +** of work. Instead just use this hack, which is quite harmless. +*/ +static const char *zShellStatic = 0; +static void shellstaticFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + assert( 0==argc ); + assert( zShellStatic ); + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(argv); + sqlite3_result_text(context, zShellStatic, -1, SQLITE_STATIC); +} + + +/* +** This routine reads a line of text from FILE in, stores +** the text in memory obtained from malloc() and returns a pointer +** to the text. NULL is returned at end of file, or if malloc() +** fails. +** +** The interface is like "readline" but no command-line editing +** is done. +*/ +static char *local_getline(char *zPrompt, FILE *in){ + char *zLine; + int nLine; + int n; + int eol; + + if( zPrompt && *zPrompt ){ + printf("%s",zPrompt); + fflush(stdout); + } + nLine = 100; + zLine = malloc( nLine ); + if( zLine==0 ) return 0; + n = 0; + eol = 0; + while( !eol ){ + if( n+100>nLine ){ + nLine = nLine*2 + 100; + zLine = realloc(zLine, nLine); + if( zLine==0 ) return 0; + } + if( fgets(&zLine[n], nLine - n, in)==0 ){ + if( n==0 ){ + free(zLine); + return 0; + } + zLine[n] = 0; + eol = 1; + break; + } + while( zLine[n] ){ n++; } + if( n>0 && zLine[n-1]=='\n' ){ + n--; + if( n>0 && zLine[n-1]=='\r' ) n--; + zLine[n] = 0; + eol = 1; + } + } + zLine = realloc( zLine, n+1 ); + return zLine; +} + +/* +** Retrieve a single line of input text. +** +** zPrior is a string of prior text retrieved. If not the empty +** string, then issue a continuation prompt. +*/ +static char *one_input_line(const char *zPrior, FILE *in){ + char *zPrompt; + char *zResult; + if( in!=0 ){ + return local_getline(0, in); + } + if( zPrior && zPrior[0] ){ + zPrompt = continuePrompt; + }else{ + zPrompt = mainPrompt; + } + zResult = readline(zPrompt); +#if defined(HAVE_READLINE) && HAVE_READLINE==1 + if( zResult && *zResult ) add_history(zResult); +#endif + return zResult; +} + +struct previous_mode_data { + int valid; /* Is there legit data in here? */ + int mode; + int showHeader; + int colWidth[100]; +}; + +/* +** An pointer to an instance of this structure is passed from +** the main program to the callback. This is used to communicate +** state and mode information. +*/ +struct callback_data { + sqlite3 *db; /* The database */ + int echoOn; /* True to echo input commands */ + int statsOn; /* True to display memory stats before each finalize */ + int cnt; /* Number of records displayed so far */ + FILE *out; /* Write results here */ + int mode; /* An output mode setting */ + int writableSchema; /* True if PRAGMA writable_schema=ON */ + int showHeader; /* True to show column names in List or Column mode */ + char *zDestTable; /* Name of destination table when MODE_Insert */ + char separator[20]; /* Separator character for MODE_List */ + int colWidth[100]; /* Requested width of each column when in column mode*/ + int actualWidth[100]; /* Actual width of each column */ + char nullvalue[20]; /* The text to print when a NULL comes back from + ** the database */ + struct previous_mode_data explainPrev; + /* Holds the mode information just before + ** .explain ON */ + char outfile[FILENAME_MAX]; /* Filename for *out */ + const char *zDbFilename; /* name of the database file */ + const char *zVfs; /* Name of VFS to use */ + sqlite3_stmt *pStmt; /* Current statement if any. */ + FILE *pLog; /* Write log output here */ +}; + +/* +** These are the allowed modes. +*/ +#define MODE_Line 0 /* One column per line. Blank line between records */ +#define MODE_Column 1 /* One record per line in neat columns */ +#define MODE_List 2 /* One record per line with a separator */ +#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */ +#define MODE_Html 4 /* Generate an XHTML table */ +#define MODE_Insert 5 /* Generate SQL "insert" statements */ +#define MODE_Tcl 6 /* Generate ANSI-C or TCL quoted elements */ +#define MODE_Csv 7 /* Quote strings, numbers are plain */ +#define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */ + +static const char *modeDescr[] = { + "line", + "column", + "list", + "semi", + "html", + "insert", + "tcl", + "csv", + "explain", +}; + +/* +** Number of elements in an array +*/ +#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0])) + +/* +** Compute a string length that is limited to what can be stored in +** lower 30 bits of a 32-bit signed integer. +*/ +static int strlen30(const char *z){ + const char *z2 = z; + while( *z2 ){ z2++; } + return 0x3fffffff & (int)(z2 - z); +} + +/* +** A callback for the sqlite3_log() interface. +*/ +static void shellLog(void *pArg, int iErrCode, const char *zMsg){ + struct callback_data *p = (struct callback_data*)pArg; + if( p->pLog==0 ) return; + fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg); + fflush(p->pLog); +} + +/* +** Output the given string as a hex-encoded blob (eg. X'1234' ) +*/ +static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){ + int i; + char *zBlob = (char *)pBlob; + fprintf(out,"X'"); + for(i=0; i0 ){ + fprintf(out,"%.*s",i,z); + } + if( z[i]=='<' ){ + fprintf(out,"<"); + }else if( z[i]=='&' ){ + fprintf(out,"&"); + }else if( z[i]=='>' ){ + fprintf(out,">"); + }else if( z[i]=='\"' ){ + fprintf(out,"""); + }else if( z[i]=='\'' ){ + fprintf(out,"'"); + }else{ + break; + } + z += i + 1; + } +} + +/* +** If a field contains any character identified by a 1 in the following +** array, then the string must be quoted for CSV. +*/ +static const char needCsvQuote[] = { + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, +}; + +/* +** Output a single term of CSV. Actually, p->separator is used for +** the separator, which may or may not be a comma. p->nullvalue is +** the null value. Strings are quoted using ANSI-C rules. Numbers +** appear outside of quotes. +*/ +static void output_csv(struct callback_data *p, const char *z, int bSep){ + FILE *out = p->out; + if( z==0 ){ + fprintf(out,"%s",p->nullvalue); + }else{ + int i; + int nSep = strlen30(p->separator); + for(i=0; z[i]; i++){ + if( needCsvQuote[((unsigned char*)z)[i]] + || (z[i]==p->separator[0] && + (nSep==1 || memcmp(z, p->separator, nSep)==0)) ){ + i = 0; + break; + } + } + if( i==0 ){ + putc('"', out); + for(i=0; z[i]; i++){ + if( z[i]=='"' ) putc('"', out); + putc(z[i], out); + } + putc('"', out); + }else{ + fprintf(out, "%s", z); + } + } + if( bSep ){ + fprintf(p->out, "%s", p->separator); + } +} + +#ifdef SIGINT +/* +** This routine runs when the user presses Ctrl-C +*/ +static void interrupt_handler(int NotUsed){ + UNUSED_PARAMETER(NotUsed); + seenInterrupt = 1; + if( db ) sqlite3_interrupt(db); +} +#endif + +/* +** This is the callback routine that the shell +** invokes for each row of a query result. +*/ +static int shell_callback(void *pArg, int nArg, char **azArg, char **azCol, int *aiType){ + int i; + struct callback_data *p = (struct callback_data*)pArg; + + switch( p->mode ){ + case MODE_Line: { + int w = 5; + if( azArg==0 ) break; + for(i=0; iw ) w = len; + } + if( p->cnt++>0 ) fprintf(p->out,"\n"); + for(i=0; iout,"%*s = %s\n", w, azCol[i], + azArg[i] ? azArg[i] : p->nullvalue); + } + break; + } + case MODE_Explain: + case MODE_Column: { + if( p->cnt++==0 ){ + for(i=0; icolWidth) ){ + w = p->colWidth[i]; + }else{ + w = 0; + } + if( w<=0 ){ + w = strlen30(azCol[i] ? azCol[i] : ""); + if( w<10 ) w = 10; + n = strlen30(azArg && azArg[i] ? azArg[i] : p->nullvalue); + if( wactualWidth) ){ + p->actualWidth[i] = w; + } + if( p->showHeader ){ + fprintf(p->out,"%-*.*s%s",w,w,azCol[i], i==nArg-1 ? "\n": " "); + } + } + if( p->showHeader ){ + for(i=0; iactualWidth) ){ + w = p->actualWidth[i]; + }else{ + w = 10; + } + fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------" + "----------------------------------------------------------", + i==nArg-1 ? "\n": " "); + } + } + } + if( azArg==0 ) break; + for(i=0; iactualWidth) ){ + w = p->actualWidth[i]; + }else{ + w = 10; + } + if( p->mode==MODE_Explain && azArg[i] && + strlen30(azArg[i])>w ){ + w = strlen30(azArg[i]); + } + fprintf(p->out,"%-*.*s%s",w,w, + azArg[i] ? azArg[i] : p->nullvalue, i==nArg-1 ? "\n": " "); + } + break; + } + case MODE_Semi: + case MODE_List: { + if( p->cnt++==0 && p->showHeader ){ + for(i=0; iout,"%s%s",azCol[i], i==nArg-1 ? "\n" : p->separator); + } + } + if( azArg==0 ) break; + for(i=0; inullvalue; + fprintf(p->out, "%s", z); + if( iout, "%s", p->separator); + }else if( p->mode==MODE_Semi ){ + fprintf(p->out, ";\n"); + }else{ + fprintf(p->out, "\n"); + } + } + break; + } + case MODE_Html: { + if( p->cnt++==0 && p->showHeader ){ + fprintf(p->out,""); + for(i=0; iout,""); + output_html_string(p->out, azCol[i]); + fprintf(p->out,"\n"); + } + fprintf(p->out,"\n"); + } + if( azArg==0 ) break; + fprintf(p->out,""); + for(i=0; iout,""); + output_html_string(p->out, azArg[i] ? azArg[i] : p->nullvalue); + fprintf(p->out,"\n"); + } + fprintf(p->out,"\n"); + break; + } + case MODE_Tcl: { + if( p->cnt++==0 && p->showHeader ){ + for(i=0; iout,azCol[i] ? azCol[i] : ""); + fprintf(p->out, "%s", p->separator); + } + fprintf(p->out,"\n"); + } + if( azArg==0 ) break; + for(i=0; iout, azArg[i] ? azArg[i] : p->nullvalue); + fprintf(p->out, "%s", p->separator); + } + fprintf(p->out,"\n"); + break; + } + case MODE_Csv: { + if( p->cnt++==0 && p->showHeader ){ + for(i=0; iout,"\n"); + } + if( azArg==0 ) break; + for(i=0; iout,"\n"); + break; + } + case MODE_Insert: { + p->cnt++; + if( azArg==0 ) break; + fprintf(p->out,"INSERT INTO %s VALUES(",p->zDestTable); + for(i=0; i0 ? ",": ""; + if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){ + fprintf(p->out,"%sNULL",zSep); + }else if( aiType && aiType[i]==SQLITE_TEXT ){ + if( zSep[0] ) fprintf(p->out,"%s",zSep); + output_quoted_string(p->out, azArg[i]); + }else if( aiType && (aiType[i]==SQLITE_INTEGER || aiType[i]==SQLITE_FLOAT) ){ + fprintf(p->out,"%s%s",zSep, azArg[i]); + }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){ + const void *pBlob = sqlite3_column_blob(p->pStmt, i); + int nBlob = sqlite3_column_bytes(p->pStmt, i); + if( zSep[0] ) fprintf(p->out,"%s",zSep); + output_hex_blob(p->out, pBlob, nBlob); + }else if( isNumber(azArg[i], 0) ){ + fprintf(p->out,"%s%s",zSep, azArg[i]); + }else{ + if( zSep[0] ) fprintf(p->out,"%s",zSep); + output_quoted_string(p->out, azArg[i]); + } + } + fprintf(p->out,");\n"); + break; + } + } + return 0; +} + +/* +** This is the callback routine that the SQLite library +** invokes for each row of a query result. +*/ +static int callback(void *pArg, int nArg, char **azArg, char **azCol){ + /* since we don't have type info, call the shell_callback with a NULL value */ + return shell_callback(pArg, nArg, azArg, azCol, NULL); +} + +/* +** Set the destination table field of the callback_data structure to +** the name of the table given. Escape any quote characters in the +** table name. +*/ +static void set_table_name(struct callback_data *p, const char *zName){ + int i, n; + int needQuote; + char *z; + + if( p->zDestTable ){ + free(p->zDestTable); + p->zDestTable = 0; + } + if( zName==0 ) return; + needQuote = !isalpha((unsigned char)*zName) && *zName!='_'; + for(i=n=0; zName[i]; i++, n++){ + if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ){ + needQuote = 1; + if( zName[i]=='\'' ) n++; + } + } + if( needQuote ) n += 2; + z = p->zDestTable = malloc( n+1 ); + if( z==0 ){ + fprintf(stderr,"Error: out of memory\n"); + exit(1); + } + n = 0; + if( needQuote ) z[n++] = '\''; + for(i=0; zName[i]; i++){ + z[n++] = zName[i]; + if( zName[i]=='\'' ) z[n++] = '\''; + } + if( needQuote ) z[n++] = '\''; + z[n] = 0; +} + +/* zIn is either a pointer to a NULL-terminated string in memory obtained +** from malloc(), or a NULL pointer. The string pointed to by zAppend is +** added to zIn, and the result returned in memory obtained from malloc(). +** zIn, if it was not NULL, is freed. +** +** If the third argument, quote, is not '\0', then it is used as a +** quote character for zAppend. +*/ +static char *appendText(char *zIn, char const *zAppend, char quote){ + int len; + int i; + int nAppend = strlen30(zAppend); + int nIn = (zIn?strlen30(zIn):0); + + len = nAppend+nIn+1; + if( quote ){ + len += 2; + for(i=0; iout ){ + + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Memory Used: %d (max %d) bytes\n", iCur, iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n", iCur, iHiwtr); +/* +** Not currently used by the CLI. +** iHiwtr = iCur = -1; +** sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset); +** fprintf(pArg->out, "Number of Pcache Pages Used: %d (max %d) pages\n", iCur, iHiwtr); +*/ + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Number of Pcache Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr); +/* +** Not currently used by the CLI. +** iHiwtr = iCur = -1; +** sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset); +** fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n", iCur, iHiwtr); +*/ + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Number of Scratch Overflow Bytes: %d (max %d) bytes\n", iCur, iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Largest Allocation: %d bytes\n", iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Largest Pcache Allocation: %d bytes\n", iHiwtr); + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Largest Scratch Allocation: %d bytes\n", iHiwtr); +#ifdef YYTRACKMAXSTACKDEPTH + iHiwtr = iCur = -1; + sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n", iCur, iHiwtr); +#endif + } + + if( pArg && pArg->out && db ){ + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr); + sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Schema Heap Usage: %d bytes\n", iCur); + iHiwtr = iCur = -1; + sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset); + fprintf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n", iCur); + } + + if( pArg && pArg->out && db && pArg->pStmt ){ + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP, bReset); + fprintf(pArg->out, "Fullscan Steps: %d\n", iCur); + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset); + fprintf(pArg->out, "Sort Operations: %d\n", iCur); + iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX, bReset); + fprintf(pArg->out, "Autoindex Inserts: %d\n", iCur); + } + + return 0; +} + +/* +** Execute a statement or set of statements. Print +** any result rows/columns depending on the current mode +** set via the supplied callback. +** +** This is very similar to SQLite's built-in sqlite3_exec() +** function except it takes a slightly different callback +** and callback data argument. +*/ +static int shell_exec( + sqlite3 *db, /* An open database */ + const char *zSql, /* SQL to be evaluated */ + int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */ + /* (not the same as sqlite3_exec) */ + struct callback_data *pArg, /* Pointer to struct callback_data */ + char **pzErrMsg /* Error msg written here */ +){ + sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ + int rc = SQLITE_OK; /* Return Code */ + const char *zLeftover; /* Tail of unprocessed SQL */ + + if( pzErrMsg ){ + *pzErrMsg = NULL; + } + + while( zSql[0] && (SQLITE_OK == rc) ){ + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); + if( SQLITE_OK != rc ){ + if( pzErrMsg ){ + *pzErrMsg = save_err_msg(db); + } + }else{ + if( !pStmt ){ + /* this happens for a comment or white-space */ + zSql = zLeftover; + while( isspace(zSql[0]) ) zSql++; + continue; + } + + /* save off the prepared statment handle and reset row count */ + if( pArg ){ + pArg->pStmt = pStmt; + pArg->cnt = 0; + } + + /* echo the sql statement if echo on */ + if( pArg && pArg->echoOn ){ + const char *zStmtSql = sqlite3_sql(pStmt); + fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); + } + + /* perform the first step. this will tell us if we + ** have a result set or not and how wide it is. + */ + rc = sqlite3_step(pStmt); + /* if we have a result set... */ + if( SQLITE_ROW == rc ){ + /* if we have a callback... */ + if( xCallback ){ + /* allocate space for col name ptr, value ptr, and type */ + int nCol = sqlite3_column_count(pStmt); + void *pData = sqlite3_malloc(3*nCol*sizeof(const char*) + 1); + if( !pData ){ + rc = SQLITE_NOMEM; + }else{ + char **azCols = (char **)pData; /* Names of result columns */ + char **azVals = &azCols[nCol]; /* Results */ + int *aiTypes = (int *)&azVals[nCol]; /* Result types */ + int i; + assert(sizeof(int) <= sizeof(char *)); + /* save off ptrs to column names */ + for(i=0; istatsOn ){ + display_stats(db, pArg, 0); + } + + /* Finalize the statement just executed. If this fails, save a + ** copy of the error message. Otherwise, set zSql to point to the + ** next statement to execute. */ + rc = sqlite3_finalize(pStmt); + if( rc==SQLITE_OK ){ + zSql = zLeftover; + while( isspace(zSql[0]) ) zSql++; + }else if( pzErrMsg ){ + *pzErrMsg = save_err_msg(db); + } + + /* clear saved stmt handle */ + if( pArg ){ + pArg->pStmt = NULL; + } + } + } /* end while */ + + return rc; +} + + +/* +** This is a different callback routine used for dumping the database. +** Each row received by this callback consists of a table name, +** the table type ("index" or "table") and SQL to create the table. +** This routine should print text sufficient to recreate the table. +*/ +static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){ + int rc; + const char *zTable; + const char *zType; + const char *zSql; + const char *zPrepStmt = 0; + struct callback_data *p = (struct callback_data *)pArg; + + UNUSED_PARAMETER(azCol); + if( nArg!=3 ) return 1; + zTable = azArg[0]; + zType = azArg[1]; + zSql = azArg[2]; + + if( strcmp(zTable, "sqlite_sequence")==0 ){ + zPrepStmt = "DELETE FROM sqlite_sequence;\n"; + }else if( strcmp(zTable, "sqlite_stat1")==0 ){ + fprintf(p->out, "ANALYZE sqlite_master;\n"); + }else if( strncmp(zTable, "sqlite_", 7)==0 ){ + return 0; + }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){ + char *zIns; + if( !p->writableSchema ){ + fprintf(p->out, "PRAGMA writable_schema=ON;\n"); + p->writableSchema = 1; + } + zIns = sqlite3_mprintf( + "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)" + "VALUES('table','%q','%q',0,'%q');", + zTable, zTable, zSql); + fprintf(p->out, "%s\n", zIns); + sqlite3_free(zIns); + return 0; + }else{ + fprintf(p->out, "%s;\n", zSql); + } + + if( strcmp(zType, "table")==0 ){ + sqlite3_stmt *pTableInfo = 0; + char *zSelect = 0; + char *zTableInfo = 0; + char *zTmp = 0; + int nRow = 0; + + zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0); + zTableInfo = appendText(zTableInfo, zTable, '"'); + zTableInfo = appendText(zTableInfo, ");", 0); + + rc = sqlite3_prepare(p->db, zTableInfo, -1, &pTableInfo, 0); + free(zTableInfo); + if( rc!=SQLITE_OK || !pTableInfo ){ + return 1; + } + + zSelect = appendText(zSelect, "SELECT 'INSERT INTO ' || ", 0); + zTmp = appendText(zTmp, zTable, '"'); + if( zTmp ){ + zSelect = appendText(zSelect, zTmp, '\''); + } + zSelect = appendText(zSelect, " || ' VALUES(' || ", 0); + rc = sqlite3_step(pTableInfo); + while( rc==SQLITE_ROW ){ + const char *zText = (const char *)sqlite3_column_text(pTableInfo, 1); + zSelect = appendText(zSelect, "quote(", 0); + zSelect = appendText(zSelect, zText, '"'); + rc = sqlite3_step(pTableInfo); + if( rc==SQLITE_ROW ){ + zSelect = appendText(zSelect, ") || ',' || ", 0); + }else{ + zSelect = appendText(zSelect, ") ", 0); + } + nRow++; + } + rc = sqlite3_finalize(pTableInfo); + if( rc!=SQLITE_OK || nRow==0 ){ + free(zSelect); + return 1; + } + zSelect = appendText(zSelect, "|| ')' FROM ", 0); + zSelect = appendText(zSelect, zTable, '"'); + + rc = run_table_dump_query(p->out, p->db, zSelect, zPrepStmt); + if( rc==SQLITE_CORRUPT ){ + zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0); + rc = run_table_dump_query(p->out, p->db, zSelect, 0); + } + if( zSelect ) free(zSelect); + } + return 0; +} + +/* +** Run zQuery. Use dump_callback() as the callback routine so that +** the contents of the query are output as SQL statements. +** +** If we get a SQLITE_CORRUPT error, rerun the query after appending +** "ORDER BY rowid DESC" to the end. +*/ +static int run_schema_dump_query( + struct callback_data *p, + const char *zQuery, + char **pzErrMsg +){ + int rc; + rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg); + if( rc==SQLITE_CORRUPT ){ + char *zQ2; + int len = strlen30(zQuery); + if( pzErrMsg ) sqlite3_free(*pzErrMsg); + zQ2 = malloc( len+100 ); + if( zQ2==0 ) return rc; + sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery); + rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg); + free(zQ2); + } + return rc; +} + +/* +** Text of a help message +*/ +static char zHelp[] = + ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n" + ".bail ON|OFF Stop after hitting an error. Default OFF\n" + ".databases List names and files of attached databases\n" + ".dump ?TABLE? ... Dump the database in an SQL text format\n" + " If TABLE specified, only dump tables matching\n" + " LIKE pattern TABLE.\n" + ".echo ON|OFF Turn command echo on or off\n" + ".exit Exit this program\n" + ".explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.\n" + " With no args, it turns EXPLAIN on.\n" + ".header(s) ON|OFF Turn display of headers on or off\n" + ".help Show this message\n" + ".import FILE TABLE Import data from FILE into TABLE\n" + ".indices ?TABLE? Show names of all indices\n" + " If TABLE specified, only show indices for tables\n" + " matching LIKE pattern TABLE.\n" +#ifdef SQLITE_ENABLE_IOTRACE + ".iotrace FILE Enable I/O diagnostic logging to FILE\n" +#endif +#ifndef SQLITE_OMIT_LOAD_EXTENSION + ".load FILE ?ENTRY? Load an extension library\n" +#endif + ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n" + ".mode MODE ?TABLE? Set output mode where MODE is one of:\n" + " csv Comma-separated values\n" + " column Left-aligned columns. (See .width)\n" + " html HTML code\n" + " insert SQL insert statements for TABLE\n" + " line One value per line\n" + " list Values delimited by .separator string\n" + " tabs Tab-separated values\n" + " tcl TCL list elements\n" + ".nullvalue STRING Print STRING in place of NULL values\n" + ".output FILENAME Send output to FILENAME\n" + ".output stdout Send output to the screen\n" + ".prompt MAIN CONTINUE Replace the standard prompts\n" + ".quit Exit this program\n" + ".read FILENAME Execute SQL in FILENAME\n" + ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n" + ".schema ?TABLE? Show the CREATE statements\n" + " If TABLE specified, only show tables matching\n" + " LIKE pattern TABLE.\n" + ".separator STRING Change separator used by output mode and .import\n" + ".show Show the current values for various settings\n" + ".stats ON|OFF Turn stats on or off\n" + ".tables ?TABLE? List names of tables\n" + " If TABLE specified, only list tables matching\n" + " LIKE pattern TABLE.\n" + ".timeout MS Try opening locked tables for MS milliseconds\n" + ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n" +; + +static char zTimerHelp[] = + ".timer ON|OFF Turn the CPU timer measurement on or off\n" +; + +/* Forward reference */ +static int process_input(struct callback_data *p, FILE *in); + +/* +** Make sure the database is open. If it is not, then open it. If +** the database fails to open, print an error message and exit. +*/ +static void open_db(struct callback_data *p){ + if( p->db==0 ){ + sqlite3_open(p->zDbFilename, &p->db); + db = p->db; + if( db && sqlite3_errcode(db)==SQLITE_OK ){ + sqlite3_create_function(db, "shellstatic", 0, SQLITE_UTF8, 0, + shellstaticFunc, 0, 0); + } + if( db==0 || SQLITE_OK!=sqlite3_errcode(db) ){ + fprintf(stderr,"Error: unable to open database \"%s\": %s\n", + p->zDbFilename, sqlite3_errmsg(db)); + exit(1); + } +#ifndef SQLITE_OMIT_LOAD_EXTENSION + sqlite3_enable_load_extension(p->db, 1); +#endif + } +} + +/* +** Do C-language style dequoting. +** +** \t -> tab +** \n -> newline +** \r -> carriage return +** \NNN -> ascii character NNN in octal +** \\ -> backslash +*/ +static void resolve_backslashes(char *z){ + int i, j; + char c; + for(i=j=0; (c = z[i])!=0; i++, j++){ + if( c=='\\' ){ + c = z[++i]; + if( c=='n' ){ + c = '\n'; + }else if( c=='t' ){ + c = '\t'; + }else if( c=='r' ){ + c = '\r'; + }else if( c>='0' && c<='7' ){ + c -= '0'; + if( z[i+1]>='0' && z[i+1]<='7' ){ + i++; + c = (c<<3) + z[i] - '0'; + if( z[i+1]>='0' && z[i+1]<='7' ){ + i++; + c = (c<<3) + z[i] - '0'; + } + } + } + } + z[j] = c; + } + z[j] = 0; +} + +/* +** Interpret zArg as a boolean value. Return either 0 or 1. +*/ +static int booleanValue(char *zArg){ + int val = atoi(zArg); + int j; + for(j=0; zArg[j]; j++){ + zArg[j] = (char)tolower(zArg[j]); + } + if( strcmp(zArg,"on")==0 ){ + val = 1; + }else if( strcmp(zArg,"yes")==0 ){ + val = 1; + } + return val; +} + +/* +** If an input line begins with "." then invoke this routine to +** process that line. +** +** Return 1 on error, 2 to exit, and 0 otherwise. +*/ +static int do_meta_command(char *zLine, struct callback_data *p){ + int i = 1; + int nArg = 0; + int n, c; + int rc = 0; + char *azArg[50]; + + /* Parse the input line into tokens. + */ + while( zLine[i] && nArg=3 && strncmp(azArg[0], "backup", n)==0 && nArg>1 && nArg<4){ + const char *zDestFile; + const char *zDb; + sqlite3 *pDest; + sqlite3_backup *pBackup; + if( nArg==2 ){ + zDestFile = azArg[1]; + zDb = "main"; + }else{ + zDestFile = azArg[2]; + zDb = azArg[1]; + } + rc = sqlite3_open(zDestFile, &pDest); + if( rc!=SQLITE_OK ){ + fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile); + sqlite3_close(pDest); + return 1; + } + open_db(p); + pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb); + if( pBackup==0 ){ + fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); + sqlite3_close(pDest); + return 1; + } + while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){} + sqlite3_backup_finish(pBackup); + if( rc==SQLITE_DONE ){ + rc = 0; + }else{ + fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest)); + rc = 1; + } + sqlite3_close(pDest); + }else + + if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 && nArg>1 && nArg<3 ){ + bail_on_error = booleanValue(azArg[1]); + }else + + if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 && nArg==1 ){ + struct callback_data data; + char *zErrMsg = 0; + open_db(p); + memcpy(&data, p, sizeof(data)); + data.showHeader = 1; + data.mode = MODE_Column; + data.colWidth[0] = 3; + data.colWidth[1] = 15; + data.colWidth[2] = 58; + data.cnt = 0; + sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg); + if( zErrMsg ){ + fprintf(stderr,"Error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + } + }else + + if( c=='d' && strncmp(azArg[0], "dump", n)==0 && nArg<3 ){ + char *zErrMsg = 0; + open_db(p); + /* When playing back a "dump", the content might appear in an order + ** which causes immediate foreign key constraints to be violated. + ** So disable foreign-key constraint enforcement to prevent problems. */ + fprintf(p->out, "PRAGMA foreign_keys=OFF;\n"); + fprintf(p->out, "BEGIN TRANSACTION;\n"); + p->writableSchema = 0; + sqlite3_exec(p->db, "PRAGMA writable_schema=ON", 0, 0, 0); + if( nArg==1 ){ + run_schema_dump_query(p, + "SELECT name, type, sql FROM sqlite_master " + "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'", 0 + ); + run_schema_dump_query(p, + "SELECT name, type, sql FROM sqlite_master " + "WHERE name=='sqlite_sequence'", 0 + ); + run_table_dump_query(p->out, p->db, + "SELECT sql FROM sqlite_master " + "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0 + ); + }else{ + int i; + for(i=1; iout, p->db, + "SELECT sql FROM sqlite_master " + "WHERE sql NOT NULL" + " AND type IN ('index','trigger','view')" + " AND tbl_name LIKE shellstatic()", 0 + ); + zShellStatic = 0; + } + } + if( p->writableSchema ){ + fprintf(p->out, "PRAGMA writable_schema=OFF;\n"); + p->writableSchema = 0; + } + sqlite3_exec(p->db, "PRAGMA writable_schema=OFF", 0, 0, 0); + if( zErrMsg ){ + fprintf(stderr,"Error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + }else{ + fprintf(p->out, "COMMIT;\n"); + } + }else + + if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){ + p->echoOn = booleanValue(azArg[1]); + }else + + if( c=='e' && strncmp(azArg[0], "exit", n)==0 && nArg==1 ){ + rc = 2; + }else + + if( c=='e' && strncmp(azArg[0], "explain", n)==0 && nArg<3 ){ + int val = nArg>=2 ? booleanValue(azArg[1]) : 1; + if(val == 1) { + if(!p->explainPrev.valid) { + p->explainPrev.valid = 1; + p->explainPrev.mode = p->mode; + p->explainPrev.showHeader = p->showHeader; + memcpy(p->explainPrev.colWidth,p->colWidth,sizeof(p->colWidth)); + } + /* We could put this code under the !p->explainValid + ** condition so that it does not execute if we are already in + ** explain mode. However, always executing it allows us an easy + ** was to reset to explain mode in case the user previously + ** did an .explain followed by a .width, .mode or .header + ** command. + */ + p->mode = MODE_Explain; + p->showHeader = 1; + memset(p->colWidth,0,ArraySize(p->colWidth)); + p->colWidth[0] = 4; /* addr */ + p->colWidth[1] = 13; /* opcode */ + p->colWidth[2] = 4; /* P1 */ + p->colWidth[3] = 4; /* P2 */ + p->colWidth[4] = 4; /* P3 */ + p->colWidth[5] = 13; /* P4 */ + p->colWidth[6] = 2; /* P5 */ + p->colWidth[7] = 13; /* Comment */ + }else if (p->explainPrev.valid) { + p->explainPrev.valid = 0; + p->mode = p->explainPrev.mode; + p->showHeader = p->explainPrev.showHeader; + memcpy(p->colWidth,p->explainPrev.colWidth,sizeof(p->colWidth)); + } + }else + + if( c=='h' && (strncmp(azArg[0], "header", n)==0 || + strncmp(azArg[0], "headers", n)==0) && nArg>1 && nArg<3 ){ + p->showHeader = booleanValue(azArg[1]); + }else + + if( c=='h' && strncmp(azArg[0], "help", n)==0 ){ + fprintf(stderr,"%s",zHelp); + if( HAS_TIMER ){ + fprintf(stderr,"%s",zTimerHelp); + } + }else + + if( c=='i' && strncmp(azArg[0], "import", n)==0 && nArg==3 ){ + char *zTable = azArg[2]; /* Insert data into this table */ + char *zFile = azArg[1]; /* The file from which to extract data */ + sqlite3_stmt *pStmt = NULL; /* A statement */ + int nCol; /* Number of columns in the table */ + int nByte; /* Number of bytes in an SQL string */ + int i, j; /* Loop counters */ + int nSep; /* Number of bytes in p->separator[] */ + char *zSql; /* An SQL statement */ + char *zLine; /* A single line of input from the file */ + char **azCol; /* zLine[] broken up into columns */ + char *zCommit; /* How to commit changes */ + FILE *in; /* The input file */ + int lineno = 0; /* Line number of input file */ + + open_db(p); + nSep = strlen30(p->separator); + if( nSep==0 ){ + fprintf(stderr, "Error: non-null separator required for import\n"); + return 1; + } + zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable); + if( zSql==0 ){ + fprintf(stderr, "Error: out of memory\n"); + return 1; + } + nByte = strlen30(zSql); + rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0); + sqlite3_free(zSql); + if( rc ){ + if (pStmt) sqlite3_finalize(pStmt); + fprintf(stderr,"Error: %s\n", sqlite3_errmsg(db)); + return 1; + } + nCol = sqlite3_column_count(pStmt); + sqlite3_finalize(pStmt); + pStmt = 0; + if( nCol==0 ) return 0; /* no columns, no error */ + zSql = malloc( nByte + 20 + nCol*2 ); + if( zSql==0 ){ + fprintf(stderr, "Error: out of memory\n"); + return 1; + } + sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable); + j = strlen30(zSql); + for(i=1; idb, zSql, -1, &pStmt, 0); + free(zSql); + if( rc ){ + fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db)); + if (pStmt) sqlite3_finalize(pStmt); + return 1; + } + in = fopen(zFile, "rb"); + if( in==0 ){ + fprintf(stderr, "Error: cannot open \"%s\"\n", zFile); + sqlite3_finalize(pStmt); + return 1; + } + azCol = malloc( sizeof(azCol[0])*(nCol+1) ); + if( azCol==0 ){ + fprintf(stderr, "Error: out of memory\n"); + fclose(in); + sqlite3_finalize(pStmt); + return 1; + } + sqlite3_exec(p->db, "BEGIN", 0, 0, 0); + zCommit = "COMMIT"; + while( (zLine = local_getline(0, in))!=0 ){ + char *z; + i = 0; + lineno++; + azCol[0] = zLine; + for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){ + if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){ + *z = 0; + i++; + if( idb, zCommit, 0, 0, 0); + }else + + if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg<3 ){ + struct callback_data data; + char *zErrMsg = 0; + open_db(p); + memcpy(&data, p, sizeof(data)); + data.showHeader = 0; + data.mode = MODE_List; + if( nArg==1 ){ + rc = sqlite3_exec(p->db, + "SELECT name FROM sqlite_master " + "WHERE type='index' AND name NOT LIKE 'sqlite_%' " + "UNION ALL " + "SELECT name FROM sqlite_temp_master " + "WHERE type='index' " + "ORDER BY 1", + callback, &data, &zErrMsg + ); + }else{ + zShellStatic = azArg[1]; + rc = sqlite3_exec(p->db, + "SELECT name FROM sqlite_master " + "WHERE type='index' AND tbl_name LIKE shellstatic() " + "UNION ALL " + "SELECT name FROM sqlite_temp_master " + "WHERE type='index' AND tbl_name LIKE shellstatic() " + "ORDER BY 1", + callback, &data, &zErrMsg + ); + zShellStatic = 0; + } + if( zErrMsg ){ + fprintf(stderr,"Error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + }else if( rc != SQLITE_OK ){ + fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n"); + rc = 1; + } + }else + +#ifdef SQLITE_ENABLE_IOTRACE + if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){ + extern void (*sqlite3IoTrace)(const char*, ...); + if( iotrace && iotrace!=stdout ) fclose(iotrace); + iotrace = 0; + if( nArg<2 ){ + sqlite3IoTrace = 0; + }else if( strcmp(azArg[1], "-")==0 ){ + sqlite3IoTrace = iotracePrintf; + iotrace = stdout; + }else{ + iotrace = fopen(azArg[1], "w"); + if( iotrace==0 ){ + fprintf(stderr, "Error: cannot open \"%s\"\n", azArg[1]); + sqlite3IoTrace = 0; + rc = 1; + }else{ + sqlite3IoTrace = iotracePrintf; + } + } + }else +#endif + +#ifndef SQLITE_OMIT_LOAD_EXTENSION + if( c=='l' && strncmp(azArg[0], "load", n)==0 && nArg>=2 ){ + const char *zFile, *zProc; + char *zErrMsg = 0; + zFile = azArg[1]; + zProc = nArg>=3 ? azArg[2] : 0; + open_db(p); + rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg); + if( rc!=SQLITE_OK ){ + fprintf(stderr, "Error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + } + }else +#endif + + if( c=='l' && strncmp(azArg[0], "log", n)==0 && nArg>=1 ){ + const char *zFile = azArg[1]; + if( p->pLog && p->pLog!=stdout && p->pLog!=stderr ){ + fclose(p->pLog); + p->pLog = 0; + } + if( strcmp(zFile,"stdout")==0 ){ + p->pLog = stdout; + }else if( strcmp(zFile, "stderr")==0 ){ + p->pLog = stderr; + }else if( strcmp(zFile, "off")==0 ){ + p->pLog = 0; + }else{ + p->pLog = fopen(zFile, "w"); + if( p->pLog==0 ){ + fprintf(stderr, "Error: cannot open \"%s\"\n", zFile); + } + } + }else + + if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==2 ){ + int n2 = strlen30(azArg[1]); + if( (n2==4 && strncmp(azArg[1],"line",n2)==0) + || + (n2==5 && strncmp(azArg[1],"lines",n2)==0) ){ + p->mode = MODE_Line; + }else if( (n2==6 && strncmp(azArg[1],"column",n2)==0) + || + (n2==7 && strncmp(azArg[1],"columns",n2)==0) ){ + p->mode = MODE_Column; + }else if( n2==4 && strncmp(azArg[1],"list",n2)==0 ){ + p->mode = MODE_List; + }else if( n2==4 && strncmp(azArg[1],"html",n2)==0 ){ + p->mode = MODE_Html; + }else if( n2==3 && strncmp(azArg[1],"tcl",n2)==0 ){ + p->mode = MODE_Tcl; + }else if( n2==3 && strncmp(azArg[1],"csv",n2)==0 ){ + p->mode = MODE_Csv; + sqlite3_snprintf(sizeof(p->separator), p->separator, ","); + }else if( n2==4 && strncmp(azArg[1],"tabs",n2)==0 ){ + p->mode = MODE_List; + sqlite3_snprintf(sizeof(p->separator), p->separator, "\t"); + }else if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){ + p->mode = MODE_Insert; + set_table_name(p, "table"); + }else { + fprintf(stderr,"Error: mode should be one of: " + "column csv html insert line list tabs tcl\n"); + rc = 1; + } + }else + + if( c=='m' && strncmp(azArg[0], "mode", n)==0 && nArg==3 ){ + int n2 = strlen30(azArg[1]); + if( n2==6 && strncmp(azArg[1],"insert",n2)==0 ){ + p->mode = MODE_Insert; + set_table_name(p, azArg[2]); + }else { + fprintf(stderr, "Error: invalid arguments: " + " \"%s\". Enter \".help\" for help\n", azArg[2]); + rc = 1; + } + }else + + if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 && nArg==2 ) { + sqlite3_snprintf(sizeof(p->nullvalue), p->nullvalue, + "%.*s", (int)ArraySize(p->nullvalue)-1, azArg[1]); + }else + + if( c=='o' && strncmp(azArg[0], "output", n)==0 && nArg==2 ){ + if( p->out!=stdout ){ + fclose(p->out); + } + if( strcmp(azArg[1],"stdout")==0 ){ + p->out = stdout; + sqlite3_snprintf(sizeof(p->outfile), p->outfile, "stdout"); + }else{ + p->out = fopen(azArg[1], "wb"); + if( p->out==0 ){ + fprintf(stderr,"Error: cannot write to \"%s\"\n", azArg[1]); + p->out = stdout; + rc = 1; + } else { + sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", azArg[1]); + } + } + }else + + if( c=='p' && strncmp(azArg[0], "prompt", n)==0 && (nArg==2 || nArg==3)){ + if( nArg >= 2) { + strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1); + } + if( nArg >= 3) { + strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1); + } + }else + + if( c=='q' && strncmp(azArg[0], "quit", n)==0 && nArg==1 ){ + rc = 2; + }else + + if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 && nArg==2 ){ + FILE *alt = fopen(azArg[1], "rb"); + if( alt==0 ){ + fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]); + rc = 1; + }else{ + rc = process_input(p, alt); + fclose(alt); + } + }else + + if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 && nArg>1 && nArg<4){ + const char *zSrcFile; + const char *zDb; + sqlite3 *pSrc; + sqlite3_backup *pBackup; + int nTimeout = 0; + + if( nArg==2 ){ + zSrcFile = azArg[1]; + zDb = "main"; + }else{ + zSrcFile = azArg[2]; + zDb = azArg[1]; + } + rc = sqlite3_open(zSrcFile, &pSrc); + if( rc!=SQLITE_OK ){ + fprintf(stderr, "Error: cannot open \"%s\"\n", zSrcFile); + sqlite3_close(pSrc); + return 1; + } + open_db(p); + pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main"); + if( pBackup==0 ){ + fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); + sqlite3_close(pSrc); + return 1; + } + while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK + || rc==SQLITE_BUSY ){ + if( rc==SQLITE_BUSY ){ + if( nTimeout++ >= 3 ) break; + sqlite3_sleep(100); + } + } + sqlite3_backup_finish(pBackup); + if( rc==SQLITE_DONE ){ + rc = 0; + }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){ + fprintf(stderr, "Error: source database is busy\n"); + rc = 1; + }else{ + fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db)); + rc = 1; + } + sqlite3_close(pSrc); + }else + + if( c=='s' && strncmp(azArg[0], "schema", n)==0 && nArg<3 ){ + struct callback_data data; + char *zErrMsg = 0; + open_db(p); + memcpy(&data, p, sizeof(data)); + data.showHeader = 0; + data.mode = MODE_Semi; + if( nArg>1 ){ + int i; + for(i=0; azArg[1][i]; i++) azArg[1][i] = (char)tolower(azArg[1][i]); + if( strcmp(azArg[1],"sqlite_master")==0 ){ + char *new_argv[2], *new_colv[2]; + new_argv[0] = "CREATE TABLE sqlite_master (\n" + " type text,\n" + " name text,\n" + " tbl_name text,\n" + " rootpage integer,\n" + " sql text\n" + ")"; + new_argv[1] = 0; + new_colv[0] = "sql"; + new_colv[1] = 0; + callback(&data, 1, new_argv, new_colv); + rc = SQLITE_OK; + }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){ + char *new_argv[2], *new_colv[2]; + new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n" + " type text,\n" + " name text,\n" + " tbl_name text,\n" + " rootpage integer,\n" + " sql text\n" + ")"; + new_argv[1] = 0; + new_colv[0] = "sql"; + new_colv[1] = 0; + callback(&data, 1, new_argv, new_colv); + rc = SQLITE_OK; + }else{ + zShellStatic = azArg[1]; + rc = sqlite3_exec(p->db, + "SELECT sql FROM " + " (SELECT sql sql, type type, tbl_name tbl_name, name name" + " FROM sqlite_master UNION ALL" + " SELECT sql, type, tbl_name, name FROM sqlite_temp_master) " + "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL " + "ORDER BY substr(type,2,1), name", + callback, &data, &zErrMsg); + zShellStatic = 0; + } + }else{ + rc = sqlite3_exec(p->db, + "SELECT sql FROM " + " (SELECT sql sql, type type, tbl_name tbl_name, name name" + " FROM sqlite_master UNION ALL" + " SELECT sql, type, tbl_name, name FROM sqlite_temp_master) " + "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%'" + "ORDER BY substr(type,2,1), name", + callback, &data, &zErrMsg + ); + } + if( zErrMsg ){ + fprintf(stderr,"Error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + }else if( rc != SQLITE_OK ){ + fprintf(stderr,"Error: querying schema information\n"); + rc = 1; + }else{ + rc = 0; + } + }else + + if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){ + sqlite3_snprintf(sizeof(p->separator), p->separator, + "%.*s", (int)sizeof(p->separator)-1, azArg[1]); + }else + + if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){ + int i; + fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off"); + fprintf(p->out,"%9.9s: %s\n","explain", p->explainPrev.valid ? "on" :"off"); + fprintf(p->out,"%9.9s: %s\n","headers", p->showHeader ? "on" : "off"); + fprintf(p->out,"%9.9s: %s\n","mode", modeDescr[p->mode]); + fprintf(p->out,"%9.9s: ", "nullvalue"); + output_c_string(p->out, p->nullvalue); + fprintf(p->out, "\n"); + fprintf(p->out,"%9.9s: %s\n","output", + strlen30(p->outfile) ? p->outfile : "stdout"); + fprintf(p->out,"%9.9s: ", "separator"); + output_c_string(p->out, p->separator); + fprintf(p->out, "\n"); + fprintf(p->out,"%9.9s: %s\n","stats", p->statsOn ? "on" : "off"); + fprintf(p->out,"%9.9s: ","width"); + for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) { + fprintf(p->out,"%d ",p->colWidth[i]); + } + fprintf(p->out,"\n"); + }else + + if( c=='s' && strncmp(azArg[0], "stats", n)==0 && nArg>1 && nArg<3 ){ + p->statsOn = booleanValue(azArg[1]); + }else + + if( c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0 && nArg<3 ){ + char **azResult; + int nRow; + char *zErrMsg; + open_db(p); + if( nArg==1 ){ + rc = sqlite3_get_table(p->db, + "SELECT name FROM sqlite_master " + "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' " + "UNION ALL " + "SELECT name FROM sqlite_temp_master " + "WHERE type IN ('table','view') " + "ORDER BY 1", + &azResult, &nRow, 0, &zErrMsg + ); + }else{ + zShellStatic = azArg[1]; + rc = sqlite3_get_table(p->db, + "SELECT name FROM sqlite_master " + "WHERE type IN ('table','view') AND name LIKE shellstatic() " + "UNION ALL " + "SELECT name FROM sqlite_temp_master " + "WHERE type IN ('table','view') AND name LIKE shellstatic() " + "ORDER BY 1", + &azResult, &nRow, 0, &zErrMsg + ); + zShellStatic = 0; + } + if( zErrMsg ){ + fprintf(stderr,"Error: %s\n", zErrMsg); + sqlite3_free(zErrMsg); + rc = 1; + }else if( rc != SQLITE_OK ){ + fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n"); + rc = 1; + }else{ + int len, maxlen = 0; + int i, j; + int nPrintCol, nPrintRow; + for(i=1; i<=nRow; i++){ + if( azResult[i]==0 ) continue; + len = strlen30(azResult[i]); + if( len>maxlen ) maxlen = len; + } + nPrintCol = 80/(maxlen+2); + if( nPrintCol<1 ) nPrintCol = 1; + nPrintRow = (nRow + nPrintCol - 1)/nPrintCol; + for(i=0; i=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){ + int testctrl = -1; + int rc = 0; + open_db(p); + + /* convert testctrl text option to value. allow only the first + ** three characters of the option to be used or the numerical + ** value. */ + if( strncmp( azArg[1], "prng_save", 6 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_SAVE; + else if( strncmp( azArg[1], "prng_restore", 10 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_RESTORE; + else if( strncmp( azArg[1], "prng_reset", 10 )==0 ) testctrl = SQLITE_TESTCTRL_PRNG_RESET; + else if( strncmp( azArg[1], "bitvec_test", 6 )==3 ) testctrl = SQLITE_TESTCTRL_BITVEC_TEST; + else if( strncmp( azArg[1], "fault_install", 6 )==3 ) testctrl = SQLITE_TESTCTRL_FAULT_INSTALL; + else if( strncmp( azArg[1], "benign_malloc_hooks", 3 )==0 ) testctrl = SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS; + else if( strncmp( azArg[1], "pending_byte", 3 )==0 ) testctrl = SQLITE_TESTCTRL_PENDING_BYTE; + else if( strncmp( azArg[1], "assert", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ASSERT; + else if( strncmp( azArg[1], "always", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ALWAYS; + else if( strncmp( azArg[1], "reserve", 3 )==0 ) testctrl = SQLITE_TESTCTRL_RESERVE; + else if( strncmp( azArg[1], "optimizations", 3 )==0 ) testctrl = SQLITE_TESTCTRL_OPTIMIZATIONS; + else if( strncmp( azArg[1], "iskeyword", 3 )==0 ) testctrl = SQLITE_TESTCTRL_ISKEYWORD; + else if( strncmp( azArg[1], "pghdrsz", 3 )==0 ) testctrl = SQLITE_TESTCTRL_PGHDRSZ; + else if( strncmp( azArg[1], "scratchmalloc", 3 )==0 ) testctrl = SQLITE_TESTCTRL_SCRATCHMALLOC; + else testctrl = atoi(azArg[1]); + + if( (testctrlSQLITE_TESTCTRL_LAST) ){ + fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]); + }else{ + switch(testctrl){ + + /* sqlite3_test_control(int, db, int) */ + case SQLITE_TESTCTRL_OPTIMIZATIONS: + case SQLITE_TESTCTRL_RESERVE: + if( nArg==3 ){ + int opt = (int)strtol(azArg[2], 0, 0); + rc = sqlite3_test_control(testctrl, p->db, opt); + printf("%d (0x%08x)\n", rc, rc); + } else { + fprintf(stderr,"Error: testctrl %s takes a single int option\n", azArg[1]); + } + break; + + /* sqlite3_test_control(int) */ + case SQLITE_TESTCTRL_PRNG_SAVE: + case SQLITE_TESTCTRL_PRNG_RESTORE: + case SQLITE_TESTCTRL_PRNG_RESET: + case SQLITE_TESTCTRL_PGHDRSZ: + if( nArg==2 ){ + rc = sqlite3_test_control(testctrl); + printf("%d (0x%08x)\n", rc, rc); + } else { + fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]); + } + break; + + /* sqlite3_test_control(int, uint) */ + case SQLITE_TESTCTRL_PENDING_BYTE: + if( nArg==3 ){ + unsigned int opt = (unsigned int)atoi(azArg[2]); + rc = sqlite3_test_control(testctrl, opt); + printf("%d (0x%08x)\n", rc, rc); + } else { + fprintf(stderr,"Error: testctrl %s takes a single unsigned int option\n", azArg[1]); + } + break; + + /* sqlite3_test_control(int, int) */ + case SQLITE_TESTCTRL_ASSERT: + case SQLITE_TESTCTRL_ALWAYS: + if( nArg==3 ){ + int opt = atoi(azArg[2]); + rc = sqlite3_test_control(testctrl, opt); + printf("%d (0x%08x)\n", rc, rc); + } else { + fprintf(stderr,"Error: testctrl %s takes a single int option\n", azArg[1]); + } + break; + + /* sqlite3_test_control(int, char *) */ +#ifdef SQLITE_N_KEYWORD + case SQLITE_TESTCTRL_ISKEYWORD: + if( nArg==3 ){ + const char *opt = azArg[2]; + rc = sqlite3_test_control(testctrl, opt); + printf("%d (0x%08x)\n", rc, rc); + } else { + fprintf(stderr,"Error: testctrl %s takes a single char * option\n", azArg[1]); + } + break; +#endif + + case SQLITE_TESTCTRL_BITVEC_TEST: + case SQLITE_TESTCTRL_FAULT_INSTALL: + case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: + case SQLITE_TESTCTRL_SCRATCHMALLOC: + default: + fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n", azArg[1]); + break; + } + } + }else + + if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 && nArg==2 ){ + open_db(p); + sqlite3_busy_timeout(p->db, atoi(azArg[1])); + }else + + if( HAS_TIMER && c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 && nArg==2 ){ + enableTimer = booleanValue(azArg[1]); + }else + + if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){ + int j; + assert( nArg<=ArraySize(azArg) ); + for(j=1; jcolWidth); j++){ + p->colWidth[j-1] = atoi(azArg[j]); + } + }else + + { + fprintf(stderr, "Error: unknown command or invalid arguments: " + " \"%s\". Enter \".help\" for help\n", azArg[0]); + rc = 1; + } + + return rc; +} + +/* +** Return TRUE if a semicolon occurs anywhere in the first N characters +** of string z[]. +*/ +static int _contains_semicolon(const char *z, int N){ + int i; + for(i=0; iout); + free(zLine); + zLine = one_input_line(zSql, in); + if( zLine==0 ){ + break; /* We have reached EOF */ + } + if( seenInterrupt ){ + if( in!=0 ) break; + seenInterrupt = 0; + } + lineno++; + if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue; + if( zLine && zLine[0]=='.' && nSql==0 ){ + if( p->echoOn ) printf("%s\n", zLine); + rc = do_meta_command(zLine, p); + if( rc==2 ){ /* exit requested */ + break; + }else if( rc ){ + errCnt++; + } + continue; + } + if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){ + memcpy(zLine,";",2); + } + nSqlPrior = nSql; + if( zSql==0 ){ + int i; + for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){} + if( zLine[i]!=0 ){ + nSql = strlen30(zLine); + zSql = malloc( nSql+3 ); + if( zSql==0 ){ + fprintf(stderr, "Error: out of memory\n"); + exit(1); + } + memcpy(zSql, zLine, nSql+1); + startline = lineno; + } + }else{ + int len = strlen30(zLine); + zSql = realloc( zSql, nSql + len + 4 ); + if( zSql==0 ){ + fprintf(stderr,"Error: out of memory\n"); + exit(1); + } + zSql[nSql++] = '\n'; + memcpy(&zSql[nSql], zLine, len+1); + nSql += len; + } + if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior) + && sqlite3_complete(zSql) ){ + p->cnt = 0; + open_db(p); + BEGIN_TIMER; + rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg); + END_TIMER; + if( rc || zErrMsg ){ + char zPrefix[100]; + if( in!=0 || !stdin_is_interactive ){ + sqlite3_snprintf(sizeof(zPrefix), zPrefix, + "Error: near line %d:", startline); + }else{ + sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:"); + } + if( zErrMsg!=0 ){ + fprintf(stderr, "%s %s\n", zPrefix, zErrMsg); + sqlite3_free(zErrMsg); + zErrMsg = 0; + }else{ + fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db)); + } + errCnt++; + } + free(zSql); + zSql = 0; + nSql = 0; + } + } + if( zSql ){ + if( !_all_whitespace(zSql) ) fprintf(stderr, "Error: incomplete SQL: %s\n", zSql); + free(zSql); + } + free(zLine); + return errCnt; +} + +/* +** Return a pathname which is the user's home directory. A +** 0 return indicates an error of some kind. Space to hold the +** resulting string is obtained from malloc(). The calling +** function should free the result. +*/ +static char *find_home_dir(void){ + char *home_dir = NULL; + +#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(_WIN32_WCE) && !defined(__RTP__) && !defined(_WRS_KERNEL) + struct passwd *pwent; + uid_t uid = getuid(); + if( (pwent=getpwuid(uid)) != NULL) { + home_dir = pwent->pw_dir; + } +#endif + +#if defined(_WIN32_WCE) + /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv() + */ + home_dir = strdup("/"); +#else + +#if defined(_WIN32) || defined(WIN32) || defined(__OS2__) + if (!home_dir) { + home_dir = getenv("USERPROFILE"); + } +#endif + + if (!home_dir) { + home_dir = getenv("HOME"); + } + +#if defined(_WIN32) || defined(WIN32) || defined(__OS2__) + if (!home_dir) { + char *zDrive, *zPath; + int n; + zDrive = getenv("HOMEDRIVE"); + zPath = getenv("HOMEPATH"); + if( zDrive && zPath ){ + n = strlen30(zDrive) + strlen30(zPath) + 1; + home_dir = malloc( n ); + if( home_dir==0 ) return 0; + sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath); + return home_dir; + } + home_dir = "c:\\"; + } +#endif + +#endif /* !_WIN32_WCE */ + + if( home_dir ){ + int n = strlen30(home_dir) + 1; + char *z = malloc( n ); + if( z ) memcpy(z, home_dir, n); + home_dir = z; + } + + return home_dir; +} + +/* +** Read input from the file given by sqliterc_override. Or if that +** parameter is NULL, take input from ~/.sqliterc +** +** Returns the number of errors. +*/ +static int process_sqliterc( + struct callback_data *p, /* Configuration data */ + const char *sqliterc_override /* Name of config file. NULL to use default */ +){ + char *home_dir = NULL; + const char *sqliterc = sqliterc_override; + char *zBuf = 0; + FILE *in = NULL; + int nBuf; + int rc = 0; + + if (sqliterc == NULL) { + home_dir = find_home_dir(); + if( home_dir==0 ){ +#if !defined(__RTP__) && !defined(_WRS_KERNEL) + fprintf(stderr,"%s: Error: cannot locate your home directory\n", Argv0); +#endif + return 1; + } + nBuf = strlen30(home_dir) + 16; + zBuf = malloc( nBuf ); + if( zBuf==0 ){ + fprintf(stderr,"%s: Error: out of memory\n",Argv0); + return 1; + } + sqlite3_snprintf(nBuf, zBuf,"%s/.sqliterc",home_dir); + free(home_dir); + sqliterc = (const char*)zBuf; + } + in = fopen(sqliterc,"rb"); + if( in ){ + if( stdin_is_interactive ){ + fprintf(stderr,"-- Loading resources from %s\n",sqliterc); + } + rc = process_input(p,in); + fclose(in); + } + free(zBuf); + return rc; +} + +/* +** Show available command line options +*/ +static const char zOptions[] = + " -help show this message\n" + " -init filename read/process named file\n" + " -echo print commands before execution\n" + " -[no]header turn headers on or off\n" + " -bail stop after hitting an error\n" + " -interactive force interactive I/O\n" + " -batch force batch I/O\n" + " -column set output mode to 'column'\n" + " -csv set output mode to 'csv'\n" + " -html set output mode to HTML\n" + " -line set output mode to 'line'\n" + " -list set output mode to 'list'\n" + " -separator 'x' set output field separator (|)\n" + " -stats print memory stats before each finalize\n" + " -nullvalue 'text' set text string for NULL values\n" + " -version show SQLite version\n" + " -vfs NAME use NAME as the default VFS\n" +#ifdef SQLITE_ENABLE_VFSTRACE + " -vfstrace enable tracing of all VFS calls\n" +#endif +; +static void usage(int showDetail){ + fprintf(stderr, + "Usage: %s [OPTIONS] FILENAME [SQL]\n" + "FILENAME is the name of an SQLite database. A new database is created\n" + "if the file does not previously exist.\n", Argv0); + if( showDetail ){ + fprintf(stderr, "OPTIONS include:\n%s", zOptions); + }else{ + fprintf(stderr, "Use the -help option for additional information\n"); + } + exit(1); +} + +/* +** Initialize the state information in data +*/ +static void main_init(struct callback_data *data) { + memset(data, 0, sizeof(*data)); + data->mode = MODE_List; + memcpy(data->separator,"|", 2); + data->showHeader = 0; + sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data); + sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> "); + sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> "); + sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); +} + +int main(int argc, char **argv){ + char *zErrMsg = 0; + struct callback_data data; + const char *zInitFile = 0; + char *zFirstCmd = 0; + int i; + int rc = 0; + + Argv0 = argv[0]; + main_init(&data); + stdin_is_interactive = isatty(0); + + /* Make sure we have a valid signal handler early, before anything + ** else is done. + */ +#ifdef SIGINT + signal(SIGINT, interrupt_handler); +#endif + + /* Do an initial pass through the command-line argument to locate + ** the name of the database file, the name of the initialization file, + ** the size of the alternative malloc heap, + ** and the first command to execute. + */ + for(i=1; i0x7fff0000 ) szHeap = 0x7fff0000; +#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) + sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64); +#endif +#ifdef SQLITE_ENABLE_VFSTRACE + }else if( strcmp(argv[i],"-vfstrace")==0 ){ + extern int vfstrace_register( + const char *zTraceName, + const char *zOldVfsName, + int (*xOut)(const char*,void*), + void *pOutArg, + int makeDefault + ); + vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1); +#endif + }else if( strcmp(argv[i],"-vfs")==0 ){ + sqlite3_vfs *pVfs = sqlite3_vfs_find(argv[++i]); + if( pVfs ){ + sqlite3_vfs_register(pVfs, 1); + }else{ + fprintf(stderr, "no such VFS: \"%s\"\n", argv[i]); + exit(1); + } + } + } + if( i0 ){ + return rc; + } + + /* Make a second pass through the command-line argument and set + ** options. This second pass is delayed until after the initialization + ** file is processed so that the command-line arguments will override + ** settings in the initialization file. + */ + for(i=1; i=argc){ + fprintf(stderr,"%s: Error: missing argument for option: %s\n", Argv0, z); + fprintf(stderr,"Use -help for a list of options.\n"); + return 1; + } + sqlite3_snprintf(sizeof(data.separator), data.separator, + "%.*s",(int)sizeof(data.separator)-1,argv[i]); + }else if( strcmp(z,"-nullvalue")==0 ){ + i++; + if(i>=argc){ + fprintf(stderr,"%s: Error: missing argument for option: %s\n", Argv0, z); + fprintf(stderr,"Use -help for a list of options.\n"); + return 1; + } + sqlite3_snprintf(sizeof(data.nullvalue), data.nullvalue, + "%.*s",(int)sizeof(data.nullvalue)-1,argv[i]); + }else if( strcmp(z,"-header")==0 ){ + data.showHeader = 1; + }else if( strcmp(z,"-noheader")==0 ){ + data.showHeader = 0; + }else if( strcmp(z,"-echo")==0 ){ + data.echoOn = 1; + }else if( strcmp(z,"-stats")==0 ){ + data.statsOn = 1; + }else if( strcmp(z,"-bail")==0 ){ + bail_on_error = 1; + }else if( strcmp(z,"-version")==0 ){ + printf("%s\n", sqlite3_libversion()); + return 0; + }else if( strcmp(z,"-interactive")==0 ){ + stdin_is_interactive = 1; + }else if( strcmp(z,"-batch")==0 ){ + stdin_is_interactive = 0; + }else if( strcmp(z,"-heap")==0 ){ + i++; + }else if( strcmp(z,"-vfs")==0 ){ + i++; + }else if( strcmp(z,"-vfstrace")==0 ){ + i++; + }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){ + usage(1); + }else{ + fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z); + fprintf(stderr,"Use -help for a list of options.\n"); + return 1; + } + } + + if( zFirstCmd ){ + /* Run just the command that follows the database name + */ + if( zFirstCmd[0]=='.' ){ + rc = do_meta_command(zFirstCmd, &data); + }else{ + open_db(&data); + rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg); + if( zErrMsg!=0 ){ + fprintf(stderr,"Error: %s\n", zErrMsg); + return rc!=0 ? rc : 1; + }else if( rc!=0 ){ + fprintf(stderr,"Error: unable to process SQL \"%s\"\n", zFirstCmd); + return rc; + } + } + }else{ + /* Run commands received from standard input + */ + if( stdin_is_interactive ){ + char *zHome; + char *zHistory = 0; + int nHistory; + printf( + "SQLite version %s\n" + "Enter \".help\" for instructions\n" + "Enter SQL statements terminated with a \";\"\n", + sqlite3_libversion() + ); + zHome = find_home_dir(); + if( zHome ){ + nHistory = strlen30(zHome) + 20; + if( (zHistory = malloc(nHistory))!=0 ){ + sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); + } + } +#if defined(HAVE_READLINE) && HAVE_READLINE==1 + if( zHistory ) read_history(zHistory); +#endif + rc = process_input(&data, 0); + if( zHistory ){ + stifle_history(100); + write_history(zHistory); + free(zHistory); + } + free(zHome); + }else{ + rc = process_input(&data, stdin); + } + } + set_table_name(&data, 0); + if( data.db ){ + sqlite3_close(data.db); + } + return rc; +} diff --git a/fdbserver/sqlite/sqlite3.amalgamation.c b/fdbserver/sqlite/sqlite3.amalgamation.c new file mode 100644 index 0000000000..de4b00b4a9 --- /dev/null +++ b/fdbserver/sqlite/sqlite3.amalgamation.c @@ -0,0 +1,110686 @@ +#ifndef NDEBUG + #define SQLITE_DEBUG 1 +#endif +#define SQLITE_THREADSAFE 0 +#define ENABLE_SCRATCHALLOC_CHECK 0 +#define SQLITE_OMIT_SHARED_CACHE 1 +#define SQLITE_FILE_HEADER "FoundationDB100" +#define HAVE_USLEEP 1 + +/****************************************************************************** +** This file is an amalgamation of many separate C source files from SQLite +** version 3.7.6. By combining all the individual C code files into this +** single large file, the entire code can be compiled as a one translation +** unit. This allows many compilers to do optimizations that would not be +** possible if the files were compiled separately. Performance improvements +** of 5% or more are commonly seen when SQLite is compiled as a single +** translation unit. +** +** This file is all you need to compile SQLite. To use SQLite in other +** programs, you need this file and the "sqlite3.h" header file that defines +** the programming interface to the SQLite library. (If you do not have +** the "sqlite3.h" header file at hand, you will find a copy embedded within +** the text of this file. Search for "Begin file sqlite3.h" to find the start +** of the embedded sqlite3.h header file.) Additional code files may be needed +** if you want a wrapper to interface SQLite with your choice of programming +** language. The code for the "sqlite3" command-line shell is also in a +** separate file. This file contains only code for the core SQLite library. +*/ +#define SQLITE_CORE 1 +#define SQLITE_AMALGAMATION 1 +#ifndef SQLITE_PRIVATE +# define SQLITE_PRIVATE //static +#endif +#ifndef SQLITE_API +# define SQLITE_API +#endif +/************** Begin file sqliteInt.h ***************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Internal interface definitions for SQLite. +** +*/ +#ifndef _SQLITEINT_H_ +#define _SQLITEINT_H_ + +/* +** These #defines should enable >2GB file support on POSIX if the +** underlying operating system supports it. If the OS lacks +** large file support, or if the OS is windows, these should be no-ops. +** +** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any +** system #includes. Hence, this block of code must be the very first +** code in all source files. +** +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch +** on the compiler command line. This is necessary if you are compiling +** on a recent machine (ex: Red Hat 7.2) but you want your code to work +** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2 +** without this option, LFS is enable. But LFS does not exist in the kernel +** in Red Hat 6.0, so the code won't work. Hence, for maximum binary +** portability you should omit LFS. +** +** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later. +*/ +#ifndef SQLITE_DISABLE_LFS +# define _LARGE_FILE 1 +# ifndef _FILE_OFFSET_BITS +# define _FILE_OFFSET_BITS 64 +# endif +# define _LARGEFILE_SOURCE 1 +#endif + +/* +** Include the configuration header output by 'configure' if we're using the +** autoconf-based build +*/ +#ifdef _HAVE_SQLITE_CONFIG_H +#include "config.h" +#endif + +/************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/ +/************** Begin file sqliteLimit.h *************************************/ +/* +** 2007 May 7 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file defines various limits of what SQLite can process. +*/ + +/* +** The maximum length of a TEXT or BLOB in bytes. This also +** limits the size of a row in a table or index. +** +** The hard limit is the ability of a 32-bit signed integer +** to count the size: 2^31-1 or 2147483647. +*/ +#ifndef SQLITE_MAX_LENGTH +# define SQLITE_MAX_LENGTH 1000000000 +#endif + +/* +** This is the maximum number of +** +** * Columns in a table +** * Columns in an index +** * Columns in a view +** * Terms in the SET clause of an UPDATE statement +** * Terms in the result set of a SELECT statement +** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement. +** * Terms in the VALUES clause of an INSERT statement +** +** The hard upper limit here is 32676. Most database people will +** tell you that in a well-normalized database, you usually should +** not have more than a dozen or so columns in any table. And if +** that is the case, there is no point in having more than a few +** dozen values in any of the other situations described above. +*/ +#ifndef SQLITE_MAX_COLUMN +# define SQLITE_MAX_COLUMN 2000 +#endif + +/* +** The maximum length of a single SQL statement in bytes. +** +** It used to be the case that setting this value to zero would +** turn the limit off. That is no longer true. It is not possible +** to turn this limit off. +*/ +#ifndef SQLITE_MAX_SQL_LENGTH +# define SQLITE_MAX_SQL_LENGTH 1000000000 +#endif + +/* +** The maximum depth of an expression tree. This is limited to +** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might +** want to place more severe limits on the complexity of an +** expression. +** +** A value of 0 used to mean that the limit was not enforced. +** But that is no longer true. The limit is now strictly enforced +** at all times. +*/ +#ifndef SQLITE_MAX_EXPR_DEPTH +# define SQLITE_MAX_EXPR_DEPTH 1000 +#endif + +/* +** The maximum number of terms in a compound SELECT statement. +** The code generator for compound SELECT statements does one +** level of recursion for each term. A stack overflow can result +** if the number of terms is too large. In practice, most SQL +** never has more than 3 or 4 terms. Use a value of 0 to disable +** any limit on the number of terms in a compount SELECT. +*/ +#ifndef SQLITE_MAX_COMPOUND_SELECT +# define SQLITE_MAX_COMPOUND_SELECT 500 +#endif + +/* +** The maximum number of opcodes in a VDBE program. +** Not currently enforced. +*/ +#ifndef SQLITE_MAX_VDBE_OP +# define SQLITE_MAX_VDBE_OP 25000 +#endif + +/* +** The maximum number of arguments to an SQL function. +*/ +#ifndef SQLITE_MAX_FUNCTION_ARG +# define SQLITE_MAX_FUNCTION_ARG 127 +#endif + +/* +** The maximum number of in-memory pages to use for the main database +** table and for temporary tables. The SQLITE_DEFAULT_CACHE_SIZE +*/ +#ifndef SQLITE_DEFAULT_CACHE_SIZE +# define SQLITE_DEFAULT_CACHE_SIZE 2000 +#endif +#ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE +# define SQLITE_DEFAULT_TEMP_CACHE_SIZE 500 +#endif + +/* +** The default number of frames to accumulate in the log file before +** checkpointing the database in WAL mode. +*/ +#ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT +# define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT 1000 +#endif + +/* +** The maximum number of attached databases. This must be between 0 +** and 30. The upper bound on 30 is because a 32-bit integer bitmap +** is used internally to track attached databases. +*/ +#ifndef SQLITE_MAX_ATTACHED +# define SQLITE_MAX_ATTACHED 10 +#endif + + +/* +** The maximum value of a ?nnn wildcard that the parser will accept. +*/ +#ifndef SQLITE_MAX_VARIABLE_NUMBER +# define SQLITE_MAX_VARIABLE_NUMBER 999 +#endif + +/* Maximum page size. The upper bound on this value is 65536. This a limit +** imposed by the use of 16-bit offsets within each page. +** +** Earlier versions of SQLite allowed the user to change this value at +** compile time. This is no longer permitted, on the grounds that it creates +** a library that is technically incompatible with an SQLite library +** compiled with a different limit. If a process operating on a database +** with a page-size of 65536 bytes crashes, then an instance of SQLite +** compiled with the default page-size limit will not be able to rollback +** the aborted transaction. This could lead to database corruption. +*/ +#ifdef SQLITE_MAX_PAGE_SIZE +# undef SQLITE_MAX_PAGE_SIZE +#endif +#define SQLITE_MAX_PAGE_SIZE 65536 + + +/* +** The default size of a database page. +*/ +#ifndef SQLITE_DEFAULT_PAGE_SIZE +# define SQLITE_DEFAULT_PAGE_SIZE 1024 +#endif +#if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE +# undef SQLITE_DEFAULT_PAGE_SIZE +# define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE +#endif + +/* +** Ordinarily, if no value is explicitly provided, SQLite creates databases +** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain +** device characteristics (sector-size and atomic write() support), +** SQLite may choose a larger value. This constant is the maximum value +** SQLite will choose on its own. +*/ +#ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE +# define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192 +#endif +#if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE +# undef SQLITE_MAX_DEFAULT_PAGE_SIZE +# define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE +#endif + + +/* +** Maximum number of pages in one database file. +** +** This is really just the default value for the max_page_count pragma. +** This value can be lowered (or raised) at run-time using that the +** max_page_count macro. +*/ +#ifndef SQLITE_MAX_PAGE_COUNT +# define SQLITE_MAX_PAGE_COUNT 1073741823 +#endif + +/* +** Maximum length (in bytes) of the pattern in a LIKE or GLOB +** operator. +*/ +#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH +# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000 +#endif + +/* +** Maximum depth of recursion for triggers. +** +** A value of 1 means that a trigger program will not be able to itself +** fire any triggers. A value of 0 means that no trigger programs at all +** may be executed. +*/ +#ifndef SQLITE_MAX_TRIGGER_DEPTH +# define SQLITE_MAX_TRIGGER_DEPTH 1000 +#endif + +/************** End of sqliteLimit.h *****************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ + +/* Disable nuisance warnings on Borland compilers */ +#if defined(__BORLANDC__) +#pragma warn -rch /* unreachable code */ +#pragma warn -ccc /* Condition is always true or false */ +#pragma warn -aus /* Assigned value is never used */ +#pragma warn -csu /* Comparing signed and unsigned */ +#pragma warn -spa /* Suspicious pointer arithmetic */ +#endif + +/* Needed for various definitions... */ +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + +/* +** Include standard header files as necessary +*/ +#ifdef HAVE_STDINT_H +#include +#endif +#ifdef HAVE_INTTYPES_H +#include +#endif + +/* +** The number of samples of an index that SQLite takes in order to +** construct a histogram of the table content when running ANALYZE +** and with SQLITE_ENABLE_STAT2 +*/ +#define SQLITE_INDEX_SAMPLES 10 + +/* +** The following macros are used to cast pointers to integers and +** integers to pointers. The way you do this varies from one compiler +** to the next, so we have developed the following set of #if statements +** to generate appropriate macros for a wide range of compilers. +** +** The correct "ANSI" way to do this is to use the intptr_t type. +** Unfortunately, that typedef is not available on all compilers, or +** if it is available, it requires an #include of specific headers +** that vary from one machine to the next. +** +** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on +** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)). +** So we have to define the macros in different ways depending on the +** compiler. +*/ +#if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */ +# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X)) +# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X)) +#elif !defined(__GNUC__) /* Works for compilers other than LLVM */ +# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X]) +# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0)) +#elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */ +# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X)) +# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X)) +#else /* Generates a warning - but it always works */ +# define SQLITE_INT_TO_PTR(X) ((void*)(X)) +# define SQLITE_PTR_TO_INT(X) ((int)(X)) +#endif + +/* +** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. +** 0 means mutexes are permanently disable and the library is never +** threadsafe. 1 means the library is serialized which is the highest +** level of threadsafety. 2 means the libary is multithreaded - multiple +** threads can use SQLite as long as no two threads try to use the same +** database connection at the same time. +** +** Older versions of SQLite used an optional THREADSAFE macro. +** We support that for legacy. +*/ +#if !defined(SQLITE_THREADSAFE) +#if defined(THREADSAFE) +# define SQLITE_THREADSAFE THREADSAFE +#else +# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */ +#endif +#endif + +/* +** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1. +** It determines whether or not the features related to +** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can +** be overridden at runtime using the sqlite3_config() API. +*/ +/* FIXME: this needs to be true for better tracking memory use in our + sqlite threads, but causes large slowdowns due to mutex + contention. Disabled for now, but a better long term solution is + required. */ +#if !defined(SQLITE_DEFAULT_MEMSTATUS) +# define SQLITE_DEFAULT_MEMSTATUS 0 +#endif + +/* +** Exactly one of the following macros must be defined in order to +** specify which memory allocation subsystem to use. +** +** SQLITE_SYSTEM_MALLOC // Use normal system malloc() +** SQLITE_MEMDEBUG // Debugging version of system malloc() +** +** (Historical note: There used to be several other options, but we've +** pared it down to just these two.) +** +** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as +** the default. +*/ +#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)>1 +# error "At most one of the following compile-time configuration options\ + is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG" +#endif +#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)==0 +# define SQLITE_SYSTEM_MALLOC 1 +#endif + +/* +** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the +** sizes of memory allocations below this value where possible. +*/ +#if !defined(SQLITE_MALLOC_SOFT_LIMIT) +# define SQLITE_MALLOC_SOFT_LIMIT 1024 +#endif + +/* +** We need to define _XOPEN_SOURCE as follows in order to enable +** recursive mutexes on most Unix systems. But Mac OS X is different. +** The _XOPEN_SOURCE define causes problems for Mac OS X we are told, +** so it is omitted there. See ticket #2673. +** +** Later we learn that _XOPEN_SOURCE is poorly or incorrectly +** implemented on some systems. So we avoid defining it at all +** if it is already defined or if it is unneeded because we are +** not doing a threadsafe build. Ticket #2681. +** +** See also ticket #2741. +*/ +#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE +# define _XOPEN_SOURCE 500 /* Needed to enable pthread recursive mutexes */ +#endif + +/* +** The TCL headers are only needed when compiling the TCL bindings. +*/ +#if defined(SQLITE_TCL) || defined(TCLSH) +# include +#endif + +/* +** Many people are failing to set -DNDEBUG=1 when compiling SQLite. +** Setting NDEBUG makes the code smaller and run faster. So the following +** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1 +** option is set. Thus NDEBUG becomes an opt-in rather than an opt-out +** feature. +*/ +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) +# define NDEBUG 1 +#endif +#if defined(NDEBUG) && defined(SQLITE_DEBUG) +# undef NDEBUG +#endif + +/* +** The testcase() macro is used to aid in coverage testing. When +** doing coverage testing, the condition inside the argument to +** testcase() must be evaluated both true and false in order to +** get full branch coverage. The testcase() macro is inserted +** to help ensure adequate test coverage in places where simple +** condition/decision coverage is inadequate. For example, testcase() +** can be used to make sure boundary values are tested. For +** bitmask tests, testcase() can be used to make sure each bit +** is significant and used at least once. On switch statements +** where multiple cases go to the same block of code, testcase() +** can insure that all cases are evaluated. +** +*/ +#ifdef SQLITE_COVERAGE_TEST +SQLITE_PRIVATE void sqlite3Coverage(int); +# define testcase(X) if( X ){ sqlite3Coverage(__LINE__); } +#else +# define testcase(X) +#endif + +/* +** The TESTONLY macro is used to enclose variable declarations or +** other bits of code that are needed to support the arguments +** within testcase() and assert() macros. +*/ +#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST) +# define TESTONLY(X) X +#else +# define TESTONLY(X) +#endif + +/* +** Sometimes we need a small amount of code such as a variable initialization +** to setup for a later assert() statement. We do not want this code to +** appear when assert() is disabled. The following macro is therefore +** used to contain that setup code. The "VVA" acronym stands for +** "Verification, Validation, and Accreditation". In other words, the +** code within VVA_ONLY() will only run during verification processes. +*/ +#ifndef NDEBUG +# define VVA_ONLY(X) X +#else +# define VVA_ONLY(X) +#endif + +/* +** The ALWAYS and NEVER macros surround boolean expressions which +** are intended to always be true or false, respectively. Such +** expressions could be omitted from the code completely. But they +** are included in a few cases in order to enhance the resilience +** of SQLite to unexpected behavior - to make the code "self-healing" +** or "ductile" rather than being "brittle" and crashing at the first +** hint of unplanned behavior. +** +** In other words, ALWAYS and NEVER are added for defensive code. +** +** When doing coverage testing ALWAYS and NEVER are hard-coded to +** be true and false so that the unreachable code then specify will +** not be counted as untested code. +*/ +#if defined(SQLITE_COVERAGE_TEST) +# define ALWAYS(X) (1) +# define NEVER(X) (0) +#elif !defined(NDEBUG) +# define ALWAYS(X) ((X)?1:(assert(0),0)) +# define NEVER(X) ((X)?(assert(0),1):0) +#else +# define ALWAYS(X) ((X) != 0) +# define NEVER(X) ((X) != 0) +#endif + +/* +** Return true (non-zero) if the input is a integer that is too large +** to fit in 32-bits. This macro is used inside of various testcase() +** macros to verify that we have tested SQLite for large-file support. +*/ +#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0) + +/* +** The macro unlikely() is a hint that surrounds a boolean +** expression that is usually false. Macro likely() surrounds +** a boolean expression that is usually true. GCC is able to +** use these hints to generate better code, sometimes. +*/ +#if defined(__GNUC__) && 0 +# define likely(X) __builtin_expect((X),1) +# define unlikely(X) __builtin_expect((X),0) +#else +# define likely(X) !!(X) +# define unlikely(X) !!(X) +#endif + +/************** Include sqlite3.h in the middle of sqliteInt.h ***************/ +#include "sqlite3.h" + +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include hash.h in the middle of sqliteInt.h ******************/ +/************** Begin file hash.h ********************************************/ +/* +** 2001 September 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This is the header file for the generic hash-table implemenation +** used in SQLite. +*/ +#ifndef _SQLITE_HASH_H_ +#define _SQLITE_HASH_H_ + +/* Forward declarations of structures. */ +typedef struct Hash Hash; +typedef struct HashElem HashElem; + +/* A complete hash table is an instance of the following structure. +** The internals of this structure are intended to be opaque -- client +** code should not attempt to access or modify the fields of this structure +** directly. Change this structure only by using the routines below. +** However, some of the "procedures" and "functions" for modifying and +** accessing this structure are really macros, so we can't really make +** this structure opaque. +** +** All elements of the hash table are on a single doubly-linked list. +** Hash.first points to the head of this list. +** +** There are Hash.htsize buckets. Each bucket points to a spot in +** the global doubly-linked list. The contents of the bucket are the +** element pointed to plus the next _ht.count-1 elements in the list. +** +** Hash.htsize and Hash.ht may be zero. In that case lookup is done +** by a linear search of the global list. For small tables, the +** Hash.ht table is never allocated because if there are few elements +** in the table, it is faster to do a linear search than to manage +** the hash table. +*/ +struct Hash { + unsigned int htsize; /* Number of buckets in the hash table */ + unsigned int count; /* Number of entries in this table */ + HashElem *first; /* The first element of the array */ + struct _ht { /* the hash table */ + int count; /* Number of entries with this hash */ + HashElem *chain; /* Pointer to first entry with this hash */ + } *ht; +}; + +/* Each element in the hash table is an instance of the following +** structure. All elements are stored on a single doubly-linked list. +** +** Again, this structure is intended to be opaque, but it can't really +** be opaque because it is used by macros. +*/ +struct HashElem { + HashElem *next, *prev; /* Next and previous elements in the table */ + void *data; /* Data associated with this element */ + const char *pKey; int nKey; /* Key associated with this element */ +}; + +/* +** Access routines. To delete, insert a NULL pointer. +*/ +SQLITE_PRIVATE void sqlite3HashInit(Hash*); +SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData); +SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey); +SQLITE_PRIVATE void sqlite3HashClear(Hash*); + +/* +** Macros for looping over all elements of a hash table. The idiom is +** like this: +** +** Hash h; +** HashElem *p; +** ... +** for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){ +** SomeStructure *pData = sqliteHashData(p); +** // do something with pData +** } +*/ +#define sqliteHashFirst(H) ((H)->first) +#define sqliteHashNext(E) ((E)->next) +#define sqliteHashData(E) ((E)->data) +/* #define sqliteHashKey(E) ((E)->pKey) // NOT USED */ +/* #define sqliteHashKeysize(E) ((E)->nKey) // NOT USED */ + +/* +** Number of entries in a hash table +*/ +/* #define sqliteHashCount(H) ((H)->count) // NOT USED */ + +#endif /* _SQLITE_HASH_H_ */ + +/************** End of hash.h ************************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include parse.h in the middle of sqliteInt.h *****************/ +/************** Begin file parse.h *******************************************/ +#define TK_SEMI 1 +#define TK_EXPLAIN 2 +#define TK_QUERY 3 +#define TK_PLAN 4 +#define TK_BEGIN 5 +#define TK_TRANSACTION 6 +#define TK_DEFERRED 7 +#define TK_IMMEDIATE 8 +#define TK_EXCLUSIVE 9 +#define TK_COMMIT 10 +#define TK_END 11 +#define TK_ROLLBACK 12 +#define TK_SAVEPOINT 13 +#define TK_RELEASE 14 +#define TK_TO 15 +#define TK_TABLE 16 +#define TK_CREATE 17 +#define TK_IF 18 +#define TK_NOT 19 +#define TK_EXISTS 20 +#define TK_TEMP 21 +#define TK_LP 22 +#define TK_RP 23 +#define TK_AS 24 +#define TK_COMMA 25 +#define TK_ID 26 +#define TK_INDEXED 27 +#define TK_ABORT 28 +#define TK_ACTION 29 +#define TK_AFTER 30 +#define TK_ANALYZE 31 +#define TK_ASC 32 +#define TK_ATTACH 33 +#define TK_BEFORE 34 +#define TK_BY 35 +#define TK_CASCADE 36 +#define TK_CAST 37 +#define TK_COLUMNKW 38 +#define TK_CONFLICT 39 +#define TK_DATABASE 40 +#define TK_DESC 41 +#define TK_DETACH 42 +#define TK_EACH 43 +#define TK_FAIL 44 +#define TK_FOR 45 +#define TK_IGNORE 46 +#define TK_INITIALLY 47 +#define TK_INSTEAD 48 +#define TK_LIKE_KW 49 +#define TK_MATCH 50 +#define TK_NO 51 +#define TK_KEY 52 +#define TK_OF 53 +#define TK_OFFSET 54 +#define TK_PRAGMA 55 +#define TK_RAISE 56 +#define TK_REPLACE 57 +#define TK_RESTRICT 58 +#define TK_ROW 59 +#define TK_TRIGGER 60 +#define TK_VACUUM 61 +#define TK_VIEW 62 +#define TK_VIRTUAL 63 +#define TK_REINDEX 64 +#define TK_RENAME 65 +#define TK_CTIME_KW 66 +#define TK_ANY 67 +#define TK_OR 68 +#define TK_AND 69 +#define TK_IS 70 +#define TK_BETWEEN 71 +#define TK_IN 72 +#define TK_ISNULL 73 +#define TK_NOTNULL 74 +#define TK_NE 75 +#define TK_EQ 76 +#define TK_GT 77 +#define TK_LE 78 +#define TK_LT 79 +#define TK_GE 80 +#define TK_ESCAPE 81 +#define TK_BITAND 82 +#define TK_BITOR 83 +#define TK_LSHIFT 84 +#define TK_RSHIFT 85 +#define TK_PLUS 86 +#define TK_MINUS 87 +#define TK_STAR 88 +#define TK_SLASH 89 +#define TK_REM 90 +#define TK_CONCAT 91 +#define TK_COLLATE 92 +#define TK_BITNOT 93 +#define TK_STRING 94 +#define TK_JOIN_KW 95 +#define TK_CONSTRAINT 96 +#define TK_DEFAULT 97 +#define TK_NULL 98 +#define TK_PRIMARY 99 +#define TK_UNIQUE 100 +#define TK_CHECK 101 +#define TK_REFERENCES 102 +#define TK_AUTOINCR 103 +#define TK_ON 104 +#define TK_INSERT 105 +#define TK_DELETE 106 +#define TK_UPDATE 107 +#define TK_SET 108 +#define TK_DEFERRABLE 109 +#define TK_FOREIGN 110 +#define TK_DROP 111 +#define TK_UNION 112 +#define TK_ALL 113 +#define TK_EXCEPT 114 +#define TK_INTERSECT 115 +#define TK_SELECT 116 +#define TK_DISTINCT 117 +#define TK_DOT 118 +#define TK_FROM 119 +#define TK_JOIN 120 +#define TK_USING 121 +#define TK_ORDER 122 +#define TK_GROUP 123 +#define TK_HAVING 124 +#define TK_LIMIT 125 +#define TK_WHERE 126 +#define TK_INTO 127 +#define TK_VALUES 128 +#define TK_INTEGER 129 +#define TK_FLOAT 130 +#define TK_BLOB 131 +#define TK_REGISTER 132 +#define TK_VARIABLE 133 +#define TK_CASE 134 +#define TK_WHEN 135 +#define TK_THEN 136 +#define TK_ELSE 137 +#define TK_INDEX 138 +#define TK_ALTER 139 +#define TK_ADD 140 +#define TK_TO_TEXT 141 +#define TK_TO_BLOB 142 +#define TK_TO_NUMERIC 143 +#define TK_TO_INT 144 +#define TK_TO_REAL 145 +#define TK_ISNOT 146 +#define TK_END_OF_FILE 147 +#define TK_ILLEGAL 148 +#define TK_SPACE 149 +#define TK_UNCLOSED_STRING 150 +#define TK_FUNCTION 151 +#define TK_COLUMN 152 +#define TK_AGG_FUNCTION 153 +#define TK_AGG_COLUMN 154 +#define TK_CONST_FUNC 155 +#define TK_UMINUS 156 +#define TK_UPLUS 157 + +/************** End of parse.h ***********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +#include +#include +#include +#include +#include + +/* +** If compiling for a processor that lacks floating point support, +** substitute integer for floating-point +*/ +#ifdef SQLITE_OMIT_FLOATING_POINT +# define double sqlite_int64 +# define float sqlite_int64 +# define LONGDOUBLE_TYPE sqlite_int64 +# ifndef SQLITE_BIG_DBL +# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50) +# endif +# define SQLITE_OMIT_DATETIME_FUNCS 1 +# define SQLITE_OMIT_TRACE 1 +# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT +# undef SQLITE_HAVE_ISNAN +#endif +#ifndef SQLITE_BIG_DBL +# define SQLITE_BIG_DBL (1e99) +#endif + +/* +** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0 +** afterward. Having this macro allows us to cause the C compiler +** to omit code used by TEMP tables without messy #ifndef statements. +*/ +#ifdef SQLITE_OMIT_TEMPDB +#define OMIT_TEMPDB 1 +#else +#define OMIT_TEMPDB 0 +#endif + +/* +** The "file format" number is an integer that is incremented whenever +** the VDBE-level file format changes. The following macros define the +** the default file format for new databases and the maximum file format +** that the library can read. +*/ +#define SQLITE_MAX_FILE_FORMAT 4 +#ifndef SQLITE_DEFAULT_FILE_FORMAT +# define SQLITE_DEFAULT_FILE_FORMAT 1 +#endif + +/* +** Determine whether triggers are recursive by default. This can be +** changed at run-time using a pragma. +*/ +#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS +# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0 +#endif + +/* +** Provide a default value for SQLITE_TEMP_STORE in case it is not specified +** on the command-line +*/ +#ifndef SQLITE_TEMP_STORE +# define SQLITE_TEMP_STORE 1 +#endif + +/* +** GCC does not define the offsetof() macro so we'll have to do it +** ourselves. +*/ +#ifndef offsetof +#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) +#endif + +/* +** Check to see if this machine uses EBCDIC. (Yes, believe it or +** not, there are still machines out there that use EBCDIC.) +*/ +#if 'A' == '\301' +# define SQLITE_EBCDIC 1 +#else +# define SQLITE_ASCII 1 +#endif + +/* +** Integers of known sizes. These typedefs might change for architectures +** where the sizes very. Preprocessor macros are available so that the +** types can be conveniently redefined at compile-type. Like this: +** +** cc '-DUINTPTR_TYPE=long long int' ... +*/ +#ifndef UINT32_TYPE +# ifdef HAVE_UINT32_T +# define UINT32_TYPE uint32_t +# else +# define UINT32_TYPE unsigned int +# endif +#endif +#ifndef UINT16_TYPE +# ifdef HAVE_UINT16_T +# define UINT16_TYPE uint16_t +# else +# define UINT16_TYPE unsigned short int +# endif +#endif +#ifndef INT16_TYPE +# ifdef HAVE_INT16_T +# define INT16_TYPE int16_t +# else +# define INT16_TYPE short int +# endif +#endif +#ifndef UINT8_TYPE +# ifdef HAVE_UINT8_T +# define UINT8_TYPE uint8_t +# else +# define UINT8_TYPE unsigned char +# endif +#endif +#ifndef INT8_TYPE +# ifdef HAVE_INT8_T +# define INT8_TYPE int8_t +# else +# define INT8_TYPE signed char +# endif +#endif +#ifndef LONGDOUBLE_TYPE +# define LONGDOUBLE_TYPE long double +#endif +typedef sqlite_int64 i64; /* 8-byte signed integer */ +typedef sqlite_uint64 u64; /* 8-byte unsigned integer */ +typedef UINT32_TYPE u32; /* 4-byte unsigned integer */ +typedef UINT16_TYPE u16; /* 2-byte unsigned integer */ +typedef INT16_TYPE i16; /* 2-byte signed integer */ +typedef UINT8_TYPE u8; /* 1-byte unsigned integer */ +typedef INT8_TYPE i8; /* 1-byte signed integer */ + +/* +** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value +** that can be stored in a u32 without loss of data. The value +** is 0x00000000ffffffff. But because of quirks of some compilers, we +** have to specify the value in the less intuitive manner shown: +*/ +#define SQLITE_MAX_U32 ((((u64)1)<<32)-1) + +/* +** Macros to determine whether the machine is big or little endian, +** evaluated at runtime. +*/ +#ifdef SQLITE_AMALGAMATION +SQLITE_PRIVATE const int sqlite3one = 1; +#else +SQLITE_PRIVATE const int sqlite3one; +#endif +#if defined(i386) || defined(__i386__) || defined(_M_IX86)\ + || defined(__x86_64) || defined(__x86_64__) +# define SQLITE_BIGENDIAN 0 +# define SQLITE_LITTLEENDIAN 1 +# define SQLITE_UTF16NATIVE SQLITE_UTF16LE +#else +# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0) +# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1) +# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE) +#endif + +/* +** Constants for the largest and smallest possible 64-bit signed integers. +** These macros are designed to work correctly on both 32-bit and 64-bit +** compilers. +*/ +#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) +#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64) + +/* +** Round up a number to the next larger multiple of 8. This is used +** to force 8-byte alignment on 64-bit architectures. +*/ +#define ROUND8(x) (((x)+7)&~7) + +/* +** Round down to the nearest multiple of 8 +*/ +#define ROUNDDOWN8(x) ((x)&~7) + +/* +** Assert that the pointer X is aligned to an 8-byte boundary. This +** macro is used only within assert() to verify that the code gets +** all alignment restrictions correct. +** +** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the +** underlying malloc() implemention might return us 4-byte aligned +** pointers. In that case, only verify 4-byte alignment. +*/ +#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC +# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0) +#else +# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0) +#endif + + +/* +** An instance of the following structure is used to store the busy-handler +** callback for a given sqlite handle. +** +** The sqlite.busyHandler member of the sqlite struct contains the busy +** callback for the database handle. Each pager opened via the sqlite +** handle is passed a pointer to sqlite.busyHandler. The busy-handler +** callback is currently invoked only from within pager.c. +*/ +typedef struct BusyHandler BusyHandler; +struct BusyHandler { + int (*xFunc)(void *,int); /* The busy callback */ + void *pArg; /* First arg to busy callback */ + int nBusy; /* Incremented with each busy call */ +}; + +/* +** Name of the master database table. The master database table +** is a special table that holds the names and attributes of all +** user tables and indices. +*/ +#define MASTER_NAME "sqlite_master" +#define TEMP_MASTER_NAME "sqlite_temp_master" + +/* +** The root-page of the master database table. +*/ +#define MASTER_ROOT 1 + +/* +** The name of the schema table. +*/ +#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME) + +/* +** A convenience macro that returns the number of elements in +** an array. +*/ +#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0]))) + +/* +** The following value as a destructor means to use sqlite3DbFree(). +** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT. +*/ +#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3DbFree) + +/* +** When SQLITE_OMIT_WSD is defined, it means that the target platform does +** not support Writable Static Data (WSD) such as global and static variables. +** All variables must either be on the stack or dynamically allocated from +** the heap. When WSD is unsupported, the variable declarations scattered +** throughout the SQLite code must become constants instead. The SQLITE_WSD +** macro is used for this purpose. And instead of referencing the variable +** directly, we use its constant as a key to lookup the run-time allocated +** buffer that holds real variable. The constant is also the initializer +** for the run-time allocated buffer. +** +** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL +** macros become no-ops and have zero performance impact. +*/ +#ifdef SQLITE_OMIT_WSD + #define SQLITE_WSD const + #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v))) + #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config) +SQLITE_API int sqlite3_wsd_init(int N, int J); +SQLITE_API void *sqlite3_wsd_find(void *K, int L); +#else + #define SQLITE_WSD + #define GLOBAL(t,v) v + #define sqlite3GlobalConfig sqlite3Config +#endif + +/* +** The following macros are used to suppress compiler warnings and to +** make it clear to human readers when a function parameter is deliberately +** left unused within the body of a function. This usually happens when +** a function is called via a function pointer. For example the +** implementation of an SQL aggregate step callback may not use the +** parameter indicating the number of arguments passed to the aggregate, +** if it knows that this is enforced elsewhere. +** +** When a function parameter is not used at all within the body of a function, +** it is generally named "NotUsed" or "NotUsed2" to make things even clearer. +** However, these macros may also be used to suppress warnings related to +** parameters that may or may not be used depending on compilation options. +** For example those parameters only used in assert() statements. In these +** cases the parameters are named as per the usual conventions. +*/ +#define UNUSED_PARAMETER(x) (void)(x) +#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y) + +/* +** Forward references to structures +*/ +typedef struct AggInfo AggInfo; +typedef struct AuthContext AuthContext; +typedef struct AutoincInfo AutoincInfo; +typedef struct Bitvec Bitvec; +typedef struct CollSeq CollSeq; +typedef struct Column Column; +typedef struct Db Db; +typedef struct Schema Schema; +typedef struct Expr Expr; +typedef struct ExprList ExprList; +typedef struct ExprSpan ExprSpan; +typedef struct FKey FKey; +typedef struct FuncDestructor FuncDestructor; +typedef struct FuncDef FuncDef; +typedef struct FuncDefHash FuncDefHash; +typedef struct IdList IdList; +typedef struct Index Index; +typedef struct IndexSample IndexSample; +typedef struct KeyClass KeyClass; +typedef struct KeyInfo KeyInfo; +typedef struct Lookaside Lookaside; +typedef struct LookasideSlot LookasideSlot; +typedef struct Module Module; +typedef struct NameContext NameContext; +typedef struct Parse Parse; +typedef struct RowSet RowSet; +typedef struct Savepoint Savepoint; +typedef struct Select Select; +typedef struct SrcList SrcList; +typedef struct StrAccum StrAccum; +typedef struct Table Table; +typedef struct TableLock TableLock; +typedef struct Token Token; +typedef struct Trigger Trigger; +typedef struct TriggerPrg TriggerPrg; +typedef struct TriggerStep TriggerStep; +typedef struct UnpackedRecord UnpackedRecord; +typedef struct VTable VTable; +typedef struct Walker Walker; +typedef struct WherePlan WherePlan; +typedef struct WhereInfo WhereInfo; +typedef struct WhereLevel WhereLevel; + +/* +** Defer sourcing vdbe.h and btree.h until after the "u8" and +** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque +** pointer types (i.e. FuncDef) defined above. +*/ +/************** Include btree.h in the middle of sqliteInt.h *****************/ +/************** Begin file btree.h *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface that the sqlite B-Tree file +** subsystem. See comments in the source code for a detailed description +** of what each interface routine does. +*/ +#ifndef _BTREE_H_ +#define _BTREE_H_ + +/* TODO: This definition is just included so other modules compile. It +** needs to be revisited. +*/ +#define SQLITE_N_BTREE_META 10 + +/* +** If defined as non-zero, auto-vacuum is enabled by default. Otherwise +** it must be turned on for each database using "PRAGMA auto_vacuum = 1". +*/ +#ifndef SQLITE_DEFAULT_AUTOVACUUM + #define SQLITE_DEFAULT_AUTOVACUUM 0 +#endif + +#define BTREE_AUTOVACUUM_NONE 0 /* Do not do auto-vacuum */ +#define BTREE_AUTOVACUUM_FULL 1 /* Do full auto-vacuum */ +#define BTREE_AUTOVACUUM_INCR 2 /* Incremental vacuum */ + +/* +** Forward declarations of structure +*/ +typedef struct BtCursor BtCursor; +typedef struct BtShared BtShared; +typedef struct BtreeMutexArray BtreeMutexArray; + +/* +** This structure records all of the Btrees that need to hold +** a mutex before we enter sqlite3VdbeExec(). The Btrees are +** are placed in aBtree[] in order of aBtree[]->pBt. That way, +** we can always lock and unlock them all quickly. +*/ +struct BtreeMutexArray { + int nMutex; + Btree *aBtree[SQLITE_MAX_ATTACHED+1]; +}; + + +SQLITE_PRIVATE int sqlite3BtreeOpen( + const char *zFilename, /* Name of database file to open */ + sqlite3 *db, /* Associated database connection */ + Btree **ppBtree, /* Return open Btree* here */ + int flags, /* Flags */ + int vfsFlags /* Flags passed through to VFS open */ +); + +/* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the +** following values. +** +** NOTE: These values must match the corresponding PAGER_ values in +** pager.h. +*/ +#define BTREE_OMIT_JOURNAL 1 /* Do not create or use a rollback journal */ +#define BTREE_NO_READLOCK 2 /* Omit readlocks on readonly files */ +#define BTREE_MEMORY 4 /* This is an in-memory DB */ +#define BTREE_SINGLE 8 /* The file contains at most 1 b-tree */ +#define BTREE_UNORDERED 16 /* Use of a hash implementation is OK */ + +SQLITE_PRIVATE int sqlite3BtreeClose(Btree*); +SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int); +SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int); +SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*); +SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix); +SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*); +SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int); +SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*); +SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int); +SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*); +SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int); +SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *); +SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int); +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster); +SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*); +SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*); +SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*); +SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int); +SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags); +SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*); +SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*); +SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*); +SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *)); +SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree); +SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock); +SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int); + +SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *); +SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *); +SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *); + +SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *); + +/* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR +** of the flags shown below. +** +** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set. +** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data +** is stored in the leaves. (BTREE_INTKEY is used for SQL tables.) With +** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored +** anywhere - the key is the content. (BTREE_BLOBKEY is used for SQL +** indices.) +*/ +#define BTREE_INTKEY 1 /* Table has only 64-bit signed integer keys */ +#define BTREE_BLOBKEY 2 /* Table has keys only - no data */ + +SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*); +SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*); +SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int); + +SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue); +SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value); + +/* +** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta +** should be one of the following values. The integer values are assigned +** to constants so that the offset of the corresponding field in an +** SQLite database header may be found using the following formula: +** +** offset = 36 + (idx * 4) +** +** For example, the free-page-count field is located at byte offset 36 of +** the database file header. The incr-vacuum-flag field is located at +** byte offset 64 (== 36+4*7). +*/ +#define BTREE_FREE_PAGE_COUNT 0 +#define BTREE_SCHEMA_VERSION 1 +#define BTREE_FILE_FORMAT 2 +#define BTREE_DEFAULT_CACHE_SIZE 3 +#define BTREE_LARGEST_ROOT_PAGE 4 +#define BTREE_TEXT_ENCODING 5 +#define BTREE_USER_VERSION 6 +#define BTREE_INCR_VACUUM 7 + +SQLITE_PRIVATE int sqlite3BtreeCursor( + Btree*, /* BTree containing table to open */ + int iTable, /* Index of root page */ + int wrFlag, /* 1 for writing. 0 for read-only */ + struct KeyInfo*, /* First argument to compare function */ + BtCursor *pCursor /* Space to write cursor structure */ +); +SQLITE_PRIVATE int sqlite3BtreeCursorSize(void); +SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*); + +SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*); +SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked( + BtCursor*, + UnpackedRecord *pUnKey, + i64 intKey, + int bias, + int *pRes +); +SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*); +SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*); +SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey, + const void *pData, int nData, + int nZero, int bias, int seekResult); +SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes); +SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes); +SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes); +SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*); +SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes); +SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize); +SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*); +SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, int *pAmt); +SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, int *pAmt); +SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize); +SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*); +SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64); +SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*); + +SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*, int); +SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*); + +SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*); +SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *); +SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *); + +SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion); + +#ifndef NDEBUG +SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*); +#endif + +#ifndef SQLITE_OMIT_BTREECOUNT +SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *); +#endif + +#ifdef SQLITE_TEST +SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int); +SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*); +#endif + +#ifndef SQLITE_OMIT_WAL +SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree*, int, int *, int *); +#endif + +/* +** If we are not using shared cache, then there is no need to +** use mutexes to access the BtShared structures. So make the +** Enter and Leave procedures no-ops. +*/ +#ifndef SQLITE_OMIT_SHARED_CACHE +SQLITE_PRIVATE void sqlite3BtreeEnter(Btree*); +SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3*); +#else +# define sqlite3BtreeEnter(X) +# define sqlite3BtreeEnterAll(X) +#endif + +#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE +SQLITE_PRIVATE void sqlite3BtreeLeave(Btree*); +SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor*); +SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor*); +SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3*); +SQLITE_PRIVATE void sqlite3BtreeMutexArrayEnter(BtreeMutexArray*); +SQLITE_PRIVATE void sqlite3BtreeMutexArrayLeave(BtreeMutexArray*); +SQLITE_PRIVATE void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*); +#ifndef NDEBUG + /* These routines are used inside assert() statements only. */ +SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree*); +SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3*); +#endif +#else + +# define sqlite3BtreeLeave(X) +# define sqlite3BtreeEnterCursor(X) +# define sqlite3BtreeLeaveCursor(X) +# define sqlite3BtreeLeaveAll(X) +# define sqlite3BtreeMutexArrayEnter(X) +# define sqlite3BtreeMutexArrayLeave(X) +# define sqlite3BtreeMutexArrayInsert(X,Y) + +# define sqlite3BtreeHoldsMutex(X) 1 +# define sqlite3BtreeHoldsAllMutexes(X) 1 +#endif + + +#endif /* _BTREE_H_ */ + +/************** End of btree.h ***********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include vdbe.h in the middle of sqliteInt.h ******************/ +/************** Begin file vdbe.h ********************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Header file for the Virtual DataBase Engine (VDBE) +** +** This header defines the interface to the virtual database engine +** or VDBE. The VDBE implements an abstract machine that runs a +** simple program to access and modify the underlying database. +*/ +#ifndef _SQLITE_VDBE_H_ +#define _SQLITE_VDBE_H_ + +/* +** A single VDBE is an opaque structure named "Vdbe". Only routines +** in the source file sqliteVdbe.c are allowed to see the insides +** of this structure. +*/ +typedef struct Vdbe Vdbe; + +/* +** The names of the following types declared in vdbeInt.h are required +** for the VdbeOp definition. +*/ +typedef struct VdbeFunc VdbeFunc; +typedef struct Mem Mem; +typedef struct SubProgram SubProgram; + +/* +** A single instruction of the virtual machine has an opcode +** and as many as three operands. The instruction is recorded +** as an instance of the following structure: +*/ +struct VdbeOp { + u8 opcode; /* What operation to perform */ + signed char p4type; /* One of the P4_xxx constants for p4 */ + u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */ + u8 p5; /* Fifth parameter is an unsigned character */ + int p1; /* First operand */ + int p2; /* Second parameter (often the jump destination) */ + int p3; /* The third parameter */ + union { /* fourth parameter */ + int i; /* Integer value if p4type==P4_INT32 */ + void *p; /* Generic pointer */ + char *z; /* Pointer to data for string (char array) types */ + i64 *pI64; /* Used when p4type is P4_INT64 */ + double *pReal; /* Used when p4type is P4_REAL */ + FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */ + VdbeFunc *pVdbeFunc; /* Used when p4type is P4_VDBEFUNC */ + CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */ + Mem *pMem; /* Used when p4type is P4_MEM */ + VTable *pVtab; /* Used when p4type is P4_VTAB */ + KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */ + int *ai; /* Used when p4type is P4_INTARRAY */ + SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */ + } p4; +#ifdef SQLITE_DEBUG + char *zComment; /* Comment to improve readability */ +#endif +#ifdef VDBE_PROFILE + int cnt; /* Number of times this instruction was executed */ + u64 cycles; /* Total time spent executing this instruction */ +#endif +}; +typedef struct VdbeOp VdbeOp; + + +/* +** A sub-routine used to implement a trigger program. +*/ +struct SubProgram { + VdbeOp *aOp; /* Array of opcodes for sub-program */ + int nOp; /* Elements in aOp[] */ + int nMem; /* Number of memory cells required */ + int nCsr; /* Number of cursors required */ + void *token; /* id that may be used to recursive triggers */ + SubProgram *pNext; /* Next sub-program already visited */ +}; + +/* +** A smaller version of VdbeOp used for the VdbeAddOpList() function because +** it takes up less space. +*/ +struct VdbeOpList { + u8 opcode; /* What operation to perform */ + signed char p1; /* First operand */ + signed char p2; /* Second parameter (often the jump destination) */ + signed char p3; /* Third parameter */ +}; +typedef struct VdbeOpList VdbeOpList; + +/* +** Allowed values of VdbeOp.p4type +*/ +#define P4_NOTUSED 0 /* The P4 parameter is not used */ +#define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */ +#define P4_STATIC (-2) /* Pointer to a static string */ +#define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */ +#define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */ +#define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */ +#define P4_VDBEFUNC (-7) /* P4 is a pointer to a VdbeFunc structure */ +#define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */ +#define P4_TRANSIENT (-9) /* P4 is a pointer to a transient string */ +#define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */ +#define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */ +#define P4_REAL (-12) /* P4 is a 64-bit floating point value */ +#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */ +#define P4_INT32 (-14) /* P4 is a 32-bit signed integer */ +#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */ +#define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */ + +/* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure +** is made. That copy is freed when the Vdbe is finalized. But if the +** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still +** gets freed when the Vdbe is finalized so it still should be obtained +** from a single sqliteMalloc(). But no copy is made and the calling +** function should *not* try to free the KeyInfo. +*/ +#define P4_KEYINFO_HANDOFF (-16) +#define P4_KEYINFO_STATIC (-17) + +/* +** The Vdbe.aColName array contains 5n Mem structures, where n is the +** number of columns of data returned by the statement. +*/ +#define COLNAME_NAME 0 +#define COLNAME_DECLTYPE 1 +#define COLNAME_DATABASE 2 +#define COLNAME_TABLE 3 +#define COLNAME_COLUMN 4 +#ifdef SQLITE_ENABLE_COLUMN_METADATA +# define COLNAME_N 5 /* Number of COLNAME_xxx symbols */ +#else +# ifdef SQLITE_OMIT_DECLTYPE +# define COLNAME_N 1 /* Store only the name */ +# else +# define COLNAME_N 2 /* Store the name and decltype */ +# endif +#endif + +/* +** The following macro converts a relative address in the p2 field +** of a VdbeOp structure into a negative number so that +** sqlite3VdbeAddOpList() knows that the address is relative. Calling +** the macro again restores the address. +*/ +#define ADDR(X) (-1-(X)) + +/* +** The makefile scans the vdbe.c source file and creates the "opcodes.h" +** header file that defines a number for each opcode used by the VDBE. +*/ +/************** Include opcodes.h in the middle of vdbe.h ********************/ +/************** Begin file opcodes.h *****************************************/ +/* Automatically generated. Do not edit */ +/* See the mkopcodeh.awk script for details */ +#define OP_Goto 1 +#define OP_Gosub 2 +#define OP_Return 3 +#define OP_Yield 4 +#define OP_HaltIfNull 5 +#define OP_Halt 6 +#define OP_Integer 7 +#define OP_Int64 8 +#define OP_Real 130 /* same as TK_FLOAT */ +#define OP_String8 94 /* same as TK_STRING */ +#define OP_String 9 +#define OP_Null 10 +#define OP_Blob 11 +#define OP_Variable 12 +#define OP_Move 13 +#define OP_Copy 14 +#define OP_SCopy 15 +#define OP_ResultRow 16 +#define OP_Concat 91 /* same as TK_CONCAT */ +#define OP_Add 86 /* same as TK_PLUS */ +#define OP_Subtract 87 /* same as TK_MINUS */ +#define OP_Multiply 88 /* same as TK_STAR */ +#define OP_Divide 89 /* same as TK_SLASH */ +#define OP_Remainder 90 /* same as TK_REM */ +#define OP_CollSeq 17 +#define OP_Function 18 +#define OP_BitAnd 82 /* same as TK_BITAND */ +#define OP_BitOr 83 /* same as TK_BITOR */ +#define OP_ShiftLeft 84 /* same as TK_LSHIFT */ +#define OP_ShiftRight 85 /* same as TK_RSHIFT */ +#define OP_AddImm 20 +#define OP_MustBeInt 21 +#define OP_RealAffinity 22 +#define OP_ToText 141 /* same as TK_TO_TEXT */ +#define OP_ToBlob 142 /* same as TK_TO_BLOB */ +#define OP_ToNumeric 143 /* same as TK_TO_NUMERIC*/ +#define OP_ToInt 144 /* same as TK_TO_INT */ +#define OP_ToReal 145 /* same as TK_TO_REAL */ +#define OP_Eq 76 /* same as TK_EQ */ +#define OP_Ne 75 /* same as TK_NE */ +#define OP_Lt 79 /* same as TK_LT */ +#define OP_Le 78 /* same as TK_LE */ +#define OP_Gt 77 /* same as TK_GT */ +#define OP_Ge 80 /* same as TK_GE */ +#define OP_Permutation 23 +#define OP_Compare 24 +#define OP_Jump 25 +#define OP_And 69 /* same as TK_AND */ +#define OP_Or 68 /* same as TK_OR */ +#define OP_Not 19 /* same as TK_NOT */ +#define OP_BitNot 93 /* same as TK_BITNOT */ +#define OP_If 26 +#define OP_IfNot 27 +#define OP_IsNull 73 /* same as TK_ISNULL */ +#define OP_NotNull 74 /* same as TK_NOTNULL */ +#define OP_Column 28 +#define OP_Affinity 29 +#define OP_MakeRecord 30 +#define OP_Count 31 +#define OP_Savepoint 32 +#define OP_AutoCommit 33 +#define OP_Transaction 34 +#define OP_ReadCookie 35 +#define OP_SetCookie 36 +#define OP_VerifyCookie 37 +#define OP_OpenRead 38 +#define OP_OpenWrite 39 +#define OP_OpenAutoindex 40 +#define OP_OpenEphemeral 41 +#define OP_OpenPseudo 42 +#define OP_Close 43 +#define OP_SeekLt 44 +#define OP_SeekLe 45 +#define OP_SeekGe 46 +#define OP_SeekGt 47 +#define OP_Seek 48 +#define OP_NotFound 49 +#define OP_Found 50 +#define OP_IsUnique 51 +#define OP_NotExists 52 +#define OP_Sequence 53 +#define OP_NewRowid 54 +#define OP_Insert 55 +#define OP_InsertInt 56 +#define OP_Delete 57 +#define OP_ResetCount 58 +#define OP_RowKey 59 +#define OP_RowData 60 +#define OP_Rowid 61 +#define OP_NullRow 62 +#define OP_Last 63 +#define OP_Sort 64 +#define OP_Rewind 65 +#define OP_Prev 66 +#define OP_Next 67 +#define OP_IdxInsert 70 +#define OP_IdxDelete 71 +#define OP_IdxRowid 72 +#define OP_IdxLT 81 +#define OP_IdxGE 92 +#define OP_Destroy 95 +#define OP_Clear 96 +#define OP_CreateIndex 97 +#define OP_CreateTable 98 +#define OP_ParseSchema 99 +#define OP_LoadAnalysis 100 +#define OP_DropTable 101 +#define OP_DropIndex 102 +#define OP_DropTrigger 103 +#define OP_IntegrityCk 104 +#define OP_RowSetAdd 105 +#define OP_RowSetRead 106 +#define OP_RowSetTest 107 +#define OP_Program 108 +#define OP_Param 109 +#define OP_FkCounter 110 +#define OP_FkIfZero 111 +#define OP_MemMax 112 +#define OP_IfPos 113 +#define OP_IfNeg 114 +#define OP_IfZero 115 +#define OP_AggStep 116 +#define OP_AggFinal 117 +#define OP_Checkpoint 118 +#define OP_JournalMode 119 +#define OP_Vacuum 120 +#define OP_IncrVacuum 121 +#define OP_Expire 122 +#define OP_TableLock 123 +#define OP_VBegin 124 +#define OP_VCreate 125 +#define OP_VDestroy 126 +#define OP_VOpen 127 +#define OP_VFilter 128 +#define OP_VColumn 129 +#define OP_VNext 131 +#define OP_VRename 132 +#define OP_VUpdate 133 +#define OP_Pagecount 134 +#define OP_MaxPgcnt 135 +#define OP_Trace 136 +#define OP_Noop 137 +#define OP_Explain 138 + +/* The following opcode values are never used */ +#define OP_NotUsed_139 139 +#define OP_NotUsed_140 140 + + +/* Properties such as "out2" or "jump" that are specified in +** comments following the "case" for each opcode in the vdbe.c +** are encoded into bitvectors as follows: +*/ +#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */ +#define OPFLG_OUT2_PRERELEASE 0x0002 /* out2-prerelease: */ +#define OPFLG_IN1 0x0004 /* in1: P1 is an input */ +#define OPFLG_IN2 0x0008 /* in2: P2 is an input */ +#define OPFLG_IN3 0x0010 /* in3: P3 is an input */ +#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */ +#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */ +#define OPFLG_INITIALIZER {\ +/* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\ +/* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\ +/* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\ +/* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\ +/* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\ +/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\ +/* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\ +/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\ +/* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\ +/* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\ +/* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\ +/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\ +/* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\ +/* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\ +/* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\ +/* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\ +/* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x02,\ +/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\ +/* 144 */ 0x04, 0x04,} + +/************** End of opcodes.h *********************************************/ +/************** Continuing where we left off in vdbe.h ***********************/ + +/* +** Prototypes for the VDBE interface. See comments on the implementation +** for a description of what each of these routines does. +*/ +SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(sqlite3*); +SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int); +SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int); +SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp); +SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1); +SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2); +SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3); +SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5); +SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr); +SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N); +SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N); +SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int); +SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int); +SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeDeleteObject(sqlite3*,Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int,int,int); +SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int); +SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*); +#ifdef SQLITE_DEBUG +SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *, int); +SQLITE_PRIVATE void sqlite3VdbeTrace(Vdbe*,FILE*); +#endif +SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*); +SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int); +SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*)); +SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*); +SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*); +SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int); +SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*); +SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*); +SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetValue(Vdbe*, int, u8); +SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int); +#ifndef SQLITE_OMIT_TRACE +SQLITE_PRIVATE char *sqlite3VdbeExpandSql(Vdbe*, const char*); +#endif + +SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,char*,int); +SQLITE_PRIVATE void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*); +SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*,int,int*); + +#ifndef SQLITE_OMIT_TRIGGER +SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *); +#endif + + +#ifndef NDEBUG +SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe*, const char*, ...); +# define VdbeComment(X) sqlite3VdbeComment X +SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...); +# define VdbeNoopComment(X) sqlite3VdbeNoopComment X +#else +# define VdbeComment(X) +# define VdbeNoopComment(X) +#endif + +#endif + +/************** End of vdbe.h ************************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include pager.h in the middle of sqliteInt.h *****************/ +/************** Begin file pager.h *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface that the sqlite page cache +** subsystem. The page cache subsystem reads and writes a file a page +** at a time and provides a journal for rollback. +*/ + +#ifndef _PAGER_H_ +#define _PAGER_H_ + +/* +** Default maximum size for persistent journal files. A negative +** value means no limit. This value may be overridden using the +** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit". +*/ +#ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT + #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1 +#endif + +/* +** Each open file is managed by a separate instance of the "Pager" structure. +*/ +typedef struct Pager Pager; + +/* +** Handle type for pages. +*/ +typedef struct PgHdr DbPage; + +/* +** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is +** reserved for working around a windows/posix incompatibility). It is +** used in the journal to signify that the remainder of the journal file +** is devoted to storing a master journal name - there are no more pages to +** roll back. See comments for function writeMasterJournal() in pager.c +** for details. +*/ +#define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1)) + +/* +** Allowed values for the flags parameter to sqlite3PagerOpen(). +** +** NOTE: These values must match the corresponding BTREE_ values in btree.h. +*/ +#define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */ +#define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */ +#define PAGER_MEMORY 0x0004 /* In-memory database */ + +/* +** Valid values for the second argument to sqlite3PagerLockingMode(). +*/ +#define PAGER_LOCKINGMODE_QUERY -1 +#define PAGER_LOCKINGMODE_NORMAL 0 +#define PAGER_LOCKINGMODE_EXCLUSIVE 1 + +/* +** Numeric constants that encode the journalmode. +*/ +#define PAGER_JOURNALMODE_QUERY (-1) /* Query the value of journalmode */ +#define PAGER_JOURNALMODE_DELETE 0 /* Commit by deleting journal file */ +#define PAGER_JOURNALMODE_PERSIST 1 /* Commit by zeroing journal header */ +#define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */ +#define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */ +#define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */ +#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */ + +/* +** The remainder of this file contains the declarations of the functions +** that make up the Pager sub-system API. See source code comments for +** a detailed description of each routine. +*/ + +/* Open and close a Pager connection. */ +SQLITE_PRIVATE int sqlite3PagerOpen( + sqlite3_vfs*, + Pager **ppPager, + const char*, + int, + int, + int, + void(*)(DbPage*) +); +SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager); +SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*); + +/* Functions used to configure a Pager object. */ +SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *); +SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int); +SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int); +SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int); +SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int); +SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int); +SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int); +SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*); +SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*); +SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64); +SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*); + +/* Functions used to obtain and release page references. */ +SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag); +#define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0) +SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno); +SQLITE_PRIVATE void sqlite3PagerRef(DbPage*); +SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*); + +/* Operations on page references. */ +SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*); +SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*); +SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int); +SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*); +SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *); +SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *); + +/* Functions used to manage pager transactions and savepoints. */ +SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*); +SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int); +SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int); +SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*); +SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager); +SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*); +SQLITE_PRIVATE int sqlite3PagerRollback(Pager*); +SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n); +SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint); +SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager); + +SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*); +SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager); +SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager); +SQLITE_PRIVATE int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); +SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager); + +/* Functions used to query pager state and configuration. */ +SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*); +SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*); +SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*); +SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*); +SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*); +SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*); +SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*); +SQLITE_PRIVATE int sqlite3PagerNosync(Pager*); +SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*); +SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*); + +/* Functions used to truncate the database file. */ +SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno); + +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) +SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *); +#endif + +/* Functions to support testing and debugging. */ +#if !defined(NDEBUG) || defined(SQLITE_TEST) +SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage*); +SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage*); +#endif +#ifdef SQLITE_TEST +SQLITE_PRIVATE int *sqlite3PagerStats(Pager*); +SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*); + void disable_simulated_io_errors(void); + void enable_simulated_io_errors(void); +#else +# define disable_simulated_io_errors() +# define enable_simulated_io_errors() +#endif + +#endif /* _PAGER_H_ */ + +/************** End of pager.h ***********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include pcache.h in the middle of sqliteInt.h ****************/ +/************** Begin file pcache.h ******************************************/ +/* +** 2008 August 05 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface that the sqlite page cache +** subsystem. +*/ + +#ifndef _PCACHE_H_ + +typedef struct PgHdr PgHdr; +typedef struct PCache PCache; + +/* +** Every page in the cache is controlled by an instance of the following +** structure. +*/ +struct PgHdr { + void *pData; /* Content of this page */ + void *pExtra; /* Extra content */ + PgHdr *pDirty; /* Transient list of dirty pages */ + Pgno pgno; /* Page number for this page */ + Pager *pPager; /* The pager this page is part of */ +#ifdef SQLITE_CHECK_PAGES + u32 pageHash; /* Hash of page content */ +#endif + u16 flags; /* PGHDR flags defined below */ + + /********************************************************************** + ** Elements above are public. All that follows is private to pcache.c + ** and should not be accessed by other modules. + */ + i16 nRef; /* Number of users of this page */ + PCache *pCache; /* Cache that owns this page */ + + PgHdr *pDirtyNext; /* Next element in list of dirty pages */ + PgHdr *pDirtyPrev; /* Previous element in list of dirty pages */ +}; + +/* Bit values for PgHdr.flags */ +#define PGHDR_DIRTY 0x002 /* Page has changed */ +#define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before + ** writing this page to the database */ +#define PGHDR_NEED_READ 0x008 /* Content is unread */ +#define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */ +#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */ + +#define PGHDR_ZERO_COPY 0x040 /* Content was read via xReadZeroCopy and must be released with xReleaseZeroCopy */ + +/* Initialize and shutdown the page cache subsystem */ +SQLITE_PRIVATE int sqlite3PcacheInitialize(void); +SQLITE_PRIVATE void sqlite3PcacheShutdown(void); + +/* Page cache buffer management: +** These routines implement SQLITE_CONFIG_PAGECACHE. +*/ +SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n); + +/* Create a new pager cache. +** Under memory stress, invoke xStress to try to make pages clean. +** Only clean and unpinned pages can be reclaimed. +*/ +SQLITE_PRIVATE void sqlite3PcacheOpen( + int szPage, /* Size of every page */ + int szExtra, /* Extra space associated with each page */ + int bPurgeable, /* True if pages are on backing store */ + int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */ + void *pStress, /* Argument to xStress */ + PCache *pToInit /* Preallocated space for the PCache */ +); + +/* Modify the page-size after the cache has been created. */ +SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int); + +/* Return the size in bytes of a PCache object. Used to preallocate +** storage space. +*/ +SQLITE_PRIVATE int sqlite3PcacheSize(void); + +/* One release per successful fetch. Page is pinned until released. +** Reference counted. +*/ +SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**); +SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*); + +SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*); /* Remove page from cache */ +SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*); /* Make sure page is marked dirty */ +SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*); /* Mark a single page as clean */ +SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*); /* Mark all dirty list pages as clean */ + +/* Change a page number. Used by incr-vacuum. */ +SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno); + +/* Remove all pages with pgno>x. Reset the cache if x==0 */ +SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x); + +/* Get a list of all dirty pages in the cache, sorted by page number */ +SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*); + +/* Reset and close the cache object */ +SQLITE_PRIVATE void sqlite3PcacheClose(PCache*); + +/* Clear flags from pages of the page cache */ +SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *); + +/* Discard the contents of the cache */ +SQLITE_PRIVATE void sqlite3PcacheClear(PCache*); + +/* Return the total number of outstanding page references */ +SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*); + +/* Increment the reference count of an existing page */ +SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*); + +SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*); + +/* Return the total number of pages stored in the cache */ +SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*); + +#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG) +/* Iterate through all dirty pages currently stored in the cache. This +** interface is only available if SQLITE_CHECK_PAGES is defined when the +** library is built. +*/ +SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)); +#endif + +/* Set and get the suggested cache-size for the specified pager-cache. +** +** If no global maximum is configured, then the system attempts to limit +** the total number of pages cached by purgeable pager-caches to the sum +** of the suggested cache-sizes. +*/ +SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int); +#ifdef SQLITE_TEST +SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *); +#endif + +#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT +/* Try to return memory used by the pcache module to the main memory heap */ +SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int); +#endif + +#ifdef SQLITE_TEST +SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*); +#endif + +SQLITE_PRIVATE void sqlite3PCacheSetDefault(void); + +#endif /* _PCACHE_H_ */ + +/************** End of pcache.h **********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ + +/************** Include os.h in the middle of sqliteInt.h ********************/ +/************** Begin file os.h **********************************************/ +/* +** 2001 September 16 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This header file (together with is companion C source-code file +** "os.c") attempt to abstract the underlying operating system so that +** the SQLite library will work on both POSIX and windows systems. +** +** This header file is #include-ed by sqliteInt.h and thus ends up +** being included by every source file. +*/ +#ifndef _SQLITE_OS_H_ +#define _SQLITE_OS_H_ + +/* +** Figure out if we are dealing with Unix, Windows, or some other +** operating system. After the following block of preprocess macros, +** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, SQLITE_OS_OS2, and SQLITE_OS_OTHER +** will defined to either 1 or 0. One of the four will be 1. The other +** three will be 0. +*/ +#if defined(SQLITE_OS_OTHER) +# if SQLITE_OS_OTHER==1 +# undef SQLITE_OS_UNIX +# define SQLITE_OS_UNIX 0 +# undef SQLITE_OS_WIN +# define SQLITE_OS_WIN 0 +# undef SQLITE_OS_OS2 +# define SQLITE_OS_OS2 0 +# else +# undef SQLITE_OS_OTHER +# endif +#endif +#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER) +# define SQLITE_OS_OTHER 0 +# ifndef SQLITE_OS_WIN +# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) +# define SQLITE_OS_WIN 1 +# define SQLITE_OS_UNIX 0 +# define SQLITE_OS_OS2 0 +# elif defined(__EMX__) || defined(_OS2) || defined(OS2) || defined(_OS2_) || defined(__OS2__) +# define SQLITE_OS_WIN 0 +# define SQLITE_OS_UNIX 0 +# define SQLITE_OS_OS2 1 +# else +# define SQLITE_OS_WIN 0 +# define SQLITE_OS_UNIX 1 +# define SQLITE_OS_OS2 0 +# endif +# else +# define SQLITE_OS_UNIX 0 +# define SQLITE_OS_OS2 0 +# endif +#else +# ifndef SQLITE_OS_WIN +# define SQLITE_OS_WIN 0 +# endif +#endif + +/* +** Determine if we are dealing with WindowsCE - which has a much +** reduced API. +*/ +#if defined(_WIN32_WCE) +# define SQLITE_OS_WINCE 1 +#else +# define SQLITE_OS_WINCE 0 +#endif + + +/* +** Define the maximum size of a temporary filename +*/ +#if SQLITE_OS_WIN +# include +# define SQLITE_TEMPNAME_SIZE (MAX_PATH+50) +#elif SQLITE_OS_OS2 +# if (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3) && defined(OS2_HIGH_MEMORY) +# include /* has to be included before os2.h for linking to work */ +# endif +# define INCL_DOSDATETIME +# define INCL_DOSFILEMGR +# define INCL_DOSERRORS +# define INCL_DOSMISC +# define INCL_DOSPROCESS +# define INCL_DOSMODULEMGR +# define INCL_DOSSEMAPHORES +# include +# include +# define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP) +#else +# define SQLITE_TEMPNAME_SIZE 200 +#endif + +/* If the SET_FULLSYNC macro is not defined above, then make it +** a no-op +*/ +#ifndef SET_FULLSYNC +# define SET_FULLSYNC(x,y) +#endif + +/* +** The default size of a disk sector +*/ +#ifndef SQLITE_DEFAULT_SECTOR_SIZE +# define SQLITE_DEFAULT_SECTOR_SIZE 512 +#endif + +/* +** Temporary files are named starting with this prefix followed by 16 random +** alphanumeric characters, and no file extension. They are stored in the +** OS's standard temporary file directory, and are deleted prior to exit. +** If sqlite is being embedded in another program, you may wish to change the +** prefix to reflect your program's name, so that if your program exits +** prematurely, old temporary files can be easily identified. This can be done +** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line. +** +** 2006-10-31: The default prefix used to be "sqlite_". But then +** Mcafee started using SQLite in their anti-virus product and it +** started putting files with the "sqlite" name in the c:/temp folder. +** This annoyed many windows users. Those users would then do a +** Google search for "sqlite", find the telephone numbers of the +** developers and call to wake them up at night and complain. +** For this reason, the default name prefix is changed to be "sqlite" +** spelled backwards. So the temp files are still identified, but +** anybody smart enough to figure out the code is also likely smart +** enough to know that calling the developer will not help get rid +** of the file. +*/ +#ifndef SQLITE_TEMP_FILE_PREFIX +# define SQLITE_TEMP_FILE_PREFIX "etilqs_" +#endif + +/* +** The following values may be passed as the second argument to +** sqlite3OsLock(). The various locks exhibit the following semantics: +** +** SHARED: Any number of processes may hold a SHARED lock simultaneously. +** RESERVED: A single process may hold a RESERVED lock on a file at +** any time. Other processes may hold and obtain new SHARED locks. +** PENDING: A single process may hold a PENDING lock on a file at +** any one time. Existing SHARED locks may persist, but no new +** SHARED locks may be obtained by other processes. +** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks. +** +** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a +** process that requests an EXCLUSIVE lock may actually obtain a PENDING +** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to +** sqlite3OsLock(). +*/ +#define NO_LOCK 0 +#define SHARED_LOCK 1 +#define RESERVED_LOCK 2 +#define PENDING_LOCK 3 +#define EXCLUSIVE_LOCK 4 + +/* +** File Locking Notes: (Mostly about windows but also some info for Unix) +** +** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because +** those functions are not available. So we use only LockFile() and +** UnlockFile(). +** +** LockFile() prevents not just writing but also reading by other processes. +** A SHARED_LOCK is obtained by locking a single randomly-chosen +** byte out of a specific range of bytes. The lock byte is obtained at +** random so two separate readers can probably access the file at the +** same time, unless they are unlucky and choose the same lock byte. +** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range. +** There can only be one writer. A RESERVED_LOCK is obtained by locking +** a single byte of the file that is designated as the reserved lock byte. +** A PENDING_LOCK is obtained by locking a designated byte different from +** the RESERVED_LOCK byte. +** +** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available, +** which means we can use reader/writer locks. When reader/writer locks +** are used, the lock is placed on the same range of bytes that is used +** for probabilistic locking in Win95/98/ME. Hence, the locking scheme +** will support two or more Win95 readers or two or more WinNT readers. +** But a single Win95 reader will lock out all WinNT readers and a single +** WinNT reader will lock out all other Win95 readers. +** +** The following #defines specify the range of bytes used for locking. +** SHARED_SIZE is the number of bytes available in the pool from which +** a random byte is selected for a shared lock. The pool of bytes for +** shared locks begins at SHARED_FIRST. +** +** The same locking strategy and +** byte ranges are used for Unix. This leaves open the possiblity of having +** clients on win95, winNT, and unix all talking to the same shared file +** and all locking correctly. To do so would require that samba (or whatever +** tool is being used for file sharing) implements locks correctly between +** windows and unix. I'm guessing that isn't likely to happen, but by +** using the same locking range we are at least open to the possibility. +** +** Locking in windows is manditory. For this reason, we cannot store +** actual data in the bytes used for locking. The pager never allocates +** the pages involved in locking therefore. SHARED_SIZE is selected so +** that all locks will fit on a single page even at the minimum page size. +** PENDING_BYTE defines the beginning of the locks. By default PENDING_BYTE +** is set high so that we don't have to allocate an unused page except +** for very large databases. But one should test the page skipping logic +** by setting PENDING_BYTE low and running the entire regression suite. +** +** Changing the value of PENDING_BYTE results in a subtly incompatible +** file format. Depending on how it is changed, you might not notice +** the incompatibility right away, even running a full regression test. +** The default location of PENDING_BYTE is the first byte past the +** 1GB boundary. +** +*/ +#ifdef SQLITE_OMIT_WSD +# define PENDING_BYTE (0x40000000) +#else +# define PENDING_BYTE sqlite3PendingByte +#endif +#define RESERVED_BYTE (PENDING_BYTE+1) +#define SHARED_FIRST (PENDING_BYTE+2) +#define SHARED_SIZE 510 + +/* +** Wrapper around OS specific sqlite3_os_init() function. +*/ +SQLITE_PRIVATE int sqlite3OsInit(void); + +/* +** Functions for accessing sqlite3_file methods +*/ +SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*); +SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset); +SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset); +SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size); +SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int); +SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize); +SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int); +SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int); +SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut); +SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*); +#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0 +SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id); +SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id); +SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **); +SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int); +SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id); +SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int); + +/* +** Functions for accessing sqlite3_vfs methods +*/ +SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *); +SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int); +SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut); +SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *); +#ifndef SQLITE_OMIT_LOAD_EXTENSION +SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *); +SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *); +SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void); +SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *); +#endif /* SQLITE_OMIT_LOAD_EXTENSION */ +SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *); +SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int); +SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*); + +/* +** Convenience functions for opening and closing files using +** sqlite3_malloc() to obtain space for the file-handle structure. +*/ +SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*); +SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *); + +#endif /* _SQLITE_OS_H_ */ + +/************** End of os.h **************************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ +/************** Include mutex.h in the middle of sqliteInt.h *****************/ +/************** Begin file mutex.h *******************************************/ +/* +** 2007 August 28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains the common header for all mutex implementations. +** The sqliteInt.h header #includes this file so that it is available +** to all source files. We break it out in an effort to keep the code +** better organized. +** +** NOTE: source files should *not* #include this header file directly. +** Source files should #include the sqliteInt.h file and let that file +** include this one indirectly. +*/ + + +/* +** Figure out what version of the code to use. The choices are +** +** SQLITE_MUTEX_OMIT No mutex logic. Not even stubs. The +** mutexes implemention cannot be overridden +** at start-time. +** +** SQLITE_MUTEX_NOOP For single-threaded applications. No +** mutual exclusion is provided. But this +** implementation can be overridden at +** start-time. +** +** SQLITE_MUTEX_PTHREADS For multi-threaded applications on Unix. +** +** SQLITE_MUTEX_W32 For multi-threaded applications on Win32. +** +** SQLITE_MUTEX_OS2 For multi-threaded applications on OS/2. +*/ +#if !SQLITE_THREADSAFE +# define SQLITE_MUTEX_OMIT +#endif +#if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP) +# if SQLITE_OS_UNIX +# define SQLITE_MUTEX_PTHREADS +# elif SQLITE_OS_WIN +# define SQLITE_MUTEX_W32 +# elif SQLITE_OS_OS2 +# define SQLITE_MUTEX_OS2 +# else +# define SQLITE_MUTEX_NOOP +# endif +#endif + +#ifdef SQLITE_MUTEX_OMIT +/* +** If this is a no-op implementation, implement everything as macros. +*/ +#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8) +#define sqlite3_mutex_free(X) +#define sqlite3_mutex_enter(X) +#define sqlite3_mutex_try(X) SQLITE_OK +#define sqlite3_mutex_leave(X) +#define sqlite3_mutex_held(X) ((void)(X),1) +#define sqlite3_mutex_notheld(X) ((void)(X),1) +#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8) +#define sqlite3MutexInit() SQLITE_OK +#define sqlite3MutexEnd() +#endif /* defined(SQLITE_MUTEX_OMIT) */ + +/************** End of mutex.h ***********************************************/ +/************** Continuing where we left off in sqliteInt.h ******************/ + + +/* +** Each database file to be accessed by the system is an instance +** of the following structure. There are normally two of these structures +** in the sqlite.aDb[] array. aDb[0] is the main database file and +** aDb[1] is the database file used to hold temporary tables. Additional +** databases may be attached. +*/ +struct Db { + char *zName; /* Name of this database */ + Btree *pBt; /* The B*Tree structure for this database file */ + u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */ + u8 safety_level; /* How aggressive at syncing data to disk */ + Schema *pSchema; /* Pointer to database schema (possibly shared) */ +}; + +/* +** An instance of the following structure stores a database schema. +*/ +struct Schema { + int schema_cookie; /* Database schema version number for this file */ + Hash tblHash; /* All tables indexed by name */ + Hash idxHash; /* All (named) indices indexed by name */ + Hash trigHash; /* All triggers indexed by name */ + Hash fkeyHash; /* All foreign keys by referenced table name */ + Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */ + u8 file_format; /* Schema format version for this file */ + u8 enc; /* Text encoding used by this database */ + u16 flags; /* Flags associated with this schema */ + int cache_size; /* Number of pages to use in the cache */ +}; + +/* +** These macros can be used to test, set, or clear bits in the +** Db.pSchema->flags field. +*/ +#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))==(P)) +#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))!=0) +#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->flags|=(P) +#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->flags&=~(P) + +/* +** Allowed values for the DB.pSchema->flags field. +** +** The DB_SchemaLoaded flag is set after the database schema has been +** read into internal hash tables. +** +** DB_UnresetViews means that one or more views have column names that +** have been filled out. If the schema changes, these column names might +** changes and so the view will need to be reset. +*/ +#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */ +#define DB_UnresetViews 0x0002 /* Some views have defined column names */ +#define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */ + +/* +** The number of different kinds of things that can be limited +** using the sqlite3_limit() interface. +*/ +#define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1) + +/* +** Lookaside malloc is a set of fixed-size buffers that can be used +** to satisfy small transient memory allocation requests for objects +** associated with a particular database connection. The use of +** lookaside malloc provides a significant performance enhancement +** (approx 10%) by avoiding numerous malloc/free requests while parsing +** SQL statements. +** +** The Lookaside structure holds configuration information about the +** lookaside malloc subsystem. Each available memory allocation in +** the lookaside subsystem is stored on a linked list of LookasideSlot +** objects. +** +** Lookaside allocations are only allowed for objects that are associated +** with a particular database connection. Hence, schema information cannot +** be stored in lookaside because in shared cache mode the schema information +** is shared by multiple database connections. Therefore, while parsing +** schema information, the Lookaside.bEnabled flag is cleared so that +** lookaside allocations are not used to construct the schema objects. +*/ +struct Lookaside { + u16 sz; /* Size of each buffer in bytes */ + u8 bEnabled; /* False to disable new lookaside allocations */ + u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */ + int nOut; /* Number of buffers currently checked out */ + int mxOut; /* Highwater mark for nOut */ + int anStat[3]; /* 0: hits. 1: size misses. 2: full misses */ + LookasideSlot *pFree; /* List of available buffers */ + void *pStart; /* First byte of available memory space */ + void *pEnd; /* First byte past end of available space */ +}; +struct LookasideSlot { + LookasideSlot *pNext; /* Next buffer in the list of free buffers */ +}; + +/* +** A hash table for function definitions. +** +** Hash each FuncDef structure into one of the FuncDefHash.a[] slots. +** Collisions are on the FuncDef.pHash chain. +*/ +struct FuncDefHash { + FuncDef *a[23]; /* Hash table for functions */ +}; + +/* +** Each database connection is an instance of the following structure. +** +** The sqlite.lastRowid records the last insert rowid generated by an +** insert statement. Inserts on views do not affect its value. Each +** trigger has its own context, so that lastRowid can be updated inside +** triggers as usual. The previous value will be restored once the trigger +** exits. Upon entering a before or instead of trigger, lastRowid is no +** longer (since after version 2.8.12) reset to -1. +** +** The sqlite.nChange does not count changes within triggers and keeps no +** context. It is reset at start of sqlite3_exec. +** The sqlite.lsChange represents the number of changes made by the last +** insert, update, or delete statement. It remains constant throughout the +** length of a statement and is then updated by OP_SetCounts. It keeps a +** context stack just like lastRowid so that the count of changes +** within a trigger is not seen outside the trigger. Changes to views do not +** affect the value of lsChange. +** The sqlite.csChange keeps track of the number of current changes (since +** the last statement) and is used to update sqlite_lsChange. +** +** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16 +** store the most recent error code and, if applicable, string. The +** internal function sqlite3Error() is used to set these variables +** consistently. +*/ +struct sqlite3 { + sqlite3_vfs *pVfs; /* OS Interface */ + int nDb; /* Number of backends currently in use */ + Db *aDb; /* All backends */ + int flags; /* Miscellaneous flags. See below */ + int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */ + int errCode; /* Most recent error code (SQLITE_*) */ + int errMask; /* & result codes with this before returning */ + u8 autoCommit; /* The auto-commit flag. */ + u8 temp_store; /* 1: file 2: memory 0: default */ + u8 mallocFailed; /* True if we have seen a malloc failure */ + u8 dfltLockMode; /* Default locking-mode for attached dbs */ + signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */ + u8 suppressErr; /* Do not issue error messages if true */ + int nextPagesize; /* Pagesize after VACUUM if >0 */ + int nTable; /* Number of tables in the database */ + CollSeq *pDfltColl; /* The default collating sequence (BINARY) */ + i64 lastRowid; /* ROWID of most recent insert (see above) */ + u32 magic; /* Magic number for detect library misuse */ + int nChange; /* Value returned by sqlite3_changes() */ + int nTotalChange; /* Value returned by sqlite3_total_changes() */ + sqlite3_mutex *mutex; /* Connection mutex */ + int aLimit[SQLITE_N_LIMIT]; /* Limits */ + struct sqlite3InitInfo { /* Information used during initialization */ + int iDb; /* When back is being initialized */ + int newTnum; /* Rootpage of table being initialized */ + u8 busy; /* TRUE if currently initializing */ + u8 orphanTrigger; /* Last statement is orphaned TEMP trigger */ + } init; + int nExtension; /* Number of loaded extensions */ + void **aExtension; /* Array of shared library handles */ + struct Vdbe *pVdbe; /* List of active virtual machines */ + int activeVdbeCnt; /* Number of VDBEs currently executing */ + int writeVdbeCnt; /* Number of active VDBEs that are writing */ + int vdbeExecCnt; /* Number of nested calls to VdbeExec() */ + void (*xTrace)(void*,const char*); /* Trace function */ + void *pTraceArg; /* Argument to the trace function */ + void (*xProfile)(void*,const char*,u64); /* Profiling function */ + void *pProfileArg; /* Argument to profile function */ + void *pCommitArg; /* Argument to xCommitCallback() */ + int (*xCommitCallback)(void*); /* Invoked at every commit. */ + void *pRollbackArg; /* Argument to xRollbackCallback() */ + void (*xRollbackCallback)(void*); /* Invoked at every commit. */ + void *pUpdateArg; + void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64); +#ifndef SQLITE_OMIT_WAL + int (*xWalCallback)(void *, sqlite3 *, const char *, int); + void *pWalArg; +#endif + void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*); + void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*); + void *pCollNeededArg; + sqlite3_value *pErr; /* Most recent error message */ + char *zErrMsg; /* Most recent error message (UTF-8 encoded) */ + char *zErrMsg16; /* Most recent error message (UTF-16 encoded) */ + union { + volatile int isInterrupted; /* True if sqlite3_interrupt has been called */ + double notUsed1; /* Spacer */ + } u1; + Lookaside lookaside; /* Lookaside malloc configuration */ +#ifndef SQLITE_OMIT_AUTHORIZATION + int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); + /* Access authorization function */ + void *pAuthArg; /* 1st argument to the access auth function */ +#endif +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + int (*xProgress)(void *); /* The progress callback */ + void *pProgressArg; /* Argument to the progress callback */ + int nProgressOps; /* Number of opcodes for progress callback */ +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + Hash aModule; /* populated by sqlite3_create_module() */ + Table *pVTab; /* vtab with active Connect/Create method */ + VTable **aVTrans; /* Virtual tables with open transactions */ + int nVTrans; /* Allocated size of aVTrans */ + VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */ +#endif + FuncDefHash aFunc; /* Hash table of connection functions */ + Hash aCollSeq; /* All collating sequences */ + BusyHandler busyHandler; /* Busy callback */ + int busyTimeout; /* Busy handler timeout, in msec */ + Db aDbStatic[2]; /* Static space for the 2 default backends */ + Savepoint *pSavepoint; /* List of active savepoints */ + int nSavepoint; /* Number of non-transaction savepoints */ + int nStatement; /* Number of nested statement-transactions */ + u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */ + i64 nDeferredCons; /* Net deferred constraints this transaction. */ + int *pnBytesFreed; /* If not NULL, increment this in DbFree() */ + +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY + /* The following variables are all protected by the STATIC_MASTER + ** mutex, not by sqlite3.mutex. They are used by code in notify.c. + ** + ** When X.pUnlockConnection==Y, that means that X is waiting for Y to + ** unlock so that it can proceed. + ** + ** When X.pBlockingConnection==Y, that means that something that X tried + ** tried to do recently failed with an SQLITE_LOCKED error due to locks + ** held by Y. + */ + sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */ + sqlite3 *pUnlockConnection; /* Connection to watch for unlock */ + void *pUnlockArg; /* Argument to xUnlockNotify */ + void (*xUnlockNotify)(void **, int); /* Unlock notify callback */ + sqlite3 *pNextBlocked; /* Next in list of all blocked connections */ +#endif +}; + +/* +** A macro to discover the encoding of a database. +*/ +#define ENC(db) ((db)->aDb[0].pSchema->enc) + +/* +** Possible values for the sqlite3.flags. +*/ +#define SQLITE_VdbeTrace 0x00000100 /* True to trace VDBE execution */ +#define SQLITE_InternChanges 0x00000200 /* Uncommitted Hash table changes */ +#define SQLITE_FullColNames 0x00000400 /* Show full column names on SELECT */ +#define SQLITE_ShortColNames 0x00000800 /* Show short columns names */ +#define SQLITE_CountRows 0x00001000 /* Count rows changed by INSERT, */ + /* DELETE, or UPDATE and return */ + /* the count using a callback. */ +#define SQLITE_NullCallback 0x00002000 /* Invoke the callback once if the */ + /* result set is empty */ +#define SQLITE_SqlTrace 0x00004000 /* Debug print SQL as it executes */ +#define SQLITE_VdbeListing 0x00008000 /* Debug listings of VDBE programs */ +#define SQLITE_WriteSchema 0x00010000 /* OK to update SQLITE_MASTER */ +#define SQLITE_NoReadlock 0x00020000 /* Readlocks are omitted when + ** accessing read-only databases */ +#define SQLITE_IgnoreChecks 0x00040000 /* Do not enforce check constraints */ +#define SQLITE_ReadUncommitted 0x0080000 /* For shared-cache mode */ +#define SQLITE_LegacyFileFmt 0x00100000 /* Create new databases in format 1 */ +#define SQLITE_FullFSync 0x00200000 /* Use full fsync on the backend */ +#define SQLITE_CkptFullFSync 0x00400000 /* Use full fsync for checkpoint */ +#define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */ +#define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */ +#define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */ +#define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */ +#define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */ +#define SQLITE_PreferBuiltin 0x10000000 /* Preference to built-in funcs */ +#define SQLITE_LoadExtension 0x20000000 /* Enable load_extension */ + +/* +** Bits of the sqlite3.flags field that are used by the +** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface. +** These must be the low-order bits of the flags field. +*/ +#define SQLITE_QueryFlattener 0x01 /* Disable query flattening */ +#define SQLITE_ColumnCache 0x02 /* Disable the column cache */ +#define SQLITE_IndexSort 0x04 /* Disable indexes for sorting */ +#define SQLITE_IndexSearch 0x08 /* Disable indexes for searching */ +#define SQLITE_IndexCover 0x10 /* Disable index covering table */ +#define SQLITE_GroupByOrder 0x20 /* Disable GROUPBY cover of ORDERBY */ +#define SQLITE_FactorOutConst 0x40 /* Disable factoring out constants */ +#define SQLITE_OptMask 0xff /* Mask of all disablable opts */ + +/* +** Possible values for the sqlite.magic field. +** The numbers are obtained at random and have no special meaning, other +** than being distinct from one another. +*/ +#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */ +#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */ +#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */ +#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */ +#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */ + +/* +** Each SQL function is defined by an instance of the following +** structure. A pointer to this structure is stored in the sqlite.aFunc +** hash table. When multiple functions have the same name, the hash table +** points to a linked list of these structures. +*/ +struct FuncDef { + i16 nArg; /* Number of arguments. -1 means unlimited */ + u8 iPrefEnc; /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */ + u8 flags; /* Some combination of SQLITE_FUNC_* */ + void *pUserData; /* User data parameter */ + FuncDef *pNext; /* Next function with same name */ + void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */ + void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */ + void (*xFinalize)(sqlite3_context*); /* Aggregate finalizer */ + char *zName; /* SQL name of the function. */ + FuncDef *pHash; /* Next with a different name but the same hash */ + FuncDestructor *pDestructor; /* Reference counted destructor function */ +}; + +/* +** This structure encapsulates a user-function destructor callback (as +** configured using create_function_v2()) and a reference counter. When +** create_function_v2() is called to create a function with a destructor, +** a single object of this type is allocated. FuncDestructor.nRef is set to +** the number of FuncDef objects created (either 1 or 3, depending on whether +** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor +** member of each of the new FuncDef objects is set to point to the allocated +** FuncDestructor. +** +** Thereafter, when one of the FuncDef objects is deleted, the reference +** count on this object is decremented. When it reaches 0, the destructor +** is invoked and the FuncDestructor structure freed. +*/ +struct FuncDestructor { + int nRef; + void (*xDestroy)(void *); + void *pUserData; +}; + +/* +** Possible values for FuncDef.flags +*/ +#define SQLITE_FUNC_LIKE 0x01 /* Candidate for the LIKE optimization */ +#define SQLITE_FUNC_CASE 0x02 /* Case-sensitive LIKE-type function */ +#define SQLITE_FUNC_EPHEM 0x04 /* Ephemeral. Delete with VDBE */ +#define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */ +#define SQLITE_FUNC_PRIVATE 0x10 /* Allowed for internal use only */ +#define SQLITE_FUNC_COUNT 0x20 /* Built-in count(*) aggregate */ +#define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */ + +/* +** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are +** used to create the initializers for the FuncDef structures. +** +** FUNCTION(zName, nArg, iArg, bNC, xFunc) +** Used to create a scalar function definition of a function zName +** implemented by C function xFunc that accepts nArg arguments. The +** value passed as iArg is cast to a (void*) and made available +** as the user-data (sqlite3_user_data()) for the function. If +** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set. +** +** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal) +** Used to create an aggregate function definition implemented by +** the C functions xStep and xFinal. The first four parameters +** are interpreted in the same way as the first 4 parameters to +** FUNCTION(). +** +** LIKEFUNC(zName, nArg, pArg, flags) +** Used to create a scalar function definition of a function zName +** that accepts nArg arguments and is implemented by a call to C +** function likeFunc. Argument pArg is cast to a (void *) and made +** available as the function user-data (sqlite3_user_data()). The +** FuncDef.flags variable is set to the value passed as the flags +** parameter. +*/ +#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \ + {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \ + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0} +#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \ + {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \ + pArg, 0, xFunc, 0, 0, #zName, 0, 0} +#define LIKEFUNC(zName, nArg, arg, flags) \ + {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0} +#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \ + {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \ + SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0} + +/* +** All current savepoints are stored in a linked list starting at +** sqlite3.pSavepoint. The first element in the list is the most recently +** opened savepoint. Savepoints are added to the list by the vdbe +** OP_Savepoint instruction. +*/ +struct Savepoint { + char *zName; /* Savepoint name (nul-terminated) */ + i64 nDeferredCons; /* Number of deferred fk violations */ + Savepoint *pNext; /* Parent savepoint (if any) */ +}; + +/* +** The following are used as the second parameter to sqlite3Savepoint(), +** and as the P1 argument to the OP_Savepoint instruction. +*/ +#define SAVEPOINT_BEGIN 0 +#define SAVEPOINT_RELEASE 1 +#define SAVEPOINT_ROLLBACK 2 + + +/* +** Each SQLite module (virtual table definition) is defined by an +** instance of the following structure, stored in the sqlite3.aModule +** hash table. +*/ +struct Module { + const sqlite3_module *pModule; /* Callback pointers */ + const char *zName; /* Name passed to create_module() */ + void *pAux; /* pAux passed to create_module() */ + void (*xDestroy)(void *); /* Module destructor function */ +}; + +/* +** information about each column of an SQL table is held in an instance +** of this structure. +*/ +struct Column { + char *zName; /* Name of this column */ + Expr *pDflt; /* Default value of this column */ + char *zDflt; /* Original text of the default value */ + char *zType; /* Data type for this column */ + char *zColl; /* Collating sequence. If NULL, use the default */ + u8 notNull; /* True if there is a NOT NULL constraint */ + u8 isPrimKey; /* True if this column is part of the PRIMARY KEY */ + char affinity; /* One of the SQLITE_AFF_... values */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + u8 isHidden; /* True if this column is 'hidden' */ +#endif +}; + +/* +** A "Collating Sequence" is defined by an instance of the following +** structure. Conceptually, a collating sequence consists of a name and +** a comparison routine that defines the order of that sequence. +** +** There may two separate implementations of the collation function, one +** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that +** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine +** native byte order. When a collation sequence is invoked, SQLite selects +** the version that will require the least expensive encoding +** translations, if any. +** +** The CollSeq.pUser member variable is an extra parameter that passed in +** as the first argument to the UTF-8 comparison function, xCmp. +** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function, +** xCmp16. +** +** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the +** collating sequence is undefined. Indices built on an undefined +** collating sequence may not be read or written. +*/ +struct CollSeq { + char *zName; /* Name of the collating sequence, UTF-8 encoded */ + u8 enc; /* Text encoding handled by xCmp() */ + u8 type; /* One of the SQLITE_COLL_... values below */ + void *pUser; /* First argument to xCmp() */ + int (*xCmp)(void*,int, const void*, int, const void*); + void (*xDel)(void*); /* Destructor for pUser */ +}; + +/* +** Allowed values of CollSeq.type: +*/ +#define SQLITE_COLL_BINARY 1 /* The default memcmp() collating sequence */ +#define SQLITE_COLL_NOCASE 2 /* The built-in NOCASE collating sequence */ +#define SQLITE_COLL_REVERSE 3 /* The built-in REVERSE collating sequence */ +#define SQLITE_COLL_USER 0 /* Any other user-defined collating sequence */ + +/* +** A sort order can be either ASC or DESC. +*/ +#define SQLITE_SO_ASC 0 /* Sort in ascending order */ +#define SQLITE_SO_DESC 1 /* Sort in ascending order */ + +/* +** Column affinity types. +** +** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and +** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve +** the speed a little by numbering the values consecutively. +** +** But rather than start with 0 or 1, we begin with 'a'. That way, +** when multiple affinity types are concatenated into a string and +** used as the P4 operand, they will be more readable. +** +** Note also that the numeric types are grouped together so that testing +** for a numeric type is a single comparison. +*/ +#define SQLITE_AFF_TEXT 'a' +#define SQLITE_AFF_NONE 'b' +#define SQLITE_AFF_NUMERIC 'c' +#define SQLITE_AFF_INTEGER 'd' +#define SQLITE_AFF_REAL 'e' + +#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC) + +/* +** The SQLITE_AFF_MASK values masks off the significant bits of an +** affinity value. +*/ +#define SQLITE_AFF_MASK 0x67 + +/* +** Additional bit values that can be ORed with an affinity without +** changing the affinity. +*/ +#define SQLITE_JUMPIFNULL 0x08 /* jumps if either operand is NULL */ +#define SQLITE_STOREP2 0x10 /* Store result in reg[P2] rather than jump */ +#define SQLITE_NULLEQ 0x80 /* NULL=NULL */ + +/* +** An object of this type is created for each virtual table present in +** the database schema. +** +** If the database schema is shared, then there is one instance of this +** structure for each database connection (sqlite3*) that uses the shared +** schema. This is because each database connection requires its own unique +** instance of the sqlite3_vtab* handle used to access the virtual table +** implementation. sqlite3_vtab* handles can not be shared between +** database connections, even when the rest of the in-memory database +** schema is shared, as the implementation often stores the database +** connection handle passed to it via the xConnect() or xCreate() method +** during initialization internally. This database connection handle may +** then used by the virtual table implementation to access real tables +** within the database. So that they appear as part of the callers +** transaction, these accesses need to be made via the same database +** connection as that used to execute SQL operations on the virtual table. +** +** All VTable objects that correspond to a single table in a shared +** database schema are initially stored in a linked-list pointed to by +** the Table.pVTable member variable of the corresponding Table object. +** When an sqlite3_prepare() operation is required to access the virtual +** table, it searches the list for the VTable that corresponds to the +** database connection doing the preparing so as to use the correct +** sqlite3_vtab* handle in the compiled query. +** +** When an in-memory Table object is deleted (for example when the +** schema is being reloaded for some reason), the VTable objects are not +** deleted and the sqlite3_vtab* handles are not xDisconnect()ed +** immediately. Instead, they are moved from the Table.pVTable list to +** another linked list headed by the sqlite3.pDisconnect member of the +** corresponding sqlite3 structure. They are then deleted/xDisconnected +** next time a statement is prepared using said sqlite3*. This is done +** to avoid deadlock issues involving multiple sqlite3.mutex mutexes. +** Refer to comments above function sqlite3VtabUnlockList() for an +** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect +** list without holding the corresponding sqlite3.mutex mutex. +** +** The memory for objects of this type is always allocated by +** sqlite3DbMalloc(), using the connection handle stored in VTable.db as +** the first argument. +*/ +struct VTable { + sqlite3 *db; /* Database connection associated with this table */ + Module *pMod; /* Pointer to module implementation */ + sqlite3_vtab *pVtab; /* Pointer to vtab instance */ + int nRef; /* Number of pointers to this structure */ + VTable *pNext; /* Next in linked list (see above) */ +}; + +/* +** Each SQL table is represented in memory by an instance of the +** following structure. +** +** Table.zName is the name of the table. The case of the original +** CREATE TABLE statement is stored, but case is not significant for +** comparisons. +** +** Table.nCol is the number of columns in this table. Table.aCol is a +** pointer to an array of Column structures, one for each column. +** +** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of +** the column that is that key. Otherwise Table.iPKey is negative. Note +** that the datatype of the PRIMARY KEY must be INTEGER for this field to +** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of +** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid +** is generated for each row of the table. TF_HasPrimaryKey is set if +** the table has any PRIMARY KEY, INTEGER or otherwise. +** +** Table.tnum is the page number for the root BTree page of the table in the +** database file. If Table.iDb is the index of the database table backend +** in sqlite.aDb[]. 0 is for the main database and 1 is for the file that +** holds temporary tables and indices. If TF_Ephemeral is set +** then the table is stored in a file that is automatically deleted +** when the VDBE cursor to the table is closed. In this case Table.tnum +** refers VDBE cursor number that holds the table open, not to the root +** page number. Transient tables are used to hold the results of a +** sub-query that appears instead of a real table name in the FROM clause +** of a SELECT statement. +*/ +struct Table { + char *zName; /* Name of the table or view */ + int iPKey; /* If not negative, use aCol[iPKey] as the primary key */ + int nCol; /* Number of columns in this table */ + Column *aCol; /* Information about each column */ + Index *pIndex; /* List of SQL indexes on this table. */ + int tnum; /* Root BTree node for this table (see note above) */ + unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */ + Select *pSelect; /* NULL for tables. Points to definition if a view. */ + u16 nRef; /* Number of pointers to this Table */ + u8 tabFlags; /* Mask of TF_* values */ + u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ + FKey *pFKey; /* Linked list of all foreign keys in this table */ + char *zColAff; /* String defining the affinity of each column */ +#ifndef SQLITE_OMIT_CHECK + Expr *pCheck; /* The AND of all CHECK constraints */ +#endif +#ifndef SQLITE_OMIT_ALTERTABLE + int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */ +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + VTable *pVTable; /* List of VTable objects. */ + int nModuleArg; /* Number of arguments to the module */ + char **azModuleArg; /* Text of all module args. [0] is module name */ +#endif + Trigger *pTrigger; /* List of triggers stored in pSchema */ + Schema *pSchema; /* Schema that contains this table */ + Table *pNextZombie; /* Next on the Parse.pZombieTab list */ +}; + +/* +** Allowed values for Tabe.tabFlags. +*/ +#define TF_Readonly 0x01 /* Read-only system table */ +#define TF_Ephemeral 0x02 /* An ephemeral table */ +#define TF_HasPrimaryKey 0x04 /* Table has a primary key */ +#define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */ +#define TF_Virtual 0x10 /* Is a virtual table */ +#define TF_NeedMetadata 0x20 /* aCol[].zType and aCol[].pColl missing */ + + + +/* +** Test to see whether or not a table is a virtual table. This is +** done as a macro so that it will be optimized out when virtual +** table support is omitted from the build. +*/ +#ifndef SQLITE_OMIT_VIRTUALTABLE +# define IsVirtual(X) (((X)->tabFlags & TF_Virtual)!=0) +# define IsHiddenColumn(X) ((X)->isHidden) +#else +# define IsVirtual(X) 0 +# define IsHiddenColumn(X) 0 +#endif + +/* +** Each foreign key constraint is an instance of the following structure. +** +** A foreign key is associated with two tables. The "from" table is +** the table that contains the REFERENCES clause that creates the foreign +** key. The "to" table is the table that is named in the REFERENCES clause. +** Consider this example: +** +** CREATE TABLE ex1( +** a INTEGER PRIMARY KEY, +** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x) +** ); +** +** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2". +** +** Each REFERENCES clause generates an instance of the following structure +** which is attached to the from-table. The to-table need not exist when +** the from-table is created. The existence of the to-table is not checked. +*/ +struct FKey { + Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */ + FKey *pNextFrom; /* Next foreign key in pFrom */ + char *zTo; /* Name of table that the key points to (aka: Parent) */ + FKey *pNextTo; /* Next foreign key on table named zTo */ + FKey *pPrevTo; /* Previous foreign key on table named zTo */ + int nCol; /* Number of columns in this key */ + /* EV: R-30323-21917 */ + u8 isDeferred; /* True if constraint checking is deferred till COMMIT */ + u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */ + Trigger *apTrigger[2]; /* Triggers for aAction[] actions */ + struct sColMap { /* Mapping of columns in pFrom to columns in zTo */ + int iFrom; /* Index of column in pFrom */ + char *zCol; /* Name of column in zTo. If 0 use PRIMARY KEY */ + } aCol[1]; /* One entry for each of nCol column s */ +}; + +/* +** SQLite supports many different ways to resolve a constraint +** error. ROLLBACK processing means that a constraint violation +** causes the operation in process to fail and for the current transaction +** to be rolled back. ABORT processing means the operation in process +** fails and any prior changes from that one operation are backed out, +** but the transaction is not rolled back. FAIL processing means that +** the operation in progress stops and returns an error code. But prior +** changes due to the same operation are not backed out and no rollback +** occurs. IGNORE means that the particular row that caused the constraint +** error is not inserted or updated. Processing continues and no error +** is returned. REPLACE means that preexisting database rows that caused +** a UNIQUE constraint violation are removed so that the new insert or +** update can proceed. Processing continues and no error is reported. +** +** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys. +** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the +** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign +** key is set to NULL. CASCADE means that a DELETE or UPDATE of the +** referenced table row is propagated into the row that holds the +** foreign key. +** +** The following symbolic values are used to record which type +** of action to take. +*/ +#define OE_None 0 /* There is no constraint to check */ +#define OE_Rollback 1 /* Fail the operation and rollback the transaction */ +#define OE_Abort 2 /* Back out changes but do no rollback transaction */ +#define OE_Fail 3 /* Stop the operation but leave all prior changes */ +#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */ +#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */ + +#define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */ +#define OE_SetNull 7 /* Set the foreign key value to NULL */ +#define OE_SetDflt 8 /* Set the foreign key value to its default */ +#define OE_Cascade 9 /* Cascade the changes */ + +#define OE_Default 99 /* Do whatever the default action is */ + + +/* +** An instance of the following structure is passed as the first +** argument to sqlite3VdbeKeyCompare and is used to control the +** comparison of the two index keys. +*/ +struct KeyInfo { + sqlite3 *db; /* The database connection */ + u8 enc; /* Text encoding - one of the SQLITE_UTF* values */ + u16 nField; /* Number of entries in aColl[] */ + u8 *aSortOrder; /* Sort order for each column. May be NULL */ + CollSeq *aColl[1]; /* Collating sequence for each term of the key */ +}; + +/* +** An instance of the following structure holds information about a +** single index record that has already been parsed out into individual +** values. +** +** A record is an object that contains one or more fields of data. +** Records are used to store the content of a table row and to store +** the key of an index. A blob encoding of a record is created by +** the OP_MakeRecord opcode of the VDBE and is disassembled by the +** OP_Column opcode. +** +** This structure holds a record that has already been disassembled +** into its constituent fields. +*/ +struct UnpackedRecord { + KeyInfo *pKeyInfo; /* Collation and sort-order information */ + u16 nField; /* Number of entries in apMem[] */ + u16 flags; /* Boolean settings. UNPACKED_... below */ + i64 rowid; /* Used by UNPACKED_PREFIX_SEARCH */ + Mem *aMem; /* Values */ +}; + +/* +** Allowed values of UnpackedRecord.flags +*/ +#define UNPACKED_NEED_FREE 0x0001 /* Memory is from sqlite3Malloc() */ +#define UNPACKED_NEED_DESTROY 0x0002 /* apMem[]s should all be destroyed */ +#define UNPACKED_IGNORE_ROWID 0x0004 /* Ignore trailing rowid on key1 */ +#define UNPACKED_INCRKEY 0x0008 /* Make this key an epsilon larger */ +#define UNPACKED_PREFIX_MATCH 0x0010 /* A prefix match is considered OK */ +#define UNPACKED_PREFIX_SEARCH 0x0020 /* A prefix match is considered OK */ + +/* +** Each SQL index is represented in memory by an +** instance of the following structure. +** +** The columns of the table that are to be indexed are described +** by the aiColumn[] field of this structure. For example, suppose +** we have the following table and index: +** +** CREATE TABLE Ex1(c1 int, c2 int, c3 text); +** CREATE INDEX Ex2 ON Ex1(c3,c1); +** +** In the Table structure describing Ex1, nCol==3 because there are +** three columns in the table. In the Index structure describing +** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed. +** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the +** first column to be indexed (c3) has an index of 2 in Ex1.aCol[]. +** The second column to be indexed (c1) has an index of 0 in +** Ex1.aCol[], hence Ex2.aiColumn[1]==0. +** +** The Index.onError field determines whether or not the indexed columns +** must be unique and what to do if they are not. When Index.onError=OE_None, +** it means this is not a unique index. Otherwise it is a unique index +** and the value of Index.onError indicate the which conflict resolution +** algorithm to employ whenever an attempt is made to insert a non-unique +** element. +*/ +struct Index { + char *zName; /* Name of this index */ + int nColumn; /* Number of columns in the table used by this index */ + int *aiColumn; /* Which columns are used by this index. 1st is 0 */ + unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */ + Table *pTable; /* The SQL table being indexed */ + int tnum; /* Page containing root of this index in database file */ + u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ + u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */ + char *zColAff; /* String defining the affinity of each column */ + Index *pNext; /* The next index associated with the same table */ + Schema *pSchema; /* Schema containing this index */ + u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */ + char **azColl; /* Array of collation sequence names for index */ + IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */ +}; + +/* +** Each sample stored in the sqlite_stat2 table is represented in memory +** using a structure of this type. +*/ +struct IndexSample { + union { + char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */ + double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */ + } u; + u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */ + u8 nByte; /* Size in byte of text or blob. */ +}; + +/* +** Each token coming out of the lexer is an instance of +** this structure. Tokens are also used as part of an expression. +** +** Note if Token.z==0 then Token.dyn and Token.n are undefined and +** may contain random values. Do not make any assumptions about Token.dyn +** and Token.n when Token.z==0. +*/ +struct Token { + const char *z; /* Text of the token. Not NULL-terminated! */ + unsigned int n; /* Number of characters in this token */ +}; + +/* +** An instance of this structure contains information needed to generate +** code for a SELECT that contains aggregate functions. +** +** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a +** pointer to this structure. The Expr.iColumn field is the index in +** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate +** code for that node. +** +** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the +** original Select structure that describes the SELECT statement. These +** fields do not need to be freed when deallocating the AggInfo structure. +*/ +struct AggInfo { + u8 directMode; /* Direct rendering mode means take data directly + ** from source tables rather than from accumulators */ + u8 useSortingIdx; /* In direct mode, reference the sorting index rather + ** than the source table */ + int sortingIdx; /* Cursor number of the sorting index */ + ExprList *pGroupBy; /* The group by clause */ + int nSortingColumn; /* Number of columns in the sorting index */ + struct AggInfo_col { /* For each column used in source tables */ + Table *pTab; /* Source table */ + int iTable; /* Cursor number of the source table */ + int iColumn; /* Column number within the source table */ + int iSorterColumn; /* Column number in the sorting index */ + int iMem; /* Memory location that acts as accumulator */ + Expr *pExpr; /* The original expression */ + } *aCol; + int nColumn; /* Number of used entries in aCol[] */ + int nColumnAlloc; /* Number of slots allocated for aCol[] */ + int nAccumulator; /* Number of columns that show through to the output. + ** Additional columns are used only as parameters to + ** aggregate functions */ + struct AggInfo_func { /* For each aggregate function */ + Expr *pExpr; /* Expression encoding the function */ + FuncDef *pFunc; /* The aggregate function implementation */ + int iMem; /* Memory location that acts as accumulator */ + int iDistinct; /* Ephemeral table used to enforce DISTINCT */ + } *aFunc; + int nFunc; /* Number of entries in aFunc[] */ + int nFuncAlloc; /* Number of slots allocated for aFunc[] */ +}; + +/* +** The datatype ynVar is a signed integer, either 16-bit or 32-bit. +** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater +** than 32767 we have to make it 32-bit. 16-bit is preferred because +** it uses less memory in the Expr object, which is a big memory user +** in systems with lots of prepared statements. And few applications +** need more than about 10 or 20 variables. But some extreme users want +** to have prepared statements with over 32767 variables, and for them +** the option is available (at compile-time). +*/ +#if SQLITE_MAX_VARIABLE_NUMBER<=32767 +typedef i16 ynVar; +#else +typedef int ynVar; +#endif + +/* +** Each node of an expression in the parse tree is an instance +** of this structure. +** +** Expr.op is the opcode. The integer parser token codes are reused +** as opcodes here. For example, the parser defines TK_GE to be an integer +** code representing the ">=" operator. This same integer code is reused +** to represent the greater-than-or-equal-to operator in the expression +** tree. +** +** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, +** or TK_STRING), then Expr.token contains the text of the SQL literal. If +** the expression is a variable (TK_VARIABLE), then Expr.token contains the +** variable name. Finally, if the expression is an SQL function (TK_FUNCTION), +** then Expr.token contains the name of the function. +** +** Expr.pRight and Expr.pLeft are the left and right subexpressions of a +** binary operator. Either or both may be NULL. +** +** Expr.x.pList is a list of arguments if the expression is an SQL function, +** a CASE expression or an IN expression of the form " IN (, ...)". +** Expr.x.pSelect is used if the expression is a sub-select or an expression of +** the form " IN (SELECT ...)". If the EP_xIsSelect bit is set in the +** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is +** valid. +** +** An expression of the form ID or ID.ID refers to a column in a table. +** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is +** the integer cursor number of a VDBE cursor pointing to that table and +** Expr.iColumn is the column number for the specific column. If the +** expression is used as a result in an aggregate SELECT, then the +** value is also stored in the Expr.iAgg column in the aggregate so that +** it can be accessed after all aggregates are computed. +** +** If the expression is an unbound variable marker (a question mark +** character '?' in the original SQL) then the Expr.iTable holds the index +** number for that variable. +** +** If the expression is a subquery then Expr.iColumn holds an integer +** register number containing the result of the subquery. If the +** subquery gives a constant result, then iTable is -1. If the subquery +** gives a different answer at different times during statement processing +** then iTable is the address of a subroutine that computes the subquery. +** +** If the Expr is of type OP_Column, and the table it is selecting from +** is a disk table or the "old.*" pseudo-table, then pTab points to the +** corresponding table definition. +** +** ALLOCATION NOTES: +** +** Expr objects can use a lot of memory space in database schema. To +** help reduce memory requirements, sometimes an Expr object will be +** truncated. And to reduce the number of memory allocations, sometimes +** two or more Expr objects will be stored in a single memory allocation, +** together with Expr.zToken strings. +** +** If the EP_Reduced and EP_TokenOnly flags are set when +** an Expr object is truncated. When EP_Reduced is set, then all +** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees +** are contained within the same memory allocation. Note, however, that +** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately +** allocated, regardless of whether or not EP_Reduced is set. +*/ +struct Expr { + u8 op; /* Operation performed by this node */ + char affinity; /* The affinity of the column or 0 if not a column */ + u16 flags; /* Various flags. EP_* See below */ + union { + char *zToken; /* Token value. Zero terminated and dequoted */ + int iValue; /* Non-negative integer value if EP_IntValue */ + } u; + + /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no + ** space is allocated for the fields below this point. An attempt to + ** access them will result in a segfault or malfunction. + *********************************************************************/ + + Expr *pLeft; /* Left subnode */ + Expr *pRight; /* Right subnode */ + union { + ExprList *pList; /* Function arguments or in " IN ( IN (
    +** +** If the prNotFound parameter is 0, then the b-tree will be used to iterate +** through the set members, skipping any duplicates. In this case an +** epheremal table must be used unless the selected is guaranteed +** to be unique - either because it is an INTEGER PRIMARY KEY or it +** has a UNIQUE constraint or UNIQUE index. +** +** If the prNotFound parameter is not 0, then the b-tree will be used +** for fast set membership tests. In this case an epheremal table must +** be used unless is an INTEGER PRIMARY KEY or an index can +** be found with as its left-most column. +** +** When the b-tree is being used for membership tests, the calling function +** needs to know whether or not the structure contains an SQL NULL +** value in order to correctly evaluate expressions like "X IN (Y, Z)". +** If there is any chance that the (...) might contain a NULL value at +** runtime, then a register is allocated and the register number written +** to *prNotFound. If there is no chance that the (...) contains a +** NULL value, then *prNotFound is left unchanged. +** +** If a register is allocated and its location stored in *prNotFound, then +** its initial value is NULL. If the (...) does not remain constant +** for the duration of the query (i.e. the SELECT within the (...) +** is a correlated subquery) then the value of the allocated register is +** reset to NULL each time the subquery is rerun. This allows the +** caller to use vdbe code equivalent to the following: +** +** if( register==NULL ){ +** has_null = +** register = 1 +** } +** +** in order to avoid running the +** test more often than is necessary. +*/ +#ifndef SQLITE_OMIT_SUBQUERY +SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){ + Select *p; /* SELECT to the right of IN operator */ + int eType = 0; /* Type of RHS table. IN_INDEX_* */ + int iTab = pParse->nTab++; /* Cursor of the RHS table */ + int mustBeUnique = (prNotFound==0); /* True if RHS must be unique */ + + assert( pX->op==TK_IN ); + + /* Check to see if an existing table or index can be used to + ** satisfy the query. This is preferable to generating a new + ** ephemeral table. + */ + p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0); + if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){ + sqlite3 *db = pParse->db; /* Database connection */ + Expr *pExpr = p->pEList->a[0].pExpr; /* Expression */ + int iCol = pExpr->iColumn; /* Index of column */ + Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */ + Table *pTab = p->pSrc->a[0].pTab; /* Table
    . */ + int iDb; /* Database idx for pTab */ + + /* Code an OP_VerifyCookie and OP_TableLock for
    . */ + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + sqlite3CodeVerifySchema(pParse, iDb); + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); + + /* This function is only called from two places. In both cases the vdbe + ** has already been allocated. So assume sqlite3GetVdbe() is always + ** successful here. + */ + assert(v); + if( iCol<0 ){ + int iMem = ++pParse->nMem; + int iAddr; + + iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); + + sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); + eType = IN_INDEX_ROWID; + + sqlite3VdbeJumpHere(v, iAddr); + }else{ + Index *pIdx; /* Iterator variable */ + + /* The collation sequence used by the comparison. If an index is to + ** be used in place of a temp-table, it must be ordered according + ** to this collation sequence. */ + CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr); + + /* Check that the affinity that will be used to perform the + ** comparison is the same as the affinity of the column. If + ** it is not, it is not possible to use any index. + */ + char aff = comparisonAffinity(pX); + int affinity_ok = (pTab->aCol[iCol].affinity==aff||aff==SQLITE_AFF_NONE); + + for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){ + if( (pIdx->aiColumn[0]==iCol) + && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq + && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None)) + ){ + int iMem = ++pParse->nMem; + int iAddr; + char *pKey; + + pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx); + iAddr = sqlite3VdbeAddOp1(v, OP_If, iMem); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iMem); + + sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb, + pKey,P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pIdx->zName)); + eType = IN_INDEX_INDEX; + + sqlite3VdbeJumpHere(v, iAddr); + if( prNotFound && !pTab->aCol[iCol].notNull ){ + *prNotFound = ++pParse->nMem; + } + } + } + } + } + + if( eType==0 ){ + /* Could not found an existing table or index to use as the RHS b-tree. + ** We will have to generate an ephemeral table to do the job. + */ + double savedNQueryLoop = pParse->nQueryLoop; + int rMayHaveNull = 0; + eType = IN_INDEX_EPH; + if( prNotFound ){ + *prNotFound = rMayHaveNull = ++pParse->nMem; + }else{ + testcase( pParse->nQueryLoop>(double)1 ); + pParse->nQueryLoop = (double)1; + if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){ + eType = IN_INDEX_ROWID; + } + } + sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID); + pParse->nQueryLoop = savedNQueryLoop; + }else{ + pX->iTable = iTab; + } + return eType; +} +#endif + +/* +** Generate code for scalar subqueries used as a subquery expression, EXISTS, +** or IN operators. Examples: +** +** (SELECT a FROM b) -- subquery +** EXISTS (SELECT a FROM b) -- EXISTS subquery +** x IN (4,5,11) -- IN operator with list on right-hand side +** x IN (SELECT a FROM b) -- IN operator with subquery on the right +** +** The pExpr parameter describes the expression that contains the IN +** operator or subquery. +** +** If parameter isRowid is non-zero, then expression pExpr is guaranteed +** to be of the form " IN (?, ?, ?)", where is a reference +** to some integer key column of a table B-Tree. In this case, use an +** intkey B-Tree to store the set of IN(...) values instead of the usual +** (slower) variable length keys B-Tree. +** +** If rMayHaveNull is non-zero, that means that the operation is an IN +** (not a SELECT or EXISTS) and that the RHS might contains NULLs. +** Furthermore, the IN is in a WHERE clause and that we really want +** to iterate over the RHS of the IN operator in order to quickly locate +** all corresponding LHS elements. All this routine does is initialize +** the register given by rMayHaveNull to NULL. Calling routines will take +** care of changing this register value to non-NULL if the RHS is NULL-free. +** +** If rMayHaveNull is zero, that means that the subquery is being used +** for membership testing only. There is no need to initialize any +** registers to indicate the presense or absence of NULLs on the RHS. +** +** For a SELECT or EXISTS operator, return the register that holds the +** result. For IN operators or if an error occurs, the return value is 0. +*/ +#ifndef SQLITE_OMIT_SUBQUERY +SQLITE_PRIVATE int sqlite3CodeSubselect( + Parse *pParse, /* Parsing context */ + Expr *pExpr, /* The IN, SELECT, or EXISTS operator */ + int rMayHaveNull, /* Register that records whether NULLs exist in RHS */ + int isRowid /* If true, LHS of IN operator is a rowid */ +){ + int testAddr = 0; /* One-time test address */ + int rReg = 0; /* Register storing resulting */ + Vdbe *v = sqlite3GetVdbe(pParse); + if( NEVER(v==0) ) return 0; + sqlite3ExprCachePush(pParse); + + /* This code must be run in its entirety every time it is encountered + ** if any of the following is true: + ** + ** * The right-hand side is a correlated subquery + ** * The right-hand side is an expression list containing variables + ** * We are inside a trigger + ** + ** If all of the above are false, then we can run this code just once + ** save the results, and reuse the same result on subsequent invocations. + */ + if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->pTriggerTab ){ + int mem = ++pParse->nMem; + sqlite3VdbeAddOp1(v, OP_If, mem); + testAddr = sqlite3VdbeAddOp2(v, OP_Integer, 1, mem); + assert( testAddr>0 || pParse->db->mallocFailed ); + } + +#ifndef SQLITE_OMIT_EXPLAIN + if( pParse->explain==2 ){ + char *zMsg = sqlite3MPrintf( + pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr?"":"CORRELATED ", + pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId + ); + sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); + } +#endif + + switch( pExpr->op ){ + case TK_IN: { + char affinity; /* Affinity of the LHS of the IN */ + KeyInfo keyInfo; /* Keyinfo for the generated table */ + int addr; /* Address of OP_OpenEphemeral instruction */ + Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */ + + if( rMayHaveNull ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull); + } + + affinity = sqlite3ExprAffinity(pLeft); + + /* Whether this is an 'x IN(SELECT...)' or an 'x IN()' + ** expression it is handled the same way. An ephemeral table is + ** filled with single-field index keys representing the results + ** from the SELECT or the . + ** + ** If the 'x' expression is a column value, or the SELECT... + ** statement returns a column value, then the affinity of that + ** column is used to build the index keys. If both 'x' and the + ** SELECT... statement are columns, then numeric affinity is used + ** if either column has NUMERIC or INTEGER affinity. If neither + ** 'x' nor the SELECT... statement are columns, then numeric affinity + ** is used. + */ + pExpr->iTable = pParse->nTab++; + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid); + if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED); + memset(&keyInfo, 0, sizeof(keyInfo)); + keyInfo.nField = 1; + + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + /* Case 1: expr IN (SELECT ...) + ** + ** Generate code to write the results of the select into the temporary + ** table allocated and opened above. + */ + SelectDest dest; + ExprList *pEList; + + assert( !isRowid ); + sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable); + dest.affinity = (u8)affinity; + assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable ); + pExpr->x.pSelect->iLimit = 0; + if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){ + return 0; + } + pEList = pExpr->x.pSelect->pEList; + if( ALWAYS(pEList!=0 && pEList->nExpr>0) ){ + keyInfo.aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft, + pEList->a[0].pExpr); + } + }else if( ALWAYS(pExpr->x.pList!=0) ){ + /* Case 2: expr IN (exprlist) + ** + ** For each expression, build an index key from the evaluation and + ** store it in the temporary table. If is a column, then use + ** that columns affinity when building index keys. If is not + ** a column, use numeric affinity. + */ + int i; + ExprList *pList = pExpr->x.pList; + struct ExprList_item *pItem; + int r1, r2, r3; + + if( !affinity ){ + affinity = SQLITE_AFF_NONE; + } + keyInfo.aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft); + + /* Loop through each expression in . */ + r1 = sqlite3GetTempReg(pParse); + r2 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_Null, 0, r2); + for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){ + Expr *pE2 = pItem->pExpr; + int iValToIns; + + /* If the expression is not constant then we will need to + ** disable the test that was generated above that makes sure + ** this code only executes once. Because for a non-constant + ** expression we need to rerun this code each time. + */ + if( testAddr && !sqlite3ExprIsConstant(pE2) ){ + sqlite3VdbeChangeToNoop(v, testAddr-1, 2); + testAddr = 0; + } + + /* Evaluate the expression and insert it into the temp table */ + if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){ + sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns); + }else{ + r3 = sqlite3ExprCodeTarget(pParse, pE2, r1); + if( isRowid ){ + sqlite3VdbeAddOp2(v, OP_MustBeInt, r3, + sqlite3VdbeCurrentAddr(v)+2); + sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3); + }else{ + sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1); + sqlite3ExprCacheAffinityChange(pParse, r3, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2); + } + } + } + sqlite3ReleaseTempReg(pParse, r1); + sqlite3ReleaseTempReg(pParse, r2); + } + if( !isRowid ){ + sqlite3VdbeChangeP4(v, addr, (void *)&keyInfo, P4_KEYINFO); + } + break; + } + + case TK_EXISTS: + case TK_SELECT: + default: { + /* If this has to be a scalar SELECT. Generate code to put the + ** value of this select in a memory cell and record the number + ** of the memory cell in iColumn. If this is an EXISTS, write + ** an integer 0 (not exists) or 1 (exists) into a memory cell + ** and record that memory cell in iColumn. + */ + Select *pSel; /* SELECT statement to encode */ + SelectDest dest; /* How to deal with SELECt result */ + + testcase( pExpr->op==TK_EXISTS ); + testcase( pExpr->op==TK_SELECT ); + assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT ); + + assert( ExprHasProperty(pExpr, EP_xIsSelect) ); + pSel = pExpr->x.pSelect; + sqlite3SelectDestInit(&dest, 0, ++pParse->nMem); + if( pExpr->op==TK_SELECT ){ + dest.eDest = SRT_Mem; + sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iParm); + VdbeComment((v, "Init subquery result")); + }else{ + dest.eDest = SRT_Exists; + sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iParm); + VdbeComment((v, "Init EXISTS result")); + } + sqlite3ExprDelete(pParse->db, pSel->pLimit); + pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, + &sqlite3IntTokens[1]); + pSel->iLimit = 0; + if( sqlite3Select(pParse, pSel, &dest) ){ + return 0; + } + rReg = dest.iParm; + ExprSetIrreducible(pExpr); + break; + } + } + + if( testAddr ){ + sqlite3VdbeJumpHere(v, testAddr-1); + } + sqlite3ExprCachePop(pParse, 1); + + return rReg; +} +#endif /* SQLITE_OMIT_SUBQUERY */ + +#ifndef SQLITE_OMIT_SUBQUERY +/* +** Generate code for an IN expression. +** +** x IN (SELECT ...) +** x IN (value, value, ...) +** +** The left-hand side (LHS) is a scalar expression. The right-hand side (RHS) +** is an array of zero or more values. The expression is true if the LHS is +** contained within the RHS. The value of the expression is unknown (NULL) +** if the LHS is NULL or if the LHS is not contained within the RHS and the +** RHS contains one or more NULL values. +** +** This routine generates code will jump to destIfFalse if the LHS is not +** contained within the RHS. If due to NULLs we cannot determine if the LHS +** is contained in the RHS then jump to destIfNull. If the LHS is contained +** within the RHS then fall through. +*/ +static void sqlite3ExprCodeIN( + Parse *pParse, /* Parsing and code generating context */ + Expr *pExpr, /* The IN expression */ + int destIfFalse, /* Jump here if LHS is not contained in the RHS */ + int destIfNull /* Jump here if the results are unknown due to NULLs */ +){ + int rRhsHasNull = 0; /* Register that is true if RHS contains NULL values */ + char affinity; /* Comparison affinity to use */ + int eType; /* Type of the RHS */ + int r1; /* Temporary use register */ + Vdbe *v; /* Statement under construction */ + + /* Compute the RHS. After this step, the table with cursor + ** pExpr->iTable will contains the values that make up the RHS. + */ + v = pParse->pVdbe; + assert( v!=0 ); /* OOM detected prior to this routine */ + VdbeNoopComment((v, "begin IN expr")); + eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull); + + /* Figure out the affinity to use to create a key from the results + ** of the expression. affinityStr stores a static string suitable for + ** P4 of OP_MakeRecord. + */ + affinity = comparisonAffinity(pExpr); + + /* Code the LHS, the from " IN (...)". + */ + sqlite3ExprCachePush(pParse); + r1 = sqlite3GetTempReg(pParse); + sqlite3ExprCode(pParse, pExpr->pLeft, r1); + + /* If the LHS is NULL, then the result is either false or NULL depending + ** on whether the RHS is empty or not, respectively. + */ + if( destIfNull==destIfFalse ){ + /* Shortcut for the common case where the false and NULL outcomes are + ** the same. */ + sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull); + }else{ + int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1); + sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse); + sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull); + sqlite3VdbeJumpHere(v, addr1); + } + + if( eType==IN_INDEX_ROWID ){ + /* In this case, the RHS is the ROWID of table b-tree + */ + sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse); + sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1); + }else{ + /* In this case, the RHS is an index b-tree. + */ + sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1); + + /* If the set membership test fails, then the result of the + ** "x IN (...)" expression must be either 0 or NULL. If the set + ** contains no NULL values, then the result is 0. If the set + ** contains one or more NULL values, then the result of the + ** expression is also NULL. + */ + if( rRhsHasNull==0 || destIfFalse==destIfNull ){ + /* This branch runs if it is known at compile time that the RHS + ** cannot contain NULL values. This happens as the result + ** of a "NOT NULL" constraint in the database schema. + ** + ** Also run this branch if NULL is equivalent to FALSE + ** for this particular IN operator. + */ + sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1); + + }else{ + /* In this branch, the RHS of the IN might contain a NULL and + ** the presence of a NULL on the RHS makes a difference in the + ** outcome. + */ + int j1, j2, j3; + + /* First check to see if the LHS is contained in the RHS. If so, + ** then the presence of NULLs in the RHS does not matter, so jump + ** over all of the code that follows. + */ + j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1); + + /* Here we begin generating code that runs if the LHS is not + ** contained within the RHS. Generate additional code that + ** tests the RHS for NULLs. If the RHS contains a NULL then + ** jump to destIfNull. If there are no NULLs in the RHS then + ** jump to destIfFalse. + */ + j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull); + j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1); + sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull); + sqlite3VdbeJumpHere(v, j3); + sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1); + sqlite3VdbeJumpHere(v, j2); + + /* Jump to the appropriate target depending on whether or not + ** the RHS contains a NULL + */ + sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull); + sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse); + + /* The OP_Found at the top of this branch jumps here when true, + ** causing the overall IN expression evaluation to fall through. + */ + sqlite3VdbeJumpHere(v, j1); + } + } + sqlite3ReleaseTempReg(pParse, r1); + sqlite3ExprCachePop(pParse, 1); + VdbeComment((v, "end IN expr")); +} +#endif /* SQLITE_OMIT_SUBQUERY */ + +/* +** Duplicate an 8-byte value +*/ +static char *dup8bytes(Vdbe *v, const char *in){ + char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8); + if( out ){ + memcpy(out, in, 8); + } + return out; +} + +#ifndef SQLITE_OMIT_FLOATING_POINT +/* +** Generate an instruction that will put the floating point +** value described by z[0..n-1] into register iMem. +** +** The z[] string will probably not be zero-terminated. But the +** z[n] character is guaranteed to be something that does not look +** like the continuation of the number. +*/ +static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){ + if( ALWAYS(z!=0) ){ + double value; + char *zV; + sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); + assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */ + if( negateFlag ) value = -value; + zV = dup8bytes(v, (char*)&value); + sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL); + } +} +#endif + + +/* +** Generate an instruction that will put the integer describe by +** text z[0..n-1] into register iMem. +** +** Expr.u.zToken is always UTF8 and zero-terminated. +*/ +static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){ + Vdbe *v = pParse->pVdbe; + if( pExpr->flags & EP_IntValue ){ + int i = pExpr->u.iValue; + assert( i>=0 ); + if( negFlag ) i = -i; + sqlite3VdbeAddOp2(v, OP_Integer, i, iMem); + }else{ + int c; + i64 value; + const char *z = pExpr->u.zToken; + assert( z!=0 ); + c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8); + if( c==0 || (c==2 && negFlag) ){ + char *zV; + if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; } + zV = dup8bytes(v, (char*)&value); + sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64); + }else{ +#ifdef SQLITE_OMIT_FLOATING_POINT + sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z); +#else + codeReal(v, z, negFlag, iMem); +#endif + } + } +} + +/* +** Clear a cache entry. +*/ +static void cacheEntryClear(Parse *pParse, struct yColCache *p){ + if( p->tempReg ){ + if( pParse->nTempRegaTempReg) ){ + pParse->aTempReg[pParse->nTempReg++] = p->iReg; + } + p->tempReg = 0; + } +} + + +/* +** Record in the column cache that a particular column from a +** particular table is stored in a particular register. +*/ +SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){ + int i; + int minLru; + int idxLru; + struct yColCache *p; + + assert( iReg>0 ); /* Register numbers are always positive */ + assert( iCol>=-1 && iCol<32768 ); /* Finite column numbers */ + + /* The SQLITE_ColumnCache flag disables the column cache. This is used + ** for testing only - to verify that SQLite always gets the same answer + ** with and without the column cache. + */ + if( pParse->db->flags & SQLITE_ColumnCache ) return; + + /* First replace any existing entry. + ** + ** Actually, the way the column cache is currently used, we are guaranteed + ** that the object will never already be in cache. Verify this guarantee. + */ +#ifndef NDEBUG + for(i=0, p=pParse->aColCache; iiReg && p->iTable==iTab && p->iColumn==iCol ){ + cacheEntryClear(pParse, p); + p->iLevel = pParse->iCacheLevel; + p->iReg = iReg; + p->lru = pParse->iCacheCnt++; + return; + } +#endif + assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol ); + } +#endif + + /* Find an empty slot and replace it */ + for(i=0, p=pParse->aColCache; iiReg==0 ){ + p->iLevel = pParse->iCacheLevel; + p->iTable = iTab; + p->iColumn = iCol; + p->iReg = iReg; + p->tempReg = 0; + p->lru = pParse->iCacheCnt++; + return; + } + } + + /* Replace the last recently used */ + minLru = 0x7fffffff; + idxLru = -1; + for(i=0, p=pParse->aColCache; ilrulru; + } + } + if( ALWAYS(idxLru>=0) ){ + p = &pParse->aColCache[idxLru]; + p->iLevel = pParse->iCacheLevel; + p->iTable = iTab; + p->iColumn = iCol; + p->iReg = iReg; + p->tempReg = 0; + p->lru = pParse->iCacheCnt++; + return; + } +} + +/* +** Indicate that registers between iReg..iReg+nReg-1 are being overwritten. +** Purge the range of registers from the column cache. +*/ +SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){ + int i; + int iLast = iReg + nReg - 1; + struct yColCache *p; + for(i=0, p=pParse->aColCache; iiReg; + if( r>=iReg && r<=iLast ){ + cacheEntryClear(pParse, p); + p->iReg = 0; + } + } +} + +/* +** Remember the current column cache context. Any new entries added +** added to the column cache after this call are removed when the +** corresponding pop occurs. +*/ +SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){ + pParse->iCacheLevel++; +} + +/* +** Remove from the column cache any entries that were added since the +** the previous N Push operations. In other words, restore the cache +** to the state it was in N Pushes ago. +*/ +SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){ + int i; + struct yColCache *p; + assert( N>0 ); + assert( pParse->iCacheLevel>=N ); + pParse->iCacheLevel -= N; + for(i=0, p=pParse->aColCache; iiReg && p->iLevel>pParse->iCacheLevel ){ + cacheEntryClear(pParse, p); + p->iReg = 0; + } + } +} + +/* +** When a cached column is reused, make sure that its register is +** no longer available as a temp register. ticket #3879: that same +** register might be in the cache in multiple places, so be sure to +** get them all. +*/ +static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){ + int i; + struct yColCache *p; + for(i=0, p=pParse->aColCache; iiReg==iReg ){ + p->tempReg = 0; + } + } +} + +/* +** Generate code to extract the value of the iCol-th column of a table. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable( + Vdbe *v, /* The VDBE under construction */ + Table *pTab, /* The table containing the value */ + int iTabCur, /* The cursor for this table */ + int iCol, /* Index of the column to extract */ + int regOut /* Extract the valud into this register */ +){ + if( iCol<0 || iCol==pTab->iPKey ){ + sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut); + }else{ + int op = IsVirtual(pTab) ? OP_VColumn : OP_Column; + sqlite3VdbeAddOp3(v, op, iTabCur, iCol, regOut); + } + if( iCol>=0 ){ + sqlite3ColumnDefault(v, pTab, iCol, regOut); + } +} + +/* +** Generate code that will extract the iColumn-th column from +** table pTab and store the column value in a register. An effort +** is made to store the column value in register iReg, but this is +** not guaranteed. The location of the column value is returned. +** +** There must be an open cursor to pTab in iTable when this routine +** is called. If iColumn<0 then code is generated that extracts the rowid. +*/ +SQLITE_PRIVATE int sqlite3ExprCodeGetColumn( + Parse *pParse, /* Parsing and code generating context */ + Table *pTab, /* Description of the table we are reading from */ + int iColumn, /* Index of the table column */ + int iTable, /* The cursor pointing to the table */ + int iReg /* Store results here */ +){ + Vdbe *v = pParse->pVdbe; + int i; + struct yColCache *p; + + for(i=0, p=pParse->aColCache; iiReg>0 && p->iTable==iTable && p->iColumn==iColumn ){ + p->lru = pParse->iCacheCnt++; + sqlite3ExprCachePinRegister(pParse, p->iReg); + return p->iReg; + } + } + assert( v!=0 ); + sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg); + sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg); + return iReg; +} + +/* +** Clear all column cache entries. +*/ +SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){ + int i; + struct yColCache *p; + + for(i=0, p=pParse->aColCache; iiReg ){ + cacheEntryClear(pParse, p); + p->iReg = 0; + } + } +} + +/* +** Record the fact that an affinity change has occurred on iCount +** registers starting with iStart. +*/ +SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){ + sqlite3ExprCacheRemove(pParse, iStart, iCount); +} + +/* +** Generate code to move content from registers iFrom...iFrom+nReg-1 +** over to iTo..iTo+nReg-1. Keep the column cache up-to-date. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){ + int i; + struct yColCache *p; + if( NEVER(iFrom==iTo) ) return; + sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg); + for(i=0, p=pParse->aColCache; iiReg; + if( x>=iFrom && xiReg += iTo-iFrom; + } + } +} + +/* +** Generate code to copy content from registers iFrom...iFrom+nReg-1 +** over to iTo..iTo+nReg-1. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeCopy(Parse *pParse, int iFrom, int iTo, int nReg){ + int i; + if( NEVER(iFrom==iTo) ) return; + for(i=0; ipVdbe, OP_Copy, iFrom+i, iTo+i); + } +} + +#if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST) +/* +** Return true if any register in the range iFrom..iTo (inclusive) +** is used as part of the column cache. +** +** This routine is used within assert() and testcase() macros only +** and does not appear in a normal build. +*/ +static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){ + int i; + struct yColCache *p; + for(i=0, p=pParse->aColCache; iiReg; + if( r>=iFrom && r<=iTo ) return 1; /*NO_TEST*/ + } + return 0; +} +#endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */ + +/* +** Generate code into the current Vdbe to evaluate the given +** expression. Attempt to store the results in register "target". +** Return the register where results are stored. +** +** With this routine, there is no guarantee that results will +** be stored in target. The result might be stored in some other +** register if it is convenient to do so. The calling function +** must check the return code and move the results to the desired +** register. +*/ +SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){ + Vdbe *v = pParse->pVdbe; /* The VM under construction */ + int op; /* The opcode being coded */ + int inReg = target; /* Results stored in register inReg */ + int regFree1 = 0; /* If non-zero free this temporary register */ + int regFree2 = 0; /* If non-zero free this temporary register */ + int r1, r2, r3, r4; /* Various register numbers */ + sqlite3 *db = pParse->db; /* The database connection */ + + assert( target>0 && target<=pParse->nMem ); + if( v==0 ){ + assert( pParse->db->mallocFailed ); + return 0; + } + + if( pExpr==0 ){ + op = TK_NULL; + }else{ + op = pExpr->op; + } + switch( op ){ + case TK_AGG_COLUMN: { + AggInfo *pAggInfo = pExpr->pAggInfo; + struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg]; + if( !pAggInfo->directMode ){ + assert( pCol->iMem>0 ); + inReg = pCol->iMem; + break; + }else if( pAggInfo->useSortingIdx ){ + sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx, + pCol->iSorterColumn, target); + break; + } + /* Otherwise, fall thru into the TK_COLUMN case */ + } + case TK_COLUMN: { + if( pExpr->iTable<0 ){ + /* This only happens when coding check constraints */ + assert( pParse->ckBase>0 ); + inReg = pExpr->iColumn + pParse->ckBase; + }else{ + inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab, + pExpr->iColumn, pExpr->iTable, target); + } + break; + } + case TK_INTEGER: { + codeInteger(pParse, pExpr, 0, target); + break; + } +#ifndef SQLITE_OMIT_FLOATING_POINT + case TK_FLOAT: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + codeReal(v, pExpr->u.zToken, 0, target); + break; + } +#endif + case TK_STRING: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0); + break; + } + case TK_NULL: { + sqlite3VdbeAddOp2(v, OP_Null, 0, target); + break; + } +#ifndef SQLITE_OMIT_BLOB_LITERAL + case TK_BLOB: { + int n; + const char *z; + char *zBlob; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' ); + assert( pExpr->u.zToken[1]=='\'' ); + z = &pExpr->u.zToken[2]; + n = sqlite3Strlen30(z) - 1; + assert( z[n]=='\'' ); + zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n); + sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC); + break; + } +#endif + case TK_VARIABLE: { + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + assert( pExpr->u.zToken!=0 ); + assert( pExpr->u.zToken[0]!=0 ); + sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target); + if( pExpr->u.zToken[1]!=0 ){ + sqlite3VdbeChangeP4(v, -1, pExpr->u.zToken, 0); + } + break; + } + case TK_REGISTER: { + inReg = pExpr->iTable; + break; + } + case TK_AS: { + inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); + break; + } +#ifndef SQLITE_OMIT_CAST + case TK_CAST: { + /* Expressions of the form: CAST(pLeft AS token) */ + int aff, to_op; + inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + aff = sqlite3AffinityType(pExpr->u.zToken); + to_op = aff - SQLITE_AFF_TEXT + OP_ToText; + assert( to_op==OP_ToText || aff!=SQLITE_AFF_TEXT ); + assert( to_op==OP_ToBlob || aff!=SQLITE_AFF_NONE ); + assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC ); + assert( to_op==OP_ToInt || aff!=SQLITE_AFF_INTEGER ); + assert( to_op==OP_ToReal || aff!=SQLITE_AFF_REAL ); + testcase( to_op==OP_ToText ); + testcase( to_op==OP_ToBlob ); + testcase( to_op==OP_ToNumeric ); + testcase( to_op==OP_ToInt ); + testcase( to_op==OP_ToReal ); + if( inReg!=target ){ + sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target); + inReg = target; + } + sqlite3VdbeAddOp1(v, to_op, inReg); + testcase( usedAsColumnCache(pParse, inReg, inReg) ); + sqlite3ExprCacheAffinityChange(pParse, inReg, 1); + break; + } +#endif /* SQLITE_OMIT_CAST */ + case TK_LT: + case TK_LE: + case TK_GT: + case TK_GE: + case TK_NE: + case TK_EQ: { + assert( TK_LT==OP_Lt ); + assert( TK_LE==OP_Le ); + assert( TK_GT==OP_Gt ); + assert( TK_GE==OP_Ge ); + assert( TK_EQ==OP_Eq ); + assert( TK_NE==OP_Ne ); + testcase( op==TK_LT ); + testcase( op==TK_LE ); + testcase( op==TK_GT ); + testcase( op==TK_GE ); + testcase( op==TK_EQ ); + testcase( op==TK_NE ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, inReg, SQLITE_STOREP2); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_IS: + case TK_ISNOT: { + testcase( op==TK_IS ); + testcase( op==TK_ISNOT ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + op = (op==TK_IS) ? TK_EQ : TK_NE; + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_AND: + case TK_OR: + case TK_PLUS: + case TK_STAR: + case TK_MINUS: + case TK_REM: + case TK_BITAND: + case TK_BITOR: + case TK_SLASH: + case TK_LSHIFT: + case TK_RSHIFT: + case TK_CONCAT: { + assert( TK_AND==OP_And ); + assert( TK_OR==OP_Or ); + assert( TK_PLUS==OP_Add ); + assert( TK_MINUS==OP_Subtract ); + assert( TK_REM==OP_Remainder ); + assert( TK_BITAND==OP_BitAnd ); + assert( TK_BITOR==OP_BitOr ); + assert( TK_SLASH==OP_Divide ); + assert( TK_LSHIFT==OP_ShiftLeft ); + assert( TK_RSHIFT==OP_ShiftRight ); + assert( TK_CONCAT==OP_Concat ); + testcase( op==TK_AND ); + testcase( op==TK_OR ); + testcase( op==TK_PLUS ); + testcase( op==TK_MINUS ); + testcase( op==TK_REM ); + testcase( op==TK_BITAND ); + testcase( op==TK_BITOR ); + testcase( op==TK_SLASH ); + testcase( op==TK_LSHIFT ); + testcase( op==TK_RSHIFT ); + testcase( op==TK_CONCAT ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + sqlite3VdbeAddOp3(v, op, r2, r1, target); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_UMINUS: { + Expr *pLeft = pExpr->pLeft; + assert( pLeft ); + if( pLeft->op==TK_INTEGER ){ + codeInteger(pParse, pLeft, 1, target); +#ifndef SQLITE_OMIT_FLOATING_POINT + }else if( pLeft->op==TK_FLOAT ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + codeReal(v, pLeft->u.zToken, 1, target); +#endif + }else{ + regFree1 = r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_Integer, 0, r1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free2); + sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target); + testcase( regFree2==0 ); + } + inReg = target; + break; + } + case TK_BITNOT: + case TK_NOT: { + assert( TK_BITNOT==OP_BitNot ); + assert( TK_NOT==OP_Not ); + testcase( op==TK_BITNOT ); + testcase( op==TK_NOT ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + testcase( regFree1==0 ); + inReg = target; + sqlite3VdbeAddOp2(v, op, r1, inReg); + break; + } + case TK_ISNULL: + case TK_NOTNULL: { + int addr; + assert( TK_ISNULL==OP_IsNull ); + assert( TK_NOTNULL==OP_NotNull ); + testcase( op==TK_ISNULL ); + testcase( op==TK_NOTNULL ); + sqlite3VdbeAddOp2(v, OP_Integer, 1, target); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + testcase( regFree1==0 ); + addr = sqlite3VdbeAddOp1(v, op, r1); + sqlite3VdbeAddOp2(v, OP_AddImm, target, -1); + sqlite3VdbeJumpHere(v, addr); + break; + } + case TK_AGG_FUNCTION: { + AggInfo *pInfo = pExpr->pAggInfo; + if( pInfo==0 ){ + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken); + }else{ + inReg = pInfo->aFunc[pExpr->iAgg].iMem; + } + break; + } + case TK_CONST_FUNC: + case TK_FUNCTION: { + ExprList *pFarg; /* List of function arguments */ + int nFarg; /* Number of function arguments */ + FuncDef *pDef; /* The function definition object */ + int nId; /* Length of the function name in bytes */ + const char *zId; /* The function name */ + int constMask = 0; /* Mask of function arguments that are constant */ + int i; /* Loop counter */ + u8 enc = ENC(db); /* The text encoding used by this database */ + CollSeq *pColl = 0; /* A collating sequence */ + + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + testcase( op==TK_CONST_FUNC ); + testcase( op==TK_FUNCTION ); + if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){ + pFarg = 0; + }else{ + pFarg = pExpr->x.pList; + } + nFarg = pFarg ? pFarg->nExpr : 0; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + zId = pExpr->u.zToken; + nId = sqlite3Strlen30(zId); + pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0); + if( pDef==0 ){ + sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId); + break; + } + + /* Attempt a direct implementation of the built-in COALESCE() and + ** IFNULL() functions. This avoids unnecessary evalation of + ** arguments past the first non-NULL argument. + */ + if( pDef->flags & SQLITE_FUNC_COALESCE ){ + int endCoalesce = sqlite3VdbeMakeLabel(v); + assert( nFarg>=2 ); + sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target); + for(i=1; ia[i].pExpr, target); + sqlite3ExprCachePop(pParse, 1); + } + sqlite3VdbeResolveLabel(v, endCoalesce); + break; + } + + + if( pFarg ){ + r1 = sqlite3GetTempRange(pParse, nFarg); + sqlite3ExprCachePush(pParse); /* Ticket 2ea2425d34be */ + sqlite3ExprCodeExprList(pParse, pFarg, r1, 1); + sqlite3ExprCachePop(pParse, 1); /* Ticket 2ea2425d34be */ + }else{ + r1 = 0; + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + /* Possibly overload the function if the first argument is + ** a virtual table column. + ** + ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the + ** second argument, not the first, as the argument to test to + ** see if it is a column in a virtual table. This is done because + ** the left operand of infix functions (the operand we want to + ** control overloading) ends up as the second argument to the + ** function. The expression "A glob B" is equivalent to + ** "glob(B,A). We want to use the A in "A glob B" to test + ** for function overloading. But we use the B term in "glob(B,A)". + */ + if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){ + pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr); + }else if( nFarg>0 ){ + pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr); + } +#endif + for(i=0; ia[i].pExpr) ){ + constMask |= (1<flags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ + pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); + } + } + if( pDef->flags & SQLITE_FUNC_NEEDCOLL ){ + if( !pColl ) pColl = db->pDfltColl; + sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); + } + sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target, + (char*)pDef, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, (u8)nFarg); + if( nFarg ){ + sqlite3ReleaseTempRange(pParse, r1, nFarg); + } + break; + } +#ifndef SQLITE_OMIT_SUBQUERY + case TK_EXISTS: + case TK_SELECT: { + testcase( op==TK_EXISTS ); + testcase( op==TK_SELECT ); + inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0); + break; + } + case TK_IN: { + int destIfFalse = sqlite3VdbeMakeLabel(v); + int destIfNull = sqlite3VdbeMakeLabel(v); + sqlite3VdbeAddOp2(v, OP_Null, 0, target); + sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); + sqlite3VdbeAddOp2(v, OP_Integer, 1, target); + sqlite3VdbeResolveLabel(v, destIfFalse); + sqlite3VdbeAddOp2(v, OP_AddImm, target, 0); + sqlite3VdbeResolveLabel(v, destIfNull); + break; + } +#endif /* SQLITE_OMIT_SUBQUERY */ + + + /* + ** x BETWEEN y AND z + ** + ** This is equivalent to + ** + ** x>=y AND x<=z + ** + ** X is stored in pExpr->pLeft. + ** Y is stored in pExpr->pList->a[0].pExpr. + ** Z is stored in pExpr->pList->a[1].pExpr. + */ + case TK_BETWEEN: { + Expr *pLeft = pExpr->pLeft; + struct ExprList_item *pLItem = pExpr->x.pList->a; + Expr *pRight = pLItem->pExpr; + + r1 = sqlite3ExprCodeTemp(pParse, pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + r3 = sqlite3GetTempReg(pParse); + r4 = sqlite3GetTempReg(pParse); + codeCompare(pParse, pLeft, pRight, OP_Ge, + r1, r2, r3, SQLITE_STOREP2); + pLItem++; + pRight = pLItem->pExpr; + sqlite3ReleaseTempReg(pParse, regFree2); + r2 = sqlite3ExprCodeTemp(pParse, pRight, ®Free2); + testcase( regFree2==0 ); + codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2); + sqlite3VdbeAddOp3(v, OP_And, r3, r4, target); + sqlite3ReleaseTempReg(pParse, r3); + sqlite3ReleaseTempReg(pParse, r4); + break; + } + case TK_UPLUS: { + inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target); + break; + } + + case TK_TRIGGER: { + /* If the opcode is TK_TRIGGER, then the expression is a reference + ** to a column in the new.* or old.* pseudo-tables available to + ** trigger programs. In this case Expr.iTable is set to 1 for the + ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn + ** is set to the column of the pseudo-table to read, or to -1 to + ** read the rowid field. + ** + ** The expression is implemented using an OP_Param opcode. The p1 + ** parameter is set to 0 for an old.rowid reference, or to (i+1) + ** to reference another column of the old.* pseudo-table, where + ** i is the index of the column. For a new.rowid reference, p1 is + ** set to (n+1), where n is the number of columns in each pseudo-table. + ** For a reference to any other column in the new.* pseudo-table, p1 + ** is set to (n+2+i), where n and i are as defined previously. For + ** example, if the table on which triggers are being fired is + ** declared as: + ** + ** CREATE TABLE t1(a, b); + ** + ** Then p1 is interpreted as follows: + ** + ** p1==0 -> old.rowid p1==3 -> new.rowid + ** p1==1 -> old.a p1==4 -> new.a + ** p1==2 -> old.b p1==5 -> new.b + */ + Table *pTab = pExpr->pTab; + int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn; + + assert( pExpr->iTable==0 || pExpr->iTable==1 ); + assert( pExpr->iColumn>=-1 && pExpr->iColumnnCol ); + assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey ); + assert( p1>=0 && p1<(pTab->nCol*2+2) ); + + sqlite3VdbeAddOp2(v, OP_Param, p1, target); + VdbeComment((v, "%s.%s -> $%d", + (pExpr->iTable ? "new" : "old"), + (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName), + target + )); + +#ifndef SQLITE_OMIT_FLOATING_POINT + /* If the column has REAL affinity, it may currently be stored as an + ** integer. Use OP_RealAffinity to make sure it is really real. */ + if( pExpr->iColumn>=0 + && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL + ){ + sqlite3VdbeAddOp1(v, OP_RealAffinity, target); + } +#endif + break; + } + + + /* + ** Form A: + ** CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END + ** + ** Form B: + ** CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END + ** + ** Form A is can be transformed into the equivalent form B as follows: + ** CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ... + ** WHEN x=eN THEN rN ELSE y END + ** + ** X (if it exists) is in pExpr->pLeft. + ** Y is in pExpr->pRight. The Y is also optional. If there is no + ** ELSE clause and no other term matches, then the result of the + ** exprssion is NULL. + ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1]. + ** + ** The result of the expression is the Ri for the first matching Ei, + ** or if there is no matching Ei, the ELSE term Y, or if there is + ** no ELSE term, NULL. + */ + default: assert( op==TK_CASE ); { + int endLabel; /* GOTO label for end of CASE stmt */ + int nextCase; /* GOTO label for next WHEN clause */ + int nExpr; /* 2x number of WHEN terms */ + int i; /* Loop counter */ + ExprList *pEList; /* List of WHEN terms */ + struct ExprList_item *aListelem; /* Array of WHEN terms */ + Expr opCompare; /* The X==Ei expression */ + Expr cacheX; /* Cached expression X */ + Expr *pX; /* The X expression */ + Expr *pTest = 0; /* X==Ei (form A) or just Ei (form B) */ + VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; ) + + assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList ); + assert((pExpr->x.pList->nExpr % 2) == 0); + assert(pExpr->x.pList->nExpr > 0); + pEList = pExpr->x.pList; + aListelem = pEList->a; + nExpr = pEList->nExpr; + endLabel = sqlite3VdbeMakeLabel(v); + if( (pX = pExpr->pLeft)!=0 ){ + cacheX = *pX; + testcase( pX->op==TK_COLUMN ); + testcase( pX->op==TK_REGISTER ); + cacheX.iTable = sqlite3ExprCodeTemp(pParse, pX, ®Free1); + testcase( regFree1==0 ); + cacheX.op = TK_REGISTER; + opCompare.op = TK_EQ; + opCompare.pLeft = &cacheX; + pTest = &opCompare; + /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001: + ** The value in regFree1 might get SCopy-ed into the file result. + ** So make sure that the regFree1 register is not reused for other + ** purposes and possibly overwritten. */ + regFree1 = 0; + } + for(i=0; iop==TK_COLUMN ); + sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL); + testcase( aListelem[i+1].pExpr->op==TK_COLUMN ); + testcase( aListelem[i+1].pExpr->op==TK_REGISTER ); + sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target); + sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel); + sqlite3ExprCachePop(pParse, 1); + sqlite3VdbeResolveLabel(v, nextCase); + } + if( pExpr->pRight ){ + sqlite3ExprCachePush(pParse); + sqlite3ExprCode(pParse, pExpr->pRight, target); + sqlite3ExprCachePop(pParse, 1); + }else{ + sqlite3VdbeAddOp2(v, OP_Null, 0, target); + } + assert( db->mallocFailed || pParse->nErr>0 + || pParse->iCacheLevel==iCacheLevel ); + sqlite3VdbeResolveLabel(v, endLabel); + break; + } +#ifndef SQLITE_OMIT_TRIGGER + case TK_RAISE: { + assert( pExpr->affinity==OE_Rollback + || pExpr->affinity==OE_Abort + || pExpr->affinity==OE_Fail + || pExpr->affinity==OE_Ignore + ); + if( !pParse->pTriggerTab ){ + sqlite3ErrorMsg(pParse, + "RAISE() may only be used within a trigger-program"); + return 0; + } + if( pExpr->affinity==OE_Abort ){ + sqlite3MayAbort(pParse); + } + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + if( pExpr->affinity==OE_Ignore ){ + sqlite3VdbeAddOp4( + v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0); + }else{ + sqlite3HaltConstraint(pParse, pExpr->affinity, pExpr->u.zToken, 0); + } + + break; + } +#endif + } + sqlite3ReleaseTempReg(pParse, regFree1); + sqlite3ReleaseTempReg(pParse, regFree2); + return inReg; +} + +/* +** Generate code to evaluate an expression and store the results +** into a register. Return the register number where the results +** are stored. +** +** If the register is a temporary register that can be deallocated, +** then write its number into *pReg. If the result register is not +** a temporary, then set *pReg to zero. +*/ +SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){ + int r1 = sqlite3GetTempReg(pParse); + int r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); + if( r2==r1 ){ + *pReg = r1; + }else{ + sqlite3ReleaseTempReg(pParse, r1); + *pReg = 0; + } + return r2; +} + +/* +** Generate code that will evaluate expression pExpr and store the +** results in register target. The results are guaranteed to appear +** in register target. +*/ +SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){ + int inReg; + + assert( target>0 && target<=pParse->nMem ); + if( pExpr && pExpr->op==TK_REGISTER ){ + sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target); + }else{ + inReg = sqlite3ExprCodeTarget(pParse, pExpr, target); + assert( pParse->pVdbe || pParse->db->mallocFailed ); + if( inReg!=target && pParse->pVdbe ){ + sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target); + } + } + return target; +} + +/* +** Generate code that evalutes the given expression and puts the result +** in register target. +** +** Also make a copy of the expression results into another "cache" register +** and modify the expression so that the next time it is evaluated, +** the result is a copy of the cache register. +** +** This routine is used for expressions that are used multiple +** times. They are evaluated once and the results of the expression +** are reused. +*/ +SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){ + Vdbe *v = pParse->pVdbe; + int inReg; + inReg = sqlite3ExprCode(pParse, pExpr, target); + assert( target>0 ); + /* This routine is called for terms to INSERT or UPDATE. And the only + ** other place where expressions can be converted into TK_REGISTER is + ** in WHERE clause processing. So as currently implemented, there is + ** no way for a TK_REGISTER to exist here. But it seems prudent to + ** keep the ALWAYS() in case the conditions above change with future + ** modifications or enhancements. */ + if( ALWAYS(pExpr->op!=TK_REGISTER) ){ + int iMem; + iMem = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem); + pExpr->iTable = iMem; + pExpr->op2 = pExpr->op; + pExpr->op = TK_REGISTER; + } + return inReg; +} + +/* +** Return TRUE if pExpr is an constant expression that is appropriate +** for factoring out of a loop. Appropriate expressions are: +** +** * Any expression that evaluates to two or more opcodes. +** +** * Any OP_Integer, OP_Real, OP_String, OP_Blob, OP_Null, +** or OP_Variable that does not need to be placed in a +** specific register. +** +** There is no point in factoring out single-instruction constant +** expressions that need to be placed in a particular register. +** We could factor them out, but then we would end up adding an +** OP_SCopy instruction to move the value into the correct register +** later. We might as well just use the original instruction and +** avoid the OP_SCopy. +*/ +static int isAppropriateForFactoring(Expr *p){ + if( !sqlite3ExprIsConstantNotJoin(p) ){ + return 0; /* Only constant expressions are appropriate for factoring */ + } + if( (p->flags & EP_FixedDest)==0 ){ + return 1; /* Any constant without a fixed destination is appropriate */ + } + while( p->op==TK_UPLUS ) p = p->pLeft; + switch( p->op ){ +#ifndef SQLITE_OMIT_BLOB_LITERAL + case TK_BLOB: +#endif + case TK_VARIABLE: + case TK_INTEGER: + case TK_FLOAT: + case TK_NULL: + case TK_STRING: { + testcase( p->op==TK_BLOB ); + testcase( p->op==TK_VARIABLE ); + testcase( p->op==TK_INTEGER ); + testcase( p->op==TK_FLOAT ); + testcase( p->op==TK_NULL ); + testcase( p->op==TK_STRING ); + /* Single-instruction constants with a fixed destination are + ** better done in-line. If we factor them, they will just end + ** up generating an OP_SCopy to move the value to the destination + ** register. */ + return 0; + } + case TK_UMINUS: { + if( p->pLeft->op==TK_FLOAT || p->pLeft->op==TK_INTEGER ){ + return 0; + } + break; + } + default: { + break; + } + } + return 1; +} + +/* +** If pExpr is a constant expression that is appropriate for +** factoring out of a loop, then evaluate the expression +** into a register and convert the expression into a TK_REGISTER +** expression. +*/ +static int evalConstExpr(Walker *pWalker, Expr *pExpr){ + Parse *pParse = pWalker->pParse; + switch( pExpr->op ){ + case TK_IN: + case TK_REGISTER: { + return WRC_Prune; + } + case TK_FUNCTION: + case TK_AGG_FUNCTION: + case TK_CONST_FUNC: { + /* The arguments to a function have a fixed destination. + ** Mark them this way to avoid generated unneeded OP_SCopy + ** instructions. + */ + ExprList *pList = pExpr->x.pList; + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + if( pList ){ + int i = pList->nExpr; + struct ExprList_item *pItem = pList->a; + for(; i>0; i--, pItem++){ + if( ALWAYS(pItem->pExpr) ) pItem->pExpr->flags |= EP_FixedDest; + } + } + break; + } + } + if( isAppropriateForFactoring(pExpr) ){ + int r1 = ++pParse->nMem; + int r2; + r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1); + if( NEVER(r1!=r2) ) sqlite3ReleaseTempReg(pParse, r1); + pExpr->op2 = pExpr->op; + pExpr->op = TK_REGISTER; + pExpr->iTable = r2; + return WRC_Prune; + } + return WRC_Continue; +} + +/* +** Preevaluate constant subexpressions within pExpr and store the +** results in registers. Modify pExpr so that the constant subexpresions +** are TK_REGISTER opcodes that refer to the precomputed values. +** +** This routine is a no-op if the jump to the cookie-check code has +** already occur. Since the cookie-check jump is generated prior to +** any other serious processing, this check ensures that there is no +** way to accidently bypass the constant initializations. +** +** This routine is also a no-op if the SQLITE_FactorOutConst optimization +** is disabled via the sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS) +** interface. This allows test logic to verify that the same answer is +** obtained for queries regardless of whether or not constants are +** precomputed into registers or if they are inserted in-line. +*/ +SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){ + Walker w; + if( pParse->cookieGoto ) return; + if( (pParse->db->flags & SQLITE_FactorOutConst)!=0 ) return; + w.xExprCallback = evalConstExpr; + w.xSelectCallback = 0; + w.pParse = pParse; + sqlite3WalkExpr(&w, pExpr); +} + + +/* +** Generate code that pushes the value of every element of the given +** expression list into a sequence of registers beginning at target. +** +** Return the number of elements evaluated. +*/ +SQLITE_PRIVATE int sqlite3ExprCodeExprList( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* The expression list to be coded */ + int target, /* Where to write results */ + int doHardCopy /* Make a hard copy of every element */ +){ + struct ExprList_item *pItem; + int i, n; + assert( pList!=0 ); + assert( target>0 ); + assert( pParse->pVdbe!=0 ); /* Never gets this far otherwise */ + n = pList->nExpr; + for(pItem=pList->a, i=0; ipExpr; + int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i); + if( inReg!=target+i ){ + sqlite3VdbeAddOp2(pParse->pVdbe, doHardCopy ? OP_Copy : OP_SCopy, + inReg, target+i); + } + } + return n; +} + +/* +** Generate code for a BETWEEN operator. +** +** x BETWEEN y AND z +** +** The above is equivalent to +** +** x>=y AND x<=z +** +** Code it as such, taking care to do the common subexpression +** elementation of x. +*/ +static void exprCodeBetween( + Parse *pParse, /* Parsing and code generating context */ + Expr *pExpr, /* The BETWEEN expression */ + int dest, /* Jump here if the jump is taken */ + int jumpIfTrue, /* Take the jump if the BETWEEN is true */ + int jumpIfNull /* Take the jump if the BETWEEN is NULL */ +){ + Expr exprAnd; /* The AND operator in x>=y AND x<=z */ + Expr compLeft; /* The x>=y term */ + Expr compRight; /* The x<=z term */ + Expr exprX; /* The x subexpression */ + int regFree1 = 0; /* Temporary use register */ + + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + exprX = *pExpr->pLeft; + exprAnd.op = TK_AND; + exprAnd.pLeft = &compLeft; + exprAnd.pRight = &compRight; + compLeft.op = TK_GE; + compLeft.pLeft = &exprX; + compLeft.pRight = pExpr->x.pList->a[0].pExpr; + compRight.op = TK_LE; + compRight.pLeft = &exprX; + compRight.pRight = pExpr->x.pList->a[1].pExpr; + exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, ®Free1); + exprX.op = TK_REGISTER; + if( jumpIfTrue ){ + sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull); + }else{ + sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull); + } + sqlite3ReleaseTempReg(pParse, regFree1); + + /* Ensure adequate test coverage */ + testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 ); + testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 ); + testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 ); + testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 ); + testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 ); + testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 ); + testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 ); + testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 ); +} + +/* +** Generate code for a boolean expression such that a jump is made +** to the label "dest" if the expression is true but execution +** continues straight thru if the expression is false. +** +** If the expression evaluates to NULL (neither true nor false), then +** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL. +** +** This code depends on the fact that certain token values (ex: TK_EQ) +** are the same as opcode values (ex: OP_Eq) that implement the corresponding +** operation. Special comments in vdbe.c and the mkopcodeh.awk script in +** the make process cause these values to align. Assert()s in the code +** below verify that the numbers are aligned correctly. +*/ +SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ + Vdbe *v = pParse->pVdbe; + int op = 0; + int regFree1 = 0; + int regFree2 = 0; + int r1, r2; + + assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); + if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */ + if( NEVER(pExpr==0) ) return; /* No way this can happen */ + op = pExpr->op; + switch( op ){ + case TK_AND: { + int d2 = sqlite3VdbeMakeLabel(v); + testcase( jumpIfNull==0 ); + sqlite3ExprCachePush(pParse); + sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL); + sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); + sqlite3VdbeResolveLabel(v, d2); + sqlite3ExprCachePop(pParse, 1); + break; + } + case TK_OR: { + testcase( jumpIfNull==0 ); + sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); + sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull); + break; + } + case TK_NOT: { + testcase( jumpIfNull==0 ); + sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); + break; + } + case TK_LT: + case TK_LE: + case TK_GT: + case TK_GE: + case TK_NE: + case TK_EQ: { + assert( TK_LT==OP_Lt ); + assert( TK_LE==OP_Le ); + assert( TK_GT==OP_Gt ); + assert( TK_GE==OP_Ge ); + assert( TK_EQ==OP_Eq ); + assert( TK_NE==OP_Ne ); + testcase( op==TK_LT ); + testcase( op==TK_LE ); + testcase( op==TK_GT ); + testcase( op==TK_GE ); + testcase( op==TK_EQ ); + testcase( op==TK_NE ); + testcase( jumpIfNull==0 ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, dest, jumpIfNull); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_IS: + case TK_ISNOT: { + testcase( op==TK_IS ); + testcase( op==TK_ISNOT ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + op = (op==TK_IS) ? TK_EQ : TK_NE; + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, dest, SQLITE_NULLEQ); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_ISNULL: + case TK_NOTNULL: { + assert( TK_ISNULL==OP_IsNull ); + assert( TK_NOTNULL==OP_NotNull ); + testcase( op==TK_ISNULL ); + testcase( op==TK_NOTNULL ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + sqlite3VdbeAddOp2(v, op, r1, dest); + testcase( regFree1==0 ); + break; + } + case TK_BETWEEN: { + testcase( jumpIfNull==0 ); + exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull); + break; + } +#ifndef SQLITE_OMIT_SUBQUERY + case TK_IN: { + int destIfFalse = sqlite3VdbeMakeLabel(v); + int destIfNull = jumpIfNull ? dest : destIfFalse; + sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull); + sqlite3VdbeAddOp2(v, OP_Goto, 0, dest); + sqlite3VdbeResolveLabel(v, destIfFalse); + break; + } +#endif + default: { + r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); + sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0); + testcase( regFree1==0 ); + testcase( jumpIfNull==0 ); + break; + } + } + sqlite3ReleaseTempReg(pParse, regFree1); + sqlite3ReleaseTempReg(pParse, regFree2); +} + +/* +** Generate code for a boolean expression such that a jump is made +** to the label "dest" if the expression is false but execution +** continues straight thru if the expression is true. +** +** If the expression evaluates to NULL (neither true nor false) then +** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull +** is 0. +*/ +SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){ + Vdbe *v = pParse->pVdbe; + int op = 0; + int regFree1 = 0; + int regFree2 = 0; + int r1, r2; + + assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 ); + if( NEVER(v==0) ) return; /* Existance of VDBE checked by caller */ + if( pExpr==0 ) return; + + /* The value of pExpr->op and op are related as follows: + ** + ** pExpr->op op + ** --------- ---------- + ** TK_ISNULL OP_NotNull + ** TK_NOTNULL OP_IsNull + ** TK_NE OP_Eq + ** TK_EQ OP_Ne + ** TK_GT OP_Le + ** TK_LE OP_Gt + ** TK_GE OP_Lt + ** TK_LT OP_Ge + ** + ** For other values of pExpr->op, op is undefined and unused. + ** The value of TK_ and OP_ constants are arranged such that we + ** can compute the mapping above using the following expression. + ** Assert()s verify that the computation is correct. + */ + op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1); + + /* Verify correct alignment of TK_ and OP_ constants + */ + assert( pExpr->op!=TK_ISNULL || op==OP_NotNull ); + assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull ); + assert( pExpr->op!=TK_NE || op==OP_Eq ); + assert( pExpr->op!=TK_EQ || op==OP_Ne ); + assert( pExpr->op!=TK_LT || op==OP_Ge ); + assert( pExpr->op!=TK_LE || op==OP_Gt ); + assert( pExpr->op!=TK_GT || op==OP_Le ); + assert( pExpr->op!=TK_GE || op==OP_Lt ); + + switch( pExpr->op ){ + case TK_AND: { + testcase( jumpIfNull==0 ); + sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull); + sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); + break; + } + case TK_OR: { + int d2 = sqlite3VdbeMakeLabel(v); + testcase( jumpIfNull==0 ); + sqlite3ExprCachePush(pParse); + sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL); + sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull); + sqlite3VdbeResolveLabel(v, d2); + sqlite3ExprCachePop(pParse, 1); + break; + } + case TK_NOT: { + testcase( jumpIfNull==0 ); + sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull); + break; + } + case TK_LT: + case TK_LE: + case TK_GT: + case TK_GE: + case TK_NE: + case TK_EQ: { + testcase( op==TK_LT ); + testcase( op==TK_LE ); + testcase( op==TK_GT ); + testcase( op==TK_GE ); + testcase( op==TK_EQ ); + testcase( op==TK_NE ); + testcase( jumpIfNull==0 ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, dest, jumpIfNull); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_IS: + case TK_ISNOT: { + testcase( pExpr->op==TK_IS ); + testcase( pExpr->op==TK_ISNOT ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, ®Free2); + op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ; + codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op, + r1, r2, dest, SQLITE_NULLEQ); + testcase( regFree1==0 ); + testcase( regFree2==0 ); + break; + } + case TK_ISNULL: + case TK_NOTNULL: { + testcase( op==TK_ISNULL ); + testcase( op==TK_NOTNULL ); + r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, ®Free1); + sqlite3VdbeAddOp2(v, op, r1, dest); + testcase( regFree1==0 ); + break; + } + case TK_BETWEEN: { + testcase( jumpIfNull==0 ); + exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull); + break; + } +#ifndef SQLITE_OMIT_SUBQUERY + case TK_IN: { + if( jumpIfNull ){ + sqlite3ExprCodeIN(pParse, pExpr, dest, dest); + }else{ + int destIfNull = sqlite3VdbeMakeLabel(v); + sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull); + sqlite3VdbeResolveLabel(v, destIfNull); + } + break; + } +#endif + default: { + r1 = sqlite3ExprCodeTemp(pParse, pExpr, ®Free1); + sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0); + testcase( regFree1==0 ); + testcase( jumpIfNull==0 ); + break; + } + } + sqlite3ReleaseTempReg(pParse, regFree1); + sqlite3ReleaseTempReg(pParse, regFree2); +} + +/* +** Do a deep comparison of two expression trees. Return 0 if the two +** expressions are completely identical. Return 1 if they differ only +** by a COLLATE operator at the top level. Return 2 if there are differences +** other than the top-level COLLATE operator. +** +** Sometimes this routine will return 2 even if the two expressions +** really are equivalent. If we cannot prove that the expressions are +** identical, we return 2 just to be safe. So if this routine +** returns 2, then you do not really know for certain if the two +** expressions are the same. But if you get a 0 or 1 return, then you +** can be sure the expressions are the same. In the places where +** this routine is used, it does not hurt to get an extra 2 - that +** just might result in some slightly slower code. But returning +** an incorrect 0 or 1 could lead to a malfunction. +*/ +SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB){ + if( pA==0||pB==0 ){ + return pB==pA ? 0 : 2; + } + assert( !ExprHasAnyProperty(pA, EP_TokenOnly|EP_Reduced) ); + assert( !ExprHasAnyProperty(pB, EP_TokenOnly|EP_Reduced) ); + if( ExprHasProperty(pA, EP_xIsSelect) || ExprHasProperty(pB, EP_xIsSelect) ){ + return 2; + } + if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2; + if( pA->op!=pB->op ) return 2; + if( sqlite3ExprCompare(pA->pLeft, pB->pLeft) ) return 2; + if( sqlite3ExprCompare(pA->pRight, pB->pRight) ) return 2; + if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList) ) return 2; + if( pA->iTable!=pB->iTable || pA->iColumn!=pB->iColumn ) return 2; + if( ExprHasProperty(pA, EP_IntValue) ){ + if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){ + return 2; + } + }else if( pA->op!=TK_COLUMN && pA->u.zToken ){ + if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2; + if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){ + return 2; + } + } + if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1; + if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2; + return 0; +} + +/* +** Compare two ExprList objects. Return 0 if they are identical and +** non-zero if they differ in any way. +** +** This routine might return non-zero for equivalent ExprLists. The +** only consequence will be disabled optimizations. But this routine +** must never return 0 if the two ExprList objects are different, or +** a malfunction will result. +** +** Two NULL pointers are considered to be the same. But a NULL pointer +** always differs from a non-NULL pointer. +*/ +SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB){ + int i; + if( pA==0 && pB==0 ) return 0; + if( pA==0 || pB==0 ) return 1; + if( pA->nExpr!=pB->nExpr ) return 1; + for(i=0; inExpr; i++){ + Expr *pExprA = pA->a[i].pExpr; + Expr *pExprB = pB->a[i].pExpr; + if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1; + if( sqlite3ExprCompare(pExprA, pExprB) ) return 1; + } + return 0; +} + +/* +** Add a new element to the pAggInfo->aCol[] array. Return the index of +** the new element. Return a negative number if malloc fails. +*/ +static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){ + int i; + pInfo->aCol = sqlite3ArrayAllocate( + db, + pInfo->aCol, + sizeof(pInfo->aCol[0]), + 3, + &pInfo->nColumn, + &pInfo->nColumnAlloc, + &i + ); + return i; +} + +/* +** Add a new element to the pAggInfo->aFunc[] array. Return the index of +** the new element. Return a negative number if malloc fails. +*/ +static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){ + int i; + pInfo->aFunc = sqlite3ArrayAllocate( + db, + pInfo->aFunc, + sizeof(pInfo->aFunc[0]), + 3, + &pInfo->nFunc, + &pInfo->nFuncAlloc, + &i + ); + return i; +} + +/* +** This is the xExprCallback for a tree walker. It is used to +** implement sqlite3ExprAnalyzeAggregates(). See sqlite3ExprAnalyzeAggregates +** for additional information. +*/ +static int analyzeAggregate(Walker *pWalker, Expr *pExpr){ + int i; + NameContext *pNC = pWalker->u.pNC; + Parse *pParse = pNC->pParse; + SrcList *pSrcList = pNC->pSrcList; + AggInfo *pAggInfo = pNC->pAggInfo; + + switch( pExpr->op ){ + case TK_AGG_COLUMN: + case TK_COLUMN: { + testcase( pExpr->op==TK_AGG_COLUMN ); + testcase( pExpr->op==TK_COLUMN ); + /* Check to see if the column is in one of the tables in the FROM + ** clause of the aggregate query */ + if( ALWAYS(pSrcList!=0) ){ + struct SrcList_item *pItem = pSrcList->a; + for(i=0; inSrc; i++, pItem++){ + struct AggInfo_col *pCol; + assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) ); + if( pExpr->iTable==pItem->iCursor ){ + /* If we reach this point, it means that pExpr refers to a table + ** that is in the FROM clause of the aggregate query. + ** + ** Make an entry for the column in pAggInfo->aCol[] if there + ** is not an entry there already. + */ + int k; + pCol = pAggInfo->aCol; + for(k=0; knColumn; k++, pCol++){ + if( pCol->iTable==pExpr->iTable && + pCol->iColumn==pExpr->iColumn ){ + break; + } + } + if( (k>=pAggInfo->nColumn) + && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0 + ){ + pCol = &pAggInfo->aCol[k]; + pCol->pTab = pExpr->pTab; + pCol->iTable = pExpr->iTable; + pCol->iColumn = pExpr->iColumn; + pCol->iMem = ++pParse->nMem; + pCol->iSorterColumn = -1; + pCol->pExpr = pExpr; + if( pAggInfo->pGroupBy ){ + int j, n; + ExprList *pGB = pAggInfo->pGroupBy; + struct ExprList_item *pTerm = pGB->a; + n = pGB->nExpr; + for(j=0; jpExpr; + if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable && + pE->iColumn==pExpr->iColumn ){ + pCol->iSorterColumn = j; + break; + } + } + } + if( pCol->iSorterColumn<0 ){ + pCol->iSorterColumn = pAggInfo->nSortingColumn++; + } + } + /* There is now an entry for pExpr in pAggInfo->aCol[] (either + ** because it was there before or because we just created it). + ** Convert the pExpr to be a TK_AGG_COLUMN referring to that + ** pAggInfo->aCol[] entry. + */ + ExprSetIrreducible(pExpr); + pExpr->pAggInfo = pAggInfo; + pExpr->op = TK_AGG_COLUMN; + pExpr->iAgg = (i16)k; + break; + } /* endif pExpr->iTable==pItem->iCursor */ + } /* end loop over pSrcList */ + } + return WRC_Prune; + } + case TK_AGG_FUNCTION: { + /* The pNC->nDepth==0 test causes aggregate functions in subqueries + ** to be ignored */ + if( pNC->nDepth==0 ){ + /* Check to see if pExpr is a duplicate of another aggregate + ** function that is already in the pAggInfo structure + */ + struct AggInfo_func *pItem = pAggInfo->aFunc; + for(i=0; inFunc; i++, pItem++){ + if( sqlite3ExprCompare(pItem->pExpr, pExpr)==0 ){ + break; + } + } + if( i>=pAggInfo->nFunc ){ + /* pExpr is original. Make a new entry in pAggInfo->aFunc[] + */ + u8 enc = ENC(pParse->db); + i = addAggInfoFunc(pParse->db, pAggInfo); + if( i>=0 ){ + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + pItem = &pAggInfo->aFunc[i]; + pItem->pExpr = pExpr; + pItem->iMem = ++pParse->nMem; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + pItem->pFunc = sqlite3FindFunction(pParse->db, + pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken), + pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0); + if( pExpr->flags & EP_Distinct ){ + pItem->iDistinct = pParse->nTab++; + }else{ + pItem->iDistinct = -1; + } + } + } + /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry + */ + assert( !ExprHasAnyProperty(pExpr, EP_TokenOnly|EP_Reduced) ); + ExprSetIrreducible(pExpr); + pExpr->iAgg = (i16)i; + pExpr->pAggInfo = pAggInfo; + return WRC_Prune; + } + } + } + return WRC_Continue; +} +static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){ + NameContext *pNC = pWalker->u.pNC; + if( pNC->nDepth==0 ){ + pNC->nDepth++; + sqlite3WalkSelect(pWalker, pSelect); + pNC->nDepth--; + return WRC_Prune; + }else{ + return WRC_Continue; + } +} + +/* +** Analyze the given expression looking for aggregate functions and +** for variables that need to be added to the pParse->aAgg[] array. +** Make additional entries to the pParse->aAgg[] array as necessary. +** +** This routine should only be called after the expression has been +** analyzed by sqlite3ResolveExprNames(). +*/ +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){ + Walker w; + w.xExprCallback = analyzeAggregate; + w.xSelectCallback = analyzeAggregatesInSelect; + w.u.pNC = pNC; + assert( pNC->pSrcList!=0 ); + sqlite3WalkExpr(&w, pExpr); +} + +/* +** Call sqlite3ExprAnalyzeAggregates() for every expression in an +** expression list. Return the number of errors. +** +** If an error is found, the analysis is cut short. +*/ +SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){ + struct ExprList_item *pItem; + int i; + if( pList ){ + for(pItem=pList->a, i=0; inExpr; i++, pItem++){ + sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr); + } + } +} + +/* +** Allocate a single new register for use to hold some intermediate result. +*/ +SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){ + if( pParse->nTempReg==0 ){ + return ++pParse->nMem; + } + return pParse->aTempReg[--pParse->nTempReg]; +} + +/* +** Deallocate a register, making available for reuse for some other +** purpose. +** +** If a register is currently being used by the column cache, then +** the dallocation is deferred until the column cache line that uses +** the register becomes stale. +*/ +SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){ + if( iReg && pParse->nTempRegaTempReg) ){ + int i; + struct yColCache *p; + for(i=0, p=pParse->aColCache; iiReg==iReg ){ + p->tempReg = 1; + return; + } + } + pParse->aTempReg[pParse->nTempReg++] = iReg; + } +} + +/* +** Allocate or deallocate a block of nReg consecutive registers +*/ +SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){ + int i, n; + i = pParse->iRangeReg; + n = pParse->nRangeReg; + if( nReg<=n ){ + assert( !usedAsColumnCache(pParse, i, i+n-1) ); + pParse->iRangeReg += nReg; + pParse->nRangeReg -= nReg; + }else{ + i = pParse->nMem+1; + pParse->nMem += nReg; + } + return i; +} +SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){ + sqlite3ExprCacheRemove(pParse, iReg, nReg); + if( nReg>pParse->nRangeReg ){ + pParse->nRangeReg = nReg; + pParse->iRangeReg = iReg; + } +} + +/************** End of expr.c ************************************************/ +/************** Begin file alter.c *******************************************/ +/* +** 2005 February 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that used to generate VDBE code +** that implements the ALTER TABLE command. +*/ + +/* +** The code in this file only exists if we are not omitting the +** ALTER TABLE logic from the build. +*/ +#ifndef SQLITE_OMIT_ALTERTABLE + + +/* +** This function is used by SQL generated to implement the +** ALTER TABLE command. The first argument is the text of a CREATE TABLE or +** CREATE INDEX command. The second is a table name. The table name in +** the CREATE TABLE or CREATE INDEX statement is replaced with the third +** argument and the result returned. Examples: +** +** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def') +** -> 'CREATE TABLE def(a, b, c)' +** +** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def') +** -> 'CREATE INDEX i ON def(a, b, c)' +*/ +static void renameTableFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + unsigned char const *zSql = sqlite3_value_text(argv[0]); + unsigned char const *zTableName = sqlite3_value_text(argv[1]); + + int token; + Token tname; + unsigned char const *zCsr = zSql; + int len = 0; + char *zRet; + + sqlite3 *db = sqlite3_context_db_handle(context); + + UNUSED_PARAMETER(NotUsed); + + /* The principle used to locate the table name in the CREATE TABLE + ** statement is that the table name is the first non-space token that + ** is immediately followed by a TK_LP or TK_USING token. + */ + if( zSql ){ + do { + if( !*zCsr ){ + /* Ran out of input before finding an opening bracket. Return NULL. */ + return; + } + + /* Store the token that zCsr points to in tname. */ + tname.z = (char*)zCsr; + tname.n = len; + + /* Advance zCsr to the next token. Store that token type in 'token', + ** and its length in 'len' (to be used next iteration of this loop). + */ + do { + zCsr += len; + len = sqlite3GetToken(zCsr, &token); + } while( token==TK_SPACE ); + assert( len>0 ); + } while( token!=TK_LP && token!=TK_USING ); + + zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, + zTableName, tname.z+tname.n); + sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); + } +} + +/* +** This C function implements an SQL user function that is used by SQL code +** generated by the ALTER TABLE ... RENAME command to modify the definition +** of any foreign key constraints that use the table being renamed as the +** parent table. It is passed three arguments: +** +** 1) The complete text of the CREATE TABLE statement being modified, +** 2) The old name of the table being renamed, and +** 3) The new name of the table being renamed. +** +** It returns the new CREATE TABLE statement. For example: +** +** sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3') +** -> 'CREATE TABLE t1(a REFERENCES t3)' +*/ +#ifndef SQLITE_OMIT_FOREIGN_KEY +static void renameParentFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + sqlite3 *db = sqlite3_context_db_handle(context); + char *zOutput = 0; + char *zResult; + unsigned char const *zInput = sqlite3_value_text(argv[0]); + unsigned char const *zOld = sqlite3_value_text(argv[1]); + unsigned char const *zNew = sqlite3_value_text(argv[2]); + + unsigned const char *z; /* Pointer to token */ + int n; /* Length of token z */ + int token; /* Type of token */ + + UNUSED_PARAMETER(NotUsed); + for(z=zInput; *z; z=z+n){ + n = sqlite3GetToken(z, &token); + if( token==TK_REFERENCES ){ + char *zParent; + do { + z += n; + n = sqlite3GetToken(z, &token); + }while( token==TK_SPACE ); + + zParent = sqlite3DbStrNDup(db, (const char *)z, n); + if( zParent==0 ) break; + sqlite3Dequote(zParent); + if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){ + char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"", + (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew + ); + sqlite3DbFree(db, zOutput); + zOutput = zOut; + zInput = &z[n]; + } + sqlite3DbFree(db, zParent); + } + } + + zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput), + sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC); + sqlite3DbFree(db, zOutput); +} +#endif + +#ifndef SQLITE_OMIT_TRIGGER +/* This function is used by SQL generated to implement the +** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER +** statement. The second is a table name. The table name in the CREATE +** TRIGGER statement is replaced with the third argument and the result +** returned. This is analagous to renameTableFunc() above, except for CREATE +** TRIGGER, not CREATE INDEX and CREATE TABLE. +*/ +static void renameTriggerFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + unsigned char const *zSql = sqlite3_value_text(argv[0]); + unsigned char const *zTableName = sqlite3_value_text(argv[1]); + + int token; + Token tname; + int dist = 3; + unsigned char const *zCsr = zSql; + int len = 0; + char *zRet; + sqlite3 *db = sqlite3_context_db_handle(context); + + UNUSED_PARAMETER(NotUsed); + + /* The principle used to locate the table name in the CREATE TRIGGER + ** statement is that the table name is the first token that is immediatedly + ** preceded by either TK_ON or TK_DOT and immediatedly followed by one + ** of TK_WHEN, TK_BEGIN or TK_FOR. + */ + if( zSql ){ + do { + + if( !*zCsr ){ + /* Ran out of input before finding the table name. Return NULL. */ + return; + } + + /* Store the token that zCsr points to in tname. */ + tname.z = (char*)zCsr; + tname.n = len; + + /* Advance zCsr to the next token. Store that token type in 'token', + ** and its length in 'len' (to be used next iteration of this loop). + */ + do { + zCsr += len; + len = sqlite3GetToken(zCsr, &token); + }while( token==TK_SPACE ); + assert( len>0 ); + + /* Variable 'dist' stores the number of tokens read since the most + ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN + ** token is read and 'dist' equals 2, the condition stated above + ** to be met. + ** + ** Note that ON cannot be a database, table or column name, so + ** there is no need to worry about syntax like + ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc. + */ + dist++; + if( token==TK_DOT || token==TK_ON ){ + dist = 0; + } + } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) ); + + /* Variable tname now contains the token that is the old table-name + ** in the CREATE TRIGGER statement. + */ + zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql, + zTableName, tname.z+tname.n); + sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC); + } +} +#endif /* !SQLITE_OMIT_TRIGGER */ + +/* +** Register built-in functions used to help implement ALTER TABLE +*/ +SQLITE_PRIVATE void sqlite3AlterFunctions(void){ + static SQLITE_WSD FuncDef aAlterTableFuncs[] = { + FUNCTION(sqlite_rename_table, 2, 0, 0, renameTableFunc), +#ifndef SQLITE_OMIT_TRIGGER + FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc), +#endif +#ifndef SQLITE_OMIT_FOREIGN_KEY + FUNCTION(sqlite_rename_parent, 3, 0, 0, renameParentFunc), +#endif + }; + int i; + FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); + FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs); + + for(i=0; i OR name= OR ... +** +** If argument zWhere is NULL, then a pointer string containing the text +** "name=" is returned, where is the quoted version +** of the string passed as argument zConstant. The returned buffer is +** allocated using sqlite3DbMalloc(). It is the responsibility of the +** caller to ensure that it is eventually freed. +** +** If argument zWhere is not NULL, then the string returned is +** " OR name=", where is the contents of zWhere. +** In this case zWhere is passed to sqlite3DbFree() before returning. +** +*/ +static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){ + char *zNew; + if( !zWhere ){ + zNew = sqlite3MPrintf(db, "name=%Q", zConstant); + }else{ + zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant); + sqlite3DbFree(db, zWhere); + } + return zNew; +} + +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) +/* +** Generate the text of a WHERE expression which can be used to select all +** tables that have foreign key constraints that refer to table pTab (i.e. +** constraints for which pTab is the parent table) from the sqlite_master +** table. +*/ +static char *whereForeignKeys(Parse *pParse, Table *pTab){ + FKey *p; + char *zWhere = 0; + for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ + zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName); + } + return zWhere; +} +#endif + +/* +** Generate the text of a WHERE expression which can be used to select all +** temporary triggers on table pTab from the sqlite_temp_master table. If +** table pTab has no temporary triggers, or is itself stored in the +** temporary database, NULL is returned. +*/ +static char *whereTempTriggers(Parse *pParse, Table *pTab){ + Trigger *pTrig; + char *zWhere = 0; + const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */ + + /* If the table is not located in the temp-db (in which case NULL is + ** returned, loop through the tables list of triggers. For each trigger + ** that is not part of the temp-db schema, add a clause to the WHERE + ** expression being built up in zWhere. + */ + if( pTab->pSchema!=pTempSchema ){ + sqlite3 *db = pParse->db; + for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ + if( pTrig->pSchema==pTempSchema ){ + zWhere = whereOrName(db, zWhere, pTrig->zName); + } + } + } + if( zWhere ){ + char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere); + sqlite3DbFree(pParse->db, zWhere); + zWhere = zNew; + } + return zWhere; +} + +/* +** Generate code to drop and reload the internal representation of table +** pTab from the database, including triggers and temporary triggers. +** Argument zName is the name of the table in the database schema at +** the time the generated code is executed. This can be different from +** pTab->zName if this function is being called to code part of an +** "ALTER TABLE RENAME TO" statement. +*/ +static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){ + Vdbe *v; + char *zWhere; + int iDb; /* Index of database containing pTab */ +#ifndef SQLITE_OMIT_TRIGGER + Trigger *pTrig; +#endif + + v = sqlite3GetVdbe(pParse); + if( NEVER(v==0) ) return; + assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); + iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + assert( iDb>=0 ); + +#ifndef SQLITE_OMIT_TRIGGER + /* Drop any table triggers from the internal schema. */ + for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){ + int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); + assert( iTrigDb==iDb || iTrigDb==1 ); + sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0); + } +#endif + + /* Drop the table and index from the internal schema. */ + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); + + /* Reload the table, index and permanent trigger schemas. */ + zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName); + if( !zWhere ) return; + sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, zWhere, P4_DYNAMIC); + +#ifndef SQLITE_OMIT_TRIGGER + /* Now, if the table is not stored in the temp database, reload any temp + ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined. + */ + if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ + sqlite3VdbeAddOp4(v, OP_ParseSchema, 1, 0, 0, zWhere, P4_DYNAMIC); + } +#endif +} + +/* +** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy" +** command. +*/ +SQLITE_PRIVATE void sqlite3AlterRenameTable( + Parse *pParse, /* Parser context. */ + SrcList *pSrc, /* The table to rename. */ + Token *pName /* The new table name. */ +){ + int iDb; /* Database that contains the table */ + char *zDb; /* Name of database iDb */ + Table *pTab; /* Table being renamed */ + char *zName = 0; /* NULL-terminated version of pName */ + sqlite3 *db = pParse->db; /* Database connection */ + int nTabName; /* Number of UTF-8 characters in zTabName */ + const char *zTabName; /* Original name of the table */ + Vdbe *v; +#ifndef SQLITE_OMIT_TRIGGER + char *zWhere = 0; /* Where clause to locate temp triggers */ +#endif + VTable *pVTab = 0; /* Non-zero if this is a v-tab with an xRename() */ + int savedDbFlags; /* Saved value of db->flags */ + + savedDbFlags = db->flags; + if( NEVER(db->mallocFailed) ) goto exit_rename_table; + assert( pSrc->nSrc==1 ); + assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); + + pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase); + if( !pTab ) goto exit_rename_table; + iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + zDb = db->aDb[iDb].zName; + db->flags |= SQLITE_PreferBuiltin; + + /* Get a NULL terminated version of the new table name. */ + zName = sqlite3NameFromToken(db, pName); + if( !zName ) goto exit_rename_table; + + /* Check that a table or index named 'zName' does not already exist + ** in database iDb. If so, this is an error. + */ + if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){ + sqlite3ErrorMsg(pParse, + "there is already another table or index with this name: %s", zName); + goto exit_rename_table; + } + + /* Make sure it is not a system table being altered, or a reserved name + ** that the table is being renamed to. + */ + if( sqlite3Strlen30(pTab->zName)>6 + && 0==sqlite3StrNICmp(pTab->zName, "sqlite_", 7) + ){ + sqlite3ErrorMsg(pParse, "table %s may not be altered", pTab->zName); + goto exit_rename_table; + } + if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ + goto exit_rename_table; + } + +#ifndef SQLITE_OMIT_VIEW + if( pTab->pSelect ){ + sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName); + goto exit_rename_table; + } +#endif + +#ifndef SQLITE_OMIT_AUTHORIZATION + /* Invoke the authorization callback. */ + if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ + goto exit_rename_table; + } +#endif + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( sqlite3ViewGetColumnNames(pParse, pTab) ){ + goto exit_rename_table; + } + if( IsVirtual(pTab) ){ + pVTab = sqlite3GetVTable(db, pTab); + if( pVTab->pVtab->pModule->xRename==0 ){ + pVTab = 0; + } + } +#endif + + /* Begin a transaction and code the VerifyCookie for database iDb. + ** Then modify the schema cookie (since the ALTER TABLE modifies the + ** schema). Open a statement transaction if the table is a virtual + ** table. + */ + v = sqlite3GetVdbe(pParse); + if( v==0 ){ + goto exit_rename_table; + } + sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb); + sqlite3ChangeCookie(pParse, iDb); + + /* If this is a virtual table, invoke the xRename() function if + ** one is defined. The xRename() callback will modify the names + ** of any resources used by the v-table implementation (including other + ** SQLite tables) that are identified by the name of the virtual table. + */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( pVTab ){ + int i = ++pParse->nMem; + sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0); + sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB); + sqlite3MayAbort(pParse); + } +#endif + + /* figure out how many UTF-8 characters are in zName */ + zTabName = pTab->zName; + nTabName = sqlite3Utf8CharLen(zTabName, -1); + +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) + if( db->flags&SQLITE_ForeignKeys ){ + /* If foreign-key support is enabled, rewrite the CREATE TABLE + ** statements corresponding to all child tables of foreign key constraints + ** for which the renamed table is the parent table. */ + if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){ + sqlite3NestedParse(pParse, + "UPDATE \"%w\".%s SET " + "sql = sqlite_rename_parent(sql, %Q, %Q) " + "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere); + sqlite3DbFree(db, zWhere); + } + } +#endif + + /* Modify the sqlite_master table to use the new table name. */ + sqlite3NestedParse(pParse, + "UPDATE %Q.%s SET " +#ifdef SQLITE_OMIT_TRIGGER + "sql = sqlite_rename_table(sql, %Q), " +#else + "sql = CASE " + "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)" + "ELSE sqlite_rename_table(sql, %Q) END, " +#endif + "tbl_name = %Q, " + "name = CASE " + "WHEN type='table' THEN %Q " + "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN " + "'sqlite_autoindex_' || %Q || substr(name,%d+18) " + "ELSE name END " + "WHERE tbl_name=%Q AND " + "(type='table' OR type='index' OR type='trigger');", + zDb, SCHEMA_TABLE(iDb), zName, zName, zName, +#ifndef SQLITE_OMIT_TRIGGER + zName, +#endif + zName, nTabName, zTabName + ); + +#ifndef SQLITE_OMIT_AUTOINCREMENT + /* If the sqlite_sequence table exists in this database, then update + ** it with the new table name. + */ + if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){ + sqlite3NestedParse(pParse, + "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q", + zDb, zName, pTab->zName); + } +#endif + +#ifndef SQLITE_OMIT_TRIGGER + /* If there are TEMP triggers on this table, modify the sqlite_temp_master + ** table. Don't do this if the table being ALTERed is itself located in + ** the temp database. + */ + if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){ + sqlite3NestedParse(pParse, + "UPDATE sqlite_temp_master SET " + "sql = sqlite_rename_trigger(sql, %Q), " + "tbl_name = %Q " + "WHERE %s;", zName, zName, zWhere); + sqlite3DbFree(db, zWhere); + } +#endif + +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) + if( db->flags&SQLITE_ForeignKeys ){ + FKey *p; + for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ + Table *pFrom = p->pFrom; + if( pFrom!=pTab ){ + reloadTableSchema(pParse, p->pFrom, pFrom->zName); + } + } + } +#endif + + /* Drop and reload the internal table schema. */ + reloadTableSchema(pParse, pTab, zName); + +exit_rename_table: + sqlite3SrcListDelete(db, pSrc); + sqlite3DbFree(db, zName); + db->flags = savedDbFlags; +} + + +/* +** Generate code to make sure the file format number is at least minFormat. +** The generated code will increase the file format number if necessary. +*/ +SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){ + Vdbe *v; + v = sqlite3GetVdbe(pParse); + /* The VDBE should have been allocated before this routine is called. + ** If that allocation failed, we would have quit before reaching this + ** point */ + if( ALWAYS(v) ){ + int r1 = sqlite3GetTempReg(pParse); + int r2 = sqlite3GetTempReg(pParse); + int j1; + sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT); + sqlite3VdbeUsesBtree(v, iDb); + sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2); + j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2); + sqlite3VdbeJumpHere(v, j1); + sqlite3ReleaseTempReg(pParse, r1); + sqlite3ReleaseTempReg(pParse, r2); + } +} + +/* +** This function is called after an "ALTER TABLE ... ADD" statement +** has been parsed. Argument pColDef contains the text of the new +** column definition. +** +** The Table structure pParse->pNewTable was extended to include +** the new column during parsing. +*/ +SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){ + Table *pNew; /* Copy of pParse->pNewTable */ + Table *pTab; /* Table being altered */ + int iDb; /* Database number */ + const char *zDb; /* Database name */ + const char *zTab; /* Table name */ + char *zCol; /* Null-terminated column definition */ + Column *pCol; /* The new column */ + Expr *pDflt; /* Default value for the new column */ + sqlite3 *db; /* The database connection; */ + + db = pParse->db; + if( pParse->nErr || db->mallocFailed ) return; + pNew = pParse->pNewTable; + assert( pNew ); + + assert( sqlite3BtreeHoldsAllMutexes(db) ); + iDb = sqlite3SchemaToIndex(db, pNew->pSchema); + zDb = db->aDb[iDb].zName; + zTab = &pNew->zName[16]; /* Skip the "sqlite_altertab_" prefix on the name */ + pCol = &pNew->aCol[pNew->nCol-1]; + pDflt = pCol->pDflt; + pTab = sqlite3FindTable(db, zTab, zDb); + assert( pTab ); + +#ifndef SQLITE_OMIT_AUTHORIZATION + /* Invoke the authorization callback. */ + if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){ + return; + } +#endif + + /* If the default value for the new column was specified with a + ** literal NULL, then set pDflt to 0. This simplifies checking + ** for an SQL NULL default below. + */ + if( pDflt && pDflt->op==TK_NULL ){ + pDflt = 0; + } + + /* Check that the new column is not specified as PRIMARY KEY or UNIQUE. + ** If there is a NOT NULL constraint, then the default value for the + ** column must not be NULL. + */ + if( pCol->isPrimKey ){ + sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column"); + return; + } + if( pNew->pIndex ){ + sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column"); + return; + } + if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){ + sqlite3ErrorMsg(pParse, + "Cannot add a REFERENCES column with non-NULL default value"); + return; + } + if( pCol->notNull && !pDflt ){ + sqlite3ErrorMsg(pParse, + "Cannot add a NOT NULL column with default value NULL"); + return; + } + + /* Ensure the default expression is something that sqlite3ValueFromExpr() + ** can handle (i.e. not CURRENT_TIME etc.) + */ + if( pDflt ){ + sqlite3_value *pVal; + if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){ + db->mallocFailed = 1; + return; + } + if( !pVal ){ + sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default"); + return; + } + sqlite3ValueFree(pVal); + } + + /* Modify the CREATE TABLE statement. */ + zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n); + if( zCol ){ + char *zEnd = &zCol[pColDef->n-1]; + int savedDbFlags = db->flags; + while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){ + *zEnd-- = '\0'; + } + db->flags |= SQLITE_PreferBuiltin; + sqlite3NestedParse(pParse, + "UPDATE \"%w\".%s SET " + "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) " + "WHERE type = 'table' AND name = %Q", + zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1, + zTab + ); + sqlite3DbFree(db, zCol); + db->flags = savedDbFlags; + } + + /* If the default value of the new column is NULL, then set the file + ** format to 2. If the default value of the new column is not NULL, + ** the file format becomes 3. + */ + sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2); + + /* Reload the schema of the modified table. */ + reloadTableSchema(pParse, pTab, pTab->zName); +} + +/* +** This function is called by the parser after the table-name in +** an "ALTER TABLE ADD" statement is parsed. Argument +** pSrc is the full-name of the table being altered. +** +** This routine makes a (partial) copy of the Table structure +** for the table being altered and sets Parse.pNewTable to point +** to it. Routines called by the parser as the column definition +** is parsed (i.e. sqlite3AddColumn()) add the new Column data to +** the copy. The copy of the Table structure is deleted by tokenize.c +** after parsing is finished. +** +** Routine sqlite3AlterFinishAddColumn() will be called to complete +** coding the "ALTER TABLE ... ADD" statement. +*/ +SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){ + Table *pNew; + Table *pTab; + Vdbe *v; + int iDb; + int i; + int nAlloc; + sqlite3 *db = pParse->db; + + /* Look up the table being altered. */ + assert( pParse->pNewTable==0 ); + assert( sqlite3BtreeHoldsAllMutexes(db) ); + if( db->mallocFailed ) goto exit_begin_add_column; + pTab = sqlite3LocateTable(pParse, 0, pSrc->a[0].zName, pSrc->a[0].zDatabase); + if( !pTab ) goto exit_begin_add_column; + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + sqlite3ErrorMsg(pParse, "virtual tables may not be altered"); + goto exit_begin_add_column; + } +#endif + + /* Make sure this is not an attempt to ALTER a view. */ + if( pTab->pSelect ){ + sqlite3ErrorMsg(pParse, "Cannot add a column to a view"); + goto exit_begin_add_column; + } + + assert( pTab->addColOffset>0 ); + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + + /* Put a copy of the Table struct in Parse.pNewTable for the + ** sqlite3AddColumn() function and friends to modify. But modify + ** the name by adding an "sqlite_altertab_" prefix. By adding this + ** prefix, we insure that the name will not collide with an existing + ** table because user table are not allowed to have the "sqlite_" + ** prefix on their name. + */ + pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table)); + if( !pNew ) goto exit_begin_add_column; + pParse->pNewTable = pNew; + pNew->nRef = 1; + pNew->nCol = pTab->nCol; + assert( pNew->nCol>0 ); + nAlloc = (((pNew->nCol-1)/8)*8)+8; + assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 ); + pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc); + pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName); + if( !pNew->aCol || !pNew->zName ){ + db->mallocFailed = 1; + goto exit_begin_add_column; + } + memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol); + for(i=0; inCol; i++){ + Column *pCol = &pNew->aCol[i]; + pCol->zName = sqlite3DbStrDup(db, pCol->zName); + pCol->zColl = 0; + pCol->zType = 0; + pCol->pDflt = 0; + pCol->zDflt = 0; + } + pNew->pSchema = db->aDb[iDb].pSchema; + pNew->addColOffset = pTab->addColOffset; + pNew->nRef = 1; + + /* Begin a transaction and increment the schema cookie. */ + sqlite3BeginWriteOperation(pParse, 0, iDb); + v = sqlite3GetVdbe(pParse); + if( !v ) goto exit_begin_add_column; + sqlite3ChangeCookie(pParse, iDb); + +exit_begin_add_column: + sqlite3SrcListDelete(db, pSrc); + return; +} +#endif /* SQLITE_ALTER_TABLE */ + +/************** End of alter.c ***********************************************/ +/************** Begin file analyze.c *****************************************/ +/* +** 2005 July 8 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code associated with the ANALYZE command. +*/ +#ifndef SQLITE_OMIT_ANALYZE + +/* +** This routine generates code that opens the sqlite_stat1 table for +** writing with cursor iStatCur. If the library was built with the +** SQLITE_ENABLE_STAT2 macro defined, then the sqlite_stat2 table is +** opened for writing using cursor (iStatCur+1) +** +** If the sqlite_stat1 tables does not previously exist, it is created. +** Similarly, if the sqlite_stat2 table does not exist and the library +** is compiled with SQLITE_ENABLE_STAT2 defined, it is created. +** +** Argument zWhere may be a pointer to a buffer containing a table name, +** or it may be a NULL pointer. If it is not NULL, then all entries in +** the sqlite_stat1 and (if applicable) sqlite_stat2 tables associated +** with the named table are deleted. If zWhere==0, then code is generated +** to delete all stat table entries. +*/ +static void openStatTable( + Parse *pParse, /* Parsing context */ + int iDb, /* The database we are looking in */ + int iStatCur, /* Open the sqlite_stat1 table on this cursor */ + const char *zWhere /* Delete entries associated with this table */ +){ + static const struct { + const char *zName; + const char *zCols; + } aTable[] = { + { "sqlite_stat1", "tbl,idx,stat" }, +#ifdef SQLITE_ENABLE_STAT2 + { "sqlite_stat2", "tbl,idx,sampleno,sample" }, +#endif + }; + + int aRoot[] = {0, 0}; + u8 aCreateTbl[] = {0, 0}; + + int i; + sqlite3 *db = pParse->db; + Db *pDb; + Vdbe *v = sqlite3GetVdbe(pParse); + if( v==0 ) return; + assert( sqlite3BtreeHoldsAllMutexes(db) ); + assert( sqlite3VdbeDb(v)==db ); + pDb = &db->aDb[iDb]; + + for(i=0; izName))==0 ){ + /* The sqlite_stat[12] table does not exist. Create it. Note that a + ** side-effect of the CREATE TABLE statement is to leave the rootpage + ** of the new table in register pParse->regRoot. This is important + ** because the OpenWrite opcode below will be needing it. */ + sqlite3NestedParse(pParse, + "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols + ); + aRoot[i] = pParse->regRoot; + aCreateTbl[i] = 1; + }else{ + /* The table already exists. If zWhere is not NULL, delete all entries + ** associated with the table zWhere. If zWhere is NULL, delete the + ** entire contents of the table. */ + aRoot[i] = pStat->tnum; + sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab); + if( zWhere ){ + sqlite3NestedParse(pParse, + "DELETE FROM %Q.%s WHERE tbl=%Q", pDb->zName, zTab, zWhere + ); + }else{ + /* The sqlite_stat[12] table already exists. Delete all rows. */ + sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb); + } + } + } + + /* Open the sqlite_stat[12] tables for writing. */ + for(i=0; idb; /* Database handle */ + Index *pIdx; /* An index to being analyzed */ + int iIdxCur; /* Cursor open on index being analyzed */ + Vdbe *v; /* The virtual machine being built up */ + int i; /* Loop counter */ + int topOfLoop; /* The top of the loop */ + int endOfLoop; /* The end of the loop */ + int addr = 0; /* The address of an instruction */ + int jZeroRows = 0; /* Jump from here if number of rows is zero */ + int iDb; /* Index of database containing pTab */ + int regTabname = iMem++; /* Register containing table name */ + int regIdxname = iMem++; /* Register containing index name */ + int regSampleno = iMem++; /* Register containing next sample number */ + int regCol = iMem++; /* Content of a column analyzed table */ + int regRec = iMem++; /* Register holding completed record */ + int regTemp = iMem++; /* Temporary use register */ + int regRowid = iMem++; /* Rowid for the inserted record */ + +#ifdef SQLITE_ENABLE_STAT2 + int regTemp2 = iMem++; /* Temporary use register */ + int regSamplerecno = iMem++; /* Index of next sample to record */ + int regRecno = iMem++; /* Current sample index */ + int regLast = iMem++; /* Index of last sample to record */ + int regFirst = iMem++; /* Index of first sample to record */ +#endif + + v = sqlite3GetVdbe(pParse); + if( v==0 || NEVER(pTab==0) ){ + return; + } + if( pTab->tnum==0 ){ + /* Do not gather statistics on views or virtual tables */ + return; + } + if( memcmp(pTab->zName, "sqlite_", 7)==0 ){ + /* Do not gather statistics on system tables */ + return; + } + assert( sqlite3BtreeHoldsAllMutexes(db) ); + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iDb>=0 ); +#ifndef SQLITE_OMIT_AUTHORIZATION + if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0, + db->aDb[iDb].zName ) ){ + return; + } +#endif + + /* Establish a read-lock on the table at the shared-cache level. */ + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); + + iIdxCur = pParse->nTab++; + sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0); + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + int nCol = pIdx->nColumn; + KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); + + if( iMem+1+(nCol*2)>pParse->nMem ){ + pParse->nMem = iMem+1+(nCol*2); + } + + /* Open a cursor to the index to be analyzed. */ + assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) ); + sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb, + (char *)pKey, P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pIdx->zName)); + + /* Populate the register containing the index name. */ + sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0); + +#ifdef SQLITE_ENABLE_STAT2 + + /* If this iteration of the loop is generating code to analyze the + ** first index in the pTab->pIndex list, then register regLast has + ** not been populated. In this case populate it now. */ + if( pTab->pIndex==pIdx ){ + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno); + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp); + sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2); + + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast); + sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst); + addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast); + sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst); + sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast); + sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2); + sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast); + sqlite3VdbeJumpHere(v, addr); + } + + /* Zero the regSampleno and regRecno registers. */ + sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno); + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno); + sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno); +#endif + + /* The block of memory cells initialized here is used as follows. + ** + ** iMem: + ** The total number of rows in the table. + ** + ** iMem+1 .. iMem+nCol: + ** Number of distinct entries in index considering the + ** left-most N columns only, where N is between 1 and nCol, + ** inclusive. + ** + ** iMem+nCol+1 .. Mem+2*nCol: + ** Previous value of indexed columns, from left to right. + ** + ** Cells iMem through iMem+nCol are initialized to 0. The others are + ** initialized to contain an SQL NULL. + */ + for(i=0; i<=nCol; i++){ + sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i); + } + for(i=0; iazColl!=0 ); + assert( pIdx->azColl[i]!=0 ); + pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]); + sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1, + (char*)pColl, P4_COLLSEQ); + sqlite3VdbeChangeP5(v, SQLITE_NULLEQ); + } + if( db->mallocFailed ){ + /* If a malloc failure has occurred, then the result of the expression + ** passed as the second argument to the call to sqlite3VdbeJumpHere() + ** below may be negative. Which causes an assert() to fail (or an + ** out-of-bounds write if SQLITE_DEBUG is not defined). */ + return; + } + sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop); + for(i=0; i0 then it is always the case the D>0 so division by zero + ** is never possible. + */ + sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno); + if( jZeroRows==0 ){ + jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem); + } + for(i=0; ipIndex==0 ){ + sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb); + VdbeComment((v, "%s", pTab->zName)); + sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno); + sqlite3VdbeAddOp1(v, OP_Close, iIdxCur); + }else{ + assert( jZeroRows>0 ); + addr = sqlite3VdbeAddOp0(v, OP_Goto); + sqlite3VdbeJumpHere(v, jZeroRows); + } + sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname); + sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0); + sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid); + sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + if( pParse->nMemnMem = regRec; + if( jZeroRows ){ + sqlite3VdbeJumpHere(v, addr); + } +} + +/* +** Generate code that will cause the most recent index analysis to +** be loaded into internal hash tables where is can be used. +*/ +static void loadAnalysis(Parse *pParse, int iDb){ + Vdbe *v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb); + } +} + +/* +** Generate code that will do an analysis of an entire database +*/ +static void analyzeDatabase(Parse *pParse, int iDb){ + sqlite3 *db = pParse->db; + Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */ + HashElem *k; + int iStatCur; + int iMem; + + sqlite3BeginWriteOperation(pParse, 0, iDb); + iStatCur = pParse->nTab; + pParse->nTab += 2; + openStatTable(pParse, iDb, iStatCur, 0); + iMem = pParse->nMem+1; + for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){ + Table *pTab = (Table*)sqliteHashData(k); + analyzeOneTable(pParse, pTab, iStatCur, iMem); + } + loadAnalysis(pParse, iDb); +} + +/* +** Generate code that will do an analysis of a single table in +** a database. +*/ +static void analyzeTable(Parse *pParse, Table *pTab){ + int iDb; + int iStatCur; + + assert( pTab!=0 ); + assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); + iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + sqlite3BeginWriteOperation(pParse, 0, iDb); + iStatCur = pParse->nTab; + pParse->nTab += 2; + openStatTable(pParse, iDb, iStatCur, pTab->zName); + analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1); + loadAnalysis(pParse, iDb); +} + +/* +** Generate code for the ANALYZE command. The parser calls this routine +** when it recognizes an ANALYZE command. +** +** ANALYZE -- 1 +** ANALYZE -- 2 +** ANALYZE ?.? -- 3 +** +** Form 1 causes all indices in all attached databases to be analyzed. +** Form 2 analyzes all indices the single database named. +** Form 3 analyzes all indices associated with the named table. +*/ +SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){ + sqlite3 *db = pParse->db; + int iDb; + int i; + char *z, *zDb; + Table *pTab; + Token *pTableName; + + /* Read the database schema. If an error occurs, leave an error message + ** and code in pParse and return NULL. */ + assert( sqlite3BtreeHoldsAllMutexes(pParse->db) ); + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + return; + } + + assert( pName2!=0 || pName1==0 ); + if( pName1==0 ){ + /* Form 1: Analyze everything */ + for(i=0; inDb; i++){ + if( i==1 ) continue; /* Do not analyze the TEMP database */ + analyzeDatabase(pParse, i); + } + }else if( pName2->n==0 ){ + /* Form 2: Analyze the database or table named */ + iDb = sqlite3FindDb(db, pName1); + if( iDb>=0 ){ + analyzeDatabase(pParse, iDb); + }else{ + z = sqlite3NameFromToken(db, pName1); + if( z ){ + pTab = sqlite3LocateTable(pParse, 0, z, 0); + sqlite3DbFree(db, z); + if( pTab ){ + analyzeTable(pParse, pTab); + } + } + } + }else{ + /* Form 3: Analyze the fully qualified table name */ + iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName); + if( iDb>=0 ){ + zDb = db->aDb[iDb].zName; + z = sqlite3NameFromToken(db, pTableName); + if( z ){ + pTab = sqlite3LocateTable(pParse, 0, z, zDb); + sqlite3DbFree(db, z); + if( pTab ){ + analyzeTable(pParse, pTab); + } + } + } + } +} + +/* +** Used to pass information from the analyzer reader through to the +** callback routine. +*/ +typedef struct analysisInfo analysisInfo; +struct analysisInfo { + sqlite3 *db; + const char *zDatabase; +}; + +/* +** This callback is invoked once for each index when reading the +** sqlite_stat1 table. +** +** argv[0] = name of the table +** argv[1] = name of the index (might be NULL) +** argv[2] = results of analysis - on integer for each column +** +** Entries for which argv[1]==NULL simply record the number of rows in +** the table. +*/ +static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){ + analysisInfo *pInfo = (analysisInfo*)pData; + Index *pIndex; + Table *pTable; + int i, c, n; + unsigned int v; + const char *z; + + assert( argc==3 ); + UNUSED_PARAMETER2(NotUsed, argc); + + if( argv==0 || argv[0]==0 || argv[2]==0 ){ + return 0; + } + pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase); + if( pTable==0 ){ + return 0; + } + if( argv[1] ){ + pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase); + }else{ + pIndex = 0; + } + n = pIndex ? pIndex->nColumn : 0; + z = argv[2]; + for(i=0; *z && i<=n; i++){ + v = 0; + while( (c=z[0])>='0' && c<='9' ){ + v = v*10 + c - '0'; + z++; + } + if( i==0 ) pTable->nRowEst = v; + if( pIndex==0 ) break; + pIndex->aiRowEst[i] = v; + if( *z==' ' ) z++; + } + return 0; +} + +/* +** If the Index.aSample variable is not NULL, delete the aSample[] array +** and its contents. +*/ +SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){ +#ifdef SQLITE_ENABLE_STAT2 + if( pIdx->aSample ){ + int j; + for(j=0; jaSample[j]; + if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){ + sqlite3DbFree(db, p->u.z); + } + } + sqlite3DbFree(db, pIdx->aSample); + } +#else + UNUSED_PARAMETER(db); + UNUSED_PARAMETER(pIdx); +#endif +} + +/* +** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The +** contents of sqlite_stat1 are used to populate the Index.aiRowEst[] +** arrays. The contents of sqlite_stat2 are used to populate the +** Index.aSample[] arrays. +** +** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR +** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined +** during compilation and the sqlite_stat2 table is present, no data is +** read from it. +** +** If SQLITE_ENABLE_STAT2 was defined during compilation and the +** sqlite_stat2 table is not present in the database, SQLITE_ERROR is +** returned. However, in this case, data is read from the sqlite_stat1 +** table (if it is present) before returning. +** +** If an OOM error occurs, this function always sets db->mallocFailed. +** This means if the caller does not care about other errors, the return +** code may be ignored. +*/ +SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){ + analysisInfo sInfo; + HashElem *i; + char *zSql; + int rc; + + assert( iDb>=0 && iDbnDb ); + assert( db->aDb[iDb].pBt!=0 ); + assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); + + /* Clear any prior statistics */ + for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){ + Index *pIdx = sqliteHashData(i); + sqlite3DefaultRowEst(pIdx); + sqlite3DeleteIndexSamples(db, pIdx); + pIdx->aSample = 0; + } + + /* Check to make sure the sqlite_stat1 table exists */ + sInfo.db = db; + sInfo.zDatabase = db->aDb[iDb].zName; + if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){ + return SQLITE_ERROR; + } + + /* Load new statistics out of the sqlite_stat1 table */ + zSql = sqlite3MPrintf(db, + "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase); + if( zSql==0 ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0); + sqlite3DbFree(db, zSql); + } + + + /* Load the statistics from the sqlite_stat2 table. */ +#ifdef SQLITE_ENABLE_STAT2 + if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){ + rc = SQLITE_ERROR; + } + if( rc==SQLITE_OK ){ + sqlite3_stmt *pStmt = 0; + + zSql = sqlite3MPrintf(db, + "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase); + if( !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); + sqlite3DbFree(db, zSql); + } + + if( rc==SQLITE_OK ){ + while( sqlite3_step(pStmt)==SQLITE_ROW ){ + char *zIndex; /* Index name */ + Index *pIdx; /* Pointer to the index object */ + + zIndex = (char *)sqlite3_column_text(pStmt, 0); + pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0; + if( pIdx ){ + int iSample = sqlite3_column_int(pStmt, 1); + if( iSample=0 ){ + int eType = sqlite3_column_type(pStmt, 2); + + if( pIdx->aSample==0 ){ + static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES; + pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz); + if( pIdx->aSample==0 ){ + db->mallocFailed = 1; + break; + } + memset(pIdx->aSample, 0, sz); + } + + assert( pIdx->aSample ); + { + IndexSample *pSample = &pIdx->aSample[iSample]; + pSample->eType = (u8)eType; + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ + pSample->u.r = sqlite3_column_double(pStmt, 2); + }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){ + const char *z = (const char *)( + (eType==SQLITE_BLOB) ? + sqlite3_column_blob(pStmt, 2): + sqlite3_column_text(pStmt, 2) + ); + int n = sqlite3_column_bytes(pStmt, 2); + if( n>24 ){ + n = 24; + } + pSample->nByte = (u8)n; + if( n < 1){ + pSample->u.z = 0; + }else{ + pSample->u.z = sqlite3DbStrNDup(0, z, n); + if( pSample->u.z==0 ){ + db->mallocFailed = 1; + break; + } + } + } + } + } + } + } + rc = sqlite3_finalize(pStmt); + } + } +#endif + + if( rc==SQLITE_NOMEM ){ + db->mallocFailed = 1; + } + return rc; +} + + +#endif /* SQLITE_OMIT_ANALYZE */ + +/************** End of analyze.c *********************************************/ +/************** Begin file attach.c ******************************************/ +/* +** 2003 April 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to implement the ATTACH and DETACH commands. +*/ + +#ifndef SQLITE_OMIT_ATTACH +/* +** Resolve an expression that was part of an ATTACH or DETACH statement. This +** is slightly different from resolving a normal SQL expression, because simple +** identifiers are treated as strings, not possible column names or aliases. +** +** i.e. if the parser sees: +** +** ATTACH DATABASE abc AS def +** +** it treats the two expressions as literal strings 'abc' and 'def' instead of +** looking for columns of the same name. +** +** This only applies to the root node of pExpr, so the statement: +** +** ATTACH DATABASE abc||def AS 'db2' +** +** will fail because neither abc or def can be resolved. +*/ +static int resolveAttachExpr(NameContext *pName, Expr *pExpr) +{ + int rc = SQLITE_OK; + if( pExpr ){ + if( pExpr->op!=TK_ID ){ + rc = sqlite3ResolveExprNames(pName, pExpr); + if( rc==SQLITE_OK && !sqlite3ExprIsConstant(pExpr) ){ + sqlite3ErrorMsg(pName->pParse, "invalid name: \"%s\"", pExpr->u.zToken); + return SQLITE_ERROR; + } + }else{ + pExpr->op = TK_STRING; + } + } + return rc; +} + +/* +** An SQL user-function registered to do the work of an ATTACH statement. The +** three arguments to the function come directly from an attach statement: +** +** ATTACH DATABASE x AS y KEY z +** +** SELECT sqlite_attach(x, y, z) +** +** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the +** third argument. +*/ +static void attachFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + int i; + int rc = 0; + sqlite3 *db = sqlite3_context_db_handle(context); + const char *zName; + const char *zFile; + Db *aNew; + char *zErrDyn = 0; + + UNUSED_PARAMETER(NotUsed); + + zFile = (const char *)sqlite3_value_text(argv[0]); + zName = (const char *)sqlite3_value_text(argv[1]); + if( zFile==0 ) zFile = ""; + if( zName==0 ) zName = ""; + + /* Check for the following errors: + ** + ** * Too many attached databases, + ** * Transaction currently open + ** * Specified database name already being used. + */ + if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){ + zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d", + db->aLimit[SQLITE_LIMIT_ATTACHED] + ); + goto attach_error; + } + if( !db->autoCommit ){ + zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction"); + goto attach_error; + } + for(i=0; inDb; i++){ + char *z = db->aDb[i].zName; + assert( z && zName ); + if( sqlite3StrICmp(z, zName)==0 ){ + zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName); + goto attach_error; + } + } + + /* Allocate the new entry in the db->aDb[] array and initialise the schema + ** hash tables. + */ + if( db->aDb==db->aDbStatic ){ + aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 ); + if( aNew==0 ) return; + memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2); + }else{ + aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) ); + if( aNew==0 ) return; + } + db->aDb = aNew; + aNew = &db->aDb[db->nDb]; + memset(aNew, 0, sizeof(*aNew)); + + /* Open the database file. If the btree is successfully opened, use + ** it to obtain the database schema. At this point the schema may + ** or may not be initialised. + */ + rc = sqlite3BtreeOpen(zFile, db, &aNew->pBt, 0, + db->openFlags | SQLITE_OPEN_MAIN_DB); + db->nDb++; + if( rc==SQLITE_CONSTRAINT ){ + rc = SQLITE_ERROR; + zErrDyn = sqlite3MPrintf(db, "database is already attached"); + }else if( rc==SQLITE_OK ){ + Pager *pPager; + aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt); + if( !aNew->pSchema ){ + rc = SQLITE_NOMEM; + }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){ + zErrDyn = sqlite3MPrintf(db, + "attached databases must use the same text encoding as main database"); + rc = SQLITE_ERROR; + } + pPager = sqlite3BtreePager(aNew->pBt); + sqlite3PagerLockingMode(pPager, db->dfltLockMode); + sqlite3BtreeSecureDelete(aNew->pBt, + sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) ); + } + aNew->safety_level = 3; + aNew->zName = sqlite3DbStrDup(db, zName); + if( rc==SQLITE_OK && aNew->zName==0 ){ + rc = SQLITE_NOMEM; + } + + +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_HAS_CODEC_NO_ENCRYPTION) + if( rc==SQLITE_OK ){ + extern int sqlite3CodecAttach(sqlite3*, int, const void*, int); + extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); + int nKey; + char *zKey; + int t = sqlite3_value_type(argv[2]); + switch( t ){ + case SQLITE_INTEGER: + case SQLITE_FLOAT: + zErrDyn = sqlite3DbStrDup(db, "Invalid key value"); + rc = SQLITE_ERROR; + break; + + case SQLITE_TEXT: + case SQLITE_BLOB: + nKey = sqlite3_value_bytes(argv[2]); + zKey = (char *)sqlite3_value_blob(argv[2]); + rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); + break; + + case SQLITE_NULL: + /* No key specified. Use the key from the main database */ + sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); + rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey); + break; + } + } +#endif + + /* If the file was opened successfully, read the schema for the new database. + ** If this fails, or if opening the file failed, then close the file and + ** remove the entry from the db->aDb[] array. i.e. put everything back the way + ** we found it. + */ + if( rc==SQLITE_OK ){ + sqlite3BtreeEnterAll(db); + rc = sqlite3Init(db, &zErrDyn); + sqlite3BtreeLeaveAll(db); + } + if( rc ){ + int iDb = db->nDb - 1; + assert( iDb>=2 ); + if( db->aDb[iDb].pBt ){ + sqlite3BtreeClose(db->aDb[iDb].pBt); + db->aDb[iDb].pBt = 0; + db->aDb[iDb].pSchema = 0; + } + sqlite3ResetInternalSchema(db, 0); + db->nDb = iDb; + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ + db->mallocFailed = 1; + sqlite3DbFree(db, zErrDyn); + zErrDyn = sqlite3MPrintf(db, "out of memory"); + }else if( zErrDyn==0 ){ + zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile); + } + goto attach_error; + } + + return; + +attach_error: + /* Return an error if we get here */ + if( zErrDyn ){ + sqlite3_result_error(context, zErrDyn, -1); + sqlite3DbFree(db, zErrDyn); + } + if( rc ) sqlite3_result_error_code(context, rc); +} + +/* +** An SQL user-function registered to do the work of an DETACH statement. The +** three arguments to the function come directly from a detach statement: +** +** DETACH DATABASE x +** +** SELECT sqlite_detach(x) +*/ +static void detachFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + const char *zName = (const char *)sqlite3_value_text(argv[0]); + sqlite3 *db = sqlite3_context_db_handle(context); + int i; + Db *pDb = 0; + char zErr[128]; + + UNUSED_PARAMETER(NotUsed); + + if( zName==0 ) zName = ""; + for(i=0; inDb; i++){ + pDb = &db->aDb[i]; + if( pDb->pBt==0 ) continue; + if( sqlite3StrICmp(pDb->zName, zName)==0 ) break; + } + + if( i>=db->nDb ){ + sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName); + goto detach_error; + } + if( i<2 ){ + sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName); + goto detach_error; + } + if( !db->autoCommit ){ + sqlite3_snprintf(sizeof(zErr), zErr, + "cannot DETACH database within transaction"); + goto detach_error; + } + if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){ + sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName); + goto detach_error; + } + + sqlite3BtreeClose(pDb->pBt); + pDb->pBt = 0; + pDb->pSchema = 0; + sqlite3ResetInternalSchema(db, 0); + return; + +detach_error: + sqlite3_result_error(context, zErr, -1); +} + +/* +** This procedure generates VDBE code for a single invocation of either the +** sqlite_detach() or sqlite_attach() SQL user functions. +*/ +static void codeAttach( + Parse *pParse, /* The parser context */ + int type, /* Either SQLITE_ATTACH or SQLITE_DETACH */ + FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */ + Expr *pAuthArg, /* Expression to pass to authorization callback */ + Expr *pFilename, /* Name of database file */ + Expr *pDbname, /* Name of the database to use internally */ + Expr *pKey /* Database key for encryption extension */ +){ + int rc; + NameContext sName; + Vdbe *v; + sqlite3* db = pParse->db; + int regArgs; + + memset(&sName, 0, sizeof(NameContext)); + sName.pParse = pParse; + + if( + SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) || + SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) || + SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey)) + ){ + pParse->nErr++; + goto attach_end; + } + +#ifndef SQLITE_OMIT_AUTHORIZATION + if( pAuthArg ){ + char *zAuthArg; + if( pAuthArg->op==TK_STRING ){ + zAuthArg = pAuthArg->u.zToken; + }else{ + zAuthArg = 0; + } + rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0); + if(rc!=SQLITE_OK ){ + goto attach_end; + } + } +#endif /* SQLITE_OMIT_AUTHORIZATION */ + + + v = sqlite3GetVdbe(pParse); + regArgs = sqlite3GetTempRange(pParse, 4); + sqlite3ExprCode(pParse, pFilename, regArgs); + sqlite3ExprCode(pParse, pDbname, regArgs+1); + sqlite3ExprCode(pParse, pKey, regArgs+2); + + assert( v || db->mallocFailed ); + if( v ){ + sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3); + assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg ); + sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg)); + sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF); + + /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this + ** statement only). For DETACH, set it to false (expire all existing + ** statements). + */ + sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH)); + } + +attach_end: + sqlite3ExprDelete(db, pFilename); + sqlite3ExprDelete(db, pDbname); + sqlite3ExprDelete(db, pKey); +} + +/* +** Called by the parser to compile a DETACH statement. +** +** DETACH pDbname +*/ +SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){ + static const FuncDef detach_func = { + 1, /* nArg */ + SQLITE_UTF8, /* iPrefEnc */ + 0, /* flags */ + 0, /* pUserData */ + 0, /* pNext */ + detachFunc, /* xFunc */ + 0, /* xStep */ + 0, /* xFinalize */ + "sqlite_detach", /* zName */ + 0, /* pHash */ + 0 /* pDestructor */ + }; + codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname); +} + +/* +** Called by the parser to compile an ATTACH statement. +** +** ATTACH p AS pDbname KEY pKey +*/ +SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){ + static const FuncDef attach_func = { + 3, /* nArg */ + SQLITE_UTF8, /* iPrefEnc */ + 0, /* flags */ + 0, /* pUserData */ + 0, /* pNext */ + attachFunc, /* xFunc */ + 0, /* xStep */ + 0, /* xFinalize */ + "sqlite_attach", /* zName */ + 0, /* pHash */ + 0 /* pDestructor */ + }; + codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey); +} +#endif /* SQLITE_OMIT_ATTACH */ + +/* +** Initialize a DbFixer structure. This routine must be called prior +** to passing the structure to one of the sqliteFixAAAA() routines below. +** +** The return value indicates whether or not fixation is required. TRUE +** means we do need to fix the database references, FALSE means we do not. +*/ +SQLITE_PRIVATE int sqlite3FixInit( + DbFixer *pFix, /* The fixer to be initialized */ + Parse *pParse, /* Error messages will be written here */ + int iDb, /* This is the database that must be used */ + const char *zType, /* "view", "trigger", or "index" */ + const Token *pName /* Name of the view, trigger, or index */ +){ + sqlite3 *db; + + if( NEVER(iDb<0) || iDb==1 ) return 0; + db = pParse->db; + assert( db->nDb>iDb ); + pFix->pParse = pParse; + pFix->zDb = db->aDb[iDb].zName; + pFix->zType = zType; + pFix->pName = pName; + return 1; +} + +/* +** The following set of routines walk through the parse tree and assign +** a specific database to all table references where the database name +** was left unspecified in the original SQL statement. The pFix structure +** must have been initialized by a prior call to sqlite3FixInit(). +** +** These routines are used to make sure that an index, trigger, or +** view in one database does not refer to objects in a different database. +** (Exception: indices, triggers, and views in the TEMP database are +** allowed to refer to anything.) If a reference is explicitly made +** to an object in a different database, an error message is added to +** pParse->zErrMsg and these routines return non-zero. If everything +** checks out, these routines return 0. +*/ +SQLITE_PRIVATE int sqlite3FixSrcList( + DbFixer *pFix, /* Context of the fixation */ + SrcList *pList /* The Source list to check and modify */ +){ + int i; + const char *zDb; + struct SrcList_item *pItem; + + if( NEVER(pList==0) ) return 0; + zDb = pFix->zDb; + for(i=0, pItem=pList->a; inSrc; i++, pItem++){ + if( pItem->zDatabase==0 ){ + pItem->zDatabase = sqlite3DbStrDup(pFix->pParse->db, zDb); + }else if( sqlite3StrICmp(pItem->zDatabase,zDb)!=0 ){ + sqlite3ErrorMsg(pFix->pParse, + "%s %T cannot reference objects in database %s", + pFix->zType, pFix->pName, pItem->zDatabase); + return 1; + } +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) + if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1; + if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1; +#endif + } + return 0; +} +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) +SQLITE_PRIVATE int sqlite3FixSelect( + DbFixer *pFix, /* Context of the fixation */ + Select *pSelect /* The SELECT statement to be fixed to one database */ +){ + while( pSelect ){ + if( sqlite3FixExprList(pFix, pSelect->pEList) ){ + return 1; + } + if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){ + return 1; + } + if( sqlite3FixExpr(pFix, pSelect->pWhere) ){ + return 1; + } + if( sqlite3FixExpr(pFix, pSelect->pHaving) ){ + return 1; + } + pSelect = pSelect->pPrior; + } + return 0; +} +SQLITE_PRIVATE int sqlite3FixExpr( + DbFixer *pFix, /* Context of the fixation */ + Expr *pExpr /* The expression to be fixed to one database */ +){ + while( pExpr ){ + if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ) break; + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1; + }else{ + if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1; + } + if( sqlite3FixExpr(pFix, pExpr->pRight) ){ + return 1; + } + pExpr = pExpr->pLeft; + } + return 0; +} +SQLITE_PRIVATE int sqlite3FixExprList( + DbFixer *pFix, /* Context of the fixation */ + ExprList *pList /* The expression to be fixed to one database */ +){ + int i; + struct ExprList_item *pItem; + if( pList==0 ) return 0; + for(i=0, pItem=pList->a; inExpr; i++, pItem++){ + if( sqlite3FixExpr(pFix, pItem->pExpr) ){ + return 1; + } + } + return 0; +} +#endif + +#ifndef SQLITE_OMIT_TRIGGER +SQLITE_PRIVATE int sqlite3FixTriggerStep( + DbFixer *pFix, /* Context of the fixation */ + TriggerStep *pStep /* The trigger step be fixed to one database */ +){ + while( pStep ){ + if( sqlite3FixSelect(pFix, pStep->pSelect) ){ + return 1; + } + if( sqlite3FixExpr(pFix, pStep->pWhere) ){ + return 1; + } + if( sqlite3FixExprList(pFix, pStep->pExprList) ){ + return 1; + } + pStep = pStep->pNext; + } + return 0; +} +#endif + +/************** End of attach.c **********************************************/ +/************** Begin file auth.c ********************************************/ +/* +** 2003 January 11 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to implement the sqlite3_set_authorizer() +** API. This facility is an optional feature of the library. Embedded +** systems that do not need this facility may omit it by recompiling +** the library with -DSQLITE_OMIT_AUTHORIZATION=1 +*/ + +/* +** All of the code in this file may be omitted by defining a single +** macro. +*/ +#ifndef SQLITE_OMIT_AUTHORIZATION + +/* +** Set or clear the access authorization function. +** +** The access authorization function is be called during the compilation +** phase to verify that the user has read and/or write access permission on +** various fields of the database. The first argument to the auth function +** is a copy of the 3rd argument to this routine. The second argument +** to the auth function is one of these constants: +** +** SQLITE_CREATE_INDEX +** SQLITE_CREATE_TABLE +** SQLITE_CREATE_TEMP_INDEX +** SQLITE_CREATE_TEMP_TABLE +** SQLITE_CREATE_TEMP_TRIGGER +** SQLITE_CREATE_TEMP_VIEW +** SQLITE_CREATE_TRIGGER +** SQLITE_CREATE_VIEW +** SQLITE_DELETE +** SQLITE_DROP_INDEX +** SQLITE_DROP_TABLE +** SQLITE_DROP_TEMP_INDEX +** SQLITE_DROP_TEMP_TABLE +** SQLITE_DROP_TEMP_TRIGGER +** SQLITE_DROP_TEMP_VIEW +** SQLITE_DROP_TRIGGER +** SQLITE_DROP_VIEW +** SQLITE_INSERT +** SQLITE_PRAGMA +** SQLITE_READ +** SQLITE_SELECT +** SQLITE_TRANSACTION +** SQLITE_UPDATE +** +** The third and fourth arguments to the auth function are the name of +** the table and the column that are being accessed. The auth function +** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE. If +** SQLITE_OK is returned, it means that access is allowed. SQLITE_DENY +** means that the SQL statement will never-run - the sqlite3_exec() call +** will return with an error. SQLITE_IGNORE means that the SQL statement +** should run but attempts to read the specified column will return NULL +** and attempts to write the column will be ignored. +** +** Setting the auth function to NULL disables this hook. The default +** setting of the auth function is NULL. +*/ +SQLITE_API int sqlite3_set_authorizer( + sqlite3 *db, + int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), + void *pArg +){ + sqlite3_mutex_enter(db->mutex); + db->xAuth = xAuth; + db->pAuthArg = pArg; + sqlite3ExpirePreparedStatements(db); + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +/* +** Write an error message into pParse->zErrMsg that explains that the +** user-supplied authorization function returned an illegal value. +*/ +static void sqliteAuthBadReturnCode(Parse *pParse){ + sqlite3ErrorMsg(pParse, "authorizer malfunction"); + pParse->rc = SQLITE_ERROR; +} + +/* +** Invoke the authorization callback for permission to read column zCol from +** table zTab in database zDb. This function assumes that an authorization +** callback has been registered (i.e. that sqlite3.xAuth is not NULL). +** +** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed +** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE +** is treated as SQLITE_DENY. In this case an error is left in pParse. +*/ +SQLITE_PRIVATE int sqlite3AuthReadCol( + Parse *pParse, /* The parser context */ + const char *zTab, /* Table name */ + const char *zCol, /* Column name */ + int iDb /* Index of containing database. */ +){ + sqlite3 *db = pParse->db; /* Database handle */ + char *zDb = db->aDb[iDb].zName; /* Name of attached database */ + int rc; /* Auth callback return code */ + + rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext); + if( rc==SQLITE_DENY ){ + if( db->nDb>2 || iDb!=0 ){ + sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol); + }else{ + sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol); + } + pParse->rc = SQLITE_AUTH; + }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){ + sqliteAuthBadReturnCode(pParse); + } + return rc; +} + +/* +** The pExpr should be a TK_COLUMN expression. The table referred to +** is in pTabList or else it is the NEW or OLD table of a trigger. +** Check to see if it is OK to read this particular column. +** +** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN +** instruction into a TK_NULL. If the auth function returns SQLITE_DENY, +** then generate an error. +*/ +SQLITE_PRIVATE void sqlite3AuthRead( + Parse *pParse, /* The parser context */ + Expr *pExpr, /* The expression to check authorization on */ + Schema *pSchema, /* The schema of the expression */ + SrcList *pTabList /* All table that pExpr might refer to */ +){ + sqlite3 *db = pParse->db; + Table *pTab = 0; /* The table being read */ + const char *zCol; /* Name of the column of the table */ + int iSrc; /* Index in pTabList->a[] of table being read */ + int iDb; /* The index of the database the expression refers to */ + int iCol; /* Index of column in table */ + + if( db->xAuth==0 ) return; + iDb = sqlite3SchemaToIndex(pParse->db, pSchema); + if( iDb<0 ){ + /* An attempt to read a column out of a subquery or other + ** temporary table. */ + return; + } + + assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER ); + if( pExpr->op==TK_TRIGGER ){ + pTab = pParse->pTriggerTab; + }else{ + assert( pTabList ); + for(iSrc=0; ALWAYS(iSrcnSrc); iSrc++){ + if( pExpr->iTable==pTabList->a[iSrc].iCursor ){ + pTab = pTabList->a[iSrc].pTab; + break; + } + } + } + iCol = pExpr->iColumn; + if( NEVER(pTab==0) ) return; + + if( iCol>=0 ){ + assert( iColnCol ); + zCol = pTab->aCol[iCol].zName; + }else if( pTab->iPKey>=0 ){ + assert( pTab->iPKeynCol ); + zCol = pTab->aCol[pTab->iPKey].zName; + }else{ + zCol = "ROWID"; + } + assert( iDb>=0 && iDbnDb ); + if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){ + pExpr->op = TK_NULL; + } +} + +/* +** Do an authorization check using the code and arguments given. Return +** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY. If SQLITE_DENY +** is returned, then the error count and error message in pParse are +** modified appropriately. +*/ +SQLITE_PRIVATE int sqlite3AuthCheck( + Parse *pParse, + int code, + const char *zArg1, + const char *zArg2, + const char *zArg3 +){ + sqlite3 *db = pParse->db; + int rc; + + /* Don't do any authorization checks if the database is initialising + ** or if the parser is being invoked from within sqlite3_declare_vtab. + */ + if( db->init.busy || IN_DECLARE_VTAB ){ + return SQLITE_OK; + } + + if( db->xAuth==0 ){ + return SQLITE_OK; + } + rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext); + if( rc==SQLITE_DENY ){ + sqlite3ErrorMsg(pParse, "not authorized"); + pParse->rc = SQLITE_AUTH; + }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){ + rc = SQLITE_DENY; + sqliteAuthBadReturnCode(pParse); + } + return rc; +} + +/* +** Push an authorization context. After this routine is called, the +** zArg3 argument to authorization callbacks will be zContext until +** popped. Or if pParse==0, this routine is a no-op. +*/ +SQLITE_PRIVATE void sqlite3AuthContextPush( + Parse *pParse, + AuthContext *pContext, + const char *zContext +){ + assert( pParse ); + pContext->pParse = pParse; + pContext->zAuthContext = pParse->zAuthContext; + pParse->zAuthContext = zContext; +} + +/* +** Pop an authorization context that was previously pushed +** by sqlite3AuthContextPush +*/ +SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){ + if( pContext->pParse ){ + pContext->pParse->zAuthContext = pContext->zAuthContext; + pContext->pParse = 0; + } +} + +#endif /* SQLITE_OMIT_AUTHORIZATION */ + +/************** End of auth.c ************************************************/ +/************** Begin file build.c *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that are called by the SQLite parser +** when syntax rules are reduced. The routines in this file handle the +** following kinds of SQL syntax: +** +** CREATE TABLE +** DROP TABLE +** CREATE INDEX +** DROP INDEX +** creating ID lists +** BEGIN TRANSACTION +** COMMIT +** ROLLBACK +*/ + +/* +** This routine is called when a new SQL statement is beginning to +** be parsed. Initialize the pParse structure as needed. +*/ +SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){ + pParse->explain = (u8)explainFlag; + pParse->nVar = 0; +} + +#ifndef SQLITE_OMIT_SHARED_CACHE +/* +** The TableLock structure is only used by the sqlite3TableLock() and +** codeTableLocks() functions. +*/ +struct TableLock { + int iDb; /* The database containing the table to be locked */ + int iTab; /* The root page of the table to be locked */ + u8 isWriteLock; /* True for write lock. False for a read lock */ + const char *zName; /* Name of the table */ +}; + +/* +** Record the fact that we want to lock a table at run-time. +** +** The table to be locked has root page iTab and is found in database iDb. +** A read or a write lock can be taken depending on isWritelock. +** +** This routine just records the fact that the lock is desired. The +** code to make the lock occur is generated by a later call to +** codeTableLocks() which occurs during sqlite3FinishCoding(). +*/ +SQLITE_PRIVATE void sqlite3TableLock( + Parse *pParse, /* Parsing context */ + int iDb, /* Index of the database containing the table to lock */ + int iTab, /* Root page number of the table to be locked */ + u8 isWriteLock, /* True for a write lock */ + const char *zName /* Name of the table to be locked */ +){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + int i; + int nBytes; + TableLock *p; + assert( iDb>=0 ); + + for(i=0; inTableLock; i++){ + p = &pToplevel->aTableLock[i]; + if( p->iDb==iDb && p->iTab==iTab ){ + p->isWriteLock = (p->isWriteLock || isWriteLock); + return; + } + } + + nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1); + pToplevel->aTableLock = + sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes); + if( pToplevel->aTableLock ){ + p = &pToplevel->aTableLock[pToplevel->nTableLock++]; + p->iDb = iDb; + p->iTab = iTab; + p->isWriteLock = isWriteLock; + p->zName = zName; + }else{ + pToplevel->nTableLock = 0; + pToplevel->db->mallocFailed = 1; + } +} + +/* +** Code an OP_TableLock instruction for each table locked by the +** statement (configured by calls to sqlite3TableLock()). +*/ +static void codeTableLocks(Parse *pParse){ + int i; + Vdbe *pVdbe; + + pVdbe = sqlite3GetVdbe(pParse); + assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */ + + for(i=0; inTableLock; i++){ + TableLock *p = &pParse->aTableLock[i]; + int p1 = p->iDb; + sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock, + p->zName, P4_STATIC); + } +} +#else + #define codeTableLocks(x) +#endif + +/* +** This routine is called after a single SQL statement has been +** parsed and a VDBE program to execute that statement has been +** prepared. This routine puts the finishing touches on the +** VDBE program and resets the pParse structure for the next +** parse. +** +** Note that if an error occurred, it might be the case that +** no VDBE code was generated. +*/ +SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){ + sqlite3 *db; + Vdbe *v; + + db = pParse->db; + if( db->mallocFailed ) return; + if( pParse->nested ) return; + if( pParse->nErr ) return; + + /* Begin by generating some termination code at the end of the + ** vdbe program + */ + v = sqlite3GetVdbe(pParse); + assert( !pParse->isMultiWrite + || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort)); + if( v ){ + sqlite3VdbeAddOp0(v, OP_Halt); + + /* The cookie mask contains one bit for each database file open. + ** (Bit 0 is for main, bit 1 is for temp, and so forth.) Bits are + ** set for each database that is used. Generate code to start a + ** transaction on each used database and to verify the schema cookie + ** on each used database. + */ + if( pParse->cookieGoto>0 ){ + u32 mask; + int iDb; + sqlite3VdbeJumpHere(v, pParse->cookieGoto-1); + for(iDb=0, mask=1; iDbnDb; mask<<=1, iDb++){ + if( (mask & pParse->cookieMask)==0 ) continue; + sqlite3VdbeUsesBtree(v, iDb); + sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0); + if( db->init.busy==0 ){ + sqlite3VdbeAddOp2(v,OP_VerifyCookie, iDb, pParse->cookieValue[iDb]); + } + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + { + int i; + for(i=0; inVtabLock; i++){ + char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]); + sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB); + } + pParse->nVtabLock = 0; + } +#endif + + /* Once all the cookies have been verified and transactions opened, + ** obtain the required table-locks. This is a no-op unless the + ** shared-cache feature is enabled. + */ + codeTableLocks(pParse); + + /* Initialize any AUTOINCREMENT data structures required. + */ + sqlite3AutoincrementBegin(pParse); + + /* Finally, jump back to the beginning of the executable code. */ + sqlite3VdbeAddOp2(v, OP_Goto, 0, pParse->cookieGoto); + } + } + + + /* Get the VDBE program ready for execution + */ + if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){ +#ifdef SQLITE_DEBUG + FILE *trace = (db->flags & SQLITE_VdbeTrace)!=0 ? stdout : 0; + sqlite3VdbeTrace(v, trace); +#endif + assert( pParse->iCacheLevel==0 ); /* Disables and re-enables match */ + /* A minimum of one cursor is required if autoincrement is used + * See ticket [a696379c1f08866] */ + if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1; + sqlite3VdbeMakeReady(v, pParse->nVar, pParse->nMem, + pParse->nTab, pParse->nMaxArg, pParse->explain, + pParse->isMultiWrite && pParse->mayAbort); + pParse->rc = SQLITE_DONE; + pParse->colNamesSet = 0; + }else{ + pParse->rc = SQLITE_ERROR; + } + pParse->nTab = 0; + pParse->nMem = 0; + pParse->nSet = 0; + pParse->nVar = 0; + pParse->cookieMask = 0; + pParse->cookieGoto = 0; +} + +/* +** Run the parser and code generator recursively in order to generate +** code for the SQL statement given onto the end of the pParse context +** currently under construction. When the parser is run recursively +** this way, the final OP_Halt is not appended and other initialization +** and finalization steps are omitted because those are handling by the +** outermost parser. +** +** Not everything is nestable. This facility is designed to permit +** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER. Use +** care if you decide to try to use this routine for some other purposes. +*/ +SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){ + va_list ap; + char *zSql; + char *zErrMsg = 0; + sqlite3 *db = pParse->db; +# define SAVE_SZ (sizeof(Parse) - offsetof(Parse,nVar)) + char saveBuf[SAVE_SZ]; + + if( pParse->nErr ) return; + assert( pParse->nested<10 ); /* Nesting should only be of limited depth */ + va_start(ap, zFormat); + zSql = sqlite3VMPrintf(db, zFormat, ap); + va_end(ap); + if( zSql==0 ){ + return; /* A malloc must have failed */ + } + pParse->nested++; + memcpy(saveBuf, &pParse->nVar, SAVE_SZ); + memset(&pParse->nVar, 0, SAVE_SZ); + sqlite3RunParser(pParse, zSql, &zErrMsg); + sqlite3DbFree(db, zErrMsg); + sqlite3DbFree(db, zSql); + memcpy(&pParse->nVar, saveBuf, SAVE_SZ); + pParse->nested--; +} + +/* +** Locate the in-memory structure that describes a particular database +** table given the name of that table and (optionally) the name of the +** database containing the table. Return NULL if not found. +** +** If zDatabase is 0, all databases are searched for the table and the +** first matching table is returned. (No checking for duplicate table +** names is done.) The search order is TEMP first, then MAIN, then any +** auxiliary databases added using the ATTACH command. +** +** See also sqlite3LocateTable(). +*/ +SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){ + Table *p = 0; + int i; + int nName; + assert( zName!=0 ); + nName = sqlite3Strlen30(zName); + for(i=OMIT_TEMPDB; inDb; i++){ + int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ + if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue; + p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName); + if( p ) break; + } + return p; +} + +/* +** Locate the in-memory structure that describes a particular database +** table given the name of that table and (optionally) the name of the +** database containing the table. Return NULL if not found. Also leave an +** error message in pParse->zErrMsg. +** +** The difference between this routine and sqlite3FindTable() is that this +** routine leaves an error message in pParse->zErrMsg where +** sqlite3FindTable() does not. +*/ +SQLITE_PRIVATE Table *sqlite3LocateTable( + Parse *pParse, /* context in which to report errors */ + int isView, /* True if looking for a VIEW rather than a TABLE */ + const char *zName, /* Name of the table we are looking for */ + const char *zDbase /* Name of the database. Might be NULL */ +){ + Table *p; + + /* Read the database schema. If an error occurs, leave an error message + ** and code in pParse and return NULL. */ + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + return 0; + } + + p = sqlite3FindTable(pParse->db, zName, zDbase); + if( p==0 ){ + const char *zMsg = isView ? "no such view" : "no such table"; + if( zDbase ){ + sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName); + }else{ + sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName); + } + pParse->checkSchema = 1; + } + return p; +} + +/* +** Locate the in-memory structure that describes +** a particular index given the name of that index +** and the name of the database that contains the index. +** Return NULL if not found. +** +** If zDatabase is 0, all databases are searched for the +** table and the first matching index is returned. (No checking +** for duplicate index names is done.) The search order is +** TEMP first, then MAIN, then any auxiliary databases added +** using the ATTACH command. +*/ +SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){ + Index *p = 0; + int i; + int nName = sqlite3Strlen30(zName); + for(i=OMIT_TEMPDB; inDb; i++){ + int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ + Schema *pSchema = db->aDb[j].pSchema; + assert( pSchema ); + if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue; + p = sqlite3HashFind(&pSchema->idxHash, zName, nName); + if( p ) break; + } + return p; +} + +/* +** Reclaim the memory used by an index +*/ +static void freeIndex(sqlite3 *db, Index *p){ +#ifndef SQLITE_OMIT_ANALYZE + sqlite3DeleteIndexSamples(db, p); +#endif + sqlite3DbFree(db, p->zColAff); + sqlite3DbFree(db, p); +} + +/* +** For the index called zIdxName which is found in the database iDb, +** unlike that index from its Table then remove the index from +** the index hash table and free all memory structures associated +** with the index. +*/ +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){ + Index *pIndex; + int len; + Hash *pHash = &db->aDb[iDb].pSchema->idxHash; + + len = sqlite3Strlen30(zIdxName); + pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0); + if( pIndex ){ + if( pIndex->pTable->pIndex==pIndex ){ + pIndex->pTable->pIndex = pIndex->pNext; + }else{ + Index *p; + /* Justification of ALWAYS(); The index must be on the list of + ** indices. */ + p = pIndex->pTable->pIndex; + while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; } + if( ALWAYS(p && p->pNext==pIndex) ){ + p->pNext = pIndex->pNext; + } + } + freeIndex(db, pIndex); + } + db->flags |= SQLITE_InternChanges; +} + +/* +** Erase all schema information from the in-memory hash tables of +** a single database. This routine is called to reclaim memory +** before the database closes. It is also called during a rollback +** if there were schema changes during the transaction or if a +** schema-cookie mismatch occurs. +** +** If iDb==0 then reset the internal schema tables for all database +** files. If iDb>=1 then reset the internal schema for only the +** single file indicated. +*/ +SQLITE_PRIVATE void sqlite3ResetInternalSchema(sqlite3 *db, int iDb){ + int i, j; + assert( iDb>=0 && iDbnDb ); + + if( iDb==0 ){ + sqlite3BtreeEnterAll(db); + } + for(i=iDb; inDb; i++){ + Db *pDb = &db->aDb[i]; + if( pDb->pSchema ){ + assert(i==1 || (pDb->pBt && sqlite3BtreeHoldsMutex(pDb->pBt))); + sqlite3SchemaFree(pDb->pSchema); + } + if( iDb>0 ) return; + } + assert( iDb==0 ); + db->flags &= ~SQLITE_InternChanges; + sqlite3VtabUnlockList(db); + sqlite3BtreeLeaveAll(db); + + /* If one or more of the auxiliary database files has been closed, + ** then remove them from the auxiliary database list. We take the + ** opportunity to do this here since we have just deleted all of the + ** schema hash tables and therefore do not have to make any changes + ** to any of those tables. + */ + for(i=j=2; inDb; i++){ + struct Db *pDb = &db->aDb[i]; + if( pDb->pBt==0 ){ + sqlite3DbFree(db, pDb->zName); + pDb->zName = 0; + continue; + } + if( jaDb[j] = db->aDb[i]; + } + j++; + } + memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j])); + db->nDb = j; + if( db->nDb<=2 && db->aDb!=db->aDbStatic ){ + memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0])); + sqlite3DbFree(db, db->aDb); + db->aDb = db->aDbStatic; + } +} + +/* +** This routine is called when a commit occurs. +*/ +SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){ + db->flags &= ~SQLITE_InternChanges; +} + +/* +** Delete memory allocated for the column names of a table or view (the +** Table.aCol[] array). +*/ +static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){ + int i; + Column *pCol; + assert( pTable!=0 ); + if( (pCol = pTable->aCol)!=0 ){ + for(i=0; inCol; i++, pCol++){ + sqlite3DbFree(db, pCol->zName); + sqlite3ExprDelete(db, pCol->pDflt); + sqlite3DbFree(db, pCol->zDflt); + sqlite3DbFree(db, pCol->zType); + sqlite3DbFree(db, pCol->zColl); + } + sqlite3DbFree(db, pTable->aCol); + } +} + +/* +** Remove the memory data structures associated with the given +** Table. No changes are made to disk by this routine. +** +** This routine just deletes the data structure. It does not unlink +** the table data structure from the hash table. But it does destroy +** memory structures of the indices and foreign keys associated with +** the table. +*/ +SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){ + Index *pIndex, *pNext; + + assert( !pTable || pTable->nRef>0 ); + + /* Do not delete the table until the reference count reaches zero. */ + if( !pTable ) return; + if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return; + + /* Delete all indices associated with this table. */ + for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){ + pNext = pIndex->pNext; + assert( pIndex->pSchema==pTable->pSchema ); + if( !db || db->pnBytesFreed==0 ){ + char *zName = pIndex->zName; + TESTONLY ( Index *pOld = ) sqlite3HashInsert( + &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0 + ); + assert( pOld==pIndex || pOld==0 ); + } + freeIndex(db, pIndex); + } + + /* Delete any foreign keys attached to this table. */ + sqlite3FkDelete(db, pTable); + + /* Delete the Table structure itself. + */ + sqliteDeleteColumnNames(db, pTable); + sqlite3DbFree(db, pTable->zName); + sqlite3DbFree(db, pTable->zColAff); + sqlite3SelectDelete(db, pTable->pSelect); +#ifndef SQLITE_OMIT_CHECK + sqlite3ExprDelete(db, pTable->pCheck); +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + sqlite3VtabClear(db, pTable); +#endif + sqlite3DbFree(db, pTable); +} + +/* +** Unlink the given table from the hash tables and the delete the +** table structure with all its indices and foreign keys. +*/ +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){ + Table *p; + Db *pDb; + + assert( db!=0 ); + assert( iDb>=0 && iDbnDb ); + assert( zTabName ); + testcase( zTabName[0]==0 ); /* Zero-length table names are allowed */ + pDb = &db->aDb[iDb]; + p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName, + sqlite3Strlen30(zTabName),0); + sqlite3DeleteTable(db, p); + db->flags |= SQLITE_InternChanges; +} + +/* +** Given a token, return a string that consists of the text of that +** token. Space to hold the returned string +** is obtained from sqliteMalloc() and must be freed by the calling +** function. +** +** Any quotation marks (ex: "name", 'name', [name], or `name`) that +** surround the body of the token are removed. +** +** Tokens are often just pointers into the original SQL text and so +** are not \000 terminated and are not persistent. The returned string +** is \000 terminated and is persistent. +*/ +SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){ + char *zName; + if( pName ){ + zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n); + sqlite3Dequote(zName); + }else{ + zName = 0; + } + return zName; +} + +/* +** Open the sqlite_master table stored in database number iDb for +** writing. The table is opened using cursor 0. +*/ +SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){ + Vdbe *v = sqlite3GetVdbe(p); + sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb)); + sqlite3VdbeAddOp3(v, OP_OpenWrite, 0, MASTER_ROOT, iDb); + sqlite3VdbeChangeP4(v, -1, (char *)5, P4_INT32); /* 5 column table */ + if( p->nTab==0 ){ + p->nTab = 1; + } +} + +/* +** Parameter zName points to a nul-terminated buffer containing the name +** of a database ("main", "temp" or the name of an attached db). This +** function returns the index of the named database in db->aDb[], or +** -1 if the named db cannot be found. +*/ +SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){ + int i = -1; /* Database number */ + if( zName ){ + Db *pDb; + int n = sqlite3Strlen30(zName); + for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){ + if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) && + 0==sqlite3StrICmp(pDb->zName, zName) ){ + break; + } + } + } + return i; +} + +/* +** The token *pName contains the name of a database (either "main" or +** "temp" or the name of an attached db). This routine returns the +** index of the named database in db->aDb[], or -1 if the named db +** does not exist. +*/ +SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){ + int i; /* Database number */ + char *zName; /* Name we are searching for */ + zName = sqlite3NameFromToken(db, pName); + i = sqlite3FindDbName(db, zName); + sqlite3DbFree(db, zName); + return i; +} + +/* The table or view or trigger name is passed to this routine via tokens +** pName1 and pName2. If the table name was fully qualified, for example: +** +** CREATE TABLE xxx.yyy (...); +** +** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if +** the table name is not fully qualified, i.e.: +** +** CREATE TABLE yyy(...); +** +** Then pName1 is set to "yyy" and pName2 is "". +** +** This routine sets the *ppUnqual pointer to point at the token (pName1 or +** pName2) that stores the unqualified table name. The index of the +** database "xxx" is returned. +*/ +SQLITE_PRIVATE int sqlite3TwoPartName( + Parse *pParse, /* Parsing and code generating context */ + Token *pName1, /* The "xxx" in the name "xxx.yyy" or "xxx" */ + Token *pName2, /* The "yyy" in the name "xxx.yyy" */ + Token **pUnqual /* Write the unqualified object name here */ +){ + int iDb; /* Database holding the object */ + sqlite3 *db = pParse->db; + + if( ALWAYS(pName2!=0) && pName2->n>0 ){ + if( db->init.busy ) { + sqlite3ErrorMsg(pParse, "corrupt database"); + pParse->nErr++; + return -1; + } + *pUnqual = pName2; + iDb = sqlite3FindDb(db, pName1); + if( iDb<0 ){ + sqlite3ErrorMsg(pParse, "unknown database %T", pName1); + pParse->nErr++; + return -1; + } + }else{ + assert( db->init.iDb==0 || db->init.busy ); + iDb = db->init.iDb; + *pUnqual = pName1; + } + return iDb; +} + +/* +** This routine is used to check if the UTF-8 string zName is a legal +** unqualified name for a new schema object (table, index, view or +** trigger). All names are legal except those that begin with the string +** "sqlite_" (in upper, lower or mixed case). This portion of the namespace +** is reserved for internal use. +*/ +SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){ + if( !pParse->db->init.busy && pParse->nested==0 + && (pParse->db->flags & SQLITE_WriteSchema)==0 + && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){ + sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName); + return SQLITE_ERROR; + } + return SQLITE_OK; +} + +/* +** Begin constructing a new table representation in memory. This is +** the first of several action routines that get called in response +** to a CREATE TABLE statement. In particular, this routine is called +** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp +** flag is true if the table should be stored in the auxiliary database +** file instead of in the main database file. This is normally the case +** when the "TEMP" or "TEMPORARY" keyword occurs in between +** CREATE and TABLE. +** +** The new table record is initialized and put in pParse->pNewTable. +** As more of the CREATE TABLE statement is parsed, additional action +** routines will be called to add more information to this record. +** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine +** is called to complete the construction of the new table record. +*/ +SQLITE_PRIVATE void sqlite3StartTable( + Parse *pParse, /* Parser context */ + Token *pName1, /* First part of the name of the table or view */ + Token *pName2, /* Second part of the name of the table or view */ + int isTemp, /* True if this is a TEMP table */ + int isView, /* True if this is a VIEW */ + int isVirtual, /* True if this is a VIRTUAL table */ + int noErr /* Do nothing if table already exists */ +){ + Table *pTable; + char *zName = 0; /* The name of the new table */ + sqlite3 *db = pParse->db; + Vdbe *v; + int iDb; /* Database number to create the table in */ + Token *pName; /* Unqualified name of the table to create */ + + /* The table or view name to create is passed to this routine via tokens + ** pName1 and pName2. If the table name was fully qualified, for example: + ** + ** CREATE TABLE xxx.yyy (...); + ** + ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if + ** the table name is not fully qualified, i.e.: + ** + ** CREATE TABLE yyy(...); + ** + ** Then pName1 is set to "yyy" and pName2 is "". + ** + ** The call below sets the pName pointer to point at the token (pName1 or + ** pName2) that stores the unqualified table name. The variable iDb is + ** set to the index of the database that the table or view is to be + ** created in. + */ + iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); + if( iDb<0 ) return; + if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){ + /* If creating a temp table, the name may not be qualified. Unless + ** the database name is "temp" anyway. */ + sqlite3ErrorMsg(pParse, "temporary table name must be unqualified"); + return; + } + if( !OMIT_TEMPDB && isTemp ) iDb = 1; + + pParse->sNameToken = *pName; + zName = sqlite3NameFromToken(db, pName); + if( zName==0 ) return; + if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ + goto begin_table_error; + } + if( db->init.iDb==1 ) isTemp = 1; +#ifndef SQLITE_OMIT_AUTHORIZATION + assert( (isTemp & 1)==isTemp ); + { + int code; + char *zDb = db->aDb[iDb].zName; + if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){ + goto begin_table_error; + } + if( isView ){ + if( !OMIT_TEMPDB && isTemp ){ + code = SQLITE_CREATE_TEMP_VIEW; + }else{ + code = SQLITE_CREATE_VIEW; + } + }else{ + if( !OMIT_TEMPDB && isTemp ){ + code = SQLITE_CREATE_TEMP_TABLE; + }else{ + code = SQLITE_CREATE_TABLE; + } + } + if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){ + goto begin_table_error; + } + } +#endif + + /* Make sure the new table name does not collide with an existing + ** index or table name in the same database. Issue an error message if + ** it does. The exception is if the statement being parsed was passed + ** to an sqlite3_declare_vtab() call. In that case only the column names + ** and types will be used, so there is no need to test for namespace + ** collisions. + */ + if( !IN_DECLARE_VTAB ){ + char *zDb = db->aDb[iDb].zName; + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + goto begin_table_error; + } + pTable = sqlite3FindTable(db, zName, zDb); + if( pTable ){ + if( !noErr ){ + sqlite3ErrorMsg(pParse, "table %T already exists", pName); + } + goto begin_table_error; + } + if( sqlite3FindIndex(db, zName, zDb)!=0 ){ + sqlite3ErrorMsg(pParse, "there is already an index named %s", zName); + goto begin_table_error; + } + } + + pTable = sqlite3DbMallocZero(db, sizeof(Table)); + if( pTable==0 ){ + db->mallocFailed = 1; + pParse->rc = SQLITE_NOMEM; + pParse->nErr++; + goto begin_table_error; + } + pTable->zName = zName; + pTable->iPKey = -1; + pTable->pSchema = db->aDb[iDb].pSchema; + pTable->nRef = 1; + pTable->nRowEst = 1000000; + assert( pParse->pNewTable==0 ); + pParse->pNewTable = pTable; + + /* If this is the magic sqlite_sequence table used by autoincrement, + ** then record a pointer to this table in the main database structure + ** so that INSERT can find the table easily. + */ +#ifndef SQLITE_OMIT_AUTOINCREMENT + if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){ + pTable->pSchema->pSeqTab = pTable; + } +#endif + + /* Begin generating the code that will insert the table record into + ** the SQLITE_MASTER table. Note in particular that we must go ahead + ** and allocate the record number for the table entry now. Before any + ** PRIMARY KEY or UNIQUE keywords are parsed. Those keywords will cause + ** indices to be created and the table record must come before the + ** indices. Hence, the record number for the table must be allocated + ** now. + */ + if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){ + int j1; + int fileFormat; + int reg1, reg2, reg3; + sqlite3BeginWriteOperation(pParse, 0, iDb); + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( isVirtual ){ + sqlite3VdbeAddOp0(v, OP_VBegin); + } +#endif + + /* If the file format and encoding in the database have not been set, + ** set them now. + */ + reg1 = pParse->regRowid = ++pParse->nMem; + reg2 = pParse->regRoot = ++pParse->nMem; + reg3 = ++pParse->nMem; + sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT); + sqlite3VdbeUsesBtree(v, iDb); + j1 = sqlite3VdbeAddOp1(v, OP_If, reg3); + fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ? + 1 : SQLITE_MAX_FILE_FORMAT; + sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3); + sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3); + sqlite3VdbeJumpHere(v, j1); + + /* This just creates a place-holder record in the sqlite_master table. + ** The record created does not contain anything yet. It will be replaced + ** by the real entry in code generated at sqlite3EndTable(). + ** + ** The rowid for the new entry is left in register pParse->regRowid. + ** The root page number of the new table is left in reg pParse->regRoot. + ** The rowid and root page number values are needed by the code that + ** sqlite3EndTable will generate. + */ +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) + if( isView || isVirtual ){ + sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2); + }else +#endif + { + sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2); + } + sqlite3OpenMasterTable(pParse, iDb); + sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1); + sqlite3VdbeAddOp2(v, OP_Null, 0, reg3); + sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3VdbeAddOp0(v, OP_Close); + } + + /* Normal (non-error) return. */ + return; + + /* If an error occurs, we jump here */ +begin_table_error: + sqlite3DbFree(db, zName); + return; +} + +/* +** This macro is used to compare two strings in a case-insensitive manner. +** It is slightly faster than calling sqlite3StrICmp() directly, but +** produces larger code. +** +** WARNING: This macro is not compatible with the strcmp() family. It +** returns true if the two strings are equal, otherwise false. +*/ +#define STRICMP(x, y) (\ +sqlite3UpperToLower[*(unsigned char *)(x)]== \ +sqlite3UpperToLower[*(unsigned char *)(y)] \ +&& sqlite3StrICmp((x)+1,(y)+1)==0 ) + +/* +** Add a new column to the table currently being constructed. +** +** The parser calls this routine once for each column declaration +** in a CREATE TABLE statement. sqlite3StartTable() gets called +** first to get things going. Then this routine is called for each +** column. +*/ +SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){ + Table *p; + int i; + char *z; + Column *pCol; + sqlite3 *db = pParse->db; + if( (p = pParse->pNewTable)==0 ) return; +#if SQLITE_MAX_COLUMN + if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){ + sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName); + return; + } +#endif + z = sqlite3NameFromToken(db, pName); + if( z==0 ) return; + for(i=0; inCol; i++){ + if( STRICMP(z, p->aCol[i].zName) ){ + sqlite3ErrorMsg(pParse, "duplicate column name: %s", z); + sqlite3DbFree(db, z); + return; + } + } + if( (p->nCol & 0x7)==0 ){ + Column *aNew; + aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0])); + if( aNew==0 ){ + sqlite3DbFree(db, z); + return; + } + p->aCol = aNew; + } + pCol = &p->aCol[p->nCol]; + memset(pCol, 0, sizeof(p->aCol[0])); + pCol->zName = z; + + /* If there is no type specified, columns have the default affinity + ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will + ** be called next to set pCol->affinity correctly. + */ + pCol->affinity = SQLITE_AFF_NONE; + p->nCol++; +} + +/* +** This routine is called by the parser while in the middle of +** parsing a CREATE TABLE statement. A "NOT NULL" constraint has +** been seen on a column. This routine sets the notNull flag on +** the column currently under construction. +*/ +SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){ + Table *p; + p = pParse->pNewTable; + if( p==0 || NEVER(p->nCol<1) ) return; + p->aCol[p->nCol-1].notNull = (u8)onError; +} + +/* +** Scan the column type name zType (length nType) and return the +** associated affinity type. +** +** This routine does a case-independent search of zType for the +** substrings in the following table. If one of the substrings is +** found, the corresponding affinity is returned. If zType contains +** more than one of the substrings, entries toward the top of +** the table take priority. For example, if zType is 'BLOBINT', +** SQLITE_AFF_INTEGER is returned. +** +** Substring | Affinity +** -------------------------------- +** 'INT' | SQLITE_AFF_INTEGER +** 'CHAR' | SQLITE_AFF_TEXT +** 'CLOB' | SQLITE_AFF_TEXT +** 'TEXT' | SQLITE_AFF_TEXT +** 'BLOB' | SQLITE_AFF_NONE +** 'REAL' | SQLITE_AFF_REAL +** 'FLOA' | SQLITE_AFF_REAL +** 'DOUB' | SQLITE_AFF_REAL +** +** If none of the substrings in the above table are found, +** SQLITE_AFF_NUMERIC is returned. +*/ +SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn){ + u32 h = 0; + char aff = SQLITE_AFF_NUMERIC; + + if( zIn ) while( zIn[0] ){ + h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff]; + zIn++; + if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){ /* CHAR */ + aff = SQLITE_AFF_TEXT; + }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){ /* CLOB */ + aff = SQLITE_AFF_TEXT; + }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){ /* TEXT */ + aff = SQLITE_AFF_TEXT; + }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b') /* BLOB */ + && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){ + aff = SQLITE_AFF_NONE; +#ifndef SQLITE_OMIT_FLOATING_POINT + }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l') /* REAL */ + && aff==SQLITE_AFF_NUMERIC ){ + aff = SQLITE_AFF_REAL; + }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a') /* FLOA */ + && aff==SQLITE_AFF_NUMERIC ){ + aff = SQLITE_AFF_REAL; + }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b') /* DOUB */ + && aff==SQLITE_AFF_NUMERIC ){ + aff = SQLITE_AFF_REAL; +#endif + }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){ /* INT */ + aff = SQLITE_AFF_INTEGER; + break; + } + } + + return aff; +} + +/* +** This routine is called by the parser while in the middle of +** parsing a CREATE TABLE statement. The pFirst token is the first +** token in the sequence of tokens that describe the type of the +** column currently under construction. pLast is the last token +** in the sequence. Use this information to construct a string +** that contains the typename of the column and store that string +** in zType. +*/ +SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){ + Table *p; + Column *pCol; + + p = pParse->pNewTable; + if( p==0 || NEVER(p->nCol<1) ) return; + pCol = &p->aCol[p->nCol-1]; + assert( pCol->zType==0 ); + pCol->zType = sqlite3NameFromToken(pParse->db, pType); + pCol->affinity = sqlite3AffinityType(pCol->zType); +} + +/* +** The expression is the default value for the most recently added column +** of the table currently under construction. +** +** Default value expressions must be constant. Raise an exception if this +** is not the case. +** +** This routine is called by the parser while in the middle of +** parsing a CREATE TABLE statement. +*/ +SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){ + Table *p; + Column *pCol; + sqlite3 *db = pParse->db; + p = pParse->pNewTable; + if( p!=0 ){ + pCol = &(p->aCol[p->nCol-1]); + if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){ + sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant", + pCol->zName); + }else{ + /* A copy of pExpr is used instead of the original, as pExpr contains + ** tokens that point to volatile memory. The 'span' of the expression + ** is required by pragma table_info. + */ + sqlite3ExprDelete(db, pCol->pDflt); + pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE); + sqlite3DbFree(db, pCol->zDflt); + pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart, + (int)(pSpan->zEnd - pSpan->zStart)); + } + } + sqlite3ExprDelete(db, pSpan->pExpr); +} + +/* +** Designate the PRIMARY KEY for the table. pList is a list of names +** of columns that form the primary key. If pList is NULL, then the +** most recently added column of the table is the primary key. +** +** A table can have at most one primary key. If the table already has +** a primary key (and this is the second primary key) then create an +** error. +** +** If the PRIMARY KEY is on a single column whose datatype is INTEGER, +** then we will try to use that column as the rowid. Set the Table.iPKey +** field of the table under construction to be the index of the +** INTEGER PRIMARY KEY column. Table.iPKey is set to -1 if there is +** no INTEGER PRIMARY KEY. +** +** If the key is not an INTEGER PRIMARY KEY, then create a unique +** index for the key. No index is created for INTEGER PRIMARY KEYs. +*/ +SQLITE_PRIVATE void sqlite3AddPrimaryKey( + Parse *pParse, /* Parsing context */ + ExprList *pList, /* List of field names to be indexed */ + int onError, /* What to do with a uniqueness conflict */ + int autoInc, /* True if the AUTOINCREMENT keyword is present */ + int sortOrder /* SQLITE_SO_ASC or SQLITE_SO_DESC */ +){ + Table *pTab = pParse->pNewTable; + char *zType = 0; + int iCol = -1, i; + if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit; + if( pTab->tabFlags & TF_HasPrimaryKey ){ + sqlite3ErrorMsg(pParse, + "table \"%s\" has more than one primary key", pTab->zName); + goto primary_key_exit; + } + pTab->tabFlags |= TF_HasPrimaryKey; + if( pList==0 ){ + iCol = pTab->nCol - 1; + pTab->aCol[iCol].isPrimKey = 1; + }else{ + for(i=0; inExpr; i++){ + for(iCol=0; iColnCol; iCol++){ + if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){ + break; + } + } + if( iColnCol ){ + pTab->aCol[iCol].isPrimKey = 1; + } + } + if( pList->nExpr>1 ) iCol = -1; + } + if( iCol>=0 && iColnCol ){ + zType = pTab->aCol[iCol].zType; + } + if( zType && sqlite3StrICmp(zType, "INTEGER")==0 + && sortOrder==SQLITE_SO_ASC ){ + pTab->iPKey = iCol; + pTab->keyConf = (u8)onError; + assert( autoInc==0 || autoInc==1 ); + pTab->tabFlags |= autoInc*TF_Autoincrement; + }else if( autoInc ){ +#ifndef SQLITE_OMIT_AUTOINCREMENT + sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an " + "INTEGER PRIMARY KEY"); +#endif + }else{ + Index *p; + p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0, 0, sortOrder, 0); + if( p ){ + p->autoIndex = 2; + } + pList = 0; + } + +primary_key_exit: + sqlite3ExprListDelete(pParse->db, pList); + return; +} + +/* +** Add a new CHECK constraint to the table currently under construction. +*/ +SQLITE_PRIVATE void sqlite3AddCheckConstraint( + Parse *pParse, /* Parsing context */ + Expr *pCheckExpr /* The check expression */ +){ + sqlite3 *db = pParse->db; +#ifndef SQLITE_OMIT_CHECK + Table *pTab = pParse->pNewTable; + if( pTab && !IN_DECLARE_VTAB ){ + pTab->pCheck = sqlite3ExprAnd(db, pTab->pCheck, pCheckExpr); + }else +#endif + { + sqlite3ExprDelete(db, pCheckExpr); + } +} + +/* +** Set the collation function of the most recently parsed table column +** to the CollSeq given. +*/ +SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){ + Table *p; + int i; + char *zColl; /* Dequoted name of collation sequence */ + sqlite3 *db; + + if( (p = pParse->pNewTable)==0 ) return; + i = p->nCol-1; + db = pParse->db; + zColl = sqlite3NameFromToken(db, pToken); + if( !zColl ) return; + + if( sqlite3LocateCollSeq(pParse, zColl) ){ + Index *pIdx; + p->aCol[i].zColl = zColl; + + /* If the column is declared as " PRIMARY KEY COLLATE ", + ** then an index may have been created on this column before the + ** collation type was added. Correct this if it is the case. + */ + for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){ + assert( pIdx->nColumn==1 ); + if( pIdx->aiColumn[0]==i ){ + pIdx->azColl[0] = p->aCol[i].zColl; + } + } + }else{ + sqlite3DbFree(db, zColl); + } +} + +/* +** This function returns the collation sequence for database native text +** encoding identified by the string zName, length nName. +** +** If the requested collation sequence is not available, or not available +** in the database native encoding, the collation factory is invoked to +** request it. If the collation factory does not supply such a sequence, +** and the sequence is available in another text encoding, then that is +** returned instead. +** +** If no versions of the requested collations sequence are available, or +** another error occurs, NULL is returned and an error message written into +** pParse. +** +** This routine is a wrapper around sqlite3FindCollSeq(). This routine +** invokes the collation factory if the named collation cannot be found +** and generates an error message. +** +** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq() +*/ +SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){ + sqlite3 *db = pParse->db; + u8 enc = ENC(db); + u8 initbusy = db->init.busy; + CollSeq *pColl; + + pColl = sqlite3FindCollSeq(db, enc, zName, initbusy); + if( !initbusy && (!pColl || !pColl->xCmp) ){ + pColl = sqlite3GetCollSeq(db, enc, pColl, zName); + if( !pColl ){ + sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName); + } + } + + return pColl; +} + + +/* +** Generate code that will increment the schema cookie. +** +** The schema cookie is used to determine when the schema for the +** database changes. After each schema change, the cookie value +** changes. When a process first reads the schema it records the +** cookie. Thereafter, whenever it goes to access the database, +** it checks the cookie to make sure the schema has not changed +** since it was last read. +** +** This plan is not completely bullet-proof. It is possible for +** the schema to change multiple times and for the cookie to be +** set back to prior value. But schema changes are infrequent +** and the probability of hitting the same cookie value is only +** 1 chance in 2^32. So we're safe enough. +*/ +SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){ + int r1 = sqlite3GetTempReg(pParse); + sqlite3 *db = pParse->db; + Vdbe *v = pParse->pVdbe; + sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1); + sqlite3ReleaseTempReg(pParse, r1); +} + +/* +** Measure the number of characters needed to output the given +** identifier. The number returned includes any quotes used +** but does not include the null terminator. +** +** The estimate is conservative. It might be larger that what is +** really needed. +*/ +static int identLength(const char *z){ + int n; + for(n=0; *z; n++, z++){ + if( *z=='"' ){ n++; } + } + return n + 2; +} + +/* +** The first parameter is a pointer to an output buffer. The second +** parameter is a pointer to an integer that contains the offset at +** which to write into the output buffer. This function copies the +** nul-terminated string pointed to by the third parameter, zSignedIdent, +** to the specified offset in the buffer and updates *pIdx to refer +** to the first byte after the last byte written before returning. +** +** If the string zSignedIdent consists entirely of alpha-numeric +** characters, does not begin with a digit and is not an SQL keyword, +** then it is copied to the output buffer exactly as it is. Otherwise, +** it is quoted using double-quotes. +*/ +static void identPut(char *z, int *pIdx, char *zSignedIdent){ + unsigned char *zIdent = (unsigned char*)zSignedIdent; + int i, j, needQuote; + i = *pIdx; + + for(j=0; zIdent[j]; j++){ + if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break; + } + needQuote = sqlite3Isdigit(zIdent[0]) || sqlite3KeywordCode(zIdent, j)!=TK_ID; + if( !needQuote ){ + needQuote = zIdent[j]; + } + + if( needQuote ) z[i++] = '"'; + for(j=0; zIdent[j]; j++){ + z[i++] = zIdent[j]; + if( zIdent[j]=='"' ) z[i++] = '"'; + } + if( needQuote ) z[i++] = '"'; + z[i] = 0; + *pIdx = i; +} + +/* +** Generate a CREATE TABLE statement appropriate for the given +** table. Memory to hold the text of the statement is obtained +** from sqliteMalloc() and must be freed by the calling function. +*/ +static char *createTableStmt(sqlite3 *db, Table *p){ + int i, k, n; + char *zStmt; + char *zSep, *zSep2, *zEnd; + Column *pCol; + n = 0; + for(pCol = p->aCol, i=0; inCol; i++, pCol++){ + n += identLength(pCol->zName) + 5; + } + n += identLength(p->zName); + if( n<50 ){ + zSep = ""; + zSep2 = ","; + zEnd = ")"; + }else{ + zSep = "\n "; + zSep2 = ",\n "; + zEnd = "\n)"; + } + n += 35 + 6*p->nCol; + zStmt = sqlite3DbMallocRaw(0, n); + if( zStmt==0 ){ + db->mallocFailed = 1; + return 0; + } + sqlite3_snprintf(n, zStmt, "CREATE TABLE "); + k = sqlite3Strlen30(zStmt); + identPut(zStmt, &k, p->zName); + zStmt[k++] = '('; + for(pCol=p->aCol, i=0; inCol; i++, pCol++){ + static const char * const azType[] = { + /* SQLITE_AFF_TEXT */ " TEXT", + /* SQLITE_AFF_NONE */ "", + /* SQLITE_AFF_NUMERIC */ " NUM", + /* SQLITE_AFF_INTEGER */ " INT", + /* SQLITE_AFF_REAL */ " REAL" + }; + int len; + const char *zType; + + sqlite3_snprintf(n-k, &zStmt[k], zSep); + k += sqlite3Strlen30(&zStmt[k]); + zSep = zSep2; + identPut(zStmt, &k, pCol->zName); + assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 ); + assert( pCol->affinity-SQLITE_AFF_TEXT < sizeof(azType)/sizeof(azType[0]) ); + testcase( pCol->affinity==SQLITE_AFF_TEXT ); + testcase( pCol->affinity==SQLITE_AFF_NONE ); + testcase( pCol->affinity==SQLITE_AFF_NUMERIC ); + testcase( pCol->affinity==SQLITE_AFF_INTEGER ); + testcase( pCol->affinity==SQLITE_AFF_REAL ); + + zType = azType[pCol->affinity - SQLITE_AFF_TEXT]; + len = sqlite3Strlen30(zType); + assert( pCol->affinity==SQLITE_AFF_NONE + || pCol->affinity==sqlite3AffinityType(zType) ); + memcpy(&zStmt[k], zType, len); + k += len; + assert( k<=n ); + } + sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd); + return zStmt; +} + +/* +** This routine is called to report the final ")" that terminates +** a CREATE TABLE statement. +** +** The table structure that other action routines have been building +** is added to the internal hash tables, assuming no errors have +** occurred. +** +** An entry for the table is made in the master table on disk, unless +** this is a temporary table or db->init.busy==1. When db->init.busy==1 +** it means we are reading the sqlite_master table because we just +** connected to the database or because the sqlite_master table has +** recently changed, so the entry for this table already exists in +** the sqlite_master table. We do not want to create it again. +** +** If the pSelect argument is not NULL, it means that this routine +** was called to create a table generated from a +** "CREATE TABLE ... AS SELECT ..." statement. The column names of +** the new table will match the result set of the SELECT. +*/ +SQLITE_PRIVATE void sqlite3EndTable( + Parse *pParse, /* Parse context */ + Token *pCons, /* The ',' token after the last column defn. */ + Token *pEnd, /* The final ')' token in the CREATE TABLE */ + Select *pSelect /* Select from a "CREATE ... AS SELECT" */ +){ + Table *p; + sqlite3 *db = pParse->db; + int iDb; + + if( (pEnd==0 && pSelect==0) || db->mallocFailed ){ + return; + } + p = pParse->pNewTable; + if( p==0 ) return; + + assert( !db->init.busy || !pSelect ); + + iDb = sqlite3SchemaToIndex(db, p->pSchema); + +#ifndef SQLITE_OMIT_CHECK + /* Resolve names in all CHECK constraint expressions. + */ + if( p->pCheck ){ + SrcList sSrc; /* Fake SrcList for pParse->pNewTable */ + NameContext sNC; /* Name context for pParse->pNewTable */ + + memset(&sNC, 0, sizeof(sNC)); + memset(&sSrc, 0, sizeof(sSrc)); + sSrc.nSrc = 1; + sSrc.a[0].zName = p->zName; + sSrc.a[0].pTab = p; + sSrc.a[0].iCursor = -1; + sNC.pParse = pParse; + sNC.pSrcList = &sSrc; + sNC.isCheck = 1; + if( sqlite3ResolveExprNames(&sNC, p->pCheck) ){ + return; + } + } +#endif /* !defined(SQLITE_OMIT_CHECK) */ + + /* If the db->init.busy is 1 it means we are reading the SQL off the + ** "sqlite_master" or "sqlite_temp_master" table on the disk. + ** So do not write to the disk again. Extract the root page number + ** for the table from the db->init.newTnum field. (The page number + ** should have been put there by the sqliteOpenCb routine.) + */ + if( db->init.busy ){ + p->tnum = db->init.newTnum; + } + + /* If not initializing, then create a record for the new table + ** in the SQLITE_MASTER table of the database. + ** + ** If this is a TEMPORARY table, write the entry into the auxiliary + ** file instead of into the main database file. + */ + if( !db->init.busy ){ + int n; + Vdbe *v; + char *zType; /* "view" or "table" */ + char *zType2; /* "VIEW" or "TABLE" */ + char *zStmt; /* Text of the CREATE TABLE or CREATE VIEW statement */ + + v = sqlite3GetVdbe(pParse); + if( NEVER(v==0) ) return; + + sqlite3VdbeAddOp1(v, OP_Close, 0); + + /* + ** Initialize zType for the new view or table. + */ + if( p->pSelect==0 ){ + /* A regular table */ + zType = "table"; + zType2 = "TABLE"; +#ifndef SQLITE_OMIT_VIEW + }else{ + /* A view */ + zType = "view"; + zType2 = "VIEW"; +#endif + } + + /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT + ** statement to populate the new table. The root-page number for the + ** new table is in register pParse->regRoot. + ** + ** Once the SELECT has been coded by sqlite3Select(), it is in a + ** suitable state to query for the column names and types to be used + ** by the new table. + ** + ** A shared-cache write-lock is not required to write to the new table, + ** as a schema-lock must have already been obtained to create it. Since + ** a schema-lock excludes all other database users, the write-lock would + ** be redundant. + */ + if( pSelect ){ + SelectDest dest; + Table *pSelTab; + + assert(pParse->nTab==1); + sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb); + sqlite3VdbeChangeP5(v, 1); + pParse->nTab = 2; + sqlite3SelectDestInit(&dest, SRT_Table, 1); + sqlite3Select(pParse, pSelect, &dest); + sqlite3VdbeAddOp1(v, OP_Close, 1); + if( pParse->nErr==0 ){ + pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect); + if( pSelTab==0 ) return; + assert( p->aCol==0 ); + p->nCol = pSelTab->nCol; + p->aCol = pSelTab->aCol; + pSelTab->nCol = 0; + pSelTab->aCol = 0; + sqlite3DeleteTable(db, pSelTab); + } + } + + /* Compute the complete text of the CREATE statement */ + if( pSelect ){ + zStmt = createTableStmt(db, p); + }else{ + n = (int)(pEnd->z - pParse->sNameToken.z) + 1; + zStmt = sqlite3MPrintf(db, + "CREATE %s %.*s", zType2, n, pParse->sNameToken.z + ); + } + + /* A slot for the record has already been allocated in the + ** SQLITE_MASTER table. We just need to update that slot with all + ** the information we've collected. + */ + sqlite3NestedParse(pParse, + "UPDATE %Q.%s " + "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q " + "WHERE rowid=#%d", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), + zType, + p->zName, + p->zName, + pParse->regRoot, + zStmt, + pParse->regRowid + ); + sqlite3DbFree(db, zStmt); + sqlite3ChangeCookie(pParse, iDb); + +#ifndef SQLITE_OMIT_AUTOINCREMENT + /* Check to see if we need to create an sqlite_sequence table for + ** keeping track of autoincrement keys. + */ + if( p->tabFlags & TF_Autoincrement ){ + Db *pDb = &db->aDb[iDb]; + if( pDb->pSchema->pSeqTab==0 ){ + sqlite3NestedParse(pParse, + "CREATE TABLE %Q.sqlite_sequence(name,seq)", + pDb->zName + ); + } + } +#endif + + /* Reparse everything to update our internal data structures */ + sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, + sqlite3MPrintf(db, "tbl_name='%q'",p->zName), P4_DYNAMIC); + } + + + /* Add the table to the in-memory representation of the database. + */ + if( db->init.busy ){ + Table *pOld; + Schema *pSchema = p->pSchema; + pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, + sqlite3Strlen30(p->zName),p); + if( pOld ){ + assert( p==pOld ); /* Malloc must have failed inside HashInsert() */ + db->mallocFailed = 1; + return; + } + pParse->pNewTable = 0; + db->nTable++; + db->flags |= SQLITE_InternChanges; + +#ifndef SQLITE_OMIT_ALTERTABLE + if( !p->pSelect ){ + const char *zName = (const char *)pParse->sNameToken.z; + int nName; + assert( !pSelect && pCons && pEnd ); + if( pCons->z==0 ){ + pCons = pEnd; + } + nName = (int)((const char *)pCons->z - zName); + p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName); + } +#endif + } +} + +#ifndef SQLITE_OMIT_VIEW +/* +** The parser calls this routine in order to create a new VIEW +*/ +SQLITE_PRIVATE void sqlite3CreateView( + Parse *pParse, /* The parsing context */ + Token *pBegin, /* The CREATE token that begins the statement */ + Token *pName1, /* The token that holds the name of the view */ + Token *pName2, /* The token that holds the name of the view */ + Select *pSelect, /* A SELECT statement that will become the new view */ + int isTemp, /* TRUE for a TEMPORARY view */ + int noErr /* Suppress error messages if VIEW already exists */ +){ + Table *p; + int n; + const char *z; + Token sEnd; + DbFixer sFix; + Token *pName; + int iDb; + sqlite3 *db = pParse->db; + + if( pParse->nVar>0 ){ + sqlite3ErrorMsg(pParse, "parameters are not allowed in views"); + sqlite3SelectDelete(db, pSelect); + return; + } + sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr); + p = pParse->pNewTable; + if( p==0 || pParse->nErr ){ + sqlite3SelectDelete(db, pSelect); + return; + } + sqlite3TwoPartName(pParse, pName1, pName2, &pName); + iDb = sqlite3SchemaToIndex(db, p->pSchema); + if( sqlite3FixInit(&sFix, pParse, iDb, "view", pName) + && sqlite3FixSelect(&sFix, pSelect) + ){ + sqlite3SelectDelete(db, pSelect); + return; + } + + /* Make a copy of the entire SELECT statement that defines the view. + ** This will force all the Expr.token.z values to be dynamically + ** allocated rather than point to the input string - which means that + ** they will persist after the current sqlite3_exec() call returns. + */ + p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); + sqlite3SelectDelete(db, pSelect); + if( db->mallocFailed ){ + return; + } + if( !db->init.busy ){ + sqlite3ViewGetColumnNames(pParse, p); + } + + /* Locate the end of the CREATE VIEW statement. Make sEnd point to + ** the end. + */ + sEnd = pParse->sLastToken; + if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){ + sEnd.z += sEnd.n; + } + sEnd.n = 0; + n = (int)(sEnd.z - pBegin->z); + z = pBegin->z; + while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; } + sEnd.z = &z[n-1]; + sEnd.n = 1; + + /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */ + sqlite3EndTable(pParse, 0, &sEnd, 0); + return; +} +#endif /* SQLITE_OMIT_VIEW */ + +#if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) +/* +** The Table structure pTable is really a VIEW. Fill in the names of +** the columns of the view in the pTable structure. Return the number +** of errors. If an error is seen leave an error message in pParse->zErrMsg. +*/ +SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){ + Table *pSelTab; /* A fake table from which we get the result set */ + Select *pSel; /* Copy of the SELECT that implements the view */ + int nErr = 0; /* Number of errors encountered */ + int n; /* Temporarily holds the number of cursors assigned */ + sqlite3 *db = pParse->db; /* Database connection for malloc errors */ + int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); + + assert( pTable ); + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( sqlite3VtabCallConnect(pParse, pTable) ){ + return SQLITE_ERROR; + } + if( IsVirtual(pTable) ) return 0; +#endif + +#ifndef SQLITE_OMIT_VIEW + /* A positive nCol means the columns names for this view are + ** already known. + */ + if( pTable->nCol>0 ) return 0; + + /* A negative nCol is a special marker meaning that we are currently + ** trying to compute the column names. If we enter this routine with + ** a negative nCol, it means two or more views form a loop, like this: + ** + ** CREATE VIEW one AS SELECT * FROM two; + ** CREATE VIEW two AS SELECT * FROM one; + ** + ** Actually, the error above is now caught prior to reaching this point. + ** But the following test is still important as it does come up + ** in the following: + ** + ** CREATE TABLE main.ex1(a); + ** CREATE TEMP VIEW ex1 AS SELECT a FROM ex1; + ** SELECT * FROM temp.ex1; + */ + if( pTable->nCol<0 ){ + sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName); + return 1; + } + assert( pTable->nCol>=0 ); + + /* If we get this far, it means we need to compute the table names. + ** Note that the call to sqlite3ResultSetOfSelect() will expand any + ** "*" elements in the results set of the view and will assign cursors + ** to the elements of the FROM clause. But we do not want these changes + ** to be permanent. So the computation is done on a copy of the SELECT + ** statement that defines the view. + */ + assert( pTable->pSelect ); + pSel = sqlite3SelectDup(db, pTable->pSelect, 0); + if( pSel ){ + u8 enableLookaside = db->lookaside.bEnabled; + n = pParse->nTab; + sqlite3SrcListAssignCursors(pParse, pSel->pSrc); + pTable->nCol = -1; + db->lookaside.bEnabled = 0; +#ifndef SQLITE_OMIT_AUTHORIZATION + xAuth = db->xAuth; + db->xAuth = 0; + pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); + db->xAuth = xAuth; +#else + pSelTab = sqlite3ResultSetOfSelect(pParse, pSel); +#endif + db->lookaside.bEnabled = enableLookaside; + pParse->nTab = n; + if( pSelTab ){ + assert( pTable->aCol==0 ); + pTable->nCol = pSelTab->nCol; + pTable->aCol = pSelTab->aCol; + pSelTab->nCol = 0; + pSelTab->aCol = 0; + sqlite3DeleteTable(db, pSelTab); + pTable->pSchema->flags |= DB_UnresetViews; + }else{ + pTable->nCol = 0; + nErr++; + } + sqlite3SelectDelete(db, pSel); + } else { + nErr++; + } +#endif /* SQLITE_OMIT_VIEW */ + return nErr; +} +#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */ + +#ifndef SQLITE_OMIT_VIEW +/* +** Clear the column names from every VIEW in database idx. +*/ +static void sqliteViewResetAll(sqlite3 *db, int idx){ + HashElem *i; + if( !DbHasProperty(db, idx, DB_UnresetViews) ) return; + for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){ + Table *pTab = sqliteHashData(i); + if( pTab->pSelect ){ + sqliteDeleteColumnNames(db, pTab); + pTab->aCol = 0; + pTab->nCol = 0; + } + } + DbClearProperty(db, idx, DB_UnresetViews); +} +#else +# define sqliteViewResetAll(A,B) +#endif /* SQLITE_OMIT_VIEW */ + +/* +** This function is called by the VDBE to adjust the internal schema +** used by SQLite when the btree layer moves a table root page. The +** root-page of a table or index in database iDb has changed from iFrom +** to iTo. +** +** Ticket #1728: The symbol table might still contain information +** on tables and/or indices that are the process of being deleted. +** If you are unlucky, one of those deleted indices or tables might +** have the same rootpage number as the real table or index that is +** being moved. So we cannot stop searching after the first match +** because the first match might be for one of the deleted indices +** or tables and not the table/index that is actually being moved. +** We must continue looping until all tables and indices with +** rootpage==iFrom have been converted to have a rootpage of iTo +** in order to be certain that we got the right one. +*/ +#ifndef SQLITE_OMIT_AUTOVACUUM +SQLITE_PRIVATE void sqlite3RootPageMoved(Db *pDb, int iFrom, int iTo){ + HashElem *pElem; + Hash *pHash; + + pHash = &pDb->pSchema->tblHash; + for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ + Table *pTab = sqliteHashData(pElem); + if( pTab->tnum==iFrom ){ + pTab->tnum = iTo; + } + } + pHash = &pDb->pSchema->idxHash; + for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){ + Index *pIdx = sqliteHashData(pElem); + if( pIdx->tnum==iFrom ){ + pIdx->tnum = iTo; + } + } +} +#endif + +/* +** Write code to erase the table with root-page iTable from database iDb. +** Also write code to modify the sqlite_master table and internal schema +** if a root-page of another table is moved by the btree-layer whilst +** erasing iTable (this can happen with an auto-vacuum database). +*/ +static void destroyRootPage(Parse *pParse, int iTable, int iDb){ + Vdbe *v = sqlite3GetVdbe(pParse); + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb); + sqlite3MayAbort(pParse); +#ifndef SQLITE_OMIT_AUTOVACUUM + /* OP_Destroy stores an in integer r1. If this integer + ** is non-zero, then it is the root page number of a table moved to + ** location iTable. The following code modifies the sqlite_master table to + ** reflect this. + ** + ** The "#NNN" in the SQL is a special constant that means whatever value + ** is in register NNN. See grammar rules associated with the TK_REGISTER + ** token for additional information. + */ + sqlite3NestedParse(pParse, + "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d", + pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1); +#endif + sqlite3ReleaseTempReg(pParse, r1); +} + +/* +** Write VDBE code to erase table pTab and all associated indices on disk. +** Code to update the sqlite_master tables and internal schema definitions +** in case a root-page belonging to another table is moved by the btree layer +** is also added (this can happen with an auto-vacuum database). +*/ +static void destroyTable(Parse *pParse, Table *pTab){ +#ifdef SQLITE_OMIT_AUTOVACUUM + Index *pIdx; + int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + destroyRootPage(pParse, pTab->tnum, iDb); + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + destroyRootPage(pParse, pIdx->tnum, iDb); + } +#else + /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM + ** is not defined), then it is important to call OP_Destroy on the + ** table and index root-pages in order, starting with the numerically + ** largest root-page number. This guarantees that none of the root-pages + ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the + ** following were coded: + ** + ** OP_Destroy 4 0 + ** ... + ** OP_Destroy 5 0 + ** + ** and root page 5 happened to be the largest root-page number in the + ** database, then root page 5 would be moved to page 4 by the + ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit + ** a free-list page. + */ + int iTab = pTab->tnum; + int iDestroyed = 0; + + while( 1 ){ + Index *pIdx; + int iLargest = 0; + + if( iDestroyed==0 || iTabpIndex; pIdx; pIdx=pIdx->pNext){ + int iIdx = pIdx->tnum; + assert( pIdx->pSchema==pTab->pSchema ); + if( (iDestroyed==0 || (iIdxiLargest ){ + iLargest = iIdx; + } + } + if( iLargest==0 ){ + return; + }else{ + int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + destroyRootPage(pParse, iLargest, iDb); + iDestroyed = iLargest; + } + } +#endif +} + +/* +** This routine is called to do the work of a DROP TABLE statement. +** pName is the name of the table to be dropped. +*/ +SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){ + Table *pTab; + Vdbe *v; + sqlite3 *db = pParse->db; + int iDb; + + if( db->mallocFailed ){ + goto exit_drop_table; + } + assert( pParse->nErr==0 ); + assert( pName->nSrc==1 ); + if( noErr ) db->suppressErr++; + pTab = sqlite3LocateTable(pParse, isView, + pName->a[0].zName, pName->a[0].zDatabase); + if( noErr ) db->suppressErr--; + + if( pTab==0 ){ + goto exit_drop_table; + } + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iDb>=0 && iDbnDb ); + + /* If pTab is a virtual table, call ViewGetColumnNames() to ensure + ** it is initialized. + */ + if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){ + goto exit_drop_table; + } +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int code; + const char *zTab = SCHEMA_TABLE(iDb); + const char *zDb = db->aDb[iDb].zName; + const char *zArg2 = 0; + if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){ + goto exit_drop_table; + } + if( isView ){ + if( !OMIT_TEMPDB && iDb==1 ){ + code = SQLITE_DROP_TEMP_VIEW; + }else{ + code = SQLITE_DROP_VIEW; + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + }else if( IsVirtual(pTab) ){ + code = SQLITE_DROP_VTABLE; + zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName; +#endif + }else{ + if( !OMIT_TEMPDB && iDb==1 ){ + code = SQLITE_DROP_TEMP_TABLE; + }else{ + code = SQLITE_DROP_TABLE; + } + } + if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){ + goto exit_drop_table; + } + if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){ + goto exit_drop_table; + } + } +#endif + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ + sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName); + goto exit_drop_table; + } + +#ifndef SQLITE_OMIT_VIEW + /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used + ** on a table. + */ + if( isView && pTab->pSelect==0 ){ + sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName); + goto exit_drop_table; + } + if( !isView && pTab->pSelect ){ + sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName); + goto exit_drop_table; + } +#endif + + /* Generate code to remove the table from the master table + ** on disk. + */ + v = sqlite3GetVdbe(pParse); + if( v ){ + Trigger *pTrigger; + Db *pDb = &db->aDb[iDb]; + sqlite3BeginWriteOperation(pParse, 1, iDb); + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + sqlite3VdbeAddOp0(v, OP_VBegin); + } +#endif + sqlite3FkDropTable(pParse, pName, pTab); + + /* Drop all triggers associated with the table being dropped. Code + ** is generated to remove entries from sqlite_master and/or + ** sqlite_temp_master if required. + */ + pTrigger = sqlite3TriggerList(pParse, pTab); + while( pTrigger ){ + assert( pTrigger->pSchema==pTab->pSchema || + pTrigger->pSchema==db->aDb[1].pSchema ); + sqlite3DropTriggerPtr(pParse, pTrigger); + pTrigger = pTrigger->pNext; + } + +#ifndef SQLITE_OMIT_AUTOINCREMENT + /* Remove any entries of the sqlite_sequence table associated with + ** the table being dropped. This is done before the table is dropped + ** at the btree level, in case the sqlite_sequence table needs to + ** move as a result of the drop (can happen in auto-vacuum mode). + */ + if( pTab->tabFlags & TF_Autoincrement ){ + sqlite3NestedParse(pParse, + "DELETE FROM %s.sqlite_sequence WHERE name=%Q", + pDb->zName, pTab->zName + ); + } +#endif + + /* Drop all SQLITE_MASTER table and index entries that refer to the + ** table. The program name loops through the master table and deletes + ** every row that refers to a table of the same name as the one being + ** dropped. Triggers are handled seperately because a trigger can be + ** created in the temp database that refers to a table in another + ** database. + */ + sqlite3NestedParse(pParse, + "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'", + pDb->zName, SCHEMA_TABLE(iDb), pTab->zName); + + /* Drop any statistics from the sqlite_stat1 table, if it exists */ + if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){ + sqlite3NestedParse(pParse, + "DELETE FROM %Q.sqlite_stat1 WHERE tbl=%Q", pDb->zName, pTab->zName + ); + } + + if( !isView && !IsVirtual(pTab) ){ + destroyTable(pParse, pTab); + } + + /* Remove the table entry from SQLite's internal schema and modify + ** the schema cookie. + */ + if( IsVirtual(pTab) ){ + sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0); + } + sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0); + sqlite3ChangeCookie(pParse, iDb); + } + sqliteViewResetAll(db, iDb); + +exit_drop_table: + sqlite3SrcListDelete(db, pName); +} + +/* +** This routine is called to create a new foreign key on the table +** currently under construction. pFromCol determines which columns +** in the current table point to the foreign key. If pFromCol==0 then +** connect the key to the last column inserted. pTo is the name of +** the table referred to. pToCol is a list of tables in the other +** pTo table that the foreign key points to. flags contains all +** information about the conflict resolution algorithms specified +** in the ON DELETE, ON UPDATE and ON INSERT clauses. +** +** An FKey structure is created and added to the table currently +** under construction in the pParse->pNewTable field. +** +** The foreign key is set for IMMEDIATE processing. A subsequent call +** to sqlite3DeferForeignKey() might change this to DEFERRED. +*/ +SQLITE_PRIVATE void sqlite3CreateForeignKey( + Parse *pParse, /* Parsing context */ + ExprList *pFromCol, /* Columns in this table that point to other table */ + Token *pTo, /* Name of the other table */ + ExprList *pToCol, /* Columns in the other table */ + int flags /* Conflict resolution algorithms. */ +){ + sqlite3 *db = pParse->db; +#ifndef SQLITE_OMIT_FOREIGN_KEY + FKey *pFKey = 0; + FKey *pNextTo; + Table *p = pParse->pNewTable; + int nByte; + int i; + int nCol; + char *z; + + assert( pTo!=0 ); + if( p==0 || IN_DECLARE_VTAB ) goto fk_end; + if( pFromCol==0 ){ + int iCol = p->nCol-1; + if( NEVER(iCol<0) ) goto fk_end; + if( pToCol && pToCol->nExpr!=1 ){ + sqlite3ErrorMsg(pParse, "foreign key on %s" + " should reference only one column of table %T", + p->aCol[iCol].zName, pTo); + goto fk_end; + } + nCol = 1; + }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){ + sqlite3ErrorMsg(pParse, + "number of columns in foreign key does not match the number of " + "columns in the referenced table"); + goto fk_end; + }else{ + nCol = pFromCol->nExpr; + } + nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1; + if( pToCol ){ + for(i=0; inExpr; i++){ + nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1; + } + } + pFKey = sqlite3DbMallocZero(db, nByte ); + if( pFKey==0 ){ + goto fk_end; + } + pFKey->pFrom = p; + pFKey->pNextFrom = p->pFKey; + z = (char*)&pFKey->aCol[nCol]; + pFKey->zTo = z; + memcpy(z, pTo->z, pTo->n); + z[pTo->n] = 0; + sqlite3Dequote(z); + z += pTo->n+1; + pFKey->nCol = nCol; + if( pFromCol==0 ){ + pFKey->aCol[0].iFrom = p->nCol-1; + }else{ + for(i=0; inCol; j++){ + if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){ + pFKey->aCol[i].iFrom = j; + break; + } + } + if( j>=p->nCol ){ + sqlite3ErrorMsg(pParse, + "unknown column \"%s\" in foreign key definition", + pFromCol->a[i].zName); + goto fk_end; + } + } + } + if( pToCol ){ + for(i=0; ia[i].zName); + pFKey->aCol[i].zCol = z; + memcpy(z, pToCol->a[i].zName, n); + z[n] = 0; + z += n+1; + } + } + pFKey->isDeferred = 0; + pFKey->aAction[0] = (u8)(flags & 0xff); /* ON DELETE action */ + pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff); /* ON UPDATE action */ + + pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash, + pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey + ); + if( pNextTo==pFKey ){ + db->mallocFailed = 1; + goto fk_end; + } + if( pNextTo ){ + assert( pNextTo->pPrevTo==0 ); + pFKey->pNextTo = pNextTo; + pNextTo->pPrevTo = pFKey; + } + + /* Link the foreign key to the table as the last step. + */ + p->pFKey = pFKey; + pFKey = 0; + +fk_end: + sqlite3DbFree(db, pFKey); +#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ + sqlite3ExprListDelete(db, pFromCol); + sqlite3ExprListDelete(db, pToCol); +} + +/* +** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED +** clause is seen as part of a foreign key definition. The isDeferred +** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE. +** The behavior of the most recently created foreign key is adjusted +** accordingly. +*/ +SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){ +#ifndef SQLITE_OMIT_FOREIGN_KEY + Table *pTab; + FKey *pFKey; + if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return; + assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */ + pFKey->isDeferred = (u8)isDeferred; +#endif +} + +/* +** Generate code that will erase and refill index *pIdx. This is +** used to initialize a newly created index or to recompute the +** content of an index in response to a REINDEX command. +** +** if memRootPage is not negative, it means that the index is newly +** created. The register specified by memRootPage contains the +** root page number of the index. If memRootPage is negative, then +** the index already exists and must be cleared before being refilled and +** the root page number of the index is taken from pIndex->tnum. +*/ +static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){ + Table *pTab = pIndex->pTable; /* The table that is indexed */ + int iTab = pParse->nTab++; /* Btree cursor used for pTab */ + int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */ + int addr1; /* Address of top of loop */ + int tnum; /* Root page of index */ + Vdbe *v; /* Generate code into this virtual machine */ + KeyInfo *pKey; /* KeyInfo for index */ + int regIdxKey; /* Registers containing the index key */ + int regRecord; /* Register holding assemblied index record */ + sqlite3 *db = pParse->db; /* The database connection */ + int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); + +#ifndef SQLITE_OMIT_AUTHORIZATION + if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0, + db->aDb[iDb].zName ) ){ + return; + } +#endif + + /* Require a write-lock on the table to perform this operation */ + sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName); + + v = sqlite3GetVdbe(pParse); + if( v==0 ) return; + if( memRootPage>=0 ){ + tnum = memRootPage; + }else{ + tnum = pIndex->tnum; + sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb); + } + pKey = sqlite3IndexKeyinfo(pParse, pIndex); + sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb, + (char *)pKey, P4_KEYINFO_HANDOFF); + if( memRootPage>=0 ){ + sqlite3VdbeChangeP5(v, 1); + } + sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); + addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); + regRecord = sqlite3GetTempReg(pParse); + regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1); + if( pIndex->onError!=OE_None ){ + const int regRowid = regIdxKey + pIndex->nColumn; + const int j2 = sqlite3VdbeCurrentAddr(v) + 2; + void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey); + + /* The registers accessed by the OP_IsUnique opcode were allocated + ** using sqlite3GetTempRange() inside of the sqlite3GenerateIndexKey() + ** call above. Just before that function was freed they were released + ** (made available to the compiler for reuse) using + ** sqlite3ReleaseTempRange(). So in some ways having the OP_IsUnique + ** opcode use the values stored within seems dangerous. However, since + ** we can be sure that no other temp registers have been allocated + ** since sqlite3ReleaseTempRange() was called, it is safe to do so. + */ + sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32); + sqlite3HaltConstraint( + pParse, OE_Abort, "indexed columns are not unique", P4_STATIC); + } + sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord); + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); + sqlite3ReleaseTempReg(pParse, regRecord); + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1); + sqlite3VdbeJumpHere(v, addr1); + sqlite3VdbeAddOp1(v, OP_Close, iTab); + sqlite3VdbeAddOp1(v, OP_Close, iIdx); +} + +/* +** Create a new index for an SQL table. pName1.pName2 is the name of the index +** and pTblList is the name of the table that is to be indexed. Both will +** be NULL for a primary key or an index that is created to satisfy a +** UNIQUE constraint. If pTable and pIndex are NULL, use pParse->pNewTable +** as the table to be indexed. pParse->pNewTable is a table that is +** currently being constructed by a CREATE TABLE statement. +** +** pList is a list of columns to be indexed. pList will be NULL if this +** is a primary key or unique-constraint on the most recent column added +** to the table currently under construction. +** +** If the index is created successfully, return a pointer to the new Index +** structure. This is used by sqlite3AddPrimaryKey() to mark the index +** as the tables primary key (Index.autoIndex==2). +*/ +SQLITE_PRIVATE Index *sqlite3CreateIndex( + Parse *pParse, /* All information about this parse */ + Token *pName1, /* First part of index name. May be NULL */ + Token *pName2, /* Second part of index name. May be NULL */ + SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */ + ExprList *pList, /* A list of columns to be indexed */ + int onError, /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ + Token *pStart, /* The CREATE token that begins this statement */ + Token *pEnd, /* The ")" that closes the CREATE INDEX statement */ + int sortOrder, /* Sort order of primary key when pList==NULL */ + int ifNotExist /* Omit error if index already exists */ +){ + Index *pRet = 0; /* Pointer to return */ + Table *pTab = 0; /* Table to be indexed */ + Index *pIndex = 0; /* The index to be created */ + char *zName = 0; /* Name of the index */ + int nName; /* Number of characters in zName */ + int i, j; + Token nullId; /* Fake token for an empty ID list */ + DbFixer sFix; /* For assigning database names to pTable */ + int sortOrderMask; /* 1 to honor DESC in index. 0 to ignore. */ + sqlite3 *db = pParse->db; + Db *pDb; /* The specific table containing the indexed database */ + int iDb; /* Index of the database that is being written */ + Token *pName = 0; /* Unqualified name of the index to create */ + struct ExprList_item *pListItem; /* For looping over pList */ + int nCol; + int nExtra = 0; + char *zExtra; + + assert( pStart==0 || pEnd!=0 ); /* pEnd must be non-NULL if pStart is */ + assert( pParse->nErr==0 ); /* Never called with prior errors */ + if( db->mallocFailed || IN_DECLARE_VTAB ){ + goto exit_create_index; + } + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + goto exit_create_index; + } + + /* + ** Find the table that is to be indexed. Return early if not found. + */ + if( pTblName!=0 ){ + + /* Use the two-part index name to determine the database + ** to search for the table. 'Fix' the table name to this db + ** before looking up the table. + */ + assert( pName1 && pName2 ); + iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); + if( iDb<0 ) goto exit_create_index; + +#ifndef SQLITE_OMIT_TEMPDB + /* If the index name was unqualified, check if the the table + ** is a temp table. If so, set the database to 1. Do not do this + ** if initialising a database schema. + */ + if( !db->init.busy ){ + pTab = sqlite3SrcListLookup(pParse, pTblName); + if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){ + iDb = 1; + } + } +#endif + + if( sqlite3FixInit(&sFix, pParse, iDb, "index", pName) && + sqlite3FixSrcList(&sFix, pTblName) + ){ + /* Because the parser constructs pTblName from a single identifier, + ** sqlite3FixSrcList can never fail. */ + assert(0); + } + pTab = sqlite3LocateTable(pParse, 0, pTblName->a[0].zName, + pTblName->a[0].zDatabase); + if( !pTab || db->mallocFailed ) goto exit_create_index; + assert( db->aDb[iDb].pSchema==pTab->pSchema ); + }else{ + assert( pName==0 ); + pTab = pParse->pNewTable; + if( !pTab ) goto exit_create_index; + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + } + pDb = &db->aDb[iDb]; + + assert( pTab!=0 ); + assert( pParse->nErr==0 ); + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 + && memcmp(&pTab->zName[7],"altertab_",9)!=0 ){ + sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName); + goto exit_create_index; + } +#ifndef SQLITE_OMIT_VIEW + if( pTab->pSelect ){ + sqlite3ErrorMsg(pParse, "views may not be indexed"); + goto exit_create_index; + } +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + sqlite3ErrorMsg(pParse, "virtual tables may not be indexed"); + goto exit_create_index; + } +#endif + + /* + ** Find the name of the index. Make sure there is not already another + ** index or table with the same name. + ** + ** Exception: If we are reading the names of permanent indices from the + ** sqlite_master table (because some other process changed the schema) and + ** one of the index names collides with the name of a temporary table or + ** index, then we will continue to process this index. + ** + ** If pName==0 it means that we are + ** dealing with a primary key or UNIQUE constraint. We have to invent our + ** own name. + */ + if( pName ){ + zName = sqlite3NameFromToken(db, pName); + if( zName==0 ) goto exit_create_index; + if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ + goto exit_create_index; + } + if( !db->init.busy ){ + if( sqlite3FindTable(db, zName, 0)!=0 ){ + sqlite3ErrorMsg(pParse, "there is already a table named %s", zName); + goto exit_create_index; + } + } + if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){ + if( !ifNotExist ){ + sqlite3ErrorMsg(pParse, "index %s already exists", zName); + } + goto exit_create_index; + } + }else{ + int n; + Index *pLoop; + for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){} + zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n); + if( zName==0 ){ + goto exit_create_index; + } + } + + /* Check for authorization to create an index. + */ +#ifndef SQLITE_OMIT_AUTHORIZATION + { + const char *zDb = pDb->zName; + if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){ + goto exit_create_index; + } + i = SQLITE_CREATE_INDEX; + if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX; + if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){ + goto exit_create_index; + } + } +#endif + + /* If pList==0, it means this routine was called to make a primary + ** key out of the last column added to the table under construction. + ** So create a fake list to simulate this. + */ + if( pList==0 ){ + nullId.z = pTab->aCol[pTab->nCol-1].zName; + nullId.n = sqlite3Strlen30((char*)nullId.z); + pList = sqlite3ExprListAppend(pParse, 0, 0); + if( pList==0 ) goto exit_create_index; + sqlite3ExprListSetName(pParse, pList, &nullId, 0); + pList->a[0].sortOrder = (u8)sortOrder; + } + + /* Figure out how many bytes of space are required to store explicitly + ** specified collation sequence names. + */ + for(i=0; inExpr; i++){ + Expr *pExpr = pList->a[i].pExpr; + if( pExpr ){ + CollSeq *pColl = pExpr->pColl; + /* Either pColl!=0 or there was an OOM failure. But if an OOM + ** failure we have quit before reaching this point. */ + if( ALWAYS(pColl) ){ + nExtra += (1 + sqlite3Strlen30(pColl->zName)); + } + } + } + + /* + ** Allocate the index structure. + */ + nName = sqlite3Strlen30(zName); + nCol = pList->nExpr; + pIndex = sqlite3DbMallocZero(db, + sizeof(Index) + /* Index structure */ + sizeof(int)*nCol + /* Index.aiColumn */ + sizeof(int)*(nCol+1) + /* Index.aiRowEst */ + sizeof(char *)*nCol + /* Index.azColl */ + sizeof(u8)*nCol + /* Index.aSortOrder */ + nName + 1 + /* Index.zName */ + nExtra /* Collation sequence names */ + ); + if( db->mallocFailed ){ + goto exit_create_index; + } + pIndex->azColl = (char**)(&pIndex[1]); + pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]); + pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]); + pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]); + pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]); + zExtra = (char *)(&pIndex->zName[nName+1]); + memcpy(pIndex->zName, zName, nName+1); + pIndex->pTable = pTab; + pIndex->nColumn = pList->nExpr; + pIndex->onError = (u8)onError; + pIndex->autoIndex = (u8)(pName==0); + pIndex->pSchema = db->aDb[iDb].pSchema; + + /* Check to see if we should honor DESC requests on index columns + */ + if( pDb->pSchema->file_format>=4 ){ + sortOrderMask = -1; /* Honor DESC */ + }else{ + sortOrderMask = 0; /* Ignore DESC */ + } + + /* Scan the names of the columns of the table to be indexed and + ** load the column indices into the Index structure. Report an error + ** if any column is not found. + ** + ** TODO: Add a test to make sure that the same column is not named + ** more than once within the same index. Only the first instance of + ** the column will ever be used by the optimizer. Note that using the + ** same column more than once cannot be an error because that would + ** break backwards compatibility - it needs to be a warning. + */ + for(i=0, pListItem=pList->a; inExpr; i++, pListItem++){ + const char *zColName = pListItem->zName; + Column *pTabCol; + int requestedSortOrder; + char *zColl; /* Collation sequence name */ + + for(j=0, pTabCol=pTab->aCol; jnCol; j++, pTabCol++){ + if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break; + } + if( j>=pTab->nCol ){ + sqlite3ErrorMsg(pParse, "table %s has no column named %s", + pTab->zName, zColName); + pParse->checkSchema = 1; + goto exit_create_index; + } + pIndex->aiColumn[i] = j; + /* Justification of the ALWAYS(pListItem->pExpr->pColl): Because of + ** the way the "idxlist" non-terminal is constructed by the parser, + ** if pListItem->pExpr is not null then either pListItem->pExpr->pColl + ** must exist or else there must have been an OOM error. But if there + ** was an OOM error, we would never reach this point. */ + if( pListItem->pExpr && ALWAYS(pListItem->pExpr->pColl) ){ + int nColl; + zColl = pListItem->pExpr->pColl->zName; + nColl = sqlite3Strlen30(zColl) + 1; + assert( nExtra>=nColl ); + memcpy(zExtra, zColl, nColl); + zColl = zExtra; + zExtra += nColl; + nExtra -= nColl; + }else{ + zColl = pTab->aCol[j].zColl; + if( !zColl ){ + zColl = db->pDfltColl->zName; + } + } + if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){ + goto exit_create_index; + } + pIndex->azColl[i] = zColl; + requestedSortOrder = pListItem->sortOrder & sortOrderMask; + pIndex->aSortOrder[i] = (u8)requestedSortOrder; + } + sqlite3DefaultRowEst(pIndex); + + if( pTab==pParse->pNewTable ){ + /* This routine has been called to create an automatic index as a + ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or + ** a PRIMARY KEY or UNIQUE clause following the column definitions. + ** i.e. one of: + ** + ** CREATE TABLE t(x PRIMARY KEY, y); + ** CREATE TABLE t(x, y, UNIQUE(x, y)); + ** + ** Either way, check to see if the table already has such an index. If + ** so, don't bother creating this one. This only applies to + ** automatically created indices. Users can do as they wish with + ** explicit indices. + ** + ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent + ** (and thus suppressing the second one) even if they have different + ** sort orders. + ** + ** If there are different collating sequences or if the columns of + ** the constraint occur in different orders, then the constraints are + ** considered distinct and both result in separate indices. + */ + Index *pIdx; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + int k; + assert( pIdx->onError!=OE_None ); + assert( pIdx->autoIndex ); + assert( pIndex->onError!=OE_None ); + + if( pIdx->nColumn!=pIndex->nColumn ) continue; + for(k=0; knColumn; k++){ + const char *z1; + const char *z2; + if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break; + z1 = pIdx->azColl[k]; + z2 = pIndex->azColl[k]; + if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break; + } + if( k==pIdx->nColumn ){ + if( pIdx->onError!=pIndex->onError ){ + /* This constraint creates the same index as a previous + ** constraint specified somewhere in the CREATE TABLE statement. + ** However the ON CONFLICT clauses are different. If both this + ** constraint and the previous equivalent constraint have explicit + ** ON CONFLICT clauses this is an error. Otherwise, use the + ** explicitly specified behaviour for the index. + */ + if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){ + sqlite3ErrorMsg(pParse, + "conflicting ON CONFLICT clauses specified", 0); + } + if( pIdx->onError==OE_Default ){ + pIdx->onError = pIndex->onError; + } + } + goto exit_create_index; + } + } + } + + /* Link the new Index structure to its table and to the other + ** in-memory database structures. + */ + if( db->init.busy ){ + Index *p; + p = sqlite3HashInsert(&pIndex->pSchema->idxHash, + pIndex->zName, sqlite3Strlen30(pIndex->zName), + pIndex); + if( p ){ + assert( p==pIndex ); /* Malloc must have failed */ + db->mallocFailed = 1; + goto exit_create_index; + } + db->flags |= SQLITE_InternChanges; + if( pTblName!=0 ){ + pIndex->tnum = db->init.newTnum; + } + } + + /* If the db->init.busy is 0 then create the index on disk. This + ** involves writing the index into the master table and filling in the + ** index with the current table contents. + ** + ** The db->init.busy is 0 when the user first enters a CREATE INDEX + ** command. db->init.busy is 1 when a database is opened and + ** CREATE INDEX statements are read out of the master table. In + ** the latter case the index already exists on disk, which is why + ** we don't want to recreate it. + ** + ** If pTblName==0 it means this index is generated as a primary key + ** or UNIQUE constraint of a CREATE TABLE statement. Since the table + ** has just been created, it contains no data and the index initialization + ** step can be skipped. + */ + else{ /* if( db->init.busy==0 ) */ + Vdbe *v; + char *zStmt; + int iMem = ++pParse->nMem; + + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto exit_create_index; + + + /* Create the rootpage for the index + */ + sqlite3BeginWriteOperation(pParse, 1, iDb); + sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem); + + /* Gather the complete text of the CREATE INDEX statement into + ** the zStmt variable + */ + if( pStart ){ + assert( pEnd!=0 ); + /* A named index with an explicit CREATE INDEX statement */ + zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s", + onError==OE_None ? "" : " UNIQUE", + pEnd->z - pName->z + 1, + pName->z); + }else{ + /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */ + /* zStmt = sqlite3MPrintf(""); */ + zStmt = 0; + } + + /* Add an entry in sqlite_master for this index + */ + sqlite3NestedParse(pParse, + "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), + pIndex->zName, + pTab->zName, + iMem, + zStmt + ); + sqlite3DbFree(db, zStmt); + + /* Fill the index with data and reparse the schema. Code an OP_Expire + ** to invalidate all pre-compiled statements. + */ + if( pTblName ){ + sqlite3RefillIndex(pParse, pIndex, iMem); + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, + sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName), + P4_DYNAMIC); + sqlite3VdbeAddOp1(v, OP_Expire, 0); + } + } + + /* When adding an index to the list of indices for a table, make + ** sure all indices labeled OE_Replace come after all those labeled + ** OE_Ignore. This is necessary for the correct constraint check + ** processing (in sqlite3GenerateConstraintChecks()) as part of + ** UPDATE and INSERT statements. + */ + if( db->init.busy || pTblName==0 ){ + if( onError!=OE_Replace || pTab->pIndex==0 + || pTab->pIndex->onError==OE_Replace){ + pIndex->pNext = pTab->pIndex; + pTab->pIndex = pIndex; + }else{ + Index *pOther = pTab->pIndex; + while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){ + pOther = pOther->pNext; + } + pIndex->pNext = pOther->pNext; + pOther->pNext = pIndex; + } + pRet = pIndex; + pIndex = 0; + } + + /* Clean up before exiting */ +exit_create_index: + if( pIndex ){ + sqlite3DbFree(db, pIndex->zColAff); + sqlite3DbFree(db, pIndex); + } + sqlite3ExprListDelete(db, pList); + sqlite3SrcListDelete(db, pTblName); + sqlite3DbFree(db, zName); + return pRet; +} + +/* +** Fill the Index.aiRowEst[] array with default information - information +** to be used when we have not run the ANALYZE command. +** +** aiRowEst[0] is suppose to contain the number of elements in the index. +** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the +** number of rows in the table that match any particular value of the +** first column of the index. aiRowEst[2] is an estimate of the number +** of rows that match any particular combiniation of the first 2 columns +** of the index. And so forth. It must always be the case that +* +** aiRowEst[N]<=aiRowEst[N-1] +** aiRowEst[N]>=1 +** +** Apart from that, we have little to go on besides intuition as to +** how aiRowEst[] should be initialized. The numbers generated here +** are based on typical values found in actual indices. +*/ +SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){ + unsigned *a = pIdx->aiRowEst; + int i; + unsigned n; + assert( a!=0 ); + a[0] = pIdx->pTable->nRowEst; + if( a[0]<10 ) a[0] = 10; + n = 10; + for(i=1; i<=pIdx->nColumn; i++){ + a[i] = n; + if( n>5 ) n--; + } + if( pIdx->onError!=OE_None ){ + a[pIdx->nColumn] = 1; + } +} + +/* +** This routine will drop an existing named index. This routine +** implements the DROP INDEX statement. +*/ +SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){ + Index *pIndex; + Vdbe *v; + sqlite3 *db = pParse->db; + int iDb; + + assert( pParse->nErr==0 ); /* Never called with prior errors */ + if( db->mallocFailed ){ + goto exit_drop_index; + } + assert( pName->nSrc==1 ); + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + goto exit_drop_index; + } + pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase); + if( pIndex==0 ){ + if( !ifExists ){ + sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0); + } + pParse->checkSchema = 1; + goto exit_drop_index; + } + if( pIndex->autoIndex ){ + sqlite3ErrorMsg(pParse, "index associated with UNIQUE " + "or PRIMARY KEY constraint cannot be dropped", 0); + goto exit_drop_index; + } + iDb = sqlite3SchemaToIndex(db, pIndex->pSchema); +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int code = SQLITE_DROP_INDEX; + Table *pTab = pIndex->pTable; + const char *zDb = db->aDb[iDb].zName; + const char *zTab = SCHEMA_TABLE(iDb); + if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ + goto exit_drop_index; + } + if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX; + if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){ + goto exit_drop_index; + } + } +#endif + + /* Generate code to remove the index and from the master table */ + v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3BeginWriteOperation(pParse, 1, iDb); + sqlite3NestedParse(pParse, + "DELETE FROM %Q.%s WHERE name=%Q AND type='index'", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), + pIndex->zName + ); + if( sqlite3FindTable(db, "sqlite_stat1", db->aDb[iDb].zName) ){ + sqlite3NestedParse(pParse, + "DELETE FROM %Q.sqlite_stat1 WHERE idx=%Q", + db->aDb[iDb].zName, pIndex->zName + ); + } + sqlite3ChangeCookie(pParse, iDb); + destroyRootPage(pParse, pIndex->tnum, iDb); + sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0); + } + +exit_drop_index: + sqlite3SrcListDelete(db, pName); +} + +/* +** pArray is a pointer to an array of objects. Each object in the +** array is szEntry bytes in size. This routine allocates a new +** object on the end of the array. +** +** *pnEntry is the number of entries already in use. *pnAlloc is +** the previously allocated size of the array. initSize is the +** suggested initial array size allocation. +** +** The index of the new entry is returned in *pIdx. +** +** This routine returns a pointer to the array of objects. This +** might be the same as the pArray parameter or it might be a different +** pointer if the array was resized. +*/ +SQLITE_PRIVATE void *sqlite3ArrayAllocate( + sqlite3 *db, /* Connection to notify of malloc failures */ + void *pArray, /* Array of objects. Might be reallocated */ + int szEntry, /* Size of each object in the array */ + int initSize, /* Suggested initial allocation, in elements */ + int *pnEntry, /* Number of objects currently in use */ + int *pnAlloc, /* Current size of the allocation, in elements */ + int *pIdx /* Write the index of a new slot here */ +){ + char *z; + if( *pnEntry >= *pnAlloc ){ + void *pNew; + int newSize; + newSize = (*pnAlloc)*2 + initSize; + pNew = sqlite3DbRealloc(db, pArray, newSize*szEntry); + if( pNew==0 ){ + *pIdx = -1; + return pArray; + } + *pnAlloc = sqlite3DbMallocSize(db, pNew)/szEntry; + pArray = pNew; + } + z = (char*)pArray; + memset(&z[*pnEntry * szEntry], 0, szEntry); + *pIdx = *pnEntry; + ++*pnEntry; + return pArray; +} + +/* +** Append a new element to the given IdList. Create a new IdList if +** need be. +** +** A new IdList is returned, or NULL if malloc() fails. +*/ +SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){ + int i; + if( pList==0 ){ + pList = sqlite3DbMallocZero(db, sizeof(IdList) ); + if( pList==0 ) return 0; + pList->nAlloc = 0; + } + pList->a = sqlite3ArrayAllocate( + db, + pList->a, + sizeof(pList->a[0]), + 5, + &pList->nId, + &pList->nAlloc, + &i + ); + if( i<0 ){ + sqlite3IdListDelete(db, pList); + return 0; + } + pList->a[i].zName = sqlite3NameFromToken(db, pToken); + return pList; +} + +/* +** Delete an IdList. +*/ +SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){ + int i; + if( pList==0 ) return; + for(i=0; inId; i++){ + sqlite3DbFree(db, pList->a[i].zName); + } + sqlite3DbFree(db, pList->a); + sqlite3DbFree(db, pList); +} + +/* +** Return the index in pList of the identifier named zId. Return -1 +** if not found. +*/ +SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){ + int i; + if( pList==0 ) return -1; + for(i=0; inId; i++){ + if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i; + } + return -1; +} + +/* +** Expand the space allocated for the given SrcList object by +** creating nExtra new slots beginning at iStart. iStart is zero based. +** New slots are zeroed. +** +** For example, suppose a SrcList initially contains two entries: A,B. +** To append 3 new entries onto the end, do this: +** +** sqlite3SrcListEnlarge(db, pSrclist, 3, 2); +** +** After the call above it would contain: A, B, nil, nil, nil. +** If the iStart argument had been 1 instead of 2, then the result +** would have been: A, nil, nil, nil, B. To prepend the new slots, +** the iStart value would be 0. The result then would +** be: nil, nil, nil, A, B. +** +** If a memory allocation fails the SrcList is unchanged. The +** db->mallocFailed flag will be set to true. +*/ +SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge( + sqlite3 *db, /* Database connection to notify of OOM errors */ + SrcList *pSrc, /* The SrcList to be enlarged */ + int nExtra, /* Number of new slots to add to pSrc->a[] */ + int iStart /* Index in pSrc->a[] of first new slot */ +){ + int i; + + /* Sanity checking on calling parameters */ + assert( iStart>=0 ); + assert( nExtra>=1 ); + assert( pSrc!=0 ); + assert( iStart<=pSrc->nSrc ); + + /* Allocate additional space if needed */ + if( pSrc->nSrc+nExtra>pSrc->nAlloc ){ + SrcList *pNew; + int nAlloc = pSrc->nSrc+nExtra; + int nGot; + pNew = sqlite3DbRealloc(db, pSrc, + sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) ); + if( pNew==0 ){ + assert( db->mallocFailed ); + return pSrc; + } + pSrc = pNew; + nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1; + pSrc->nAlloc = (u16)nGot; + } + + /* Move existing slots that come after the newly inserted slots + ** out of the way */ + for(i=pSrc->nSrc-1; i>=iStart; i--){ + pSrc->a[i+nExtra] = pSrc->a[i]; + } + pSrc->nSrc += (i16)nExtra; + + /* Zero the newly allocated slots */ + memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra); + for(i=iStart; ia[i].iCursor = -1; + } + + /* Return a pointer to the enlarged SrcList */ + return pSrc; +} + + +/* +** Append a new table name to the given SrcList. Create a new SrcList if +** need be. A new entry is created in the SrcList even if pTable is NULL. +** +** A SrcList is returned, or NULL if there is an OOM error. The returned +** SrcList might be the same as the SrcList that was input or it might be +** a new one. If an OOM error does occurs, then the prior value of pList +** that is input to this routine is automatically freed. +** +** If pDatabase is not null, it means that the table has an optional +** database name prefix. Like this: "database.table". The pDatabase +** points to the table name and the pTable points to the database name. +** The SrcList.a[].zName field is filled with the table name which might +** come from pTable (if pDatabase is NULL) or from pDatabase. +** SrcList.a[].zDatabase is filled with the database name from pTable, +** or with NULL if no database is specified. +** +** In other words, if call like this: +** +** sqlite3SrcListAppend(D,A,B,0); +** +** Then B is a table name and the database name is unspecified. If called +** like this: +** +** sqlite3SrcListAppend(D,A,B,C); +** +** Then C is the table name and B is the database name. If C is defined +** then so is B. In other words, we never have a case where: +** +** sqlite3SrcListAppend(D,A,0,C); +** +** Both pTable and pDatabase are assumed to be quoted. They are dequoted +** before being added to the SrcList. +*/ +SQLITE_PRIVATE SrcList *sqlite3SrcListAppend( + sqlite3 *db, /* Connection to notify of malloc failures */ + SrcList *pList, /* Append to this SrcList. NULL creates a new SrcList */ + Token *pTable, /* Table to append */ + Token *pDatabase /* Database of the table */ +){ + struct SrcList_item *pItem; + assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */ + if( pList==0 ){ + pList = sqlite3DbMallocZero(db, sizeof(SrcList) ); + if( pList==0 ) return 0; + pList->nAlloc = 1; + } + pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc); + if( db->mallocFailed ){ + sqlite3SrcListDelete(db, pList); + return 0; + } + pItem = &pList->a[pList->nSrc-1]; + if( pDatabase && pDatabase->z==0 ){ + pDatabase = 0; + } + if( pDatabase ){ + Token *pTemp = pDatabase; + pDatabase = pTable; + pTable = pTemp; + } + pItem->zName = sqlite3NameFromToken(db, pTable); + pItem->zDatabase = sqlite3NameFromToken(db, pDatabase); + return pList; +} + +/* +** Assign VdbeCursor index numbers to all tables in a SrcList +*/ +SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){ + int i; + struct SrcList_item *pItem; + assert(pList || pParse->db->mallocFailed ); + if( pList ){ + for(i=0, pItem=pList->a; inSrc; i++, pItem++){ + if( pItem->iCursor>=0 ) break; + pItem->iCursor = pParse->nTab++; + if( pItem->pSelect ){ + sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc); + } + } + } +} + +/* +** Delete an entire SrcList including all its substructure. +*/ +SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){ + int i; + struct SrcList_item *pItem; + if( pList==0 ) return; + for(pItem=pList->a, i=0; inSrc; i++, pItem++){ + sqlite3DbFree(db, pItem->zDatabase); + sqlite3DbFree(db, pItem->zName); + sqlite3DbFree(db, pItem->zAlias); + sqlite3DbFree(db, pItem->zIndex); + sqlite3DeleteTable(db, pItem->pTab); + sqlite3SelectDelete(db, pItem->pSelect); + sqlite3ExprDelete(db, pItem->pOn); + sqlite3IdListDelete(db, pItem->pUsing); + } + sqlite3DbFree(db, pList); +} + +/* +** This routine is called by the parser to add a new term to the +** end of a growing FROM clause. The "p" parameter is the part of +** the FROM clause that has already been constructed. "p" is NULL +** if this is the first term of the FROM clause. pTable and pDatabase +** are the name of the table and database named in the FROM clause term. +** pDatabase is NULL if the database name qualifier is missing - the +** usual case. If the term has a alias, then pAlias points to the +** alias token. If the term is a subquery, then pSubquery is the +** SELECT statement that the subquery encodes. The pTable and +** pDatabase parameters are NULL for subqueries. The pOn and pUsing +** parameters are the content of the ON and USING clauses. +** +** Return a new SrcList which encodes is the FROM with the new +** term added. +*/ +SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm( + Parse *pParse, /* Parsing context */ + SrcList *p, /* The left part of the FROM clause already seen */ + Token *pTable, /* Name of the table to add to the FROM clause */ + Token *pDatabase, /* Name of the database containing pTable */ + Token *pAlias, /* The right-hand side of the AS subexpression */ + Select *pSubquery, /* A subquery used in place of a table name */ + Expr *pOn, /* The ON clause of a join */ + IdList *pUsing /* The USING clause of a join */ +){ + struct SrcList_item *pItem; + sqlite3 *db = pParse->db; + if( !p && (pOn || pUsing) ){ + sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s", + (pOn ? "ON" : "USING") + ); + goto append_from_error; + } + p = sqlite3SrcListAppend(db, p, pTable, pDatabase); + if( p==0 || NEVER(p->nSrc==0) ){ + goto append_from_error; + } + pItem = &p->a[p->nSrc-1]; + assert( pAlias!=0 ); + if( pAlias->n ){ + pItem->zAlias = sqlite3NameFromToken(db, pAlias); + } + pItem->pSelect = pSubquery; + pItem->pOn = pOn; + pItem->pUsing = pUsing; + return p; + + append_from_error: + assert( p==0 ); + sqlite3ExprDelete(db, pOn); + sqlite3IdListDelete(db, pUsing); + sqlite3SelectDelete(db, pSubquery); + return 0; +} + +/* +** Add an INDEXED BY or NOT INDEXED clause to the most recently added +** element of the source-list passed as the second argument. +*/ +SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){ + assert( pIndexedBy!=0 ); + if( p && ALWAYS(p->nSrc>0) ){ + struct SrcList_item *pItem = &p->a[p->nSrc-1]; + assert( pItem->notIndexed==0 && pItem->zIndex==0 ); + if( pIndexedBy->n==1 && !pIndexedBy->z ){ + /* A "NOT INDEXED" clause was supplied. See parse.y + ** construct "indexed_opt" for details. */ + pItem->notIndexed = 1; + }else{ + pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy); + } + } +} + +/* +** When building up a FROM clause in the parser, the join operator +** is initially attached to the left operand. But the code generator +** expects the join operator to be on the right operand. This routine +** Shifts all join operators from left to right for an entire FROM +** clause. +** +** Example: Suppose the join is like this: +** +** A natural cross join B +** +** The operator is "natural cross join". The A and B operands are stored +** in p->a[0] and p->a[1], respectively. The parser initially stores the +** operator with A. This routine shifts that operator over to B. +*/ +SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){ + if( p ){ + int i; + for(i=p->nSrc-1; i>0; i--){ + p->a[i].jointype = p->a[i-1].jointype; + } + p->a[0].jointype = 0; + } +} + +/* +** Begin a transaction +*/ +SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){ + sqlite3 *db; + Vdbe *v; + int i; + + assert( pParse!=0 ); + db = pParse->db; + assert( db!=0 ); +/* if( db->aDb[0].pBt==0 ) return; */ + if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){ + return; + } + v = sqlite3GetVdbe(pParse); + if( !v ) return; + if( type!=TK_DEFERRED ){ + for(i=0; inDb; i++){ + sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1); + sqlite3VdbeUsesBtree(v, i); + } + } + sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0); +} + +/* +** Commit a transaction +*/ +SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){ + sqlite3 *db; + Vdbe *v; + + assert( pParse!=0 ); + db = pParse->db; + assert( db!=0 ); +/* if( db->aDb[0].pBt==0 ) return; */ + if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){ + return; + } + v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0); + } +} + +/* +** Rollback a transaction +*/ +SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){ + sqlite3 *db; + Vdbe *v; + + assert( pParse!=0 ); + db = pParse->db; + assert( db!=0 ); +/* if( db->aDb[0].pBt==0 ) return; */ + if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){ + return; + } + v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1); + } +} + +/* +** This function is called by the parser when it parses a command to create, +** release or rollback an SQL savepoint. +*/ +SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){ + char *zName = sqlite3NameFromToken(pParse->db, pName); + if( zName ){ + Vdbe *v = sqlite3GetVdbe(pParse); +#ifndef SQLITE_OMIT_AUTHORIZATION + static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" }; + assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 ); +#endif + if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){ + sqlite3DbFree(pParse->db, zName); + return; + } + sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC); + } +} + +/* +** Make sure the TEMP database is open and available for use. Return +** the number of errors. Leave any error messages in the pParse structure. +*/ +SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){ + sqlite3 *db = pParse->db; + if( db->aDb[1].pBt==0 && !pParse->explain ){ + int rc; + Btree *pBt; + static const int flags = + SQLITE_OPEN_READWRITE | + SQLITE_OPEN_CREATE | + SQLITE_OPEN_EXCLUSIVE | + SQLITE_OPEN_DELETEONCLOSE | + SQLITE_OPEN_TEMP_DB; + + rc = sqlite3BtreeOpen(0, db, &pBt, 0, flags); + if( rc!=SQLITE_OK ){ + sqlite3ErrorMsg(pParse, "unable to open a temporary database " + "file for storing temporary tables"); + pParse->rc = rc; + return 1; + } + db->aDb[1].pBt = pBt; + assert( db->aDb[1].pSchema ); + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ + db->mallocFailed = 1; + return 1; + } + } + return 0; +} + +/* +** Generate VDBE code that will verify the schema cookie and start +** a read-transaction for all named database files. +** +** It is important that all schema cookies be verified and all +** read transactions be started before anything else happens in +** the VDBE program. But this routine can be called after much other +** code has been generated. So here is what we do: +** +** The first time this routine is called, we code an OP_Goto that +** will jump to a subroutine at the end of the program. Then we +** record every database that needs its schema verified in the +** pParse->cookieMask field. Later, after all other code has been +** generated, the subroutine that does the cookie verifications and +** starts the transactions will be coded and the OP_Goto P2 value +** will be made to point to that subroutine. The generation of the +** cookie verification subroutine code happens in sqlite3FinishCoding(). +** +** If iDb<0 then code the OP_Goto only - don't set flag to verify the +** schema on any databases. This can be used to position the OP_Goto +** early in the code, before we know if any database tables will be used. +*/ +SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + + if( pToplevel->cookieGoto==0 ){ + Vdbe *v = sqlite3GetVdbe(pToplevel); + if( v==0 ) return; /* This only happens if there was a prior error */ + pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1; + } + if( iDb>=0 ){ + sqlite3 *db = pToplevel->db; + int mask; + + assert( iDbnDb ); + assert( db->aDb[iDb].pBt!=0 || iDb==1 ); + assert( iDbcookieMask & mask)==0 ){ + pToplevel->cookieMask |= mask; + pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie; + if( !OMIT_TEMPDB && iDb==1 ){ + sqlite3OpenTempDatabase(pToplevel); + } + } + } +} + +/* +** Generate VDBE code that prepares for doing an operation that +** might change the database. +** +** This routine starts a new transaction if we are not already within +** a transaction. If we are already within a transaction, then a checkpoint +** is set if the setStatement parameter is true. A checkpoint should +** be set for operations that might fail (due to a constraint) part of +** the way through and which will need to undo some writes without having to +** rollback the whole transaction. For operations where all constraints +** can be checked before any changes are made to the database, it is never +** necessary to undo a write and the checkpoint should not be set. +*/ +SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + sqlite3CodeVerifySchema(pParse, iDb); + pToplevel->writeMask |= 1<isMultiWrite |= setStatement; +} + +/* +** Indicate that the statement currently under construction might write +** more than one entry (example: deleting one row then inserting another, +** inserting multiple rows in a table, or inserting a row and index entries.) +** If an abort occurs after some of these writes have completed, then it will +** be necessary to undo the completed writes. +*/ +SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + pToplevel->isMultiWrite = 1; +} + +/* +** The code generator calls this routine if is discovers that it is +** possible to abort a statement prior to completion. In order to +** perform this abort without corrupting the database, we need to make +** sure that the statement is protected by a statement transaction. +** +** Technically, we only need to set the mayAbort flag if the +** isMultiWrite flag was previously set. There is a time dependency +** such that the abort must occur after the multiwrite. This makes +** some statements involving the REPLACE conflict resolution algorithm +** go a little faster. But taking advantage of this time dependency +** makes it more difficult to prove that the code is correct (in +** particular, it prevents us from writing an effective +** implementation of sqlite3AssertMayAbort()) and so we have chosen +** to take the safe route and skip the optimization. +*/ +SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + pToplevel->mayAbort = 1; +} + +/* +** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT +** error. The onError parameter determines which (if any) of the statement +** and/or current transaction is rolled back. +*/ +SQLITE_PRIVATE void sqlite3HaltConstraint(Parse *pParse, int onError, char *p4, int p4type){ + Vdbe *v = sqlite3GetVdbe(pParse); + if( onError==OE_Abort ){ + sqlite3MayAbort(pParse); + } + sqlite3VdbeAddOp4(v, OP_Halt, SQLITE_CONSTRAINT, onError, 0, p4, p4type); +} + +/* +** Check to see if pIndex uses the collating sequence pColl. Return +** true if it does and false if it does not. +*/ +#ifndef SQLITE_OMIT_REINDEX +static int collationMatch(const char *zColl, Index *pIndex){ + int i; + assert( zColl!=0 ); + for(i=0; inColumn; i++){ + const char *z = pIndex->azColl[i]; + assert( z!=0 ); + if( 0==sqlite3StrICmp(z, zColl) ){ + return 1; + } + } + return 0; +} +#endif + +/* +** Recompute all indices of pTab that use the collating sequence pColl. +** If pColl==0 then recompute all indices of pTab. +*/ +#ifndef SQLITE_OMIT_REINDEX +static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){ + Index *pIndex; /* An index associated with pTab */ + + for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ + if( zColl==0 || collationMatch(zColl, pIndex) ){ + int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3RefillIndex(pParse, pIndex, -1); + } + } +} +#endif + +/* +** Recompute all indices of all tables in all databases where the +** indices use the collating sequence pColl. If pColl==0 then recompute +** all indices everywhere. +*/ +#ifndef SQLITE_OMIT_REINDEX +static void reindexDatabases(Parse *pParse, char const *zColl){ + Db *pDb; /* A single database */ + int iDb; /* The database index number */ + sqlite3 *db = pParse->db; /* The database connection */ + HashElem *k; /* For looping over tables in pDb */ + Table *pTab; /* A table in the database */ + + for(iDb=0, pDb=db->aDb; iDbnDb; iDb++, pDb++){ + assert( pDb!=0 ); + for(k=sqliteHashFirst(&pDb->pSchema->tblHash); k; k=sqliteHashNext(k)){ + pTab = (Table*)sqliteHashData(k); + reindexTable(pParse, pTab, zColl); + } + } +} +#endif + +/* +** Generate code for the REINDEX command. +** +** REINDEX -- 1 +** REINDEX -- 2 +** REINDEX ?.? -- 3 +** REINDEX ?.? -- 4 +** +** Form 1 causes all indices in all attached databases to be rebuilt. +** Form 2 rebuilds all indices in all databases that use the named +** collating function. Forms 3 and 4 rebuild the named index or all +** indices associated with the named table. +*/ +#ifndef SQLITE_OMIT_REINDEX +SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){ + CollSeq *pColl; /* Collating sequence to be reindexed, or NULL */ + char *z; /* Name of a table or index */ + const char *zDb; /* Name of the database */ + Table *pTab; /* A table in the database */ + Index *pIndex; /* An index associated with pTab */ + int iDb; /* The database index number */ + sqlite3 *db = pParse->db; /* The database connection */ + Token *pObjName; /* Name of the table or index to be reindexed */ + + /* Read the database schema. If an error occurs, leave an error message + ** and code in pParse and return NULL. */ + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + return; + } + + if( pName1==0 ){ + reindexDatabases(pParse, 0); + return; + }else if( NEVER(pName2==0) || pName2->z==0 ){ + char *zColl; + assert( pName1->z ); + zColl = sqlite3NameFromToken(pParse->db, pName1); + if( !zColl ) return; + pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0); + if( pColl ){ + reindexDatabases(pParse, zColl); + sqlite3DbFree(db, zColl); + return; + } + sqlite3DbFree(db, zColl); + } + iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName); + if( iDb<0 ) return; + z = sqlite3NameFromToken(db, pObjName); + if( z==0 ) return; + zDb = db->aDb[iDb].zName; + pTab = sqlite3FindTable(db, z, zDb); + if( pTab ){ + reindexTable(pParse, pTab, 0); + sqlite3DbFree(db, z); + return; + } + pIndex = sqlite3FindIndex(db, z, zDb); + sqlite3DbFree(db, z); + if( pIndex ){ + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3RefillIndex(pParse, pIndex, -1); + return; + } + sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed"); +} +#endif + +/* +** Return a dynamicly allocated KeyInfo structure that can be used +** with OP_OpenRead or OP_OpenWrite to access database index pIdx. +** +** If successful, a pointer to the new structure is returned. In this case +** the caller is responsible for calling sqlite3DbFree(db, ) on the returned +** pointer. If an error occurs (out of memory or missing collation +** sequence), NULL is returned and the state of pParse updated to reflect +** the error. +*/ +SQLITE_PRIVATE KeyInfo *sqlite3IndexKeyinfo(Parse *pParse, Index *pIdx){ + int i; + int nCol = pIdx->nColumn; + int nBytes = sizeof(KeyInfo) + (nCol-1)*sizeof(CollSeq*) + nCol; + sqlite3 *db = pParse->db; + KeyInfo *pKey = (KeyInfo *)sqlite3DbMallocZero(db, nBytes); + + if( pKey ){ + pKey->db = pParse->db; + pKey->aSortOrder = (u8 *)&(pKey->aColl[nCol]); + assert( &pKey->aSortOrder[nCol]==&(((u8 *)pKey)[nBytes]) ); + for(i=0; iazColl[i]; + assert( zColl ); + pKey->aColl[i] = sqlite3LocateCollSeq(pParse, zColl); + pKey->aSortOrder[i] = pIdx->aSortOrder[i]; + } + pKey->nField = (u16)nCol; + } + + if( pParse->nErr ){ + sqlite3DbFree(db, pKey); + pKey = 0; + } + return pKey; +} + +/************** End of build.c ***********************************************/ +/************** Begin file callback.c ****************************************/ +/* +** 2005 May 23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains functions used to access the internal hash tables +** of user defined functions and collation sequences. +*/ + + +/* +** Invoke the 'collation needed' callback to request a collation sequence +** in the encoding enc of name zName, length nName. +*/ +static void callCollNeeded(sqlite3 *db, int enc, const char *zName){ + assert( !db->xCollNeeded || !db->xCollNeeded16 ); + if( db->xCollNeeded ){ + char *zExternal = sqlite3DbStrDup(db, zName); + if( !zExternal ) return; + db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal); + sqlite3DbFree(db, zExternal); + } +#ifndef SQLITE_OMIT_UTF16 + if( db->xCollNeeded16 ){ + char const *zExternal; + sqlite3_value *pTmp = sqlite3ValueNew(db); + sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC); + zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE); + if( zExternal ){ + db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal); + } + sqlite3ValueFree(pTmp); + } +#endif +} + +/* +** This routine is called if the collation factory fails to deliver a +** collation function in the best encoding but there may be other versions +** of this collation function (for other text encodings) available. Use one +** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if +** possible. +*/ +static int synthCollSeq(sqlite3 *db, CollSeq *pColl){ + CollSeq *pColl2; + char *z = pColl->zName; + int i; + static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 }; + for(i=0; i<3; i++){ + pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0); + if( pColl2->xCmp!=0 ){ + memcpy(pColl, pColl2, sizeof(CollSeq)); + pColl->xDel = 0; /* Do not copy the destructor */ + return SQLITE_OK; + } + } + return SQLITE_ERROR; +} + +/* +** This function is responsible for invoking the collation factory callback +** or substituting a collation sequence of a different encoding when the +** requested collation sequence is not available in the desired encoding. +** +** If it is not NULL, then pColl must point to the database native encoding +** collation sequence with name zName, length nName. +** +** The return value is either the collation sequence to be used in database +** db for collation type name zName, length nName, or NULL, if no collation +** sequence can be found. +** +** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq() +*/ +SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq( + sqlite3* db, /* The database connection */ + u8 enc, /* The desired encoding for the collating sequence */ + CollSeq *pColl, /* Collating sequence with native encoding, or NULL */ + const char *zName /* Collating sequence name */ +){ + CollSeq *p; + + p = pColl; + if( !p ){ + p = sqlite3FindCollSeq(db, enc, zName, 0); + } + if( !p || !p->xCmp ){ + /* No collation sequence of this type for this encoding is registered. + ** Call the collation factory to see if it can supply us with one. + */ + callCollNeeded(db, enc, zName); + p = sqlite3FindCollSeq(db, enc, zName, 0); + } + if( p && !p->xCmp && synthCollSeq(db, p) ){ + p = 0; + } + assert( !p || p->xCmp ); + return p; +} + +/* +** This routine is called on a collation sequence before it is used to +** check that it is defined. An undefined collation sequence exists when +** a database is loaded that contains references to collation sequences +** that have not been defined by sqlite3_create_collation() etc. +** +** If required, this routine calls the 'collation needed' callback to +** request a definition of the collating sequence. If this doesn't work, +** an equivalent collating sequence that uses a text encoding different +** from the main database is substituted, if one is available. +*/ +SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){ + if( pColl ){ + const char *zName = pColl->zName; + sqlite3 *db = pParse->db; + CollSeq *p = sqlite3GetCollSeq(db, ENC(db), pColl, zName); + if( !p ){ + sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName); + pParse->nErr++; + return SQLITE_ERROR; + } + assert( p==pColl ); + } + return SQLITE_OK; +} + + + +/* +** Locate and return an entry from the db.aCollSeq hash table. If the entry +** specified by zName and nName is not found and parameter 'create' is +** true, then create a new entry. Otherwise return NULL. +** +** Each pointer stored in the sqlite3.aCollSeq hash table contains an +** array of three CollSeq structures. The first is the collation sequence +** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be. +** +** Stored immediately after the three collation sequences is a copy of +** the collation sequence name. A pointer to this string is stored in +** each collation sequence structure. +*/ +static CollSeq *findCollSeqEntry( + sqlite3 *db, /* Database connection */ + const char *zName, /* Name of the collating sequence */ + int create /* Create a new entry if true */ +){ + CollSeq *pColl; + int nName = sqlite3Strlen30(zName); + pColl = sqlite3HashFind(&db->aCollSeq, zName, nName); + + if( 0==pColl && create ){ + pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 ); + if( pColl ){ + CollSeq *pDel = 0; + pColl[0].zName = (char*)&pColl[3]; + pColl[0].enc = SQLITE_UTF8; + pColl[1].zName = (char*)&pColl[3]; + pColl[1].enc = SQLITE_UTF16LE; + pColl[2].zName = (char*)&pColl[3]; + pColl[2].enc = SQLITE_UTF16BE; + memcpy(pColl[0].zName, zName, nName); + pColl[0].zName[nName] = 0; + pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl); + + /* If a malloc() failure occurred in sqlite3HashInsert(), it will + ** return the pColl pointer to be deleted (because it wasn't added + ** to the hash table). + */ + assert( pDel==0 || pDel==pColl ); + if( pDel!=0 ){ + db->mallocFailed = 1; + sqlite3DbFree(db, pDel); + pColl = 0; + } + } + } + return pColl; +} + +/* +** Parameter zName points to a UTF-8 encoded string nName bytes long. +** Return the CollSeq* pointer for the collation sequence named zName +** for the encoding 'enc' from the database 'db'. +** +** If the entry specified is not found and 'create' is true, then create a +** new entry. Otherwise return NULL. +** +** A separate function sqlite3LocateCollSeq() is a wrapper around +** this routine. sqlite3LocateCollSeq() invokes the collation factory +** if necessary and generates an error message if the collating sequence +** cannot be found. +** +** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq() +*/ +SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq( + sqlite3 *db, + u8 enc, + const char *zName, + int create +){ + CollSeq *pColl; + if( zName ){ + pColl = findCollSeqEntry(db, zName, create); + }else{ + pColl = db->pDfltColl; + } + assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 ); + assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE ); + if( pColl ) pColl += enc-1; + return pColl; +} + +/* During the search for the best function definition, this procedure +** is called to test how well the function passed as the first argument +** matches the request for a function with nArg arguments in a system +** that uses encoding enc. The value returned indicates how well the +** request is matched. A higher value indicates a better match. +** +** The returned value is always between 0 and 6, as follows: +** +** 0: Not a match, or if nArg<0 and the function is has no implementation. +** 1: A variable arguments function that prefers UTF-8 when a UTF-16 +** encoding is requested, or vice versa. +** 2: A variable arguments function that uses UTF-16BE when UTF-16LE is +** requested, or vice versa. +** 3: A variable arguments function using the same text encoding. +** 4: A function with the exact number of arguments requested that +** prefers UTF-8 when a UTF-16 encoding is requested, or vice versa. +** 5: A function with the exact number of arguments requested that +** prefers UTF-16LE when UTF-16BE is requested, or vice versa. +** 6: An exact match. +** +*/ +static int matchQuality(FuncDef *p, int nArg, u8 enc){ + int match = 0; + if( p->nArg==-1 || p->nArg==nArg + || (nArg==-1 && (p->xFunc!=0 || p->xStep!=0)) + ){ + match = 1; + if( p->nArg==nArg || nArg==-1 ){ + match = 4; + } + if( enc==p->iPrefEnc ){ + match += 2; + } + else if( (enc==SQLITE_UTF16LE && p->iPrefEnc==SQLITE_UTF16BE) || + (enc==SQLITE_UTF16BE && p->iPrefEnc==SQLITE_UTF16LE) ){ + match += 1; + } + } + return match; +} + +/* +** Search a FuncDefHash for a function with the given name. Return +** a pointer to the matching FuncDef if found, or 0 if there is no match. +*/ +static FuncDef *functionSearch( + FuncDefHash *pHash, /* Hash table to search */ + int h, /* Hash of the name */ + const char *zFunc, /* Name of function */ + int nFunc /* Number of bytes in zFunc */ +){ + FuncDef *p; + for(p=pHash->a[h]; p; p=p->pHash){ + if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){ + return p; + } + } + return 0; +} + +/* +** Insert a new FuncDef into a FuncDefHash hash table. +*/ +SQLITE_PRIVATE void sqlite3FuncDefInsert( + FuncDefHash *pHash, /* The hash table into which to insert */ + FuncDef *pDef /* The function definition to insert */ +){ + FuncDef *pOther; + int nName = sqlite3Strlen30(pDef->zName); + u8 c1 = (u8)pDef->zName[0]; + int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a); + pOther = functionSearch(pHash, h, pDef->zName, nName); + if( pOther ){ + assert( pOther!=pDef && pOther->pNext!=pDef ); + pDef->pNext = pOther->pNext; + pOther->pNext = pDef; + }else{ + pDef->pNext = 0; + pDef->pHash = pHash->a[h]; + pHash->a[h] = pDef; + } +} + + + +/* +** Locate a user function given a name, a number of arguments and a flag +** indicating whether the function prefers UTF-16 over UTF-8. Return a +** pointer to the FuncDef structure that defines that function, or return +** NULL if the function does not exist. +** +** If the createFlag argument is true, then a new (blank) FuncDef +** structure is created and liked into the "db" structure if a +** no matching function previously existed. When createFlag is true +** and the nArg parameter is -1, then only a function that accepts +** any number of arguments will be returned. +** +** If createFlag is false and nArg is -1, then the first valid +** function found is returned. A function is valid if either xFunc +** or xStep is non-zero. +** +** If createFlag is false, then a function with the required name and +** number of arguments may be returned even if the eTextRep flag does not +** match that requested. +*/ +SQLITE_PRIVATE FuncDef *sqlite3FindFunction( + sqlite3 *db, /* An open database */ + const char *zName, /* Name of the function. Not null-terminated */ + int nName, /* Number of characters in the name */ + int nArg, /* Number of arguments. -1 means any number */ + u8 enc, /* Preferred text encoding */ + int createFlag /* Create new entry if true and does not otherwise exist */ +){ + FuncDef *p; /* Iterator variable */ + FuncDef *pBest = 0; /* Best match found so far */ + int bestScore = 0; /* Score of best match */ + int h; /* Hash value */ + + + assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE ); + h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a); + + /* First search for a match amongst the application-defined functions. + */ + p = functionSearch(&db->aFunc, h, zName, nName); + while( p ){ + int score = matchQuality(p, nArg, enc); + if( score>bestScore ){ + pBest = p; + bestScore = score; + } + p = p->pNext; + } + + /* If no match is found, search the built-in functions. + ** + ** If the SQLITE_PreferBuiltin flag is set, then search the built-in + ** functions even if a prior app-defined function was found. And give + ** priority to built-in functions. + ** + ** Except, if createFlag is true, that means that we are trying to + ** install a new function. Whatever FuncDef structure is returned it will + ** have fields overwritten with new information appropriate for the + ** new function. But the FuncDefs for built-in functions are read-only. + ** So we must not search for built-ins when creating a new function. + */ + if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){ + FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); + bestScore = 0; + p = functionSearch(pHash, h, zName, nName); + while( p ){ + int score = matchQuality(p, nArg, enc); + if( score>bestScore ){ + pBest = p; + bestScore = score; + } + p = p->pNext; + } + } + + /* If the createFlag parameter is true and the search did not reveal an + ** exact match for the name, number of arguments and encoding, then add a + ** new entry to the hash table and return it. + */ + if( createFlag && (bestScore<6 || pBest->nArg!=nArg) && + (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){ + pBest->zName = (char *)&pBest[1]; + pBest->nArg = (u16)nArg; + pBest->iPrefEnc = enc; + memcpy(pBest->zName, zName, nName); + pBest->zName[nName] = 0; + sqlite3FuncDefInsert(&db->aFunc, pBest); + } + + if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){ + return pBest; + } + return 0; +} + +/* +** Free all resources held by the schema structure. The void* argument points +** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the +** pointer itself, it just cleans up subsiduary resources (i.e. the contents +** of the schema hash tables). +** +** The Schema.cache_size variable is not cleared. +*/ +SQLITE_PRIVATE void sqlite3SchemaFree(void *p){ + Hash temp1; + Hash temp2; + HashElem *pElem; + Schema *pSchema = (Schema *)p; + + temp1 = pSchema->tblHash; + temp2 = pSchema->trigHash; + sqlite3HashInit(&pSchema->trigHash); + sqlite3HashClear(&pSchema->idxHash); + for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){ + sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem)); + } + sqlite3HashClear(&temp2); + sqlite3HashInit(&pSchema->tblHash); + for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){ + Table *pTab = sqliteHashData(pElem); + sqlite3DeleteTable(0, pTab); + } + sqlite3HashClear(&temp1); + sqlite3HashClear(&pSchema->fkeyHash); + pSchema->pSeqTab = 0; + pSchema->flags &= ~DB_SchemaLoaded; +} + +/* +** Find and return the schema associated with a BTree. Create +** a new one if necessary. +*/ +SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){ + Schema * p; + if( pBt ){ + p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaFree); + }else{ + p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema)); + } + if( !p ){ + db->mallocFailed = 1; + }else if ( 0==p->file_format ){ + sqlite3HashInit(&p->tblHash); + sqlite3HashInit(&p->idxHash); + sqlite3HashInit(&p->trigHash); + sqlite3HashInit(&p->fkeyHash); + p->enc = SQLITE_UTF8; + } + return p; +} + +/************** End of callback.c ********************************************/ +/************** Begin file delete.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that are called by the parser +** in order to generate code for DELETE FROM statements. +*/ + +/* +** Look up every table that is named in pSrc. If any table is not found, +** add an error message to pParse->zErrMsg and return NULL. If all tables +** are found, return a pointer to the last table. +*/ +SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){ + struct SrcList_item *pItem = pSrc->a; + Table *pTab; + assert( pItem && pSrc->nSrc==1 ); + pTab = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); + sqlite3DeleteTable(pParse->db, pItem->pTab); + pItem->pTab = pTab; + if( pTab ){ + pTab->nRef++; + } + if( sqlite3IndexedByLookup(pParse, pItem) ){ + pTab = 0; + } + return pTab; +} + +/* +** Check to make sure the given table is writable. If it is not +** writable, generate an error message and return 1. If it is +** writable return 0; +*/ +SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){ + /* A table is not writable under the following circumstances: + ** + ** 1) It is a virtual table and no implementation of the xUpdate method + ** has been provided, or + ** 2) It is a system table (i.e. sqlite_master), this call is not + ** part of a nested parse and writable_schema pragma has not + ** been specified. + ** + ** In either case leave an error message in pParse and return non-zero. + */ + if( ( IsVirtual(pTab) + && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 ) + || ( (pTab->tabFlags & TF_Readonly)!=0 + && (pParse->db->flags & SQLITE_WriteSchema)==0 + && pParse->nested==0 ) + ){ + sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName); + return 1; + } + +#ifndef SQLITE_OMIT_VIEW + if( !viewOk && pTab->pSelect ){ + sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName); + return 1; + } +#endif + return 0; +} + + +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) +/* +** Evaluate a view and store its result in an ephemeral table. The +** pWhere argument is an optional WHERE clause that restricts the +** set of rows in the view that are to be added to the ephemeral table. +*/ +SQLITE_PRIVATE void sqlite3MaterializeView( + Parse *pParse, /* Parsing context */ + Table *pView, /* View definition */ + Expr *pWhere, /* Optional WHERE clause to be added */ + int iCur /* Cursor number for ephemerial table */ +){ + SelectDest dest; + Select *pDup; + sqlite3 *db = pParse->db; + + pDup = sqlite3SelectDup(db, pView->pSelect, 0); + if( pWhere ){ + SrcList *pFrom; + + pWhere = sqlite3ExprDup(db, pWhere, 0); + pFrom = sqlite3SrcListAppend(db, 0, 0, 0); + if( pFrom ){ + assert( pFrom->nSrc==1 ); + pFrom->a[0].zAlias = sqlite3DbStrDup(db, pView->zName); + pFrom->a[0].pSelect = pDup; + assert( pFrom->a[0].pOn==0 ); + assert( pFrom->a[0].pUsing==0 ); + }else{ + sqlite3SelectDelete(db, pDup); + } + pDup = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0); + } + sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur); + sqlite3Select(pParse, pDup, &dest); + sqlite3SelectDelete(db, pDup); +} +#endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */ + +#if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) +/* +** Generate an expression tree to implement the WHERE, ORDER BY, +** and LIMIT/OFFSET portion of DELETE and UPDATE statements. +** +** DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1; +** \__________________________/ +** pLimitWhere (pInClause) +*/ +SQLITE_PRIVATE Expr *sqlite3LimitWhere( + Parse *pParse, /* The parser context */ + SrcList *pSrc, /* the FROM clause -- which tables to scan */ + Expr *pWhere, /* The WHERE clause. May be null */ + ExprList *pOrderBy, /* The ORDER BY clause. May be null */ + Expr *pLimit, /* The LIMIT clause. May be null */ + Expr *pOffset, /* The OFFSET clause. May be null */ + char *zStmtType /* Either DELETE or UPDATE. For error messages. */ +){ + Expr *pWhereRowid = NULL; /* WHERE rowid .. */ + Expr *pInClause = NULL; /* WHERE rowid IN ( select ) */ + Expr *pSelectRowid = NULL; /* SELECT rowid ... */ + ExprList *pEList = NULL; /* Expression list contaning only pSelectRowid */ + SrcList *pSelectSrc = NULL; /* SELECT rowid FROM x ... (dup of pSrc) */ + Select *pSelect = NULL; /* Complete SELECT tree */ + + /* Check that there isn't an ORDER BY without a LIMIT clause. + */ + if( pOrderBy && (pLimit == 0) ) { + sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType); + pParse->parseError = 1; + goto limit_where_cleanup_2; + } + + /* We only need to generate a select expression if there + ** is a limit/offset term to enforce. + */ + if( pLimit == 0 ) { + /* if pLimit is null, pOffset will always be null as well. */ + assert( pOffset == 0 ); + return pWhere; + } + + /* Generate a select expression tree to enforce the limit/offset + ** term for the DELETE or UPDATE statement. For example: + ** DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1 + ** becomes: + ** DELETE FROM table_a WHERE rowid IN ( + ** SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1 + ** ); + */ + + pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0); + if( pSelectRowid == 0 ) goto limit_where_cleanup_2; + pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid); + if( pEList == 0 ) goto limit_where_cleanup_2; + + /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree + ** and the SELECT subtree. */ + pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0); + if( pSelectSrc == 0 ) { + sqlite3ExprListDelete(pParse->db, pEList); + goto limit_where_cleanup_2; + } + + /* generate the SELECT expression tree. */ + pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0, + pOrderBy,0,pLimit,pOffset); + if( pSelect == 0 ) return 0; + + /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */ + pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0); + if( pWhereRowid == 0 ) goto limit_where_cleanup_1; + pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0); + if( pInClause == 0 ) goto limit_where_cleanup_1; + + pInClause->x.pSelect = pSelect; + pInClause->flags |= EP_xIsSelect; + sqlite3ExprSetHeight(pParse, pInClause); + return pInClause; + + /* something went wrong. clean up anything allocated. */ +limit_where_cleanup_1: + sqlite3SelectDelete(pParse->db, pSelect); + return 0; + +limit_where_cleanup_2: + sqlite3ExprDelete(pParse->db, pWhere); + sqlite3ExprListDelete(pParse->db, pOrderBy); + sqlite3ExprDelete(pParse->db, pLimit); + sqlite3ExprDelete(pParse->db, pOffset); + return 0; +} +#endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */ + +/* +** Generate code for a DELETE FROM statement. +** +** DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL; +** \________/ \________________/ +** pTabList pWhere +*/ +SQLITE_PRIVATE void sqlite3DeleteFrom( + Parse *pParse, /* The parser context */ + SrcList *pTabList, /* The table from which we should delete things */ + Expr *pWhere /* The WHERE clause. May be null */ +){ + Vdbe *v; /* The virtual database engine */ + Table *pTab; /* The table from which records will be deleted */ + const char *zDb; /* Name of database holding pTab */ + int end, addr = 0; /* A couple addresses of generated code */ + int i; /* Loop counter */ + WhereInfo *pWInfo; /* Information about the WHERE clause */ + Index *pIdx; /* For looping over indices of the table */ + int iCur; /* VDBE Cursor number for pTab */ + sqlite3 *db; /* Main database structure */ + AuthContext sContext; /* Authorization context */ + NameContext sNC; /* Name context to resolve expressions in */ + int iDb; /* Database number */ + int memCnt = -1; /* Memory cell used for change counting */ + int rcauth; /* Value returned by authorization callback */ + +#ifndef SQLITE_OMIT_TRIGGER + int isView; /* True if attempting to delete from a view */ + Trigger *pTrigger; /* List of table triggers, if required */ +#endif + + memset(&sContext, 0, sizeof(sContext)); + db = pParse->db; + if( pParse->nErr || db->mallocFailed ){ + goto delete_from_cleanup; + } + assert( pTabList->nSrc==1 ); + + /* Locate the table which we want to delete. This table has to be + ** put in an SrcList structure because some of the subroutines we + ** will be calling are designed to work with multiple tables and expect + ** an SrcList* parameter instead of just a Table* parameter. + */ + pTab = sqlite3SrcListLookup(pParse, pTabList); + if( pTab==0 ) goto delete_from_cleanup; + + /* Figure out if we have any triggers and if the table being + ** deleted from is a view + */ +#ifndef SQLITE_OMIT_TRIGGER + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); + isView = pTab->pSelect!=0; +#else +# define pTrigger 0 +# define isView 0 +#endif +#ifdef SQLITE_OMIT_VIEW +# undef isView +# define isView 0 +#endif + + /* If pTab is really a view, make sure it has been initialized. + */ + if( sqlite3ViewGetColumnNames(pParse, pTab) ){ + goto delete_from_cleanup; + } + + if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){ + goto delete_from_cleanup; + } + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iDbnDb ); + zDb = db->aDb[iDb].zName; + rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb); + assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE ); + if( rcauth==SQLITE_DENY ){ + goto delete_from_cleanup; + } + assert(!isView || pTrigger); + + /* Assign cursor number to the table and all its indices. + */ + assert( pTabList->nSrc==1 ); + iCur = pTabList->a[0].iCursor = pParse->nTab++; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + pParse->nTab++; + } + + /* Start the view context + */ + if( isView ){ + sqlite3AuthContextPush(pParse, &sContext, pTab->zName); + } + + /* Begin generating code. + */ + v = sqlite3GetVdbe(pParse); + if( v==0 ){ + goto delete_from_cleanup; + } + if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); + sqlite3BeginWriteOperation(pParse, 1, iDb); + + /* If we are trying to delete from a view, realize that view into + ** a ephemeral table. + */ +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) + if( isView ){ + sqlite3MaterializeView(pParse, pTab, pWhere, iCur); + } +#endif + + /* Resolve the column names in the WHERE clause. + */ + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + sNC.pSrcList = pTabList; + if( sqlite3ResolveExprNames(&sNC, pWhere) ){ + goto delete_from_cleanup; + } + + /* Initialize the counter of the number of rows deleted, if + ** we are counting rows. + */ + if( db->flags & SQLITE_CountRows ){ + memCnt = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt); + } + +#ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION + /* Special case: A DELETE without a WHERE clause deletes everything. + ** It is easier just to erase the whole table. Prior to version 3.6.5, + ** this optimization caused the row change count (the value returned by + ** API function sqlite3_count_changes) to be set incorrectly. */ + if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab) + && 0==sqlite3FkRequired(pParse, pTab, 0, 0) + ){ + assert( !isView ); + sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt, + pTab->zName, P4_STATIC); + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + assert( pIdx->pSchema==pTab->pSchema ); + sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb); + } + }else +#endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */ + /* The usual case: There is a WHERE clause so we have to scan through + ** the table and pick which records to delete. + */ + { + int iRowSet = ++pParse->nMem; /* Register for rowset of rows to delete */ + int iRowid = ++pParse->nMem; /* Used for storing rowid values. */ + int regRowid; /* Actual register containing rowids */ + + /* Collect rowids of every row to be deleted. + */ + sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0,WHERE_DUPLICATES_OK); + if( pWInfo==0 ) goto delete_from_cleanup; + regRowid = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, iRowid); + sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, regRowid); + if( db->flags & SQLITE_CountRows ){ + sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1); + } + sqlite3WhereEnd(pWInfo); + + /* Delete every item whose key was written to the list during the + ** database scan. We have to delete items after the scan is complete + ** because deleting an item can change the scan order. */ + end = sqlite3VdbeMakeLabel(v); + + /* Unless this is a view, open cursors for the table we are + ** deleting from and all its indices. If this is a view, then the + ** only effect this statement has is to fire the INSTEAD OF + ** triggers. */ + if( !isView ){ + sqlite3OpenTableAndIndices(pParse, pTab, iCur, OP_OpenWrite); + } + + addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, end, iRowid); + + /* Delete the row */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); + sqlite3VtabMakeWritable(pParse, pTab); + sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iRowid, pVTab, P4_VTAB); + sqlite3MayAbort(pParse); + }else +#endif + { + int count = (pParse->nested==0); /* True to count changes */ + sqlite3GenerateRowDelete(pParse, pTab, iCur, iRowid, count, pTrigger, OE_Default); + } + + /* End of the delete loop */ + sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); + sqlite3VdbeResolveLabel(v, end); + + /* Close the cursors open on the table and its indexes. */ + if( !isView && !IsVirtual(pTab) ){ + for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ + sqlite3VdbeAddOp2(v, OP_Close, iCur + i, pIdx->tnum); + } + sqlite3VdbeAddOp1(v, OP_Close, iCur); + } + } + + /* Update the sqlite_sequence table by storing the content of the + ** maximum rowid counter values recorded while inserting into + ** autoincrement tables. + */ + if( pParse->nested==0 && pParse->pTriggerTab==0 ){ + sqlite3AutoincrementEnd(pParse); + } + + /* Return the number of rows that were deleted. If this routine is + ** generating code because of a call to sqlite3NestedParse(), do not + ** invoke the callback function. + */ + if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){ + sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC); + } + +delete_from_cleanup: + sqlite3AuthContextPop(&sContext); + sqlite3SrcListDelete(db, pTabList); + sqlite3ExprDelete(db, pWhere); + return; +} +/* Make sure "isView" and other macros defined above are undefined. Otherwise +** thely may interfere with compilation of other functions in this file +** (or in another file, if this file becomes part of the amalgamation). */ +#ifdef isView + #undef isView +#endif +#ifdef pTrigger + #undef pTrigger +#endif + +/* +** This routine generates VDBE code that causes a single row of a +** single table to be deleted. +** +** The VDBE must be in a particular state when this routine is called. +** These are the requirements: +** +** 1. A read/write cursor pointing to pTab, the table containing the row +** to be deleted, must be opened as cursor number $iCur. +** +** 2. Read/write cursors for all indices of pTab must be open as +** cursor number base+i for the i-th index. +** +** 3. The record number of the row to be deleted must be stored in +** memory cell iRowid. +** +** This routine generates code to remove both the table record and all +** index entries that point to that record. +*/ +SQLITE_PRIVATE void sqlite3GenerateRowDelete( + Parse *pParse, /* Parsing context */ + Table *pTab, /* Table containing the row to be deleted */ + int iCur, /* Cursor number for the table */ + int iRowid, /* Memory cell that contains the rowid to delete */ + int count, /* If non-zero, increment the row change counter */ + Trigger *pTrigger, /* List of triggers to (potentially) fire */ + int onconf /* Default ON CONFLICT policy for triggers */ +){ + Vdbe *v = pParse->pVdbe; /* Vdbe */ + int iOld = 0; /* First register in OLD.* array */ + int iLabel; /* Label resolved to end of generated code */ + + /* Vdbe is guaranteed to have been allocated by this stage. */ + assert( v ); + + /* Seek cursor iCur to the row to delete. If this row no longer exists + ** (this can happen if a trigger program has already deleted it), do + ** not attempt to delete it or fire any DELETE triggers. */ + iLabel = sqlite3VdbeMakeLabel(v); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, iLabel, iRowid); + + /* If there are any triggers to fire, allocate a range of registers to + ** use for the old.* references in the triggers. */ + if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){ + u32 mask; /* Mask of OLD.* columns in use */ + int iCol; /* Iterator used while populating OLD.* */ + + /* TODO: Could use temporary registers here. Also could attempt to + ** avoid copying the contents of the rowid register. */ + mask = sqlite3TriggerColmask( + pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf + ); + mask |= sqlite3FkOldmask(pParse, pTab); + iOld = pParse->nMem+1; + pParse->nMem += (1 + pTab->nCol); + + /* Populate the OLD.* pseudo-table register array. These values will be + ** used by any BEFORE and AFTER triggers that exist. */ + sqlite3VdbeAddOp2(v, OP_Copy, iRowid, iOld); + for(iCol=0; iColnCol; iCol++){ + if( mask==0xffffffff || mask&(1<pSelect==0 ){ + sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, 0); + sqlite3VdbeAddOp2(v, OP_Delete, iCur, (count?OPFLAG_NCHANGE:0)); + if( count ){ + sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); + } + } + + /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to + ** handle rows (possibly in other tables) that refer via a foreign key + ** to the row just deleted. */ + sqlite3FkActions(pParse, pTab, 0, iOld); + + /* Invoke AFTER DELETE trigger programs. */ + sqlite3CodeRowTrigger(pParse, pTrigger, + TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel + ); + + /* Jump here if the row had already been deleted before any BEFORE + ** trigger programs were invoked. Or if a trigger program throws a + ** RAISE(IGNORE) exception. */ + sqlite3VdbeResolveLabel(v, iLabel); +} + +/* +** This routine generates VDBE code that causes the deletion of all +** index entries associated with a single row of a single table. +** +** The VDBE must be in a particular state when this routine is called. +** These are the requirements: +** +** 1. A read/write cursor pointing to pTab, the table containing the row +** to be deleted, must be opened as cursor number "iCur". +** +** 2. Read/write cursors for all indices of pTab must be open as +** cursor number iCur+i for the i-th index. +** +** 3. The "iCur" cursor must be pointing to the row that is to be +** deleted. +*/ +SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete( + Parse *pParse, /* Parsing and code generating context */ + Table *pTab, /* Table containing the row to be deleted */ + int iCur, /* Cursor number for the table */ + int *aRegIdx /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */ +){ + int i; + Index *pIdx; + int r1; + + for(i=1, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){ + if( aRegIdx!=0 && aRegIdx[i-1]==0 ) continue; + r1 = sqlite3GenerateIndexKey(pParse, pIdx, iCur, 0, 0); + sqlite3VdbeAddOp3(pParse->pVdbe, OP_IdxDelete, iCur+i, r1,pIdx->nColumn+1); + } +} + +/* +** Generate code that will assemble an index key and put it in register +** regOut. The key with be for index pIdx which is an index on pTab. +** iCur is the index of a cursor open on the pTab table and pointing to +** the entry that needs indexing. +** +** Return a register number which is the first in a block of +** registers that holds the elements of the index key. The +** block of registers has already been deallocated by the time +** this routine returns. +*/ +SQLITE_PRIVATE int sqlite3GenerateIndexKey( + Parse *pParse, /* Parsing context */ + Index *pIdx, /* The index for which to generate a key */ + int iCur, /* Cursor number for the pIdx->pTable table */ + int regOut, /* Write the new index key to this register */ + int doMakeRec /* Run the OP_MakeRecord instruction if true */ +){ + Vdbe *v = pParse->pVdbe; + int j; + Table *pTab = pIdx->pTable; + int regBase; + int nCol; + + nCol = pIdx->nColumn; + regBase = sqlite3GetTempRange(pParse, nCol+1); + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regBase+nCol); + for(j=0; jaiColumn[j]; + if( idx==pTab->iPKey ){ + sqlite3VdbeAddOp2(v, OP_SCopy, regBase+nCol, regBase+j); + }else{ + sqlite3VdbeAddOp3(v, OP_Column, iCur, idx, regBase+j); + sqlite3ColumnDefault(v, pTab, idx, -1); + } + } + if( doMakeRec ){ + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol+1, regOut); + sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0); + } + sqlite3ReleaseTempRange(pParse, regBase, nCol+1); + return regBase; +} + +/************** End of delete.c **********************************************/ +/************** Begin file func.c ********************************************/ +/* +** 2002 February 23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the C functions that implement various SQL +** functions of SQLite. +** +** There is only one exported symbol in this file - the function +** sqliteRegisterBuildinFunctions() found at the bottom of the file. +** All other code has file scope. +*/ + +/* +** Return the collating function associated with a function. +*/ +static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){ + return context->pColl; +} + +/* +** Implementation of the non-aggregate min() and max() functions +*/ +static void minmaxFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int i; + int mask; /* 0 for min() or 0xffffffff for max() */ + int iBest; + CollSeq *pColl; + + assert( argc>1 ); + mask = sqlite3_user_data(context)==0 ? 0 : -1; + pColl = sqlite3GetFuncCollSeq(context); + assert( pColl ); + assert( mask==-1 || mask==0 ); + iBest = 0; + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + for(i=1; i=0 ){ + testcase( mask==0 ); + iBest = i; + } + } + sqlite3_result_value(context, argv[iBest]); +} + +/* +** Return the type of the argument. +*/ +static void typeofFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + const char *z = 0; + UNUSED_PARAMETER(NotUsed); + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_INTEGER: z = "integer"; break; + case SQLITE_TEXT: z = "text"; break; + case SQLITE_FLOAT: z = "real"; break; + case SQLITE_BLOB: z = "blob"; break; + default: z = "null"; break; + } + sqlite3_result_text(context, z, -1, SQLITE_STATIC); +} + + +/* +** Implementation of the length() function +*/ +static void lengthFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int len; + + assert( argc==1 ); + UNUSED_PARAMETER(argc); + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_BLOB: + case SQLITE_INTEGER: + case SQLITE_FLOAT: { + sqlite3_result_int(context, sqlite3_value_bytes(argv[0])); + break; + } + case SQLITE_TEXT: { + const unsigned char *z = sqlite3_value_text(argv[0]); + if( z==0 ) return; + len = 0; + while( *z ){ + len++; + SQLITE_SKIP_UTF8(z); + } + sqlite3_result_int(context, len); + break; + } + default: { + sqlite3_result_null(context); + break; + } + } +} + +/* +** Implementation of the abs() function. +** +** IMP: R-23979-26855 The abs(X) function returns the absolute value of +** the numeric argument X. +*/ +static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ + assert( argc==1 ); + UNUSED_PARAMETER(argc); + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_INTEGER: { + i64 iVal = sqlite3_value_int64(argv[0]); + if( iVal<0 ){ + if( (iVal<<1)==0 ){ + /* IMP: R-35460-15084 If X is the integer -9223372036854775807 then + ** abs(X) throws an integer overflow error since there is no + ** equivalent positive 64-bit two complement value. */ + sqlite3_result_error(context, "integer overflow", -1); + return; + } + iVal = -iVal; + } + sqlite3_result_int64(context, iVal); + break; + } + case SQLITE_NULL: { + /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */ + sqlite3_result_null(context); + break; + } + default: { + /* Because sqlite3_value_double() returns 0.0 if the argument is not + ** something that can be converted into a number, we have: + ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that + ** cannot be converted to a numeric value. + */ + double rVal = sqlite3_value_double(argv[0]); + if( rVal<0 ) rVal = -rVal; + sqlite3_result_double(context, rVal); + break; + } + } +} + +/* +** Implementation of the substr() function. +** +** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. +** p1 is 1-indexed. So substr(x,1,1) returns the first character +** of x. If x is text, then we actually count UTF-8 characters. +** If x is a blob, then we count bytes. +** +** If p1 is negative, then we begin abs(p1) from the end of x[]. +** +** If p2 is negative, return the p2 characters preceeding p1. +*/ +static void substrFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *z; + const unsigned char *z2; + int len; + int p0type; + i64 p1, p2; + int negP2 = 0; + + assert( argc==3 || argc==2 ); + if( sqlite3_value_type(argv[1])==SQLITE_NULL + || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL) + ){ + return; + } + p0type = sqlite3_value_type(argv[0]); + p1 = sqlite3_value_int(argv[1]); + if( p0type==SQLITE_BLOB ){ + len = sqlite3_value_bytes(argv[0]); + z = sqlite3_value_blob(argv[0]); + if( z==0 ) return; + assert( len==sqlite3_value_bytes(argv[0]) ); + }else{ + z = sqlite3_value_text(argv[0]); + if( z==0 ) return; + len = 0; + if( p1<0 ){ + for(z2=z; *z2; len++){ + SQLITE_SKIP_UTF8(z2); + } + } + } + if( argc==3 ){ + p2 = sqlite3_value_int(argv[2]); + if( p2<0 ){ + p2 = -p2; + negP2 = 1; + } + }else{ + p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH]; + } + if( p1<0 ){ + p1 += len; + if( p1<0 ){ + p2 += p1; + if( p2<0 ) p2 = 0; + p1 = 0; + } + }else if( p1>0 ){ + p1--; + }else if( p2>0 ){ + p2--; + } + if( negP2 ){ + p1 -= p2; + if( p1<0 ){ + p2 += p1; + p1 = 0; + } + } + assert( p1>=0 && p2>=0 ); + if( p0type!=SQLITE_BLOB ){ + while( *z && p1 ){ + SQLITE_SKIP_UTF8(z); + p1--; + } + for(z2=z; *z2 && p2; p2--){ + SQLITE_SKIP_UTF8(z2); + } + sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT); + }else{ + if( p1+p2>len ){ + p2 = len-p1; + if( p2<0 ) p2 = 0; + } + sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT); + } +} + +/* +** Implementation of the round() function +*/ +#ifndef SQLITE_OMIT_FLOATING_POINT +static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ + int n = 0; + double r; + char *zBuf; + assert( argc==1 || argc==2 ); + if( argc==2 ){ + if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return; + n = sqlite3_value_int(argv[1]); + if( n>30 ) n = 30; + if( n<0 ) n = 0; + } + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + r = sqlite3_value_double(argv[0]); + /* If Y==0 and X will fit in a 64-bit int, + ** handle the rounding directly, + ** otherwise use printf. + */ + if( n==0 && r>=0 && r0 ); + testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] ); + testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); + if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){ + sqlite3_result_error_toobig(context); + z = 0; + }else{ + z = sqlite3Malloc((int)nByte); + if( !z ){ + sqlite3_result_error_nomem(context); + } + } + return z; +} + +/* +** Implementation of the upper() and lower() SQL functions. +*/ +static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ + char *z1; + const char *z2; + int i, n; + UNUSED_PARAMETER(argc); + z2 = (char*)sqlite3_value_text(argv[0]); + n = sqlite3_value_bytes(argv[0]); + /* Verify that the call to _bytes() does not invalidate the _text() pointer */ + assert( z2==(char*)sqlite3_value_text(argv[0]) ); + if( z2 ){ + z1 = contextMalloc(context, ((i64)n)+1); + if( z1 ){ + memcpy(z1, z2, n+1); + for(i=0; z1[i]; i++){ + z1[i] = (char)sqlite3Toupper(z1[i]); + } + sqlite3_result_text(context, z1, -1, sqlite3_free); + } + } +} +static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ + u8 *z1; + const char *z2; + int i, n; + UNUSED_PARAMETER(argc); + z2 = (char*)sqlite3_value_text(argv[0]); + n = sqlite3_value_bytes(argv[0]); + /* Verify that the call to _bytes() does not invalidate the _text() pointer */ + assert( z2==(char*)sqlite3_value_text(argv[0]) ); + if( z2 ){ + z1 = contextMalloc(context, ((i64)n)+1); + if( z1 ){ + memcpy(z1, z2, n+1); + for(i=0; z1[i]; i++){ + z1[i] = sqlite3Tolower(z1[i]); + } + sqlite3_result_text(context, (char *)z1, -1, sqlite3_free); + } + } +} + + +#if 0 /* This function is never used. */ +/* +** The COALESCE() and IFNULL() functions used to be implemented as shown +** here. But now they are implemented as VDBE code so that unused arguments +** do not have to be computed. This legacy implementation is retained as +** comment. +*/ +/* +** Implementation of the IFNULL(), NVL(), and COALESCE() functions. +** All three do the same thing. They return the first non-NULL +** argument. +*/ +static void ifnullFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int i; + for(i=0; imatchOne; + u8 matchAll = pInfo->matchAll; + u8 matchSet = pInfo->matchSet; + u8 noCase = pInfo->noCase; + int prevEscape = 0; /* True if the previous character was 'escape' */ + + while( (c = sqlite3Utf8Read(zPattern,&zPattern))!=0 ){ + if( !prevEscape && c==matchAll ){ + while( (c=sqlite3Utf8Read(zPattern,&zPattern)) == matchAll + || c == matchOne ){ + if( c==matchOne && sqlite3Utf8Read(zString, &zString)==0 ){ + return 0; + } + } + if( c==0 ){ + return 1; + }else if( c==esc ){ + c = sqlite3Utf8Read(zPattern, &zPattern); + if( c==0 ){ + return 0; + } + }else if( c==matchSet ){ + assert( esc==0 ); /* This is GLOB, not LIKE */ + assert( matchSet<0x80 ); /* '[' is a single-byte character */ + while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){ + SQLITE_SKIP_UTF8(zString); + } + return *zString!=0; + } + while( (c2 = sqlite3Utf8Read(zString,&zString))!=0 ){ + if( noCase ){ + GlogUpperToLower(c2); + GlogUpperToLower(c); + while( c2 != 0 && c2 != c ){ + c2 = sqlite3Utf8Read(zString, &zString); + GlogUpperToLower(c2); + } + }else{ + while( c2 != 0 && c2 != c ){ + c2 = sqlite3Utf8Read(zString, &zString); + } + } + if( c2==0 ) return 0; + if( patternCompare(zPattern,zString,pInfo,esc) ) return 1; + } + return 0; + }else if( !prevEscape && c==matchOne ){ + if( sqlite3Utf8Read(zString, &zString)==0 ){ + return 0; + } + }else if( c==matchSet ){ + int prior_c = 0; + assert( esc==0 ); /* This only occurs for GLOB, not LIKE */ + seen = 0; + invert = 0; + c = sqlite3Utf8Read(zString, &zString); + if( c==0 ) return 0; + c2 = sqlite3Utf8Read(zPattern, &zPattern); + if( c2=='^' ){ + invert = 1; + c2 = sqlite3Utf8Read(zPattern, &zPattern); + } + if( c2==']' ){ + if( c==']' ) seen = 1; + c2 = sqlite3Utf8Read(zPattern, &zPattern); + } + while( c2 && c2!=']' ){ + if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){ + c2 = sqlite3Utf8Read(zPattern, &zPattern); + if( c>=prior_c && c<=c2 ) seen = 1; + prior_c = 0; + }else{ + if( c==c2 ){ + seen = 1; + } + prior_c = c2; + } + c2 = sqlite3Utf8Read(zPattern, &zPattern); + } + if( c2==0 || (seen ^ invert)==0 ){ + return 0; + } + }else if( esc==c && !prevEscape ){ + prevEscape = 1; + }else{ + c2 = sqlite3Utf8Read(zString, &zString); + if( noCase ){ + GlogUpperToLower(c); + GlogUpperToLower(c2); + } + if( c!=c2 ){ + return 0; + } + prevEscape = 0; + } + } + return *zString==0; +} + +/* +** Count the number of times that the LIKE operator (or GLOB which is +** just a variation of LIKE) gets called. This is used for testing +** only. +*/ +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_like_count = 0; +#endif + + +/* +** Implementation of the like() SQL function. This function implements +** the build-in LIKE operator. The first argument to the function is the +** pattern and the second argument is the string. So, the SQL statements: +** +** A LIKE B +** +** is implemented as like(B,A). +** +** This same function (with a different compareInfo structure) computes +** the GLOB operator. +*/ +static void likeFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *zA, *zB; + int escape = 0; + int nPat; + sqlite3 *db = sqlite3_context_db_handle(context); + + zB = sqlite3_value_text(argv[0]); + zA = sqlite3_value_text(argv[1]); + + /* Limit the length of the LIKE or GLOB pattern to avoid problems + ** of deep recursion and N*N behavior in patternCompare(). + */ + nPat = sqlite3_value_bytes(argv[0]); + testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ); + testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 ); + if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){ + sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); + return; + } + assert( zB==sqlite3_value_text(argv[0]) ); /* Encoding did not change */ + + if( argc==3 ){ + /* The escape character string must consist of a single UTF-8 character. + ** Otherwise, return an error. + */ + const unsigned char *zEsc = sqlite3_value_text(argv[2]); + if( zEsc==0 ) return; + if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){ + sqlite3_result_error(context, + "ESCAPE expression must be a single character", -1); + return; + } + escape = sqlite3Utf8Read(zEsc, &zEsc); + } + if( zA && zB ){ + struct compareInfo *pInfo = sqlite3_user_data(context); +#ifdef SQLITE_TEST + sqlite3_like_count++; +#endif + + sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape)); + } +} + +/* +** Implementation of the NULLIF(x,y) function. The result is the first +** argument if the arguments are different. The result is NULL if the +** arguments are equal to each other. +*/ +static void nullifFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + CollSeq *pColl = sqlite3GetFuncCollSeq(context); + UNUSED_PARAMETER(NotUsed); + if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){ + sqlite3_result_value(context, argv[0]); + } +} + +/* +** Implementation of the sqlite_version() function. The result is the version +** of the SQLite library that is running. +*/ +static void versionFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **NotUsed2 +){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + /* IMP: R-48699-48617 This function is an SQL wrapper around the + ** sqlite3_libversion() C-interface. */ + sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC); +} + +/* +** Implementation of the sqlite_source_id() function. The result is a string +** that identifies the particular version of the source code used to build +** SQLite. +*/ +static void sourceidFunc( + sqlite3_context *context, + int NotUsed, + sqlite3_value **NotUsed2 +){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + /* IMP: R-24470-31136 This function is an SQL wrapper around the + ** sqlite3_sourceid() C interface. */ + sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC); +} + +/* +** Implementation of the sqlite_compileoption_used() function. +** The result is an integer that identifies if the compiler option +** was used to build SQLite. +*/ +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +static void compileoptionusedFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *zOptName; + assert( argc==1 ); + UNUSED_PARAMETER(argc); + /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL + ** function is a wrapper around the sqlite3_compileoption_used() C/C++ + ** function. + */ + if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){ + sqlite3_result_int(context, sqlite3_compileoption_used(zOptName)); + } +} +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ + +/* +** Implementation of the sqlite_compileoption_get() function. +** The result is a string that identifies the compiler options +** used to build SQLite. +*/ +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +static void compileoptiongetFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int n; + assert( argc==1 ); + UNUSED_PARAMETER(argc); + /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function + ** is a wrapper around the sqlite3_compileoption_get() C/C++ function. + */ + n = sqlite3_value_int(argv[0]); + sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC); +} +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ + +/* Array for converting from half-bytes (nybbles) into ASCII hex +** digits. */ +static const char hexdigits[] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' +}; + +/* +** EXPERIMENTAL - This is not an official function. The interface may +** change. This function may disappear. Do not write code that depends +** on this function. +** +** Implementation of the QUOTE() function. This function takes a single +** argument. If the argument is numeric, the return value is the same as +** the argument. If the argument is NULL, the return value is the string +** "NULL". Otherwise, the argument is enclosed in single quotes with +** single-quote escapes. +*/ +static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ + assert( argc==1 ); + UNUSED_PARAMETER(argc); + switch( sqlite3_value_type(argv[0]) ){ + case SQLITE_INTEGER: + case SQLITE_FLOAT: { + sqlite3_result_value(context, argv[0]); + break; + } + case SQLITE_BLOB: { + char *zText = 0; + char const *zBlob = sqlite3_value_blob(argv[0]); + int nBlob = sqlite3_value_bytes(argv[0]); + assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ + zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4); + if( zText ){ + int i; + for(i=0; i>4)&0x0F]; + zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F]; + } + zText[(nBlob*2)+2] = '\''; + zText[(nBlob*2)+3] = '\0'; + zText[0] = 'X'; + zText[1] = '\''; + sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT); + sqlite3_free(zText); + } + break; + } + case SQLITE_TEXT: { + int i,j; + u64 n; + const unsigned char *zArg = sqlite3_value_text(argv[0]); + char *z; + + if( zArg==0 ) return; + for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; } + z = contextMalloc(context, ((i64)i)+((i64)n)+3); + if( z ){ + z[0] = '\''; + for(i=0, j=1; zArg[i]; i++){ + z[j++] = zArg[i]; + if( zArg[i]=='\'' ){ + z[j++] = '\''; + } + } + z[j++] = '\''; + z[j] = 0; + sqlite3_result_text(context, z, j, sqlite3_free); + } + break; + } + default: { + assert( sqlite3_value_type(argv[0])==SQLITE_NULL ); + sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC); + break; + } + } +} + +/* +** The hex() function. Interpret the argument as a blob. Return +** a hexadecimal rendering as text. +*/ +static void hexFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int i, n; + const unsigned char *pBlob; + char *zHex, *z; + assert( argc==1 ); + UNUSED_PARAMETER(argc); + pBlob = sqlite3_value_blob(argv[0]); + n = sqlite3_value_bytes(argv[0]); + assert( pBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */ + z = zHex = contextMalloc(context, ((i64)n)*2 + 1); + if( zHex ){ + for(i=0; i>4)&0xf]; + *(z++) = hexdigits[c&0xf]; + } + *z = 0; + sqlite3_result_text(context, zHex, n*2, sqlite3_free); + } +} + +/* +** The zeroblob(N) function returns a zero-filled blob of size N bytes. +*/ +static void zeroblobFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + i64 n; + sqlite3 *db = sqlite3_context_db_handle(context); + assert( argc==1 ); + UNUSED_PARAMETER(argc); + n = sqlite3_value_int64(argv[0]); + testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] ); + testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 ); + if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){ + sqlite3_result_error_toobig(context); + }else{ + sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */ + } +} + +/* +** The replace() function. Three arguments are all strings: call +** them A, B, and C. The result is also a string which is derived +** from A by replacing every occurance of B with C. The match +** must be exact. Collating sequences are not used. +*/ +static void replaceFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *zStr; /* The input string A */ + const unsigned char *zPattern; /* The pattern string B */ + const unsigned char *zRep; /* The replacement string C */ + unsigned char *zOut; /* The output */ + int nStr; /* Size of zStr */ + int nPattern; /* Size of zPattern */ + int nRep; /* Size of zRep */ + i64 nOut; /* Maximum size of zOut */ + int loopLimit; /* Last zStr[] that might match zPattern[] */ + int i, j; /* Loop counters */ + + assert( argc==3 ); + UNUSED_PARAMETER(argc); + zStr = sqlite3_value_text(argv[0]); + if( zStr==0 ) return; + nStr = sqlite3_value_bytes(argv[0]); + assert( zStr==sqlite3_value_text(argv[0]) ); /* No encoding change */ + zPattern = sqlite3_value_text(argv[1]); + if( zPattern==0 ){ + assert( sqlite3_value_type(argv[1])==SQLITE_NULL + || sqlite3_context_db_handle(context)->mallocFailed ); + return; + } + if( zPattern[0]==0 ){ + assert( sqlite3_value_type(argv[1])!=SQLITE_NULL ); + sqlite3_result_value(context, argv[0]); + return; + } + nPattern = sqlite3_value_bytes(argv[1]); + assert( zPattern==sqlite3_value_text(argv[1]) ); /* No encoding change */ + zRep = sqlite3_value_text(argv[2]); + if( zRep==0 ) return; + nRep = sqlite3_value_bytes(argv[2]); + assert( zRep==sqlite3_value_text(argv[2]) ); + nOut = nStr + 1; + assert( nOutaLimit[SQLITE_LIMIT_LENGTH] ); + testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] ); + if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){ + sqlite3_result_error_toobig(context); + sqlite3_free(zOut); + return; + } + zOld = zOut; + zOut = sqlite3_realloc(zOut, (int)nOut); + if( zOut==0 ){ + sqlite3_result_error_nomem(context); + sqlite3_free(zOld); + return; + } + memcpy(&zOut[j], zRep, nRep); + j += nRep; + i += nPattern-1; + } + } + assert( j+nStr-i+1==nOut ); + memcpy(&zOut[j], &zStr[i], nStr-i); + j += nStr - i; + assert( j<=nOut ); + zOut[j] = 0; + sqlite3_result_text(context, (char*)zOut, j, sqlite3_free); +} + +/* +** Implementation of the TRIM(), LTRIM(), and RTRIM() functions. +** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both. +*/ +static void trimFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *zIn; /* Input string */ + const unsigned char *zCharSet; /* Set of characters to trim */ + int nIn; /* Number of bytes in input */ + int flags; /* 1: trimleft 2: trimright 3: trim */ + int i; /* Loop counter */ + unsigned char *aLen = 0; /* Length of each character in zCharSet */ + unsigned char **azChar = 0; /* Individual characters in zCharSet */ + int nChar; /* Number of characters in zCharSet */ + + if( sqlite3_value_type(argv[0])==SQLITE_NULL ){ + return; + } + zIn = sqlite3_value_text(argv[0]); + if( zIn==0 ) return; + nIn = sqlite3_value_bytes(argv[0]); + assert( zIn==sqlite3_value_text(argv[0]) ); + if( argc==1 ){ + static const unsigned char lenOne[] = { 1 }; + static unsigned char * const azOne[] = { (u8*)" " }; + nChar = 1; + aLen = (u8*)lenOne; + azChar = (unsigned char **)azOne; + zCharSet = 0; + }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){ + return; + }else{ + const unsigned char *z; + for(z=zCharSet, nChar=0; *z; nChar++){ + SQLITE_SKIP_UTF8(z); + } + if( nChar>0 ){ + azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1)); + if( azChar==0 ){ + return; + } + aLen = (unsigned char*)&azChar[nChar]; + for(z=zCharSet, nChar=0; *z; nChar++){ + azChar[nChar] = (unsigned char *)z; + SQLITE_SKIP_UTF8(z); + aLen[nChar] = (u8)(z - azChar[nChar]); + } + } + } + if( nChar>0 ){ + flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context)); + if( flags & 1 ){ + while( nIn>0 ){ + int len = 0; + for(i=0; i=nChar ) break; + zIn += len; + nIn -= len; + } + } + if( flags & 2 ){ + while( nIn>0 ){ + int len = 0; + for(i=0; i=nChar ) break; + nIn -= len; + } + } + if( zCharSet ){ + sqlite3_free(azChar); + } + } + sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT); +} + + +/* IMP: R-25361-16150 This function is omitted from SQLite by default. It +** is only available if the SQLITE_SOUNDEX compile-time option is used +** when SQLite is built. +*/ +#ifdef SQLITE_SOUNDEX +/* +** Compute the soundex encoding of a word. +** +** IMP: R-59782-00072 The soundex(X) function returns a string that is the +** soundex encoding of the string X. +*/ +static void soundexFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + char zResult[8]; + const u8 *zIn; + int i, j; + static const unsigned char iCode[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, + 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0, + 1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0, + }; + assert( argc==1 ); + zIn = (u8*)sqlite3_value_text(argv[0]); + if( zIn==0 ) zIn = (u8*)""; + for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){} + if( zIn[i] ){ + u8 prevcode = iCode[zIn[i]&0x7f]; + zResult[0] = sqlite3Toupper(zIn[i]); + for(j=1; j<4 && zIn[i]; i++){ + int code = iCode[zIn[i]&0x7f]; + if( code>0 ){ + if( code!=prevcode ){ + prevcode = code; + zResult[j++] = code + '0'; + } + }else{ + prevcode = 0; + } + } + while( j<4 ){ + zResult[j++] = '0'; + } + zResult[j] = 0; + sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT); + }else{ + /* IMP: R-64894-50321 The string "?000" is returned if the argument + ** is NULL or contains no ASCII alphabetic characters. */ + sqlite3_result_text(context, "?000", 4, SQLITE_STATIC); + } +} +#endif /* SQLITE_SOUNDEX */ + +#ifndef SQLITE_OMIT_LOAD_EXTENSION +/* +** A function that loads a shared-library extension then returns NULL. +*/ +static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){ + const char *zFile = (const char *)sqlite3_value_text(argv[0]); + const char *zProc; + sqlite3 *db = sqlite3_context_db_handle(context); + char *zErrMsg = 0; + + if( argc==2 ){ + zProc = (const char *)sqlite3_value_text(argv[1]); + }else{ + zProc = 0; + } + if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){ + sqlite3_result_error(context, zErrMsg, -1); + sqlite3_free(zErrMsg); + } +} +#endif + + +/* +** An instance of the following structure holds the context of a +** sum() or avg() aggregate computation. +*/ +typedef struct SumCtx SumCtx; +struct SumCtx { + double rSum; /* Floating point sum */ + i64 iSum; /* Integer sum */ + i64 cnt; /* Number of elements summed */ + u8 overflow; /* True if integer overflow seen */ + u8 approx; /* True if non-integer value was input to the sum */ +}; + +/* +** Routines used to compute the sum, average, and total. +** +** The SUM() function follows the (broken) SQL standard which means +** that it returns NULL if it sums over no inputs. TOTAL returns +** 0.0 in that case. In addition, TOTAL always returns a float where +** SUM might return an integer if it never encounters a floating point +** value. TOTAL never fails, but SUM might through an exception if +** it overflows an integer. +*/ +static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){ + SumCtx *p; + int type; + assert( argc==1 ); + UNUSED_PARAMETER(argc); + p = sqlite3_aggregate_context(context, sizeof(*p)); + type = sqlite3_value_numeric_type(argv[0]); + if( p && type!=SQLITE_NULL ){ + p->cnt++; + if( type==SQLITE_INTEGER ){ + i64 v = sqlite3_value_int64(argv[0]); + p->rSum += v; + if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){ + p->overflow = 1; + } + }else{ + p->rSum += sqlite3_value_double(argv[0]); + p->approx = 1; + } + } +} +static void sumFinalize(sqlite3_context *context){ + SumCtx *p; + p = sqlite3_aggregate_context(context, 0); + if( p && p->cnt>0 ){ + if( p->overflow ){ + sqlite3_result_error(context,"integer overflow",-1); + }else if( p->approx ){ + sqlite3_result_double(context, p->rSum); + }else{ + sqlite3_result_int64(context, p->iSum); + } + } +} +static void avgFinalize(sqlite3_context *context){ + SumCtx *p; + p = sqlite3_aggregate_context(context, 0); + if( p && p->cnt>0 ){ + sqlite3_result_double(context, p->rSum/(double)p->cnt); + } +} +static void totalFinalize(sqlite3_context *context){ + SumCtx *p; + p = sqlite3_aggregate_context(context, 0); + /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ + sqlite3_result_double(context, p ? p->rSum : (double)0); +} + +/* +** The following structure keeps track of state information for the +** count() aggregate function. +*/ +typedef struct CountCtx CountCtx; +struct CountCtx { + i64 n; +}; + +/* +** Routines to implement the count() aggregate function. +*/ +static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){ + CountCtx *p; + p = sqlite3_aggregate_context(context, sizeof(*p)); + if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){ + p->n++; + } + +#ifndef SQLITE_OMIT_DEPRECATED + /* The sqlite3_aggregate_count() function is deprecated. But just to make + ** sure it still operates correctly, verify that its count agrees with our + ** internal count when using count(*) and when the total count can be + ** expressed as a 32-bit integer. */ + assert( argc==1 || p==0 || p->n>0x7fffffff + || p->n==sqlite3_aggregate_count(context) ); +#endif +} +static void countFinalize(sqlite3_context *context){ + CountCtx *p; + p = sqlite3_aggregate_context(context, 0); + sqlite3_result_int64(context, p ? p->n : 0); +} + +/* +** Routines to implement min() and max() aggregate functions. +*/ +static void minmaxStep( + sqlite3_context *context, + int NotUsed, + sqlite3_value **argv +){ + Mem *pArg = (Mem *)argv[0]; + Mem *pBest; + UNUSED_PARAMETER(NotUsed); + + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest)); + if( !pBest ) return; + + if( pBest->flags ){ + int max; + int cmp; + CollSeq *pColl = sqlite3GetFuncCollSeq(context); + /* This step function is used for both the min() and max() aggregates, + ** the only difference between the two being that the sense of the + ** comparison is inverted. For the max() aggregate, the + ** sqlite3_user_data() function returns (void *)-1. For min() it + ** returns (void *)db, where db is the sqlite3* database pointer. + ** Therefore the next statement sets variable 'max' to 1 for the max() + ** aggregate, or 0 for min(). + */ + max = sqlite3_user_data(context)!=0; + cmp = sqlite3MemCompare(pBest, pArg, pColl); + if( (max && cmp<0) || (!max && cmp>0) ){ + sqlite3VdbeMemCopy(pBest, pArg); + } + }else{ + sqlite3VdbeMemCopy(pBest, pArg); + } +} +static void minMaxFinalize(sqlite3_context *context){ + sqlite3_value *pRes; + pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0); + if( pRes ){ + if( ALWAYS(pRes->flags) ){ + sqlite3_result_value(context, pRes); + } + sqlite3VdbeMemRelease(pRes); + } +} + +/* +** group_concat(EXPR, ?SEPARATOR?) +*/ +static void groupConcatStep( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const char *zVal; + StrAccum *pAccum; + const char *zSep; + int nVal, nSep; + assert( argc==1 || argc==2 ); + if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; + pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum)); + + if( pAccum ){ + sqlite3 *db = sqlite3_context_db_handle(context); + int firstTerm = pAccum->useMalloc==0; + pAccum->useMalloc = 2; + pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH]; + if( !firstTerm ){ + if( argc==2 ){ + zSep = (char*)sqlite3_value_text(argv[1]); + nSep = sqlite3_value_bytes(argv[1]); + }else{ + zSep = ","; + nSep = 1; + } + sqlite3StrAccumAppend(pAccum, zSep, nSep); + } + zVal = (char*)sqlite3_value_text(argv[0]); + nVal = sqlite3_value_bytes(argv[0]); + sqlite3StrAccumAppend(pAccum, zVal, nVal); + } +} +static void groupConcatFinalize(sqlite3_context *context){ + StrAccum *pAccum; + pAccum = sqlite3_aggregate_context(context, 0); + if( pAccum ){ + if( pAccum->tooBig ){ + sqlite3_result_error_toobig(context); + }else if( pAccum->mallocFailed ){ + sqlite3_result_error_nomem(context); + }else{ + sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1, + sqlite3_free); + } + } +} + +/* +** This routine does per-connection function registration. Most +** of the built-in functions above are part of the global function set. +** This routine only deals with those that are not global. +*/ +SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){ + int rc = sqlite3_overload_function(db, "MATCH", 2); + assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); + if( rc==SQLITE_NOMEM ){ + db->mallocFailed = 1; + } +} + +/* +** Set the LIKEOPT flag on the 2-argument function with the given name. +*/ +static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){ + FuncDef *pDef; + pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName), + 2, SQLITE_UTF8, 0); + if( ALWAYS(pDef) ){ + pDef->flags = flagVal; + } +} + +/* +** Register the built-in LIKE and GLOB functions. The caseSensitive +** parameter determines whether or not the LIKE operator is case +** sensitive. GLOB is always case sensitive. +*/ +SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){ + struct compareInfo *pInfo; + if( caseSensitive ){ + pInfo = (struct compareInfo*)&likeInfoAlt; + }else{ + pInfo = (struct compareInfo*)&likeInfoNorm; + } + sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0); + sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0); + sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8, + (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0); + setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE); + setLikeOptFlag(db, "like", + caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE); +} + +/* +** pExpr points to an expression which implements a function. If +** it is appropriate to apply the LIKE optimization to that function +** then set aWc[0] through aWc[2] to the wildcard characters and +** return TRUE. If the function is not a LIKE-style function then +** return FALSE. +*/ +SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){ + FuncDef *pDef; + if( pExpr->op!=TK_FUNCTION + || !pExpr->x.pList + || pExpr->x.pList->nExpr!=2 + ){ + return 0; + } + assert( !ExprHasProperty(pExpr, EP_xIsSelect) ); + pDef = sqlite3FindFunction(db, pExpr->u.zToken, + sqlite3Strlen30(pExpr->u.zToken), + 2, SQLITE_UTF8, 0); + if( NEVER(pDef==0) || (pDef->flags & SQLITE_FUNC_LIKE)==0 ){ + return 0; + } + + /* The memcpy() statement assumes that the wildcard characters are + ** the first three statements in the compareInfo structure. The + ** asserts() that follow verify that assumption + */ + memcpy(aWc, pDef->pUserData, 3); + assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll ); + assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne ); + assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet ); + *pIsNocase = (pDef->flags & SQLITE_FUNC_CASE)==0; + return 1; +} + +/* +** All all of the FuncDef structures in the aBuiltinFunc[] array above +** to the global function hash table. This occurs at start-time (as +** a consequence of calling sqlite3_initialize()). +** +** After this routine runs +*/ +SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){ + /* + ** The following array holds FuncDef structures for all of the functions + ** defined in this file. + ** + ** The array cannot be constant since changes are made to the + ** FuncDef.pHash elements at start-time. The elements of this array + ** are read-only after initialization is complete. + */ + static SQLITE_WSD FuncDef aBuiltinFunc[] = { + FUNCTION(ltrim, 1, 1, 0, trimFunc ), + FUNCTION(ltrim, 2, 1, 0, trimFunc ), + FUNCTION(rtrim, 1, 2, 0, trimFunc ), + FUNCTION(rtrim, 2, 2, 0, trimFunc ), + FUNCTION(trim, 1, 3, 0, trimFunc ), + FUNCTION(trim, 2, 3, 0, trimFunc ), + FUNCTION(min, -1, 0, 1, minmaxFunc ), + FUNCTION(min, 0, 0, 1, 0 ), + AGGREGATE(min, 1, 0, 1, minmaxStep, minMaxFinalize ), + FUNCTION(max, -1, 1, 1, minmaxFunc ), + FUNCTION(max, 0, 1, 1, 0 ), + AGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize ), + FUNCTION(typeof, 1, 0, 0, typeofFunc ), + FUNCTION(length, 1, 0, 0, lengthFunc ), + FUNCTION(substr, 2, 0, 0, substrFunc ), + FUNCTION(substr, 3, 0, 0, substrFunc ), + FUNCTION(abs, 1, 0, 0, absFunc ), +#ifndef SQLITE_OMIT_FLOATING_POINT + FUNCTION(round, 1, 0, 0, roundFunc ), + FUNCTION(round, 2, 0, 0, roundFunc ), +#endif + FUNCTION(upper, 1, 0, 0, upperFunc ), + FUNCTION(lower, 1, 0, 0, lowerFunc ), + FUNCTION(coalesce, 1, 0, 0, 0 ), + FUNCTION(coalesce, 0, 0, 0, 0 ), +/* FUNCTION(coalesce, -1, 0, 0, ifnullFunc ), */ + {-1,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"coalesce",0,0}, + FUNCTION(hex, 1, 0, 0, hexFunc ), +/* FUNCTION(ifnull, 2, 0, 0, ifnullFunc ), */ + {2,SQLITE_UTF8,SQLITE_FUNC_COALESCE,0,0,ifnullFunc,0,0,"ifnull",0,0}, + FUNCTION(random, 0, 0, 0, randomFunc ), + FUNCTION(randomblob, 1, 0, 0, randomBlob ), + FUNCTION(nullif, 2, 0, 1, nullifFunc ), + FUNCTION(sqlite_version, 0, 0, 0, versionFunc ), + FUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ), +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS + FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc ), + FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc ), +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ + FUNCTION(quote, 1, 0, 0, quoteFunc ), + FUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid), + FUNCTION(changes, 0, 0, 0, changes ), + FUNCTION(total_changes, 0, 0, 0, total_changes ), + FUNCTION(replace, 3, 0, 0, replaceFunc ), + FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ), + #ifdef SQLITE_SOUNDEX + FUNCTION(soundex, 1, 0, 0, soundexFunc ), + #endif + #ifndef SQLITE_OMIT_LOAD_EXTENSION + FUNCTION(load_extension, 1, 0, 0, loadExt ), + FUNCTION(load_extension, 2, 0, 0, loadExt ), + #endif + AGGREGATE(sum, 1, 0, 0, sumStep, sumFinalize ), + AGGREGATE(total, 1, 0, 0, sumStep, totalFinalize ), + AGGREGATE(avg, 1, 0, 0, sumStep, avgFinalize ), + /* AGGREGATE(count, 0, 0, 0, countStep, countFinalize ), */ + {0,SQLITE_UTF8,SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0}, + AGGREGATE(count, 1, 0, 0, countStep, countFinalize ), + AGGREGATE(group_concat, 1, 0, 0, groupConcatStep, groupConcatFinalize), + AGGREGATE(group_concat, 2, 0, 0, groupConcatStep, groupConcatFinalize), + + LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), + #ifdef SQLITE_CASE_SENSITIVE_LIKE + LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), + LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE), + #else + LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE), + LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE), + #endif + }; + + int i; + FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); + FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc); + + for(i=0; idb->mallocFailed flag is set. +*/ +static int locateFkeyIndex( + Parse *pParse, /* Parse context to store any error in */ + Table *pParent, /* Parent table of FK constraint pFKey */ + FKey *pFKey, /* Foreign key to find index for */ + Index **ppIdx, /* OUT: Unique index on parent table */ + int **paiCol /* OUT: Map of index columns in pFKey */ +){ + Index *pIdx = 0; /* Value to return via *ppIdx */ + int *aiCol = 0; /* Value to return via *paiCol */ + int nCol = pFKey->nCol; /* Number of columns in parent key */ + char *zKey = pFKey->aCol[0].zCol; /* Name of left-most parent key column */ + + /* The caller is responsible for zeroing output parameters. */ + assert( ppIdx && *ppIdx==0 ); + assert( !paiCol || *paiCol==0 ); + assert( pParse ); + + /* If this is a non-composite (single column) foreign key, check if it + ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx + ** and *paiCol set to zero and return early. + ** + ** Otherwise, for a composite foreign key (more than one column), allocate + ** space for the aiCol array (returned via output parameter *paiCol). + ** Non-composite foreign keys do not require the aiCol array. + */ + if( nCol==1 ){ + /* The FK maps to the IPK if any of the following are true: + ** + ** 1) There is an INTEGER PRIMARY KEY column and the FK is implicitly + ** mapped to the primary key of table pParent, or + ** 2) The FK is explicitly mapped to a column declared as INTEGER + ** PRIMARY KEY. + */ + if( pParent->iPKey>=0 ){ + if( !zKey ) return 0; + if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0; + } + }else if( paiCol ){ + assert( nCol>1 ); + aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int)); + if( !aiCol ) return 1; + *paiCol = aiCol; + } + + for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->nColumn==nCol && pIdx->onError!=OE_None ){ + /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number + ** of columns. If each indexed column corresponds to a foreign key + ** column of pFKey, then this index is a winner. */ + + if( zKey==0 ){ + /* If zKey is NULL, then this foreign key is implicitly mapped to + ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be + ** identified by the test (Index.autoIndex==2). */ + if( pIdx->autoIndex==2 ){ + if( aiCol ){ + int i; + for(i=0; iaCol[i].iFrom; + } + break; + } + }else{ + /* If zKey is non-NULL, then this foreign key was declared to + ** map to an explicit list of columns in table pParent. Check if this + ** index matches those columns. Also, check that the index uses + ** the default collation sequences for each column. */ + int i, j; + for(i=0; iaiColumn[i]; /* Index of column in parent tbl */ + char *zDfltColl; /* Def. collation for column */ + char *zIdxCol; /* Name of indexed column */ + + /* If the index uses a collation sequence that is different from + ** the default collation sequence for the column, this index is + ** unusable. Bail out early in this case. */ + zDfltColl = pParent->aCol[iCol].zColl; + if( !zDfltColl ){ + zDfltColl = "BINARY"; + } + if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break; + + zIdxCol = pParent->aCol[iCol].zName; + for(j=0; jaCol[j].zCol, zIdxCol)==0 ){ + if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom; + break; + } + } + if( j==nCol ) break; + } + if( i==nCol ) break; /* pIdx is usable */ + } + } + } + + if( !pIdx ){ + if( !pParse->disableTriggers ){ + sqlite3ErrorMsg(pParse, "foreign key mismatch"); + } + sqlite3DbFree(pParse->db, aiCol); + return 1; + } + + *ppIdx = pIdx; + return 0; +} + +/* +** This function is called when a row is inserted into or deleted from the +** child table of foreign key constraint pFKey. If an SQL UPDATE is executed +** on the child table of pFKey, this function is invoked twice for each row +** affected - once to "delete" the old row, and then again to "insert" the +** new row. +** +** Each time it is called, this function generates VDBE code to locate the +** row in the parent table that corresponds to the row being inserted into +** or deleted from the child table. If the parent row can be found, no +** special action is taken. Otherwise, if the parent row can *not* be +** found in the parent table: +** +** Operation | FK type | Action taken +** -------------------------------------------------------------------------- +** INSERT immediate Increment the "immediate constraint counter". +** +** DELETE immediate Decrement the "immediate constraint counter". +** +** INSERT deferred Increment the "deferred constraint counter". +** +** DELETE deferred Decrement the "deferred constraint counter". +** +** These operations are identified in the comment at the top of this file +** (fkey.c) as "I.1" and "D.1". +*/ +static void fkLookupParent( + Parse *pParse, /* Parse context */ + int iDb, /* Index of database housing pTab */ + Table *pTab, /* Parent table of FK pFKey */ + Index *pIdx, /* Unique index on parent key columns in pTab */ + FKey *pFKey, /* Foreign key constraint */ + int *aiCol, /* Map from parent key columns to child table columns */ + int regData, /* Address of array containing child table row */ + int nIncr, /* Increment constraint counter by this */ + int isIgnore /* If true, pretend pTab contains all NULL values */ +){ + int i; /* Iterator variable */ + Vdbe *v = sqlite3GetVdbe(pParse); /* Vdbe to add code to */ + int iCur = pParse->nTab - 1; /* Cursor number to use */ + int iOk = sqlite3VdbeMakeLabel(v); /* jump here if parent key found */ + + /* If nIncr is less than zero, then check at runtime if there are any + ** outstanding constraints to resolve. If there are not, there is no need + ** to check if deleting this row resolves any outstanding violations. + ** + ** Check if any of the key columns in the child table row are NULL. If + ** any are, then the constraint is considered satisfied. No need to + ** search for a matching row in the parent table. */ + if( nIncr<0 ){ + sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk); + } + for(i=0; inCol; i++){ + int iReg = aiCol[i] + regData + 1; + sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk); + } + + if( isIgnore==0 ){ + if( pIdx==0 ){ + /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY + ** column of the parent table (table pTab). */ + int iMustBeInt; /* Address of MustBeInt instruction */ + int regTemp = sqlite3GetTempReg(pParse); + + /* Invoke MustBeInt to coerce the child key value to an integer (i.e. + ** apply the affinity of the parent key). If this fails, then there + ** is no matching parent key. Before using MustBeInt, make a copy of + ** the value. Otherwise, the value inserted into the child key column + ** will have INTEGER affinity applied to it, which may not be correct. */ + sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp); + iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0); + + /* If the parent table is the same as the child table, and we are about + ** to increment the constraint-counter (i.e. this is an INSERT operation), + ** then check if the row being inserted matches itself. If so, do not + ** increment the constraint-counter. */ + if( pTab==pFKey->pFrom && nIncr==1 ){ + sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp); + } + + sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp); + sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk); + sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2); + sqlite3VdbeJumpHere(v, iMustBeInt); + sqlite3ReleaseTempReg(pParse, regTemp); + }else{ + int nCol = pFKey->nCol; + int regTemp = sqlite3GetTempRange(pParse, nCol); + int regRec = sqlite3GetTempReg(pParse); + KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); + + sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb); + sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF); + for(i=0; ipFrom && nIncr==1 ){ + int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1; + for(i=0; iaiColumn[i]+1+regData; + sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent); + } + sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk); + } + + sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec); + sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0); + sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0); + + sqlite3ReleaseTempReg(pParse, regRec); + sqlite3ReleaseTempRange(pParse, regTemp, nCol); + } + } + + if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){ + /* Special case: If this is an INSERT statement that will insert exactly + ** one row into the table, raise a constraint immediately instead of + ** incrementing a counter. This is necessary as the VM code is being + ** generated for will not open a statement transaction. */ + assert( nIncr==1 ); + sqlite3HaltConstraint( + pParse, OE_Abort, "foreign key constraint failed", P4_STATIC + ); + }else{ + if( nIncr>0 && pFKey->isDeferred==0 ){ + sqlite3ParseToplevel(pParse)->mayAbort = 1; + } + sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr); + } + + sqlite3VdbeResolveLabel(v, iOk); + sqlite3VdbeAddOp1(v, OP_Close, iCur); +} + +/* +** This function is called to generate code executed when a row is deleted +** from the parent table of foreign key constraint pFKey and, if pFKey is +** deferred, when a row is inserted into the same table. When generating +** code for an SQL UPDATE operation, this function may be called twice - +** once to "delete" the old row and once to "insert" the new row. +** +** The code generated by this function scans through the rows in the child +** table that correspond to the parent table row being deleted or inserted. +** For each child row found, one of the following actions is taken: +** +** Operation | FK type | Action taken +** -------------------------------------------------------------------------- +** DELETE immediate Increment the "immediate constraint counter". +** Or, if the ON (UPDATE|DELETE) action is RESTRICT, +** throw a "foreign key constraint failed" exception. +** +** INSERT immediate Decrement the "immediate constraint counter". +** +** DELETE deferred Increment the "deferred constraint counter". +** Or, if the ON (UPDATE|DELETE) action is RESTRICT, +** throw a "foreign key constraint failed" exception. +** +** INSERT deferred Decrement the "deferred constraint counter". +** +** These operations are identified in the comment at the top of this file +** (fkey.c) as "I.2" and "D.2". +*/ +static void fkScanChildren( + Parse *pParse, /* Parse context */ + SrcList *pSrc, /* SrcList containing the table to scan */ + Table *pTab, + Index *pIdx, /* Foreign key index */ + FKey *pFKey, /* Foreign key relationship */ + int *aiCol, /* Map from pIdx cols to child table cols */ + int regData, /* Referenced table data starts here */ + int nIncr /* Amount to increment deferred counter by */ +){ + sqlite3 *db = pParse->db; /* Database handle */ + int i; /* Iterator variable */ + Expr *pWhere = 0; /* WHERE clause to scan with */ + NameContext sNameContext; /* Context used to resolve WHERE clause */ + WhereInfo *pWInfo; /* Context used by sqlite3WhereXXX() */ + int iFkIfZero = 0; /* Address of OP_FkIfZero */ + Vdbe *v = sqlite3GetVdbe(pParse); + + assert( !pIdx || pIdx->pTable==pTab ); + + if( nIncr<0 ){ + iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0); + } + + /* Create an Expr object representing an SQL expression like: + ** + ** = AND = ... + ** + ** The collation sequence used for the comparison should be that of + ** the parent key columns. The affinity of the parent key column should + ** be applied to each child key value before the comparison takes place. + */ + for(i=0; inCol; i++){ + Expr *pLeft; /* Value from parent table row */ + Expr *pRight; /* Column ref to child table */ + Expr *pEq; /* Expression (pLeft = pRight) */ + int iCol; /* Index of column in child table */ + const char *zCol; /* Name of column in child table */ + + pLeft = sqlite3Expr(db, TK_REGISTER, 0); + if( pLeft ){ + /* Set the collation sequence and affinity of the LHS of each TK_EQ + ** expression to the parent key column defaults. */ + if( pIdx ){ + Column *pCol; + iCol = pIdx->aiColumn[i]; + pCol = &pTab->aCol[iCol]; + if( pTab->iPKey==iCol ) iCol = -1; + pLeft->iTable = regData+iCol+1; + pLeft->affinity = pCol->affinity; + pLeft->pColl = sqlite3LocateCollSeq(pParse, pCol->zColl); + }else{ + pLeft->iTable = regData; + pLeft->affinity = SQLITE_AFF_INTEGER; + } + } + iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; + assert( iCol>=0 ); + zCol = pFKey->pFrom->aCol[iCol].zName; + pRight = sqlite3Expr(db, TK_ID, zCol); + pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0); + pWhere = sqlite3ExprAnd(db, pWhere, pEq); + } + + /* If the child table is the same as the parent table, and this scan + ** is taking place as part of a DELETE operation (operation D.2), omit the + ** row being deleted from the scan by adding ($rowid != rowid) to the WHERE + ** clause, where $rowid is the rowid of the row being deleted. */ + if( pTab==pFKey->pFrom && nIncr>0 ){ + Expr *pEq; /* Expression (pLeft = pRight) */ + Expr *pLeft; /* Value from parent table row */ + Expr *pRight; /* Column ref to child table */ + pLeft = sqlite3Expr(db, TK_REGISTER, 0); + pRight = sqlite3Expr(db, TK_COLUMN, 0); + if( pLeft && pRight ){ + pLeft->iTable = regData; + pLeft->affinity = SQLITE_AFF_INTEGER; + pRight->iTable = pSrc->a[0].iCursor; + pRight->iColumn = -1; + } + pEq = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0); + pWhere = sqlite3ExprAnd(db, pWhere, pEq); + } + + /* Resolve the references in the WHERE clause. */ + memset(&sNameContext, 0, sizeof(NameContext)); + sNameContext.pSrcList = pSrc; + sNameContext.pParse = pParse; + sqlite3ResolveExprNames(&sNameContext, pWhere); + + /* Create VDBE to loop through the entries in pSrc that match the WHERE + ** clause. If the constraint is not deferred, throw an exception for + ** each row found. Otherwise, for deferred constraints, increment the + ** deferred constraint counter by nIncr for each row selected. */ + pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0); + if( nIncr>0 && pFKey->isDeferred==0 ){ + sqlite3ParseToplevel(pParse)->mayAbort = 1; + } + sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr); + if( pWInfo ){ + sqlite3WhereEnd(pWInfo); + } + + /* Clean up the WHERE clause constructed above. */ + sqlite3ExprDelete(db, pWhere); + if( iFkIfZero ){ + sqlite3VdbeJumpHere(v, iFkIfZero); + } +} + +/* +** This function returns a pointer to the head of a linked list of FK +** constraints for which table pTab is the parent table. For example, +** given the following schema: +** +** CREATE TABLE t1(a PRIMARY KEY); +** CREATE TABLE t2(b REFERENCES t1(a); +** +** Calling this function with table "t1" as an argument returns a pointer +** to the FKey structure representing the foreign key constraint on table +** "t2". Calling this function with "t2" as the argument would return a +** NULL pointer (as there are no FK constraints for which t2 is the parent +** table). +*/ +SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){ + int nName = sqlite3Strlen30(pTab->zName); + return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName); +} + +/* +** The second argument is a Trigger structure allocated by the +** fkActionTrigger() routine. This function deletes the Trigger structure +** and all of its sub-components. +** +** The Trigger structure or any of its sub-components may be allocated from +** the lookaside buffer belonging to database handle dbMem. +*/ +static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){ + if( p ){ + TriggerStep *pStep = p->step_list; + sqlite3ExprDelete(dbMem, pStep->pWhere); + sqlite3ExprListDelete(dbMem, pStep->pExprList); + sqlite3SelectDelete(dbMem, pStep->pSelect); + sqlite3ExprDelete(dbMem, p->pWhen); + sqlite3DbFree(dbMem, p); + } +} + +/* +** This function is called to generate code that runs when table pTab is +** being dropped from the database. The SrcList passed as the second argument +** to this function contains a single entry guaranteed to resolve to +** table pTab. +** +** Normally, no code is required. However, if either +** +** (a) The table is the parent table of a FK constraint, or +** (b) The table is the child table of a deferred FK constraint and it is +** determined at runtime that there are outstanding deferred FK +** constraint violations in the database, +** +** then the equivalent of "DELETE FROM " is executed before dropping +** the table from the database. Triggers are disabled while running this +** DELETE, but foreign key actions are not. +*/ +SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){ + sqlite3 *db = pParse->db; + if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){ + int iSkip = 0; + Vdbe *v = sqlite3GetVdbe(pParse); + + assert( v ); /* VDBE has already been allocated */ + if( sqlite3FkReferences(pTab)==0 ){ + /* Search for a deferred foreign key constraint for which this table + ** is the child table. If one cannot be found, return without + ** generating any VDBE code. If one can be found, then jump over + ** the entire DELETE if there are no outstanding deferred constraints + ** when this statement is run. */ + FKey *p; + for(p=pTab->pFKey; p; p=p->pNextFrom){ + if( p->isDeferred ) break; + } + if( !p ) return; + iSkip = sqlite3VdbeMakeLabel(v); + sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip); + } + + pParse->disableTriggers = 1; + sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0); + pParse->disableTriggers = 0; + + /* If the DELETE has generated immediate foreign key constraint + ** violations, halt the VDBE and return an error at this point, before + ** any modifications to the schema are made. This is because statement + ** transactions are not able to rollback schema changes. */ + sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2); + sqlite3HaltConstraint( + pParse, OE_Abort, "foreign key constraint failed", P4_STATIC + ); + + if( iSkip ){ + sqlite3VdbeResolveLabel(v, iSkip); + } + } +} + +/* +** This function is called when inserting, deleting or updating a row of +** table pTab to generate VDBE code to perform foreign key constraint +** processing for the operation. +** +** For a DELETE operation, parameter regOld is passed the index of the +** first register in an array of (pTab->nCol+1) registers containing the +** rowid of the row being deleted, followed by each of the column values +** of the row being deleted, from left to right. Parameter regNew is passed +** zero in this case. +** +** For an INSERT operation, regOld is passed zero and regNew is passed the +** first register of an array of (pTab->nCol+1) registers containing the new +** row data. +** +** For an UPDATE operation, this function is called twice. Once before +** the original record is deleted from the table using the calling convention +** described for DELETE. Then again after the original record is deleted +** but before the new record is inserted using the INSERT convention. +*/ +SQLITE_PRIVATE void sqlite3FkCheck( + Parse *pParse, /* Parse context */ + Table *pTab, /* Row is being deleted from this table */ + int regOld, /* Previous row data is stored here */ + int regNew /* New row data is stored here */ +){ + sqlite3 *db = pParse->db; /* Database handle */ + FKey *pFKey; /* Used to iterate through FKs */ + int iDb; /* Index of database containing pTab */ + const char *zDb; /* Name of database containing pTab */ + int isIgnoreErrors = pParse->disableTriggers; + + /* Exactly one of regOld and regNew should be non-zero. */ + assert( (regOld==0)!=(regNew==0) ); + + /* If foreign-keys are disabled, this function is a no-op. */ + if( (db->flags&SQLITE_ForeignKeys)==0 ) return; + + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + zDb = db->aDb[iDb].zName; + + /* Loop through all the foreign key constraints for which pTab is the + ** child table (the table that the foreign key definition is part of). */ + for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){ + Table *pTo; /* Parent table of foreign key pFKey */ + Index *pIdx = 0; /* Index on key columns in pTo */ + int *aiFree = 0; + int *aiCol; + int iCol; + int i; + int isIgnore = 0; + + /* Find the parent table of this foreign key. Also find a unique index + ** on the parent key columns in the parent table. If either of these + ** schema items cannot be located, set an error in pParse and return + ** early. */ + if( pParse->disableTriggers ){ + pTo = sqlite3FindTable(db, pFKey->zTo, zDb); + }else{ + pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb); + } + if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){ + if( !isIgnoreErrors || db->mallocFailed ) return; + continue; + } + assert( pFKey->nCol==1 || (aiFree && pIdx) ); + + if( aiFree ){ + aiCol = aiFree; + }else{ + iCol = pFKey->aCol[0].iFrom; + aiCol = &iCol; + } + for(i=0; inCol; i++){ + if( aiCol[i]==pTab->iPKey ){ + aiCol[i] = -1; + } +#ifndef SQLITE_OMIT_AUTHORIZATION + /* Request permission to read the parent key columns. If the + ** authorization callback returns SQLITE_IGNORE, behave as if any + ** values read from the parent table are NULL. */ + if( db->xAuth ){ + int rcauth; + char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName; + rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb); + isIgnore = (rcauth==SQLITE_IGNORE); + } +#endif + } + + /* Take a shared-cache advisory read-lock on the parent table. Allocate + ** a cursor to use to search the unique index on the parent key columns + ** in the parent table. */ + sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName); + pParse->nTab++; + + if( regOld!=0 ){ + /* A row is being removed from the child table. Search for the parent. + ** If the parent does not exist, removing the child row resolves an + ** outstanding foreign key constraint violation. */ + fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore); + } + if( regNew!=0 ){ + /* A row is being added to the child table. If a parent row cannot + ** be found, adding the child row has violated the FK constraint. */ + fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore); + } + + sqlite3DbFree(db, aiFree); + } + + /* Loop through all the foreign key constraints that refer to this table */ + for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){ + Index *pIdx = 0; /* Foreign key index for pFKey */ + SrcList *pSrc; + int *aiCol = 0; + + if( !pFKey->isDeferred && !pParse->pToplevel && !pParse->isMultiWrite ){ + assert( regOld==0 && regNew!=0 ); + /* Inserting a single row into a parent table cannot cause an immediate + ** foreign key violation. So do nothing in this case. */ + continue; + } + + if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){ + if( !isIgnoreErrors || db->mallocFailed ) return; + continue; + } + assert( aiCol || pFKey->nCol==1 ); + + /* Create a SrcList structure containing a single table (the table + ** the foreign key that refers to this table is attached to). This + ** is required for the sqlite3WhereXXX() interface. */ + pSrc = sqlite3SrcListAppend(db, 0, 0, 0); + if( pSrc ){ + struct SrcList_item *pItem = pSrc->a; + pItem->pTab = pFKey->pFrom; + pItem->zName = pFKey->pFrom->zName; + pItem->pTab->nRef++; + pItem->iCursor = pParse->nTab++; + + if( regNew!=0 ){ + fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1); + } + if( regOld!=0 ){ + /* If there is a RESTRICT action configured for the current operation + ** on the parent table of this FK, then throw an exception + ** immediately if the FK constraint is violated, even if this is a + ** deferred trigger. That's what RESTRICT means. To defer checking + ** the constraint, the FK should specify NO ACTION (represented + ** using OE_None). NO ACTION is the default. */ + fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1); + } + pItem->zName = 0; + sqlite3SrcListDelete(db, pSrc); + } + sqlite3DbFree(db, aiCol); + } +} + +#define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x))) + +/* +** This function is called before generating code to update or delete a +** row contained in table pTab. +*/ +SQLITE_PRIVATE u32 sqlite3FkOldmask( + Parse *pParse, /* Parse context */ + Table *pTab /* Table being modified */ +){ + u32 mask = 0; + if( pParse->db->flags&SQLITE_ForeignKeys ){ + FKey *p; + int i; + for(p=pTab->pFKey; p; p=p->pNextFrom){ + for(i=0; inCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom); + } + for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ + Index *pIdx = 0; + locateFkeyIndex(pParse, pTab, p, &pIdx, 0); + if( pIdx ){ + for(i=0; inColumn; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]); + } + } + } + return mask; +} + +/* +** This function is called before generating code to update or delete a +** row contained in table pTab. If the operation is a DELETE, then +** parameter aChange is passed a NULL value. For an UPDATE, aChange points +** to an array of size N, where N is the number of columns in table pTab. +** If the i'th column is not modified by the UPDATE, then the corresponding +** entry in the aChange[] array is set to -1. If the column is modified, +** the value is 0 or greater. Parameter chngRowid is set to true if the +** UPDATE statement modifies the rowid fields of the table. +** +** If any foreign key processing will be required, this function returns +** true. If there is no foreign key related processing, this function +** returns false. +*/ +SQLITE_PRIVATE int sqlite3FkRequired( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being modified */ + int *aChange, /* Non-NULL for UPDATE operations */ + int chngRowid /* True for UPDATE that affects rowid */ +){ + if( pParse->db->flags&SQLITE_ForeignKeys ){ + if( !aChange ){ + /* A DELETE operation. Foreign key processing is required if the + ** table in question is either the child or parent table for any + ** foreign key constraint. */ + return (sqlite3FkReferences(pTab) || pTab->pFKey); + }else{ + /* This is an UPDATE. Foreign key processing is only required if the + ** operation modifies one or more child or parent key columns. */ + int i; + FKey *p; + + /* Check if any child key columns are being modified. */ + for(p=pTab->pFKey; p; p=p->pNextFrom){ + for(i=0; inCol; i++){ + int iChildKey = p->aCol[i].iFrom; + if( aChange[iChildKey]>=0 ) return 1; + if( iChildKey==pTab->iPKey && chngRowid ) return 1; + } + } + + /* Check if any parent key columns are being modified. */ + for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){ + for(i=0; inCol; i++){ + char *zKey = p->aCol[i].zCol; + int iKey; + for(iKey=0; iKeynCol; iKey++){ + Column *pCol = &pTab->aCol[iKey]; + if( (zKey ? !sqlite3StrICmp(pCol->zName, zKey) : pCol->isPrimKey) ){ + if( aChange[iKey]>=0 ) return 1; + if( iKey==pTab->iPKey && chngRowid ) return 1; + } + } + } + } + } + } + return 0; +} + +/* +** This function is called when an UPDATE or DELETE operation is being +** compiled on table pTab, which is the parent table of foreign-key pFKey. +** If the current operation is an UPDATE, then the pChanges parameter is +** passed a pointer to the list of columns being modified. If it is a +** DELETE, pChanges is passed a NULL pointer. +** +** It returns a pointer to a Trigger structure containing a trigger +** equivalent to the ON UPDATE or ON DELETE action specified by pFKey. +** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is +** returned (these actions require no special handling by the triggers +** sub-system, code for them is created by fkScanChildren()). +** +** For example, if pFKey is the foreign key and pTab is table "p" in +** the following schema: +** +** CREATE TABLE p(pk PRIMARY KEY); +** CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE); +** +** then the returned trigger structure is equivalent to: +** +** CREATE TRIGGER ... DELETE ON p BEGIN +** DELETE FROM c WHERE ck = old.pk; +** END; +** +** The returned pointer is cached as part of the foreign key object. It +** is eventually freed along with the rest of the foreign key object by +** sqlite3FkDelete(). +*/ +static Trigger *fkActionTrigger( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being updated or deleted from */ + FKey *pFKey, /* Foreign key to get action for */ + ExprList *pChanges /* Change-list for UPDATE, NULL for DELETE */ +){ + sqlite3 *db = pParse->db; /* Database handle */ + int action; /* One of OE_None, OE_Cascade etc. */ + Trigger *pTrigger; /* Trigger definition to return */ + int iAction = (pChanges!=0); /* 1 for UPDATE, 0 for DELETE */ + + action = pFKey->aAction[iAction]; + pTrigger = pFKey->apTrigger[iAction]; + + if( action!=OE_None && !pTrigger ){ + u8 enableLookaside; /* Copy of db->lookaside.bEnabled */ + char const *zFrom; /* Name of child table */ + int nFrom; /* Length in bytes of zFrom */ + Index *pIdx = 0; /* Parent key index for this FK */ + int *aiCol = 0; /* child table cols -> parent key cols */ + TriggerStep *pStep = 0; /* First (only) step of trigger program */ + Expr *pWhere = 0; /* WHERE clause of trigger step */ + ExprList *pList = 0; /* Changes list if ON UPDATE CASCADE */ + Select *pSelect = 0; /* If RESTRICT, "SELECT RAISE(...)" */ + int i; /* Iterator variable */ + Expr *pWhen = 0; /* WHEN clause for the trigger */ + + if( locateFkeyIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0; + assert( aiCol || pFKey->nCol==1 ); + + for(i=0; inCol; i++){ + Token tOld = { "old", 3 }; /* Literal "old" token */ + Token tNew = { "new", 3 }; /* Literal "new" token */ + Token tFromCol; /* Name of column in child table */ + Token tToCol; /* Name of column in parent table */ + int iFromCol; /* Idx of column in child table */ + Expr *pEq; /* tFromCol = OLD.tToCol */ + + iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom; + assert( iFromCol>=0 ); + tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid"; + tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName; + + tToCol.n = sqlite3Strlen30(tToCol.z); + tFromCol.n = sqlite3Strlen30(tFromCol.z); + + /* Create the expression "OLD.zToCol = zFromCol". It is important + ** that the "OLD.zToCol" term is on the LHS of the = operator, so + ** that the affinity and collation sequence associated with the + ** parent table are used for the comparison. */ + pEq = sqlite3PExpr(pParse, TK_EQ, + sqlite3PExpr(pParse, TK_DOT, + sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld), + sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol) + , 0), + sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol) + , 0); + pWhere = sqlite3ExprAnd(db, pWhere, pEq); + + /* For ON UPDATE, construct the next term of the WHEN clause. + ** The final WHEN clause will be like this: + ** + ** WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN) + */ + if( pChanges ){ + pEq = sqlite3PExpr(pParse, TK_IS, + sqlite3PExpr(pParse, TK_DOT, + sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld), + sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol), + 0), + sqlite3PExpr(pParse, TK_DOT, + sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew), + sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol), + 0), + 0); + pWhen = sqlite3ExprAnd(db, pWhen, pEq); + } + + if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){ + Expr *pNew; + if( action==OE_Cascade ){ + pNew = sqlite3PExpr(pParse, TK_DOT, + sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew), + sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol) + , 0); + }else if( action==OE_SetDflt ){ + Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt; + if( pDflt ){ + pNew = sqlite3ExprDup(db, pDflt, 0); + }else{ + pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); + } + }else{ + pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0); + } + pList = sqlite3ExprListAppend(pParse, pList, pNew); + sqlite3ExprListSetName(pParse, pList, &tFromCol, 0); + } + } + sqlite3DbFree(db, aiCol); + + zFrom = pFKey->pFrom->zName; + nFrom = sqlite3Strlen30(zFrom); + + if( action==OE_Restrict ){ + Token tFrom; + Expr *pRaise; + + tFrom.z = zFrom; + tFrom.n = nFrom; + pRaise = sqlite3Expr(db, TK_RAISE, "foreign key constraint failed"); + if( pRaise ){ + pRaise->affinity = OE_Abort; + } + pSelect = sqlite3SelectNew(pParse, + sqlite3ExprListAppend(pParse, 0, pRaise), + sqlite3SrcListAppend(db, 0, &tFrom, 0), + pWhere, + 0, 0, 0, 0, 0, 0 + ); + pWhere = 0; + } + + /* Disable lookaside memory allocation */ + enableLookaside = db->lookaside.bEnabled; + db->lookaside.bEnabled = 0; + + pTrigger = (Trigger *)sqlite3DbMallocZero(db, + sizeof(Trigger) + /* struct Trigger */ + sizeof(TriggerStep) + /* Single step in trigger program */ + nFrom + 1 /* Space for pStep->target.z */ + ); + if( pTrigger ){ + pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1]; + pStep->target.z = (char *)&pStep[1]; + pStep->target.n = nFrom; + memcpy((char *)pStep->target.z, zFrom, nFrom); + + pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); + pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE); + pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); + if( pWhen ){ + pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0); + pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE); + } + } + + /* Re-enable the lookaside buffer, if it was disabled earlier. */ + db->lookaside.bEnabled = enableLookaside; + + sqlite3ExprDelete(db, pWhere); + sqlite3ExprDelete(db, pWhen); + sqlite3ExprListDelete(db, pList); + sqlite3SelectDelete(db, pSelect); + if( db->mallocFailed==1 ){ + fkTriggerDelete(db, pTrigger); + return 0; + } + + switch( action ){ + case OE_Restrict: + pStep->op = TK_SELECT; + break; + case OE_Cascade: + if( !pChanges ){ + pStep->op = TK_DELETE; + break; + } + default: + pStep->op = TK_UPDATE; + } + pStep->pTrig = pTrigger; + pTrigger->pSchema = pTab->pSchema; + pTrigger->pTabSchema = pTab->pSchema; + pFKey->apTrigger[iAction] = pTrigger; + pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE); + } + + return pTrigger; +} + +/* +** This function is called when deleting or updating a row to implement +** any required CASCADE, SET NULL or SET DEFAULT actions. +*/ +SQLITE_PRIVATE void sqlite3FkActions( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being updated or deleted from */ + ExprList *pChanges, /* Change-list for UPDATE, NULL for DELETE */ + int regOld /* Address of array containing old row */ +){ + /* If foreign-key support is enabled, iterate through all FKs that + ** refer to table pTab. If there is an action associated with the FK + ** for this operation (either update or delete), invoke the associated + ** trigger sub-program. */ + if( pParse->db->flags&SQLITE_ForeignKeys ){ + FKey *pFKey; /* Iterator variable */ + for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){ + Trigger *pAction = fkActionTrigger(pParse, pTab, pFKey, pChanges); + if( pAction ){ + sqlite3CodeRowTriggerDirect(pParse, pAction, pTab, regOld, OE_Abort, 0); + } + } + } +} + +#endif /* ifndef SQLITE_OMIT_TRIGGER */ + +/* +** Free all memory associated with foreign key definitions attached to +** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash +** hash table. +*/ +SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){ + FKey *pFKey; /* Iterator variable */ + FKey *pNext; /* Copy of pFKey->pNextFrom */ + + for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){ + + /* Remove the FK from the fkeyHash hash table. */ + if( !db || db->pnBytesFreed==0 ){ + if( pFKey->pPrevTo ){ + pFKey->pPrevTo->pNextTo = pFKey->pNextTo; + }else{ + void *p = (void *)pFKey->pNextTo; + const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo); + sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p); + } + if( pFKey->pNextTo ){ + pFKey->pNextTo->pPrevTo = pFKey->pPrevTo; + } + } + + /* EV: R-30323-21917 Each foreign key constraint in SQLite is + ** classified as either immediate or deferred. + */ + assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 ); + + /* Delete any triggers created to implement actions for this FK. */ +#ifndef SQLITE_OMIT_TRIGGER + fkTriggerDelete(db, pFKey->apTrigger[0]); + fkTriggerDelete(db, pFKey->apTrigger[1]); +#endif + + pNext = pFKey->pNextFrom; + sqlite3DbFree(db, pFKey); + } +} +#endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */ + +/************** End of fkey.c ************************************************/ +/************** Begin file insert.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that are called by the parser +** to handle INSERT statements in SQLite. +*/ + +/* +** Generate code that will open a table for reading. +*/ +SQLITE_PRIVATE void sqlite3OpenTable( + Parse *p, /* Generate code into this VDBE */ + int iCur, /* The cursor number of the table */ + int iDb, /* The database index in sqlite3.aDb[] */ + Table *pTab, /* The table to be opened */ + int opcode /* OP_OpenRead or OP_OpenWrite */ +){ + Vdbe *v; + if( IsVirtual(pTab) ) return; + v = sqlite3GetVdbe(p); + assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); + sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName); + sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb); + sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32); + VdbeComment((v, "%s", pTab->zName)); +} + +/* +** Return a pointer to the column affinity string associated with index +** pIdx. A column affinity string has one character for each column in +** the table, according to the affinity of the column: +** +** Character Column affinity +** ------------------------------ +** 'a' TEXT +** 'b' NONE +** 'c' NUMERIC +** 'd' INTEGER +** 'e' REAL +** +** An extra 'b' is appended to the end of the string to cover the +** rowid that appears as the last column in every index. +** +** Memory for the buffer containing the column index affinity string +** is managed along with the rest of the Index structure. It will be +** released when sqlite3DeleteIndex() is called. +*/ +SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){ + if( !pIdx->zColAff ){ + /* The first time a column affinity string for a particular index is + ** required, it is allocated and populated here. It is then stored as + ** a member of the Index structure for subsequent use. + ** + ** The column affinity string will eventually be deleted by + ** sqliteDeleteIndex() when the Index structure itself is cleaned + ** up. + */ + int n; + Table *pTab = pIdx->pTable; + sqlite3 *db = sqlite3VdbeDb(v); + pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+2); + if( !pIdx->zColAff ){ + db->mallocFailed = 1; + return 0; + } + for(n=0; nnColumn; n++){ + pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity; + } + pIdx->zColAff[n++] = SQLITE_AFF_NONE; + pIdx->zColAff[n] = 0; + } + + return pIdx->zColAff; +} + +/* +** Set P4 of the most recently inserted opcode to a column affinity +** string for table pTab. A column affinity string has one character +** for each column indexed by the index, according to the affinity of the +** column: +** +** Character Column affinity +** ------------------------------ +** 'a' TEXT +** 'b' NONE +** 'c' NUMERIC +** 'd' INTEGER +** 'e' REAL +*/ +SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){ + /* The first time a column affinity string for a particular table + ** is required, it is allocated and populated here. It is then + ** stored as a member of the Table structure for subsequent use. + ** + ** The column affinity string will eventually be deleted by + ** sqlite3DeleteTable() when the Table structure itself is cleaned up. + */ + if( !pTab->zColAff ){ + char *zColAff; + int i; + sqlite3 *db = sqlite3VdbeDb(v); + + zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1); + if( !zColAff ){ + db->mallocFailed = 1; + return; + } + + for(i=0; inCol; i++){ + zColAff[i] = pTab->aCol[i].affinity; + } + zColAff[pTab->nCol] = '\0'; + + pTab->zColAff = zColAff; + } + + sqlite3VdbeChangeP4(v, -1, pTab->zColAff, 0); +} + +/* +** Return non-zero if the table pTab in database iDb or any of its indices +** have been opened at any point in the VDBE program beginning at location +** iStartAddr throught the end of the program. This is used to see if +** a statement of the form "INSERT INTO SELECT ..." can +** run without using temporary table for the results of the SELECT. +*/ +static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){ + Vdbe *v = sqlite3GetVdbe(p); + int i; + int iEnd = sqlite3VdbeCurrentAddr(v); +#ifndef SQLITE_OMIT_VIRTUALTABLE + VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0; +#endif + + for(i=iStartAddr; iopcode==OP_OpenRead && pOp->p3==iDb ){ + Index *pIndex; + int tnum = pOp->p2; + if( tnum==pTab->tnum ){ + return 1; + } + for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){ + if( tnum==pIndex->tnum ){ + return 1; + } + } + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){ + assert( pOp->p4.pVtab!=0 ); + assert( pOp->p4type==P4_VTAB ); + return 1; + } +#endif + } + return 0; +} + +#ifndef SQLITE_OMIT_AUTOINCREMENT +/* +** Locate or create an AutoincInfo structure associated with table pTab +** which is in database iDb. Return the register number for the register +** that holds the maximum rowid. +** +** There is at most one AutoincInfo structure per table even if the +** same table is autoincremented multiple times due to inserts within +** triggers. A new AutoincInfo structure is created if this is the +** first use of table pTab. On 2nd and subsequent uses, the original +** AutoincInfo structure is used. +** +** Three memory locations are allocated: +** +** (1) Register to hold the name of the pTab table. +** (2) Register to hold the maximum ROWID of pTab. +** (3) Register to hold the rowid in sqlite_sequence of pTab +** +** The 2nd register is the one that is returned. That is all the +** insert routine needs to know about. +*/ +static int autoIncBegin( + Parse *pParse, /* Parsing context */ + int iDb, /* Index of the database holding pTab */ + Table *pTab /* The table we are writing to */ +){ + int memId = 0; /* Register holding maximum rowid */ + if( pTab->tabFlags & TF_Autoincrement ){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + AutoincInfo *pInfo; + + pInfo = pToplevel->pAinc; + while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; } + if( pInfo==0 ){ + pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo)); + if( pInfo==0 ) return 0; + pInfo->pNext = pToplevel->pAinc; + pToplevel->pAinc = pInfo; + pInfo->pTab = pTab; + pInfo->iDb = iDb; + pToplevel->nMem++; /* Register to hold name of table */ + pInfo->regCtr = ++pToplevel->nMem; /* Max rowid register */ + pToplevel->nMem++; /* Rowid in sqlite_sequence */ + } + memId = pInfo->regCtr; + } + return memId; +} + +/* +** This routine generates code that will initialize all of the +** register used by the autoincrement tracker. +*/ +SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){ + AutoincInfo *p; /* Information about an AUTOINCREMENT */ + sqlite3 *db = pParse->db; /* The database connection */ + Db *pDb; /* Database only autoinc table */ + int memId; /* Register holding max rowid */ + int addr; /* A VDBE address */ + Vdbe *v = pParse->pVdbe; /* VDBE under construction */ + + /* This routine is never called during trigger-generation. It is + ** only called from the top-level */ + assert( pParse->pTriggerTab==0 ); + assert( pParse==sqlite3ParseToplevel(pParse) ); + + assert( v ); /* We failed long ago if this is not so */ + for(p = pParse->pAinc; p; p = p->pNext){ + pDb = &db->aDb[p->iDb]; + memId = p->regCtr; + sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead); + addr = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0); + sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); + sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId); + sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); + sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL); + sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1); + sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9); + sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); + sqlite3VdbeAddOp2(v, OP_Integer, 0, memId); + sqlite3VdbeAddOp0(v, OP_Close); + } +} + +/* +** Update the maximum rowid for an autoincrement calculation. +** +** This routine should be called when the top of the stack holds a +** new rowid that is about to be inserted. If that new rowid is +** larger than the maximum rowid in the memId memory cell, then the +** memory cell is updated. The stack is unchanged. +*/ +static void autoIncStep(Parse *pParse, int memId, int regRowid){ + if( memId>0 ){ + sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid); + } +} + +/* +** This routine generates the code needed to write autoincrement +** maximum rowid values back into the sqlite_sequence register. +** Every statement that might do an INSERT into an autoincrement +** table (either directly or through triggers) needs to call this +** routine just before the "exit" code. +*/ +SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){ + AutoincInfo *p; + Vdbe *v = pParse->pVdbe; + sqlite3 *db = pParse->db; + + assert( v ); + for(p = pParse->pAinc; p; p = p->pNext){ + Db *pDb = &db->aDb[p->iDb]; + int j1, j2, j3, j4, j5; + int iRec; + int memId = p->regCtr; + + iRec = sqlite3GetTempReg(pParse); + sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite); + j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); + j2 = sqlite3VdbeAddOp0(v, OP_Rewind); + j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec); + j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec); + sqlite3VdbeAddOp2(v, OP_Next, 0, j3); + sqlite3VdbeJumpHere(v, j2); + sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1); + j5 = sqlite3VdbeAddOp0(v, OP_Goto); + sqlite3VdbeJumpHere(v, j4); + sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1); + sqlite3VdbeJumpHere(v, j1); + sqlite3VdbeJumpHere(v, j5); + sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec); + sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3VdbeAddOp0(v, OP_Close); + sqlite3ReleaseTempReg(pParse, iRec); + } +} +#else +/* +** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines +** above are all no-ops +*/ +# define autoIncBegin(A,B,C) (0) +# define autoIncStep(A,B,C) +#endif /* SQLITE_OMIT_AUTOINCREMENT */ + + +/* Forward declaration */ +static int xferOptimization( + Parse *pParse, /* Parser context */ + Table *pDest, /* The table we are inserting into */ + Select *pSelect, /* A SELECT statement to use as the data source */ + int onError, /* How to handle constraint errors */ + int iDbDest /* The database of pDest */ +); + +/* +** This routine is call to handle SQL of the following forms: +** +** insert into TABLE (IDLIST) values(EXPRLIST) +** insert into TABLE (IDLIST) select +** +** The IDLIST following the table name is always optional. If omitted, +** then a list of all columns for the table is substituted. The IDLIST +** appears in the pColumn parameter. pColumn is NULL if IDLIST is omitted. +** +** The pList parameter holds EXPRLIST in the first form of the INSERT +** statement above, and pSelect is NULL. For the second form, pList is +** NULL and pSelect is a pointer to the select statement used to generate +** data for the insert. +** +** The code generated follows one of four templates. For a simple +** select with data coming from a VALUES clause, the code executes +** once straight down through. Pseudo-code follows (we call this +** the "1st template"): +** +** open write cursor to
    and its indices +** puts VALUES clause expressions onto the stack +** write the resulting record into
    +** cleanup +** +** The three remaining templates assume the statement is of the form +** +** INSERT INTO
    SELECT ... +** +** If the SELECT clause is of the restricted form "SELECT * FROM " - +** in other words if the SELECT pulls all columns from a single table +** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and +** if and are distinct tables but have identical +** schemas, including all the same indices, then a special optimization +** is invoked that copies raw records from over to . +** See the xferOptimization() function for the implementation of this +** template. This is the 2nd template. +** +** open a write cursor to
    +** open read cursor on +** transfer all records in over to
    +** close cursors +** foreach index on
    +** open a write cursor on the
    index +** open a read cursor on the corresponding index +** transfer all records from the read to the write cursors +** close cursors +** end foreach +** +** The 3rd template is for when the second template does not apply +** and the SELECT clause does not read from
    at any time. +** The generated code follows this template: +** +** EOF <- 0 +** X <- A +** goto B +** A: setup for the SELECT +** loop over the rows in the SELECT +** load values into registers R..R+n +** yield X +** end loop +** cleanup after the SELECT +** EOF <- 1 +** yield X +** goto A +** B: open write cursor to
    and its indices +** C: yield X +** if EOF goto D +** insert the select result into
    from R..R+n +** goto C +** D: cleanup +** +** The 4th template is used if the insert statement takes its +** values from a SELECT but the data is being inserted into a table +** that is also read as part of the SELECT. In the third form, +** we have to use a intermediate table to store the results of +** the select. The template is like this: +** +** EOF <- 0 +** X <- A +** goto B +** A: setup for the SELECT +** loop over the tables in the SELECT +** load value into register R..R+n +** yield X +** end loop +** cleanup after the SELECT +** EOF <- 1 +** yield X +** halt-error +** B: open temp table +** L: yield X +** if EOF goto M +** insert row from R..R+n into temp table +** goto L +** M: open write cursor to
    and its indices +** rewind temp table +** C: loop over rows of intermediate table +** transfer values form intermediate table into
    +** end loop +** D: cleanup +*/ +SQLITE_PRIVATE void sqlite3Insert( + Parse *pParse, /* Parser context */ + SrcList *pTabList, /* Name of table into which we are inserting */ + ExprList *pList, /* List of values to be inserted */ + Select *pSelect, /* A SELECT statement to use as the data source */ + IdList *pColumn, /* Column names corresponding to IDLIST. */ + int onError /* How to handle constraint errors */ +){ + sqlite3 *db; /* The main database structure */ + Table *pTab; /* The table to insert into. aka TABLE */ + char *zTab; /* Name of the table into which we are inserting */ + const char *zDb; /* Name of the database holding this table */ + int i, j, idx; /* Loop counters */ + Vdbe *v; /* Generate code into this virtual machine */ + Index *pIdx; /* For looping over indices of the table */ + int nColumn; /* Number of columns in the data */ + int nHidden = 0; /* Number of hidden columns if TABLE is virtual */ + int baseCur = 0; /* VDBE Cursor number for pTab */ + int keyColumn = -1; /* Column that is the INTEGER PRIMARY KEY */ + int endOfLoop; /* Label for the end of the insertion loop */ + int useTempTable = 0; /* Store SELECT results in intermediate table */ + int srcTab = 0; /* Data comes from this temporary cursor if >=0 */ + int addrInsTop = 0; /* Jump to label "D" */ + int addrCont = 0; /* Top of insert loop. Label "C" in templates 3 and 4 */ + int addrSelect = 0; /* Address of coroutine that implements the SELECT */ + SelectDest dest; /* Destination for SELECT on rhs of INSERT */ + int iDb; /* Index of database holding TABLE */ + Db *pDb; /* The database containing table being inserted into */ + int appendFlag = 0; /* True if the insert is likely to be an append */ + + /* Register allocations */ + int regFromSelect = 0;/* Base register for data coming from SELECT */ + int regAutoinc = 0; /* Register holding the AUTOINCREMENT counter */ + int regRowCount = 0; /* Memory cell used for the row counter */ + int regIns; /* Block of regs holding rowid+data being inserted */ + int regRowid; /* registers holding insert rowid */ + int regData; /* register holding first column to insert */ + int regEof = 0; /* Register recording end of SELECT data */ + int *aRegIdx = 0; /* One register allocated to each index */ + +#ifndef SQLITE_OMIT_TRIGGER + int isView; /* True if attempting to insert into a view */ + Trigger *pTrigger; /* List of triggers on pTab, if required */ + int tmask; /* Mask of trigger times */ +#endif + + db = pParse->db; + memset(&dest, 0, sizeof(dest)); + if( pParse->nErr || db->mallocFailed ){ + goto insert_cleanup; + } + + /* Locate the table into which we will be inserting new information. + */ + assert( pTabList->nSrc==1 ); + zTab = pTabList->a[0].zName; + if( NEVER(zTab==0) ) goto insert_cleanup; + pTab = sqlite3SrcListLookup(pParse, pTabList); + if( pTab==0 ){ + goto insert_cleanup; + } + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + assert( iDbnDb ); + pDb = &db->aDb[iDb]; + zDb = pDb->zName; + if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){ + goto insert_cleanup; + } + + /* Figure out if we have any triggers and if the table being + ** inserted into is a view + */ +#ifndef SQLITE_OMIT_TRIGGER + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask); + isView = pTab->pSelect!=0; +#else +# define pTrigger 0 +# define tmask 0 +# define isView 0 +#endif +#ifdef SQLITE_OMIT_VIEW +# undef isView +# define isView 0 +#endif + assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) ); + + /* If pTab is really a view, make sure it has been initialized. + ** ViewGetColumnNames() is a no-op if pTab is not a view (or virtual + ** module table). + */ + if( sqlite3ViewGetColumnNames(pParse, pTab) ){ + goto insert_cleanup; + } + + /* Ensure that: + * (a) the table is not read-only, + * (b) that if it is a view then ON INSERT triggers exist + */ + if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ + goto insert_cleanup; + } + + /* Allocate a VDBE + */ + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto insert_cleanup; + if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); + sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb); + +#ifndef SQLITE_OMIT_XFER_OPT + /* If the statement is of the form + ** + ** INSERT INTO SELECT * FROM ; + ** + ** Then special optimizations can be applied that make the transfer + ** very fast and which reduce fragmentation of indices. + ** + ** This is the 2nd template. + */ + if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){ + assert( !pTrigger ); + assert( pList==0 ); + goto insert_end; + } +#endif /* SQLITE_OMIT_XFER_OPT */ + + /* If this is an AUTOINCREMENT table, look up the sequence number in the + ** sqlite_sequence table and store it in memory cell regAutoinc. + */ + regAutoinc = autoIncBegin(pParse, iDb, pTab); + + /* Figure out how many columns of data are supplied. If the data + ** is coming from a SELECT statement, then generate a co-routine that + ** produces a single row of the SELECT on each invocation. The + ** co-routine is the common header to the 3rd and 4th templates. + */ + if( pSelect ){ + /* Data is coming from a SELECT. Generate code to implement that SELECT + ** as a co-routine. The code is common to both the 3rd and 4th + ** templates: + ** + ** EOF <- 0 + ** X <- A + ** goto B + ** A: setup for the SELECT + ** loop over the tables in the SELECT + ** load value into register R..R+n + ** yield X + ** end loop + ** cleanup after the SELECT + ** EOF <- 1 + ** yield X + ** halt-error + ** + ** On each invocation of the co-routine, it puts a single row of the + ** SELECT result into registers dest.iMem...dest.iMem+dest.nMem-1. + ** (These output registers are allocated by sqlite3Select().) When + ** the SELECT completes, it sets the EOF flag stored in regEof. + */ + int rc, j1; + + regEof = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof); /* EOF <- 0 */ + VdbeComment((v, "SELECT eof flag")); + sqlite3SelectDestInit(&dest, SRT_Coroutine, ++pParse->nMem); + addrSelect = sqlite3VdbeCurrentAddr(v)+2; + sqlite3VdbeAddOp2(v, OP_Integer, addrSelect-1, dest.iParm); + j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); + VdbeComment((v, "Jump over SELECT coroutine")); + + /* Resolve the expressions in the SELECT statement and execute it. */ + rc = sqlite3Select(pParse, pSelect, &dest); + assert( pParse->nErr==0 || rc ); + if( rc || NEVER(pParse->nErr) || db->mallocFailed ){ + goto insert_cleanup; + } + sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof); /* EOF <- 1 */ + sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); /* yield X */ + sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort); + VdbeComment((v, "End of SELECT coroutine")); + sqlite3VdbeJumpHere(v, j1); /* label B: */ + + regFromSelect = dest.iMem; + assert( pSelect->pEList ); + nColumn = pSelect->pEList->nExpr; + assert( dest.nMem==nColumn ); + + /* Set useTempTable to TRUE if the result of the SELECT statement + ** should be written into a temporary table (template 4). Set to + ** FALSE if each* row of the SELECT can be written directly into + ** the destination table (template 3). + ** + ** A temp table must be used if the table being updated is also one + ** of the tables being read by the SELECT statement. Also use a + ** temp table in the case of row triggers. + */ + if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){ + useTempTable = 1; + } + + if( useTempTable ){ + /* Invoke the coroutine to extract information from the SELECT + ** and add it to a transient table srcTab. The code generated + ** here is from the 4th template: + ** + ** B: open temp table + ** L: yield X + ** if EOF goto M + ** insert row from R..R+n into temp table + ** goto L + ** M: ... + */ + int regRec; /* Register to hold packed record */ + int regTempRowid; /* Register to hold temp table ROWID */ + int addrTop; /* Label "L" */ + int addrIf; /* Address of jump to M */ + + srcTab = pParse->nTab++; + regRec = sqlite3GetTempReg(pParse); + regTempRowid = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn); + addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); + addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec); + sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid); + sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop); + sqlite3VdbeJumpHere(v, addrIf); + sqlite3ReleaseTempReg(pParse, regRec); + sqlite3ReleaseTempReg(pParse, regTempRowid); + } + }else{ + /* This is the case if the data for the INSERT is coming from a VALUES + ** clause + */ + NameContext sNC; + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + srcTab = -1; + assert( useTempTable==0 ); + nColumn = pList ? pList->nExpr : 0; + for(i=0; ia[i].pExpr) ){ + goto insert_cleanup; + } + } + } + + /* Make sure the number of columns in the source data matches the number + ** of columns to be inserted into the table. + */ + if( IsVirtual(pTab) ){ + for(i=0; inCol; i++){ + nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0); + } + } + if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){ + sqlite3ErrorMsg(pParse, + "table %S has %d columns but %d values were supplied", + pTabList, 0, pTab->nCol-nHidden, nColumn); + goto insert_cleanup; + } + if( pColumn!=0 && nColumn!=pColumn->nId ){ + sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId); + goto insert_cleanup; + } + + /* If the INSERT statement included an IDLIST term, then make sure + ** all elements of the IDLIST really are columns of the table and + ** remember the column indices. + ** + ** If the table has an INTEGER PRIMARY KEY column and that column + ** is named in the IDLIST, then record in the keyColumn variable + ** the index into IDLIST of the primary key column. keyColumn is + ** the index of the primary key as it appears in IDLIST, not as + ** is appears in the original table. (The index of the primary + ** key in the original table is pTab->iPKey.) + */ + if( pColumn ){ + for(i=0; inId; i++){ + pColumn->a[i].idx = -1; + } + for(i=0; inId; i++){ + for(j=0; jnCol; j++){ + if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){ + pColumn->a[i].idx = j; + if( j==pTab->iPKey ){ + keyColumn = i; + } + break; + } + } + if( j>=pTab->nCol ){ + if( sqlite3IsRowid(pColumn->a[i].zName) ){ + keyColumn = i; + }else{ + sqlite3ErrorMsg(pParse, "table %S has no column named %s", + pTabList, 0, pColumn->a[i].zName); + pParse->checkSchema = 1; + goto insert_cleanup; + } + } + } + } + + /* If there is no IDLIST term but the table has an integer primary + ** key, the set the keyColumn variable to the primary key column index + ** in the original table definition. + */ + if( pColumn==0 && nColumn>0 ){ + keyColumn = pTab->iPKey; + } + + /* Initialize the count of rows to be inserted + */ + if( db->flags & SQLITE_CountRows ){ + regRowCount = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); + } + + /* If this is not a view, open the table and and all indices */ + if( !isView ){ + int nIdx; + + baseCur = pParse->nTab; + nIdx = sqlite3OpenTableAndIndices(pParse, pTab, baseCur, OP_OpenWrite); + aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1)); + if( aRegIdx==0 ){ + goto insert_cleanup; + } + for(i=0; inMem; + } + } + + /* This is the top of the main insertion loop */ + if( useTempTable ){ + /* This block codes the top of loop only. The complete loop is the + ** following pseudocode (template 4): + ** + ** rewind temp table + ** C: loop over rows of intermediate table + ** transfer values form intermediate table into
    + ** end loop + ** D: ... + */ + addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab); + addrCont = sqlite3VdbeCurrentAddr(v); + }else if( pSelect ){ + /* This block codes the top of loop only. The complete loop is the + ** following pseudocode (template 3): + ** + ** C: yield X + ** if EOF goto D + ** insert the select result into
    from R..R+n + ** goto C + ** D: ... + */ + addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iParm); + addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof); + } + + /* Allocate registers for holding the rowid of the new row, + ** the content of the new row, and the assemblied row record. + */ + regRowid = regIns = pParse->nMem+1; + pParse->nMem += pTab->nCol + 1; + if( IsVirtual(pTab) ){ + regRowid++; + pParse->nMem++; + } + regData = regRowid+1; + + /* Run the BEFORE and INSTEAD OF triggers, if there are any + */ + endOfLoop = sqlite3VdbeMakeLabel(v); + if( tmask & TRIGGER_BEFORE ){ + int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1); + + /* build the NEW.* reference row. Note that if there is an INTEGER + ** PRIMARY KEY into which a NULL is being inserted, that NULL will be + ** translated into a unique ID for the row. But on a BEFORE trigger, + ** we do not know what the unique ID will be (because the insert has + ** not happened yet) so we substitute a rowid of -1 + */ + if( keyColumn<0 ){ + sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); + }else{ + int j1; + if( useTempTable ){ + sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regCols); + }else{ + assert( pSelect==0 ); /* Otherwise useTempTable is true */ + sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regCols); + } + j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols); + sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols); + sqlite3VdbeJumpHere(v, j1); + sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols); + } + + /* Cannot have triggers on a virtual table. If it were possible, + ** this block would have to account for hidden column. + */ + assert( !IsVirtual(pTab) ); + + /* Create the new column data + */ + for(i=0; inCol; i++){ + if( pColumn==0 ){ + j = i; + }else{ + for(j=0; jnId; j++){ + if( pColumn->a[j].idx==i ) break; + } + } + if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){ + sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1); + }else if( useTempTable ){ + sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1); + }else{ + assert( pSelect==0 ); /* Otherwise useTempTable is true */ + sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1); + } + } + + /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger, + ** do not attempt any conversions before assembling the record. + ** If this is a real table, attempt conversions as required by the + ** table column affinities. + */ + if( !isView ){ + sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol); + sqlite3TableAffinityStr(v, pTab); + } + + /* Fire BEFORE or INSTEAD OF triggers */ + sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE, + pTab, regCols-pTab->nCol-1, onError, endOfLoop); + + sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1); + } + + /* Push the record number for the new entry onto the stack. The + ** record number is a randomly generate integer created by NewRowid + ** except when the table has an INTEGER PRIMARY KEY column, in which + ** case the record number is the same as that column. + */ + if( !isView ){ + if( IsVirtual(pTab) ){ + /* The row that the VUpdate opcode will delete: none */ + sqlite3VdbeAddOp2(v, OP_Null, 0, regIns); + } + if( keyColumn>=0 ){ + if( useTempTable ){ + sqlite3VdbeAddOp3(v, OP_Column, srcTab, keyColumn, regRowid); + }else if( pSelect ){ + sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+keyColumn, regRowid); + }else{ + VdbeOp *pOp; + sqlite3ExprCode(pParse, pList->a[keyColumn].pExpr, regRowid); + pOp = sqlite3VdbeGetOp(v, -1); + if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){ + appendFlag = 1; + pOp->opcode = OP_NewRowid; + pOp->p1 = baseCur; + pOp->p2 = regRowid; + pOp->p3 = regAutoinc; + } + } + /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid + ** to generate a unique primary key value. + */ + if( !appendFlag ){ + int j1; + if( !IsVirtual(pTab) ){ + j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid); + sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc); + sqlite3VdbeJumpHere(v, j1); + }else{ + j1 = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2); + } + sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid); + } + }else if( IsVirtual(pTab) ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid); + }else{ + sqlite3VdbeAddOp3(v, OP_NewRowid, baseCur, regRowid, regAutoinc); + appendFlag = 1; + } + autoIncStep(pParse, regAutoinc, regRowid); + + /* Push onto the stack, data for all columns of the new entry, beginning + ** with the first column. + */ + nHidden = 0; + for(i=0; inCol; i++){ + int iRegStore = regRowid+1+i; + if( i==pTab->iPKey ){ + /* The value of the INTEGER PRIMARY KEY column is always a NULL. + ** Whenever this column is read, the record number will be substituted + ** in its place. So will fill this column with a NULL to avoid + ** taking up data space with information that will never be used. */ + sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore); + continue; + } + if( pColumn==0 ){ + if( IsHiddenColumn(&pTab->aCol[i]) ){ + assert( IsVirtual(pTab) ); + j = -1; + nHidden++; + }else{ + j = i - nHidden; + } + }else{ + for(j=0; jnId; j++){ + if( pColumn->a[j].idx==i ) break; + } + } + if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){ + sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore); + }else if( useTempTable ){ + sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore); + }else if( pSelect ){ + sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore); + }else{ + sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore); + } + } + + /* Generate code to check constraints and generate index keys and + ** do the insertion. + */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTab) ){ + const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); + sqlite3VtabMakeWritable(pParse, pTab); + sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB); + sqlite3MayAbort(pParse); + }else +#endif + { + int isReplace; /* Set to true if constraints may cause a replace */ + sqlite3GenerateConstraintChecks(pParse, pTab, baseCur, regIns, aRegIdx, + keyColumn>=0, 0, onError, endOfLoop, &isReplace + ); + sqlite3FkCheck(pParse, pTab, 0, regIns); + sqlite3CompleteInsertion( + pParse, pTab, baseCur, regIns, aRegIdx, 0, appendFlag, isReplace==0 + ); + } + } + + /* Update the count of rows that are inserted + */ + if( (db->flags & SQLITE_CountRows)!=0 ){ + sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); + } + + if( pTrigger ){ + /* Code AFTER triggers */ + sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER, + pTab, regData-2-pTab->nCol, onError, endOfLoop); + } + + /* The bottom of the main insertion loop, if the data source + ** is a SELECT statement. + */ + sqlite3VdbeResolveLabel(v, endOfLoop); + if( useTempTable ){ + sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont); + sqlite3VdbeJumpHere(v, addrInsTop); + sqlite3VdbeAddOp1(v, OP_Close, srcTab); + }else if( pSelect ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont); + sqlite3VdbeJumpHere(v, addrInsTop); + } + + if( !IsVirtual(pTab) && !isView ){ + /* Close all tables opened */ + sqlite3VdbeAddOp1(v, OP_Close, baseCur); + for(idx=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){ + sqlite3VdbeAddOp1(v, OP_Close, idx+baseCur); + } + } + +insert_end: + /* Update the sqlite_sequence table by storing the content of the + ** maximum rowid counter values recorded while inserting into + ** autoincrement tables. + */ + if( pParse->nested==0 && pParse->pTriggerTab==0 ){ + sqlite3AutoincrementEnd(pParse); + } + + /* + ** Return the number of rows inserted. If this routine is + ** generating code because of a call to sqlite3NestedParse(), do not + ** invoke the callback function. + */ + if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){ + sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC); + } + +insert_cleanup: + sqlite3SrcListDelete(db, pTabList); + sqlite3ExprListDelete(db, pList); + sqlite3SelectDelete(db, pSelect); + sqlite3IdListDelete(db, pColumn); + sqlite3DbFree(db, aRegIdx); +} + +/* Make sure "isView" and other macros defined above are undefined. Otherwise +** thely may interfere with compilation of other functions in this file +** (or in another file, if this file becomes part of the amalgamation). */ +#ifdef isView + #undef isView +#endif +#ifdef pTrigger + #undef pTrigger +#endif +#ifdef tmask + #undef tmask +#endif + + +/* +** Generate code to do constraint checks prior to an INSERT or an UPDATE. +** +** The input is a range of consecutive registers as follows: +** +** 1. The rowid of the row after the update. +** +** 2. The data in the first column of the entry after the update. +** +** i. Data from middle columns... +** +** N. The data in the last column of the entry after the update. +** +** The regRowid parameter is the index of the register containing (1). +** +** If isUpdate is true and rowidChng is non-zero, then rowidChng contains +** the address of a register containing the rowid before the update takes +** place. isUpdate is true for UPDATEs and false for INSERTs. If isUpdate +** is false, indicating an INSERT statement, then a non-zero rowidChng +** indicates that the rowid was explicitly specified as part of the +** INSERT statement. If rowidChng is false, it means that the rowid is +** computed automatically in an insert or that the rowid value is not +** modified by an update. +** +** The code generated by this routine store new index entries into +** registers identified by aRegIdx[]. No index entry is created for +** indices where aRegIdx[i]==0. The order of indices in aRegIdx[] is +** the same as the order of indices on the linked list of indices +** attached to the table. +** +** This routine also generates code to check constraints. NOT NULL, +** CHECK, and UNIQUE constraints are all checked. If a constraint fails, +** then the appropriate action is performed. There are five possible +** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE. +** +** Constraint type Action What Happens +** --------------- ---------- ---------------------------------------- +** any ROLLBACK The current transaction is rolled back and +** sqlite3_exec() returns immediately with a +** return code of SQLITE_CONSTRAINT. +** +** any ABORT Back out changes from the current command +** only (do not do a complete rollback) then +** cause sqlite3_exec() to return immediately +** with SQLITE_CONSTRAINT. +** +** any FAIL Sqlite_exec() returns immediately with a +** return code of SQLITE_CONSTRAINT. The +** transaction is not rolled back and any +** prior changes are retained. +** +** any IGNORE The record number and data is popped from +** the stack and there is an immediate jump +** to label ignoreDest. +** +** NOT NULL REPLACE The NULL value is replace by the default +** value for that column. If the default value +** is NULL, the action is the same as ABORT. +** +** UNIQUE REPLACE The other row that conflicts with the row +** being inserted is removed. +** +** CHECK REPLACE Illegal. The results in an exception. +** +** Which action to take is determined by the overrideError parameter. +** Or if overrideError==OE_Default, then the pParse->onError parameter +** is used. Or if pParse->onError==OE_Default then the onError value +** for the constraint is used. +** +** The calling routine must open a read/write cursor for pTab with +** cursor number "baseCur". All indices of pTab must also have open +** read/write cursors with cursor number baseCur+i for the i-th cursor. +** Except, if there is no possibility of a REPLACE action then +** cursors do not need to be open for indices where aRegIdx[i]==0. +*/ +SQLITE_PRIVATE void sqlite3GenerateConstraintChecks( + Parse *pParse, /* The parser context */ + Table *pTab, /* the table into which we are inserting */ + int baseCur, /* Index of a read/write cursor pointing at pTab */ + int regRowid, /* Index of the range of input registers */ + int *aRegIdx, /* Register used by each index. 0 for unused indices */ + int rowidChng, /* True if the rowid might collide with existing entry */ + int isUpdate, /* True for UPDATE, False for INSERT */ + int overrideError, /* Override onError to this if not OE_Default */ + int ignoreDest, /* Jump to this label on an OE_Ignore resolution */ + int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */ +){ + int i; /* loop counter */ + Vdbe *v; /* VDBE under constrution */ + int nCol; /* Number of columns */ + int onError; /* Conflict resolution strategy */ + int j1; /* Addresss of jump instruction */ + int j2 = 0, j3; /* Addresses of jump instructions */ + int regData; /* Register containing first data column */ + int iCur; /* Table cursor number */ + Index *pIdx; /* Pointer to one of the indices */ + int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */ + int regOldRowid = (rowidChng && isUpdate) ? rowidChng : regRowid; + + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); + assert( pTab->pSelect==0 ); /* This table is not a VIEW */ + nCol = pTab->nCol; + regData = regRowid + 1; + + /* Test all NOT NULL constraints. + */ + for(i=0; iiPKey ){ + continue; + } + onError = pTab->aCol[i].notNull; + if( onError==OE_None ) continue; + if( overrideError!=OE_Default ){ + onError = overrideError; + }else if( onError==OE_Default ){ + onError = OE_Abort; + } + if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){ + onError = OE_Abort; + } + assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail + || onError==OE_Ignore || onError==OE_Replace ); + switch( onError ){ + case OE_Abort: + sqlite3MayAbort(pParse); + case OE_Rollback: + case OE_Fail: { + char *zMsg; + sqlite3VdbeAddOp3(v, OP_HaltIfNull, + SQLITE_CONSTRAINT, onError, regData+i); + zMsg = sqlite3MPrintf(pParse->db, "%s.%s may not be NULL", + pTab->zName, pTab->aCol[i].zName); + sqlite3VdbeChangeP4(v, -1, zMsg, P4_DYNAMIC); + break; + } + case OE_Ignore: { + sqlite3VdbeAddOp2(v, OP_IsNull, regData+i, ignoreDest); + break; + } + default: { + assert( onError==OE_Replace ); + j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regData+i); + sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regData+i); + sqlite3VdbeJumpHere(v, j1); + break; + } + } + } + + /* Test all CHECK constraints + */ +#ifndef SQLITE_OMIT_CHECK + if( pTab->pCheck && (pParse->db->flags & SQLITE_IgnoreChecks)==0 ){ + int allOk = sqlite3VdbeMakeLabel(v); + pParse->ckBase = regData; + sqlite3ExprIfTrue(pParse, pTab->pCheck, allOk, SQLITE_JUMPIFNULL); + onError = overrideError!=OE_Default ? overrideError : OE_Abort; + if( onError==OE_Ignore ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); + }else{ + if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */ + sqlite3HaltConstraint(pParse, onError, 0, 0); + } + sqlite3VdbeResolveLabel(v, allOk); + } +#endif /* !defined(SQLITE_OMIT_CHECK) */ + + /* If we have an INTEGER PRIMARY KEY, make sure the primary key + ** of the new record does not previously exist. Except, if this + ** is an UPDATE and the primary key is not changing, that is OK. + */ + if( rowidChng ){ + onError = pTab->keyConf; + if( overrideError!=OE_Default ){ + onError = overrideError; + }else if( onError==OE_Default ){ + onError = OE_Abort; + } + + if( isUpdate ){ + j2 = sqlite3VdbeAddOp3(v, OP_Eq, regRowid, 0, rowidChng); + } + j3 = sqlite3VdbeAddOp3(v, OP_NotExists, baseCur, 0, regRowid); + switch( onError ){ + default: { + onError = OE_Abort; + /* Fall thru into the next case */ + } + case OE_Rollback: + case OE_Abort: + case OE_Fail: { + sqlite3HaltConstraint( + pParse, onError, "PRIMARY KEY must be unique", P4_STATIC); + break; + } + case OE_Replace: { + /* If there are DELETE triggers on this table and the + ** recursive-triggers flag is set, call GenerateRowDelete() to + ** remove the conflicting row from the the table. This will fire + ** the triggers and remove both the table and index b-tree entries. + ** + ** Otherwise, if there are no triggers or the recursive-triggers + ** flag is not set, but the table has one or more indexes, call + ** GenerateRowIndexDelete(). This removes the index b-tree entries + ** only. The table b-tree entry will be replaced by the new entry + ** when it is inserted. + ** + ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called, + ** also invoke MultiWrite() to indicate that this VDBE may require + ** statement rollback (if the statement is aborted after the delete + ** takes place). Earlier versions called sqlite3MultiWrite() regardless, + ** but being more selective here allows statements like: + ** + ** REPLACE INTO t(rowid) VALUES($newrowid) + ** + ** to run without a statement journal if there are no indexes on the + ** table. + */ + Trigger *pTrigger = 0; + if( pParse->db->flags&SQLITE_RecTriggers ){ + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); + } + if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){ + sqlite3MultiWrite(pParse); + sqlite3GenerateRowDelete( + pParse, pTab, baseCur, regRowid, 0, pTrigger, OE_Replace + ); + }else if( pTab->pIndex ){ + sqlite3MultiWrite(pParse); + sqlite3GenerateRowIndexDelete(pParse, pTab, baseCur, 0); + } + seenReplace = 1; + break; + } + case OE_Ignore: { + assert( seenReplace==0 ); + sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); + break; + } + } + sqlite3VdbeJumpHere(v, j3); + if( isUpdate ){ + sqlite3VdbeJumpHere(v, j2); + } + } + + /* Test all UNIQUE constraints by creating entries for each UNIQUE + ** index and making sure that duplicate entries do not already exist. + ** Add the new records to the indices as we go. + */ + for(iCur=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, iCur++){ + int regIdx; +#ifndef SQLITE_OMIT_UNIQUE_ENFORCEMENT + int regR; +#endif + if( aRegIdx[iCur]==0 ) continue; /* Skip unused indices */ + + /* Create a key for accessing the index entry */ + regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn+1); + for(i=0; inColumn; i++){ + int idx = pIdx->aiColumn[i]; + if( idx==pTab->iPKey ){ + sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); + }else{ + sqlite3VdbeAddOp2(v, OP_SCopy, regData+idx, regIdx+i); + } + } + sqlite3VdbeAddOp2(v, OP_SCopy, regRowid, regIdx+i); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn+1, aRegIdx[iCur]); + sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), 0); + sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn+1); + +#ifdef SQLITE_OMIT_UNIQUE_ENFORCEMENT + sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1); + continue; /* Treat pIdx as if it is not a UNIQUE index */ +#else + + /* Find out what action to take in case there is an indexing conflict */ + onError = pIdx->onError; + if( onError==OE_None ){ + sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1); + continue; /* pIdx is not a UNIQUE index */ + } + if( overrideError!=OE_Default ){ + onError = overrideError; + }else if( onError==OE_Default ){ + onError = OE_Abort; + } + if( seenReplace ){ + if( onError==OE_Ignore ) onError = OE_Replace; + else if( onError==OE_Fail ) onError = OE_Abort; + } + + /* Check to see if the new index entry will be unique */ + regR = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_SCopy, regOldRowid, regR); + j3 = sqlite3VdbeAddOp4(v, OP_IsUnique, baseCur+iCur+1, 0, + regR, SQLITE_INT_TO_PTR(regIdx), + P4_INT32); + sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn+1); + + /* Generate code that executes if the new index entry is not unique */ + assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail + || onError==OE_Ignore || onError==OE_Replace ); + switch( onError ){ + case OE_Rollback: + case OE_Abort: + case OE_Fail: { + int j; + StrAccum errMsg; + const char *zSep; + char *zErr; + + sqlite3StrAccumInit(&errMsg, 0, 0, 200); + errMsg.db = pParse->db; + zSep = pIdx->nColumn>1 ? "columns " : "column "; + for(j=0; jnColumn; j++){ + char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName; + sqlite3StrAccumAppend(&errMsg, zSep, -1); + zSep = ", "; + sqlite3StrAccumAppend(&errMsg, zCol, -1); + } + sqlite3StrAccumAppend(&errMsg, + pIdx->nColumn>1 ? " are not unique" : " is not unique", -1); + zErr = sqlite3StrAccumFinish(&errMsg); + sqlite3HaltConstraint(pParse, onError, zErr, 0); + sqlite3DbFree(errMsg.db, zErr); + break; + } + case OE_Ignore: { + assert( seenReplace==0 ); + sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest); + break; + } + default: { + Trigger *pTrigger = 0; + assert( onError==OE_Replace ); + sqlite3MultiWrite(pParse); + if( pParse->db->flags&SQLITE_RecTriggers ){ + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0); + } + sqlite3GenerateRowDelete( + pParse, pTab, baseCur, regR, 0, pTrigger, OE_Replace + ); + seenReplace = 1; + break; + } + } + sqlite3VdbeJumpHere(v, j3); + sqlite3ReleaseTempReg(pParse, regR); +#endif + } + + if( pbMayReplace ){ + *pbMayReplace = seenReplace; + } +} + +/* +** This routine generates code to finish the INSERT or UPDATE operation +** that was started by a prior call to sqlite3GenerateConstraintChecks. +** A consecutive range of registers starting at regRowid contains the +** rowid and the content to be inserted. +** +** The arguments to this routine should be the same as the first six +** arguments to sqlite3GenerateConstraintChecks. +*/ +SQLITE_PRIVATE void sqlite3CompleteInsertion( + Parse *pParse, /* The parser context */ + Table *pTab, /* the table into which we are inserting */ + int baseCur, /* Index of a read/write cursor pointing at pTab */ + int regRowid, /* Range of content */ + int *aRegIdx, /* Register used by each index. 0 for unused indices */ + int isUpdate, /* True for UPDATE, False for INSERT */ + int appendBias, /* True if this is likely to be an append */ + int useSeekResult /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */ +){ + int i; + Vdbe *v; + int nIdx; + Index *pIdx; + u8 pik_flags; + int regData; + int regRec; + + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); + assert( pTab->pSelect==0 ); /* This table is not a VIEW */ + for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} + for(i=nIdx-1; i>=0; i--){ + if( aRegIdx[i]==0 ) continue; + sqlite3VdbeAddOp2(v, OP_IdxInsert, baseCur+i+1, aRegIdx[i]); + if( useSeekResult ){ + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); + } + } + regData = regRowid + 1; + regRec = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec); + sqlite3TableAffinityStr(v, pTab); + sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol); + if( pParse->nested ){ + pik_flags = 0; + }else{ + pik_flags = OPFLAG_NCHANGE; + pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID); + } + if( appendBias ){ + pik_flags |= OPFLAG_APPEND; + } + if( useSeekResult ){ + pik_flags |= OPFLAG_USESEEKRESULT; + } + sqlite3VdbeAddOp3(v, OP_Insert, baseCur, regRec, regRowid); + if( !pParse->nested ){ + sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_STATIC); + } + sqlite3VdbeChangeP5(v, pik_flags); +} + +/* +** Generate code that will open cursors for a table and for all +** indices of that table. The "baseCur" parameter is the cursor number used +** for the table. Indices are opened on subsequent cursors. +** +** Return the number of indices on the table. +*/ +SQLITE_PRIVATE int sqlite3OpenTableAndIndices( + Parse *pParse, /* Parsing context */ + Table *pTab, /* Table to be opened */ + int baseCur, /* Cursor number assigned to the table */ + int op /* OP_OpenRead or OP_OpenWrite */ +){ + int i; + int iDb; + Index *pIdx; + Vdbe *v; + + if( IsVirtual(pTab) ) return 0; + iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); + sqlite3OpenTable(pParse, baseCur, iDb, pTab, op); + for(i=1, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ + KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); + assert( pIdx->pSchema==pTab->pSchema ); + sqlite3VdbeAddOp4(v, op, i+baseCur, pIdx->tnum, iDb, + (char*)pKey, P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pIdx->zName)); + } + if( pParse->nTabnTab = baseCur+i; + } + return i-1; +} + + +#ifdef SQLITE_TEST +/* +** The following global variable is incremented whenever the +** transfer optimization is used. This is used for testing +** purposes only - to make sure the transfer optimization really +** is happening when it is suppose to. +*/ +SQLITE_API int sqlite3_xferopt_count; +#endif /* SQLITE_TEST */ + + +#ifndef SQLITE_OMIT_XFER_OPT +/* +** Check to collation names to see if they are compatible. +*/ +static int xferCompatibleCollation(const char *z1, const char *z2){ + if( z1==0 ){ + return z2==0; + } + if( z2==0 ){ + return 0; + } + return sqlite3StrICmp(z1, z2)==0; +} + + +/* +** Check to see if index pSrc is compatible as a source of data +** for index pDest in an insert transfer optimization. The rules +** for a compatible index: +** +** * The index is over the same set of columns +** * The same DESC and ASC markings occurs on all columns +** * The same onError processing (OE_Abort, OE_Ignore, etc) +** * The same collating sequence on each column +*/ +static int xferCompatibleIndex(Index *pDest, Index *pSrc){ + int i; + assert( pDest && pSrc ); + assert( pDest->pTable!=pSrc->pTable ); + if( pDest->nColumn!=pSrc->nColumn ){ + return 0; /* Different number of columns */ + } + if( pDest->onError!=pSrc->onError ){ + return 0; /* Different conflict resolution strategies */ + } + for(i=0; inColumn; i++){ + if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){ + return 0; /* Different columns indexed */ + } + if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){ + return 0; /* Different sort orders */ + } + if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){ + return 0; /* Different collating sequences */ + } + } + + /* If no test above fails then the indices must be compatible */ + return 1; +} + +/* +** Attempt the transfer optimization on INSERTs of the form +** +** INSERT INTO tab1 SELECT * FROM tab2; +** +** This optimization is only attempted if +** +** (1) tab1 and tab2 have identical schemas including all the +** same indices and constraints +** +** (2) tab1 and tab2 are different tables +** +** (3) There must be no triggers on tab1 +** +** (4) The result set of the SELECT statement is "*" +** +** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY, +** or LIMIT clause. +** +** (6) The SELECT statement is a simple (not a compound) select that +** contains only tab2 in its FROM clause +** +** This method for implementing the INSERT transfers raw records from +** tab2 over to tab1. The columns are not decoded. Raw records from +** the indices of tab2 are transfered to tab1 as well. In so doing, +** the resulting tab1 has much less fragmentation. +** +** This routine returns TRUE if the optimization is attempted. If any +** of the conditions above fail so that the optimization should not +** be attempted, then this routine returns FALSE. +*/ +static int xferOptimization( + Parse *pParse, /* Parser context */ + Table *pDest, /* The table we are inserting into */ + Select *pSelect, /* A SELECT statement to use as the data source */ + int onError, /* How to handle constraint errors */ + int iDbDest /* The database of pDest */ +){ + ExprList *pEList; /* The result set of the SELECT */ + Table *pSrc; /* The table in the FROM clause of SELECT */ + Index *pSrcIdx, *pDestIdx; /* Source and destination indices */ + struct SrcList_item *pItem; /* An element of pSelect->pSrc */ + int i; /* Loop counter */ + int iDbSrc; /* The database of pSrc */ + int iSrc, iDest; /* Cursors from source and destination */ + int addr1, addr2; /* Loop addresses */ + int emptyDestTest; /* Address of test for empty pDest */ + int emptySrcTest; /* Address of test for empty pSrc */ + Vdbe *v; /* The VDBE we are building */ + KeyInfo *pKey; /* Key information for an index */ + int regAutoinc; /* Memory register used by AUTOINC */ + int destHasUniqueIdx = 0; /* True if pDest has a UNIQUE index */ + int regData, regRowid; /* Registers holding data and rowid */ + + if( pSelect==0 ){ + return 0; /* Must be of the form INSERT INTO ... SELECT ... */ + } + if( sqlite3TriggerList(pParse, pDest) ){ + return 0; /* tab1 must not have triggers */ + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( pDest->tabFlags & TF_Virtual ){ + return 0; /* tab1 must not be a virtual table */ + } +#endif + if( onError==OE_Default ){ + onError = OE_Abort; + } + if( onError!=OE_Abort && onError!=OE_Rollback ){ + return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */ + } + assert(pSelect->pSrc); /* allocated even if there is no FROM clause */ + if( pSelect->pSrc->nSrc!=1 ){ + return 0; /* FROM clause must have exactly one term */ + } + if( pSelect->pSrc->a[0].pSelect ){ + return 0; /* FROM clause cannot contain a subquery */ + } + if( pSelect->pWhere ){ + return 0; /* SELECT may not have a WHERE clause */ + } + if( pSelect->pOrderBy ){ + return 0; /* SELECT may not have an ORDER BY clause */ + } + /* Do not need to test for a HAVING clause. If HAVING is present but + ** there is no ORDER BY, we will get an error. */ + if( pSelect->pGroupBy ){ + return 0; /* SELECT may not have a GROUP BY clause */ + } + if( pSelect->pLimit ){ + return 0; /* SELECT may not have a LIMIT clause */ + } + assert( pSelect->pOffset==0 ); /* Must be so if pLimit==0 */ + if( pSelect->pPrior ){ + return 0; /* SELECT may not be a compound query */ + } + if( pSelect->selFlags & SF_Distinct ){ + return 0; /* SELECT may not be DISTINCT */ + } + pEList = pSelect->pEList; + assert( pEList!=0 ); + if( pEList->nExpr!=1 ){ + return 0; /* The result set must have exactly one column */ + } + assert( pEList->a[0].pExpr ); + if( pEList->a[0].pExpr->op!=TK_ALL ){ + return 0; /* The result set must be the special operator "*" */ + } + + /* At this point we have established that the statement is of the + ** correct syntactic form to participate in this optimization. Now + ** we have to check the semantics. + */ + pItem = pSelect->pSrc->a; + pSrc = sqlite3LocateTable(pParse, 0, pItem->zName, pItem->zDatabase); + if( pSrc==0 ){ + return 0; /* FROM clause does not contain a real table */ + } + if( pSrc==pDest ){ + return 0; /* tab1 and tab2 may not be the same table */ + } +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( pSrc->tabFlags & TF_Virtual ){ + return 0; /* tab2 must not be a virtual table */ + } +#endif + if( pSrc->pSelect ){ + return 0; /* tab2 may not be a view */ + } + if( pDest->nCol!=pSrc->nCol ){ + return 0; /* Number of columns must be the same in tab1 and tab2 */ + } + if( pDest->iPKey!=pSrc->iPKey ){ + return 0; /* Both tables must have the same INTEGER PRIMARY KEY */ + } + for(i=0; inCol; i++){ + if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){ + return 0; /* Affinity must be the same on all columns */ + } + if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){ + return 0; /* Collating sequence must be the same on all columns */ + } + if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){ + return 0; /* tab2 must be NOT NULL if tab1 is */ + } + } + for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ + if( pDestIdx->onError!=OE_None ){ + destHasUniqueIdx = 1; + } + for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){ + if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; + } + if( pSrcIdx==0 ){ + return 0; /* pDestIdx has no corresponding index in pSrc */ + } + } +#ifndef SQLITE_OMIT_CHECK + if( pDest->pCheck && sqlite3ExprCompare(pSrc->pCheck, pDest->pCheck) ){ + return 0; /* Tables have different CHECK constraints. Ticket #2252 */ + } +#endif + + /* If we get this far, it means either: + ** + ** * We can always do the transfer if the table contains an + ** an integer primary key + ** + ** * We can conditionally do the transfer if the destination + ** table is empty. + */ +#ifdef SQLITE_TEST + sqlite3_xferopt_count++; +#endif + iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema); + v = sqlite3GetVdbe(pParse); + sqlite3CodeVerifySchema(pParse, iDbSrc); + iSrc = pParse->nTab++; + iDest = pParse->nTab++; + regAutoinc = autoIncBegin(pParse, iDbDest, pDest); + sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite); + if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){ + /* If tables do not have an INTEGER PRIMARY KEY and there + ** are indices to be copied and the destination is not empty, + ** we have to disallow the transfer optimization because the + ** the rowids might change which will mess up indexing. + ** + ** Or if the destination has a UNIQUE index and is not empty, + ** we also disallow the transfer optimization because we cannot + ** insure that all entries in the union of DEST and SRC will be + ** unique. + */ + addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0); + emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0); + sqlite3VdbeJumpHere(v, addr1); + }else{ + emptyDestTest = 0; + } + sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead); + emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); + regData = sqlite3GetTempReg(pParse); + regRowid = sqlite3GetTempReg(pParse); + if( pDest->iPKey>=0 ){ + addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); + addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid); + sqlite3HaltConstraint( + pParse, onError, "PRIMARY KEY must be unique", P4_STATIC); + sqlite3VdbeJumpHere(v, addr2); + autoIncStep(pParse, regAutoinc, regRowid); + }else if( pDest->pIndex==0 ){ + addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid); + }else{ + addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid); + assert( (pDest->tabFlags & TF_Autoincrement)==0 ); + } + sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData); + sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid); + sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND); + sqlite3VdbeChangeP4(v, -1, pDest->zName, 0); + sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1); + for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){ + for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){ + if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break; + } + assert( pSrcIdx ); + sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); + pKey = sqlite3IndexKeyinfo(pParse, pSrcIdx); + sqlite3VdbeAddOp4(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc, + (char*)pKey, P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pSrcIdx->zName)); + pKey = sqlite3IndexKeyinfo(pParse, pDestIdx); + sqlite3VdbeAddOp4(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest, + (char*)pKey, P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pDestIdx->zName)); + addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0); + sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData); + sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1); + sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1); + sqlite3VdbeJumpHere(v, addr1); + } + sqlite3VdbeJumpHere(v, emptySrcTest); + sqlite3ReleaseTempReg(pParse, regRowid); + sqlite3ReleaseTempReg(pParse, regData); + sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0); + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); + if( emptyDestTest ){ + sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0); + sqlite3VdbeJumpHere(v, emptyDestTest); + sqlite3VdbeAddOp2(v, OP_Close, iDest, 0); + return 0; + }else{ + return 1; + } +} +#endif /* SQLITE_OMIT_XFER_OPT */ + +/************** End of insert.c **********************************************/ +/************** Begin file legacy.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Main file for the SQLite library. The routines in this file +** implement the programmer interface to the library. Routines in +** other files are for internal use by SQLite and should not be +** accessed by users of the library. +*/ + + +/* +** Execute SQL code. Return one of the SQLITE_ success/failure +** codes. Also write an error message into memory obtained from +** malloc() and make *pzErrMsg point to that message. +** +** If the SQL is a query, then for each row in the query result +** the xCallback() function is called. pArg becomes the first +** argument to xCallback(). If xCallback=NULL then no callback +** is invoked, even for queries. +*/ +SQLITE_API int sqlite3_exec( + sqlite3 *db, /* The database on which the SQL executes */ + const char *zSql, /* The SQL to be executed */ + sqlite3_callback xCallback, /* Invoke this callback routine */ + void *pArg, /* First argument to xCallback() */ + char **pzErrMsg /* Write error messages here */ +){ + int rc = SQLITE_OK; /* Return code */ + const char *zLeftover; /* Tail of unprocessed SQL */ + sqlite3_stmt *pStmt = 0; /* The current SQL statement */ + char **azCols = 0; /* Names of result columns */ + int nRetry = 0; /* Number of retry attempts */ + int callbackIsInit; /* True if callback data is initialized */ + + if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT; + if( zSql==0 ) zSql = ""; + + sqlite3_mutex_enter(db->mutex); + sqlite3Error(db, SQLITE_OK, 0); + while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){ + int nCol; + char **azVals = 0; + + pStmt = 0; + rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover); + assert( rc==SQLITE_OK || pStmt==0 ); + if( rc!=SQLITE_OK ){ + continue; + } + if( !pStmt ){ + /* this happens for a comment or white-space */ + zSql = zLeftover; + continue; + } + + callbackIsInit = 0; + nCol = sqlite3_column_count(pStmt); + + while( 1 ){ + int i; + rc = sqlite3_step(pStmt); + + /* Invoke the callback function if required */ + if( xCallback && (SQLITE_ROW==rc || + (SQLITE_DONE==rc && !callbackIsInit + && db->flags&SQLITE_NullCallback)) ){ + if( !callbackIsInit ){ + azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1); + if( azCols==0 ){ + goto exec_out; + } + for(i=0; imallocFailed = 1; + goto exec_out; + } + } + } + if( xCallback(pArg, nCol, azVals, azCols) ){ + rc = SQLITE_ABORT; + sqlite3VdbeFinalize((Vdbe *)pStmt); + pStmt = 0; + sqlite3Error(db, SQLITE_ABORT, 0); + goto exec_out; + } + } + + if( rc!=SQLITE_ROW ){ + rc = sqlite3VdbeFinalize((Vdbe *)pStmt); + pStmt = 0; + if( rc!=SQLITE_SCHEMA ){ + nRetry = 0; + zSql = zLeftover; + while( sqlite3Isspace(zSql[0]) ) zSql++; + } + break; + } + } + + sqlite3DbFree(db, azCols); + azCols = 0; + } + +exec_out: + if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt); + sqlite3DbFree(db, azCols); + + rc = sqlite3ApiExit(db, rc); + if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){ + int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db)); + *pzErrMsg = sqlite3Malloc(nErrMsg); + if( *pzErrMsg ){ + memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg); + }else{ + rc = SQLITE_NOMEM; + sqlite3Error(db, SQLITE_NOMEM, 0); + } + }else if( pzErrMsg ){ + *pzErrMsg = 0; + } + + assert( (rc&db->errMask)==rc ); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/************** End of legacy.c **********************************************/ +/************** Begin file loadext.c *****************************************/ +/* +** 2006 June 7 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to dynamically load extensions into +** the SQLite library. +*/ + +#ifndef SQLITE_CORE + #define SQLITE_CORE 1 /* Disable the API redefinition in sqlite3ext.h */ +#endif +/************** Include sqlite3ext.h in the middle of loadext.c **************/ +/************** Begin file sqlite3ext.h **************************************/ +/* +** 2006 June 7 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the SQLite interface for use by +** shared libraries that want to be imported as extensions into +** an SQLite instance. Shared libraries that intend to be loaded +** as extensions by SQLite should #include this file instead of +** sqlite3.h. +*/ +#ifndef _SQLITE3EXT_H_ +#define _SQLITE3EXT_H_ + +typedef struct sqlite3_api_routines sqlite3_api_routines; + +/* +** The following structure holds pointers to all of the SQLite API +** routines. +** +** WARNING: In order to maintain backwards compatibility, add new +** interfaces to the end of this structure only. If you insert new +** interfaces in the middle of this structure, then older different +** versions of SQLite will not be able to load each others' shared +** libraries! +*/ +struct sqlite3_api_routines { + void * (*aggregate_context)(sqlite3_context*,int nBytes); + int (*aggregate_count)(sqlite3_context*); + int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*)); + int (*bind_double)(sqlite3_stmt*,int,double); + int (*bind_int)(sqlite3_stmt*,int,int); + int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64); + int (*bind_null)(sqlite3_stmt*,int); + int (*bind_parameter_count)(sqlite3_stmt*); + int (*bind_parameter_index)(sqlite3_stmt*,const char*zName); + const char * (*bind_parameter_name)(sqlite3_stmt*,int); + int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*)); + int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*)); + int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*); + int (*busy_handler)(sqlite3*,int(*)(void*,int),void*); + int (*busy_timeout)(sqlite3*,int ms); + int (*changes)(sqlite3*); + int (*close)(sqlite3*); + int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*)); + int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*)); + const void * (*column_blob)(sqlite3_stmt*,int iCol); + int (*column_bytes)(sqlite3_stmt*,int iCol); + int (*column_bytes16)(sqlite3_stmt*,int iCol); + int (*column_count)(sqlite3_stmt*pStmt); + const char * (*column_database_name)(sqlite3_stmt*,int); + const void * (*column_database_name16)(sqlite3_stmt*,int); + const char * (*column_decltype)(sqlite3_stmt*,int i); + const void * (*column_decltype16)(sqlite3_stmt*,int); + double (*column_double)(sqlite3_stmt*,int iCol); + int (*column_int)(sqlite3_stmt*,int iCol); + sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol); + const char * (*column_name)(sqlite3_stmt*,int); + const void * (*column_name16)(sqlite3_stmt*,int); + const char * (*column_origin_name)(sqlite3_stmt*,int); + const void * (*column_origin_name16)(sqlite3_stmt*,int); + const char * (*column_table_name)(sqlite3_stmt*,int); + const void * (*column_table_name16)(sqlite3_stmt*,int); + const unsigned char * (*column_text)(sqlite3_stmt*,int iCol); + const void * (*column_text16)(sqlite3_stmt*,int iCol); + int (*column_type)(sqlite3_stmt*,int iCol); + sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol); + void * (*commit_hook)(sqlite3*,int(*)(void*),void*); + int (*complete)(const char*sql); + int (*complete16)(const void*sql); + int (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*)); + int (*create_collation16)(sqlite3*,const void*,int,void*,int(*)(void*,int,const void*,int,const void*)); + int (*create_function)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*)); + int (*create_function16)(sqlite3*,const void*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*)); + int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*); + int (*data_count)(sqlite3_stmt*pStmt); + sqlite3 * (*db_handle)(sqlite3_stmt*); + int (*declare_vtab)(sqlite3*,const char*); + int (*enable_shared_cache)(int); + int (*errcode)(sqlite3*db); + const char * (*errmsg)(sqlite3*); + const void * (*errmsg16)(sqlite3*); + int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**); + int (*expired)(sqlite3_stmt*); + int (*finalize)(sqlite3_stmt*pStmt); + void (*free)(void*); + void (*free_table)(char**result); + int (*get_autocommit)(sqlite3*); + void * (*get_auxdata)(sqlite3_context*,int); + int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**); + int (*global_recover)(void); + void (*interruptx)(sqlite3*); + sqlite_int64 (*last_insert_rowid)(sqlite3*); + const char * (*libversion)(void); + int (*libversion_number)(void); + void *(*malloc)(int); + char * (*mprintf)(const char*,...); + int (*open)(const char*,sqlite3**); + int (*open16)(const void*,sqlite3**); + int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); + int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); + void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*); + void (*progress_handler)(sqlite3*,int,int(*)(void*),void*); + void *(*realloc)(void*,int); + int (*reset)(sqlite3_stmt*pStmt); + void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_double)(sqlite3_context*,double); + void (*result_error)(sqlite3_context*,const char*,int); + void (*result_error16)(sqlite3_context*,const void*,int); + void (*result_int)(sqlite3_context*,int); + void (*result_int64)(sqlite3_context*,sqlite_int64); + void (*result_null)(sqlite3_context*); + void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*)); + void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_value)(sqlite3_context*,sqlite3_value*); + void * (*rollback_hook)(sqlite3*,void(*)(void*),void*); + int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*); + void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); + char * (*snprintf)(int,char*,const char*,...); + int (*step)(sqlite3_stmt*); + int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*); + void (*thread_cleanup)(void); + int (*total_changes)(sqlite3*); + void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*); + int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*); + void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*); + void * (*user_data)(sqlite3_context*); + const void * (*value_blob)(sqlite3_value*); + int (*value_bytes)(sqlite3_value*); + int (*value_bytes16)(sqlite3_value*); + double (*value_double)(sqlite3_value*); + int (*value_int)(sqlite3_value*); + sqlite_int64 (*value_int64)(sqlite3_value*); + int (*value_numeric_type)(sqlite3_value*); + const unsigned char * (*value_text)(sqlite3_value*); + const void * (*value_text16)(sqlite3_value*); + const void * (*value_text16be)(sqlite3_value*); + const void * (*value_text16le)(sqlite3_value*); + int (*value_type)(sqlite3_value*); + char *(*vmprintf)(const char*,va_list); + /* Added ??? */ + int (*overload_function)(sqlite3*, const char *zFuncName, int nArg); + /* Added by 3.3.13 */ + int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); + int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); + int (*clear_bindings)(sqlite3_stmt*); + /* Added by 3.4.1 */ + int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *)); + /* Added by 3.5.0 */ + int (*bind_zeroblob)(sqlite3_stmt*,int,int); + int (*blob_bytes)(sqlite3_blob*); + int (*blob_close)(sqlite3_blob*); + int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**); + int (*blob_read)(sqlite3_blob*,void*,int,int); + int (*blob_write)(sqlite3_blob*,const void*,int,int); + int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*)); + int (*file_control)(sqlite3*,const char*,int,void*); + sqlite3_int64 (*memory_highwater)(int); + sqlite3_int64 (*memory_used)(void); + sqlite3_mutex *(*mutex_alloc)(int); + void (*mutex_enter)(sqlite3_mutex*); + void (*mutex_free)(sqlite3_mutex*); + void (*mutex_leave)(sqlite3_mutex*); + int (*mutex_try)(sqlite3_mutex*); + int (*open_v2)(const char*,sqlite3**,int,const char*); + int (*release_memory)(int); + void (*result_error_nomem)(sqlite3_context*); + void (*result_error_toobig)(sqlite3_context*); + int (*sleep)(int); + void (*soft_heap_limit)(int); + sqlite3_vfs *(*vfs_find)(const char*); + int (*vfs_register)(sqlite3_vfs*,int); + int (*vfs_unregister)(sqlite3_vfs*); + int (*xthreadsafe)(void); + void (*result_zeroblob)(sqlite3_context*,int); + void (*result_error_code)(sqlite3_context*,int); + int (*test_control)(int, ...); + void (*randomness)(int,void*); + sqlite3 *(*context_db_handle)(sqlite3_context*); + int (*extended_result_codes)(sqlite3*,int); + int (*limit)(sqlite3*,int,int); + sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*); + const char *(*sql)(sqlite3_stmt*); + int (*status)(int,int*,int*,int); + int (*backup_finish)(sqlite3_backup*); + sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*); + int (*backup_pagecount)(sqlite3_backup*); + int (*backup_remaining)(sqlite3_backup*); + int (*backup_step)(sqlite3_backup*,int); + const char *(*compileoption_get)(int); + int (*compileoption_used)(const char*); + int (*create_function_v2)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*),void(*xDestroy)(void*)); + int (*db_config)(sqlite3*,int,...); + sqlite3_mutex *(*db_mutex)(sqlite3*); + int (*db_status)(sqlite3*,int,int*,int*,int); + int (*extended_errcode)(sqlite3*); + void (*log)(int,const char*,...); + sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64); + const char *(*sourceid)(void); + int (*stmt_status)(sqlite3_stmt*,int,int); + int (*strnicmp)(const char*,const char*,int); + int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*); + int (*wal_autocheckpoint)(sqlite3*,int); + int (*wal_checkpoint)(sqlite3*,const char*); + void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*); +}; + +/* +** The following macros redefine the API routines so that they are +** redirected throught the global sqlite3_api structure. +** +** This header file is also used by the loadext.c source file +** (part of the main SQLite library - not an extension) so that +** it can get access to the sqlite3_api_routines structure +** definition. But the main library does not want to redefine +** the API. So the redefinition macros are only valid if the +** SQLITE_CORE macros is undefined. +*/ +#ifndef SQLITE_CORE +#define sqlite3_aggregate_context sqlite3_api->aggregate_context +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_aggregate_count sqlite3_api->aggregate_count +#endif +#define sqlite3_bind_blob sqlite3_api->bind_blob +#define sqlite3_bind_double sqlite3_api->bind_double +#define sqlite3_bind_int sqlite3_api->bind_int +#define sqlite3_bind_int64 sqlite3_api->bind_int64 +#define sqlite3_bind_null sqlite3_api->bind_null +#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count +#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index +#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name +#define sqlite3_bind_text sqlite3_api->bind_text +#define sqlite3_bind_text16 sqlite3_api->bind_text16 +#define sqlite3_bind_value sqlite3_api->bind_value +#define sqlite3_busy_handler sqlite3_api->busy_handler +#define sqlite3_busy_timeout sqlite3_api->busy_timeout +#define sqlite3_changes sqlite3_api->changes +#define sqlite3_close sqlite3_api->close +#define sqlite3_collation_needed sqlite3_api->collation_needed +#define sqlite3_collation_needed16 sqlite3_api->collation_needed16 +#define sqlite3_column_blob sqlite3_api->column_blob +#define sqlite3_column_bytes sqlite3_api->column_bytes +#define sqlite3_column_bytes16 sqlite3_api->column_bytes16 +#define sqlite3_column_count sqlite3_api->column_count +#define sqlite3_column_database_name sqlite3_api->column_database_name +#define sqlite3_column_database_name16 sqlite3_api->column_database_name16 +#define sqlite3_column_decltype sqlite3_api->column_decltype +#define sqlite3_column_decltype16 sqlite3_api->column_decltype16 +#define sqlite3_column_double sqlite3_api->column_double +#define sqlite3_column_int sqlite3_api->column_int +#define sqlite3_column_int64 sqlite3_api->column_int64 +#define sqlite3_column_name sqlite3_api->column_name +#define sqlite3_column_name16 sqlite3_api->column_name16 +#define sqlite3_column_origin_name sqlite3_api->column_origin_name +#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16 +#define sqlite3_column_table_name sqlite3_api->column_table_name +#define sqlite3_column_table_name16 sqlite3_api->column_table_name16 +#define sqlite3_column_text sqlite3_api->column_text +#define sqlite3_column_text16 sqlite3_api->column_text16 +#define sqlite3_column_type sqlite3_api->column_type +#define sqlite3_column_value sqlite3_api->column_value +#define sqlite3_commit_hook sqlite3_api->commit_hook +#define sqlite3_complete sqlite3_api->complete +#define sqlite3_complete16 sqlite3_api->complete16 +#define sqlite3_create_collation sqlite3_api->create_collation +#define sqlite3_create_collation16 sqlite3_api->create_collation16 +#define sqlite3_create_function sqlite3_api->create_function +#define sqlite3_create_function16 sqlite3_api->create_function16 +#define sqlite3_create_module sqlite3_api->create_module +#define sqlite3_create_module_v2 sqlite3_api->create_module_v2 +#define sqlite3_data_count sqlite3_api->data_count +#define sqlite3_db_handle sqlite3_api->db_handle +#define sqlite3_declare_vtab sqlite3_api->declare_vtab +#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache +#define sqlite3_errcode sqlite3_api->errcode +#define sqlite3_errmsg sqlite3_api->errmsg +#define sqlite3_errmsg16 sqlite3_api->errmsg16 +#define sqlite3_exec sqlite3_api->exec +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_expired sqlite3_api->expired +#endif +#define sqlite3_finalize sqlite3_api->finalize +#define sqlite3_free sqlite3_api->free +#define sqlite3_free_table sqlite3_api->free_table +#define sqlite3_get_autocommit sqlite3_api->get_autocommit +#define sqlite3_get_auxdata sqlite3_api->get_auxdata +#define sqlite3_get_table sqlite3_api->get_table +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_global_recover sqlite3_api->global_recover +#endif +#define sqlite3_interrupt sqlite3_api->interruptx +#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid +#define sqlite3_libversion sqlite3_api->libversion +#define sqlite3_libversion_number sqlite3_api->libversion_number +#define sqlite3_malloc sqlite3_api->malloc +#define sqlite3_mprintf sqlite3_api->mprintf +#define sqlite3_open sqlite3_api->open +#define sqlite3_open16 sqlite3_api->open16 +#define sqlite3_prepare sqlite3_api->prepare +#define sqlite3_prepare16 sqlite3_api->prepare16 +#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 +#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 +#define sqlite3_profile sqlite3_api->profile +#define sqlite3_progress_handler sqlite3_api->progress_handler +#define sqlite3_realloc sqlite3_api->realloc +#define sqlite3_reset sqlite3_api->reset +#define sqlite3_result_blob sqlite3_api->result_blob +#define sqlite3_result_double sqlite3_api->result_double +#define sqlite3_result_error sqlite3_api->result_error +#define sqlite3_result_error16 sqlite3_api->result_error16 +#define sqlite3_result_int sqlite3_api->result_int +#define sqlite3_result_int64 sqlite3_api->result_int64 +#define sqlite3_result_null sqlite3_api->result_null +#define sqlite3_result_text sqlite3_api->result_text +#define sqlite3_result_text16 sqlite3_api->result_text16 +#define sqlite3_result_text16be sqlite3_api->result_text16be +#define sqlite3_result_text16le sqlite3_api->result_text16le +#define sqlite3_result_value sqlite3_api->result_value +#define sqlite3_rollback_hook sqlite3_api->rollback_hook +#define sqlite3_set_authorizer sqlite3_api->set_authorizer +#define sqlite3_set_auxdata sqlite3_api->set_auxdata +#define sqlite3_snprintf sqlite3_api->snprintf +#define sqlite3_step sqlite3_api->step +#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata +#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup +#define sqlite3_total_changes sqlite3_api->total_changes +#define sqlite3_trace sqlite3_api->trace +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings +#endif +#define sqlite3_update_hook sqlite3_api->update_hook +#define sqlite3_user_data sqlite3_api->user_data +#define sqlite3_value_blob sqlite3_api->value_blob +#define sqlite3_value_bytes sqlite3_api->value_bytes +#define sqlite3_value_bytes16 sqlite3_api->value_bytes16 +#define sqlite3_value_double sqlite3_api->value_double +#define sqlite3_value_int sqlite3_api->value_int +#define sqlite3_value_int64 sqlite3_api->value_int64 +#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type +#define sqlite3_value_text sqlite3_api->value_text +#define sqlite3_value_text16 sqlite3_api->value_text16 +#define sqlite3_value_text16be sqlite3_api->value_text16be +#define sqlite3_value_text16le sqlite3_api->value_text16le +#define sqlite3_value_type sqlite3_api->value_type +#define sqlite3_vmprintf sqlite3_api->vmprintf +#define sqlite3_overload_function sqlite3_api->overload_function +#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 +#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 +#define sqlite3_clear_bindings sqlite3_api->clear_bindings +#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob +#define sqlite3_blob_bytes sqlite3_api->blob_bytes +#define sqlite3_blob_close sqlite3_api->blob_close +#define sqlite3_blob_open sqlite3_api->blob_open +#define sqlite3_blob_read sqlite3_api->blob_read +#define sqlite3_blob_write sqlite3_api->blob_write +#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2 +#define sqlite3_file_control sqlite3_api->file_control +#define sqlite3_memory_highwater sqlite3_api->memory_highwater +#define sqlite3_memory_used sqlite3_api->memory_used +#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc +#define sqlite3_mutex_enter sqlite3_api->mutex_enter +#define sqlite3_mutex_free sqlite3_api->mutex_free +#define sqlite3_mutex_leave sqlite3_api->mutex_leave +#define sqlite3_mutex_try sqlite3_api->mutex_try +#define sqlite3_open_v2 sqlite3_api->open_v2 +#define sqlite3_release_memory sqlite3_api->release_memory +#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem +#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig +#define sqlite3_sleep sqlite3_api->sleep +#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit +#define sqlite3_vfs_find sqlite3_api->vfs_find +#define sqlite3_vfs_register sqlite3_api->vfs_register +#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister +#define sqlite3_threadsafe sqlite3_api->xthreadsafe +#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob +#define sqlite3_result_error_code sqlite3_api->result_error_code +#define sqlite3_test_control sqlite3_api->test_control +#define sqlite3_randomness sqlite3_api->randomness +#define sqlite3_context_db_handle sqlite3_api->context_db_handle +#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes +#define sqlite3_limit sqlite3_api->limit +#define sqlite3_next_stmt sqlite3_api->next_stmt +#define sqlite3_sql sqlite3_api->sql +#define sqlite3_status sqlite3_api->status +#define sqlite3_backup_finish sqlite3_api->backup_finish +#define sqlite3_backup_init sqlite3_api->backup_init +#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount +#define sqlite3_backup_remaining sqlite3_api->backup_remaining +#define sqlite3_backup_step sqlite3_api->backup_step +#define sqlite3_compileoption_get sqlite3_api->compileoption_get +#define sqlite3_compileoption_used sqlite3_api->compileoption_used +#define sqlite3_create_function_v2 sqlite3_api->create_function_v2 +#define sqlite3_db_config sqlite3_api->db_config +#define sqlite3_db_mutex sqlite3_api->db_mutex +#define sqlite3_db_status sqlite3_api->db_status +#define sqlite3_extended_errcode sqlite3_api->extended_errcode +#define sqlite3_log sqlite3_api->log +#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64 +#define sqlite3_sourceid sqlite3_api->sourceid +#define sqlite3_stmt_status sqlite3_api->stmt_status +#define sqlite3_strnicmp sqlite3_api->strnicmp +#define sqlite3_unlock_notify sqlite3_api->unlock_notify +#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint +#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint +#define sqlite3_wal_hook sqlite3_api->wal_hook +#endif /* SQLITE_CORE */ + +#define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api = 0; +#define SQLITE_EXTENSION_INIT2(v) sqlite3_api = v; + +#endif /* _SQLITE3EXT_H_ */ + +/************** End of sqlite3ext.h ******************************************/ +/************** Continuing where we left off in loadext.c ********************/ + +#ifndef SQLITE_OMIT_LOAD_EXTENSION + +/* +** Some API routines are omitted when various features are +** excluded from a build of SQLite. Substitute a NULL pointer +** for any missing APIs. +*/ +#ifndef SQLITE_ENABLE_COLUMN_METADATA +# define sqlite3_column_database_name 0 +# define sqlite3_column_database_name16 0 +# define sqlite3_column_table_name 0 +# define sqlite3_column_table_name16 0 +# define sqlite3_column_origin_name 0 +# define sqlite3_column_origin_name16 0 +# define sqlite3_table_column_metadata 0 +#endif + +#ifdef SQLITE_OMIT_AUTHORIZATION +# define sqlite3_set_authorizer 0 +#endif + +#ifdef SQLITE_OMIT_UTF16 +# define sqlite3_bind_text16 0 +# define sqlite3_collation_needed16 0 +# define sqlite3_column_decltype16 0 +# define sqlite3_column_name16 0 +# define sqlite3_column_text16 0 +# define sqlite3_complete16 0 +# define sqlite3_create_collation16 0 +# define sqlite3_create_function16 0 +# define sqlite3_errmsg16 0 +# define sqlite3_open16 0 +# define sqlite3_prepare16 0 +# define sqlite3_prepare16_v2 0 +# define sqlite3_result_error16 0 +# define sqlite3_result_text16 0 +# define sqlite3_result_text16be 0 +# define sqlite3_result_text16le 0 +# define sqlite3_value_text16 0 +# define sqlite3_value_text16be 0 +# define sqlite3_value_text16le 0 +# define sqlite3_column_database_name16 0 +# define sqlite3_column_table_name16 0 +# define sqlite3_column_origin_name16 0 +#endif + +#ifdef SQLITE_OMIT_COMPLETE +# define sqlite3_complete 0 +# define sqlite3_complete16 0 +#endif + +#ifdef SQLITE_OMIT_PROGRESS_CALLBACK +# define sqlite3_progress_handler 0 +#endif + +#ifdef SQLITE_OMIT_VIRTUALTABLE +# define sqlite3_create_module 0 +# define sqlite3_create_module_v2 0 +# define sqlite3_declare_vtab 0 +#endif + +#ifdef SQLITE_OMIT_SHARED_CACHE +# define sqlite3_enable_shared_cache 0 +#endif + +#ifdef SQLITE_OMIT_TRACE +# define sqlite3_profile 0 +# define sqlite3_trace 0 +#endif + +#ifdef SQLITE_OMIT_GET_TABLE +# define sqlite3_free_table 0 +# define sqlite3_get_table 0 +#endif + +#ifdef SQLITE_OMIT_INCRBLOB +#define sqlite3_bind_zeroblob 0 +#define sqlite3_blob_bytes 0 +#define sqlite3_blob_close 0 +#define sqlite3_blob_open 0 +#define sqlite3_blob_read 0 +#define sqlite3_blob_write 0 +#endif + +/* +** The following structure contains pointers to all SQLite API routines. +** A pointer to this structure is passed into extensions when they are +** loaded so that the extension can make calls back into the SQLite +** library. +** +** When adding new APIs, add them to the bottom of this structure +** in order to preserve backwards compatibility. +** +** Extensions that use newer APIs should first call the +** sqlite3_libversion_number() to make sure that the API they +** intend to use is supported by the library. Extensions should +** also check to make sure that the pointer to the function is +** not NULL before calling it. +*/ +static const sqlite3_api_routines sqlite3Apis = { + sqlite3_aggregate_context, +#ifndef SQLITE_OMIT_DEPRECATED + sqlite3_aggregate_count, +#else + 0, +#endif + sqlite3_bind_blob, + sqlite3_bind_double, + sqlite3_bind_int, + sqlite3_bind_int64, + sqlite3_bind_null, + sqlite3_bind_parameter_count, + sqlite3_bind_parameter_index, + sqlite3_bind_parameter_name, + sqlite3_bind_text, + sqlite3_bind_text16, + sqlite3_bind_value, + sqlite3_busy_handler, + sqlite3_busy_timeout, + sqlite3_changes, + sqlite3_close, + sqlite3_collation_needed, + sqlite3_collation_needed16, + sqlite3_column_blob, + sqlite3_column_bytes, + sqlite3_column_bytes16, + sqlite3_column_count, + sqlite3_column_database_name, + sqlite3_column_database_name16, + sqlite3_column_decltype, + sqlite3_column_decltype16, + sqlite3_column_double, + sqlite3_column_int, + sqlite3_column_int64, + sqlite3_column_name, + sqlite3_column_name16, + sqlite3_column_origin_name, + sqlite3_column_origin_name16, + sqlite3_column_table_name, + sqlite3_column_table_name16, + sqlite3_column_text, + sqlite3_column_text16, + sqlite3_column_type, + sqlite3_column_value, + sqlite3_commit_hook, + sqlite3_complete, + sqlite3_complete16, + sqlite3_create_collation, + sqlite3_create_collation16, + sqlite3_create_function, + sqlite3_create_function16, + sqlite3_create_module, + sqlite3_data_count, + sqlite3_db_handle, + sqlite3_declare_vtab, + sqlite3_enable_shared_cache, + sqlite3_errcode, + sqlite3_errmsg, + sqlite3_errmsg16, + sqlite3_exec, +#ifndef SQLITE_OMIT_DEPRECATED + sqlite3_expired, +#else + 0, +#endif + sqlite3_finalize, + sqlite3_free, + sqlite3_free_table, + sqlite3_get_autocommit, + sqlite3_get_auxdata, + sqlite3_get_table, + 0, /* Was sqlite3_global_recover(), but that function is deprecated */ + sqlite3_interrupt, + sqlite3_last_insert_rowid, + sqlite3_libversion, + sqlite3_libversion_number, + sqlite3_malloc, + sqlite3_mprintf, + sqlite3_open, + sqlite3_open16, + sqlite3_prepare, + sqlite3_prepare16, + sqlite3_profile, + sqlite3_progress_handler, + sqlite3_realloc, + sqlite3_reset, + sqlite3_result_blob, + sqlite3_result_double, + sqlite3_result_error, + sqlite3_result_error16, + sqlite3_result_int, + sqlite3_result_int64, + sqlite3_result_null, + sqlite3_result_text, + sqlite3_result_text16, + sqlite3_result_text16be, + sqlite3_result_text16le, + sqlite3_result_value, + sqlite3_rollback_hook, + sqlite3_set_authorizer, + sqlite3_set_auxdata, + sqlite3_snprintf, + sqlite3_step, + sqlite3_table_column_metadata, +#ifndef SQLITE_OMIT_DEPRECATED + sqlite3_thread_cleanup, +#else + 0, +#endif + sqlite3_total_changes, + sqlite3_trace, +#ifndef SQLITE_OMIT_DEPRECATED + sqlite3_transfer_bindings, +#else + 0, +#endif + sqlite3_update_hook, + sqlite3_user_data, + sqlite3_value_blob, + sqlite3_value_bytes, + sqlite3_value_bytes16, + sqlite3_value_double, + sqlite3_value_int, + sqlite3_value_int64, + sqlite3_value_numeric_type, + sqlite3_value_text, + sqlite3_value_text16, + sqlite3_value_text16be, + sqlite3_value_text16le, + sqlite3_value_type, + sqlite3_vmprintf, + /* + ** The original API set ends here. All extensions can call any + ** of the APIs above provided that the pointer is not NULL. But + ** before calling APIs that follow, extension should check the + ** sqlite3_libversion_number() to make sure they are dealing with + ** a library that is new enough to support that API. + ************************************************************************* + */ + sqlite3_overload_function, + + /* + ** Added after 3.3.13 + */ + sqlite3_prepare_v2, + sqlite3_prepare16_v2, + sqlite3_clear_bindings, + + /* + ** Added for 3.4.1 + */ + sqlite3_create_module_v2, + + /* + ** Added for 3.5.0 + */ + sqlite3_bind_zeroblob, + sqlite3_blob_bytes, + sqlite3_blob_close, + sqlite3_blob_open, + sqlite3_blob_read, + sqlite3_blob_write, + sqlite3_create_collation_v2, + sqlite3_file_control, + sqlite3_memory_highwater, + sqlite3_memory_used, +#ifdef SQLITE_MUTEX_OMIT + 0, + 0, + 0, + 0, + 0, +#else + sqlite3_mutex_alloc, + sqlite3_mutex_enter, + sqlite3_mutex_free, + sqlite3_mutex_leave, + sqlite3_mutex_try, +#endif + sqlite3_open_v2, + sqlite3_release_memory, + sqlite3_result_error_nomem, + sqlite3_result_error_toobig, + sqlite3_sleep, + sqlite3_soft_heap_limit, + sqlite3_vfs_find, + sqlite3_vfs_register, + sqlite3_vfs_unregister, + + /* + ** Added for 3.5.8 + */ + sqlite3_threadsafe, + sqlite3_result_zeroblob, + sqlite3_result_error_code, + sqlite3_test_control, + sqlite3_randomness, + sqlite3_context_db_handle, + + /* + ** Added for 3.6.0 + */ + sqlite3_extended_result_codes, + sqlite3_limit, + sqlite3_next_stmt, + sqlite3_sql, + sqlite3_status, + + /* + ** Added for 3.7.4 + */ + sqlite3_backup_finish, + sqlite3_backup_init, + sqlite3_backup_pagecount, + sqlite3_backup_remaining, + sqlite3_backup_step, +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS + sqlite3_compileoption_get, + sqlite3_compileoption_used, +#else + 0, + 0, +#endif + sqlite3_create_function_v2, + sqlite3_db_config, + sqlite3_db_mutex, + sqlite3_db_status, + sqlite3_extended_errcode, + sqlite3_log, + sqlite3_soft_heap_limit64, + sqlite3_sourceid, + sqlite3_stmt_status, + sqlite3_strnicmp, +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY + sqlite3_unlock_notify, +#else + 0, +#endif +#ifndef SQLITE_OMIT_WAL + sqlite3_wal_autocheckpoint, + sqlite3_wal_checkpoint, + sqlite3_wal_hook, +#else + 0, + 0, + 0, +#endif +}; + +/* +** Attempt to load an SQLite extension library contained in the file +** zFile. The entry point is zProc. zProc may be 0 in which case a +** default entry point name (sqlite3_extension_init) is used. Use +** of the default name is recommended. +** +** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong. +** +** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with +** error message text. The calling function should free this memory +** by calling sqlite3DbFree(db, ). +*/ +static int sqlite3LoadExtension( + sqlite3 *db, /* Load the extension into this database connection */ + const char *zFile, /* Name of the shared library containing extension */ + const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ + char **pzErrMsg /* Put error message here if not 0 */ +){ + sqlite3_vfs *pVfs = db->pVfs; + void *handle; + int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*); + char *zErrmsg = 0; + void **aHandle; + const int nMsg = 300; + + if( pzErrMsg ) *pzErrMsg = 0; + + /* Ticket #1863. To avoid a creating security problems for older + ** applications that relink against newer versions of SQLite, the + ** ability to run load_extension is turned off by default. One + ** must call sqlite3_enable_load_extension() to turn on extension + ** loading. Otherwise you get the following error. + */ + if( (db->flags & SQLITE_LoadExtension)==0 ){ + if( pzErrMsg ){ + *pzErrMsg = sqlite3_mprintf("not authorized"); + } + return SQLITE_ERROR; + } + + if( zProc==0 ){ + zProc = "sqlite3_extension_init"; + } + + handle = sqlite3OsDlOpen(pVfs, zFile); + if( handle==0 ){ + if( pzErrMsg ){ + *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg); + if( zErrmsg ){ + sqlite3_snprintf(nMsg, zErrmsg, + "unable to open shared library [%s]", zFile); + sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); + } + } + return SQLITE_ERROR; + } + xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) + sqlite3OsDlSym(pVfs, handle, zProc); + if( xInit==0 ){ + if( pzErrMsg ){ + *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg); + if( zErrmsg ){ + sqlite3_snprintf(nMsg, zErrmsg, + "no entry point [%s] in shared library [%s]", zProc,zFile); + sqlite3OsDlError(pVfs, nMsg-1, zErrmsg); + } + sqlite3OsDlClose(pVfs, handle); + } + return SQLITE_ERROR; + }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){ + if( pzErrMsg ){ + *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg); + } + sqlite3_free(zErrmsg); + sqlite3OsDlClose(pVfs, handle); + return SQLITE_ERROR; + } + + /* Append the new shared library handle to the db->aExtension array. */ + aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1)); + if( aHandle==0 ){ + return SQLITE_NOMEM; + } + if( db->nExtension>0 ){ + memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension); + } + sqlite3DbFree(db, db->aExtension); + db->aExtension = aHandle; + + db->aExtension[db->nExtension++] = handle; + return SQLITE_OK; +} +SQLITE_API int sqlite3_load_extension( + sqlite3 *db, /* Load the extension into this database connection */ + const char *zFile, /* Name of the shared library containing extension */ + const char *zProc, /* Entry point. Use "sqlite3_extension_init" if 0 */ + char **pzErrMsg /* Put error message here if not 0 */ +){ + int rc; + sqlite3_mutex_enter(db->mutex); + rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Call this routine when the database connection is closing in order +** to clean up loaded extensions +*/ +SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){ + int i; + assert( sqlite3_mutex_held(db->mutex) ); + for(i=0; inExtension; i++){ + sqlite3OsDlClose(db->pVfs, db->aExtension[i]); + } + sqlite3DbFree(db, db->aExtension); +} + +/* +** Enable or disable extension loading. Extension loading is disabled by +** default so as not to open security holes in older applications. +*/ +SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){ + sqlite3_mutex_enter(db->mutex); + if( onoff ){ + db->flags |= SQLITE_LoadExtension; + }else{ + db->flags &= ~SQLITE_LoadExtension; + } + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +#endif /* SQLITE_OMIT_LOAD_EXTENSION */ + +/* +** The auto-extension code added regardless of whether or not extension +** loading is supported. We need a dummy sqlite3Apis pointer for that +** code if regular extension loading is not available. This is that +** dummy pointer. +*/ +#ifdef SQLITE_OMIT_LOAD_EXTENSION +static const sqlite3_api_routines sqlite3Apis = { 0 }; +#endif + + +/* +** The following object holds the list of automatically loaded +** extensions. +** +** This list is shared across threads. The SQLITE_MUTEX_STATIC_MASTER +** mutex must be held while accessing this list. +*/ +typedef struct sqlite3AutoExtList sqlite3AutoExtList; +static SQLITE_WSD struct sqlite3AutoExtList { + int nExt; /* Number of entries in aExt[] */ + void (**aExt)(void); /* Pointers to the extension init functions */ +} sqlite3Autoext = { 0, 0 }; + +/* The "wsdAutoext" macro will resolve to the autoextension +** state vector. If writable static data is unsupported on the target, +** we have to locate the state vector at run-time. In the more common +** case where writable static data is supported, wsdStat can refer directly +** to the "sqlite3Autoext" state vector declared above. +*/ +#ifdef SQLITE_OMIT_WSD +# define wsdAutoextInit \ + sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext) +# define wsdAutoext x[0] +#else +# define wsdAutoextInit +# define wsdAutoext sqlite3Autoext +#endif + + +/* +** Register a statically linked extension that is automatically +** loaded by every new database connection. +*/ +SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){ + int rc = SQLITE_OK; +#ifndef SQLITE_OMIT_AUTOINIT + rc = sqlite3_initialize(); + if( rc ){ + return rc; + }else +#endif + { + int i; +#if SQLITE_THREADSAFE + sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); +#endif + wsdAutoextInit; + sqlite3_mutex_enter(mutex); + for(i=0; i=wsdAutoext.nExt ){ + xInit = 0; + go = 0; + }else{ + xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*)) + wsdAutoext.aExt[i]; + } + sqlite3_mutex_leave(mutex); + zErrmsg = 0; + if( xInit && xInit(db, &zErrmsg, &sqlite3Apis) ){ + sqlite3Error(db, SQLITE_ERROR, + "automatic extension loading failed: %s", zErrmsg); + go = 0; + } + sqlite3_free(zErrmsg); + } +} + +/************** End of loadext.c *********************************************/ +/************** Begin file pragma.c ******************************************/ +/* +** 2003 April 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to implement the PRAGMA command. +*/ + +/* Ignore this whole file if pragmas are disabled +*/ +#if !defined(SQLITE_OMIT_PRAGMA) + +/* +** Interpret the given string as a safety level. Return 0 for OFF, +** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or +** unrecognized string argument. +** +** Note that the values returned are one less that the values that +** should be passed into sqlite3BtreeSetSafetyLevel(). The is done +** to support legacy SQL code. The safety level used to be boolean +** and older scripts may have used numbers 0 for OFF and 1 for ON. +*/ +static u8 getSafetyLevel(const char *z){ + /* 123456789 123456789 */ + static const char zText[] = "onoffalseyestruefull"; + static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16}; + static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4}; + static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2}; + int i, n; + if( sqlite3Isdigit(*z) ){ + return (u8)sqlite3Atoi(z); + } + n = sqlite3Strlen30(z); + for(i=0; i=0&&i<=2)?i:0); +} +#endif /* ifndef SQLITE_OMIT_AUTOVACUUM */ + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +/* +** Interpret the given string as a temp db location. Return 1 for file +** backed temporary databases, 2 for the Red-Black tree in memory database +** and 0 to use the compile-time default. +*/ +static int getTempStore(const char *z){ + if( z[0]>='0' && z[0]<='2' ){ + return z[0] - '0'; + }else if( sqlite3StrICmp(z, "file")==0 ){ + return 1; + }else if( sqlite3StrICmp(z, "memory")==0 ){ + return 2; + }else{ + return 0; + } +} +#endif /* SQLITE_PAGER_PRAGMAS */ + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +/* +** Invalidate temp storage, either when the temp storage is changed +** from default, or when 'file' and the temp_store_directory has changed +*/ +static int invalidateTempStorage(Parse *pParse){ + sqlite3 *db = pParse->db; + if( db->aDb[1].pBt!=0 ){ + if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){ + sqlite3ErrorMsg(pParse, "temporary storage cannot be changed " + "from within a transaction"); + return SQLITE_ERROR; + } + sqlite3BtreeClose(db->aDb[1].pBt); + db->aDb[1].pBt = 0; + sqlite3ResetInternalSchema(db, 0); + } + return SQLITE_OK; +} +#endif /* SQLITE_PAGER_PRAGMAS */ + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS +/* +** If the TEMP database is open, close it and mark the database schema +** as needing reloading. This must be done when using the SQLITE_TEMP_STORE +** or DEFAULT_TEMP_STORE pragmas. +*/ +static int changeTempStorage(Parse *pParse, const char *zStorageType){ + int ts = getTempStore(zStorageType); + sqlite3 *db = pParse->db; + if( db->temp_store==ts ) return SQLITE_OK; + if( invalidateTempStorage( pParse ) != SQLITE_OK ){ + return SQLITE_ERROR; + } + db->temp_store = (u8)ts; + return SQLITE_OK; +} +#endif /* SQLITE_PAGER_PRAGMAS */ + +/* +** Generate code to return a single integer value. +*/ +static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){ + Vdbe *v = sqlite3GetVdbe(pParse); + int mem = ++pParse->nMem; + i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value)); + if( pI64 ){ + memcpy(pI64, &value, sizeof(value)); + } + sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1); +} + +#ifndef SQLITE_OMIT_FLAG_PRAGMAS +/* +** Check to see if zRight and zLeft refer to a pragma that queries +** or changes one of the flags in db->flags. Return 1 if so and 0 if not. +** Also, implement the pragma. +*/ +static int flagPragma(Parse *pParse, const char *zLeft, const char *zRight){ + static const struct sPragmaType { + const char *zName; /* Name of the pragma */ + int mask; /* Mask for the db->flags value */ + } aPragma[] = { + { "full_column_names", SQLITE_FullColNames }, + { "short_column_names", SQLITE_ShortColNames }, + { "count_changes", SQLITE_CountRows }, + { "empty_result_callbacks", SQLITE_NullCallback }, + { "legacy_file_format", SQLITE_LegacyFileFmt }, + { "fullfsync", SQLITE_FullFSync }, + { "checkpoint_fullfsync", SQLITE_CkptFullFSync }, + { "reverse_unordered_selects", SQLITE_ReverseOrder }, +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX + { "automatic_index", SQLITE_AutoIndex }, +#endif +#ifdef SQLITE_DEBUG + { "sql_trace", SQLITE_SqlTrace }, + { "vdbe_listing", SQLITE_VdbeListing }, + { "vdbe_trace", SQLITE_VdbeTrace }, +#endif +#ifndef SQLITE_OMIT_CHECK + { "ignore_check_constraints", SQLITE_IgnoreChecks }, +#endif + /* The following is VERY experimental */ + { "writable_schema", SQLITE_WriteSchema|SQLITE_RecoveryMode }, + { "omit_readlock", SQLITE_NoReadlock }, + + /* TODO: Maybe it shouldn't be possible to change the ReadUncommitted + ** flag if there are any active statements. */ + { "read_uncommitted", SQLITE_ReadUncommitted }, + { "recursive_triggers", SQLITE_RecTriggers }, + + /* This flag may only be set if both foreign-key and trigger support + ** are present in the build. */ +#if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER) + { "foreign_keys", SQLITE_ForeignKeys }, +#endif + }; + int i; + const struct sPragmaType *p; + for(i=0, p=aPragma; izName)==0 ){ + sqlite3 *db = pParse->db; + Vdbe *v; + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); /* Already allocated by sqlite3Pragma() */ + if( ALWAYS(v) ){ + if( zRight==0 ){ + returnSingleInt(pParse, p->zName, (db->flags & p->mask)!=0 ); + }else{ + int mask = p->mask; /* Mask of bits to set or clear. */ + if( db->autoCommit==0 ){ + /* Foreign key support may not be enabled or disabled while not + ** in auto-commit mode. */ + mask &= ~(SQLITE_ForeignKeys); + } + + if( getBoolean(zRight) ){ + db->flags |= mask; + }else{ + db->flags &= ~mask; + } + + /* Many of the flag-pragmas modify the code generated by the SQL + ** compiler (eg. count_changes). So add an opcode to expire all + ** compiled SQL statements after modifying a pragma value. + */ + sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); + } + } + + return 1; + } + } + return 0; +} +#endif /* SQLITE_OMIT_FLAG_PRAGMAS */ + +/* +** Return a human-readable name for a constraint resolution action. +*/ +#ifndef SQLITE_OMIT_FOREIGN_KEY +static const char *actionName(u8 action){ + const char *zName; + switch( action ){ + case OE_SetNull: zName = "SET NULL"; break; + case OE_SetDflt: zName = "SET DEFAULT"; break; + case OE_Cascade: zName = "CASCADE"; break; + case OE_Restrict: zName = "RESTRICT"; break; + default: zName = "NO ACTION"; + assert( action==OE_None ); break; + } + return zName; +} +#endif + + +/* +** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants +** defined in pager.h. This function returns the associated lowercase +** journal-mode name. +*/ +SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){ + static char * const azModeName[] = { + "delete", "persist", "off", "truncate", "memory" +#ifndef SQLITE_OMIT_WAL + , "wal" +#endif + }; + assert( PAGER_JOURNALMODE_DELETE==0 ); + assert( PAGER_JOURNALMODE_PERSIST==1 ); + assert( PAGER_JOURNALMODE_OFF==2 ); + assert( PAGER_JOURNALMODE_TRUNCATE==3 ); + assert( PAGER_JOURNALMODE_MEMORY==4 ); + assert( PAGER_JOURNALMODE_WAL==5 ); + assert( eMode>=0 && eMode<=ArraySize(azModeName) ); + + if( eMode==ArraySize(azModeName) ) return 0; + return azModeName[eMode]; +} + +/* +** Process a pragma statement. +** +** Pragmas are of this form: +** +** PRAGMA [database.]id [= value] +** +** The identifier might also be a string. The value is a string, and +** identifier, or a number. If minusFlag is true, then the value is +** a number that was preceded by a minus sign. +** +** If the left side is "database.id" then pId1 is the database name +** and pId2 is the id. If the left side is just "id" then pId1 is the +** id and pId2 is any empty string. +*/ +SQLITE_PRIVATE void sqlite3Pragma( + Parse *pParse, + Token *pId1, /* First part of [database.]id field */ + Token *pId2, /* Second part of [database.]id field, or NULL */ + Token *pValue, /* Token for , or NULL */ + int minusFlag /* True if a '-' sign preceded */ +){ + char *zLeft = 0; /* Nul-terminated UTF-8 string */ + char *zRight = 0; /* Nul-terminated UTF-8 string , or NULL */ + const char *zDb = 0; /* The database name */ + Token *pId; /* Pointer to token */ + int iDb; /* Database index for */ + sqlite3 *db = pParse->db; + Db *pDb; + Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db); + if( v==0 ) return; + sqlite3VdbeRunOnlyOnce(v); + pParse->nMem = 2; + + /* Interpret the [database.] part of the pragma statement. iDb is the + ** index of the database this pragma is being applied to in db.aDb[]. */ + iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId); + if( iDb<0 ) return; + pDb = &db->aDb[iDb]; + + /* If the temp database has been explicitly named as part of the + ** pragma, make sure it is open. + */ + if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){ + return; + } + + zLeft = sqlite3NameFromToken(db, pId); + if( !zLeft ) return; + if( minusFlag ){ + zRight = sqlite3MPrintf(db, "-%T", pValue); + }else{ + zRight = sqlite3NameFromToken(db, pValue); + } + + assert( pId2 ); + zDb = pId2->n>0 ? pDb->zName : 0; + if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){ + goto pragma_out; + } + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS + /* + ** PRAGMA [database.]default_cache_size + ** PRAGMA [database.]default_cache_size=N + ** + ** The first form reports the current persistent setting for the + ** page cache size. The value returned is the maximum number of + ** pages in the page cache. The second form sets both the current + ** page cache size value and the persistent page cache size value + ** stored in the database file. + ** + ** Older versions of SQLite would set the default cache size to a + ** negative number to indicate synchronous=OFF. These days, synchronous + ** is always on by default regardless of the sign of the default cache + ** size. But continue to take the absolute value of the default cache + ** size of historical compatibility. + */ + if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){ + static const VdbeOpList getCacheSize[] = { + { OP_Transaction, 0, 0, 0}, /* 0 */ + { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */ + { OP_IfPos, 1, 7, 0}, + { OP_Integer, 0, 2, 0}, + { OP_Subtract, 1, 2, 1}, + { OP_IfPos, 1, 7, 0}, + { OP_Integer, 0, 1, 0}, /* 6 */ + { OP_ResultRow, 1, 1, 0}, + }; + int addr; + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + sqlite3VdbeUsesBtree(v, iDb); + if( !zRight ){ + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC); + pParse->nMem += 2; + addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize); + sqlite3VdbeChangeP1(v, addr, iDb); + sqlite3VdbeChangeP1(v, addr+1, iDb); + sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE); + }else{ + int size = sqlite3AbsInt32(sqlite3Atoi(zRight)); + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3VdbeAddOp2(v, OP_Integer, size, 1); + sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1); + pDb->pSchema->cache_size = size; + sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); + } + }else + + /* + ** PRAGMA [database.]page_size + ** PRAGMA [database.]page_size=N + ** + ** The first form reports the current setting for the + ** database page size in bytes. The second form sets the + ** database page size value. The value can only be set if + ** the database has not yet been created. + */ + if( sqlite3StrICmp(zLeft,"page_size")==0 ){ + Btree *pBt = pDb->pBt; + assert( pBt!=0 ); + if( !zRight ){ + int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0; + returnSingleInt(pParse, "page_size", size); + }else{ + /* Malloc may fail when setting the page-size, as there is an internal + ** buffer that the pager module resizes using sqlite3_realloc(). + */ + db->nextPagesize = sqlite3Atoi(zRight); + if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){ + db->mallocFailed = 1; + } + } + }else + + /* + ** PRAGMA [database.]secure_delete + ** PRAGMA [database.]secure_delete=ON/OFF + ** + ** The first form reports the current setting for the + ** secure_delete flag. The second form changes the secure_delete + ** flag setting and reports thenew value. + */ + if( sqlite3StrICmp(zLeft,"secure_delete")==0 ){ + Btree *pBt = pDb->pBt; + int b = -1; + assert( pBt!=0 ); + if( zRight ){ + b = getBoolean(zRight); + } + if( pId2->n==0 && b>=0 ){ + int ii; + for(ii=0; iinDb; ii++){ + sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b); + } + } + b = sqlite3BtreeSecureDelete(pBt, b); + returnSingleInt(pParse, "secure_delete", b); + }else + + /* + ** PRAGMA [database.]max_page_count + ** PRAGMA [database.]max_page_count=N + ** + ** The first form reports the current setting for the + ** maximum number of pages in the database file. The + ** second form attempts to change this setting. Both + ** forms return the current setting. + ** + ** PRAGMA [database.]page_count + ** + ** Return the number of pages in the specified database. + */ + if( sqlite3StrICmp(zLeft,"page_count")==0 + || sqlite3StrICmp(zLeft,"max_page_count")==0 + ){ + int iReg; + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + sqlite3CodeVerifySchema(pParse, iDb); + iReg = ++pParse->nMem; + if( zLeft[0]=='p' ){ + sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg); + }else{ + sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight)); + } + sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); + }else + + /* + ** PRAGMA [database.]locking_mode + ** PRAGMA [database.]locking_mode = (normal|exclusive) + */ + if( sqlite3StrICmp(zLeft,"locking_mode")==0 ){ + const char *zRet = "normal"; + int eMode = getLockingMode(zRight); + + if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){ + /* Simple "PRAGMA locking_mode;" statement. This is a query for + ** the current default locking mode (which may be different to + ** the locking-mode of the main database). + */ + eMode = db->dfltLockMode; + }else{ + Pager *pPager; + if( pId2->n==0 ){ + /* This indicates that no database name was specified as part + ** of the PRAGMA command. In this case the locking-mode must be + ** set on all attached databases, as well as the main db file. + ** + ** Also, the sqlite3.dfltLockMode variable is set so that + ** any subsequently attached databases also use the specified + ** locking mode. + */ + int ii; + assert(pDb==&db->aDb[0]); + for(ii=2; iinDb; ii++){ + pPager = sqlite3BtreePager(db->aDb[ii].pBt); + sqlite3PagerLockingMode(pPager, eMode); + } + db->dfltLockMode = (u8)eMode; + } + pPager = sqlite3BtreePager(pDb->pBt); + eMode = sqlite3PagerLockingMode(pPager, eMode); + } + + assert(eMode==PAGER_LOCKINGMODE_NORMAL||eMode==PAGER_LOCKINGMODE_EXCLUSIVE); + if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){ + zRet = "exclusive"; + } + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + }else + + /* + ** PRAGMA [database.]journal_mode + ** PRAGMA [database.]journal_mode = + ** (delete|persist|off|truncate|memory|wal|off) + */ + if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){ + int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */ + int ii; /* Loop counter */ + + /* Force the schema to be loaded on all databases. This cases all + ** database files to be opened and the journal_modes set. */ + if( sqlite3ReadSchema(pParse) ){ + goto pragma_out; + } + + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC); + + if( zRight==0 ){ + /* If there is no "=MODE" part of the pragma, do a query for the + ** current mode */ + eMode = PAGER_JOURNALMODE_QUERY; + }else{ + const char *zMode; + int n = sqlite3Strlen30(zRight); + for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){ + if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break; + } + if( !zMode ){ + /* If the "=MODE" part does not match any known journal mode, + ** then do a query */ + eMode = PAGER_JOURNALMODE_QUERY; + } + } + if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){ + /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */ + iDb = 0; + pId2->n = 1; + } + for(ii=db->nDb-1; ii>=0; ii--){ + if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){ + sqlite3VdbeUsesBtree(v, ii); + sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode); + } + } + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + }else + + /* + ** PRAGMA [database.]journal_size_limit + ** PRAGMA [database.]journal_size_limit=N + ** + ** Get or set the size limit on rollback journal files. + */ + if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){ + Pager *pPager = sqlite3BtreePager(pDb->pBt); + i64 iLimit = -2; + if( zRight ){ + sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8); + if( iLimit<-1 ) iLimit = -1; + } + iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit); + returnSingleInt(pParse, "journal_size_limit", iLimit); + }else + +#endif /* SQLITE_OMIT_PAGER_PRAGMAS */ + + /* + ** PRAGMA [database.]auto_vacuum + ** PRAGMA [database.]auto_vacuum=N + ** + ** Get or set the value of the database 'auto-vacuum' parameter. + ** The value is one of: 0 NONE 1 FULL 2 INCREMENTAL + */ +#ifndef SQLITE_OMIT_AUTOVACUUM + if( sqlite3StrICmp(zLeft,"auto_vacuum")==0 ){ + Btree *pBt = pDb->pBt; + assert( pBt!=0 ); + if( sqlite3ReadSchema(pParse) ){ + goto pragma_out; + } + if( !zRight ){ + int auto_vacuum; + if( ALWAYS(pBt) ){ + auto_vacuum = sqlite3BtreeGetAutoVacuum(pBt); + }else{ + auto_vacuum = SQLITE_DEFAULT_AUTOVACUUM; + } + returnSingleInt(pParse, "auto_vacuum", auto_vacuum); + }else{ + int eAuto = getAutoVacuum(zRight); + assert( eAuto>=0 && eAuto<=2 ); + db->nextAutovac = (u8)eAuto; + if( ALWAYS(eAuto>=0) ){ + /* Call SetAutoVacuum() to set initialize the internal auto and + ** incr-vacuum flags. This is required in case this connection + ** creates the database file. It is important that it is created + ** as an auto-vacuum capable db. + */ + int rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto); + if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){ + /* When setting the auto_vacuum mode to either "full" or + ** "incremental", write the value of meta[6] in the database + ** file. Before writing to meta[6], check that meta[3] indicates + ** that this really is an auto-vacuum capable database. + */ + static const VdbeOpList setMeta6[] = { + { OP_Transaction, 0, 1, 0}, /* 0 */ + { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE}, + { OP_If, 1, 0, 0}, /* 2 */ + { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */ + { OP_Integer, 0, 1, 0}, /* 4 */ + { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */ + }; + int iAddr; + iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6); + sqlite3VdbeChangeP1(v, iAddr, iDb); + sqlite3VdbeChangeP1(v, iAddr+1, iDb); + sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4); + sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1); + sqlite3VdbeChangeP1(v, iAddr+5, iDb); + sqlite3VdbeUsesBtree(v, iDb); + } + } + } + }else +#endif + + /* + ** PRAGMA [database.]incremental_vacuum(N) + ** + ** Do N steps of incremental vacuuming on a database. + */ +#ifndef SQLITE_OMIT_AUTOVACUUM + if( sqlite3StrICmp(zLeft,"incremental_vacuum")==0 ){ + int iLimit, addr; + if( sqlite3ReadSchema(pParse) ){ + goto pragma_out; + } + if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){ + iLimit = 0x7fffffff; + } + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1); + addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb); + sqlite3VdbeAddOp1(v, OP_ResultRow, 1); + sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); + sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr); + sqlite3VdbeJumpHere(v, addr); + }else +#endif + +#ifndef SQLITE_OMIT_PAGER_PRAGMAS + /* + ** PRAGMA [database.]cache_size + ** PRAGMA [database.]cache_size=N + ** + ** The first form reports the current local setting for the + ** page cache size. The local setting can be different from + ** the persistent cache size value that is stored in the database + ** file itself. The value returned is the maximum number of + ** pages in the page cache. The second form sets the local + ** page cache size value. It does not change the persistent + ** cache size stored on the disk so the cache size will revert + ** to its default value when the database is closed and reopened. + ** N should be a positive integer. + */ + if( sqlite3StrICmp(zLeft,"cache_size")==0 ){ + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + if( !zRight ){ + returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size); + }else{ + int size = sqlite3AbsInt32(sqlite3Atoi(zRight)); + pDb->pSchema->cache_size = size; + sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); + } + }else + + /* + ** PRAGMA temp_store + ** PRAGMA temp_store = "default"|"memory"|"file" + ** + ** Return or set the local value of the temp_store flag. Changing + ** the local value does not make changes to the disk file and the default + ** value will be restored the next time the database is opened. + ** + ** Note that it is possible for the library compile-time options to + ** override this setting + */ + if( sqlite3StrICmp(zLeft, "temp_store")==0 ){ + if( !zRight ){ + returnSingleInt(pParse, "temp_store", db->temp_store); + }else{ + changeTempStorage(pParse, zRight); + } + }else + + /* + ** PRAGMA temp_store_directory + ** PRAGMA temp_store_directory = ""|"directory_name" + ** + ** Return or set the local value of the temp_store_directory flag. Changing + ** the value sets a specific directory to be used for temporary files. + ** Setting to a null string reverts to the default temporary directory search. + ** If temporary directory is changed, then invalidateTempStorage. + ** + */ + if( sqlite3StrICmp(zLeft, "temp_store_directory")==0 ){ + if( !zRight ){ + if( sqlite3_temp_directory ){ + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, + "temp_store_directory", SQLITE_STATIC); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } + }else{ +#ifndef SQLITE_OMIT_WSD + if( zRight[0] ){ + int rc; + int res; + rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res); + if( rc!=SQLITE_OK || res==0 ){ + sqlite3ErrorMsg(pParse, "not a writable directory"); + goto pragma_out; + } + } + if( SQLITE_TEMP_STORE==0 + || (SQLITE_TEMP_STORE==1 && db->temp_store<=1) + || (SQLITE_TEMP_STORE==2 && db->temp_store==1) + ){ + invalidateTempStorage(pParse); + } + sqlite3_free(sqlite3_temp_directory); + if( zRight[0] ){ + sqlite3_temp_directory = sqlite3_mprintf("%s", zRight); + }else{ + sqlite3_temp_directory = 0; + } +#endif /* SQLITE_OMIT_WSD */ + } + }else + +#if !defined(SQLITE_ENABLE_LOCKING_STYLE) +# if defined(__APPLE__) +# define SQLITE_ENABLE_LOCKING_STYLE 1 +# else +# define SQLITE_ENABLE_LOCKING_STYLE 0 +# endif +#endif +#if SQLITE_ENABLE_LOCKING_STYLE + /* + ** PRAGMA [database.]lock_proxy_file + ** PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path" + ** + ** Return or set the value of the lock_proxy_file flag. Changing + ** the value sets a specific file to be used for database access locks. + ** + */ + if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){ + if( !zRight ){ + Pager *pPager = sqlite3BtreePager(pDb->pBt); + char *proxy_file_path = NULL; + sqlite3_file *pFile = sqlite3PagerFile(pPager); + sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE, + &proxy_file_path); + + if( proxy_file_path ){ + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, + "lock_proxy_file", SQLITE_STATIC); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } + }else{ + Pager *pPager = sqlite3BtreePager(pDb->pBt); + sqlite3_file *pFile = sqlite3PagerFile(pPager); + int res; + if( zRight[0] ){ + res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, + zRight); + } else { + res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE, + NULL); + } + if( res!=SQLITE_OK ){ + sqlite3ErrorMsg(pParse, "failed to set lock proxy file"); + goto pragma_out; + } + } + }else +#endif /* SQLITE_ENABLE_LOCKING_STYLE */ + + /* + ** PRAGMA [database.]synchronous + ** PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL + ** + ** Return or set the local value of the synchronous flag. Changing + ** the local value does not make changes to the disk file and the + ** default value will be restored the next time the database is + ** opened. + */ + if( sqlite3StrICmp(zLeft,"synchronous")==0 ){ + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + if( !zRight ){ + returnSingleInt(pParse, "synchronous", pDb->safety_level-1); + }else{ + if( !db->autoCommit ){ + sqlite3ErrorMsg(pParse, + "Safety level may not be changed inside a transaction"); + }else{ + pDb->safety_level = getSafetyLevel(zRight)+1; + } + } + }else +#endif /* SQLITE_OMIT_PAGER_PRAGMAS */ + +#ifndef SQLITE_OMIT_FLAG_PRAGMAS + if( flagPragma(pParse, zLeft, zRight) ){ + /* The flagPragma() subroutine also generates any necessary code + ** there is nothing more to do here */ + }else +#endif /* SQLITE_OMIT_FLAG_PRAGMAS */ + +#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS + /* + ** PRAGMA table_info(
    ) + ** + ** Return a single row for each column of the named table. The columns of + ** the returned data set are: + ** + ** cid: Column id (numbered from left to right, starting at 0) + ** name: Column name + ** type: Column declaration type. + ** notnull: True if 'NOT NULL' is part of column declaration + ** dflt_value: The default value for the column, if any. + */ + if( sqlite3StrICmp(zLeft, "table_info")==0 && zRight ){ + Table *pTab; + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + pTab = sqlite3FindTable(db, zRight, zDb); + if( pTab ){ + int i; + int nHidden = 0; + Column *pCol; + sqlite3VdbeSetNumCols(v, 6); + pParse->nMem = 6; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC); + sqlite3ViewGetColumnNames(pParse, pTab); + for(i=0, pCol=pTab->aCol; inCol; i++, pCol++){ + if( IsHiddenColumn(pCol) ){ + nHidden++; + continue; + } + sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1); + sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, + pCol->zType ? pCol->zType : "", 0); + sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4); + if( pCol->zDflt ){ + sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0); + }else{ + sqlite3VdbeAddOp2(v, OP_Null, 0, 5); + } + sqlite3VdbeAddOp2(v, OP_Integer, pCol->isPrimKey, 6); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6); + } + } + }else + + if( sqlite3StrICmp(zLeft, "index_info")==0 && zRight ){ + Index *pIdx; + Table *pTab; + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + pIdx = sqlite3FindIndex(db, zRight, zDb); + if( pIdx ){ + int i; + pTab = pIdx->pTable; + sqlite3VdbeSetNumCols(v, 3); + pParse->nMem = 3; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC); + for(i=0; inColumn; i++){ + int cnum = pIdx->aiColumn[i]; + sqlite3VdbeAddOp2(v, OP_Integer, i, 1); + sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2); + assert( pTab->nCol>cnum ); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); + } + } + }else + + if( sqlite3StrICmp(zLeft, "index_list")==0 && zRight ){ + Index *pIdx; + Table *pTab; + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + pTab = sqlite3FindTable(db, zRight, zDb); + if( pTab ){ + v = sqlite3GetVdbe(pParse); + pIdx = pTab->pIndex; + if( pIdx ){ + int i = 0; + sqlite3VdbeSetNumCols(v, 3); + pParse->nMem = 3; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC); + while(pIdx){ + sqlite3VdbeAddOp2(v, OP_Integer, i, 1); + sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0); + sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); + ++i; + pIdx = pIdx->pNext; + } + } + } + }else + + if( sqlite3StrICmp(zLeft, "database_list")==0 ){ + int i; + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + sqlite3VdbeSetNumCols(v, 3); + pParse->nMem = 3; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC); + for(i=0; inDb; i++){ + if( db->aDb[i].pBt==0 ) continue; + assert( db->aDb[i].zName!=0 ); + sqlite3VdbeAddOp2(v, OP_Integer, i, 1); + sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, + sqlite3BtreeGetFilename(db->aDb[i].pBt), 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); + } + }else + + if( sqlite3StrICmp(zLeft, "collation_list")==0 ){ + int i = 0; + HashElem *p; + sqlite3VdbeSetNumCols(v, 2); + pParse->nMem = 2; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC); + for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){ + CollSeq *pColl = (CollSeq *)sqliteHashData(p); + sqlite3VdbeAddOp2(v, OP_Integer, i++, 1); + sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); + } + }else +#endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */ + +#ifndef SQLITE_OMIT_FOREIGN_KEY + if( sqlite3StrICmp(zLeft, "foreign_key_list")==0 && zRight ){ + FKey *pFK; + Table *pTab; + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + pTab = sqlite3FindTable(db, zRight, zDb); + if( pTab ){ + v = sqlite3GetVdbe(pParse); + pFK = pTab->pFKey; + if( pFK ){ + int i = 0; + sqlite3VdbeSetNumCols(v, 8); + pParse->nMem = 8; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC); + while(pFK){ + int j; + for(j=0; jnCol; j++){ + char *zCol = pFK->aCol[j].zCol; + char *zOnDelete = (char *)actionName(pFK->aAction[0]); + char *zOnUpdate = (char *)actionName(pFK->aAction[1]); + sqlite3VdbeAddOp2(v, OP_Integer, i, 1); + sqlite3VdbeAddOp2(v, OP_Integer, j, 2); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, + pTab->aCol[pFK->aCol[j].iFrom].zName, 0); + sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0); + sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8); + } + ++i; + pFK = pFK->pNextFrom; + } + } + } + }else +#endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */ + +#ifndef NDEBUG + if( sqlite3StrICmp(zLeft, "parser_trace")==0 ){ + if( zRight ){ + if( getBoolean(zRight) ){ + sqlite3ParserTrace(stderr, "parser: "); + }else{ + sqlite3ParserTrace(0, 0); + } + } + }else +#endif + + /* Reinstall the LIKE and GLOB functions. The variant of LIKE + ** used will be case sensitive or not depending on the RHS. + */ + if( sqlite3StrICmp(zLeft, "case_sensitive_like")==0 ){ + if( zRight ){ + sqlite3RegisterLikeFunctions(db, getBoolean(zRight)); + } + }else + +#ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX +# define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100 +#endif + +#ifndef SQLITE_OMIT_INTEGRITY_CHECK + /* Pragma "quick_check" is an experimental reduced version of + ** integrity_check designed to detect most database corruption + ** without most of the overhead of a full integrity-check. + */ + if( sqlite3StrICmp(zLeft, "integrity_check")==0 + || sqlite3StrICmp(zLeft, "quick_check")==0 + ){ + int i, j, addr, mxErr; + + /* Code that appears at the end of the integrity check. If no error + ** messages have been generated, output OK. Otherwise output the + ** error message + */ + static const VdbeOpList endCode[] = { + { OP_AddImm, 1, 0, 0}, /* 0 */ + { OP_IfNeg, 1, 0, 0}, /* 1 */ + { OP_String8, 0, 3, 0}, /* 2 */ + { OP_ResultRow, 3, 1, 0}, + }; + + int isQuick = (zLeft[0]=='q'); + + /* Initialize the VDBE program */ + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + pParse->nMem = 6; + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC); + + /* Set the maximum error count */ + mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; + if( zRight ){ + sqlite3GetInt32(zRight, &mxErr); + if( mxErr<=0 ){ + mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX; + } + } + sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1); /* reg[1] holds errors left */ + + /* Do an integrity check on each database file */ + for(i=0; inDb; i++){ + HashElem *x; + Hash *pTbls; + int cnt = 0; + + if( OMIT_TEMPDB && i==1 ) continue; + + sqlite3CodeVerifySchema(pParse, i); + addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */ + sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); + sqlite3VdbeJumpHere(v, addr); + + /* Do an integrity check of the B-Tree + ** + ** Begin by filling registers 2, 3, ... with the root pages numbers + ** for all tables and indices in the database. + */ + pTbls = &db->aDb[i].pSchema->tblHash; + for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){ + Table *pTab = sqliteHashData(x); + Index *pIdx; + sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt); + cnt++; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt); + cnt++; + } + } + + /* Make sure sufficient number of registers have been allocated */ + if( pParse->nMem < cnt+4 ){ + pParse->nMem = cnt+4; + } + + /* Do the b-tree integrity checks */ + sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1); + sqlite3VdbeChangeP5(v, (u8)i); + addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2); + sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, + sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName), + P4_DYNAMIC); + sqlite3VdbeAddOp3(v, OP_Move, 2, 4, 1); + sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2); + sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1); + sqlite3VdbeJumpHere(v, addr); + + /* Make sure all the indices are constructed correctly. + */ + for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){ + Table *pTab = sqliteHashData(x); + Index *pIdx; + int loopTop; + + if( pTab->pIndex==0 ) continue; + addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Stop if out of errors */ + sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); + sqlite3VdbeJumpHere(v, addr); + sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead); + sqlite3VdbeAddOp2(v, OP_Integer, 0, 2); /* reg(2) will count entries */ + loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0); + sqlite3VdbeAddOp2(v, OP_AddImm, 2, 1); /* increment entry count */ + for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + int jmp2; + int r1; + static const VdbeOpList idxErr[] = { + { OP_AddImm, 1, -1, 0}, + { OP_String8, 0, 3, 0}, /* 1 */ + { OP_Rowid, 1, 4, 0}, + { OP_String8, 0, 5, 0}, /* 3 */ + { OP_String8, 0, 6, 0}, /* 4 */ + { OP_Concat, 4, 3, 3}, + { OP_Concat, 5, 3, 3}, + { OP_Concat, 6, 3, 3}, + { OP_ResultRow, 3, 1, 0}, + { OP_IfPos, 1, 0, 0}, /* 9 */ + { OP_Halt, 0, 0, 0}, + }; + r1 = sqlite3GenerateIndexKey(pParse, pIdx, 1, 3, 0); + jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, j+2, 0, r1, pIdx->nColumn+1); + addr = sqlite3VdbeAddOpList(v, ArraySize(idxErr), idxErr); + sqlite3VdbeChangeP4(v, addr+1, "rowid ", P4_STATIC); + sqlite3VdbeChangeP4(v, addr+3, " missing from index ", P4_STATIC); + sqlite3VdbeChangeP4(v, addr+4, pIdx->zName, P4_STATIC); + sqlite3VdbeJumpHere(v, addr+9); + sqlite3VdbeJumpHere(v, jmp2); + } + sqlite3VdbeAddOp2(v, OP_Next, 1, loopTop+1); + sqlite3VdbeJumpHere(v, loopTop); + for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + static const VdbeOpList cntIdx[] = { + { OP_Integer, 0, 3, 0}, + { OP_Rewind, 0, 0, 0}, /* 1 */ + { OP_AddImm, 3, 1, 0}, + { OP_Next, 0, 0, 0}, /* 3 */ + { OP_Eq, 2, 0, 3}, /* 4 */ + { OP_AddImm, 1, -1, 0}, + { OP_String8, 0, 2, 0}, /* 6 */ + { OP_String8, 0, 3, 0}, /* 7 */ + { OP_Concat, 3, 2, 2}, + { OP_ResultRow, 2, 1, 0}, + }; + addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); + sqlite3VdbeAddOp2(v, OP_Halt, 0, 0); + sqlite3VdbeJumpHere(v, addr); + addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx); + sqlite3VdbeChangeP1(v, addr+1, j+2); + sqlite3VdbeChangeP2(v, addr+1, addr+4); + sqlite3VdbeChangeP1(v, addr+3, j+2); + sqlite3VdbeChangeP2(v, addr+3, addr+2); + sqlite3VdbeJumpHere(v, addr+4); + sqlite3VdbeChangeP4(v, addr+6, + "wrong # of entries in index ", P4_STATIC); + sqlite3VdbeChangeP4(v, addr+7, pIdx->zName, P4_STATIC); + } + } + } + addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode); + sqlite3VdbeChangeP2(v, addr, -mxErr); + sqlite3VdbeJumpHere(v, addr+1); + sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC); + }else +#endif /* SQLITE_OMIT_INTEGRITY_CHECK */ + +#ifndef SQLITE_OMIT_UTF16 + /* + ** PRAGMA encoding + ** PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be" + ** + ** In its first form, this pragma returns the encoding of the main + ** database. If the database is not initialized, it is initialized now. + ** + ** The second form of this pragma is a no-op if the main database file + ** has not already been initialized. In this case it sets the default + ** encoding that will be used for the main database file if a new file + ** is created. If an existing main database file is opened, then the + ** default text encoding for the existing database is used. + ** + ** In all cases new databases created using the ATTACH command are + ** created to use the same default text encoding as the main database. If + ** the main database has not been initialized and/or created when ATTACH + ** is executed, this is done before the ATTACH operation. + ** + ** In the second form this pragma sets the text encoding to be used in + ** new database files created using this database handle. It is only + ** useful if invoked immediately after the main database i + */ + if( sqlite3StrICmp(zLeft, "encoding")==0 ){ + static const struct EncName { + char *zName; + u8 enc; + } encnames[] = { + { "UTF8", SQLITE_UTF8 }, + { "UTF-8", SQLITE_UTF8 }, /* Must be element [1] */ + { "UTF-16le", SQLITE_UTF16LE }, /* Must be element [2] */ + { "UTF-16be", SQLITE_UTF16BE }, /* Must be element [3] */ + { "UTF16le", SQLITE_UTF16LE }, + { "UTF16be", SQLITE_UTF16BE }, + { "UTF-16", 0 }, /* SQLITE_UTF16NATIVE */ + { "UTF16", 0 }, /* SQLITE_UTF16NATIVE */ + { 0, 0 } + }; + const struct EncName *pEnc; + if( !zRight ){ /* "PRAGMA encoding" */ + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC); + sqlite3VdbeAddOp2(v, OP_String8, 0, 1); + assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 ); + assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE ); + assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE ); + sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + }else{ /* "PRAGMA encoding = XXX" */ + /* Only change the value of sqlite.enc if the database handle is not + ** initialized. If the main database exists, the new sqlite.enc value + ** will be overwritten when the schema is next loaded. If it does not + ** already exists, it will be created to use the new encoding value. + */ + if( + !(DbHasProperty(db, 0, DB_SchemaLoaded)) || + DbHasProperty(db, 0, DB_Empty) + ){ + for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ + if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){ + ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE; + break; + } + } + if( !pEnc->zName ){ + sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight); + } + } + } + }else +#endif /* SQLITE_OMIT_UTF16 */ + +#ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS + /* + ** PRAGMA [database.]schema_version + ** PRAGMA [database.]schema_version = + ** + ** PRAGMA [database.]user_version + ** PRAGMA [database.]user_version = + ** + ** The pragma's schema_version and user_version are used to set or get + ** the value of the schema-version and user-version, respectively. Both + ** the schema-version and the user-version are 32-bit signed integers + ** stored in the database header. + ** + ** The schema-cookie is usually only manipulated internally by SQLite. It + ** is incremented by SQLite whenever the database schema is modified (by + ** creating or dropping a table or index). The schema version is used by + ** SQLite each time a query is executed to ensure that the internal cache + ** of the schema used when compiling the SQL query matches the schema of + ** the database against which the compiled query is actually executed. + ** Subverting this mechanism by using "PRAGMA schema_version" to modify + ** the schema-version is potentially dangerous and may lead to program + ** crashes or database corruption. Use with caution! + ** + ** The user-version is not used internally by SQLite. It may be used by + ** applications for any purpose. + */ + if( sqlite3StrICmp(zLeft, "schema_version")==0 + || sqlite3StrICmp(zLeft, "user_version")==0 + || sqlite3StrICmp(zLeft, "freelist_count")==0 + ){ + int iCookie; /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */ + sqlite3VdbeUsesBtree(v, iDb); + switch( zLeft[0] ){ + case 'f': case 'F': + iCookie = BTREE_FREE_PAGE_COUNT; + break; + case 's': case 'S': + iCookie = BTREE_SCHEMA_VERSION; + break; + default: + iCookie = BTREE_USER_VERSION; + break; + } + + if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){ + /* Write the specified cookie value */ + static const VdbeOpList setCookie[] = { + { OP_Transaction, 0, 1, 0}, /* 0 */ + { OP_Integer, 0, 1, 0}, /* 1 */ + { OP_SetCookie, 0, 0, 1}, /* 2 */ + }; + int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie); + sqlite3VdbeChangeP1(v, addr, iDb); + sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight)); + sqlite3VdbeChangeP1(v, addr+2, iDb); + sqlite3VdbeChangeP2(v, addr+2, iCookie); + }else{ + /* Read the specified cookie value */ + static const VdbeOpList readCookie[] = { + { OP_Transaction, 0, 0, 0}, /* 0 */ + { OP_ReadCookie, 0, 1, 0}, /* 1 */ + { OP_ResultRow, 1, 1, 0} + }; + int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie); + sqlite3VdbeChangeP1(v, addr, iDb); + sqlite3VdbeChangeP1(v, addr+1, iDb); + sqlite3VdbeChangeP3(v, addr+1, iCookie); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT); + } + }else +#endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */ + +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS + /* + ** PRAGMA compile_options + ** + ** Return the names of all compile-time options used in this build, + ** one option per row. + */ + if( sqlite3StrICmp(zLeft, "compile_options")==0 ){ + int i = 0; + const char *zOpt; + sqlite3VdbeSetNumCols(v, 1); + pParse->nMem = 1; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC); + while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){ + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } + }else +#endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */ + +#ifndef SQLITE_OMIT_WAL + /* + ** PRAGMA [database.]wal_checkpoint = passive|full|restart + ** + ** Checkpoint the database. + */ + if( sqlite3StrICmp(zLeft, "wal_checkpoint")==0 ){ + int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED); + int eMode = SQLITE_CHECKPOINT_PASSIVE; + if( zRight ){ + if( sqlite3StrICmp(zRight, "full")==0 ){ + eMode = SQLITE_CHECKPOINT_FULL; + }else if( sqlite3StrICmp(zRight, "restart")==0 ){ + eMode = SQLITE_CHECKPOINT_RESTART; + } + } + if( sqlite3ReadSchema(pParse) ) goto pragma_out; + sqlite3VdbeSetNumCols(v, 3); + pParse->nMem = 3; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC); + + sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3); + }else + + /* + ** PRAGMA wal_autocheckpoint + ** PRAGMA wal_autocheckpoint = N + ** + ** Configure a database connection to automatically checkpoint a database + ** after accumulating N frames in the log. Or query for the current value + ** of N. + */ + if( sqlite3StrICmp(zLeft, "wal_autocheckpoint")==0 ){ + if( zRight ){ + sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight)); + } + returnSingleInt(pParse, "wal_autocheckpoint", + db->xWalCallback==sqlite3WalDefaultHook ? + SQLITE_PTR_TO_INT(db->pWalArg) : 0); + }else +#endif + +#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + /* + ** Report the current state of file logs for all databases + */ + if( sqlite3StrICmp(zLeft, "lock_status")==0 ){ + static const char *const azLockName[] = { + "unlocked", "shared", "reserved", "pending", "exclusive" + }; + int i; + sqlite3VdbeSetNumCols(v, 2); + pParse->nMem = 2; + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC); + sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC); + for(i=0; inDb; i++){ + Btree *pBt; + Pager *pPager; + const char *zState = "unknown"; + int j; + if( db->aDb[i].zName==0 ) continue; + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC); + pBt = db->aDb[i].pBt; + if( pBt==0 || (pPager = sqlite3BtreePager(pBt))==0 ){ + zState = "closed"; + }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0, + SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){ + zState = azLockName[j]; + } + sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2); + } + + }else +#endif + +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_HAS_CODEC_NO_ENCRYPTION) + if( sqlite3StrICmp(zLeft, "key")==0 && zRight ){ + sqlite3_key(db, zRight, sqlite3Strlen30(zRight)); + }else + if( sqlite3StrICmp(zLeft, "rekey")==0 && zRight ){ + sqlite3_rekey(db, zRight, sqlite3Strlen30(zRight)); + }else + if( zRight && (sqlite3StrICmp(zLeft, "hexkey")==0 || + sqlite3StrICmp(zLeft, "hexrekey")==0) ){ + int i, h1, h2; + char zKey[40]; + for(i=0; (h1 = zRight[i])!=0 && (h2 = zRight[i+1])!=0; i+=2){ + h1 += 9*(1&(h1>>6)); + h2 += 9*(1&(h2>>6)); + zKey[i/2] = (h2 & 0x0f) | ((h1 & 0xf)<<4); + } + if( (zLeft[3] & 0xf)==0xb ){ + sqlite3_key(db, zKey, i/2); + }else{ + sqlite3_rekey(db, zKey, i/2); + } + }else +#endif +#if (defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)) && !defined(SQLITE_HAS_CODEC_NO_ENCRYPTION) + if( sqlite3StrICmp(zLeft, "activate_extensions")==0 ){ +#ifdef SQLITE_HAS_CODEC + if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){ + sqlite3_activate_see(&zRight[4]); + } +#endif +#ifdef SQLITE_ENABLE_CEROD + if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){ + sqlite3_activate_cerod(&zRight[6]); + } +#endif + }else +#endif + + + {/* Empty ELSE clause */} + + /* + ** Reset the safety level, in case the fullfsync flag or synchronous + ** setting changed. + */ +#ifndef SQLITE_OMIT_PAGER_PRAGMAS + if( db->autoCommit ){ + sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level, + (db->flags&SQLITE_FullFSync)!=0, + (db->flags&SQLITE_CkptFullFSync)!=0); + } +#endif +pragma_out: + sqlite3DbFree(db, zLeft); + sqlite3DbFree(db, zRight); +} + +#endif /* SQLITE_OMIT_PRAGMA */ + +/************** End of pragma.c **********************************************/ +/************** Begin file prepare.c *****************************************/ +/* +** 2005 May 25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the implementation of the sqlite3_prepare() +** interface, and routines that contribute to loading the database schema +** from disk. +*/ + +/* +** Fill the InitData structure with an error message that indicates +** that the database is corrupt. +*/ +static void corruptSchema( + InitData *pData, /* Initialization context */ + const char *zObj, /* Object being parsed at the point of error */ + const char *zExtra /* Error information */ +){ + sqlite3 *db = pData->db; + if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){ + if( zObj==0 ) zObj = "?"; + sqlite3SetString(pData->pzErrMsg, db, + "malformed database schema (%s)", zObj); + if( zExtra ){ + *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg, + "%s - %s", *pData->pzErrMsg, zExtra); + } + } + pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT; +} + +/* +** This is the callback routine for the code that initializes the +** database. See sqlite3Init() below for additional information. +** This routine is also called from the OP_ParseSchema opcode of the VDBE. +** +** Each callback contains the following information: +** +** argv[0] = name of thing being created +** argv[1] = root page number for table or index. 0 for trigger or view. +** argv[2] = SQL text for the CREATE statement. +** +*/ +SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){ + InitData *pData = (InitData*)pInit; + sqlite3 *db = pData->db; + int iDb = pData->iDb; + + assert( argc==3 ); + UNUSED_PARAMETER2(NotUsed, argc); + assert( sqlite3_mutex_held(db->mutex) ); + DbClearProperty(db, iDb, DB_Empty); + if( db->mallocFailed ){ + corruptSchema(pData, argv[0], 0); + return 1; + } + + assert( iDb>=0 && iDbnDb ); + if( argv==0 ) return 0; /* Might happen if EMPTY_RESULT_CALLBACKS are on */ + if( argv[1]==0 ){ + corruptSchema(pData, argv[0], 0); + }else if( argv[2] && argv[2][0] ){ + /* Call the parser to process a CREATE TABLE, INDEX or VIEW. + ** But because db->init.busy is set to 1, no VDBE code is generated + ** or executed. All the parser does is build the internal data + ** structures that describe the table, index, or view. + */ + int rc; + sqlite3_stmt *pStmt; + TESTONLY(int rcp); /* Return code from sqlite3_prepare() */ + + assert( db->init.busy ); + db->init.iDb = iDb; + db->init.newTnum = sqlite3Atoi(argv[1]); + db->init.orphanTrigger = 0; + TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0); + rc = db->errCode; + assert( (rc&0xFF)==(rcp&0xFF) ); + db->init.iDb = 0; + if( SQLITE_OK!=rc ){ + if( db->init.orphanTrigger ){ + assert( iDb==1 ); + }else{ + pData->rc = rc; + if( rc==SQLITE_NOMEM ){ + db->mallocFailed = 1; + }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){ + corruptSchema(pData, argv[0], sqlite3_errmsg(db)); + } + } + } + sqlite3_finalize(pStmt); + }else if( argv[0]==0 ){ + corruptSchema(pData, 0, 0); + }else{ + /* If the SQL column is blank it means this is an index that + ** was created to be the PRIMARY KEY or to fulfill a UNIQUE + ** constraint for a CREATE TABLE. The index should have already + ** been created when we processed the CREATE TABLE. All we have + ** to do here is record the root page number for that index. + */ + Index *pIndex; + pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName); + if( pIndex==0 ){ + /* This can occur if there exists an index on a TEMP table which + ** has the same name as another index on a permanent index. Since + ** the permanent table is hidden by the TEMP table, we can also + ** safely ignore the index on the permanent table. + */ + /* Do Nothing */; + }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){ + corruptSchema(pData, argv[0], "invalid rootpage"); + } + } + return 0; +} + +/* +** Attempt to read the database schema and initialize internal +** data structures for a single database file. The index of the +** database file is given by iDb. iDb==0 is used for the main +** database. iDb==1 should never be used. iDb>=2 is used for +** auxiliary databases. Return one of the SQLITE_ error codes to +** indicate success or failure. +*/ +static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){ + int rc; + int i; + int size; + Table *pTab; + Db *pDb; + char const *azArg[4]; + int meta[5]; + InitData initData; + char const *zMasterSchema; + char const *zMasterName; + int openedTransaction = 0; + + /* + ** The master database table has a structure like this + */ + static const char master_schema[] = + "CREATE TABLE sqlite_master(\n" + " type text,\n" + " name text,\n" + " tbl_name text,\n" + " rootpage integer,\n" + " sql text\n" + ")" + ; +#ifndef SQLITE_OMIT_TEMPDB + static const char temp_master_schema[] = + "CREATE TEMP TABLE sqlite_temp_master(\n" + " type text,\n" + " name text,\n" + " tbl_name text,\n" + " rootpage integer,\n" + " sql text\n" + ")" + ; +#else + #define temp_master_schema 0 +#endif + + assert( iDb>=0 && iDbnDb ); + assert( db->aDb[iDb].pSchema ); + assert( sqlite3_mutex_held(db->mutex) ); + assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) ); + + /* zMasterSchema and zInitScript are set to point at the master schema + ** and initialisation script appropriate for the database being + ** initialised. zMasterName is the name of the master table. + */ + if( !OMIT_TEMPDB && iDb==1 ){ + zMasterSchema = temp_master_schema; + }else{ + zMasterSchema = master_schema; + } + zMasterName = SCHEMA_TABLE(iDb); + + /* Construct the schema tables. */ + azArg[0] = zMasterName; + azArg[1] = "1"; + azArg[2] = zMasterSchema; + azArg[3] = 0; + initData.db = db; + initData.iDb = iDb; + initData.rc = SQLITE_OK; + initData.pzErrMsg = pzErrMsg; + sqlite3InitCallback(&initData, 3, (char **)azArg, 0); + if( initData.rc ){ + rc = initData.rc; + goto error_out; + } + pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName); + if( ALWAYS(pTab) ){ + pTab->tabFlags |= TF_Readonly; + } + + /* Create a cursor to hold the database open + */ + pDb = &db->aDb[iDb]; + if( pDb->pBt==0 ){ + if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){ + DbSetProperty(db, 1, DB_SchemaLoaded); + } + return SQLITE_OK; + } + + /* If there is not already a read-only (or read-write) transaction opened + ** on the b-tree database, open one now. If a transaction is opened, it + ** will be closed before this function returns. */ + sqlite3BtreeEnter(pDb->pBt); + if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){ + rc = sqlite3BtreeBeginTrans(pDb->pBt, 0); + if( rc!=SQLITE_OK ){ + sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc)); + goto initone_error_out; + } + openedTransaction = 1; + } + + /* Get the database meta information. + ** + ** Meta values are as follows: + ** meta[0] Schema cookie. Changes with each schema change. + ** meta[1] File format of schema layer. + ** meta[2] Size of the page cache. + ** meta[3] Largest rootpage (auto/incr_vacuum mode) + ** meta[4] Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE + ** meta[5] User version + ** meta[6] Incremental vacuum mode + ** meta[7] unused + ** meta[8] unused + ** meta[9] unused + ** + ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to + ** the possible values of meta[4]. + */ + for(i=0; ipBt, i+1, (u32 *)&meta[i]); + } + pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1]; + + /* If opening a non-empty database, check the text encoding. For the + ** main database, set sqlite3.enc to the encoding of the main database. + ** For an attached db, it is an error if the encoding is not the same + ** as sqlite3.enc. + */ + if( meta[BTREE_TEXT_ENCODING-1] ){ /* text encoding */ + if( iDb==0 ){ + u8 encoding; + /* If opening the main database, set ENC(db). */ + encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3; + if( encoding==0 ) encoding = SQLITE_UTF8; + ENC(db) = encoding; + db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); + }else{ + /* If opening an attached database, the encoding much match ENC(db) */ + if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){ + sqlite3SetString(pzErrMsg, db, "attached databases must use the same" + " text encoding as main database"); + rc = SQLITE_ERROR; + goto initone_error_out; + } + } + }else{ + DbSetProperty(db, iDb, DB_Empty); + } + pDb->pSchema->enc = ENC(db); + + if( pDb->pSchema->cache_size==0 ){ + size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]); + if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; } + pDb->pSchema->cache_size = size; + sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size); + } + + /* + ** file_format==1 Version 3.0.0. + ** file_format==2 Version 3.1.3. // ALTER TABLE ADD COLUMN + ** file_format==3 Version 3.1.4. // ditto but with non-NULL defaults + ** file_format==4 Version 3.3.0. // DESC indices. Boolean constants + */ + pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1]; + if( pDb->pSchema->file_format==0 ){ + pDb->pSchema->file_format = 1; + } + if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){ + sqlite3SetString(pzErrMsg, db, "unsupported file format"); + rc = SQLITE_ERROR; + goto initone_error_out; + } + + /* Ticket #2804: When we open a database in the newer file format, + ** clear the legacy_file_format pragma flag so that a VACUUM will + ** not downgrade the database and thus invalidate any descending + ** indices that the user might have created. + */ + if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){ + db->flags &= ~SQLITE_LegacyFileFmt; + } + + /* Read the schema information out of the schema tables + */ + assert( db->init.busy ); + { + char *zSql; + zSql = sqlite3MPrintf(db, + "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid", + db->aDb[iDb].zName, zMasterName); +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); + xAuth = db->xAuth; + db->xAuth = 0; +#endif + rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0); +#ifndef SQLITE_OMIT_AUTHORIZATION + db->xAuth = xAuth; + } +#endif + if( rc==SQLITE_OK ) rc = initData.rc; + sqlite3DbFree(db, zSql); +#ifndef SQLITE_OMIT_ANALYZE + if( rc==SQLITE_OK ){ + sqlite3AnalysisLoad(db, iDb); + } +#endif + } + if( db->mallocFailed ){ + rc = SQLITE_NOMEM; + sqlite3ResetInternalSchema(db, 0); + } + if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){ + /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider + ** the schema loaded, even if errors occurred. In this situation the + ** current sqlite3_prepare() operation will fail, but the following one + ** will attempt to compile the supplied statement against whatever subset + ** of the schema was loaded before the error occurred. The primary + ** purpose of this is to allow access to the sqlite_master table + ** even when its contents have been corrupted. + */ + DbSetProperty(db, iDb, DB_SchemaLoaded); + rc = SQLITE_OK; + } + + /* Jump here for an error that occurs after successfully allocating + ** curMain and calling sqlite3BtreeEnter(). For an error that occurs + ** before that point, jump to error_out. + */ +initone_error_out: + if( openedTransaction ){ + sqlite3BtreeCommit(pDb->pBt); + } + sqlite3BtreeLeave(pDb->pBt); + +error_out: + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ + db->mallocFailed = 1; + } + return rc; +} + +/* +** Initialize all database files - the main database file, the file +** used to store temporary tables, and any additional database files +** created using ATTACH statements. Return a success code. If an +** error occurs, write an error message into *pzErrMsg. +** +** After a database is initialized, the DB_SchemaLoaded bit is set +** bit is set in the flags field of the Db structure. If the database +** file was of zero-length, then the DB_Empty flag is also set. +*/ +SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){ + int i, rc; + int commit_internal = !(db->flags&SQLITE_InternChanges); + + assert( sqlite3_mutex_held(db->mutex) ); + rc = SQLITE_OK; + db->init.busy = 1; + for(i=0; rc==SQLITE_OK && inDb; i++){ + if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue; + rc = sqlite3InitOne(db, i, pzErrMsg); + if( rc ){ + sqlite3ResetInternalSchema(db, i); + } + } + + /* Once all the other databases have been initialised, load the schema + ** for the TEMP database. This is loaded last, as the TEMP database + ** schema may contain references to objects in other databases. + */ +#ifndef SQLITE_OMIT_TEMPDB + if( rc==SQLITE_OK && ALWAYS(db->nDb>1) + && !DbHasProperty(db, 1, DB_SchemaLoaded) ){ + rc = sqlite3InitOne(db, 1, pzErrMsg); + if( rc ){ + sqlite3ResetInternalSchema(db, 1); + } + } +#endif + + db->init.busy = 0; + if( rc==SQLITE_OK && commit_internal ){ + sqlite3CommitInternalChanges(db); + } + + return rc; +} + +/* +** This routine is a no-op if the database schema is already initialised. +** Otherwise, the schema is loaded. An error code is returned. +*/ +SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){ + int rc = SQLITE_OK; + sqlite3 *db = pParse->db; + assert( sqlite3_mutex_held(db->mutex) ); + if( !db->init.busy ){ + rc = sqlite3Init(db, &pParse->zErrMsg); + } + if( rc!=SQLITE_OK ){ + pParse->rc = rc; + pParse->nErr++; + } + return rc; +} + + +/* +** Check schema cookies in all databases. If any cookie is out +** of date set pParse->rc to SQLITE_SCHEMA. If all schema cookies +** make no changes to pParse->rc. +*/ +static void schemaIsValid(Parse *pParse){ + sqlite3 *db = pParse->db; + int iDb; + int rc; + int cookie; + + assert( pParse->checkSchema ); + assert( sqlite3_mutex_held(db->mutex) ); + for(iDb=0; iDbnDb; iDb++){ + int openedTransaction = 0; /* True if a transaction is opened */ + Btree *pBt = db->aDb[iDb].pBt; /* Btree database to read cookie from */ + if( pBt==0 ) continue; + + /* If there is not already a read-only (or read-write) transaction opened + ** on the b-tree database, open one now. If a transaction is opened, it + ** will be closed immediately after reading the meta-value. */ + if( !sqlite3BtreeIsInReadTrans(pBt) ){ + rc = sqlite3BtreeBeginTrans(pBt, 0); + if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){ + db->mallocFailed = 1; + } + if( rc!=SQLITE_OK ) return; + openedTransaction = 1; + } + + /* Read the schema cookie from the database. If it does not match the + ** value stored as part of the in-memory schema representation, + ** set Parse.rc to SQLITE_SCHEMA. */ + sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie); + if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){ + pParse->rc = SQLITE_SCHEMA; + } + + /* Close the transaction, if one was opened. */ + if( openedTransaction ){ + sqlite3BtreeCommit(pBt); + } + } +} + +/* +** Convert a schema pointer into the iDb index that indicates +** which database file in db->aDb[] the schema refers to. +** +** If the same database is attached more than once, the first +** attached database is returned. +*/ +SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){ + int i = -1000000; + + /* If pSchema is NULL, then return -1000000. This happens when code in + ** expr.c is trying to resolve a reference to a transient table (i.e. one + ** created by a sub-select). In this case the return value of this + ** function should never be used. + ** + ** We return -1000000 instead of the more usual -1 simply because using + ** -1000000 as the incorrect index into db->aDb[] is much + ** more likely to cause a segfault than -1 (of course there are assert() + ** statements too, but it never hurts to play the odds). + */ + assert( sqlite3_mutex_held(db->mutex) ); + if( pSchema ){ + for(i=0; ALWAYS(inDb); i++){ + if( db->aDb[i].pSchema==pSchema ){ + break; + } + } + assert( i>=0 && inDb ); + } + return i; +} + +/* +** Compile the UTF-8 encoded SQL statement zSql into a statement handle. +*/ +static int sqlite3Prepare( + sqlite3 *db, /* Database handle. */ + const char *zSql, /* UTF-8 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ + Vdbe *pReprepare, /* VM being reprepared */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const char **pzTail /* OUT: End of parsed string */ +){ + Parse *pParse; /* Parsing context */ + char *zErrMsg = 0; /* Error message */ + int rc = SQLITE_OK; /* Result code */ + int i; /* Loop counter */ + + /* Allocate the parsing context */ + pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); + if( pParse==0 ){ + rc = SQLITE_NOMEM; + goto end_prepare; + } + pParse->pReprepare = pReprepare; + assert( ppStmt && *ppStmt==0 ); + assert( !db->mallocFailed ); + assert( sqlite3_mutex_held(db->mutex) ); + + /* Check to verify that it is possible to get a read lock on all + ** database schemas. The inability to get a read lock indicates that + ** some other database connection is holding a write-lock, which in + ** turn means that the other connection has made uncommitted changes + ** to the schema. + ** + ** Were we to proceed and prepare the statement against the uncommitted + ** schema changes and if those schema changes are subsequently rolled + ** back and different changes are made in their place, then when this + ** prepared statement goes to run the schema cookie would fail to detect + ** the schema change. Disaster would follow. + ** + ** This thread is currently holding mutexes on all Btrees (because + ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it + ** is not possible for another thread to start a new schema change + ** while this routine is running. Hence, we do not need to hold + ** locks on the schema, we just need to make sure nobody else is + ** holding them. + ** + ** Note that setting READ_UNCOMMITTED overrides most lock detection, + ** but it does *not* override schema lock detection, so this all still + ** works even if READ_UNCOMMITTED is set. + */ + for(i=0; inDb; i++) { + Btree *pBt = db->aDb[i].pBt; + if( pBt ){ + assert( sqlite3BtreeHoldsMutex(pBt) ); + rc = sqlite3BtreeSchemaLocked(pBt); + if( rc ){ + const char *zDb = db->aDb[i].zName; + sqlite3Error(db, rc, "database schema is locked: %s", zDb); + testcase( db->flags & SQLITE_ReadUncommitted ); + goto end_prepare; + } + } + } + + sqlite3VtabUnlockList(db); + + pParse->db = db; + pParse->nQueryLoop = (double)1; + if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){ + char *zSqlCopy; + int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; + testcase( nBytes==mxLen ); + testcase( nBytes==mxLen+1 ); + if( nBytes>mxLen ){ + sqlite3Error(db, SQLITE_TOOBIG, "statement too long"); + rc = sqlite3ApiExit(db, SQLITE_TOOBIG); + goto end_prepare; + } + zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes); + if( zSqlCopy ){ + sqlite3RunParser(pParse, zSqlCopy, &zErrMsg); + sqlite3DbFree(db, zSqlCopy); + pParse->zTail = &zSql[pParse->zTail-zSqlCopy]; + }else{ + pParse->zTail = &zSql[nBytes]; + } + }else{ + sqlite3RunParser(pParse, zSql, &zErrMsg); + } + assert( 1==(int)pParse->nQueryLoop ); + + if( db->mallocFailed ){ + pParse->rc = SQLITE_NOMEM; + } + if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK; + if( pParse->checkSchema ){ + schemaIsValid(pParse); + } + if( pParse->rc==SQLITE_SCHEMA ){ + sqlite3ResetInternalSchema(db, 0); + } + if( db->mallocFailed ){ + pParse->rc = SQLITE_NOMEM; + } + if( pzTail ){ + *pzTail = pParse->zTail; + } + rc = pParse->rc; + +#ifndef SQLITE_OMIT_EXPLAIN + if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){ + static const char * const azColName[] = { + "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment", + "selectid", "order", "from", "detail" + }; + int iFirst, mx; + if( pParse->explain==2 ){ + sqlite3VdbeSetNumCols(pParse->pVdbe, 4); + iFirst = 8; + mx = 12; + }else{ + sqlite3VdbeSetNumCols(pParse->pVdbe, 8); + iFirst = 0; + mx = 8; + } + for(i=iFirst; ipVdbe, i-iFirst, COLNAME_NAME, + azColName[i], SQLITE_STATIC); + } + } +#endif + + assert( db->init.busy==0 || saveSqlFlag==0 ); + if( db->init.busy==0 ){ + Vdbe *pVdbe = pParse->pVdbe; + sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag); + } + if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){ + sqlite3VdbeFinalize(pParse->pVdbe); + assert(!(*ppStmt)); + }else{ + *ppStmt = (sqlite3_stmt*)pParse->pVdbe; + } + + if( zErrMsg ){ + sqlite3Error(db, rc, "%s", zErrMsg); + sqlite3DbFree(db, zErrMsg); + }else{ + sqlite3Error(db, rc, 0); + } + + /* Delete any TriggerPrg structures allocated while parsing this statement. */ + while( pParse->pTriggerPrg ){ + TriggerPrg *pT = pParse->pTriggerPrg; + pParse->pTriggerPrg = pT->pNext; + sqlite3DbFree(db, pT); + } + +end_prepare: + + sqlite3StackFree(db, pParse); + rc = sqlite3ApiExit(db, rc); + assert( (rc&db->errMask)==rc ); + return rc; +} +static int sqlite3LockAndPrepare( + sqlite3 *db, /* Database handle. */ + const char *zSql, /* UTF-8 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + int saveSqlFlag, /* True to copy SQL text into the sqlite3_stmt */ + Vdbe *pOld, /* VM being reprepared */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const char **pzTail /* OUT: End of parsed string */ +){ + int rc; + assert( ppStmt!=0 ); + *ppStmt = 0; + if( !sqlite3SafetyCheckOk(db) ){ + return SQLITE_MISUSE_BKPT; + } + sqlite3_mutex_enter(db->mutex); + sqlite3BtreeEnterAll(db); + rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); + if( rc==SQLITE_SCHEMA ){ + sqlite3_finalize(*ppStmt); + rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail); + } + sqlite3BtreeLeaveAll(db); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Rerun the compilation of a statement after a schema change. +** +** If the statement is successfully recompiled, return SQLITE_OK. Otherwise, +** if the statement cannot be recompiled because another connection has +** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error +** occurs, return SQLITE_SCHEMA. +*/ +SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){ + int rc; + sqlite3_stmt *pNew; + const char *zSql; + sqlite3 *db; + + assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) ); + zSql = sqlite3_sql((sqlite3_stmt *)p); + assert( zSql!=0 ); /* Reprepare only called for prepare_v2() statements */ + db = sqlite3VdbeDb(p); + assert( sqlite3_mutex_held(db->mutex) ); + rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0); + if( rc ){ + if( rc==SQLITE_NOMEM ){ + db->mallocFailed = 1; + } + assert( pNew==0 ); + return rc; + }else{ + assert( pNew!=0 ); + } + sqlite3VdbeSwap((Vdbe*)pNew, p); + sqlite3TransferBindings(pNew, (sqlite3_stmt*)p); + sqlite3VdbeResetStepResult((Vdbe*)pNew); + sqlite3VdbeFinalize((Vdbe*)pNew); + return SQLITE_OK; +} + + +/* +** Two versions of the official API. Legacy and new use. In the legacy +** version, the original SQL text is not saved in the prepared statement +** and so if a schema change occurs, SQLITE_SCHEMA is returned by +** sqlite3_step(). In the new version, the original SQL text is retained +** and the statement is automatically recompiled if an schema change +** occurs. +*/ +SQLITE_API int sqlite3_prepare( + sqlite3 *db, /* Database handle. */ + const char *zSql, /* UTF-8 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const char **pzTail /* OUT: End of parsed string */ +){ + int rc; + rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; +} +SQLITE_API int sqlite3_prepare_v2( + sqlite3 *db, /* Database handle. */ + const char *zSql, /* UTF-8 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const char **pzTail /* OUT: End of parsed string */ +){ + int rc; + rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; +} + + +#ifndef SQLITE_OMIT_UTF16 +/* +** Compile the UTF-16 encoded SQL statement zSql into a statement handle. +*/ +static int sqlite3Prepare16( + sqlite3 *db, /* Database handle. */ + const void *zSql, /* UTF-16 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + int saveSqlFlag, /* True to save SQL text into the sqlite3_stmt */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const void **pzTail /* OUT: End of parsed string */ +){ + /* This function currently works by first transforming the UTF-16 + ** encoded string to UTF-8, then invoking sqlite3_prepare(). The + ** tricky bit is figuring out the pointer to return in *pzTail. + */ + char *zSql8; + const char *zTail8 = 0; + int rc = SQLITE_OK; + + assert( ppStmt ); + *ppStmt = 0; + if( !sqlite3SafetyCheckOk(db) ){ + return SQLITE_MISUSE_BKPT; + } + sqlite3_mutex_enter(db->mutex); + zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE); + if( zSql8 ){ + rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8); + } + + if( zTail8 && pzTail ){ + /* If sqlite3_prepare returns a tail pointer, we calculate the + ** equivalent pointer into the UTF-16 string by counting the unicode + ** characters between zSql8 and zTail8, and then returning a pointer + ** the same number of characters into the UTF-16 string. + */ + int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8)); + *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed); + } + sqlite3DbFree(db, zSql8); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Two versions of the official API. Legacy and new use. In the legacy +** version, the original SQL text is not saved in the prepared statement +** and so if a schema change occurs, SQLITE_SCHEMA is returned by +** sqlite3_step(). In the new version, the original SQL text is retained +** and the statement is automatically recompiled if an schema change +** occurs. +*/ +SQLITE_API int sqlite3_prepare16( + sqlite3 *db, /* Database handle. */ + const void *zSql, /* UTF-16 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const void **pzTail /* OUT: End of parsed string */ +){ + int rc; + rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; +} +SQLITE_API int sqlite3_prepare16_v2( + sqlite3 *db, /* Database handle. */ + const void *zSql, /* UTF-16 encoded SQL statement. */ + int nBytes, /* Length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: A pointer to the prepared statement */ + const void **pzTail /* OUT: End of parsed string */ +){ + int rc; + rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail); + assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 ); /* VERIFY: F13021 */ + return rc; +} + +#endif /* SQLITE_OMIT_UTF16 */ + +/************** End of prepare.c *********************************************/ +/************** Begin file select.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that are called by the parser +** to handle SELECT statements in SQLite. +*/ + + +/* +** Delete all the content of a Select structure but do not deallocate +** the select structure itself. +*/ +static void clearSelect(sqlite3 *db, Select *p){ + sqlite3ExprListDelete(db, p->pEList); + sqlite3SrcListDelete(db, p->pSrc); + sqlite3ExprDelete(db, p->pWhere); + sqlite3ExprListDelete(db, p->pGroupBy); + sqlite3ExprDelete(db, p->pHaving); + sqlite3ExprListDelete(db, p->pOrderBy); + sqlite3SelectDelete(db, p->pPrior); + sqlite3ExprDelete(db, p->pLimit); + sqlite3ExprDelete(db, p->pOffset); +} + +/* +** Initialize a SelectDest structure. +*/ +SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){ + pDest->eDest = (u8)eDest; + pDest->iParm = iParm; + pDest->affinity = 0; + pDest->iMem = 0; + pDest->nMem = 0; +} + + +/* +** Allocate a new Select structure and return a pointer to that +** structure. +*/ +SQLITE_PRIVATE Select *sqlite3SelectNew( + Parse *pParse, /* Parsing context */ + ExprList *pEList, /* which columns to include in the result */ + SrcList *pSrc, /* the FROM clause -- which tables to scan */ + Expr *pWhere, /* the WHERE clause */ + ExprList *pGroupBy, /* the GROUP BY clause */ + Expr *pHaving, /* the HAVING clause */ + ExprList *pOrderBy, /* the ORDER BY clause */ + int isDistinct, /* true if the DISTINCT keyword is present */ + Expr *pLimit, /* LIMIT value. NULL means not used */ + Expr *pOffset /* OFFSET value. NULL means no offset */ +){ + Select *pNew; + Select standin; + sqlite3 *db = pParse->db; + pNew = sqlite3DbMallocZero(db, sizeof(*pNew) ); + assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */ + if( pNew==0 ){ + pNew = &standin; + memset(pNew, 0, sizeof(*pNew)); + } + if( pEList==0 ){ + pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0)); + } + pNew->pEList = pEList; + pNew->pSrc = pSrc; + pNew->pWhere = pWhere; + pNew->pGroupBy = pGroupBy; + pNew->pHaving = pHaving; + pNew->pOrderBy = pOrderBy; + pNew->selFlags = isDistinct ? SF_Distinct : 0; + pNew->op = TK_SELECT; + pNew->pLimit = pLimit; + pNew->pOffset = pOffset; + assert( pOffset==0 || pLimit!=0 ); + pNew->addrOpenEphm[0] = -1; + pNew->addrOpenEphm[1] = -1; + pNew->addrOpenEphm[2] = -1; + if( db->mallocFailed ) { + clearSelect(db, pNew); + if( pNew!=&standin ) sqlite3DbFree(db, pNew); + pNew = 0; + } + return pNew; +} + +/* +** Delete the given Select structure and all of its substructures. +*/ +SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){ + if( p ){ + clearSelect(db, p); + sqlite3DbFree(db, p); + } +} + +/* +** Given 1 to 3 identifiers preceeding the JOIN keyword, determine the +** type of join. Return an integer constant that expresses that type +** in terms of the following bit values: +** +** JT_INNER +** JT_CROSS +** JT_OUTER +** JT_NATURAL +** JT_LEFT +** JT_RIGHT +** +** A full outer join is the combination of JT_LEFT and JT_RIGHT. +** +** If an illegal or unsupported join type is seen, then still return +** a join type, but put an error in the pParse structure. +*/ +SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){ + int jointype = 0; + Token *apAll[3]; + Token *p; + /* 0123456789 123456789 123456789 123 */ + static const char zKeyText[] = "naturaleftouterightfullinnercross"; + static const struct { + u8 i; /* Beginning of keyword text in zKeyText[] */ + u8 nChar; /* Length of the keyword in characters */ + u8 code; /* Join type mask */ + } aKeyword[] = { + /* natural */ { 0, 7, JT_NATURAL }, + /* left */ { 6, 4, JT_LEFT|JT_OUTER }, + /* outer */ { 10, 5, JT_OUTER }, + /* right */ { 14, 5, JT_RIGHT|JT_OUTER }, + /* full */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER }, + /* inner */ { 23, 5, JT_INNER }, + /* cross */ { 28, 5, JT_INNER|JT_CROSS }, + }; + int i, j; + apAll[0] = pA; + apAll[1] = pB; + apAll[2] = pC; + for(i=0; i<3 && apAll[i]; i++){ + p = apAll[i]; + for(j=0; jn==aKeyword[j].nChar + && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){ + jointype |= aKeyword[j].code; + break; + } + } + testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 ); + if( j>=ArraySize(aKeyword) ){ + jointype |= JT_ERROR; + break; + } + } + if( + (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) || + (jointype & JT_ERROR)!=0 + ){ + const char *zSp = " "; + assert( pB!=0 ); + if( pC==0 ){ zSp++; } + sqlite3ErrorMsg(pParse, "unknown or unsupported join type: " + "%T %T%s%T", pA, pB, zSp, pC); + jointype = JT_INNER; + }else if( (jointype & JT_OUTER)!=0 + && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){ + sqlite3ErrorMsg(pParse, + "RIGHT and FULL OUTER JOINs are not currently supported"); + jointype = JT_INNER; + } + return jointype; +} + +/* +** Return the index of a column in a table. Return -1 if the column +** is not contained in the table. +*/ +static int columnIndex(Table *pTab, const char *zCol){ + int i; + for(i=0; inCol; i++){ + if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i; + } + return -1; +} + +/* +** Search the first N tables in pSrc, from left to right, looking for a +** table that has a column named zCol. +** +** When found, set *piTab and *piCol to the table index and column index +** of the matching column and return TRUE. +** +** If not found, return FALSE. +*/ +static int tableAndColumnIndex( + SrcList *pSrc, /* Array of tables to search */ + int N, /* Number of tables in pSrc->a[] to search */ + const char *zCol, /* Name of the column we are looking for */ + int *piTab, /* Write index of pSrc->a[] here */ + int *piCol /* Write index of pSrc->a[*piTab].pTab->aCol[] here */ +){ + int i; /* For looping over tables in pSrc */ + int iCol; /* Index of column matching zCol */ + + assert( (piTab==0)==(piCol==0) ); /* Both or neither are NULL */ + for(i=0; ia[i].pTab, zCol); + if( iCol>=0 ){ + if( piTab ){ + *piTab = i; + *piCol = iCol; + } + return 1; + } + } + return 0; +} + +/* +** This function is used to add terms implied by JOIN syntax to the +** WHERE clause expression of a SELECT statement. The new term, which +** is ANDed with the existing WHERE clause, is of the form: +** +** (tab1.col1 = tab2.col2) +** +** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the +** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is +** column iColRight of tab2. +*/ +static void addWhereTerm( + Parse *pParse, /* Parsing context */ + SrcList *pSrc, /* List of tables in FROM clause */ + int iLeft, /* Index of first table to join in pSrc */ + int iColLeft, /* Index of column in first table */ + int iRight, /* Index of second table in pSrc */ + int iColRight, /* Index of column in second table */ + int isOuterJoin, /* True if this is an OUTER join */ + Expr **ppWhere /* IN/OUT: The WHERE clause to add to */ +){ + sqlite3 *db = pParse->db; + Expr *pE1; + Expr *pE2; + Expr *pEq; + + assert( iLeftnSrc>iRight ); + assert( pSrc->a[iLeft].pTab ); + assert( pSrc->a[iRight].pTab ); + + pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft); + pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight); + + pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0); + if( pEq && isOuterJoin ){ + ExprSetProperty(pEq, EP_FromJoin); + assert( !ExprHasAnyProperty(pEq, EP_TokenOnly|EP_Reduced) ); + ExprSetIrreducible(pEq); + pEq->iRightJoinTable = (i16)pE2->iTable; + } + *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq); +} + +/* +** Set the EP_FromJoin property on all terms of the given expression. +** And set the Expr.iRightJoinTable to iTable for every term in the +** expression. +** +** The EP_FromJoin property is used on terms of an expression to tell +** the LEFT OUTER JOIN processing logic that this term is part of the +** join restriction specified in the ON or USING clause and not a part +** of the more general WHERE clause. These terms are moved over to the +** WHERE clause during join processing but we need to remember that they +** originated in the ON or USING clause. +** +** The Expr.iRightJoinTable tells the WHERE clause processing that the +** expression depends on table iRightJoinTable even if that table is not +** explicitly mentioned in the expression. That information is needed +** for cases like this: +** +** SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5 +** +** The where clause needs to defer the handling of the t1.x=5 +** term until after the t2 loop of the join. In that way, a +** NULL t2 row will be inserted whenever t1.x!=5. If we do not +** defer the handling of t1.x=5, it will be processed immediately +** after the t1 loop and rows with t1.x!=5 will never appear in +** the output, which is incorrect. +*/ +static void setJoinExpr(Expr *p, int iTable){ + while( p ){ + ExprSetProperty(p, EP_FromJoin); + assert( !ExprHasAnyProperty(p, EP_TokenOnly|EP_Reduced) ); + ExprSetIrreducible(p); + p->iRightJoinTable = (i16)iTable; + setJoinExpr(p->pLeft, iTable); + p = p->pRight; + } +} + +/* +** This routine processes the join information for a SELECT statement. +** ON and USING clauses are converted into extra terms of the WHERE clause. +** NATURAL joins also create extra WHERE clause terms. +** +** The terms of a FROM clause are contained in the Select.pSrc structure. +** The left most table is the first entry in Select.pSrc. The right-most +** table is the last entry. The join operator is held in the entry to +** the left. Thus entry 0 contains the join operator for the join between +** entries 0 and 1. Any ON or USING clauses associated with the join are +** also attached to the left entry. +** +** This routine returns the number of errors encountered. +*/ +static int sqliteProcessJoin(Parse *pParse, Select *p){ + SrcList *pSrc; /* All tables in the FROM clause */ + int i, j; /* Loop counters */ + struct SrcList_item *pLeft; /* Left table being joined */ + struct SrcList_item *pRight; /* Right table being joined */ + + pSrc = p->pSrc; + pLeft = &pSrc->a[0]; + pRight = &pLeft[1]; + for(i=0; inSrc-1; i++, pRight++, pLeft++){ + Table *pLeftTab = pLeft->pTab; + Table *pRightTab = pRight->pTab; + int isOuter; + + if( NEVER(pLeftTab==0 || pRightTab==0) ) continue; + isOuter = (pRight->jointype & JT_OUTER)!=0; + + /* When the NATURAL keyword is present, add WHERE clause terms for + ** every column that the two tables have in common. + */ + if( pRight->jointype & JT_NATURAL ){ + if( pRight->pOn || pRight->pUsing ){ + sqlite3ErrorMsg(pParse, "a NATURAL join may not have " + "an ON or USING clause", 0); + return 1; + } + for(j=0; jnCol; j++){ + char *zName; /* Name of column in the right table */ + int iLeft; /* Matching left table */ + int iLeftCol; /* Matching column in the left table */ + + zName = pRightTab->aCol[j].zName; + if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){ + addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j, + isOuter, &p->pWhere); + } + } + } + + /* Disallow both ON and USING clauses in the same join + */ + if( pRight->pOn && pRight->pUsing ){ + sqlite3ErrorMsg(pParse, "cannot have both ON and USING " + "clauses in the same join"); + return 1; + } + + /* Add the ON clause to the end of the WHERE clause, connected by + ** an AND operator. + */ + if( pRight->pOn ){ + if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor); + p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn); + pRight->pOn = 0; + } + + /* Create extra terms on the WHERE clause for each column named + ** in the USING clause. Example: If the two tables to be joined are + ** A and B and the USING clause names X, Y, and Z, then add this + ** to the WHERE clause: A.X=B.X AND A.Y=B.Y AND A.Z=B.Z + ** Report an error if any column mentioned in the USING clause is + ** not contained in both tables to be joined. + */ + if( pRight->pUsing ){ + IdList *pList = pRight->pUsing; + for(j=0; jnId; j++){ + char *zName; /* Name of the term in the USING clause */ + int iLeft; /* Table on the left with matching column name */ + int iLeftCol; /* Column number of matching column on the left */ + int iRightCol; /* Column number of matching column on the right */ + + zName = pList->a[j].zName; + iRightCol = columnIndex(pRightTab, zName); + if( iRightCol<0 + || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) + ){ + sqlite3ErrorMsg(pParse, "cannot join using column %s - column " + "not present in both tables", zName); + return 1; + } + addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol, + isOuter, &p->pWhere); + } + } + } + return 0; +} + +/* +** Insert code into "v" that will push the record on the top of the +** stack into the sorter. +*/ +static void pushOntoSorter( + Parse *pParse, /* Parser context */ + ExprList *pOrderBy, /* The ORDER BY clause */ + Select *pSelect, /* The whole SELECT statement */ + int regData /* Register holding data to be sorted */ +){ + Vdbe *v = pParse->pVdbe; + int nExpr = pOrderBy->nExpr; + int regBase = sqlite3GetTempRange(pParse, nExpr+2); + int regRecord = sqlite3GetTempReg(pParse); + sqlite3ExprCacheClear(pParse); + sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0); + sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr); + sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord); + sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord); + sqlite3ReleaseTempReg(pParse, regRecord); + sqlite3ReleaseTempRange(pParse, regBase, nExpr+2); + if( pSelect->iLimit ){ + int addr1, addr2; + int iLimit; + if( pSelect->iOffset ){ + iLimit = pSelect->iOffset+1; + }else{ + iLimit = pSelect->iLimit; + } + addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit); + sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1); + addr2 = sqlite3VdbeAddOp0(v, OP_Goto); + sqlite3VdbeJumpHere(v, addr1); + sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor); + sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor); + sqlite3VdbeJumpHere(v, addr2); + } +} + +/* +** Add code to implement the OFFSET +*/ +static void codeOffset( + Vdbe *v, /* Generate code into this VM */ + Select *p, /* The SELECT statement being coded */ + int iContinue /* Jump here to skip the current record */ +){ + if( p->iOffset && iContinue!=0 ){ + int addr; + sqlite3VdbeAddOp2(v, OP_AddImm, p->iOffset, -1); + addr = sqlite3VdbeAddOp1(v, OP_IfNeg, p->iOffset); + sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue); + VdbeComment((v, "skip OFFSET records")); + sqlite3VdbeJumpHere(v, addr); + } +} + +/* +** Add code that will check to make sure the N registers starting at iMem +** form a distinct entry. iTab is a sorting index that holds previously +** seen combinations of the N values. A new entry is made in iTab +** if the current N values are new. +** +** A jump to addrRepeat is made and the N+1 values are popped from the +** stack if the top N elements are not distinct. +*/ +static void codeDistinct( + Parse *pParse, /* Parsing and code generating context */ + int iTab, /* A sorting index used to test for distinctness */ + int addrRepeat, /* Jump to here if not distinct */ + int N, /* Number of elements */ + int iMem /* First element */ +){ + Vdbe *v; + int r1; + + v = pParse->pVdbe; + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N); + sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1); + sqlite3ReleaseTempReg(pParse, r1); +} + +#ifndef SQLITE_OMIT_SUBQUERY +/* +** Generate an error message when a SELECT is used within a subexpression +** (example: "a IN (SELECT * FROM table)") but it has more than 1 result +** column. We do this in a subroutine because the error used to occur +** in multiple places. (The error only occurs in one place now, but we +** retain the subroutine to minimize code disruption.) +*/ +static int checkForMultiColumnSelectError( + Parse *pParse, /* Parse context. */ + SelectDest *pDest, /* Destination of SELECT results */ + int nExpr /* Number of result columns returned by SELECT */ +){ + int eDest = pDest->eDest; + if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){ + sqlite3ErrorMsg(pParse, "only a single result allowed for " + "a SELECT that is part of an expression"); + return 1; + }else{ + return 0; + } +} +#endif + +/* +** This routine generates the code for the inside of the inner loop +** of a SELECT. +** +** If srcTab and nColumn are both zero, then the pEList expressions +** are evaluated in order to get the data for this row. If nColumn>0 +** then data is pulled from srcTab and pEList is used only to get the +** datatypes for each column. +*/ +static void selectInnerLoop( + Parse *pParse, /* The parser context */ + Select *p, /* The complete select statement being coded */ + ExprList *pEList, /* List of values being extracted */ + int srcTab, /* Pull data from this table */ + int nColumn, /* Number of columns in the source table */ + ExprList *pOrderBy, /* If not NULL, sort results using this key */ + int distinct, /* If >=0, make sure results are distinct */ + SelectDest *pDest, /* How to dispose of the results */ + int iContinue, /* Jump here to continue with next row */ + int iBreak /* Jump here to break out of the inner loop */ +){ + Vdbe *v = pParse->pVdbe; + int i; + int hasDistinct; /* True if the DISTINCT keyword is present */ + int regResult; /* Start of memory holding result set */ + int eDest = pDest->eDest; /* How to dispose of results */ + int iParm = pDest->iParm; /* First argument to disposal method */ + int nResultCol; /* Number of result columns */ + + assert( v ); + if( NEVER(v==0) ) return; + assert( pEList!=0 ); + hasDistinct = distinct>=0; + if( pOrderBy==0 && !hasDistinct ){ + codeOffset(v, p, iContinue); + } + + /* Pull the requested columns. + */ + if( nColumn>0 ){ + nResultCol = nColumn; + }else{ + nResultCol = pEList->nExpr; + } + if( pDest->iMem==0 ){ + pDest->iMem = pParse->nMem+1; + pDest->nMem = nResultCol; + pParse->nMem += nResultCol; + }else{ + assert( pDest->nMem==nResultCol ); + } + regResult = pDest->iMem; + if( nColumn>0 ){ + for(i=0; inExpr==nColumn ); + codeDistinct(pParse, distinct, iContinue, nColumn, regResult); + if( pOrderBy==0 ){ + codeOffset(v, p, iContinue); + } + } + + switch( eDest ){ + /* In this mode, write each query result to the key of the temporary + ** table iParm. + */ +#ifndef SQLITE_OMIT_COMPOUND_SELECT + case SRT_Union: { + int r1; + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); + sqlite3ReleaseTempReg(pParse, r1); + break; + } + + /* Construct a record from the query result, but instead of + ** saving that record, use it as a key to delete elements from + ** the temporary table iParm. + */ + case SRT_Except: { + sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nColumn); + break; + } +#endif + + /* Store the result as data using a unique key. + */ + case SRT_Table: + case SRT_EphemTab: { + int r1 = sqlite3GetTempReg(pParse); + testcase( eDest==SRT_Table ); + testcase( eDest==SRT_EphemTab ); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1); + if( pOrderBy ){ + pushOntoSorter(pParse, pOrderBy, p, r1); + }else{ + int r2 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2); + sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3ReleaseTempReg(pParse, r2); + } + sqlite3ReleaseTempReg(pParse, r1); + break; + } + +#ifndef SQLITE_OMIT_SUBQUERY + /* If we are creating a set for an "expr IN (SELECT ...)" construct, + ** then there should be a single item on the stack. Write this + ** item into the set table with bogus data. + */ + case SRT_Set: { + assert( nColumn==1 ); + p->affinity = sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affinity); + if( pOrderBy ){ + /* At first glance you would think we could optimize out the + ** ORDER BY in this case since the order of entries in the set + ** does not matter. But there might be a LIMIT clause, in which + ** case the order does matter */ + pushOntoSorter(pParse, pOrderBy, p, regResult); + }else{ + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult, 1, r1, &p->affinity, 1); + sqlite3ExprCacheAffinityChange(pParse, regResult, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1); + sqlite3ReleaseTempReg(pParse, r1); + } + break; + } + + /* If any row exist in the result set, record that fact and abort. + */ + case SRT_Exists: { + sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm); + /* The LIMIT clause will terminate the loop for us */ + break; + } + + /* If this is a scalar select that is part of an expression, then + ** store the results in the appropriate memory cell and break out + ** of the scan loop. + */ + case SRT_Mem: { + assert( nColumn==1 ); + if( pOrderBy ){ + pushOntoSorter(pParse, pOrderBy, p, regResult); + }else{ + sqlite3ExprCodeMove(pParse, regResult, iParm, 1); + /* The LIMIT clause will jump out of the loop for us */ + } + break; + } +#endif /* #ifndef SQLITE_OMIT_SUBQUERY */ + + /* Send the data to the callback function or to a subroutine. In the + ** case of a subroutine, the subroutine itself is responsible for + ** popping the data from the stack. + */ + case SRT_Coroutine: + case SRT_Output: { + testcase( eDest==SRT_Coroutine ); + testcase( eDest==SRT_Output ); + if( pOrderBy ){ + int r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nColumn, r1); + pushOntoSorter(pParse, pOrderBy, p, r1); + sqlite3ReleaseTempReg(pParse, r1); + }else if( eDest==SRT_Coroutine ){ + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm); + }else{ + sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nColumn); + sqlite3ExprCacheAffinityChange(pParse, regResult, nColumn); + } + break; + } + +#if !defined(SQLITE_OMIT_TRIGGER) + /* Discard the results. This is used for SELECT statements inside + ** the body of a TRIGGER. The purpose of such selects is to call + ** user-defined functions that have side effects. We do not care + ** about the actual results of the select. + */ + default: { + assert( eDest==SRT_Discard ); + break; + } +#endif + } + + /* Jump to the end of the loop if the LIMIT is reached. Except, if + ** there is a sorter, in which case the sorter has already limited + ** the output for us. + */ + if( pOrderBy==0 && p->iLimit ){ + sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); + } +} + +/* +** Given an expression list, generate a KeyInfo structure that records +** the collating sequence for each expression in that expression list. +** +** If the ExprList is an ORDER BY or GROUP BY clause then the resulting +** KeyInfo structure is appropriate for initializing a virtual index to +** implement that clause. If the ExprList is the result set of a SELECT +** then the KeyInfo structure is appropriate for initializing a virtual +** index to implement a DISTINCT test. +** +** Space to hold the KeyInfo structure is obtain from malloc. The calling +** function is responsible for seeing that this structure is eventually +** freed. Add the KeyInfo structure to the P4 field of an opcode using +** P4_KEYINFO_HANDOFF is the usual way of dealing with this. +*/ +static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList){ + sqlite3 *db = pParse->db; + int nExpr; + KeyInfo *pInfo; + struct ExprList_item *pItem; + int i; + + nExpr = pList->nExpr; + pInfo = sqlite3DbMallocZero(db, sizeof(*pInfo) + nExpr*(sizeof(CollSeq*)+1) ); + if( pInfo ){ + pInfo->aSortOrder = (u8*)&pInfo->aColl[nExpr]; + pInfo->nField = (u16)nExpr; + pInfo->enc = ENC(db); + pInfo->db = db; + for(i=0, pItem=pList->a; ipExpr); + if( !pColl ){ + pColl = db->pDfltColl; + } + pInfo->aColl[i] = pColl; + pInfo->aSortOrder[i] = pItem->sortOrder; + } + } + return pInfo; +} + +#ifndef SQLITE_OMIT_COMPOUND_SELECT +/* +** Name of the connection operator, used for error messages. +*/ +static const char *selectOpName(int id){ + char *z; + switch( id ){ + case TK_ALL: z = "UNION ALL"; break; + case TK_INTERSECT: z = "INTERSECT"; break; + case TK_EXCEPT: z = "EXCEPT"; break; + default: z = "UNION"; break; + } + return z; +} +#endif /* SQLITE_OMIT_COMPOUND_SELECT */ + +#ifndef SQLITE_OMIT_EXPLAIN +/* +** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function +** is a no-op. Otherwise, it adds a single row of output to the EQP result, +** where the caption is of the form: +** +** "USE TEMP B-TREE FOR xxx" +** +** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which +** is determined by the zUsage argument. +*/ +static void explainTempTable(Parse *pParse, const char *zUsage){ + if( pParse->explain==2 ){ + Vdbe *v = pParse->pVdbe; + char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage); + sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); + } +} + +/* +** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function +** is a no-op. Otherwise, it adds a single row of output to the EQP result, +** where the caption is of one of the two forms: +** +** "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)" +** "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)" +** +** where iSub1 and iSub2 are the integers passed as the corresponding +** function parameters, and op is the text representation of the parameter +** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT, +** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is +** false, or the second form if it is true. +*/ +static void explainComposite( + Parse *pParse, /* Parse context */ + int op, /* One of TK_UNION, TK_EXCEPT etc. */ + int iSub1, /* Subquery id 1 */ + int iSub2, /* Subquery id 2 */ + int bUseTmp /* True if a temp table was used */ +){ + assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL ); + if( pParse->explain==2 ){ + Vdbe *v = pParse->pVdbe; + char *zMsg = sqlite3MPrintf( + pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2, + bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op) + ); + sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC); + } +} + +/* +** Assign expression b to lvalue a. A second, no-op, version of this macro +** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code +** in sqlite3Select() to assign values to structure member variables that +** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the +** code with #ifndef directives. +*/ +# define explainSetInteger(a, b) a = b + +#else +/* No-op versions of the explainXXX() functions and macros. */ +# define explainTempTable(y,z) +# define explainComposite(v,w,x,y,z) +# define explainSetInteger(y,z) +#endif + +/* +** If the inner loop was generated using a non-null pOrderBy argument, +** then the results were placed in a sorter. After the loop is terminated +** we need to run the sorter and output the results. The following +** routine generates the code needed to do that. +*/ +static void generateSortTail( + Parse *pParse, /* Parsing context */ + Select *p, /* The SELECT statement */ + Vdbe *v, /* Generate code into this VDBE */ + int nColumn, /* Number of columns of data */ + SelectDest *pDest /* Write the sorted results here */ +){ + int addrBreak = sqlite3VdbeMakeLabel(v); /* Jump here to exit loop */ + int addrContinue = sqlite3VdbeMakeLabel(v); /* Jump here for next cycle */ + int addr; + int iTab; + int pseudoTab = 0; + ExprList *pOrderBy = p->pOrderBy; + + int eDest = pDest->eDest; + int iParm = pDest->iParm; + + int regRow; + int regRowid; + + iTab = pOrderBy->iECursor; + regRow = sqlite3GetTempReg(pParse); + if( eDest==SRT_Output || eDest==SRT_Coroutine ){ + pseudoTab = pParse->nTab++; + sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn); + regRowid = 0; + }else{ + regRowid = sqlite3GetTempReg(pParse); + } + addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak); + codeOffset(v, p, addrContinue); + sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow); + switch( eDest ){ + case SRT_Table: + case SRT_EphemTab: { + testcase( eDest==SRT_Table ); + testcase( eDest==SRT_EphemTab ); + sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid); + sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + break; + } +#ifndef SQLITE_OMIT_SUBQUERY + case SRT_Set: { + assert( nColumn==1 ); + sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid, &p->affinity, 1); + sqlite3ExprCacheAffinityChange(pParse, regRow, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid); + break; + } + case SRT_Mem: { + assert( nColumn==1 ); + sqlite3ExprCodeMove(pParse, regRow, iParm, 1); + /* The LIMIT clause will terminate the loop for us */ + break; + } +#endif + default: { + int i; + assert( eDest==SRT_Output || eDest==SRT_Coroutine ); + testcase( eDest==SRT_Output ); + testcase( eDest==SRT_Coroutine ); + for(i=0; iiMem+i ); + sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iMem+i); + if( i==0 ){ + sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE); + } + } + if( eDest==SRT_Output ){ + sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iMem, nColumn); + sqlite3ExprCacheAffinityChange(pParse, pDest->iMem, nColumn); + }else{ + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm); + } + break; + } + } + sqlite3ReleaseTempReg(pParse, regRow); + sqlite3ReleaseTempReg(pParse, regRowid); + + /* The bottom of the loop + */ + sqlite3VdbeResolveLabel(v, addrContinue); + sqlite3VdbeAddOp2(v, OP_Next, iTab, addr); + sqlite3VdbeResolveLabel(v, addrBreak); + if( eDest==SRT_Output || eDest==SRT_Coroutine ){ + sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0); + } +} + +/* +** Return a pointer to a string containing the 'declaration type' of the +** expression pExpr. The string may be treated as static by the caller. +** +** The declaration type is the exact datatype definition extracted from the +** original CREATE TABLE statement if the expression is a column. The +** declaration type for a ROWID field is INTEGER. Exactly when an expression +** is considered a column can be complex in the presence of subqueries. The +** result-set expression in all of the following SELECT statements is +** considered a column by this function. +** +** SELECT col FROM tbl; +** SELECT (SELECT col FROM tbl; +** SELECT (SELECT col FROM tbl); +** SELECT abc FROM (SELECT col AS abc FROM tbl); +** +** The declaration type for any expression other than a column is NULL. +*/ +static const char *columnType( + NameContext *pNC, + Expr *pExpr, + const char **pzOriginDb, + const char **pzOriginTab, + const char **pzOriginCol +){ + char const *zType = 0; + char const *zOriginDb = 0; + char const *zOriginTab = 0; + char const *zOriginCol = 0; + int j; + if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0; + + switch( pExpr->op ){ + case TK_AGG_COLUMN: + case TK_COLUMN: { + /* The expression is a column. Locate the table the column is being + ** extracted from in NameContext.pSrcList. This table may be real + ** database table or a subquery. + */ + Table *pTab = 0; /* Table structure column is extracted from */ + Select *pS = 0; /* Select the column is extracted from */ + int iCol = pExpr->iColumn; /* Index of column in pTab */ + testcase( pExpr->op==TK_AGG_COLUMN ); + testcase( pExpr->op==TK_COLUMN ); + while( pNC && !pTab ){ + SrcList *pTabList = pNC->pSrcList; + for(j=0;jnSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++); + if( jnSrc ){ + pTab = pTabList->a[j].pTab; + pS = pTabList->a[j].pSelect; + }else{ + pNC = pNC->pNext; + } + } + + if( pTab==0 ){ + /* At one time, code such as "SELECT new.x" within a trigger would + ** cause this condition to run. Since then, we have restructured how + ** trigger code is generated and so this condition is no longer + ** possible. However, it can still be true for statements like + ** the following: + ** + ** CREATE TABLE t1(col INTEGER); + ** SELECT (SELECT t1.col) FROM FROM t1; + ** + ** when columnType() is called on the expression "t1.col" in the + ** sub-select. In this case, set the column type to NULL, even + ** though it should really be "INTEGER". + ** + ** This is not a problem, as the column type of "t1.col" is never + ** used. When columnType() is called on the expression + ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT + ** branch below. */ + break; + } + + assert( pTab && pExpr->pTab==pTab ); + if( pS ){ + /* The "table" is actually a sub-select or a view in the FROM clause + ** of the SELECT statement. Return the declaration type and origin + ** data for the result-set column of the sub-select. + */ + if( iCol>=0 && ALWAYS(iColpEList->nExpr) ){ + /* If iCol is less than zero, then the expression requests the + ** rowid of the sub-select or view. This expression is legal (see + ** test case misc2.2.2) - it always evaluates to NULL. + */ + NameContext sNC; + Expr *p = pS->pEList->a[iCol].pExpr; + sNC.pSrcList = pS->pSrc; + sNC.pNext = pNC; + sNC.pParse = pNC->pParse; + zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); + } + }else if( ALWAYS(pTab->pSchema) ){ + /* A real table */ + assert( !pS ); + if( iCol<0 ) iCol = pTab->iPKey; + assert( iCol==-1 || (iCol>=0 && iColnCol) ); + if( iCol<0 ){ + zType = "INTEGER"; + zOriginCol = "rowid"; + }else{ + zType = pTab->aCol[iCol].zType; + zOriginCol = pTab->aCol[iCol].zName; + } + zOriginTab = pTab->zName; + if( pNC->pParse ){ + int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema); + zOriginDb = pNC->pParse->db->aDb[iDb].zName; + } + } + break; + } +#ifndef SQLITE_OMIT_SUBQUERY + case TK_SELECT: { + /* The expression is a sub-select. Return the declaration type and + ** origin info for the single column in the result set of the SELECT + ** statement. + */ + NameContext sNC; + Select *pS = pExpr->x.pSelect; + Expr *p = pS->pEList->a[0].pExpr; + assert( ExprHasProperty(pExpr, EP_xIsSelect) ); + sNC.pSrcList = pS->pSrc; + sNC.pNext = pNC; + sNC.pParse = pNC->pParse; + zType = columnType(&sNC, p, &zOriginDb, &zOriginTab, &zOriginCol); + break; + } +#endif + } + + if( pzOriginDb ){ + assert( pzOriginTab && pzOriginCol ); + *pzOriginDb = zOriginDb; + *pzOriginTab = zOriginTab; + *pzOriginCol = zOriginCol; + } + return zType; +} + +/* +** Generate code that will tell the VDBE the declaration types of columns +** in the result set. +*/ +static void generateColumnTypes( + Parse *pParse, /* Parser context */ + SrcList *pTabList, /* List of tables */ + ExprList *pEList /* Expressions defining the result set */ +){ +#ifndef SQLITE_OMIT_DECLTYPE + Vdbe *v = pParse->pVdbe; + int i; + NameContext sNC; + sNC.pSrcList = pTabList; + sNC.pParse = pParse; + for(i=0; inExpr; i++){ + Expr *p = pEList->a[i].pExpr; + const char *zType; +#ifdef SQLITE_ENABLE_COLUMN_METADATA + const char *zOrigDb = 0; + const char *zOrigTab = 0; + const char *zOrigCol = 0; + zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol); + + /* The vdbe must make its own copy of the column-type and other + ** column specific strings, in case the schema is reset before this + ** virtual machine is deleted. + */ + sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT); + sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT); + sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT); +#else + zType = columnType(&sNC, p, 0, 0, 0); +#endif + sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT); + } +#endif /* SQLITE_OMIT_DECLTYPE */ +} + +/* +** Generate code that will tell the VDBE the names of columns +** in the result set. This information is used to provide the +** azCol[] values in the callback. +*/ +static void generateColumnNames( + Parse *pParse, /* Parser context */ + SrcList *pTabList, /* List of tables */ + ExprList *pEList /* Expressions defining the result set */ +){ + Vdbe *v = pParse->pVdbe; + int i, j; + sqlite3 *db = pParse->db; + int fullNames, shortNames; + +#ifndef SQLITE_OMIT_EXPLAIN + /* If this is an EXPLAIN, skip this step */ + if( pParse->explain ){ + return; + } +#endif + + if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return; + pParse->colNamesSet = 1; + fullNames = (db->flags & SQLITE_FullColNames)!=0; + shortNames = (db->flags & SQLITE_ShortColNames)!=0; + sqlite3VdbeSetNumCols(v, pEList->nExpr); + for(i=0; inExpr; i++){ + Expr *p; + p = pEList->a[i].pExpr; + if( NEVER(p==0) ) continue; + if( pEList->a[i].zName ){ + char *zName = pEList->a[i].zName; + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT); + }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){ + Table *pTab; + char *zCol; + int iCol = p->iColumn; + for(j=0; ALWAYS(jnSrc); j++){ + if( pTabList->a[j].iCursor==p->iTable ) break; + } + assert( jnSrc ); + pTab = pTabList->a[j].pTab; + if( iCol<0 ) iCol = pTab->iPKey; + assert( iCol==-1 || (iCol>=0 && iColnCol) ); + if( iCol<0 ){ + zCol = "rowid"; + }else{ + zCol = pTab->aCol[iCol].zName; + } + if( !shortNames && !fullNames ){ + sqlite3VdbeSetColName(v, i, COLNAME_NAME, + sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC); + }else if( fullNames ){ + char *zName = 0; + zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol); + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC); + }else{ + sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT); + } + }else{ + sqlite3VdbeSetColName(v, i, COLNAME_NAME, + sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC); + } + } + generateColumnTypes(pParse, pTabList, pEList); +} + +/* +** Given a an expression list (which is really the list of expressions +** that form the result set of a SELECT statement) compute appropriate +** column names for a table that would hold the expression list. +** +** All column names will be unique. +** +** Only the column names are computed. Column.zType, Column.zColl, +** and other fields of Column are zeroed. +** +** Return SQLITE_OK on success. If a memory allocation error occurs, +** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM. +*/ +static int selectColumnsFromExprList( + Parse *pParse, /* Parsing context */ + ExprList *pEList, /* Expr list from which to derive column names */ + int *pnCol, /* Write the number of columns here */ + Column **paCol /* Write the new column list here */ +){ + sqlite3 *db = pParse->db; /* Database connection */ + int i, j; /* Loop counters */ + int cnt; /* Index added to make the name unique */ + Column *aCol, *pCol; /* For looping over result columns */ + int nCol; /* Number of columns in the result set */ + Expr *p; /* Expression for a single result column */ + char *zName; /* Column name */ + int nName; /* Size of name in zName[] */ + + *pnCol = nCol = pEList->nExpr; + aCol = *paCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol); + if( aCol==0 ) return SQLITE_NOMEM; + for(i=0, pCol=aCol; ia[i].pExpr; + assert( p->pRight==0 || ExprHasProperty(p->pRight, EP_IntValue) + || p->pRight->u.zToken==0 || p->pRight->u.zToken[0]!=0 ); + if( (zName = pEList->a[i].zName)!=0 ){ + /* If the column contains an "AS " phrase, use as the name */ + zName = sqlite3DbStrDup(db, zName); + }else{ + Expr *pColExpr = p; /* The expression that is the result column name */ + Table *pTab; /* Table associated with this expression */ + while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight; + if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){ + /* For columns use the column name name */ + int iCol = pColExpr->iColumn; + pTab = pColExpr->pTab; + if( iCol<0 ) iCol = pTab->iPKey; + zName = sqlite3MPrintf(db, "%s", + iCol>=0 ? pTab->aCol[iCol].zName : "rowid"); + }else if( pColExpr->op==TK_ID ){ + assert( !ExprHasProperty(pColExpr, EP_IntValue) ); + zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken); + }else{ + /* Use the original text of the column expression as its name */ + zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan); + } + } + if( db->mallocFailed ){ + sqlite3DbFree(db, zName); + break; + } + + /* Make sure the column name is unique. If the name is not unique, + ** append a integer to the name so that it becomes unique. + */ + nName = sqlite3Strlen30(zName); + for(j=cnt=0; jzName = zName; + } + if( db->mallocFailed ){ + for(j=0; jdb; + NameContext sNC; + Column *pCol; + CollSeq *pColl; + int i; + Expr *p; + struct ExprList_item *a; + + assert( pSelect!=0 ); + assert( (pSelect->selFlags & SF_Resolved)!=0 ); + assert( nCol==pSelect->pEList->nExpr || db->mallocFailed ); + if( db->mallocFailed ) return; + memset(&sNC, 0, sizeof(sNC)); + sNC.pSrcList = pSelect->pSrc; + a = pSelect->pEList->a; + for(i=0, pCol=aCol; izType = sqlite3DbStrDup(db, columnType(&sNC, p, 0, 0, 0)); + pCol->affinity = sqlite3ExprAffinity(p); + if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE; + pColl = sqlite3ExprCollSeq(pParse, p); + if( pColl ){ + pCol->zColl = sqlite3DbStrDup(db, pColl->zName); + } + } +} + +/* +** Given a SELECT statement, generate a Table structure that describes +** the result set of that SELECT. +*/ +SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){ + Table *pTab; + sqlite3 *db = pParse->db; + int savedFlags; + + savedFlags = db->flags; + db->flags &= ~SQLITE_FullColNames; + db->flags |= SQLITE_ShortColNames; + sqlite3SelectPrep(pParse, pSelect, 0); + if( pParse->nErr ) return 0; + while( pSelect->pPrior ) pSelect = pSelect->pPrior; + db->flags = savedFlags; + pTab = sqlite3DbMallocZero(db, sizeof(Table) ); + if( pTab==0 ){ + return 0; + } + /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside + ** is disabled */ + assert( db->lookaside.bEnabled==0 ); + pTab->nRef = 1; + pTab->zName = 0; + pTab->nRowEst = 1000000; + selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol); + selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSelect); + pTab->iPKey = -1; + if( db->mallocFailed ){ + sqlite3DeleteTable(db, pTab); + return 0; + } + return pTab; +} + +/* +** Get a VDBE for the given parser context. Create a new one if necessary. +** If an error occurs, return NULL and leave a message in pParse. +*/ +SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){ + Vdbe *v = pParse->pVdbe; + if( v==0 ){ + v = pParse->pVdbe = sqlite3VdbeCreate(pParse->db); +#ifndef SQLITE_OMIT_TRACE + if( v ){ + sqlite3VdbeAddOp0(v, OP_Trace); + } +#endif + } + return v; +} + + +/* +** Compute the iLimit and iOffset fields of the SELECT based on the +** pLimit and pOffset expressions. pLimit and pOffset hold the expressions +** that appear in the original SQL statement after the LIMIT and OFFSET +** keywords. Or NULL if those keywords are omitted. iLimit and iOffset +** are the integer memory register numbers for counters used to compute +** the limit and offset. If there is no limit and/or offset, then +** iLimit and iOffset are negative. +** +** This routine changes the values of iLimit and iOffset only if +** a limit or offset is defined by pLimit and pOffset. iLimit and +** iOffset should have been preset to appropriate default values +** (usually but not always -1) prior to calling this routine. +** Only if pLimit!=0 or pOffset!=0 do the limit registers get +** redefined. The UNION ALL operator uses this property to force +** the reuse of the same limit and offset registers across multiple +** SELECT statements. +*/ +static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){ + Vdbe *v = 0; + int iLimit = 0; + int iOffset; + int addr1, n; + if( p->iLimit ) return; + + /* + ** "LIMIT -1" always shows all rows. There is some + ** contraversy about what the correct behavior should be. + ** The current implementation interprets "LIMIT 0" to mean + ** no rows. + */ + sqlite3ExprCacheClear(pParse); + assert( p->pOffset==0 || p->pLimit!=0 ); + if( p->pLimit ){ + p->iLimit = iLimit = ++pParse->nMem; + v = sqlite3GetVdbe(pParse); + if( NEVER(v==0) ) return; /* VDBE should have already been allocated */ + if( sqlite3ExprIsInteger(p->pLimit, &n) ){ + sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit); + VdbeComment((v, "LIMIT counter")); + if( n==0 ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak); + }else{ + if( p->nSelectRow > (double)n ) p->nSelectRow = (double)n; + } + }else{ + sqlite3ExprCode(pParse, p->pLimit, iLimit); + sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit); + VdbeComment((v, "LIMIT counter")); + sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak); + } + if( p->pOffset ){ + p->iOffset = iOffset = ++pParse->nMem; + pParse->nMem++; /* Allocate an extra register for limit+offset */ + sqlite3ExprCode(pParse, p->pOffset, iOffset); + sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); + VdbeComment((v, "OFFSET counter")); + addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset); + sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset); + sqlite3VdbeJumpHere(v, addr1); + sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1); + VdbeComment((v, "LIMIT+OFFSET")); + addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit); + sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1); + sqlite3VdbeJumpHere(v, addr1); + } + } +} + +#ifndef SQLITE_OMIT_COMPOUND_SELECT +/* +** Return the appropriate collating sequence for the iCol-th column of +** the result set for the compound-select statement "p". Return NULL if +** the column has no default collating sequence. +** +** The collating sequence for the compound select is taken from the +** left-most term of the select that has a collating sequence. +*/ +static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){ + CollSeq *pRet; + if( p->pPrior ){ + pRet = multiSelectCollSeq(pParse, p->pPrior, iCol); + }else{ + pRet = 0; + } + assert( iCol>=0 ); + if( pRet==0 && iColpEList->nExpr ){ + pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); + } + return pRet; +} +#endif /* SQLITE_OMIT_COMPOUND_SELECT */ + +/* Forward reference */ +static int multiSelectOrderBy( + Parse *pParse, /* Parsing context */ + Select *p, /* The right-most of SELECTs to be coded */ + SelectDest *pDest /* What to do with query results */ +); + + +#ifndef SQLITE_OMIT_COMPOUND_SELECT +/* +** This routine is called to process a compound query form from +** two or more separate queries using UNION, UNION ALL, EXCEPT, or +** INTERSECT +** +** "p" points to the right-most of the two queries. the query on the +** left is p->pPrior. The left query could also be a compound query +** in which case this routine will be called recursively. +** +** The results of the total query are to be written into a destination +** of type eDest with parameter iParm. +** +** Example 1: Consider a three-way compound SQL statement. +** +** SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3 +** +** This statement is parsed up as follows: +** +** SELECT c FROM t3 +** | +** `-----> SELECT b FROM t2 +** | +** `------> SELECT a FROM t1 +** +** The arrows in the diagram above represent the Select.pPrior pointer. +** So if this routine is called with p equal to the t3 query, then +** pPrior will be the t2 query. p->op will be TK_UNION in this case. +** +** Notice that because of the way SQLite parses compound SELECTs, the +** individual selects always group from left to right. +*/ +static int multiSelect( + Parse *pParse, /* Parsing context */ + Select *p, /* The right-most of SELECTs to be coded */ + SelectDest *pDest /* What to do with query results */ +){ + int rc = SQLITE_OK; /* Success code from a subroutine */ + Select *pPrior; /* Another SELECT immediately to our left */ + Vdbe *v; /* Generate code to this VDBE */ + SelectDest dest; /* Alternative data destination */ + Select *pDelete = 0; /* Chain of simple selects to delete */ + sqlite3 *db; /* Database connection */ +#ifndef SQLITE_OMIT_EXPLAIN + int iSub1; /* EQP id of left-hand query */ + int iSub2; /* EQP id of right-hand query */ +#endif + + /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs. Only + ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT. + */ + assert( p && p->pPrior ); /* Calling function guarantees this much */ + db = pParse->db; + pPrior = p->pPrior; + assert( pPrior->pRightmost!=pPrior ); + assert( pPrior->pRightmost==p->pRightmost ); + dest = *pDest; + if( pPrior->pOrderBy ){ + sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before", + selectOpName(p->op)); + rc = 1; + goto multi_select_end; + } + if( pPrior->pLimit ){ + sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before", + selectOpName(p->op)); + rc = 1; + goto multi_select_end; + } + + v = sqlite3GetVdbe(pParse); + assert( v!=0 ); /* The VDBE already created by calling function */ + + /* Create the destination temporary table if necessary + */ + if( dest.eDest==SRT_EphemTab ){ + assert( p->pEList ); + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iParm, p->pEList->nExpr); + sqlite3VdbeChangeP5(v, BTREE_UNORDERED); + dest.eDest = SRT_Table; + } + + /* Make sure all SELECTs in the statement have the same number of elements + ** in their result sets. + */ + assert( p->pEList && pPrior->pEList ); + if( p->pEList->nExpr!=pPrior->pEList->nExpr ){ + sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s" + " do not have the same number of result columns", selectOpName(p->op)); + rc = 1; + goto multi_select_end; + } + + /* Compound SELECTs that have an ORDER BY clause are handled separately. + */ + if( p->pOrderBy ){ + return multiSelectOrderBy(pParse, p, pDest); + } + + /* Generate code for the left and right SELECT statements. + */ + switch( p->op ){ + case TK_ALL: { + int addr = 0; + int nLimit; + assert( !pPrior->pLimit ); + pPrior->pLimit = p->pLimit; + pPrior->pOffset = p->pOffset; + explainSetInteger(iSub1, pParse->iNextSelectId); + rc = sqlite3Select(pParse, pPrior, &dest); + p->pLimit = 0; + p->pOffset = 0; + if( rc ){ + goto multi_select_end; + } + p->pPrior = 0; + p->iLimit = pPrior->iLimit; + p->iOffset = pPrior->iOffset; + if( p->iLimit ){ + addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit); + VdbeComment((v, "Jump ahead if LIMIT reached")); + } + explainSetInteger(iSub2, pParse->iNextSelectId); + rc = sqlite3Select(pParse, p, &dest); + testcase( rc!=SQLITE_OK ); + pDelete = p->pPrior; + p->pPrior = pPrior; + p->nSelectRow += pPrior->nSelectRow; + if( pPrior->pLimit + && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit) + && p->nSelectRow > (double)nLimit + ){ + p->nSelectRow = (double)nLimit; + } + if( addr ){ + sqlite3VdbeJumpHere(v, addr); + } + break; + } + case TK_EXCEPT: + case TK_UNION: { + int unionTab; /* Cursor number of the temporary table holding result */ + u8 op = 0; /* One of the SRT_ operations to apply to self */ + int priorOp; /* The SRT_ operation to apply to prior selects */ + Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */ + int addr; + SelectDest uniondest; + + testcase( p->op==TK_EXCEPT ); + testcase( p->op==TK_UNION ); + priorOp = SRT_Union; + if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){ + /* We can reuse a temporary table generated by a SELECT to our + ** right. + */ + assert( p->pRightmost!=p ); /* Can only happen for leftward elements + ** of a 3-way or more compound */ + assert( p->pLimit==0 ); /* Not allowed on leftward elements */ + assert( p->pOffset==0 ); /* Not allowed on leftward elements */ + unionTab = dest.iParm; + }else{ + /* We will need to create our own temporary table to hold the + ** intermediate results. + */ + unionTab = pParse->nTab++; + assert( p->pOrderBy==0 ); + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0); + assert( p->addrOpenEphm[0] == -1 ); + p->addrOpenEphm[0] = addr; + p->pRightmost->selFlags |= SF_UsesEphemeral; + assert( p->pEList ); + } + + /* Code the SELECT statements to our left + */ + assert( !pPrior->pOrderBy ); + sqlite3SelectDestInit(&uniondest, priorOp, unionTab); + explainSetInteger(iSub1, pParse->iNextSelectId); + rc = sqlite3Select(pParse, pPrior, &uniondest); + if( rc ){ + goto multi_select_end; + } + + /* Code the current SELECT statement + */ + if( p->op==TK_EXCEPT ){ + op = SRT_Except; + }else{ + assert( p->op==TK_UNION ); + op = SRT_Union; + } + p->pPrior = 0; + pLimit = p->pLimit; + p->pLimit = 0; + pOffset = p->pOffset; + p->pOffset = 0; + uniondest.eDest = op; + explainSetInteger(iSub2, pParse->iNextSelectId); + rc = sqlite3Select(pParse, p, &uniondest); + testcase( rc!=SQLITE_OK ); + /* Query flattening in sqlite3Select() might refill p->pOrderBy. + ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */ + sqlite3ExprListDelete(db, p->pOrderBy); + pDelete = p->pPrior; + p->pPrior = pPrior; + p->pOrderBy = 0; + if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow; + sqlite3ExprDelete(db, p->pLimit); + p->pLimit = pLimit; + p->pOffset = pOffset; + p->iLimit = 0; + p->iOffset = 0; + + /* Convert the data in the temporary table into whatever form + ** it is that we currently need. + */ + assert( unionTab==dest.iParm || dest.eDest!=priorOp ); + if( dest.eDest!=priorOp ){ + int iCont, iBreak, iStart; + assert( p->pEList ); + if( dest.eDest==SRT_Output ){ + Select *pFirst = p; + while( pFirst->pPrior ) pFirst = pFirst->pPrior; + generateColumnNames(pParse, 0, pFirst->pEList); + } + iBreak = sqlite3VdbeMakeLabel(v); + iCont = sqlite3VdbeMakeLabel(v); + computeLimitRegisters(pParse, p, iBreak); + sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak); + iStart = sqlite3VdbeCurrentAddr(v); + selectInnerLoop(pParse, p, p->pEList, unionTab, p->pEList->nExpr, + 0, -1, &dest, iCont, iBreak); + sqlite3VdbeResolveLabel(v, iCont); + sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart); + sqlite3VdbeResolveLabel(v, iBreak); + sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0); + } + break; + } + default: assert( p->op==TK_INTERSECT ); { + int tab1, tab2; + int iCont, iBreak, iStart; + Expr *pLimit, *pOffset; + int addr; + SelectDest intersectdest; + int r1; + + /* INTERSECT is different from the others since it requires + ** two temporary tables. Hence it has its own case. Begin + ** by allocating the tables we will need. + */ + tab1 = pParse->nTab++; + tab2 = pParse->nTab++; + assert( p->pOrderBy==0 ); + + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0); + assert( p->addrOpenEphm[0] == -1 ); + p->addrOpenEphm[0] = addr; + p->pRightmost->selFlags |= SF_UsesEphemeral; + assert( p->pEList ); + + /* Code the SELECTs to our left into temporary table "tab1". + */ + sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1); + explainSetInteger(iSub1, pParse->iNextSelectId); + rc = sqlite3Select(pParse, pPrior, &intersectdest); + if( rc ){ + goto multi_select_end; + } + + /* Code the current SELECT into temporary table "tab2" + */ + addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0); + assert( p->addrOpenEphm[1] == -1 ); + p->addrOpenEphm[1] = addr; + p->pPrior = 0; + pLimit = p->pLimit; + p->pLimit = 0; + pOffset = p->pOffset; + p->pOffset = 0; + intersectdest.iParm = tab2; + explainSetInteger(iSub2, pParse->iNextSelectId); + rc = sqlite3Select(pParse, p, &intersectdest); + testcase( rc!=SQLITE_OK ); + pDelete = p->pPrior; + p->pPrior = pPrior; + if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; + sqlite3ExprDelete(db, p->pLimit); + p->pLimit = pLimit; + p->pOffset = pOffset; + + /* Generate code to take the intersection of the two temporary + ** tables. + */ + assert( p->pEList ); + if( dest.eDest==SRT_Output ){ + Select *pFirst = p; + while( pFirst->pPrior ) pFirst = pFirst->pPrior; + generateColumnNames(pParse, 0, pFirst->pEList); + } + iBreak = sqlite3VdbeMakeLabel(v); + iCont = sqlite3VdbeMakeLabel(v); + computeLimitRegisters(pParse, p, iBreak); + sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); + r1 = sqlite3GetTempReg(pParse); + iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1); + sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0); + sqlite3ReleaseTempReg(pParse, r1); + selectInnerLoop(pParse, p, p->pEList, tab1, p->pEList->nExpr, + 0, -1, &dest, iCont, iBreak); + sqlite3VdbeResolveLabel(v, iCont); + sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); + sqlite3VdbeResolveLabel(v, iBreak); + sqlite3VdbeAddOp2(v, OP_Close, tab2, 0); + sqlite3VdbeAddOp2(v, OP_Close, tab1, 0); + break; + } + } + + explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL); + + /* Compute collating sequences used by + ** temporary tables needed to implement the compound select. + ** Attach the KeyInfo structure to all temporary tables. + ** + ** This section is run by the right-most SELECT statement only. + ** SELECT statements to the left always skip this part. The right-most + ** SELECT might also skip this part if it has no ORDER BY clause and + ** no temp tables are required. + */ + if( p->selFlags & SF_UsesEphemeral ){ + int i; /* Loop counter */ + KeyInfo *pKeyInfo; /* Collating sequence for the result set */ + Select *pLoop; /* For looping through SELECT statements */ + CollSeq **apColl; /* For looping through pKeyInfo->aColl[] */ + int nCol; /* Number of columns in result set */ + + assert( p->pRightmost==p ); + nCol = p->pEList->nExpr; + pKeyInfo = sqlite3DbMallocZero(db, + sizeof(*pKeyInfo)+nCol*(sizeof(CollSeq*) + 1)); + if( !pKeyInfo ){ + rc = SQLITE_NOMEM; + goto multi_select_end; + } + + pKeyInfo->enc = ENC(db); + pKeyInfo->nField = (u16)nCol; + + for(i=0, apColl=pKeyInfo->aColl; ipDfltColl; + } + } + + for(pLoop=p; pLoop; pLoop=pLoop->pPrior){ + for(i=0; i<2; i++){ + int addr = pLoop->addrOpenEphm[i]; + if( addr<0 ){ + /* If [0] is unused then [1] is also unused. So we can + ** always safely abort as soon as the first unused slot is found */ + assert( pLoop->addrOpenEphm[1]<0 ); + break; + } + sqlite3VdbeChangeP2(v, addr, nCol); + sqlite3VdbeChangeP4(v, addr, (char*)pKeyInfo, P4_KEYINFO); + pLoop->addrOpenEphm[i] = -1; + } + } + sqlite3DbFree(db, pKeyInfo); + } + +multi_select_end: + pDest->iMem = dest.iMem; + pDest->nMem = dest.nMem; + sqlite3SelectDelete(db, pDelete); + return rc; +} +#endif /* SQLITE_OMIT_COMPOUND_SELECT */ + +/* +** Code an output subroutine for a coroutine implementation of a +** SELECT statment. +** +** The data to be output is contained in pIn->iMem. There are +** pIn->nMem columns to be output. pDest is where the output should +** be sent. +** +** regReturn is the number of the register holding the subroutine +** return address. +** +** If regPrev>0 then it is the first register in a vector that +** records the previous output. mem[regPrev] is a flag that is false +** if there has been no previous output. If regPrev>0 then code is +** generated to suppress duplicates. pKeyInfo is used for comparing +** keys. +** +** If the LIMIT found in p->iLimit is reached, jump immediately to +** iBreak. +*/ +static int generateOutputSubroutine( + Parse *pParse, /* Parsing context */ + Select *p, /* The SELECT statement */ + SelectDest *pIn, /* Coroutine supplying data */ + SelectDest *pDest, /* Where to send the data */ + int regReturn, /* The return address register */ + int regPrev, /* Previous result register. No uniqueness if 0 */ + KeyInfo *pKeyInfo, /* For comparing with previous entry */ + int p4type, /* The p4 type for pKeyInfo */ + int iBreak /* Jump here if we hit the LIMIT */ +){ + Vdbe *v = pParse->pVdbe; + int iContinue; + int addr; + + addr = sqlite3VdbeCurrentAddr(v); + iContinue = sqlite3VdbeMakeLabel(v); + + /* Suppress duplicates for UNION, EXCEPT, and INTERSECT + */ + if( regPrev ){ + int j1, j2; + j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev); + j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iMem, regPrev+1, pIn->nMem, + (char*)pKeyInfo, p4type); + sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2); + sqlite3VdbeJumpHere(v, j1); + sqlite3ExprCodeCopy(pParse, pIn->iMem, regPrev+1, pIn->nMem); + sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev); + } + if( pParse->db->mallocFailed ) return 0; + + /* Suppress the the first OFFSET entries if there is an OFFSET clause + */ + codeOffset(v, p, iContinue); + + switch( pDest->eDest ){ + /* Store the result as data using a unique key. + */ + case SRT_Table: + case SRT_EphemTab: { + int r1 = sqlite3GetTempReg(pParse); + int r2 = sqlite3GetTempReg(pParse); + testcase( pDest->eDest==SRT_Table ); + testcase( pDest->eDest==SRT_EphemTab ); + sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iMem, pIn->nMem, r1); + sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iParm, r2); + sqlite3VdbeAddOp3(v, OP_Insert, pDest->iParm, r1, r2); + sqlite3VdbeChangeP5(v, OPFLAG_APPEND); + sqlite3ReleaseTempReg(pParse, r2); + sqlite3ReleaseTempReg(pParse, r1); + break; + } + +#ifndef SQLITE_OMIT_SUBQUERY + /* If we are creating a set for an "expr IN (SELECT ...)" construct, + ** then there should be a single item on the stack. Write this + ** item into the set table with bogus data. + */ + case SRT_Set: { + int r1; + assert( pIn->nMem==1 ); + p->affinity = + sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affinity); + r1 = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iMem, 1, r1, &p->affinity, 1); + sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iParm, r1); + sqlite3ReleaseTempReg(pParse, r1); + break; + } + +#if 0 /* Never occurs on an ORDER BY query */ + /* If any row exist in the result set, record that fact and abort. + */ + case SRT_Exists: { + sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iParm); + /* The LIMIT clause will terminate the loop for us */ + break; + } +#endif + + /* If this is a scalar select that is part of an expression, then + ** store the results in the appropriate memory cell and break out + ** of the scan loop. + */ + case SRT_Mem: { + assert( pIn->nMem==1 ); + sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iParm, 1); + /* The LIMIT clause will jump out of the loop for us */ + break; + } +#endif /* #ifndef SQLITE_OMIT_SUBQUERY */ + + /* The results are stored in a sequence of registers + ** starting at pDest->iMem. Then the co-routine yields. + */ + case SRT_Coroutine: { + if( pDest->iMem==0 ){ + pDest->iMem = sqlite3GetTempRange(pParse, pIn->nMem); + pDest->nMem = pIn->nMem; + } + sqlite3ExprCodeMove(pParse, pIn->iMem, pDest->iMem, pDest->nMem); + sqlite3VdbeAddOp1(v, OP_Yield, pDest->iParm); + break; + } + + /* If none of the above, then the result destination must be + ** SRT_Output. This routine is never called with any other + ** destination other than the ones handled above or SRT_Output. + ** + ** For SRT_Output, results are stored in a sequence of registers. + ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to + ** return the next row of result. + */ + default: { + assert( pDest->eDest==SRT_Output ); + sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iMem, pIn->nMem); + sqlite3ExprCacheAffinityChange(pParse, pIn->iMem, pIn->nMem); + break; + } + } + + /* Jump to the end of the loop if the LIMIT is reached. + */ + if( p->iLimit ){ + sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1); + } + + /* Generate the subroutine return + */ + sqlite3VdbeResolveLabel(v, iContinue); + sqlite3VdbeAddOp1(v, OP_Return, regReturn); + + return addr; +} + +/* +** Alternative compound select code generator for cases when there +** is an ORDER BY clause. +** +** We assume a query of the following form: +** +** ORDER BY +** +** is one of UNION ALL, UNION, EXCEPT, or INTERSECT. The idea +** is to code both and with the ORDER BY clause as +** co-routines. Then run the co-routines in parallel and merge the results +** into the output. In addition to the two coroutines (called selectA and +** selectB) there are 7 subroutines: +** +** outA: Move the output of the selectA coroutine into the output +** of the compound query. +** +** outB: Move the output of the selectB coroutine into the output +** of the compound query. (Only generated for UNION and +** UNION ALL. EXCEPT and INSERTSECT never output a row that +** appears only in B.) +** +** AltB: Called when there is data from both coroutines and AB. +** +** EofA: Called when data is exhausted from selectA. +** +** EofB: Called when data is exhausted from selectB. +** +** The implementation of the latter five subroutines depend on which +** is used: +** +** +** UNION ALL UNION EXCEPT INTERSECT +** ------------- ----------------- -------------- ----------------- +** AltB: outA, nextA outA, nextA outA, nextA nextA +** +** AeqB: outA, nextA nextA nextA outA, nextA +** +** AgtB: outB, nextB outB, nextB nextB nextB +** +** EofA: outB, nextB outB, nextB halt halt +** +** EofB: outA, nextA outA, nextA outA, nextA halt +** +** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA +** causes an immediate jump to EofA and an EOF on B following nextB causes +** an immediate jump to EofB. Within EofA and EofB, and EOF on entry or +** following nextX causes a jump to the end of the select processing. +** +** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled +** within the output subroutine. The regPrev register set holds the previously +** output value. A comparison is made against this value and the output +** is skipped if the next results would be the same as the previous. +** +** The implementation plan is to implement the two coroutines and seven +** subroutines first, then put the control logic at the bottom. Like this: +** +** goto Init +** coA: coroutine for left query (A) +** coB: coroutine for right query (B) +** outA: output one row of A +** outB: output one row of B (UNION and UNION ALL only) +** EofA: ... +** EofB: ... +** AltB: ... +** AeqB: ... +** AgtB: ... +** Init: initialize coroutine registers +** yield coA +** if eof(A) goto EofA +** yield coB +** if eof(B) goto EofB +** Cmpr: Compare A, B +** Jump AltB, AeqB, AgtB +** End: ... +** +** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not +** actually called using Gosub and they do not Return. EofA and EofB loop +** until all data is exhausted then jump to the "end" labe. AltB, AeqB, +** and AgtB jump to either L2 or to one of EofA or EofB. +*/ +#ifndef SQLITE_OMIT_COMPOUND_SELECT +static int multiSelectOrderBy( + Parse *pParse, /* Parsing context */ + Select *p, /* The right-most of SELECTs to be coded */ + SelectDest *pDest /* What to do with query results */ +){ + int i, j; /* Loop counters */ + Select *pPrior; /* Another SELECT immediately to our left */ + Vdbe *v; /* Generate code to this VDBE */ + SelectDest destA; /* Destination for coroutine A */ + SelectDest destB; /* Destination for coroutine B */ + int regAddrA; /* Address register for select-A coroutine */ + int regEofA; /* Flag to indicate when select-A is complete */ + int regAddrB; /* Address register for select-B coroutine */ + int regEofB; /* Flag to indicate when select-B is complete */ + int addrSelectA; /* Address of the select-A coroutine */ + int addrSelectB; /* Address of the select-B coroutine */ + int regOutA; /* Address register for the output-A subroutine */ + int regOutB; /* Address register for the output-B subroutine */ + int addrOutA; /* Address of the output-A subroutine */ + int addrOutB = 0; /* Address of the output-B subroutine */ + int addrEofA; /* Address of the select-A-exhausted subroutine */ + int addrEofB; /* Address of the select-B-exhausted subroutine */ + int addrAltB; /* Address of the AB subroutine */ + int regLimitA; /* Limit register for select-A */ + int regLimitB; /* Limit register for select-A */ + int regPrev; /* A range of registers to hold previous output */ + int savedLimit; /* Saved value of p->iLimit */ + int savedOffset; /* Saved value of p->iOffset */ + int labelCmpr; /* Label for the start of the merge algorithm */ + int labelEnd; /* Label for the end of the overall SELECT stmt */ + int j1; /* Jump instructions that get retargetted */ + int op; /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */ + KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */ + KeyInfo *pKeyMerge; /* Comparison information for merging rows */ + sqlite3 *db; /* Database connection */ + ExprList *pOrderBy; /* The ORDER BY clause */ + int nOrderBy; /* Number of terms in the ORDER BY clause */ + int *aPermute; /* Mapping from ORDER BY terms to result set columns */ +#ifndef SQLITE_OMIT_EXPLAIN + int iSub1; /* EQP id of left-hand query */ + int iSub2; /* EQP id of right-hand query */ +#endif + + assert( p->pOrderBy!=0 ); + assert( pKeyDup==0 ); /* "Managed" code needs this. Ticket #3382. */ + db = pParse->db; + v = pParse->pVdbe; + assert( v!=0 ); /* Already thrown the error if VDBE alloc failed */ + labelEnd = sqlite3VdbeMakeLabel(v); + labelCmpr = sqlite3VdbeMakeLabel(v); + + + /* Patch up the ORDER BY clause + */ + op = p->op; + pPrior = p->pPrior; + assert( pPrior->pOrderBy==0 ); + pOrderBy = p->pOrderBy; + assert( pOrderBy ); + nOrderBy = pOrderBy->nExpr; + + /* For operators other than UNION ALL we have to make sure that + ** the ORDER BY clause covers every term of the result set. Add + ** terms to the ORDER BY clause as necessary. + */ + if( op!=TK_ALL ){ + for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){ + struct ExprList_item *pItem; + for(j=0, pItem=pOrderBy->a; jiCol>0 ); + if( pItem->iCol==i ) break; + } + if( j==nOrderBy ){ + Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0); + if( pNew==0 ) return SQLITE_NOMEM; + pNew->flags |= EP_IntValue; + pNew->u.iValue = i; + pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew); + pOrderBy->a[nOrderBy++].iCol = (u16)i; + } + } + } + + /* Compute the comparison permutation and keyinfo that is used with + ** the permutation used to determine if the next + ** row of results comes from selectA or selectB. Also add explicit + ** collations to the ORDER BY clause terms so that when the subqueries + ** to the right and the left are evaluated, they use the correct + ** collation. + */ + aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy); + if( aPermute ){ + struct ExprList_item *pItem; + for(i=0, pItem=pOrderBy->a; iiCol>0 && pItem->iCol<=p->pEList->nExpr ); + aPermute[i] = pItem->iCol - 1; + } + pKeyMerge = + sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1)); + if( pKeyMerge ){ + pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy]; + pKeyMerge->nField = (u16)nOrderBy; + pKeyMerge->enc = ENC(db); + for(i=0; ia[i].pExpr; + if( pTerm->flags & EP_ExpCollate ){ + pColl = pTerm->pColl; + }else{ + pColl = multiSelectCollSeq(pParse, p, aPermute[i]); + pTerm->flags |= EP_ExpCollate; + pTerm->pColl = pColl; + } + pKeyMerge->aColl[i] = pColl; + pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder; + } + } + }else{ + pKeyMerge = 0; + } + + /* Reattach the ORDER BY clause to the query. + */ + p->pOrderBy = pOrderBy; + pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0); + + /* Allocate a range of temporary registers and the KeyInfo needed + ** for the logic that removes duplicate result rows when the + ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL). + */ + if( op==TK_ALL ){ + regPrev = 0; + }else{ + int nExpr = p->pEList->nExpr; + assert( nOrderBy>=nExpr || db->mallocFailed ); + regPrev = sqlite3GetTempRange(pParse, nExpr+1); + sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev); + pKeyDup = sqlite3DbMallocZero(db, + sizeof(*pKeyDup) + nExpr*(sizeof(CollSeq*)+1) ); + if( pKeyDup ){ + pKeyDup->aSortOrder = (u8*)&pKeyDup->aColl[nExpr]; + pKeyDup->nField = (u16)nExpr; + pKeyDup->enc = ENC(db); + for(i=0; iaColl[i] = multiSelectCollSeq(pParse, p, i); + pKeyDup->aSortOrder[i] = 0; + } + } + } + + /* Separate the left and the right query from one another + */ + p->pPrior = 0; + sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER"); + if( pPrior->pPrior==0 ){ + sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER"); + } + + /* Compute the limit registers */ + computeLimitRegisters(pParse, p, labelEnd); + if( p->iLimit && op==TK_ALL ){ + regLimitA = ++pParse->nMem; + regLimitB = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit, + regLimitA); + sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB); + }else{ + regLimitA = regLimitB = 0; + } + sqlite3ExprDelete(db, p->pLimit); + p->pLimit = 0; + sqlite3ExprDelete(db, p->pOffset); + p->pOffset = 0; + + regAddrA = ++pParse->nMem; + regEofA = ++pParse->nMem; + regAddrB = ++pParse->nMem; + regEofB = ++pParse->nMem; + regOutA = ++pParse->nMem; + regOutB = ++pParse->nMem; + sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA); + sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB); + + /* Jump past the various subroutines and coroutines to the main + ** merge loop + */ + j1 = sqlite3VdbeAddOp0(v, OP_Goto); + addrSelectA = sqlite3VdbeCurrentAddr(v); + + + /* Generate a coroutine to evaluate the SELECT statement to the + ** left of the compound operator - the "A" select. + */ + VdbeNoopComment((v, "Begin coroutine for left SELECT")); + pPrior->iLimit = regLimitA; + explainSetInteger(iSub1, pParse->iNextSelectId); + sqlite3Select(pParse, pPrior, &destA); + sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA); + sqlite3VdbeAddOp1(v, OP_Yield, regAddrA); + VdbeNoopComment((v, "End coroutine for left SELECT")); + + /* Generate a coroutine to evaluate the SELECT statement on + ** the right - the "B" select + */ + addrSelectB = sqlite3VdbeCurrentAddr(v); + VdbeNoopComment((v, "Begin coroutine for right SELECT")); + savedLimit = p->iLimit; + savedOffset = p->iOffset; + p->iLimit = regLimitB; + p->iOffset = 0; + explainSetInteger(iSub2, pParse->iNextSelectId); + sqlite3Select(pParse, p, &destB); + p->iLimit = savedLimit; + p->iOffset = savedOffset; + sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB); + sqlite3VdbeAddOp1(v, OP_Yield, regAddrB); + VdbeNoopComment((v, "End coroutine for right SELECT")); + + /* Generate a subroutine that outputs the current row of the A + ** select as the next output row of the compound select. + */ + VdbeNoopComment((v, "Output routine for A")); + addrOutA = generateOutputSubroutine(pParse, + p, &destA, pDest, regOutA, + regPrev, pKeyDup, P4_KEYINFO_HANDOFF, labelEnd); + + /* Generate a subroutine that outputs the current row of the B + ** select as the next output row of the compound select. + */ + if( op==TK_ALL || op==TK_UNION ){ + VdbeNoopComment((v, "Output routine for B")); + addrOutB = generateOutputSubroutine(pParse, + p, &destB, pDest, regOutB, + regPrev, pKeyDup, P4_KEYINFO_STATIC, labelEnd); + } + + /* Generate a subroutine to run when the results from select A + ** are exhausted and only data in select B remains. + */ + VdbeNoopComment((v, "eof-A subroutine")); + if( op==TK_EXCEPT || op==TK_INTERSECT ){ + addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd); + }else{ + addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd); + sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); + sqlite3VdbeAddOp1(v, OP_Yield, regAddrB); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA); + p->nSelectRow += pPrior->nSelectRow; + } + + /* Generate a subroutine to run when the results from select B + ** are exhausted and only data in select A remains. + */ + if( op==TK_INTERSECT ){ + addrEofB = addrEofA; + if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow; + }else{ + VdbeNoopComment((v, "eof-B subroutine")); + addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd); + sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA); + sqlite3VdbeAddOp1(v, OP_Yield, regAddrA); + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB); + } + + /* Generate code to handle the case of AB + */ + VdbeNoopComment((v, "A-gt-B subroutine")); + addrAgtB = sqlite3VdbeCurrentAddr(v); + if( op==TK_ALL || op==TK_UNION ){ + sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB); + } + sqlite3VdbeAddOp1(v, OP_Yield, regAddrB); + sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB); + sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr); + + /* This code runs once to initialize everything. + */ + sqlite3VdbeJumpHere(v, j1); + sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA); + sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB); + sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA); + sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB); + sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA); + sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB); + + /* Implement the main merge loop + */ + sqlite3VdbeResolveLabel(v, labelCmpr); + sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY); + sqlite3VdbeAddOp4(v, OP_Compare, destA.iMem, destB.iMem, nOrderBy, + (char*)pKeyMerge, P4_KEYINFO_HANDOFF); + sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB); + + /* Release temporary registers + */ + if( regPrev ){ + sqlite3ReleaseTempRange(pParse, regPrev, nOrderBy+1); + } + + /* Jump to the this point in order to terminate the query. + */ + sqlite3VdbeResolveLabel(v, labelEnd); + + /* Set the number of output columns + */ + if( pDest->eDest==SRT_Output ){ + Select *pFirst = pPrior; + while( pFirst->pPrior ) pFirst = pFirst->pPrior; + generateColumnNames(pParse, 0, pFirst->pEList); + } + + /* Reassembly the compound query so that it will be freed correctly + ** by the calling function */ + if( p->pPrior ){ + sqlite3SelectDelete(db, p->pPrior); + } + p->pPrior = pPrior; + + /*** TBD: Insert subroutine calls to close cursors on incomplete + **** subqueries ****/ + explainComposite(pParse, p->op, iSub1, iSub2, 0); + return SQLITE_OK; +} +#endif + +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) +/* Forward Declarations */ +static void substExprList(sqlite3*, ExprList*, int, ExprList*); +static void substSelect(sqlite3*, Select *, int, ExprList *); + +/* +** Scan through the expression pExpr. Replace every reference to +** a column in table number iTable with a copy of the iColumn-th +** entry in pEList. (But leave references to the ROWID column +** unchanged.) +** +** This routine is part of the flattening procedure. A subquery +** whose result set is defined by pEList appears as entry in the +** FROM clause of a SELECT such that the VDBE cursor assigned to that +** FORM clause entry is iTable. This routine make the necessary +** changes to pExpr so that it refers directly to the source table +** of the subquery rather the result set of the subquery. +*/ +static Expr *substExpr( + sqlite3 *db, /* Report malloc errors to this connection */ + Expr *pExpr, /* Expr in which substitution occurs */ + int iTable, /* Table to be substituted */ + ExprList *pEList /* Substitute expressions */ +){ + if( pExpr==0 ) return 0; + if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){ + if( pExpr->iColumn<0 ){ + pExpr->op = TK_NULL; + }else{ + Expr *pNew; + assert( pEList!=0 && pExpr->iColumnnExpr ); + assert( pExpr->pLeft==0 && pExpr->pRight==0 ); + pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0); + if( pNew && pExpr->pColl ){ + pNew->pColl = pExpr->pColl; + } + sqlite3ExprDelete(db, pExpr); + pExpr = pNew; + } + }else{ + pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList); + pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList); + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + substSelect(db, pExpr->x.pSelect, iTable, pEList); + }else{ + substExprList(db, pExpr->x.pList, iTable, pEList); + } + } + return pExpr; +} +static void substExprList( + sqlite3 *db, /* Report malloc errors here */ + ExprList *pList, /* List to scan and in which to make substitutes */ + int iTable, /* Table to be substituted */ + ExprList *pEList /* Substitute values */ +){ + int i; + if( pList==0 ) return; + for(i=0; inExpr; i++){ + pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList); + } +} +static void substSelect( + sqlite3 *db, /* Report malloc errors here */ + Select *p, /* SELECT statement in which to make substitutions */ + int iTable, /* Table to be replaced */ + ExprList *pEList /* Substitute values */ +){ + SrcList *pSrc; + struct SrcList_item *pItem; + int i; + if( !p ) return; + substExprList(db, p->pEList, iTable, pEList); + substExprList(db, p->pGroupBy, iTable, pEList); + substExprList(db, p->pOrderBy, iTable, pEList); + p->pHaving = substExpr(db, p->pHaving, iTable, pEList); + p->pWhere = substExpr(db, p->pWhere, iTable, pEList); + substSelect(db, p->pPrior, iTable, pEList); + pSrc = p->pSrc; + assert( pSrc ); /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */ + if( ALWAYS(pSrc) ){ + for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){ + substSelect(db, pItem->pSelect, iTable, pEList); + } + } +} +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ + +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) +/* +** This routine attempts to flatten subqueries in order to speed +** execution. It returns 1 if it makes changes and 0 if no flattening +** occurs. +** +** To understand the concept of flattening, consider the following +** query: +** +** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5 +** +** The default way of implementing this query is to execute the +** subquery first and store the results in a temporary table, then +** run the outer query on that temporary table. This requires two +** passes over the data. Furthermore, because the temporary table +** has no indices, the WHERE clause on the outer query cannot be +** optimized. +** +** This routine attempts to rewrite queries such as the above into +** a single flat select, like this: +** +** SELECT x+y AS a FROM t1 WHERE z<100 AND a>5 +** +** The code generated for this simpification gives the same result +** but only has to scan the data once. And because indices might +** exist on the table t1, a complete scan of the data might be +** avoided. +** +** Flattening is only attempted if all of the following are true: +** +** (1) The subquery and the outer query do not both use aggregates. +** +** (2) The subquery is not an aggregate or the outer query is not a join. +** +** (3) The subquery is not the right operand of a left outer join +** (Originally ticket #306. Strengthened by ticket #3300) +** +** (4) The subquery is not DISTINCT. +** +** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT +** sub-queries that were excluded from this optimization. Restriction +** (4) has since been expanded to exclude all DISTINCT subqueries. +** +** (6) The subquery does not use aggregates or the outer query is not +** DISTINCT. +** +** (7) The subquery has a FROM clause. +** +** (8) The subquery does not use LIMIT or the outer query is not a join. +** +** (9) The subquery does not use LIMIT or the outer query does not use +** aggregates. +** +** (10) The subquery does not use aggregates or the outer query does not +** use LIMIT. +** +** (11) The subquery and the outer query do not both have ORDER BY clauses. +** +** (**) Not implemented. Subsumed into restriction (3). Was previously +** a separate restriction deriving from ticket #350. +** +** (13) The subquery and outer query do not both use LIMIT. +** +** (14) The subquery does not use OFFSET. +** +** (15) The outer query is not part of a compound select or the +** subquery does not have a LIMIT clause. +** (See ticket #2339 and ticket [02a8e81d44]). +** +** (16) The outer query is not an aggregate or the subquery does +** not contain ORDER BY. (Ticket #2942) This used to not matter +** until we introduced the group_concat() function. +** +** (17) The sub-query is not a compound select, or it is a UNION ALL +** compound clause made up entirely of non-aggregate queries, and +** the parent query: +** +** * is not itself part of a compound select, +** * is not an aggregate or DISTINCT query, and +** * has no other tables or sub-selects in the FROM clause. +** +** The parent and sub-query may contain WHERE clauses. Subject to +** rules (11), (13) and (14), they may also contain ORDER BY, +** LIMIT and OFFSET clauses. +** +** (18) If the sub-query is a compound select, then all terms of the +** ORDER by clause of the parent must be simple references to +** columns of the sub-query. +** +** (19) The subquery does not use LIMIT or the outer query does not +** have a WHERE clause. +** +** (20) If the sub-query is a compound select, then it must not use +** an ORDER BY clause. Ticket #3773. We could relax this constraint +** somewhat by saying that the terms of the ORDER BY clause must +** appear as unmodified result columns in the outer query. But +** have other optimizations in mind to deal with that case. +** +** (21) The subquery does not use LIMIT or the outer query is not +** DISTINCT. (See ticket [752e1646fc]). +** +** In this routine, the "p" parameter is a pointer to the outer query. +** The subquery is p->pSrc->a[iFrom]. isAgg is true if the outer query +** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates. +** +** If flattening is not attempted, this routine is a no-op and returns 0. +** If flattening is attempted this routine returns 1. +** +** All of the expression analysis must occur on both the outer query and +** the subquery before this routine runs. +*/ +static int flattenSubquery( + Parse *pParse, /* Parsing context */ + Select *p, /* The parent or outer SELECT statement */ + int iFrom, /* Index in p->pSrc->a[] of the inner subquery */ + int isAgg, /* True if outer SELECT uses aggregate functions */ + int subqueryIsAgg /* True if the subquery uses aggregate functions */ +){ + const char *zSavedAuthContext = pParse->zAuthContext; + Select *pParent; + Select *pSub; /* The inner query or "subquery" */ + Select *pSub1; /* Pointer to the rightmost select in sub-query */ + SrcList *pSrc; /* The FROM clause of the outer query */ + SrcList *pSubSrc; /* The FROM clause of the subquery */ + ExprList *pList; /* The result set of the outer query */ + int iParent; /* VDBE cursor number of the pSub result set temp table */ + int i; /* Loop counter */ + Expr *pWhere; /* The WHERE clause */ + struct SrcList_item *pSubitem; /* The subquery */ + sqlite3 *db = pParse->db; + + /* Check to see if flattening is permitted. Return 0 if not. + */ + assert( p!=0 ); + assert( p->pPrior==0 ); /* Unable to flatten compound queries */ + if( db->flags & SQLITE_QueryFlattener ) return 0; + pSrc = p->pSrc; + assert( pSrc && iFrom>=0 && iFromnSrc ); + pSubitem = &pSrc->a[iFrom]; + iParent = pSubitem->iCursor; + pSub = pSubitem->pSelect; + assert( pSub!=0 ); + if( isAgg && subqueryIsAgg ) return 0; /* Restriction (1) */ + if( subqueryIsAgg && pSrc->nSrc>1 ) return 0; /* Restriction (2) */ + pSubSrc = pSub->pSrc; + assert( pSubSrc ); + /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants, + ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET + ** because they could be computed at compile-time. But when LIMIT and OFFSET + ** became arbitrary expressions, we were forced to add restrictions (13) + ** and (14). */ + if( pSub->pLimit && p->pLimit ) return 0; /* Restriction (13) */ + if( pSub->pOffset ) return 0; /* Restriction (14) */ + if( p->pRightmost && pSub->pLimit ){ + return 0; /* Restriction (15) */ + } + if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */ + if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */ + if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){ + return 0; /* Restrictions (8)(9) */ + } + if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){ + return 0; /* Restriction (6) */ + } + if( p->pOrderBy && pSub->pOrderBy ){ + return 0; /* Restriction (11) */ + } + if( isAgg && pSub->pOrderBy ) return 0; /* Restriction (16) */ + if( pSub->pLimit && p->pWhere ) return 0; /* Restriction (19) */ + if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){ + return 0; /* Restriction (21) */ + } + + /* OBSOLETE COMMENT 1: + ** Restriction 3: If the subquery is a join, make sure the subquery is + ** not used as the right operand of an outer join. Examples of why this + ** is not allowed: + ** + ** t1 LEFT OUTER JOIN (t2 JOIN t3) + ** + ** If we flatten the above, we would get + ** + ** (t1 LEFT OUTER JOIN t2) JOIN t3 + ** + ** which is not at all the same thing. + ** + ** OBSOLETE COMMENT 2: + ** Restriction 12: If the subquery is the right operand of a left outer + ** join, make sure the subquery has no WHERE clause. + ** An examples of why this is not allowed: + ** + ** t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0) + ** + ** If we flatten the above, we would get + ** + ** (t1 LEFT OUTER JOIN t2) WHERE t2.x>0 + ** + ** But the t2.x>0 test will always fail on a NULL row of t2, which + ** effectively converts the OUTER JOIN into an INNER JOIN. + ** + ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE: + ** Ticket #3300 shows that flattening the right term of a LEFT JOIN + ** is fraught with danger. Best to avoid the whole thing. If the + ** subquery is the right term of a LEFT JOIN, then do not flatten. + */ + if( (pSubitem->jointype & JT_OUTER)!=0 ){ + return 0; + } + + /* Restriction 17: If the sub-query is a compound SELECT, then it must + ** use only the UNION ALL operator. And none of the simple select queries + ** that make up the compound SELECT are allowed to be aggregate or distinct + ** queries. + */ + if( pSub->pPrior ){ + if( pSub->pOrderBy ){ + return 0; /* Restriction 20 */ + } + if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){ + return 0; + } + for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){ + testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ); + testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate ); + if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0 + || (pSub1->pPrior && pSub1->op!=TK_ALL) + || NEVER(pSub1->pSrc==0) || pSub1->pSrc->nSrc!=1 + ){ + return 0; + } + } + + /* Restriction 18. */ + if( p->pOrderBy ){ + int ii; + for(ii=0; iipOrderBy->nExpr; ii++){ + if( p->pOrderBy->a[ii].iCol==0 ) return 0; + } + } + } + + /***** If we reach this point, flattening is permitted. *****/ + + /* Authorize the subquery */ + pParse->zAuthContext = pSubitem->zName; + sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0); + pParse->zAuthContext = zSavedAuthContext; + + /* If the sub-query is a compound SELECT statement, then (by restrictions + ** 17 and 18 above) it must be a UNION ALL and the parent query must + ** be of the form: + ** + ** SELECT FROM () + ** + ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block + ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or + ** OFFSET clauses and joins them to the left-hand-side of the original + ** using UNION ALL operators. In this case N is the number of simple + ** select statements in the compound sub-query. + ** + ** Example: + ** + ** SELECT a+1 FROM ( + ** SELECT x FROM tab + ** UNION ALL + ** SELECT y FROM tab + ** UNION ALL + ** SELECT abs(z*2) FROM tab2 + ** ) WHERE a!=5 ORDER BY 1 + ** + ** Transformed into: + ** + ** SELECT x+1 FROM tab WHERE x+1!=5 + ** UNION ALL + ** SELECT y+1 FROM tab WHERE y+1!=5 + ** UNION ALL + ** SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5 + ** ORDER BY 1 + ** + ** We call this the "compound-subquery flattening". + */ + for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){ + Select *pNew; + ExprList *pOrderBy = p->pOrderBy; + Expr *pLimit = p->pLimit; + Select *pPrior = p->pPrior; + p->pOrderBy = 0; + p->pSrc = 0; + p->pPrior = 0; + p->pLimit = 0; + pNew = sqlite3SelectDup(db, p, 0); + p->pLimit = pLimit; + p->pOrderBy = pOrderBy; + p->pSrc = pSrc; + p->op = TK_ALL; + p->pRightmost = 0; + if( pNew==0 ){ + pNew = pPrior; + }else{ + pNew->pPrior = pPrior; + pNew->pRightmost = 0; + } + p->pPrior = pNew; + if( db->mallocFailed ) return 1; + } + + /* Begin flattening the iFrom-th entry of the FROM clause + ** in the outer query. + */ + pSub = pSub1 = pSubitem->pSelect; + + /* Delete the transient table structure associated with the + ** subquery + */ + sqlite3DbFree(db, pSubitem->zDatabase); + sqlite3DbFree(db, pSubitem->zName); + sqlite3DbFree(db, pSubitem->zAlias); + pSubitem->zDatabase = 0; + pSubitem->zName = 0; + pSubitem->zAlias = 0; + pSubitem->pSelect = 0; + + /* Defer deleting the Table object associated with the + ** subquery until code generation is + ** complete, since there may still exist Expr.pTab entries that + ** refer to the subquery even after flattening. Ticket #3346. + ** + ** pSubitem->pTab is always non-NULL by test restrictions and tests above. + */ + if( ALWAYS(pSubitem->pTab!=0) ){ + Table *pTabToDel = pSubitem->pTab; + if( pTabToDel->nRef==1 ){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + pTabToDel->pNextZombie = pToplevel->pZombieTab; + pToplevel->pZombieTab = pTabToDel; + }else{ + pTabToDel->nRef--; + } + pSubitem->pTab = 0; + } + + /* The following loop runs once for each term in a compound-subquery + ** flattening (as described above). If we are doing a different kind + ** of flattening - a flattening other than a compound-subquery flattening - + ** then this loop only runs once. + ** + ** This loop moves all of the FROM elements of the subquery into the + ** the FROM clause of the outer query. Before doing this, remember + ** the cursor number for the original outer query FROM element in + ** iParent. The iParent cursor will never be used. Subsequent code + ** will scan expressions looking for iParent references and replace + ** those references with expressions that resolve to the subquery FROM + ** elements we are now copying in. + */ + for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){ + int nSubSrc; + u8 jointype = 0; + pSubSrc = pSub->pSrc; /* FROM clause of subquery */ + nSubSrc = pSubSrc->nSrc; /* Number of terms in subquery FROM clause */ + pSrc = pParent->pSrc; /* FROM clause of the outer query */ + + if( pSrc ){ + assert( pParent==p ); /* First time through the loop */ + jointype = pSubitem->jointype; + }else{ + assert( pParent!=p ); /* 2nd and subsequent times through the loop */ + pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0); + if( pSrc==0 ){ + assert( db->mallocFailed ); + break; + } + } + + /* The subquery uses a single slot of the FROM clause of the outer + ** query. If the subquery has more than one element in its FROM clause, + ** then expand the outer query to make space for it to hold all elements + ** of the subquery. + ** + ** Example: + ** + ** SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB; + ** + ** The outer query has 3 slots in its FROM clause. One slot of the + ** outer query (the middle slot) is used by the subquery. The next + ** block of code will expand the out query to 4 slots. The middle + ** slot is expanded to two slots in order to make space for the + ** two elements in the FROM clause of the subquery. + */ + if( nSubSrc>1 ){ + pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1); + if( db->mallocFailed ){ + break; + } + } + + /* Transfer the FROM clause terms from the subquery into the + ** outer query. + */ + for(i=0; ia[i+iFrom].pUsing); + pSrc->a[i+iFrom] = pSubSrc->a[i]; + memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i])); + } + pSrc->a[iFrom].jointype = jointype; + + /* Now begin substituting subquery result set expressions for + ** references to the iParent in the outer query. + ** + ** Example: + ** + ** SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b; + ** \ \_____________ subquery __________/ / + ** \_____________________ outer query ______________________________/ + ** + ** We look at every expression in the outer query and every place we see + ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10". + */ + pList = pParent->pEList; + for(i=0; inExpr; i++){ + if( pList->a[i].zName==0 ){ + const char *zSpan = pList->a[i].zSpan; + if( ALWAYS(zSpan) ){ + pList->a[i].zName = sqlite3DbStrDup(db, zSpan); + } + } + } + substExprList(db, pParent->pEList, iParent, pSub->pEList); + if( isAgg ){ + substExprList(db, pParent->pGroupBy, iParent, pSub->pEList); + pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList); + } + if( pSub->pOrderBy ){ + assert( pParent->pOrderBy==0 ); + pParent->pOrderBy = pSub->pOrderBy; + pSub->pOrderBy = 0; + }else if( pParent->pOrderBy ){ + substExprList(db, pParent->pOrderBy, iParent, pSub->pEList); + } + if( pSub->pWhere ){ + pWhere = sqlite3ExprDup(db, pSub->pWhere, 0); + }else{ + pWhere = 0; + } + if( subqueryIsAgg ){ + assert( pParent->pHaving==0 ); + pParent->pHaving = pParent->pWhere; + pParent->pWhere = pWhere; + pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList); + pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving, + sqlite3ExprDup(db, pSub->pHaving, 0)); + assert( pParent->pGroupBy==0 ); + pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0); + }else{ + pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList); + pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere); + } + + /* The flattened query is distinct if either the inner or the + ** outer query is distinct. + */ + pParent->selFlags |= pSub->selFlags & SF_Distinct; + + /* + ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y; + ** + ** One is tempted to try to add a and b to combine the limits. But this + ** does not work if either limit is negative. + */ + if( pSub->pLimit ){ + pParent->pLimit = pSub->pLimit; + pSub->pLimit = 0; + } + } + + /* Finially, delete what is left of the subquery and return + ** success. + */ + sqlite3SelectDelete(db, pSub1); + + return 1; +} +#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */ + +/* +** Analyze the SELECT statement passed as an argument to see if it +** is a min() or max() query. Return WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX if +** it is, or 0 otherwise. At present, a query is considered to be +** a min()/max() query if: +** +** 1. There is a single object in the FROM clause. +** +** 2. There is a single expression in the result set, and it is +** either min(x) or max(x), where x is a column reference. +*/ +static u8 minMaxQuery(Select *p){ + Expr *pExpr; + ExprList *pEList = p->pEList; + + if( pEList->nExpr!=1 ) return WHERE_ORDERBY_NORMAL; + pExpr = pEList->a[0].pExpr; + if( pExpr->op!=TK_AGG_FUNCTION ) return 0; + if( NEVER(ExprHasProperty(pExpr, EP_xIsSelect)) ) return 0; + pEList = pExpr->x.pList; + if( pEList==0 || pEList->nExpr!=1 ) return 0; + if( pEList->a[0].pExpr->op!=TK_AGG_COLUMN ) return WHERE_ORDERBY_NORMAL; + assert( !ExprHasProperty(pExpr, EP_IntValue) ); + if( sqlite3StrICmp(pExpr->u.zToken,"min")==0 ){ + return WHERE_ORDERBY_MIN; + }else if( sqlite3StrICmp(pExpr->u.zToken,"max")==0 ){ + return WHERE_ORDERBY_MAX; + } + return WHERE_ORDERBY_NORMAL; +} + +/* +** The select statement passed as the first argument is an aggregate query. +** The second argment is the associated aggregate-info object. This +** function tests if the SELECT is of the form: +** +** SELECT count(*) FROM +** +** where table is a database table, not a sub-select or view. If the query +** does match this pattern, then a pointer to the Table object representing +** is returned. Otherwise, 0 is returned. +*/ +static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){ + Table *pTab; + Expr *pExpr; + + assert( !p->pGroupBy ); + + if( p->pWhere || p->pEList->nExpr!=1 + || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect + ){ + return 0; + } + pTab = p->pSrc->a[0].pTab; + pExpr = p->pEList->a[0].pExpr; + assert( pTab && !pTab->pSelect && pExpr ); + + if( IsVirtual(pTab) ) return 0; + if( pExpr->op!=TK_AGG_FUNCTION ) return 0; + if( (pAggInfo->aFunc[0].pFunc->flags&SQLITE_FUNC_COUNT)==0 ) return 0; + if( pExpr->flags&EP_Distinct ) return 0; + + return pTab; +} + +/* +** If the source-list item passed as an argument was augmented with an +** INDEXED BY clause, then try to locate the specified index. If there +** was such a clause and the named index cannot be found, return +** SQLITE_ERROR and leave an error in pParse. Otherwise, populate +** pFrom->pIndex and return SQLITE_OK. +*/ +SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){ + if( pFrom->pTab && pFrom->zIndex ){ + Table *pTab = pFrom->pTab; + char *zIndex = pFrom->zIndex; + Index *pIdx; + for(pIdx=pTab->pIndex; + pIdx && sqlite3StrICmp(pIdx->zName, zIndex); + pIdx=pIdx->pNext + ); + if( !pIdx ){ + sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0); + pParse->checkSchema = 1; + return SQLITE_ERROR; + } + pFrom->pIndex = pIdx; + } + return SQLITE_OK; +} + +/* +** This routine is a Walker callback for "expanding" a SELECT statement. +** "Expanding" means to do the following: +** +** (1) Make sure VDBE cursor numbers have been assigned to every +** element of the FROM clause. +** +** (2) Fill in the pTabList->a[].pTab fields in the SrcList that +** defines FROM clause. When views appear in the FROM clause, +** fill pTabList->a[].pSelect with a copy of the SELECT statement +** that implements the view. A copy is made of the view's SELECT +** statement so that we can freely modify or delete that statement +** without worrying about messing up the presistent representation +** of the view. +** +** (3) Add terms to the WHERE clause to accomodate the NATURAL keyword +** on joins and the ON and USING clause of joins. +** +** (4) Scan the list of columns in the result set (pEList) looking +** for instances of the "*" operator or the TABLE.* operator. +** If found, expand each "*" to be every column in every table +** and TABLE.* to be every column in TABLE. +** +*/ +static int selectExpander(Walker *pWalker, Select *p){ + Parse *pParse = pWalker->pParse; + int i, j, k; + SrcList *pTabList; + ExprList *pEList; + struct SrcList_item *pFrom; + sqlite3 *db = pParse->db; + + if( db->mallocFailed ){ + return WRC_Abort; + } + if( NEVER(p->pSrc==0) || (p->selFlags & SF_Expanded)!=0 ){ + return WRC_Prune; + } + p->selFlags |= SF_Expanded; + pTabList = p->pSrc; + pEList = p->pEList; + + /* Make sure cursor numbers have been assigned to all entries in + ** the FROM clause of the SELECT statement. + */ + sqlite3SrcListAssignCursors(pParse, pTabList); + + /* Look up every table named in the FROM clause of the select. If + ** an entry of the FROM clause is a subquery instead of a table or view, + ** then create a transient table structure to describe the subquery. + */ + for(i=0, pFrom=pTabList->a; inSrc; i++, pFrom++){ + Table *pTab; + if( pFrom->pTab!=0 ){ + /* This statement has already been prepared. There is no need + ** to go further. */ + assert( i==0 ); + return WRC_Prune; + } + if( pFrom->zName==0 ){ +#ifndef SQLITE_OMIT_SUBQUERY + Select *pSel = pFrom->pSelect; + /* A sub-query in the FROM clause of a SELECT */ + assert( pSel!=0 ); + assert( pFrom->pTab==0 ); + sqlite3WalkSelect(pWalker, pSel); + pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table)); + if( pTab==0 ) return WRC_Abort; + pTab->nRef = 1; + pTab->zName = sqlite3MPrintf(db, "sqlite_subquery_%p_", (void*)pTab); + while( pSel->pPrior ){ pSel = pSel->pPrior; } + selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol); + pTab->iPKey = -1; + pTab->nRowEst = 1000000; + pTab->tabFlags |= TF_Ephemeral; +#endif + }else{ + /* An ordinary table or view name in the FROM clause */ + assert( pFrom->pTab==0 ); + pFrom->pTab = pTab = + sqlite3LocateTable(pParse,0,pFrom->zName,pFrom->zDatabase); + if( pTab==0 ) return WRC_Abort; + pTab->nRef++; +#if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE) + if( pTab->pSelect || IsVirtual(pTab) ){ + /* We reach here if the named table is a really a view */ + if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort; + assert( pFrom->pSelect==0 ); + pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0); + sqlite3WalkSelect(pWalker, pFrom->pSelect); + } +#endif + } + + /* Locate the index named by the INDEXED BY clause, if any. */ + if( sqlite3IndexedByLookup(pParse, pFrom) ){ + return WRC_Abort; + } + } + + /* Process NATURAL keywords, and ON and USING clauses of joins. + */ + if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){ + return WRC_Abort; + } + + /* For every "*" that occurs in the column list, insert the names of + ** all columns in all tables. And for every TABLE.* insert the names + ** of all columns in TABLE. The parser inserted a special expression + ** with the TK_ALL operator for each "*" that it found in the column list. + ** The following code just has to locate the TK_ALL expressions and expand + ** each one to the list of all columns in all tables. + ** + ** The first loop just checks to see if there are any "*" operators + ** that need expanding. + */ + for(k=0; knExpr; k++){ + Expr *pE = pEList->a[k].pExpr; + if( pE->op==TK_ALL ) break; + assert( pE->op!=TK_DOT || pE->pRight!=0 ); + assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) ); + if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break; + } + if( knExpr ){ + /* + ** If we get here it means the result set contains one or more "*" + ** operators that need to be expanded. Loop through each expression + ** in the result set and expand them one by one. + */ + struct ExprList_item *a = pEList->a; + ExprList *pNew = 0; + int flags = pParse->db->flags; + int longNames = (flags & SQLITE_FullColNames)!=0 + && (flags & SQLITE_ShortColNames)==0; + + for(k=0; knExpr; k++){ + Expr *pE = a[k].pExpr; + assert( pE->op!=TK_DOT || pE->pRight!=0 ); + if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pE->pRight->op!=TK_ALL) ){ + /* This particular expression does not need to be expanded. + */ + pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr); + if( pNew ){ + pNew->a[pNew->nExpr-1].zName = a[k].zName; + pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan; + a[k].zName = 0; + a[k].zSpan = 0; + } + a[k].pExpr = 0; + }else{ + /* This expression is a "*" or a "TABLE.*" and needs to be + ** expanded. */ + int tableSeen = 0; /* Set to 1 when TABLE matches */ + char *zTName; /* text of name of TABLE */ + if( pE->op==TK_DOT ){ + assert( pE->pLeft!=0 ); + assert( !ExprHasProperty(pE->pLeft, EP_IntValue) ); + zTName = pE->pLeft->u.zToken; + }else{ + zTName = 0; + } + for(i=0, pFrom=pTabList->a; inSrc; i++, pFrom++){ + Table *pTab = pFrom->pTab; + char *zTabName = pFrom->zAlias; + if( zTabName==0 ){ + zTabName = pTab->zName; + } + if( db->mallocFailed ) break; + if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){ + continue; + } + tableSeen = 1; + for(j=0; jnCol; j++){ + Expr *pExpr, *pRight; + char *zName = pTab->aCol[j].zName; + char *zColname; /* The computed column name */ + char *zToFree; /* Malloced string that needs to be freed */ + Token sColname; /* Computed column name as a token */ + + /* If a column is marked as 'hidden' (currently only possible + ** for virtual tables), do not include it in the expanded + ** result-set list. + */ + if( IsHiddenColumn(&pTab->aCol[j]) ){ + assert(IsVirtual(pTab)); + continue; + } + + if( i>0 && zTName==0 ){ + if( (pFrom->jointype & JT_NATURAL)!=0 + && tableAndColumnIndex(pTabList, i, zName, 0, 0) + ){ + /* In a NATURAL join, omit the join columns from the + ** table to the right of the join */ + continue; + } + if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){ + /* In a join with a USING clause, omit columns in the + ** using clause from the table on the right. */ + continue; + } + } + pRight = sqlite3Expr(db, TK_ID, zName); + zColname = zName; + zToFree = 0; + if( longNames || pTabList->nSrc>1 ){ + Expr *pLeft; + pLeft = sqlite3Expr(db, TK_ID, zTabName); + pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); + if( longNames ){ + zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName); + zToFree = zColname; + } + }else{ + pExpr = pRight; + } + pNew = sqlite3ExprListAppend(pParse, pNew, pExpr); + sColname.z = zColname; + sColname.n = sqlite3Strlen30(zColname); + sqlite3ExprListSetName(pParse, pNew, &sColname, 0); + sqlite3DbFree(db, zToFree); + } + } + if( !tableSeen ){ + if( zTName ){ + sqlite3ErrorMsg(pParse, "no such table: %s", zTName); + }else{ + sqlite3ErrorMsg(pParse, "no tables specified"); + } + } + } + } + sqlite3ExprListDelete(db, pEList); + p->pEList = pNew; + } +#if SQLITE_MAX_COLUMN + if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){ + sqlite3ErrorMsg(pParse, "too many columns in result set"); + } +#endif + return WRC_Continue; +} + +/* +** No-op routine for the parse-tree walker. +** +** When this routine is the Walker.xExprCallback then expression trees +** are walked without any actions being taken at each node. Presumably, +** when this routine is used for Walker.xExprCallback then +** Walker.xSelectCallback is set to do something useful for every +** subquery in the parser tree. +*/ +static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){ + UNUSED_PARAMETER2(NotUsed, NotUsed2); + return WRC_Continue; +} + +/* +** This routine "expands" a SELECT statement and all of its subqueries. +** For additional information on what it means to "expand" a SELECT +** statement, see the comment on the selectExpand worker callback above. +** +** Expanding a SELECT statement is the first step in processing a +** SELECT statement. The SELECT statement must be expanded before +** name resolution is performed. +** +** If anything goes wrong, an error message is written into pParse. +** The calling function can detect the problem by looking at pParse->nErr +** and/or pParse->db->mallocFailed. +*/ +static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){ + Walker w; + w.xSelectCallback = selectExpander; + w.xExprCallback = exprWalkNoop; + w.pParse = pParse; + sqlite3WalkSelect(&w, pSelect); +} + + +#ifndef SQLITE_OMIT_SUBQUERY +/* +** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo() +** interface. +** +** For each FROM-clause subquery, add Column.zType and Column.zColl +** information to the Table structure that represents the result set +** of that subquery. +** +** The Table structure that represents the result set was constructed +** by selectExpander() but the type and collation information was omitted +** at that point because identifiers had not yet been resolved. This +** routine is called after identifier resolution. +*/ +static int selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){ + Parse *pParse; + int i; + SrcList *pTabList; + struct SrcList_item *pFrom; + + assert( p->selFlags & SF_Resolved ); + if( (p->selFlags & SF_HasTypeInfo)==0 ){ + p->selFlags |= SF_HasTypeInfo; + pParse = pWalker->pParse; + pTabList = p->pSrc; + for(i=0, pFrom=pTabList->a; inSrc; i++, pFrom++){ + Table *pTab = pFrom->pTab; + if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){ + /* A sub-query in the FROM clause of a SELECT */ + Select *pSel = pFrom->pSelect; + assert( pSel ); + while( pSel->pPrior ) pSel = pSel->pPrior; + selectAddColumnTypeAndCollation(pParse, pTab->nCol, pTab->aCol, pSel); + } + } + } + return WRC_Continue; +} +#endif + + +/* +** This routine adds datatype and collating sequence information to +** the Table structures of all FROM-clause subqueries in a +** SELECT statement. +** +** Use this routine after name resolution. +*/ +static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){ +#ifndef SQLITE_OMIT_SUBQUERY + Walker w; + w.xSelectCallback = selectAddSubqueryTypeInfo; + w.xExprCallback = exprWalkNoop; + w.pParse = pParse; + sqlite3WalkSelect(&w, pSelect); +#endif +} + + +/* +** This routine sets of a SELECT statement for processing. The +** following is accomplished: +** +** * VDBE Cursor numbers are assigned to all FROM-clause terms. +** * Ephemeral Table objects are created for all FROM-clause subqueries. +** * ON and USING clauses are shifted into WHERE statements +** * Wildcards "*" and "TABLE.*" in result sets are expanded. +** * Identifiers in expression are matched to tables. +** +** This routine acts recursively on all subqueries within the SELECT. +*/ +SQLITE_PRIVATE void sqlite3SelectPrep( + Parse *pParse, /* The parser context */ + Select *p, /* The SELECT statement being coded. */ + NameContext *pOuterNC /* Name context for container */ +){ + sqlite3 *db; + if( NEVER(p==0) ) return; + db = pParse->db; + if( p->selFlags & SF_HasTypeInfo ) return; + sqlite3SelectExpand(pParse, p); + if( pParse->nErr || db->mallocFailed ) return; + sqlite3ResolveSelectNames(pParse, p, pOuterNC); + if( pParse->nErr || db->mallocFailed ) return; + sqlite3SelectAddTypeInfo(pParse, p); +} + +/* +** Reset the aggregate accumulator. +** +** The aggregate accumulator is a set of memory cells that hold +** intermediate results while calculating an aggregate. This +** routine simply stores NULLs in all of those memory cells. +*/ +static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){ + Vdbe *v = pParse->pVdbe; + int i; + struct AggInfo_func *pFunc; + if( pAggInfo->nFunc+pAggInfo->nColumn==0 ){ + return; + } + for(i=0; inColumn; i++){ + sqlite3VdbeAddOp2(v, OP_Null, 0, pAggInfo->aCol[i].iMem); + } + for(pFunc=pAggInfo->aFunc, i=0; inFunc; i++, pFunc++){ + sqlite3VdbeAddOp2(v, OP_Null, 0, pFunc->iMem); + if( pFunc->iDistinct>=0 ){ + Expr *pE = pFunc->pExpr; + assert( !ExprHasProperty(pE, EP_xIsSelect) ); + if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){ + sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one " + "argument"); + pFunc->iDistinct = -1; + }else{ + KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList); + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0, + (char*)pKeyInfo, P4_KEYINFO_HANDOFF); + } + } + } +} + +/* +** Invoke the OP_AggFinalize opcode for every aggregate function +** in the AggInfo structure. +*/ +static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){ + Vdbe *v = pParse->pVdbe; + int i; + struct AggInfo_func *pF; + for(i=0, pF=pAggInfo->aFunc; inFunc; i++, pF++){ + ExprList *pList = pF->pExpr->x.pList; + assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); + sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0, + (void*)pF->pFunc, P4_FUNCDEF); + } +} + +/* +** Update the accumulator memory cells for an aggregate based on +** the current cursor position. +*/ +static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){ + Vdbe *v = pParse->pVdbe; + int i; + struct AggInfo_func *pF; + struct AggInfo_col *pC; + + pAggInfo->directMode = 1; + sqlite3ExprCacheClear(pParse); + for(i=0, pF=pAggInfo->aFunc; inFunc; i++, pF++){ + int nArg; + int addrNext = 0; + int regAgg; + ExprList *pList = pF->pExpr->x.pList; + assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) ); + if( pList ){ + nArg = pList->nExpr; + regAgg = sqlite3GetTempRange(pParse, nArg); + sqlite3ExprCodeExprList(pParse, pList, regAgg, 1); + }else{ + nArg = 0; + regAgg = 0; + } + if( pF->iDistinct>=0 ){ + addrNext = sqlite3VdbeMakeLabel(v); + assert( nArg==1 ); + codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg); + } + if( pF->pFunc->flags & SQLITE_FUNC_NEEDCOLL ){ + CollSeq *pColl = 0; + struct ExprList_item *pItem; + int j; + assert( pList!=0 ); /* pList!=0 if pF->pFunc has NEEDCOLL */ + for(j=0, pItem=pList->a; !pColl && jpExpr); + } + if( !pColl ){ + pColl = pParse->db->pDfltColl; + } + sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ); + } + sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem, + (void*)pF->pFunc, P4_FUNCDEF); + sqlite3VdbeChangeP5(v, (u8)nArg); + sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg); + sqlite3ReleaseTempRange(pParse, regAgg, nArg); + if( addrNext ){ + sqlite3VdbeResolveLabel(v, addrNext); + sqlite3ExprCacheClear(pParse); + } + } + + /* Before populating the accumulator registers, clear the column cache. + ** Otherwise, if any of the required column values are already present + ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value + ** to pC->iMem. But by the time the value is used, the original register + ** may have been used, invalidating the underlying buffer holding the + ** text or blob value. See ticket [883034dcb5]. + ** + ** Another solution would be to change the OP_SCopy used to copy cached + ** values to an OP_Copy. + */ + sqlite3ExprCacheClear(pParse); + for(i=0, pC=pAggInfo->aCol; inAccumulator; i++, pC++){ + sqlite3ExprCode(pParse, pC->pExpr, pC->iMem); + } + pAggInfo->directMode = 0; + sqlite3ExprCacheClear(pParse); +} + +/* +** Add a single OP_Explain instruction to the VDBE to explain a simple +** count(*) query ("SELECT count(*) FROM pTab"). +*/ +#ifndef SQLITE_OMIT_EXPLAIN +static void explainSimpleCount( + Parse *pParse, /* Parse context */ + Table *pTab, /* Table being queried */ + Index *pIdx /* Index used to optimize scan, or NULL */ +){ + if( pParse->explain==2 ){ + char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s %s%s(~%d rows)", + pTab->zName, + pIdx ? "USING COVERING INDEX " : "", + pIdx ? pIdx->zName : "", + pTab->nRowEst + ); + sqlite3VdbeAddOp4( + pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC + ); + } +} +#else +# define explainSimpleCount(a,b,c) +#endif + +/* +** Generate code for the SELECT statement given in the p argument. +** +** The results are distributed in various ways depending on the +** contents of the SelectDest structure pointed to by argument pDest +** as follows: +** +** pDest->eDest Result +** ------------ ------------------------------------------- +** SRT_Output Generate a row of output (using the OP_ResultRow +** opcode) for each row in the result set. +** +** SRT_Mem Only valid if the result is a single column. +** Store the first column of the first result row +** in register pDest->iParm then abandon the rest +** of the query. This destination implies "LIMIT 1". +** +** SRT_Set The result must be a single column. Store each +** row of result as the key in table pDest->iParm. +** Apply the affinity pDest->affinity before storing +** results. Used to implement "IN (SELECT ...)". +** +** SRT_Union Store results as a key in a temporary table pDest->iParm. +** +** SRT_Except Remove results from the temporary table pDest->iParm. +** +** SRT_Table Store results in temporary table pDest->iParm. +** This is like SRT_EphemTab except that the table +** is assumed to already be open. +** +** SRT_EphemTab Create an temporary table pDest->iParm and store +** the result there. The cursor is left open after +** returning. This is like SRT_Table except that +** this destination uses OP_OpenEphemeral to create +** the table first. +** +** SRT_Coroutine Generate a co-routine that returns a new row of +** results each time it is invoked. The entry point +** of the co-routine is stored in register pDest->iParm. +** +** SRT_Exists Store a 1 in memory cell pDest->iParm if the result +** set is not empty. +** +** SRT_Discard Throw the results away. This is used by SELECT +** statements within triggers whose only purpose is +** the side-effects of functions. +** +** This routine returns the number of errors. If any errors are +** encountered, then an appropriate error message is left in +** pParse->zErrMsg. +** +** This routine does NOT free the Select structure passed in. The +** calling function needs to do that. +*/ +SQLITE_PRIVATE int sqlite3Select( + Parse *pParse, /* The parser context */ + Select *p, /* The SELECT statement being coded. */ + SelectDest *pDest /* What to do with the query results */ +){ + int i, j; /* Loop counters */ + WhereInfo *pWInfo; /* Return from sqlite3WhereBegin() */ + Vdbe *v; /* The virtual machine under construction */ + int isAgg; /* True for select lists like "count(*)" */ + ExprList *pEList; /* List of columns to extract. */ + SrcList *pTabList; /* List of tables to select from */ + Expr *pWhere; /* The WHERE clause. May be NULL */ + ExprList *pOrderBy; /* The ORDER BY clause. May be NULL */ + ExprList *pGroupBy; /* The GROUP BY clause. May be NULL */ + Expr *pHaving; /* The HAVING clause. May be NULL */ + int isDistinct; /* True if the DISTINCT keyword is present */ + int distinct; /* Table to use for the distinct set */ + int rc = 1; /* Value to return from this function */ + int addrSortIndex; /* Address of an OP_OpenEphemeral instruction */ + AggInfo sAggInfo; /* Information used by aggregate queries */ + int iEnd; /* Address of the end of the query */ + sqlite3 *db; /* The database connection */ + +#ifndef SQLITE_OMIT_EXPLAIN + int iRestoreSelectId = pParse->iSelectId; + pParse->iSelectId = pParse->iNextSelectId++; +#endif + + db = pParse->db; + if( p==0 || db->mallocFailed || pParse->nErr ){ + return 1; + } + if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1; + memset(&sAggInfo, 0, sizeof(sAggInfo)); + + if( IgnorableOrderby(pDest) ){ + assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union || + pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard); + /* If ORDER BY makes no difference in the output then neither does + ** DISTINCT so it can be removed too. */ + sqlite3ExprListDelete(db, p->pOrderBy); + p->pOrderBy = 0; + p->selFlags &= ~SF_Distinct; + } + sqlite3SelectPrep(pParse, p, 0); + pOrderBy = p->pOrderBy; + pTabList = p->pSrc; + pEList = p->pEList; + if( pParse->nErr || db->mallocFailed ){ + goto select_end; + } + isAgg = (p->selFlags & SF_Aggregate)!=0; + assert( pEList!=0 ); + + /* Begin generating code. + */ + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto select_end; + + /* If writing to memory or generating a set + ** only a single column may be output. + */ +#ifndef SQLITE_OMIT_SUBQUERY + if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){ + goto select_end; + } +#endif + + /* Generate code for all sub-queries in the FROM clause + */ +#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) + for(i=0; !p->pPrior && inSrc; i++){ + struct SrcList_item *pItem = &pTabList->a[i]; + SelectDest dest; + Select *pSub = pItem->pSelect; + int isAggSub; + + if( pSub==0 || pItem->isPopulated ) continue; + + /* Increment Parse.nHeight by the height of the largest expression + ** tree refered to by this, the parent select. The child select + ** may contain expression trees of at most + ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit + ** more conservative than necessary, but much easier than enforcing + ** an exact limit. + */ + pParse->nHeight += sqlite3SelectExprHeight(p); + + /* Check to see if the subquery can be absorbed into the parent. */ + isAggSub = (pSub->selFlags & SF_Aggregate)!=0; + if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){ + if( isAggSub ){ + isAgg = 1; + p->selFlags |= SF_Aggregate; + } + i = -1; + }else{ + sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor); + assert( pItem->isPopulated==0 ); + explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId); + sqlite3Select(pParse, pSub, &dest); + pItem->isPopulated = 1; + pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow; + } + if( /*pParse->nErr ||*/ db->mallocFailed ){ + goto select_end; + } + pParse->nHeight -= sqlite3SelectExprHeight(p); + pTabList = p->pSrc; + if( !IgnorableOrderby(pDest) ){ + pOrderBy = p->pOrderBy; + } + } + pEList = p->pEList; +#endif + pWhere = p->pWhere; + pGroupBy = p->pGroupBy; + pHaving = p->pHaving; + isDistinct = (p->selFlags & SF_Distinct)!=0; + +#ifndef SQLITE_OMIT_COMPOUND_SELECT + /* If there is are a sequence of queries, do the earlier ones first. + */ + if( p->pPrior ){ + if( p->pRightmost==0 ){ + Select *pLoop, *pRight = 0; + int cnt = 0; + int mxSelect; + for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){ + pLoop->pRightmost = p; + pLoop->pNext = pRight; + pRight = pLoop; + } + mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT]; + if( mxSelect && cnt>mxSelect ){ + sqlite3ErrorMsg(pParse, "too many terms in compound SELECT"); + goto select_end; + } + } + rc = multiSelect(pParse, p, pDest); + explainSetInteger(pParse->iSelectId, iRestoreSelectId); + return rc; + } +#endif + + /* If possible, rewrite the query to use GROUP BY instead of DISTINCT. + ** GROUP BY might use an index, DISTINCT never does. + */ + assert( p->pGroupBy==0 || (p->selFlags & SF_Aggregate)!=0 ); + if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct ){ + p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0); + pGroupBy = p->pGroupBy; + p->selFlags &= ~SF_Distinct; + } + + /* If there is both a GROUP BY and an ORDER BY clause and they are + ** identical, then disable the ORDER BY clause since the GROUP BY + ** will cause elements to come out in the correct order. This is + ** an optimization - the correct answer should result regardless. + ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER + ** to disable this optimization for testing purposes. + */ + if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy)==0 + && (db->flags & SQLITE_GroupByOrder)==0 ){ + pOrderBy = 0; + } + + /* If there is an ORDER BY clause, then this sorting + ** index might end up being unused if the data can be + ** extracted in pre-sorted order. If that is the case, then the + ** OP_OpenEphemeral instruction will be changed to an OP_Noop once + ** we figure out that the sorting index is not needed. The addrSortIndex + ** variable is used to facilitate that change. + */ + if( pOrderBy ){ + KeyInfo *pKeyInfo; + pKeyInfo = keyInfoFromExprList(pParse, pOrderBy); + pOrderBy->iECursor = pParse->nTab++; + p->addrOpenEphm[2] = addrSortIndex = + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, + pOrderBy->iECursor, pOrderBy->nExpr+2, 0, + (char*)pKeyInfo, P4_KEYINFO_HANDOFF); + }else{ + addrSortIndex = -1; + } + + /* If the output is destined for a temporary table, open that table. + */ + if( pDest->eDest==SRT_EphemTab ){ + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iParm, pEList->nExpr); + } + + /* Set the limiter. + */ + iEnd = sqlite3VdbeMakeLabel(v); + p->nSelectRow = (double)LARGEST_INT64; + computeLimitRegisters(pParse, p, iEnd); + + /* Open a virtual index to use for the distinct set. + */ + if( p->selFlags & SF_Distinct ){ + KeyInfo *pKeyInfo; + assert( isAgg || pGroupBy ); + distinct = pParse->nTab++; + pKeyInfo = keyInfoFromExprList(pParse, p->pEList); + sqlite3VdbeAddOp4(v, OP_OpenEphemeral, distinct, 0, 0, + (char*)pKeyInfo, P4_KEYINFO_HANDOFF); + sqlite3VdbeChangeP5(v, BTREE_UNORDERED); + }else{ + distinct = -1; + } + + /* Aggregate and non-aggregate queries are handled differently */ + if( !isAgg && pGroupBy==0 ){ + /* This case is for non-aggregate queries + ** Begin the database scan + */ + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pOrderBy, 0); + if( pWInfo==0 ) goto select_end; + if( pWInfo->nRowOut < p->nSelectRow ) p->nSelectRow = pWInfo->nRowOut; + + /* If sorting index that was created by a prior OP_OpenEphemeral + ** instruction ended up not being needed, then change the OP_OpenEphemeral + ** into an OP_Noop. + */ + if( addrSortIndex>=0 && pOrderBy==0 ){ + sqlite3VdbeChangeToNoop(v, addrSortIndex, 1); + p->addrOpenEphm[2] = -1; + } + + /* Use the standard inner loop + */ + assert(!isDistinct); + selectInnerLoop(pParse, p, pEList, 0, 0, pOrderBy, -1, pDest, + pWInfo->iContinue, pWInfo->iBreak); + + /* End the database scan loop. + */ + sqlite3WhereEnd(pWInfo); + }else{ + /* This is the processing for aggregate queries */ + NameContext sNC; /* Name context for processing aggregate information */ + int iAMem; /* First Mem address for storing current GROUP BY */ + int iBMem; /* First Mem address for previous GROUP BY */ + int iUseFlag; /* Mem address holding flag indicating that at least + ** one row of the input to the aggregator has been + ** processed */ + int iAbortFlag; /* Mem address which causes query abort if positive */ + int groupBySort; /* Rows come from source in GROUP BY order */ + int addrEnd; /* End of processing for this SELECT */ + + /* Remove any and all aliases between the result set and the + ** GROUP BY clause. + */ + if( pGroupBy ){ + int k; /* Loop counter */ + struct ExprList_item *pItem; /* For looping over expression in a list */ + + for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){ + pItem->iAlias = 0; + } + for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){ + pItem->iAlias = 0; + } + if( p->nSelectRow>(double)100 ) p->nSelectRow = (double)100; + }else{ + p->nSelectRow = (double)1; + } + + + /* Create a label to jump to when we want to abort the query */ + addrEnd = sqlite3VdbeMakeLabel(v); + + /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in + ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the + ** SELECT statement. + */ + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + sNC.pSrcList = pTabList; + sNC.pAggInfo = &sAggInfo; + sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0; + sAggInfo.pGroupBy = pGroupBy; + sqlite3ExprAnalyzeAggList(&sNC, pEList); + sqlite3ExprAnalyzeAggList(&sNC, pOrderBy); + if( pHaving ){ + sqlite3ExprAnalyzeAggregates(&sNC, pHaving); + } + sAggInfo.nAccumulator = sAggInfo.nColumn; + for(i=0; ix.pList); + } + if( db->mallocFailed ) goto select_end; + + /* Processing for aggregates with GROUP BY is very different and + ** much more complex than aggregates without a GROUP BY. + */ + if( pGroupBy ){ + KeyInfo *pKeyInfo; /* Keying information for the group by clause */ + int j1; /* A-vs-B comparision jump */ + int addrOutputRow; /* Start of subroutine that outputs a result row */ + int regOutputRow; /* Return address register for output subroutine */ + int addrSetAbort; /* Set the abort flag and return */ + int addrTopOfLoop; /* Top of the input loop */ + int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */ + int addrReset; /* Subroutine for resetting the accumulator */ + int regReset; /* Return address register for reset subroutine */ + + /* If there is a GROUP BY clause we might need a sorting index to + ** implement it. Allocate that sorting index now. If it turns out + ** that we do not need it after all, the OpenEphemeral instruction + ** will be converted into a Noop. + */ + sAggInfo.sortingIdx = pParse->nTab++; + pKeyInfo = keyInfoFromExprList(pParse, pGroupBy); + addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral, + sAggInfo.sortingIdx, sAggInfo.nSortingColumn, + 0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF); + + /* Initialize memory locations used by GROUP BY aggregate processing + */ + iUseFlag = ++pParse->nMem; + iAbortFlag = ++pParse->nMem; + regOutputRow = ++pParse->nMem; + addrOutputRow = sqlite3VdbeMakeLabel(v); + regReset = ++pParse->nMem; + addrReset = sqlite3VdbeMakeLabel(v); + iAMem = pParse->nMem + 1; + pParse->nMem += pGroupBy->nExpr; + iBMem = pParse->nMem + 1; + pParse->nMem += pGroupBy->nExpr; + sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag); + VdbeComment((v, "clear abort flag")); + sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag); + VdbeComment((v, "indicate accumulator empty")); + + /* Begin a loop that will extract all source rows in GROUP BY order. + ** This might involve two separate loops with an OP_Sort in between, or + ** it might be a single loop that uses an index to extract information + ** in the right order to begin with. + */ + sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pGroupBy, 0); + if( pWInfo==0 ) goto select_end; + if( pGroupBy==0 ){ + /* The optimizer is able to deliver rows in group by order so + ** we do not have to sort. The OP_OpenEphemeral table will be + ** cancelled later because we still need to use the pKeyInfo + */ + pGroupBy = p->pGroupBy; + groupBySort = 0; + }else{ + /* Rows are coming out in undetermined order. We have to push + ** each row into a sorting index, terminate the first loop, + ** then loop over the sorting index in order to get the output + ** in sorted order + */ + int regBase; + int regRecord; + int nCol; + int nGroupBy; + + explainTempTable(pParse, + isDistinct && !(p->selFlags&SF_Distinct)?"DISTINCT":"GROUP BY"); + + groupBySort = 1; + nGroupBy = pGroupBy->nExpr; + nCol = nGroupBy + 1; + j = nGroupBy+1; + for(i=0; i=j ){ + nCol++; + j++; + } + } + regBase = sqlite3GetTempRange(pParse, nCol); + sqlite3ExprCacheClear(pParse); + sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0); + sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy); + j = nGroupBy+1; + for(i=0; iiSorterColumn>=j ){ + int r1 = j + regBase; + int r2; + + r2 = sqlite3ExprCodeGetColumn(pParse, + pCol->pTab, pCol->iColumn, pCol->iTable, r1); + if( r1!=r2 ){ + sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1); + } + j++; + } + } + regRecord = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord); + sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord); + sqlite3ReleaseTempReg(pParse, regRecord); + sqlite3ReleaseTempRange(pParse, regBase, nCol); + sqlite3WhereEnd(pWInfo); + sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd); + VdbeComment((v, "GROUP BY sort")); + sAggInfo.useSortingIdx = 1; + sqlite3ExprCacheClear(pParse); + } + + /* Evaluate the current GROUP BY terms and store in b0, b1, b2... + ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth) + ** Then compare the current GROUP BY terms against the GROUP BY terms + ** from the previous row currently stored in a0, a1, a2... + */ + addrTopOfLoop = sqlite3VdbeCurrentAddr(v); + sqlite3ExprCacheClear(pParse); + for(j=0; jnExpr; j++){ + if( groupBySort ){ + sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j); + }else{ + sAggInfo.directMode = 1; + sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j); + } + } + sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr, + (char*)pKeyInfo, P4_KEYINFO); + j1 = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1); + + /* Generate code that runs whenever the GROUP BY changes. + ** Changes in the GROUP BY are detected by the previous code + ** block. If there were no changes, this block is skipped. + ** + ** This code copies current group by terms in b0,b1,b2,... + ** over to a0,a1,a2. It then calls the output subroutine + ** and resets the aggregate accumulator registers in preparation + ** for the next GROUP BY batch. + */ + sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr); + sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow); + VdbeComment((v, "output one row")); + sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd); + VdbeComment((v, "check abort flag")); + sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset); + VdbeComment((v, "reset accumulator")); + + /* Update the aggregate accumulators based on the content of + ** the current row + */ + sqlite3VdbeJumpHere(v, j1); + updateAccumulator(pParse, &sAggInfo); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag); + VdbeComment((v, "indicate data in accumulator")); + + /* End of the loop + */ + if( groupBySort ){ + sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop); + }else{ + sqlite3WhereEnd(pWInfo); + sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1); + } + + /* Output the final row of result + */ + sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow); + VdbeComment((v, "output final row")); + + /* Jump over the subroutines + */ + sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd); + + /* Generate a subroutine that outputs a single row of the result + ** set. This subroutine first looks at the iUseFlag. If iUseFlag + ** is less than or equal to zero, the subroutine is a no-op. If + ** the processing calls for the query to abort, this subroutine + ** increments the iAbortFlag memory location before returning in + ** order to signal the caller to abort. + */ + addrSetAbort = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag); + VdbeComment((v, "set abort flag")); + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); + sqlite3VdbeResolveLabel(v, addrOutputRow); + addrOutputRow = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2); + VdbeComment((v, "Groupby result generator entry point")); + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); + finalizeAggFunctions(pParse, &sAggInfo); + sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL); + selectInnerLoop(pParse, p, p->pEList, 0, 0, pOrderBy, + distinct, pDest, + addrOutputRow+1, addrSetAbort); + sqlite3VdbeAddOp1(v, OP_Return, regOutputRow); + VdbeComment((v, "end groupby result generator")); + + /* Generate a subroutine that will reset the group-by accumulator + */ + sqlite3VdbeResolveLabel(v, addrReset); + resetAccumulator(pParse, &sAggInfo); + sqlite3VdbeAddOp1(v, OP_Return, regReset); + + } /* endif pGroupBy. Begin aggregate queries without GROUP BY: */ + else { + ExprList *pDel = 0; +#ifndef SQLITE_OMIT_BTREECOUNT + Table *pTab; + if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){ + /* If isSimpleCount() returns a pointer to a Table structure, then + ** the SQL statement is of the form: + ** + ** SELECT count(*) FROM + ** + ** where the Table structure returned represents table . + ** + ** This statement is so common that it is optimized specially. The + ** OP_Count instruction is executed either on the intkey table that + ** contains the data for table or on one of its indexes. It + ** is better to execute the op on an index, as indexes are almost + ** always spread across less pages than their corresponding tables. + */ + const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + const int iCsr = pParse->nTab++; /* Cursor to scan b-tree */ + Index *pIdx; /* Iterator variable */ + KeyInfo *pKeyInfo = 0; /* Keyinfo for scanned index */ + Index *pBest = 0; /* Best index found so far */ + int iRoot = pTab->tnum; /* Root page of scanned b-tree */ + + sqlite3CodeVerifySchema(pParse, iDb); + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); + + /* Search for the index that has the least amount of columns. If + ** there is such an index, and it has less columns than the table + ** does, then we can assume that it consumes less space on disk and + ** will therefore be cheaper to scan to determine the query result. + ** In this case set iRoot to the root page number of the index b-tree + ** and pKeyInfo to the KeyInfo structure required to navigate the + ** index. + ** + ** In practice the KeyInfo structure will not be used. It is only + ** passed to keep OP_OpenRead happy. + */ + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( !pBest || pIdx->nColumnnColumn ){ + pBest = pIdx; + } + } + if( pBest && pBest->nColumnnCol ){ + iRoot = pBest->tnum; + pKeyInfo = sqlite3IndexKeyinfo(pParse, pBest); + } + + /* Open a read-only cursor, execute the OP_Count, close the cursor. */ + sqlite3VdbeAddOp3(v, OP_OpenRead, iCsr, iRoot, iDb); + if( pKeyInfo ){ + sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO_HANDOFF); + } + sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem); + sqlite3VdbeAddOp1(v, OP_Close, iCsr); + explainSimpleCount(pParse, pTab, pBest); + }else +#endif /* SQLITE_OMIT_BTREECOUNT */ + { + /* Check if the query is of one of the following forms: + ** + ** SELECT min(x) FROM ... + ** SELECT max(x) FROM ... + ** + ** If it is, then ask the code in where.c to attempt to sort results + ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause. + ** If where.c is able to produce results sorted in this order, then + ** add vdbe code to break out of the processing loop after the + ** first iteration (since the first iteration of the loop is + ** guaranteed to operate on the row with the minimum or maximum + ** value of x, the only row required). + ** + ** A special flag must be passed to sqlite3WhereBegin() to slightly + ** modify behaviour as follows: + ** + ** + If the query is a "SELECT min(x)", then the loop coded by + ** where.c should not iterate over any values with a NULL value + ** for x. + ** + ** + The optimizer code in where.c (the thing that decides which + ** index or indices to use) should place a different priority on + ** satisfying the 'ORDER BY' clause than it does in other cases. + ** Refer to code and comments in where.c for details. + */ + ExprList *pMinMax = 0; + u8 flag = minMaxQuery(p); + if( flag ){ + assert( !ExprHasProperty(p->pEList->a[0].pExpr, EP_xIsSelect) ); + pMinMax = sqlite3ExprListDup(db, p->pEList->a[0].pExpr->x.pList,0); + pDel = pMinMax; + if( pMinMax && !db->mallocFailed ){ + pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0; + pMinMax->a[0].pExpr->op = TK_COLUMN; + } + } + + /* This case runs if the aggregate has no GROUP BY clause. The + ** processing is much simpler since there is only a single row + ** of output. + */ + resetAccumulator(pParse, &sAggInfo); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, &pMinMax, flag); + if( pWInfo==0 ){ + sqlite3ExprListDelete(db, pDel); + goto select_end; + } + updateAccumulator(pParse, &sAggInfo); + if( !pMinMax && flag ){ + sqlite3VdbeAddOp2(v, OP_Goto, 0, pWInfo->iBreak); + VdbeComment((v, "%s() by index", + (flag==WHERE_ORDERBY_MIN?"min":"max"))); + } + sqlite3WhereEnd(pWInfo); + finalizeAggFunctions(pParse, &sAggInfo); + } + + pOrderBy = 0; + sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL); + selectInnerLoop(pParse, p, p->pEList, 0, 0, 0, -1, + pDest, addrEnd, addrEnd); + sqlite3ExprListDelete(db, pDel); + } + sqlite3VdbeResolveLabel(v, addrEnd); + + } /* endif aggregate query */ + + if( distinct>=0 ){ + explainTempTable(pParse, "DISTINCT"); + } + + /* If there is an ORDER BY clause, then we need to sort the results + ** and send them to the callback one by one. + */ + if( pOrderBy ){ + explainTempTable(pParse, "ORDER BY"); + generateSortTail(pParse, p, v, pEList->nExpr, pDest); + } + + /* Jump here to skip this query + */ + sqlite3VdbeResolveLabel(v, iEnd); + + /* The SELECT was successfully coded. Set the return code to 0 + ** to indicate no errors. + */ + rc = 0; + + /* Control jumps to here if an error is encountered above, or upon + ** successful coding of the SELECT. + */ +select_end: + explainSetInteger(pParse->iSelectId, iRestoreSelectId); + + /* Identify column names if results of the SELECT are to be output. + */ + if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){ + generateColumnNames(pParse, pTabList, pEList); + } + + sqlite3DbFree(db, sAggInfo.aCol); + sqlite3DbFree(db, sAggInfo.aFunc); + return rc; +} + +#if defined(SQLITE_DEBUG) +/* +******************************************************************************* +** The following code is used for testing and debugging only. The code +** that follows does not appear in normal builds. +** +** These routines are used to print out the content of all or part of a +** parse structures such as Select or Expr. Such printouts are useful +** for helping to understand what is happening inside the code generator +** during the execution of complex SELECT statements. +** +** These routine are not called anywhere from within the normal +** code base. Then are intended to be called from within the debugger +** or from temporary "printf" statements inserted for debugging. +*/ +SQLITE_PRIVATE void sqlite3PrintExpr(Expr *p){ + if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){ + sqlite3DebugPrintf("(%s", p->u.zToken); + }else{ + sqlite3DebugPrintf("(%d", p->op); + } + if( p->pLeft ){ + sqlite3DebugPrintf(" "); + sqlite3PrintExpr(p->pLeft); + } + if( p->pRight ){ + sqlite3DebugPrintf(" "); + sqlite3PrintExpr(p->pRight); + } + sqlite3DebugPrintf(")"); +} +SQLITE_PRIVATE void sqlite3PrintExprList(ExprList *pList){ + int i; + for(i=0; inExpr; i++){ + sqlite3PrintExpr(pList->a[i].pExpr); + if( inExpr-1 ){ + sqlite3DebugPrintf(", "); + } + } +} +SQLITE_PRIVATE void sqlite3PrintSelect(Select *p, int indent){ + sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p); + sqlite3PrintExprList(p->pEList); + sqlite3DebugPrintf("\n"); + if( p->pSrc ){ + char *zPrefix; + int i; + zPrefix = "FROM"; + for(i=0; ipSrc->nSrc; i++){ + struct SrcList_item *pItem = &p->pSrc->a[i]; + sqlite3DebugPrintf("%*s ", indent+6, zPrefix); + zPrefix = ""; + if( pItem->pSelect ){ + sqlite3DebugPrintf("(\n"); + sqlite3PrintSelect(pItem->pSelect, indent+10); + sqlite3DebugPrintf("%*s)", indent+8, ""); + }else if( pItem->zName ){ + sqlite3DebugPrintf("%s", pItem->zName); + } + if( pItem->pTab ){ + sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName); + } + if( pItem->zAlias ){ + sqlite3DebugPrintf(" AS %s", pItem->zAlias); + } + if( ipSrc->nSrc-1 ){ + sqlite3DebugPrintf(","); + } + sqlite3DebugPrintf("\n"); + } + } + if( p->pWhere ){ + sqlite3DebugPrintf("%*s WHERE ", indent, ""); + sqlite3PrintExpr(p->pWhere); + sqlite3DebugPrintf("\n"); + } + if( p->pGroupBy ){ + sqlite3DebugPrintf("%*s GROUP BY ", indent, ""); + sqlite3PrintExprList(p->pGroupBy); + sqlite3DebugPrintf("\n"); + } + if( p->pHaving ){ + sqlite3DebugPrintf("%*s HAVING ", indent, ""); + sqlite3PrintExpr(p->pHaving); + sqlite3DebugPrintf("\n"); + } + if( p->pOrderBy ){ + sqlite3DebugPrintf("%*s ORDER BY ", indent, ""); + sqlite3PrintExprList(p->pOrderBy); + sqlite3DebugPrintf("\n"); + } +} +/* End of the structure debug printing code +*****************************************************************************/ +#endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */ + +/************** End of select.c **********************************************/ +/************** Begin file table.c *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains the sqlite3_get_table() and sqlite3_free_table() +** interface routines. These are just wrappers around the main +** interface routine of sqlite3_exec(). +** +** These routines are in a separate files so that they will not be linked +** if they are not used. +*/ + +#ifndef SQLITE_OMIT_GET_TABLE + +/* +** This structure is used to pass data from sqlite3_get_table() through +** to the callback function is uses to build the result. +*/ +typedef struct TabResult { + char **azResult; /* Accumulated output */ + char *zErrMsg; /* Error message text, if an error occurs */ + int nAlloc; /* Slots allocated for azResult[] */ + int nRow; /* Number of rows in the result */ + int nColumn; /* Number of columns in the result */ + int nData; /* Slots used in azResult[]. (nRow+1)*nColumn */ + int rc; /* Return code from sqlite3_exec() */ +} TabResult; + +/* +** This routine is called once for each row in the result table. Its job +** is to fill in the TabResult structure appropriately, allocating new +** memory as necessary. +*/ +static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){ + TabResult *p = (TabResult*)pArg; /* Result accumulator */ + int need; /* Slots needed in p->azResult[] */ + int i; /* Loop counter */ + char *z; /* A single column of result */ + + /* Make sure there is enough space in p->azResult to hold everything + ** we need to remember from this invocation of the callback. + */ + if( p->nRow==0 && argv!=0 ){ + need = nCol*2; + }else{ + need = nCol; + } + if( p->nData + need > p->nAlloc ){ + char **azNew; + p->nAlloc = p->nAlloc*2 + need; + azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc ); + if( azNew==0 ) goto malloc_failed; + p->azResult = azNew; + } + + /* If this is the first row, then generate an extra row containing + ** the names of all columns. + */ + if( p->nRow==0 ){ + p->nColumn = nCol; + for(i=0; iazResult[p->nData++] = z; + } + }else if( p->nColumn!=nCol ){ + sqlite3_free(p->zErrMsg); + p->zErrMsg = sqlite3_mprintf( + "sqlite3_get_table() called with two or more incompatible queries" + ); + p->rc = SQLITE_ERROR; + return 1; + } + + /* Copy over the row data + */ + if( argv!=0 ){ + for(i=0; iazResult[p->nData++] = z; + } + p->nRow++; + } + return 0; + +malloc_failed: + p->rc = SQLITE_NOMEM; + return 1; +} + +/* +** Query the database. But instead of invoking a callback for each row, +** malloc() for space to hold the result and return the entire results +** at the conclusion of the call. +** +** The result that is written to ***pazResult is held in memory obtained +** from malloc(). But the caller cannot free this memory directly. +** Instead, the entire table should be passed to sqlite3_free_table() when +** the calling procedure is finished using it. +*/ +SQLITE_API int sqlite3_get_table( + sqlite3 *db, /* The database on which the SQL executes */ + const char *zSql, /* The SQL to be executed */ + char ***pazResult, /* Write the result table here */ + int *pnRow, /* Write the number of rows in the result here */ + int *pnColumn, /* Write the number of columns of result here */ + char **pzErrMsg /* Write error messages here */ +){ + int rc; + TabResult res; + + *pazResult = 0; + if( pnColumn ) *pnColumn = 0; + if( pnRow ) *pnRow = 0; + if( pzErrMsg ) *pzErrMsg = 0; + res.zErrMsg = 0; + res.nRow = 0; + res.nColumn = 0; + res.nData = 1; + res.nAlloc = 20; + res.rc = SQLITE_OK; + res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc ); + if( res.azResult==0 ){ + db->errCode = SQLITE_NOMEM; + return SQLITE_NOMEM; + } + res.azResult[0] = 0; + rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg); + assert( sizeof(res.azResult[0])>= sizeof(res.nData) ); + res.azResult[0] = SQLITE_INT_TO_PTR(res.nData); + if( (rc&0xff)==SQLITE_ABORT ){ + sqlite3_free_table(&res.azResult[1]); + if( res.zErrMsg ){ + if( pzErrMsg ){ + sqlite3_free(*pzErrMsg); + *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg); + } + sqlite3_free(res.zErrMsg); + } + db->errCode = res.rc; /* Assume 32-bit assignment is atomic */ + return res.rc; + } + sqlite3_free(res.zErrMsg); + if( rc!=SQLITE_OK ){ + sqlite3_free_table(&res.azResult[1]); + return rc; + } + if( res.nAlloc>res.nData ){ + char **azNew; + azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData ); + if( azNew==0 ){ + sqlite3_free_table(&res.azResult[1]); + db->errCode = SQLITE_NOMEM; + return SQLITE_NOMEM; + } + res.azResult = azNew; + } + *pazResult = &res.azResult[1]; + if( pnColumn ) *pnColumn = res.nColumn; + if( pnRow ) *pnRow = res.nRow; + return rc; +} + +/* +** This routine frees the space the sqlite3_get_table() malloced. +*/ +SQLITE_API void sqlite3_free_table( + char **azResult /* Result returned from from sqlite3_get_table() */ +){ + if( azResult ){ + int i, n; + azResult--; + assert( azResult!=0 ); + n = SQLITE_PTR_TO_INT(azResult[0]); + for(i=1; ipNext; + + sqlite3ExprDelete(db, pTmp->pWhere); + sqlite3ExprListDelete(db, pTmp->pExprList); + sqlite3SelectDelete(db, pTmp->pSelect); + sqlite3IdListDelete(db, pTmp->pIdList); + + sqlite3DbFree(db, pTmp); + } +} + +/* +** Given table pTab, return a list of all the triggers attached to +** the table. The list is connected by Trigger.pNext pointers. +** +** All of the triggers on pTab that are in the same database as pTab +** are already attached to pTab->pTrigger. But there might be additional +** triggers on pTab in the TEMP schema. This routine prepends all +** TEMP triggers on pTab to the beginning of the pTab->pTrigger list +** and returns the combined list. +** +** To state it another way: This routine returns a list of all triggers +** that fire off of pTab. The list will include any TEMP triggers on +** pTab as well as the triggers lised in pTab->pTrigger. +*/ +SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){ + Schema * const pTmpSchema = pParse->db->aDb[1].pSchema; + Trigger *pList = 0; /* List of triggers to return */ + + if( pParse->disableTriggers ){ + return 0; + } + + if( pTmpSchema!=pTab->pSchema ){ + HashElem *p; + for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){ + Trigger *pTrig = (Trigger *)sqliteHashData(p); + if( pTrig->pTabSchema==pTab->pSchema + && 0==sqlite3StrICmp(pTrig->table, pTab->zName) + ){ + pTrig->pNext = (pList ? pList : pTab->pTrigger); + pList = pTrig; + } + } + } + + return (pList ? pList : pTab->pTrigger); +} + +/* +** This is called by the parser when it sees a CREATE TRIGGER statement +** up to the point of the BEGIN before the trigger actions. A Trigger +** structure is generated based on the information available and stored +** in pParse->pNewTrigger. After the trigger actions have been parsed, the +** sqlite3FinishTrigger() function is called to complete the trigger +** construction process. +*/ +SQLITE_PRIVATE void sqlite3BeginTrigger( + Parse *pParse, /* The parse context of the CREATE TRIGGER statement */ + Token *pName1, /* The name of the trigger */ + Token *pName2, /* The name of the trigger */ + int tr_tm, /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */ + int op, /* One of TK_INSERT, TK_UPDATE, TK_DELETE */ + IdList *pColumns, /* column list if this is an UPDATE OF trigger */ + SrcList *pTableName,/* The name of the table/view the trigger applies to */ + Expr *pWhen, /* WHEN clause */ + int isTemp, /* True if the TEMPORARY keyword is present */ + int noErr /* Suppress errors if the trigger already exists */ +){ + Trigger *pTrigger = 0; /* The new trigger */ + Table *pTab; /* Table that the trigger fires off of */ + char *zName = 0; /* Name of the trigger */ + sqlite3 *db = pParse->db; /* The database connection */ + int iDb; /* The database to store the trigger in */ + Token *pName; /* The unqualified db name */ + DbFixer sFix; /* State vector for the DB fixer */ + int iTabDb; /* Index of the database holding pTab */ + + assert( pName1!=0 ); /* pName1->z might be NULL, but not pName1 itself */ + assert( pName2!=0 ); + assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE ); + assert( op>0 && op<0xff ); + if( isTemp ){ + /* If TEMP was specified, then the trigger name may not be qualified. */ + if( pName2->n>0 ){ + sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name"); + goto trigger_cleanup; + } + iDb = 1; + pName = pName1; + }else{ + /* Figure out the db that the the trigger will be created in */ + iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName); + if( iDb<0 ){ + goto trigger_cleanup; + } + } + + /* If the trigger name was unqualified, and the table is a temp table, + ** then set iDb to 1 to create the trigger in the temporary database. + ** If sqlite3SrcListLookup() returns 0, indicating the table does not + ** exist, the error is caught by the block below. + */ + if( !pTableName || db->mallocFailed ){ + goto trigger_cleanup; + } + pTab = sqlite3SrcListLookup(pParse, pTableName); + if( db->init.busy==0 && pName2->n==0 && pTab + && pTab->pSchema==db->aDb[1].pSchema ){ + iDb = 1; + } + + /* Ensure the table name matches database name and that the table exists */ + if( db->mallocFailed ) goto trigger_cleanup; + assert( pTableName->nSrc==1 ); + if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName) && + sqlite3FixSrcList(&sFix, pTableName) ){ + goto trigger_cleanup; + } + pTab = sqlite3SrcListLookup(pParse, pTableName); + if( !pTab ){ + /* The table does not exist. */ + if( db->init.iDb==1 ){ + /* Ticket #3810. + ** Normally, whenever a table is dropped, all associated triggers are + ** dropped too. But if a TEMP trigger is created on a non-TEMP table + ** and the table is dropped by a different database connection, the + ** trigger is not visible to the database connection that does the + ** drop so the trigger cannot be dropped. This results in an + ** "orphaned trigger" - a trigger whose associated table is missing. + */ + db->init.orphanTrigger = 1; + } + goto trigger_cleanup; + } + if( IsVirtual(pTab) ){ + sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables"); + goto trigger_cleanup; + } + + /* Check that the trigger name is not reserved and that no trigger of the + ** specified name exists */ + zName = sqlite3NameFromToken(db, pName); + if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ + goto trigger_cleanup; + } + if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash), + zName, sqlite3Strlen30(zName)) ){ + if( !noErr ){ + sqlite3ErrorMsg(pParse, "trigger %T already exists", pName); + } + goto trigger_cleanup; + } + + /* Do not create a trigger on a system table */ + if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){ + sqlite3ErrorMsg(pParse, "cannot create trigger on system table"); + pParse->nErr++; + goto trigger_cleanup; + } + + /* INSTEAD of triggers are only for views and views only support INSTEAD + ** of triggers. + */ + if( pTab->pSelect && tr_tm!=TK_INSTEAD ){ + sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S", + (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0); + goto trigger_cleanup; + } + if( !pTab->pSelect && tr_tm==TK_INSTEAD ){ + sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF" + " trigger on table: %S", pTableName, 0); + goto trigger_cleanup; + } + iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema); + +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int code = SQLITE_CREATE_TRIGGER; + const char *zDb = db->aDb[iTabDb].zName; + const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb; + if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER; + if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){ + goto trigger_cleanup; + } + if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){ + goto trigger_cleanup; + } + } +#endif + + /* INSTEAD OF triggers can only appear on views and BEFORE triggers + ** cannot appear on views. So we might as well translate every + ** INSTEAD OF trigger into a BEFORE trigger. It simplifies code + ** elsewhere. + */ + if (tr_tm == TK_INSTEAD){ + tr_tm = TK_BEFORE; + } + + /* Build the Trigger object */ + pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger)); + if( pTrigger==0 ) goto trigger_cleanup; + pTrigger->zName = zName; + zName = 0; + pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName); + pTrigger->pSchema = db->aDb[iDb].pSchema; + pTrigger->pTabSchema = pTab->pSchema; + pTrigger->op = (u8)op; + pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER; + pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE); + pTrigger->pColumns = sqlite3IdListDup(db, pColumns); + assert( pParse->pNewTrigger==0 ); + pParse->pNewTrigger = pTrigger; + +trigger_cleanup: + sqlite3DbFree(db, zName); + sqlite3SrcListDelete(db, pTableName); + sqlite3IdListDelete(db, pColumns); + sqlite3ExprDelete(db, pWhen); + if( !pParse->pNewTrigger ){ + sqlite3DeleteTrigger(db, pTrigger); + }else{ + assert( pParse->pNewTrigger==pTrigger ); + } +} + +/* +** This routine is called after all of the trigger actions have been parsed +** in order to complete the process of building the trigger. +*/ +SQLITE_PRIVATE void sqlite3FinishTrigger( + Parse *pParse, /* Parser context */ + TriggerStep *pStepList, /* The triggered program */ + Token *pAll /* Token that describes the complete CREATE TRIGGER */ +){ + Trigger *pTrig = pParse->pNewTrigger; /* Trigger being finished */ + char *zName; /* Name of trigger */ + sqlite3 *db = pParse->db; /* The database */ + DbFixer sFix; /* Fixer object */ + int iDb; /* Database containing the trigger */ + Token nameToken; /* Trigger name for error reporting */ + + pParse->pNewTrigger = 0; + if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup; + zName = pTrig->zName; + iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema); + pTrig->step_list = pStepList; + while( pStepList ){ + pStepList->pTrig = pTrig; + pStepList = pStepList->pNext; + } + nameToken.z = pTrig->zName; + nameToken.n = sqlite3Strlen30(nameToken.z); + if( sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken) + && sqlite3FixTriggerStep(&sFix, pTrig->step_list) ){ + goto triggerfinish_cleanup; + } + + /* if we are not initializing, + ** build the sqlite_master entry + */ + if( !db->init.busy ){ + Vdbe *v; + char *z; + + /* Make an entry in the sqlite_master table */ + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto triggerfinish_cleanup; + sqlite3BeginWriteOperation(pParse, 0, iDb); + z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n); + sqlite3NestedParse(pParse, + "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName, + pTrig->table, z); + sqlite3DbFree(db, z); + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 0, 0, sqlite3MPrintf( + db, "type='trigger' AND name='%q'", zName), P4_DYNAMIC + ); + } + + if( db->init.busy ){ + Trigger *pLink = pTrig; + Hash *pHash = &db->aDb[iDb].pSchema->trigHash; + pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig); + if( pTrig ){ + db->mallocFailed = 1; + }else if( pLink->pSchema==pLink->pTabSchema ){ + Table *pTab; + int n = sqlite3Strlen30(pLink->table); + pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n); + assert( pTab!=0 ); + pLink->pNext = pTab->pTrigger; + pTab->pTrigger = pLink; + } + } + +triggerfinish_cleanup: + sqlite3DeleteTrigger(db, pTrig); + assert( !pParse->pNewTrigger ); + sqlite3DeleteTriggerStep(db, pStepList); +} + +/* +** Turn a SELECT statement (that the pSelect parameter points to) into +** a trigger step. Return a pointer to a TriggerStep structure. +** +** The parser calls this routine when it finds a SELECT statement in +** body of a TRIGGER. +*/ +SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){ + TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep)); + if( pTriggerStep==0 ) { + sqlite3SelectDelete(db, pSelect); + return 0; + } + pTriggerStep->op = TK_SELECT; + pTriggerStep->pSelect = pSelect; + pTriggerStep->orconf = OE_Default; + return pTriggerStep; +} + +/* +** Allocate space to hold a new trigger step. The allocated space +** holds both the TriggerStep object and the TriggerStep.target.z string. +** +** If an OOM error occurs, NULL is returned and db->mallocFailed is set. +*/ +static TriggerStep *triggerStepAllocate( + sqlite3 *db, /* Database connection */ + u8 op, /* Trigger opcode */ + Token *pName /* The target name */ +){ + TriggerStep *pTriggerStep; + + pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n); + if( pTriggerStep ){ + char *z = (char*)&pTriggerStep[1]; + memcpy(z, pName->z, pName->n); + pTriggerStep->target.z = z; + pTriggerStep->target.n = pName->n; + pTriggerStep->op = op; + } + return pTriggerStep; +} + +/* +** Build a trigger step out of an INSERT statement. Return a pointer +** to the new trigger step. +** +** The parser calls this routine when it sees an INSERT inside the +** body of a trigger. +*/ +SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep( + sqlite3 *db, /* The database connection */ + Token *pTableName, /* Name of the table into which we insert */ + IdList *pColumn, /* List of columns in pTableName to insert into */ + ExprList *pEList, /* The VALUE clause: a list of values to be inserted */ + Select *pSelect, /* A SELECT statement that supplies values */ + u8 orconf /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */ +){ + TriggerStep *pTriggerStep; + + assert(pEList == 0 || pSelect == 0); + assert(pEList != 0 || pSelect != 0 || db->mallocFailed); + + pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName); + if( pTriggerStep ){ + pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE); + pTriggerStep->pIdList = pColumn; + pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE); + pTriggerStep->orconf = orconf; + }else{ + sqlite3IdListDelete(db, pColumn); + } + sqlite3ExprListDelete(db, pEList); + sqlite3SelectDelete(db, pSelect); + + return pTriggerStep; +} + +/* +** Construct a trigger step that implements an UPDATE statement and return +** a pointer to that trigger step. The parser calls this routine when it +** sees an UPDATE statement inside the body of a CREATE TRIGGER. +*/ +SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep( + sqlite3 *db, /* The database connection */ + Token *pTableName, /* Name of the table to be updated */ + ExprList *pEList, /* The SET clause: list of column and new values */ + Expr *pWhere, /* The WHERE clause */ + u8 orconf /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */ +){ + TriggerStep *pTriggerStep; + + pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName); + if( pTriggerStep ){ + pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE); + pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); + pTriggerStep->orconf = orconf; + } + sqlite3ExprListDelete(db, pEList); + sqlite3ExprDelete(db, pWhere); + return pTriggerStep; +} + +/* +** Construct a trigger step that implements a DELETE statement and return +** a pointer to that trigger step. The parser calls this routine when it +** sees a DELETE statement inside the body of a CREATE TRIGGER. +*/ +SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep( + sqlite3 *db, /* Database connection */ + Token *pTableName, /* The table from which rows are deleted */ + Expr *pWhere /* The WHERE clause */ +){ + TriggerStep *pTriggerStep; + + pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName); + if( pTriggerStep ){ + pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE); + pTriggerStep->orconf = OE_Default; + } + sqlite3ExprDelete(db, pWhere); + return pTriggerStep; +} + +/* +** Recursively delete a Trigger structure +*/ +SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){ + if( pTrigger==0 ) return; + sqlite3DeleteTriggerStep(db, pTrigger->step_list); + sqlite3DbFree(db, pTrigger->zName); + sqlite3DbFree(db, pTrigger->table); + sqlite3ExprDelete(db, pTrigger->pWhen); + sqlite3IdListDelete(db, pTrigger->pColumns); + sqlite3DbFree(db, pTrigger); +} + +/* +** This function is called to drop a trigger from the database schema. +** +** This may be called directly from the parser and therefore identifies +** the trigger by name. The sqlite3DropTriggerPtr() routine does the +** same job as this routine except it takes a pointer to the trigger +** instead of the trigger name. +**/ +SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){ + Trigger *pTrigger = 0; + int i; + const char *zDb; + const char *zName; + int nName; + sqlite3 *db = pParse->db; + + if( db->mallocFailed ) goto drop_trigger_cleanup; + if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){ + goto drop_trigger_cleanup; + } + + assert( pName->nSrc==1 ); + zDb = pName->a[0].zDatabase; + zName = pName->a[0].zName; + nName = sqlite3Strlen30(zName); + for(i=OMIT_TEMPDB; inDb; i++){ + int j = (i<2) ? i^1 : i; /* Search TEMP before MAIN */ + if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue; + pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName); + if( pTrigger ) break; + } + if( !pTrigger ){ + if( !noErr ){ + sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0); + } + pParse->checkSchema = 1; + goto drop_trigger_cleanup; + } + sqlite3DropTriggerPtr(pParse, pTrigger); + +drop_trigger_cleanup: + sqlite3SrcListDelete(db, pName); +} + +/* +** Return a pointer to the Table structure for the table that a trigger +** is set on. +*/ +static Table *tableOfTrigger(Trigger *pTrigger){ + int n = sqlite3Strlen30(pTrigger->table); + return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n); +} + + +/* +** Drop a trigger given a pointer to that trigger. +*/ +SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){ + Table *pTable; + Vdbe *v; + sqlite3 *db = pParse->db; + int iDb; + + iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema); + assert( iDb>=0 && iDbnDb ); + pTable = tableOfTrigger(pTrigger); + assert( pTable ); + assert( pTable->pSchema==pTrigger->pSchema || iDb==1 ); +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int code = SQLITE_DROP_TRIGGER; + const char *zDb = db->aDb[iDb].zName; + const char *zTab = SCHEMA_TABLE(iDb); + if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER; + if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) || + sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){ + return; + } + } +#endif + + /* Generate code to destroy the database record of the trigger. + */ + assert( pTable!=0 ); + if( (v = sqlite3GetVdbe(pParse))!=0 ){ + int base; + static const VdbeOpList dropTrigger[] = { + { OP_Rewind, 0, ADDR(9), 0}, + { OP_String8, 0, 1, 0}, /* 1 */ + { OP_Column, 0, 1, 2}, + { OP_Ne, 2, ADDR(8), 1}, + { OP_String8, 0, 1, 0}, /* 4: "trigger" */ + { OP_Column, 0, 0, 2}, + { OP_Ne, 2, ADDR(8), 1}, + { OP_Delete, 0, 0, 0}, + { OP_Next, 0, ADDR(1), 0}, /* 8 */ + }; + + sqlite3BeginWriteOperation(pParse, 0, iDb); + sqlite3OpenMasterTable(pParse, iDb); + base = sqlite3VdbeAddOpList(v, ArraySize(dropTrigger), dropTrigger); + sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, 0); + sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC); + sqlite3ChangeCookie(pParse, iDb); + sqlite3VdbeAddOp2(v, OP_Close, 0, 0); + sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0); + if( pParse->nMem<3 ){ + pParse->nMem = 3; + } + } +} + +/* +** Remove a trigger from the hash tables of the sqlite* pointer. +*/ +SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){ + Hash *pHash = &(db->aDb[iDb].pSchema->trigHash); + Trigger *pTrigger; + pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0); + if( ALWAYS(pTrigger) ){ + if( pTrigger->pSchema==pTrigger->pTabSchema ){ + Table *pTab = tableOfTrigger(pTrigger); + Trigger **pp; + for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext)); + *pp = (*pp)->pNext; + } + sqlite3DeleteTrigger(db, pTrigger); + db->flags |= SQLITE_InternChanges; + } +} + +/* +** pEList is the SET clause of an UPDATE statement. Each entry +** in pEList is of the format =. If any of the entries +** in pEList have an which matches an identifier in pIdList, +** then return TRUE. If pIdList==NULL, then it is considered a +** wildcard that matches anything. Likewise if pEList==NULL then +** it matches anything so always return true. Return false only +** if there is no match. +*/ +static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){ + int e; + if( pIdList==0 || NEVER(pEList==0) ) return 1; + for(e=0; enExpr; e++){ + if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1; + } + return 0; +} + +/* +** Return a list of all triggers on table pTab if there exists at least +** one trigger that must be fired when an operation of type 'op' is +** performed on the table, and, if that operation is an UPDATE, if at +** least one of the columns in pChanges is being modified. +*/ +SQLITE_PRIVATE Trigger *sqlite3TriggersExist( + Parse *pParse, /* Parse context */ + Table *pTab, /* The table the contains the triggers */ + int op, /* one of TK_DELETE, TK_INSERT, TK_UPDATE */ + ExprList *pChanges, /* Columns that change in an UPDATE statement */ + int *pMask /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ +){ + int mask = 0; + Trigger *pList = sqlite3TriggerList(pParse, pTab); + Trigger *p; + assert( pList==0 || IsVirtual(pTab)==0 ); + for(p=pList; p; p=p->pNext){ + if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){ + mask |= p->tr_tm; + } + } + if( pMask ){ + *pMask = mask; + } + return (mask ? pList : 0); +} + +/* +** Convert the pStep->target token into a SrcList and return a pointer +** to that SrcList. +** +** This routine adds a specific database name, if needed, to the target when +** forming the SrcList. This prevents a trigger in one database from +** referring to a target in another database. An exception is when the +** trigger is in TEMP in which case it can refer to any other database it +** wants. +*/ +static SrcList *targetSrcList( + Parse *pParse, /* The parsing context */ + TriggerStep *pStep /* The trigger containing the target token */ +){ + int iDb; /* Index of the database to use */ + SrcList *pSrc; /* SrcList to be returned */ + + pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0); + if( pSrc ){ + assert( pSrc->nSrc>0 ); + assert( pSrc->a!=0 ); + iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema); + if( iDb==0 || iDb>=2 ){ + sqlite3 *db = pParse->db; + assert( iDbdb->nDb ); + pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName); + } + } + return pSrc; +} + +/* +** Generate VDBE code for the statements inside the body of a single +** trigger. +*/ +static int codeTriggerProgram( + Parse *pParse, /* The parser context */ + TriggerStep *pStepList, /* List of statements inside the trigger body */ + int orconf /* Conflict algorithm. (OE_Abort, etc) */ +){ + TriggerStep *pStep; + Vdbe *v = pParse->pVdbe; + sqlite3 *db = pParse->db; + + assert( pParse->pTriggerTab && pParse->pToplevel ); + assert( pStepList ); + assert( v!=0 ); + for(pStep=pStepList; pStep; pStep=pStep->pNext){ + /* Figure out the ON CONFLICT policy that will be used for this step + ** of the trigger program. If the statement that caused this trigger + ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use + ** the ON CONFLICT policy that was specified as part of the trigger + ** step statement. Example: + ** + ** CREATE TRIGGER AFTER INSERT ON t1 BEGIN; + ** INSERT OR REPLACE INTO t2 VALUES(new.a, new.b); + ** END; + ** + ** INSERT INTO t1 ... ; -- insert into t2 uses REPLACE policy + ** INSERT OR IGNORE INTO t1 ... ; -- insert into t2 uses IGNORE policy + */ + pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf; + + switch( pStep->op ){ + case TK_UPDATE: { + sqlite3Update(pParse, + targetSrcList(pParse, pStep), + sqlite3ExprListDup(db, pStep->pExprList, 0), + sqlite3ExprDup(db, pStep->pWhere, 0), + pParse->eOrconf + ); + break; + } + case TK_INSERT: { + sqlite3Insert(pParse, + targetSrcList(pParse, pStep), + sqlite3ExprListDup(db, pStep->pExprList, 0), + sqlite3SelectDup(db, pStep->pSelect, 0), + sqlite3IdListDup(db, pStep->pIdList), + pParse->eOrconf + ); + break; + } + case TK_DELETE: { + sqlite3DeleteFrom(pParse, + targetSrcList(pParse, pStep), + sqlite3ExprDup(db, pStep->pWhere, 0) + ); + break; + } + default: assert( pStep->op==TK_SELECT ); { + SelectDest sDest; + Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0); + sqlite3SelectDestInit(&sDest, SRT_Discard, 0); + sqlite3Select(pParse, pSelect, &sDest); + sqlite3SelectDelete(db, pSelect); + break; + } + } + if( pStep->op!=TK_SELECT ){ + sqlite3VdbeAddOp0(v, OP_ResetCount); + } + } + + return 0; +} + +#ifdef SQLITE_DEBUG +/* +** This function is used to add VdbeComment() annotations to a VDBE +** program. It is not used in production code, only for debugging. +*/ +static const char *onErrorText(int onError){ + switch( onError ){ + case OE_Abort: return "abort"; + case OE_Rollback: return "rollback"; + case OE_Fail: return "fail"; + case OE_Replace: return "replace"; + case OE_Ignore: return "ignore"; + case OE_Default: return "default"; + } + return "n/a"; +} +#endif + +/* +** Parse context structure pFrom has just been used to create a sub-vdbe +** (trigger program). If an error has occurred, transfer error information +** from pFrom to pTo. +*/ +static void transferParseError(Parse *pTo, Parse *pFrom){ + assert( pFrom->zErrMsg==0 || pFrom->nErr ); + assert( pTo->zErrMsg==0 || pTo->nErr ); + if( pTo->nErr==0 ){ + pTo->zErrMsg = pFrom->zErrMsg; + pTo->nErr = pFrom->nErr; + }else{ + sqlite3DbFree(pFrom->db, pFrom->zErrMsg); + } +} + +/* +** Create and populate a new TriggerPrg object with a sub-program +** implementing trigger pTrigger with ON CONFLICT policy orconf. +*/ +static TriggerPrg *codeRowTrigger( + Parse *pParse, /* Current parse context */ + Trigger *pTrigger, /* Trigger to code */ + Table *pTab, /* The table pTrigger is attached to */ + int orconf /* ON CONFLICT policy to code trigger program with */ +){ + Parse *pTop = sqlite3ParseToplevel(pParse); + sqlite3 *db = pParse->db; /* Database handle */ + TriggerPrg *pPrg; /* Value to return */ + Expr *pWhen = 0; /* Duplicate of trigger WHEN expression */ + Vdbe *v; /* Temporary VM */ + NameContext sNC; /* Name context for sub-vdbe */ + SubProgram *pProgram = 0; /* Sub-vdbe for trigger program */ + Parse *pSubParse; /* Parse context for sub-vdbe */ + int iEndTrigger = 0; /* Label to jump to if WHEN is false */ + + assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); + assert( pTop->pVdbe ); + + /* Allocate the TriggerPrg and SubProgram objects. To ensure that they + ** are freed if an error occurs, link them into the Parse.pTriggerPrg + ** list of the top-level Parse object sooner rather than later. */ + pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg)); + if( !pPrg ) return 0; + pPrg->pNext = pTop->pTriggerPrg; + pTop->pTriggerPrg = pPrg; + pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram)); + if( !pProgram ) return 0; + sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram); + pPrg->pTrigger = pTrigger; + pPrg->orconf = orconf; + pPrg->aColmask[0] = 0xffffffff; + pPrg->aColmask[1] = 0xffffffff; + + /* Allocate and populate a new Parse context to use for coding the + ** trigger sub-program. */ + pSubParse = sqlite3StackAllocZero(db, sizeof(Parse)); + if( !pSubParse ) return 0; + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pSubParse; + pSubParse->db = db; + pSubParse->pTriggerTab = pTab; + pSubParse->pToplevel = pTop; + pSubParse->zAuthContext = pTrigger->zName; + pSubParse->eTriggerOp = pTrigger->op; + pSubParse->nQueryLoop = pParse->nQueryLoop; + + v = sqlite3GetVdbe(pSubParse); + if( v ){ + VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)", + pTrigger->zName, onErrorText(orconf), + (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"), + (pTrigger->op==TK_UPDATE ? "UPDATE" : ""), + (pTrigger->op==TK_INSERT ? "INSERT" : ""), + (pTrigger->op==TK_DELETE ? "DELETE" : ""), + pTab->zName + )); +#ifndef SQLITE_OMIT_TRACE + sqlite3VdbeChangeP4(v, -1, + sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC + ); +#endif + + /* If one was specified, code the WHEN clause. If it evaluates to false + ** (or NULL) the sub-vdbe is immediately halted by jumping to the + ** OP_Halt inserted at the end of the program. */ + if( pTrigger->pWhen ){ + pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0); + if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen) + && db->mallocFailed==0 + ){ + iEndTrigger = sqlite3VdbeMakeLabel(v); + sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL); + } + sqlite3ExprDelete(db, pWhen); + } + + /* Code the trigger program into the sub-vdbe. */ + codeTriggerProgram(pSubParse, pTrigger->step_list, orconf); + + /* Insert an OP_Halt at the end of the sub-program. */ + if( iEndTrigger ){ + sqlite3VdbeResolveLabel(v, iEndTrigger); + } + sqlite3VdbeAddOp0(v, OP_Halt); + VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf))); + + transferParseError(pParse, pSubParse); + if( db->mallocFailed==0 ){ + pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg); + } + pProgram->nMem = pSubParse->nMem; + pProgram->nCsr = pSubParse->nTab; + pProgram->token = (void *)pTrigger; + pPrg->aColmask[0] = pSubParse->oldmask; + pPrg->aColmask[1] = pSubParse->newmask; + sqlite3VdbeDelete(v); + } + + assert( !pSubParse->pAinc && !pSubParse->pZombieTab ); + assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg ); + sqlite3StackFree(db, pSubParse); + + return pPrg; +} + +/* +** Return a pointer to a TriggerPrg object containing the sub-program for +** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such +** TriggerPrg object exists, a new object is allocated and populated before +** being returned. +*/ +static TriggerPrg *getRowTrigger( + Parse *pParse, /* Current parse context */ + Trigger *pTrigger, /* Trigger to code */ + Table *pTab, /* The table trigger pTrigger is attached to */ + int orconf /* ON CONFLICT algorithm. */ +){ + Parse *pRoot = sqlite3ParseToplevel(pParse); + TriggerPrg *pPrg; + + assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) ); + + /* It may be that this trigger has already been coded (or is in the + ** process of being coded). If this is the case, then an entry with + ** a matching TriggerPrg.pTrigger field will be present somewhere + ** in the Parse.pTriggerPrg list. Search for such an entry. */ + for(pPrg=pRoot->pTriggerPrg; + pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf); + pPrg=pPrg->pNext + ); + + /* If an existing TriggerPrg could not be located, create a new one. */ + if( !pPrg ){ + pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf); + } + + return pPrg; +} + +/* +** Generate code for the trigger program associated with trigger p on +** table pTab. The reg, orconf and ignoreJump parameters passed to this +** function are the same as those described in the header function for +** sqlite3CodeRowTrigger() +*/ +SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect( + Parse *pParse, /* Parse context */ + Trigger *p, /* Trigger to code */ + Table *pTab, /* The table to code triggers from */ + int reg, /* Reg array containing OLD.* and NEW.* values */ + int orconf, /* ON CONFLICT policy */ + int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ +){ + Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */ + TriggerPrg *pPrg; + pPrg = getRowTrigger(pParse, p, pTab, orconf); + assert( pPrg || pParse->nErr || pParse->db->mallocFailed ); + + /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program + ** is a pointer to the sub-vdbe containing the trigger program. */ + if( pPrg ){ + int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers)); + + sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem); + sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM); + VdbeComment( + (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf))); + + /* Set the P5 operand of the OP_Program instruction to non-zero if + ** recursive invocation of this trigger program is disallowed. Recursive + ** invocation is disallowed if (a) the sub-program is really a trigger, + ** not a foreign key action, and (b) the flag to enable recursive triggers + ** is clear. */ + sqlite3VdbeChangeP5(v, (u8)bRecursive); + } +} + +/* +** This is called to code the required FOR EACH ROW triggers for an operation +** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE) +** is given by the op paramater. The tr_tm parameter determines whether the +** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then +** parameter pChanges is passed the list of columns being modified. +** +** If there are no triggers that fire at the specified time for the specified +** operation on pTab, this function is a no-op. +** +** The reg argument is the address of the first in an array of registers +** that contain the values substituted for the new.* and old.* references +** in the trigger program. If N is the number of columns in table pTab +** (a copy of pTab->nCol), then registers are populated as follows: +** +** Register Contains +** ------------------------------------------------------ +** reg+0 OLD.rowid +** reg+1 OLD.* value of left-most column of pTab +** ... ... +** reg+N OLD.* value of right-most column of pTab +** reg+N+1 NEW.rowid +** reg+N+2 OLD.* value of left-most column of pTab +** ... ... +** reg+N+N+1 NEW.* value of right-most column of pTab +** +** For ON DELETE triggers, the registers containing the NEW.* values will +** never be accessed by the trigger program, so they are not allocated or +** populated by the caller (there is no data to populate them with anyway). +** Similarly, for ON INSERT triggers the values stored in the OLD.* registers +** are never accessed, and so are not allocated by the caller. So, for an +** ON INSERT trigger, the value passed to this function as parameter reg +** is not a readable register, although registers (reg+N) through +** (reg+N+N+1) are. +** +** Parameter orconf is the default conflict resolution algorithm for the +** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump +** is the instruction that control should jump to if a trigger program +** raises an IGNORE exception. +*/ +SQLITE_PRIVATE void sqlite3CodeRowTrigger( + Parse *pParse, /* Parse context */ + Trigger *pTrigger, /* List of triggers on table pTab */ + int op, /* One of TK_UPDATE, TK_INSERT, TK_DELETE */ + ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ + int tr_tm, /* One of TRIGGER_BEFORE, TRIGGER_AFTER */ + Table *pTab, /* The table to code triggers from */ + int reg, /* The first in an array of registers (see above) */ + int orconf, /* ON CONFLICT policy */ + int ignoreJump /* Instruction to jump to for RAISE(IGNORE) */ +){ + Trigger *p; /* Used to iterate through pTrigger list */ + + assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE ); + assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER ); + assert( (op==TK_UPDATE)==(pChanges!=0) ); + + for(p=pTrigger; p; p=p->pNext){ + + /* Sanity checking: The schema for the trigger and for the table are + ** always defined. The trigger must be in the same schema as the table + ** or else it must be a TEMP trigger. */ + assert( p->pSchema!=0 ); + assert( p->pTabSchema!=0 ); + assert( p->pSchema==p->pTabSchema + || p->pSchema==pParse->db->aDb[1].pSchema ); + + /* Determine whether we should code this trigger */ + if( p->op==op + && p->tr_tm==tr_tm + && checkColumnOverlap(p->pColumns, pChanges) + ){ + sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump); + } + } +} + +/* +** Triggers may access values stored in the old.* or new.* pseudo-table. +** This function returns a 32-bit bitmask indicating which columns of the +** old.* or new.* tables actually are used by triggers. This information +** may be used by the caller, for example, to avoid having to load the entire +** old.* record into memory when executing an UPDATE or DELETE command. +** +** Bit 0 of the returned mask is set if the left-most column of the +** table may be accessed using an [old|new].reference. Bit 1 is set if +** the second leftmost column value is required, and so on. If there +** are more than 32 columns in the table, and at least one of the columns +** with an index greater than 32 may be accessed, 0xffffffff is returned. +** +** It is not possible to determine if the old.rowid or new.rowid column is +** accessed by triggers. The caller must always assume that it is. +** +** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned +** applies to the old.* table. If 1, the new.* table. +** +** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE +** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only +** included in the returned mask if the TRIGGER_BEFORE bit is set in the +** tr_tm parameter. Similarly, values accessed by AFTER triggers are only +** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm. +*/ +SQLITE_PRIVATE u32 sqlite3TriggerColmask( + Parse *pParse, /* Parse context */ + Trigger *pTrigger, /* List of triggers on table pTab */ + ExprList *pChanges, /* Changes list for any UPDATE OF triggers */ + int isNew, /* 1 for new.* ref mask, 0 for old.* ref mask */ + int tr_tm, /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ + Table *pTab, /* The table to code triggers from */ + int orconf /* Default ON CONFLICT policy for trigger steps */ +){ + const int op = pChanges ? TK_UPDATE : TK_DELETE; + u32 mask = 0; + Trigger *p; + + assert( isNew==1 || isNew==0 ); + for(p=pTrigger; p; p=p->pNext){ + if( p->op==op && (tr_tm&p->tr_tm) + && checkColumnOverlap(p->pColumns,pChanges) + ){ + TriggerPrg *pPrg; + pPrg = getRowTrigger(pParse, p, pTab, orconf); + if( pPrg ){ + mask |= pPrg->aColmask[isNew]; + } + } + } + + return mask; +} + +#endif /* !defined(SQLITE_OMIT_TRIGGER) */ + +/************** End of trigger.c *********************************************/ +/************** Begin file update.c ******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains C code routines that are called by the parser +** to handle UPDATE statements. +*/ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* Forward declaration */ +static void updateVirtualTable( + Parse *pParse, /* The parsing context */ + SrcList *pSrc, /* The virtual table to be modified */ + Table *pTab, /* The virtual table */ + ExprList *pChanges, /* The columns to change in the UPDATE statement */ + Expr *pRowidExpr, /* Expression used to recompute the rowid */ + int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ + Expr *pWhere /* WHERE clause of the UPDATE statement */ +); +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +/* +** The most recently coded instruction was an OP_Column to retrieve the +** i-th column of table pTab. This routine sets the P4 parameter of the +** OP_Column to the default value, if any. +** +** The default value of a column is specified by a DEFAULT clause in the +** column definition. This was either supplied by the user when the table +** was created, or added later to the table definition by an ALTER TABLE +** command. If the latter, then the row-records in the table btree on disk +** may not contain a value for the column and the default value, taken +** from the P4 parameter of the OP_Column instruction, is returned instead. +** If the former, then all row-records are guaranteed to include a value +** for the column and the P4 value is not required. +** +** Column definitions created by an ALTER TABLE command may only have +** literal default values specified: a number, null or a string. (If a more +** complicated default expression value was provided, it is evaluated +** when the ALTER TABLE is executed and one of the literal values written +** into the sqlite_master table.) +** +** Therefore, the P4 parameter is only required if the default value for +** the column is a literal number, string or null. The sqlite3ValueFromExpr() +** function is capable of transforming these types of expressions into +** sqlite3_value objects. +** +** If parameter iReg is not negative, code an OP_RealAffinity instruction +** on register iReg. This is used when an equivalent integer value is +** stored in place of an 8-byte floating point value in order to save +** space. +*/ +SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){ + assert( pTab!=0 ); + if( !pTab->pSelect ){ + sqlite3_value *pValue; + u8 enc = ENC(sqlite3VdbeDb(v)); + Column *pCol = &pTab->aCol[i]; + VdbeComment((v, "%s.%s", pTab->zName, pCol->zName)); + assert( inCol ); + sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc, + pCol->affinity, &pValue); + if( pValue ){ + sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM); + } +#ifndef SQLITE_OMIT_FLOATING_POINT + if( iReg>=0 && pTab->aCol[i].affinity==SQLITE_AFF_REAL ){ + sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg); + } +#endif + } +} + +/* +** Process an UPDATE statement. +** +** UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL; +** \_______/ \________/ \______/ \________________/ +* onError pTabList pChanges pWhere +*/ +SQLITE_PRIVATE void sqlite3Update( + Parse *pParse, /* The parser context */ + SrcList *pTabList, /* The table in which we should change things */ + ExprList *pChanges, /* Things to be changed */ + Expr *pWhere, /* The WHERE clause. May be null */ + int onError /* How to handle constraint errors */ +){ + int i, j; /* Loop counters */ + Table *pTab; /* The table to be updated */ + int addr = 0; /* VDBE instruction address of the start of the loop */ + WhereInfo *pWInfo; /* Information about the WHERE clause */ + Vdbe *v; /* The virtual database engine */ + Index *pIdx; /* For looping over indices */ + int nIdx; /* Number of indices that need updating */ + int iCur; /* VDBE Cursor number of pTab */ + sqlite3 *db; /* The database structure */ + int *aRegIdx = 0; /* One register assigned to each index to be updated */ + int *aXRef = 0; /* aXRef[i] is the index in pChanges->a[] of the + ** an expression for the i-th column of the table. + ** aXRef[i]==-1 if the i-th column is not changed. */ + int chngRowid; /* True if the record number is being changed */ + Expr *pRowidExpr = 0; /* Expression defining the new record number */ + int openAll = 0; /* True if all indices need to be opened */ + AuthContext sContext; /* The authorization context */ + NameContext sNC; /* The name-context to resolve expressions in */ + int iDb; /* Database containing the table being updated */ + int okOnePass; /* True for one-pass algorithm without the FIFO */ + int hasFK; /* True if foreign key processing is required */ + +#ifndef SQLITE_OMIT_TRIGGER + int isView; /* True when updating a view (INSTEAD OF trigger) */ + Trigger *pTrigger; /* List of triggers on pTab, if required */ + int tmask; /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */ +#endif + int newmask; /* Mask of NEW.* columns accessed by BEFORE triggers */ + + /* Register Allocations */ + int regRowCount = 0; /* A count of rows changed */ + int regOldRowid; /* The old rowid */ + int regNewRowid; /* The new rowid */ + int regNew; + int regOld = 0; + int regRowSet = 0; /* Rowset of rows to be updated */ + + memset(&sContext, 0, sizeof(sContext)); + db = pParse->db; + if( pParse->nErr || db->mallocFailed ){ + goto update_cleanup; + } + assert( pTabList->nSrc==1 ); + + /* Locate the table which we want to update. + */ + pTab = sqlite3SrcListLookup(pParse, pTabList); + if( pTab==0 ) goto update_cleanup; + iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema); + + /* Figure out if we have any triggers and if the table being + ** updated is a view. + */ +#ifndef SQLITE_OMIT_TRIGGER + pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask); + isView = pTab->pSelect!=0; + assert( pTrigger || tmask==0 ); +#else +# define pTrigger 0 +# define isView 0 +# define tmask 0 +#endif +#ifdef SQLITE_OMIT_VIEW +# undef isView +# define isView 0 +#endif + + if( sqlite3ViewGetColumnNames(pParse, pTab) ){ + goto update_cleanup; + } + if( sqlite3IsReadOnly(pParse, pTab, tmask) ){ + goto update_cleanup; + } + aXRef = sqlite3DbMallocRaw(db, sizeof(int) * pTab->nCol ); + if( aXRef==0 ) goto update_cleanup; + for(i=0; inCol; i++) aXRef[i] = -1; + + /* Allocate a cursors for the main database table and for all indices. + ** The index cursors might not be used, but if they are used they + ** need to occur right after the database cursor. So go ahead and + ** allocate enough space, just in case. + */ + pTabList->a[0].iCursor = iCur = pParse->nTab++; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + pParse->nTab++; + } + + /* Initialize the name-context */ + memset(&sNC, 0, sizeof(sNC)); + sNC.pParse = pParse; + sNC.pSrcList = pTabList; + + /* Resolve the column names in all the expressions of the + ** of the UPDATE statement. Also find the column index + ** for each column to be updated in the pChanges array. For each + ** column to be updated, make sure we have authorization to change + ** that column. + */ + chngRowid = 0; + for(i=0; inExpr; i++){ + if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){ + goto update_cleanup; + } + for(j=0; jnCol; j++){ + if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){ + if( j==pTab->iPKey ){ + chngRowid = 1; + pRowidExpr = pChanges->a[i].pExpr; + } + aXRef[j] = i; + break; + } + } + if( j>=pTab->nCol ){ + if( sqlite3IsRowid(pChanges->a[i].zName) ){ + chngRowid = 1; + pRowidExpr = pChanges->a[i].pExpr; + }else{ + sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName); + pParse->checkSchema = 1; + goto update_cleanup; + } + } +#ifndef SQLITE_OMIT_AUTHORIZATION + { + int rc; + rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName, + pTab->aCol[j].zName, db->aDb[iDb].zName); + if( rc==SQLITE_DENY ){ + goto update_cleanup; + }else if( rc==SQLITE_IGNORE ){ + aXRef[j] = -1; + } + } +#endif + } + + hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngRowid); + + /* Allocate memory for the array aRegIdx[]. There is one entry in the + ** array for each index associated with table being updated. Fill in + ** the value with a register number for indices that are to be used + ** and with zero for unused indices. + */ + for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){} + if( nIdx>0 ){ + aRegIdx = sqlite3DbMallocRaw(db, sizeof(Index*) * nIdx ); + if( aRegIdx==0 ) goto update_cleanup; + } + for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){ + int reg; + if( chngRowid ){ + reg = ++pParse->nMem; + }else{ + reg = 0; + for(i=0; inColumn; i++){ + if( aXRef[pIdx->aiColumn[i]]>=0 ){ + reg = ++pParse->nMem; + break; + } + } + } + aRegIdx[j] = reg; + } + + /* Begin generating code. */ + v = sqlite3GetVdbe(pParse); + if( v==0 ) goto update_cleanup; + if( pParse->nested==0 ) sqlite3VdbeCountChanges(v); + sqlite3BeginWriteOperation(pParse, 1, iDb); + +#ifndef SQLITE_OMIT_VIRTUALTABLE + /* Virtual tables must be handled separately */ + if( IsVirtual(pTab) ){ + updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef, + pWhere); + pWhere = 0; + pTabList = 0; + goto update_cleanup; + } +#endif + + /* Allocate required registers. */ + regOldRowid = regNewRowid = ++pParse->nMem; + if( pTrigger || hasFK ){ + regOld = pParse->nMem + 1; + pParse->nMem += pTab->nCol; + } + if( chngRowid || pTrigger || hasFK ){ + regNewRowid = ++pParse->nMem; + } + regNew = pParse->nMem + 1; + pParse->nMem += pTab->nCol; + + /* Start the view context. */ + if( isView ){ + sqlite3AuthContextPush(pParse, &sContext, pTab->zName); + } + + /* If we are trying to update a view, realize that view into + ** a ephemeral table. + */ +#if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) + if( isView ){ + sqlite3MaterializeView(pParse, pTab, pWhere, iCur); + } +#endif + + /* Resolve the column names in all the expressions in the + ** WHERE clause. + */ + if( sqlite3ResolveExprNames(&sNC, pWhere) ){ + goto update_cleanup; + } + + /* Begin the database scan + */ + sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid); + pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere,0, WHERE_ONEPASS_DESIRED); + if( pWInfo==0 ) goto update_cleanup; + okOnePass = pWInfo->okOnePass; + + /* Remember the rowid of every item to be updated. + */ + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid); + if( !okOnePass ){ + regRowSet = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid); + } + + /* End the database scan loop. + */ + sqlite3WhereEnd(pWInfo); + + /* Initialize the count of updated rows + */ + if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){ + regRowCount = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount); + } + + if( !isView ){ + /* + ** Open every index that needs updating. Note that if any + ** index could potentially invoke a REPLACE conflict resolution + ** action, then we need to open all indices because we might need + ** to be deleting some records. + */ + if( !okOnePass ) sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenWrite); + if( onError==OE_Replace ){ + openAll = 1; + }else{ + openAll = 0; + for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){ + if( pIdx->onError==OE_Replace ){ + openAll = 1; + break; + } + } + } + for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ + if( openAll || aRegIdx[i]>0 ){ + KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx); + sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb, + (char*)pKey, P4_KEYINFO_HANDOFF); + assert( pParse->nTab>iCur+i+1 ); + } + } + } + + /* Top of the update loop */ + if( okOnePass ){ + int a1 = sqlite3VdbeAddOp1(v, OP_NotNull, regOldRowid); + addr = sqlite3VdbeAddOp0(v, OP_Goto); + sqlite3VdbeJumpHere(v, a1); + }else{ + addr = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, 0, regOldRowid); + } + + /* Make cursor iCur point to the record that is being updated. If + ** this record does not exist for some reason (deleted by a trigger, + ** for example, then jump to the next iteration of the RowSet loop. */ + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid); + + /* If the record number will change, set register regNewRowid to + ** contain the new value. If the record number is not being modified, + ** then regNewRowid is the same register as regOldRowid, which is + ** already populated. */ + assert( chngRowid || pTrigger || hasFK || regOldRowid==regNewRowid ); + if( chngRowid ){ + sqlite3ExprCode(pParse, pRowidExpr, regNewRowid); + sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid); + } + + /* If there are triggers on this table, populate an array of registers + ** with the required old.* column data. */ + if( hasFK || pTrigger ){ + u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0); + oldmask |= sqlite3TriggerColmask(pParse, + pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError + ); + for(i=0; inCol; i++){ + if( aXRef[i]<0 || oldmask==0xffffffff || (i<32 && (oldmask & (1<nCol; i++){ + if( i==pTab->iPKey ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); + }else{ + j = aXRef[i]; + if( j>=0 ){ + sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i); + }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<nCol); + sqlite3TableAffinityStr(v, pTab); + sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, + TRIGGER_BEFORE, pTab, regOldRowid, onError, addr); + + /* The row-trigger may have deleted the row being updated. In this + ** case, jump to the next row. No updates or AFTER triggers are + ** required. This behaviour - what happens when the row being updated + ** is deleted or renamed by a BEFORE trigger - is left undefined in the + ** documentation. + */ + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addr, regOldRowid); + + /* If it did not delete it, the row-trigger may still have modified + ** some of the columns of the row being updated. Load the values for + ** all columns not modified by the update statement into their + ** registers in case this has happened. + */ + for(i=0; inCol; i++){ + if( aXRef[i]<0 && i!=pTab->iPKey ){ + sqlite3VdbeAddOp3(v, OP_Column, iCur, i, regNew+i); + sqlite3ColumnDefault(v, pTab, i, regNew+i); + } + } + } + + if( !isView ){ + int j1; /* Address of jump instruction */ + + /* Do constraint checks. */ + sqlite3GenerateConstraintChecks(pParse, pTab, iCur, regNewRowid, + aRegIdx, (chngRowid?regOldRowid:0), 1, onError, addr, 0); + + /* Do FK constraint checks. */ + if( hasFK ){ + sqlite3FkCheck(pParse, pTab, regOldRowid, 0); + } + + /* Delete the index entries associated with the current record. */ + j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regOldRowid); + sqlite3GenerateRowIndexDelete(pParse, pTab, iCur, aRegIdx); + + /* If changing the record number, delete the old record. */ + if( hasFK || chngRowid ){ + sqlite3VdbeAddOp2(v, OP_Delete, iCur, 0); + } + sqlite3VdbeJumpHere(v, j1); + + if( hasFK ){ + sqlite3FkCheck(pParse, pTab, 0, regNewRowid); + } + + /* Insert the new index entries and the new record. */ + sqlite3CompleteInsertion(pParse, pTab, iCur, regNewRowid, aRegIdx, 1, 0, 0); + + /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to + ** handle rows (possibly in other tables) that refer via a foreign key + ** to the row just updated. */ + if( hasFK ){ + sqlite3FkActions(pParse, pTab, pChanges, regOldRowid); + } + } + + /* Increment the row counter + */ + if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){ + sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1); + } + + sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges, + TRIGGER_AFTER, pTab, regOldRowid, onError, addr); + + /* Repeat the above with the next record to be updated, until + ** all record selected by the WHERE clause have been updated. + */ + sqlite3VdbeAddOp2(v, OP_Goto, 0, addr); + sqlite3VdbeJumpHere(v, addr); + + /* Close all tables */ + for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){ + if( openAll || aRegIdx[i]>0 ){ + sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0); + } + } + sqlite3VdbeAddOp2(v, OP_Close, iCur, 0); + + /* Update the sqlite_sequence table by storing the content of the + ** maximum rowid counter values recorded while inserting into + ** autoincrement tables. + */ + if( pParse->nested==0 && pParse->pTriggerTab==0 ){ + sqlite3AutoincrementEnd(pParse); + } + + /* + ** Return the number of rows that were changed. If this routine is + ** generating code because of a call to sqlite3NestedParse(), do not + ** invoke the callback function. + */ + if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){ + sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC); + } + +update_cleanup: + sqlite3AuthContextPop(&sContext); + sqlite3DbFree(db, aRegIdx); + sqlite3DbFree(db, aXRef); + sqlite3SrcListDelete(db, pTabList); + sqlite3ExprListDelete(db, pChanges); + sqlite3ExprDelete(db, pWhere); + return; +} +/* Make sure "isView" and other macros defined above are undefined. Otherwise +** thely may interfere with compilation of other functions in this file +** (or in another file, if this file becomes part of the amalgamation). */ +#ifdef isView + #undef isView +#endif +#ifdef pTrigger + #undef pTrigger +#endif + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Generate code for an UPDATE of a virtual table. +** +** The strategy is that we create an ephemerial table that contains +** for each row to be changed: +** +** (A) The original rowid of that row. +** (B) The revised rowid for the row. (note1) +** (C) The content of every column in the row. +** +** Then we loop over this ephemeral table and for each row in +** the ephermeral table call VUpdate. +** +** When finished, drop the ephemeral table. +** +** (note1) Actually, if we know in advance that (A) is always the same +** as (B) we only store (A), then duplicate (A) when pulling +** it out of the ephemeral table before calling VUpdate. +*/ +static void updateVirtualTable( + Parse *pParse, /* The parsing context */ + SrcList *pSrc, /* The virtual table to be modified */ + Table *pTab, /* The virtual table */ + ExprList *pChanges, /* The columns to change in the UPDATE statement */ + Expr *pRowid, /* Expression used to recompute the rowid */ + int *aXRef, /* Mapping from columns of pTab to entries in pChanges */ + Expr *pWhere /* WHERE clause of the UPDATE statement */ +){ + Vdbe *v = pParse->pVdbe; /* Virtual machine under construction */ + ExprList *pEList = 0; /* The result set of the SELECT statement */ + Select *pSelect = 0; /* The SELECT statement */ + Expr *pExpr; /* Temporary expression */ + int ephemTab; /* Table holding the result of the SELECT */ + int i; /* Loop counter */ + int addr; /* Address of top of loop */ + int iReg; /* First register in set passed to OP_VUpdate */ + sqlite3 *db = pParse->db; /* Database connection */ + const char *pVTab = (const char*)sqlite3GetVTable(db, pTab); + SelectDest dest; + + /* Construct the SELECT statement that will find the new values for + ** all updated rows. + */ + pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_")); + if( pRowid ){ + pEList = sqlite3ExprListAppend(pParse, pEList, + sqlite3ExprDup(db, pRowid, 0)); + } + assert( pTab->iPKey<0 ); + for(i=0; inCol; i++){ + if( aXRef[i]>=0 ){ + pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0); + }else{ + pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName); + } + pEList = sqlite3ExprListAppend(pParse, pEList, pExpr); + } + pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0); + + /* Create the ephemeral table into which the update results will + ** be stored. + */ + assert( v ); + ephemTab = pParse->nTab++; + sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0)); + sqlite3VdbeChangeP5(v, BTREE_UNORDERED); + + /* fill the ephemeral table + */ + sqlite3SelectDestInit(&dest, SRT_Table, ephemTab); + sqlite3Select(pParse, pSelect, &dest); + + /* Generate code to scan the ephemeral table and call VUpdate. */ + iReg = ++pParse->nMem; + pParse->nMem += pTab->nCol+1; + addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0); + sqlite3VdbeAddOp3(v, OP_Column, ephemTab, 0, iReg); + sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1); + for(i=0; inCol; i++){ + sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i); + } + sqlite3VtabMakeWritable(pParse, pTab); + sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB); + sqlite3MayAbort(pParse); + sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1); + sqlite3VdbeJumpHere(v, addr); + sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0); + + /* Cleanup */ + sqlite3SelectDelete(db, pSelect); +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +/************** End of update.c **********************************************/ +/************** Begin file vacuum.c ******************************************/ +/* +** 2003 April 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to implement the VACUUM command. +** +** Most of the code in this file may be omitted by defining the +** SQLITE_OMIT_VACUUM macro. +*/ + +#if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH) +/* +** Finalize a prepared statement. If there was an error, store the +** text of the error message in *pzErrMsg. Return the result code. +*/ +static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){ + int rc; + rc = sqlite3VdbeFinalize((Vdbe*)pStmt); + if( rc ){ + sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); + } + return rc; +} + +/* +** Execute zSql on database db. Return an error code. +*/ +static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ + sqlite3_stmt *pStmt; + VVA_ONLY( int rc; ) + if( !zSql ){ + return SQLITE_NOMEM; + } + if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){ + sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); + return sqlite3_errcode(db); + } + VVA_ONLY( rc = ) sqlite3_step(pStmt); + assert( rc!=SQLITE_ROW ); + return vacuumFinalize(db, pStmt, pzErrMsg); +} + +/* +** Execute zSql on database db. The statement returns exactly +** one column. Execute this as SQL on the same database. +*/ +static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ + sqlite3_stmt *pStmt; + int rc; + + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ) return rc; + + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0)); + if( rc!=SQLITE_OK ){ + vacuumFinalize(db, pStmt, pzErrMsg); + return rc; + } + } + + return vacuumFinalize(db, pStmt, pzErrMsg); +} + +/* +** The non-standard VACUUM command is used to clean up the database, +** collapse free space, etc. It is modelled after the VACUUM command +** in PostgreSQL. +** +** In version 1.0.x of SQLite, the VACUUM command would call +** gdbm_reorganize() on all the database tables. But beginning +** with 2.0.0, SQLite no longer uses GDBM so this command has +** become a no-op. +*/ +SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){ + Vdbe *v = sqlite3GetVdbe(pParse); + if( v ){ + sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0); + } + return; +} + +/* +** This routine implements the OP_Vacuum opcode of the VDBE. +*/ +SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ + int rc = SQLITE_OK; /* Return code from service routines */ + Btree *pMain; /* The database being vacuumed */ + Btree *pTemp; /* The temporary database we vacuum into */ + char *zSql = 0; /* SQL statements */ + int saved_flags; /* Saved value of the db->flags */ + int saved_nChange; /* Saved value of db->nChange */ + int saved_nTotalChange; /* Saved value of db->nTotalChange */ + void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */ + Db *pDb = 0; /* Database to detach at end of vacuum */ + int isMemDb; /* True if vacuuming a :memory: database */ + int nRes; /* Bytes of reserved space at the end of each page */ + int nDb; /* Number of attached databases */ + + if( !db->autoCommit ){ + sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction"); + return SQLITE_ERROR; + } + if( db->activeVdbeCnt>1 ){ + sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress"); + return SQLITE_ERROR; + } + + /* Save the current value of the database flags so that it can be + ** restored before returning. Then set the writable-schema flag, and + ** disable CHECK and foreign key constraints. */ + saved_flags = db->flags; + saved_nChange = db->nChange; + saved_nTotalChange = db->nTotalChange; + saved_xTrace = db->xTrace; + db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin; + db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder); + db->xTrace = 0; + + pMain = db->aDb[0].pBt; + isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain)); + + /* Attach the temporary database as 'vacuum_db'. The synchronous pragma + ** can be set to 'off' for this file, as it is not recovered if a crash + ** occurs anyway. The integrity of the database is maintained by a + ** (possibly synchronous) transaction opened on the main database before + ** sqlite3BtreeCopyFile() is called. + ** + ** An optimisation would be to use a non-journaled pager. + ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but + ** that actually made the VACUUM run slower. Very little journalling + ** actually occurs when doing a vacuum since the vacuum_db is initially + ** empty. Only the journal header is written. Apparently it takes more + ** time to parse and run the PRAGMA to turn journalling off than it does + ** to write the journal header file. + */ + nDb = db->nDb; + if( sqlite3TempInMemory(db) ){ + zSql = "ATTACH ':memory:' AS vacuum_db;"; + }else{ + zSql = "ATTACH '' AS vacuum_db;"; + } + rc = execSql(db, pzErrMsg, zSql); + if( db->nDb>nDb ){ + pDb = &db->aDb[db->nDb-1]; + assert( strcmp(pDb->zName,"vacuum_db")==0 ); + } + if( rc!=SQLITE_OK ) goto end_of_vacuum; + pTemp = db->aDb[db->nDb-1].pBt; + + /* The call to execSql() to attach the temp database has left the file + ** locked (as there was more than one active statement when the transaction + ** to read the schema was concluded. Unlock it here so that this doesn't + ** cause problems for the call to BtreeSetPageSize() below. */ + sqlite3BtreeCommit(pTemp); + + nRes = sqlite3BtreeGetReserve(pMain); + + /* A VACUUM cannot change the pagesize of an encrypted database. */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_HAS_CODEC_NO_ENCRYPTION) + if( db->nextPagesize ){ + extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); + int nKey; + char *zKey; + sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); + if( nKey ) db->nextPagesize = 0; + } +#endif + + /* Do not attempt to change the page size for a WAL database */ + if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain)) + ==PAGER_JOURNALMODE_WAL ){ + db->nextPagesize = 0; + } + + if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0) + || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0)) + || NEVER(db->mallocFailed) + ){ + rc = SQLITE_NOMEM; + goto end_of_vacuum; + } + rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF"); + if( rc!=SQLITE_OK ){ + goto end_of_vacuum; + } + +#ifndef SQLITE_OMIT_AUTOVACUUM + sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac : + sqlite3BtreeGetAutoVacuum(pMain)); +#endif + + /* Begin a transaction */ + rc = execSql(db, pzErrMsg, "BEGIN EXCLUSIVE;"); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + + /* Query the schema of the main database. Create a mirror schema + ** in the temporary database. + */ + rc = execExecSql(db, pzErrMsg, + "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) " + " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'" + " AND rootpage>0" + ); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + rc = execExecSql(db, pzErrMsg, + "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)" + " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' "); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + rc = execExecSql(db, pzErrMsg, + "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) " + " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'"); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + + /* Loop through the tables in the main database. For each, do + ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy + ** the contents to the temporary database. + */ + rc = execExecSql(db, pzErrMsg, + "SELECT 'INSERT INTO vacuum_db.' || quote(name) " + "|| ' SELECT * FROM main.' || quote(name) || ';'" + "FROM main.sqlite_master " + "WHERE type = 'table' AND name!='sqlite_sequence' " + " AND rootpage>0" + ); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + + /* Copy over the sequence table + */ + rc = execExecSql(db, pzErrMsg, + "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' " + "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' " + ); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + rc = execExecSql(db, pzErrMsg, + "SELECT 'INSERT INTO vacuum_db.' || quote(name) " + "|| ' SELECT * FROM main.' || quote(name) || ';' " + "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';" + ); + if( rc!=SQLITE_OK ) goto end_of_vacuum; + + + /* Copy the triggers, views, and virtual tables from the main database + ** over to the temporary database. None of these objects has any + ** associated storage, so all we have to do is copy their entries + ** from the SQLITE_MASTER table. + */ + rc = execSql(db, pzErrMsg, + "INSERT INTO vacuum_db.sqlite_master " + " SELECT type, name, tbl_name, rootpage, sql" + " FROM main.sqlite_master" + " WHERE type='view' OR type='trigger'" + " OR (type='table' AND rootpage=0)" + ); + if( rc ) goto end_of_vacuum; + + /* At this point, unless the main db was completely empty, there is now a + ** transaction open on the vacuum database, but not on the main database. + ** Open a btree level transaction on the main database. This allows a + ** call to sqlite3BtreeCopyFile(). The main database btree level + ** transaction is then committed, so the SQL level never knows it was + ** opened for writing. This way, the SQL transaction used to create the + ** temporary database never needs to be committed. + */ + { + u32 meta; + int i; + + /* This array determines which meta meta values are preserved in the + ** vacuum. Even entries are the meta value number and odd entries + ** are an increment to apply to the meta value after the vacuum. + ** The increment is used to increase the schema cookie so that other + ** connections to the same database will know to reread the schema. + */ + static const unsigned char aCopy[] = { + BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */ + BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */ + BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */ + BTREE_USER_VERSION, 0, /* Preserve the user version */ + }; + + assert( 1==sqlite3BtreeIsInTrans(pTemp) ); + assert( 1==sqlite3BtreeIsInTrans(pMain) ); + + /* Copy Btree meta values */ + for(i=0; iflags */ + db->flags = saved_flags; + db->nChange = saved_nChange; + db->nTotalChange = saved_nTotalChange; + db->xTrace = saved_xTrace; + sqlite3BtreeSetPageSize(pMain, -1, -1, 1); + + /* Currently there is an SQL level transaction open on the vacuum + ** database. No locks are held on any other files (since the main file + ** was committed at the btree level). So it safe to end the transaction + ** by manually setting the autoCommit flag to true and detaching the + ** vacuum database. The vacuum_db journal file is deleted when the pager + ** is closed by the DETACH. + */ + db->autoCommit = 1; + + if( pDb ){ + sqlite3BtreeClose(pDb->pBt); + pDb->pBt = 0; + pDb->pSchema = 0; + } + + sqlite3ResetInternalSchema(db, 0); + + return rc; +} +#endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */ + +/************** End of vacuum.c **********************************************/ +/************** Begin file vtab.c ********************************************/ +/* +** 2006 June 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code used to help implement virtual tables. +*/ +#ifndef SQLITE_OMIT_VIRTUALTABLE + +/* +** The actual function that does the work of creating a new module. +** This function implements the sqlite3_create_module() and +** sqlite3_create_module_v2() interfaces. +*/ +static int createModule( + sqlite3 *db, /* Database in which module is registered */ + const char *zName, /* Name assigned to this module */ + const sqlite3_module *pModule, /* The definition of the module */ + void *pAux, /* Context pointer for xCreate/xConnect */ + void (*xDestroy)(void *) /* Module destructor function */ +){ + int rc, nName; + Module *pMod; + + sqlite3_mutex_enter(db->mutex); + nName = sqlite3Strlen30(zName); + pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1); + if( pMod ){ + Module *pDel; + char *zCopy = (char *)(&pMod[1]); + memcpy(zCopy, zName, nName+1); + pMod->zName = zCopy; + pMod->pModule = pModule; + pMod->pAux = pAux; + pMod->xDestroy = xDestroy; + pDel = (Module *)sqlite3HashInsert(&db->aModule, zCopy, nName, (void*)pMod); + if( pDel && pDel->xDestroy ){ + pDel->xDestroy(pDel->pAux); + } + sqlite3DbFree(db, pDel); + if( pDel==pMod ){ + db->mallocFailed = 1; + } + sqlite3ResetInternalSchema(db, 0); + }else if( xDestroy ){ + xDestroy(pAux); + } + rc = sqlite3ApiExit(db, SQLITE_OK); + sqlite3_mutex_leave(db->mutex); + return rc; +} + + +/* +** External API function used to create a new virtual-table module. +*/ +SQLITE_API int sqlite3_create_module( + sqlite3 *db, /* Database in which module is registered */ + const char *zName, /* Name assigned to this module */ + const sqlite3_module *pModule, /* The definition of the module */ + void *pAux /* Context pointer for xCreate/xConnect */ +){ + return createModule(db, zName, pModule, pAux, 0); +} + +/* +** External API function used to create a new virtual-table module. +*/ +SQLITE_API int sqlite3_create_module_v2( + sqlite3 *db, /* Database in which module is registered */ + const char *zName, /* Name assigned to this module */ + const sqlite3_module *pModule, /* The definition of the module */ + void *pAux, /* Context pointer for xCreate/xConnect */ + void (*xDestroy)(void *) /* Module destructor function */ +){ + return createModule(db, zName, pModule, pAux, xDestroy); +} + +/* +** Lock the virtual table so that it cannot be disconnected. +** Locks nest. Every lock should have a corresponding unlock. +** If an unlock is omitted, resources leaks will occur. +** +** If a disconnect is attempted while a virtual table is locked, +** the disconnect is deferred until all locks have been removed. +*/ +SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){ + pVTab->nRef++; +} + + +/* +** pTab is a pointer to a Table structure representing a virtual-table. +** Return a pointer to the VTable object used by connection db to access +** this virtual-table, if one has been created, or NULL otherwise. +*/ +SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){ + VTable *pVtab; + assert( IsVirtual(pTab) ); + for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext); + return pVtab; +} + +/* +** Decrement the ref-count on a virtual table object. When the ref-count +** reaches zero, call the xDisconnect() method to delete the object. +*/ +SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){ + sqlite3 *db = pVTab->db; + + assert( db ); + assert( pVTab->nRef>0 ); + assert( sqlite3SafetyCheckOk(db) ); + + pVTab->nRef--; + if( pVTab->nRef==0 ){ + sqlite3_vtab *p = pVTab->pVtab; + if( p ){ + p->pModule->xDisconnect(p); + } + sqlite3DbFree(db, pVTab); + } +} + +/* +** Table p is a virtual table. This function moves all elements in the +** p->pVTable list to the sqlite3.pDisconnect lists of their associated +** database connections to be disconnected at the next opportunity. +** Except, if argument db is not NULL, then the entry associated with +** connection db is left in the p->pVTable list. +*/ +static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){ + VTable *pRet = 0; + VTable *pVTable = p->pVTable; + p->pVTable = 0; + + /* Assert that the mutex (if any) associated with the BtShared database + ** that contains table p is held by the caller. See header comments + ** above function sqlite3VtabUnlockList() for an explanation of why + ** this makes it safe to access the sqlite3.pDisconnect list of any + ** database connection that may have an entry in the p->pVTable list. */ + assert( db==0 || + sqlite3BtreeHoldsMutex(db->aDb[sqlite3SchemaToIndex(db, p->pSchema)].pBt) + ); + + while( pVTable ){ + sqlite3 *db2 = pVTable->db; + VTable *pNext = pVTable->pNext; + assert( db2 ); + if( db2==db ){ + pRet = pVTable; + p->pVTable = pRet; + pRet->pNext = 0; + }else{ + pVTable->pNext = db2->pDisconnect; + db2->pDisconnect = pVTable; + } + pVTable = pNext; + } + + assert( !db || pRet ); + return pRet; +} + + +/* +** Disconnect all the virtual table objects in the sqlite3.pDisconnect list. +** +** This function may only be called when the mutexes associated with all +** shared b-tree databases opened using connection db are held by the +** caller. This is done to protect the sqlite3.pDisconnect list. The +** sqlite3.pDisconnect list is accessed only as follows: +** +** 1) By this function. In this case, all BtShared mutexes and the mutex +** associated with the database handle itself must be held. +** +** 2) By function vtabDisconnectAll(), when it adds a VTable entry to +** the sqlite3.pDisconnect list. In this case either the BtShared mutex +** associated with the database the virtual table is stored in is held +** or, if the virtual table is stored in a non-sharable database, then +** the database handle mutex is held. +** +** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously +** by multiple threads. It is thread-safe. +*/ +SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){ + VTable *p = db->pDisconnect; + db->pDisconnect = 0; + + assert( sqlite3BtreeHoldsAllMutexes(db) ); + assert( sqlite3_mutex_held(db->mutex) ); + + if( p ){ + sqlite3ExpirePreparedStatements(db); + do { + VTable *pNext = p->pNext; + sqlite3VtabUnlock(p); + p = pNext; + }while( p ); + } +} + +/* +** Clear any and all virtual-table information from the Table record. +** This routine is called, for example, just before deleting the Table +** record. +** +** Since it is a virtual-table, the Table structure contains a pointer +** to the head of a linked list of VTable structures. Each VTable +** structure is associated with a single sqlite3* user of the schema. +** The reference count of the VTable structure associated with database +** connection db is decremented immediately (which may lead to the +** structure being xDisconnected and free). Any other VTable structures +** in the list are moved to the sqlite3.pDisconnect list of the associated +** database connection. +*/ +SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){ + if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p); + if( p->azModuleArg ){ + int i; + for(i=0; inModuleArg; i++){ + sqlite3DbFree(db, p->azModuleArg[i]); + } + sqlite3DbFree(db, p->azModuleArg); + } +} + +/* +** Add a new module argument to pTable->azModuleArg[]. +** The string is not copied - the pointer is stored. The +** string will be freed automatically when the table is +** deleted. +*/ +static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){ + int i = pTable->nModuleArg++; + int nBytes = sizeof(char *)*(1+pTable->nModuleArg); + char **azModuleArg; + azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes); + if( azModuleArg==0 ){ + int j; + for(j=0; jazModuleArg[j]); + } + sqlite3DbFree(db, zArg); + sqlite3DbFree(db, pTable->azModuleArg); + pTable->nModuleArg = 0; + }else{ + azModuleArg[i] = zArg; + azModuleArg[i+1] = 0; + } + pTable->azModuleArg = azModuleArg; +} + +/* +** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE +** statement. The module name has been parsed, but the optional list +** of parameters that follow the module name are still pending. +*/ +SQLITE_PRIVATE void sqlite3VtabBeginParse( + Parse *pParse, /* Parsing context */ + Token *pName1, /* Name of new table, or database name */ + Token *pName2, /* Name of new table or NULL */ + Token *pModuleName /* Name of the module for the virtual table */ +){ + int iDb; /* The database the table is being created in */ + Table *pTable; /* The new virtual table */ + sqlite3 *db; /* Database connection */ + + sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, 0); + pTable = pParse->pNewTable; + if( pTable==0 ) return; + assert( 0==pTable->pIndex ); + + db = pParse->db; + iDb = sqlite3SchemaToIndex(db, pTable->pSchema); + assert( iDb>=0 ); + + pTable->tabFlags |= TF_Virtual; + pTable->nModuleArg = 0; + addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName)); + addModuleArgument(db, pTable, sqlite3DbStrDup(db, db->aDb[iDb].zName)); + addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName)); + pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z); + +#ifndef SQLITE_OMIT_AUTHORIZATION + /* Creating a virtual table invokes the authorization callback twice. + ** The first invocation, to obtain permission to INSERT a row into the + ** sqlite_master table, has already been made by sqlite3StartTable(). + ** The second call, to obtain permission to create the table, is made now. + */ + if( pTable->azModuleArg ){ + sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName, + pTable->azModuleArg[0], pParse->db->aDb[iDb].zName); + } +#endif +} + +/* +** This routine takes the module argument that has been accumulating +** in pParse->zArg[] and appends it to the list of arguments on the +** virtual table currently under construction in pParse->pTable. +*/ +static void addArgumentToVtab(Parse *pParse){ + if( pParse->sArg.z && ALWAYS(pParse->pNewTable) ){ + const char *z = (const char*)pParse->sArg.z; + int n = pParse->sArg.n; + sqlite3 *db = pParse->db; + addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n)); + } +} + +/* +** The parser calls this routine after the CREATE VIRTUAL TABLE statement +** has been completely parsed. +*/ +SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){ + Table *pTab = pParse->pNewTable; /* The table being constructed */ + sqlite3 *db = pParse->db; /* The database connection */ + + if( pTab==0 ) return; + addArgumentToVtab(pParse); + pParse->sArg.z = 0; + if( pTab->nModuleArg<1 ) return; + + /* If the CREATE VIRTUAL TABLE statement is being entered for the + ** first time (in other words if the virtual table is actually being + ** created now instead of just being read out of sqlite_master) then + ** do additional initialization work and store the statement text + ** in the sqlite_master table. + */ + if( !db->init.busy ){ + char *zStmt; + char *zWhere; + int iDb; + Vdbe *v; + + /* Compute the complete text of the CREATE VIRTUAL TABLE statement */ + if( pEnd ){ + pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n; + } + zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken); + + /* A slot for the record has already been allocated in the + ** SQLITE_MASTER table. We just need to update that slot with all + ** the information we've collected. + ** + ** The VM register number pParse->regRowid holds the rowid of an + ** entry in the sqlite_master table tht was created for this vtab + ** by sqlite3StartTable(). + */ + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + sqlite3NestedParse(pParse, + "UPDATE %Q.%s " + "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q " + "WHERE rowid=#%d", + db->aDb[iDb].zName, SCHEMA_TABLE(iDb), + pTab->zName, + pTab->zName, + zStmt, + pParse->regRowid + ); + sqlite3DbFree(db, zStmt); + v = sqlite3GetVdbe(pParse); + sqlite3ChangeCookie(pParse, iDb); + + sqlite3VdbeAddOp2(v, OP_Expire, 0, 0); + zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName); + sqlite3VdbeAddOp4(v, OP_ParseSchema, iDb, 1, 0, zWhere, P4_DYNAMIC); + sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0, + pTab->zName, sqlite3Strlen30(pTab->zName) + 1); + } + + /* If we are rereading the sqlite_master table create the in-memory + ** record of the table. The xConnect() method is not called until + ** the first time the virtual table is used in an SQL statement. This + ** allows a schema that contains virtual tables to be loaded before + ** the required virtual table implementations are registered. */ + else { + Table *pOld; + Schema *pSchema = pTab->pSchema; + const char *zName = pTab->zName; + int nName = sqlite3Strlen30(zName); + pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab); + if( pOld ){ + db->mallocFailed = 1; + assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */ + return; + } + pParse->pNewTable = 0; + } +} + +/* +** The parser calls this routine when it sees the first token +** of an argument to the module name in a CREATE VIRTUAL TABLE statement. +*/ +SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){ + addArgumentToVtab(pParse); + pParse->sArg.z = 0; + pParse->sArg.n = 0; +} + +/* +** The parser calls this routine for each token after the first token +** in an argument to the module name in a CREATE VIRTUAL TABLE statement. +*/ +SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){ + Token *pArg = &pParse->sArg; + if( pArg->z==0 ){ + pArg->z = p->z; + pArg->n = p->n; + }else{ + assert(pArg->z < p->z); + pArg->n = (int)(&p->z[p->n] - pArg->z); + } +} + +/* +** Invoke a virtual table constructor (either xCreate or xConnect). The +** pointer to the function to invoke is passed as the fourth parameter +** to this procedure. +*/ +static int vtabCallConstructor( + sqlite3 *db, + Table *pTab, + Module *pMod, + int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**), + char **pzErr +){ + VTable *pVTable; + int rc; + const char *const*azArg = (const char *const*)pTab->azModuleArg; + int nArg = pTab->nModuleArg; + char *zErr = 0; + char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName); + + if( !zModuleName ){ + return SQLITE_NOMEM; + } + + pVTable = sqlite3DbMallocZero(db, sizeof(VTable)); + if( !pVTable ){ + sqlite3DbFree(db, zModuleName); + return SQLITE_NOMEM; + } + pVTable->db = db; + pVTable->pMod = pMod; + + assert( !db->pVTab ); + assert( xConstruct ); + db->pVTab = pTab; + + /* Invoke the virtual table constructor */ + rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr); + if( rc==SQLITE_NOMEM ) db->mallocFailed = 1; + + if( SQLITE_OK!=rc ){ + if( zErr==0 ){ + *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName); + }else { + *pzErr = sqlite3MPrintf(db, "%s", zErr); + sqlite3_free(zErr); + } + sqlite3DbFree(db, pVTable); + }else if( ALWAYS(pVTable->pVtab) ){ + /* Justification of ALWAYS(): A correct vtab constructor must allocate + ** the sqlite3_vtab object if successful. */ + pVTable->pVtab->pModule = pMod->pModule; + pVTable->nRef = 1; + if( db->pVTab ){ + const char *zFormat = "vtable constructor did not declare schema: %s"; + *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName); + sqlite3VtabUnlock(pVTable); + rc = SQLITE_ERROR; + }else{ + int iCol; + /* If everything went according to plan, link the new VTable structure + ** into the linked list headed by pTab->pVTable. Then loop through the + ** columns of the table to see if any of them contain the token "hidden". + ** If so, set the Column.isHidden flag and remove the token from + ** the type string. */ + pVTable->pNext = pTab->pVTable; + pTab->pVTable = pVTable; + + for(iCol=0; iColnCol; iCol++){ + char *zType = pTab->aCol[iCol].zType; + int nType; + int i = 0; + if( !zType ) continue; + nType = sqlite3Strlen30(zType); + if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){ + for(i=0; i0 ){ + assert(zType[i-1]==' '); + zType[i-1] = '\0'; + } + pTab->aCol[iCol].isHidden = 1; + } + } + } + } + + sqlite3DbFree(db, zModuleName); + db->pVTab = 0; + return rc; +} + +/* +** This function is invoked by the parser to call the xConnect() method +** of the virtual table pTab. If an error occurs, an error code is returned +** and an error left in pParse. +** +** This call is a no-op if table pTab is not a virtual table. +*/ +SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){ + sqlite3 *db = pParse->db; + const char *zMod; + Module *pMod; + int rc; + + assert( pTab ); + if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){ + return SQLITE_OK; + } + + /* Locate the required virtual table module */ + zMod = pTab->azModuleArg[0]; + pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod)); + + if( !pMod ){ + const char *zModule = pTab->azModuleArg[0]; + sqlite3ErrorMsg(pParse, "no such module: %s", zModule); + rc = SQLITE_ERROR; + }else{ + char *zErr = 0; + rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr); + if( rc!=SQLITE_OK ){ + sqlite3ErrorMsg(pParse, "%s", zErr); + } + sqlite3DbFree(db, zErr); + } + + return rc; +} + +/* +** Add the virtual table pVTab to the array sqlite3.aVTrans[]. +*/ +static int addToVTrans(sqlite3 *db, VTable *pVTab){ + const int ARRAY_INCR = 5; + + /* Grow the sqlite3.aVTrans array if required */ + if( (db->nVTrans%ARRAY_INCR)==0 ){ + VTable **aVTrans; + int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR); + aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes); + if( !aVTrans ){ + return SQLITE_NOMEM; + } + memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR); + db->aVTrans = aVTrans; + } + + /* Add pVtab to the end of sqlite3.aVTrans */ + db->aVTrans[db->nVTrans++] = pVTab; + sqlite3VtabLock(pVTab); + return SQLITE_OK; +} + +/* +** This function is invoked by the vdbe to call the xCreate method +** of the virtual table named zTab in database iDb. +** +** If an error occurs, *pzErr is set to point an an English language +** description of the error and an SQLITE_XXX error code is returned. +** In this case the caller must call sqlite3DbFree(db, ) on *pzErr. +*/ +SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){ + int rc = SQLITE_OK; + Table *pTab; + Module *pMod; + const char *zMod; + + pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); + assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable ); + + /* Locate the required virtual table module */ + zMod = pTab->azModuleArg[0]; + pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod)); + + /* If the module has been registered and includes a Create method, + ** invoke it now. If the module has not been registered, return an + ** error. Otherwise, do nothing. + */ + if( !pMod ){ + *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod); + rc = SQLITE_ERROR; + }else{ + rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr); + } + + /* Justification of ALWAYS(): The xConstructor method is required to + ** create a valid sqlite3_vtab if it returns SQLITE_OK. */ + if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){ + rc = addToVTrans(db, sqlite3GetVTable(db, pTab)); + } + + return rc; +} + +/* +** This function is used to set the schema of a virtual table. It is only +** valid to call this function from within the xCreate() or xConnect() of a +** virtual table module. +*/ +SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){ + Parse *pParse; + + int rc = SQLITE_OK; + Table *pTab; + char *zErr = 0; + + sqlite3_mutex_enter(db->mutex); + pTab = db->pVTab; + if( !pTab ){ + sqlite3Error(db, SQLITE_MISUSE, 0); + sqlite3_mutex_leave(db->mutex); + return SQLITE_MISUSE_BKPT; + } + assert( (pTab->tabFlags & TF_Virtual)!=0 ); + + pParse = sqlite3StackAllocZero(db, sizeof(*pParse)); + if( pParse==0 ){ + rc = SQLITE_NOMEM; + }else{ + pParse->declareVtab = 1; + pParse->db = db; + pParse->nQueryLoop = 1; + + if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr) + && pParse->pNewTable + && !db->mallocFailed + && !pParse->pNewTable->pSelect + && (pParse->pNewTable->tabFlags & TF_Virtual)==0 + ){ + if( !pTab->aCol ){ + pTab->aCol = pParse->pNewTable->aCol; + pTab->nCol = pParse->pNewTable->nCol; + pParse->pNewTable->nCol = 0; + pParse->pNewTable->aCol = 0; + } + db->pVTab = 0; + }else{ + sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr); + sqlite3DbFree(db, zErr); + rc = SQLITE_ERROR; + } + pParse->declareVtab = 0; + + if( pParse->pVdbe ){ + sqlite3VdbeFinalize(pParse->pVdbe); + } + sqlite3DeleteTable(db, pParse->pNewTable); + sqlite3StackFree(db, pParse); + } + + assert( (rc&0xff)==rc ); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** This function is invoked by the vdbe to call the xDestroy method +** of the virtual table named zTab in database iDb. This occurs +** when a DROP TABLE is mentioned. +** +** This call is a no-op if zTab is not a virtual table. +*/ +SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){ + int rc = SQLITE_OK; + Table *pTab; + + pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName); + if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){ + VTable *p = vtabDisconnectAll(db, pTab); + + assert( rc==SQLITE_OK ); + rc = p->pMod->pModule->xDestroy(p->pVtab); + + /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */ + if( rc==SQLITE_OK ){ + assert( pTab->pVTable==p && p->pNext==0 ); + p->pVtab = 0; + pTab->pVTable = 0; + sqlite3VtabUnlock(p); + } + } + + return rc; +} + +/* +** This function invokes either the xRollback or xCommit method +** of each of the virtual tables in the sqlite3.aVTrans array. The method +** called is identified by the second argument, "offset", which is +** the offset of the method to call in the sqlite3_module structure. +** +** The array is cleared after invoking the callbacks. +*/ +static void callFinaliser(sqlite3 *db, int offset){ + int i; + if( db->aVTrans ){ + for(i=0; inVTrans; i++){ + VTable *pVTab = db->aVTrans[i]; + sqlite3_vtab *p = pVTab->pVtab; + if( p ){ + int (*x)(sqlite3_vtab *); + x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset); + if( x ) x(p); + } + sqlite3VtabUnlock(pVTab); + } + sqlite3DbFree(db, db->aVTrans); + db->nVTrans = 0; + db->aVTrans = 0; + } +} + +/* +** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans +** array. Return the error code for the first error that occurs, or +** SQLITE_OK if all xSync operations are successful. +** +** Set *pzErrmsg to point to a buffer that should be released using +** sqlite3DbFree() containing an error message, if one is available. +*/ +SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){ + int i; + int rc = SQLITE_OK; + VTable **aVTrans = db->aVTrans; + + db->aVTrans = 0; + for(i=0; rc==SQLITE_OK && inVTrans; i++){ + int (*x)(sqlite3_vtab *); + sqlite3_vtab *pVtab = aVTrans[i]->pVtab; + if( pVtab && (x = pVtab->pModule->xSync)!=0 ){ + rc = x(pVtab); + sqlite3DbFree(db, *pzErrmsg); + *pzErrmsg = sqlite3DbStrDup(db, pVtab->zErrMsg); + sqlite3_free(pVtab->zErrMsg); + } + } + db->aVTrans = aVTrans; + return rc; +} + +/* +** Invoke the xRollback method of all virtual tables in the +** sqlite3.aVTrans array. Then clear the array itself. +*/ +SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){ + callFinaliser(db, offsetof(sqlite3_module,xRollback)); + return SQLITE_OK; +} + +/* +** Invoke the xCommit method of all virtual tables in the +** sqlite3.aVTrans array. Then clear the array itself. +*/ +SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){ + callFinaliser(db, offsetof(sqlite3_module,xCommit)); + return SQLITE_OK; +} + +/* +** If the virtual table pVtab supports the transaction interface +** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is +** not currently open, invoke the xBegin method now. +** +** If the xBegin call is successful, place the sqlite3_vtab pointer +** in the sqlite3.aVTrans array. +*/ +SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){ + int rc = SQLITE_OK; + const sqlite3_module *pModule; + + /* Special case: If db->aVTrans is NULL and db->nVTrans is greater + ** than zero, then this function is being called from within a + ** virtual module xSync() callback. It is illegal to write to + ** virtual module tables in this case, so return SQLITE_LOCKED. + */ + if( sqlite3VtabInSync(db) ){ + return SQLITE_LOCKED; + } + if( !pVTab ){ + return SQLITE_OK; + } + pModule = pVTab->pVtab->pModule; + + if( pModule->xBegin ){ + int i; + + + /* If pVtab is already in the aVTrans array, return early */ + for(i=0; inVTrans; i++){ + if( db->aVTrans[i]==pVTab ){ + return SQLITE_OK; + } + } + + /* Invoke the xBegin method */ + rc = pModule->xBegin(pVTab->pVtab); + if( rc==SQLITE_OK ){ + rc = addToVTrans(db, pVTab); + } + } + return rc; +} + +/* +** The first parameter (pDef) is a function implementation. The +** second parameter (pExpr) is the first argument to this function. +** If pExpr is a column in a virtual table, then let the virtual +** table implementation have an opportunity to overload the function. +** +** This routine is used to allow virtual table implementations to +** overload MATCH, LIKE, GLOB, and REGEXP operators. +** +** Return either the pDef argument (indicating no change) or a +** new FuncDef structure that is marked as ephemeral using the +** SQLITE_FUNC_EPHEM flag. +*/ +SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction( + sqlite3 *db, /* Database connection for reporting malloc problems */ + FuncDef *pDef, /* Function to possibly overload */ + int nArg, /* Number of arguments to the function */ + Expr *pExpr /* First argument to the function */ +){ + Table *pTab; + sqlite3_vtab *pVtab; + sqlite3_module *pMod; + void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0; + void *pArg = 0; + FuncDef *pNew; + int rc = 0; + char *zLowerName; + unsigned char *z; + + + /* Check to see the left operand is a column in a virtual table */ + if( NEVER(pExpr==0) ) return pDef; + if( pExpr->op!=TK_COLUMN ) return pDef; + pTab = pExpr->pTab; + if( NEVER(pTab==0) ) return pDef; + if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef; + pVtab = sqlite3GetVTable(db, pTab)->pVtab; + assert( pVtab!=0 ); + assert( pVtab->pModule!=0 ); + pMod = (sqlite3_module *)pVtab->pModule; + if( pMod->xFindFunction==0 ) return pDef; + + /* Call the xFindFunction method on the virtual table implementation + ** to see if the implementation wants to overload this function + */ + zLowerName = sqlite3DbStrDup(db, pDef->zName); + if( zLowerName ){ + for(z=(unsigned char*)zLowerName; *z; z++){ + *z = sqlite3UpperToLower[*z]; + } + rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg); + sqlite3DbFree(db, zLowerName); + } + if( rc==0 ){ + return pDef; + } + + /* Create a new ephemeral function definition for the overloaded + ** function */ + pNew = sqlite3DbMallocZero(db, sizeof(*pNew) + + sqlite3Strlen30(pDef->zName) + 1); + if( pNew==0 ){ + return pDef; + } + *pNew = *pDef; + pNew->zName = (char *)&pNew[1]; + memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1); + pNew->xFunc = xFunc; + pNew->pUserData = pArg; + pNew->flags |= SQLITE_FUNC_EPHEM; + return pNew; +} + +/* +** Make sure virtual table pTab is contained in the pParse->apVirtualLock[] +** array so that an OP_VBegin will get generated for it. Add pTab to the +** array if it is missing. If pTab is already in the array, this routine +** is a no-op. +*/ +SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){ + Parse *pToplevel = sqlite3ParseToplevel(pParse); + int i, n; + Table **apVtabLock; + + assert( IsVirtual(pTab) ); + for(i=0; inVtabLock; i++){ + if( pTab==pToplevel->apVtabLock[i] ) return; + } + n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]); + apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n); + if( apVtabLock ){ + pToplevel->apVtabLock = apVtabLock; + pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab; + }else{ + pToplevel->db->mallocFailed = 1; + } +} + +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +/************** End of vtab.c ************************************************/ +/************** Begin file where.c *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This module contains C code that generates VDBE code used to process +** the WHERE clause of SQL statements. This module is responsible for +** generating the code that loops through a table looking for applicable +** rows. Indices are selected and used to speed the search when doing +** so is applicable. Because this module is responsible for selecting +** indices, you might also think of this module as the "query optimizer". +*/ + + +/* +** Trace output macros +*/ +#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG) +SQLITE_PRIVATE int sqlite3WhereTrace = 0; +#endif +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) +# define WHERETRACE(X) if(sqlite3WhereTrace) sqlite3DebugPrintf X +#else +# define WHERETRACE(X) +#endif + +/* Forward reference +*/ +typedef struct WhereClause WhereClause; +typedef struct WhereMaskSet WhereMaskSet; +typedef struct WhereOrInfo WhereOrInfo; +typedef struct WhereAndInfo WhereAndInfo; +typedef struct WhereCost WhereCost; + +/* +** The query generator uses an array of instances of this structure to +** help it analyze the subexpressions of the WHERE clause. Each WHERE +** clause subexpression is separated from the others by AND operators, +** usually, or sometimes subexpressions separated by OR. +** +** All WhereTerms are collected into a single WhereClause structure. +** The following identity holds: +** +** WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm +** +** When a term is of the form: +** +** X +** +** where X is a column name and is one of certain operators, +** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the +** cursor number and column number for X. WhereTerm.eOperator records +** the using a bitmask encoding defined by WO_xxx below. The +** use of a bitmask encoding for the operator allows us to search +** quickly for terms that match any of several different operators. +** +** A WhereTerm might also be two or more subterms connected by OR: +** +** (t1.X ) OR (t1.Y ) OR .... +** +** In this second case, wtFlag as the TERM_ORINFO set and eOperator==WO_OR +** and the WhereTerm.u.pOrInfo field points to auxiliary information that +** is collected about the +** +** If a term in the WHERE clause does not match either of the two previous +** categories, then eOperator==0. The WhereTerm.pExpr field is still set +** to the original subexpression content and wtFlags is set up appropriately +** but no other fields in the WhereTerm object are meaningful. +** +** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers, +** but they do so indirectly. A single WhereMaskSet structure translates +** cursor number into bits and the translated bit is stored in the prereq +** fields. The translation is used in order to maximize the number of +** bits that will fit in a Bitmask. The VDBE cursor numbers might be +** spread out over the non-negative integers. For example, the cursor +** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45. The WhereMaskSet +** translates these sparse cursor numbers into consecutive integers +** beginning with 0 in order to make the best possible use of the available +** bits in the Bitmask. So, in the example above, the cursor numbers +** would be mapped into integers 0 through 7. +** +** The number of terms in a join is limited by the number of bits +** in prereqRight and prereqAll. The default is 64 bits, hence SQLite +** is only able to process joins with 64 or fewer tables. +*/ +typedef struct WhereTerm WhereTerm; +struct WhereTerm { + Expr *pExpr; /* Pointer to the subexpression that is this term */ + int iParent; /* Disable pWC->a[iParent] when this term disabled */ + int leftCursor; /* Cursor number of X in "X " */ + union { + int leftColumn; /* Column number of X in "X " */ + WhereOrInfo *pOrInfo; /* Extra information if eOperator==WO_OR */ + WhereAndInfo *pAndInfo; /* Extra information if eOperator==WO_AND */ + } u; + u16 eOperator; /* A WO_xx value describing */ + u8 wtFlags; /* TERM_xxx bit flags. See below */ + u8 nChild; /* Number of children that must disable us */ + WhereClause *pWC; /* The clause this term is part of */ + Bitmask prereqRight; /* Bitmask of tables used by pExpr->pRight */ + Bitmask prereqAll; /* Bitmask of tables referenced by pExpr */ +}; + +/* +** Allowed values of WhereTerm.wtFlags +*/ +#define TERM_DYNAMIC 0x01 /* Need to call sqlite3ExprDelete(db, pExpr) */ +#define TERM_VIRTUAL 0x02 /* Added by the optimizer. Do not code */ +#define TERM_CODED 0x04 /* This term is already coded */ +#define TERM_COPIED 0x08 /* Has a child */ +#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */ +#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */ +#define TERM_OR_OK 0x40 /* Used during OR-clause processing */ +#ifdef SQLITE_ENABLE_STAT2 +# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */ +#else +# define TERM_VNULL 0x00 /* Disabled if not using stat2 */ +#endif + +/* +** An instance of the following structure holds all information about a +** WHERE clause. Mostly this is a container for one or more WhereTerms. +*/ +struct WhereClause { + Parse *pParse; /* The parser context */ + WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */ + Bitmask vmask; /* Bitmask identifying virtual table cursors */ + u8 op; /* Split operator. TK_AND or TK_OR */ + int nTerm; /* Number of terms */ + int nSlot; /* Number of entries in a[] */ + WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */ +#if defined(SQLITE_SMALL_STACK) + WhereTerm aStatic[1]; /* Initial static space for a[] */ +#else + WhereTerm aStatic[8]; /* Initial static space for a[] */ +#endif +}; + +/* +** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to +** a dynamically allocated instance of the following structure. +*/ +struct WhereOrInfo { + WhereClause wc; /* Decomposition into subterms */ + Bitmask indexable; /* Bitmask of all indexable tables in the clause */ +}; + +/* +** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to +** a dynamically allocated instance of the following structure. +*/ +struct WhereAndInfo { + WhereClause wc; /* The subexpression broken out */ +}; + +/* +** An instance of the following structure keeps track of a mapping +** between VDBE cursor numbers and bits of the bitmasks in WhereTerm. +** +** The VDBE cursor numbers are small integers contained in +** SrcList_item.iCursor and Expr.iTable fields. For any given WHERE +** clause, the cursor numbers might not begin with 0 and they might +** contain gaps in the numbering sequence. But we want to make maximum +** use of the bits in our bitmasks. This structure provides a mapping +** from the sparse cursor numbers into consecutive integers beginning +** with 0. +** +** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask +** corresponds VDBE cursor number B. The A-th bit of a bitmask is 1<3, 5->1, 8->2, 29->0, +** 57->5, 73->4. Or one of 719 other combinations might be used. It +** does not really matter. What is important is that sparse cursor +** numbers all get mapped into bit numbers that begin with 0 and contain +** no gaps. +*/ +struct WhereMaskSet { + int n; /* Number of assigned cursor values */ + int ix[BMS]; /* Cursor assigned to each bit */ +}; + +/* +** A WhereCost object records a lookup strategy and the estimated +** cost of pursuing that strategy. +*/ +struct WhereCost { + WherePlan plan; /* The lookup strategy */ + double rCost; /* Overall cost of pursuing this search strategy */ + Bitmask used; /* Bitmask of cursors used by this plan */ +}; + +/* +** Bitmasks for the operators that indices are able to exploit. An +** OR-ed combination of these values can be used when searching for +** terms in the where clause. +*/ +#define WO_IN 0x001 +#define WO_EQ 0x002 +#define WO_LT (WO_EQ<<(TK_LT-TK_EQ)) +#define WO_LE (WO_EQ<<(TK_LE-TK_EQ)) +#define WO_GT (WO_EQ<<(TK_GT-TK_EQ)) +#define WO_GE (WO_EQ<<(TK_GE-TK_EQ)) +#define WO_MATCH 0x040 +#define WO_ISNULL 0x080 +#define WO_OR 0x100 /* Two or more OR-connected terms */ +#define WO_AND 0x200 /* Two or more AND-connected terms */ +#define WO_NOOP 0x800 /* This term does not restrict search space */ + +#define WO_ALL 0xfff /* Mask of all possible WO_* values */ +#define WO_SINGLE 0x0ff /* Mask of all non-compound WO_* values */ + +/* +** Value for wsFlags returned by bestIndex() and stored in +** WhereLevel.wsFlags. These flags determine which search +** strategies are appropriate. +** +** The least significant 12 bits is reserved as a mask for WO_ values above. +** The WhereLevel.wsFlags field is usually set to WO_IN|WO_EQ|WO_ISNULL. +** But if the table is the right table of a left join, WhereLevel.wsFlags +** is set to WO_IN|WO_EQ. The WhereLevel.wsFlags field can then be used as +** the "op" parameter to findTerm when we are resolving equality constraints. +** ISNULL constraints will then not be used on the right table of a left +** join. Tickets #2177 and #2189. +*/ +#define WHERE_ROWID_EQ 0x00001000 /* rowid=EXPR or rowid IN (...) */ +#define WHERE_ROWID_RANGE 0x00002000 /* rowidEXPR */ +#define WHERE_COLUMN_EQ 0x00010000 /* x=EXPR or x IN (...) or x IS NULL */ +#define WHERE_COLUMN_RANGE 0x00020000 /* xEXPR */ +#define WHERE_COLUMN_IN 0x00040000 /* x IN (...) */ +#define WHERE_COLUMN_NULL 0x00080000 /* x IS NULL */ +#define WHERE_INDEXED 0x000f0000 /* Anything that uses an index */ +#define WHERE_NOT_FULLSCAN 0x100f3000 /* Does not do a full table scan */ +#define WHERE_IN_ABLE 0x000f1000 /* Able to support an IN operator */ +#define WHERE_TOP_LIMIT 0x00100000 /* xEXPR or x>=EXPR constraint */ +#define WHERE_BOTH_LIMIT 0x00300000 /* Both x>EXPR and xpParse = pParse; + pWC->pMaskSet = pMaskSet; + pWC->nTerm = 0; + pWC->nSlot = ArraySize(pWC->aStatic); + pWC->a = pWC->aStatic; + pWC->vmask = 0; +} + +/* Forward reference */ +static void whereClauseClear(WhereClause*); + +/* +** Deallocate all memory associated with a WhereOrInfo object. +*/ +static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){ + whereClauseClear(&p->wc); + sqlite3DbFree(db, p); +} + +/* +** Deallocate all memory associated with a WhereAndInfo object. +*/ +static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){ + whereClauseClear(&p->wc); + sqlite3DbFree(db, p); +} + +/* +** Deallocate a WhereClause structure. The WhereClause structure +** itself is not freed. This routine is the inverse of whereClauseInit(). +*/ +static void whereClauseClear(WhereClause *pWC){ + int i; + WhereTerm *a; + sqlite3 *db = pWC->pParse->db; + for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){ + if( a->wtFlags & TERM_DYNAMIC ){ + sqlite3ExprDelete(db, a->pExpr); + } + if( a->wtFlags & TERM_ORINFO ){ + whereOrInfoDelete(db, a->u.pOrInfo); + }else if( a->wtFlags & TERM_ANDINFO ){ + whereAndInfoDelete(db, a->u.pAndInfo); + } + } + if( pWC->a!=pWC->aStatic ){ + sqlite3DbFree(db, pWC->a); + } +} + +/* +** Add a single new WhereTerm entry to the WhereClause object pWC. +** The new WhereTerm object is constructed from Expr p and with wtFlags. +** The index in pWC->a[] of the new WhereTerm is returned on success. +** 0 is returned if the new WhereTerm could not be added due to a memory +** allocation error. The memory allocation failure will be recorded in +** the db->mallocFailed flag so that higher-level functions can detect it. +** +** This routine will increase the size of the pWC->a[] array as necessary. +** +** If the wtFlags argument includes TERM_DYNAMIC, then responsibility +** for freeing the expression p is assumed by the WhereClause object pWC. +** This is true even if this routine fails to allocate a new WhereTerm. +** +** WARNING: This routine might reallocate the space used to store +** WhereTerms. All pointers to WhereTerms should be invalidated after +** calling this routine. Such pointers may be reinitialized by referencing +** the pWC->a[] array. +*/ +static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){ + WhereTerm *pTerm; + int idx; + testcase( wtFlags & TERM_VIRTUAL ); /* EV: R-00211-15100 */ + if( pWC->nTerm>=pWC->nSlot ){ + WhereTerm *pOld = pWC->a; + sqlite3 *db = pWC->pParse->db; + pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 ); + if( pWC->a==0 ){ + if( wtFlags & TERM_DYNAMIC ){ + sqlite3ExprDelete(db, p); + } + pWC->a = pOld; + return 0; + } + memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm); + if( pOld!=pWC->aStatic ){ + sqlite3DbFree(db, pOld); + } + pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]); + } + pTerm = &pWC->a[idx = pWC->nTerm++]; + pTerm->pExpr = p; + pTerm->wtFlags = wtFlags; + pTerm->pWC = pWC; + pTerm->iParent = -1; + return idx; +} + +/* +** This routine identifies subexpressions in the WHERE clause where +** each subexpression is separated by the AND operator or some other +** operator specified in the op parameter. The WhereClause structure +** is filled with pointers to subexpressions. For example: +** +** WHERE a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22) +** \________/ \_______________/ \________________/ +** slot[0] slot[1] slot[2] +** +** The original WHERE clause in pExpr is unaltered. All this routine +** does is make slot[] entries point to substructure within pExpr. +** +** In the previous sentence and in the diagram, "slot[]" refers to +** the WhereClause.a[] array. The slot[] array grows as needed to contain +** all terms of the WHERE clause. +*/ +static void whereSplit(WhereClause *pWC, Expr *pExpr, int op){ + pWC->op = (u8)op; + if( pExpr==0 ) return; + if( pExpr->op!=op ){ + whereClauseInsert(pWC, pExpr, 0); + }else{ + whereSplit(pWC, pExpr->pLeft, op); + whereSplit(pWC, pExpr->pRight, op); + } +} + +/* +** Initialize an expression mask set (a WhereMaskSet object) +*/ +#define initMaskSet(P) memset(P, 0, sizeof(*P)) + +/* +** Return the bitmask for the given cursor number. Return 0 if +** iCursor is not in the set. +*/ +static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){ + int i; + assert( pMaskSet->n<=sizeof(Bitmask)*8 ); + for(i=0; in; i++){ + if( pMaskSet->ix[i]==iCursor ){ + return ((Bitmask)1)<ix[] +** array will never overflow. +*/ +static void createMask(WhereMaskSet *pMaskSet, int iCursor){ + assert( pMaskSet->n < ArraySize(pMaskSet->ix) ); + pMaskSet->ix[pMaskSet->n++] = iCursor; +} + +/* +** This routine walks (recursively) an expression tree and generates +** a bitmask indicating which tables are used in that expression +** tree. +** +** In order for this routine to work, the calling function must have +** previously invoked sqlite3ResolveExprNames() on the expression. See +** the header comment on that routine for additional information. +** The sqlite3ResolveExprNames() routines looks for column names and +** sets their opcodes to TK_COLUMN and their Expr.iTable fields to +** the VDBE cursor number of the table. This routine just has to +** translate the cursor numbers into bitmask values and OR all +** the bitmasks together. +*/ +static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*); +static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*); +static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){ + Bitmask mask = 0; + if( p==0 ) return 0; + if( p->op==TK_COLUMN ){ + mask = getMask(pMaskSet, p->iTable); + return mask; + } + mask = exprTableUsage(pMaskSet, p->pRight); + mask |= exprTableUsage(pMaskSet, p->pLeft); + if( ExprHasProperty(p, EP_xIsSelect) ){ + mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect); + }else{ + mask |= exprListTableUsage(pMaskSet, p->x.pList); + } + return mask; +} +static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){ + int i; + Bitmask mask = 0; + if( pList ){ + for(i=0; inExpr; i++){ + mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr); + } + } + return mask; +} +static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){ + Bitmask mask = 0; + while( pS ){ + mask |= exprListTableUsage(pMaskSet, pS->pEList); + mask |= exprListTableUsage(pMaskSet, pS->pGroupBy); + mask |= exprListTableUsage(pMaskSet, pS->pOrderBy); + mask |= exprTableUsage(pMaskSet, pS->pWhere); + mask |= exprTableUsage(pMaskSet, pS->pHaving); + pS = pS->pPrior; + } + return mask; +} + +/* +** Return TRUE if the given operator is one of the operators that is +** allowed for an indexable WHERE clause term. The allowed operators are +** "=", "<", ">", "<=", ">=", and "IN". +** +** IMPLEMENTATION-OF: R-59926-26393 To be usable by an index a term must be +** of one of the following forms: column = expression column > expression +** column >= expression column < expression column <= expression +** expression = column expression > column expression >= column +** expression < column expression <= column column IN +** (expression-list) column IN (subquery) column IS NULL +*/ +static int allowedOp(int op){ + assert( TK_GT>TK_EQ && TK_GTTK_EQ && TK_LTTK_EQ && TK_LE=TK_EQ && op<=TK_GE) || op==TK_ISNULL; +} + +/* +** Swap two objects of type TYPE. +*/ +#define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;} + +/* +** Commute a comparison operator. Expressions of the form "X op Y" +** are converted into "Y op X". +** +** If a collation sequence is associated with either the left or right +** side of the comparison, it remains associated with the same side after +** the commutation. So "Y collate NOCASE op X" becomes +** "X collate NOCASE op Y". This is because any collation sequence on +** the left hand side of a comparison overrides any collation sequence +** attached to the right. For the same reason the EP_ExpCollate flag +** is not commuted. +*/ +static void exprCommute(Parse *pParse, Expr *pExpr){ + u16 expRight = (pExpr->pRight->flags & EP_ExpCollate); + u16 expLeft = (pExpr->pLeft->flags & EP_ExpCollate); + assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN ); + pExpr->pRight->pColl = sqlite3ExprCollSeq(pParse, pExpr->pRight); + pExpr->pLeft->pColl = sqlite3ExprCollSeq(pParse, pExpr->pLeft); + SWAP(CollSeq*,pExpr->pRight->pColl,pExpr->pLeft->pColl); + pExpr->pRight->flags = (pExpr->pRight->flags & ~EP_ExpCollate) | expLeft; + pExpr->pLeft->flags = (pExpr->pLeft->flags & ~EP_ExpCollate) | expRight; + SWAP(Expr*,pExpr->pRight,pExpr->pLeft); + if( pExpr->op>=TK_GT ){ + assert( TK_LT==TK_GT+2 ); + assert( TK_GE==TK_LE+2 ); + assert( TK_GT>TK_EQ ); + assert( TK_GTop>=TK_GT && pExpr->op<=TK_GE ); + pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT; + } +} + +/* +** Translate from TK_xx operator to WO_xx bitmask. +*/ +static u16 operatorMask(int op){ + u16 c; + assert( allowedOp(op) ); + if( op==TK_IN ){ + c = WO_IN; + }else if( op==TK_ISNULL ){ + c = WO_ISNULL; + }else{ + assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff ); + c = (u16)(WO_EQ<<(op-TK_EQ)); + } + assert( op!=TK_ISNULL || c==WO_ISNULL ); + assert( op!=TK_IN || c==WO_IN ); + assert( op!=TK_EQ || c==WO_EQ ); + assert( op!=TK_LT || c==WO_LT ); + assert( op!=TK_LE || c==WO_LE ); + assert( op!=TK_GT || c==WO_GT ); + assert( op!=TK_GE || c==WO_GE ); + return c; +} + +/* +** Search for a term in the WHERE clause that is of the form "X " +** where X is a reference to the iColumn of table iCur and is one of +** the WO_xx operator codes specified by the op parameter. +** Return a pointer to the term. Return 0 if not found. +*/ +static WhereTerm *findTerm( + WhereClause *pWC, /* The WHERE clause to be searched */ + int iCur, /* Cursor number of LHS */ + int iColumn, /* Column number of LHS */ + Bitmask notReady, /* RHS must not overlap with this mask */ + u32 op, /* Mask of WO_xx values describing operator */ + Index *pIdx /* Must be compatible with this index, if not NULL */ +){ + WhereTerm *pTerm; + int k; + assert( iCur>=0 ); + op &= WO_ALL; + for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){ + if( pTerm->leftCursor==iCur + && (pTerm->prereqRight & notReady)==0 + && pTerm->u.leftColumn==iColumn + && (pTerm->eOperator & op)!=0 + ){ + if( pIdx && pTerm->eOperator!=WO_ISNULL ){ + Expr *pX = pTerm->pExpr; + CollSeq *pColl; + char idxaff; + int j; + Parse *pParse = pWC->pParse; + + idxaff = pIdx->pTable->aCol[iColumn].affinity; + if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue; + + /* Figure out the collation sequence required from an index for + ** it to be useful for optimising expression pX. Store this + ** value in variable pColl. + */ + assert(pX->pLeft); + pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); + assert(pColl || pParse->nErr); + + for(j=0; pIdx->aiColumn[j]!=iColumn; j++){ + if( NEVER(j>=pIdx->nColumn) ) return 0; + } + if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue; + } + return pTerm; + } + } + return 0; +} + +/* Forward reference */ +static void exprAnalyze(SrcList*, WhereClause*, int); + +/* +** Call exprAnalyze on all terms in a WHERE clause. +** +** +*/ +static void exprAnalyzeAll( + SrcList *pTabList, /* the FROM clause */ + WhereClause *pWC /* the WHERE clause to be analyzed */ +){ + int i; + for(i=pWC->nTerm-1; i>=0; i--){ + exprAnalyze(pTabList, pWC, i); + } +} + +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION +/* +** Check to see if the given expression is a LIKE or GLOB operator that +** can be optimized using inequality constraints. Return TRUE if it is +** so and false if not. +** +** In order for the operator to be optimizible, the RHS must be a string +** literal that does not begin with a wildcard. +*/ +static int isLikeOrGlob( + Parse *pParse, /* Parsing and code generating context */ + Expr *pExpr, /* Test this expression */ + Expr **ppPrefix, /* Pointer to TK_STRING expression with pattern prefix */ + int *pisComplete, /* True if the only wildcard is % in the last character */ + int *pnoCase /* True if uppercase is equivalent to lowercase */ +){ + const char *z = 0; /* String on RHS of LIKE operator */ + Expr *pRight, *pLeft; /* Right and left size of LIKE operator */ + ExprList *pList; /* List of operands to the LIKE operator */ + int c; /* One character in z[] */ + int cnt; /* Number of non-wildcard prefix characters */ + char wc[3]; /* Wildcard characters */ + sqlite3 *db = pParse->db; /* Database connection */ + sqlite3_value *pVal = 0; + int op; /* Opcode of pRight */ + + if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){ + return 0; + } +#ifdef SQLITE_EBCDIC + if( *pnoCase ) return 0; +#endif + pList = pExpr->x.pList; + pLeft = pList->a[1].pExpr; + if( pLeft->op!=TK_COLUMN || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT ){ + /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must + ** be the name of an indexed column with TEXT affinity. */ + return 0; + } + assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */ + + pRight = pList->a[0].pExpr; + op = pRight->op; + if( op==TK_REGISTER ){ + op = pRight->op2; + } + if( op==TK_VARIABLE ){ + Vdbe *pReprepare = pParse->pReprepare; + int iCol = pRight->iColumn; + pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE); + if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){ + z = (char *)sqlite3_value_text(pVal); + } + sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */ + assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER ); + }else if( op==TK_STRING ){ + z = pRight->u.zToken; + } + if( z ){ + cnt = 0; + while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){ + cnt++; + } + if( cnt!=0 && 255!=(u8)z[cnt-1] ){ + Expr *pPrefix; + *pisComplete = c==wc[0] && z[cnt+1]==0; + pPrefix = sqlite3Expr(db, TK_STRING, z); + if( pPrefix ) pPrefix->u.zToken[cnt] = 0; + *ppPrefix = pPrefix; + if( op==TK_VARIABLE ){ + Vdbe *v = pParse->pVdbe; + sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */ + if( *pisComplete && pRight->u.zToken[1] ){ + /* If the rhs of the LIKE expression is a variable, and the current + ** value of the variable means there is no need to invoke the LIKE + ** function, then no OP_Variable will be added to the program. + ** This causes problems for the sqlite3_bind_parameter_name() + ** API. To workaround them, add a dummy OP_Variable here. + */ + int r1 = sqlite3GetTempReg(pParse); + sqlite3ExprCodeTarget(pParse, pRight, r1); + sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0); + sqlite3ReleaseTempReg(pParse, r1); + } + } + }else{ + z = 0; + } + } + + sqlite3ValueFree(pVal); + return (z!=0); +} +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ + + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Check to see if the given expression is of the form +** +** column MATCH expr +** +** If it is then return TRUE. If not, return FALSE. +*/ +static int isMatchOfColumn( + Expr *pExpr /* Test this expression */ +){ + ExprList *pList; + + if( pExpr->op!=TK_FUNCTION ){ + return 0; + } + if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){ + return 0; + } + pList = pExpr->x.pList; + if( pList->nExpr!=2 ){ + return 0; + } + if( pList->a[1].pExpr->op != TK_COLUMN ){ + return 0; + } + return 1; +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +/* +** If the pBase expression originated in the ON or USING clause of +** a join, then transfer the appropriate markings over to derived. +*/ +static void transferJoinMarkings(Expr *pDerived, Expr *pBase){ + pDerived->flags |= pBase->flags & EP_FromJoin; + pDerived->iRightJoinTable = pBase->iRightJoinTable; +} + +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) +/* +** Analyze a term that consists of two or more OR-connected +** subterms. So in: +** +** ... WHERE (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13) +** ^^^^^^^^^^^^^^^^^^^^ +** +** This routine analyzes terms such as the middle term in the above example. +** A WhereOrTerm object is computed and attached to the term under +** analysis, regardless of the outcome of the analysis. Hence: +** +** WhereTerm.wtFlags |= TERM_ORINFO +** WhereTerm.u.pOrInfo = a dynamically allocated WhereOrTerm object +** +** The term being analyzed must have two or more of OR-connected subterms. +** A single subterm might be a set of AND-connected sub-subterms. +** Examples of terms under analysis: +** +** (A) t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5 +** (B) x=expr1 OR expr2=x OR x=expr3 +** (C) t1.x=t2.y OR (t1.x=t2.z AND t1.y=15) +** (D) x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*') +** (E) (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6) +** +** CASE 1: +** +** If all subterms are of the form T.C=expr for some single column of C +** a single table T (as shown in example B above) then create a new virtual +** term that is an equivalent IN expression. In other words, if the term +** being analyzed is: +** +** x = expr1 OR expr2 = x OR x = expr3 +** +** then create a new virtual term like this: +** +** x IN (expr1,expr2,expr3) +** +** CASE 2: +** +** If all subterms are indexable by a single table T, then set +** +** WhereTerm.eOperator = WO_OR +** WhereTerm.u.pOrInfo->indexable |= the cursor number for table T +** +** A subterm is "indexable" if it is of the form +** "T.C " where C is any column of table T and +** is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN". +** A subterm is also indexable if it is an AND of two or more +** subsubterms at least one of which is indexable. Indexable AND +** subterms have their eOperator set to WO_AND and they have +** u.pAndInfo set to a dynamically allocated WhereAndTerm object. +** +** From another point of view, "indexable" means that the subterm could +** potentially be used with an index if an appropriate index exists. +** This analysis does not consider whether or not the index exists; that +** is something the bestIndex() routine will determine. This analysis +** only looks at whether subterms appropriate for indexing exist. +** +** All examples A through E above all satisfy case 2. But if a term +** also statisfies case 1 (such as B) we know that the optimizer will +** always prefer case 1, so in that case we pretend that case 2 is not +** satisfied. +** +** It might be the case that multiple tables are indexable. For example, +** (E) above is indexable on tables P, Q, and R. +** +** Terms that satisfy case 2 are candidates for lookup by using +** separate indices to find rowids for each subterm and composing +** the union of all rowids using a RowSet object. This is similar +** to "bitmap indices" in other database engines. +** +** OTHERWISE: +** +** If neither case 1 nor case 2 apply, then leave the eOperator set to +** zero. This term is not useful for search. +*/ +static void exprAnalyzeOrTerm( + SrcList *pSrc, /* the FROM clause */ + WhereClause *pWC, /* the complete WHERE clause */ + int idxTerm /* Index of the OR-term to be analyzed */ +){ + Parse *pParse = pWC->pParse; /* Parser context */ + sqlite3 *db = pParse->db; /* Database connection */ + WhereTerm *pTerm = &pWC->a[idxTerm]; /* The term to be analyzed */ + Expr *pExpr = pTerm->pExpr; /* The expression of the term */ + WhereMaskSet *pMaskSet = pWC->pMaskSet; /* Table use masks */ + int i; /* Loop counters */ + WhereClause *pOrWc; /* Breakup of pTerm into subterms */ + WhereTerm *pOrTerm; /* A Sub-term within the pOrWc */ + WhereOrInfo *pOrInfo; /* Additional information associated with pTerm */ + Bitmask chngToIN; /* Tables that might satisfy case 1 */ + Bitmask indexable; /* Tables that are indexable, satisfying case 2 */ + + /* + ** Break the OR clause into its separate subterms. The subterms are + ** stored in a WhereClause structure containing within the WhereOrInfo + ** object that is attached to the original OR clause term. + */ + assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 ); + assert( pExpr->op==TK_OR ); + pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo)); + if( pOrInfo==0 ) return; + pTerm->wtFlags |= TERM_ORINFO; + pOrWc = &pOrInfo->wc; + whereClauseInit(pOrWc, pWC->pParse, pMaskSet); + whereSplit(pOrWc, pExpr, TK_OR); + exprAnalyzeAll(pSrc, pOrWc); + if( db->mallocFailed ) return; + assert( pOrWc->nTerm>=2 ); + + /* + ** Compute the set of tables that might satisfy cases 1 or 2. + */ + indexable = ~(Bitmask)0; + chngToIN = ~(pWC->vmask); + for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){ + if( (pOrTerm->eOperator & WO_SINGLE)==0 ){ + WhereAndInfo *pAndInfo; + assert( pOrTerm->eOperator==0 ); + assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 ); + chngToIN = 0; + pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo)); + if( pAndInfo ){ + WhereClause *pAndWC; + WhereTerm *pAndTerm; + int j; + Bitmask b = 0; + pOrTerm->u.pAndInfo = pAndInfo; + pOrTerm->wtFlags |= TERM_ANDINFO; + pOrTerm->eOperator = WO_AND; + pAndWC = &pAndInfo->wc; + whereClauseInit(pAndWC, pWC->pParse, pMaskSet); + whereSplit(pAndWC, pOrTerm->pExpr, TK_AND); + exprAnalyzeAll(pSrc, pAndWC); + testcase( db->mallocFailed ); + if( !db->mallocFailed ){ + for(j=0, pAndTerm=pAndWC->a; jnTerm; j++, pAndTerm++){ + assert( pAndTerm->pExpr ); + if( allowedOp(pAndTerm->pExpr->op) ){ + b |= getMask(pMaskSet, pAndTerm->leftCursor); + } + } + } + indexable &= b; + } + }else if( pOrTerm->wtFlags & TERM_COPIED ){ + /* Skip this term for now. We revisit it when we process the + ** corresponding TERM_VIRTUAL term */ + }else{ + Bitmask b; + b = getMask(pMaskSet, pOrTerm->leftCursor); + if( pOrTerm->wtFlags & TERM_VIRTUAL ){ + WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent]; + b |= getMask(pMaskSet, pOther->leftCursor); + } + indexable &= b; + if( pOrTerm->eOperator!=WO_EQ ){ + chngToIN = 0; + }else{ + chngToIN &= b; + } + } + } + + /* + ** Record the set of tables that satisfy case 2. The set might be + ** empty. + */ + pOrInfo->indexable = indexable; + pTerm->eOperator = indexable==0 ? 0 : WO_OR; + + /* + ** chngToIN holds a set of tables that *might* satisfy case 1. But + ** we have to do some additional checking to see if case 1 really + ** is satisfied. + ** + ** chngToIN will hold either 0, 1, or 2 bits. The 0-bit case means + ** that there is no possibility of transforming the OR clause into an + ** IN operator because one or more terms in the OR clause contain + ** something other than == on a column in the single table. The 1-bit + ** case means that every term of the OR clause is of the form + ** "table.column=expr" for some single table. The one bit that is set + ** will correspond to the common table. We still need to check to make + ** sure the same column is used on all terms. The 2-bit case is when + ** the all terms are of the form "table1.column=table2.column". It + ** might be possible to form an IN operator with either table1.column + ** or table2.column as the LHS if either is common to every term of + ** the OR clause. + ** + ** Note that terms of the form "table.column1=table.column2" (the + ** same table on both sizes of the ==) cannot be optimized. + */ + if( chngToIN ){ + int okToChngToIN = 0; /* True if the conversion to IN is valid */ + int iColumn = -1; /* Column index on lhs of IN operator */ + int iCursor = -1; /* Table cursor common to all terms */ + int j = 0; /* Loop counter */ + + /* Search for a table and column that appears on one side or the + ** other of the == operator in every subterm. That table and column + ** will be recorded in iCursor and iColumn. There might not be any + ** such table and column. Set okToChngToIN if an appropriate table + ** and column is found but leave okToChngToIN false if not found. + */ + for(j=0; j<2 && !okToChngToIN; j++){ + pOrTerm = pOrWc->a; + for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){ + assert( pOrTerm->eOperator==WO_EQ ); + pOrTerm->wtFlags &= ~TERM_OR_OK; + if( pOrTerm->leftCursor==iCursor ){ + /* This is the 2-bit case and we are on the second iteration and + ** current term is from the first iteration. So skip this term. */ + assert( j==1 ); + continue; + } + if( (chngToIN & getMask(pMaskSet, pOrTerm->leftCursor))==0 ){ + /* This term must be of the form t1.a==t2.b where t2 is in the + ** chngToIN set but t1 is not. This term will be either preceeded + ** or follwed by an inverted copy (t2.b==t1.a). Skip this term + ** and use its inversion. */ + testcase( pOrTerm->wtFlags & TERM_COPIED ); + testcase( pOrTerm->wtFlags & TERM_VIRTUAL ); + assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) ); + continue; + } + iColumn = pOrTerm->u.leftColumn; + iCursor = pOrTerm->leftCursor; + break; + } + if( i<0 ){ + /* No candidate table+column was found. This can only occur + ** on the second iteration */ + assert( j==1 ); + assert( (chngToIN&(chngToIN-1))==0 ); + assert( chngToIN==getMask(pMaskSet, iCursor) ); + break; + } + testcase( j==1 ); + + /* We have found a candidate table and column. Check to see if that + ** table and column is common to every term in the OR clause */ + okToChngToIN = 1; + for(; i>=0 && okToChngToIN; i--, pOrTerm++){ + assert( pOrTerm->eOperator==WO_EQ ); + if( pOrTerm->leftCursor!=iCursor ){ + pOrTerm->wtFlags &= ~TERM_OR_OK; + }else if( pOrTerm->u.leftColumn!=iColumn ){ + okToChngToIN = 0; + }else{ + int affLeft, affRight; + /* If the right-hand side is also a column, then the affinities + ** of both right and left sides must be such that no type + ** conversions are required on the right. (Ticket #2249) + */ + affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight); + affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft); + if( affRight!=0 && affRight!=affLeft ){ + okToChngToIN = 0; + }else{ + pOrTerm->wtFlags |= TERM_OR_OK; + } + } + } + } + + /* At this point, okToChngToIN is true if original pTerm satisfies + ** case 1. In that case, construct a new virtual term that is + ** pTerm converted into an IN operator. + ** + ** EV: R-00211-15100 + */ + if( okToChngToIN ){ + Expr *pDup; /* A transient duplicate expression */ + ExprList *pList = 0; /* The RHS of the IN operator */ + Expr *pLeft = 0; /* The LHS of the IN operator */ + Expr *pNew; /* The complete IN operator */ + + for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){ + if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue; + assert( pOrTerm->eOperator==WO_EQ ); + assert( pOrTerm->leftCursor==iCursor ); + assert( pOrTerm->u.leftColumn==iColumn ); + pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0); + pList = sqlite3ExprListAppend(pWC->pParse, pList, pDup); + pLeft = pOrTerm->pExpr->pLeft; + } + assert( pLeft!=0 ); + pDup = sqlite3ExprDup(db, pLeft, 0); + pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0); + if( pNew ){ + int idxNew; + transferJoinMarkings(pNew, pExpr); + assert( !ExprHasProperty(pNew, EP_xIsSelect) ); + pNew->x.pList = pList; + idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC); + testcase( idxNew==0 ); + exprAnalyze(pSrc, pWC, idxNew); + pTerm = &pWC->a[idxTerm]; + pWC->a[idxNew].iParent = idxTerm; + pTerm->nChild = 1; + }else{ + sqlite3ExprListDelete(db, pList); + } + pTerm->eOperator = WO_NOOP; /* case 1 trumps case 2 */ + } + } +} +#endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */ + + +/* +** The input to this routine is an WhereTerm structure with only the +** "pExpr" field filled in. The job of this routine is to analyze the +** subexpression and populate all the other fields of the WhereTerm +** structure. +** +** If the expression is of the form " X" it gets commuted +** to the standard form of "X ". +** +** If the expression is of the form "X Y" where both X and Y are +** columns, then the original expression is unchanged and a new virtual +** term of the form "Y X" is added to the WHERE clause and +** analyzed separately. The original term is marked with TERM_COPIED +** and the new term is marked with TERM_DYNAMIC (because it's pExpr +** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it +** is a commuted copy of a prior term.) The original term has nChild=1 +** and the copy has idxParent set to the index of the original term. +*/ +static void exprAnalyze( + SrcList *pSrc, /* the FROM clause */ + WhereClause *pWC, /* the WHERE clause */ + int idxTerm /* Index of the term to be analyzed */ +){ + WhereTerm *pTerm; /* The term to be analyzed */ + WhereMaskSet *pMaskSet; /* Set of table index masks */ + Expr *pExpr; /* The expression to be analyzed */ + Bitmask prereqLeft; /* Prerequesites of the pExpr->pLeft */ + Bitmask prereqAll; /* Prerequesites of pExpr */ + Bitmask extraRight = 0; /* Extra dependencies on LEFT JOIN */ + Expr *pStr1 = 0; /* RHS of LIKE/GLOB operator */ + int isComplete = 0; /* RHS of LIKE/GLOB ends with wildcard */ + int noCase = 0; /* LIKE/GLOB distinguishes case */ + int op; /* Top-level operator. pExpr->op */ + Parse *pParse = pWC->pParse; /* Parsing context */ + sqlite3 *db = pParse->db; /* Database connection */ + + if( db->mallocFailed ){ + return; + } + pTerm = &pWC->a[idxTerm]; + pMaskSet = pWC->pMaskSet; + pExpr = pTerm->pExpr; + prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft); + op = pExpr->op; + if( op==TK_IN ){ + assert( pExpr->pRight==0 ); + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect); + }else{ + pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList); + } + }else if( op==TK_ISNULL ){ + pTerm->prereqRight = 0; + }else{ + pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight); + } + prereqAll = exprTableUsage(pMaskSet, pExpr); + if( ExprHasProperty(pExpr, EP_FromJoin) ){ + Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable); + prereqAll |= x; + extraRight = x-1; /* ON clause terms may not be used with an index + ** on left table of a LEFT JOIN. Ticket #3015 */ + } + pTerm->prereqAll = prereqAll; + pTerm->leftCursor = -1; + pTerm->iParent = -1; + pTerm->eOperator = 0; + if( allowedOp(op) && (pTerm->prereqRight & prereqLeft)==0 ){ + Expr *pLeft = pExpr->pLeft; + Expr *pRight = pExpr->pRight; + if( pLeft->op==TK_COLUMN ){ + pTerm->leftCursor = pLeft->iTable; + pTerm->u.leftColumn = pLeft->iColumn; + pTerm->eOperator = operatorMask(op); + } + if( pRight && pRight->op==TK_COLUMN ){ + WhereTerm *pNew; + Expr *pDup; + if( pTerm->leftCursor>=0 ){ + int idxNew; + pDup = sqlite3ExprDup(db, pExpr, 0); + if( db->mallocFailed ){ + sqlite3ExprDelete(db, pDup); + return; + } + idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC); + if( idxNew==0 ) return; + pNew = &pWC->a[idxNew]; + pNew->iParent = idxTerm; + pTerm = &pWC->a[idxTerm]; + pTerm->nChild = 1; + pTerm->wtFlags |= TERM_COPIED; + }else{ + pDup = pExpr; + pNew = pTerm; + } + exprCommute(pParse, pDup); + pLeft = pDup->pLeft; + pNew->leftCursor = pLeft->iTable; + pNew->u.leftColumn = pLeft->iColumn; + testcase( (prereqLeft | extraRight) != prereqLeft ); + pNew->prereqRight = prereqLeft | extraRight; + pNew->prereqAll = prereqAll; + pNew->eOperator = operatorMask(pDup->op); + } + } + +#ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION + /* If a term is the BETWEEN operator, create two new virtual terms + ** that define the range that the BETWEEN implements. For example: + ** + ** a BETWEEN b AND c + ** + ** is converted into: + ** + ** (a BETWEEN b AND c) AND (a>=b) AND (a<=c) + ** + ** The two new terms are added onto the end of the WhereClause object. + ** The new terms are "dynamic" and are children of the original BETWEEN + ** term. That means that if the BETWEEN term is coded, the children are + ** skipped. Or, if the children are satisfied by an index, the original + ** BETWEEN term is skipped. + */ + else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){ + ExprList *pList = pExpr->x.pList; + int i; + static const u8 ops[] = {TK_GE, TK_LE}; + assert( pList!=0 ); + assert( pList->nExpr==2 ); + for(i=0; i<2; i++){ + Expr *pNewExpr; + int idxNew; + pNewExpr = sqlite3PExpr(pParse, ops[i], + sqlite3ExprDup(db, pExpr->pLeft, 0), + sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0); + idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); + testcase( idxNew==0 ); + exprAnalyze(pSrc, pWC, idxNew); + pTerm = &pWC->a[idxTerm]; + pWC->a[idxNew].iParent = idxTerm; + } + pTerm->nChild = 2; + } +#endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */ + +#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY) + /* Analyze a term that is composed of two or more subterms connected by + ** an OR operator. + */ + else if( pExpr->op==TK_OR ){ + assert( pWC->op==TK_AND ); + exprAnalyzeOrTerm(pSrc, pWC, idxTerm); + pTerm = &pWC->a[idxTerm]; + } +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */ + +#ifndef SQLITE_OMIT_LIKE_OPTIMIZATION + /* Add constraints to reduce the search space on a LIKE or GLOB + ** operator. + ** + ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints + ** + ** x>='abc' AND x<'abd' AND x LIKE 'abc%' + ** + ** The last character of the prefix "abc" is incremented to form the + ** termination condition "abd". + */ + if( pWC->op==TK_AND + && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase) + ){ + Expr *pLeft; /* LHS of LIKE/GLOB operator */ + Expr *pStr2; /* Copy of pStr1 - RHS of LIKE/GLOB operator */ + Expr *pNewExpr1; + Expr *pNewExpr2; + int idxNew1; + int idxNew2; + CollSeq *pColl; /* Collating sequence to use */ + + pLeft = pExpr->x.pList->a[1].pExpr; + pStr2 = sqlite3ExprDup(db, pStr1, 0); + if( !db->mallocFailed ){ + u8 c, *pC; /* Last character before the first wildcard */ + pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1]; + c = *pC; + if( noCase ){ + /* The point is to increment the last character before the first + ** wildcard. But if we increment '@', that will push it into the + ** alphabetic range where case conversions will mess up the + ** inequality. To avoid this, make sure to also run the full + ** LIKE on all candidate expressions by clearing the isComplete flag + */ + if( c=='A'-1 ) isComplete = 0; /* EV: R-64339-08207 */ + + + c = sqlite3UpperToLower[c]; + } + *pC = c + 1; + } + pColl = sqlite3FindCollSeq(db, SQLITE_UTF8, noCase ? "NOCASE" : "BINARY",0); + pNewExpr1 = sqlite3PExpr(pParse, TK_GE, + sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl), + pStr1, 0); + idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC); + testcase( idxNew1==0 ); + exprAnalyze(pSrc, pWC, idxNew1); + pNewExpr2 = sqlite3PExpr(pParse, TK_LT, + sqlite3ExprSetColl(sqlite3ExprDup(db,pLeft,0), pColl), + pStr2, 0); + idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC); + testcase( idxNew2==0 ); + exprAnalyze(pSrc, pWC, idxNew2); + pTerm = &pWC->a[idxTerm]; + if( isComplete ){ + pWC->a[idxNew1].iParent = idxTerm; + pWC->a[idxNew2].iParent = idxTerm; + pTerm->nChild = 2; + } + } +#endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE + /* Add a WO_MATCH auxiliary term to the constraint set if the + ** current expression is of the form: column MATCH expr. + ** This information is used by the xBestIndex methods of + ** virtual tables. The native query optimizer does not attempt + ** to do anything with MATCH functions. + */ + if( isMatchOfColumn(pExpr) ){ + int idxNew; + Expr *pRight, *pLeft; + WhereTerm *pNewTerm; + Bitmask prereqColumn, prereqExpr; + + pRight = pExpr->x.pList->a[0].pExpr; + pLeft = pExpr->x.pList->a[1].pExpr; + prereqExpr = exprTableUsage(pMaskSet, pRight); + prereqColumn = exprTableUsage(pMaskSet, pLeft); + if( (prereqExpr & prereqColumn)==0 ){ + Expr *pNewExpr; + pNewExpr = sqlite3PExpr(pParse, TK_MATCH, + 0, sqlite3ExprDup(db, pRight, 0), 0); + idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC); + testcase( idxNew==0 ); + pNewTerm = &pWC->a[idxNew]; + pNewTerm->prereqRight = prereqExpr; + pNewTerm->leftCursor = pLeft->iTable; + pNewTerm->u.leftColumn = pLeft->iColumn; + pNewTerm->eOperator = WO_MATCH; + pNewTerm->iParent = idxTerm; + pTerm = &pWC->a[idxTerm]; + pTerm->nChild = 1; + pTerm->wtFlags |= TERM_COPIED; + pNewTerm->prereqAll = pTerm->prereqAll; + } + } +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +#ifdef SQLITE_ENABLE_STAT2 + /* When sqlite_stat2 histogram data is available an operator of the + ** form "x IS NOT NULL" can sometimes be evaluated more efficiently + ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a + ** virtual term of that form. + ** + ** Note that the virtual term must be tagged with TERM_VNULL. This + ** TERM_VNULL tag will suppress the not-null check at the beginning + ** of the loop. Without the TERM_VNULL flag, the not-null check at + ** the start of the loop will prevent any results from being returned. + */ + if( pExpr->op==TK_NOTNULL && pExpr->pLeft->iColumn>=0 ){ + Expr *pNewExpr; + Expr *pLeft = pExpr->pLeft; + int idxNew; + WhereTerm *pNewTerm; + + pNewExpr = sqlite3PExpr(pParse, TK_GT, + sqlite3ExprDup(db, pLeft, 0), + sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0); + + idxNew = whereClauseInsert(pWC, pNewExpr, + TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL); + if( idxNew ){ + pNewTerm = &pWC->a[idxNew]; + pNewTerm->prereqRight = 0; + pNewTerm->leftCursor = pLeft->iTable; + pNewTerm->u.leftColumn = pLeft->iColumn; + pNewTerm->eOperator = WO_GT; + pNewTerm->iParent = idxTerm; + pTerm = &pWC->a[idxTerm]; + pTerm->nChild = 1; + pTerm->wtFlags |= TERM_COPIED; + pNewTerm->prereqAll = pTerm->prereqAll; + } + } +#endif /* SQLITE_ENABLE_STAT2 */ + + /* Prevent ON clause terms of a LEFT JOIN from being used to drive + ** an index for tables to the left of the join. + */ + pTerm->prereqRight |= extraRight; +} + +/* +** Return TRUE if any of the expressions in pList->a[iFirst...] contain +** a reference to any table other than the iBase table. +*/ +static int referencesOtherTables( + ExprList *pList, /* Search expressions in ths list */ + WhereMaskSet *pMaskSet, /* Mapping from tables to bitmaps */ + int iFirst, /* Be searching with the iFirst-th expression */ + int iBase /* Ignore references to this table */ +){ + Bitmask allowed = ~getMask(pMaskSet, iBase); + while( iFirstnExpr ){ + if( (exprTableUsage(pMaskSet, pList->a[iFirst++].pExpr)&allowed)!=0 ){ + return 1; + } + } + return 0; +} + + +/* +** This routine decides if pIdx can be used to satisfy the ORDER BY +** clause. If it can, it returns 1. If pIdx cannot satisfy the +** ORDER BY clause, this routine returns 0. +** +** pOrderBy is an ORDER BY clause from a SELECT statement. pTab is the +** left-most table in the FROM clause of that same SELECT statement and +** the table has a cursor number of "base". pIdx is an index on pTab. +** +** nEqCol is the number of columns of pIdx that are used as equality +** constraints. Any of these columns may be missing from the ORDER BY +** clause and the match can still be a success. +** +** All terms of the ORDER BY that match against the index must be either +** ASC or DESC. (Terms of the ORDER BY clause past the end of a UNIQUE +** index do not need to satisfy this constraint.) The *pbRev value is +** set to 1 if the ORDER BY clause is all DESC and it is set to 0 if +** the ORDER BY clause is all ASC. +*/ +static int isSortingIndex( + Parse *pParse, /* Parsing context */ + WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmaps */ + Index *pIdx, /* The index we are testing */ + int base, /* Cursor number for the table to be sorted */ + ExprList *pOrderBy, /* The ORDER BY clause */ + int nEqCol, /* Number of index columns with == constraints */ + int wsFlags, /* Index usages flags */ + int *pbRev /* Set to 1 if ORDER BY is DESC */ +){ + int i, j; /* Loop counters */ + int sortOrder = 0; /* XOR of index and ORDER BY sort direction */ + int nTerm; /* Number of ORDER BY terms */ + struct ExprList_item *pTerm; /* A term of the ORDER BY clause */ + sqlite3 *db = pParse->db; + + assert( pOrderBy!=0 ); + nTerm = pOrderBy->nExpr; + assert( nTerm>0 ); + + /* Argument pIdx must either point to a 'real' named index structure, + ** or an index structure allocated on the stack by bestBtreeIndex() to + ** represent the rowid index that is part of every table. */ + assert( pIdx->zName || (pIdx->nColumn==1 && pIdx->aiColumn[0]==-1) ); + + /* Match terms of the ORDER BY clause against columns of + ** the index. + ** + ** Note that indices have pIdx->nColumn regular columns plus + ** one additional column containing the rowid. The rowid column + ** of the index is also allowed to match against the ORDER BY + ** clause. + */ + for(i=j=0, pTerm=pOrderBy->a; jnColumn; i++){ + Expr *pExpr; /* The expression of the ORDER BY pTerm */ + CollSeq *pColl; /* The collating sequence of pExpr */ + int termSortOrder; /* Sort order for this term */ + int iColumn; /* The i-th column of the index. -1 for rowid */ + int iSortOrder; /* 1 for DESC, 0 for ASC on the i-th index term */ + const char *zColl; /* Name of the collating sequence for i-th index term */ + + pExpr = pTerm->pExpr; + if( pExpr->op!=TK_COLUMN || pExpr->iTable!=base ){ + /* Can not use an index sort on anything that is not a column in the + ** left-most table of the FROM clause */ + break; + } + pColl = sqlite3ExprCollSeq(pParse, pExpr); + if( !pColl ){ + pColl = db->pDfltColl; + } + if( pIdx->zName && inColumn ){ + iColumn = pIdx->aiColumn[i]; + if( iColumn==pIdx->pTable->iPKey ){ + iColumn = -1; + } + iSortOrder = pIdx->aSortOrder[i]; + zColl = pIdx->azColl[i]; + }else{ + iColumn = -1; + iSortOrder = 0; + zColl = pColl->zName; + } + if( pExpr->iColumn!=iColumn || sqlite3StrICmp(pColl->zName, zColl) ){ + /* Term j of the ORDER BY clause does not match column i of the index */ + if( inColumn ){ + /* Index column i is the rowid. All other terms match. */ + break; + }else{ + /* If an index column fails to match and is not constrained by == + ** then the index cannot satisfy the ORDER BY constraint. + */ + return 0; + } + } + assert( pIdx->aSortOrder!=0 || iColumn==-1 ); + assert( pTerm->sortOrder==0 || pTerm->sortOrder==1 ); + assert( iSortOrder==0 || iSortOrder==1 ); + termSortOrder = iSortOrder ^ pTerm->sortOrder; + if( i>nEqCol ){ + if( termSortOrder!=sortOrder ){ + /* Indices can only be used if all ORDER BY terms past the + ** equality constraints are all either DESC or ASC. */ + return 0; + } + }else{ + sortOrder = termSortOrder; + } + j++; + pTerm++; + if( iColumn<0 && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){ + /* If the indexed column is the primary key and everything matches + ** so far and none of the ORDER BY terms to the right reference other + ** tables in the join, then we are assured that the index can be used + ** to sort because the primary key is unique and so none of the other + ** columns will make any difference + */ + j = nTerm; + } + } + + *pbRev = sortOrder!=0; + if( j>=nTerm ){ + /* All terms of the ORDER BY clause are covered by this index so + ** this index can be used for sorting. */ + return 1; + } + if( pIdx->onError!=OE_None && i==pIdx->nColumn + && (wsFlags & WHERE_COLUMN_NULL)==0 + && !referencesOtherTables(pOrderBy, pMaskSet, j, base) ){ + /* All terms of this index match some prefix of the ORDER BY clause + ** and the index is UNIQUE and no terms on the tail of the ORDER BY + ** clause reference other tables in a join. If this is all true then + ** the order by clause is superfluous. Not that if the matching + ** condition is IS NULL then the result is not necessarily unique + ** even on a UNIQUE index, so disallow those cases. */ + return 1; + } + return 0; +} + +/* +** Prepare a crude estimate of the logarithm of the input value. +** The results need not be exact. This is only used for estimating +** the total cost of performing operations with O(logN) or O(NlogN) +** complexity. Because N is just a guess, it is no great tragedy if +** logN is a little off. +*/ +static double estLog(double N){ + double logN = 1; + double x = 10; + while( N>x ){ + logN += 1; + x *= 10; + } + return logN; +} + +/* +** Two routines for printing the content of an sqlite3_index_info +** structure. Used for testing and debugging only. If neither +** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines +** are no-ops. +*/ +#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_DEBUG) +static void TRACE_IDX_INPUTS(sqlite3_index_info *p){ + int i; + if( !sqlite3WhereTrace ) return; + for(i=0; inConstraint; i++){ + sqlite3DebugPrintf(" constraint[%d]: col=%d termid=%d op=%d usabled=%d\n", + i, + p->aConstraint[i].iColumn, + p->aConstraint[i].iTermOffset, + p->aConstraint[i].op, + p->aConstraint[i].usable); + } + for(i=0; inOrderBy; i++){ + sqlite3DebugPrintf(" orderby[%d]: col=%d desc=%d\n", + i, + p->aOrderBy[i].iColumn, + p->aOrderBy[i].desc); + } +} +static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){ + int i; + if( !sqlite3WhereTrace ) return; + for(i=0; inConstraint; i++){ + sqlite3DebugPrintf(" usage[%d]: argvIdx=%d omit=%d\n", + i, + p->aConstraintUsage[i].argvIndex, + p->aConstraintUsage[i].omit); + } + sqlite3DebugPrintf(" idxNum=%d\n", p->idxNum); + sqlite3DebugPrintf(" idxStr=%s\n", p->idxStr); + sqlite3DebugPrintf(" orderByConsumed=%d\n", p->orderByConsumed); + sqlite3DebugPrintf(" estimatedCost=%g\n", p->estimatedCost); +} +#else +#define TRACE_IDX_INPUTS(A) +#define TRACE_IDX_OUTPUTS(A) +#endif + +/* +** Required because bestIndex() is called by bestOrClauseIndex() +*/ +static void bestIndex( + Parse*, WhereClause*, struct SrcList_item*, + Bitmask, Bitmask, ExprList*, WhereCost*); + +/* +** This routine attempts to find an scanning strategy that can be used +** to optimize an 'OR' expression that is part of a WHERE clause. +** +** The table associated with FROM clause term pSrc may be either a +** regular B-Tree table or a virtual table. +*/ +static void bestOrClauseIndex( + Parse *pParse, /* The parsing context */ + WhereClause *pWC, /* The WHERE clause */ + struct SrcList_item *pSrc, /* The FROM clause term to search */ + Bitmask notReady, /* Mask of cursors not available for indexing */ + Bitmask notValid, /* Cursors not available for any purpose */ + ExprList *pOrderBy, /* The ORDER BY clause */ + WhereCost *pCost /* Lowest cost query plan */ +){ +#ifndef SQLITE_OMIT_OR_OPTIMIZATION + const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */ + const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */ + WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */ + WhereTerm *pTerm; /* A single term of the WHERE clause */ + + /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses + ** are used */ + if( pSrc->notIndexed || pSrc->pIndex!=0 ){ + return; + } + + /* Search the WHERE clause terms for a usable WO_OR term. */ + for(pTerm=pWC->a; pTermeOperator==WO_OR + && ((pTerm->prereqAll & ~maskSrc) & notReady)==0 + && (pTerm->u.pOrInfo->indexable & maskSrc)!=0 + ){ + WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc; + WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm]; + WhereTerm *pOrTerm; + int flags = WHERE_MULTI_OR; + double rTotal = 0; + double nRow = 0; + Bitmask used = 0; + + for(pOrTerm=pOrWC->a; pOrTerma), (pTerm - pWC->a) + )); + if( pOrTerm->eOperator==WO_AND ){ + WhereClause *pAndWC = &pOrTerm->u.pAndInfo->wc; + bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost); + }else if( pOrTerm->leftCursor==iCur ){ + WhereClause tempWC; + tempWC.pParse = pWC->pParse; + tempWC.pMaskSet = pWC->pMaskSet; + tempWC.op = TK_AND; + tempWC.a = pOrTerm; + tempWC.nTerm = 1; + bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost); + }else{ + continue; + } + rTotal += sTermCost.rCost; + nRow += sTermCost.plan.nRow; + used |= sTermCost.used; + if( rTotal>=pCost->rCost ) break; + } + + /* If there is an ORDER BY clause, increase the scan cost to account + ** for the cost of the sort. */ + if( pOrderBy!=0 ){ + WHERETRACE(("... sorting increases OR cost %.9g to %.9g\n", + rTotal, rTotal+nRow*estLog(nRow))); + rTotal += nRow*estLog(nRow); + } + + /* If the cost of scanning using this OR term for optimization is + ** less than the current cost stored in pCost, replace the contents + ** of pCost. */ + WHERETRACE(("... multi-index OR cost=%.9g nrow=%.9g\n", rTotal, nRow)); + if( rTotalrCost ){ + pCost->rCost = rTotal; + pCost->used = used; + pCost->plan.nRow = nRow; + pCost->plan.wsFlags = flags; + pCost->plan.u.pTerm = pTerm; + } + } + } +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */ +} + +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX +/* +** Return TRUE if the WHERE clause term pTerm is of a form where it +** could be used with an index to access pSrc, assuming an appropriate +** index existed. +*/ +static int termCanDriveIndex( + WhereTerm *pTerm, /* WHERE clause term to check */ + struct SrcList_item *pSrc, /* Table we are trying to access */ + Bitmask notReady /* Tables in outer loops of the join */ +){ + char aff; + if( pTerm->leftCursor!=pSrc->iCursor ) return 0; + if( pTerm->eOperator!=WO_EQ ) return 0; + if( (pTerm->prereqRight & notReady)!=0 ) return 0; + aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity; + if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0; + return 1; +} +#endif + +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX +/* +** If the query plan for pSrc specified in pCost is a full table scan +** and indexing is allows (if there is no NOT INDEXED clause) and it +** possible to construct a transient index that would perform better +** than a full table scan even when the cost of constructing the index +** is taken into account, then alter the query plan to use the +** transient index. +*/ +static void bestAutomaticIndex( + Parse *pParse, /* The parsing context */ + WhereClause *pWC, /* The WHERE clause */ + struct SrcList_item *pSrc, /* The FROM clause term to search */ + Bitmask notReady, /* Mask of cursors that are not available */ + WhereCost *pCost /* Lowest cost query plan */ +){ + double nTableRow; /* Rows in the input table */ + double logN; /* log(nTableRow) */ + double costTempIdx; /* per-query cost of the transient index */ + WhereTerm *pTerm; /* A single term of the WHERE clause */ + WhereTerm *pWCEnd; /* End of pWC->a[] */ + Table *pTable; /* Table tht might be indexed */ + + if( (pParse->db->flags & SQLITE_AutoIndex)==0 ){ + /* Automatic indices are disabled at run-time */ + return; + } + if( (pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)!=0 ){ + /* We already have some kind of index in use for this query. */ + return; + } + if( pSrc->notIndexed ){ + /* The NOT INDEXED clause appears in the SQL. */ + return; + } + + assert( pParse->nQueryLoop >= (double)1 ); + pTable = pSrc->pTab; + nTableRow = pTable->nRowEst; + logN = estLog(nTableRow); + costTempIdx = 2*logN*(nTableRow/pParse->nQueryLoop + 1); + if( costTempIdx>=pCost->rCost ){ + /* The cost of creating the transient table would be greater than + ** doing the full table scan */ + return; + } + + /* Search for any equality comparison term */ + pWCEnd = &pWC->a[pWC->nTerm]; + for(pTerm=pWC->a; pTermrCost, costTempIdx)); + pCost->rCost = costTempIdx; + pCost->plan.nRow = logN + 1; + pCost->plan.wsFlags = WHERE_TEMP_INDEX; + pCost->used = pTerm->prereqRight; + break; + } + } +} +#else +# define bestAutomaticIndex(A,B,C,D,E) /* no-op */ +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ + + +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX +/* +** Generate code to construct the Index object for an automatic index +** and to set up the WhereLevel object pLevel so that the code generator +** makes use of the automatic index. +*/ +static void constructAutomaticIndex( + Parse *pParse, /* The parsing context */ + WhereClause *pWC, /* The WHERE clause */ + struct SrcList_item *pSrc, /* The FROM clause term to get the next index */ + Bitmask notReady, /* Mask of cursors that are not available */ + WhereLevel *pLevel /* Write new index here */ +){ + int nColumn; /* Number of columns in the constructed index */ + WhereTerm *pTerm; /* A single term of the WHERE clause */ + WhereTerm *pWCEnd; /* End of pWC->a[] */ + int nByte; /* Byte of memory needed for pIdx */ + Index *pIdx; /* Object describing the transient index */ + Vdbe *v; /* Prepared statement under construction */ + int regIsInit; /* Register set by initialization */ + int addrInit; /* Address of the initialization bypass jump */ + Table *pTable; /* The table being indexed */ + KeyInfo *pKeyinfo; /* Key information for the index */ + int addrTop; /* Top of the index fill loop */ + int regRecord; /* Register holding an index record */ + int n; /* Column counter */ + int i; /* Loop counter */ + int mxBitCol; /* Maximum column in pSrc->colUsed */ + CollSeq *pColl; /* Collating sequence to on a column */ + Bitmask idxCols; /* Bitmap of columns used for indexing */ + Bitmask extraCols; /* Bitmap of additional columns */ + + /* Generate code to skip over the creation and initialization of the + ** transient index on 2nd and subsequent iterations of the loop. */ + v = pParse->pVdbe; + assert( v!=0 ); + regIsInit = ++pParse->nMem; + addrInit = sqlite3VdbeAddOp1(v, OP_If, regIsInit); + sqlite3VdbeAddOp2(v, OP_Integer, 1, regIsInit); + + /* Count the number of columns that will be added to the index + ** and used to match WHERE clause constraints */ + nColumn = 0; + pTable = pSrc->pTab; + pWCEnd = &pWC->a[pWC->nTerm]; + idxCols = 0; + for(pTerm=pWC->a; pTermu.leftColumn; + Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<0 ); + pLevel->plan.nEq = nColumn; + + /* Count the number of additional columns needed to create a + ** covering index. A "covering index" is an index that contains all + ** columns that are needed by the query. With a covering index, the + ** original table never needs to be accessed. Automatic indices must + ** be a covering index because the index will not be updated if the + ** original table changes and the index and table cannot both be used + ** if they go out of sync. + */ + extraCols = pSrc->colUsed & (~idxCols | (((Bitmask)1)<<(BMS-1))); + mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol; + testcase( pTable->nCol==BMS-1 ); + testcase( pTable->nCol==BMS-2 ); + for(i=0; icolUsed & (((Bitmask)1)<<(BMS-1)) ){ + nColumn += pTable->nCol - BMS + 1; + } + pLevel->plan.wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WO_EQ; + + /* Construct the Index object to describe this index */ + nByte = sizeof(Index); + nByte += nColumn*sizeof(int); /* Index.aiColumn */ + nByte += nColumn*sizeof(char*); /* Index.azColl */ + nByte += nColumn; /* Index.aSortOrder */ + pIdx = sqlite3DbMallocZero(pParse->db, nByte); + if( pIdx==0 ) return; + pLevel->plan.u.pIdx = pIdx; + pIdx->azColl = (char**)&pIdx[1]; + pIdx->aiColumn = (int*)&pIdx->azColl[nColumn]; + pIdx->aSortOrder = (u8*)&pIdx->aiColumn[nColumn]; + pIdx->zName = "auto-index"; + pIdx->nColumn = nColumn; + pIdx->pTable = pTable; + n = 0; + idxCols = 0; + for(pTerm=pWC->a; pTermu.leftColumn; + Bitmask cMask = iCol>=BMS ? ((Bitmask)1)<<(BMS-1) : ((Bitmask)1)<pExpr; + idxCols |= cMask; + pIdx->aiColumn[n] = pTerm->u.leftColumn; + pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight); + pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY"; + n++; + } + } + } + assert( (u32)n==pLevel->plan.nEq ); + + /* Add additional columns needed to make the automatic index into + ** a covering index */ + for(i=0; iaiColumn[n] = i; + pIdx->azColl[n] = "BINARY"; + n++; + } + } + if( pSrc->colUsed & (((Bitmask)1)<<(BMS-1)) ){ + for(i=BMS-1; inCol; i++){ + pIdx->aiColumn[n] = i; + pIdx->azColl[n] = "BINARY"; + n++; + } + } + assert( n==nColumn ); + + /* Create the automatic index */ + pKeyinfo = sqlite3IndexKeyinfo(pParse, pIdx); + assert( pLevel->iIdxCur>=0 ); + sqlite3VdbeAddOp4(v, OP_OpenAutoindex, pLevel->iIdxCur, nColumn+1, 0, + (char*)pKeyinfo, P4_KEYINFO_HANDOFF); + VdbeComment((v, "for %s", pTable->zName)); + + /* Fill the automatic index with content */ + addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur); + regRecord = sqlite3GetTempReg(pParse); + sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 1); + sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord); + sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT); + sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1); + sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX); + sqlite3VdbeJumpHere(v, addrTop); + sqlite3ReleaseTempReg(pParse, regRecord); + + /* Jump here when skipping the initialization */ + sqlite3VdbeJumpHere(v, addrInit); +} +#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */ + +#ifndef SQLITE_OMIT_VIRTUALTABLE +/* +** Allocate and populate an sqlite3_index_info structure. It is the +** responsibility of the caller to eventually release the structure +** by passing the pointer returned by this function to sqlite3_free(). +*/ +static sqlite3_index_info *allocateIndexInfo( + Parse *pParse, + WhereClause *pWC, + struct SrcList_item *pSrc, + ExprList *pOrderBy +){ + int i, j; + int nTerm; + struct sqlite3_index_constraint *pIdxCons; + struct sqlite3_index_orderby *pIdxOrderBy; + struct sqlite3_index_constraint_usage *pUsage; + WhereTerm *pTerm; + int nOrderBy; + sqlite3_index_info *pIdxInfo; + + WHERETRACE(("Recomputing index info for %s...\n", pSrc->pTab->zName)); + + /* Count the number of possible WHERE clause constraints referring + ** to this virtual table */ + for(i=nTerm=0, pTerm=pWC->a; inTerm; i++, pTerm++){ + if( pTerm->leftCursor != pSrc->iCursor ) continue; + assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 ); + testcase( pTerm->eOperator==WO_IN ); + testcase( pTerm->eOperator==WO_ISNULL ); + if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue; + nTerm++; + } + + /* If the ORDER BY clause contains only columns in the current + ** virtual table then allocate space for the aOrderBy part of + ** the sqlite3_index_info structure. + */ + nOrderBy = 0; + if( pOrderBy ){ + for(i=0; inExpr; i++){ + Expr *pExpr = pOrderBy->a[i].pExpr; + if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break; + } + if( i==pOrderBy->nExpr ){ + nOrderBy = pOrderBy->nExpr; + } + } + + /* Allocate the sqlite3_index_info structure + */ + pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo) + + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm + + sizeof(*pIdxOrderBy)*nOrderBy ); + if( pIdxInfo==0 ){ + sqlite3ErrorMsg(pParse, "out of memory"); + /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */ + return 0; + } + + /* Initialize the structure. The sqlite3_index_info structure contains + ** many fields that are declared "const" to prevent xBestIndex from + ** changing them. We have to do some funky casting in order to + ** initialize those fields. + */ + pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1]; + pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm]; + pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy]; + *(int*)&pIdxInfo->nConstraint = nTerm; + *(int*)&pIdxInfo->nOrderBy = nOrderBy; + *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons; + *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy; + *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage = + pUsage; + + for(i=j=0, pTerm=pWC->a; inTerm; i++, pTerm++){ + if( pTerm->leftCursor != pSrc->iCursor ) continue; + assert( (pTerm->eOperator&(pTerm->eOperator-1))==0 ); + testcase( pTerm->eOperator==WO_IN ); + testcase( pTerm->eOperator==WO_ISNULL ); + if( pTerm->eOperator & (WO_IN|WO_ISNULL) ) continue; + pIdxCons[j].iColumn = pTerm->u.leftColumn; + pIdxCons[j].iTermOffset = i; + pIdxCons[j].op = (u8)pTerm->eOperator; + /* The direct assignment in the previous line is possible only because + ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical. The + ** following asserts verify this fact. */ + assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ ); + assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT ); + assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE ); + assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT ); + assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE ); + assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH ); + assert( pTerm->eOperator & (WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) ); + j++; + } + for(i=0; ia[i].pExpr; + pIdxOrderBy[i].iColumn = pExpr->iColumn; + pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder; + } + + return pIdxInfo; +} + +/* +** The table object reference passed as the second argument to this function +** must represent a virtual table. This function invokes the xBestIndex() +** method of the virtual table with the sqlite3_index_info pointer passed +** as the argument. +** +** If an error occurs, pParse is populated with an error message and a +** non-zero value is returned. Otherwise, 0 is returned and the output +** part of the sqlite3_index_info structure is left populated. +** +** Whether or not an error is returned, it is the responsibility of the +** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates +** that this is required. +*/ +static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){ + sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab; + int i; + int rc; + + WHERETRACE(("xBestIndex for %s\n", pTab->zName)); + TRACE_IDX_INPUTS(p); + rc = pVtab->pModule->xBestIndex(pVtab, p); + TRACE_IDX_OUTPUTS(p); + + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_NOMEM ){ + pParse->db->mallocFailed = 1; + }else if( !pVtab->zErrMsg ){ + sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc)); + }else{ + sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg); + } + } + sqlite3_free(pVtab->zErrMsg); + pVtab->zErrMsg = 0; + + for(i=0; inConstraint; i++){ + if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){ + sqlite3ErrorMsg(pParse, + "table %s: xBestIndex returned an invalid plan", pTab->zName); + } + } + + return pParse->nErr; +} + + +/* +** Compute the best index for a virtual table. +** +** The best index is computed by the xBestIndex method of the virtual +** table module. This routine is really just a wrapper that sets up +** the sqlite3_index_info structure that is used to communicate with +** xBestIndex. +** +** In a join, this routine might be called multiple times for the +** same virtual table. The sqlite3_index_info structure is created +** and initialized on the first invocation and reused on all subsequent +** invocations. The sqlite3_index_info structure is also used when +** code is generated to access the virtual table. The whereInfoDelete() +** routine takes care of freeing the sqlite3_index_info structure after +** everybody has finished with it. +*/ +static void bestVirtualIndex( + Parse *pParse, /* The parsing context */ + WhereClause *pWC, /* The WHERE clause */ + struct SrcList_item *pSrc, /* The FROM clause term to search */ + Bitmask notReady, /* Mask of cursors not available for index */ + Bitmask notValid, /* Cursors not valid for any purpose */ + ExprList *pOrderBy, /* The order by clause */ + WhereCost *pCost, /* Lowest cost query plan */ + sqlite3_index_info **ppIdxInfo /* Index information passed to xBestIndex */ +){ + Table *pTab = pSrc->pTab; + sqlite3_index_info *pIdxInfo; + struct sqlite3_index_constraint *pIdxCons; + struct sqlite3_index_constraint_usage *pUsage; + WhereTerm *pTerm; + int i, j; + int nOrderBy; + double rCost; + + /* Make sure wsFlags is initialized to some sane value. Otherwise, if the + ** malloc in allocateIndexInfo() fails and this function returns leaving + ** wsFlags in an uninitialized state, the caller may behave unpredictably. + */ + memset(pCost, 0, sizeof(*pCost)); + pCost->plan.wsFlags = WHERE_VIRTUALTABLE; + + /* If the sqlite3_index_info structure has not been previously + ** allocated and initialized, then allocate and initialize it now. + */ + pIdxInfo = *ppIdxInfo; + if( pIdxInfo==0 ){ + *ppIdxInfo = pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pOrderBy); + } + if( pIdxInfo==0 ){ + return; + } + + /* At this point, the sqlite3_index_info structure that pIdxInfo points + ** to will have been initialized, either during the current invocation or + ** during some prior invocation. Now we just have to customize the + ** details of pIdxInfo for the current invocation and pass it to + ** xBestIndex. + */ + + /* The module name must be defined. Also, by this point there must + ** be a pointer to an sqlite3_vtab structure. Otherwise + ** sqlite3ViewGetColumnNames() would have picked up the error. + */ + assert( pTab->azModuleArg && pTab->azModuleArg[0] ); + assert( sqlite3GetVTable(pParse->db, pTab) ); + + /* Set the aConstraint[].usable fields and initialize all + ** output variables to zero. + ** + ** aConstraint[].usable is true for constraints where the right-hand + ** side contains only references to tables to the left of the current + ** table. In other words, if the constraint is of the form: + ** + ** column = expr + ** + ** and we are evaluating a join, then the constraint on column is + ** only valid if all tables referenced in expr occur to the left + ** of the table containing column. + ** + ** The aConstraints[] array contains entries for all constraints + ** on the current table. That way we only have to compute it once + ** even though we might try to pick the best index multiple times. + ** For each attempt at picking an index, the order of tables in the + ** join might be different so we have to recompute the usable flag + ** each time. + */ + pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; + pUsage = pIdxInfo->aConstraintUsage; + for(i=0; inConstraint; i++, pIdxCons++){ + j = pIdxCons->iTermOffset; + pTerm = &pWC->a[j]; + pIdxCons->usable = (pTerm->prereqRight¬Ready) ? 0 : 1; + } + memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint); + if( pIdxInfo->needToFreeIdxStr ){ + sqlite3_free(pIdxInfo->idxStr); + } + pIdxInfo->idxStr = 0; + pIdxInfo->idxNum = 0; + pIdxInfo->needToFreeIdxStr = 0; + pIdxInfo->orderByConsumed = 0; + /* ((double)2) In case of SQLITE_OMIT_FLOATING_POINT... */ + pIdxInfo->estimatedCost = SQLITE_BIG_DBL / ((double)2); + nOrderBy = pIdxInfo->nOrderBy; + if( !pOrderBy ){ + pIdxInfo->nOrderBy = 0; + } + + if( vtabBestIndex(pParse, pTab, pIdxInfo) ){ + return; + } + + pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint; + for(i=0; inConstraint; i++){ + if( pUsage[i].argvIndex>0 ){ + pCost->used |= pWC->a[pIdxCons[i].iTermOffset].prereqRight; + } + } + + /* If there is an ORDER BY clause, and the selected virtual table index + ** does not satisfy it, increase the cost of the scan accordingly. This + ** matches the processing for non-virtual tables in bestBtreeIndex(). + */ + rCost = pIdxInfo->estimatedCost; + if( pOrderBy && pIdxInfo->orderByConsumed==0 ){ + rCost += estLog(rCost)*rCost; + } + + /* The cost is not allowed to be larger than SQLITE_BIG_DBL (the + ** inital value of lowestCost in this loop. If it is, then the + ** (costrCost = (SQLITE_BIG_DBL/((double)2)); + }else{ + pCost->rCost = rCost; + } + pCost->plan.u.pVtabIdx = pIdxInfo; + if( pIdxInfo->orderByConsumed ){ + pCost->plan.wsFlags |= WHERE_ORDERBY; + } + pCost->plan.nEq = 0; + pIdxInfo->nOrderBy = nOrderBy; + + /* Try to find a more efficient access pattern by using multiple indexes + ** to optimize an OR expression within the WHERE clause. + */ + bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost); +} +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + +/* +** Argument pIdx is a pointer to an index structure that has an array of +** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column +** stored in Index.aSample. These samples divide the domain of values stored +** the index into (SQLITE_INDEX_SAMPLES+1) regions. +** Region 0 contains all values less than the first sample value. Region +** 1 contains values between the first and second samples. Region 2 contains +** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES +** contains values larger than the last sample. +** +** If the index contains many duplicates of a single value, then it is +** possible that two or more adjacent samples can hold the same value. +** When that is the case, the smallest possible region code is returned +** when roundUp is false and the largest possible region code is returned +** when roundUp is true. +** +** If successful, this function determines which of the regions value +** pVal lies in, sets *piRegion to the region index (a value between 0 +** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK. +** Or, if an OOM occurs while converting text values between encodings, +** SQLITE_NOMEM is returned and *piRegion is undefined. +*/ +#ifdef SQLITE_ENABLE_STAT2 +static int whereRangeRegion( + Parse *pParse, /* Database connection */ + Index *pIdx, /* Index to consider domain of */ + sqlite3_value *pVal, /* Value to consider */ + int roundUp, /* Return largest valid region if true */ + int *piRegion /* OUT: Region of domain in which value lies */ +){ + assert( roundUp==0 || roundUp==1 ); + if( ALWAYS(pVal) ){ + IndexSample *aSample = pIdx->aSample; + int i = 0; + int eType = sqlite3_value_type(pVal); + + if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){ + double r = sqlite3_value_double(pVal); + for(i=0; i=SQLITE_TEXT ) break; + if( roundUp ){ + if( aSample[i].u.r>r ) break; + }else{ + if( aSample[i].u.r>=r ) break; + } + } + }else if( eType==SQLITE_NULL ){ + i = 0; + if( roundUp ){ + while( idb; + CollSeq *pColl; + const u8 *z; + int n; + + /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */ + assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); + + if( eType==SQLITE_BLOB ){ + z = (const u8 *)sqlite3_value_blob(pVal); + pColl = db->pDfltColl; + assert( pColl->enc==SQLITE_UTF8 ); + }else{ + pColl = sqlite3GetCollSeq(db, SQLITE_UTF8, 0, *pIdx->azColl); + if( pColl==0 ){ + sqlite3ErrorMsg(pParse, "no such collation sequence: %s", + *pIdx->azColl); + return SQLITE_ERROR; + } + z = (const u8 *)sqlite3ValueText(pVal, pColl->enc); + if( !z ){ + return SQLITE_NOMEM; + } + assert( z && pColl && pColl->xCmp ); + } + n = sqlite3ValueBytes(pVal, pColl->enc); + + for(i=0; ienc!=SQLITE_UTF8 ){ + int nSample; + char *zSample = sqlite3Utf8to16( + db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample + ); + if( !zSample ){ + assert( db->mallocFailed ); + return SQLITE_NOMEM; + } + c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z); + sqlite3DbFree(db, zSample); + }else +#endif + { + c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z); + } + if( c-roundUp>=0 ) break; + } + } + + assert( i>=0 && i<=SQLITE_INDEX_SAMPLES ); + *piRegion = i; + } + return SQLITE_OK; +} +#endif /* #ifdef SQLITE_ENABLE_STAT2 */ + +/* +** If expression pExpr represents a literal value, set *pp to point to +** an sqlite3_value structure containing the same value, with affinity +** aff applied to it, before returning. It is the responsibility of the +** caller to eventually release this structure by passing it to +** sqlite3ValueFree(). +** +** If the current parse is a recompile (sqlite3Reprepare()) and pExpr +** is an SQL variable that currently has a non-NULL value bound to it, +** create an sqlite3_value structure containing this value, again with +** affinity aff applied to it, instead. +** +** If neither of the above apply, set *pp to NULL. +** +** If an error occurs, return an error code. Otherwise, SQLITE_OK. +*/ +#ifdef SQLITE_ENABLE_STAT2 +static int valueFromExpr( + Parse *pParse, + Expr *pExpr, + u8 aff, + sqlite3_value **pp +){ + if( pExpr->op==TK_VARIABLE + || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE) + ){ + int iVar = pExpr->iColumn; + sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */ + *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff); + return SQLITE_OK; + } + return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp); +} +#endif + +/* +** This function is used to estimate the number of rows that will be visited +** by scanning an index for a range of values. The range may have an upper +** bound, a lower bound, or both. The WHERE clause terms that set the upper +** and lower bounds are represented by pLower and pUpper respectively. For +** example, assuming that index p is on t1(a): +** +** ... FROM t1 WHERE a > ? AND a < ? ... +** |_____| |_____| +** | | +** pLower pUpper +** +** If either of the upper or lower bound is not present, then NULL is passed in +** place of the corresponding WhereTerm. +** +** The nEq parameter is passed the index of the index column subject to the +** range constraint. Or, equivalently, the number of equality constraints +** optimized by the proposed index scan. For example, assuming index p is +** on t1(a, b), and the SQL query is: +** +** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ... +** +** then nEq should be passed the value 1 (as the range restricted column, +** b, is the second left-most column of the index). Or, if the query is: +** +** ... FROM t1 WHERE a > ? AND a < ? ... +** +** then nEq should be passed 0. +** +** The returned value is an integer between 1 and 100, inclusive. A return +** value of 1 indicates that the proposed range scan is expected to visit +** approximately 1/100th (1%) of the rows selected by the nEq equality +** constraints (if any). A return value of 100 indicates that it is expected +** that the range scan will visit every row (100%) selected by the equality +** constraints. +** +** In the absence of sqlite_stat2 ANALYZE data, each range inequality +** reduces the search space by 3/4ths. Hence a single constraint (x>?) +** results in a return of 25 and a range constraint (x>? AND xaCol[] of the range-compared column */ + WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */ + WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */ + int *piEst /* OUT: Return value */ +){ + int rc = SQLITE_OK; + +#ifdef SQLITE_ENABLE_STAT2 + + if( nEq==0 && p->aSample ){ + sqlite3_value *pLowerVal = 0; + sqlite3_value *pUpperVal = 0; + int iEst; + int iLower = 0; + int iUpper = SQLITE_INDEX_SAMPLES; + int roundUpUpper = 0; + int roundUpLower = 0; + u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity; + + if( pLower ){ + Expr *pExpr = pLower->pExpr->pRight; + rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal); + assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE ); + roundUpLower = (pLower->eOperator==WO_GT) ?1:0; + } + if( rc==SQLITE_OK && pUpper ){ + Expr *pExpr = pUpper->pExpr->pRight; + rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal); + assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE ); + roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0; + } + + if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){ + sqlite3ValueFree(pLowerVal); + sqlite3ValueFree(pUpperVal); + goto range_est_fallback; + }else if( pLowerVal==0 ){ + rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper); + if( pLower ) iLower = iUpper/2; + }else if( pUpperVal==0 ){ + rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower); + if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2; + }else{ + rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper); + if( rc==SQLITE_OK ){ + rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower); + } + } + WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper)); + + iEst = iUpper - iLower; + testcase( iEst==SQLITE_INDEX_SAMPLES ); + assert( iEst<=SQLITE_INDEX_SAMPLES ); + if( iEst<1 ){ + *piEst = 50/SQLITE_INDEX_SAMPLES; + }else{ + *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES; + } + sqlite3ValueFree(pLowerVal); + sqlite3ValueFree(pUpperVal); + return rc; + } +range_est_fallback: +#else + UNUSED_PARAMETER(pParse); + UNUSED_PARAMETER(p); + UNUSED_PARAMETER(nEq); +#endif + assert( pLower || pUpper ); + *piEst = 100; + if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4; + if( pUpper ) *piEst /= 4; + return rc; +} + +#ifdef SQLITE_ENABLE_STAT2 +/* +** Estimate the number of rows that will be returned based on +** an equality constraint x=VALUE and where that VALUE occurs in +** the histogram data. This only works when x is the left-most +** column of an index and sqlite_stat2 histogram data is available +** for that index. When pExpr==NULL that means the constraint is +** "x IS NULL" instead of "x=VALUE". +** +** Write the estimated row count into *pnRow and return SQLITE_OK. +** If unable to make an estimate, leave *pnRow unchanged and return +** non-zero. +** +** This routine can fail if it is unable to load a collating sequence +** required for string comparison, or if unable to allocate memory +** for a UTF conversion required for comparison. The error is stored +** in the pParse structure. +*/ +int whereEqualScanEst( + Parse *pParse, /* Parsing & code generating context */ + Index *p, /* The index whose left-most column is pTerm */ + Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */ + double *pnRow /* Write the revised row estimate here */ +){ + sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */ + int iLower, iUpper; /* Range of histogram regions containing pRhs */ + u8 aff; /* Column affinity */ + int rc; /* Subfunction return code */ + double nRowEst; /* New estimate of the number of rows */ + + assert( p->aSample!=0 ); + aff = p->pTable->aCol[p->aiColumn[0]].affinity; + if( pExpr ){ + rc = valueFromExpr(pParse, pExpr, aff, &pRhs); + if( rc ) goto whereEqualScanEst_cancel; + }else{ + pRhs = sqlite3ValueNew(pParse->db); + } + if( pRhs==0 ) return SQLITE_NOTFOUND; + rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower); + if( rc ) goto whereEqualScanEst_cancel; + rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper); + if( rc ) goto whereEqualScanEst_cancel; + WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper)); + if( iLower>=iUpper ){ + nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2); + if( nRowEst<*pnRow ) *pnRow = nRowEst; + }else{ + nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES; + *pnRow = nRowEst; + } + +whereEqualScanEst_cancel: + sqlite3ValueFree(pRhs); + return rc; +} +#endif /* defined(SQLITE_ENABLE_STAT2) */ + +#ifdef SQLITE_ENABLE_STAT2 +/* +** Estimate the number of rows that will be returned based on +** an IN constraint where the right-hand side of the IN operator +** is a list of values. Example: +** +** WHERE x IN (1,2,3,4) +** +** Write the estimated row count into *pnRow and return SQLITE_OK. +** If unable to make an estimate, leave *pnRow unchanged and return +** non-zero. +** +** This routine can fail if it is unable to load a collating sequence +** required for string comparison, or if unable to allocate memory +** for a UTF conversion required for comparison. The error is stored +** in the pParse structure. +*/ +int whereInScanEst( + Parse *pParse, /* Parsing & code generating context */ + Index *p, /* The index whose left-most column is pTerm */ + ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */ + double *pnRow /* Write the revised row estimate here */ +){ + sqlite3_value *pVal = 0; /* One value from list */ + int iLower, iUpper; /* Range of histogram regions containing pRhs */ + u8 aff; /* Column affinity */ + int rc = SQLITE_OK; /* Subfunction return code */ + double nRowEst; /* New estimate of the number of rows */ + int nSpan = 0; /* Number of histogram regions spanned */ + int nSingle = 0; /* Histogram regions hit by a single value */ + int nNotFound = 0; /* Count of values that are not constants */ + int i; /* Loop counter */ + u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */ + u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */ + + assert( p->aSample!=0 ); + aff = p->pTable->aCol[p->aiColumn[0]].affinity; + memset(aSpan, 0, sizeof(aSpan)); + memset(aSingle, 0, sizeof(aSingle)); + for(i=0; inExpr; i++){ + sqlite3ValueFree(pVal); + rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal); + if( rc ) break; + if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){ + nNotFound++; + continue; + } + rc = whereRangeRegion(pParse, p, pVal, 0, &iLower); + if( rc ) break; + rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper); + if( rc ) break; + if( iLower>=iUpper ){ + aSingle[iLower] = 1; + }else{ + assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES ); + while( iLoweraiRowEst[0]/(2*SQLITE_INDEX_SAMPLES) + + nNotFound*p->aiRowEst[1]; + if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0]; + *pnRow = nRowEst; + WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n", + nSpan, nSingle, nNotFound, nRowEst)); + } + sqlite3ValueFree(pVal); + return rc; +} +#endif /* defined(SQLITE_ENABLE_STAT2) */ + + +/* +** Find the best query plan for accessing a particular table. Write the +** best query plan and its cost into the WhereCost object supplied as the +** last parameter. +** +** The lowest cost plan wins. The cost is an estimate of the amount of +** CPU and disk I/O needed to process the requested result. +** Factors that influence cost include: +** +** * The estimated number of rows that will be retrieved. (The +** fewer the better.) +** +** * Whether or not sorting must occur. +** +** * Whether or not there must be separate lookups in the +** index and in the main table. +** +** If there was an INDEXED BY clause (pSrc->pIndex) attached to the table in +** the SQL statement, then this function only considers plans using the +** named index. If no such plan is found, then the returned cost is +** SQLITE_BIG_DBL. If a plan is found that uses the named index, +** then the cost is calculated in the usual way. +** +** If a NOT INDEXED clause (pSrc->notIndexed!=0) was attached to the table +** in the SELECT statement, then no indexes are considered. However, the +** selected plan may still take advantage of the built-in rowid primary key +** index. +*/ +static void bestBtreeIndex( + Parse *pParse, /* The parsing context */ + WhereClause *pWC, /* The WHERE clause */ + struct SrcList_item *pSrc, /* The FROM clause term to search */ + Bitmask notReady, /* Mask of cursors not available for indexing */ + Bitmask notValid, /* Cursors not available for any purpose */ + ExprList *pOrderBy, /* The ORDER BY clause */ + WhereCost *pCost /* Lowest cost query plan */ +){ + int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */ + Index *pProbe; /* An index we are evaluating */ + Index *pIdx; /* Copy of pProbe, or zero for IPK index */ + int eqTermMask; /* Current mask of valid equality operators */ + int idxEqTermMask; /* Index mask of valid equality operators */ + Index sPk; /* A fake index object for the primary key */ + unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */ + int aiColumnPk = -1; /* The aColumn[] value for the sPk index */ + int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */ + + /* Initialize the cost to a worst-case value */ + memset(pCost, 0, sizeof(*pCost)); + pCost->rCost = SQLITE_BIG_DBL; + + /* If the pSrc table is the right table of a LEFT JOIN then we may not + ** use an index to satisfy IS NULL constraints on that table. This is + ** because columns might end up being NULL if the table does not match - + ** a circumstance which the index cannot help us discover. Ticket #2177. + */ + if( pSrc->jointype & JT_LEFT ){ + idxEqTermMask = WO_EQ|WO_IN; + }else{ + idxEqTermMask = WO_EQ|WO_IN|WO_ISNULL; + } + + if( pSrc->pIndex ){ + /* An INDEXED BY clause specifies a particular index to use */ + pIdx = pProbe = pSrc->pIndex; + wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE); + eqTermMask = idxEqTermMask; + }else{ + /* There is no INDEXED BY clause. Create a fake Index object in local + ** variable sPk to represent the rowid primary key index. Make this + ** fake index the first in a chain of Index objects with all of the real + ** indices to follow */ + Index *pFirst; /* First of real indices on the table */ + memset(&sPk, 0, sizeof(Index)); + sPk.nColumn = 1; + sPk.aiColumn = &aiColumnPk; + sPk.aiRowEst = aiRowEstPk; + sPk.onError = OE_Replace; + sPk.pTable = pSrc->pTab; + aiRowEstPk[0] = pSrc->pTab->nRowEst; + aiRowEstPk[1] = 1; + pFirst = pSrc->pTab->pIndex; + if( pSrc->notIndexed==0 ){ + /* The real indices of the table are only considered if the + ** NOT INDEXED qualifier is omitted from the FROM clause */ + sPk.pNext = pFirst; + } + pProbe = &sPk; + wsFlagMask = ~( + WHERE_COLUMN_IN|WHERE_COLUMN_EQ|WHERE_COLUMN_NULL|WHERE_COLUMN_RANGE + ); + eqTermMask = WO_EQ|WO_IN; + pIdx = 0; + } + + /* Loop over all indices looking for the best one to use + */ + for(; pProbe; pIdx=pProbe=pProbe->pNext){ + const unsigned int * const aiRowEst = pProbe->aiRowEst; + double cost; /* Cost of using pProbe */ + double nRow; /* Estimated number of rows in result set */ + double log10N; /* base-10 logarithm of nRow (inexact) */ + int rev; /* True to scan in reverse order */ + int wsFlags = 0; + Bitmask used = 0; + + /* The following variables are populated based on the properties of + ** index being evaluated. They are then used to determine the expected + ** cost and number of rows returned. + ** + ** nEq: + ** Number of equality terms that can be implemented using the index. + ** In other words, the number of initial fields in the index that + ** are used in == or IN or NOT NULL constraints of the WHERE clause. + ** + ** nInMul: + ** The "in-multiplier". This is an estimate of how many seek operations + ** SQLite must perform on the index in question. For example, if the + ** WHERE clause is: + ** + ** WHERE a IN (1, 2, 3) AND b IN (4, 5, 6) + ** + ** SQLite must perform 9 lookups on an index on (a, b), so nInMul is + ** set to 9. Given the same schema and either of the following WHERE + ** clauses: + ** + ** WHERE a = 1 + ** WHERE a >= 2 + ** + ** nInMul is set to 1. + ** + ** If there exists a WHERE term of the form "x IN (SELECT ...)", then + ** the sub-select is assumed to return 25 rows for the purposes of + ** determining nInMul. + ** + ** bInEst: + ** Set to true if there was at least one "x IN (SELECT ...)" term used + ** in determining the value of nInMul. Note that the RHS of the + ** IN operator must be a SELECT, not a value list, for this variable + ** to be true. + ** + ** estBound: + ** An estimate on the amount of the table that must be searched. A + ** value of 100 means the entire table is searched. Range constraints + ** might reduce this to a value less than 100 to indicate that only + ** a fraction of the table needs searching. In the absence of + ** sqlite_stat2 ANALYZE data, a single inequality reduces the search + ** space to 1/4rd its original size. So an x>? constraint reduces + ** estBound to 25. Two constraints (x>? AND xnColumn; nEq++){ + int j = pProbe->aiColumn[nEq]; + pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx); + if( pTerm==0 ) break; + wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ); + if( pTerm->eOperator & WO_IN ){ + Expr *pExpr = pTerm->pExpr; + wsFlags |= WHERE_COLUMN_IN; + if( ExprHasProperty(pExpr, EP_xIsSelect) ){ + /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */ + nInMul *= 25; + bInEst = 1; + }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){ + /* "x IN (value, value, ...)" */ + nInMul *= pExpr->x.pList->nExpr; + } + }else if( pTerm->eOperator & WO_ISNULL ){ + wsFlags |= WHERE_COLUMN_NULL; + } +#ifdef SQLITE_ENABLE_STAT2 + if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm; +#endif + used |= pTerm->prereqRight; + } + + /* Determine the value of estBound. */ + if( nEqnColumn ){ + int j = pProbe->aiColumn[nEq]; + if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){ + WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx); + WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx); + whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound); + if( pTop ){ + nBound = 1; + wsFlags |= WHERE_TOP_LIMIT; + used |= pTop->prereqRight; + } + if( pBtm ){ + nBound++; + wsFlags |= WHERE_BTM_LIMIT; + used |= pBtm->prereqRight; + } + wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE); + } + }else if( pProbe->onError!=OE_None ){ + testcase( wsFlags & WHERE_COLUMN_IN ); + testcase( wsFlags & WHERE_COLUMN_NULL ); + if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){ + wsFlags |= WHERE_UNIQUE; + } + } + + /* If there is an ORDER BY clause and the index being considered will + ** naturally scan rows in the required order, set the appropriate flags + ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index + ** will scan rows in a different order, set the bSort variable. */ + if( pOrderBy ){ + if( (wsFlags & WHERE_COLUMN_IN)==0 + && isSortingIndex(pParse, pWC->pMaskSet, pProbe, iCur, pOrderBy, + nEq, wsFlags, &rev) + ){ + wsFlags |= WHERE_ROWID_RANGE|WHERE_COLUMN_RANGE|WHERE_ORDERBY; + wsFlags |= (rev ? WHERE_REVERSE : 0); + }else{ + bSort = 1; + } + } + + /* If currently calculating the cost of using an index (not the IPK + ** index), determine if all required column data may be obtained without + ** using the main table (i.e. if the index is a covering + ** index for this query). If it is, set the WHERE_IDX_ONLY flag in + ** wsFlags. Otherwise, set the bLookup variable to true. */ + if( pIdx && wsFlags ){ + Bitmask m = pSrc->colUsed; + int j; + for(j=0; jnColumn; j++){ + int x = pIdx->aiColumn[j]; + if( xaiRowEst[0] ){ + nRow = aiRowEst[0]/2; + nInMul = (int)(nRow / aiRowEst[nEq]); + } + +#ifdef SQLITE_ENABLE_STAT2 + /* If the constraint is of the form x=VALUE and histogram + ** data is available for column x, then it might be possible + ** to get a better estimate on the number of rows based on + ** VALUE and how common that value is according to the histogram. + */ + if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 ){ + if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){ + testcase( pFirstTerm->eOperator==WO_EQ ); + testcase( pFirstTerm->pOperator==WO_ISNULL ); + whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow); + }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){ + whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow); + } + } +#endif /* SQLITE_ENABLE_STAT2 */ + + /* Adjust the number of output rows and downward to reflect rows + ** that are excluded by range constraints. + */ + nRow = (nRow * (double)estBound) / (double)100; + if( nRow<1 ) nRow = 1; + + /* Experiments run on real SQLite databases show that the time needed + ** to do a binary search to locate a row in a table or index is roughly + ** log10(N) times the time to move from one row to the next row within + ** a table or index. The actual times can vary, with the size of + ** records being an important factor. Both moves and searches are + ** slower with larger records, presumably because fewer records fit + ** on one page and hence more pages have to be fetched. + ** + ** The ANALYZE command and the sqlite_stat1 and sqlite_stat2 tables do + ** not give us data on the relative sizes of table and index records. + ** So this computation assumes table records are about twice as big + ** as index records + */ + if( (wsFlags & WHERE_NOT_FULLSCAN)==0 ){ + /* The cost of a full table scan is a number of move operations equal + ** to the number of rows in the table. + ** + ** We add an additional 4x penalty to full table scans. This causes + ** the cost function to err on the side of choosing an index over + ** choosing a full scan. This 4x full-scan penalty is an arguable + ** decision and one which we expect to revisit in the future. But + ** it seems to be working well enough at the moment. + */ + cost = aiRowEst[0]*4; + }else{ + log10N = estLog(aiRowEst[0]); + cost = nRow; + if( pIdx ){ + if( bLookup ){ + /* For an index lookup followed by a table lookup: + ** nInMul index searches to find the start of each index range + ** + nRow steps through the index + ** + nRow table searches to lookup the table entry using the rowid + */ + cost += (nInMul + nRow)*log10N; + }else{ + /* For a covering index: + ** nInMul index searches to find the initial entry + ** + nRow steps through the index + */ + cost += nInMul*log10N; + } + }else{ + /* For a rowid primary key lookup: + ** nInMult table searches to find the initial entry for each range + ** + nRow steps through the table + */ + cost += nInMul*log10N; + } + } + + /* Add in the estimated cost of sorting the result. Actual experimental + ** measurements of sorting performance in SQLite show that sorting time + ** adds C*N*log10(N) to the cost, where N is the number of rows to be + ** sorted and C is a factor between 1.95 and 4.3. We will split the + ** difference and select C of 3.0. + */ + if( bSort ){ + cost += nRow*estLog(nRow)*3; + } + + /**** Cost of using this index has now been computed ****/ + + /* If there are additional constraints on this table that cannot + ** be used with the current index, but which might lower the number + ** of output rows, adjust the nRow value accordingly. This only + ** matters if the current index is the least costly, so do not bother + ** with this step if we already know this index will not be chosen. + ** Also, never reduce the output row count below 2 using this step. + ** + ** It is critical that the notValid mask be used here instead of + ** the notReady mask. When computing an "optimal" index, the notReady + ** mask will only have one bit set - the bit for the current table. + ** The notValid mask, on the other hand, always has all bits set for + ** tables that are not in outer loops. If notReady is used here instead + ** of notValid, then a optimal index that depends on inner joins loops + ** might be selected even when there exists an optimal index that has + ** no such dependency. + */ + if( nRow>2 && cost<=pCost->rCost ){ + int k; /* Loop counter */ + int nSkipEq = nEq; /* Number of == constraints to skip */ + int nSkipRange = nBound; /* Number of < constraints to skip */ + Bitmask thisTab; /* Bitmap for pSrc */ + + thisTab = getMask(pWC->pMaskSet, iCur); + for(pTerm=pWC->a, k=pWC->nTerm; nRow>2 && k; k--, pTerm++){ + if( pTerm->wtFlags & TERM_VIRTUAL ) continue; + if( (pTerm->prereqAll & notValid)!=thisTab ) continue; + if( pTerm->eOperator & (WO_EQ|WO_IN|WO_ISNULL) ){ + if( nSkipEq ){ + /* Ignore the first nEq equality matches since the index + ** has already accounted for these */ + nSkipEq--; + }else{ + /* Assume each additional equality match reduces the result + ** set size by a factor of 10 */ + nRow /= 10; + } + }else if( pTerm->eOperator & (WO_LT|WO_LE|WO_GT|WO_GE) ){ + if( nSkipRange ){ + /* Ignore the first nSkipRange range constraints since the index + ** has already accounted for these */ + nSkipRange--; + }else{ + /* Assume each additional range constraint reduces the result + ** set size by a factor of 3. Indexed range constraints reduce + ** the search space by a larger factor: 4. We make indexed range + ** more selective intentionally because of the subjective + ** observation that indexed range constraints really are more + ** selective in practice, on average. */ + nRow /= 3; + } + }else if( pTerm->eOperator!=WO_NOOP ){ + /* Any other expression lowers the output row count by half */ + nRow /= 2; + } + } + if( nRow<2 ) nRow = 2; + } + + + WHERETRACE(( + "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n" + " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n", + pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"), + nEq, nInMul, estBound, bSort, bLookup, wsFlags, + notReady, log10N, nRow, cost, used + )); + + /* If this index is the best we have seen so far, then record this + ** index and its cost in the pCost structure. + */ + if( (!pIdx || wsFlags) + && (costrCost || (cost<=pCost->rCost && nRowplan.nRow)) + ){ + pCost->rCost = cost; + pCost->used = used; + pCost->plan.nRow = nRow; + pCost->plan.wsFlags = (wsFlags&wsFlagMask); + pCost->plan.nEq = nEq; + pCost->plan.u.pIdx = pIdx; + } + + /* If there was an INDEXED BY clause, then only that one index is + ** considered. */ + if( pSrc->pIndex ) break; + + /* Reset masks for the next index in the loop */ + wsFlagMask = ~(WHERE_ROWID_EQ|WHERE_ROWID_RANGE); + eqTermMask = idxEqTermMask; + } + + /* If there is no ORDER BY clause and the SQLITE_ReverseOrder flag + ** is set, then reverse the order that the index will be scanned + ** in. This is used for application testing, to help find cases + ** where application behaviour depends on the (undefined) order that + ** SQLite outputs rows in in the absence of an ORDER BY clause. */ + if( !pOrderBy && pParse->db->flags & SQLITE_ReverseOrder ){ + pCost->plan.wsFlags |= WHERE_REVERSE; + } + + assert( pOrderBy || (pCost->plan.wsFlags&WHERE_ORDERBY)==0 ); + assert( pCost->plan.u.pIdx==0 || (pCost->plan.wsFlags&WHERE_ROWID_EQ)==0 ); + assert( pSrc->pIndex==0 + || pCost->plan.u.pIdx==0 + || pCost->plan.u.pIdx==pSrc->pIndex + ); + + WHERETRACE(("best index is: %s\n", + ((pCost->plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ? "none" : + pCost->plan.u.pIdx ? pCost->plan.u.pIdx->zName : "ipk") + )); + + bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost); + bestAutomaticIndex(pParse, pWC, pSrc, notReady, pCost); + pCost->plan.wsFlags |= eqTermMask; +} + +/* +** Find the query plan for accessing table pSrc->pTab. Write the +** best query plan and its cost into the WhereCost object supplied +** as the last parameter. This function may calculate the cost of +** both real and virtual table scans. +*/ +static void bestIndex( + Parse *pParse, /* The parsing context */ + WhereClause *pWC, /* The WHERE clause */ + struct SrcList_item *pSrc, /* The FROM clause term to search */ + Bitmask notReady, /* Mask of cursors not available for indexing */ + Bitmask notValid, /* Cursors not available for any purpose */ + ExprList *pOrderBy, /* The ORDER BY clause */ + WhereCost *pCost /* Lowest cost query plan */ +){ +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pSrc->pTab) ){ + sqlite3_index_info *p = 0; + bestVirtualIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost,&p); + if( p->needToFreeIdxStr ){ + sqlite3_free(p->idxStr); + } + sqlite3DbFree(pParse->db, p); + }else +#endif + { + bestBtreeIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost); + } +} + +/* +** Disable a term in the WHERE clause. Except, do not disable the term +** if it controls a LEFT OUTER JOIN and it did not originate in the ON +** or USING clause of that join. +** +** Consider the term t2.z='ok' in the following queries: +** +** (1) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok' +** (2) SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok' +** (3) SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok' +** +** The t2.z='ok' is disabled in the in (2) because it originates +** in the ON clause. The term is disabled in (3) because it is not part +** of a LEFT OUTER JOIN. In (1), the term is not disabled. +** +** IMPLEMENTATION-OF: R-24597-58655 No tests are done for terms that are +** completely satisfied by indices. +** +** Disabling a term causes that term to not be tested in the inner loop +** of the join. Disabling is an optimization. When terms are satisfied +** by indices, we disable them to prevent redundant tests in the inner +** loop. We would get the correct results if nothing were ever disabled, +** but joins might run a little slower. The trick is to disable as much +** as we can without disabling too much. If we disabled in (1), we'd get +** the wrong answer. See ticket #813. +*/ +static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){ + if( pTerm + && (pTerm->wtFlags & TERM_CODED)==0 + && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin)) + ){ + pTerm->wtFlags |= TERM_CODED; + if( pTerm->iParent>=0 ){ + WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent]; + if( (--pOther->nChild)==0 ){ + disableTerm(pLevel, pOther); + } + } + } +} + +/* +** Code an OP_Affinity opcode to apply the column affinity string zAff +** to the n registers starting at base. +** +** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the +** beginning and end of zAff are ignored. If all entries in zAff are +** SQLITE_AFF_NONE, then no code gets generated. +** +** This routine makes its own copy of zAff so that the caller is free +** to modify zAff after this routine returns. +*/ +static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){ + Vdbe *v = pParse->pVdbe; + if( zAff==0 ){ + assert( pParse->db->mallocFailed ); + return; + } + assert( v!=0 ); + + /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning + ** and end of the affinity string. + */ + while( n>0 && zAff[0]==SQLITE_AFF_NONE ){ + n--; + base++; + zAff++; + } + while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){ + n--; + } + + /* Code the OP_Affinity opcode if there is anything left to do. */ + if( n>0 ){ + sqlite3VdbeAddOp2(v, OP_Affinity, base, n); + sqlite3VdbeChangeP4(v, -1, zAff, n); + sqlite3ExprCacheAffinityChange(pParse, base, n); + } +} + + +/* +** Generate code for a single equality term of the WHERE clause. An equality +** term can be either X=expr or X IN (...). pTerm is the term to be +** coded. +** +** The current value for the constraint is left in register iReg. +** +** For a constraint of the form X=expr, the expression is evaluated and its +** result is left on the stack. For constraints of the form X IN (...) +** this routine sets up a loop that will iterate over all values of X. +*/ +static int codeEqualityTerm( + Parse *pParse, /* The parsing context */ + WhereTerm *pTerm, /* The term of the WHERE clause to be coded */ + WhereLevel *pLevel, /* When level of the FROM clause we are working on */ + int iTarget /* Attempt to leave results in this register */ +){ + Expr *pX = pTerm->pExpr; + Vdbe *v = pParse->pVdbe; + int iReg; /* Register holding results */ + + assert( iTarget>0 ); + if( pX->op==TK_EQ ){ + iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget); + }else if( pX->op==TK_ISNULL ){ + iReg = iTarget; + sqlite3VdbeAddOp2(v, OP_Null, 0, iReg); +#ifndef SQLITE_OMIT_SUBQUERY + }else{ + int eType; + int iTab; + struct InLoop *pIn; + + assert( pX->op==TK_IN ); + iReg = iTarget; + eType = sqlite3FindInIndex(pParse, pX, 0); + iTab = pX->iTable; + sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); + assert( pLevel->plan.wsFlags & WHERE_IN_ABLE ); + if( pLevel->u.in.nIn==0 ){ + pLevel->addrNxt = sqlite3VdbeMakeLabel(v); + } + pLevel->u.in.nIn++; + pLevel->u.in.aInLoop = + sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop, + sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn); + pIn = pLevel->u.in.aInLoop; + if( pIn ){ + pIn += pLevel->u.in.nIn - 1; + pIn->iCur = iTab; + if( eType==IN_INDEX_ROWID ){ + pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg); + }else{ + pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg); + } + sqlite3VdbeAddOp1(v, OP_IsNull, iReg); + }else{ + pLevel->u.in.nIn = 0; + } +#endif + } + disableTerm(pLevel, pTerm); + return iReg; +} + +/* +** Generate code that will evaluate all == and IN constraints for an +** index. +** +** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c). +** Suppose the WHERE clause is this: a==5 AND b IN (1,2,3) AND c>5 AND c<10 +** The index has as many as three equality constraints, but in this +** example, the third "c" value is an inequality. So only two +** constraints are coded. This routine will generate code to evaluate +** a==5 and b IN (1,2,3). The current values for a and b will be stored +** in consecutive registers and the index of the first register is returned. +** +** In the example above nEq==2. But this subroutine works for any value +** of nEq including 0. If nEq==0, this routine is nearly a no-op. +** The only thing it does is allocate the pLevel->iMem memory cell and +** compute the affinity string. +** +** This routine always allocates at least one memory cell and returns +** the index of that memory cell. The code that +** calls this routine will use that memory cell to store the termination +** key value of the loop. If one or more IN operators appear, then +** this routine allocates an additional nEq memory cells for internal +** use. +** +** Before returning, *pzAff is set to point to a buffer containing a +** copy of the column affinity string of the index allocated using +** sqlite3DbMalloc(). Except, entries in the copy of the string associated +** with equality constraints that use NONE affinity are set to +** SQLITE_AFF_NONE. This is to deal with SQL such as the following: +** +** CREATE TABLE t1(a TEXT PRIMARY KEY, b); +** SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b; +** +** In the example above, the index on t1(a) has TEXT affinity. But since +** the right hand side of the equality constraint (t2.b) has NONE affinity, +** no conversion should be attempted before using a t2.b value as part of +** a key to search the index. Hence the first byte in the returned affinity +** string in this example would be set to SQLITE_AFF_NONE. +*/ +static int codeAllEqualityTerms( + Parse *pParse, /* Parsing context */ + WhereLevel *pLevel, /* Which nested loop of the FROM we are coding */ + WhereClause *pWC, /* The WHERE clause */ + Bitmask notReady, /* Which parts of FROM have not yet been coded */ + int nExtraReg, /* Number of extra registers to allocate */ + char **pzAff /* OUT: Set to point to affinity string */ +){ + int nEq = pLevel->plan.nEq; /* The number of == or IN constraints to code */ + Vdbe *v = pParse->pVdbe; /* The vm under construction */ + Index *pIdx; /* The index being used for this loop */ + int iCur = pLevel->iTabCur; /* The cursor of the table */ + WhereTerm *pTerm; /* A single constraint term */ + int j; /* Loop counter */ + int regBase; /* Base register */ + int nReg; /* Number of registers to allocate */ + char *zAff; /* Affinity string to return */ + + /* This module is only called on query plans that use an index. */ + assert( pLevel->plan.wsFlags & WHERE_INDEXED ); + pIdx = pLevel->plan.u.pIdx; + + /* Figure out how many memory cells we will need then allocate them. + */ + regBase = pParse->nMem + 1; + nReg = pLevel->plan.nEq + nExtraReg; + pParse->nMem += nReg; + + zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx)); + if( !zAff ){ + pParse->db->mallocFailed = 1; + } + + /* Evaluate the equality constraints + */ + assert( pIdx->nColumn>=nEq ); + for(j=0; jaiColumn[j]; + pTerm = findTerm(pWC, iCur, k, notReady, pLevel->plan.wsFlags, pIdx); + if( NEVER(pTerm==0) ) break; + /* The following true for indices with redundant columns. + ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */ + testcase( (pTerm->wtFlags & TERM_CODED)!=0 ); + testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ + r1 = codeEqualityTerm(pParse, pTerm, pLevel, regBase+j); + if( r1!=regBase+j ){ + if( nReg==1 ){ + sqlite3ReleaseTempReg(pParse, regBase); + regBase = r1; + }else{ + sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j); + } + } + testcase( pTerm->eOperator & WO_ISNULL ); + testcase( pTerm->eOperator & WO_IN ); + if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ + Expr *pRight = pTerm->pExpr->pRight; + sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk); + if( zAff ){ + if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){ + zAff[j] = SQLITE_AFF_NONE; + } + if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){ + zAff[j] = SQLITE_AFF_NONE; + } + } + } + } + *pzAff = zAff; + return regBase; +} + +#ifndef SQLITE_OMIT_EXPLAIN +/* +** This routine is a helper for explainIndexRange() below +** +** pStr holds the text of an expression that we are building up one term +** at a time. This routine adds a new term to the end of the expression. +** Terms are separated by AND so add the "AND" text for second and subsequent +** terms only. +*/ +static void explainAppendTerm( + StrAccum *pStr, /* The text expression being built */ + int iTerm, /* Index of this term. First is zero */ + const char *zColumn, /* Name of the column */ + const char *zOp /* Name of the operator */ +){ + if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5); + sqlite3StrAccumAppend(pStr, zColumn, -1); + sqlite3StrAccumAppend(pStr, zOp, 1); + sqlite3StrAccumAppend(pStr, "?", 1); +} + +/* +** Argument pLevel describes a strategy for scanning table pTab. This +** function returns a pointer to a string buffer containing a description +** of the subset of table rows scanned by the strategy in the form of an +** SQL expression. Or, if all rows are scanned, NULL is returned. +** +** For example, if the query: +** +** SELECT * FROM t1 WHERE a=1 AND b>2; +** +** is run and there is an index on (a, b), then this function returns a +** string similar to: +** +** "a=? AND b>?" +** +** The returned pointer points to memory obtained from sqlite3DbMalloc(). +** It is the responsibility of the caller to free the buffer when it is +** no longer required. +*/ +static char *explainIndexRange(sqlite3 *db, WhereLevel *pLevel, Table *pTab){ + WherePlan *pPlan = &pLevel->plan; + Index *pIndex = pPlan->u.pIdx; + int nEq = pPlan->nEq; + int i, j; + Column *aCol = pTab->aCol; + int *aiColumn = pIndex->aiColumn; + StrAccum txt; + + if( nEq==0 && (pPlan->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){ + return 0; + } + sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH); + txt.db = db; + sqlite3StrAccumAppend(&txt, " (", 2); + for(i=0; i"); + } + if( pPlan->wsFlags&WHERE_TOP_LIMIT ){ + explainAppendTerm(&txt, i, aCol[aiColumn[j]].zName, "<"); + } + sqlite3StrAccumAppend(&txt, ")", 1); + return sqlite3StrAccumFinish(&txt); +} + +/* +** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN +** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single +** record is added to the output to describe the table scan strategy in +** pLevel. +*/ +static void explainOneScan( + Parse *pParse, /* Parse context */ + SrcList *pTabList, /* Table list this loop refers to */ + WhereLevel *pLevel, /* Scan to write OP_Explain opcode for */ + int iLevel, /* Value for "level" column of output */ + int iFrom, /* Value for "from" column of output */ + u16 wctrlFlags /* Flags passed to sqlite3WhereBegin() */ +){ + if( pParse->explain==2 ){ + u32 flags = pLevel->plan.wsFlags; + struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom]; + Vdbe *v = pParse->pVdbe; /* VM being constructed */ + sqlite3 *db = pParse->db; /* Database handle */ + char *zMsg; /* Text to add to EQP output */ + sqlite3_int64 nRow; /* Expected number of rows visited by scan */ + int iId = pParse->iSelectId; /* Select id (left-most output column) */ + int isSearch; /* True for a SEARCH. False for SCAN. */ + + if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return; + + isSearch = (pLevel->plan.nEq>0) + || (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 + || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX)); + + zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN"); + if( pItem->pSelect ){ + zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId); + }else{ + zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName); + } + + if( pItem->zAlias ){ + zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias); + } + if( (flags & WHERE_INDEXED)!=0 ){ + char *zWhere = explainIndexRange(db, pLevel, pItem->pTab); + zMsg = sqlite3MAppendf(db, zMsg, "%s USING %s%sINDEX%s%s%s", zMsg, + ((flags & WHERE_TEMP_INDEX)?"AUTOMATIC ":""), + ((flags & WHERE_IDX_ONLY)?"COVERING ":""), + ((flags & WHERE_TEMP_INDEX)?"":" "), + ((flags & WHERE_TEMP_INDEX)?"": pLevel->plan.u.pIdx->zName), + zWhere + ); + sqlite3DbFree(db, zWhere); + }else if( flags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){ + zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg); + + if( flags&WHERE_ROWID_EQ ){ + zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg); + }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){ + zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid?)", zMsg); + }else if( flags&WHERE_TOP_LIMIT ){ + zMsg = sqlite3MAppendf(db, zMsg, "%s (rowidplan.u.pVtabIdx; + zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg, + pVtabIdx->idxNum, pVtabIdx->idxStr); + } +#endif + if( wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX) ){ + testcase( wctrlFlags & WHERE_ORDERBY_MIN ); + nRow = 1; + }else{ + nRow = (sqlite3_int64)pLevel->plan.nRow; + } + zMsg = sqlite3MAppendf(db, zMsg, "%s (~%lld rows)", zMsg, nRow); + sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC); + } +} +#else +#define explainOneScan(u,v,w,x,y,z) +#endif /* SQLITE_OMIT_EXPLAIN */ + + +/* +** Generate code for the start of the iLevel-th loop in the WHERE clause +** implementation described by pWInfo. +*/ +static Bitmask codeOneLoopStart( + WhereInfo *pWInfo, /* Complete information about the WHERE clause */ + int iLevel, /* Which level of pWInfo->a[] should be coded */ + u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */ + Bitmask notReady /* Which tables are currently available */ +){ + int j, k; /* Loop counters */ + int iCur; /* The VDBE cursor for the table */ + int addrNxt; /* Where to jump to continue with the next IN case */ + int omitTable; /* True if we use the index only */ + int bRev; /* True if we need to scan in reverse order */ + WhereLevel *pLevel; /* The where level to be coded */ + WhereClause *pWC; /* Decomposition of the entire WHERE clause */ + WhereTerm *pTerm; /* A WHERE clause term */ + Parse *pParse; /* Parsing context */ + Vdbe *v; /* The prepared stmt under constructions */ + struct SrcList_item *pTabItem; /* FROM clause term being coded */ + int addrBrk; /* Jump here to break out of the loop */ + int addrCont; /* Jump here to continue with next cycle */ + int iRowidReg = 0; /* Rowid is stored in this register, if not zero */ + int iReleaseReg = 0; /* Temp register to free before returning */ + + pParse = pWInfo->pParse; + v = pParse->pVdbe; + pWC = pWInfo->pWC; + pLevel = &pWInfo->a[iLevel]; + pTabItem = &pWInfo->pTabList->a[pLevel->iFrom]; + iCur = pTabItem->iCursor; + bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0; + omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0 + && (wctrlFlags & WHERE_FORCE_TABLE)==0; + + /* Create labels for the "break" and "continue" instructions + ** for the current loop. Jump to addrBrk to break out of a loop. + ** Jump to cont to go immediately to the next iteration of the + ** loop. + ** + ** When there is an IN operator, we also have a "addrNxt" label that + ** means to continue with the next IN value combination. When + ** there are no IN operators in the constraints, the "addrNxt" label + ** is the same as "addrBrk". + */ + addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v); + addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v); + + /* If this is the right table of a LEFT OUTER JOIN, allocate and + ** initialize a memory cell that records if this table matches any + ** row of the left table of the join. + */ + if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){ + pLevel->iLeftJoin = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin); + VdbeComment((v, "init LEFT JOIN no-match flag")); + } + +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){ + /* Case 0: The table is a virtual-table. Use the VFilter and VNext + ** to access the data. + */ + int iReg; /* P3 Value for OP_VFilter */ + sqlite3_index_info *pVtabIdx = pLevel->plan.u.pVtabIdx; + int nConstraint = pVtabIdx->nConstraint; + struct sqlite3_index_constraint_usage *aUsage = + pVtabIdx->aConstraintUsage; + const struct sqlite3_index_constraint *aConstraint = + pVtabIdx->aConstraint; + + sqlite3ExprCachePush(pParse); + iReg = sqlite3GetTempRange(pParse, nConstraint+2); + for(j=1; j<=nConstraint; j++){ + for(k=0; ka[iTerm].pExpr->pRight, iReg+j+1); + break; + } + } + if( k==nConstraint ) break; + } + sqlite3VdbeAddOp2(v, OP_Integer, pVtabIdx->idxNum, iReg); + sqlite3VdbeAddOp2(v, OP_Integer, j-1, iReg+1); + sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrBrk, iReg, pVtabIdx->idxStr, + pVtabIdx->needToFreeIdxStr ? P4_MPRINTF : P4_STATIC); + pVtabIdx->needToFreeIdxStr = 0; + for(j=0; ja[iTerm]); + } + } + pLevel->op = OP_VNext; + pLevel->p1 = iCur; + pLevel->p2 = sqlite3VdbeCurrentAddr(v); + sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2); + sqlite3ExprCachePop(pParse, 1); + }else +#endif /* SQLITE_OMIT_VIRTUALTABLE */ + + if( pLevel->plan.wsFlags & WHERE_ROWID_EQ ){ + /* Case 1: We can directly reference a single row using an + ** equality comparison against the ROWID field. Or + ** we reference multiple rows using a "rowid IN (...)" + ** construct. + */ + iReleaseReg = sqlite3GetTempReg(pParse); + pTerm = findTerm(pWC, iCur, -1, notReady, WO_EQ|WO_IN, 0); + assert( pTerm!=0 ); + assert( pTerm->pExpr!=0 ); + assert( pTerm->leftCursor==iCur ); + assert( omitTable==0 ); + testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ + iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, iReleaseReg); + addrNxt = pLevel->addrNxt; + sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt); + sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg); + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); + VdbeComment((v, "pk")); + pLevel->op = OP_Noop; + }else if( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ){ + /* Case 2: We have an inequality comparison against the ROWID field. + */ + int testOp = OP_Noop; + int start; + int memEndValue = 0; + WhereTerm *pStart, *pEnd; + + assert( omitTable==0 ); + pStart = findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0); + pEnd = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0); + if( bRev ){ + pTerm = pStart; + pStart = pEnd; + pEnd = pTerm; + } + if( pStart ){ + Expr *pX; /* The expression that defines the start bound */ + int r1, rTemp; /* Registers for holding the start boundary */ + + /* The following constant maps TK_xx codes into corresponding + ** seek opcodes. It depends on a particular ordering of TK_xx + */ + const u8 aMoveOp[] = { + /* TK_GT */ OP_SeekGt, + /* TK_LE */ OP_SeekLe, + /* TK_LT */ OP_SeekLt, + /* TK_GE */ OP_SeekGe + }; + assert( TK_LE==TK_GT+1 ); /* Make sure the ordering.. */ + assert( TK_LT==TK_GT+2 ); /* ... of the TK_xx values... */ + assert( TK_GE==TK_GT+3 ); /* ... is correcct. */ + + testcase( pStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ + pX = pStart->pExpr; + assert( pX!=0 ); + assert( pStart->leftCursor==iCur ); + r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp); + sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1); + VdbeComment((v, "pk")); + sqlite3ExprCacheAffinityChange(pParse, r1, 1); + sqlite3ReleaseTempReg(pParse, rTemp); + disableTerm(pLevel, pStart); + }else{ + sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk); + } + if( pEnd ){ + Expr *pX; + pX = pEnd->pExpr; + assert( pX!=0 ); + assert( pEnd->leftCursor==iCur ); + testcase( pEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ + memEndValue = ++pParse->nMem; + sqlite3ExprCode(pParse, pX->pRight, memEndValue); + if( pX->op==TK_LT || pX->op==TK_GT ){ + testOp = bRev ? OP_Le : OP_Ge; + }else{ + testOp = bRev ? OP_Lt : OP_Gt; + } + disableTerm(pLevel, pEnd); + } + start = sqlite3VdbeCurrentAddr(v); + pLevel->op = bRev ? OP_Prev : OP_Next; + pLevel->p1 = iCur; + pLevel->p2 = start; + if( pStart==0 && pEnd==0 ){ + pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; + }else{ + assert( pLevel->p5==0 ); + } + if( testOp!=OP_Noop ){ + iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg); + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); + sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg); + sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL); + } + }else if( pLevel->plan.wsFlags & (WHERE_COLUMN_RANGE|WHERE_COLUMN_EQ) ){ + /* Case 3: A scan using an index. + ** + ** The WHERE clause may contain zero or more equality + ** terms ("==" or "IN" operators) that refer to the N + ** left-most columns of the index. It may also contain + ** inequality constraints (>, <, >= or <=) on the indexed + ** column that immediately follows the N equalities. Only + ** the right-most column can be an inequality - the rest must + ** use the "==" and "IN" operators. For example, if the + ** index is on (x,y,z), then the following clauses are all + ** optimized: + ** + ** x=5 + ** x=5 AND y=10 + ** x=5 AND y<10 + ** x=5 AND y>5 AND y<10 + ** x=5 AND y=5 AND z<=10 + ** + ** The z<10 term of the following cannot be used, only + ** the x=5 term: + ** + ** x=5 AND z<10 + ** + ** N may be zero if there are inequality constraints. + ** If there are no inequality constraints, then N is at + ** least one. + ** + ** This case is also used when there are no WHERE clause + ** constraints but an index is selected anyway, in order + ** to force the output order to conform to an ORDER BY. + */ + static const u8 aStartOp[] = { + 0, + 0, + OP_Rewind, /* 2: (!start_constraints && startEq && !bRev) */ + OP_Last, /* 3: (!start_constraints && startEq && bRev) */ + OP_SeekGt, /* 4: (start_constraints && !startEq && !bRev) */ + OP_SeekLt, /* 5: (start_constraints && !startEq && bRev) */ + OP_SeekGe, /* 6: (start_constraints && startEq && !bRev) */ + OP_SeekLe /* 7: (start_constraints && startEq && bRev) */ + }; + static const u8 aEndOp[] = { + OP_Noop, /* 0: (!end_constraints) */ + OP_IdxGE, /* 1: (end_constraints && !bRev) */ + OP_IdxLT /* 2: (end_constraints && bRev) */ + }; + int nEq = pLevel->plan.nEq; /* Number of == or IN terms */ + int isMinQuery = 0; /* If this is an optimized SELECT min(x).. */ + int regBase; /* Base register holding constraint values */ + int r1; /* Temp register */ + WhereTerm *pRangeStart = 0; /* Inequality constraint at range start */ + WhereTerm *pRangeEnd = 0; /* Inequality constraint at range end */ + int startEq; /* True if range start uses ==, >= or <= */ + int endEq; /* True if range end uses ==, >= or <= */ + int start_constraints; /* Start of range is constrained */ + int nConstraint; /* Number of constraint terms */ + Index *pIdx; /* The index we will be using */ + int iIdxCur; /* The VDBE cursor for the index */ + int nExtraReg = 0; /* Number of extra registers needed */ + int op; /* Instruction opcode */ + char *zStartAff; /* Affinity for start of range constraint */ + char *zEndAff; /* Affinity for end of range constraint */ + + pIdx = pLevel->plan.u.pIdx; + iIdxCur = pLevel->iIdxCur; + k = pIdx->aiColumn[nEq]; /* Column for inequality constraints */ + + /* If this loop satisfies a sort order (pOrderBy) request that + ** was passed to this function to implement a "SELECT min(x) ..." + ** query, then the caller will only allow the loop to run for + ** a single iteration. This means that the first row returned + ** should not have a NULL value stored in 'x'. If column 'x' is + ** the first one after the nEq equality constraints in the index, + ** this requires some special handling. + */ + if( (wctrlFlags&WHERE_ORDERBY_MIN)!=0 + && (pLevel->plan.wsFlags&WHERE_ORDERBY) + && (pIdx->nColumn>nEq) + ){ + /* assert( pOrderBy->nExpr==1 ); */ + /* assert( pOrderBy->a[0].pExpr->iColumn==pIdx->aiColumn[nEq] ); */ + isMinQuery = 1; + nExtraReg = 1; + } + + /* Find any inequality constraint terms for the start and end + ** of the range. + */ + if( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ){ + pRangeEnd = findTerm(pWC, iCur, k, notReady, (WO_LT|WO_LE), pIdx); + nExtraReg = 1; + } + if( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ){ + pRangeStart = findTerm(pWC, iCur, k, notReady, (WO_GT|WO_GE), pIdx); + nExtraReg = 1; + } + + /* Generate code to evaluate all constraint terms using == or IN + ** and store the values of those terms in an array of registers + ** starting at regBase. + */ + regBase = codeAllEqualityTerms( + pParse, pLevel, pWC, notReady, nExtraReg, &zStartAff + ); + zEndAff = sqlite3DbStrDup(pParse->db, zStartAff); + addrNxt = pLevel->addrNxt; + + /* If we are doing a reverse order scan on an ascending index, or + ** a forward order scan on a descending index, interchange the + ** start and end terms (pRangeStart and pRangeEnd). + */ + if( nEqnColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC) ){ + SWAP(WhereTerm *, pRangeEnd, pRangeStart); + } + + testcase( pRangeStart && pRangeStart->eOperator & WO_LE ); + testcase( pRangeStart && pRangeStart->eOperator & WO_GE ); + testcase( pRangeEnd && pRangeEnd->eOperator & WO_LE ); + testcase( pRangeEnd && pRangeEnd->eOperator & WO_GE ); + startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE); + endEq = !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE); + start_constraints = pRangeStart || nEq>0; + + /* Seek the index cursor to the start of the range. */ + nConstraint = nEq; + if( pRangeStart ){ + Expr *pRight = pRangeStart->pExpr->pRight; + sqlite3ExprCode(pParse, pRight, regBase+nEq); + if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){ + sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); + } + if( zStartAff ){ + if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){ + /* Since the comparison is to be performed with no conversions + ** applied to the operands, set the affinity to apply to pRight to + ** SQLITE_AFF_NONE. */ + zStartAff[nEq] = SQLITE_AFF_NONE; + } + if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){ + zStartAff[nEq] = SQLITE_AFF_NONE; + } + } + nConstraint++; + testcase( pRangeStart->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ + }else if( isMinQuery ){ + sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq); + nConstraint++; + startEq = 0; + start_constraints = 1; + } + codeApplyAffinity(pParse, regBase, nConstraint, zStartAff); + op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev]; + assert( op!=0 ); + testcase( op==OP_Rewind ); + testcase( op==OP_Last ); + testcase( op==OP_SeekGt ); + testcase( op==OP_SeekGe ); + testcase( op==OP_SeekLe ); + testcase( op==OP_SeekLt ); + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); + + /* Load the value for the inequality constraint at the end of the + ** range (if any). + */ + nConstraint = nEq; + if( pRangeEnd ){ + Expr *pRight = pRangeEnd->pExpr->pRight; + sqlite3ExprCacheRemove(pParse, regBase+nEq, 1); + sqlite3ExprCode(pParse, pRight, regBase+nEq); + if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){ + sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt); + } + if( zEndAff ){ + if( sqlite3CompareAffinity(pRight, zEndAff[nEq])==SQLITE_AFF_NONE){ + /* Since the comparison is to be performed with no conversions + ** applied to the operands, set the affinity to apply to pRight to + ** SQLITE_AFF_NONE. */ + zEndAff[nEq] = SQLITE_AFF_NONE; + } + if( sqlite3ExprNeedsNoAffinityChange(pRight, zEndAff[nEq]) ){ + zEndAff[nEq] = SQLITE_AFF_NONE; + } + } + codeApplyAffinity(pParse, regBase, nEq+1, zEndAff); + nConstraint++; + testcase( pRangeEnd->wtFlags & TERM_VIRTUAL ); /* EV: R-30575-11662 */ + } + sqlite3DbFree(pParse->db, zStartAff); + sqlite3DbFree(pParse->db, zEndAff); + + /* Top of the loop body */ + pLevel->p2 = sqlite3VdbeCurrentAddr(v); + + /* Check if the index cursor is past the end of the range. */ + op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)]; + testcase( op==OP_Noop ); + testcase( op==OP_IdxGE ); + testcase( op==OP_IdxLT ); + if( op!=OP_Noop ){ + sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint); + sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0); + } + + /* If there are inequality constraints, check that the value + ** of the table column that the inequality contrains is not NULL. + ** If it is, jump to the next iteration of the loop. + */ + r1 = sqlite3GetTempReg(pParse); + testcase( pLevel->plan.wsFlags & WHERE_BTM_LIMIT ); + testcase( pLevel->plan.wsFlags & WHERE_TOP_LIMIT ); + if( (pLevel->plan.wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0 ){ + sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1); + sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont); + } + sqlite3ReleaseTempReg(pParse, r1); + + /* Seek the table cursor, if required */ + disableTerm(pLevel, pRangeStart); + disableTerm(pLevel, pRangeEnd); + if( !omitTable ){ + iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse); + sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg); + sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg); + sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */ + } + + /* Record the instruction used to terminate the loop. Disable + ** WHERE clause terms made redundant by the index range scan. + */ + if( pLevel->plan.wsFlags & WHERE_UNIQUE ){ + pLevel->op = OP_Noop; + }else if( bRev ){ + pLevel->op = OP_Prev; + }else{ + pLevel->op = OP_Next; + } + pLevel->p1 = iIdxCur; + }else + +#ifndef SQLITE_OMIT_OR_OPTIMIZATION + if( pLevel->plan.wsFlags & WHERE_MULTI_OR ){ + /* Case 4: Two or more separately indexed terms connected by OR + ** + ** Example: + ** + ** CREATE TABLE t1(a,b,c,d); + ** CREATE INDEX i1 ON t1(a); + ** CREATE INDEX i2 ON t1(b); + ** CREATE INDEX i3 ON t1(c); + ** + ** SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13) + ** + ** In the example, there are three indexed terms connected by OR. + ** The top of the loop looks like this: + ** + ** Null 1 # Zero the rowset in reg 1 + ** + ** Then, for each indexed term, the following. The arguments to + ** RowSetTest are such that the rowid of the current row is inserted + ** into the RowSet. If it is already present, control skips the + ** Gosub opcode and jumps straight to the code generated by WhereEnd(). + ** + ** sqlite3WhereBegin() + ** RowSetTest # Insert rowid into rowset + ** Gosub 2 A + ** sqlite3WhereEnd() + ** + ** Following the above, code to terminate the loop. Label A, the target + ** of the Gosub above, jumps to the instruction right after the Goto. + ** + ** Null 1 # Zero the rowset in reg 1 + ** Goto B # The loop is finished. + ** + ** A: # Return data, whatever. + ** + ** Return 2 # Jump back to the Gosub + ** + ** B: + ** + */ + WhereClause *pOrWc; /* The OR-clause broken out into subterms */ + SrcList *pOrTab; /* Shortened table list or OR-clause generation */ + + int regReturn = ++pParse->nMem; /* Register used with OP_Gosub */ + int regRowset = 0; /* Register for RowSet object */ + int regRowid = 0; /* Register holding rowid */ + int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */ + int iRetInit; /* Address of regReturn init */ + int untestedTerms = 0; /* Some terms not completely tested */ + int ii; + + pTerm = pLevel->plan.u.pTerm; + assert( pTerm!=0 ); + assert( pTerm->eOperator==WO_OR ); + assert( (pTerm->wtFlags & TERM_ORINFO)!=0 ); + pOrWc = &pTerm->u.pOrInfo->wc; + pLevel->op = OP_Return; + pLevel->p1 = regReturn; + + /* Set up a new SrcList ni pOrTab containing the table being scanned + ** by this loop in the a[0] slot and all notReady tables in a[1..] slots. + ** This becomes the SrcList in the recursive call to sqlite3WhereBegin(). + */ + if( pWInfo->nLevel>1 ){ + int nNotReady; /* The number of notReady tables */ + struct SrcList_item *origSrc; /* Original list of tables */ + nNotReady = pWInfo->nLevel - iLevel - 1; + pOrTab = sqlite3StackAllocRaw(pParse->db, + sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0])); + if( pOrTab==0 ) return notReady; + pOrTab->nAlloc = (i16)(nNotReady + 1); + pOrTab->nSrc = pOrTab->nAlloc; + memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem)); + origSrc = pWInfo->pTabList->a; + for(k=1; k<=nNotReady; k++){ + memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k])); + } + }else{ + pOrTab = pWInfo->pTabList; + } + + /* Initialize the rowset register to contain NULL. An SQL NULL is + ** equivalent to an empty rowset. + ** + ** Also initialize regReturn to contain the address of the instruction + ** immediately following the OP_Return at the bottom of the loop. This + ** is required in a few obscure LEFT JOIN cases where control jumps + ** over the top of the loop into the body of it. In this case the + ** correct response for the end-of-loop code (the OP_Return) is to + ** fall through to the next instruction, just as an OP_Next does if + ** called on an uninitialized cursor. + */ + if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ + regRowset = ++pParse->nMem; + regRowid = ++pParse->nMem; + sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset); + } + iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn); + + for(ii=0; iinTerm; ii++){ + WhereTerm *pOrTerm = &pOrWc->a[ii]; + if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){ + WhereInfo *pSubWInfo; /* Info for single OR-term scan */ + /* Loop through table entries that match term pOrTerm. */ + pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrTerm->pExpr, 0, + WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE | + WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY); + if( pSubWInfo ){ + explainOneScan( + pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0 + ); + if( (wctrlFlags & WHERE_DUPLICATES_OK)==0 ){ + int iSet = ((ii==pOrWc->nTerm-1)?-1:ii); + int r; + r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur, + regRowid); + sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, + sqlite3VdbeCurrentAddr(v)+2, r, iSet); + } + sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody); + + /* The pSubWInfo->untestedTerms flag means that this OR term + ** contained one or more AND term from a notReady table. The + ** terms from the notReady table could not be tested and will + ** need to be tested later. + */ + if( pSubWInfo->untestedTerms ) untestedTerms = 1; + + /* Finish the loop through table entries that match term pOrTerm. */ + sqlite3WhereEnd(pSubWInfo); + } + } + } + sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v)); + sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk); + sqlite3VdbeResolveLabel(v, iLoopBody); + + if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab); + if( !untestedTerms ) disableTerm(pLevel, pTerm); + }else +#endif /* SQLITE_OMIT_OR_OPTIMIZATION */ + + { + /* Case 5: There is no usable index. We must do a complete + ** scan of the entire table. + */ + static const u8 aStep[] = { OP_Next, OP_Prev }; + static const u8 aStart[] = { OP_Rewind, OP_Last }; + assert( bRev==0 || bRev==1 ); + assert( omitTable==0 ); + pLevel->op = aStep[bRev]; + pLevel->p1 = iCur; + pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk); + pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP; + } + notReady &= ~getMask(pWC->pMaskSet, iCur); + + /* Insert code to test every subexpression that can be completely + ** computed using the current set of tables. + ** + ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through + ** the use of indices become tests that are evaluated against each row of + ** the relevant input tables. + */ + for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){ + Expr *pE; + testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ + testcase( pTerm->wtFlags & TERM_CODED ); + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; + if( (pTerm->prereqAll & notReady)!=0 ){ + testcase( pWInfo->untestedTerms==0 + && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ); + pWInfo->untestedTerms = 1; + continue; + } + pE = pTerm->pExpr; + assert( pE!=0 ); + if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){ + continue; + } + sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL); + pTerm->wtFlags |= TERM_CODED; + } + + /* For a LEFT OUTER JOIN, generate code that will record the fact that + ** at least one row of the right table has matched the left table. + */ + if( pLevel->iLeftJoin ){ + pLevel->addrFirst = sqlite3VdbeCurrentAddr(v); + sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin); + VdbeComment((v, "record LEFT JOIN hit")); + sqlite3ExprCacheClear(pParse); + for(pTerm=pWC->a, j=0; jnTerm; j++, pTerm++){ + testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */ + testcase( pTerm->wtFlags & TERM_CODED ); + if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue; + if( (pTerm->prereqAll & notReady)!=0 ){ + assert( pWInfo->untestedTerms ); + continue; + } + assert( pTerm->pExpr ); + sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL); + pTerm->wtFlags |= TERM_CODED; + } + } + sqlite3ReleaseTempReg(pParse, iReleaseReg); + + return notReady; +} + +#if defined(SQLITE_TEST) +/* +** The following variable holds a text description of query plan generated +** by the most recent call to sqlite3WhereBegin(). Each call to WhereBegin +** overwrites the previous. This information is used for testing and +** analysis only. +*/ +SQLITE_API char sqlite3_query_plan[BMS*2*40]; /* Text of the join */ +static int nQPlan = 0; /* Next free slow in _query_plan[] */ + +#endif /* SQLITE_TEST */ + + +/* +** Free a WhereInfo structure +*/ +static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){ + if( ALWAYS(pWInfo) ){ + int i; + for(i=0; inLevel; i++){ + sqlite3_index_info *pInfo = pWInfo->a[i].pIdxInfo; + if( pInfo ){ + /* assert( pInfo->needToFreeIdxStr==0 || db->mallocFailed ); */ + if( pInfo->needToFreeIdxStr ){ + sqlite3_free(pInfo->idxStr); + } + sqlite3DbFree(db, pInfo); + } + if( pWInfo->a[i].plan.wsFlags & WHERE_TEMP_INDEX ){ + Index *pIdx = pWInfo->a[i].plan.u.pIdx; + if( pIdx ){ + sqlite3DbFree(db, pIdx->zColAff); + sqlite3DbFree(db, pIdx); + } + } + } + whereClauseClear(pWInfo->pWC); + sqlite3DbFree(db, pWInfo); + } +} + + +/* +** Generate the beginning of the loop used for WHERE clause processing. +** The return value is a pointer to an opaque structure that contains +** information needed to terminate the loop. Later, the calling routine +** should invoke sqlite3WhereEnd() with the return value of this function +** in order to complete the WHERE clause processing. +** +** If an error occurs, this routine returns NULL. +** +** The basic idea is to do a nested loop, one loop for each table in +** the FROM clause of a select. (INSERT and UPDATE statements are the +** same as a SELECT with only a single table in the FROM clause.) For +** example, if the SQL is this: +** +** SELECT * FROM t1, t2, t3 WHERE ...; +** +** Then the code generated is conceptually like the following: +** +** foreach row1 in t1 do \ Code generated +** foreach row2 in t2 do |-- by sqlite3WhereBegin() +** foreach row3 in t3 do / +** ... +** end \ Code generated +** end |-- by sqlite3WhereEnd() +** end / +** +** Note that the loops might not be nested in the order in which they +** appear in the FROM clause if a different order is better able to make +** use of indices. Note also that when the IN operator appears in +** the WHERE clause, it might result in additional nested loops for +** scanning through all values on the right-hand side of the IN. +** +** There are Btree cursors associated with each table. t1 uses cursor +** number pTabList->a[0].iCursor. t2 uses the cursor pTabList->a[1].iCursor. +** And so forth. This routine generates code to open those VDBE cursors +** and sqlite3WhereEnd() generates the code to close them. +** +** The code that sqlite3WhereBegin() generates leaves the cursors named +** in pTabList pointing at their appropriate entries. The [...] code +** can use OP_Column and OP_Rowid opcodes on these cursors to extract +** data from the various tables of the loop. +** +** If the WHERE clause is empty, the foreach loops must each scan their +** entire tables. Thus a three-way join is an O(N^3) operation. But if +** the tables have indices and there are terms in the WHERE clause that +** refer to those indices, a complete table scan can be avoided and the +** code will run much faster. Most of the work of this routine is checking +** to see if there are indices that can be used to speed up the loop. +** +** Terms of the WHERE clause are also used to limit which rows actually +** make it to the "..." in the middle of the loop. After each "foreach", +** terms of the WHERE clause that use only terms in that loop and outer +** loops are evaluated and if false a jump is made around all subsequent +** inner loops (or around the "..." if the test occurs within the inner- +** most loop) +** +** OUTER JOINS +** +** An outer join of tables t1 and t2 is conceptally coded as follows: +** +** foreach row1 in t1 do +** flag = 0 +** foreach row2 in t2 do +** start: +** ... +** flag = 1 +** end +** if flag==0 then +** move the row2 cursor to a null row +** goto start +** fi +** end +** +** ORDER BY CLAUSE PROCESSING +** +** *ppOrderBy is a pointer to the ORDER BY clause of a SELECT statement, +** if there is one. If there is no ORDER BY clause or if this routine +** is called from an UPDATE or DELETE statement, then ppOrderBy is NULL. +** +** If an index can be used so that the natural output order of the table +** scan is correct for the ORDER BY clause, then that index is used and +** *ppOrderBy is set to NULL. This is an optimization that prevents an +** unnecessary sort of the result set if an index appropriate for the +** ORDER BY clause already exists. +** +** If the where clause loops cannot be arranged to provide the correct +** output order, then the *ppOrderBy is unchanged. +*/ +SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin( + Parse *pParse, /* The parser context */ + SrcList *pTabList, /* A list of all tables to be scanned */ + Expr *pWhere, /* The WHERE clause */ + ExprList **ppOrderBy, /* An ORDER BY clause, or NULL */ + u16 wctrlFlags /* One of the WHERE_* flags defined in sqliteInt.h */ +){ + int i; /* Loop counter */ + int nByteWInfo; /* Num. bytes allocated for WhereInfo struct */ + int nTabList; /* Number of elements in pTabList */ + WhereInfo *pWInfo; /* Will become the return value of this function */ + Vdbe *v = pParse->pVdbe; /* The virtual database engine */ + Bitmask notReady; /* Cursors that are not yet positioned */ + WhereMaskSet *pMaskSet; /* The expression mask set */ + WhereClause *pWC; /* Decomposition of the WHERE clause */ + struct SrcList_item *pTabItem; /* A single entry from pTabList */ + WhereLevel *pLevel; /* A single level in the pWInfo list */ + int iFrom; /* First unused FROM clause element */ + int andFlags; /* AND-ed combination of all pWC->a[].wtFlags */ + sqlite3 *db; /* Database connection */ + + /* The number of tables in the FROM clause is limited by the number of + ** bits in a Bitmask + */ + testcase( pTabList->nSrc==BMS ); + if( pTabList->nSrc>BMS ){ + sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS); + return 0; + } + + /* This function normally generates a nested loop for all tables in + ** pTabList. But if the WHERE_ONETABLE_ONLY flag is set, then we should + ** only generate code for the first table in pTabList and assume that + ** any cursors associated with subsequent tables are uninitialized. + */ + nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc; + + /* Allocate and initialize the WhereInfo structure that will become the + ** return value. A single allocation is used to store the WhereInfo + ** struct, the contents of WhereInfo.a[], the WhereClause structure + ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte + ** field (type Bitmask) it must be aligned on an 8-byte boundary on + ** some architectures. Hence the ROUND8() below. + */ + db = pParse->db; + nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel)); + pWInfo = sqlite3DbMallocZero(db, + nByteWInfo + + sizeof(WhereClause) + + sizeof(WhereMaskSet) + ); + if( db->mallocFailed ){ + sqlite3DbFree(db, pWInfo); + pWInfo = 0; + goto whereBeginError; + } + pWInfo->nLevel = nTabList; + pWInfo->pParse = pParse; + pWInfo->pTabList = pTabList; + pWInfo->iBreak = sqlite3VdbeMakeLabel(v); + pWInfo->pWC = pWC = (WhereClause *)&((u8 *)pWInfo)[nByteWInfo]; + pWInfo->wctrlFlags = wctrlFlags; + pWInfo->savedNQueryLoop = pParse->nQueryLoop; + pMaskSet = (WhereMaskSet*)&pWC[1]; + + /* Split the WHERE clause into separate subexpressions where each + ** subexpression is separated by an AND operator. + */ + initMaskSet(pMaskSet); + whereClauseInit(pWC, pParse, pMaskSet); + sqlite3ExprCodeConstants(pParse, pWhere); + whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */ + + /* Special case: a WHERE clause that is constant. Evaluate the + ** expression and either jump over all of the code or fall thru. + */ + if( pWhere && (nTabList==0 || sqlite3ExprIsConstantNotJoin(pWhere)) ){ + sqlite3ExprIfFalse(pParse, pWhere, pWInfo->iBreak, SQLITE_JUMPIFNULL); + pWhere = 0; + } + + /* Assign a bit from the bitmask to every term in the FROM clause. + ** + ** When assigning bitmask values to FROM clause cursors, it must be + ** the case that if X is the bitmask for the N-th FROM clause term then + ** the bitmask for all FROM clause terms to the left of the N-th term + ** is (X-1). An expression from the ON clause of a LEFT JOIN can use + ** its Expr.iRightJoinTable value to find the bitmask of the right table + ** of the join. Subtracting one from the right table bitmask gives a + ** bitmask for all tables to the left of the join. Knowing the bitmask + ** for all tables to the left of a left join is important. Ticket #3015. + ** + ** Configure the WhereClause.vmask variable so that bits that correspond + ** to virtual table cursors are set. This is used to selectively disable + ** the OR-to-IN transformation in exprAnalyzeOrTerm(). It is not helpful + ** with virtual tables. + ** + ** Note that bitmasks are created for all pTabList->nSrc tables in + ** pTabList, not just the first nTabList tables. nTabList is normally + ** equal to pTabList->nSrc but might be shortened to 1 if the + ** WHERE_ONETABLE_ONLY flag is set. + */ + assert( pWC->vmask==0 && pMaskSet->n==0 ); + for(i=0; inSrc; i++){ + createMask(pMaskSet, pTabList->a[i].iCursor); +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( ALWAYS(pTabList->a[i].pTab) && IsVirtual(pTabList->a[i].pTab) ){ + pWC->vmask |= ((Bitmask)1 << i); + } +#endif + } +#ifndef NDEBUG + { + Bitmask toTheLeft = 0; + for(i=0; inSrc; i++){ + Bitmask m = getMask(pMaskSet, pTabList->a[i].iCursor); + assert( (m-1)==toTheLeft ); + toTheLeft |= m; + } + } +#endif + + /* Analyze all of the subexpressions. Note that exprAnalyze() might + ** add new virtual terms onto the end of the WHERE clause. We do not + ** want to analyze these virtual terms, so start analyzing at the end + ** and work forward so that the added virtual terms are never processed. + */ + exprAnalyzeAll(pTabList, pWC); + if( db->mallocFailed ){ + goto whereBeginError; + } + + /* Chose the best index to use for each table in the FROM clause. + ** + ** This loop fills in the following fields: + ** + ** pWInfo->a[].pIdx The index to use for this level of the loop. + ** pWInfo->a[].wsFlags WHERE_xxx flags associated with pIdx + ** pWInfo->a[].nEq The number of == and IN constraints + ** pWInfo->a[].iFrom Which term of the FROM clause is being coded + ** pWInfo->a[].iTabCur The VDBE cursor for the database table + ** pWInfo->a[].iIdxCur The VDBE cursor for the index + ** pWInfo->a[].pTerm When wsFlags==WO_OR, the OR-clause term + ** + ** This loop also figures out the nesting order of tables in the FROM + ** clause. + */ + notReady = ~(Bitmask)0; + andFlags = ~0; + WHERETRACE(("*** Optimizer Start ***\n")); + for(i=iFrom=0, pLevel=pWInfo->a; i=0 && bestJ<0; isOptimal--){ + Bitmask mask; /* Mask of tables not yet ready */ + for(j=iFrom, pTabItem=&pTabList->a[j]; jjointype & (JT_LEFT|JT_CROSS))!=0; + if( j!=iFrom && doNotReorder ) break; + m = getMask(pMaskSet, pTabItem->iCursor); + if( (m & notReady)==0 ){ + if( j==iFrom ) iFrom++; + continue; + } + mask = (isOptimal ? m : notReady); + pOrderBy = ((i==0 && ppOrderBy )?*ppOrderBy:0); + if( pTabItem->pIndex==0 ) nUnconstrained++; + + WHERETRACE(("=== trying table %d with isOptimal=%d ===\n", + j, isOptimal)); + assert( pTabItem->pTab ); +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( IsVirtual(pTabItem->pTab) ){ + sqlite3_index_info **pp = &pWInfo->a[j].pIdxInfo; + bestVirtualIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy, + &sCost, pp); + }else +#endif + { + bestBtreeIndex(pParse, pWC, pTabItem, mask, notReady, pOrderBy, + &sCost); + } + assert( isOptimal || (sCost.used¬Ready)==0 ); + + /* If an INDEXED BY clause is present, then the plan must use that + ** index if it uses any index at all */ + assert( pTabItem->pIndex==0 + || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 + || sCost.plan.u.pIdx==pTabItem->pIndex ); + + if( isOptimal && (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 ){ + notIndexed |= m; + } + + /* Conditions under which this table becomes the best so far: + ** + ** (1) The table must not depend on other tables that have not + ** yet run. + ** + ** (2) A full-table-scan plan cannot supercede indexed plan unless + ** the full-table-scan is an "optimal" plan as defined above. + ** + ** (3) All tables have an INDEXED BY clause or this table lacks an + ** INDEXED BY clause or this table uses the specific + ** index specified by its INDEXED BY clause. This rule ensures + ** that a best-so-far is always selected even if an impossible + ** combination of INDEXED BY clauses are given. The error + ** will be detected and relayed back to the application later. + ** The NEVER() comes about because rule (2) above prevents + ** An indexable full-table-scan from reaching rule (3). + ** + ** (4) The plan cost must be lower than prior plans or else the + ** cost must be the same and the number of rows must be lower. + */ + if( (sCost.used¬Ready)==0 /* (1) */ + && (bestJ<0 || (notIndexed&m)!=0 /* (2) */ + || (bestPlan.plan.wsFlags & WHERE_NOT_FULLSCAN)==0 + || (sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0) + && (nUnconstrained==0 || pTabItem->pIndex==0 /* (3) */ + || NEVER((sCost.plan.wsFlags & WHERE_NOT_FULLSCAN)!=0)) + && (bestJ<0 || sCost.rCost=0 ); + assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) ); + WHERETRACE(("*** Optimizer selects table %d for loop %d" + " with cost=%g and nRow=%g\n", + bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow)); + if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){ + *ppOrderBy = 0; + } + andFlags &= bestPlan.plan.wsFlags; + pLevel->plan = bestPlan.plan; + testcase( bestPlan.plan.wsFlags & WHERE_INDEXED ); + testcase( bestPlan.plan.wsFlags & WHERE_TEMP_INDEX ); + if( bestPlan.plan.wsFlags & (WHERE_INDEXED|WHERE_TEMP_INDEX) ){ + pLevel->iIdxCur = pParse->nTab++; + }else{ + pLevel->iIdxCur = -1; + } + notReady &= ~getMask(pMaskSet, pTabList->a[bestJ].iCursor); + pLevel->iFrom = (u8)bestJ; + if( bestPlan.plan.nRow>=(double)1 ){ + pParse->nQueryLoop *= bestPlan.plan.nRow; + } + + /* Check that if the table scanned by this loop iteration had an + ** INDEXED BY clause attached to it, that the named index is being + ** used for the scan. If not, then query compilation has failed. + ** Return an error. + */ + pIdx = pTabList->a[bestJ].pIndex; + if( pIdx ){ + if( (bestPlan.plan.wsFlags & WHERE_INDEXED)==0 ){ + sqlite3ErrorMsg(pParse, "cannot use index: %s", pIdx->zName); + goto whereBeginError; + }else{ + /* If an INDEXED BY clause is used, the bestIndex() function is + ** guaranteed to find the index specified in the INDEXED BY clause + ** if it find an index at all. */ + assert( bestPlan.plan.u.pIdx==pIdx ); + } + } + } + WHERETRACE(("*** Optimizer Finished ***\n")); + if( pParse->nErr || db->mallocFailed ){ + goto whereBeginError; + } + + /* If the total query only selects a single row, then the ORDER BY + ** clause is irrelevant. + */ + if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){ + *ppOrderBy = 0; + } + + /* If the caller is an UPDATE or DELETE statement that is requesting + ** to use a one-pass algorithm, determine if this is appropriate. + ** The one-pass algorithm only works if the WHERE clause constraints + ** the statement to update a single row. + */ + assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 ); + if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 && (andFlags & WHERE_UNIQUE)!=0 ){ + pWInfo->okOnePass = 1; + pWInfo->a[0].plan.wsFlags &= ~WHERE_IDX_ONLY; + } + + /* Open all tables in the pTabList and any indices selected for + ** searching those tables. + */ + sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */ + notReady = ~(Bitmask)0; + pWInfo->nRowOut = (double)1; + for(i=0, pLevel=pWInfo->a; ia[pLevel->iFrom]; + pTab = pTabItem->pTab; + pLevel->iTabCur = pTabItem->iCursor; + pWInfo->nRowOut *= pLevel->plan.nRow; + iDb = sqlite3SchemaToIndex(db, pTab->pSchema); + if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){ + /* Do nothing */ + }else +#ifndef SQLITE_OMIT_VIRTUALTABLE + if( (pLevel->plan.wsFlags & WHERE_VIRTUALTABLE)!=0 ){ + const char *pVTab = (const char *)sqlite3GetVTable(db, pTab); + int iCur = pTabItem->iCursor; + sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB); + }else +#endif + if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 + && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){ + int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; + sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); + testcase( pTab->nCol==BMS-1 ); + testcase( pTab->nCol==BMS ); + if( !pWInfo->okOnePass && pTab->nColcolUsed; + int n = 0; + for(; b; b=b>>1, n++){} + sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, + SQLITE_INT_TO_PTR(n), P4_INT32); + assert( n<=pTab->nCol ); + } + }else{ + sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName); + } +#ifndef SQLITE_OMIT_AUTOMATIC_INDEX + if( (pLevel->plan.wsFlags & WHERE_TEMP_INDEX)!=0 ){ + constructAutomaticIndex(pParse, pWC, pTabItem, notReady, pLevel); + }else +#endif + if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ + Index *pIx = pLevel->plan.u.pIdx; + KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx); + int iIdxCur = pLevel->iIdxCur; + assert( pIx->pSchema==pTab->pSchema ); + assert( iIdxCur>=0 ); + sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIx->tnum, iDb, + (char*)pKey, P4_KEYINFO_HANDOFF); + VdbeComment((v, "%s", pIx->zName)); + } + sqlite3CodeVerifySchema(pParse, iDb); + notReady &= ~getMask(pWC->pMaskSet, pTabItem->iCursor); + } + pWInfo->iTop = sqlite3VdbeCurrentAddr(v); + if( db->mallocFailed ) goto whereBeginError; + + /* Generate the code to do the search. Each iteration of the for + ** loop below generates code for a single nested loop of the VM + ** program. + */ + notReady = ~(Bitmask)0; + for(i=0; ia[i]; + explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags); + notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady); + pWInfo->iContinue = pLevel->addrCont; + } + +#ifdef SQLITE_TEST /* For testing and debugging use only */ + /* Record in the query plan information about the current table + ** and the index used to access it (if any). If the table itself + ** is not used, its name is just '{}'. If no index is used + ** the index is listed as "{}". If the primary key is used the + ** index name is '*'. + */ + for(i=0; ia[i]; + pTabItem = &pTabList->a[pLevel->iFrom]; + z = pTabItem->zAlias; + if( z==0 ) z = pTabItem->pTab->zName; + n = sqlite3Strlen30(z); + if( n+nQPlan < sizeof(sqlite3_query_plan)-10 ){ + if( pLevel->plan.wsFlags & WHERE_IDX_ONLY ){ + memcpy(&sqlite3_query_plan[nQPlan], "{}", 2); + nQPlan += 2; + }else{ + memcpy(&sqlite3_query_plan[nQPlan], z, n); + nQPlan += n; + } + sqlite3_query_plan[nQPlan++] = ' '; + } + testcase( pLevel->plan.wsFlags & WHERE_ROWID_EQ ); + testcase( pLevel->plan.wsFlags & WHERE_ROWID_RANGE ); + if( pLevel->plan.wsFlags & (WHERE_ROWID_EQ|WHERE_ROWID_RANGE) ){ + memcpy(&sqlite3_query_plan[nQPlan], "* ", 2); + nQPlan += 2; + }else if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ){ + n = sqlite3Strlen30(pLevel->plan.u.pIdx->zName); + if( n+nQPlan < sizeof(sqlite3_query_plan)-2 ){ + memcpy(&sqlite3_query_plan[nQPlan], pLevel->plan.u.pIdx->zName, n); + nQPlan += n; + sqlite3_query_plan[nQPlan++] = ' '; + } + }else{ + memcpy(&sqlite3_query_plan[nQPlan], "{} ", 3); + nQPlan += 3; + } + } + while( nQPlan>0 && sqlite3_query_plan[nQPlan-1]==' ' ){ + sqlite3_query_plan[--nQPlan] = 0; + } + sqlite3_query_plan[nQPlan] = 0; + nQPlan = 0; +#endif /* SQLITE_TEST // Testing and debugging use only */ + + /* Record the continuation address in the WhereInfo structure. Then + ** clean up and return. + */ + return pWInfo; + + /* Jump here if malloc fails */ +whereBeginError: + if( pWInfo ){ + pParse->nQueryLoop = pWInfo->savedNQueryLoop; + whereInfoFree(db, pWInfo); + } + return 0; +} + +/* +** Generate the end of the WHERE loop. See comments on +** sqlite3WhereBegin() for additional information. +*/ +SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){ + Parse *pParse = pWInfo->pParse; + Vdbe *v = pParse->pVdbe; + int i; + WhereLevel *pLevel; + SrcList *pTabList = pWInfo->pTabList; + sqlite3 *db = pParse->db; + + /* Generate loop termination code. + */ + sqlite3ExprCacheClear(pParse); + for(i=pWInfo->nLevel-1; i>=0; i--){ + pLevel = &pWInfo->a[i]; + sqlite3VdbeResolveLabel(v, pLevel->addrCont); + if( pLevel->op!=OP_Noop ){ + sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2); + sqlite3VdbeChangeP5(v, pLevel->p5); + } + if( pLevel->plan.wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){ + struct InLoop *pIn; + int j; + sqlite3VdbeResolveLabel(v, pLevel->addrNxt); + for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){ + sqlite3VdbeJumpHere(v, pIn->addrInTop+1); + sqlite3VdbeAddOp2(v, OP_Next, pIn->iCur, pIn->addrInTop); + sqlite3VdbeJumpHere(v, pIn->addrInTop-1); + } + sqlite3DbFree(db, pLevel->u.in.aInLoop); + } + sqlite3VdbeResolveLabel(v, pLevel->addrBrk); + if( pLevel->iLeftJoin ){ + int addr; + addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin); + assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 + || (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 ); + if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 ){ + sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor); + } + if( pLevel->iIdxCur>=0 ){ + sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur); + } + if( pLevel->op==OP_Return ){ + sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst); + }else{ + sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst); + } + sqlite3VdbeJumpHere(v, addr); + } + } + + /* The "break" point is here, just past the end of the outer loop. + ** Set it. + */ + sqlite3VdbeResolveLabel(v, pWInfo->iBreak); + + /* Close all of the cursors that were opened by sqlite3WhereBegin. + */ + assert( pWInfo->nLevel==1 || pWInfo->nLevel==pTabList->nSrc ); + for(i=0, pLevel=pWInfo->a; inLevel; i++, pLevel++){ + struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom]; + Table *pTab = pTabItem->pTab; + assert( pTab!=0 ); + if( (pTab->tabFlags & TF_Ephemeral)==0 + && pTab->pSelect==0 + && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0 + ){ + int ws = pLevel->plan.wsFlags; + if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){ + sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor); + } + if( (ws & WHERE_INDEXED)!=0 && (ws & WHERE_TEMP_INDEX)==0 ){ + sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur); + } + } + + /* If this scan uses an index, make code substitutions to read data + ** from the index in preference to the table. Sometimes, this means + ** the table need never be read from. This is a performance boost, + ** as the vdbe level waits until the table is read before actually + ** seeking the table cursor to the record corresponding to the current + ** position in the index. + ** + ** Calls to the code generator in between sqlite3WhereBegin and + ** sqlite3WhereEnd will have created code that references the table + ** directly. This loop scans all that code looking for opcodes + ** that reference the table and converts them into opcodes that + ** reference the index. + */ + if( (pLevel->plan.wsFlags & WHERE_INDEXED)!=0 && !db->mallocFailed){ + int k, j, last; + VdbeOp *pOp; + Index *pIdx = pLevel->plan.u.pIdx; + + assert( pIdx!=0 ); + pOp = sqlite3VdbeGetOp(v, pWInfo->iTop); + last = sqlite3VdbeCurrentAddr(v); + for(k=pWInfo->iTop; kp1!=pLevel->iTabCur ) continue; + if( pOp->opcode==OP_Column ){ + for(j=0; jnColumn; j++){ + if( pOp->p2==pIdx->aiColumn[j] ){ + pOp->p2 = j; + pOp->p1 = pLevel->iIdxCur; + break; + } + } + assert( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0 + || jnColumn ); + }else if( pOp->opcode==OP_Rowid ){ + pOp->p1 = pLevel->iIdxCur; + pOp->opcode = OP_IdxRowid; + } + } + } + } + + /* Final cleanup + */ + pParse->nQueryLoop = pWInfo->savedNQueryLoop; + whereInfoFree(db, pWInfo); + return; +} + +/************** End of where.c ***********************************************/ +/************** Begin file parse.c *******************************************/ +/* Driver template for the LEMON parser generator. +** The author disclaims copyright to this source code. +** +** This version of "lempar.c" is modified, slightly, for use by SQLite. +** The only modifications are the addition of a couple of NEVER() +** macros to disable tests that are needed in the case of a general +** LALR(1) grammar but which are always false in the +** specific grammar used by SQLite. +*/ +/* First off, code is included that follows the "include" declaration +** in the input grammar file. */ + + +/* +** Disable all error recovery processing in the parser push-down +** automaton. +*/ +#define YYNOERRORRECOVERY 1 + +/* +** Make yytestcase() the same as testcase() +*/ +#define yytestcase(X) testcase(X) + +/* +** An instance of this structure holds information about the +** LIMIT clause of a SELECT statement. +*/ +struct LimitVal { + Expr *pLimit; /* The LIMIT expression. NULL if there is no limit */ + Expr *pOffset; /* The OFFSET expression. NULL if there is none */ +}; + +/* +** An instance of this structure is used to store the LIKE, +** GLOB, NOT LIKE, and NOT GLOB operators. +*/ +struct LikeOp { + Token eOperator; /* "like" or "glob" or "regexp" */ + int not; /* True if the NOT keyword is present */ +}; + +/* +** An instance of the following structure describes the event of a +** TRIGGER. "a" is the event type, one of TK_UPDATE, TK_INSERT, +** TK_DELETE, or TK_INSTEAD. If the event is of the form +** +** UPDATE ON (a,b,c) +** +** Then the "b" IdList records the list "a,b,c". +*/ +struct TrigEvent { int a; IdList * b; }; + +/* +** An instance of this structure holds the ATTACH key and the key type. +*/ +struct AttachKey { int type; Token key; }; + + + /* This is a utility routine used to set the ExprSpan.zStart and + ** ExprSpan.zEnd values of pOut so that the span covers the complete + ** range of text beginning with pStart and going to the end of pEnd. + */ + static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){ + pOut->zStart = pStart->z; + pOut->zEnd = &pEnd->z[pEnd->n]; + } + + /* Construct a new Expr object from a single identifier. Use the + ** new Expr to populate pOut. Set the span of pOut to be the identifier + ** that created the expression. + */ + static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){ + pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue); + pOut->zStart = pValue->z; + pOut->zEnd = &pValue->z[pValue->n]; + } + + /* This routine constructs a binary expression node out of two ExprSpan + ** objects and uses the result to populate a new ExprSpan object. + */ + static void spanBinaryExpr( + ExprSpan *pOut, /* Write the result here */ + Parse *pParse, /* The parsing context. Errors accumulate here */ + int op, /* The binary operation */ + ExprSpan *pLeft, /* The left operand */ + ExprSpan *pRight /* The right operand */ + ){ + pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0); + pOut->zStart = pLeft->zStart; + pOut->zEnd = pRight->zEnd; + } + + /* Construct an expression node for a unary postfix operator + */ + static void spanUnaryPostfix( + ExprSpan *pOut, /* Write the new expression node here */ + Parse *pParse, /* Parsing context to record errors */ + int op, /* The operator */ + ExprSpan *pOperand, /* The operand */ + Token *pPostOp /* The operand token for setting the span */ + ){ + pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0); + pOut->zStart = pOperand->zStart; + pOut->zEnd = &pPostOp->z[pPostOp->n]; + } + + /* A routine to convert a binary TK_IS or TK_ISNOT expression into a + ** unary TK_ISNULL or TK_NOTNULL expression. */ + static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){ + sqlite3 *db = pParse->db; + if( db->mallocFailed==0 && pY->op==TK_NULL ){ + pA->op = (u8)op; + sqlite3ExprDelete(db, pA->pRight); + pA->pRight = 0; + } + } + + /* Construct an expression node for a unary prefix operator + */ + static void spanUnaryPrefix( + ExprSpan *pOut, /* Write the new expression node here */ + Parse *pParse, /* Parsing context to record errors */ + int op, /* The operator */ + ExprSpan *pOperand, /* The operand */ + Token *pPreOp /* The operand token for setting the span */ + ){ + pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0); + pOut->zStart = pPreOp->z; + pOut->zEnd = pOperand->zEnd; + } +/* Next is all token values, in a form suitable for use by makeheaders. +** This section will be null unless lemon is run with the -m switch. +*/ +/* +** These constants (all generated automatically by the parser generator) +** specify the various kinds of tokens (terminals) that the parser +** understands. +** +** Each symbol here is a terminal symbol in the grammar. +*/ +/* Make sure the INTERFACE macro is defined. +*/ +#ifndef INTERFACE +# define INTERFACE 1 +#endif +/* The next thing included is series of defines which control +** various aspects of the generated parser. +** YYCODETYPE is the data type used for storing terminal +** and nonterminal numbers. "unsigned char" is +** used if there are fewer than 250 terminals +** and nonterminals. "int" is used otherwise. +** YYNOCODE is a number of type YYCODETYPE which corresponds +** to no legal terminal or nonterminal number. This +** number is used to fill in empty slots of the hash +** table. +** YYFALLBACK If defined, this indicates that one or more tokens +** have fall-back values which should be used if the +** original value of the token will not parse. +** YYACTIONTYPE is the data type used for storing terminal +** and nonterminal numbers. "unsigned char" is +** used if there are fewer than 250 rules and +** states combined. "int" is used otherwise. +** sqlite3ParserTOKENTYPE is the data type used for minor tokens given +** directly to the parser from the tokenizer. +** YYMINORTYPE is the data type used for all minor tokens. +** This is typically a union of many types, one of +** which is sqlite3ParserTOKENTYPE. The entry in the union +** for base tokens is called "yy0". +** YYSTACKDEPTH is the maximum depth of the parser's stack. If +** zero the stack is dynamically sized using realloc() +** sqlite3ParserARG_SDECL A static variable declaration for the %extra_argument +** sqlite3ParserARG_PDECL A parameter declaration for the %extra_argument +** sqlite3ParserARG_STORE Code to store %extra_argument into yypParser +** sqlite3ParserARG_FETCH Code to extract %extra_argument from yypParser +** YYNSTATE the combined number of states. +** YYNRULE the number of rules in the grammar +** YYERRORSYMBOL is the code number of the error symbol. If not +** defined, then do no error processing. +*/ +#define YYCODETYPE unsigned char +#define YYNOCODE 253 +#define YYACTIONTYPE unsigned short int +#define YYWILDCARD 67 +#define sqlite3ParserTOKENTYPE Token +typedef union { + int yyinit; + sqlite3ParserTOKENTYPE yy0; + int yy4; + struct TrigEvent yy90; + ExprSpan yy118; + TriggerStep* yy203; + u8 yy210; + struct {int value; int mask;} yy215; + SrcList* yy259; + struct LimitVal yy292; + Expr* yy314; + ExprList* yy322; + struct LikeOp yy342; + IdList* yy384; + Select* yy387; +} YYMINORTYPE; +#ifndef YYSTACKDEPTH +#define YYSTACKDEPTH 100 +#endif +#define sqlite3ParserARG_SDECL Parse *pParse; +#define sqlite3ParserARG_PDECL ,Parse *pParse +#define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse +#define sqlite3ParserARG_STORE yypParser->pParse = pParse +#define YYNSTATE 630 +#define YYNRULE 329 +#define YYFALLBACK 1 +#define YY_NO_ACTION (YYNSTATE+YYNRULE+2) +#define YY_ACCEPT_ACTION (YYNSTATE+YYNRULE+1) +#define YY_ERROR_ACTION (YYNSTATE+YYNRULE) + +/* The yyzerominor constant is used to initialize instances of +** YYMINORTYPE objects to zero. */ +static const YYMINORTYPE yyzerominor = { 0 }; + +/* Define the yytestcase() macro to be a no-op if is not already defined +** otherwise. +** +** Applications can choose to define yytestcase() in the %include section +** to a macro that can assist in verifying code coverage. For production +** code the yytestcase() macro should be turned off. But it is useful +** for testing. +*/ +#ifndef yytestcase +# define yytestcase(X) +#endif + + +/* Next are the tables used to determine what action to take based on the +** current state and lookahead token. These tables are used to implement +** functions that take a state number and lookahead value and return an +** action integer. +** +** Suppose the action integer is N. Then the action is determined as +** follows +** +** 0 <= N < YYNSTATE Shift N. That is, push the lookahead +** token onto the stack and goto state N. +** +** YYNSTATE <= N < YYNSTATE+YYNRULE Reduce by rule N-YYNSTATE. +** +** N == YYNSTATE+YYNRULE A syntax error has occurred. +** +** N == YYNSTATE+YYNRULE+1 The parser accepts its input. +** +** N == YYNSTATE+YYNRULE+2 No such action. Denotes unused +** slots in the yy_action[] table. +** +** The action table is constructed as a single large table named yy_action[]. +** Given state S and lookahead X, the action is computed as +** +** yy_action[ yy_shift_ofst[S] + X ] +** +** If the index value yy_shift_ofst[S]+X is out of range or if the value +** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S] +** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table +** and that yy_default[S] should be used instead. +** +** The formula above is for computing the action when the lookahead is +** a terminal symbol. If the lookahead is a non-terminal (as occurs after +** a reduce action) then the yy_reduce_ofst[] array is used in place of +** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of +** YY_SHIFT_USE_DFLT. +** +** The following are the tables generated in this section: +** +** yy_action[] A single table containing all actions. +** yy_lookahead[] A table containing the lookahead for each entry in +** yy_action. Used to detect hash collisions. +** yy_shift_ofst[] For each state, the offset into yy_action for +** shifting terminals. +** yy_reduce_ofst[] For each state, the offset into yy_action for +** shifting non-terminals after a reduce. +** yy_default[] Default action for each state. +*/ +#define YY_ACTTAB_COUNT (1557) +static const YYACTIONTYPE yy_action[] = { + /* 0 */ 313, 960, 186, 419, 2, 172, 627, 597, 55, 55, + /* 10 */ 55, 55, 48, 53, 53, 53, 53, 52, 52, 51, + /* 20 */ 51, 51, 50, 238, 302, 283, 623, 622, 516, 515, + /* 30 */ 590, 584, 55, 55, 55, 55, 282, 53, 53, 53, + /* 40 */ 53, 52, 52, 51, 51, 51, 50, 238, 6, 56, + /* 50 */ 57, 47, 582, 581, 583, 583, 54, 54, 55, 55, + /* 60 */ 55, 55, 608, 53, 53, 53, 53, 52, 52, 51, + /* 70 */ 51, 51, 50, 238, 313, 597, 409, 330, 579, 579, + /* 80 */ 32, 53, 53, 53, 53, 52, 52, 51, 51, 51, + /* 90 */ 50, 238, 330, 217, 620, 619, 166, 411, 624, 382, + /* 100 */ 379, 378, 7, 491, 590, 584, 200, 199, 198, 58, + /* 110 */ 377, 300, 414, 621, 481, 66, 623, 622, 621, 580, + /* 120 */ 254, 601, 94, 56, 57, 47, 582, 581, 583, 583, + /* 130 */ 54, 54, 55, 55, 55, 55, 671, 53, 53, 53, + /* 140 */ 53, 52, 52, 51, 51, 51, 50, 238, 313, 532, + /* 150 */ 226, 506, 507, 133, 177, 139, 284, 385, 279, 384, + /* 160 */ 169, 197, 342, 398, 251, 226, 253, 275, 388, 167, + /* 170 */ 139, 284, 385, 279, 384, 169, 570, 236, 590, 584, + /* 180 */ 672, 240, 275, 157, 620, 619, 554, 437, 51, 51, + /* 190 */ 51, 50, 238, 343, 439, 553, 438, 56, 57, 47, + /* 200 */ 582, 581, 583, 583, 54, 54, 55, 55, 55, 55, + /* 210 */ 465, 53, 53, 53, 53, 52, 52, 51, 51, 51, + /* 220 */ 50, 238, 313, 390, 52, 52, 51, 51, 51, 50, + /* 230 */ 238, 391, 166, 491, 566, 382, 379, 378, 409, 440, + /* 240 */ 579, 579, 252, 440, 607, 66, 377, 513, 621, 49, + /* 250 */ 46, 147, 590, 584, 621, 16, 466, 189, 621, 441, + /* 260 */ 442, 673, 526, 441, 340, 577, 595, 64, 194, 482, + /* 270 */ 434, 56, 57, 47, 582, 581, 583, 583, 54, 54, + /* 280 */ 55, 55, 55, 55, 30, 53, 53, 53, 53, 52, + /* 290 */ 52, 51, 51, 51, 50, 238, 313, 593, 593, 593, + /* 300 */ 387, 578, 606, 493, 259, 351, 258, 411, 1, 623, + /* 310 */ 622, 496, 623, 622, 65, 240, 623, 622, 597, 443, + /* 320 */ 237, 239, 414, 341, 237, 602, 590, 584, 18, 603, + /* 330 */ 166, 601, 87, 382, 379, 378, 67, 623, 622, 38, + /* 340 */ 623, 622, 176, 270, 377, 56, 57, 47, 582, 581, + /* 350 */ 583, 583, 54, 54, 55, 55, 55, 55, 175, 53, + /* 360 */ 53, 53, 53, 52, 52, 51, 51, 51, 50, 238, + /* 370 */ 313, 396, 233, 411, 531, 565, 317, 620, 619, 44, + /* 380 */ 620, 619, 240, 206, 620, 619, 597, 266, 414, 268, + /* 390 */ 409, 597, 579, 579, 352, 184, 505, 601, 73, 533, + /* 400 */ 590, 584, 466, 548, 190, 620, 619, 576, 620, 619, + /* 410 */ 547, 383, 551, 35, 332, 575, 574, 600, 504, 56, + /* 420 */ 57, 47, 582, 581, 583, 583, 54, 54, 55, 55, + /* 430 */ 55, 55, 567, 53, 53, 53, 53, 52, 52, 51, + /* 440 */ 51, 51, 50, 238, 313, 411, 561, 561, 528, 364, + /* 450 */ 259, 351, 258, 183, 361, 549, 524, 374, 411, 597, + /* 460 */ 414, 240, 560, 560, 409, 604, 579, 579, 328, 601, + /* 470 */ 93, 623, 622, 414, 590, 584, 237, 564, 559, 559, + /* 480 */ 520, 402, 601, 87, 409, 210, 579, 579, 168, 421, + /* 490 */ 950, 519, 950, 56, 57, 47, 582, 581, 583, 583, + /* 500 */ 54, 54, 55, 55, 55, 55, 192, 53, 53, 53, + /* 510 */ 53, 52, 52, 51, 51, 51, 50, 238, 313, 600, + /* 520 */ 293, 563, 511, 234, 357, 146, 475, 475, 367, 411, + /* 530 */ 562, 411, 358, 542, 425, 171, 411, 215, 144, 620, + /* 540 */ 619, 544, 318, 353, 414, 203, 414, 275, 590, 584, + /* 550 */ 549, 414, 174, 601, 94, 601, 79, 558, 471, 61, + /* 560 */ 601, 79, 421, 949, 350, 949, 34, 56, 57, 47, + /* 570 */ 582, 581, 583, 583, 54, 54, 55, 55, 55, 55, + /* 580 */ 535, 53, 53, 53, 53, 52, 52, 51, 51, 51, + /* 590 */ 50, 238, 313, 307, 424, 394, 272, 49, 46, 147, + /* 600 */ 349, 322, 4, 411, 491, 312, 321, 425, 568, 492, + /* 610 */ 216, 264, 407, 575, 574, 429, 66, 549, 414, 621, + /* 620 */ 540, 602, 590, 584, 13, 603, 621, 601, 72, 12, + /* 630 */ 618, 617, 616, 202, 210, 621, 546, 469, 422, 319, + /* 640 */ 148, 56, 57, 47, 582, 581, 583, 583, 54, 54, + /* 650 */ 55, 55, 55, 55, 338, 53, 53, 53, 53, 52, + /* 660 */ 52, 51, 51, 51, 50, 238, 313, 600, 600, 411, + /* 670 */ 39, 21, 37, 170, 237, 875, 411, 572, 572, 201, + /* 680 */ 144, 473, 538, 331, 414, 474, 143, 146, 630, 628, + /* 690 */ 334, 414, 353, 601, 68, 168, 590, 584, 132, 365, + /* 700 */ 601, 96, 307, 423, 530, 336, 49, 46, 147, 568, + /* 710 */ 406, 216, 549, 360, 529, 56, 57, 47, 582, 581, + /* 720 */ 583, 583, 54, 54, 55, 55, 55, 55, 411, 53, + /* 730 */ 53, 53, 53, 52, 52, 51, 51, 51, 50, 238, + /* 740 */ 313, 411, 605, 414, 484, 510, 172, 422, 597, 318, + /* 750 */ 496, 485, 601, 99, 411, 142, 414, 411, 231, 411, + /* 760 */ 540, 411, 359, 629, 2, 601, 97, 426, 308, 414, + /* 770 */ 590, 584, 414, 20, 414, 621, 414, 621, 601, 106, + /* 780 */ 503, 601, 105, 601, 108, 601, 109, 204, 28, 56, + /* 790 */ 57, 47, 582, 581, 583, 583, 54, 54, 55, 55, + /* 800 */ 55, 55, 411, 53, 53, 53, 53, 52, 52, 51, + /* 810 */ 51, 51, 50, 238, 313, 411, 597, 414, 411, 276, + /* 820 */ 214, 600, 411, 366, 213, 381, 601, 134, 274, 500, + /* 830 */ 414, 167, 130, 414, 621, 411, 354, 414, 376, 601, + /* 840 */ 135, 129, 601, 100, 590, 584, 601, 104, 522, 521, + /* 850 */ 414, 621, 224, 273, 600, 167, 327, 282, 600, 601, + /* 860 */ 103, 468, 521, 56, 57, 47, 582, 581, 583, 583, + /* 870 */ 54, 54, 55, 55, 55, 55, 411, 53, 53, 53, + /* 880 */ 53, 52, 52, 51, 51, 51, 50, 238, 313, 411, + /* 890 */ 27, 414, 411, 375, 276, 167, 359, 544, 50, 238, + /* 900 */ 601, 95, 128, 223, 414, 411, 165, 414, 411, 621, + /* 910 */ 411, 621, 612, 601, 102, 372, 601, 76, 590, 584, + /* 920 */ 414, 570, 236, 414, 470, 414, 167, 621, 188, 601, + /* 930 */ 98, 225, 601, 138, 601, 137, 232, 56, 45, 47, + /* 940 */ 582, 581, 583, 583, 54, 54, 55, 55, 55, 55, + /* 950 */ 411, 53, 53, 53, 53, 52, 52, 51, 51, 51, + /* 960 */ 50, 238, 313, 276, 276, 414, 411, 276, 544, 459, + /* 970 */ 359, 171, 209, 479, 601, 136, 628, 334, 621, 621, + /* 980 */ 125, 414, 621, 368, 411, 621, 257, 540, 589, 588, + /* 990 */ 601, 75, 590, 584, 458, 446, 23, 23, 124, 414, + /* 1000 */ 326, 325, 621, 427, 324, 309, 600, 288, 601, 92, + /* 1010 */ 586, 585, 57, 47, 582, 581, 583, 583, 54, 54, + /* 1020 */ 55, 55, 55, 55, 411, 53, 53, 53, 53, 52, + /* 1030 */ 52, 51, 51, 51, 50, 238, 313, 587, 411, 414, + /* 1040 */ 411, 207, 611, 476, 171, 472, 160, 123, 601, 91, + /* 1050 */ 323, 261, 15, 414, 464, 414, 411, 621, 411, 354, + /* 1060 */ 222, 411, 601, 74, 601, 90, 590, 584, 159, 264, + /* 1070 */ 158, 414, 461, 414, 621, 600, 414, 121, 120, 25, + /* 1080 */ 601, 89, 601, 101, 621, 601, 88, 47, 582, 581, + /* 1090 */ 583, 583, 54, 54, 55, 55, 55, 55, 544, 53, + /* 1100 */ 53, 53, 53, 52, 52, 51, 51, 51, 50, 238, + /* 1110 */ 43, 405, 263, 3, 610, 264, 140, 415, 622, 24, + /* 1120 */ 410, 11, 456, 594, 118, 155, 219, 452, 408, 621, + /* 1130 */ 621, 621, 156, 43, 405, 621, 3, 286, 621, 113, + /* 1140 */ 415, 622, 111, 445, 411, 400, 557, 403, 545, 10, + /* 1150 */ 411, 408, 264, 110, 205, 436, 541, 566, 453, 414, + /* 1160 */ 621, 621, 63, 621, 435, 414, 411, 621, 601, 94, + /* 1170 */ 403, 621, 411, 337, 601, 86, 150, 40, 41, 534, + /* 1180 */ 566, 414, 242, 264, 42, 413, 412, 414, 600, 595, + /* 1190 */ 601, 85, 191, 333, 107, 451, 601, 84, 621, 539, + /* 1200 */ 40, 41, 420, 230, 411, 149, 316, 42, 413, 412, + /* 1210 */ 398, 127, 595, 315, 621, 399, 278, 625, 181, 414, + /* 1220 */ 593, 593, 593, 592, 591, 14, 450, 411, 601, 71, + /* 1230 */ 240, 621, 43, 405, 264, 3, 615, 180, 264, 415, + /* 1240 */ 622, 614, 414, 593, 593, 593, 592, 591, 14, 621, + /* 1250 */ 408, 601, 70, 621, 417, 33, 405, 613, 3, 411, + /* 1260 */ 264, 411, 415, 622, 418, 626, 178, 509, 8, 403, + /* 1270 */ 241, 416, 126, 408, 414, 621, 414, 449, 208, 566, + /* 1280 */ 240, 221, 621, 601, 83, 601, 82, 599, 297, 277, + /* 1290 */ 296, 30, 403, 31, 395, 264, 295, 397, 489, 40, + /* 1300 */ 41, 411, 566, 220, 621, 294, 42, 413, 412, 271, + /* 1310 */ 621, 595, 600, 621, 59, 60, 414, 269, 267, 623, + /* 1320 */ 622, 36, 40, 41, 621, 601, 81, 598, 235, 42, + /* 1330 */ 413, 412, 621, 621, 595, 265, 344, 411, 248, 556, + /* 1340 */ 173, 185, 593, 593, 593, 592, 591, 14, 218, 29, + /* 1350 */ 621, 543, 414, 305, 304, 303, 179, 301, 411, 566, + /* 1360 */ 454, 601, 80, 289, 335, 593, 593, 593, 592, 591, + /* 1370 */ 14, 411, 287, 414, 151, 392, 246, 260, 411, 196, + /* 1380 */ 195, 523, 601, 69, 411, 245, 414, 526, 537, 285, + /* 1390 */ 389, 595, 621, 414, 536, 601, 17, 362, 153, 414, + /* 1400 */ 466, 463, 601, 78, 154, 414, 462, 152, 601, 77, + /* 1410 */ 355, 255, 621, 455, 601, 9, 621, 386, 444, 517, + /* 1420 */ 247, 621, 593, 593, 593, 621, 621, 244, 621, 243, + /* 1430 */ 430, 518, 292, 621, 329, 621, 145, 393, 280, 513, + /* 1440 */ 291, 131, 621, 514, 621, 621, 311, 621, 259, 346, + /* 1450 */ 249, 621, 621, 229, 314, 621, 228, 512, 227, 240, + /* 1460 */ 494, 488, 310, 164, 487, 486, 373, 480, 163, 262, + /* 1470 */ 369, 371, 162, 26, 212, 478, 477, 161, 141, 363, + /* 1480 */ 467, 122, 339, 187, 119, 348, 347, 117, 116, 115, + /* 1490 */ 114, 112, 182, 457, 320, 22, 433, 432, 448, 19, + /* 1500 */ 609, 431, 428, 62, 193, 596, 573, 298, 555, 552, + /* 1510 */ 571, 404, 290, 380, 498, 510, 495, 306, 281, 499, + /* 1520 */ 250, 5, 497, 460, 345, 447, 569, 550, 238, 299, + /* 1530 */ 527, 525, 508, 961, 502, 501, 961, 401, 961, 211, + /* 1540 */ 490, 356, 256, 961, 483, 961, 961, 961, 961, 961, + /* 1550 */ 961, 961, 961, 961, 961, 961, 370, +}; +static const YYCODETYPE yy_lookahead[] = { + /* 0 */ 19, 142, 143, 144, 145, 24, 1, 26, 77, 78, + /* 10 */ 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + /* 20 */ 89, 90, 91, 92, 15, 98, 26, 27, 7, 8, + /* 30 */ 49, 50, 77, 78, 79, 80, 109, 82, 83, 84, + /* 40 */ 85, 86, 87, 88, 89, 90, 91, 92, 22, 68, + /* 50 */ 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, + /* 60 */ 79, 80, 23, 82, 83, 84, 85, 86, 87, 88, + /* 70 */ 89, 90, 91, 92, 19, 94, 112, 19, 114, 115, + /* 80 */ 25, 82, 83, 84, 85, 86, 87, 88, 89, 90, + /* 90 */ 91, 92, 19, 22, 94, 95, 96, 150, 150, 99, + /* 100 */ 100, 101, 76, 150, 49, 50, 105, 106, 107, 54, + /* 110 */ 110, 158, 165, 165, 161, 162, 26, 27, 165, 113, + /* 120 */ 16, 174, 175, 68, 69, 70, 71, 72, 73, 74, + /* 130 */ 75, 76, 77, 78, 79, 80, 118, 82, 83, 84, + /* 140 */ 85, 86, 87, 88, 89, 90, 91, 92, 19, 23, + /* 150 */ 92, 97, 98, 24, 96, 97, 98, 99, 100, 101, + /* 160 */ 102, 25, 97, 216, 60, 92, 62, 109, 221, 25, + /* 170 */ 97, 98, 99, 100, 101, 102, 86, 87, 49, 50, + /* 180 */ 118, 116, 109, 25, 94, 95, 32, 97, 88, 89, + /* 190 */ 90, 91, 92, 128, 104, 41, 106, 68, 69, 70, + /* 200 */ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + /* 210 */ 11, 82, 83, 84, 85, 86, 87, 88, 89, 90, + /* 220 */ 91, 92, 19, 19, 86, 87, 88, 89, 90, 91, + /* 230 */ 92, 27, 96, 150, 66, 99, 100, 101, 112, 150, + /* 240 */ 114, 115, 138, 150, 161, 162, 110, 103, 165, 222, + /* 250 */ 223, 224, 49, 50, 165, 22, 57, 24, 165, 170, + /* 260 */ 171, 118, 94, 170, 171, 23, 98, 25, 185, 186, + /* 270 */ 243, 68, 69, 70, 71, 72, 73, 74, 75, 76, + /* 280 */ 77, 78, 79, 80, 126, 82, 83, 84, 85, 86, + /* 290 */ 87, 88, 89, 90, 91, 92, 19, 129, 130, 131, + /* 300 */ 88, 23, 172, 173, 105, 106, 107, 150, 22, 26, + /* 310 */ 27, 181, 26, 27, 22, 116, 26, 27, 26, 230, + /* 320 */ 231, 197, 165, 230, 231, 113, 49, 50, 204, 117, + /* 330 */ 96, 174, 175, 99, 100, 101, 22, 26, 27, 136, + /* 340 */ 26, 27, 118, 16, 110, 68, 69, 70, 71, 72, + /* 350 */ 73, 74, 75, 76, 77, 78, 79, 80, 118, 82, + /* 360 */ 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, + /* 370 */ 19, 214, 215, 150, 23, 23, 155, 94, 95, 22, + /* 380 */ 94, 95, 116, 160, 94, 95, 94, 60, 165, 62, + /* 390 */ 112, 26, 114, 115, 128, 23, 36, 174, 175, 88, + /* 400 */ 49, 50, 57, 120, 22, 94, 95, 23, 94, 95, + /* 410 */ 120, 51, 25, 136, 169, 170, 171, 194, 58, 68, + /* 420 */ 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, + /* 430 */ 79, 80, 23, 82, 83, 84, 85, 86, 87, 88, + /* 440 */ 89, 90, 91, 92, 19, 150, 12, 12, 23, 228, + /* 450 */ 105, 106, 107, 23, 233, 25, 165, 19, 150, 94, + /* 460 */ 165, 116, 28, 28, 112, 174, 114, 115, 108, 174, + /* 470 */ 175, 26, 27, 165, 49, 50, 231, 11, 44, 44, + /* 480 */ 46, 46, 174, 175, 112, 160, 114, 115, 50, 22, + /* 490 */ 23, 57, 25, 68, 69, 70, 71, 72, 73, 74, + /* 500 */ 75, 76, 77, 78, 79, 80, 119, 82, 83, 84, + /* 510 */ 85, 86, 87, 88, 89, 90, 91, 92, 19, 194, + /* 520 */ 225, 23, 23, 215, 19, 95, 105, 106, 107, 150, + /* 530 */ 23, 150, 27, 23, 67, 25, 150, 206, 207, 94, + /* 540 */ 95, 166, 104, 218, 165, 22, 165, 109, 49, 50, + /* 550 */ 120, 165, 25, 174, 175, 174, 175, 23, 21, 234, + /* 560 */ 174, 175, 22, 23, 239, 25, 25, 68, 69, 70, + /* 570 */ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + /* 580 */ 205, 82, 83, 84, 85, 86, 87, 88, 89, 90, + /* 590 */ 91, 92, 19, 22, 23, 216, 23, 222, 223, 224, + /* 600 */ 63, 220, 35, 150, 150, 163, 220, 67, 166, 167, + /* 610 */ 168, 150, 169, 170, 171, 161, 162, 25, 165, 165, + /* 620 */ 150, 113, 49, 50, 25, 117, 165, 174, 175, 35, + /* 630 */ 7, 8, 9, 160, 160, 165, 120, 100, 67, 247, + /* 640 */ 248, 68, 69, 70, 71, 72, 73, 74, 75, 76, + /* 650 */ 77, 78, 79, 80, 193, 82, 83, 84, 85, 86, + /* 660 */ 87, 88, 89, 90, 91, 92, 19, 194, 194, 150, + /* 670 */ 135, 24, 137, 35, 231, 138, 150, 129, 130, 206, + /* 680 */ 207, 30, 27, 213, 165, 34, 118, 95, 0, 1, + /* 690 */ 2, 165, 218, 174, 175, 50, 49, 50, 22, 48, + /* 700 */ 174, 175, 22, 23, 23, 244, 222, 223, 224, 166, + /* 710 */ 167, 168, 120, 239, 23, 68, 69, 70, 71, 72, + /* 720 */ 73, 74, 75, 76, 77, 78, 79, 80, 150, 82, + /* 730 */ 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, + /* 740 */ 19, 150, 173, 165, 181, 182, 24, 67, 26, 104, + /* 750 */ 181, 188, 174, 175, 150, 39, 165, 150, 52, 150, + /* 760 */ 150, 150, 150, 144, 145, 174, 175, 249, 250, 165, + /* 770 */ 49, 50, 165, 52, 165, 165, 165, 165, 174, 175, + /* 780 */ 29, 174, 175, 174, 175, 174, 175, 160, 22, 68, + /* 790 */ 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, + /* 800 */ 79, 80, 150, 82, 83, 84, 85, 86, 87, 88, + /* 810 */ 89, 90, 91, 92, 19, 150, 94, 165, 150, 150, + /* 820 */ 160, 194, 150, 213, 160, 52, 174, 175, 23, 23, + /* 830 */ 165, 25, 22, 165, 165, 150, 150, 165, 52, 174, + /* 840 */ 175, 22, 174, 175, 49, 50, 174, 175, 190, 191, + /* 850 */ 165, 165, 240, 23, 194, 25, 187, 109, 194, 174, + /* 860 */ 175, 190, 191, 68, 69, 70, 71, 72, 73, 74, + /* 870 */ 75, 76, 77, 78, 79, 80, 150, 82, 83, 84, + /* 880 */ 85, 86, 87, 88, 89, 90, 91, 92, 19, 150, + /* 890 */ 22, 165, 150, 23, 150, 25, 150, 166, 91, 92, + /* 900 */ 174, 175, 22, 217, 165, 150, 102, 165, 150, 165, + /* 910 */ 150, 165, 150, 174, 175, 19, 174, 175, 49, 50, + /* 920 */ 165, 86, 87, 165, 23, 165, 25, 165, 24, 174, + /* 930 */ 175, 187, 174, 175, 174, 175, 205, 68, 69, 70, + /* 940 */ 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + /* 950 */ 150, 82, 83, 84, 85, 86, 87, 88, 89, 90, + /* 960 */ 91, 92, 19, 150, 150, 165, 150, 150, 166, 23, + /* 970 */ 150, 25, 160, 20, 174, 175, 1, 2, 165, 165, + /* 980 */ 104, 165, 165, 43, 150, 165, 240, 150, 49, 50, + /* 990 */ 174, 175, 49, 50, 23, 23, 25, 25, 53, 165, + /* 1000 */ 187, 187, 165, 23, 187, 25, 194, 205, 174, 175, + /* 1010 */ 71, 72, 69, 70, 71, 72, 73, 74, 75, 76, + /* 1020 */ 77, 78, 79, 80, 150, 82, 83, 84, 85, 86, + /* 1030 */ 87, 88, 89, 90, 91, 92, 19, 98, 150, 165, + /* 1040 */ 150, 160, 150, 59, 25, 53, 104, 22, 174, 175, + /* 1050 */ 213, 138, 5, 165, 1, 165, 150, 165, 150, 150, + /* 1060 */ 240, 150, 174, 175, 174, 175, 49, 50, 118, 150, + /* 1070 */ 35, 165, 27, 165, 165, 194, 165, 108, 127, 76, + /* 1080 */ 174, 175, 174, 175, 165, 174, 175, 70, 71, 72, + /* 1090 */ 73, 74, 75, 76, 77, 78, 79, 80, 166, 82, + /* 1100 */ 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, + /* 1110 */ 19, 20, 193, 22, 150, 150, 150, 26, 27, 76, + /* 1120 */ 150, 22, 1, 150, 119, 121, 217, 20, 37, 165, + /* 1130 */ 165, 165, 16, 19, 20, 165, 22, 205, 165, 119, + /* 1140 */ 26, 27, 108, 128, 150, 150, 150, 56, 150, 22, + /* 1150 */ 150, 37, 150, 127, 160, 23, 150, 66, 193, 165, + /* 1160 */ 165, 165, 16, 165, 23, 165, 150, 165, 174, 175, + /* 1170 */ 56, 165, 150, 65, 174, 175, 15, 86, 87, 88, + /* 1180 */ 66, 165, 140, 150, 93, 94, 95, 165, 194, 98, + /* 1190 */ 174, 175, 22, 3, 164, 193, 174, 175, 165, 150, + /* 1200 */ 86, 87, 4, 180, 150, 248, 251, 93, 94, 95, + /* 1210 */ 216, 180, 98, 251, 165, 221, 150, 149, 6, 165, + /* 1220 */ 129, 130, 131, 132, 133, 134, 193, 150, 174, 175, + /* 1230 */ 116, 165, 19, 20, 150, 22, 149, 151, 150, 26, + /* 1240 */ 27, 149, 165, 129, 130, 131, 132, 133, 134, 165, + /* 1250 */ 37, 174, 175, 165, 149, 19, 20, 13, 22, 150, + /* 1260 */ 150, 150, 26, 27, 146, 147, 151, 150, 25, 56, + /* 1270 */ 152, 159, 154, 37, 165, 165, 165, 193, 160, 66, + /* 1280 */ 116, 193, 165, 174, 175, 174, 175, 194, 199, 150, + /* 1290 */ 200, 126, 56, 124, 123, 150, 201, 122, 150, 86, + /* 1300 */ 87, 150, 66, 193, 165, 202, 93, 94, 95, 150, + /* 1310 */ 165, 98, 194, 165, 125, 22, 165, 150, 150, 26, + /* 1320 */ 27, 135, 86, 87, 165, 174, 175, 203, 226, 93, + /* 1330 */ 94, 95, 165, 165, 98, 150, 218, 150, 193, 157, + /* 1340 */ 118, 157, 129, 130, 131, 132, 133, 134, 5, 104, + /* 1350 */ 165, 211, 165, 10, 11, 12, 13, 14, 150, 66, + /* 1360 */ 17, 174, 175, 210, 246, 129, 130, 131, 132, 133, + /* 1370 */ 134, 150, 210, 165, 31, 121, 33, 150, 150, 86, + /* 1380 */ 87, 176, 174, 175, 150, 42, 165, 94, 211, 210, + /* 1390 */ 150, 98, 165, 165, 211, 174, 175, 150, 55, 165, + /* 1400 */ 57, 150, 174, 175, 61, 165, 150, 64, 174, 175, + /* 1410 */ 150, 150, 165, 150, 174, 175, 165, 104, 150, 184, + /* 1420 */ 150, 165, 129, 130, 131, 165, 165, 150, 165, 150, + /* 1430 */ 150, 176, 150, 165, 47, 165, 150, 150, 176, 103, + /* 1440 */ 150, 22, 165, 178, 165, 165, 179, 165, 105, 106, + /* 1450 */ 107, 165, 165, 229, 111, 165, 92, 176, 229, 116, + /* 1460 */ 184, 176, 179, 156, 176, 176, 18, 157, 156, 237, + /* 1470 */ 45, 157, 156, 135, 157, 157, 238, 156, 68, 157, + /* 1480 */ 189, 189, 139, 219, 22, 157, 18, 192, 192, 192, + /* 1490 */ 192, 189, 219, 199, 157, 242, 40, 157, 199, 242, + /* 1500 */ 153, 157, 38, 245, 196, 166, 232, 198, 177, 177, + /* 1510 */ 232, 227, 209, 178, 166, 182, 166, 148, 177, 177, + /* 1520 */ 209, 196, 177, 199, 209, 199, 166, 208, 92, 195, + /* 1530 */ 174, 174, 183, 252, 183, 183, 252, 191, 252, 235, + /* 1540 */ 186, 241, 241, 252, 186, 252, 252, 252, 252, 252, + /* 1550 */ 252, 252, 252, 252, 252, 252, 236, +}; +#define YY_SHIFT_USE_DFLT (-74) +#define YY_SHIFT_COUNT (418) +#define YY_SHIFT_MIN (-73) +#define YY_SHIFT_MAX (1468) +static const short yy_shift_ofst[] = { + /* 0 */ 975, 1114, 1343, 1114, 1213, 1213, 90, 90, 0, -19, + /* 10 */ 1213, 1213, 1213, 1213, 1213, 345, 445, 721, 1091, 1213, + /* 20 */ 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, + /* 30 */ 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, + /* 40 */ 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1236, 1213, 1213, + /* 50 */ 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, 1213, + /* 60 */ 1213, 199, 445, 445, 835, 835, 365, 1164, 55, 647, + /* 70 */ 573, 499, 425, 351, 277, 203, 129, 795, 795, 795, + /* 80 */ 795, 795, 795, 795, 795, 795, 795, 795, 795, 795, + /* 90 */ 795, 795, 795, 795, 795, 869, 795, 943, 1017, 1017, + /* 100 */ -69, -45, -45, -45, -45, -45, -1, 58, 138, 100, + /* 110 */ 445, 445, 445, 445, 445, 445, 445, 445, 445, 445, + /* 120 */ 445, 445, 445, 445, 445, 445, 537, 438, 445, 445, + /* 130 */ 445, 445, 445, 365, 807, 1436, -74, -74, -74, 1293, + /* 140 */ 73, 434, 434, 311, 314, 290, 283, 286, 540, 467, + /* 150 */ 445, 445, 445, 445, 445, 445, 445, 445, 445, 445, + /* 160 */ 445, 445, 445, 445, 445, 445, 445, 445, 445, 445, + /* 170 */ 445, 445, 445, 445, 445, 445, 445, 445, 445, 445, + /* 180 */ 445, 445, 65, 722, 722, 722, 688, 266, 1164, 1164, + /* 190 */ 1164, -74, -74, -74, 136, 168, 168, 234, 360, 360, + /* 200 */ 360, 430, 372, 435, 352, 278, 126, -36, -36, -36, + /* 210 */ -36, 421, 651, -36, -36, 592, 292, 212, 623, 158, + /* 220 */ 204, 204, 505, 158, 505, 144, 365, 154, 365, 154, + /* 230 */ 645, 154, 204, 154, 154, 535, 548, 548, 365, 387, + /* 240 */ 508, 233, 1464, 1222, 1222, 1456, 1456, 1222, 1462, 1410, + /* 250 */ 1165, 1468, 1468, 1468, 1468, 1222, 1165, 1462, 1410, 1410, + /* 260 */ 1222, 1448, 1338, 1425, 1222, 1222, 1448, 1222, 1448, 1222, + /* 270 */ 1448, 1419, 1313, 1313, 1313, 1387, 1364, 1364, 1419, 1313, + /* 280 */ 1336, 1313, 1387, 1313, 1313, 1254, 1245, 1254, 1245, 1254, + /* 290 */ 1245, 1222, 1222, 1186, 1189, 1175, 1169, 1171, 1165, 1164, + /* 300 */ 1243, 1244, 1244, 1212, 1212, 1212, 1212, -74, -74, -74, + /* 310 */ -74, -74, -74, 939, 104, 680, 571, 327, 1, 980, + /* 320 */ 26, 972, 971, 946, 901, 870, 830, 806, 54, 21, + /* 330 */ -73, 510, 242, 1198, 1190, 1170, 1042, 1161, 1108, 1146, + /* 340 */ 1141, 1132, 1015, 1127, 1026, 1034, 1020, 1107, 1004, 1116, + /* 350 */ 1121, 1005, 1099, 951, 1043, 1003, 969, 1045, 1035, 950, + /* 360 */ 1053, 1047, 1025, 942, 913, 992, 1019, 945, 984, 940, + /* 370 */ 876, 904, 953, 896, 748, 804, 880, 786, 868, 819, + /* 380 */ 805, 810, 773, 751, 766, 706, 716, 691, 681, 568, + /* 390 */ 655, 638, 676, 516, 541, 594, 599, 567, 541, 534, + /* 400 */ 507, 527, 498, 523, 466, 382, 409, 384, 357, 6, + /* 410 */ 240, 224, 143, 62, 18, 71, 39, 9, 5, +}; +#define YY_REDUCE_USE_DFLT (-142) +#define YY_REDUCE_COUNT (312) +#define YY_REDUCE_MIN (-141) +#define YY_REDUCE_MAX (1369) +static const short yy_reduce_ofst[] = { + /* 0 */ -141, 994, 1118, 223, 157, -53, 93, 89, 83, 375, + /* 10 */ 386, 381, 379, 308, 295, 325, -47, 27, 1240, 1234, + /* 20 */ 1228, 1221, 1208, 1187, 1151, 1111, 1109, 1077, 1054, 1022, + /* 30 */ 1016, 1000, 911, 908, 906, 890, 888, 874, 834, 816, + /* 40 */ 800, 760, 758, 755, 742, 739, 726, 685, 672, 668, + /* 50 */ 665, 652, 611, 609, 607, 604, 591, 578, 526, 519, + /* 60 */ 453, 474, 454, 461, 443, 245, 442, 473, 484, 484, + /* 70 */ 484, 484, 484, 484, 484, 484, 484, 484, 484, 484, + /* 80 */ 484, 484, 484, 484, 484, 484, 484, 484, 484, 484, + /* 90 */ 484, 484, 484, 484, 484, 484, 484, 484, 484, 484, + /* 100 */ 484, 484, 484, 484, 484, 484, 484, 130, 484, 484, + /* 110 */ 1145, 909, 1110, 1088, 1084, 1033, 1002, 965, 820, 837, + /* 120 */ 746, 686, 612, 817, 610, 919, 221, 563, 814, 813, + /* 130 */ 744, 669, 470, 543, 484, 484, 484, 484, 484, 291, + /* 140 */ 569, 671, 658, 970, 1290, 1287, 1286, 1282, 518, 518, + /* 150 */ 1280, 1279, 1277, 1270, 1268, 1263, 1261, 1260, 1256, 1251, + /* 160 */ 1247, 1227, 1185, 1168, 1167, 1159, 1148, 1139, 1117, 1066, + /* 170 */ 1049, 1006, 998, 996, 995, 973, 970, 966, 964, 892, + /* 180 */ 762, -52, 881, 932, 802, 731, 619, 812, 664, 660, + /* 190 */ 627, 392, 331, 124, 1358, 1357, 1356, 1354, 1352, 1351, + /* 200 */ 1349, 1319, 1334, 1346, 1334, 1334, 1334, 1334, 1334, 1334, + /* 210 */ 1334, 1320, 1304, 1334, 1334, 1319, 1360, 1325, 1369, 1326, + /* 220 */ 1315, 1311, 1301, 1324, 1300, 1335, 1350, 1345, 1348, 1342, + /* 230 */ 1333, 1341, 1303, 1332, 1331, 1284, 1278, 1274, 1339, 1309, + /* 240 */ 1308, 1347, 1258, 1344, 1340, 1257, 1253, 1337, 1273, 1302, + /* 250 */ 1299, 1298, 1297, 1296, 1295, 1328, 1294, 1264, 1292, 1291, + /* 260 */ 1322, 1321, 1238, 1232, 1318, 1317, 1316, 1314, 1312, 1310, + /* 270 */ 1307, 1283, 1289, 1288, 1285, 1276, 1229, 1224, 1267, 1281, + /* 280 */ 1265, 1262, 1235, 1255, 1205, 1183, 1179, 1177, 1162, 1140, + /* 290 */ 1153, 1184, 1182, 1102, 1124, 1103, 1095, 1090, 1089, 1093, + /* 300 */ 1112, 1115, 1086, 1105, 1092, 1087, 1068, 962, 955, 957, + /* 310 */ 1031, 1023, 1030, +}; +static const YYACTIONTYPE yy_default[] = { + /* 0 */ 635, 870, 959, 959, 959, 870, 899, 899, 959, 759, + /* 10 */ 959, 959, 959, 959, 868, 959, 959, 933, 959, 959, + /* 20 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 30 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 40 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 50 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 60 */ 959, 959, 959, 959, 899, 899, 674, 763, 794, 959, + /* 70 */ 959, 959, 959, 959, 959, 959, 959, 932, 934, 809, + /* 80 */ 808, 802, 801, 912, 774, 799, 792, 785, 796, 871, + /* 90 */ 864, 865, 863, 867, 872, 959, 795, 831, 848, 830, + /* 100 */ 842, 847, 854, 846, 843, 833, 832, 666, 834, 835, + /* 110 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 120 */ 959, 959, 959, 959, 959, 959, 661, 728, 959, 959, + /* 130 */ 959, 959, 959, 959, 836, 837, 851, 850, 849, 959, + /* 140 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 150 */ 959, 939, 937, 959, 883, 959, 959, 959, 959, 959, + /* 160 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 170 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 180 */ 959, 641, 959, 759, 759, 759, 635, 959, 959, 959, + /* 190 */ 959, 951, 763, 753, 719, 959, 959, 959, 959, 959, + /* 200 */ 959, 959, 959, 959, 959, 959, 959, 804, 742, 922, + /* 210 */ 924, 959, 905, 740, 663, 761, 676, 751, 643, 798, + /* 220 */ 776, 776, 917, 798, 917, 700, 959, 788, 959, 788, + /* 230 */ 697, 788, 776, 788, 788, 866, 959, 959, 959, 760, + /* 240 */ 751, 959, 944, 767, 767, 936, 936, 767, 810, 732, + /* 250 */ 798, 739, 739, 739, 739, 767, 798, 810, 732, 732, + /* 260 */ 767, 658, 911, 909, 767, 767, 658, 767, 658, 767, + /* 270 */ 658, 876, 730, 730, 730, 715, 880, 880, 876, 730, + /* 280 */ 700, 730, 715, 730, 730, 780, 775, 780, 775, 780, + /* 290 */ 775, 767, 767, 959, 793, 781, 791, 789, 798, 959, + /* 300 */ 718, 651, 651, 640, 640, 640, 640, 956, 956, 951, + /* 310 */ 702, 702, 684, 959, 959, 959, 959, 959, 959, 959, + /* 320 */ 885, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 330 */ 959, 959, 959, 959, 636, 946, 959, 959, 943, 959, + /* 340 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 350 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 915, + /* 360 */ 959, 959, 959, 959, 959, 959, 908, 907, 959, 959, + /* 370 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 380 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 959, + /* 390 */ 959, 959, 959, 959, 790, 959, 782, 959, 869, 959, + /* 400 */ 959, 959, 959, 959, 959, 959, 959, 959, 959, 745, + /* 410 */ 819, 959, 818, 822, 817, 668, 959, 649, 959, 632, + /* 420 */ 637, 955, 958, 957, 954, 953, 952, 947, 945, 942, + /* 430 */ 941, 940, 938, 935, 931, 889, 887, 894, 893, 892, + /* 440 */ 891, 890, 888, 886, 884, 805, 803, 800, 797, 930, + /* 450 */ 882, 741, 738, 737, 657, 948, 914, 923, 921, 811, + /* 460 */ 920, 919, 918, 916, 913, 900, 807, 806, 733, 874, + /* 470 */ 873, 660, 904, 903, 902, 906, 910, 901, 769, 659, + /* 480 */ 656, 665, 722, 721, 729, 727, 726, 725, 724, 723, + /* 490 */ 720, 667, 675, 686, 714, 699, 698, 879, 881, 878, + /* 500 */ 877, 707, 706, 712, 711, 710, 709, 708, 705, 704, + /* 510 */ 703, 696, 695, 701, 694, 717, 716, 713, 693, 736, + /* 520 */ 735, 734, 731, 692, 691, 690, 822, 689, 688, 828, + /* 530 */ 827, 815, 858, 756, 755, 754, 766, 765, 778, 777, + /* 540 */ 813, 812, 779, 764, 758, 757, 773, 772, 771, 770, + /* 550 */ 762, 752, 784, 787, 786, 783, 860, 768, 857, 929, + /* 560 */ 928, 927, 926, 925, 862, 861, 829, 826, 679, 680, + /* 570 */ 898, 896, 897, 895, 682, 681, 678, 677, 859, 747, + /* 580 */ 746, 855, 852, 844, 840, 856, 853, 845, 841, 839, + /* 590 */ 838, 824, 823, 821, 820, 816, 825, 670, 748, 744, + /* 600 */ 743, 814, 750, 749, 687, 685, 683, 664, 662, 655, + /* 610 */ 653, 652, 654, 650, 648, 647, 646, 645, 644, 673, + /* 620 */ 672, 671, 669, 668, 642, 639, 638, 634, 633, 631, +}; + +/* The next table maps tokens into fallback tokens. If a construct +** like the following: +** +** %fallback ID X Y Z. +** +** appears in the grammar, then ID becomes a fallback token for X, Y, +** and Z. Whenever one of the tokens X, Y, or Z is input to the parser +** but it does not parse, the type of the token is changed to ID and +** the parse is retried before an error is thrown. +*/ +#ifdef YYFALLBACK +static const YYCODETYPE yyFallback[] = { + 0, /* $ => nothing */ + 0, /* SEMI => nothing */ + 26, /* EXPLAIN => ID */ + 26, /* QUERY => ID */ + 26, /* PLAN => ID */ + 26, /* BEGIN => ID */ + 0, /* TRANSACTION => nothing */ + 26, /* DEFERRED => ID */ + 26, /* IMMEDIATE => ID */ + 26, /* EXCLUSIVE => ID */ + 0, /* COMMIT => nothing */ + 26, /* END => ID */ + 26, /* ROLLBACK => ID */ + 26, /* SAVEPOINT => ID */ + 26, /* RELEASE => ID */ + 0, /* TO => nothing */ + 0, /* TABLE => nothing */ + 0, /* CREATE => nothing */ + 26, /* IF => ID */ + 0, /* NOT => nothing */ + 0, /* EXISTS => nothing */ + 26, /* TEMP => ID */ + 0, /* LP => nothing */ + 0, /* RP => nothing */ + 0, /* AS => nothing */ + 0, /* COMMA => nothing */ + 0, /* ID => nothing */ + 0, /* INDEXED => nothing */ + 26, /* ABORT => ID */ + 26, /* ACTION => ID */ + 26, /* AFTER => ID */ + 26, /* ANALYZE => ID */ + 26, /* ASC => ID */ + 26, /* ATTACH => ID */ + 26, /* BEFORE => ID */ + 26, /* BY => ID */ + 26, /* CASCADE => ID */ + 26, /* CAST => ID */ + 26, /* COLUMNKW => ID */ + 26, /* CONFLICT => ID */ + 26, /* DATABASE => ID */ + 26, /* DESC => ID */ + 26, /* DETACH => ID */ + 26, /* EACH => ID */ + 26, /* FAIL => ID */ + 26, /* FOR => ID */ + 26, /* IGNORE => ID */ + 26, /* INITIALLY => ID */ + 26, /* INSTEAD => ID */ + 26, /* LIKE_KW => ID */ + 26, /* MATCH => ID */ + 26, /* NO => ID */ + 26, /* KEY => ID */ + 26, /* OF => ID */ + 26, /* OFFSET => ID */ + 26, /* PRAGMA => ID */ + 26, /* RAISE => ID */ + 26, /* REPLACE => ID */ + 26, /* RESTRICT => ID */ + 26, /* ROW => ID */ + 26, /* TRIGGER => ID */ + 26, /* VACUUM => ID */ + 26, /* VIEW => ID */ + 26, /* VIRTUAL => ID */ + 26, /* REINDEX => ID */ + 26, /* RENAME => ID */ + 26, /* CTIME_KW => ID */ +}; +#endif /* YYFALLBACK */ + +/* The following structure represents a single element of the +** parser's stack. Information stored includes: +** +** + The state number for the parser at this level of the stack. +** +** + The value of the token stored at this level of the stack. +** (In other words, the "major" token.) +** +** + The semantic value stored at this level of the stack. This is +** the information used by the action routines in the grammar. +** It is sometimes called the "minor" token. +*/ +struct yyStackEntry { + YYACTIONTYPE stateno; /* The state-number */ + YYCODETYPE major; /* The major token value. This is the code + ** number for the token at this stack level */ + YYMINORTYPE minor; /* The user-supplied minor token value. This + ** is the value of the token */ +}; +typedef struct yyStackEntry yyStackEntry; + +/* The state of the parser is completely contained in an instance of +** the following structure */ +struct yyParser { + int yyidx; /* Index of top element in stack */ +#ifdef YYTRACKMAXSTACKDEPTH + int yyidxMax; /* Maximum value of yyidx */ +#endif + int yyerrcnt; /* Shifts left before out of the error */ + sqlite3ParserARG_SDECL /* A place to hold %extra_argument */ +#if YYSTACKDEPTH<=0 + int yystksz; /* Current side of the stack */ + yyStackEntry *yystack; /* The parser's stack */ +#else + yyStackEntry yystack[YYSTACKDEPTH]; /* The parser's stack */ +#endif +}; +typedef struct yyParser yyParser; + +#ifndef NDEBUG +static FILE *yyTraceFILE = 0; +static char *yyTracePrompt = 0; +#endif /* NDEBUG */ + +#ifndef NDEBUG +/* +** Turn parser tracing on by giving a stream to which to write the trace +** and a prompt to preface each trace message. Tracing is turned off +** by making either argument NULL +** +** Inputs: +**
      +**
    • A FILE* to which trace output should be written. +** If NULL, then tracing is turned off. +**
    • A prefix string written at the beginning of every +** line of trace output. If NULL, then tracing is +** turned off. +**
    +** +** Outputs: +** None. +*/ +SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){ + yyTraceFILE = TraceFILE; + yyTracePrompt = zTracePrompt; + if( yyTraceFILE==0 ) yyTracePrompt = 0; + else if( yyTracePrompt==0 ) yyTraceFILE = 0; +} +#endif /* NDEBUG */ + +#ifndef NDEBUG +/* For tracing shifts, the names of all terminals and nonterminals +** are required. The following table supplies these names */ +static const char *const yyTokenName[] = { + "$", "SEMI", "EXPLAIN", "QUERY", + "PLAN", "BEGIN", "TRANSACTION", "DEFERRED", + "IMMEDIATE", "EXCLUSIVE", "COMMIT", "END", + "ROLLBACK", "SAVEPOINT", "RELEASE", "TO", + "TABLE", "CREATE", "IF", "NOT", + "EXISTS", "TEMP", "LP", "RP", + "AS", "COMMA", "ID", "INDEXED", + "ABORT", "ACTION", "AFTER", "ANALYZE", + "ASC", "ATTACH", "BEFORE", "BY", + "CASCADE", "CAST", "COLUMNKW", "CONFLICT", + "DATABASE", "DESC", "DETACH", "EACH", + "FAIL", "FOR", "IGNORE", "INITIALLY", + "INSTEAD", "LIKE_KW", "MATCH", "NO", + "KEY", "OF", "OFFSET", "PRAGMA", + "RAISE", "REPLACE", "RESTRICT", "ROW", + "TRIGGER", "VACUUM", "VIEW", "VIRTUAL", + "REINDEX", "RENAME", "CTIME_KW", "ANY", + "OR", "AND", "IS", "BETWEEN", + "IN", "ISNULL", "NOTNULL", "NE", + "EQ", "GT", "LE", "LT", + "GE", "ESCAPE", "BITAND", "BITOR", + "LSHIFT", "RSHIFT", "PLUS", "MINUS", + "STAR", "SLASH", "REM", "CONCAT", + "COLLATE", "BITNOT", "STRING", "JOIN_KW", + "CONSTRAINT", "DEFAULT", "NULL", "PRIMARY", + "UNIQUE", "CHECK", "REFERENCES", "AUTOINCR", + "ON", "INSERT", "DELETE", "UPDATE", + "SET", "DEFERRABLE", "FOREIGN", "DROP", + "UNION", "ALL", "EXCEPT", "INTERSECT", + "SELECT", "DISTINCT", "DOT", "FROM", + "JOIN", "USING", "ORDER", "GROUP", + "HAVING", "LIMIT", "WHERE", "INTO", + "VALUES", "INTEGER", "FLOAT", "BLOB", + "REGISTER", "VARIABLE", "CASE", "WHEN", + "THEN", "ELSE", "INDEX", "ALTER", + "ADD", "error", "input", "cmdlist", + "ecmd", "explain", "cmdx", "cmd", + "transtype", "trans_opt", "nm", "savepoint_opt", + "create_table", "create_table_args", "createkw", "temp", + "ifnotexists", "dbnm", "columnlist", "conslist_opt", + "select", "column", "columnid", "type", + "carglist", "id", "ids", "typetoken", + "typename", "signed", "plus_num", "minus_num", + "carg", "ccons", "term", "expr", + "onconf", "sortorder", "autoinc", "idxlist_opt", + "refargs", "defer_subclause", "refarg", "refact", + "init_deferred_pred_opt", "conslist", "tcons", "idxlist", + "defer_subclause_opt", "orconf", "resolvetype", "raisetype", + "ifexists", "fullname", "oneselect", "multiselect_op", + "distinct", "selcollist", "from", "where_opt", + "groupby_opt", "having_opt", "orderby_opt", "limit_opt", + "sclp", "as", "seltablist", "stl_prefix", + "joinop", "indexed_opt", "on_opt", "using_opt", + "joinop2", "inscollist", "sortlist", "sortitem", + "nexprlist", "setlist", "insert_cmd", "inscollist_opt", + "itemlist", "exprlist", "likeop", "between_op", + "in_op", "case_operand", "case_exprlist", "case_else", + "uniqueflag", "collate", "nmnum", "plus_opt", + "number", "trigger_decl", "trigger_cmd_list", "trigger_time", + "trigger_event", "foreach_clause", "when_clause", "trigger_cmd", + "trnm", "tridxby", "database_kw_opt", "key_opt", + "add_column_fullname", "kwcolumn_opt", "create_vtab", "vtabarglist", + "vtabarg", "vtabargtoken", "lp", "anylist", +}; +#endif /* NDEBUG */ + +#ifndef NDEBUG +/* For tracing reduce actions, the names of all rules are required. +*/ +static const char *const yyRuleName[] = { + /* 0 */ "input ::= cmdlist", + /* 1 */ "cmdlist ::= cmdlist ecmd", + /* 2 */ "cmdlist ::= ecmd", + /* 3 */ "ecmd ::= SEMI", + /* 4 */ "ecmd ::= explain cmdx SEMI", + /* 5 */ "explain ::=", + /* 6 */ "explain ::= EXPLAIN", + /* 7 */ "explain ::= EXPLAIN QUERY PLAN", + /* 8 */ "cmdx ::= cmd", + /* 9 */ "cmd ::= BEGIN transtype trans_opt", + /* 10 */ "trans_opt ::=", + /* 11 */ "trans_opt ::= TRANSACTION", + /* 12 */ "trans_opt ::= TRANSACTION nm", + /* 13 */ "transtype ::=", + /* 14 */ "transtype ::= DEFERRED", + /* 15 */ "transtype ::= IMMEDIATE", + /* 16 */ "transtype ::= EXCLUSIVE", + /* 17 */ "cmd ::= COMMIT trans_opt", + /* 18 */ "cmd ::= END trans_opt", + /* 19 */ "cmd ::= ROLLBACK trans_opt", + /* 20 */ "savepoint_opt ::= SAVEPOINT", + /* 21 */ "savepoint_opt ::=", + /* 22 */ "cmd ::= SAVEPOINT nm", + /* 23 */ "cmd ::= RELEASE savepoint_opt nm", + /* 24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm", + /* 25 */ "cmd ::= create_table create_table_args", + /* 26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm", + /* 27 */ "createkw ::= CREATE", + /* 28 */ "ifnotexists ::=", + /* 29 */ "ifnotexists ::= IF NOT EXISTS", + /* 30 */ "temp ::= TEMP", + /* 31 */ "temp ::=", + /* 32 */ "create_table_args ::= LP columnlist conslist_opt RP", + /* 33 */ "create_table_args ::= AS select", + /* 34 */ "columnlist ::= columnlist COMMA column", + /* 35 */ "columnlist ::= column", + /* 36 */ "column ::= columnid type carglist", + /* 37 */ "columnid ::= nm", + /* 38 */ "id ::= ID", + /* 39 */ "id ::= INDEXED", + /* 40 */ "ids ::= ID|STRING", + /* 41 */ "nm ::= id", + /* 42 */ "nm ::= STRING", + /* 43 */ "nm ::= JOIN_KW", + /* 44 */ "type ::=", + /* 45 */ "type ::= typetoken", + /* 46 */ "typetoken ::= typename", + /* 47 */ "typetoken ::= typename LP signed RP", + /* 48 */ "typetoken ::= typename LP signed COMMA signed RP", + /* 49 */ "typename ::= ids", + /* 50 */ "typename ::= typename ids", + /* 51 */ "signed ::= plus_num", + /* 52 */ "signed ::= minus_num", + /* 53 */ "carglist ::= carglist carg", + /* 54 */ "carglist ::=", + /* 55 */ "carg ::= CONSTRAINT nm ccons", + /* 56 */ "carg ::= ccons", + /* 57 */ "ccons ::= DEFAULT term", + /* 58 */ "ccons ::= DEFAULT LP expr RP", + /* 59 */ "ccons ::= DEFAULT PLUS term", + /* 60 */ "ccons ::= DEFAULT MINUS term", + /* 61 */ "ccons ::= DEFAULT id", + /* 62 */ "ccons ::= NULL onconf", + /* 63 */ "ccons ::= NOT NULL onconf", + /* 64 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc", + /* 65 */ "ccons ::= UNIQUE onconf", + /* 66 */ "ccons ::= CHECK LP expr RP", + /* 67 */ "ccons ::= REFERENCES nm idxlist_opt refargs", + /* 68 */ "ccons ::= defer_subclause", + /* 69 */ "ccons ::= COLLATE ids", + /* 70 */ "autoinc ::=", + /* 71 */ "autoinc ::= AUTOINCR", + /* 72 */ "refargs ::=", + /* 73 */ "refargs ::= refargs refarg", + /* 74 */ "refarg ::= MATCH nm", + /* 75 */ "refarg ::= ON INSERT refact", + /* 76 */ "refarg ::= ON DELETE refact", + /* 77 */ "refarg ::= ON UPDATE refact", + /* 78 */ "refact ::= SET NULL", + /* 79 */ "refact ::= SET DEFAULT", + /* 80 */ "refact ::= CASCADE", + /* 81 */ "refact ::= RESTRICT", + /* 82 */ "refact ::= NO ACTION", + /* 83 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt", + /* 84 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt", + /* 85 */ "init_deferred_pred_opt ::=", + /* 86 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED", + /* 87 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE", + /* 88 */ "conslist_opt ::=", + /* 89 */ "conslist_opt ::= COMMA conslist", + /* 90 */ "conslist ::= conslist COMMA tcons", + /* 91 */ "conslist ::= conslist tcons", + /* 92 */ "conslist ::= tcons", + /* 93 */ "tcons ::= CONSTRAINT nm", + /* 94 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf", + /* 95 */ "tcons ::= UNIQUE LP idxlist RP onconf", + /* 96 */ "tcons ::= CHECK LP expr RP onconf", + /* 97 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt", + /* 98 */ "defer_subclause_opt ::=", + /* 99 */ "defer_subclause_opt ::= defer_subclause", + /* 100 */ "onconf ::=", + /* 101 */ "onconf ::= ON CONFLICT resolvetype", + /* 102 */ "orconf ::=", + /* 103 */ "orconf ::= OR resolvetype", + /* 104 */ "resolvetype ::= raisetype", + /* 105 */ "resolvetype ::= IGNORE", + /* 106 */ "resolvetype ::= REPLACE", + /* 107 */ "cmd ::= DROP TABLE ifexists fullname", + /* 108 */ "ifexists ::= IF EXISTS", + /* 109 */ "ifexists ::=", + /* 110 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select", + /* 111 */ "cmd ::= DROP VIEW ifexists fullname", + /* 112 */ "cmd ::= select", + /* 113 */ "select ::= oneselect", + /* 114 */ "select ::= select multiselect_op oneselect", + /* 115 */ "multiselect_op ::= UNION", + /* 116 */ "multiselect_op ::= UNION ALL", + /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT", + /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt", + /* 119 */ "distinct ::= DISTINCT", + /* 120 */ "distinct ::= ALL", + /* 121 */ "distinct ::=", + /* 122 */ "sclp ::= selcollist COMMA", + /* 123 */ "sclp ::=", + /* 124 */ "selcollist ::= sclp expr as", + /* 125 */ "selcollist ::= sclp STAR", + /* 126 */ "selcollist ::= sclp nm DOT STAR", + /* 127 */ "as ::= AS nm", + /* 128 */ "as ::= ids", + /* 129 */ "as ::=", + /* 130 */ "from ::=", + /* 131 */ "from ::= FROM seltablist", + /* 132 */ "stl_prefix ::= seltablist joinop", + /* 133 */ "stl_prefix ::=", + /* 134 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt", + /* 135 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt", + /* 136 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt", + /* 137 */ "dbnm ::=", + /* 138 */ "dbnm ::= DOT nm", + /* 139 */ "fullname ::= nm dbnm", + /* 140 */ "joinop ::= COMMA|JOIN", + /* 141 */ "joinop ::= JOIN_KW JOIN", + /* 142 */ "joinop ::= JOIN_KW nm JOIN", + /* 143 */ "joinop ::= JOIN_KW nm nm JOIN", + /* 144 */ "on_opt ::= ON expr", + /* 145 */ "on_opt ::=", + /* 146 */ "indexed_opt ::=", + /* 147 */ "indexed_opt ::= INDEXED BY nm", + /* 148 */ "indexed_opt ::= NOT INDEXED", + /* 149 */ "using_opt ::= USING LP inscollist RP", + /* 150 */ "using_opt ::=", + /* 151 */ "orderby_opt ::=", + /* 152 */ "orderby_opt ::= ORDER BY sortlist", + /* 153 */ "sortlist ::= sortlist COMMA sortitem sortorder", + /* 154 */ "sortlist ::= sortitem sortorder", + /* 155 */ "sortitem ::= expr", + /* 156 */ "sortorder ::= ASC", + /* 157 */ "sortorder ::= DESC", + /* 158 */ "sortorder ::=", + /* 159 */ "groupby_opt ::=", + /* 160 */ "groupby_opt ::= GROUP BY nexprlist", + /* 161 */ "having_opt ::=", + /* 162 */ "having_opt ::= HAVING expr", + /* 163 */ "limit_opt ::=", + /* 164 */ "limit_opt ::= LIMIT expr", + /* 165 */ "limit_opt ::= LIMIT expr OFFSET expr", + /* 166 */ "limit_opt ::= LIMIT expr COMMA expr", + /* 167 */ "cmd ::= DELETE FROM fullname indexed_opt where_opt", + /* 168 */ "where_opt ::=", + /* 169 */ "where_opt ::= WHERE expr", + /* 170 */ "cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt", + /* 171 */ "setlist ::= setlist COMMA nm EQ expr", + /* 172 */ "setlist ::= nm EQ expr", + /* 173 */ "cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP", + /* 174 */ "cmd ::= insert_cmd INTO fullname inscollist_opt select", + /* 175 */ "cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES", + /* 176 */ "insert_cmd ::= INSERT orconf", + /* 177 */ "insert_cmd ::= REPLACE", + /* 178 */ "itemlist ::= itemlist COMMA expr", + /* 179 */ "itemlist ::= expr", + /* 180 */ "inscollist_opt ::=", + /* 181 */ "inscollist_opt ::= LP inscollist RP", + /* 182 */ "inscollist ::= inscollist COMMA nm", + /* 183 */ "inscollist ::= nm", + /* 184 */ "expr ::= term", + /* 185 */ "expr ::= LP expr RP", + /* 186 */ "term ::= NULL", + /* 187 */ "expr ::= id", + /* 188 */ "expr ::= JOIN_KW", + /* 189 */ "expr ::= nm DOT nm", + /* 190 */ "expr ::= nm DOT nm DOT nm", + /* 191 */ "term ::= INTEGER|FLOAT|BLOB", + /* 192 */ "term ::= STRING", + /* 193 */ "expr ::= REGISTER", + /* 194 */ "expr ::= VARIABLE", + /* 195 */ "expr ::= expr COLLATE ids", + /* 196 */ "expr ::= CAST LP expr AS typetoken RP", + /* 197 */ "expr ::= ID LP distinct exprlist RP", + /* 198 */ "expr ::= ID LP STAR RP", + /* 199 */ "term ::= CTIME_KW", + /* 200 */ "expr ::= expr AND expr", + /* 201 */ "expr ::= expr OR expr", + /* 202 */ "expr ::= expr LT|GT|GE|LE expr", + /* 203 */ "expr ::= expr EQ|NE expr", + /* 204 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr", + /* 205 */ "expr ::= expr PLUS|MINUS expr", + /* 206 */ "expr ::= expr STAR|SLASH|REM expr", + /* 207 */ "expr ::= expr CONCAT expr", + /* 208 */ "likeop ::= LIKE_KW", + /* 209 */ "likeop ::= NOT LIKE_KW", + /* 210 */ "likeop ::= MATCH", + /* 211 */ "likeop ::= NOT MATCH", + /* 212 */ "expr ::= expr likeop expr", + /* 213 */ "expr ::= expr likeop expr ESCAPE expr", + /* 214 */ "expr ::= expr ISNULL|NOTNULL", + /* 215 */ "expr ::= expr NOT NULL", + /* 216 */ "expr ::= expr IS expr", + /* 217 */ "expr ::= expr IS NOT expr", + /* 218 */ "expr ::= NOT expr", + /* 219 */ "expr ::= BITNOT expr", + /* 220 */ "expr ::= MINUS expr", + /* 221 */ "expr ::= PLUS expr", + /* 222 */ "between_op ::= BETWEEN", + /* 223 */ "between_op ::= NOT BETWEEN", + /* 224 */ "expr ::= expr between_op expr AND expr", + /* 225 */ "in_op ::= IN", + /* 226 */ "in_op ::= NOT IN", + /* 227 */ "expr ::= expr in_op LP exprlist RP", + /* 228 */ "expr ::= LP select RP", + /* 229 */ "expr ::= expr in_op LP select RP", + /* 230 */ "expr ::= expr in_op nm dbnm", + /* 231 */ "expr ::= EXISTS LP select RP", + /* 232 */ "expr ::= CASE case_operand case_exprlist case_else END", + /* 233 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr", + /* 234 */ "case_exprlist ::= WHEN expr THEN expr", + /* 235 */ "case_else ::= ELSE expr", + /* 236 */ "case_else ::=", + /* 237 */ "case_operand ::= expr", + /* 238 */ "case_operand ::=", + /* 239 */ "exprlist ::= nexprlist", + /* 240 */ "exprlist ::=", + /* 241 */ "nexprlist ::= nexprlist COMMA expr", + /* 242 */ "nexprlist ::= expr", + /* 243 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP", + /* 244 */ "uniqueflag ::= UNIQUE", + /* 245 */ "uniqueflag ::=", + /* 246 */ "idxlist_opt ::=", + /* 247 */ "idxlist_opt ::= LP idxlist RP", + /* 248 */ "idxlist ::= idxlist COMMA nm collate sortorder", + /* 249 */ "idxlist ::= nm collate sortorder", + /* 250 */ "collate ::=", + /* 251 */ "collate ::= COLLATE ids", + /* 252 */ "cmd ::= DROP INDEX ifexists fullname", + /* 253 */ "cmd ::= VACUUM", + /* 254 */ "cmd ::= VACUUM nm", + /* 255 */ "cmd ::= PRAGMA nm dbnm", + /* 256 */ "cmd ::= PRAGMA nm dbnm EQ nmnum", + /* 257 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP", + /* 258 */ "cmd ::= PRAGMA nm dbnm EQ minus_num", + /* 259 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP", + /* 260 */ "nmnum ::= plus_num", + /* 261 */ "nmnum ::= nm", + /* 262 */ "nmnum ::= ON", + /* 263 */ "nmnum ::= DELETE", + /* 264 */ "nmnum ::= DEFAULT", + /* 265 */ "plus_num ::= plus_opt number", + /* 266 */ "minus_num ::= MINUS number", + /* 267 */ "number ::= INTEGER|FLOAT", + /* 268 */ "plus_opt ::= PLUS", + /* 269 */ "plus_opt ::=", + /* 270 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END", + /* 271 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause", + /* 272 */ "trigger_time ::= BEFORE", + /* 273 */ "trigger_time ::= AFTER", + /* 274 */ "trigger_time ::= INSTEAD OF", + /* 275 */ "trigger_time ::=", + /* 276 */ "trigger_event ::= DELETE|INSERT", + /* 277 */ "trigger_event ::= UPDATE", + /* 278 */ "trigger_event ::= UPDATE OF inscollist", + /* 279 */ "foreach_clause ::=", + /* 280 */ "foreach_clause ::= FOR EACH ROW", + /* 281 */ "when_clause ::=", + /* 282 */ "when_clause ::= WHEN expr", + /* 283 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI", + /* 284 */ "trigger_cmd_list ::= trigger_cmd SEMI", + /* 285 */ "trnm ::= nm", + /* 286 */ "trnm ::= nm DOT nm", + /* 287 */ "tridxby ::=", + /* 288 */ "tridxby ::= INDEXED BY nm", + /* 289 */ "tridxby ::= NOT INDEXED", + /* 290 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt", + /* 291 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP", + /* 292 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select", + /* 293 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt", + /* 294 */ "trigger_cmd ::= select", + /* 295 */ "expr ::= RAISE LP IGNORE RP", + /* 296 */ "expr ::= RAISE LP raisetype COMMA nm RP", + /* 297 */ "raisetype ::= ROLLBACK", + /* 298 */ "raisetype ::= ABORT", + /* 299 */ "raisetype ::= FAIL", + /* 300 */ "cmd ::= DROP TRIGGER ifexists fullname", + /* 301 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt", + /* 302 */ "cmd ::= DETACH database_kw_opt expr", + /* 303 */ "key_opt ::=", + /* 304 */ "key_opt ::= KEY expr", + /* 305 */ "database_kw_opt ::= DATABASE", + /* 306 */ "database_kw_opt ::=", + /* 307 */ "cmd ::= REINDEX", + /* 308 */ "cmd ::= REINDEX nm dbnm", + /* 309 */ "cmd ::= ANALYZE", + /* 310 */ "cmd ::= ANALYZE nm dbnm", + /* 311 */ "cmd ::= ALTER TABLE fullname RENAME TO nm", + /* 312 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column", + /* 313 */ "add_column_fullname ::= fullname", + /* 314 */ "kwcolumn_opt ::=", + /* 315 */ "kwcolumn_opt ::= COLUMNKW", + /* 316 */ "cmd ::= create_vtab", + /* 317 */ "cmd ::= create_vtab LP vtabarglist RP", + /* 318 */ "create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm", + /* 319 */ "vtabarglist ::= vtabarg", + /* 320 */ "vtabarglist ::= vtabarglist COMMA vtabarg", + /* 321 */ "vtabarg ::=", + /* 322 */ "vtabarg ::= vtabarg vtabargtoken", + /* 323 */ "vtabargtoken ::= ANY", + /* 324 */ "vtabargtoken ::= lp anylist RP", + /* 325 */ "lp ::= LP", + /* 326 */ "anylist ::=", + /* 327 */ "anylist ::= anylist LP anylist RP", + /* 328 */ "anylist ::= anylist ANY", +}; +#endif /* NDEBUG */ + + +#if YYSTACKDEPTH<=0 +/* +** Try to increase the size of the parser stack. +*/ +static void yyGrowStack(yyParser *p){ + int newSize; + yyStackEntry *pNew; + + newSize = p->yystksz*2 + 100; + pNew = realloc(p->yystack, newSize*sizeof(pNew[0])); + if( pNew ){ + p->yystack = pNew; + p->yystksz = newSize; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sStack grows to %d entries!\n", + yyTracePrompt, p->yystksz); + } +#endif + } +} +#endif + +/* +** This function allocates a new parser. +** The only argument is a pointer to a function which works like +** malloc. +** +** Inputs: +** A pointer to the function used to allocate memory. +** +** Outputs: +** A pointer to a parser. This pointer is used in subsequent calls +** to sqlite3Parser and sqlite3ParserFree. +*/ +SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){ + yyParser *pParser; + pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) ); + if( pParser ){ + pParser->yyidx = -1; +#ifdef YYTRACKMAXSTACKDEPTH + pParser->yyidxMax = 0; +#endif +#if YYSTACKDEPTH<=0 + pParser->yystack = NULL; + pParser->yystksz = 0; + yyGrowStack(pParser); +#endif + } + return pParser; +} + +/* The following function deletes the value associated with a +** symbol. The symbol can be either a terminal or nonterminal. +** "yymajor" is the symbol code, and "yypminor" is a pointer to +** the value. +*/ +static void yy_destructor( + yyParser *yypParser, /* The parser */ + YYCODETYPE yymajor, /* Type code for object to destroy */ + YYMINORTYPE *yypminor /* The object to be destroyed */ +){ + sqlite3ParserARG_FETCH; + switch( yymajor ){ + /* Here is inserted the actions which take place when a + ** terminal or non-terminal is destroyed. This can happen + ** when the symbol is popped from the stack during a + ** reduce or during error processing or when a parser is + ** being destroyed before it is finished parsing. + ** + ** Note: during a reduce, the only symbols destroyed are those + ** which appear on the RHS of the rule, but which are not used + ** inside the C code. + */ + case 160: /* select */ + case 194: /* oneselect */ +{ +sqlite3SelectDelete(pParse->db, (yypminor->yy387)); +} + break; + case 174: /* term */ + case 175: /* expr */ +{ +sqlite3ExprDelete(pParse->db, (yypminor->yy118).pExpr); +} + break; + case 179: /* idxlist_opt */ + case 187: /* idxlist */ + case 197: /* selcollist */ + case 200: /* groupby_opt */ + case 202: /* orderby_opt */ + case 204: /* sclp */ + case 214: /* sortlist */ + case 216: /* nexprlist */ + case 217: /* setlist */ + case 220: /* itemlist */ + case 221: /* exprlist */ + case 226: /* case_exprlist */ +{ +sqlite3ExprListDelete(pParse->db, (yypminor->yy322)); +} + break; + case 193: /* fullname */ + case 198: /* from */ + case 206: /* seltablist */ + case 207: /* stl_prefix */ +{ +sqlite3SrcListDelete(pParse->db, (yypminor->yy259)); +} + break; + case 199: /* where_opt */ + case 201: /* having_opt */ + case 210: /* on_opt */ + case 215: /* sortitem */ + case 225: /* case_operand */ + case 227: /* case_else */ + case 238: /* when_clause */ + case 243: /* key_opt */ +{ +sqlite3ExprDelete(pParse->db, (yypminor->yy314)); +} + break; + case 211: /* using_opt */ + case 213: /* inscollist */ + case 219: /* inscollist_opt */ +{ +sqlite3IdListDelete(pParse->db, (yypminor->yy384)); +} + break; + case 234: /* trigger_cmd_list */ + case 239: /* trigger_cmd */ +{ +sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy203)); +} + break; + case 236: /* trigger_event */ +{ +sqlite3IdListDelete(pParse->db, (yypminor->yy90).b); +} + break; + default: break; /* If no destructor action specified: do nothing */ + } +} + +/* +** Pop the parser's stack once. +** +** If there is a destructor routine associated with the token which +** is popped from the stack, then call it. +** +** Return the major token number for the symbol popped. +*/ +static int yy_pop_parser_stack(yyParser *pParser){ + YYCODETYPE yymajor; + yyStackEntry *yytos = &pParser->yystack[pParser->yyidx]; + + /* There is no mechanism by which the parser stack can be popped below + ** empty in SQLite. */ + if( NEVER(pParser->yyidx<0) ) return 0; +#ifndef NDEBUG + if( yyTraceFILE && pParser->yyidx>=0 ){ + fprintf(yyTraceFILE,"%sPopping %s\n", + yyTracePrompt, + yyTokenName[yytos->major]); + } +#endif + yymajor = yytos->major; + yy_destructor(pParser, yymajor, &yytos->minor); + pParser->yyidx--; + return yymajor; +} + +/* +** Deallocate and destroy a parser. Destructors are all called for +** all stack elements before shutting the parser down. +** +** Inputs: +**
      +**
    • A pointer to the parser. This should be a pointer +** obtained from sqlite3ParserAlloc. +**
    • A pointer to a function used to reclaim memory obtained +** from malloc. +**
    +*/ +SQLITE_PRIVATE void sqlite3ParserFree( + void *p, /* The parser to be deleted */ + void (*freeProc)(void*) /* Function used to reclaim memory */ +){ + yyParser *pParser = (yyParser*)p; + /* In SQLite, we never try to destroy a parser that was not successfully + ** created in the first place. */ + if( NEVER(pParser==0) ) return; + while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser); +#if YYSTACKDEPTH<=0 + free(pParser->yystack); +#endif + (*freeProc)((void*)pParser); +} + +/* +** Return the peak depth of the stack for a parser. +*/ +#ifdef YYTRACKMAXSTACKDEPTH +SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){ + yyParser *pParser = (yyParser*)p; + return pParser->yyidxMax; +} +#endif + +/* +** Find the appropriate action for a parser given the terminal +** look-ahead token iLookAhead. +** +** If the look-ahead token is YYNOCODE, then check to see if the action is +** independent of the look-ahead. If it is, return the action, otherwise +** return YY_NO_ACTION. +*/ +static int yy_find_shift_action( + yyParser *pParser, /* The parser */ + YYCODETYPE iLookAhead /* The look-ahead token */ +){ + int i; + int stateno = pParser->yystack[pParser->yyidx].stateno; + + if( stateno>YY_SHIFT_COUNT + || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){ + return yy_default[stateno]; + } + assert( iLookAhead!=YYNOCODE ); + i += iLookAhead; + if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ + if( iLookAhead>0 ){ +#ifdef YYFALLBACK + YYCODETYPE iFallback; /* Fallback token */ + if( iLookAhead %s\n", + yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]); + } +#endif + return yy_find_shift_action(pParser, iFallback); + } +#endif +#ifdef YYWILDCARD + { + int j = i - iLookAhead + YYWILDCARD; + if( +#if YY_SHIFT_MIN+YYWILDCARD<0 + j>=0 && +#endif +#if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT + j %s\n", + yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]); + } +#endif /* NDEBUG */ + return yy_action[j]; + } + } +#endif /* YYWILDCARD */ + } + return yy_default[stateno]; + }else{ + return yy_action[i]; + } +} + +/* +** Find the appropriate action for a parser given the non-terminal +** look-ahead token iLookAhead. +** +** If the look-ahead token is YYNOCODE, then check to see if the action is +** independent of the look-ahead. If it is, return the action, otherwise +** return YY_NO_ACTION. +*/ +static int yy_find_reduce_action( + int stateno, /* Current state number */ + YYCODETYPE iLookAhead /* The look-ahead token */ +){ + int i; +#ifdef YYERRORSYMBOL + if( stateno>YY_REDUCE_COUNT ){ + return yy_default[stateno]; + } +#else + assert( stateno<=YY_REDUCE_COUNT ); +#endif + i = yy_reduce_ofst[stateno]; + assert( i!=YY_REDUCE_USE_DFLT ); + assert( iLookAhead!=YYNOCODE ); + i += iLookAhead; +#ifdef YYERRORSYMBOL + if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){ + return yy_default[stateno]; + } +#else + assert( i>=0 && iyyidx--; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt); + } +#endif + while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); + /* Here code is inserted which will execute if the parser + ** stack every overflows */ + + UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */ + sqlite3ErrorMsg(pParse, "parser stack overflow"); + pParse->parseError = 1; + sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */ +} + +/* +** Perform a shift action. +*/ +static void yy_shift( + yyParser *yypParser, /* The parser to be shifted */ + int yyNewState, /* The new state to shift in */ + int yyMajor, /* The major token to shift in */ + YYMINORTYPE *yypMinor /* Pointer to the minor token to shift in */ +){ + yyStackEntry *yytos; + yypParser->yyidx++; +#ifdef YYTRACKMAXSTACKDEPTH + if( yypParser->yyidx>yypParser->yyidxMax ){ + yypParser->yyidxMax = yypParser->yyidx; + } +#endif +#if YYSTACKDEPTH>0 + if( yypParser->yyidx>=YYSTACKDEPTH ){ + yyStackOverflow(yypParser, yypMinor); + return; + } +#else + if( yypParser->yyidx>=yypParser->yystksz ){ + yyGrowStack(yypParser); + if( yypParser->yyidx>=yypParser->yystksz ){ + yyStackOverflow(yypParser, yypMinor); + return; + } + } +#endif + yytos = &yypParser->yystack[yypParser->yyidx]; + yytos->stateno = (YYACTIONTYPE)yyNewState; + yytos->major = (YYCODETYPE)yyMajor; + yytos->minor = *yypMinor; +#ifndef NDEBUG + if( yyTraceFILE && yypParser->yyidx>0 ){ + int i; + fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState); + fprintf(yyTraceFILE,"%sStack:",yyTracePrompt); + for(i=1; i<=yypParser->yyidx; i++) + fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]); + fprintf(yyTraceFILE,"\n"); + } +#endif +} + +/* The following table contains information about every rule that +** is used during the reduce. +*/ +static const struct { + YYCODETYPE lhs; /* Symbol on the left-hand side of the rule */ + unsigned char nrhs; /* Number of right-hand side symbols in the rule */ +} yyRuleInfo[] = { + { 142, 1 }, + { 143, 2 }, + { 143, 1 }, + { 144, 1 }, + { 144, 3 }, + { 145, 0 }, + { 145, 1 }, + { 145, 3 }, + { 146, 1 }, + { 147, 3 }, + { 149, 0 }, + { 149, 1 }, + { 149, 2 }, + { 148, 0 }, + { 148, 1 }, + { 148, 1 }, + { 148, 1 }, + { 147, 2 }, + { 147, 2 }, + { 147, 2 }, + { 151, 1 }, + { 151, 0 }, + { 147, 2 }, + { 147, 3 }, + { 147, 5 }, + { 147, 2 }, + { 152, 6 }, + { 154, 1 }, + { 156, 0 }, + { 156, 3 }, + { 155, 1 }, + { 155, 0 }, + { 153, 4 }, + { 153, 2 }, + { 158, 3 }, + { 158, 1 }, + { 161, 3 }, + { 162, 1 }, + { 165, 1 }, + { 165, 1 }, + { 166, 1 }, + { 150, 1 }, + { 150, 1 }, + { 150, 1 }, + { 163, 0 }, + { 163, 1 }, + { 167, 1 }, + { 167, 4 }, + { 167, 6 }, + { 168, 1 }, + { 168, 2 }, + { 169, 1 }, + { 169, 1 }, + { 164, 2 }, + { 164, 0 }, + { 172, 3 }, + { 172, 1 }, + { 173, 2 }, + { 173, 4 }, + { 173, 3 }, + { 173, 3 }, + { 173, 2 }, + { 173, 2 }, + { 173, 3 }, + { 173, 5 }, + { 173, 2 }, + { 173, 4 }, + { 173, 4 }, + { 173, 1 }, + { 173, 2 }, + { 178, 0 }, + { 178, 1 }, + { 180, 0 }, + { 180, 2 }, + { 182, 2 }, + { 182, 3 }, + { 182, 3 }, + { 182, 3 }, + { 183, 2 }, + { 183, 2 }, + { 183, 1 }, + { 183, 1 }, + { 183, 2 }, + { 181, 3 }, + { 181, 2 }, + { 184, 0 }, + { 184, 2 }, + { 184, 2 }, + { 159, 0 }, + { 159, 2 }, + { 185, 3 }, + { 185, 2 }, + { 185, 1 }, + { 186, 2 }, + { 186, 7 }, + { 186, 5 }, + { 186, 5 }, + { 186, 10 }, + { 188, 0 }, + { 188, 1 }, + { 176, 0 }, + { 176, 3 }, + { 189, 0 }, + { 189, 2 }, + { 190, 1 }, + { 190, 1 }, + { 190, 1 }, + { 147, 4 }, + { 192, 2 }, + { 192, 0 }, + { 147, 8 }, + { 147, 4 }, + { 147, 1 }, + { 160, 1 }, + { 160, 3 }, + { 195, 1 }, + { 195, 2 }, + { 195, 1 }, + { 194, 9 }, + { 196, 1 }, + { 196, 1 }, + { 196, 0 }, + { 204, 2 }, + { 204, 0 }, + { 197, 3 }, + { 197, 2 }, + { 197, 4 }, + { 205, 2 }, + { 205, 1 }, + { 205, 0 }, + { 198, 0 }, + { 198, 2 }, + { 207, 2 }, + { 207, 0 }, + { 206, 7 }, + { 206, 7 }, + { 206, 7 }, + { 157, 0 }, + { 157, 2 }, + { 193, 2 }, + { 208, 1 }, + { 208, 2 }, + { 208, 3 }, + { 208, 4 }, + { 210, 2 }, + { 210, 0 }, + { 209, 0 }, + { 209, 3 }, + { 209, 2 }, + { 211, 4 }, + { 211, 0 }, + { 202, 0 }, + { 202, 3 }, + { 214, 4 }, + { 214, 2 }, + { 215, 1 }, + { 177, 1 }, + { 177, 1 }, + { 177, 0 }, + { 200, 0 }, + { 200, 3 }, + { 201, 0 }, + { 201, 2 }, + { 203, 0 }, + { 203, 2 }, + { 203, 4 }, + { 203, 4 }, + { 147, 5 }, + { 199, 0 }, + { 199, 2 }, + { 147, 7 }, + { 217, 5 }, + { 217, 3 }, + { 147, 8 }, + { 147, 5 }, + { 147, 6 }, + { 218, 2 }, + { 218, 1 }, + { 220, 3 }, + { 220, 1 }, + { 219, 0 }, + { 219, 3 }, + { 213, 3 }, + { 213, 1 }, + { 175, 1 }, + { 175, 3 }, + { 174, 1 }, + { 175, 1 }, + { 175, 1 }, + { 175, 3 }, + { 175, 5 }, + { 174, 1 }, + { 174, 1 }, + { 175, 1 }, + { 175, 1 }, + { 175, 3 }, + { 175, 6 }, + { 175, 5 }, + { 175, 4 }, + { 174, 1 }, + { 175, 3 }, + { 175, 3 }, + { 175, 3 }, + { 175, 3 }, + { 175, 3 }, + { 175, 3 }, + { 175, 3 }, + { 175, 3 }, + { 222, 1 }, + { 222, 2 }, + { 222, 1 }, + { 222, 2 }, + { 175, 3 }, + { 175, 5 }, + { 175, 2 }, + { 175, 3 }, + { 175, 3 }, + { 175, 4 }, + { 175, 2 }, + { 175, 2 }, + { 175, 2 }, + { 175, 2 }, + { 223, 1 }, + { 223, 2 }, + { 175, 5 }, + { 224, 1 }, + { 224, 2 }, + { 175, 5 }, + { 175, 3 }, + { 175, 5 }, + { 175, 4 }, + { 175, 4 }, + { 175, 5 }, + { 226, 5 }, + { 226, 4 }, + { 227, 2 }, + { 227, 0 }, + { 225, 1 }, + { 225, 0 }, + { 221, 1 }, + { 221, 0 }, + { 216, 3 }, + { 216, 1 }, + { 147, 11 }, + { 228, 1 }, + { 228, 0 }, + { 179, 0 }, + { 179, 3 }, + { 187, 5 }, + { 187, 3 }, + { 229, 0 }, + { 229, 2 }, + { 147, 4 }, + { 147, 1 }, + { 147, 2 }, + { 147, 3 }, + { 147, 5 }, + { 147, 6 }, + { 147, 5 }, + { 147, 6 }, + { 230, 1 }, + { 230, 1 }, + { 230, 1 }, + { 230, 1 }, + { 230, 1 }, + { 170, 2 }, + { 171, 2 }, + { 232, 1 }, + { 231, 1 }, + { 231, 0 }, + { 147, 5 }, + { 233, 11 }, + { 235, 1 }, + { 235, 1 }, + { 235, 2 }, + { 235, 0 }, + { 236, 1 }, + { 236, 1 }, + { 236, 3 }, + { 237, 0 }, + { 237, 3 }, + { 238, 0 }, + { 238, 2 }, + { 234, 3 }, + { 234, 2 }, + { 240, 1 }, + { 240, 3 }, + { 241, 0 }, + { 241, 3 }, + { 241, 2 }, + { 239, 7 }, + { 239, 8 }, + { 239, 5 }, + { 239, 5 }, + { 239, 1 }, + { 175, 4 }, + { 175, 6 }, + { 191, 1 }, + { 191, 1 }, + { 191, 1 }, + { 147, 4 }, + { 147, 6 }, + { 147, 3 }, + { 243, 0 }, + { 243, 2 }, + { 242, 1 }, + { 242, 0 }, + { 147, 1 }, + { 147, 3 }, + { 147, 1 }, + { 147, 3 }, + { 147, 6 }, + { 147, 6 }, + { 244, 1 }, + { 245, 0 }, + { 245, 1 }, + { 147, 1 }, + { 147, 4 }, + { 246, 7 }, + { 247, 1 }, + { 247, 3 }, + { 248, 0 }, + { 248, 2 }, + { 249, 1 }, + { 249, 3 }, + { 250, 1 }, + { 251, 0 }, + { 251, 4 }, + { 251, 2 }, +}; + +static void yy_accept(yyParser*); /* Forward Declaration */ + +/* +** Perform a reduce action and the shift that must immediately +** follow the reduce. +*/ +static void yy_reduce( + yyParser *yypParser, /* The parser */ + int yyruleno /* Number of the rule by which to reduce */ +){ + int yygoto; /* The next state */ + int yyact; /* The next action */ + YYMINORTYPE yygotominor; /* The LHS of the rule reduced */ + yyStackEntry *yymsp; /* The top of the parser's stack */ + int yysize; /* Amount to pop the stack */ + sqlite3ParserARG_FETCH; + yymsp = &yypParser->yystack[yypParser->yyidx]; +#ifndef NDEBUG + if( yyTraceFILE && yyruleno>=0 + && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){ + fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt, + yyRuleName[yyruleno]); + } +#endif /* NDEBUG */ + + /* Silence complaints from purify about yygotominor being uninitialized + ** in some cases when it is copied into the stack after the following + ** switch. yygotominor is uninitialized when a rule reduces that does + ** not set the value of its left-hand side nonterminal. Leaving the + ** value of the nonterminal uninitialized is utterly harmless as long + ** as the value is never used. So really the only thing this code + ** accomplishes is to quieten purify. + ** + ** 2007-01-16: The wireshark project (www.wireshark.org) reports that + ** without this code, their parser segfaults. I'm not sure what there + ** parser is doing to make this happen. This is the second bug report + ** from wireshark this week. Clearly they are stressing Lemon in ways + ** that it has not been previously stressed... (SQLite ticket #2172) + */ + /*memset(&yygotominor, 0, sizeof(yygotominor));*/ + yygotominor = yyzerominor; + + + switch( yyruleno ){ + /* Beginning here are the reduction cases. A typical example + ** follows: + ** case 0: + ** #line + ** { ... } // User supplied code + ** #line + ** break; + */ + case 5: /* explain ::= */ +{ sqlite3BeginParse(pParse, 0); } + break; + case 6: /* explain ::= EXPLAIN */ +{ sqlite3BeginParse(pParse, 1); } + break; + case 7: /* explain ::= EXPLAIN QUERY PLAN */ +{ sqlite3BeginParse(pParse, 2); } + break; + case 8: /* cmdx ::= cmd */ +{ sqlite3FinishCoding(pParse); } + break; + case 9: /* cmd ::= BEGIN transtype trans_opt */ +{sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy4);} + break; + case 13: /* transtype ::= */ +{yygotominor.yy4 = TK_DEFERRED;} + break; + case 14: /* transtype ::= DEFERRED */ + case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15); + case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16); + case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115); + case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117); +{yygotominor.yy4 = yymsp[0].major;} + break; + case 17: /* cmd ::= COMMIT trans_opt */ + case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18); +{sqlite3CommitTransaction(pParse);} + break; + case 19: /* cmd ::= ROLLBACK trans_opt */ +{sqlite3RollbackTransaction(pParse);} + break; + case 22: /* cmd ::= SAVEPOINT nm */ +{ + sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0); +} + break; + case 23: /* cmd ::= RELEASE savepoint_opt nm */ +{ + sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0); +} + break; + case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */ +{ + sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0); +} + break; + case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */ +{ + sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy4,0,0,yymsp[-2].minor.yy4); +} + break; + case 27: /* createkw ::= CREATE */ +{ + pParse->db->lookaside.bEnabled = 0; + yygotominor.yy0 = yymsp[0].minor.yy0; +} + break; + case 28: /* ifnotexists ::= */ + case 31: /* temp ::= */ yytestcase(yyruleno==31); + case 70: /* autoinc ::= */ yytestcase(yyruleno==70); + case 83: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==83); + case 85: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==85); + case 87: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==87); + case 98: /* defer_subclause_opt ::= */ yytestcase(yyruleno==98); + case 109: /* ifexists ::= */ yytestcase(yyruleno==109); + case 120: /* distinct ::= ALL */ yytestcase(yyruleno==120); + case 121: /* distinct ::= */ yytestcase(yyruleno==121); + case 222: /* between_op ::= BETWEEN */ yytestcase(yyruleno==222); + case 225: /* in_op ::= IN */ yytestcase(yyruleno==225); +{yygotominor.yy4 = 0;} + break; + case 29: /* ifnotexists ::= IF NOT EXISTS */ + case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30); + case 71: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==71); + case 86: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==86); + case 108: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==108); + case 119: /* distinct ::= DISTINCT */ yytestcase(yyruleno==119); + case 223: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==223); + case 226: /* in_op ::= NOT IN */ yytestcase(yyruleno==226); +{yygotominor.yy4 = 1;} + break; + case 32: /* create_table_args ::= LP columnlist conslist_opt RP */ +{ + sqlite3EndTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0); +} + break; + case 33: /* create_table_args ::= AS select */ +{ + sqlite3EndTable(pParse,0,0,yymsp[0].minor.yy387); + sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387); +} + break; + case 36: /* column ::= columnid type carglist */ +{ + yygotominor.yy0.z = yymsp[-2].minor.yy0.z; + yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n; +} + break; + case 37: /* columnid ::= nm */ +{ + sqlite3AddColumn(pParse,&yymsp[0].minor.yy0); + yygotominor.yy0 = yymsp[0].minor.yy0; +} + break; + case 38: /* id ::= ID */ + case 39: /* id ::= INDEXED */ yytestcase(yyruleno==39); + case 40: /* ids ::= ID|STRING */ yytestcase(yyruleno==40); + case 41: /* nm ::= id */ yytestcase(yyruleno==41); + case 42: /* nm ::= STRING */ yytestcase(yyruleno==42); + case 43: /* nm ::= JOIN_KW */ yytestcase(yyruleno==43); + case 46: /* typetoken ::= typename */ yytestcase(yyruleno==46); + case 49: /* typename ::= ids */ yytestcase(yyruleno==49); + case 127: /* as ::= AS nm */ yytestcase(yyruleno==127); + case 128: /* as ::= ids */ yytestcase(yyruleno==128); + case 138: /* dbnm ::= DOT nm */ yytestcase(yyruleno==138); + case 147: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==147); + case 251: /* collate ::= COLLATE ids */ yytestcase(yyruleno==251); + case 260: /* nmnum ::= plus_num */ yytestcase(yyruleno==260); + case 261: /* nmnum ::= nm */ yytestcase(yyruleno==261); + case 262: /* nmnum ::= ON */ yytestcase(yyruleno==262); + case 263: /* nmnum ::= DELETE */ yytestcase(yyruleno==263); + case 264: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==264); + case 265: /* plus_num ::= plus_opt number */ yytestcase(yyruleno==265); + case 266: /* minus_num ::= MINUS number */ yytestcase(yyruleno==266); + case 267: /* number ::= INTEGER|FLOAT */ yytestcase(yyruleno==267); + case 285: /* trnm ::= nm */ yytestcase(yyruleno==285); +{yygotominor.yy0 = yymsp[0].minor.yy0;} + break; + case 45: /* type ::= typetoken */ +{sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);} + break; + case 47: /* typetoken ::= typename LP signed RP */ +{ + yygotominor.yy0.z = yymsp[-3].minor.yy0.z; + yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z); +} + break; + case 48: /* typetoken ::= typename LP signed COMMA signed RP */ +{ + yygotominor.yy0.z = yymsp[-5].minor.yy0.z; + yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z); +} + break; + case 50: /* typename ::= typename ids */ +{yygotominor.yy0.z=yymsp[-1].minor.yy0.z; yygotominor.yy0.n=yymsp[0].minor.yy0.n+(int)(yymsp[0].minor.yy0.z-yymsp[-1].minor.yy0.z);} + break; + case 57: /* ccons ::= DEFAULT term */ + case 59: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==59); +{sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy118);} + break; + case 58: /* ccons ::= DEFAULT LP expr RP */ +{sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy118);} + break; + case 60: /* ccons ::= DEFAULT MINUS term */ +{ + ExprSpan v; + v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy118.pExpr, 0, 0); + v.zStart = yymsp[-1].minor.yy0.z; + v.zEnd = yymsp[0].minor.yy118.zEnd; + sqlite3AddDefaultValue(pParse,&v); +} + break; + case 61: /* ccons ::= DEFAULT id */ +{ + ExprSpan v; + spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0); + sqlite3AddDefaultValue(pParse,&v); +} + break; + case 63: /* ccons ::= NOT NULL onconf */ +{sqlite3AddNotNull(pParse, yymsp[0].minor.yy4);} + break; + case 64: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */ +{sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy4,yymsp[0].minor.yy4,yymsp[-2].minor.yy4);} + break; + case 65: /* ccons ::= UNIQUE onconf */ +{sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy4,0,0,0,0);} + break; + case 66: /* ccons ::= CHECK LP expr RP */ +{sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy118.pExpr);} + break; + case 67: /* ccons ::= REFERENCES nm idxlist_opt refargs */ +{sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy322,yymsp[0].minor.yy4);} + break; + case 68: /* ccons ::= defer_subclause */ +{sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy4);} + break; + case 69: /* ccons ::= COLLATE ids */ +{sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);} + break; + case 72: /* refargs ::= */ +{ yygotominor.yy4 = OE_None*0x0101; /* EV: R-19803-45884 */} + break; + case 73: /* refargs ::= refargs refarg */ +{ yygotominor.yy4 = (yymsp[-1].minor.yy4 & ~yymsp[0].minor.yy215.mask) | yymsp[0].minor.yy215.value; } + break; + case 74: /* refarg ::= MATCH nm */ + case 75: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==75); +{ yygotominor.yy215.value = 0; yygotominor.yy215.mask = 0x000000; } + break; + case 76: /* refarg ::= ON DELETE refact */ +{ yygotominor.yy215.value = yymsp[0].minor.yy4; yygotominor.yy215.mask = 0x0000ff; } + break; + case 77: /* refarg ::= ON UPDATE refact */ +{ yygotominor.yy215.value = yymsp[0].minor.yy4<<8; yygotominor.yy215.mask = 0x00ff00; } + break; + case 78: /* refact ::= SET NULL */ +{ yygotominor.yy4 = OE_SetNull; /* EV: R-33326-45252 */} + break; + case 79: /* refact ::= SET DEFAULT */ +{ yygotominor.yy4 = OE_SetDflt; /* EV: R-33326-45252 */} + break; + case 80: /* refact ::= CASCADE */ +{ yygotominor.yy4 = OE_Cascade; /* EV: R-33326-45252 */} + break; + case 81: /* refact ::= RESTRICT */ +{ yygotominor.yy4 = OE_Restrict; /* EV: R-33326-45252 */} + break; + case 82: /* refact ::= NO ACTION */ +{ yygotominor.yy4 = OE_None; /* EV: R-33326-45252 */} + break; + case 84: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */ + case 99: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==99); + case 101: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==101); + case 104: /* resolvetype ::= raisetype */ yytestcase(yyruleno==104); +{yygotominor.yy4 = yymsp[0].minor.yy4;} + break; + case 88: /* conslist_opt ::= */ +{yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;} + break; + case 89: /* conslist_opt ::= COMMA conslist */ +{yygotominor.yy0 = yymsp[-1].minor.yy0;} + break; + case 94: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */ +{sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy322,yymsp[0].minor.yy4,yymsp[-2].minor.yy4,0);} + break; + case 95: /* tcons ::= UNIQUE LP idxlist RP onconf */ +{sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy322,yymsp[0].minor.yy4,0,0,0,0);} + break; + case 96: /* tcons ::= CHECK LP expr RP onconf */ +{sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy118.pExpr);} + break; + case 97: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */ +{ + sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy322, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy322, yymsp[-1].minor.yy4); + sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy4); +} + break; + case 100: /* onconf ::= */ +{yygotominor.yy4 = OE_Default;} + break; + case 102: /* orconf ::= */ +{yygotominor.yy210 = OE_Default;} + break; + case 103: /* orconf ::= OR resolvetype */ +{yygotominor.yy210 = (u8)yymsp[0].minor.yy4;} + break; + case 105: /* resolvetype ::= IGNORE */ +{yygotominor.yy4 = OE_Ignore;} + break; + case 106: /* resolvetype ::= REPLACE */ +{yygotominor.yy4 = OE_Replace;} + break; + case 107: /* cmd ::= DROP TABLE ifexists fullname */ +{ + sqlite3DropTable(pParse, yymsp[0].minor.yy259, 0, yymsp[-1].minor.yy4); +} + break; + case 110: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */ +{ + sqlite3CreateView(pParse, &yymsp[-7].minor.yy0, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, yymsp[0].minor.yy387, yymsp[-6].minor.yy4, yymsp[-4].minor.yy4); +} + break; + case 111: /* cmd ::= DROP VIEW ifexists fullname */ +{ + sqlite3DropTable(pParse, yymsp[0].minor.yy259, 1, yymsp[-1].minor.yy4); +} + break; + case 112: /* cmd ::= select */ +{ + SelectDest dest = {SRT_Output, 0, 0, 0, 0}; + sqlite3Select(pParse, yymsp[0].minor.yy387, &dest); + sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387); +} + break; + case 113: /* select ::= oneselect */ +{yygotominor.yy387 = yymsp[0].minor.yy387;} + break; + case 114: /* select ::= select multiselect_op oneselect */ +{ + if( yymsp[0].minor.yy387 ){ + yymsp[0].minor.yy387->op = (u8)yymsp[-1].minor.yy4; + yymsp[0].minor.yy387->pPrior = yymsp[-2].minor.yy387; + }else{ + sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy387); + } + yygotominor.yy387 = yymsp[0].minor.yy387; +} + break; + case 116: /* multiselect_op ::= UNION ALL */ +{yygotominor.yy4 = TK_ALL;} + break; + case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */ +{ + yygotominor.yy387 = sqlite3SelectNew(pParse,yymsp[-6].minor.yy322,yymsp[-5].minor.yy259,yymsp[-4].minor.yy314,yymsp[-3].minor.yy322,yymsp[-2].minor.yy314,yymsp[-1].minor.yy322,yymsp[-7].minor.yy4,yymsp[0].minor.yy292.pLimit,yymsp[0].minor.yy292.pOffset); +} + break; + case 122: /* sclp ::= selcollist COMMA */ + case 247: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==247); +{yygotominor.yy322 = yymsp[-1].minor.yy322;} + break; + case 123: /* sclp ::= */ + case 151: /* orderby_opt ::= */ yytestcase(yyruleno==151); + case 159: /* groupby_opt ::= */ yytestcase(yyruleno==159); + case 240: /* exprlist ::= */ yytestcase(yyruleno==240); + case 246: /* idxlist_opt ::= */ yytestcase(yyruleno==246); +{yygotominor.yy322 = 0;} + break; + case 124: /* selcollist ::= sclp expr as */ +{ + yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy322, yymsp[-1].minor.yy118.pExpr); + if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[0].minor.yy0, 1); + sqlite3ExprListSetSpan(pParse,yygotominor.yy322,&yymsp[-1].minor.yy118); +} + break; + case 125: /* selcollist ::= sclp STAR */ +{ + Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0); + yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy322, p); +} + break; + case 126: /* selcollist ::= sclp nm DOT STAR */ +{ + Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0); + Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0); + Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0); + yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322, pDot); +} + break; + case 129: /* as ::= */ +{yygotominor.yy0.n = 0;} + break; + case 130: /* from ::= */ +{yygotominor.yy259 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy259));} + break; + case 131: /* from ::= FROM seltablist */ +{ + yygotominor.yy259 = yymsp[0].minor.yy259; + sqlite3SrcListShiftJoinType(yygotominor.yy259); +} + break; + case 132: /* stl_prefix ::= seltablist joinop */ +{ + yygotominor.yy259 = yymsp[-1].minor.yy259; + if( ALWAYS(yygotominor.yy259 && yygotominor.yy259->nSrc>0) ) yygotominor.yy259->a[yygotominor.yy259->nSrc-1].jointype = (u8)yymsp[0].minor.yy4; +} + break; + case 133: /* stl_prefix ::= */ +{yygotominor.yy259 = 0;} + break; + case 134: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */ +{ + yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,&yymsp[-5].minor.yy0,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,0,yymsp[-1].minor.yy314,yymsp[0].minor.yy384); + sqlite3SrcListIndexedBy(pParse, yygotominor.yy259, &yymsp[-2].minor.yy0); +} + break; + case 135: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */ +{ + yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].minor.yy0,yymsp[-4].minor.yy387,yymsp[-1].minor.yy314,yymsp[0].minor.yy384); + } + break; + case 136: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */ +{ + if( yymsp[-6].minor.yy259==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy314==0 && yymsp[0].minor.yy384==0 ){ + yygotominor.yy259 = yymsp[-4].minor.yy259; + }else{ + Select *pSubquery; + sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy259); + pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy259,0,0,0,0,0,0,0); + yygotominor.yy259 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy259,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy314,yymsp[0].minor.yy384); + } + } + break; + case 137: /* dbnm ::= */ + case 146: /* indexed_opt ::= */ yytestcase(yyruleno==146); +{yygotominor.yy0.z=0; yygotominor.yy0.n=0;} + break; + case 139: /* fullname ::= nm dbnm */ +{yygotominor.yy259 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);} + break; + case 140: /* joinop ::= COMMA|JOIN */ +{ yygotominor.yy4 = JT_INNER; } + break; + case 141: /* joinop ::= JOIN_KW JOIN */ +{ yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); } + break; + case 142: /* joinop ::= JOIN_KW nm JOIN */ +{ yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); } + break; + case 143: /* joinop ::= JOIN_KW nm nm JOIN */ +{ yygotominor.yy4 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); } + break; + case 144: /* on_opt ::= ON expr */ + case 155: /* sortitem ::= expr */ yytestcase(yyruleno==155); + case 162: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==162); + case 169: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==169); + case 235: /* case_else ::= ELSE expr */ yytestcase(yyruleno==235); + case 237: /* case_operand ::= expr */ yytestcase(yyruleno==237); +{yygotominor.yy314 = yymsp[0].minor.yy118.pExpr;} + break; + case 145: /* on_opt ::= */ + case 161: /* having_opt ::= */ yytestcase(yyruleno==161); + case 168: /* where_opt ::= */ yytestcase(yyruleno==168); + case 236: /* case_else ::= */ yytestcase(yyruleno==236); + case 238: /* case_operand ::= */ yytestcase(yyruleno==238); +{yygotominor.yy314 = 0;} + break; + case 148: /* indexed_opt ::= NOT INDEXED */ +{yygotominor.yy0.z=0; yygotominor.yy0.n=1;} + break; + case 149: /* using_opt ::= USING LP inscollist RP */ + case 181: /* inscollist_opt ::= LP inscollist RP */ yytestcase(yyruleno==181); +{yygotominor.yy384 = yymsp[-1].minor.yy384;} + break; + case 150: /* using_opt ::= */ + case 180: /* inscollist_opt ::= */ yytestcase(yyruleno==180); +{yygotominor.yy384 = 0;} + break; + case 152: /* orderby_opt ::= ORDER BY sortlist */ + case 160: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==160); + case 239: /* exprlist ::= nexprlist */ yytestcase(yyruleno==239); +{yygotominor.yy322 = yymsp[0].minor.yy322;} + break; + case 153: /* sortlist ::= sortlist COMMA sortitem sortorder */ +{ + yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy322,yymsp[-1].minor.yy314); + if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4; +} + break; + case 154: /* sortlist ::= sortitem sortorder */ +{ + yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy314); + if( yygotominor.yy322 && ALWAYS(yygotominor.yy322->a) ) yygotominor.yy322->a[0].sortOrder = (u8)yymsp[0].minor.yy4; +} + break; + case 156: /* sortorder ::= ASC */ + case 158: /* sortorder ::= */ yytestcase(yyruleno==158); +{yygotominor.yy4 = SQLITE_SO_ASC;} + break; + case 157: /* sortorder ::= DESC */ +{yygotominor.yy4 = SQLITE_SO_DESC;} + break; + case 163: /* limit_opt ::= */ +{yygotominor.yy292.pLimit = 0; yygotominor.yy292.pOffset = 0;} + break; + case 164: /* limit_opt ::= LIMIT expr */ +{yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr; yygotominor.yy292.pOffset = 0;} + break; + case 165: /* limit_opt ::= LIMIT expr OFFSET expr */ +{yygotominor.yy292.pLimit = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pOffset = yymsp[0].minor.yy118.pExpr;} + break; + case 166: /* limit_opt ::= LIMIT expr COMMA expr */ +{yygotominor.yy292.pOffset = yymsp[-2].minor.yy118.pExpr; yygotominor.yy292.pLimit = yymsp[0].minor.yy118.pExpr;} + break; + case 167: /* cmd ::= DELETE FROM fullname indexed_opt where_opt */ +{ + sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy259, &yymsp[-1].minor.yy0); + sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy259,yymsp[0].minor.yy314); +} + break; + case 170: /* cmd ::= UPDATE orconf fullname indexed_opt SET setlist where_opt */ +{ + sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy259, &yymsp[-3].minor.yy0); + sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy322,"set list"); + sqlite3Update(pParse,yymsp[-4].minor.yy259,yymsp[-1].minor.yy322,yymsp[0].minor.yy314,yymsp[-5].minor.yy210); +} + break; + case 171: /* setlist ::= setlist COMMA nm EQ expr */ +{ + yygotominor.yy322 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy322, yymsp[0].minor.yy118.pExpr); + sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1); +} + break; + case 172: /* setlist ::= nm EQ expr */ +{ + yygotominor.yy322 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy118.pExpr); + sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1); +} + break; + case 173: /* cmd ::= insert_cmd INTO fullname inscollist_opt VALUES LP itemlist RP */ +{sqlite3Insert(pParse, yymsp[-5].minor.yy259, yymsp[-1].minor.yy322, 0, yymsp[-4].minor.yy384, yymsp[-7].minor.yy210);} + break; + case 174: /* cmd ::= insert_cmd INTO fullname inscollist_opt select */ +{sqlite3Insert(pParse, yymsp[-2].minor.yy259, 0, yymsp[0].minor.yy387, yymsp[-1].minor.yy384, yymsp[-4].minor.yy210);} + break; + case 175: /* cmd ::= insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */ +{sqlite3Insert(pParse, yymsp[-3].minor.yy259, 0, 0, yymsp[-2].minor.yy384, yymsp[-5].minor.yy210);} + break; + case 176: /* insert_cmd ::= INSERT orconf */ +{yygotominor.yy210 = yymsp[0].minor.yy210;} + break; + case 177: /* insert_cmd ::= REPLACE */ +{yygotominor.yy210 = OE_Replace;} + break; + case 178: /* itemlist ::= itemlist COMMA expr */ + case 241: /* nexprlist ::= nexprlist COMMA expr */ yytestcase(yyruleno==241); +{yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy322,yymsp[0].minor.yy118.pExpr);} + break; + case 179: /* itemlist ::= expr */ + case 242: /* nexprlist ::= expr */ yytestcase(yyruleno==242); +{yygotominor.yy322 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy118.pExpr);} + break; + case 182: /* inscollist ::= inscollist COMMA nm */ +{yygotominor.yy384 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy384,&yymsp[0].minor.yy0);} + break; + case 183: /* inscollist ::= nm */ +{yygotominor.yy384 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);} + break; + case 184: /* expr ::= term */ +{yygotominor.yy118 = yymsp[0].minor.yy118;} + break; + case 185: /* expr ::= LP expr RP */ +{yygotominor.yy118.pExpr = yymsp[-1].minor.yy118.pExpr; spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);} + break; + case 186: /* term ::= NULL */ + case 191: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==191); + case 192: /* term ::= STRING */ yytestcase(yyruleno==192); +{spanExpr(&yygotominor.yy118, pParse, yymsp[0].major, &yymsp[0].minor.yy0);} + break; + case 187: /* expr ::= id */ + case 188: /* expr ::= JOIN_KW */ yytestcase(yyruleno==188); +{spanExpr(&yygotominor.yy118, pParse, TK_ID, &yymsp[0].minor.yy0);} + break; + case 189: /* expr ::= nm DOT nm */ +{ + Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0); + Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0); + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0); + spanSet(&yygotominor.yy118,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0); +} + break; + case 190: /* expr ::= nm DOT nm DOT nm */ +{ + Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0); + Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0); + Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0); + Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0); + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0); + spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); +} + break; + case 193: /* expr ::= REGISTER */ +{ + /* When doing a nested parse, one can include terms in an expression + ** that look like this: #1 #2 ... These terms refer to registers + ** in the virtual machine. #N is the N-th register. */ + if( pParse->nested==0 ){ + sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0); + yygotominor.yy118.pExpr = 0; + }else{ + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0); + if( yygotominor.yy118.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy118.pExpr->iTable); + } + spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0); +} + break; + case 194: /* expr ::= VARIABLE */ +{ + spanExpr(&yygotominor.yy118, pParse, TK_VARIABLE, &yymsp[0].minor.yy0); + sqlite3ExprAssignVarNumber(pParse, yygotominor.yy118.pExpr); + spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0); +} + break; + case 195: /* expr ::= expr COLLATE ids */ +{ + yygotominor.yy118.pExpr = sqlite3ExprSetCollByToken(pParse, yymsp[-2].minor.yy118.pExpr, &yymsp[0].minor.yy0); + yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart; + yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; +} + break; + case 196: /* expr ::= CAST LP expr AS typetoken RP */ +{ + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy118.pExpr, 0, &yymsp[-1].minor.yy0); + spanSet(&yygotominor.yy118,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0); +} + break; + case 197: /* expr ::= ID LP distinct exprlist RP */ +{ + if( yymsp[-1].minor.yy322 && yymsp[-1].minor.yy322->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){ + sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0); + } + yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy322, &yymsp[-4].minor.yy0); + spanSet(&yygotominor.yy118,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0); + if( yymsp[-2].minor.yy4 && yygotominor.yy118.pExpr ){ + yygotominor.yy118.pExpr->flags |= EP_Distinct; + } +} + break; + case 198: /* expr ::= ID LP STAR RP */ +{ + yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0); + spanSet(&yygotominor.yy118,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0); +} + break; + case 199: /* term ::= CTIME_KW */ +{ + /* The CURRENT_TIME, CURRENT_DATE, and CURRENT_TIMESTAMP values are + ** treated as functions that return constants */ + yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, 0,&yymsp[0].minor.yy0); + if( yygotominor.yy118.pExpr ){ + yygotominor.yy118.pExpr->op = TK_CONST_FUNC; + } + spanSet(&yygotominor.yy118, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0); +} + break; + case 200: /* expr ::= expr AND expr */ + case 201: /* expr ::= expr OR expr */ yytestcase(yyruleno==201); + case 202: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==202); + case 203: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==203); + case 204: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==204); + case 205: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==205); + case 206: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==206); + case 207: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==207); +{spanBinaryExpr(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118);} + break; + case 208: /* likeop ::= LIKE_KW */ + case 210: /* likeop ::= MATCH */ yytestcase(yyruleno==210); +{yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 0;} + break; + case 209: /* likeop ::= NOT LIKE_KW */ + case 211: /* likeop ::= NOT MATCH */ yytestcase(yyruleno==211); +{yygotominor.yy342.eOperator = yymsp[0].minor.yy0; yygotominor.yy342.not = 1;} + break; + case 212: /* expr ::= expr likeop expr */ +{ + ExprList *pList; + pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy118.pExpr); + pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy118.pExpr); + yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy342.eOperator); + if( yymsp[-1].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0); + yygotominor.yy118.zStart = yymsp[-2].minor.yy118.zStart; + yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd; + if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc; +} + break; + case 213: /* expr ::= expr likeop expr ESCAPE expr */ +{ + ExprList *pList; + pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr); + pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy118.pExpr); + pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr); + yygotominor.yy118.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy342.eOperator); + if( yymsp[-3].minor.yy342.not ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0); + yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart; + yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd; + if( yygotominor.yy118.pExpr ) yygotominor.yy118.pExpr->flags |= EP_InfixFunc; +} + break; + case 214: /* expr ::= expr ISNULL|NOTNULL */ +{spanUnaryPostfix(&yygotominor.yy118,pParse,yymsp[0].major,&yymsp[-1].minor.yy118,&yymsp[0].minor.yy0);} + break; + case 215: /* expr ::= expr NOT NULL */ +{spanUnaryPostfix(&yygotominor.yy118,pParse,TK_NOTNULL,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy0);} + break; + case 216: /* expr ::= expr IS expr */ +{ + spanBinaryExpr(&yygotominor.yy118,pParse,TK_IS,&yymsp[-2].minor.yy118,&yymsp[0].minor.yy118); + binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_ISNULL); +} + break; + case 217: /* expr ::= expr IS NOT expr */ +{ + spanBinaryExpr(&yygotominor.yy118,pParse,TK_ISNOT,&yymsp[-3].minor.yy118,&yymsp[0].minor.yy118); + binaryToUnaryIfNull(pParse, yymsp[0].minor.yy118.pExpr, yygotominor.yy118.pExpr, TK_NOTNULL); +} + break; + case 218: /* expr ::= NOT expr */ + case 219: /* expr ::= BITNOT expr */ yytestcase(yyruleno==219); +{spanUnaryPrefix(&yygotominor.yy118,pParse,yymsp[-1].major,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);} + break; + case 220: /* expr ::= MINUS expr */ +{spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UMINUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);} + break; + case 221: /* expr ::= PLUS expr */ +{spanUnaryPrefix(&yygotominor.yy118,pParse,TK_UPLUS,&yymsp[0].minor.yy118,&yymsp[-1].minor.yy0);} + break; + case 224: /* expr ::= expr between_op expr AND expr */ +{ + ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr); + pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy118.pExpr); + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy118.pExpr, 0, 0); + if( yygotominor.yy118.pExpr ){ + yygotominor.yy118.pExpr->x.pList = pList; + }else{ + sqlite3ExprListDelete(pParse->db, pList); + } + if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0); + yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart; + yygotominor.yy118.zEnd = yymsp[0].minor.yy118.zEnd; +} + break; + case 227: /* expr ::= expr in_op LP exprlist RP */ +{ + if( yymsp[-1].minor.yy322==0 ){ + /* Expressions of the form + ** + ** expr1 IN () + ** expr1 NOT IN () + ** + ** simplify to constants 0 (false) and 1 (true), respectively, + ** regardless of the value of expr1. + */ + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy4]); + sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy118.pExpr); + }else{ + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0); + if( yygotominor.yy118.pExpr ){ + yygotominor.yy118.pExpr->x.pList = yymsp[-1].minor.yy322; + sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr); + }else{ + sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy322); + } + if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0); + } + yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart; + yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; + } + break; + case 228: /* expr ::= LP select RP */ +{ + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0); + if( yygotominor.yy118.pExpr ){ + yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387; + ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect); + sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr); + }else{ + sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387); + } + yygotominor.yy118.zStart = yymsp[-2].minor.yy0.z; + yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; + } + break; + case 229: /* expr ::= expr in_op LP select RP */ +{ + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy118.pExpr, 0, 0); + if( yygotominor.yy118.pExpr ){ + yygotominor.yy118.pExpr->x.pSelect = yymsp[-1].minor.yy387; + ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect); + sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr); + }else{ + sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387); + } + if( yymsp[-3].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0); + yygotominor.yy118.zStart = yymsp[-4].minor.yy118.zStart; + yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; + } + break; + case 230: /* expr ::= expr in_op nm dbnm */ +{ + SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0); + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy118.pExpr, 0, 0); + if( yygotominor.yy118.pExpr ){ + yygotominor.yy118.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0); + ExprSetProperty(yygotominor.yy118.pExpr, EP_xIsSelect); + sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr); + }else{ + sqlite3SrcListDelete(pParse->db, pSrc); + } + if( yymsp[-2].minor.yy4 ) yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy118.pExpr, 0, 0); + yygotominor.yy118.zStart = yymsp[-3].minor.yy118.zStart; + yygotominor.yy118.zEnd = yymsp[0].minor.yy0.z ? &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] : &yymsp[-1].minor.yy0.z[yymsp[-1].minor.yy0.n]; + } + break; + case 231: /* expr ::= EXISTS LP select RP */ +{ + Expr *p = yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0); + if( p ){ + p->x.pSelect = yymsp[-1].minor.yy387; + ExprSetProperty(p, EP_xIsSelect); + sqlite3ExprSetHeight(pParse, p); + }else{ + sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy387); + } + yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z; + yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; + } + break; + case 232: /* expr ::= CASE case_operand case_exprlist case_else END */ +{ + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy314, yymsp[-1].minor.yy314, 0); + if( yygotominor.yy118.pExpr ){ + yygotominor.yy118.pExpr->x.pList = yymsp[-2].minor.yy322; + sqlite3ExprSetHeight(pParse, yygotominor.yy118.pExpr); + }else{ + sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy322); + } + yygotominor.yy118.zStart = yymsp[-4].minor.yy0.z; + yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; +} + break; + case 233: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */ +{ + yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, yymsp[-2].minor.yy118.pExpr); + yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr); +} + break; + case 234: /* case_exprlist ::= WHEN expr THEN expr */ +{ + yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy118.pExpr); + yygotominor.yy322 = sqlite3ExprListAppend(pParse,yygotominor.yy322, yymsp[0].minor.yy118.pExpr); +} + break; + case 243: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP */ +{ + sqlite3CreateIndex(pParse, &yymsp[-6].minor.yy0, &yymsp[-5].minor.yy0, + sqlite3SrcListAppend(pParse->db,0,&yymsp[-3].minor.yy0,0), yymsp[-1].minor.yy322, yymsp[-9].minor.yy4, + &yymsp[-10].minor.yy0, &yymsp[0].minor.yy0, SQLITE_SO_ASC, yymsp[-7].minor.yy4); +} + break; + case 244: /* uniqueflag ::= UNIQUE */ + case 298: /* raisetype ::= ABORT */ yytestcase(yyruleno==298); +{yygotominor.yy4 = OE_Abort;} + break; + case 245: /* uniqueflag ::= */ +{yygotominor.yy4 = OE_None;} + break; + case 248: /* idxlist ::= idxlist COMMA nm collate sortorder */ +{ + Expr *p = 0; + if( yymsp[-1].minor.yy0.n>0 ){ + p = sqlite3Expr(pParse->db, TK_COLUMN, 0); + sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0); + } + yygotominor.yy322 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy322, p); + sqlite3ExprListSetName(pParse,yygotominor.yy322,&yymsp[-2].minor.yy0,1); + sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index"); + if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4; +} + break; + case 249: /* idxlist ::= nm collate sortorder */ +{ + Expr *p = 0; + if( yymsp[-1].minor.yy0.n>0 ){ + p = sqlite3PExpr(pParse, TK_COLUMN, 0, 0, 0); + sqlite3ExprSetCollByToken(pParse, p, &yymsp[-1].minor.yy0); + } + yygotominor.yy322 = sqlite3ExprListAppend(pParse,0, p); + sqlite3ExprListSetName(pParse, yygotominor.yy322, &yymsp[-2].minor.yy0, 1); + sqlite3ExprListCheckLength(pParse, yygotominor.yy322, "index"); + if( yygotominor.yy322 ) yygotominor.yy322->a[yygotominor.yy322->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy4; +} + break; + case 250: /* collate ::= */ +{yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;} + break; + case 252: /* cmd ::= DROP INDEX ifexists fullname */ +{sqlite3DropIndex(pParse, yymsp[0].minor.yy259, yymsp[-1].minor.yy4);} + break; + case 253: /* cmd ::= VACUUM */ + case 254: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==254); +{sqlite3Vacuum(pParse);} + break; + case 255: /* cmd ::= PRAGMA nm dbnm */ +{sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);} + break; + case 256: /* cmd ::= PRAGMA nm dbnm EQ nmnum */ +{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);} + break; + case 257: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */ +{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);} + break; + case 258: /* cmd ::= PRAGMA nm dbnm EQ minus_num */ +{sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);} + break; + case 259: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */ +{sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);} + break; + case 270: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */ +{ + Token all; + all.z = yymsp[-3].minor.yy0.z; + all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n; + sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy203, &all); +} + break; + case 271: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */ +{ + sqlite3BeginTrigger(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0, yymsp[-5].minor.yy4, yymsp[-4].minor.yy90.a, yymsp[-4].minor.yy90.b, yymsp[-2].minor.yy259, yymsp[0].minor.yy314, yymsp[-10].minor.yy4, yymsp[-8].minor.yy4); + yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0); +} + break; + case 272: /* trigger_time ::= BEFORE */ + case 275: /* trigger_time ::= */ yytestcase(yyruleno==275); +{ yygotominor.yy4 = TK_BEFORE; } + break; + case 273: /* trigger_time ::= AFTER */ +{ yygotominor.yy4 = TK_AFTER; } + break; + case 274: /* trigger_time ::= INSTEAD OF */ +{ yygotominor.yy4 = TK_INSTEAD;} + break; + case 276: /* trigger_event ::= DELETE|INSERT */ + case 277: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==277); +{yygotominor.yy90.a = yymsp[0].major; yygotominor.yy90.b = 0;} + break; + case 278: /* trigger_event ::= UPDATE OF inscollist */ +{yygotominor.yy90.a = TK_UPDATE; yygotominor.yy90.b = yymsp[0].minor.yy384;} + break; + case 281: /* when_clause ::= */ + case 303: /* key_opt ::= */ yytestcase(yyruleno==303); +{ yygotominor.yy314 = 0; } + break; + case 282: /* when_clause ::= WHEN expr */ + case 304: /* key_opt ::= KEY expr */ yytestcase(yyruleno==304); +{ yygotominor.yy314 = yymsp[0].minor.yy118.pExpr; } + break; + case 283: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */ +{ + assert( yymsp[-2].minor.yy203!=0 ); + yymsp[-2].minor.yy203->pLast->pNext = yymsp[-1].minor.yy203; + yymsp[-2].minor.yy203->pLast = yymsp[-1].minor.yy203; + yygotominor.yy203 = yymsp[-2].minor.yy203; +} + break; + case 284: /* trigger_cmd_list ::= trigger_cmd SEMI */ +{ + assert( yymsp[-1].minor.yy203!=0 ); + yymsp[-1].minor.yy203->pLast = yymsp[-1].minor.yy203; + yygotominor.yy203 = yymsp[-1].minor.yy203; +} + break; + case 286: /* trnm ::= nm DOT nm */ +{ + yygotominor.yy0 = yymsp[0].minor.yy0; + sqlite3ErrorMsg(pParse, + "qualified table names are not allowed on INSERT, UPDATE, and DELETE " + "statements within triggers"); +} + break; + case 288: /* tridxby ::= INDEXED BY nm */ +{ + sqlite3ErrorMsg(pParse, + "the INDEXED BY clause is not allowed on UPDATE or DELETE statements " + "within triggers"); +} + break; + case 289: /* tridxby ::= NOT INDEXED */ +{ + sqlite3ErrorMsg(pParse, + "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements " + "within triggers"); +} + break; + case 290: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */ +{ yygotominor.yy203 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy322, yymsp[0].minor.yy314, yymsp[-5].minor.yy210); } + break; + case 291: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt VALUES LP itemlist RP */ +{yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy384, yymsp[-1].minor.yy322, 0, yymsp[-7].minor.yy210);} + break; + case 292: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */ +{yygotominor.yy203 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy384, 0, yymsp[0].minor.yy387, yymsp[-4].minor.yy210);} + break; + case 293: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */ +{yygotominor.yy203 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy314);} + break; + case 294: /* trigger_cmd ::= select */ +{yygotominor.yy203 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy387); } + break; + case 295: /* expr ::= RAISE LP IGNORE RP */ +{ + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0); + if( yygotominor.yy118.pExpr ){ + yygotominor.yy118.pExpr->affinity = OE_Ignore; + } + yygotominor.yy118.zStart = yymsp[-3].minor.yy0.z; + yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; +} + break; + case 296: /* expr ::= RAISE LP raisetype COMMA nm RP */ +{ + yygotominor.yy118.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0); + if( yygotominor.yy118.pExpr ) { + yygotominor.yy118.pExpr->affinity = (char)yymsp[-3].minor.yy4; + } + yygotominor.yy118.zStart = yymsp[-5].minor.yy0.z; + yygotominor.yy118.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n]; +} + break; + case 297: /* raisetype ::= ROLLBACK */ +{yygotominor.yy4 = OE_Rollback;} + break; + case 299: /* raisetype ::= FAIL */ +{yygotominor.yy4 = OE_Fail;} + break; + case 300: /* cmd ::= DROP TRIGGER ifexists fullname */ +{ + sqlite3DropTrigger(pParse,yymsp[0].minor.yy259,yymsp[-1].minor.yy4); +} + break; + case 301: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */ +{ + sqlite3Attach(pParse, yymsp[-3].minor.yy118.pExpr, yymsp[-1].minor.yy118.pExpr, yymsp[0].minor.yy314); +} + break; + case 302: /* cmd ::= DETACH database_kw_opt expr */ +{ + sqlite3Detach(pParse, yymsp[0].minor.yy118.pExpr); +} + break; + case 307: /* cmd ::= REINDEX */ +{sqlite3Reindex(pParse, 0, 0);} + break; + case 308: /* cmd ::= REINDEX nm dbnm */ +{sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);} + break; + case 309: /* cmd ::= ANALYZE */ +{sqlite3Analyze(pParse, 0, 0);} + break; + case 310: /* cmd ::= ANALYZE nm dbnm */ +{sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);} + break; + case 311: /* cmd ::= ALTER TABLE fullname RENAME TO nm */ +{ + sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy259,&yymsp[0].minor.yy0); +} + break; + case 312: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */ +{ + sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0); +} + break; + case 313: /* add_column_fullname ::= fullname */ +{ + pParse->db->lookaside.bEnabled = 0; + sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy259); +} + break; + case 316: /* cmd ::= create_vtab */ +{sqlite3VtabFinishParse(pParse,0);} + break; + case 317: /* cmd ::= create_vtab LP vtabarglist RP */ +{sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);} + break; + case 318: /* create_vtab ::= createkw VIRTUAL TABLE nm dbnm USING nm */ +{ + sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0); +} + break; + case 321: /* vtabarg ::= */ +{sqlite3VtabArgInit(pParse);} + break; + case 323: /* vtabargtoken ::= ANY */ + case 324: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==324); + case 325: /* lp ::= LP */ yytestcase(yyruleno==325); +{sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);} + break; + default: + /* (0) input ::= cmdlist */ yytestcase(yyruleno==0); + /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1); + /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2); + /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3); + /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4); + /* (10) trans_opt ::= */ yytestcase(yyruleno==10); + /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11); + /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12); + /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20); + /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21); + /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25); + /* (34) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==34); + /* (35) columnlist ::= column */ yytestcase(yyruleno==35); + /* (44) type ::= */ yytestcase(yyruleno==44); + /* (51) signed ::= plus_num */ yytestcase(yyruleno==51); + /* (52) signed ::= minus_num */ yytestcase(yyruleno==52); + /* (53) carglist ::= carglist carg */ yytestcase(yyruleno==53); + /* (54) carglist ::= */ yytestcase(yyruleno==54); + /* (55) carg ::= CONSTRAINT nm ccons */ yytestcase(yyruleno==55); + /* (56) carg ::= ccons */ yytestcase(yyruleno==56); + /* (62) ccons ::= NULL onconf */ yytestcase(yyruleno==62); + /* (90) conslist ::= conslist COMMA tcons */ yytestcase(yyruleno==90); + /* (91) conslist ::= conslist tcons */ yytestcase(yyruleno==91); + /* (92) conslist ::= tcons */ yytestcase(yyruleno==92); + /* (93) tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==93); + /* (268) plus_opt ::= PLUS */ yytestcase(yyruleno==268); + /* (269) plus_opt ::= */ yytestcase(yyruleno==269); + /* (279) foreach_clause ::= */ yytestcase(yyruleno==279); + /* (280) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==280); + /* (287) tridxby ::= */ yytestcase(yyruleno==287); + /* (305) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==305); + /* (306) database_kw_opt ::= */ yytestcase(yyruleno==306); + /* (314) kwcolumn_opt ::= */ yytestcase(yyruleno==314); + /* (315) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==315); + /* (319) vtabarglist ::= vtabarg */ yytestcase(yyruleno==319); + /* (320) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==320); + /* (322) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==322); + /* (326) anylist ::= */ yytestcase(yyruleno==326); + /* (327) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==327); + /* (328) anylist ::= anylist ANY */ yytestcase(yyruleno==328); + break; + }; + yygoto = yyRuleInfo[yyruleno].lhs; + yysize = yyRuleInfo[yyruleno].nrhs; + yypParser->yyidx -= yysize; + yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto); + if( yyact < YYNSTATE ){ +#ifdef NDEBUG + /* If we are not debugging and the reduce action popped at least + ** one element off the stack, then we can push the new element back + ** onto the stack here, and skip the stack overflow test in yy_shift(). + ** That gives a significant speed improvement. */ + if( yysize ){ + yypParser->yyidx++; + yymsp -= yysize-1; + yymsp->stateno = (YYACTIONTYPE)yyact; + yymsp->major = (YYCODETYPE)yygoto; + yymsp->minor = yygotominor; + }else +#endif + { + yy_shift(yypParser,yyact,yygoto,&yygotominor); + } + }else{ + assert( yyact == YYNSTATE + YYNRULE + 1 ); + yy_accept(yypParser); + } +} + +/* +** The following code executes when the parse fails +*/ +#ifndef YYNOERRORRECOVERY +static void yy_parse_failed( + yyParser *yypParser /* The parser */ +){ + sqlite3ParserARG_FETCH; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt); + } +#endif + while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); + /* Here code is inserted which will be executed whenever the + ** parser fails */ + sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */ +} +#endif /* YYNOERRORRECOVERY */ + +/* +** The following code executes when a syntax error first occurs. +*/ +static void yy_syntax_error( + yyParser *yypParser, /* The parser */ + int yymajor, /* The major type of the error token */ + YYMINORTYPE yyminor /* The minor type of the error token */ +){ + sqlite3ParserARG_FETCH; +#define TOKEN (yyminor.yy0) + + UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ + assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */ + sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); + pParse->parseError = 1; + sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */ +} + +/* +** The following is executed when the parser accepts +*/ +static void yy_accept( + yyParser *yypParser /* The parser */ +){ + sqlite3ParserARG_FETCH; +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt); + } +#endif + while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser); + /* Here code is inserted which will be executed whenever the + ** parser accepts */ + sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */ +} + +/* The main parser program. +** The first argument is a pointer to a structure obtained from +** "sqlite3ParserAlloc" which describes the current state of the parser. +** The second argument is the major token number. The third is +** the minor token. The fourth optional argument is whatever the +** user wants (and specified in the grammar) and is available for +** use by the action routines. +** +** Inputs: +**
      +**
    • A pointer to the parser (an opaque structure.) +**
    • The major token number. +**
    • The minor token number. +**
    • An option argument of a grammar-specified type. +**
    +** +** Outputs: +** None. +*/ +SQLITE_PRIVATE void sqlite3Parser( + void *yyp, /* The parser */ + int yymajor, /* The major token code number */ + sqlite3ParserTOKENTYPE yyminor /* The value for the token */ + sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */ +){ + YYMINORTYPE yyminorunion; + int yyact; /* The parser action. */ + int yyendofinput; /* True if we are at the end of input */ +#ifdef YYERRORSYMBOL + int yyerrorhit = 0; /* True if yymajor has invoked an error */ +#endif + yyParser *yypParser; /* The parser */ + + /* (re)initialize the parser, if necessary */ + yypParser = (yyParser*)yyp; + if( yypParser->yyidx<0 ){ +#if YYSTACKDEPTH<=0 + if( yypParser->yystksz <=0 ){ + /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/ + yyminorunion = yyzerominor; + yyStackOverflow(yypParser, &yyminorunion); + return; + } +#endif + yypParser->yyidx = 0; + yypParser->yyerrcnt = -1; + yypParser->yystack[0].stateno = 0; + yypParser->yystack[0].major = 0; + } + yyminorunion.yy0 = yyminor; + yyendofinput = (yymajor==0); + sqlite3ParserARG_STORE; + +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]); + } +#endif + + do{ + yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor); + if( yyactyyerrcnt--; + yymajor = YYNOCODE; + }else if( yyact < YYNSTATE + YYNRULE ){ + yy_reduce(yypParser,yyact-YYNSTATE); + }else{ + assert( yyact == YY_ERROR_ACTION ); +#ifdef YYERRORSYMBOL + int yymx; +#endif +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt); + } +#endif +#ifdef YYERRORSYMBOL + /* A syntax error has occurred. + ** The response to an error depends upon whether or not the + ** grammar defines an error token "ERROR". + ** + ** This is what we do if the grammar does define ERROR: + ** + ** * Call the %syntax_error function. + ** + ** * Begin popping the stack until we enter a state where + ** it is legal to shift the error symbol, then shift + ** the error symbol. + ** + ** * Set the error count to three. + ** + ** * Begin accepting and shifting new tokens. No new error + ** processing will occur until three tokens have been + ** shifted successfully. + ** + */ + if( yypParser->yyerrcnt<0 ){ + yy_syntax_error(yypParser,yymajor,yyminorunion); + } + yymx = yypParser->yystack[yypParser->yyidx].major; + if( yymx==YYERRORSYMBOL || yyerrorhit ){ +#ifndef NDEBUG + if( yyTraceFILE ){ + fprintf(yyTraceFILE,"%sDiscard input token %s\n", + yyTracePrompt,yyTokenName[yymajor]); + } +#endif + yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion); + yymajor = YYNOCODE; + }else{ + while( + yypParser->yyidx >= 0 && + yymx != YYERRORSYMBOL && + (yyact = yy_find_reduce_action( + yypParser->yystack[yypParser->yyidx].stateno, + YYERRORSYMBOL)) >= YYNSTATE + ){ + yy_pop_parser_stack(yypParser); + } + if( yypParser->yyidx < 0 || yymajor==0 ){ + yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); + yy_parse_failed(yypParser); + yymajor = YYNOCODE; + }else if( yymx!=YYERRORSYMBOL ){ + YYMINORTYPE u2; + u2.YYERRSYMDT = 0; + yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2); + } + } + yypParser->yyerrcnt = 3; + yyerrorhit = 1; +#elif defined(YYNOERRORRECOVERY) + /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to + ** do any kind of error recovery. Instead, simply invoke the syntax + ** error routine and continue going as if nothing had happened. + ** + ** Applications can set this macro (for example inside %include) if + ** they intend to abandon the parse upon the first syntax error seen. + */ + yy_syntax_error(yypParser,yymajor,yyminorunion); + yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); + yymajor = YYNOCODE; + +#else /* YYERRORSYMBOL is not defined */ + /* This is what we do if the grammar does not define ERROR: + ** + ** * Report an error message, and throw away the input token. + ** + ** * If the input token is $, then fail the parse. + ** + ** As before, subsequent error messages are suppressed until + ** three input tokens have been successfully shifted. + */ + if( yypParser->yyerrcnt<=0 ){ + yy_syntax_error(yypParser,yymajor,yyminorunion); + } + yypParser->yyerrcnt = 3; + yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion); + if( yyendofinput ){ + yy_parse_failed(yypParser); + } + yymajor = YYNOCODE; +#endif + } + }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 ); + return; +} + +/************** End of parse.c ***********************************************/ +/************** Begin file tokenize.c ****************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** An tokenizer for SQL +** +** This file contains C code that splits an SQL input string up into +** individual tokens and sends those tokens one-by-one over to the +** parser for analysis. +*/ + +/* +** The charMap() macro maps alphabetic characters into their +** lower-case ASCII equivalent. On ASCII machines, this is just +** an upper-to-lower case map. On EBCDIC machines we also need +** to adjust the encoding. Only alphabetic characters and underscores +** need to be translated. +*/ +#ifdef SQLITE_ASCII +# define charMap(X) sqlite3UpperToLower[(unsigned char)X] +#endif +#ifdef SQLITE_EBCDIC +# define charMap(X) ebcdicToAscii[(unsigned char)X] +const unsigned char ebcdicToAscii[] = { +/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 3x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 4x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 5x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 95, 0, 0, /* 6x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 7x */ + 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* 8x */ + 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* 9x */ + 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ax */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ + 0, 97, 98, 99,100,101,102,103,104,105, 0, 0, 0, 0, 0, 0, /* Cx */ + 0,106,107,108,109,110,111,112,113,114, 0, 0, 0, 0, 0, 0, /* Dx */ + 0, 0,115,116,117,118,119,120,121,122, 0, 0, 0, 0, 0, 0, /* Ex */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Fx */ +}; +#endif + +/* +** The sqlite3KeywordCode function looks up an identifier to determine if +** it is a keyword. If it is a keyword, the token code of that keyword is +** returned. If the input is not a keyword, TK_ID is returned. +** +** The implementation of this routine was generated by a program, +** mkkeywordhash.h, located in the tool subdirectory of the distribution. +** The output of the mkkeywordhash.c program is written into a file +** named keywordhash.h and then included into this source file by +** the #include below. +*/ +/************** Include keywordhash.h in the middle of tokenize.c ************/ +/************** Begin file keywordhash.h *************************************/ +/***** This file contains automatically generated code ****** +** +** The code in this file has been automatically generated by +** +** sqlite/tool/mkkeywordhash.c +** +** The code in this file implements a function that determines whether +** or not a given identifier is really an SQL keyword. The same thing +** might be implemented more directly using a hand-written hash table. +** But by using this automatically generated code, the size of the code +** is substantially reduced. This is important for embedded applications +** on platforms with limited memory. +*/ +/* Hash score: 175 */ +static int keywordCode(const char *z, int n){ + /* zText[] encodes 811 bytes of keywords in 541 bytes */ + /* REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT */ + /* ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE */ + /* XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY */ + /* UNIQUERYATTACHAVINGROUPDATEBEGINNERELEASEBETWEENOTNULLIKE */ + /* CASCADELETECASECOLLATECREATECURRENT_DATEDETACHIMMEDIATEJOIN */ + /* SERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHENWHERENAME */ + /* AFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMITCONFLICTCROSS */ + /* CURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAILFROMFULLGLOBYIF */ + /* ISNULLORDERESTRICTOUTERIGHTROLLBACKROWUNIONUSINGVACUUMVIEW */ + /* INITIALLY */ + static const char zText[540] = { + 'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H', + 'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G', + 'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A', + 'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F', + 'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N', + 'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I', + 'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E', + 'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E', + 'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T', + 'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q', + 'U','E','R','Y','A','T','T','A','C','H','A','V','I','N','G','R','O','U', + 'P','D','A','T','E','B','E','G','I','N','N','E','R','E','L','E','A','S', + 'E','B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C', + 'A','S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L', + 'A','T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D', + 'A','T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E', + 'J','O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A', + 'L','Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U', + 'E','S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W', + 'H','E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C', + 'E','A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R', + 'E','M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M', + 'M','I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U', + 'R','R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M', + 'A','R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T', + 'D','R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L', + 'O','B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S', + 'T','R','I','C','T','O','U','T','E','R','I','G','H','T','R','O','L','L', + 'B','A','C','K','R','O','W','U','N','I','O','N','U','S','I','N','G','V', + 'A','C','U','U','M','V','I','E','W','I','N','I','T','I','A','L','L','Y', + }; + static const unsigned char aHash[127] = { + 72, 101, 114, 70, 0, 45, 0, 0, 78, 0, 73, 0, 0, + 42, 12, 74, 15, 0, 113, 81, 50, 108, 0, 19, 0, 0, + 118, 0, 116, 111, 0, 22, 89, 0, 9, 0, 0, 66, 67, + 0, 65, 6, 0, 48, 86, 98, 0, 115, 97, 0, 0, 44, + 0, 99, 24, 0, 17, 0, 119, 49, 23, 0, 5, 106, 25, + 92, 0, 0, 121, 102, 56, 120, 53, 28, 51, 0, 87, 0, + 96, 26, 0, 95, 0, 0, 0, 91, 88, 93, 84, 105, 14, + 39, 104, 0, 77, 0, 18, 85, 107, 32, 0, 117, 76, 109, + 58, 46, 80, 0, 0, 90, 40, 0, 112, 0, 36, 0, 0, + 29, 0, 82, 59, 60, 0, 20, 57, 0, 52, + }; + static const unsigned char aNext[121] = { + 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 2, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, + 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 33, 0, 21, 0, 0, 0, 43, 3, 47, + 0, 0, 0, 0, 30, 0, 54, 0, 38, 0, 0, 0, 1, + 62, 0, 0, 63, 0, 41, 0, 0, 0, 0, 0, 0, 0, + 61, 0, 0, 0, 0, 31, 55, 16, 34, 10, 0, 0, 0, + 0, 0, 0, 0, 11, 68, 75, 0, 8, 0, 100, 94, 0, + 103, 0, 83, 0, 71, 0, 0, 110, 27, 37, 69, 79, 0, + 35, 64, 0, 0, + }; + static const unsigned char aLen[121] = { + 7, 7, 5, 4, 6, 4, 5, 3, 6, 7, 3, 6, 6, + 7, 7, 3, 8, 2, 6, 5, 4, 4, 3, 10, 4, 6, + 11, 6, 2, 7, 5, 5, 9, 6, 9, 9, 7, 10, 10, + 4, 6, 2, 3, 9, 4, 2, 6, 5, 6, 6, 5, 6, + 5, 5, 7, 7, 7, 3, 2, 4, 4, 7, 3, 6, 4, + 7, 6, 12, 6, 9, 4, 6, 5, 4, 7, 6, 5, 6, + 7, 5, 4, 5, 6, 5, 7, 3, 7, 13, 2, 2, 4, + 6, 6, 8, 5, 17, 12, 7, 8, 8, 2, 4, 4, 4, + 4, 4, 2, 2, 6, 5, 8, 5, 5, 8, 3, 5, 5, + 6, 4, 9, 3, + }; + static const unsigned short int aOffset[121] = { + 0, 2, 2, 8, 9, 14, 16, 20, 23, 25, 25, 29, 33, + 36, 41, 46, 48, 53, 54, 59, 62, 65, 67, 69, 78, 81, + 86, 91, 95, 96, 101, 105, 109, 117, 122, 128, 136, 142, 152, + 159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 189, 194, 197, + 203, 206, 210, 217, 223, 223, 223, 226, 229, 233, 234, 238, 244, + 248, 255, 261, 273, 279, 288, 290, 296, 301, 303, 310, 315, 320, + 326, 332, 337, 341, 344, 350, 354, 361, 363, 370, 372, 374, 383, + 387, 393, 399, 407, 412, 412, 428, 435, 442, 443, 450, 454, 458, + 462, 466, 469, 471, 473, 479, 483, 491, 495, 500, 508, 511, 516, + 521, 527, 531, 536, + }; + static const unsigned char aCode[121] = { + TK_REINDEX, TK_INDEXED, TK_INDEX, TK_DESC, TK_ESCAPE, + TK_EACH, TK_CHECK, TK_KEY, TK_BEFORE, TK_FOREIGN, + TK_FOR, TK_IGNORE, TK_LIKE_KW, TK_EXPLAIN, TK_INSTEAD, + TK_ADD, TK_DATABASE, TK_AS, TK_SELECT, TK_TABLE, + TK_JOIN_KW, TK_THEN, TK_END, TK_DEFERRABLE, TK_ELSE, + TK_EXCEPT, TK_TRANSACTION,TK_ACTION, TK_ON, TK_JOIN_KW, + TK_ALTER, TK_RAISE, TK_EXCLUSIVE, TK_EXISTS, TK_SAVEPOINT, + TK_INTERSECT, TK_TRIGGER, TK_REFERENCES, TK_CONSTRAINT, TK_INTO, + TK_OFFSET, TK_OF, TK_SET, TK_TEMP, TK_TEMP, + TK_OR, TK_UNIQUE, TK_QUERY, TK_ATTACH, TK_HAVING, + TK_GROUP, TK_UPDATE, TK_BEGIN, TK_JOIN_KW, TK_RELEASE, + TK_BETWEEN, TK_NOTNULL, TK_NOT, TK_NO, TK_NULL, + TK_LIKE_KW, TK_CASCADE, TK_ASC, TK_DELETE, TK_CASE, + TK_COLLATE, TK_CREATE, TK_CTIME_KW, TK_DETACH, TK_IMMEDIATE, + TK_JOIN, TK_INSERT, TK_MATCH, TK_PLAN, TK_ANALYZE, + TK_PRAGMA, TK_ABORT, TK_VALUES, TK_VIRTUAL, TK_LIMIT, + TK_WHEN, TK_WHERE, TK_RENAME, TK_AFTER, TK_REPLACE, + TK_AND, TK_DEFAULT, TK_AUTOINCR, TK_TO, TK_IN, + TK_CAST, TK_COLUMNKW, TK_COMMIT, TK_CONFLICT, TK_JOIN_KW, + TK_CTIME_KW, TK_CTIME_KW, TK_PRIMARY, TK_DEFERRED, TK_DISTINCT, + TK_IS, TK_DROP, TK_FAIL, TK_FROM, TK_JOIN_KW, + TK_LIKE_KW, TK_BY, TK_IF, TK_ISNULL, TK_ORDER, + TK_RESTRICT, TK_JOIN_KW, TK_JOIN_KW, TK_ROLLBACK, TK_ROW, + TK_UNION, TK_USING, TK_VACUUM, TK_VIEW, TK_INITIALLY, + TK_ALL, + }; + int h, i; + if( n<2 ) return TK_ID; + h = ((charMap(z[0])*4) ^ + (charMap(z[n-1])*3) ^ + n) % 127; + for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){ + if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){ + testcase( i==0 ); /* REINDEX */ + testcase( i==1 ); /* INDEXED */ + testcase( i==2 ); /* INDEX */ + testcase( i==3 ); /* DESC */ + testcase( i==4 ); /* ESCAPE */ + testcase( i==5 ); /* EACH */ + testcase( i==6 ); /* CHECK */ + testcase( i==7 ); /* KEY */ + testcase( i==8 ); /* BEFORE */ + testcase( i==9 ); /* FOREIGN */ + testcase( i==10 ); /* FOR */ + testcase( i==11 ); /* IGNORE */ + testcase( i==12 ); /* REGEXP */ + testcase( i==13 ); /* EXPLAIN */ + testcase( i==14 ); /* INSTEAD */ + testcase( i==15 ); /* ADD */ + testcase( i==16 ); /* DATABASE */ + testcase( i==17 ); /* AS */ + testcase( i==18 ); /* SELECT */ + testcase( i==19 ); /* TABLE */ + testcase( i==20 ); /* LEFT */ + testcase( i==21 ); /* THEN */ + testcase( i==22 ); /* END */ + testcase( i==23 ); /* DEFERRABLE */ + testcase( i==24 ); /* ELSE */ + testcase( i==25 ); /* EXCEPT */ + testcase( i==26 ); /* TRANSACTION */ + testcase( i==27 ); /* ACTION */ + testcase( i==28 ); /* ON */ + testcase( i==29 ); /* NATURAL */ + testcase( i==30 ); /* ALTER */ + testcase( i==31 ); /* RAISE */ + testcase( i==32 ); /* EXCLUSIVE */ + testcase( i==33 ); /* EXISTS */ + testcase( i==34 ); /* SAVEPOINT */ + testcase( i==35 ); /* INTERSECT */ + testcase( i==36 ); /* TRIGGER */ + testcase( i==37 ); /* REFERENCES */ + testcase( i==38 ); /* CONSTRAINT */ + testcase( i==39 ); /* INTO */ + testcase( i==40 ); /* OFFSET */ + testcase( i==41 ); /* OF */ + testcase( i==42 ); /* SET */ + testcase( i==43 ); /* TEMPORARY */ + testcase( i==44 ); /* TEMP */ + testcase( i==45 ); /* OR */ + testcase( i==46 ); /* UNIQUE */ + testcase( i==47 ); /* QUERY */ + testcase( i==48 ); /* ATTACH */ + testcase( i==49 ); /* HAVING */ + testcase( i==50 ); /* GROUP */ + testcase( i==51 ); /* UPDATE */ + testcase( i==52 ); /* BEGIN */ + testcase( i==53 ); /* INNER */ + testcase( i==54 ); /* RELEASE */ + testcase( i==55 ); /* BETWEEN */ + testcase( i==56 ); /* NOTNULL */ + testcase( i==57 ); /* NOT */ + testcase( i==58 ); /* NO */ + testcase( i==59 ); /* NULL */ + testcase( i==60 ); /* LIKE */ + testcase( i==61 ); /* CASCADE */ + testcase( i==62 ); /* ASC */ + testcase( i==63 ); /* DELETE */ + testcase( i==64 ); /* CASE */ + testcase( i==65 ); /* COLLATE */ + testcase( i==66 ); /* CREATE */ + testcase( i==67 ); /* CURRENT_DATE */ + testcase( i==68 ); /* DETACH */ + testcase( i==69 ); /* IMMEDIATE */ + testcase( i==70 ); /* JOIN */ + testcase( i==71 ); /* INSERT */ + testcase( i==72 ); /* MATCH */ + testcase( i==73 ); /* PLAN */ + testcase( i==74 ); /* ANALYZE */ + testcase( i==75 ); /* PRAGMA */ + testcase( i==76 ); /* ABORT */ + testcase( i==77 ); /* VALUES */ + testcase( i==78 ); /* VIRTUAL */ + testcase( i==79 ); /* LIMIT */ + testcase( i==80 ); /* WHEN */ + testcase( i==81 ); /* WHERE */ + testcase( i==82 ); /* RENAME */ + testcase( i==83 ); /* AFTER */ + testcase( i==84 ); /* REPLACE */ + testcase( i==85 ); /* AND */ + testcase( i==86 ); /* DEFAULT */ + testcase( i==87 ); /* AUTOINCREMENT */ + testcase( i==88 ); /* TO */ + testcase( i==89 ); /* IN */ + testcase( i==90 ); /* CAST */ + testcase( i==91 ); /* COLUMN */ + testcase( i==92 ); /* COMMIT */ + testcase( i==93 ); /* CONFLICT */ + testcase( i==94 ); /* CROSS */ + testcase( i==95 ); /* CURRENT_TIMESTAMP */ + testcase( i==96 ); /* CURRENT_TIME */ + testcase( i==97 ); /* PRIMARY */ + testcase( i==98 ); /* DEFERRED */ + testcase( i==99 ); /* DISTINCT */ + testcase( i==100 ); /* IS */ + testcase( i==101 ); /* DROP */ + testcase( i==102 ); /* FAIL */ + testcase( i==103 ); /* FROM */ + testcase( i==104 ); /* FULL */ + testcase( i==105 ); /* GLOB */ + testcase( i==106 ); /* BY */ + testcase( i==107 ); /* IF */ + testcase( i==108 ); /* ISNULL */ + testcase( i==109 ); /* ORDER */ + testcase( i==110 ); /* RESTRICT */ + testcase( i==111 ); /* OUTER */ + testcase( i==112 ); /* RIGHT */ + testcase( i==113 ); /* ROLLBACK */ + testcase( i==114 ); /* ROW */ + testcase( i==115 ); /* UNION */ + testcase( i==116 ); /* USING */ + testcase( i==117 ); /* VACUUM */ + testcase( i==118 ); /* VIEW */ + testcase( i==119 ); /* INITIALLY */ + testcase( i==120 ); /* ALL */ + return aCode[i]; + } + } + return TK_ID; +} +SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){ + return keywordCode((char*)z, n); +} +#define SQLITE_N_KEYWORD 121 + +/************** End of keywordhash.h *****************************************/ +/************** Continuing where we left off in tokenize.c *******************/ + + +/* +** If X is a character that can be used in an identifier then +** IdChar(X) will be true. Otherwise it is false. +** +** For ASCII, any character with the high-order bit set is +** allowed in an identifier. For 7-bit characters, +** sqlite3IsIdChar[X] must be 1. +** +** For EBCDIC, the rules are more complex but have the same +** end result. +** +** Ticket #1066. the SQL standard does not allow '$' in the +** middle of identfiers. But many SQL implementations do. +** SQLite will allow '$' in identifiers for compatibility. +** But the feature is undocumented. +*/ +#ifdef SQLITE_ASCII +#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) +#endif +#ifdef SQLITE_EBCDIC +SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = { +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ + 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 4x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, /* 5x */ + 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, /* 6x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, /* 7x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, /* 8x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, /* 9x */ + 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, /* Ax */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* Bx */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Cx */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Dx */ + 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* Ex */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, /* Fx */ +}; +#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) +#endif + + +/* +** Return the length of the token that begins at z[0]. +** Store the token type in *tokenType before returning. +*/ +SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){ + int i, c; + switch( *z ){ + case ' ': case '\t': case '\n': case '\f': case '\r': { + testcase( z[0]==' ' ); + testcase( z[0]=='\t' ); + testcase( z[0]=='\n' ); + testcase( z[0]=='\f' ); + testcase( z[0]=='\r' ); + for(i=1; sqlite3Isspace(z[i]); i++){} + *tokenType = TK_SPACE; + return i; + } + case '-': { + if( z[1]=='-' ){ + /* IMP: R-15891-05542 -- syntax diagram for comments */ + for(i=2; (c=z[i])!=0 && c!='\n'; i++){} + *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ + return i; + } + *tokenType = TK_MINUS; + return 1; + } + case '(': { + *tokenType = TK_LP; + return 1; + } + case ')': { + *tokenType = TK_RP; + return 1; + } + case ';': { + *tokenType = TK_SEMI; + return 1; + } + case '+': { + *tokenType = TK_PLUS; + return 1; + } + case '*': { + *tokenType = TK_STAR; + return 1; + } + case '/': { + if( z[1]!='*' || z[2]==0 ){ + *tokenType = TK_SLASH; + return 1; + } + /* IMP: R-15891-05542 -- syntax diagram for comments */ + for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){} + if( c ) i++; + *tokenType = TK_SPACE; /* IMP: R-22934-25134 */ + return i; + } + case '%': { + *tokenType = TK_REM; + return 1; + } + case '=': { + *tokenType = TK_EQ; + return 1 + (z[1]=='='); + } + case '<': { + if( (c=z[1])=='=' ){ + *tokenType = TK_LE; + return 2; + }else if( c=='>' ){ + *tokenType = TK_NE; + return 2; + }else if( c=='<' ){ + *tokenType = TK_LSHIFT; + return 2; + }else{ + *tokenType = TK_LT; + return 1; + } + } + case '>': { + if( (c=z[1])=='=' ){ + *tokenType = TK_GE; + return 2; + }else if( c=='>' ){ + *tokenType = TK_RSHIFT; + return 2; + }else{ + *tokenType = TK_GT; + return 1; + } + } + case '!': { + if( z[1]!='=' ){ + *tokenType = TK_ILLEGAL; + return 2; + }else{ + *tokenType = TK_NE; + return 2; + } + } + case '|': { + if( z[1]!='|' ){ + *tokenType = TK_BITOR; + return 1; + }else{ + *tokenType = TK_CONCAT; + return 2; + } + } + case ',': { + *tokenType = TK_COMMA; + return 1; + } + case '&': { + *tokenType = TK_BITAND; + return 1; + } + case '~': { + *tokenType = TK_BITNOT; + return 1; + } + case '`': + case '\'': + case '"': { + int delim = z[0]; + testcase( delim=='`' ); + testcase( delim=='\'' ); + testcase( delim=='"' ); + for(i=1; (c=z[i])!=0; i++){ + if( c==delim ){ + if( z[i+1]==delim ){ + i++; + }else{ + break; + } + } + } + if( c=='\'' ){ + *tokenType = TK_STRING; + return i+1; + }else if( c!=0 ){ + *tokenType = TK_ID; + return i+1; + }else{ + *tokenType = TK_ILLEGAL; + return i; + } + } + case '.': { +#ifndef SQLITE_OMIT_FLOATING_POINT + if( !sqlite3Isdigit(z[1]) ) +#endif + { + *tokenType = TK_DOT; + return 1; + } + /* If the next character is a digit, this is a floating point + ** number that begins with ".". Fall thru into the next case */ + } + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': { + testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' ); + testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' ); + testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' ); + testcase( z[0]=='9' ); + *tokenType = TK_INTEGER; + for(i=0; sqlite3Isdigit(z[i]); i++){} +#ifndef SQLITE_OMIT_FLOATING_POINT + if( z[i]=='.' ){ + i++; + while( sqlite3Isdigit(z[i]) ){ i++; } + *tokenType = TK_FLOAT; + } + if( (z[i]=='e' || z[i]=='E') && + ( sqlite3Isdigit(z[i+1]) + || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2])) + ) + ){ + i += 2; + while( sqlite3Isdigit(z[i]) ){ i++; } + *tokenType = TK_FLOAT; + } +#endif + while( IdChar(z[i]) ){ + *tokenType = TK_ILLEGAL; + i++; + } + return i; + } + case '[': { + for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){} + *tokenType = c==']' ? TK_ID : TK_ILLEGAL; + return i; + } + case '?': { + *tokenType = TK_VARIABLE; + for(i=1; sqlite3Isdigit(z[i]); i++){} + return i; + } + case '#': { + for(i=1; sqlite3Isdigit(z[i]); i++){} + if( i>1 ){ + /* Parameters of the form #NNN (where NNN is a number) are used + ** internally by sqlite3NestedParse. */ + *tokenType = TK_REGISTER; + return i; + } + /* Fall through into the next case if the '#' is not followed by + ** a digit. Try to match #AAAA where AAAA is a parameter name. */ + } +#ifndef SQLITE_OMIT_TCL_VARIABLE + case '$': +#endif + case '@': /* For compatibility with MS SQL Server */ + case ':': { + int n = 0; + testcase( z[0]=='$' ); testcase( z[0]=='@' ); testcase( z[0]==':' ); + *tokenType = TK_VARIABLE; + for(i=1; (c=z[i])!=0; i++){ + if( IdChar(c) ){ + n++; +#ifndef SQLITE_OMIT_TCL_VARIABLE + }else if( c=='(' && n>0 ){ + do{ + i++; + }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' ); + if( c==')' ){ + i++; + }else{ + *tokenType = TK_ILLEGAL; + } + break; + }else if( c==':' && z[i+1]==':' ){ + i++; +#endif + }else{ + break; + } + } + if( n==0 ) *tokenType = TK_ILLEGAL; + return i; + } +#ifndef SQLITE_OMIT_BLOB_LITERAL + case 'x': case 'X': { + testcase( z[0]=='x' ); testcase( z[0]=='X' ); + if( z[1]=='\'' ){ + *tokenType = TK_BLOB; + for(i=2; (c=z[i])!=0 && c!='\''; i++){ + if( !sqlite3Isxdigit(c) ){ + *tokenType = TK_ILLEGAL; + } + } + if( i%2 || !c ) *tokenType = TK_ILLEGAL; + if( c ) i++; + return i; + } + /* Otherwise fall through to the next case */ + } +#endif + default: { + if( !IdChar(*z) ){ + break; + } + for(i=1; IdChar(z[i]); i++){} + *tokenType = keywordCode((char*)z, i); + return i; + } + } + *tokenType = TK_ILLEGAL; + return 1; +} + +/* +** Run the parser on the given SQL string. The parser structure is +** passed in. An SQLITE_ status code is returned. If an error occurs +** then an and attempt is made to write an error message into +** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that +** error message. +*/ +SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){ + int nErr = 0; /* Number of errors encountered */ + int i; /* Loop counter */ + void *pEngine; /* The LEMON-generated LALR(1) parser */ + int tokenType; /* type of the next token */ + int lastTokenParsed = -1; /* type of the previous token */ + u8 enableLookaside; /* Saved value of db->lookaside.bEnabled */ + sqlite3 *db = pParse->db; /* The database connection */ + int mxSqlLen; /* Max length of an SQL string */ + + + mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH]; + if( db->activeVdbeCnt==0 ){ + db->u1.isInterrupted = 0; + } + pParse->rc = SQLITE_OK; + pParse->zTail = zSql; + i = 0; + assert( pzErrMsg!=0 ); + pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc); + if( pEngine==0 ){ + db->mallocFailed = 1; + return SQLITE_NOMEM; + } + assert( pParse->pNewTable==0 ); + assert( pParse->pNewTrigger==0 ); + assert( pParse->nVar==0 ); + assert( pParse->nVarExpr==0 ); + assert( pParse->nVarExprAlloc==0 ); + assert( pParse->apVarExpr==0 ); + enableLookaside = db->lookaside.bEnabled; + if( db->lookaside.pStart ) db->lookaside.bEnabled = 1; + while( !db->mallocFailed && zSql[i]!=0 ){ + assert( i>=0 ); + pParse->sLastToken.z = &zSql[i]; + pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType); + i += pParse->sLastToken.n; + if( i>mxSqlLen ){ + pParse->rc = SQLITE_TOOBIG; + break; + } + switch( tokenType ){ + case TK_SPACE: { + if( db->u1.isInterrupted ){ + sqlite3ErrorMsg(pParse, "interrupt"); + pParse->rc = SQLITE_INTERRUPT; + goto abort_parse; + } + break; + } + case TK_ILLEGAL: { + sqlite3DbFree(db, *pzErrMsg); + *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"", + &pParse->sLastToken); + nErr++; + goto abort_parse; + } + case TK_SEMI: { + pParse->zTail = &zSql[i]; + /* Fall thru into the default case */ + } + default: { + sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse); + lastTokenParsed = tokenType; + if( pParse->rc!=SQLITE_OK ){ + goto abort_parse; + } + break; + } + } + } +abort_parse: + if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){ + if( lastTokenParsed!=TK_SEMI ){ + sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse); + pParse->zTail = &zSql[i]; + } + sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse); + } +#ifdef YYTRACKMAXSTACKDEPTH + sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK, + sqlite3ParserStackPeak(pEngine) + ); +#endif /* YYDEBUG */ + sqlite3ParserFree(pEngine, sqlite3_free); + db->lookaside.bEnabled = enableLookaside; + if( db->mallocFailed ){ + pParse->rc = SQLITE_NOMEM; + } + if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){ + sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc)); + } + assert( pzErrMsg!=0 ); + if( pParse->zErrMsg ){ + *pzErrMsg = pParse->zErrMsg; + sqlite3_log(pParse->rc, "%s", *pzErrMsg); + pParse->zErrMsg = 0; + nErr++; + } + if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){ + sqlite3VdbeDelete(pParse->pVdbe); + pParse->pVdbe = 0; + } +#ifndef SQLITE_OMIT_SHARED_CACHE + if( pParse->nested==0 ){ + sqlite3DbFree(db, pParse->aTableLock); + pParse->aTableLock = 0; + pParse->nTableLock = 0; + } +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + sqlite3_free(pParse->apVtabLock); +#endif + + if( !IN_DECLARE_VTAB ){ + /* If the pParse->declareVtab flag is set, do not delete any table + ** structure built up in pParse->pNewTable. The calling code (see vtab.c) + ** will take responsibility for freeing the Table structure. + */ + sqlite3DeleteTable(db, pParse->pNewTable); + } + + sqlite3DeleteTrigger(db, pParse->pNewTrigger); + sqlite3DbFree(db, pParse->apVarExpr); + sqlite3DbFree(db, pParse->aAlias); + while( pParse->pAinc ){ + AutoincInfo *p = pParse->pAinc; + pParse->pAinc = p->pNext; + sqlite3DbFree(db, p); + } + while( pParse->pZombieTab ){ + Table *p = pParse->pZombieTab; + pParse->pZombieTab = p->pNextZombie; + sqlite3DeleteTable(db, p); + } + if( nErr>0 && pParse->rc==SQLITE_OK ){ + pParse->rc = SQLITE_ERROR; + } + return nErr; +} + +/************** End of tokenize.c ********************************************/ +/************** Begin file complete.c ****************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** An tokenizer for SQL +** +** This file contains C code that implements the sqlite3_complete() API. +** This code used to be part of the tokenizer.c source file. But by +** separating it out, the code will be automatically omitted from +** static links that do not use it. +*/ +#ifndef SQLITE_OMIT_COMPLETE + +/* +** This is defined in tokenize.c. We just have to import the definition. +*/ +#ifndef SQLITE_AMALGAMATION +#ifdef SQLITE_ASCII +#define IdChar(C) ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0) +#endif +#ifdef SQLITE_EBCDIC +SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[]; +#define IdChar(C) (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40])) +#endif +#endif /* SQLITE_AMALGAMATION */ + + +/* +** Token types used by the sqlite3_complete() routine. See the header +** comments on that procedure for additional information. +*/ +#define tkSEMI 0 +#define tkWS 1 +#define tkOTHER 2 +#ifndef SQLITE_OMIT_TRIGGER +#define tkEXPLAIN 3 +#define tkCREATE 4 +#define tkTEMP 5 +#define tkTRIGGER 6 +#define tkEND 7 +#endif + +/* +** Return TRUE if the given SQL string ends in a semicolon. +** +** Special handling is require for CREATE TRIGGER statements. +** Whenever the CREATE TRIGGER keywords are seen, the statement +** must end with ";END;". +** +** This implementation uses a state machine with 8 states: +** +** (0) INVALID We have not yet seen a non-whitespace character. +** +** (1) START At the beginning or end of an SQL statement. This routine +** returns 1 if it ends in the START state and 0 if it ends +** in any other state. +** +** (2) NORMAL We are in the middle of statement which ends with a single +** semicolon. +** +** (3) EXPLAIN The keyword EXPLAIN has been seen at the beginning of +** a statement. +** +** (4) CREATE The keyword CREATE has been seen at the beginning of a +** statement, possibly preceeded by EXPLAIN and/or followed by +** TEMP or TEMPORARY +** +** (5) TRIGGER We are in the middle of a trigger definition that must be +** ended by a semicolon, the keyword END, and another semicolon. +** +** (6) SEMI We've seen the first semicolon in the ";END;" that occurs at +** the end of a trigger definition. +** +** (7) END We've seen the ";END" of the ";END;" that occurs at the end +** of a trigger difinition. +** +** Transitions between states above are determined by tokens extracted +** from the input. The following tokens are significant: +** +** (0) tkSEMI A semicolon. +** (1) tkWS Whitespace. +** (2) tkOTHER Any other SQL token. +** (3) tkEXPLAIN The "explain" keyword. +** (4) tkCREATE The "create" keyword. +** (5) tkTEMP The "temp" or "temporary" keyword. +** (6) tkTRIGGER The "trigger" keyword. +** (7) tkEND The "end" keyword. +** +** Whitespace never causes a state transition and is always ignored. +** This means that a SQL string of all whitespace is invalid. +** +** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed +** to recognize the end of a trigger can be omitted. All we have to do +** is look for a semicolon that is not part of an string or comment. +*/ +SQLITE_API int sqlite3_complete(const char *zSql){ + u8 state = 0; /* Current state, using numbers defined in header comment */ + u8 token; /* Value of the next token */ + +#ifndef SQLITE_OMIT_TRIGGER + /* A complex statement machine used to detect the end of a CREATE TRIGGER + ** statement. This is the normal case. + */ + static const u8 trans[8][8] = { + /* Token: */ + /* State: ** SEMI WS OTHER EXPLAIN CREATE TEMP TRIGGER END */ + /* 0 INVALID: */ { 1, 0, 2, 3, 4, 2, 2, 2, }, + /* 1 START: */ { 1, 1, 2, 3, 4, 2, 2, 2, }, + /* 2 NORMAL: */ { 1, 2, 2, 2, 2, 2, 2, 2, }, + /* 3 EXPLAIN: */ { 1, 3, 3, 2, 4, 2, 2, 2, }, + /* 4 CREATE: */ { 1, 4, 2, 2, 2, 4, 5, 2, }, + /* 5 TRIGGER: */ { 6, 5, 5, 5, 5, 5, 5, 5, }, + /* 6 SEMI: */ { 6, 6, 5, 5, 5, 5, 5, 7, }, + /* 7 END: */ { 1, 7, 5, 5, 5, 5, 5, 5, }, + }; +#else + /* If triggers are not supported by this compile then the statement machine + ** used to detect the end of a statement is much simplier + */ + static const u8 trans[3][3] = { + /* Token: */ + /* State: ** SEMI WS OTHER */ + /* 0 INVALID: */ { 1, 0, 2, }, + /* 1 START: */ { 1, 1, 2, }, + /* 2 NORMAL: */ { 1, 2, 2, }, + }; +#endif /* SQLITE_OMIT_TRIGGER */ + + while( *zSql ){ + switch( *zSql ){ + case ';': { /* A semicolon */ + token = tkSEMI; + break; + } + case ' ': + case '\r': + case '\t': + case '\n': + case '\f': { /* White space is ignored */ + token = tkWS; + break; + } + case '/': { /* C-style comments */ + if( zSql[1]!='*' ){ + token = tkOTHER; + break; + } + zSql += 2; + while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; } + if( zSql[0]==0 ) return 0; + zSql++; + token = tkWS; + break; + } + case '-': { /* SQL-style comments from "--" to end of line */ + if( zSql[1]!='-' ){ + token = tkOTHER; + break; + } + while( *zSql && *zSql!='\n' ){ zSql++; } + if( *zSql==0 ) return state==1; + token = tkWS; + break; + } + case '[': { /* Microsoft-style identifiers in [...] */ + zSql++; + while( *zSql && *zSql!=']' ){ zSql++; } + if( *zSql==0 ) return 0; + token = tkOTHER; + break; + } + case '`': /* Grave-accent quoted symbols used by MySQL */ + case '"': /* single- and double-quoted strings */ + case '\'': { + int c = *zSql; + zSql++; + while( *zSql && *zSql!=c ){ zSql++; } + if( *zSql==0 ) return 0; + token = tkOTHER; + break; + } + default: { +#ifdef SQLITE_EBCDIC + unsigned char c; +#endif + if( IdChar((u8)*zSql) ){ + /* Keywords and unquoted identifiers */ + int nId; + for(nId=1; IdChar(zSql[nId]); nId++){} +#ifdef SQLITE_OMIT_TRIGGER + token = tkOTHER; +#else + switch( *zSql ){ + case 'c': case 'C': { + if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){ + token = tkCREATE; + }else{ + token = tkOTHER; + } + break; + } + case 't': case 'T': { + if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){ + token = tkTRIGGER; + }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){ + token = tkTEMP; + }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){ + token = tkTEMP; + }else{ + token = tkOTHER; + } + break; + } + case 'e': case 'E': { + if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){ + token = tkEND; + }else +#ifndef SQLITE_OMIT_EXPLAIN + if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){ + token = tkEXPLAIN; + }else +#endif + { + token = tkOTHER; + } + break; + } + default: { + token = tkOTHER; + break; + } + } +#endif /* SQLITE_OMIT_TRIGGER */ + zSql += nId-1; + }else{ + /* Operators and special symbols */ + token = tkOTHER; + } + break; + } + } + state = trans[state][token]; + zSql++; + } + return state==1; +} + +#ifndef SQLITE_OMIT_UTF16 +/* +** This routine is the same as the sqlite3_complete() routine described +** above, except that the parameter is required to be UTF-16 encoded, not +** UTF-8. +*/ +SQLITE_API int sqlite3_complete16(const void *zSql){ + sqlite3_value *pVal; + char const *zSql8; + int rc = SQLITE_NOMEM; + +#ifndef SQLITE_OMIT_AUTOINIT + rc = sqlite3_initialize(); + if( rc ) return rc; +#endif + pVal = sqlite3ValueNew(0); + sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC); + zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8); + if( zSql8 ){ + rc = sqlite3_complete(zSql8); + }else{ + rc = SQLITE_NOMEM; + } + sqlite3ValueFree(pVal); + return sqlite3ApiExit(0, rc); +} +#endif /* SQLITE_OMIT_UTF16 */ +#endif /* SQLITE_OMIT_COMPLETE */ + +/************** End of complete.c ********************************************/ +/************** Begin file main.c ********************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Main file for the SQLite library. The routines in this file +** implement the programmer interface to the library. Routines in +** other files are for internal use by SQLite and should not be +** accessed by users of the library. +*/ + +#ifdef SQLITE_ENABLE_FTS3 +/************** Include fts3.h in the middle of main.c ***********************/ +/************** Begin file fts3.h ********************************************/ +/* +** 2006 Oct 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This header file is used by programs that want to link against the +** FTS3 library. All it does is declare the sqlite3Fts3Init() interface. +*/ + +#if 0 +extern "C" { +#endif /* __cplusplus */ + +SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db); + +#if 0 +} /* extern "C" */ +#endif /* __cplusplus */ + +/************** End of fts3.h ************************************************/ +/************** Continuing where we left off in main.c ***********************/ +#endif +#ifdef SQLITE_ENABLE_RTREE +/************** Include rtree.h in the middle of main.c **********************/ +/************** Begin file rtree.h *******************************************/ +/* +** 2008 May 26 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This header file is used by programs that want to link against the +** RTREE library. All it does is declare the sqlite3RtreeInit() interface. +*/ + +#if 0 +extern "C" { +#endif /* __cplusplus */ + +SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db); + +#if 0 +} /* extern "C" */ +#endif /* __cplusplus */ + +/************** End of rtree.h ***********************************************/ +/************** Continuing where we left off in main.c ***********************/ +#endif +#ifdef SQLITE_ENABLE_ICU +/************** Include sqliteicu.h in the middle of main.c ******************/ +/************** Begin file sqliteicu.h ***************************************/ +/* +** 2008 May 26 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This header file is used by programs that want to link against the +** ICU extension. All it does is declare the sqlite3IcuInit() interface. +*/ + +#if 0 +extern "C" { +#endif /* __cplusplus */ + +SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db); + +#if 0 +} /* extern "C" */ +#endif /* __cplusplus */ + + +/************** End of sqliteicu.h *******************************************/ +/************** Continuing where we left off in main.c ***********************/ +#endif + +//#ifndef SQLITE_AMALGAMATION +/* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant +** contains the text of SQLITE_VERSION macro. +*/ +SQLITE_API const char sqlite3_version[] = SQLITE_VERSION; +//#endif + +/* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns +** a pointer to the to the sqlite3_version[] string constant. +*/ +SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; } + +/* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a +** pointer to a string constant whose value is the same as the +** SQLITE_SOURCE_ID C preprocessor macro. +*/ +SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; } + +/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function +** returns an integer equal to SQLITE_VERSION_NUMBER. +*/ +SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; } + +/* IMPLEMENTATION-OF: R-54823-41343 The sqlite3_threadsafe() function returns +** zero if and only if SQLite was compiled mutexing code omitted due to +** the SQLITE_THREADSAFE compile-time option being set to 0. +*/ +SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; } + +#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE) +/* +** If the following function pointer is not NULL and if +** SQLITE_ENABLE_IOTRACE is enabled, then messages describing +** I/O active are written using this function. These messages +** are intended for debugging activity only. +*/ +SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0; +#endif + +/* +** If the following global variable points to a string which is the +** name of a directory, then that directory will be used to store +** temporary files. +** +** See also the "PRAGMA temp_store_directory" SQL command. +*/ +SQLITE_API char *sqlite3_temp_directory = 0; + +/* +** Initialize SQLite. +** +** This routine must be called to initialize the memory allocation, +** VFS, and mutex subsystems prior to doing any serious work with +** SQLite. But as long as you do not compile with SQLITE_OMIT_AUTOINIT +** this routine will be called automatically by key routines such as +** sqlite3_open(). +** +** This routine is a no-op except on its very first call for the process, +** or for the first call after a call to sqlite3_shutdown. +** +** The first thread to call this routine runs the initialization to +** completion. If subsequent threads call this routine before the first +** thread has finished the initialization process, then the subsequent +** threads must block until the first thread finishes with the initialization. +** +** The first thread might call this routine recursively. Recursive +** calls to this routine should not block, of course. Otherwise the +** initialization process would never complete. +** +** Let X be the first thread to enter this routine. Let Y be some other +** thread. Then while the initial invocation of this routine by X is +** incomplete, it is required that: +** +** * Calls to this routine from Y must block until the outer-most +** call by X completes. +** +** * Recursive calls to this routine from thread X return immediately +** without blocking. +*/ +SQLITE_API int sqlite3_initialize(void){ + sqlite3_mutex *pMaster; /* The main static mutex */ + int rc; /* Result code */ + +#ifdef SQLITE_OMIT_WSD + rc = sqlite3_wsd_init(4096, 24); + if( rc!=SQLITE_OK ){ + return rc; + } +#endif + + /* If SQLite is already completely initialized, then this call + ** to sqlite3_initialize() should be a no-op. But the initialization + ** must be complete. So isInit must not be set until the very end + ** of this routine. + */ + if( sqlite3GlobalConfig.isInit ) return SQLITE_OK; + + /* Make sure the mutex subsystem is initialized. If unable to + ** initialize the mutex subsystem, return early with the error. + ** If the system is so sick that we are unable to allocate a mutex, + ** there is not much SQLite is going to be able to do. + ** + ** The mutex subsystem must take care of serializing its own + ** initialization. + */ + rc = sqlite3MutexInit(); + if( rc ) return rc; + + /* Initialize the malloc() system and the recursive pInitMutex mutex. + ** This operation is protected by the STATIC_MASTER mutex. Note that + ** MutexAlloc() is called for a static mutex prior to initializing the + ** malloc subsystem - this implies that the allocation of a static + ** mutex must not require support from the malloc subsystem. + */ + pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); + sqlite3_mutex_enter(pMaster); + sqlite3GlobalConfig.isMutexInit = 1; + if( !sqlite3GlobalConfig.isMallocInit ){ + rc = sqlite3MallocInit(); + } + if( rc==SQLITE_OK ){ + sqlite3GlobalConfig.isMallocInit = 1; + if( !sqlite3GlobalConfig.pInitMutex ){ + sqlite3GlobalConfig.pInitMutex = + sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); + if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){ + rc = SQLITE_NOMEM; + } + } + } + if( rc==SQLITE_OK ){ + sqlite3GlobalConfig.nRefInitMutex++; + } + sqlite3_mutex_leave(pMaster); + + /* If rc is not SQLITE_OK at this point, then either the malloc + ** subsystem could not be initialized or the system failed to allocate + ** the pInitMutex mutex. Return an error in either case. */ + if( rc!=SQLITE_OK ){ + return rc; + } + + /* Do the rest of the initialization under the recursive mutex so + ** that we will be able to handle recursive calls into + ** sqlite3_initialize(). The recursive calls normally come through + ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other + ** recursive calls might also be possible. + ** + ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls + ** to the xInit method, so the xInit method need not be threadsafe. + ** + ** The following mutex is what serializes access to the appdef pcache xInit + ** methods. The sqlite3_pcache_methods.xInit() all is embedded in the + ** call to sqlite3PcacheInitialize(). + */ + sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex); + if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){ + FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions); + sqlite3GlobalConfig.inProgress = 1; + memset(pHash, 0, sizeof(sqlite3GlobalFunctions)); + sqlite3RegisterGlobalFunctions(); + if( sqlite3GlobalConfig.isPCacheInit==0 ){ + rc = sqlite3PcacheInitialize(); + } + if( rc==SQLITE_OK ){ + sqlite3GlobalConfig.isPCacheInit = 1; + rc = sqlite3OsInit(); + } + if( rc==SQLITE_OK ){ + sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage, + sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage); + sqlite3GlobalConfig.isInit = 1; + } + sqlite3GlobalConfig.inProgress = 0; + } + sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex); + + /* Go back under the static mutex and clean up the recursive + ** mutex to prevent a resource leak. + */ + sqlite3_mutex_enter(pMaster); + sqlite3GlobalConfig.nRefInitMutex--; + if( sqlite3GlobalConfig.nRefInitMutex<=0 ){ + assert( sqlite3GlobalConfig.nRefInitMutex==0 ); + sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex); + sqlite3GlobalConfig.pInitMutex = 0; + } + sqlite3_mutex_leave(pMaster); + + /* The following is just a sanity check to make sure SQLite has + ** been compiled correctly. It is important to run this code, but + ** we don't want to run it too often and soak up CPU cycles for no + ** reason. So we run it once during initialization. + */ +#ifndef NDEBUG +#ifndef SQLITE_OMIT_FLOATING_POINT + /* This section of code's only "output" is via assert() statements. */ + if ( rc==SQLITE_OK ){ + u64 x = (((u64)1)<<63)-1; + double y; + assert(sizeof(x)==8); + assert(sizeof(x)==sizeof(y)); + memcpy(&y, &x, 8); + assert( sqlite3IsNaN(y) ); + } +#endif +#endif + + return rc; +} + +/* +** Undo the effects of sqlite3_initialize(). Must not be called while +** there are outstanding database connections or memory allocations or +** while any part of SQLite is otherwise in use in any thread. This +** routine is not threadsafe. But it is safe to invoke this routine +** on when SQLite is already shut down. If SQLite is already shut down +** when this routine is invoked, then this routine is a harmless no-op. +*/ +SQLITE_API int sqlite3_shutdown(void){ + if( sqlite3GlobalConfig.isInit ){ + sqlite3_os_end(); + sqlite3_reset_auto_extension(); + sqlite3GlobalConfig.isInit = 0; + } + if( sqlite3GlobalConfig.isPCacheInit ){ + sqlite3PcacheShutdown(); + sqlite3GlobalConfig.isPCacheInit = 0; + } + if( sqlite3GlobalConfig.isMallocInit ){ + sqlite3MallocEnd(); + sqlite3GlobalConfig.isMallocInit = 0; + } + if( sqlite3GlobalConfig.isMutexInit ){ + sqlite3MutexEnd(); + sqlite3GlobalConfig.isMutexInit = 0; + } + + return SQLITE_OK; +} + +/* +** This API allows applications to modify the global configuration of +** the SQLite library at run-time. +** +** This routine should only be called when there are no outstanding +** database connections or memory allocations. This routine is not +** threadsafe. Failure to heed these warnings can lead to unpredictable +** behavior. +*/ +SQLITE_API int sqlite3_config(int op, ...){ + va_list ap; + int rc = SQLITE_OK; + + /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while + ** the SQLite library is in use. */ + if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT; + + va_start(ap, op); + switch( op ){ + + /* Mutex configuration options are only available in a threadsafe + ** compile. + */ +#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0 + case SQLITE_CONFIG_SINGLETHREAD: { + /* Disable all mutexing */ + sqlite3GlobalConfig.bCoreMutex = 0; + sqlite3GlobalConfig.bFullMutex = 0; + break; + } + case SQLITE_CONFIG_MULTITHREAD: { + /* Disable mutexing of database connections */ + /* Enable mutexing of core data structures */ + sqlite3GlobalConfig.bCoreMutex = 1; + sqlite3GlobalConfig.bFullMutex = 0; + break; + } + case SQLITE_CONFIG_SERIALIZED: { + /* Enable all mutexing */ + sqlite3GlobalConfig.bCoreMutex = 1; + sqlite3GlobalConfig.bFullMutex = 1; + break; + } + case SQLITE_CONFIG_MUTEX: { + /* Specify an alternative mutex implementation */ + sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*); + break; + } + case SQLITE_CONFIG_GETMUTEX: { + /* Retrieve the current mutex implementation */ + *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex; + break; + } +#endif + + + case SQLITE_CONFIG_MALLOC: { + /* Specify an alternative malloc implementation */ + sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*); + break; + } + case SQLITE_CONFIG_GETMALLOC: { + /* Retrieve the current malloc() implementation */ + if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault(); + *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m; + break; + } + case SQLITE_CONFIG_MEMSTATUS: { + /* Enable or disable the malloc status collection */ + sqlite3GlobalConfig.bMemstat = va_arg(ap, int); + break; + } + case SQLITE_CONFIG_SCRATCH: { + /* Designate a buffer for scratch memory space */ + sqlite3GlobalConfig.pScratch = va_arg(ap, void*); + sqlite3GlobalConfig.szScratch = va_arg(ap, int); + sqlite3GlobalConfig.nScratch = va_arg(ap, int); + break; + } + case SQLITE_CONFIG_PAGECACHE: { + /* Designate a buffer for page cache memory space */ + sqlite3GlobalConfig.pPage = va_arg(ap, void*); + sqlite3GlobalConfig.szPage = va_arg(ap, int); + sqlite3GlobalConfig.nPage = va_arg(ap, int); + break; + } + + case SQLITE_CONFIG_PCACHE: { + /* Specify an alternative page cache implementation */ + sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*); + break; + } + + case SQLITE_CONFIG_GETPCACHE: { + if( sqlite3GlobalConfig.pcache.xInit==0 ){ + sqlite3PCacheSetDefault(); + } + *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache; + break; + } + +#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5) + case SQLITE_CONFIG_HEAP: { + /* Designate a buffer for heap memory space */ + sqlite3GlobalConfig.pHeap = va_arg(ap, void*); + sqlite3GlobalConfig.nHeap = va_arg(ap, int); + sqlite3GlobalConfig.mnReq = va_arg(ap, int); + + if( sqlite3GlobalConfig.mnReq<1 ){ + sqlite3GlobalConfig.mnReq = 1; + }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){ + /* cap min request size at 2^12 */ + sqlite3GlobalConfig.mnReq = (1<<12); + } + + if( sqlite3GlobalConfig.pHeap==0 ){ + /* If the heap pointer is NULL, then restore the malloc implementation + ** back to NULL pointers too. This will cause the malloc to go + ** back to its default implementation when sqlite3_initialize() is + ** run. + */ + memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m)); + }else{ + /* The heap pointer is not NULL, then install one of the + ** mem5.c/mem3.c methods. If neither ENABLE_MEMSYS3 nor + ** ENABLE_MEMSYS5 is defined, return an error. + */ +#ifdef SQLITE_ENABLE_MEMSYS3 + sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3(); +#endif +#ifdef SQLITE_ENABLE_MEMSYS5 + sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5(); +#endif + } + break; + } +#endif + + case SQLITE_CONFIG_LOOKASIDE: { + sqlite3GlobalConfig.szLookaside = va_arg(ap, int); + sqlite3GlobalConfig.nLookaside = va_arg(ap, int); + break; + } + + /* Record a pointer to the logger funcction and its first argument. + ** The default is NULL. Logging is disabled if the function pointer is + ** NULL. + */ + case SQLITE_CONFIG_LOG: { + /* MSVC is picky about pulling func ptrs from va lists. + ** http://support.microsoft.com/kb/47961 + ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*)); + */ + typedef void(*LOGFUNC_t)(void*,int,const char*); + sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t); + sqlite3GlobalConfig.pLogArg = va_arg(ap, void*); + break; + } + + default: { + rc = SQLITE_ERROR; + break; + } + } + va_end(ap); + return rc; +} + +/* +** Set up the lookaside buffers for a database connection. +** Return SQLITE_OK on success. +** If lookaside is already active, return SQLITE_BUSY. +** +** The sz parameter is the number of bytes in each lookaside slot. +** The cnt parameter is the number of slots. If pStart is NULL the +** space for the lookaside memory is obtained from sqlite3_malloc(). +** If pStart is not NULL then it is sz*cnt bytes of memory to use for +** the lookaside memory. +*/ +static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){ + void *pStart; + if( db->lookaside.nOut ){ + return SQLITE_BUSY; + } + /* Free any existing lookaside buffer for this handle before + ** allocating a new one so we don't have to have space for + ** both at the same time. + */ + if( db->lookaside.bMalloced ){ + sqlite3_free(db->lookaside.pStart); + } + /* The size of a lookaside slot needs to be larger than a pointer + ** to be useful. + */ + if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0; + if( cnt<0 ) cnt = 0; + if( sz==0 || cnt==0 ){ + sz = 0; + pStart = 0; + }else if( pBuf==0 ){ + sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ + sqlite3BeginBenignMalloc(); + pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */ + sqlite3EndBenignMalloc(); + }else{ + sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */ + pStart = pBuf; + } + db->lookaside.pStart = pStart; + db->lookaside.pFree = 0; + db->lookaside.sz = (u16)sz; + if( pStart ){ + int i; + LookasideSlot *p; + assert( sz > (int)sizeof(LookasideSlot*) ); + p = (LookasideSlot*)pStart; + for(i=cnt-1; i>=0; i--){ + p->pNext = db->lookaside.pFree; + db->lookaside.pFree = p; + p = (LookasideSlot*)&((u8*)p)[sz]; + } + db->lookaside.pEnd = p; + db->lookaside.bEnabled = 1; + db->lookaside.bMalloced = pBuf==0 ?1:0; + }else{ + db->lookaside.pEnd = 0; + db->lookaside.bEnabled = 0; + db->lookaside.bMalloced = 0; + } + return SQLITE_OK; +} + +/* +** Return the mutex associated with a database connection. +*/ +SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){ + return db->mutex; +} + +/* +** Configuration settings for an individual database connection +*/ +SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){ + va_list ap; + int rc; + va_start(ap, op); + switch( op ){ + case SQLITE_DBCONFIG_LOOKASIDE: { + void *pBuf = va_arg(ap, void*); /* IMP: R-21112-12275 */ + int sz = va_arg(ap, int); /* IMP: R-47871-25994 */ + int cnt = va_arg(ap, int); /* IMP: R-04460-53386 */ + rc = setupLookaside(db, pBuf, sz, cnt); + break; + } + default: { + rc = SQLITE_ERROR; /* IMP: R-42790-23372 */ + break; + } + } + va_end(ap); + return rc; +} + + +/* +** Return true if the buffer z[0..n-1] contains all spaces. +*/ +static int allSpaces(const char *z, int n){ + while( n>0 && z[n-1]==' ' ){ n--; } + return n==0; +} + +/* +** This is the default collating function named "BINARY" which is always +** available. +** +** If the padFlag argument is not NULL then space padding at the end +** of strings is ignored. This implements the RTRIM collation. +*/ +static int binCollFunc( + void *padFlag, + int nKey1, const void *pKey1, + int nKey2, const void *pKey2 +){ + int rc, n; + n = nKey1lastRowid; +} + +/* +** Return the number of changes in the most recent call to sqlite3_exec(). +*/ +SQLITE_API int sqlite3_changes(sqlite3 *db){ + return db->nChange; +} + +/* +** Return the number of changes since the database handle was opened. +*/ +SQLITE_API int sqlite3_total_changes(sqlite3 *db){ + return db->nTotalChange; +} + +/* +** Close all open savepoints. This function only manipulates fields of the +** database handle object, it does not close any savepoints that may be open +** at the b-tree/pager level. +*/ +SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){ + while( db->pSavepoint ){ + Savepoint *pTmp = db->pSavepoint; + db->pSavepoint = pTmp->pNext; + sqlite3DbFree(db, pTmp); + } + db->nSavepoint = 0; + db->nStatement = 0; + db->isTransactionSavepoint = 0; +} + +/* +** Invoke the destructor function associated with FuncDef p, if any. Except, +** if this is not the last copy of the function, do not invoke it. Multiple +** copies of a single function are created when create_function() is called +** with SQLITE_ANY as the encoding. +*/ +static void functionDestroy(sqlite3 *db, FuncDef *p){ + FuncDestructor *pDestructor = p->pDestructor; + if( pDestructor ){ + pDestructor->nRef--; + if( pDestructor->nRef==0 ){ + pDestructor->xDestroy(pDestructor->pUserData); + sqlite3DbFree(db, pDestructor); + } + } +} + +/* +** Close an existing SQLite database +*/ +SQLITE_API int sqlite3_close(sqlite3 *db){ + HashElem *i; /* Hash table iterator */ + int j; + + if( !db ){ + return SQLITE_OK; + } + if( !sqlite3SafetyCheckSickOrOk(db) ){ + return SQLITE_MISUSE_BKPT; + } + sqlite3_mutex_enter(db->mutex); + + sqlite3ResetInternalSchema(db, 0); + + /* If a transaction is open, the ResetInternalSchema() call above + ** will not have called the xDisconnect() method on any virtual + ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback() + ** call will do so. We need to do this before the check for active + ** SQL statements below, as the v-table implementation may be storing + ** some prepared statements internally. + */ + sqlite3VtabRollback(db); + + /* If there are any outstanding VMs, return SQLITE_BUSY. */ + if( db->pVdbe ){ + sqlite3Error(db, SQLITE_BUSY, + "unable to close due to unfinalised statements"); + sqlite3_mutex_leave(db->mutex); + return SQLITE_BUSY; + } + assert( sqlite3SafetyCheckSickOrOk(db) ); + + for(j=0; jnDb; j++){ + Btree *pBt = db->aDb[j].pBt; + if( pBt && sqlite3BtreeIsInBackup(pBt) ){ + sqlite3Error(db, SQLITE_BUSY, + "unable to close due to unfinished backup operation"); + sqlite3_mutex_leave(db->mutex); + return SQLITE_BUSY; + } + } + + /* Free any outstanding Savepoint structures. */ + sqlite3CloseSavepoints(db); + + for(j=0; jnDb; j++){ + struct Db *pDb = &db->aDb[j]; + if( pDb->pBt ){ + sqlite3BtreeClose(pDb->pBt); + pDb->pBt = 0; + if( j!=1 ){ + pDb->pSchema = 0; + } + } + } + sqlite3ResetInternalSchema(db, 0); + + /* Tell the code in notify.c that the connection no longer holds any + ** locks and does not require any further unlock-notify callbacks. + */ + sqlite3ConnectionClosed(db); + + assert( db->nDb<=2 ); + assert( db->aDb==db->aDbStatic ); + for(j=0; jaFunc.a); j++){ + FuncDef *pNext, *pHash, *p; + for(p=db->aFunc.a[j]; p; p=pHash){ + pHash = p->pHash; + while( p ){ + functionDestroy(db, p); + pNext = p->pNext; + sqlite3DbFree(db, p); + p = pNext; + } + } + } + for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){ + CollSeq *pColl = (CollSeq *)sqliteHashData(i); + /* Invoke any destructors registered for collation sequence user data. */ + for(j=0; j<3; j++){ + if( pColl[j].xDel ){ + pColl[j].xDel(pColl[j].pUser); + } + } + sqlite3DbFree(db, pColl); + } + sqlite3HashClear(&db->aCollSeq); +#ifndef SQLITE_OMIT_VIRTUALTABLE + for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){ + Module *pMod = (Module *)sqliteHashData(i); + if( pMod->xDestroy ){ + pMod->xDestroy(pMod->pAux); + } + sqlite3DbFree(db, pMod); + } + sqlite3HashClear(&db->aModule); +#endif + + sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */ + if( db->pErr ){ + sqlite3ValueFree(db->pErr); + } + sqlite3CloseExtensions(db); + + db->magic = SQLITE_MAGIC_ERROR; + + /* The temp-database schema is allocated differently from the other schema + ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()). + ** So it needs to be freed here. Todo: Why not roll the temp schema into + ** the same sqliteMalloc() as the one that allocates the database + ** structure? + */ + sqlite3DbFree(db, db->aDb[1].pSchema); + sqlite3_mutex_leave(db->mutex); + db->magic = SQLITE_MAGIC_CLOSED; + sqlite3_mutex_free(db->mutex); + assert( db->lookaside.nOut==0 ); /* Fails on a lookaside memory leak */ + if( db->lookaside.bMalloced ){ + sqlite3_free(db->lookaside.pStart); + } + sqlite3_free(db); + return SQLITE_OK; +} + +/* +** Rollback all database files. +*/ +SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db){ + int i; + int inTrans = 0; + assert( sqlite3_mutex_held(db->mutex) ); + sqlite3BeginBenignMalloc(); + for(i=0; inDb; i++){ + if( db->aDb[i].pBt ){ + if( sqlite3BtreeIsInTrans(db->aDb[i].pBt) ){ + inTrans = 1; + } + sqlite3BtreeRollback(db->aDb[i].pBt); + db->aDb[i].inTrans = 0; + } + } + sqlite3VtabRollback(db); + sqlite3EndBenignMalloc(); + + if( db->flags&SQLITE_InternChanges ){ + sqlite3ExpirePreparedStatements(db); + sqlite3ResetInternalSchema(db, 0); + } + + /* Any deferred constraint violations have now been resolved. */ + db->nDeferredCons = 0; + + /* If one has been configured, invoke the rollback-hook callback */ + if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){ + db->xRollbackCallback(db->pRollbackArg); + } +} + +/* +** Return a static string that describes the kind of error specified in the +** argument. +*/ +SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){ + static const char* const aMsg[] = { + /* SQLITE_OK */ "not an error", + /* SQLITE_ERROR */ "SQL logic error or missing database", + /* SQLITE_INTERNAL */ 0, + /* SQLITE_PERM */ "access permission denied", + /* SQLITE_ABORT */ "callback requested query abort", + /* SQLITE_BUSY */ "database is locked", + /* SQLITE_LOCKED */ "database table is locked", + /* SQLITE_NOMEM */ "out of memory", + /* SQLITE_READONLY */ "attempt to write a readonly database", + /* SQLITE_INTERRUPT */ "interrupted", + /* SQLITE_IOERR */ "disk I/O error", + /* SQLITE_CORRUPT */ "database disk image is malformed", + /* SQLITE_NOTFOUND */ "unknown operation", + /* SQLITE_FULL */ "database or disk is full", + /* SQLITE_CANTOPEN */ "unable to open database file", + /* SQLITE_PROTOCOL */ "locking protocol", + /* SQLITE_EMPTY */ "table contains no data", + /* SQLITE_SCHEMA */ "database schema has changed", + /* SQLITE_TOOBIG */ "string or blob too big", + /* SQLITE_CONSTRAINT */ "constraint failed", + /* SQLITE_MISMATCH */ "datatype mismatch", + /* SQLITE_MISUSE */ "library routine called out of sequence", + /* SQLITE_NOLFS */ "large file support is disabled", + /* SQLITE_AUTH */ "authorization denied", + /* SQLITE_FORMAT */ "auxiliary database format error", + /* SQLITE_RANGE */ "bind or column index out of range", + /* SQLITE_NOTADB */ "file is encrypted or is not a database", + }; + rc &= 0xff; + if( ALWAYS(rc>=0) && rc<(int)(sizeof(aMsg)/sizeof(aMsg[0])) && aMsg[rc]!=0 ){ + return aMsg[rc]; + }else{ + return "unknown error"; + } +} + +/* +** This routine implements a busy callback that sleeps and tries +** again until a timeout value is reached. The timeout value is +** an integer number of milliseconds passed in as the first +** argument. +*/ +static int sqliteDefaultBusyCallback( + void *ptr, /* Database connection */ + int count /* Number of times table has been busy */ +){ +#if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP) + static const u8 delays[] = + { 1, 2, 5, 10, 15, 20, 25, 25, 25, 50, 50, 100 }; + static const u8 totals[] = + { 0, 1, 3, 8, 18, 33, 53, 78, 103, 128, 178, 228 }; +# define NDELAY (sizeof(delays)/sizeof(delays[0])) + sqlite3 *db = (sqlite3 *)ptr; + int timeout = db->busyTimeout; + int delay, prior; + + assert( count>=0 ); + if( count < NDELAY ){ + delay = delays[count]; + prior = totals[count]; + }else{ + delay = delays[NDELAY-1]; + prior = totals[NDELAY-1] + delay*(count-(NDELAY-1)); + } + if( prior + delay > timeout ){ + delay = timeout - prior; + if( delay<=0 ) return 0; + } + sqlite3OsSleep(db->pVfs, delay*1000); + return 1; +#else + sqlite3 *db = (sqlite3 *)ptr; + int timeout = ((sqlite3 *)ptr)->busyTimeout; + if( (count+1)*1000 > timeout ){ + return 0; + } + sqlite3OsSleep(db->pVfs, 1000000); + return 1; +#endif +} + +/* +** Invoke the given busy handler. +** +** This routine is called when an operation failed with a lock. +** If this routine returns non-zero, the lock is retried. If it +** returns 0, the operation aborts with an SQLITE_BUSY error. +*/ +SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){ + int rc; + if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0; + rc = p->xFunc(p->pArg, p->nBusy); + if( rc==0 ){ + p->nBusy = -1; + }else{ + p->nBusy++; + } + return rc; +} + +/* +** This routine sets the busy callback for an Sqlite database to the +** given callback function with the given argument. +*/ +SQLITE_API int sqlite3_busy_handler( + sqlite3 *db, + int (*xBusy)(void*,int), + void *pArg +){ + sqlite3_mutex_enter(db->mutex); + db->busyHandler.xFunc = xBusy; + db->busyHandler.pArg = pArg; + db->busyHandler.nBusy = 0; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK +/* +** This routine sets the progress callback for an Sqlite database to the +** given callback function with the given argument. The progress callback will +** be invoked every nOps opcodes. +*/ +SQLITE_API void sqlite3_progress_handler( + sqlite3 *db, + int nOps, + int (*xProgress)(void*), + void *pArg +){ + sqlite3_mutex_enter(db->mutex); + if( nOps>0 ){ + db->xProgress = xProgress; + db->nProgressOps = nOps; + db->pProgressArg = pArg; + }else{ + db->xProgress = 0; + db->nProgressOps = 0; + db->pProgressArg = 0; + } + sqlite3_mutex_leave(db->mutex); +} +#endif + + +/* +** This routine installs a default busy handler that waits for the +** specified number of milliseconds before returning 0. +*/ +SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){ + if( ms>0 ){ + db->busyTimeout = ms; + sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db); + }else{ + sqlite3_busy_handler(db, 0, 0); + } + return SQLITE_OK; +} + +/* +** Cause any pending operation to stop at its earliest opportunity. +*/ +SQLITE_API void sqlite3_interrupt(sqlite3 *db){ + db->u1.isInterrupted = 1; +} + + +/* +** This function is exactly the same as sqlite3_create_function(), except +** that it is designed to be called by internal code. The difference is +** that if a malloc() fails in sqlite3_create_function(), an error code +** is returned and the mallocFailed flag cleared. +*/ +SQLITE_PRIVATE int sqlite3CreateFunc( + sqlite3 *db, + const char *zFunctionName, + int nArg, + int enc, + void *pUserData, + void (*xFunc)(sqlite3_context*,int,sqlite3_value **), + void (*xStep)(sqlite3_context*,int,sqlite3_value **), + void (*xFinal)(sqlite3_context*), + FuncDestructor *pDestructor +){ + FuncDef *p; + int nName; + + assert( sqlite3_mutex_held(db->mutex) ); + if( zFunctionName==0 || + (xFunc && (xFinal || xStep)) || + (!xFunc && (xFinal && !xStep)) || + (!xFunc && (!xFinal && xStep)) || + (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) || + (255<(nName = sqlite3Strlen30( zFunctionName))) ){ + return SQLITE_MISUSE_BKPT; + } + +#ifndef SQLITE_OMIT_UTF16 + /* If SQLITE_UTF16 is specified as the encoding type, transform this + ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the + ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. + ** + ** If SQLITE_ANY is specified, add three versions of the function + ** to the hash table. + */ + if( enc==SQLITE_UTF16 ){ + enc = SQLITE_UTF16NATIVE; + }else if( enc==SQLITE_ANY ){ + int rc; + rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8, + pUserData, xFunc, xStep, xFinal, pDestructor); + if( rc==SQLITE_OK ){ + rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE, + pUserData, xFunc, xStep, xFinal, pDestructor); + } + if( rc!=SQLITE_OK ){ + return rc; + } + enc = SQLITE_UTF16BE; + } +#else + enc = SQLITE_UTF8; +#endif + + /* Check if an existing function is being overridden or deleted. If so, + ** and there are active VMs, then return SQLITE_BUSY. If a function + ** is being overridden/deleted but there are no active VMs, allow the + ** operation to continue but invalidate all precompiled statements. + */ + p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0); + if( p && p->iPrefEnc==enc && p->nArg==nArg ){ + if( db->activeVdbeCnt ){ + sqlite3Error(db, SQLITE_BUSY, + "unable to delete/modify user-function due to active statements"); + assert( !db->mallocFailed ); + return SQLITE_BUSY; + }else{ + sqlite3ExpirePreparedStatements(db); + } + } + + p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1); + assert(p || db->mallocFailed); + if( !p ){ + return SQLITE_NOMEM; + } + + /* If an older version of the function with a configured destructor is + ** being replaced invoke the destructor function here. */ + functionDestroy(db, p); + + if( pDestructor ){ + pDestructor->nRef++; + } + p->pDestructor = pDestructor; + p->flags = 0; + p->xFunc = xFunc; + p->xStep = xStep; + p->xFinalize = xFinal; + p->pUserData = pUserData; + p->nArg = (u16)nArg; + return SQLITE_OK; +} + +/* +** Create new user functions. +*/ +SQLITE_API int sqlite3_create_function( + sqlite3 *db, + const char *zFunc, + int nArg, + int enc, + void *p, + void (*xFunc)(sqlite3_context*,int,sqlite3_value **), + void (*xStep)(sqlite3_context*,int,sqlite3_value **), + void (*xFinal)(sqlite3_context*) +){ + return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep, + xFinal, 0); +} + +SQLITE_API int sqlite3_create_function_v2( + sqlite3 *db, + const char *zFunc, + int nArg, + int enc, + void *p, + void (*xFunc)(sqlite3_context*,int,sqlite3_value **), + void (*xStep)(sqlite3_context*,int,sqlite3_value **), + void (*xFinal)(sqlite3_context*), + void (*xDestroy)(void *) +){ + int rc = SQLITE_ERROR; + FuncDestructor *pArg = 0; + sqlite3_mutex_enter(db->mutex); + if( xDestroy ){ + pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor)); + if( !pArg ){ + xDestroy(p); + goto out; + } + pArg->xDestroy = xDestroy; + pArg->pUserData = p; + } + rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg); + if( pArg && pArg->nRef==0 ){ + assert( rc!=SQLITE_OK ); + xDestroy(p); + sqlite3DbFree(db, pArg); + } + + out: + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +#ifndef SQLITE_OMIT_UTF16 +SQLITE_API int sqlite3_create_function16( + sqlite3 *db, + const void *zFunctionName, + int nArg, + int eTextRep, + void *p, + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*) +){ + int rc; + char *zFunc8; + sqlite3_mutex_enter(db->mutex); + assert( !db->mallocFailed ); + zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE); + rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0); + sqlite3DbFree(db, zFunc8); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} +#endif + + +/* +** Declare that a function has been overloaded by a virtual table. +** +** If the function already exists as a regular global function, then +** this routine is a no-op. If the function does not exist, then create +** a new one that always throws a run-time error. +** +** When virtual tables intend to provide an overloaded function, they +** should call this routine to make sure the global function exists. +** A global function must exist in order for name resolution to work +** properly. +*/ +SQLITE_API int sqlite3_overload_function( + sqlite3 *db, + const char *zName, + int nArg +){ + int nName = sqlite3Strlen30(zName); + int rc; + sqlite3_mutex_enter(db->mutex); + if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){ + sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8, + 0, sqlite3InvalidFunction, 0, 0, 0); + } + rc = sqlite3ApiExit(db, SQLITE_OK); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +#ifndef SQLITE_OMIT_TRACE +/* +** Register a trace function. The pArg from the previously registered trace +** is returned. +** +** A NULL trace function means that no tracing is executes. A non-NULL +** trace is a pointer to a function that is invoked at the start of each +** SQL statement. +*/ +SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){ + void *pOld; + sqlite3_mutex_enter(db->mutex); + pOld = db->pTraceArg; + db->xTrace = xTrace; + db->pTraceArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pOld; +} +/* +** Register a profile function. The pArg from the previously registered +** profile function is returned. +** +** A NULL profile function means that no profiling is executes. A non-NULL +** profile is a pointer to a function that is invoked at the conclusion of +** each SQL statement that is run. +*/ +SQLITE_API void *sqlite3_profile( + sqlite3 *db, + void (*xProfile)(void*,const char*,sqlite_uint64), + void *pArg +){ + void *pOld; + sqlite3_mutex_enter(db->mutex); + pOld = db->pProfileArg; + db->xProfile = xProfile; + db->pProfileArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pOld; +} +#endif /* SQLITE_OMIT_TRACE */ + +/*** EXPERIMENTAL *** +** +** Register a function to be invoked when a transaction comments. +** If the invoked function returns non-zero, then the commit becomes a +** rollback. +*/ +SQLITE_API void *sqlite3_commit_hook( + sqlite3 *db, /* Attach the hook to this database */ + int (*xCallback)(void*), /* Function to invoke on each commit */ + void *pArg /* Argument to the function */ +){ + void *pOld; + sqlite3_mutex_enter(db->mutex); + pOld = db->pCommitArg; + db->xCommitCallback = xCallback; + db->pCommitArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pOld; +} + +/* +** Register a callback to be invoked each time a row is updated, +** inserted or deleted using this database connection. +*/ +SQLITE_API void *sqlite3_update_hook( + sqlite3 *db, /* Attach the hook to this database */ + void (*xCallback)(void*,int,char const *,char const *,sqlite_int64), + void *pArg /* Argument to the function */ +){ + void *pRet; + sqlite3_mutex_enter(db->mutex); + pRet = db->pUpdateArg; + db->xUpdateCallback = xCallback; + db->pUpdateArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pRet; +} + +/* +** Register a callback to be invoked each time a transaction is rolled +** back by this database connection. +*/ +SQLITE_API void *sqlite3_rollback_hook( + sqlite3 *db, /* Attach the hook to this database */ + void (*xCallback)(void*), /* Callback function */ + void *pArg /* Argument to the function */ +){ + void *pRet; + sqlite3_mutex_enter(db->mutex); + pRet = db->pRollbackArg; + db->xRollbackCallback = xCallback; + db->pRollbackArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pRet; +} + +#ifndef SQLITE_OMIT_WAL +/* +** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint(). +** Invoke sqlite3_wal_checkpoint if the number of frames in the log file +** is greater than sqlite3.pWalArg cast to an integer (the value configured by +** wal_autocheckpoint()). +*/ +SQLITE_PRIVATE int sqlite3WalDefaultHook( + void *pClientData, /* Argument */ + sqlite3 *db, /* Connection */ + const char *zDb, /* Database */ + int nFrame /* Size of WAL */ +){ + if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){ + sqlite3BeginBenignMalloc(); + sqlite3_wal_checkpoint(db, zDb); + sqlite3EndBenignMalloc(); + } + return SQLITE_OK; +} +#endif /* SQLITE_OMIT_WAL */ + +/* +** Configure an sqlite3_wal_hook() callback to automatically checkpoint +** a database after committing a transaction if there are nFrame or +** more frames in the log file. Passing zero or a negative value as the +** nFrame parameter disables automatic checkpoints entirely. +** +** The callback registered by this function replaces any existing callback +** registered using sqlite3_wal_hook(). Likewise, registering a callback +** using sqlite3_wal_hook() disables the automatic checkpoint mechanism +** configured by this function. +*/ +SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){ +#ifdef SQLITE_OMIT_WAL + UNUSED_PARAMETER(db); + UNUSED_PARAMETER(nFrame); +#else + if( nFrame>0 ){ + sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame)); + }else{ + sqlite3_wal_hook(db, 0, 0); + } +#endif + return SQLITE_OK; +} + +/* +** Register a callback to be invoked each time a transaction is written +** into the write-ahead-log by this database connection. +*/ +SQLITE_API void *sqlite3_wal_hook( + sqlite3 *db, /* Attach the hook to this db handle */ + int(*xCallback)(void *, sqlite3*, const char*, int), + void *pArg /* First argument passed to xCallback() */ +){ +#ifndef SQLITE_OMIT_WAL + void *pRet; + sqlite3_mutex_enter(db->mutex); + pRet = db->pWalArg; + db->xWalCallback = xCallback; + db->pWalArg = pArg; + sqlite3_mutex_leave(db->mutex); + return pRet; +#else + return 0; +#endif +} + +/* +** Checkpoint database zDb. +*/ +SQLITE_API int sqlite3_wal_checkpoint_v2( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Name of attached database (or NULL) */ + int eMode, /* SQLITE_CHECKPOINT_* value */ + int *pnLog, /* OUT: Size of WAL log in frames */ + int *pnCkpt /* OUT: Total number of frames checkpointed */ +){ +#ifdef SQLITE_OMIT_WAL + return SQLITE_OK; +#else + int rc; /* Return code */ + int iDb = SQLITE_MAX_ATTACHED; /* sqlite3.aDb[] index of db to checkpoint */ + + /* Initialize the output variables to -1 in case an error occurs. */ + if( pnLog ) *pnLog = -1; + if( pnCkpt ) *pnCkpt = -1; + + assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE ); + assert( SQLITE_CHECKPOINT_FULLSQLITE_CHECKPOINT_RESTART ){ + return SQLITE_MISUSE; + } + + sqlite3_mutex_enter(db->mutex); + if( zDb && zDb[0] ){ + iDb = sqlite3FindDbName(db, zDb); + } + if( iDb<0 ){ + rc = SQLITE_ERROR; + sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb); + }else{ + rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt); + sqlite3Error(db, rc, 0); + } + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +#endif +} + + +/* +** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points +** to contains a zero-length string, all attached databases are +** checkpointed. +*/ +SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){ + return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0); +} + +#ifndef SQLITE_OMIT_WAL +/* +** Run a checkpoint on database iDb. This is a no-op if database iDb is +** not currently open in WAL mode. +** +** If a transaction is open on the database being checkpointed, this +** function returns SQLITE_LOCKED and a checkpoint is not attempted. If +** an error occurs while running the checkpoint, an SQLite error code is +** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK. +** +** The mutex on database handle db should be held by the caller. The mutex +** associated with the specific b-tree being checkpointed is taken by +** this function while the checkpoint is running. +** +** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are +** checkpointed. If an error is encountered it is returned immediately - +** no attempt is made to checkpoint any remaining databases. +** +** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. +*/ +SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){ + int rc = SQLITE_OK; /* Return code */ + int i; /* Used to iterate through attached dbs */ + int bBusy = 0; /* True if SQLITE_BUSY has been encountered */ + + assert( sqlite3_mutex_held(db->mutex) ); + assert( !pnLog || *pnLog==-1 ); + assert( !pnCkpt || *pnCkpt==-1 ); + + for(i=0; inDb && rc==SQLITE_OK; i++){ + if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){ + rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt); + pnLog = 0; + pnCkpt = 0; + if( rc==SQLITE_BUSY ){ + bBusy = 1; + rc = SQLITE_OK; + } + } + } + + return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc; +} +#endif /* SQLITE_OMIT_WAL */ + +/* +** This function returns true if main-memory should be used instead of +** a temporary file for transient pager files and statement journals. +** The value returned depends on the value of db->temp_store (runtime +** parameter) and the compile time value of SQLITE_TEMP_STORE. The +** following table describes the relationship between these two values +** and this functions return value. +** +** SQLITE_TEMP_STORE db->temp_store Location of temporary database +** ----------------- -------------- ------------------------------ +** 0 any file (return 0) +** 1 1 file (return 0) +** 1 2 memory (return 1) +** 1 0 file (return 0) +** 2 1 file (return 0) +** 2 2 memory (return 1) +** 2 0 memory (return 1) +** 3 any memory (return 1) +*/ +SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){ +#if SQLITE_TEMP_STORE==1 + return ( db->temp_store==2 ); +#endif +#if SQLITE_TEMP_STORE==2 + return ( db->temp_store!=1 ); +#endif +#if SQLITE_TEMP_STORE==3 + return 1; +#endif +#if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3 + return 0; +#endif +} + +/* +** Return UTF-8 encoded English language explanation of the most recent +** error. +*/ +SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){ + const char *z; + if( !db ){ + return sqlite3ErrStr(SQLITE_NOMEM); + } + if( !sqlite3SafetyCheckSickOrOk(db) ){ + return sqlite3ErrStr(SQLITE_MISUSE_BKPT); + } + sqlite3_mutex_enter(db->mutex); + if( db->mallocFailed ){ + z = sqlite3ErrStr(SQLITE_NOMEM); + }else{ + z = (char*)sqlite3_value_text(db->pErr); + assert( !db->mallocFailed ); + if( z==0 ){ + z = sqlite3ErrStr(db->errCode); + } + } + sqlite3_mutex_leave(db->mutex); + return z; +} + +#ifndef SQLITE_OMIT_UTF16 +/* +** Return UTF-16 encoded English language explanation of the most recent +** error. +*/ +SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){ + static const u16 outOfMem[] = { + 'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0 + }; + static const u16 misuse[] = { + 'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ', + 'r', 'o', 'u', 't', 'i', 'n', 'e', ' ', + 'c', 'a', 'l', 'l', 'e', 'd', ' ', + 'o', 'u', 't', ' ', + 'o', 'f', ' ', + 's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0 + }; + + const void *z; + if( !db ){ + return (void *)outOfMem; + } + if( !sqlite3SafetyCheckSickOrOk(db) ){ + return (void *)misuse; + } + sqlite3_mutex_enter(db->mutex); + if( db->mallocFailed ){ + z = (void *)outOfMem; + }else{ + z = sqlite3_value_text16(db->pErr); + if( z==0 ){ + sqlite3ValueSetStr(db->pErr, -1, sqlite3ErrStr(db->errCode), + SQLITE_UTF8, SQLITE_STATIC); + z = sqlite3_value_text16(db->pErr); + } + /* A malloc() may have failed within the call to sqlite3_value_text16() + ** above. If this is the case, then the db->mallocFailed flag needs to + ** be cleared before returning. Do this directly, instead of via + ** sqlite3ApiExit(), to avoid setting the database handle error message. + */ + db->mallocFailed = 0; + } + sqlite3_mutex_leave(db->mutex); + return z; +} +#endif /* SQLITE_OMIT_UTF16 */ + +/* +** Return the most recent error code generated by an SQLite routine. If NULL is +** passed to this function, we assume a malloc() failed during sqlite3_open(). +*/ +SQLITE_API int sqlite3_errcode(sqlite3 *db){ + if( db && !sqlite3SafetyCheckSickOrOk(db) ){ + return SQLITE_MISUSE_BKPT; + } + if( !db || db->mallocFailed ){ + return SQLITE_NOMEM; + } + return db->errCode & db->errMask; +} +SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){ + if( db && !sqlite3SafetyCheckSickOrOk(db) ){ + return SQLITE_MISUSE_BKPT; + } + if( !db || db->mallocFailed ){ + return SQLITE_NOMEM; + } + return db->errCode; +} + +/* +** Create a new collating function for database "db". The name is zName +** and the encoding is enc. +*/ +static int createCollation( + sqlite3* db, + const char *zName, + u8 enc, + u8 collType, + void* pCtx, + int(*xCompare)(void*,int,const void*,int,const void*), + void(*xDel)(void*) +){ + CollSeq *pColl; + int enc2; + int nName = sqlite3Strlen30(zName); + + assert( sqlite3_mutex_held(db->mutex) ); + + /* If SQLITE_UTF16 is specified as the encoding type, transform this + ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the + ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally. + */ + enc2 = enc; + testcase( enc2==SQLITE_UTF16 ); + testcase( enc2==SQLITE_UTF16_ALIGNED ); + if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){ + enc2 = SQLITE_UTF16NATIVE; + } + if( enc2SQLITE_UTF16BE ){ + return SQLITE_MISUSE_BKPT; + } + + /* Check if this call is removing or replacing an existing collation + ** sequence. If so, and there are active VMs, return busy. If there + ** are no active VMs, invalidate any pre-compiled statements. + */ + pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0); + if( pColl && pColl->xCmp ){ + if( db->activeVdbeCnt ){ + sqlite3Error(db, SQLITE_BUSY, + "unable to delete/modify collation sequence due to active statements"); + return SQLITE_BUSY; + } + sqlite3ExpirePreparedStatements(db); + + /* If collation sequence pColl was created directly by a call to + ** sqlite3_create_collation, and not generated by synthCollSeq(), + ** then any copies made by synthCollSeq() need to be invalidated. + ** Also, collation destructor - CollSeq.xDel() - function may need + ** to be called. + */ + if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){ + CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName); + int j; + for(j=0; j<3; j++){ + CollSeq *p = &aColl[j]; + if( p->enc==pColl->enc ){ + if( p->xDel ){ + p->xDel(p->pUser); + } + p->xCmp = 0; + } + } + } + } + + pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1); + if( pColl==0 ) return SQLITE_NOMEM; + pColl->xCmp = xCompare; + pColl->pUser = pCtx; + pColl->xDel = xDel; + pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED)); + pColl->type = collType; + sqlite3Error(db, SQLITE_OK, 0); + return SQLITE_OK; +} + + +/* +** This array defines hard upper bounds on limit values. The +** initializer must be kept in sync with the SQLITE_LIMIT_* +** #defines in sqlite3.h. +*/ +static const int aHardLimit[] = { + SQLITE_MAX_LENGTH, + SQLITE_MAX_SQL_LENGTH, + SQLITE_MAX_COLUMN, + SQLITE_MAX_EXPR_DEPTH, + SQLITE_MAX_COMPOUND_SELECT, + SQLITE_MAX_VDBE_OP, + SQLITE_MAX_FUNCTION_ARG, + SQLITE_MAX_ATTACHED, + SQLITE_MAX_LIKE_PATTERN_LENGTH, + SQLITE_MAX_VARIABLE_NUMBER, + SQLITE_MAX_TRIGGER_DEPTH, +}; + +/* +** Make sure the hard limits are set to reasonable values +*/ +#if SQLITE_MAX_LENGTH<100 +# error SQLITE_MAX_LENGTH must be at least 100 +#endif +#if SQLITE_MAX_SQL_LENGTH<100 +# error SQLITE_MAX_SQL_LENGTH must be at least 100 +#endif +#if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH +# error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH +#endif +#if SQLITE_MAX_COMPOUND_SELECT<2 +# error SQLITE_MAX_COMPOUND_SELECT must be at least 2 +#endif +#if SQLITE_MAX_VDBE_OP<40 +# error SQLITE_MAX_VDBE_OP must be at least 40 +#endif +#if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000 +# error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000 +#endif +#if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>30 +# error SQLITE_MAX_ATTACHED must be between 0 and 30 +#endif +#if SQLITE_MAX_LIKE_PATTERN_LENGTH<1 +# error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1 +#endif +#if SQLITE_MAX_COLUMN>32767 +# error SQLITE_MAX_COLUMN must not exceed 32767 +#endif +#if SQLITE_MAX_TRIGGER_DEPTH<1 +# error SQLITE_MAX_TRIGGER_DEPTH must be at least 1 +#endif + + +/* +** Change the value of a limit. Report the old value. +** If an invalid limit index is supplied, report -1. +** Make no changes but still report the old value if the +** new limit is negative. +** +** A new lower limit does not shrink existing constructs. +** It merely prevents new constructs that exceed the limit +** from forming. +*/ +SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){ + int oldLimit; + + + /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME + ** there is a hard upper bound set at compile-time by a C preprocessor + ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to + ** "_MAX_".) + */ + assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH ); + assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH ); + assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN ); + assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH ); + assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT); + assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP ); + assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG ); + assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED ); + assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]== + SQLITE_MAX_LIKE_PATTERN_LENGTH ); + assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER); + assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH ); + assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) ); + + + if( limitId<0 || limitId>=SQLITE_N_LIMIT ){ + return -1; + } + oldLimit = db->aLimit[limitId]; + if( newLimit>=0 ){ /* IMP: R-52476-28732 */ + if( newLimit>aHardLimit[limitId] ){ + newLimit = aHardLimit[limitId]; /* IMP: R-51463-25634 */ + } + db->aLimit[limitId] = newLimit; + } + return oldLimit; /* IMP: R-53341-35419 */ +} + +/* +** This routine does the work of opening a database on behalf of +** sqlite3_open() and sqlite3_open16(). The database filename "zFilename" +** is UTF-8 encoded. +*/ +static int openDatabase( + const char *zFilename, /* Database filename UTF-8 encoded */ + sqlite3 **ppDb, /* OUT: Returned database handle */ + unsigned flags, /* Operational flags */ + const char *zVfs /* Name of the VFS to use */ +){ + sqlite3 *db; + int rc; + int isThreadsafe; + + *ppDb = 0; +#ifndef SQLITE_OMIT_AUTOINIT + rc = sqlite3_initialize(); + if( rc ) return rc; +#endif + + /* Only allow sensible combinations of bits in the flags argument. + ** Throw an error if any non-sense combination is used. If we + ** do not block illegal combinations here, it could trigger + ** assert() statements in deeper layers. Sensible combinations + ** are: + ** + ** 1: SQLITE_OPEN_READONLY + ** 2: SQLITE_OPEN_READWRITE + ** 6: SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE + */ + assert( SQLITE_OPEN_READONLY == 0x01 ); + assert( SQLITE_OPEN_READWRITE == 0x02 ); + assert( SQLITE_OPEN_CREATE == 0x04 ); + testcase( (1<<(flags&7))==0x02 ); /* READONLY */ + testcase( (1<<(flags&7))==0x04 ); /* READWRITE */ + testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */ + if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE; + + if( sqlite3GlobalConfig.bCoreMutex==0 ){ + isThreadsafe = 0; + }else if( flags & SQLITE_OPEN_NOMUTEX ){ + isThreadsafe = 0; + }else if( flags & SQLITE_OPEN_FULLMUTEX ){ + isThreadsafe = 1; + }else{ + isThreadsafe = sqlite3GlobalConfig.bFullMutex; + } + if( flags & SQLITE_OPEN_PRIVATECACHE ){ + flags &= ~SQLITE_OPEN_SHAREDCACHE; + }else if( sqlite3GlobalConfig.sharedCacheEnabled ){ + flags |= SQLITE_OPEN_SHAREDCACHE; + } + + /* Remove harmful bits from the flags parameter + ** + ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were + ** dealt with in the previous code block. Besides these, the only + ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY, + ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE, + ** SQLITE_OPEN_PRIVATECACHE, SQLITE_OPEN_READAHEAD, and some reserved bits. + ** Silently mask off all other flags. + */ + flags &= ~( SQLITE_OPEN_DELETEONCLOSE | + SQLITE_OPEN_EXCLUSIVE | + SQLITE_OPEN_MAIN_DB | + SQLITE_OPEN_TEMP_DB | + SQLITE_OPEN_TRANSIENT_DB | + SQLITE_OPEN_MAIN_JOURNAL | + SQLITE_OPEN_TEMP_JOURNAL | + SQLITE_OPEN_SUBJOURNAL | + SQLITE_OPEN_MASTER_JOURNAL | + SQLITE_OPEN_NOMUTEX | + SQLITE_OPEN_FULLMUTEX | + SQLITE_OPEN_WAL + ); + + /* Allocate the sqlite data structure */ + db = sqlite3MallocZero( sizeof(sqlite3) ); + if( db==0 ) goto opendb_out; + if( isThreadsafe ){ + db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE); + if( db->mutex==0 ){ + sqlite3_free(db); + db = 0; + goto opendb_out; + } + } + sqlite3_mutex_enter(db->mutex); + db->errMask = 0xff; + db->nDb = 2; + db->magic = SQLITE_MAGIC_BUSY; + db->aDb = db->aDbStatic; + + assert( sizeof(db->aLimit)==sizeof(aHardLimit) ); + memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit)); + db->autoCommit = 1; + db->nextAutovac = -1; + db->nextPagesize = 0; + db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex +#if SQLITE_DEFAULT_FILE_FORMAT<4 + | SQLITE_LegacyFileFmt +#endif +#ifdef SQLITE_ENABLE_LOAD_EXTENSION + | SQLITE_LoadExtension +#endif +#if SQLITE_DEFAULT_RECURSIVE_TRIGGERS + | SQLITE_RecTriggers +#endif +#if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS + | SQLITE_ForeignKeys +#endif + ; + sqlite3HashInit(&db->aCollSeq); +#ifndef SQLITE_OMIT_VIRTUALTABLE + sqlite3HashInit(&db->aModule); +#endif + + db->pVfs = sqlite3_vfs_find(zVfs); + if( !db->pVfs ){ + rc = SQLITE_ERROR; + sqlite3Error(db, rc, "no such vfs: %s", zVfs); + goto opendb_out; + } + + /* Add the default collation sequence BINARY. BINARY works for both UTF-8 + ** and UTF-16, so add a version for each to avoid any unnecessary + ** conversions. The only error that can occur here is a malloc() failure. + */ + createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0, + binCollFunc, 0); + createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0, + binCollFunc, 0); + createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0, + binCollFunc, 0); + createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1, + binCollFunc, 0); + if( db->mallocFailed ){ + goto opendb_out; + } + db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0); + assert( db->pDfltColl!=0 ); + + /* Also add a UTF-8 case-insensitive collation sequence. */ + createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0, + nocaseCollatingFunc, 0); + + /* Open the backend database driver */ + db->openFlags = flags; + rc = sqlite3BtreeOpen(zFilename, db, &db->aDb[0].pBt, 0, + flags | SQLITE_OPEN_MAIN_DB); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_IOERR_NOMEM ){ + rc = SQLITE_NOMEM; + } + sqlite3Error(db, rc, 0); + goto opendb_out; + } + db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt); + db->aDb[1].pSchema = sqlite3SchemaGet(db, 0); + + + /* The default safety_level for the main database is 'full'; for the temp + ** database it is 'NONE'. This matches the pager layer defaults. + */ + db->aDb[0].zName = "main"; + db->aDb[0].safety_level = 3; + db->aDb[1].zName = "temp"; + db->aDb[1].safety_level = 1; + + db->magic = SQLITE_MAGIC_OPEN; + if( db->mallocFailed ){ + goto opendb_out; + } + + /* Register all built-in functions, but do not attempt to read the + ** database schema yet. This is delayed until the first time the database + ** is accessed. + */ + sqlite3Error(db, SQLITE_OK, 0); + sqlite3RegisterBuiltinFunctions(db); + + /* Load automatic extensions - extensions that have been registered + ** using the sqlite3_automatic_extension() API. + */ + sqlite3AutoLoadExtensions(db); + rc = sqlite3_errcode(db); + if( rc!=SQLITE_OK ){ + goto opendb_out; + } + +#ifdef SQLITE_ENABLE_FTS1 + if( !db->mallocFailed ){ + extern int sqlite3Fts1Init(sqlite3*); + rc = sqlite3Fts1Init(db); + } +#endif + +#ifdef SQLITE_ENABLE_FTS2 + if( !db->mallocFailed && rc==SQLITE_OK ){ + extern int sqlite3Fts2Init(sqlite3*); + rc = sqlite3Fts2Init(db); + } +#endif + +#ifdef SQLITE_ENABLE_FTS3 + if( !db->mallocFailed && rc==SQLITE_OK ){ + rc = sqlite3Fts3Init(db); + } +#endif + +#ifdef SQLITE_ENABLE_ICU + if( !db->mallocFailed && rc==SQLITE_OK ){ + rc = sqlite3IcuInit(db); + } +#endif + +#ifdef SQLITE_ENABLE_RTREE + if( !db->mallocFailed && rc==SQLITE_OK){ + rc = sqlite3RtreeInit(db); + } +#endif + + sqlite3Error(db, rc, 0); + + /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking + ** mode. -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking + ** mode. Doing nothing at all also makes NORMAL the default. + */ +#ifdef SQLITE_DEFAULT_LOCKING_MODE + db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE; + sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt), + SQLITE_DEFAULT_LOCKING_MODE); +#endif + + /* Enable the lookaside-malloc subsystem */ + setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside, + sqlite3GlobalConfig.nLookaside); + + sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT); + +opendb_out: + if( db ){ + assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 ); + sqlite3_mutex_leave(db->mutex); + } + rc = sqlite3_errcode(db); + if( rc==SQLITE_NOMEM ){ + sqlite3_close(db); + db = 0; + }else if( rc!=SQLITE_OK ){ + db->magic = SQLITE_MAGIC_SICK; + } + *ppDb = db; + return sqlite3ApiExit(0, rc); +} + +/* +** Open a new database handle. +*/ +SQLITE_API int sqlite3_open( + const char *zFilename, + sqlite3 **ppDb +){ + return openDatabase(zFilename, ppDb, + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); +} +SQLITE_API int sqlite3_open_v2( + const char *filename, /* Database filename (UTF-8) */ + sqlite3 **ppDb, /* OUT: SQLite db handle */ + int flags, /* Flags */ + const char *zVfs /* Name of VFS module to use */ +){ + return openDatabase(filename, ppDb, flags, zVfs); +} + +#ifndef SQLITE_OMIT_UTF16 +/* +** Open a new database handle. +*/ +SQLITE_API int sqlite3_open16( + const void *zFilename, + sqlite3 **ppDb +){ + char const *zFilename8; /* zFilename encoded in UTF-8 instead of UTF-16 */ + sqlite3_value *pVal; + int rc; + + assert( zFilename ); + assert( ppDb ); + *ppDb = 0; +#ifndef SQLITE_OMIT_AUTOINIT + rc = sqlite3_initialize(); + if( rc ) return rc; +#endif + pVal = sqlite3ValueNew(0); + sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC); + zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8); + if( zFilename8 ){ + rc = openDatabase(zFilename8, ppDb, + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); + assert( *ppDb || rc==SQLITE_NOMEM ); + if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){ + ENC(*ppDb) = SQLITE_UTF16NATIVE; + } + }else{ + rc = SQLITE_NOMEM; + } + sqlite3ValueFree(pVal); + + return sqlite3ApiExit(0, rc); +} +#endif /* SQLITE_OMIT_UTF16 */ + +/* +** Register a new collation sequence with the database handle db. +*/ +SQLITE_API int sqlite3_create_collation( + sqlite3* db, + const char *zName, + int enc, + void* pCtx, + int(*xCompare)(void*,int,const void*,int,const void*) +){ + int rc; + sqlite3_mutex_enter(db->mutex); + assert( !db->mallocFailed ); + rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Register a new collation sequence with the database handle db. +*/ +SQLITE_API int sqlite3_create_collation_v2( + sqlite3* db, + const char *zName, + int enc, + void* pCtx, + int(*xCompare)(void*,int,const void*,int,const void*), + void(*xDel)(void*) +){ + int rc; + sqlite3_mutex_enter(db->mutex); + assert( !db->mallocFailed ); + rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +#ifndef SQLITE_OMIT_UTF16 +/* +** Register a new collation sequence with the database handle db. +*/ +SQLITE_API int sqlite3_create_collation16( + sqlite3* db, + const void *zName, + int enc, + void* pCtx, + int(*xCompare)(void*,int,const void*,int,const void*) +){ + int rc = SQLITE_OK; + char *zName8; + sqlite3_mutex_enter(db->mutex); + assert( !db->mallocFailed ); + zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE); + if( zName8 ){ + rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0); + sqlite3DbFree(db, zName8); + } + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} +#endif /* SQLITE_OMIT_UTF16 */ + +/* +** Register a collation sequence factory callback with the database handle +** db. Replace any previously installed collation sequence factory. +*/ +SQLITE_API int sqlite3_collation_needed( + sqlite3 *db, + void *pCollNeededArg, + void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*) +){ + sqlite3_mutex_enter(db->mutex); + db->xCollNeeded = xCollNeeded; + db->xCollNeeded16 = 0; + db->pCollNeededArg = pCollNeededArg; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +#ifndef SQLITE_OMIT_UTF16 +/* +** Register a collation sequence factory callback with the database handle +** db. Replace any previously installed collation sequence factory. +*/ +SQLITE_API int sqlite3_collation_needed16( + sqlite3 *db, + void *pCollNeededArg, + void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*) +){ + sqlite3_mutex_enter(db->mutex); + db->xCollNeeded = 0; + db->xCollNeeded16 = xCollNeeded16; + db->pCollNeededArg = pCollNeededArg; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} +#endif /* SQLITE_OMIT_UTF16 */ + +#ifndef SQLITE_OMIT_DEPRECATED +/* +** This function is now an anachronism. It used to be used to recover from a +** malloc() failure, but SQLite now does this automatically. +*/ +SQLITE_API int sqlite3_global_recover(void){ + return SQLITE_OK; +} +#endif + +/* +** Test to see whether or not the database connection is in autocommit +** mode. Return TRUE if it is and FALSE if not. Autocommit mode is on +** by default. Autocommit is disabled by a BEGIN statement and reenabled +** by the next COMMIT or ROLLBACK. +** +******* THIS IS AN EXPERIMENTAL API AND IS SUBJECT TO CHANGE ****** +*/ +SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){ + return db->autoCommit; +} + +/* +** The following routines are subtitutes for constants SQLITE_CORRUPT, +** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error +** constants. They server two purposes: +** +** 1. Serve as a convenient place to set a breakpoint in a debugger +** to detect when version error conditions occurs. +** +** 2. Invoke sqlite3_log() to provide the source code location where +** a low-level error is first detected. +*/ +SQLITE_PRIVATE int sqlite3CorruptError(int lineno){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + printf("database corruption line %d\n", lineno); + sqlite3_log(SQLITE_CORRUPT, + "database corruption at line %d of [%.10s]", + lineno, 20+sqlite3_sourceid()); + return SQLITE_CORRUPT; +} +SQLITE_PRIVATE int sqlite3MisuseError(int lineno){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + sqlite3_log(SQLITE_MISUSE, + "misuse at line %d of [%.10s]", + lineno, 20+sqlite3_sourceid()); + return SQLITE_MISUSE; +} +SQLITE_PRIVATE int sqlite3CantopenError(int lineno){ + testcase( sqlite3GlobalConfig.xLog!=0 ); + sqlite3_log(SQLITE_CANTOPEN, + "cannot open file at line %d of [%.10s]", + lineno, 20+sqlite3_sourceid()); + return SQLITE_CANTOPEN; +} + + +#ifndef SQLITE_OMIT_DEPRECATED +/* +** This is a convenience routine that makes sure that all thread-specific +** data for this thread has been deallocated. +** +** SQLite no longer uses thread-specific data so this routine is now a +** no-op. It is retained for historical compatibility. +*/ +SQLITE_API void sqlite3_thread_cleanup(void){ +} +#endif + +/* +** Return meta information about a specific column of a database table. +** See comment in sqlite3.h (sqlite.h.in) for details. +*/ +#ifdef SQLITE_ENABLE_COLUMN_METADATA +SQLITE_API int sqlite3_table_column_metadata( + sqlite3 *db, /* Connection handle */ + const char *zDbName, /* Database name or NULL */ + const char *zTableName, /* Table name */ + const char *zColumnName, /* Column name */ + char const **pzDataType, /* OUTPUT: Declared data type */ + char const **pzCollSeq, /* OUTPUT: Collation sequence name */ + int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ + int *pPrimaryKey, /* OUTPUT: True if column part of PK */ + int *pAutoinc /* OUTPUT: True if column is auto-increment */ +){ + int rc; + char *zErrMsg = 0; + Table *pTab = 0; + Column *pCol = 0; + int iCol; + + char const *zDataType = 0; + char const *zCollSeq = 0; + int notnull = 0; + int primarykey = 0; + int autoinc = 0; + + /* Ensure the database schema has been loaded */ + sqlite3_mutex_enter(db->mutex); + sqlite3BtreeEnterAll(db); + rc = sqlite3Init(db, &zErrMsg); + if( SQLITE_OK!=rc ){ + goto error_out; + } + + /* Locate the table in question */ + pTab = sqlite3FindTable(db, zTableName, zDbName); + if( !pTab || pTab->pSelect ){ + pTab = 0; + goto error_out; + } + + /* Find the column for which info is requested */ + if( sqlite3IsRowid(zColumnName) ){ + iCol = pTab->iPKey; + if( iCol>=0 ){ + pCol = &pTab->aCol[iCol]; + } + }else{ + for(iCol=0; iColnCol; iCol++){ + pCol = &pTab->aCol[iCol]; + if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){ + break; + } + } + if( iCol==pTab->nCol ){ + pTab = 0; + goto error_out; + } + } + + /* The following block stores the meta information that will be returned + ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey + ** and autoinc. At this point there are two possibilities: + ** + ** 1. The specified column name was rowid", "oid" or "_rowid_" + ** and there is no explicitly declared IPK column. + ** + ** 2. The table is not a view and the column name identified an + ** explicitly declared column. Copy meta information from *pCol. + */ + if( pCol ){ + zDataType = pCol->zType; + zCollSeq = pCol->zColl; + notnull = pCol->notNull!=0; + primarykey = pCol->isPrimKey!=0; + autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0; + }else{ + zDataType = "INTEGER"; + primarykey = 1; + } + if( !zCollSeq ){ + zCollSeq = "BINARY"; + } + +error_out: + sqlite3BtreeLeaveAll(db); + + /* Whether the function call succeeded or failed, set the output parameters + ** to whatever their local counterparts contain. If an error did occur, + ** this has the effect of zeroing all output parameters. + */ + if( pzDataType ) *pzDataType = zDataType; + if( pzCollSeq ) *pzCollSeq = zCollSeq; + if( pNotNull ) *pNotNull = notnull; + if( pPrimaryKey ) *pPrimaryKey = primarykey; + if( pAutoinc ) *pAutoinc = autoinc; + + if( SQLITE_OK==rc && !pTab ){ + sqlite3DbFree(db, zErrMsg); + zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName, + zColumnName); + rc = SQLITE_ERROR; + } + sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg); + sqlite3DbFree(db, zErrMsg); + rc = sqlite3ApiExit(db, rc); + sqlite3_mutex_leave(db->mutex); + return rc; +} +#endif + +/* +** Sleep for a little while. Return the amount of time slept. +*/ +SQLITE_API int sqlite3_sleep(int ms){ + sqlite3_vfs *pVfs; + int rc; + pVfs = sqlite3_vfs_find(0); + if( pVfs==0 ) return 0; + + /* This function works in milliseconds, but the underlying OsSleep() + ** API uses microseconds. Hence the 1000's. + */ + rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000); + return rc; +} + +/* +** Enable or disable the extended result codes. +*/ +SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){ + sqlite3_mutex_enter(db->mutex); + db->errMask = onoff ? 0xffffffff : 0xff; + sqlite3_mutex_leave(db->mutex); + return SQLITE_OK; +} + +/* +** Invoke the xFileControl method on a particular database. +*/ +SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){ + int rc = SQLITE_ERROR; + int iDb; + sqlite3_mutex_enter(db->mutex); + if( zDbName==0 ){ + iDb = 0; + }else{ + for(iDb=0; iDbnDb; iDb++){ + if( strcmp(db->aDb[iDb].zName, zDbName)==0 ) break; + } + } + if( iDbnDb ){ + Btree *pBtree = db->aDb[iDb].pBt; + if( pBtree ){ + Pager *pPager; + sqlite3_file *fd; + sqlite3BtreeEnter(pBtree); + pPager = sqlite3BtreePager(pBtree); + assert( pPager!=0 ); + fd = sqlite3PagerFile(pPager); + assert( fd!=0 ); + if( op==SQLITE_FCNTL_FILE_POINTER ){ + *(sqlite3_file**)pArg = fd; + rc = SQLITE_OK; + }else if( fd->pMethods ){ + rc = sqlite3OsFileControl(fd, op, pArg); + }else{ + rc = SQLITE_NOTFOUND; + } + sqlite3BtreeLeave(pBtree); + } + } + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** Interface to the testing logic. +*/ +SQLITE_API int sqlite3_test_control(int op, ...){ + int rc = 0; +#ifndef SQLITE_OMIT_BUILTIN_TEST + va_list ap; + va_start(ap, op); + switch( op ){ + + /* + ** Save the current state of the PRNG. + */ + case SQLITE_TESTCTRL_PRNG_SAVE: { + sqlite3PrngSaveState(); + break; + } + + /* + ** Restore the state of the PRNG to the last state saved using + ** PRNG_SAVE. If PRNG_SAVE has never before been called, then + ** this verb acts like PRNG_RESET. + */ + case SQLITE_TESTCTRL_PRNG_RESTORE: { + sqlite3PrngRestoreState(); + break; + } + + /* + ** Reset the PRNG back to its uninitialized state. The next call + ** to sqlite3_randomness() will reseed the PRNG using a single call + ** to the xRandomness method of the default VFS. + */ + case SQLITE_TESTCTRL_PRNG_RESET: { + sqlite3PrngResetState(); + break; + } + + /* + ** sqlite3_test_control(BITVEC_TEST, size, program) + ** + ** Run a test against a Bitvec object of size. The program argument + ** is an array of integers that defines the test. Return -1 on a + ** memory allocation error, 0 on success, or non-zero for an error. + ** See the sqlite3BitvecBuiltinTest() for additional information. + */ + case SQLITE_TESTCTRL_BITVEC_TEST: { + int sz = va_arg(ap, int); + int *aProg = va_arg(ap, int*); + rc = sqlite3BitvecBuiltinTest(sz, aProg); + break; + } + + /* + ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd) + ** + ** Register hooks to call to indicate which malloc() failures + ** are benign. + */ + case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: { + typedef void (*void_function)(void); + void_function xBenignBegin; + void_function xBenignEnd; + xBenignBegin = va_arg(ap, void_function); + xBenignEnd = va_arg(ap, void_function); + sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd); + break; + } + + /* + ** sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X) + ** + ** Set the PENDING byte to the value in the argument, if X>0. + ** Make no changes if X==0. Return the value of the pending byte + ** as it existing before this routine was called. + ** + ** IMPORTANT: Changing the PENDING byte from 0x40000000 results in + ** an incompatible database file format. Changing the PENDING byte + ** while any database connection is open results in undefined and + ** dileterious behavior. + */ + case SQLITE_TESTCTRL_PENDING_BYTE: { + rc = PENDING_BYTE; +#ifndef SQLITE_OMIT_WSD + { + unsigned int newVal = va_arg(ap, unsigned int); + if( newVal ) sqlite3PendingByte = newVal; + } +#endif + break; + } + + /* + ** sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X) + ** + ** This action provides a run-time test to see whether or not + ** assert() was enabled at compile-time. If X is true and assert() + ** is enabled, then the return value is true. If X is true and + ** assert() is disabled, then the return value is zero. If X is + ** false and assert() is enabled, then the assertion fires and the + ** process aborts. If X is false and assert() is disabled, then the + ** return value is zero. + */ + case SQLITE_TESTCTRL_ASSERT: { + volatile int x = 0; + assert( (x = va_arg(ap,int))!=0 ); + rc = x; + break; + } + + + /* + ** sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X) + ** + ** This action provides a run-time test to see how the ALWAYS and + ** NEVER macros were defined at compile-time. + ** + ** The return value is ALWAYS(X). + ** + ** The recommended test is X==2. If the return value is 2, that means + ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the + ** default setting. If the return value is 1, then ALWAYS() is either + ** hard-coded to true or else it asserts if its argument is false. + ** The first behavior (hard-coded to true) is the case if + ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second + ** behavior (assert if the argument to ALWAYS() is false) is the case if + ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled. + ** + ** The run-time test procedure might look something like this: + ** + ** if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){ + ** // ALWAYS() and NEVER() are no-op pass-through macros + ** }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){ + ** // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false. + ** }else{ + ** // ALWAYS(x) is a constant 1. NEVER(x) is a constant 0. + ** } + */ + case SQLITE_TESTCTRL_ALWAYS: { + int x = va_arg(ap,int); + rc = ALWAYS(x); + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N) + ** + ** Set the nReserve size to N for the main database on the database + ** connection db. + */ + case SQLITE_TESTCTRL_RESERVE: { + sqlite3 *db = va_arg(ap, sqlite3*); + int x = va_arg(ap,int); + sqlite3_mutex_enter(db->mutex); + sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0); + sqlite3_mutex_leave(db->mutex); + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N) + ** + ** Enable or disable various optimizations for testing purposes. The + ** argument N is a bitmask of optimizations to be disabled. For normal + ** operation N should be 0. The idea is that a test program (like the + ** SQL Logic Test or SLT test module) can run the same SQL multiple times + ** with various optimizations disabled to verify that the same answer + ** is obtained in every case. + */ + case SQLITE_TESTCTRL_OPTIMIZATIONS: { + sqlite3 *db = va_arg(ap, sqlite3*); + int x = va_arg(ap,int); + db->flags = (x & SQLITE_OptMask) | (db->flags & ~SQLITE_OptMask); + break; + } + +#ifdef SQLITE_N_KEYWORD + /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord) + ** + ** If zWord is a keyword recognized by the parser, then return the + ** number of keywords. Or if zWord is not a keyword, return 0. + ** + ** This test feature is only available in the amalgamation since + ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite + ** is built using separate source files. + */ + case SQLITE_TESTCTRL_ISKEYWORD: { + const char *zWord = va_arg(ap, const char*); + int n = sqlite3Strlen30(zWord); + rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0; + break; + } +#endif + + /* sqlite3_test_control(SQLITE_TESTCTRL_PGHDRSZ) + ** + ** Return the size of a pcache header in bytes. + */ + case SQLITE_TESTCTRL_PGHDRSZ: { + rc = sizeof(PgHdr); + break; + } + + /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree); + ** + ** Pass pFree into sqlite3ScratchFree(). + ** If sz>0 then allocate a scratch buffer into pNew. + */ + case SQLITE_TESTCTRL_SCRATCHMALLOC: { + void *pFree, **ppNew; + int sz; + sz = va_arg(ap, int); + ppNew = va_arg(ap, void**); + pFree = va_arg(ap, void*); + if( sz ) *ppNew = sqlite3ScratchMalloc(sz); + sqlite3ScratchFree(pFree); + break; + } + + } + va_end(ap); +#endif /* SQLITE_OMIT_BUILTIN_TEST */ + return rc; +} + +/************** End of main.c ************************************************/ +/************** Begin file notify.c ******************************************/ +/* +** 2009 March 3 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** +** This file contains the implementation of the sqlite3_unlock_notify() +** API method and its associated functionality. +*/ + +/* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */ +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY + +/* +** Public interfaces: +** +** sqlite3ConnectionBlocked() +** sqlite3ConnectionUnlocked() +** sqlite3ConnectionClosed() +** sqlite3_unlock_notify() +*/ + +#define assertMutexHeld() \ + assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) ) + +/* +** Head of a linked list of all sqlite3 objects created by this process +** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection +** is not NULL. This variable may only accessed while the STATIC_MASTER +** mutex is held. +*/ +static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0; + +#ifndef NDEBUG +/* +** This function is a complex assert() that verifies the following +** properties of the blocked connections list: +** +** 1) Each entry in the list has a non-NULL value for either +** pUnlockConnection or pBlockingConnection, or both. +** +** 2) All entries in the list that share a common value for +** xUnlockNotify are grouped together. +** +** 3) If the argument db is not NULL, then none of the entries in the +** blocked connections list have pUnlockConnection or pBlockingConnection +** set to db. This is used when closing connection db. +*/ +static void checkListProperties(sqlite3 *db){ + sqlite3 *p; + for(p=sqlite3BlockedList; p; p=p->pNextBlocked){ + int seen = 0; + sqlite3 *p2; + + /* Verify property (1) */ + assert( p->pUnlockConnection || p->pBlockingConnection ); + + /* Verify property (2) */ + for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){ + if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1; + assert( p2->xUnlockNotify==p->xUnlockNotify || !seen ); + assert( db==0 || p->pUnlockConnection!=db ); + assert( db==0 || p->pBlockingConnection!=db ); + } + } +} +#else +# define checkListProperties(x) +#endif + +/* +** Remove connection db from the blocked connections list. If connection +** db is not currently a part of the list, this function is a no-op. +*/ +static void removeFromBlockedList(sqlite3 *db){ + sqlite3 **pp; + assertMutexHeld(); + for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){ + if( *pp==db ){ + *pp = (*pp)->pNextBlocked; + break; + } + } +} + +/* +** Add connection db to the blocked connections list. It is assumed +** that it is not already a part of the list. +*/ +static void addToBlockedList(sqlite3 *db){ + sqlite3 **pp; + assertMutexHeld(); + for( + pp=&sqlite3BlockedList; + *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify; + pp=&(*pp)->pNextBlocked + ); + db->pNextBlocked = *pp; + *pp = db; +} + +/* +** Obtain the STATIC_MASTER mutex. +*/ +static void enterMutex(void){ + sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); + checkListProperties(0); +} + +/* +** Release the STATIC_MASTER mutex. +*/ +static void leaveMutex(void){ + assertMutexHeld(); + checkListProperties(0); + sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); +} + +/* +** Register an unlock-notify callback. +** +** This is called after connection "db" has attempted some operation +** but has received an SQLITE_LOCKED error because another connection +** (call it pOther) in the same process was busy using the same shared +** cache. pOther is found by looking at db->pBlockingConnection. +** +** If there is no blocking connection, the callback is invoked immediately, +** before this routine returns. +** +** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate +** a deadlock. +** +** Otherwise, make arrangements to invoke xNotify when pOther drops +** its locks. +** +** Each call to this routine overrides any prior callbacks registered +** on the same "db". If xNotify==0 then any prior callbacks are immediately +** cancelled. +*/ +SQLITE_API int sqlite3_unlock_notify( + sqlite3 *db, + void (*xNotify)(void **, int), + void *pArg +){ + int rc = SQLITE_OK; + + sqlite3_mutex_enter(db->mutex); + enterMutex(); + + if( xNotify==0 ){ + removeFromBlockedList(db); + db->pBlockingConnection = 0; + db->pUnlockConnection = 0; + db->xUnlockNotify = 0; + db->pUnlockArg = 0; + }else if( 0==db->pBlockingConnection ){ + /* The blocking transaction has been concluded. Or there never was a + ** blocking transaction. In either case, invoke the notify callback + ** immediately. + */ + xNotify(&pArg, 1); + }else{ + sqlite3 *p; + + for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){} + if( p ){ + rc = SQLITE_LOCKED; /* Deadlock detected. */ + }else{ + db->pUnlockConnection = db->pBlockingConnection; + db->xUnlockNotify = xNotify; + db->pUnlockArg = pArg; + removeFromBlockedList(db); + addToBlockedList(db); + } + } + + leaveMutex(); + assert( !db->mallocFailed ); + sqlite3Error(db, rc, (rc?"database is deadlocked":0)); + sqlite3_mutex_leave(db->mutex); + return rc; +} + +/* +** This function is called while stepping or preparing a statement +** associated with connection db. The operation will return SQLITE_LOCKED +** to the user because it requires a lock that will not be available +** until connection pBlocker concludes its current transaction. +*/ +SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){ + enterMutex(); + if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){ + addToBlockedList(db); + } + db->pBlockingConnection = pBlocker; + leaveMutex(); +} + +/* +** This function is called when +** the transaction opened by database db has just finished. Locks held +** by database connection db have been released. +** +** This function loops through each entry in the blocked connections +** list and does the following: +** +** 1) If the sqlite3.pBlockingConnection member of a list entry is +** set to db, then set pBlockingConnection=0. +** +** 2) If the sqlite3.pUnlockConnection member of a list entry is +** set to db, then invoke the configured unlock-notify callback and +** set pUnlockConnection=0. +** +** 3) If the two steps above mean that pBlockingConnection==0 and +** pUnlockConnection==0, remove the entry from the blocked connections +** list. +*/ +SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){ + void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */ + int nArg = 0; /* Number of entries in aArg[] */ + sqlite3 **pp; /* Iterator variable */ + void **aArg; /* Arguments to the unlock callback */ + void **aDyn = 0; /* Dynamically allocated space for aArg[] */ + void *aStatic[16]; /* Starter space for aArg[]. No malloc required */ + + aArg = aStatic; + enterMutex(); /* Enter STATIC_MASTER mutex */ + + /* This loop runs once for each entry in the blocked-connections list. */ + for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){ + sqlite3 *p = *pp; + + /* Step 1. */ + if( p->pBlockingConnection==db ){ + p->pBlockingConnection = 0; + } + + /* Step 2. */ + if( p->pUnlockConnection==db ){ + assert( p->xUnlockNotify ); + if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){ + xUnlockNotify(aArg, nArg); + nArg = 0; + } + + sqlite3BeginBenignMalloc(); + assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) ); + assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn ); + if( (!aDyn && nArg==(int)ArraySize(aStatic)) + || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*))) + ){ + /* The aArg[] array needs to grow. */ + void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2); + if( pNew ){ + memcpy(pNew, aArg, nArg*sizeof(void *)); + sqlite3_free(aDyn); + aDyn = aArg = pNew; + }else{ + /* This occurs when the array of context pointers that need to + ** be passed to the unlock-notify callback is larger than the + ** aStatic[] array allocated on the stack and the attempt to + ** allocate a larger array from the heap has failed. + ** + ** This is a difficult situation to handle. Returning an error + ** code to the caller is insufficient, as even if an error code + ** is returned the transaction on connection db will still be + ** closed and the unlock-notify callbacks on blocked connections + ** will go unissued. This might cause the application to wait + ** indefinitely for an unlock-notify callback that will never + ** arrive. + ** + ** Instead, invoke the unlock-notify callback with the context + ** array already accumulated. We can then clear the array and + ** begin accumulating any further context pointers without + ** requiring any dynamic allocation. This is sub-optimal because + ** it means that instead of one callback with a large array of + ** context pointers the application will receive two or more + ** callbacks with smaller arrays of context pointers, which will + ** reduce the applications ability to prioritize multiple + ** connections. But it is the best that can be done under the + ** circumstances. + */ + xUnlockNotify(aArg, nArg); + nArg = 0; + } + } + sqlite3EndBenignMalloc(); + + aArg[nArg++] = p->pUnlockArg; + xUnlockNotify = p->xUnlockNotify; + p->pUnlockConnection = 0; + p->xUnlockNotify = 0; + p->pUnlockArg = 0; + } + + /* Step 3. */ + if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){ + /* Remove connection p from the blocked connections list. */ + *pp = p->pNextBlocked; + p->pNextBlocked = 0; + }else{ + pp = &p->pNextBlocked; + } + } + + if( nArg!=0 ){ + xUnlockNotify(aArg, nArg); + } + sqlite3_free(aDyn); + leaveMutex(); /* Leave STATIC_MASTER mutex */ +} + +/* +** This is called when the database connection passed as an argument is +** being closed. The connection is removed from the blocked list. +*/ +SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){ + sqlite3ConnectionUnlocked(db); + enterMutex(); + removeFromBlockedList(db); + checkListProperties(db); + leaveMutex(); +} +#endif + +/************** End of notify.c **********************************************/ +/************** Begin file fts3.c ********************************************/ +/* +** 2006 Oct 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This is an SQLite module implementing full-text search. +*/ + +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ + +/* The full-text index is stored in a series of b+tree (-like) +** structures called segments which map terms to doclists. The +** structures are like b+trees in layout, but are constructed from the +** bottom up in optimal fashion and are not updatable. Since trees +** are built from the bottom up, things will be described from the +** bottom up. +** +** +**** Varints **** +** The basic unit of encoding is a variable-length integer called a +** varint. We encode variable-length integers in little-endian order +** using seven bits * per byte as follows: +** +** KEY: +** A = 0xxxxxxx 7 bits of data and one flag bit +** B = 1xxxxxxx 7 bits of data and one flag bit +** +** 7 bits - A +** 14 bits - BA +** 21 bits - BBA +** and so on. +** +** This is similar in concept to how sqlite encodes "varints" but +** the encoding is not the same. SQLite varints are big-endian +** are are limited to 9 bytes in length whereas FTS3 varints are +** little-endian and can be up to 10 bytes in length (in theory). +** +** Example encodings: +** +** 1: 0x01 +** 127: 0x7f +** 128: 0x81 0x00 +** +** +**** Document lists **** +** A doclist (document list) holds a docid-sorted list of hits for a +** given term. Doclists hold docids and associated token positions. +** A docid is the unique integer identifier for a single document. +** A position is the index of a word within the document. The first +** word of the document has a position of 0. +** +** FTS3 used to optionally store character offsets using a compile-time +** option. But that functionality is no longer supported. +** +** A doclist is stored like this: +** +** array { +** varint docid; +** array { (position list for column 0) +** varint position; (2 more than the delta from previous position) +** } +** array { +** varint POS_COLUMN; (marks start of position list for new column) +** varint column; (index of new column) +** array { +** varint position; (2 more than the delta from previous position) +** } +** } +** varint POS_END; (marks end of positions for this document. +** } +** +** Here, array { X } means zero or more occurrences of X, adjacent in +** memory. A "position" is an index of a token in the token stream +** generated by the tokenizer. Note that POS_END and POS_COLUMN occur +** in the same logical place as the position element, and act as sentinals +** ending a position list array. POS_END is 0. POS_COLUMN is 1. +** The positions numbers are not stored literally but rather as two more +** than the difference from the prior position, or the just the position plus +** 2 for the first position. Example: +** +** label: A B C D E F G H I J K +** value: 123 5 9 1 1 14 35 0 234 72 0 +** +** The 123 value is the first docid. For column zero in this document +** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1 +** at D signals the start of a new column; the 1 at E indicates that the +** new column is column number 1. There are two positions at 12 and 45 +** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The +** 234 at I is the next docid. It has one position 72 (72-2) and then +** terminates with the 0 at K. +** +** A "position-list" is the list of positions for multiple columns for +** a single docid. A "column-list" is the set of positions for a single +** column. Hence, a position-list consists of one or more column-lists, +** a document record consists of a docid followed by a position-list and +** a doclist consists of one or more document records. +** +** A bare doclist omits the position information, becoming an +** array of varint-encoded docids. +** +**** Segment leaf nodes **** +** Segment leaf nodes store terms and doclists, ordered by term. Leaf +** nodes are written using LeafWriter, and read using LeafReader (to +** iterate through a single leaf node's data) and LeavesReader (to +** iterate through a segment's entire leaf layer). Leaf nodes have +** the format: +** +** varint iHeight; (height from leaf level, always 0) +** varint nTerm; (length of first term) +** char pTerm[nTerm]; (content of first term) +** varint nDoclist; (length of term's associated doclist) +** char pDoclist[nDoclist]; (content of doclist) +** array { +** (further terms are delta-encoded) +** varint nPrefix; (length of prefix shared with previous term) +** varint nSuffix; (length of unshared suffix) +** char pTermSuffix[nSuffix];(unshared suffix of next term) +** varint nDoclist; (length of term's associated doclist) +** char pDoclist[nDoclist]; (content of doclist) +** } +** +** Here, array { X } means zero or more occurrences of X, adjacent in +** memory. +** +** Leaf nodes are broken into blocks which are stored contiguously in +** the %_segments table in sorted order. This means that when the end +** of a node is reached, the next term is in the node with the next +** greater node id. +** +** New data is spilled to a new leaf node when the current node +** exceeds LEAF_MAX bytes (default 2048). New data which itself is +** larger than STANDALONE_MIN (default 1024) is placed in a standalone +** node (a leaf node with a single term and doclist). The goal of +** these settings is to pack together groups of small doclists while +** making it efficient to directly access large doclists. The +** assumption is that large doclists represent terms which are more +** likely to be query targets. +** +** TODO(shess) It may be useful for blocking decisions to be more +** dynamic. For instance, it may make more sense to have a 2.5k leaf +** node rather than splitting into 2k and .5k nodes. My intuition is +** that this might extend through 2x or 4x the pagesize. +** +** +**** Segment interior nodes **** +** Segment interior nodes store blockids for subtree nodes and terms +** to describe what data is stored by the each subtree. Interior +** nodes are written using InteriorWriter, and read using +** InteriorReader. InteriorWriters are created as needed when +** SegmentWriter creates new leaf nodes, or when an interior node +** itself grows too big and must be split. The format of interior +** nodes: +** +** varint iHeight; (height from leaf level, always >0) +** varint iBlockid; (block id of node's leftmost subtree) +** optional { +** varint nTerm; (length of first term) +** char pTerm[nTerm]; (content of first term) +** array { +** (further terms are delta-encoded) +** varint nPrefix; (length of shared prefix with previous term) +** varint nSuffix; (length of unshared suffix) +** char pTermSuffix[nSuffix]; (unshared suffix of next term) +** } +** } +** +** Here, optional { X } means an optional element, while array { X } +** means zero or more occurrences of X, adjacent in memory. +** +** An interior node encodes n terms separating n+1 subtrees. The +** subtree blocks are contiguous, so only the first subtree's blockid +** is encoded. The subtree at iBlockid will contain all terms less +** than the first term encoded (or all terms if no term is encoded). +** Otherwise, for terms greater than or equal to pTerm[i] but less +** than pTerm[i+1], the subtree for that term will be rooted at +** iBlockid+i. Interior nodes only store enough term data to +** distinguish adjacent children (if the rightmost term of the left +** child is "something", and the leftmost term of the right child is +** "wicked", only "w" is stored). +** +** New data is spilled to a new interior node at the same height when +** the current node exceeds INTERIOR_MAX bytes (default 2048). +** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing +** interior nodes and making the tree too skinny. The interior nodes +** at a given height are naturally tracked by interior nodes at +** height+1, and so on. +** +** +**** Segment directory **** +** The segment directory in table %_segdir stores meta-information for +** merging and deleting segments, and also the root node of the +** segment's tree. +** +** The root node is the top node of the segment's tree after encoding +** the entire segment, restricted to ROOT_MAX bytes (default 1024). +** This could be either a leaf node or an interior node. If the top +** node requires more than ROOT_MAX bytes, it is flushed to %_segments +** and a new root interior node is generated (which should always fit +** within ROOT_MAX because it only needs space for 2 varints, the +** height and the blockid of the previous root). +** +** The meta-information in the segment directory is: +** level - segment level (see below) +** idx - index within level +** - (level,idx uniquely identify a segment) +** start_block - first leaf node +** leaves_end_block - last leaf node +** end_block - last block (including interior nodes) +** root - contents of root node +** +** If the root node is a leaf node, then start_block, +** leaves_end_block, and end_block are all 0. +** +** +**** Segment merging **** +** To amortize update costs, segments are grouped into levels and +** merged in batches. Each increase in level represents exponentially +** more documents. +** +** New documents (actually, document updates) are tokenized and +** written individually (using LeafWriter) to a level 0 segment, with +** incrementing idx. When idx reaches MERGE_COUNT (default 16), all +** level 0 segments are merged into a single level 1 segment. Level 1 +** is populated like level 0, and eventually MERGE_COUNT level 1 +** segments are merged to a single level 2 segment (representing +** MERGE_COUNT^2 updates), and so on. +** +** A segment merge traverses all segments at a given level in +** parallel, performing a straightforward sorted merge. Since segment +** leaf nodes are written in to the %_segments table in order, this +** merge traverses the underlying sqlite disk structures efficiently. +** After the merge, all segment blocks from the merged level are +** deleted. +** +** MERGE_COUNT controls how often we merge segments. 16 seems to be +** somewhat of a sweet spot for insertion performance. 32 and 64 show +** very similar performance numbers to 16 on insertion, though they're +** a tiny bit slower (perhaps due to more overhead in merge-time +** sorting). 8 is about 20% slower than 16, 4 about 50% slower than +** 16, 2 about 66% slower than 16. +** +** At query time, high MERGE_COUNT increases the number of segments +** which need to be scanned and merged. For instance, with 100k docs +** inserted: +** +** MERGE_COUNT segments +** 16 25 +** 8 12 +** 4 10 +** 2 6 +** +** This appears to have only a moderate impact on queries for very +** frequent terms (which are somewhat dominated by segment merge +** costs), and infrequent and non-existent terms still seem to be fast +** even with many segments. +** +** TODO(shess) That said, it would be nice to have a better query-side +** argument for MERGE_COUNT of 16. Also, it is possible/likely that +** optimizations to things like doclist merging will swing the sweet +** spot around. +** +** +** +**** Handling of deletions and updates **** +** Since we're using a segmented structure, with no docid-oriented +** index into the term index, we clearly cannot simply update the term +** index when a document is deleted or updated. For deletions, we +** write an empty doclist (varint(docid) varint(POS_END)), for updates +** we simply write the new doclist. Segment merges overwrite older +** data for a particular docid with newer data, so deletes or updates +** will eventually overtake the earlier data and knock it out. The +** query logic likewise merges doclists so that newer data knocks out +** older data. +** +** TODO(shess) Provide a VACUUM type operation to clear out all +** deletions and duplications. This would basically be a forced merge +** into a single segment. +*/ + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE) +# define SQLITE_CORE 1 +#endif + +/************** Include fts3Int.h in the middle of fts3.c ********************/ +/************** Begin file fts3Int.h *****************************************/ +/* +** 2009 Nov 12 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +*/ + +#ifndef _FTSINT_H +#define _FTSINT_H + +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) +# define NDEBUG 1 +#endif + +/************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/ +/************** Begin file fts3_tokenizer.h **********************************/ +/* +** 2006 July 10 +** +** The author disclaims copyright to this source code. +** +************************************************************************* +** Defines the interface to tokenizers used by fulltext-search. There +** are three basic components: +** +** sqlite3_tokenizer_module is a singleton defining the tokenizer +** interface functions. This is essentially the class structure for +** tokenizers. +** +** sqlite3_tokenizer is used to define a particular tokenizer, perhaps +** including customization information defined at creation time. +** +** sqlite3_tokenizer_cursor is generated by a tokenizer to generate +** tokens from a particular input. +*/ +#ifndef _FTS3_TOKENIZER_H_ +#define _FTS3_TOKENIZER_H_ + +/* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time. +** If tokenizers are to be allowed to call sqlite3_*() functions, then +** we will need a way to register the API consistently. +*/ + +/* +** Structures used by the tokenizer interface. When a new tokenizer +** implementation is registered, the caller provides a pointer to +** an sqlite3_tokenizer_module containing pointers to the callback +** functions that make up an implementation. +** +** When an fts3 table is created, it passes any arguments passed to +** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the +** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer +** implementation. The xCreate() function in turn returns an +** sqlite3_tokenizer structure representing the specific tokenizer to +** be used for the fts3 table (customized by the tokenizer clause arguments). +** +** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen() +** method is called. It returns an sqlite3_tokenizer_cursor object +** that may be used to tokenize a specific input buffer based on +** the tokenization rules supplied by a specific sqlite3_tokenizer +** object. +*/ +typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module; +typedef struct sqlite3_tokenizer sqlite3_tokenizer; +typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor; + +struct sqlite3_tokenizer_module { + + /* + ** Structure version. Should always be set to 0. + */ + int iVersion; + + /* + ** Create a new tokenizer. The values in the argv[] array are the + ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL + ** TABLE statement that created the fts3 table. For example, if + ** the following SQL is executed: + ** + ** CREATE .. USING fts3( ... , tokenizer arg1 arg2) + ** + ** then argc is set to 2, and the argv[] array contains pointers + ** to the strings "arg1" and "arg2". + ** + ** This method should return either SQLITE_OK (0), or an SQLite error + ** code. If SQLITE_OK is returned, then *ppTokenizer should be set + ** to point at the newly created tokenizer structure. The generic + ** sqlite3_tokenizer.pModule variable should not be initialised by + ** this callback. The caller will do so. + */ + int (*xCreate)( + int argc, /* Size of argv array */ + const char *const*argv, /* Tokenizer argument strings */ + sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ + ); + + /* + ** Destroy an existing tokenizer. The fts3 module calls this method + ** exactly once for each successful call to xCreate(). + */ + int (*xDestroy)(sqlite3_tokenizer *pTokenizer); + + /* + ** Create a tokenizer cursor to tokenize an input buffer. The caller + ** is responsible for ensuring that the input buffer remains valid + ** until the cursor is closed (using the xClose() method). + */ + int (*xOpen)( + sqlite3_tokenizer *pTokenizer, /* Tokenizer object */ + const char *pInput, int nBytes, /* Input buffer */ + sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */ + ); + + /* + ** Destroy an existing tokenizer cursor. The fts3 module calls this + ** method exactly once for each successful call to xOpen(). + */ + int (*xClose)(sqlite3_tokenizer_cursor *pCursor); + + /* + ** Retrieve the next token from the tokenizer cursor pCursor. This + ** method should either return SQLITE_OK and set the values of the + ** "OUT" variables identified below, or SQLITE_DONE to indicate that + ** the end of the buffer has been reached, or an SQLite error code. + ** + ** *ppToken should be set to point at a buffer containing the + ** normalized version of the token (i.e. after any case-folding and/or + ** stemming has been performed). *pnBytes should be set to the length + ** of this buffer in bytes. The input text that generated the token is + ** identified by the byte offsets returned in *piStartOffset and + ** *piEndOffset. *piStartOffset should be set to the index of the first + ** byte of the token in the input buffer. *piEndOffset should be set + ** to the index of the first byte just past the end of the token in + ** the input buffer. + ** + ** The buffer *ppToken is set to point at is managed by the tokenizer + ** implementation. It is only required to be valid until the next call + ** to xNext() or xClose(). + */ + /* TODO(shess) current implementation requires pInput to be + ** nul-terminated. This should either be fixed, or pInput/nBytes + ** should be converted to zInput. + */ + int (*xNext)( + sqlite3_tokenizer_cursor *pCursor, /* Tokenizer cursor */ + const char **ppToken, int *pnBytes, /* OUT: Normalized text for token */ + int *piStartOffset, /* OUT: Byte offset of token in input buffer */ + int *piEndOffset, /* OUT: Byte offset of end of token in input buffer */ + int *piPosition /* OUT: Number of tokens returned before this one */ + ); +}; + +struct sqlite3_tokenizer { + const sqlite3_tokenizer_module *pModule; /* The module for this tokenizer */ + /* Tokenizer implementations will typically add additional fields */ +}; + +struct sqlite3_tokenizer_cursor { + sqlite3_tokenizer *pTokenizer; /* Tokenizer for this cursor. */ + /* Tokenizer implementations will typically add additional fields */ +}; + +int fts3_global_term_cnt(int iTerm, int iCol); +int fts3_term_cnt(int iTerm, int iCol); + + +#endif /* _FTS3_TOKENIZER_H_ */ + +/************** End of fts3_tokenizer.h **************************************/ +/************** Continuing where we left off in fts3Int.h ********************/ +/************** Include fts3_hash.h in the middle of fts3Int.h ***************/ +/************** Begin file fts3_hash.h ***************************************/ +/* +** 2001 September 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This is the header file for the generic hash-table implemenation +** used in SQLite. We've modified it slightly to serve as a standalone +** hash table implementation for the full-text indexing module. +** +*/ +#ifndef _FTS3_HASH_H_ +#define _FTS3_HASH_H_ + +/* Forward declarations of structures. */ +typedef struct Fts3Hash Fts3Hash; +typedef struct Fts3HashElem Fts3HashElem; + +/* A complete hash table is an instance of the following structure. +** The internals of this structure are intended to be opaque -- client +** code should not attempt to access or modify the fields of this structure +** directly. Change this structure only by using the routines below. +** However, many of the "procedures" and "functions" for modifying and +** accessing this structure are really macros, so we can't really make +** this structure opaque. +*/ +struct Fts3Hash { + char keyClass; /* HASH_INT, _POINTER, _STRING, _BINARY */ + char copyKey; /* True if copy of key made on insert */ + int count; /* Number of entries in this table */ + Fts3HashElem *first; /* The first element of the array */ + int htsize; /* Number of buckets in the hash table */ + struct _fts3ht { /* the hash table */ + int count; /* Number of entries with this hash */ + Fts3HashElem *chain; /* Pointer to first entry with this hash */ + } *ht; +}; + +/* Each element in the hash table is an instance of the following +** structure. All elements are stored on a single doubly-linked list. +** +** Again, this structure is intended to be opaque, but it can't really +** be opaque because it is used by macros. +*/ +struct Fts3HashElem { + Fts3HashElem *next, *prev; /* Next and previous elements in the table */ + void *data; /* Data associated with this element */ + void *pKey; int nKey; /* Key associated with this element */ +}; + +/* +** There are 2 different modes of operation for a hash table: +** +** FTS3_HASH_STRING pKey points to a string that is nKey bytes long +** (including the null-terminator, if any). Case +** is respected in comparisons. +** +** FTS3_HASH_BINARY pKey points to binary data nKey bytes long. +** memcmp() is used to compare keys. +** +** A copy of the key is made if the copyKey parameter to fts3HashInit is 1. +*/ +#define FTS3_HASH_STRING 1 +#define FTS3_HASH_BINARY 2 + +/* +** Access routines. To delete, insert a NULL pointer. +*/ +SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey); +SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData); +SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey); +SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*); +SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int); + +/* +** Shorthand for the functions above +*/ +#define fts3HashInit sqlite3Fts3HashInit +#define fts3HashInsert sqlite3Fts3HashInsert +#define fts3HashFind sqlite3Fts3HashFind +#define fts3HashClear sqlite3Fts3HashClear +#define fts3HashFindElem sqlite3Fts3HashFindElem + +/* +** Macros for looping over all elements of a hash table. The idiom is +** like this: +** +** Fts3Hash h; +** Fts3HashElem *p; +** ... +** for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){ +** SomeStructure *pData = fts3HashData(p); +** // do something with pData +** } +*/ +#define fts3HashFirst(H) ((H)->first) +#define fts3HashNext(E) ((E)->next) +#define fts3HashData(E) ((E)->data) +#define fts3HashKey(E) ((E)->pKey) +#define fts3HashKeysize(E) ((E)->nKey) + +/* +** Number of entries in a hash table +*/ +#define fts3HashCount(H) ((H)->count) + +#endif /* _FTS3_HASH_H_ */ + +/************** End of fts3_hash.h *******************************************/ +/************** Continuing where we left off in fts3Int.h ********************/ + +/* +** This constant controls how often segments are merged. Once there are +** FTS3_MERGE_COUNT segments of level N, they are merged into a single +** segment of level N+1. +*/ +#define FTS3_MERGE_COUNT 16 + +/* +** This is the maximum amount of data (in bytes) to store in the +** Fts3Table.pendingTerms hash table. Normally, the hash table is +** populated as documents are inserted/updated/deleted in a transaction +** and used to create a new segment when the transaction is committed. +** However if this limit is reached midway through a transaction, a new +** segment is created and the hash table cleared immediately. +*/ +#define FTS3_MAX_PENDING_DATA (1*1024*1024) + +/* +** Macro to return the number of elements in an array. SQLite has a +** similar macro called ArraySize(). Use a different name to avoid +** a collision when building an amalgamation with built-in FTS3. +*/ +#define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0]))) + +/* +** Maximum length of a varint encoded integer. The varint format is different +** from that used by SQLite, so the maximum length is 10, not 9. +*/ +#define FTS3_VARINT_MAX 10 + +/* +** The testcase() macro is only used by the amalgamation. If undefined, +** make it a no-op. +*/ +#ifndef testcase +# define testcase(X) +#endif + +/* +** Terminator values for position-lists and column-lists. +*/ +#define POS_COLUMN (1) /* Column-list terminator */ +#define POS_END (0) /* Position-list terminator */ + +/* +** This section provides definitions to allow the +** FTS3 extension to be compiled outside of the +** amalgamation. +*/ +#ifndef SQLITE_AMALGAMATION +/* +** Macros indicating that conditional expressions are always true or +** false. +*/ +#ifdef SQLITE_COVERAGE_TEST +# define ALWAYS(x) (1) +# define NEVER(X) (0) +#else +# define ALWAYS(x) (x) +# define NEVER(X) (x) +#endif + +/* +** Internal types used by SQLite. +*/ +typedef unsigned char u8; /* 1-byte (or larger) unsigned integer */ +typedef short int i16; /* 2-byte (or larger) signed integer */ +typedef unsigned int u32; /* 4-byte unsigned integer */ +typedef sqlite3_uint64 u64; /* 8-byte unsigned integer */ +/* +** Macro used to suppress compiler warnings for unused parameters. +*/ +#define UNUSED_PARAMETER(x) (void)(x) +#endif + +typedef struct Fts3Table Fts3Table; +typedef struct Fts3Cursor Fts3Cursor; +typedef struct Fts3Expr Fts3Expr; +typedef struct Fts3Phrase Fts3Phrase; +typedef struct Fts3PhraseToken Fts3PhraseToken; + +typedef struct Fts3SegFilter Fts3SegFilter; +typedef struct Fts3DeferredToken Fts3DeferredToken; +typedef struct Fts3SegReader Fts3SegReader; +typedef struct Fts3SegReaderCursor Fts3SegReaderCursor; + +/* +** A connection to a fulltext index is an instance of the following +** structure. The xCreate and xConnect methods create an instance +** of this structure and xDestroy and xDisconnect free that instance. +** All other methods receive a pointer to the structure as one of their +** arguments. +*/ +struct Fts3Table { + sqlite3_vtab base; /* Base class used by SQLite core */ + sqlite3 *db; /* The database connection */ + const char *zDb; /* logical database name */ + const char *zName; /* virtual table name */ + int nColumn; /* number of named columns in virtual table */ + char **azColumn; /* column names. malloced */ + sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */ + + /* Precompiled statements used by the implementation. Each of these + ** statements is run and reset within a single virtual table API call. + */ + sqlite3_stmt *aStmt[24]; + + char *zReadExprlist; + char *zWriteExprlist; + + int nNodeSize; /* Soft limit for node size */ + u8 bHasStat; /* True if %_stat table exists */ + u8 bHasDocsize; /* True if %_docsize table exists */ + int nPgsz; /* Page size for host database */ + char *zSegmentsTbl; /* Name of %_segments table */ + sqlite3_blob *pSegments; /* Blob handle open on %_segments table */ + + /* The following hash table is used to buffer pending index updates during + ** transactions. Variable nPendingData estimates the memory size of the + ** pending data, including hash table overhead, but not malloc overhead. + ** When nPendingData exceeds nMaxPendingData, the buffer is flushed + ** automatically. Variable iPrevDocid is the docid of the most recently + ** inserted record. + */ + int nMaxPendingData; + int nPendingData; + sqlite_int64 iPrevDocid; + Fts3Hash pendingTerms; +}; + +/* +** When the core wants to read from the virtual table, it creates a +** virtual table cursor (an instance of the following structure) using +** the xOpen method. Cursors are destroyed using the xClose method. +*/ +struct Fts3Cursor { + sqlite3_vtab_cursor base; /* Base class used by SQLite core */ + i16 eSearch; /* Search strategy (see below) */ + u8 isEof; /* True if at End Of Results */ + u8 isRequireSeek; /* True if must seek pStmt to %_content row */ + sqlite3_stmt *pStmt; /* Prepared statement in use by the cursor */ + Fts3Expr *pExpr; /* Parsed MATCH query string */ + int nPhrase; /* Number of matchable phrases in query */ + Fts3DeferredToken *pDeferred; /* Deferred search tokens, if any */ + sqlite3_int64 iPrevId; /* Previous id read from aDoclist */ + char *pNextId; /* Pointer into the body of aDoclist */ + char *aDoclist; /* List of docids for full-text queries */ + int nDoclist; /* Size of buffer at aDoclist */ + int eEvalmode; /* An FTS3_EVAL_XX constant */ + int nRowAvg; /* Average size of database rows, in pages */ + + int isMatchinfoNeeded; /* True when aMatchinfo[] needs filling in */ + u32 *aMatchinfo; /* Information about most recent match */ + int nMatchinfo; /* Number of elements in aMatchinfo[] */ + char *zMatchinfo; /* Matchinfo specification */ +}; + +#define FTS3_EVAL_FILTER 0 +#define FTS3_EVAL_NEXT 1 +#define FTS3_EVAL_MATCHINFO 2 + +/* +** The Fts3Cursor.eSearch member is always set to one of the following. +** Actualy, Fts3Cursor.eSearch can be greater than or equal to +** FTS3_FULLTEXT_SEARCH. If so, then Fts3Cursor.eSearch - 2 is the index +** of the column to be searched. For example, in +** +** CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d); +** SELECT docid FROM ex1 WHERE b MATCH 'one two three'; +** +** Because the LHS of the MATCH operator is 2nd column "b", +** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1. (+0 for a, +** +1 for b, +2 for c, +3 for d.) If the LHS of MATCH were "ex1" +** indicating that all columns should be searched, +** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4. +*/ +#define FTS3_FULLSCAN_SEARCH 0 /* Linear scan of %_content table */ +#define FTS3_DOCID_SEARCH 1 /* Lookup by rowid on %_content table */ +#define FTS3_FULLTEXT_SEARCH 2 /* Full-text index search */ + +/* +** A "phrase" is a sequence of one or more tokens that must match in +** sequence. A single token is the base case and the most common case. +** For a sequence of tokens contained in double-quotes (i.e. "one two three") +** nToken will be the number of tokens in the string. +** +** The nDocMatch and nMatch variables contain data that may be used by the +** matchinfo() function. They are populated when the full-text index is +** queried for hits on the phrase. If one or more tokens in the phrase +** are deferred, the nDocMatch and nMatch variables are populated based +** on the assumption that the +*/ +struct Fts3PhraseToken { + char *z; /* Text of the token */ + int n; /* Number of bytes in buffer z */ + int isPrefix; /* True if token ends with a "*" character */ + int bFulltext; /* True if full-text index was used */ + Fts3SegReaderCursor *pSegcsr; /* Segment-reader for this token */ + Fts3DeferredToken *pDeferred; /* Deferred token object for this token */ +}; + +struct Fts3Phrase { + /* Variables populated by fts3_expr.c when parsing a MATCH expression */ + int nToken; /* Number of tokens in the phrase */ + int iColumn; /* Index of column this phrase must match */ + int isNot; /* Phrase prefixed by unary not (-) operator */ + Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */ +}; + +/* +** A tree of these objects forms the RHS of a MATCH operator. +** +** If Fts3Expr.eType is either FTSQUERY_NEAR or FTSQUERY_PHRASE and isLoaded +** is true, then aDoclist points to a malloced buffer, size nDoclist bytes, +** containing the results of the NEAR or phrase query in FTS3 doclist +** format. As usual, the initial "Length" field found in doclists stored +** on disk is omitted from this buffer. +** +** Variable pCurrent always points to the start of a docid field within +** aDoclist. Since the doclist is usually scanned in docid order, this can +** be used to accelerate seeking to the required docid within the doclist. +*/ +struct Fts3Expr { + int eType; /* One of the FTSQUERY_XXX values defined below */ + int nNear; /* Valid if eType==FTSQUERY_NEAR */ + Fts3Expr *pParent; /* pParent->pLeft==this or pParent->pRight==this */ + Fts3Expr *pLeft; /* Left operand */ + Fts3Expr *pRight; /* Right operand */ + Fts3Phrase *pPhrase; /* Valid if eType==FTSQUERY_PHRASE */ + + int isLoaded; /* True if aDoclist/nDoclist are initialized. */ + char *aDoclist; /* Buffer containing doclist */ + int nDoclist; /* Size of aDoclist in bytes */ + + sqlite3_int64 iCurrent; + char *pCurrent; +}; + +/* +** Candidate values for Fts3Query.eType. Note that the order of the first +** four values is in order of precedence when parsing expressions. For +** example, the following: +** +** "a OR b AND c NOT d NEAR e" +** +** is equivalent to: +** +** "a OR (b AND (c NOT (d NEAR e)))" +*/ +#define FTSQUERY_NEAR 1 +#define FTSQUERY_NOT 2 +#define FTSQUERY_AND 3 +#define FTSQUERY_OR 4 +#define FTSQUERY_PHRASE 5 + + +/* fts3_write.c */ +SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*); +SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *); +SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *); +SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *); +SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, sqlite3_int64, + sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**); +SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(Fts3Table*,const char*,int,int,Fts3SegReader**); +SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *); +SQLITE_PRIVATE int sqlite3Fts3SegReaderCost(Fts3Cursor *, Fts3SegReader *, int *); +SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, sqlite3_stmt **); +SQLITE_PRIVATE int sqlite3Fts3ReadLock(Fts3Table *); +SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*); + +SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **); +SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **); + +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *); +SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int); +SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *); +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *); +SQLITE_PRIVATE char *sqlite3Fts3DeferredDoclist(Fts3DeferredToken *, int *); +SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *); + +#define FTS3_SEGCURSOR_PENDING -1 +#define FTS3_SEGCURSOR_ALL -2 + +SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3SegReaderCursor*, Fts3SegFilter*); +SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3SegReaderCursor *); +SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3SegReaderCursor *); +SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor( + Fts3Table *, int, const char *, int, int, int, Fts3SegReaderCursor *); + +/* Flags allowed as part of the 4th argument to SegmentReaderIterate() */ +#define FTS3_SEGMENT_REQUIRE_POS 0x00000001 +#define FTS3_SEGMENT_IGNORE_EMPTY 0x00000002 +#define FTS3_SEGMENT_COLUMN_FILTER 0x00000004 +#define FTS3_SEGMENT_PREFIX 0x00000008 +#define FTS3_SEGMENT_SCAN 0x00000010 + +/* Type passed as 4th argument to SegmentReaderIterate() */ +struct Fts3SegFilter { + const char *zTerm; + int nTerm; + int iCol; + int flags; +}; + +struct Fts3SegReaderCursor { + /* Used internally by sqlite3Fts3SegReaderXXX() calls */ + Fts3SegReader **apSegment; /* Array of Fts3SegReader objects */ + int nSegment; /* Size of apSegment array */ + int nAdvance; /* How many seg-readers to advance */ + Fts3SegFilter *pFilter; /* Pointer to filter object */ + char *aBuffer; /* Buffer to merge doclists in */ + int nBuffer; /* Allocated size of aBuffer[] in bytes */ + + /* Cost of running this iterator. Used by fts3.c only. */ + int nCost; + + /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */ + char *zTerm; /* Pointer to term buffer */ + int nTerm; /* Size of zTerm in bytes */ + char *aDoclist; /* Pointer to doclist buffer */ + int nDoclist; /* Size of aDoclist[] in bytes */ +}; + +/* fts3.c */ +SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64); +SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *); +SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *); +SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64); +SQLITE_PRIVATE void sqlite3Fts3Dequote(char *); + +SQLITE_PRIVATE char *sqlite3Fts3FindPositions(Fts3Expr *, sqlite3_int64, int); +SQLITE_PRIVATE int sqlite3Fts3ExprLoadDoclist(Fts3Cursor *, Fts3Expr *); +SQLITE_PRIVATE int sqlite3Fts3ExprLoadFtDoclist(Fts3Cursor *, Fts3Expr *, char **, int *); +SQLITE_PRIVATE int sqlite3Fts3ExprNearTrim(Fts3Expr *, Fts3Expr *, int); + +/* fts3_tokenizer.c */ +SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *); +SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *); +SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *, + sqlite3_tokenizer **, char ** +); +SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char); + +/* fts3_snippet.c */ +SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*); +SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *, + const char *, const char *, int, int +); +SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *); + +/* fts3_expr.c */ +SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, + char **, int, int, const char *, int, Fts3Expr ** +); +SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *); +#ifdef SQLITE_TEST +SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db); +#endif + +/* fts3_aux.c */ +SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db); + +#endif /* _FTSINT_H */ + +/************** End of fts3Int.h *********************************************/ +/************** Continuing where we left off in fts3.c ***********************/ + + +#ifndef SQLITE_CORE + SQLITE_EXTENSION_INIT1 +#endif + +/* +** Write a 64-bit variable-length integer to memory starting at p[0]. +** The length of data written will be between 1 and FTS3_VARINT_MAX bytes. +** The number of bytes written is returned. +*/ +SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){ + unsigned char *q = (unsigned char *) p; + sqlite_uint64 vu = v; + do{ + *q++ = (unsigned char) ((vu & 0x7f) | 0x80); + vu >>= 7; + }while( vu!=0 ); + q[-1] &= 0x7f; /* turn off high bit in final byte */ + assert( q - (unsigned char *)p <= FTS3_VARINT_MAX ); + return (int) (q - (unsigned char *)p); +} + +/* +** Read a 64-bit variable-length integer from memory starting at p[0]. +** Return the number of bytes read, or 0 on error. +** The value is stored in *v. +*/ +SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){ + const unsigned char *q = (const unsigned char *) p; + sqlite_uint64 x = 0, y = 1; + while( (*q&0x80)==0x80 && q-(unsigned char *)p>= 7; + }while( v!=0 ); + return i; +} + +/* +** Convert an SQL-style quoted string into a normal string by removing +** the quote characters. The conversion is done in-place. If the +** input does not begin with a quote character, then this routine +** is a no-op. +** +** Examples: +** +** "abc" becomes abc +** 'xyz' becomes xyz +** [pqr] becomes pqr +** `mno` becomes mno +** +*/ +SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){ + char quote; /* Quote character (if any ) */ + + quote = z[0]; + if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){ + int iIn = 1; /* Index of next byte to read from input */ + int iOut = 0; /* Index of next byte to write to output */ + + /* If the first byte was a '[', then the close-quote character is a ']' */ + if( quote=='[' ) quote = ']'; + + while( ALWAYS(z[iIn]) ){ + if( z[iIn]==quote ){ + if( z[iIn+1]!=quote ) break; + z[iOut++] = quote; + iIn += 2; + }else{ + z[iOut++] = z[iIn++]; + } + } + z[iOut] = '\0'; + } +} + +/* +** Read a single varint from the doclist at *pp and advance *pp to point +** to the first byte past the end of the varint. Add the value of the varint +** to *pVal. +*/ +static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){ + sqlite3_int64 iVal; + *pp += sqlite3Fts3GetVarint(*pp, &iVal); + *pVal += iVal; +} + +/* +** As long as *pp has not reached its end (pEnd), then do the same +** as fts3GetDeltaVarint(): read a single varint and add it to *pVal. +** But if we have reached the end of the varint, just set *pp=0 and +** leave *pVal unchanged. +*/ +static void fts3GetDeltaVarint2(char **pp, char *pEnd, sqlite3_int64 *pVal){ + if( *pp>=pEnd ){ + *pp = 0; + }else{ + fts3GetDeltaVarint(pp, pVal); + } +} + +/* +** The xDisconnect() virtual table method. +*/ +static int fts3DisconnectMethod(sqlite3_vtab *pVtab){ + Fts3Table *p = (Fts3Table *)pVtab; + int i; + + assert( p->nPendingData==0 ); + assert( p->pSegments==0 ); + + /* Free any prepared statements held */ + for(i=0; iaStmt); i++){ + sqlite3_finalize(p->aStmt[i]); + } + sqlite3_free(p->zSegmentsTbl); + sqlite3_free(p->zReadExprlist); + sqlite3_free(p->zWriteExprlist); + + /* Invoke the tokenizer destructor to free the tokenizer. */ + p->pTokenizer->pModule->xDestroy(p->pTokenizer); + + sqlite3_free(p); + return SQLITE_OK; +} + +/* +** Construct one or more SQL statements from the format string given +** and then evaluate those statements. The success code is written +** into *pRc. +** +** If *pRc is initially non-zero then this routine is a no-op. +*/ +static void fts3DbExec( + int *pRc, /* Success code */ + sqlite3 *db, /* Database in which to run SQL */ + const char *zFormat, /* Format string for SQL */ + ... /* Arguments to the format string */ +){ + va_list ap; + char *zSql; + if( *pRc ) return; + va_start(ap, zFormat); + zSql = sqlite3_vmprintf(zFormat, ap); + va_end(ap); + if( zSql==0 ){ + *pRc = SQLITE_NOMEM; + }else{ + *pRc = sqlite3_exec(db, zSql, 0, 0, 0); + sqlite3_free(zSql); + } +} + +/* +** The xDestroy() virtual table method. +*/ +static int fts3DestroyMethod(sqlite3_vtab *pVtab){ + int rc = SQLITE_OK; /* Return code */ + Fts3Table *p = (Fts3Table *)pVtab; + sqlite3 *db = p->db; + + /* Drop the shadow tables */ + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", p->zDb, p->zName); + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", p->zDb,p->zName); + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", p->zDb, p->zName); + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", p->zDb, p->zName); + fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", p->zDb, p->zName); + + /* If everything has worked, invoke fts3DisconnectMethod() to free the + ** memory associated with the Fts3Table structure and return SQLITE_OK. + ** Otherwise, return an SQLite error code. + */ + return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc); +} + + +/* +** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table +** passed as the first argument. This is done as part of the xConnect() +** and xCreate() methods. +** +** If *pRc is non-zero when this function is called, it is a no-op. +** Otherwise, if an error occurs, an SQLite error code is stored in *pRc +** before returning. +*/ +static void fts3DeclareVtab(int *pRc, Fts3Table *p){ + if( *pRc==SQLITE_OK ){ + int i; /* Iterator variable */ + int rc; /* Return code */ + char *zSql; /* SQL statement passed to declare_vtab() */ + char *zCols; /* List of user defined columns */ + + /* Create a list of user columns for the virtual table */ + zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]); + for(i=1; zCols && inColumn; i++){ + zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]); + } + + /* Create the whole "CREATE TABLE" statement to pass to SQLite */ + zSql = sqlite3_mprintf( + "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN)", zCols, p->zName + ); + if( !zCols || !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_declare_vtab(p->db, zSql); + } + + sqlite3_free(zSql); + sqlite3_free(zCols); + *pRc = rc; + } +} + +/* +** Create the backing store tables (%_content, %_segments and %_segdir) +** required by the FTS3 table passed as the only argument. This is done +** as part of the vtab xCreate() method. +** +** If the p->bHasDocsize boolean is true (indicating that this is an +** FTS4 table, not an FTS3 table) then also create the %_docsize and +** %_stat tables required by FTS4. +*/ +static int fts3CreateTables(Fts3Table *p){ + int rc = SQLITE_OK; /* Return code */ + int i; /* Iterator variable */ + char *zContentCols; /* Columns of %_content table */ + sqlite3 *db = p->db; /* The database connection */ + + /* Create a list of user columns for the content table */ + zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY"); + for(i=0; zContentCols && inColumn; i++){ + char *z = p->azColumn[i]; + zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z); + } + if( zContentCols==0 ) rc = SQLITE_NOMEM; + + /* Create the content table */ + fts3DbExec(&rc, db, + "CREATE TABLE %Q.'%q_content'(%s)", + p->zDb, p->zName, zContentCols + ); + sqlite3_free(zContentCols); + /* Create other tables */ + fts3DbExec(&rc, db, + "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);", + p->zDb, p->zName + ); + fts3DbExec(&rc, db, + "CREATE TABLE %Q.'%q_segdir'(" + "level INTEGER," + "idx INTEGER," + "start_block INTEGER," + "leaves_end_block INTEGER," + "end_block INTEGER," + "root BLOB," + "PRIMARY KEY(level, idx)" + ");", + p->zDb, p->zName + ); + if( p->bHasDocsize ){ + fts3DbExec(&rc, db, + "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);", + p->zDb, p->zName + ); + } + if( p->bHasStat ){ + fts3DbExec(&rc, db, + "CREATE TABLE %Q.'%q_stat'(id INTEGER PRIMARY KEY, value BLOB);", + p->zDb, p->zName + ); + } + return rc; +} + +/* +** Store the current database page-size in bytes in p->nPgsz. +** +** If *pRc is non-zero when this function is called, it is a no-op. +** Otherwise, if an error occurs, an SQLite error code is stored in *pRc +** before returning. +*/ +static void fts3DatabasePageSize(int *pRc, Fts3Table *p){ + if( *pRc==SQLITE_OK ){ + int rc; /* Return code */ + char *zSql; /* SQL text "PRAGMA %Q.page_size" */ + sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */ + + zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb); + if( !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_step(pStmt); + p->nPgsz = sqlite3_column_int(pStmt, 0); + rc = sqlite3_finalize(pStmt); + } + } + assert( p->nPgsz>0 || rc!=SQLITE_OK ); + sqlite3_free(zSql); + *pRc = rc; + } +} + +/* +** "Special" FTS4 arguments are column specifications of the following form: +** +** = +** +** There may not be whitespace surrounding the "=" character. The +** term may be quoted, but the may not. +*/ +static int fts3IsSpecialColumn( + const char *z, + int *pnKey, + char **pzValue +){ + char *zValue; + const char *zCsr = z; + + while( *zCsr!='=' ){ + if( *zCsr=='\0' ) return 0; + zCsr++; + } + + *pnKey = (int)(zCsr-z); + zValue = sqlite3_mprintf("%s", &zCsr[1]); + if( zValue ){ + sqlite3Fts3Dequote(zValue); + } + *pzValue = zValue; + return 1; +} + +/* +** Append the output of a printf() style formatting to an existing string. +*/ +static void fts3Appendf( + int *pRc, /* IN/OUT: Error code */ + char **pz, /* IN/OUT: Pointer to string buffer */ + const char *zFormat, /* Printf format string to append */ + ... /* Arguments for printf format string */ +){ + if( *pRc==SQLITE_OK ){ + va_list ap; + char *z; + va_start(ap, zFormat); + z = sqlite3_vmprintf(zFormat, ap); + if( z && *pz ){ + char *z2 = sqlite3_mprintf("%s%s", *pz, z); + sqlite3_free(z); + z = z2; + } + if( z==0 ) *pRc = SQLITE_NOMEM; + sqlite3_free(*pz); + *pz = z; + } +} + +/* +** Return a copy of input string zInput enclosed in double-quotes (") and +** with all double quote characters escaped. For example: +** +** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\"" +** +** The pointer returned points to memory obtained from sqlite3_malloc(). It +** is the callers responsibility to call sqlite3_free() to release this +** memory. +*/ +static char *fts3QuoteId(char const *zInput){ + int nRet; + char *zRet; + nRet = 2 + strlen(zInput)*2 + 1; + zRet = sqlite3_malloc(nRet); + if( zRet ){ + int i; + char *z = zRet; + *(z++) = '"'; + for(i=0; zInput[i]; i++){ + if( zInput[i]=='"' ) *(z++) = '"'; + *(z++) = zInput[i]; + } + *(z++) = '"'; + *(z++) = '\0'; + } + return zRet; +} + +/* +** Return a list of comma separated SQL expressions that could be used +** in a SELECT statement such as the following: +** +** SELECT FROM %_content AS x ... +** +** to return the docid, followed by each column of text data in order +** from left to write. If parameter zFunc is not NULL, then instead of +** being returned directly each column of text data is passed to an SQL +** function named zFunc first. For example, if zFunc is "unzip" and the +** table has the three user-defined columns "a", "b", and "c", the following +** string is returned: +** +** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c')" +** +** The pointer returned points to a buffer allocated by sqlite3_malloc(). It +** is the responsibility of the caller to eventually free it. +** +** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and +** a NULL pointer is returned). Otherwise, if an OOM error is encountered +** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If +** no error occurs, *pRc is left unmodified. +*/ +static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){ + char *zRet = 0; + char *zFree = 0; + char *zFunction; + int i; + + if( !zFunc ){ + zFunction = ""; + }else{ + zFree = zFunction = fts3QuoteId(zFunc); + } + fts3Appendf(pRc, &zRet, "docid"); + for(i=0; inColumn; i++){ + fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]); + } + sqlite3_free(zFree); + return zRet; +} + +/* +** Return a list of N comma separated question marks, where N is the number +** of columns in the %_content table (one for the docid plus one for each +** user-defined text column). +** +** If argument zFunc is not NULL, then all but the first question mark +** is preceded by zFunc and an open bracket, and followed by a closed +** bracket. For example, if zFunc is "zip" and the FTS3 table has three +** user-defined text columns, the following string is returned: +** +** "?, zip(?), zip(?), zip(?)" +** +** The pointer returned points to a buffer allocated by sqlite3_malloc(). It +** is the responsibility of the caller to eventually free it. +** +** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and +** a NULL pointer is returned). Otherwise, if an OOM error is encountered +** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If +** no error occurs, *pRc is left unmodified. +*/ +static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){ + char *zRet = 0; + char *zFree = 0; + char *zFunction; + int i; + + if( !zFunc ){ + zFunction = ""; + }else{ + zFree = zFunction = fts3QuoteId(zFunc); + } + fts3Appendf(pRc, &zRet, "?"); + for(i=0; inColumn; i++){ + fts3Appendf(pRc, &zRet, ",%s(?)", zFunction); + } + sqlite3_free(zFree); + return zRet; +} + +/* +** This function is the implementation of both the xConnect and xCreate +** methods of the FTS3 virtual table. +** +** The argv[] array contains the following: +** +** argv[0] -> module name ("fts3" or "fts4") +** argv[1] -> database name +** argv[2] -> table name +** argv[...] -> "column name" and other module argument fields. +*/ +static int fts3InitVtab( + int isCreate, /* True for xCreate, false for xConnect */ + sqlite3 *db, /* The SQLite database connection */ + void *pAux, /* Hash table containing tokenizers */ + int argc, /* Number of elements in argv array */ + const char * const *argv, /* xCreate/xConnect argument array */ + sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */ + char **pzErr /* Write any error message here */ +){ + Fts3Hash *pHash = (Fts3Hash *)pAux; + Fts3Table *p = 0; /* Pointer to allocated vtab */ + int rc = SQLITE_OK; /* Return code */ + int i; /* Iterator variable */ + int nByte; /* Size of allocation used for *p */ + int iCol; /* Column index */ + int nString = 0; /* Bytes required to hold all column names */ + int nCol = 0; /* Number of columns in the FTS table */ + char *zCsr; /* Space for holding column names */ + int nDb; /* Bytes required to hold database name */ + int nName; /* Bytes required to hold table name */ + int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */ + int bNoDocsize = 0; /* True to omit %_docsize table */ + const char **aCol; /* Array of column names */ + sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */ + + char *zCompress = 0; + char *zUncompress = 0; + + assert( strlen(argv[0])==4 ); + assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4) + || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4) + ); + + nDb = (int)strlen(argv[1]) + 1; + nName = (int)strlen(argv[2]) + 1; + + aCol = (const char **)sqlite3_malloc(sizeof(const char *) * (argc-2) ); + if( !aCol ) return SQLITE_NOMEM; + memset((void *)aCol, 0, sizeof(const char *) * (argc-2)); + + /* Loop through all of the arguments passed by the user to the FTS3/4 + ** module (i.e. all the column names and special arguments). This loop + ** does the following: + ** + ** + Figures out the number of columns the FTSX table will have, and + ** the number of bytes of space that must be allocated to store copies + ** of the column names. + ** + ** + If there is a tokenizer specification included in the arguments, + ** initializes the tokenizer pTokenizer. + */ + for(i=3; rc==SQLITE_OK && i8 + && 0==sqlite3_strnicmp(z, "tokenize", 8) + && 0==sqlite3Fts3IsIdChar(z[8]) + ){ + rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr); + } + + /* Check if it is an FTS4 special argument. */ + else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){ + if( !zVal ){ + rc = SQLITE_NOMEM; + goto fts3_init_out; + } + if( nKey==9 && 0==sqlite3_strnicmp(z, "matchinfo", 9) ){ + if( strlen(zVal)==4 && 0==sqlite3_strnicmp(zVal, "fts3", 4) ){ + bNoDocsize = 1; + }else{ + *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal); + rc = SQLITE_ERROR; + } + }else if( nKey==8 && 0==sqlite3_strnicmp(z, "compress", 8) ){ + zCompress = zVal; + zVal = 0; + }else if( nKey==10 && 0==sqlite3_strnicmp(z, "uncompress", 10) ){ + zUncompress = zVal; + zVal = 0; + }else{ + *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z); + rc = SQLITE_ERROR; + } + sqlite3_free(zVal); + } + + /* Otherwise, the argument is a column name. */ + else { + nString += (int)(strlen(z) + 1); + aCol[nCol++] = z; + } + } + if( rc!=SQLITE_OK ) goto fts3_init_out; + + if( nCol==0 ){ + assert( nString==0 ); + aCol[0] = "content"; + nString = 8; + nCol = 1; + } + + if( pTokenizer==0 ){ + rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr); + if( rc!=SQLITE_OK ) goto fts3_init_out; + } + assert( pTokenizer ); + + + /* Allocate and populate the Fts3Table structure. */ + nByte = sizeof(Fts3Table) + /* Fts3Table */ + nCol * sizeof(char *) + /* azColumn */ + nName + /* zName */ + nDb + /* zDb */ + nString; /* Space for azColumn strings */ + p = (Fts3Table*)sqlite3_malloc(nByte); + if( p==0 ){ + rc = SQLITE_NOMEM; + goto fts3_init_out; + } + memset(p, 0, nByte); + p->db = db; + p->nColumn = nCol; + p->nPendingData = 0; + p->azColumn = (char **)&p[1]; + p->pTokenizer = pTokenizer; + p->nNodeSize = 1000; + p->nMaxPendingData = FTS3_MAX_PENDING_DATA; + p->bHasDocsize = (isFts4 && bNoDocsize==0); + p->bHasStat = isFts4; + fts3HashInit(&p->pendingTerms, FTS3_HASH_STRING, 1); + + /* Fill in the zName and zDb fields of the vtab structure. */ + zCsr = (char *)&p->azColumn[nCol]; + p->zName = zCsr; + memcpy(zCsr, argv[2], nName); + zCsr += nName; + p->zDb = zCsr; + memcpy(zCsr, argv[1], nDb); + zCsr += nDb; + + /* Fill in the azColumn array */ + for(iCol=0; iColazColumn[iCol] = zCsr; + zCsr += n+1; + assert( zCsr <= &((char *)p)[nByte] ); + } + + if( (zCompress==0)!=(zUncompress==0) ){ + char const *zMiss = (zCompress==0 ? "compress" : "uncompress"); + rc = SQLITE_ERROR; + *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss); + } + p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc); + p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc); + if( rc!=SQLITE_OK ) goto fts3_init_out; + + /* If this is an xCreate call, create the underlying tables in the + ** database. TODO: For xConnect(), it could verify that said tables exist. + */ + if( isCreate ){ + rc = fts3CreateTables(p); + } + + /* Figure out the page-size for the database. This is required in order to + ** estimate the cost of loading large doclists from the database (see + ** function sqlite3Fts3SegReaderCost() for details). + */ + fts3DatabasePageSize(&rc, p); + + /* Declare the table schema to SQLite. */ + fts3DeclareVtab(&rc, p); + +fts3_init_out: + sqlite3_free(zCompress); + sqlite3_free(zUncompress); + sqlite3_free((void *)aCol); + if( rc!=SQLITE_OK ){ + if( p ){ + fts3DisconnectMethod((sqlite3_vtab *)p); + }else if( pTokenizer ){ + pTokenizer->pModule->xDestroy(pTokenizer); + } + }else{ + *ppVTab = &p->base; + } + return rc; +} + +/* +** The xConnect() and xCreate() methods for the virtual table. All the +** work is done in function fts3InitVtab(). +*/ +static int fts3ConnectMethod( + sqlite3 *db, /* Database connection */ + void *pAux, /* Pointer to tokenizer hash table */ + int argc, /* Number of elements in argv array */ + const char * const *argv, /* xCreate/xConnect argument array */ + sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ + char **pzErr /* OUT: sqlite3_malloc'd error message */ +){ + return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr); +} +static int fts3CreateMethod( + sqlite3 *db, /* Database connection */ + void *pAux, /* Pointer to tokenizer hash table */ + int argc, /* Number of elements in argv array */ + const char * const *argv, /* xCreate/xConnect argument array */ + sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ + char **pzErr /* OUT: sqlite3_malloc'd error message */ +){ + return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr); +} + +/* +** Implementation of the xBestIndex method for FTS3 tables. There +** are three possible strategies, in order of preference: +** +** 1. Direct lookup by rowid or docid. +** 2. Full-text search using a MATCH operator on a non-docid column. +** 3. Linear scan of %_content table. +*/ +static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){ + Fts3Table *p = (Fts3Table *)pVTab; + int i; /* Iterator variable */ + int iCons = -1; /* Index of constraint to use */ + + /* By default use a full table scan. This is an expensive option, + ** so search through the constraints to see if a more efficient + ** strategy is possible. + */ + pInfo->idxNum = FTS3_FULLSCAN_SEARCH; + pInfo->estimatedCost = 500000; + for(i=0; inConstraint; i++){ + struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i]; + if( pCons->usable==0 ) continue; + + /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */ + if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ + && (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1 ) + ){ + pInfo->idxNum = FTS3_DOCID_SEARCH; + pInfo->estimatedCost = 1.0; + iCons = i; + } + + /* A MATCH constraint. Use a full-text search. + ** + ** If there is more than one MATCH constraint available, use the first + ** one encountered. If there is both a MATCH constraint and a direct + ** rowid/docid lookup, prefer the MATCH strategy. This is done even + ** though the rowid/docid lookup is faster than a MATCH query, selecting + ** it would lead to an "unable to use function MATCH in the requested + ** context" error. + */ + if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH + && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn + ){ + pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn; + pInfo->estimatedCost = 2.0; + iCons = i; + break; + } + } + + if( iCons>=0 ){ + pInfo->aConstraintUsage[iCons].argvIndex = 1; + pInfo->aConstraintUsage[iCons].omit = 1; + } + return SQLITE_OK; +} + +/* +** Implementation of xOpen method. +*/ +static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ + sqlite3_vtab_cursor *pCsr; /* Allocated cursor */ + + UNUSED_PARAMETER(pVTab); + + /* Allocate a buffer large enough for an Fts3Cursor structure. If the + ** allocation succeeds, zero it and return SQLITE_OK. Otherwise, + ** if the allocation fails, return SQLITE_NOMEM. + */ + *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor)); + if( !pCsr ){ + return SQLITE_NOMEM; + } + memset(pCsr, 0, sizeof(Fts3Cursor)); + return SQLITE_OK; +} + +/* +** Close the cursor. For additional information see the documentation +** on the xClose method of the virtual table interface. +*/ +static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){ + Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; + assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 ); + sqlite3_finalize(pCsr->pStmt); + sqlite3Fts3ExprFree(pCsr->pExpr); + sqlite3Fts3FreeDeferredTokens(pCsr); + sqlite3_free(pCsr->aDoclist); + sqlite3_free(pCsr->aMatchinfo); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* +** Position the pCsr->pStmt statement so that it is on the row +** of the %_content table that contains the last match. Return +** SQLITE_OK on success. +*/ +static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){ + if( pCsr->isRequireSeek ){ + pCsr->isRequireSeek = 0; + sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId); + if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){ + return SQLITE_OK; + }else{ + int rc = sqlite3_reset(pCsr->pStmt); + if( rc==SQLITE_OK ){ + /* If no row was found and no error has occured, then the %_content + ** table is missing a row that is present in the full-text index. + ** The data structures are corrupt. + */ + rc = SQLITE_CORRUPT; + } + pCsr->isEof = 1; + if( pContext ){ + sqlite3_result_error_code(pContext, rc); + } + return rc; + } + }else{ + return SQLITE_OK; + } +} + +/* +** This function is used to process a single interior node when searching +** a b-tree for a term or term prefix. The node data is passed to this +** function via the zNode/nNode parameters. The term to search for is +** passed in zTerm/nTerm. +** +** If piFirst is not NULL, then this function sets *piFirst to the blockid +** of the child node that heads the sub-tree that may contain the term. +** +** If piLast is not NULL, then *piLast is set to the right-most child node +** that heads a sub-tree that may contain a term for which zTerm/nTerm is +** a prefix. +** +** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK. +*/ +static int fts3ScanInteriorNode( + const char *zTerm, /* Term to select leaves for */ + int nTerm, /* Size of term zTerm in bytes */ + const char *zNode, /* Buffer containing segment interior node */ + int nNode, /* Size of buffer at zNode */ + sqlite3_int64 *piFirst, /* OUT: Selected child node */ + sqlite3_int64 *piLast /* OUT: Selected child node */ +){ + int rc = SQLITE_OK; /* Return code */ + const char *zCsr = zNode; /* Cursor to iterate through node */ + const char *zEnd = &zCsr[nNode];/* End of interior node buffer */ + char *zBuffer = 0; /* Buffer to load terms into */ + int nAlloc = 0; /* Size of allocated buffer */ + int isFirstTerm = 1; /* True when processing first term on page */ + sqlite3_int64 iChild; /* Block id of child node to descend to */ + + /* Skip over the 'height' varint that occurs at the start of every + ** interior node. Then load the blockid of the left-child of the b-tree + ** node into variable iChild. + ** + ** Even if the data structure on disk is corrupted, this (reading two + ** varints from the buffer) does not risk an overread. If zNode is a + ** root node, then the buffer comes from a SELECT statement. SQLite does + ** not make this guarantee explicitly, but in practice there are always + ** either more than 20 bytes of allocated space following the nNode bytes of + ** contents, or two zero bytes. Or, if the node is read from the %_segments + ** table, then there are always 20 bytes of zeroed padding following the + ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details). + */ + zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); + zCsr += sqlite3Fts3GetVarint(zCsr, &iChild); + if( zCsr>zEnd ){ + return SQLITE_CORRUPT; + } + + while( zCsrzEnd ){ + rc = SQLITE_CORRUPT; + goto finish_scan; + } + if( nPrefix+nSuffix>nAlloc ){ + char *zNew; + nAlloc = (nPrefix+nSuffix) * 2; + zNew = (char *)sqlite3_realloc(zBuffer, nAlloc); + if( !zNew ){ + rc = SQLITE_NOMEM; + goto finish_scan; + } + zBuffer = zNew; + } + memcpy(&zBuffer[nPrefix], zCsr, nSuffix); + nBuffer = nPrefix + nSuffix; + zCsr += nSuffix; + + /* Compare the term we are searching for with the term just loaded from + ** the interior node. If the specified term is greater than or equal + ** to the term from the interior node, then all terms on the sub-tree + ** headed by node iChild are smaller than zTerm. No need to search + ** iChild. + ** + ** If the interior node term is larger than the specified term, then + ** the tree headed by iChild may contain the specified term. + */ + cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer)); + if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){ + *piFirst = iChild; + piFirst = 0; + } + + if( piLast && cmp<0 ){ + *piLast = iChild; + piLast = 0; + } + + iChild++; + }; + + if( piFirst ) *piFirst = iChild; + if( piLast ) *piLast = iChild; + + finish_scan: + sqlite3_free(zBuffer); + return rc; +} + + +/* +** The buffer pointed to by argument zNode (size nNode bytes) contains an +** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes) +** contains a term. This function searches the sub-tree headed by the zNode +** node for the range of leaf nodes that may contain the specified term +** or terms for which the specified term is a prefix. +** +** If piLeaf is not NULL, then *piLeaf is set to the blockid of the +** left-most leaf node in the tree that may contain the specified term. +** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the +** right-most leaf node that may contain a term for which the specified +** term is a prefix. +** +** It is possible that the range of returned leaf nodes does not contain +** the specified term or any terms for which it is a prefix. However, if the +** segment does contain any such terms, they are stored within the identified +** range. Because this function only inspects interior segment nodes (and +** never loads leaf nodes into memory), it is not possible to be sure. +** +** If an error occurs, an error code other than SQLITE_OK is returned. +*/ +static int fts3SelectLeaf( + Fts3Table *p, /* Virtual table handle */ + const char *zTerm, /* Term to select leaves for */ + int nTerm, /* Size of term zTerm in bytes */ + const char *zNode, /* Buffer containing segment interior node */ + int nNode, /* Size of buffer at zNode */ + sqlite3_int64 *piLeaf, /* Selected leaf node */ + sqlite3_int64 *piLeaf2 /* Selected leaf node */ +){ + int rc; /* Return code */ + int iHeight; /* Height of this node in tree */ + + assert( piLeaf || piLeaf2 ); + + sqlite3Fts3GetVarint32(zNode, &iHeight); + rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2); + assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) ); + + if( rc==SQLITE_OK && iHeight>1 ){ + char *zBlob = 0; /* Blob read from %_segments table */ + int nBlob; /* Size of zBlob in bytes */ + + if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){ + rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob); + if( rc==SQLITE_OK ){ + rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0); + } + sqlite3_free(zBlob); + piLeaf = 0; + zBlob = 0; + } + + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3ReadBlock(p, piLeaf ? *piLeaf : *piLeaf2, &zBlob, &nBlob); + } + if( rc==SQLITE_OK ){ + rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2); + } + sqlite3_free(zBlob); + } + + return rc; +} + +/* +** This function is used to create delta-encoded serialized lists of FTS3 +** varints. Each call to this function appends a single varint to a list. +*/ +static void fts3PutDeltaVarint( + char **pp, /* IN/OUT: Output pointer */ + sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */ + sqlite3_int64 iVal /* Write this value to the list */ +){ + assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) ); + *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev); + *piPrev = iVal; +} + +/* +** When this function is called, *ppPoslist is assumed to point to the +** start of a position-list. After it returns, *ppPoslist points to the +** first byte after the position-list. +** +** A position list is list of positions (delta encoded) and columns for +** a single document record of a doclist. So, in other words, this +** routine advances *ppPoslist so that it points to the next docid in +** the doclist, or to the first byte past the end of the doclist. +** +** If pp is not NULL, then the contents of the position list are copied +** to *pp. *pp is set to point to the first byte past the last byte copied +** before this function returns. +*/ +static void fts3PoslistCopy(char **pp, char **ppPoslist){ + char *pEnd = *ppPoslist; + char c = 0; + + /* The end of a position list is marked by a zero encoded as an FTS3 + ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by + ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail + ** of some other, multi-byte, value. + ** + ** The following while-loop moves pEnd to point to the first byte that is not + ** immediately preceded by a byte with the 0x80 bit set. Then increments + ** pEnd once more so that it points to the byte immediately following the + ** last byte in the position-list. + */ + while( *pEnd | c ){ + c = *pEnd++ & 0x80; + testcase( c!=0 && (*pEnd)==0 ); + } + pEnd++; /* Advance past the POS_END terminator byte */ + + if( pp ){ + int n = (int)(pEnd - *ppPoslist); + char *p = *pp; + memcpy(p, *ppPoslist, n); + p += n; + *pp = p; + } + *ppPoslist = pEnd; +} + +/* +** When this function is called, *ppPoslist is assumed to point to the +** start of a column-list. After it returns, *ppPoslist points to the +** to the terminator (POS_COLUMN or POS_END) byte of the column-list. +** +** A column-list is list of delta-encoded positions for a single column +** within a single document within a doclist. +** +** The column-list is terminated either by a POS_COLUMN varint (1) or +** a POS_END varint (0). This routine leaves *ppPoslist pointing to +** the POS_COLUMN or POS_END that terminates the column-list. +** +** If pp is not NULL, then the contents of the column-list are copied +** to *pp. *pp is set to point to the first byte past the last byte copied +** before this function returns. The POS_COLUMN or POS_END terminator +** is not copied into *pp. +*/ +static void fts3ColumnlistCopy(char **pp, char **ppPoslist){ + char *pEnd = *ppPoslist; + char c = 0; + + /* A column-list is terminated by either a 0x01 or 0x00 byte that is + ** not part of a multi-byte varint. + */ + while( 0xFE & (*pEnd | c) ){ + c = *pEnd++ & 0x80; + testcase( c!=0 && ((*pEnd)&0xfe)==0 ); + } + if( pp ){ + int n = (int)(pEnd - *ppPoslist); + char *p = *pp; + memcpy(p, *ppPoslist, n); + p += n; + *pp = p; + } + *ppPoslist = pEnd; +} + +/* +** Value used to signify the end of an position-list. This is safe because +** it is not possible to have a document with 2^31 terms. +*/ +#define POSITION_LIST_END 0x7fffffff + +/* +** This function is used to help parse position-lists. When this function is +** called, *pp may point to the start of the next varint in the position-list +** being parsed, or it may point to 1 byte past the end of the position-list +** (in which case **pp will be a terminator bytes POS_END (0) or +** (1)). +** +** If *pp points past the end of the current position-list, set *pi to +** POSITION_LIST_END and return. Otherwise, read the next varint from *pp, +** increment the current value of *pi by the value read, and set *pp to +** point to the next value before returning. +** +** Before calling this routine *pi must be initialized to the value of +** the previous position, or zero if we are reading the first position +** in the position-list. Because positions are delta-encoded, the value +** of the previous position is needed in order to compute the value of +** the next position. +*/ +static void fts3ReadNextPos( + char **pp, /* IN/OUT: Pointer into position-list buffer */ + sqlite3_int64 *pi /* IN/OUT: Value read from position-list */ +){ + if( (**pp)&0xFE ){ + fts3GetDeltaVarint(pp, pi); + *pi -= 2; + }else{ + *pi = POSITION_LIST_END; + } +} + +/* +** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by +** the value of iCol encoded as a varint to *pp. This will start a new +** column list. +** +** Set *pp to point to the byte just after the last byte written before +** returning (do not modify it if iCol==0). Return the total number of bytes +** written (0 if iCol==0). +*/ +static int fts3PutColNumber(char **pp, int iCol){ + int n = 0; /* Number of bytes written */ + if( iCol ){ + char *p = *pp; /* Output pointer */ + n = 1 + sqlite3Fts3PutVarint(&p[1], iCol); + *p = 0x01; + *pp = &p[n]; + } + return n; +} + +/* +** Compute the union of two position lists. The output written +** into *pp contains all positions of both *pp1 and *pp2 in sorted +** order and with any duplicates removed. All pointers are +** updated appropriately. The caller is responsible for insuring +** that there is enough space in *pp to hold the complete output. +*/ +static void fts3PoslistMerge( + char **pp, /* Output buffer */ + char **pp1, /* Left input list */ + char **pp2 /* Right input list */ +){ + char *p = *pp; + char *p1 = *pp1; + char *p2 = *pp2; + + while( *p1 || *p2 ){ + int iCol1; /* The current column index in pp1 */ + int iCol2; /* The current column index in pp2 */ + + if( *p1==POS_COLUMN ) sqlite3Fts3GetVarint32(&p1[1], &iCol1); + else if( *p1==POS_END ) iCol1 = POSITION_LIST_END; + else iCol1 = 0; + + if( *p2==POS_COLUMN ) sqlite3Fts3GetVarint32(&p2[1], &iCol2); + else if( *p2==POS_END ) iCol2 = POSITION_LIST_END; + else iCol2 = 0; + + if( iCol1==iCol2 ){ + sqlite3_int64 i1 = 0; /* Last position from pp1 */ + sqlite3_int64 i2 = 0; /* Last position from pp2 */ + sqlite3_int64 iPrev = 0; + int n = fts3PutColNumber(&p, iCol1); + p1 += n; + p2 += n; + + /* At this point, both p1 and p2 point to the start of column-lists + ** for the same column (the column with index iCol1 and iCol2). + ** A column-list is a list of non-negative delta-encoded varints, each + ** incremented by 2 before being stored. Each list is terminated by a + ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists + ** and writes the results to buffer p. p is left pointing to the byte + ** after the list written. No terminator (POS_END or POS_COLUMN) is + ** written to the output. + */ + fts3GetDeltaVarint(&p1, &i1); + fts3GetDeltaVarint(&p2, &i2); + do { + fts3PutDeltaVarint(&p, &iPrev, (i1pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e. +** when the *pp1 token appears before the *pp2 token, but not more than nToken +** slots before it. +*/ +static int fts3PoslistPhraseMerge( + char **pp, /* IN/OUT: Preallocated output buffer */ + int nToken, /* Maximum difference in token positions */ + int isSaveLeft, /* Save the left position */ + int isExact, /* If *pp1 is exactly nTokens before *pp2 */ + char **pp1, /* IN/OUT: Left input list */ + char **pp2 /* IN/OUT: Right input list */ +){ + char *p = (pp ? *pp : 0); + char *p1 = *pp1; + char *p2 = *pp2; + int iCol1 = 0; + int iCol2 = 0; + + /* Never set both isSaveLeft and isExact for the same invocation. */ + assert( isSaveLeft==0 || isExact==0 ); + + assert( *p1!=0 && *p2!=0 ); + if( *p1==POS_COLUMN ){ + p1++; + p1 += sqlite3Fts3GetVarint32(p1, &iCol1); + } + if( *p2==POS_COLUMN ){ + p2++; + p2 += sqlite3Fts3GetVarint32(p2, &iCol2); + } + + while( 1 ){ + if( iCol1==iCol2 ){ + char *pSave = p; + sqlite3_int64 iPrev = 0; + sqlite3_int64 iPos1 = 0; + sqlite3_int64 iPos2 = 0; + + if( pp && iCol1 ){ + *p++ = POS_COLUMN; + p += sqlite3Fts3PutVarint(p, iCol1); + } + + assert( *p1!=POS_END && *p1!=POS_COLUMN ); + assert( *p2!=POS_END && *p2!=POS_COLUMN ); + fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2; + fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; + + while( 1 ){ + if( iPos2==iPos1+nToken + || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken) + ){ + sqlite3_int64 iSave; + if( !pp ){ + fts3PoslistCopy(0, &p2); + fts3PoslistCopy(0, &p1); + *pp1 = p1; + *pp2 = p2; + return 1; + } + iSave = isSaveLeft ? iPos1 : iPos2; + fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2; + pSave = 0; + } + if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){ + if( (*p2&0xFE)==0 ) break; + fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2; + }else{ + if( (*p1&0xFE)==0 ) break; + fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2; + } + } + + if( pSave ){ + assert( pp && p ); + p = pSave; + } + + fts3ColumnlistCopy(0, &p1); + fts3ColumnlistCopy(0, &p2); + assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 ); + if( 0==*p1 || 0==*p2 ) break; + + p1++; + p1 += sqlite3Fts3GetVarint32(p1, &iCol1); + p2++; + p2 += sqlite3Fts3GetVarint32(p2, &iCol2); + } + + /* Advance pointer p1 or p2 (whichever corresponds to the smaller of + ** iCol1 and iCol2) so that it points to either the 0x00 that marks the + ** end of the position list, or the 0x01 that precedes the next + ** column-number in the position list. + */ + else if( iCol1 D */ +#define MERGE_AND 3 /* D + D -> D */ +#define MERGE_OR 4 /* D + D -> D */ +#define MERGE_POS_OR 5 /* P + P -> P */ +#define MERGE_PHRASE 6 /* P + P -> D */ +#define MERGE_POS_PHRASE 7 /* P + P -> P */ +#define MERGE_NEAR 8 /* P + P -> D */ +#define MERGE_POS_NEAR 9 /* P + P -> P */ + +/* +** Merge the two doclists passed in buffer a1 (size n1 bytes) and a2 +** (size n2 bytes). The output is written to pre-allocated buffer aBuffer, +** which is guaranteed to be large enough to hold the results. The number +** of bytes written to aBuffer is stored in *pnBuffer before returning. +** +** If successful, SQLITE_OK is returned. Otherwise, if a malloc error +** occurs while allocating a temporary buffer as part of the merge operation, +** SQLITE_NOMEM is returned. +*/ +static int fts3DoclistMerge( + int mergetype, /* One of the MERGE_XXX constants */ + int nParam1, /* Used by MERGE_NEAR and MERGE_POS_NEAR */ + int nParam2, /* Used by MERGE_NEAR and MERGE_POS_NEAR */ + char *aBuffer, /* Pre-allocated output buffer */ + int *pnBuffer, /* OUT: Bytes written to aBuffer */ + char *a1, /* Buffer containing first doclist */ + int n1, /* Size of buffer a1 */ + char *a2, /* Buffer containing second doclist */ + int n2, /* Size of buffer a2 */ + int *pnDoc /* OUT: Number of docids in output */ +){ + sqlite3_int64 i1 = 0; + sqlite3_int64 i2 = 0; + sqlite3_int64 iPrev = 0; + + char *p = aBuffer; + char *p1 = a1; + char *p2 = a2; + char *pEnd1 = &a1[n1]; + char *pEnd2 = &a2[n2]; + int nDoc = 0; + + assert( mergetype==MERGE_OR || mergetype==MERGE_POS_OR + || mergetype==MERGE_AND || mergetype==MERGE_NOT + || mergetype==MERGE_PHRASE || mergetype==MERGE_POS_PHRASE + || mergetype==MERGE_NEAR || mergetype==MERGE_POS_NEAR + ); + + if( !aBuffer ){ + *pnBuffer = 0; + return SQLITE_NOMEM; + } + + /* Read the first docid from each doclist */ + fts3GetDeltaVarint2(&p1, pEnd1, &i1); + fts3GetDeltaVarint2(&p2, pEnd2, &i2); + + switch( mergetype ){ + case MERGE_OR: + case MERGE_POS_OR: + while( p1 || p2 ){ + if( p2 && p1 && i1==i2 ){ + fts3PutDeltaVarint(&p, &iPrev, i1); + if( mergetype==MERGE_POS_OR ) fts3PoslistMerge(&p, &p1, &p2); + fts3GetDeltaVarint2(&p1, pEnd1, &i1); + fts3GetDeltaVarint2(&p2, pEnd2, &i2); + }else if( !p2 || (p1 && i1isReqPos ? MERGE_POS_OR : MERGE_OR); + char *aOut = 0; + int nOut = 0; + int i; + + /* Loop through the doclists in the aaOutput[] array. Merge them all + ** into a single doclist. + */ + for(i=0; iaaOutput); i++){ + if( pTS->aaOutput[i] ){ + if( !aOut ){ + aOut = pTS->aaOutput[i]; + nOut = pTS->anOutput[i]; + pTS->aaOutput[i] = 0; + }else{ + int nNew = nOut + pTS->anOutput[i]; + char *aNew = sqlite3_malloc(nNew); + if( !aNew ){ + sqlite3_free(aOut); + return SQLITE_NOMEM; + } + fts3DoclistMerge(mergetype, 0, 0, + aNew, &nNew, pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, 0 + ); + sqlite3_free(pTS->aaOutput[i]); + sqlite3_free(aOut); + pTS->aaOutput[i] = 0; + aOut = aNew; + nOut = nNew; + } + } + } + + pTS->aaOutput[0] = aOut; + pTS->anOutput[0] = nOut; + return SQLITE_OK; +} + +/* +** This function is used as the sqlite3Fts3SegReaderIterate() callback when +** querying the full-text index for a doclist associated with a term or +** term-prefix. +*/ +static int fts3TermSelectCb( + Fts3Table *p, /* Virtual table object */ + void *pContext, /* Pointer to TermSelect structure */ + char *zTerm, + int nTerm, + char *aDoclist, + int nDoclist +){ + TermSelect *pTS = (TermSelect *)pContext; + + UNUSED_PARAMETER(p); + UNUSED_PARAMETER(zTerm); + UNUSED_PARAMETER(nTerm); + + if( pTS->aaOutput[0]==0 ){ + /* If this is the first term selected, copy the doclist to the output + ** buffer using memcpy(). TODO: Add a way to transfer control of the + ** aDoclist buffer from the caller so as to avoid the memcpy(). + */ + pTS->aaOutput[0] = sqlite3_malloc(nDoclist); + pTS->anOutput[0] = nDoclist; + if( pTS->aaOutput[0] ){ + memcpy(pTS->aaOutput[0], aDoclist, nDoclist); + }else{ + return SQLITE_NOMEM; + } + }else{ + int mergetype = (pTS->isReqPos ? MERGE_POS_OR : MERGE_OR); + char *aMerge = aDoclist; + int nMerge = nDoclist; + int iOut; + + for(iOut=0; iOutaaOutput); iOut++){ + char *aNew; + int nNew; + if( pTS->aaOutput[iOut]==0 ){ + assert( iOut>0 ); + pTS->aaOutput[iOut] = aMerge; + pTS->anOutput[iOut] = nMerge; + break; + } + + nNew = nMerge + pTS->anOutput[iOut]; + aNew = sqlite3_malloc(nNew); + if( !aNew ){ + if( aMerge!=aDoclist ){ + sqlite3_free(aMerge); + } + return SQLITE_NOMEM; + } + fts3DoclistMerge(mergetype, 0, 0, aNew, &nNew, + pTS->aaOutput[iOut], pTS->anOutput[iOut], aMerge, nMerge, 0 + ); + + if( iOut>0 ) sqlite3_free(aMerge); + sqlite3_free(pTS->aaOutput[iOut]); + pTS->aaOutput[iOut] = 0; + + aMerge = aNew; + nMerge = nNew; + if( (iOut+1)==SizeofArray(pTS->aaOutput) ){ + pTS->aaOutput[iOut] = aMerge; + pTS->anOutput[iOut] = nMerge; + } + } + } + return SQLITE_OK; +} + +static int fts3DeferredTermSelect( + Fts3DeferredToken *pToken, /* Phrase token */ + int isTermPos, /* True to include positions */ + int *pnOut, /* OUT: Size of list */ + char **ppOut /* OUT: Body of list */ +){ + char *aSource; + int nSource; + + aSource = sqlite3Fts3DeferredDoclist(pToken, &nSource); + if( !aSource ){ + *pnOut = 0; + *ppOut = 0; + }else if( isTermPos ){ + *ppOut = sqlite3_malloc(nSource); + if( !*ppOut ) return SQLITE_NOMEM; + memcpy(*ppOut, aSource, nSource); + *pnOut = nSource; + }else{ + sqlite3_int64 docid; + *pnOut = sqlite3Fts3GetVarint(aSource, &docid); + *ppOut = sqlite3_malloc(*pnOut); + if( !*ppOut ) return SQLITE_NOMEM; + sqlite3Fts3PutVarint(*ppOut, docid); + } + + return SQLITE_OK; +} + +SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor( + Fts3Table *p, /* FTS3 table handle */ + int iLevel, /* Level of segments to scan */ + const char *zTerm, /* Term to query for */ + int nTerm, /* Size of zTerm in bytes */ + int isPrefix, /* True for a prefix search */ + int isScan, /* True to scan from zTerm to EOF */ + Fts3SegReaderCursor *pCsr /* Cursor object to populate */ +){ + int rc = SQLITE_OK; + int rc2; + int iAge = 0; + sqlite3_stmt *pStmt = 0; + Fts3SegReader *pPending = 0; + + assert( iLevel==FTS3_SEGCURSOR_ALL + || iLevel==FTS3_SEGCURSOR_PENDING + || iLevel>=0 + ); + assert( FTS3_SEGCURSOR_PENDING<0 ); + assert( FTS3_SEGCURSOR_ALL<0 ); + assert( iLevel==FTS3_SEGCURSOR_ALL || (zTerm==0 && isPrefix==1) ); + assert( isPrefix==0 || isScan==0 ); + + + memset(pCsr, 0, sizeof(Fts3SegReaderCursor)); + + /* If iLevel is less than 0, include a seg-reader for the pending-terms. */ + assert( isScan==0 || fts3HashCount(&p->pendingTerms)==0 ); + if( iLevel<0 && isScan==0 ){ + rc = sqlite3Fts3SegReaderPending(p, zTerm, nTerm, isPrefix, &pPending); + if( rc==SQLITE_OK && pPending ){ + int nByte = (sizeof(Fts3SegReader *) * 16); + pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte); + if( pCsr->apSegment==0 ){ + rc = SQLITE_NOMEM; + }else{ + pCsr->apSegment[0] = pPending; + pCsr->nSegment = 1; + pPending = 0; + } + } + } + + if( iLevel!=FTS3_SEGCURSOR_PENDING ){ + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3AllSegdirs(p, iLevel, &pStmt); + } + while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){ + + /* Read the values returned by the SELECT into local variables. */ + sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1); + sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2); + sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3); + int nRoot = sqlite3_column_bytes(pStmt, 4); + char const *zRoot = sqlite3_column_blob(pStmt, 4); + + /* If nSegment is a multiple of 16 the array needs to be extended. */ + if( (pCsr->nSegment%16)==0 ){ + Fts3SegReader **apNew; + int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*); + apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte); + if( !apNew ){ + rc = SQLITE_NOMEM; + goto finished; + } + pCsr->apSegment = apNew; + } + + /* If zTerm is not NULL, and this segment is not stored entirely on its + ** root node, the range of leaves scanned can be reduced. Do this. */ + if( iStartBlock && zTerm ){ + sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0); + rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi); + if( rc!=SQLITE_OK ) goto finished; + if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock; + } + + rc = sqlite3Fts3SegReaderNew(iAge, iStartBlock, iLeavesEndBlock, + iEndBlock, zRoot, nRoot, &pCsr->apSegment[pCsr->nSegment] + ); + if( rc!=SQLITE_OK ) goto finished; + pCsr->nSegment++; + iAge++; + } + } + + finished: + rc2 = sqlite3_reset(pStmt); + if( rc==SQLITE_DONE ) rc = rc2; + sqlite3Fts3SegReaderFree(pPending); + + return rc; +} + + +static int fts3TermSegReaderCursor( + Fts3Cursor *pCsr, /* Virtual table cursor handle */ + const char *zTerm, /* Term to query for */ + int nTerm, /* Size of zTerm in bytes */ + int isPrefix, /* True for a prefix search */ + Fts3SegReaderCursor **ppSegcsr /* OUT: Allocated seg-reader cursor */ +){ + Fts3SegReaderCursor *pSegcsr; /* Object to allocate and return */ + int rc = SQLITE_NOMEM; /* Return code */ + + pSegcsr = sqlite3_malloc(sizeof(Fts3SegReaderCursor)); + if( pSegcsr ){ + Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; + int i; + int nCost = 0; + rc = sqlite3Fts3SegReaderCursor( + p, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr); + + for(i=0; rc==SQLITE_OK && inSegment; i++){ + rc = sqlite3Fts3SegReaderCost(pCsr, pSegcsr->apSegment[i], &nCost); + } + pSegcsr->nCost = nCost; + } + + *ppSegcsr = pSegcsr; + return rc; +} + +static void fts3SegReaderCursorFree(Fts3SegReaderCursor *pSegcsr){ + sqlite3Fts3SegReaderFinish(pSegcsr); + sqlite3_free(pSegcsr); +} + +/* +** This function retreives the doclist for the specified term (or term +** prefix) from the database. +** +** The returned doclist may be in one of two formats, depending on the +** value of parameter isReqPos. If isReqPos is zero, then the doclist is +** a sorted list of delta-compressed docids (a bare doclist). If isReqPos +** is non-zero, then the returned list is in the same format as is stored +** in the database without the found length specifier at the start of on-disk +** doclists. +*/ +static int fts3TermSelect( + Fts3Table *p, /* Virtual table handle */ + Fts3PhraseToken *pTok, /* Token to query for */ + int iColumn, /* Column to query (or -ve for all columns) */ + int isReqPos, /* True to include position lists in output */ + int *pnOut, /* OUT: Size of buffer at *ppOut */ + char **ppOut /* OUT: Malloced result buffer */ +){ + int rc; /* Return code */ + Fts3SegReaderCursor *pSegcsr; /* Seg-reader cursor for this term */ + TermSelect tsc; /* Context object for fts3TermSelectCb() */ + Fts3SegFilter filter; /* Segment term filter configuration */ + + pSegcsr = pTok->pSegcsr; + memset(&tsc, 0, sizeof(TermSelect)); + tsc.isReqPos = isReqPos; + + filter.flags = FTS3_SEGMENT_IGNORE_EMPTY + | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0) + | (isReqPos ? FTS3_SEGMENT_REQUIRE_POS : 0) + | (iColumnnColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0); + filter.iCol = iColumn; + filter.zTerm = pTok->z; + filter.nTerm = pTok->n; + + rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter); + while( SQLITE_OK==rc + && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr)) + ){ + rc = fts3TermSelectCb(p, (void *)&tsc, + pSegcsr->zTerm, pSegcsr->nTerm, pSegcsr->aDoclist, pSegcsr->nDoclist + ); + } + + if( rc==SQLITE_OK ){ + rc = fts3TermSelectMerge(&tsc); + } + if( rc==SQLITE_OK ){ + *ppOut = tsc.aaOutput[0]; + *pnOut = tsc.anOutput[0]; + }else{ + int i; + for(i=0; ipSegcsr = 0; + return rc; +} + +/* +** This function counts the total number of docids in the doclist stored +** in buffer aList[], size nList bytes. +** +** If the isPoslist argument is true, then it is assumed that the doclist +** contains a position-list following each docid. Otherwise, it is assumed +** that the doclist is simply a list of docids stored as delta encoded +** varints. +*/ +static int fts3DoclistCountDocids(int isPoslist, char *aList, int nList){ + int nDoc = 0; /* Return value */ + if( aList ){ + char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */ + char *p = aList; /* Cursor */ + if( !isPoslist ){ + /* The number of docids in the list is the same as the number of + ** varints. In FTS3 a varint consists of a single byte with the 0x80 + ** bit cleared and zero or more bytes with the 0x80 bit set. So to + ** count the varints in the buffer, just count the number of bytes + ** with the 0x80 bit clear. */ + while( ppLeft); + if( rc==SQLITE_OK ){ + rc = fts3DeferExpression(pCsr, pExpr->pRight); + } + if( pExpr->eType==FTSQUERY_PHRASE ){ + int iCol = pExpr->pPhrase->iColumn; + int i; + for(i=0; rc==SQLITE_OK && ipPhrase->nToken; i++){ + Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i]; + if( pToken->pDeferred==0 ){ + rc = sqlite3Fts3DeferToken(pCsr, pToken, iCol); + } + } + } + } + return rc; +} + +/* +** This function removes the position information from a doclist. When +** called, buffer aList (size *pnList bytes) contains a doclist that includes +** position information. This function removes the position information so +** that aList contains only docids, and adjusts *pnList to reflect the new +** (possibly reduced) size of the doclist. +*/ +static void fts3DoclistStripPositions( + char *aList, /* IN/OUT: Buffer containing doclist */ + int *pnList /* IN/OUT: Size of doclist in bytes */ +){ + if( aList ){ + char *aEnd = &aList[*pnList]; /* Pointer to one byte after EOF */ + char *p = aList; /* Input cursor */ + char *pOut = aList; /* Output cursor */ + + while( piColumn; + int isTermPos = (pPhrase->nToken>1 || isReqPos); + Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; + int isFirst = 1; + + int iPrevTok = 0; + int nDoc = 0; + + /* If this is an xFilter() evaluation, create a segment-reader for each + ** phrase token. Or, if this is an xNext() or snippet/offsets/matchinfo + ** evaluation, only create segment-readers if there are no Fts3DeferredToken + ** objects attached to the phrase-tokens. + */ + for(ii=0; iinToken; ii++){ + Fts3PhraseToken *pTok = &pPhrase->aToken[ii]; + if( pTok->pSegcsr==0 ){ + if( (pCsr->eEvalmode==FTS3_EVAL_FILTER) + || (pCsr->eEvalmode==FTS3_EVAL_NEXT && pCsr->pDeferred==0) + || (pCsr->eEvalmode==FTS3_EVAL_MATCHINFO && pTok->bFulltext) + ){ + rc = fts3TermSegReaderCursor( + pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr + ); + if( rc!=SQLITE_OK ) return rc; + } + } + } + + for(ii=0; iinToken; ii++){ + Fts3PhraseToken *pTok; /* Token to find doclist for */ + int iTok = 0; /* The token being queried this iteration */ + char *pList = 0; /* Pointer to token doclist */ + int nList = 0; /* Size of buffer at pList */ + + /* Select a token to process. If this is an xFilter() call, then tokens + ** are processed in order from least to most costly. Otherwise, tokens + ** are processed in the order in which they occur in the phrase. + */ + if( pCsr->eEvalmode==FTS3_EVAL_MATCHINFO ){ + assert( isReqPos ); + iTok = ii; + pTok = &pPhrase->aToken[iTok]; + if( pTok->bFulltext==0 ) continue; + }else if( pCsr->eEvalmode==FTS3_EVAL_NEXT || isReqPos ){ + iTok = ii; + pTok = &pPhrase->aToken[iTok]; + }else{ + int nMinCost = 0x7FFFFFFF; + int jj; + + /* Find the remaining token with the lowest cost. */ + for(jj=0; jjnToken; jj++){ + Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[jj].pSegcsr; + if( pSegcsr && pSegcsr->nCostnCost; + } + } + pTok = &pPhrase->aToken[iTok]; + + /* This branch is taken if it is determined that loading the doclist + ** for the next token would require more IO than loading all documents + ** currently identified by doclist pOut/nOut. No further doclists will + ** be loaded from the full-text index for this phrase. + */ + if( nMinCost>nDoc && ii>0 ){ + rc = fts3DeferExpression(pCsr, pCsr->pExpr); + break; + } + } + + if( pCsr->eEvalmode==FTS3_EVAL_NEXT && pTok->pDeferred ){ + rc = fts3DeferredTermSelect(pTok->pDeferred, isTermPos, &nList, &pList); + }else{ + if( pTok->pSegcsr ){ + rc = fts3TermSelect(p, pTok, iCol, isTermPos, &nList, &pList); + } + pTok->bFulltext = 1; + } + assert( rc!=SQLITE_OK || pCsr->eEvalmode || pTok->pSegcsr==0 ); + if( rc!=SQLITE_OK ) break; + + if( isFirst ){ + pOut = pList; + nOut = nList; + if( pCsr->eEvalmode==FTS3_EVAL_FILTER && pPhrase->nToken>1 ){ + nDoc = fts3DoclistCountDocids(1, pOut, nOut); + } + isFirst = 0; + iPrevTok = iTok; + }else{ + /* Merge the new term list and the current output. */ + char *aLeft, *aRight; + int nLeft, nRight; + int nDist; + int mt; + + /* If this is the final token of the phrase, and positions were not + ** requested by the caller, use MERGE_PHRASE instead of POS_PHRASE. + ** This drops the position information from the output list. + */ + mt = MERGE_POS_PHRASE; + if( ii==pPhrase->nToken-1 && !isReqPos ) mt = MERGE_PHRASE; + + assert( iPrevTok!=iTok ); + if( iPrevToknToken ){ + assert( pCsr->eEvalmode==FTS3_EVAL_FILTER && isReqPos==0 ); + fts3DoclistStripPositions(pOut, &nOut); + } + *paOut = pOut; + *pnOut = nOut; + }else{ + sqlite3_free(pOut); + } + return rc; +} + +/* +** This function merges two doclists according to the requirements of a +** NEAR operator. +** +** Both input doclists must include position information. The output doclist +** includes position information if the first argument to this function +** is MERGE_POS_NEAR, or does not if it is MERGE_NEAR. +*/ +static int fts3NearMerge( + int mergetype, /* MERGE_POS_NEAR or MERGE_NEAR */ + int nNear, /* Parameter to NEAR operator */ + int nTokenLeft, /* Number of tokens in LHS phrase arg */ + char *aLeft, /* Doclist for LHS (incl. positions) */ + int nLeft, /* Size of LHS doclist in bytes */ + int nTokenRight, /* As nTokenLeft */ + char *aRight, /* As aLeft */ + int nRight, /* As nRight */ + char **paOut, /* OUT: Results of merge (malloced) */ + int *pnOut /* OUT: Sized of output buffer */ +){ + char *aOut; /* Buffer to write output doclist to */ + int rc; /* Return code */ + + assert( mergetype==MERGE_POS_NEAR || MERGE_NEAR ); + + aOut = sqlite3_malloc(nLeft+nRight+1); + if( aOut==0 ){ + rc = SQLITE_NOMEM; + }else{ + rc = fts3DoclistMerge(mergetype, nNear+nTokenRight, nNear+nTokenLeft, + aOut, pnOut, aLeft, nLeft, aRight, nRight, 0 + ); + if( rc!=SQLITE_OK ){ + sqlite3_free(aOut); + aOut = 0; + } + } + + *paOut = aOut; + return rc; +} + +/* +** This function is used as part of the processing for the snippet() and +** offsets() functions. +** +** Both pLeft and pRight are expression nodes of type FTSQUERY_PHRASE. Both +** have their respective doclists (including position information) loaded +** in Fts3Expr.aDoclist/nDoclist. This function removes all entries from +** each doclist that are not within nNear tokens of a corresponding entry +** in the other doclist. +*/ +SQLITE_PRIVATE int sqlite3Fts3ExprNearTrim(Fts3Expr *pLeft, Fts3Expr *pRight, int nNear){ + int rc; /* Return code */ + + assert( pLeft->eType==FTSQUERY_PHRASE ); + assert( pRight->eType==FTSQUERY_PHRASE ); + assert( pLeft->isLoaded && pRight->isLoaded ); + + if( pLeft->aDoclist==0 || pRight->aDoclist==0 ){ + sqlite3_free(pLeft->aDoclist); + sqlite3_free(pRight->aDoclist); + pRight->aDoclist = 0; + pLeft->aDoclist = 0; + rc = SQLITE_OK; + }else{ + char *aOut; /* Buffer in which to assemble new doclist */ + int nOut; /* Size of buffer aOut in bytes */ + + rc = fts3NearMerge(MERGE_POS_NEAR, nNear, + pLeft->pPhrase->nToken, pLeft->aDoclist, pLeft->nDoclist, + pRight->pPhrase->nToken, pRight->aDoclist, pRight->nDoclist, + &aOut, &nOut + ); + if( rc!=SQLITE_OK ) return rc; + sqlite3_free(pRight->aDoclist); + pRight->aDoclist = aOut; + pRight->nDoclist = nOut; + + rc = fts3NearMerge(MERGE_POS_NEAR, nNear, + pRight->pPhrase->nToken, pRight->aDoclist, pRight->nDoclist, + pLeft->pPhrase->nToken, pLeft->aDoclist, pLeft->nDoclist, + &aOut, &nOut + ); + sqlite3_free(pLeft->aDoclist); + pLeft->aDoclist = aOut; + pLeft->nDoclist = nOut; + } + return rc; +} + + +/* +** Allocate an Fts3SegReaderArray for each token in the expression pExpr. +** The allocated objects are stored in the Fts3PhraseToken.pArray member +** variables of each token structure. +*/ +static int fts3ExprAllocateSegReaders( + Fts3Cursor *pCsr, /* FTS3 table */ + Fts3Expr *pExpr, /* Expression to create seg-readers for */ + int *pnExpr /* OUT: Number of AND'd expressions */ +){ + int rc = SQLITE_OK; /* Return code */ + + assert( pCsr->eEvalmode==FTS3_EVAL_FILTER ); + if( pnExpr && pExpr->eType!=FTSQUERY_AND ){ + (*pnExpr)++; + pnExpr = 0; + } + + if( pExpr->eType==FTSQUERY_PHRASE ){ + Fts3Phrase *pPhrase = pExpr->pPhrase; + int ii; + + for(ii=0; rc==SQLITE_OK && iinToken; ii++){ + Fts3PhraseToken *pTok = &pPhrase->aToken[ii]; + if( pTok->pSegcsr==0 ){ + rc = fts3TermSegReaderCursor( + pCsr, pTok->z, pTok->n, pTok->isPrefix, &pTok->pSegcsr + ); + } + } + }else{ + rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pLeft, pnExpr); + if( rc==SQLITE_OK ){ + rc = fts3ExprAllocateSegReaders(pCsr, pExpr->pRight, pnExpr); + } + } + return rc; +} + +/* +** Free the Fts3SegReaderArray objects associated with each token in the +** expression pExpr. In other words, this function frees the resources +** allocated by fts3ExprAllocateSegReaders(). +*/ +static void fts3ExprFreeSegReaders(Fts3Expr *pExpr){ + if( pExpr ){ + Fts3Phrase *pPhrase = pExpr->pPhrase; + if( pPhrase ){ + int kk; + for(kk=0; kknToken; kk++){ + fts3SegReaderCursorFree(pPhrase->aToken[kk].pSegcsr); + pPhrase->aToken[kk].pSegcsr = 0; + } + } + fts3ExprFreeSegReaders(pExpr->pLeft); + fts3ExprFreeSegReaders(pExpr->pRight); + } +} + +/* +** Return the sum of the costs of all tokens in the expression pExpr. This +** function must be called after Fts3SegReaderArrays have been allocated +** for all tokens using fts3ExprAllocateSegReaders(). +*/ +static int fts3ExprCost(Fts3Expr *pExpr){ + int nCost; /* Return value */ + if( pExpr->eType==FTSQUERY_PHRASE ){ + Fts3Phrase *pPhrase = pExpr->pPhrase; + int ii; + nCost = 0; + for(ii=0; iinToken; ii++){ + Fts3SegReaderCursor *pSegcsr = pPhrase->aToken[ii].pSegcsr; + if( pSegcsr ) nCost += pSegcsr->nCost; + } + }else{ + nCost = fts3ExprCost(pExpr->pLeft) + fts3ExprCost(pExpr->pRight); + } + return nCost; +} + +/* +** The following is a helper function (and type) for fts3EvalExpr(). It +** must be called after Fts3SegReaders have been allocated for every token +** in the expression. See the context it is called from in fts3EvalExpr() +** for further explanation. +*/ +typedef struct ExprAndCost ExprAndCost; +struct ExprAndCost { + Fts3Expr *pExpr; + int nCost; +}; +static void fts3ExprAssignCosts( + Fts3Expr *pExpr, /* Expression to create seg-readers for */ + ExprAndCost **ppExprCost /* OUT: Write to *ppExprCost */ +){ + if( pExpr->eType==FTSQUERY_AND ){ + fts3ExprAssignCosts(pExpr->pLeft, ppExprCost); + fts3ExprAssignCosts(pExpr->pRight, ppExprCost); + }else{ + (*ppExprCost)->pExpr = pExpr; + (*ppExprCost)->nCost = fts3ExprCost(pExpr); + (*ppExprCost)++; + } +} + +/* +** Evaluate the full-text expression pExpr against FTS3 table pTab. Store +** the resulting doclist in *paOut and *pnOut. This routine mallocs for +** the space needed to store the output. The caller is responsible for +** freeing the space when it has finished. +** +** This function is called in two distinct contexts: +** +** * From within the virtual table xFilter() method. In this case, the +** output doclist contains entries for all rows in the table, based on +** data read from the full-text index. +** +** In this case, if the query expression contains one or more tokens that +** are very common, then the returned doclist may contain a superset of +** the documents that actually match the expression. +** +** * From within the virtual table xNext() method. This call is only made +** if the call from within xFilter() found that there were very common +** tokens in the query expression and did return a superset of the +** matching documents. In this case the returned doclist contains only +** entries that correspond to the current row of the table. Instead of +** reading the data for each token from the full-text index, the data is +** already available in-memory in the Fts3PhraseToken.pDeferred structures. +** See fts3EvalDeferred() for how it gets there. +** +** In the first case above, Fts3Cursor.doDeferred==0. In the second (if it is +** required) Fts3Cursor.doDeferred==1. +** +** If the SQLite invokes the snippet(), offsets() or matchinfo() function +** as part of a SELECT on an FTS3 table, this function is called on each +** individual phrase expression in the query. If there were very common tokens +** found in the xFilter() call, then this function is called once for phrase +** for each row visited, and the returned doclist contains entries for the +** current row only. Otherwise, if there were no very common tokens, then this +** function is called once only for each phrase in the query and the returned +** doclist contains entries for all rows of the table. +** +** Fts3Cursor.doDeferred==1 when this function is called on phrases as a +** result of a snippet(), offsets() or matchinfo() invocation. +*/ +static int fts3EvalExpr( + Fts3Cursor *p, /* Virtual table cursor handle */ + Fts3Expr *pExpr, /* Parsed fts3 expression */ + char **paOut, /* OUT: Pointer to malloc'd result buffer */ + int *pnOut, /* OUT: Size of buffer at *paOut */ + int isReqPos /* Require positions in output buffer */ +){ + int rc = SQLITE_OK; /* Return code */ + + /* Zero the output parameters. */ + *paOut = 0; + *pnOut = 0; + + if( pExpr ){ + assert( pExpr->eType==FTSQUERY_NEAR || pExpr->eType==FTSQUERY_OR + || pExpr->eType==FTSQUERY_AND || pExpr->eType==FTSQUERY_NOT + || pExpr->eType==FTSQUERY_PHRASE + ); + assert( pExpr->eType==FTSQUERY_PHRASE || isReqPos==0 ); + + if( pExpr->eType==FTSQUERY_PHRASE ){ + rc = fts3PhraseSelect(p, pExpr->pPhrase, + isReqPos || (pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR), + paOut, pnOut + ); + fts3ExprFreeSegReaders(pExpr); + }else if( p->eEvalmode==FTS3_EVAL_FILTER && pExpr->eType==FTSQUERY_AND ){ + ExprAndCost *aExpr = 0; /* Array of AND'd expressions and costs */ + int nExpr = 0; /* Size of aExpr[] */ + char *aRet = 0; /* Doclist to return to caller */ + int nRet = 0; /* Length of aRet[] in bytes */ + int nDoc = 0x7FFFFFFF; + + assert( !isReqPos ); + + rc = fts3ExprAllocateSegReaders(p, pExpr, &nExpr); + if( rc==SQLITE_OK ){ + assert( nExpr>1 ); + aExpr = sqlite3_malloc(sizeof(ExprAndCost) * nExpr); + if( !aExpr ) rc = SQLITE_NOMEM; + } + if( rc==SQLITE_OK ){ + int ii; /* Used to iterate through expressions */ + + fts3ExprAssignCosts(pExpr, &aExpr); + aExpr -= nExpr; + for(ii=0; iipExpr && (pBest==0 || pCand->nCostnCost) ){ + pBest = pCand; + } + } + + if( pBest->nCost>nDoc ){ + rc = fts3DeferExpression(p, p->pExpr); + break; + }else{ + rc = fts3EvalExpr(p, pBest->pExpr, &aNew, &nNew, 0); + if( rc!=SQLITE_OK ) break; + pBest->pExpr = 0; + if( ii==0 ){ + aRet = aNew; + nRet = nNew; + nDoc = fts3DoclistCountDocids(0, aRet, nRet); + }else{ + fts3DoclistMerge( + MERGE_AND, 0, 0, aRet, &nRet, aRet, nRet, aNew, nNew, &nDoc + ); + sqlite3_free(aNew); + } + } + } + } + + if( rc==SQLITE_OK ){ + *paOut = aRet; + *pnOut = nRet; + }else{ + assert( *paOut==0 ); + sqlite3_free(aRet); + } + sqlite3_free(aExpr); + fts3ExprFreeSegReaders(pExpr); + + }else{ + char *aLeft; + char *aRight; + int nLeft; + int nRight; + + assert( pExpr->eType==FTSQUERY_NEAR + || pExpr->eType==FTSQUERY_OR + || pExpr->eType==FTSQUERY_NOT + || (pExpr->eType==FTSQUERY_AND && p->eEvalmode==FTS3_EVAL_NEXT) + ); + + if( 0==(rc = fts3EvalExpr(p, pExpr->pRight, &aRight, &nRight, isReqPos)) + && 0==(rc = fts3EvalExpr(p, pExpr->pLeft, &aLeft, &nLeft, isReqPos)) + ){ + switch( pExpr->eType ){ + case FTSQUERY_NEAR: { + Fts3Expr *pLeft; + Fts3Expr *pRight; + int mergetype = MERGE_NEAR; + if( pExpr->pParent && pExpr->pParent->eType==FTSQUERY_NEAR ){ + mergetype = MERGE_POS_NEAR; + } + pLeft = pExpr->pLeft; + while( pLeft->eType==FTSQUERY_NEAR ){ + pLeft=pLeft->pRight; + } + pRight = pExpr->pRight; + assert( pRight->eType==FTSQUERY_PHRASE ); + assert( pLeft->eType==FTSQUERY_PHRASE ); + + rc = fts3NearMerge(mergetype, pExpr->nNear, + pLeft->pPhrase->nToken, aLeft, nLeft, + pRight->pPhrase->nToken, aRight, nRight, + paOut, pnOut + ); + sqlite3_free(aLeft); + break; + } + + case FTSQUERY_OR: { + /* Allocate a buffer for the output. The maximum size is the + ** sum of the sizes of the two input buffers. The +1 term is + ** so that a buffer of zero bytes is never allocated - this can + ** cause fts3DoclistMerge() to incorrectly return SQLITE_NOMEM. + */ + char *aBuffer = sqlite3_malloc(nRight+nLeft+1); + rc = fts3DoclistMerge(MERGE_OR, 0, 0, aBuffer, pnOut, + aLeft, nLeft, aRight, nRight, 0 + ); + *paOut = aBuffer; + sqlite3_free(aLeft); + break; + } + + default: { + assert( FTSQUERY_NOT==MERGE_NOT && FTSQUERY_AND==MERGE_AND ); + fts3DoclistMerge(pExpr->eType, 0, 0, aLeft, pnOut, + aLeft, nLeft, aRight, nRight, 0 + ); + *paOut = aLeft; + break; + } + } + } + sqlite3_free(aRight); + } + } + + assert( rc==SQLITE_OK || *paOut==0 ); + return rc; +} + +/* +** This function is called from within xNext() for each row visited by +** an FTS3 query. If evaluating the FTS3 query expression within xFilter() +** was able to determine the exact set of matching rows, this function sets +** *pbRes to true and returns SQLITE_IO immediately. +** +** Otherwise, if evaluating the query expression within xFilter() returned a +** superset of the matching documents instead of an exact set (this happens +** when the query includes very common tokens and it is deemed too expensive to +** load their doclists from disk), this function tests if the current row +** really does match the FTS3 query. +** +** If an error occurs, an SQLite error code is returned. Otherwise, SQLITE_OK +** is returned and *pbRes is set to true if the current row matches the +** FTS3 query (and should be included in the results returned to SQLite), or +** false otherwise. +*/ +static int fts3EvalDeferred( + Fts3Cursor *pCsr, /* FTS3 cursor pointing at row to test */ + int *pbRes /* OUT: Set to true if row is a match */ +){ + int rc = SQLITE_OK; + if( pCsr->pDeferred==0 ){ + *pbRes = 1; + }else{ + rc = fts3CursorSeek(0, pCsr); + if( rc==SQLITE_OK ){ + sqlite3Fts3FreeDeferredDoclists(pCsr); + rc = sqlite3Fts3CacheDeferredDoclists(pCsr); + } + if( rc==SQLITE_OK ){ + char *a = 0; + int n = 0; + rc = fts3EvalExpr(pCsr, pCsr->pExpr, &a, &n, 0); + assert( n>=0 ); + *pbRes = (n>0); + sqlite3_free(a); + } + } + return rc; +} + +/* +** Advance the cursor to the next row in the %_content table that +** matches the search criteria. For a MATCH search, this will be +** the next row that matches. For a full-table scan, this will be +** simply the next row in the %_content table. For a docid lookup, +** this routine simply sets the EOF flag. +** +** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned +** even if we reach end-of-file. The fts3EofMethod() will be called +** subsequently to determine whether or not an EOF was hit. +*/ +static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){ + int res; + int rc = SQLITE_OK; /* Return code */ + Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; + + pCsr->eEvalmode = FTS3_EVAL_NEXT; + do { + if( pCsr->aDoclist==0 ){ + if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){ + pCsr->isEof = 1; + rc = sqlite3_reset(pCsr->pStmt); + break; + } + pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0); + }else{ + if( pCsr->pNextId>=&pCsr->aDoclist[pCsr->nDoclist] ){ + pCsr->isEof = 1; + break; + } + sqlite3_reset(pCsr->pStmt); + fts3GetDeltaVarint(&pCsr->pNextId, &pCsr->iPrevId); + pCsr->isRequireSeek = 1; + pCsr->isMatchinfoNeeded = 1; + } + }while( SQLITE_OK==(rc = fts3EvalDeferred(pCsr, &res)) && res==0 ); + + return rc; +} + +/* +** This is the xFilter interface for the virtual table. See +** the virtual table xFilter method documentation for additional +** information. +** +** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against +** the %_content table. +** +** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry +** in the %_content table. +** +** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The +** column on the left-hand side of the MATCH operator is column +** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand +** side of the MATCH operator. +*/ +static int fts3FilterMethod( + sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ + int idxNum, /* Strategy index */ + const char *idxStr, /* Unused */ + int nVal, /* Number of elements in apVal */ + sqlite3_value **apVal /* Arguments for the indexing scheme */ +){ + const char *azSql[] = { + "SELECT %s FROM %Q.'%q_content' AS x WHERE docid = ?", /* non-full-scan */ + "SELECT %s FROM %Q.'%q_content' AS x ", /* full-scan */ + }; + int rc; /* Return code */ + char *zSql; /* SQL statement used to access %_content */ + Fts3Table *p = (Fts3Table *)pCursor->pVtab; + Fts3Cursor *pCsr = (Fts3Cursor *)pCursor; + + UNUSED_PARAMETER(idxStr); + UNUSED_PARAMETER(nVal); + + assert( idxNum>=0 && idxNum<=(FTS3_FULLTEXT_SEARCH+p->nColumn) ); + assert( nVal==0 || nVal==1 ); + assert( (nVal==0)==(idxNum==FTS3_FULLSCAN_SEARCH) ); + assert( p->pSegments==0 ); + + /* In case the cursor has been used before, clear it now. */ + sqlite3_finalize(pCsr->pStmt); + sqlite3_free(pCsr->aDoclist); + sqlite3Fts3ExprFree(pCsr->pExpr); + memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor)); + + if( idxNum!=FTS3_DOCID_SEARCH && idxNum!=FTS3_FULLSCAN_SEARCH ){ + int iCol = idxNum-FTS3_FULLTEXT_SEARCH; + const char *zQuery = (const char *)sqlite3_value_text(apVal[0]); + + if( zQuery==0 && sqlite3_value_type(apVal[0])!=SQLITE_NULL ){ + return SQLITE_NOMEM; + } + + rc = sqlite3Fts3ExprParse(p->pTokenizer, p->azColumn, p->nColumn, + iCol, zQuery, -1, &pCsr->pExpr + ); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_ERROR ){ + p->base.zErrMsg = sqlite3_mprintf("malformed MATCH expression: [%s]", + zQuery); + } + return rc; + } + + rc = sqlite3Fts3ReadLock(p); + if( rc!=SQLITE_OK ) return rc; + + rc = fts3EvalExpr(pCsr, pCsr->pExpr, &pCsr->aDoclist, &pCsr->nDoclist, 0); + sqlite3Fts3SegmentsClose(p); + if( rc!=SQLITE_OK ) return rc; + pCsr->pNextId = pCsr->aDoclist; + pCsr->iPrevId = 0; + } + + /* Compile a SELECT statement for this cursor. For a full-table-scan, the + ** statement loops through all rows of the %_content table. For a + ** full-text query or docid lookup, the statement retrieves a single + ** row by docid. + */ + zSql = (char *)azSql[idxNum==FTS3_FULLSCAN_SEARCH]; + zSql = sqlite3_mprintf(zSql, p->zReadExprlist, p->zDb, p->zName); + if( !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0); + sqlite3_free(zSql); + } + if( rc==SQLITE_OK && idxNum==FTS3_DOCID_SEARCH ){ + rc = sqlite3_bind_value(pCsr->pStmt, 1, apVal[0]); + } + pCsr->eSearch = (i16)idxNum; + + if( rc!=SQLITE_OK ) return rc; + return fts3NextMethod(pCursor); +} + +/* +** This is the xEof method of the virtual table. SQLite calls this +** routine to find out if it has reached the end of a result set. +*/ +static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){ + return ((Fts3Cursor *)pCursor)->isEof; +} + +/* +** This is the xRowid method. The SQLite core calls this routine to +** retrieve the rowid for the current row of the result set. fts3 +** exposes %_content.docid as the rowid for the virtual table. The +** rowid should be written to *pRowid. +*/ +static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ + Fts3Cursor *pCsr = (Fts3Cursor *) pCursor; + if( pCsr->aDoclist ){ + *pRowid = pCsr->iPrevId; + }else{ + /* This branch runs if the query is implemented using a full-table scan + ** (not using the full-text index). In this case grab the rowid from the + ** SELECT statement. + */ + assert( pCsr->isRequireSeek==0 ); + *pRowid = sqlite3_column_int64(pCsr->pStmt, 0); + } + return SQLITE_OK; +} + +/* +** This is the xColumn method, called by SQLite to request a value from +** the row that the supplied cursor currently points to. +*/ +static int fts3ColumnMethod( + sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ + sqlite3_context *pContext, /* Context for sqlite3_result_xxx() calls */ + int iCol /* Index of column to read value from */ +){ + int rc; /* Return Code */ + Fts3Cursor *pCsr = (Fts3Cursor *) pCursor; + Fts3Table *p = (Fts3Table *)pCursor->pVtab; + + /* The column value supplied by SQLite must be in range. */ + assert( iCol>=0 && iCol<=p->nColumn+1 ); + + if( iCol==p->nColumn+1 ){ + /* This call is a request for the "docid" column. Since "docid" is an + ** alias for "rowid", use the xRowid() method to obtain the value. + */ + sqlite3_int64 iRowid; + rc = fts3RowidMethod(pCursor, &iRowid); + sqlite3_result_int64(pContext, iRowid); + }else if( iCol==p->nColumn ){ + /* The extra column whose name is the same as the table. + ** Return a blob which is a pointer to the cursor. + */ + sqlite3_result_blob(pContext, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT); + rc = SQLITE_OK; + }else{ + rc = fts3CursorSeek(0, pCsr); + if( rc==SQLITE_OK ){ + sqlite3_result_value(pContext, sqlite3_column_value(pCsr->pStmt, iCol+1)); + } + } + return rc; +} + +/* +** This function is the implementation of the xUpdate callback used by +** FTS3 virtual tables. It is invoked by SQLite each time a row is to be +** inserted, updated or deleted. +*/ +static int fts3UpdateMethod( + sqlite3_vtab *pVtab, /* Virtual table handle */ + int nArg, /* Size of argument array */ + sqlite3_value **apVal, /* Array of arguments */ + sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */ +){ + return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid); +} + +/* +** Implementation of xSync() method. Flush the contents of the pending-terms +** hash-table to the database. +*/ +static int fts3SyncMethod(sqlite3_vtab *pVtab){ + int rc = sqlite3Fts3PendingTermsFlush((Fts3Table *)pVtab); + sqlite3Fts3SegmentsClose((Fts3Table *)pVtab); + return rc; +} + +/* +** Implementation of xBegin() method. This is a no-op. +*/ +static int fts3BeginMethod(sqlite3_vtab *pVtab){ + UNUSED_PARAMETER(pVtab); + assert( ((Fts3Table *)pVtab)->nPendingData==0 ); + return SQLITE_OK; +} + +/* +** Implementation of xCommit() method. This is a no-op. The contents of +** the pending-terms hash-table have already been flushed into the database +** by fts3SyncMethod(). +*/ +static int fts3CommitMethod(sqlite3_vtab *pVtab){ + UNUSED_PARAMETER(pVtab); + assert( ((Fts3Table *)pVtab)->nPendingData==0 ); + return SQLITE_OK; +} + +/* +** Implementation of xRollback(). Discard the contents of the pending-terms +** hash-table. Any changes made to the database are reverted by SQLite. +*/ +static int fts3RollbackMethod(sqlite3_vtab *pVtab){ + sqlite3Fts3PendingTermsClear((Fts3Table *)pVtab); + return SQLITE_OK; +} + +/* +** Load the doclist associated with expression pExpr to pExpr->aDoclist. +** The loaded doclist contains positions as well as the document ids. +** This is used by the matchinfo(), snippet() and offsets() auxillary +** functions. +*/ +SQLITE_PRIVATE int sqlite3Fts3ExprLoadDoclist(Fts3Cursor *pCsr, Fts3Expr *pExpr){ + int rc; + assert( pExpr->eType==FTSQUERY_PHRASE && pExpr->pPhrase ); + assert( pCsr->eEvalmode==FTS3_EVAL_NEXT ); + rc = fts3EvalExpr(pCsr, pExpr, &pExpr->aDoclist, &pExpr->nDoclist, 1); + return rc; +} + +SQLITE_PRIVATE int sqlite3Fts3ExprLoadFtDoclist( + Fts3Cursor *pCsr, + Fts3Expr *pExpr, + char **paDoclist, + int *pnDoclist +){ + int rc; + assert( pCsr->eEvalmode==FTS3_EVAL_NEXT ); + assert( pExpr->eType==FTSQUERY_PHRASE && pExpr->pPhrase ); + pCsr->eEvalmode = FTS3_EVAL_MATCHINFO; + rc = fts3EvalExpr(pCsr, pExpr, paDoclist, pnDoclist, 1); + pCsr->eEvalmode = FTS3_EVAL_NEXT; + return rc; +} + +/* +** After ExprLoadDoclist() (see above) has been called, this function is +** used to iterate/search through the position lists that make up the doclist +** stored in pExpr->aDoclist. +*/ +SQLITE_PRIVATE char *sqlite3Fts3FindPositions( + Fts3Expr *pExpr, /* Access this expressions doclist */ + sqlite3_int64 iDocid, /* Docid associated with requested pos-list */ + int iCol /* Column of requested pos-list */ +){ + assert( pExpr->isLoaded ); + if( pExpr->aDoclist ){ + char *pEnd = &pExpr->aDoclist[pExpr->nDoclist]; + char *pCsr; + + if( pExpr->pCurrent==0 ){ + pExpr->pCurrent = pExpr->aDoclist; + pExpr->iCurrent = 0; + pExpr->pCurrent += sqlite3Fts3GetVarint(pExpr->pCurrent,&pExpr->iCurrent); + } + pCsr = pExpr->pCurrent; + assert( pCsr ); + + while( pCsriCurrentiCurrent); + } + pExpr->pCurrent = pCsr; + }else{ + if( pExpr->iCurrent==iDocid ){ + int iThis = 0; + if( iCol<0 ){ + /* If iCol is negative, return a pointer to the start of the + ** position-list (instead of a pointer to the start of a list + ** of offsets associated with a specific column). + */ + return pCsr; + } + while( iThis=1 ); + + if( nVal>6 ){ + sqlite3_result_error(pContext, + "wrong number of arguments to function snippet()", -1); + return; + } + if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return; + + switch( nVal ){ + case 6: nToken = sqlite3_value_int(apVal[5]); + case 5: iCol = sqlite3_value_int(apVal[4]); + case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]); + case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]); + case 2: zStart = (const char*)sqlite3_value_text(apVal[1]); + } + if( !zEllipsis || !zEnd || !zStart ){ + sqlite3_result_error_nomem(pContext); + }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){ + sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken); + } +} + +/* +** Implementation of the offsets() function for FTS3 +*/ +static void fts3OffsetsFunc( + sqlite3_context *pContext, /* SQLite function call context */ + int nVal, /* Size of argument array */ + sqlite3_value **apVal /* Array of arguments */ +){ + Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */ + + UNUSED_PARAMETER(nVal); + + assert( nVal==1 ); + if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return; + assert( pCsr ); + if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){ + sqlite3Fts3Offsets(pContext, pCsr); + } +} + +/* +** Implementation of the special optimize() function for FTS3. This +** function merges all segments in the database to a single segment. +** Example usage is: +** +** SELECT optimize(t) FROM t LIMIT 1; +** +** where 't' is the name of an FTS3 table. +*/ +static void fts3OptimizeFunc( + sqlite3_context *pContext, /* SQLite function call context */ + int nVal, /* Size of argument array */ + sqlite3_value **apVal /* Array of arguments */ +){ + int rc; /* Return code */ + Fts3Table *p; /* Virtual table handle */ + Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */ + + UNUSED_PARAMETER(nVal); + + assert( nVal==1 ); + if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return; + p = (Fts3Table *)pCursor->base.pVtab; + assert( p ); + + rc = sqlite3Fts3Optimize(p); + + switch( rc ){ + case SQLITE_OK: + sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC); + break; + case SQLITE_DONE: + sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC); + break; + default: + sqlite3_result_error_code(pContext, rc); + break; + } +} + +/* +** Implementation of the matchinfo() function for FTS3 +*/ +static void fts3MatchinfoFunc( + sqlite3_context *pContext, /* SQLite function call context */ + int nVal, /* Size of argument array */ + sqlite3_value **apVal /* Array of arguments */ +){ + Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */ + assert( nVal==1 || nVal==2 ); + if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){ + const char *zArg = 0; + if( nVal>1 ){ + zArg = (const char *)sqlite3_value_text(apVal[1]); + } + sqlite3Fts3Matchinfo(pContext, pCsr, zArg); + } +} + +/* +** This routine implements the xFindFunction method for the FTS3 +** virtual table. +*/ +static int fts3FindFunctionMethod( + sqlite3_vtab *pVtab, /* Virtual table handle */ + int nArg, /* Number of SQL function arguments */ + const char *zName, /* Name of SQL function */ + void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */ + void **ppArg /* Unused */ +){ + struct Overloaded { + const char *zName; + void (*xFunc)(sqlite3_context*,int,sqlite3_value**); + } aOverload[] = { + { "snippet", fts3SnippetFunc }, + { "offsets", fts3OffsetsFunc }, + { "optimize", fts3OptimizeFunc }, + { "matchinfo", fts3MatchinfoFunc }, + }; + int i; /* Iterator variable */ + + UNUSED_PARAMETER(pVtab); + UNUSED_PARAMETER(nArg); + UNUSED_PARAMETER(ppArg); + + for(i=0; idb; /* Database connection */ + int rc; /* Return Code */ + + rc = sqlite3Fts3PendingTermsFlush(p); + if( rc!=SQLITE_OK ){ + return rc; + } + + fts3DbExec(&rc, db, + "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';", + p->zDb, p->zName, zName + ); + if( p->bHasDocsize ){ + fts3DbExec(&rc, db, + "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';", + p->zDb, p->zName, zName + ); + } + if( p->bHasStat ){ + fts3DbExec(&rc, db, + "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';", + p->zDb, p->zName, zName + ); + } + fts3DbExec(&rc, db, + "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';", + p->zDb, p->zName, zName + ); + fts3DbExec(&rc, db, + "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';", + p->zDb, p->zName, zName + ); + return rc; +} + +static const sqlite3_module fts3Module = { + /* iVersion */ 0, + /* xCreate */ fts3CreateMethod, + /* xConnect */ fts3ConnectMethod, + /* xBestIndex */ fts3BestIndexMethod, + /* xDisconnect */ fts3DisconnectMethod, + /* xDestroy */ fts3DestroyMethod, + /* xOpen */ fts3OpenMethod, + /* xClose */ fts3CloseMethod, + /* xFilter */ fts3FilterMethod, + /* xNext */ fts3NextMethod, + /* xEof */ fts3EofMethod, + /* xColumn */ fts3ColumnMethod, + /* xRowid */ fts3RowidMethod, + /* xUpdate */ fts3UpdateMethod, + /* xBegin */ fts3BeginMethod, + /* xSync */ fts3SyncMethod, + /* xCommit */ fts3CommitMethod, + /* xRollback */ fts3RollbackMethod, + /* xFindFunction */ fts3FindFunctionMethod, + /* xRename */ fts3RenameMethod, +}; + +/* +** This function is registered as the module destructor (called when an +** FTS3 enabled database connection is closed). It frees the memory +** allocated for the tokenizer hash table. +*/ +static void hashDestroy(void *p){ + Fts3Hash *pHash = (Fts3Hash *)p; + sqlite3Fts3HashClear(pHash); + sqlite3_free(pHash); +} + +/* +** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are +** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c +** respectively. The following three forward declarations are for functions +** declared in these files used to retrieve the respective implementations. +** +** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed +** to by the argument to point to the "simple" tokenizer implementation. +** And so on. +*/ +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); +SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule); +#ifdef SQLITE_ENABLE_ICU +SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule); +#endif + +/* +** Initialise the fts3 extension. If this extension is built as part +** of the sqlite library, then this function is called directly by +** SQLite. If fts3 is built as a dynamically loadable extension, this +** function is called by the sqlite3_extension_init() entry point. +*/ +SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){ + int rc = SQLITE_OK; + Fts3Hash *pHash = 0; + const sqlite3_tokenizer_module *pSimple = 0; + const sqlite3_tokenizer_module *pPorter = 0; + +#ifdef SQLITE_ENABLE_ICU + const sqlite3_tokenizer_module *pIcu = 0; + sqlite3Fts3IcuTokenizerModule(&pIcu); +#endif + + rc = sqlite3Fts3InitAux(db); + if( rc!=SQLITE_OK ) return rc; + + sqlite3Fts3SimpleTokenizerModule(&pSimple); + sqlite3Fts3PorterTokenizerModule(&pPorter); + + /* Allocate and initialise the hash-table used to store tokenizers. */ + pHash = sqlite3_malloc(sizeof(Fts3Hash)); + if( !pHash ){ + rc = SQLITE_NOMEM; + }else{ + sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); + } + + /* Load the built-in tokenizers into the hash table */ + if( rc==SQLITE_OK ){ + if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple) + || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter) +#ifdef SQLITE_ENABLE_ICU + || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu)) +#endif + ){ + rc = SQLITE_NOMEM; + } + } + +#ifdef SQLITE_TEST + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3ExprInitTestInterface(db); + } +#endif + + /* Create the virtual table wrapper around the hash-table and overload + ** the two scalar functions. If this is successful, register the + ** module with sqlite. + */ + if( SQLITE_OK==rc + && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer")) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1)) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1)) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1)) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2)) + && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1)) + ){ + rc = sqlite3_create_module_v2( + db, "fts3", &fts3Module, (void *)pHash, hashDestroy + ); + if( rc==SQLITE_OK ){ + rc = sqlite3_create_module_v2( + db, "fts4", &fts3Module, (void *)pHash, 0 + ); + } + return rc; + } + + /* An error has occurred. Delete the hash table and return the error code. */ + assert( rc!=SQLITE_OK ); + if( pHash ){ + sqlite3Fts3HashClear(pHash); + sqlite3_free(pHash); + } + return rc; +} + +#if !SQLITE_CORE +SQLITE_API int sqlite3_extension_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + SQLITE_EXTENSION_INIT2(pApi) + return sqlite3Fts3Init(db); +} +#endif + +#endif + +/************** End of fts3.c ************************************************/ +/************** Begin file fts3_aux.c ****************************************/ +/* +** 2011 Jan 27 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +*/ + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + + +typedef struct Fts3auxTable Fts3auxTable; +typedef struct Fts3auxCursor Fts3auxCursor; + +struct Fts3auxTable { + sqlite3_vtab base; /* Base class used by SQLite core */ + Fts3Table *pFts3Tab; +}; + +struct Fts3auxCursor { + sqlite3_vtab_cursor base; /* Base class used by SQLite core */ + Fts3SegReaderCursor csr; /* Must be right after "base" */ + Fts3SegFilter filter; + char *zStop; + int nStop; /* Byte-length of string zStop */ + int isEof; /* True if cursor is at EOF */ + sqlite3_int64 iRowid; /* Current rowid */ + + int iCol; /* Current value of 'col' column */ + int nStat; /* Size of aStat[] array */ + struct Fts3auxColstats { + sqlite3_int64 nDoc; /* 'documents' values for current csr row */ + sqlite3_int64 nOcc; /* 'occurrences' values for current csr row */ + } *aStat; +}; + +/* +** Schema of the terms table. +*/ +#define FTS3_TERMS_SCHEMA "CREATE TABLE x(term, col, documents, occurrences)" + +/* +** This function does all the work for both the xConnect and xCreate methods. +** These tables have no persistent representation of their own, so xConnect +** and xCreate are identical operations. +*/ +static int fts3auxConnectMethod( + sqlite3 *db, /* Database connection */ + void *pUnused, /* Unused */ + int argc, /* Number of elements in argv array */ + const char * const *argv, /* xCreate/xConnect argument array */ + sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */ + char **pzErr /* OUT: sqlite3_malloc'd error message */ +){ + char const *zDb; /* Name of database (e.g. "main") */ + char const *zFts3; /* Name of fts3 table */ + int nDb; /* Result of strlen(zDb) */ + int nFts3; /* Result of strlen(zFts3) */ + int nByte; /* Bytes of space to allocate here */ + int rc; /* value returned by declare_vtab() */ + Fts3auxTable *p; /* Virtual table object to return */ + + /* The user should specify a single argument - the name of an fts3 table. */ + if( argc!=4 ){ + *pzErr = sqlite3_mprintf( + "wrong number of arguments to fts4aux constructor" + ); + return SQLITE_ERROR; + } + + zDb = argv[1]; + nDb = strlen(zDb); + zFts3 = argv[3]; + nFts3 = strlen(zFts3); + + rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA); + if( rc!=SQLITE_OK ) return rc; + + nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2; + p = (Fts3auxTable *)sqlite3_malloc(nByte); + if( !p ) return SQLITE_NOMEM; + memset(p, 0, nByte); + + p->pFts3Tab = (Fts3Table *)&p[1]; + p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1]; + p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1]; + p->pFts3Tab->db = db; + + memcpy((char *)p->pFts3Tab->zDb, zDb, nDb); + memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3); + sqlite3Fts3Dequote((char *)p->pFts3Tab->zName); + + *ppVtab = (sqlite3_vtab *)p; + return SQLITE_OK; +} + +/* +** This function does the work for both the xDisconnect and xDestroy methods. +** These tables have no persistent representation of their own, so xDisconnect +** and xDestroy are identical operations. +*/ +static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){ + Fts3auxTable *p = (Fts3auxTable *)pVtab; + Fts3Table *pFts3 = p->pFts3Tab; + int i; + + /* Free any prepared statements held */ + for(i=0; iaStmt); i++){ + sqlite3_finalize(pFts3->aStmt[i]); + } + sqlite3_free(pFts3->zSegmentsTbl); + sqlite3_free(p); + return SQLITE_OK; +} + +#define FTS4AUX_EQ_CONSTRAINT 1 +#define FTS4AUX_GE_CONSTRAINT 2 +#define FTS4AUX_LE_CONSTRAINT 4 + +/* +** xBestIndex - Analyze a WHERE and ORDER BY clause. +*/ +static int fts3auxBestIndexMethod( + sqlite3_vtab *pVTab, + sqlite3_index_info *pInfo +){ + int i; + int iEq = -1; + int iGe = -1; + int iLe = -1; + + /* This vtab delivers always results in "ORDER BY term ASC" order. */ + if( pInfo->nOrderBy==1 + && pInfo->aOrderBy[0].iColumn==0 + && pInfo->aOrderBy[0].desc==0 + ){ + pInfo->orderByConsumed = 1; + } + + /* Search for equality and range constraints on the "term" column. */ + for(i=0; inConstraint; i++){ + if( pInfo->aConstraint[i].usable && pInfo->aConstraint[i].iColumn==0 ){ + int op = pInfo->aConstraint[i].op; + if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i; + if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i; + if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i; + if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i; + if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i; + } + } + + if( iEq>=0 ){ + pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT; + pInfo->aConstraintUsage[iEq].argvIndex = 1; + pInfo->estimatedCost = 5; + }else{ + pInfo->idxNum = 0; + pInfo->estimatedCost = 20000; + if( iGe>=0 ){ + pInfo->idxNum += FTS4AUX_GE_CONSTRAINT; + pInfo->aConstraintUsage[iGe].argvIndex = 1; + pInfo->estimatedCost /= 2; + } + if( iLe>=0 ){ + pInfo->idxNum += FTS4AUX_LE_CONSTRAINT; + pInfo->aConstraintUsage[iLe].argvIndex = 1 + (iGe>=0); + pInfo->estimatedCost /= 2; + } + } + + return SQLITE_OK; +} + +/* +** xOpen - Open a cursor. +*/ +static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){ + Fts3auxCursor *pCsr; /* Pointer to cursor object to return */ + + pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor)); + if( !pCsr ) return SQLITE_NOMEM; + memset(pCsr, 0, sizeof(Fts3auxCursor)); + + *ppCsr = (sqlite3_vtab_cursor *)pCsr; + return SQLITE_OK; +} + +/* +** xClose - Close a cursor. +*/ +static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){ + Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab; + Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; + + sqlite3Fts3SegmentsClose(pFts3); + sqlite3Fts3SegReaderFinish(&pCsr->csr); + sqlite3_free((void *)pCsr->filter.zTerm); + sqlite3_free(pCsr->zStop); + sqlite3_free(pCsr->aStat); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){ + if( nSize>pCsr->nStat ){ + struct Fts3auxColstats *aNew; + aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat, + sizeof(struct Fts3auxColstats) * nSize + ); + if( aNew==0 ) return SQLITE_NOMEM; + memset(&aNew[pCsr->nStat], 0, + sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat) + ); + pCsr->aStat = aNew; + pCsr->nStat = nSize; + } + return SQLITE_OK; +} + +/* +** xNext - Advance the cursor to the next row, if any. +*/ +static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){ + Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; + Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab; + int rc; + + /* Increment our pretend rowid value. */ + pCsr->iRowid++; + + for(pCsr->iCol++; pCsr->iColnStat; pCsr->iCol++){ + if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK; + } + + rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr); + if( rc==SQLITE_ROW ){ + int i = 0; + int nDoclist = pCsr->csr.nDoclist; + char *aDoclist = pCsr->csr.aDoclist; + int iCol; + + int eState = 0; + + if( pCsr->zStop ){ + int n = (pCsr->nStopcsr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm; + int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n); + if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){ + pCsr->isEof = 1; + return SQLITE_OK; + } + } + + if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM; + memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat); + iCol = 0; + + while( iaStat[0].nDoc++; + eState = 1; + iCol = 0; + break; + + /* State 1. In this state we are expecting either a 1, indicating + ** that the following integer will be a column number, or the + ** start of a position list for column 0. + ** + ** The only difference between state 1 and state 2 is that if the + ** integer encountered in state 1 is not 0 or 1, then we need to + ** increment the column 0 "nDoc" count for this term. + */ + case 1: + assert( iCol==0 ); + if( v>1 ){ + pCsr->aStat[1].nDoc++; + } + eState = 2; + /* fall through */ + + case 2: + if( v==0 ){ /* 0x00. Next integer will be a docid. */ + eState = 0; + }else if( v==1 ){ /* 0x01. Next integer will be a column number. */ + eState = 3; + }else{ /* 2 or greater. A position. */ + pCsr->aStat[iCol+1].nOcc++; + pCsr->aStat[0].nOcc++; + } + break; + + /* State 3. The integer just read is a column number. */ + default: assert( eState==3 ); + iCol = (int)v; + if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM; + pCsr->aStat[iCol+1].nDoc++; + eState = 2; + break; + } + } + + pCsr->iCol = 0; + rc = SQLITE_OK; + }else{ + pCsr->isEof = 1; + } + return rc; +} + +/* +** xFilter - Initialize a cursor to point at the start of its data. +*/ +static int fts3auxFilterMethod( + sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */ + int idxNum, /* Strategy index */ + const char *idxStr, /* Unused */ + int nVal, /* Number of elements in apVal */ + sqlite3_value **apVal /* Arguments for the indexing scheme */ +){ + Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; + Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab; + int rc; + int isScan; + + assert( idxStr==0 ); + assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0 + || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT + || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) + ); + isScan = (idxNum!=FTS4AUX_EQ_CONSTRAINT); + + /* In case this cursor is being reused, close and zero it. */ + testcase(pCsr->filter.zTerm); + sqlite3Fts3SegReaderFinish(&pCsr->csr); + sqlite3_free((void *)pCsr->filter.zTerm); + sqlite3_free(pCsr->aStat); + memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr); + + pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY; + if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN; + + if( idxNum&(FTS4AUX_EQ_CONSTRAINT|FTS4AUX_GE_CONSTRAINT) ){ + const unsigned char *zStr = sqlite3_value_text(apVal[0]); + if( zStr ){ + pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr); + pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]); + if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM; + } + } + if( idxNum&FTS4AUX_LE_CONSTRAINT ){ + int iIdx = (idxNum&FTS4AUX_GE_CONSTRAINT) ? 1 : 0; + pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iIdx])); + pCsr->nStop = sqlite3_value_bytes(apVal[iIdx]); + if( pCsr->zStop==0 ) return SQLITE_NOMEM; + } + + rc = sqlite3Fts3SegReaderCursor(pFts3, FTS3_SEGCURSOR_ALL, + pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr + ); + if( rc==SQLITE_OK ){ + rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter); + } + + if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor); + return rc; +} + +/* +** xEof - Return true if the cursor is at EOF, or false otherwise. +*/ +static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){ + Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; + return pCsr->isEof; +} + +/* +** xColumn - Return a column value. +*/ +static int fts3auxColumnMethod( + sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ + sqlite3_context *pContext, /* Context for sqlite3_result_xxx() calls */ + int iCol /* Index of column to read value from */ +){ + Fts3auxCursor *p = (Fts3auxCursor *)pCursor; + + assert( p->isEof==0 ); + if( iCol==0 ){ /* Column "term" */ + sqlite3_result_text(pContext, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT); + }else if( iCol==1 ){ /* Column "col" */ + if( p->iCol ){ + sqlite3_result_int(pContext, p->iCol-1); + }else{ + sqlite3_result_text(pContext, "*", -1, SQLITE_STATIC); + } + }else if( iCol==2 ){ /* Column "documents" */ + sqlite3_result_int64(pContext, p->aStat[p->iCol].nDoc); + }else{ /* Column "occurrences" */ + sqlite3_result_int64(pContext, p->aStat[p->iCol].nOcc); + } + + return SQLITE_OK; +} + +/* +** xRowid - Return the current rowid for the cursor. +*/ +static int fts3auxRowidMethod( + sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */ + sqlite_int64 *pRowid /* OUT: Rowid value */ +){ + Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor; + *pRowid = pCsr->iRowid; + return SQLITE_OK; +} + +/* +** Register the fts3aux module with database connection db. Return SQLITE_OK +** if successful or an error code if sqlite3_create_module() fails. +*/ +SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){ + static const sqlite3_module fts3aux_module = { + 0, /* iVersion */ + fts3auxConnectMethod, /* xCreate */ + fts3auxConnectMethod, /* xConnect */ + fts3auxBestIndexMethod, /* xBestIndex */ + fts3auxDisconnectMethod, /* xDisconnect */ + fts3auxDisconnectMethod, /* xDestroy */ + fts3auxOpenMethod, /* xOpen */ + fts3auxCloseMethod, /* xClose */ + fts3auxFilterMethod, /* xFilter */ + fts3auxNextMethod, /* xNext */ + fts3auxEofMethod, /* xEof */ + fts3auxColumnMethod, /* xColumn */ + fts3auxRowidMethod, /* xRowid */ + 0, /* xUpdate */ + 0, /* xBegin */ + 0, /* xSync */ + 0, /* xCommit */ + 0, /* xRollback */ + 0, /* xFindFunction */ + 0 /* xRename */ + }; + int rc; /* Return code */ + + rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0); + return rc; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_aux.c ********************************************/ +/************** Begin file fts3_expr.c ***************************************/ +/* +** 2008 Nov 28 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This module contains code that implements a parser for fts3 query strings +** (the right-hand argument to the MATCH operator). Because the supported +** syntax is relatively simple, the whole tokenizer/parser system is +** hand-coded. +*/ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +/* +** By default, this module parses the legacy syntax that has been +** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS +** is defined, then it uses the new syntax. The differences between +** the new and the old syntaxes are: +** +** a) The new syntax supports parenthesis. The old does not. +** +** b) The new syntax supports the AND and NOT operators. The old does not. +** +** c) The old syntax supports the "-" token qualifier. This is not +** supported by the new syntax (it is replaced by the NOT operator). +** +** d) When using the old syntax, the OR operator has a greater precedence +** than an implicit AND. When using the new, both implicity and explicit +** AND operators have a higher precedence than OR. +** +** If compiled with SQLITE_TEST defined, then this module exports the +** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable +** to zero causes the module to use the old syntax. If it is set to +** non-zero the new syntax is activated. This is so both syntaxes can +** be tested using a single build of testfixture. +** +** The following describes the syntax supported by the fts3 MATCH +** operator in a similar format to that used by the lemon parser +** generator. This module does not use actually lemon, it uses a +** custom parser. +** +** query ::= andexpr (OR andexpr)*. +** +** andexpr ::= notexpr (AND? notexpr)*. +** +** notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*. +** notexpr ::= LP query RP. +** +** nearexpr ::= phrase (NEAR distance_opt nearexpr)*. +** +** distance_opt ::= . +** distance_opt ::= / INTEGER. +** +** phrase ::= TOKEN. +** phrase ::= COLUMN:TOKEN. +** phrase ::= "TOKEN TOKEN TOKEN...". +*/ + +#ifdef SQLITE_TEST +SQLITE_API int sqlite3_fts3_enable_parentheses = 0; +#else +# ifdef SQLITE_ENABLE_FTS3_PARENTHESIS +# define sqlite3_fts3_enable_parentheses 1 +# else +# define sqlite3_fts3_enable_parentheses 0 +# endif +#endif + +/* +** Default span for NEAR operators. +*/ +#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10 + + +typedef struct ParseContext ParseContext; +struct ParseContext { + sqlite3_tokenizer *pTokenizer; /* Tokenizer module */ + const char **azCol; /* Array of column names for fts3 table */ + int nCol; /* Number of entries in azCol[] */ + int iDefaultCol; /* Default column to query */ + sqlite3_context *pCtx; /* Write error message here */ + int nNest; /* Number of nested brackets */ +}; + +/* +** This function is equivalent to the standard isspace() function. +** +** The standard isspace() can be awkward to use safely, because although it +** is defined to accept an argument of type int, its behaviour when passed +** an integer that falls outside of the range of the unsigned char type +** is undefined (and sometimes, "undefined" means segfault). This wrapper +** is defined to accept an argument of type char, and always returns 0 for +** any values that fall outside of the range of the unsigned char type (i.e. +** negative values). +*/ +static int fts3isspace(char c){ + return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f'; +} + +/* +** Allocate nByte bytes of memory using sqlite3_malloc(). If successful, +** zero the memory before returning a pointer to it. If unsuccessful, +** return NULL. +*/ +static void *fts3MallocZero(int nByte){ + void *pRet = sqlite3_malloc(nByte); + if( pRet ) memset(pRet, 0, nByte); + return pRet; +} + + +/* +** Extract the next token from buffer z (length n) using the tokenizer +** and other information (column names etc.) in pParse. Create an Fts3Expr +** structure of type FTSQUERY_PHRASE containing a phrase consisting of this +** single token and set *ppExpr to point to it. If the end of the buffer is +** reached before a token is found, set *ppExpr to zero. It is the +** responsibility of the caller to eventually deallocate the allocated +** Fts3Expr structure (if any) by passing it to sqlite3_free(). +** +** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation +** fails. +*/ +static int getNextToken( + ParseContext *pParse, /* fts3 query parse context */ + int iCol, /* Value for Fts3Phrase.iColumn */ + const char *z, int n, /* Input string */ + Fts3Expr **ppExpr, /* OUT: expression */ + int *pnConsumed /* OUT: Number of bytes consumed */ +){ + sqlite3_tokenizer *pTokenizer = pParse->pTokenizer; + sqlite3_tokenizer_module const *pModule = pTokenizer->pModule; + int rc; + sqlite3_tokenizer_cursor *pCursor; + Fts3Expr *pRet = 0; + int nConsumed = 0; + + rc = pModule->xOpen(pTokenizer, z, n, &pCursor); + if( rc==SQLITE_OK ){ + const char *zToken; + int nToken, iStart, iEnd, iPosition; + int nByte; /* total space to allocate */ + + pCursor->pTokenizer = pTokenizer; + rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition); + + if( rc==SQLITE_OK ){ + nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken; + pRet = (Fts3Expr *)fts3MallocZero(nByte); + if( !pRet ){ + rc = SQLITE_NOMEM; + }else{ + pRet->eType = FTSQUERY_PHRASE; + pRet->pPhrase = (Fts3Phrase *)&pRet[1]; + pRet->pPhrase->nToken = 1; + pRet->pPhrase->iColumn = iCol; + pRet->pPhrase->aToken[0].n = nToken; + pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1]; + memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken); + + if( iEndpPhrase->aToken[0].isPrefix = 1; + iEnd++; + } + if( !sqlite3_fts3_enable_parentheses && iStart>0 && z[iStart-1]=='-' ){ + pRet->pPhrase->isNot = 1; + } + } + nConsumed = iEnd; + } + + pModule->xClose(pCursor); + } + + *pnConsumed = nConsumed; + *ppExpr = pRet; + return rc; +} + + +/* +** Enlarge a memory allocation. If an out-of-memory allocation occurs, +** then free the old allocation. +*/ +static void *fts3ReallocOrFree(void *pOrig, int nNew){ + void *pRet = sqlite3_realloc(pOrig, nNew); + if( !pRet ){ + sqlite3_free(pOrig); + } + return pRet; +} + +/* +** Buffer zInput, length nInput, contains the contents of a quoted string +** that appeared as part of an fts3 query expression. Neither quote character +** is included in the buffer. This function attempts to tokenize the entire +** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE +** containing the results. +** +** If successful, SQLITE_OK is returned and *ppExpr set to point at the +** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory +** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set +** to 0. +*/ +static int getNextString( + ParseContext *pParse, /* fts3 query parse context */ + const char *zInput, int nInput, /* Input string */ + Fts3Expr **ppExpr /* OUT: expression */ +){ + sqlite3_tokenizer *pTokenizer = pParse->pTokenizer; + sqlite3_tokenizer_module const *pModule = pTokenizer->pModule; + int rc; + Fts3Expr *p = 0; + sqlite3_tokenizer_cursor *pCursor = 0; + char *zTemp = 0; + int nTemp = 0; + + rc = pModule->xOpen(pTokenizer, zInput, nInput, &pCursor); + if( rc==SQLITE_OK ){ + int ii; + pCursor->pTokenizer = pTokenizer; + for(ii=0; rc==SQLITE_OK; ii++){ + const char *zToken; + int nToken, iBegin, iEnd, iPos; + rc = pModule->xNext(pCursor, &zToken, &nToken, &iBegin, &iEnd, &iPos); + if( rc==SQLITE_OK ){ + int nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase); + p = fts3ReallocOrFree(p, nByte+ii*sizeof(Fts3PhraseToken)); + zTemp = fts3ReallocOrFree(zTemp, nTemp + nToken); + if( !p || !zTemp ){ + goto no_mem; + } + if( ii==0 ){ + memset(p, 0, nByte); + p->pPhrase = (Fts3Phrase *)&p[1]; + } + p->pPhrase = (Fts3Phrase *)&p[1]; + memset(&p->pPhrase->aToken[ii], 0, sizeof(Fts3PhraseToken)); + p->pPhrase->nToken = ii+1; + p->pPhrase->aToken[ii].n = nToken; + memcpy(&zTemp[nTemp], zToken, nToken); + nTemp += nToken; + if( iEndpPhrase->aToken[ii].isPrefix = 1; + }else{ + p->pPhrase->aToken[ii].isPrefix = 0; + } + } + } + + pModule->xClose(pCursor); + pCursor = 0; + } + + if( rc==SQLITE_DONE ){ + int jj; + char *zNew = NULL; + int nNew = 0; + int nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase); + nByte += (p?(p->pPhrase->nToken-1):0) * sizeof(Fts3PhraseToken); + p = fts3ReallocOrFree(p, nByte + nTemp); + if( !p ){ + goto no_mem; + } + if( zTemp ){ + zNew = &(((char *)p)[nByte]); + memcpy(zNew, zTemp, nTemp); + }else{ + memset(p, 0, nByte+nTemp); + } + p->pPhrase = (Fts3Phrase *)&p[1]; + for(jj=0; jjpPhrase->nToken; jj++){ + p->pPhrase->aToken[jj].z = &zNew[nNew]; + nNew += p->pPhrase->aToken[jj].n; + } + sqlite3_free(zTemp); + p->eType = FTSQUERY_PHRASE; + p->pPhrase->iColumn = pParse->iDefaultCol; + rc = SQLITE_OK; + } + + *ppExpr = p; + return rc; +no_mem: + + if( pCursor ){ + pModule->xClose(pCursor); + } + sqlite3_free(zTemp); + sqlite3_free(p); + *ppExpr = 0; + return SQLITE_NOMEM; +} + +/* +** Function getNextNode(), which is called by fts3ExprParse(), may itself +** call fts3ExprParse(). So this forward declaration is required. +*/ +static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *); + +/* +** The output variable *ppExpr is populated with an allocated Fts3Expr +** structure, or set to 0 if the end of the input buffer is reached. +** +** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM +** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered. +** If SQLITE_ERROR is returned, pContext is populated with an error message. +*/ +static int getNextNode( + ParseContext *pParse, /* fts3 query parse context */ + const char *z, int n, /* Input string */ + Fts3Expr **ppExpr, /* OUT: expression */ + int *pnConsumed /* OUT: Number of bytes consumed */ +){ + static const struct Fts3Keyword { + char *z; /* Keyword text */ + unsigned char n; /* Length of the keyword */ + unsigned char parenOnly; /* Only valid in paren mode */ + unsigned char eType; /* Keyword code */ + } aKeyword[] = { + { "OR" , 2, 0, FTSQUERY_OR }, + { "AND", 3, 1, FTSQUERY_AND }, + { "NOT", 3, 1, FTSQUERY_NOT }, + { "NEAR", 4, 0, FTSQUERY_NEAR } + }; + int ii; + int iCol; + int iColLen; + int rc; + Fts3Expr *pRet = 0; + + const char *zInput = z; + int nInput = n; + + /* Skip over any whitespace before checking for a keyword, an open or + ** close bracket, or a quoted string. + */ + while( nInput>0 && fts3isspace(*zInput) ){ + nInput--; + zInput++; + } + if( nInput==0 ){ + return SQLITE_DONE; + } + + /* See if we are dealing with a keyword. */ + for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){ + const struct Fts3Keyword *pKey = &aKeyword[ii]; + + if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){ + continue; + } + + if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){ + int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM; + int nKey = pKey->n; + char cNext; + + /* If this is a "NEAR" keyword, check for an explicit nearness. */ + if( pKey->eType==FTSQUERY_NEAR ){ + assert( nKey==4 ); + if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){ + nNear = 0; + for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){ + nNear = nNear * 10 + (zInput[nKey] - '0'); + } + } + } + + /* At this point this is probably a keyword. But for that to be true, + ** the next byte must contain either whitespace, an open or close + ** parenthesis, a quote character, or EOF. + */ + cNext = zInput[nKey]; + if( fts3isspace(cNext) + || cNext=='"' || cNext=='(' || cNext==')' || cNext==0 + ){ + pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr)); + if( !pRet ){ + return SQLITE_NOMEM; + } + pRet->eType = pKey->eType; + pRet->nNear = nNear; + *ppExpr = pRet; + *pnConsumed = (int)((zInput - z) + nKey); + return SQLITE_OK; + } + + /* Turns out that wasn't a keyword after all. This happens if the + ** user has supplied a token such as "ORacle". Continue. + */ + } + } + + /* Check for an open bracket. */ + if( sqlite3_fts3_enable_parentheses ){ + if( *zInput=='(' ){ + int nConsumed; + pParse->nNest++; + rc = fts3ExprParse(pParse, &zInput[1], nInput-1, ppExpr, &nConsumed); + if( rc==SQLITE_OK && !*ppExpr ){ + rc = SQLITE_DONE; + } + *pnConsumed = (int)((zInput - z) + 1 + nConsumed); + return rc; + } + + /* Check for a close bracket. */ + if( *zInput==')' ){ + pParse->nNest--; + *pnConsumed = (int)((zInput - z) + 1); + return SQLITE_DONE; + } + } + + /* See if we are dealing with a quoted phrase. If this is the case, then + ** search for the closing quote and pass the whole string to getNextString() + ** for processing. This is easy to do, as fts3 has no syntax for escaping + ** a quote character embedded in a string. + */ + if( *zInput=='"' ){ + for(ii=1; iiiDefaultCol; + iColLen = 0; + for(ii=0; iinCol; ii++){ + const char *zStr = pParse->azCol[ii]; + int nStr = (int)strlen(zStr); + if( nInput>nStr && zInput[nStr]==':' + && sqlite3_strnicmp(zStr, zInput, nStr)==0 + ){ + iCol = ii; + iColLen = (int)((zInput - z) + nStr + 1); + break; + } + } + rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed); + *pnConsumed += iColLen; + return rc; +} + +/* +** The argument is an Fts3Expr structure for a binary operator (any type +** except an FTSQUERY_PHRASE). Return an integer value representing the +** precedence of the operator. Lower values have a higher precedence (i.e. +** group more tightly). For example, in the C language, the == operator +** groups more tightly than ||, and would therefore have a higher precedence. +** +** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS +** is defined), the order of the operators in precedence from highest to +** lowest is: +** +** NEAR +** NOT +** AND (including implicit ANDs) +** OR +** +** Note that when using the old query syntax, the OR operator has a higher +** precedence than the AND operator. +*/ +static int opPrecedence(Fts3Expr *p){ + assert( p->eType!=FTSQUERY_PHRASE ); + if( sqlite3_fts3_enable_parentheses ){ + return p->eType; + }else if( p->eType==FTSQUERY_NEAR ){ + return 1; + }else if( p->eType==FTSQUERY_OR ){ + return 2; + } + assert( p->eType==FTSQUERY_AND ); + return 3; +} + +/* +** Argument ppHead contains a pointer to the current head of a query +** expression tree being parsed. pPrev is the expression node most recently +** inserted into the tree. This function adds pNew, which is always a binary +** operator node, into the expression tree based on the relative precedence +** of pNew and the existing nodes of the tree. This may result in the head +** of the tree changing, in which case *ppHead is set to the new root node. +*/ +static void insertBinaryOperator( + Fts3Expr **ppHead, /* Pointer to the root node of a tree */ + Fts3Expr *pPrev, /* Node most recently inserted into the tree */ + Fts3Expr *pNew /* New binary node to insert into expression tree */ +){ + Fts3Expr *pSplit = pPrev; + while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){ + pSplit = pSplit->pParent; + } + + if( pSplit->pParent ){ + assert( pSplit->pParent->pRight==pSplit ); + pSplit->pParent->pRight = pNew; + pNew->pParent = pSplit->pParent; + }else{ + *ppHead = pNew; + } + pNew->pLeft = pSplit; + pSplit->pParent = pNew; +} + +/* +** Parse the fts3 query expression found in buffer z, length n. This function +** returns either when the end of the buffer is reached or an unmatched +** closing bracket - ')' - is encountered. +** +** If successful, SQLITE_OK is returned, *ppExpr is set to point to the +** parsed form of the expression and *pnConsumed is set to the number of +** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM +** (out of memory error) or SQLITE_ERROR (parse error) is returned. +*/ +static int fts3ExprParse( + ParseContext *pParse, /* fts3 query parse context */ + const char *z, int n, /* Text of MATCH query */ + Fts3Expr **ppExpr, /* OUT: Parsed query structure */ + int *pnConsumed /* OUT: Number of bytes consumed */ +){ + Fts3Expr *pRet = 0; + Fts3Expr *pPrev = 0; + Fts3Expr *pNotBranch = 0; /* Only used in legacy parse mode */ + int nIn = n; + const char *zIn = z; + int rc = SQLITE_OK; + int isRequirePhrase = 1; + + while( rc==SQLITE_OK ){ + Fts3Expr *p = 0; + int nByte = 0; + rc = getNextNode(pParse, zIn, nIn, &p, &nByte); + if( rc==SQLITE_OK ){ + int isPhrase; + + if( !sqlite3_fts3_enable_parentheses + && p->eType==FTSQUERY_PHRASE && p->pPhrase->isNot + ){ + /* Create an implicit NOT operator. */ + Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr)); + if( !pNot ){ + sqlite3Fts3ExprFree(p); + rc = SQLITE_NOMEM; + goto exprparse_out; + } + pNot->eType = FTSQUERY_NOT; + pNot->pRight = p; + if( pNotBranch ){ + pNot->pLeft = pNotBranch; + } + pNotBranch = pNot; + p = pPrev; + }else{ + int eType = p->eType; + assert( eType!=FTSQUERY_PHRASE || !p->pPhrase->isNot ); + isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft); + + /* The isRequirePhrase variable is set to true if a phrase or + ** an expression contained in parenthesis is required. If a + ** binary operator (AND, OR, NOT or NEAR) is encounted when + ** isRequirePhrase is set, this is a syntax error. + */ + if( !isPhrase && isRequirePhrase ){ + sqlite3Fts3ExprFree(p); + rc = SQLITE_ERROR; + goto exprparse_out; + } + + if( isPhrase && !isRequirePhrase ){ + /* Insert an implicit AND operator. */ + Fts3Expr *pAnd; + assert( pRet && pPrev ); + pAnd = fts3MallocZero(sizeof(Fts3Expr)); + if( !pAnd ){ + sqlite3Fts3ExprFree(p); + rc = SQLITE_NOMEM; + goto exprparse_out; + } + pAnd->eType = FTSQUERY_AND; + insertBinaryOperator(&pRet, pPrev, pAnd); + pPrev = pAnd; + } + + /* This test catches attempts to make either operand of a NEAR + ** operator something other than a phrase. For example, either of + ** the following: + ** + ** (bracketed expression) NEAR phrase + ** phrase NEAR (bracketed expression) + ** + ** Return an error in either case. + */ + if( pPrev && ( + (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE) + || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR) + )){ + sqlite3Fts3ExprFree(p); + rc = SQLITE_ERROR; + goto exprparse_out; + } + + if( isPhrase ){ + if( pRet ){ + assert( pPrev && pPrev->pLeft && pPrev->pRight==0 ); + pPrev->pRight = p; + p->pParent = pPrev; + }else{ + pRet = p; + } + }else{ + insertBinaryOperator(&pRet, pPrev, p); + } + isRequirePhrase = !isPhrase; + } + assert( nByte>0 ); + } + assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) ); + nIn -= nByte; + zIn += nByte; + pPrev = p; + } + + if( rc==SQLITE_DONE && pRet && isRequirePhrase ){ + rc = SQLITE_ERROR; + } + + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + if( !sqlite3_fts3_enable_parentheses && pNotBranch ){ + if( !pRet ){ + rc = SQLITE_ERROR; + }else{ + Fts3Expr *pIter = pNotBranch; + while( pIter->pLeft ){ + pIter = pIter->pLeft; + } + pIter->pLeft = pRet; + pRet = pNotBranch; + } + } + } + *pnConsumed = n - nIn; + +exprparse_out: + if( rc!=SQLITE_OK ){ + sqlite3Fts3ExprFree(pRet); + sqlite3Fts3ExprFree(pNotBranch); + pRet = 0; + } + *ppExpr = pRet; + return rc; +} + +/* +** Parameters z and n contain a pointer to and length of a buffer containing +** an fts3 query expression, respectively. This function attempts to parse the +** query expression and create a tree of Fts3Expr structures representing the +** parsed expression. If successful, *ppExpr is set to point to the head +** of the parsed expression tree and SQLITE_OK is returned. If an error +** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse +** error) is returned and *ppExpr is set to 0. +** +** If parameter n is a negative number, then z is assumed to point to a +** nul-terminated string and the length is determined using strlen(). +** +** The first parameter, pTokenizer, is passed the fts3 tokenizer module to +** use to normalize query tokens while parsing the expression. The azCol[] +** array, which is assumed to contain nCol entries, should contain the names +** of each column in the target fts3 table, in order from left to right. +** Column names must be nul-terminated strings. +** +** The iDefaultCol parameter should be passed the index of the table column +** that appears on the left-hand-side of the MATCH operator (the default +** column to match against for tokens for which a column name is not explicitly +** specified as part of the query string), or -1 if tokens may by default +** match any table column. +*/ +SQLITE_PRIVATE int sqlite3Fts3ExprParse( + sqlite3_tokenizer *pTokenizer, /* Tokenizer module */ + char **azCol, /* Array of column names for fts3 table */ + int nCol, /* Number of entries in azCol[] */ + int iDefaultCol, /* Default column to query */ + const char *z, int n, /* Text of MATCH query */ + Fts3Expr **ppExpr /* OUT: Parsed query structure */ +){ + int nParsed; + int rc; + ParseContext sParse; + sParse.pTokenizer = pTokenizer; + sParse.azCol = (const char **)azCol; + sParse.nCol = nCol; + sParse.iDefaultCol = iDefaultCol; + sParse.nNest = 0; + if( z==0 ){ + *ppExpr = 0; + return SQLITE_OK; + } + if( n<0 ){ + n = (int)strlen(z); + } + rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed); + + /* Check for mismatched parenthesis */ + if( rc==SQLITE_OK && sParse.nNest ){ + rc = SQLITE_ERROR; + sqlite3Fts3ExprFree(*ppExpr); + *ppExpr = 0; + } + + return rc; +} + +/* +** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse(). +*/ +SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *p){ + if( p ){ + sqlite3Fts3ExprFree(p->pLeft); + sqlite3Fts3ExprFree(p->pRight); + sqlite3_free(p->aDoclist); + sqlite3_free(p); + } +} + +/**************************************************************************** +***************************************************************************** +** Everything after this point is just test code. +*/ + +#ifdef SQLITE_TEST + + +/* +** Function to query the hash-table of tokenizers (see README.tokenizers). +*/ +static int queryTestTokenizer( + sqlite3 *db, + const char *zName, + const sqlite3_tokenizer_module **pp +){ + int rc; + sqlite3_stmt *pStmt; + const char zSql[] = "SELECT fts3_tokenizer(?)"; + + *pp = 0; + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ + memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); + } + } + + return sqlite3_finalize(pStmt); +} + +/* +** Return a pointer to a buffer containing a text representation of the +** expression passed as the first argument. The buffer is obtained from +** sqlite3_malloc(). It is the responsibility of the caller to use +** sqlite3_free() to release the memory. If an OOM condition is encountered, +** NULL is returned. +** +** If the second argument is not NULL, then its contents are prepended to +** the returned expression text and then freed using sqlite3_free(). +*/ +static char *exprToString(Fts3Expr *pExpr, char *zBuf){ + switch( pExpr->eType ){ + case FTSQUERY_PHRASE: { + Fts3Phrase *pPhrase = pExpr->pPhrase; + int i; + zBuf = sqlite3_mprintf( + "%zPHRASE %d %d", zBuf, pPhrase->iColumn, pPhrase->isNot); + for(i=0; zBuf && inToken; i++){ + zBuf = sqlite3_mprintf("%z %.*s%s", zBuf, + pPhrase->aToken[i].n, pPhrase->aToken[i].z, + (pPhrase->aToken[i].isPrefix?"+":"") + ); + } + return zBuf; + } + + case FTSQUERY_NEAR: + zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear); + break; + case FTSQUERY_NOT: + zBuf = sqlite3_mprintf("%zNOT ", zBuf); + break; + case FTSQUERY_AND: + zBuf = sqlite3_mprintf("%zAND ", zBuf); + break; + case FTSQUERY_OR: + zBuf = sqlite3_mprintf("%zOR ", zBuf); + break; + } + + if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf); + if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf); + if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf); + + if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf); + if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf); + + return zBuf; +} + +/* +** This is the implementation of a scalar SQL function used to test the +** expression parser. It should be called as follows: +** +** fts3_exprtest(, , , ...); +** +** The first argument, , is the name of the fts3 tokenizer used +** to parse the query expression (see README.tokenizers). The second argument +** is the query expression to parse. Each subsequent argument is the name +** of a column of the fts3 table that the query expression may refer to. +** For example: +** +** SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2'); +*/ +static void fts3ExprTest( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + sqlite3_tokenizer_module const *pModule = 0; + sqlite3_tokenizer *pTokenizer = 0; + int rc; + char **azCol = 0; + const char *zExpr; + int nExpr; + int nCol; + int ii; + Fts3Expr *pExpr; + char *zBuf = 0; + sqlite3 *db = sqlite3_context_db_handle(context); + + if( argc<3 ){ + sqlite3_result_error(context, + "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1 + ); + return; + } + + rc = queryTestTokenizer(db, + (const char *)sqlite3_value_text(argv[0]), &pModule); + if( rc==SQLITE_NOMEM ){ + sqlite3_result_error_nomem(context); + goto exprtest_out; + }else if( !pModule ){ + sqlite3_result_error(context, "No such tokenizer module", -1); + goto exprtest_out; + } + + rc = pModule->xCreate(0, 0, &pTokenizer); + assert( rc==SQLITE_NOMEM || rc==SQLITE_OK ); + if( rc==SQLITE_NOMEM ){ + sqlite3_result_error_nomem(context); + goto exprtest_out; + } + pTokenizer->pModule = pModule; + + zExpr = (const char *)sqlite3_value_text(argv[1]); + nExpr = sqlite3_value_bytes(argv[1]); + nCol = argc-2; + azCol = (char **)sqlite3_malloc(nCol*sizeof(char *)); + if( !azCol ){ + sqlite3_result_error_nomem(context); + goto exprtest_out; + } + for(ii=0; iixDestroy(pTokenizer); + } + sqlite3_free(azCol); +} + +/* +** Register the query expression parser test function fts3_exprtest() +** with database connection db. +*/ +SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){ + return sqlite3_create_function( + db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0 + ); +} + +#endif +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_expr.c *******************************************/ +/************** Begin file fts3_hash.c ***************************************/ +/* +** 2001 September 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This is the implementation of generic hash-tables used in SQLite. +** We've modified it slightly to serve as a standalone hash table +** implementation for the full-text indexing module. +*/ + +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + + + +/* +** Malloc and Free functions +*/ +static void *fts3HashMalloc(int n){ + void *p = sqlite3_malloc(n); + if( p ){ + memset(p, 0, n); + } + return p; +} +static void fts3HashFree(void *p){ + sqlite3_free(p); +} + +/* Turn bulk memory into a hash table object by initializing the +** fields of the Hash structure. +** +** "pNew" is a pointer to the hash table that is to be initialized. +** keyClass is one of the constants +** FTS3_HASH_BINARY or FTS3_HASH_STRING. The value of keyClass +** determines what kind of key the hash table will use. "copyKey" is +** true if the hash table should make its own private copy of keys and +** false if it should just use the supplied pointer. +*/ +SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){ + assert( pNew!=0 ); + assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY ); + pNew->keyClass = keyClass; + pNew->copyKey = copyKey; + pNew->first = 0; + pNew->count = 0; + pNew->htsize = 0; + pNew->ht = 0; +} + +/* Remove all entries from a hash table. Reclaim all memory. +** Call this routine to delete a hash table or to reset a hash table +** to the empty state. +*/ +SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){ + Fts3HashElem *elem; /* For looping over all elements of the table */ + + assert( pH!=0 ); + elem = pH->first; + pH->first = 0; + fts3HashFree(pH->ht); + pH->ht = 0; + pH->htsize = 0; + while( elem ){ + Fts3HashElem *next_elem = elem->next; + if( pH->copyKey && elem->pKey ){ + fts3HashFree(elem->pKey); + } + fts3HashFree(elem); + elem = next_elem; + } + pH->count = 0; +} + +/* +** Hash and comparison functions when the mode is FTS3_HASH_STRING +*/ +static int fts3StrHash(const void *pKey, int nKey){ + const char *z = (const char *)pKey; + int h = 0; + if( nKey<=0 ) nKey = (int) strlen(z); + while( nKey > 0 ){ + h = (h<<3) ^ h ^ *z++; + nKey--; + } + return h & 0x7fffffff; +} +static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){ + if( n1!=n2 ) return 1; + return strncmp((const char*)pKey1,(const char*)pKey2,n1); +} + +/* +** Hash and comparison functions when the mode is FTS3_HASH_BINARY +*/ +static int fts3BinHash(const void *pKey, int nKey){ + int h = 0; + const char *z = (const char *)pKey; + while( nKey-- > 0 ){ + h = (h<<3) ^ h ^ *(z++); + } + return h & 0x7fffffff; +} +static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){ + if( n1!=n2 ) return 1; + return memcmp(pKey1,pKey2,n1); +} + +/* +** Return a pointer to the appropriate hash function given the key class. +** +** The C syntax in this function definition may be unfamilar to some +** programmers, so we provide the following additional explanation: +** +** The name of the function is "ftsHashFunction". The function takes a +** single parameter "keyClass". The return value of ftsHashFunction() +** is a pointer to another function. Specifically, the return value +** of ftsHashFunction() is a pointer to a function that takes two parameters +** with types "const void*" and "int" and returns an "int". +*/ +static int (*ftsHashFunction(int keyClass))(const void*,int){ + if( keyClass==FTS3_HASH_STRING ){ + return &fts3StrHash; + }else{ + assert( keyClass==FTS3_HASH_BINARY ); + return &fts3BinHash; + } +} + +/* +** Return a pointer to the appropriate hash function given the key class. +** +** For help in interpreted the obscure C code in the function definition, +** see the header comment on the previous function. +*/ +static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){ + if( keyClass==FTS3_HASH_STRING ){ + return &fts3StrCompare; + }else{ + assert( keyClass==FTS3_HASH_BINARY ); + return &fts3BinCompare; + } +} + +/* Link an element into the hash table +*/ +static void fts3HashInsertElement( + Fts3Hash *pH, /* The complete hash table */ + struct _fts3ht *pEntry, /* The entry into which pNew is inserted */ + Fts3HashElem *pNew /* The element to be inserted */ +){ + Fts3HashElem *pHead; /* First element already in pEntry */ + pHead = pEntry->chain; + if( pHead ){ + pNew->next = pHead; + pNew->prev = pHead->prev; + if( pHead->prev ){ pHead->prev->next = pNew; } + else { pH->first = pNew; } + pHead->prev = pNew; + }else{ + pNew->next = pH->first; + if( pH->first ){ pH->first->prev = pNew; } + pNew->prev = 0; + pH->first = pNew; + } + pEntry->count++; + pEntry->chain = pNew; +} + + +/* Resize the hash table so that it cantains "new_size" buckets. +** "new_size" must be a power of 2. The hash table might fail +** to resize if sqliteMalloc() fails. +** +** Return non-zero if a memory allocation error occurs. +*/ +static int fts3Rehash(Fts3Hash *pH, int new_size){ + struct _fts3ht *new_ht; /* The new hash table */ + Fts3HashElem *elem, *next_elem; /* For looping over existing elements */ + int (*xHash)(const void*,int); /* The hash function */ + + assert( (new_size & (new_size-1))==0 ); + new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) ); + if( new_ht==0 ) return 1; + fts3HashFree(pH->ht); + pH->ht = new_ht; + pH->htsize = new_size; + xHash = ftsHashFunction(pH->keyClass); + for(elem=pH->first, pH->first=0; elem; elem = next_elem){ + int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1); + next_elem = elem->next; + fts3HashInsertElement(pH, &new_ht[h], elem); + } + return 0; +} + +/* This function (for internal use only) locates an element in an +** hash table that matches the given key. The hash for this key has +** already been computed and is passed as the 4th parameter. +*/ +static Fts3HashElem *fts3FindElementByHash( + const Fts3Hash *pH, /* The pH to be searched */ + const void *pKey, /* The key we are searching for */ + int nKey, + int h /* The hash for this key. */ +){ + Fts3HashElem *elem; /* Used to loop thru the element list */ + int count; /* Number of elements left to test */ + int (*xCompare)(const void*,int,const void*,int); /* comparison function */ + + if( pH->ht ){ + struct _fts3ht *pEntry = &pH->ht[h]; + elem = pEntry->chain; + count = pEntry->count; + xCompare = ftsCompareFunction(pH->keyClass); + while( count-- && elem ){ + if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){ + return elem; + } + elem = elem->next; + } + } + return 0; +} + +/* Remove a single entry from the hash table given a pointer to that +** element and a hash on the element's key. +*/ +static void fts3RemoveElementByHash( + Fts3Hash *pH, /* The pH containing "elem" */ + Fts3HashElem* elem, /* The element to be removed from the pH */ + int h /* Hash value for the element */ +){ + struct _fts3ht *pEntry; + if( elem->prev ){ + elem->prev->next = elem->next; + }else{ + pH->first = elem->next; + } + if( elem->next ){ + elem->next->prev = elem->prev; + } + pEntry = &pH->ht[h]; + if( pEntry->chain==elem ){ + pEntry->chain = elem->next; + } + pEntry->count--; + if( pEntry->count<=0 ){ + pEntry->chain = 0; + } + if( pH->copyKey && elem->pKey ){ + fts3HashFree(elem->pKey); + } + fts3HashFree( elem ); + pH->count--; + if( pH->count<=0 ){ + assert( pH->first==0 ); + assert( pH->count==0 ); + fts3HashClear(pH); + } +} + +SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem( + const Fts3Hash *pH, + const void *pKey, + int nKey +){ + int h; /* A hash on key */ + int (*xHash)(const void*,int); /* The hash function */ + + if( pH==0 || pH->ht==0 ) return 0; + xHash = ftsHashFunction(pH->keyClass); + assert( xHash!=0 ); + h = (*xHash)(pKey,nKey); + assert( (pH->htsize & (pH->htsize-1))==0 ); + return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1)); +} + +/* +** Attempt to locate an element of the hash table pH with a key +** that matches pKey,nKey. Return the data for this element if it is +** found, or NULL if there is no match. +*/ +SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){ + Fts3HashElem *pElem; /* The element that matches key (if any) */ + + pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey); + return pElem ? pElem->data : 0; +} + +/* Insert an element into the hash table pH. The key is pKey,nKey +** and the data is "data". +** +** If no element exists with a matching key, then a new +** element is created. A copy of the key is made if the copyKey +** flag is set. NULL is returned. +** +** If another element already exists with the same key, then the +** new data replaces the old data and the old data is returned. +** The key is not copied in this instance. If a malloc fails, then +** the new data is returned and the hash table is unchanged. +** +** If the "data" parameter to this function is NULL, then the +** element corresponding to "key" is removed from the hash table. +*/ +SQLITE_PRIVATE void *sqlite3Fts3HashInsert( + Fts3Hash *pH, /* The hash table to insert into */ + const void *pKey, /* The key */ + int nKey, /* Number of bytes in the key */ + void *data /* The data */ +){ + int hraw; /* Raw hash value of the key */ + int h; /* the hash of the key modulo hash table size */ + Fts3HashElem *elem; /* Used to loop thru the element list */ + Fts3HashElem *new_elem; /* New element added to the pH */ + int (*xHash)(const void*,int); /* The hash function */ + + assert( pH!=0 ); + xHash = ftsHashFunction(pH->keyClass); + assert( xHash!=0 ); + hraw = (*xHash)(pKey, nKey); + assert( (pH->htsize & (pH->htsize-1))==0 ); + h = hraw & (pH->htsize-1); + elem = fts3FindElementByHash(pH,pKey,nKey,h); + if( elem ){ + void *old_data = elem->data; + if( data==0 ){ + fts3RemoveElementByHash(pH,elem,h); + }else{ + elem->data = data; + } + return old_data; + } + if( data==0 ) return 0; + if( (pH->htsize==0 && fts3Rehash(pH,8)) + || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2)) + ){ + pH->count = 0; + return data; + } + assert( pH->htsize>0 ); + new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) ); + if( new_elem==0 ) return data; + if( pH->copyKey && pKey!=0 ){ + new_elem->pKey = fts3HashMalloc( nKey ); + if( new_elem->pKey==0 ){ + fts3HashFree(new_elem); + return data; + } + memcpy((void*)new_elem->pKey, pKey, nKey); + }else{ + new_elem->pKey = (void*)pKey; + } + new_elem->nKey = nKey; + pH->count++; + assert( pH->htsize>0 ); + assert( (pH->htsize & (pH->htsize-1))==0 ); + h = hraw & (pH->htsize-1); + fts3HashInsertElement(pH, &pH->ht[h], new_elem); + new_elem->data = data; + return 0; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_hash.c *******************************************/ +/************** Begin file fts3_porter.c *************************************/ +/* +** 2006 September 30 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Implementation of the full-text-search tokenizer that implements +** a Porter stemmer. +*/ + +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + + + + +/* +** Class derived from sqlite3_tokenizer +*/ +typedef struct porter_tokenizer { + sqlite3_tokenizer base; /* Base class */ +} porter_tokenizer; + +/* +** Class derived from sqlit3_tokenizer_cursor +*/ +typedef struct porter_tokenizer_cursor { + sqlite3_tokenizer_cursor base; + const char *zInput; /* input we are tokenizing */ + int nInput; /* size of the input */ + int iOffset; /* current position in zInput */ + int iToken; /* index of next token to be returned */ + char *zToken; /* storage for current token */ + int nAllocated; /* space allocated to zToken buffer */ +} porter_tokenizer_cursor; + + +/* +** Create a new tokenizer instance. +*/ +static int porterCreate( + int argc, const char * const *argv, + sqlite3_tokenizer **ppTokenizer +){ + porter_tokenizer *t; + + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(argv); + + t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t)); + if( t==NULL ) return SQLITE_NOMEM; + memset(t, 0, sizeof(*t)); + *ppTokenizer = &t->base; + return SQLITE_OK; +} + +/* +** Destroy a tokenizer +*/ +static int porterDestroy(sqlite3_tokenizer *pTokenizer){ + sqlite3_free(pTokenizer); + return SQLITE_OK; +} + +/* +** Prepare to begin tokenizing a particular string. The input +** string to be tokenized is zInput[0..nInput-1]. A cursor +** used to incrementally tokenize this string is returned in +** *ppCursor. +*/ +static int porterOpen( + sqlite3_tokenizer *pTokenizer, /* The tokenizer */ + const char *zInput, int nInput, /* String to be tokenized */ + sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ +){ + porter_tokenizer_cursor *c; + + UNUSED_PARAMETER(pTokenizer); + + c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); + if( c==NULL ) return SQLITE_NOMEM; + + c->zInput = zInput; + if( zInput==0 ){ + c->nInput = 0; + }else if( nInput<0 ){ + c->nInput = (int)strlen(zInput); + }else{ + c->nInput = nInput; + } + c->iOffset = 0; /* start tokenizing at the beginning */ + c->iToken = 0; + c->zToken = NULL; /* no space allocated, yet. */ + c->nAllocated = 0; + + *ppCursor = &c->base; + return SQLITE_OK; +} + +/* +** Close a tokenization cursor previously opened by a call to +** porterOpen() above. +*/ +static int porterClose(sqlite3_tokenizer_cursor *pCursor){ + porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; + sqlite3_free(c->zToken); + sqlite3_free(c); + return SQLITE_OK; +} +/* +** Vowel or consonant +*/ +static const char cType[] = { + 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, + 1, 1, 1, 2, 1 +}; + +/* +** isConsonant() and isVowel() determine if their first character in +** the string they point to is a consonant or a vowel, according +** to Porter ruls. +** +** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'. +** 'Y' is a consonant unless it follows another consonant, +** in which case it is a vowel. +** +** In these routine, the letters are in reverse order. So the 'y' rule +** is that 'y' is a consonant unless it is followed by another +** consonent. +*/ +static int isVowel(const char*); +static int isConsonant(const char *z){ + int j; + char x = *z; + if( x==0 ) return 0; + assert( x>='a' && x<='z' ); + j = cType[x-'a']; + if( j<2 ) return j; + return z[1]==0 || isVowel(z + 1); +} +static int isVowel(const char *z){ + int j; + char x = *z; + if( x==0 ) return 0; + assert( x>='a' && x<='z' ); + j = cType[x-'a']; + if( j<2 ) return 1-j; + return isConsonant(z + 1); +} + +/* +** Let any sequence of one or more vowels be represented by V and let +** C be sequence of one or more consonants. Then every word can be +** represented as: +** +** [C] (VC){m} [V] +** +** In prose: A word is an optional consonant followed by zero or +** vowel-consonant pairs followed by an optional vowel. "m" is the +** number of vowel consonant pairs. This routine computes the value +** of m for the first i bytes of a word. +** +** Return true if the m-value for z is 1 or more. In other words, +** return true if z contains at least one vowel that is followed +** by a consonant. +** +** In this routine z[] is in reverse order. So we are really looking +** for an instance of of a consonant followed by a vowel. +*/ +static int m_gt_0(const char *z){ + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + return *z!=0; +} + +/* Like mgt0 above except we are looking for a value of m which is +** exactly 1 +*/ +static int m_eq_1(const char *z){ + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + if( *z==0 ) return 0; + while( isVowel(z) ){ z++; } + if( *z==0 ) return 1; + while( isConsonant(z) ){ z++; } + return *z==0; +} + +/* Like mgt0 above except we are looking for a value of m>1 instead +** or m>0 +*/ +static int m_gt_1(const char *z){ + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + if( *z==0 ) return 0; + while( isVowel(z) ){ z++; } + if( *z==0 ) return 0; + while( isConsonant(z) ){ z++; } + return *z!=0; +} + +/* +** Return TRUE if there is a vowel anywhere within z[0..n-1] +*/ +static int hasVowel(const char *z){ + while( isConsonant(z) ){ z++; } + return *z!=0; +} + +/* +** Return TRUE if the word ends in a double consonant. +** +** The text is reversed here. So we are really looking at +** the first two characters of z[]. +*/ +static int doubleConsonant(const char *z){ + return isConsonant(z) && z[0]==z[1]; +} + +/* +** Return TRUE if the word ends with three letters which +** are consonant-vowel-consonent and where the final consonant +** is not 'w', 'x', or 'y'. +** +** The word is reversed here. So we are really checking the +** first three letters and the first one cannot be in [wxy]. +*/ +static int star_oh(const char *z){ + return + isConsonant(z) && + z[0]!='w' && z[0]!='x' && z[0]!='y' && + isVowel(z+1) && + isConsonant(z+2); +} + +/* +** If the word ends with zFrom and xCond() is true for the stem +** of the word that preceeds the zFrom ending, then change the +** ending to zTo. +** +** The input word *pz and zFrom are both in reverse order. zTo +** is in normal order. +** +** Return TRUE if zFrom matches. Return FALSE if zFrom does not +** match. Not that TRUE is returned even if xCond() fails and +** no substitution occurs. +*/ +static int stem( + char **pz, /* The word being stemmed (Reversed) */ + const char *zFrom, /* If the ending matches this... (Reversed) */ + const char *zTo, /* ... change the ending to this (not reversed) */ + int (*xCond)(const char*) /* Condition that must be true */ +){ + char *z = *pz; + while( *zFrom && *zFrom==*z ){ z++; zFrom++; } + if( *zFrom!=0 ) return 0; + if( xCond && !xCond(z) ) return 1; + while( *zTo ){ + *(--z) = *(zTo++); + } + *pz = z; + return 1; +} + +/* +** This is the fallback stemmer used when the porter stemmer is +** inappropriate. The input word is copied into the output with +** US-ASCII case folding. If the input word is too long (more +** than 20 bytes if it contains no digits or more than 6 bytes if +** it contains digits) then word is truncated to 20 or 6 bytes +** by taking 10 or 3 bytes from the beginning and end. +*/ +static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){ + int i, mx, j; + int hasDigit = 0; + for(i=0; i='A' && c<='Z' ){ + zOut[i] = c - 'A' + 'a'; + }else{ + if( c>='0' && c<='9' ) hasDigit = 1; + zOut[i] = c; + } + } + mx = hasDigit ? 3 : 10; + if( nIn>mx*2 ){ + for(j=mx, i=nIn-mx; i=(int)sizeof(zReverse)-7 ){ + /* The word is too big or too small for the porter stemmer. + ** Fallback to the copy stemmer */ + copy_stemmer(zIn, nIn, zOut, pnOut); + return; + } + for(i=0, j=sizeof(zReverse)-6; i='A' && c<='Z' ){ + zReverse[j] = c + 'a' - 'A'; + }else if( c>='a' && c<='z' ){ + zReverse[j] = c; + }else{ + /* The use of a character not in [a-zA-Z] means that we fallback + ** to the copy stemmer */ + copy_stemmer(zIn, nIn, zOut, pnOut); + return; + } + } + memset(&zReverse[sizeof(zReverse)-5], 0, 5); + z = &zReverse[j+1]; + + + /* Step 1a */ + if( z[0]=='s' ){ + if( + !stem(&z, "sess", "ss", 0) && + !stem(&z, "sei", "i", 0) && + !stem(&z, "ss", "ss", 0) + ){ + z++; + } + } + + /* Step 1b */ + z2 = z; + if( stem(&z, "dee", "ee", m_gt_0) ){ + /* Do nothing. The work was all in the test */ + }else if( + (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel)) + && z!=z2 + ){ + if( stem(&z, "ta", "ate", 0) || + stem(&z, "lb", "ble", 0) || + stem(&z, "zi", "ize", 0) ){ + /* Do nothing. The work was all in the test */ + }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){ + z++; + }else if( m_eq_1(z) && star_oh(z) ){ + *(--z) = 'e'; + } + } + + /* Step 1c */ + if( z[0]=='y' && hasVowel(z+1) ){ + z[0] = 'i'; + } + + /* Step 2 */ + switch( z[1] ){ + case 'a': + stem(&z, "lanoita", "ate", m_gt_0) || + stem(&z, "lanoit", "tion", m_gt_0); + break; + case 'c': + stem(&z, "icne", "ence", m_gt_0) || + stem(&z, "icna", "ance", m_gt_0); + break; + case 'e': + stem(&z, "rezi", "ize", m_gt_0); + break; + case 'g': + stem(&z, "igol", "log", m_gt_0); + break; + case 'l': + stem(&z, "ilb", "ble", m_gt_0) || + stem(&z, "illa", "al", m_gt_0) || + stem(&z, "iltne", "ent", m_gt_0) || + stem(&z, "ile", "e", m_gt_0) || + stem(&z, "ilsuo", "ous", m_gt_0); + break; + case 'o': + stem(&z, "noitazi", "ize", m_gt_0) || + stem(&z, "noita", "ate", m_gt_0) || + stem(&z, "rota", "ate", m_gt_0); + break; + case 's': + stem(&z, "msila", "al", m_gt_0) || + stem(&z, "ssenevi", "ive", m_gt_0) || + stem(&z, "ssenluf", "ful", m_gt_0) || + stem(&z, "ssensuo", "ous", m_gt_0); + break; + case 't': + stem(&z, "itila", "al", m_gt_0) || + stem(&z, "itivi", "ive", m_gt_0) || + stem(&z, "itilib", "ble", m_gt_0); + break; + } + + /* Step 3 */ + switch( z[0] ){ + case 'e': + stem(&z, "etaci", "ic", m_gt_0) || + stem(&z, "evita", "", m_gt_0) || + stem(&z, "ezila", "al", m_gt_0); + break; + case 'i': + stem(&z, "itici", "ic", m_gt_0); + break; + case 'l': + stem(&z, "laci", "ic", m_gt_0) || + stem(&z, "luf", "", m_gt_0); + break; + case 's': + stem(&z, "ssen", "", m_gt_0); + break; + } + + /* Step 4 */ + switch( z[1] ){ + case 'a': + if( z[0]=='l' && m_gt_1(z+2) ){ + z += 2; + } + break; + case 'c': + if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e') && m_gt_1(z+4) ){ + z += 4; + } + break; + case 'e': + if( z[0]=='r' && m_gt_1(z+2) ){ + z += 2; + } + break; + case 'i': + if( z[0]=='c' && m_gt_1(z+2) ){ + z += 2; + } + break; + case 'l': + if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){ + z += 4; + } + break; + case 'n': + if( z[0]=='t' ){ + if( z[2]=='a' ){ + if( m_gt_1(z+3) ){ + z += 3; + } + }else if( z[2]=='e' ){ + stem(&z, "tneme", "", m_gt_1) || + stem(&z, "tnem", "", m_gt_1) || + stem(&z, "tne", "", m_gt_1); + } + } + break; + case 'o': + if( z[0]=='u' ){ + if( m_gt_1(z+2) ){ + z += 2; + } + }else if( z[3]=='s' || z[3]=='t' ){ + stem(&z, "noi", "", m_gt_1); + } + break; + case 's': + if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){ + z += 3; + } + break; + case 't': + stem(&z, "eta", "", m_gt_1) || + stem(&z, "iti", "", m_gt_1); + break; + case 'u': + if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){ + z += 3; + } + break; + case 'v': + case 'z': + if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){ + z += 3; + } + break; + } + + /* Step 5a */ + if( z[0]=='e' ){ + if( m_gt_1(z+1) ){ + z++; + }else if( m_eq_1(z+1) && !star_oh(z+1) ){ + z++; + } + } + + /* Step 5b */ + if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){ + z++; + } + + /* z[] is now the stemmed word in reverse order. Flip it back + ** around into forward order and return. + */ + *pnOut = i = (int)strlen(z); + zOut[i] = 0; + while( *z ){ + zOut[--i] = *(z++); + } +} + +/* +** Characters that can be part of a token. We assume any character +** whose value is greater than 0x80 (any UTF character) can be +** part of a token. In other words, delimiters all must have +** values of 0x7f or lower. +*/ +static const char porterIdChar[] = { +/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ +}; +#define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30])) + +/* +** Extract the next token from a tokenization cursor. The cursor must +** have been opened by a prior call to porterOpen(). +*/ +static int porterNext( + sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by porterOpen */ + const char **pzToken, /* OUT: *pzToken is the token text */ + int *pnBytes, /* OUT: Number of bytes in token */ + int *piStartOffset, /* OUT: Starting offset of token */ + int *piEndOffset, /* OUT: Ending offset of token */ + int *piPosition /* OUT: Position integer of token */ +){ + porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor; + const char *z = c->zInput; + + while( c->iOffsetnInput ){ + int iStartOffset, ch; + + /* Scan past delimiter characters */ + while( c->iOffsetnInput && isDelim(z[c->iOffset]) ){ + c->iOffset++; + } + + /* Count non-delimiter characters. */ + iStartOffset = c->iOffset; + while( c->iOffsetnInput && !isDelim(z[c->iOffset]) ){ + c->iOffset++; + } + + if( c->iOffset>iStartOffset ){ + int n = c->iOffset-iStartOffset; + if( n>c->nAllocated ){ + char *pNew; + c->nAllocated = n+20; + pNew = sqlite3_realloc(c->zToken, c->nAllocated); + if( !pNew ) return SQLITE_NOMEM; + c->zToken = pNew; + } + porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes); + *pzToken = c->zToken; + *piStartOffset = iStartOffset; + *piEndOffset = c->iOffset; + *piPosition = c->iToken++; + return SQLITE_OK; + } + } + return SQLITE_DONE; +} + +/* +** The set of routines that implement the porter-stemmer tokenizer +*/ +static const sqlite3_tokenizer_module porterTokenizerModule = { + 0, + porterCreate, + porterDestroy, + porterOpen, + porterClose, + porterNext, +}; + +/* +** Allocate a new porter tokenizer. Return a pointer to the new +** tokenizer in *ppModule +*/ +SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule( + sqlite3_tokenizer_module const**ppModule +){ + *ppModule = &porterTokenizerModule; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_porter.c *****************************************/ +/************** Begin file fts3_tokenizer.c **********************************/ +/* +** 2007 June 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This is part of an SQLite module implementing full-text search. +** This particular file implements the generic tokenizer interface. +*/ + +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + +#ifndef SQLITE_CORE + SQLITE_EXTENSION_INIT1 +#endif + + +/* +** Implementation of the SQL scalar function for accessing the underlying +** hash table. This function may be called as follows: +** +** SELECT (); +** SELECT (, ); +** +** where is the name passed as the second argument +** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer'). +** +** If the argument is specified, it must be a blob value +** containing a pointer to be stored as the hash data corresponding +** to the string . If is not specified, then +** the string must already exist in the has table. Otherwise, +** an error is returned. +** +** Whether or not the argument is specified, the value returned +** is a blob containing the pointer stored as the hash data corresponding +** to string (after the hash-table is updated, if applicable). +*/ +static void scalarFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Fts3Hash *pHash; + void *pPtr = 0; + const unsigned char *zName; + int nName; + + assert( argc==1 || argc==2 ); + + pHash = (Fts3Hash *)sqlite3_user_data(context); + + zName = sqlite3_value_text(argv[0]); + nName = sqlite3_value_bytes(argv[0])+1; + + if( argc==2 ){ + void *pOld; + int n = sqlite3_value_bytes(argv[1]); + if( n!=sizeof(pPtr) ){ + sqlite3_result_error(context, "argument type mismatch", -1); + return; + } + pPtr = *(void **)sqlite3_value_blob(argv[1]); + pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr); + if( pOld==pPtr ){ + sqlite3_result_error(context, "out of memory", -1); + return; + } + }else{ + pPtr = sqlite3Fts3HashFind(pHash, zName, nName); + if( !pPtr ){ + char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); + sqlite3_result_error(context, zErr, -1); + sqlite3_free(zErr); + return; + } + } + + sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT); +} + +SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){ + static const char isFtsIdChar[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 1x */ + 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */ + 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */ + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */ + }; + return (c&0x80 || isFtsIdChar[(int)(c)]); +} + +SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){ + const char *z1; + const char *z2 = 0; + + /* Find the start of the next token. */ + z1 = zStr; + while( z2==0 ){ + char c = *z1; + switch( c ){ + case '\0': return 0; /* No more tokens here */ + case '\'': + case '"': + case '`': { + z2 = z1; + while( *++z2 && (*z2!=c || *++z2==c) ); + break; + } + case '[': + z2 = &z1[1]; + while( *z2 && z2[0]!=']' ) z2++; + if( *z2 ) z2++; + break; + + default: + if( sqlite3Fts3IsIdChar(*z1) ){ + z2 = &z1[1]; + while( sqlite3Fts3IsIdChar(*z2) ) z2++; + }else{ + z1++; + } + } + } + + *pn = (int)(z2-z1); + return z1; +} + +SQLITE_PRIVATE int sqlite3Fts3InitTokenizer( + Fts3Hash *pHash, /* Tokenizer hash table */ + const char *zArg, /* Tokenizer name */ + sqlite3_tokenizer **ppTok, /* OUT: Tokenizer (if applicable) */ + char **pzErr /* OUT: Set to malloced error message */ +){ + int rc; + char *z = (char *)zArg; + int n; + char *zCopy; + char *zEnd; /* Pointer to nul-term of zCopy */ + sqlite3_tokenizer_module *m; + + zCopy = sqlite3_mprintf("%s", zArg); + if( !zCopy ) return SQLITE_NOMEM; + zEnd = &zCopy[strlen(zCopy)]; + + z = (char *)sqlite3Fts3NextToken(zCopy, &n); + z[n] = '\0'; + sqlite3Fts3Dequote(z); + + m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1); + if( !m ){ + *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z); + rc = SQLITE_ERROR; + }else{ + char const **aArg = 0; + int iArg = 0; + z = &z[n+1]; + while( zxCreate(iArg, aArg, ppTok); + assert( rc!=SQLITE_OK || *ppTok ); + if( rc!=SQLITE_OK ){ + *pzErr = sqlite3_mprintf("unknown tokenizer"); + }else{ + (*ppTok)->pModule = m; + } + sqlite3_free((void *)aArg); + } + + sqlite3_free(zCopy); + return rc; +} + + +#ifdef SQLITE_TEST + + +/* +** Implementation of a special SQL scalar function for testing tokenizers +** designed to be used in concert with the Tcl testing framework. This +** function must be called with two arguments: +** +** SELECT (, ); +** SELECT (, ); +** +** where is the name passed as the second argument +** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer') +** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test'). +** +** The return value is a string that may be interpreted as a Tcl +** list. For each token in the , three elements are +** added to the returned list. The first is the token position, the +** second is the token text (folded, stemmed, etc.) and the third is the +** substring of associated with the token. For example, +** using the built-in "simple" tokenizer: +** +** SELECT fts_tokenizer_test('simple', 'I don't see how'); +** +** will return the string: +** +** "{0 i I 1 dont don't 2 see see 3 how how}" +** +*/ +static void testFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + Fts3Hash *pHash; + sqlite3_tokenizer_module *p; + sqlite3_tokenizer *pTokenizer = 0; + sqlite3_tokenizer_cursor *pCsr = 0; + + const char *zErr = 0; + + const char *zName; + int nName; + const char *zInput; + int nInput; + + const char *zArg = 0; + + const char *zToken; + int nToken; + int iStart; + int iEnd; + int iPos; + + Tcl_Obj *pRet; + + assert( argc==2 || argc==3 ); + + nName = sqlite3_value_bytes(argv[0]); + zName = (const char *)sqlite3_value_text(argv[0]); + nInput = sqlite3_value_bytes(argv[argc-1]); + zInput = (const char *)sqlite3_value_text(argv[argc-1]); + + if( argc==3 ){ + zArg = (const char *)sqlite3_value_text(argv[1]); + } + + pHash = (Fts3Hash *)sqlite3_user_data(context); + p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1); + + if( !p ){ + char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName); + sqlite3_result_error(context, zErr, -1); + sqlite3_free(zErr); + return; + } + + pRet = Tcl_NewObj(); + Tcl_IncrRefCount(pRet); + + if( SQLITE_OK!=p->xCreate(zArg ? 1 : 0, &zArg, &pTokenizer) ){ + zErr = "error in xCreate()"; + goto finish; + } + pTokenizer->pModule = p; + if( SQLITE_OK!=p->xOpen(pTokenizer, zInput, nInput, &pCsr) ){ + zErr = "error in xOpen()"; + goto finish; + } + pCsr->pTokenizer = pTokenizer; + + while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){ + Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos)); + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); + zToken = &zInput[iStart]; + nToken = iEnd-iStart; + Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken)); + } + + if( SQLITE_OK!=p->xClose(pCsr) ){ + zErr = "error in xClose()"; + goto finish; + } + if( SQLITE_OK!=p->xDestroy(pTokenizer) ){ + zErr = "error in xDestroy()"; + goto finish; + } + +finish: + if( zErr ){ + sqlite3_result_error(context, zErr, -1); + }else{ + sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT); + } + Tcl_DecrRefCount(pRet); +} + +static +int registerTokenizer( + sqlite3 *db, + char *zName, + const sqlite3_tokenizer_module *p +){ + int rc; + sqlite3_stmt *pStmt; + const char zSql[] = "SELECT fts3_tokenizer(?, ?)"; + + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); + sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC); + sqlite3_step(pStmt); + + return sqlite3_finalize(pStmt); +} + +static +int queryTokenizer( + sqlite3 *db, + char *zName, + const sqlite3_tokenizer_module **pp +){ + int rc; + sqlite3_stmt *pStmt; + const char zSql[] = "SELECT fts3_tokenizer(?)"; + + *pp = 0; + rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC); + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){ + memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp)); + } + } + + return sqlite3_finalize(pStmt); +} + +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule); + +/* +** Implementation of the scalar function fts3_tokenizer_internal_test(). +** This function is used for testing only, it is not included in the +** build unless SQLITE_TEST is defined. +** +** The purpose of this is to test that the fts3_tokenizer() function +** can be used as designed by the C-code in the queryTokenizer and +** registerTokenizer() functions above. These two functions are repeated +** in the README.tokenizer file as an example, so it is important to +** test them. +** +** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar +** function with no arguments. An assert() will fail if a problem is +** detected. i.e.: +** +** SELECT fts3_tokenizer_internal_test(); +** +*/ +static void intTestFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + int rc; + const sqlite3_tokenizer_module *p1; + const sqlite3_tokenizer_module *p2; + sqlite3 *db = (sqlite3 *)sqlite3_user_data(context); + + UNUSED_PARAMETER(argc); + UNUSED_PARAMETER(argv); + + /* Test the query function */ + sqlite3Fts3SimpleTokenizerModule(&p1); + rc = queryTokenizer(db, "simple", &p2); + assert( rc==SQLITE_OK ); + assert( p1==p2 ); + rc = queryTokenizer(db, "nosuchtokenizer", &p2); + assert( rc==SQLITE_ERROR ); + assert( p2==0 ); + assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") ); + + /* Test the storage function */ + rc = registerTokenizer(db, "nosuchtokenizer", p1); + assert( rc==SQLITE_OK ); + rc = queryTokenizer(db, "nosuchtokenizer", &p2); + assert( rc==SQLITE_OK ); + assert( p2==p1 ); + + sqlite3_result_text(context, "ok", -1, SQLITE_STATIC); +} + +#endif + +/* +** Set up SQL objects in database db used to access the contents of +** the hash table pointed to by argument pHash. The hash table must +** been initialised to use string keys, and to take a private copy +** of the key when a value is inserted. i.e. by a call similar to: +** +** sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1); +** +** This function adds a scalar function (see header comment above +** scalarFunc() in this file for details) and, if ENABLE_TABLE is +** defined at compilation time, a temporary virtual table (see header +** comment above struct HashTableVtab) to the database schema. Both +** provide read/write access to the contents of *pHash. +** +** The third argument to this function, zName, is used as the name +** of both the scalar and, if created, the virtual table. +*/ +SQLITE_PRIVATE int sqlite3Fts3InitHashTable( + sqlite3 *db, + Fts3Hash *pHash, + const char *zName +){ + int rc = SQLITE_OK; + void *p = (void *)pHash; + const int any = SQLITE_ANY; + +#ifdef SQLITE_TEST + char *zTest = 0; + char *zTest2 = 0; + void *pdb = (void *)db; + zTest = sqlite3_mprintf("%s_test", zName); + zTest2 = sqlite3_mprintf("%s_internal_test", zName); + if( !zTest || !zTest2 ){ + rc = SQLITE_NOMEM; + } +#endif + + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0); + } + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0); + } +#ifdef SQLITE_TEST + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zTest, 2, any, p, testFunc, 0, 0); + } + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zTest, 3, any, p, testFunc, 0, 0); + } + if( SQLITE_OK==rc ){ + rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0); + } +#endif + +#ifdef SQLITE_TEST + sqlite3_free(zTest); + sqlite3_free(zTest2); +#endif + + return rc; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_tokenizer.c **************************************/ +/************** Begin file fts3_tokenizer1.c *********************************/ +/* +** 2006 Oct 10 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** Implementation of the "simple" full-text-search tokenizer. +*/ + +/* +** The code in this file is only compiled if: +** +** * The FTS3 module is being built as an extension +** (in which case SQLITE_CORE is not defined), or +** +** * The FTS3 module is being built into the core of +** SQLite (in which case SQLITE_ENABLE_FTS3 is defined). +*/ +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + + + + +typedef struct simple_tokenizer { + sqlite3_tokenizer base; + char delim[128]; /* flag ASCII delimiters */ +} simple_tokenizer; + +typedef struct simple_tokenizer_cursor { + sqlite3_tokenizer_cursor base; + const char *pInput; /* input we are tokenizing */ + int nBytes; /* size of the input */ + int iOffset; /* current position in pInput */ + int iToken; /* index of next token to be returned */ + char *pToken; /* storage for current token */ + int nTokenAllocated; /* space allocated to zToken buffer */ +} simple_tokenizer_cursor; + + +static int simpleDelim(simple_tokenizer *t, unsigned char c){ + return c<0x80 && t->delim[c]; +} +static int fts3_isalnum(int x){ + return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z'); +} + +/* +** Create a new tokenizer instance. +*/ +static int simpleCreate( + int argc, const char * const *argv, + sqlite3_tokenizer **ppTokenizer +){ + simple_tokenizer *t; + + t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t)); + if( t==NULL ) return SQLITE_NOMEM; + memset(t, 0, sizeof(*t)); + + /* TODO(shess) Delimiters need to remain the same from run to run, + ** else we need to reindex. One solution would be a meta-table to + ** track such information in the database, then we'd only want this + ** information on the initial create. + */ + if( argc>1 ){ + int i, n = (int)strlen(argv[1]); + for(i=0; i=0x80 ){ + sqlite3_free(t); + return SQLITE_ERROR; + } + t->delim[ch] = 1; + } + } else { + /* Mark non-alphanumeric ASCII characters as delimiters */ + int i; + for(i=1; i<0x80; i++){ + t->delim[i] = !fts3_isalnum(i) ? -1 : 0; + } + } + + *ppTokenizer = &t->base; + return SQLITE_OK; +} + +/* +** Destroy a tokenizer +*/ +static int simpleDestroy(sqlite3_tokenizer *pTokenizer){ + sqlite3_free(pTokenizer); + return SQLITE_OK; +} + +/* +** Prepare to begin tokenizing a particular string. The input +** string to be tokenized is pInput[0..nBytes-1]. A cursor +** used to incrementally tokenize this string is returned in +** *ppCursor. +*/ +static int simpleOpen( + sqlite3_tokenizer *pTokenizer, /* The tokenizer */ + const char *pInput, int nBytes, /* String to be tokenized */ + sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ +){ + simple_tokenizer_cursor *c; + + UNUSED_PARAMETER(pTokenizer); + + c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c)); + if( c==NULL ) return SQLITE_NOMEM; + + c->pInput = pInput; + if( pInput==0 ){ + c->nBytes = 0; + }else if( nBytes<0 ){ + c->nBytes = (int)strlen(pInput); + }else{ + c->nBytes = nBytes; + } + c->iOffset = 0; /* start tokenizing at the beginning */ + c->iToken = 0; + c->pToken = NULL; /* no space allocated, yet. */ + c->nTokenAllocated = 0; + + *ppCursor = &c->base; + return SQLITE_OK; +} + +/* +** Close a tokenization cursor previously opened by a call to +** simpleOpen() above. +*/ +static int simpleClose(sqlite3_tokenizer_cursor *pCursor){ + simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; + sqlite3_free(c->pToken); + sqlite3_free(c); + return SQLITE_OK; +} + +/* +** Extract the next token from a tokenization cursor. The cursor must +** have been opened by a prior call to simpleOpen(). +*/ +static int simpleNext( + sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */ + const char **ppToken, /* OUT: *ppToken is the token text */ + int *pnBytes, /* OUT: Number of bytes in token */ + int *piStartOffset, /* OUT: Starting offset of token */ + int *piEndOffset, /* OUT: Ending offset of token */ + int *piPosition /* OUT: Position integer of token */ +){ + simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor; + simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer; + unsigned char *p = (unsigned char *)c->pInput; + + while( c->iOffsetnBytes ){ + int iStartOffset; + + /* Scan past delimiter characters */ + while( c->iOffsetnBytes && simpleDelim(t, p[c->iOffset]) ){ + c->iOffset++; + } + + /* Count non-delimiter characters. */ + iStartOffset = c->iOffset; + while( c->iOffsetnBytes && !simpleDelim(t, p[c->iOffset]) ){ + c->iOffset++; + } + + if( c->iOffset>iStartOffset ){ + int i, n = c->iOffset-iStartOffset; + if( n>c->nTokenAllocated ){ + char *pNew; + c->nTokenAllocated = n+20; + pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated); + if( !pNew ) return SQLITE_NOMEM; + c->pToken = pNew; + } + for(i=0; ipToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch); + } + *ppToken = c->pToken; + *pnBytes = n; + *piStartOffset = iStartOffset; + *piEndOffset = c->iOffset; + *piPosition = c->iToken++; + + return SQLITE_OK; + } + } + return SQLITE_DONE; +} + +/* +** The set of routines that implement the simple tokenizer +*/ +static const sqlite3_tokenizer_module simpleTokenizerModule = { + 0, + simpleCreate, + simpleDestroy, + simpleOpen, + simpleClose, + simpleNext, +}; + +/* +** Allocate a new simple tokenizer. Return a pointer to the new +** tokenizer in *ppModule +*/ +SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule( + sqlite3_tokenizer_module const**ppModule +){ + *ppModule = &simpleTokenizerModule; +} + +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_tokenizer1.c *************************************/ +/************** Begin file fts3_write.c **************************************/ +/* +** 2009 Oct 23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +** This file is part of the SQLite FTS3 extension module. Specifically, +** this file contains code to insert, update and delete rows from FTS3 +** tables. It also contains code to merge FTS3 b-tree segments. Some +** of the sub-routines used to merge segments are also used by the query +** code in fts3.c. +*/ + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + + +/* +** When full-text index nodes are loaded from disk, the buffer that they +** are loaded into has the following number of bytes of padding at the end +** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer +** of 920 bytes is allocated for it. +** +** This means that if we have a pointer into a buffer containing node data, +** it is always safe to read up to two varints from it without risking an +** overread, even if the node data is corrupted. +*/ +#define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2) + +typedef struct PendingList PendingList; +typedef struct SegmentNode SegmentNode; +typedef struct SegmentWriter SegmentWriter; + +/* +** Data structure used while accumulating terms in the pending-terms hash +** table. The hash table entry maps from term (a string) to a malloc'd +** instance of this structure. +*/ +struct PendingList { + int nData; + char *aData; + int nSpace; + sqlite3_int64 iLastDocid; + sqlite3_int64 iLastCol; + sqlite3_int64 iLastPos; +}; + + +/* +** Each cursor has a (possibly empty) linked list of the following objects. +*/ +struct Fts3DeferredToken { + Fts3PhraseToken *pToken; /* Pointer to corresponding expr token */ + int iCol; /* Column token must occur in */ + Fts3DeferredToken *pNext; /* Next in list of deferred tokens */ + PendingList *pList; /* Doclist is assembled here */ +}; + +/* +** An instance of this structure is used to iterate through the terms on +** a contiguous set of segment b-tree leaf nodes. Although the details of +** this structure are only manipulated by code in this file, opaque handles +** of type Fts3SegReader* are also used by code in fts3.c to iterate through +** terms when querying the full-text index. See functions: +** +** sqlite3Fts3SegReaderNew() +** sqlite3Fts3SegReaderFree() +** sqlite3Fts3SegReaderCost() +** sqlite3Fts3SegReaderIterate() +** +** Methods used to manipulate Fts3SegReader structures: +** +** fts3SegReaderNext() +** fts3SegReaderFirstDocid() +** fts3SegReaderNextDocid() +*/ +struct Fts3SegReader { + int iIdx; /* Index within level, or 0x7FFFFFFF for PT */ + + sqlite3_int64 iStartBlock; /* Rowid of first leaf block to traverse */ + sqlite3_int64 iLeafEndBlock; /* Rowid of final leaf block to traverse */ + sqlite3_int64 iEndBlock; /* Rowid of final block in segment (or 0) */ + sqlite3_int64 iCurrentBlock; /* Current leaf block (or 0) */ + + char *aNode; /* Pointer to node data (or NULL) */ + int nNode; /* Size of buffer at aNode (or 0) */ + Fts3HashElem **ppNextElem; + + /* Variables set by fts3SegReaderNext(). These may be read directly + ** by the caller. They are valid from the time SegmentReaderNew() returns + ** until SegmentReaderNext() returns something other than SQLITE_OK + ** (i.e. SQLITE_DONE). + */ + int nTerm; /* Number of bytes in current term */ + char *zTerm; /* Pointer to current term */ + int nTermAlloc; /* Allocated size of zTerm buffer */ + char *aDoclist; /* Pointer to doclist of current entry */ + int nDoclist; /* Size of doclist in current entry */ + + /* The following variables are used to iterate through the current doclist */ + char *pOffsetList; + sqlite3_int64 iDocid; +}; + +#define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0) +#define fts3SegReaderIsRootOnly(p) ((p)->aNode==(char *)&(p)[1]) + +/* +** An instance of this structure is used to create a segment b-tree in the +** database. The internal details of this type are only accessed by the +** following functions: +** +** fts3SegWriterAdd() +** fts3SegWriterFlush() +** fts3SegWriterFree() +*/ +struct SegmentWriter { + SegmentNode *pTree; /* Pointer to interior tree structure */ + sqlite3_int64 iFirst; /* First slot in %_segments written */ + sqlite3_int64 iFree; /* Next free slot in %_segments */ + char *zTerm; /* Pointer to previous term buffer */ + int nTerm; /* Number of bytes in zTerm */ + int nMalloc; /* Size of malloc'd buffer at zMalloc */ + char *zMalloc; /* Malloc'd space (possibly) used for zTerm */ + int nSize; /* Size of allocation at aData */ + int nData; /* Bytes of data in aData */ + char *aData; /* Pointer to block from malloc() */ +}; + +/* +** Type SegmentNode is used by the following three functions to create +** the interior part of the segment b+-tree structures (everything except +** the leaf nodes). These functions and type are only ever used by code +** within the fts3SegWriterXXX() family of functions described above. +** +** fts3NodeAddTerm() +** fts3NodeWrite() +** fts3NodeFree() +*/ +struct SegmentNode { + SegmentNode *pParent; /* Parent node (or NULL for root node) */ + SegmentNode *pRight; /* Pointer to right-sibling */ + SegmentNode *pLeftmost; /* Pointer to left-most node of this depth */ + int nEntry; /* Number of terms written to node so far */ + char *zTerm; /* Pointer to previous term buffer */ + int nTerm; /* Number of bytes in zTerm */ + int nMalloc; /* Size of malloc'd buffer at zMalloc */ + char *zMalloc; /* Malloc'd space (possibly) used for zTerm */ + int nData; /* Bytes of valid data so far */ + char *aData; /* Node data */ +}; + +/* +** Valid values for the second argument to fts3SqlStmt(). +*/ +#define SQL_DELETE_CONTENT 0 +#define SQL_IS_EMPTY 1 +#define SQL_DELETE_ALL_CONTENT 2 +#define SQL_DELETE_ALL_SEGMENTS 3 +#define SQL_DELETE_ALL_SEGDIR 4 +#define SQL_DELETE_ALL_DOCSIZE 5 +#define SQL_DELETE_ALL_STAT 6 +#define SQL_SELECT_CONTENT_BY_ROWID 7 +#define SQL_NEXT_SEGMENT_INDEX 8 +#define SQL_INSERT_SEGMENTS 9 +#define SQL_NEXT_SEGMENTS_ID 10 +#define SQL_INSERT_SEGDIR 11 +#define SQL_SELECT_LEVEL 12 +#define SQL_SELECT_ALL_LEVEL 13 +#define SQL_SELECT_LEVEL_COUNT 14 +#define SQL_SELECT_SEGDIR_COUNT_MAX 15 +#define SQL_DELETE_SEGDIR_BY_LEVEL 16 +#define SQL_DELETE_SEGMENTS_RANGE 17 +#define SQL_CONTENT_INSERT 18 +#define SQL_DELETE_DOCSIZE 19 +#define SQL_REPLACE_DOCSIZE 20 +#define SQL_SELECT_DOCSIZE 21 +#define SQL_SELECT_DOCTOTAL 22 +#define SQL_REPLACE_DOCTOTAL 23 + +/* +** This function is used to obtain an SQLite prepared statement handle +** for the statement identified by the second argument. If successful, +** *pp is set to the requested statement handle and SQLITE_OK returned. +** Otherwise, an SQLite error code is returned and *pp is set to 0. +** +** If argument apVal is not NULL, then it must point to an array with +** at least as many entries as the requested statement has bound +** parameters. The values are bound to the statements parameters before +** returning. +*/ +static int fts3SqlStmt( + Fts3Table *p, /* Virtual table handle */ + int eStmt, /* One of the SQL_XXX constants above */ + sqlite3_stmt **pp, /* OUT: Statement handle */ + sqlite3_value **apVal /* Values to bind to statement */ +){ + const char *azSql[] = { +/* 0 */ "DELETE FROM %Q.'%q_content' WHERE rowid = ?", +/* 1 */ "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)", +/* 2 */ "DELETE FROM %Q.'%q_content'", +/* 3 */ "DELETE FROM %Q.'%q_segments'", +/* 4 */ "DELETE FROM %Q.'%q_segdir'", +/* 5 */ "DELETE FROM %Q.'%q_docsize'", +/* 6 */ "DELETE FROM %Q.'%q_stat'", +/* 7 */ "SELECT %s FROM %Q.'%q_content' AS x WHERE rowid=?", +/* 8 */ "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1", +/* 9 */ "INSERT INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)", +/* 10 */ "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)", +/* 11 */ "INSERT INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)", + + /* Return segments in order from oldest to newest.*/ +/* 12 */ "SELECT idx, start_block, leaves_end_block, end_block, root " + "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC", +/* 13 */ "SELECT idx, start_block, leaves_end_block, end_block, root " + "FROM %Q.'%q_segdir' ORDER BY level DESC, idx ASC", + +/* 14 */ "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?", +/* 15 */ "SELECT count(*), max(level) FROM %Q.'%q_segdir'", + +/* 16 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ?", +/* 17 */ "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?", +/* 18 */ "INSERT INTO %Q.'%q_content' VALUES(%s)", +/* 19 */ "DELETE FROM %Q.'%q_docsize' WHERE docid = ?", +/* 20 */ "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)", +/* 21 */ "SELECT size FROM %Q.'%q_docsize' WHERE docid=?", +/* 22 */ "SELECT value FROM %Q.'%q_stat' WHERE id=0", +/* 23 */ "REPLACE INTO %Q.'%q_stat' VALUES(0,?)", + }; + int rc = SQLITE_OK; + sqlite3_stmt *pStmt; + + assert( SizeofArray(azSql)==SizeofArray(p->aStmt) ); + assert( eStmt=0 ); + + pStmt = p->aStmt[eStmt]; + if( !pStmt ){ + char *zSql; + if( eStmt==SQL_CONTENT_INSERT ){ + zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist); + }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){ + zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist, p->zDb, p->zName); + }else{ + zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName); + } + if( !zSql ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL); + sqlite3_free(zSql); + assert( rc==SQLITE_OK || pStmt==0 ); + p->aStmt[eStmt] = pStmt; + } + } + if( apVal ){ + int i; + int nParam = sqlite3_bind_parameter_count(pStmt); + for(i=0; rc==SQLITE_OK && inSpace = 100; + p->aData = (char *)&p[1]; + p->nData = 0; + } + else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){ + int nNew = p->nSpace * 2; + p = sqlite3_realloc(p, sizeof(*p) + nNew); + if( !p ){ + sqlite3_free(*pp); + *pp = 0; + return SQLITE_NOMEM; + } + p->nSpace = nNew; + p->aData = (char *)&p[1]; + } + + /* Append the new serialized varint to the end of the list. */ + p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i); + p->aData[p->nData] = '\0'; + *pp = p; + return SQLITE_OK; +} + +/* +** Add a docid/column/position entry to a PendingList structure. Non-zero +** is returned if the structure is sqlite3_realloced as part of adding +** the entry. Otherwise, zero. +** +** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning. +** Zero is always returned in this case. Otherwise, if no OOM error occurs, +** it is set to SQLITE_OK. +*/ +static int fts3PendingListAppend( + PendingList **pp, /* IN/OUT: PendingList structure */ + sqlite3_int64 iDocid, /* Docid for entry to add */ + sqlite3_int64 iCol, /* Column for entry to add */ + sqlite3_int64 iPos, /* Position of term for entry to add */ + int *pRc /* OUT: Return code */ +){ + PendingList *p = *pp; + int rc = SQLITE_OK; + + assert( !p || p->iLastDocid<=iDocid ); + + if( !p || p->iLastDocid!=iDocid ){ + sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0); + if( p ){ + assert( p->nDatanSpace ); + assert( p->aData[p->nData]==0 ); + p->nData++; + } + if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){ + goto pendinglistappend_out; + } + p->iLastCol = -1; + p->iLastPos = 0; + p->iLastDocid = iDocid; + } + if( iCol>0 && p->iLastCol!=iCol ){ + if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1)) + || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol)) + ){ + goto pendinglistappend_out; + } + p->iLastCol = iCol; + p->iLastPos = 0; + } + if( iCol>=0 ){ + assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) ); + rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos); + if( rc==SQLITE_OK ){ + p->iLastPos = iPos; + } + } + + pendinglistappend_out: + *pRc = rc; + if( p!=*pp ){ + *pp = p; + return 1; + } + return 0; +} + +/* +** Tokenize the nul-terminated string zText and add all tokens to the +** pending-terms hash-table. The docid used is that currently stored in +** p->iPrevDocid, and the column is specified by argument iCol. +** +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code. +*/ +static int fts3PendingTermsAdd( + Fts3Table *p, /* Table into which text will be inserted */ + const char *zText, /* Text of document to be inserted */ + int iCol, /* Column into which text is being inserted */ + u32 *pnWord /* OUT: Number of tokens inserted */ +){ + int rc; + int iStart; + int iEnd; + int iPos; + int nWord = 0; + + char const *zToken; + int nToken; + + sqlite3_tokenizer *pTokenizer = p->pTokenizer; + sqlite3_tokenizer_module const *pModule = pTokenizer->pModule; + sqlite3_tokenizer_cursor *pCsr; + int (*xNext)(sqlite3_tokenizer_cursor *pCursor, + const char**,int*,int*,int*,int*); + + assert( pTokenizer && pModule ); + + rc = pModule->xOpen(pTokenizer, zText, -1, &pCsr); + if( rc!=SQLITE_OK ){ + return rc; + } + pCsr->pTokenizer = pTokenizer; + + xNext = pModule->xNext; + while( SQLITE_OK==rc + && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos)) + ){ + PendingList *pList; + + if( iPos>=nWord ) nWord = iPos+1; + + /* Positions cannot be negative; we use -1 as a terminator internally. + ** Tokens must have a non-zero length. + */ + if( iPos<0 || !zToken || nToken<=0 ){ + rc = SQLITE_ERROR; + break; + } + + pList = (PendingList *)fts3HashFind(&p->pendingTerms, zToken, nToken); + if( pList ){ + p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem)); + } + if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){ + if( pList==fts3HashInsert(&p->pendingTerms, zToken, nToken, pList) ){ + /* Malloc failed while inserting the new entry. This can only + ** happen if there was no previous entry for this token. + */ + assert( 0==fts3HashFind(&p->pendingTerms, zToken, nToken) ); + sqlite3_free(pList); + rc = SQLITE_NOMEM; + } + } + if( rc==SQLITE_OK ){ + p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem)); + } + } + + pModule->xClose(pCsr); + *pnWord = nWord; + return (rc==SQLITE_DONE ? SQLITE_OK : rc); +} + +/* +** Calling this function indicates that subsequent calls to +** fts3PendingTermsAdd() are to add term/position-list pairs for the +** contents of the document with docid iDocid. +*/ +static int fts3PendingTermsDocid(Fts3Table *p, sqlite_int64 iDocid){ + /* TODO(shess) Explore whether partially flushing the buffer on + ** forced-flush would provide better performance. I suspect that if + ** we ordered the doclists by size and flushed the largest until the + ** buffer was half empty, that would let the less frequent terms + ** generate longer doclists. + */ + if( iDocid<=p->iPrevDocid || p->nPendingData>p->nMaxPendingData ){ + int rc = sqlite3Fts3PendingTermsFlush(p); + if( rc!=SQLITE_OK ) return rc; + } + p->iPrevDocid = iDocid; + return SQLITE_OK; +} + +/* +** Discard the contents of the pending-terms hash table. +*/ +SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){ + Fts3HashElem *pElem; + for(pElem=fts3HashFirst(&p->pendingTerms); pElem; pElem=fts3HashNext(pElem)){ + sqlite3_free(fts3HashData(pElem)); + } + fts3HashClear(&p->pendingTerms); + p->nPendingData = 0; +} + +/* +** This function is called by the xUpdate() method as part of an INSERT +** operation. It adds entries for each term in the new record to the +** pendingTerms hash table. +** +** Argument apVal is the same as the similarly named argument passed to +** fts3InsertData(). Parameter iDocid is the docid of the new row. +*/ +static int fts3InsertTerms(Fts3Table *p, sqlite3_value **apVal, u32 *aSz){ + int i; /* Iterator variable */ + for(i=2; inColumn+2; i++){ + const char *zText = (const char *)sqlite3_value_text(apVal[i]); + if( zText ){ + int rc = fts3PendingTermsAdd(p, zText, i-2, &aSz[i-2]); + if( rc!=SQLITE_OK ){ + return rc; + } + } + aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]); + } + return SQLITE_OK; +} + +/* +** This function is called by the xUpdate() method for an INSERT operation. +** The apVal parameter is passed a copy of the apVal argument passed by +** SQLite to the xUpdate() method. i.e: +** +** apVal[0] Not used for INSERT. +** apVal[1] rowid +** apVal[2] Left-most user-defined column +** ... +** apVal[p->nColumn+1] Right-most user-defined column +** apVal[p->nColumn+2] Hidden column with same name as table +** apVal[p->nColumn+3] Hidden "docid" column (alias for rowid) +*/ +static int fts3InsertData( + Fts3Table *p, /* Full-text table */ + sqlite3_value **apVal, /* Array of values to insert */ + sqlite3_int64 *piDocid /* OUT: Docid for row just inserted */ +){ + int rc; /* Return code */ + sqlite3_stmt *pContentInsert; /* INSERT INTO %_content VALUES(...) */ + + /* Locate the statement handle used to insert data into the %_content + ** table. The SQL for this statement is: + ** + ** INSERT INTO %_content VALUES(?, ?, ?, ...) + ** + ** The statement features N '?' variables, where N is the number of user + ** defined columns in the FTS3 table, plus one for the docid field. + */ + rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]); + if( rc!=SQLITE_OK ){ + return rc; + } + + /* There is a quirk here. The users INSERT statement may have specified + ** a value for the "rowid" field, for the "docid" field, or for both. + ** Which is a problem, since "rowid" and "docid" are aliases for the + ** same value. For example: + ** + ** INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2); + ** + ** In FTS3, this is an error. It is an error to specify non-NULL values + ** for both docid and some other rowid alias. + */ + if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){ + if( SQLITE_NULL==sqlite3_value_type(apVal[0]) + && SQLITE_NULL!=sqlite3_value_type(apVal[1]) + ){ + /* A rowid/docid conflict. */ + return SQLITE_ERROR; + } + rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]); + if( rc!=SQLITE_OK ) return rc; + } + + /* Execute the statement to insert the record. Set *piDocid to the + ** new docid value. + */ + sqlite3_step(pContentInsert); + rc = sqlite3_reset(pContentInsert); + + *piDocid = sqlite3_last_insert_rowid(p->db); + return rc; +} + + + +/* +** Remove all data from the FTS3 table. Clear the hash table containing +** pending terms. +*/ +static int fts3DeleteAll(Fts3Table *p){ + int rc = SQLITE_OK; /* Return code */ + + /* Discard the contents of the pending-terms hash table. */ + sqlite3Fts3PendingTermsClear(p); + + /* Delete everything from the %_content, %_segments and %_segdir tables. */ + fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0); + fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0); + fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0); + if( p->bHasDocsize ){ + fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0); + } + if( p->bHasStat ){ + fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0); + } + return rc; +} + +/* +** The first element in the apVal[] array is assumed to contain the docid +** (an integer) of a row about to be deleted. Remove all terms from the +** full-text index. +*/ +static void fts3DeleteTerms( + int *pRC, /* Result code */ + Fts3Table *p, /* The FTS table to delete from */ + sqlite3_value **apVal, /* apVal[] contains the docid to be deleted */ + u32 *aSz /* Sizes of deleted document written here */ +){ + int rc; + sqlite3_stmt *pSelect; + + if( *pRC ) return; + rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, apVal); + if( rc==SQLITE_OK ){ + if( SQLITE_ROW==sqlite3_step(pSelect) ){ + int i; + for(i=1; i<=p->nColumn; i++){ + const char *zText = (const char *)sqlite3_column_text(pSelect, i); + rc = fts3PendingTermsAdd(p, zText, -1, &aSz[i-1]); + if( rc!=SQLITE_OK ){ + sqlite3_reset(pSelect); + *pRC = rc; + return; + } + aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i); + } + } + rc = sqlite3_reset(pSelect); + }else{ + sqlite3_reset(pSelect); + } + *pRC = rc; +} + +/* +** Forward declaration to account for the circular dependency between +** functions fts3SegmentMerge() and fts3AllocateSegdirIdx(). +*/ +static int fts3SegmentMerge(Fts3Table *, int); + +/* +** This function allocates a new level iLevel index in the segdir table. +** Usually, indexes are allocated within a level sequentially starting +** with 0, so the allocated index is one greater than the value returned +** by: +** +** SELECT max(idx) FROM %_segdir WHERE level = :iLevel +** +** However, if there are already FTS3_MERGE_COUNT indexes at the requested +** level, they are merged into a single level (iLevel+1) segment and the +** allocated index is 0. +** +** If successful, *piIdx is set to the allocated index slot and SQLITE_OK +** returned. Otherwise, an SQLite error code is returned. +*/ +static int fts3AllocateSegdirIdx(Fts3Table *p, int iLevel, int *piIdx){ + int rc; /* Return Code */ + sqlite3_stmt *pNextIdx; /* Query for next idx at level iLevel */ + int iNext = 0; /* Result of query pNextIdx */ + + /* Set variable iNext to the next available segdir index at level iLevel. */ + rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int(pNextIdx, 1, iLevel); + if( SQLITE_ROW==sqlite3_step(pNextIdx) ){ + iNext = sqlite3_column_int(pNextIdx, 0); + } + rc = sqlite3_reset(pNextIdx); + } + + if( rc==SQLITE_OK ){ + /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already + ** full, merge all segments in level iLevel into a single iLevel+1 + ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise, + ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext. + */ + if( iNext>=FTS3_MERGE_COUNT ){ + rc = fts3SegmentMerge(p, iLevel); + *piIdx = 0; + }else{ + *piIdx = iNext; + } + } + + return rc; +} + +/* +** The %_segments table is declared as follows: +** +** CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB) +** +** This function reads data from a single row of the %_segments table. The +** specific row is identified by the iBlockid parameter. If paBlob is not +** NULL, then a buffer is allocated using sqlite3_malloc() and populated +** with the contents of the blob stored in the "block" column of the +** identified table row is. Whether or not paBlob is NULL, *pnBlob is set +** to the size of the blob in bytes before returning. +** +** If an error occurs, or the table does not contain the specified row, +** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If +** paBlob is non-NULL, then it is the responsibility of the caller to +** eventually free the returned buffer. +** +** This function may leave an open sqlite3_blob* handle in the +** Fts3Table.pSegments variable. This handle is reused by subsequent calls +** to this function. The handle may be closed by calling the +** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy +** performance improvement, but the blob handle should always be closed +** before control is returned to the user (to prevent a lock being held +** on the database file for longer than necessary). Thus, any virtual table +** method (xFilter etc.) that may directly or indirectly call this function +** must call sqlite3Fts3SegmentsClose() before returning. +*/ +SQLITE_PRIVATE int sqlite3Fts3ReadBlock( + Fts3Table *p, /* FTS3 table handle */ + sqlite3_int64 iBlockid, /* Access the row with blockid=$iBlockid */ + char **paBlob, /* OUT: Blob data in malloc'd buffer */ + int *pnBlob /* OUT: Size of blob data */ +){ + int rc; /* Return code */ + + /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */ + assert( pnBlob); + + if( p->pSegments ){ + rc = sqlite3_blob_reopen(p->pSegments, iBlockid); + }else{ + if( 0==p->zSegmentsTbl ){ + p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName); + if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM; + } + rc = sqlite3_blob_open( + p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments + ); + } + + if( rc==SQLITE_OK ){ + int nByte = sqlite3_blob_bytes(p->pSegments); + if( paBlob ){ + char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING); + if( !aByte ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0); + memset(&aByte[nByte], 0, FTS3_NODE_PADDING); + if( rc!=SQLITE_OK ){ + sqlite3_free(aByte); + aByte = 0; + } + } + *paBlob = aByte; + } + *pnBlob = nByte; + } + + return rc; +} + +/* +** Close the blob handle at p->pSegments, if it is open. See comments above +** the sqlite3Fts3ReadBlock() function for details. +*/ +SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){ + sqlite3_blob_close(p->pSegments); + p->pSegments = 0; +} + +/* +** Move the iterator passed as the first argument to the next term in the +** segment. If successful, SQLITE_OK is returned. If there is no next term, +** SQLITE_DONE. Otherwise, an SQLite error code. +*/ +static int fts3SegReaderNext(Fts3Table *p, Fts3SegReader *pReader){ + char *pNext; /* Cursor variable */ + int nPrefix; /* Number of bytes in term prefix */ + int nSuffix; /* Number of bytes in term suffix */ + + if( !pReader->aDoclist ){ + pNext = pReader->aNode; + }else{ + pNext = &pReader->aDoclist[pReader->nDoclist]; + } + + if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){ + int rc; /* Return code from Fts3ReadBlock() */ + + if( fts3SegReaderIsPending(pReader) ){ + Fts3HashElem *pElem = *(pReader->ppNextElem); + if( pElem==0 ){ + pReader->aNode = 0; + }else{ + PendingList *pList = (PendingList *)fts3HashData(pElem); + pReader->zTerm = (char *)fts3HashKey(pElem); + pReader->nTerm = fts3HashKeysize(pElem); + pReader->nNode = pReader->nDoclist = pList->nData + 1; + pReader->aNode = pReader->aDoclist = pList->aData; + pReader->ppNextElem++; + assert( pReader->aNode ); + } + return SQLITE_OK; + } + + if( !fts3SegReaderIsRootOnly(pReader) ){ + sqlite3_free(pReader->aNode); + } + pReader->aNode = 0; + + /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf + ** blocks have already been traversed. */ + assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock ); + if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){ + return SQLITE_OK; + } + + rc = sqlite3Fts3ReadBlock( + p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode + ); + if( rc!=SQLITE_OK ) return rc; + pNext = pReader->aNode; + } + + /* Because of the FTS3_NODE_PADDING bytes of padding, the following is + ** safe (no risk of overread) even if the node data is corrupted. + */ + pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix); + pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix); + if( nPrefix<0 || nSuffix<=0 + || &pNext[nSuffix]>&pReader->aNode[pReader->nNode] + ){ + return SQLITE_CORRUPT; + } + + if( nPrefix+nSuffix>pReader->nTermAlloc ){ + int nNew = (nPrefix+nSuffix)*2; + char *zNew = sqlite3_realloc(pReader->zTerm, nNew); + if( !zNew ){ + return SQLITE_NOMEM; + } + pReader->zTerm = zNew; + pReader->nTermAlloc = nNew; + } + memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix); + pReader->nTerm = nPrefix+nSuffix; + pNext += nSuffix; + pNext += sqlite3Fts3GetVarint32(pNext, &pReader->nDoclist); + pReader->aDoclist = pNext; + pReader->pOffsetList = 0; + + /* Check that the doclist does not appear to extend past the end of the + ** b-tree node. And that the final byte of the doclist is 0x00. If either + ** of these statements is untrue, then the data structure is corrupt. + */ + if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode] + || pReader->aDoclist[pReader->nDoclist-1] + ){ + return SQLITE_CORRUPT; + } + return SQLITE_OK; +} + +/* +** Set the SegReader to point to the first docid in the doclist associated +** with the current term. +*/ +static void fts3SegReaderFirstDocid(Fts3SegReader *pReader){ + int n; + assert( pReader->aDoclist ); + assert( !pReader->pOffsetList ); + n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid); + pReader->pOffsetList = &pReader->aDoclist[n]; +} + +/* +** Advance the SegReader to point to the next docid in the doclist +** associated with the current term. +** +** If arguments ppOffsetList and pnOffsetList are not NULL, then +** *ppOffsetList is set to point to the first column-offset list +** in the doclist entry (i.e. immediately past the docid varint). +** *pnOffsetList is set to the length of the set of column-offset +** lists, not including the nul-terminator byte. For example: +*/ +static void fts3SegReaderNextDocid( + Fts3SegReader *pReader, + char **ppOffsetList, + int *pnOffsetList +){ + char *p = pReader->pOffsetList; + char c = 0; + + /* Pointer p currently points at the first byte of an offset list. The + ** following two lines advance it to point one byte past the end of + ** the same offset list. + */ + while( *p | c ) c = *p++ & 0x80; + p++; + + /* If required, populate the output variables with a pointer to and the + ** size of the previous offset-list. + */ + if( ppOffsetList ){ + *ppOffsetList = pReader->pOffsetList; + *pnOffsetList = (int)(p - pReader->pOffsetList - 1); + } + + /* If there are no more entries in the doclist, set pOffsetList to + ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and + ** Fts3SegReader.pOffsetList to point to the next offset list before + ** returning. + */ + if( p>=&pReader->aDoclist[pReader->nDoclist] ){ + pReader->pOffsetList = 0; + }else{ + sqlite3_int64 iDelta; + pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta); + pReader->iDocid += iDelta; + } +} + +/* +** This function is called to estimate the amount of data that will be +** loaded from the disk If SegReaderIterate() is called on this seg-reader, +** in units of average document size. +** +** This can be used as follows: If the caller has a small doclist that +** contains references to N documents, and is considering merging it with +** a large doclist (size X "average documents"), it may opt not to load +** the large doclist if X>N. +*/ +SQLITE_PRIVATE int sqlite3Fts3SegReaderCost( + Fts3Cursor *pCsr, /* FTS3 cursor handle */ + Fts3SegReader *pReader, /* Segment-reader handle */ + int *pnCost /* IN/OUT: Number of bytes read */ +){ + Fts3Table *p = (Fts3Table*)pCsr->base.pVtab; + int rc = SQLITE_OK; /* Return code */ + int nCost = 0; /* Cost in bytes to return */ + int pgsz = p->nPgsz; /* Database page size */ + + /* If this seg-reader is reading the pending-terms table, or if all data + ** for the segment is stored on the root page of the b-tree, then the cost + ** is zero. In this case all required data is already in main memory. + */ + if( p->bHasStat + && !fts3SegReaderIsPending(pReader) + && !fts3SegReaderIsRootOnly(pReader) + ){ + int nBlob = 0; + sqlite3_int64 iBlock; + + if( pCsr->nRowAvg==0 ){ + /* The average document size, which is required to calculate the cost + ** of each doclist, has not yet been determined. Read the required + ** data from the %_stat table to calculate it. + ** + ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3 + ** varints, where nCol is the number of columns in the FTS3 table. + ** The first varint is the number of documents currently stored in + ** the table. The following nCol varints contain the total amount of + ** data stored in all rows of each column of the table, from left + ** to right. + */ + sqlite3_stmt *pStmt; + sqlite3_int64 nDoc = 0; + sqlite3_int64 nByte = 0; + const char *pEnd; + const char *a; + + rc = sqlite3Fts3SelectDoctotal(p, &pStmt); + if( rc!=SQLITE_OK ) return rc; + a = sqlite3_column_blob(pStmt, 0); + assert( a ); + + pEnd = &a[sqlite3_column_bytes(pStmt, 0)]; + a += sqlite3Fts3GetVarint(a, &nDoc); + while( anRowAvg = (int)(((nByte / nDoc) + pgsz) / pgsz); + assert( pCsr->nRowAvg>0 ); + rc = sqlite3_reset(pStmt); + if( rc!=SQLITE_OK ) return rc; + } + + /* Assume that a blob flows over onto overflow pages if it is larger + ** than (pgsz-35) bytes in size (the file-format documentation + ** confirms this). + */ + for(iBlock=pReader->iStartBlock; iBlock<=pReader->iLeafEndBlock; iBlock++){ + rc = sqlite3Fts3ReadBlock(p, iBlock, 0, &nBlob); + if( rc!=SQLITE_OK ) break; + if( (nBlob+35)>pgsz ){ + int nOvfl = (nBlob + 34)/pgsz; + nCost += ((nOvfl + pCsr->nRowAvg - 1)/pCsr->nRowAvg); + } + } + } + + *pnCost += nCost; + return rc; +} + +/* +** Free all allocations associated with the iterator passed as the +** second argument. +*/ +SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){ + if( pReader && !fts3SegReaderIsPending(pReader) ){ + sqlite3_free(pReader->zTerm); + if( !fts3SegReaderIsRootOnly(pReader) ){ + sqlite3_free(pReader->aNode); + } + } + sqlite3_free(pReader); +} + +/* +** Allocate a new SegReader object. +*/ +SQLITE_PRIVATE int sqlite3Fts3SegReaderNew( + int iAge, /* Segment "age". */ + sqlite3_int64 iStartLeaf, /* First leaf to traverse */ + sqlite3_int64 iEndLeaf, /* Final leaf to traverse */ + sqlite3_int64 iEndBlock, /* Final block of segment */ + const char *zRoot, /* Buffer containing root node */ + int nRoot, /* Size of buffer containing root node */ + Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */ +){ + int rc = SQLITE_OK; /* Return code */ + Fts3SegReader *pReader; /* Newly allocated SegReader object */ + int nExtra = 0; /* Bytes to allocate segment root node */ + + assert( iStartLeaf<=iEndLeaf ); + if( iStartLeaf==0 ){ + nExtra = nRoot + FTS3_NODE_PADDING; + } + + pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra); + if( !pReader ){ + return SQLITE_NOMEM; + } + memset(pReader, 0, sizeof(Fts3SegReader)); + pReader->iIdx = iAge; + pReader->iStartBlock = iStartLeaf; + pReader->iLeafEndBlock = iEndLeaf; + pReader->iEndBlock = iEndBlock; + + if( nExtra ){ + /* The entire segment is stored in the root node. */ + pReader->aNode = (char *)&pReader[1]; + pReader->nNode = nRoot; + memcpy(pReader->aNode, zRoot, nRoot); + memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING); + }else{ + pReader->iCurrentBlock = iStartLeaf-1; + } + + if( rc==SQLITE_OK ){ + *ppReader = pReader; + }else{ + sqlite3Fts3SegReaderFree(pReader); + } + return rc; +} + +/* +** This is a comparison function used as a qsort() callback when sorting +** an array of pending terms by term. This occurs as part of flushing +** the contents of the pending-terms hash table to the database. +*/ +static int fts3CompareElemByTerm(const void *lhs, const void *rhs){ + char *z1 = fts3HashKey(*(Fts3HashElem **)lhs); + char *z2 = fts3HashKey(*(Fts3HashElem **)rhs); + int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs); + int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs); + + int n = (n1pendingTerms); pE; pE=fts3HashNext(pE)){ + char *zKey = (char *)fts3HashKey(pE); + int nKey = fts3HashKeysize(pE); + if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){ + if( nElem==nAlloc ){ + Fts3HashElem **aElem2; + nAlloc += 16; + aElem2 = (Fts3HashElem **)sqlite3_realloc( + aElem, nAlloc*sizeof(Fts3HashElem *) + ); + if( !aElem2 ){ + rc = SQLITE_NOMEM; + nElem = 0; + break; + } + aElem = aElem2; + } + aElem[nElem++] = pE; + } + } + + /* If more than one term matches the prefix, sort the Fts3HashElem + ** objects in term order using qsort(). This uses the same comparison + ** callback as is used when flushing terms to disk. + */ + if( nElem>1 ){ + qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm); + } + + }else{ + Fts3HashElem *pE = fts3HashFindElem(&p->pendingTerms, zTerm, nTerm); + if( pE ){ + aElem = &pE; + nElem = 1; + } + } + + if( nElem>0 ){ + int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *); + pReader = (Fts3SegReader *)sqlite3_malloc(nByte); + if( !pReader ){ + rc = SQLITE_NOMEM; + }else{ + memset(pReader, 0, nByte); + pReader->iIdx = 0x7FFFFFFF; + pReader->ppNextElem = (Fts3HashElem **)&pReader[1]; + memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *)); + } + } + + if( isPrefix ){ + sqlite3_free(aElem); + } + *ppReader = pReader; + return rc; +} + +/* +** Compare the entries pointed to by two Fts3SegReader structures. +** Comparison is as follows: +** +** 1) EOF is greater than not EOF. +** +** 2) The current terms (if any) are compared using memcmp(). If one +** term is a prefix of another, the longer term is considered the +** larger. +** +** 3) By segment age. An older segment is considered larger. +*/ +static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){ + int rc; + if( pLhs->aNode && pRhs->aNode ){ + int rc2 = pLhs->nTerm - pRhs->nTerm; + if( rc2<0 ){ + rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm); + }else{ + rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm); + } + if( rc==0 ){ + rc = rc2; + } + }else{ + rc = (pLhs->aNode==0) - (pRhs->aNode==0); + } + if( rc==0 ){ + rc = pRhs->iIdx - pLhs->iIdx; + } + assert( rc!=0 ); + return rc; +} + +/* +** A different comparison function for SegReader structures. In this +** version, it is assumed that each SegReader points to an entry in +** a doclist for identical terms. Comparison is made as follows: +** +** 1) EOF (end of doclist in this case) is greater than not EOF. +** +** 2) By current docid. +** +** 3) By segment age. An older segment is considered larger. +*/ +static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){ + int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0); + if( rc==0 ){ + if( pLhs->iDocid==pRhs->iDocid ){ + rc = pRhs->iIdx - pLhs->iIdx; + }else{ + rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1; + } + } + assert( pLhs->aNode && pRhs->aNode ); + return rc; +} + +/* +** Compare the term that the Fts3SegReader object passed as the first argument +** points to with the term specified by arguments zTerm and nTerm. +** +** If the pSeg iterator is already at EOF, return 0. Otherwise, return +** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are +** equal, or +ve if the pSeg term is greater than zTerm/nTerm. +*/ +static int fts3SegReaderTermCmp( + Fts3SegReader *pSeg, /* Segment reader object */ + const char *zTerm, /* Term to compare to */ + int nTerm /* Size of term zTerm in bytes */ +){ + int res = 0; + if( pSeg->aNode ){ + if( pSeg->nTerm>nTerm ){ + res = memcmp(pSeg->zTerm, zTerm, nTerm); + }else{ + res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm); + } + if( res==0 ){ + res = pSeg->nTerm-nTerm; + } + } + return res; +} + +/* +** Argument apSegment is an array of nSegment elements. It is known that +** the final (nSegment-nSuspect) members are already in sorted order +** (according to the comparison function provided). This function shuffles +** the array around until all entries are in sorted order. +*/ +static void fts3SegReaderSort( + Fts3SegReader **apSegment, /* Array to sort entries of */ + int nSegment, /* Size of apSegment array */ + int nSuspect, /* Unsorted entry count */ + int (*xCmp)(Fts3SegReader *, Fts3SegReader *) /* Comparison function */ +){ + int i; /* Iterator variable */ + + assert( nSuspect<=nSegment ); + + if( nSuspect==nSegment ) nSuspect--; + for(i=nSuspect-1; i>=0; i--){ + int j; + for(j=i; j<(nSegment-1); j++){ + Fts3SegReader *pTmp; + if( xCmp(apSegment[j], apSegment[j+1])<0 ) break; + pTmp = apSegment[j+1]; + apSegment[j+1] = apSegment[j]; + apSegment[j] = pTmp; + } + } + +#ifndef NDEBUG + /* Check that the list really is sorted now. */ + for(i=0; i<(nSuspect-1); i++){ + assert( xCmp(apSegment[i], apSegment[i+1])<0 ); + } +#endif +} + +/* +** Insert a record into the %_segments table. +*/ +static int fts3WriteSegment( + Fts3Table *p, /* Virtual table handle */ + sqlite3_int64 iBlock, /* Block id for new block */ + char *z, /* Pointer to buffer containing block data */ + int n /* Size of buffer z in bytes */ +){ + sqlite3_stmt *pStmt; + int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pStmt, 1, iBlock); + sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC); + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + } + return rc; +} + +/* +** Insert a record into the %_segdir table. +*/ +static int fts3WriteSegdir( + Fts3Table *p, /* Virtual table handle */ + int iLevel, /* Value for "level" field */ + int iIdx, /* Value for "idx" field */ + sqlite3_int64 iStartBlock, /* Value for "start_block" field */ + sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */ + sqlite3_int64 iEndBlock, /* Value for "end_block" field */ + char *zRoot, /* Blob value for "root" field */ + int nRoot /* Number of bytes in buffer zRoot */ +){ + sqlite3_stmt *pStmt; + int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int(pStmt, 1, iLevel); + sqlite3_bind_int(pStmt, 2, iIdx); + sqlite3_bind_int64(pStmt, 3, iStartBlock); + sqlite3_bind_int64(pStmt, 4, iLeafEndBlock); + sqlite3_bind_int64(pStmt, 5, iEndBlock); + sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC); + sqlite3_step(pStmt); + rc = sqlite3_reset(pStmt); + } + return rc; +} + +/* +** Return the size of the common prefix (if any) shared by zPrev and +** zNext, in bytes. For example, +** +** fts3PrefixCompress("abc", 3, "abcdef", 6) // returns 3 +** fts3PrefixCompress("abX", 3, "abcdef", 6) // returns 2 +** fts3PrefixCompress("abX", 3, "Xbcdef", 6) // returns 0 +*/ +static int fts3PrefixCompress( + const char *zPrev, /* Buffer containing previous term */ + int nPrev, /* Size of buffer zPrev in bytes */ + const char *zNext, /* Buffer containing next term */ + int nNext /* Size of buffer zNext in bytes */ +){ + int n; + UNUSED_PARAMETER(nNext); + for(n=0; nnData; /* Current size of node in bytes */ + int nReq = nData; /* Required space after adding zTerm */ + int nPrefix; /* Number of bytes of prefix compression */ + int nSuffix; /* Suffix length */ + + nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm); + nSuffix = nTerm-nPrefix; + + nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix; + if( nReq<=p->nNodeSize || !pTree->zTerm ){ + + if( nReq>p->nNodeSize ){ + /* An unusual case: this is the first term to be added to the node + ** and the static node buffer (p->nNodeSize bytes) is not large + ** enough. Use a separately malloced buffer instead This wastes + ** p->nNodeSize bytes, but since this scenario only comes about when + ** the database contain two terms that share a prefix of almost 2KB, + ** this is not expected to be a serious problem. + */ + assert( pTree->aData==(char *)&pTree[1] ); + pTree->aData = (char *)sqlite3_malloc(nReq); + if( !pTree->aData ){ + return SQLITE_NOMEM; + } + } + + if( pTree->zTerm ){ + /* There is no prefix-length field for first term in a node */ + nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix); + } + + nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix); + memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix); + pTree->nData = nData + nSuffix; + pTree->nEntry++; + + if( isCopyTerm ){ + if( pTree->nMalloczMalloc, nTerm*2); + if( !zNew ){ + return SQLITE_NOMEM; + } + pTree->nMalloc = nTerm*2; + pTree->zMalloc = zNew; + } + pTree->zTerm = pTree->zMalloc; + memcpy(pTree->zTerm, zTerm, nTerm); + pTree->nTerm = nTerm; + }else{ + pTree->zTerm = (char *)zTerm; + pTree->nTerm = nTerm; + } + return SQLITE_OK; + } + } + + /* If control flows to here, it was not possible to append zTerm to the + ** current node. Create a new node (a right-sibling of the current node). + ** If this is the first node in the tree, the term is added to it. + ** + ** Otherwise, the term is not added to the new node, it is left empty for + ** now. Instead, the term is inserted into the parent of pTree. If pTree + ** has no parent, one is created here. + */ + pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize); + if( !pNew ){ + return SQLITE_NOMEM; + } + memset(pNew, 0, sizeof(SegmentNode)); + pNew->nData = 1 + FTS3_VARINT_MAX; + pNew->aData = (char *)&pNew[1]; + + if( pTree ){ + SegmentNode *pParent = pTree->pParent; + rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm); + if( pTree->pParent==0 ){ + pTree->pParent = pParent; + } + pTree->pRight = pNew; + pNew->pLeftmost = pTree->pLeftmost; + pNew->pParent = pParent; + pNew->zMalloc = pTree->zMalloc; + pNew->nMalloc = pTree->nMalloc; + pTree->zMalloc = 0; + }else{ + pNew->pLeftmost = pNew; + rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm); + } + + *ppTree = pNew; + return rc; +} + +/* +** Helper function for fts3NodeWrite(). +*/ +static int fts3TreeFinishNode( + SegmentNode *pTree, + int iHeight, + sqlite3_int64 iLeftChild +){ + int nStart; + assert( iHeight>=1 && iHeight<128 ); + nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild); + pTree->aData[nStart] = (char)iHeight; + sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild); + return nStart; +} + +/* +** Write the buffer for the segment node pTree and all of its peers to the +** database. Then call this function recursively to write the parent of +** pTree and its peers to the database. +** +** Except, if pTree is a root node, do not write it to the database. Instead, +** set output variables *paRoot and *pnRoot to contain the root node. +** +** If successful, SQLITE_OK is returned and output variable *piLast is +** set to the largest blockid written to the database (or zero if no +** blocks were written to the db). Otherwise, an SQLite error code is +** returned. +*/ +static int fts3NodeWrite( + Fts3Table *p, /* Virtual table handle */ + SegmentNode *pTree, /* SegmentNode handle */ + int iHeight, /* Height of this node in tree */ + sqlite3_int64 iLeaf, /* Block id of first leaf node */ + sqlite3_int64 iFree, /* Block id of next free slot in %_segments */ + sqlite3_int64 *piLast, /* OUT: Block id of last entry written */ + char **paRoot, /* OUT: Data for root node */ + int *pnRoot /* OUT: Size of root node in bytes */ +){ + int rc = SQLITE_OK; + + if( !pTree->pParent ){ + /* Root node of the tree. */ + int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf); + *piLast = iFree-1; + *pnRoot = pTree->nData - nStart; + *paRoot = &pTree->aData[nStart]; + }else{ + SegmentNode *pIter; + sqlite3_int64 iNextFree = iFree; + sqlite3_int64 iNextLeaf = iLeaf; + for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){ + int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf); + int nWrite = pIter->nData - nStart; + + rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite); + iNextFree++; + iNextLeaf += (pIter->nEntry+1); + } + if( rc==SQLITE_OK ){ + assert( iNextLeaf==iFree ); + rc = fts3NodeWrite( + p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot + ); + } + } + + return rc; +} + +/* +** Free all memory allocations associated with the tree pTree. +*/ +static void fts3NodeFree(SegmentNode *pTree){ + if( pTree ){ + SegmentNode *p = pTree->pLeftmost; + fts3NodeFree(p->pParent); + while( p ){ + SegmentNode *pRight = p->pRight; + if( p->aData!=(char *)&p[1] ){ + sqlite3_free(p->aData); + } + assert( pRight==0 || p->zMalloc==0 ); + sqlite3_free(p->zMalloc); + sqlite3_free(p); + p = pRight; + } + } +} + +/* +** Add a term to the segment being constructed by the SegmentWriter object +** *ppWriter. When adding the first term to a segment, *ppWriter should +** be passed NULL. This function will allocate a new SegmentWriter object +** and return it via the input/output variable *ppWriter in this case. +** +** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code. +*/ +static int fts3SegWriterAdd( + Fts3Table *p, /* Virtual table handle */ + SegmentWriter **ppWriter, /* IN/OUT: SegmentWriter handle */ + int isCopyTerm, /* True if buffer zTerm must be copied */ + const char *zTerm, /* Pointer to buffer containing term */ + int nTerm, /* Size of term in bytes */ + const char *aDoclist, /* Pointer to buffer containing doclist */ + int nDoclist /* Size of doclist in bytes */ +){ + int nPrefix; /* Size of term prefix in bytes */ + int nSuffix; /* Size of term suffix in bytes */ + int nReq; /* Number of bytes required on leaf page */ + int nData; + SegmentWriter *pWriter = *ppWriter; + + if( !pWriter ){ + int rc; + sqlite3_stmt *pStmt; + + /* Allocate the SegmentWriter structure */ + pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter)); + if( !pWriter ) return SQLITE_NOMEM; + memset(pWriter, 0, sizeof(SegmentWriter)); + *ppWriter = pWriter; + + /* Allocate a buffer in which to accumulate data */ + pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize); + if( !pWriter->aData ) return SQLITE_NOMEM; + pWriter->nSize = p->nNodeSize; + + /* Find the next free blockid in the %_segments table */ + rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0); + if( rc!=SQLITE_OK ) return rc; + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + pWriter->iFree = sqlite3_column_int64(pStmt, 0); + pWriter->iFirst = pWriter->iFree; + } + rc = sqlite3_reset(pStmt); + if( rc!=SQLITE_OK ) return rc; + } + nData = pWriter->nData; + + nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm); + nSuffix = nTerm-nPrefix; + + /* Figure out how many bytes are required by this new entry */ + nReq = sqlite3Fts3VarintLen(nPrefix) + /* varint containing prefix size */ + sqlite3Fts3VarintLen(nSuffix) + /* varint containing suffix size */ + nSuffix + /* Term suffix */ + sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */ + nDoclist; /* Doclist data */ + + if( nData>0 && nData+nReq>p->nNodeSize ){ + int rc; + + /* The current leaf node is full. Write it out to the database. */ + rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData); + if( rc!=SQLITE_OK ) return rc; + + /* Add the current term to the interior node tree. The term added to + ** the interior tree must: + ** + ** a) be greater than the largest term on the leaf node just written + ** to the database (still available in pWriter->zTerm), and + ** + ** b) be less than or equal to the term about to be added to the new + ** leaf node (zTerm/nTerm). + ** + ** In other words, it must be the prefix of zTerm 1 byte longer than + ** the common prefix (if any) of zTerm and pWriter->zTerm. + */ + assert( nPrefixpTree, isCopyTerm, zTerm, nPrefix+1); + if( rc!=SQLITE_OK ) return rc; + + nData = 0; + pWriter->nTerm = 0; + + nPrefix = 0; + nSuffix = nTerm; + nReq = 1 + /* varint containing prefix size */ + sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */ + nTerm + /* Term suffix */ + sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */ + nDoclist; /* Doclist data */ + } + + /* If the buffer currently allocated is too small for this entry, realloc + ** the buffer to make it large enough. + */ + if( nReq>pWriter->nSize ){ + char *aNew = sqlite3_realloc(pWriter->aData, nReq); + if( !aNew ) return SQLITE_NOMEM; + pWriter->aData = aNew; + pWriter->nSize = nReq; + } + assert( nData+nReq<=pWriter->nSize ); + + /* Append the prefix-compressed term and doclist to the buffer. */ + nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix); + nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix); + memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix); + nData += nSuffix; + nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist); + memcpy(&pWriter->aData[nData], aDoclist, nDoclist); + pWriter->nData = nData + nDoclist; + + /* Save the current term so that it can be used to prefix-compress the next. + ** If the isCopyTerm parameter is true, then the buffer pointed to by + ** zTerm is transient, so take a copy of the term data. Otherwise, just + ** store a copy of the pointer. + */ + if( isCopyTerm ){ + if( nTerm>pWriter->nMalloc ){ + char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2); + if( !zNew ){ + return SQLITE_NOMEM; + } + pWriter->nMalloc = nTerm*2; + pWriter->zMalloc = zNew; + pWriter->zTerm = zNew; + } + assert( pWriter->zTerm==pWriter->zMalloc ); + memcpy(pWriter->zTerm, zTerm, nTerm); + }else{ + pWriter->zTerm = (char *)zTerm; + } + pWriter->nTerm = nTerm; + + return SQLITE_OK; +} + +/* +** Flush all data associated with the SegmentWriter object pWriter to the +** database. This function must be called after all terms have been added +** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is +** returned. Otherwise, an SQLite error code. +*/ +static int fts3SegWriterFlush( + Fts3Table *p, /* Virtual table handle */ + SegmentWriter *pWriter, /* SegmentWriter to flush to the db */ + int iLevel, /* Value for 'level' column of %_segdir */ + int iIdx /* Value for 'idx' column of %_segdir */ +){ + int rc; /* Return code */ + if( pWriter->pTree ){ + sqlite3_int64 iLast = 0; /* Largest block id written to database */ + sqlite3_int64 iLastLeaf; /* Largest leaf block id written to db */ + char *zRoot = NULL; /* Pointer to buffer containing root node */ + int nRoot = 0; /* Size of buffer zRoot */ + + iLastLeaf = pWriter->iFree; + rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData); + if( rc==SQLITE_OK ){ + rc = fts3NodeWrite(p, pWriter->pTree, 1, + pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot); + } + if( rc==SQLITE_OK ){ + rc = fts3WriteSegdir( + p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot); + } + }else{ + /* The entire tree fits on the root node. Write it to the segdir table. */ + rc = fts3WriteSegdir( + p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData); + } + return rc; +} + +/* +** Release all memory held by the SegmentWriter object passed as the +** first argument. +*/ +static void fts3SegWriterFree(SegmentWriter *pWriter){ + if( pWriter ){ + sqlite3_free(pWriter->aData); + sqlite3_free(pWriter->zMalloc); + fts3NodeFree(pWriter->pTree); + sqlite3_free(pWriter); + } +} + +/* +** The first value in the apVal[] array is assumed to contain an integer. +** This function tests if there exist any documents with docid values that +** are different from that integer. i.e. if deleting the document with docid +** apVal[0] would mean the FTS3 table were empty. +** +** If successful, *pisEmpty is set to true if the table is empty except for +** document apVal[0], or false otherwise, and SQLITE_OK is returned. If an +** error occurs, an SQLite error code is returned. +*/ +static int fts3IsEmpty(Fts3Table *p, sqlite3_value **apVal, int *pisEmpty){ + sqlite3_stmt *pStmt; + int rc; + rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, apVal); + if( rc==SQLITE_OK ){ + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + *pisEmpty = sqlite3_column_int(pStmt, 0); + } + rc = sqlite3_reset(pStmt); + } + return rc; +} + +/* +** Set *pnSegment to the total number of segments in the database. Set +** *pnMax to the largest segment level in the database (segment levels +** are stored in the 'level' column of the %_segdir table). +** +** Return SQLITE_OK if successful, or an SQLite error code if not. +*/ +static int fts3SegmentCountMax(Fts3Table *p, int *pnSegment, int *pnMax){ + sqlite3_stmt *pStmt; + int rc; + + rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_COUNT_MAX, &pStmt, 0); + if( rc!=SQLITE_OK ) return rc; + if( SQLITE_ROW==sqlite3_step(pStmt) ){ + *pnSegment = sqlite3_column_int(pStmt, 0); + *pnMax = sqlite3_column_int(pStmt, 1); + } + return sqlite3_reset(pStmt); +} + +/* +** This function is used after merging multiple segments into a single large +** segment to delete the old, now redundant, segment b-trees. Specifically, +** it: +** +** 1) Deletes all %_segments entries for the segments associated with +** each of the SegReader objects in the array passed as the third +** argument, and +** +** 2) deletes all %_segdir entries with level iLevel, or all %_segdir +** entries regardless of level if (iLevel<0). +** +** SQLITE_OK is returned if successful, otherwise an SQLite error code. +*/ +static int fts3DeleteSegdir( + Fts3Table *p, /* Virtual table handle */ + int iLevel, /* Level of %_segdir entries to delete */ + Fts3SegReader **apSegment, /* Array of SegReader objects */ + int nReader /* Size of array apSegment */ +){ + int rc; /* Return Code */ + int i; /* Iterator variable */ + sqlite3_stmt *pDelete; /* SQL statement to delete rows */ + + rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0); + for(i=0; rc==SQLITE_OK && iiStartBlock ){ + sqlite3_bind_int64(pDelete, 1, pSegment->iStartBlock); + sqlite3_bind_int64(pDelete, 2, pSegment->iEndBlock); + sqlite3_step(pDelete); + rc = sqlite3_reset(pDelete); + } + } + if( rc!=SQLITE_OK ){ + return rc; + } + + if( iLevel==FTS3_SEGCURSOR_ALL ){ + fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0); + }else if( iLevel==FTS3_SEGCURSOR_PENDING ){ + sqlite3Fts3PendingTermsClear(p); + }else{ + assert( iLevel>=0 ); + rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_BY_LEVEL, &pDelete, 0); + if( rc==SQLITE_OK ){ + sqlite3_bind_int(pDelete, 1, iLevel); + sqlite3_step(pDelete); + rc = sqlite3_reset(pDelete); + } + } + + return rc; +} + +/* +** When this function is called, buffer *ppList (size *pnList bytes) contains +** a position list that may (or may not) feature multiple columns. This +** function adjusts the pointer *ppList and the length *pnList so that they +** identify the subset of the position list that corresponds to column iCol. +** +** If there are no entries in the input position list for column iCol, then +** *pnList is set to zero before returning. +*/ +static void fts3ColumnFilter( + int iCol, /* Column to filter on */ + char **ppList, /* IN/OUT: Pointer to position list */ + int *pnList /* IN/OUT: Size of buffer *ppList in bytes */ +){ + char *pList = *ppList; + int nList = *pnList; + char *pEnd = &pList[nList]; + int iCurrent = 0; + char *p = pList; + + assert( iCol>=0 ); + while( 1 ){ + char c = 0; + while( ppFilter = pFilter; + + /* If the Fts3SegFilter defines a specific term (or term prefix) to search + ** for, then advance each segment iterator until it points to a term of + ** equal or greater value than the specified term. This prevents many + ** unnecessary merge/sort operations for the case where single segment + ** b-tree leaf nodes contain more than one term. + */ + for(i=0; inSegment; i++){ + int nTerm = pFilter->nTerm; + const char *zTerm = pFilter->zTerm; + Fts3SegReader *pSeg = pCsr->apSegment[i]; + do { + int rc = fts3SegReaderNext(p, pSeg); + if( rc!=SQLITE_OK ) return rc; + }while( zTerm && fts3SegReaderTermCmp(pSeg, zTerm, nTerm)<0 ); + } + fts3SegReaderSort( + pCsr->apSegment, pCsr->nSegment, pCsr->nSegment, fts3SegReaderCmp); + + return SQLITE_OK; +} + +SQLITE_PRIVATE int sqlite3Fts3SegReaderStep( + Fts3Table *p, /* Virtual table handle */ + Fts3SegReaderCursor *pCsr /* Cursor object */ +){ + int rc = SQLITE_OK; + + int isIgnoreEmpty = (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY); + int isRequirePos = (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS); + int isColFilter = (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER); + int isPrefix = (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX); + int isScan = (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN); + + Fts3SegReader **apSegment = pCsr->apSegment; + int nSegment = pCsr->nSegment; + Fts3SegFilter *pFilter = pCsr->pFilter; + + if( pCsr->nSegment==0 ) return SQLITE_OK; + + do { + int nMerge; + int i; + + /* Advance the first pCsr->nAdvance entries in the apSegment[] array + ** forward. Then sort the list in order of current term again. + */ + for(i=0; inAdvance; i++){ + rc = fts3SegReaderNext(p, apSegment[i]); + if( rc!=SQLITE_OK ) return rc; + } + fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp); + pCsr->nAdvance = 0; + + /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */ + assert( rc==SQLITE_OK ); + if( apSegment[0]->aNode==0 ) break; + + pCsr->nTerm = apSegment[0]->nTerm; + pCsr->zTerm = apSegment[0]->zTerm; + + /* If this is a prefix-search, and if the term that apSegment[0] points + ** to does not share a suffix with pFilter->zTerm/nTerm, then all + ** required callbacks have been made. In this case exit early. + ** + ** Similarly, if this is a search for an exact match, and the first term + ** of segment apSegment[0] is not a match, exit early. + */ + if( pFilter->zTerm && !isScan ){ + if( pCsr->nTermnTerm + || (!isPrefix && pCsr->nTerm>pFilter->nTerm) + || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm) + ){ + break; + } + } + + nMerge = 1; + while( nMergeaNode + && apSegment[nMerge]->nTerm==pCsr->nTerm + && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm) + ){ + nMerge++; + } + + assert( isIgnoreEmpty || (isRequirePos && !isColFilter) ); + if( nMerge==1 && !isIgnoreEmpty ){ + pCsr->aDoclist = apSegment[0]->aDoclist; + pCsr->nDoclist = apSegment[0]->nDoclist; + rc = SQLITE_ROW; + }else{ + int nDoclist = 0; /* Size of doclist */ + sqlite3_int64 iPrev = 0; /* Previous docid stored in doclist */ + + /* The current term of the first nMerge entries in the array + ** of Fts3SegReader objects is the same. The doclists must be merged + ** and a single term returned with the merged doclist. + */ + for(i=0; ipOffsetList ){ + int j; /* Number of segments that share a docid */ + char *pList; + int nList; + int nByte; + sqlite3_int64 iDocid = apSegment[0]->iDocid; + fts3SegReaderNextDocid(apSegment[0], &pList, &nList); + j = 1; + while( jpOffsetList + && apSegment[j]->iDocid==iDocid + ){ + fts3SegReaderNextDocid(apSegment[j], 0, 0); + j++; + } + + if( isColFilter ){ + fts3ColumnFilter(pFilter->iCol, &pList, &nList); + } + + if( !isIgnoreEmpty || nList>0 ){ + nByte = sqlite3Fts3VarintLen(iDocid-iPrev) + (isRequirePos?nList+1:0); + if( nDoclist+nByte>pCsr->nBuffer ){ + char *aNew; + pCsr->nBuffer = (nDoclist+nByte)*2; + aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer); + if( !aNew ){ + return SQLITE_NOMEM; + } + pCsr->aBuffer = aNew; + } + nDoclist += sqlite3Fts3PutVarint( + &pCsr->aBuffer[nDoclist], iDocid-iPrev + ); + iPrev = iDocid; + if( isRequirePos ){ + memcpy(&pCsr->aBuffer[nDoclist], pList, nList); + nDoclist += nList; + pCsr->aBuffer[nDoclist++] = '\0'; + } + } + + fts3SegReaderSort(apSegment, nMerge, j, fts3SegReaderDoclistCmp); + } + if( nDoclist>0 ){ + pCsr->aDoclist = pCsr->aBuffer; + pCsr->nDoclist = nDoclist; + rc = SQLITE_ROW; + } + } + pCsr->nAdvance = nMerge; + }while( rc==SQLITE_OK ); + + return rc; +} + +SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish( + Fts3SegReaderCursor *pCsr /* Cursor object */ +){ + if( pCsr ){ + int i; + for(i=0; inSegment; i++){ + sqlite3Fts3SegReaderFree(pCsr->apSegment[i]); + } + sqlite3_free(pCsr->apSegment); + sqlite3_free(pCsr->aBuffer); + + pCsr->nSegment = 0; + pCsr->apSegment = 0; + pCsr->aBuffer = 0; + } +} + +/* +** Merge all level iLevel segments in the database into a single +** iLevel+1 segment. Or, if iLevel<0, merge all segments into a +** single segment with a level equal to the numerically largest level +** currently present in the database. +** +** If this function is called with iLevel<0, but there is only one +** segment in the database, SQLITE_DONE is returned immediately. +** Otherwise, if successful, SQLITE_OK is returned. If an error occurs, +** an SQLite error code is returned. +*/ +static int fts3SegmentMerge(Fts3Table *p, int iLevel){ + int rc; /* Return code */ + int iIdx = 0; /* Index of new segment */ + int iNewLevel = 0; /* Level to create new segment at */ + SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */ + Fts3SegFilter filter; /* Segment term filter condition */ + Fts3SegReaderCursor csr; /* Cursor to iterate through level(s) */ + + rc = sqlite3Fts3SegReaderCursor(p, iLevel, 0, 0, 1, 0, &csr); + if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished; + + if( iLevel==FTS3_SEGCURSOR_ALL ){ + /* This call is to merge all segments in the database to a single + ** segment. The level of the new segment is equal to the the numerically + ** greatest segment level currently present in the database. The index + ** of the new segment is always 0. */ + int nDummy; /* TODO: Remove this */ + if( csr.nSegment==1 ){ + rc = SQLITE_DONE; + goto finished; + } + rc = fts3SegmentCountMax(p, &nDummy, &iNewLevel); + }else{ + /* This call is to merge all segments at level iLevel. Find the next + ** available segment index at level iLevel+1. The call to + ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to + ** a single iLevel+2 segment if necessary. */ + iNewLevel = iLevel+1; + rc = fts3AllocateSegdirIdx(p, iNewLevel, &iIdx); + } + if( rc!=SQLITE_OK ) goto finished; + assert( csr.nSegment>0 ); + assert( iNewLevel>=0 ); + + memset(&filter, 0, sizeof(Fts3SegFilter)); + filter.flags = FTS3_SEGMENT_REQUIRE_POS; + filter.flags |= (iLevel==FTS3_SEGCURSOR_ALL ? FTS3_SEGMENT_IGNORE_EMPTY : 0); + + rc = sqlite3Fts3SegReaderStart(p, &csr, &filter); + while( SQLITE_OK==rc ){ + rc = sqlite3Fts3SegReaderStep(p, &csr); + if( rc!=SQLITE_ROW ) break; + rc = fts3SegWriterAdd(p, &pWriter, 1, + csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist); + } + if( rc!=SQLITE_OK ) goto finished; + assert( pWriter ); + + rc = fts3DeleteSegdir(p, iLevel, csr.apSegment, csr.nSegment); + if( rc!=SQLITE_OK ) goto finished; + rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx); + + finished: + fts3SegWriterFree(pWriter); + sqlite3Fts3SegReaderFinish(&csr); + return rc; +} + + +/* +** Flush the contents of pendingTerms to a level 0 segment. +*/ +SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){ + return fts3SegmentMerge(p, FTS3_SEGCURSOR_PENDING); +} + +/* +** Encode N integers as varints into a blob. +*/ +static void fts3EncodeIntArray( + int N, /* The number of integers to encode */ + u32 *a, /* The integer values */ + char *zBuf, /* Write the BLOB here */ + int *pNBuf /* Write number of bytes if zBuf[] used here */ +){ + int i, j; + for(i=j=0; iiPrevDocid. The sizes are encoded as +** a blob of varints. +*/ +static void fts3InsertDocsize( + int *pRC, /* Result code */ + Fts3Table *p, /* Table into which to insert */ + u32 *aSz /* Sizes of each column */ +){ + char *pBlob; /* The BLOB encoding of the document size */ + int nBlob; /* Number of bytes in the BLOB */ + sqlite3_stmt *pStmt; /* Statement used to insert the encoding */ + int rc; /* Result code from subfunctions */ + + if( *pRC ) return; + pBlob = sqlite3_malloc( 10*p->nColumn ); + if( pBlob==0 ){ + *pRC = SQLITE_NOMEM; + return; + } + fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob); + rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0); + if( rc ){ + sqlite3_free(pBlob); + *pRC = rc; + return; + } + sqlite3_bind_int64(pStmt, 1, p->iPrevDocid); + sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free); + sqlite3_step(pStmt); + *pRC = sqlite3_reset(pStmt); +} + +/* +** Record 0 of the %_stat table contains a blob consisting of N varints, +** where N is the number of user defined columns in the fts3 table plus +** two. If nCol is the number of user defined columns, then values of the +** varints are set as follows: +** +** Varint 0: Total number of rows in the table. +** +** Varint 1..nCol: For each column, the total number of tokens stored in +** the column for all rows of the table. +** +** Varint 1+nCol: The total size, in bytes, of all text values in all +** columns of all rows of the table. +** +*/ +static void fts3UpdateDocTotals( + int *pRC, /* The result code */ + Fts3Table *p, /* Table being updated */ + u32 *aSzIns, /* Size increases */ + u32 *aSzDel, /* Size decreases */ + int nChng /* Change in the number of documents */ +){ + char *pBlob; /* Storage for BLOB written into %_stat */ + int nBlob; /* Size of BLOB written into %_stat */ + u32 *a; /* Array of integers that becomes the BLOB */ + sqlite3_stmt *pStmt; /* Statement for reading and writing */ + int i; /* Loop counter */ + int rc; /* Result code from subfunctions */ + + const int nStat = p->nColumn+2; + + if( *pRC ) return; + a = sqlite3_malloc( (sizeof(u32)+10)*nStat ); + if( a==0 ){ + *pRC = SQLITE_NOMEM; + return; + } + pBlob = (char*)&a[nStat]; + rc = fts3SqlStmt(p, SQL_SELECT_DOCTOTAL, &pStmt, 0); + if( rc ){ + sqlite3_free(a); + *pRC = rc; + return; + } + if( sqlite3_step(pStmt)==SQLITE_ROW ){ + fts3DecodeIntArray(nStat, a, + sqlite3_column_blob(pStmt, 0), + sqlite3_column_bytes(pStmt, 0)); + }else{ + memset(a, 0, sizeof(u32)*(nStat) ); + } + sqlite3_reset(pStmt); + if( nChng<0 && a[0]<(u32)(-nChng) ){ + a[0] = 0; + }else{ + a[0] += nChng; + } + for(i=0; inColumn+1; i++){ + u32 x = a[i+1]; + if( x+aSzIns[i] < aSzDel[i] ){ + x = 0; + }else{ + x = x + aSzIns[i] - aSzDel[i]; + } + a[i+1] = x; + } + fts3EncodeIntArray(nStat, a, pBlob, &nBlob); + rc = fts3SqlStmt(p, SQL_REPLACE_DOCTOTAL, &pStmt, 0); + if( rc ){ + sqlite3_free(a); + *pRC = rc; + return; + } + sqlite3_bind_blob(pStmt, 1, pBlob, nBlob, SQLITE_STATIC); + sqlite3_step(pStmt); + *pRC = sqlite3_reset(pStmt); + sqlite3_free(a); +} + +/* +** Handle a 'special' INSERT of the form: +** +** "INSERT INTO tbl(tbl) VALUES()" +** +** Argument pVal contains the result of . Currently the only +** meaningful value to insert is the text 'optimize'. +*/ +static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){ + int rc; /* Return Code */ + const char *zVal = (const char *)sqlite3_value_text(pVal); + int nVal = sqlite3_value_bytes(pVal); + + if( !zVal ){ + return SQLITE_NOMEM; + }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){ + rc = fts3SegmentMerge(p, FTS3_SEGCURSOR_ALL); + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + }else{ + sqlite3Fts3PendingTermsClear(p); + } +#ifdef SQLITE_TEST + }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){ + p->nNodeSize = atoi(&zVal[9]); + rc = SQLITE_OK; + }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){ + p->nMaxPendingData = atoi(&zVal[11]); + rc = SQLITE_OK; +#endif + }else{ + rc = SQLITE_ERROR; + } + + sqlite3Fts3SegmentsClose(p); + return rc; +} + +/* +** Return the deferred doclist associated with deferred token pDeferred. +** This function assumes that sqlite3Fts3CacheDeferredDoclists() has already +** been called to allocate and populate the doclist. +*/ +SQLITE_PRIVATE char *sqlite3Fts3DeferredDoclist(Fts3DeferredToken *pDeferred, int *pnByte){ + if( pDeferred->pList ){ + *pnByte = pDeferred->pList->nData; + return pDeferred->pList->aData; + } + *pnByte = 0; + return 0; +} + +/* +** Helper fucntion for FreeDeferredDoclists(). This function removes all +** references to deferred doclists from within the tree of Fts3Expr +** structures headed by +*/ +static void fts3DeferredDoclistClear(Fts3Expr *pExpr){ + if( pExpr ){ + fts3DeferredDoclistClear(pExpr->pLeft); + fts3DeferredDoclistClear(pExpr->pRight); + if( pExpr->isLoaded ){ + sqlite3_free(pExpr->aDoclist); + pExpr->isLoaded = 0; + pExpr->aDoclist = 0; + pExpr->nDoclist = 0; + pExpr->pCurrent = 0; + pExpr->iCurrent = 0; + } + } +} + +/* +** Delete all cached deferred doclists. Deferred doclists are cached +** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function. +*/ +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){ + Fts3DeferredToken *pDef; + for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){ + sqlite3_free(pDef->pList); + pDef->pList = 0; + } + if( pCsr->pDeferred ){ + fts3DeferredDoclistClear(pCsr->pExpr); + } +} + +/* +** Free all entries in the pCsr->pDeffered list. Entries are added to +** this list using sqlite3Fts3DeferToken(). +*/ +SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){ + Fts3DeferredToken *pDef; + Fts3DeferredToken *pNext; + for(pDef=pCsr->pDeferred; pDef; pDef=pNext){ + pNext = pDef->pNext; + sqlite3_free(pDef->pList); + sqlite3_free(pDef); + } + pCsr->pDeferred = 0; +} + +/* +** Generate deferred-doclists for all tokens in the pCsr->pDeferred list +** based on the row that pCsr currently points to. +** +** A deferred-doclist is like any other doclist with position information +** included, except that it only contains entries for a single row of the +** table, not for all rows. +*/ +SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){ + int rc = SQLITE_OK; /* Return code */ + if( pCsr->pDeferred ){ + int i; /* Used to iterate through table columns */ + sqlite3_int64 iDocid; /* Docid of the row pCsr points to */ + Fts3DeferredToken *pDef; /* Used to iterate through deferred tokens */ + + Fts3Table *p = (Fts3Table *)pCsr->base.pVtab; + sqlite3_tokenizer *pT = p->pTokenizer; + sqlite3_tokenizer_module const *pModule = pT->pModule; + + assert( pCsr->isRequireSeek==0 ); + iDocid = sqlite3_column_int64(pCsr->pStmt, 0); + + for(i=0; inColumn && rc==SQLITE_OK; i++){ + const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1); + sqlite3_tokenizer_cursor *pTC = 0; + + rc = pModule->xOpen(pT, zText, -1, &pTC); + while( rc==SQLITE_OK ){ + char const *zToken; /* Buffer containing token */ + int nToken; /* Number of bytes in token */ + int iDum1, iDum2; /* Dummy variables */ + int iPos; /* Position of token in zText */ + + pTC->pTokenizer = pT; + rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos); + for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){ + Fts3PhraseToken *pPT = pDef->pToken; + if( (pDef->iCol>=p->nColumn || pDef->iCol==i) + && (pPT->n==nToken || (pPT->isPrefix && pPT->nz, pPT->n)) + ){ + fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc); + } + } + } + if( pTC ) pModule->xClose(pTC); + if( rc==SQLITE_DONE ) rc = SQLITE_OK; + } + + for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){ + if( pDef->pList ){ + rc = fts3PendingListAppendVarint(&pDef->pList, 0); + } + } + } + + return rc; +} + +/* +** Add an entry for token pToken to the pCsr->pDeferred list. +*/ +SQLITE_PRIVATE int sqlite3Fts3DeferToken( + Fts3Cursor *pCsr, /* Fts3 table cursor */ + Fts3PhraseToken *pToken, /* Token to defer */ + int iCol /* Column that token must appear in (or -1) */ +){ + Fts3DeferredToken *pDeferred; + pDeferred = sqlite3_malloc(sizeof(*pDeferred)); + if( !pDeferred ){ + return SQLITE_NOMEM; + } + memset(pDeferred, 0, sizeof(*pDeferred)); + pDeferred->pToken = pToken; + pDeferred->pNext = pCsr->pDeferred; + pDeferred->iCol = iCol; + pCsr->pDeferred = pDeferred; + + assert( pToken->pDeferred==0 ); + pToken->pDeferred = pDeferred; + + return SQLITE_OK; +} + + +/* +** This function does the work for the xUpdate method of FTS3 virtual +** tables. +*/ +SQLITE_PRIVATE int sqlite3Fts3UpdateMethod( + sqlite3_vtab *pVtab, /* FTS3 vtab object */ + int nArg, /* Size of argument array */ + sqlite3_value **apVal, /* Array of arguments */ + sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */ +){ + Fts3Table *p = (Fts3Table *)pVtab; + int rc = SQLITE_OK; /* Return Code */ + int isRemove = 0; /* True for an UPDATE or DELETE */ + sqlite3_int64 iRemove = 0; /* Rowid removed by UPDATE or DELETE */ + u32 *aSzIns; /* Sizes of inserted documents */ + u32 *aSzDel; /* Sizes of deleted documents */ + int nChng = 0; /* Net change in number of documents */ + + assert( p->pSegments==0 ); + + /* Allocate space to hold the change in document sizes */ + aSzIns = sqlite3_malloc( sizeof(aSzIns[0])*(p->nColumn+1)*2 ); + if( aSzIns==0 ) return SQLITE_NOMEM; + aSzDel = &aSzIns[p->nColumn+1]; + memset(aSzIns, 0, sizeof(aSzIns[0])*(p->nColumn+1)*2); + + /* If this is a DELETE or UPDATE operation, remove the old record. */ + if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){ + int isEmpty = 0; + rc = fts3IsEmpty(p, apVal, &isEmpty); + if( rc==SQLITE_OK ){ + if( isEmpty ){ + /* Deleting this row means the whole table is empty. In this case + ** delete the contents of all three tables and throw away any + ** data in the pendingTerms hash table. + */ + rc = fts3DeleteAll(p); + }else{ + isRemove = 1; + iRemove = sqlite3_value_int64(apVal[0]); + rc = fts3PendingTermsDocid(p, iRemove); + fts3DeleteTerms(&rc, p, apVal, aSzDel); + fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, apVal); + if( p->bHasDocsize ){ + fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, apVal); + } + nChng--; + } + } + }else if( sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL ){ + sqlite3_free(aSzIns); + return fts3SpecialInsert(p, apVal[p->nColumn+2]); + } + + /* If this is an INSERT or UPDATE operation, insert the new record. */ + if( nArg>1 && rc==SQLITE_OK ){ + rc = fts3InsertData(p, apVal, pRowid); + if( rc==SQLITE_OK && (!isRemove || *pRowid!=iRemove) ){ + rc = fts3PendingTermsDocid(p, *pRowid); + } + if( rc==SQLITE_OK ){ + rc = fts3InsertTerms(p, apVal, aSzIns); + } + if( p->bHasDocsize ){ + fts3InsertDocsize(&rc, p, aSzIns); + } + nChng++; + } + + if( p->bHasStat ){ + fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng); + } + + sqlite3_free(aSzIns); + sqlite3Fts3SegmentsClose(p); + return rc; +} + +/* +** Flush any data in the pending-terms hash table to disk. If successful, +** merge all segments in the database (including the new segment, if +** there was any data to flush) into a single segment. +*/ +SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){ + int rc; + rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0); + if( rc==SQLITE_OK ){ + rc = fts3SegmentMerge(p, FTS3_SEGCURSOR_ALL); + if( rc==SQLITE_OK ){ + rc = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0); + if( rc==SQLITE_OK ){ + sqlite3Fts3PendingTermsClear(p); + } + }else{ + sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0); + sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0); + } + } + sqlite3Fts3SegmentsClose(p); + return rc; +} + +#endif + +/************** End of fts3_write.c ******************************************/ +/************** Begin file fts3_snippet.c ************************************/ +/* +** 2009 Oct 23 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +*/ + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) + + +/* +** Characters that may appear in the second argument to matchinfo(). +*/ +#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */ +#define FTS3_MATCHINFO_NCOL 'c' /* 1 value */ +#define FTS3_MATCHINFO_NDOC 'n' /* 1 value */ +#define FTS3_MATCHINFO_AVGLENGTH 'a' /* nCol values */ +#define FTS3_MATCHINFO_LENGTH 'l' /* nCol values */ +#define FTS3_MATCHINFO_LCS 's' /* nCol values */ +#define FTS3_MATCHINFO_HITS 'x' /* 3*nCol*nPhrase values */ + +/* +** The default value for the second argument to matchinfo(). +*/ +#define FTS3_MATCHINFO_DEFAULT "pcx" + + +/* +** Used as an fts3ExprIterate() context when loading phrase doclists to +** Fts3Expr.aDoclist[]/nDoclist. +*/ +typedef struct LoadDoclistCtx LoadDoclistCtx; +struct LoadDoclistCtx { + Fts3Cursor *pCsr; /* FTS3 Cursor */ + int nPhrase; /* Number of phrases seen so far */ + int nToken; /* Number of tokens seen so far */ +}; + +/* +** The following types are used as part of the implementation of the +** fts3BestSnippet() routine. +*/ +typedef struct SnippetIter SnippetIter; +typedef struct SnippetPhrase SnippetPhrase; +typedef struct SnippetFragment SnippetFragment; + +struct SnippetIter { + Fts3Cursor *pCsr; /* Cursor snippet is being generated from */ + int iCol; /* Extract snippet from this column */ + int nSnippet; /* Requested snippet length (in tokens) */ + int nPhrase; /* Number of phrases in query */ + SnippetPhrase *aPhrase; /* Array of size nPhrase */ + int iCurrent; /* First token of current snippet */ +}; + +struct SnippetPhrase { + int nToken; /* Number of tokens in phrase */ + char *pList; /* Pointer to start of phrase position list */ + int iHead; /* Next value in position list */ + char *pHead; /* Position list data following iHead */ + int iTail; /* Next value in trailing position list */ + char *pTail; /* Position list data following iTail */ +}; + +struct SnippetFragment { + int iCol; /* Column snippet is extracted from */ + int iPos; /* Index of first token in snippet */ + u64 covered; /* Mask of query phrases covered */ + u64 hlmask; /* Mask of snippet terms to highlight */ +}; + +/* +** This type is used as an fts3ExprIterate() context object while +** accumulating the data returned by the matchinfo() function. +*/ +typedef struct MatchInfo MatchInfo; +struct MatchInfo { + Fts3Cursor *pCursor; /* FTS3 Cursor */ + int nCol; /* Number of columns in table */ + int nPhrase; /* Number of matchable phrases in query */ + sqlite3_int64 nDoc; /* Number of docs in database */ + u32 *aMatchinfo; /* Pre-allocated buffer */ +}; + + + +/* +** The snippet() and offsets() functions both return text values. An instance +** of the following structure is used to accumulate those values while the +** functions are running. See fts3StringAppend() for details. +*/ +typedef struct StrBuffer StrBuffer; +struct StrBuffer { + char *z; /* Pointer to buffer containing string */ + int n; /* Length of z in bytes (excl. nul-term) */ + int nAlloc; /* Allocated size of buffer z in bytes */ +}; + + +/* +** This function is used to help iterate through a position-list. A position +** list is a list of unique integers, sorted from smallest to largest. Each +** element of the list is represented by an FTS3 varint that takes the value +** of the difference between the current element and the previous one plus +** two. For example, to store the position-list: +** +** 4 9 113 +** +** the three varints: +** +** 6 7 106 +** +** are encoded. +** +** When this function is called, *pp points to the start of an element of +** the list. *piPos contains the value of the previous entry in the list. +** After it returns, *piPos contains the value of the next element of the +** list and *pp is advanced to the following varint. +*/ +static void fts3GetDeltaPosition(char **pp, int *piPos){ + int iVal; + *pp += sqlite3Fts3GetVarint32(*pp, &iVal); + *piPos += (iVal-2); +} + +/* +** Helper function for fts3ExprIterate() (see below). +*/ +static int fts3ExprIterate2( + Fts3Expr *pExpr, /* Expression to iterate phrases of */ + int *piPhrase, /* Pointer to phrase counter */ + int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ + void *pCtx /* Second argument to pass to callback */ +){ + int rc; /* Return code */ + int eType = pExpr->eType; /* Type of expression node pExpr */ + + if( eType!=FTSQUERY_PHRASE ){ + assert( pExpr->pLeft && pExpr->pRight ); + rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx); + if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){ + rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx); + } + }else{ + rc = x(pExpr, *piPhrase, pCtx); + (*piPhrase)++; + } + return rc; +} + +/* +** Iterate through all phrase nodes in an FTS3 query, except those that +** are part of a sub-tree that is the right-hand-side of a NOT operator. +** For each phrase node found, the supplied callback function is invoked. +** +** If the callback function returns anything other than SQLITE_OK, +** the iteration is abandoned and the error code returned immediately. +** Otherwise, SQLITE_OK is returned after a callback has been made for +** all eligible phrase nodes. +*/ +static int fts3ExprIterate( + Fts3Expr *pExpr, /* Expression to iterate phrases of */ + int (*x)(Fts3Expr*,int,void*), /* Callback function to invoke for phrases */ + void *pCtx /* Second argument to pass to callback */ +){ + int iPhrase = 0; /* Variable used as the phrase counter */ + return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx); +} + +/* +** The argument to this function is always a phrase node. Its doclist +** (Fts3Expr.aDoclist[]) and the doclists associated with all phrase nodes +** to the left of this one in the query tree have already been loaded. +** +** If this phrase node is part of a series of phrase nodes joined by +** NEAR operators (and is not the left-most of said series), then elements are +** removed from the phrases doclist consistent with the NEAR restriction. If +** required, elements may be removed from the doclists of phrases to the +** left of this one that are part of the same series of NEAR operator +** connected phrases. +** +** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK. +*/ +static int fts3ExprNearTrim(Fts3Expr *pExpr){ + int rc = SQLITE_OK; + Fts3Expr *pParent = pExpr->pParent; + + assert( pExpr->eType==FTSQUERY_PHRASE ); + while( rc==SQLITE_OK + && pParent + && pParent->eType==FTSQUERY_NEAR + && pParent->pRight==pExpr + ){ + /* This expression (pExpr) is the right-hand-side of a NEAR operator. + ** Find the expression to the left of the same operator. + */ + int nNear = pParent->nNear; + Fts3Expr *pLeft = pParent->pLeft; + + if( pLeft->eType!=FTSQUERY_PHRASE ){ + assert( pLeft->eType==FTSQUERY_NEAR ); + assert( pLeft->pRight->eType==FTSQUERY_PHRASE ); + pLeft = pLeft->pRight; + } + + rc = sqlite3Fts3ExprNearTrim(pLeft, pExpr, nNear); + + pExpr = pLeft; + pParent = pExpr->pParent; + } + + return rc; +} + +/* +** This is an fts3ExprIterate() callback used while loading the doclists +** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also +** fts3ExprLoadDoclists(). +*/ +static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ + int rc = SQLITE_OK; + LoadDoclistCtx *p = (LoadDoclistCtx *)ctx; + + UNUSED_PARAMETER(iPhrase); + + p->nPhrase++; + p->nToken += pExpr->pPhrase->nToken; + + if( pExpr->isLoaded==0 ){ + rc = sqlite3Fts3ExprLoadDoclist(p->pCsr, pExpr); + pExpr->isLoaded = 1; + if( rc==SQLITE_OK ){ + rc = fts3ExprNearTrim(pExpr); + } + } + + return rc; +} + +/* +** Load the doclists for each phrase in the query associated with FTS3 cursor +** pCsr. +** +** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable +** phrases in the expression (all phrases except those directly or +** indirectly descended from the right-hand-side of a NOT operator). If +** pnToken is not NULL, then it is set to the number of tokens in all +** matchable phrases of the expression. +*/ +static int fts3ExprLoadDoclists( + Fts3Cursor *pCsr, /* Fts3 cursor for current query */ + int *pnPhrase, /* OUT: Number of phrases in query */ + int *pnToken /* OUT: Number of tokens in query */ +){ + int rc; /* Return Code */ + LoadDoclistCtx sCtx = {0,0,0}; /* Context for fts3ExprIterate() */ + sCtx.pCsr = pCsr; + rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx); + if( pnPhrase ) *pnPhrase = sCtx.nPhrase; + if( pnToken ) *pnToken = sCtx.nToken; + return rc; +} + +static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){ + (*(int *)ctx)++; + UNUSED_PARAMETER(pExpr); + UNUSED_PARAMETER(iPhrase); + return SQLITE_OK; +} +static int fts3ExprPhraseCount(Fts3Expr *pExpr){ + int nPhrase = 0; + (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase); + return nPhrase; +} + +/* +** Advance the position list iterator specified by the first two +** arguments so that it points to the first element with a value greater +** than or equal to parameter iNext. +*/ +static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){ + char *pIter = *ppIter; + if( pIter ){ + int iIter = *piIter; + + while( iIteriCurrent<0 ){ + /* The SnippetIter object has just been initialized. The first snippet + ** candidate always starts at offset 0 (even if this candidate has a + ** score of 0.0). + */ + pIter->iCurrent = 0; + + /* Advance the 'head' iterator of each phrase to the first offset that + ** is greater than or equal to (iNext+nSnippet). + */ + for(i=0; inPhrase; i++){ + SnippetPhrase *pPhrase = &pIter->aPhrase[i]; + fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet); + } + }else{ + int iStart; + int iEnd = 0x7FFFFFFF; + + for(i=0; inPhrase; i++){ + SnippetPhrase *pPhrase = &pIter->aPhrase[i]; + if( pPhrase->pHead && pPhrase->iHeadiHead; + } + } + if( iEnd==0x7FFFFFFF ){ + return 1; + } + + pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1; + for(i=0; inPhrase; i++){ + SnippetPhrase *pPhrase = &pIter->aPhrase[i]; + fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1); + fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart); + } + } + + return 0; +} + +/* +** Retrieve information about the current candidate snippet of snippet +** iterator pIter. +*/ +static void fts3SnippetDetails( + SnippetIter *pIter, /* Snippet iterator */ + u64 mCovered, /* Bitmask of phrases already covered */ + int *piToken, /* OUT: First token of proposed snippet */ + int *piScore, /* OUT: "Score" for this snippet */ + u64 *pmCover, /* OUT: Bitmask of phrases covered */ + u64 *pmHighlight /* OUT: Bitmask of terms to highlight */ +){ + int iStart = pIter->iCurrent; /* First token of snippet */ + int iScore = 0; /* Score of this snippet */ + int i; /* Loop counter */ + u64 mCover = 0; /* Mask of phrases covered by this snippet */ + u64 mHighlight = 0; /* Mask of tokens to highlight in snippet */ + + for(i=0; inPhrase; i++){ + SnippetPhrase *pPhrase = &pIter->aPhrase[i]; + if( pPhrase->pTail ){ + char *pCsr = pPhrase->pTail; + int iCsr = pPhrase->iTail; + + while( iCsr<(iStart+pIter->nSnippet) ){ + int j; + u64 mPhrase = (u64)1 << i; + u64 mPos = (u64)1 << (iCsr - iStart); + assert( iCsr>=iStart ); + if( (mCover|mCovered)&mPhrase ){ + iScore++; + }else{ + iScore += 1000; + } + mCover |= mPhrase; + + for(j=0; jnToken; j++){ + mHighlight |= (mPos>>j); + } + + if( 0==(*pCsr & 0x0FE) ) break; + fts3GetDeltaPosition(&pCsr, &iCsr); + } + } + } + + /* Set the output variables before returning. */ + *piToken = iStart; + *piScore = iScore; + *pmCover = mCover; + *pmHighlight = mHighlight; +} + +/* +** This function is an fts3ExprIterate() callback used by fts3BestSnippet(). +** Each invocation populates an element of the SnippetIter.aPhrase[] array. +*/ +static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){ + SnippetIter *p = (SnippetIter *)ctx; + SnippetPhrase *pPhrase = &p->aPhrase[iPhrase]; + char *pCsr; + + pPhrase->nToken = pExpr->pPhrase->nToken; + + pCsr = sqlite3Fts3FindPositions(pExpr, p->pCsr->iPrevId, p->iCol); + if( pCsr ){ + int iFirst = 0; + pPhrase->pList = pCsr; + fts3GetDeltaPosition(&pCsr, &iFirst); + pPhrase->pHead = pCsr; + pPhrase->pTail = pCsr; + pPhrase->iHead = iFirst; + pPhrase->iTail = iFirst; + }else{ + assert( pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0 ); + } + + return SQLITE_OK; +} + +/* +** Select the fragment of text consisting of nFragment contiguous tokens +** from column iCol that represent the "best" snippet. The best snippet +** is the snippet with the highest score, where scores are calculated +** by adding: +** +** (a) +1 point for each occurence of a matchable phrase in the snippet. +** +** (b) +1000 points for the first occurence of each matchable phrase in +** the snippet for which the corresponding mCovered bit is not set. +** +** The selected snippet parameters are stored in structure *pFragment before +** returning. The score of the selected snippet is stored in *piScore +** before returning. +*/ +static int fts3BestSnippet( + int nSnippet, /* Desired snippet length */ + Fts3Cursor *pCsr, /* Cursor to create snippet for */ + int iCol, /* Index of column to create snippet from */ + u64 mCovered, /* Mask of phrases already covered */ + u64 *pmSeen, /* IN/OUT: Mask of phrases seen */ + SnippetFragment *pFragment, /* OUT: Best snippet found */ + int *piScore /* OUT: Score of snippet pFragment */ +){ + int rc; /* Return Code */ + int nList; /* Number of phrases in expression */ + SnippetIter sIter; /* Iterates through snippet candidates */ + int nByte; /* Number of bytes of space to allocate */ + int iBestScore = -1; /* Best snippet score found so far */ + int i; /* Loop counter */ + + memset(&sIter, 0, sizeof(sIter)); + + /* Iterate through the phrases in the expression to count them. The same + ** callback makes sure the doclists are loaded for each phrase. + */ + rc = fts3ExprLoadDoclists(pCsr, &nList, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + /* Now that it is known how many phrases there are, allocate and zero + ** the required space using malloc(). + */ + nByte = sizeof(SnippetPhrase) * nList; + sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte); + if( !sIter.aPhrase ){ + return SQLITE_NOMEM; + } + memset(sIter.aPhrase, 0, nByte); + + /* Initialize the contents of the SnippetIter object. Then iterate through + ** the set of phrases in the expression to populate the aPhrase[] array. + */ + sIter.pCsr = pCsr; + sIter.iCol = iCol; + sIter.nSnippet = nSnippet; + sIter.nPhrase = nList; + sIter.iCurrent = -1; + (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter); + + /* Set the *pmSeen output variable. */ + for(i=0; iiCol = iCol; + while( !fts3SnippetNextCandidate(&sIter) ){ + int iPos; + int iScore; + u64 mCover; + u64 mHighlight; + fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight); + assert( iScore>=0 ); + if( iScore>iBestScore ){ + pFragment->iPos = iPos; + pFragment->hlmask = mHighlight; + pFragment->covered = mCover; + iBestScore = iScore; + } + } + + sqlite3_free(sIter.aPhrase); + *piScore = iBestScore; + return SQLITE_OK; +} + + +/* +** Append a string to the string-buffer passed as the first argument. +** +** If nAppend is negative, then the length of the string zAppend is +** determined using strlen(). +*/ +static int fts3StringAppend( + StrBuffer *pStr, /* Buffer to append to */ + const char *zAppend, /* Pointer to data to append to buffer */ + int nAppend /* Size of zAppend in bytes (or -1) */ +){ + if( nAppend<0 ){ + nAppend = (int)strlen(zAppend); + } + + /* If there is insufficient space allocated at StrBuffer.z, use realloc() + ** to grow the buffer until so that it is big enough to accomadate the + ** appended data. + */ + if( pStr->n+nAppend+1>=pStr->nAlloc ){ + int nAlloc = pStr->nAlloc+nAppend+100; + char *zNew = sqlite3_realloc(pStr->z, nAlloc); + if( !zNew ){ + return SQLITE_NOMEM; + } + pStr->z = zNew; + pStr->nAlloc = nAlloc; + } + + /* Append the data to the string buffer. */ + memcpy(&pStr->z[pStr->n], zAppend, nAppend); + pStr->n += nAppend; + pStr->z[pStr->n] = '\0'; + + return SQLITE_OK; +} + +/* +** The fts3BestSnippet() function often selects snippets that end with a +** query term. That is, the final term of the snippet is always a term +** that requires highlighting. For example, if 'X' is a highlighted term +** and '.' is a non-highlighted term, BestSnippet() may select: +** +** ........X.....X +** +** This function "shifts" the beginning of the snippet forward in the +** document so that there are approximately the same number of +** non-highlighted terms to the right of the final highlighted term as there +** are to the left of the first highlighted term. For example, to this: +** +** ....X.....X.... +** +** This is done as part of extracting the snippet text, not when selecting +** the snippet. Snippet selection is done based on doclists only, so there +** is no way for fts3BestSnippet() to know whether or not the document +** actually contains terms that follow the final highlighted term. +*/ +static int fts3SnippetShift( + Fts3Table *pTab, /* FTS3 table snippet comes from */ + int nSnippet, /* Number of tokens desired for snippet */ + const char *zDoc, /* Document text to extract snippet from */ + int nDoc, /* Size of buffer zDoc in bytes */ + int *piPos, /* IN/OUT: First token of snippet */ + u64 *pHlmask /* IN/OUT: Mask of tokens to highlight */ +){ + u64 hlmask = *pHlmask; /* Local copy of initial highlight-mask */ + + if( hlmask ){ + int nLeft; /* Tokens to the left of first highlight */ + int nRight; /* Tokens to the right of last highlight */ + int nDesired; /* Ideal number of tokens to shift forward */ + + for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++); + for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++); + nDesired = (nLeft-nRight)/2; + + /* Ideally, the start of the snippet should be pushed forward in the + ** document nDesired tokens. This block checks if there are actually + ** nDesired tokens to the right of the snippet. If so, *piPos and + ** *pHlMask are updated to shift the snippet nDesired tokens to the + ** right. Otherwise, the snippet is shifted by the number of tokens + ** available. + */ + if( nDesired>0 ){ + int nShift; /* Number of tokens to shift snippet by */ + int iCurrent = 0; /* Token counter */ + int rc; /* Return Code */ + sqlite3_tokenizer_module *pMod; + sqlite3_tokenizer_cursor *pC; + pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule; + + /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired) + ** or more tokens in zDoc/nDoc. + */ + rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC); + if( rc!=SQLITE_OK ){ + return rc; + } + pC->pTokenizer = pTab->pTokenizer; + while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){ + const char *ZDUMMY; int DUMMY1, DUMMY2, DUMMY3; + rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent); + } + pMod->xClose(pC); + if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; } + + nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet; + assert( nShift<=nDesired ); + if( nShift>0 ){ + *piPos += nShift; + *pHlmask = hlmask >> nShift; + } + } + } + return SQLITE_OK; +} + +/* +** Extract the snippet text for fragment pFragment from cursor pCsr and +** append it to string buffer pOut. +*/ +static int fts3SnippetText( + Fts3Cursor *pCsr, /* FTS3 Cursor */ + SnippetFragment *pFragment, /* Snippet to extract */ + int iFragment, /* Fragment number */ + int isLast, /* True for final fragment in snippet */ + int nSnippet, /* Number of tokens in extracted snippet */ + const char *zOpen, /* String inserted before highlighted term */ + const char *zClose, /* String inserted after highlighted term */ + const char *zEllipsis, /* String inserted between snippets */ + StrBuffer *pOut /* Write output here */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int rc; /* Return code */ + const char *zDoc; /* Document text to extract snippet from */ + int nDoc; /* Size of zDoc in bytes */ + int iCurrent = 0; /* Current token number of document */ + int iEnd = 0; /* Byte offset of end of current token */ + int isShiftDone = 0; /* True after snippet is shifted */ + int iPos = pFragment->iPos; /* First token of snippet */ + u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */ + int iCol = pFragment->iCol+1; /* Query column to extract text from */ + sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */ + sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor open on zDoc/nDoc */ + const char *ZDUMMY; /* Dummy argument used with tokenizer */ + int DUMMY1; /* Dummy argument used with tokenizer */ + + zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol); + if( zDoc==0 ){ + if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){ + return SQLITE_NOMEM; + } + return SQLITE_OK; + } + nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol); + + /* Open a token cursor on the document. */ + pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule; + rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC); + if( rc!=SQLITE_OK ){ + return rc; + } + pC->pTokenizer = pTab->pTokenizer; + + while( rc==SQLITE_OK ){ + int iBegin; /* Offset in zDoc of start of token */ + int iFin; /* Offset in zDoc of end of token */ + int isHighlight; /* True for highlighted terms */ + + rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent); + if( rc!=SQLITE_OK ){ + if( rc==SQLITE_DONE ){ + /* Special case - the last token of the snippet is also the last token + ** of the column. Append any punctuation that occurred between the end + ** of the previous token and the end of the document to the output. + ** Then break out of the loop. */ + rc = fts3StringAppend(pOut, &zDoc[iEnd], -1); + } + break; + } + if( iCurrent0 || iFragment>0) ){ + rc = fts3StringAppend(pOut, zEllipsis, -1); + } + if( rc!=SQLITE_OK || iCurrent=(iPos+nSnippet) ){ + if( isLast ){ + rc = fts3StringAppend(pOut, zEllipsis, -1); + } + break; + } + + /* Set isHighlight to true if this term should be highlighted. */ + isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0; + + if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd); + if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1); + if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin); + if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1); + + iEnd = iFin; + } + + pMod->xClose(pC); + return rc; +} + + +/* +** This function is used to count the entries in a column-list (a +** delta-encoded list of term offsets within a single column of a single +** row). When this function is called, *ppCollist should point to the +** beginning of the first varint in the column-list (the varint that +** contains the position of the first matching term in the column data). +** Before returning, *ppCollist is set to point to the first byte after +** the last varint in the column-list (either the 0x00 signifying the end +** of the position-list, or the 0x01 that precedes the column number of +** the next column in the position-list). +** +** The number of elements in the column-list is returned. +*/ +static int fts3ColumnlistCount(char **ppCollist){ + char *pEnd = *ppCollist; + char c = 0; + int nEntry = 0; + + /* A column-list is terminated by either a 0x01 or 0x00. */ + while( 0xFE & (*pEnd | c) ){ + c = *pEnd++ & 0x80; + if( !c ) nEntry++; + } + + *ppCollist = pEnd; + return nEntry; +} + +static void fts3LoadColumnlistCounts(char **pp, u32 *aOut, int isGlobal){ + char *pCsr = *pp; + while( *pCsr ){ + int nHit; + sqlite3_int64 iCol = 0; + if( *pCsr==0x01 ){ + pCsr++; + pCsr += sqlite3Fts3GetVarint(pCsr, &iCol); + } + nHit = fts3ColumnlistCount(&pCsr); + assert( nHit>0 ); + if( isGlobal ){ + aOut[iCol*3+1]++; + } + aOut[iCol*3] += nHit; + } + pCsr++; + *pp = pCsr; +} + +/* +** fts3ExprIterate() callback used to collect the "global" matchinfo stats +** for a single query. +** +** fts3ExprIterate() callback to load the 'global' elements of a +** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements +** of the matchinfo array that are constant for all rows returned by the +** current query. +** +** Argument pCtx is actually a pointer to a struct of type MatchInfo. This +** function populates Matchinfo.aMatchinfo[] as follows: +** +** for(iCol=0; iColpCursor; + char *pIter; + char *pEnd; + char *pFree = 0; + u32 *aOut = &p->aMatchinfo[3*iPhrase*p->nCol]; + + assert( pExpr->isLoaded ); + assert( pExpr->eType==FTSQUERY_PHRASE ); + + if( pCsr->pDeferred ){ + Fts3Phrase *pPhrase = pExpr->pPhrase; + int ii; + for(ii=0; iinToken; ii++){ + if( pPhrase->aToken[ii].bFulltext ) break; + } + if( iinToken ){ + int nFree = 0; + int rc = sqlite3Fts3ExprLoadFtDoclist(pCsr, pExpr, &pFree, &nFree); + if( rc!=SQLITE_OK ) return rc; + pIter = pFree; + pEnd = &pFree[nFree]; + }else{ + int iCol; /* Column index */ + for(iCol=0; iColnCol; iCol++){ + aOut[iCol*3 + 1] = (u32)p->nDoc; + aOut[iCol*3 + 2] = (u32)p->nDoc; + } + return SQLITE_OK; + } + }else{ + pIter = pExpr->aDoclist; + pEnd = &pExpr->aDoclist[pExpr->nDoclist]; + } + + /* Fill in the global hit count matrix row for this phrase. */ + while( pIteraDoclist ){ + char *pCsr; + int iStart = iPhrase * p->nCol * 3; + int i; + + for(i=0; inCol; i++) p->aMatchinfo[iStart+i*3] = 0; + + pCsr = sqlite3Fts3FindPositions(pExpr, p->pCursor->iPrevId, -1); + if( pCsr ){ + fts3LoadColumnlistCounts(&pCsr, &p->aMatchinfo[iStart], 0); + } + } + + return SQLITE_OK; +} + +static int fts3MatchinfoCheck( + Fts3Table *pTab, + char cArg, + char **pzErr +){ + if( (cArg==FTS3_MATCHINFO_NPHRASE) + || (cArg==FTS3_MATCHINFO_NCOL) + || (cArg==FTS3_MATCHINFO_NDOC && pTab->bHasStat) + || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bHasStat) + || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize) + || (cArg==FTS3_MATCHINFO_LCS) + || (cArg==FTS3_MATCHINFO_HITS) + ){ + return SQLITE_OK; + } + *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg); + return SQLITE_ERROR; +} + +static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){ + int nVal; /* Number of integers output by cArg */ + + switch( cArg ){ + case FTS3_MATCHINFO_NDOC: + case FTS3_MATCHINFO_NPHRASE: + case FTS3_MATCHINFO_NCOL: + nVal = 1; + break; + + case FTS3_MATCHINFO_AVGLENGTH: + case FTS3_MATCHINFO_LENGTH: + case FTS3_MATCHINFO_LCS: + nVal = pInfo->nCol; + break; + + default: + assert( cArg==FTS3_MATCHINFO_HITS ); + nVal = pInfo->nCol * pInfo->nPhrase * 3; + break; + } + + return nVal; +} + +static int fts3MatchinfoSelectDoctotal( + Fts3Table *pTab, + sqlite3_stmt **ppStmt, + sqlite3_int64 *pnDoc, + const char **paLen +){ + sqlite3_stmt *pStmt; + const char *a; + sqlite3_int64 nDoc; + + if( !*ppStmt ){ + int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt); + if( rc!=SQLITE_OK ) return rc; + } + pStmt = *ppStmt; + assert( sqlite3_data_count(pStmt)==1 ); + + a = sqlite3_column_blob(pStmt, 0); + a += sqlite3Fts3GetVarint(a, &nDoc); + if( nDoc==0 ) return SQLITE_CORRUPT; + *pnDoc = (u32)nDoc; + + if( paLen ) *paLen = a; + return SQLITE_OK; +} + +/* +** An instance of the following structure is used to store state while +** iterating through a multi-column position-list corresponding to the +** hits for a single phrase on a single row in order to calculate the +** values for a matchinfo() FTS3_MATCHINFO_LCS request. +*/ +typedef struct LcsIterator LcsIterator; +struct LcsIterator { + Fts3Expr *pExpr; /* Pointer to phrase expression */ + char *pRead; /* Cursor used to iterate through aDoclist */ + int iPosOffset; /* Tokens count up to end of this phrase */ + int iCol; /* Current column number */ + int iPos; /* Current position */ +}; + +/* +** If LcsIterator.iCol is set to the following value, the iterator has +** finished iterating through all offsets for all columns. +*/ +#define LCS_ITERATOR_FINISHED 0x7FFFFFFF; + +static int fts3MatchinfoLcsCb( + Fts3Expr *pExpr, /* Phrase expression node */ + int iPhrase, /* Phrase number (numbered from zero) */ + void *pCtx /* Pointer to MatchInfo structure */ +){ + LcsIterator *aIter = (LcsIterator *)pCtx; + aIter[iPhrase].pExpr = pExpr; + return SQLITE_OK; +} + +/* +** Advance the iterator passed as an argument to the next position. Return +** 1 if the iterator is at EOF or if it now points to the start of the +** position list for the next column. +*/ +static int fts3LcsIteratorAdvance(LcsIterator *pIter){ + char *pRead = pIter->pRead; + sqlite3_int64 iRead; + int rc = 0; + + pRead += sqlite3Fts3GetVarint(pRead, &iRead); + if( iRead==0 ){ + pIter->iCol = LCS_ITERATOR_FINISHED; + rc = 1; + }else{ + if( iRead==1 ){ + pRead += sqlite3Fts3GetVarint(pRead, &iRead); + pIter->iCol = (int)iRead; + pIter->iPos = pIter->iPosOffset; + pRead += sqlite3Fts3GetVarint(pRead, &iRead); + rc = 1; + } + pIter->iPos += (int)(iRead-2); + } + + pIter->pRead = pRead; + return rc; +} + +/* +** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag. +** +** If the call is successful, the longest-common-substring lengths for each +** column are written into the first nCol elements of the pInfo->aMatchinfo[] +** array before returning. SQLITE_OK is returned in this case. +** +** Otherwise, if an error occurs, an SQLite error code is returned and the +** data written to the first nCol elements of pInfo->aMatchinfo[] is +** undefined. +*/ +static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){ + LcsIterator *aIter; + int i; + int iCol; + int nToken = 0; + + /* Allocate and populate the array of LcsIterator objects. The array + ** contains one element for each matchable phrase in the query. + **/ + aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase); + if( !aIter ) return SQLITE_NOMEM; + memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase); + (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter); + for(i=0; inPhrase; i++){ + LcsIterator *pIter = &aIter[i]; + nToken -= pIter->pExpr->pPhrase->nToken; + pIter->iPosOffset = nToken; + pIter->pRead = sqlite3Fts3FindPositions(pIter->pExpr, pCsr->iPrevId, -1); + if( pIter->pRead ){ + pIter->iPos = pIter->iPosOffset; + fts3LcsIteratorAdvance(&aIter[i]); + }else{ + pIter->iCol = LCS_ITERATOR_FINISHED; + } + } + + for(iCol=0; iColnCol; iCol++){ + int nLcs = 0; /* LCS value for this column */ + int nLive = 0; /* Number of iterators in aIter not at EOF */ + + /* Loop through the iterators in aIter[]. Set nLive to the number of + ** iterators that point to a position-list corresponding to column iCol. + */ + for(i=0; inPhrase; i++){ + assert( aIter[i].iCol>=iCol ); + if( aIter[i].iCol==iCol ) nLive++; + } + + /* The following loop runs until all iterators in aIter[] have finished + ** iterating through positions in column iCol. Exactly one of the + ** iterators is advanced each time the body of the loop is run. + */ + while( nLive>0 ){ + LcsIterator *pAdv = 0; /* The iterator to advance by one position */ + int nThisLcs = 0; /* LCS for the current iterator positions */ + + for(i=0; inPhrase; i++){ + LcsIterator *pIter = &aIter[i]; + if( iCol!=pIter->iCol ){ + /* This iterator is already at EOF for this column. */ + nThisLcs = 0; + }else{ + if( pAdv==0 || pIter->iPosiPos ){ + pAdv = pIter; + } + if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){ + nThisLcs++; + }else{ + nThisLcs = 1; + } + if( nThisLcs>nLcs ) nLcs = nThisLcs; + } + } + if( fts3LcsIteratorAdvance(pAdv) ) nLive--; + } + + pInfo->aMatchinfo[iCol] = nLcs; + } + + sqlite3_free(aIter); + return SQLITE_OK; +} + +/* +** Populate the buffer pInfo->aMatchinfo[] with an array of integers to +** be returned by the matchinfo() function. Argument zArg contains the +** format string passed as the second argument to matchinfo (or the +** default value "pcx" if no second argument was specified). The format +** string has already been validated and the pInfo->aMatchinfo[] array +** is guaranteed to be large enough for the output. +** +** If bGlobal is true, then populate all fields of the matchinfo() output. +** If it is false, then assume that those fields that do not change between +** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS) +** have already been populated. +** +** Return SQLITE_OK if successful, or an SQLite error code if an error +** occurs. If a value other than SQLITE_OK is returned, the state the +** pInfo->aMatchinfo[] buffer is left in is undefined. +*/ +static int fts3MatchinfoValues( + Fts3Cursor *pCsr, /* FTS3 cursor object */ + int bGlobal, /* True to grab the global stats */ + MatchInfo *pInfo, /* Matchinfo context object */ + const char *zArg /* Matchinfo format string */ +){ + int rc = SQLITE_OK; + int i; + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + sqlite3_stmt *pSelect = 0; + + for(i=0; rc==SQLITE_OK && zArg[i]; i++){ + + switch( zArg[i] ){ + case FTS3_MATCHINFO_NPHRASE: + if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase; + break; + + case FTS3_MATCHINFO_NCOL: + if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol; + break; + + case FTS3_MATCHINFO_NDOC: + if( bGlobal ){ + sqlite3_int64 nDoc; + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0); + pInfo->aMatchinfo[0] = (u32)nDoc; + } + break; + + case FTS3_MATCHINFO_AVGLENGTH: + if( bGlobal ){ + sqlite3_int64 nDoc; /* Number of rows in table */ + const char *a; /* Aggregate column length array */ + + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a); + if( rc==SQLITE_OK ){ + int iCol; + for(iCol=0; iColnCol; iCol++){ + u32 iVal; + sqlite3_int64 nToken; + a += sqlite3Fts3GetVarint(a, &nToken); + iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc); + pInfo->aMatchinfo[iCol] = iVal; + } + } + } + break; + + case FTS3_MATCHINFO_LENGTH: { + sqlite3_stmt *pSelectDocsize = 0; + rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize); + if( rc==SQLITE_OK ){ + int iCol; + const char *a = sqlite3_column_blob(pSelectDocsize, 0); + for(iCol=0; iColnCol; iCol++){ + sqlite3_int64 nToken; + a += sqlite3Fts3GetVarint(a, &nToken); + pInfo->aMatchinfo[iCol] = (u32)nToken; + } + } + sqlite3_reset(pSelectDocsize); + break; + } + + case FTS3_MATCHINFO_LCS: + rc = fts3ExprLoadDoclists(pCsr, 0, 0); + if( rc==SQLITE_OK ){ + rc = fts3MatchinfoLcs(pCsr, pInfo); + } + break; + + default: { + Fts3Expr *pExpr; + assert( zArg[i]==FTS3_MATCHINFO_HITS ); + pExpr = pCsr->pExpr; + rc = fts3ExprLoadDoclists(pCsr, 0, 0); + if( rc!=SQLITE_OK ) break; + if( bGlobal ){ + if( pCsr->pDeferred ){ + rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0); + if( rc!=SQLITE_OK ) break; + } + rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo); + if( rc!=SQLITE_OK ) break; + } + (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo); + break; + } + } + + pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]); + } + + sqlite3_reset(pSelect); + return rc; +} + + +/* +** Populate pCsr->aMatchinfo[] with data for the current row. The +** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32). +*/ +static int fts3GetMatchinfo( + Fts3Cursor *pCsr, /* FTS3 Cursor object */ + const char *zArg /* Second argument to matchinfo() function */ +){ + MatchInfo sInfo; + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int rc = SQLITE_OK; + int bGlobal = 0; /* Collect 'global' stats as well as local */ + + memset(&sInfo, 0, sizeof(MatchInfo)); + sInfo.pCursor = pCsr; + sInfo.nCol = pTab->nColumn; + + /* If there is cached matchinfo() data, but the format string for the + ** cache does not match the format string for this request, discard + ** the cached data. */ + if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){ + assert( pCsr->aMatchinfo ); + sqlite3_free(pCsr->aMatchinfo); + pCsr->zMatchinfo = 0; + pCsr->aMatchinfo = 0; + } + + /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the + ** matchinfo function has been called for this query. In this case + ** allocate the array used to accumulate the matchinfo data and + ** initialize those elements that are constant for every row. + */ + if( pCsr->aMatchinfo==0 ){ + int nMatchinfo = 0; /* Number of u32 elements in match-info */ + int nArg; /* Bytes in zArg */ + int i; /* Used to iterate through zArg */ + + /* Determine the number of phrases in the query */ + pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr); + sInfo.nPhrase = pCsr->nPhrase; + + /* Determine the number of integers in the buffer returned by this call. */ + for(i=0; zArg[i]; i++){ + nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]); + } + + /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */ + nArg = (int)strlen(zArg); + pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1); + if( !pCsr->aMatchinfo ) return SQLITE_NOMEM; + + pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo]; + pCsr->nMatchinfo = nMatchinfo; + memcpy(pCsr->zMatchinfo, zArg, nArg+1); + memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo); + pCsr->isMatchinfoNeeded = 1; + bGlobal = 1; + } + + sInfo.aMatchinfo = pCsr->aMatchinfo; + sInfo.nPhrase = pCsr->nPhrase; + if( pCsr->isMatchinfoNeeded ){ + rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg); + pCsr->isMatchinfoNeeded = 0; + } + + return rc; +} + +/* +** Implementation of snippet() function. +*/ +SQLITE_PRIVATE void sqlite3Fts3Snippet( + sqlite3_context *pCtx, /* SQLite function call context */ + Fts3Cursor *pCsr, /* Cursor object */ + const char *zStart, /* Snippet start text - "" */ + const char *zEnd, /* Snippet end text - "" */ + const char *zEllipsis, /* Snippet ellipsis text - "..." */ + int iCol, /* Extract snippet from this column */ + int nToken /* Approximate number of tokens in snippet */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int rc = SQLITE_OK; + int i; + StrBuffer res = {0, 0, 0}; + + /* The returned text includes up to four fragments of text extracted from + ** the data in the current row. The first iteration of the for(...) loop + ** below attempts to locate a single fragment of text nToken tokens in + ** size that contains at least one instance of all phrases in the query + ** expression that appear in the current row. If such a fragment of text + ** cannot be found, the second iteration of the loop attempts to locate + ** a pair of fragments, and so on. + */ + int nSnippet = 0; /* Number of fragments in this snippet */ + SnippetFragment aSnippet[4]; /* Maximum of 4 fragments per snippet */ + int nFToken = -1; /* Number of tokens in each fragment */ + + if( !pCsr->pExpr ){ + sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC); + return; + } + + for(nSnippet=1; 1; nSnippet++){ + + int iSnip; /* Loop counter 0..nSnippet-1 */ + u64 mCovered = 0; /* Bitmask of phrases covered by snippet */ + u64 mSeen = 0; /* Bitmask of phrases seen by BestSnippet() */ + + if( nToken>=0 ){ + nFToken = (nToken+nSnippet-1) / nSnippet; + }else{ + nFToken = -1 * nToken; + } + + for(iSnip=0; iSnipnColumn; iRead++){ + SnippetFragment sF = {0, 0, 0, 0}; + int iS; + if( iCol>=0 && iRead!=iCol ) continue; + + /* Find the best snippet of nFToken tokens in column iRead. */ + rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS); + if( rc!=SQLITE_OK ){ + goto snippet_out; + } + if( iS>iBestScore ){ + *pFragment = sF; + iBestScore = iS; + } + } + + mCovered |= pFragment->covered; + } + + /* If all query phrases seen by fts3BestSnippet() are present in at least + ** one of the nSnippet snippet fragments, break out of the loop. + */ + assert( (mCovered&mSeen)==mCovered ); + if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break; + } + + assert( nFToken>0 ); + + for(i=0; iiDocid, p->iCol); + nTerm = pExpr->pPhrase->nToken; + if( pList ){ + fts3GetDeltaPosition(&pList, &iPos); + assert( iPos>=0 ); + } + + for(iTerm=0; iTermaTerm[p->iTerm++]; + pT->iOff = nTerm-iTerm-1; + pT->pList = pList; + pT->iPos = iPos; + } + + return SQLITE_OK; +} + +/* +** Implementation of offsets() function. +*/ +SQLITE_PRIVATE void sqlite3Fts3Offsets( + sqlite3_context *pCtx, /* SQLite function call context */ + Fts3Cursor *pCsr /* Cursor object */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule; + const char *ZDUMMY; /* Dummy argument used with xNext() */ + int NDUMMY; /* Dummy argument used with xNext() */ + int rc; /* Return Code */ + int nToken; /* Number of tokens in query */ + int iCol; /* Column currently being processed */ + StrBuffer res = {0, 0, 0}; /* Result string */ + TermOffsetCtx sCtx; /* Context for fts3ExprTermOffsetInit() */ + + if( !pCsr->pExpr ){ + sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC); + return; + } + + memset(&sCtx, 0, sizeof(sCtx)); + assert( pCsr->isRequireSeek==0 ); + + /* Count the number of terms in the query */ + rc = fts3ExprLoadDoclists(pCsr, 0, &nToken); + if( rc!=SQLITE_OK ) goto offsets_out; + + /* Allocate the array of TermOffset iterators. */ + sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken); + if( 0==sCtx.aTerm ){ + rc = SQLITE_NOMEM; + goto offsets_out; + } + sCtx.iDocid = pCsr->iPrevId; + + /* Loop through the table columns, appending offset information to + ** string-buffer res for each column. + */ + for(iCol=0; iColnColumn; iCol++){ + sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */ + int iStart; + int iEnd; + int iCurrent; + const char *zDoc; + int nDoc; + + /* Initialize the contents of sCtx.aTerm[] for column iCol. There is + ** no way that this operation can fail, so the return code from + ** fts3ExprIterate() can be discarded. + */ + sCtx.iCol = iCol; + sCtx.iTerm = 0; + (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx); + + /* Retreive the text stored in column iCol. If an SQL NULL is stored + ** in column iCol, jump immediately to the next iteration of the loop. + ** If an OOM occurs while retrieving the data (this can happen if SQLite + ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM + ** to the caller. + */ + zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1); + nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1); + if( zDoc==0 ){ + if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){ + continue; + } + rc = SQLITE_NOMEM; + goto offsets_out; + } + + /* Initialize a tokenizer iterator to iterate through column iCol. */ + rc = pMod->xOpen(pTab->pTokenizer, zDoc, nDoc, &pC); + if( rc!=SQLITE_OK ) goto offsets_out; + pC->pTokenizer = pTab->pTokenizer; + + rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent); + while( rc==SQLITE_OK ){ + int i; /* Used to loop through terms */ + int iMinPos = 0x7FFFFFFF; /* Position of next token */ + TermOffset *pTerm = 0; /* TermOffset associated with next token */ + + for(i=0; ipList && (pT->iPos-pT->iOff)iPos-pT->iOff; + pTerm = pT; + } + } + + if( !pTerm ){ + /* All offsets for this column have been gathered. */ + break; + }else{ + assert( iCurrent<=iMinPos ); + if( 0==(0xFE&*pTerm->pList) ){ + pTerm->pList = 0; + }else{ + fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos); + } + while( rc==SQLITE_OK && iCurrentxNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent); + } + if( rc==SQLITE_OK ){ + char aBuffer[64]; + sqlite3_snprintf(sizeof(aBuffer), aBuffer, + "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart + ); + rc = fts3StringAppend(&res, aBuffer, -1); + }else if( rc==SQLITE_DONE ){ + rc = SQLITE_CORRUPT; + } + } + } + if( rc==SQLITE_DONE ){ + rc = SQLITE_OK; + } + + pMod->xClose(pC); + if( rc!=SQLITE_OK ) goto offsets_out; + } + + offsets_out: + sqlite3_free(sCtx.aTerm); + assert( rc!=SQLITE_DONE ); + sqlite3Fts3SegmentsClose(pTab); + if( rc!=SQLITE_OK ){ + sqlite3_result_error_code(pCtx, rc); + sqlite3_free(res.z); + }else{ + sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free); + } + return; +} + +/* +** Implementation of matchinfo() function. +*/ +SQLITE_PRIVATE void sqlite3Fts3Matchinfo( + sqlite3_context *pContext, /* Function call context */ + Fts3Cursor *pCsr, /* FTS3 table cursor */ + const char *zArg /* Second arg to matchinfo() function */ +){ + Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab; + int rc; + int i; + const char *zFormat; + + if( zArg ){ + for(i=0; zArg[i]; i++){ + char *zErr = 0; + if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){ + sqlite3_result_error(pContext, zErr, -1); + sqlite3_free(zErr); + return; + } + } + zFormat = zArg; + }else{ + zFormat = FTS3_MATCHINFO_DEFAULT; + } + + if( !pCsr->pExpr ){ + sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC); + return; + } + + /* Retrieve matchinfo() data. */ + rc = fts3GetMatchinfo(pCsr, zFormat); + sqlite3Fts3SegmentsClose(pTab); + + if( rc!=SQLITE_OK ){ + sqlite3_result_error_code(pContext, rc); + }else{ + int n = pCsr->nMatchinfo * sizeof(u32); + sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT); + } +} + +#endif + +/************** End of fts3_snippet.c ****************************************/ +/************** Begin file rtree.c *******************************************/ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file contains code for implementations of the r-tree and r*-tree +** algorithms packaged as an SQLite virtual table module. +*/ + +/* +** Database Format of R-Tree Tables +** -------------------------------- +** +** The data structure for a single virtual r-tree table is stored in three +** native SQLite tables declared as follows. In each case, the '%' character +** in the table name is replaced with the user-supplied name of the r-tree +** table. +** +** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB) +** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER) +** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER) +** +** The data for each node of the r-tree structure is stored in the %_node +** table. For each node that is not the root node of the r-tree, there is +** an entry in the %_parent table associating the node with its parent. +** And for each row of data in the table, there is an entry in the %_rowid +** table that maps from the entries rowid to the id of the node that it +** is stored on. +** +** The root node of an r-tree always exists, even if the r-tree table is +** empty. The nodeno of the root node is always 1. All other nodes in the +** table must be the same size as the root node. The content of each node +** is formatted as follows: +** +** 1. If the node is the root node (node 1), then the first 2 bytes +** of the node contain the tree depth as a big-endian integer. +** For non-root nodes, the first 2 bytes are left unused. +** +** 2. The next 2 bytes contain the number of entries currently +** stored in the node. +** +** 3. The remainder of the node contains the node entries. Each entry +** consists of a single 8-byte integer followed by an even number +** of 4-byte coordinates. For leaf nodes the integer is the rowid +** of a record. For internal nodes it is the node number of a +** child page. +*/ + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE) + +/* +** This file contains an implementation of a couple of different variants +** of the r-tree algorithm. See the README file for further details. The +** same data-structure is used for all, but the algorithms for insert and +** delete operations vary. The variants used are selected at compile time +** by defining the following symbols: +*/ + +/* Either, both or none of the following may be set to activate +** r*tree variant algorithms. +*/ +#define VARIANT_RSTARTREE_CHOOSESUBTREE 0 +#define VARIANT_RSTARTREE_REINSERT 1 + +/* +** Exactly one of the following must be set to 1. +*/ +#define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0 +#define VARIANT_GUTTMAN_LINEAR_SPLIT 0 +#define VARIANT_RSTARTREE_SPLIT 1 + +#define VARIANT_GUTTMAN_SPLIT \ + (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT) + +#if VARIANT_GUTTMAN_QUADRATIC_SPLIT + #define PickNext QuadraticPickNext + #define PickSeeds QuadraticPickSeeds + #define AssignCells splitNodeGuttman +#endif +#if VARIANT_GUTTMAN_LINEAR_SPLIT + #define PickNext LinearPickNext + #define PickSeeds LinearPickSeeds + #define AssignCells splitNodeGuttman +#endif +#if VARIANT_RSTARTREE_SPLIT + #define AssignCells splitNodeStartree +#endif + +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) +# define NDEBUG 1 +#endif + +#ifndef SQLITE_CORE + SQLITE_EXTENSION_INIT1 +#else +#endif + + +#ifndef SQLITE_AMALGAMATION +#include "sqlite3rtree.h" +typedef sqlite3_int64 i64; +typedef unsigned char u8; +typedef unsigned int u32; +#endif + +/* The following macro is used to suppress compiler warnings. +*/ +#ifndef UNUSED_PARAMETER +# define UNUSED_PARAMETER(x) (void)(x) +#endif + +typedef struct Rtree Rtree; +typedef struct RtreeCursor RtreeCursor; +typedef struct RtreeNode RtreeNode; +typedef struct RtreeCell RtreeCell; +typedef struct RtreeConstraint RtreeConstraint; +typedef struct RtreeMatchArg RtreeMatchArg; +typedef struct RtreeGeomCallback RtreeGeomCallback; +typedef union RtreeCoord RtreeCoord; + +/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */ +#define RTREE_MAX_DIMENSIONS 5 + +/* Size of hash table Rtree.aHash. This hash table is not expected to +** ever contain very many entries, so a fixed number of buckets is +** used. +*/ +#define HASHSIZE 128 + +/* +** An rtree virtual-table object. +*/ +struct Rtree { + sqlite3_vtab base; + sqlite3 *db; /* Host database connection */ + int iNodeSize; /* Size in bytes of each node in the node table */ + int nDim; /* Number of dimensions */ + int nBytesPerCell; /* Bytes consumed per cell */ + int iDepth; /* Current depth of the r-tree structure */ + char *zDb; /* Name of database containing r-tree table */ + char *zName; /* Name of r-tree table */ + RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ + int nBusy; /* Current number of users of this structure */ + + /* List of nodes removed during a CondenseTree operation. List is + ** linked together via the pointer normally used for hash chains - + ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree + ** headed by the node (leaf nodes have RtreeNode.iNode==0). + */ + RtreeNode *pDeleted; + int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */ + + /* Statements to read/write/delete a record from xxx_node */ + sqlite3_stmt *pReadNode; + sqlite3_stmt *pWriteNode; + sqlite3_stmt *pDeleteNode; + + /* Statements to read/write/delete a record from xxx_rowid */ + sqlite3_stmt *pReadRowid; + sqlite3_stmt *pWriteRowid; + sqlite3_stmt *pDeleteRowid; + + /* Statements to read/write/delete a record from xxx_parent */ + sqlite3_stmt *pReadParent; + sqlite3_stmt *pWriteParent; + sqlite3_stmt *pDeleteParent; + + int eCoordType; +}; + +/* Possible values for eCoordType: */ +#define RTREE_COORD_REAL32 0 +#define RTREE_COORD_INT32 1 + +/* +** The minimum number of cells allowed for a node is a third of the +** maximum. In Gutman's notation: +** +** m = M/3 +** +** If an R*-tree "Reinsert" operation is required, the same number of +** cells are removed from the overfull node and reinserted into the tree. +*/ +#define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) +#define RTREE_REINSERT(p) RTREE_MINCELLS(p) +#define RTREE_MAXCELLS 51 + +/* +** The smallest possible node-size is (512-64)==448 bytes. And the largest +** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates). +** Therefore all non-root nodes must contain at least 3 entries. Since +** 2^40 is greater than 2^64, an r-tree structure always has a depth of +** 40 or less. +*/ +#define RTREE_MAX_DEPTH 40 + +/* +** An rtree cursor object. +*/ +struct RtreeCursor { + sqlite3_vtab_cursor base; + RtreeNode *pNode; /* Node cursor is currently pointing at */ + int iCell; /* Index of current cell in pNode */ + int iStrategy; /* Copy of idxNum search parameter */ + int nConstraint; /* Number of entries in aConstraint */ + RtreeConstraint *aConstraint; /* Search constraints. */ +}; + +union RtreeCoord { + float f; + int i; +}; + +/* +** The argument is an RtreeCoord. Return the value stored within the RtreeCoord +** formatted as a double. This macro assumes that local variable pRtree points +** to the Rtree structure associated with the RtreeCoord. +*/ +#define DCOORD(coord) ( \ + (pRtree->eCoordType==RTREE_COORD_REAL32) ? \ + ((double)coord.f) : \ + ((double)coord.i) \ +) + +/* +** A search constraint. +*/ +struct RtreeConstraint { + int iCoord; /* Index of constrained coordinate */ + int op; /* Constraining operation */ + double rValue; /* Constraint value. */ + int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *); + sqlite3_rtree_geometry *pGeom; /* Constraint callback argument for a MATCH */ +}; + +/* Possible values for RtreeConstraint.op */ +#define RTREE_EQ 0x41 +#define RTREE_LE 0x42 +#define RTREE_LT 0x43 +#define RTREE_GE 0x44 +#define RTREE_GT 0x45 +#define RTREE_MATCH 0x46 + +/* +** An rtree structure node. +*/ +struct RtreeNode { + RtreeNode *pParent; /* Parent node */ + i64 iNode; + int nRef; + int isDirty; + u8 *zData; + RtreeNode *pNext; /* Next node in this hash chain */ +}; +#define NCELL(pNode) readInt16(&(pNode)->zData[2]) + +/* +** Structure to store a deserialized rtree record. +*/ +struct RtreeCell { + i64 iRowid; + RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; +}; + + +/* +** Value for the first field of every RtreeMatchArg object. The MATCH +** operator tests that the first field of a blob operand matches this +** value to avoid operating on invalid blobs (which could cause a segfault). +*/ +#define RTREE_GEOMETRY_MAGIC 0x891245AB + +/* +** An instance of this structure must be supplied as a blob argument to +** the right-hand-side of an SQL MATCH operator used to constrain an +** r-tree query. +*/ +struct RtreeMatchArg { + u32 magic; /* Always RTREE_GEOMETRY_MAGIC */ + int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *); + void *pContext; + int nParam; + double aParam[1]; +}; + +/* +** When a geometry callback is created (see sqlite3_rtree_geometry_callback), +** a single instance of the following structure is allocated. It is used +** as the context for the user-function created by by s_r_g_c(). The object +** is eventually deleted by the destructor mechanism provided by +** sqlite3_create_function_v2() (which is called by s_r_g_c() to create +** the geometry callback function). +*/ +struct RtreeGeomCallback { + int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *); + void *pContext; +}; + +#ifndef MAX +# define MAX(x,y) ((x) < (y) ? (y) : (x)) +#endif +#ifndef MIN +# define MIN(x,y) ((x) > (y) ? (y) : (x)) +#endif + +/* +** Functions to deserialize a 16 bit integer, 32 bit real number and +** 64 bit integer. The deserialized value is returned. +*/ +static int readInt16(u8 *p){ + return (p[0]<<8) + p[1]; +} +static void readCoord(u8 *p, RtreeCoord *pCoord){ + u32 i = ( + (((u32)p[0]) << 24) + + (((u32)p[1]) << 16) + + (((u32)p[2]) << 8) + + (((u32)p[3]) << 0) + ); + *(u32 *)pCoord = i; +} +static i64 readInt64(u8 *p){ + return ( + (((i64)p[0]) << 56) + + (((i64)p[1]) << 48) + + (((i64)p[2]) << 40) + + (((i64)p[3]) << 32) + + (((i64)p[4]) << 24) + + (((i64)p[5]) << 16) + + (((i64)p[6]) << 8) + + (((i64)p[7]) << 0) + ); +} + +/* +** Functions to serialize a 16 bit integer, 32 bit real number and +** 64 bit integer. The value returned is the number of bytes written +** to the argument buffer (always 2, 4 and 8 respectively). +*/ +static int writeInt16(u8 *p, int i){ + p[0] = (i>> 8)&0xFF; + p[1] = (i>> 0)&0xFF; + return 2; +} +static int writeCoord(u8 *p, RtreeCoord *pCoord){ + u32 i; + assert( sizeof(RtreeCoord)==4 ); + assert( sizeof(u32)==4 ); + i = *(u32 *)pCoord; + p[0] = (i>>24)&0xFF; + p[1] = (i>>16)&0xFF; + p[2] = (i>> 8)&0xFF; + p[3] = (i>> 0)&0xFF; + return 4; +} +static int writeInt64(u8 *p, i64 i){ + p[0] = (i>>56)&0xFF; + p[1] = (i>>48)&0xFF; + p[2] = (i>>40)&0xFF; + p[3] = (i>>32)&0xFF; + p[4] = (i>>24)&0xFF; + p[5] = (i>>16)&0xFF; + p[6] = (i>> 8)&0xFF; + p[7] = (i>> 0)&0xFF; + return 8; +} + +/* +** Increment the reference count of node p. +*/ +static void nodeReference(RtreeNode *p){ + if( p ){ + p->nRef++; + } +} + +/* +** Clear the content of node p (set all bytes to 0x00). +*/ +static void nodeZero(Rtree *pRtree, RtreeNode *p){ + memset(&p->zData[2], 0, pRtree->iNodeSize-2); + p->isDirty = 1; +} + +/* +** Given a node number iNode, return the corresponding key to use +** in the Rtree.aHash table. +*/ +static int nodeHash(i64 iNode){ + return ( + (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^ + (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0) + ) % HASHSIZE; +} + +/* +** Search the node hash table for node iNode. If found, return a pointer +** to it. Otherwise, return 0. +*/ +static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){ + RtreeNode *p; + for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext); + return p; +} + +/* +** Add node pNode to the node hash table. +*/ +static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){ + int iHash; + assert( pNode->pNext==0 ); + iHash = nodeHash(pNode->iNode); + pNode->pNext = pRtree->aHash[iHash]; + pRtree->aHash[iHash] = pNode; +} + +/* +** Remove node pNode from the node hash table. +*/ +static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){ + RtreeNode **pp; + if( pNode->iNode!=0 ){ + pp = &pRtree->aHash[nodeHash(pNode->iNode)]; + for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); } + *pp = pNode->pNext; + pNode->pNext = 0; + } +} + +/* +** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0), +** indicating that node has not yet been assigned a node number. It is +** assigned a node number when nodeWrite() is called to write the +** node contents out to the database. +*/ +static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){ + RtreeNode *pNode; + pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize); + if( pNode ){ + memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize); + pNode->zData = (u8 *)&pNode[1]; + pNode->nRef = 1; + pNode->pParent = pParent; + pNode->isDirty = 1; + nodeReference(pParent); + } + return pNode; +} + +/* +** Obtain a reference to an r-tree node. +*/ +static int +nodeAcquire( + Rtree *pRtree, /* R-tree structure */ + i64 iNode, /* Node number to load */ + RtreeNode *pParent, /* Either the parent node or NULL */ + RtreeNode **ppNode /* OUT: Acquired node */ +){ + int rc; + int rc2 = SQLITE_OK; + RtreeNode *pNode; + + /* Check if the requested node is already in the hash table. If so, + ** increase its reference count and return it. + */ + if( (pNode = nodeHashLookup(pRtree, iNode)) ){ + assert( !pParent || !pNode->pParent || pNode->pParent==pParent ); + if( pParent && !pNode->pParent ){ + nodeReference(pParent); + pNode->pParent = pParent; + } + pNode->nRef++; + *ppNode = pNode; + return SQLITE_OK; + } + + sqlite3_bind_int64(pRtree->pReadNode, 1, iNode); + rc = sqlite3_step(pRtree->pReadNode); + if( rc==SQLITE_ROW ){ + const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0); + if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){ + pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize); + if( !pNode ){ + rc2 = SQLITE_NOMEM; + }else{ + pNode->pParent = pParent; + pNode->zData = (u8 *)&pNode[1]; + pNode->nRef = 1; + pNode->iNode = iNode; + pNode->isDirty = 0; + pNode->pNext = 0; + memcpy(pNode->zData, zBlob, pRtree->iNodeSize); + nodeReference(pParent); + } + } + } + rc = sqlite3_reset(pRtree->pReadNode); + if( rc==SQLITE_OK ) rc = rc2; + + /* If the root node was just loaded, set pRtree->iDepth to the height + ** of the r-tree structure. A height of zero means all data is stored on + ** the root node. A height of one means the children of the root node + ** are the leaves, and so on. If the depth as specified on the root node + ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt. + */ + if( pNode && iNode==1 ){ + pRtree->iDepth = readInt16(pNode->zData); + if( pRtree->iDepth>RTREE_MAX_DEPTH ){ + rc = SQLITE_CORRUPT; + } + } + + /* If no error has occurred so far, check if the "number of entries" + ** field on the node is too large. If so, set the return code to + ** SQLITE_CORRUPT. + */ + if( pNode && rc==SQLITE_OK ){ + if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){ + rc = SQLITE_CORRUPT; + } + } + + if( rc==SQLITE_OK ){ + if( pNode!=0 ){ + nodeHashInsert(pRtree, pNode); + }else{ + rc = SQLITE_CORRUPT; + } + *ppNode = pNode; + }else{ + sqlite3_free(pNode); + *ppNode = 0; + } + + return rc; +} + +/* +** Overwrite cell iCell of node pNode with the contents of pCell. +*/ +static void nodeOverwriteCell( + Rtree *pRtree, + RtreeNode *pNode, + RtreeCell *pCell, + int iCell +){ + int ii; + u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell]; + p += writeInt64(p, pCell->iRowid); + for(ii=0; ii<(pRtree->nDim*2); ii++){ + p += writeCoord(p, &pCell->aCoord[ii]); + } + pNode->isDirty = 1; +} + +/* +** Remove cell the cell with index iCell from node pNode. +*/ +static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){ + u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell]; + u8 *pSrc = &pDst[pRtree->nBytesPerCell]; + int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell; + memmove(pDst, pSrc, nByte); + writeInt16(&pNode->zData[2], NCELL(pNode)-1); + pNode->isDirty = 1; +} + +/* +** Insert the contents of cell pCell into node pNode. If the insert +** is successful, return SQLITE_OK. +** +** If there is not enough free space in pNode, return SQLITE_FULL. +*/ +static int +nodeInsertCell( + Rtree *pRtree, + RtreeNode *pNode, + RtreeCell *pCell +){ + int nCell; /* Current number of cells in pNode */ + int nMaxCell; /* Maximum number of cells for pNode */ + + nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell; + nCell = NCELL(pNode); + + assert( nCell<=nMaxCell ); + if( nCellzData[2], nCell+1); + pNode->isDirty = 1; + } + + return (nCell==nMaxCell); +} + +/* +** If the node is dirty, write it out to the database. +*/ +static int +nodeWrite(Rtree *pRtree, RtreeNode *pNode){ + int rc = SQLITE_OK; + if( pNode->isDirty ){ + sqlite3_stmt *p = pRtree->pWriteNode; + if( pNode->iNode ){ + sqlite3_bind_int64(p, 1, pNode->iNode); + }else{ + sqlite3_bind_null(p, 1); + } + sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC); + sqlite3_step(p); + pNode->isDirty = 0; + rc = sqlite3_reset(p); + if( pNode->iNode==0 && rc==SQLITE_OK ){ + pNode->iNode = sqlite3_last_insert_rowid(pRtree->db); + nodeHashInsert(pRtree, pNode); + } + } + return rc; +} + +/* +** Release a reference to a node. If the node is dirty and the reference +** count drops to zero, the node data is written to the database. +*/ +static int +nodeRelease(Rtree *pRtree, RtreeNode *pNode){ + int rc = SQLITE_OK; + if( pNode ){ + assert( pNode->nRef>0 ); + pNode->nRef--; + if( pNode->nRef==0 ){ + if( pNode->iNode==1 ){ + pRtree->iDepth = -1; + } + if( pNode->pParent ){ + rc = nodeRelease(pRtree, pNode->pParent); + } + if( rc==SQLITE_OK ){ + rc = nodeWrite(pRtree, pNode); + } + nodeHashDelete(pRtree, pNode); + sqlite3_free(pNode); + } + } + return rc; +} + +/* +** Return the 64-bit integer value associated with cell iCell of +** node pNode. If pNode is a leaf node, this is a rowid. If it is +** an internal node, then the 64-bit integer is a child page number. +*/ +static i64 nodeGetRowid( + Rtree *pRtree, + RtreeNode *pNode, + int iCell +){ + assert( iCellzData[4 + pRtree->nBytesPerCell*iCell]); +} + +/* +** Return coordinate iCoord from cell iCell in node pNode. +*/ +static void nodeGetCoord( + Rtree *pRtree, + RtreeNode *pNode, + int iCell, + int iCoord, + RtreeCoord *pCoord /* Space to write result to */ +){ + readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord); +} + +/* +** Deserialize cell iCell of node pNode. Populate the structure pointed +** to by pCell with the results. +*/ +static void nodeGetCell( + Rtree *pRtree, + RtreeNode *pNode, + int iCell, + RtreeCell *pCell +){ + int ii; + pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell); + for(ii=0; iinDim*2; ii++){ + nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]); + } +} + + +/* Forward declaration for the function that does the work of +** the virtual table module xCreate() and xConnect() methods. +*/ +static int rtreeInit( + sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int +); + +/* +** Rtree virtual table module xCreate method. +*/ +static int rtreeCreate( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1); +} + +/* +** Rtree virtual table module xConnect method. +*/ +static int rtreeConnect( + sqlite3 *db, + void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVtab, + char **pzErr +){ + return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0); +} + +/* +** Increment the r-tree reference count. +*/ +static void rtreeReference(Rtree *pRtree){ + pRtree->nBusy++; +} + +/* +** Decrement the r-tree reference count. When the reference count reaches +** zero the structure is deleted. +*/ +static void rtreeRelease(Rtree *pRtree){ + pRtree->nBusy--; + if( pRtree->nBusy==0 ){ + sqlite3_finalize(pRtree->pReadNode); + sqlite3_finalize(pRtree->pWriteNode); + sqlite3_finalize(pRtree->pDeleteNode); + sqlite3_finalize(pRtree->pReadRowid); + sqlite3_finalize(pRtree->pWriteRowid); + sqlite3_finalize(pRtree->pDeleteRowid); + sqlite3_finalize(pRtree->pReadParent); + sqlite3_finalize(pRtree->pWriteParent); + sqlite3_finalize(pRtree->pDeleteParent); + sqlite3_free(pRtree); + } +} + +/* +** Rtree virtual table module xDisconnect method. +*/ +static int rtreeDisconnect(sqlite3_vtab *pVtab){ + rtreeRelease((Rtree *)pVtab); + return SQLITE_OK; +} + +/* +** Rtree virtual table module xDestroy method. +*/ +static int rtreeDestroy(sqlite3_vtab *pVtab){ + Rtree *pRtree = (Rtree *)pVtab; + int rc; + char *zCreate = sqlite3_mprintf( + "DROP TABLE '%q'.'%q_node';" + "DROP TABLE '%q'.'%q_rowid';" + "DROP TABLE '%q'.'%q_parent';", + pRtree->zDb, pRtree->zName, + pRtree->zDb, pRtree->zName, + pRtree->zDb, pRtree->zName + ); + if( !zCreate ){ + rc = SQLITE_NOMEM; + }else{ + rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0); + sqlite3_free(zCreate); + } + if( rc==SQLITE_OK ){ + rtreeRelease(pRtree); + } + + return rc; +} + +/* +** Rtree virtual table module xOpen method. +*/ +static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ + int rc = SQLITE_NOMEM; + RtreeCursor *pCsr; + + pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor)); + if( pCsr ){ + memset(pCsr, 0, sizeof(RtreeCursor)); + pCsr->base.pVtab = pVTab; + rc = SQLITE_OK; + } + *ppCursor = (sqlite3_vtab_cursor *)pCsr; + + return rc; +} + + +/* +** Free the RtreeCursor.aConstraint[] array and its contents. +*/ +static void freeCursorConstraints(RtreeCursor *pCsr){ + if( pCsr->aConstraint ){ + int i; /* Used to iterate through constraint array */ + for(i=0; inConstraint; i++){ + sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom; + if( pGeom ){ + if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser); + sqlite3_free(pGeom); + } + } + sqlite3_free(pCsr->aConstraint); + pCsr->aConstraint = 0; + } +} + +/* +** Rtree virtual table module xClose method. +*/ +static int rtreeClose(sqlite3_vtab_cursor *cur){ + Rtree *pRtree = (Rtree *)(cur->pVtab); + int rc; + RtreeCursor *pCsr = (RtreeCursor *)cur; + freeCursorConstraints(pCsr); + rc = nodeRelease(pRtree, pCsr->pNode); + sqlite3_free(pCsr); + return rc; +} + +/* +** Rtree virtual table module xEof method. +** +** Return non-zero if the cursor does not currently point to a valid +** record (i.e if the scan has finished), or zero otherwise. +*/ +static int rtreeEof(sqlite3_vtab_cursor *cur){ + RtreeCursor *pCsr = (RtreeCursor *)cur; + return (pCsr->pNode==0); +} + +/* +** The r-tree constraint passed as the second argument to this function is +** guaranteed to be a MATCH constraint. +*/ +static int testRtreeGeom( + Rtree *pRtree, /* R-Tree object */ + RtreeConstraint *pConstraint, /* MATCH constraint to test */ + RtreeCell *pCell, /* Cell to test */ + int *pbRes /* OUT: Test result */ +){ + int i; + double aCoord[RTREE_MAX_DIMENSIONS*2]; + int nCoord = pRtree->nDim*2; + + assert( pConstraint->op==RTREE_MATCH ); + assert( pConstraint->pGeom ); + + for(i=0; iaCoord[i]); + } + return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes); +} + +/* +** Cursor pCursor currently points to a cell in a non-leaf page. +** Set *pbEof to true if the sub-tree headed by the cell is filtered +** (excluded) by the constraints in the pCursor->aConstraint[] +** array, or false otherwise. +** +** Return SQLITE_OK if successful or an SQLite error code if an error +** occurs within a geometry callback. +*/ +static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){ + RtreeCell cell; + int ii; + int bRes = 0; + int rc = SQLITE_OK; + + nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell); + for(ii=0; bRes==0 && iinConstraint; ii++){ + RtreeConstraint *p = &pCursor->aConstraint[ii]; + double cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]); + double cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]); + + assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE + || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH + ); + + switch( p->op ){ + case RTREE_LE: case RTREE_LT: + bRes = p->rValuerValue>cell_max; + break; + + case RTREE_EQ: + bRes = (p->rValue>cell_max || p->rValueop==RTREE_MATCH ); + rc = testRtreeGeom(pRtree, p, &cell, &bRes); + bRes = !bRes; + break; + } + } + } + + *pbEof = bRes; + return rc; +} + +/* +** Test if the cell that cursor pCursor currently points to +** would be filtered (excluded) by the constraints in the +** pCursor->aConstraint[] array. If so, set *pbEof to true before +** returning. If the cell is not filtered (excluded) by the constraints, +** set pbEof to zero. +** +** Return SQLITE_OK if successful or an SQLite error code if an error +** occurs within a geometry callback. +** +** This function assumes that the cell is part of a leaf node. +*/ +static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){ + RtreeCell cell; + int ii; + *pbEof = 0; + + nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell); + for(ii=0; iinConstraint; ii++){ + RtreeConstraint *p = &pCursor->aConstraint[ii]; + double coord = DCOORD(cell.aCoord[p->iCoord]); + int res; + assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE + || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH + ); + switch( p->op ){ + case RTREE_LE: res = (coord<=p->rValue); break; + case RTREE_LT: res = (coordrValue); break; + case RTREE_GE: res = (coord>=p->rValue); break; + case RTREE_GT: res = (coord>p->rValue); break; + case RTREE_EQ: res = (coord==p->rValue); break; + default: { + int rc; + assert( p->op==RTREE_MATCH ); + rc = testRtreeGeom(pRtree, p, &cell, &res); + if( rc!=SQLITE_OK ){ + return rc; + } + break; + } + } + + if( !res ){ + *pbEof = 1; + return SQLITE_OK; + } + } + + return SQLITE_OK; +} + +/* +** Cursor pCursor currently points at a node that heads a sub-tree of +** height iHeight (if iHeight==0, then the node is a leaf). Descend +** to point to the left-most cell of the sub-tree that matches the +** configured constraints. +*/ +static int descendToCell( + Rtree *pRtree, + RtreeCursor *pCursor, + int iHeight, + int *pEof /* OUT: Set to true if cannot descend */ +){ + int isEof; + int rc; + int ii; + RtreeNode *pChild; + sqlite3_int64 iRowid; + + RtreeNode *pSavedNode = pCursor->pNode; + int iSavedCell = pCursor->iCell; + + assert( iHeight>=0 ); + + if( iHeight==0 ){ + rc = testRtreeEntry(pRtree, pCursor, &isEof); + }else{ + rc = testRtreeCell(pRtree, pCursor, &isEof); + } + if( rc!=SQLITE_OK || isEof || iHeight==0 ){ + goto descend_to_cell_out; + } + + iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell); + rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild); + if( rc!=SQLITE_OK ){ + goto descend_to_cell_out; + } + + nodeRelease(pRtree, pCursor->pNode); + pCursor->pNode = pChild; + isEof = 1; + for(ii=0; isEof && iiiCell = ii; + rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof); + if( rc!=SQLITE_OK ){ + goto descend_to_cell_out; + } + } + + if( isEof ){ + assert( pCursor->pNode==pChild ); + nodeReference(pSavedNode); + nodeRelease(pRtree, pChild); + pCursor->pNode = pSavedNode; + pCursor->iCell = iSavedCell; + } + +descend_to_cell_out: + *pEof = isEof; + return rc; +} + +/* +** One of the cells in node pNode is guaranteed to have a 64-bit +** integer value equal to iRowid. Return the index of this cell. +*/ +static int nodeRowidIndex( + Rtree *pRtree, + RtreeNode *pNode, + i64 iRowid, + int *piIndex +){ + int ii; + int nCell = NCELL(pNode); + for(ii=0; iipParent; + if( pParent ){ + return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex); + } + *piIndex = -1; + return SQLITE_OK; +} + +/* +** Rtree virtual table module xNext method. +*/ +static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){ + Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab); + RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; + int rc = SQLITE_OK; + + /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is + ** already at EOF. It is against the rules to call the xNext() method of + ** a cursor that has already reached EOF. + */ + assert( pCsr->pNode ); + + if( pCsr->iStrategy==1 ){ + /* This "scan" is a direct lookup by rowid. There is no next entry. */ + nodeRelease(pRtree, pCsr->pNode); + pCsr->pNode = 0; + }else{ + /* Move to the next entry that matches the configured constraints. */ + int iHeight = 0; + while( pCsr->pNode ){ + RtreeNode *pNode = pCsr->pNode; + int nCell = NCELL(pNode); + for(pCsr->iCell++; pCsr->iCelliCell++){ + int isEof; + rc = descendToCell(pRtree, pCsr, iHeight, &isEof); + if( rc!=SQLITE_OK || !isEof ){ + return rc; + } + } + pCsr->pNode = pNode->pParent; + rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell); + if( rc!=SQLITE_OK ){ + return rc; + } + nodeReference(pCsr->pNode); + nodeRelease(pRtree, pNode); + iHeight++; + } + } + + return rc; +} + +/* +** Rtree virtual table module xRowid method. +*/ +static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){ + Rtree *pRtree = (Rtree *)pVtabCursor->pVtab; + RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; + + assert(pCsr->pNode); + *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell); + + return SQLITE_OK; +} + +/* +** Rtree virtual table module xColumn method. +*/ +static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ + Rtree *pRtree = (Rtree *)cur->pVtab; + RtreeCursor *pCsr = (RtreeCursor *)cur; + + if( i==0 ){ + i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell); + sqlite3_result_int64(ctx, iRowid); + }else{ + RtreeCoord c; + nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c); + if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ + sqlite3_result_double(ctx, c.f); + }else{ + assert( pRtree->eCoordType==RTREE_COORD_INT32 ); + sqlite3_result_int(ctx, c.i); + } + } + + return SQLITE_OK; +} + +/* +** Use nodeAcquire() to obtain the leaf node containing the record with +** rowid iRowid. If successful, set *ppLeaf to point to the node and +** return SQLITE_OK. If there is no such record in the table, set +** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf +** to zero and return an SQLite error code. +*/ +static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){ + int rc; + *ppLeaf = 0; + sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid); + if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){ + i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0); + rc = nodeAcquire(pRtree, iNode, 0, ppLeaf); + sqlite3_reset(pRtree->pReadRowid); + }else{ + rc = sqlite3_reset(pRtree->pReadRowid); + } + return rc; +} + +/* +** This function is called to configure the RtreeConstraint object passed +** as the second argument for a MATCH constraint. The value passed as the +** first argument to this function is the right-hand operand to the MATCH +** operator. +*/ +static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){ + RtreeMatchArg *p; + sqlite3_rtree_geometry *pGeom; + int nBlob; + + /* Check that value is actually a blob. */ + if( !sqlite3_value_type(pValue)==SQLITE_BLOB ) return SQLITE_ERROR; + + /* Check that the blob is roughly the right size. */ + nBlob = sqlite3_value_bytes(pValue); + if( nBlob<(int)sizeof(RtreeMatchArg) + || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0 + ){ + return SQLITE_ERROR; + } + + pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc( + sizeof(sqlite3_rtree_geometry) + nBlob + ); + if( !pGeom ) return SQLITE_NOMEM; + memset(pGeom, 0, sizeof(sqlite3_rtree_geometry)); + p = (RtreeMatchArg *)&pGeom[1]; + + memcpy(p, sqlite3_value_blob(pValue), nBlob); + if( p->magic!=RTREE_GEOMETRY_MAGIC + || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(double)) + ){ + sqlite3_free(pGeom); + return SQLITE_ERROR; + } + + pGeom->pContext = p->pContext; + pGeom->nParam = p->nParam; + pGeom->aParam = p->aParam; + + pCons->xGeom = p->xGeom; + pCons->pGeom = pGeom; + return SQLITE_OK; +} + +/* +** Rtree virtual table module xFilter method. +*/ +static int rtreeFilter( + sqlite3_vtab_cursor *pVtabCursor, + int idxNum, const char *idxStr, + int argc, sqlite3_value **argv +){ + Rtree *pRtree = (Rtree *)pVtabCursor->pVtab; + RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; + + RtreeNode *pRoot = 0; + int ii; + int rc = SQLITE_OK; + + rtreeReference(pRtree); + + freeCursorConstraints(pCsr); + pCsr->iStrategy = idxNum; + + if( idxNum==1 ){ + /* Special case - lookup by rowid. */ + RtreeNode *pLeaf; /* Leaf on which the required cell resides */ + i64 iRowid = sqlite3_value_int64(argv[0]); + rc = findLeafNode(pRtree, iRowid, &pLeaf); + pCsr->pNode = pLeaf; + if( pLeaf ){ + assert( rc==SQLITE_OK ); + rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell); + } + }else{ + /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array + ** with the configured constraints. + */ + if( argc>0 ){ + pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc); + pCsr->nConstraint = argc; + if( !pCsr->aConstraint ){ + rc = SQLITE_NOMEM; + }else{ + memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc); + assert( (idxStr==0 && argc==0) || strlen(idxStr)==argc*2 ); + for(ii=0; iiaConstraint[ii]; + p->op = idxStr[ii*2]; + p->iCoord = idxStr[ii*2+1]-'a'; + if( p->op==RTREE_MATCH ){ + /* A MATCH operator. The right-hand-side must be a blob that + ** can be cast into an RtreeMatchArg object. One created using + ** an sqlite3_rtree_geometry_callback() SQL user function. + */ + rc = deserializeGeometry(argv[ii], p); + if( rc!=SQLITE_OK ){ + break; + } + }else{ + p->rValue = sqlite3_value_double(argv[ii]); + } + } + } + } + + if( rc==SQLITE_OK ){ + pCsr->pNode = 0; + rc = nodeAcquire(pRtree, 1, 0, &pRoot); + } + if( rc==SQLITE_OK ){ + int isEof = 1; + int nCell = NCELL(pRoot); + pCsr->pNode = pRoot; + for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCelliCell++){ + assert( pCsr->pNode==pRoot ); + rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof); + if( !isEof ){ + break; + } + } + if( rc==SQLITE_OK && isEof ){ + assert( pCsr->pNode==pRoot ); + nodeRelease(pRtree, pRoot); + pCsr->pNode = 0; + } + assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCellpNode) ); + } + } + + rtreeRelease(pRtree); + return rc; +} + +/* +** Rtree virtual table module xBestIndex method. There are three +** table scan strategies to choose from (in order from most to +** least desirable): +** +** idxNum idxStr Strategy +** ------------------------------------------------ +** 1 Unused Direct lookup by rowid. +** 2 See below R-tree query or full-table scan. +** ------------------------------------------------ +** +** If strategy 1 is used, then idxStr is not meaningful. If strategy +** 2 is used, idxStr is formatted to contain 2 bytes for each +** constraint used. The first two bytes of idxStr correspond to +** the constraint in sqlite3_index_info.aConstraintUsage[] with +** (argvIndex==1) etc. +** +** The first of each pair of bytes in idxStr identifies the constraint +** operator as follows: +** +** Operator Byte Value +** ---------------------- +** = 0x41 ('A') +** <= 0x42 ('B') +** < 0x43 ('C') +** >= 0x44 ('D') +** > 0x45 ('E') +** MATCH 0x46 ('F') +** ---------------------- +** +** The second of each pair of bytes identifies the coordinate column +** to which the constraint applies. The leftmost coordinate column +** is 'a', the second from the left 'b' etc. +*/ +static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ + int rc = SQLITE_OK; + int ii, cCol; + + int iIdx = 0; + char zIdxStr[RTREE_MAX_DIMENSIONS*8+1]; + memset(zIdxStr, 0, sizeof(zIdxStr)); + UNUSED_PARAMETER(tab); + + assert( pIdxInfo->idxStr==0 ); + for(ii=0; iinConstraint; ii++){ + struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii]; + + if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ + /* We have an equality constraint on the rowid. Use strategy 1. */ + int jj; + for(jj=0; jjaConstraintUsage[jj].argvIndex = 0; + pIdxInfo->aConstraintUsage[jj].omit = 0; + } + pIdxInfo->idxNum = 1; + pIdxInfo->aConstraintUsage[ii].argvIndex = 1; + pIdxInfo->aConstraintUsage[jj].omit = 1; + + /* This strategy involves a two rowid lookups on an B-Tree structures + ** and then a linear search of an R-Tree node. This should be + ** considered almost as quick as a direct rowid lookup (for which + ** sqlite uses an internal cost of 0.0). + */ + pIdxInfo->estimatedCost = 10.0; + return SQLITE_OK; + } + + if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){ + int j, opmsk; + static const unsigned char compatible[] = { 0, 0, 1, 1, 2, 2 }; + u8 op = 0; + switch( p->op ){ + case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break; + case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break; + case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break; + case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break; + case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break; + default: + assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH ); + op = RTREE_MATCH; + break; + } + assert( op!=0 ); + + /* Make sure this particular constraint has not been used before. + ** If it has been used before, ignore it. + ** + ** A <= or < can be used if there is a prior >= or >. + ** A >= or > can be used if there is a prior < or <=. + ** A <= or < is disqualified if there is a prior <=, <, or ==. + ** A >= or > is disqualified if there is a prior >=, >, or ==. + ** A == is disqualifed if there is any prior constraint. + */ + assert( compatible[RTREE_EQ & 7]==0 ); + assert( compatible[RTREE_LT & 7]==1 ); + assert( compatible[RTREE_LE & 7]==1 ); + assert( compatible[RTREE_GT & 7]==2 ); + assert( compatible[RTREE_GE & 7]==2 ); + cCol = p->iColumn - 1 + 'a'; + opmsk = compatible[op & 7]; + for(j=0; jaConstraintUsage[ii].argvIndex = (iIdx/2); + pIdxInfo->aConstraintUsage[ii].omit = 1; + } + } + } + + pIdxInfo->idxNum = 2; + pIdxInfo->needToFreeIdxStr = 1; + if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){ + return SQLITE_NOMEM; + } + assert( iIdx>=0 ); + pIdxInfo->estimatedCost = (2000000.0 / (double)(iIdx + 1)); + return rc; +} + +/* +** Return the N-dimensional volumn of the cell stored in *p. +*/ +static float cellArea(Rtree *pRtree, RtreeCell *p){ + float area = 1.0; + int ii; + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + area = area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])); + } + return area; +} + +/* +** Return the margin length of cell p. The margin length is the sum +** of the objects size in each dimension. +*/ +static float cellMargin(Rtree *pRtree, RtreeCell *p){ + float margin = 0.0; + int ii; + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])); + } + return margin; +} + +/* +** Store the union of cells p1 and p2 in p1. +*/ +static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){ + int ii; + if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f); + p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f); + } + }else{ + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i); + p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i); + } + } +} + +/* +** Return true if the area covered by p2 is a subset of the area covered +** by p1. False otherwise. +*/ +static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){ + int ii; + int isInt = (pRtree->eCoordType==RTREE_COORD_INT32); + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + RtreeCoord *a1 = &p1->aCoord[ii]; + RtreeCoord *a2 = &p2->aCoord[ii]; + if( (!isInt && (a2[0].fa1[1].f)) + || ( isInt && (a2[0].ia1[1].i)) + ){ + return 0; + } + } + return 1; +} + +/* +** Return the amount cell p would grow by if it were unioned with pCell. +*/ +static float cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){ + float area; + RtreeCell cell; + memcpy(&cell, p, sizeof(RtreeCell)); + area = cellArea(pRtree, &cell); + cellUnion(pRtree, &cell, pCell); + return (cellArea(pRtree, &cell)-area); +} + +#if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT +static float cellOverlap( + Rtree *pRtree, + RtreeCell *p, + RtreeCell *aCell, + int nCell, + int iExclude +){ + int ii; + float overlap = 0.0; + for(ii=0; iinDim*2); jj+=2){ + double x1; + double x2; + + x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj])); + x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1])); + + if( x2iDepth-iHeight); ii++){ + int iCell; + sqlite3_int64 iBest; + + float fMinGrowth; + float fMinArea; + float fMinOverlap; + + int nCell = NCELL(pNode); + RtreeCell cell; + RtreeNode *pChild; + + RtreeCell *aCell = 0; + +#if VARIANT_RSTARTREE_CHOOSESUBTREE + if( ii==(pRtree->iDepth-1) ){ + int jj; + aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell); + if( !aCell ){ + rc = SQLITE_NOMEM; + nodeRelease(pRtree, pNode); + pNode = 0; + continue; + } + for(jj=0; jjiDepth-1) ){ + overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell); + } + if( (iCell==0) + || (overlappParent ){ + RtreeNode *pParent = p->pParent; + RtreeCell cell; + int iCell; + + if( nodeParentIndex(pRtree, p, &iCell) ){ + return SQLITE_CORRUPT; + } + + nodeGetCell(pRtree, pParent, iCell, &cell); + if( !cellContains(pRtree, &cell, pCell) ){ + cellUnion(pRtree, &cell, pCell); + nodeOverwriteCell(pRtree, pParent, &cell, iCell); + } + + p = pParent; + } + return SQLITE_OK; +} + +/* +** Write mapping (iRowid->iNode) to the _rowid table. +*/ +static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){ + sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid); + sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode); + sqlite3_step(pRtree->pWriteRowid); + return sqlite3_reset(pRtree->pWriteRowid); +} + +/* +** Write mapping (iNode->iPar) to the _parent table. +*/ +static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){ + sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode); + sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar); + sqlite3_step(pRtree->pWriteParent); + return sqlite3_reset(pRtree->pWriteParent); +} + +static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int); + +#if VARIANT_GUTTMAN_LINEAR_SPLIT +/* +** Implementation of the linear variant of the PickNext() function from +** Guttman[84]. +*/ +static RtreeCell *LinearPickNext( + Rtree *pRtree, + RtreeCell *aCell, + int nCell, + RtreeCell *pLeftBox, + RtreeCell *pRightBox, + int *aiUsed +){ + int ii; + for(ii=0; aiUsed[ii]; ii++); + aiUsed[ii] = 1; + return &aCell[ii]; +} + +/* +** Implementation of the linear variant of the PickSeeds() function from +** Guttman[84]. +*/ +static void LinearPickSeeds( + Rtree *pRtree, + RtreeCell *aCell, + int nCell, + int *piLeftSeed, + int *piRightSeed +){ + int i; + int iLeftSeed = 0; + int iRightSeed = 1; + float maxNormalInnerWidth = 0.0; + + /* Pick two "seed" cells from the array of cells. The algorithm used + ** here is the LinearPickSeeds algorithm from Gutman[1984]. The + ** indices of the two seed cells in the array are stored in local + ** variables iLeftSeek and iRightSeed. + */ + for(i=0; inDim; i++){ + float x1 = DCOORD(aCell[0].aCoord[i*2]); + float x2 = DCOORD(aCell[0].aCoord[i*2+1]); + float x3 = x1; + float x4 = x2; + int jj; + + int iCellLeft = 0; + int iCellRight = 0; + + for(jj=1; jjx4 ) x4 = right; + if( left>x3 ){ + x3 = left; + iCellRight = jj; + } + if( rightmaxNormalInnerWidth ){ + iLeftSeed = iCellLeft; + iRightSeed = iCellRight; + } + } + } + + *piLeftSeed = iLeftSeed; + *piRightSeed = iRightSeed; +} +#endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */ + +#if VARIANT_GUTTMAN_QUADRATIC_SPLIT +/* +** Implementation of the quadratic variant of the PickNext() function from +** Guttman[84]. +*/ +static RtreeCell *QuadraticPickNext( + Rtree *pRtree, + RtreeCell *aCell, + int nCell, + RtreeCell *pLeftBox, + RtreeCell *pRightBox, + int *aiUsed +){ + #define FABS(a) ((a)<0.0?-1.0*(a):(a)) + + int iSelect = -1; + float fDiff; + int ii; + for(ii=0; iifDiff ){ + fDiff = diff; + iSelect = ii; + } + } + } + aiUsed[iSelect] = 1; + return &aCell[iSelect]; +} + +/* +** Implementation of the quadratic variant of the PickSeeds() function from +** Guttman[84]. +*/ +static void QuadraticPickSeeds( + Rtree *pRtree, + RtreeCell *aCell, + int nCell, + int *piLeftSeed, + int *piRightSeed +){ + int ii; + int jj; + + int iLeftSeed = 0; + int iRightSeed = 1; + float fWaste = 0.0; + + for(ii=0; iifWaste ){ + iLeftSeed = ii; + iRightSeed = jj; + fWaste = waste; + } + } + } + + *piLeftSeed = iLeftSeed; + *piRightSeed = iRightSeed; +} +#endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */ + +/* +** Arguments aIdx, aDistance and aSpare all point to arrays of size +** nIdx. The aIdx array contains the set of integers from 0 to +** (nIdx-1) in no particular order. This function sorts the values +** in aIdx according to the indexed values in aDistance. For +** example, assuming the inputs: +** +** aIdx = { 0, 1, 2, 3 } +** aDistance = { 5.0, 2.0, 7.0, 6.0 } +** +** this function sets the aIdx array to contain: +** +** aIdx = { 0, 1, 2, 3 } +** +** The aSpare array is used as temporary working space by the +** sorting algorithm. +*/ +static void SortByDistance( + int *aIdx, + int nIdx, + float *aDistance, + int *aSpare +){ + if( nIdx>1 ){ + int iLeft = 0; + int iRight = 0; + + int nLeft = nIdx/2; + int nRight = nIdx-nLeft; + int *aLeft = aIdx; + int *aRight = &aIdx[nLeft]; + + SortByDistance(aLeft, nLeft, aDistance, aSpare); + SortByDistance(aRight, nRight, aDistance, aSpare); + + memcpy(aSpare, aLeft, sizeof(int)*nLeft); + aLeft = aSpare; + + while( iLeft1 ){ + + int iLeft = 0; + int iRight = 0; + + int nLeft = nIdx/2; + int nRight = nIdx-nLeft; + int *aLeft = aIdx; + int *aRight = &aIdx[nLeft]; + + SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare); + SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare); + + memcpy(aSpare, aLeft, sizeof(int)*nLeft); + aLeft = aSpare; + while( iLeftnDim+1)*(sizeof(int*)+nCell*sizeof(int)); + + aaSorted = (int **)sqlite3_malloc(nByte); + if( !aaSorted ){ + return SQLITE_NOMEM; + } + + aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell]; + memset(aaSorted, 0, nByte); + for(ii=0; iinDim; ii++){ + int jj; + aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell]; + for(jj=0; jjnDim; ii++){ + float margin = 0.0; + float fBestOverlap; + float fBestArea; + int iBestLeft; + int nLeft; + + for( + nLeft=RTREE_MINCELLS(pRtree); + nLeft<=(nCell-RTREE_MINCELLS(pRtree)); + nLeft++ + ){ + RtreeCell left; + RtreeCell right; + int kk; + float overlap; + float area; + + memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell)); + memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell)); + for(kk=1; kk<(nCell-1); kk++){ + if( kk0; i--){ + RtreeCell *pNext; + pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed); + float diff = + cellGrowth(pRtree, pBboxLeft, pNext) - + cellGrowth(pRtree, pBboxRight, pNext) + ; + if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i) + || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i)) + ){ + nodeInsertCell(pRtree, pRight, pNext); + cellUnion(pRtree, pBboxRight, pNext); + }else{ + nodeInsertCell(pRtree, pLeft, pNext); + cellUnion(pRtree, pBboxLeft, pNext); + } + } + + sqlite3_free(aiUsed); + return SQLITE_OK; +} +#endif + +static int updateMapping( + Rtree *pRtree, + i64 iRowid, + RtreeNode *pNode, + int iHeight +){ + int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64); + xSetMapping = ((iHeight==0)?rowidWrite:parentWrite); + if( iHeight>0 ){ + RtreeNode *pChild = nodeHashLookup(pRtree, iRowid); + if( pChild ){ + nodeRelease(pRtree, pChild->pParent); + nodeReference(pNode); + pChild->pParent = pNode; + } + } + return xSetMapping(pRtree, iRowid, pNode->iNode); +} + +static int SplitNode( + Rtree *pRtree, + RtreeNode *pNode, + RtreeCell *pCell, + int iHeight +){ + int i; + int newCellIsRight = 0; + + int rc = SQLITE_OK; + int nCell = NCELL(pNode); + RtreeCell *aCell; + int *aiUsed; + + RtreeNode *pLeft = 0; + RtreeNode *pRight = 0; + + RtreeCell leftbbox; + RtreeCell rightbbox; + + /* Allocate an array and populate it with a copy of pCell and + ** all cells from node pLeft. Then zero the original node. + */ + aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1)); + if( !aCell ){ + rc = SQLITE_NOMEM; + goto splitnode_out; + } + aiUsed = (int *)&aCell[nCell+1]; + memset(aiUsed, 0, sizeof(int)*(nCell+1)); + for(i=0; iiNode==1 ){ + pRight = nodeNew(pRtree, pNode); + pLeft = nodeNew(pRtree, pNode); + pRtree->iDepth++; + pNode->isDirty = 1; + writeInt16(pNode->zData, pRtree->iDepth); + }else{ + pLeft = pNode; + pRight = nodeNew(pRtree, pLeft->pParent); + nodeReference(pLeft); + } + + if( !pLeft || !pRight ){ + rc = SQLITE_NOMEM; + goto splitnode_out; + } + + memset(pLeft->zData, 0, pRtree->iNodeSize); + memset(pRight->zData, 0, pRtree->iNodeSize); + + rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox); + if( rc!=SQLITE_OK ){ + goto splitnode_out; + } + + /* Ensure both child nodes have node numbers assigned to them by calling + ** nodeWrite(). Node pRight always needs a node number, as it was created + ** by nodeNew() above. But node pLeft sometimes already has a node number. + ** In this case avoid the all to nodeWrite(). + */ + if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight)) + || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft))) + ){ + goto splitnode_out; + } + + rightbbox.iRowid = pRight->iNode; + leftbbox.iRowid = pLeft->iNode; + + if( pNode->iNode==1 ){ + rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1); + if( rc!=SQLITE_OK ){ + goto splitnode_out; + } + }else{ + RtreeNode *pParent = pLeft->pParent; + int iCell; + rc = nodeParentIndex(pRtree, pLeft, &iCell); + if( rc==SQLITE_OK ){ + nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell); + rc = AdjustTree(pRtree, pParent, &leftbbox); + } + if( rc!=SQLITE_OK ){ + goto splitnode_out; + } + } + if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){ + goto splitnode_out; + } + + for(i=0; iiRowid ){ + newCellIsRight = 1; + } + if( rc!=SQLITE_OK ){ + goto splitnode_out; + } + } + if( pNode->iNode==1 ){ + for(i=0; iiRowid, pLeft, iHeight); + } + + if( rc==SQLITE_OK ){ + rc = nodeRelease(pRtree, pRight); + pRight = 0; + } + if( rc==SQLITE_OK ){ + rc = nodeRelease(pRtree, pLeft); + pLeft = 0; + } + +splitnode_out: + nodeRelease(pRtree, pRight); + nodeRelease(pRtree, pLeft); + sqlite3_free(aCell); + return rc; +} + +/* +** If node pLeaf is not the root of the r-tree and its pParent pointer is +** still NULL, load all ancestor nodes of pLeaf into memory and populate +** the pLeaf->pParent chain all the way up to the root node. +** +** This operation is required when a row is deleted (or updated - an update +** is implemented as a delete followed by an insert). SQLite provides the +** rowid of the row to delete, which can be used to find the leaf on which +** the entry resides (argument pLeaf). Once the leaf is located, this +** function is called to determine its ancestry. +*/ +static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){ + int rc = SQLITE_OK; + RtreeNode *pChild = pLeaf; + while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){ + int rc2 = SQLITE_OK; /* sqlite3_reset() return code */ + sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode); + rc = sqlite3_step(pRtree->pReadParent); + if( rc==SQLITE_ROW ){ + RtreeNode *pTest; /* Used to test for reference loops */ + i64 iNode; /* Node number of parent node */ + + /* Before setting pChild->pParent, test that we are not creating a + ** loop of references (as we would if, say, pChild==pParent). We don't + ** want to do this as it leads to a memory leak when trying to delete + ** the referenced counted node structures. + */ + iNode = sqlite3_column_int64(pRtree->pReadParent, 0); + for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent); + if( !pTest ){ + rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent); + } + } + rc = sqlite3_reset(pRtree->pReadParent); + if( rc==SQLITE_OK ) rc = rc2; + if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT; + pChild = pChild->pParent; + } + return rc; +} + +static int deleteCell(Rtree *, RtreeNode *, int, int); + +static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){ + int rc; + int rc2; + RtreeNode *pParent; + int iCell; + + assert( pNode->nRef==1 ); + + /* Remove the entry in the parent cell. */ + rc = nodeParentIndex(pRtree, pNode, &iCell); + if( rc==SQLITE_OK ){ + pParent = pNode->pParent; + pNode->pParent = 0; + rc = deleteCell(pRtree, pParent, iCell, iHeight+1); + } + rc2 = nodeRelease(pRtree, pParent); + if( rc==SQLITE_OK ){ + rc = rc2; + } + if( rc!=SQLITE_OK ){ + return rc; + } + + /* Remove the xxx_node entry. */ + sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode); + sqlite3_step(pRtree->pDeleteNode); + if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){ + return rc; + } + + /* Remove the xxx_parent entry. */ + sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode); + sqlite3_step(pRtree->pDeleteParent); + if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){ + return rc; + } + + /* Remove the node from the in-memory hash table and link it into + ** the Rtree.pDeleted list. Its contents will be re-inserted later on. + */ + nodeHashDelete(pRtree, pNode); + pNode->iNode = iHeight; + pNode->pNext = pRtree->pDeleted; + pNode->nRef++; + pRtree->pDeleted = pNode; + + return SQLITE_OK; +} + +static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){ + RtreeNode *pParent = pNode->pParent; + int rc = SQLITE_OK; + if( pParent ){ + int ii; + int nCell = NCELL(pNode); + RtreeCell box; /* Bounding box for pNode */ + nodeGetCell(pRtree, pNode, 0, &box); + for(ii=1; iiiNode; + rc = nodeParentIndex(pRtree, pNode, &ii); + if( rc==SQLITE_OK ){ + nodeOverwriteCell(pRtree, pParent, &box, ii); + rc = fixBoundingBox(pRtree, pParent); + } + } + return rc; +} + +/* +** Delete the cell at index iCell of node pNode. After removing the +** cell, adjust the r-tree data structure if required. +*/ +static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){ + RtreeNode *pParent; + int rc; + + if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){ + return rc; + } + + /* Remove the cell from the node. This call just moves bytes around + ** the in-memory node image, so it cannot fail. + */ + nodeDeleteCell(pRtree, pNode, iCell); + + /* If the node is not the tree root and now has less than the minimum + ** number of cells, remove it from the tree. Otherwise, update the + ** cell in the parent node so that it tightly contains the updated + ** node. + */ + pParent = pNode->pParent; + assert( pParent || pNode->iNode==1 ); + if( pParent ){ + if( NCELL(pNode)nDim; iDim++){ + aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]); + aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]); + } + } + for(iDim=0; iDimnDim; iDim++){ + aCenterCoord[iDim] = aCenterCoord[iDim]/((float)nCell*2.0); + } + + for(ii=0; iinDim; iDim++){ + float coord = DCOORD(aCell[ii].aCoord[iDim*2+1]) - + DCOORD(aCell[ii].aCoord[iDim*2]); + aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]); + } + } + + SortByDistance(aOrder, nCell, aDistance, aSpare); + nodeZero(pRtree, pNode); + + for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){ + RtreeCell *p = &aCell[aOrder[ii]]; + nodeInsertCell(pRtree, pNode, p); + if( p->iRowid==pCell->iRowid ){ + if( iHeight==0 ){ + rc = rowidWrite(pRtree, p->iRowid, pNode->iNode); + }else{ + rc = parentWrite(pRtree, p->iRowid, pNode->iNode); + } + } + } + if( rc==SQLITE_OK ){ + rc = fixBoundingBox(pRtree, pNode); + } + for(; rc==SQLITE_OK && iiiNode currently contains + ** the height of the sub-tree headed by the cell. + */ + RtreeNode *pInsert; + RtreeCell *p = &aCell[aOrder[ii]]; + rc = ChooseLeaf(pRtree, p, iHeight, &pInsert); + if( rc==SQLITE_OK ){ + int rc2; + rc = rtreeInsertCell(pRtree, pInsert, p, iHeight); + rc2 = nodeRelease(pRtree, pInsert); + if( rc==SQLITE_OK ){ + rc = rc2; + } + } + } + + sqlite3_free(aCell); + return rc; +} + +/* +** Insert cell pCell into node pNode. Node pNode is the head of a +** subtree iHeight high (leaf nodes have iHeight==0). +*/ +static int rtreeInsertCell( + Rtree *pRtree, + RtreeNode *pNode, + RtreeCell *pCell, + int iHeight +){ + int rc = SQLITE_OK; + if( iHeight>0 ){ + RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid); + if( pChild ){ + nodeRelease(pRtree, pChild->pParent); + nodeReference(pNode); + pChild->pParent = pNode; + } + } + if( nodeInsertCell(pRtree, pNode, pCell) ){ +#if VARIANT_RSTARTREE_REINSERT + if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){ + rc = SplitNode(pRtree, pNode, pCell, iHeight); + }else{ + pRtree->iReinsertHeight = iHeight; + rc = Reinsert(pRtree, pNode, pCell, iHeight); + } +#else + rc = SplitNode(pRtree, pNode, pCell, iHeight); +#endif + }else{ + rc = AdjustTree(pRtree, pNode, pCell); + if( rc==SQLITE_OK ){ + if( iHeight==0 ){ + rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode); + }else{ + rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode); + } + } + } + return rc; +} + +static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){ + int ii; + int rc = SQLITE_OK; + int nCell = NCELL(pNode); + + for(ii=0; rc==SQLITE_OK && iiiNode currently contains + ** the height of the sub-tree headed by the cell. + */ + rc = ChooseLeaf(pRtree, &cell, pNode->iNode, &pInsert); + if( rc==SQLITE_OK ){ + int rc2; + rc = rtreeInsertCell(pRtree, pInsert, &cell, pNode->iNode); + rc2 = nodeRelease(pRtree, pInsert); + if( rc==SQLITE_OK ){ + rc = rc2; + } + } + } + return rc; +} + +/* +** Select a currently unused rowid for a new r-tree record. +*/ +static int newRowid(Rtree *pRtree, i64 *piRowid){ + int rc; + sqlite3_bind_null(pRtree->pWriteRowid, 1); + sqlite3_bind_null(pRtree->pWriteRowid, 2); + sqlite3_step(pRtree->pWriteRowid); + rc = sqlite3_reset(pRtree->pWriteRowid); + *piRowid = sqlite3_last_insert_rowid(pRtree->db); + return rc; +} + +/* +** The xUpdate method for rtree module virtual tables. +*/ +static int rtreeUpdate( + sqlite3_vtab *pVtab, + int nData, + sqlite3_value **azData, + sqlite_int64 *pRowid +){ + Rtree *pRtree = (Rtree *)pVtab; + int rc = SQLITE_OK; + + rtreeReference(pRtree); + + assert(nData>=1); + + /* If azData[0] is not an SQL NULL value, it is the rowid of a + ** record to delete from the r-tree table. The following block does + ** just that. + */ + if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){ + i64 iDelete; /* The rowid to delete */ + RtreeNode *pLeaf; /* Leaf node containing record iDelete */ + int iCell; /* Index of iDelete cell in pLeaf */ + RtreeNode *pRoot; + + /* Obtain a reference to the root node to initialise Rtree.iDepth */ + rc = nodeAcquire(pRtree, 1, 0, &pRoot); + + /* Obtain a reference to the leaf node that contains the entry + ** about to be deleted. + */ + if( rc==SQLITE_OK ){ + iDelete = sqlite3_value_int64(azData[0]); + rc = findLeafNode(pRtree, iDelete, &pLeaf); + } + + /* Delete the cell in question from the leaf node. */ + if( rc==SQLITE_OK ){ + int rc2; + rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell); + if( rc==SQLITE_OK ){ + rc = deleteCell(pRtree, pLeaf, iCell, 0); + } + rc2 = nodeRelease(pRtree, pLeaf); + if( rc==SQLITE_OK ){ + rc = rc2; + } + } + + /* Delete the corresponding entry in the _rowid table. */ + if( rc==SQLITE_OK ){ + sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete); + sqlite3_step(pRtree->pDeleteRowid); + rc = sqlite3_reset(pRtree->pDeleteRowid); + } + + /* Check if the root node now has exactly one child. If so, remove + ** it, schedule the contents of the child for reinsertion and + ** reduce the tree height by one. + ** + ** This is equivalent to copying the contents of the child into + ** the root node (the operation that Gutman's paper says to perform + ** in this scenario). + */ + if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){ + int rc2; + RtreeNode *pChild; + i64 iChild = nodeGetRowid(pRtree, pRoot, 0); + rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); + if( rc==SQLITE_OK ){ + rc = removeNode(pRtree, pChild, pRtree->iDepth-1); + } + rc2 = nodeRelease(pRtree, pChild); + if( rc==SQLITE_OK ) rc = rc2; + if( rc==SQLITE_OK ){ + pRtree->iDepth--; + writeInt16(pRoot->zData, pRtree->iDepth); + pRoot->isDirty = 1; + } + } + + /* Re-insert the contents of any underfull nodes removed from the tree. */ + for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){ + if( rc==SQLITE_OK ){ + rc = reinsertNodeContent(pRtree, pLeaf); + } + pRtree->pDeleted = pLeaf->pNext; + sqlite3_free(pLeaf); + } + + /* Release the reference to the root node. */ + if( rc==SQLITE_OK ){ + rc = nodeRelease(pRtree, pRoot); + }else{ + nodeRelease(pRtree, pRoot); + } + } + + /* If the azData[] array contains more than one element, elements + ** (azData[2]..azData[argc-1]) contain a new record to insert into + ** the r-tree structure. + */ + if( rc==SQLITE_OK && nData>1 ){ + /* Insert a new record into the r-tree */ + RtreeCell cell; + int ii; + RtreeNode *pLeaf; + + /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */ + assert( nData==(pRtree->nDim*2 + 3) ); + if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + cell.aCoord[ii].f = (float)sqlite3_value_double(azData[ii+3]); + cell.aCoord[ii+1].f = (float)sqlite3_value_double(azData[ii+4]); + if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){ + rc = SQLITE_CONSTRAINT; + goto constraint; + } + } + }else{ + for(ii=0; ii<(pRtree->nDim*2); ii+=2){ + cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]); + cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]); + if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){ + rc = SQLITE_CONSTRAINT; + goto constraint; + } + } + } + + /* Figure out the rowid of the new row. */ + if( sqlite3_value_type(azData[2])==SQLITE_NULL ){ + rc = newRowid(pRtree, &cell.iRowid); + }else{ + cell.iRowid = sqlite3_value_int64(azData[2]); + sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid); + if( SQLITE_ROW==sqlite3_step(pRtree->pReadRowid) ){ + sqlite3_reset(pRtree->pReadRowid); + rc = SQLITE_CONSTRAINT; + goto constraint; + } + rc = sqlite3_reset(pRtree->pReadRowid); + } + *pRowid = cell.iRowid; + + if( rc==SQLITE_OK ){ + rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf); + } + if( rc==SQLITE_OK ){ + int rc2; + pRtree->iReinsertHeight = -1; + rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0); + rc2 = nodeRelease(pRtree, pLeaf); + if( rc==SQLITE_OK ){ + rc = rc2; + } + } + } + +constraint: + rtreeRelease(pRtree); + return rc; +} + +/* +** The xRename method for rtree module virtual tables. +*/ +static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){ + Rtree *pRtree = (Rtree *)pVtab; + int rc = SQLITE_NOMEM; + char *zSql = sqlite3_mprintf( + "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";" + "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";" + "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";" + , pRtree->zDb, pRtree->zName, zNewName + , pRtree->zDb, pRtree->zName, zNewName + , pRtree->zDb, pRtree->zName, zNewName + ); + if( zSql ){ + rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0); + sqlite3_free(zSql); + } + return rc; +} + +static sqlite3_module rtreeModule = { + 0, /* iVersion */ + rtreeCreate, /* xCreate - create a table */ + rtreeConnect, /* xConnect - connect to an existing table */ + rtreeBestIndex, /* xBestIndex - Determine search strategy */ + rtreeDisconnect, /* xDisconnect - Disconnect from a table */ + rtreeDestroy, /* xDestroy - Drop a table */ + rtreeOpen, /* xOpen - open a cursor */ + rtreeClose, /* xClose - close a cursor */ + rtreeFilter, /* xFilter - configure scan constraints */ + rtreeNext, /* xNext - advance a cursor */ + rtreeEof, /* xEof */ + rtreeColumn, /* xColumn - read data */ + rtreeRowid, /* xRowid - read data */ + rtreeUpdate, /* xUpdate - write data */ + 0, /* xBegin - begin transaction */ + 0, /* xSync - sync transaction */ + 0, /* xCommit - commit transaction */ + 0, /* xRollback - rollback transaction */ + 0, /* xFindFunction - function overloading */ + rtreeRename /* xRename - rename the table */ +}; + +static int rtreeSqlInit( + Rtree *pRtree, + sqlite3 *db, + const char *zDb, + const char *zPrefix, + int isCreate +){ + int rc = SQLITE_OK; + + #define N_STATEMENT 9 + static const char *azSql[N_STATEMENT] = { + /* Read and write the xxx_node table */ + "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1", + "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)", + "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1", + + /* Read and write the xxx_rowid table */ + "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1", + "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)", + "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1", + + /* Read and write the xxx_parent table */ + "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1", + "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)", + "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1" + }; + sqlite3_stmt **appStmt[N_STATEMENT]; + int i; + + pRtree->db = db; + + if( isCreate ){ + char *zCreate = sqlite3_mprintf( +"CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);" +"CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);" +"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);" +"INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))", + zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize + ); + if( !zCreate ){ + return SQLITE_NOMEM; + } + rc = sqlite3_exec(db, zCreate, 0, 0, 0); + sqlite3_free(zCreate); + if( rc!=SQLITE_OK ){ + return rc; + } + } + + appStmt[0] = &pRtree->pReadNode; + appStmt[1] = &pRtree->pWriteNode; + appStmt[2] = &pRtree->pDeleteNode; + appStmt[3] = &pRtree->pReadRowid; + appStmt[4] = &pRtree->pWriteRowid; + appStmt[5] = &pRtree->pDeleteRowid; + appStmt[6] = &pRtree->pReadParent; + appStmt[7] = &pRtree->pWriteParent; + appStmt[8] = &pRtree->pDeleteParent; + + for(i=0; iiNodeSize is populated and SQLITE_OK returned. +** Otherwise, an SQLite error code is returned. +** +** If this function is being called as part of an xConnect(), then the rtree +** table already exists. In this case the node-size is determined by inspecting +** the root node of the tree. +** +** Otherwise, for an xCreate(), use 64 bytes less than the database page-size. +** This ensures that each node is stored on a single database page. If the +** database page-size is so large that more than RTREE_MAXCELLS entries +** would fit in a single node, use a smaller node-size. +*/ +static int getNodeSize( + sqlite3 *db, /* Database handle */ + Rtree *pRtree, /* Rtree handle */ + int isCreate /* True for xCreate, false for xConnect */ +){ + int rc; + char *zSql; + if( isCreate ){ + int iPageSize; + zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb); + rc = getIntFromStmt(db, zSql, &iPageSize); + if( rc==SQLITE_OK ){ + pRtree->iNodeSize = iPageSize-64; + if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)iNodeSize ){ + pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS; + } + } + }else{ + zSql = sqlite3_mprintf( + "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1", + pRtree->zDb, pRtree->zName + ); + rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize); + } + + sqlite3_free(zSql); + return rc; +} + +/* +** This function is the implementation of both the xConnect and xCreate +** methods of the r-tree virtual table. +** +** argv[0] -> module name +** argv[1] -> database name +** argv[2] -> table name +** argv[...] -> column names... +*/ +static int rtreeInit( + sqlite3 *db, /* Database connection */ + void *pAux, /* One of the RTREE_COORD_* constants */ + int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */ + sqlite3_vtab **ppVtab, /* OUT: New virtual table */ + char **pzErr, /* OUT: Error message, if any */ + int isCreate /* True for xCreate, false for xConnect */ +){ + int rc = SQLITE_OK; + Rtree *pRtree; + int nDb; /* Length of string argv[1] */ + int nName; /* Length of string argv[2] */ + int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32); + + const char *aErrMsg[] = { + 0, /* 0 */ + "Wrong number of columns for an rtree table", /* 1 */ + "Too few columns for an rtree table", /* 2 */ + "Too many columns for an rtree table" /* 3 */ + }; + + int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2; + if( aErrMsg[iErr] ){ + *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]); + return SQLITE_ERROR; + } + + /* Allocate the sqlite3_vtab structure */ + nDb = strlen(argv[1]); + nName = strlen(argv[2]); + pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2); + if( !pRtree ){ + return SQLITE_NOMEM; + } + memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2); + pRtree->nBusy = 1; + pRtree->base.pModule = &rtreeModule; + pRtree->zDb = (char *)&pRtree[1]; + pRtree->zName = &pRtree->zDb[nDb+1]; + pRtree->nDim = (argc-4)/2; + pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2; + pRtree->eCoordType = eCoordType; + memcpy(pRtree->zDb, argv[1], nDb); + memcpy(pRtree->zName, argv[2], nName); + + /* Figure out the node size to use. */ + rc = getNodeSize(db, pRtree, isCreate); + + /* Create/Connect to the underlying relational database schema. If + ** that is successful, call sqlite3_declare_vtab() to configure + ** the r-tree table schema. + */ + if( rc==SQLITE_OK ){ + if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){ + *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); + }else{ + char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]); + char *zTmp; + int ii; + for(ii=4; zSql && ii*2 coordinates. +*/ +static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){ + char *zText = 0; + RtreeNode node; + Rtree tree; + int ii; + + UNUSED_PARAMETER(nArg); + memset(&node, 0, sizeof(RtreeNode)); + memset(&tree, 0, sizeof(Rtree)); + tree.nDim = sqlite3_value_int(apArg[0]); + tree.nBytesPerCell = 8 + 8 * tree.nDim; + node.zData = (u8 *)sqlite3_value_blob(apArg[1]); + + for(ii=0; iimagic = RTREE_GEOMETRY_MAGIC; + pBlob->xGeom = pGeomCtx->xGeom; + pBlob->pContext = pGeomCtx->pContext; + pBlob->nParam = nArg; + for(i=0; iaParam[i] = sqlite3_value_double(aArg[i]); + } + sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free); + } +} + +/* +** Register a new geometry function for use with the r-tree MATCH operator. +*/ +SQLITE_API int sqlite3_rtree_geometry_callback( + sqlite3 *db, + const char *zGeom, + int (*xGeom)(sqlite3_rtree_geometry *, int, double *, int *), + void *pContext +){ + RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */ + + /* Allocate and populate the context object. */ + pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback)); + if( !pGeomCtx ) return SQLITE_NOMEM; + pGeomCtx->xGeom = xGeom; + pGeomCtx->pContext = pContext; + + /* Create the new user-function. Register a destructor function to delete + ** the context object when it is no longer required. */ + return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY, + (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free + ); +} + +#if !SQLITE_CORE +SQLITE_API int sqlite3_extension_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + SQLITE_EXTENSION_INIT2(pApi) + return sqlite3RtreeInit(db); +} +#endif + +#endif + +/************** End of rtree.c ***********************************************/ +/************** Begin file icu.c *********************************************/ +/* +** 2007 May 6 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $ +** +** This file implements an integration between the ICU library +** ("International Components for Unicode", an open-source library +** for handling unicode data) and SQLite. The integration uses +** ICU to provide the following to SQLite: +** +** * An implementation of the SQL regexp() function (and hence REGEXP +** operator) using the ICU uregex_XX() APIs. +** +** * Implementations of the SQL scalar upper() and lower() functions +** for case mapping. +** +** * Integration of ICU and SQLite collation seqences. +** +** * An implementation of the LIKE operator that uses ICU to +** provide case-independent matching. +*/ + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU) + +/* Include ICU headers */ +#include +#include +#include +#include + + +#ifndef SQLITE_CORE + SQLITE_EXTENSION_INIT1 +#else +#endif + +/* +** Maximum length (in bytes) of the pattern in a LIKE or GLOB +** operator. +*/ +#ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH +# define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000 +#endif + +/* +** Version of sqlite3_free() that is always a function, never a macro. +*/ +static void xFree(void *p){ + sqlite3_free(p); +} + +/* +** Compare two UTF-8 strings for equality where the first string is +** a "LIKE" expression. Return true (1) if they are the same and +** false (0) if they are different. +*/ +static int icuLikeCompare( + const uint8_t *zPattern, /* LIKE pattern */ + const uint8_t *zString, /* The UTF-8 string to compare against */ + const UChar32 uEsc /* The escape character */ +){ + static const int MATCH_ONE = (UChar32)'_'; + static const int MATCH_ALL = (UChar32)'%'; + + int iPattern = 0; /* Current byte index in zPattern */ + int iString = 0; /* Current byte index in zString */ + + int prevEscape = 0; /* True if the previous character was uEsc */ + + while( zPattern[iPattern]!=0 ){ + + /* Read (and consume) the next character from the input pattern. */ + UChar32 uPattern; + U8_NEXT_UNSAFE(zPattern, iPattern, uPattern); + assert(uPattern!=0); + + /* There are now 4 possibilities: + ** + ** 1. uPattern is an unescaped match-all character "%", + ** 2. uPattern is an unescaped match-one character "_", + ** 3. uPattern is an unescaped escape character, or + ** 4. uPattern is to be handled as an ordinary character + */ + if( !prevEscape && uPattern==MATCH_ALL ){ + /* Case 1. */ + uint8_t c; + + /* Skip any MATCH_ALL or MATCH_ONE characters that follow a + ** MATCH_ALL. For each MATCH_ONE, skip one character in the + ** test string. + */ + while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){ + if( c==MATCH_ONE ){ + if( zString[iString]==0 ) return 0; + U8_FWD_1_UNSAFE(zString, iString); + } + iPattern++; + } + + if( zPattern[iPattern]==0 ) return 1; + + while( zString[iString] ){ + if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){ + return 1; + } + U8_FWD_1_UNSAFE(zString, iString); + } + return 0; + + }else if( !prevEscape && uPattern==MATCH_ONE ){ + /* Case 2. */ + if( zString[iString]==0 ) return 0; + U8_FWD_1_UNSAFE(zString, iString); + + }else if( !prevEscape && uPattern==uEsc){ + /* Case 3. */ + prevEscape = 1; + + }else{ + /* Case 4. */ + UChar32 uString; + U8_NEXT_UNSAFE(zString, iString, uString); + uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT); + uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT); + if( uString!=uPattern ){ + return 0; + } + prevEscape = 0; + } + } + + return zString[iString]==0; +} + +/* +** Implementation of the like() SQL function. This function implements +** the build-in LIKE operator. The first argument to the function is the +** pattern and the second argument is the string. So, the SQL statements: +** +** A LIKE B +** +** is implemented as like(B, A). If there is an escape character E, +** +** A LIKE B ESCAPE E +** +** is mapped to like(B, A, E). +*/ +static void icuLikeFunc( + sqlite3_context *context, + int argc, + sqlite3_value **argv +){ + const unsigned char *zA = sqlite3_value_text(argv[0]); + const unsigned char *zB = sqlite3_value_text(argv[1]); + UChar32 uEsc = 0; + + /* Limit the length of the LIKE or GLOB pattern to avoid problems + ** of deep recursion and N*N behavior in patternCompare(). + */ + if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){ + sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1); + return; + } + + + if( argc==3 ){ + /* The escape character string must consist of a single UTF-8 character. + ** Otherwise, return an error. + */ + int nE= sqlite3_value_bytes(argv[2]); + const unsigned char *zE = sqlite3_value_text(argv[2]); + int i = 0; + if( zE==0 ) return; + U8_NEXT(zE, i, nE, uEsc); + if( i!=nE){ + sqlite3_result_error(context, + "ESCAPE expression must be a single character", -1); + return; + } + } + + if( zA && zB ){ + sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc)); + } +} + +/* +** This function is called when an ICU function called from within +** the implementation of an SQL scalar function returns an error. +** +** The scalar function context passed as the first argument is +** loaded with an error message based on the following two args. +*/ +static void icuFunctionError( + sqlite3_context *pCtx, /* SQLite scalar function context */ + const char *zName, /* Name of ICU function that failed */ + UErrorCode e /* Error code returned by ICU function */ +){ + char zBuf[128]; + sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e)); + zBuf[127] = '\0'; + sqlite3_result_error(pCtx, zBuf, -1); +} + +/* +** Function to delete compiled regexp objects. Registered as +** a destructor function with sqlite3_set_auxdata(). +*/ +static void icuRegexpDelete(void *p){ + URegularExpression *pExpr = (URegularExpression *)p; + uregex_close(pExpr); +} + +/* +** Implementation of SQLite REGEXP operator. This scalar function takes +** two arguments. The first is a regular expression pattern to compile +** the second is a string to match against that pattern. If either +** argument is an SQL NULL, then NULL Is returned. Otherwise, the result +** is 1 if the string matches the pattern, or 0 otherwise. +** +** SQLite maps the regexp() function to the regexp() operator such +** that the following two are equivalent: +** +** zString REGEXP zPattern +** regexp(zPattern, zString) +** +** Uses the following ICU regexp APIs: +** +** uregex_open() +** uregex_matches() +** uregex_close() +*/ +static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){ + UErrorCode status = U_ZERO_ERROR; + URegularExpression *pExpr; + UBool res; + const UChar *zString = sqlite3_value_text16(apArg[1]); + + /* If the left hand side of the regexp operator is NULL, + ** then the result is also NULL. + */ + if( !zString ){ + return; + } + + pExpr = sqlite3_get_auxdata(p, 0); + if( !pExpr ){ + const UChar *zPattern = sqlite3_value_text16(apArg[0]); + if( !zPattern ){ + return; + } + pExpr = uregex_open(zPattern, -1, 0, 0, &status); + + if( U_SUCCESS(status) ){ + sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete); + }else{ + assert(!pExpr); + icuFunctionError(p, "uregex_open", status); + return; + } + } + + /* Configure the text that the regular expression operates on. */ + uregex_setText(pExpr, zString, -1, &status); + if( !U_SUCCESS(status) ){ + icuFunctionError(p, "uregex_setText", status); + return; + } + + /* Attempt the match */ + res = uregex_matches(pExpr, 0, &status); + if( !U_SUCCESS(status) ){ + icuFunctionError(p, "uregex_matches", status); + return; + } + + /* Set the text that the regular expression operates on to a NULL + ** pointer. This is not really necessary, but it is tidier than + ** leaving the regular expression object configured with an invalid + ** pointer after this function returns. + */ + uregex_setText(pExpr, 0, 0, &status); + + /* Return 1 or 0. */ + sqlite3_result_int(p, res ? 1 : 0); +} + +/* +** Implementations of scalar functions for case mapping - upper() and +** lower(). Function upper() converts its input to upper-case (ABC). +** Function lower() converts to lower-case (abc). +** +** ICU provides two types of case mapping, "general" case mapping and +** "language specific". Refer to ICU documentation for the differences +** between the two. +** +** To utilise "general" case mapping, the upper() or lower() scalar +** functions are invoked with one argument: +** +** upper('ABC') -> 'abc' +** lower('abc') -> 'ABC' +** +** To access ICU "language specific" case mapping, upper() or lower() +** should be invoked with two arguments. The second argument is the name +** of the locale to use. Passing an empty string ("") or SQL NULL value +** as the second argument is the same as invoking the 1 argument version +** of upper() or lower(). +** +** lower('I', 'en_us') -> 'i' +** lower('I', 'tr_tr') -> 'ı' (small dotless i) +** +** http://www.icu-project.org/userguide/posix.html#case_mappings +*/ +static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){ + const UChar *zInput; + UChar *zOutput; + int nInput; + int nOutput; + + UErrorCode status = U_ZERO_ERROR; + const char *zLocale = 0; + + assert(nArg==1 || nArg==2); + if( nArg==2 ){ + zLocale = (const char *)sqlite3_value_text(apArg[1]); + } + + zInput = sqlite3_value_text16(apArg[0]); + if( !zInput ){ + return; + } + nInput = sqlite3_value_bytes16(apArg[0]); + + nOutput = nInput * 2 + 2; + zOutput = sqlite3_malloc(nOutput); + if( !zOutput ){ + return; + } + + if( sqlite3_user_data(p) ){ + u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status); + }else{ + u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status); + } + + if( !U_SUCCESS(status) ){ + icuFunctionError(p, "u_strToLower()/u_strToUpper", status); + return; + } + + sqlite3_result_text16(p, zOutput, -1, xFree); +} + +/* +** Collation sequence destructor function. The pCtx argument points to +** a UCollator structure previously allocated using ucol_open(). +*/ +static void icuCollationDel(void *pCtx){ + UCollator *p = (UCollator *)pCtx; + ucol_close(p); +} + +/* +** Collation sequence comparison function. The pCtx argument points to +** a UCollator structure previously allocated using ucol_open(). +*/ +static int icuCollationColl( + void *pCtx, + int nLeft, + const void *zLeft, + int nRight, + const void *zRight +){ + UCollationResult res; + UCollator *p = (UCollator *)pCtx; + res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2); + switch( res ){ + case UCOL_LESS: return -1; + case UCOL_GREATER: return +1; + case UCOL_EQUAL: return 0; + } + assert(!"Unexpected return value from ucol_strcoll()"); + return 0; +} + +/* +** Implementation of the scalar function icu_load_collation(). +** +** This scalar function is used to add ICU collation based collation +** types to an SQLite database connection. It is intended to be called +** as follows: +** +** SELECT icu_load_collation(, ); +** +** Where is a string containing an ICU locale identifier (i.e. +** "en_AU", "tr_TR" etc.) and is the name of the +** collation sequence to create. +*/ +static void icuLoadCollation( + sqlite3_context *p, + int nArg, + sqlite3_value **apArg +){ + sqlite3 *db = (sqlite3 *)sqlite3_user_data(p); + UErrorCode status = U_ZERO_ERROR; + const char *zLocale; /* Locale identifier - (eg. "jp_JP") */ + const char *zName; /* SQL Collation sequence name (eg. "japanese") */ + UCollator *pUCollator; /* ICU library collation object */ + int rc; /* Return code from sqlite3_create_collation_x() */ + + assert(nArg==2); + zLocale = (const char *)sqlite3_value_text(apArg[0]); + zName = (const char *)sqlite3_value_text(apArg[1]); + + if( !zLocale || !zName ){ + return; + } + + pUCollator = ucol_open(zLocale, &status); + if( !U_SUCCESS(status) ){ + icuFunctionError(p, "ucol_open", status); + return; + } + assert(p); + + rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator, + icuCollationColl, icuCollationDel + ); + if( rc!=SQLITE_OK ){ + ucol_close(pUCollator); + sqlite3_result_error(p, "Error registering collation function", -1); + } +} + +/* +** Register the ICU extension functions with database db. +*/ +SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){ + struct IcuScalar { + const char *zName; /* Function name */ + int nArg; /* Number of arguments */ + int enc; /* Optimal text encoding */ + void *pContext; /* sqlite3_user_data() context */ + void (*xFunc)(sqlite3_context*,int,sqlite3_value**); + } scalars[] = { + {"regexp", 2, SQLITE_ANY, 0, icuRegexpFunc}, + + {"lower", 1, SQLITE_UTF16, 0, icuCaseFunc16}, + {"lower", 2, SQLITE_UTF16, 0, icuCaseFunc16}, + {"upper", 1, SQLITE_UTF16, (void*)1, icuCaseFunc16}, + {"upper", 2, SQLITE_UTF16, (void*)1, icuCaseFunc16}, + + {"lower", 1, SQLITE_UTF8, 0, icuCaseFunc16}, + {"lower", 2, SQLITE_UTF8, 0, icuCaseFunc16}, + {"upper", 1, SQLITE_UTF8, (void*)1, icuCaseFunc16}, + {"upper", 2, SQLITE_UTF8, (void*)1, icuCaseFunc16}, + + {"like", 2, SQLITE_UTF8, 0, icuLikeFunc}, + {"like", 3, SQLITE_UTF8, 0, icuLikeFunc}, + + {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation}, + }; + + int rc = SQLITE_OK; + int i; + + for(i=0; rc==SQLITE_OK && i<(sizeof(scalars)/sizeof(struct IcuScalar)); i++){ + struct IcuScalar *p = &scalars[i]; + rc = sqlite3_create_function( + db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0 + ); + } + + return rc; +} + +#if !SQLITE_CORE +SQLITE_API int sqlite3_extension_init( + sqlite3 *db, + char **pzErrMsg, + const sqlite3_api_routines *pApi +){ + SQLITE_EXTENSION_INIT2(pApi) + return sqlite3IcuInit(db); +} +#endif + +#endif + +/************** End of icu.c *************************************************/ +/************** Begin file fts3_icu.c ****************************************/ +/* +** 2007 June 22 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This file implements a tokenizer for fts3 based on the ICU library. +** +** $Id: fts3_icu.c,v 1.3 2008/09/01 18:34:20 danielk1977 Exp $ +*/ + +#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) +#ifdef SQLITE_ENABLE_ICU + + +#include +#include + +typedef struct IcuTokenizer IcuTokenizer; +typedef struct IcuCursor IcuCursor; + +struct IcuTokenizer { + sqlite3_tokenizer base; + char *zLocale; +}; + +struct IcuCursor { + sqlite3_tokenizer_cursor base; + + UBreakIterator *pIter; /* ICU break-iterator object */ + int nChar; /* Number of UChar elements in pInput */ + UChar *aChar; /* Copy of input using utf-16 encoding */ + int *aOffset; /* Offsets of each character in utf-8 input */ + + int nBuffer; + char *zBuffer; + + int iToken; +}; + +/* +** Create a new tokenizer instance. +*/ +static int icuCreate( + int argc, /* Number of entries in argv[] */ + const char * const *argv, /* Tokenizer creation arguments */ + sqlite3_tokenizer **ppTokenizer /* OUT: Created tokenizer */ +){ + IcuTokenizer *p; + int n = 0; + + if( argc>0 ){ + n = strlen(argv[0])+1; + } + p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n); + if( !p ){ + return SQLITE_NOMEM; + } + memset(p, 0, sizeof(IcuTokenizer)); + + if( n ){ + p->zLocale = (char *)&p[1]; + memcpy(p->zLocale, argv[0], n); + } + + *ppTokenizer = (sqlite3_tokenizer *)p; + + return SQLITE_OK; +} + +/* +** Destroy a tokenizer +*/ +static int icuDestroy(sqlite3_tokenizer *pTokenizer){ + IcuTokenizer *p = (IcuTokenizer *)pTokenizer; + sqlite3_free(p); + return SQLITE_OK; +} + +/* +** Prepare to begin tokenizing a particular string. The input +** string to be tokenized is pInput[0..nBytes-1]. A cursor +** used to incrementally tokenize this string is returned in +** *ppCursor. +*/ +static int icuOpen( + sqlite3_tokenizer *pTokenizer, /* The tokenizer */ + const char *zInput, /* Input string */ + int nInput, /* Length of zInput in bytes */ + sqlite3_tokenizer_cursor **ppCursor /* OUT: Tokenization cursor */ +){ + IcuTokenizer *p = (IcuTokenizer *)pTokenizer; + IcuCursor *pCsr; + + const int32_t opt = U_FOLD_CASE_DEFAULT; + UErrorCode status = U_ZERO_ERROR; + int nChar; + + UChar32 c; + int iInput = 0; + int iOut = 0; + + *ppCursor = 0; + + if( nInput<0 ){ + nInput = strlen(zInput); + } + nChar = nInput+1; + pCsr = (IcuCursor *)sqlite3_malloc( + sizeof(IcuCursor) + /* IcuCursor */ + nChar * sizeof(UChar) + /* IcuCursor.aChar[] */ + (nChar+1) * sizeof(int) /* IcuCursor.aOffset[] */ + ); + if( !pCsr ){ + return SQLITE_NOMEM; + } + memset(pCsr, 0, sizeof(IcuCursor)); + pCsr->aChar = (UChar *)&pCsr[1]; + pCsr->aOffset = (int *)&pCsr->aChar[nChar]; + + pCsr->aOffset[iOut] = iInput; + U8_NEXT(zInput, iInput, nInput, c); + while( c>0 ){ + int isError = 0; + c = u_foldCase(c, opt); + U16_APPEND(pCsr->aChar, iOut, nChar, c, isError); + if( isError ){ + sqlite3_free(pCsr); + return SQLITE_ERROR; + } + pCsr->aOffset[iOut] = iInput; + + if( iInputpIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status); + if( !U_SUCCESS(status) ){ + sqlite3_free(pCsr); + return SQLITE_ERROR; + } + pCsr->nChar = iOut; + + ubrk_first(pCsr->pIter); + *ppCursor = (sqlite3_tokenizer_cursor *)pCsr; + return SQLITE_OK; +} + +/* +** Close a tokenization cursor previously opened by a call to icuOpen(). +*/ +static int icuClose(sqlite3_tokenizer_cursor *pCursor){ + IcuCursor *pCsr = (IcuCursor *)pCursor; + ubrk_close(pCsr->pIter); + sqlite3_free(pCsr->zBuffer); + sqlite3_free(pCsr); + return SQLITE_OK; +} + +/* +** Extract the next token from a tokenization cursor. +*/ +static int icuNext( + sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by simpleOpen */ + const char **ppToken, /* OUT: *ppToken is the token text */ + int *pnBytes, /* OUT: Number of bytes in token */ + int *piStartOffset, /* OUT: Starting offset of token */ + int *piEndOffset, /* OUT: Ending offset of token */ + int *piPosition /* OUT: Position integer of token */ +){ + IcuCursor *pCsr = (IcuCursor *)pCursor; + + int iStart = 0; + int iEnd = 0; + int nByte = 0; + + while( iStart==iEnd ){ + UChar32 c; + + iStart = ubrk_current(pCsr->pIter); + iEnd = ubrk_next(pCsr->pIter); + if( iEnd==UBRK_DONE ){ + return SQLITE_DONE; + } + + while( iStartaChar, iWhite, pCsr->nChar, c); + if( u_isspace(c) ){ + iStart = iWhite; + }else{ + break; + } + } + assert(iStart<=iEnd); + } + + do { + UErrorCode status = U_ZERO_ERROR; + if( nByte ){ + char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte); + if( !zNew ){ + return SQLITE_NOMEM; + } + pCsr->zBuffer = zNew; + pCsr->nBuffer = nByte; + } + + u_strToUTF8( + pCsr->zBuffer, pCsr->nBuffer, &nByte, /* Output vars */ + &pCsr->aChar[iStart], iEnd-iStart, /* Input vars */ + &status /* Output success/failure */ + ); + } while( nByte>pCsr->nBuffer ); + + *ppToken = pCsr->zBuffer; + *pnBytes = nByte; + *piStartOffset = pCsr->aOffset[iStart]; + *piEndOffset = pCsr->aOffset[iEnd]; + *piPosition = pCsr->iToken++; + + return SQLITE_OK; +} + +/* +** The set of routines that implement the simple tokenizer +*/ +static const sqlite3_tokenizer_module icuTokenizerModule = { + 0, /* iVersion */ + icuCreate, /* xCreate */ + icuDestroy, /* xCreate */ + icuOpen, /* xOpen */ + icuClose, /* xClose */ + icuNext, /* xNext */ +}; + +/* +** Set *ppModule to point at the implementation of the ICU tokenizer. +*/ +SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule( + sqlite3_tokenizer_module const**ppModule +){ + *ppModule = &icuTokenizerModule; +} + +#endif /* defined(SQLITE_ENABLE_ICU) */ +#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */ + +/************** End of fts3_icu.c ********************************************/ + +SQLITE_PRIVATE Pgno ptrmapPageno(BtShared *pBt, Pgno pgno); + +// TODO: Do something to read and initialize the database in case +// the client did not prior to calling this. +int tryReadEveryDbPage(sqlite3 *db, Pgno start, Pgno *pBadPage, int *pBadPageType, int *pBadPageZero) { + Btree *pBt = db->aDb[0].pBt; + // Get the pager, from there we can read every page in the db file + Pager *pPager = sqlite3BtreePager(pBt); + + // Logical database size and also the highest actual page number. + // (the name sounds like btree last page but it's for the entire DB). + Pgno lastPage = sqlite3BtreeLastPage(pBt); + + // Buffer for page data to be written to, if necessary + u8 *pageBuf = (u8 *)malloc(pPager->pageSize); + + PgHdr page; + page.pPager = pPager; + page.pExtra = NULL; + page.pDirty = NULL; + + // Calculate the page number that includes the "pending" byte so it can be skipped + // as that page is never written or read directly by SQLite and is never handled + // by the pager codec. + Pgno skip = (PENDING_BYTE / pPager->pageSize) + 1; + int rc = SQLITE_OK; + + for(page.pgno = start; page.pgno <= lastPage; ++page.pgno) { + if(page.pgno == skip) + continue; + + page.flags = 0; + page.pData = pageBuf; + rc = readDbPage(&page); + + // If page was read but is corrupt then set *pBadPageZero and *pBadPageType. + if(rc == SQLITE_CORRUPT) { + // Check if page is all null bytes + const u8 *pdata = (const u8 *)page.pData; + if(pdata != NULL) { + *pBadPageZero = 1; + int i; + for(i = 0; i < pPager->pageSize; ++i) + if(pdata[i] != 0) { + *pBadPageZero = 0; + break; + } + } + else + *pBadPageZero = -1; + + // Get page type + *pBadPageType = -1; // Unknown + PgHdr ptrMap; + ptrMap.pPager = pPager; + ptrMap.pExtra = NULL; + ptrMap.pDirty = NULL; + ptrMap.pgno = PTRMAP_PAGENO(pBt->pBt, page.pgno); + // If the ptrmap page is not 0 and not the bad page then we can get the bad page's + // expected type IF the map page is successfully read. + if(ptrMap.pgno != 0 && ptrMap.pgno != page.pgno) { + ptrMap.flags = 0; + ptrMap.pData = pageBuf; //reuse pageBuf because we're done looking at bad page data + int rc2 = readDbPage(&ptrMap); + + if(rc2 == SQLITE_OK) { + int offset = PTRMAP_PTROFFSET(ptrMap.pgno, page.pgno); + *pBadPageType = *(((u8 *)ptrMap.pData) + offset); + } + + // xReleaseZeroCopy for pointer map page if necessary + if(ptrMap.flags & PGHDR_ZERO_COPY) + unpinZeroCopy(&ptrMap); + } + } + + // xReleaseZeroCopy for checked page if necessary. + if(page.flags & PGHDR_ZERO_COPY) + unpinZeroCopy(&page); + + // If any error occurred then set *pBadPage to the bad page number and return the error. + if(rc != SQLITE_OK) { + *pBadPage = page.pgno; + break; + } + } + + free(pageBuf); + return rc; +} diff --git a/fdbserver/sqlite/sqlite3.h b/fdbserver/sqlite/sqlite3.h new file mode 100755 index 0000000000..fc33edfd73 --- /dev/null +++ b/fdbserver/sqlite/sqlite3.h @@ -0,0 +1,6486 @@ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the interface that the SQLite library +** presents to client programs. If a C-function, structure, datatype, +** or constant definition does not appear in this file, then it is +** not a published API of SQLite, is subject to change without +** notice, and should not be referenced by programs that use SQLite. +** +** Some of the definitions that are in this file are marked as +** "experimental". Experimental interfaces are normally new +** features recently added to SQLite. We do not anticipate changes +** to experimental interfaces but reserve the right to make minor changes +** if experience from use "in the wild" suggest such changes are prudent. +** +** The official C-language API documentation for SQLite is derived +** from comments in this file. This file is the authoritative source +** on how SQLite interfaces are suppose to operate. +** +** The name of this file under configuration management is "sqlite.h.in". +** The makefile makes some minor changes to this file (such as inserting +** the version number) and changes its name to "sqlite3.h" as +** part of the build process. +*/ +#ifndef _SQLITE3_H_ +#define _SQLITE3_H_ +#include /* Needed for the definition of va_list */ + +#define SQLITE_HAS_CODEC 1 +#define SQLITE_HAS_CODEC_NO_ENCRYPTION 1 +/* +** Make sure we can call this stuff from C++. +*/ +#ifdef __cplusplus +extern "C" { +#endif + + +/* +** Add the ability to override 'extern' +*/ +#ifndef SQLITE_EXTERN +# define SQLITE_EXTERN extern +#endif + +#ifndef SQLITE_API +# define SQLITE_API +#endif + + +/* +** These no-op macros are used in front of interfaces to mark those +** interfaces as either deprecated or experimental. New applications +** should not use deprecated interfaces - they are support for backwards +** compatibility only. Application writers should be aware that +** experimental interfaces are subject to change in point releases. +** +** These macros used to resolve to various kinds of compiler magic that +** would generate warning messages when they were used. But that +** compiler magic ended up generating such a flurry of bug reports +** that we have taken it all out and gone back to using simple +** noop macros. +*/ +#define SQLITE_DEPRECATED +#define SQLITE_EXPERIMENTAL + +/* +** Ensure these symbols were not defined by some previous header file. +*/ +#ifdef SQLITE_VERSION +# undef SQLITE_VERSION +#endif +#ifdef SQLITE_VERSION_NUMBER +# undef SQLITE_VERSION_NUMBER +#endif + +/* +** CAPI3REF: Compile-Time Library Version Numbers +** +** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header +** evaluates to a string literal that is the SQLite version in the +** format "X.Y.Z" where X is the major version number (always 3 for +** SQLite3) and Y is the minor version number and Z is the release number.)^ +** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer +** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same +** numbers used in [SQLITE_VERSION].)^ +** The SQLITE_VERSION_NUMBER for any given release of SQLite will also +** be larger than the release from which it is derived. Either Y will +** be held constant and Z will be incremented or else Y will be incremented +** and Z will be reset to zero. +** +** Since version 3.6.18, SQLite source code has been stored in the +** Fossil configuration management +** system. ^The SQLITE_SOURCE_ID macro evaluates to +** a string which identifies a particular check-in of SQLite +** within its configuration management system. ^The SQLITE_SOURCE_ID +** string contains the date and time of the check-in (UTC) and an SHA1 +** hash of the entire source tree. +** +** See also: [sqlite3_libversion()], +** [sqlite3_libversion_number()], [sqlite3_sourceid()], +** [sqlite_version()] and [sqlite_source_id()]. +*/ +#define SQLITE_VERSION "3.7.6" +#define SQLITE_VERSION_NUMBER 3007006 +#define SQLITE_SOURCE_ID "2011-03-17 01:58:21 869f894798a65f8bc0e0b083866a784fa0189f68" + +/* +** CAPI3REF: Run-Time Library Version Numbers +** KEYWORDS: sqlite3_version, sqlite3_sourceid +** +** These interfaces provide the same information as the [SQLITE_VERSION], +** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros +** but are associated with the library instead of the header file. ^(Cautious +** programmers might include assert() statements in their application to +** verify that values returned by these interfaces match the macros in +** the header, and thus insure that the application is +** compiled with matching library and header files. +** +**
    +** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
    +** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
    +** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
    +** 
    )^ +** +** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION] +** macro. ^The sqlite3_libversion() function returns a pointer to the +** to the sqlite3_version[] string constant. The sqlite3_libversion() +** function is provided for use in DLLs since DLL users usually do not have +** direct access to string constants within the DLL. ^The +** sqlite3_libversion_number() function returns an integer equal to +** [SQLITE_VERSION_NUMBER]. ^The sqlite3_sourceid() function returns +** a pointer to a string constant whose value is the same as the +** [SQLITE_SOURCE_ID] C preprocessor macro. +** +** See also: [sqlite_version()] and [sqlite_source_id()]. +*/ +SQLITE_API SQLITE_EXTERN const char sqlite3_version[]; +SQLITE_API const char *sqlite3_libversion(void); +SQLITE_API const char *sqlite3_sourceid(void); +SQLITE_API int sqlite3_libversion_number(void); + +/* +** CAPI3REF: Run-Time Library Compilation Options Diagnostics +** +** ^The sqlite3_compileoption_used() function returns 0 or 1 +** indicating whether the specified option was defined at +** compile time. ^The SQLITE_ prefix may be omitted from the +** option name passed to sqlite3_compileoption_used(). +** +** ^The sqlite3_compileoption_get() function allows iterating +** over the list of options that were defined at compile time by +** returning the N-th compile time option string. ^If N is out of range, +** sqlite3_compileoption_get() returns a NULL pointer. ^The SQLITE_ +** prefix is omitted from any strings returned by +** sqlite3_compileoption_get(). +** +** ^Support for the diagnostic functions sqlite3_compileoption_used() +** and sqlite3_compileoption_get() may be omitted by specifying the +** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time. +** +** See also: SQL functions [sqlite_compileoption_used()] and +** [sqlite_compileoption_get()] and the [compile_options pragma]. +*/ +#ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS +SQLITE_API int sqlite3_compileoption_used(const char *zOptName); +SQLITE_API const char *sqlite3_compileoption_get(int N); +#endif + +/* +** CAPI3REF: Test To See If The Library Is Threadsafe +** +** ^The sqlite3_threadsafe() function returns zero if and only if +** SQLite was compiled mutexing code omitted due to the +** [SQLITE_THREADSAFE] compile-time option being set to 0. +** +** SQLite can be compiled with or without mutexes. When +** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes +** are enabled and SQLite is threadsafe. When the +** [SQLITE_THREADSAFE] macro is 0, +** the mutexes are omitted. Without the mutexes, it is not safe +** to use SQLite concurrently from more than one thread. +** +** Enabling mutexes incurs a measurable performance penalty. +** So if speed is of utmost importance, it makes sense to disable +** the mutexes. But for maximum safety, mutexes should be enabled. +** ^The default behavior is for mutexes to be enabled. +** +** This interface can be used by an application to make sure that the +** version of SQLite that it is linking against was compiled with +** the desired setting of the [SQLITE_THREADSAFE] macro. +** +** This interface only reports on the compile-time mutex setting +** of the [SQLITE_THREADSAFE] flag. If SQLite is compiled with +** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but +** can be fully or partially disabled using a call to [sqlite3_config()] +** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD], +** or [SQLITE_CONFIG_MUTEX]. ^(The return value of the +** sqlite3_threadsafe() function shows only the compile-time setting of +** thread safety, not any run-time changes to that setting made by +** sqlite3_config(). In other words, the return value from sqlite3_threadsafe() +** is unchanged by calls to sqlite3_config().)^ +** +** See the [threading mode] documentation for additional information. +*/ +SQLITE_API int sqlite3_threadsafe(void); + +/* +** CAPI3REF: Database Connection Handle +** KEYWORDS: {database connection} {database connections} +** +** Each open SQLite database is represented by a pointer to an instance of +** the opaque structure named "sqlite3". It is useful to think of an sqlite3 +** pointer as an object. The [sqlite3_open()], [sqlite3_open16()], and +** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()] +** is its destructor. There are many other interfaces (such as +** [sqlite3_prepare_v2()], [sqlite3_create_function()], and +** [sqlite3_busy_timeout()] to name but three) that are methods on an +** sqlite3 object. +*/ +typedef struct sqlite3 sqlite3; + +/* +** CAPI3REF: 64-Bit Integer Types +** KEYWORDS: sqlite_int64 sqlite_uint64 +** +** Because there is no cross-platform way to specify 64-bit integer types +** SQLite includes typedefs for 64-bit signed and unsigned integers. +** +** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions. +** The sqlite_int64 and sqlite_uint64 types are supported for backwards +** compatibility only. +** +** ^The sqlite3_int64 and sqlite_int64 types can store integer values +** between -9223372036854775808 and +9223372036854775807 inclusive. ^The +** sqlite3_uint64 and sqlite_uint64 types can store integer values +** between 0 and +18446744073709551615 inclusive. +*/ +#ifdef SQLITE_INT64_TYPE + typedef SQLITE_INT64_TYPE sqlite_int64; + typedef unsigned SQLITE_INT64_TYPE sqlite_uint64; +#elif defined(_MSC_VER) || defined(__BORLANDC__) + typedef __int64 sqlite_int64; + typedef unsigned __int64 sqlite_uint64; +#else + typedef long long int sqlite_int64; + typedef unsigned long long int sqlite_uint64; +#endif +typedef sqlite_int64 sqlite3_int64; +typedef sqlite_uint64 sqlite3_uint64; + +/* +** If compiling for a processor that lacks floating point support, +** substitute integer for floating-point. +*/ +#ifdef SQLITE_OMIT_FLOATING_POINT +# define double sqlite3_int64 +#endif + +/* +** CAPI3REF: Closing A Database Connection +** +** ^The sqlite3_close() routine is the destructor for the [sqlite3] object. +** ^Calls to sqlite3_close() return SQLITE_OK if the [sqlite3] object is +** successfully destroyed and all associated resources are deallocated. +** +** Applications must [sqlite3_finalize | finalize] all [prepared statements] +** and [sqlite3_blob_close | close] all [BLOB handles] associated with +** the [sqlite3] object prior to attempting to close the object. ^If +** sqlite3_close() is called on a [database connection] that still has +** outstanding [prepared statements] or [BLOB handles], then it returns +** SQLITE_BUSY. +** +** ^If [sqlite3_close()] is invoked while a transaction is open, +** the transaction is automatically rolled back. +** +** The C parameter to [sqlite3_close(C)] must be either a NULL +** pointer or an [sqlite3] object pointer obtained +** from [sqlite3_open()], [sqlite3_open16()], or +** [sqlite3_open_v2()], and not previously closed. +** ^Calling sqlite3_close() with a NULL pointer argument is a +** harmless no-op. +*/ +SQLITE_API int sqlite3_close(sqlite3 *); + +/* +** The type for a callback function. +** This is legacy and deprecated. It is included for historical +** compatibility and is not documented. +*/ +typedef int (*sqlite3_callback)(void*,int,char**, char**); + +/* +** CAPI3REF: One-Step Query Execution Interface +** +** The sqlite3_exec() interface is a convenience wrapper around +** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()], +** that allows an application to run multiple statements of SQL +** without having to use a lot of C code. +** +** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded, +** semicolon-separate SQL statements passed into its 2nd argument, +** in the context of the [database connection] passed in as its 1st +** argument. ^If the callback function of the 3rd argument to +** sqlite3_exec() is not NULL, then it is invoked for each result row +** coming out of the evaluated SQL statements. ^The 4th argument to +** to sqlite3_exec() is relayed through to the 1st argument of each +** callback invocation. ^If the callback pointer to sqlite3_exec() +** is NULL, then no callback is ever invoked and result rows are +** ignored. +** +** ^If an error occurs while evaluating the SQL statements passed into +** sqlite3_exec(), then execution of the current statement stops and +** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec() +** is not NULL then any error message is written into memory obtained +** from [sqlite3_malloc()] and passed back through the 5th parameter. +** To avoid memory leaks, the application should invoke [sqlite3_free()] +** on error message strings returned through the 5th parameter of +** of sqlite3_exec() after the error message string is no longer needed. +** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors +** occur, then sqlite3_exec() sets the pointer in its 5th parameter to +** NULL before returning. +** +** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec() +** routine returns SQLITE_ABORT without invoking the callback again and +** without running any subsequent SQL statements. +** +** ^The 2nd argument to the sqlite3_exec() callback function is the +** number of columns in the result. ^The 3rd argument to the sqlite3_exec() +** callback is an array of pointers to strings obtained as if from +** [sqlite3_column_text()], one for each column. ^If an element of a +** result row is NULL then the corresponding string pointer for the +** sqlite3_exec() callback is a NULL pointer. ^The 4th argument to the +** sqlite3_exec() callback is an array of pointers to strings where each +** entry represents the name of corresponding result column as obtained +** from [sqlite3_column_name()]. +** +** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer +** to an empty string, or a pointer that contains only whitespace and/or +** SQL comments, then no SQL statements are evaluated and the database +** is not changed. +** +** Restrictions: +** +**
      +**
    • The application must insure that the 1st parameter to sqlite3_exec() +** is a valid and open [database connection]. +**
    • The application must not close [database connection] specified by +** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running. +**
    • The application must not modify the SQL statement text passed into +** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running. +**
    +*/ +SQLITE_API int sqlite3_exec( + sqlite3*, /* An open database */ + const char *sql, /* SQL to be evaluated */ + int (*callback)(void*,int,char**,char**), /* Callback function */ + void *, /* 1st argument to callback */ + char **errmsg /* Error msg written here */ +); + +/* +** CAPI3REF: Result Codes +** KEYWORDS: SQLITE_OK {error code} {error codes} +** KEYWORDS: {result code} {result codes} +** +** Many SQLite functions return an integer result code from the set shown +** here in order to indicates success or failure. +** +** New error codes may be added in future versions of SQLite. +** +** See also: [SQLITE_IOERR_READ | extended result codes] +*/ +#define SQLITE_OK 0 /* Successful result */ +/* beginning-of-error-codes */ +#define SQLITE_ERROR 1 /* SQL error or missing database */ +#define SQLITE_INTERNAL 2 /* Internal logic error in SQLite */ +#define SQLITE_PERM 3 /* Access permission denied */ +#define SQLITE_ABORT 4 /* Callback routine requested an abort */ +#define SQLITE_BUSY 5 /* The database file is locked */ +#define SQLITE_LOCKED 6 /* A table in the database is locked */ +#define SQLITE_NOMEM 7 /* A malloc() failed */ +#define SQLITE_READONLY 8 /* Attempt to write a readonly database */ +#define SQLITE_INTERRUPT 9 /* Operation terminated by sqlite3_interrupt()*/ +#define SQLITE_IOERR 10 /* Some kind of disk I/O error occurred */ +#define SQLITE_CORRUPT 11 /* The database disk image is malformed */ +#define SQLITE_NOTFOUND 12 /* Unknown opcode in sqlite3_file_control() */ +#define SQLITE_FULL 13 /* Insertion failed because database is full */ +#define SQLITE_CANTOPEN 14 /* Unable to open the database file */ +#define SQLITE_PROTOCOL 15 /* Database lock protocol error */ +#define SQLITE_EMPTY 16 /* Database is empty */ +#define SQLITE_SCHEMA 17 /* The database schema changed */ +#define SQLITE_TOOBIG 18 /* String or BLOB exceeds size limit */ +#define SQLITE_CONSTRAINT 19 /* Abort due to constraint violation */ +#define SQLITE_MISMATCH 20 /* Data type mismatch */ +#define SQLITE_MISUSE 21 /* Library used incorrectly */ +#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */ +#define SQLITE_AUTH 23 /* Authorization denied */ +#define SQLITE_FORMAT 24 /* Auxiliary database format error */ +#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */ +#define SQLITE_NOTADB 26 /* File opened that is not a database file */ +#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */ +#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */ +/* end-of-error-codes */ + +/* +** CAPI3REF: Extended Result Codes +** KEYWORDS: {extended error code} {extended error codes} +** KEYWORDS: {extended result code} {extended result codes} +** +** In its default configuration, SQLite API routines return one of 26 integer +** [SQLITE_OK | result codes]. However, experience has shown that many of +** these result codes are too coarse-grained. They do not provide as +** much information about problems as programmers might like. In an effort to +** address this, newer versions of SQLite (version 3.3.8 and later) include +** support for additional result codes that provide more detailed information +** about errors. The extended result codes are enabled or disabled +** on a per database connection basis using the +** [sqlite3_extended_result_codes()] API. +** +** Some of the available extended result codes are listed here. +** One may expect the number of extended result codes will be expand +** over time. Software that uses extended result codes should expect +** to see new result codes in future releases of SQLite. +** +** The SQLITE_OK result code will never be extended. It will always +** be exactly zero. +*/ +#define SQLITE_IOERR_READ (SQLITE_IOERR | (1<<8)) +#define SQLITE_IOERR_SHORT_READ (SQLITE_IOERR | (2<<8)) +#define SQLITE_IOERR_WRITE (SQLITE_IOERR | (3<<8)) +#define SQLITE_IOERR_FSYNC (SQLITE_IOERR | (4<<8)) +#define SQLITE_IOERR_DIR_FSYNC (SQLITE_IOERR | (5<<8)) +#define SQLITE_IOERR_TRUNCATE (SQLITE_IOERR | (6<<8)) +#define SQLITE_IOERR_FSTAT (SQLITE_IOERR | (7<<8)) +#define SQLITE_IOERR_UNLOCK (SQLITE_IOERR | (8<<8)) +#define SQLITE_IOERR_RDLOCK (SQLITE_IOERR | (9<<8)) +#define SQLITE_IOERR_DELETE (SQLITE_IOERR | (10<<8)) +#define SQLITE_IOERR_BLOCKED (SQLITE_IOERR | (11<<8)) +#define SQLITE_IOERR_NOMEM (SQLITE_IOERR | (12<<8)) +#define SQLITE_IOERR_ACCESS (SQLITE_IOERR | (13<<8)) +#define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8)) +#define SQLITE_IOERR_LOCK (SQLITE_IOERR | (15<<8)) +#define SQLITE_IOERR_CLOSE (SQLITE_IOERR | (16<<8)) +#define SQLITE_IOERR_DIR_CLOSE (SQLITE_IOERR | (17<<8)) +#define SQLITE_IOERR_SHMOPEN (SQLITE_IOERR | (18<<8)) +#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8)) +#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8)) +#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8)) +#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8)) +#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8)) + +/* +** CAPI3REF: Flags For File Open Operations +** +** These bit values are intended for use in the +** 3rd parameter to the [sqlite3_open_v2()] interface and +** in the 4th parameter to the xOpen method of the +** [sqlite3_vfs] object. +*/ +#define SQLITE_OPEN_READONLY 0x00000001 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_READWRITE 0x00000002 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_CREATE 0x00000004 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_DELETEONCLOSE 0x00000008 /* VFS only */ +#define SQLITE_OPEN_EXCLUSIVE 0x00000010 /* VFS only */ +#define SQLITE_OPEN_AUTOPROXY 0x00000020 /* VFS only */ +#define SQLITE_OPEN_MAIN_DB 0x00000100 /* VFS only */ +#define SQLITE_OPEN_TEMP_DB 0x00000200 /* VFS only */ +#define SQLITE_OPEN_TRANSIENT_DB 0x00000400 /* VFS only */ +#define SQLITE_OPEN_MAIN_JOURNAL 0x00000800 /* VFS only */ +#define SQLITE_OPEN_TEMP_JOURNAL 0x00001000 /* VFS only */ +#define SQLITE_OPEN_SUBJOURNAL 0x00002000 /* VFS only */ +#define SQLITE_OPEN_MASTER_JOURNAL 0x00004000 /* VFS only */ +#define SQLITE_OPEN_NOMUTEX 0x00008000 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_FULLMUTEX 0x00010000 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_SHAREDCACHE 0x00020000 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_PRIVATECACHE 0x00040000 /* Ok for sqlite3_open_v2() */ +#define SQLITE_OPEN_WAL 0x00080000 /* VFS only */ +#define SQLITE_OPEN_READAHEAD 0x01000000 /* Ok for sqlite3_open_v2() -- NOT YET USED */ + +/* Reserved: 0x00F00000 */ + +/* +** CAPI3REF: Device Characteristics +** +** The xDeviceCharacteristics method of the [sqlite3_io_methods] +** object returns an integer which is a vector of the these +** bit values expressing I/O characteristics of the mass storage +** device that holds the file that the [sqlite3_io_methods] +** refers to. +** +** The SQLITE_IOCAP_ATOMIC property means that all writes of +** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values +** mean that writes of blocks that are nnn bytes in size and +** are aligned to an address which is an integer multiple of +** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means +** that when data is appended to a file, the data is appended +** first then the size of the file is extended, never the other +** way around. The SQLITE_IOCAP_SEQUENTIAL property means that +** information is written to disk in the same order as calls +** to xWrite(). +*/ +#define SQLITE_IOCAP_ATOMIC 0x00000001 +#define SQLITE_IOCAP_ATOMIC512 0x00000002 +#define SQLITE_IOCAP_ATOMIC1K 0x00000004 +#define SQLITE_IOCAP_ATOMIC2K 0x00000008 +#define SQLITE_IOCAP_ATOMIC4K 0x00000010 +#define SQLITE_IOCAP_ATOMIC8K 0x00000020 +#define SQLITE_IOCAP_ATOMIC16K 0x00000040 +#define SQLITE_IOCAP_ATOMIC32K 0x00000080 +#define SQLITE_IOCAP_ATOMIC64K 0x00000100 +#define SQLITE_IOCAP_SAFE_APPEND 0x00000200 +#define SQLITE_IOCAP_SEQUENTIAL 0x00000400 +#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800 + +/* +** CAPI3REF: File Locking Levels +** +** SQLite uses one of these integer values as the second +** argument to calls it makes to the xLock() and xUnlock() methods +** of an [sqlite3_io_methods] object. +*/ +#define SQLITE_LOCK_NONE 0 +#define SQLITE_LOCK_SHARED 1 +#define SQLITE_LOCK_RESERVED 2 +#define SQLITE_LOCK_PENDING 3 +#define SQLITE_LOCK_EXCLUSIVE 4 + +/* +** CAPI3REF: Synchronization Type Flags +** +** When SQLite invokes the xSync() method of an +** [sqlite3_io_methods] object it uses a combination of +** these integer values as the second argument. +** +** When the SQLITE_SYNC_DATAONLY flag is used, it means that the +** sync operation only needs to flush data to mass storage. Inode +** information need not be flushed. If the lower four bits of the flag +** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics. +** If the lower four bits equal SQLITE_SYNC_FULL, that means +** to use Mac OS X style fullsync instead of fsync(). +** +** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags +** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL +** settings. The [synchronous pragma] determines when calls to the +** xSync VFS method occur and applies uniformly across all platforms. +** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how +** energetic or rigorous or forceful the sync operations are and +** only make a difference on Mac OSX for the default SQLite code. +** (Third-party VFS implementations might also make the distinction +** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the +** operating systems natively supported by SQLite, only Mac OSX +** cares about the difference.) +*/ +#define SQLITE_SYNC_NORMAL 0x00002 +#define SQLITE_SYNC_FULL 0x00003 +#define SQLITE_SYNC_DATAONLY 0x00010 + +/* +** CAPI3REF: OS Interface Open File Handle +** +** An [sqlite3_file] object represents an open file in the +** [sqlite3_vfs | OS interface layer]. Individual OS interface +** implementations will +** want to subclass this object by appending additional fields +** for their own use. The pMethods entry is a pointer to an +** [sqlite3_io_methods] object that defines methods for performing +** I/O operations on the open file. +*/ +typedef struct sqlite3_file sqlite3_file; +struct sqlite3_file { + const struct sqlite3_io_methods *pMethods; /* Methods for an open file */ +}; + +/* +** CAPI3REF: OS Interface File Virtual Methods Object +** +** Every file opened by the [sqlite3_vfs] xOpen method populates an +** [sqlite3_file] object (or, more commonly, a subclass of the +** [sqlite3_file] object) with a pointer to an instance of this object. +** This object defines the methods used to perform various operations +** against the open file represented by the [sqlite3_file] object. +** +** If the xOpen method sets the sqlite3_file.pMethods element +** to a non-NULL pointer, then the sqlite3_io_methods.xClose method +** may be invoked even if the xOpen reported that it failed. The +** only way to prevent a call to xClose following a failed xOpen +** is for the xOpen to set the sqlite3_file.pMethods element to NULL. +** +** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or +** [SQLITE_SYNC_FULL]. The first choice is the normal fsync(). +** The second choice is a Mac OS X style fullsync. The [SQLITE_SYNC_DATAONLY] +** flag may be ORed in to indicate that only the data of the file +** and not its inode needs to be synced. +** +** The integer values to xLock() and xUnlock() are one of +**
      +**
    • [SQLITE_LOCK_NONE], +**
    • [SQLITE_LOCK_SHARED], +**
    • [SQLITE_LOCK_RESERVED], +**
    • [SQLITE_LOCK_PENDING], or +**
    • [SQLITE_LOCK_EXCLUSIVE]. +**
    +** xLock() increases the lock. xUnlock() decreases the lock. +** The xCheckReservedLock() method checks whether any database connection, +** either in this process or in some other process, is holding a RESERVED, +** PENDING, or EXCLUSIVE lock on the file. It returns true +** if such a lock exists and false otherwise. +** +** The xFileControl() method is a generic interface that allows custom +** VFS implementations to directly control an open file using the +** [sqlite3_file_control()] interface. The second "op" argument is an +** integer opcode. The third argument is a generic pointer intended to +** point to a structure that may contain arguments or space in which to +** write return values. Potential uses for xFileControl() might be +** functions to enable blocking locks with timeouts, to change the +** locking strategy (for example to use dot-file locks), to inquire +** about the status of a lock, or to break stale locks. The SQLite +** core reserves all opcodes less than 100 for its own use. +** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available. +** Applications that define a custom xFileControl method should use opcodes +** greater than 100 to avoid conflicts. VFS implementations should +** return [SQLITE_NOTFOUND] for file control opcodes that they do not +** recognize. +** +** The xSectorSize() method returns the sector size of the +** device that underlies the file. The sector size is the +** minimum write that can be performed without disturbing +** other bytes in the file. The xDeviceCharacteristics() +** method returns a bit vector describing behaviors of the +** underlying device: +** +**
      +**
    • [SQLITE_IOCAP_ATOMIC] +**
    • [SQLITE_IOCAP_ATOMIC512] +**
    • [SQLITE_IOCAP_ATOMIC1K] +**
    • [SQLITE_IOCAP_ATOMIC2K] +**
    • [SQLITE_IOCAP_ATOMIC4K] +**
    • [SQLITE_IOCAP_ATOMIC8K] +**
    • [SQLITE_IOCAP_ATOMIC16K] +**
    • [SQLITE_IOCAP_ATOMIC32K] +**
    • [SQLITE_IOCAP_ATOMIC64K] +**
    • [SQLITE_IOCAP_SAFE_APPEND] +**
    • [SQLITE_IOCAP_SEQUENTIAL] +**
    +** +** The SQLITE_IOCAP_ATOMIC property means that all writes of +** any size are atomic. The SQLITE_IOCAP_ATOMICnnn values +** mean that writes of blocks that are nnn bytes in size and +** are aligned to an address which is an integer multiple of +** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means +** that when data is appended to a file, the data is appended +** first then the size of the file is extended, never the other +** way around. The SQLITE_IOCAP_SEQUENTIAL property means that +** information is written to disk in the same order as calls +** to xWrite(). +** +** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill +** in the unread portions of the buffer with zeros. A VFS that +** fails to zero-fill short reads might seem to work. However, +** failure to zero-fill short reads will eventually lead to +** database corruption. +*/ +typedef struct sqlite3_io_methods sqlite3_io_methods; +struct sqlite3_io_methods { + int iVersion; + int (*xClose)(sqlite3_file*); + int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); + int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); + int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); + int (*xSync)(sqlite3_file*, int flags); + int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); + int (*xLock)(sqlite3_file*, int); + int (*xUnlock)(sqlite3_file*, int); + int (*xCheckReservedLock)(sqlite3_file*, int *pResOut); + int (*xFileControl)(sqlite3_file*, int op, void *pArg); + int (*xSectorSize)(sqlite3_file*); + int (*xDeviceCharacteristics)(sqlite3_file*); + /* Methods above are valid for version 1 */ + int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**); + int (*xShmLock)(sqlite3_file*, int offset, int n, int flags); + void (*xShmBarrier)(sqlite3_file*); + int (*xShmUnmap)(sqlite3_file*, int deleteFlag); + /* Methods above are valid for version 2 */ + /* Additional methods may be added in future releases */ + int (*xReadZeroCopy)(sqlite3_file*, void** data, int iAmt, sqlite3_int64 iOfst, int *pWasCached); + int (*xReleaseZeroCopy)(sqlite3_file*, void* data, int iAmt, sqlite3_int64 iOfst); +}; + +/* +** CAPI3REF: Standard File Control Opcodes +** +** These integer constants are opcodes for the xFileControl method +** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()] +** interface. +** +** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging. This +** opcode causes the xFileControl method to write the current state of +** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED], +** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE]) +** into an integer that the pArg argument points to. This capability +** is used during testing and only needs to be supported when SQLITE_TEST +** is defined. +** +** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS +** layer a hint of how large the database file will grow to be during the +** current transaction. This hint is not guaranteed to be accurate but it +** is often close. The underlying VFS might choose to preallocate database +** file space based on this hint in order to help writes to the database +** file run faster. +** +** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS +** extends and truncates the database file in chunks of a size specified +** by the user. The fourth argument to [sqlite3_file_control()] should +** point to an integer (type int) containing the new chunk-size to use +** for the nominated database. Allocating database file space in large +** chunks (say 1MB at a time), may reduce file-system fragmentation and +** improve performance on some systems. +** +** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer +** to the [sqlite3_file] object associated with a particular database +** connection. See the [sqlite3_file_control()] documentation for +** additional information. +** +** ^(The [SQLITE_FCNTL_SYNC_OMITTED] opcode is generated internally by +** SQLite and sent to all VFSes in place of a call to the xSync method +** when the database connection has [PRAGMA synchronous] set to OFF.)^ +** Some specialized VFSes need this signal in order to operate correctly +** when [PRAGMA synchronous | PRAGMA synchronous=OFF] is set, but most +** VFSes do not need this signal and should silently ignore this opcode. +** Applications should not call [sqlite3_file_control()] with this +** opcode as doing so may disrupt the operation of the specilized VFSes +** that do require it. +*/ +#define SQLITE_FCNTL_LOCKSTATE 1 +#define SQLITE_GET_LOCKPROXYFILE 2 +#define SQLITE_SET_LOCKPROXYFILE 3 +#define SQLITE_LAST_ERRNO 4 +#define SQLITE_FCNTL_SIZE_HINT 5 +#define SQLITE_FCNTL_CHUNK_SIZE 6 +#define SQLITE_FCNTL_FILE_POINTER 7 +#define SQLITE_FCNTL_SYNC_OMITTED 8 + + +/* +** CAPI3REF: Mutex Handle +** +** The mutex module within SQLite defines [sqlite3_mutex] to be an +** abstract type for a mutex object. The SQLite core never looks +** at the internal representation of an [sqlite3_mutex]. It only +** deals with pointers to the [sqlite3_mutex] object. +** +** Mutexes are created using [sqlite3_mutex_alloc()]. +*/ +typedef struct sqlite3_mutex sqlite3_mutex; + +/* +** CAPI3REF: OS Interface Object +** +** An instance of the sqlite3_vfs object defines the interface between +** the SQLite core and the underlying operating system. The "vfs" +** in the name of the object stands for "virtual file system". +** +** The value of the iVersion field is initially 1 but may be larger in +** future versions of SQLite. Additional fields may be appended to this +** object when the iVersion value is increased. Note that the structure +** of the sqlite3_vfs object changes in the transaction between +** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not +** modified. +** +** The szOsFile field is the size of the subclassed [sqlite3_file] +** structure used by this VFS. mxPathname is the maximum length of +** a pathname in this VFS. +** +** Registered sqlite3_vfs objects are kept on a linked list formed by +** the pNext pointer. The [sqlite3_vfs_register()] +** and [sqlite3_vfs_unregister()] interfaces manage this list +** in a thread-safe way. The [sqlite3_vfs_find()] interface +** searches the list. Neither the application code nor the VFS +** implementation should use the pNext pointer. +** +** The pNext field is the only field in the sqlite3_vfs +** structure that SQLite will ever modify. SQLite will only access +** or modify this field while holding a particular static mutex. +** The application should never modify anything within the sqlite3_vfs +** object once the object has been registered. +** +** The zName field holds the name of the VFS module. The name must +** be unique across all VFS modules. +** +** ^SQLite guarantees that the zFilename parameter to xOpen +** is either a NULL pointer or string obtained +** from xFullPathname() with an optional suffix added. +** ^If a suffix is added to the zFilename parameter, it will +** consist of a single "-" character followed by no more than +** 10 alphanumeric and/or "-" characters. +** ^SQLite further guarantees that +** the string will be valid and unchanged until xClose() is +** called. Because of the previous sentence, +** the [sqlite3_file] can safely store a pointer to the +** filename if it needs to remember the filename for some reason. +** If the zFilename parameter to xOpen is a NULL pointer then xOpen +** must invent its own temporary name for the file. ^Whenever the +** xFilename parameter is NULL it will also be the case that the +** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE]. +** +** The flags argument to xOpen() includes all bits set in +** the flags argument to [sqlite3_open_v2()]. Or if [sqlite3_open()] +** or [sqlite3_open16()] is used, then flags includes at least +** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]. +** If xOpen() opens a file read-only then it sets *pOutFlags to +** include [SQLITE_OPEN_READONLY]. Other bits in *pOutFlags may be set. +** +** ^(SQLite will also add one of the following flags to the xOpen() +** call, depending on the object being opened: +** +**
      +**
    • [SQLITE_OPEN_MAIN_DB] +**
    • [SQLITE_OPEN_MAIN_JOURNAL] +**
    • [SQLITE_OPEN_TEMP_DB] +**
    • [SQLITE_OPEN_TEMP_JOURNAL] +**
    • [SQLITE_OPEN_TRANSIENT_DB] +**
    • [SQLITE_OPEN_SUBJOURNAL] +**
    • [SQLITE_OPEN_MASTER_JOURNAL] +**
    • [SQLITE_OPEN_WAL] +**
    )^ +** +** The file I/O implementation can use the object type flags to +** change the way it deals with files. For example, an application +** that does not care about crash recovery or rollback might make +** the open of a journal file a no-op. Writes to this journal would +** also be no-ops, and any attempt to read the journal would return +** SQLITE_IOERR. Or the implementation might recognize that a database +** file will be doing page-aligned sector reads and writes in a random +** order and set up its I/O subsystem accordingly. +** +** SQLite might also add one of the following flags to the xOpen method: +** +**
      +**
    • [SQLITE_OPEN_DELETEONCLOSE] +**
    • [SQLITE_OPEN_EXCLUSIVE] +**
    +** +** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be +** deleted when it is closed. ^The [SQLITE_OPEN_DELETEONCLOSE] +** will be set for TEMP databases and their journals, transient +** databases, and subjournals. +** +** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction +** with the [SQLITE_OPEN_CREATE] flag, which are both directly +** analogous to the O_EXCL and O_CREAT flags of the POSIX open() +** API. The SQLITE_OPEN_EXCLUSIVE flag, when paired with the +** SQLITE_OPEN_CREATE, is used to indicate that file should always +** be created, and that it is an error if it already exists. +** It is not used to indicate the file should be opened +** for exclusive access. +** +** ^At least szOsFile bytes of memory are allocated by SQLite +** to hold the [sqlite3_file] structure passed as the third +** argument to xOpen. The xOpen method does not have to +** allocate the structure; it should just fill it in. Note that +** the xOpen method must set the sqlite3_file.pMethods to either +** a valid [sqlite3_io_methods] object or to NULL. xOpen must do +** this even if the open fails. SQLite expects that the sqlite3_file.pMethods +** element will be valid after xOpen returns regardless of the success +** or failure of the xOpen call. +** +** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS] +** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to +** test whether a file is readable and writable, or [SQLITE_ACCESS_READ] +** to test whether a file is at least readable. The file can be a +** directory. +** +** ^SQLite will always allocate at least mxPathname+1 bytes for the +** output buffer xFullPathname. The exact size of the output buffer +** is also passed as a parameter to both methods. If the output buffer +** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is +** handled as a fatal error by SQLite, vfs implementations should endeavor +** to prevent this by setting mxPathname to a sufficiently large value. +** +** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64() +** interfaces are not strictly a part of the filesystem, but they are +** included in the VFS structure for completeness. +** The xRandomness() function attempts to return nBytes bytes +** of good-quality randomness into zOut. The return value is +** the actual number of bytes of randomness obtained. +** The xSleep() method causes the calling thread to sleep for at +** least the number of microseconds given. ^The xCurrentTime() +** method returns a Julian Day Number for the current date and time as +** a floating point value. +** ^The xCurrentTimeInt64() method returns, as an integer, the Julian +** Day Number multipled by 86400000 (the number of milliseconds in +** a 24-hour day). +** ^SQLite will use the xCurrentTimeInt64() method to get the current +** date and time if that method is available (if iVersion is 2 or +** greater and the function pointer is not NULL) and will fall back +** to xCurrentTime() if xCurrentTimeInt64() is unavailable. +** +** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces +** are not used by the SQLite core. These optional interfaces are provided +** by some VFSes to facilitate testing of the VFS code. By overriding +** system calls with functions under its control, a test program can +** simulate faults and error conditions that would otherwise be difficult +** or impossible to induce. The set of system calls that can be overridden +** varies from one VFS to another, and from one version of the same VFS to the +** next. Applications that use these interfaces must be prepared for any +** or all of these interfaces to be NULL or for their behavior to change +** from one release to the next. Applications must not attempt to access +** any of these methods if the iVersion of the VFS is less than 3. +*/ +typedef struct sqlite3_vfs sqlite3_vfs; +struct sqlite3_vfs { + int iVersion; /* Structure version number (currently 3) */ + int szOsFile; /* Size of subclassed sqlite3_file */ + int mxPathname; /* Maximum file pathname length */ + sqlite3_vfs *pNext; /* Next registered VFS */ + const char *zName; /* Name of this virtual file system */ + void *pAppData; /* Pointer to application-specific data */ + int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, + int flags, int *pOutFlags); + int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); + int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut); + int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); + void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); + void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); + void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); + void (*xDlClose)(sqlite3_vfs*, void*); + int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); + int (*xSleep)(sqlite3_vfs*, int microseconds); + int (*xCurrentTime)(sqlite3_vfs*, double*); + int (*xGetLastError)(sqlite3_vfs*, int, char *); + /* + ** The methods above are in version 1 of the sqlite_vfs object + ** definition. Those that follow are added in version 2 or later + */ + int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*); + /* + ** The methods above are in versions 1 and 2 of the sqlite_vfs object. + ** Those below are for version 3 and greater. + */ + int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, void *pFunc); + void *(*xGetSystemCall)(sqlite3_vfs*, const char *zName); + const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName); + /* + ** The methods above are in versions 1 through 3 of the sqlite_vfs object. + ** New fields may be appended in figure versions. The iVersion + ** value will increment whenever this happens. + */ +}; + +/* +** CAPI3REF: Flags for the xAccess VFS method +** +** These integer constants can be used as the third parameter to +** the xAccess method of an [sqlite3_vfs] object. They determine +** what kind of permissions the xAccess method is looking for. +** With SQLITE_ACCESS_EXISTS, the xAccess method +** simply checks whether the file exists. +** With SQLITE_ACCESS_READWRITE, the xAccess method +** checks whether the named directory is both readable and writable +** (in other words, if files can be added, removed, and renamed within +** the directory). +** The SQLITE_ACCESS_READWRITE constant is currently used only by the +** [temp_store_directory pragma], though this could change in a future +** release of SQLite. +** With SQLITE_ACCESS_READ, the xAccess method +** checks whether the file is readable. The SQLITE_ACCESS_READ constant is +** currently unused, though it might be used in a future release of +** SQLite. +*/ +#define SQLITE_ACCESS_EXISTS 0 +#define SQLITE_ACCESS_READWRITE 1 /* Used by PRAGMA temp_store_directory */ +#define SQLITE_ACCESS_READ 2 /* Unused */ + +/* +** CAPI3REF: Flags for the xShmLock VFS method +** +** These integer constants define the various locking operations +** allowed by the xShmLock method of [sqlite3_io_methods]. The +** following are the only legal combinations of flags to the +** xShmLock method: +** +**
      +**
    • SQLITE_SHM_LOCK | SQLITE_SHM_SHARED +**
    • SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE +**
    • SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED +**
    • SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE +**
    +** +** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as +** was given no the corresponding lock. +** +** The xShmLock method can transition between unlocked and SHARED or +** between unlocked and EXCLUSIVE. It cannot transition between SHARED +** and EXCLUSIVE. +*/ +#define SQLITE_SHM_UNLOCK 1 +#define SQLITE_SHM_LOCK 2 +#define SQLITE_SHM_SHARED 4 +#define SQLITE_SHM_EXCLUSIVE 8 + +/* +** CAPI3REF: Maximum xShmLock index +** +** The xShmLock method on [sqlite3_io_methods] may use values +** between 0 and this upper bound as its "offset" argument. +** The SQLite core will never attempt to acquire or release a +** lock outside of this range +*/ +#define SQLITE_SHM_NLOCK 8 + + +/* +** CAPI3REF: Initialize The SQLite Library +** +** ^The sqlite3_initialize() routine initializes the +** SQLite library. ^The sqlite3_shutdown() routine +** deallocates any resources that were allocated by sqlite3_initialize(). +** These routines are designed to aid in process initialization and +** shutdown on embedded systems. Workstation applications using +** SQLite normally do not need to invoke either of these routines. +** +** A call to sqlite3_initialize() is an "effective" call if it is +** the first time sqlite3_initialize() is invoked during the lifetime of +** the process, or if it is the first time sqlite3_initialize() is invoked +** following a call to sqlite3_shutdown(). ^(Only an effective call +** of sqlite3_initialize() does any initialization. All other calls +** are harmless no-ops.)^ +** +** A call to sqlite3_shutdown() is an "effective" call if it is the first +** call to sqlite3_shutdown() since the last sqlite3_initialize(). ^(Only +** an effective call to sqlite3_shutdown() does any deinitialization. +** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^ +** +** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown() +** is not. The sqlite3_shutdown() interface must only be called from a +** single thread. All open [database connections] must be closed and all +** other SQLite resources must be deallocated prior to invoking +** sqlite3_shutdown(). +** +** Among other things, ^sqlite3_initialize() will invoke +** sqlite3_os_init(). Similarly, ^sqlite3_shutdown() +** will invoke sqlite3_os_end(). +** +** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success. +** ^If for some reason, sqlite3_initialize() is unable to initialize +** the library (perhaps it is unable to allocate a needed resource such +** as a mutex) it returns an [error code] other than [SQLITE_OK]. +** +** ^The sqlite3_initialize() routine is called internally by many other +** SQLite interfaces so that an application usually does not need to +** invoke sqlite3_initialize() directly. For example, [sqlite3_open()] +** calls sqlite3_initialize() so the SQLite library will be automatically +** initialized when [sqlite3_open()] is called if it has not be initialized +** already. ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT] +** compile-time option, then the automatic calls to sqlite3_initialize() +** are omitted and the application must call sqlite3_initialize() directly +** prior to using any other SQLite interface. For maximum portability, +** it is recommended that applications always invoke sqlite3_initialize() +** directly prior to using any other SQLite interface. Future releases +** of SQLite may require this. In other words, the behavior exhibited +** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the +** default behavior in some future release of SQLite. +** +** The sqlite3_os_init() routine does operating-system specific +** initialization of the SQLite library. The sqlite3_os_end() +** routine undoes the effect of sqlite3_os_init(). Typical tasks +** performed by these routines include allocation or deallocation +** of static resources, initialization of global variables, +** setting up a default [sqlite3_vfs] module, or setting up +** a default configuration using [sqlite3_config()]. +** +** The application should never invoke either sqlite3_os_init() +** or sqlite3_os_end() directly. The application should only invoke +** sqlite3_initialize() and sqlite3_shutdown(). The sqlite3_os_init() +** interface is called automatically by sqlite3_initialize() and +** sqlite3_os_end() is called by sqlite3_shutdown(). Appropriate +** implementations for sqlite3_os_init() and sqlite3_os_end() +** are built into SQLite when it is compiled for Unix, Windows, or OS/2. +** When [custom builds | built for other platforms] +** (using the [SQLITE_OS_OTHER=1] compile-time +** option) the application must supply a suitable implementation for +** sqlite3_os_init() and sqlite3_os_end(). An application-supplied +** implementation of sqlite3_os_init() or sqlite3_os_end() +** must return [SQLITE_OK] on success and some other [error code] upon +** failure. +*/ +SQLITE_API int sqlite3_initialize(void); +SQLITE_API int sqlite3_shutdown(void); +SQLITE_API int sqlite3_os_init(void); +SQLITE_API int sqlite3_os_end(void); + +/* +** CAPI3REF: Configuring The SQLite Library +** +** The sqlite3_config() interface is used to make global configuration +** changes to SQLite in order to tune SQLite to the specific needs of +** the application. The default configuration is recommended for most +** applications and so this routine is usually not necessary. It is +** provided to support rare applications with unusual needs. +** +** The sqlite3_config() interface is not threadsafe. The application +** must insure that no other SQLite interfaces are invoked by other +** threads while sqlite3_config() is running. Furthermore, sqlite3_config() +** may only be invoked prior to library initialization using +** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()]. +** ^If sqlite3_config() is called after [sqlite3_initialize()] and before +** [sqlite3_shutdown()] then it will return SQLITE_MISUSE. +** Note, however, that ^sqlite3_config() can be called as part of the +** implementation of an application-defined [sqlite3_os_init()]. +** +** The first argument to sqlite3_config() is an integer +** [SQLITE_CONFIG_SINGLETHREAD | configuration option] that determines +** what property of SQLite is to be configured. Subsequent arguments +** vary depending on the [SQLITE_CONFIG_SINGLETHREAD | configuration option] +** in the first argument. +** +** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK]. +** ^If the option is unknown or SQLite is unable to set the option +** then this routine returns a non-zero [error code]. +*/ +SQLITE_API int sqlite3_config(int, ...); + +/* +** CAPI3REF: Configure database connections +** +** The sqlite3_db_config() interface is used to make configuration +** changes to a [database connection]. The interface is similar to +** [sqlite3_config()] except that the changes apply to a single +** [database connection] (specified in the first argument). The +** sqlite3_db_config() interface should only be used immediately after +** the database connection is created using [sqlite3_open()], +** [sqlite3_open16()], or [sqlite3_open_v2()]. +** +** The second argument to sqlite3_db_config(D,V,...) is the +** configuration verb - an integer code that indicates what +** aspect of the [database connection] is being configured. +** The only choice for this value is [SQLITE_DBCONFIG_LOOKASIDE]. +** New verbs are likely to be added in future releases of SQLite. +** Additional arguments depend on the verb. +** +** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if +** the call is considered successful. +*/ +SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...); + +/* +** CAPI3REF: Memory Allocation Routines +** +** An instance of this object defines the interface between SQLite +** and low-level memory allocation routines. +** +** This object is used in only one place in the SQLite interface. +** A pointer to an instance of this object is the argument to +** [sqlite3_config()] when the configuration option is +** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC]. +** By creating an instance of this object +** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC]) +** during configuration, an application can specify an alternative +** memory allocation subsystem for SQLite to use for all of its +** dynamic memory needs. +** +** Note that SQLite comes with several [built-in memory allocators] +** that are perfectly adequate for the overwhelming majority of applications +** and that this object is only useful to a tiny minority of applications +** with specialized memory allocation requirements. This object is +** also used during testing of SQLite in order to specify an alternative +** memory allocator that simulates memory out-of-memory conditions in +** order to verify that SQLite recovers gracefully from such +** conditions. +** +** The xMalloc and xFree methods must work like the +** malloc() and free() functions from the standard C library. +** The xRealloc method must work like realloc() from the standard C library +** with the exception that if the second argument to xRealloc is zero, +** xRealloc must be a no-op - it must not perform any allocation or +** deallocation. ^SQLite guarantees that the second argument to +** xRealloc is always a value returned by a prior call to xRoundup. +** And so in cases where xRoundup always returns a positive number, +** xRealloc can perform exactly as the standard library realloc() and +** still be in compliance with this specification. +** +** xSize should return the allocated size of a memory allocation +** previously obtained from xMalloc or xRealloc. The allocated size +** is always at least as big as the requested size but may be larger. +** +** The xRoundup method returns what would be the allocated size of +** a memory allocation given a particular requested size. Most memory +** allocators round up memory allocations at least to the next multiple +** of 8. Some allocators round up to a larger multiple or to a power of 2. +** Every memory allocation request coming in through [sqlite3_malloc()] +** or [sqlite3_realloc()] first calls xRoundup. If xRoundup returns 0, +** that causes the corresponding memory allocation to fail. +** +** The xInit method initializes the memory allocator. (For example, +** it might allocate any require mutexes or initialize internal data +** structures. The xShutdown method is invoked (indirectly) by +** [sqlite3_shutdown()] and should deallocate any resources acquired +** by xInit. The pAppData pointer is used as the only parameter to +** xInit and xShutdown. +** +** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes +** the xInit method, so the xInit method need not be threadsafe. The +** xShutdown method is only called from [sqlite3_shutdown()] so it does +** not need to be threadsafe either. For all other methods, SQLite +** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the +** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which +** it is by default) and so the methods are automatically serialized. +** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other +** methods must be threadsafe or else make their own arrangements for +** serialization. +** +** SQLite will never invoke xInit() more than once without an intervening +** call to xShutdown(). +*/ +typedef struct sqlite3_mem_methods sqlite3_mem_methods; +struct sqlite3_mem_methods { + void *(*xMalloc)(int); /* Memory allocation function */ + void (*xFree)(void*); /* Free a prior allocation */ + void *(*xRealloc)(void*,int); /* Resize an allocation */ + int (*xSize)(void*); /* Return the size of an allocation */ + int (*xRoundup)(int); /* Round up request size to allocation size */ + int (*xInit)(void*); /* Initialize the memory allocator */ + void (*xShutdown)(void*); /* Deinitialize the memory allocator */ + void *pAppData; /* Argument to xInit() and xShutdown() */ +}; + +/* +** CAPI3REF: Configuration Options +** +** These constants are the available integer configuration options that +** can be passed as the first argument to the [sqlite3_config()] interface. +** +** New configuration options may be added in future releases of SQLite. +** Existing configuration options might be discontinued. Applications +** should check the return code from [sqlite3_config()] to make sure that +** the call worked. The [sqlite3_config()] interface will return a +** non-zero [error code] if a discontinued or unsupported configuration option +** is invoked. +** +**
    +**
    SQLITE_CONFIG_SINGLETHREAD
    +**
    There are no arguments to this option. ^This option sets the +** [threading mode] to Single-thread. In other words, it disables +** all mutexing and puts SQLite into a mode where it can only be used +** by a single thread. ^If SQLite is compiled with +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then +** it is not possible to change the [threading mode] from its default +** value of Single-thread and so [sqlite3_config()] will return +** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD +** configuration option.
    +** +**
    SQLITE_CONFIG_MULTITHREAD
    +**
    There are no arguments to this option. ^This option sets the +** [threading mode] to Multi-thread. In other words, it disables +** mutexing on [database connection] and [prepared statement] objects. +** The application is responsible for serializing access to +** [database connections] and [prepared statements]. But other mutexes +** are enabled so that SQLite will be safe to use in a multi-threaded +** environment as long as no two threads attempt to use the same +** [database connection] at the same time. ^If SQLite is compiled with +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then +** it is not possible to set the Multi-thread [threading mode] and +** [sqlite3_config()] will return [SQLITE_ERROR] if called with the +** SQLITE_CONFIG_MULTITHREAD configuration option.
    +** +**
    SQLITE_CONFIG_SERIALIZED
    +**
    There are no arguments to this option. ^This option sets the +** [threading mode] to Serialized. In other words, this option enables +** all mutexes including the recursive +** mutexes on [database connection] and [prepared statement] objects. +** In this mode (which is the default when SQLite is compiled with +** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access +** to [database connections] and [prepared statements] so that the +** application is free to use the same [database connection] or the +** same [prepared statement] in different threads at the same time. +** ^If SQLite is compiled with +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then +** it is not possible to set the Serialized [threading mode] and +** [sqlite3_config()] will return [SQLITE_ERROR] if called with the +** SQLITE_CONFIG_SERIALIZED configuration option.
    +** +**
    SQLITE_CONFIG_MALLOC
    +**
    ^(This option takes a single argument which is a pointer to an +** instance of the [sqlite3_mem_methods] structure. The argument specifies +** alternative low-level memory allocation routines to be used in place of +** the memory allocation routines built into SQLite.)^ ^SQLite makes +** its own private copy of the content of the [sqlite3_mem_methods] structure +** before the [sqlite3_config()] call returns.
    +** +**
    SQLITE_CONFIG_GETMALLOC
    +**
    ^(This option takes a single argument which is a pointer to an +** instance of the [sqlite3_mem_methods] structure. The [sqlite3_mem_methods] +** structure is filled with the currently defined memory allocation routines.)^ +** This option can be used to overload the default memory allocation +** routines with a wrapper that simulations memory allocation failure or +** tracks memory usage, for example.
    +** +**
    SQLITE_CONFIG_MEMSTATUS
    +**
    ^This option takes single argument of type int, interpreted as a +** boolean, which enables or disables the collection of memory allocation +** statistics. ^(When memory allocation statistics are disabled, the +** following SQLite interfaces become non-operational: +**
      +**
    • [sqlite3_memory_used()] +**
    • [sqlite3_memory_highwater()] +**
    • [sqlite3_soft_heap_limit64()] +**
    • [sqlite3_status()] +**
    )^ +** ^Memory allocation statistics are enabled by default unless SQLite is +** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory +** allocation statistics are disabled by default. +**
    +** +**
    SQLITE_CONFIG_SCRATCH
    +**
    ^This option specifies a static memory buffer that SQLite can use for +** scratch memory. There are three arguments: A pointer an 8-byte +** aligned memory buffer from which the scrach allocations will be +** drawn, the size of each scratch allocation (sz), +** and the maximum number of scratch allocations (N). The sz +** argument must be a multiple of 16. +** The first argument must be a pointer to an 8-byte aligned buffer +** of at least sz*N bytes of memory. +** ^SQLite will use no more than two scratch buffers per thread. So +** N should be set to twice the expected maximum number of threads. +** ^SQLite will never require a scratch buffer that is more than 6 +** times the database page size. ^If SQLite needs needs additional +** scratch memory beyond what is provided by this configuration option, then +** [sqlite3_malloc()] will be used to obtain the memory needed.
    +** +**
    SQLITE_CONFIG_PAGECACHE
    +**
    ^This option specifies a static memory buffer that SQLite can use for +** the database page cache with the default page cache implemenation. +** This configuration should not be used if an application-define page +** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option. +** There are three arguments to this option: A pointer to 8-byte aligned +** memory, the size of each page buffer (sz), and the number of pages (N). +** The sz argument should be the size of the largest database page +** (a power of two between 512 and 32768) plus a little extra for each +** page header. ^The page header size is 20 to 40 bytes depending on +** the host architecture. ^It is harmless, apart from the wasted memory, +** to make sz a little too large. The first +** argument should point to an allocation of at least sz*N bytes of memory. +** ^SQLite will use the memory provided by the first argument to satisfy its +** memory needs for the first N pages that it adds to cache. ^If additional +** page cache memory is needed beyond what is provided by this option, then +** SQLite goes to [sqlite3_malloc()] for the additional storage space. +** The pointer in the first argument must +** be aligned to an 8-byte boundary or subsequent behavior of SQLite +** will be undefined.
    +** +**
    SQLITE_CONFIG_HEAP
    +**
    ^This option specifies a static memory buffer that SQLite will use +** for all of its dynamic memory allocation needs beyond those provided +** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE]. +** There are three arguments: An 8-byte aligned pointer to the memory, +** the number of bytes in the memory buffer, and the minimum allocation size. +** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts +** to using its default memory allocator (the system malloc() implementation), +** undoing any prior invocation of [SQLITE_CONFIG_MALLOC]. ^If the +** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or +** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory +** allocator is engaged to handle all of SQLites memory allocation needs. +** The first pointer (the memory pointer) must be aligned to an 8-byte +** boundary or subsequent behavior of SQLite will be undefined. +** The minimum allocation size is capped at 2^12. Reasonable values +** for the minimum allocation size are 2^5 through 2^8.
    +** +**
    SQLITE_CONFIG_MUTEX
    +**
    ^(This option takes a single argument which is a pointer to an +** instance of the [sqlite3_mutex_methods] structure. The argument specifies +** alternative low-level mutex routines to be used in place +** the mutex routines built into SQLite.)^ ^SQLite makes a copy of the +** content of the [sqlite3_mutex_methods] structure before the call to +** [sqlite3_config()] returns. ^If SQLite is compiled with +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then +** the entire mutexing subsystem is omitted from the build and hence calls to +** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will +** return [SQLITE_ERROR].
    +** +**
    SQLITE_CONFIG_GETMUTEX
    +**
    ^(This option takes a single argument which is a pointer to an +** instance of the [sqlite3_mutex_methods] structure. The +** [sqlite3_mutex_methods] +** structure is filled with the currently defined mutex routines.)^ +** This option can be used to overload the default mutex allocation +** routines with a wrapper used to track mutex usage for performance +** profiling or testing, for example. ^If SQLite is compiled with +** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then +** the entire mutexing subsystem is omitted from the build and hence calls to +** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will +** return [SQLITE_ERROR].
    +** +**
    SQLITE_CONFIG_LOOKASIDE
    +**
    ^(This option takes two arguments that determine the default +** memory allocation for the lookaside memory allocator on each +** [database connection]. The first argument is the +** size of each lookaside buffer slot and the second is the number of +** slots allocated to each database connection.)^ ^(This option sets the +** default lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE] +** verb to [sqlite3_db_config()] can be used to change the lookaside +** configuration on individual connections.)^
    +** +**
    SQLITE_CONFIG_PCACHE
    +**
    ^(This option takes a single argument which is a pointer to +** an [sqlite3_pcache_methods] object. This object specifies the interface +** to a custom page cache implementation.)^ ^SQLite makes a copy of the +** object and uses it for page cache memory allocations.
    +** +**
    SQLITE_CONFIG_GETPCACHE
    +**
    ^(This option takes a single argument which is a pointer to an +** [sqlite3_pcache_methods] object. SQLite copies of the current +** page cache implementation into that object.)^
    +** +**
    SQLITE_CONFIG_LOG
    +**
    ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a +** function with a call signature of void(*)(void*,int,const char*), +** and a pointer to void. ^If the function pointer is not NULL, it is +** invoked by [sqlite3_log()] to process each logging event. ^If the +** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op. +** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is +** passed through as the first parameter to the application-defined logger +** function whenever that function is invoked. ^The second parameter to +** the logger function is a copy of the first parameter to the corresponding +** [sqlite3_log()] call and is intended to be a [result code] or an +** [extended result code]. ^The third parameter passed to the logger is +** log message after formatting via [sqlite3_snprintf()]. +** The SQLite logging interface is not reentrant; the logger function +** supplied by the application must not invoke any SQLite interface. +** In a multi-threaded application, the application-defined logger +** function must be threadsafe.
    +** +**
    +*/ +#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */ +#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */ +#define SQLITE_CONFIG_SERIALIZED 3 /* nil */ +#define SQLITE_CONFIG_MALLOC 4 /* sqlite3_mem_methods* */ +#define SQLITE_CONFIG_GETMALLOC 5 /* sqlite3_mem_methods* */ +#define SQLITE_CONFIG_SCRATCH 6 /* void*, int sz, int N */ +#define SQLITE_CONFIG_PAGECACHE 7 /* void*, int sz, int N */ +#define SQLITE_CONFIG_HEAP 8 /* void*, int nByte, int min */ +#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */ +#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */ +#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */ +/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */ +#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */ +#define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */ +#define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */ +#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */ + +/* +** CAPI3REF: Database Connection Configuration Options +** +** These constants are the available integer configuration options that +** can be passed as the second argument to the [sqlite3_db_config()] interface. +** +** New configuration options may be added in future releases of SQLite. +** Existing configuration options might be discontinued. Applications +** should check the return code from [sqlite3_db_config()] to make sure that +** the call worked. ^The [sqlite3_db_config()] interface will return a +** non-zero [error code] if a discontinued or unsupported configuration option +** is invoked. +** +**
    +**
    SQLITE_DBCONFIG_LOOKASIDE
    +**
    ^This option takes three additional arguments that determine the +** [lookaside memory allocator] configuration for the [database connection]. +** ^The first argument (the third parameter to [sqlite3_db_config()] is a +** pointer to an memory buffer to use for lookaside memory. +** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb +** may be NULL in which case SQLite will allocate the +** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the +** size of each lookaside buffer slot. ^The third argument is the number of +** slots. The size of the buffer in the first argument must be greater than +** or equal to the product of the second and third arguments. The buffer +** must be aligned to an 8-byte boundary. ^If the second argument to +** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally +** rounded down to the next smaller multiple of 8. ^(The lookaside memory +** configuration for a database connection can only be changed when that +** connection is not currently using lookaside memory, or in other words +** when the "current value" returned by +** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero. +** Any attempt to change the lookaside memory configuration when lookaside +** memory is in use leaves the configuration unchanged and returns +** [SQLITE_BUSY].)^
    +** +**
    +*/ +#define SQLITE_DBCONFIG_LOOKASIDE 1001 /* void* int int */ + + +/* +** CAPI3REF: Enable Or Disable Extended Result Codes +** +** ^The sqlite3_extended_result_codes() routine enables or disables the +** [extended result codes] feature of SQLite. ^The extended result +** codes are disabled by default for historical compatibility. +*/ +SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff); + +/* +** CAPI3REF: Last Insert Rowid +** +** ^Each entry in an SQLite table has a unique 64-bit signed +** integer key called the [ROWID | "rowid"]. ^The rowid is always available +** as an undeclared column named ROWID, OID, or _ROWID_ as long as those +** names are not also used by explicitly declared columns. ^If +** the table has a column of type [INTEGER PRIMARY KEY] then that column +** is another alias for the rowid. +** +** ^This routine returns the [rowid] of the most recent +** successful [INSERT] into the database from the [database connection] +** in the first argument. ^If no successful [INSERT]s +** have ever occurred on that database connection, zero is returned. +** +** ^(If an [INSERT] occurs within a trigger, then the [rowid] of the inserted +** row is returned by this routine as long as the trigger is running. +** But once the trigger terminates, the value returned by this routine +** reverts to the last value inserted before the trigger fired.)^ +** +** ^An [INSERT] that fails due to a constraint violation is not a +** successful [INSERT] and does not change the value returned by this +** routine. ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, +** and INSERT OR ABORT make no changes to the return value of this +** routine when their insertion fails. ^(When INSERT OR REPLACE +** encounters a constraint violation, it does not fail. The +** INSERT continues to completion after deleting rows that caused +** the constraint problem so INSERT OR REPLACE will always change +** the return value of this interface.)^ +** +** ^For the purposes of this routine, an [INSERT] is considered to +** be successful even if it is subsequently rolled back. +** +** This function is accessible to SQL statements via the +** [last_insert_rowid() SQL function]. +** +** If a separate thread performs a new [INSERT] on the same +** database connection while the [sqlite3_last_insert_rowid()] +** function is running and thus changes the last insert [rowid], +** then the value returned by [sqlite3_last_insert_rowid()] is +** unpredictable and might not equal either the old or the new +** last insert [rowid]. +*/ +SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); + +/* +** CAPI3REF: Count The Number Of Rows Modified +** +** ^This function returns the number of database rows that were changed +** or inserted or deleted by the most recently completed SQL statement +** on the [database connection] specified by the first parameter. +** ^(Only changes that are directly specified by the [INSERT], [UPDATE], +** or [DELETE] statement are counted. Auxiliary changes caused by +** triggers or [foreign key actions] are not counted.)^ Use the +** [sqlite3_total_changes()] function to find the total number of changes +** including changes caused by triggers and foreign key actions. +** +** ^Changes to a view that are simulated by an [INSTEAD OF trigger] +** are not counted. Only real table changes are counted. +** +** ^(A "row change" is a change to a single row of a single table +** caused by an INSERT, DELETE, or UPDATE statement. Rows that +** are changed as side effects of [REPLACE] constraint resolution, +** rollback, ABORT processing, [DROP TABLE], or by any other +** mechanisms do not count as direct row changes.)^ +** +** A "trigger context" is a scope of execution that begins and +** ends with the script of a [CREATE TRIGGER | trigger]. +** Most SQL statements are +** evaluated outside of any trigger. This is the "top level" +** trigger context. If a trigger fires from the top level, a +** new trigger context is entered for the duration of that one +** trigger. Subtriggers create subcontexts for their duration. +** +** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does +** not create a new trigger context. +** +** ^This function returns the number of direct row changes in the +** most recent INSERT, UPDATE, or DELETE statement within the same +** trigger context. +** +** ^Thus, when called from the top level, this function returns the +** number of changes in the most recent INSERT, UPDATE, or DELETE +** that also occurred at the top level. ^(Within the body of a trigger, +** the sqlite3_changes() interface can be called to find the number of +** changes in the most recently completed INSERT, UPDATE, or DELETE +** statement within the body of the same trigger. +** However, the number returned does not include changes +** caused by subtriggers since those have their own context.)^ +** +** See also the [sqlite3_total_changes()] interface, the +** [count_changes pragma], and the [changes() SQL function]. +** +** If a separate thread makes changes on the same database connection +** while [sqlite3_changes()] is running then the value returned +** is unpredictable and not meaningful. +*/ +SQLITE_API int sqlite3_changes(sqlite3*); + +/* +** CAPI3REF: Total Number Of Rows Modified +** +** ^This function returns the number of row changes caused by [INSERT], +** [UPDATE] or [DELETE] statements since the [database connection] was opened. +** ^(The count returned by sqlite3_total_changes() includes all changes +** from all [CREATE TRIGGER | trigger] contexts and changes made by +** [foreign key actions]. However, +** the count does not include changes used to implement [REPLACE] constraints, +** do rollbacks or ABORT processing, or [DROP TABLE] processing. The +** count does not include rows of views that fire an [INSTEAD OF trigger], +** though if the INSTEAD OF trigger makes changes of its own, those changes +** are counted.)^ +** ^The sqlite3_total_changes() function counts the changes as soon as +** the statement that makes them is completed (when the statement handle +** is passed to [sqlite3_reset()] or [sqlite3_finalize()]). +** +** See also the [sqlite3_changes()] interface, the +** [count_changes pragma], and the [total_changes() SQL function]. +** +** If a separate thread makes changes on the same database connection +** while [sqlite3_total_changes()] is running then the value +** returned is unpredictable and not meaningful. +*/ +SQLITE_API int sqlite3_total_changes(sqlite3*); + +/* +** CAPI3REF: Interrupt A Long-Running Query +** +** ^This function causes any pending database operation to abort and +** return at its earliest opportunity. This routine is typically +** called in response to a user action such as pressing "Cancel" +** or Ctrl-C where the user wants a long query operation to halt +** immediately. +** +** ^It is safe to call this routine from a thread different from the +** thread that is currently running the database operation. But it +** is not safe to call this routine with a [database connection] that +** is closed or might close before sqlite3_interrupt() returns. +** +** ^If an SQL operation is very nearly finished at the time when +** sqlite3_interrupt() is called, then it might not have an opportunity +** to be interrupted and might continue to completion. +** +** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT]. +** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE +** that is inside an explicit transaction, then the entire transaction +** will be rolled back automatically. +** +** ^The sqlite3_interrupt(D) call is in effect until all currently running +** SQL statements on [database connection] D complete. ^Any new SQL statements +** that are started after the sqlite3_interrupt() call and before the +** running statements reaches zero are interrupted as if they had been +** running prior to the sqlite3_interrupt() call. ^New SQL statements +** that are started after the running statement count reaches zero are +** not effected by the sqlite3_interrupt(). +** ^A call to sqlite3_interrupt(D) that occurs when there are no running +** SQL statements is a no-op and has no effect on SQL statements +** that are started after the sqlite3_interrupt() call returns. +** +** If the database connection closes while [sqlite3_interrupt()] +** is running then bad things will likely happen. +*/ +SQLITE_API void sqlite3_interrupt(sqlite3*); + +/* +** CAPI3REF: Determine If An SQL Statement Is Complete +** +** These routines are useful during command-line input to determine if the +** currently entered text seems to form a complete SQL statement or +** if additional input is needed before sending the text into +** SQLite for parsing. ^These routines return 1 if the input string +** appears to be a complete SQL statement. ^A statement is judged to be +** complete if it ends with a semicolon token and is not a prefix of a +** well-formed CREATE TRIGGER statement. ^Semicolons that are embedded within +** string literals or quoted identifier names or comments are not +** independent tokens (they are part of the token in which they are +** embedded) and thus do not count as a statement terminator. ^Whitespace +** and comments that follow the final semicolon are ignored. +** +** ^These routines return 0 if the statement is incomplete. ^If a +** memory allocation fails, then SQLITE_NOMEM is returned. +** +** ^These routines do not parse the SQL statements thus +** will not detect syntactically incorrect SQL. +** +** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior +** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked +** automatically by sqlite3_complete16(). If that initialization fails, +** then the return value from sqlite3_complete16() will be non-zero +** regardless of whether or not the input SQL is complete.)^ +** +** The input to [sqlite3_complete()] must be a zero-terminated +** UTF-8 string. +** +** The input to [sqlite3_complete16()] must be a zero-terminated +** UTF-16 string in native byte order. +*/ +SQLITE_API int sqlite3_complete(const char *sql); +SQLITE_API int sqlite3_complete16(const void *sql); + +/* +** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors +** +** ^This routine sets a callback function that might be invoked whenever +** an attempt is made to open a database table that another thread +** or process has locked. +** +** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] +** is returned immediately upon encountering the lock. ^If the busy callback +** is not NULL, then the callback might be invoked with two arguments. +** +** ^The first argument to the busy handler is a copy of the void* pointer which +** is the third argument to sqlite3_busy_handler(). ^The second argument to +** the busy handler callback is the number of times that the busy handler has +** been invoked for this locking event. ^If the +** busy callback returns 0, then no additional attempts are made to +** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned. +** ^If the callback returns non-zero, then another attempt +** is made to open the database for reading and the cycle repeats. +** +** The presence of a busy handler does not guarantee that it will be invoked +** when there is lock contention. ^If SQLite determines that invoking the busy +** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY] +** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler. +** Consider a scenario where one process is holding a read lock that +** it is trying to promote to a reserved lock and +** a second process is holding a reserved lock that it is trying +** to promote to an exclusive lock. The first process cannot proceed +** because it is blocked by the second and the second process cannot +** proceed because it is blocked by the first. If both processes +** invoke the busy handlers, neither will make any progress. Therefore, +** SQLite returns [SQLITE_BUSY] for the first process, hoping that this +** will induce the first process to release its read lock and allow +** the second process to proceed. +** +** ^The default busy callback is NULL. +** +** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED] +** when SQLite is in the middle of a large transaction where all the +** changes will not fit into the in-memory cache. SQLite will +** already hold a RESERVED lock on the database file, but it needs +** to promote this lock to EXCLUSIVE so that it can spill cache +** pages into the database file without harm to concurrent +** readers. ^If it is unable to promote the lock, then the in-memory +** cache will be left in an inconsistent state and so the error +** code is promoted from the relatively benign [SQLITE_BUSY] to +** the more severe [SQLITE_IOERR_BLOCKED]. ^This error code promotion +** forces an automatic rollback of the changes. See the +** +** CorruptionFollowingBusyError wiki page for a discussion of why +** this is important. +** +** ^(There can only be a single busy handler defined for each +** [database connection]. Setting a new busy handler clears any +** previously set handler.)^ ^Note that calling [sqlite3_busy_timeout()] +** will also set or clear the busy handler. +** +** The busy callback should not take any actions which modify the +** database connection that invoked the busy handler. Any such actions +** result in undefined behavior. +** +** A busy handler must not close the database connection +** or [prepared statement] that invoked the busy handler. +*/ +SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); + +/* +** CAPI3REF: Set A Busy Timeout +** +** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps +** for a specified amount of time when a table is locked. ^The handler +** will sleep multiple times until at least "ms" milliseconds of sleeping +** have accumulated. ^After at least "ms" milliseconds of sleeping, +** the handler returns 0 which causes [sqlite3_step()] to return +** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]. +** +** ^Calling this routine with an argument less than or equal to zero +** turns off all busy handlers. +** +** ^(There can only be a single busy handler for a particular +** [database connection] any any given moment. If another busy handler +** was defined (using [sqlite3_busy_handler()]) prior to calling +** this routine, that other busy handler is cleared.)^ +*/ +SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms); + +/* +** CAPI3REF: Convenience Routines For Running Queries +** +** This is a legacy interface that is preserved for backwards compatibility. +** Use of this interface is not recommended. +** +** Definition: A result table is memory data structure created by the +** [sqlite3_get_table()] interface. A result table records the +** complete query results from one or more queries. +** +** The table conceptually has a number of rows and columns. But +** these numbers are not part of the result table itself. These +** numbers are obtained separately. Let N be the number of rows +** and M be the number of columns. +** +** A result table is an array of pointers to zero-terminated UTF-8 strings. +** There are (N+1)*M elements in the array. The first M pointers point +** to zero-terminated strings that contain the names of the columns. +** The remaining entries all point to query results. NULL values result +** in NULL pointers. All other values are in their UTF-8 zero-terminated +** string representation as returned by [sqlite3_column_text()]. +** +** A result table might consist of one or more memory allocations. +** It is not safe to pass a result table directly to [sqlite3_free()]. +** A result table should be deallocated using [sqlite3_free_table()]. +** +** ^(As an example of the result table format, suppose a query result +** is as follows: +** +**
    +**        Name        | Age
    +**        -----------------------
    +**        Alice       | 43
    +**        Bob         | 28
    +**        Cindy       | 21
    +** 
    +** +** There are two column (M==2) and three rows (N==3). Thus the +** result table has 8 entries. Suppose the result table is stored +** in an array names azResult. Then azResult holds this content: +** +**
    +**        azResult[0] = "Name";
    +**        azResult[1] = "Age";
    +**        azResult[2] = "Alice";
    +**        azResult[3] = "43";
    +**        azResult[4] = "Bob";
    +**        azResult[5] = "28";
    +**        azResult[6] = "Cindy";
    +**        azResult[7] = "21";
    +** 
    )^ +** +** ^The sqlite3_get_table() function evaluates one or more +** semicolon-separated SQL statements in the zero-terminated UTF-8 +** string of its 2nd parameter and returns a result table to the +** pointer given in its 3rd parameter. +** +** After the application has finished with the result from sqlite3_get_table(), +** it must pass the result table pointer to sqlite3_free_table() in order to +** release the memory that was malloced. Because of the way the +** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling +** function must not try to call [sqlite3_free()] directly. Only +** [sqlite3_free_table()] is able to release the memory properly and safely. +** +** The sqlite3_get_table() interface is implemented as a wrapper around +** [sqlite3_exec()]. The sqlite3_get_table() routine does not have access +** to any internal data structures of SQLite. It uses only the public +** interface defined here. As a consequence, errors that occur in the +** wrapper layer outside of the internal [sqlite3_exec()] call are not +** reflected in subsequent calls to [sqlite3_errcode()] or +** [sqlite3_errmsg()]. +*/ +SQLITE_API int sqlite3_get_table( + sqlite3 *db, /* An open database */ + const char *zSql, /* SQL to be evaluated */ + char ***pazResult, /* Results of the query */ + int *pnRow, /* Number of result rows written here */ + int *pnColumn, /* Number of result columns written here */ + char **pzErrmsg /* Error msg written here */ +); +SQLITE_API void sqlite3_free_table(char **result); + +/* +** CAPI3REF: Formatted String Printing Functions +** +** These routines are work-alikes of the "printf()" family of functions +** from the standard C library. +** +** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their +** results into memory obtained from [sqlite3_malloc()]. +** The strings returned by these two routines should be +** released by [sqlite3_free()]. ^Both routines return a +** NULL pointer if [sqlite3_malloc()] is unable to allocate enough +** memory to hold the resulting string. +** +** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from +** the standard C library. The result is written into the +** buffer supplied as the second parameter whose size is given by +** the first parameter. Note that the order of the +** first two parameters is reversed from snprintf().)^ This is an +** historical accident that cannot be fixed without breaking +** backwards compatibility. ^(Note also that sqlite3_snprintf() +** returns a pointer to its buffer instead of the number of +** characters actually written into the buffer.)^ We admit that +** the number of characters written would be a more useful return +** value but we cannot change the implementation of sqlite3_snprintf() +** now without breaking compatibility. +** +** ^As long as the buffer size is greater than zero, sqlite3_snprintf() +** guarantees that the buffer is always zero-terminated. ^The first +** parameter "n" is the total size of the buffer, including space for +** the zero terminator. So the longest string that can be completely +** written will be n-1 characters. +** +** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf(). +** +** These routines all implement some additional formatting +** options that are useful for constructing SQL statements. +** All of the usual printf() formatting options apply. In addition, there +** is are "%q", "%Q", and "%z" options. +** +** ^(The %q option works like %s in that it substitutes a null-terminated +** string from the argument list. But %q also doubles every '\'' character. +** %q is designed for use inside a string literal.)^ By doubling each '\'' +** character it escapes that character and allows it to be inserted into +** the string. +** +** For example, assume the string variable zText contains text as follows: +** +**
    +**  char *zText = "It's a happy day!";
    +** 
    +** +** One can use this text in an SQL statement as follows: +** +**
    +**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
    +**  sqlite3_exec(db, zSQL, 0, 0, 0);
    +**  sqlite3_free(zSQL);
    +** 
    +** +** Because the %q format string is used, the '\'' character in zText +** is escaped and the SQL generated is as follows: +** +**
    +**  INSERT INTO table1 VALUES('It''s a happy day!')
    +** 
    +** +** This is correct. Had we used %s instead of %q, the generated SQL +** would have looked like this: +** +**
    +**  INSERT INTO table1 VALUES('It's a happy day!');
    +** 
    +** +** This second example is an SQL syntax error. As a general rule you should +** always use %q instead of %s when inserting text into a string literal. +** +** ^(The %Q option works like %q except it also adds single quotes around +** the outside of the total string. Additionally, if the parameter in the +** argument list is a NULL pointer, %Q substitutes the text "NULL" (without +** single quotes).)^ So, for example, one could say: +** +**
    +**  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
    +**  sqlite3_exec(db, zSQL, 0, 0, 0);
    +**  sqlite3_free(zSQL);
    +** 
    +** +** The code above will render a correct SQL statement in the zSQL +** variable even if the zText variable is a NULL pointer. +** +** ^(The "%z" formatting option works like "%s" but with the +** addition that after the string has been read and copied into +** the result, [sqlite3_free()] is called on the input string.)^ +*/ +SQLITE_API char *sqlite3_mprintf(const char*,...); +SQLITE_API char *sqlite3_vmprintf(const char*, va_list); +SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...); +SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list); + +/* +** CAPI3REF: Memory Allocation Subsystem +** +** The SQLite core uses these three routines for all of its own +** internal memory allocation needs. "Core" in the previous sentence +** does not include operating-system specific VFS implementation. The +** Windows VFS uses native malloc() and free() for some operations. +** +** ^The sqlite3_malloc() routine returns a pointer to a block +** of memory at least N bytes in length, where N is the parameter. +** ^If sqlite3_malloc() is unable to obtain sufficient free +** memory, it returns a NULL pointer. ^If the parameter N to +** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns +** a NULL pointer. +** +** ^Calling sqlite3_free() with a pointer previously returned +** by sqlite3_malloc() or sqlite3_realloc() releases that memory so +** that it might be reused. ^The sqlite3_free() routine is +** a no-op if is called with a NULL pointer. Passing a NULL pointer +** to sqlite3_free() is harmless. After being freed, memory +** should neither be read nor written. Even reading previously freed +** memory might result in a segmentation fault or other severe error. +** Memory corruption, a segmentation fault, or other severe error +** might result if sqlite3_free() is called with a non-NULL pointer that +** was not obtained from sqlite3_malloc() or sqlite3_realloc(). +** +** ^(The sqlite3_realloc() interface attempts to resize a +** prior memory allocation to be at least N bytes, where N is the +** second parameter. The memory allocation to be resized is the first +** parameter.)^ ^ If the first parameter to sqlite3_realloc() +** is a NULL pointer then its behavior is identical to calling +** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc(). +** ^If the second parameter to sqlite3_realloc() is zero or +** negative then the behavior is exactly the same as calling +** sqlite3_free(P) where P is the first parameter to sqlite3_realloc(). +** ^sqlite3_realloc() returns a pointer to a memory allocation +** of at least N bytes in size or NULL if sufficient memory is unavailable. +** ^If M is the size of the prior allocation, then min(N,M) bytes +** of the prior allocation are copied into the beginning of buffer returned +** by sqlite3_realloc() and the prior allocation is freed. +** ^If sqlite3_realloc() returns NULL, then the prior allocation +** is not freed. +** +** ^The memory returned by sqlite3_malloc() and sqlite3_realloc() +** is always aligned to at least an 8 byte boundary, or to a +** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time +** option is used. +** +** In SQLite version 3.5.0 and 3.5.1, it was possible to define +** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in +** implementation of these routines to be omitted. That capability +** is no longer provided. Only built-in memory allocators can be used. +** +** The Windows OS interface layer calls +** the system malloc() and free() directly when converting +** filenames between the UTF-8 encoding used by SQLite +** and whatever filename encoding is used by the particular Windows +** installation. Memory allocation errors are detected, but +** they are reported back as [SQLITE_CANTOPEN] or +** [SQLITE_IOERR] rather than [SQLITE_NOMEM]. +** +** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()] +** must be either NULL or else pointers obtained from a prior +** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have +** not yet been released. +** +** The application must not read or write any part of +** a block of memory after it has been released using +** [sqlite3_free()] or [sqlite3_realloc()]. +*/ +SQLITE_API void *sqlite3_malloc(int); +SQLITE_API void *sqlite3_realloc(void*, int); +SQLITE_API void sqlite3_free(void*); + +/* +** CAPI3REF: Memory Allocator Statistics +** +** SQLite provides these two interfaces for reporting on the status +** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()] +** routines, which form the built-in memory allocation subsystem. +** +** ^The [sqlite3_memory_used()] routine returns the number of bytes +** of memory currently outstanding (malloced but not freed). +** ^The [sqlite3_memory_highwater()] routine returns the maximum +** value of [sqlite3_memory_used()] since the high-water mark +** was last reset. ^The values returned by [sqlite3_memory_used()] and +** [sqlite3_memory_highwater()] include any overhead +** added by SQLite in its implementation of [sqlite3_malloc()], +** but not overhead added by the any underlying system library +** routines that [sqlite3_malloc()] may call. +** +** ^The memory high-water mark is reset to the current value of +** [sqlite3_memory_used()] if and only if the parameter to +** [sqlite3_memory_highwater()] is true. ^The value returned +** by [sqlite3_memory_highwater(1)] is the high-water mark +** prior to the reset. +*/ +SQLITE_API sqlite3_int64 sqlite3_memory_used(void); +SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag); + +/* +** CAPI3REF: Pseudo-Random Number Generator +** +** SQLite contains a high-quality pseudo-random number generator (PRNG) used to +** select random [ROWID | ROWIDs] when inserting new records into a table that +** already uses the largest possible [ROWID]. The PRNG is also used for +** the build-in random() and randomblob() SQL functions. This interface allows +** applications to access the same PRNG for other purposes. +** +** ^A call to this routine stores N bytes of randomness into buffer P. +** +** ^The first time this routine is invoked (either internally or by +** the application) the PRNG is seeded using randomness obtained +** from the xRandomness method of the default [sqlite3_vfs] object. +** ^On all subsequent invocations, the pseudo-randomness is generated +** internally and without recourse to the [sqlite3_vfs] xRandomness +** method. +*/ +SQLITE_API void sqlite3_randomness(int N, void *P); + +/* +** CAPI3REF: Compile-Time Authorization Callbacks +** +** ^This routine registers a authorizer callback with a particular +** [database connection], supplied in the first argument. +** ^The authorizer callback is invoked as SQL statements are being compiled +** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()], +** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()]. ^At various +** points during the compilation process, as logic is being created +** to perform various actions, the authorizer callback is invoked to +** see if those actions are allowed. ^The authorizer callback should +** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the +** specific action but allow the SQL statement to continue to be +** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be +** rejected with an error. ^If the authorizer callback returns +** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY] +** then the [sqlite3_prepare_v2()] or equivalent call that triggered +** the authorizer will fail with an error message. +** +** When the callback returns [SQLITE_OK], that means the operation +** requested is ok. ^When the callback returns [SQLITE_DENY], the +** [sqlite3_prepare_v2()] or equivalent call that triggered the +** authorizer will fail with an error message explaining that +** access is denied. +** +** ^The first parameter to the authorizer callback is a copy of the third +** parameter to the sqlite3_set_authorizer() interface. ^The second parameter +** to the callback is an integer [SQLITE_COPY | action code] that specifies +** the particular action to be authorized. ^The third through sixth parameters +** to the callback are zero-terminated strings that contain additional +** details about the action to be authorized. +** +** ^If the action code is [SQLITE_READ] +** and the callback returns [SQLITE_IGNORE] then the +** [prepared statement] statement is constructed to substitute +** a NULL value in place of the table column that would have +** been read if [SQLITE_OK] had been returned. The [SQLITE_IGNORE] +** return can be used to deny an untrusted user access to individual +** columns of a table. +** ^If the action code is [SQLITE_DELETE] and the callback returns +** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the +** [truncate optimization] is disabled and all rows are deleted individually. +** +** An authorizer is used when [sqlite3_prepare | preparing] +** SQL statements from an untrusted source, to ensure that the SQL statements +** do not try to access data they are not allowed to see, or that they do not +** try to execute malicious statements that damage the database. For +** example, an application may allow a user to enter arbitrary +** SQL queries for evaluation by a database. But the application does +** not want the user to be able to make arbitrary changes to the +** database. An authorizer could then be put in place while the +** user-entered SQL is being [sqlite3_prepare | prepared] that +** disallows everything except [SELECT] statements. +** +** Applications that need to process SQL from untrusted sources +** might also consider lowering resource limits using [sqlite3_limit()] +** and limiting database size using the [max_page_count] [PRAGMA] +** in addition to using an authorizer. +** +** ^(Only a single authorizer can be in place on a database connection +** at a time. Each call to sqlite3_set_authorizer overrides the +** previous call.)^ ^Disable the authorizer by installing a NULL callback. +** The authorizer is disabled by default. +** +** The authorizer callback must not do anything that will modify +** the database connection that invoked the authorizer callback. +** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their +** database connections for the meaning of "modify" in this paragraph. +** +** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the +** statement might be re-prepared during [sqlite3_step()] due to a +** schema change. Hence, the application should ensure that the +** correct authorizer callback remains in place during the [sqlite3_step()]. +** +** ^Note that the authorizer callback is invoked only during +** [sqlite3_prepare()] or its variants. Authorization is not +** performed during statement evaluation in [sqlite3_step()], unless +** as stated in the previous paragraph, sqlite3_step() invokes +** sqlite3_prepare_v2() to reprepare a statement after a schema change. +*/ +SQLITE_API int sqlite3_set_authorizer( + sqlite3*, + int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), + void *pUserData +); + +/* +** CAPI3REF: Authorizer Return Codes +** +** The [sqlite3_set_authorizer | authorizer callback function] must +** return either [SQLITE_OK] or one of these two constants in order +** to signal SQLite whether or not the action is permitted. See the +** [sqlite3_set_authorizer | authorizer documentation] for additional +** information. +*/ +#define SQLITE_DENY 1 /* Abort the SQL statement with an error */ +#define SQLITE_IGNORE 2 /* Don't allow access, but don't generate an error */ + +/* +** CAPI3REF: Authorizer Action Codes +** +** The [sqlite3_set_authorizer()] interface registers a callback function +** that is invoked to authorize certain SQL statement actions. The +** second parameter to the callback is an integer code that specifies +** what action is being authorized. These are the integer action codes that +** the authorizer callback may be passed. +** +** These action code values signify what kind of operation is to be +** authorized. The 3rd and 4th parameters to the authorization +** callback function will be parameters or NULL depending on which of these +** codes is used as the second parameter. ^(The 5th parameter to the +** authorizer callback is the name of the database ("main", "temp", +** etc.) if applicable.)^ ^The 6th parameter to the authorizer callback +** is the name of the inner-most trigger or view that is responsible for +** the access attempt or NULL if this access attempt is directly from +** top-level SQL code. +*/ +/******************************************* 3rd ************ 4th ***********/ +#define SQLITE_CREATE_INDEX 1 /* Index Name Table Name */ +#define SQLITE_CREATE_TABLE 2 /* Table Name NULL */ +#define SQLITE_CREATE_TEMP_INDEX 3 /* Index Name Table Name */ +#define SQLITE_CREATE_TEMP_TABLE 4 /* Table Name NULL */ +#define SQLITE_CREATE_TEMP_TRIGGER 5 /* Trigger Name Table Name */ +#define SQLITE_CREATE_TEMP_VIEW 6 /* View Name NULL */ +#define SQLITE_CREATE_TRIGGER 7 /* Trigger Name Table Name */ +#define SQLITE_CREATE_VIEW 8 /* View Name NULL */ +#define SQLITE_DELETE 9 /* Table Name NULL */ +#define SQLITE_DROP_INDEX 10 /* Index Name Table Name */ +#define SQLITE_DROP_TABLE 11 /* Table Name NULL */ +#define SQLITE_DROP_TEMP_INDEX 12 /* Index Name Table Name */ +#define SQLITE_DROP_TEMP_TABLE 13 /* Table Name NULL */ +#define SQLITE_DROP_TEMP_TRIGGER 14 /* Trigger Name Table Name */ +#define SQLITE_DROP_TEMP_VIEW 15 /* View Name NULL */ +#define SQLITE_DROP_TRIGGER 16 /* Trigger Name Table Name */ +#define SQLITE_DROP_VIEW 17 /* View Name NULL */ +#define SQLITE_INSERT 18 /* Table Name NULL */ +#define SQLITE_PRAGMA 19 /* Pragma Name 1st arg or NULL */ +#define SQLITE_READ 20 /* Table Name Column Name */ +#define SQLITE_SELECT 21 /* NULL NULL */ +#define SQLITE_TRANSACTION 22 /* Operation NULL */ +#define SQLITE_UPDATE 23 /* Table Name Column Name */ +#define SQLITE_ATTACH 24 /* Filename NULL */ +#define SQLITE_DETACH 25 /* Database Name NULL */ +#define SQLITE_ALTER_TABLE 26 /* Database Name Table Name */ +#define SQLITE_REINDEX 27 /* Index Name NULL */ +#define SQLITE_ANALYZE 28 /* Table Name NULL */ +#define SQLITE_CREATE_VTABLE 29 /* Table Name Module Name */ +#define SQLITE_DROP_VTABLE 30 /* Table Name Module Name */ +#define SQLITE_FUNCTION 31 /* NULL Function Name */ +#define SQLITE_SAVEPOINT 32 /* Operation Savepoint Name */ +#define SQLITE_COPY 0 /* No longer used */ + +/* +** CAPI3REF: Tracing And Profiling Functions +** +** These routines register callback functions that can be used for +** tracing and profiling the execution of SQL statements. +** +** ^The callback function registered by sqlite3_trace() is invoked at +** various times when an SQL statement is being run by [sqlite3_step()]. +** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the +** SQL statement text as the statement first begins executing. +** ^(Additional sqlite3_trace() callbacks might occur +** as each triggered subprogram is entered. The callbacks for triggers +** contain a UTF-8 SQL comment that identifies the trigger.)^ +** +** ^The callback function registered by sqlite3_profile() is invoked +** as each SQL statement finishes. ^The profile callback contains +** the original statement text and an estimate of wall-clock time +** of how long that statement took to run. ^The profile callback +** time is in units of nanoseconds, however the current implementation +** is only capable of millisecond resolution so the six least significant +** digits in the time are meaningless. Future versions of SQLite +** might provide greater resolution on the profiler callback. The +** sqlite3_profile() function is considered experimental and is +** subject to change in future versions of SQLite. +*/ +SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); +SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*, + void(*xProfile)(void*,const char*,sqlite3_uint64), void*); + +/* +** CAPI3REF: Query Progress Callbacks +** +** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback +** function X to be invoked periodically during long running calls to +** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for +** database connection D. An example use for this +** interface is to keep a GUI updated during a large query. +** +** ^The parameter P is passed through as the only parameter to the +** callback function X. ^The parameter N is the number of +** [virtual machine instructions] that are evaluated between successive +** invocations of the callback X. +** +** ^Only a single progress handler may be defined at one time per +** [database connection]; setting a new progress handler cancels the +** old one. ^Setting parameter X to NULL disables the progress handler. +** ^The progress handler is also disabled by setting N to a value less +** than 1. +** +** ^If the progress callback returns non-zero, the operation is +** interrupted. This feature can be used to implement a +** "Cancel" button on a GUI progress dialog box. +** +** The progress handler callback must not do anything that will modify +** the database connection that invoked the progress handler. +** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their +** database connections for the meaning of "modify" in this paragraph. +** +*/ +SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); + +/* +** CAPI3REF: Opening A New Database Connection +** +** ^These routines open an SQLite database file whose name is given by the +** filename argument. ^The filename argument is interpreted as UTF-8 for +** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte +** order for sqlite3_open16(). ^(A [database connection] handle is usually +** returned in *ppDb, even if an error occurs. The only exception is that +** if SQLite is unable to allocate memory to hold the [sqlite3] object, +** a NULL will be written into *ppDb instead of a pointer to the [sqlite3] +** object.)^ ^(If the database is opened (and/or created) successfully, then +** [SQLITE_OK] is returned. Otherwise an [error code] is returned.)^ ^The +** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain +** an English language description of the error following a failure of any +** of the sqlite3_open() routines. +** +** ^The default encoding for the database will be UTF-8 if +** sqlite3_open() or sqlite3_open_v2() is called and +** UTF-16 in the native byte order if sqlite3_open16() is used. +** +** Whether or not an error occurs when it is opened, resources +** associated with the [database connection] handle should be released by +** passing it to [sqlite3_close()] when it is no longer required. +** +** The sqlite3_open_v2() interface works like sqlite3_open() +** except that it accepts two additional parameters for additional control +** over the new database connection. ^(The flags parameter to +** sqlite3_open_v2() can take one of +** the following three values, optionally combined with the +** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE], +** and/or [SQLITE_OPEN_PRIVATECACHE] flags:)^ +** +**
    +** ^(
    [SQLITE_OPEN_READONLY]
    +**
    The database is opened in read-only mode. If the database does not +** already exist, an error is returned.
    )^ +** +** ^(
    [SQLITE_OPEN_READWRITE]
    +**
    The database is opened for reading and writing if possible, or reading +** only if the file is write protected by the operating system. In either +** case the database must already exist, otherwise an error is returned.
    )^ +** +** ^(
    [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]
    +**
    The database is opened for reading and writing, and is created if +** it does not already exist. This is the behavior that is always used for +** sqlite3_open() and sqlite3_open16().
    )^ +**
    +** +** If the 3rd parameter to sqlite3_open_v2() is not one of the +** combinations shown above or one of the combinations shown above combined +** with the [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], +** [SQLITE_OPEN_SHAREDCACHE] and/or [SQLITE_OPEN_PRIVATECACHE] flags, +** then the behavior is undefined. +** +** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection +** opens in the multi-thread [threading mode] as long as the single-thread +** mode has not been set at compile-time or start-time. ^If the +** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens +** in the serialized [threading mode] unless single-thread was +** previously selected at compile-time or start-time. +** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be +** eligible to use [shared cache mode], regardless of whether or not shared +** cache is enabled using [sqlite3_enable_shared_cache()]. ^The +** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not +** participate in [shared cache mode] even if it is enabled. +** +** ^If the filename is ":memory:", then a private, temporary in-memory database +** is created for the connection. ^This in-memory database will vanish when +** the database connection is closed. Future versions of SQLite might +** make use of additional special filenames that begin with the ":" character. +** It is recommended that when a database filename actually does begin with +** a ":" character you should prefix the filename with a pathname such as +** "./" to avoid ambiguity. +** +** ^If the filename is an empty string, then a private, temporary +** on-disk database will be created. ^This private database will be +** automatically deleted as soon as the database connection is closed. +** +** ^The fourth parameter to sqlite3_open_v2() is the name of the +** [sqlite3_vfs] object that defines the operating system interface that +** the new database connection should use. ^If the fourth parameter is +** a NULL pointer then the default [sqlite3_vfs] object is used. +** +** Note to Windows users: The encoding used for the filename argument +** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever +** codepage is currently defined. Filenames containing international +** characters must be converted to UTF-8 prior to passing them into +** sqlite3_open() or sqlite3_open_v2(). +*/ +SQLITE_API int sqlite3_open( + const char *filename, /* Database filename (UTF-8) */ + sqlite3 **ppDb /* OUT: SQLite db handle */ +); +SQLITE_API int sqlite3_open16( + const void *filename, /* Database filename (UTF-16) */ + sqlite3 **ppDb /* OUT: SQLite db handle */ +); +SQLITE_API int sqlite3_open_v2( + const char *filename, /* Database filename (UTF-8) */ + sqlite3 **ppDb, /* OUT: SQLite db handle */ + int flags, /* Flags */ + const char *zVfs /* Name of VFS module to use */ +); + +/* +** CAPI3REF: Error Codes And Messages +** +** ^The sqlite3_errcode() interface returns the numeric [result code] or +** [extended result code] for the most recent failed sqlite3_* API call +** associated with a [database connection]. If a prior API call failed +** but the most recent API call succeeded, the return value from +** sqlite3_errcode() is undefined. ^The sqlite3_extended_errcode() +** interface is the same except that it always returns the +** [extended result code] even when extended result codes are +** disabled. +** +** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language +** text that describes the error, as either UTF-8 or UTF-16 respectively. +** ^(Memory to hold the error message string is managed internally. +** The application does not need to worry about freeing the result. +** However, the error string might be overwritten or deallocated by +** subsequent calls to other SQLite interface functions.)^ +** +** When the serialized [threading mode] is in use, it might be the +** case that a second error occurs on a separate thread in between +** the time of the first error and the call to these interfaces. +** When that happens, the second error will be reported since these +** interfaces always report the most recent result. To avoid +** this, each thread can obtain exclusive use of the [database connection] D +** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning +** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after +** all calls to the interfaces listed here are completed. +** +** If an interface fails with SQLITE_MISUSE, that means the interface +** was invoked incorrectly by the application. In that case, the +** error code and message may or may not be set. +*/ +SQLITE_API int sqlite3_errcode(sqlite3 *db); +SQLITE_API int sqlite3_extended_errcode(sqlite3 *db); +SQLITE_API const char *sqlite3_errmsg(sqlite3*); +SQLITE_API const void *sqlite3_errmsg16(sqlite3*); + +/* +** CAPI3REF: SQL Statement Object +** KEYWORDS: {prepared statement} {prepared statements} +** +** An instance of this object represents a single SQL statement. +** This object is variously known as a "prepared statement" or a +** "compiled SQL statement" or simply as a "statement". +** +** The life of a statement object goes something like this: +** +**
      +**
    1. Create the object using [sqlite3_prepare_v2()] or a related +** function. +**
    2. Bind values to [host parameters] using the sqlite3_bind_*() +** interfaces. +**
    3. Run the SQL by calling [sqlite3_step()] one or more times. +**
    4. Reset the statement using [sqlite3_reset()] then go back +** to step 2. Do this zero or more times. +**
    5. Destroy the object using [sqlite3_finalize()]. +**
    +** +** Refer to documentation on individual methods above for additional +** information. +*/ +typedef struct sqlite3_stmt sqlite3_stmt; + +/* +** CAPI3REF: Run-time Limits +** +** ^(This interface allows the size of various constructs to be limited +** on a connection by connection basis. The first parameter is the +** [database connection] whose limit is to be set or queried. The +** second parameter is one of the [limit categories] that define a +** class of constructs to be size limited. The third parameter is the +** new limit for that construct.)^ +** +** ^If the new limit is a negative number, the limit is unchanged. +** ^(For each limit category SQLITE_LIMIT_NAME there is a +** [limits | hard upper bound] +** set at compile-time by a C preprocessor macro called +** [limits | SQLITE_MAX_NAME]. +** (The "_LIMIT_" in the name is changed to "_MAX_".))^ +** ^Attempts to increase a limit above its hard upper bound are +** silently truncated to the hard upper bound. +** +** ^Regardless of whether or not the limit was changed, the +** [sqlite3_limit()] interface returns the prior value of the limit. +** ^Hence, to find the current value of a limit without changing it, +** simply invoke this interface with the third parameter set to -1. +** +** Run-time limits are intended for use in applications that manage +** both their own internal database and also databases that are controlled +** by untrusted external sources. An example application might be a +** web browser that has its own databases for storing history and +** separate databases controlled by JavaScript applications downloaded +** off the Internet. The internal databases can be given the +** large, default limits. Databases managed by external sources can +** be given much smaller limits designed to prevent a denial of service +** attack. Developers might also want to use the [sqlite3_set_authorizer()] +** interface to further control untrusted SQL. The size of the database +** created by an untrusted script can be contained using the +** [max_page_count] [PRAGMA]. +** +** New run-time limit categories may be added in future releases. +*/ +SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal); + +/* +** CAPI3REF: Run-Time Limit Categories +** KEYWORDS: {limit category} {*limit categories} +** +** These constants define various performance limits +** that can be lowered at run-time using [sqlite3_limit()]. +** The synopsis of the meanings of the various limits is shown below. +** Additional information is available at [limits | Limits in SQLite]. +** +**
    +** ^(
    SQLITE_LIMIT_LENGTH
    +**
    The maximum size of any string or BLOB or table row, in bytes.
    )^ +** +** ^(
    SQLITE_LIMIT_SQL_LENGTH
    +**
    The maximum length of an SQL statement, in bytes.
    )^ +** +** ^(
    SQLITE_LIMIT_COLUMN
    +**
    The maximum number of columns in a table definition or in the +** result set of a [SELECT] or the maximum number of columns in an index +** or in an ORDER BY or GROUP BY clause.
    )^ +** +** ^(
    SQLITE_LIMIT_EXPR_DEPTH
    +**
    The maximum depth of the parse tree on any expression.
    )^ +** +** ^(
    SQLITE_LIMIT_COMPOUND_SELECT
    +**
    The maximum number of terms in a compound SELECT statement.
    )^ +** +** ^(
    SQLITE_LIMIT_VDBE_OP
    +**
    The maximum number of instructions in a virtual machine program +** used to implement an SQL statement. This limit is not currently +** enforced, though that might be added in some future release of +** SQLite.
    )^ +** +** ^(
    SQLITE_LIMIT_FUNCTION_ARG
    +**
    The maximum number of arguments on a function.
    )^ +** +** ^(
    SQLITE_LIMIT_ATTACHED
    +**
    The maximum number of [ATTACH | attached databases].)^
    +** +** ^(
    SQLITE_LIMIT_LIKE_PATTERN_LENGTH
    +**
    The maximum length of the pattern argument to the [LIKE] or +** [GLOB] operators.
    )^ +** +** ^(
    SQLITE_LIMIT_VARIABLE_NUMBER
    +**
    The maximum index number of any [parameter] in an SQL statement.)^ +** +** ^(
    SQLITE_LIMIT_TRIGGER_DEPTH
    +**
    The maximum depth of recursion for triggers.
    )^ +**
    +*/ +#define SQLITE_LIMIT_LENGTH 0 +#define SQLITE_LIMIT_SQL_LENGTH 1 +#define SQLITE_LIMIT_COLUMN 2 +#define SQLITE_LIMIT_EXPR_DEPTH 3 +#define SQLITE_LIMIT_COMPOUND_SELECT 4 +#define SQLITE_LIMIT_VDBE_OP 5 +#define SQLITE_LIMIT_FUNCTION_ARG 6 +#define SQLITE_LIMIT_ATTACHED 7 +#define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8 +#define SQLITE_LIMIT_VARIABLE_NUMBER 9 +#define SQLITE_LIMIT_TRIGGER_DEPTH 10 + +/* +** CAPI3REF: Compiling An SQL Statement +** KEYWORDS: {SQL statement compiler} +** +** To execute an SQL query, it must first be compiled into a byte-code +** program using one of these routines. +** +** The first argument, "db", is a [database connection] obtained from a +** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or +** [sqlite3_open16()]. The database connection must not have been closed. +** +** The second argument, "zSql", is the statement to be compiled, encoded +** as either UTF-8 or UTF-16. The sqlite3_prepare() and sqlite3_prepare_v2() +** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2() +** use UTF-16. +** +** ^If the nByte argument is less than zero, then zSql is read up to the +** first zero terminator. ^If nByte is non-negative, then it is the maximum +** number of bytes read from zSql. ^When nByte is non-negative, the +** zSql string ends at either the first '\000' or '\u0000' character or +** the nByte-th byte, whichever comes first. If the caller knows +** that the supplied string is nul-terminated, then there is a small +** performance advantage to be gained by passing an nByte parameter that +** is equal to the number of bytes in the input string including +** the nul-terminator bytes. +** +** ^If pzTail is not NULL then *pzTail is made to point to the first byte +** past the end of the first SQL statement in zSql. These routines only +** compile the first statement in zSql, so *pzTail is left pointing to +** what remains uncompiled. +** +** ^*ppStmt is left pointing to a compiled [prepared statement] that can be +** executed using [sqlite3_step()]. ^If there is an error, *ppStmt is set +** to NULL. ^If the input text contains no SQL (if the input is an empty +** string or a comment) then *ppStmt is set to NULL. +** The calling procedure is responsible for deleting the compiled +** SQL statement using [sqlite3_finalize()] after it has finished with it. +** ppStmt may not be NULL. +** +** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK]; +** otherwise an [error code] is returned. +** +** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are +** recommended for all new programs. The two older interfaces are retained +** for backwards compatibility, but their use is discouraged. +** ^In the "v2" interfaces, the prepared statement +** that is returned (the [sqlite3_stmt] object) contains a copy of the +** original SQL text. This causes the [sqlite3_step()] interface to +** behave differently in three ways: +** +**
      +**
    1. +** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it +** always used to do, [sqlite3_step()] will automatically recompile the SQL +** statement and try to run it again. +**
    2. +** +**
    3. +** ^When an error occurs, [sqlite3_step()] will return one of the detailed +** [error codes] or [extended error codes]. ^The legacy behavior was that +** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code +** and the application would have to make a second call to [sqlite3_reset()] +** in order to find the underlying cause of the problem. With the "v2" prepare +** interfaces, the underlying reason for the error is returned immediately. +**
    4. +** +**
    5. +** ^If the specific value bound to [parameter | host parameter] in the +** WHERE clause might influence the choice of query plan for a statement, +** then the statement will be automatically recompiled, as if there had been +** a schema change, on the first [sqlite3_step()] call following any change +** to the [sqlite3_bind_text | bindings] of that [parameter]. +** ^The specific value of WHERE-clause [parameter] might influence the +** choice of query plan if the parameter is the left-hand side of a [LIKE] +** or [GLOB] operator or if the parameter is compared to an indexed column +** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled. +** the +**
    6. +**
    +*/ +SQLITE_API int sqlite3_prepare( + sqlite3 *db, /* Database handle */ + const char *zSql, /* SQL statement, UTF-8 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const char **pzTail /* OUT: Pointer to unused portion of zSql */ +); +SQLITE_API int sqlite3_prepare_v2( + sqlite3 *db, /* Database handle */ + const char *zSql, /* SQL statement, UTF-8 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const char **pzTail /* OUT: Pointer to unused portion of zSql */ +); +SQLITE_API int sqlite3_prepare16( + sqlite3 *db, /* Database handle */ + const void *zSql, /* SQL statement, UTF-16 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const void **pzTail /* OUT: Pointer to unused portion of zSql */ +); +SQLITE_API int sqlite3_prepare16_v2( + sqlite3 *db, /* Database handle */ + const void *zSql, /* SQL statement, UTF-16 encoded */ + int nByte, /* Maximum length of zSql in bytes. */ + sqlite3_stmt **ppStmt, /* OUT: Statement handle */ + const void **pzTail /* OUT: Pointer to unused portion of zSql */ +); + +/* +** CAPI3REF: Retrieving Statement SQL +** +** ^This interface can be used to retrieve a saved copy of the original +** SQL text used to create a [prepared statement] if that statement was +** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()]. +*/ +SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Determine If An SQL Statement Writes The Database +** +** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if +** and only if the [prepared statement] X makes no direct changes to +** the content of the database file. +** +** Note that [application-defined SQL functions] or +** [virtual tables] might change the database indirectly as a side effect. +** ^(For example, if an application defines a function "eval()" that +** calls [sqlite3_exec()], then the following SQL statement would +** change the database file through side-effects: +** +**
    +**    SELECT eval('DELETE FROM t1') FROM t2;
    +** 
    +** +** But because the [SELECT] statement does not change the database file +** directly, sqlite3_stmt_readonly() would still return true.)^ +** +** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK], +** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true, +** since the statements themselves do not actually modify the database but +** rather they control the timing of when other statements modify the +** database. ^The [ATTACH] and [DETACH] statements also cause +** sqlite3_stmt_readonly() to return true since, while those statements +** change the configuration of a database connection, they do not make +** changes to the content of the database files on disk. +*/ +SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Dynamically Typed Value Object +** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value} +** +** SQLite uses the sqlite3_value object to represent all values +** that can be stored in a database table. SQLite uses dynamic typing +** for the values it stores. ^Values stored in sqlite3_value objects +** can be integers, floating point values, strings, BLOBs, or NULL. +** +** An sqlite3_value object may be either "protected" or "unprotected". +** Some interfaces require a protected sqlite3_value. Other interfaces +** will accept either a protected or an unprotected sqlite3_value. +** Every interface that accepts sqlite3_value arguments specifies +** whether or not it requires a protected sqlite3_value. +** +** The terms "protected" and "unprotected" refer to whether or not +** a mutex is held. A internal mutex is held for a protected +** sqlite3_value object but no mutex is held for an unprotected +** sqlite3_value object. If SQLite is compiled to be single-threaded +** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0) +** or if SQLite is run in one of reduced mutex modes +** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD] +** then there is no distinction between protected and unprotected +** sqlite3_value objects and they can be used interchangeably. However, +** for maximum code portability it is recommended that applications +** still make the distinction between protected and unprotected +** sqlite3_value objects even when not strictly required. +** +** ^The sqlite3_value objects that are passed as parameters into the +** implementation of [application-defined SQL functions] are protected. +** ^The sqlite3_value object returned by +** [sqlite3_column_value()] is unprotected. +** Unprotected sqlite3_value objects may only be used with +** [sqlite3_result_value()] and [sqlite3_bind_value()]. +** The [sqlite3_value_blob | sqlite3_value_type()] family of +** interfaces require protected sqlite3_value objects. +*/ +typedef struct Mem sqlite3_value; + +/* +** CAPI3REF: SQL Function Context Object +** +** The context in which an SQL function executes is stored in an +** sqlite3_context object. ^A pointer to an sqlite3_context object +** is always first parameter to [application-defined SQL functions]. +** The application-defined SQL function implementation will pass this +** pointer through into calls to [sqlite3_result_int | sqlite3_result()], +** [sqlite3_aggregate_context()], [sqlite3_user_data()], +** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()], +** and/or [sqlite3_set_auxdata()]. +*/ +typedef struct sqlite3_context sqlite3_context; + +/* +** CAPI3REF: Binding Values To Prepared Statements +** KEYWORDS: {host parameter} {host parameters} {host parameter name} +** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding} +** +** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants, +** literals may be replaced by a [parameter] that matches one of following +** templates: +** +**
      +**
    • ? +**
    • ?NNN +**
    • :VVV +**
    • @VVV +**
    • $VVV +**
    +** +** In the templates above, NNN represents an integer literal, +** and VVV represents an alphanumeric identifier.)^ ^The values of these +** parameters (also called "host parameter names" or "SQL parameters") +** can be set using the sqlite3_bind_*() routines defined here. +** +** ^The first argument to the sqlite3_bind_*() routines is always +** a pointer to the [sqlite3_stmt] object returned from +** [sqlite3_prepare_v2()] or its variants. +** +** ^The second argument is the index of the SQL parameter to be set. +** ^The leftmost SQL parameter has an index of 1. ^When the same named +** SQL parameter is used more than once, second and subsequent +** occurrences have the same index as the first occurrence. +** ^The index for named parameters can be looked up using the +** [sqlite3_bind_parameter_index()] API if desired. ^The index +** for "?NNN" parameters is the value of NNN. +** ^The NNN value must be between 1 and the [sqlite3_limit()] +** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999). +** +** ^The third argument is the value to bind to the parameter. +** +** ^(In those routines that have a fourth argument, its value is the +** number of bytes in the parameter. To be clear: the value is the +** number of bytes in the value, not the number of characters.)^ +** ^If the fourth parameter is negative, the length of the string is +** the number of bytes up to the first zero terminator. +** +** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and +** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or +** string after SQLite has finished with it. ^The destructor is called +** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(), +** sqlite3_bind_text(), or sqlite3_bind_text16() fails. +** ^If the fifth argument is +** the special value [SQLITE_STATIC], then SQLite assumes that the +** information is in static, unmanaged space and does not need to be freed. +** ^If the fifth argument has the value [SQLITE_TRANSIENT], then +** SQLite makes its own private copy of the data immediately, before +** the sqlite3_bind_*() routine returns. +** +** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that +** is filled with zeroes. ^A zeroblob uses a fixed amount of memory +** (just an integer to hold its size) while it is being processed. +** Zeroblobs are intended to serve as placeholders for BLOBs whose +** content is later written using +** [sqlite3_blob_open | incremental BLOB I/O] routines. +** ^A negative value for the zeroblob results in a zero-length BLOB. +** +** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer +** for the [prepared statement] or with a prepared statement for which +** [sqlite3_step()] has been called more recently than [sqlite3_reset()], +** then the call will return [SQLITE_MISUSE]. If any sqlite3_bind_() +** routine is passed a [prepared statement] that has been finalized, the +** result is undefined and probably harmful. +** +** ^Bindings are not cleared by the [sqlite3_reset()] routine. +** ^Unbound parameters are interpreted as NULL. +** +** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an +** [error code] if anything goes wrong. +** ^[SQLITE_RANGE] is returned if the parameter +** index is out of range. ^[SQLITE_NOMEM] is returned if malloc() fails. +** +** See also: [sqlite3_bind_parameter_count()], +** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()]. +*/ +SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); +SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double); +SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int); +SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); +SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int); +SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); +SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); +SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); +SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); + +/* +** CAPI3REF: Number Of SQL Parameters +** +** ^This routine can be used to find the number of [SQL parameters] +** in a [prepared statement]. SQL parameters are tokens of the +** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as +** placeholders for values that are [sqlite3_bind_blob | bound] +** to the parameters at a later time. +** +** ^(This routine actually returns the index of the largest (rightmost) +** parameter. For all forms except ?NNN, this will correspond to the +** number of unique parameters. If parameters of the ?NNN form are used, +** there may be gaps in the list.)^ +** +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_name()], and +** [sqlite3_bind_parameter_index()]. +*/ +SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*); + +/* +** CAPI3REF: Name Of A Host Parameter +** +** ^The sqlite3_bind_parameter_name(P,N) interface returns +** the name of the N-th [SQL parameter] in the [prepared statement] P. +** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA" +** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA" +** respectively. +** In other words, the initial ":" or "$" or "@" or "?" +** is included as part of the name.)^ +** ^Parameters of the form "?" without a following integer have no name +** and are referred to as "nameless" or "anonymous parameters". +** +** ^The first host parameter has an index of 1, not 0. +** +** ^If the value N is out of range or if the N-th parameter is +** nameless, then NULL is returned. ^The returned string is +** always in UTF-8 encoding even if the named parameter was +** originally specified as UTF-16 in [sqlite3_prepare16()] or +** [sqlite3_prepare16_v2()]. +** +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_count()], and +** [sqlite3_bind_parameter_index()]. +*/ +SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); + +/* +** CAPI3REF: Index Of A Parameter With A Given Name +** +** ^Return the index of an SQL parameter given its name. ^The +** index value returned is suitable for use as the second +** parameter to [sqlite3_bind_blob|sqlite3_bind()]. ^A zero +** is returned if no matching parameter is found. ^The parameter +** name must be given in UTF-8 even if the original statement +** was prepared from UTF-16 text using [sqlite3_prepare16_v2()]. +** +** See also: [sqlite3_bind_blob|sqlite3_bind()], +** [sqlite3_bind_parameter_count()], and +** [sqlite3_bind_parameter_index()]. +*/ +SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); + +/* +** CAPI3REF: Reset All Bindings On A Prepared Statement +** +** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset +** the [sqlite3_bind_blob | bindings] on a [prepared statement]. +** ^Use this routine to reset all host parameters to NULL. +*/ +SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*); + +/* +** CAPI3REF: Number Of Columns In A Result Set +** +** ^Return the number of columns in the result set returned by the +** [prepared statement]. ^This routine returns 0 if pStmt is an SQL +** statement that does not return data (for example an [UPDATE]). +** +** See also: [sqlite3_data_count()] +*/ +SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Column Names In A Result Set +** +** ^These routines return the name assigned to a particular column +** in the result set of a [SELECT] statement. ^The sqlite3_column_name() +** interface returns a pointer to a zero-terminated UTF-8 string +** and sqlite3_column_name16() returns a pointer to a zero-terminated +** UTF-16 string. ^The first parameter is the [prepared statement] +** that implements the [SELECT] statement. ^The second parameter is the +** column number. ^The leftmost column is number 0. +** +** ^The returned string pointer is valid until either the [prepared statement] +** is destroyed by [sqlite3_finalize()] or until the next call to +** sqlite3_column_name() or sqlite3_column_name16() on the same column. +** +** ^If sqlite3_malloc() fails during the processing of either routine +** (for example during a conversion from UTF-8 to UTF-16) then a +** NULL pointer is returned. +** +** ^The name of a result column is the value of the "AS" clause for +** that column, if there is an AS clause. If there is no AS clause +** then the name of the column is unspecified and may change from +** one release of SQLite to the next. +*/ +SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N); +SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N); + +/* +** CAPI3REF: Source Of Data In A Query Result +** +** ^These routines provide a means to determine the database, table, and +** table column that is the origin of a particular result column in +** [SELECT] statement. +** ^The name of the database or table or column can be returned as +** either a UTF-8 or UTF-16 string. ^The _database_ routines return +** the database name, the _table_ routines return the table name, and +** the origin_ routines return the column name. +** ^The returned string is valid until the [prepared statement] is destroyed +** using [sqlite3_finalize()] or until the same information is requested +** again in a different encoding. +** +** ^The names returned are the original un-aliased names of the +** database, table, and column. +** +** ^The first argument to these interfaces is a [prepared statement]. +** ^These functions return information about the Nth result column returned by +** the statement, where N is the second function argument. +** ^The left-most column is column 0 for these routines. +** +** ^If the Nth column returned by the statement is an expression or +** subquery and is not a column value, then all of these functions return +** NULL. ^These routine might also return NULL if a memory allocation error +** occurs. ^Otherwise, they return the name of the attached database, table, +** or column that query result column was extracted from. +** +** ^As with all other SQLite APIs, those whose names end with "16" return +** UTF-16 encoded strings and the other functions return UTF-8. +** +** ^These APIs are only available if the library was compiled with the +** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol. +** +** If two or more threads call one or more of these routines against the same +** prepared statement and column at the same time then the results are +** undefined. +** +** If two or more threads call one or more +** [sqlite3_column_database_name | column metadata interfaces] +** for the same [prepared statement] and result column +** at the same time then the results are undefined. +*/ +SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int); +SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int); +SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int); +SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int); +SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int); +SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); + +/* +** CAPI3REF: Declared Datatype Of A Query Result +** +** ^(The first parameter is a [prepared statement]. +** If this statement is a [SELECT] statement and the Nth column of the +** returned result set of that [SELECT] is a table column (not an +** expression or subquery) then the declared type of the table +** column is returned.)^ ^If the Nth column of the result set is an +** expression or subquery, then a NULL pointer is returned. +** ^The returned string is always UTF-8 encoded. +** +** ^(For example, given the database schema: +** +** CREATE TABLE t1(c1 VARIANT); +** +** and the following statement to be compiled: +** +** SELECT c1 + 1, c1 FROM t1; +** +** this routine would return the string "VARIANT" for the second result +** column (i==1), and a NULL pointer for the first result column (i==0).)^ +** +** ^SQLite uses dynamic run-time typing. ^So just because a column +** is declared to contain a particular type does not mean that the +** data stored in that column is of the declared type. SQLite is +** strongly typed, but the typing is dynamic not static. ^Type +** is associated with individual values, not with the containers +** used to hold those values. +*/ +SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int); +SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int); + +/* +** CAPI3REF: Evaluate An SQL Statement +** +** After a [prepared statement] has been prepared using either +** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy +** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function +** must be called one or more times to evaluate the statement. +** +** The details of the behavior of the sqlite3_step() interface depend +** on whether the statement was prepared using the newer "v2" interface +** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy +** interface [sqlite3_prepare()] and [sqlite3_prepare16()]. The use of the +** new "v2" interface is recommended for new applications but the legacy +** interface will continue to be supported. +** +** ^In the legacy interface, the return value will be either [SQLITE_BUSY], +** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE]. +** ^With the "v2" interface, any of the other [result codes] or +** [extended result codes] might be returned as well. +** +** ^[SQLITE_BUSY] means that the database engine was unable to acquire the +** database locks it needs to do its job. ^If the statement is a [COMMIT] +** or occurs outside of an explicit transaction, then you can retry the +** statement. If the statement is not a [COMMIT] and occurs within a +** explicit transaction then you should rollback the transaction before +** continuing. +** +** ^[SQLITE_DONE] means that the statement has finished executing +** successfully. sqlite3_step() should not be called again on this virtual +** machine without first calling [sqlite3_reset()] to reset the virtual +** machine back to its initial state. +** +** ^If the SQL statement being executed returns any data, then [SQLITE_ROW] +** is returned each time a new row of data is ready for processing by the +** caller. The values may be accessed using the [column access functions]. +** sqlite3_step() is called again to retrieve the next row of data. +** +** ^[SQLITE_ERROR] means that a run-time error (such as a constraint +** violation) has occurred. sqlite3_step() should not be called again on +** the VM. More information may be found by calling [sqlite3_errmsg()]. +** ^With the legacy interface, a more specific error code (for example, +** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth) +** can be obtained by calling [sqlite3_reset()] on the +** [prepared statement]. ^In the "v2" interface, +** the more specific error code is returned directly by sqlite3_step(). +** +** [SQLITE_MISUSE] means that the this routine was called inappropriately. +** Perhaps it was called on a [prepared statement] that has +** already been [sqlite3_finalize | finalized] or on one that had +** previously returned [SQLITE_ERROR] or [SQLITE_DONE]. Or it could +** be the case that the same database connection is being used by two or +** more threads at the same moment in time. +** +** For all versions of SQLite up to and including 3.6.23.1, a call to +** [sqlite3_reset()] was required after sqlite3_step() returned anything +** other than [SQLITE_ROW] before any subsequent invocation of +** sqlite3_step(). Failure to reset the prepared statement using +** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from +** sqlite3_step(). But after version 3.6.23.1, sqlite3_step() began +** calling [sqlite3_reset()] automatically in this circumstance rather +** than returning [SQLITE_MISUSE]. This is not considered a compatibility +** break because any application that ever receives an SQLITE_MISUSE error +** is broken by definition. The [SQLITE_OMIT_AUTORESET] compile-time option +** can be used to restore the legacy behavior. +** +** Goofy Interface Alert: In the legacy interface, the sqlite3_step() +** API always returns a generic error code, [SQLITE_ERROR], following any +** error other than [SQLITE_BUSY] and [SQLITE_MISUSE]. You must call +** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the +** specific [error codes] that better describes the error. +** We admit that this is a goofy design. The problem has been fixed +** with the "v2" interface. If you prepare all of your SQL statements +** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead +** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces, +** then the more specific [error codes] are returned directly +** by sqlite3_step(). The use of the "v2" interface is recommended. +*/ +SQLITE_API int sqlite3_step(sqlite3_stmt*); + +/* +** CAPI3REF: Number of columns in a result set +** +** ^The sqlite3_data_count(P) interface returns the number of columns in the +** current row of the result set of [prepared statement] P. +** ^If prepared statement P does not have results ready to return +** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of +** interfaces) then sqlite3_data_count(P) returns 0. +** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer. +** +** See also: [sqlite3_column_count()] +*/ +SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Fundamental Datatypes +** KEYWORDS: SQLITE_TEXT +** +** ^(Every value in SQLite has one of five fundamental datatypes: +** +**
      +**
    • 64-bit signed integer +**
    • 64-bit IEEE floating point number +**
    • string +**
    • BLOB +**
    • NULL +**
    )^ +** +** These constants are codes for each of those types. +** +** Note that the SQLITE_TEXT constant was also used in SQLite version 2 +** for a completely different meaning. Software that links against both +** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not +** SQLITE_TEXT. +*/ +#define SQLITE_INTEGER 1 +#define SQLITE_FLOAT 2 +#define SQLITE_BLOB 4 +#define SQLITE_NULL 5 +#ifdef SQLITE_TEXT +# undef SQLITE_TEXT +#else +# define SQLITE_TEXT 3 +#endif +#define SQLITE3_TEXT 3 + +/* +** CAPI3REF: Result Values From A Query +** KEYWORDS: {column access functions} +** +** These routines form the "result set" interface. +** +** ^These routines return information about a single column of the current +** result row of a query. ^In every case the first argument is a pointer +** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*] +** that was returned from [sqlite3_prepare_v2()] or one of its variants) +** and the second argument is the index of the column for which information +** should be returned. ^The leftmost column of the result set has the index 0. +** ^The number of columns in the result can be determined using +** [sqlite3_column_count()]. +** +** If the SQL statement does not currently point to a valid row, or if the +** column index is out of range, the result is undefined. +** These routines may only be called when the most recent call to +** [sqlite3_step()] has returned [SQLITE_ROW] and neither +** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently. +** If any of these routines are called after [sqlite3_reset()] or +** [sqlite3_finalize()] or after [sqlite3_step()] has returned +** something other than [SQLITE_ROW], the results are undefined. +** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()] +** are called from a different thread while any of these routines +** are pending, then the results are undefined. +** +** ^The sqlite3_column_type() routine returns the +** [SQLITE_INTEGER | datatype code] for the initial data type +** of the result column. ^The returned value is one of [SQLITE_INTEGER], +** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL]. The value +** returned by sqlite3_column_type() is only meaningful if no type +** conversions have occurred as described below. After a type conversion, +** the value returned by sqlite3_column_type() is undefined. Future +** versions of SQLite may change the behavior of sqlite3_column_type() +** following a type conversion. +** +** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes() +** routine returns the number of bytes in that BLOB or string. +** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts +** the string to UTF-8 and then returns the number of bytes. +** ^If the result is a numeric value then sqlite3_column_bytes() uses +** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns +** the number of bytes in that string. +** ^If the result is NULL, then sqlite3_column_bytes() returns zero. +** +** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16() +** routine returns the number of bytes in that BLOB or string. +** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts +** the string to UTF-16 and then returns the number of bytes. +** ^If the result is a numeric value then sqlite3_column_bytes16() uses +** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns +** the number of bytes in that string. +** ^If the result is NULL, then sqlite3_column_bytes16() returns zero. +** +** ^The values returned by [sqlite3_column_bytes()] and +** [sqlite3_column_bytes16()] do not include the zero terminators at the end +** of the string. ^For clarity: the values returned by +** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of +** bytes in the string, not the number of characters. +** +** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(), +** even empty strings, are always zero terminated. ^The return +** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer. +** +** ^The object returned by [sqlite3_column_value()] is an +** [unprotected sqlite3_value] object. An unprotected sqlite3_value object +** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()]. +** If the [unprotected sqlite3_value] object returned by +** [sqlite3_column_value()] is used in any other way, including calls +** to routines like [sqlite3_value_int()], [sqlite3_value_text()], +** or [sqlite3_value_bytes()], then the behavior is undefined. +** +** These routines attempt to convert the value where appropriate. ^For +** example, if the internal representation is FLOAT and a text result +** is requested, [sqlite3_snprintf()] is used internally to perform the +** conversion automatically. ^(The following table details the conversions +** that are applied: +** +**
    +**
    +**
    Internal
    Type
    Requested
    Type
    Conversion +** +**
    NULL INTEGER Result is 0 +**
    NULL FLOAT Result is 0.0 +**
    NULL TEXT Result is NULL pointer +**
    NULL BLOB Result is NULL pointer +**
    INTEGER FLOAT Convert from integer to float +**
    INTEGER TEXT ASCII rendering of the integer +**
    INTEGER BLOB Same as INTEGER->TEXT +**
    FLOAT INTEGER Convert from float to integer +**
    FLOAT TEXT ASCII rendering of the float +**
    FLOAT BLOB Same as FLOAT->TEXT +**
    TEXT INTEGER Use atoi() +**
    TEXT FLOAT Use atof() +**
    TEXT BLOB No change +**
    BLOB INTEGER Convert to TEXT then use atoi() +**
    BLOB FLOAT Convert to TEXT then use atof() +**
    BLOB TEXT Add a zero terminator if needed +**
    +** )^ +** +** The table above makes reference to standard C library functions atoi() +** and atof(). SQLite does not really use these functions. It has its +** own equivalent internal routines. The atoi() and atof() names are +** used in the table for brevity and because they are familiar to most +** C programmers. +** +** Note that when type conversions occur, pointers returned by prior +** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or +** sqlite3_column_text16() may be invalidated. +** Type conversions and pointer invalidations might occur +** in the following cases: +** +**
      +**
    • The initial content is a BLOB and sqlite3_column_text() or +** sqlite3_column_text16() is called. A zero-terminator might +** need to be added to the string.
    • +**
    • The initial content is UTF-8 text and sqlite3_column_bytes16() or +** sqlite3_column_text16() is called. The content must be converted +** to UTF-16.
    • +**
    • The initial content is UTF-16 text and sqlite3_column_bytes() or +** sqlite3_column_text() is called. The content must be converted +** to UTF-8.
    • +**
    +** +** ^Conversions between UTF-16be and UTF-16le are always done in place and do +** not invalidate a prior pointer, though of course the content of the buffer +** that the prior pointer references will have been modified. Other kinds +** of conversion are done in place when it is possible, but sometimes they +** are not possible and in those cases prior pointers are invalidated. +** +** The safest and easiest to remember policy is to invoke these routines +** in one of the following ways: +** +**
      +**
    • sqlite3_column_text() followed by sqlite3_column_bytes()
    • +**
    • sqlite3_column_blob() followed by sqlite3_column_bytes()
    • +**
    • sqlite3_column_text16() followed by sqlite3_column_bytes16()
    • +**
    +** +** In other words, you should call sqlite3_column_text(), +** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result +** into the desired format, then invoke sqlite3_column_bytes() or +** sqlite3_column_bytes16() to find the size of the result. Do not mix calls +** to sqlite3_column_text() or sqlite3_column_blob() with calls to +** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16() +** with calls to sqlite3_column_bytes(). +** +** ^The pointers returned are valid until a type conversion occurs as +** described above, or until [sqlite3_step()] or [sqlite3_reset()] or +** [sqlite3_finalize()] is called. ^The memory space used to hold strings +** and BLOBs is freed automatically. Do not pass the pointers returned +** [sqlite3_column_blob()], [sqlite3_column_text()], etc. into +** [sqlite3_free()]. +** +** ^(If a memory allocation error occurs during the evaluation of any +** of these routines, a default value is returned. The default value +** is either the integer 0, the floating point number 0.0, or a NULL +** pointer. Subsequent calls to [sqlite3_errcode()] will return +** [SQLITE_NOMEM].)^ +*/ +SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); +SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol); +SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); +SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol); +SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol); +SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); +SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); +SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); +SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol); +SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); + +/* +** CAPI3REF: Destroy A Prepared Statement Object +** +** ^The sqlite3_finalize() function is called to delete a [prepared statement]. +** ^If the most recent evaluation of the statement encountered no errors or +** or if the statement is never been evaluated, then sqlite3_finalize() returns +** SQLITE_OK. ^If the most recent evaluation of statement S failed, then +** sqlite3_finalize(S) returns the appropriate [error code] or +** [extended error code]. +** +** ^The sqlite3_finalize(S) routine can be called at any point during +** the life cycle of [prepared statement] S: +** before statement S is ever evaluated, after +** one or more calls to [sqlite3_reset()], or after any call +** to [sqlite3_step()] regardless of whether or not the statement has +** completed execution. +** +** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op. +** +** The application must finalize every [prepared statement] in order to avoid +** resource leaks. It is a grievous error for the application to try to use +** a prepared statement after it has been finalized. Any use of a prepared +** statement after it has been finalized can result in undefined and +** undesirable behavior such as segfaults and heap corruption. +*/ +SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Reset A Prepared Statement Object +** +** The sqlite3_reset() function is called to reset a [prepared statement] +** object back to its initial state, ready to be re-executed. +** ^Any SQL statement variables that had values bound to them using +** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values. +** Use [sqlite3_clear_bindings()] to reset the bindings. +** +** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S +** back to the beginning of its program. +** +** ^If the most recent call to [sqlite3_step(S)] for the +** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE], +** or if [sqlite3_step(S)] has never before been called on S, +** then [sqlite3_reset(S)] returns [SQLITE_OK]. +** +** ^If the most recent call to [sqlite3_step(S)] for the +** [prepared statement] S indicated an error, then +** [sqlite3_reset(S)] returns an appropriate [error code]. +** +** ^The [sqlite3_reset(S)] interface does not change the values +** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S. +*/ +SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Create Or Redefine SQL Functions +** KEYWORDS: {function creation routines} +** KEYWORDS: {application-defined SQL function} +** KEYWORDS: {application-defined SQL functions} +** +** ^These functions (collectively known as "function creation routines") +** are used to add SQL functions or aggregates or to redefine the behavior +** of existing SQL functions or aggregates. The only differences between +** these routines are the text encoding expected for +** the the second parameter (the name of the function being created) +** and the presence or absence of a destructor callback for +** the application data pointer. +** +** ^The first parameter is the [database connection] to which the SQL +** function is to be added. ^If an application uses more than one database +** connection then application-defined SQL functions must be added +** to each database connection separately. +** +** ^The second parameter is the name of the SQL function to be created or +** redefined. ^The length of the name is limited to 255 bytes in a UTF-8 +** representation, exclusive of the zero-terminator. ^Note that the name +** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes. +** ^Any attempt to create a function with a longer name +** will result in [SQLITE_MISUSE] being returned. +** +** ^The third parameter (nArg) +** is the number of arguments that the SQL function or +** aggregate takes. ^If this parameter is -1, then the SQL function or +** aggregate may take any number of arguments between 0 and the limit +** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]). If the third +** parameter is less than -1 or greater than 127 then the behavior is +** undefined. +** +** ^The fourth parameter, eTextRep, specifies what +** [SQLITE_UTF8 | text encoding] this SQL function prefers for +** its parameters. Every SQL function implementation must be able to work +** with UTF-8, UTF-16le, or UTF-16be. But some implementations may be +** more efficient with one encoding than another. ^An application may +** invoke sqlite3_create_function() or sqlite3_create_function16() multiple +** times with the same function but with different values of eTextRep. +** ^When multiple implementations of the same function are available, SQLite +** will pick the one that involves the least amount of data conversion. +** If there is only a single implementation which does not care what text +** encoding is used, then the fourth argument should be [SQLITE_ANY]. +** +** ^(The fifth parameter is an arbitrary pointer. The implementation of the +** function can gain access to this pointer using [sqlite3_user_data()].)^ +** +** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are +** pointers to C-language functions that implement the SQL function or +** aggregate. ^A scalar SQL function requires an implementation of the xFunc +** callback only; NULL pointers must be passed as the xStep and xFinal +** parameters. ^An aggregate SQL function requires an implementation of xStep +** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing +** SQL function or aggregate, pass NULL poiners for all three function +** callbacks. +** +** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL, +** then it is destructor for the application data pointer. +** The destructor is invoked when the function is deleted, either by being +** overloaded or when the database connection closes.)^ +** ^The destructor is also invoked if the call to +** sqlite3_create_function_v2() fails. +** ^When the destructor callback of the tenth parameter is invoked, it +** is passed a single argument which is a copy of the application data +** pointer which was the fifth parameter to sqlite3_create_function_v2(). +** +** ^It is permitted to register multiple implementations of the same +** functions with the same name but with either differing numbers of +** arguments or differing preferred text encodings. ^SQLite will use +** the implementation that most closely matches the way in which the +** SQL function is used. ^A function implementation with a non-negative +** nArg parameter is a better match than a function implementation with +** a negative nArg. ^A function where the preferred text encoding +** matches the database encoding is a better +** match than a function where the encoding is different. +** ^A function where the encoding difference is between UTF16le and UTF16be +** is a closer match than a function where the encoding difference is +** between UTF8 and UTF16. +** +** ^Built-in functions may be overloaded by new application-defined functions. +** +** ^An application-defined function is permitted to call other +** SQLite interfaces. However, such calls must not +** close the database connection nor finalize or reset the prepared +** statement in which the function is running. +*/ +SQLITE_API int sqlite3_create_function( + sqlite3 *db, + const char *zFunctionName, + int nArg, + int eTextRep, + void *pApp, + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*) +); +SQLITE_API int sqlite3_create_function16( + sqlite3 *db, + const void *zFunctionName, + int nArg, + int eTextRep, + void *pApp, + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*) +); +SQLITE_API int sqlite3_create_function_v2( + sqlite3 *db, + const char *zFunctionName, + int nArg, + int eTextRep, + void *pApp, + void (*xFunc)(sqlite3_context*,int,sqlite3_value**), + void (*xStep)(sqlite3_context*,int,sqlite3_value**), + void (*xFinal)(sqlite3_context*), + void(*xDestroy)(void*) +); + +/* +** CAPI3REF: Text Encodings +** +** These constant define integer codes that represent the various +** text encodings supported by SQLite. +*/ +#define SQLITE_UTF8 1 +#define SQLITE_UTF16LE 2 +#define SQLITE_UTF16BE 3 +#define SQLITE_UTF16 4 /* Use native byte order */ +#define SQLITE_ANY 5 /* sqlite3_create_function only */ +#define SQLITE_UTF16_ALIGNED 8 /* sqlite3_create_collation only */ + +/* +** CAPI3REF: Deprecated Functions +** DEPRECATED +** +** These functions are [deprecated]. In order to maintain +** backwards compatibility with older code, these functions continue +** to be supported. However, new applications should avoid +** the use of these functions. To help encourage people to avoid +** using these functions, we are not going to tell you what they do. +*/ +#ifndef SQLITE_OMIT_DEPRECATED +SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*); +SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*); +SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); +SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void); +SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void); +SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); +#endif + +/* +** CAPI3REF: Obtaining SQL Function Parameter Values +** +** The C-language implementation of SQL functions and aggregates uses +** this set of interface routines to access the parameter values on +** the function or aggregate. +** +** The xFunc (for scalar functions) or xStep (for aggregates) parameters +** to [sqlite3_create_function()] and [sqlite3_create_function16()] +** define callbacks that implement the SQL functions and aggregates. +** The 3rd parameter to these callbacks is an array of pointers to +** [protected sqlite3_value] objects. There is one [sqlite3_value] object for +** each parameter to the SQL function. These routines are used to +** extract values from the [sqlite3_value] objects. +** +** These routines work only with [protected sqlite3_value] objects. +** Any attempt to use these routines on an [unprotected sqlite3_value] +** object results in undefined behavior. +** +** ^These routines work just like the corresponding [column access functions] +** except that these routines take a single [protected sqlite3_value] object +** pointer instead of a [sqlite3_stmt*] pointer and an integer column number. +** +** ^The sqlite3_value_text16() interface extracts a UTF-16 string +** in the native byte-order of the host machine. ^The +** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces +** extract UTF-16 strings as big-endian and little-endian respectively. +** +** ^(The sqlite3_value_numeric_type() interface attempts to apply +** numeric affinity to the value. This means that an attempt is +** made to convert the value to an integer or floating point. If +** such a conversion is possible without loss of information (in other +** words, if the value is a string that looks like a number) +** then the conversion is performed. Otherwise no conversion occurs. +** The [SQLITE_INTEGER | datatype] after conversion is returned.)^ +** +** Please pay particular attention to the fact that the pointer returned +** from [sqlite3_value_blob()], [sqlite3_value_text()], or +** [sqlite3_value_text16()] can be invalidated by a subsequent call to +** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()], +** or [sqlite3_value_text16()]. +** +** These routines must be called from the same thread as +** the SQL function that supplied the [sqlite3_value*] parameters. +*/ +SQLITE_API const void *sqlite3_value_blob(sqlite3_value*); +SQLITE_API int sqlite3_value_bytes(sqlite3_value*); +SQLITE_API int sqlite3_value_bytes16(sqlite3_value*); +SQLITE_API double sqlite3_value_double(sqlite3_value*); +SQLITE_API int sqlite3_value_int(sqlite3_value*); +SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*); +SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*); +SQLITE_API const void *sqlite3_value_text16(sqlite3_value*); +SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*); +SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*); +SQLITE_API int sqlite3_value_type(sqlite3_value*); +SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*); + +/* +** CAPI3REF: Obtain Aggregate Function Context +** +** Implementations of aggregate SQL functions use this +** routine to allocate memory for storing their state. +** +** ^The first time the sqlite3_aggregate_context(C,N) routine is called +** for a particular aggregate function, SQLite +** allocates N of memory, zeroes out that memory, and returns a pointer +** to the new memory. ^On second and subsequent calls to +** sqlite3_aggregate_context() for the same aggregate function instance, +** the same buffer is returned. Sqlite3_aggregate_context() is normally +** called once for each invocation of the xStep callback and then one +** last time when the xFinal callback is invoked. ^(When no rows match +** an aggregate query, the xStep() callback of the aggregate function +** implementation is never called and xFinal() is called exactly once. +** In those cases, sqlite3_aggregate_context() might be called for the +** first time from within xFinal().)^ +** +** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer if N is +** less than or equal to zero or if a memory allocate error occurs. +** +** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is +** determined by the N parameter on first successful call. Changing the +** value of N in subsequent call to sqlite3_aggregate_context() within +** the same aggregate function instance will not resize the memory +** allocation.)^ +** +** ^SQLite automatically frees the memory allocated by +** sqlite3_aggregate_context() when the aggregate query concludes. +** +** The first parameter must be a copy of the +** [sqlite3_context | SQL function context] that is the first parameter +** to the xStep or xFinal callback routine that implements the aggregate +** function. +** +** This routine must be called from the same thread in which +** the aggregate SQL function is running. +*/ +SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); + +/* +** CAPI3REF: User Data For Functions +** +** ^The sqlite3_user_data() interface returns a copy of +** the pointer that was the pUserData parameter (the 5th parameter) +** of the [sqlite3_create_function()] +** and [sqlite3_create_function16()] routines that originally +** registered the application defined function. +** +** This routine must be called from the same thread in which +** the application-defined function is running. +*/ +SQLITE_API void *sqlite3_user_data(sqlite3_context*); + +/* +** CAPI3REF: Database Connection For Functions +** +** ^The sqlite3_context_db_handle() interface returns a copy of +** the pointer to the [database connection] (the 1st parameter) +** of the [sqlite3_create_function()] +** and [sqlite3_create_function16()] routines that originally +** registered the application defined function. +*/ +SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*); + +/* +** CAPI3REF: Function Auxiliary Data +** +** The following two functions may be used by scalar SQL functions to +** associate metadata with argument values. If the same value is passed to +** multiple invocations of the same SQL function during query execution, under +** some circumstances the associated metadata may be preserved. This may +** be used, for example, to add a regular-expression matching scalar +** function. The compiled version of the regular expression is stored as +** metadata associated with the SQL value passed as the regular expression +** pattern. The compiled regular expression can be reused on multiple +** invocations of the same function so that the original pattern string +** does not need to be recompiled on each invocation. +** +** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata +** associated by the sqlite3_set_auxdata() function with the Nth argument +** value to the application-defined function. ^If no metadata has been ever +** been set for the Nth argument of the function, or if the corresponding +** function parameter has changed since the meta-data was set, +** then sqlite3_get_auxdata() returns a NULL pointer. +** +** ^The sqlite3_set_auxdata() interface saves the metadata +** pointed to by its 3rd parameter as the metadata for the N-th +** argument of the application-defined function. Subsequent +** calls to sqlite3_get_auxdata() might return this data, if it has +** not been destroyed. +** ^If it is not NULL, SQLite will invoke the destructor +** function given by the 4th parameter to sqlite3_set_auxdata() on +** the metadata when the corresponding function parameter changes +** or when the SQL statement completes, whichever comes first. +** +** SQLite is free to call the destructor and drop metadata on any +** parameter of any function at any time. ^The only guarantee is that +** the destructor will be called before the metadata is dropped. +** +** ^(In practice, metadata is preserved between function calls for +** expressions that are constant at compile time. This includes literal +** values and [parameters].)^ +** +** These routines must be called from the same thread in which +** the SQL function is running. +*/ +SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N); +SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); + + +/* +** CAPI3REF: Constants Defining Special Destructor Behavior +** +** These are special values for the destructor that is passed in as the +** final argument to routines like [sqlite3_result_blob()]. ^If the destructor +** argument is SQLITE_STATIC, it means that the content pointer is constant +** and will never change. It does not need to be destroyed. ^The +** SQLITE_TRANSIENT value means that the content will likely change in +** the near future and that SQLite should make its own private copy of +** the content before returning. +** +** The typedef is necessary to work around problems in certain +** C++ compilers. See ticket #2191. +*/ +typedef void (*sqlite3_destructor_type)(void*); +#define SQLITE_STATIC ((sqlite3_destructor_type)0) +#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1) + +/* +** CAPI3REF: Setting The Result Of An SQL Function +** +** These routines are used by the xFunc or xFinal callbacks that +** implement SQL functions and aggregates. See +** [sqlite3_create_function()] and [sqlite3_create_function16()] +** for additional information. +** +** These functions work very much like the [parameter binding] family of +** functions used to bind values to host parameters in prepared statements. +** Refer to the [SQL parameter] documentation for additional information. +** +** ^The sqlite3_result_blob() interface sets the result from +** an application-defined function to be the BLOB whose content is pointed +** to by the second parameter and which is N bytes long where N is the +** third parameter. +** +** ^The sqlite3_result_zeroblob() interfaces set the result of +** the application-defined function to be a BLOB containing all zero +** bytes and N bytes in size, where N is the value of the 2nd parameter. +** +** ^The sqlite3_result_double() interface sets the result from +** an application-defined function to be a floating point value specified +** by its 2nd argument. +** +** ^The sqlite3_result_error() and sqlite3_result_error16() functions +** cause the implemented SQL function to throw an exception. +** ^SQLite uses the string pointed to by the +** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16() +** as the text of an error message. ^SQLite interprets the error +** message string from sqlite3_result_error() as UTF-8. ^SQLite +** interprets the string from sqlite3_result_error16() as UTF-16 in native +** byte order. ^If the third parameter to sqlite3_result_error() +** or sqlite3_result_error16() is negative then SQLite takes as the error +** message all text up through the first zero character. +** ^If the third parameter to sqlite3_result_error() or +** sqlite3_result_error16() is non-negative then SQLite takes that many +** bytes (not characters) from the 2nd parameter as the error message. +** ^The sqlite3_result_error() and sqlite3_result_error16() +** routines make a private copy of the error message text before +** they return. Hence, the calling function can deallocate or +** modify the text after they return without harm. +** ^The sqlite3_result_error_code() function changes the error code +** returned by SQLite as a result of an error in a function. ^By default, +** the error code is SQLITE_ERROR. ^A subsequent call to sqlite3_result_error() +** or sqlite3_result_error16() resets the error code to SQLITE_ERROR. +** +** ^The sqlite3_result_toobig() interface causes SQLite to throw an error +** indicating that a string or BLOB is too long to represent. +** +** ^The sqlite3_result_nomem() interface causes SQLite to throw an error +** indicating that a memory allocation failed. +** +** ^The sqlite3_result_int() interface sets the return value +** of the application-defined function to be the 32-bit signed integer +** value given in the 2nd argument. +** ^The sqlite3_result_int64() interface sets the return value +** of the application-defined function to be the 64-bit signed integer +** value given in the 2nd argument. +** +** ^The sqlite3_result_null() interface sets the return value +** of the application-defined function to be NULL. +** +** ^The sqlite3_result_text(), sqlite3_result_text16(), +** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces +** set the return value of the application-defined function to be +** a text string which is represented as UTF-8, UTF-16 native byte order, +** UTF-16 little endian, or UTF-16 big endian, respectively. +** ^SQLite takes the text result from the application from +** the 2nd parameter of the sqlite3_result_text* interfaces. +** ^If the 3rd parameter to the sqlite3_result_text* interfaces +** is negative, then SQLite takes result text from the 2nd parameter +** through the first zero character. +** ^If the 3rd parameter to the sqlite3_result_text* interfaces +** is non-negative, then as many bytes (not characters) of the text +** pointed to by the 2nd parameter are taken as the application-defined +** function result. +** ^If the 4th parameter to the sqlite3_result_text* interfaces +** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that +** function as the destructor on the text or BLOB result when it has +** finished using that result. +** ^If the 4th parameter to the sqlite3_result_text* interfaces or to +** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite +** assumes that the text or BLOB result is in constant space and does not +** copy the content of the parameter nor call a destructor on the content +** when it has finished using that result. +** ^If the 4th parameter to the sqlite3_result_text* interfaces +** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT +** then SQLite makes a copy of the result into space obtained from +** from [sqlite3_malloc()] before it returns. +** +** ^The sqlite3_result_value() interface sets the result of +** the application-defined function to be a copy the +** [unprotected sqlite3_value] object specified by the 2nd parameter. ^The +** sqlite3_result_value() interface makes a copy of the [sqlite3_value] +** so that the [sqlite3_value] specified in the parameter may change or +** be deallocated after sqlite3_result_value() returns without harm. +** ^A [protected sqlite3_value] object may always be used where an +** [unprotected sqlite3_value] object is required, so either +** kind of [sqlite3_value] object can be used with this interface. +** +** If these routines are called from within the different thread +** than the one containing the application-defined function that received +** the [sqlite3_context] pointer, the results are undefined. +*/ +SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); +SQLITE_API void sqlite3_result_double(sqlite3_context*, double); +SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int); +SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int); +SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*); +SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*); +SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int); +SQLITE_API void sqlite3_result_int(sqlite3_context*, int); +SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); +SQLITE_API void sqlite3_result_null(sqlite3_context*); +SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); +SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); +SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); +SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); +SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*); +SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n); + +/* +** CAPI3REF: Define New Collating Sequences +** +** ^These functions add, remove, or modify a [collation] associated +** with the [database connection] specified as the first argument. +** +** ^The name of the collation is a UTF-8 string +** for sqlite3_create_collation() and sqlite3_create_collation_v2() +** and a UTF-16 string in native byte order for sqlite3_create_collation16(). +** ^Collation names that compare equal according to [sqlite3_strnicmp()] are +** considered to be the same name. +** +** ^(The third argument (eTextRep) must be one of the constants: +**
      +**
    • [SQLITE_UTF8], +**
    • [SQLITE_UTF16LE], +**
    • [SQLITE_UTF16BE], +**
    • [SQLITE_UTF16], or +**
    • [SQLITE_UTF16_ALIGNED]. +**
    )^ +** ^The eTextRep argument determines the encoding of strings passed +** to the collating function callback, xCallback. +** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep +** force strings to be UTF16 with native byte order. +** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin +** on an even byte address. +** +** ^The fourth argument, pArg, is a application data pointer that is passed +** through as the first argument to the collating function callback. +** +** ^The fifth argument, xCallback, is a pointer to the collating function. +** ^Multiple collating functions can be registered using the same name but +** with different eTextRep parameters and SQLite will use whichever +** function requires the least amount of data transformation. +** ^If the xCallback argument is NULL then the collating function is +** deleted. ^When all collating functions having the same name are deleted, +** that collation is no longer usable. +** +** ^The collating function callback is invoked with a copy of the pArg +** application data pointer and with two strings in the encoding specified +** by the eTextRep argument. The collating function must return an +** integer that is negative, zero, or positive +** if the first string is less than, equal to, or greater than the second, +** respectively. A collating function must alway return the same answer +** given the same inputs. If two or more collating functions are registered +** to the same collation name (using different eTextRep values) then all +** must give an equivalent answer when invoked with equivalent strings. +** The collating function must obey the following properties for all +** strings A, B, and C: +** +**
      +**
    1. If A==B then B==A. +**
    2. If A==B and B==C then A==C. +**
    3. If A<B THEN B>A. +**
    4. If A<B and B<C then A<C. +**
    +** +** If a collating function fails any of the above constraints and that +** collating function is registered and used, then the behavior of SQLite +** is undefined. +** +** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation() +** with the addition that the xDestroy callback is invoked on pArg when +** the collating function is deleted. +** ^Collating functions are deleted when they are overridden by later +** calls to the collation creation functions or when the +** [database connection] is closed using [sqlite3_close()]. +** +** ^The xDestroy callback is not called if the +** sqlite3_create_collation_v2() function fails. Applications that invoke +** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should +** check the return code and dispose of the application data pointer +** themselves rather than expecting SQLite to deal with it for them. +** This is different from every other SQLite interface. The inconsistency +** is unfortunate but cannot be changed without breaking backwards +** compatibility. +** +** See also: [sqlite3_collation_needed()] and [sqlite3_collation_needed16()]. +*/ +SQLITE_API int sqlite3_create_collation( + sqlite3*, + const char *zName, + int eTextRep, + void *pArg, + int(*xCompare)(void*,int,const void*,int,const void*) +); +SQLITE_API int sqlite3_create_collation_v2( + sqlite3*, + const char *zName, + int eTextRep, + void *pArg, + int(*xCompare)(void*,int,const void*,int,const void*), + void(*xDestroy)(void*) +); +SQLITE_API int sqlite3_create_collation16( + sqlite3*, + const void *zName, + int eTextRep, + void *pArg, + int(*xCompare)(void*,int,const void*,int,const void*) +); + +/* +** CAPI3REF: Collation Needed Callbacks +** +** ^To avoid having to register all collation sequences before a database +** can be used, a single callback function may be registered with the +** [database connection] to be invoked whenever an undefined collation +** sequence is required. +** +** ^If the function is registered using the sqlite3_collation_needed() API, +** then it is passed the names of undefined collation sequences as strings +** encoded in UTF-8. ^If sqlite3_collation_needed16() is used, +** the names are passed as UTF-16 in machine native byte order. +** ^A call to either function replaces the existing collation-needed callback. +** +** ^(When the callback is invoked, the first argument passed is a copy +** of the second argument to sqlite3_collation_needed() or +** sqlite3_collation_needed16(). The second argument is the database +** connection. The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE], +** or [SQLITE_UTF16LE], indicating the most desirable form of the collation +** sequence function required. The fourth parameter is the name of the +** required collation sequence.)^ +** +** The callback function should register the desired collation using +** [sqlite3_create_collation()], [sqlite3_create_collation16()], or +** [sqlite3_create_collation_v2()]. +*/ +SQLITE_API int sqlite3_collation_needed( + sqlite3*, + void*, + void(*)(void*,sqlite3*,int eTextRep,const char*) +); +SQLITE_API int sqlite3_collation_needed16( + sqlite3*, + void*, + void(*)(void*,sqlite3*,int eTextRep,const void*) +); + +typedef struct Btree Btree; + +/* +** The type used to represent a page number. The first page in a file +** is called page 1. 0 is used to represent "not a page". +*/ +typedef unsigned int Pgno; + +#ifdef SQLITE_HAS_CODEC +SQLITE_API void sqlite3BtreePagerSetCodec( + Btree *bt, + void *(*xCodec)(void*,void*,Pgno,int), + void (*xCodecSizeChng)(void*,int,int), + void (*xCodecFree)(void*), + void *pCodec +); + +#if !defined(SQLITE_HAS_CODEC_NO_ENCRYPTION) +/* +** Specify the key for an encrypted database. This routine should be +** called right after sqlite3_open(). +** +** The code to implement this API is not available in the public release +** of SQLite. +*/ +SQLITE_API int sqlite3_key( + sqlite3 *db, /* Database to be rekeyed */ + const void *pKey, int nKey /* The key */ +); + +/* +** Change the key on an open database. If the current database is not +** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the +** database is decrypted. +** +** The code to implement this API is not available in the public release +** of SQLite. +*/ +SQLITE_API int sqlite3_rekey( + sqlite3 *db, /* Database to be rekeyed */ + const void *pKey, int nKey /* The new key */ +); + +/* +** Specify the activation key for a SEE database. Unless +** activated, none of the SEE routines will work. +*/ +SQLITE_API void sqlite3_activate_see( + const char *zPassPhrase /* Activation phrase */ +); +#endif +#endif + +#ifdef SQLITE_ENABLE_CEROD +/* +** Specify the activation key for a CEROD database. Unless +** activated, none of the CEROD routines will work. +*/ +SQLITE_API void sqlite3_activate_cerod( + const char *zPassPhrase /* Activation phrase */ +); +#endif + +/* +** CAPI3REF: Suspend Execution For A Short Time +** +** The sqlite3_sleep() function causes the current thread to suspend execution +** for at least a number of milliseconds specified in its parameter. +** +** If the operating system does not support sleep requests with +** millisecond time resolution, then the time will be rounded up to +** the nearest second. The number of milliseconds of sleep actually +** requested from the operating system is returned. +** +** ^SQLite implements this interface by calling the xSleep() +** method of the default [sqlite3_vfs] object. If the xSleep() method +** of the default VFS is not implemented correctly, or not implemented at +** all, then the behavior of sqlite3_sleep() may deviate from the description +** in the previous paragraphs. +*/ +SQLITE_API int sqlite3_sleep(int); + +/* +** CAPI3REF: Name Of The Folder Holding Temporary Files +** +** ^(If this global variable is made to point to a string which is +** the name of a folder (a.k.a. directory), then all temporary files +** created by SQLite when using a built-in [sqlite3_vfs | VFS] +** will be placed in that directory.)^ ^If this variable +** is a NULL pointer, then SQLite performs a search for an appropriate +** temporary file directory. +** +** It is not safe to read or modify this variable in more than one +** thread at a time. It is not safe to read or modify this variable +** if a [database connection] is being used at the same time in a separate +** thread. +** It is intended that this variable be set once +** as part of process initialization and before any SQLite interface +** routines have been called and that this variable remain unchanged +** thereafter. +** +** ^The [temp_store_directory pragma] may modify this variable and cause +** it to point to memory obtained from [sqlite3_malloc]. ^Furthermore, +** the [temp_store_directory pragma] always assumes that any string +** that this variable points to is held in memory obtained from +** [sqlite3_malloc] and the pragma may attempt to free that memory +** using [sqlite3_free]. +** Hence, if this variable is modified directly, either it should be +** made NULL or made to point to memory obtained from [sqlite3_malloc] +** or else the use of the [temp_store_directory pragma] should be avoided. +*/ +SQLITE_API SQLITE_EXTERN char *sqlite3_temp_directory; + +/* +** CAPI3REF: Test For Auto-Commit Mode +** KEYWORDS: {autocommit mode} +** +** ^The sqlite3_get_autocommit() interface returns non-zero or +** zero if the given database connection is or is not in autocommit mode, +** respectively. ^Autocommit mode is on by default. +** ^Autocommit mode is disabled by a [BEGIN] statement. +** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK]. +** +** If certain kinds of errors occur on a statement within a multi-statement +** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR], +** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the +** transaction might be rolled back automatically. The only way to +** find out whether SQLite automatically rolled back the transaction after +** an error is to use this function. +** +** If another thread changes the autocommit status of the database +** connection while this routine is running, then the return value +** is undefined. +*/ +SQLITE_API int sqlite3_get_autocommit(sqlite3*); + +/* +** CAPI3REF: Find The Database Handle Of A Prepared Statement +** +** ^The sqlite3_db_handle interface returns the [database connection] handle +** to which a [prepared statement] belongs. ^The [database connection] +** returned by sqlite3_db_handle is the same [database connection] +** that was the first argument +** to the [sqlite3_prepare_v2()] call (or its variants) that was used to +** create the statement in the first place. +*/ +SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*); + +/* +** CAPI3REF: Find the next prepared statement +** +** ^This interface returns a pointer to the next [prepared statement] after +** pStmt associated with the [database connection] pDb. ^If pStmt is NULL +** then this interface returns a pointer to the first prepared statement +** associated with the database connection pDb. ^If no prepared statement +** satisfies the conditions of this routine, it returns NULL. +** +** The [database connection] pointer D in a call to +** [sqlite3_next_stmt(D,S)] must refer to an open database +** connection and in particular must not be a NULL pointer. +*/ +SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt); + +/* +** CAPI3REF: Commit And Rollback Notification Callbacks +** +** ^The sqlite3_commit_hook() interface registers a callback +** function to be invoked whenever a transaction is [COMMIT | committed]. +** ^Any callback set by a previous call to sqlite3_commit_hook() +** for the same database connection is overridden. +** ^The sqlite3_rollback_hook() interface registers a callback +** function to be invoked whenever a transaction is [ROLLBACK | rolled back]. +** ^Any callback set by a previous call to sqlite3_rollback_hook() +** for the same database connection is overridden. +** ^The pArg argument is passed through to the callback. +** ^If the callback on a commit hook function returns non-zero, +** then the commit is converted into a rollback. +** +** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions +** return the P argument from the previous call of the same function +** on the same [database connection] D, or NULL for +** the first call for each function on D. +** +** The callback implementation must not do anything that will modify +** the database connection that invoked the callback. Any actions +** to modify the database connection must be deferred until after the +** completion of the [sqlite3_step()] call that triggered the commit +** or rollback hook in the first place. +** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their +** database connections for the meaning of "modify" in this paragraph. +** +** ^Registering a NULL function disables the callback. +** +** ^When the commit hook callback routine returns zero, the [COMMIT] +** operation is allowed to continue normally. ^If the commit hook +** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK]. +** ^The rollback hook is invoked on a rollback that results from a commit +** hook returning non-zero, just as it would be with any other rollback. +** +** ^For the purposes of this API, a transaction is said to have been +** rolled back if an explicit "ROLLBACK" statement is executed, or +** an error or constraint causes an implicit rollback to occur. +** ^The rollback callback is not invoked if a transaction is +** automatically rolled back because the database connection is closed. +** +** See also the [sqlite3_update_hook()] interface. +*/ +SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); +SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); + +/* +** CAPI3REF: Data Change Notification Callbacks +** +** ^The sqlite3_update_hook() interface registers a callback function +** with the [database connection] identified by the first argument +** to be invoked whenever a row is updated, inserted or deleted. +** ^Any callback set by a previous call to this function +** for the same database connection is overridden. +** +** ^The second argument is a pointer to the function to invoke when a +** row is updated, inserted or deleted. +** ^The first argument to the callback is a copy of the third argument +** to sqlite3_update_hook(). +** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE], +** or [SQLITE_UPDATE], depending on the operation that caused the callback +** to be invoked. +** ^The third and fourth arguments to the callback contain pointers to the +** database and table name containing the affected row. +** ^The final callback parameter is the [rowid] of the row. +** ^In the case of an update, this is the [rowid] after the update takes place. +** +** ^(The update hook is not invoked when internal system tables are +** modified (i.e. sqlite_master and sqlite_sequence).)^ +** +** ^In the current implementation, the update hook +** is not invoked when duplication rows are deleted because of an +** [ON CONFLICT | ON CONFLICT REPLACE] clause. ^Nor is the update hook +** invoked when rows are deleted using the [truncate optimization]. +** The exceptions defined in this paragraph might change in a future +** release of SQLite. +** +** The update hook implementation must not do anything that will modify +** the database connection that invoked the update hook. Any actions +** to modify the database connection must be deferred until after the +** completion of the [sqlite3_step()] call that triggered the update hook. +** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their +** database connections for the meaning of "modify" in this paragraph. +** +** ^The sqlite3_update_hook(D,C,P) function +** returns the P argument from the previous call +** on the same [database connection] D, or NULL for +** the first call on D. +** +** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()] +** interfaces. +*/ +SQLITE_API void *sqlite3_update_hook( + sqlite3*, + void(*)(void *,int ,char const *,char const *,sqlite3_int64), + void* +); + +/* +** CAPI3REF: Enable Or Disable Shared Pager Cache +** KEYWORDS: {shared cache} +** +** ^(This routine enables or disables the sharing of the database cache +** and schema data structures between [database connection | connections] +** to the same database. Sharing is enabled if the argument is true +** and disabled if the argument is false.)^ +** +** ^Cache sharing is enabled and disabled for an entire process. +** This is a change as of SQLite version 3.5.0. In prior versions of SQLite, +** sharing was enabled or disabled for each thread separately. +** +** ^(The cache sharing mode set by this interface effects all subsequent +** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()]. +** Existing database connections continue use the sharing mode +** that was in effect at the time they were opened.)^ +** +** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled +** successfully. An [error code] is returned otherwise.)^ +** +** ^Shared cache is disabled by default. But this might change in +** future releases of SQLite. Applications that care about shared +** cache setting should set it explicitly. +** +** See Also: [SQLite Shared-Cache Mode] +*/ +SQLITE_API int sqlite3_enable_shared_cache(int); + +/* +** CAPI3REF: Attempt To Free Heap Memory +** +** ^The sqlite3_release_memory() interface attempts to free N bytes +** of heap memory by deallocating non-essential memory allocations +** held by the database library. Memory used to cache database +** pages to improve performance is an example of non-essential memory. +** ^sqlite3_release_memory() returns the number of bytes actually freed, +** which might be more or less than the amount requested. +** ^The sqlite3_release_memory() routine is a no-op returning zero +** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT]. +*/ +SQLITE_API int sqlite3_release_memory(int); + +/* +** CAPI3REF: Impose A Limit On Heap Size +** +** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the +** soft limit on the amount of heap memory that may be allocated by SQLite. +** ^SQLite strives to keep heap memory utilization below the soft heap +** limit by reducing the number of pages held in the page cache +** as heap memory usages approaches the limit. +** ^The soft heap limit is "soft" because even though SQLite strives to stay +** below the limit, it will exceed the limit rather than generate +** an [SQLITE_NOMEM] error. In other words, the soft heap limit +** is advisory only. +** +** ^The return value from sqlite3_soft_heap_limit64() is the size of +** the soft heap limit prior to the call. ^If the argument N is negative +** then no change is made to the soft heap limit. Hence, the current +** size of the soft heap limit can be determined by invoking +** sqlite3_soft_heap_limit64() with a negative argument. +** +** ^If the argument N is zero then the soft heap limit is disabled. +** +** ^(The soft heap limit is not enforced in the current implementation +** if one or more of following conditions are true: +** +**
      +**
    • The soft heap limit is set to zero. +**
    • Memory accounting is disabled using a combination of the +** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and +** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option. +**
    • An alternative page cache implementation is specifed using +** [sqlite3_config]([SQLITE_CONFIG_PCACHE],...). +**
    • The page cache allocates from its own memory pool supplied +** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than +** from the heap. +**
    )^ +** +** Beginning with SQLite version 3.7.3, the soft heap limit is enforced +** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT] +** compile-time option is invoked. With [SQLITE_ENABLE_MEMORY_MANAGEMENT], +** the soft heap limit is enforced on every memory allocation. Without +** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced +** when memory is allocated by the page cache. Testing suggests that because +** the page cache is the predominate memory user in SQLite, most +** applications will achieve adequate soft heap limit enforcement without +** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT]. +** +** The circumstances under which SQLite will enforce the soft heap limit may +** changes in future releases of SQLite. +*/ +SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N); + +/* +** CAPI3REF: Deprecated Soft Heap Limit Interface +** DEPRECATED +** +** This is a deprecated version of the [sqlite3_soft_heap_limit64()] +** interface. This routine is provided for historical compatibility +** only. All new applications should use the +** [sqlite3_soft_heap_limit64()] interface rather than this one. +*/ +SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N); + + +/* +** CAPI3REF: Extract Metadata About A Column Of A Table +** +** ^This routine returns metadata about a specific column of a specific +** database table accessible using the [database connection] handle +** passed as the first function argument. +** +** ^The column is identified by the second, third and fourth parameters to +** this function. ^The second parameter is either the name of the database +** (i.e. "main", "temp", or an attached database) containing the specified +** table or NULL. ^If it is NULL, then all attached databases are searched +** for the table using the same algorithm used by the database engine to +** resolve unqualified table references. +** +** ^The third and fourth parameters to this function are the table and column +** name of the desired column, respectively. Neither of these parameters +** may be NULL. +** +** ^Metadata is returned by writing to the memory locations passed as the 5th +** and subsequent parameters to this function. ^Any of these arguments may be +** NULL, in which case the corresponding element of metadata is omitted. +** +** ^(
    +** +**
    Parameter Output
    Type
    Description +** +**
    5th const char* Data type +**
    6th const char* Name of default collation sequence +**
    7th int True if column has a NOT NULL constraint +**
    8th int True if column is part of the PRIMARY KEY +**
    9th int True if column is [AUTOINCREMENT] +**
    +**
    )^ +** +** ^The memory pointed to by the character pointers returned for the +** declaration type and collation sequence is valid only until the next +** call to any SQLite API function. +** +** ^If the specified table is actually a view, an [error code] is returned. +** +** ^If the specified column is "rowid", "oid" or "_rowid_" and an +** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output +** parameters are set for the explicitly declared column. ^(If there is no +** explicitly declared [INTEGER PRIMARY KEY] column, then the output +** parameters are set as follows: +** +**
    +**     data type: "INTEGER"
    +**     collation sequence: "BINARY"
    +**     not null: 0
    +**     primary key: 1
    +**     auto increment: 0
    +** 
    )^ +** +** ^(This function may load one or more schemas from database files. If an +** error occurs during this process, or if the requested table or column +** cannot be found, an [error code] is returned and an error message left +** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^ +** +** ^This API is only available if the library was compiled with the +** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined. +*/ +SQLITE_API int sqlite3_table_column_metadata( + sqlite3 *db, /* Connection handle */ + const char *zDbName, /* Database name or NULL */ + const char *zTableName, /* Table name */ + const char *zColumnName, /* Column name */ + char const **pzDataType, /* OUTPUT: Declared data type */ + char const **pzCollSeq, /* OUTPUT: Collation sequence name */ + int *pNotNull, /* OUTPUT: True if NOT NULL constraint exists */ + int *pPrimaryKey, /* OUTPUT: True if column part of PK */ + int *pAutoinc /* OUTPUT: True if column is auto-increment */ +); + +/* +** CAPI3REF: Load An Extension +** +** ^This interface loads an SQLite extension library from the named file. +** +** ^The sqlite3_load_extension() interface attempts to load an +** SQLite extension library contained in the file zFile. +** +** ^The entry point is zProc. +** ^zProc may be 0, in which case the name of the entry point +** defaults to "sqlite3_extension_init". +** ^The sqlite3_load_extension() interface returns +** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong. +** ^If an error occurs and pzErrMsg is not 0, then the +** [sqlite3_load_extension()] interface shall attempt to +** fill *pzErrMsg with error message text stored in memory +** obtained from [sqlite3_malloc()]. The calling function +** should free this memory by calling [sqlite3_free()]. +** +** ^Extension loading must be enabled using +** [sqlite3_enable_load_extension()] prior to calling this API, +** otherwise an error will be returned. +** +** See also the [load_extension() SQL function]. +*/ +SQLITE_API int sqlite3_load_extension( + sqlite3 *db, /* Load the extension into this database connection */ + const char *zFile, /* Name of the shared library containing extension */ + const char *zProc, /* Entry point. Derived from zFile if 0 */ + char **pzErrMsg /* Put error message here if not 0 */ +); + +/* +** CAPI3REF: Enable Or Disable Extension Loading +** +** ^So as not to open security holes in older applications that are +** unprepared to deal with extension loading, and as a means of disabling +** extension loading while evaluating user-entered SQL, the following API +** is provided to turn the [sqlite3_load_extension()] mechanism on and off. +** +** ^Extension loading is off by default. See ticket #1863. +** ^Call the sqlite3_enable_load_extension() routine with onoff==1 +** to turn extension loading on and call it with onoff==0 to turn +** it back off again. +*/ +SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff); + +/* +** CAPI3REF: Automatically Load Statically Linked Extensions +** +** ^This interface causes the xEntryPoint() function to be invoked for +** each new [database connection] that is created. The idea here is that +** xEntryPoint() is the entry point for a statically linked SQLite extension +** that is to be automatically loaded into all new database connections. +** +** ^(Even though the function prototype shows that xEntryPoint() takes +** no arguments and returns void, SQLite invokes xEntryPoint() with three +** arguments and expects and integer result as if the signature of the +** entry point where as follows: +** +**
    +**    int xEntryPoint(
    +**      sqlite3 *db,
    +**      const char **pzErrMsg,
    +**      const struct sqlite3_api_routines *pThunk
    +**    );
    +** 
    )^ +** +** If the xEntryPoint routine encounters an error, it should make *pzErrMsg +** point to an appropriate error message (obtained from [sqlite3_mprintf()]) +** and return an appropriate [error code]. ^SQLite ensures that *pzErrMsg +** is NULL before calling the xEntryPoint(). ^SQLite will invoke +** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns. ^If any +** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()], +** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail. +** +** ^Calling sqlite3_auto_extension(X) with an entry point X that is already +** on the list of automatic extensions is a harmless no-op. ^No entry point +** will be called more than once for each database connection that is opened. +** +** See also: [sqlite3_reset_auto_extension()]. +*/ +SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void)); + +/* +** CAPI3REF: Reset Automatic Extension Loading +** +** ^This interface disables all automatic extensions previously +** registered using [sqlite3_auto_extension()]. +*/ +SQLITE_API void sqlite3_reset_auto_extension(void); + +/* +** The interface to the virtual-table mechanism is currently considered +** to be experimental. The interface might change in incompatible ways. +** If this is a problem for you, do not use the interface at this time. +** +** When the virtual-table mechanism stabilizes, we will declare the +** interface fixed, support it indefinitely, and remove this comment. +*/ + +/* +** Structures used by the virtual table interface +*/ +typedef struct sqlite3_vtab sqlite3_vtab; +typedef struct sqlite3_index_info sqlite3_index_info; +typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; +typedef struct sqlite3_module sqlite3_module; + +/* +** CAPI3REF: Virtual Table Object +** KEYWORDS: sqlite3_module {virtual table module} +** +** This structure, sometimes called a a "virtual table module", +** defines the implementation of a [virtual tables]. +** This structure consists mostly of methods for the module. +** +** ^A virtual table module is created by filling in a persistent +** instance of this structure and passing a pointer to that instance +** to [sqlite3_create_module()] or [sqlite3_create_module_v2()]. +** ^The registration remains valid until it is replaced by a different +** module or until the [database connection] closes. The content +** of this structure must not change while it is registered with +** any database connection. +*/ +struct sqlite3_module { + int iVersion; + int (*xCreate)(sqlite3*, void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVTab, char**); + int (*xConnect)(sqlite3*, void *pAux, + int argc, const char *const*argv, + sqlite3_vtab **ppVTab, char**); + int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); + int (*xDisconnect)(sqlite3_vtab *pVTab); + int (*xDestroy)(sqlite3_vtab *pVTab); + int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); + int (*xClose)(sqlite3_vtab_cursor*); + int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, + int argc, sqlite3_value **argv); + int (*xNext)(sqlite3_vtab_cursor*); + int (*xEof)(sqlite3_vtab_cursor*); + int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); + int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); + int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); + int (*xBegin)(sqlite3_vtab *pVTab); + int (*xSync)(sqlite3_vtab *pVTab); + int (*xCommit)(sqlite3_vtab *pVTab); + int (*xRollback)(sqlite3_vtab *pVTab); + int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, + void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), + void **ppArg); + int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); +}; + +/* +** CAPI3REF: Virtual Table Indexing Information +** KEYWORDS: sqlite3_index_info +** +** The sqlite3_index_info structure and its substructures is used as part +** of the [virtual table] interface to +** pass information into and receive the reply from the [xBestIndex] +** method of a [virtual table module]. The fields under **Inputs** are the +** inputs to xBestIndex and are read-only. xBestIndex inserts its +** results into the **Outputs** fields. +** +** ^(The aConstraint[] array records WHERE clause constraints of the form: +** +**
    column OP expr
    +** +** where OP is =, <, <=, >, or >=.)^ ^(The particular operator is +** stored in aConstraint[].op using one of the +** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^ +** ^(The index of the column is stored in +** aConstraint[].iColumn.)^ ^(aConstraint[].usable is TRUE if the +** expr on the right-hand side can be evaluated (and thus the constraint +** is usable) and false if it cannot.)^ +** +** ^The optimizer automatically inverts terms of the form "expr OP column" +** and makes other simplifications to the WHERE clause in an attempt to +** get as many WHERE clause terms into the form shown above as possible. +** ^The aConstraint[] array only reports WHERE clause terms that are +** relevant to the particular virtual table being queried. +** +** ^Information about the ORDER BY clause is stored in aOrderBy[]. +** ^Each term of aOrderBy records a column of the ORDER BY clause. +** +** The [xBestIndex] method must fill aConstraintUsage[] with information +** about what parameters to pass to xFilter. ^If argvIndex>0 then +** the right-hand side of the corresponding aConstraint[] is evaluated +** and becomes the argvIndex-th entry in argv. ^(If aConstraintUsage[].omit +** is true, then the constraint is assumed to be fully handled by the +** virtual table and is not checked again by SQLite.)^ +** +** ^The idxNum and idxPtr values are recorded and passed into the +** [xFilter] method. +** ^[sqlite3_free()] is used to free idxPtr if and only if +** needToFreeIdxPtr is true. +** +** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in +** the correct order to satisfy the ORDER BY clause so that no separate +** sorting step is required. +** +** ^The estimatedCost value is an estimate of the cost of doing the +** particular lookup. A full scan of a table with N entries should have +** a cost of N. A binary search of a table of N entries should have a +** cost of approximately log(N). +*/ +struct sqlite3_index_info { + /* Inputs */ + int nConstraint; /* Number of entries in aConstraint */ + struct sqlite3_index_constraint { + int iColumn; /* Column on left-hand side of constraint */ + unsigned char op; /* Constraint operator */ + unsigned char usable; /* True if this constraint is usable */ + int iTermOffset; /* Used internally - xBestIndex should ignore */ + } *aConstraint; /* Table of WHERE clause constraints */ + int nOrderBy; /* Number of terms in the ORDER BY clause */ + struct sqlite3_index_orderby { + int iColumn; /* Column number */ + unsigned char desc; /* True for DESC. False for ASC. */ + } *aOrderBy; /* The ORDER BY clause */ + /* Outputs */ + struct sqlite3_index_constraint_usage { + int argvIndex; /* if >0, constraint is part of argv to xFilter */ + unsigned char omit; /* Do not code a test for this constraint */ + } *aConstraintUsage; + int idxNum; /* Number used to identify the index */ + char *idxStr; /* String, possibly obtained from sqlite3_malloc */ + int needToFreeIdxStr; /* Free idxStr using sqlite3_free() if true */ + int orderByConsumed; /* True if output is already ordered */ + double estimatedCost; /* Estimated cost of using this index */ +}; + +/* +** CAPI3REF: Virtual Table Constraint Operator Codes +** +** These macros defined the allowed values for the +** [sqlite3_index_info].aConstraint[].op field. Each value represents +** an operator that is part of a constraint term in the wHERE clause of +** a query that uses a [virtual table]. +*/ +#define SQLITE_INDEX_CONSTRAINT_EQ 2 +#define SQLITE_INDEX_CONSTRAINT_GT 4 +#define SQLITE_INDEX_CONSTRAINT_LE 8 +#define SQLITE_INDEX_CONSTRAINT_LT 16 +#define SQLITE_INDEX_CONSTRAINT_GE 32 +#define SQLITE_INDEX_CONSTRAINT_MATCH 64 + +/* +** CAPI3REF: Register A Virtual Table Implementation +** +** ^These routines are used to register a new [virtual table module] name. +** ^Module names must be registered before +** creating a new [virtual table] using the module and before using a +** preexisting [virtual table] for the module. +** +** ^The module name is registered on the [database connection] specified +** by the first parameter. ^The name of the module is given by the +** second parameter. ^The third parameter is a pointer to +** the implementation of the [virtual table module]. ^The fourth +** parameter is an arbitrary client data pointer that is passed through +** into the [xCreate] and [xConnect] methods of the virtual table module +** when a new virtual table is be being created or reinitialized. +** +** ^The sqlite3_create_module_v2() interface has a fifth parameter which +** is a pointer to a destructor for the pClientData. ^SQLite will +** invoke the destructor function (if it is not NULL) when SQLite +** no longer needs the pClientData pointer. ^The destructor will also +** be invoked if the call to sqlite3_create_module_v2() fails. +** ^The sqlite3_create_module() +** interface is equivalent to sqlite3_create_module_v2() with a NULL +** destructor. +*/ +SQLITE_API int sqlite3_create_module( + sqlite3 *db, /* SQLite connection to register module with */ + const char *zName, /* Name of the module */ + const sqlite3_module *p, /* Methods for the module */ + void *pClientData /* Client data for xCreate/xConnect */ +); +SQLITE_API int sqlite3_create_module_v2( + sqlite3 *db, /* SQLite connection to register module with */ + const char *zName, /* Name of the module */ + const sqlite3_module *p, /* Methods for the module */ + void *pClientData, /* Client data for xCreate/xConnect */ + void(*xDestroy)(void*) /* Module destructor function */ +); + +/* +** CAPI3REF: Virtual Table Instance Object +** KEYWORDS: sqlite3_vtab +** +** Every [virtual table module] implementation uses a subclass +** of this object to describe a particular instance +** of the [virtual table]. Each subclass will +** be tailored to the specific needs of the module implementation. +** The purpose of this superclass is to define certain fields that are +** common to all module implementations. +** +** ^Virtual tables methods can set an error message by assigning a +** string obtained from [sqlite3_mprintf()] to zErrMsg. The method should +** take care that any prior string is freed by a call to [sqlite3_free()] +** prior to assigning a new string to zErrMsg. ^After the error message +** is delivered up to the client application, the string will be automatically +** freed by sqlite3_free() and the zErrMsg field will be zeroed. +*/ +struct sqlite3_vtab { + const sqlite3_module *pModule; /* The module for this virtual table */ + int nRef; /* NO LONGER USED */ + char *zErrMsg; /* Error message from sqlite3_mprintf() */ + /* Virtual table implementations will typically add additional fields */ +}; + +/* +** CAPI3REF: Virtual Table Cursor Object +** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor} +** +** Every [virtual table module] implementation uses a subclass of the +** following structure to describe cursors that point into the +** [virtual table] and are used +** to loop through the virtual table. Cursors are created using the +** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed +** by the [sqlite3_module.xClose | xClose] method. Cursors are used +** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods +** of the module. Each module implementation will define +** the content of a cursor structure to suit its own needs. +** +** This superclass exists in order to define fields of the cursor that +** are common to all implementations. +*/ +struct sqlite3_vtab_cursor { + sqlite3_vtab *pVtab; /* Virtual table of this cursor */ + /* Virtual table implementations will typically add additional fields */ +}; + +/* +** CAPI3REF: Declare The Schema Of A Virtual Table +** +** ^The [xCreate] and [xConnect] methods of a +** [virtual table module] call this interface +** to declare the format (the names and datatypes of the columns) of +** the virtual tables they implement. +*/ +SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL); + +/* +** CAPI3REF: Overload A Function For A Virtual Table +** +** ^(Virtual tables can provide alternative implementations of functions +** using the [xFindFunction] method of the [virtual table module]. +** But global versions of those functions +** must exist in order to be overloaded.)^ +** +** ^(This API makes sure a global version of a function with a particular +** name and number of parameters exists. If no such function exists +** before this API is called, a new function is created.)^ ^The implementation +** of the new function always causes an exception to be thrown. So +** the new function is not good for anything by itself. Its only +** purpose is to be a placeholder function that can be overloaded +** by a [virtual table]. +*/ +SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); + +/* +** The interface to the virtual-table mechanism defined above (back up +** to a comment remarkably similar to this one) is currently considered +** to be experimental. The interface might change in incompatible ways. +** If this is a problem for you, do not use the interface at this time. +** +** When the virtual-table mechanism stabilizes, we will declare the +** interface fixed, support it indefinitely, and remove this comment. +*/ + +/* +** CAPI3REF: A Handle To An Open BLOB +** KEYWORDS: {BLOB handle} {BLOB handles} +** +** An instance of this object represents an open BLOB on which +** [sqlite3_blob_open | incremental BLOB I/O] can be performed. +** ^Objects of this type are created by [sqlite3_blob_open()] +** and destroyed by [sqlite3_blob_close()]. +** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces +** can be used to read or write small subsections of the BLOB. +** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes. +*/ +typedef struct sqlite3_blob sqlite3_blob; + +/* +** CAPI3REF: Open A BLOB For Incremental I/O +** +** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located +** in row iRow, column zColumn, table zTable in database zDb; +** in other words, the same BLOB that would be selected by: +** +**
    +**     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
    +** 
    )^ +** +** ^If the flags parameter is non-zero, then the BLOB is opened for read +** and write access. ^If it is zero, the BLOB is opened for read access. +** ^It is not possible to open a column that is part of an index or primary +** key for writing. ^If [foreign key constraints] are enabled, it is +** not possible to open a column that is part of a [child key] for writing. +** +** ^Note that the database name is not the filename that contains +** the database but rather the symbolic name of the database that +** appears after the AS keyword when the database is connected using [ATTACH]. +** ^For the main database file, the database name is "main". +** ^For TEMP tables, the database name is "temp". +** +** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written +** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set +** to be a null pointer.)^ +** ^This function sets the [database connection] error code and message +** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related +** functions. ^Note that the *ppBlob variable is always initialized in a +** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob +** regardless of the success or failure of this routine. +** +** ^(If the row that a BLOB handle points to is modified by an +** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects +** then the BLOB handle is marked as "expired". +** This is true if any column of the row is changed, even a column +** other than the one the BLOB handle is open on.)^ +** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for +** a expired BLOB handle fail with an return code of [SQLITE_ABORT]. +** ^(Changes written into a BLOB prior to the BLOB expiring are not +** rolled back by the expiration of the BLOB. Such changes will eventually +** commit if the transaction continues to completion.)^ +** +** ^Use the [sqlite3_blob_bytes()] interface to determine the size of +** the opened blob. ^The size of a blob may not be changed by this +** interface. Use the [UPDATE] SQL command to change the size of a +** blob. +** +** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces +** and the built-in [zeroblob] SQL function can be used, if desired, +** to create an empty, zero-filled blob in which to read or write using +** this interface. +** +** To avoid a resource leak, every open [BLOB handle] should eventually +** be released by a call to [sqlite3_blob_close()]. +*/ +SQLITE_API int sqlite3_blob_open( + sqlite3*, + const char *zDb, + const char *zTable, + const char *zColumn, + sqlite3_int64 iRow, + int flags, + sqlite3_blob **ppBlob +); + +/* +** CAPI3REF: Move a BLOB Handle to a New Row +** +** ^This function is used to move an existing blob handle so that it points +** to a different row of the same database table. ^The new row is identified +** by the rowid value passed as the second argument. Only the row can be +** changed. ^The database, table and column on which the blob handle is open +** remain the same. Moving an existing blob handle to a new row can be +** faster than closing the existing handle and opening a new one. +** +** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] - +** it must exist and there must be either a blob or text value stored in +** the nominated column.)^ ^If the new row is not present in the table, or if +** it does not contain a blob or text value, or if another error occurs, an +** SQLite error code is returned and the blob handle is considered aborted. +** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or +** [sqlite3_blob_reopen()] on an aborted blob handle immediately return +** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle +** always returns zero. +** +** ^This function sets the database handle error code and message. +*/ +SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); + +/* +** CAPI3REF: Close A BLOB Handle +** +** ^Closes an open [BLOB handle]. +** +** ^Closing a BLOB shall cause the current transaction to commit +** if there are no other BLOBs, no pending prepared statements, and the +** database connection is in [autocommit mode]. +** ^If any writes were made to the BLOB, they might be held in cache +** until the close operation if they will fit. +** +** ^(Closing the BLOB often forces the changes +** out to disk and so if any I/O errors occur, they will likely occur +** at the time when the BLOB is closed. Any errors that occur during +** closing are reported as a non-zero return value.)^ +** +** ^(The BLOB is closed unconditionally. Even if this routine returns +** an error code, the BLOB is still closed.)^ +** +** ^Calling this routine with a null pointer (such as would be returned +** by a failed call to [sqlite3_blob_open()]) is a harmless no-op. +*/ +SQLITE_API int sqlite3_blob_close(sqlite3_blob *); + +/* +** CAPI3REF: Return The Size Of An Open BLOB +** +** ^Returns the size in bytes of the BLOB accessible via the +** successfully opened [BLOB handle] in its only argument. ^The +** incremental blob I/O routines can only read or overwriting existing +** blob content; they cannot change the size of a blob. +** +** This routine only works on a [BLOB handle] which has been created +** by a prior successful call to [sqlite3_blob_open()] and which has not +** been closed by [sqlite3_blob_close()]. Passing any other pointer in +** to this routine results in undefined and probably undesirable behavior. +*/ +SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *); + +/* +** CAPI3REF: Read Data From A BLOB Incrementally +** +** ^(This function is used to read data from an open [BLOB handle] into a +** caller-supplied buffer. N bytes of data are copied into buffer Z +** from the open BLOB, starting at offset iOffset.)^ +** +** ^If offset iOffset is less than N bytes from the end of the BLOB, +** [SQLITE_ERROR] is returned and no data is read. ^If N or iOffset is +** less than zero, [SQLITE_ERROR] is returned and no data is read. +** ^The size of the blob (and hence the maximum value of N+iOffset) +** can be determined using the [sqlite3_blob_bytes()] interface. +** +** ^An attempt to read from an expired [BLOB handle] fails with an +** error code of [SQLITE_ABORT]. +** +** ^(On success, sqlite3_blob_read() returns SQLITE_OK. +** Otherwise, an [error code] or an [extended error code] is returned.)^ +** +** This routine only works on a [BLOB handle] which has been created +** by a prior successful call to [sqlite3_blob_open()] and which has not +** been closed by [sqlite3_blob_close()]. Passing any other pointer in +** to this routine results in undefined and probably undesirable behavior. +** +** See also: [sqlite3_blob_write()]. +*/ +SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); + +/* +** CAPI3REF: Write Data Into A BLOB Incrementally +** +** ^This function is used to write data into an open [BLOB handle] from a +** caller-supplied buffer. ^N bytes of data are copied from the buffer Z +** into the open BLOB, starting at offset iOffset. +** +** ^If the [BLOB handle] passed as the first argument was not opened for +** writing (the flags parameter to [sqlite3_blob_open()] was zero), +** this function returns [SQLITE_READONLY]. +** +** ^This function may only modify the contents of the BLOB; it is +** not possible to increase the size of a BLOB using this API. +** ^If offset iOffset is less than N bytes from the end of the BLOB, +** [SQLITE_ERROR] is returned and no data is written. ^If N is +** less than zero [SQLITE_ERROR] is returned and no data is written. +** The size of the BLOB (and hence the maximum value of N+iOffset) +** can be determined using the [sqlite3_blob_bytes()] interface. +** +** ^An attempt to write to an expired [BLOB handle] fails with an +** error code of [SQLITE_ABORT]. ^Writes to the BLOB that occurred +** before the [BLOB handle] expired are not rolled back by the +** expiration of the handle, though of course those changes might +** have been overwritten by the statement that expired the BLOB handle +** or by other independent statements. +** +** ^(On success, sqlite3_blob_write() returns SQLITE_OK. +** Otherwise, an [error code] or an [extended error code] is returned.)^ +** +** This routine only works on a [BLOB handle] which has been created +** by a prior successful call to [sqlite3_blob_open()] and which has not +** been closed by [sqlite3_blob_close()]. Passing any other pointer in +** to this routine results in undefined and probably undesirable behavior. +** +** See also: [sqlite3_blob_read()]. +*/ +SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); + +/* +** CAPI3REF: Virtual File System Objects +** +** A virtual filesystem (VFS) is an [sqlite3_vfs] object +** that SQLite uses to interact +** with the underlying operating system. Most SQLite builds come with a +** single default VFS that is appropriate for the host computer. +** New VFSes can be registered and existing VFSes can be unregistered. +** The following interfaces are provided. +** +** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name. +** ^Names are case sensitive. +** ^Names are zero-terminated UTF-8 strings. +** ^If there is no match, a NULL pointer is returned. +** ^If zVfsName is NULL then the default VFS is returned. +** +** ^New VFSes are registered with sqlite3_vfs_register(). +** ^Each new VFS becomes the default VFS if the makeDflt flag is set. +** ^The same VFS can be registered multiple times without injury. +** ^To make an existing VFS into the default VFS, register it again +** with the makeDflt flag set. If two different VFSes with the +** same name are registered, the behavior is undefined. If a +** VFS is registered with a name that is NULL or an empty string, +** then the behavior is undefined. +** +** ^Unregister a VFS with the sqlite3_vfs_unregister() interface. +** ^(If the default VFS is unregistered, another VFS is chosen as +** the default. The choice for the new VFS is arbitrary.)^ +*/ +SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); +SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); +SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*); + +/* +** CAPI3REF: Mutexes +** +** The SQLite core uses these routines for thread +** synchronization. Though they are intended for internal +** use by SQLite, code that links against SQLite is +** permitted to use any of these routines. +** +** The SQLite source code contains multiple implementations +** of these mutex routines. An appropriate implementation +** is selected automatically at compile-time. ^(The following +** implementations are available in the SQLite core: +** +**
      +**
    • SQLITE_MUTEX_OS2 +**
    • SQLITE_MUTEX_PTHREAD +**
    • SQLITE_MUTEX_W32 +**
    • SQLITE_MUTEX_NOOP +**
    )^ +** +** ^The SQLITE_MUTEX_NOOP implementation is a set of routines +** that does no real locking and is appropriate for use in +** a single-threaded application. ^The SQLITE_MUTEX_OS2, +** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations +** are appropriate for use on OS/2, Unix, and Windows. +** +** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor +** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex +** implementation is included with the library. In this case the +** application must supply a custom mutex implementation using the +** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function +** before calling sqlite3_initialize() or any other public sqlite3_ +** function that calls sqlite3_initialize().)^ +** +** ^The sqlite3_mutex_alloc() routine allocates a new +** mutex and returns a pointer to it. ^If it returns NULL +** that means that a mutex could not be allocated. ^SQLite +** will unwind its stack and return an error. ^(The argument +** to sqlite3_mutex_alloc() is one of these integer constants: +** +**
      +**
    • SQLITE_MUTEX_FAST +**
    • SQLITE_MUTEX_RECURSIVE +**
    • SQLITE_MUTEX_STATIC_MASTER +**
    • SQLITE_MUTEX_STATIC_MEM +**
    • SQLITE_MUTEX_STATIC_MEM2 +**
    • SQLITE_MUTEX_STATIC_PRNG +**
    • SQLITE_MUTEX_STATIC_LRU +**
    • SQLITE_MUTEX_STATIC_LRU2 +**
    )^ +** +** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) +** cause sqlite3_mutex_alloc() to create +** a new mutex. ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE +** is used but not necessarily so when SQLITE_MUTEX_FAST is used. +** The mutex implementation does not need to make a distinction +** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does +** not want to. ^SQLite will only request a recursive mutex in +** cases where it really needs one. ^If a faster non-recursive mutex +** implementation is available on the host platform, the mutex subsystem +** might return such a mutex in response to SQLITE_MUTEX_FAST. +** +** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other +** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return +** a pointer to a static preexisting mutex. ^Six static mutexes are +** used by the current version of SQLite. Future versions of SQLite +** may add additional static mutexes. Static mutexes are for internal +** use by SQLite only. Applications that use SQLite mutexes should +** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or +** SQLITE_MUTEX_RECURSIVE. +** +** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST +** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc() +** returns a different mutex on every call. ^But for the static +** mutex types, the same mutex is returned on every call that has +** the same type number. +** +** ^The sqlite3_mutex_free() routine deallocates a previously +** allocated dynamic mutex. ^SQLite is careful to deallocate every +** dynamic mutex that it allocates. The dynamic mutexes must not be in +** use when they are deallocated. Attempting to deallocate a static +** mutex results in undefined behavior. ^SQLite never deallocates +** a static mutex. +** +** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt +** to enter a mutex. ^If another thread is already within the mutex, +** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return +** SQLITE_BUSY. ^The sqlite3_mutex_try() interface returns [SQLITE_OK] +** upon successful entry. ^(Mutexes created using +** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread. +** In such cases the, +** mutex must be exited an equal number of times before another thread +** can enter.)^ ^(If the same thread tries to enter any other +** kind of mutex more than once, the behavior is undefined. +** SQLite will never exhibit +** such behavior in its own use of mutexes.)^ +** +** ^(Some systems (for example, Windows 95) do not support the operation +** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try() +** will always return SQLITE_BUSY. The SQLite core only ever uses +** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^ +** +** ^The sqlite3_mutex_leave() routine exits a mutex that was +** previously entered by the same thread. ^(The behavior +** is undefined if the mutex is not currently entered by the +** calling thread or is not currently allocated. SQLite will +** never do either.)^ +** +** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or +** sqlite3_mutex_leave() is a NULL pointer, then all three routines +** behave as no-ops. +** +** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()]. +*/ +SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int); +SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*); +SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*); +SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*); +SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*); + +/* +** CAPI3REF: Mutex Methods Object +** +** An instance of this structure defines the low-level routines +** used to allocate and use mutexes. +** +** Usually, the default mutex implementations provided by SQLite are +** sufficient, however the user has the option of substituting a custom +** implementation for specialized deployments or systems for which SQLite +** does not provide a suitable implementation. In this case, the user +** creates and populates an instance of this structure to pass +** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option. +** Additionally, an instance of this structure can be used as an +** output variable when querying the system for the current mutex +** implementation, using the [SQLITE_CONFIG_GETMUTEX] option. +** +** ^The xMutexInit method defined by this structure is invoked as +** part of system initialization by the sqlite3_initialize() function. +** ^The xMutexInit routine is called by SQLite exactly once for each +** effective call to [sqlite3_initialize()]. +** +** ^The xMutexEnd method defined by this structure is invoked as +** part of system shutdown by the sqlite3_shutdown() function. The +** implementation of this method is expected to release all outstanding +** resources obtained by the mutex methods implementation, especially +** those obtained by the xMutexInit method. ^The xMutexEnd() +** interface is invoked exactly once for each call to [sqlite3_shutdown()]. +** +** ^(The remaining seven methods defined by this structure (xMutexAlloc, +** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and +** xMutexNotheld) implement the following interfaces (respectively): +** +**
      +**
    • [sqlite3_mutex_alloc()]
    • +**
    • [sqlite3_mutex_free()]
    • +**
    • [sqlite3_mutex_enter()]
    • +**
    • [sqlite3_mutex_try()]
    • +**
    • [sqlite3_mutex_leave()]
    • +**
    • [sqlite3_mutex_held()]
    • +**
    • [sqlite3_mutex_notheld()]
    • +**
    )^ +** +** The only difference is that the public sqlite3_XXX functions enumerated +** above silently ignore any invocations that pass a NULL pointer instead +** of a valid mutex handle. The implementations of the methods defined +** by this structure are not required to handle this case, the results +** of passing a NULL pointer instead of a valid mutex handle are undefined +** (i.e. it is acceptable to provide an implementation that segfaults if +** it is passed a NULL pointer). +** +** The xMutexInit() method must be threadsafe. ^It must be harmless to +** invoke xMutexInit() multiple times within the same process and without +** intervening calls to xMutexEnd(). Second and subsequent calls to +** xMutexInit() must be no-ops. +** +** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()] +** and its associates). ^Similarly, xMutexAlloc() must not use SQLite memory +** allocation for a static mutex. ^However xMutexAlloc() may use SQLite +** memory allocation for a fast or recursive mutex. +** +** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is +** called, but only if the prior call to xMutexInit returned SQLITE_OK. +** If xMutexInit fails in any way, it is expected to clean up after itself +** prior to returning. +*/ +typedef struct sqlite3_mutex_methods sqlite3_mutex_methods; +struct sqlite3_mutex_methods { + int (*xMutexInit)(void); + int (*xMutexEnd)(void); + sqlite3_mutex *(*xMutexAlloc)(int); + void (*xMutexFree)(sqlite3_mutex *); + void (*xMutexEnter)(sqlite3_mutex *); + int (*xMutexTry)(sqlite3_mutex *); + void (*xMutexLeave)(sqlite3_mutex *); + int (*xMutexHeld)(sqlite3_mutex *); + int (*xMutexNotheld)(sqlite3_mutex *); +}; + +/* +** CAPI3REF: Mutex Verification Routines +** +** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines +** are intended for use inside assert() statements. ^The SQLite core +** never uses these routines except inside an assert() and applications +** are advised to follow the lead of the core. ^The SQLite core only +** provides implementations for these routines when it is compiled +** with the SQLITE_DEBUG flag. ^External mutex implementations +** are only required to provide these routines if SQLITE_DEBUG is +** defined and if NDEBUG is not defined. +** +** ^These routines should return true if the mutex in their argument +** is held or not held, respectively, by the calling thread. +** +** ^The implementation is not required to provided versions of these +** routines that actually work. If the implementation does not provide working +** versions of these routines, it should at least provide stubs that always +** return true so that one does not get spurious assertion failures. +** +** ^If the argument to sqlite3_mutex_held() is a NULL pointer then +** the routine should return 1. This seems counter-intuitive since +** clearly the mutex cannot be held if it does not exist. But the +** the reason the mutex does not exist is because the build is not +** using mutexes. And we do not want the assert() containing the +** call to sqlite3_mutex_held() to fail, so a non-zero return is +** the appropriate thing to do. ^The sqlite3_mutex_notheld() +** interface should also return 1 when given a NULL pointer. +*/ +#ifndef NDEBUG +SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*); +SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*); +#endif + +/* +** CAPI3REF: Mutex Types +** +** The [sqlite3_mutex_alloc()] interface takes a single argument +** which is one of these integer constants. +** +** The set of static mutexes may change from one SQLite release to the +** next. Applications that override the built-in mutex logic must be +** prepared to accommodate additional static mutexes. +*/ +#define SQLITE_MUTEX_FAST 0 +#define SQLITE_MUTEX_RECURSIVE 1 +#define SQLITE_MUTEX_STATIC_MASTER 2 +#define SQLITE_MUTEX_STATIC_MEM 3 /* sqlite3_malloc() */ +#define SQLITE_MUTEX_STATIC_MEM2 4 /* NOT USED */ +#define SQLITE_MUTEX_STATIC_OPEN 4 /* sqlite3BtreeOpen() */ +#define SQLITE_MUTEX_STATIC_PRNG 5 /* sqlite3_random() */ +#define SQLITE_MUTEX_STATIC_LRU 6 /* lru page list */ +#define SQLITE_MUTEX_STATIC_LRU2 7 /* NOT USED */ +#define SQLITE_MUTEX_STATIC_PMEM 7 /* sqlite3PageMalloc() */ + +/* +** CAPI3REF: Retrieve the mutex for a database connection +** +** ^This interface returns a pointer the [sqlite3_mutex] object that +** serializes access to the [database connection] given in the argument +** when the [threading mode] is Serialized. +** ^If the [threading mode] is Single-thread or Multi-thread then this +** routine returns a NULL pointer. +*/ +SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*); + +/* +** CAPI3REF: Low-Level Control Of Database Files +** +** ^The [sqlite3_file_control()] interface makes a direct call to the +** xFileControl method for the [sqlite3_io_methods] object associated +** with a particular database identified by the second argument. ^The +** name of the database is "main" for the main database or "temp" for the +** TEMP database, or the name that appears after the AS keyword for +** databases that are added using the [ATTACH] SQL command. +** ^A NULL pointer can be used in place of "main" to refer to the +** main database file. +** ^The third and fourth parameters to this routine +** are passed directly through to the second and third parameters of +** the xFileControl method. ^The return value of the xFileControl +** method becomes the return value of this routine. +** +** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes +** a pointer to the underlying [sqlite3_file] object to be written into +** the space pointed to by the 4th parameter. ^The SQLITE_FCNTL_FILE_POINTER +** case is a short-circuit path which does not actually invoke the +** underlying sqlite3_io_methods.xFileControl method. +** +** ^If the second parameter (zDbName) does not match the name of any +** open database file, then SQLITE_ERROR is returned. ^This error +** code is not remembered and will not be recalled by [sqlite3_errcode()] +** or [sqlite3_errmsg()]. The underlying xFileControl method might +** also return SQLITE_ERROR. There is no way to distinguish between +** an incorrect zDbName and an SQLITE_ERROR return from the underlying +** xFileControl method. +** +** See also: [SQLITE_FCNTL_LOCKSTATE] +*/ +SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); + +/* +** CAPI3REF: Testing Interface +** +** ^The sqlite3_test_control() interface is used to read out internal +** state of SQLite and to inject faults into SQLite for testing +** purposes. ^The first parameter is an operation code that determines +** the number, meaning, and operation of all subsequent parameters. +** +** This interface is not for use by applications. It exists solely +** for verifying the correct operation of the SQLite library. Depending +** on how the SQLite library is compiled, this interface might not exist. +** +** The details of the operation codes, their meanings, the parameters +** they take, and what they do are all subject to change without notice. +** Unlike most of the SQLite API, this function is not guaranteed to +** operate consistently from one release to the next. +*/ +SQLITE_API int sqlite3_test_control(int op, ...); + +/* +** CAPI3REF: Testing Interface Operation Codes +** +** These constants are the valid operation code parameters used +** as the first argument to [sqlite3_test_control()]. +** +** These parameters and their meanings are subject to change +** without notice. These values are for testing purposes only. +** Applications should not use any of these parameters or the +** [sqlite3_test_control()] interface. +*/ +#define SQLITE_TESTCTRL_FIRST 5 +#define SQLITE_TESTCTRL_PRNG_SAVE 5 +#define SQLITE_TESTCTRL_PRNG_RESTORE 6 +#define SQLITE_TESTCTRL_PRNG_RESET 7 +#define SQLITE_TESTCTRL_BITVEC_TEST 8 +#define SQLITE_TESTCTRL_FAULT_INSTALL 9 +#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10 +#define SQLITE_TESTCTRL_PENDING_BYTE 11 +#define SQLITE_TESTCTRL_ASSERT 12 +#define SQLITE_TESTCTRL_ALWAYS 13 +#define SQLITE_TESTCTRL_RESERVE 14 +#define SQLITE_TESTCTRL_OPTIMIZATIONS 15 +#define SQLITE_TESTCTRL_ISKEYWORD 16 +#define SQLITE_TESTCTRL_PGHDRSZ 17 +#define SQLITE_TESTCTRL_SCRATCHMALLOC 18 +#define SQLITE_TESTCTRL_LAST 18 + +/* +** CAPI3REF: SQLite Runtime Status +** +** ^This interface is used to retrieve runtime status information +** about the performance of SQLite, and optionally to reset various +** highwater marks. ^The first argument is an integer code for +** the specific parameter to measure. ^(Recognized integer codes +** are of the form [SQLITE_STATUS_MEMORY_USED | SQLITE_STATUS_...].)^ +** ^The current value of the parameter is returned into *pCurrent. +** ^The highest recorded value is returned in *pHighwater. ^If the +** resetFlag is true, then the highest record value is reset after +** *pHighwater is written. ^(Some parameters do not record the highest +** value. For those parameters +** nothing is written into *pHighwater and the resetFlag is ignored.)^ +** ^(Other parameters record only the highwater mark and not the current +** value. For these latter parameters nothing is written into *pCurrent.)^ +** +** ^The sqlite3_status() routine returns SQLITE_OK on success and a +** non-zero [error code] on failure. +** +** This routine is threadsafe but is not atomic. This routine can be +** called while other threads are running the same or different SQLite +** interfaces. However the values returned in *pCurrent and +** *pHighwater reflect the status of SQLite at different points in time +** and it is possible that another thread might change the parameter +** in between the times when *pCurrent and *pHighwater are written. +** +** See also: [sqlite3_db_status()] +*/ +SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); + + +/* +** CAPI3REF: Status Parameters +** +** These integer constants designate various run-time status parameters +** that can be returned by [sqlite3_status()]. +** +**
    +** ^(
    SQLITE_STATUS_MEMORY_USED
    +**
    This parameter is the current amount of memory checked out +** using [sqlite3_malloc()], either directly or indirectly. The +** figure includes calls made to [sqlite3_malloc()] by the application +** and internal memory usage by the SQLite library. Scratch memory +** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache +** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in +** this parameter. The amount returned is the sum of the allocation +** sizes as reported by the xSize method in [sqlite3_mem_methods].
    )^ +** +** ^(
    SQLITE_STATUS_MALLOC_SIZE
    +**
    This parameter records the largest memory allocation request +** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their +** internal equivalents). Only the value returned in the +** *pHighwater parameter to [sqlite3_status()] is of interest. +** The value written into the *pCurrent parameter is undefined.
    )^ +** +** ^(
    SQLITE_STATUS_MALLOC_COUNT
    +**
    This parameter records the number of separate memory allocations +** currently checked out.
    )^ +** +** ^(
    SQLITE_STATUS_PAGECACHE_USED
    +**
    This parameter returns the number of pages used out of the +** [pagecache memory allocator] that was configured using +** [SQLITE_CONFIG_PAGECACHE]. The +** value returned is in pages, not in bytes.
    )^ +** +** ^(
    SQLITE_STATUS_PAGECACHE_OVERFLOW
    +**
    This parameter returns the number of bytes of page cache +** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE] +** buffer and where forced to overflow to [sqlite3_malloc()]. The +** returned value includes allocations that overflowed because they +** where too large (they were larger than the "sz" parameter to +** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because +** no space was left in the page cache.
    )^ +** +** ^(
    SQLITE_STATUS_PAGECACHE_SIZE
    +**
    This parameter records the largest memory allocation request +** handed to [pagecache memory allocator]. Only the value returned in the +** *pHighwater parameter to [sqlite3_status()] is of interest. +** The value written into the *pCurrent parameter is undefined.
    )^ +** +** ^(
    SQLITE_STATUS_SCRATCH_USED
    +**
    This parameter returns the number of allocations used out of the +** [scratch memory allocator] configured using +** [SQLITE_CONFIG_SCRATCH]. The value returned is in allocations, not +** in bytes. Since a single thread may only have one scratch allocation +** outstanding at time, this parameter also reports the number of threads +** using scratch memory at the same time.
    )^ +** +** ^(
    SQLITE_STATUS_SCRATCH_OVERFLOW
    +**
    This parameter returns the number of bytes of scratch memory +** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH] +** buffer and where forced to overflow to [sqlite3_malloc()]. The values +** returned include overflows because the requested allocation was too +** larger (that is, because the requested allocation was larger than the +** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer +** slots were available. +**
    )^ +** +** ^(
    SQLITE_STATUS_SCRATCH_SIZE
    +**
    This parameter records the largest memory allocation request +** handed to [scratch memory allocator]. Only the value returned in the +** *pHighwater parameter to [sqlite3_status()] is of interest. +** The value written into the *pCurrent parameter is undefined.
    )^ +** +** ^(
    SQLITE_STATUS_PARSER_STACK
    +**
    This parameter records the deepest parser stack. It is only +** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].
    )^ +**
    +** +** New status parameters may be added from time to time. +*/ +#define SQLITE_STATUS_MEMORY_USED 0 +#define SQLITE_STATUS_PAGECACHE_USED 1 +#define SQLITE_STATUS_PAGECACHE_OVERFLOW 2 +#define SQLITE_STATUS_SCRATCH_USED 3 +#define SQLITE_STATUS_SCRATCH_OVERFLOW 4 +#define SQLITE_STATUS_MALLOC_SIZE 5 +#define SQLITE_STATUS_PARSER_STACK 6 +#define SQLITE_STATUS_PAGECACHE_SIZE 7 +#define SQLITE_STATUS_SCRATCH_SIZE 8 +#define SQLITE_STATUS_MALLOC_COUNT 9 + +/* +** CAPI3REF: Database Connection Status +** +** ^This interface is used to retrieve runtime status information +** about a single [database connection]. ^The first argument is the +** database connection object to be interrogated. ^The second argument +** is an integer constant, taken from the set of +** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros, that +** determines the parameter to interrogate. The set of +** [SQLITE_DBSTATUS_LOOKASIDE_USED | SQLITE_DBSTATUS_*] macros is likely +** to grow in future releases of SQLite. +** +** ^The current value of the requested parameter is written into *pCur +** and the highest instantaneous value is written into *pHiwtr. ^If +** the resetFlg is true, then the highest instantaneous value is +** reset back down to the current value. +** +** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a +** non-zero [error code] on failure. +** +** See also: [sqlite3_status()] and [sqlite3_stmt_status()]. +*/ +SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); + +/* +** CAPI3REF: Status Parameters for database connections +** +** These constants are the available integer "verbs" that can be passed as +** the second argument to the [sqlite3_db_status()] interface. +** +** New verbs may be added in future releases of SQLite. Existing verbs +** might be discontinued. Applications should check the return code from +** [sqlite3_db_status()] to make sure that the call worked. +** The [sqlite3_db_status()] interface will return a non-zero error code +** if a discontinued or unsupported verb is invoked. +** +**
    +** ^(
    SQLITE_DBSTATUS_LOOKASIDE_USED
    +**
    This parameter returns the number of lookaside memory slots currently +** checked out.
    )^ +** +** ^(
    SQLITE_DBSTATUS_LOOKASIDE_HIT
    +**
    This parameter returns the number malloc attempts that were +** satisfied using lookaside memory. Only the high-water value is meaningful; +** the current value is always zero.)^ +** +** ^(
    SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
    +**
    This parameter returns the number malloc attempts that might have +** been satisfied using lookaside memory but failed due to the amount of +** memory requested being larger than the lookaside slot size. +** Only the high-water value is meaningful; +** the current value is always zero.)^ +** +** ^(
    SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
    +**
    This parameter returns the number malloc attempts that might have +** been satisfied using lookaside memory but failed due to all lookaside +** memory already being in use. +** Only the high-water value is meaningful; +** the current value is always zero.)^ +** +** ^(
    SQLITE_DBSTATUS_CACHE_USED
    +**
    This parameter returns the approximate number of of bytes of heap +** memory used by all pager caches associated with the database connection.)^ +** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0. +** +** ^(
    SQLITE_DBSTATUS_SCHEMA_USED
    +**
    This parameter returns the approximate number of of bytes of heap +** memory used to store the schema for all databases associated +** with the connection - main, temp, and any [ATTACH]-ed databases.)^ +** ^The full amount of memory used by the schemas is reported, even if the +** schema memory is shared with other database connections due to +** [shared cache mode] being enabled. +** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0. +** +** ^(
    SQLITE_DBSTATUS_STMT_USED
    +**
    This parameter returns the approximate number of of bytes of heap +** and lookaside memory used by all prepared statements associated with +** the database connection.)^ +** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0. +**
    +**
    +*/ +#define SQLITE_DBSTATUS_LOOKASIDE_USED 0 +#define SQLITE_DBSTATUS_CACHE_USED 1 +#define SQLITE_DBSTATUS_SCHEMA_USED 2 +#define SQLITE_DBSTATUS_STMT_USED 3 +#define SQLITE_DBSTATUS_LOOKASIDE_HIT 4 +#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5 +#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6 +#define SQLITE_DBSTATUS_MAX 6 /* Largest defined DBSTATUS */ + + +/* +** CAPI3REF: Prepared Statement Status +** +** ^(Each prepared statement maintains various +** [SQLITE_STMTSTATUS_SORT | counters] that measure the number +** of times it has performed specific operations.)^ These counters can +** be used to monitor the performance characteristics of the prepared +** statements. For example, if the number of table steps greatly exceeds +** the number of table searches or result rows, that would tend to indicate +** that the prepared statement is using a full table scan rather than +** an index. +** +** ^(This interface is used to retrieve and reset counter values from +** a [prepared statement]. The first argument is the prepared statement +** object to be interrogated. The second argument +** is an integer code for a specific [SQLITE_STMTSTATUS_SORT | counter] +** to be interrogated.)^ +** ^The current value of the requested counter is returned. +** ^If the resetFlg is true, then the counter is reset to zero after this +** interface call returns. +** +** See also: [sqlite3_status()] and [sqlite3_db_status()]. +*/ +SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); + +/* +** CAPI3REF: Status Parameters for prepared statements +** +** These preprocessor macros define integer codes that name counter +** values associated with the [sqlite3_stmt_status()] interface. +** The meanings of the various counters are as follows: +** +**
    +**
    SQLITE_STMTSTATUS_FULLSCAN_STEP
    +**
    ^This is the number of times that SQLite has stepped forward in +** a table as part of a full table scan. Large numbers for this counter +** may indicate opportunities for performance improvement through +** careful use of indices.
    +** +**
    SQLITE_STMTSTATUS_SORT
    +**
    ^This is the number of sort operations that have occurred. +** A non-zero value in this counter may indicate an opportunity to +** improvement performance through careful use of indices.
    +** +**
    SQLITE_STMTSTATUS_AUTOINDEX
    +**
    ^This is the number of rows inserted into transient indices that +** were created automatically in order to help joins run faster. +** A non-zero value in this counter may indicate an opportunity to +** improvement performance by adding permanent indices that do not +** need to be reinitialized each time the statement is run.
    +** +**
    +*/ +#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1 +#define SQLITE_STMTSTATUS_SORT 2 +#define SQLITE_STMTSTATUS_AUTOINDEX 3 + +/* +** CAPI3REF: Custom Page Cache Object +** +** The sqlite3_pcache type is opaque. It is implemented by +** the pluggable module. The SQLite core has no knowledge of +** its size or internal structure and never deals with the +** sqlite3_pcache object except by holding and passing pointers +** to the object. +** +** See [sqlite3_pcache_methods] for additional information. +*/ +typedef struct sqlite3_pcache sqlite3_pcache; + +/* +** CAPI3REF: Application Defined Page Cache. +** KEYWORDS: {page cache} +** +** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can +** register an alternative page cache implementation by passing in an +** instance of the sqlite3_pcache_methods structure.)^ +** In many applications, most of the heap memory allocated by +** SQLite is used for the page cache. +** By implementing a +** custom page cache using this API, an application can better control +** the amount of memory consumed by SQLite, the way in which +** that memory is allocated and released, and the policies used to +** determine exactly which parts of a database file are cached and for +** how long. +** +** The alternative page cache mechanism is an +** extreme measure that is only needed by the most demanding applications. +** The built-in page cache is recommended for most uses. +** +** ^(The contents of the sqlite3_pcache_methods structure are copied to an +** internal buffer by SQLite within the call to [sqlite3_config]. Hence +** the application may discard the parameter after the call to +** [sqlite3_config()] returns.)^ +** +** ^(The xInit() method is called once for each effective +** call to [sqlite3_initialize()])^ +** (usually only once during the lifetime of the process). ^(The xInit() +** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^ +** The intent of the xInit() method is to set up global data structures +** required by the custom page cache implementation. +** ^(If the xInit() method is NULL, then the +** built-in default page cache is used instead of the application defined +** page cache.)^ +** +** ^The xShutdown() method is called by [sqlite3_shutdown()]. +** It can be used to clean up +** any outstanding resources before process shutdown, if required. +** ^The xShutdown() method may be NULL. +** +** ^SQLite automatically serializes calls to the xInit method, +** so the xInit method need not be threadsafe. ^The +** xShutdown method is only called from [sqlite3_shutdown()] so it does +** not need to be threadsafe either. All other methods must be threadsafe +** in multithreaded applications. +** +** ^SQLite will never invoke xInit() more than once without an intervening +** call to xShutdown(). +** +** ^SQLite invokes the xCreate() method to construct a new cache instance. +** SQLite will typically create one cache instance for each open database file, +** though this is not guaranteed. ^The +** first parameter, szPage, is the size in bytes of the pages that must +** be allocated by the cache. ^szPage will not be a power of two. ^szPage +** will the page size of the database file that is to be cached plus an +** increment (here called "R") of less than 250. SQLite will use the +** extra R bytes on each page to store metadata about the underlying +** database page on disk. The value of R depends +** on the SQLite version, the target platform, and how SQLite was compiled. +** ^(R is constant for a particular build of SQLite. Except, there are two +** distinct values of R when SQLite is compiled with the proprietary +** ZIPVFS extension.)^ ^The second argument to +** xCreate(), bPurgeable, is true if the cache being created will +** be used to cache database pages of a file stored on disk, or +** false if it is used for an in-memory database. The cache implementation +** does not have to do anything special based with the value of bPurgeable; +** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will +** never invoke xUnpin() except to deliberately delete a page. +** ^In other words, calls to xUnpin() on a cache with bPurgeable set to +** false will always have the "discard" flag set to true. +** ^Hence, a cache created with bPurgeable false will +** never contain any unpinned pages. +** +** ^(The xCachesize() method may be called at any time by SQLite to set the +** suggested maximum cache-size (number of pages stored by) the cache +** instance passed as the first argument. This is the value configured using +** the SQLite "[PRAGMA cache_size]" command.)^ As with the bPurgeable +** parameter, the implementation is not required to do anything with this +** value; it is advisory only. +** +** The xPagecount() method must return the number of pages currently +** stored in the cache, both pinned and unpinned. +** +** The xFetch() method locates a page in the cache and returns a pointer to +** the page, or a NULL pointer. +** A "page", in this context, means a buffer of szPage bytes aligned at an +** 8-byte boundary. The page to be fetched is determined by the key. ^The +** mimimum key value is 1. After it has been retrieved using xFetch, the page +** is considered to be "pinned". +** +** If the requested page is already in the page cache, then the page cache +** implementation must return a pointer to the page buffer with its content +** intact. If the requested page is not already in the cache, then the +** cache implementation should use the value of the createFlag +** parameter to help it determined what action to take: +** +** +**
    createFlag Behaviour when page is not already in cache +**
    0 Do not allocate a new page. Return NULL. +**
    1 Allocate a new page if it easy and convenient to do so. +** Otherwise return NULL. +**
    2 Make every effort to allocate a new page. Only return +** NULL if allocating a new page is effectively impossible. +**
    +** +** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1. SQLite +** will only use a createFlag of 2 after a prior call with a createFlag of 1 +** failed.)^ In between the to xFetch() calls, SQLite may +** attempt to unpin one or more cache pages by spilling the content of +** pinned pages to disk and synching the operating system disk cache. +** +** ^xUnpin() is called by SQLite with a pointer to a currently pinned page +** as its second argument. If the third parameter, discard, is non-zero, +** then the page must be evicted from the cache. +** ^If the discard parameter is +** zero, then the page may be discarded or retained at the discretion of +** page cache implementation. ^The page cache implementation +** may choose to evict unpinned pages at any time. +** +** The cache must not perform any reference counting. A single +** call to xUnpin() unpins the page regardless of the number of prior calls +** to xFetch(). +** +** The xRekey() method is used to change the key value associated with the +** page passed as the second argument. If the cache +** previously contains an entry associated with newKey, it must be +** discarded. ^Any prior cache entry associated with newKey is guaranteed not +** to be pinned. +** +** When SQLite calls the xTruncate() method, the cache must discard all +** existing cache entries with page numbers (keys) greater than or equal +** to the value of the iLimit parameter passed to xTruncate(). If any +** of these pages are pinned, they are implicitly unpinned, meaning that +** they can be safely discarded. +** +** ^The xDestroy() method is used to delete a cache allocated by xCreate(). +** All resources associated with the specified cache should be freed. ^After +** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*] +** handle invalid, and will not use it with any other sqlite3_pcache_methods +** functions. +*/ +typedef struct sqlite3_pcache_methods sqlite3_pcache_methods; +struct sqlite3_pcache_methods { + void *pArg; + int (*xInit)(void*); + void (*xShutdown)(void*); + sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable); + void (*xCachesize)(sqlite3_pcache*, int nCachesize); + int (*xPagecount)(sqlite3_pcache*); + void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag); + void (*xUnpin)(sqlite3_pcache*, void*, int discard); + void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey); + void (*xTruncate)(sqlite3_pcache*, unsigned iLimit); + void (*xDestroy)(sqlite3_pcache*); +}; + +/* +** CAPI3REF: Online Backup Object +** +** The sqlite3_backup object records state information about an ongoing +** online backup operation. ^The sqlite3_backup object is created by +** a call to [sqlite3_backup_init()] and is destroyed by a call to +** [sqlite3_backup_finish()]. +** +** See Also: [Using the SQLite Online Backup API] +*/ +typedef struct sqlite3_backup sqlite3_backup; + +/* +** CAPI3REF: Online Backup API. +** +** The backup API copies the content of one database into another. +** It is useful either for creating backups of databases or +** for copying in-memory databases to or from persistent files. +** +** See Also: [Using the SQLite Online Backup API] +** +** ^SQLite holds a write transaction open on the destination database file +** for the duration of the backup operation. +** ^The source database is read-locked only while it is being read; +** it is not locked continuously for the entire backup operation. +** ^Thus, the backup may be performed on a live source database without +** preventing other database connections from +** reading or writing to the source database while the backup is underway. +** +** ^(To perform a backup operation: +**
      +**
    1. sqlite3_backup_init() is called once to initialize the +** backup, +**
    2. sqlite3_backup_step() is called one or more times to transfer +** the data between the two databases, and finally +**
    3. sqlite3_backup_finish() is called to release all resources +** associated with the backup operation. +**
    )^ +** There should be exactly one call to sqlite3_backup_finish() for each +** successful call to sqlite3_backup_init(). +** +** sqlite3_backup_init() +** +** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the +** [database connection] associated with the destination database +** and the database name, respectively. +** ^The database name is "main" for the main database, "temp" for the +** temporary database, or the name specified after the AS keyword in +** an [ATTACH] statement for an attached database. +** ^The S and M arguments passed to +** sqlite3_backup_init(D,N,S,M) identify the [database connection] +** and database name of the source database, respectively. +** ^The source and destination [database connections] (parameters S and D) +** must be different or else sqlite3_backup_init(D,N,S,M) will fail with +** an error. +** +** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is +** returned and an error code and error message are stored in the +** destination [database connection] D. +** ^The error code and message for the failed call to sqlite3_backup_init() +** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or +** [sqlite3_errmsg16()] functions. +** ^A successful call to sqlite3_backup_init() returns a pointer to an +** [sqlite3_backup] object. +** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and +** sqlite3_backup_finish() functions to perform the specified backup +** operation. +** +** sqlite3_backup_step() +** +** ^Function sqlite3_backup_step(B,N) will copy up to N pages between +** the source and destination databases specified by [sqlite3_backup] object B. +** ^If N is negative, all remaining source pages are copied. +** ^If sqlite3_backup_step(B,N) successfully copies N pages and there +** are still more pages to be copied, then the function returns [SQLITE_OK]. +** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages +** from source to destination, then it returns [SQLITE_DONE]. +** ^If an error occurs while running sqlite3_backup_step(B,N), +** then an [error code] is returned. ^As well as [SQLITE_OK] and +** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY], +** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an +** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code. +** +** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if +**
      +**
    1. the destination database was opened read-only, or +**
    2. the destination database is using write-ahead-log journaling +** and the destination and source page sizes differ, or +**
    3. the destination database is an in-memory database and the +** destination and source page sizes differ. +**
    )^ +** +** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then +** the [sqlite3_busy_handler | busy-handler function] +** is invoked (if one is specified). ^If the +** busy-handler returns non-zero before the lock is available, then +** [SQLITE_BUSY] is returned to the caller. ^In this case the call to +** sqlite3_backup_step() can be retried later. ^If the source +** [database connection] +** is being used to write to the source database when sqlite3_backup_step() +** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this +** case the call to sqlite3_backup_step() can be retried later on. ^(If +** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or +** [SQLITE_READONLY] is returned, then +** there is no point in retrying the call to sqlite3_backup_step(). These +** errors are considered fatal.)^ The application must accept +** that the backup operation has failed and pass the backup operation handle +** to the sqlite3_backup_finish() to release associated resources. +** +** ^The first call to sqlite3_backup_step() obtains an exclusive lock +** on the destination file. ^The exclusive lock is not released until either +** sqlite3_backup_finish() is called or the backup operation is complete +** and sqlite3_backup_step() returns [SQLITE_DONE]. ^Every call to +** sqlite3_backup_step() obtains a [shared lock] on the source database that +** lasts for the duration of the sqlite3_backup_step() call. +** ^Because the source database is not locked between calls to +** sqlite3_backup_step(), the source database may be modified mid-way +** through the backup process. ^If the source database is modified by an +** external process or via a database connection other than the one being +** used by the backup operation, then the backup will be automatically +** restarted by the next call to sqlite3_backup_step(). ^If the source +** database is modified by the using the same database connection as is used +** by the backup operation, then the backup database is automatically +** updated at the same time. +** +** sqlite3_backup_finish() +** +** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the +** application wishes to abandon the backup operation, the application +** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish(). +** ^The sqlite3_backup_finish() interfaces releases all +** resources associated with the [sqlite3_backup] object. +** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any +** active write-transaction on the destination database is rolled back. +** The [sqlite3_backup] object is invalid +** and may not be used following a call to sqlite3_backup_finish(). +** +** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no +** sqlite3_backup_step() errors occurred, regardless or whether or not +** sqlite3_backup_step() completed. +** ^If an out-of-memory condition or IO error occurred during any prior +** sqlite3_backup_step() call on the same [sqlite3_backup] object, then +** sqlite3_backup_finish() returns the corresponding [error code]. +** +** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step() +** is not a permanent error and does not affect the return value of +** sqlite3_backup_finish(). +** +** sqlite3_backup_remaining(), sqlite3_backup_pagecount() +** +** ^Each call to sqlite3_backup_step() sets two values inside +** the [sqlite3_backup] object: the number of pages still to be backed +** up and the total number of pages in the source database file. +** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces +** retrieve these two values, respectively. +** +** ^The values returned by these functions are only updated by +** sqlite3_backup_step(). ^If the source database is modified during a backup +** operation, then the values are not updated to account for any extra +** pages that need to be updated or the size of the source database file +** changing. +** +** Concurrent Usage of Database Handles +** +** ^The source [database connection] may be used by the application for other +** purposes while a backup operation is underway or being initialized. +** ^If SQLite is compiled and configured to support threadsafe database +** connections, then the source database connection may be used concurrently +** from within other threads. +** +** However, the application must guarantee that the destination +** [database connection] is not passed to any other API (by any thread) after +** sqlite3_backup_init() is called and before the corresponding call to +** sqlite3_backup_finish(). SQLite does not currently check to see +** if the application incorrectly accesses the destination [database connection] +** and so no error code is reported, but the operations may malfunction +** nevertheless. Use of the destination database connection while a +** backup is in progress might also also cause a mutex deadlock. +** +** If running in [shared cache mode], the application must +** guarantee that the shared cache used by the destination database +** is not accessed while the backup is running. In practice this means +** that the application must guarantee that the disk file being +** backed up to is not accessed by any connection within the process, +** not just the specific connection that was passed to sqlite3_backup_init(). +** +** The [sqlite3_backup] object itself is partially threadsafe. Multiple +** threads may safely make multiple concurrent calls to sqlite3_backup_step(). +** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount() +** APIs are not strictly speaking threadsafe. If they are invoked at the +** same time as another thread is invoking sqlite3_backup_step() it is +** possible that they return invalid values. +*/ +SQLITE_API sqlite3_backup *sqlite3_backup_init( + sqlite3 *pDest, /* Destination database handle */ + const char *zDestName, /* Destination database name */ + sqlite3 *pSource, /* Source database handle */ + const char *zSourceName /* Source database name */ +); +SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage); +SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p); +SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p); +SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p); + +/* +** CAPI3REF: Unlock Notification +** +** ^When running in shared-cache mode, a database operation may fail with +** an [SQLITE_LOCKED] error if the required locks on the shared-cache or +** individual tables within the shared-cache cannot be obtained. See +** [SQLite Shared-Cache Mode] for a description of shared-cache locking. +** ^This API may be used to register a callback that SQLite will invoke +** when the connection currently holding the required lock relinquishes it. +** ^This API is only available if the library was compiled with the +** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined. +** +** See Also: [Using the SQLite Unlock Notification Feature]. +** +** ^Shared-cache locks are released when a database connection concludes +** its current transaction, either by committing it or rolling it back. +** +** ^When a connection (known as the blocked connection) fails to obtain a +** shared-cache lock and SQLITE_LOCKED is returned to the caller, the +** identity of the database connection (the blocking connection) that +** has locked the required resource is stored internally. ^After an +** application receives an SQLITE_LOCKED error, it may call the +** sqlite3_unlock_notify() method with the blocked connection handle as +** the first argument to register for a callback that will be invoked +** when the blocking connections current transaction is concluded. ^The +** callback is invoked from within the [sqlite3_step] or [sqlite3_close] +** call that concludes the blocking connections transaction. +** +** ^(If sqlite3_unlock_notify() is called in a multi-threaded application, +** there is a chance that the blocking connection will have already +** concluded its transaction by the time sqlite3_unlock_notify() is invoked. +** If this happens, then the specified callback is invoked immediately, +** from within the call to sqlite3_unlock_notify().)^ +** +** ^If the blocked connection is attempting to obtain a write-lock on a +** shared-cache table, and more than one other connection currently holds +** a read-lock on the same table, then SQLite arbitrarily selects one of +** the other connections to use as the blocking connection. +** +** ^(There may be at most one unlock-notify callback registered by a +** blocked connection. If sqlite3_unlock_notify() is called when the +** blocked connection already has a registered unlock-notify callback, +** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is +** called with a NULL pointer as its second argument, then any existing +** unlock-notify callback is canceled. ^The blocked connections +** unlock-notify callback may also be canceled by closing the blocked +** connection using [sqlite3_close()]. +** +** The unlock-notify callback is not reentrant. If an application invokes +** any sqlite3_xxx API functions from within an unlock-notify callback, a +** crash or deadlock may be the result. +** +** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always +** returns SQLITE_OK. +** +** Callback Invocation Details +** +** When an unlock-notify callback is registered, the application provides a +** single void* pointer that is passed to the callback when it is invoked. +** However, the signature of the callback function allows SQLite to pass +** it an array of void* context pointers. The first argument passed to +** an unlock-notify callback is a pointer to an array of void* pointers, +** and the second is the number of entries in the array. +** +** When a blocking connections transaction is concluded, there may be +** more than one blocked connection that has registered for an unlock-notify +** callback. ^If two or more such blocked connections have specified the +** same callback function, then instead of invoking the callback function +** multiple times, it is invoked once with the set of void* context pointers +** specified by the blocked connections bundled together into an array. +** This gives the application an opportunity to prioritize any actions +** related to the set of unblocked database connections. +** +** Deadlock Detection +** +** Assuming that after registering for an unlock-notify callback a +** database waits for the callback to be issued before taking any further +** action (a reasonable assumption), then using this API may cause the +** application to deadlock. For example, if connection X is waiting for +** connection Y's transaction to be concluded, and similarly connection +** Y is waiting on connection X's transaction, then neither connection +** will proceed and the system may remain deadlocked indefinitely. +** +** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock +** detection. ^If a given call to sqlite3_unlock_notify() would put the +** system in a deadlocked state, then SQLITE_LOCKED is returned and no +** unlock-notify callback is registered. The system is said to be in +** a deadlocked state if connection A has registered for an unlock-notify +** callback on the conclusion of connection B's transaction, and connection +** B has itself registered for an unlock-notify callback when connection +** A's transaction is concluded. ^Indirect deadlock is also detected, so +** the system is also considered to be deadlocked if connection B has +** registered for an unlock-notify callback on the conclusion of connection +** C's transaction, where connection C is waiting on connection A. ^Any +** number of levels of indirection are allowed. +** +** The "DROP TABLE" Exception +** +** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost +** always appropriate to call sqlite3_unlock_notify(). There is however, +** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement, +** SQLite checks if there are any currently executing SELECT statements +** that belong to the same connection. If there are, SQLITE_LOCKED is +** returned. In this case there is no "blocking connection", so invoking +** sqlite3_unlock_notify() results in the unlock-notify callback being +** invoked immediately. If the application then re-attempts the "DROP TABLE" +** or "DROP INDEX" query, an infinite loop might be the result. +** +** One way around this problem is to check the extended error code returned +** by an sqlite3_step() call. ^(If there is a blocking connection, then the +** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in +** the special "DROP TABLE/INDEX" case, the extended error code is just +** SQLITE_LOCKED.)^ +*/ +SQLITE_API int sqlite3_unlock_notify( + sqlite3 *pBlocked, /* Waiting connection */ + void (*xNotify)(void **apArg, int nArg), /* Callback function to invoke */ + void *pNotifyArg /* Argument to pass to xNotify */ +); + + +/* +** CAPI3REF: String Comparison +** +** ^The [sqlite3_strnicmp()] API allows applications and extensions to +** compare the contents of two buffers containing UTF-8 strings in a +** case-independent fashion, using the same definition of case independence +** that SQLite uses internally when comparing identifiers. +*/ +SQLITE_API int sqlite3_strnicmp(const char *, const char *, int); + +/* +** CAPI3REF: Error Logging Interface +** +** ^The [sqlite3_log()] interface writes a message into the error log +** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()]. +** ^If logging is enabled, the zFormat string and subsequent arguments are +** used with [sqlite3_snprintf()] to generate the final output string. +** +** The sqlite3_log() interface is intended for use by extensions such as +** virtual tables, collating functions, and SQL functions. While there is +** nothing to prevent an application from calling sqlite3_log(), doing so +** is considered bad form. +** +** The zFormat string must not be NULL. +** +** To avoid deadlocks and other threading problems, the sqlite3_log() routine +** will not use dynamically allocated memory. The log message is stored in +** a fixed-length buffer on the stack. If the log message is longer than +** a few hundred characters, it will be truncated to the length of the +** buffer. +*/ +SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...); + +/* +** CAPI3REF: Write-Ahead Log Commit Hook +** +** ^The [sqlite3_wal_hook()] function is used to register a callback that +** will be invoked each time a database connection commits data to a +** [write-ahead log] (i.e. whenever a transaction is committed in +** [journal_mode | journal_mode=WAL mode]). +** +** ^The callback is invoked by SQLite after the commit has taken place and +** the associated write-lock on the database released, so the implementation +** may read, write or [checkpoint] the database as required. +** +** ^The first parameter passed to the callback function when it is invoked +** is a copy of the third parameter passed to sqlite3_wal_hook() when +** registering the callback. ^The second is a copy of the database handle. +** ^The third parameter is the name of the database that was written to - +** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter +** is the number of pages currently in the write-ahead log file, +** including those that were just committed. +** +** The callback function should normally return [SQLITE_OK]. ^If an error +** code is returned, that error will propagate back up through the +** SQLite code base to cause the statement that provoked the callback +** to report an error, though the commit will have still occurred. If the +** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value +** that does not correspond to any valid SQLite error code, the results +** are undefined. +** +** A single database handle may have at most a single write-ahead log callback +** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any +** previously registered write-ahead log callback. ^Note that the +** [sqlite3_wal_autocheckpoint()] interface and the +** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will +** those overwrite any prior [sqlite3_wal_hook()] settings. +*/ +SQLITE_API void *sqlite3_wal_hook( + sqlite3*, + int(*)(void *,sqlite3*,const char*,int), + void* +); + +/* +** CAPI3REF: Configure an auto-checkpoint +** +** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around +** [sqlite3_wal_hook()] that causes any database on [database connection] D +** to automatically [checkpoint] +** after committing a transaction if there are N or +** more frames in the [write-ahead log] file. ^Passing zero or +** a negative value as the nFrame parameter disables automatic +** checkpoints entirely. +** +** ^The callback registered by this function replaces any existing callback +** registered using [sqlite3_wal_hook()]. ^Likewise, registering a callback +** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism +** configured by this function. +** +** ^The [wal_autocheckpoint pragma] can be used to invoke this interface +** from SQL. +** +** ^Every new [database connection] defaults to having the auto-checkpoint +** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT] +** pages. The use of this interface +** is only necessary if the default setting is found to be suboptimal +** for a particular application. +*/ +SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N); + +/* +** CAPI3REF: Checkpoint a database +** +** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X +** on [database connection] D to be [checkpointed]. ^If X is NULL or an +** empty string, then a checkpoint is run on all databases of +** connection D. ^If the database connection D is not in +** [WAL | write-ahead log mode] then this interface is a harmless no-op. +** +** ^The [wal_checkpoint pragma] can be used to invoke this interface +** from SQL. ^The [sqlite3_wal_autocheckpoint()] interface and the +** [wal_autocheckpoint pragma] can be used to cause this interface to be +** run whenever the WAL reaches a certain size threshold. +** +** See also: [sqlite3_wal_checkpoint_v2()] +*/ +SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); + +/* +** CAPI3REF: Checkpoint a database +** +** Run a checkpoint operation on WAL database zDb attached to database +** handle db. The specific operation is determined by the value of the +** eMode parameter: +** +**
    +**
    SQLITE_CHECKPOINT_PASSIVE
    +** Checkpoint as many frames as possible without waiting for any database +** readers or writers to finish. Sync the db file if all frames in the log +** are checkpointed. This mode is the same as calling +** sqlite3_wal_checkpoint(). The busy-handler callback is never invoked. +** +**
    SQLITE_CHECKPOINT_FULL
    +** This mode blocks (calls the busy-handler callback) until there is no +** database writer and all readers are reading from the most recent database +** snapshot. It then checkpoints all frames in the log file and syncs the +** database file. This call blocks database writers while it is running, +** but not database readers. +** +**
    SQLITE_CHECKPOINT_RESTART
    +** This mode works the same way as SQLITE_CHECKPOINT_FULL, except after +** checkpointing the log file it blocks (calls the busy-handler callback) +** until all readers are reading from the database file only. This ensures +** that the next client to write to the database file restarts the log file +** from the beginning. This call blocks database writers while it is running, +** but not database readers. +**
    +** +** If pnLog is not NULL, then *pnLog is set to the total number of frames in +** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to +** the total number of checkpointed frames (including any that were already +** checkpointed when this function is called). *pnLog and *pnCkpt may be +** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK. +** If no values are available because of an error, they are both set to -1 +** before returning to communicate this to the caller. +** +** All calls obtain an exclusive "checkpoint" lock on the database file. If +** any other process is running a checkpoint operation at the same time, the +** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a +** busy-handler configured, it will not be invoked in this case. +** +** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive +** "writer" lock on the database file. If the writer lock cannot be obtained +** immediately, and a busy-handler is configured, it is invoked and the writer +** lock retried until either the busy-handler returns 0 or the lock is +** successfully obtained. The busy-handler is also invoked while waiting for +** database readers as described above. If the busy-handler returns 0 before +** the writer lock is obtained or while waiting for database readers, the +** checkpoint operation proceeds from that point in the same way as +** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible +** without blocking any further. SQLITE_BUSY is returned in this case. +** +** If parameter zDb is NULL or points to a zero length string, then the +** specified operation is attempted on all WAL databases. In this case the +** values written to output parameters *pnLog and *pnCkpt are undefined. If +** an SQLITE_BUSY error is encountered when processing one or more of the +** attached WAL databases, the operation is still attempted on any remaining +** attached databases and SQLITE_BUSY is returned to the caller. If any other +** error occurs while processing an attached database, processing is abandoned +** and the error code returned to the caller immediately. If no error +** (SQLITE_BUSY or otherwise) is encountered while processing the attached +** databases, SQLITE_OK is returned. +** +** If database zDb is the name of an attached database that is not in WAL +** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If +** zDb is not NULL (or a zero length string) and is not the name of any +** attached database, SQLITE_ERROR is returned to the caller. +*/ +SQLITE_API int sqlite3_wal_checkpoint_v2( + sqlite3 *db, /* Database handle */ + const char *zDb, /* Name of attached database (or NULL) */ + int eMode, /* SQLITE_CHECKPOINT_* value */ + int *pnLog, /* OUT: Size of WAL log in frames */ + int *pnCkpt /* OUT: Total number of frames checkpointed */ +); + +/* +** CAPI3REF: Checkpoint operation parameters +** +** These constants can be used as the 3rd parameter to +** [sqlite3_wal_checkpoint_v2()]. See the [sqlite3_wal_checkpoint_v2()] +** documentation for additional information about the meaning and use of +** each of these values. +*/ +#define SQLITE_CHECKPOINT_PASSIVE 0 +#define SQLITE_CHECKPOINT_FULL 1 +#define SQLITE_CHECKPOINT_RESTART 2 + + +/* +** Undo the hack that converts floating point types to integer for +** builds on processors without floating point support. +*/ +#ifdef SQLITE_OMIT_FLOATING_POINT +# undef double +#endif + +#ifdef __cplusplus +} /* End of the 'extern "C"' block */ +#endif +#endif + +/* +** 2010 August 30 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +*/ + +#ifndef _SQLITE3RTREE_H_ +#define _SQLITE3RTREE_H_ + + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry; + +/* +** Register a geometry callback named zGeom that can be used as part of an +** R-Tree geometry query as follows: +** +** SELECT ... FROM WHERE MATCH $zGeom(... params ...) +*/ +SQLITE_API int sqlite3_rtree_geometry_callback( + sqlite3 *db, + const char *zGeom, + int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes), + void *pContext +); + + +/* +** A pointer to a structure of the following type is passed as the first +** argument to callbacks registered using rtree_geometry_callback(). +*/ +struct sqlite3_rtree_geometry { + void *pContext; /* Copy of pContext passed to s_r_g_c() */ + int nParam; /* Size of array aParam[] */ + double *aParam; /* Parameters passed to SQL geom function */ + void *pUser; /* Callback implementation user data */ + void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */ +}; + + +#ifdef __cplusplus +} /* end of the 'extern "C"' block */ +#endif + +#endif /* ifndef _SQLITE3RTREE_H_ */ + +// Try to read, but do not cache, every database page exactly once +// starting from page start up to and including the last page. +// +// If the scan completed without error then SQLITE_OK is returned. +// +// The scan will stop on the first error, set *pBadPage to the page number +// and return the error code. +// +// If the error code returned is SQLITE_CORRUPT then a pager codec +// error was encountered, and the following will be set: +// *pBadPageType will be -1 for unknown or >=0 for the bad page's +// page type accord to the pointer map. +// *pBadPageZero will be -1 for unknown, 1 if the bad page was all +// null bytes, and 0 if it was not. +SQLITE_API int tryReadEveryDbPage( + sqlite3 *db, + Pgno start, + Pgno *pBadPage, + int *pBadPageType, + int *pBadPageZero +); diff --git a/fdbserver/sqlite/sqlite3ext.h b/fdbserver/sqlite/sqlite3ext.h new file mode 100644 index 0000000000..e45e691fd6 --- /dev/null +++ b/fdbserver/sqlite/sqlite3ext.h @@ -0,0 +1,420 @@ +/* +** 2006 June 7 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** This header file defines the SQLite interface for use by +** shared libraries that want to be imported as extensions into +** an SQLite instance. Shared libraries that intend to be loaded +** as extensions by SQLite should #include this file instead of +** sqlite3.h. +*/ +#ifndef _SQLITE3EXT_H_ +#define _SQLITE3EXT_H_ +#include "sqlite3.h" + +typedef struct sqlite3_api_routines sqlite3_api_routines; + +/* +** The following structure holds pointers to all of the SQLite API +** routines. +** +** WARNING: In order to maintain backwards compatibility, add new +** interfaces to the end of this structure only. If you insert new +** interfaces in the middle of this structure, then older different +** versions of SQLite will not be able to load each others' shared +** libraries! +*/ +struct sqlite3_api_routines { + void * (*aggregate_context)(sqlite3_context*,int nBytes); + int (*aggregate_count)(sqlite3_context*); + int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*)); + int (*bind_double)(sqlite3_stmt*,int,double); + int (*bind_int)(sqlite3_stmt*,int,int); + int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64); + int (*bind_null)(sqlite3_stmt*,int); + int (*bind_parameter_count)(sqlite3_stmt*); + int (*bind_parameter_index)(sqlite3_stmt*,const char*zName); + const char * (*bind_parameter_name)(sqlite3_stmt*,int); + int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*)); + int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*)); + int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*); + int (*busy_handler)(sqlite3*,int(*)(void*,int),void*); + int (*busy_timeout)(sqlite3*,int ms); + int (*changes)(sqlite3*); + int (*close)(sqlite3*); + int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const char*)); + int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,int eTextRep,const void*)); + const void * (*column_blob)(sqlite3_stmt*,int iCol); + int (*column_bytes)(sqlite3_stmt*,int iCol); + int (*column_bytes16)(sqlite3_stmt*,int iCol); + int (*column_count)(sqlite3_stmt*pStmt); + const char * (*column_database_name)(sqlite3_stmt*,int); + const void * (*column_database_name16)(sqlite3_stmt*,int); + const char * (*column_decltype)(sqlite3_stmt*,int i); + const void * (*column_decltype16)(sqlite3_stmt*,int); + double (*column_double)(sqlite3_stmt*,int iCol); + int (*column_int)(sqlite3_stmt*,int iCol); + sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol); + const char * (*column_name)(sqlite3_stmt*,int); + const void * (*column_name16)(sqlite3_stmt*,int); + const char * (*column_origin_name)(sqlite3_stmt*,int); + const void * (*column_origin_name16)(sqlite3_stmt*,int); + const char * (*column_table_name)(sqlite3_stmt*,int); + const void * (*column_table_name16)(sqlite3_stmt*,int); + const unsigned char * (*column_text)(sqlite3_stmt*,int iCol); + const void * (*column_text16)(sqlite3_stmt*,int iCol); + int (*column_type)(sqlite3_stmt*,int iCol); + sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol); + void * (*commit_hook)(sqlite3*,int(*)(void*),void*); + int (*complete)(const char*sql); + int (*complete16)(const void*sql); + int (*create_collation)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*)); + int (*create_collation16)(sqlite3*,const void*,int,void*,int(*)(void*,int,const void*,int,const void*)); + int (*create_function)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*)); + int (*create_function16)(sqlite3*,const void*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*)); + int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*); + int (*data_count)(sqlite3_stmt*pStmt); + sqlite3 * (*db_handle)(sqlite3_stmt*); + int (*declare_vtab)(sqlite3*,const char*); + int (*enable_shared_cache)(int); + int (*errcode)(sqlite3*db); + const char * (*errmsg)(sqlite3*); + const void * (*errmsg16)(sqlite3*); + int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**); + int (*expired)(sqlite3_stmt*); + int (*finalize)(sqlite3_stmt*pStmt); + void (*free)(void*); + void (*free_table)(char**result); + int (*get_autocommit)(sqlite3*); + void * (*get_auxdata)(sqlite3_context*,int); + int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**); + int (*global_recover)(void); + void (*interruptx)(sqlite3*); + sqlite_int64 (*last_insert_rowid)(sqlite3*); + const char * (*libversion)(void); + int (*libversion_number)(void); + void *(*malloc)(int); + char * (*mprintf)(const char*,...); + int (*open)(const char*,sqlite3**); + int (*open16)(const void*,sqlite3**); + int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); + int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); + void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*); + void (*progress_handler)(sqlite3*,int,int(*)(void*),void*); + void *(*realloc)(void*,int); + int (*reset)(sqlite3_stmt*pStmt); + void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_double)(sqlite3_context*,double); + void (*result_error)(sqlite3_context*,const char*,int); + void (*result_error16)(sqlite3_context*,const void*,int); + void (*result_int)(sqlite3_context*,int); + void (*result_int64)(sqlite3_context*,sqlite_int64); + void (*result_null)(sqlite3_context*); + void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*)); + void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*)); + void (*result_value)(sqlite3_context*,sqlite3_value*); + void * (*rollback_hook)(sqlite3*,void(*)(void*),void*); + int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,const char*,const char*),void*); + void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*)); + char * (*snprintf)(int,char*,const char*,...); + int (*step)(sqlite3_stmt*); + int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,char const**,char const**,int*,int*,int*); + void (*thread_cleanup)(void); + int (*total_changes)(sqlite3*); + void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*); + int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*); + void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,sqlite_int64),void*); + void * (*user_data)(sqlite3_context*); + const void * (*value_blob)(sqlite3_value*); + int (*value_bytes)(sqlite3_value*); + int (*value_bytes16)(sqlite3_value*); + double (*value_double)(sqlite3_value*); + int (*value_int)(sqlite3_value*); + sqlite_int64 (*value_int64)(sqlite3_value*); + int (*value_numeric_type)(sqlite3_value*); + const unsigned char * (*value_text)(sqlite3_value*); + const void * (*value_text16)(sqlite3_value*); + const void * (*value_text16be)(sqlite3_value*); + const void * (*value_text16le)(sqlite3_value*); + int (*value_type)(sqlite3_value*); + char *(*vmprintf)(const char*,va_list); + /* Added ??? */ + int (*overload_function)(sqlite3*, const char *zFuncName, int nArg); + /* Added by 3.3.13 */ + int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**); + int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**); + int (*clear_bindings)(sqlite3_stmt*); + /* Added by 3.4.1 */ + int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,void (*xDestroy)(void *)); + /* Added by 3.5.0 */ + int (*bind_zeroblob)(sqlite3_stmt*,int,int); + int (*blob_bytes)(sqlite3_blob*); + int (*blob_close)(sqlite3_blob*); + int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,int,sqlite3_blob**); + int (*blob_read)(sqlite3_blob*,void*,int,int); + int (*blob_write)(sqlite3_blob*,const void*,int,int); + int (*create_collation_v2)(sqlite3*,const char*,int,void*,int(*)(void*,int,const void*,int,const void*),void(*)(void*)); + int (*file_control)(sqlite3*,const char*,int,void*); + sqlite3_int64 (*memory_highwater)(int); + sqlite3_int64 (*memory_used)(void); + sqlite3_mutex *(*mutex_alloc)(int); + void (*mutex_enter)(sqlite3_mutex*); + void (*mutex_free)(sqlite3_mutex*); + void (*mutex_leave)(sqlite3_mutex*); + int (*mutex_try)(sqlite3_mutex*); + int (*open_v2)(const char*,sqlite3**,int,const char*); + int (*release_memory)(int); + void (*result_error_nomem)(sqlite3_context*); + void (*result_error_toobig)(sqlite3_context*); + int (*sleep)(int); + void (*soft_heap_limit)(int); + sqlite3_vfs *(*vfs_find)(const char*); + int (*vfs_register)(sqlite3_vfs*,int); + int (*vfs_unregister)(sqlite3_vfs*); + int (*xthreadsafe)(void); + void (*result_zeroblob)(sqlite3_context*,int); + void (*result_error_code)(sqlite3_context*,int); + int (*test_control)(int, ...); + void (*randomness)(int,void*); + sqlite3 *(*context_db_handle)(sqlite3_context*); + int (*extended_result_codes)(sqlite3*,int); + int (*limit)(sqlite3*,int,int); + sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*); + const char *(*sql)(sqlite3_stmt*); + int (*status)(int,int*,int*,int); + int (*backup_finish)(sqlite3_backup*); + sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*); + int (*backup_pagecount)(sqlite3_backup*); + int (*backup_remaining)(sqlite3_backup*); + int (*backup_step)(sqlite3_backup*,int); + const char *(*compileoption_get)(int); + int (*compileoption_used)(const char*); + int (*create_function_v2)(sqlite3*,const char*,int,int,void*,void (*xFunc)(sqlite3_context*,int,sqlite3_value**),void (*xStep)(sqlite3_context*,int,sqlite3_value**),void (*xFinal)(sqlite3_context*),void(*xDestroy)(void*)); + int (*db_config)(sqlite3*,int,...); + sqlite3_mutex *(*db_mutex)(sqlite3*); + int (*db_status)(sqlite3*,int,int*,int*,int); + int (*extended_errcode)(sqlite3*); + void (*log)(int,const char*,...); + sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64); + const char *(*sourceid)(void); + int (*stmt_status)(sqlite3_stmt*,int,int); + int (*strnicmp)(const char*,const char*,int); + int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*); + int (*wal_autocheckpoint)(sqlite3*,int); + int (*wal_checkpoint)(sqlite3*,const char*); + void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*); +}; + +/* +** The following macros redefine the API routines so that they are +** redirected throught the global sqlite3_api structure. +** +** This header file is also used by the loadext.c source file +** (part of the main SQLite library - not an extension) so that +** it can get access to the sqlite3_api_routines structure +** definition. But the main library does not want to redefine +** the API. So the redefinition macros are only valid if the +** SQLITE_CORE macros is undefined. +*/ +#ifndef SQLITE_CORE +#define sqlite3_aggregate_context sqlite3_api->aggregate_context +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_aggregate_count sqlite3_api->aggregate_count +#endif +#define sqlite3_bind_blob sqlite3_api->bind_blob +#define sqlite3_bind_double sqlite3_api->bind_double +#define sqlite3_bind_int sqlite3_api->bind_int +#define sqlite3_bind_int64 sqlite3_api->bind_int64 +#define sqlite3_bind_null sqlite3_api->bind_null +#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count +#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index +#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name +#define sqlite3_bind_text sqlite3_api->bind_text +#define sqlite3_bind_text16 sqlite3_api->bind_text16 +#define sqlite3_bind_value sqlite3_api->bind_value +#define sqlite3_busy_handler sqlite3_api->busy_handler +#define sqlite3_busy_timeout sqlite3_api->busy_timeout +#define sqlite3_changes sqlite3_api->changes +#define sqlite3_close sqlite3_api->close +#define sqlite3_collation_needed sqlite3_api->collation_needed +#define sqlite3_collation_needed16 sqlite3_api->collation_needed16 +#define sqlite3_column_blob sqlite3_api->column_blob +#define sqlite3_column_bytes sqlite3_api->column_bytes +#define sqlite3_column_bytes16 sqlite3_api->column_bytes16 +#define sqlite3_column_count sqlite3_api->column_count +#define sqlite3_column_database_name sqlite3_api->column_database_name +#define sqlite3_column_database_name16 sqlite3_api->column_database_name16 +#define sqlite3_column_decltype sqlite3_api->column_decltype +#define sqlite3_column_decltype16 sqlite3_api->column_decltype16 +#define sqlite3_column_double sqlite3_api->column_double +#define sqlite3_column_int sqlite3_api->column_int +#define sqlite3_column_int64 sqlite3_api->column_int64 +#define sqlite3_column_name sqlite3_api->column_name +#define sqlite3_column_name16 sqlite3_api->column_name16 +#define sqlite3_column_origin_name sqlite3_api->column_origin_name +#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16 +#define sqlite3_column_table_name sqlite3_api->column_table_name +#define sqlite3_column_table_name16 sqlite3_api->column_table_name16 +#define sqlite3_column_text sqlite3_api->column_text +#define sqlite3_column_text16 sqlite3_api->column_text16 +#define sqlite3_column_type sqlite3_api->column_type +#define sqlite3_column_value sqlite3_api->column_value +#define sqlite3_commit_hook sqlite3_api->commit_hook +#define sqlite3_complete sqlite3_api->complete +#define sqlite3_complete16 sqlite3_api->complete16 +#define sqlite3_create_collation sqlite3_api->create_collation +#define sqlite3_create_collation16 sqlite3_api->create_collation16 +#define sqlite3_create_function sqlite3_api->create_function +#define sqlite3_create_function16 sqlite3_api->create_function16 +#define sqlite3_create_module sqlite3_api->create_module +#define sqlite3_create_module_v2 sqlite3_api->create_module_v2 +#define sqlite3_data_count sqlite3_api->data_count +#define sqlite3_db_handle sqlite3_api->db_handle +#define sqlite3_declare_vtab sqlite3_api->declare_vtab +#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache +#define sqlite3_errcode sqlite3_api->errcode +#define sqlite3_errmsg sqlite3_api->errmsg +#define sqlite3_errmsg16 sqlite3_api->errmsg16 +#define sqlite3_exec sqlite3_api->exec +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_expired sqlite3_api->expired +#endif +#define sqlite3_finalize sqlite3_api->finalize +#define sqlite3_free sqlite3_api->free +#define sqlite3_free_table sqlite3_api->free_table +#define sqlite3_get_autocommit sqlite3_api->get_autocommit +#define sqlite3_get_auxdata sqlite3_api->get_auxdata +#define sqlite3_get_table sqlite3_api->get_table +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_global_recover sqlite3_api->global_recover +#endif +#define sqlite3_interrupt sqlite3_api->interruptx +#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid +#define sqlite3_libversion sqlite3_api->libversion +#define sqlite3_libversion_number sqlite3_api->libversion_number +#define sqlite3_malloc sqlite3_api->malloc +#define sqlite3_mprintf sqlite3_api->mprintf +#define sqlite3_open sqlite3_api->open +#define sqlite3_open16 sqlite3_api->open16 +#define sqlite3_prepare sqlite3_api->prepare +#define sqlite3_prepare16 sqlite3_api->prepare16 +#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 +#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 +#define sqlite3_profile sqlite3_api->profile +#define sqlite3_progress_handler sqlite3_api->progress_handler +#define sqlite3_realloc sqlite3_api->realloc +#define sqlite3_reset sqlite3_api->reset +#define sqlite3_result_blob sqlite3_api->result_blob +#define sqlite3_result_double sqlite3_api->result_double +#define sqlite3_result_error sqlite3_api->result_error +#define sqlite3_result_error16 sqlite3_api->result_error16 +#define sqlite3_result_int sqlite3_api->result_int +#define sqlite3_result_int64 sqlite3_api->result_int64 +#define sqlite3_result_null sqlite3_api->result_null +#define sqlite3_result_text sqlite3_api->result_text +#define sqlite3_result_text16 sqlite3_api->result_text16 +#define sqlite3_result_text16be sqlite3_api->result_text16be +#define sqlite3_result_text16le sqlite3_api->result_text16le +#define sqlite3_result_value sqlite3_api->result_value +#define sqlite3_rollback_hook sqlite3_api->rollback_hook +#define sqlite3_set_authorizer sqlite3_api->set_authorizer +#define sqlite3_set_auxdata sqlite3_api->set_auxdata +#define sqlite3_snprintf sqlite3_api->snprintf +#define sqlite3_step sqlite3_api->step +#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata +#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup +#define sqlite3_total_changes sqlite3_api->total_changes +#define sqlite3_trace sqlite3_api->trace +#ifndef SQLITE_OMIT_DEPRECATED +#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings +#endif +#define sqlite3_update_hook sqlite3_api->update_hook +#define sqlite3_user_data sqlite3_api->user_data +#define sqlite3_value_blob sqlite3_api->value_blob +#define sqlite3_value_bytes sqlite3_api->value_bytes +#define sqlite3_value_bytes16 sqlite3_api->value_bytes16 +#define sqlite3_value_double sqlite3_api->value_double +#define sqlite3_value_int sqlite3_api->value_int +#define sqlite3_value_int64 sqlite3_api->value_int64 +#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type +#define sqlite3_value_text sqlite3_api->value_text +#define sqlite3_value_text16 sqlite3_api->value_text16 +#define sqlite3_value_text16be sqlite3_api->value_text16be +#define sqlite3_value_text16le sqlite3_api->value_text16le +#define sqlite3_value_type sqlite3_api->value_type +#define sqlite3_vmprintf sqlite3_api->vmprintf +#define sqlite3_overload_function sqlite3_api->overload_function +#define sqlite3_prepare_v2 sqlite3_api->prepare_v2 +#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2 +#define sqlite3_clear_bindings sqlite3_api->clear_bindings +#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob +#define sqlite3_blob_bytes sqlite3_api->blob_bytes +#define sqlite3_blob_close sqlite3_api->blob_close +#define sqlite3_blob_open sqlite3_api->blob_open +#define sqlite3_blob_read sqlite3_api->blob_read +#define sqlite3_blob_write sqlite3_api->blob_write +#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2 +#define sqlite3_file_control sqlite3_api->file_control +#define sqlite3_memory_highwater sqlite3_api->memory_highwater +#define sqlite3_memory_used sqlite3_api->memory_used +#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc +#define sqlite3_mutex_enter sqlite3_api->mutex_enter +#define sqlite3_mutex_free sqlite3_api->mutex_free +#define sqlite3_mutex_leave sqlite3_api->mutex_leave +#define sqlite3_mutex_try sqlite3_api->mutex_try +#define sqlite3_open_v2 sqlite3_api->open_v2 +#define sqlite3_release_memory sqlite3_api->release_memory +#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem +#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig +#define sqlite3_sleep sqlite3_api->sleep +#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit +#define sqlite3_vfs_find sqlite3_api->vfs_find +#define sqlite3_vfs_register sqlite3_api->vfs_register +#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister +#define sqlite3_threadsafe sqlite3_api->xthreadsafe +#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob +#define sqlite3_result_error_code sqlite3_api->result_error_code +#define sqlite3_test_control sqlite3_api->test_control +#define sqlite3_randomness sqlite3_api->randomness +#define sqlite3_context_db_handle sqlite3_api->context_db_handle +#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes +#define sqlite3_limit sqlite3_api->limit +#define sqlite3_next_stmt sqlite3_api->next_stmt +#define sqlite3_sql sqlite3_api->sql +#define sqlite3_status sqlite3_api->status +#define sqlite3_backup_finish sqlite3_api->backup_finish +#define sqlite3_backup_init sqlite3_api->backup_init +#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount +#define sqlite3_backup_remaining sqlite3_api->backup_remaining +#define sqlite3_backup_step sqlite3_api->backup_step +#define sqlite3_compileoption_get sqlite3_api->compileoption_get +#define sqlite3_compileoption_used sqlite3_api->compileoption_used +#define sqlite3_create_function_v2 sqlite3_api->create_function_v2 +#define sqlite3_db_config sqlite3_api->db_config +#define sqlite3_db_mutex sqlite3_api->db_mutex +#define sqlite3_db_status sqlite3_api->db_status +#define sqlite3_extended_errcode sqlite3_api->extended_errcode +#define sqlite3_log sqlite3_api->log +#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64 +#define sqlite3_sourceid sqlite3_api->sourceid +#define sqlite3_stmt_status sqlite3_api->stmt_status +#define sqlite3_strnicmp sqlite3_api->strnicmp +#define sqlite3_unlock_notify sqlite3_api->unlock_notify +#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint +#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint +#define sqlite3_wal_hook sqlite3_api->wal_hook +#endif /* SQLITE_CORE */ + +#define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api = 0; +#define SQLITE_EXTENSION_INIT2(v) sqlite3_api = v; + +#endif /* _SQLITE3EXT_H_ */ diff --git a/fdbserver/sqlite/sqliteInt.h b/fdbserver/sqlite/sqliteInt.h new file mode 100644 index 0000000000..1b0f531f60 --- /dev/null +++ b/fdbserver/sqlite/sqliteInt.h @@ -0,0 +1,3270 @@ +/* +** 2001 September 15 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +************************************************************************* +** Internal interface definitions for SQLite. +** +*/ +#ifndef _SQLITEINT_H_ +#define _SQLITEINT_H_ + +/* +** These #defines should enable >2GB file support on POSIX if the +** underlying operating system supports it. If the OS lacks +** large file support, or if the OS is windows, these should be no-ops. +** +** Ticket #2739: The _LARGEFILE_SOURCE macro must appear before any +** system #includes. Hence, this block of code must be the very first +** code in all source files. +** +** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch +** on the compiler command line. This is necessary if you are compiling +** on a recent machine (ex: Red Hat 7.2) but you want your code to work +** on an older machine (ex: Red Hat 6.0). If you compile on Red Hat 7.2 +** without this option, LFS is enable. But LFS does not exist in the kernel +** in Red Hat 6.0, so the code won't work. Hence, for maximum binary +** portability you should omit LFS. +** +** Similar is true for Mac OS X. LFS is only supported on Mac OS X 9 and later. +*/ +#ifndef SQLITE_DISABLE_LFS +# define _LARGE_FILE 1 +# ifndef _FILE_OFFSET_BITS +# define _FILE_OFFSET_BITS 64 +# endif +# define _LARGEFILE_SOURCE 1 +#endif + +/* +** Include the configuration header output by 'configure' if we're using the +** autoconf-based build +*/ +#ifdef _HAVE_SQLITE_CONFIG_H +#include "config.h" +#endif + +#include "sqliteLimit.h" + +/* Disable nuisance warnings on Borland compilers */ +#if defined(__BORLANDC__) +#pragma warn -rch /* unreachable code */ +#pragma warn -ccc /* Condition is always true or false */ +#pragma warn -aus /* Assigned value is never used */ +#pragma warn -csu /* Comparing signed and unsigned */ +#pragma warn -spa /* Suspicious pointer arithmetic */ +#endif + +/* Needed for various definitions... */ +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + +/* +** Include standard header files as necessary +*/ +#ifdef HAVE_STDINT_H +#include +#endif +#ifdef HAVE_INTTYPES_H +#include +#endif + +/* +** The number of samples of an index that SQLite takes in order to +** construct a histogram of the table content when running ANALYZE +** and with SQLITE_ENABLE_STAT2 +*/ +#define SQLITE_INDEX_SAMPLES 10 + +/* +** The following macros are used to cast pointers to integers and +** integers to pointers. The way you do this varies from one compiler +** to the next, so we have developed the following set of #if statements +** to generate appropriate macros for a wide range of compilers. +** +** The correct "ANSI" way to do this is to use the intptr_t type. +** Unfortunately, that typedef is not available on all compilers, or +** if it is available, it requires an #include of specific headers +** that vary from one machine to the next. +** +** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on +** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)). +** So we have to define the macros in different ways depending on the +** compiler. +*/ +#if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */ +# define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X)) +# define SQLITE_PTR_TO_INT(X) ((int)(__PTRDIFF_TYPE__)(X)) +#elif !defined(__GNUC__) /* Works for compilers other than LLVM */ +# define SQLITE_INT_TO_PTR(X) ((void*)&((char*)0)[X]) +# define SQLITE_PTR_TO_INT(X) ((int)(((char*)X)-(char*)0)) +#elif defined(HAVE_STDINT_H) /* Use this case if we have ANSI headers */ +# define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X)) +# define SQLITE_PTR_TO_INT(X) ((int)(intptr_t)(X)) +#else /* Generates a warning - but it always works */ +# define SQLITE_INT_TO_PTR(X) ((void*)(X)) +# define SQLITE_PTR_TO_INT(X) ((int)(X)) +#endif + +/* +** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2. +** 0 means mutexes are permanently disable and the library is never +** threadsafe. 1 means the library is serialized which is the highest +** level of threadsafety. 2 means the libary is multithreaded - multiple +** threads can use SQLite as long as no two threads try to use the same +** database connection at the same time. +** +** Older versions of SQLite used an optional THREADSAFE macro. +** We support that for legacy. +*/ +#if !defined(SQLITE_THREADSAFE) +#if defined(THREADSAFE) +# define SQLITE_THREADSAFE THREADSAFE +#else +# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */ +#endif +#endif + +/* +** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1. +** It determines whether or not the features related to +** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can +** be overridden at runtime using the sqlite3_config() API. +*/ +#if !defined(SQLITE_DEFAULT_MEMSTATUS) +# define SQLITE_DEFAULT_MEMSTATUS 1 +#endif + +/* +** Exactly one of the following macros must be defined in order to +** specify which memory allocation subsystem to use. +** +** SQLITE_SYSTEM_MALLOC // Use normal system malloc() +** SQLITE_MEMDEBUG // Debugging version of system malloc() +** +** (Historical note: There used to be several other options, but we've +** pared it down to just these two.) +** +** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as +** the default. +*/ +#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)>1 +# error "At most one of the following compile-time configuration options\ + is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG" +#endif +#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)==0 +# define SQLITE_SYSTEM_MALLOC 1 +#endif + +/* +** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the +** sizes of memory allocations below this value where possible. +*/ +#if !defined(SQLITE_MALLOC_SOFT_LIMIT) +# define SQLITE_MALLOC_SOFT_LIMIT 1024 +#endif + +/* +** We need to define _XOPEN_SOURCE as follows in order to enable +** recursive mutexes on most Unix systems. But Mac OS X is different. +** The _XOPEN_SOURCE define causes problems for Mac OS X we are told, +** so it is omitted there. See ticket #2673. +** +** Later we learn that _XOPEN_SOURCE is poorly or incorrectly +** implemented on some systems. So we avoid defining it at all +** if it is already defined or if it is unneeded because we are +** not doing a threadsafe build. Ticket #2681. +** +** See also ticket #2741. +*/ +#if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__) && SQLITE_THREADSAFE +# define _XOPEN_SOURCE 500 /* Needed to enable pthread recursive mutexes */ +#endif + +/* +** The TCL headers are only needed when compiling the TCL bindings. +*/ +#if defined(SQLITE_TCL) || defined(TCLSH) +# include +#endif + +/* +** Many people are failing to set -DNDEBUG=1 when compiling SQLite. +** Setting NDEBUG makes the code smaller and run faster. So the following +** lines are added to automatically set NDEBUG unless the -DSQLITE_DEBUG=1 +** option is set. Thus NDEBUG becomes an opt-in rather than an opt-out +** feature. +*/ +#if !defined(NDEBUG) && !defined(SQLITE_DEBUG) +# define NDEBUG 1 +#endif + +/* +** The testcase() macro is used to aid in coverage testing. When +** doing coverage testing, the condition inside the argument to +** testcase() must be evaluated both true and false in order to +** get full branch coverage. The testcase() macro is inserted +** to help ensure adequate test coverage in places where simple +** condition/decision coverage is inadequate. For example, testcase() +** can be used to make sure boundary values are tested. For +** bitmask tests, testcase() can be used to make sure each bit +** is significant and used at least once. On switch statements +** where multiple cases go to the same block of code, testcase() +** can insure that all cases are evaluated. +** +*/ +#ifdef SQLITE_COVERAGE_TEST + void sqlite3Coverage(int); +# define testcase(X) if( X ){ sqlite3Coverage(__LINE__); } +#else +# define testcase(X) +#endif + +/* +** The TESTONLY macro is used to enclose variable declarations or +** other bits of code that are needed to support the arguments +** within testcase() and assert() macros. +*/ +#if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST) +# define TESTONLY(X) X +#else +# define TESTONLY(X) +#endif + +/* +** Sometimes we need a small amount of code such as a variable initialization +** to setup for a later assert() statement. We do not want this code to +** appear when assert() is disabled. The following macro is therefore +** used to contain that setup code. The "VVA" acronym stands for +** "Verification, Validation, and Accreditation". In other words, the +** code within VVA_ONLY() will only run during verification processes. +*/ +#ifndef NDEBUG +# define VVA_ONLY(X) X +#else +# define VVA_ONLY(X) +#endif + +/* +** The ALWAYS and NEVER macros surround boolean expressions which +** are intended to always be true or false, respectively. Such +** expressions could be omitted from the code completely. But they +** are included in a few cases in order to enhance the resilience +** of SQLite to unexpected behavior - to make the code "self-healing" +** or "ductile" rather than being "brittle" and crashing at the first +** hint of unplanned behavior. +** +** In other words, ALWAYS and NEVER are added for defensive code. +** +** When doing coverage testing ALWAYS and NEVER are hard-coded to +** be true and false so that the unreachable code then specify will +** not be counted as untested code. +*/ +#if defined(SQLITE_COVERAGE_TEST) +# define ALWAYS(X) (1) +# define NEVER(X) (0) +#elif !defined(NDEBUG) +# define ALWAYS(X) ((X)?1:(assert(0),0)) +# define NEVER(X) ((X)?(assert(0),1):0) +#else +# define ALWAYS(X) (X) +# define NEVER(X) (X) +#endif + +/* +** Return true (non-zero) if the input is a integer that is too large +** to fit in 32-bits. This macro is used inside of various testcase() +** macros to verify that we have tested SQLite for large-file support. +*/ +#define IS_BIG_INT(X) (((X)&~(i64)0xffffffff)!=0) + +/* +** The macro unlikely() is a hint that surrounds a boolean +** expression that is usually false. Macro likely() surrounds +** a boolean expression that is usually true. GCC is able to +** use these hints to generate better code, sometimes. +*/ +#if defined(__GNUC__) && 0 +# define likely(X) __builtin_expect((X),1) +# define unlikely(X) __builtin_expect((X),0) +#else +# define likely(X) !!(X) +# define unlikely(X) !!(X) +#endif + +#include "sqlite3.h" +#include "hash.h" +#include +#include +#include +#include +#include + +/* +** If compiling for a processor that lacks floating point support, +** substitute integer for floating-point +*/ +#ifdef SQLITE_OMIT_FLOATING_POINT +# define double sqlite_int64 +# define float sqlite_int64 +# define LONGDOUBLE_TYPE sqlite_int64 +# ifndef SQLITE_BIG_DBL +# define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50) +# endif +# define SQLITE_OMIT_DATETIME_FUNCS 1 +# define SQLITE_OMIT_TRACE 1 +# undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT +# undef SQLITE_HAVE_ISNAN +#endif +#ifndef SQLITE_BIG_DBL +# define SQLITE_BIG_DBL (1e99) +#endif + +/* +** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0 +** afterward. Having this macro allows us to cause the C compiler +** to omit code used by TEMP tables without messy #ifndef statements. +*/ +#ifdef SQLITE_OMIT_TEMPDB +#define OMIT_TEMPDB 1 +#else +#define OMIT_TEMPDB 0 +#endif + +/* +** The "file format" number is an integer that is incremented whenever +** the VDBE-level file format changes. The following macros define the +** the default file format for new databases and the maximum file format +** that the library can read. +*/ +#define SQLITE_MAX_FILE_FORMAT 4 +#ifndef SQLITE_DEFAULT_FILE_FORMAT +# define SQLITE_DEFAULT_FILE_FORMAT 1 +#endif + +/* +** Determine whether triggers are recursive by default. This can be +** changed at run-time using a pragma. +*/ +#ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS +# define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0 +#endif + +/* +** Provide a default value for SQLITE_TEMP_STORE in case it is not specified +** on the command-line +*/ +#ifndef SQLITE_TEMP_STORE +# define SQLITE_TEMP_STORE 1 +#endif + +/* +** GCC does not define the offsetof() macro so we'll have to do it +** ourselves. +*/ +#ifndef offsetof +#define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD)) +#endif + +/* +** Check to see if this machine uses EBCDIC. (Yes, believe it or +** not, there are still machines out there that use EBCDIC.) +*/ +#if 'A' == '\301' +# define SQLITE_EBCDIC 1 +#else +# define SQLITE_ASCII 1 +#endif + +/* +** Integers of known sizes. These typedefs might change for architectures +** where the sizes very. Preprocessor macros are available so that the +** types can be conveniently redefined at compile-type. Like this: +** +** cc '-DUINTPTR_TYPE=long long int' ... +*/ +#ifndef UINT32_TYPE +# ifdef HAVE_UINT32_T +# define UINT32_TYPE uint32_t +# else +# define UINT32_TYPE unsigned int +# endif +#endif +#ifndef UINT16_TYPE +# ifdef HAVE_UINT16_T +# define UINT16_TYPE uint16_t +# else +# define UINT16_TYPE unsigned short int +# endif +#endif +#ifndef INT16_TYPE +# ifdef HAVE_INT16_T +# define INT16_TYPE int16_t +# else +# define INT16_TYPE short int +# endif +#endif +#ifndef UINT8_TYPE +# ifdef HAVE_UINT8_T +# define UINT8_TYPE uint8_t +# else +# define UINT8_TYPE unsigned char +# endif +#endif +#ifndef INT8_TYPE +# ifdef HAVE_INT8_T +# define INT8_TYPE int8_t +# else +# define INT8_TYPE signed char +# endif +#endif +#ifndef LONGDOUBLE_TYPE +# define LONGDOUBLE_TYPE long double +#endif +typedef sqlite_int64 i64; /* 8-byte signed integer */ +typedef sqlite_uint64 u64; /* 8-byte unsigned integer */ +typedef UINT32_TYPE u32; /* 4-byte unsigned integer */ +typedef UINT16_TYPE u16; /* 2-byte unsigned integer */ +typedef INT16_TYPE i16; /* 2-byte signed integer */ +typedef UINT8_TYPE u8; /* 1-byte unsigned integer */ +typedef INT8_TYPE i8; /* 1-byte signed integer */ + +/* +** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value +** that can be stored in a u32 without loss of data. The value +** is 0x00000000ffffffff. But because of quirks of some compilers, we +** have to specify the value in the less intuitive manner shown: +*/ +#define SQLITE_MAX_U32 ((((u64)1)<<32)-1) + +/* +** Macros to determine whether the machine is big or little endian, +** evaluated at runtime. +*/ +#ifdef SQLITE_AMALGAMATION +const int sqlite3one = 1; +#else +extern const int sqlite3one; +#endif +#if defined(i386) || defined(__i386__) || defined(_M_IX86)\ + || defined(__x86_64) || defined(__x86_64__) +# define SQLITE_BIGENDIAN 0 +# define SQLITE_LITTLEENDIAN 1 +# define SQLITE_UTF16NATIVE SQLITE_UTF16LE +#else +# define SQLITE_BIGENDIAN (*(char *)(&sqlite3one)==0) +# define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1) +# define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE) +#endif + +/* +** Constants for the largest and smallest possible 64-bit signed integers. +** These macros are designed to work correctly on both 32-bit and 64-bit +** compilers. +*/ +#define LARGEST_INT64 (0xffffffff|(((i64)0x7fffffff)<<32)) +#define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64) + +/* +** Round up a number to the next larger multiple of 8. This is used +** to force 8-byte alignment on 64-bit architectures. +*/ +#define ROUND8(x) (((x)+7)&~7) + +/* +** Round down to the nearest multiple of 8 +*/ +#define ROUNDDOWN8(x) ((x)&~7) + +/* +** Assert that the pointer X is aligned to an 8-byte boundary. This +** macro is used only within assert() to verify that the code gets +** all alignment restrictions correct. +** +** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the +** underlying malloc() implemention might return us 4-byte aligned +** pointers. In that case, only verify 4-byte alignment. +*/ +#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC +# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0) +#else +# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0) +#endif + + +/* +** An instance of the following structure is used to store the busy-handler +** callback for a given sqlite handle. +** +** The sqlite.busyHandler member of the sqlite struct contains the busy +** callback for the database handle. Each pager opened via the sqlite +** handle is passed a pointer to sqlite.busyHandler. The busy-handler +** callback is currently invoked only from within pager.c. +*/ +typedef struct BusyHandler BusyHandler; +struct BusyHandler { + int (*xFunc)(void *,int); /* The busy callback */ + void *pArg; /* First arg to busy callback */ + int nBusy; /* Incremented with each busy call */ +}; + +/* +** Name of the master database table. The master database table +** is a special table that holds the names and attributes of all +** user tables and indices. +*/ +#define MASTER_NAME "sqlite_master" +#define TEMP_MASTER_NAME "sqlite_temp_master" + +/* +** The root-page of the master database table. +*/ +#define MASTER_ROOT 1 + +/* +** The name of the schema table. +*/ +#define SCHEMA_TABLE(x) ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME) + +/* +** A convenience macro that returns the number of elements in +** an array. +*/ +#define ArraySize(X) ((int)(sizeof(X)/sizeof(X[0]))) + +/* +** The following value as a destructor means to use sqlite3DbFree(). +** This is an internal extension to SQLITE_STATIC and SQLITE_TRANSIENT. +*/ +#define SQLITE_DYNAMIC ((sqlite3_destructor_type)sqlite3DbFree) + +/* +** When SQLITE_OMIT_WSD is defined, it means that the target platform does +** not support Writable Static Data (WSD) such as global and static variables. +** All variables must either be on the stack or dynamically allocated from +** the heap. When WSD is unsupported, the variable declarations scattered +** throughout the SQLite code must become constants instead. The SQLITE_WSD +** macro is used for this purpose. And instead of referencing the variable +** directly, we use its constant as a key to lookup the run-time allocated +** buffer that holds real variable. The constant is also the initializer +** for the run-time allocated buffer. +** +** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL +** macros become no-ops and have zero performance impact. +*/ +#ifdef SQLITE_OMIT_WSD + #define SQLITE_WSD const + #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v))) + #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config) + int sqlite3_wsd_init(int N, int J); + void *sqlite3_wsd_find(void *K, int L); +#else + #define SQLITE_WSD + #define GLOBAL(t,v) v + #define sqlite3GlobalConfig sqlite3Config +#endif + +/* +** The following macros are used to suppress compiler warnings and to +** make it clear to human readers when a function parameter is deliberately +** left unused within the body of a function. This usually happens when +** a function is called via a function pointer. For example the +** implementation of an SQL aggregate step callback may not use the +** parameter indicating the number of arguments passed to the aggregate, +** if it knows that this is enforced elsewhere. +** +** When a function parameter is not used at all within the body of a function, +** it is generally named "NotUsed" or "NotUsed2" to make things even clearer. +** However, these macros may also be used to suppress warnings related to +** parameters that may or may not be used depending on compilation options. +** For example those parameters only used in assert() statements. In these +** cases the parameters are named as per the usual conventions. +*/ +#define UNUSED_PARAMETER(x) (void)(x) +#define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y) + +/* +** Forward references to structures +*/ +typedef struct AggInfo AggInfo; +typedef struct AuthContext AuthContext; +typedef struct AutoincInfo AutoincInfo; +typedef struct Bitvec Bitvec; +typedef struct CollSeq CollSeq; +typedef struct Column Column; +typedef struct Db Db; +typedef struct Schema Schema; +typedef struct Expr Expr; +typedef struct ExprList ExprList; +typedef struct ExprSpan ExprSpan; +typedef struct FKey FKey; +typedef struct FuncDestructor FuncDestructor; +typedef struct FuncDef FuncDef; +typedef struct FuncDefHash FuncDefHash; +typedef struct IdList IdList; +typedef struct Index Index; +typedef struct IndexSample IndexSample; +typedef struct KeyClass KeyClass; +typedef struct KeyInfo KeyInfo; +typedef struct Lookaside Lookaside; +typedef struct LookasideSlot LookasideSlot; +typedef struct Module Module; +typedef struct NameContext NameContext; +typedef struct Parse Parse; +typedef struct RowSet RowSet; +typedef struct Savepoint Savepoint; +typedef struct Select Select; +typedef struct SrcList SrcList; +typedef struct StrAccum StrAccum; +typedef struct Table Table; +typedef struct TableLock TableLock; +typedef struct Token Token; +typedef struct Trigger Trigger; +typedef struct TriggerPrg TriggerPrg; +typedef struct TriggerStep TriggerStep; +typedef struct UnpackedRecord UnpackedRecord; +typedef struct VTable VTable; +typedef struct Walker Walker; +typedef struct WherePlan WherePlan; +typedef struct WhereInfo WhereInfo; +typedef struct WhereLevel WhereLevel; + +/* +** Defer sourcing vdbe.h and btree.h until after the "u8" and +** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque +** pointer types (i.e. FuncDef) defined above. +*/ +#include "btree.h" +#if 0 +#include "vdbe.h" +#include "pager.h" +#include "pcache.h" + +#include "os.h" +#include "mutex.h" +#endif + +/* +** Each database file to be accessed by the system is an instance +** of the following structure. There are normally two of these structures +** in the sqlite.aDb[] array. aDb[0] is the main database file and +** aDb[1] is the database file used to hold temporary tables. Additional +** databases may be attached. +*/ +struct Db { + char *zName; /* Name of this database */ + Btree *pBt; /* The B*Tree structure for this database file */ + u8 inTrans; /* 0: not writable. 1: Transaction. 2: Checkpoint */ + u8 safety_level; /* How aggressive at syncing data to disk */ + Schema *pSchema; /* Pointer to database schema (possibly shared) */ +}; + +/* +** An instance of the following structure stores a database schema. +** +** Most Schema objects are associated with a Btree. The exception is +** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing. +** In shared cache mode, a single Schema object can be shared by multiple +** Btrees that refer to the same underlying BtShared object. +** +** Schema objects are automatically deallocated when the last Btree that +** references them is destroyed. The TEMP Schema is manually freed by +** sqlite3_close(). +* +** A thread must be holding a mutex on the corresponding Btree in order +** to access Schema content. This implies that the thread must also be +** holding a mutex on the sqlite3 connection pointer that owns the Btree. +** For a TEMP Schema, on the connection mutex is required. +*/ +struct Schema { + int schema_cookie; /* Database schema version number for this file */ + int iGeneration; /* Generation counter. Incremented with each change */ + Hash tblHash; /* All tables indexed by name */ + Hash idxHash; /* All (named) indices indexed by name */ + Hash trigHash; /* All triggers indexed by name */ + Hash fkeyHash; /* All foreign keys by referenced table name */ + Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */ + u8 file_format; /* Schema format version for this file */ + u8 enc; /* Text encoding used by this database */ + u16 flags; /* Flags associated with this schema */ + int cache_size; /* Number of pages to use in the cache */ +}; + +/* +** These macros can be used to test, set, or clear bits in the +** Db.pSchema->flags field. +*/ +#define DbHasProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))==(P)) +#define DbHasAnyProperty(D,I,P) (((D)->aDb[I].pSchema->flags&(P))!=0) +#define DbSetProperty(D,I,P) (D)->aDb[I].pSchema->flags|=(P) +#define DbClearProperty(D,I,P) (D)->aDb[I].pSchema->flags&=~(P) + +/* +** Allowed values for the DB.pSchema->flags field. +** +** The DB_SchemaLoaded flag is set after the database schema has been +** read into internal hash tables. +** +** DB_UnresetViews means that one or more views have column names that +** have been filled out. If the schema changes, these column names might +** changes and so the view will need to be reset. +*/ +#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */ +#define DB_UnresetViews 0x0002 /* Some views have defined column names */ +#define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */ + +/* +** The number of different kinds of things that can be limited +** using the sqlite3_limit() interface. +*/ +#define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1) + +/* +** Lookaside malloc is a set of fixed-size buffers that can be used +** to satisfy small transient memory allocation requests for objects +** associated with a particular database connection. The use of +** lookaside malloc provides a significant performance enhancement +** (approx 10%) by avoiding numerous malloc/free requests while parsing +** SQL statements. +** +** The Lookaside structure holds configuration information about the +** lookaside malloc subsystem. Each available memory allocation in +** the lookaside subsystem is stored on a linked list of LookasideSlot +** objects. +** +** Lookaside allocations are only allowed for objects that are associated +** with a particular database connection. Hence, schema information cannot +** be stored in lookaside because in shared cache mode the schema information +** is shared by multiple database connections. Therefore, while parsing +** schema information, the Lookaside.bEnabled flag is cleared so that +** lookaside allocations are not used to construct the schema objects. +*/ +struct Lookaside { + u16 sz; /* Size of each buffer in bytes */ + u8 bEnabled; /* False to disable new lookaside allocations */ + u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */ + int nOut; /* Number of buffers currently checked out */ + int mxOut; /* Highwater mark for nOut */ + int anStat[3]; /* 0: hits. 1: size misses. 2: full misses */ + LookasideSlot *pFree; /* List of available buffers */ + void *pStart; /* First byte of available memory space */ + void *pEnd; /* First byte past end of available space */ +}; +struct LookasideSlot { + LookasideSlot *pNext; /* Next buffer in the list of free buffers */ +}; + +/* +** A hash table for function definitions. +** +** Hash each FuncDef structure into one of the FuncDefHash.a[] slots. +** Collisions are on the FuncDef.pHash chain. +*/ +struct FuncDefHash { + FuncDef *a[23]; /* Hash table for functions */ +}; + +/* +** Each database connection is an instance of the following structure. +** +** The sqlite.lastRowid records the last insert rowid generated by an +** insert statement. Inserts on views do not affect its value. Each +** trigger has its own context, so that lastRowid can be updated inside +** triggers as usual. The previous value will be restored once the trigger +** exits. Upon entering a before or instead of trigger, lastRowid is no +** longer (since after version 2.8.12) reset to -1. +** +** The sqlite.nChange does not count changes within triggers and keeps no +** context. It is reset at start of sqlite3_exec. +** The sqlite.lsChange represents the number of changes made by the last +** insert, update, or delete statement. It remains constant throughout the +** length of a statement and is then updated by OP_SetCounts. It keeps a +** context stack just like lastRowid so that the count of changes +** within a trigger is not seen outside the trigger. Changes to views do not +** affect the value of lsChange. +** The sqlite.csChange keeps track of the number of current changes (since +** the last statement) and is used to update sqlite_lsChange. +** +** The member variables sqlite.errCode, sqlite.zErrMsg and sqlite.zErrMsg16 +** store the most recent error code and, if applicable, string. The +** internal function sqlite3Error() is used to set these variables +** consistently. +*/ +struct sqlite3 { + sqlite3_vfs *pVfs; /* OS Interface */ + int nDb; /* Number of backends currently in use */ + Db *aDb; /* All backends */ + int flags; /* Miscellaneous flags. See below */ + int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */ + int errCode; /* Most recent error code (SQLITE_*) */ + int errMask; /* & result codes with this before returning */ + u8 autoCommit; /* The auto-commit flag. */ + u8 temp_store; /* 1: file 2: memory 0: default */ + u8 mallocFailed; /* True if we have seen a malloc failure */ + u8 dfltLockMode; /* Default locking-mode for attached dbs */ + signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */ + u8 suppressErr; /* Do not issue error messages if true */ + int nextPagesize; /* Pagesize after VACUUM if >0 */ + int nTable; /* Number of tables in the database */ + CollSeq *pDfltColl; /* The default collating sequence (BINARY) */ + i64 lastRowid; /* ROWID of most recent insert (see above) */ + u32 magic; /* Magic number for detect library misuse */ + int nChange; /* Value returned by sqlite3_changes() */ + int nTotalChange; /* Value returned by sqlite3_total_changes() */ + sqlite3_mutex *mutex; /* Connection mutex */ + int aLimit[SQLITE_N_LIMIT]; /* Limits */ + struct sqlite3InitInfo { /* Information used during initialization */ + int iDb; /* When back is being initialized */ + int newTnum; /* Rootpage of table being initialized */ + u8 busy; /* TRUE if currently initializing */ + u8 orphanTrigger; /* Last statement is orphaned TEMP trigger */ + } init; + int nExtension; /* Number of loaded extensions */ + void **aExtension; /* Array of shared library handles */ + struct Vdbe *pVdbe; /* List of active virtual machines */ + int activeVdbeCnt; /* Number of VDBEs currently executing */ + int writeVdbeCnt; /* Number of active VDBEs that are writing */ + int vdbeExecCnt; /* Number of nested calls to VdbeExec() */ + void (*xTrace)(void*,const char*); /* Trace function */ + void *pTraceArg; /* Argument to the trace function */ + void (*xProfile)(void*,const char*,u64); /* Profiling function */ + void *pProfileArg; /* Argument to profile function */ + void *pCommitArg; /* Argument to xCommitCallback() */ + int (*xCommitCallback)(void*); /* Invoked at every commit. */ + void *pRollbackArg; /* Argument to xRollbackCallback() */ + void (*xRollbackCallback)(void*); /* Invoked at every commit. */ + void *pUpdateArg; + void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64); +#ifndef SQLITE_OMIT_WAL + int (*xWalCallback)(void *, sqlite3 *, const char *, int); + void *pWalArg; +#endif + void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*); + void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*); + void *pCollNeededArg; + sqlite3_value *pErr; /* Most recent error message */ + char *zErrMsg; /* Most recent error message (UTF-8 encoded) */ + char *zErrMsg16; /* Most recent error message (UTF-16 encoded) */ + union { + volatile int isInterrupted; /* True if sqlite3_interrupt has been called */ + double notUsed1; /* Spacer */ + } u1; + Lookaside lookaside; /* Lookaside malloc configuration */ +#ifndef SQLITE_OMIT_AUTHORIZATION + int (*xAuth)(void*,int,const char*,const char*,const char*,const char*); + /* Access authorization function */ + void *pAuthArg; /* 1st argument to the access auth function */ +#endif +#ifndef SQLITE_OMIT_PROGRESS_CALLBACK + int (*xProgress)(void *); /* The progress callback */ + void *pProgressArg; /* Argument to the progress callback */ + int nProgressOps; /* Number of opcodes for progress callback */ +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + Hash aModule; /* populated by sqlite3_create_module() */ + Table *pVTab; /* vtab with active Connect/Create method */ + VTable **aVTrans; /* Virtual tables with open transactions */ + int nVTrans; /* Allocated size of aVTrans */ + VTable *pDisconnect; /* Disconnect these in next sqlite3_prepare() */ +#endif + FuncDefHash aFunc; /* Hash table of connection functions */ + Hash aCollSeq; /* All collating sequences */ + BusyHandler busyHandler; /* Busy callback */ + int busyTimeout; /* Busy handler timeout, in msec */ + Db aDbStatic[2]; /* Static space for the 2 default backends */ + Savepoint *pSavepoint; /* List of active savepoints */ + int nSavepoint; /* Number of non-transaction savepoints */ + int nStatement; /* Number of nested statement-transactions */ + u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */ + i64 nDeferredCons; /* Net deferred constraints this transaction. */ + int *pnBytesFreed; /* If not NULL, increment this in DbFree() */ + +#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY + /* The following variables are all protected by the STATIC_MASTER + ** mutex, not by sqlite3.mutex. They are used by code in notify.c. + ** + ** When X.pUnlockConnection==Y, that means that X is waiting for Y to + ** unlock so that it can proceed. + ** + ** When X.pBlockingConnection==Y, that means that something that X tried + ** tried to do recently failed with an SQLITE_LOCKED error due to locks + ** held by Y. + */ + sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */ + sqlite3 *pUnlockConnection; /* Connection to watch for unlock */ + void *pUnlockArg; /* Argument to xUnlockNotify */ + void (*xUnlockNotify)(void **, int); /* Unlock notify callback */ + sqlite3 *pNextBlocked; /* Next in list of all blocked connections */ +#endif +}; + +#if 0 +/* +** A macro to discover the encoding of a database. +*/ +#define ENC(db) ((db)->aDb[0].pSchema->enc) + +/* +** Possible values for the sqlite3.flags. +*/ +#define SQLITE_VdbeTrace 0x00000100 /* True to trace VDBE execution */ +#define SQLITE_InternChanges 0x00000200 /* Uncommitted Hash table changes */ +#define SQLITE_FullColNames 0x00000400 /* Show full column names on SELECT */ +#define SQLITE_ShortColNames 0x00000800 /* Show short columns names */ +#define SQLITE_CountRows 0x00001000 /* Count rows changed by INSERT, */ + /* DELETE, or UPDATE and return */ + /* the count using a callback. */ +#define SQLITE_NullCallback 0x00002000 /* Invoke the callback once if the */ + /* result set is empty */ +#define SQLITE_SqlTrace 0x00004000 /* Debug print SQL as it executes */ +#define SQLITE_VdbeListing 0x00008000 /* Debug listings of VDBE programs */ +#define SQLITE_WriteSchema 0x00010000 /* OK to update SQLITE_MASTER */ +#define SQLITE_NoReadlock 0x00020000 /* Readlocks are omitted when + ** accessing read-only databases */ +#define SQLITE_IgnoreChecks 0x00040000 /* Do not enforce check constraints */ +#define SQLITE_ReadUncommitted 0x0080000 /* For shared-cache mode */ +#define SQLITE_LegacyFileFmt 0x00100000 /* Create new databases in format 1 */ +#define SQLITE_FullFSync 0x00200000 /* Use full fsync on the backend */ +#define SQLITE_CkptFullFSync 0x00400000 /* Use full fsync for checkpoint */ +#define SQLITE_RecoveryMode 0x00800000 /* Ignore schema errors */ +#define SQLITE_ReverseOrder 0x01000000 /* Reverse unordered SELECTs */ +#define SQLITE_RecTriggers 0x02000000 /* Enable recursive triggers */ +#define SQLITE_ForeignKeys 0x04000000 /* Enforce foreign key constraints */ +#define SQLITE_AutoIndex 0x08000000 /* Enable automatic indexes */ +#define SQLITE_PreferBuiltin 0x10000000 /* Preference to built-in funcs */ +#define SQLITE_LoadExtension 0x20000000 /* Enable load_extension */ +#define SQLITE_EnableTrigger 0x40000000 /* True to enable triggers */ + +/* +** Bits of the sqlite3.flags field that are used by the +** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface. +** These must be the low-order bits of the flags field. +*/ +#define SQLITE_QueryFlattener 0x01 /* Disable query flattening */ +#define SQLITE_ColumnCache 0x02 /* Disable the column cache */ +#define SQLITE_IndexSort 0x04 /* Disable indexes for sorting */ +#define SQLITE_IndexSearch 0x08 /* Disable indexes for searching */ +#define SQLITE_IndexCover 0x10 /* Disable index covering table */ +#define SQLITE_GroupByOrder 0x20 /* Disable GROUPBY cover of ORDERBY */ +#define SQLITE_FactorOutConst 0x40 /* Disable factoring out constants */ +#define SQLITE_OptMask 0xff /* Mask of all disablable opts */ + +/* +** Possible values for the sqlite.magic field. +** The numbers are obtained at random and have no special meaning, other +** than being distinct from one another. +*/ +#define SQLITE_MAGIC_OPEN 0xa029a697 /* Database is open */ +#define SQLITE_MAGIC_CLOSED 0x9f3c2d33 /* Database is closed */ +#define SQLITE_MAGIC_SICK 0x4b771290 /* Error and awaiting close */ +#define SQLITE_MAGIC_BUSY 0xf03b7906 /* Database currently in use */ +#define SQLITE_MAGIC_ERROR 0xb5357930 /* An SQLITE_MISUSE error occurred */ + +/* +** Each SQL function is defined by an instance of the following +** structure. A pointer to this structure is stored in the sqlite.aFunc +** hash table. When multiple functions have the same name, the hash table +** points to a linked list of these structures. +*/ +struct FuncDef { + i16 nArg; /* Number of arguments. -1 means unlimited */ + u8 iPrefEnc; /* Preferred text encoding (SQLITE_UTF8, 16LE, 16BE) */ + u8 flags; /* Some combination of SQLITE_FUNC_* */ + void *pUserData; /* User data parameter */ + FuncDef *pNext; /* Next function with same name */ + void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */ + void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */ + void (*xFinalize)(sqlite3_context*); /* Aggregate finalizer */ + char *zName; /* SQL name of the function. */ + FuncDef *pHash; /* Next with a different name but the same hash */ + FuncDestructor *pDestructor; /* Reference counted destructor function */ +}; + +/* +** This structure encapsulates a user-function destructor callback (as +** configured using create_function_v2()) and a reference counter. When +** create_function_v2() is called to create a function with a destructor, +** a single object of this type is allocated. FuncDestructor.nRef is set to +** the number of FuncDef objects created (either 1 or 3, depending on whether +** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor +** member of each of the new FuncDef objects is set to point to the allocated +** FuncDestructor. +** +** Thereafter, when one of the FuncDef objects is deleted, the reference +** count on this object is decremented. When it reaches 0, the destructor +** is invoked and the FuncDestructor structure freed. +*/ +struct FuncDestructor { + int nRef; + void (*xDestroy)(void *); + void *pUserData; +}; + +/* +** Possible values for FuncDef.flags +*/ +#define SQLITE_FUNC_LIKE 0x01 /* Candidate for the LIKE optimization */ +#define SQLITE_FUNC_CASE 0x02 /* Case-sensitive LIKE-type function */ +#define SQLITE_FUNC_EPHEM 0x04 /* Ephemeral. Delete with VDBE */ +#define SQLITE_FUNC_NEEDCOLL 0x08 /* sqlite3GetFuncCollSeq() might be called */ +#define SQLITE_FUNC_PRIVATE 0x10 /* Allowed for internal use only */ +#define SQLITE_FUNC_COUNT 0x20 /* Built-in count(*) aggregate */ +#define SQLITE_FUNC_COALESCE 0x40 /* Built-in coalesce() or ifnull() function */ + +/* +** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are +** used to create the initializers for the FuncDef structures. +** +** FUNCTION(zName, nArg, iArg, bNC, xFunc) +** Used to create a scalar function definition of a function zName +** implemented by C function xFunc that accepts nArg arguments. The +** value passed as iArg is cast to a (void*) and made available +** as the user-data (sqlite3_user_data()) for the function. If +** argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set. +** +** AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal) +** Used to create an aggregate function definition implemented by +** the C functions xStep and xFinal. The first four parameters +** are interpreted in the same way as the first 4 parameters to +** FUNCTION(). +** +** LIKEFUNC(zName, nArg, pArg, flags) +** Used to create a scalar function definition of a function zName +** that accepts nArg arguments and is implemented by a call to C +** function likeFunc. Argument pArg is cast to a (void *) and made +** available as the function user-data (sqlite3_user_data()). The +** FuncDef.flags variable is set to the value passed as the flags +** parameter. +*/ +#define FUNCTION(zName, nArg, iArg, bNC, xFunc) \ + {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \ + SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0} +#define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \ + {nArg, SQLITE_UTF8, bNC*SQLITE_FUNC_NEEDCOLL, \ + pArg, 0, xFunc, 0, 0, #zName, 0, 0} +#define LIKEFUNC(zName, nArg, arg, flags) \ + {nArg, SQLITE_UTF8, flags, (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0} +#define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \ + {nArg, SQLITE_UTF8, nc*SQLITE_FUNC_NEEDCOLL, \ + SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0} + +/* +** All current savepoints are stored in a linked list starting at +** sqlite3.pSavepoint. The first element in the list is the most recently +** opened savepoint. Savepoints are added to the list by the vdbe +** OP_Savepoint instruction. +*/ +struct Savepoint { + char *zName; /* Savepoint name (nul-terminated) */ + i64 nDeferredCons; /* Number of deferred fk violations */ + Savepoint *pNext; /* Parent savepoint (if any) */ +}; + +/* +** The following are used as the second parameter to sqlite3Savepoint(), +** and as the P1 argument to the OP_Savepoint instruction. +*/ +#define SAVEPOINT_BEGIN 0 +#define SAVEPOINT_RELEASE 1 +#define SAVEPOINT_ROLLBACK 2 + + +/* +** Each SQLite module (virtual table definition) is defined by an +** instance of the following structure, stored in the sqlite3.aModule +** hash table. +*/ +struct Module { + const sqlite3_module *pModule; /* Callback pointers */ + const char *zName; /* Name passed to create_module() */ + void *pAux; /* pAux passed to create_module() */ + void (*xDestroy)(void *); /* Module destructor function */ +}; + +/* +** information about each column of an SQL table is held in an instance +** of this structure. +*/ +struct Column { + char *zName; /* Name of this column */ + Expr *pDflt; /* Default value of this column */ + char *zDflt; /* Original text of the default value */ + char *zType; /* Data type for this column */ + char *zColl; /* Collating sequence. If NULL, use the default */ + u8 notNull; /* True if there is a NOT NULL constraint */ + u8 isPrimKey; /* True if this column is part of the PRIMARY KEY */ + char affinity; /* One of the SQLITE_AFF_... values */ +#ifndef SQLITE_OMIT_VIRTUALTABLE + u8 isHidden; /* True if this column is 'hidden' */ +#endif +}; + +/* +** A "Collating Sequence" is defined by an instance of the following +** structure. Conceptually, a collating sequence consists of a name and +** a comparison routine that defines the order of that sequence. +** +** There may two separate implementations of the collation function, one +** that processes text in UTF-8 encoding (CollSeq.xCmp) and another that +** processes text encoded in UTF-16 (CollSeq.xCmp16), using the machine +** native byte order. When a collation sequence is invoked, SQLite selects +** the version that will require the least expensive encoding +** translations, if any. +** +** The CollSeq.pUser member variable is an extra parameter that passed in +** as the first argument to the UTF-8 comparison function, xCmp. +** CollSeq.pUser16 is the equivalent for the UTF-16 comparison function, +** xCmp16. +** +** If both CollSeq.xCmp and CollSeq.xCmp16 are NULL, it means that the +** collating sequence is undefined. Indices built on an undefined +** collating sequence may not be read or written. +*/ +struct CollSeq { + char *zName; /* Name of the collating sequence, UTF-8 encoded */ + u8 enc; /* Text encoding handled by xCmp() */ + u8 type; /* One of the SQLITE_COLL_... values below */ + void *pUser; /* First argument to xCmp() */ + int (*xCmp)(void*,int, const void*, int, const void*); + void (*xDel)(void*); /* Destructor for pUser */ +}; + +/* +** Allowed values of CollSeq.type: +*/ +#define SQLITE_COLL_BINARY 1 /* The default memcmp() collating sequence */ +#define SQLITE_COLL_NOCASE 2 /* The built-in NOCASE collating sequence */ +#define SQLITE_COLL_REVERSE 3 /* The built-in REVERSE collating sequence */ +#define SQLITE_COLL_USER 0 /* Any other user-defined collating sequence */ + +/* +** A sort order can be either ASC or DESC. +*/ +#define SQLITE_SO_ASC 0 /* Sort in ascending order */ +#define SQLITE_SO_DESC 1 /* Sort in ascending order */ + +/* +** Column affinity types. +** +** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and +** 't' for SQLITE_AFF_TEXT. But we can save a little space and improve +** the speed a little by numbering the values consecutively. +** +** But rather than start with 0 or 1, we begin with 'a'. That way, +** when multiple affinity types are concatenated into a string and +** used as the P4 operand, they will be more readable. +** +** Note also that the numeric types are grouped together so that testing +** for a numeric type is a single comparison. +*/ +#define SQLITE_AFF_TEXT 'a' +#define SQLITE_AFF_NONE 'b' +#define SQLITE_AFF_NUMERIC 'c' +#define SQLITE_AFF_INTEGER 'd' +#define SQLITE_AFF_REAL 'e' + +#define sqlite3IsNumericAffinity(X) ((X)>=SQLITE_AFF_NUMERIC) + +/* +** The SQLITE_AFF_MASK values masks off the significant bits of an +** affinity value. +*/ +#define SQLITE_AFF_MASK 0x67 + +/* +** Additional bit values that can be ORed with an affinity without +** changing the affinity. +*/ +#define SQLITE_JUMPIFNULL 0x08 /* jumps if either operand is NULL */ +#define SQLITE_STOREP2 0x10 /* Store result in reg[P2] rather than jump */ +#define SQLITE_NULLEQ 0x80 /* NULL=NULL */ + +/* +** An object of this type is created for each virtual table present in +** the database schema. +** +** If the database schema is shared, then there is one instance of this +** structure for each database connection (sqlite3*) that uses the shared +** schema. This is because each database connection requires its own unique +** instance of the sqlite3_vtab* handle used to access the virtual table +** implementation. sqlite3_vtab* handles can not be shared between +** database connections, even when the rest of the in-memory database +** schema is shared, as the implementation often stores the database +** connection handle passed to it via the xConnect() or xCreate() method +** during initialization internally. This database connection handle may +** then be used by the virtual table implementation to access real tables +** within the database. So that they appear as part of the callers +** transaction, these accesses need to be made via the same database +** connection as that used to execute SQL operations on the virtual table. +** +** All VTable objects that correspond to a single table in a shared +** database schema are initially stored in a linked-list pointed to by +** the Table.pVTable member variable of the corresponding Table object. +** When an sqlite3_prepare() operation is required to access the virtual +** table, it searches the list for the VTable that corresponds to the +** database connection doing the preparing so as to use the correct +** sqlite3_vtab* handle in the compiled query. +** +** When an in-memory Table object is deleted (for example when the +** schema is being reloaded for some reason), the VTable objects are not +** deleted and the sqlite3_vtab* handles are not xDisconnect()ed +** immediately. Instead, they are moved from the Table.pVTable list to +** another linked list headed by the sqlite3.pDisconnect member of the +** corresponding sqlite3 structure. They are then deleted/xDisconnected +** next time a statement is prepared using said sqlite3*. This is done +** to avoid deadlock issues involving multiple sqlite3.mutex mutexes. +** Refer to comments above function sqlite3VtabUnlockList() for an +** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect +** list without holding the corresponding sqlite3.mutex mutex. +** +** The memory for objects of this type is always allocated by +** sqlite3DbMalloc(), using the connection handle stored in VTable.db as +** the first argument. +*/ +struct VTable { + sqlite3 *db; /* Database connection associated with this table */ + Module *pMod; /* Pointer to module implementation */ + sqlite3_vtab *pVtab; /* Pointer to vtab instance */ + int nRef; /* Number of pointers to this structure */ + VTable *pNext; /* Next in linked list (see above) */ +}; + +/* +** Each SQL table is represented in memory by an instance of the +** following structure. +** +** Table.zName is the name of the table. The case of the original +** CREATE TABLE statement is stored, but case is not significant for +** comparisons. +** +** Table.nCol is the number of columns in this table. Table.aCol is a +** pointer to an array of Column structures, one for each column. +** +** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of +** the column that is that key. Otherwise Table.iPKey is negative. Note +** that the datatype of the PRIMARY KEY must be INTEGER for this field to +** be set. An INTEGER PRIMARY KEY is used as the rowid for each row of +** the table. If a table has no INTEGER PRIMARY KEY, then a random rowid +** is generated for each row of the table. TF_HasPrimaryKey is set if +** the table has any PRIMARY KEY, INTEGER or otherwise. +** +** Table.tnum is the page number for the root BTree page of the table in the +** database file. If Table.iDb is the index of the database table backend +** in sqlite.aDb[]. 0 is for the main database and 1 is for the file that +** holds temporary tables and indices. If TF_Ephemeral is set +** then the table is stored in a file that is automatically deleted +** when the VDBE cursor to the table is closed. In this case Table.tnum +** refers VDBE cursor number that holds the table open, not to the root +** page number. Transient tables are used to hold the results of a +** sub-query that appears instead of a real table name in the FROM clause +** of a SELECT statement. +*/ +struct Table { + char *zName; /* Name of the table or view */ + int iPKey; /* If not negative, use aCol[iPKey] as the primary key */ + int nCol; /* Number of columns in this table */ + Column *aCol; /* Information about each column */ + Index *pIndex; /* List of SQL indexes on this table. */ + int tnum; /* Root BTree node for this table (see note above) */ + unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */ + Select *pSelect; /* NULL for tables. Points to definition if a view. */ + u16 nRef; /* Number of pointers to this Table */ + u8 tabFlags; /* Mask of TF_* values */ + u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */ + FKey *pFKey; /* Linked list of all foreign keys in this table */ + char *zColAff; /* String defining the affinity of each column */ +#ifndef SQLITE_OMIT_CHECK + Expr *pCheck; /* The AND of all CHECK constraints */ +#endif +#ifndef SQLITE_OMIT_ALTERTABLE + int addColOffset; /* Offset in CREATE TABLE stmt to add a new column */ +#endif +#ifndef SQLITE_OMIT_VIRTUALTABLE + VTable *pVTable; /* List of VTable objects. */ + int nModuleArg; /* Number of arguments to the module */ + char **azModuleArg; /* Text of all module args. [0] is module name */ +#endif + Trigger *pTrigger; /* List of triggers stored in pSchema */ + Schema *pSchema; /* Schema that contains this table */ + Table *pNextZombie; /* Next on the Parse.pZombieTab list */ +}; + +/* +** Allowed values for Tabe.tabFlags. +*/ +#define TF_Readonly 0x01 /* Read-only system table */ +#define TF_Ephemeral 0x02 /* An ephemeral table */ +#define TF_HasPrimaryKey 0x04 /* Table has a primary key */ +#define TF_Autoincrement 0x08 /* Integer primary key is autoincrement */ +#define TF_Virtual 0x10 /* Is a virtual table */ +#define TF_NeedMetadata 0x20 /* aCol[].zType and aCol[].pColl missing */ + + + +/* +** Test to see whether or not a table is a virtual table. This is +** done as a macro so that it will be optimized out when virtual +** table support is omitted from the build. +*/ +#ifndef SQLITE_OMIT_VIRTUALTABLE +# define IsVirtual(X) (((X)->tabFlags & TF_Virtual)!=0) +# define IsHiddenColumn(X) ((X)->isHidden) +#else +# define IsVirtual(X) 0 +# define IsHiddenColumn(X) 0 +#endif + +/* +** Each foreign key constraint is an instance of the following structure. +** +** A foreign key is associated with two tables. The "from" table is +** the table that contains the REFERENCES clause that creates the foreign +** key. The "to" table is the table that is named in the REFERENCES clause. +** Consider this example: +** +** CREATE TABLE ex1( +** a INTEGER PRIMARY KEY, +** b INTEGER CONSTRAINT fk1 REFERENCES ex2(x) +** ); +** +** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2". +** +** Each REFERENCES clause generates an instance of the following structure +** which is attached to the from-table. The to-table need not exist when +** the from-table is created. The existence of the to-table is not checked. +*/ +struct FKey { + Table *pFrom; /* Table containing the REFERENCES clause (aka: Child) */ + FKey *pNextFrom; /* Next foreign key in pFrom */ + char *zTo; /* Name of table that the key points to (aka: Parent) */ + FKey *pNextTo; /* Next foreign key on table named zTo */ + FKey *pPrevTo; /* Previous foreign key on table named zTo */ + int nCol; /* Number of columns in this key */ + /* EV: R-30323-21917 */ + u8 isDeferred; /* True if constraint checking is deferred till COMMIT */ + u8 aAction[2]; /* ON DELETE and ON UPDATE actions, respectively */ + Trigger *apTrigger[2]; /* Triggers for aAction[] actions */ + struct sColMap { /* Mapping of columns in pFrom to columns in zTo */ + int iFrom; /* Index of column in pFrom */ + char *zCol; /* Name of column in zTo. If 0 use PRIMARY KEY */ + } aCol[1]; /* One entry for each of nCol column s */ +}; + +/* +** SQLite supports many different ways to resolve a constraint +** error. ROLLBACK processing means that a constraint violation +** causes the operation in process to fail and for the current transaction +** to be rolled back. ABORT processing means the operation in process +** fails and any prior changes from that one operation are backed out, +** but the transaction is not rolled back. FAIL processing means that +** the operation in progress stops and returns an error code. But prior +** changes due to the same operation are not backed out and no rollback +** occurs. IGNORE means that the particular row that caused the constraint +** error is not inserted or updated. Processing continues and no error +** is returned. REPLACE means that preexisting database rows that caused +** a UNIQUE constraint violation are removed so that the new insert or +** update can proceed. Processing continues and no error is reported. +** +** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys. +** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the +** same as ROLLBACK for DEFERRED keys. SETNULL means that the foreign +** key is set to NULL. CASCADE means that a DELETE or UPDATE of the +** referenced table row is propagated into the row that holds the +** foreign key. +** +** The following symbolic values are used to record which type +** of action to take. +*/ +#define OE_None 0 /* There is no constraint to check */ +#define OE_Rollback 1 /* Fail the operation and rollback the transaction */ +#define OE_Abort 2 /* Back out changes but do no rollback transaction */ +#define OE_Fail 3 /* Stop the operation but leave all prior changes */ +#define OE_Ignore 4 /* Ignore the error. Do not do the INSERT or UPDATE */ +#define OE_Replace 5 /* Delete existing record, then do INSERT or UPDATE */ + +#define OE_Restrict 6 /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */ +#define OE_SetNull 7 /* Set the foreign key value to NULL */ +#define OE_SetDflt 8 /* Set the foreign key value to its default */ +#define OE_Cascade 9 /* Cascade the changes */ + +#define OE_Default 99 /* Do whatever the default action is */ + +#endif +/* +** An instance of the following structure is passed as the first +** argument to sqlite3VdbeKeyCompare and is used to control the +** comparison of the two index keys. +*/ +struct KeyInfo { + sqlite3 *db; /* The database connection */ + u8 enc; /* Text encoding - one of the SQLITE_UTF* values */ + u16 nField; /* Number of entries in aColl[] */ + u8 *aSortOrder; /* Sort order for each column. May be NULL */ + CollSeq *aColl[1]; /* Collating sequence for each term of the key */ +}; + +/* +** An instance of the following structure holds information about a +** single index record that has already been parsed out into individual +** values. +** +** A record is an object that contains one or more fields of data. +** Records are used to store the content of a table row and to store +** the key of an index. A blob encoding of a record is created by +** the OP_MakeRecord opcode of the VDBE and is disassembled by the +** OP_Column opcode. +** +** This structure holds a record that has already been disassembled +** into its constituent fields. +*/ +struct UnpackedRecord { + KeyInfo *pKeyInfo; /* Collation and sort-order information */ + u16 nField; /* Number of entries in apMem[] */ + u16 flags; /* Boolean settings. UNPACKED_... below */ + i64 rowid; /* Used by UNPACKED_PREFIX_SEARCH */ + Mem *aMem; /* Values */ +}; + +/* +** Allowed values of UnpackedRecord.flags +*/ +#define UNPACKED_NEED_FREE 0x0001 /* Memory is from sqlite3Malloc() */ +#define UNPACKED_NEED_DESTROY 0x0002 /* apMem[]s should all be destroyed */ +#define UNPACKED_IGNORE_ROWID 0x0004 /* Ignore trailing rowid on key1 */ +#define UNPACKED_INCRKEY 0x0008 /* Make this key an epsilon larger */ +#define UNPACKED_PREFIX_MATCH 0x0010 /* A prefix match is considered OK */ +#define UNPACKED_PREFIX_SEARCH 0x0020 /* A prefix match is considered OK */ + +#if 0 + +/* +** Each SQL index is represented in memory by an +** instance of the following structure. +** +** The columns of the table that are to be indexed are described +** by the aiColumn[] field of this structure. For example, suppose +** we have the following table and index: +** +** CREATE TABLE Ex1(c1 int, c2 int, c3 text); +** CREATE INDEX Ex2 ON Ex1(c3,c1); +** +** In the Table structure describing Ex1, nCol==3 because there are +** three columns in the table. In the Index structure describing +** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed. +** The value of aiColumn is {2, 0}. aiColumn[0]==2 because the +** first column to be indexed (c3) has an index of 2 in Ex1.aCol[]. +** The second column to be indexed (c1) has an index of 0 in +** Ex1.aCol[], hence Ex2.aiColumn[1]==0. +** +** The Index.onError field determines whether or not the indexed columns +** must be unique and what to do if they are not. When Index.onError=OE_None, +** it means this is not a unique index. Otherwise it is a unique index +** and the value of Index.onError indicate the which conflict resolution +** algorithm to employ whenever an attempt is made to insert a non-unique +** element. +*/ +struct Index { + char *zName; /* Name of this index */ + int nColumn; /* Number of columns in the table used by this index */ + int *aiColumn; /* Which columns are used by this index. 1st is 0 */ + unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */ + Table *pTable; /* The SQL table being indexed */ + int tnum; /* Page containing root of this index in database file */ + u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */ + u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */ + char *zColAff; /* String defining the affinity of each column */ + Index *pNext; /* The next index associated with the same table */ + Schema *pSchema; /* Schema containing this index */ + u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */ + char **azColl; /* Array of collation sequence names for index */ + IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */ +}; + +/* +** Each sample stored in the sqlite_stat2 table is represented in memory +** using a structure of this type. +*/ +struct IndexSample { + union { + char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */ + double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */ + } u; + u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */ + u8 nByte; /* Size in byte of text or blob. */ +}; + +/* +** Each token coming out of the lexer is an instance of +** this structure. Tokens are also used as part of an expression. +** +** Note if Token.z==0 then Token.dyn and Token.n are undefined and +** may contain random values. Do not make any assumptions about Token.dyn +** and Token.n when Token.z==0. +*/ +struct Token { + const char *z; /* Text of the token. Not NULL-terminated! */ + unsigned int n; /* Number of characters in this token */ +}; + +/* +** An instance of this structure contains information needed to generate +** code for a SELECT that contains aggregate functions. +** +** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a +** pointer to this structure. The Expr.iColumn field is the index in +** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate +** code for that node. +** +** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the +** original Select structure that describes the SELECT statement. These +** fields do not need to be freed when deallocating the AggInfo structure. +*/ +struct AggInfo { + u8 directMode; /* Direct rendering mode means take data directly + ** from source tables rather than from accumulators */ + u8 useSortingIdx; /* In direct mode, reference the sorting index rather + ** than the source table */ + int sortingIdx; /* Cursor number of the sorting index */ + ExprList *pGroupBy; /* The group by clause */ + int nSortingColumn; /* Number of columns in the sorting index */ + struct AggInfo_col { /* For each column used in source tables */ + Table *pTab; /* Source table */ + int iTable; /* Cursor number of the source table */ + int iColumn; /* Column number within the source table */ + int iSorterColumn; /* Column number in the sorting index */ + int iMem; /* Memory location that acts as accumulator */ + Expr *pExpr; /* The original expression */ + } *aCol; + int nColumn; /* Number of used entries in aCol[] */ + int nColumnAlloc; /* Number of slots allocated for aCol[] */ + int nAccumulator; /* Number of columns that show through to the output. + ** Additional columns are used only as parameters to + ** aggregate functions */ + struct AggInfo_func { /* For each aggregate function */ + Expr *pExpr; /* Expression encoding the function */ + FuncDef *pFunc; /* The aggregate function implementation */ + int iMem; /* Memory location that acts as accumulator */ + int iDistinct; /* Ephemeral table used to enforce DISTINCT */ + } *aFunc; + int nFunc; /* Number of entries in aFunc[] */ + int nFuncAlloc; /* Number of slots allocated for aFunc[] */ +}; + +/* +** The datatype ynVar is a signed integer, either 16-bit or 32-bit. +** Usually it is 16-bits. But if SQLITE_MAX_VARIABLE_NUMBER is greater +** than 32767 we have to make it 32-bit. 16-bit is preferred because +** it uses less memory in the Expr object, which is a big memory user +** in systems with lots of prepared statements. And few applications +** need more than about 10 or 20 variables. But some extreme users want +** to have prepared statements with over 32767 variables, and for them +** the option is available (at compile-time). +*/ +#if SQLITE_MAX_VARIABLE_NUMBER<=32767 +typedef i16 ynVar; +#else +typedef int ynVar; +#endif + +/* +** Each node of an expression in the parse tree is an instance +** of this structure. +** +** Expr.op is the opcode. The integer parser token codes are reused +** as opcodes here. For example, the parser defines TK_GE to be an integer +** code representing the ">=" operator. This same integer code is reused +** to represent the greater-than-or-equal-to operator in the expression +** tree. +** +** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB, +** or TK_STRING), then Expr.token contains the text of the SQL literal. If +** the expression is a variable (TK_VARIABLE), then Expr.token contains the +** variable name. Finally, if the expression is an SQL function (TK_FUNCTION), +** then Expr.token contains the name of the function. +** +** Expr.pRight and Expr.pLeft are the left and right subexpressions of a +** binary operator. Either or both may be NULL. +** +** Expr.x.pList is a list of arguments if the expression is an SQL function, +** a CASE expression or an IN expression of the form " IN (, ...)". +** Expr.x.pSelect is used if the expression is a sub-select or an expression of +** the form " IN (SELECT ...)". If the EP_xIsSelect bit is set in the +** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is +** valid. +** +** An expression of the form ID or ID.ID refers to a column in a table. +** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is +** the integer cursor number of a VDBE cursor pointing to that table and +** Expr.iColumn is the column number for the specific column. If the +** expression is used as a result in an aggregate SELECT, then the +** value is also stored in the Expr.iAgg column in the aggregate so that +** it can be accessed after all aggregates are computed. +** +** If the expression is an unbound variable marker (a question mark +** character '?' in the original SQL) then the Expr.iTable holds the index +** number for that variable. +** +** If the expression is a subquery then Expr.iColumn holds an integer +** register number containing the result of the subquery. If the +** subquery gives a constant result, then iTable is -1. If the subquery +** gives a different answer at different times during statement processing +** then iTable is the address of a subroutine that computes the subquery. +** +** If the Expr is of type OP_Column, and the table it is selecting from +** is a disk table or the "old.*" pseudo-table, then pTab points to the +** corresponding table definition. +** +** ALLOCATION NOTES: +** +** Expr objects can use a lot of memory space in database schema. To +** help reduce memory requirements, sometimes an Expr object will be +** truncated. And to reduce the number of memory allocations, sometimes +** two or more Expr objects will be stored in a single memory allocation, +** together with Expr.zToken strings. +** +** If the EP_Reduced and EP_TokenOnly flags are set when +** an Expr object is truncated. When EP_Reduced is set, then all +** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees +** are contained within the same memory allocation. Note, however, that +** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately +** allocated, regardless of whether or not EP_Reduced is set. +*/ +struct Expr { + u8 op; /* Operation performed by this node */ + char affinity; /* The affinity of the column or 0 if not a column */ + u16 flags; /* Various flags. EP_* See below */ + union { + char *zToken; /* Token value. Zero terminated and dequoted */ + int iValue; /* Non-negative integer value if EP_IntValue */ + } u; + + /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no + ** space is allocated for the fields below this point. An attempt to + ** access them will result in a segfault or malfunction. + *********************************************************************/ + + Expr *pLeft; /* Left subnode */ + Expr *pRight; /* Right subnode */ + union { + ExprList *pList; /* Function arguments or in " IN ( IN (